ctree.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * From linux/fs/btrfs/ctree.h
  3. * Copyright (C) 2007,2008 Oracle. All rights reserved.
  4. *
  5. * Modified in 2017 by Marek Behun, CZ.NIC, marek.behun@nic.cz
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #ifndef __BTRFS_CTREE_H__
  10. #define __BTRFS_CTREE_H__
  11. #include <common.h>
  12. #include <compiler.h>
  13. #include "btrfs_tree.h"
  14. #define BTRFS_MAGIC 0x4D5F53665248425FULL /* ascii _BHRfS_M, no null */
  15. #define BTRFS_MAX_MIRRORS 3
  16. #define BTRFS_MAX_LEVEL 8
  17. #define BTRFS_COMPAT_EXTENT_TREE_V0
  18. /*
  19. * the max metadata block size. This limit is somewhat artificial,
  20. * but the memmove costs go through the roof for larger blocks.
  21. */
  22. #define BTRFS_MAX_METADATA_BLOCKSIZE 65536
  23. /*
  24. * we can actually store much bigger names, but lets not confuse the rest
  25. * of linux
  26. */
  27. #define BTRFS_NAME_LEN 255
  28. /*
  29. * Theoretical limit is larger, but we keep this down to a sane
  30. * value. That should limit greatly the possibility of collisions on
  31. * inode ref items.
  32. */
  33. #define BTRFS_LINK_MAX 65535U
  34. static const int btrfs_csum_sizes[] = { 4 };
  35. /* four bytes for CRC32 */
  36. #define BTRFS_EMPTY_DIR_SIZE 0
  37. /* ioprio of readahead is set to idle */
  38. #define BTRFS_IOPRIO_READA (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0))
  39. #define BTRFS_DIRTY_METADATA_THRESH SZ_32M
  40. #define BTRFS_MAX_EXTENT_SIZE SZ_128M
  41. /*
  42. * File system states
  43. */
  44. #define BTRFS_FS_STATE_ERROR 0
  45. #define BTRFS_FS_STATE_REMOUNTING 1
  46. #define BTRFS_FS_STATE_TRANS_ABORTED 2
  47. #define BTRFS_FS_STATE_DEV_REPLACING 3
  48. #define BTRFS_FS_STATE_DUMMY_FS_INFO 4
  49. #define BTRFS_BACKREF_REV_MAX 256
  50. #define BTRFS_BACKREF_REV_SHIFT 56
  51. #define BTRFS_BACKREF_REV_MASK (((u64)BTRFS_BACKREF_REV_MAX - 1) << \
  52. BTRFS_BACKREF_REV_SHIFT)
  53. #define BTRFS_OLD_BACKREF_REV 0
  54. #define BTRFS_MIXED_BACKREF_REV 1
  55. /*
  56. * every tree block (leaf or node) starts with this header.
  57. */
  58. struct btrfs_header {
  59. /* these first four must match the super block */
  60. __u8 csum[BTRFS_CSUM_SIZE];
  61. __u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
  62. __u64 bytenr; /* which block this node is supposed to live in */
  63. __u64 flags;
  64. /* allowed to be different from the super from here on down */
  65. __u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
  66. __u64 generation;
  67. __u64 owner;
  68. __u32 nritems;
  69. __u8 level;
  70. } __attribute__ ((__packed__));
  71. /*
  72. * this is a very generous portion of the super block, giving us
  73. * room to translate 14 chunks with 3 stripes each.
  74. */
  75. #define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
  76. /*
  77. * just in case we somehow lose the roots and are not able to mount,
  78. * we store an array of the roots from previous transactions
  79. * in the super.
  80. */
  81. #define BTRFS_NUM_BACKUP_ROOTS 4
  82. struct btrfs_root_backup {
  83. __u64 tree_root;
  84. __u64 tree_root_gen;
  85. __u64 chunk_root;
  86. __u64 chunk_root_gen;
  87. __u64 extent_root;
  88. __u64 extent_root_gen;
  89. __u64 fs_root;
  90. __u64 fs_root_gen;
  91. __u64 dev_root;
  92. __u64 dev_root_gen;
  93. __u64 csum_root;
  94. __u64 csum_root_gen;
  95. __u64 total_bytes;
  96. __u64 bytes_used;
  97. __u64 num_devices;
  98. /* future */
  99. __u64 unused_64[4];
  100. __u8 tree_root_level;
  101. __u8 chunk_root_level;
  102. __u8 extent_root_level;
  103. __u8 fs_root_level;
  104. __u8 dev_root_level;
  105. __u8 csum_root_level;
  106. /* future and to align */
  107. __u8 unused_8[10];
  108. } __attribute__ ((__packed__));
  109. /*
  110. * the super block basically lists the main trees of the FS
  111. * it currently lacks any block count etc etc
  112. */
  113. struct btrfs_super_block {
  114. __u8 csum[BTRFS_CSUM_SIZE];
  115. /* the first 4 fields must match struct btrfs_header */
  116. __u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
  117. __u64 bytenr; /* this block number */
  118. __u64 flags;
  119. /* allowed to be different from the btrfs_header from here own down */
  120. __u64 magic;
  121. __u64 generation;
  122. __u64 root;
  123. __u64 chunk_root;
  124. __u64 log_root;
  125. /* this will help find the new super based on the log root */
  126. __u64 log_root_transid;
  127. __u64 total_bytes;
  128. __u64 bytes_used;
  129. __u64 root_dir_objectid;
  130. __u64 num_devices;
  131. __u32 sectorsize;
  132. __u32 nodesize;
  133. __u32 __unused_leafsize;
  134. __u32 stripesize;
  135. __u32 sys_chunk_array_size;
  136. __u64 chunk_root_generation;
  137. __u64 compat_flags;
  138. __u64 compat_ro_flags;
  139. __u64 incompat_flags;
  140. __u16 csum_type;
  141. __u8 root_level;
  142. __u8 chunk_root_level;
  143. __u8 log_root_level;
  144. struct btrfs_dev_item dev_item;
  145. char label[BTRFS_LABEL_SIZE];
  146. __u64 cache_generation;
  147. __u64 uuid_tree_generation;
  148. /* future expansion */
  149. __u64 reserved[30];
  150. __u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
  151. struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
  152. } __attribute__ ((__packed__));
  153. /*
  154. * Compat flags that we support. If any incompat flags are set other than the
  155. * ones specified below then we will fail to mount
  156. */
  157. #define BTRFS_FEATURE_COMPAT_SUPP 0ULL
  158. #define BTRFS_FEATURE_COMPAT_SAFE_SET 0ULL
  159. #define BTRFS_FEATURE_COMPAT_SAFE_CLEAR 0ULL
  160. #define BTRFS_FEATURE_COMPAT_RO_SUPP \
  161. (BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE | \
  162. BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID)
  163. #define BTRFS_FEATURE_COMPAT_RO_SAFE_SET 0ULL
  164. #define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR 0ULL
  165. #define BTRFS_FEATURE_INCOMPAT_SUPP \
  166. (BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF | \
  167. BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL | \
  168. BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS | \
  169. BTRFS_FEATURE_INCOMPAT_BIG_METADATA | \
  170. BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO | \
  171. BTRFS_FEATURE_INCOMPAT_RAID56 | \
  172. BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF | \
  173. BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA | \
  174. BTRFS_FEATURE_INCOMPAT_NO_HOLES)
  175. #define BTRFS_FEATURE_INCOMPAT_SAFE_SET \
  176. (BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
  177. #define BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR 0ULL
  178. /*
  179. * A leaf is full of items. offset and size tell us where to find
  180. * the item in the leaf (relative to the start of the data area)
  181. */
  182. struct btrfs_item {
  183. struct btrfs_key key;
  184. __u32 offset;
  185. __u32 size;
  186. } __attribute__ ((__packed__));
  187. /*
  188. * leaves have an item area and a data area:
  189. * [item0, item1....itemN] [free space] [dataN...data1, data0]
  190. *
  191. * The data is separate from the items to get the keys closer together
  192. * during searches.
  193. */
  194. struct btrfs_leaf {
  195. struct btrfs_header header;
  196. struct btrfs_item items[];
  197. } __attribute__ ((__packed__));
  198. /*
  199. * all non-leaf blocks are nodes, they hold only keys and pointers to
  200. * other blocks
  201. */
  202. struct btrfs_key_ptr {
  203. struct btrfs_key key;
  204. __u64 blockptr;
  205. __u64 generation;
  206. } __attribute__ ((__packed__));
  207. struct btrfs_node {
  208. struct btrfs_header header;
  209. struct btrfs_key_ptr ptrs[];
  210. } __attribute__ ((__packed__));
  211. union btrfs_tree_node {
  212. struct btrfs_header header;
  213. struct btrfs_leaf leaf;
  214. struct btrfs_node node;
  215. };
  216. typedef __u8 u8;
  217. typedef __u16 u16;
  218. typedef __u32 u32;
  219. typedef __u64 u64;
  220. struct btrfs_path {
  221. union btrfs_tree_node *nodes[BTRFS_MAX_LEVEL];
  222. u32 slots[BTRFS_MAX_LEVEL];
  223. };
  224. struct btrfs_root {
  225. u64 objectid;
  226. u64 bytenr;
  227. u64 root_dirid;
  228. };
  229. int btrfs_comp_keys(struct btrfs_key *, struct btrfs_key *);
  230. int btrfs_comp_keys_type(struct btrfs_key *, struct btrfs_key *);
  231. int btrfs_bin_search(union btrfs_tree_node *, struct btrfs_key *, int *);
  232. void btrfs_free_path(struct btrfs_path *);
  233. int btrfs_search_tree(const struct btrfs_root *, struct btrfs_key *,
  234. struct btrfs_path *);
  235. int btrfs_prev_slot(struct btrfs_path *);
  236. int btrfs_next_slot(struct btrfs_path *);
  237. static inline struct btrfs_key *btrfs_path_leaf_key(struct btrfs_path *p) {
  238. return &p->nodes[0]->leaf.items[p->slots[0]].key;
  239. }
  240. static inline struct btrfs_key *
  241. btrfs_search_tree_key_type(const struct btrfs_root *root, u64 objectid,
  242. u8 type, struct btrfs_path *path)
  243. {
  244. struct btrfs_key key, *res;
  245. key.objectid = objectid;
  246. key.type = type;
  247. key.offset = 0;
  248. if (btrfs_search_tree(root, &key, path))
  249. return NULL;
  250. res = btrfs_path_leaf_key(path);
  251. if (btrfs_comp_keys_type(&key, res)) {
  252. btrfs_free_path(path);
  253. return NULL;
  254. }
  255. return res;
  256. }
  257. static inline u32 btrfs_path_item_size(struct btrfs_path *p)
  258. {
  259. return p->nodes[0]->leaf.items[p->slots[0]].size;
  260. }
  261. static inline void *btrfs_leaf_data(struct btrfs_leaf *leaf, u32 slot)
  262. {
  263. return ((u8 *) leaf) + sizeof(struct btrfs_header)
  264. + leaf->items[slot].offset;
  265. }
  266. static inline void *btrfs_path_leaf_data(struct btrfs_path *p)
  267. {
  268. return btrfs_leaf_data(&p->nodes[0]->leaf, p->slots[0]);
  269. }
  270. #define btrfs_item_ptr(l,s,t) \
  271. ((t *) btrfs_leaf_data((l),(s)))
  272. #define btrfs_path_item_ptr(p,t) \
  273. ((t *) btrfs_path_leaf_data((p)))
  274. #endif /* __BTRFS_CTREE_H__ */