ctree.h 29 KB

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