ctree.h 29 KB

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