ctree.h 28 KB

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