ctree.h 28 KB

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