1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_key_cache.h"
5 #include "btree_update.h"
11 static int bch2_subvolume_delete(struct btree_trans *, u32);
15 void bch2_snapshot_tree_to_text(struct printbuf *out, struct bch_fs *c,
18 struct bkey_s_c_snapshot_tree t = bkey_s_c_to_snapshot_tree(k);
20 prt_printf(out, "subvol %u root snapshot %u",
21 le32_to_cpu(t.v->master_subvol),
22 le32_to_cpu(t.v->root_snapshot));
25 int bch2_snapshot_tree_invalid(const struct bch_fs *c, struct bkey_s_c k,
26 unsigned flags, struct printbuf *err)
28 if (bkey_gt(k.k->p, POS(0, U32_MAX)) ||
29 bkey_lt(k.k->p, POS(0, 1))) {
30 prt_printf(err, "bad pos");
31 return -BCH_ERR_invalid_bkey;
37 int bch2_snapshot_tree_lookup(struct btree_trans *trans, u32 id,
38 struct bch_snapshot_tree *s)
40 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_snapshot_trees, POS(0, id),
41 BTREE_ITER_WITH_UPDATES, snapshot_tree, s);
43 if (bch2_err_matches(ret, ENOENT))
44 ret = -BCH_ERR_ENOENT_snapshot_tree;
48 static struct bkey_i_snapshot_tree *
49 __snapshot_tree_create(struct btree_trans *trans)
51 struct btree_iter iter;
52 int ret = bch2_bkey_get_empty_slot(trans, &iter,
53 BTREE_ID_snapshot_trees, POS(0, U32_MAX));
54 struct bkey_i_snapshot_tree *s_t;
56 if (ret == -BCH_ERR_ENOSPC_btree_slot)
57 ret = -BCH_ERR_ENOSPC_snapshot_tree;
61 s_t = bch2_bkey_alloc(trans, &iter, 0, snapshot_tree);
62 ret = PTR_ERR_OR_ZERO(s_t);
63 bch2_trans_iter_exit(trans, &iter);
64 return ret ? ERR_PTR(ret) : s_t;
67 static int snapshot_tree_create(struct btree_trans *trans,
68 u32 root_id, u32 subvol_id, u32 *tree_id)
70 struct bkey_i_snapshot_tree *n_tree =
71 __snapshot_tree_create(trans);
74 return PTR_ERR(n_tree);
76 n_tree->v.master_subvol = cpu_to_le32(subvol_id);
77 n_tree->v.root_snapshot = cpu_to_le32(root_id);
78 *tree_id = n_tree->k.p.offset;
84 void bch2_snapshot_to_text(struct printbuf *out, struct bch_fs *c,
87 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(k);
89 prt_printf(out, "is_subvol %llu deleted %llu parent %10u children %10u %10u subvol %u",
90 BCH_SNAPSHOT_SUBVOL(s.v),
91 BCH_SNAPSHOT_DELETED(s.v),
92 le32_to_cpu(s.v->parent),
93 le32_to_cpu(s.v->children[0]),
94 le32_to_cpu(s.v->children[1]),
95 le32_to_cpu(s.v->subvol));
98 int bch2_snapshot_invalid(const struct bch_fs *c, struct bkey_s_c k,
99 unsigned flags, struct printbuf *err)
101 struct bkey_s_c_snapshot s;
104 if (bkey_gt(k.k->p, POS(0, U32_MAX)) ||
105 bkey_lt(k.k->p, POS(0, 1))) {
106 prt_printf(err, "bad pos");
107 return -BCH_ERR_invalid_bkey;
110 s = bkey_s_c_to_snapshot(k);
112 id = le32_to_cpu(s.v->parent);
113 if (id && id <= k.k->p.offset) {
114 prt_printf(err, "bad parent node (%u <= %llu)",
116 return -BCH_ERR_invalid_bkey;
119 if (le32_to_cpu(s.v->children[0]) < le32_to_cpu(s.v->children[1])) {
120 prt_printf(err, "children not normalized");
121 return -BCH_ERR_invalid_bkey;
124 if (s.v->children[0] &&
125 s.v->children[0] == s.v->children[1]) {
126 prt_printf(err, "duplicate child nodes");
127 return -BCH_ERR_invalid_bkey;
130 for (i = 0; i < 2; i++) {
131 id = le32_to_cpu(s.v->children[i]);
133 if (id >= k.k->p.offset) {
134 prt_printf(err, "bad child node (%u >= %llu)",
136 return -BCH_ERR_invalid_bkey;
143 int bch2_mark_snapshot(struct btree_trans *trans,
144 enum btree_id btree, unsigned level,
145 struct bkey_s_c old, struct bkey_s_c new,
148 struct bch_fs *c = trans->c;
149 struct snapshot_t *t;
151 t = genradix_ptr_alloc(&c->snapshots,
152 U32_MAX - new.k->p.offset,
155 return -BCH_ERR_ENOMEM_mark_snapshot;
157 if (new.k->type == KEY_TYPE_snapshot) {
158 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(new);
160 t->parent = le32_to_cpu(s.v->parent);
161 t->children[0] = le32_to_cpu(s.v->children[0]);
162 t->children[1] = le32_to_cpu(s.v->children[1]);
163 t->subvol = BCH_SNAPSHOT_SUBVOL(s.v) ? le32_to_cpu(s.v->subvol) : 0;
164 t->tree = le32_to_cpu(s.v->tree);
176 static int snapshot_lookup(struct btree_trans *trans, u32 id,
177 struct bch_snapshot *s)
179 return bch2_bkey_get_val_typed(trans, BTREE_ID_snapshots, POS(0, id),
180 BTREE_ITER_WITH_UPDATES, snapshot, s);
183 static int snapshot_live(struct btree_trans *trans, u32 id)
185 struct bch_snapshot v;
191 ret = snapshot_lookup(trans, id, &v);
192 if (bch2_err_matches(ret, ENOENT))
193 bch_err(trans->c, "snapshot node %u not found", id);
197 return !BCH_SNAPSHOT_DELETED(&v);
200 static int bch2_snapshot_set_equiv(struct btree_trans *trans, struct bkey_s_c k)
202 struct bch_fs *c = trans->c;
203 unsigned i, nr_live = 0, live_idx = 0;
204 struct bkey_s_c_snapshot snap;
205 u32 id = k.k->p.offset, child[2];
207 if (k.k->type != KEY_TYPE_snapshot)
210 snap = bkey_s_c_to_snapshot(k);
212 child[0] = le32_to_cpu(snap.v->children[0]);
213 child[1] = le32_to_cpu(snap.v->children[1]);
215 for (i = 0; i < 2; i++) {
216 int ret = snapshot_live(trans, child[i]);
226 snapshot_t(c, id)->equiv = nr_live == 1
227 ? snapshot_t(c, child[live_idx])->equiv
234 static u32 bch2_snapshot_child(struct bch_fs *c, u32 id, unsigned child)
236 return snapshot_t(c, id)->children[child];
239 static u32 bch2_snapshot_left_child(struct bch_fs *c, u32 id)
241 return bch2_snapshot_child(c, id, 0);
244 static u32 bch2_snapshot_right_child(struct bch_fs *c, u32 id)
246 return bch2_snapshot_child(c, id, 1);
249 static u32 bch2_snapshot_tree_next(struct bch_fs *c, u32 id)
253 n = bch2_snapshot_left_child(c, id);
257 while ((parent = bch2_snapshot_parent(c, id))) {
258 n = bch2_snapshot_right_child(c, parent);
267 static u32 bch2_snapshot_tree_oldest_subvol(struct bch_fs *c, u32 snapshot_root)
269 u32 id = snapshot_root;
273 s = snapshot_t(c, id)->subvol;
275 if (s && (!subvol || s < subvol))
278 id = bch2_snapshot_tree_next(c, id);
284 static int bch2_snapshot_tree_master_subvol(struct btree_trans *trans,
285 u32 snapshot_root, u32 *subvol_id)
287 struct bch_fs *c = trans->c;
288 struct btree_iter iter;
290 struct bkey_s_c_subvolume s;
294 for_each_btree_key_norestart(trans, iter, BTREE_ID_subvolumes, POS_MIN,
296 if (k.k->type != KEY_TYPE_subvolume)
299 s = bkey_s_c_to_subvolume(k);
300 if (!bch2_snapshot_is_ancestor(c, le32_to_cpu(s.v->snapshot), snapshot_root))
302 if (!BCH_SUBVOLUME_SNAP(s.v)) {
303 *subvol_id = s.k->p.offset;
309 bch2_trans_iter_exit(trans, &iter);
311 if (!ret && !found) {
312 struct bkey_i_subvolume *s;
314 *subvol_id = bch2_snapshot_tree_oldest_subvol(c, snapshot_root);
316 s = bch2_bkey_get_mut_typed(trans, &iter,
317 BTREE_ID_subvolumes, POS(0, *subvol_id),
319 ret = PTR_ERR_OR_ZERO(s);
323 SET_BCH_SUBVOLUME_SNAP(&s->v, false);
329 static int check_snapshot_tree(struct btree_trans *trans,
330 struct btree_iter *iter,
333 struct bch_fs *c = trans->c;
334 struct bkey_s_c_snapshot_tree st;
335 struct bch_snapshot s;
336 struct bch_subvolume subvol;
337 struct printbuf buf = PRINTBUF;
341 if (k.k->type != KEY_TYPE_snapshot_tree)
344 st = bkey_s_c_to_snapshot_tree(k);
345 root_id = le32_to_cpu(st.v->root_snapshot);
347 ret = snapshot_lookup(trans, root_id, &s);
348 if (ret && !bch2_err_matches(ret, ENOENT))
351 if (fsck_err_on(ret ||
352 root_id != bch2_snapshot_root(c, root_id) ||
353 st.k->p.offset != le32_to_cpu(s.tree),
355 "snapshot tree points to missing/incorrect snapshot:\n %s",
356 (bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
357 ret = bch2_btree_delete_at(trans, iter, 0);
361 ret = bch2_subvolume_get(trans, le32_to_cpu(st.v->master_subvol),
363 if (ret && !bch2_err_matches(ret, ENOENT))
366 if (fsck_err_on(ret, c,
367 "snapshot tree points to missing subvolume:\n %s",
368 (printbuf_reset(&buf),
369 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
370 fsck_err_on(!bch2_snapshot_is_ancestor(c,
371 le32_to_cpu(subvol.snapshot),
373 "snapshot tree points to subvolume that does not point to snapshot in this tree:\n %s",
374 (printbuf_reset(&buf),
375 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf)) ||
376 fsck_err_on(BCH_SUBVOLUME_SNAP(&subvol), c,
377 "snapshot tree points to snapshot subvolume:\n %s",
378 (printbuf_reset(&buf),
379 bch2_bkey_val_to_text(&buf, c, st.s_c), buf.buf))) {
380 struct bkey_i_snapshot_tree *u;
383 ret = bch2_snapshot_tree_master_subvol(trans, root_id, &subvol_id);
387 u = bch2_bkey_make_mut_typed(trans, iter, k, 0, snapshot_tree);
388 ret = PTR_ERR_OR_ZERO(u);
392 u->v.master_subvol = cpu_to_le32(subvol_id);
393 st = snapshot_tree_i_to_s_c(u);
402 * For each snapshot_tree, make sure it points to the root of a snapshot tree
403 * and that snapshot entry points back to it, or delete it.
405 * And, make sure it points to a subvolume within that snapshot tree, or correct
406 * it to point to the oldest subvolume within that snapshot tree.
408 int bch2_fs_check_snapshot_trees(struct bch_fs *c)
410 struct btree_iter iter;
414 ret = bch2_trans_run(c,
415 for_each_btree_key_commit(&trans, iter,
416 BTREE_ID_snapshot_trees, POS_MIN,
417 BTREE_ITER_PREFETCH, k,
418 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
419 check_snapshot_tree(&trans, &iter, k)));
422 bch_err(c, "error %i checking snapshot trees", ret);
427 * Look up snapshot tree for @tree_id and find root,
428 * make sure @snap_id is a descendent:
430 static int snapshot_tree_ptr_good(struct btree_trans *trans,
431 u32 snap_id, u32 tree_id)
433 struct bch_snapshot_tree s_t;
434 int ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
436 if (bch2_err_matches(ret, ENOENT))
441 return bch2_snapshot_is_ancestor(trans->c, snap_id, le32_to_cpu(s_t.root_snapshot));
445 * snapshot_tree pointer was incorrect: look up root snapshot node, make sure
446 * its snapshot_tree pointer is correct (allocate new one if necessary), then
447 * update this node's pointer to root node's pointer:
449 static int snapshot_tree_ptr_repair(struct btree_trans *trans,
450 struct btree_iter *iter,
451 struct bkey_s_c_snapshot *s)
453 struct bch_fs *c = trans->c;
454 struct btree_iter root_iter;
455 struct bch_snapshot_tree s_t;
456 struct bkey_s_c_snapshot root;
457 struct bkey_i_snapshot *u;
458 u32 root_id = bch2_snapshot_root(c, s->k->p.offset), tree_id;
461 root = bch2_bkey_get_iter_typed(trans, &root_iter,
462 BTREE_ID_snapshots, POS(0, root_id),
463 BTREE_ITER_WITH_UPDATES, snapshot);
464 ret = bkey_err(root);
468 tree_id = le32_to_cpu(root.v->tree);
470 ret = bch2_snapshot_tree_lookup(trans, tree_id, &s_t);
471 if (ret && !bch2_err_matches(ret, ENOENT))
474 if (ret || le32_to_cpu(s_t.root_snapshot) != root_id) {
475 u = bch2_bkey_make_mut_typed(trans, &root_iter, root.s_c, 0, snapshot);
476 ret = PTR_ERR_OR_ZERO(u) ?:
477 snapshot_tree_create(trans, root_id,
478 bch2_snapshot_tree_oldest_subvol(c, root_id),
483 u->v.tree = cpu_to_le32(tree_id);
484 if (s->k->p.snapshot == root_id)
485 *s = snapshot_i_to_s_c(u);
488 if (s->k->p.snapshot != root_id) {
489 u = bch2_bkey_make_mut_typed(trans, iter, s->s_c, 0, snapshot);
490 ret = PTR_ERR_OR_ZERO(u);
494 u->v.tree = cpu_to_le32(tree_id);
495 *s = snapshot_i_to_s_c(u);
498 bch2_trans_iter_exit(trans, &root_iter);
502 static int check_snapshot(struct btree_trans *trans,
503 struct btree_iter *iter,
506 struct bch_fs *c = trans->c;
507 struct bkey_s_c_snapshot s;
508 struct bch_subvolume subvol;
509 struct bch_snapshot v;
510 struct printbuf buf = PRINTBUF;
511 bool should_have_subvol;
515 if (k.k->type != KEY_TYPE_snapshot)
518 s = bkey_s_c_to_snapshot(k);
519 id = le32_to_cpu(s.v->parent);
521 ret = snapshot_lookup(trans, id, &v);
522 if (bch2_err_matches(ret, ENOENT))
523 bch_err(c, "snapshot with nonexistent parent:\n %s",
524 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf));
528 if (le32_to_cpu(v.children[0]) != s.k->p.offset &&
529 le32_to_cpu(v.children[1]) != s.k->p.offset) {
530 bch_err(c, "snapshot parent %u missing pointer to child %llu",
537 for (i = 0; i < 2 && s.v->children[i]; i++) {
538 id = le32_to_cpu(s.v->children[i]);
540 ret = snapshot_lookup(trans, id, &v);
541 if (bch2_err_matches(ret, ENOENT))
542 bch_err(c, "snapshot node %llu has nonexistent child %u",
547 if (le32_to_cpu(v.parent) != s.k->p.offset) {
548 bch_err(c, "snapshot child %u has wrong parent (got %u should be %llu)",
549 id, le32_to_cpu(v.parent), s.k->p.offset);
555 should_have_subvol = BCH_SNAPSHOT_SUBVOL(s.v) &&
556 !BCH_SNAPSHOT_DELETED(s.v);
558 if (should_have_subvol) {
559 id = le32_to_cpu(s.v->subvol);
560 ret = bch2_subvolume_get(trans, id, 0, false, &subvol);
561 if (bch2_err_matches(ret, ENOENT))
562 bch_err(c, "snapshot points to nonexistent subvolume:\n %s",
563 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf));
567 if (BCH_SNAPSHOT_SUBVOL(s.v) != (le32_to_cpu(subvol.snapshot) == s.k->p.offset)) {
568 bch_err(c, "snapshot node %llu has wrong BCH_SNAPSHOT_SUBVOL",
574 if (fsck_err_on(s.v->subvol, c, "snapshot should not point to subvol:\n %s",
575 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
576 struct bkey_i_snapshot *u = bch2_trans_kmalloc(trans, sizeof(*u));
578 ret = PTR_ERR_OR_ZERO(u);
582 bkey_reassemble(&u->k_i, s.s_c);
584 ret = bch2_trans_update(trans, iter, &u->k_i, 0);
588 s = snapshot_i_to_s_c(u);
592 ret = snapshot_tree_ptr_good(trans, s.k->p.offset, le32_to_cpu(s.v->tree));
596 if (fsck_err_on(!ret, c, "snapshot points to missing/incorrect tree:\n %s",
597 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
598 ret = snapshot_tree_ptr_repair(trans, iter, &s);
604 if (BCH_SNAPSHOT_DELETED(s.v))
605 set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
612 int bch2_fs_check_snapshots(struct bch_fs *c)
614 struct btree_iter iter;
618 ret = bch2_trans_run(c,
619 for_each_btree_key_commit(&trans, iter,
620 BTREE_ID_snapshots, POS_MIN,
621 BTREE_ITER_PREFETCH, k,
622 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
623 check_snapshot(&trans, &iter, k)));
625 bch_err(c, "%s: error %s", __func__, bch2_err_str(ret));
629 static int check_subvol(struct btree_trans *trans,
630 struct btree_iter *iter,
633 struct bch_fs *c = trans->c;
634 struct bkey_s_c_subvolume subvol;
635 struct bch_snapshot snapshot;
639 if (k.k->type != KEY_TYPE_subvolume)
642 subvol = bkey_s_c_to_subvolume(k);
643 snapid = le32_to_cpu(subvol.v->snapshot);
644 ret = snapshot_lookup(trans, snapid, &snapshot);
646 if (bch2_err_matches(ret, ENOENT))
647 bch_err(c, "subvolume %llu points to nonexistent snapshot %u",
648 k.k->p.offset, snapid);
652 if (BCH_SUBVOLUME_UNLINKED(subvol.v)) {
655 ret = bch2_subvolume_delete(trans, iter->pos.offset);
657 bch_err(c, "error deleting subvolume %llu: %s",
658 iter->pos.offset, bch2_err_str(ret));
659 return ret ?: -BCH_ERR_transaction_restart_nested;
662 if (!BCH_SUBVOLUME_SNAP(subvol.v)) {
663 u32 snapshot_root = bch2_snapshot_root(c, le32_to_cpu(subvol.v->snapshot));
664 u32 snapshot_tree = snapshot_t(c, snapshot_root)->tree;
665 struct bch_snapshot_tree st;
667 ret = bch2_snapshot_tree_lookup(trans, snapshot_tree, &st);
669 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
670 "%s: snapshot tree %u not found", __func__, snapshot_tree);
675 if (fsck_err_on(le32_to_cpu(st.master_subvol) != subvol.k->p.offset, c,
676 "subvolume %llu is not set as snapshot but is not master subvolume",
678 struct bkey_i_subvolume *s =
679 bch2_bkey_make_mut_typed(trans, iter, subvol.s_c, 0, subvolume);
680 ret = PTR_ERR_OR_ZERO(s);
684 SET_BCH_SUBVOLUME_SNAP(&s->v, true);
692 int bch2_fs_check_subvols(struct bch_fs *c)
694 struct btree_iter iter;
698 ret = bch2_trans_run(c,
699 for_each_btree_key_commit(&trans, iter,
700 BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k,
701 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
702 check_subvol(&trans, &iter, k)));
704 bch_err(c, "%s: error %s", __func__, bch2_err_str(ret));
709 void bch2_fs_snapshots_exit(struct bch_fs *c)
711 genradix_free(&c->snapshots);
714 int bch2_fs_snapshots_start(struct bch_fs *c)
716 struct btree_iter iter;
720 ret = bch2_trans_run(c,
721 for_each_btree_key2(&trans, iter, BTREE_ID_snapshots,
723 bch2_mark_snapshot(&trans, BTREE_ID_snapshots, 0, bkey_s_c_null, k, 0) ?:
724 bch2_snapshot_set_equiv(&trans, k)));
726 bch_err(c, "error starting snapshots: %s", bch2_err_str(ret));
731 * Mark a snapshot as deleted, for future cleanup:
733 static int bch2_snapshot_node_set_deleted(struct btree_trans *trans, u32 id)
735 struct btree_iter iter;
736 struct bkey_i_snapshot *s;
739 s = bch2_bkey_get_mut_typed(trans, &iter,
740 BTREE_ID_snapshots, POS(0, id),
742 ret = PTR_ERR_OR_ZERO(s);
744 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT),
745 trans->c, "missing snapshot %u", id);
749 /* already deleted? */
750 if (BCH_SNAPSHOT_DELETED(&s->v))
753 SET_BCH_SNAPSHOT_DELETED(&s->v, true);
754 SET_BCH_SNAPSHOT_SUBVOL(&s->v, false);
757 bch2_trans_iter_exit(trans, &iter);
761 static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
763 struct bch_fs *c = trans->c;
764 struct btree_iter iter, p_iter = (struct btree_iter) { NULL };
765 struct btree_iter tree_iter = (struct btree_iter) { NULL };
766 struct bkey_s_c_snapshot s;
771 s = bch2_bkey_get_iter_typed(trans, &iter, BTREE_ID_snapshots, POS(0, id),
772 BTREE_ITER_INTENT, snapshot);
774 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
775 "missing snapshot %u", id);
780 BUG_ON(!BCH_SNAPSHOT_DELETED(s.v));
781 parent_id = le32_to_cpu(s.v->parent);
784 struct bkey_i_snapshot *parent;
786 parent = bch2_bkey_get_mut_typed(trans, &p_iter,
787 BTREE_ID_snapshots, POS(0, parent_id),
789 ret = PTR_ERR_OR_ZERO(parent);
791 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
792 "missing snapshot %u", parent_id);
796 for (i = 0; i < 2; i++)
797 if (le32_to_cpu(parent->v.children[i]) == id)
801 bch_err(c, "snapshot %u missing child pointer to %u",
804 parent->v.children[i] = 0;
806 if (le32_to_cpu(parent->v.children[0]) <
807 le32_to_cpu(parent->v.children[1]))
808 swap(parent->v.children[0],
809 parent->v.children[1]);
812 * We're deleting the root of a snapshot tree: update the
813 * snapshot_tree entry to point to the new root, or delete it if
814 * this is the last snapshot ID in this tree:
816 struct bkey_i_snapshot_tree *s_t;
818 BUG_ON(s.v->children[1]);
820 s_t = bch2_bkey_get_mut_typed(trans, &tree_iter,
821 BTREE_ID_snapshot_trees, POS(0, le32_to_cpu(s.v->tree)),
823 ret = PTR_ERR_OR_ZERO(s_t);
827 if (s.v->children[0]) {
828 s_t->v.root_snapshot = cpu_to_le32(s.v->children[0]);
830 s_t->k.type = KEY_TYPE_deleted;
831 set_bkey_val_u64s(&s_t->k, 0);
835 ret = bch2_btree_delete_at(trans, &iter, 0);
837 bch2_trans_iter_exit(trans, &tree_iter);
838 bch2_trans_iter_exit(trans, &p_iter);
839 bch2_trans_iter_exit(trans, &iter);
843 static int create_snapids(struct btree_trans *trans, u32 parent, u32 tree,
845 u32 *snapshot_subvols,
848 struct btree_iter iter;
849 struct bkey_i_snapshot *n;
854 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
855 POS_MIN, BTREE_ITER_INTENT);
856 k = bch2_btree_iter_peek(&iter);
861 for (i = 0; i < nr_snapids; i++) {
862 k = bch2_btree_iter_prev_slot(&iter);
867 if (!k.k || !k.k->p.offset) {
868 ret = -BCH_ERR_ENOSPC_snapshot_create;
872 n = bch2_bkey_alloc(trans, &iter, 0, snapshot);
873 ret = PTR_ERR_OR_ZERO(n);
878 n->v.parent = cpu_to_le32(parent);
879 n->v.subvol = cpu_to_le32(snapshot_subvols[i]);
880 n->v.tree = cpu_to_le32(tree);
881 SET_BCH_SNAPSHOT_SUBVOL(&n->v, true);
883 ret = bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0,
884 bkey_s_c_null, bkey_i_to_s_c(&n->k_i), 0);
888 new_snapids[i] = iter.pos.offset;
891 bch2_trans_iter_exit(trans, &iter);
896 * Create new snapshot IDs as children of an existing snapshot ID:
898 static int bch2_snapshot_node_create_children(struct btree_trans *trans, u32 parent,
900 u32 *snapshot_subvols,
903 struct btree_iter iter;
904 struct bkey_i_snapshot *n_parent;
907 n_parent = bch2_bkey_get_mut_typed(trans, &iter,
908 BTREE_ID_snapshots, POS(0, parent),
910 ret = PTR_ERR_OR_ZERO(n_parent);
912 if (bch2_err_matches(ret, ENOENT))
913 bch_err(trans->c, "snapshot %u not found", parent);
917 if (n_parent->v.children[0] || n_parent->v.children[1]) {
918 bch_err(trans->c, "Trying to add child snapshot nodes to parent that already has children");
923 ret = create_snapids(trans, parent, le32_to_cpu(n_parent->v.tree),
924 new_snapids, snapshot_subvols, nr_snapids);
928 n_parent->v.children[0] = cpu_to_le32(new_snapids[0]);
929 n_parent->v.children[1] = cpu_to_le32(new_snapids[1]);
930 n_parent->v.subvol = 0;
931 SET_BCH_SNAPSHOT_SUBVOL(&n_parent->v, false);
933 bch2_trans_iter_exit(trans, &iter);
938 * Create a snapshot node that is the root of a new tree:
940 static int bch2_snapshot_node_create_tree(struct btree_trans *trans,
942 u32 *snapshot_subvols,
945 struct bkey_i_snapshot_tree *n_tree;
948 n_tree = __snapshot_tree_create(trans);
949 ret = PTR_ERR_OR_ZERO(n_tree) ?:
950 create_snapids(trans, 0, n_tree->k.p.offset,
951 new_snapids, snapshot_subvols, nr_snapids);
955 n_tree->v.master_subvol = cpu_to_le32(snapshot_subvols[0]);
956 n_tree->v.root_snapshot = cpu_to_le32(new_snapids[0]);
960 int bch2_snapshot_node_create(struct btree_trans *trans, u32 parent,
962 u32 *snapshot_subvols,
965 BUG_ON((parent == 0) != (nr_snapids == 1));
966 BUG_ON((parent != 0) != (nr_snapids == 2));
969 ? bch2_snapshot_node_create_children(trans, parent,
970 new_snapids, snapshot_subvols, nr_snapids)
971 : bch2_snapshot_node_create_tree(trans,
972 new_snapids, snapshot_subvols, nr_snapids);
976 static int snapshot_delete_key(struct btree_trans *trans,
977 struct btree_iter *iter,
979 snapshot_id_list *deleted,
980 snapshot_id_list *equiv_seen,
981 struct bpos *last_pos)
983 struct bch_fs *c = trans->c;
984 u32 equiv = snapshot_t(c, k.k->p.snapshot)->equiv;
986 if (!bkey_eq(k.k->p, *last_pos))
990 if (snapshot_list_has_id(deleted, k.k->p.snapshot) ||
991 snapshot_list_has_id(equiv_seen, equiv)) {
992 return bch2_btree_delete_at(trans, iter,
993 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
995 return snapshot_list_add(c, equiv_seen, equiv);
999 static int bch2_delete_redundant_snapshot(struct btree_trans *trans, struct btree_iter *iter,
1002 struct bkey_s_c_snapshot snap;
1006 if (k.k->type != KEY_TYPE_snapshot)
1009 snap = bkey_s_c_to_snapshot(k);
1010 if (BCH_SNAPSHOT_DELETED(snap.v) ||
1011 BCH_SNAPSHOT_SUBVOL(snap.v))
1014 children[0] = le32_to_cpu(snap.v->children[0]);
1015 children[1] = le32_to_cpu(snap.v->children[1]);
1017 ret = snapshot_live(trans, children[0]) ?:
1018 snapshot_live(trans, children[1]);
1023 return bch2_snapshot_node_set_deleted(trans, k.k->p.offset);
1027 int bch2_delete_dead_snapshots(struct bch_fs *c)
1029 struct btree_trans trans;
1030 struct btree_iter iter;
1032 struct bkey_s_c_snapshot snap;
1033 snapshot_id_list deleted = { 0 };
1037 if (!test_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags))
1040 if (!test_bit(BCH_FS_STARTED, &c->flags)) {
1041 ret = bch2_fs_read_write_early(c);
1043 bch_err(c, "error deleleting dead snapshots: error going rw: %s", bch2_err_str(ret));
1048 bch2_trans_init(&trans, c, 0, 0);
1051 * For every snapshot node: If we have no live children and it's not
1052 * pointed to by a subvolume, delete it:
1054 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_snapshots,
1057 bch2_delete_redundant_snapshot(&trans, &iter, k));
1059 bch_err(c, "error deleting redundant snapshots: %s", bch2_err_str(ret));
1063 for_each_btree_key2(&trans, iter, BTREE_ID_snapshots,
1065 bch2_snapshot_set_equiv(&trans, k));
1067 bch_err(c, "error in bch2_snapshots_set_equiv: %s", bch2_err_str(ret));
1071 for_each_btree_key(&trans, iter, BTREE_ID_snapshots,
1072 POS_MIN, 0, k, ret) {
1073 if (k.k->type != KEY_TYPE_snapshot)
1076 snap = bkey_s_c_to_snapshot(k);
1077 if (BCH_SNAPSHOT_DELETED(snap.v)) {
1078 ret = snapshot_list_add(c, &deleted, k.k->p.offset);
1083 bch2_trans_iter_exit(&trans, &iter);
1086 bch_err(c, "error walking snapshots: %s", bch2_err_str(ret));
1090 for (id = 0; id < BTREE_ID_NR; id++) {
1091 struct bpos last_pos = POS_MIN;
1092 snapshot_id_list equiv_seen = { 0 };
1094 if (!btree_type_has_snapshots(id))
1097 ret = for_each_btree_key_commit(&trans, iter,
1099 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
1100 NULL, NULL, BTREE_INSERT_NOFAIL,
1101 snapshot_delete_key(&trans, &iter, k, &deleted, &equiv_seen, &last_pos));
1103 darray_exit(&equiv_seen);
1106 bch_err(c, "error deleting snapshot keys: %s", bch2_err_str(ret));
1111 for (i = 0; i < deleted.nr; i++) {
1112 ret = commit_do(&trans, NULL, NULL, 0,
1113 bch2_snapshot_node_delete(&trans, deleted.data[i]));
1115 bch_err(c, "error deleting snapshot %u: %s",
1116 deleted.data[i], bch2_err_str(ret));
1121 clear_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
1123 darray_exit(&deleted);
1124 bch2_trans_exit(&trans);
1128 static void bch2_delete_dead_snapshots_work(struct work_struct *work)
1130 struct bch_fs *c = container_of(work, struct bch_fs, snapshot_delete_work);
1132 bch2_delete_dead_snapshots(c);
1133 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1136 void bch2_delete_dead_snapshots_async(struct bch_fs *c)
1138 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_delete_dead_snapshots) &&
1139 !queue_work(c->write_ref_wq, &c->snapshot_delete_work))
1140 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
1143 static int bch2_delete_dead_snapshots_hook(struct btree_trans *trans,
1144 struct btree_trans_commit_hook *h)
1146 struct bch_fs *c = trans->c;
1148 set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
1150 if (!test_bit(BCH_FS_FSCK_DONE, &c->flags))
1153 bch2_delete_dead_snapshots_async(c);
1159 int bch2_subvolume_invalid(const struct bch_fs *c, struct bkey_s_c k,
1160 unsigned flags, struct printbuf *err)
1162 if (bkey_lt(k.k->p, SUBVOL_POS_MIN) ||
1163 bkey_gt(k.k->p, SUBVOL_POS_MAX)) {
1164 prt_printf(err, "invalid pos");
1165 return -BCH_ERR_invalid_bkey;
1171 void bch2_subvolume_to_text(struct printbuf *out, struct bch_fs *c,
1174 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
1176 prt_printf(out, "root %llu snapshot id %u",
1177 le64_to_cpu(s.v->inode),
1178 le32_to_cpu(s.v->snapshot));
1180 if (bkey_val_bytes(s.k) > offsetof(struct bch_subvolume, parent))
1181 prt_printf(out, " parent %u", le32_to_cpu(s.v->parent));
1184 static __always_inline int
1185 bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol,
1186 bool inconsistent_if_not_found,
1188 struct bch_subvolume *s)
1190 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, subvol),
1191 iter_flags, subvolume, s);
1192 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT) &&
1193 inconsistent_if_not_found,
1194 trans->c, "missing subvolume %u", subvol);
1198 int bch2_subvolume_get(struct btree_trans *trans, unsigned subvol,
1199 bool inconsistent_if_not_found,
1201 struct bch_subvolume *s)
1203 return bch2_subvolume_get_inlined(trans, subvol, inconsistent_if_not_found, iter_flags, s);
1206 int bch2_snapshot_get_subvol(struct btree_trans *trans, u32 snapshot,
1207 struct bch_subvolume *subvol)
1209 struct bch_snapshot snap;
1211 return snapshot_lookup(trans, snapshot, &snap) ?:
1212 bch2_subvolume_get(trans, le32_to_cpu(snap.subvol), true, 0, subvol);
1215 int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvol,
1218 struct btree_iter iter;
1222 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_subvolumes, POS(0, subvol),
1224 BTREE_ITER_WITH_UPDATES);
1225 ret = bkey_err(k) ?: k.k->type == KEY_TYPE_subvolume ? 0 : -BCH_ERR_ENOENT_subvolume;
1228 *snapid = le32_to_cpu(bkey_s_c_to_subvolume(k).v->snapshot);
1229 else if (bch2_err_matches(ret, ENOENT))
1230 bch2_fs_inconsistent(trans->c, "missing subvolume %u", subvol);
1231 bch2_trans_iter_exit(trans, &iter);
1235 static int bch2_subvolume_reparent(struct btree_trans *trans,
1236 struct btree_iter *iter,
1238 u32 old_parent, u32 new_parent)
1240 struct bkey_i_subvolume *s;
1243 if (k.k->type != KEY_TYPE_subvolume)
1246 if (bkey_val_bytes(k.k) > offsetof(struct bch_subvolume, parent) &&
1247 le32_to_cpu(bkey_s_c_to_subvolume(k).v->parent) != old_parent)
1250 s = bch2_bkey_make_mut_typed(trans, iter, k, 0, subvolume);
1251 ret = PTR_ERR_OR_ZERO(s);
1255 s->v.parent = cpu_to_le32(new_parent);
1260 * Scan for subvolumes with parent @subvolid_to_delete, reparent:
1262 static int bch2_subvolumes_reparent(struct btree_trans *trans, u32 subvolid_to_delete)
1264 struct btree_iter iter;
1266 struct bch_subvolume s;
1268 return lockrestart_do(trans,
1269 bch2_subvolume_get(trans, subvolid_to_delete, true,
1270 BTREE_ITER_CACHED, &s)) ?:
1271 for_each_btree_key_commit(trans, iter,
1272 BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k,
1273 NULL, NULL, BTREE_INSERT_NOFAIL,
1274 bch2_subvolume_reparent(trans, &iter, k,
1275 subvolid_to_delete, le32_to_cpu(s.parent)));
1279 * Delete subvolume, mark snapshot ID as deleted, queue up snapshot
1282 static int __bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
1284 struct btree_iter iter;
1285 struct bkey_s_c_subvolume subvol;
1286 struct btree_trans_commit_hook *h;
1290 subvol = bch2_bkey_get_iter_typed(trans, &iter,
1291 BTREE_ID_subvolumes, POS(0, subvolid),
1292 BTREE_ITER_CACHED|BTREE_ITER_INTENT,
1294 ret = bkey_err(subvol);
1295 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
1296 "missing subvolume %u", subvolid);
1300 snapid = le32_to_cpu(subvol.v->snapshot);
1302 ret = bch2_btree_delete_at(trans, &iter, 0);
1306 ret = bch2_snapshot_node_set_deleted(trans, snapid);
1310 h = bch2_trans_kmalloc(trans, sizeof(*h));
1311 ret = PTR_ERR_OR_ZERO(h);
1315 h->fn = bch2_delete_dead_snapshots_hook;
1316 bch2_trans_commit_hook(trans, h);
1318 bch2_trans_iter_exit(trans, &iter);
1322 static int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
1324 return bch2_subvolumes_reparent(trans, subvolid) ?:
1325 commit_do(trans, NULL, NULL, BTREE_INSERT_NOFAIL,
1326 __bch2_subvolume_delete(trans, subvolid));
1329 void bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct *work)
1331 struct bch_fs *c = container_of(work, struct bch_fs,
1332 snapshot_wait_for_pagecache_and_delete_work);
1338 mutex_lock(&c->snapshots_unlinked_lock);
1339 s = c->snapshots_unlinked;
1340 darray_init(&c->snapshots_unlinked);
1341 mutex_unlock(&c->snapshots_unlinked_lock);
1346 bch2_evict_subvolume_inodes(c, &s);
1348 for (id = s.data; id < s.data + s.nr; id++) {
1349 ret = bch2_trans_run(c, bch2_subvolume_delete(&trans, *id));
1351 bch_err(c, "error deleting subvolume %u: %s", *id, bch2_err_str(ret));
1359 bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
1362 struct subvolume_unlink_hook {
1363 struct btree_trans_commit_hook h;
1367 int bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans *trans,
1368 struct btree_trans_commit_hook *_h)
1370 struct subvolume_unlink_hook *h = container_of(_h, struct subvolume_unlink_hook, h);
1371 struct bch_fs *c = trans->c;
1374 mutex_lock(&c->snapshots_unlinked_lock);
1375 if (!snapshot_list_has_id(&c->snapshots_unlinked, h->subvol))
1376 ret = snapshot_list_add(c, &c->snapshots_unlinked, h->subvol);
1377 mutex_unlock(&c->snapshots_unlinked_lock);
1382 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_snapshot_delete_pagecache))
1385 if (!queue_work(c->write_ref_wq, &c->snapshot_wait_for_pagecache_and_delete_work))
1386 bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
1390 int bch2_subvolume_unlink(struct btree_trans *trans, u32 subvolid)
1392 struct btree_iter iter;
1393 struct bkey_i_subvolume *n;
1394 struct subvolume_unlink_hook *h;
1397 h = bch2_trans_kmalloc(trans, sizeof(*h));
1398 ret = PTR_ERR_OR_ZERO(h);
1402 h->h.fn = bch2_subvolume_wait_for_pagecache_and_delete_hook;
1403 h->subvol = subvolid;
1404 bch2_trans_commit_hook(trans, &h->h);
1406 n = bch2_bkey_get_mut_typed(trans, &iter,
1407 BTREE_ID_subvolumes, POS(0, subvolid),
1408 BTREE_ITER_CACHED, subvolume);
1409 ret = PTR_ERR_OR_ZERO(n);
1410 if (unlikely(ret)) {
1411 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
1412 "missing subvolume %u", subvolid);
1416 SET_BCH_SUBVOLUME_UNLINKED(&n->v, true);
1417 bch2_trans_iter_exit(trans, &iter);
1421 int bch2_subvolume_create(struct btree_trans *trans, u64 inode,
1424 u32 *new_snapshotid,
1427 struct bch_fs *c = trans->c;
1428 struct btree_iter dst_iter, src_iter = (struct btree_iter) { NULL };
1429 struct bkey_i_subvolume *new_subvol = NULL;
1430 struct bkey_i_subvolume *src_subvol = NULL;
1431 u32 parent = 0, new_nodes[2], snapshot_subvols[2];
1434 ret = bch2_bkey_get_empty_slot(trans, &dst_iter,
1435 BTREE_ID_subvolumes, POS(0, U32_MAX));
1436 if (ret == -BCH_ERR_ENOSPC_btree_slot)
1437 ret = -BCH_ERR_ENOSPC_subvolume_create;
1441 snapshot_subvols[0] = dst_iter.pos.offset;
1442 snapshot_subvols[1] = src_subvolid;
1445 /* Creating a snapshot: */
1447 src_subvol = bch2_bkey_get_mut_typed(trans, &src_iter,
1448 BTREE_ID_subvolumes, POS(0, src_subvolid),
1449 BTREE_ITER_CACHED, subvolume);
1450 ret = PTR_ERR_OR_ZERO(src_subvol);
1451 if (unlikely(ret)) {
1452 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
1453 "subvolume %u not found", src_subvolid);
1457 parent = le32_to_cpu(src_subvol->v.snapshot);
1460 ret = bch2_snapshot_node_create(trans, parent, new_nodes,
1462 src_subvolid ? 2 : 1);
1467 src_subvol->v.snapshot = cpu_to_le32(new_nodes[1]);
1468 ret = bch2_trans_update(trans, &src_iter, &src_subvol->k_i, 0);
1473 new_subvol = bch2_bkey_alloc(trans, &dst_iter, 0, subvolume);
1474 ret = PTR_ERR_OR_ZERO(new_subvol);
1478 new_subvol->v.flags = 0;
1479 new_subvol->v.snapshot = cpu_to_le32(new_nodes[0]);
1480 new_subvol->v.inode = cpu_to_le64(inode);
1481 new_subvol->v.parent = cpu_to_le32(src_subvolid);
1482 new_subvol->v.otime.lo = cpu_to_le64(bch2_current_time(c));
1483 new_subvol->v.otime.hi = 0;
1485 SET_BCH_SUBVOLUME_RO(&new_subvol->v, ro);
1486 SET_BCH_SUBVOLUME_SNAP(&new_subvol->v, src_subvolid != 0);
1488 *new_subvolid = new_subvol->k.p.offset;
1489 *new_snapshotid = new_nodes[0];
1491 bch2_trans_iter_exit(trans, &src_iter);
1492 bch2_trans_iter_exit(trans, &dst_iter);
1496 int bch2_fs_subvolumes_init(struct bch_fs *c)
1498 INIT_WORK(&c->snapshot_delete_work, bch2_delete_dead_snapshots_work);
1499 INIT_WORK(&c->snapshot_wait_for_pagecache_and_delete_work,
1500 bch2_subvolume_wait_for_pagecache_and_delete);
1501 mutex_init(&c->snapshots_unlinked_lock);