ctree.h 26 KB

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