ctree.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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. /* tag for the radix tree of block groups in ram */
  217. #define BTRFS_BLOCK_GROUP_DIRTY 0
  218. #define BTRFS_BLOCK_GROUP_HINTS 8
  219. #define BTRFS_BLOCK_GROUP_SIZE (256 * 1024 * 1024)
  220. struct btrfs_block_group_item {
  221. __le64 used;
  222. } __attribute__ ((__packed__));
  223. struct btrfs_block_group_cache {
  224. struct btrfs_key key;
  225. struct btrfs_block_group_item item;
  226. u64 first_free;
  227. u64 last_alloc;
  228. };
  229. struct crypto_hash;
  230. struct btrfs_fs_info {
  231. struct btrfs_root *extent_root;
  232. struct btrfs_root *tree_root;
  233. struct btrfs_root *dev_root;
  234. struct btrfs_block_group_cache *block_group_cache;
  235. struct radix_tree_root fs_roots_radix;
  236. struct radix_tree_root pending_del_radix;
  237. struct radix_tree_root pinned_radix;
  238. struct radix_tree_root dev_radix;
  239. struct radix_tree_root block_group_radix;
  240. u64 extent_tree_insert[BTRFS_MAX_LEVEL * 3];
  241. int extent_tree_insert_nr;
  242. u64 extent_tree_prealloc[BTRFS_MAX_LEVEL * 3];
  243. int extent_tree_prealloc_nr;
  244. u64 generation;
  245. struct btrfs_transaction *running_transaction;
  246. struct btrfs_super_block *disk_super;
  247. struct buffer_head *sb_buffer;
  248. struct super_block *sb;
  249. struct inode *btree_inode;
  250. struct mutex trans_mutex;
  251. struct mutex fs_mutex;
  252. struct list_head trans_list;
  253. struct crypto_hash *hash_tfm;
  254. spinlock_t hash_lock;
  255. int do_barriers;
  256. struct kobject kobj;
  257. };
  258. /*
  259. * in ram representation of the tree. extent_root is used for all allocations
  260. * and for the extent tree extent_root root.
  261. */
  262. struct btrfs_root {
  263. struct buffer_head *node;
  264. struct buffer_head *commit_root;
  265. struct btrfs_root_item root_item;
  266. struct btrfs_key root_key;
  267. struct btrfs_fs_info *fs_info;
  268. struct inode *inode;
  269. u64 objectid;
  270. u64 last_trans;
  271. u32 blocksize;
  272. int ref_cows;
  273. u32 type;
  274. u64 highest_inode;
  275. u64 last_inode_alloc;
  276. };
  277. /* the lower bits in the key flags defines the item type */
  278. #define BTRFS_KEY_TYPE_MAX 256
  279. #define BTRFS_KEY_TYPE_SHIFT 24
  280. #define BTRFS_KEY_TYPE_MASK (((u32)BTRFS_KEY_TYPE_MAX - 1) << \
  281. BTRFS_KEY_TYPE_SHIFT)
  282. /*
  283. * inode items have the data typically returned from stat and store other
  284. * info about object characteristics. There is one for every file and dir in
  285. * the FS
  286. */
  287. #define BTRFS_INODE_ITEM_KEY 1
  288. /* reserve 2-15 close to the inode for later flexibility */
  289. /*
  290. * dir items are the name -> inode pointers in a directory. There is one
  291. * for every name in a directory.
  292. */
  293. #define BTRFS_DIR_ITEM_KEY 16
  294. #define BTRFS_DIR_INDEX_KEY 17
  295. /*
  296. * extent data is for file data
  297. */
  298. #define BTRFS_EXTENT_DATA_KEY 18
  299. /*
  300. * csum items have the checksums for data in the extents
  301. */
  302. #define BTRFS_CSUM_ITEM_KEY 19
  303. /* reserve 20-31 for other file stuff */
  304. /*
  305. * root items point to tree roots. There are typically in the root
  306. * tree used by the super block to find all the other trees
  307. */
  308. #define BTRFS_ROOT_ITEM_KEY 32
  309. /*
  310. * extent items are in the extent map tree. These record which blocks
  311. * are used, and how many references there are to each block
  312. */
  313. #define BTRFS_EXTENT_ITEM_KEY 33
  314. /*
  315. * block groups give us hints into the extent allocation trees. Which
  316. * blocks are free etc etc
  317. */
  318. #define BTRFS_BLOCK_GROUP_ITEM_KEY 34
  319. /*
  320. * dev items list the devices that make up the FS
  321. */
  322. #define BTRFS_DEV_ITEM_KEY 35
  323. /*
  324. * string items are for debugging. They just store a short string of
  325. * data in the FS
  326. */
  327. #define BTRFS_STRING_ITEM_KEY 253
  328. static inline u64 btrfs_block_group_used(struct btrfs_block_group_item *bi)
  329. {
  330. return le64_to_cpu(bi->used);
  331. }
  332. static inline void btrfs_set_block_group_used(struct
  333. btrfs_block_group_item *bi,
  334. u64 val)
  335. {
  336. bi->used = cpu_to_le64(val);
  337. }
  338. static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
  339. {
  340. return le64_to_cpu(i->generation);
  341. }
  342. static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
  343. u64 val)
  344. {
  345. i->generation = cpu_to_le64(val);
  346. }
  347. static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
  348. {
  349. return le64_to_cpu(i->size);
  350. }
  351. static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
  352. {
  353. i->size = cpu_to_le64(val);
  354. }
  355. static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
  356. {
  357. return le64_to_cpu(i->nblocks);
  358. }
  359. static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
  360. {
  361. i->nblocks = cpu_to_le64(val);
  362. }
  363. static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
  364. {
  365. return le32_to_cpu(i->nlink);
  366. }
  367. static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
  368. {
  369. i->nlink = cpu_to_le32(val);
  370. }
  371. static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
  372. {
  373. return le32_to_cpu(i->uid);
  374. }
  375. static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
  376. {
  377. i->uid = cpu_to_le32(val);
  378. }
  379. static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
  380. {
  381. return le32_to_cpu(i->gid);
  382. }
  383. static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
  384. {
  385. i->gid = cpu_to_le32(val);
  386. }
  387. static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
  388. {
  389. return le32_to_cpu(i->mode);
  390. }
  391. static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
  392. {
  393. i->mode = cpu_to_le32(val);
  394. }
  395. static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
  396. {
  397. return le32_to_cpu(i->rdev);
  398. }
  399. static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
  400. {
  401. i->rdev = cpu_to_le32(val);
  402. }
  403. static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
  404. {
  405. return le16_to_cpu(i->flags);
  406. }
  407. static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
  408. {
  409. i->flags = cpu_to_le16(val);
  410. }
  411. static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
  412. {
  413. return le16_to_cpu(i->compat_flags);
  414. }
  415. static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
  416. u16 val)
  417. {
  418. i->compat_flags = cpu_to_le16(val);
  419. }
  420. static inline u64 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
  421. {
  422. return le64_to_cpu(ts->sec);
  423. }
  424. static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
  425. u64 val)
  426. {
  427. ts->sec = cpu_to_le64(val);
  428. }
  429. static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
  430. {
  431. return le32_to_cpu(ts->nsec);
  432. }
  433. static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
  434. u32 val)
  435. {
  436. ts->nsec = cpu_to_le32(val);
  437. }
  438. static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
  439. {
  440. return le32_to_cpu(ei->refs);
  441. }
  442. static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
  443. {
  444. ei->refs = cpu_to_le32(val);
  445. }
  446. static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
  447. {
  448. return le64_to_cpu(ei->owner);
  449. }
  450. static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
  451. {
  452. ei->owner = cpu_to_le64(val);
  453. }
  454. static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
  455. {
  456. return le64_to_cpu(n->ptrs[nr].blockptr);
  457. }
  458. static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
  459. u64 val)
  460. {
  461. n->ptrs[nr].blockptr = cpu_to_le64(val);
  462. }
  463. static inline u32 btrfs_item_offset(struct btrfs_item *item)
  464. {
  465. return le32_to_cpu(item->offset);
  466. }
  467. static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
  468. {
  469. item->offset = cpu_to_le32(val);
  470. }
  471. static inline u32 btrfs_item_end(struct btrfs_item *item)
  472. {
  473. return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
  474. }
  475. static inline u16 btrfs_item_size(struct btrfs_item *item)
  476. {
  477. return le16_to_cpu(item->size);
  478. }
  479. static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
  480. {
  481. item->size = cpu_to_le16(val);
  482. }
  483. static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
  484. {
  485. return le16_to_cpu(d->flags);
  486. }
  487. static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
  488. {
  489. d->flags = cpu_to_le16(val);
  490. }
  491. static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
  492. {
  493. return d->type;
  494. }
  495. static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
  496. {
  497. d->type = val;
  498. }
  499. static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
  500. {
  501. return le16_to_cpu(d->name_len);
  502. }
  503. static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
  504. {
  505. d->name_len = cpu_to_le16(val);
  506. }
  507. static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
  508. struct btrfs_disk_key *disk)
  509. {
  510. cpu->offset = le64_to_cpu(disk->offset);
  511. cpu->flags = le32_to_cpu(disk->flags);
  512. cpu->objectid = le64_to_cpu(disk->objectid);
  513. }
  514. static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
  515. struct btrfs_key *cpu)
  516. {
  517. disk->offset = cpu_to_le64(cpu->offset);
  518. disk->flags = cpu_to_le32(cpu->flags);
  519. disk->objectid = cpu_to_le64(cpu->objectid);
  520. }
  521. static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
  522. {
  523. return le64_to_cpu(disk->objectid);
  524. }
  525. static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
  526. u64 val)
  527. {
  528. disk->objectid = cpu_to_le64(val);
  529. }
  530. static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
  531. {
  532. return le64_to_cpu(disk->offset);
  533. }
  534. static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
  535. u64 val)
  536. {
  537. disk->offset = cpu_to_le64(val);
  538. }
  539. static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
  540. {
  541. return le32_to_cpu(disk->flags);
  542. }
  543. static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
  544. u32 val)
  545. {
  546. disk->flags = cpu_to_le32(val);
  547. }
  548. static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
  549. {
  550. return le32_to_cpu(key->flags) >> BTRFS_KEY_TYPE_SHIFT;
  551. }
  552. static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key,
  553. u32 val)
  554. {
  555. u32 flags = btrfs_disk_key_flags(key);
  556. BUG_ON(val >= BTRFS_KEY_TYPE_MAX);
  557. val = val << BTRFS_KEY_TYPE_SHIFT;
  558. flags = (flags & ~BTRFS_KEY_TYPE_MASK) | val;
  559. btrfs_set_disk_key_flags(key, flags);
  560. }
  561. static inline u32 btrfs_key_type(struct btrfs_key *key)
  562. {
  563. return key->flags >> BTRFS_KEY_TYPE_SHIFT;
  564. }
  565. static inline void btrfs_set_key_type(struct btrfs_key *key, u32 val)
  566. {
  567. BUG_ON(val >= BTRFS_KEY_TYPE_MAX);
  568. val = val << BTRFS_KEY_TYPE_SHIFT;
  569. key->flags = (key->flags & ~(BTRFS_KEY_TYPE_MASK)) | val;
  570. }
  571. static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
  572. {
  573. return le64_to_cpu(h->blocknr);
  574. }
  575. static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
  576. {
  577. h->blocknr = cpu_to_le64(blocknr);
  578. }
  579. static inline u64 btrfs_header_generation(struct btrfs_header *h)
  580. {
  581. return le64_to_cpu(h->generation);
  582. }
  583. static inline void btrfs_set_header_generation(struct btrfs_header *h,
  584. u64 val)
  585. {
  586. h->generation = cpu_to_le64(val);
  587. }
  588. static inline u64 btrfs_header_owner(struct btrfs_header *h)
  589. {
  590. return le64_to_cpu(h->owner);
  591. }
  592. static inline void btrfs_set_header_owner(struct btrfs_header *h,
  593. u64 val)
  594. {
  595. h->owner = cpu_to_le64(val);
  596. }
  597. static inline u16 btrfs_header_nritems(struct btrfs_header *h)
  598. {
  599. return le16_to_cpu(h->nritems);
  600. }
  601. static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
  602. {
  603. h->nritems = cpu_to_le16(val);
  604. }
  605. static inline u16 btrfs_header_flags(struct btrfs_header *h)
  606. {
  607. return le16_to_cpu(h->flags);
  608. }
  609. static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
  610. {
  611. h->flags = cpu_to_le16(val);
  612. }
  613. static inline int btrfs_header_level(struct btrfs_header *h)
  614. {
  615. return h->level;
  616. }
  617. static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
  618. {
  619. BUG_ON(level > BTRFS_MAX_LEVEL);
  620. h->level = level;
  621. }
  622. static inline int btrfs_is_leaf(struct btrfs_node *n)
  623. {
  624. return (btrfs_header_level(&n->header) == 0);
  625. }
  626. static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
  627. {
  628. return le64_to_cpu(item->blocknr);
  629. }
  630. static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
  631. {
  632. item->blocknr = cpu_to_le64(val);
  633. }
  634. static inline u64 btrfs_root_dirid(struct btrfs_root_item *item)
  635. {
  636. return le64_to_cpu(item->root_dirid);
  637. }
  638. static inline void btrfs_set_root_dirid(struct btrfs_root_item *item, u64 val)
  639. {
  640. item->root_dirid = cpu_to_le64(val);
  641. }
  642. static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
  643. {
  644. return le32_to_cpu(item->refs);
  645. }
  646. static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
  647. {
  648. item->refs = cpu_to_le32(val);
  649. }
  650. static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
  651. {
  652. return le64_to_cpu(s->blocknr);
  653. }
  654. static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
  655. {
  656. s->blocknr = cpu_to_le64(val);
  657. }
  658. static inline u64 btrfs_super_generation(struct btrfs_super_block *s)
  659. {
  660. return le64_to_cpu(s->generation);
  661. }
  662. static inline void btrfs_set_super_generation(struct btrfs_super_block *s,
  663. u64 val)
  664. {
  665. s->generation = cpu_to_le64(val);
  666. }
  667. static inline u64 btrfs_super_root(struct btrfs_super_block *s)
  668. {
  669. return le64_to_cpu(s->root);
  670. }
  671. static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
  672. {
  673. s->root = cpu_to_le64(val);
  674. }
  675. static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
  676. {
  677. return le64_to_cpu(s->total_blocks);
  678. }
  679. static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
  680. u64 val)
  681. {
  682. s->total_blocks = cpu_to_le64(val);
  683. }
  684. static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
  685. {
  686. return le64_to_cpu(s->blocks_used);
  687. }
  688. static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
  689. u64 val)
  690. {
  691. s->blocks_used = cpu_to_le64(val);
  692. }
  693. static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
  694. {
  695. return le32_to_cpu(s->blocksize);
  696. }
  697. static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
  698. u32 val)
  699. {
  700. s->blocksize = cpu_to_le32(val);
  701. }
  702. static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
  703. {
  704. return le64_to_cpu(s->root_dir_objectid);
  705. }
  706. static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
  707. val)
  708. {
  709. s->root_dir_objectid = cpu_to_le64(val);
  710. }
  711. static inline u64 btrfs_super_last_device_id(struct btrfs_super_block *s)
  712. {
  713. return le64_to_cpu(s->last_device_id);
  714. }
  715. static inline void btrfs_set_super_last_device_id(struct btrfs_super_block *s,
  716. u64 val)
  717. {
  718. s->last_device_id = cpu_to_le64(val);
  719. }
  720. static inline u64 btrfs_super_device_id(struct btrfs_super_block *s)
  721. {
  722. return le64_to_cpu(s->device_id);
  723. }
  724. static inline void btrfs_set_super_device_id(struct btrfs_super_block *s,
  725. u64 val)
  726. {
  727. s->device_id = cpu_to_le64(val);
  728. }
  729. static inline u64 btrfs_super_device_block_start(struct btrfs_super_block *s)
  730. {
  731. return le64_to_cpu(s->device_block_start);
  732. }
  733. static inline void btrfs_set_super_device_block_start(struct btrfs_super_block
  734. *s, u64 val)
  735. {
  736. s->device_block_start = cpu_to_le64(val);
  737. }
  738. static inline u64 btrfs_super_device_num_blocks(struct btrfs_super_block *s)
  739. {
  740. return le64_to_cpu(s->device_num_blocks);
  741. }
  742. static inline void btrfs_set_super_device_num_blocks(struct btrfs_super_block
  743. *s, u64 val)
  744. {
  745. s->device_num_blocks = cpu_to_le64(val);
  746. }
  747. static inline u64 btrfs_super_device_root(struct btrfs_super_block *s)
  748. {
  749. return le64_to_cpu(s->device_root);
  750. }
  751. static inline void btrfs_set_super_device_root(struct btrfs_super_block
  752. *s, u64 val)
  753. {
  754. s->device_root = cpu_to_le64(val);
  755. }
  756. static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
  757. {
  758. return (u8 *)l->items;
  759. }
  760. static inline int btrfs_file_extent_type(struct btrfs_file_extent_item *e)
  761. {
  762. return e->type;
  763. }
  764. static inline void btrfs_set_file_extent_type(struct btrfs_file_extent_item *e,
  765. u8 val)
  766. {
  767. e->type = val;
  768. }
  769. static inline char *btrfs_file_extent_inline_start(struct
  770. btrfs_file_extent_item *e)
  771. {
  772. return (char *)(&e->disk_blocknr);
  773. }
  774. static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
  775. {
  776. return (unsigned long)(&((struct
  777. btrfs_file_extent_item *)NULL)->disk_blocknr) + datasize;
  778. }
  779. static inline u32 btrfs_file_extent_inline_len(struct btrfs_item *e)
  780. {
  781. struct btrfs_file_extent_item *fe = NULL;
  782. return btrfs_item_size(e) - (unsigned long)(&fe->disk_blocknr);
  783. }
  784. static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
  785. *e)
  786. {
  787. return le64_to_cpu(e->disk_blocknr);
  788. }
  789. static inline void btrfs_set_file_extent_disk_blocknr(struct
  790. btrfs_file_extent_item
  791. *e, u64 val)
  792. {
  793. e->disk_blocknr = cpu_to_le64(val);
  794. }
  795. static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e)
  796. {
  797. return le64_to_cpu(e->generation);
  798. }
  799. static inline void btrfs_set_file_extent_generation(struct
  800. btrfs_file_extent_item *e,
  801. u64 val)
  802. {
  803. e->generation = cpu_to_le64(val);
  804. }
  805. static inline u64 btrfs_file_extent_disk_num_blocks(struct
  806. btrfs_file_extent_item *e)
  807. {
  808. return le64_to_cpu(e->disk_num_blocks);
  809. }
  810. static inline void btrfs_set_file_extent_disk_num_blocks(struct
  811. btrfs_file_extent_item
  812. *e, u64 val)
  813. {
  814. e->disk_num_blocks = cpu_to_le64(val);
  815. }
  816. static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
  817. {
  818. return le64_to_cpu(e->offset);
  819. }
  820. static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
  821. *e, u64 val)
  822. {
  823. e->offset = cpu_to_le64(val);
  824. }
  825. static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
  826. *e)
  827. {
  828. return le64_to_cpu(e->num_blocks);
  829. }
  830. static inline void btrfs_set_file_extent_num_blocks(struct
  831. btrfs_file_extent_item *e,
  832. u64 val)
  833. {
  834. e->num_blocks = cpu_to_le64(val);
  835. }
  836. static inline u16 btrfs_device_pathlen(struct btrfs_device_item *d)
  837. {
  838. return le16_to_cpu(d->pathlen);
  839. }
  840. static inline void btrfs_set_device_pathlen(struct btrfs_device_item *d,
  841. u16 val)
  842. {
  843. d->pathlen = cpu_to_le16(val);
  844. }
  845. static inline u64 btrfs_device_id(struct btrfs_device_item *d)
  846. {
  847. return le64_to_cpu(d->device_id);
  848. }
  849. static inline void btrfs_set_device_id(struct btrfs_device_item *d,
  850. u64 val)
  851. {
  852. d->device_id = cpu_to_le64(val);
  853. }
  854. static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
  855. {
  856. return sb->s_fs_info;
  857. }
  858. static inline void btrfs_check_bounds(void *vptr, size_t len,
  859. void *vcontainer, size_t container_len)
  860. {
  861. char *ptr = vptr;
  862. char *container = vcontainer;
  863. WARN_ON(ptr < container);
  864. WARN_ON(ptr + len > container + container_len);
  865. }
  866. static inline void btrfs_memcpy(struct btrfs_root *root,
  867. void *dst_block,
  868. void *dst, const void *src, size_t nr)
  869. {
  870. btrfs_check_bounds(dst, nr, dst_block, root->fs_info->sb->s_blocksize);
  871. memcpy(dst, src, nr);
  872. }
  873. static inline void btrfs_memmove(struct btrfs_root *root,
  874. void *dst_block,
  875. void *dst, void *src, size_t nr)
  876. {
  877. btrfs_check_bounds(dst, nr, dst_block, root->fs_info->sb->s_blocksize);
  878. memmove(dst, src, nr);
  879. }
  880. static inline void btrfs_mark_buffer_dirty(struct buffer_head *bh)
  881. {
  882. WARN_ON(!atomic_read(&bh->b_count));
  883. mark_buffer_dirty(bh);
  884. }
  885. /* helper function to cast into the data area of the leaf. */
  886. #define btrfs_item_ptr(leaf, slot, type) \
  887. ((type *)(btrfs_leaf_data(leaf) + \
  888. btrfs_item_offset((leaf)->items + (slot))))
  889. /* extent-tree.c */
  890. int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
  891. struct btrfs_root *root);
  892. struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  893. struct btrfs_root *root);
  894. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  895. struct btrfs_root *root, u64 owner,
  896. u64 num_blocks, u64 search_start,
  897. u64 search_end, struct btrfs_key *ins);
  898. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  899. struct buffer_head *buf);
  900. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  901. *root, u64 blocknr, u64 num_blocks, int pin);
  902. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
  903. btrfs_root *root);
  904. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  905. struct btrfs_root *root,
  906. u64 blocknr, u64 num_blocks);
  907. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  908. struct btrfs_root *root);
  909. int btrfs_free_block_groups(struct btrfs_fs_info *info);
  910. int btrfs_read_block_groups(struct btrfs_root *root);
  911. /* ctree.c */
  912. int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
  913. *root, struct btrfs_path *path, u32 data_size);
  914. int btrfs_truncate_item(struct btrfs_trans_handle *trans,
  915. struct btrfs_root *root,
  916. struct btrfs_path *path,
  917. u32 new_size);
  918. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
  919. *root, struct btrfs_key *key, struct btrfs_path *p, int
  920. ins_len, int cow);
  921. void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
  922. struct btrfs_path *btrfs_alloc_path(void);
  923. void btrfs_free_path(struct btrfs_path *p);
  924. void btrfs_init_path(struct btrfs_path *p);
  925. int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  926. struct btrfs_path *path);
  927. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  928. *root, struct btrfs_key *key, void *data, u32 data_size);
  929. int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
  930. *root, struct btrfs_path *path, struct btrfs_key
  931. *cpu_key, u32 data_size);
  932. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
  933. int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
  934. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  935. *root, struct buffer_head *snap);
  936. /* root-item.c */
  937. int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  938. struct btrfs_key *key);
  939. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
  940. *root, struct btrfs_key *key, struct btrfs_root_item
  941. *item);
  942. int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
  943. *root, struct btrfs_key *key, struct btrfs_root_item
  944. *item);
  945. int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
  946. btrfs_root_item *item, struct btrfs_key *key);
  947. /* dir-item.c */
  948. int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
  949. *root, const char *name, int name_len, u64 dir,
  950. struct btrfs_key *location, u8 type);
  951. struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
  952. struct btrfs_root *root,
  953. struct btrfs_path *path, u64 dir,
  954. const char *name, int name_len,
  955. int mod);
  956. struct btrfs_dir_item *
  957. btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
  958. struct btrfs_root *root,
  959. struct btrfs_path *path, u64 dir,
  960. u64 objectid, const char *name, int name_len,
  961. int mod);
  962. struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
  963. struct btrfs_path *path,
  964. const char *name, int name_len);
  965. int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
  966. struct btrfs_root *root,
  967. struct btrfs_path *path,
  968. struct btrfs_dir_item *di);
  969. /* inode-map.c */
  970. int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
  971. struct btrfs_root *fs_root,
  972. u64 dirid, u64 *objectid);
  973. int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
  974. /* inode-item.c */
  975. int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  976. *root, u64 objectid, struct btrfs_inode_item
  977. *inode_item);
  978. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  979. *root, struct btrfs_path *path,
  980. struct btrfs_key *location, int mod);
  981. /* file-item.c */
  982. int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
  983. struct btrfs_root *root,
  984. u64 objectid, u64 pos, u64 offset,
  985. u64 num_blocks);
  986. int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
  987. struct btrfs_root *root,
  988. struct btrfs_path *path, u64 objectid,
  989. u64 blocknr, int mod);
  990. int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
  991. struct btrfs_root *root,
  992. u64 objectid, u64 offset,
  993. char *data, size_t len);
  994. int btrfs_csum_verify_file_block(struct btrfs_root *root,
  995. u64 objectid, u64 offset,
  996. char *data, size_t len);
  997. struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
  998. struct btrfs_root *root,
  999. struct btrfs_path *path,
  1000. u64 objectid, u64 offset,
  1001. int cow);
  1002. /* super.c */
  1003. extern struct subsystem btrfs_subsys;
  1004. #endif