ctree.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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. static inline void btrfs_mark_buffer_dirty(struct buffer_head *bh)
  861. {
  862. WARN_ON(!atomic_read(&bh->b_count));
  863. mark_buffer_dirty(bh);
  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. struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
  871. btrfs_fs_info *info,
  872. u64 blocknr);
  873. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  874. struct btrfs_block_group_cache
  875. *hint, u64 search_start,
  876. int data, int owner);
  877. int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
  878. struct btrfs_root *root);
  879. struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  880. struct btrfs_root *root, u64 hint);
  881. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  882. struct btrfs_root *root, u64 owner,
  883. u64 num_blocks, u64 search_start,
  884. u64 search_end, struct btrfs_key *ins, int data);
  885. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  886. struct buffer_head *buf);
  887. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  888. *root, u64 blocknr, u64 num_blocks, int pin);
  889. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
  890. btrfs_root *root);
  891. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  892. struct btrfs_root *root,
  893. u64 blocknr, u64 num_blocks);
  894. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  895. struct btrfs_root *root);
  896. int btrfs_free_block_groups(struct btrfs_fs_info *info);
  897. int btrfs_read_block_groups(struct btrfs_root *root);
  898. /* ctree.c */
  899. int btrfs_extend_item(struct btrfs_trans_handle *trans, struct btrfs_root
  900. *root, struct btrfs_path *path, u32 data_size);
  901. int btrfs_truncate_item(struct btrfs_trans_handle *trans,
  902. struct btrfs_root *root,
  903. struct btrfs_path *path,
  904. u32 new_size);
  905. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
  906. *root, struct btrfs_key *key, struct btrfs_path *p, int
  907. ins_len, int cow);
  908. void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p);
  909. struct btrfs_path *btrfs_alloc_path(void);
  910. void btrfs_free_path(struct btrfs_path *p);
  911. void btrfs_init_path(struct btrfs_path *p);
  912. int btrfs_del_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  913. struct btrfs_path *path);
  914. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  915. *root, struct btrfs_key *key, void *data, u32 data_size);
  916. int btrfs_insert_empty_item(struct btrfs_trans_handle *trans, struct btrfs_root
  917. *root, struct btrfs_path *path, struct btrfs_key
  918. *cpu_key, u32 data_size);
  919. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
  920. int btrfs_leaf_free_space(struct btrfs_root *root, struct btrfs_leaf *leaf);
  921. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  922. *root, struct buffer_head *snap);
  923. /* root-item.c */
  924. int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  925. struct btrfs_key *key);
  926. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
  927. *root, struct btrfs_key *key, struct btrfs_root_item
  928. *item);
  929. int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
  930. *root, struct btrfs_key *key, struct btrfs_root_item
  931. *item);
  932. int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, struct
  933. btrfs_root_item *item, struct btrfs_key *key);
  934. int btrfs_find_dead_roots(struct btrfs_root *root);
  935. /* dir-item.c */
  936. int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
  937. *root, const char *name, int name_len, u64 dir,
  938. struct btrfs_key *location, u8 type);
  939. struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
  940. struct btrfs_root *root,
  941. struct btrfs_path *path, u64 dir,
  942. const char *name, int name_len,
  943. int mod);
  944. struct btrfs_dir_item *
  945. btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
  946. struct btrfs_root *root,
  947. struct btrfs_path *path, u64 dir,
  948. u64 objectid, const char *name, int name_len,
  949. int mod);
  950. struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
  951. struct btrfs_path *path,
  952. const char *name, int name_len);
  953. int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
  954. struct btrfs_root *root,
  955. struct btrfs_path *path,
  956. struct btrfs_dir_item *di);
  957. /* inode-map.c */
  958. int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
  959. struct btrfs_root *fs_root,
  960. u64 dirid, u64 *objectid);
  961. int btrfs_find_highest_inode(struct btrfs_root *fs_root, u64 *objectid);
  962. /* inode-item.c */
  963. int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  964. *root, u64 objectid, struct btrfs_inode_item
  965. *inode_item);
  966. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  967. *root, struct btrfs_path *path,
  968. struct btrfs_key *location, int mod);
  969. /* file-item.c */
  970. int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
  971. struct btrfs_root *root,
  972. u64 objectid, u64 pos, u64 offset,
  973. u64 disk_num_blocks,
  974. u64 num_blocks);
  975. int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
  976. struct btrfs_root *root,
  977. struct btrfs_path *path, u64 objectid,
  978. u64 blocknr, int mod);
  979. int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
  980. struct btrfs_root *root,
  981. u64 objectid, u64 offset,
  982. char *data, size_t len);
  983. struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
  984. struct btrfs_root *root,
  985. struct btrfs_path *path,
  986. u64 objectid, u64 offset,
  987. int cow);
  988. int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
  989. struct btrfs_root *root, struct btrfs_path *path,
  990. u64 isize);
  991. /* inode.c */
  992. int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page);
  993. int btrfs_readpage(struct file *file, struct page *page);
  994. void btrfs_delete_inode(struct inode *inode);
  995. void btrfs_read_locked_inode(struct inode *inode);
  996. int btrfs_write_inode(struct inode *inode, int wait);
  997. void btrfs_dirty_inode(struct inode *inode);
  998. struct inode *btrfs_alloc_inode(struct super_block *sb);
  999. void btrfs_destroy_inode(struct inode *inode);
  1000. int btrfs_init_cachep(void);
  1001. void btrfs_destroy_cachep(void);
  1002. int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
  1003. unsigned long arg);
  1004. long btrfs_compat_ioctl(struct file *file, unsigned int cmd,
  1005. unsigned long arg);
  1006. struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
  1007. struct btrfs_root *root);
  1008. int btrfs_commit_write(struct file *file, struct page *page,
  1009. unsigned from, unsigned to);
  1010. int btrfs_get_block(struct inode *inode, sector_t iblock,
  1011. struct buffer_head *result, int create);
  1012. /* file.c */
  1013. extern struct file_operations btrfs_file_operations;
  1014. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  1015. struct btrfs_root *root, struct inode *inode,
  1016. u64 start, u64 end, u64 *hint_block);
  1017. #endif