ctree.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. #ifndef __BTRFS__
  2. #define __BTRFS__
  3. #include <linux/fs.h>
  4. #include <linux/buffer_head.h>
  5. #include <linux/kobject.h>
  6. #include "bit-radix.h"
  7. struct btrfs_trans_handle;
  8. struct btrfs_transaction;
  9. extern struct kmem_cache *btrfs_trans_handle_cachep;
  10. extern struct kmem_cache *btrfs_transaction_cachep;
  11. extern struct kmem_cache *btrfs_bit_radix_cachep;
  12. extern struct kmem_cache *btrfs_path_cachep;
  13. #define BTRFS_MAGIC "_BtRfS_M"
  14. #define BTRFS_ROOT_TREE_OBJECTID 1ULL
  15. #define BTRFS_DEV_TREE_OBJECTID 2ULL
  16. #define BTRFS_EXTENT_TREE_OBJECTID 3ULL
  17. #define BTRFS_FS_TREE_OBJECTID 4ULL
  18. #define BTRFS_ROOT_TREE_DIR_OBJECTID 5ULL
  19. #define BTRFS_FIRST_FREE_OBJECTID 6ULL
  20. /*
  21. * we can actually store much bigger names, but lets not confuse the rest
  22. * of linux
  23. */
  24. #define BTRFS_NAME_LEN 255
  25. /* 32 bytes in various csum fields */
  26. #define BTRFS_CSUM_SIZE 32
  27. /* four bytes for CRC32 */
  28. #define BTRFS_CRC32_SIZE 4
  29. /*
  30. * the key defines the order in the tree, and so it also defines (optimal)
  31. * block layout. objectid corresonds to the inode number. The flags
  32. * tells us things about the object, and is a kind of stream selector.
  33. * so for a given inode, keys with flags of 1 might refer to the inode
  34. * data, flags of 2 may point to file data in the btree and flags == 3
  35. * may point to extents.
  36. *
  37. * offset is the starting byte offset for this key in the stream.
  38. *
  39. * btrfs_disk_key is in disk byte order. struct btrfs_key is always
  40. * in cpu native order. Otherwise they are identical and their sizes
  41. * should be the same (ie both packed)
  42. */
  43. struct btrfs_disk_key {
  44. __le64 objectid;
  45. __le32 flags;
  46. __le64 offset;
  47. } __attribute__ ((__packed__));
  48. struct btrfs_key {
  49. u64 objectid;
  50. u32 flags;
  51. u64 offset;
  52. } __attribute__ ((__packed__));
  53. /*
  54. * every tree block (leaf or node) starts with this header.
  55. */
  56. struct btrfs_header {
  57. u8 csum[BTRFS_CSUM_SIZE];
  58. u8 fsid[16]; /* FS specific uuid */
  59. __le64 blocknr; /* which block this node is supposed to live in */
  60. __le64 generation;
  61. __le64 owner;
  62. __le16 nritems;
  63. __le16 flags;
  64. u8 level;
  65. } __attribute__ ((__packed__));
  66. #define BTRFS_MAX_LEVEL 8
  67. #define BTRFS_NODEPTRS_PER_BLOCK(r) (((r)->blocksize - \
  68. sizeof(struct btrfs_header)) / \
  69. (sizeof(struct btrfs_disk_key) + sizeof(u64)))
  70. #define __BTRFS_LEAF_DATA_SIZE(bs) ((bs) - sizeof(struct btrfs_header))
  71. #define BTRFS_LEAF_DATA_SIZE(r) (__BTRFS_LEAF_DATA_SIZE(r->blocksize))
  72. #define BTRFS_MAX_INLINE_DATA_SIZE(r) (BTRFS_LEAF_DATA_SIZE(r) - \
  73. sizeof(struct btrfs_item) - \
  74. sizeof(struct btrfs_file_extent_item))
  75. struct buffer_head;
  76. /*
  77. * the super block basically lists the main trees of the FS
  78. * it currently lacks any block count etc etc
  79. */
  80. struct btrfs_super_block {
  81. u8 csum[BTRFS_CSUM_SIZE];
  82. /* the first 3 fields must match struct btrfs_header */
  83. u8 fsid[16]; /* FS specific uuid */
  84. __le64 blocknr; /* this block number */
  85. __le64 magic;
  86. __le32 blocksize;
  87. __le64 generation;
  88. __le64 root;
  89. __le64 total_blocks;
  90. __le64 blocks_used;
  91. __le64 root_dir_objectid;
  92. __le64 last_device_id;
  93. /* fields below here vary with the underlying disk */
  94. __le64 device_block_start;
  95. __le64 device_num_blocks;
  96. __le64 device_root;
  97. __le64 device_id;
  98. } __attribute__ ((__packed__));
  99. /*
  100. * A leaf is full of items. offset and size tell us where to find
  101. * the item in the leaf (relative to the start of the data area)
  102. */
  103. struct btrfs_item {
  104. struct btrfs_disk_key key;
  105. __le32 offset;
  106. __le16 size;
  107. } __attribute__ ((__packed__));
  108. /*
  109. * leaves have an item area and a data area:
  110. * [item0, item1....itemN] [free space] [dataN...data1, data0]
  111. *
  112. * The data is separate from the items to get the keys closer together
  113. * during searches.
  114. */
  115. struct btrfs_leaf {
  116. struct btrfs_header header;
  117. struct btrfs_item items[];
  118. } __attribute__ ((__packed__));
  119. /*
  120. * all non-leaf blocks are nodes, they hold only keys and pointers to
  121. * other blocks
  122. */
  123. struct btrfs_key_ptr {
  124. struct btrfs_disk_key key;
  125. __le64 blockptr;
  126. } __attribute__ ((__packed__));
  127. struct btrfs_node {
  128. struct btrfs_header header;
  129. struct btrfs_key_ptr ptrs[];
  130. } __attribute__ ((__packed__));
  131. /*
  132. * btrfs_paths remember the path taken from the root down to the leaf.
  133. * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
  134. * to any other levels that are present.
  135. *
  136. * The slots array records the index of the item or block pointer
  137. * used while walking the tree.
  138. */
  139. struct btrfs_path {
  140. struct buffer_head *nodes[BTRFS_MAX_LEVEL];
  141. int slots[BTRFS_MAX_LEVEL];
  142. };
  143. /*
  144. * items in the extent btree are used to record the objectid of the
  145. * owner of the block and the number of references
  146. */
  147. struct btrfs_extent_item {
  148. __le32 refs;
  149. __le64 owner;
  150. } __attribute__ ((__packed__));
  151. struct btrfs_inode_timespec {
  152. __le64 sec;
  153. __le32 nsec;
  154. } __attribute__ ((__packed__));
  155. /*
  156. * there is no padding here on purpose. If you want to extent the inode,
  157. * make a new item type
  158. */
  159. struct btrfs_inode_item {
  160. __le64 generation;
  161. __le64 size;
  162. __le64 nblocks;
  163. __le64 block_group;
  164. __le32 nlink;
  165. __le32 uid;
  166. __le32 gid;
  167. __le32 mode;
  168. __le32 rdev;
  169. __le16 flags;
  170. __le16 compat_flags;
  171. struct btrfs_inode_timespec atime;
  172. struct btrfs_inode_timespec ctime;
  173. struct btrfs_inode_timespec mtime;
  174. struct btrfs_inode_timespec otime;
  175. } __attribute__ ((__packed__));
  176. struct btrfs_dir_item {
  177. struct btrfs_disk_key location;
  178. __le16 flags;
  179. __le16 name_len;
  180. u8 type;
  181. } __attribute__ ((__packed__));
  182. struct btrfs_root_item {
  183. struct btrfs_inode_item inode;
  184. __le64 root_dirid;
  185. __le64 blocknr;
  186. __le32 flags;
  187. __le64 block_limit;
  188. __le64 blocks_used;
  189. __le32 refs;
  190. } __attribute__ ((__packed__));
  191. #define BTRFS_FILE_EXTENT_REG 0
  192. #define BTRFS_FILE_EXTENT_INLINE 1
  193. struct btrfs_file_extent_item {
  194. __le64 generation;
  195. u8 type;
  196. /*
  197. * disk space consumed by the extent, checksum blocks are included
  198. * in these numbers
  199. */
  200. __le64 disk_blocknr;
  201. __le64 disk_num_blocks;
  202. /*
  203. * the logical offset in file blocks (no csums)
  204. * this extent record is for. This allows a file extent to point
  205. * into the middle of an existing extent on disk, sharing it
  206. * between two snapshots (useful if some bytes in the middle of the
  207. * extent have changed
  208. */
  209. __le64 offset;
  210. /*
  211. * the logical number of file blocks (no csums included)
  212. */
  213. __le64 num_blocks;
  214. } __attribute__ ((__packed__));
  215. struct btrfs_csum_item {
  216. u8 csum;
  217. } __attribute__ ((__packed__));
  218. struct btrfs_device_item {
  219. __le16 pathlen;
  220. __le64 device_id;
  221. } __attribute__ ((__packed__));
  222. /* tag for the radix tree of block groups in ram */
  223. #define BTRFS_BLOCK_GROUP_DIRTY 0
  224. #define BTRFS_BLOCK_GROUP_AVAIL 1
  225. #define BTRFS_BLOCK_GROUP_HINTS 8
  226. #define BTRFS_BLOCK_GROUP_SIZE (256 * 1024 * 1024)
  227. struct btrfs_block_group_item {
  228. __le64 used;
  229. } __attribute__ ((__packed__));
  230. struct btrfs_block_group_cache {
  231. struct btrfs_key key;
  232. struct btrfs_block_group_item item;
  233. struct radix_tree_root *radix;
  234. u64 first_free;
  235. u64 last_alloc;
  236. u64 pinned;
  237. u64 last_prealloc;
  238. int data;
  239. int cached;
  240. };
  241. struct crypto_hash;
  242. struct btrfs_fs_info {
  243. struct btrfs_root *extent_root;
  244. struct btrfs_root *tree_root;
  245. struct btrfs_root *dev_root;
  246. struct radix_tree_root fs_roots_radix;
  247. struct radix_tree_root pending_del_radix;
  248. struct radix_tree_root pinned_radix;
  249. struct radix_tree_root dev_radix;
  250. struct radix_tree_root block_group_radix;
  251. struct radix_tree_root block_group_data_radix;
  252. struct radix_tree_root extent_map_radix;
  253. u64 extent_tree_insert[BTRFS_MAX_LEVEL * 3];
  254. int extent_tree_insert_nr;
  255. u64 extent_tree_prealloc[BTRFS_MAX_LEVEL * 3];
  256. int extent_tree_prealloc_nr;
  257. u64 generation;
  258. struct btrfs_transaction *running_transaction;
  259. struct btrfs_super_block *disk_super;
  260. struct buffer_head *sb_buffer;
  261. struct super_block *sb;
  262. struct inode *btree_inode;
  263. struct mutex trans_mutex;
  264. struct mutex fs_mutex;
  265. struct list_head trans_list;
  266. struct crypto_hash *hash_tfm;
  267. spinlock_t hash_lock;
  268. int do_barriers;
  269. struct kobject kobj;
  270. };
  271. /*
  272. * in ram representation of the tree. extent_root is used for all allocations
  273. * and for the extent tree extent_root root.
  274. */
  275. struct btrfs_root {
  276. struct buffer_head *node;
  277. struct buffer_head *commit_root;
  278. struct btrfs_root_item root_item;
  279. struct btrfs_key root_key;
  280. struct btrfs_fs_info *fs_info;
  281. struct inode *inode;
  282. u64 objectid;
  283. u64 last_trans;
  284. u32 blocksize;
  285. int ref_cows;
  286. u32 type;
  287. u64 highest_inode;
  288. u64 last_inode_alloc;
  289. };
  290. /* the lower bits in the key flags defines the item type */
  291. #define BTRFS_KEY_TYPE_MAX 256
  292. #define BTRFS_KEY_TYPE_SHIFT 24
  293. #define BTRFS_KEY_TYPE_MASK (((u32)BTRFS_KEY_TYPE_MAX - 1) << \
  294. BTRFS_KEY_TYPE_SHIFT)
  295. /*
  296. * inode items have the data typically returned from stat and store other
  297. * info about object characteristics. There is one for every file and dir in
  298. * the FS
  299. */
  300. #define BTRFS_INODE_ITEM_KEY 1
  301. /* reserve 2-15 close to the inode for later flexibility */
  302. /*
  303. * dir items are the name -> inode pointers in a directory. There is one
  304. * for every name in a directory.
  305. */
  306. #define BTRFS_DIR_ITEM_KEY 16
  307. #define BTRFS_DIR_INDEX_KEY 17
  308. /*
  309. * extent data is for file data
  310. */
  311. #define BTRFS_EXTENT_DATA_KEY 18
  312. /*
  313. * csum items have the checksums for data in the extents
  314. */
  315. #define BTRFS_CSUM_ITEM_KEY 19
  316. /* reserve 20-31 for other file stuff */
  317. /*
  318. * root items point to tree roots. There are typically in the root
  319. * tree used by the super block to find all the other trees
  320. */
  321. #define BTRFS_ROOT_ITEM_KEY 32
  322. /*
  323. * extent items are in the extent map tree. These record which blocks
  324. * are used, and how many references there are to each block
  325. */
  326. #define BTRFS_EXTENT_ITEM_KEY 33
  327. /*
  328. * block groups give us hints into the extent allocation trees. Which
  329. * blocks are free etc etc
  330. */
  331. #define BTRFS_BLOCK_GROUP_ITEM_KEY 34
  332. /*
  333. * dev items list the devices that make up the FS
  334. */
  335. #define BTRFS_DEV_ITEM_KEY 35
  336. /*
  337. * string items are for debugging. They just store a short string of
  338. * data in the FS
  339. */
  340. #define BTRFS_STRING_ITEM_KEY 253
  341. static inline u64 btrfs_block_group_used(struct btrfs_block_group_item *bi)
  342. {
  343. return le64_to_cpu(bi->used);
  344. }
  345. static inline void btrfs_set_block_group_used(struct
  346. btrfs_block_group_item *bi,
  347. u64 val)
  348. {
  349. bi->used = cpu_to_le64(val);
  350. }
  351. static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
  352. {
  353. return le64_to_cpu(i->generation);
  354. }
  355. static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
  356. u64 val)
  357. {
  358. i->generation = cpu_to_le64(val);
  359. }
  360. static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
  361. {
  362. return le64_to_cpu(i->size);
  363. }
  364. static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
  365. {
  366. i->size = cpu_to_le64(val);
  367. }
  368. static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
  369. {
  370. return le64_to_cpu(i->nblocks);
  371. }
  372. static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
  373. {
  374. i->nblocks = cpu_to_le64(val);
  375. }
  376. static inline u64 btrfs_inode_block_group(struct btrfs_inode_item *i)
  377. {
  378. return le64_to_cpu(i->block_group);
  379. }
  380. static inline void btrfs_set_inode_block_group(struct btrfs_inode_item *i,
  381. u64 val)
  382. {
  383. i->block_group = cpu_to_le64(val);
  384. }
  385. static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
  386. {
  387. return le32_to_cpu(i->nlink);
  388. }
  389. static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
  390. {
  391. i->nlink = cpu_to_le32(val);
  392. }
  393. static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
  394. {
  395. return le32_to_cpu(i->uid);
  396. }
  397. static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
  398. {
  399. i->uid = cpu_to_le32(val);
  400. }
  401. static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
  402. {
  403. return le32_to_cpu(i->gid);
  404. }
  405. static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
  406. {
  407. i->gid = cpu_to_le32(val);
  408. }
  409. static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
  410. {
  411. return le32_to_cpu(i->mode);
  412. }
  413. static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
  414. {
  415. i->mode = cpu_to_le32(val);
  416. }
  417. static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
  418. {
  419. return le32_to_cpu(i->rdev);
  420. }
  421. static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
  422. {
  423. i->rdev = cpu_to_le32(val);
  424. }
  425. static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
  426. {
  427. return le16_to_cpu(i->flags);
  428. }
  429. static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
  430. {
  431. i->flags = cpu_to_le16(val);
  432. }
  433. static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
  434. {
  435. return le16_to_cpu(i->compat_flags);
  436. }
  437. static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
  438. u16 val)
  439. {
  440. i->compat_flags = cpu_to_le16(val);
  441. }
  442. static inline u64 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
  443. {
  444. return le64_to_cpu(ts->sec);
  445. }
  446. static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
  447. u64 val)
  448. {
  449. ts->sec = cpu_to_le64(val);
  450. }
  451. static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
  452. {
  453. return le32_to_cpu(ts->nsec);
  454. }
  455. static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
  456. u32 val)
  457. {
  458. ts->nsec = cpu_to_le32(val);
  459. }
  460. static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
  461. {
  462. return le32_to_cpu(ei->refs);
  463. }
  464. static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
  465. {
  466. ei->refs = cpu_to_le32(val);
  467. }
  468. static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
  469. {
  470. return le64_to_cpu(ei->owner);
  471. }
  472. static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
  473. {
  474. ei->owner = cpu_to_le64(val);
  475. }
  476. static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
  477. {
  478. return le64_to_cpu(n->ptrs[nr].blockptr);
  479. }
  480. static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
  481. u64 val)
  482. {
  483. n->ptrs[nr].blockptr = cpu_to_le64(val);
  484. }
  485. static inline u32 btrfs_item_offset(struct btrfs_item *item)
  486. {
  487. return le32_to_cpu(item->offset);
  488. }
  489. static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
  490. {
  491. item->offset = cpu_to_le32(val);
  492. }
  493. static inline u32 btrfs_item_end(struct btrfs_item *item)
  494. {
  495. return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
  496. }
  497. static inline u16 btrfs_item_size(struct btrfs_item *item)
  498. {
  499. return le16_to_cpu(item->size);
  500. }
  501. static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
  502. {
  503. item->size = cpu_to_le16(val);
  504. }
  505. static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
  506. {
  507. return le16_to_cpu(d->flags);
  508. }
  509. static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
  510. {
  511. d->flags = cpu_to_le16(val);
  512. }
  513. static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
  514. {
  515. return d->type;
  516. }
  517. static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
  518. {
  519. d->type = val;
  520. }
  521. static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
  522. {
  523. return le16_to_cpu(d->name_len);
  524. }
  525. static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
  526. {
  527. d->name_len = cpu_to_le16(val);
  528. }
  529. static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
  530. struct btrfs_disk_key *disk)
  531. {
  532. cpu->offset = le64_to_cpu(disk->offset);
  533. cpu->flags = le32_to_cpu(disk->flags);
  534. cpu->objectid = le64_to_cpu(disk->objectid);
  535. }
  536. static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
  537. struct btrfs_key *cpu)
  538. {
  539. disk->offset = cpu_to_le64(cpu->offset);
  540. disk->flags = cpu_to_le32(cpu->flags);
  541. disk->objectid = cpu_to_le64(cpu->objectid);
  542. }
  543. static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
  544. {
  545. return le64_to_cpu(disk->objectid);
  546. }
  547. static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
  548. u64 val)
  549. {
  550. disk->objectid = cpu_to_le64(val);
  551. }
  552. static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
  553. {
  554. return le64_to_cpu(disk->offset);
  555. }
  556. static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
  557. u64 val)
  558. {
  559. disk->offset = cpu_to_le64(val);
  560. }
  561. static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
  562. {
  563. return le32_to_cpu(disk->flags);
  564. }
  565. static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
  566. u32 val)
  567. {
  568. disk->flags = cpu_to_le32(val);
  569. }
  570. static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
  571. {
  572. return le32_to_cpu(key->flags) >> BTRFS_KEY_TYPE_SHIFT;
  573. }
  574. static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key,
  575. u32 val)
  576. {
  577. u32 flags = btrfs_disk_key_flags(key);
  578. BUG_ON(val >= BTRFS_KEY_TYPE_MAX);
  579. val = val << BTRFS_KEY_TYPE_SHIFT;
  580. flags = (flags & ~BTRFS_KEY_TYPE_MASK) | val;
  581. btrfs_set_disk_key_flags(key, flags);
  582. }
  583. static inline u32 btrfs_key_type(struct btrfs_key *key)
  584. {
  585. return key->flags >> BTRFS_KEY_TYPE_SHIFT;
  586. }
  587. static inline void btrfs_set_key_type(struct btrfs_key *key, u32 val)
  588. {
  589. BUG_ON(val >= BTRFS_KEY_TYPE_MAX);
  590. val = val << BTRFS_KEY_TYPE_SHIFT;
  591. key->flags = (key->flags & ~(BTRFS_KEY_TYPE_MASK)) | val;
  592. }
  593. static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
  594. {
  595. return le64_to_cpu(h->blocknr);
  596. }
  597. static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
  598. {
  599. h->blocknr = cpu_to_le64(blocknr);
  600. }
  601. static inline u64 btrfs_header_generation(struct btrfs_header *h)
  602. {
  603. return le64_to_cpu(h->generation);
  604. }
  605. static inline void btrfs_set_header_generation(struct btrfs_header *h,
  606. u64 val)
  607. {
  608. h->generation = cpu_to_le64(val);
  609. }
  610. static inline u64 btrfs_header_owner(struct btrfs_header *h)
  611. {
  612. return le64_to_cpu(h->owner);
  613. }
  614. static inline void btrfs_set_header_owner(struct btrfs_header *h,
  615. u64 val)
  616. {
  617. h->owner = cpu_to_le64(val);
  618. }
  619. static inline u16 btrfs_header_nritems(struct btrfs_header *h)
  620. {
  621. return le16_to_cpu(h->nritems);
  622. }
  623. static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
  624. {
  625. h->nritems = cpu_to_le16(val);
  626. }
  627. static inline u16 btrfs_header_flags(struct btrfs_header *h)
  628. {
  629. return le16_to_cpu(h->flags);
  630. }
  631. static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
  632. {
  633. h->flags = cpu_to_le16(val);
  634. }
  635. static inline int btrfs_header_level(struct btrfs_header *h)
  636. {
  637. return h->level;
  638. }
  639. static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
  640. {
  641. BUG_ON(level > BTRFS_MAX_LEVEL);
  642. h->level = level;
  643. }
  644. static inline int btrfs_is_leaf(struct btrfs_node *n)
  645. {
  646. return (btrfs_header_level(&n->header) == 0);
  647. }
  648. static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
  649. {
  650. return le64_to_cpu(item->blocknr);
  651. }
  652. static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
  653. {
  654. item->blocknr = cpu_to_le64(val);
  655. }
  656. static inline u64 btrfs_root_dirid(struct btrfs_root_item *item)
  657. {
  658. return le64_to_cpu(item->root_dirid);
  659. }
  660. static inline void btrfs_set_root_dirid(struct btrfs_root_item *item, u64 val)
  661. {
  662. item->root_dirid = cpu_to_le64(val);
  663. }
  664. static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
  665. {
  666. return le32_to_cpu(item->refs);
  667. }
  668. static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
  669. {
  670. item->refs = cpu_to_le32(val);
  671. }
  672. static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
  673. {
  674. return le64_to_cpu(s->blocknr);
  675. }
  676. static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
  677. {
  678. s->blocknr = cpu_to_le64(val);
  679. }
  680. static inline u64 btrfs_super_generation(struct btrfs_super_block *s)
  681. {
  682. return le64_to_cpu(s->generation);
  683. }
  684. static inline void btrfs_set_super_generation(struct btrfs_super_block *s,
  685. u64 val)
  686. {
  687. s->generation = cpu_to_le64(val);
  688. }
  689. static inline u64 btrfs_super_root(struct btrfs_super_block *s)
  690. {
  691. return le64_to_cpu(s->root);
  692. }
  693. static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
  694. {
  695. s->root = cpu_to_le64(val);
  696. }
  697. static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
  698. {
  699. return le64_to_cpu(s->total_blocks);
  700. }
  701. static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
  702. u64 val)
  703. {
  704. s->total_blocks = cpu_to_le64(val);
  705. }
  706. static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
  707. {
  708. return le64_to_cpu(s->blocks_used);
  709. }
  710. static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
  711. u64 val)
  712. {
  713. s->blocks_used = cpu_to_le64(val);
  714. }
  715. static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
  716. {
  717. return le32_to_cpu(s->blocksize);
  718. }
  719. static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
  720. u32 val)
  721. {
  722. s->blocksize = cpu_to_le32(val);
  723. }
  724. static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
  725. {
  726. return le64_to_cpu(s->root_dir_objectid);
  727. }
  728. static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
  729. val)
  730. {
  731. s->root_dir_objectid = cpu_to_le64(val);
  732. }
  733. static inline u64 btrfs_super_last_device_id(struct btrfs_super_block *s)
  734. {
  735. return le64_to_cpu(s->last_device_id);
  736. }
  737. static inline void btrfs_set_super_last_device_id(struct btrfs_super_block *s,
  738. u64 val)
  739. {
  740. s->last_device_id = cpu_to_le64(val);
  741. }
  742. static inline u64 btrfs_super_device_id(struct btrfs_super_block *s)
  743. {
  744. return le64_to_cpu(s->device_id);
  745. }
  746. static inline void btrfs_set_super_device_id(struct btrfs_super_block *s,
  747. u64 val)
  748. {
  749. s->device_id = cpu_to_le64(val);
  750. }
  751. static inline u64 btrfs_super_device_block_start(struct btrfs_super_block *s)
  752. {
  753. return le64_to_cpu(s->device_block_start);
  754. }
  755. static inline void btrfs_set_super_device_block_start(struct btrfs_super_block
  756. *s, u64 val)
  757. {
  758. s->device_block_start = cpu_to_le64(val);
  759. }
  760. static inline u64 btrfs_super_device_num_blocks(struct btrfs_super_block *s)
  761. {
  762. return le64_to_cpu(s->device_num_blocks);
  763. }
  764. static inline void btrfs_set_super_device_num_blocks(struct btrfs_super_block
  765. *s, u64 val)
  766. {
  767. s->device_num_blocks = cpu_to_le64(val);
  768. }
  769. static inline u64 btrfs_super_device_root(struct btrfs_super_block *s)
  770. {
  771. return le64_to_cpu(s->device_root);
  772. }
  773. static inline void btrfs_set_super_device_root(struct btrfs_super_block
  774. *s, u64 val)
  775. {
  776. s->device_root = cpu_to_le64(val);
  777. }
  778. static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
  779. {
  780. return (u8 *)l->items;
  781. }
  782. static inline int btrfs_file_extent_type(struct btrfs_file_extent_item *e)
  783. {
  784. return e->type;
  785. }
  786. static inline void btrfs_set_file_extent_type(struct btrfs_file_extent_item *e,
  787. u8 val)
  788. {
  789. e->type = val;
  790. }
  791. static inline char *btrfs_file_extent_inline_start(struct
  792. btrfs_file_extent_item *e)
  793. {
  794. return (char *)(&e->disk_blocknr);
  795. }
  796. static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
  797. {
  798. return (unsigned long)(&((struct
  799. btrfs_file_extent_item *)NULL)->disk_blocknr) + datasize;
  800. }
  801. static inline u32 btrfs_file_extent_inline_len(struct btrfs_item *e)
  802. {
  803. struct btrfs_file_extent_item *fe = NULL;
  804. return btrfs_item_size(e) - (unsigned long)(&fe->disk_blocknr);
  805. }
  806. static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
  807. *e)
  808. {
  809. return le64_to_cpu(e->disk_blocknr);
  810. }
  811. static inline void btrfs_set_file_extent_disk_blocknr(struct
  812. btrfs_file_extent_item
  813. *e, u64 val)
  814. {
  815. e->disk_blocknr = cpu_to_le64(val);
  816. }
  817. static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e)
  818. {
  819. return le64_to_cpu(e->generation);
  820. }
  821. static inline void btrfs_set_file_extent_generation(struct
  822. btrfs_file_extent_item *e,
  823. u64 val)
  824. {
  825. e->generation = cpu_to_le64(val);
  826. }
  827. static inline u64 btrfs_file_extent_disk_num_blocks(struct
  828. btrfs_file_extent_item *e)
  829. {
  830. return le64_to_cpu(e->disk_num_blocks);
  831. }
  832. static inline void btrfs_set_file_extent_disk_num_blocks(struct
  833. btrfs_file_extent_item
  834. *e, u64 val)
  835. {
  836. e->disk_num_blocks = cpu_to_le64(val);
  837. }
  838. static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
  839. {
  840. return le64_to_cpu(e->offset);
  841. }
  842. static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
  843. *e, u64 val)
  844. {
  845. e->offset = cpu_to_le64(val);
  846. }
  847. static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
  848. *e)
  849. {
  850. return le64_to_cpu(e->num_blocks);
  851. }
  852. static inline void btrfs_set_file_extent_num_blocks(struct
  853. btrfs_file_extent_item *e,
  854. u64 val)
  855. {
  856. e->num_blocks = cpu_to_le64(val);
  857. }
  858. static inline u16 btrfs_device_pathlen(struct btrfs_device_item *d)
  859. {
  860. return le16_to_cpu(d->pathlen);
  861. }
  862. static inline void btrfs_set_device_pathlen(struct btrfs_device_item *d,
  863. u16 val)
  864. {
  865. d->pathlen = cpu_to_le16(val);
  866. }
  867. static inline u64 btrfs_device_id(struct btrfs_device_item *d)
  868. {
  869. return le64_to_cpu(d->device_id);
  870. }
  871. static inline void btrfs_set_device_id(struct btrfs_device_item *d,
  872. u64 val)
  873. {
  874. d->device_id = cpu_to_le64(val);
  875. }
  876. static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
  877. {
  878. return sb->s_fs_info;
  879. }
  880. static inline void btrfs_check_bounds(void *vptr, size_t len,
  881. void *vcontainer, size_t container_len)
  882. {
  883. char *ptr = vptr;
  884. char *container = vcontainer;
  885. WARN_ON(ptr < container);
  886. WARN_ON(ptr + len > container + container_len);
  887. }
  888. static inline void btrfs_memcpy(struct btrfs_root *root,
  889. void *dst_block,
  890. void *dst, const void *src, size_t nr)
  891. {
  892. btrfs_check_bounds(dst, nr, dst_block, root->fs_info->sb->s_blocksize);
  893. memcpy(dst, src, nr);
  894. }
  895. static inline void btrfs_memmove(struct btrfs_root *root,
  896. void *dst_block,
  897. void *dst, void *src, size_t nr)
  898. {
  899. btrfs_check_bounds(dst, nr, dst_block, root->fs_info->sb->s_blocksize);
  900. memmove(dst, src, nr);
  901. }
  902. static inline void btrfs_mark_buffer_dirty(struct buffer_head *bh)
  903. {
  904. WARN_ON(!atomic_read(&bh->b_count));
  905. mark_buffer_dirty(bh);
  906. }
  907. /* helper function to cast into the data area of the leaf. */
  908. #define btrfs_item_ptr(leaf, slot, type) \
  909. ((type *)(btrfs_leaf_data(leaf) + \
  910. btrfs_item_offset((leaf)->items + (slot))))
  911. /* extent-tree.c */
  912. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  913. struct btrfs_block_group_cache
  914. *hint, u64 search_start,
  915. int data, int owner);
  916. int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
  917. struct btrfs_root *root);
  918. struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  919. struct btrfs_root *root, u64 hint);
  920. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  921. struct btrfs_root *root, u64 owner,
  922. u64 num_blocks, u64 search_start,
  923. u64 search_end, struct btrfs_key *ins, int data);
  924. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  925. struct buffer_head *buf);
  926. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  927. *root, u64 blocknr, u64 num_blocks, int pin);
  928. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
  929. btrfs_root *root);
  930. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  931. struct btrfs_root *root,
  932. u64 blocknr, u64 num_blocks);
  933. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  934. struct btrfs_root *root);
  935. int btrfs_free_block_groups(struct btrfs_fs_info *info);
  936. int btrfs_read_block_groups(struct btrfs_root *root);
  937. /* ctree.c */
  938. int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
  939. *root, struct btrfs_path *path, u32 data_size);
  940. int btrfs_truncate_item(struct btrfs_trans_handle *trans,
  941. struct btrfs_root *root,
  942. struct btrfs_path *path,
  943. u32 new_size);
  944. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
  945. *root, struct btrfs_key *key, struct btrfs_path *p, int
  946. ins_len, int cow);
  947. void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
  948. struct btrfs_path *btrfs_alloc_path(void);
  949. void btrfs_free_path(struct btrfs_path *p);
  950. void btrfs_init_path(struct btrfs_path *p);
  951. int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  952. struct btrfs_path *path);
  953. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  954. *root, struct btrfs_key *key, void *data, u32 data_size);
  955. int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
  956. *root, struct btrfs_path *path, struct btrfs_key
  957. *cpu_key, u32 data_size);
  958. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
  959. int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
  960. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  961. *root, struct buffer_head *snap);
  962. /* root-item.c */
  963. int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  964. struct btrfs_key *key);
  965. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
  966. *root, struct btrfs_key *key, struct btrfs_root_item
  967. *item);
  968. int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
  969. *root, struct btrfs_key *key, struct btrfs_root_item
  970. *item);
  971. int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
  972. btrfs_root_item *item, struct btrfs_key *key);
  973. /* dir-item.c */
  974. int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
  975. *root, const char *name, int name_len, u64 dir,
  976. struct btrfs_key *location, u8 type);
  977. struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
  978. struct btrfs_root *root,
  979. struct btrfs_path *path, u64 dir,
  980. const char *name, int name_len,
  981. int mod);
  982. struct btrfs_dir_item *
  983. btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
  984. struct btrfs_root *root,
  985. struct btrfs_path *path, u64 dir,
  986. u64 objectid, const char *name, int name_len,
  987. int mod);
  988. struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
  989. struct btrfs_path *path,
  990. const char *name, int name_len);
  991. int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
  992. struct btrfs_root *root,
  993. struct btrfs_path *path,
  994. struct btrfs_dir_item *di);
  995. /* inode-map.c */
  996. int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
  997. struct btrfs_root *fs_root,
  998. u64 dirid, u64 *objectid);
  999. int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
  1000. /* inode-item.c */
  1001. int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  1002. *root, u64 objectid, struct btrfs_inode_item
  1003. *inode_item);
  1004. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  1005. *root, struct btrfs_path *path,
  1006. struct btrfs_key *location, int mod);
  1007. /* file-item.c */
  1008. int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
  1009. struct btrfs_root *root,
  1010. u64 objectid, u64 pos, u64 offset,
  1011. u64 num_blocks);
  1012. int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
  1013. struct btrfs_root *root,
  1014. struct btrfs_path *path, u64 objectid,
  1015. u64 blocknr, int mod);
  1016. int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
  1017. struct btrfs_root *root,
  1018. u64 objectid, u64 offset,
  1019. char *data, size_t len);
  1020. int btrfs_csum_verify_file_block(struct btrfs_root *root,
  1021. u64 objectid, u64 offset,
  1022. char *data, size_t len);
  1023. struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
  1024. struct btrfs_root *root,
  1025. struct btrfs_path *path,
  1026. u64 objectid, u64 offset,
  1027. int cow);
  1028. /* super.c */
  1029. #endif