ctree.h 30 KB

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