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 static bool test_ancestor_bitmap(struct snapshot_table *t, u32 id, u32 ancestor)
130 const struct snapshot_t *s = __snapshot_t(t, id);
134 return test_bit(ancestor - id - 1, s->is_ancestor);
137 bool __bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ancestor)
142 struct snapshot_table *t = rcu_dereference(c->snapshots);
144 if (unlikely(c->recovery_pass_done < BCH_RECOVERY_PASS_check_snapshots)) {
145 ret = __bch2_snapshot_is_ancestor_early(t, id, ancestor);
149 while (id && id < ancestor - IS_ANCESTOR_BITMAP)
150 id = get_ancestor_below(t, id, ancestor);
152 ret = id && id < ancestor
153 ? test_ancestor_bitmap(t, id, ancestor)
156 EBUG_ON(ret != __bch2_snapshot_is_ancestor_early(t, id, ancestor));
163 static noinline struct snapshot_t *__snapshot_t_mut(struct bch_fs *c, u32 id)
165 size_t idx = U32_MAX - id;
166 struct snapshot_table *new, *old;
168 size_t new_bytes = kmalloc_size_roundup(struct_size(new, s, idx + 1));
169 size_t new_size = (new_bytes - sizeof(*new)) / sizeof(new->s[0]);
171 new = kvzalloc(new_bytes, GFP_KERNEL);
177 old = rcu_dereference_protected(c->snapshots, true);
179 memcpy(new->s, old->s, sizeof(old->s[0]) * old->nr);
181 rcu_assign_pointer(c->snapshots, new);
182 kvfree_rcu(old, rcu);
184 return &rcu_dereference_protected(c->snapshots,
185 lockdep_is_held(&c->snapshot_table_lock))->s[idx];
188 static inline struct snapshot_t *snapshot_t_mut(struct bch_fs *c, u32 id)
190 size_t idx = U32_MAX - id;
191 struct snapshot_table *table =
192 rcu_dereference_protected(c->snapshots,
193 lockdep_is_held(&c->snapshot_table_lock));
195 lockdep_assert_held(&c->snapshot_table_lock);
197 if (likely(table && idx < table->nr))
198 return &table->s[idx];
200 return __snapshot_t_mut(c, id);
203 void bch2_snapshot_to_text(struct printbuf *out, struct bch_fs *c,
206 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(k);
208 prt_printf(out, "is_subvol %llu deleted %llu parent %10u children %10u %10u subvol %u tree %u",
209 BCH_SNAPSHOT_SUBVOL(s.v),
210 BCH_SNAPSHOT_DELETED(s.v),
211 le32_to_cpu(s.v->parent),
212 le32_to_cpu(s.v->children[0]),
213 le32_to_cpu(s.v->children[1]),
214 le32_to_cpu(s.v->subvol),
215 le32_to_cpu(s.v->tree));
217 if (bkey_val_bytes(k.k) > offsetof(struct bch_snapshot, depth))
218 prt_printf(out, " depth %u skiplist %u %u %u",
219 le32_to_cpu(s.v->depth),
220 le32_to_cpu(s.v->skip[0]),
221 le32_to_cpu(s.v->skip[1]),
222 le32_to_cpu(s.v->skip[2]));
225 int bch2_snapshot_invalid(struct bch_fs *c, struct bkey_s_c k,
226 enum bkey_invalid_flags flags,
227 struct printbuf *err)
229 struct bkey_s_c_snapshot s;
233 bkey_fsck_err_on(bkey_gt(k.k->p, POS(0, U32_MAX)) ||
234 bkey_lt(k.k->p, POS(0, 1)), c, err,
238 s = bkey_s_c_to_snapshot(k);
240 id = le32_to_cpu(s.v->parent);
241 bkey_fsck_err_on(id && id <= k.k->p.offset, c, err,
243 "bad parent node (%u <= %llu)",
246 bkey_fsck_err_on(le32_to_cpu(s.v->children[0]) < le32_to_cpu(s.v->children[1]), c, err,
247 snapshot_children_not_normalized,
248 "children not normalized");
250 bkey_fsck_err_on(s.v->children[0] && s.v->children[0] == s.v->children[1], c, err,
251 snapshot_child_duplicate,
252 "duplicate child nodes");
254 for (i = 0; i < 2; i++) {
255 id = le32_to_cpu(s.v->children[i]);
257 bkey_fsck_err_on(id >= k.k->p.offset, c, err,
259 "bad child node (%u >= %llu)",
263 if (bkey_val_bytes(k.k) > offsetof(struct bch_snapshot, skip)) {
264 bkey_fsck_err_on(le32_to_cpu(s.v->skip[0]) > le32_to_cpu(s.v->skip[1]) ||
265 le32_to_cpu(s.v->skip[1]) > le32_to_cpu(s.v->skip[2]), c, err,
266 snapshot_skiplist_not_normalized,
267 "skiplist not normalized");
269 for (i = 0; i < ARRAY_SIZE(s.v->skip); i++) {
270 id = le32_to_cpu(s.v->skip[i]);
272 bkey_fsck_err_on(id && id < le32_to_cpu(s.v->parent), c, err,
273 snapshot_skiplist_bad,
274 "bad skiplist node %u", id);
281 static void __set_is_ancestor_bitmap(struct bch_fs *c, u32 id)
283 struct snapshot_t *t = snapshot_t_mut(c, id);
286 while ((parent = bch2_snapshot_parent_early(c, parent)) &&
287 parent - id - 1 < IS_ANCESTOR_BITMAP)
288 __set_bit(parent - id - 1, t->is_ancestor);
291 static void set_is_ancestor_bitmap(struct bch_fs *c, u32 id)
293 mutex_lock(&c->snapshot_table_lock);
294 __set_is_ancestor_bitmap(c, id);
295 mutex_unlock(&c->snapshot_table_lock);
298 static int __bch2_mark_snapshot(struct btree_trans *trans,
299 enum btree_id btree, unsigned level,
300 struct bkey_s_c old, struct bkey_s_c new,
303 struct bch_fs *c = trans->c;
304 struct snapshot_t *t;
305 u32 id = new.k->p.offset;
308 mutex_lock(&c->snapshot_table_lock);
310 t = snapshot_t_mut(c, id);
312 ret = -BCH_ERR_ENOMEM_mark_snapshot;
316 if (new.k->type == KEY_TYPE_snapshot) {
317 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(new);
319 t->parent = le32_to_cpu(s.v->parent);
320 t->children[0] = le32_to_cpu(s.v->children[0]);
321 t->children[1] = le32_to_cpu(s.v->children[1]);
322 t->subvol = BCH_SNAPSHOT_SUBVOL(s.v) ? le32_to_cpu(s.v->subvol) : 0;
323 t->tree = le32_to_cpu(s.v->tree);
325 if (bkey_val_bytes(s.k) > offsetof(struct bch_snapshot, depth)) {
326 t->depth = le32_to_cpu(s.v->depth);
327 t->skip[0] = le32_to_cpu(s.v->skip[0]);
328 t->skip[1] = le32_to_cpu(s.v->skip[1]);
329 t->skip[2] = le32_to_cpu(s.v->skip[2]);
337 __set_is_ancestor_bitmap(c, id);
339 if (BCH_SNAPSHOT_DELETED(s.v)) {
340 set_bit(BCH_FS_need_delete_dead_snapshots, &c->flags);
341 if (c->curr_recovery_pass > BCH_RECOVERY_PASS_delete_dead_snapshots)
342 bch2_delete_dead_snapshots_async(c);
345 memset(t, 0, sizeof(*t));
348 mutex_unlock(&c->snapshot_table_lock);
352 int bch2_mark_snapshot(struct btree_trans *trans,
353 enum btree_id btree, unsigned level,
354 struct bkey_s_c old, struct bkey_s new,
357 return __bch2_mark_snapshot(trans, btree, level, old, new.s_c, flags);
360 int bch2_snapshot_lookup(struct btree_trans *trans, u32 id,
361 struct bch_snapshot *s)
363 return bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots, POS(0, id),
364 BTREE_ITER_WITH_UPDATES, snapshot, s);
367 static int bch2_snapshot_live(struct btree_trans *trans, u32 id)
369 struct bch_snapshot v;
375 ret = bch2_snapshot_lookup(trans, id, &v);
376 if (bch2_err_matches(ret, ENOENT))
377 bch_err(trans->c, "snapshot node %u not found", id);
381 return !BCH_SNAPSHOT_DELETED(&v);
385 * If @k is a snapshot with just one live child, it's part of a linear chain,
386 * which we consider to be an equivalence class: and then after snapshot
387 * deletion cleanup, there should only be a single key at a given position in
388 * this equivalence class.
390 * This sets the equivalence class of @k to be the child's equivalence class, if
391 * it's part of such a linear chain: this correctly sets equivalence classes on
392 * startup if we run leaf to root (i.e. in natural key order).
394 static int bch2_snapshot_set_equiv(struct btree_trans *trans, struct bkey_s_c k)
396 struct bch_fs *c = trans->c;
397 unsigned i, nr_live = 0, live_idx = 0;
398 struct bkey_s_c_snapshot snap;
399 u32 id = k.k->p.offset, child[2];
401 if (k.k->type != KEY_TYPE_snapshot)
404 snap = bkey_s_c_to_snapshot(k);
406 child[0] = le32_to_cpu(snap.v->children[0]);
407 child[1] = le32_to_cpu(snap.v->children[1]);
409 for (i = 0; i < 2; i++) {
410 int ret = bch2_snapshot_live(trans, child[i]);
420 mutex_lock(&c->snapshot_table_lock);
422 snapshot_t_mut(c, id)->equiv = nr_live == 1
423 ? snapshot_t_mut(c, child[live_idx])->equiv
426 mutex_unlock(&c->snapshot_table_lock);
433 static u32 bch2_snapshot_child(struct bch_fs *c, u32 id, unsigned child)
435 return snapshot_t(c, id)->children[child];
438 static u32 bch2_snapshot_left_child(struct bch_fs *c, u32 id)
440 return bch2_snapshot_child(c, id, 0);
443 static u32 bch2_snapshot_right_child(struct bch_fs *c, u32 id)
445 return bch2_snapshot_child(c, id, 1);
448 static u32 bch2_snapshot_tree_next(struct bch_fs *c, u32 id)
452 n = bch2_snapshot_left_child(c, id);
456 while ((parent = bch2_snapshot_parent(c, id))) {
457 n = bch2_snapshot_right_child(c, parent);
466 static u32 bch2_snapshot_tree_oldest_subvol(struct bch_fs *c, u32 snapshot_root)
468 u32 id = snapshot_root;
472 s = snapshot_t(c, id)->subvol;
474 if (s && (!subvol || s < subvol))
477 id = bch2_snapshot_tree_next(c, id);
483 static int bch2_snapshot_tree_master_subvol(struct btree_trans *trans,
484 u32 snapshot_root, u32 *subvol_id)
486 struct bch_fs *c = trans->c;
487 struct btree_iter iter;
492 for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN,
494 if (k.k->type != KEY_TYPE_subvolume)
497 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
498 if (!bch2_snapshot_is_ancestor(c, le32_to_cpu(s.v->snapshot), snapshot_root))
500 if (!BCH_SUBVOLUME_SNAP(s.v)) {
501 *subvol_id = s.k->p.offset;
507 bch2_trans_iter_exit(trans, &iter);
509 if (!ret && !found) {
510 struct bkey_i_subvolume *u;
512 *subvol_id = bch2_snapshot_tree_oldest_subvol(c, snapshot_root);
514 u = bch2_bkey_get_mut_typed(trans, &iter,
515 BTREE_ID_subvolumes, POS(0, *subvol_id),
517 ret = PTR_ERR_OR_ZERO(u);
521 SET_BCH_SUBVOLUME_SNAP(&u->v, false);
527 static int check_snapshot_tree(struct btree_trans *trans,
528 struct btree_iter *iter,
531 struct bch_fs *c = trans->c;
532 struct bkey_s_c_snapshot_tree st;
533 struct bch_snapshot s;
534 struct bch_subvolume subvol;
535 struct printbuf buf = PRINTBUF;
539 if (k.k->type != KEY_TYPE_snapshot_tree)
542 st = bkey_s_c_to_snapshot_tree(k);
543 root_id = le32_to_cpu(st.v->root_snapshot);
545 ret = bch2_snapshot_lookup(trans, root_id, &s);
546 if (ret && !bch2_err_matches(ret, ENOENT))
549 if (fsck_err_on(ret ||
550 root_id != bch2_snapshot_root(c, root_id) ||
551 st.k->p.offset != le32_to_cpu(s.tree),
552 c, snapshot_tree_to_missing_snapshot,
553 "snapshot tree points to missing/incorrect snapshot:\n %s",
554 (bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
555 ret = bch2_btree_delete_at(trans, iter, 0);
559 ret = bch2_subvolume_get(trans, le32_to_cpu(st.v->master_subvol),
561 if (ret && !bch2_err_matches(ret, ENOENT))
565 c, snapshot_tree_to_missing_subvol,
566 "snapshot tree points to missing subvolume:\n %s",
567 (printbuf_reset(&buf),
568 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
569 fsck_err_on(!bch2_snapshot_is_ancestor(c,
570 le32_to_cpu(subvol.snapshot),
572 c, snapshot_tree_to_wrong_subvol,
573 "snapshot tree points to subvolume that does not point to snapshot in this tree:\n %s",
574 (printbuf_reset(&buf),
575 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
576 fsck_err_on(BCH_SUBVOLUME_SNAP(&subvol),
577 c, snapshot_tree_to_snapshot_subvol,
578 "snapshot tree points to snapshot subvolume:\n %s",
579 (printbuf_reset(&buf),
580 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
581 struct bkey_i_snapshot_tree *u;
584 ret = bch2_snapshot_tree_master_subvol(trans, root_id, &subvol_id);
587 if (bch2_err_matches(ret, ENOENT)) { /* nothing to be done here */
595 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot_tree);
596 ret = PTR_ERR_OR_ZERO(u);
600 u->v.master_subvol = cpu_to_le32(subvol_id);
601 st = snapshot_tree_i_to_s_c(u);
610 * For each snapshot_tree, make sure it points to the root of a snapshot tree
611 * and that snapshot entry points back to it, or delete it.
613 * And, make sure it points to a subvolume within that snapshot tree, or correct
614 * it to point to the oldest subvolume within that snapshot tree.
616 int bch2_check_snapshot_trees(struct bch_fs *c)
618 int ret = bch2_trans_run(c,
619 for_each_btree_key_commit(trans, iter,
620 BTREE_ID_snapshot_trees, POS_MIN,
621 BTREE_ITER_PREFETCH, k,
622 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
623 check_snapshot_tree(trans, &iter, k)));
629 * Look up snapshot tree for @tree_id and find root,
630 * make sure @snap_id is a descendent:
632 static int snapshot_tree_ptr_good(struct btree_trans *trans,
633 u32 snap_id, u32 tree_id)
635 struct bch_snapshot_tree s_t;
636 int ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
638 if (bch2_err_matches(ret, ENOENT))
643 return bch2_snapshot_is_ancestor_early(trans->c, snap_id, le32_to_cpu(s_t.root_snapshot));
646 u32 bch2_snapshot_skiplist_get(struct bch_fs *c, u32 id)
648 const struct snapshot_t *s;
654 s = snapshot_t(c, id);
656 id = bch2_snapshot_nth_parent(c, id, get_random_u32_below(s->depth));
662 static int snapshot_skiplist_good(struct btree_trans *trans, u32 id, struct bch_snapshot s)
666 for (i = 0; i < 3; i++)
671 if (!bch2_snapshot_is_ancestor_early(trans->c, id, le32_to_cpu(s.skip[i])))
679 * snapshot_tree pointer was incorrect: look up root snapshot node, make sure
680 * its snapshot_tree pointer is correct (allocate new one if necessary), then
681 * update this node's pointer to root node's pointer:
683 static int snapshot_tree_ptr_repair(struct btree_trans *trans,
684 struct btree_iter *iter,
686 struct bch_snapshot *s)
688 struct bch_fs *c = trans->c;
689 struct btree_iter root_iter;
690 struct bch_snapshot_tree s_t;
691 struct bkey_s_c_snapshot root;
692 struct bkey_i_snapshot *u;
693 u32 root_id = bch2_snapshot_root(c, k.k->p.offset), tree_id;
696 root = bch2_bkey_get_iter_typed(trans, &root_iter,
697 BTREE_ID_snapshots, POS(0, root_id),
698 BTREE_ITER_WITH_UPDATES, snapshot);
699 ret = bkey_err(root);
703 tree_id = le32_to_cpu(root.v->tree);
705 ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
706 if (ret && !bch2_err_matches(ret, ENOENT))
709 if (ret || le32_to_cpu(s_t.root_snapshot) != root_id) {
710 u = bch2_bkey_make_mut_typed(trans, &root_iter, &root.s_c, 0, snapshot);
711 ret = PTR_ERR_OR_ZERO(u) ?:
712 bch2_snapshot_tree_create(trans, root_id,
713 bch2_snapshot_tree_oldest_subvol(c, root_id),
718 u->v.tree = cpu_to_le32(tree_id);
719 if (k.k->p.offset == root_id)
723 if (k.k->p.offset != root_id) {
724 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
725 ret = PTR_ERR_OR_ZERO(u);
729 u->v.tree = cpu_to_le32(tree_id);
733 bch2_trans_iter_exit(trans, &root_iter);
737 static int check_snapshot(struct btree_trans *trans,
738 struct btree_iter *iter,
741 struct bch_fs *c = trans->c;
742 struct bch_snapshot s;
743 struct bch_subvolume subvol;
744 struct bch_snapshot v;
745 struct bkey_i_snapshot *u;
746 u32 parent_id = bch2_snapshot_parent_early(c, k.k->p.offset);
748 struct printbuf buf = PRINTBUF;
752 if (k.k->type != KEY_TYPE_snapshot)
755 memset(&s, 0, sizeof(s));
756 memcpy(&s, k.v, min(sizeof(s), bkey_val_bytes(k.k)));
758 id = le32_to_cpu(s.parent);
760 ret = bch2_snapshot_lookup(trans, id, &v);
761 if (bch2_err_matches(ret, ENOENT))
762 bch_err(c, "snapshot with nonexistent parent:\n %s",
763 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
767 if (le32_to_cpu(v.children[0]) != k.k->p.offset &&
768 le32_to_cpu(v.children[1]) != k.k->p.offset) {
769 bch_err(c, "snapshot parent %u missing pointer to child %llu",
776 for (i = 0; i < 2 && s.children[i]; i++) {
777 id = le32_to_cpu(s.children[i]);
779 ret = bch2_snapshot_lookup(trans, id, &v);
780 if (bch2_err_matches(ret, ENOENT))
781 bch_err(c, "snapshot node %llu has nonexistent child %u",
786 if (le32_to_cpu(v.parent) != k.k->p.offset) {
787 bch_err(c, "snapshot child %u has wrong parent (got %u should be %llu)",
788 id, le32_to_cpu(v.parent), k.k->p.offset);
794 bool should_have_subvol = BCH_SNAPSHOT_SUBVOL(&s) &&
795 !BCH_SNAPSHOT_DELETED(&s);
797 if (should_have_subvol) {
798 id = le32_to_cpu(s.subvol);
799 ret = bch2_subvolume_get(trans, id, 0, false, &subvol);
800 if (bch2_err_matches(ret, ENOENT))
801 bch_err(c, "snapshot points to nonexistent subvolume:\n %s",
802 (bch2_bkey_val_to_text(&buf, c, k), buf.buf));
806 if (BCH_SNAPSHOT_SUBVOL(&s) != (le32_to_cpu(subvol.snapshot) == k.k->p.offset)) {
807 bch_err(c, "snapshot node %llu has wrong BCH_SNAPSHOT_SUBVOL",
813 if (fsck_err_on(s.subvol,
814 c, snapshot_should_not_have_subvol,
815 "snapshot should not point to subvol:\n %s",
816 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
817 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
818 ret = PTR_ERR_OR_ZERO(u);
827 ret = snapshot_tree_ptr_good(trans, k.k->p.offset, le32_to_cpu(s.tree));
831 if (fsck_err_on(!ret, c, snapshot_to_bad_snapshot_tree,
832 "snapshot points to missing/incorrect tree:\n %s",
833 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
834 ret = snapshot_tree_ptr_repair(trans, iter, k, &s);
840 real_depth = bch2_snapshot_depth(c, parent_id);
842 if (fsck_err_on(le32_to_cpu(s.depth) != real_depth,
843 c, snapshot_bad_depth,
844 "snapshot with incorrect depth field, should be %u:\n %s",
845 real_depth, (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
846 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
847 ret = PTR_ERR_OR_ZERO(u);
851 u->v.depth = cpu_to_le32(real_depth);
855 ret = snapshot_skiplist_good(trans, k.k->p.offset, s);
859 if (fsck_err_on(!ret, c, snapshot_bad_skiplist,
860 "snapshot with bad skiplist field:\n %s",
861 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
862 u = bch2_bkey_make_mut_typed(trans, iter, &k, 0, snapshot);
863 ret = PTR_ERR_OR_ZERO(u);
867 for (i = 0; i < ARRAY_SIZE(u->v.skip); i++)
868 u->v.skip[i] = cpu_to_le32(bch2_snapshot_skiplist_get(c, parent_id));
870 bubble_sort(u->v.skip, ARRAY_SIZE(u->v.skip), cmp_le32);
880 int bch2_check_snapshots(struct bch_fs *c)
883 * We iterate backwards as checking/fixing the depth field requires that
884 * the parent's depth already be correct:
886 int ret = bch2_trans_run(c,
887 for_each_btree_key_reverse_commit(trans, iter,
888 BTREE_ID_snapshots, POS_MAX,
889 BTREE_ITER_PREFETCH, k,
890 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
891 check_snapshot(trans, &iter, k)));
896 static int check_snapshot_exists(struct btree_trans *trans, u32 id)
898 struct bch_fs *c = trans->c;
900 if (bch2_snapshot_equiv(c, id))
904 int ret = bch2_snapshot_tree_create(trans, id, 0, &tree_id);
908 struct bkey_i_snapshot *snapshot = bch2_trans_kmalloc(trans, sizeof(*snapshot));
909 ret = PTR_ERR_OR_ZERO(snapshot);
913 bkey_snapshot_init(&snapshot->k_i);
914 snapshot->k.p = POS(0, id);
915 snapshot->v.tree = cpu_to_le32(tree_id);
916 snapshot->v.btime.lo = cpu_to_le64(bch2_current_time(c));
918 return bch2_btree_insert_trans(trans, BTREE_ID_snapshots, &snapshot->k_i, 0) ?:
919 bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0,
920 bkey_s_c_null, bkey_i_to_s(&snapshot->k_i), 0) ?:
921 bch2_snapshot_set_equiv(trans, bkey_i_to_s_c(&snapshot->k_i));
924 /* Figure out which snapshot nodes belong in the same tree: */
925 struct snapshot_tree_reconstruct {
928 snapshot_id_list cur_ids;
929 DARRAY(snapshot_id_list) trees;
932 static void snapshot_tree_reconstruct_exit(struct snapshot_tree_reconstruct *r)
934 darray_for_each(r->trees, i)
936 darray_exit(&r->trees);
937 darray_exit(&r->cur_ids);
940 static inline bool same_snapshot(struct snapshot_tree_reconstruct *r, struct bpos pos)
942 return r->btree == BTREE_ID_inodes
943 ? r->cur_pos.offset == pos.offset
944 : r->cur_pos.inode == pos.inode;
947 static inline bool snapshot_id_lists_have_common(snapshot_id_list *l, snapshot_id_list *r)
949 darray_for_each(*l, i)
950 if (snapshot_list_has_id(r, *i))
955 static void snapshot_id_list_to_text(struct printbuf *out, snapshot_id_list *s)
958 darray_for_each(*s, i) {
962 prt_printf(out, "%u", *i);
966 static int snapshot_tree_reconstruct_next(struct bch_fs *c, struct snapshot_tree_reconstruct *r)
969 darray_for_each(r->trees, i)
970 if (snapshot_id_lists_have_common(i, &r->cur_ids)) {
971 int ret = snapshot_list_merge(c, i, &r->cur_ids);
976 darray_push(&r->trees, r->cur_ids);
977 darray_init(&r->cur_ids);
984 static int get_snapshot_trees(struct bch_fs *c, struct snapshot_tree_reconstruct *r, struct bpos pos)
986 if (!same_snapshot(r, pos))
987 snapshot_tree_reconstruct_next(c, r);
989 return snapshot_list_add_nodup(c, &r->cur_ids, pos.snapshot);
992 int bch2_reconstruct_snapshots(struct bch_fs *c)
994 struct btree_trans *trans = bch2_trans_get(c);
995 struct printbuf buf = PRINTBUF;
996 struct snapshot_tree_reconstruct r = {};
999 for (unsigned btree = 0; btree < BTREE_ID_NR; btree++) {
1000 if (btree_type_has_snapshots(btree)) {
1003 ret = for_each_btree_key(trans, iter, btree, POS_MIN,
1004 BTREE_ITER_ALL_SNAPSHOTS|BTREE_ITER_PREFETCH, k, ({
1005 get_snapshot_trees(c, &r, k.k->p);
1010 snapshot_tree_reconstruct_next(c, &r);
1014 darray_for_each(r.trees, t) {
1015 printbuf_reset(&buf);
1016 snapshot_id_list_to_text(&buf, t);
1018 darray_for_each(*t, id) {
1019 if (fsck_err_on(!bch2_snapshot_equiv(c, *id),
1020 c, snapshot_node_missing,
1021 "snapshot node %u from tree %s missing", *id, buf.buf)) {
1023 bch_err(c, "cannot reconstruct snapshot trees with multiple nodes");
1024 ret = -BCH_ERR_fsck_repair_unimplemented;
1028 ret = commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1029 check_snapshot_exists(trans, *id));
1037 bch2_trans_put(trans);
1038 snapshot_tree_reconstruct_exit(&r);
1039 printbuf_exit(&buf);
1045 * Mark a snapshot as deleted, for future cleanup:
1047 int bch2_snapshot_node_set_deleted(struct btree_trans *trans, u32 id)
1049 struct btree_iter iter;
1050 struct bkey_i_snapshot *s;
1053 s = bch2_bkey_get_mut_typed(trans, &iter,
1054 BTREE_ID_snapshots, POS(0, id),
1056 ret = PTR_ERR_OR_ZERO(s);
1057 if (unlikely(ret)) {
1058 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT),
1059 trans->c, "missing snapshot %u", id);
1063 /* already deleted? */
1064 if (BCH_SNAPSHOT_DELETED(&s->v))
1067 SET_BCH_SNAPSHOT_DELETED(&s->v, true);
1068 SET_BCH_SNAPSHOT_SUBVOL(&s->v, false);
1071 bch2_trans_iter_exit(trans, &iter);
1075 static inline void normalize_snapshot_child_pointers(struct bch_snapshot *s)
1077 if (le32_to_cpu(s->children[0]) < le32_to_cpu(s->children[1]))
1078 swap(s->children[0], s->children[1]);
1081 static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
1083 struct bch_fs *c = trans->c;
1084 struct btree_iter iter, p_iter = (struct btree_iter) { NULL };
1085 struct btree_iter c_iter = (struct btree_iter) { NULL };
1086 struct btree_iter tree_iter = (struct btree_iter) { NULL };
1087 struct bkey_s_c_snapshot s;
1088 u32 parent_id, child_id;
1092 s = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_snapshots, POS(0, id),
1093 BTREE_ITER_INTENT, snapshot);
1095 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
1096 "missing snapshot %u", id);
1101 BUG_ON(s.v->children[1]);
1103 parent_id = le32_to_cpu(s.v->parent);
1104 child_id = le32_to_cpu(s.v->children[0]);
1107 struct bkey_i_snapshot *parent;
1109 parent = bch2_bkey_get_mut_typed(trans, &p_iter,
1110 BTREE_ID_snapshots, POS(0, parent_id),
1112 ret = PTR_ERR_OR_ZERO(parent);
1113 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
1114 "missing snapshot %u", parent_id);
1118 /* find entry in parent->children for node being deleted */
1119 for (i = 0; i < 2; i++)
1120 if (le32_to_cpu(parent->v.children[i]) == id)
1123 if (bch2_fs_inconsistent_on(i == 2, c,
1124 "snapshot %u missing child pointer to %u",
1128 parent->v.children[i] = cpu_to_le32(child_id);
1130 normalize_snapshot_child_pointers(&parent->v);
1134 struct bkey_i_snapshot *child;
1136 child = bch2_bkey_get_mut_typed(trans, &c_iter,
1137 BTREE_ID_snapshots, POS(0, child_id),
1139 ret = PTR_ERR_OR_ZERO(child);
1140 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
1141 "missing snapshot %u", child_id);
1145 child->v.parent = cpu_to_le32(parent_id);
1147 if (!child->v.parent) {
1148 child->v.skip[0] = 0;
1149 child->v.skip[1] = 0;
1150 child->v.skip[2] = 0;
1156 * We're deleting the root of a snapshot tree: update the
1157 * snapshot_tree entry to point to the new root, or delete it if
1158 * this is the last snapshot ID in this tree:
1160 struct bkey_i_snapshot_tree *s_t;
1162 BUG_ON(s.v->children[1]);
1164 s_t = bch2_bkey_get_mut_typed(trans, &tree_iter,
1165 BTREE_ID_snapshot_trees, POS(0, le32_to_cpu(s.v->tree)),
1167 ret = PTR_ERR_OR_ZERO(s_t);
1171 if (s.v->children[0]) {
1172 s_t->v.root_snapshot = s.v->children[0];
1174 s_t->k.type = KEY_TYPE_deleted;
1175 set_bkey_val_u64s(&s_t->k, 0);
1179 ret = bch2_btree_delete_at(trans, &iter, 0);
1181 bch2_trans_iter_exit(trans, &tree_iter);
1182 bch2_trans_iter_exit(trans, &p_iter);
1183 bch2_trans_iter_exit(trans, &c_iter);
1184 bch2_trans_iter_exit(trans, &iter);
1188 static int create_snapids(struct btree_trans *trans, u32 parent, u32 tree,
1190 u32 *snapshot_subvols,
1191 unsigned nr_snapids)
1193 struct bch_fs *c = trans->c;
1194 struct btree_iter iter;
1195 struct bkey_i_snapshot *n;
1198 u32 depth = bch2_snapshot_depth(c, parent);
1201 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
1202 POS_MIN, BTREE_ITER_INTENT);
1203 k = bch2_btree_iter_peek(&iter);
1208 for (i = 0; i < nr_snapids; i++) {
1209 k = bch2_btree_iter_prev_slot(&iter);
1214 if (!k.k || !k.k->p.offset) {
1215 ret = -BCH_ERR_ENOSPC_snapshot_create;
1219 n = bch2_bkey_alloc(trans, &iter, 0, snapshot);
1220 ret = PTR_ERR_OR_ZERO(n);
1225 n->v.parent = cpu_to_le32(parent);
1226 n->v.subvol = cpu_to_le32(snapshot_subvols[i]);
1227 n->v.tree = cpu_to_le32(tree);
1228 n->v.depth = cpu_to_le32(depth);
1229 n->v.btime.lo = cpu_to_le64(bch2_current_time(c));
1232 for (j = 0; j < ARRAY_SIZE(n->v.skip); j++)
1233 n->v.skip[j] = cpu_to_le32(bch2_snapshot_skiplist_get(c, parent));
1235 bubble_sort(n->v.skip, ARRAY_SIZE(n->v.skip), cmp_le32);
1236 SET_BCH_SNAPSHOT_SUBVOL(&n->v, true);
1238 ret = __bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0,
1239 bkey_s_c_null, bkey_i_to_s_c(&n->k_i), 0);
1243 new_snapids[i] = iter.pos.offset;
1245 mutex_lock(&c->snapshot_table_lock);
1246 snapshot_t_mut(c, new_snapids[i])->equiv = new_snapids[i];
1247 mutex_unlock(&c->snapshot_table_lock);
1250 bch2_trans_iter_exit(trans, &iter);
1255 * Create new snapshot IDs as children of an existing snapshot ID:
1257 static int bch2_snapshot_node_create_children(struct btree_trans *trans, u32 parent,
1259 u32 *snapshot_subvols,
1260 unsigned nr_snapids)
1262 struct btree_iter iter;
1263 struct bkey_i_snapshot *n_parent;
1266 n_parent = bch2_bkey_get_mut_typed(trans, &iter,
1267 BTREE_ID_snapshots, POS(0, parent),
1269 ret = PTR_ERR_OR_ZERO(n_parent);
1270 if (unlikely(ret)) {
1271 if (bch2_err_matches(ret, ENOENT))
1272 bch_err(trans->c, "snapshot %u not found", parent);
1276 if (n_parent->v.children[0] || n_parent->v.children[1]) {
1277 bch_err(trans->c, "Trying to add child snapshot nodes to parent that already has children");
1282 ret = create_snapids(trans, parent, le32_to_cpu(n_parent->v.tree),
1283 new_snapids, snapshot_subvols, nr_snapids);
1287 n_parent->v.children[0] = cpu_to_le32(new_snapids[0]);
1288 n_parent->v.children[1] = cpu_to_le32(new_snapids[1]);
1289 n_parent->v.subvol = 0;
1290 SET_BCH_SNAPSHOT_SUBVOL(&n_parent->v, false);
1292 bch2_trans_iter_exit(trans, &iter);
1297 * Create a snapshot node that is the root of a new tree:
1299 static int bch2_snapshot_node_create_tree(struct btree_trans *trans,
1301 u32 *snapshot_subvols,
1302 unsigned nr_snapids)
1304 struct bkey_i_snapshot_tree *n_tree;
1307 n_tree = __bch2_snapshot_tree_create(trans);
1308 ret = PTR_ERR_OR_ZERO(n_tree) ?:
1309 create_snapids(trans, 0, n_tree->k.p.offset,
1310 new_snapids, snapshot_subvols, nr_snapids);
1314 n_tree->v.master_subvol = cpu_to_le32(snapshot_subvols[0]);
1315 n_tree->v.root_snapshot = cpu_to_le32(new_snapids[0]);
1319 int bch2_snapshot_node_create(struct btree_trans *trans, u32 parent,
1321 u32 *snapshot_subvols,
1322 unsigned nr_snapids)
1324 BUG_ON((parent == 0) != (nr_snapids == 1));
1325 BUG_ON((parent != 0) != (nr_snapids == 2));
1328 ? bch2_snapshot_node_create_children(trans, parent,
1329 new_snapids, snapshot_subvols, nr_snapids)
1330 : bch2_snapshot_node_create_tree(trans,
1331 new_snapids, snapshot_subvols, nr_snapids);
1336 * If we have an unlinked inode in an internal snapshot node, and the inode
1337 * really has been deleted in all child snapshots, how does this get cleaned up?
1339 * first there is the problem of how keys that have been overwritten in all
1340 * child snapshots get deleted (unimplemented?), but inodes may perhaps be
1343 * also: unlinked inode in internal snapshot appears to not be getting deleted
1344 * correctly if inode doesn't exist in leaf snapshots
1348 * for a key in an interior snapshot node that needs work to be done that
1349 * requires it to be mutated: iterate over all descendent leaf nodes and copy
1350 * that key to snapshot leaf nodes, where we can mutate it
1353 static int snapshot_delete_key(struct btree_trans *trans,
1354 struct btree_iter *iter,
1356 snapshot_id_list *deleted,
1357 snapshot_id_list *equiv_seen,
1358 struct bpos *last_pos)
1360 struct bch_fs *c = trans->c;
1361 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
1363 if (!bkey_eq(k.k->p, *last_pos))
1367 if (snapshot_list_has_id(deleted, k.k->p.snapshot) ||
1368 snapshot_list_has_id(equiv_seen, equiv)) {
1369 return bch2_btree_delete_at(trans, iter,
1370 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1372 return snapshot_list_add(c, equiv_seen, equiv);
1376 static int move_key_to_correct_snapshot(struct btree_trans *trans,
1377 struct btree_iter *iter,
1380 struct bch_fs *c = trans->c;
1381 u32 equiv = bch2_snapshot_equiv(c, k.k->p.snapshot);
1384 * When we have a linear chain of snapshot nodes, we consider
1385 * those to form an equivalence class: we're going to collapse
1386 * them all down to a single node, and keep the leaf-most node -
1387 * which has the same id as the equivalence class id.
1389 * If there are multiple keys in different snapshots at the same
1390 * position, we're only going to keep the one in the newest
1391 * snapshot - the rest have been overwritten and are redundant,
1392 * and for the key we're going to keep we need to move it to the
1393 * equivalance class ID if it's not there already.
1395 if (equiv != k.k->p.snapshot) {
1396 struct bkey_i *new = bch2_bkey_make_mut_noupdate(trans, k);
1397 struct btree_iter new_iter;
1400 ret = PTR_ERR_OR_ZERO(new);
1404 new->k.p.snapshot = equiv;
1406 bch2_trans_iter_init(trans, &new_iter, iter->btree_id, new->k.p,
1407 BTREE_ITER_ALL_SNAPSHOTS|
1411 ret = bch2_btree_iter_traverse(&new_iter) ?:
1412 bch2_trans_update(trans, &new_iter, new,
1413 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
1414 bch2_btree_delete_at(trans, iter,
1415 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
1416 bch2_trans_iter_exit(trans, &new_iter);
1424 static int bch2_snapshot_needs_delete(struct btree_trans *trans, struct bkey_s_c k)
1426 struct bkey_s_c_snapshot snap;
1430 if (k.k->type != KEY_TYPE_snapshot)
1433 snap = bkey_s_c_to_snapshot(k);
1434 if (BCH_SNAPSHOT_DELETED(snap.v) ||
1435 BCH_SNAPSHOT_SUBVOL(snap.v))
1438 children[0] = le32_to_cpu(snap.v->children[0]);
1439 children[1] = le32_to_cpu(snap.v->children[1]);
1441 ret = bch2_snapshot_live(trans, children[0]) ?:
1442 bch2_snapshot_live(trans, children[1]);
1449 * For a given snapshot, if it doesn't have a subvolume that points to it, and
1450 * it doesn't have child snapshot nodes - it's now redundant and we can mark it
1453 static int bch2_delete_redundant_snapshot(struct btree_trans *trans, struct bkey_s_c k)
1455 int ret = bch2_snapshot_needs_delete(trans, k);
1459 : bch2_snapshot_node_set_deleted(trans, k.k->p.offset);
1462 static inline u32 bch2_snapshot_nth_parent_skip(struct bch_fs *c, u32 id, u32 n,
1463 snapshot_id_list *skip)
1466 while (snapshot_list_has_id(skip, id))
1467 id = __bch2_snapshot_parent(c, id);
1471 id = __bch2_snapshot_parent(c, id);
1472 } while (snapshot_list_has_id(skip, id));
1479 static int bch2_fix_child_of_deleted_snapshot(struct btree_trans *trans,
1480 struct btree_iter *iter, struct bkey_s_c k,
1481 snapshot_id_list *deleted)
1483 struct bch_fs *c = trans->c;
1484 u32 nr_deleted_ancestors = 0;
1485 struct bkey_i_snapshot *s;
1488 if (k.k->type != KEY_TYPE_snapshot)
1491 if (snapshot_list_has_id(deleted, k.k->p.offset))
1494 s = bch2_bkey_make_mut_noupdate_typed(trans, k, snapshot);
1495 ret = PTR_ERR_OR_ZERO(s);
1499 darray_for_each(*deleted, i)
1500 nr_deleted_ancestors += bch2_snapshot_is_ancestor(c, s->k.p.offset, *i);
1502 if (!nr_deleted_ancestors)
1505 le32_add_cpu(&s->v.depth, -nr_deleted_ancestors);
1512 u32 depth = le32_to_cpu(s->v.depth);
1513 u32 parent = bch2_snapshot_parent(c, s->k.p.offset);
1515 for (unsigned j = 0; j < ARRAY_SIZE(s->v.skip); j++) {
1516 u32 id = le32_to_cpu(s->v.skip[j]);
1518 if (snapshot_list_has_id(deleted, id)) {
1519 id = bch2_snapshot_nth_parent_skip(c,
1522 ? get_random_u32_below(depth - 1)
1525 s->v.skip[j] = cpu_to_le32(id);
1529 bubble_sort(s->v.skip, ARRAY_SIZE(s->v.skip), cmp_le32);
1532 return bch2_trans_update(trans, iter, &s->k_i, 0);
1535 int bch2_delete_dead_snapshots(struct bch_fs *c)
1537 struct btree_trans *trans;
1538 snapshot_id_list deleted = { 0 };
1539 snapshot_id_list deleted_interior = { 0 };
1543 if (!test_and_clear_bit(BCH_FS_need_delete_dead_snapshots, &c->flags))
1546 if (!test_bit(BCH_FS_started, &c->flags)) {
1547 ret = bch2_fs_read_write_early(c);
1548 bch_err_msg(c, ret, "deleting dead snapshots: error going rw");
1553 trans = bch2_trans_get(c);
1556 * For every snapshot node: If we have no live children and it's not
1557 * pointed to by a subvolume, delete it:
1559 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_snapshots,
1562 bch2_delete_redundant_snapshot(trans, k));
1563 bch_err_msg(c, ret, "deleting redundant snapshots");
1567 ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1569 bch2_snapshot_set_equiv(trans, k));
1570 bch_err_msg(c, ret, "in bch2_snapshots_set_equiv");
1574 ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1576 if (k.k->type != KEY_TYPE_snapshot)
1579 BCH_SNAPSHOT_DELETED(bkey_s_c_to_snapshot(k).v)
1580 ? snapshot_list_add(c, &deleted, k.k->p.offset)
1583 bch_err_msg(c, ret, "walking snapshots");
1587 for (id = 0; id < BTREE_ID_NR; id++) {
1588 struct bpos last_pos = POS_MIN;
1589 snapshot_id_list equiv_seen = { 0 };
1590 struct disk_reservation res = { 0 };
1592 if (!btree_type_has_snapshots(id))
1596 * deleted inodes btree is maintained by a trigger on the inodes
1597 * btree - no work for us to do here, and it's not safe to scan
1598 * it because we'll see out of date keys due to the btree write
1601 if (id == BTREE_ID_deleted_inodes)
1604 ret = for_each_btree_key_commit(trans, iter,
1606 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1607 &res, NULL, BCH_TRANS_COMMIT_no_enospc,
1608 snapshot_delete_key(trans, &iter, k, &deleted, &equiv_seen, &last_pos)) ?:
1609 for_each_btree_key_commit(trans, iter,
1611 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1612 &res, NULL, BCH_TRANS_COMMIT_no_enospc,
1613 move_key_to_correct_snapshot(trans, &iter, k));
1615 bch2_disk_reservation_put(c, &res);
1616 darray_exit(&equiv_seen);
1618 bch_err_msg(c, ret, "deleting keys from dying snapshots");
1623 bch2_trans_unlock(trans);
1624 down_write(&c->snapshot_create_lock);
1626 ret = for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1628 u32 snapshot = k.k->p.offset;
1629 u32 equiv = bch2_snapshot_equiv(c, snapshot);
1632 ? snapshot_list_add(c, &deleted_interior, snapshot)
1636 bch_err_msg(c, ret, "walking snapshots");
1638 goto err_create_lock;
1641 * Fixing children of deleted snapshots can't be done completely
1642 * atomically, if we crash between here and when we delete the interior
1643 * nodes some depth fields will be off:
1645 ret = for_each_btree_key_commit(trans, iter, BTREE_ID_snapshots, POS_MIN,
1646 BTREE_ITER_INTENT, k,
1647 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1648 bch2_fix_child_of_deleted_snapshot(trans, &iter, k, &deleted_interior));
1650 goto err_create_lock;
1652 darray_for_each(deleted, i) {
1653 ret = commit_do(trans, NULL, NULL, 0,
1654 bch2_snapshot_node_delete(trans, *i));
1655 bch_err_msg(c, ret, "deleting snapshot %u", *i);
1657 goto err_create_lock;
1660 darray_for_each(deleted_interior, i) {
1661 ret = commit_do(trans, NULL, NULL, 0,
1662 bch2_snapshot_node_delete(trans, *i));
1663 bch_err_msg(c, ret, "deleting snapshot %u", *i);
1665 goto err_create_lock;
1668 up_write(&c->snapshot_create_lock);
1670 darray_exit(&deleted_interior);
1671 darray_exit(&deleted);
1672 bch2_trans_put(trans);
1677 void bch2_delete_dead_snapshots_work(struct work_struct *work)
1679 struct bch_fs *c = container_of(work, struct bch_fs, snapshot_delete_work);
1681 bch2_delete_dead_snapshots(c);
1682 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1685 void bch2_delete_dead_snapshots_async(struct bch_fs *c)
1687 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_delete_dead_snapshots) &&
1688 !queue_work(c->write_ref_wq, &c->snapshot_delete_work))
1689 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1692 int __bch2_key_has_snapshot_overwrites(struct btree_trans *trans,
1696 struct bch_fs *c = trans->c;
1697 struct btree_iter iter;
1701 bch2_trans_iter_init(trans, &iter, id, pos,
1702 BTREE_ITER_NOT_EXTENTS|
1703 BTREE_ITER_ALL_SNAPSHOTS);
1705 k = bch2_btree_iter_prev(&iter);
1713 if (!bkey_eq(pos, k.k->p))
1716 if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, pos.snapshot)) {
1721 bch2_trans_iter_exit(trans, &iter);
1726 static u32 bch2_snapshot_smallest_child(struct bch_fs *c, u32 id)
1728 const struct snapshot_t *s = snapshot_t(c, id);
1730 return s->children[1] ?: s->children[0];
1733 static u32 bch2_snapshot_smallest_descendent(struct bch_fs *c, u32 id)
1737 while ((child = bch2_snapshot_smallest_child(c, id)))
1742 static int bch2_propagate_key_to_snapshot_leaf(struct btree_trans *trans,
1743 enum btree_id btree,
1744 struct bkey_s_c interior_k,
1745 u32 leaf_id, struct bpos *new_min_pos)
1747 struct btree_iter iter;
1748 struct bpos pos = interior_k.k->p;
1753 pos.snapshot = leaf_id;
1755 bch2_trans_iter_init(trans, &iter, btree, pos, BTREE_ITER_INTENT);
1756 k = bch2_btree_iter_peek_slot(&iter);
1761 /* key already overwritten in this snapshot? */
1762 if (k.k->p.snapshot != interior_k.k->p.snapshot)
1765 if (bpos_eq(*new_min_pos, POS_MIN)) {
1766 *new_min_pos = k.k->p;
1767 new_min_pos->snapshot = leaf_id;
1770 new = bch2_bkey_make_mut_noupdate(trans, interior_k);
1771 ret = PTR_ERR_OR_ZERO(new);
1775 new->k.p.snapshot = leaf_id;
1776 ret = bch2_trans_update(trans, &iter, new, 0);
1778 bch2_trans_iter_exit(trans, &iter);
1782 int bch2_propagate_key_to_snapshot_leaves(struct btree_trans *trans,
1783 enum btree_id btree,
1785 struct bpos *new_min_pos)
1787 struct bch_fs *c = trans->c;
1789 u32 restart_count = trans->restart_count;
1792 bch2_bkey_buf_init(&sk);
1793 bch2_bkey_buf_reassemble(&sk, c, k);
1794 k = bkey_i_to_s_c(sk.k);
1796 *new_min_pos = POS_MIN;
1798 for (u32 id = bch2_snapshot_smallest_descendent(c, k.k->p.snapshot);
1799 id < k.k->p.snapshot;
1801 if (!bch2_snapshot_is_ancestor(c, id, k.k->p.snapshot) ||
1802 !bch2_snapshot_is_leaf(c, id))
1805 ret = btree_trans_too_many_iters(trans) ?:
1806 bch2_propagate_key_to_snapshot_leaf(trans, btree, k, id, new_min_pos) ?:
1807 bch2_trans_commit(trans, NULL, NULL, 0);
1808 if (ret && bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
1809 bch2_trans_begin(trans);
1817 bch2_bkey_buf_exit(&sk, c);
1819 return ret ?: trans_was_restarted(trans, restart_count);
1822 static int bch2_check_snapshot_needs_deletion(struct btree_trans *trans, struct bkey_s_c k)
1824 struct bch_fs *c = trans->c;
1825 struct bkey_s_c_snapshot snap;
1828 if (k.k->type != KEY_TYPE_snapshot)
1831 snap = bkey_s_c_to_snapshot(k);
1832 if (BCH_SNAPSHOT_DELETED(snap.v) ||
1833 bch2_snapshot_equiv(c, k.k->p.offset) != k.k->p.offset ||
1834 (ret = bch2_snapshot_needs_delete(trans, k)) > 0) {
1835 set_bit(BCH_FS_need_delete_dead_snapshots, &c->flags);
1842 int bch2_snapshots_read(struct bch_fs *c)
1844 int ret = bch2_trans_run(c,
1845 for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1847 __bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0, bkey_s_c_null, k, 0) ?:
1848 bch2_snapshot_set_equiv(trans, k) ?:
1849 bch2_check_snapshot_needs_deletion(trans, k)) ?:
1850 for_each_btree_key(trans, iter, BTREE_ID_snapshots,
1852 (set_is_ancestor_bitmap(c, k.k->p.offset), 0)));
1856 * It's important that we check if we need to reconstruct snapshots
1857 * before going RW, so we mark that pass as required in the superblock -
1858 * otherwise, we could end up deleting keys with missing snapshot nodes
1861 BUG_ON(!test_bit(BCH_FS_new_fs, &c->flags) &&
1862 test_bit(BCH_FS_may_go_rw, &c->flags));
1864 if (bch2_err_matches(ret, EIO) ||
1865 (c->sb.btrees_lost_data & BIT_ULL(BTREE_ID_snapshots)))
1866 ret = bch2_run_explicit_recovery_pass_persistent(c, BCH_RECOVERY_PASS_reconstruct_snapshots);
1871 void bch2_fs_snapshots_exit(struct bch_fs *c)
1873 kvfree(rcu_dereference_protected(c->snapshots, true));