ctree.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. #ifndef __BTRFS__
  2. #define __BTRFS__
  3. #include <linux/fs.h>
  4. #include "bit-radix.h"
  5. struct btrfs_trans_handle;
  6. struct btrfs_transaction;
  7. #define BTRFS_MAGIC "_BtRfS_M"
  8. #define BTRFS_ROOT_TREE_OBJECTID 1ULL
  9. #define BTRFS_EXTENT_TREE_OBJECTID 2ULL
  10. #define BTRFS_INODE_MAP_OBJECTID 3ULL
  11. #define BTRFS_FS_TREE_OBJECTID 4ULL
  12. #define BTRFS_FIRST_FREE_OBJECTID 5ULL
  13. /*
  14. * we can actually store much bigger names, but lets not confuse the rest
  15. * of linux
  16. */
  17. #define BTRFS_NAME_LEN 255
  18. /*
  19. * the key defines the order in the tree, and so it also defines (optimal)
  20. * block layout. objectid corresonds to the inode number. The flags
  21. * tells us things about the object, and is a kind of stream selector.
  22. * so for a given inode, keys with flags of 1 might refer to the inode
  23. * data, flags of 2 may point to file data in the btree and flags == 3
  24. * may point to extents.
  25. *
  26. * offset is the starting byte offset for this key in the stream.
  27. *
  28. * btrfs_disk_key is in disk byte order. struct btrfs_key is always
  29. * in cpu native order. Otherwise they are identical and their sizes
  30. * should be the same (ie both packed)
  31. */
  32. struct btrfs_disk_key {
  33. __le64 objectid;
  34. __le32 flags;
  35. __le64 offset;
  36. } __attribute__ ((__packed__));
  37. struct btrfs_key {
  38. u64 objectid;
  39. u32 flags;
  40. u64 offset;
  41. } __attribute__ ((__packed__));
  42. /*
  43. * every tree block (leaf or node) starts with this header.
  44. */
  45. struct btrfs_header {
  46. __le32 csum[8];
  47. u8 fsid[16]; /* FS specific uuid */
  48. __le64 blocknr; /* which block this node is supposed to live in */
  49. __le64 generation;
  50. __le64 parentid; /* objectid of the tree root */
  51. __le32 ham;
  52. __le16 nritems;
  53. __le16 flags;
  54. u8 level;
  55. } __attribute__ ((__packed__));
  56. #define BTRFS_MAX_LEVEL 8
  57. #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
  58. sizeof(struct btrfs_header)) / \
  59. (sizeof(struct btrfs_disk_key) + sizeof(u64)))
  60. #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
  61. #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
  62. struct buffer_head;
  63. /*
  64. * the super block basically lists the main trees of the FS
  65. * it currently lacks any block count etc etc
  66. */
  67. struct btrfs_super_block {
  68. __le32 csum[8];
  69. /* the first 3 fields must match struct btrfs_header */
  70. u8 fsid[16]; /* FS specific uuid */
  71. __le64 blocknr; /* this block number */
  72. __le64 magic;
  73. __le32 blocksize;
  74. __le64 generation;
  75. __le64 root;
  76. __le64 total_blocks;
  77. __le64 blocks_used;
  78. __le64 root_dir_objectid;
  79. } __attribute__ ((__packed__));
  80. /*
  81. * A leaf is full of items. offset and size tell us where to find
  82. * the item in the leaf (relative to the start of the data area)
  83. */
  84. struct btrfs_item {
  85. struct btrfs_disk_key key;
  86. __le32 offset;
  87. __le16 size;
  88. } __attribute__ ((__packed__));
  89. /*
  90. * leaves have an item area and a data area:
  91. * [item0, item1....itemN] [free space] [dataN...data1, data0]
  92. *
  93. * The data is separate from the items to get the keys closer together
  94. * during searches.
  95. */
  96. struct btrfs_leaf {
  97. struct btrfs_header header;
  98. struct btrfs_item items[];
  99. } __attribute__ ((__packed__));
  100. /*
  101. * all non-leaf blocks are nodes, they hold only keys and pointers to
  102. * other blocks
  103. */
  104. struct btrfs_key_ptr {
  105. struct btrfs_disk_key key;
  106. __le64 blockptr;
  107. } __attribute__ ((__packed__));
  108. struct btrfs_node {
  109. struct btrfs_header header;
  110. struct btrfs_key_ptr ptrs[];
  111. } __attribute__ ((__packed__));
  112. /*
  113. * btrfs_paths remember the path taken from the root down to the leaf.
  114. * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
  115. * to any other levels that are present.
  116. *
  117. * The slots array records the index of the item or block pointer
  118. * used while walking the tree.
  119. */
  120. struct btrfs_path {
  121. struct buffer_head *nodes[BTRFS_MAX_LEVEL];
  122. int slots[BTRFS_MAX_LEVEL];
  123. };
  124. /*
  125. * items in the extent btree are used to record the objectid of the
  126. * owner of the block and the number of references
  127. */
  128. struct btrfs_extent_item {
  129. __le32 refs;
  130. __le64 owner;
  131. } __attribute__ ((__packed__));
  132. struct btrfs_inode_timespec {
  133. __le32 sec;
  134. __le32 nsec;
  135. } __attribute__ ((__packed__));
  136. /*
  137. * there is no padding here on purpose. If you want to extent the inode,
  138. * make a new item type
  139. */
  140. struct btrfs_inode_item {
  141. __le64 generation;
  142. __le64 size;
  143. __le64 nblocks;
  144. __le32 nlink;
  145. __le32 uid;
  146. __le32 gid;
  147. __le32 mode;
  148. __le32 rdev;
  149. __le16 flags;
  150. __le16 compat_flags;
  151. struct btrfs_inode_timespec atime;
  152. struct btrfs_inode_timespec ctime;
  153. struct btrfs_inode_timespec mtime;
  154. struct btrfs_inode_timespec otime;
  155. } __attribute__ ((__packed__));
  156. /* inline data is just a blob of bytes */
  157. struct btrfs_inline_data_item {
  158. u8 data;
  159. } __attribute__ ((__packed__));
  160. struct btrfs_dir_item {
  161. __le64 objectid;
  162. __le16 flags;
  163. __le16 name_len;
  164. u8 type;
  165. } __attribute__ ((__packed__));
  166. struct btrfs_root_item {
  167. __le64 blocknr;
  168. __le32 flags;
  169. __le64 block_limit;
  170. __le64 blocks_used;
  171. __le32 refs;
  172. } __attribute__ ((__packed__));
  173. struct btrfs_file_extent_item {
  174. __le64 generation;
  175. /*
  176. * disk space consumed by the extent, checksum blocks are included
  177. * in these numbers
  178. */
  179. __le64 disk_blocknr;
  180. __le64 disk_num_blocks;
  181. /*
  182. * the logical offset in file blocks (no csums)
  183. * this extent record is for. This allows a file extent to point
  184. * into the middle of an existing extent on disk, sharing it
  185. * between two snapshots (useful if some bytes in the middle of the
  186. * extent have changed
  187. */
  188. __le64 offset;
  189. /*
  190. * the logical number of file blocks (no csums included)
  191. */
  192. __le64 num_blocks;
  193. } __attribute__ ((__packed__));
  194. struct btrfs_inode_map_item {
  195. struct btrfs_disk_key key;
  196. } __attribute__ ((__packed__));
  197. struct crypto_hash;
  198. struct btrfs_fs_info {
  199. struct btrfs_root *fs_root;
  200. struct btrfs_root *extent_root;
  201. struct btrfs_root *tree_root;
  202. struct btrfs_root *inode_root;
  203. struct btrfs_key current_insert;
  204. struct btrfs_key last_insert;
  205. struct radix_tree_root pending_del_radix;
  206. struct radix_tree_root pinned_radix;
  207. u64 last_inode_alloc;
  208. u64 last_inode_alloc_dirid;
  209. u64 generation;
  210. struct btrfs_transaction *running_transaction;
  211. struct btrfs_super_block *disk_super;
  212. struct buffer_head *sb_buffer;
  213. struct super_block *sb;
  214. struct inode *btree_inode;
  215. struct mutex trans_mutex;
  216. struct mutex fs_mutex;
  217. struct crypto_hash *hash_tfm;
  218. spinlock_t hash_lock;
  219. };
  220. /*
  221. * in ram representation of the tree. extent_root is used for all allocations
  222. * and for the extent tree extent_root root. current_insert is used
  223. * only for the extent tree.
  224. */
  225. struct btrfs_root {
  226. struct buffer_head *node;
  227. struct buffer_head *commit_root;
  228. struct btrfs_root_item root_item;
  229. struct btrfs_key root_key;
  230. struct btrfs_fs_info *fs_info;
  231. u32 blocksize;
  232. int ref_cows;
  233. u32 type;
  234. };
  235. /* the lower bits in the key flags defines the item type */
  236. #define BTRFS_KEY_TYPE_MAX 256
  237. #define BTRFS_KEY_TYPE_MASK (BTRFS_KEY_TYPE_MAX - 1)
  238. /*
  239. * inode items have the data typically returned from stat and store other
  240. * info about object characteristics. There is one for every file and dir in
  241. * the FS
  242. */
  243. #define BTRFS_INODE_ITEM_KEY 1
  244. /*
  245. * dir items are the name -> inode pointers in a directory. There is one
  246. * for every name in a directory.
  247. */
  248. #define BTRFS_DIR_ITEM_KEY 2
  249. /*
  250. * inline data is file data that fits in the btree.
  251. */
  252. #define BTRFS_INLINE_DATA_KEY 3
  253. /*
  254. * extent data is for data that can't fit in the btree. It points to
  255. * a (hopefully) huge chunk of disk
  256. */
  257. #define BTRFS_EXTENT_DATA_KEY 4
  258. /*
  259. * root items point to tree roots. There are typically in the root
  260. * tree used by the super block to find all the other trees
  261. */
  262. #define BTRFS_ROOT_ITEM_KEY 5
  263. /*
  264. * extent items are in the extent map tree. These record which blocks
  265. * are used, and how many references there are to each block
  266. */
  267. #define BTRFS_EXTENT_ITEM_KEY 6
  268. /*
  269. * the inode map records which inode numbers are in use and where
  270. * they actually live on disk
  271. */
  272. #define BTRFS_INODE_MAP_ITEM_KEY 7
  273. /*
  274. * string items are for debugging. They just store a short string of
  275. * data in the FS
  276. */
  277. #define BTRFS_STRING_ITEM_KEY 8
  278. static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
  279. {
  280. return le64_to_cpu(i->generation);
  281. }
  282. static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
  283. u64 val)
  284. {
  285. i->generation = cpu_to_le64(val);
  286. }
  287. static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
  288. {
  289. return le64_to_cpu(i->size);
  290. }
  291. static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
  292. {
  293. i->size = cpu_to_le64(val);
  294. }
  295. static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
  296. {
  297. return le64_to_cpu(i->nblocks);
  298. }
  299. static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
  300. {
  301. i->nblocks = cpu_to_le64(val);
  302. }
  303. static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
  304. {
  305. return le32_to_cpu(i->nlink);
  306. }
  307. static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
  308. {
  309. i->nlink = cpu_to_le32(val);
  310. }
  311. static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
  312. {
  313. return le32_to_cpu(i->uid);
  314. }
  315. static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
  316. {
  317. i->uid = cpu_to_le32(val);
  318. }
  319. static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
  320. {
  321. return le32_to_cpu(i->gid);
  322. }
  323. static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
  324. {
  325. i->gid = cpu_to_le32(val);
  326. }
  327. static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
  328. {
  329. return le32_to_cpu(i->mode);
  330. }
  331. static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
  332. {
  333. i->mode = cpu_to_le32(val);
  334. }
  335. static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
  336. {
  337. return le32_to_cpu(i->rdev);
  338. }
  339. static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
  340. {
  341. i->rdev = cpu_to_le32(val);
  342. }
  343. static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
  344. {
  345. return le16_to_cpu(i->flags);
  346. }
  347. static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
  348. {
  349. i->flags = cpu_to_le16(val);
  350. }
  351. static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
  352. {
  353. return le16_to_cpu(i->compat_flags);
  354. }
  355. static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
  356. u16 val)
  357. {
  358. i->compat_flags = cpu_to_le16(val);
  359. }
  360. static inline u32 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
  361. {
  362. return le32_to_cpu(ts->sec);
  363. }
  364. static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
  365. u32 val)
  366. {
  367. ts->sec = cpu_to_le32(val);
  368. }
  369. static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
  370. {
  371. return le32_to_cpu(ts->nsec);
  372. }
  373. static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
  374. u32 val)
  375. {
  376. ts->nsec = cpu_to_le32(val);
  377. }
  378. static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
  379. {
  380. return le64_to_cpu(ei->owner);
  381. }
  382. static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
  383. {
  384. ei->owner = cpu_to_le64(val);
  385. }
  386. static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
  387. {
  388. return le32_to_cpu(ei->refs);
  389. }
  390. static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
  391. {
  392. ei->refs = cpu_to_le32(val);
  393. }
  394. static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
  395. {
  396. return le64_to_cpu(n->ptrs[nr].blockptr);
  397. }
  398. static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
  399. u64 val)
  400. {
  401. n->ptrs[nr].blockptr = cpu_to_le64(val);
  402. }
  403. static inline u32 btrfs_item_offset(struct btrfs_item *item)
  404. {
  405. return le32_to_cpu(item->offset);
  406. }
  407. static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
  408. {
  409. item->offset = cpu_to_le32(val);
  410. }
  411. static inline u32 btrfs_item_end(struct btrfs_item *item)
  412. {
  413. return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
  414. }
  415. static inline u16 btrfs_item_size(struct btrfs_item *item)
  416. {
  417. return le16_to_cpu(item->size);
  418. }
  419. static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
  420. {
  421. item->size = cpu_to_le16(val);
  422. }
  423. static inline u64 btrfs_dir_objectid(struct btrfs_dir_item *d)
  424. {
  425. return le64_to_cpu(d->objectid);
  426. }
  427. static inline void btrfs_set_dir_objectid(struct btrfs_dir_item *d, u64 val)
  428. {
  429. d->objectid = cpu_to_le64(val);
  430. }
  431. static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
  432. {
  433. return le16_to_cpu(d->flags);
  434. }
  435. static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
  436. {
  437. d->flags = cpu_to_le16(val);
  438. }
  439. static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
  440. {
  441. return d->type;
  442. }
  443. static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
  444. {
  445. d->type = val;
  446. }
  447. static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
  448. {
  449. return le16_to_cpu(d->name_len);
  450. }
  451. static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
  452. {
  453. d->name_len = cpu_to_le16(val);
  454. }
  455. static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
  456. struct btrfs_disk_key *disk)
  457. {
  458. cpu->offset = le64_to_cpu(disk->offset);
  459. cpu->flags = le32_to_cpu(disk->flags);
  460. cpu->objectid = le64_to_cpu(disk->objectid);
  461. }
  462. static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
  463. struct btrfs_key *cpu)
  464. {
  465. disk->offset = cpu_to_le64(cpu->offset);
  466. disk->flags = cpu_to_le32(cpu->flags);
  467. disk->objectid = cpu_to_le64(cpu->objectid);
  468. }
  469. static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
  470. {
  471. return le64_to_cpu(disk->objectid);
  472. }
  473. static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
  474. u64 val)
  475. {
  476. disk->objectid = cpu_to_le64(val);
  477. }
  478. static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
  479. {
  480. return le64_to_cpu(disk->offset);
  481. }
  482. static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
  483. u64 val)
  484. {
  485. disk->offset = cpu_to_le64(val);
  486. }
  487. static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
  488. {
  489. return le32_to_cpu(disk->flags);
  490. }
  491. static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
  492. u32 val)
  493. {
  494. disk->flags = cpu_to_le32(val);
  495. }
  496. static inline u32 btrfs_key_type(struct btrfs_key *key)
  497. {
  498. return key->flags & BTRFS_KEY_TYPE_MASK;
  499. }
  500. static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
  501. {
  502. return le32_to_cpu(key->flags) & BTRFS_KEY_TYPE_MASK;
  503. }
  504. static inline void btrfs_set_key_type(struct btrfs_key *key, u32 type)
  505. {
  506. BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
  507. key->flags = (key->flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
  508. }
  509. static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key, u32 type)
  510. {
  511. u32 flags = btrfs_disk_key_flags(key);
  512. BUG_ON(type >= BTRFS_KEY_TYPE_MAX);
  513. flags = (flags & ~((u64)BTRFS_KEY_TYPE_MASK)) | type;
  514. btrfs_set_disk_key_flags(key, flags);
  515. }
  516. static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
  517. {
  518. return le64_to_cpu(h->blocknr);
  519. }
  520. static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
  521. {
  522. h->blocknr = cpu_to_le64(blocknr);
  523. }
  524. static inline u64 btrfs_header_generation(struct btrfs_header *h)
  525. {
  526. return le64_to_cpu(h->generation);
  527. }
  528. static inline void btrfs_set_header_generation(struct btrfs_header *h,
  529. u64 val)
  530. {
  531. h->generation = cpu_to_le64(val);
  532. }
  533. static inline u64 btrfs_header_parentid(struct btrfs_header *h)
  534. {
  535. return le64_to_cpu(h->parentid);
  536. }
  537. static inline void btrfs_set_header_parentid(struct btrfs_header *h,
  538. u64 parentid)
  539. {
  540. h->parentid = cpu_to_le64(parentid);
  541. }
  542. static inline u16 btrfs_header_nritems(struct btrfs_header *h)
  543. {
  544. return le16_to_cpu(h->nritems);
  545. }
  546. static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
  547. {
  548. h->nritems = cpu_to_le16(val);
  549. }
  550. static inline u16 btrfs_header_flags(struct btrfs_header *h)
  551. {
  552. return le16_to_cpu(h->flags);
  553. }
  554. static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
  555. {
  556. h->flags = cpu_to_le16(val);
  557. }
  558. static inline int btrfs_header_level(struct btrfs_header *h)
  559. {
  560. return h->level;
  561. }
  562. static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
  563. {
  564. BUG_ON(level > BTRFS_MAX_LEVEL);
  565. h->level = level;
  566. }
  567. static inline int btrfs_is_leaf(struct btrfs_node *n)
  568. {
  569. return (btrfs_header_level(&n->header) == 0);
  570. }
  571. static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
  572. {
  573. return le64_to_cpu(item->blocknr);
  574. }
  575. static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
  576. {
  577. item->blocknr = cpu_to_le64(val);
  578. }
  579. static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
  580. {
  581. return le32_to_cpu(item->refs);
  582. }
  583. static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
  584. {
  585. item->refs = cpu_to_le32(val);
  586. }
  587. static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
  588. {
  589. return le64_to_cpu(s->blocknr);
  590. }
  591. static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
  592. {
  593. s->blocknr = cpu_to_le64(val);
  594. }
  595. static inline u64 btrfs_super_root(struct btrfs_super_block *s)
  596. {
  597. return le64_to_cpu(s->root);
  598. }
  599. static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
  600. {
  601. s->root = cpu_to_le64(val);
  602. }
  603. static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
  604. {
  605. return le64_to_cpu(s->total_blocks);
  606. }
  607. static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
  608. u64 val)
  609. {
  610. s->total_blocks = cpu_to_le64(val);
  611. }
  612. static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
  613. {
  614. return le64_to_cpu(s->blocks_used);
  615. }
  616. static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
  617. u64 val)
  618. {
  619. s->blocks_used = cpu_to_le64(val);
  620. }
  621. static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
  622. {
  623. return le32_to_cpu(s->blocksize);
  624. }
  625. static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
  626. u32 val)
  627. {
  628. s->blocksize = cpu_to_le32(val);
  629. }
  630. static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
  631. {
  632. return le64_to_cpu(s->root_dir_objectid);
  633. }
  634. static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
  635. val)
  636. {
  637. s->root_dir_objectid = cpu_to_le64(val);
  638. }
  639. static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
  640. {
  641. return (u8 *)l->items;
  642. }
  643. static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
  644. *e)
  645. {
  646. return le64_to_cpu(e->disk_blocknr);
  647. }
  648. static inline void btrfs_set_file_extent_disk_blocknr(struct
  649. btrfs_file_extent_item
  650. *e, u64 val)
  651. {
  652. e->disk_blocknr = cpu_to_le64(val);
  653. }
  654. static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e)
  655. {
  656. return le64_to_cpu(e->generation);
  657. }
  658. static inline void btrfs_set_file_extent_generation(struct
  659. btrfs_file_extent_item *e,
  660. u64 val)
  661. {
  662. e->generation = cpu_to_le64(val);
  663. }
  664. static inline u64 btrfs_file_extent_disk_num_blocks(struct
  665. btrfs_file_extent_item *e)
  666. {
  667. return le64_to_cpu(e->disk_num_blocks);
  668. }
  669. static inline void btrfs_set_file_extent_disk_num_blocks(struct
  670. btrfs_file_extent_item
  671. *e, u64 val)
  672. {
  673. e->disk_num_blocks = cpu_to_le64(val);
  674. }
  675. static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
  676. {
  677. return le64_to_cpu(e->offset);
  678. }
  679. static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
  680. *e, u64 val)
  681. {
  682. e->offset = cpu_to_le64(val);
  683. }
  684. static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
  685. *e)
  686. {
  687. return le64_to_cpu(e->num_blocks);
  688. }
  689. static inline void btrfs_set_file_extent_num_blocks(struct
  690. btrfs_file_extent_item *e,
  691. u64 val)
  692. {
  693. e->num_blocks = cpu_to_le64(val);
  694. }
  695. static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
  696. {
  697. return sb->s_fs_info;
  698. }
  699. /* helper function to cast into the data area of the leaf. */
  700. #define btrfs_item_ptr(leaf, slot, type) \
  701. ((type *)(btrfs_leaf_data(leaf) + \
  702. btrfs_item_offset((leaf)->items + (slot))))
  703. /* extent-item.c */
  704. struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  705. struct btrfs_root *root);
  706. int btrfs_alloc_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  707. *root, u64 num_blocks, u64 search_start, u64
  708. search_end, u64 owner, struct btrfs_key *ins);
  709. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  710. struct buffer_head *buf);
  711. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  712. *root, u64 blocknr, u64 num_blocks, int pin);
  713. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
  714. btrfs_root *root);
  715. /* ctree.c */
  716. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
  717. *root, struct btrfs_key *key, struct btrfs_path *p, int
  718. ins_len, int cow);
  719. void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
  720. void btrfs_init_path(struct btrfs_path *p);
  721. int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  722. struct btrfs_path *path);
  723. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  724. *root, struct btrfs_key *key, void *data, u32 data_size);
  725. int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
  726. *root, struct btrfs_path *path, struct btrfs_key
  727. *cpu_key, u32 data_size);
  728. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
  729. int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
  730. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  731. *root, struct buffer_head *snap);
  732. /* root-item.c */
  733. int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  734. struct btrfs_key *key);
  735. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
  736. *root, struct btrfs_key *key, struct btrfs_root_item
  737. *item);
  738. int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
  739. *root, struct btrfs_key *key, struct btrfs_root_item
  740. *item);
  741. int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
  742. btrfs_root_item *item, struct btrfs_key *key);
  743. /* dir-item.c */
  744. int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
  745. *root, const char *name, int name_len, u64 dir, u64
  746. objectid, u8 type);
  747. int btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
  748. *root, struct btrfs_path *path, u64 dir,
  749. const char *name, int name_len, int mod);
  750. int btrfs_match_dir_item_name(struct btrfs_root *root, struct btrfs_path *path,
  751. const char *name, int name_len);
  752. /* inode-map.c */
  753. int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
  754. struct btrfs_root *fs_root,
  755. u64 dirid, u64 *objectid);
  756. int btrfs_insert_inode_map(struct btrfs_trans_handle *trans,
  757. struct btrfs_root *root,
  758. u64 objectid, struct btrfs_key *location);
  759. int btrfs_lookup_inode_map(struct btrfs_trans_handle *trans,
  760. struct btrfs_root *root, struct btrfs_path *path,
  761. u64 objectid, int mod);
  762. /* inode-item.c */
  763. int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  764. *root, u64 objectid, struct btrfs_inode_item
  765. *inode_item);
  766. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  767. *root, struct btrfs_path *path, u64 objectid, int mod);
  768. /* file-item.c */
  769. int btrfs_alloc_file_extent(struct btrfs_trans_handle *trans,
  770. struct btrfs_root *root,
  771. u64 objectid, u64 offset,
  772. u64 num_blocks, u64 hint_block,
  773. u64 *result);
  774. int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
  775. struct btrfs_root *root,
  776. struct btrfs_path *path, u64 objectid,
  777. u64 blocknr, int mod);
  778. #endif