ctree.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. #ifndef __BTRFS__
  2. #define __BTRFS__
  3. #include <linux/fs.h>
  4. #include <linux/buffer_head.h>
  5. #include <linux/workqueue.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. spinlock_t hash_lock;
  255. struct btrfs_root *extent_root;
  256. struct btrfs_root *tree_root;
  257. struct btrfs_root *dev_root;
  258. struct radix_tree_root fs_roots_radix;
  259. struct radix_tree_root pending_del_radix;
  260. struct radix_tree_root pinned_radix;
  261. struct radix_tree_root dev_radix;
  262. struct radix_tree_root block_group_radix;
  263. struct radix_tree_root block_group_data_radix;
  264. struct radix_tree_root extent_map_radix;
  265. u64 extent_tree_insert[BTRFS_MAX_LEVEL * 3];
  266. int extent_tree_insert_nr;
  267. u64 extent_tree_prealloc[BTRFS_MAX_LEVEL * 3];
  268. int extent_tree_prealloc_nr;
  269. u64 generation;
  270. struct btrfs_transaction *running_transaction;
  271. struct btrfs_super_block *disk_super;
  272. struct buffer_head *sb_buffer;
  273. struct super_block *sb;
  274. struct inode *btree_inode;
  275. struct mutex trans_mutex;
  276. struct mutex fs_mutex;
  277. struct list_head trans_list;
  278. struct list_head dead_roots;
  279. struct crypto_hash *hash_tfm;
  280. struct delayed_work trans_work;
  281. int do_barriers;
  282. int closing;
  283. };
  284. /*
  285. * in ram representation of the tree. extent_root is used for all allocations
  286. * and for the extent tree extent_root root.
  287. */
  288. struct btrfs_root {
  289. struct buffer_head *node;
  290. struct buffer_head *commit_root;
  291. struct btrfs_root_item root_item;
  292. struct btrfs_key root_key;
  293. struct btrfs_fs_info *fs_info;
  294. struct inode *inode;
  295. u64 objectid;
  296. u64 last_trans;
  297. u32 blocksize;
  298. int ref_cows;
  299. u32 type;
  300. u64 highest_inode;
  301. u64 last_inode_alloc;
  302. };
  303. /* the lower bits in the key flags defines the item type */
  304. #define BTRFS_KEY_TYPE_MAX 256
  305. #define BTRFS_KEY_TYPE_SHIFT 24
  306. #define BTRFS_KEY_TYPE_MASK (((u32)BTRFS_KEY_TYPE_MAX - 1) << \
  307. BTRFS_KEY_TYPE_SHIFT)
  308. /*
  309. * inode items have the data typically returned from stat and store other
  310. * info about object characteristics. There is one for every file and dir in
  311. * the FS
  312. */
  313. #define BTRFS_INODE_ITEM_KEY 1
  314. /* reserve 2-15 close to the inode for later flexibility */
  315. /*
  316. * dir items are the name -> inode pointers in a directory. There is one
  317. * for every name in a directory.
  318. */
  319. #define BTRFS_DIR_ITEM_KEY 16
  320. #define BTRFS_DIR_INDEX_KEY 17
  321. /*
  322. * extent data is for file data
  323. */
  324. #define BTRFS_EXTENT_DATA_KEY 18
  325. /*
  326. * csum items have the checksums for data in the extents
  327. */
  328. #define BTRFS_CSUM_ITEM_KEY 19
  329. /* reserve 20-31 for other file stuff */
  330. /*
  331. * root items point to tree roots. There are typically in the root
  332. * tree used by the super block to find all the other trees
  333. */
  334. #define BTRFS_ROOT_ITEM_KEY 32
  335. /*
  336. * extent items are in the extent map tree. These record which blocks
  337. * are used, and how many references there are to each block
  338. */
  339. #define BTRFS_EXTENT_ITEM_KEY 33
  340. /*
  341. * block groups give us hints into the extent allocation trees. Which
  342. * blocks are free etc etc
  343. */
  344. #define BTRFS_BLOCK_GROUP_ITEM_KEY 34
  345. /*
  346. * dev items list the devices that make up the FS
  347. */
  348. #define BTRFS_DEV_ITEM_KEY 35
  349. /*
  350. * string items are for debugging. They just store a short string of
  351. * data in the FS
  352. */
  353. #define BTRFS_STRING_ITEM_KEY 253
  354. static inline u64 btrfs_block_group_used(struct btrfs_block_group_item *bi)
  355. {
  356. return le64_to_cpu(bi->used);
  357. }
  358. static inline void btrfs_set_block_group_used(struct
  359. btrfs_block_group_item *bi,
  360. u64 val)
  361. {
  362. bi->used = cpu_to_le64(val);
  363. }
  364. static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
  365. {
  366. return le64_to_cpu(i->generation);
  367. }
  368. static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
  369. u64 val)
  370. {
  371. i->generation = cpu_to_le64(val);
  372. }
  373. static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
  374. {
  375. return le64_to_cpu(i->size);
  376. }
  377. static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
  378. {
  379. i->size = cpu_to_le64(val);
  380. }
  381. static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
  382. {
  383. return le64_to_cpu(i->nblocks);
  384. }
  385. static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
  386. {
  387. i->nblocks = cpu_to_le64(val);
  388. }
  389. static inline u64 btrfs_inode_block_group(struct btrfs_inode_item *i)
  390. {
  391. return le64_to_cpu(i->block_group);
  392. }
  393. static inline void btrfs_set_inode_block_group(struct btrfs_inode_item *i,
  394. u64 val)
  395. {
  396. i->block_group = cpu_to_le64(val);
  397. }
  398. static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
  399. {
  400. return le32_to_cpu(i->nlink);
  401. }
  402. static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
  403. {
  404. i->nlink = cpu_to_le32(val);
  405. }
  406. static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
  407. {
  408. return le32_to_cpu(i->uid);
  409. }
  410. static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
  411. {
  412. i->uid = cpu_to_le32(val);
  413. }
  414. static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
  415. {
  416. return le32_to_cpu(i->gid);
  417. }
  418. static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
  419. {
  420. i->gid = cpu_to_le32(val);
  421. }
  422. static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
  423. {
  424. return le32_to_cpu(i->mode);
  425. }
  426. static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
  427. {
  428. i->mode = cpu_to_le32(val);
  429. }
  430. static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
  431. {
  432. return le32_to_cpu(i->rdev);
  433. }
  434. static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
  435. {
  436. i->rdev = cpu_to_le32(val);
  437. }
  438. static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
  439. {
  440. return le16_to_cpu(i->flags);
  441. }
  442. static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
  443. {
  444. i->flags = cpu_to_le16(val);
  445. }
  446. static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
  447. {
  448. return le16_to_cpu(i->compat_flags);
  449. }
  450. static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
  451. u16 val)
  452. {
  453. i->compat_flags = cpu_to_le16(val);
  454. }
  455. static inline u64 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
  456. {
  457. return le64_to_cpu(ts->sec);
  458. }
  459. static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
  460. u64 val)
  461. {
  462. ts->sec = cpu_to_le64(val);
  463. }
  464. static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
  465. {
  466. return le32_to_cpu(ts->nsec);
  467. }
  468. static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
  469. u32 val)
  470. {
  471. ts->nsec = cpu_to_le32(val);
  472. }
  473. static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
  474. {
  475. return le32_to_cpu(ei->refs);
  476. }
  477. static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
  478. {
  479. ei->refs = cpu_to_le32(val);
  480. }
  481. static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
  482. {
  483. return le64_to_cpu(ei->owner);
  484. }
  485. static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
  486. {
  487. ei->owner = cpu_to_le64(val);
  488. }
  489. static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
  490. {
  491. return le64_to_cpu(n->ptrs[nr].blockptr);
  492. }
  493. static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
  494. u64 val)
  495. {
  496. n->ptrs[nr].blockptr = cpu_to_le64(val);
  497. }
  498. static inline u32 btrfs_item_offset(struct btrfs_item *item)
  499. {
  500. return le32_to_cpu(item->offset);
  501. }
  502. static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
  503. {
  504. item->offset = cpu_to_le32(val);
  505. }
  506. static inline u32 btrfs_item_end(struct btrfs_item *item)
  507. {
  508. return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
  509. }
  510. static inline u16 btrfs_item_size(struct btrfs_item *item)
  511. {
  512. return le16_to_cpu(item->size);
  513. }
  514. static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
  515. {
  516. item->size = cpu_to_le16(val);
  517. }
  518. static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
  519. {
  520. return le16_to_cpu(d->flags);
  521. }
  522. static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
  523. {
  524. d->flags = cpu_to_le16(val);
  525. }
  526. static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
  527. {
  528. return d->type;
  529. }
  530. static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
  531. {
  532. d->type = val;
  533. }
  534. static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
  535. {
  536. return le16_to_cpu(d->name_len);
  537. }
  538. static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
  539. {
  540. d->name_len = cpu_to_le16(val);
  541. }
  542. static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
  543. struct btrfs_disk_key *disk)
  544. {
  545. cpu->offset = le64_to_cpu(disk->offset);
  546. cpu->flags = le32_to_cpu(disk->flags);
  547. cpu->objectid = le64_to_cpu(disk->objectid);
  548. }
  549. static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
  550. struct btrfs_key *cpu)
  551. {
  552. disk->offset = cpu_to_le64(cpu->offset);
  553. disk->flags = cpu_to_le32(cpu->flags);
  554. disk->objectid = cpu_to_le64(cpu->objectid);
  555. }
  556. static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
  557. {
  558. return le64_to_cpu(disk->objectid);
  559. }
  560. static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
  561. u64 val)
  562. {
  563. disk->objectid = cpu_to_le64(val);
  564. }
  565. static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
  566. {
  567. return le64_to_cpu(disk->offset);
  568. }
  569. static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
  570. u64 val)
  571. {
  572. disk->offset = cpu_to_le64(val);
  573. }
  574. static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
  575. {
  576. return le32_to_cpu(disk->flags);
  577. }
  578. static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
  579. u32 val)
  580. {
  581. disk->flags = cpu_to_le32(val);
  582. }
  583. static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
  584. {
  585. return le32_to_cpu(key->flags) >> BTRFS_KEY_TYPE_SHIFT;
  586. }
  587. static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key,
  588. u32 val)
  589. {
  590. u32 flags = btrfs_disk_key_flags(key);
  591. BUG_ON(val >= BTRFS_KEY_TYPE_MAX);
  592. val = val << BTRFS_KEY_TYPE_SHIFT;
  593. flags = (flags & ~BTRFS_KEY_TYPE_MASK) | val;
  594. btrfs_set_disk_key_flags(key, flags);
  595. }
  596. static inline u32 btrfs_key_type(struct btrfs_key *key)
  597. {
  598. return key->flags >> BTRFS_KEY_TYPE_SHIFT;
  599. }
  600. static inline void btrfs_set_key_type(struct btrfs_key *key, u32 val)
  601. {
  602. BUG_ON(val >= BTRFS_KEY_TYPE_MAX);
  603. val = val << BTRFS_KEY_TYPE_SHIFT;
  604. key->flags = (key->flags & ~(BTRFS_KEY_TYPE_MASK)) | val;
  605. }
  606. static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
  607. {
  608. return le64_to_cpu(h->blocknr);
  609. }
  610. static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
  611. {
  612. h->blocknr = cpu_to_le64(blocknr);
  613. }
  614. static inline u64 btrfs_header_generation(struct btrfs_header *h)
  615. {
  616. return le64_to_cpu(h->generation);
  617. }
  618. static inline void btrfs_set_header_generation(struct btrfs_header *h,
  619. u64 val)
  620. {
  621. h->generation = cpu_to_le64(val);
  622. }
  623. static inline u64 btrfs_header_owner(struct btrfs_header *h)
  624. {
  625. return le64_to_cpu(h->owner);
  626. }
  627. static inline void btrfs_set_header_owner(struct btrfs_header *h,
  628. u64 val)
  629. {
  630. h->owner = cpu_to_le64(val);
  631. }
  632. static inline u16 btrfs_header_nritems(struct btrfs_header *h)
  633. {
  634. return le16_to_cpu(h->nritems);
  635. }
  636. static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
  637. {
  638. h->nritems = cpu_to_le16(val);
  639. }
  640. static inline u16 btrfs_header_flags(struct btrfs_header *h)
  641. {
  642. return le16_to_cpu(h->flags);
  643. }
  644. static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
  645. {
  646. h->flags = cpu_to_le16(val);
  647. }
  648. static inline int btrfs_header_level(struct btrfs_header *h)
  649. {
  650. return h->level;
  651. }
  652. static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
  653. {
  654. BUG_ON(level > BTRFS_MAX_LEVEL);
  655. h->level = level;
  656. }
  657. static inline int btrfs_is_leaf(struct btrfs_node *n)
  658. {
  659. return (btrfs_header_level(&n->header) == 0);
  660. }
  661. static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
  662. {
  663. return le64_to_cpu(item->blocknr);
  664. }
  665. static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
  666. {
  667. item->blocknr = cpu_to_le64(val);
  668. }
  669. static inline u64 btrfs_root_dirid(struct btrfs_root_item *item)
  670. {
  671. return le64_to_cpu(item->root_dirid);
  672. }
  673. static inline void btrfs_set_root_dirid(struct btrfs_root_item *item, u64 val)
  674. {
  675. item->root_dirid = cpu_to_le64(val);
  676. }
  677. static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
  678. {
  679. return le32_to_cpu(item->refs);
  680. }
  681. static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
  682. {
  683. item->refs = cpu_to_le32(val);
  684. }
  685. static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
  686. {
  687. return le64_to_cpu(s->blocknr);
  688. }
  689. static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
  690. {
  691. s->blocknr = cpu_to_le64(val);
  692. }
  693. static inline u64 btrfs_super_generation(struct btrfs_super_block *s)
  694. {
  695. return le64_to_cpu(s->generation);
  696. }
  697. static inline void btrfs_set_super_generation(struct btrfs_super_block *s,
  698. u64 val)
  699. {
  700. s->generation = cpu_to_le64(val);
  701. }
  702. static inline u64 btrfs_super_root(struct btrfs_super_block *s)
  703. {
  704. return le64_to_cpu(s->root);
  705. }
  706. static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
  707. {
  708. s->root = cpu_to_le64(val);
  709. }
  710. static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
  711. {
  712. return le64_to_cpu(s->total_blocks);
  713. }
  714. static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
  715. u64 val)
  716. {
  717. s->total_blocks = cpu_to_le64(val);
  718. }
  719. static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
  720. {
  721. return le64_to_cpu(s->blocks_used);
  722. }
  723. static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
  724. u64 val)
  725. {
  726. s->blocks_used = cpu_to_le64(val);
  727. }
  728. static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
  729. {
  730. return le32_to_cpu(s->blocksize);
  731. }
  732. static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
  733. u32 val)
  734. {
  735. s->blocksize = cpu_to_le32(val);
  736. }
  737. static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
  738. {
  739. return le64_to_cpu(s->root_dir_objectid);
  740. }
  741. static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
  742. val)
  743. {
  744. s->root_dir_objectid = cpu_to_le64(val);
  745. }
  746. static inline u64 btrfs_super_last_device_id(struct btrfs_super_block *s)
  747. {
  748. return le64_to_cpu(s->last_device_id);
  749. }
  750. static inline void btrfs_set_super_last_device_id(struct btrfs_super_block *s,
  751. u64 val)
  752. {
  753. s->last_device_id = cpu_to_le64(val);
  754. }
  755. static inline u64 btrfs_super_device_id(struct btrfs_super_block *s)
  756. {
  757. return le64_to_cpu(s->device_id);
  758. }
  759. static inline void btrfs_set_super_device_id(struct btrfs_super_block *s,
  760. u64 val)
  761. {
  762. s->device_id = cpu_to_le64(val);
  763. }
  764. static inline u64 btrfs_super_device_block_start(struct btrfs_super_block *s)
  765. {
  766. return le64_to_cpu(s->device_block_start);
  767. }
  768. static inline void btrfs_set_super_device_block_start(struct btrfs_super_block
  769. *s, u64 val)
  770. {
  771. s->device_block_start = cpu_to_le64(val);
  772. }
  773. static inline u64 btrfs_super_device_num_blocks(struct btrfs_super_block *s)
  774. {
  775. return le64_to_cpu(s->device_num_blocks);
  776. }
  777. static inline void btrfs_set_super_device_num_blocks(struct btrfs_super_block
  778. *s, u64 val)
  779. {
  780. s->device_num_blocks = cpu_to_le64(val);
  781. }
  782. static inline u64 btrfs_super_device_root(struct btrfs_super_block *s)
  783. {
  784. return le64_to_cpu(s->device_root);
  785. }
  786. static inline void btrfs_set_super_device_root(struct btrfs_super_block
  787. *s, u64 val)
  788. {
  789. s->device_root = cpu_to_le64(val);
  790. }
  791. static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
  792. {
  793. return (u8 *)l->items;
  794. }
  795. static inline int btrfs_file_extent_type(struct btrfs_file_extent_item *e)
  796. {
  797. return e->type;
  798. }
  799. static inline void btrfs_set_file_extent_type(struct btrfs_file_extent_item *e,
  800. u8 val)
  801. {
  802. e->type = val;
  803. }
  804. static inline char *btrfs_file_extent_inline_start(struct
  805. btrfs_file_extent_item *e)
  806. {
  807. return (char *)(&e->disk_blocknr);
  808. }
  809. static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
  810. {
  811. return (unsigned long)(&((struct
  812. btrfs_file_extent_item *)NULL)->disk_blocknr) + datasize;
  813. }
  814. static inline u32 btrfs_file_extent_inline_len(struct btrfs_item *e)
  815. {
  816. struct btrfs_file_extent_item *fe = NULL;
  817. return btrfs_item_size(e) - (unsigned long)(&fe->disk_blocknr);
  818. }
  819. static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
  820. *e)
  821. {
  822. return le64_to_cpu(e->disk_blocknr);
  823. }
  824. static inline void btrfs_set_file_extent_disk_blocknr(struct
  825. btrfs_file_extent_item
  826. *e, u64 val)
  827. {
  828. e->disk_blocknr = cpu_to_le64(val);
  829. }
  830. static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e)
  831. {
  832. return le64_to_cpu(e->generation);
  833. }
  834. static inline void btrfs_set_file_extent_generation(struct
  835. btrfs_file_extent_item *e,
  836. u64 val)
  837. {
  838. e->generation = cpu_to_le64(val);
  839. }
  840. static inline u64 btrfs_file_extent_disk_num_blocks(struct
  841. btrfs_file_extent_item *e)
  842. {
  843. return le64_to_cpu(e->disk_num_blocks);
  844. }
  845. static inline void btrfs_set_file_extent_disk_num_blocks(struct
  846. btrfs_file_extent_item
  847. *e, u64 val)
  848. {
  849. e->disk_num_blocks = cpu_to_le64(val);
  850. }
  851. static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
  852. {
  853. return le64_to_cpu(e->offset);
  854. }
  855. static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
  856. *e, u64 val)
  857. {
  858. e->offset = cpu_to_le64(val);
  859. }
  860. static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
  861. *e)
  862. {
  863. return le64_to_cpu(e->num_blocks);
  864. }
  865. static inline void btrfs_set_file_extent_num_blocks(struct
  866. btrfs_file_extent_item *e,
  867. u64 val)
  868. {
  869. e->num_blocks = cpu_to_le64(val);
  870. }
  871. static inline u16 btrfs_device_pathlen(struct btrfs_device_item *d)
  872. {
  873. return le16_to_cpu(d->pathlen);
  874. }
  875. static inline void btrfs_set_device_pathlen(struct btrfs_device_item *d,
  876. u16 val)
  877. {
  878. d->pathlen = cpu_to_le16(val);
  879. }
  880. static inline u64 btrfs_device_id(struct btrfs_device_item *d)
  881. {
  882. return le64_to_cpu(d->device_id);
  883. }
  884. static inline void btrfs_set_device_id(struct btrfs_device_item *d,
  885. u64 val)
  886. {
  887. d->device_id = cpu_to_le64(val);
  888. }
  889. static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
  890. {
  891. return sb->s_fs_info;
  892. }
  893. static inline void btrfs_check_bounds(void *vptr, size_t len,
  894. void *vcontainer, size_t container_len)
  895. {
  896. char *ptr = vptr;
  897. char *container = vcontainer;
  898. WARN_ON(ptr < container);
  899. WARN_ON(ptr + len > container + container_len);
  900. }
  901. static inline void btrfs_memcpy(struct btrfs_root *root,
  902. void *dst_block,
  903. void *dst, const void *src, size_t nr)
  904. {
  905. btrfs_check_bounds(dst, nr, dst_block, root->fs_info->sb->s_blocksize);
  906. memcpy(dst, src, nr);
  907. }
  908. static inline void btrfs_memmove(struct btrfs_root *root,
  909. void *dst_block,
  910. void *dst, void *src, size_t nr)
  911. {
  912. btrfs_check_bounds(dst, nr, dst_block, root->fs_info->sb->s_blocksize);
  913. memmove(dst, src, nr);
  914. }
  915. static inline void btrfs_mark_buffer_dirty(struct buffer_head *bh)
  916. {
  917. WARN_ON(!atomic_read(&bh->b_count));
  918. mark_buffer_dirty(bh);
  919. }
  920. /* helper function to cast into the data area of the leaf. */
  921. #define btrfs_item_ptr(leaf, slot, type) \
  922. ((type *)(btrfs_leaf_data(leaf) + \
  923. btrfs_item_offset((leaf)->items + (slot))))
  924. /* extent-tree.c */
  925. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  926. struct btrfs_block_group_cache
  927. *hint, u64 search_start,
  928. int data, int owner);
  929. int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
  930. struct btrfs_root *root);
  931. struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  932. struct btrfs_root *root, u64 hint);
  933. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  934. struct btrfs_root *root, u64 owner,
  935. u64 num_blocks, u64 search_start,
  936. u64 search_end, struct btrfs_key *ins, int data);
  937. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  938. struct buffer_head *buf);
  939. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  940. *root, u64 blocknr, u64 num_blocks, int pin);
  941. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
  942. btrfs_root *root);
  943. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  944. struct btrfs_root *root,
  945. u64 blocknr, u64 num_blocks);
  946. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  947. struct btrfs_root *root);
  948. int btrfs_free_block_groups(struct btrfs_fs_info *info);
  949. int btrfs_read_block_groups(struct btrfs_root *root);
  950. /* ctree.c */
  951. int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
  952. *root, struct btrfs_path *path, u32 data_size);
  953. int btrfs_truncate_item(struct btrfs_trans_handle *trans,
  954. struct btrfs_root *root,
  955. struct btrfs_path *path,
  956. u32 new_size);
  957. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
  958. *root, struct btrfs_key *key, struct btrfs_path *p, int
  959. ins_len, int cow);
  960. void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
  961. struct btrfs_path *btrfs_alloc_path(void);
  962. void btrfs_free_path(struct btrfs_path *p);
  963. void btrfs_init_path(struct btrfs_path *p);
  964. int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  965. struct btrfs_path *path);
  966. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  967. *root, struct btrfs_key *key, void *data, u32 data_size);
  968. int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
  969. *root, struct btrfs_path *path, struct btrfs_key
  970. *cpu_key, u32 data_size);
  971. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
  972. int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
  973. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  974. *root, struct buffer_head *snap);
  975. /* root-item.c */
  976. int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  977. struct btrfs_key *key);
  978. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
  979. *root, struct btrfs_key *key, struct btrfs_root_item
  980. *item);
  981. int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
  982. *root, struct btrfs_key *key, struct btrfs_root_item
  983. *item);
  984. int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
  985. btrfs_root_item *item, struct btrfs_key *key);
  986. /* dir-item.c */
  987. int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
  988. *root, const char *name, int name_len, u64 dir,
  989. struct btrfs_key *location, u8 type);
  990. struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
  991. struct btrfs_root *root,
  992. struct btrfs_path *path, u64 dir,
  993. const char *name, int name_len,
  994. int mod);
  995. struct btrfs_dir_item *
  996. btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
  997. struct btrfs_root *root,
  998. struct btrfs_path *path, u64 dir,
  999. u64 objectid, const char *name, int name_len,
  1000. int mod);
  1001. struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
  1002. struct btrfs_path *path,
  1003. const char *name, int name_len);
  1004. int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
  1005. struct btrfs_root *root,
  1006. struct btrfs_path *path,
  1007. struct btrfs_dir_item *di);
  1008. /* inode-map.c */
  1009. int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
  1010. struct btrfs_root *fs_root,
  1011. u64 dirid, u64 *objectid);
  1012. int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
  1013. /* inode-item.c */
  1014. int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  1015. *root, u64 objectid, struct btrfs_inode_item
  1016. *inode_item);
  1017. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  1018. *root, struct btrfs_path *path,
  1019. struct btrfs_key *location, int mod);
  1020. /* file-item.c */
  1021. int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
  1022. struct btrfs_root *root,
  1023. u64 objectid, u64 pos, u64 offset,
  1024. u64 disk_num_blocks,
  1025. u64 num_blocks);
  1026. int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
  1027. struct btrfs_root *root,
  1028. struct btrfs_path *path, u64 objectid,
  1029. u64 blocknr, int mod);
  1030. int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
  1031. struct btrfs_root *root,
  1032. u64 objectid, u64 offset,
  1033. char *data, size_t len);
  1034. int btrfs_csum_verify_file_block(struct btrfs_root *root,
  1035. u64 objectid, u64 offset,
  1036. char *data, size_t len);
  1037. struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
  1038. struct btrfs_root *root,
  1039. struct btrfs_path *path,
  1040. u64 objectid, u64 offset,
  1041. int cow);
  1042. int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
  1043. struct btrfs_root *root, struct btrfs_path *path,
  1044. u64 isize);
  1045. /* super.c */
  1046. #endif