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(struct bch_fs *c, struct bkey_s_c k,
34 enum bkey_invalid_flags flags,
39 bkey_fsck_err_on(bkey_gt(k.k->p, POS(0, U32_MAX)) ||
40 bkey_lt(k.k->p, POS(0, 1)), c, err,
41 snapshot_tree_pos_bad,
47 int bch2_snapshot_tree_lookup(struct btree_trans *trans, u32 id,
48 struct bch_snapshot_tree *s)
50 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshot_trees, POS(0, id),
51 BTREE_ITER_WITH_UPDATES, snapshot_tree, s);
53 if (bch2_err_matches(ret, ENOENT))
54 ret = -BCH_ERR_ENOENT_snapshot_tree;
58 struct bkey_i_snapshot_tree *
59 __bch2_snapshot_tree_create(struct btree_trans *trans)
61 struct btree_iter iter;
62 int ret = bch2_bkey_get_empty_slot(trans, &iter,
63 BTREE_ID_snapshot_trees, POS(0, U32_MAX));
64 struct bkey_i_snapshot_tree *s_t;
66 if (ret == -BCH_ERR_ENOSPC_btree_slot)
67 ret = -BCH_ERR_ENOSPC_snapshot_tree;
71 s_t = bch2_bkey_alloc(trans, &iter, 0, snapshot_tree);
72 ret = PTR_ERR_OR_ZERO(s_t);
73 bch2_trans_iter_exit(trans, &iter);
74 return ret ? ERR_PTR(ret) : s_t;
77 static int bch2_snapshot_tree_create(struct btree_trans *trans,
78 u32 root_id, u32 subvol_id, u32 *tree_id)
80 struct bkey_i_snapshot_tree *n_tree =
81 __bch2_snapshot_tree_create(trans);
84 return PTR_ERR(n_tree);
86 n_tree->v.master_subvol = cpu_to_le32(subvol_id);
87 n_tree->v.root_snapshot = cpu_to_le32(root_id);
88 *tree_id = n_tree->k.p.offset;
94 static bool bch2_snapshot_is_ancestor_early(struct bch_fs *c, u32 id, u32 ancestor)
96 struct snapshot_table *t;
99 t = rcu_dereference(c->snapshots);
101 while (id && id < ancestor)
102 id = __snapshot_t(t, id)->parent;
105 return id == ancestor;
108 static inline u32 get_ancestor_below(struct snapshot_table *t, u32 id, u32 ancestor)
110 const struct snapshot_t *s = __snapshot_t(t, id);
112 if (s->skip[2] <= ancestor)
114 if (s->skip[1] <= ancestor)
116 if (s->skip[0] <= ancestor)
121 bool __bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
123 struct snapshot_table *t;
126 EBUG_ON(c->recovery_pass_done <= BCH_RECOVERY_PASS_check_snapshots);
129 t = rcu_dereference(c->snapshots);
131 while (id && id < ancestor - IS_ANCESTOR_BITMAP)
132 id = get_ancestor_below(t, id, ancestor);
134 if (id && id < ancestor) {
135 ret = test_bit(ancestor - id - 1, __snapshot_t(t, id)->is_ancestor);
137 EBUG_ON(ret != bch2_snapshot_is_ancestor_early(c, id, ancestor));
139 ret = id == ancestor;
147 static noinline struct snapshot_t *__snapshot_t_mut(struct bch_fs *c, u32 id)
149 size_t idx = U32_MAX - id;
151 struct snapshot_table *new, *old;
153 new_size = max(16UL, roundup_pow_of_two(idx + 1));
155 new = kvzalloc(struct_size(new, s, new_size), GFP_KERNEL);
159 old = rcu_dereference_protected(c->snapshots, true);
162 rcu_dereference_protected(c->snapshots, true)->s,
163 sizeof(new->s[0]) * c->snapshot_table_size);
165 rcu_assign_pointer(c->snapshots, new);
166 c->snapshot_table_size = new_size;
167 kvfree_rcu_mightsleep(old);
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(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;
214 bkey_fsck_err_on(bkey_gt(k.k->p, POS(0, U32_MAX)) ||
215 bkey_lt(k.k->p, POS(0, 1)), c, err,
219 s = bkey_s_c_to_snapshot(k);
221 id = le32_to_cpu(s.v->parent);
222 bkey_fsck_err_on(id && id <= k.k->p.offset, c, err,
224 "bad parent node (%u <= %llu)",
227 bkey_fsck_err_on(le32_to_cpu(s.v->children[0]) < le32_to_cpu(s.v->children[1]), c, err,
228 snapshot_children_not_normalized,
229 "children not normalized");
231 bkey_fsck_err_on(s.v->children[0] && s.v->children[0] == s.v->children[1], c, err,
232 snapshot_child_duplicate,
233 "duplicate child nodes");
235 for (i = 0; i < 2; i++) {
236 id = le32_to_cpu(s.v->children[i]);
238 bkey_fsck_err_on(id >= k.k->p.offset, c, err,
240 "bad child node (%u >= %llu)",
244 if (bkey_val_bytes(k.k) > offsetof(struct bch_snapshot, skip)) {
245 bkey_fsck_err_on(le32_to_cpu(s.v->skip[0]) > le32_to_cpu(s.v->skip[1]) ||
246 le32_to_cpu(s.v->skip[1]) > le32_to_cpu(s.v->skip[2]), c, err,
247 snapshot_skiplist_not_normalized,
248 "skiplist not normalized");
250 for (i = 0; i < ARRAY_SIZE(s.v->skip); i++) {
251 id = le32_to_cpu(s.v->skip[i]);
253 bkey_fsck_err_on(id && id < le32_to_cpu(s.v->parent), c, err,
254 snapshot_skiplist_bad,
255 "bad skiplist node %u", id);
262 static void __set_is_ancestor_bitmap(struct bch_fs *c, u32 id)
264 struct snapshot_t *t = snapshot_t_mut(c, id);
267 while ((parent = bch2_snapshot_parent_early(c, parent)) &&
268 parent - id - 1 < IS_ANCESTOR_BITMAP)
269 __set_bit(parent - id - 1, t->is_ancestor);
272 static void set_is_ancestor_bitmap(struct bch_fs *c, u32 id)
274 mutex_lock(&c->snapshot_table_lock);
275 __set_is_ancestor_bitmap(c, id);
276 mutex_unlock(&c->snapshot_table_lock);
279 int bch2_mark_snapshot(struct btree_trans *trans,
280 enum btree_id btree, unsigned level,
281 struct bkey_s_c old, struct bkey_s_c new,
284 struct bch_fs *c = trans->c;
285 struct snapshot_t *t;
286 u32 id = new.k->p.offset;
289 mutex_lock(&c->snapshot_table_lock);
291 t = snapshot_t_mut(c, id);
293 ret = -BCH_ERR_ENOMEM_mark_snapshot;
297 if (new.k->type == KEY_TYPE_snapshot) {
298 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(new);
300 t->parent = le32_to_cpu(s.v->parent);
301 t->children[0] = le32_to_cpu(s.v->children[0]);
302 t->children[1] = le32_to_cpu(s.v->children[1]);
303 t->subvol = BCH_SNAPSHOT_SUBVOL(s.v) ? le32_to_cpu(s.v->subvol) : 0;
304 t->tree = le32_to_cpu(s.v->tree);
306 if (bkey_val_bytes(s.k) > offsetof(struct bch_snapshot, depth)) {
307 t->depth = le32_to_cpu(s.v->depth);
308 t->skip[0] = le32_to_cpu(s.v->skip[0]);
309 t->skip[1] = le32_to_cpu(s.v->skip[1]);
310 t->skip[2] = le32_to_cpu(s.v->skip[2]);
318 __set_is_ancestor_bitmap(c, id);
320 if (BCH_SNAPSHOT_DELETED(s.v)) {
321 set_bit(BCH_FS_need_delete_dead_snapshots, &c->flags);
322 if (c->curr_recovery_pass > BCH_RECOVERY_PASS_delete_dead_snapshots)
323 bch2_delete_dead_snapshots_async(c);
326 memset(t, 0, sizeof(*t));
329 mutex_unlock(&c->snapshot_table_lock);
333 int bch2_snapshot_lookup(struct btree_trans *trans, u32 id,
334 struct bch_snapshot *s)
336 return bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots, POS(0, id),
337 BTREE_ITER_WITH_UPDATES, snapshot, s);
340 static int bch2_snapshot_live(struct btree_trans *trans, u32 id)
342 struct bch_snapshot v;
348 ret = bch2_snapshot_lookup(trans, id, &v);
349 if (bch2_err_matches(ret, ENOENT))
350 bch_err(trans->c, "snapshot node %u not found", id);
354 return !BCH_SNAPSHOT_DELETED(&v);
358 * If @k is a snapshot with just one live child, it's part of a linear chain,
359 * which we consider to be an equivalence class: and then after snapshot
360 * deletion cleanup, there should only be a single key at a given position in
361 * this equivalence class.
363 * This sets the equivalence class of @k to be the child's equivalence class, if
364 * it's part of such a linear chain: this correctly sets equivalence classes on
365 * startup if we run leaf to root (i.e. in natural key order).
367 static int bch2_snapshot_set_equiv(struct btree_trans *trans, struct bkey_s_c k)
369 struct bch_fs *c = trans->c;
370 unsigned i, nr_live = 0, live_idx = 0;
371 struct bkey_s_c_snapshot snap;
372 u32 id = k.k->p.offset, child[2];
374 if (k.k->type != KEY_TYPE_snapshot)
377 snap = bkey_s_c_to_snapshot(k);
379 child[0] = le32_to_cpu(snap.v->children[0]);
380 child[1] = le32_to_cpu(snap.v->children[1]);
382 for (i = 0; i < 2; i++) {
383 int ret = bch2_snapshot_live(trans, child[i]);
393 mutex_lock(&c->snapshot_table_lock);
395 snapshot_t_mut(c, id)->equiv = nr_live == 1
396 ? snapshot_t_mut(c, child[live_idx])->equiv
399 mutex_unlock(&c->snapshot_table_lock);
406 static u32 bch2_snapshot_child(struct bch_fs *c, u32 id, unsigned child)
408 return snapshot_t(c, id)->children[child];
411 static u32 bch2_snapshot_left_child(struct bch_fs *c, u32 id)
413 return bch2_snapshot_child(c, id, 0);
416 static u32 bch2_snapshot_right_child(struct bch_fs *c, u32 id)
418 return bch2_snapshot_child(c, id, 1);
421 static u32 bch2_snapshot_tree_next(struct bch_fs *c, u32 id)
425 n = bch2_snapshot_left_child(c, id);
429 while ((parent = bch2_snapshot_parent(c, id))) {
430 n = bch2_snapshot_right_child(c, parent);
439 static u32 bch2_snapshot_tree_oldest_subvol(struct bch_fs *c, u32 snapshot_root)
441 u32 id = snapshot_root;
445 s = snapshot_t(c, id)->subvol;
447 if (s && (!subvol || s < subvol))
450 id = bch2_snapshot_tree_next(c, id);
456 static int bch2_snapshot_tree_master_subvol(struct btree_trans *trans,
457 u32 snapshot_root, u32 *subvol_id)
459 struct bch_fs *c = trans->c;
460 struct btree_iter iter;
465 for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN,
467 if (k.k->type != KEY_TYPE_subvolume)
470 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
471 if (!bch2_snapshot_is_ancestor(c, le32_to_cpu(s.v->snapshot), snapshot_root))
473 if (!BCH_SUBVOLUME_SNAP(s.v)) {
474 *subvol_id = s.k->p.offset;
480 bch2_trans_iter_exit(trans, &iter);
482 if (!ret && !found) {
483 struct bkey_i_subvolume *u;
485 *subvol_id = bch2_snapshot_tree_oldest_subvol(c, snapshot_root);
487 u = bch2_bkey_get_mut_typed(trans, &iter,
488 BTREE_ID_subvolumes, POS(0, *subvol_id),
490 ret = PTR_ERR_OR_ZERO(u);
494 SET_BCH_SUBVOLUME_SNAP(&u->v, false);
500 static int check_snapshot_tree(struct btree_trans *trans,
501 struct btree_iter *iter,
504 struct bch_fs *c = trans->c;
505 struct bkey_s_c_snapshot_tree st;
506 struct bch_snapshot s;
507 struct bch_subvolume subvol;
508 struct printbuf buf = PRINTBUF;
512 if (k.k->type != KEY_TYPE_snapshot_tree)
515 st = bkey_s_c_to_snapshot_tree(k);
516 root_id = le32_to_cpu(st.v->root_snapshot);
518 ret = bch2_snapshot_lookup(trans, root_id, &s);
519 if (ret && !bch2_err_matches(ret, ENOENT))
522 if (fsck_err_on(ret ||
523 root_id != bch2_snapshot_root(c, root_id) ||
524 st.k->p.offset != le32_to_cpu(s.tree),
525 c, snapshot_tree_to_missing_snapshot,
526 "snapshot tree points to missing/incorrect snapshot:\n %s",
527 (bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
528 ret = bch2_btree_delete_at(trans, iter, 0);
532 ret = bch2_subvolume_get(trans, le32_to_cpu(st.v->master_subvol),
534 if (ret && !bch2_err_matches(ret, ENOENT))
538 c, snapshot_tree_to_missing_subvol,
539 "snapshot tree points to missing subvolume:\n %s",
540 (printbuf_reset(&buf),
541 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
542 fsck_err_on(!bch2_snapshot_is_ancestor_early(c,
543 le32_to_cpu(subvol.snapshot),
545 c, snapshot_tree_to_wrong_subvol,
546 "snapshot tree points to subvolume that does not point to snapshot in this tree:\n %s",
547 (printbuf_reset(&buf),
548 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
549 fsck_err_on(BCH_SUBVOLUME_SNAP(&subvol),
550 c, snapshot_tree_to_snapshot_subvol,
551 "snapshot tree points to snapshot subvolume:\n %s",
552 (printbuf_reset(&buf),
553 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
554 struct bkey_i_snapshot_tree *u;
557 ret = bch2_snapshot_tree_master_subvol(trans, root_id, &subvol_id);
561 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot_tree);
562 ret = PTR_ERR_OR_ZERO(u);
566 u->v.master_subvol = cpu_to_le32(subvol_id);
567 st = snapshot_tree_i_to_s_c(u);
576 * For each snapshot_tree, make sure it points to the root of a snapshot tree
577 * and that snapshot entry points back to it, or delete it.
579 * And, make sure it points to a subvolume within that snapshot tree, or correct
580 * it to point to the oldest subvolume within that snapshot tree.
582 int bch2_check_snapshot_trees(struct bch_fs *c)
584 int ret = bch2_trans_run(c,
585 for_each_btree_key_commit(trans, iter,
586 BTREE_ID_snapshot_trees, POS_MIN,
587 BTREE_ITER_PREFETCH, k,
588 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
589 check_snapshot_tree(trans, &iter, k)));
595 * Look up snapshot tree for @tree_id and find root,
596 * make sure @snap_id is a descendent:
598 static int snapshot_tree_ptr_good(struct btree_trans *trans,
599 u32 snap_id, u32 tree_id)
601 struct bch_snapshot_tree s_t;
602 int ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
604 if (bch2_err_matches(ret, ENOENT))
609 return bch2_snapshot_is_ancestor_early(trans->c, snap_id, le32_to_cpu(s_t.root_snapshot));
612 u32 bch2_snapshot_skiplist_get(struct bch_fs *c, u32 id)
614 const struct snapshot_t *s;
620 s = snapshot_t(c, id);
622 id = bch2_snapshot_nth_parent(c, id, get_random_u32_below(s->depth));
628 static int snapshot_skiplist_good(struct btree_trans *trans, u32 id, struct bch_snapshot s)
632 for (i = 0; i < 3; i++)
637 if (!bch2_snapshot_is_ancestor_early(trans->c, id, le32_to_cpu(s.skip[i])))
645 * snapshot_tree pointer was incorrect: look up root snapshot node, make sure
646 * its snapshot_tree pointer is correct (allocate new one if necessary), then
647 * update this node's pointer to root node's pointer:
649 static int snapshot_tree_ptr_repair(struct btree_trans *trans,
650 struct btree_iter *iter,
652 struct bch_snapshot *s)
654 struct bch_fs *c = trans->c;
655 struct btree_iter root_iter;
656 struct bch_snapshot_tree s_t;
657 struct bkey_s_c_snapshot root;
658 struct bkey_i_snapshot *u;
659 u32 root_id = bch2_snapshot_root(c, k.k->p.offset), tree_id;
662 root = bch2_bkey_get_iter_typed(trans, &root_iter,
663 BTREE_ID_snapshots, POS(0, root_id),
664 BTREE_ITER_WITH_UPDATES, snapshot);
665 ret = bkey_err(root);
669 tree_id = le32_to_cpu(root.v->tree);
671 ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
672 if (ret && !bch2_err_matches(ret, ENOENT))
675 if (ret || le32_to_cpu(s_t.root_snapshot) != root_id) {
676 u = bch2_bkey_make_mut_typed(trans, &root_iter, &root.s_c, 0, snapshot);
677 ret = PTR_ERR_OR_ZERO(u) ?:
678 bch2_snapshot_tree_create(trans, root_id,
679 bch2_snapshot_tree_oldest_subvol(c, root_id),
684 u->v.tree = cpu_to_le32(tree_id);
685 if (k.k->p.offset == root_id)
689 if (k.k->p.offset != root_id) {
690 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
691 ret = PTR_ERR_OR_ZERO(u);
695 u->v.tree = cpu_to_le32(tree_id);
699 bch2_trans_iter_exit(trans, &root_iter);
703 static int check_snapshot(struct btree_trans *trans,
704 struct btree_iter *iter,
707 struct bch_fs *c = trans->c;
708 struct bch_snapshot s;
709 struct bch_subvolume subvol;
710 struct bch_snapshot v;
711 struct bkey_i_snapshot *u;
712 u32 parent_id = bch2_snapshot_parent_early(c, k.k->p.offset);
714 struct printbuf buf = PRINTBUF;
715 bool should_have_subvol;
719 if (k.k->type != KEY_TYPE_snapshot)
722 memset(&s, 0, sizeof(s));
723 memcpy(&s, k.v, bkey_val_bytes(k.k));
725 id = le32_to_cpu(s.parent);
727 ret = bch2_snapshot_lookup(trans, id, &v);
728 if (bch2_err_matches(ret, ENOENT))
729 bch_err(c, "snapshot with nonexistent parent:\n %s",
730 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
734 if (le32_to_cpu(v.children[0]) != k.k->p.offset &&
735 le32_to_cpu(v.children[1]) != k.k->p.offset) {
736 bch_err(c, "snapshot parent %u missing pointer to child %llu",
743 for (i = 0; i < 2 && s.children[i]; i++) {
744 id = le32_to_cpu(s.children[i]);
746 ret = bch2_snapshot_lookup(trans, id, &v);
747 if (bch2_err_matches(ret, ENOENT))
748 bch_err(c, "snapshot node %llu has nonexistent child %u",
753 if (le32_to_cpu(v.parent) != k.k->p.offset) {
754 bch_err(c, "snapshot child %u has wrong parent (got %u should be %llu)",
755 id, le32_to_cpu(v.parent), k.k->p.offset);
761 should_have_subvol = BCH_SNAPSHOT_SUBVOL(&s) &&
762 !BCH_SNAPSHOT_DELETED(&s);
764 if (should_have_subvol) {
765 id = le32_to_cpu(s.subvol);
766 ret = bch2_subvolume_get(trans, id, 0, false, &subvol);
767 if (bch2_err_matches(ret, ENOENT))
768 bch_err(c, "snapshot points to nonexistent subvolume:\n %s",
769 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
773 if (BCH_SNAPSHOT_SUBVOL(&s) != (le32_to_cpu(subvol.snapshot) == k.k->p.offset)) {
774 bch_err(c, "snapshot node %llu has wrong BCH_SNAPSHOT_SUBVOL",
780 if (fsck_err_on(s.subvol,
781 c, snapshot_should_not_have_subvol,
782 "snapshot should not point to subvol:\n %s",
783 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
784 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
785 ret = PTR_ERR_OR_ZERO(u);
794 ret = snapshot_tree_ptr_good(trans, k.k->p.offset, le32_to_cpu(s.tree));
798 if (fsck_err_on(!ret, c, snapshot_to_bad_snapshot_tree,
799 "snapshot points to missing/incorrect tree:\n %s",
800 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
801 ret = snapshot_tree_ptr_repair(trans, iter, k, &s);
807 real_depth = bch2_snapshot_depth(c, parent_id);
809 if (le32_to_cpu(s.depth) != real_depth &&
810 (c->sb.version_upgrade_complete < bcachefs_metadata_version_snapshot_skiplists ||
811 fsck_err(c, snapshot_bad_depth,
812 "snapshot with incorrect depth field, should be %u:\n %s",
813 real_depth, (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))) {
814 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
815 ret = PTR_ERR_OR_ZERO(u);
819 u->v.depth = cpu_to_le32(real_depth);
823 ret = snapshot_skiplist_good(trans, k.k->p.offset, s);
828 (c->sb.version_upgrade_complete < bcachefs_metadata_version_snapshot_skiplists ||
829 fsck_err(c, snapshot_bad_skiplist,
830 "snapshot with bad skiplist field:\n %s",
831 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))) {
832 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
833 ret = PTR_ERR_OR_ZERO(u);
837 for (i = 0; i < ARRAY_SIZE(u->v.skip); i++)
838 u->v.skip[i] = cpu_to_le32(bch2_snapshot_skiplist_get(c, parent_id));
840 bubble_sort(u->v.skip, ARRAY_SIZE(u->v.skip), cmp_le32);
850 int bch2_check_snapshots(struct bch_fs *c)
853 * We iterate backwards as checking/fixing the depth field requires that
854 * the parent's depth already be correct:
856 int ret = bch2_trans_run(c,
857 for_each_btree_key_reverse_commit(trans, iter,
858 BTREE_ID_snapshots, POS_MAX,
859 BTREE_ITER_PREFETCH, k,
860 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
861 check_snapshot(trans, &iter, k)));
867 * Mark a snapshot as deleted, for future cleanup:
869 int bch2_snapshot_node_set_deleted(struct btree_trans *trans, u32 id)
871 struct btree_iter iter;
872 struct bkey_i_snapshot *s;
875 s = bch2_bkey_get_mut_typed(trans, &iter,
876 BTREE_ID_snapshots, POS(0, id),
878 ret = PTR_ERR_OR_ZERO(s);
880 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT),
881 trans->c, "missing snapshot %u", id);
885 /* already deleted? */
886 if (BCH_SNAPSHOT_DELETED(&s->v))
889 SET_BCH_SNAPSHOT_DELETED(&s->v, true);
890 SET_BCH_SNAPSHOT_SUBVOL(&s->v, false);
893 bch2_trans_iter_exit(trans, &iter);
897 static inline void normalize_snapshot_child_pointers(struct bch_snapshot *s)
899 if (le32_to_cpu(s->children[0]) < le32_to_cpu(s->children[1]))
900 swap(s->children[0], s->children[1]);
903 static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
905 struct bch_fs *c = trans->c;
906 struct btree_iter iter, p_iter = (struct btree_iter) { NULL };
907 struct btree_iter c_iter = (struct btree_iter) { NULL };
908 struct btree_iter tree_iter = (struct btree_iter) { NULL };
909 struct bkey_s_c_snapshot s;
910 u32 parent_id, child_id;
914 s = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_snapshots, POS(0, id),
915 BTREE_ITER_INTENT, snapshot);
917 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
918 "missing snapshot %u", id);
923 BUG_ON(s.v->children[1]);
925 parent_id = le32_to_cpu(s.v->parent);
926 child_id = le32_to_cpu(s.v->children[0]);
929 struct bkey_i_snapshot *parent;
931 parent = bch2_bkey_get_mut_typed(trans, &p_iter,
932 BTREE_ID_snapshots, POS(0, parent_id),
934 ret = PTR_ERR_OR_ZERO(parent);
935 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
936 "missing snapshot %u", parent_id);
940 /* find entry in parent->children for node being deleted */
941 for (i = 0; i < 2; i++)
942 if (le32_to_cpu(parent->v.children[i]) == id)
945 if (bch2_fs_inconsistent_on(i == 2, c,
946 "snapshot %u missing child pointer to %u",
950 parent->v.children[i] = cpu_to_le32(child_id);
952 normalize_snapshot_child_pointers(&parent->v);
956 struct bkey_i_snapshot *child;
958 child = bch2_bkey_get_mut_typed(trans, &c_iter,
959 BTREE_ID_snapshots, POS(0, child_id),
961 ret = PTR_ERR_OR_ZERO(child);
962 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
963 "missing snapshot %u", child_id);
967 child->v.parent = cpu_to_le32(parent_id);
969 if (!child->v.parent) {
970 child->v.skip[0] = 0;
971 child->v.skip[1] = 0;
972 child->v.skip[2] = 0;
978 * We're deleting the root of a snapshot tree: update the
979 * snapshot_tree entry to point to the new root, or delete it if
980 * this is the last snapshot ID in this tree:
982 struct bkey_i_snapshot_tree *s_t;
984 BUG_ON(s.v->children[1]);
986 s_t = bch2_bkey_get_mut_typed(trans, &tree_iter,
987 BTREE_ID_snapshot_trees, POS(0, le32_to_cpu(s.v->tree)),
989 ret = PTR_ERR_OR_ZERO(s_t);
993 if (s.v->children[0]) {
994 s_t->v.root_snapshot = s.v->children[0];
996 s_t->k.type = KEY_TYPE_deleted;
997 set_bkey_val_u64s(&s_t->k, 0);
1001 ret = bch2_btree_delete_at(trans, &iter, 0);
1003 bch2_trans_iter_exit(trans, &tree_iter);
1004 bch2_trans_iter_exit(trans, &p_iter);
1005 bch2_trans_iter_exit(trans, &c_iter);
1006 bch2_trans_iter_exit(trans, &iter);
1010 static int create_snapids(struct btree_trans *trans, u32 parent, u32 tree,
1012 u32 *snapshot_subvols,
1013 unsigned nr_snapids)
1015 struct bch_fs *c = trans->c;
1016 struct btree_iter iter;
1017 struct bkey_i_snapshot *n;
1020 u32 depth = bch2_snapshot_depth(c, parent);
1023 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
1024 POS_MIN, BTREE_ITER_INTENT);
1025 k = bch2_btree_iter_peek(&iter);
1030 for (i = 0; i < nr_snapids; i++) {
1031 k = bch2_btree_iter_prev_slot(&iter);
1036 if (!k.k || !k.k->p.offset) {
1037 ret = -BCH_ERR_ENOSPC_snapshot_create;
1041 n = bch2_bkey_alloc(trans, &iter, 0, snapshot);
1042 ret = PTR_ERR_OR_ZERO(n);
1047 n->v.parent = cpu_to_le32(parent);
1048 n->v.subvol = cpu_to_le32(snapshot_subvols[i]);
1049 n->v.tree = cpu_to_le32(tree);
1050 n->v.depth = cpu_to_le32(depth);
1052 for (j = 0; j < ARRAY_SIZE(n->v.skip); j++)
1053 n->v.skip[j] = cpu_to_le32(bch2_snapshot_skiplist_get(c, parent));
1055 bubble_sort(n->v.skip, ARRAY_SIZE(n->v.skip), cmp_le32);
1056 SET_BCH_SNAPSHOT_SUBVOL(&n->v, true);
1058 ret = bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0,
1059 bkey_s_c_null, bkey_i_to_s_c(&n->k_i), 0);
1063 new_snapids[i] = iter.pos.offset;
1065 mutex_lock(&c->snapshot_table_lock);
1066 snapshot_t_mut(c, new_snapids[i])->equiv = new_snapids[i];
1067 mutex_unlock(&c->snapshot_table_lock);
1070 bch2_trans_iter_exit(trans, &iter);
1075 * Create new snapshot IDs as children of an existing snapshot ID:
1077 static int bch2_snapshot_node_create_children(struct btree_trans *trans, u32 parent,
1079 u32 *snapshot_subvols,
1080 unsigned nr_snapids)
1082 struct btree_iter iter;
1083 struct bkey_i_snapshot *n_parent;
1086 n_parent = bch2_bkey_get_mut_typed(trans, &iter,
1087 BTREE_ID_snapshots, POS(0, parent),
1089 ret = PTR_ERR_OR_ZERO(n_parent);
1090 if (unlikely(ret)) {
1091 if (bch2_err_matches(ret, ENOENT))
1092 bch_err(trans->c, "snapshot %u not found", parent);
1096 if (n_parent->v.children[0] || n_parent->v.children[1]) {
1097 bch_err(trans->c, "Trying to add child snapshot nodes to parent that already has children");
1102 ret = create_snapids(trans, parent, le32_to_cpu(n_parent->v.tree),
1103 new_snapids, snapshot_subvols, nr_snapids);
1107 n_parent->v.children[0] = cpu_to_le32(new_snapids[0]);
1108 n_parent->v.children[1] = cpu_to_le32(new_snapids[1]);
1109 n_parent->v.subvol = 0;
1110 SET_BCH_SNAPSHOT_SUBVOL(&n_parent->v, false);
1112 bch2_trans_iter_exit(trans, &iter);
1117 * Create a snapshot node that is the root of a new tree:
1119 static int bch2_snapshot_node_create_tree(struct btree_trans *trans,
1121 u32 *snapshot_subvols,
1122 unsigned nr_snapids)
1124 struct bkey_i_snapshot_tree *n_tree;
1127 n_tree = __bch2_snapshot_tree_create(trans);
1128 ret = PTR_ERR_OR_ZERO(n_tree) ?:
1129 create_snapids(trans, 0, n_tree->k.p.offset,
1130 new_snapids, snapshot_subvols, nr_snapids);
1134 n_tree->v.master_subvol = cpu_to_le32(snapshot_subvols[0]);
1135 n_tree->v.root_snapshot = cpu_to_le32(new_snapids[0]);
1139 int bch2_snapshot_node_create(struct btree_trans *trans, u32 parent,
1141 u32 *snapshot_subvols,
1142 unsigned nr_snapids)
1144 BUG_ON((parent == 0) != (nr_snapids == 1));
1145 BUG_ON((parent != 0) != (nr_snapids == 2));
1148 ? bch2_snapshot_node_create_children(trans, parent,
1149 new_snapids, snapshot_subvols, nr_snapids)
1150 : bch2_snapshot_node_create_tree(trans,
1151 new_snapids, snapshot_subvols, nr_snapids);
1156 * If we have an unlinked inode in an internal snapshot node, and the inode
1157 * really has been deleted in all child snapshots, how does this get cleaned up?
1159 * first there is the problem of how keys that have been overwritten in all
1160 * child snapshots get deleted (unimplemented?), but inodes may perhaps be
1163 * also: unlinked inode in internal snapshot appears to not be getting deleted
1164 * correctly if inode doesn't exist in leaf snapshots
1168 * for a key in an interior snapshot node that needs work to be done that
1169 * requires it to be mutated: iterate over all descendent leaf nodes and copy
1170 * that key to snapshot leaf nodes, where we can mutate it
1173 static int snapshot_delete_key(struct btree_trans *trans,
1174 struct btree_iter *iter,
1176 snapshot_id_list *deleted,
1177 snapshot_id_list *equiv_seen,
1178 struct bpos *last_pos)
1180 struct bch_fs *c = trans->c;
1181 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
1183 if (!bkey_eq(k.k->p, *last_pos))
1187 if (snapshot_list_has_id(deleted, k.k->p.snapshot) ||
1188 snapshot_list_has_id(equiv_seen, equiv)) {
1189 return bch2_btree_delete_at(trans, iter,
1190 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1192 return snapshot_list_add(c, equiv_seen, equiv);
1196 static int move_key_to_correct_snapshot(struct btree_trans *trans,
1197 struct btree_iter *iter,
1200 struct bch_fs *c = trans->c;
1201 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
1204 * When we have a linear chain of snapshot nodes, we consider
1205 * those to form an equivalence class: we're going to collapse
1206 * them all down to a single node, and keep the leaf-most node -
1207 * which has the same id as the equivalence class id.
1209 * If there are multiple keys in different snapshots at the same
1210 * position, we're only going to keep the one in the newest
1211 * snapshot - the rest have been overwritten and are redundant,
1212 * and for the key we're going to keep we need to move it to the
1213 * equivalance class ID if it's not there already.
1215 if (equiv != k.k->p.snapshot) {
1216 struct bkey_i *new = bch2_bkey_make_mut_noupdate(trans, k);
1217 struct btree_iter new_iter;
1220 ret = PTR_ERR_OR_ZERO(new);
1224 new->k.p.snapshot = equiv;
1226 bch2_trans_iter_init(trans, &new_iter, iter->btree_id, new->k.p,
1227 BTREE_ITER_ALL_SNAPSHOTS|
1231 ret = bch2_btree_iter_traverse(&new_iter) ?:
1232 bch2_trans_update(trans, &new_iter, new,
1233 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
1234 bch2_btree_delete_at(trans, iter,
1235 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1236 bch2_trans_iter_exit(trans, &new_iter);
1244 static int bch2_snapshot_needs_delete(struct btree_trans *trans, struct bkey_s_c k)
1246 struct bkey_s_c_snapshot snap;
1250 if (k.k->type != KEY_TYPE_snapshot)
1253 snap = bkey_s_c_to_snapshot(k);
1254 if (BCH_SNAPSHOT_DELETED(snap.v) ||
1255 BCH_SNAPSHOT_SUBVOL(snap.v))
1258 children[0] = le32_to_cpu(snap.v->children[0]);
1259 children[1] = le32_to_cpu(snap.v->children[1]);
1261 ret = bch2_snapshot_live(trans, children[0]) ?:
1262 bch2_snapshot_live(trans, children[1]);
1269 * For a given snapshot, if it doesn't have a subvolume that points to it, and
1270 * it doesn't have child snapshot nodes - it's now redundant and we can mark it
1273 static int bch2_delete_redundant_snapshot(struct btree_trans *trans, struct bkey_s_c k)
1275 int ret = bch2_snapshot_needs_delete(trans, k);
1279 : bch2_snapshot_node_set_deleted(trans, k.k->p.offset);
1282 static inline u32 bch2_snapshot_nth_parent_skip(struct bch_fs *c, u32 id, u32 n,
1283 snapshot_id_list *skip)
1286 while (snapshot_list_has_id(skip, id))
1287 id = __bch2_snapshot_parent(c, id);
1291 id = __bch2_snapshot_parent(c, id);
1292 } while (snapshot_list_has_id(skip, id));
1299 static int bch2_fix_child_of_deleted_snapshot(struct btree_trans *trans,
1300 struct btree_iter *iter, struct bkey_s_c k,
1301 snapshot_id_list *deleted)
1303 struct bch_fs *c = trans->c;
1304 u32 nr_deleted_ancestors = 0;
1305 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)) {
1339 id = 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 snapshot_id_list deleted = { 0 };
1359 snapshot_id_list deleted_interior = { 0 };
1363 if (!test_and_clear_bit(BCH_FS_need_delete_dead_snapshots, &c->flags))
1366 if (!test_bit(BCH_FS_started, &c->flags)) {
1367 ret = bch2_fs_read_write_early(c);
1368 bch_err_msg(c, ret, "deleting dead snapshots: error going rw");
1373 trans = bch2_trans_get(c);
1376 * For every snapshot node: If we have no live children and it's not
1377 * pointed to by a subvolume, delete it:
1379 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_snapshots,
1382 bch2_delete_redundant_snapshot(trans, k));
1383 bch_err_msg(c, ret, "deleting redundant snapshots");
1387 ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1389 bch2_snapshot_set_equiv(trans, k));
1390 bch_err_msg(c, ret, "in bch2_snapshots_set_equiv");
1394 ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1396 if (k.k->type != KEY_TYPE_snapshot)
1399 BCH_SNAPSHOT_DELETED(bkey_s_c_to_snapshot(k).v)
1400 ? snapshot_list_add(c, &deleted, k.k->p.offset)
1403 bch_err_msg(c, ret, "walking snapshots");
1407 for (id = 0; id < BTREE_ID_NR; id++) {
1408 struct bpos last_pos = POS_MIN;
1409 snapshot_id_list equiv_seen = { 0 };
1410 struct disk_reservation res = { 0 };
1412 if (!btree_type_has_snapshots(id))
1416 * deleted inodes btree is maintained by a trigger on the inodes
1417 * btree - no work for us to do here, and it's not safe to scan
1418 * it because we'll see out of date keys due to the btree write
1421 if (id == BTREE_ID_deleted_inodes)
1424 ret = for_each_btree_key_commit(trans, iter,
1426 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1427 &res, NULL, BCH_TRANS_COMMIT_no_enospc,
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, BCH_TRANS_COMMIT_no_enospc,
1433 move_key_to_correct_snapshot(trans, &iter, k));
1435 bch2_disk_reservation_put(c, &res);
1436 darray_exit(&equiv_seen);
1438 bch_err_msg(c, ret, "deleting keys from dying snapshots");
1443 bch2_trans_unlock(trans);
1444 down_write(&c->snapshot_create_lock);
1446 ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1448 u32 snapshot = k.k->p.offset;
1449 u32 equiv = bch2_snapshot_equiv(c, snapshot);
1452 ? snapshot_list_add(c, &deleted_interior, snapshot)
1456 bch_err_msg(c, ret, "walking snapshots");
1458 goto err_create_lock;
1461 * Fixing children of deleted snapshots can't be done completely
1462 * atomically, if we crash between here and when we delete the interior
1463 * nodes some depth fields will be off:
1465 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_snapshots, POS_MIN,
1466 BTREE_ITER_INTENT, k,
1467 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1468 bch2_fix_child_of_deleted_snapshot(trans, &iter, k, &deleted_interior));
1470 goto err_create_lock;
1472 darray_for_each(deleted, i) {
1473 ret = commit_do(trans, NULL, NULL, 0,
1474 bch2_snapshot_node_delete(trans, *i));
1475 bch_err_msg(c, ret, "deleting snapshot %u", *i);
1477 goto err_create_lock;
1480 darray_for_each(deleted_interior, i) {
1481 ret = commit_do(trans, NULL, NULL, 0,
1482 bch2_snapshot_node_delete(trans, *i));
1483 bch_err_msg(c, ret, "deleting snapshot %u", *i);
1485 goto err_create_lock;
1488 up_write(&c->snapshot_create_lock);
1490 darray_exit(&deleted_interior);
1491 darray_exit(&deleted);
1492 bch2_trans_put(trans);
1497 void bch2_delete_dead_snapshots_work(struct work_struct *work)
1499 struct bch_fs *c = container_of(work, struct bch_fs, snapshot_delete_work);
1501 bch2_delete_dead_snapshots(c);
1502 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1505 void bch2_delete_dead_snapshots_async(struct bch_fs *c)
1507 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_delete_dead_snapshots) &&
1508 !queue_work(c->write_ref_wq, &c->snapshot_delete_work))
1509 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1512 int __bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
1516 struct bch_fs *c = trans->c;
1517 struct btree_iter iter;
1521 bch2_trans_iter_init(trans, &iter, id, pos,
1522 BTREE_ITER_NOT_EXTENTS|
1523 BTREE_ITER_ALL_SNAPSHOTS);
1525 k = bch2_btree_iter_prev(&iter);
1533 if (!bkey_eq(pos, k.k->p))
1536 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, pos.snapshot)) {
1541 bch2_trans_iter_exit(trans, &iter);
1546 static u32 bch2_snapshot_smallest_child(struct bch_fs *c, u32 id)
1548 const struct snapshot_t *s = snapshot_t(c, id);
1550 return s->children[1] ?: s->children[0];
1553 static u32 bch2_snapshot_smallest_descendent(struct bch_fs *c, u32 id)
1557 while ((child = bch2_snapshot_smallest_child(c, id)))
1562 static int bch2_propagate_key_to_snapshot_leaf(struct btree_trans *trans,
1563 enum btree_id btree,
1564 struct bkey_s_c interior_k,
1565 u32 leaf_id, struct bpos *new_min_pos)
1567 struct btree_iter iter;
1568 struct bpos pos = interior_k.k->p;
1573 pos.snapshot = leaf_id;
1575 bch2_trans_iter_init(trans, &iter, btree, pos, BTREE_ITER_INTENT);
1576 k = bch2_btree_iter_peek_slot(&iter);
1581 /* key already overwritten in this snapshot? */
1582 if (k.k->p.snapshot != interior_k.k->p.snapshot)
1585 if (bpos_eq(*new_min_pos, POS_MIN)) {
1586 *new_min_pos = k.k->p;
1587 new_min_pos->snapshot = leaf_id;
1590 new = bch2_bkey_make_mut_noupdate(trans, interior_k);
1591 ret = PTR_ERR_OR_ZERO(new);
1595 new->k.p.snapshot = leaf_id;
1596 ret = bch2_trans_update(trans, &iter, new, 0);
1598 bch2_trans_iter_exit(trans, &iter);
1602 int bch2_propagate_key_to_snapshot_leaves(struct btree_trans *trans,
1603 enum btree_id btree,
1605 struct bpos *new_min_pos)
1607 struct bch_fs *c = trans->c;
1609 u32 restart_count = trans->restart_count;
1612 bch2_bkey_buf_init(&sk);
1613 bch2_bkey_buf_reassemble(&sk, c, k);
1614 k = bkey_i_to_s_c(sk.k);
1616 *new_min_pos = POS_MIN;
1618 for (u32 id = bch2_snapshot_smallest_descendent(c, k.k->p.snapshot);
1619 id < k.k->p.snapshot;
1621 if (!bch2_snapshot_is_ancestor(c, id, k.k->p.snapshot) ||
1622 !bch2_snapshot_is_leaf(c, id))
1625 ret = btree_trans_too_many_iters(trans) ?:
1626 bch2_propagate_key_to_snapshot_leaf(trans, btree, k, id, new_min_pos) ?:
1627 bch2_trans_commit(trans, NULL, NULL, 0);
1628 if (ret && bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
1629 bch2_trans_begin(trans);
1637 bch2_bkey_buf_exit(&sk, c);
1639 return ret ?: trans_was_restarted(trans, restart_count);
1642 static int bch2_check_snapshot_needs_deletion(struct btree_trans *trans, struct bkey_s_c k)
1644 struct bch_fs *c = trans->c;
1645 struct bkey_s_c_snapshot snap;
1648 if (k.k->type != KEY_TYPE_snapshot)
1651 snap = bkey_s_c_to_snapshot(k);
1652 if (BCH_SNAPSHOT_DELETED(snap.v) ||
1653 bch2_snapshot_equiv(c, k.k->p.offset) != k.k->p.offset ||
1654 (ret = bch2_snapshot_needs_delete(trans, k)) > 0) {
1655 set_bit(BCH_FS_need_delete_dead_snapshots, &c->flags);
1662 int bch2_snapshots_read(struct bch_fs *c)
1664 int ret = bch2_trans_run(c,
1665 for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1667 bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0, bkey_s_c_null, k, 0) ?:
1668 bch2_snapshot_set_equiv(trans, k) ?:
1669 bch2_check_snapshot_needs_deletion(trans, k)) ?:
1670 for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1672 (set_is_ancestor_bitmap(c, k.k->p.offset), 0)));
1677 void bch2_fs_snapshots_exit(struct bch_fs *c)
1679 kfree(rcu_dereference_protected(c->snapshots, true));