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 164ab35c34SKent Overstreet static int check_subvol(struct btree_trans *trans, 176738dd19SKent Overstreet struct btree_iter *iter, 186738dd19SKent Overstreet struct bkey_s_c k) 194ab35c34SKent Overstreet { 201c59b483SKent Overstreet struct bch_fs *c = trans->c; 214ab35c34SKent Overstreet struct bkey_s_c_subvolume subvol; 2235f1a503SKent Overstreet struct bch_snapshot snapshot; 2335f1a503SKent Overstreet unsigned snapid; 241c59b483SKent Overstreet int ret = 0; 254ab35c34SKent Overstreet 264ab35c34SKent Overstreet if (k.k->type != KEY_TYPE_subvolume) 274ab35c34SKent Overstreet return 0; 284ab35c34SKent Overstreet 294ab35c34SKent Overstreet subvol = bkey_s_c_to_subvolume(k); 3035f1a503SKent Overstreet snapid = le32_to_cpu(subvol.v->snapshot); 318e877caaSKent Overstreet ret = bch2_snapshot_lookup(trans, snapid, &snapshot); 3235f1a503SKent Overstreet 331c59b483SKent Overstreet if (bch2_err_matches(ret, ENOENT)) 341c59b483SKent Overstreet bch_err(c, "subvolume %llu points to nonexistent snapshot %u", 3535f1a503SKent Overstreet k.k->p.offset, snapid); 3635f1a503SKent Overstreet if (ret) 3735f1a503SKent Overstreet return ret; 384ab35c34SKent Overstreet 394ab35c34SKent Overstreet if (BCH_SUBVOLUME_UNLINKED(subvol.v)) { 40653693beSKent Overstreet bch2_fs_lazy_rw(c); 41653693beSKent Overstreet 424ab35c34SKent Overstreet ret = bch2_subvolume_delete(trans, iter->pos.offset); 43653693beSKent Overstreet if (ret) 44e46c181aSKent Overstreet bch_err_msg(c, ret, "deleting subvolume %llu", iter->pos.offset); 45653693beSKent Overstreet return ret ?: -BCH_ERR_transaction_restart_nested; 464ab35c34SKent Overstreet } 474ab35c34SKent Overstreet 481c59b483SKent Overstreet if (!BCH_SUBVOLUME_SNAP(subvol.v)) { 491c59b483SKent Overstreet u32 snapshot_root = bch2_snapshot_root(c, le32_to_cpu(subvol.v->snapshot)); 508479938dSKent Overstreet u32 snapshot_tree; 511c59b483SKent Overstreet struct bch_snapshot_tree st; 521c59b483SKent Overstreet 538479938dSKent Overstreet rcu_read_lock(); 548479938dSKent Overstreet snapshot_tree = snapshot_t(c, snapshot_root)->tree; 558479938dSKent Overstreet rcu_read_unlock(); 568479938dSKent Overstreet 57cb1b479dSKent Overstreet ret = bch2_snapshot_tree_lookup(trans, snapshot_tree, &st); 581c59b483SKent Overstreet 591c59b483SKent Overstreet bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c, 601c59b483SKent Overstreet "%s: snapshot tree %u not found", __func__, snapshot_tree); 611c59b483SKent Overstreet 621c59b483SKent Overstreet if (ret) 631c59b483SKent Overstreet return ret; 641c59b483SKent Overstreet 651c59b483SKent Overstreet if (fsck_err_on(le32_to_cpu(st.master_subvol) != subvol.k->p.offset, c, 661c59b483SKent Overstreet "subvolume %llu is not set as snapshot but is not master subvolume", 671c59b483SKent Overstreet k.k->p.offset)) { 681c59b483SKent Overstreet struct bkey_i_subvolume *s = 690fb3355dSKent Overstreet bch2_bkey_make_mut_typed(trans, iter, &subvol.s_c, 0, subvolume); 701c59b483SKent Overstreet ret = PTR_ERR_OR_ZERO(s); 711c59b483SKent Overstreet if (ret) 721c59b483SKent Overstreet return ret; 731c59b483SKent Overstreet 741c59b483SKent Overstreet SET_BCH_SUBVOLUME_SNAP(&s->v, true); 751c59b483SKent Overstreet } 761c59b483SKent Overstreet } 771c59b483SKent Overstreet 781c59b483SKent Overstreet fsck_err: 791c59b483SKent Overstreet return ret; 804ab35c34SKent Overstreet } 814ab35c34SKent Overstreet 82067d228bSKent Overstreet int bch2_check_subvols(struct bch_fs *c) 834ab35c34SKent Overstreet { 844ab35c34SKent Overstreet struct btree_iter iter; 856738dd19SKent Overstreet struct bkey_s_c k; 864ab35c34SKent Overstreet int ret; 874ab35c34SKent Overstreet 881c59b483SKent Overstreet ret = bch2_trans_run(c, 896bd68ec2SKent Overstreet for_each_btree_key_commit(trans, iter, 906738dd19SKent Overstreet BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k, 916738dd19SKent Overstreet NULL, NULL, BTREE_INSERT_LAZY_RW|BTREE_INSERT_NOFAIL, 926bd68ec2SKent Overstreet check_subvol(trans, &iter, k))); 931c59b483SKent Overstreet if (ret) 941bb3c2a9SKent Overstreet bch_err_fn(c, ret); 954ab35c34SKent Overstreet return ret; 964ab35c34SKent Overstreet } 974ab35c34SKent Overstreet 9814b393eeSKent Overstreet /* Subvolumes: */ 9914b393eeSKent Overstreet 100f0ac7df2SKent Overstreet int bch2_subvolume_invalid(const struct bch_fs *c, struct bkey_s_c k, 1011f70225dSNathan Chancellor enum bkey_invalid_flags flags, struct printbuf *err) 10214b393eeSKent Overstreet { 103e88a75ebSKent Overstreet if (bkey_lt(k.k->p, SUBVOL_POS_MIN) || 104e88a75ebSKent Overstreet bkey_gt(k.k->p, SUBVOL_POS_MAX)) { 105401ec4dbSKent Overstreet prt_printf(err, "invalid pos"); 10678c0b75cSKent Overstreet return -BCH_ERR_invalid_bkey; 107f0ac7df2SKent Overstreet } 10814b393eeSKent Overstreet 109f0ac7df2SKent Overstreet return 0; 11014b393eeSKent Overstreet } 11114b393eeSKent Overstreet 11214b393eeSKent Overstreet void bch2_subvolume_to_text(struct printbuf *out, struct bch_fs *c, 11314b393eeSKent Overstreet struct bkey_s_c k) 11414b393eeSKent Overstreet { 11514b393eeSKent Overstreet struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k); 11614b393eeSKent Overstreet 117401ec4dbSKent Overstreet prt_printf(out, "root %llu snapshot id %u", 11814b393eeSKent Overstreet le64_to_cpu(s.v->inode), 11914b393eeSKent Overstreet le32_to_cpu(s.v->snapshot)); 120653693beSKent Overstreet 121653693beSKent Overstreet if (bkey_val_bytes(s.k) > offsetof(struct bch_subvolume, parent)) 122653693beSKent Overstreet prt_printf(out, " parent %u", le32_to_cpu(s.v->parent)); 12314b393eeSKent Overstreet } 12414b393eeSKent Overstreet 12598638ffaSKent Overstreet static __always_inline int 12698638ffaSKent Overstreet bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol, 12797996ddfSKent Overstreet bool inconsistent_if_not_found, 12897996ddfSKent Overstreet int iter_flags, 12997996ddfSKent Overstreet struct bch_subvolume *s) 13014b393eeSKent Overstreet { 131bcb79a51SKent Overstreet int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, subvol), 132bcb79a51SKent Overstreet iter_flags, subvolume, s); 1331c59b483SKent Overstreet bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT) && 1341c59b483SKent Overstreet inconsistent_if_not_found, 135bcb79a51SKent Overstreet trans->c, "missing subvolume %u", subvol); 13697996ddfSKent Overstreet return ret; 13714b393eeSKent Overstreet } 13814b393eeSKent Overstreet 13998638ffaSKent Overstreet int bch2_subvolume_get(struct btree_trans *trans, unsigned subvol, 14098638ffaSKent Overstreet bool inconsistent_if_not_found, 14198638ffaSKent Overstreet int iter_flags, 14298638ffaSKent Overstreet struct bch_subvolume *s) 14398638ffaSKent Overstreet { 14498638ffaSKent Overstreet return bch2_subvolume_get_inlined(trans, subvol, inconsistent_if_not_found, iter_flags, s); 14598638ffaSKent Overstreet } 14698638ffaSKent Overstreet 1479ca4853bSKent Overstreet int bch2_snapshot_get_subvol(struct btree_trans *trans, u32 snapshot, 1489ca4853bSKent Overstreet struct bch_subvolume *subvol) 1499ca4853bSKent Overstreet { 1509ca4853bSKent Overstreet struct bch_snapshot snap; 1519ca4853bSKent Overstreet 1528e877caaSKent Overstreet return bch2_snapshot_lookup(trans, snapshot, &snap) ?: 1539ca4853bSKent Overstreet bch2_subvolume_get(trans, le32_to_cpu(snap.subvol), true, 0, subvol); 1549ca4853bSKent Overstreet } 1559ca4853bSKent Overstreet 1568e877caaSKent Overstreet int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvolid, 15797996ddfSKent Overstreet u32 *snapid) 15897996ddfSKent Overstreet { 159653693beSKent Overstreet struct btree_iter iter; 1608e877caaSKent Overstreet struct bkey_s_c_subvolume subvol; 16197996ddfSKent Overstreet int ret; 16297996ddfSKent Overstreet 1638e877caaSKent Overstreet subvol = bch2_bkey_get_iter_typed(trans, &iter, 1648e877caaSKent Overstreet BTREE_ID_subvolumes, POS(0, subvolid), 1658e877caaSKent Overstreet BTREE_ITER_CACHED|BTREE_ITER_WITH_UPDATES, 1668e877caaSKent Overstreet subvolume); 1678e877caaSKent Overstreet ret = bkey_err(subvol); 1688e877caaSKent Overstreet bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c, 1698e877caaSKent Overstreet "missing subvolume %u", subvolid); 170653693beSKent Overstreet 171653693beSKent Overstreet if (likely(!ret)) 1728e877caaSKent Overstreet *snapid = le32_to_cpu(subvol.v->snapshot); 173653693beSKent Overstreet bch2_trans_iter_exit(trans, &iter); 17414b393eeSKent Overstreet return ret; 17514b393eeSKent Overstreet } 17614b393eeSKent Overstreet 177653693beSKent Overstreet static int bch2_subvolume_reparent(struct btree_trans *trans, 178653693beSKent Overstreet struct btree_iter *iter, 179653693beSKent Overstreet struct bkey_s_c k, 180653693beSKent Overstreet u32 old_parent, u32 new_parent) 181653693beSKent Overstreet { 182653693beSKent Overstreet struct bkey_i_subvolume *s; 183653693beSKent Overstreet int ret; 184653693beSKent Overstreet 185653693beSKent Overstreet if (k.k->type != KEY_TYPE_subvolume) 186653693beSKent Overstreet return 0; 187653693beSKent Overstreet 188653693beSKent Overstreet if (bkey_val_bytes(k.k) > offsetof(struct bch_subvolume, parent) && 189653693beSKent Overstreet le32_to_cpu(bkey_s_c_to_subvolume(k).v->parent) != old_parent) 190653693beSKent Overstreet return 0; 191653693beSKent Overstreet 1920fb3355dSKent Overstreet s = bch2_bkey_make_mut_typed(trans, iter, &k, 0, subvolume); 193653693beSKent Overstreet ret = PTR_ERR_OR_ZERO(s); 194653693beSKent Overstreet if (ret) 195653693beSKent Overstreet return ret; 196653693beSKent Overstreet 197653693beSKent Overstreet s->v.parent = cpu_to_le32(new_parent); 198653693beSKent Overstreet return 0; 199653693beSKent Overstreet } 200653693beSKent Overstreet 201653693beSKent Overstreet /* 2028e877caaSKent Overstreet * Separate from the snapshot tree in the snapshots btree, we record the tree 2038e877caaSKent Overstreet * structure of how snapshot subvolumes were created - the parent subvolume of 2048e877caaSKent Overstreet * each snapshot subvolume. 2058e877caaSKent Overstreet * 2068e877caaSKent Overstreet * When a subvolume is deleted, we scan for child subvolumes and reparant them, 2078e877caaSKent Overstreet * to avoid dangling references: 208653693beSKent Overstreet */ 209653693beSKent Overstreet static int bch2_subvolumes_reparent(struct btree_trans *trans, u32 subvolid_to_delete) 210653693beSKent Overstreet { 211653693beSKent Overstreet struct btree_iter iter; 212653693beSKent Overstreet struct bkey_s_c k; 213653693beSKent Overstreet struct bch_subvolume s; 214653693beSKent Overstreet 215653693beSKent Overstreet return lockrestart_do(trans, 216653693beSKent Overstreet bch2_subvolume_get(trans, subvolid_to_delete, true, 217653693beSKent Overstreet BTREE_ITER_CACHED, &s)) ?: 218653693beSKent Overstreet for_each_btree_key_commit(trans, iter, 219653693beSKent Overstreet BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_PREFETCH, k, 220653693beSKent Overstreet NULL, NULL, BTREE_INSERT_NOFAIL, 221653693beSKent Overstreet bch2_subvolume_reparent(trans, &iter, k, 222653693beSKent Overstreet subvolid_to_delete, le32_to_cpu(s.parent))); 223653693beSKent Overstreet } 224653693beSKent Overstreet 2252027875bSKent Overstreet /* 2262027875bSKent Overstreet * Delete subvolume, mark snapshot ID as deleted, queue up snapshot 2272027875bSKent Overstreet * deletion/cleanup: 2282027875bSKent Overstreet */ 229653693beSKent Overstreet static int __bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid) 23014b393eeSKent Overstreet { 23114b393eeSKent Overstreet struct btree_iter iter; 23214b393eeSKent Overstreet struct bkey_s_c_subvolume subvol; 23314b393eeSKent Overstreet u32 snapid; 23414b393eeSKent Overstreet int ret = 0; 23514b393eeSKent Overstreet 236bcb79a51SKent Overstreet subvol = bch2_bkey_get_iter_typed(trans, &iter, 237bcb79a51SKent Overstreet BTREE_ID_subvolumes, POS(0, subvolid), 238bcb79a51SKent Overstreet BTREE_ITER_CACHED|BTREE_ITER_INTENT, 239bcb79a51SKent Overstreet subvolume); 240bcb79a51SKent Overstreet ret = bkey_err(subvol); 2411c59b483SKent Overstreet bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c, 2421c59b483SKent Overstreet "missing subvolume %u", subvolid); 24314b393eeSKent Overstreet if (ret) 244bcb79a51SKent Overstreet return ret; 24514b393eeSKent Overstreet 24614b393eeSKent Overstreet snapid = le32_to_cpu(subvol.v->snapshot); 24714b393eeSKent Overstreet 248*b0b5bbf9SKent Overstreet ret = bch2_btree_delete_at(trans, &iter, 0) ?: 249*b0b5bbf9SKent Overstreet bch2_snapshot_node_set_deleted(trans, snapid); 25014b393eeSKent Overstreet bch2_trans_iter_exit(trans, &iter); 25114b393eeSKent Overstreet return ret; 25214b393eeSKent Overstreet } 25314b393eeSKent Overstreet 254653693beSKent Overstreet static int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid) 255653693beSKent Overstreet { 256653693beSKent Overstreet return bch2_subvolumes_reparent(trans, subvolid) ?: 257653693beSKent Overstreet commit_do(trans, NULL, NULL, BTREE_INSERT_NOFAIL, 258653693beSKent Overstreet __bch2_subvolume_delete(trans, subvolid)); 259653693beSKent Overstreet } 260653693beSKent Overstreet 26173bd774dSKent Overstreet static void bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct *work) 2622027875bSKent Overstreet { 2632027875bSKent Overstreet struct bch_fs *c = container_of(work, struct bch_fs, 2642027875bSKent Overstreet snapshot_wait_for_pagecache_and_delete_work); 26591d961baSKent Overstreet snapshot_id_list s; 2662027875bSKent Overstreet u32 *id; 2672027875bSKent Overstreet int ret = 0; 2682027875bSKent Overstreet 2692027875bSKent Overstreet while (!ret) { 2702027875bSKent Overstreet mutex_lock(&c->snapshots_unlinked_lock); 2712027875bSKent Overstreet s = c->snapshots_unlinked; 27291d961baSKent Overstreet darray_init(&c->snapshots_unlinked); 2732027875bSKent Overstreet mutex_unlock(&c->snapshots_unlinked_lock); 2742027875bSKent Overstreet 2752027875bSKent Overstreet if (!s.nr) 2762027875bSKent Overstreet break; 2772027875bSKent Overstreet 2782027875bSKent Overstreet bch2_evict_subvolume_inodes(c, &s); 2792027875bSKent Overstreet 28091d961baSKent Overstreet for (id = s.data; id < s.data + s.nr; id++) { 2816bd68ec2SKent Overstreet ret = bch2_trans_run(c, bch2_subvolume_delete(trans, *id)); 2822027875bSKent Overstreet if (ret) { 283e46c181aSKent Overstreet bch_err_msg(c, ret, "deleting subvolume %u", *id); 2842027875bSKent Overstreet break; 2852027875bSKent Overstreet } 2862027875bSKent Overstreet } 2872027875bSKent Overstreet 28891d961baSKent Overstreet darray_exit(&s); 2892027875bSKent Overstreet } 2902027875bSKent Overstreet 291d94189adSKent Overstreet bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache); 2922027875bSKent Overstreet } 2932027875bSKent Overstreet 2942027875bSKent Overstreet struct subvolume_unlink_hook { 2952027875bSKent Overstreet struct btree_trans_commit_hook h; 2962027875bSKent Overstreet u32 subvol; 2972027875bSKent Overstreet }; 2982027875bSKent Overstreet 29973bd774dSKent Overstreet static int bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans *trans, 3002027875bSKent Overstreet struct btree_trans_commit_hook *_h) 3012027875bSKent Overstreet { 3022027875bSKent Overstreet struct subvolume_unlink_hook *h = container_of(_h, struct subvolume_unlink_hook, h); 3032027875bSKent Overstreet struct bch_fs *c = trans->c; 3042027875bSKent Overstreet int ret = 0; 3052027875bSKent Overstreet 3062027875bSKent Overstreet mutex_lock(&c->snapshots_unlinked_lock); 3072027875bSKent Overstreet if (!snapshot_list_has_id(&c->snapshots_unlinked, h->subvol)) 308597dee1cSKent Overstreet ret = snapshot_list_add(c, &c->snapshots_unlinked, h->subvol); 3092027875bSKent Overstreet mutex_unlock(&c->snapshots_unlinked_lock); 3102027875bSKent Overstreet 3112027875bSKent Overstreet if (ret) 3122027875bSKent Overstreet return ret; 3132027875bSKent Overstreet 314d94189adSKent Overstreet if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_snapshot_delete_pagecache)) 3152027875bSKent Overstreet return -EROFS; 3162027875bSKent Overstreet 3178bff9875SBrian Foster if (!queue_work(c->write_ref_wq, &c->snapshot_wait_for_pagecache_and_delete_work)) 318d94189adSKent Overstreet bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache); 3192027875bSKent Overstreet return 0; 3202027875bSKent Overstreet } 3212027875bSKent Overstreet 3222027875bSKent Overstreet int bch2_subvolume_unlink(struct btree_trans *trans, u32 subvolid) 3232027875bSKent Overstreet { 3242027875bSKent Overstreet struct btree_iter iter; 3252027875bSKent Overstreet struct bkey_i_subvolume *n; 3262027875bSKent Overstreet struct subvolume_unlink_hook *h; 3272027875bSKent Overstreet int ret = 0; 3282027875bSKent Overstreet 329f12a798aSKent Overstreet h = bch2_trans_kmalloc(trans, sizeof(*h)); 330f12a798aSKent Overstreet ret = PTR_ERR_OR_ZERO(h); 331f12a798aSKent Overstreet if (ret) 332f12a798aSKent Overstreet return ret; 333f12a798aSKent Overstreet 334f12a798aSKent Overstreet h->h.fn = bch2_subvolume_wait_for_pagecache_and_delete_hook; 335f12a798aSKent Overstreet h->subvol = subvolid; 336f12a798aSKent Overstreet bch2_trans_commit_hook(trans, &h->h); 337f12a798aSKent Overstreet 33834dfa5dbSKent Overstreet n = bch2_bkey_get_mut_typed(trans, &iter, 33934dfa5dbSKent Overstreet BTREE_ID_subvolumes, POS(0, subvolid), 34034dfa5dbSKent Overstreet BTREE_ITER_CACHED, subvolume); 341994ba475SKent Overstreet ret = PTR_ERR_OR_ZERO(n); 342994ba475SKent Overstreet if (unlikely(ret)) { 3431c59b483SKent Overstreet bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c, 3441c59b483SKent Overstreet "missing subvolume %u", subvolid); 345f12a798aSKent Overstreet return ret; 3462027875bSKent Overstreet } 3472027875bSKent Overstreet 3482027875bSKent Overstreet SET_BCH_SUBVOLUME_UNLINKED(&n->v, true); 3492027875bSKent Overstreet bch2_trans_iter_exit(trans, &iter); 3502027875bSKent Overstreet return ret; 3512027875bSKent Overstreet } 3522027875bSKent Overstreet 35314b393eeSKent Overstreet int bch2_subvolume_create(struct btree_trans *trans, u64 inode, 35414b393eeSKent Overstreet u32 src_subvolid, 35514b393eeSKent Overstreet u32 *new_subvolid, 35614b393eeSKent Overstreet u32 *new_snapshotid, 35714b393eeSKent Overstreet bool ro) 35814b393eeSKent Overstreet { 359653693beSKent Overstreet struct bch_fs *c = trans->c; 36014b393eeSKent Overstreet struct btree_iter dst_iter, src_iter = (struct btree_iter) { NULL }; 36114b393eeSKent Overstreet struct bkey_i_subvolume *new_subvol = NULL; 36214b393eeSKent Overstreet struct bkey_i_subvolume *src_subvol = NULL; 36314b393eeSKent Overstreet u32 parent = 0, new_nodes[2], snapshot_subvols[2]; 36414b393eeSKent Overstreet int ret = 0; 36514b393eeSKent Overstreet 36651e84d3bSKent Overstreet ret = bch2_bkey_get_empty_slot(trans, &dst_iter, 36751e84d3bSKent Overstreet BTREE_ID_subvolumes, POS(0, U32_MAX)); 36851e84d3bSKent Overstreet if (ret == -BCH_ERR_ENOSPC_btree_slot) 369098ef98dSKent Overstreet ret = -BCH_ERR_ENOSPC_subvolume_create; 37051e84d3bSKent Overstreet if (ret) 37151e84d3bSKent Overstreet return ret; 37251e84d3bSKent Overstreet 37314b393eeSKent Overstreet snapshot_subvols[0] = dst_iter.pos.offset; 37414b393eeSKent Overstreet snapshot_subvols[1] = src_subvolid; 37514b393eeSKent Overstreet 37614b393eeSKent Overstreet if (src_subvolid) { 37714b393eeSKent Overstreet /* Creating a snapshot: */ 37814b393eeSKent Overstreet 37934dfa5dbSKent Overstreet src_subvol = bch2_bkey_get_mut_typed(trans, &src_iter, 38034dfa5dbSKent Overstreet BTREE_ID_subvolumes, POS(0, src_subvolid), 38134dfa5dbSKent Overstreet BTREE_ITER_CACHED, subvolume); 382994ba475SKent Overstreet ret = PTR_ERR_OR_ZERO(src_subvol); 383994ba475SKent Overstreet if (unlikely(ret)) { 384e47a390aSKent Overstreet bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c, 385994ba475SKent Overstreet "subvolume %u not found", src_subvolid); 38614b393eeSKent Overstreet goto err; 38714b393eeSKent Overstreet } 38814b393eeSKent Overstreet 38914b393eeSKent Overstreet parent = le32_to_cpu(src_subvol->v.snapshot); 39014b393eeSKent Overstreet } 39114b393eeSKent Overstreet 39214b393eeSKent Overstreet ret = bch2_snapshot_node_create(trans, parent, new_nodes, 39314b393eeSKent Overstreet snapshot_subvols, 39414b393eeSKent Overstreet src_subvolid ? 2 : 1); 39514b393eeSKent Overstreet if (ret) 39614b393eeSKent Overstreet goto err; 39714b393eeSKent Overstreet 39814b393eeSKent Overstreet if (src_subvolid) { 39914b393eeSKent Overstreet src_subvol->v.snapshot = cpu_to_le32(new_nodes[1]); 40094a3e1a6SKent Overstreet ret = bch2_trans_update(trans, &src_iter, &src_subvol->k_i, 0); 40194a3e1a6SKent Overstreet if (ret) 40294a3e1a6SKent Overstreet goto err; 40314b393eeSKent Overstreet } 40414b393eeSKent Overstreet 405f8cb35fdSKent Overstreet new_subvol = bch2_bkey_alloc(trans, &dst_iter, 0, subvolume); 40614b393eeSKent Overstreet ret = PTR_ERR_OR_ZERO(new_subvol); 40714b393eeSKent Overstreet if (ret) 40814b393eeSKent Overstreet goto err; 40914b393eeSKent Overstreet 41014b393eeSKent Overstreet new_subvol->v.flags = 0; 41114b393eeSKent Overstreet new_subvol->v.snapshot = cpu_to_le32(new_nodes[0]); 41214b393eeSKent Overstreet new_subvol->v.inode = cpu_to_le64(inode); 413653693beSKent Overstreet new_subvol->v.parent = cpu_to_le32(src_subvolid); 414653693beSKent Overstreet new_subvol->v.otime.lo = cpu_to_le64(bch2_current_time(c)); 415653693beSKent Overstreet new_subvol->v.otime.hi = 0; 416653693beSKent Overstreet 41714b393eeSKent Overstreet SET_BCH_SUBVOLUME_RO(&new_subvol->v, ro); 41814b393eeSKent Overstreet SET_BCH_SUBVOLUME_SNAP(&new_subvol->v, src_subvolid != 0); 41914b393eeSKent Overstreet 42014b393eeSKent Overstreet *new_subvolid = new_subvol->k.p.offset; 42114b393eeSKent Overstreet *new_snapshotid = new_nodes[0]; 42214b393eeSKent Overstreet err: 42314b393eeSKent Overstreet bch2_trans_iter_exit(trans, &src_iter); 42414b393eeSKent Overstreet bch2_trans_iter_exit(trans, &dst_iter); 42514b393eeSKent Overstreet return ret; 42614b393eeSKent Overstreet } 42714b393eeSKent Overstreet 42814b393eeSKent Overstreet int bch2_fs_subvolumes_init(struct bch_fs *c) 42914b393eeSKent Overstreet { 43014b393eeSKent Overstreet INIT_WORK(&c->snapshot_delete_work, bch2_delete_dead_snapshots_work); 4312027875bSKent Overstreet INIT_WORK(&c->snapshot_wait_for_pagecache_and_delete_work, 4322027875bSKent Overstreet bch2_subvolume_wait_for_pagecache_and_delete); 4332027875bSKent Overstreet mutex_init(&c->snapshots_unlinked_lock); 43414b393eeSKent Overstreet return 0; 43514b393eeSKent Overstreet } 436