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