Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Marek Behún | 4eff426 | 2017-09-03 17:00:26 +0200 | [diff] [blame] | 2 | /* |
| 3 | * From linux/fs/btrfs/ctree.h |
| 4 | * Copyright (C) 2007,2008 Oracle. All rights reserved. |
| 5 | * |
| 6 | * Modified in 2017 by Marek Behun, CZ.NIC, marek.behun@nic.cz |
Marek Behún | 4eff426 | 2017-09-03 17:00:26 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #ifndef __BTRFS_CTREE_H__ |
| 10 | #define __BTRFS_CTREE_H__ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <compiler.h> |
Qu Wenruo | c43c529 | 2020-06-24 18:02:52 +0200 | [diff] [blame^] | 14 | #include <linux/rbtree.h> |
Qu Wenruo | 07d977f | 2020-06-24 18:02:47 +0200 | [diff] [blame] | 15 | #include "kernel-shared/btrfs_tree.h" |
Qu Wenruo | 1a61808 | 2020-06-24 18:02:49 +0200 | [diff] [blame] | 16 | #include "compat.h" |
Qu Wenruo | c43c529 | 2020-06-24 18:02:52 +0200 | [diff] [blame^] | 17 | #include "extent-io.h" |
Marek Behún | 4eff426 | 2017-09-03 17:00:26 +0200 | [diff] [blame] | 18 | |
| 19 | #define BTRFS_MAX_MIRRORS 3 |
| 20 | |
Marek Behún | 4eff426 | 2017-09-03 17:00:26 +0200 | [diff] [blame] | 21 | /* |
| 22 | * the max metadata block size. This limit is somewhat artificial, |
| 23 | * but the memmove costs go through the roof for larger blocks. |
| 24 | */ |
| 25 | #define BTRFS_MAX_METADATA_BLOCKSIZE 65536 |
| 26 | |
| 27 | /* |
Marek Behún | 4eff426 | 2017-09-03 17:00:26 +0200 | [diff] [blame] | 28 | * Theoretical limit is larger, but we keep this down to a sane |
| 29 | * value. That should limit greatly the possibility of collisions on |
| 30 | * inode ref items. |
| 31 | */ |
| 32 | #define BTRFS_LINK_MAX 65535U |
| 33 | |
Marek Behún | 4eff426 | 2017-09-03 17:00:26 +0200 | [diff] [blame] | 34 | /* four bytes for CRC32 */ |
| 35 | #define BTRFS_EMPTY_DIR_SIZE 0 |
| 36 | |
| 37 | /* ioprio of readahead is set to idle */ |
| 38 | #define BTRFS_IOPRIO_READA (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0)) |
| 39 | |
| 40 | #define BTRFS_DIRTY_METADATA_THRESH SZ_32M |
| 41 | |
| 42 | #define BTRFS_MAX_EXTENT_SIZE SZ_128M |
| 43 | |
| 44 | /* |
| 45 | * File system states |
| 46 | */ |
| 47 | #define BTRFS_FS_STATE_ERROR 0 |
| 48 | #define BTRFS_FS_STATE_REMOUNTING 1 |
| 49 | #define BTRFS_FS_STATE_TRANS_ABORTED 2 |
| 50 | #define BTRFS_FS_STATE_DEV_REPLACING 3 |
| 51 | #define BTRFS_FS_STATE_DUMMY_FS_INFO 4 |
| 52 | |
Qu Wenruo | c43c529 | 2020-06-24 18:02:52 +0200 | [diff] [blame^] | 53 | #define read_eb_member(eb, ptr, type, member, result) ( \ |
| 54 | read_extent_buffer(eb, (char *)(result), \ |
| 55 | ((unsigned long)(ptr)) + \ |
| 56 | offsetof(type, member), \ |
| 57 | sizeof(((type *)0)->member))) |
| 58 | |
| 59 | #define write_eb_member(eb, ptr, type, member, result) ( \ |
| 60 | write_extent_buffer(eb, (char *)(result), \ |
| 61 | ((unsigned long)(ptr)) + \ |
| 62 | offsetof(type, member), \ |
| 63 | sizeof(((type *)0)->member))) |
| 64 | |
Qu Wenruo | 1a61808 | 2020-06-24 18:02:49 +0200 | [diff] [blame] | 65 | #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \ |
| 66 | static inline u##bits btrfs_##name(const type *s) \ |
| 67 | { \ |
| 68 | return le##bits##_to_cpu(s->member); \ |
| 69 | } \ |
| 70 | static inline void btrfs_set_##name(type *s, u##bits val) \ |
| 71 | { \ |
| 72 | s->member = cpu_to_le##bits(val); \ |
| 73 | } |
| 74 | |
Marek Behún | 4eff426 | 2017-09-03 17:00:26 +0200 | [diff] [blame] | 75 | union btrfs_tree_node { |
| 76 | struct btrfs_header header; |
| 77 | struct btrfs_leaf leaf; |
| 78 | struct btrfs_node node; |
| 79 | }; |
| 80 | |
Marek Behún | 4eff426 | 2017-09-03 17:00:26 +0200 | [diff] [blame] | 81 | struct btrfs_path { |
| 82 | union btrfs_tree_node *nodes[BTRFS_MAX_LEVEL]; |
| 83 | u32 slots[BTRFS_MAX_LEVEL]; |
| 84 | }; |
| 85 | |
| 86 | struct btrfs_root { |
| 87 | u64 objectid; |
| 88 | u64 bytenr; |
| 89 | u64 root_dirid; |
| 90 | }; |
| 91 | |
Qu Wenruo | c43c529 | 2020-06-24 18:02:52 +0200 | [diff] [blame^] | 92 | struct btrfs_mapping_tree { |
| 93 | struct cache_tree cache_tree; |
| 94 | }; |
| 95 | |
| 96 | struct btrfs_device; |
| 97 | struct btrfs_fs_info { |
| 98 | u8 chunk_tree_uuid[BTRFS_UUID_SIZE]; |
| 99 | u8 *new_chunk_tree_uuid; |
| 100 | struct btrfs_root *fs_root; |
| 101 | struct btrfs_root *tree_root; |
| 102 | struct btrfs_root *chunk_root; |
| 103 | struct btrfs_root *csum_root; |
| 104 | |
| 105 | struct rb_root fs_root_tree; |
| 106 | |
| 107 | struct extent_io_tree extent_cache; |
| 108 | struct extent_io_tree free_space_cache; |
| 109 | struct extent_io_tree pinned_extents; |
| 110 | struct extent_io_tree extent_ins; |
| 111 | struct extent_io_tree *excluded_extents; |
| 112 | |
| 113 | struct rb_root block_group_cache_tree; |
| 114 | /* logical->physical extent mapping */ |
| 115 | struct btrfs_mapping_tree mapping_tree; |
| 116 | |
| 117 | u64 generation; |
| 118 | u64 last_trans_committed; |
| 119 | |
| 120 | struct btrfs_super_block *super_copy; |
| 121 | |
| 122 | u64 super_bytenr; |
| 123 | |
| 124 | /* Only support one device yet */ |
| 125 | struct btrfs_devvice *dev; |
| 126 | |
| 127 | /* Cached block sizes */ |
| 128 | u32 nodesize; |
| 129 | u32 sectorsize; |
| 130 | u32 stripesize; |
| 131 | }; |
| 132 | |
Marek Behún | 4eff426 | 2017-09-03 17:00:26 +0200 | [diff] [blame] | 133 | int btrfs_comp_keys(struct btrfs_key *, struct btrfs_key *); |
| 134 | int btrfs_comp_keys_type(struct btrfs_key *, struct btrfs_key *); |
| 135 | int btrfs_bin_search(union btrfs_tree_node *, struct btrfs_key *, int *); |
| 136 | void btrfs_free_path(struct btrfs_path *); |
| 137 | int btrfs_search_tree(const struct btrfs_root *, struct btrfs_key *, |
| 138 | struct btrfs_path *); |
| 139 | int btrfs_prev_slot(struct btrfs_path *); |
| 140 | int btrfs_next_slot(struct btrfs_path *); |
| 141 | |
| 142 | static inline struct btrfs_key *btrfs_path_leaf_key(struct btrfs_path *p) { |
Qu Wenruo | 07d977f | 2020-06-24 18:02:47 +0200 | [diff] [blame] | 143 | /* At tree read time we have converted the endian for btrfs_disk_key */ |
| 144 | return (struct btrfs_key *)&p->nodes[0]->leaf.items[p->slots[0]].key; |
Marek Behún | 4eff426 | 2017-09-03 17:00:26 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static inline struct btrfs_key * |
| 148 | btrfs_search_tree_key_type(const struct btrfs_root *root, u64 objectid, |
| 149 | u8 type, struct btrfs_path *path) |
| 150 | { |
| 151 | struct btrfs_key key, *res; |
| 152 | |
| 153 | key.objectid = objectid; |
| 154 | key.type = type; |
Tom Rini | 2d8200c | 2019-04-27 11:34:55 -0400 | [diff] [blame] | 155 | key.offset = 0; |
Marek Behún | 4eff426 | 2017-09-03 17:00:26 +0200 | [diff] [blame] | 156 | |
| 157 | if (btrfs_search_tree(root, &key, path)) |
| 158 | return NULL; |
| 159 | |
Pierre Bourdon | 9fffe32 | 2019-04-13 23:50:49 +0200 | [diff] [blame] | 160 | res = btrfs_path_leaf_key(path); |
Tom Rini | 2d8200c | 2019-04-27 11:34:55 -0400 | [diff] [blame] | 161 | if (btrfs_comp_keys_type(&key, res)) { |
| 162 | btrfs_free_path(path); |
| 163 | return NULL; |
| 164 | } |
Pierre Bourdon | 9fffe32 | 2019-04-13 23:50:49 +0200 | [diff] [blame] | 165 | |
Tom Rini | 2d8200c | 2019-04-27 11:34:55 -0400 | [diff] [blame] | 166 | return res; |
Marek Behún | 4eff426 | 2017-09-03 17:00:26 +0200 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static inline u32 btrfs_path_item_size(struct btrfs_path *p) |
| 170 | { |
| 171 | return p->nodes[0]->leaf.items[p->slots[0]].size; |
| 172 | } |
| 173 | |
| 174 | static inline void *btrfs_leaf_data(struct btrfs_leaf *leaf, u32 slot) |
| 175 | { |
| 176 | return ((u8 *) leaf) + sizeof(struct btrfs_header) |
| 177 | + leaf->items[slot].offset; |
| 178 | } |
| 179 | |
| 180 | static inline void *btrfs_path_leaf_data(struct btrfs_path *p) |
| 181 | { |
| 182 | return btrfs_leaf_data(&p->nodes[0]->leaf, p->slots[0]); |
| 183 | } |
| 184 | |
| 185 | #define btrfs_item_ptr(l,s,t) \ |
| 186 | ((t *) btrfs_leaf_data((l),(s))) |
| 187 | |
| 188 | #define btrfs_path_item_ptr(p,t) \ |
| 189 | ((t *) btrfs_path_leaf_data((p))) |
| 190 | |
Qu Wenruo | 1a61808 | 2020-06-24 18:02:49 +0200 | [diff] [blame] | 191 | u16 btrfs_super_csum_size(const struct btrfs_super_block *s); |
| 192 | const char *btrfs_super_csum_name(u16 csum_type); |
| 193 | u16 btrfs_csum_type_size(u16 csum_type); |
| 194 | size_t btrfs_super_num_csums(void); |
| 195 | |
| 196 | /* struct btrfs_super_block */ |
| 197 | |
| 198 | BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64); |
| 199 | BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64); |
| 200 | BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block, |
| 201 | generation, 64); |
| 202 | BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64); |
| 203 | BTRFS_SETGET_STACK_FUNCS(super_sys_array_size, |
| 204 | struct btrfs_super_block, sys_chunk_array_size, 32); |
| 205 | BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation, |
| 206 | struct btrfs_super_block, chunk_root_generation, 64); |
| 207 | BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block, |
| 208 | root_level, 8); |
| 209 | BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block, |
| 210 | chunk_root, 64); |
| 211 | BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block, |
| 212 | chunk_root_level, 8); |
| 213 | BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block, |
| 214 | log_root, 64); |
| 215 | BTRFS_SETGET_STACK_FUNCS(super_log_root_transid, struct btrfs_super_block, |
| 216 | log_root_transid, 64); |
| 217 | BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block, |
| 218 | log_root_level, 8); |
| 219 | BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block, |
| 220 | total_bytes, 64); |
| 221 | BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block, |
| 222 | bytes_used, 64); |
| 223 | BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block, |
| 224 | sectorsize, 32); |
| 225 | BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block, |
| 226 | nodesize, 32); |
| 227 | BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block, |
| 228 | stripesize, 32); |
| 229 | BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block, |
| 230 | root_dir_objectid, 64); |
| 231 | BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block, |
| 232 | num_devices, 64); |
| 233 | BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block, |
| 234 | compat_flags, 64); |
| 235 | BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block, |
| 236 | compat_ro_flags, 64); |
| 237 | BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block, |
| 238 | incompat_flags, 64); |
| 239 | BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block, |
| 240 | csum_type, 16); |
| 241 | BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block, |
| 242 | cache_generation, 64); |
| 243 | BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block, |
| 244 | uuid_tree_generation, 64); |
| 245 | BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64); |
| 246 | |
Marek Behún | 4eff426 | 2017-09-03 17:00:26 +0200 | [diff] [blame] | 247 | #endif /* __BTRFS_CTREE_H__ */ |