1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_key_cache.h"
6 #include "btree_update.h"
13 #include <linux/random.h>
18 * Keys in BTREE_ID_snapshot_trees identify a whole tree of snapshot nodes; they
19 * exist to provide a stable identifier for the whole lifetime of a snapshot
23 void bch2_snapshot_tree_to_text(struct printbuf *out, struct bch_fs *c,
26 struct bkey_s_c_snapshot_tree t = bkey_s_c_to_snapshot_tree(k);
28 prt_printf(out, "subvol %u root snapshot %u",
29 le32_to_cpu(t.v->master_subvol),
30 le32_to_cpu(t.v->root_snapshot));
33 int bch2_snapshot_tree_invalid(const struct bch_fs *c, struct bkey_s_c k,
34 enum bkey_invalid_flags flags,
37 if (bkey_gt(k.k->p, POS(0, U32_MAX)) ||
38 bkey_lt(k.k->p, POS(0, 1))) {
39 prt_printf(err, "bad pos");
40 return -BCH_ERR_invalid_bkey;
46 int bch2_snapshot_tree_lookup(struct btree_trans *trans, u32 id,
47 struct bch_snapshot_tree *s)
49 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshot_trees, POS(0, id),
50 BTREE_ITER_WITH_UPDATES, snapshot_tree, s);
52 if (bch2_err_matches(ret, ENOENT))
53 ret = -BCH_ERR_ENOENT_snapshot_tree;
57 struct bkey_i_snapshot_tree *
58 __bch2_snapshot_tree_create(struct btree_trans *trans)
60 struct btree_iter iter;
61 int ret = bch2_bkey_get_empty_slot(trans, &iter,
62 BTREE_ID_snapshot_trees, POS(0, U32_MAX));
63 struct bkey_i_snapshot_tree *s_t;
65 if (ret == -BCH_ERR_ENOSPC_btree_slot)
66 ret = -BCH_ERR_ENOSPC_snapshot_tree;
70 s_t = bch2_bkey_alloc(trans, &iter, 0, snapshot_tree);
71 ret = PTR_ERR_OR_ZERO(s_t);
72 bch2_trans_iter_exit(trans, &iter);
73 return ret ? ERR_PTR(ret) : s_t;
76 static int bch2_snapshot_tree_create(struct btree_trans *trans,
77 u32 root_id, u32 subvol_id, u32 *tree_id)
79 struct bkey_i_snapshot_tree *n_tree =
80 __bch2_snapshot_tree_create(trans);
83 return PTR_ERR(n_tree);
85 n_tree->v.master_subvol = cpu_to_le32(subvol_id);
86 n_tree->v.root_snapshot = cpu_to_le32(root_id);
87 *tree_id = n_tree->k.p.offset;
93 static bool bch2_snapshot_is_ancestor_early(struct bch_fs *c, u32 id, u32 ancestor)
95 struct snapshot_table *t;
98 t = rcu_dereference(c->snapshots);
100 while (id && id < ancestor)
101 id = __snapshot_t(t, id)->parent;
104 return id == ancestor;
107 static inline u32 get_ancestor_below(struct snapshot_table *t, u32 id, u32 ancestor)
109 const struct snapshot_t *s = __snapshot_t(t, id);
111 if (s->skip[2] <= ancestor)
113 if (s->skip[1] <= ancestor)
115 if (s->skip[0] <= ancestor)
120 bool __bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
122 struct snapshot_table *t;
125 EBUG_ON(c->curr_recovery_pass <= BCH_RECOVERY_PASS_check_snapshots);
128 t = rcu_dereference(c->snapshots);
130 while (id && id < ancestor - IS_ANCESTOR_BITMAP)
131 id = get_ancestor_below(t, id, ancestor);
133 if (id && id < ancestor) {
134 ret = test_bit(ancestor - id - 1, __snapshot_t(t, id)->is_ancestor);
136 EBUG_ON(ret != bch2_snapshot_is_ancestor_early(c, id, ancestor));
138 ret = id == ancestor;
146 static noinline struct snapshot_t *__snapshot_t_mut(struct bch_fs *c, u32 id)
148 size_t idx = U32_MAX - id;
150 struct snapshot_table *new, *old;
152 new_size = max(16UL, roundup_pow_of_two(idx + 1));
154 new = kvzalloc(struct_size(new, s, new_size), GFP_KERNEL);
158 old = rcu_dereference_protected(c->snapshots, true);
161 rcu_dereference_protected(c->snapshots, true)->s,
162 sizeof(new->s[0]) * c->snapshot_table_size);
164 rcu_assign_pointer(c->snapshots, new);
165 c->snapshot_table_size = new_size;
169 return &rcu_dereference_protected(c->snapshots, true)->s[idx];
172 static inline struct snapshot_t *snapshot_t_mut(struct bch_fs *c, u32 id)
174 size_t idx = U32_MAX - id;
176 lockdep_assert_held(&c->snapshot_table_lock);
178 if (likely(idx < c->snapshot_table_size))
179 return &rcu_dereference_protected(c->snapshots, true)->s[idx];
181 return __snapshot_t_mut(c, id);
184 void bch2_snapshot_to_text(struct printbuf *out, struct bch_fs *c,
187 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(k);
189 prt_printf(out, "is_subvol %llu deleted %llu parent %10u children %10u %10u subvol %u tree %u",
190 BCH_SNAPSHOT_SUBVOL(s.v),
191 BCH_SNAPSHOT_DELETED(s.v),
192 le32_to_cpu(s.v->parent),
193 le32_to_cpu(s.v->children[0]),
194 le32_to_cpu(s.v->children[1]),
195 le32_to_cpu(s.v->subvol),
196 le32_to_cpu(s.v->tree));
198 if (bkey_val_bytes(k.k) > offsetof(struct bch_snapshot, depth))
199 prt_printf(out, " depth %u skiplist %u %u %u",
200 le32_to_cpu(s.v->depth),
201 le32_to_cpu(s.v->skip[0]),
202 le32_to_cpu(s.v->skip[1]),
203 le32_to_cpu(s.v->skip[2]));
206 int bch2_snapshot_invalid(const struct bch_fs *c, struct bkey_s_c k,
207 enum bkey_invalid_flags flags,
208 struct printbuf *err)
210 struct bkey_s_c_snapshot s;
213 if (bkey_gt(k.k->p, POS(0, U32_MAX)) ||
214 bkey_lt(k.k->p, POS(0, 1))) {
215 prt_printf(err, "bad pos");
216 return -BCH_ERR_invalid_bkey;
219 s = bkey_s_c_to_snapshot(k);
221 id = le32_to_cpu(s.v->parent);
222 if (id && id <= k.k->p.offset) {
223 prt_printf(err, "bad parent node (%u <= %llu)",
225 return -BCH_ERR_invalid_bkey;
228 if (le32_to_cpu(s.v->children[0]) < le32_to_cpu(s.v->children[1])) {
229 prt_printf(err, "children not normalized");
230 return -BCH_ERR_invalid_bkey;
233 if (s.v->children[0] &&
234 s.v->children[0] == s.v->children[1]) {
235 prt_printf(err, "duplicate child nodes");
236 return -BCH_ERR_invalid_bkey;
239 for (i = 0; i < 2; i++) {
240 id = le32_to_cpu(s.v->children[i]);
242 if (id >= k.k->p.offset) {
243 prt_printf(err, "bad child node (%u >= %llu)",
245 return -BCH_ERR_invalid_bkey;
249 if (bkey_val_bytes(k.k) > offsetof(struct bch_snapshot, skip)) {
250 if (le32_to_cpu(s.v->skip[0]) > le32_to_cpu(s.v->skip[1]) ||
251 le32_to_cpu(s.v->skip[1]) > le32_to_cpu(s.v->skip[2])) {
252 prt_printf(err, "skiplist not normalized");
253 return -BCH_ERR_invalid_bkey;
256 for (i = 0; i < ARRAY_SIZE(s.v->skip); i++) {
257 id = le32_to_cpu(s.v->skip[i]);
259 if ((id && !s.v->parent) ||
260 (id && id <= k.k->p.offset)) {
261 prt_printf(err, "bad skiplist node %u", id);
262 return -BCH_ERR_invalid_bkey;
270 static void __set_is_ancestor_bitmap(struct bch_fs *c, u32 id)
272 struct snapshot_t *t = snapshot_t_mut(c, id);
275 while ((parent = bch2_snapshot_parent_early(c, parent)) &&
276 parent - id - 1 < IS_ANCESTOR_BITMAP)
277 __set_bit(parent - id - 1, t->is_ancestor);
280 static void set_is_ancestor_bitmap(struct bch_fs *c, u32 id)
282 mutex_lock(&c->snapshot_table_lock);
283 __set_is_ancestor_bitmap(c, id);
284 mutex_unlock(&c->snapshot_table_lock);
287 int bch2_mark_snapshot(struct btree_trans *trans,
288 enum btree_id btree, unsigned level,
289 struct bkey_s_c old, struct bkey_s_c new,
292 struct bch_fs *c = trans->c;
293 struct snapshot_t *t;
294 u32 id = new.k->p.offset;
297 mutex_lock(&c->snapshot_table_lock);
299 t = snapshot_t_mut(c, id);
301 ret = -BCH_ERR_ENOMEM_mark_snapshot;
305 if (new.k->type == KEY_TYPE_snapshot) {
306 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(new);
308 t->parent = le32_to_cpu(s.v->parent);
309 t->children[0] = le32_to_cpu(s.v->children[0]);
310 t->children[1] = le32_to_cpu(s.v->children[1]);
311 t->subvol = BCH_SNAPSHOT_SUBVOL(s.v) ? le32_to_cpu(s.v->subvol) : 0;
312 t->tree = le32_to_cpu(s.v->tree);
314 if (bkey_val_bytes(s.k) > offsetof(struct bch_snapshot, depth)) {
315 t->depth = le32_to_cpu(s.v->depth);
316 t->skip[0] = le32_to_cpu(s.v->skip[0]);
317 t->skip[1] = le32_to_cpu(s.v->skip[1]);
318 t->skip[2] = le32_to_cpu(s.v->skip[2]);
326 __set_is_ancestor_bitmap(c, id);
328 if (BCH_SNAPSHOT_DELETED(s.v)) {
329 set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
330 c->recovery_passes_explicit |= BIT_ULL(BCH_RECOVERY_PASS_delete_dead_snapshots);
333 memset(t, 0, sizeof(*t));
336 mutex_unlock(&c->snapshot_table_lock);
340 int bch2_snapshot_lookup(struct btree_trans *trans, u32 id,
341 struct bch_snapshot *s)
343 return bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots, POS(0, id),
344 BTREE_ITER_WITH_UPDATES, snapshot, s);
347 int bch2_snapshot_live(struct btree_trans *trans, u32 id)
349 struct bch_snapshot v;
355 ret = bch2_snapshot_lookup(trans, id, &v);
356 if (bch2_err_matches(ret, ENOENT))
357 bch_err(trans->c, "snapshot node %u not found", id);
361 return !BCH_SNAPSHOT_DELETED(&v);
365 * If @k is a snapshot with just one live child, it's part of a linear chain,
366 * which we consider to be an equivalence class: and then after snapshot
367 * deletion cleanup, there should only be a single key at a given position in
368 * this equivalence class.
370 * This sets the equivalence class of @k to be the child's equivalence class, if
371 * it's part of such a linear chain: this correctly sets equivalence classes on
372 * startup if we run leaf to root (i.e. in natural key order).
374 int bch2_snapshot_set_equiv(struct btree_trans *trans, struct bkey_s_c k)
376 struct bch_fs *c = trans->c;
377 unsigned i, nr_live = 0, live_idx = 0;
378 struct bkey_s_c_snapshot snap;
379 u32 id = k.k->p.offset, child[2];
381 if (k.k->type != KEY_TYPE_snapshot)
384 snap = bkey_s_c_to_snapshot(k);
386 child[0] = le32_to_cpu(snap.v->children[0]);
387 child[1] = le32_to_cpu(snap.v->children[1]);
389 for (i = 0; i < 2; i++) {
390 int ret = bch2_snapshot_live(trans, child[i]);
400 mutex_lock(&c->snapshot_table_lock);
402 snapshot_t_mut(c, id)->equiv = nr_live == 1
403 ? snapshot_t_mut(c, child[live_idx])->equiv
406 mutex_unlock(&c->snapshot_table_lock);
413 static u32 bch2_snapshot_child(struct bch_fs *c, u32 id, unsigned child)
415 return snapshot_t(c, id)->children[child];
418 static u32 bch2_snapshot_left_child(struct bch_fs *c, u32 id)
420 return bch2_snapshot_child(c, id, 0);
423 static u32 bch2_snapshot_right_child(struct bch_fs *c, u32 id)
425 return bch2_snapshot_child(c, id, 1);
428 static u32 bch2_snapshot_tree_next(struct bch_fs *c, u32 id)
432 n = bch2_snapshot_left_child(c, id);
436 while ((parent = bch2_snapshot_parent(c, id))) {
437 n = bch2_snapshot_right_child(c, parent);
446 static u32 bch2_snapshot_tree_oldest_subvol(struct bch_fs *c, u32 snapshot_root)
448 u32 id = snapshot_root;
452 s = snapshot_t(c, id)->subvol;
454 if (s && (!subvol || s < subvol))
457 id = bch2_snapshot_tree_next(c, id);
463 static int bch2_snapshot_tree_master_subvol(struct btree_trans *trans,
464 u32 snapshot_root, u32 *subvol_id)
466 struct bch_fs *c = trans->c;
467 struct btree_iter iter;
469 struct bkey_s_c_subvolume s;
473 for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN,
475 if (k.k->type != KEY_TYPE_subvolume)
478 s = bkey_s_c_to_subvolume(k);
479 if (!bch2_snapshot_is_ancestor(c, le32_to_cpu(s.v->snapshot), snapshot_root))
481 if (!BCH_SUBVOLUME_SNAP(s.v)) {
482 *subvol_id = s.k->p.offset;
488 bch2_trans_iter_exit(trans, &iter);
490 if (!ret && !found) {
491 struct bkey_i_subvolume *s;
493 *subvol_id = bch2_snapshot_tree_oldest_subvol(c, snapshot_root);
495 s = bch2_bkey_get_mut_typed(trans, &iter,
496 BTREE_ID_subvolumes, POS(0, *subvol_id),
498 ret = PTR_ERR_OR_ZERO(s);
502 SET_BCH_SUBVOLUME_SNAP(&s->v, false);
508 static int check_snapshot_tree(struct btree_trans *trans,
509 struct btree_iter *iter,
512 struct bch_fs *c = trans->c;
513 struct bkey_s_c_snapshot_tree st;
514 struct bch_snapshot s;
515 struct bch_subvolume subvol;
516 struct printbuf buf = PRINTBUF;
520 if (k.k->type != KEY_TYPE_snapshot_tree)
523 st = bkey_s_c_to_snapshot_tree(k);
524 root_id = le32_to_cpu(st.v->root_snapshot);
526 ret = bch2_snapshot_lookup(trans, root_id, &s);
527 if (ret && !bch2_err_matches(ret, ENOENT))
530 if (fsck_err_on(ret ||
531 root_id != bch2_snapshot_root(c, root_id) ||
532 st.k->p.offset != le32_to_cpu(s.tree),
534 "snapshot tree points to missing/incorrect snapshot:\n %s",
535 (bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
536 ret = bch2_btree_delete_at(trans, iter, 0);
540 ret = bch2_subvolume_get(trans, le32_to_cpu(st.v->master_subvol),
542 if (ret && !bch2_err_matches(ret, ENOENT))
545 if (fsck_err_on(ret, c,
546 "snapshot tree points to missing subvolume:\n %s",
547 (printbuf_reset(&buf),
548 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
549 fsck_err_on(!bch2_snapshot_is_ancestor_early(c,
550 le32_to_cpu(subvol.snapshot),
552 "snapshot tree points to subvolume that does not point to snapshot in this tree:\n %s",
553 (printbuf_reset(&buf),
554 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
555 fsck_err_on(BCH_SUBVOLUME_SNAP(&subvol), c,
556 "snapshot tree points to snapshot subvolume:\n %s",
557 (printbuf_reset(&buf),
558 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
559 struct bkey_i_snapshot_tree *u;
562 ret = bch2_snapshot_tree_master_subvol(trans, root_id, &subvol_id);
566 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot_tree);
567 ret = PTR_ERR_OR_ZERO(u);
571 u->v.master_subvol = cpu_to_le32(subvol_id);
572 st = snapshot_tree_i_to_s_c(u);
581 * For each snapshot_tree, make sure it points to the root of a snapshot tree
582 * and that snapshot entry points back to it, or delete it.
584 * And, make sure it points to a subvolume within that snapshot tree, or correct
585 * it to point to the oldest subvolume within that snapshot tree.
587 int bch2_check_snapshot_trees(struct bch_fs *c)
589 struct btree_iter iter;
593 ret = bch2_trans_run(c,
594 for_each_btree_key_commit(&trans, iter,
595 BTREE_ID_snapshot_trees, POS_MIN,
596 BTREE_ITER_PREFETCH, k,
597 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
598 check_snapshot_tree(&trans, &iter, k)));
601 bch_err(c, "error %i checking snapshot trees", ret);
606 * Look up snapshot tree for @tree_id and find root,
607 * make sure @snap_id is a descendent:
609 static int snapshot_tree_ptr_good(struct btree_trans *trans,
610 u32 snap_id, u32 tree_id)
612 struct bch_snapshot_tree s_t;
613 int ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
615 if (bch2_err_matches(ret, ENOENT))
620 return bch2_snapshot_is_ancestor_early(trans->c, snap_id, le32_to_cpu(s_t.root_snapshot));
623 u32 bch2_snapshot_skiplist_get(struct bch_fs *c, u32 id)
625 const struct snapshot_t *s;
631 s = snapshot_t(c, id);
633 id = bch2_snapshot_nth_parent(c, id, get_random_u32_below(s->depth));
639 static int snapshot_skiplist_good(struct btree_trans *trans, u32 id, struct bch_snapshot s)
643 for (i = 0; i < 3; i++)
648 if (!bch2_snapshot_is_ancestor_early(trans->c, id, le32_to_cpu(s.skip[i])))
656 * snapshot_tree pointer was incorrect: look up root snapshot node, make sure
657 * its snapshot_tree pointer is correct (allocate new one if necessary), then
658 * update this node's pointer to root node's pointer:
660 static int snapshot_tree_ptr_repair(struct btree_trans *trans,
661 struct btree_iter *iter,
663 struct bch_snapshot *s)
665 struct bch_fs *c = trans->c;
666 struct btree_iter root_iter;
667 struct bch_snapshot_tree s_t;
668 struct bkey_s_c_snapshot root;
669 struct bkey_i_snapshot *u;
670 u32 root_id = bch2_snapshot_root(c, k.k->p.offset), tree_id;
673 root = bch2_bkey_get_iter_typed(trans, &root_iter,
674 BTREE_ID_snapshots, POS(0, root_id),
675 BTREE_ITER_WITH_UPDATES, snapshot);
676 ret = bkey_err(root);
680 tree_id = le32_to_cpu(root.v->tree);
682 ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
683 if (ret && !bch2_err_matches(ret, ENOENT))
686 if (ret || le32_to_cpu(s_t.root_snapshot) != root_id) {
687 u = bch2_bkey_make_mut_typed(trans, &root_iter, &root.s_c, 0, snapshot);
688 ret = PTR_ERR_OR_ZERO(u) ?:
689 bch2_snapshot_tree_create(trans, root_id,
690 bch2_snapshot_tree_oldest_subvol(c, root_id),
695 u->v.tree = cpu_to_le32(tree_id);
696 if (k.k->p.offset == root_id)
700 if (k.k->p.offset != root_id) {
701 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
702 ret = PTR_ERR_OR_ZERO(u);
706 u->v.tree = cpu_to_le32(tree_id);
710 bch2_trans_iter_exit(trans, &root_iter);
714 static int check_snapshot(struct btree_trans *trans,
715 struct btree_iter *iter,
718 struct bch_fs *c = trans->c;
719 struct bch_snapshot s;
720 struct bch_subvolume subvol;
721 struct bch_snapshot v;
722 struct bkey_i_snapshot *u;
723 u32 parent_id = bch2_snapshot_parent_early(c, k.k->p.offset);
725 struct printbuf buf = PRINTBUF;
726 bool should_have_subvol;
730 if (k.k->type != KEY_TYPE_snapshot)
733 memset(&s, 0, sizeof(s));
734 memcpy(&s, k.v, bkey_val_bytes(k.k));
736 id = le32_to_cpu(s.parent);
738 ret = bch2_snapshot_lookup(trans, id, &v);
739 if (bch2_err_matches(ret, ENOENT))
740 bch_err(c, "snapshot with nonexistent parent:\n %s",
741 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
745 if (le32_to_cpu(v.children[0]) != k.k->p.offset &&
746 le32_to_cpu(v.children[1]) != k.k->p.offset) {
747 bch_err(c, "snapshot parent %u missing pointer to child %llu",
754 for (i = 0; i < 2 && s.children[i]; i++) {
755 id = le32_to_cpu(s.children[i]);
757 ret = bch2_snapshot_lookup(trans, id, &v);
758 if (bch2_err_matches(ret, ENOENT))
759 bch_err(c, "snapshot node %llu has nonexistent child %u",
764 if (le32_to_cpu(v.parent) != k.k->p.offset) {
765 bch_err(c, "snapshot child %u has wrong parent (got %u should be %llu)",
766 id, le32_to_cpu(v.parent), k.k->p.offset);
772 should_have_subvol = BCH_SNAPSHOT_SUBVOL(&s) &&
773 !BCH_SNAPSHOT_DELETED(&s);
775 if (should_have_subvol) {
776 id = le32_to_cpu(s.subvol);
777 ret = bch2_subvolume_get(trans, id, 0, false, &subvol);
778 if (bch2_err_matches(ret, ENOENT))
779 bch_err(c, "snapshot points to nonexistent subvolume:\n %s",
780 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
784 if (BCH_SNAPSHOT_SUBVOL(&s) != (le32_to_cpu(subvol.snapshot) == k.k->p.offset)) {
785 bch_err(c, "snapshot node %llu has wrong BCH_SNAPSHOT_SUBVOL",
791 if (fsck_err_on(s.subvol, c, "snapshot should not point to subvol:\n %s",
792 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
793 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
794 ret = PTR_ERR_OR_ZERO(u);
803 ret = snapshot_tree_ptr_good(trans, k.k->p.offset, le32_to_cpu(s.tree));
807 if (fsck_err_on(!ret, c, "snapshot points to missing/incorrect tree:\n %s",
808 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
809 ret = snapshot_tree_ptr_repair(trans, iter, k, &s);
815 real_depth = bch2_snapshot_depth(c, parent_id);
817 if (le32_to_cpu(s.depth) != real_depth &&
818 (c->sb.version_upgrade_complete < bcachefs_metadata_version_snapshot_skiplists ||
819 fsck_err(c, "snapshot with incorrect depth field, should be %u:\n %s",
820 real_depth, (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))) {
821 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
822 ret = PTR_ERR_OR_ZERO(u);
826 u->v.depth = cpu_to_le32(real_depth);
830 ret = snapshot_skiplist_good(trans, k.k->p.offset, s);
835 (c->sb.version_upgrade_complete < bcachefs_metadata_version_snapshot_skiplists ||
836 fsck_err(c, "snapshot with bad skiplist field:\n %s",
837 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))) {
838 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
839 ret = PTR_ERR_OR_ZERO(u);
843 for (i = 0; i < ARRAY_SIZE(u->v.skip); i++)
844 u->v.skip[i] = cpu_to_le32(bch2_snapshot_skiplist_get(c, parent_id));
846 bubble_sort(u->v.skip, ARRAY_SIZE(u->v.skip), cmp_le32);
856 int bch2_check_snapshots(struct bch_fs *c)
858 struct btree_iter iter;
863 * We iterate backwards as checking/fixing the depth field requires that
864 * the parent's depth already be correct:
866 ret = bch2_trans_run(c,
867 for_each_btree_key_reverse_commit(&trans, iter,
868 BTREE_ID_snapshots, POS_MAX,
869 BTREE_ITER_PREFETCH, k,
870 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
871 check_snapshot(&trans, &iter, k)));
878 * Mark a snapshot as deleted, for future cleanup:
880 int bch2_snapshot_node_set_deleted(struct btree_trans *trans, u32 id)
882 struct btree_iter iter;
883 struct bkey_i_snapshot *s;
886 s = bch2_bkey_get_mut_typed(trans, &iter,
887 BTREE_ID_snapshots, POS(0, id),
889 ret = PTR_ERR_OR_ZERO(s);
891 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT),
892 trans->c, "missing snapshot %u", id);
896 /* already deleted? */
897 if (BCH_SNAPSHOT_DELETED(&s->v))
900 SET_BCH_SNAPSHOT_DELETED(&s->v, true);
901 SET_BCH_SNAPSHOT_SUBVOL(&s->v, false);
904 bch2_trans_iter_exit(trans, &iter);
908 static inline void normalize_snapshot_child_pointers(struct bch_snapshot *s)
910 if (le32_to_cpu(s->children[0]) < le32_to_cpu(s->children[1]))
911 swap(s->children[0], s->children[1]);
914 int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
916 struct bch_fs *c = trans->c;
917 struct btree_iter iter, p_iter = (struct btree_iter) { NULL };
918 struct btree_iter c_iter = (struct btree_iter) { NULL };
919 struct btree_iter tree_iter = (struct btree_iter) { NULL };
920 struct bkey_s_c_snapshot s;
921 u32 parent_id, child_id;
925 s = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_snapshots, POS(0, id),
926 BTREE_ITER_INTENT, snapshot);
928 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
929 "missing snapshot %u", id);
934 BUG_ON(s.v->children[1]);
936 parent_id = le32_to_cpu(s.v->parent);
937 child_id = le32_to_cpu(s.v->children[0]);
940 struct bkey_i_snapshot *parent;
942 parent = bch2_bkey_get_mut_typed(trans, &p_iter,
943 BTREE_ID_snapshots, POS(0, parent_id),
945 ret = PTR_ERR_OR_ZERO(parent);
946 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
947 "missing snapshot %u", parent_id);
951 /* find entry in parent->children for node being deleted */
952 for (i = 0; i < 2; i++)
953 if (le32_to_cpu(parent->v.children[i]) == id)
956 if (bch2_fs_inconsistent_on(i == 2, c,
957 "snapshot %u missing child pointer to %u",
961 parent->v.children[i] = le32_to_cpu(child_id);
963 normalize_snapshot_child_pointers(&parent->v);
967 struct bkey_i_snapshot *child;
969 child = bch2_bkey_get_mut_typed(trans, &c_iter,
970 BTREE_ID_snapshots, POS(0, child_id),
972 ret = PTR_ERR_OR_ZERO(child);
973 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
974 "missing snapshot %u", child_id);
978 child->v.parent = cpu_to_le32(parent_id);
980 if (!child->v.parent) {
981 child->v.skip[0] = 0;
982 child->v.skip[1] = 0;
983 child->v.skip[2] = 0;
989 * We're deleting the root of a snapshot tree: update the
990 * snapshot_tree entry to point to the new root, or delete it if
991 * this is the last snapshot ID in this tree:
993 struct bkey_i_snapshot_tree *s_t;
995 BUG_ON(s.v->children[1]);
997 s_t = bch2_bkey_get_mut_typed(trans, &tree_iter,
998 BTREE_ID_snapshot_trees, POS(0, le32_to_cpu(s.v->tree)),
1000 ret = PTR_ERR_OR_ZERO(s_t);
1004 if (s.v->children[0]) {
1005 s_t->v.root_snapshot = s.v->children[0];
1007 s_t->k.type = KEY_TYPE_deleted;
1008 set_bkey_val_u64s(&s_t->k, 0);
1012 ret = bch2_btree_delete_at(trans, &iter, 0);
1014 bch2_trans_iter_exit(trans, &tree_iter);
1015 bch2_trans_iter_exit(trans, &p_iter);
1016 bch2_trans_iter_exit(trans, &c_iter);
1017 bch2_trans_iter_exit(trans, &iter);
1021 static int create_snapids(struct btree_trans *trans, u32 parent, u32 tree,
1023 u32 *snapshot_subvols,
1024 unsigned nr_snapids)
1026 struct bch_fs *c = trans->c;
1027 struct btree_iter iter;
1028 struct bkey_i_snapshot *n;
1031 u32 depth = bch2_snapshot_depth(c, parent);
1034 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
1035 POS_MIN, BTREE_ITER_INTENT);
1036 k = bch2_btree_iter_peek(&iter);
1041 for (i = 0; i < nr_snapids; i++) {
1042 k = bch2_btree_iter_prev_slot(&iter);
1047 if (!k.k || !k.k->p.offset) {
1048 ret = -BCH_ERR_ENOSPC_snapshot_create;
1052 n = bch2_bkey_alloc(trans, &iter, 0, snapshot);
1053 ret = PTR_ERR_OR_ZERO(n);
1058 n->v.parent = cpu_to_le32(parent);
1059 n->v.subvol = cpu_to_le32(snapshot_subvols[i]);
1060 n->v.tree = cpu_to_le32(tree);
1061 n->v.depth = cpu_to_le32(depth);
1063 for (j = 0; j < ARRAY_SIZE(n->v.skip); j++)
1064 n->v.skip[j] = cpu_to_le32(bch2_snapshot_skiplist_get(c, parent));
1066 bubble_sort(n->v.skip, ARRAY_SIZE(n->v.skip), cmp_le32);
1067 SET_BCH_SNAPSHOT_SUBVOL(&n->v, true);
1069 ret = bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0,
1070 bkey_s_c_null, bkey_i_to_s_c(&n->k_i), 0);
1074 new_snapids[i] = iter.pos.offset;
1077 bch2_trans_iter_exit(trans, &iter);
1082 * Create new snapshot IDs as children of an existing snapshot ID:
1084 static int bch2_snapshot_node_create_children(struct btree_trans *trans, u32 parent,
1086 u32 *snapshot_subvols,
1087 unsigned nr_snapids)
1089 struct btree_iter iter;
1090 struct bkey_i_snapshot *n_parent;
1093 n_parent = bch2_bkey_get_mut_typed(trans, &iter,
1094 BTREE_ID_snapshots, POS(0, parent),
1096 ret = PTR_ERR_OR_ZERO(n_parent);
1097 if (unlikely(ret)) {
1098 if (bch2_err_matches(ret, ENOENT))
1099 bch_err(trans->c, "snapshot %u not found", parent);
1103 if (n_parent->v.children[0] || n_parent->v.children[1]) {
1104 bch_err(trans->c, "Trying to add child snapshot nodes to parent that already has children");
1109 ret = create_snapids(trans, parent, le32_to_cpu(n_parent->v.tree),
1110 new_snapids, snapshot_subvols, nr_snapids);
1114 n_parent->v.children[0] = cpu_to_le32(new_snapids[0]);
1115 n_parent->v.children[1] = cpu_to_le32(new_snapids[1]);
1116 n_parent->v.subvol = 0;
1117 SET_BCH_SNAPSHOT_SUBVOL(&n_parent->v, false);
1119 bch2_trans_iter_exit(trans, &iter);
1124 * Create a snapshot node that is the root of a new tree:
1126 static int bch2_snapshot_node_create_tree(struct btree_trans *trans,
1128 u32 *snapshot_subvols,
1129 unsigned nr_snapids)
1131 struct bkey_i_snapshot_tree *n_tree;
1134 n_tree = __bch2_snapshot_tree_create(trans);
1135 ret = PTR_ERR_OR_ZERO(n_tree) ?:
1136 create_snapids(trans, 0, n_tree->k.p.offset,
1137 new_snapids, snapshot_subvols, nr_snapids);
1141 n_tree->v.master_subvol = cpu_to_le32(snapshot_subvols[0]);
1142 n_tree->v.root_snapshot = cpu_to_le32(new_snapids[0]);
1146 int bch2_snapshot_node_create(struct btree_trans *trans, u32 parent,
1148 u32 *snapshot_subvols,
1149 unsigned nr_snapids)
1151 BUG_ON((parent == 0) != (nr_snapids == 1));
1152 BUG_ON((parent != 0) != (nr_snapids == 2));
1155 ? bch2_snapshot_node_create_children(trans, parent,
1156 new_snapids, snapshot_subvols, nr_snapids)
1157 : bch2_snapshot_node_create_tree(trans,
1158 new_snapids, snapshot_subvols, nr_snapids);
1163 * If we have an unlinked inode in an internal snapshot node, and the inode
1164 * really has been deleted in all child snapshots, how does this get cleaned up?
1166 * first there is the problem of how keys that have been overwritten in all
1167 * child snapshots get deleted (unimplemented?), but inodes may perhaps be
1170 * also: unlinked inode in internal snapshot appears to not be getting deleted
1171 * correctly if inode doesn't exist in leaf snapshots
1175 * for a key in an interior snapshot node that needs work to be done that
1176 * requires it to be mutated: iterate over all descendent leaf nodes and copy
1177 * that key to snapshot leaf nodes, where we can mutate it
1180 static int snapshot_delete_key(struct btree_trans *trans,
1181 struct btree_iter *iter,
1183 snapshot_id_list *deleted,
1184 snapshot_id_list *equiv_seen,
1185 struct bpos *last_pos)
1187 struct bch_fs *c = trans->c;
1188 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
1190 if (!bkey_eq(k.k->p, *last_pos))
1194 if (snapshot_list_has_id(deleted, k.k->p.snapshot) ||
1195 snapshot_list_has_id(equiv_seen, equiv)) {
1196 return bch2_btree_delete_at(trans, iter,
1197 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1199 return snapshot_list_add(c, equiv_seen, equiv);
1203 static int move_key_to_correct_snapshot(struct btree_trans *trans,
1204 struct btree_iter *iter,
1207 struct bch_fs *c = trans->c;
1208 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
1211 * When we have a linear chain of snapshot nodes, we consider
1212 * those to form an equivalence class: we're going to collapse
1213 * them all down to a single node, and keep the leaf-most node -
1214 * which has the same id as the equivalence class id.
1216 * If there are multiple keys in different snapshots at the same
1217 * position, we're only going to keep the one in the newest
1218 * snapshot - the rest have been overwritten and are redundant,
1219 * and for the key we're going to keep we need to move it to the
1220 * equivalance class ID if it's not there already.
1222 if (equiv != k.k->p.snapshot) {
1223 struct bkey_i *new = bch2_bkey_make_mut_noupdate(trans, k);
1224 struct btree_iter new_iter;
1227 ret = PTR_ERR_OR_ZERO(new);
1231 new->k.p.snapshot = equiv;
1233 bch2_trans_iter_init(trans, &new_iter, iter->btree_id, new->k.p,
1234 BTREE_ITER_ALL_SNAPSHOTS|
1238 ret = bch2_btree_iter_traverse(&new_iter) ?:
1239 bch2_trans_update(trans, &new_iter, new,
1240 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
1241 bch2_btree_delete_at(trans, iter,
1242 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1243 bch2_trans_iter_exit(trans, &new_iter);
1252 * For a given snapshot, if it doesn't have a subvolume that points to it, and
1253 * it doesn't have child snapshot nodes - it's now redundant and we can mark it
1256 static int bch2_delete_redundant_snapshot(struct btree_trans *trans, struct btree_iter *iter,
1259 struct bkey_s_c_snapshot snap;
1263 if (k.k->type != KEY_TYPE_snapshot)
1266 snap = bkey_s_c_to_snapshot(k);
1267 if (BCH_SNAPSHOT_DELETED(snap.v) ||
1268 BCH_SNAPSHOT_SUBVOL(snap.v))
1271 children[0] = le32_to_cpu(snap.v->children[0]);
1272 children[1] = le32_to_cpu(snap.v->children[1]);
1274 ret = bch2_snapshot_live(trans, children[0]) ?:
1275 bch2_snapshot_live(trans, children[1]);
1280 return bch2_snapshot_node_set_deleted(trans, k.k->p.offset);
1284 static inline u32 bch2_snapshot_nth_parent_skip(struct bch_fs *c, u32 id, u32 n,
1285 snapshot_id_list *skip)
1290 id = __bch2_snapshot_parent(c, id);
1291 } while (snapshot_list_has_id(skip, id));
1298 static int bch2_fix_child_of_deleted_snapshot(struct btree_trans *trans,
1299 struct btree_iter *iter, struct bkey_s_c k,
1300 snapshot_id_list *deleted)
1302 struct bch_fs *c = trans->c;
1303 u32 nr_deleted_ancestors = 0;
1304 struct bkey_i_snapshot *s;
1308 if (k.k->type != KEY_TYPE_snapshot)
1311 if (snapshot_list_has_id(deleted, k.k->p.offset))
1314 s = bch2_bkey_make_mut_noupdate_typed(trans, k, snapshot);
1315 ret = PTR_ERR_OR_ZERO(s);
1319 darray_for_each(*deleted, i)
1320 nr_deleted_ancestors += bch2_snapshot_is_ancestor(c, s->k.p.offset, *i);
1322 if (!nr_deleted_ancestors)
1325 le32_add_cpu(&s->v.depth, -nr_deleted_ancestors);
1332 u32 depth = le32_to_cpu(s->v.depth);
1333 u32 parent = bch2_snapshot_parent(c, s->k.p.offset);
1335 for (unsigned j = 0; j < ARRAY_SIZE(s->v.skip); j++) {
1336 u32 id = le32_to_cpu(s->v.skip[j]);
1338 if (snapshot_list_has_id(deleted, id)) {
1340 ? bch2_snapshot_nth_parent_skip(c,
1342 get_random_u32_below(depth - 1),
1345 s->v.skip[j] = cpu_to_le32(id);
1349 bubble_sort(s->v.skip, ARRAY_SIZE(s->v.skip), cmp_le32);
1352 return bch2_trans_update(trans, iter, &s->k_i, 0);
1355 int bch2_delete_dead_snapshots(struct bch_fs *c)
1357 struct btree_trans trans;
1358 struct btree_iter iter;
1360 struct bkey_s_c_snapshot snap;
1361 snapshot_id_list deleted = { 0 };
1362 snapshot_id_list deleted_interior = { 0 };
1366 if (!test_bit(BCH_FS_STARTED, &c->flags)) {
1367 ret = bch2_fs_read_write_early(c);
1369 bch_err(c, "error deleleting dead snapshots: error going rw: %s", bch2_err_str(ret));
1374 bch2_trans_init(&trans, c, 0, 0);
1377 * For every snapshot node: If we have no live children and it's not
1378 * pointed to by a subvolume, delete it:
1380 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_snapshots,
1383 bch2_delete_redundant_snapshot(&trans, &iter, k));
1385 bch_err(c, "error deleting redundant snapshots: %s", bch2_err_str(ret));
1389 for_each_btree_key2(&trans, iter, BTREE_ID_snapshots,
1391 bch2_snapshot_set_equiv(&trans, k));
1393 bch_err(c, "error in bch2_snapshots_set_equiv: %s", bch2_err_str(ret));
1397 for_each_btree_key(&trans, iter, BTREE_ID_snapshots,
1398 POS_MIN, 0, k, ret) {
1399 if (k.k->type != KEY_TYPE_snapshot)
1402 snap = bkey_s_c_to_snapshot(k);
1403 if (BCH_SNAPSHOT_DELETED(snap.v)) {
1404 ret = snapshot_list_add(c, &deleted, k.k->p.offset);
1409 bch2_trans_iter_exit(&trans, &iter);
1412 bch_err_msg(c, ret, "walking snapshots");
1416 for (id = 0; id < BTREE_ID_NR; id++) {
1417 struct bpos last_pos = POS_MIN;
1418 snapshot_id_list equiv_seen = { 0 };
1419 struct disk_reservation res = { 0 };
1421 if (!btree_type_has_snapshots(id))
1424 ret = for_each_btree_key_commit(&trans, iter,
1426 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1427 &res, NULL, BTREE_INSERT_NOFAIL,
1428 snapshot_delete_key(&trans, &iter, k, &deleted, &equiv_seen, &last_pos)) ?:
1429 for_each_btree_key_commit(&trans, iter,
1431 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1432 &res, NULL, BTREE_INSERT_NOFAIL,
1433 move_key_to_correct_snapshot(&trans, &iter, k));
1435 bch2_disk_reservation_put(c, &res);
1436 darray_exit(&equiv_seen);
1439 bch_err_msg(c, ret, "deleting keys from dying snapshots");
1444 for_each_btree_key(&trans, iter, BTREE_ID_snapshots,
1445 POS_MIN, 0, k, ret) {
1446 u32 snapshot = k.k->p.offset;
1447 u32 equiv = bch2_snapshot_equiv(c, snapshot);
1449 if (equiv != snapshot)
1450 snapshot_list_add(c, &deleted_interior, snapshot);
1452 bch2_trans_iter_exit(&trans, &iter);
1455 * Fixing children of deleted snapshots can't be done completely
1456 * atomically, if we crash between here and when we delete the interior
1457 * nodes some depth fields will be off:
1459 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_snapshots, POS_MIN,
1460 BTREE_ITER_INTENT, k,
1461 NULL, NULL, BTREE_INSERT_NOFAIL,
1462 bch2_fix_child_of_deleted_snapshot(&trans, &iter, k, &deleted_interior));
1466 darray_for_each(deleted, i) {
1467 ret = commit_do(&trans, NULL, NULL, 0,
1468 bch2_snapshot_node_delete(&trans, *i));
1470 bch_err_msg(c, ret, "deleting snapshot %u", *i);
1475 darray_for_each(deleted_interior, i) {
1476 ret = commit_do(&trans, NULL, NULL, 0,
1477 bch2_snapshot_node_delete(&trans, *i));
1479 bch_err_msg(c, ret, "deleting snapshot %u", *i);
1484 clear_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
1486 darray_exit(&deleted_interior);
1487 darray_exit(&deleted);
1488 bch2_trans_exit(&trans);
1494 void bch2_delete_dead_snapshots_work(struct work_struct *work)
1496 struct bch_fs *c = container_of(work, struct bch_fs, snapshot_delete_work);
1498 if (test_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags))
1499 bch2_delete_dead_snapshots(c);
1500 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1503 void bch2_delete_dead_snapshots_async(struct bch_fs *c)
1505 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_delete_dead_snapshots) &&
1506 !queue_work(c->write_ref_wq, &c->snapshot_delete_work))
1507 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1510 int bch2_delete_dead_snapshots_hook(struct btree_trans *trans,
1511 struct btree_trans_commit_hook *h)
1513 struct bch_fs *c = trans->c;
1515 set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
1517 if (c->curr_recovery_pass <= BCH_RECOVERY_PASS_delete_dead_snapshots)
1520 bch2_delete_dead_snapshots_async(c);
1524 int __bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
1528 struct bch_fs *c = trans->c;
1529 struct btree_iter iter;
1533 bch2_trans_iter_init(trans, &iter, id, pos,
1534 BTREE_ITER_NOT_EXTENTS|
1535 BTREE_ITER_ALL_SNAPSHOTS);
1537 k = bch2_btree_iter_prev(&iter);
1545 if (!bkey_eq(pos, k.k->p))
1548 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, pos.snapshot)) {
1553 bch2_trans_iter_exit(trans, &iter);
1558 static u32 bch2_snapshot_smallest_child(struct bch_fs *c, u32 id)
1560 const struct snapshot_t *s = snapshot_t(c, id);
1562 return s->children[1] ?: s->children[0];
1565 static u32 bch2_snapshot_smallest_descendent(struct bch_fs *c, u32 id)
1569 while ((child = bch2_snapshot_smallest_child(c, id)))
1574 static int bch2_propagate_key_to_snapshot_leaf(struct btree_trans *trans,
1575 enum btree_id btree,
1576 struct bkey_s_c interior_k,
1577 u32 leaf_id, struct bpos *new_min_pos)
1579 struct btree_iter iter;
1580 struct bpos pos = interior_k.k->p;
1585 pos.snapshot = leaf_id;
1587 bch2_trans_iter_init(trans, &iter, btree, pos, BTREE_ITER_INTENT);
1588 k = bch2_btree_iter_peek_slot(&iter);
1593 /* key already overwritten in this snapshot? */
1594 if (k.k->p.snapshot != interior_k.k->p.snapshot)
1597 if (bpos_eq(*new_min_pos, POS_MIN)) {
1598 *new_min_pos = k.k->p;
1599 new_min_pos->snapshot = leaf_id;
1602 new = bch2_bkey_make_mut_noupdate(trans, interior_k);
1603 ret = PTR_ERR_OR_ZERO(new);
1607 new->k.p.snapshot = leaf_id;
1608 ret = bch2_trans_update(trans, &iter, new, 0);
1610 bch2_trans_iter_exit(trans, &iter);
1614 int bch2_propagate_key_to_snapshot_leaves(struct btree_trans *trans,
1615 enum btree_id btree,
1617 struct bpos *new_min_pos)
1619 struct bch_fs *c = trans->c;
1623 bch2_bkey_buf_init(&sk);
1624 bch2_bkey_buf_reassemble(&sk, c, k);
1625 k = bkey_i_to_s_c(sk.k);
1627 *new_min_pos = POS_MIN;
1629 for (u32 id = bch2_snapshot_smallest_descendent(c, k.k->p.snapshot);
1630 id < k.k->p.snapshot;
1632 if (!bch2_snapshot_is_ancestor(c, id, k.k->p.snapshot) ||
1633 !bch2_snapshot_is_leaf(c, id))
1636 ret = commit_do(trans, NULL, NULL, 0,
1637 bch2_propagate_key_to_snapshot_leaf(trans, btree, k, id, new_min_pos));
1642 bch2_bkey_buf_exit(&sk, c);
1646 int bch2_snapshots_read(struct bch_fs *c)
1648 struct btree_iter iter;
1652 ret = bch2_trans_run(c,
1653 for_each_btree_key2(&trans, iter, BTREE_ID_snapshots,
1655 bch2_mark_snapshot(&trans, BTREE_ID_snapshots, 0, bkey_s_c_null, k, 0) ?:
1656 bch2_snapshot_set_equiv(&trans, k)) ?:
1657 for_each_btree_key2(&trans, iter, BTREE_ID_snapshots,
1659 (set_is_ancestor_bitmap(c, k.k->p.offset), 0)));
1665 void bch2_fs_snapshots_exit(struct bch_fs *c)
1667 kfree(rcu_dereference_protected(c->snapshots, true));