ctree.h 24 KB

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