ctree.h 26 KB

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