114b393eeSKent Overstreet // SPDX-License-Identifier: GPL-2.0
214b393eeSKent Overstreet
314b393eeSKent Overstreet #include "bcachefs.h"
414b393eeSKent Overstreet #include "btree_key_cache.h"
514b393eeSKent Overstreet #include "btree_update.h"
6d4bf5eecSKent Overstreet #include "errcode.h"
714b393eeSKent Overstreet #include "error.h"
82027875bSKent Overstreet #include "fs.h"
98e877caaSKent Overstreet #include "snapshot.h"
1014b393eeSKent Overstreet #include "subvolume.h"
1114b393eeSKent Overstreet
12f26c67f4SKent Overstreet #include <linux/random.h>
13f26c67f4SKent Overstreet
14653693beSKent Overstreet static int bch2_subvolume_delete(struct btree_trans *, u32);
15653693beSKent Overstreet
subvolume_children_pos(struct bkey_s_c k)16b26d7914SKent Overstreet static struct bpos subvolume_children_pos(struct bkey_s_c k)
17b26d7914SKent Overstreet {
18b26d7914SKent Overstreet if (k.k->type != KEY_TYPE_subvolume)
19b26d7914SKent Overstreet return POS_MIN;
20b26d7914SKent Overstreet
21b26d7914SKent Overstreet struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
22b26d7914SKent Overstreet if (!s.v->fs_path_parent)
23b26d7914SKent Overstreet return POS_MIN;
24b26d7914SKent Overstreet return POS(le32_to_cpu(s.v->fs_path_parent), s.k->p.offset);
25b26d7914SKent Overstreet }
26b26d7914SKent Overstreet
check_subvol(struct btree_trans * trans,struct btree_iter * iter,struct bkey_s_c k)274ab35c34SKent Overstreet static int check_subvol(struct btree_trans *trans,
286738dd19SKent Overstreet struct btree_iter *iter,
296738dd19SKent Overstreet struct bkey_s_c k)
304ab35c34SKent Overstreet {
311c59b483SKent Overstreet struct bch_fs *c = trans->c;
324ab35c34SKent Overstreet struct bkey_s_c_subvolume subvol;
33b26d7914SKent Overstreet struct btree_iter subvol_children_iter = {};
3435f1a503SKent Overstreet struct bch_snapshot snapshot;
35b8628a25SKent Overstreet struct printbuf buf = PRINTBUF;
3635f1a503SKent Overstreet unsigned snapid;
371c59b483SKent Overstreet int ret = 0;
384ab35c34SKent Overstreet
394ab35c34SKent Overstreet if (k.k->type != KEY_TYPE_subvolume)
404ab35c34SKent Overstreet return 0;
414ab35c34SKent Overstreet
424ab35c34SKent Overstreet subvol = bkey_s_c_to_subvolume(k);
4335f1a503SKent Overstreet snapid = le32_to_cpu(subvol.v->snapshot);
448e877caaSKent Overstreet ret = bch2_snapshot_lookup(trans, snapid, &snapshot);
4535f1a503SKent Overstreet
461c59b483SKent Overstreet if (bch2_err_matches(ret, ENOENT))
471c59b483SKent Overstreet bch_err(c, "subvolume %llu points to nonexistent snapshot %u",
4835f1a503SKent Overstreet k.k->p.offset, snapid);
4935f1a503SKent Overstreet if (ret)
5035f1a503SKent Overstreet return ret;
514ab35c34SKent Overstreet
524ab35c34SKent Overstreet if (BCH_SUBVOLUME_UNLINKED(subvol.v)) {
534ab35c34SKent Overstreet ret = bch2_subvolume_delete(trans, iter->pos.offset);
54e46c181aSKent Overstreet bch_err_msg(c, ret, "deleting subvolume %llu", iter->pos.offset);
55653693beSKent Overstreet return ret ?: -BCH_ERR_transaction_restart_nested;
564ab35c34SKent Overstreet }
574ab35c34SKent Overstreet
58b8628a25SKent Overstreet if (fsck_err_on(subvol.k->p.offset == BCACHEFS_ROOT_SUBVOL &&
59b8628a25SKent Overstreet subvol.v->fs_path_parent,
60a850bde6SKent Overstreet trans, subvol_root_fs_path_parent_nonzero,
61b8628a25SKent Overstreet "root subvolume has nonzero fs_path_parent\n%s",
62b8628a25SKent Overstreet (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
63b8628a25SKent Overstreet struct bkey_i_subvolume *n =
64b8628a25SKent Overstreet bch2_bkey_make_mut_typed(trans, iter, &subvol.s_c, 0, subvolume);
65b8628a25SKent Overstreet ret = PTR_ERR_OR_ZERO(n);
66b8628a25SKent Overstreet if (ret)
67b8628a25SKent Overstreet goto err;
68b8628a25SKent Overstreet
69b8628a25SKent Overstreet n->v.fs_path_parent = 0;
70b8628a25SKent Overstreet }
71b8628a25SKent Overstreet
72b26d7914SKent Overstreet if (subvol.v->fs_path_parent) {
73b26d7914SKent Overstreet struct bpos pos = subvolume_children_pos(k);
74b26d7914SKent Overstreet
75b26d7914SKent Overstreet struct bkey_s_c subvol_children_k =
76b26d7914SKent Overstreet bch2_bkey_get_iter(trans, &subvol_children_iter,
77b26d7914SKent Overstreet BTREE_ID_subvolume_children, pos, 0);
78b26d7914SKent Overstreet ret = bkey_err(subvol_children_k);
79b26d7914SKent Overstreet if (ret)
80b26d7914SKent Overstreet goto err;
81b26d7914SKent Overstreet
82b26d7914SKent Overstreet if (fsck_err_on(subvol_children_k.k->type != KEY_TYPE_set,
83a850bde6SKent Overstreet trans, subvol_children_not_set,
84b26d7914SKent Overstreet "subvolume not set in subvolume_children btree at %llu:%llu\n%s",
85b26d7914SKent Overstreet pos.inode, pos.offset,
86b26d7914SKent Overstreet (printbuf_reset(&buf),
87b26d7914SKent Overstreet bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
88b26d7914SKent Overstreet ret = bch2_btree_bit_mod(trans, BTREE_ID_subvolume_children, pos, true);
89b26d7914SKent Overstreet if (ret)
90b26d7914SKent Overstreet goto err;
91b26d7914SKent Overstreet }
92b26d7914SKent Overstreet }
93b26d7914SKent Overstreet
944c20278eSKent Overstreet struct bch_inode_unpacked inode;
95951dd86eSKent Overstreet ret = bch2_inode_find_by_inum_nowarn_trans(trans,
964c20278eSKent Overstreet (subvol_inum) { k.k->p.offset, le64_to_cpu(subvol.v->inode) },
97951dd86eSKent Overstreet &inode);
98951dd86eSKent Overstreet if (!ret) {
99951dd86eSKent Overstreet if (fsck_err_on(inode.bi_subvol != subvol.k->p.offset,
100951dd86eSKent Overstreet trans, subvol_root_wrong_bi_subvol,
101951dd86eSKent Overstreet "subvol root %llu:%u has wrong bi_subvol field: got %u, should be %llu",
102951dd86eSKent Overstreet inode.bi_inum, inode.bi_snapshot,
103951dd86eSKent Overstreet inode.bi_subvol, subvol.k->p.offset)) {
104951dd86eSKent Overstreet inode.bi_subvol = subvol.k->p.offset;
10572350ee0SKent Overstreet inode.bi_snapshot = le32_to_cpu(subvol.v->snapshot);
10672350ee0SKent Overstreet ret = __bch2_fsck_write_inode(trans, &inode);
107951dd86eSKent Overstreet if (ret)
108951dd86eSKent Overstreet goto err;
109951dd86eSKent Overstreet }
110951dd86eSKent Overstreet } else if (bch2_err_matches(ret, ENOENT)) {
111951dd86eSKent Overstreet if (fsck_err(trans, subvol_to_missing_root,
1124c20278eSKent Overstreet "subvolume %llu points to missing subvolume root %llu:%u",
1134c20278eSKent Overstreet k.k->p.offset, le64_to_cpu(subvol.v->inode),
1144c20278eSKent Overstreet le32_to_cpu(subvol.v->snapshot))) {
1154c20278eSKent Overstreet ret = bch2_subvolume_delete(trans, iter->pos.offset);
1164c20278eSKent Overstreet bch_err_msg(c, ret, "deleting subvolume %llu", iter->pos.offset);
117951dd86eSKent Overstreet ret = ret ?: -BCH_ERR_transaction_restart_nested;
118951dd86eSKent Overstreet goto err;
1194c20278eSKent Overstreet }
120951dd86eSKent Overstreet } else {
1214c20278eSKent Overstreet goto err;
1224c20278eSKent Overstreet }
1234c20278eSKent Overstreet
1241c59b483SKent Overstreet if (!BCH_SUBVOLUME_SNAP(subvol.v)) {
1251c59b483SKent Overstreet u32 snapshot_root = bch2_snapshot_root(c, le32_to_cpu(subvol.v->snapshot));
1268479938dSKent Overstreet u32 snapshot_tree;
1271c59b483SKent Overstreet struct bch_snapshot_tree st;
1281c59b483SKent Overstreet
1298479938dSKent Overstreet rcu_read_lock();
1308479938dSKent Overstreet snapshot_tree = snapshot_t(c, snapshot_root)->tree;
1318479938dSKent Overstreet rcu_read_unlock();
1328479938dSKent Overstreet
133cb1b479dSKent Overstreet ret = bch2_snapshot_tree_lookup(trans, snapshot_tree, &st);
1341c59b483SKent Overstreet
1351c59b483SKent Overstreet bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
1361c59b483SKent Overstreet "%s: snapshot tree %u not found", __func__, snapshot_tree);
1371c59b483SKent Overstreet
1381c59b483SKent Overstreet if (ret)
139951dd86eSKent Overstreet goto err;
1401c59b483SKent Overstreet
141b65db750SKent Overstreet if (fsck_err_on(le32_to_cpu(st.master_subvol) != subvol.k->p.offset,
142a850bde6SKent Overstreet trans, subvol_not_master_and_not_snapshot,
1431c59b483SKent Overstreet "subvolume %llu is not set as snapshot but is not master subvolume",
1441c59b483SKent Overstreet k.k->p.offset)) {
1451c59b483SKent Overstreet struct bkey_i_subvolume *s =
1460fb3355dSKent Overstreet bch2_bkey_make_mut_typed(trans, iter, &subvol.s_c, 0, subvolume);
1471c59b483SKent Overstreet ret = PTR_ERR_OR_ZERO(s);
1481c59b483SKent Overstreet if (ret)
149951dd86eSKent Overstreet goto err;
1501c59b483SKent Overstreet
1511c59b483SKent Overstreet SET_BCH_SUBVOLUME_SNAP(&s->v, true);
1521c59b483SKent Overstreet }
1531c59b483SKent Overstreet }
1544c20278eSKent Overstreet err:
1551c59b483SKent Overstreet fsck_err:
156b26d7914SKent Overstreet bch2_trans_iter_exit(trans, &subvol_children_iter);
157b8628a25SKent Overstreet printbuf_exit(&buf);
1581c59b483SKent Overstreet return ret;
1594ab35c34SKent Overstreet }
1604ab35c34SKent Overstreet
bch2_check_subvols(struct bch_fs * c)161067d228bSKent Overstreet int bch2_check_subvols(struct bch_fs *c)
1624ab35c34SKent Overstreet {
16380eab7a7SKent Overstreet int ret = bch2_trans_run(c,
1646bd68ec2SKent Overstreet for_each_btree_key_commit(trans, iter,
1655dd8c60eSKent Overstreet BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_prefetch, k,
1663f0e297dSKent Overstreet NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1676bd68ec2SKent Overstreet check_subvol(trans, &iter, k)));
1681bb3c2a9SKent Overstreet bch_err_fn(c, ret);
1694ab35c34SKent Overstreet return ret;
1704ab35c34SKent Overstreet }
1714ab35c34SKent Overstreet
check_subvol_child(struct btree_trans * trans,struct btree_iter * child_iter,struct bkey_s_c child_k)172b26d7914SKent Overstreet static int check_subvol_child(struct btree_trans *trans,
173b26d7914SKent Overstreet struct btree_iter *child_iter,
174b26d7914SKent Overstreet struct bkey_s_c child_k)
175b26d7914SKent Overstreet {
176b26d7914SKent Overstreet struct bch_subvolume s;
177b26d7914SKent Overstreet int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, child_k.k->p.offset),
178b26d7914SKent Overstreet 0, subvolume, &s);
179b26d7914SKent Overstreet if (ret && !bch2_err_matches(ret, ENOENT))
180b26d7914SKent Overstreet return ret;
181b26d7914SKent Overstreet
182b26d7914SKent Overstreet if (fsck_err_on(ret ||
183b26d7914SKent Overstreet le32_to_cpu(s.fs_path_parent) != child_k.k->p.inode,
184a850bde6SKent Overstreet trans, subvol_children_bad,
185b26d7914SKent Overstreet "incorrect entry in subvolume_children btree %llu:%llu",
186b26d7914SKent Overstreet child_k.k->p.inode, child_k.k->p.offset)) {
187b26d7914SKent Overstreet ret = bch2_btree_delete_at(trans, child_iter, 0);
188b26d7914SKent Overstreet if (ret)
189b26d7914SKent Overstreet goto err;
190b26d7914SKent Overstreet }
191b26d7914SKent Overstreet err:
192b26d7914SKent Overstreet fsck_err:
193b26d7914SKent Overstreet return ret;
194b26d7914SKent Overstreet }
195b26d7914SKent Overstreet
bch2_check_subvol_children(struct bch_fs * c)196b26d7914SKent Overstreet int bch2_check_subvol_children(struct bch_fs *c)
197b26d7914SKent Overstreet {
198b26d7914SKent Overstreet int ret = bch2_trans_run(c,
199b26d7914SKent Overstreet for_each_btree_key_commit(trans, iter,
2005dd8c60eSKent Overstreet BTREE_ID_subvolume_children, POS_MIN, BTREE_ITER_prefetch, k,
201b26d7914SKent Overstreet NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
202b26d7914SKent Overstreet check_subvol_child(trans, &iter, k)));
203b26d7914SKent Overstreet bch_err_fn(c, ret);
204b26d7914SKent Overstreet return 0;
205b26d7914SKent Overstreet }
206b26d7914SKent Overstreet
20714b393eeSKent Overstreet /* Subvolumes: */
20814b393eeSKent Overstreet
bch2_subvolume_validate(struct bch_fs * c,struct bkey_s_c k,enum bch_validate_flags flags)209d97de0d0SKent Overstreet int bch2_subvolume_validate(struct bch_fs *c, struct bkey_s_c k,
210d97de0d0SKent Overstreet enum bch_validate_flags flags)
21114b393eeSKent Overstreet {
212765b8cb8SKent Overstreet struct bkey_s_c_subvolume subvol = bkey_s_c_to_subvolume(k);
213b65db750SKent Overstreet int ret = 0;
21414b393eeSKent Overstreet
215b65db750SKent Overstreet bkey_fsck_err_on(bkey_lt(k.k->p, SUBVOL_POS_MIN) ||
216d97de0d0SKent Overstreet bkey_gt(k.k->p, SUBVOL_POS_MAX),
217d97de0d0SKent Overstreet c, subvol_pos_bad,
218b65db750SKent Overstreet "invalid pos");
219765b8cb8SKent Overstreet
220d97de0d0SKent Overstreet bkey_fsck_err_on(!subvol.v->snapshot,
221d97de0d0SKent Overstreet c, subvol_snapshot_bad,
222765b8cb8SKent Overstreet "invalid snapshot");
223765b8cb8SKent Overstreet
224d97de0d0SKent Overstreet bkey_fsck_err_on(!subvol.v->inode,
225d97de0d0SKent Overstreet c, subvol_inode_bad,
226765b8cb8SKent Overstreet "invalid inode");
227b65db750SKent Overstreet fsck_err:
228b65db750SKent Overstreet return ret;
22914b393eeSKent Overstreet }
23014b393eeSKent Overstreet
bch2_subvolume_to_text(struct printbuf * out,struct bch_fs * c,struct bkey_s_c k)23114b393eeSKent Overstreet void bch2_subvolume_to_text(struct printbuf *out, struct bch_fs *c,
23214b393eeSKent Overstreet struct bkey_s_c k)
23314b393eeSKent Overstreet {
23414b393eeSKent Overstreet struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);
23514b393eeSKent Overstreet
236401ec4dbSKent Overstreet prt_printf(out, "root %llu snapshot id %u",
23714b393eeSKent Overstreet le64_to_cpu(s.v->inode),
23814b393eeSKent Overstreet le32_to_cpu(s.v->snapshot));
239653693beSKent Overstreet
240b8628a25SKent Overstreet if (bkey_val_bytes(s.k) > offsetof(struct bch_subvolume, creation_parent)) {
24164304aafSKent Overstreet prt_printf(out, " creation_parent %u", le32_to_cpu(s.v->creation_parent));
242b8628a25SKent Overstreet prt_printf(out, " fs_parent %u", le32_to_cpu(s.v->fs_path_parent));
243b8628a25SKent Overstreet }
24414b393eeSKent Overstreet }
24514b393eeSKent Overstreet
subvolume_children_mod(struct btree_trans * trans,struct bpos pos,bool set)246b26d7914SKent Overstreet static int subvolume_children_mod(struct btree_trans *trans, struct bpos pos, bool set)
247b26d7914SKent Overstreet {
248b26d7914SKent Overstreet return !bpos_eq(pos, POS_MIN)
249b26d7914SKent Overstreet ? bch2_btree_bit_mod(trans, BTREE_ID_subvolume_children, pos, set)
250b26d7914SKent Overstreet : 0;
251b26d7914SKent Overstreet }
252b26d7914SKent Overstreet
bch2_subvolume_trigger(struct btree_trans * trans,enum btree_id btree_id,unsigned level,struct bkey_s_c old,struct bkey_s new,enum btree_iter_update_trigger_flags flags)253b26d7914SKent Overstreet int bch2_subvolume_trigger(struct btree_trans *trans,
254b26d7914SKent Overstreet enum btree_id btree_id, unsigned level,
255b26d7914SKent Overstreet struct bkey_s_c old, struct bkey_s new,
2562d288745SNathan Chancellor enum btree_iter_update_trigger_flags flags)
257b26d7914SKent Overstreet {
2585dd8c60eSKent Overstreet if (flags & BTREE_TRIGGER_transactional) {
259b26d7914SKent Overstreet struct bpos children_pos_old = subvolume_children_pos(old);
260b26d7914SKent Overstreet struct bpos children_pos_new = subvolume_children_pos(new.s_c);
261b26d7914SKent Overstreet
262b26d7914SKent Overstreet if (!bpos_eq(children_pos_old, children_pos_new)) {
263b26d7914SKent Overstreet int ret = subvolume_children_mod(trans, children_pos_old, false) ?:
264b26d7914SKent Overstreet subvolume_children_mod(trans, children_pos_new, true);
265b26d7914SKent Overstreet if (ret)
266b26d7914SKent Overstreet return ret;
267b26d7914SKent Overstreet }
268b26d7914SKent Overstreet }
269b26d7914SKent Overstreet
270b26d7914SKent Overstreet return 0;
271b26d7914SKent Overstreet }
272b26d7914SKent Overstreet
bch2_subvol_has_children(struct btree_trans * trans,u32 subvol)273835cd3e1SKent Overstreet int bch2_subvol_has_children(struct btree_trans *trans, u32 subvol)
274835cd3e1SKent Overstreet {
275835cd3e1SKent Overstreet struct btree_iter iter;
276835cd3e1SKent Overstreet
277835cd3e1SKent Overstreet bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolume_children, POS(subvol, 0), 0);
278835cd3e1SKent Overstreet struct bkey_s_c k = bch2_btree_iter_peek(&iter);
279835cd3e1SKent Overstreet bch2_trans_iter_exit(trans, &iter);
280835cd3e1SKent Overstreet
281835cd3e1SKent Overstreet return bkey_err(k) ?: k.k && k.k->p.inode == subvol
282835cd3e1SKent Overstreet ? -BCH_ERR_ENOTEMPTY_subvol_not_empty
283835cd3e1SKent Overstreet : 0;
284835cd3e1SKent Overstreet }
285835cd3e1SKent Overstreet
28698638ffaSKent Overstreet static __always_inline int
bch2_subvolume_get_inlined(struct btree_trans * trans,unsigned subvol,bool inconsistent_if_not_found,int iter_flags,struct bch_subvolume * s)28798638ffaSKent Overstreet bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol,
28897996ddfSKent Overstreet bool inconsistent_if_not_found,
28997996ddfSKent Overstreet int iter_flags,
29097996ddfSKent Overstreet struct bch_subvolume *s)
29114b393eeSKent Overstreet {
292bcb79a51SKent Overstreet int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, subvol),
293bcb79a51SKent Overstreet iter_flags, subvolume, s);
2941c59b483SKent Overstreet bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT) &&
2951c59b483SKent Overstreet inconsistent_if_not_found,
296bcb79a51SKent Overstreet trans->c, "missing subvolume %u", subvol);
29797996ddfSKent Overstreet return ret;
29814b393eeSKent Overstreet }
29914b393eeSKent Overstreet
bch2_subvolume_get(struct btree_trans * trans,unsigned subvol,bool inconsistent_if_not_found,int iter_flags,struct bch_subvolume * s)30098638ffaSKent Overstreet int bch2_subvolume_get(struct btree_trans *trans, unsigned subvol,
30198638ffaSKent Overstreet bool inconsistent_if_not_found,
30298638ffaSKent Overstreet int iter_flags,
30398638ffaSKent Overstreet struct bch_subvolume *s)
30498638ffaSKent Overstreet {
30598638ffaSKent Overstreet return bch2_subvolume_get_inlined(trans, subvol, inconsistent_if_not_found, iter_flags, s);
30698638ffaSKent Overstreet }
30798638ffaSKent Overstreet
bch2_subvol_is_ro_trans(struct btree_trans * trans,u32 subvol)3080d72ab35SKent Overstreet int bch2_subvol_is_ro_trans(struct btree_trans *trans, u32 subvol)
3090d72ab35SKent Overstreet {
3100d72ab35SKent Overstreet struct bch_subvolume s;
3110d72ab35SKent Overstreet int ret = bch2_subvolume_get_inlined(trans, subvol, true, 0, &s);
3120d72ab35SKent Overstreet if (ret)
3130d72ab35SKent Overstreet return ret;
3140d72ab35SKent Overstreet
3150d72ab35SKent Overstreet if (BCH_SUBVOLUME_RO(&s))
3160d72ab35SKent Overstreet return -EROFS;
3170d72ab35SKent Overstreet return 0;
3180d72ab35SKent Overstreet }
3190d72ab35SKent Overstreet
bch2_subvol_is_ro(struct bch_fs * c,u32 subvol)3200d72ab35SKent Overstreet int bch2_subvol_is_ro(struct bch_fs *c, u32 subvol)
3210d72ab35SKent Overstreet {
322*a0d11feeSKent Overstreet return bch2_trans_do(c, bch2_subvol_is_ro_trans(trans, subvol));
3230d72ab35SKent Overstreet }
3240d72ab35SKent Overstreet
bch2_snapshot_get_subvol(struct btree_trans * trans,u32 snapshot,struct bch_subvolume * subvol)3259ca4853bSKent Overstreet int bch2_snapshot_get_subvol(struct btree_trans *trans, u32 snapshot,
3269ca4853bSKent Overstreet struct bch_subvolume *subvol)
3279ca4853bSKent Overstreet {
3289ca4853bSKent Overstreet struct bch_snapshot snap;
3299ca4853bSKent Overstreet
3308e877caaSKent Overstreet return bch2_snapshot_lookup(trans, snapshot, &snap) ?:
3319ca4853bSKent Overstreet bch2_subvolume_get(trans, le32_to_cpu(snap.subvol), true, 0, subvol);
3329ca4853bSKent Overstreet }
3339ca4853bSKent Overstreet
__bch2_subvolume_get_snapshot(struct btree_trans * trans,u32 subvolid,u32 * snapid,bool warn)3341f73cb4dSKent Overstreet int __bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvolid,
3351f73cb4dSKent Overstreet u32 *snapid, bool warn)
33697996ddfSKent Overstreet {
337653693beSKent Overstreet struct btree_iter iter;
3388e877caaSKent Overstreet struct bkey_s_c_subvolume subvol;
33997996ddfSKent Overstreet int ret;
34097996ddfSKent Overstreet
3418e877caaSKent Overstreet subvol = bch2_bkey_get_iter_typed(trans, &iter,
3428e877caaSKent Overstreet BTREE_ID_subvolumes, POS(0, subvolid),
3435dd8c60eSKent Overstreet BTREE_ITER_cached|BTREE_ITER_with_updates,
3448e877caaSKent Overstreet subvolume);
3458e877caaSKent Overstreet ret = bkey_err(subvol);
3461f73cb4dSKent Overstreet
3471f73cb4dSKent Overstreet bch2_fs_inconsistent_on(warn && bch2_err_matches(ret, ENOENT), trans->c,
3488e877caaSKent Overstreet "missing subvolume %u", subvolid);
349653693beSKent Overstreet
350653693beSKent Overstreet if (likely(!ret))
3518e877caaSKent Overstreet *snapid = le32_to_cpu(subvol.v->snapshot);
352653693beSKent Overstreet bch2_trans_iter_exit(trans, &iter);
35314b393eeSKent Overstreet return ret;
35414b393eeSKent Overstreet }
35514b393eeSKent Overstreet
bch2_subvolume_get_snapshot(struct btree_trans * trans,u32 subvolid,u32 * snapid)3561f73cb4dSKent Overstreet int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvolid,
3571f73cb4dSKent Overstreet u32 *snapid)
3581f73cb4dSKent Overstreet {
3591f73cb4dSKent Overstreet return __bch2_subvolume_get_snapshot(trans, subvolid, snapid, true);
3601f73cb4dSKent Overstreet }
3611f73cb4dSKent Overstreet
bch2_subvolume_reparent(struct btree_trans * trans,struct btree_iter * iter,struct bkey_s_c k,u32 old_parent,u32 new_parent)362653693beSKent Overstreet static int bch2_subvolume_reparent(struct btree_trans *trans,
363653693beSKent Overstreet struct btree_iter *iter,
364653693beSKent Overstreet struct bkey_s_c k,
365653693beSKent Overstreet u32 old_parent, u32 new_parent)
366653693beSKent Overstreet {
367653693beSKent Overstreet struct bkey_i_subvolume *s;
368653693beSKent Overstreet int ret;
369653693beSKent Overstreet
370653693beSKent Overstreet if (k.k->type != KEY_TYPE_subvolume)
371653693beSKent Overstreet return 0;
372653693beSKent Overstreet
37364304aafSKent Overstreet if (bkey_val_bytes(k.k) > offsetof(struct bch_subvolume, creation_parent) &&
37464304aafSKent Overstreet le32_to_cpu(bkey_s_c_to_subvolume(k).v->creation_parent) != old_parent)
375653693beSKent Overstreet return 0;
376653693beSKent Overstreet
3770fb3355dSKent Overstreet s = bch2_bkey_make_mut_typed(trans, iter, &k, 0, subvolume);
378653693beSKent Overstreet ret = PTR_ERR_OR_ZERO(s);
379653693beSKent Overstreet if (ret)
380653693beSKent Overstreet return ret;
381653693beSKent Overstreet
38264304aafSKent Overstreet s->v.creation_parent = cpu_to_le32(new_parent);
383653693beSKent Overstreet return 0;
384653693beSKent Overstreet }
385653693beSKent Overstreet
386653693beSKent Overstreet /*
3878e877caaSKent Overstreet * Separate from the snapshot tree in the snapshots btree, we record the tree
3888e877caaSKent Overstreet * structure of how snapshot subvolumes were created - the parent subvolume of
3898e877caaSKent Overstreet * each snapshot subvolume.
3908e877caaSKent Overstreet *
3918e877caaSKent Overstreet * When a subvolume is deleted, we scan for child subvolumes and reparant them,
3928e877caaSKent Overstreet * to avoid dangling references:
393653693beSKent Overstreet */
bch2_subvolumes_reparent(struct btree_trans * trans,u32 subvolid_to_delete)394653693beSKent Overstreet static int bch2_subvolumes_reparent(struct btree_trans *trans, u32 subvolid_to_delete)
395653693beSKent Overstreet {
396653693beSKent Overstreet struct bch_subvolume s;
397653693beSKent Overstreet
398653693beSKent Overstreet return lockrestart_do(trans,
399653693beSKent Overstreet bch2_subvolume_get(trans, subvolid_to_delete, true,
4005dd8c60eSKent Overstreet BTREE_ITER_cached, &s)) ?:
401653693beSKent Overstreet for_each_btree_key_commit(trans, iter,
4025dd8c60eSKent Overstreet BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_prefetch, k,
403cb52d23eSKent Overstreet NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
404653693beSKent Overstreet bch2_subvolume_reparent(trans, &iter, k,
40564304aafSKent Overstreet subvolid_to_delete, le32_to_cpu(s.creation_parent)));
406653693beSKent Overstreet }
407653693beSKent Overstreet
4082027875bSKent Overstreet /*
4092027875bSKent Overstreet * Delete subvolume, mark snapshot ID as deleted, queue up snapshot
4102027875bSKent Overstreet * deletion/cleanup:
4112027875bSKent Overstreet */
__bch2_subvolume_delete(struct btree_trans * trans,u32 subvolid)412653693beSKent Overstreet static int __bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
41314b393eeSKent Overstreet {
41414b393eeSKent Overstreet struct btree_iter iter;
41514b393eeSKent Overstreet struct bkey_s_c_subvolume subvol;
41614b393eeSKent Overstreet u32 snapid;
41714b393eeSKent Overstreet int ret = 0;
41814b393eeSKent Overstreet
419bcb79a51SKent Overstreet subvol = bch2_bkey_get_iter_typed(trans, &iter,
420bcb79a51SKent Overstreet BTREE_ID_subvolumes, POS(0, subvolid),
4215dd8c60eSKent Overstreet BTREE_ITER_cached|BTREE_ITER_intent,
422bcb79a51SKent Overstreet subvolume);
423bcb79a51SKent Overstreet ret = bkey_err(subvol);
4241c59b483SKent Overstreet bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
4251c59b483SKent Overstreet "missing subvolume %u", subvolid);
42614b393eeSKent Overstreet if (ret)
427bcb79a51SKent Overstreet return ret;
42814b393eeSKent Overstreet
42914b393eeSKent Overstreet snapid = le32_to_cpu(subvol.v->snapshot);
43014b393eeSKent Overstreet
431b0b5bbf9SKent Overstreet ret = bch2_btree_delete_at(trans, &iter, 0) ?:
432b0b5bbf9SKent Overstreet bch2_snapshot_node_set_deleted(trans, snapid);
43314b393eeSKent Overstreet bch2_trans_iter_exit(trans, &iter);
43414b393eeSKent Overstreet return ret;
43514b393eeSKent Overstreet }
43614b393eeSKent Overstreet
bch2_subvolume_delete(struct btree_trans * trans,u32 subvolid)437653693beSKent Overstreet static int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)
438653693beSKent Overstreet {
439653693beSKent Overstreet return bch2_subvolumes_reparent(trans, subvolid) ?:
440cb52d23eSKent Overstreet commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
441653693beSKent Overstreet __bch2_subvolume_delete(trans, subvolid));
442653693beSKent Overstreet }
443653693beSKent Overstreet
bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct * work)44473bd774dSKent Overstreet static void bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct *work)
4452027875bSKent Overstreet {
4462027875bSKent Overstreet struct bch_fs *c = container_of(work, struct bch_fs,
4472027875bSKent Overstreet snapshot_wait_for_pagecache_and_delete_work);
44891d961baSKent Overstreet snapshot_id_list s;
4492027875bSKent Overstreet u32 *id;
4502027875bSKent Overstreet int ret = 0;
4512027875bSKent Overstreet
4522027875bSKent Overstreet while (!ret) {
4532027875bSKent Overstreet mutex_lock(&c->snapshots_unlinked_lock);
4542027875bSKent Overstreet s = c->snapshots_unlinked;
45591d961baSKent Overstreet darray_init(&c->snapshots_unlinked);
4562027875bSKent Overstreet mutex_unlock(&c->snapshots_unlinked_lock);
4572027875bSKent Overstreet
4582027875bSKent Overstreet if (!s.nr)
4592027875bSKent Overstreet break;
4602027875bSKent Overstreet
4612027875bSKent Overstreet bch2_evict_subvolume_inodes(c, &s);
4622027875bSKent Overstreet
46391d961baSKent Overstreet for (id = s.data; id < s.data + s.nr; id++) {
4646bd68ec2SKent Overstreet ret = bch2_trans_run(c, bch2_subvolume_delete(trans, *id));
465e46c181aSKent Overstreet bch_err_msg(c, ret, "deleting subvolume %u", *id);
466cf904c8dSKent Overstreet if (ret)
4672027875bSKent Overstreet break;
4682027875bSKent Overstreet }
4692027875bSKent Overstreet
47091d961baSKent Overstreet darray_exit(&s);
4712027875bSKent Overstreet }
4722027875bSKent Overstreet
473d94189adSKent Overstreet bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
4742027875bSKent Overstreet }
4752027875bSKent Overstreet
4762027875bSKent Overstreet struct subvolume_unlink_hook {
4772027875bSKent Overstreet struct btree_trans_commit_hook h;
4782027875bSKent Overstreet u32 subvol;
4792027875bSKent Overstreet };
4802027875bSKent Overstreet
bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans * trans,struct btree_trans_commit_hook * _h)48173bd774dSKent Overstreet static int bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans *trans,
4822027875bSKent Overstreet struct btree_trans_commit_hook *_h)
4832027875bSKent Overstreet {
4842027875bSKent Overstreet struct subvolume_unlink_hook *h = container_of(_h, struct subvolume_unlink_hook, h);
4852027875bSKent Overstreet struct bch_fs *c = trans->c;
4862027875bSKent Overstreet int ret = 0;
4872027875bSKent Overstreet
4882027875bSKent Overstreet mutex_lock(&c->snapshots_unlinked_lock);
4892027875bSKent Overstreet if (!snapshot_list_has_id(&c->snapshots_unlinked, h->subvol))
490597dee1cSKent Overstreet ret = snapshot_list_add(c, &c->snapshots_unlinked, h->subvol);
4912027875bSKent Overstreet mutex_unlock(&c->snapshots_unlinked_lock);
4922027875bSKent Overstreet
4932027875bSKent Overstreet if (ret)
4942027875bSKent Overstreet return ret;
4952027875bSKent Overstreet
496d94189adSKent Overstreet if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_snapshot_delete_pagecache))
4972027875bSKent Overstreet return -EROFS;
4982027875bSKent Overstreet
4998bff9875SBrian Foster if (!queue_work(c->write_ref_wq, &c->snapshot_wait_for_pagecache_and_delete_work))
500d94189adSKent Overstreet bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);
5012027875bSKent Overstreet return 0;
5022027875bSKent Overstreet }
5032027875bSKent Overstreet
bch2_subvolume_unlink(struct btree_trans * trans,u32 subvolid)5042027875bSKent Overstreet int bch2_subvolume_unlink(struct btree_trans *trans, u32 subvolid)
5052027875bSKent Overstreet {
5062027875bSKent Overstreet struct btree_iter iter;
5072027875bSKent Overstreet struct bkey_i_subvolume *n;
5082027875bSKent Overstreet struct subvolume_unlink_hook *h;
5092027875bSKent Overstreet int ret = 0;
5102027875bSKent Overstreet
511f12a798aSKent Overstreet h = bch2_trans_kmalloc(trans, sizeof(*h));
512f12a798aSKent Overstreet ret = PTR_ERR_OR_ZERO(h);
513f12a798aSKent Overstreet if (ret)
514f12a798aSKent Overstreet return ret;
515f12a798aSKent Overstreet
516f12a798aSKent Overstreet h->h.fn = bch2_subvolume_wait_for_pagecache_and_delete_hook;
517f12a798aSKent Overstreet h->subvol = subvolid;
518f12a798aSKent Overstreet bch2_trans_commit_hook(trans, &h->h);
519f12a798aSKent Overstreet
52034dfa5dbSKent Overstreet n = bch2_bkey_get_mut_typed(trans, &iter,
52134dfa5dbSKent Overstreet BTREE_ID_subvolumes, POS(0, subvolid),
5225dd8c60eSKent Overstreet BTREE_ITER_cached, subvolume);
523994ba475SKent Overstreet ret = PTR_ERR_OR_ZERO(n);
524994ba475SKent Overstreet if (unlikely(ret)) {
5251c59b483SKent Overstreet bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,
5261c59b483SKent Overstreet "missing subvolume %u", subvolid);
527f12a798aSKent Overstreet return ret;
5282027875bSKent Overstreet }
5292027875bSKent Overstreet
5302027875bSKent Overstreet SET_BCH_SUBVOLUME_UNLINKED(&n->v, true);
5312027875bSKent Overstreet bch2_trans_iter_exit(trans, &iter);
5322027875bSKent Overstreet return ret;
5332027875bSKent Overstreet }
5342027875bSKent Overstreet
bch2_subvolume_create(struct btree_trans * trans,u64 inode,u32 parent_subvolid,u32 src_subvolid,u32 * new_subvolid,u32 * new_snapshotid,bool ro)53514b393eeSKent Overstreet int bch2_subvolume_create(struct btree_trans *trans, u64 inode,
536b8628a25SKent Overstreet u32 parent_subvolid,
53714b393eeSKent Overstreet u32 src_subvolid,
53814b393eeSKent Overstreet u32 *new_subvolid,
53914b393eeSKent Overstreet u32 *new_snapshotid,
54014b393eeSKent Overstreet bool ro)
54114b393eeSKent Overstreet {
542653693beSKent Overstreet struct bch_fs *c = trans->c;
54314b393eeSKent Overstreet struct btree_iter dst_iter, src_iter = (struct btree_iter) { NULL };
54414b393eeSKent Overstreet struct bkey_i_subvolume *new_subvol = NULL;
54514b393eeSKent Overstreet struct bkey_i_subvolume *src_subvol = NULL;
54614b393eeSKent Overstreet u32 parent = 0, new_nodes[2], snapshot_subvols[2];
54714b393eeSKent Overstreet int ret = 0;
54814b393eeSKent Overstreet
54951e84d3bSKent Overstreet ret = bch2_bkey_get_empty_slot(trans, &dst_iter,
55051e84d3bSKent Overstreet BTREE_ID_subvolumes, POS(0, U32_MAX));
55151e84d3bSKent Overstreet if (ret == -BCH_ERR_ENOSPC_btree_slot)
552098ef98dSKent Overstreet ret = -BCH_ERR_ENOSPC_subvolume_create;
55351e84d3bSKent Overstreet if (ret)
55451e84d3bSKent Overstreet return ret;
55551e84d3bSKent Overstreet
55614b393eeSKent Overstreet snapshot_subvols[0] = dst_iter.pos.offset;
55714b393eeSKent Overstreet snapshot_subvols[1] = src_subvolid;
55814b393eeSKent Overstreet
55914b393eeSKent Overstreet if (src_subvolid) {
56014b393eeSKent Overstreet /* Creating a snapshot: */
56114b393eeSKent Overstreet
56234dfa5dbSKent Overstreet src_subvol = bch2_bkey_get_mut_typed(trans, &src_iter,
56334dfa5dbSKent Overstreet BTREE_ID_subvolumes, POS(0, src_subvolid),
5645dd8c60eSKent Overstreet BTREE_ITER_cached, subvolume);
565994ba475SKent Overstreet ret = PTR_ERR_OR_ZERO(src_subvol);
566994ba475SKent Overstreet if (unlikely(ret)) {
567e47a390aSKent Overstreet bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,
568994ba475SKent Overstreet "subvolume %u not found", src_subvolid);
56914b393eeSKent Overstreet goto err;
57014b393eeSKent Overstreet }
57114b393eeSKent Overstreet
57214b393eeSKent Overstreet parent = le32_to_cpu(src_subvol->v.snapshot);
57314b393eeSKent Overstreet }
57414b393eeSKent Overstreet
57514b393eeSKent Overstreet ret = bch2_snapshot_node_create(trans, parent, new_nodes,
57614b393eeSKent Overstreet snapshot_subvols,
57714b393eeSKent Overstreet src_subvolid ? 2 : 1);
57814b393eeSKent Overstreet if (ret)
57914b393eeSKent Overstreet goto err;
58014b393eeSKent Overstreet
58114b393eeSKent Overstreet if (src_subvolid) {
58214b393eeSKent Overstreet src_subvol->v.snapshot = cpu_to_le32(new_nodes[1]);
58394a3e1a6SKent Overstreet ret = bch2_trans_update(trans, &src_iter, &src_subvol->k_i, 0);
58494a3e1a6SKent Overstreet if (ret)
58594a3e1a6SKent Overstreet goto err;
58614b393eeSKent Overstreet }
58714b393eeSKent Overstreet
588f8cb35fdSKent Overstreet new_subvol = bch2_bkey_alloc(trans, &dst_iter, 0, subvolume);
58914b393eeSKent Overstreet ret = PTR_ERR_OR_ZERO(new_subvol);
59014b393eeSKent Overstreet if (ret)
59114b393eeSKent Overstreet goto err;
59214b393eeSKent Overstreet
59314b393eeSKent Overstreet new_subvol->v.flags = 0;
59414b393eeSKent Overstreet new_subvol->v.snapshot = cpu_to_le32(new_nodes[0]);
59514b393eeSKent Overstreet new_subvol->v.inode = cpu_to_le64(inode);
59664304aafSKent Overstreet new_subvol->v.creation_parent = cpu_to_le32(src_subvolid);
597b8628a25SKent Overstreet new_subvol->v.fs_path_parent = cpu_to_le32(parent_subvolid);
598653693beSKent Overstreet new_subvol->v.otime.lo = cpu_to_le64(bch2_current_time(c));
599653693beSKent Overstreet new_subvol->v.otime.hi = 0;
600653693beSKent Overstreet
60114b393eeSKent Overstreet SET_BCH_SUBVOLUME_RO(&new_subvol->v, ro);
60214b393eeSKent Overstreet SET_BCH_SUBVOLUME_SNAP(&new_subvol->v, src_subvolid != 0);
60314b393eeSKent Overstreet
60414b393eeSKent Overstreet *new_subvolid = new_subvol->k.p.offset;
60514b393eeSKent Overstreet *new_snapshotid = new_nodes[0];
60614b393eeSKent Overstreet err:
60714b393eeSKent Overstreet bch2_trans_iter_exit(trans, &src_iter);
60814b393eeSKent Overstreet bch2_trans_iter_exit(trans, &dst_iter);
60914b393eeSKent Overstreet return ret;
61014b393eeSKent Overstreet }
61114b393eeSKent Overstreet
bch2_initialize_subvolumes(struct bch_fs * c)612d2554263SKent Overstreet int bch2_initialize_subvolumes(struct bch_fs *c)
613d2554263SKent Overstreet {
614d2554263SKent Overstreet struct bkey_i_snapshot_tree root_tree;
615d2554263SKent Overstreet struct bkey_i_snapshot root_snapshot;
616d2554263SKent Overstreet struct bkey_i_subvolume root_volume;
617d2554263SKent Overstreet int ret;
618d2554263SKent Overstreet
619d2554263SKent Overstreet bkey_snapshot_tree_init(&root_tree.k_i);
620d2554263SKent Overstreet root_tree.k.p.offset = 1;
621d2554263SKent Overstreet root_tree.v.master_subvol = cpu_to_le32(1);
622d2554263SKent Overstreet root_tree.v.root_snapshot = cpu_to_le32(U32_MAX);
623d2554263SKent Overstreet
624d2554263SKent Overstreet bkey_snapshot_init(&root_snapshot.k_i);
625d2554263SKent Overstreet root_snapshot.k.p.offset = U32_MAX;
626d2554263SKent Overstreet root_snapshot.v.flags = 0;
627d2554263SKent Overstreet root_snapshot.v.parent = 0;
628d2554263SKent Overstreet root_snapshot.v.subvol = cpu_to_le32(BCACHEFS_ROOT_SUBVOL);
629d2554263SKent Overstreet root_snapshot.v.tree = cpu_to_le32(1);
630d2554263SKent Overstreet SET_BCH_SNAPSHOT_SUBVOL(&root_snapshot.v, true);
631d2554263SKent Overstreet
632d2554263SKent Overstreet bkey_subvolume_init(&root_volume.k_i);
633d2554263SKent Overstreet root_volume.k.p.offset = BCACHEFS_ROOT_SUBVOL;
634d2554263SKent Overstreet root_volume.v.flags = 0;
635d2554263SKent Overstreet root_volume.v.snapshot = cpu_to_le32(U32_MAX);
636d2554263SKent Overstreet root_volume.v.inode = cpu_to_le64(BCACHEFS_ROOT_INO);
637d2554263SKent Overstreet
63849858d86SAriel Miculas ret = bch2_btree_insert(c, BTREE_ID_snapshot_trees, &root_tree.k_i, NULL, 0, 0) ?:
63949858d86SAriel Miculas bch2_btree_insert(c, BTREE_ID_snapshots, &root_snapshot.k_i, NULL, 0, 0) ?:
64049858d86SAriel Miculas bch2_btree_insert(c, BTREE_ID_subvolumes, &root_volume.k_i, NULL, 0, 0);
641d2554263SKent Overstreet bch_err_fn(c, ret);
642d2554263SKent Overstreet return ret;
643d2554263SKent Overstreet }
644d2554263SKent Overstreet
__bch2_fs_upgrade_for_subvolumes(struct btree_trans * trans)645d2554263SKent Overstreet static int __bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)
646d2554263SKent Overstreet {
647d2554263SKent Overstreet struct btree_iter iter;
648d2554263SKent Overstreet struct bkey_s_c k;
649d2554263SKent Overstreet struct bch_inode_unpacked inode;
650d2554263SKent Overstreet int ret;
651d2554263SKent Overstreet
652d2554263SKent Overstreet k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,
653d2554263SKent Overstreet SPOS(0, BCACHEFS_ROOT_INO, U32_MAX), 0);
654d2554263SKent Overstreet ret = bkey_err(k);
655d2554263SKent Overstreet if (ret)
656d2554263SKent Overstreet return ret;
657d2554263SKent Overstreet
658d2554263SKent Overstreet if (!bkey_is_inode(k.k)) {
659d2554263SKent Overstreet bch_err(trans->c, "root inode not found");
660d2554263SKent Overstreet ret = -BCH_ERR_ENOENT_inode;
661d2554263SKent Overstreet goto err;
662d2554263SKent Overstreet }
663d2554263SKent Overstreet
664d2554263SKent Overstreet ret = bch2_inode_unpack(k, &inode);
665d2554263SKent Overstreet BUG_ON(ret);
666d2554263SKent Overstreet
667d2554263SKent Overstreet inode.bi_subvol = BCACHEFS_ROOT_SUBVOL;
668d2554263SKent Overstreet
669d2554263SKent Overstreet ret = bch2_inode_write(trans, &iter, &inode);
670d2554263SKent Overstreet err:
671d2554263SKent Overstreet bch2_trans_iter_exit(trans, &iter);
672d2554263SKent Overstreet return ret;
673d2554263SKent Overstreet }
674d2554263SKent Overstreet
675d2554263SKent Overstreet /* set bi_subvol on root inode */
bch2_fs_upgrade_for_subvolumes(struct bch_fs * c)676d2554263SKent Overstreet int bch2_fs_upgrade_for_subvolumes(struct bch_fs *c)
677d2554263SKent Overstreet {
678*a0d11feeSKent Overstreet int ret = bch2_trans_commit_do(c, NULL, NULL, BCH_TRANS_COMMIT_lazy_rw,
679d2554263SKent Overstreet __bch2_fs_upgrade_for_subvolumes(trans));
680d2554263SKent Overstreet bch_err_fn(c, ret);
681d2554263SKent Overstreet return ret;
682d2554263SKent Overstreet }
683d2554263SKent Overstreet
bch2_fs_subvolumes_init(struct bch_fs * c)68414b393eeSKent Overstreet int bch2_fs_subvolumes_init(struct bch_fs *c)
68514b393eeSKent Overstreet {
68614b393eeSKent Overstreet INIT_WORK(&c->snapshot_delete_work, bch2_delete_dead_snapshots_work);
6872027875bSKent Overstreet INIT_WORK(&c->snapshot_wait_for_pagecache_and_delete_work,
6882027875bSKent Overstreet bch2_subvolume_wait_for_pagecache_and_delete);
6892027875bSKent Overstreet mutex_init(&c->snapshots_unlinked_lock);
69014b393eeSKent Overstreet return 0;
69114b393eeSKent Overstreet }
692