sheepdog.h:vnode
vnode的数据结构:
1 struct sd_vnode { 2 struct rb_node rb; 3 const struct sd_node *node; 4 uint64_t hash; 5 }; 6 7 struct vnode_info { 8 struct rb_root vroot; // vnode的root ? 9 struct rb_root nroot; // node的root ? 10 int nr_nodes; 11 int nr_zones; 12 refcnt_t refcnt; 13 };
oid和vnode的映射关系:
1 /* If v1_hash < oid_hash <= v2_hash, then oid is resident on v2 根据oid,从root出发查找第一个合适的位置 */ 2 static inline struct sd_vnode * 3 oid_to_first_vnode(uint64_t oid, struct rb_root *root) 4 { 5 struct sd_vnode dummy = { 6 .hash = sd_hash_oid(oid), 7 }; 8 return rb_nsearch(root, &dummy, rb, vnode_cmp); 9 } 10 /* oid向vnode映射: 原则是先找到第一个存放的位置,随后的位置按照next向下找,只要在不同的zone下,都可以 */ 11 /* Replica are placed along the ring one by one with different zones */ 12 static inline void oid_to_vnodes(uint64_t oid, struct rb_root *root, 13 int nr_copies, 14 const struct sd_vnode **vnodes) 15 { 16 const struct sd_vnode *next = oid_to_first_vnode(oid, root); 17 18 vnodes[0] = next; 19 for (int i = 1; i < nr_copies; i++) { 20 next: 21 next = rb_entry(rb_next(&next->rb), struct sd_vnode, rb); 22 if (!next) /* Wrap around */ 23 next = rb_entry(rb_first(root), struct sd_vnode, rb); 24 if (unlikely(next == vnodes[0])) 25 panic("can't find a valid vnode"); 26 for (int j = 0; j < i; j++) 27 if (same_zone(vnodes[j], next)) // 逐个比较已分配的vnode,不能在同一个分区zone内 28 goto next; 29 vnodes[i] = next; 30 } 31 } 32 33 static inline const struct sd_vnode * 34 oid_to_vnode(uint64_t oid, struct rb_root *root, int copy_idx) 35 { 36 const struct sd_vnode *vnodes[SD_MAX_COPIES]; 37 38 oid_to_vnodes(oid, root, copy_idx + 1, vnodes); 39 40 return vnodes[copy_idx]; 41 } 42 /* 为一个oid分配copy_idx个vnode */ 43 static inline const struct sd_node * 44 oid_to_node(uint64_t oid, struct rb_root *root, int copy_idx) 45 { 46 const struct sd_vnode *vnode; 47 48 vnode = oid_to_vnode(oid, root, copy_idx); 49 50 return vnode->node; 51 } 52 53 static inline void oid_to_nodes(uint64_t oid, struct rb_root *root, 54 int nr_copies, 55 const struct sd_node **nodes) 56 { 57 const struct sd_vnode *vnodes[SD_MAX_COPIES]; 58 59 oid_to_vnodes(oid, root, nr_copies, vnodes); 60 for (int i = 0; i < nr_copies; i++) 61 nodes[i] = vnodes[i]->node; 62 }
物理node的结构体:
1 struct sd_node { 2 struct rb_node rb; // 存储node信息的rbtree 3 struct node_id nid; // nodeid 4 uint16_t nr_vnodes; // 对应vnode的个数 5 uint32_t zone; // zone的id ? 6 uint64_t space; // ? 7 #ifdef HAVE_DISKVNODES 8 #define SD_MAX_NODES 830 // vdisk mode下最大支持节点个数 9 #define SD_NODE_SIZE (80 + sizeof(struct disk_info) * DISK_MAX) 10 struct disk_info disks[DISK_MAX]; // 节点下物理磁盘的个数,最大支持32盘 11 #else // 非vdisk mode模式 12 #define SD_MAX_NODES 6144 // 最大节点个数 13 #define SD_NODE_SIZE 80 // 该模式下所有的磁盘一视同仁,并没有vdisk的管理和disk管理 14 struct disk_info disks[0]; 15 #endif 16 };