1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_key_cache.h"
6 #include "btree_update.h"
11 #include "recovery_passes.h"
14 #include <linux/random.h>
19 * Keys in BTREE_ID_snapshot_trees identify a whole tree of snapshot nodes; they
20 * exist to provide a stable identifier for the whole lifetime of a snapshot
24 void bch2_snapshot_tree_to_text(struct printbuf *out, struct bch_fs *c,
27 struct bkey_s_c_snapshot_tree t = bkey_s_c_to_snapshot_tree(k);
29 prt_printf(out, "subvol %u root snapshot %u",
30 le32_to_cpu(t.v->master_subvol),
31 le32_to_cpu(t.v->root_snapshot));
34 int bch2_snapshot_tree_invalid(struct bch_fs *c, struct bkey_s_c k,
35 enum bkey_invalid_flags flags,
40 bkey_fsck_err_on(bkey_gt(k.k->p, POS(0, U32_MAX)) ||
41 bkey_lt(k.k->p, POS(0, 1)), c, err,
42 snapshot_tree_pos_bad,
48 int bch2_snapshot_tree_lookup(struct btree_trans *trans, u32 id,
49 struct bch_snapshot_tree *s)
51 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshot_trees, POS(0, id),
52 BTREE_ITER_WITH_UPDATES, snapshot_tree, s);
54 if (bch2_err_matches(ret, ENOENT))
55 ret = -BCH_ERR_ENOENT_snapshot_tree;
59 struct bkey_i_snapshot_tree *
60 __bch2_snapshot_tree_create(struct btree_trans *trans)
62 struct btree_iter iter;
63 int ret = bch2_bkey_get_empty_slot(trans, &iter,
64 BTREE_ID_snapshot_trees, POS(0, U32_MAX));
65 struct bkey_i_snapshot_tree *s_t;
67 if (ret == -BCH_ERR_ENOSPC_btree_slot)
68 ret = -BCH_ERR_ENOSPC_snapshot_tree;
72 s_t = bch2_bkey_alloc(trans, &iter, 0, snapshot_tree);
73 ret = PTR_ERR_OR_ZERO(s_t);
74 bch2_trans_iter_exit(trans, &iter);
75 return ret ? ERR_PTR(ret) : s_t;
78 static int bch2_snapshot_tree_create(struct btree_trans *trans,
79 u32 root_id, u32 subvol_id, u32 *tree_id)
81 struct bkey_i_snapshot_tree *n_tree =
82 __bch2_snapshot_tree_create(trans);
85 return PTR_ERR(n_tree);
87 n_tree->v.master_subvol = cpu_to_le32(subvol_id);
88 n_tree->v.root_snapshot = cpu_to_le32(root_id);
89 *tree_id = n_tree->k.p.offset;
95 static bool __bch2_snapshot_is_ancestor_early(struct snapshot_table *t, u32 id, u32 ancestor)
97 while (id && id < ancestor) {
98 const struct snapshot_t *s = __snapshot_t(t, id);
99 id = s ? s->parent : 0;
101 return id == ancestor;
104 static bool bch2_snapshot_is_ancestor_early(struct bch_fs *c, u32 id, u32 ancestor)
107 bool ret = __bch2_snapshot_is_ancestor_early(rcu_dereference(c->snapshots), id, ancestor);
113 static inline u32 get_ancestor_below(struct snapshot_table *t, u32 id, u32 ancestor)
115 const struct snapshot_t *s = __snapshot_t(t, id);
119 if (s->skip[2] <= ancestor)
121 if (s->skip[1] <= ancestor)
123 if (s->skip[0] <= ancestor)
128 bool __bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
133 struct snapshot_table *t = rcu_dereference(c->snapshots);
135 if (unlikely(c->recovery_pass_done < BCH_RECOVERY_PASS_check_snapshots)) {
136 ret = __bch2_snapshot_is_ancestor_early(t, id, ancestor);
140 while (id && id < ancestor - IS_ANCESTOR_BITMAP)
141 id = get_ancestor_below(t, id, ancestor);
143 if (id && id < ancestor) {
144 ret = test_bit(ancestor - id - 1, __snapshot_t(t, id)->is_ancestor);
146 EBUG_ON(ret != __bch2_snapshot_is_ancestor_early(t, id, ancestor));
148 ret = id == ancestor;
156 static noinline struct snapshot_t *__snapshot_t_mut(struct bch_fs *c, u32 id)
158 size_t idx = U32_MAX - id;
159 struct snapshot_table *new, *old;
161 size_t new_bytes = kmalloc_size_roundup(struct_size(new, s, idx + 1));
162 size_t new_size = (new_bytes - sizeof(*new)) / sizeof(new->s[0]);
164 new = kvzalloc(new_bytes, GFP_KERNEL);
170 old = rcu_dereference_protected(c->snapshots, true);
172 memcpy(new->s, old->s, sizeof(old->s[0]) * old->nr);
174 rcu_assign_pointer(c->snapshots, new);
175 kvfree_rcu(old, rcu);
177 return &rcu_dereference_protected(c->snapshots,
178 lockdep_is_held(&c->snapshot_table_lock))->s[idx];
181 static inline struct snapshot_t *snapshot_t_mut(struct bch_fs *c, u32 id)
183 size_t idx = U32_MAX - id;
184 struct snapshot_table *table =
185 rcu_dereference_protected(c->snapshots,
186 lockdep_is_held(&c->snapshot_table_lock));
188 lockdep_assert_held(&c->snapshot_table_lock);
190 if (likely(table && idx < table->nr))
191 return &table->s[idx];
193 return __snapshot_t_mut(c, id);
196 void bch2_snapshot_to_text(struct printbuf *out, struct bch_fs *c,
199 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(k);
201 prt_printf(out, "is_subvol %llu deleted %llu parent %10u children %10u %10u subvol %u tree %u",
202 BCH_SNAPSHOT_SUBVOL(s.v),
203 BCH_SNAPSHOT_DELETED(s.v),
204 le32_to_cpu(s.v->parent),
205 le32_to_cpu(s.v->children[0]),
206 le32_to_cpu(s.v->children[1]),
207 le32_to_cpu(s.v->subvol),
208 le32_to_cpu(s.v->tree));
210 if (bkey_val_bytes(k.k) > offsetof(struct bch_snapshot, depth))
211 prt_printf(out, " depth %u skiplist %u %u %u",
212 le32_to_cpu(s.v->depth),
213 le32_to_cpu(s.v->skip[0]),
214 le32_to_cpu(s.v->skip[1]),
215 le32_to_cpu(s.v->skip[2]));
218 int bch2_snapshot_invalid(struct bch_fs *c, struct bkey_s_c k,
219 enum bkey_invalid_flags flags,
220 struct printbuf *err)
222 struct bkey_s_c_snapshot s;
226 bkey_fsck_err_on(bkey_gt(k.k->p, POS(0, U32_MAX)) ||
227 bkey_lt(k.k->p, POS(0, 1)), c, err,
231 s = bkey_s_c_to_snapshot(k);
233 id = le32_to_cpu(s.v->parent);
234 bkey_fsck_err_on(id && id <= k.k->p.offset, c, err,
236 "bad parent node (%u <= %llu)",
239 bkey_fsck_err_on(le32_to_cpu(s.v->children[0]) < le32_to_cpu(s.v->children[1]), c, err,
240 snapshot_children_not_normalized,
241 "children not normalized");
243 bkey_fsck_err_on(s.v->children[0] && s.v->children[0] == s.v->children[1], c, err,
244 snapshot_child_duplicate,
245 "duplicate child nodes");
247 for (i = 0; i < 2; i++) {
248 id = le32_to_cpu(s.v->children[i]);
250 bkey_fsck_err_on(id >= k.k->p.offset, c, err,
252 "bad child node (%u >= %llu)",
256 if (bkey_val_bytes(k.k) > offsetof(struct bch_snapshot, skip)) {
257 bkey_fsck_err_on(le32_to_cpu(s.v->skip[0]) > le32_to_cpu(s.v->skip[1]) ||
258 le32_to_cpu(s.v->skip[1]) > le32_to_cpu(s.v->skip[2]), c, err,
259 snapshot_skiplist_not_normalized,
260 "skiplist not normalized");
262 for (i = 0; i < ARRAY_SIZE(s.v->skip); i++) {
263 id = le32_to_cpu(s.v->skip[i]);
265 bkey_fsck_err_on(id && id < le32_to_cpu(s.v->parent), c, err,
266 snapshot_skiplist_bad,
267 "bad skiplist node %u", id);
274 static void __set_is_ancestor_bitmap(struct bch_fs *c, u32 id)
276 struct snapshot_t *t = snapshot_t_mut(c, id);
279 while ((parent = bch2_snapshot_parent_early(c, parent)) &&
280 parent - id - 1 < IS_ANCESTOR_BITMAP)
281 __set_bit(parent - id - 1, t->is_ancestor);
284 static void set_is_ancestor_bitmap(struct bch_fs *c, u32 id)
286 mutex_lock(&c->snapshot_table_lock);
287 __set_is_ancestor_bitmap(c, id);
288 mutex_unlock(&c->snapshot_table_lock);
291 static int __bch2_mark_snapshot(struct btree_trans *trans,
292 enum btree_id btree, unsigned level,
293 struct bkey_s_c old, struct bkey_s_c new,
296 struct bch_fs *c = trans->c;
297 struct snapshot_t *t;
298 u32 id = new.k->p.offset;
301 mutex_lock(&c->snapshot_table_lock);
303 t = snapshot_t_mut(c, id);
305 ret = -BCH_ERR_ENOMEM_mark_snapshot;
309 if (new.k->type == KEY_TYPE_snapshot) {
310 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(new);
312 t->parent = le32_to_cpu(s.v->parent);
313 t->children[0] = le32_to_cpu(s.v->children[0]);
314 t->children[1] = le32_to_cpu(s.v->children[1]);
315 t->subvol = BCH_SNAPSHOT_SUBVOL(s.v) ? le32_to_cpu(s.v->subvol) : 0;
316 t->tree = le32_to_cpu(s.v->tree);
318 if (bkey_val_bytes(s.k) > offsetof(struct bch_snapshot, depth)) {
319 t->depth = le32_to_cpu(s.v->depth);
320 t->skip[0] = le32_to_cpu(s.v->skip[0]);
321 t->skip[1] = le32_to_cpu(s.v->skip[1]);
322 t->skip[2] = le32_to_cpu(s.v->skip[2]);
330 __set_is_ancestor_bitmap(c, id);
332 if (BCH_SNAPSHOT_DELETED(s.v)) {
333 set_bit(BCH_FS_need_delete_dead_snapshots, &c->flags);
334 if (c->curr_recovery_pass > BCH_RECOVERY_PASS_delete_dead_snapshots)
335 bch2_delete_dead_snapshots_async(c);
338 memset(t, 0, sizeof(*t));
341 mutex_unlock(&c->snapshot_table_lock);
345 int bch2_mark_snapshot(struct btree_trans *trans,
346 enum btree_id btree, unsigned level,
347 struct bkey_s_c old, struct bkey_s new,
350 return __bch2_mark_snapshot(trans, btree, level, old, new.s_c, flags);
353 int bch2_snapshot_lookup(struct btree_trans *trans, u32 id,
354 struct bch_snapshot *s)
356 return bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots, POS(0, id),
357 BTREE_ITER_WITH_UPDATES, snapshot, s);
360 static int bch2_snapshot_live(struct btree_trans *trans, u32 id)
362 struct bch_snapshot v;
368 ret = bch2_snapshot_lookup(trans, id, &v);
369 if (bch2_err_matches(ret, ENOENT))
370 bch_err(trans->c, "snapshot node %u not found", id);
374 return !BCH_SNAPSHOT_DELETED(&v);
378 * If @k is a snapshot with just one live child, it's part of a linear chain,
379 * which we consider to be an equivalence class: and then after snapshot
380 * deletion cleanup, there should only be a single key at a given position in
381 * this equivalence class.
383 * This sets the equivalence class of @k to be the child's equivalence class, if
384 * it's part of such a linear chain: this correctly sets equivalence classes on
385 * startup if we run leaf to root (i.e. in natural key order).
387 static int bch2_snapshot_set_equiv(struct btree_trans *trans, struct bkey_s_c k)
389 struct bch_fs *c = trans->c;
390 unsigned i, nr_live = 0, live_idx = 0;
391 struct bkey_s_c_snapshot snap;
392 u32 id = k.k->p.offset, child[2];
394 if (k.k->type != KEY_TYPE_snapshot)
397 snap = bkey_s_c_to_snapshot(k);
399 child[0] = le32_to_cpu(snap.v->children[0]);
400 child[1] = le32_to_cpu(snap.v->children[1]);
402 for (i = 0; i < 2; i++) {
403 int ret = bch2_snapshot_live(trans, child[i]);
413 mutex_lock(&c->snapshot_table_lock);
415 snapshot_t_mut(c, id)->equiv = nr_live == 1
416 ? snapshot_t_mut(c, child[live_idx])->equiv
419 mutex_unlock(&c->snapshot_table_lock);
426 static u32 bch2_snapshot_child(struct bch_fs *c, u32 id, unsigned child)
428 return snapshot_t(c, id)->children[child];
431 static u32 bch2_snapshot_left_child(struct bch_fs *c, u32 id)
433 return bch2_snapshot_child(c, id, 0);
436 static u32 bch2_snapshot_right_child(struct bch_fs *c, u32 id)
438 return bch2_snapshot_child(c, id, 1);
441 static u32 bch2_snapshot_tree_next(struct bch_fs *c, u32 id)
445 n = bch2_snapshot_left_child(c, id);
449 while ((parent = bch2_snapshot_parent(c, id))) {
450 n = bch2_snapshot_right_child(c, parent);
459 static u32 bch2_snapshot_tree_oldest_subvol(struct bch_fs *c, u32 snapshot_root)
461 u32 id = snapshot_root;
465 s = snapshot_t(c, id)->subvol;
467 if (s && (!subvol || s < subvol))
470 id = bch2_snapshot_tree_next(c, id);
476 static int bch2_snapshot_tree_master_subvol(struct btree_trans *trans,
477 u32 snapshot_root, u32 *subvol_id)
479 struct bch_fs *c = trans->c;
480 struct btree_iter iter;
485 for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN,
487 if (k.k->type != KEY_TYPE_subvolume)
490 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
491 if (!bch2_snapshot_is_ancestor(c, le32_to_cpu(s.v->snapshot), snapshot_root))
493 if (!BCH_SUBVOLUME_SNAP(s.v)) {
494 *subvol_id = s.k->p.offset;
500 bch2_trans_iter_exit(trans, &iter);
502 if (!ret && !found) {
503 struct bkey_i_subvolume *u;
505 *subvol_id = bch2_snapshot_tree_oldest_subvol(c, snapshot_root);
507 u = bch2_bkey_get_mut_typed(trans, &iter,
508 BTREE_ID_subvolumes, POS(0, *subvol_id),
510 ret = PTR_ERR_OR_ZERO(u);
514 SET_BCH_SUBVOLUME_SNAP(&u->v, false);
520 static int check_snapshot_tree(struct btree_trans *trans,
521 struct btree_iter *iter,
524 struct bch_fs *c = trans->c;
525 struct bkey_s_c_snapshot_tree st;
526 struct bch_snapshot s;
527 struct bch_subvolume subvol;
528 struct printbuf buf = PRINTBUF;
532 if (k.k->type != KEY_TYPE_snapshot_tree)
535 st = bkey_s_c_to_snapshot_tree(k);
536 root_id = le32_to_cpu(st.v->root_snapshot);
538 ret = bch2_snapshot_lookup(trans, root_id, &s);
539 if (ret && !bch2_err_matches(ret, ENOENT))
542 if (fsck_err_on(ret ||
543 root_id != bch2_snapshot_root(c, root_id) ||
544 st.k->p.offset != le32_to_cpu(s.tree),
545 c, snapshot_tree_to_missing_snapshot,
546 "snapshot tree points to missing/incorrect snapshot:\n %s",
547 (bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
548 ret = bch2_btree_delete_at(trans, iter, 0);
552 ret = bch2_subvolume_get(trans, le32_to_cpu(st.v->master_subvol),
554 if (ret && !bch2_err_matches(ret, ENOENT))
558 c, snapshot_tree_to_missing_subvol,
559 "snapshot tree points to missing subvolume:\n %s",
560 (printbuf_reset(&buf),
561 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
562 fsck_err_on(!bch2_snapshot_is_ancestor(c,
563 le32_to_cpu(subvol.snapshot),
565 c, snapshot_tree_to_wrong_subvol,
566 "snapshot tree points to subvolume that does not point to snapshot in this tree:\n %s",
567 (printbuf_reset(&buf),
568 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
569 fsck_err_on(BCH_SUBVOLUME_SNAP(&subvol),
570 c, snapshot_tree_to_snapshot_subvol,
571 "snapshot tree points to snapshot subvolume:\n %s",
572 (printbuf_reset(&buf),
573 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
574 struct bkey_i_snapshot_tree *u;
577 ret = bch2_snapshot_tree_master_subvol(trans, root_id, &subvol_id);
580 if (bch2_err_matches(ret, ENOENT)) { /* nothing to be done here */
588 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot_tree);
589 ret = PTR_ERR_OR_ZERO(u);
593 u->v.master_subvol = cpu_to_le32(subvol_id);
594 st = snapshot_tree_i_to_s_c(u);
603 * For each snapshot_tree, make sure it points to the root of a snapshot tree
604 * and that snapshot entry points back to it, or delete it.
606 * And, make sure it points to a subvolume within that snapshot tree, or correct
607 * it to point to the oldest subvolume within that snapshot tree.
609 int bch2_check_snapshot_trees(struct bch_fs *c)
611 int ret = bch2_trans_run(c,
612 for_each_btree_key_commit(trans, iter,
613 BTREE_ID_snapshot_trees, POS_MIN,
614 BTREE_ITER_PREFETCH, k,
615 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
616 check_snapshot_tree(trans, &iter, k)));
622 * Look up snapshot tree for @tree_id and find root,
623 * make sure @snap_id is a descendent:
625 static int snapshot_tree_ptr_good(struct btree_trans *trans,
626 u32 snap_id, u32 tree_id)
628 struct bch_snapshot_tree s_t;
629 int ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
631 if (bch2_err_matches(ret, ENOENT))
636 return bch2_snapshot_is_ancestor_early(trans->c, snap_id, le32_to_cpu(s_t.root_snapshot));
639 u32 bch2_snapshot_skiplist_get(struct bch_fs *c, u32 id)
641 const struct snapshot_t *s;
647 s = snapshot_t(c, id);
649 id = bch2_snapshot_nth_parent(c, id, get_random_u32_below(s->depth));
655 static int snapshot_skiplist_good(struct btree_trans *trans, u32 id, struct bch_snapshot s)
659 for (i = 0; i < 3; i++)
664 if (!bch2_snapshot_is_ancestor_early(trans->c, id, le32_to_cpu(s.skip[i])))
672 * snapshot_tree pointer was incorrect: look up root snapshot node, make sure
673 * its snapshot_tree pointer is correct (allocate new one if necessary), then
674 * update this node's pointer to root node's pointer:
676 static int snapshot_tree_ptr_repair(struct btree_trans *trans,
677 struct btree_iter *iter,
679 struct bch_snapshot *s)
681 struct bch_fs *c = trans->c;
682 struct btree_iter root_iter;
683 struct bch_snapshot_tree s_t;
684 struct bkey_s_c_snapshot root;
685 struct bkey_i_snapshot *u;
686 u32 root_id = bch2_snapshot_root(c, k.k->p.offset), tree_id;
689 root = bch2_bkey_get_iter_typed(trans, &root_iter,
690 BTREE_ID_snapshots, POS(0, root_id),
691 BTREE_ITER_WITH_UPDATES, snapshot);
692 ret = bkey_err(root);
696 tree_id = le32_to_cpu(root.v->tree);
698 ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
699 if (ret && !bch2_err_matches(ret, ENOENT))
702 if (ret || le32_to_cpu(s_t.root_snapshot) != root_id) {
703 u = bch2_bkey_make_mut_typed(trans, &root_iter, &root.s_c, 0, snapshot);
704 ret = PTR_ERR_OR_ZERO(u) ?:
705 bch2_snapshot_tree_create(trans, root_id,
706 bch2_snapshot_tree_oldest_subvol(c, root_id),
711 u->v.tree = cpu_to_le32(tree_id);
712 if (k.k->p.offset == root_id)
716 if (k.k->p.offset != root_id) {
717 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
718 ret = PTR_ERR_OR_ZERO(u);
722 u->v.tree = cpu_to_le32(tree_id);
726 bch2_trans_iter_exit(trans, &root_iter);
730 static int check_snapshot(struct btree_trans *trans,
731 struct btree_iter *iter,
734 struct bch_fs *c = trans->c;
735 struct bch_snapshot s;
736 struct bch_subvolume subvol;
737 struct bch_snapshot v;
738 struct bkey_i_snapshot *u;
739 u32 parent_id = bch2_snapshot_parent_early(c, k.k->p.offset);
741 struct printbuf buf = PRINTBUF;
745 if (k.k->type != KEY_TYPE_snapshot)
748 memset(&s, 0, sizeof(s));
749 memcpy(&s, k.v, min(sizeof(s), bkey_val_bytes(k.k)));
751 id = le32_to_cpu(s.parent);
753 ret = bch2_snapshot_lookup(trans, id, &v);
754 if (bch2_err_matches(ret, ENOENT))
755 bch_err(c, "snapshot with nonexistent parent:\n %s",
756 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
760 if (le32_to_cpu(v.children[0]) != k.k->p.offset &&
761 le32_to_cpu(v.children[1]) != k.k->p.offset) {
762 bch_err(c, "snapshot parent %u missing pointer to child %llu",
769 for (i = 0; i < 2 && s.children[i]; i++) {
770 id = le32_to_cpu(s.children[i]);
772 ret = bch2_snapshot_lookup(trans, id, &v);
773 if (bch2_err_matches(ret, ENOENT))
774 bch_err(c, "snapshot node %llu has nonexistent child %u",
779 if (le32_to_cpu(v.parent) != k.k->p.offset) {
780 bch_err(c, "snapshot child %u has wrong parent (got %u should be %llu)",
781 id, le32_to_cpu(v.parent), k.k->p.offset);
787 bool should_have_subvol = BCH_SNAPSHOT_SUBVOL(&s) &&
788 !BCH_SNAPSHOT_DELETED(&s);
790 if (should_have_subvol) {
791 id = le32_to_cpu(s.subvol);
792 ret = bch2_subvolume_get(trans, id, 0, false, &subvol);
793 if (bch2_err_matches(ret, ENOENT))
794 bch_err(c, "snapshot points to nonexistent subvolume:\n %s",
795 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
799 if (BCH_SNAPSHOT_SUBVOL(&s) != (le32_to_cpu(subvol.snapshot) == k.k->p.offset)) {
800 bch_err(c, "snapshot node %llu has wrong BCH_SNAPSHOT_SUBVOL",
806 if (fsck_err_on(s.subvol,
807 c, snapshot_should_not_have_subvol,
808 "snapshot should not point to subvol:\n %s",
809 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
810 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
811 ret = PTR_ERR_OR_ZERO(u);
820 ret = snapshot_tree_ptr_good(trans, k.k->p.offset, le32_to_cpu(s.tree));
824 if (fsck_err_on(!ret, c, snapshot_to_bad_snapshot_tree,
825 "snapshot points to missing/incorrect tree:\n %s",
826 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
827 ret = snapshot_tree_ptr_repair(trans, iter, k, &s);
833 real_depth = bch2_snapshot_depth(c, parent_id);
835 if (fsck_err_on(le32_to_cpu(s.depth) != real_depth,
836 c, snapshot_bad_depth,
837 "snapshot with incorrect depth field, should be %u:\n %s",
838 real_depth, (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
839 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
840 ret = PTR_ERR_OR_ZERO(u);
844 u->v.depth = cpu_to_le32(real_depth);
848 ret = snapshot_skiplist_good(trans, k.k->p.offset, s);
852 if (fsck_err_on(!ret, c, snapshot_bad_skiplist,
853 "snapshot with bad skiplist field:\n %s",
854 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
855 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
856 ret = PTR_ERR_OR_ZERO(u);
860 for (i = 0; i < ARRAY_SIZE(u->v.skip); i++)
861 u->v.skip[i] = cpu_to_le32(bch2_snapshot_skiplist_get(c, parent_id));
863 bubble_sort(u->v.skip, ARRAY_SIZE(u->v.skip), cmp_le32);
873 int bch2_check_snapshots(struct bch_fs *c)
876 * We iterate backwards as checking/fixing the depth field requires that
877 * the parent's depth already be correct:
879 int ret = bch2_trans_run(c,
880 for_each_btree_key_reverse_commit(trans, iter,
881 BTREE_ID_snapshots, POS_MAX,
882 BTREE_ITER_PREFETCH, k,
883 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
884 check_snapshot(trans, &iter, k)));
889 static int check_snapshot_exists(struct btree_trans *trans, u32 id)
891 struct bch_fs *c = trans->c;
893 if (bch2_snapshot_equiv(c, id))
897 int ret = bch2_snapshot_tree_create(trans, id, 0, &tree_id);
901 struct bkey_i_snapshot *snapshot = bch2_trans_kmalloc(trans, sizeof(*snapshot));
902 ret = PTR_ERR_OR_ZERO(snapshot);
906 bkey_snapshot_init(&snapshot->k_i);
907 snapshot->k.p = POS(0, id);
908 snapshot->v.tree = cpu_to_le32(tree_id);
909 snapshot->v.btime.lo = cpu_to_le64(bch2_current_time(c));
911 return bch2_btree_insert_trans(trans, BTREE_ID_snapshots, &snapshot->k_i, 0) ?:
912 bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0,
913 bkey_s_c_null, bkey_i_to_s(&snapshot->k_i), 0) ?:
914 bch2_snapshot_set_equiv(trans, bkey_i_to_s_c(&snapshot->k_i));
917 /* Figure out which snapshot nodes belong in the same tree: */
918 struct snapshot_tree_reconstruct {
921 snapshot_id_list cur_ids;
922 DARRAY(snapshot_id_list) trees;
925 static void snapshot_tree_reconstruct_exit(struct snapshot_tree_reconstruct *r)
927 darray_for_each(r->trees, i)
929 darray_exit(&r->trees);
930 darray_exit(&r->cur_ids);
933 static inline bool same_snapshot(struct snapshot_tree_reconstruct *r, struct bpos pos)
935 return r->btree == BTREE_ID_inodes
936 ? r->cur_pos.offset == pos.offset
937 : r->cur_pos.inode == pos.inode;
940 static inline bool snapshot_id_lists_have_common(snapshot_id_list *l, snapshot_id_list *r)
942 darray_for_each(*l, i)
943 if (snapshot_list_has_id(r, *i))
948 static void snapshot_id_list_to_text(struct printbuf *out, snapshot_id_list *s)
951 darray_for_each(*s, i) {
955 prt_printf(out, "%u", *i);
959 static int snapshot_tree_reconstruct_next(struct bch_fs *c, struct snapshot_tree_reconstruct *r)
962 darray_for_each(r->trees, i)
963 if (snapshot_id_lists_have_common(i, &r->cur_ids)) {
964 int ret = snapshot_list_merge(c, i, &r->cur_ids);
969 darray_push(&r->trees, r->cur_ids);
970 darray_init(&r->cur_ids);
977 static int get_snapshot_trees(struct bch_fs *c, struct snapshot_tree_reconstruct *r, struct bpos pos)
979 if (!same_snapshot(r, pos))
980 snapshot_tree_reconstruct_next(c, r);
982 return snapshot_list_add_nodup(c, &r->cur_ids, pos.snapshot);
985 int bch2_reconstruct_snapshots(struct bch_fs *c)
987 struct btree_trans *trans = bch2_trans_get(c);
988 struct printbuf buf = PRINTBUF;
989 struct snapshot_tree_reconstruct r = {};
992 for (unsigned btree = 0; btree < BTREE_ID_NR; btree++) {
993 if (btree_type_has_snapshots(btree)) {
996 ret = for_each_btree_key(trans, iter, btree, POS_MIN,
997 BTREE_ITER_ALL_SNAPSHOTS|BTREE_ITER_PREFETCH, k, ({
998 get_snapshot_trees(c, &r, k.k->p);
1003 snapshot_tree_reconstruct_next(c, &r);
1007 darray_for_each(r.trees, t) {
1008 printbuf_reset(&buf);
1009 snapshot_id_list_to_text(&buf, t);
1011 darray_for_each(*t, id) {
1012 if (fsck_err_on(!bch2_snapshot_equiv(c, *id),
1013 c, snapshot_node_missing,
1014 "snapshot node %u from tree %s missing", *id, buf.buf)) {
1016 bch_err(c, "cannot reconstruct snapshot trees with multiple nodes");
1017 ret = -BCH_ERR_fsck_repair_unimplemented;
1021 ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1022 check_snapshot_exists(trans, *id));
1030 bch2_trans_put(trans);
1031 snapshot_tree_reconstruct_exit(&r);
1032 printbuf_exit(&buf);
1038 * Mark a snapshot as deleted, for future cleanup:
1040 int bch2_snapshot_node_set_deleted(struct btree_trans *trans, u32 id)
1042 struct btree_iter iter;
1043 struct bkey_i_snapshot *s;
1046 s = bch2_bkey_get_mut_typed(trans, &iter,
1047 BTREE_ID_snapshots, POS(0, id),
1049 ret = PTR_ERR_OR_ZERO(s);
1050 if (unlikely(ret)) {
1051 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT),
1052 trans->c, "missing snapshot %u", id);
1056 /* already deleted? */
1057 if (BCH_SNAPSHOT_DELETED(&s->v))
1060 SET_BCH_SNAPSHOT_DELETED(&s->v, true);
1061 SET_BCH_SNAPSHOT_SUBVOL(&s->v, false);
1064 bch2_trans_iter_exit(trans, &iter);
1068 static inline void normalize_snapshot_child_pointers(struct bch_snapshot *s)
1070 if (le32_to_cpu(s->children[0]) < le32_to_cpu(s->children[1]))
1071 swap(s->children[0], s->children[1]);
1074 static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
1076 struct bch_fs *c = trans->c;
1077 struct btree_iter iter, p_iter = (struct btree_iter) { NULL };
1078 struct btree_iter c_iter = (struct btree_iter) { NULL };
1079 struct btree_iter tree_iter = (struct btree_iter) { NULL };
1080 struct bkey_s_c_snapshot s;
1081 u32 parent_id, child_id;
1085 s = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_snapshots, POS(0, id),
1086 BTREE_ITER_INTENT, snapshot);
1088 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
1089 "missing snapshot %u", id);
1094 BUG_ON(s.v->children[1]);
1096 parent_id = le32_to_cpu(s.v->parent);
1097 child_id = le32_to_cpu(s.v->children[0]);
1100 struct bkey_i_snapshot *parent;
1102 parent = bch2_bkey_get_mut_typed(trans, &p_iter,
1103 BTREE_ID_snapshots, POS(0, parent_id),
1105 ret = PTR_ERR_OR_ZERO(parent);
1106 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
1107 "missing snapshot %u", parent_id);
1111 /* find entry in parent->children for node being deleted */
1112 for (i = 0; i < 2; i++)
1113 if (le32_to_cpu(parent->v.children[i]) == id)
1116 if (bch2_fs_inconsistent_on(i == 2, c,
1117 "snapshot %u missing child pointer to %u",
1121 parent->v.children[i] = cpu_to_le32(child_id);
1123 normalize_snapshot_child_pointers(&parent->v);
1127 struct bkey_i_snapshot *child;
1129 child = bch2_bkey_get_mut_typed(trans, &c_iter,
1130 BTREE_ID_snapshots, POS(0, child_id),
1132 ret = PTR_ERR_OR_ZERO(child);
1133 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
1134 "missing snapshot %u", child_id);
1138 child->v.parent = cpu_to_le32(parent_id);
1140 if (!child->v.parent) {
1141 child->v.skip[0] = 0;
1142 child->v.skip[1] = 0;
1143 child->v.skip[2] = 0;
1149 * We're deleting the root of a snapshot tree: update the
1150 * snapshot_tree entry to point to the new root, or delete it if
1151 * this is the last snapshot ID in this tree:
1153 struct bkey_i_snapshot_tree *s_t;
1155 BUG_ON(s.v->children[1]);
1157 s_t = bch2_bkey_get_mut_typed(trans, &tree_iter,
1158 BTREE_ID_snapshot_trees, POS(0, le32_to_cpu(s.v->tree)),
1160 ret = PTR_ERR_OR_ZERO(s_t);
1164 if (s.v->children[0]) {
1165 s_t->v.root_snapshot = s.v->children[0];
1167 s_t->k.type = KEY_TYPE_deleted;
1168 set_bkey_val_u64s(&s_t->k, 0);
1172 ret = bch2_btree_delete_at(trans, &iter, 0);
1174 bch2_trans_iter_exit(trans, &tree_iter);
1175 bch2_trans_iter_exit(trans, &p_iter);
1176 bch2_trans_iter_exit(trans, &c_iter);
1177 bch2_trans_iter_exit(trans, &iter);
1181 static int create_snapids(struct btree_trans *trans, u32 parent, u32 tree,
1183 u32 *snapshot_subvols,
1184 unsigned nr_snapids)
1186 struct bch_fs *c = trans->c;
1187 struct btree_iter iter;
1188 struct bkey_i_snapshot *n;
1191 u32 depth = bch2_snapshot_depth(c, parent);
1194 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
1195 POS_MIN, BTREE_ITER_INTENT);
1196 k = bch2_btree_iter_peek(&iter);
1201 for (i = 0; i < nr_snapids; i++) {
1202 k = bch2_btree_iter_prev_slot(&iter);
1207 if (!k.k || !k.k->p.offset) {
1208 ret = -BCH_ERR_ENOSPC_snapshot_create;
1212 n = bch2_bkey_alloc(trans, &iter, 0, snapshot);
1213 ret = PTR_ERR_OR_ZERO(n);
1218 n->v.parent = cpu_to_le32(parent);
1219 n->v.subvol = cpu_to_le32(snapshot_subvols[i]);
1220 n->v.tree = cpu_to_le32(tree);
1221 n->v.depth = cpu_to_le32(depth);
1222 n->v.btime.lo = cpu_to_le64(bch2_current_time(c));
1225 for (j = 0; j < ARRAY_SIZE(n->v.skip); j++)
1226 n->v.skip[j] = cpu_to_le32(bch2_snapshot_skiplist_get(c, parent));
1228 bubble_sort(n->v.skip, ARRAY_SIZE(n->v.skip), cmp_le32);
1229 SET_BCH_SNAPSHOT_SUBVOL(&n->v, true);
1231 ret = __bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0,
1232 bkey_s_c_null, bkey_i_to_s_c(&n->k_i), 0);
1236 new_snapids[i] = iter.pos.offset;
1238 mutex_lock(&c->snapshot_table_lock);
1239 snapshot_t_mut(c, new_snapids[i])->equiv = new_snapids[i];
1240 mutex_unlock(&c->snapshot_table_lock);
1243 bch2_trans_iter_exit(trans, &iter);
1248 * Create new snapshot IDs as children of an existing snapshot ID:
1250 static int bch2_snapshot_node_create_children(struct btree_trans *trans, u32 parent,
1252 u32 *snapshot_subvols,
1253 unsigned nr_snapids)
1255 struct btree_iter iter;
1256 struct bkey_i_snapshot *n_parent;
1259 n_parent = bch2_bkey_get_mut_typed(trans, &iter,
1260 BTREE_ID_snapshots, POS(0, parent),
1262 ret = PTR_ERR_OR_ZERO(n_parent);
1263 if (unlikely(ret)) {
1264 if (bch2_err_matches(ret, ENOENT))
1265 bch_err(trans->c, "snapshot %u not found", parent);
1269 if (n_parent->v.children[0] || n_parent->v.children[1]) {
1270 bch_err(trans->c, "Trying to add child snapshot nodes to parent that already has children");
1275 ret = create_snapids(trans, parent, le32_to_cpu(n_parent->v.tree),
1276 new_snapids, snapshot_subvols, nr_snapids);
1280 n_parent->v.children[0] = cpu_to_le32(new_snapids[0]);
1281 n_parent->v.children[1] = cpu_to_le32(new_snapids[1]);
1282 n_parent->v.subvol = 0;
1283 SET_BCH_SNAPSHOT_SUBVOL(&n_parent->v, false);
1285 bch2_trans_iter_exit(trans, &iter);
1290 * Create a snapshot node that is the root of a new tree:
1292 static int bch2_snapshot_node_create_tree(struct btree_trans *trans,
1294 u32 *snapshot_subvols,
1295 unsigned nr_snapids)
1297 struct bkey_i_snapshot_tree *n_tree;
1300 n_tree = __bch2_snapshot_tree_create(trans);
1301 ret = PTR_ERR_OR_ZERO(n_tree) ?:
1302 create_snapids(trans, 0, n_tree->k.p.offset,
1303 new_snapids, snapshot_subvols, nr_snapids);
1307 n_tree->v.master_subvol = cpu_to_le32(snapshot_subvols[0]);
1308 n_tree->v.root_snapshot = cpu_to_le32(new_snapids[0]);
1312 int bch2_snapshot_node_create(struct btree_trans *trans, u32 parent,
1314 u32 *snapshot_subvols,
1315 unsigned nr_snapids)
1317 BUG_ON((parent == 0) != (nr_snapids == 1));
1318 BUG_ON((parent != 0) != (nr_snapids == 2));
1321 ? bch2_snapshot_node_create_children(trans, parent,
1322 new_snapids, snapshot_subvols, nr_snapids)
1323 : bch2_snapshot_node_create_tree(trans,
1324 new_snapids, snapshot_subvols, nr_snapids);
1329 * If we have an unlinked inode in an internal snapshot node, and the inode
1330 * really has been deleted in all child snapshots, how does this get cleaned up?
1332 * first there is the problem of how keys that have been overwritten in all
1333 * child snapshots get deleted (unimplemented?), but inodes may perhaps be
1336 * also: unlinked inode in internal snapshot appears to not be getting deleted
1337 * correctly if inode doesn't exist in leaf snapshots
1341 * for a key in an interior snapshot node that needs work to be done that
1342 * requires it to be mutated: iterate over all descendent leaf nodes and copy
1343 * that key to snapshot leaf nodes, where we can mutate it
1346 static int snapshot_delete_key(struct btree_trans *trans,
1347 struct btree_iter *iter,
1349 snapshot_id_list *deleted,
1350 snapshot_id_list *equiv_seen,
1351 struct bpos *last_pos)
1353 struct bch_fs *c = trans->c;
1354 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
1356 if (!bkey_eq(k.k->p, *last_pos))
1360 if (snapshot_list_has_id(deleted, k.k->p.snapshot) ||
1361 snapshot_list_has_id(equiv_seen, equiv)) {
1362 return bch2_btree_delete_at(trans, iter,
1363 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1365 return snapshot_list_add(c, equiv_seen, equiv);
1369 static int move_key_to_correct_snapshot(struct btree_trans *trans,
1370 struct btree_iter *iter,
1373 struct bch_fs *c = trans->c;
1374 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
1377 * When we have a linear chain of snapshot nodes, we consider
1378 * those to form an equivalence class: we're going to collapse
1379 * them all down to a single node, and keep the leaf-most node -
1380 * which has the same id as the equivalence class id.
1382 * If there are multiple keys in different snapshots at the same
1383 * position, we're only going to keep the one in the newest
1384 * snapshot - the rest have been overwritten and are redundant,
1385 * and for the key we're going to keep we need to move it to the
1386 * equivalance class ID if it's not there already.
1388 if (equiv != k.k->p.snapshot) {
1389 struct bkey_i *new = bch2_bkey_make_mut_noupdate(trans, k);
1390 struct btree_iter new_iter;
1393 ret = PTR_ERR_OR_ZERO(new);
1397 new->k.p.snapshot = equiv;
1399 bch2_trans_iter_init(trans, &new_iter, iter->btree_id, new->k.p,
1400 BTREE_ITER_ALL_SNAPSHOTS|
1404 ret = bch2_btree_iter_traverse(&new_iter) ?:
1405 bch2_trans_update(trans, &new_iter, new,
1406 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
1407 bch2_btree_delete_at(trans, iter,
1408 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1409 bch2_trans_iter_exit(trans, &new_iter);
1417 static int bch2_snapshot_needs_delete(struct btree_trans *trans, struct bkey_s_c k)
1419 struct bkey_s_c_snapshot snap;
1423 if (k.k->type != KEY_TYPE_snapshot)
1426 snap = bkey_s_c_to_snapshot(k);
1427 if (BCH_SNAPSHOT_DELETED(snap.v) ||
1428 BCH_SNAPSHOT_SUBVOL(snap.v))
1431 children[0] = le32_to_cpu(snap.v->children[0]);
1432 children[1] = le32_to_cpu(snap.v->children[1]);
1434 ret = bch2_snapshot_live(trans, children[0]) ?:
1435 bch2_snapshot_live(trans, children[1]);
1442 * For a given snapshot, if it doesn't have a subvolume that points to it, and
1443 * it doesn't have child snapshot nodes - it's now redundant and we can mark it
1446 static int bch2_delete_redundant_snapshot(struct btree_trans *trans, struct bkey_s_c k)
1448 int ret = bch2_snapshot_needs_delete(trans, k);
1452 : bch2_snapshot_node_set_deleted(trans, k.k->p.offset);
1455 static inline u32 bch2_snapshot_nth_parent_skip(struct bch_fs *c, u32 id, u32 n,
1456 snapshot_id_list *skip)
1459 while (snapshot_list_has_id(skip, id))
1460 id = __bch2_snapshot_parent(c, id);
1464 id = __bch2_snapshot_parent(c, id);
1465 } while (snapshot_list_has_id(skip, id));
1472 static int bch2_fix_child_of_deleted_snapshot(struct btree_trans *trans,
1473 struct btree_iter *iter, struct bkey_s_c k,
1474 snapshot_id_list *deleted)
1476 struct bch_fs *c = trans->c;
1477 u32 nr_deleted_ancestors = 0;
1478 struct bkey_i_snapshot *s;
1481 if (k.k->type != KEY_TYPE_snapshot)
1484 if (snapshot_list_has_id(deleted, k.k->p.offset))
1487 s = bch2_bkey_make_mut_noupdate_typed(trans, k, snapshot);
1488 ret = PTR_ERR_OR_ZERO(s);
1492 darray_for_each(*deleted, i)
1493 nr_deleted_ancestors += bch2_snapshot_is_ancestor(c, s->k.p.offset, *i);
1495 if (!nr_deleted_ancestors)
1498 le32_add_cpu(&s->v.depth, -nr_deleted_ancestors);
1505 u32 depth = le32_to_cpu(s->v.depth);
1506 u32 parent = bch2_snapshot_parent(c, s->k.p.offset);
1508 for (unsigned j = 0; j < ARRAY_SIZE(s->v.skip); j++) {
1509 u32 id = le32_to_cpu(s->v.skip[j]);
1511 if (snapshot_list_has_id(deleted, id)) {
1512 id = bch2_snapshot_nth_parent_skip(c,
1515 ? get_random_u32_below(depth - 1)
1518 s->v.skip[j] = cpu_to_le32(id);
1522 bubble_sort(s->v.skip, ARRAY_SIZE(s->v.skip), cmp_le32);
1525 return bch2_trans_update(trans, iter, &s->k_i, 0);
1528 int bch2_delete_dead_snapshots(struct bch_fs *c)
1530 struct btree_trans *trans;
1531 snapshot_id_list deleted = { 0 };
1532 snapshot_id_list deleted_interior = { 0 };
1536 if (!test_and_clear_bit(BCH_FS_need_delete_dead_snapshots, &c->flags))
1539 if (!test_bit(BCH_FS_started, &c->flags)) {
1540 ret = bch2_fs_read_write_early(c);
1541 bch_err_msg(c, ret, "deleting dead snapshots: error going rw");
1546 trans = bch2_trans_get(c);
1549 * For every snapshot node: If we have no live children and it's not
1550 * pointed to by a subvolume, delete it:
1552 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_snapshots,
1555 bch2_delete_redundant_snapshot(trans, k));
1556 bch_err_msg(c, ret, "deleting redundant snapshots");
1560 ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1562 bch2_snapshot_set_equiv(trans, k));
1563 bch_err_msg(c, ret, "in bch2_snapshots_set_equiv");
1567 ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1569 if (k.k->type != KEY_TYPE_snapshot)
1572 BCH_SNAPSHOT_DELETED(bkey_s_c_to_snapshot(k).v)
1573 ? snapshot_list_add(c, &deleted, k.k->p.offset)
1576 bch_err_msg(c, ret, "walking snapshots");
1580 for (id = 0; id < BTREE_ID_NR; id++) {
1581 struct bpos last_pos = POS_MIN;
1582 snapshot_id_list equiv_seen = { 0 };
1583 struct disk_reservation res = { 0 };
1585 if (!btree_type_has_snapshots(id))
1589 * deleted inodes btree is maintained by a trigger on the inodes
1590 * btree - no work for us to do here, and it's not safe to scan
1591 * it because we'll see out of date keys due to the btree write
1594 if (id == BTREE_ID_deleted_inodes)
1597 ret = for_each_btree_key_commit(trans, iter,
1599 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1600 &res, NULL, BCH_TRANS_COMMIT_no_enospc,
1601 snapshot_delete_key(trans, &iter, k, &deleted, &equiv_seen, &last_pos)) ?:
1602 for_each_btree_key_commit(trans, iter,
1604 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1605 &res, NULL, BCH_TRANS_COMMIT_no_enospc,
1606 move_key_to_correct_snapshot(trans, &iter, k));
1608 bch2_disk_reservation_put(c, &res);
1609 darray_exit(&equiv_seen);
1611 bch_err_msg(c, ret, "deleting keys from dying snapshots");
1616 bch2_trans_unlock(trans);
1617 down_write(&c->snapshot_create_lock);
1619 ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1621 u32 snapshot = k.k->p.offset;
1622 u32 equiv = bch2_snapshot_equiv(c, snapshot);
1625 ? snapshot_list_add(c, &deleted_interior, snapshot)
1629 bch_err_msg(c, ret, "walking snapshots");
1631 goto err_create_lock;
1634 * Fixing children of deleted snapshots can't be done completely
1635 * atomically, if we crash between here and when we delete the interior
1636 * nodes some depth fields will be off:
1638 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_snapshots, POS_MIN,
1639 BTREE_ITER_INTENT, k,
1640 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1641 bch2_fix_child_of_deleted_snapshot(trans, &iter, k, &deleted_interior));
1643 goto err_create_lock;
1645 darray_for_each(deleted, i) {
1646 ret = commit_do(trans, NULL, NULL, 0,
1647 bch2_snapshot_node_delete(trans, *i));
1648 bch_err_msg(c, ret, "deleting snapshot %u", *i);
1650 goto err_create_lock;
1653 darray_for_each(deleted_interior, i) {
1654 ret = commit_do(trans, NULL, NULL, 0,
1655 bch2_snapshot_node_delete(trans, *i));
1656 bch_err_msg(c, ret, "deleting snapshot %u", *i);
1658 goto err_create_lock;
1661 up_write(&c->snapshot_create_lock);
1663 darray_exit(&deleted_interior);
1664 darray_exit(&deleted);
1665 bch2_trans_put(trans);
1670 void bch2_delete_dead_snapshots_work(struct work_struct *work)
1672 struct bch_fs *c = container_of(work, struct bch_fs, snapshot_delete_work);
1674 bch2_delete_dead_snapshots(c);
1675 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1678 void bch2_delete_dead_snapshots_async(struct bch_fs *c)
1680 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_delete_dead_snapshots) &&
1681 !queue_work(c->write_ref_wq, &c->snapshot_delete_work))
1682 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1685 int __bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
1689 struct bch_fs *c = trans->c;
1690 struct btree_iter iter;
1694 bch2_trans_iter_init(trans, &iter, id, pos,
1695 BTREE_ITER_NOT_EXTENTS|
1696 BTREE_ITER_ALL_SNAPSHOTS);
1698 k = bch2_btree_iter_prev(&iter);
1706 if (!bkey_eq(pos, k.k->p))
1709 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, pos.snapshot)) {
1714 bch2_trans_iter_exit(trans, &iter);
1719 static u32 bch2_snapshot_smallest_child(struct bch_fs *c, u32 id)
1721 const struct snapshot_t *s = snapshot_t(c, id);
1723 return s->children[1] ?: s->children[0];
1726 static u32 bch2_snapshot_smallest_descendent(struct bch_fs *c, u32 id)
1730 while ((child = bch2_snapshot_smallest_child(c, id)))
1735 static int bch2_propagate_key_to_snapshot_leaf(struct btree_trans *trans,
1736 enum btree_id btree,
1737 struct bkey_s_c interior_k,
1738 u32 leaf_id, struct bpos *new_min_pos)
1740 struct btree_iter iter;
1741 struct bpos pos = interior_k.k->p;
1746 pos.snapshot = leaf_id;
1748 bch2_trans_iter_init(trans, &iter, btree, pos, BTREE_ITER_INTENT);
1749 k = bch2_btree_iter_peek_slot(&iter);
1754 /* key already overwritten in this snapshot? */
1755 if (k.k->p.snapshot != interior_k.k->p.snapshot)
1758 if (bpos_eq(*new_min_pos, POS_MIN)) {
1759 *new_min_pos = k.k->p;
1760 new_min_pos->snapshot = leaf_id;
1763 new = bch2_bkey_make_mut_noupdate(trans, interior_k);
1764 ret = PTR_ERR_OR_ZERO(new);
1768 new->k.p.snapshot = leaf_id;
1769 ret = bch2_trans_update(trans, &iter, new, 0);
1771 bch2_trans_iter_exit(trans, &iter);
1775 int bch2_propagate_key_to_snapshot_leaves(struct btree_trans *trans,
1776 enum btree_id btree,
1778 struct bpos *new_min_pos)
1780 struct bch_fs *c = trans->c;
1782 u32 restart_count = trans->restart_count;
1785 bch2_bkey_buf_init(&sk);
1786 bch2_bkey_buf_reassemble(&sk, c, k);
1787 k = bkey_i_to_s_c(sk.k);
1789 *new_min_pos = POS_MIN;
1791 for (u32 id = bch2_snapshot_smallest_descendent(c, k.k->p.snapshot);
1792 id < k.k->p.snapshot;
1794 if (!bch2_snapshot_is_ancestor(c, id, k.k->p.snapshot) ||
1795 !bch2_snapshot_is_leaf(c, id))
1798 ret = btree_trans_too_many_iters(trans) ?:
1799 bch2_propagate_key_to_snapshot_leaf(trans, btree, k, id, new_min_pos) ?:
1800 bch2_trans_commit(trans, NULL, NULL, 0);
1801 if (ret && bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
1802 bch2_trans_begin(trans);
1810 bch2_bkey_buf_exit(&sk, c);
1812 return ret ?: trans_was_restarted(trans, restart_count);
1815 static int bch2_check_snapshot_needs_deletion(struct btree_trans *trans, struct bkey_s_c k)
1817 struct bch_fs *c = trans->c;
1818 struct bkey_s_c_snapshot snap;
1821 if (k.k->type != KEY_TYPE_snapshot)
1824 snap = bkey_s_c_to_snapshot(k);
1825 if (BCH_SNAPSHOT_DELETED(snap.v) ||
1826 bch2_snapshot_equiv(c, k.k->p.offset) != k.k->p.offset ||
1827 (ret = bch2_snapshot_needs_delete(trans, k)) > 0) {
1828 set_bit(BCH_FS_need_delete_dead_snapshots, &c->flags);
1835 int bch2_snapshots_read(struct bch_fs *c)
1837 int ret = bch2_trans_run(c,
1838 for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1840 __bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0, bkey_s_c_null, k, 0) ?:
1841 bch2_snapshot_set_equiv(trans, k) ?:
1842 bch2_check_snapshot_needs_deletion(trans, k)) ?:
1843 for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1845 (set_is_ancestor_bitmap(c, k.k->p.offset), 0)));
1849 * It's important that we check if we need to reconstruct snapshots
1850 * before going RW, so we mark that pass as required in the superblock -
1851 * otherwise, we could end up deleting keys with missing snapshot nodes
1854 BUG_ON(!test_bit(BCH_FS_new_fs, &c->flags) &&
1855 test_bit(BCH_FS_may_go_rw, &c->flags));
1857 if (bch2_err_matches(ret, EIO) ||
1858 (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_snapshots)))
1859 ret = bch2_run_explicit_recovery_pass_persistent(c, BCH_RECOVERY_PASS_reconstruct_snapshots);
1864 void bch2_fs_snapshots_exit(struct bch_fs *c)
1866 kvfree(rcu_dereference_protected(c->snapshots, true));