ctree.h 30 KB

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