1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_key_cache.h"
5 #include "btree_update.h"
10 #include "subvolume.h"
12 #include <linux/random.h>
14 static int bch2_subvolume_delete(struct btree_trans *, u32);
16 static int check_subvol(struct btree_trans *trans,
17 struct btree_iter *iter,
20 struct bch_fs *c = trans->c;
21 struct bkey_s_c_subvolume subvol;
22 struct bch_snapshot snapshot;
26 if (k.k->type != KEY_TYPE_subvolume)
29 subvol = bkey_s_c_to_subvolume(k);
30 snapid = le32_to_cpu(subvol.v->snapshot);
31 ret = bch2_snapshot_lookup(trans, snapid, &snapshot);
33 if (bch2_err_matches(ret, ENOENT))
34 bch_err(c, "subvolume %llu points to nonexistent snapshot %u",
35 k.k->p.offset, snapid);
39 if (BCH_SUBVOLUME_UNLINKED(subvol.v)) {
40 ret = bch2_subvolume_delete(trans, iter->pos.offset);
41 bch_err_msg(c, ret, "deleting subvolume %llu", iter->pos.offset);
42 return ret ?: -BCH_ERR_transaction_restart_nested;
45 struct bch_inode_unpacked inode;
46 struct btree_iter inode_iter = {};
47 ret = bch2_inode_peek_nowarn(trans, &inode_iter, &inode,
48 (subvol_inum) { k.k->p.offset, le64_to_cpu(subvol.v->inode) },
50 bch2_trans_iter_exit(trans, &inode_iter);
52 if (ret && !bch2_err_matches(ret, ENOENT))
55 if (fsck_err_on(ret, c, subvol_to_missing_root,
56 "subvolume %llu points to missing subvolume root %llu:%u",
57 k.k->p.offset, le64_to_cpu(subvol.v->inode),
58 le32_to_cpu(subvol.v->snapshot))) {
59 ret = bch2_subvolume_delete(trans, iter->pos.offset);
60 bch_err_msg(c, ret, "deleting subvolume %llu", iter->pos.offset);
61 return ret ?: -BCH_ERR_transaction_restart_nested;
64 if (fsck_err_on(inode.bi_subvol != subvol.k->p.offset,
65 c, subvol_root_wrong_bi_subvol,
66 "subvol root %llu:%u has wrong bi_subvol field: got %u, should be %llu",
67 inode.bi_inum, inode_iter.k.p.snapshot,
68 inode.bi_subvol, subvol.k->p.offset)) {
69 inode.bi_subvol = subvol.k->p.offset;
70 ret = __bch2_fsck_write_inode(trans, &inode, le32_to_cpu(subvol.v->snapshot));
75 if (!BCH_SUBVOLUME_SNAP(subvol.v)) {
76 u32 snapshot_root = bch2_snapshot_root(c, le32_to_cpu(subvol.v->snapshot));
78 struct bch_snapshot_tree st;
81 snapshot_tree = snapshot_t(c, snapshot_root)->tree;
84 ret = bch2_snapshot_tree_lookup(trans, snapshot_tree, &st);
86 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
87 "%s: snapshot tree %u not found", __func__, snapshot_tree);
92 if (fsck_err_on(le32_to_cpu(st.master_subvol) != subvol.k->p.offset,
93 c, subvol_not_master_and_not_snapshot,
94 "subvolume %llu is not set as snapshot but is not master subvolume",
96 struct bkey_i_subvolume *s =
97 bch2_bkey_make_mut_typed(trans, iter, &subvol.s_c, 0, subvolume);
98 ret = PTR_ERR_OR_ZERO(s);
102 SET_BCH_SUBVOLUME_SNAP(&s->v, true);
111 int bch2_check_subvols(struct bch_fs *c)
113 int ret = bch2_trans_run(c,
114 for_each_btree_key_commit(trans, iter,
115 BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k,
116 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
117 check_subvol(trans, &iter, k)));
124 int bch2_subvolume_invalid(struct bch_fs *c, struct bkey_s_c k,
125 enum bkey_invalid_flags flags, struct printbuf *err)
129 bkey_fsck_err_on(bkey_lt(k.k->p, SUBVOL_POS_MIN) ||
130 bkey_gt(k.k->p, SUBVOL_POS_MAX), c, err,
137 void bch2_subvolume_to_text(struct printbuf *out, struct bch_fs *c,
140 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
142 prt_printf(out, "root %llu snapshot id %u",
143 le64_to_cpu(s.v->inode),
144 le32_to_cpu(s.v->snapshot));
146 if (bkey_val_bytes(s.k) > offsetof(struct bch_subvolume, parent))
147 prt_printf(out, " parent %u", le32_to_cpu(s.v->parent));
150 static __always_inline int
151 bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol,
152 bool inconsistent_if_not_found,
154 struct bch_subvolume *s)
156 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, subvol),
157 iter_flags, subvolume, s);
158 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT) &&
159 inconsistent_if_not_found,
160 trans->c, "missing subvolume %u", subvol);
164 int bch2_subvolume_get(struct btree_trans *trans, unsigned subvol,
165 bool inconsistent_if_not_found,
167 struct bch_subvolume *s)
169 return bch2_subvolume_get_inlined(trans, subvol, inconsistent_if_not_found, iter_flags, s);
172 int bch2_subvol_is_ro_trans(struct btree_trans *trans, u32 subvol)
174 struct bch_subvolume s;
175 int ret = bch2_subvolume_get_inlined(trans, subvol, true, 0, &s);
179 if (BCH_SUBVOLUME_RO(&s))
184 int bch2_subvol_is_ro(struct bch_fs *c, u32 subvol)
186 return bch2_trans_do(c, NULL, NULL, 0,
187 bch2_subvol_is_ro_trans(trans, subvol));
190 int bch2_snapshot_get_subvol(struct btree_trans *trans, u32 snapshot,
191 struct bch_subvolume *subvol)
193 struct bch_snapshot snap;
195 return bch2_snapshot_lookup(trans, snapshot, &snap) ?:
196 bch2_subvolume_get(trans, le32_to_cpu(snap.subvol), true, 0, subvol);
199 int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvolid,
202 struct btree_iter iter;
203 struct bkey_s_c_subvolume subvol;
206 subvol = bch2_bkey_get_iter_typed(trans, &iter,
207 BTREE_ID_subvolumes, POS(0, subvolid),
208 BTREE_ITER_CACHED|BTREE_ITER_WITH_UPDATES,
210 ret = bkey_err(subvol);
211 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
212 "missing subvolume %u", subvolid);
215 *snapid = le32_to_cpu(subvol.v->snapshot);
216 bch2_trans_iter_exit(trans, &iter);
220 static int bch2_subvolume_reparent(struct btree_trans *trans,
221 struct btree_iter *iter,
223 u32 old_parent, u32 new_parent)
225 struct bkey_i_subvolume *s;
228 if (k.k->type != KEY_TYPE_subvolume)
231 if (bkey_val_bytes(k.k) > offsetof(struct bch_subvolume, parent) &&
232 le32_to_cpu(bkey_s_c_to_subvolume(k).v->parent) != old_parent)
235 s = bch2_bkey_make_mut_typed(trans, iter, &k, 0, subvolume);
236 ret = PTR_ERR_OR_ZERO(s);
240 s->v.parent = cpu_to_le32(new_parent);
245 * Separate from the snapshot tree in the snapshots btree, we record the tree
246 * structure of how snapshot subvolumes were created - the parent subvolume of
247 * each snapshot subvolume.
249 * When a subvolume is deleted, we scan for child subvolumes and reparant them,
250 * to avoid dangling references:
252 static int bch2_subvolumes_reparent(struct btree_trans *trans, u32 subvolid_to_delete)
254 struct bch_subvolume s;
256 return lockrestart_do(trans,
257 bch2_subvolume_get(trans, subvolid_to_delete, true,
258 BTREE_ITER_CACHED, &s)) ?:
259 for_each_btree_key_commit(trans, iter,
260 BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k,
261 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
262 bch2_subvolume_reparent(trans, &iter, k,
263 subvolid_to_delete, le32_to_cpu(s.parent)));
267 * Delete subvolume, mark snapshot ID as deleted, queue up snapshot
270 static int __bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
272 struct btree_iter iter;
273 struct bkey_s_c_subvolume subvol;
277 subvol = bch2_bkey_get_iter_typed(trans, &iter,
278 BTREE_ID_subvolumes, POS(0, subvolid),
279 BTREE_ITER_CACHED|BTREE_ITER_INTENT,
281 ret = bkey_err(subvol);
282 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
283 "missing subvolume %u", subvolid);
287 snapid = le32_to_cpu(subvol.v->snapshot);
289 ret = bch2_btree_delete_at(trans, &iter, 0) ?:
290 bch2_snapshot_node_set_deleted(trans, snapid);
291 bch2_trans_iter_exit(trans, &iter);
295 static int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
297 return bch2_subvolumes_reparent(trans, subvolid) ?:
298 commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
299 __bch2_subvolume_delete(trans, subvolid));
302 static void bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct *work)
304 struct bch_fs *c = container_of(work, struct bch_fs,
305 snapshot_wait_for_pagecache_and_delete_work);
311 mutex_lock(&c->snapshots_unlinked_lock);
312 s = c->snapshots_unlinked;
313 darray_init(&c->snapshots_unlinked);
314 mutex_unlock(&c->snapshots_unlinked_lock);
319 bch2_evict_subvolume_inodes(c, &s);
321 for (id = s.data; id < s.data + s.nr; id++) {
322 ret = bch2_trans_run(c, bch2_subvolume_delete(trans, *id));
323 bch_err_msg(c, ret, "deleting subvolume %u", *id);
331 bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
334 struct subvolume_unlink_hook {
335 struct btree_trans_commit_hook h;
339 static int bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans *trans,
340 struct btree_trans_commit_hook *_h)
342 struct subvolume_unlink_hook *h = container_of(_h, struct subvolume_unlink_hook, h);
343 struct bch_fs *c = trans->c;
346 mutex_lock(&c->snapshots_unlinked_lock);
347 if (!snapshot_list_has_id(&c->snapshots_unlinked, h->subvol))
348 ret = snapshot_list_add(c, &c->snapshots_unlinked, h->subvol);
349 mutex_unlock(&c->snapshots_unlinked_lock);
354 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_snapshot_delete_pagecache))
357 if (!queue_work(c->write_ref_wq, &c->snapshot_wait_for_pagecache_and_delete_work))
358 bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
362 int bch2_subvolume_unlink(struct btree_trans *trans, u32 subvolid)
364 struct btree_iter iter;
365 struct bkey_i_subvolume *n;
366 struct subvolume_unlink_hook *h;
369 h = bch2_trans_kmalloc(trans, sizeof(*h));
370 ret = PTR_ERR_OR_ZERO(h);
374 h->h.fn = bch2_subvolume_wait_for_pagecache_and_delete_hook;
375 h->subvol = subvolid;
376 bch2_trans_commit_hook(trans, &h->h);
378 n = bch2_bkey_get_mut_typed(trans, &iter,
379 BTREE_ID_subvolumes, POS(0, subvolid),
380 BTREE_ITER_CACHED, subvolume);
381 ret = PTR_ERR_OR_ZERO(n);
383 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
384 "missing subvolume %u", subvolid);
388 SET_BCH_SUBVOLUME_UNLINKED(&n->v, true);
389 bch2_trans_iter_exit(trans, &iter);
393 int bch2_subvolume_create(struct btree_trans *trans, u64 inode,
399 struct bch_fs *c = trans->c;
400 struct btree_iter dst_iter, src_iter = (struct btree_iter) { NULL };
401 struct bkey_i_subvolume *new_subvol = NULL;
402 struct bkey_i_subvolume *src_subvol = NULL;
403 u32 parent = 0, new_nodes[2], snapshot_subvols[2];
406 ret = bch2_bkey_get_empty_slot(trans, &dst_iter,
407 BTREE_ID_subvolumes, POS(0, U32_MAX));
408 if (ret == -BCH_ERR_ENOSPC_btree_slot)
409 ret = -BCH_ERR_ENOSPC_subvolume_create;
413 snapshot_subvols[0] = dst_iter.pos.offset;
414 snapshot_subvols[1] = src_subvolid;
417 /* Creating a snapshot: */
419 src_subvol = bch2_bkey_get_mut_typed(trans, &src_iter,
420 BTREE_ID_subvolumes, POS(0, src_subvolid),
421 BTREE_ITER_CACHED, subvolume);
422 ret = PTR_ERR_OR_ZERO(src_subvol);
424 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
425 "subvolume %u not found", src_subvolid);
429 parent = le32_to_cpu(src_subvol->v.snapshot);
432 ret = bch2_snapshot_node_create(trans, parent, new_nodes,
434 src_subvolid ? 2 : 1);
439 src_subvol->v.snapshot = cpu_to_le32(new_nodes[1]);
440 ret = bch2_trans_update(trans, &src_iter, &src_subvol->k_i, 0);
445 new_subvol = bch2_bkey_alloc(trans, &dst_iter, 0, subvolume);
446 ret = PTR_ERR_OR_ZERO(new_subvol);
450 new_subvol->v.flags = 0;
451 new_subvol->v.snapshot = cpu_to_le32(new_nodes[0]);
452 new_subvol->v.inode = cpu_to_le64(inode);
453 new_subvol->v.parent = cpu_to_le32(src_subvolid);
454 new_subvol->v.otime.lo = cpu_to_le64(bch2_current_time(c));
455 new_subvol->v.otime.hi = 0;
457 SET_BCH_SUBVOLUME_RO(&new_subvol->v, ro);
458 SET_BCH_SUBVOLUME_SNAP(&new_subvol->v, src_subvolid != 0);
460 *new_subvolid = new_subvol->k.p.offset;
461 *new_snapshotid = new_nodes[0];
463 bch2_trans_iter_exit(trans, &src_iter);
464 bch2_trans_iter_exit(trans, &dst_iter);
468 int bch2_fs_subvolumes_init(struct bch_fs *c)
470 INIT_WORK(&c->snapshot_delete_work, bch2_delete_dead_snapshots_work);
471 INIT_WORK(&c->snapshot_wait_for_pagecache_and_delete_work,
472 bch2_subvolume_wait_for_pagecache_and_delete);
473 mutex_init(&c->snapshots_unlinked_lock);