1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_SNAPSHOT_H
3 #define _BCACHEFS_SNAPSHOT_H
4
5 void bch2_snapshot_tree_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
6 int bch2_snapshot_tree_validate(struct bch_fs *, struct bkey_s_c,
7 struct bkey_validate_context);
8
9 #define bch2_bkey_ops_snapshot_tree ((struct bkey_ops) { \
10 .key_validate = bch2_snapshot_tree_validate, \
11 .val_to_text = bch2_snapshot_tree_to_text, \
12 .min_val_size = 8, \
13 })
14
15 struct bkey_i_snapshot_tree *__bch2_snapshot_tree_create(struct btree_trans *);
16
17 int bch2_snapshot_tree_lookup(struct btree_trans *, u32, struct bch_snapshot_tree *);
18
19 void bch2_snapshot_to_text(struct printbuf *, struct bch_fs *, struct bkey_s_c);
20 int bch2_snapshot_validate(struct bch_fs *, struct bkey_s_c,
21 struct bkey_validate_context);
22 int bch2_mark_snapshot(struct btree_trans *, enum btree_id, unsigned,
23 struct bkey_s_c, struct bkey_s,
24 enum btree_iter_update_trigger_flags);
25
26 #define bch2_bkey_ops_snapshot ((struct bkey_ops) { \
27 .key_validate = bch2_snapshot_validate, \
28 .val_to_text = bch2_snapshot_to_text, \
29 .trigger = bch2_mark_snapshot, \
30 .min_val_size = 24, \
31 })
32
__snapshot_t(struct snapshot_table * t,u32 id)33 static inline struct snapshot_t *__snapshot_t(struct snapshot_table *t, u32 id)
34 {
35 u32 idx = U32_MAX - id;
36
37 return likely(t && idx < t->nr)
38 ? &t->s[idx]
39 : NULL;
40 }
41
snapshot_t(struct bch_fs * c,u32 id)42 static inline const struct snapshot_t *snapshot_t(struct bch_fs *c, u32 id)
43 {
44 return __snapshot_t(rcu_dereference(c->snapshots), id);
45 }
46
bch2_snapshot_tree(struct bch_fs * c,u32 id)47 static inline u32 bch2_snapshot_tree(struct bch_fs *c, u32 id)
48 {
49 rcu_read_lock();
50 const struct snapshot_t *s = snapshot_t(c, id);
51 id = s ? s->tree : 0;
52 rcu_read_unlock();
53
54 return id;
55 }
56
__bch2_snapshot_parent_early(struct bch_fs * c,u32 id)57 static inline u32 __bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
58 {
59 const struct snapshot_t *s = snapshot_t(c, id);
60 return s ? s->parent : 0;
61 }
62
bch2_snapshot_parent_early(struct bch_fs * c,u32 id)63 static inline u32 bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
64 {
65 rcu_read_lock();
66 id = __bch2_snapshot_parent_early(c, id);
67 rcu_read_unlock();
68
69 return id;
70 }
71
__bch2_snapshot_parent(struct bch_fs * c,u32 id)72 static inline u32 __bch2_snapshot_parent(struct bch_fs *c, u32 id)
73 {
74 const struct snapshot_t *s = snapshot_t(c, id);
75 if (!s)
76 return 0;
77
78 u32 parent = s->parent;
79 if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
80 parent &&
81 s->depth != snapshot_t(c, parent)->depth + 1)
82 panic("id %u depth=%u parent %u depth=%u\n",
83 id, snapshot_t(c, id)->depth,
84 parent, snapshot_t(c, parent)->depth);
85
86 return parent;
87 }
88
bch2_snapshot_parent(struct bch_fs * c,u32 id)89 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
90 {
91 rcu_read_lock();
92 id = __bch2_snapshot_parent(c, id);
93 rcu_read_unlock();
94
95 return id;
96 }
97
bch2_snapshot_nth_parent(struct bch_fs * c,u32 id,u32 n)98 static inline u32 bch2_snapshot_nth_parent(struct bch_fs *c, u32 id, u32 n)
99 {
100 rcu_read_lock();
101 while (n--)
102 id = __bch2_snapshot_parent(c, id);
103 rcu_read_unlock();
104
105 return id;
106 }
107
108 u32 bch2_snapshot_skiplist_get(struct bch_fs *, u32);
109
bch2_snapshot_root(struct bch_fs * c,u32 id)110 static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
111 {
112 u32 parent;
113
114 rcu_read_lock();
115 while ((parent = __bch2_snapshot_parent(c, id)))
116 id = parent;
117 rcu_read_unlock();
118
119 return id;
120 }
121
__bch2_snapshot_exists(struct bch_fs * c,u32 id)122 static inline bool __bch2_snapshot_exists(struct bch_fs *c, u32 id)
123 {
124 const struct snapshot_t *s = snapshot_t(c, id);
125 return s ? s->live : 0;
126 }
127
bch2_snapshot_exists(struct bch_fs * c,u32 id)128 static inline bool bch2_snapshot_exists(struct bch_fs *c, u32 id)
129 {
130 rcu_read_lock();
131 bool ret = __bch2_snapshot_exists(c, id);
132 rcu_read_unlock();
133
134 return ret;
135 }
136
bch2_snapshot_is_internal_node(struct bch_fs * c,u32 id)137 static inline int bch2_snapshot_is_internal_node(struct bch_fs *c, u32 id)
138 {
139 rcu_read_lock();
140 const struct snapshot_t *s = snapshot_t(c, id);
141 int ret = s ? s->children[0] : -BCH_ERR_invalid_snapshot_node;
142 rcu_read_unlock();
143
144 return ret;
145 }
146
bch2_snapshot_is_leaf(struct bch_fs * c,u32 id)147 static inline int bch2_snapshot_is_leaf(struct bch_fs *c, u32 id)
148 {
149 int ret = bch2_snapshot_is_internal_node(c, id);
150 if (ret < 0)
151 return ret;
152 return !ret;
153 }
154
bch2_snapshot_depth(struct bch_fs * c,u32 parent)155 static inline u32 bch2_snapshot_depth(struct bch_fs *c, u32 parent)
156 {
157 u32 depth;
158
159 rcu_read_lock();
160 depth = parent ? snapshot_t(c, parent)->depth + 1 : 0;
161 rcu_read_unlock();
162
163 return depth;
164 }
165
166 bool __bch2_snapshot_is_ancestor(struct bch_fs *, u32, u32);
167
bch2_snapshot_is_ancestor(struct bch_fs * c,u32 id,u32 ancestor)168 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
169 {
170 return id == ancestor
171 ? true
172 : __bch2_snapshot_is_ancestor(c, id, ancestor);
173 }
174
bch2_snapshot_has_children(struct bch_fs * c,u32 id)175 static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
176 {
177 rcu_read_lock();
178 const struct snapshot_t *t = snapshot_t(c, id);
179 bool ret = t && (t->children[0]|t->children[1]) != 0;
180 rcu_read_unlock();
181
182 return ret;
183 }
184
snapshot_list_has_id(snapshot_id_list * s,u32 id)185 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
186 {
187 darray_for_each(*s, i)
188 if (*i == id)
189 return true;
190 return false;
191 }
192
snapshot_list_has_ancestor(struct bch_fs * c,snapshot_id_list * s,u32 id)193 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
194 {
195 darray_for_each(*s, i)
196 if (bch2_snapshot_is_ancestor(c, id, *i))
197 return true;
198 return false;
199 }
200
snapshot_list_add(struct bch_fs * c,snapshot_id_list * s,u32 id)201 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
202 {
203 BUG_ON(snapshot_list_has_id(s, id));
204 int ret = darray_push(s, id);
205 if (ret)
206 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
207 return ret;
208 }
209
snapshot_list_add_nodup(struct bch_fs * c,snapshot_id_list * s,u32 id)210 static inline int snapshot_list_add_nodup(struct bch_fs *c, snapshot_id_list *s, u32 id)
211 {
212 int ret = snapshot_list_has_id(s, id)
213 ? 0
214 : darray_push(s, id);
215 if (ret)
216 bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
217 return ret;
218 }
219
snapshot_list_merge(struct bch_fs * c,snapshot_id_list * dst,snapshot_id_list * src)220 static inline int snapshot_list_merge(struct bch_fs *c, snapshot_id_list *dst, snapshot_id_list *src)
221 {
222 darray_for_each(*src, i) {
223 int ret = snapshot_list_add_nodup(c, dst, *i);
224 if (ret)
225 return ret;
226 }
227
228 return 0;
229 }
230
231 int bch2_snapshot_lookup(struct btree_trans *trans, u32 id,
232 struct bch_snapshot *s);
233 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
234 struct bch_subvolume *);
235
236 /* only exported for tests: */
237 int bch2_snapshot_node_create(struct btree_trans *, u32,
238 u32 *, u32 *, unsigned);
239
240 int bch2_check_snapshot_trees(struct bch_fs *);
241 int bch2_check_snapshots(struct bch_fs *);
242 int bch2_reconstruct_snapshots(struct bch_fs *);
243 int bch2_check_key_has_snapshot(struct btree_trans *, struct btree_iter *, struct bkey_s_c);
244
245 int bch2_snapshot_node_set_deleted(struct btree_trans *, u32);
246 void bch2_delete_dead_snapshots_work(struct work_struct *);
247
248 int __bch2_key_has_snapshot_overwrites(struct btree_trans *, enum btree_id, struct bpos);
249
bch2_key_has_snapshot_overwrites(struct btree_trans * trans,enum btree_id id,struct bpos pos)250 static inline int bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
251 enum btree_id id,
252 struct bpos pos)
253 {
254 if (!btree_type_has_snapshots(id) ||
255 bch2_snapshot_is_leaf(trans->c, pos.snapshot) > 0)
256 return 0;
257
258 return __bch2_key_has_snapshot_overwrites(trans, id, pos);
259 }
260
261 int bch2_snapshots_read(struct bch_fs *);
262 void bch2_fs_snapshots_exit(struct bch_fs *);
263
264 #endif /* _BCACHEFS_SNAPSHOT_H */
265