ctree.h 26 KB

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