ctree.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #ifndef __BTRFS__
  2. #define __BTRFS__
  3. #include "list.h"
  4. #include "kerncompat.h"
  5. #define BTRFS_MAGIC "_BtRfS_M"
  6. #define BTRFS_ROOT_TREE_OBJECTID 1
  7. #define BTRFS_EXTENT_TREE_OBJECTID 2
  8. #define BTRFS_FS_TREE_OBJECTID 3
  9. /*
  10. * the key defines the order in the tree, and so it also defines (optimal)
  11. * block layout. objectid corresonds to the inode number. The flags
  12. * tells us things about the object, and is a kind of stream selector.
  13. * so for a given inode, keys with flags of 1 might refer to the inode
  14. * data, flags of 2 may point to file data in the btree and flags == 3
  15. * may point to extents.
  16. *
  17. * offset is the starting byte offset for this key in the stream.
  18. *
  19. * btrfs_disk_key is in disk byte order. struct btrfs_key is always
  20. * in cpu native order. Otherwise they are identical and their sizes
  21. * should be the same (ie both packed)
  22. */
  23. struct btrfs_disk_key {
  24. __le64 objectid;
  25. __le64 offset;
  26. __le32 flags;
  27. } __attribute__ ((__packed__));
  28. struct btrfs_key {
  29. u64 objectid;
  30. u64 offset;
  31. u32 flags;
  32. } __attribute__ ((__packed__));
  33. /*
  34. * every tree block (leaf or node) starts with this header.
  35. */
  36. struct btrfs_header {
  37. u8 fsid[16]; /* FS specific uuid */
  38. __le64 blocknr; /* which block this node is supposed to live in */
  39. __le64 parentid; /* objectid of the tree root */
  40. __le32 csum;
  41. __le32 ham;
  42. __le16 nritems;
  43. __le16 flags;
  44. /* generation flags to be added */
  45. } __attribute__ ((__packed__));
  46. #define BTRFS_MAX_LEVEL 8
  47. #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
  48. sizeof(struct btrfs_header)) / \
  49. (sizeof(struct btrfs_disk_key) + sizeof(u64)))
  50. #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
  51. #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
  52. struct btrfs_buffer;
  53. struct btrfs_root_item {
  54. __le64 blocknr;
  55. __le32 flags;
  56. __le64 block_limit;
  57. __le64 blocks_used;
  58. __le32 refs;
  59. };
  60. /*
  61. * in ram representation of the tree. extent_root is used for all allocations
  62. * and for the extent tree extent_root root. current_insert is used
  63. * only for the extent tree.
  64. */
  65. struct btrfs_root {
  66. struct btrfs_buffer *node;
  67. struct btrfs_buffer *commit_root;
  68. struct btrfs_root *extent_root;
  69. struct btrfs_root *tree_root;
  70. struct btrfs_key current_insert;
  71. struct btrfs_key last_insert;
  72. int fp;
  73. struct radix_tree_root cache_radix;
  74. struct radix_tree_root pinned_radix;
  75. struct list_head trans;
  76. struct list_head cache;
  77. int cache_size;
  78. int ref_cows;
  79. struct btrfs_root_item root_item;
  80. struct btrfs_key root_key;
  81. u32 blocksize;
  82. };
  83. /*
  84. * the super block basically lists the main trees of the FS
  85. * it currently lacks any block count etc etc
  86. */
  87. struct btrfs_super_block {
  88. u8 fsid[16]; /* FS specific uuid */
  89. __le64 blocknr; /* this block number */
  90. __le32 csum;
  91. __le64 magic;
  92. __le32 blocksize;
  93. __le64 generation;
  94. __le64 root;
  95. __le64 total_blocks;
  96. __le64 blocks_used;
  97. } __attribute__ ((__packed__));
  98. /*
  99. * A leaf is full of items. The exact type of item is defined by
  100. * the key flags parameter. offset and size tell us where to find
  101. * the item in the leaf (relative to the start of the data area)
  102. */
  103. struct btrfs_item {
  104. struct btrfs_disk_key key;
  105. __le32 offset;
  106. __le16 size;
  107. } __attribute__ ((__packed__));
  108. /*
  109. * leaves have an item area and a data area:
  110. * [item0, item1....itemN] [free space] [dataN...data1, data0]
  111. *
  112. * The data is separate from the items to get the keys closer together
  113. * during searches.
  114. */
  115. struct btrfs_leaf {
  116. struct btrfs_header header;
  117. struct btrfs_item items[];
  118. } __attribute__ ((__packed__));
  119. /*
  120. * all non-leaf blocks are nodes, they hold only keys and pointers to
  121. * other blocks
  122. */
  123. struct btrfs_key_ptr {
  124. struct btrfs_disk_key key;
  125. __le64 blockptr;
  126. } __attribute__ ((__packed__));
  127. struct btrfs_node {
  128. struct btrfs_header header;
  129. struct btrfs_key_ptr ptrs[];
  130. } __attribute__ ((__packed__));
  131. /*
  132. * items in the extent btree are used to record the objectid of the
  133. * owner of the block and the number of references
  134. */
  135. struct btrfs_extent_item {
  136. __le32 refs;
  137. __le64 owner;
  138. } __attribute__ ((__packed__));
  139. /*
  140. * btrfs_paths remember the path taken from the root down to the leaf.
  141. * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
  142. * to any other levels that are present.
  143. *
  144. * The slots array records the index of the item or block pointer
  145. * used while walking the tree.
  146. */
  147. struct btrfs_path {
  148. struct btrfs_buffer *nodes[BTRFS_MAX_LEVEL];
  149. int slots[BTRFS_MAX_LEVEL];
  150. };
  151. static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
  152. {
  153. return le64_to_cpu(ei->owner);
  154. }
  155. static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
  156. {
  157. ei->owner = cpu_to_le64(val);
  158. }
  159. static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
  160. {
  161. return le32_to_cpu(ei->refs);
  162. }
  163. static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
  164. {
  165. ei->refs = cpu_to_le32(val);
  166. }
  167. static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
  168. {
  169. return le64_to_cpu(n->ptrs[nr].blockptr);
  170. }
  171. static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
  172. u64 val)
  173. {
  174. n->ptrs[nr].blockptr = cpu_to_le64(val);
  175. }
  176. static inline u32 btrfs_item_offset(struct btrfs_item *item)
  177. {
  178. return le32_to_cpu(item->offset);
  179. }
  180. static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
  181. {
  182. item->offset = cpu_to_le32(val);
  183. }
  184. static inline u32 btrfs_item_end(struct btrfs_item *item)
  185. {
  186. return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
  187. }
  188. static inline u16 btrfs_item_size(struct btrfs_item *item)
  189. {
  190. return le16_to_cpu(item->size);
  191. }
  192. static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
  193. {
  194. item->size = cpu_to_le16(val);
  195. }
  196. static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
  197. struct btrfs_disk_key *disk)
  198. {
  199. cpu->offset = le64_to_cpu(disk->offset);
  200. cpu->flags = le32_to_cpu(disk->flags);
  201. cpu->objectid = le64_to_cpu(disk->objectid);
  202. }
  203. static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
  204. struct btrfs_key *cpu)
  205. {
  206. disk->offset = cpu_to_le64(cpu->offset);
  207. disk->flags = cpu_to_le32(cpu->flags);
  208. disk->objectid = cpu_to_le64(cpu->objectid);
  209. }
  210. static inline u64 btrfs_key_objectid(struct btrfs_disk_key *disk)
  211. {
  212. return le64_to_cpu(disk->objectid);
  213. }
  214. static inline void btrfs_set_key_objectid(struct btrfs_disk_key *disk,
  215. u64 val)
  216. {
  217. disk->objectid = cpu_to_le64(val);
  218. }
  219. static inline u64 btrfs_key_offset(struct btrfs_disk_key *disk)
  220. {
  221. return le64_to_cpu(disk->offset);
  222. }
  223. static inline void btrfs_set_key_offset(struct btrfs_disk_key *disk,
  224. u64 val)
  225. {
  226. disk->offset = cpu_to_le64(val);
  227. }
  228. static inline u32 btrfs_key_flags(struct btrfs_disk_key *disk)
  229. {
  230. return le32_to_cpu(disk->flags);
  231. }
  232. static inline void btrfs_set_key_flags(struct btrfs_disk_key *disk,
  233. u32 val)
  234. {
  235. disk->flags = cpu_to_le32(val);
  236. }
  237. static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
  238. {
  239. return le64_to_cpu(h->blocknr);
  240. }
  241. static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
  242. {
  243. h->blocknr = cpu_to_le64(blocknr);
  244. }
  245. static inline u64 btrfs_header_parentid(struct btrfs_header *h)
  246. {
  247. return le64_to_cpu(h->parentid);
  248. }
  249. static inline void btrfs_set_header_parentid(struct btrfs_header *h,
  250. u64 parentid)
  251. {
  252. h->parentid = cpu_to_le64(parentid);
  253. }
  254. static inline u16 btrfs_header_nritems(struct btrfs_header *h)
  255. {
  256. return le16_to_cpu(h->nritems);
  257. }
  258. static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
  259. {
  260. h->nritems = cpu_to_le16(val);
  261. }
  262. static inline u16 btrfs_header_flags(struct btrfs_header *h)
  263. {
  264. return le16_to_cpu(h->flags);
  265. }
  266. static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
  267. {
  268. h->flags = cpu_to_le16(val);
  269. }
  270. static inline int btrfs_header_level(struct btrfs_header *h)
  271. {
  272. return btrfs_header_flags(h) & (BTRFS_MAX_LEVEL - 1);
  273. }
  274. static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
  275. {
  276. u16 flags;
  277. BUG_ON(level > BTRFS_MAX_LEVEL);
  278. flags = btrfs_header_flags(h) & ~(BTRFS_MAX_LEVEL - 1);
  279. btrfs_set_header_flags(h, flags | level);
  280. }
  281. static inline int btrfs_is_leaf(struct btrfs_node *n)
  282. {
  283. return (btrfs_header_level(&n->header) == 0);
  284. }
  285. static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
  286. {
  287. return le64_to_cpu(item->blocknr);
  288. }
  289. static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
  290. {
  291. item->blocknr = cpu_to_le64(val);
  292. }
  293. static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
  294. {
  295. return le32_to_cpu(item->refs);
  296. }
  297. static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
  298. {
  299. item->refs = cpu_to_le32(val);
  300. }
  301. static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
  302. {
  303. return le64_to_cpu(s->blocknr);
  304. }
  305. static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
  306. {
  307. s->blocknr = cpu_to_le64(val);
  308. }
  309. static inline u64 btrfs_super_root(struct btrfs_super_block *s)
  310. {
  311. return le64_to_cpu(s->root);
  312. }
  313. static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
  314. {
  315. s->root = cpu_to_le64(val);
  316. }
  317. static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
  318. {
  319. return le64_to_cpu(s->total_blocks);
  320. }
  321. static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
  322. u64 val)
  323. {
  324. s->total_blocks = cpu_to_le64(val);
  325. }
  326. static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
  327. {
  328. return le64_to_cpu(s->blocks_used);
  329. }
  330. static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
  331. u64 val)
  332. {
  333. s->blocks_used = cpu_to_le64(val);
  334. }
  335. static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
  336. {
  337. return le32_to_cpu(s->blocksize);
  338. }
  339. static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
  340. u32 val)
  341. {
  342. s->blocksize = cpu_to_le32(val);
  343. }
  344. static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
  345. {
  346. return (u8 *)l->items;
  347. }
  348. /* helper function to cast into the data area of the leaf. */
  349. #define btrfs_item_ptr(leaf, slot, type) \
  350. ((type *)(btrfs_leaf_data(leaf) + \
  351. btrfs_item_offset((leaf)->items + (slot))))
  352. struct btrfs_buffer *btrfs_alloc_free_block(struct btrfs_root *root);
  353. int btrfs_inc_ref(struct btrfs_root *root, struct btrfs_buffer *buf);
  354. int btrfs_free_extent(struct btrfs_root *root, u64 blocknr, u64 num_blocks);
  355. int btrfs_search_slot(struct btrfs_root *root, struct btrfs_key *key,
  356. struct btrfs_path *p, int ins_len, int cow);
  357. void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
  358. void btrfs_init_path(struct btrfs_path *p);
  359. int btrfs_del_item(struct btrfs_root *root, struct btrfs_path *path);
  360. int btrfs_insert_item(struct btrfs_root *root, struct btrfs_key *key,
  361. void *data, int data_size);
  362. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
  363. int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
  364. int btrfs_drop_snapshot(struct btrfs_root *root, struct btrfs_buffer *snap);
  365. int btrfs_finish_extent_commit(struct btrfs_root *root);
  366. int btrfs_del_root(struct btrfs_root *root, struct btrfs_key *key);
  367. int btrfs_insert_root(struct btrfs_root *root, struct btrfs_key *key,
  368. struct btrfs_root_item *item);
  369. int btrfs_update_root(struct btrfs_root *root, struct btrfs_key *key,
  370. struct btrfs_root_item *item);
  371. int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
  372. struct btrfs_root_item *item, struct btrfs_key *key);
  373. #endif