ctree.h 30 KB

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