ctree.h 31 KB

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