1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_key_cache.h"
5 #include "btree_update.h"
13 void bch2_snapshot_to_text(struct printbuf *out, struct bch_fs *c,
16 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(k);
18 prt_printf(out, "is_subvol %llu deleted %llu parent %10u children %10u %10u subvol %u",
19 BCH_SNAPSHOT_SUBVOL(s.v),
20 BCH_SNAPSHOT_DELETED(s.v),
21 le32_to_cpu(s.v->parent),
22 le32_to_cpu(s.v->children[0]),
23 le32_to_cpu(s.v->children[1]),
24 le32_to_cpu(s.v->subvol));
27 int bch2_snapshot_invalid(const struct bch_fs *c, struct bkey_s_c k,
28 unsigned flags, struct printbuf *err)
30 struct bkey_s_c_snapshot s;
33 if (bkey_gt(k.k->p, POS(0, U32_MAX)) ||
34 bkey_lt(k.k->p, POS(0, 1))) {
35 prt_printf(err, "bad pos");
36 return -BCH_ERR_invalid_bkey;
39 if (bkey_val_bytes(k.k) != sizeof(struct bch_snapshot)) {
40 prt_printf(err, "bad val size (%zu != %zu)",
41 bkey_val_bytes(k.k), sizeof(struct bch_snapshot));
42 return -BCH_ERR_invalid_bkey;
45 s = bkey_s_c_to_snapshot(k);
47 id = le32_to_cpu(s.v->parent);
48 if (id && id <= k.k->p.offset) {
49 prt_printf(err, "bad parent node (%u <= %llu)",
51 return -BCH_ERR_invalid_bkey;
54 if (le32_to_cpu(s.v->children[0]) < le32_to_cpu(s.v->children[1])) {
55 prt_printf(err, "children not normalized");
56 return -BCH_ERR_invalid_bkey;
59 if (s.v->children[0] &&
60 s.v->children[0] == s.v->children[1]) {
61 prt_printf(err, "duplicate child nodes");
62 return -BCH_ERR_invalid_bkey;
65 for (i = 0; i < 2; i++) {
66 id = le32_to_cpu(s.v->children[i]);
68 if (id >= k.k->p.offset) {
69 prt_printf(err, "bad child node (%u >= %llu)",
71 return -BCH_ERR_invalid_bkey;
78 int bch2_mark_snapshot(struct btree_trans *trans,
79 enum btree_id btree, unsigned level,
80 struct bkey_s_c old, struct bkey_s_c new,
83 struct bch_fs *c = trans->c;
86 t = genradix_ptr_alloc(&c->snapshots,
87 U32_MAX - new.k->p.offset,
92 if (new.k->type == KEY_TYPE_snapshot) {
93 struct bkey_s_c_snapshot s = bkey_s_c_to_snapshot(new);
95 t->parent = le32_to_cpu(s.v->parent);
96 t->children[0] = le32_to_cpu(s.v->children[0]);
97 t->children[1] = le32_to_cpu(s.v->children[1]);
98 t->subvol = BCH_SNAPSHOT_SUBVOL(s.v) ? le32_to_cpu(s.v->subvol) : 0;
109 static int snapshot_lookup(struct btree_trans *trans, u32 id,
110 struct bch_snapshot *s)
112 struct btree_iter iter;
116 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots, POS(0, id),
117 BTREE_ITER_WITH_UPDATES);
118 k = bch2_btree_iter_peek_slot(&iter);
119 ret = bkey_err(k) ?: k.k->type == KEY_TYPE_snapshot ? 0 : -ENOENT;
122 *s = *bkey_s_c_to_snapshot(k).v;
124 bch2_trans_iter_exit(trans, &iter);
128 static int snapshot_live(struct btree_trans *trans, u32 id)
130 struct bch_snapshot v;
136 ret = snapshot_lookup(trans, id, &v);
138 bch_err(trans->c, "snapshot node %u not found", id);
142 return !BCH_SNAPSHOT_DELETED(&v);
145 static int bch2_snapshot_set_equiv(struct btree_trans *trans, struct bkey_s_c k)
147 struct bch_fs *c = trans->c;
148 unsigned i, nr_live = 0, live_idx = 0;
149 struct bkey_s_c_snapshot snap;
150 u32 id = k.k->p.offset, child[2];
152 if (k.k->type != KEY_TYPE_snapshot)
155 snap = bkey_s_c_to_snapshot(k);
157 child[0] = le32_to_cpu(snap.v->children[0]);
158 child[1] = le32_to_cpu(snap.v->children[1]);
160 for (i = 0; i < 2; i++) {
161 int ret = snapshot_live(trans, child[i]);
171 snapshot_t(c, id)->equiv = nr_live == 1
172 ? snapshot_t(c, child[live_idx])->equiv
178 static int check_snapshot(struct btree_trans *trans,
179 struct btree_iter *iter,
182 struct bch_fs *c = trans->c;
183 struct bkey_s_c_snapshot s;
184 struct bch_subvolume subvol;
185 struct bch_snapshot v;
186 struct printbuf buf = PRINTBUF;
187 bool should_have_subvol;
191 if (k.k->type != KEY_TYPE_snapshot)
194 s = bkey_s_c_to_snapshot(k);
195 id = le32_to_cpu(s.v->parent);
197 ret = snapshot_lookup(trans, id, &v);
199 bch_err(c, "snapshot with nonexistent parent:\n %s",
200 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf));
204 if (le32_to_cpu(v.children[0]) != s.k->p.offset &&
205 le32_to_cpu(v.children[1]) != s.k->p.offset) {
206 bch_err(c, "snapshot parent %u missing pointer to child %llu",
213 for (i = 0; i < 2 && s.v->children[i]; i++) {
214 id = le32_to_cpu(s.v->children[i]);
216 ret = snapshot_lookup(trans, id, &v);
218 bch_err(c, "snapshot node %llu has nonexistent child %u",
223 if (le32_to_cpu(v.parent) != s.k->p.offset) {
224 bch_err(c, "snapshot child %u has wrong parent (got %u should be %llu)",
225 id, le32_to_cpu(v.parent), s.k->p.offset);
231 should_have_subvol = BCH_SNAPSHOT_SUBVOL(s.v) &&
232 !BCH_SNAPSHOT_DELETED(s.v);
234 if (should_have_subvol) {
235 id = le32_to_cpu(s.v->subvol);
236 ret = bch2_subvolume_get(trans, id, 0, false, &subvol);
238 bch_err(c, "snapshot points to nonexistent subvolume:\n %s",
239 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf));
243 if (BCH_SNAPSHOT_SUBVOL(s.v) != (le32_to_cpu(subvol.snapshot) == s.k->p.offset)) {
244 bch_err(c, "snapshot node %llu has wrong BCH_SNAPSHOT_SUBVOL",
250 if (fsck_err_on(s.v->subvol, c, "snapshot should not point to subvol:\n %s",
251 (bch2_bkey_val_to_text(&buf, c, s.s_c), buf.buf))) {
252 struct bkey_i_snapshot *u = bch2_trans_kmalloc(trans, sizeof(*u));
254 ret = PTR_ERR_OR_ZERO(u);
258 bkey_reassemble(&u->k_i, s.s_c);
260 ret = bch2_trans_update(trans, iter, &u->k_i, 0);
266 if (BCH_SNAPSHOT_DELETED(s.v))
267 set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
274 int bch2_fs_check_snapshots(struct bch_fs *c)
276 struct btree_trans trans;
277 struct btree_iter iter;
281 bch2_trans_init(&trans, c, 0, 0);
283 ret = for_each_btree_key_commit(&trans, iter,
284 BTREE_ID_snapshots, POS_MIN,
285 BTREE_ITER_PREFETCH, k,
286 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
287 check_snapshot(&trans, &iter, k));
290 bch_err(c, "error %i checking snapshots", ret);
292 bch2_trans_exit(&trans);
296 static int check_subvol(struct btree_trans *trans,
297 struct btree_iter *iter,
300 struct bkey_s_c_subvolume subvol;
301 struct bch_snapshot snapshot;
305 if (k.k->type != KEY_TYPE_subvolume)
308 subvol = bkey_s_c_to_subvolume(k);
309 snapid = le32_to_cpu(subvol.v->snapshot);
310 ret = snapshot_lookup(trans, snapid, &snapshot);
313 bch_err(trans->c, "subvolume %llu points to nonexistent snapshot %u",
314 k.k->p.offset, snapid);
318 if (BCH_SUBVOLUME_UNLINKED(subvol.v)) {
319 ret = bch2_subvolume_delete(trans, iter->pos.offset);
320 if (ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart))
321 bch_err(trans->c, "error deleting subvolume %llu: %s",
322 iter->pos.offset, bch2_err_str(ret));
330 int bch2_fs_check_subvols(struct bch_fs *c)
332 struct btree_trans trans;
333 struct btree_iter iter;
337 bch2_trans_init(&trans, c, 0, 0);
339 ret = for_each_btree_key_commit(&trans, iter,
340 BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k,
341 NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL,
342 check_subvol(&trans, &iter, k));
344 bch2_trans_exit(&trans);
349 void bch2_fs_snapshots_exit(struct bch_fs *c)
351 genradix_free(&c->snapshots);
354 int bch2_fs_snapshots_start(struct bch_fs *c)
356 struct btree_trans trans;
357 struct btree_iter iter;
361 bch2_trans_init(&trans, c, 0, 0);
363 for_each_btree_key2(&trans, iter, BTREE_ID_snapshots,
365 bch2_mark_snapshot(&trans, BTREE_ID_snapshots, 0, bkey_s_c_null, k, 0) ?:
366 bch2_snapshot_set_equiv(&trans, k));
368 bch2_trans_exit(&trans);
371 bch_err(c, "error starting snapshots: %s", bch2_err_str(ret));
376 * Mark a snapshot as deleted, for future cleanup:
378 static int bch2_snapshot_node_set_deleted(struct btree_trans *trans, u32 id)
380 struct btree_iter iter;
381 struct bkey_i_snapshot *s;
384 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots, POS(0, id),
386 s = bch2_bkey_get_mut_typed(trans, &iter, snapshot);
387 ret = PTR_ERR_OR_ZERO(s);
389 bch2_fs_inconsistent_on(ret == -ENOENT, trans->c, "missing snapshot %u", id);
393 /* already deleted? */
394 if (BCH_SNAPSHOT_DELETED(&s->v))
397 SET_BCH_SNAPSHOT_DELETED(&s->v, true);
398 SET_BCH_SNAPSHOT_SUBVOL(&s->v, false);
401 ret = bch2_trans_update(trans, &iter, &s->k_i, 0);
405 bch2_trans_iter_exit(trans, &iter);
409 static int bch2_snapshot_node_delete(struct btree_trans *trans, u32 id)
411 struct btree_iter iter, p_iter = (struct btree_iter) { NULL };
413 struct bkey_s_c_snapshot s;
418 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots, POS(0, id),
420 k = bch2_btree_iter_peek_slot(&iter);
425 if (k.k->type != KEY_TYPE_snapshot) {
426 bch2_fs_inconsistent(trans->c, "missing snapshot %u", id);
431 s = bkey_s_c_to_snapshot(k);
433 BUG_ON(!BCH_SNAPSHOT_DELETED(s.v));
434 parent_id = le32_to_cpu(s.v->parent);
437 struct bkey_i_snapshot *parent;
439 bch2_trans_iter_init(trans, &p_iter, BTREE_ID_snapshots,
442 parent = bch2_bkey_get_mut_typed(trans, &p_iter, snapshot);
443 ret = PTR_ERR_OR_ZERO(parent);
445 bch2_fs_inconsistent_on(ret == -ENOENT, trans->c, "missing snapshot %u", parent_id);
449 for (i = 0; i < 2; i++)
450 if (le32_to_cpu(parent->v.children[i]) == id)
454 bch_err(trans->c, "snapshot %u missing child pointer to %u",
457 parent->v.children[i] = 0;
459 if (le32_to_cpu(parent->v.children[0]) <
460 le32_to_cpu(parent->v.children[1]))
461 swap(parent->v.children[0],
462 parent->v.children[1]);
464 ret = bch2_trans_update(trans, &p_iter, &parent->k_i, 0);
469 ret = bch2_btree_delete_at(trans, &iter, 0);
471 bch2_trans_iter_exit(trans, &p_iter);
472 bch2_trans_iter_exit(trans, &iter);
476 int bch2_snapshot_node_create(struct btree_trans *trans, u32 parent,
478 u32 *snapshot_subvols,
481 struct btree_iter iter;
482 struct bkey_i_snapshot *n;
487 bch2_trans_iter_init(trans, &iter, BTREE_ID_snapshots,
488 POS_MIN, BTREE_ITER_INTENT);
489 k = bch2_btree_iter_peek(&iter);
494 for (i = 0; i < nr_snapids; i++) {
495 k = bch2_btree_iter_prev_slot(&iter);
500 if (!k.k || !k.k->p.offset) {
501 ret = -BCH_ERR_ENOSPC_snapshot_create;
505 n = bch2_bkey_alloc(trans, &iter, snapshot);
506 ret = PTR_ERR_OR_ZERO(n);
511 n->v.parent = cpu_to_le32(parent);
512 n->v.subvol = cpu_to_le32(snapshot_subvols[i]);
514 SET_BCH_SNAPSHOT_SUBVOL(&n->v, true);
516 ret = bch2_trans_update(trans, &iter, &n->k_i, 0) ?:
517 bch2_mark_snapshot(trans, BTREE_ID_snapshots, 0,
518 bkey_s_c_null, bkey_i_to_s_c(&n->k_i), 0);
522 new_snapids[i] = iter.pos.offset;
526 bch2_btree_iter_set_pos(&iter, POS(0, parent));
527 n = bch2_bkey_get_mut_typed(trans, &iter, snapshot);
528 ret = PTR_ERR_OR_ZERO(n);
531 bch_err(trans->c, "snapshot %u not found", parent);
535 if (n->v.children[0] || n->v.children[1]) {
536 bch_err(trans->c, "Trying to add child snapshot nodes to parent that already has children");
541 n->v.children[0] = cpu_to_le32(new_snapids[0]);
542 n->v.children[1] = cpu_to_le32(new_snapids[1]);
544 SET_BCH_SNAPSHOT_SUBVOL(&n->v, false);
545 ret = bch2_trans_update(trans, &iter, &n->k_i, 0);
550 bch2_trans_iter_exit(trans, &iter);
554 static int snapshot_delete_key(struct btree_trans *trans,
555 struct btree_iter *iter,
557 snapshot_id_list *deleted,
558 snapshot_id_list *equiv_seen,
559 struct bpos *last_pos)
561 struct bch_fs *c = trans->c;
562 u32 equiv = snapshot_t(c, k.k->p.snapshot)->equiv;
564 if (!bkey_eq(k.k->p, *last_pos))
568 if (snapshot_list_has_id(deleted, k.k->p.snapshot) ||
569 snapshot_list_has_id(equiv_seen, equiv)) {
570 return bch2_btree_delete_at(trans, iter,
571 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
573 return snapshot_list_add(c, equiv_seen, equiv);
577 static int bch2_delete_redundant_snapshot(struct btree_trans *trans, struct btree_iter *iter,
580 struct bkey_s_c_snapshot snap;
584 if (k.k->type != KEY_TYPE_snapshot)
587 snap = bkey_s_c_to_snapshot(k);
588 if (BCH_SNAPSHOT_DELETED(snap.v) ||
589 BCH_SNAPSHOT_SUBVOL(snap.v))
592 children[0] = le32_to_cpu(snap.v->children[0]);
593 children[1] = le32_to_cpu(snap.v->children[1]);
595 ret = snapshot_live(trans, children[0]) ?:
596 snapshot_live(trans, children[1]);
601 return bch2_snapshot_node_set_deleted(trans, k.k->p.offset);
605 int bch2_delete_dead_snapshots(struct bch_fs *c)
607 struct btree_trans trans;
608 struct btree_iter iter;
610 struct bkey_s_c_snapshot snap;
611 snapshot_id_list deleted = { 0 };
615 if (!test_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags))
618 if (!test_bit(BCH_FS_STARTED, &c->flags)) {
619 ret = bch2_fs_read_write_early(c);
621 bch_err(c, "error deleleting dead snapshots: error going rw: %s", bch2_err_str(ret));
626 bch2_trans_init(&trans, c, 0, 0);
629 * For every snapshot node: If we have no live children and it's not
630 * pointed to by a subvolume, delete it:
632 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_snapshots,
635 bch2_delete_redundant_snapshot(&trans, &iter, k));
637 bch_err(c, "error deleting redundant snapshots: %s", bch2_err_str(ret));
641 for_each_btree_key2(&trans, iter, BTREE_ID_snapshots,
643 bch2_snapshot_set_equiv(&trans, k));
645 bch_err(c, "error in bch2_snapshots_set_equiv: %s", bch2_err_str(ret));
649 for_each_btree_key(&trans, iter, BTREE_ID_snapshots,
650 POS_MIN, 0, k, ret) {
651 if (k.k->type != KEY_TYPE_snapshot)
654 snap = bkey_s_c_to_snapshot(k);
655 if (BCH_SNAPSHOT_DELETED(snap.v)) {
656 ret = snapshot_list_add(c, &deleted, k.k->p.offset);
661 bch2_trans_iter_exit(&trans, &iter);
664 bch_err(c, "error walking snapshots: %s", bch2_err_str(ret));
668 for (id = 0; id < BTREE_ID_NR; id++) {
669 struct bpos last_pos = POS_MIN;
670 snapshot_id_list equiv_seen = { 0 };
672 if (!btree_type_has_snapshots(id))
675 ret = for_each_btree_key_commit(&trans, iter,
677 BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
678 NULL, NULL, BTREE_INSERT_NOFAIL,
679 snapshot_delete_key(&trans, &iter, k, &deleted, &equiv_seen, &last_pos));
681 darray_exit(&equiv_seen);
684 bch_err(c, "error deleting snapshot keys: %s", bch2_err_str(ret));
689 for (i = 0; i < deleted.nr; i++) {
690 ret = commit_do(&trans, NULL, NULL, 0,
691 bch2_snapshot_node_delete(&trans, deleted.data[i]));
693 bch_err(c, "error deleting snapshot %u: %s",
694 deleted.data[i], bch2_err_str(ret));
699 clear_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
701 darray_exit(&deleted);
702 bch2_trans_exit(&trans);
706 static void bch2_delete_dead_snapshots_work(struct work_struct *work)
708 struct bch_fs *c = container_of(work, struct bch_fs, snapshot_delete_work);
710 bch2_delete_dead_snapshots(c);
711 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
714 void bch2_delete_dead_snapshots_async(struct bch_fs *c)
716 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_delete_dead_snapshots) &&
717 !queue_work(system_long_wq, &c->snapshot_delete_work))
718 bch2_write_ref_put(c, BCH_WRITE_REF_delete_dead_snapshots);
721 static int bch2_delete_dead_snapshots_hook(struct btree_trans *trans,
722 struct btree_trans_commit_hook *h)
724 struct bch_fs *c = trans->c;
726 set_bit(BCH_FS_HAVE_DELETED_SNAPSHOTS, &c->flags);
728 if (!test_bit(BCH_FS_FSCK_DONE, &c->flags))
731 bch2_delete_dead_snapshots_async(c);
737 int bch2_subvolume_invalid(const struct bch_fs *c, struct bkey_s_c k,
738 unsigned flags, struct printbuf *err)
740 if (bkey_lt(k.k->p, SUBVOL_POS_MIN) ||
741 bkey_gt(k.k->p, SUBVOL_POS_MAX)) {
742 prt_printf(err, "invalid pos");
743 return -BCH_ERR_invalid_bkey;
746 if (bkey_val_bytes(k.k) != sizeof(struct bch_subvolume)) {
747 prt_printf(err, "incorrect value size (%zu != %zu)",
748 bkey_val_bytes(k.k), sizeof(struct bch_subvolume));
749 return -BCH_ERR_invalid_bkey;
755 void bch2_subvolume_to_text(struct printbuf *out, struct bch_fs *c,
758 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
760 prt_printf(out, "root %llu snapshot id %u",
761 le64_to_cpu(s.v->inode),
762 le32_to_cpu(s.v->snapshot));
765 static __always_inline int
766 bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol,
767 bool inconsistent_if_not_found,
769 struct bch_subvolume *s)
771 struct btree_iter iter;
775 bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes, POS(0, subvol),
777 k = bch2_btree_iter_peek_slot(&iter);
778 ret = bkey_err(k) ?: k.k->type == KEY_TYPE_subvolume ? 0 : -ENOENT;
780 if (ret == -ENOENT && inconsistent_if_not_found)
781 bch2_fs_inconsistent(trans->c, "missing subvolume %u", subvol);
783 *s = *bkey_s_c_to_subvolume(k).v;
785 bch2_trans_iter_exit(trans, &iter);
789 int bch2_subvolume_get(struct btree_trans *trans, unsigned subvol,
790 bool inconsistent_if_not_found,
792 struct bch_subvolume *s)
794 return bch2_subvolume_get_inlined(trans, subvol, inconsistent_if_not_found, iter_flags, s);
797 int bch2_snapshot_get_subvol(struct btree_trans *trans, u32 snapshot,
798 struct bch_subvolume *subvol)
800 struct bch_snapshot snap;
802 return snapshot_lookup(trans, snapshot, &snap) ?:
803 bch2_subvolume_get(trans, le32_to_cpu(snap.subvol), true, 0, subvol);
806 int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvol,
809 struct bch_subvolume s;
812 ret = bch2_subvolume_get_inlined(trans, subvol, true,
814 BTREE_ITER_WITH_UPDATES,
817 *snapid = le32_to_cpu(s.snapshot);
822 * Delete subvolume, mark snapshot ID as deleted, queue up snapshot
825 int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
827 struct btree_iter iter;
829 struct bkey_s_c_subvolume subvol;
830 struct btree_trans_commit_hook *h;
834 bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes,
838 k = bch2_btree_iter_peek_slot(&iter);
843 if (k.k->type != KEY_TYPE_subvolume) {
844 bch2_fs_inconsistent(trans->c, "missing subvolume %u", subvolid);
849 subvol = bkey_s_c_to_subvolume(k);
850 snapid = le32_to_cpu(subvol.v->snapshot);
852 ret = bch2_btree_delete_at(trans, &iter, 0);
856 ret = bch2_snapshot_node_set_deleted(trans, snapid);
860 h = bch2_trans_kmalloc(trans, sizeof(*h));
861 ret = PTR_ERR_OR_ZERO(h);
865 h->fn = bch2_delete_dead_snapshots_hook;
866 bch2_trans_commit_hook(trans, h);
868 bch2_trans_iter_exit(trans, &iter);
872 void bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct *work)
874 struct bch_fs *c = container_of(work, struct bch_fs,
875 snapshot_wait_for_pagecache_and_delete_work);
881 mutex_lock(&c->snapshots_unlinked_lock);
882 s = c->snapshots_unlinked;
883 darray_init(&c->snapshots_unlinked);
884 mutex_unlock(&c->snapshots_unlinked_lock);
889 bch2_evict_subvolume_inodes(c, &s);
891 for (id = s.data; id < s.data + s.nr; id++) {
892 ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL,
893 bch2_subvolume_delete(&trans, *id));
895 bch_err(c, "error deleting subvolume %u: %s", *id, bch2_err_str(ret));
903 bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
906 struct subvolume_unlink_hook {
907 struct btree_trans_commit_hook h;
911 int bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans *trans,
912 struct btree_trans_commit_hook *_h)
914 struct subvolume_unlink_hook *h = container_of(_h, struct subvolume_unlink_hook, h);
915 struct bch_fs *c = trans->c;
918 mutex_lock(&c->snapshots_unlinked_lock);
919 if (!snapshot_list_has_id(&c->snapshots_unlinked, h->subvol))
920 ret = snapshot_list_add(c, &c->snapshots_unlinked, h->subvol);
921 mutex_unlock(&c->snapshots_unlinked_lock);
926 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_snapshot_delete_pagecache))
929 if (!queue_work(system_long_wq, &c->snapshot_wait_for_pagecache_and_delete_work))
930 bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
934 int bch2_subvolume_unlink(struct btree_trans *trans, u32 subvolid)
936 struct btree_iter iter;
937 struct bkey_i_subvolume *n;
938 struct subvolume_unlink_hook *h;
941 bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolumes,
945 n = bch2_bkey_get_mut_typed(trans, &iter, subvolume);
946 ret = PTR_ERR_OR_ZERO(n);
948 bch2_fs_inconsistent_on(ret == -ENOENT, trans->c, "missing subvolume %u", subvolid);
952 SET_BCH_SUBVOLUME_UNLINKED(&n->v, true);
954 ret = bch2_trans_update(trans, &iter, &n->k_i, 0);
958 h = bch2_trans_kmalloc(trans, sizeof(*h));
959 ret = PTR_ERR_OR_ZERO(h);
963 h->h.fn = bch2_subvolume_wait_for_pagecache_and_delete_hook;
964 h->subvol = subvolid;
965 bch2_trans_commit_hook(trans, &h->h);
967 bch2_trans_iter_exit(trans, &iter);
971 int bch2_subvolume_create(struct btree_trans *trans, u64 inode,
977 struct bch_fs *c = trans->c;
978 struct btree_iter dst_iter, src_iter = (struct btree_iter) { NULL };
979 struct bkey_i_subvolume *new_subvol = NULL;
980 struct bkey_i_subvolume *src_subvol = NULL;
982 u32 parent = 0, new_nodes[2], snapshot_subvols[2];
985 for_each_btree_key(trans, dst_iter, BTREE_ID_subvolumes, SUBVOL_POS_MIN,
986 BTREE_ITER_SLOTS|BTREE_ITER_INTENT, k, ret) {
987 if (bkey_gt(k.k->p, SUBVOL_POS_MAX))
991 * bch2_subvolume_delete() doesn't flush the btree key cache -
992 * ideally it would but that's tricky
994 if (bkey_deleted(k.k) &&
995 !bch2_btree_key_cache_find(c, BTREE_ID_subvolumes, dst_iter.pos))
1000 ret = -BCH_ERR_ENOSPC_subvolume_create;
1003 snapshot_subvols[0] = dst_iter.pos.offset;
1004 snapshot_subvols[1] = src_subvolid;
1007 /* Creating a snapshot: */
1009 bch2_trans_iter_init(trans, &src_iter, BTREE_ID_subvolumes,
1010 POS(0, src_subvolid),
1013 src_subvol = bch2_bkey_get_mut_typed(trans, &src_iter, subvolume);
1014 ret = PTR_ERR_OR_ZERO(src_subvol);
1015 if (unlikely(ret)) {
1016 bch2_fs_inconsistent_on(ret == -ENOENT, trans->c,
1017 "subvolume %u not found", src_subvolid);
1021 parent = le32_to_cpu(src_subvol->v.snapshot);
1024 ret = bch2_snapshot_node_create(trans, parent, new_nodes,
1026 src_subvolid ? 2 : 1);
1031 src_subvol->v.snapshot = cpu_to_le32(new_nodes[1]);
1032 ret = bch2_trans_update(trans, &src_iter, &src_subvol->k_i, 0);
1037 new_subvol = bch2_bkey_alloc(trans, &dst_iter, subvolume);
1038 ret = PTR_ERR_OR_ZERO(new_subvol);
1042 new_subvol->v.flags = 0;
1043 new_subvol->v.snapshot = cpu_to_le32(new_nodes[0]);
1044 new_subvol->v.inode = cpu_to_le64(inode);
1045 SET_BCH_SUBVOLUME_RO(&new_subvol->v, ro);
1046 SET_BCH_SUBVOLUME_SNAP(&new_subvol->v, src_subvolid != 0);
1047 ret = bch2_trans_update(trans, &dst_iter, &new_subvol->k_i, 0);
1051 *new_subvolid = new_subvol->k.p.offset;
1052 *new_snapshotid = new_nodes[0];
1054 bch2_trans_iter_exit(trans, &src_iter);
1055 bch2_trans_iter_exit(trans, &dst_iter);
1059 int bch2_fs_subvolumes_init(struct bch_fs *c)
1061 INIT_WORK(&c->snapshot_delete_work, bch2_delete_dead_snapshots_work);
1062 INIT_WORK(&c->snapshot_wait_for_pagecache_and_delete_work,
1063 bch2_subvolume_wait_for_pagecache_and_delete);
1064 mutex_init(&c->snapshots_unlinked_lock);