ctree.h 24 KB

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