ctree.h 31 KB

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