ctree.h 29 KB

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