xref: /linux/fs/bcachefs/snapshot.h (revision ff0905bbf991f4337b5ebc19c0d43525ebb0d96b)
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 	guard(rcu)();
50 	const struct snapshot_t *s = snapshot_t(c, id);
51 	return s ? s->tree : 0;
52 }
53 
__bch2_snapshot_parent_early(struct bch_fs * c,u32 id)54 static inline u32 __bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
55 {
56 	const struct snapshot_t *s = snapshot_t(c, id);
57 	return s ? s->parent : 0;
58 }
59 
bch2_snapshot_parent_early(struct bch_fs * c,u32 id)60 static inline u32 bch2_snapshot_parent_early(struct bch_fs *c, u32 id)
61 {
62 	guard(rcu)();
63 	return __bch2_snapshot_parent_early(c, id);
64 }
65 
__bch2_snapshot_parent(struct bch_fs * c,u32 id)66 static inline u32 __bch2_snapshot_parent(struct bch_fs *c, u32 id)
67 {
68 	const struct snapshot_t *s = snapshot_t(c, id);
69 	if (!s)
70 		return 0;
71 
72 	u32 parent = s->parent;
73 	if (IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
74 	    parent &&
75 	    s->depth != snapshot_t(c, parent)->depth + 1)
76 		panic("id %u depth=%u parent %u depth=%u\n",
77 		      id, snapshot_t(c, id)->depth,
78 		      parent, snapshot_t(c, parent)->depth);
79 
80 	return parent;
81 }
82 
bch2_snapshot_parent(struct bch_fs * c,u32 id)83 static inline u32 bch2_snapshot_parent(struct bch_fs *c, u32 id)
84 {
85 	guard(rcu)();
86 	return __bch2_snapshot_parent(c, id);
87 }
88 
bch2_snapshot_nth_parent(struct bch_fs * c,u32 id,u32 n)89 static inline u32 bch2_snapshot_nth_parent(struct bch_fs *c, u32 id, u32 n)
90 {
91 	guard(rcu)();
92 	while (n--)
93 		id = __bch2_snapshot_parent(c, id);
94 	return id;
95 }
96 
97 u32 bch2_snapshot_oldest_subvol(struct bch_fs *, u32, snapshot_id_list *);
98 u32 bch2_snapshot_skiplist_get(struct bch_fs *, u32);
99 
bch2_snapshot_root(struct bch_fs * c,u32 id)100 static inline u32 bch2_snapshot_root(struct bch_fs *c, u32 id)
101 {
102 	guard(rcu)();
103 
104 	u32 parent;
105 	while ((parent = __bch2_snapshot_parent(c, id)))
106 		id = parent;
107 	return id;
108 }
109 
__bch2_snapshot_id_state(struct bch_fs * c,u32 id)110 static inline enum snapshot_id_state __bch2_snapshot_id_state(struct bch_fs *c, u32 id)
111 {
112 	const struct snapshot_t *s = snapshot_t(c, id);
113 	return s ? s->state : SNAPSHOT_ID_empty;
114 }
115 
bch2_snapshot_id_state(struct bch_fs * c,u32 id)116 static inline enum snapshot_id_state bch2_snapshot_id_state(struct bch_fs *c, u32 id)
117 {
118 	guard(rcu)();
119 	return __bch2_snapshot_id_state(c, 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 	return bch2_snapshot_id_state(c, id) == SNAPSHOT_ID_live;
125 }
126 
bch2_snapshot_is_internal_node(struct bch_fs * c,u32 id)127 static inline int bch2_snapshot_is_internal_node(struct bch_fs *c, u32 id)
128 {
129 	guard(rcu)();
130 	const struct snapshot_t *s = snapshot_t(c, id);
131 	return s ? s->children[0] : -BCH_ERR_invalid_snapshot_node;
132 }
133 
bch2_snapshot_is_leaf(struct bch_fs * c,u32 id)134 static inline int bch2_snapshot_is_leaf(struct bch_fs *c, u32 id)
135 {
136 	int ret = bch2_snapshot_is_internal_node(c, id);
137 	if (ret < 0)
138 		return ret;
139 	return !ret;
140 }
141 
bch2_snapshot_depth(struct bch_fs * c,u32 parent)142 static inline u32 bch2_snapshot_depth(struct bch_fs *c, u32 parent)
143 {
144 	guard(rcu)();
145 	return parent ? snapshot_t(c, parent)->depth + 1 : 0;
146 }
147 
148 bool __bch2_snapshot_is_ancestor(struct bch_fs *, u32, u32);
149 
bch2_snapshot_is_ancestor(struct bch_fs * c,u32 id,u32 ancestor)150 static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
151 {
152 	return id == ancestor
153 		? true
154 		: __bch2_snapshot_is_ancestor(c, id, ancestor);
155 }
156 
bch2_snapshot_has_children(struct bch_fs * c,u32 id)157 static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
158 {
159 	guard(rcu)();
160 	const struct snapshot_t *t = snapshot_t(c, id);
161 	return t && (t->children[0]|t->children[1]) != 0;
162 }
163 
snapshot_list_has_id(snapshot_id_list * s,u32 id)164 static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
165 {
166 	return darray_find(*s, id) != NULL;
167 }
168 
snapshot_list_has_ancestor(struct bch_fs * c,snapshot_id_list * s,u32 id)169 static inline bool snapshot_list_has_ancestor(struct bch_fs *c, snapshot_id_list *s, u32 id)
170 {
171 	darray_for_each(*s, i)
172 		if (bch2_snapshot_is_ancestor(c, id, *i))
173 			return true;
174 	return false;
175 }
176 
snapshot_list_add(struct bch_fs * c,snapshot_id_list * s,u32 id)177 static inline int snapshot_list_add(struct bch_fs *c, snapshot_id_list *s, u32 id)
178 {
179 	BUG_ON(snapshot_list_has_id(s, id));
180 	int ret = darray_push(s, id);
181 	if (ret)
182 		bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
183 	return ret;
184 }
185 
snapshot_list_add_nodup(struct bch_fs * c,snapshot_id_list * s,u32 id)186 static inline int snapshot_list_add_nodup(struct bch_fs *c, snapshot_id_list *s, u32 id)
187 {
188 	int ret = snapshot_list_has_id(s, id)
189 		? 0
190 		: darray_push(s, id);
191 	if (ret)
192 		bch_err(c, "error reallocating snapshot_id_list (size %zu)", s->size);
193 	return ret;
194 }
195 
snapshot_list_merge(struct bch_fs * c,snapshot_id_list * dst,snapshot_id_list * src)196 static inline int snapshot_list_merge(struct bch_fs *c, snapshot_id_list *dst, snapshot_id_list *src)
197 {
198 	darray_for_each(*src, i) {
199 		int ret = snapshot_list_add_nodup(c, dst, *i);
200 		if (ret)
201 			return ret;
202 	}
203 
204 	return 0;
205 }
206 
207 int bch2_snapshot_lookup(struct btree_trans *trans, u32 id,
208 			 struct bch_snapshot *s);
209 int bch2_snapshot_get_subvol(struct btree_trans *, u32,
210 			     struct bch_subvolume *);
211 
212 /* only exported for tests: */
213 int bch2_snapshot_node_create(struct btree_trans *, u32,
214 			      u32 *, u32 *, unsigned);
215 
216 int bch2_check_snapshot_trees(struct bch_fs *);
217 int bch2_check_snapshots(struct bch_fs *);
218 int bch2_reconstruct_snapshots(struct bch_fs *);
219 
220 int __bch2_check_key_has_snapshot(struct btree_trans *, struct btree_iter *, struct bkey_s_c);
221 
bch2_check_key_has_snapshot(struct btree_trans * trans,struct btree_iter * iter,struct bkey_s_c k)222 static inline int bch2_check_key_has_snapshot(struct btree_trans *trans,
223 					      struct btree_iter *iter,
224 					      struct bkey_s_c k)
225 {
226 	return likely(bch2_snapshot_exists(trans->c, k.k->p.snapshot))
227 		? 0
228 		: __bch2_check_key_has_snapshot(trans, iter, k);
229 }
230 
231 int __bch2_get_snapshot_overwrites(struct btree_trans *,
232 				   enum btree_id, struct bpos,
233 				   snapshot_id_list *);
234 
235 /*
236  * Get a list of snapshot IDs that have overwritten a given key:
237  */
bch2_get_snapshot_overwrites(struct btree_trans * trans,enum btree_id btree,struct bpos pos,snapshot_id_list * s)238 static inline int bch2_get_snapshot_overwrites(struct btree_trans *trans,
239 					       enum btree_id btree, struct bpos pos,
240 					       snapshot_id_list *s)
241 {
242 	darray_init(s);
243 
244 	return bch2_snapshot_has_children(trans->c, pos.snapshot)
245 		? __bch2_get_snapshot_overwrites(trans, btree, pos, s)
246 		: 0;
247 
248 }
249 
250 int bch2_snapshot_node_set_deleted(struct btree_trans *, u32);
251 
252 int __bch2_key_has_snapshot_overwrites(struct btree_trans *, enum btree_id, struct bpos);
253 
bch2_key_has_snapshot_overwrites(struct btree_trans * trans,enum btree_id id,struct bpos pos)254 static inline int bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
255 					  enum btree_id id,
256 					  struct bpos pos)
257 {
258 	if (!btree_type_has_snapshots(id) ||
259 	    bch2_snapshot_is_leaf(trans->c, pos.snapshot) > 0)
260 		return 0;
261 
262 	return __bch2_key_has_snapshot_overwrites(trans, id, pos);
263 }
264 
265 int __bch2_delete_dead_snapshots(struct bch_fs *);
266 int bch2_delete_dead_snapshots(struct bch_fs *);
267 void bch2_delete_dead_snapshots_work(struct work_struct *);
268 void bch2_delete_dead_snapshots_async(struct bch_fs *);
269 void bch2_snapshot_delete_status_to_text(struct printbuf *, struct bch_fs *);
270 
271 int bch2_snapshots_read(struct bch_fs *);
272 void bch2_fs_snapshots_exit(struct bch_fs *);
273 void bch2_fs_snapshots_init_early(struct bch_fs *);
274 
275 #endif /* _BCACHEFS_SNAPSHOT_H */
276