xref: /linux/fs/bcachefs/snapshot.c (revision 42d37fc0c819b81f6f6afd108b55d04ba9d32d0f)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "bcachefs.h"
4 #include "bkey_buf.h"
5 #include "btree_key_cache.h"
6 #include "btree_update.h"
7 #include "buckets.h"
8 #include "errcode.h"
9 #include "error.h"
10 #include "fs.h"
11 #include "recovery_passes.h"
12 #include "snapshot.h"
13 
14 #include <linux/random.h>
15 
16 /*
17  * Snapshot trees:
18  *
19  * Keys in BTREE_ID_snapshot_trees identify a whole tree of snapshot nodes; they
20  * exist to provide a stable identifier for the whole lifetime of a snapshot
21  * tree.
22  */
23 
24 void bch2_snapshot_tree_to_text(struct printbuf *out, struct bch_fs *c,
25 				struct bkey_s_c k)
26 {
27 	struct bkey_s_c_snapshot_tree t = bkey_s_c_to_snapshot_tree(k);
28 
29 	prt_printf(out, "subvol %u root snapshot %u",
30 		   le32_to_cpu(t.v->master_subvol),
31 		   le32_to_cpu(t.v->root_snapshot));
32 }
33 
34 int bch2_snapshot_tree_invalid(struct bch_fs *c, struct bkey_s_c k,
35 			       enum bch_validate_flags flags,
36 			       struct printbuf *err)
37 {
38 	int ret = 0;
39 
40 	bkey_fsck_err_on(bkey_gt(k.k->p, POS(0, U32_MAX)) ||
41 			 bkey_lt(k.k->p, POS(0, 1)), c, err,
42 			 snapshot_tree_pos_bad,
43 			 "bad pos");
44 fsck_err:
45 	return ret;
46 }
47 
48 int bch2_snapshot_tree_lookup(struct btree_trans *trans, u32 id,
49 			      struct bch_snapshot_tree *s)
50 {
51 	int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshot_trees, POS(0, id),
52 					  BTREE_ITER_with_updates, snapshot_tree, s);
53 
54 	if (bch2_err_matches(ret, ENOENT))
55 		ret = -BCH_ERR_ENOENT_snapshot_tree;
56 	return ret;
57 }
58 
59 struct bkey_i_snapshot_tree *
60 __bch2_snapshot_tree_create(struct btree_trans *trans)
61 {
62 	struct btree_iter iter;
63 	int ret = bch2_bkey_get_empty_slot(trans, &iter,
64 			BTREE_ID_snapshot_trees, POS(0, U32_MAX));
65 	struct bkey_i_snapshot_tree *s_t;
66 
67 	if (ret == -BCH_ERR_ENOSPC_btree_slot)
68 		ret = -BCH_ERR_ENOSPC_snapshot_tree;
69 	if (ret)
70 		return ERR_PTR(ret);
71 
72 	s_t = bch2_bkey_alloc(trans, &iter, 0, snapshot_tree);
73 	ret = PTR_ERR_OR_ZERO(s_t);
74 	bch2_trans_iter_exit(trans, &iter);
75 	return ret ? ERR_PTR(ret) : s_t;
76 }
77 
78 static int bch2_snapshot_tree_create(struct btree_trans *trans,
79 				u32 root_id, u32 subvol_id, u32 *tree_id)
80 {
81 	struct bkey_i_snapshot_tree *n_tree =
82 		__bch2_snapshot_tree_create(trans);
83 
84 	if (IS_ERR(n_tree))
85 		return PTR_ERR(n_tree);
86 
87 	n_tree->v.master_subvol	= cpu_to_le32(subvol_id);
88 	n_tree->v.root_snapshot	= cpu_to_le32(root_id);
89 	*tree_id = n_tree->k.p.offset;
90 	return 0;
91 }
92 
93 /* Snapshot nodes: */
94 
95 static bool __bch2_snapshot_is_ancestor_early(struct snapshot_table *t, u32 id, u32 ancestor)
96 {
97 	while (id && id < ancestor) {
98 		const struct snapshot_t *s = __snapshot_t(t, id);
99 		id = s ? s->parent : 0;
100 	}
101 	return id == ancestor;
102 }
103 
104 static bool bch2_snapshot_is_ancestor_early(struct bch_fs *c, u32 id, u32 ancestor)
105 {
106 	rcu_read_lock();
107 	bool ret = __bch2_snapshot_is_ancestor_early(rcu_dereference(c->snapshots), id, ancestor);
108 	rcu_read_unlock();
109 
110 	return ret;
111 }
112 
113 static inline u32 get_ancestor_below(struct snapshot_table *t, u32 id, u32 ancestor)
114 {
115 	const struct snapshot_t *s = __snapshot_t(t, id);
116 	if (!s)
117 		return 0;
118 
119 	if (s->skip[2] <= ancestor)
120 		return s->skip[2];
121 	if (s->skip[1] <= ancestor)
122 		return s->skip[1];
123 	if (s->skip[0] <= ancestor)
124 		return s->skip[0];
125 	return s->parent;
126 }
127 
128 static bool test_ancestor_bitmap(struct snapshot_table *t, u32 id, u32 ancestor)
129 {
130 	const struct snapshot_t *s = __snapshot_t(t, id);
131 	if (!s)
132 		return false;
133 
134 	return test_bit(ancestor - id - 1, s->is_ancestor);
135 }
136 
137 bool __bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
138 {
139 	bool ret;
140 
141 	rcu_read_lock();
142 	struct snapshot_table *t = rcu_dereference(c->snapshots);
143 
144 	if (unlikely(c->recovery_pass_done < BCH_RECOVERY_PASS_check_snapshots)) {
145 		ret = __bch2_snapshot_is_ancestor_early(t, id, ancestor);
146 		goto out;
147 	}
148 
149 	while (id && id < ancestor - IS_ANCESTOR_BITMAP)
150 		id = get_ancestor_below(t, id, ancestor);
151 
152 	ret = id && id < ancestor
153 		? test_ancestor_bitmap(t, id, ancestor)
154 		: id == ancestor;
155 
156 	EBUG_ON(ret != __bch2_snapshot_is_ancestor_early(t, id, ancestor));
157 out:
158 	rcu_read_unlock();
159 
160 	return ret;
161 }
162 
163 static noinline struct snapshot_t *__snapshot_t_mut(struct bch_fs *c, u32 id)
164 {
165 	size_t idx = U32_MAX - id;
166 	struct snapshot_table *new, *old;
167 
168 	size_t new_bytes = kmalloc_size_roundup(struct_size(new, s, idx + 1));
169 	size_t new_size = (new_bytes - sizeof(*new)) / sizeof(new->s[0]);
170 
171 	if (unlikely(new_bytes > INT_MAX))
172 		return NULL;
173 
174 	new = kvzalloc(new_bytes, GFP_KERNEL);
175 	if (!new)
176 		return NULL;
177 
178 	new->nr = new_size;
179 
180 	old = rcu_dereference_protected(c->snapshots, true);
181 	if (old)
182 		memcpy(new->s, old->s, sizeof(old->s[0]) * old->nr);
183 
184 	rcu_assign_pointer(c->snapshots, new);
185 	kvfree_rcu(old, rcu);
186 
187 	return &rcu_dereference_protected(c->snapshots,
188 				lockdep_is_held(&c->snapshot_table_lock))->s[idx];
189 }
190 
191 static inline struct snapshot_t *snapshot_t_mut(struct bch_fs *c, u32 id)
192 {
193 	size_t idx = U32_MAX - id;
194 	struct snapshot_table *table =
195 		rcu_dereference_protected(c->snapshots,
196 				lockdep_is_held(&c->snapshot_table_lock));
197 
198 	lockdep_assert_held(&c->snapshot_table_lock);
199 
200 	if (likely(table && idx < table->nr))
201 		return &table->s[idx];
202 
203 	return __snapshot_t_mut(c, id);
204 }
205 
206 void bch2_snapshot_to_text(struct printbuf *out, struct bch_fs *c,
207 			   struct bkey_s_c k)
208 {
209 	struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(k);
210 
211 	prt_printf(out, "is_subvol %llu deleted %llu parent %10u children %10u %10u subvol %u tree %u",
212 	       BCH_SNAPSHOT_SUBVOL(s.v),
213 	       BCH_SNAPSHOT_DELETED(s.v),
214 	       le32_to_cpu(s.v->parent),
215 	       le32_to_cpu(s.v->children[0]),
216 	       le32_to_cpu(s.v->children[1]),
217 	       le32_to_cpu(s.v->subvol),
218 	       le32_to_cpu(s.v->tree));
219 
220 	if (bkey_val_bytes(k.k) > offsetof(struct bch_snapshot, depth))
221 		prt_printf(out, " depth %u skiplist %u %u %u",
222 			   le32_to_cpu(s.v->depth),
223 			   le32_to_cpu(s.v->skip[0]),
224 			   le32_to_cpu(s.v->skip[1]),
225 			   le32_to_cpu(s.v->skip[2]));
226 }
227 
228 int bch2_snapshot_invalid(struct bch_fs *c, struct bkey_s_c k,
229 			  enum bch_validate_flags flags,
230 			  struct printbuf *err)
231 {
232 	struct bkey_s_c_snapshot s;
233 	u32 i, id;
234 	int ret = 0;
235 
236 	bkey_fsck_err_on(bkey_gt(k.k->p, POS(0, U32_MAX)) ||
237 			 bkey_lt(k.k->p, POS(0, 1)), c, err,
238 			 snapshot_pos_bad,
239 			 "bad pos");
240 
241 	s = bkey_s_c_to_snapshot(k);
242 
243 	id = le32_to_cpu(s.v->parent);
244 	bkey_fsck_err_on(id && id <= k.k->p.offset, c, err,
245 			 snapshot_parent_bad,
246 			 "bad parent node (%u <= %llu)",
247 			 id, k.k->p.offset);
248 
249 	bkey_fsck_err_on(le32_to_cpu(s.v->children[0]) < le32_to_cpu(s.v->children[1]), c, err,
250 			 snapshot_children_not_normalized,
251 			 "children not normalized");
252 
253 	bkey_fsck_err_on(s.v->children[0] && s.v->children[0] == s.v->children[1], c, err,
254 			 snapshot_child_duplicate,
255 			 "duplicate child nodes");
256 
257 	for (i = 0; i < 2; i++) {
258 		id = le32_to_cpu(s.v->children[i]);
259 
260 		bkey_fsck_err_on(id >= k.k->p.offset, c, err,
261 				 snapshot_child_bad,
262 				 "bad child node (%u >= %llu)",
263 				 id, k.k->p.offset);
264 	}
265 
266 	if (bkey_val_bytes(k.k) > offsetof(struct bch_snapshot, skip)) {
267 		bkey_fsck_err_on(le32_to_cpu(s.v->skip[0]) > le32_to_cpu(s.v->skip[1]) ||
268 				 le32_to_cpu(s.v->skip[1]) > le32_to_cpu(s.v->skip[2]), c, err,
269 				 snapshot_skiplist_not_normalized,
270 				 "skiplist not normalized");
271 
272 		for (i = 0; i < ARRAY_SIZE(s.v->skip); i++) {
273 			id = le32_to_cpu(s.v->skip[i]);
274 
275 			bkey_fsck_err_on(id && id < le32_to_cpu(s.v->parent), c, err,
276 					 snapshot_skiplist_bad,
277 					 "bad skiplist node %u", id);
278 		}
279 	}
280 fsck_err:
281 	return ret;
282 }
283 
284 static void __set_is_ancestor_bitmap(struct bch_fs *c, u32 id)
285 {
286 	struct snapshot_t *t = snapshot_t_mut(c, id);
287 	u32 parent = id;
288 
289 	while ((parent = bch2_snapshot_parent_early(c, parent)) &&
290 	       parent - id - 1 < IS_ANCESTOR_BITMAP)
291 		__set_bit(parent - id - 1, t->is_ancestor);
292 }
293 
294 static void set_is_ancestor_bitmap(struct bch_fs *c, u32 id)
295 {
296 	mutex_lock(&c->snapshot_table_lock);
297 	__set_is_ancestor_bitmap(c, id);
298 	mutex_unlock(&c->snapshot_table_lock);
299 }
300 
301 static int __bch2_mark_snapshot(struct btree_trans *trans,
302 		       enum btree_id btree, unsigned level,
303 		       struct bkey_s_c old, struct bkey_s_c new,
304 		       enum btree_iter_update_trigger_flags flags)
305 {
306 	struct bch_fs *c = trans->c;
307 	struct snapshot_t *t;
308 	u32 id = new.k->p.offset;
309 	int ret = 0;
310 
311 	mutex_lock(&c->snapshot_table_lock);
312 
313 	t = snapshot_t_mut(c, id);
314 	if (!t) {
315 		ret = -BCH_ERR_ENOMEM_mark_snapshot;
316 		goto err;
317 	}
318 
319 	if (new.k->type == KEY_TYPE_snapshot) {
320 		struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(new);
321 
322 		t->parent	= le32_to_cpu(s.v->parent);
323 		t->children[0]	= le32_to_cpu(s.v->children[0]);
324 		t->children[1]	= le32_to_cpu(s.v->children[1]);
325 		t->subvol	= BCH_SNAPSHOT_SUBVOL(s.v) ? le32_to_cpu(s.v->subvol) : 0;
326 		t->tree		= le32_to_cpu(s.v->tree);
327 
328 		if (bkey_val_bytes(s.k) > offsetof(struct bch_snapshot, depth)) {
329 			t->depth	= le32_to_cpu(s.v->depth);
330 			t->skip[0]	= le32_to_cpu(s.v->skip[0]);
331 			t->skip[1]	= le32_to_cpu(s.v->skip[1]);
332 			t->skip[2]	= le32_to_cpu(s.v->skip[2]);
333 		} else {
334 			t->depth	= 0;
335 			t->skip[0]	= 0;
336 			t->skip[1]	= 0;
337 			t->skip[2]	= 0;
338 		}
339 
340 		__set_is_ancestor_bitmap(c, id);
341 
342 		if (BCH_SNAPSHOT_DELETED(s.v)) {
343 			set_bit(BCH_FS_need_delete_dead_snapshots, &c->flags);
344 			if (c->curr_recovery_pass > BCH_RECOVERY_PASS_delete_dead_snapshots)
345 				bch2_delete_dead_snapshots_async(c);
346 		}
347 	} else {
348 		memset(t, 0, sizeof(*t));
349 	}
350 err:
351 	mutex_unlock(&c->snapshot_table_lock);
352 	return ret;
353 }
354 
355 int bch2_mark_snapshot(struct btree_trans *trans,
356 		       enum btree_id btree, unsigned level,
357 		       struct bkey_s_c old, struct bkey_s new,
358 		       enum btree_iter_update_trigger_flags flags)
359 {
360 	return __bch2_mark_snapshot(trans, btree, level, old, new.s_c, flags);
361 }
362 
363 int bch2_snapshot_lookup(struct btree_trans *trans, u32 id,
364 			 struct bch_snapshot *s)
365 {
366 	return bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots, POS(0, id),
367 				       BTREE_ITER_with_updates, snapshot, s);
368 }
369 
370 static int bch2_snapshot_live(struct btree_trans *trans, u32 id)
371 {
372 	struct bch_snapshot v;
373 	int ret;
374 
375 	if (!id)
376 		return 0;
377 
378 	ret = bch2_snapshot_lookup(trans, id, &v);
379 	if (bch2_err_matches(ret, ENOENT))
380 		bch_err(trans->c, "snapshot node %u not found", id);
381 	if (ret)
382 		return ret;
383 
384 	return !BCH_SNAPSHOT_DELETED(&v);
385 }
386 
387 /*
388  * If @k is a snapshot with just one live child, it's part of a linear chain,
389  * which we consider to be an equivalence class: and then after snapshot
390  * deletion cleanup, there should only be a single key at a given position in
391  * this equivalence class.
392  *
393  * This sets the equivalence class of @k to be the child's equivalence class, if
394  * it's part of such a linear chain: this correctly sets equivalence classes on
395  * startup if we run leaf to root (i.e. in natural key order).
396  */
397 static int bch2_snapshot_set_equiv(struct btree_trans *trans, struct bkey_s_c k)
398 {
399 	struct bch_fs *c = trans->c;
400 	unsigned i, nr_live = 0, live_idx = 0;
401 	struct bkey_s_c_snapshot snap;
402 	u32 id = k.k->p.offset, child[2];
403 
404 	if (k.k->type != KEY_TYPE_snapshot)
405 		return 0;
406 
407 	snap = bkey_s_c_to_snapshot(k);
408 
409 	child[0] = le32_to_cpu(snap.v->children[0]);
410 	child[1] = le32_to_cpu(snap.v->children[1]);
411 
412 	for (i = 0; i < 2; i++) {
413 		int ret = bch2_snapshot_live(trans, child[i]);
414 
415 		if (ret < 0)
416 			return ret;
417 
418 		if (ret)
419 			live_idx = i;
420 		nr_live += ret;
421 	}
422 
423 	mutex_lock(&c->snapshot_table_lock);
424 
425 	snapshot_t_mut(c, id)->equiv = nr_live == 1
426 		? snapshot_t_mut(c, child[live_idx])->equiv
427 		: id;
428 
429 	mutex_unlock(&c->snapshot_table_lock);
430 
431 	return 0;
432 }
433 
434 /* fsck: */
435 
436 static u32 bch2_snapshot_child(struct bch_fs *c, u32 id, unsigned child)
437 {
438 	return snapshot_t(c, id)->children[child];
439 }
440 
441 static u32 bch2_snapshot_left_child(struct bch_fs *c, u32 id)
442 {
443 	return bch2_snapshot_child(c, id, 0);
444 }
445 
446 static u32 bch2_snapshot_right_child(struct bch_fs *c, u32 id)
447 {
448 	return bch2_snapshot_child(c, id, 1);
449 }
450 
451 static u32 bch2_snapshot_tree_next(struct bch_fs *c, u32 id)
452 {
453 	u32 n, parent;
454 
455 	n = bch2_snapshot_left_child(c, id);
456 	if (n)
457 		return n;
458 
459 	while ((parent = bch2_snapshot_parent(c, id))) {
460 		n = bch2_snapshot_right_child(c, parent);
461 		if (n && n != id)
462 			return n;
463 		id = parent;
464 	}
465 
466 	return 0;
467 }
468 
469 static u32 bch2_snapshot_tree_oldest_subvol(struct bch_fs *c, u32 snapshot_root)
470 {
471 	u32 id = snapshot_root;
472 	u32 subvol = 0, s;
473 
474 	while (id) {
475 		s = snapshot_t(c, id)->subvol;
476 
477 		if (s && (!subvol || s < subvol))
478 			subvol = s;
479 
480 		id = bch2_snapshot_tree_next(c, id);
481 	}
482 
483 	return subvol;
484 }
485 
486 static int bch2_snapshot_tree_master_subvol(struct btree_trans *trans,
487 					    u32 snapshot_root, u32 *subvol_id)
488 {
489 	struct bch_fs *c = trans->c;
490 	struct btree_iter iter;
491 	struct bkey_s_c k;
492 	bool found = false;
493 	int ret;
494 
495 	for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN,
496 				     0, k, ret) {
497 		if (k.k->type != KEY_TYPE_subvolume)
498 			continue;
499 
500 		struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
501 		if (!bch2_snapshot_is_ancestor(c, le32_to_cpu(s.v->snapshot), snapshot_root))
502 			continue;
503 		if (!BCH_SUBVOLUME_SNAP(s.v)) {
504 			*subvol_id = s.k->p.offset;
505 			found = true;
506 			break;
507 		}
508 	}
509 
510 	bch2_trans_iter_exit(trans, &iter);
511 
512 	if (!ret && !found) {
513 		struct bkey_i_subvolume *u;
514 
515 		*subvol_id = bch2_snapshot_tree_oldest_subvol(c, snapshot_root);
516 
517 		u = bch2_bkey_get_mut_typed(trans, &iter,
518 					    BTREE_ID_subvolumes, POS(0, *subvol_id),
519 					    0, subvolume);
520 		ret = PTR_ERR_OR_ZERO(u);
521 		if (ret)
522 			return ret;
523 
524 		SET_BCH_SUBVOLUME_SNAP(&u->v, false);
525 	}
526 
527 	return ret;
528 }
529 
530 static int check_snapshot_tree(struct btree_trans *trans,
531 			       struct btree_iter *iter,
532 			       struct bkey_s_c k)
533 {
534 	struct bch_fs *c = trans->c;
535 	struct bkey_s_c_snapshot_tree st;
536 	struct bch_snapshot s;
537 	struct bch_subvolume subvol;
538 	struct printbuf buf = PRINTBUF;
539 	u32 root_id;
540 	int ret;
541 
542 	if (k.k->type != KEY_TYPE_snapshot_tree)
543 		return 0;
544 
545 	st = bkey_s_c_to_snapshot_tree(k);
546 	root_id = le32_to_cpu(st.v->root_snapshot);
547 
548 	ret = bch2_snapshot_lookup(trans, root_id, &s);
549 	if (ret && !bch2_err_matches(ret, ENOENT))
550 		goto err;
551 
552 	if (fsck_err_on(ret ||
553 			root_id != bch2_snapshot_root(c, root_id) ||
554 			st.k->p.offset != le32_to_cpu(s.tree),
555 			trans, snapshot_tree_to_missing_snapshot,
556 			"snapshot tree points to missing/incorrect snapshot:\n  %s",
557 			(bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
558 		ret = bch2_btree_delete_at(trans, iter, 0);
559 		goto err;
560 	}
561 
562 	ret = bch2_subvolume_get(trans, le32_to_cpu(st.v->master_subvol),
563 				 false, 0, &subvol);
564 	if (ret && !bch2_err_matches(ret, ENOENT))
565 		goto err;
566 
567 	if (fsck_err_on(ret,
568 			trans, snapshot_tree_to_missing_subvol,
569 			"snapshot tree points to missing subvolume:\n  %s",
570 			(printbuf_reset(&buf),
571 			 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
572 	    fsck_err_on(!bch2_snapshot_is_ancestor(c,
573 						le32_to_cpu(subvol.snapshot),
574 						root_id),
575 			trans, snapshot_tree_to_wrong_subvol,
576 			"snapshot tree points to subvolume that does not point to snapshot in this tree:\n  %s",
577 			(printbuf_reset(&buf),
578 			 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
579 	    fsck_err_on(BCH_SUBVOLUME_SNAP(&subvol),
580 			trans, snapshot_tree_to_snapshot_subvol,
581 			"snapshot tree points to snapshot subvolume:\n  %s",
582 			(printbuf_reset(&buf),
583 			 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
584 		struct bkey_i_snapshot_tree *u;
585 		u32 subvol_id;
586 
587 		ret = bch2_snapshot_tree_master_subvol(trans, root_id, &subvol_id);
588 		bch_err_fn(c, ret);
589 
590 		if (bch2_err_matches(ret, ENOENT)) { /* nothing to be done here */
591 			ret = 0;
592 			goto err;
593 		}
594 
595 		if (ret)
596 			goto err;
597 
598 		u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot_tree);
599 		ret = PTR_ERR_OR_ZERO(u);
600 		if (ret)
601 			goto err;
602 
603 		u->v.master_subvol = cpu_to_le32(subvol_id);
604 		st = snapshot_tree_i_to_s_c(u);
605 	}
606 err:
607 fsck_err:
608 	printbuf_exit(&buf);
609 	return ret;
610 }
611 
612 /*
613  * For each snapshot_tree, make sure it points to the root of a snapshot tree
614  * and that snapshot entry points back to it, or delete it.
615  *
616  * And, make sure it points to a subvolume within that snapshot tree, or correct
617  * it to point to the oldest subvolume within that snapshot tree.
618  */
619 int bch2_check_snapshot_trees(struct bch_fs *c)
620 {
621 	int ret = bch2_trans_run(c,
622 		for_each_btree_key_commit(trans, iter,
623 			BTREE_ID_snapshot_trees, POS_MIN,
624 			BTREE_ITER_prefetch, k,
625 			NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
626 		check_snapshot_tree(trans, &iter, k)));
627 	bch_err_fn(c, ret);
628 	return ret;
629 }
630 
631 /*
632  * Look up snapshot tree for @tree_id and find root,
633  * make sure @snap_id is a descendent:
634  */
635 static int snapshot_tree_ptr_good(struct btree_trans *trans,
636 				  u32 snap_id, u32 tree_id)
637 {
638 	struct bch_snapshot_tree s_t;
639 	int ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
640 
641 	if (bch2_err_matches(ret, ENOENT))
642 		return 0;
643 	if (ret)
644 		return ret;
645 
646 	return bch2_snapshot_is_ancestor_early(trans->c, snap_id, le32_to_cpu(s_t.root_snapshot));
647 }
648 
649 u32 bch2_snapshot_skiplist_get(struct bch_fs *c, u32 id)
650 {
651 	const struct snapshot_t *s;
652 
653 	if (!id)
654 		return 0;
655 
656 	rcu_read_lock();
657 	s = snapshot_t(c, id);
658 	if (s->parent)
659 		id = bch2_snapshot_nth_parent(c, id, get_random_u32_below(s->depth));
660 	rcu_read_unlock();
661 
662 	return id;
663 }
664 
665 static int snapshot_skiplist_good(struct btree_trans *trans, u32 id, struct bch_snapshot s)
666 {
667 	unsigned i;
668 
669 	for (i = 0; i < 3; i++)
670 		if (!s.parent) {
671 			if (s.skip[i])
672 				return false;
673 		} else {
674 			if (!bch2_snapshot_is_ancestor_early(trans->c, id, le32_to_cpu(s.skip[i])))
675 				return false;
676 		}
677 
678 	return true;
679 }
680 
681 /*
682  * snapshot_tree pointer was incorrect: look up root snapshot node, make sure
683  * its snapshot_tree pointer is correct (allocate new one if necessary), then
684  * update this node's pointer to root node's pointer:
685  */
686 static int snapshot_tree_ptr_repair(struct btree_trans *trans,
687 				    struct btree_iter *iter,
688 				    struct bkey_s_c k,
689 				    struct bch_snapshot *s)
690 {
691 	struct bch_fs *c = trans->c;
692 	struct btree_iter root_iter;
693 	struct bch_snapshot_tree s_t;
694 	struct bkey_s_c_snapshot root;
695 	struct bkey_i_snapshot *u;
696 	u32 root_id = bch2_snapshot_root(c, k.k->p.offset), tree_id;
697 	int ret;
698 
699 	root = bch2_bkey_get_iter_typed(trans, &root_iter,
700 			       BTREE_ID_snapshots, POS(0, root_id),
701 			       BTREE_ITER_with_updates, snapshot);
702 	ret = bkey_err(root);
703 	if (ret)
704 		goto err;
705 
706 	tree_id = le32_to_cpu(root.v->tree);
707 
708 	ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
709 	if (ret && !bch2_err_matches(ret, ENOENT))
710 		return ret;
711 
712 	if (ret || le32_to_cpu(s_t.root_snapshot) != root_id) {
713 		u = bch2_bkey_make_mut_typed(trans, &root_iter, &root.s_c, 0, snapshot);
714 		ret =   PTR_ERR_OR_ZERO(u) ?:
715 			bch2_snapshot_tree_create(trans, root_id,
716 				bch2_snapshot_tree_oldest_subvol(c, root_id),
717 				&tree_id);
718 		if (ret)
719 			goto err;
720 
721 		u->v.tree = cpu_to_le32(tree_id);
722 		if (k.k->p.offset == root_id)
723 			*s = u->v;
724 	}
725 
726 	if (k.k->p.offset != root_id) {
727 		u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
728 		ret = PTR_ERR_OR_ZERO(u);
729 		if (ret)
730 			goto err;
731 
732 		u->v.tree = cpu_to_le32(tree_id);
733 		*s = u->v;
734 	}
735 err:
736 	bch2_trans_iter_exit(trans, &root_iter);
737 	return ret;
738 }
739 
740 static int check_snapshot(struct btree_trans *trans,
741 			  struct btree_iter *iter,
742 			  struct bkey_s_c k)
743 {
744 	struct bch_fs *c = trans->c;
745 	struct bch_snapshot s;
746 	struct bch_subvolume subvol;
747 	struct bch_snapshot v;
748 	struct bkey_i_snapshot *u;
749 	u32 parent_id = bch2_snapshot_parent_early(c, k.k->p.offset);
750 	u32 real_depth;
751 	struct printbuf buf = PRINTBUF;
752 	u32 i, id;
753 	int ret = 0;
754 
755 	if (k.k->type != KEY_TYPE_snapshot)
756 		return 0;
757 
758 	memset(&s, 0, sizeof(s));
759 	memcpy(&s, k.v, min(sizeof(s), bkey_val_bytes(k.k)));
760 
761 	id = le32_to_cpu(s.parent);
762 	if (id) {
763 		ret = bch2_snapshot_lookup(trans, id, &v);
764 		if (bch2_err_matches(ret, ENOENT))
765 			bch_err(c, "snapshot with nonexistent parent:\n  %s",
766 				(bch2_bkey_val_to_text(&buf, c, k), buf.buf));
767 		if (ret)
768 			goto err;
769 
770 		if (le32_to_cpu(v.children[0]) != k.k->p.offset &&
771 		    le32_to_cpu(v.children[1]) != k.k->p.offset) {
772 			bch_err(c, "snapshot parent %u missing pointer to child %llu",
773 				id, k.k->p.offset);
774 			ret = -EINVAL;
775 			goto err;
776 		}
777 	}
778 
779 	for (i = 0; i < 2 && s.children[i]; i++) {
780 		id = le32_to_cpu(s.children[i]);
781 
782 		ret = bch2_snapshot_lookup(trans, id, &v);
783 		if (bch2_err_matches(ret, ENOENT))
784 			bch_err(c, "snapshot node %llu has nonexistent child %u",
785 				k.k->p.offset, id);
786 		if (ret)
787 			goto err;
788 
789 		if (le32_to_cpu(v.parent) != k.k->p.offset) {
790 			bch_err(c, "snapshot child %u has wrong parent (got %u should be %llu)",
791 				id, le32_to_cpu(v.parent), k.k->p.offset);
792 			ret = -EINVAL;
793 			goto err;
794 		}
795 	}
796 
797 	bool should_have_subvol = BCH_SNAPSHOT_SUBVOL(&s) &&
798 		!BCH_SNAPSHOT_DELETED(&s);
799 
800 	if (should_have_subvol) {
801 		id = le32_to_cpu(s.subvol);
802 		ret = bch2_subvolume_get(trans, id, 0, false, &subvol);
803 		if (bch2_err_matches(ret, ENOENT))
804 			bch_err(c, "snapshot points to nonexistent subvolume:\n  %s",
805 				(bch2_bkey_val_to_text(&buf, c, k), buf.buf));
806 		if (ret)
807 			goto err;
808 
809 		if (BCH_SNAPSHOT_SUBVOL(&s) != (le32_to_cpu(subvol.snapshot) == k.k->p.offset)) {
810 			bch_err(c, "snapshot node %llu has wrong BCH_SNAPSHOT_SUBVOL",
811 				k.k->p.offset);
812 			ret = -EINVAL;
813 			goto err;
814 		}
815 	} else {
816 		if (fsck_err_on(s.subvol,
817 				trans, snapshot_should_not_have_subvol,
818 				"snapshot should not point to subvol:\n  %s",
819 				(bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
820 			u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
821 			ret = PTR_ERR_OR_ZERO(u);
822 			if (ret)
823 				goto err;
824 
825 			u->v.subvol = 0;
826 			s = u->v;
827 		}
828 	}
829 
830 	ret = snapshot_tree_ptr_good(trans, k.k->p.offset, le32_to_cpu(s.tree));
831 	if (ret < 0)
832 		goto err;
833 
834 	if (fsck_err_on(!ret,
835 			trans, snapshot_to_bad_snapshot_tree,
836 			"snapshot points to missing/incorrect tree:\n  %s",
837 			(bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
838 		ret = snapshot_tree_ptr_repair(trans, iter, k, &s);
839 		if (ret)
840 			goto err;
841 	}
842 	ret = 0;
843 
844 	real_depth = bch2_snapshot_depth(c, parent_id);
845 
846 	if (fsck_err_on(le32_to_cpu(s.depth) != real_depth,
847 			trans, snapshot_bad_depth,
848 			"snapshot with incorrect depth field, should be %u:\n  %s",
849 			real_depth, (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
850 		u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
851 		ret = PTR_ERR_OR_ZERO(u);
852 		if (ret)
853 			goto err;
854 
855 		u->v.depth = cpu_to_le32(real_depth);
856 		s = u->v;
857 	}
858 
859 	ret = snapshot_skiplist_good(trans, k.k->p.offset, s);
860 	if (ret < 0)
861 		goto err;
862 
863 	if (fsck_err_on(!ret,
864 			trans, snapshot_bad_skiplist,
865 			"snapshot with bad skiplist field:\n  %s",
866 			(bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
867 		u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
868 		ret = PTR_ERR_OR_ZERO(u);
869 		if (ret)
870 			goto err;
871 
872 		for (i = 0; i < ARRAY_SIZE(u->v.skip); i++)
873 			u->v.skip[i] = cpu_to_le32(bch2_snapshot_skiplist_get(c, parent_id));
874 
875 		bubble_sort(u->v.skip, ARRAY_SIZE(u->v.skip), cmp_le32);
876 		s = u->v;
877 	}
878 	ret = 0;
879 err:
880 fsck_err:
881 	printbuf_exit(&buf);
882 	return ret;
883 }
884 
885 int bch2_check_snapshots(struct bch_fs *c)
886 {
887 	/*
888 	 * We iterate backwards as checking/fixing the depth field requires that
889 	 * the parent's depth already be correct:
890 	 */
891 	int ret = bch2_trans_run(c,
892 		for_each_btree_key_reverse_commit(trans, iter,
893 				BTREE_ID_snapshots, POS_MAX,
894 				BTREE_ITER_prefetch, k,
895 				NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
896 			check_snapshot(trans, &iter, k)));
897 	bch_err_fn(c, ret);
898 	return ret;
899 }
900 
901 static int check_snapshot_exists(struct btree_trans *trans, u32 id)
902 {
903 	struct bch_fs *c = trans->c;
904 
905 	if (bch2_snapshot_equiv(c, id))
906 		return 0;
907 
908 	/* 0 is an invalid tree ID */
909 	u32 tree_id = 0;
910 	int ret = bch2_snapshot_tree_create(trans, id, 0, &tree_id);
911 	if (ret)
912 		return ret;
913 
914 	struct bkey_i_snapshot *snapshot = bch2_trans_kmalloc(trans, sizeof(*snapshot));
915 	ret = PTR_ERR_OR_ZERO(snapshot);
916 	if (ret)
917 		return ret;
918 
919 	bkey_snapshot_init(&snapshot->k_i);
920 	snapshot->k.p		= POS(0, id);
921 	snapshot->v.tree	= cpu_to_le32(tree_id);
922 	snapshot->v.btime.lo	= cpu_to_le64(bch2_current_time(c));
923 
924 	return  bch2_btree_insert_trans(trans, BTREE_ID_snapshots, &snapshot->k_i, 0) ?:
925 		bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0,
926 				   bkey_s_c_null, bkey_i_to_s(&snapshot->k_i), 0) ?:
927 		bch2_snapshot_set_equiv(trans, bkey_i_to_s_c(&snapshot->k_i));
928 }
929 
930 /* Figure out which snapshot nodes belong in the same tree: */
931 struct snapshot_tree_reconstruct {
932 	enum btree_id			btree;
933 	struct bpos			cur_pos;
934 	snapshot_id_list		cur_ids;
935 	DARRAY(snapshot_id_list)	trees;
936 };
937 
938 static void snapshot_tree_reconstruct_exit(struct snapshot_tree_reconstruct *r)
939 {
940 	darray_for_each(r->trees, i)
941 		darray_exit(i);
942 	darray_exit(&r->trees);
943 	darray_exit(&r->cur_ids);
944 }
945 
946 static inline bool same_snapshot(struct snapshot_tree_reconstruct *r, struct bpos pos)
947 {
948 	return r->btree == BTREE_ID_inodes
949 		? r->cur_pos.offset == pos.offset
950 		: r->cur_pos.inode == pos.inode;
951 }
952 
953 static inline bool snapshot_id_lists_have_common(snapshot_id_list *l, snapshot_id_list *r)
954 {
955 	darray_for_each(*l, i)
956 		if (snapshot_list_has_id(r, *i))
957 			return true;
958 	return false;
959 }
960 
961 static void snapshot_id_list_to_text(struct printbuf *out, snapshot_id_list *s)
962 {
963 	bool first = true;
964 	darray_for_each(*s, i) {
965 		if (!first)
966 			prt_char(out, ' ');
967 		first = false;
968 		prt_printf(out, "%u", *i);
969 	}
970 }
971 
972 static int snapshot_tree_reconstruct_next(struct bch_fs *c, struct snapshot_tree_reconstruct *r)
973 {
974 	if (r->cur_ids.nr) {
975 		darray_for_each(r->trees, i)
976 			if (snapshot_id_lists_have_common(i, &r->cur_ids)) {
977 				int ret = snapshot_list_merge(c, i, &r->cur_ids);
978 				if (ret)
979 					return ret;
980 				goto out;
981 			}
982 		darray_push(&r->trees, r->cur_ids);
983 		darray_init(&r->cur_ids);
984 	}
985 out:
986 	r->cur_ids.nr = 0;
987 	return 0;
988 }
989 
990 static int get_snapshot_trees(struct bch_fs *c, struct snapshot_tree_reconstruct *r, struct bpos pos)
991 {
992 	if (!same_snapshot(r, pos))
993 		snapshot_tree_reconstruct_next(c, r);
994 	r->cur_pos = pos;
995 	return snapshot_list_add_nodup(c, &r->cur_ids, pos.snapshot);
996 }
997 
998 int bch2_reconstruct_snapshots(struct bch_fs *c)
999 {
1000 	struct btree_trans *trans = bch2_trans_get(c);
1001 	struct printbuf buf = PRINTBUF;
1002 	struct snapshot_tree_reconstruct r = {};
1003 	int ret = 0;
1004 
1005 	for (unsigned btree = 0; btree < BTREE_ID_NR; btree++) {
1006 		if (btree_type_has_snapshots(btree)) {
1007 			r.btree = btree;
1008 
1009 			ret = for_each_btree_key(trans, iter, btree, POS_MIN,
1010 					BTREE_ITER_all_snapshots|BTREE_ITER_prefetch, k, ({
1011 				get_snapshot_trees(c, &r, k.k->p);
1012 			}));
1013 			if (ret)
1014 				goto err;
1015 
1016 			snapshot_tree_reconstruct_next(c, &r);
1017 		}
1018 	}
1019 
1020 	darray_for_each(r.trees, t) {
1021 		printbuf_reset(&buf);
1022 		snapshot_id_list_to_text(&buf, t);
1023 
1024 		darray_for_each(*t, id) {
1025 			if (fsck_err_on(!bch2_snapshot_equiv(c, *id),
1026 					trans, snapshot_node_missing,
1027 					"snapshot node %u from tree %s missing, recreate?", *id, buf.buf)) {
1028 				if (t->nr > 1) {
1029 					bch_err(c, "cannot reconstruct snapshot trees with multiple nodes");
1030 					ret = -BCH_ERR_fsck_repair_unimplemented;
1031 					goto err;
1032 				}
1033 
1034 				ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1035 						check_snapshot_exists(trans, *id));
1036 				if (ret)
1037 					goto err;
1038 			}
1039 		}
1040 	}
1041 fsck_err:
1042 err:
1043 	bch2_trans_put(trans);
1044 	snapshot_tree_reconstruct_exit(&r);
1045 	printbuf_exit(&buf);
1046 	bch_err_fn(c, ret);
1047 	return ret;
1048 }
1049 
1050 int bch2_check_key_has_snapshot(struct btree_trans *trans,
1051 				struct btree_iter *iter,
1052 				struct bkey_s_c k)
1053 {
1054 	struct bch_fs *c = trans->c;
1055 	struct printbuf buf = PRINTBUF;
1056 	int ret = 0;
1057 
1058 	if (fsck_err_on(!bch2_snapshot_equiv(c, k.k->p.snapshot),
1059 			trans, bkey_in_missing_snapshot,
1060 			"key in missing snapshot %s, delete?",
1061 			(bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1062 		ret = bch2_btree_delete_at(trans, iter,
1063 					    BTREE_UPDATE_internal_snapshot_node) ?: 1;
1064 fsck_err:
1065 	printbuf_exit(&buf);
1066 	return ret;
1067 }
1068 
1069 /*
1070  * Mark a snapshot as deleted, for future cleanup:
1071  */
1072 int bch2_snapshot_node_set_deleted(struct btree_trans *trans, u32 id)
1073 {
1074 	struct btree_iter iter;
1075 	struct bkey_i_snapshot *s;
1076 	int ret = 0;
1077 
1078 	s = bch2_bkey_get_mut_typed(trans, &iter,
1079 				    BTREE_ID_snapshots, POS(0, id),
1080 				    0, snapshot);
1081 	ret = PTR_ERR_OR_ZERO(s);
1082 	if (unlikely(ret)) {
1083 		bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT),
1084 					trans->c, "missing snapshot %u", id);
1085 		return ret;
1086 	}
1087 
1088 	/* already deleted? */
1089 	if (BCH_SNAPSHOT_DELETED(&s->v))
1090 		goto err;
1091 
1092 	SET_BCH_SNAPSHOT_DELETED(&s->v, true);
1093 	SET_BCH_SNAPSHOT_SUBVOL(&s->v, false);
1094 	s->v.subvol = 0;
1095 err:
1096 	bch2_trans_iter_exit(trans, &iter);
1097 	return ret;
1098 }
1099 
1100 static inline void normalize_snapshot_child_pointers(struct bch_snapshot *s)
1101 {
1102 	if (le32_to_cpu(s->children[0]) < le32_to_cpu(s->children[1]))
1103 		swap(s->children[0], s->children[1]);
1104 }
1105 
1106 static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
1107 {
1108 	struct bch_fs *c = trans->c;
1109 	struct btree_iter iter, p_iter = (struct btree_iter) { NULL };
1110 	struct btree_iter c_iter = (struct btree_iter) { NULL };
1111 	struct btree_iter tree_iter = (struct btree_iter) { NULL };
1112 	struct bkey_s_c_snapshot s;
1113 	u32 parent_id, child_id;
1114 	unsigned i;
1115 	int ret = 0;
1116 
1117 	s = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_snapshots, POS(0, id),
1118 				     BTREE_ITER_intent, snapshot);
1119 	ret = bkey_err(s);
1120 	bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
1121 				"missing snapshot %u", id);
1122 
1123 	if (ret)
1124 		goto err;
1125 
1126 	BUG_ON(s.v->children[1]);
1127 
1128 	parent_id = le32_to_cpu(s.v->parent);
1129 	child_id = le32_to_cpu(s.v->children[0]);
1130 
1131 	if (parent_id) {
1132 		struct bkey_i_snapshot *parent;
1133 
1134 		parent = bch2_bkey_get_mut_typed(trans, &p_iter,
1135 				     BTREE_ID_snapshots, POS(0, parent_id),
1136 				     0, snapshot);
1137 		ret = PTR_ERR_OR_ZERO(parent);
1138 		bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
1139 					"missing snapshot %u", parent_id);
1140 		if (unlikely(ret))
1141 			goto err;
1142 
1143 		/* find entry in parent->children for node being deleted */
1144 		for (i = 0; i < 2; i++)
1145 			if (le32_to_cpu(parent->v.children[i]) == id)
1146 				break;
1147 
1148 		if (bch2_fs_inconsistent_on(i == 2, c,
1149 					"snapshot %u missing child pointer to %u",
1150 					parent_id, id))
1151 			goto err;
1152 
1153 		parent->v.children[i] = cpu_to_le32(child_id);
1154 
1155 		normalize_snapshot_child_pointers(&parent->v);
1156 	}
1157 
1158 	if (child_id) {
1159 		struct bkey_i_snapshot *child;
1160 
1161 		child = bch2_bkey_get_mut_typed(trans, &c_iter,
1162 				     BTREE_ID_snapshots, POS(0, child_id),
1163 				     0, snapshot);
1164 		ret = PTR_ERR_OR_ZERO(child);
1165 		bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
1166 					"missing snapshot %u", child_id);
1167 		if (unlikely(ret))
1168 			goto err;
1169 
1170 		child->v.parent = cpu_to_le32(parent_id);
1171 
1172 		if (!child->v.parent) {
1173 			child->v.skip[0] = 0;
1174 			child->v.skip[1] = 0;
1175 			child->v.skip[2] = 0;
1176 		}
1177 	}
1178 
1179 	if (!parent_id) {
1180 		/*
1181 		 * We're deleting the root of a snapshot tree: update the
1182 		 * snapshot_tree entry to point to the new root, or delete it if
1183 		 * this is the last snapshot ID in this tree:
1184 		 */
1185 		struct bkey_i_snapshot_tree *s_t;
1186 
1187 		BUG_ON(s.v->children[1]);
1188 
1189 		s_t = bch2_bkey_get_mut_typed(trans, &tree_iter,
1190 				BTREE_ID_snapshot_trees, POS(0, le32_to_cpu(s.v->tree)),
1191 				0, snapshot_tree);
1192 		ret = PTR_ERR_OR_ZERO(s_t);
1193 		if (ret)
1194 			goto err;
1195 
1196 		if (s.v->children[0]) {
1197 			s_t->v.root_snapshot = s.v->children[0];
1198 		} else {
1199 			s_t->k.type = KEY_TYPE_deleted;
1200 			set_bkey_val_u64s(&s_t->k, 0);
1201 		}
1202 	}
1203 
1204 	ret = bch2_btree_delete_at(trans, &iter, 0);
1205 err:
1206 	bch2_trans_iter_exit(trans, &tree_iter);
1207 	bch2_trans_iter_exit(trans, &p_iter);
1208 	bch2_trans_iter_exit(trans, &c_iter);
1209 	bch2_trans_iter_exit(trans, &iter);
1210 	return ret;
1211 }
1212 
1213 static int create_snapids(struct btree_trans *trans, u32 parent, u32 tree,
1214 			  u32 *new_snapids,
1215 			  u32 *snapshot_subvols,
1216 			  unsigned nr_snapids)
1217 {
1218 	struct bch_fs *c = trans->c;
1219 	struct btree_iter iter;
1220 	struct bkey_i_snapshot *n;
1221 	struct bkey_s_c k;
1222 	unsigned i, j;
1223 	u32 depth = bch2_snapshot_depth(c, parent);
1224 	int ret;
1225 
1226 	bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
1227 			     POS_MIN, BTREE_ITER_intent);
1228 	k = bch2_btree_iter_peek(&iter);
1229 	ret = bkey_err(k);
1230 	if (ret)
1231 		goto err;
1232 
1233 	for (i = 0; i < nr_snapids; i++) {
1234 		k = bch2_btree_iter_prev_slot(&iter);
1235 		ret = bkey_err(k);
1236 		if (ret)
1237 			goto err;
1238 
1239 		if (!k.k || !k.k->p.offset) {
1240 			ret = -BCH_ERR_ENOSPC_snapshot_create;
1241 			goto err;
1242 		}
1243 
1244 		n = bch2_bkey_alloc(trans, &iter, 0, snapshot);
1245 		ret = PTR_ERR_OR_ZERO(n);
1246 		if (ret)
1247 			goto err;
1248 
1249 		n->v.flags	= 0;
1250 		n->v.parent	= cpu_to_le32(parent);
1251 		n->v.subvol	= cpu_to_le32(snapshot_subvols[i]);
1252 		n->v.tree	= cpu_to_le32(tree);
1253 		n->v.depth	= cpu_to_le32(depth);
1254 		n->v.btime.lo	= cpu_to_le64(bch2_current_time(c));
1255 		n->v.btime.hi	= 0;
1256 
1257 		for (j = 0; j < ARRAY_SIZE(n->v.skip); j++)
1258 			n->v.skip[j] = cpu_to_le32(bch2_snapshot_skiplist_get(c, parent));
1259 
1260 		bubble_sort(n->v.skip, ARRAY_SIZE(n->v.skip), cmp_le32);
1261 		SET_BCH_SNAPSHOT_SUBVOL(&n->v, true);
1262 
1263 		ret = __bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0,
1264 					 bkey_s_c_null, bkey_i_to_s_c(&n->k_i), 0);
1265 		if (ret)
1266 			goto err;
1267 
1268 		new_snapids[i]	= iter.pos.offset;
1269 
1270 		mutex_lock(&c->snapshot_table_lock);
1271 		snapshot_t_mut(c, new_snapids[i])->equiv = new_snapids[i];
1272 		mutex_unlock(&c->snapshot_table_lock);
1273 	}
1274 err:
1275 	bch2_trans_iter_exit(trans, &iter);
1276 	return ret;
1277 }
1278 
1279 /*
1280  * Create new snapshot IDs as children of an existing snapshot ID:
1281  */
1282 static int bch2_snapshot_node_create_children(struct btree_trans *trans, u32 parent,
1283 			      u32 *new_snapids,
1284 			      u32 *snapshot_subvols,
1285 			      unsigned nr_snapids)
1286 {
1287 	struct btree_iter iter;
1288 	struct bkey_i_snapshot *n_parent;
1289 	int ret = 0;
1290 
1291 	n_parent = bch2_bkey_get_mut_typed(trans, &iter,
1292 			BTREE_ID_snapshots, POS(0, parent),
1293 			0, snapshot);
1294 	ret = PTR_ERR_OR_ZERO(n_parent);
1295 	if (unlikely(ret)) {
1296 		if (bch2_err_matches(ret, ENOENT))
1297 			bch_err(trans->c, "snapshot %u not found", parent);
1298 		return ret;
1299 	}
1300 
1301 	if (n_parent->v.children[0] || n_parent->v.children[1]) {
1302 		bch_err(trans->c, "Trying to add child snapshot nodes to parent that already has children");
1303 		ret = -EINVAL;
1304 		goto err;
1305 	}
1306 
1307 	ret = create_snapids(trans, parent, le32_to_cpu(n_parent->v.tree),
1308 			     new_snapids, snapshot_subvols, nr_snapids);
1309 	if (ret)
1310 		goto err;
1311 
1312 	n_parent->v.children[0] = cpu_to_le32(new_snapids[0]);
1313 	n_parent->v.children[1] = cpu_to_le32(new_snapids[1]);
1314 	n_parent->v.subvol = 0;
1315 	SET_BCH_SNAPSHOT_SUBVOL(&n_parent->v, false);
1316 err:
1317 	bch2_trans_iter_exit(trans, &iter);
1318 	return ret;
1319 }
1320 
1321 /*
1322  * Create a snapshot node that is the root of a new tree:
1323  */
1324 static int bch2_snapshot_node_create_tree(struct btree_trans *trans,
1325 			      u32 *new_snapids,
1326 			      u32 *snapshot_subvols,
1327 			      unsigned nr_snapids)
1328 {
1329 	struct bkey_i_snapshot_tree *n_tree;
1330 	int ret;
1331 
1332 	n_tree = __bch2_snapshot_tree_create(trans);
1333 	ret =   PTR_ERR_OR_ZERO(n_tree) ?:
1334 		create_snapids(trans, 0, n_tree->k.p.offset,
1335 			     new_snapids, snapshot_subvols, nr_snapids);
1336 	if (ret)
1337 		return ret;
1338 
1339 	n_tree->v.master_subvol	= cpu_to_le32(snapshot_subvols[0]);
1340 	n_tree->v.root_snapshot	= cpu_to_le32(new_snapids[0]);
1341 	return 0;
1342 }
1343 
1344 int bch2_snapshot_node_create(struct btree_trans *trans, u32 parent,
1345 			      u32 *new_snapids,
1346 			      u32 *snapshot_subvols,
1347 			      unsigned nr_snapids)
1348 {
1349 	BUG_ON((parent == 0) != (nr_snapids == 1));
1350 	BUG_ON((parent != 0) != (nr_snapids == 2));
1351 
1352 	return parent
1353 		? bch2_snapshot_node_create_children(trans, parent,
1354 				new_snapids, snapshot_subvols, nr_snapids)
1355 		: bch2_snapshot_node_create_tree(trans,
1356 				new_snapids, snapshot_subvols, nr_snapids);
1357 
1358 }
1359 
1360 /*
1361  * If we have an unlinked inode in an internal snapshot node, and the inode
1362  * really has been deleted in all child snapshots, how does this get cleaned up?
1363  *
1364  * first there is the problem of how keys that have been overwritten in all
1365  * child snapshots get deleted (unimplemented?), but inodes may perhaps be
1366  * special?
1367  *
1368  * also: unlinked inode in internal snapshot appears to not be getting deleted
1369  * correctly if inode doesn't exist in leaf snapshots
1370  *
1371  * solution:
1372  *
1373  * for a key in an interior snapshot node that needs work to be done that
1374  * requires it to be mutated: iterate over all descendent leaf nodes and copy
1375  * that key to snapshot leaf nodes, where we can mutate it
1376  */
1377 
1378 static int delete_dead_snapshots_process_key(struct btree_trans *trans,
1379 			       struct btree_iter *iter,
1380 			       struct bkey_s_c k,
1381 			       snapshot_id_list *deleted,
1382 			       snapshot_id_list *equiv_seen,
1383 			       struct bpos *last_pos)
1384 {
1385 	int ret = bch2_check_key_has_snapshot(trans, iter, k);
1386 	if (ret)
1387 		return ret < 0 ? ret : 0;
1388 
1389 	struct bch_fs *c = trans->c;
1390 	u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
1391 	if (!equiv) /* key for invalid snapshot node, but we chose not to delete */
1392 		return 0;
1393 
1394 	if (!bkey_eq(k.k->p, *last_pos))
1395 		equiv_seen->nr = 0;
1396 
1397 	if (snapshot_list_has_id(deleted, k.k->p.snapshot))
1398 		return bch2_btree_delete_at(trans, iter,
1399 					    BTREE_UPDATE_internal_snapshot_node);
1400 
1401 	if (!bpos_eq(*last_pos, k.k->p) &&
1402 	    snapshot_list_has_id(equiv_seen, equiv))
1403 		return bch2_btree_delete_at(trans, iter,
1404 					    BTREE_UPDATE_internal_snapshot_node);
1405 
1406 	*last_pos = k.k->p;
1407 
1408 	ret = snapshot_list_add_nodup(c, equiv_seen, equiv);
1409 	if (ret)
1410 		return ret;
1411 
1412 	/*
1413 	 * When we have a linear chain of snapshot nodes, we consider
1414 	 * those to form an equivalence class: we're going to collapse
1415 	 * them all down to a single node, and keep the leaf-most node -
1416 	 * which has the same id as the equivalence class id.
1417 	 *
1418 	 * If there are multiple keys in different snapshots at the same
1419 	 * position, we're only going to keep the one in the newest
1420 	 * snapshot (we delete the others above) - the rest have been
1421 	 * overwritten and are redundant, and for the key we're going to keep we
1422 	 * need to move it to the equivalance class ID if it's not there
1423 	 * already.
1424 	 */
1425 	if (equiv != k.k->p.snapshot) {
1426 		struct bkey_i *new = bch2_bkey_make_mut_noupdate(trans, k);
1427 		int ret = PTR_ERR_OR_ZERO(new);
1428 		if (ret)
1429 			return ret;
1430 
1431 		new->k.p.snapshot = equiv;
1432 
1433 		struct btree_iter new_iter;
1434 		bch2_trans_iter_init(trans, &new_iter, iter->btree_id, new->k.p,
1435 				     BTREE_ITER_all_snapshots|
1436 				     BTREE_ITER_cached|
1437 				     BTREE_ITER_intent);
1438 
1439 		ret =   bch2_btree_iter_traverse(&new_iter) ?:
1440 			bch2_trans_update(trans, &new_iter, new,
1441 					BTREE_UPDATE_internal_snapshot_node) ?:
1442 			bch2_btree_delete_at(trans, iter,
1443 					BTREE_UPDATE_internal_snapshot_node);
1444 		bch2_trans_iter_exit(trans, &new_iter);
1445 		if (ret)
1446 			return ret;
1447 	}
1448 
1449 	return 0;
1450 }
1451 
1452 static int bch2_snapshot_needs_delete(struct btree_trans *trans, struct bkey_s_c k)
1453 {
1454 	struct bkey_s_c_snapshot snap;
1455 	u32 children[2];
1456 	int ret;
1457 
1458 	if (k.k->type != KEY_TYPE_snapshot)
1459 		return 0;
1460 
1461 	snap = bkey_s_c_to_snapshot(k);
1462 	if (BCH_SNAPSHOT_DELETED(snap.v) ||
1463 	    BCH_SNAPSHOT_SUBVOL(snap.v))
1464 		return 0;
1465 
1466 	children[0] = le32_to_cpu(snap.v->children[0]);
1467 	children[1] = le32_to_cpu(snap.v->children[1]);
1468 
1469 	ret   = bch2_snapshot_live(trans, children[0]) ?:
1470 		bch2_snapshot_live(trans, children[1]);
1471 	if (ret < 0)
1472 		return ret;
1473 	return !ret;
1474 }
1475 
1476 /*
1477  * For a given snapshot, if it doesn't have a subvolume that points to it, and
1478  * it doesn't have child snapshot nodes - it's now redundant and we can mark it
1479  * as deleted.
1480  */
1481 static int bch2_delete_redundant_snapshot(struct btree_trans *trans, struct bkey_s_c k)
1482 {
1483 	int ret = bch2_snapshot_needs_delete(trans, k);
1484 
1485 	return ret <= 0
1486 		? ret
1487 		: bch2_snapshot_node_set_deleted(trans, k.k->p.offset);
1488 }
1489 
1490 static inline u32 bch2_snapshot_nth_parent_skip(struct bch_fs *c, u32 id, u32 n,
1491 						snapshot_id_list *skip)
1492 {
1493 	rcu_read_lock();
1494 	while (snapshot_list_has_id(skip, id))
1495 		id = __bch2_snapshot_parent(c, id);
1496 
1497 	while (n--) {
1498 		do {
1499 			id = __bch2_snapshot_parent(c, id);
1500 		} while (snapshot_list_has_id(skip, id));
1501 	}
1502 	rcu_read_unlock();
1503 
1504 	return id;
1505 }
1506 
1507 static int bch2_fix_child_of_deleted_snapshot(struct btree_trans *trans,
1508 					      struct btree_iter *iter, struct bkey_s_c k,
1509 					      snapshot_id_list *deleted)
1510 {
1511 	struct bch_fs *c = trans->c;
1512 	u32 nr_deleted_ancestors = 0;
1513 	struct bkey_i_snapshot *s;
1514 	int ret;
1515 
1516 	if (k.k->type != KEY_TYPE_snapshot)
1517 		return 0;
1518 
1519 	if (snapshot_list_has_id(deleted, k.k->p.offset))
1520 		return 0;
1521 
1522 	s = bch2_bkey_make_mut_noupdate_typed(trans, k, snapshot);
1523 	ret = PTR_ERR_OR_ZERO(s);
1524 	if (ret)
1525 		return ret;
1526 
1527 	darray_for_each(*deleted, i)
1528 		nr_deleted_ancestors += bch2_snapshot_is_ancestor(c, s->k.p.offset, *i);
1529 
1530 	if (!nr_deleted_ancestors)
1531 		return 0;
1532 
1533 	le32_add_cpu(&s->v.depth, -nr_deleted_ancestors);
1534 
1535 	if (!s->v.depth) {
1536 		s->v.skip[0] = 0;
1537 		s->v.skip[1] = 0;
1538 		s->v.skip[2] = 0;
1539 	} else {
1540 		u32 depth = le32_to_cpu(s->v.depth);
1541 		u32 parent = bch2_snapshot_parent(c, s->k.p.offset);
1542 
1543 		for (unsigned j = 0; j < ARRAY_SIZE(s->v.skip); j++) {
1544 			u32 id = le32_to_cpu(s->v.skip[j]);
1545 
1546 			if (snapshot_list_has_id(deleted, id)) {
1547 				id = bch2_snapshot_nth_parent_skip(c,
1548 							parent,
1549 							depth > 1
1550 							? get_random_u32_below(depth - 1)
1551 							: 0,
1552 							deleted);
1553 				s->v.skip[j] = cpu_to_le32(id);
1554 			}
1555 		}
1556 
1557 		bubble_sort(s->v.skip, ARRAY_SIZE(s->v.skip), cmp_le32);
1558 	}
1559 
1560 	return bch2_trans_update(trans, iter, &s->k_i, 0);
1561 }
1562 
1563 int bch2_delete_dead_snapshots(struct bch_fs *c)
1564 {
1565 	struct btree_trans *trans;
1566 	snapshot_id_list deleted = { 0 };
1567 	snapshot_id_list deleted_interior = { 0 };
1568 	int ret = 0;
1569 
1570 	if (!test_and_clear_bit(BCH_FS_need_delete_dead_snapshots, &c->flags))
1571 		return 0;
1572 
1573 	trans = bch2_trans_get(c);
1574 
1575 	/*
1576 	 * For every snapshot node: If we have no live children and it's not
1577 	 * pointed to by a subvolume, delete it:
1578 	 */
1579 	ret = for_each_btree_key_commit(trans, iter, BTREE_ID_snapshots,
1580 			POS_MIN, 0, k,
1581 			NULL, NULL, 0,
1582 		bch2_delete_redundant_snapshot(trans, k));
1583 	bch_err_msg(c, ret, "deleting redundant snapshots");
1584 	if (ret)
1585 		goto err;
1586 
1587 	ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1588 				 POS_MIN, 0, k,
1589 		bch2_snapshot_set_equiv(trans, k));
1590 	bch_err_msg(c, ret, "in bch2_snapshots_set_equiv");
1591 	if (ret)
1592 		goto err;
1593 
1594 	ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1595 				 POS_MIN, 0, k, ({
1596 		if (k.k->type != KEY_TYPE_snapshot)
1597 			continue;
1598 
1599 		BCH_SNAPSHOT_DELETED(bkey_s_c_to_snapshot(k).v)
1600 			? snapshot_list_add(c, &deleted, k.k->p.offset)
1601 			: 0;
1602 	}));
1603 	bch_err_msg(c, ret, "walking snapshots");
1604 	if (ret)
1605 		goto err;
1606 
1607 	for (unsigned btree = 0; btree < BTREE_ID_NR; btree++) {
1608 		struct bpos last_pos = POS_MIN;
1609 		snapshot_id_list equiv_seen = { 0 };
1610 		struct disk_reservation res = { 0 };
1611 
1612 		if (!btree_type_has_snapshots(btree))
1613 			continue;
1614 
1615 		ret = for_each_btree_key_commit(trans, iter,
1616 				btree, POS_MIN,
1617 				BTREE_ITER_prefetch|BTREE_ITER_all_snapshots, k,
1618 				&res, NULL, BCH_TRANS_COMMIT_no_enospc,
1619 			delete_dead_snapshots_process_key(trans, &iter, k, &deleted,
1620 							  &equiv_seen, &last_pos));
1621 
1622 		bch2_disk_reservation_put(c, &res);
1623 		darray_exit(&equiv_seen);
1624 
1625 		bch_err_msg(c, ret, "deleting keys from dying snapshots");
1626 		if (ret)
1627 			goto err;
1628 	}
1629 
1630 	bch2_trans_unlock(trans);
1631 	down_write(&c->snapshot_create_lock);
1632 
1633 	ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1634 				 POS_MIN, 0, k, ({
1635 		u32 snapshot = k.k->p.offset;
1636 		u32 equiv = bch2_snapshot_equiv(c, snapshot);
1637 
1638 		equiv != snapshot
1639 			? snapshot_list_add(c, &deleted_interior, snapshot)
1640 			: 0;
1641 	}));
1642 
1643 	bch_err_msg(c, ret, "walking snapshots");
1644 	if (ret)
1645 		goto err_create_lock;
1646 
1647 	/*
1648 	 * Fixing children of deleted snapshots can't be done completely
1649 	 * atomically, if we crash between here and when we delete the interior
1650 	 * nodes some depth fields will be off:
1651 	 */
1652 	ret = for_each_btree_key_commit(trans, iter, BTREE_ID_snapshots, POS_MIN,
1653 				  BTREE_ITER_intent, k,
1654 				  NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1655 		bch2_fix_child_of_deleted_snapshot(trans, &iter, k, &deleted_interior));
1656 	if (ret)
1657 		goto err_create_lock;
1658 
1659 	darray_for_each(deleted, i) {
1660 		ret = commit_do(trans, NULL, NULL, 0,
1661 			bch2_snapshot_node_delete(trans, *i));
1662 		bch_err_msg(c, ret, "deleting snapshot %u", *i);
1663 		if (ret)
1664 			goto err_create_lock;
1665 	}
1666 
1667 	darray_for_each(deleted_interior, i) {
1668 		ret = commit_do(trans, NULL, NULL, 0,
1669 			bch2_snapshot_node_delete(trans, *i));
1670 		bch_err_msg(c, ret, "deleting snapshot %u", *i);
1671 		if (ret)
1672 			goto err_create_lock;
1673 	}
1674 err_create_lock:
1675 	up_write(&c->snapshot_create_lock);
1676 err:
1677 	darray_exit(&deleted_interior);
1678 	darray_exit(&deleted);
1679 	bch2_trans_put(trans);
1680 	bch_err_fn(c, ret);
1681 	return ret;
1682 }
1683 
1684 void bch2_delete_dead_snapshots_work(struct work_struct *work)
1685 {
1686 	struct bch_fs *c = container_of(work, struct bch_fs, snapshot_delete_work);
1687 
1688 	set_worker_desc("bcachefs-delete-dead-snapshots/%s", c->name);
1689 
1690 	bch2_delete_dead_snapshots(c);
1691 	bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1692 }
1693 
1694 void bch2_delete_dead_snapshots_async(struct bch_fs *c)
1695 {
1696 	if (bch2_write_ref_tryget(c, BCH_WRITE_REF_delete_dead_snapshots) &&
1697 	    !queue_work(c->write_ref_wq, &c->snapshot_delete_work))
1698 		bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1699 }
1700 
1701 int __bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
1702 				       enum btree_id id,
1703 				       struct bpos pos)
1704 {
1705 	struct bch_fs *c = trans->c;
1706 	struct btree_iter iter;
1707 	struct bkey_s_c k;
1708 	int ret;
1709 
1710 	bch2_trans_iter_init(trans, &iter, id, pos,
1711 			     BTREE_ITER_not_extents|
1712 			     BTREE_ITER_all_snapshots);
1713 	while (1) {
1714 		k = bch2_btree_iter_prev(&iter);
1715 		ret = bkey_err(k);
1716 		if (ret)
1717 			break;
1718 
1719 		if (!k.k)
1720 			break;
1721 
1722 		if (!bkey_eq(pos, k.k->p))
1723 			break;
1724 
1725 		if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, pos.snapshot)) {
1726 			ret = 1;
1727 			break;
1728 		}
1729 	}
1730 	bch2_trans_iter_exit(trans, &iter);
1731 
1732 	return ret;
1733 }
1734 
1735 static u32 bch2_snapshot_smallest_child(struct bch_fs *c, u32 id)
1736 {
1737 	const struct snapshot_t *s = snapshot_t(c, id);
1738 
1739 	return s->children[1] ?: s->children[0];
1740 }
1741 
1742 static u32 bch2_snapshot_smallest_descendent(struct bch_fs *c, u32 id)
1743 {
1744 	u32 child;
1745 
1746 	while ((child = bch2_snapshot_smallest_child(c, id)))
1747 		id = child;
1748 	return id;
1749 }
1750 
1751 static int bch2_propagate_key_to_snapshot_leaf(struct btree_trans *trans,
1752 					       enum btree_id btree,
1753 					       struct bkey_s_c interior_k,
1754 					       u32 leaf_id, struct bpos *new_min_pos)
1755 {
1756 	struct btree_iter iter;
1757 	struct bpos pos = interior_k.k->p;
1758 	struct bkey_s_c k;
1759 	struct bkey_i *new;
1760 	int ret;
1761 
1762 	pos.snapshot = leaf_id;
1763 
1764 	bch2_trans_iter_init(trans, &iter, btree, pos, BTREE_ITER_intent);
1765 	k = bch2_btree_iter_peek_slot(&iter);
1766 	ret = bkey_err(k);
1767 	if (ret)
1768 		goto out;
1769 
1770 	/* key already overwritten in this snapshot? */
1771 	if (k.k->p.snapshot != interior_k.k->p.snapshot)
1772 		goto out;
1773 
1774 	if (bpos_eq(*new_min_pos, POS_MIN)) {
1775 		*new_min_pos = k.k->p;
1776 		new_min_pos->snapshot = leaf_id;
1777 	}
1778 
1779 	new = bch2_bkey_make_mut_noupdate(trans, interior_k);
1780 	ret = PTR_ERR_OR_ZERO(new);
1781 	if (ret)
1782 		goto out;
1783 
1784 	new->k.p.snapshot = leaf_id;
1785 	ret = bch2_trans_update(trans, &iter, new, 0);
1786 out:
1787 	bch2_trans_iter_exit(trans, &iter);
1788 	return ret;
1789 }
1790 
1791 int bch2_propagate_key_to_snapshot_leaves(struct btree_trans *trans,
1792 					  enum btree_id btree,
1793 					  struct bkey_s_c k,
1794 					  struct bpos *new_min_pos)
1795 {
1796 	struct bch_fs *c = trans->c;
1797 	struct bkey_buf sk;
1798 	u32 restart_count = trans->restart_count;
1799 	int ret = 0;
1800 
1801 	bch2_bkey_buf_init(&sk);
1802 	bch2_bkey_buf_reassemble(&sk, c, k);
1803 	k = bkey_i_to_s_c(sk.k);
1804 
1805 	*new_min_pos = POS_MIN;
1806 
1807 	for (u32 id = bch2_snapshot_smallest_descendent(c, k.k->p.snapshot);
1808 	     id < k.k->p.snapshot;
1809 	     id++) {
1810 		if (!bch2_snapshot_is_ancestor(c, id, k.k->p.snapshot) ||
1811 		    !bch2_snapshot_is_leaf(c, id))
1812 			continue;
1813 again:
1814 		ret =   btree_trans_too_many_iters(trans) ?:
1815 			bch2_propagate_key_to_snapshot_leaf(trans, btree, k, id, new_min_pos) ?:
1816 			bch2_trans_commit(trans, NULL, NULL, 0);
1817 		if (ret && bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
1818 			bch2_trans_begin(trans);
1819 			goto again;
1820 		}
1821 
1822 		if (ret)
1823 			break;
1824 	}
1825 
1826 	bch2_bkey_buf_exit(&sk, c);
1827 
1828 	return ret ?: trans_was_restarted(trans, restart_count);
1829 }
1830 
1831 static int bch2_check_snapshot_needs_deletion(struct btree_trans *trans, struct bkey_s_c k)
1832 {
1833 	struct bch_fs *c = trans->c;
1834 	struct bkey_s_c_snapshot snap;
1835 	int ret = 0;
1836 
1837 	if (k.k->type != KEY_TYPE_snapshot)
1838 		return 0;
1839 
1840 	snap = bkey_s_c_to_snapshot(k);
1841 	if (BCH_SNAPSHOT_DELETED(snap.v) ||
1842 	    bch2_snapshot_equiv(c, k.k->p.offset) != k.k->p.offset ||
1843 	    (ret = bch2_snapshot_needs_delete(trans, k)) > 0) {
1844 		set_bit(BCH_FS_need_delete_dead_snapshots, &c->flags);
1845 		return 0;
1846 	}
1847 
1848 	return ret;
1849 }
1850 
1851 int bch2_snapshots_read(struct bch_fs *c)
1852 {
1853 	int ret = bch2_trans_run(c,
1854 		for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1855 				   POS_MIN, 0, k,
1856 			__bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0, bkey_s_c_null, k, 0) ?:
1857 			bch2_snapshot_set_equiv(trans, k) ?:
1858 			bch2_check_snapshot_needs_deletion(trans, k)) ?:
1859 		for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1860 				   POS_MIN, 0, k,
1861 			   (set_is_ancestor_bitmap(c, k.k->p.offset), 0)));
1862 	bch_err_fn(c, ret);
1863 
1864 	/*
1865 	 * It's important that we check if we need to reconstruct snapshots
1866 	 * before going RW, so we mark that pass as required in the superblock -
1867 	 * otherwise, we could end up deleting keys with missing snapshot nodes
1868 	 * instead
1869 	 */
1870 	BUG_ON(!test_bit(BCH_FS_new_fs, &c->flags) &&
1871 	       test_bit(BCH_FS_may_go_rw, &c->flags));
1872 
1873 	if (bch2_err_matches(ret, EIO) ||
1874 	    (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_snapshots)))
1875 		ret = bch2_run_explicit_recovery_pass_persistent(c, BCH_RECOVERY_PASS_reconstruct_snapshots);
1876 
1877 	return ret;
1878 }
1879 
1880 void bch2_fs_snapshots_exit(struct bch_fs *c)
1881 {
1882 	kvfree(rcu_dereference_protected(c->snapshots, true));
1883 }
1884