]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/subvolume.h
Update bcachefs sources to e14d7c7195 bcachefs: Compression levels
[bcachefs-tools-debian] / libbcachefs / subvolume.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SUBVOLUME_H
3 #define _BCACHEFS_SUBVOLUME_H
4
5 #include "darray.h"
6 #include "subvolume_types.h"
7
8 enum bkey_invalid_flags;
9
10 void bch2_snapshot_tree_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
11 int bch2_snapshot_tree_invalid(const struct bch_fs *, struct bkey_s_c,
12                                enum bkey_invalid_flags, struct printbuf *);
13
14 #define bch2_bkey_ops_snapshot_tree ((struct bkey_ops) {        \
15         .key_invalid    = bch2_snapshot_tree_invalid,           \
16         .val_to_text    = bch2_snapshot_tree_to_text,           \
17         .min_val_size   = 8,                                    \
18 })
19
20 int bch2_snapshot_tree_lookup(struct btree_trans *, u32, struct bch_snapshot_tree *);
21
22 void bch2_snapshot_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
23 int bch2_snapshot_invalid(const struct bch_fs *, struct bkey_s_c,
24                           enum bkey_invalid_flags, struct printbuf *);
25 int bch2_mark_snapshot(struct btree_trans *, enum btree_id, unsigned,
26                        struct bkey_s_c, struct bkey_s_c, unsigned);
27
28 #define bch2_bkey_ops_snapshot ((struct bkey_ops) {             \
29         .key_invalid    = bch2_snapshot_invalid,                \
30         .val_to_text    = bch2_snapshot_to_text,                \
31         .atomic_trigger = bch2_mark_snapshot,                   \
32         .min_val_size   = 24,                                   \
33 })
34
35 static inline struct snapshot_t *__snapshot_t(struct snapshot_table *t, u32 id)
36 {
37         return &t->s[U32_MAX - id];
38 }
39
40 static inline const struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
41 {
42         return __snapshot_t(rcu_dereference(c->snapshots), id);
43 }
44
45 static inline u32 __bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
46 {
47         return snapshot_t(c, id)->parent;
48 }
49
50 static inline u32 bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
51 {
52         rcu_read_lock();
53         id = __bch2_snapshot_parent_early(c, id);
54         rcu_read_unlock();
55
56         return id;
57 }
58
59 static inline u32 __bch2_snapshot_parent(struct bch_fs *c, u32 id)
60 {
61 #ifdef CONFIG_BCACHEFS_DEBUG
62         u32 parent = snapshot_t(c, id)->parent;
63
64         if (parent &&
65             snapshot_t(c, id)->depth != snapshot_t(c, parent)->depth + 1)
66                 panic("id %u depth=%u parent %u depth=%u\n",
67                       id, snapshot_t(c, id)->depth,
68                       parent, snapshot_t(c, parent)->depth);
69
70         return parent;
71 #else
72         return snapshot_t(c, id)->parent;
73 #endif
74 }
75
76 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
77 {
78         rcu_read_lock();
79         id = __bch2_snapshot_parent(c, id);
80         rcu_read_unlock();
81
82         return id;
83 }
84
85 static inline u32 bch2_snapshot_nth_parent(struct bch_fs *c, u32 id, u32 n)
86 {
87         rcu_read_lock();
88         while (n--)
89                 id = __bch2_snapshot_parent(c, id);
90         rcu_read_unlock();
91
92         return id;
93 }
94
95 static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
96 {
97         u32 parent;
98
99         rcu_read_lock();
100         while ((parent = __bch2_snapshot_parent(c, id)))
101                 id = parent;
102         rcu_read_unlock();
103
104         return id;
105 }
106
107 static inline u32 __bch2_snapshot_equiv(struct bch_fs *c, u32 id)
108 {
109         return snapshot_t(c, id)->equiv;
110 }
111
112 static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
113 {
114         rcu_read_lock();
115         id = __bch2_snapshot_equiv(c, id);
116         rcu_read_unlock();
117
118         return id;
119 }
120
121 static inline bool bch2_snapshot_is_equiv(struct bch_fs *c, u32 id)
122 {
123         return id == bch2_snapshot_equiv(c, id);
124 }
125
126 static inline bool bch2_snapshot_is_internal_node(struct bch_fs *c, u32 id)
127 {
128         const struct snapshot_t *s;
129         bool ret;
130
131         rcu_read_lock();
132         s = snapshot_t(c, id);
133         ret = s->children[0];
134         rcu_read_unlock();
135
136         return ret;
137 }
138
139 static inline u32 bch2_snapshot_is_leaf(struct bch_fs *c, u32 id)
140 {
141         return !bch2_snapshot_is_internal_node(c, id);
142 }
143
144 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
145 {
146         const struct snapshot_t *s;
147         u32 parent = __bch2_snapshot_parent(c, id);
148
149         if (!parent)
150                 return 0;
151
152         s = snapshot_t(c, __bch2_snapshot_parent(c, id));
153         if (id == s->children[0])
154                 return s->children[1];
155         if (id == s->children[1])
156                 return s->children[0];
157         return 0;
158 }
159
160 bool bch2_snapshot_is_ancestor(struct bch_fs *, u32, u32);
161
162 static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
163 {
164         const struct snapshot_t *t;
165         bool ret;
166
167         rcu_read_lock();
168         t = snapshot_t(c, id);
169         ret = (t->children[0]|t->children[1]) != 0;
170         rcu_read_unlock();
171
172         return ret;
173 }
174
175 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
176 {
177         u32 *i;
178
179         darray_for_each(*s, i)
180                 if (*i == id)
181                         return true;
182         return false;
183 }
184
185 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
186 {
187         u32 *i;
188
189         darray_for_each(*s, i)
190                 if (bch2_snapshot_is_ancestor(c, id, *i))
191                         return true;
192         return false;
193 }
194
195 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
196 {
197         int ret;
198
199         BUG_ON(snapshot_list_has_id(s, id));
200         ret = darray_push(s, id);
201         if (ret)
202                 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
203         return ret;
204 }
205
206 int bch2_check_snapshot_trees(struct bch_fs *);
207 int bch2_check_snapshots(struct bch_fs *);
208 int bch2_check_subvols(struct bch_fs *);
209
210 void bch2_fs_snapshots_exit(struct bch_fs *);
211 int bch2_snapshots_read(struct bch_fs *);
212
213 int bch2_subvolume_invalid(const struct bch_fs *, struct bkey_s_c,
214                            unsigned, struct printbuf *);
215 void bch2_subvolume_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
216
217 #define bch2_bkey_ops_subvolume ((struct bkey_ops) {            \
218         .key_invalid    = bch2_subvolume_invalid,               \
219         .val_to_text    = bch2_subvolume_to_text,               \
220         .min_val_size   = 16,                                   \
221 })
222
223 int bch2_subvolume_get(struct btree_trans *, unsigned,
224                        bool, int, struct bch_subvolume *);
225 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
226                              struct bch_subvolume *);
227 int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *);
228
229 /* only exported for tests: */
230 int bch2_snapshot_node_create(struct btree_trans *, u32,
231                               u32 *, u32 *, unsigned);
232
233 int bch2_delete_dead_snapshots(struct bch_fs *);
234 void bch2_delete_dead_snapshots_async(struct bch_fs *);
235
236 int bch2_subvolume_unlink(struct btree_trans *, u32);
237 int bch2_subvolume_create(struct btree_trans *, u64, u32,
238                           u32 *, u32 *, bool);
239
240 int bch2_fs_subvolumes_init(struct bch_fs *);
241
242 #endif /* _BCACHEFS_SUBVOLUME_H */