ctree.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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. int reada;
  163. int lowest_level;
  164. };
  165. /*
  166. * items in the extent btree are used to record the objectid of the
  167. * owner of the block and the number of references
  168. */
  169. struct btrfs_extent_item {
  170. __le32 refs;
  171. __le64 owner;
  172. } __attribute__ ((__packed__));
  173. struct btrfs_inode_timespec {
  174. __le64 sec;
  175. __le32 nsec;
  176. } __attribute__ ((__packed__));
  177. /*
  178. * there is no padding here on purpose. If you want to extent the inode,
  179. * make a new item type
  180. */
  181. struct btrfs_inode_item {
  182. __le64 generation;
  183. __le64 size;
  184. __le64 nblocks;
  185. __le64 block_group;
  186. __le32 nlink;
  187. __le32 uid;
  188. __le32 gid;
  189. __le32 mode;
  190. __le32 rdev;
  191. __le16 flags;
  192. __le16 compat_flags;
  193. struct btrfs_inode_timespec atime;
  194. struct btrfs_inode_timespec ctime;
  195. struct btrfs_inode_timespec mtime;
  196. struct btrfs_inode_timespec otime;
  197. } __attribute__ ((__packed__));
  198. struct btrfs_dir_item {
  199. struct btrfs_disk_key location;
  200. __le16 flags;
  201. __le16 name_len;
  202. u8 type;
  203. } __attribute__ ((__packed__));
  204. struct btrfs_root_item {
  205. struct btrfs_inode_item inode;
  206. __le64 root_dirid;
  207. __le64 blocknr;
  208. __le64 block_limit;
  209. __le64 blocks_used;
  210. __le32 flags;
  211. __le32 refs;
  212. struct btrfs_disk_key drop_progress;
  213. u8 drop_level;
  214. } __attribute__ ((__packed__));
  215. #define BTRFS_FILE_EXTENT_REG 0
  216. #define BTRFS_FILE_EXTENT_INLINE 1
  217. struct btrfs_file_extent_item {
  218. __le64 generation;
  219. u8 type;
  220. /*
  221. * disk space consumed by the extent, checksum blocks are included
  222. * in these numbers
  223. */
  224. __le64 disk_blocknr;
  225. __le64 disk_num_blocks;
  226. /*
  227. * the logical offset in file blocks (no csums)
  228. * this extent record is for. This allows a file extent to point
  229. * into the middle of an existing extent on disk, sharing it
  230. * between two snapshots (useful if some bytes in the middle of the
  231. * extent have changed
  232. */
  233. __le64 offset;
  234. /*
  235. * the logical number of file blocks (no csums included)
  236. */
  237. __le64 num_blocks;
  238. } __attribute__ ((__packed__));
  239. struct btrfs_csum_item {
  240. u8 csum;
  241. } __attribute__ ((__packed__));
  242. /* tag for the radix tree of block groups in ram */
  243. #define BTRFS_BLOCK_GROUP_DIRTY 0
  244. #define BTRFS_BLOCK_GROUP_AVAIL 1
  245. #define BTRFS_BLOCK_GROUP_SIZE (256 * 1024 * 1024)
  246. #define BTRFS_BLOCK_GROUP_DATA 1
  247. struct btrfs_block_group_item {
  248. __le64 used;
  249. u8 flags;
  250. } __attribute__ ((__packed__));
  251. struct btrfs_block_group_cache {
  252. struct btrfs_key key;
  253. struct btrfs_block_group_item item;
  254. struct radix_tree_root *radix;
  255. u64 first_free;
  256. u64 last_alloc;
  257. u64 pinned;
  258. u64 last_prealloc;
  259. int data;
  260. int cached;
  261. };
  262. struct btrfs_fs_info {
  263. struct btrfs_root *extent_root;
  264. struct btrfs_root *tree_root;
  265. struct radix_tree_root fs_roots_radix;
  266. struct radix_tree_root pending_del_radix;
  267. struct radix_tree_root pinned_radix;
  268. struct radix_tree_root block_group_radix;
  269. struct radix_tree_root block_group_data_radix;
  270. struct radix_tree_root extent_map_radix;
  271. u64 extent_tree_insert[BTRFS_MAX_LEVEL * 6];
  272. int extent_tree_insert_nr;
  273. u64 extent_tree_prealloc[BTRFS_MAX_LEVEL * 6];
  274. int extent_tree_prealloc_nr;
  275. u64 generation;
  276. struct btrfs_transaction *running_transaction;
  277. struct btrfs_super_block *disk_super;
  278. struct btrfs_super_block super_copy;
  279. struct buffer_head *sb_buffer;
  280. struct super_block *sb;
  281. struct inode *btree_inode;
  282. struct mutex trans_mutex;
  283. struct mutex fs_mutex;
  284. struct list_head trans_list;
  285. struct list_head dead_roots;
  286. struct delayed_work trans_work;
  287. int do_barriers;
  288. int closing;
  289. };
  290. /*
  291. * in ram representation of the tree. extent_root is used for all allocations
  292. * and for the extent tree extent_root root.
  293. */
  294. struct btrfs_root {
  295. struct buffer_head *node;
  296. struct buffer_head *commit_root;
  297. struct btrfs_root_item root_item;
  298. struct btrfs_key root_key;
  299. struct btrfs_fs_info *fs_info;
  300. struct inode *inode;
  301. u64 objectid;
  302. u64 last_trans;
  303. u32 blocksize;
  304. u32 type;
  305. u64 highest_inode;
  306. u64 last_inode_alloc;
  307. int ref_cows;
  308. struct btrfs_key defrag_progress;
  309. int defrag_running;
  310. int defrag_level;
  311. };
  312. /* the lower bits in the key flags defines the item type */
  313. #define BTRFS_KEY_TYPE_MAX 256
  314. #define BTRFS_KEY_TYPE_SHIFT 24
  315. #define BTRFS_KEY_TYPE_MASK (((u32)BTRFS_KEY_TYPE_MAX - 1) << \
  316. BTRFS_KEY_TYPE_SHIFT)
  317. /*
  318. * inode items have the data typically returned from stat and store other
  319. * info about object characteristics. There is one for every file and dir in
  320. * the FS
  321. */
  322. #define BTRFS_INODE_ITEM_KEY 1
  323. /* reserve 2-15 close to the inode for later flexibility */
  324. /*
  325. * dir items are the name -> inode pointers in a directory. There is one
  326. * for every name in a directory.
  327. */
  328. #define BTRFS_DIR_ITEM_KEY 16
  329. #define BTRFS_DIR_INDEX_KEY 17
  330. /*
  331. * extent data is for file data
  332. */
  333. #define BTRFS_EXTENT_DATA_KEY 18
  334. /*
  335. * csum items have the checksums for data in the extents
  336. */
  337. #define BTRFS_CSUM_ITEM_KEY 19
  338. /* reserve 20-31 for other file stuff */
  339. /*
  340. * root items point to tree roots. There are typically in the root
  341. * tree used by the super block to find all the other trees
  342. */
  343. #define BTRFS_ROOT_ITEM_KEY 32
  344. /*
  345. * extent items are in the extent map tree. These record which blocks
  346. * are used, and how many references there are to each block
  347. */
  348. #define BTRFS_EXTENT_ITEM_KEY 33
  349. /*
  350. * block groups give us hints into the extent allocation trees. Which
  351. * blocks are free etc etc
  352. */
  353. #define BTRFS_BLOCK_GROUP_ITEM_KEY 34
  354. /*
  355. * string items are for debugging. They just store a short string of
  356. * data in the FS
  357. */
  358. #define BTRFS_STRING_ITEM_KEY 253
  359. static inline u64 btrfs_block_group_used(struct btrfs_block_group_item *bi)
  360. {
  361. return le64_to_cpu(bi->used);
  362. }
  363. static inline void btrfs_set_block_group_used(struct
  364. btrfs_block_group_item *bi,
  365. u64 val)
  366. {
  367. bi->used = cpu_to_le64(val);
  368. }
  369. static inline u64 btrfs_inode_generation(struct btrfs_inode_item *i)
  370. {
  371. return le64_to_cpu(i->generation);
  372. }
  373. static inline void btrfs_set_inode_generation(struct btrfs_inode_item *i,
  374. u64 val)
  375. {
  376. i->generation = cpu_to_le64(val);
  377. }
  378. static inline u64 btrfs_inode_size(struct btrfs_inode_item *i)
  379. {
  380. return le64_to_cpu(i->size);
  381. }
  382. static inline void btrfs_set_inode_size(struct btrfs_inode_item *i, u64 val)
  383. {
  384. i->size = cpu_to_le64(val);
  385. }
  386. static inline u64 btrfs_inode_nblocks(struct btrfs_inode_item *i)
  387. {
  388. return le64_to_cpu(i->nblocks);
  389. }
  390. static inline void btrfs_set_inode_nblocks(struct btrfs_inode_item *i, u64 val)
  391. {
  392. i->nblocks = cpu_to_le64(val);
  393. }
  394. static inline u64 btrfs_inode_block_group(struct btrfs_inode_item *i)
  395. {
  396. return le64_to_cpu(i->block_group);
  397. }
  398. static inline void btrfs_set_inode_block_group(struct btrfs_inode_item *i,
  399. u64 val)
  400. {
  401. i->block_group = cpu_to_le64(val);
  402. }
  403. static inline u32 btrfs_inode_nlink(struct btrfs_inode_item *i)
  404. {
  405. return le32_to_cpu(i->nlink);
  406. }
  407. static inline void btrfs_set_inode_nlink(struct btrfs_inode_item *i, u32 val)
  408. {
  409. i->nlink = cpu_to_le32(val);
  410. }
  411. static inline u32 btrfs_inode_uid(struct btrfs_inode_item *i)
  412. {
  413. return le32_to_cpu(i->uid);
  414. }
  415. static inline void btrfs_set_inode_uid(struct btrfs_inode_item *i, u32 val)
  416. {
  417. i->uid = cpu_to_le32(val);
  418. }
  419. static inline u32 btrfs_inode_gid(struct btrfs_inode_item *i)
  420. {
  421. return le32_to_cpu(i->gid);
  422. }
  423. static inline void btrfs_set_inode_gid(struct btrfs_inode_item *i, u32 val)
  424. {
  425. i->gid = cpu_to_le32(val);
  426. }
  427. static inline u32 btrfs_inode_mode(struct btrfs_inode_item *i)
  428. {
  429. return le32_to_cpu(i->mode);
  430. }
  431. static inline void btrfs_set_inode_mode(struct btrfs_inode_item *i, u32 val)
  432. {
  433. i->mode = cpu_to_le32(val);
  434. }
  435. static inline u32 btrfs_inode_rdev(struct btrfs_inode_item *i)
  436. {
  437. return le32_to_cpu(i->rdev);
  438. }
  439. static inline void btrfs_set_inode_rdev(struct btrfs_inode_item *i, u32 val)
  440. {
  441. i->rdev = cpu_to_le32(val);
  442. }
  443. static inline u16 btrfs_inode_flags(struct btrfs_inode_item *i)
  444. {
  445. return le16_to_cpu(i->flags);
  446. }
  447. static inline void btrfs_set_inode_flags(struct btrfs_inode_item *i, u16 val)
  448. {
  449. i->flags = cpu_to_le16(val);
  450. }
  451. static inline u16 btrfs_inode_compat_flags(struct btrfs_inode_item *i)
  452. {
  453. return le16_to_cpu(i->compat_flags);
  454. }
  455. static inline void btrfs_set_inode_compat_flags(struct btrfs_inode_item *i,
  456. u16 val)
  457. {
  458. i->compat_flags = cpu_to_le16(val);
  459. }
  460. static inline u64 btrfs_timespec_sec(struct btrfs_inode_timespec *ts)
  461. {
  462. return le64_to_cpu(ts->sec);
  463. }
  464. static inline void btrfs_set_timespec_sec(struct btrfs_inode_timespec *ts,
  465. u64 val)
  466. {
  467. ts->sec = cpu_to_le64(val);
  468. }
  469. static inline u32 btrfs_timespec_nsec(struct btrfs_inode_timespec *ts)
  470. {
  471. return le32_to_cpu(ts->nsec);
  472. }
  473. static inline void btrfs_set_timespec_nsec(struct btrfs_inode_timespec *ts,
  474. u32 val)
  475. {
  476. ts->nsec = cpu_to_le32(val);
  477. }
  478. static inline u32 btrfs_extent_refs(struct btrfs_extent_item *ei)
  479. {
  480. return le32_to_cpu(ei->refs);
  481. }
  482. static inline void btrfs_set_extent_refs(struct btrfs_extent_item *ei, u32 val)
  483. {
  484. ei->refs = cpu_to_le32(val);
  485. }
  486. static inline u64 btrfs_extent_owner(struct btrfs_extent_item *ei)
  487. {
  488. return le64_to_cpu(ei->owner);
  489. }
  490. static inline void btrfs_set_extent_owner(struct btrfs_extent_item *ei, u64 val)
  491. {
  492. ei->owner = cpu_to_le64(val);
  493. }
  494. static inline u64 btrfs_node_blockptr(struct btrfs_node *n, int nr)
  495. {
  496. return le64_to_cpu(n->ptrs[nr].blockptr);
  497. }
  498. static inline void btrfs_set_node_blockptr(struct btrfs_node *n, int nr,
  499. u64 val)
  500. {
  501. n->ptrs[nr].blockptr = cpu_to_le64(val);
  502. }
  503. static inline u32 btrfs_item_offset(struct btrfs_item *item)
  504. {
  505. return le32_to_cpu(item->offset);
  506. }
  507. static inline void btrfs_set_item_offset(struct btrfs_item *item, u32 val)
  508. {
  509. item->offset = cpu_to_le32(val);
  510. }
  511. static inline u32 btrfs_item_end(struct btrfs_item *item)
  512. {
  513. return le32_to_cpu(item->offset) + le16_to_cpu(item->size);
  514. }
  515. static inline u16 btrfs_item_size(struct btrfs_item *item)
  516. {
  517. return le16_to_cpu(item->size);
  518. }
  519. static inline void btrfs_set_item_size(struct btrfs_item *item, u16 val)
  520. {
  521. item->size = cpu_to_le16(val);
  522. }
  523. static inline u16 btrfs_dir_flags(struct btrfs_dir_item *d)
  524. {
  525. return le16_to_cpu(d->flags);
  526. }
  527. static inline void btrfs_set_dir_flags(struct btrfs_dir_item *d, u16 val)
  528. {
  529. d->flags = cpu_to_le16(val);
  530. }
  531. static inline u8 btrfs_dir_type(struct btrfs_dir_item *d)
  532. {
  533. return d->type;
  534. }
  535. static inline void btrfs_set_dir_type(struct btrfs_dir_item *d, u8 val)
  536. {
  537. d->type = val;
  538. }
  539. static inline u16 btrfs_dir_name_len(struct btrfs_dir_item *d)
  540. {
  541. return le16_to_cpu(d->name_len);
  542. }
  543. static inline void btrfs_set_dir_name_len(struct btrfs_dir_item *d, u16 val)
  544. {
  545. d->name_len = cpu_to_le16(val);
  546. }
  547. static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
  548. struct btrfs_disk_key *disk)
  549. {
  550. cpu->offset = le64_to_cpu(disk->offset);
  551. cpu->flags = le32_to_cpu(disk->flags);
  552. cpu->objectid = le64_to_cpu(disk->objectid);
  553. }
  554. static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
  555. struct btrfs_key *cpu)
  556. {
  557. disk->offset = cpu_to_le64(cpu->offset);
  558. disk->flags = cpu_to_le32(cpu->flags);
  559. disk->objectid = cpu_to_le64(cpu->objectid);
  560. }
  561. static inline u64 btrfs_disk_key_objectid(struct btrfs_disk_key *disk)
  562. {
  563. return le64_to_cpu(disk->objectid);
  564. }
  565. static inline void btrfs_set_disk_key_objectid(struct btrfs_disk_key *disk,
  566. u64 val)
  567. {
  568. disk->objectid = cpu_to_le64(val);
  569. }
  570. static inline u64 btrfs_disk_key_offset(struct btrfs_disk_key *disk)
  571. {
  572. return le64_to_cpu(disk->offset);
  573. }
  574. static inline void btrfs_set_disk_key_offset(struct btrfs_disk_key *disk,
  575. u64 val)
  576. {
  577. disk->offset = cpu_to_le64(val);
  578. }
  579. static inline u32 btrfs_disk_key_flags(struct btrfs_disk_key *disk)
  580. {
  581. return le32_to_cpu(disk->flags);
  582. }
  583. static inline void btrfs_set_disk_key_flags(struct btrfs_disk_key *disk,
  584. u32 val)
  585. {
  586. disk->flags = cpu_to_le32(val);
  587. }
  588. static inline u32 btrfs_disk_key_type(struct btrfs_disk_key *key)
  589. {
  590. return le32_to_cpu(key->flags) >> BTRFS_KEY_TYPE_SHIFT;
  591. }
  592. static inline void btrfs_set_disk_key_type(struct btrfs_disk_key *key,
  593. u32 val)
  594. {
  595. u32 flags = btrfs_disk_key_flags(key);
  596. BUG_ON(val >= BTRFS_KEY_TYPE_MAX);
  597. val = val << BTRFS_KEY_TYPE_SHIFT;
  598. flags = (flags & ~BTRFS_KEY_TYPE_MASK) | val;
  599. btrfs_set_disk_key_flags(key, flags);
  600. }
  601. static inline u32 btrfs_key_type(struct btrfs_key *key)
  602. {
  603. return key->flags >> BTRFS_KEY_TYPE_SHIFT;
  604. }
  605. static inline void btrfs_set_key_type(struct btrfs_key *key, u32 val)
  606. {
  607. BUG_ON(val >= BTRFS_KEY_TYPE_MAX);
  608. val = val << BTRFS_KEY_TYPE_SHIFT;
  609. key->flags = (key->flags & ~(BTRFS_KEY_TYPE_MASK)) | val;
  610. }
  611. static inline u64 btrfs_header_blocknr(struct btrfs_header *h)
  612. {
  613. return le64_to_cpu(h->blocknr);
  614. }
  615. static inline void btrfs_set_header_blocknr(struct btrfs_header *h, u64 blocknr)
  616. {
  617. h->blocknr = cpu_to_le64(blocknr);
  618. }
  619. static inline u64 btrfs_header_generation(struct btrfs_header *h)
  620. {
  621. return le64_to_cpu(h->generation);
  622. }
  623. static inline void btrfs_set_header_generation(struct btrfs_header *h,
  624. u64 val)
  625. {
  626. h->generation = cpu_to_le64(val);
  627. }
  628. static inline u64 btrfs_header_owner(struct btrfs_header *h)
  629. {
  630. return le64_to_cpu(h->owner);
  631. }
  632. static inline void btrfs_set_header_owner(struct btrfs_header *h,
  633. u64 val)
  634. {
  635. h->owner = cpu_to_le64(val);
  636. }
  637. static inline u16 btrfs_header_nritems(struct btrfs_header *h)
  638. {
  639. return le16_to_cpu(h->nritems);
  640. }
  641. static inline void btrfs_set_header_nritems(struct btrfs_header *h, u16 val)
  642. {
  643. h->nritems = cpu_to_le16(val);
  644. }
  645. static inline u16 btrfs_header_flags(struct btrfs_header *h)
  646. {
  647. return le16_to_cpu(h->flags);
  648. }
  649. static inline void btrfs_set_header_flags(struct btrfs_header *h, u16 val)
  650. {
  651. h->flags = cpu_to_le16(val);
  652. }
  653. static inline int btrfs_header_level(struct btrfs_header *h)
  654. {
  655. return h->level;
  656. }
  657. static inline void btrfs_set_header_level(struct btrfs_header *h, int level)
  658. {
  659. BUG_ON(level > BTRFS_MAX_LEVEL);
  660. h->level = level;
  661. }
  662. static inline int btrfs_is_leaf(struct btrfs_node *n)
  663. {
  664. return (btrfs_header_level(&n->header) == 0);
  665. }
  666. static inline u64 btrfs_root_blocknr(struct btrfs_root_item *item)
  667. {
  668. return le64_to_cpu(item->blocknr);
  669. }
  670. static inline void btrfs_set_root_blocknr(struct btrfs_root_item *item, u64 val)
  671. {
  672. item->blocknr = cpu_to_le64(val);
  673. }
  674. static inline u64 btrfs_root_dirid(struct btrfs_root_item *item)
  675. {
  676. return le64_to_cpu(item->root_dirid);
  677. }
  678. static inline void btrfs_set_root_dirid(struct btrfs_root_item *item, u64 val)
  679. {
  680. item->root_dirid = cpu_to_le64(val);
  681. }
  682. static inline u32 btrfs_root_refs(struct btrfs_root_item *item)
  683. {
  684. return le32_to_cpu(item->refs);
  685. }
  686. static inline void btrfs_set_root_refs(struct btrfs_root_item *item, u32 val)
  687. {
  688. item->refs = cpu_to_le32(val);
  689. }
  690. static inline u32 btrfs_root_flags(struct btrfs_root_item *item)
  691. {
  692. return le32_to_cpu(item->flags);
  693. }
  694. static inline void btrfs_set_root_flags(struct btrfs_root_item *item, u32 val)
  695. {
  696. item->flags = cpu_to_le32(val);
  697. }
  698. static inline u64 btrfs_super_blocknr(struct btrfs_super_block *s)
  699. {
  700. return le64_to_cpu(s->blocknr);
  701. }
  702. static inline void btrfs_set_super_blocknr(struct btrfs_super_block *s, u64 val)
  703. {
  704. s->blocknr = cpu_to_le64(val);
  705. }
  706. static inline u64 btrfs_super_generation(struct btrfs_super_block *s)
  707. {
  708. return le64_to_cpu(s->generation);
  709. }
  710. static inline void btrfs_set_super_generation(struct btrfs_super_block *s,
  711. u64 val)
  712. {
  713. s->generation = cpu_to_le64(val);
  714. }
  715. static inline u64 btrfs_super_root(struct btrfs_super_block *s)
  716. {
  717. return le64_to_cpu(s->root);
  718. }
  719. static inline void btrfs_set_super_root(struct btrfs_super_block *s, u64 val)
  720. {
  721. s->root = cpu_to_le64(val);
  722. }
  723. static inline u64 btrfs_super_total_blocks(struct btrfs_super_block *s)
  724. {
  725. return le64_to_cpu(s->total_blocks);
  726. }
  727. static inline void btrfs_set_super_total_blocks(struct btrfs_super_block *s,
  728. u64 val)
  729. {
  730. s->total_blocks = cpu_to_le64(val);
  731. }
  732. static inline u64 btrfs_super_blocks_used(struct btrfs_super_block *s)
  733. {
  734. return le64_to_cpu(s->blocks_used);
  735. }
  736. static inline void btrfs_set_super_blocks_used(struct btrfs_super_block *s,
  737. u64 val)
  738. {
  739. s->blocks_used = cpu_to_le64(val);
  740. }
  741. static inline u32 btrfs_super_blocksize(struct btrfs_super_block *s)
  742. {
  743. return le32_to_cpu(s->blocksize);
  744. }
  745. static inline void btrfs_set_super_blocksize(struct btrfs_super_block *s,
  746. u32 val)
  747. {
  748. s->blocksize = cpu_to_le32(val);
  749. }
  750. static inline u64 btrfs_super_root_dir(struct btrfs_super_block *s)
  751. {
  752. return le64_to_cpu(s->root_dir_objectid);
  753. }
  754. static inline void btrfs_set_super_root_dir(struct btrfs_super_block *s, u64
  755. val)
  756. {
  757. s->root_dir_objectid = cpu_to_le64(val);
  758. }
  759. static inline u8 *btrfs_leaf_data(struct btrfs_leaf *l)
  760. {
  761. return (u8 *)l->items;
  762. }
  763. static inline int btrfs_file_extent_type(struct btrfs_file_extent_item *e)
  764. {
  765. return e->type;
  766. }
  767. static inline void btrfs_set_file_extent_type(struct btrfs_file_extent_item *e,
  768. u8 val)
  769. {
  770. e->type = val;
  771. }
  772. static inline char *btrfs_file_extent_inline_start(struct
  773. btrfs_file_extent_item *e)
  774. {
  775. return (char *)(&e->disk_blocknr);
  776. }
  777. static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
  778. {
  779. return (unsigned long)(&((struct
  780. btrfs_file_extent_item *)NULL)->disk_blocknr) + datasize;
  781. }
  782. static inline u32 btrfs_file_extent_inline_len(struct btrfs_item *e)
  783. {
  784. struct btrfs_file_extent_item *fe = NULL;
  785. return btrfs_item_size(e) - (unsigned long)(&fe->disk_blocknr);
  786. }
  787. static inline u64 btrfs_file_extent_disk_blocknr(struct btrfs_file_extent_item
  788. *e)
  789. {
  790. return le64_to_cpu(e->disk_blocknr);
  791. }
  792. static inline void btrfs_set_file_extent_disk_blocknr(struct
  793. btrfs_file_extent_item
  794. *e, u64 val)
  795. {
  796. e->disk_blocknr = cpu_to_le64(val);
  797. }
  798. static inline u64 btrfs_file_extent_generation(struct btrfs_file_extent_item *e)
  799. {
  800. return le64_to_cpu(e->generation);
  801. }
  802. static inline void btrfs_set_file_extent_generation(struct
  803. btrfs_file_extent_item *e,
  804. u64 val)
  805. {
  806. e->generation = cpu_to_le64(val);
  807. }
  808. static inline u64 btrfs_file_extent_disk_num_blocks(struct
  809. btrfs_file_extent_item *e)
  810. {
  811. return le64_to_cpu(e->disk_num_blocks);
  812. }
  813. static inline void btrfs_set_file_extent_disk_num_blocks(struct
  814. btrfs_file_extent_item
  815. *e, u64 val)
  816. {
  817. e->disk_num_blocks = cpu_to_le64(val);
  818. }
  819. static inline u64 btrfs_file_extent_offset(struct btrfs_file_extent_item *e)
  820. {
  821. return le64_to_cpu(e->offset);
  822. }
  823. static inline void btrfs_set_file_extent_offset(struct btrfs_file_extent_item
  824. *e, u64 val)
  825. {
  826. e->offset = cpu_to_le64(val);
  827. }
  828. static inline u64 btrfs_file_extent_num_blocks(struct btrfs_file_extent_item
  829. *e)
  830. {
  831. return le64_to_cpu(e->num_blocks);
  832. }
  833. static inline void btrfs_set_file_extent_num_blocks(struct
  834. btrfs_file_extent_item *e,
  835. u64 val)
  836. {
  837. e->num_blocks = cpu_to_le64(val);
  838. }
  839. static inline struct btrfs_root *btrfs_sb(struct super_block *sb)
  840. {
  841. return sb->s_fs_info;
  842. }
  843. static inline void btrfs_check_bounds(void *vptr, size_t len,
  844. void *vcontainer, size_t container_len)
  845. {
  846. char *ptr = vptr;
  847. char *container = vcontainer;
  848. WARN_ON(ptr < container);
  849. WARN_ON(ptr + len > container + container_len);
  850. }
  851. static inline void btrfs_memcpy(struct btrfs_root *root,
  852. void *dst_block,
  853. void *dst, const void *src, size_t nr)
  854. {
  855. btrfs_check_bounds(dst, nr, dst_block, root->fs_info->sb->s_blocksize);
  856. memcpy(dst, src, nr);
  857. }
  858. static inline void btrfs_memmove(struct btrfs_root *root,
  859. void *dst_block,
  860. void *dst, void *src, size_t nr)
  861. {
  862. btrfs_check_bounds(dst, nr, dst_block, root->fs_info->sb->s_blocksize);
  863. memmove(dst, src, nr);
  864. }
  865. /* helper function to cast into the data area of the leaf. */
  866. #define btrfs_item_ptr(leaf, slot, type) \
  867. ((type *)(btrfs_leaf_data(leaf) + \
  868. btrfs_item_offset((leaf)->items + (slot))))
  869. /* extent-tree.c */
  870. int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy);
  871. struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
  872. btrfs_fs_info *info,
  873. u64 blocknr);
  874. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  875. struct btrfs_block_group_cache
  876. *hint, u64 search_start,
  877. int data, int owner);
  878. int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
  879. struct btrfs_root *root);
  880. struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  881. struct btrfs_root *root, u64 hint,
  882. u64 empty_size);
  883. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  884. struct btrfs_root *root, u64 owner,
  885. u64 num_blocks, u64 empty_size, u64 search_start,
  886. u64 search_end, struct btrfs_key *ins, int data);
  887. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  888. struct buffer_head *buf);
  889. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  890. *root, u64 blocknr, u64 num_blocks, int pin);
  891. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  892. struct btrfs_root *root,
  893. struct radix_tree_root *unpin_radix);
  894. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  895. struct btrfs_root *root,
  896. u64 blocknr, u64 num_blocks);
  897. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  898. struct btrfs_root *root);
  899. int btrfs_free_block_groups(struct btrfs_fs_info *info);
  900. int btrfs_read_block_groups(struct btrfs_root *root);
  901. /* ctree.c */
  902. int btrfs_cow_block(struct btrfs_trans_handle *trans, struct btrfs_root
  903. *root, struct buffer_head *buf, struct buffer_head
  904. *parent, int parent_slot, struct buffer_head
  905. **cow_ret);
  906. int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
  907. *root, struct btrfs_path *path, u32 data_size);
  908. int btrfs_truncate_item(struct btrfs_trans_handle *trans,
  909. struct btrfs_root *root,
  910. struct btrfs_path *path,
  911. u32 new_size);
  912. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
  913. *root, struct btrfs_key *key, struct btrfs_path *p, int
  914. ins_len, int cow);
  915. int btrfs_realloc_node(struct btrfs_trans_handle *trans,
  916. struct btrfs_root *root, struct buffer_head *parent,
  917. int cache_only);
  918. void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
  919. struct btrfs_path *btrfs_alloc_path(void);
  920. void btrfs_free_path(struct btrfs_path *p);
  921. void btrfs_init_path(struct btrfs_path *p);
  922. int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  923. struct btrfs_path *path);
  924. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  925. *root, struct btrfs_key *key, void *data, u32 data_size);
  926. int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
  927. *root, struct btrfs_path *path, struct btrfs_key
  928. *cpu_key, u32 data_size);
  929. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
  930. int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
  931. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  932. *root);
  933. /* root-item.c */
  934. int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  935. struct btrfs_key *key);
  936. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
  937. *root, struct btrfs_key *key, struct btrfs_root_item
  938. *item);
  939. int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
  940. *root, struct btrfs_key *key, struct btrfs_root_item
  941. *item);
  942. int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
  943. btrfs_root_item *item, struct btrfs_key *key);
  944. int btrfs_find_dead_roots(struct btrfs_root *root);
  945. /* dir-item.c */
  946. int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
  947. *root, const char *name, int name_len, u64 dir,
  948. struct btrfs_key *location, u8 type);
  949. struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
  950. struct btrfs_root *root,
  951. struct btrfs_path *path, u64 dir,
  952. const char *name, int name_len,
  953. int mod);
  954. struct btrfs_dir_item *
  955. btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
  956. struct btrfs_root *root,
  957. struct btrfs_path *path, u64 dir,
  958. u64 objectid, const char *name, int name_len,
  959. int mod);
  960. struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
  961. struct btrfs_path *path,
  962. const char *name, int name_len);
  963. int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
  964. struct btrfs_root *root,
  965. struct btrfs_path *path,
  966. struct btrfs_dir_item *di);
  967. /* inode-map.c */
  968. int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
  969. struct btrfs_root *fs_root,
  970. u64 dirid, u64 *objectid);
  971. int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
  972. /* inode-item.c */
  973. int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  974. *root, u64 objectid, struct btrfs_inode_item
  975. *inode_item);
  976. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  977. *root, struct btrfs_path *path,
  978. struct btrfs_key *location, int mod);
  979. /* file-item.c */
  980. int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
  981. struct btrfs_root *root,
  982. u64 objectid, u64 pos, u64 offset,
  983. u64 disk_num_blocks,
  984. u64 num_blocks);
  985. int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
  986. struct btrfs_root *root,
  987. struct btrfs_path *path, u64 objectid,
  988. u64 blocknr, int mod);
  989. int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
  990. struct btrfs_root *root,
  991. u64 objectid, u64 offset,
  992. char *data, size_t len);
  993. struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
  994. struct btrfs_root *root,
  995. struct btrfs_path *path,
  996. u64 objectid, u64 offset,
  997. int cow);
  998. int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
  999. struct btrfs_root *root, struct btrfs_path *path,
  1000. u64 isize);
  1001. /* inode.c */
  1002. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page);
  1003. int btrfs_readpage(struct file *file, struct page *page);
  1004. void btrfs_delete_inode(struct inode *inode);
  1005. void btrfs_read_locked_inode(struct inode *inode);
  1006. int btrfs_write_inode(struct inode *inode, int wait);
  1007. void btrfs_dirty_inode(struct inode *inode);
  1008. struct inode *btrfs_alloc_inode(struct super_block *sb);
  1009. void btrfs_destroy_inode(struct inode *inode);
  1010. int btrfs_init_cachep(void);
  1011. void btrfs_destroy_cachep(void);
  1012. int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  1013. unsigned long arg);
  1014. long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
  1015. unsigned long arg);
  1016. struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
  1017. struct btrfs_root *root);
  1018. int btrfs_commit_write(struct file *file, struct page *page,
  1019. unsigned from, unsigned to);
  1020. int btrfs_get_block(struct inode *inode, sector_t iblock,
  1021. struct buffer_head *result, int create);
  1022. /* file.c */
  1023. extern struct file_operations btrfs_file_operations;
  1024. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  1025. struct btrfs_root *root, struct inode *inode,
  1026. u64 start, u64 end, u64 *hint_block);
  1027. /* tree-defrag.c */
  1028. int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
  1029. struct btrfs_root *root, int cache_only);
  1030. #endif