ctree.h 28 KB

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