ctree.h 25 KB

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