ctree.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #ifndef __CTREE__
  2. #define __CTREE__
  3. #include "list.h"
  4. #define CTREE_BLOCKSIZE 1024
  5. /*
  6. * the key defines the order in the tree, and so it also defines (optimal)
  7. * block layout. objectid corresonds to the inode number. The flags
  8. * tells us things about the object, and is a kind of stream selector.
  9. * so for a given inode, keys with flags of 1 might refer to the inode
  10. * data, flags of 2 may point to file data in the btree and flags == 3
  11. * may point to extents.
  12. *
  13. * offset is the starting byte offset for this key in the stream.
  14. */
  15. struct key {
  16. u64 objectid;
  17. u32 flags;
  18. u64 offset;
  19. } __attribute__ ((__packed__));
  20. /*
  21. * every tree block (leaf or node) starts with this header.
  22. */
  23. struct btrfs_header {
  24. __le64 fsid[2]; /* FS specific uuid */
  25. __le64 blocknr; /* which block this node is supposed to live in */
  26. __le64 parentid; /* objectid of the tree root */
  27. __le32 csum;
  28. __le32 ham;
  29. __le16 nritems;
  30. __le16 flags;
  31. /* generation flags to be added */
  32. } __attribute__ ((__packed__));
  33. #define MAX_LEVEL 8
  34. #define NODEPTRS_PER_BLOCK ((CTREE_BLOCKSIZE - sizeof(struct btrfs_header)) / \
  35. (sizeof(struct key) + sizeof(u64)))
  36. struct tree_buffer;
  37. /*
  38. * in ram representation of the tree. extent_root is used for all allocations
  39. * and for the extent tree extent_root root. current_insert is used
  40. * only for the extent tree.
  41. */
  42. struct ctree_root {
  43. struct tree_buffer *node;
  44. struct tree_buffer *commit_root;
  45. struct ctree_root *extent_root;
  46. struct key current_insert;
  47. struct key last_insert;
  48. int fp;
  49. struct radix_tree_root cache_radix;
  50. struct radix_tree_root pinned_radix;
  51. struct list_head trans;
  52. struct list_head cache;
  53. int cache_size;
  54. };
  55. /*
  56. * describes a tree on disk
  57. */
  58. struct ctree_root_info {
  59. u64 fsid[2]; /* FS specific uuid */
  60. u64 blocknr; /* blocknr of this block */
  61. u64 objectid; /* inode number of this root */
  62. u64 tree_root; /* the tree root block */
  63. u32 csum;
  64. u32 ham;
  65. u64 snapuuid[2]; /* root specific uuid */
  66. } __attribute__ ((__packed__));
  67. /*
  68. * the super block basically lists the main trees of the FS
  69. * it currently lacks any block count etc etc
  70. */
  71. struct ctree_super_block {
  72. struct ctree_root_info root_info;
  73. struct ctree_root_info extent_info;
  74. } __attribute__ ((__packed__));
  75. /*
  76. * A leaf is full of items. The exact type of item is defined by
  77. * the key flags parameter. offset and size tell us where to find
  78. * the item in the leaf (relative to the start of the data area)
  79. */
  80. struct item {
  81. struct key key;
  82. u16 offset;
  83. u16 size;
  84. } __attribute__ ((__packed__));
  85. /*
  86. * leaves have an item area and a data area:
  87. * [item0, item1....itemN] [free space] [dataN...data1, data0]
  88. *
  89. * The data is separate from the items to get the keys closer together
  90. * during searches.
  91. */
  92. #define LEAF_DATA_SIZE (CTREE_BLOCKSIZE - sizeof(struct btrfs_header))
  93. struct leaf {
  94. struct btrfs_header header;
  95. union {
  96. struct item items[LEAF_DATA_SIZE/sizeof(struct item)];
  97. u8 data[CTREE_BLOCKSIZE-sizeof(struct btrfs_header)];
  98. };
  99. } __attribute__ ((__packed__));
  100. /*
  101. * all non-leaf blocks are nodes, they hold only keys and pointers to
  102. * other blocks
  103. */
  104. struct node {
  105. struct btrfs_header header;
  106. struct key keys[NODEPTRS_PER_BLOCK];
  107. u64 blockptrs[NODEPTRS_PER_BLOCK];
  108. } __attribute__ ((__packed__));
  109. /*
  110. * items in the extent btree are used to record the objectid of the
  111. * owner of the block and the number of references
  112. */
  113. struct extent_item {
  114. u32 refs;
  115. u64 owner;
  116. } __attribute__ ((__packed__));
  117. /*
  118. * ctree_paths remember the path taken from the root down to the leaf.
  119. * level 0 is always the leaf, and nodes[1...MAX_LEVEL] will point
  120. * to any other levels that are present.
  121. *
  122. * The slots array records the index of the item or block pointer
  123. * used while walking the tree.
  124. */
  125. struct ctree_path {
  126. struct tree_buffer *nodes[MAX_LEVEL];
  127. int slots[MAX_LEVEL];
  128. };
  129. static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
  130. {
  131. return le64_to_cpu(h->blocknr);
  132. }
  133. static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
  134. {
  135. h->blocknr = cpu_to_le64(blocknr);
  136. }
  137. static inline u64 btrfs_header_parentid(struct btrfs_header *h)
  138. {
  139. return le64_to_cpu(h->parentid);
  140. }
  141. static inline void btrfs_set_header_parentid(struct btrfs_header *h,
  142. u64 parentid)
  143. {
  144. h->parentid = cpu_to_le64(parentid);
  145. }
  146. static inline u16 btrfs_header_nritems(struct btrfs_header *h)
  147. {
  148. return le16_to_cpu(h->nritems);
  149. }
  150. static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
  151. {
  152. h->nritems = cpu_to_le16(val);
  153. }
  154. static inline u16 btrfs_header_flags(struct btrfs_header *h)
  155. {
  156. return le16_to_cpu(h->flags);
  157. }
  158. static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
  159. {
  160. h->flags = cpu_to_le16(val);
  161. }
  162. static inline int btrfs_header_level(struct btrfs_header *h)
  163. {
  164. return btrfs_header_flags(h) & (MAX_LEVEL - 1);
  165. }
  166. static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
  167. {
  168. u16 flags;
  169. BUG_ON(level > MAX_LEVEL);
  170. flags = btrfs_header_flags(h) & ~(MAX_LEVEL - 1);
  171. btrfs_set_header_flags(h, flags | level);
  172. }
  173. static inline int btrfs_is_leaf(struct node *n)
  174. {
  175. return (btrfs_header_level(&n->header) == 0);
  176. }
  177. struct tree_buffer *alloc_free_block(struct ctree_root *root);
  178. int btrfs_inc_ref(struct ctree_root *root, struct tree_buffer *buf);
  179. int free_extent(struct ctree_root *root, u64 blocknr, u64 num_blocks);
  180. int search_slot(struct ctree_root *root, struct key *key, struct ctree_path *p, int ins_len, int cow);
  181. void release_path(struct ctree_root *root, struct ctree_path *p);
  182. void init_path(struct ctree_path *p);
  183. int del_item(struct ctree_root *root, struct ctree_path *path);
  184. int insert_item(struct ctree_root *root, struct key *key, void *data, int data_size);
  185. int next_leaf(struct ctree_root *root, struct ctree_path *path);
  186. int leaf_free_space(struct leaf *leaf);
  187. int btrfs_drop_snapshot(struct ctree_root *root, struct tree_buffer *snap);
  188. int btrfs_finish_extent_commit(struct ctree_root *root);
  189. #endif