ctree.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #ifndef __BTRFS__
  2. #define __BTRFS__
  3. #include "list.h"
  4. #include "kerncompat.h"
  5. #define BTRFS_BLOCKSIZE 1024
  6. /*
  7. * the key defines the order in the tree, and so it also defines (optimal)
  8. * block layout. objectid corresonds to the inode number. The flags
  9. * tells us things about the object, and is a kind of stream selector.
  10. * so for a given inode, keys with flags of 1 might refer to the inode
  11. * data, flags of 2 may point to file data in the btree and flags == 3
  12. * may point to extents.
  13. *
  14. * offset is the starting byte offset for this key in the stream.
  15. *
  16. * btrfs_disk_key is in disk byte order. struct btrfs_key is always
  17. * in cpu native order. Otherwise they are identical and their sizes
  18. * should be the same (ie both packed)
  19. */
  20. struct btrfs_disk_key {
  21. __le64 objectid;
  22. __le32 flags;
  23. __le64 offset;
  24. } __attribute__ ((__packed__));
  25. struct btrfs_key {
  26. u64 objectid;
  27. u32 flags;
  28. u64 offset;
  29. } __attribute__ ((__packed__));
  30. /*
  31. * every tree block (leaf or node) starts with this header.
  32. */
  33. struct btrfs_header {
  34. __le64 fsid[2]; /* FS specific uuid */
  35. __le64 blocknr; /* which block this node is supposed to live in */
  36. __le64 parentid; /* objectid of the tree root */
  37. __le32 csum;
  38. __le32 ham;
  39. __le16 nritems;
  40. __le16 flags;
  41. /* generation flags to be added */
  42. } __attribute__ ((__packed__));
  43. #define BTRFS_MAX_LEVEL 8
  44. #define NODEPTRS_PER_BLOCK ((BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)) / \
  45. (sizeof(struct btrfs_disk_key) + sizeof(u64)))
  46. struct btrfs_buffer;
  47. /*
  48. * in ram representation of the tree. extent_root is used for all allocations
  49. * and for the extent tree extent_root root. current_insert is used
  50. * only for the extent tree.
  51. */
  52. struct btrfs_root {
  53. struct btrfs_buffer *node;
  54. struct btrfs_buffer *commit_root;
  55. struct btrfs_root *extent_root;
  56. struct btrfs_key current_insert;
  57. struct btrfs_key last_insert;
  58. int fp;
  59. struct radix_tree_root cache_radix;
  60. struct radix_tree_root pinned_radix;
  61. struct list_head trans;
  62. struct list_head cache;
  63. int cache_size;
  64. };
  65. /*
  66. * describes a tree on disk
  67. */
  68. struct btrfs_root_info {
  69. u64 fsid[2]; /* FS specific uuid */
  70. u64 blocknr; /* blocknr of this block */
  71. u64 objectid; /* inode number of this root */
  72. u64 tree_root; /* the tree root block */
  73. u32 csum;
  74. u32 ham;
  75. u64 snapuuid[2]; /* root specific uuid */
  76. } __attribute__ ((__packed__));
  77. /*
  78. * the super block basically lists the main trees of the FS
  79. * it currently lacks any block count etc etc
  80. */
  81. struct btrfs_super_block {
  82. struct btrfs_root_info root_info;
  83. struct btrfs_root_info extent_info;
  84. } __attribute__ ((__packed__));
  85. /*
  86. * A leaf is full of items. The exact type of item is defined by
  87. * the key flags parameter. offset and size tell us where to find
  88. * the item in the leaf (relative to the start of the data area)
  89. */
  90. struct btrfs_item {
  91. struct btrfs_disk_key key;
  92. __le16 offset;
  93. __le16 size;
  94. } __attribute__ ((__packed__));
  95. /*
  96. * leaves have an item area and a data area:
  97. * [item0, item1....itemN] [free space] [dataN...data1, data0]
  98. *
  99. * The data is separate from the items to get the keys closer together
  100. * during searches.
  101. */
  102. #define LEAF_DATA_SIZE (BTRFS_BLOCKSIZE - sizeof(struct btrfs_header))
  103. struct btrfs_leaf {
  104. struct btrfs_header header;
  105. union {
  106. struct btrfs_item items[LEAF_DATA_SIZE/
  107. sizeof(struct btrfs_item)];
  108. u8 data[BTRFS_BLOCKSIZE - sizeof(struct btrfs_header)];
  109. };
  110. } __attribute__ ((__packed__));
  111. /*
  112. * all non-leaf blocks are nodes, they hold only keys and pointers to
  113. * other blocks
  114. */
  115. struct btrfs_node {
  116. struct btrfs_header header;
  117. struct btrfs_disk_key keys[NODEPTRS_PER_BLOCK];
  118. __le64 blockptrs[NODEPTRS_PER_BLOCK];
  119. } __attribute__ ((__packed__));
  120. /*
  121. * items in the extent btree are used to record the objectid of the
  122. * owner of the block and the number of references
  123. */
  124. struct btrfs_extent_item {
  125. __le32 refs;
  126. __le64 owner;
  127. } __attribute__ ((__packed__));
  128. /*
  129. * btrfs_paths remember the path taken from the root down to the leaf.
  130. * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
  131. * to any other levels that are present.
  132. *
  133. * The slots array records the index of the item or block pointer
  134. * used while walking the tree.
  135. */
  136. struct btrfs_path {
  137. struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
  138. int slots[BTRFS_MAX_LEVEL];
  139. };
  140. static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
  141. {
  142. return le64_to_cpu(ei->owner);
  143. }
  144. static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
  145. {
  146. ei->owner = cpu_to_le64(val);
  147. }
  148. static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
  149. {
  150. return le32_to_cpu(ei->refs);
  151. }
  152. static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
  153. {
  154. ei->refs = cpu_to_le32(val);
  155. }
  156. static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
  157. {
  158. return le64_to_cpu(n->blockptrs[nr]);
  159. }
  160. static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
  161. u64 val)
  162. {
  163. n->blockptrs[nr] = cpu_to_le64(val);
  164. }
  165. static inline u16 btrfs_item_offset(struct btrfs_item *item)
  166. {
  167. return le16_to_cpu(item->offset);
  168. }
  169. static inline void btrfs_set_item_offset(struct btrfs_item *item, u16 val)
  170. {
  171. item->offset = cpu_to_le16(val);
  172. }
  173. static inline u16 btrfs_item_end(struct btrfs_item *item)
  174. {
  175. return le16_to_cpu(item->offset) + le16_to_cpu(item->size);
  176. }
  177. static inline u16 btrfs_item_size(struct btrfs_item *item)
  178. {
  179. return le16_to_cpu(item->size);
  180. }
  181. static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
  182. {
  183. item->size = cpu_to_le16(val);
  184. }
  185. static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
  186. struct btrfs_disk_key *disk)
  187. {
  188. cpu->offset = le64_to_cpu(disk->offset);
  189. cpu->flags = le32_to_cpu(disk->flags);
  190. cpu->objectid = le64_to_cpu(disk->objectid);
  191. }
  192. static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
  193. struct btrfs_key *cpu)
  194. {
  195. disk->offset = cpu_to_le64(cpu->offset);
  196. disk->flags = cpu_to_le32(cpu->flags);
  197. disk->objectid = cpu_to_le64(cpu->objectid);
  198. }
  199. static inline u64 btrfs_key_objectid(struct btrfs_disk_key *disk)
  200. {
  201. return le64_to_cpu(disk->objectid);
  202. }
  203. static inline void btrfs_set_key_objectid(struct btrfs_disk_key *disk,
  204. u64 val)
  205. {
  206. disk->objectid = cpu_to_le64(val);
  207. }
  208. static inline u64 btrfs_key_offset(struct btrfs_disk_key *disk)
  209. {
  210. return le64_to_cpu(disk->offset);
  211. }
  212. static inline void btrfs_set_key_offset(struct btrfs_disk_key *disk,
  213. u64 val)
  214. {
  215. disk->offset = cpu_to_le64(val);
  216. }
  217. static inline u32 btrfs_key_flags(struct btrfs_disk_key *disk)
  218. {
  219. return le32_to_cpu(disk->flags);
  220. }
  221. static inline void btrfs_set_key_flags(struct btrfs_disk_key *disk,
  222. u32 val)
  223. {
  224. disk->flags = cpu_to_le32(val);
  225. }
  226. static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
  227. {
  228. return le64_to_cpu(h->blocknr);
  229. }
  230. static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
  231. {
  232. h->blocknr = cpu_to_le64(blocknr);
  233. }
  234. static inline u64 btrfs_header_parentid(struct btrfs_header *h)
  235. {
  236. return le64_to_cpu(h->parentid);
  237. }
  238. static inline void btrfs_set_header_parentid(struct btrfs_header *h,
  239. u64 parentid)
  240. {
  241. h->parentid = cpu_to_le64(parentid);
  242. }
  243. static inline u16 btrfs_header_nritems(struct btrfs_header *h)
  244. {
  245. return le16_to_cpu(h->nritems);
  246. }
  247. static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
  248. {
  249. h->nritems = cpu_to_le16(val);
  250. }
  251. static inline u16 btrfs_header_flags(struct btrfs_header *h)
  252. {
  253. return le16_to_cpu(h->flags);
  254. }
  255. static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
  256. {
  257. h->flags = cpu_to_le16(val);
  258. }
  259. static inline int btrfs_header_level(struct btrfs_header *h)
  260. {
  261. return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
  262. }
  263. static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
  264. {
  265. u16 flags;
  266. BUG_ON(level > BTRFS_MAX_LEVEL);
  267. flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
  268. btrfs_set_header_flags(h, flags | level);
  269. }
  270. static inline int btrfs_is_leaf(struct btrfs_node *n)
  271. {
  272. return (btrfs_header_level(&n->header) == 0);
  273. }
  274. struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
  275. int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
  276. int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks);
  277. int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
  278. struct btrfs_path *p, int ins_len, int cow);
  279. void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
  280. void btrfs_init_path(struct btrfs_path *p);
  281. int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path);
  282. int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key,
  283. void *data, int data_size);
  284. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
  285. int btrfs_leaf_free_space(struct btrfs_leaf *leaf);
  286. int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap);
  287. int btrfs_finish_extent_commit(struct btrfs_root *root);
  288. #endif