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 snapshot_table *t, u32 id, u32 ancestor)
96 while (id && id < ancestor)
97 id = __snapshot_t(t, id)->parent;
98 return id == ancestor;
101 static bool bch2_snapshot_is_ancestor_early(struct bch_fs *c, u32 id, u32 ancestor)
104 bool ret = __bch2_snapshot_is_ancestor_early(rcu_dereference(c->snapshots), id, ancestor);
110 static inline u32 get_ancestor_below(struct snapshot_table *t, u32 id, u32 ancestor)
112 const struct snapshot_t *s = __snapshot_t(t, id);
114 if (s->skip[2] <= ancestor)
116 if (s->skip[1] <= ancestor)
118 if (s->skip[0] <= ancestor)
123 bool __bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
128 struct snapshot_table *t = rcu_dereference(c->snapshots);
130 if (unlikely(c->recovery_pass_done <= BCH_RECOVERY_PASS_check_snapshots)) {
131 ret = __bch2_snapshot_is_ancestor_early(t, id, ancestor);
135 while (id && id < ancestor - IS_ANCESTOR_BITMAP)
136 id = get_ancestor_below(t, id, ancestor);
138 if (id && id < ancestor) {
139 ret = test_bit(ancestor - id - 1, __snapshot_t(t, id)->is_ancestor);
141 EBUG_ON(ret != __bch2_snapshot_is_ancestor_early(t, id, ancestor));
143 ret = id == ancestor;
151 static noinline struct snapshot_t *__snapshot_t_mut(struct bch_fs *c, u32 id)
153 size_t idx = U32_MAX - id;
155 struct snapshot_table *new, *old;
157 new_size = max(16UL, roundup_pow_of_two(idx + 1));
159 new = kvzalloc(struct_size(new, s, new_size), GFP_KERNEL);
163 old = rcu_dereference_protected(c->snapshots, true);
166 rcu_dereference_protected(c->snapshots, true)->s,
167 sizeof(new->s[0]) * c->snapshot_table_size);
169 rcu_assign_pointer(c->snapshots, new);
170 c->snapshot_table_size = new_size;
171 kvfree_rcu_mightsleep(old);
173 return &rcu_dereference_protected(c->snapshots, true)->s[idx];
176 static inline struct snapshot_t *snapshot_t_mut(struct bch_fs *c, u32 id)
178 size_t idx = U32_MAX - id;
180 lockdep_assert_held(&c->snapshot_table_lock);
182 if (likely(idx < c->snapshot_table_size))
183 return &rcu_dereference_protected(c->snapshots, true)->s[idx];
185 return __snapshot_t_mut(c, id);
188 void bch2_snapshot_to_text(struct printbuf *out, struct bch_fs *c,
191 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(k);
193 prt_printf(out, "is_subvol %llu deleted %llu parent %10u children %10u %10u subvol %u tree %u",
194 BCH_SNAPSHOT_SUBVOL(s.v),
195 BCH_SNAPSHOT_DELETED(s.v),
196 le32_to_cpu(s.v->parent),
197 le32_to_cpu(s.v->children[0]),
198 le32_to_cpu(s.v->children[1]),
199 le32_to_cpu(s.v->subvol),
200 le32_to_cpu(s.v->tree));
202 if (bkey_val_bytes(k.k) > offsetof(struct bch_snapshot, depth))
203 prt_printf(out, " depth %u skiplist %u %u %u",
204 le32_to_cpu(s.v->depth),
205 le32_to_cpu(s.v->skip[0]),
206 le32_to_cpu(s.v->skip[1]),
207 le32_to_cpu(s.v->skip[2]));
210 int bch2_snapshot_invalid(struct bch_fs *c, struct bkey_s_c k,
211 enum bkey_invalid_flags flags,
212 struct printbuf *err)
214 struct bkey_s_c_snapshot s;
218 bkey_fsck_err_on(bkey_gt(k.k->p, POS(0, U32_MAX)) ||
219 bkey_lt(k.k->p, POS(0, 1)), c, err,
223 s = bkey_s_c_to_snapshot(k);
225 id = le32_to_cpu(s.v->parent);
226 bkey_fsck_err_on(id && id <= k.k->p.offset, c, err,
228 "bad parent node (%u <= %llu)",
231 bkey_fsck_err_on(le32_to_cpu(s.v->children[0]) < le32_to_cpu(s.v->children[1]), c, err,
232 snapshot_children_not_normalized,
233 "children not normalized");
235 bkey_fsck_err_on(s.v->children[0] && s.v->children[0] == s.v->children[1], c, err,
236 snapshot_child_duplicate,
237 "duplicate child nodes");
239 for (i = 0; i < 2; i++) {
240 id = le32_to_cpu(s.v->children[i]);
242 bkey_fsck_err_on(id >= k.k->p.offset, c, err,
244 "bad child node (%u >= %llu)",
248 if (bkey_val_bytes(k.k) > offsetof(struct bch_snapshot, skip)) {
249 bkey_fsck_err_on(le32_to_cpu(s.v->skip[0]) > le32_to_cpu(s.v->skip[1]) ||
250 le32_to_cpu(s.v->skip[1]) > le32_to_cpu(s.v->skip[2]), c, err,
251 snapshot_skiplist_not_normalized,
252 "skiplist not normalized");
254 for (i = 0; i < ARRAY_SIZE(s.v->skip); i++) {
255 id = le32_to_cpu(s.v->skip[i]);
257 bkey_fsck_err_on(id && id < le32_to_cpu(s.v->parent), c, err,
258 snapshot_skiplist_bad,
259 "bad skiplist node %u", id);
266 static void __set_is_ancestor_bitmap(struct bch_fs *c, u32 id)
268 struct snapshot_t *t = snapshot_t_mut(c, id);
271 while ((parent = bch2_snapshot_parent_early(c, parent)) &&
272 parent - id - 1 < IS_ANCESTOR_BITMAP)
273 __set_bit(parent - id - 1, t->is_ancestor);
276 static void set_is_ancestor_bitmap(struct bch_fs *c, u32 id)
278 mutex_lock(&c->snapshot_table_lock);
279 __set_is_ancestor_bitmap(c, id);
280 mutex_unlock(&c->snapshot_table_lock);
283 static int __bch2_mark_snapshot(struct btree_trans *trans,
284 enum btree_id btree, unsigned level,
285 struct bkey_s_c old, struct bkey_s_c new,
288 struct bch_fs *c = trans->c;
289 struct snapshot_t *t;
290 u32 id = new.k->p.offset;
293 mutex_lock(&c->snapshot_table_lock);
295 t = snapshot_t_mut(c, id);
297 ret = -BCH_ERR_ENOMEM_mark_snapshot;
301 if (new.k->type == KEY_TYPE_snapshot) {
302 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(new);
304 t->parent = le32_to_cpu(s.v->parent);
305 t->children[0] = le32_to_cpu(s.v->children[0]);
306 t->children[1] = le32_to_cpu(s.v->children[1]);
307 t->subvol = BCH_SNAPSHOT_SUBVOL(s.v) ? le32_to_cpu(s.v->subvol) : 0;
308 t->tree = le32_to_cpu(s.v->tree);
310 if (bkey_val_bytes(s.k) > offsetof(struct bch_snapshot, depth)) {
311 t->depth = le32_to_cpu(s.v->depth);
312 t->skip[0] = le32_to_cpu(s.v->skip[0]);
313 t->skip[1] = le32_to_cpu(s.v->skip[1]);
314 t->skip[2] = le32_to_cpu(s.v->skip[2]);
322 __set_is_ancestor_bitmap(c, id);
324 if (BCH_SNAPSHOT_DELETED(s.v)) {
325 set_bit(BCH_FS_need_delete_dead_snapshots, &c->flags);
326 if (c->curr_recovery_pass > BCH_RECOVERY_PASS_delete_dead_snapshots)
327 bch2_delete_dead_snapshots_async(c);
330 memset(t, 0, sizeof(*t));
333 mutex_unlock(&c->snapshot_table_lock);
337 int bch2_mark_snapshot(struct btree_trans *trans,
338 enum btree_id btree, unsigned level,
339 struct bkey_s_c old, struct bkey_s new,
342 return __bch2_mark_snapshot(trans, btree, level, old, new.s_c, flags);
345 int bch2_snapshot_lookup(struct btree_trans *trans, u32 id,
346 struct bch_snapshot *s)
348 return bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots, POS(0, id),
349 BTREE_ITER_WITH_UPDATES, snapshot, s);
352 static int bch2_snapshot_live(struct btree_trans *trans, u32 id)
354 struct bch_snapshot v;
360 ret = bch2_snapshot_lookup(trans, id, &v);
361 if (bch2_err_matches(ret, ENOENT))
362 bch_err(trans->c, "snapshot node %u not found", id);
366 return !BCH_SNAPSHOT_DELETED(&v);
370 * If @k is a snapshot with just one live child, it's part of a linear chain,
371 * which we consider to be an equivalence class: and then after snapshot
372 * deletion cleanup, there should only be a single key at a given position in
373 * this equivalence class.
375 * This sets the equivalence class of @k to be the child's equivalence class, if
376 * it's part of such a linear chain: this correctly sets equivalence classes on
377 * startup if we run leaf to root (i.e. in natural key order).
379 static int bch2_snapshot_set_equiv(struct btree_trans *trans, struct bkey_s_c k)
381 struct bch_fs *c = trans->c;
382 unsigned i, nr_live = 0, live_idx = 0;
383 struct bkey_s_c_snapshot snap;
384 u32 id = k.k->p.offset, child[2];
386 if (k.k->type != KEY_TYPE_snapshot)
389 snap = bkey_s_c_to_snapshot(k);
391 child[0] = le32_to_cpu(snap.v->children[0]);
392 child[1] = le32_to_cpu(snap.v->children[1]);
394 for (i = 0; i < 2; i++) {
395 int ret = bch2_snapshot_live(trans, child[i]);
405 mutex_lock(&c->snapshot_table_lock);
407 snapshot_t_mut(c, id)->equiv = nr_live == 1
408 ? snapshot_t_mut(c, child[live_idx])->equiv
411 mutex_unlock(&c->snapshot_table_lock);
418 static u32 bch2_snapshot_child(struct bch_fs *c, u32 id, unsigned child)
420 return snapshot_t(c, id)->children[child];
423 static u32 bch2_snapshot_left_child(struct bch_fs *c, u32 id)
425 return bch2_snapshot_child(c, id, 0);
428 static u32 bch2_snapshot_right_child(struct bch_fs *c, u32 id)
430 return bch2_snapshot_child(c, id, 1);
433 static u32 bch2_snapshot_tree_next(struct bch_fs *c, u32 id)
437 n = bch2_snapshot_left_child(c, id);
441 while ((parent = bch2_snapshot_parent(c, id))) {
442 n = bch2_snapshot_right_child(c, parent);
451 static u32 bch2_snapshot_tree_oldest_subvol(struct bch_fs *c, u32 snapshot_root)
453 u32 id = snapshot_root;
457 s = snapshot_t(c, id)->subvol;
459 if (s && (!subvol || s < subvol))
462 id = bch2_snapshot_tree_next(c, id);
468 static int bch2_snapshot_tree_master_subvol(struct btree_trans *trans,
469 u32 snapshot_root, u32 *subvol_id)
471 struct bch_fs *c = trans->c;
472 struct btree_iter iter;
477 for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN,
479 if (k.k->type != KEY_TYPE_subvolume)
482 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
483 if (!bch2_snapshot_is_ancestor(c, le32_to_cpu(s.v->snapshot), snapshot_root))
485 if (!BCH_SUBVOLUME_SNAP(s.v)) {
486 *subvol_id = s.k->p.offset;
492 bch2_trans_iter_exit(trans, &iter);
494 if (!ret && !found) {
495 struct bkey_i_subvolume *u;
497 *subvol_id = bch2_snapshot_tree_oldest_subvol(c, snapshot_root);
499 u = bch2_bkey_get_mut_typed(trans, &iter,
500 BTREE_ID_subvolumes, POS(0, *subvol_id),
502 ret = PTR_ERR_OR_ZERO(u);
506 SET_BCH_SUBVOLUME_SNAP(&u->v, false);
512 static int check_snapshot_tree(struct btree_trans *trans,
513 struct btree_iter *iter,
516 struct bch_fs *c = trans->c;
517 struct bkey_s_c_snapshot_tree st;
518 struct bch_snapshot s;
519 struct bch_subvolume subvol;
520 struct printbuf buf = PRINTBUF;
524 if (k.k->type != KEY_TYPE_snapshot_tree)
527 st = bkey_s_c_to_snapshot_tree(k);
528 root_id = le32_to_cpu(st.v->root_snapshot);
530 ret = bch2_snapshot_lookup(trans, root_id, &s);
531 if (ret && !bch2_err_matches(ret, ENOENT))
534 if (fsck_err_on(ret ||
535 root_id != bch2_snapshot_root(c, root_id) ||
536 st.k->p.offset != le32_to_cpu(s.tree),
537 c, snapshot_tree_to_missing_snapshot,
538 "snapshot tree points to missing/incorrect snapshot:\n %s",
539 (bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
540 ret = bch2_btree_delete_at(trans, iter, 0);
544 ret = bch2_subvolume_get(trans, le32_to_cpu(st.v->master_subvol),
546 if (ret && !bch2_err_matches(ret, ENOENT))
550 c, snapshot_tree_to_missing_subvol,
551 "snapshot tree points to missing subvolume:\n %s",
552 (printbuf_reset(&buf),
553 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
554 fsck_err_on(!bch2_snapshot_is_ancestor(c,
555 le32_to_cpu(subvol.snapshot),
557 c, snapshot_tree_to_wrong_subvol,
558 "snapshot tree points to subvolume that does not point to snapshot in this tree:\n %s",
559 (printbuf_reset(&buf),
560 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
561 fsck_err_on(BCH_SUBVOLUME_SNAP(&subvol),
562 c, snapshot_tree_to_snapshot_subvol,
563 "snapshot tree points to snapshot subvolume:\n %s",
564 (printbuf_reset(&buf),
565 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
566 struct bkey_i_snapshot_tree *u;
569 ret = bch2_snapshot_tree_master_subvol(trans, root_id, &subvol_id);
573 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot_tree);
574 ret = PTR_ERR_OR_ZERO(u);
578 u->v.master_subvol = cpu_to_le32(subvol_id);
579 st = snapshot_tree_i_to_s_c(u);
588 * For each snapshot_tree, make sure it points to the root of a snapshot tree
589 * and that snapshot entry points back to it, or delete it.
591 * And, make sure it points to a subvolume within that snapshot tree, or correct
592 * it to point to the oldest subvolume within that snapshot tree.
594 int bch2_check_snapshot_trees(struct bch_fs *c)
596 int ret = bch2_trans_run(c,
597 for_each_btree_key_commit(trans, iter,
598 BTREE_ID_snapshot_trees, POS_MIN,
599 BTREE_ITER_PREFETCH, k,
600 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
601 check_snapshot_tree(trans, &iter, k)));
607 * Look up snapshot tree for @tree_id and find root,
608 * make sure @snap_id is a descendent:
610 static int snapshot_tree_ptr_good(struct btree_trans *trans,
611 u32 snap_id, u32 tree_id)
613 struct bch_snapshot_tree s_t;
614 int ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
616 if (bch2_err_matches(ret, ENOENT))
621 return bch2_snapshot_is_ancestor_early(trans->c, snap_id, le32_to_cpu(s_t.root_snapshot));
624 u32 bch2_snapshot_skiplist_get(struct bch_fs *c, u32 id)
626 const struct snapshot_t *s;
632 s = snapshot_t(c, id);
634 id = bch2_snapshot_nth_parent(c, id, get_random_u32_below(s->depth));
640 static int snapshot_skiplist_good(struct btree_trans *trans, u32 id, struct bch_snapshot s)
644 for (i = 0; i < 3; i++)
649 if (!bch2_snapshot_is_ancestor_early(trans->c, id, le32_to_cpu(s.skip[i])))
657 * snapshot_tree pointer was incorrect: look up root snapshot node, make sure
658 * its snapshot_tree pointer is correct (allocate new one if necessary), then
659 * update this node's pointer to root node's pointer:
661 static int snapshot_tree_ptr_repair(struct btree_trans *trans,
662 struct btree_iter *iter,
664 struct bch_snapshot *s)
666 struct bch_fs *c = trans->c;
667 struct btree_iter root_iter;
668 struct bch_snapshot_tree s_t;
669 struct bkey_s_c_snapshot root;
670 struct bkey_i_snapshot *u;
671 u32 root_id = bch2_snapshot_root(c, k.k->p.offset), tree_id;
674 root = bch2_bkey_get_iter_typed(trans, &root_iter,
675 BTREE_ID_snapshots, POS(0, root_id),
676 BTREE_ITER_WITH_UPDATES, snapshot);
677 ret = bkey_err(root);
681 tree_id = le32_to_cpu(root.v->tree);
683 ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
684 if (ret && !bch2_err_matches(ret, ENOENT))
687 if (ret || le32_to_cpu(s_t.root_snapshot) != root_id) {
688 u = bch2_bkey_make_mut_typed(trans, &root_iter, &root.s_c, 0, snapshot);
689 ret = PTR_ERR_OR_ZERO(u) ?:
690 bch2_snapshot_tree_create(trans, root_id,
691 bch2_snapshot_tree_oldest_subvol(c, root_id),
696 u->v.tree = cpu_to_le32(tree_id);
697 if (k.k->p.offset == root_id)
701 if (k.k->p.offset != root_id) {
702 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
703 ret = PTR_ERR_OR_ZERO(u);
707 u->v.tree = cpu_to_le32(tree_id);
711 bch2_trans_iter_exit(trans, &root_iter);
715 static int check_snapshot(struct btree_trans *trans,
716 struct btree_iter *iter,
719 struct bch_fs *c = trans->c;
720 struct bch_snapshot s;
721 struct bch_subvolume subvol;
722 struct bch_snapshot v;
723 struct bkey_i_snapshot *u;
724 u32 parent_id = bch2_snapshot_parent_early(c, k.k->p.offset);
726 struct printbuf buf = PRINTBUF;
727 bool should_have_subvol;
731 if (k.k->type != KEY_TYPE_snapshot)
734 memset(&s, 0, sizeof(s));
735 memcpy(&s, k.v, min(sizeof(s), bkey_val_bytes(k.k)));
737 id = le32_to_cpu(s.parent);
739 ret = bch2_snapshot_lookup(trans, id, &v);
740 if (bch2_err_matches(ret, ENOENT))
741 bch_err(c, "snapshot with nonexistent parent:\n %s",
742 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
746 if (le32_to_cpu(v.children[0]) != k.k->p.offset &&
747 le32_to_cpu(v.children[1]) != k.k->p.offset) {
748 bch_err(c, "snapshot parent %u missing pointer to child %llu",
755 for (i = 0; i < 2 && s.children[i]; i++) {
756 id = le32_to_cpu(s.children[i]);
758 ret = bch2_snapshot_lookup(trans, id, &v);
759 if (bch2_err_matches(ret, ENOENT))
760 bch_err(c, "snapshot node %llu has nonexistent child %u",
765 if (le32_to_cpu(v.parent) != k.k->p.offset) {
766 bch_err(c, "snapshot child %u has wrong parent (got %u should be %llu)",
767 id, le32_to_cpu(v.parent), k.k->p.offset);
773 should_have_subvol = BCH_SNAPSHOT_SUBVOL(&s) &&
774 !BCH_SNAPSHOT_DELETED(&s);
776 if (should_have_subvol) {
777 id = le32_to_cpu(s.subvol);
778 ret = bch2_subvolume_get(trans, id, 0, false, &subvol);
779 if (bch2_err_matches(ret, ENOENT))
780 bch_err(c, "snapshot points to nonexistent subvolume:\n %s",
781 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
785 if (BCH_SNAPSHOT_SUBVOL(&s) != (le32_to_cpu(subvol.snapshot) == k.k->p.offset)) {
786 bch_err(c, "snapshot node %llu has wrong BCH_SNAPSHOT_SUBVOL",
792 if (fsck_err_on(s.subvol,
793 c, snapshot_should_not_have_subvol,
794 "snapshot should not point to subvol:\n %s",
795 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
796 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
797 ret = PTR_ERR_OR_ZERO(u);
806 ret = snapshot_tree_ptr_good(trans, k.k->p.offset, le32_to_cpu(s.tree));
810 if (fsck_err_on(!ret, c, snapshot_to_bad_snapshot_tree,
811 "snapshot points to missing/incorrect tree:\n %s",
812 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
813 ret = snapshot_tree_ptr_repair(trans, iter, k, &s);
819 real_depth = bch2_snapshot_depth(c, parent_id);
821 if (fsck_err_on(le32_to_cpu(s.depth) != real_depth,
822 c, snapshot_bad_depth,
823 "snapshot with incorrect depth field, should be %u:\n %s",
824 real_depth, (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
825 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
826 ret = PTR_ERR_OR_ZERO(u);
830 u->v.depth = cpu_to_le32(real_depth);
834 ret = snapshot_skiplist_good(trans, k.k->p.offset, s);
838 if (fsck_err_on(!ret, c, snapshot_bad_skiplist,
839 "snapshot with bad skiplist field:\n %s",
840 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
841 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
842 ret = PTR_ERR_OR_ZERO(u);
846 for (i = 0; i < ARRAY_SIZE(u->v.skip); i++)
847 u->v.skip[i] = cpu_to_le32(bch2_snapshot_skiplist_get(c, parent_id));
849 bubble_sort(u->v.skip, ARRAY_SIZE(u->v.skip), cmp_le32);
859 int bch2_check_snapshots(struct bch_fs *c)
862 * We iterate backwards as checking/fixing the depth field requires that
863 * the parent's depth already be correct:
865 int ret = bch2_trans_run(c,
866 for_each_btree_key_reverse_commit(trans, iter,
867 BTREE_ID_snapshots, POS_MAX,
868 BTREE_ITER_PREFETCH, k,
869 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
870 check_snapshot(trans, &iter, k)));
876 * Mark a snapshot as deleted, for future cleanup:
878 int bch2_snapshot_node_set_deleted(struct btree_trans *trans, u32 id)
880 struct btree_iter iter;
881 struct bkey_i_snapshot *s;
884 s = bch2_bkey_get_mut_typed(trans, &iter,
885 BTREE_ID_snapshots, POS(0, id),
887 ret = PTR_ERR_OR_ZERO(s);
889 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT),
890 trans->c, "missing snapshot %u", id);
894 /* already deleted? */
895 if (BCH_SNAPSHOT_DELETED(&s->v))
898 SET_BCH_SNAPSHOT_DELETED(&s->v, true);
899 SET_BCH_SNAPSHOT_SUBVOL(&s->v, false);
902 bch2_trans_iter_exit(trans, &iter);
906 static inline void normalize_snapshot_child_pointers(struct bch_snapshot *s)
908 if (le32_to_cpu(s->children[0]) < le32_to_cpu(s->children[1]))
909 swap(s->children[0], s->children[1]);
912 static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
914 struct bch_fs *c = trans->c;
915 struct btree_iter iter, p_iter = (struct btree_iter) { NULL };
916 struct btree_iter c_iter = (struct btree_iter) { NULL };
917 struct btree_iter tree_iter = (struct btree_iter) { NULL };
918 struct bkey_s_c_snapshot s;
919 u32 parent_id, child_id;
923 s = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_snapshots, POS(0, id),
924 BTREE_ITER_INTENT, snapshot);
926 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
927 "missing snapshot %u", id);
932 BUG_ON(s.v->children[1]);
934 parent_id = le32_to_cpu(s.v->parent);
935 child_id = le32_to_cpu(s.v->children[0]);
938 struct bkey_i_snapshot *parent;
940 parent = bch2_bkey_get_mut_typed(trans, &p_iter,
941 BTREE_ID_snapshots, POS(0, parent_id),
943 ret = PTR_ERR_OR_ZERO(parent);
944 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
945 "missing snapshot %u", parent_id);
949 /* find entry in parent->children for node being deleted */
950 for (i = 0; i < 2; i++)
951 if (le32_to_cpu(parent->v.children[i]) == id)
954 if (bch2_fs_inconsistent_on(i == 2, c,
955 "snapshot %u missing child pointer to %u",
959 parent->v.children[i] = cpu_to_le32(child_id);
961 normalize_snapshot_child_pointers(&parent->v);
965 struct bkey_i_snapshot *child;
967 child = bch2_bkey_get_mut_typed(trans, &c_iter,
968 BTREE_ID_snapshots, POS(0, child_id),
970 ret = PTR_ERR_OR_ZERO(child);
971 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
972 "missing snapshot %u", child_id);
976 child->v.parent = cpu_to_le32(parent_id);
978 if (!child->v.parent) {
979 child->v.skip[0] = 0;
980 child->v.skip[1] = 0;
981 child->v.skip[2] = 0;
987 * We're deleting the root of a snapshot tree: update the
988 * snapshot_tree entry to point to the new root, or delete it if
989 * this is the last snapshot ID in this tree:
991 struct bkey_i_snapshot_tree *s_t;
993 BUG_ON(s.v->children[1]);
995 s_t = bch2_bkey_get_mut_typed(trans, &tree_iter,
996 BTREE_ID_snapshot_trees, POS(0, le32_to_cpu(s.v->tree)),
998 ret = PTR_ERR_OR_ZERO(s_t);
1002 if (s.v->children[0]) {
1003 s_t->v.root_snapshot = s.v->children[0];
1005 s_t->k.type = KEY_TYPE_deleted;
1006 set_bkey_val_u64s(&s_t->k, 0);
1010 ret = bch2_btree_delete_at(trans, &iter, 0);
1012 bch2_trans_iter_exit(trans, &tree_iter);
1013 bch2_trans_iter_exit(trans, &p_iter);
1014 bch2_trans_iter_exit(trans, &c_iter);
1015 bch2_trans_iter_exit(trans, &iter);
1019 static int create_snapids(struct btree_trans *trans, u32 parent, u32 tree,
1021 u32 *snapshot_subvols,
1022 unsigned nr_snapids)
1024 struct bch_fs *c = trans->c;
1025 struct btree_iter iter;
1026 struct bkey_i_snapshot *n;
1029 u32 depth = bch2_snapshot_depth(c, parent);
1032 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
1033 POS_MIN, BTREE_ITER_INTENT);
1034 k = bch2_btree_iter_peek(&iter);
1039 for (i = 0; i < nr_snapids; i++) {
1040 k = bch2_btree_iter_prev_slot(&iter);
1045 if (!k.k || !k.k->p.offset) {
1046 ret = -BCH_ERR_ENOSPC_snapshot_create;
1050 n = bch2_bkey_alloc(trans, &iter, 0, snapshot);
1051 ret = PTR_ERR_OR_ZERO(n);
1056 n->v.parent = cpu_to_le32(parent);
1057 n->v.subvol = cpu_to_le32(snapshot_subvols[i]);
1058 n->v.tree = cpu_to_le32(tree);
1059 n->v.depth = cpu_to_le32(depth);
1060 n->v.btime.lo = cpu_to_le64(bch2_current_time(c));
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;
1076 mutex_lock(&c->snapshot_table_lock);
1077 snapshot_t_mut(c, new_snapids[i])->equiv = new_snapids[i];
1078 mutex_unlock(&c->snapshot_table_lock);
1081 bch2_trans_iter_exit(trans, &iter);
1086 * Create new snapshot IDs as children of an existing snapshot ID:
1088 static int bch2_snapshot_node_create_children(struct btree_trans *trans, u32 parent,
1090 u32 *snapshot_subvols,
1091 unsigned nr_snapids)
1093 struct btree_iter iter;
1094 struct bkey_i_snapshot *n_parent;
1097 n_parent = bch2_bkey_get_mut_typed(trans, &iter,
1098 BTREE_ID_snapshots, POS(0, parent),
1100 ret = PTR_ERR_OR_ZERO(n_parent);
1101 if (unlikely(ret)) {
1102 if (bch2_err_matches(ret, ENOENT))
1103 bch_err(trans->c, "snapshot %u not found", parent);
1107 if (n_parent->v.children[0] || n_parent->v.children[1]) {
1108 bch_err(trans->c, "Trying to add child snapshot nodes to parent that already has children");
1113 ret = create_snapids(trans, parent, le32_to_cpu(n_parent->v.tree),
1114 new_snapids, snapshot_subvols, nr_snapids);
1118 n_parent->v.children[0] = cpu_to_le32(new_snapids[0]);
1119 n_parent->v.children[1] = cpu_to_le32(new_snapids[1]);
1120 n_parent->v.subvol = 0;
1121 SET_BCH_SNAPSHOT_SUBVOL(&n_parent->v, false);
1123 bch2_trans_iter_exit(trans, &iter);
1128 * Create a snapshot node that is the root of a new tree:
1130 static int bch2_snapshot_node_create_tree(struct btree_trans *trans,
1132 u32 *snapshot_subvols,
1133 unsigned nr_snapids)
1135 struct bkey_i_snapshot_tree *n_tree;
1138 n_tree = __bch2_snapshot_tree_create(trans);
1139 ret = PTR_ERR_OR_ZERO(n_tree) ?:
1140 create_snapids(trans, 0, n_tree->k.p.offset,
1141 new_snapids, snapshot_subvols, nr_snapids);
1145 n_tree->v.master_subvol = cpu_to_le32(snapshot_subvols[0]);
1146 n_tree->v.root_snapshot = cpu_to_le32(new_snapids[0]);
1150 int bch2_snapshot_node_create(struct btree_trans *trans, u32 parent,
1152 u32 *snapshot_subvols,
1153 unsigned nr_snapids)
1155 BUG_ON((parent == 0) != (nr_snapids == 1));
1156 BUG_ON((parent != 0) != (nr_snapids == 2));
1159 ? bch2_snapshot_node_create_children(trans, parent,
1160 new_snapids, snapshot_subvols, nr_snapids)
1161 : bch2_snapshot_node_create_tree(trans,
1162 new_snapids, snapshot_subvols, nr_snapids);
1167 * If we have an unlinked inode in an internal snapshot node, and the inode
1168 * really has been deleted in all child snapshots, how does this get cleaned up?
1170 * first there is the problem of how keys that have been overwritten in all
1171 * child snapshots get deleted (unimplemented?), but inodes may perhaps be
1174 * also: unlinked inode in internal snapshot appears to not be getting deleted
1175 * correctly if inode doesn't exist in leaf snapshots
1179 * for a key in an interior snapshot node that needs work to be done that
1180 * requires it to be mutated: iterate over all descendent leaf nodes and copy
1181 * that key to snapshot leaf nodes, where we can mutate it
1184 static int snapshot_delete_key(struct btree_trans *trans,
1185 struct btree_iter *iter,
1187 snapshot_id_list *deleted,
1188 snapshot_id_list *equiv_seen,
1189 struct bpos *last_pos)
1191 struct bch_fs *c = trans->c;
1192 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
1194 if (!bkey_eq(k.k->p, *last_pos))
1198 if (snapshot_list_has_id(deleted, k.k->p.snapshot) ||
1199 snapshot_list_has_id(equiv_seen, equiv)) {
1200 return bch2_btree_delete_at(trans, iter,
1201 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1203 return snapshot_list_add(c, equiv_seen, equiv);
1207 static int move_key_to_correct_snapshot(struct btree_trans *trans,
1208 struct btree_iter *iter,
1211 struct bch_fs *c = trans->c;
1212 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
1215 * When we have a linear chain of snapshot nodes, we consider
1216 * those to form an equivalence class: we're going to collapse
1217 * them all down to a single node, and keep the leaf-most node -
1218 * which has the same id as the equivalence class id.
1220 * If there are multiple keys in different snapshots at the same
1221 * position, we're only going to keep the one in the newest
1222 * snapshot - the rest have been overwritten and are redundant,
1223 * and for the key we're going to keep we need to move it to the
1224 * equivalance class ID if it's not there already.
1226 if (equiv != k.k->p.snapshot) {
1227 struct bkey_i *new = bch2_bkey_make_mut_noupdate(trans, k);
1228 struct btree_iter new_iter;
1231 ret = PTR_ERR_OR_ZERO(new);
1235 new->k.p.snapshot = equiv;
1237 bch2_trans_iter_init(trans, &new_iter, iter->btree_id, new->k.p,
1238 BTREE_ITER_ALL_SNAPSHOTS|
1242 ret = bch2_btree_iter_traverse(&new_iter) ?:
1243 bch2_trans_update(trans, &new_iter, new,
1244 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
1245 bch2_btree_delete_at(trans, iter,
1246 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1247 bch2_trans_iter_exit(trans, &new_iter);
1255 static int bch2_snapshot_needs_delete(struct btree_trans *trans, struct bkey_s_c k)
1257 struct bkey_s_c_snapshot snap;
1261 if (k.k->type != KEY_TYPE_snapshot)
1264 snap = bkey_s_c_to_snapshot(k);
1265 if (BCH_SNAPSHOT_DELETED(snap.v) ||
1266 BCH_SNAPSHOT_SUBVOL(snap.v))
1269 children[0] = le32_to_cpu(snap.v->children[0]);
1270 children[1] = le32_to_cpu(snap.v->children[1]);
1272 ret = bch2_snapshot_live(trans, children[0]) ?:
1273 bch2_snapshot_live(trans, children[1]);
1280 * For a given snapshot, if it doesn't have a subvolume that points to it, and
1281 * it doesn't have child snapshot nodes - it's now redundant and we can mark it
1284 static int bch2_delete_redundant_snapshot(struct btree_trans *trans, struct bkey_s_c k)
1286 int ret = bch2_snapshot_needs_delete(trans, k);
1290 : bch2_snapshot_node_set_deleted(trans, k.k->p.offset);
1293 static inline u32 bch2_snapshot_nth_parent_skip(struct bch_fs *c, u32 id, u32 n,
1294 snapshot_id_list *skip)
1297 while (snapshot_list_has_id(skip, id))
1298 id = __bch2_snapshot_parent(c, id);
1302 id = __bch2_snapshot_parent(c, id);
1303 } while (snapshot_list_has_id(skip, id));
1310 static int bch2_fix_child_of_deleted_snapshot(struct btree_trans *trans,
1311 struct btree_iter *iter, struct bkey_s_c k,
1312 snapshot_id_list *deleted)
1314 struct bch_fs *c = trans->c;
1315 u32 nr_deleted_ancestors = 0;
1316 struct bkey_i_snapshot *s;
1319 if (k.k->type != KEY_TYPE_snapshot)
1322 if (snapshot_list_has_id(deleted, k.k->p.offset))
1325 s = bch2_bkey_make_mut_noupdate_typed(trans, k, snapshot);
1326 ret = PTR_ERR_OR_ZERO(s);
1330 darray_for_each(*deleted, i)
1331 nr_deleted_ancestors += bch2_snapshot_is_ancestor(c, s->k.p.offset, *i);
1333 if (!nr_deleted_ancestors)
1336 le32_add_cpu(&s->v.depth, -nr_deleted_ancestors);
1343 u32 depth = le32_to_cpu(s->v.depth);
1344 u32 parent = bch2_snapshot_parent(c, s->k.p.offset);
1346 for (unsigned j = 0; j < ARRAY_SIZE(s->v.skip); j++) {
1347 u32 id = le32_to_cpu(s->v.skip[j]);
1349 if (snapshot_list_has_id(deleted, id)) {
1350 id = bch2_snapshot_nth_parent_skip(c,
1353 ? get_random_u32_below(depth - 1)
1356 s->v.skip[j] = cpu_to_le32(id);
1360 bubble_sort(s->v.skip, ARRAY_SIZE(s->v.skip), cmp_le32);
1363 return bch2_trans_update(trans, iter, &s->k_i, 0);
1366 int bch2_delete_dead_snapshots(struct bch_fs *c)
1368 struct btree_trans *trans;
1369 snapshot_id_list deleted = { 0 };
1370 snapshot_id_list deleted_interior = { 0 };
1374 if (!test_and_clear_bit(BCH_FS_need_delete_dead_snapshots, &c->flags))
1377 if (!test_bit(BCH_FS_started, &c->flags)) {
1378 ret = bch2_fs_read_write_early(c);
1379 bch_err_msg(c, ret, "deleting dead snapshots: error going rw");
1384 trans = bch2_trans_get(c);
1387 * For every snapshot node: If we have no live children and it's not
1388 * pointed to by a subvolume, delete it:
1390 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_snapshots,
1393 bch2_delete_redundant_snapshot(trans, k));
1394 bch_err_msg(c, ret, "deleting redundant snapshots");
1398 ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1400 bch2_snapshot_set_equiv(trans, k));
1401 bch_err_msg(c, ret, "in bch2_snapshots_set_equiv");
1405 ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1407 if (k.k->type != KEY_TYPE_snapshot)
1410 BCH_SNAPSHOT_DELETED(bkey_s_c_to_snapshot(k).v)
1411 ? snapshot_list_add(c, &deleted, k.k->p.offset)
1414 bch_err_msg(c, ret, "walking snapshots");
1418 for (id = 0; id < BTREE_ID_NR; id++) {
1419 struct bpos last_pos = POS_MIN;
1420 snapshot_id_list equiv_seen = { 0 };
1421 struct disk_reservation res = { 0 };
1423 if (!btree_type_has_snapshots(id))
1427 * deleted inodes btree is maintained by a trigger on the inodes
1428 * btree - no work for us to do here, and it's not safe to scan
1429 * it because we'll see out of date keys due to the btree write
1432 if (id == BTREE_ID_deleted_inodes)
1435 ret = for_each_btree_key_commit(trans, iter,
1437 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1438 &res, NULL, BCH_TRANS_COMMIT_no_enospc,
1439 snapshot_delete_key(trans, &iter, k, &deleted, &equiv_seen, &last_pos)) ?:
1440 for_each_btree_key_commit(trans, iter,
1442 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1443 &res, NULL, BCH_TRANS_COMMIT_no_enospc,
1444 move_key_to_correct_snapshot(trans, &iter, k));
1446 bch2_disk_reservation_put(c, &res);
1447 darray_exit(&equiv_seen);
1449 bch_err_msg(c, ret, "deleting keys from dying snapshots");
1454 bch2_trans_unlock(trans);
1455 down_write(&c->snapshot_create_lock);
1457 ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1459 u32 snapshot = k.k->p.offset;
1460 u32 equiv = bch2_snapshot_equiv(c, snapshot);
1463 ? snapshot_list_add(c, &deleted_interior, snapshot)
1467 bch_err_msg(c, ret, "walking snapshots");
1469 goto err_create_lock;
1472 * Fixing children of deleted snapshots can't be done completely
1473 * atomically, if we crash between here and when we delete the interior
1474 * nodes some depth fields will be off:
1476 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_snapshots, POS_MIN,
1477 BTREE_ITER_INTENT, k,
1478 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1479 bch2_fix_child_of_deleted_snapshot(trans, &iter, k, &deleted_interior));
1481 goto err_create_lock;
1483 darray_for_each(deleted, i) {
1484 ret = commit_do(trans, NULL, NULL, 0,
1485 bch2_snapshot_node_delete(trans, *i));
1486 bch_err_msg(c, ret, "deleting snapshot %u", *i);
1488 goto err_create_lock;
1491 darray_for_each(deleted_interior, i) {
1492 ret = commit_do(trans, NULL, NULL, 0,
1493 bch2_snapshot_node_delete(trans, *i));
1494 bch_err_msg(c, ret, "deleting snapshot %u", *i);
1496 goto err_create_lock;
1499 up_write(&c->snapshot_create_lock);
1501 darray_exit(&deleted_interior);
1502 darray_exit(&deleted);
1503 bch2_trans_put(trans);
1508 void bch2_delete_dead_snapshots_work(struct work_struct *work)
1510 struct bch_fs *c = container_of(work, struct bch_fs, snapshot_delete_work);
1512 bch2_delete_dead_snapshots(c);
1513 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1516 void bch2_delete_dead_snapshots_async(struct bch_fs *c)
1518 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_delete_dead_snapshots) &&
1519 !queue_work(c->write_ref_wq, &c->snapshot_delete_work))
1520 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1523 int __bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
1527 struct bch_fs *c = trans->c;
1528 struct btree_iter iter;
1532 bch2_trans_iter_init(trans, &iter, id, pos,
1533 BTREE_ITER_NOT_EXTENTS|
1534 BTREE_ITER_ALL_SNAPSHOTS);
1536 k = bch2_btree_iter_prev(&iter);
1544 if (!bkey_eq(pos, k.k->p))
1547 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, pos.snapshot)) {
1552 bch2_trans_iter_exit(trans, &iter);
1557 static u32 bch2_snapshot_smallest_child(struct bch_fs *c, u32 id)
1559 const struct snapshot_t *s = snapshot_t(c, id);
1561 return s->children[1] ?: s->children[0];
1564 static u32 bch2_snapshot_smallest_descendent(struct bch_fs *c, u32 id)
1568 while ((child = bch2_snapshot_smallest_child(c, id)))
1573 static int bch2_propagate_key_to_snapshot_leaf(struct btree_trans *trans,
1574 enum btree_id btree,
1575 struct bkey_s_c interior_k,
1576 u32 leaf_id, struct bpos *new_min_pos)
1578 struct btree_iter iter;
1579 struct bpos pos = interior_k.k->p;
1584 pos.snapshot = leaf_id;
1586 bch2_trans_iter_init(trans, &iter, btree, pos, BTREE_ITER_INTENT);
1587 k = bch2_btree_iter_peek_slot(&iter);
1592 /* key already overwritten in this snapshot? */
1593 if (k.k->p.snapshot != interior_k.k->p.snapshot)
1596 if (bpos_eq(*new_min_pos, POS_MIN)) {
1597 *new_min_pos = k.k->p;
1598 new_min_pos->snapshot = leaf_id;
1601 new = bch2_bkey_make_mut_noupdate(trans, interior_k);
1602 ret = PTR_ERR_OR_ZERO(new);
1606 new->k.p.snapshot = leaf_id;
1607 ret = bch2_trans_update(trans, &iter, new, 0);
1609 bch2_trans_iter_exit(trans, &iter);
1613 int bch2_propagate_key_to_snapshot_leaves(struct btree_trans *trans,
1614 enum btree_id btree,
1616 struct bpos *new_min_pos)
1618 struct bch_fs *c = trans->c;
1620 u32 restart_count = trans->restart_count;
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 = btree_trans_too_many_iters(trans) ?:
1637 bch2_propagate_key_to_snapshot_leaf(trans, btree, k, id, new_min_pos) ?:
1638 bch2_trans_commit(trans, NULL, NULL, 0);
1639 if (ret && bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
1640 bch2_trans_begin(trans);
1648 bch2_bkey_buf_exit(&sk, c);
1650 return ret ?: trans_was_restarted(trans, restart_count);
1653 static int bch2_check_snapshot_needs_deletion(struct btree_trans *trans, struct bkey_s_c k)
1655 struct bch_fs *c = trans->c;
1656 struct bkey_s_c_snapshot snap;
1659 if (k.k->type != KEY_TYPE_snapshot)
1662 snap = bkey_s_c_to_snapshot(k);
1663 if (BCH_SNAPSHOT_DELETED(snap.v) ||
1664 bch2_snapshot_equiv(c, k.k->p.offset) != k.k->p.offset ||
1665 (ret = bch2_snapshot_needs_delete(trans, k)) > 0) {
1666 set_bit(BCH_FS_need_delete_dead_snapshots, &c->flags);
1673 int bch2_snapshots_read(struct bch_fs *c)
1675 int ret = bch2_trans_run(c,
1676 for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1678 __bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0, bkey_s_c_null, k, 0) ?:
1679 bch2_snapshot_set_equiv(trans, k) ?:
1680 bch2_check_snapshot_needs_deletion(trans, k)) ?:
1681 for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1683 (set_is_ancestor_bitmap(c, k.k->p.offset), 0)));
1688 void bch2_fs_snapshots_exit(struct bch_fs *c)
1690 kvfree(rcu_dereference_protected(c->snapshots, true));