]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/subvolume.h
6905e91a947087fabdcce1525ade64d5cd4ef684
[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_tree(struct bch_fs *c, u32 id)
46 {
47         rcu_read_lock();
48         id = snapshot_t(c, id)->tree;
49         rcu_read_unlock();
50
51         return id;
52 }
53
54 static inline u32 __bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
55 {
56         return snapshot_t(c, id)->parent;
57 }
58
59 static inline u32 bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
60 {
61         rcu_read_lock();
62         id = __bch2_snapshot_parent_early(c, id);
63         rcu_read_unlock();
64
65         return id;
66 }
67
68 static inline u32 __bch2_snapshot_parent(struct bch_fs *c, u32 id)
69 {
70 #ifdef CONFIG_BCACHEFS_DEBUG
71         u32 parent = snapshot_t(c, id)->parent;
72
73         if (parent &&
74             snapshot_t(c, id)->depth != snapshot_t(c, parent)->depth + 1)
75                 panic("id %u depth=%u parent %u depth=%u\n",
76                       id, snapshot_t(c, id)->depth,
77                       parent, snapshot_t(c, parent)->depth);
78
79         return parent;
80 #else
81         return snapshot_t(c, id)->parent;
82 #endif
83 }
84
85 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
86 {
87         rcu_read_lock();
88         id = __bch2_snapshot_parent(c, id);
89         rcu_read_unlock();
90
91         return id;
92 }
93
94 static inline u32 bch2_snapshot_nth_parent(struct bch_fs *c, u32 id, u32 n)
95 {
96         rcu_read_lock();
97         while (n--)
98                 id = __bch2_snapshot_parent(c, id);
99         rcu_read_unlock();
100
101         return id;
102 }
103
104 static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
105 {
106         u32 parent;
107
108         rcu_read_lock();
109         while ((parent = __bch2_snapshot_parent(c, id)))
110                 id = parent;
111         rcu_read_unlock();
112
113         return id;
114 }
115
116 static inline u32 __bch2_snapshot_equiv(struct bch_fs *c, u32 id)
117 {
118         return snapshot_t(c, id)->equiv;
119 }
120
121 static inline u32 bch2_snapshot_equiv(struct bch_fs *c, u32 id)
122 {
123         rcu_read_lock();
124         id = __bch2_snapshot_equiv(c, id);
125         rcu_read_unlock();
126
127         return id;
128 }
129
130 static inline bool bch2_snapshot_is_equiv(struct bch_fs *c, u32 id)
131 {
132         return id == bch2_snapshot_equiv(c, id);
133 }
134
135 static inline bool bch2_snapshot_is_internal_node(struct bch_fs *c, u32 id)
136 {
137         const struct snapshot_t *s;
138         bool ret;
139
140         rcu_read_lock();
141         s = snapshot_t(c, id);
142         ret = s->children[0];
143         rcu_read_unlock();
144
145         return ret;
146 }
147
148 static inline u32 bch2_snapshot_is_leaf(struct bch_fs *c, u32 id)
149 {
150         return !bch2_snapshot_is_internal_node(c, id);
151 }
152
153 static inline u32 bch2_snapshot_sibling(struct bch_fs *c, u32 id)
154 {
155         const struct snapshot_t *s;
156         u32 parent = __bch2_snapshot_parent(c, id);
157
158         if (!parent)
159                 return 0;
160
161         s = snapshot_t(c, __bch2_snapshot_parent(c, id));
162         if (id == s->children[0])
163                 return s->children[1];
164         if (id == s->children[1])
165                 return s->children[0];
166         return 0;
167 }
168
169 bool __bch2_snapshot_is_ancestor(struct bch_fs *, u32, u32);
170
171 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
172 {
173         return id == ancestor
174                 ? true
175                 : __bch2_snapshot_is_ancestor(c, id, ancestor);
176 }
177
178 static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
179 {
180         const struct snapshot_t *t;
181         bool ret;
182
183         rcu_read_lock();
184         t = snapshot_t(c, id);
185         ret = (t->children[0]|t->children[1]) != 0;
186         rcu_read_unlock();
187
188         return ret;
189 }
190
191 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
192 {
193         u32 *i;
194
195         darray_for_each(*s, i)
196                 if (*i == id)
197                         return true;
198         return false;
199 }
200
201 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
202 {
203         u32 *i;
204
205         darray_for_each(*s, i)
206                 if (bch2_snapshot_is_ancestor(c, id, *i))
207                         return true;
208         return false;
209 }
210
211 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
212 {
213         int ret;
214
215         BUG_ON(snapshot_list_has_id(s, id));
216         ret = darray_push(s, id);
217         if (ret)
218                 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
219         return ret;
220 }
221
222 int bch2_check_snapshot_trees(struct bch_fs *);
223 int bch2_check_snapshots(struct bch_fs *);
224 int bch2_check_subvols(struct bch_fs *);
225
226 void bch2_fs_snapshots_exit(struct bch_fs *);
227 int bch2_snapshots_read(struct bch_fs *);
228
229 int bch2_subvolume_invalid(const struct bch_fs *, struct bkey_s_c,
230                            unsigned, struct printbuf *);
231 void bch2_subvolume_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
232
233 #define bch2_bkey_ops_subvolume ((struct bkey_ops) {            \
234         .key_invalid    = bch2_subvolume_invalid,               \
235         .val_to_text    = bch2_subvolume_to_text,               \
236         .min_val_size   = 16,                                   \
237 })
238
239 int bch2_subvolume_get(struct btree_trans *, unsigned,
240                        bool, int, struct bch_subvolume *);
241 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
242                              struct bch_subvolume *);
243 int bch2_subvolume_get_snapshot(struct btree_trans *, u32, u32 *);
244
245 /* only exported for tests: */
246 int bch2_snapshot_node_create(struct btree_trans *, u32,
247                               u32 *, u32 *, unsigned);
248
249 int bch2_delete_dead_snapshots(struct bch_fs *);
250 void bch2_delete_dead_snapshots_async(struct bch_fs *);
251
252 int bch2_subvolume_unlink(struct btree_trans *, u32);
253 int bch2_subvolume_create(struct btree_trans *, u64, u32,
254                           u32 *, u32 *, bool);
255
256 int bch2_fs_subvolumes_init(struct bch_fs *);
257
258 #endif /* _BCACHEFS_SUBVOLUME_H */