blob: c7528d1ac3dbd82f74888364db1d5baffd57f203 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Marek Behún4eff4262017-09-03 17:00:26 +02002/*
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ún4eff4262017-09-03 17:00:26 +02007 */
8
9#ifndef __BTRFS_CTREE_H__
10#define __BTRFS_CTREE_H__
11
12#include <common.h>
13#include <compiler.h>
Qu Wenruoc43c5292020-06-24 18:02:52 +020014#include <linux/rbtree.h>
Qu Wenruo07d977f2020-06-24 18:02:47 +020015#include "kernel-shared/btrfs_tree.h"
Qu Wenruo1a618082020-06-24 18:02:49 +020016#include "compat.h"
Qu Wenruoc43c5292020-06-24 18:02:52 +020017#include "extent-io.h"
Marek Behún4eff4262017-09-03 17:00:26 +020018
19#define BTRFS_MAX_MIRRORS 3
20
Marek Behún4eff4262017-09-03 17:00:26 +020021/*
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ún4eff4262017-09-03 17:00:26 +020028 * 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ún4eff4262017-09-03 17:00:26 +020034/* 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 Wenruoc43c5292020-06-24 18:02:52 +020053#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 Wenruo1a618082020-06-24 18:02:49 +020065#define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
66static inline u##bits btrfs_##name(const type *s) \
67{ \
68 return le##bits##_to_cpu(s->member); \
69} \
70static inline void btrfs_set_##name(type *s, u##bits val) \
71{ \
72 s->member = cpu_to_le##bits(val); \
73}
74
Marek Behún4eff4262017-09-03 17:00:26 +020075union btrfs_tree_node {
76 struct btrfs_header header;
77 struct btrfs_leaf leaf;
78 struct btrfs_node node;
79};
80
Marek Behún4eff4262017-09-03 17:00:26 +020081struct btrfs_path {
82 union btrfs_tree_node *nodes[BTRFS_MAX_LEVEL];
83 u32 slots[BTRFS_MAX_LEVEL];
84};
85
86struct btrfs_root {
87 u64 objectid;
88 u64 bytenr;
89 u64 root_dirid;
90};
91
Qu Wenruoc43c5292020-06-24 18:02:52 +020092struct btrfs_mapping_tree {
93 struct cache_tree cache_tree;
94};
95
96struct btrfs_device;
97struct 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ún4eff4262017-09-03 17:00:26 +0200133int btrfs_comp_keys(struct btrfs_key *, struct btrfs_key *);
134int btrfs_comp_keys_type(struct btrfs_key *, struct btrfs_key *);
135int btrfs_bin_search(union btrfs_tree_node *, struct btrfs_key *, int *);
136void btrfs_free_path(struct btrfs_path *);
137int btrfs_search_tree(const struct btrfs_root *, struct btrfs_key *,
138 struct btrfs_path *);
139int btrfs_prev_slot(struct btrfs_path *);
140int btrfs_next_slot(struct btrfs_path *);
141
142static inline struct btrfs_key *btrfs_path_leaf_key(struct btrfs_path *p) {
Qu Wenruo07d977f2020-06-24 18:02:47 +0200143 /* 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ún4eff4262017-09-03 17:00:26 +0200145}
146
147static inline struct btrfs_key *
148btrfs_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 Rini2d8200c2019-04-27 11:34:55 -0400155 key.offset = 0;
Marek Behún4eff4262017-09-03 17:00:26 +0200156
157 if (btrfs_search_tree(root, &key, path))
158 return NULL;
159
Pierre Bourdon9fffe322019-04-13 23:50:49 +0200160 res = btrfs_path_leaf_key(path);
Tom Rini2d8200c2019-04-27 11:34:55 -0400161 if (btrfs_comp_keys_type(&key, res)) {
162 btrfs_free_path(path);
163 return NULL;
164 }
Pierre Bourdon9fffe322019-04-13 23:50:49 +0200165
Tom Rini2d8200c2019-04-27 11:34:55 -0400166 return res;
Marek Behún4eff4262017-09-03 17:00:26 +0200167}
168
169static inline u32 btrfs_path_item_size(struct btrfs_path *p)
170{
171 return p->nodes[0]->leaf.items[p->slots[0]].size;
172}
173
174static 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
180static 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 Wenruo1a618082020-06-24 18:02:49 +0200191u16 btrfs_super_csum_size(const struct btrfs_super_block *s);
192const char *btrfs_super_csum_name(u16 csum_type);
193u16 btrfs_csum_type_size(u16 csum_type);
194size_t btrfs_super_num_csums(void);
195
196/* struct btrfs_super_block */
197
198BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
199BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
200BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
201 generation, 64);
202BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
203BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
204 struct btrfs_super_block, sys_chunk_array_size, 32);
205BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
206 struct btrfs_super_block, chunk_root_generation, 64);
207BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
208 root_level, 8);
209BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
210 chunk_root, 64);
211BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
212 chunk_root_level, 8);
213BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
214 log_root, 64);
215BTRFS_SETGET_STACK_FUNCS(super_log_root_transid, struct btrfs_super_block,
216 log_root_transid, 64);
217BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
218 log_root_level, 8);
219BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
220 total_bytes, 64);
221BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
222 bytes_used, 64);
223BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
224 sectorsize, 32);
225BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
226 nodesize, 32);
227BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
228 stripesize, 32);
229BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
230 root_dir_objectid, 64);
231BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
232 num_devices, 64);
233BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
234 compat_flags, 64);
235BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
236 compat_ro_flags, 64);
237BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
238 incompat_flags, 64);
239BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
240 csum_type, 16);
241BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
242 cache_generation, 64);
243BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
244 uuid_tree_generation, 64);
245BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
246
Marek Behún4eff4262017-09-03 17:00:26 +0200247#endif /* __BTRFS_CTREE_H__ */