692 lines · c
1// SPDX-License-Identifier: GPL-2.02 3#include "bcachefs.h"4#include "btree_key_cache.h"5#include "btree_update.h"6#include "errcode.h"7#include "error.h"8#include "fs.h"9#include "snapshot.h"10#include "subvolume.h"11 12#include <linux/random.h>13 14static int bch2_subvolume_delete(struct btree_trans *, u32);15 16static struct bpos subvolume_children_pos(struct bkey_s_c k)17{18 if (k.k->type != KEY_TYPE_subvolume)19 return POS_MIN;20 21 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);22 if (!s.v->fs_path_parent)23 return POS_MIN;24 return POS(le32_to_cpu(s.v->fs_path_parent), s.k->p.offset);25}26 27static int check_subvol(struct btree_trans *trans,28 struct btree_iter *iter,29 struct bkey_s_c k)30{31 struct bch_fs *c = trans->c;32 struct bkey_s_c_subvolume subvol;33 struct btree_iter subvol_children_iter = {};34 struct bch_snapshot snapshot;35 struct printbuf buf = PRINTBUF;36 unsigned snapid;37 int ret = 0;38 39 if (k.k->type != KEY_TYPE_subvolume)40 return 0;41 42 subvol = bkey_s_c_to_subvolume(k);43 snapid = le32_to_cpu(subvol.v->snapshot);44 ret = bch2_snapshot_lookup(trans, snapid, &snapshot);45 46 if (bch2_err_matches(ret, ENOENT))47 bch_err(c, "subvolume %llu points to nonexistent snapshot %u",48 k.k->p.offset, snapid);49 if (ret)50 return ret;51 52 if (BCH_SUBVOLUME_UNLINKED(subvol.v)) {53 ret = bch2_subvolume_delete(trans, iter->pos.offset);54 bch_err_msg(c, ret, "deleting subvolume %llu", iter->pos.offset);55 return ret ?: -BCH_ERR_transaction_restart_nested;56 }57 58 if (fsck_err_on(subvol.k->p.offset == BCACHEFS_ROOT_SUBVOL &&59 subvol.v->fs_path_parent,60 trans, subvol_root_fs_path_parent_nonzero,61 "root subvolume has nonzero fs_path_parent\n%s",62 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {63 struct bkey_i_subvolume *n =64 bch2_bkey_make_mut_typed(trans, iter, &subvol.s_c, 0, subvolume);65 ret = PTR_ERR_OR_ZERO(n);66 if (ret)67 goto err;68 69 n->v.fs_path_parent = 0;70 }71 72 if (subvol.v->fs_path_parent) {73 struct bpos pos = subvolume_children_pos(k);74 75 struct bkey_s_c subvol_children_k =76 bch2_bkey_get_iter(trans, &subvol_children_iter,77 BTREE_ID_subvolume_children, pos, 0);78 ret = bkey_err(subvol_children_k);79 if (ret)80 goto err;81 82 if (fsck_err_on(subvol_children_k.k->type != KEY_TYPE_set,83 trans, subvol_children_not_set,84 "subvolume not set in subvolume_children btree at %llu:%llu\n%s",85 pos.inode, pos.offset,86 (printbuf_reset(&buf),87 bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {88 ret = bch2_btree_bit_mod(trans, BTREE_ID_subvolume_children, pos, true);89 if (ret)90 goto err;91 }92 }93 94 struct bch_inode_unpacked inode;95 ret = bch2_inode_find_by_inum_nowarn_trans(trans,96 (subvol_inum) { k.k->p.offset, le64_to_cpu(subvol.v->inode) },97 &inode);98 if (!ret) {99 if (fsck_err_on(inode.bi_subvol != subvol.k->p.offset,100 trans, subvol_root_wrong_bi_subvol,101 "subvol root %llu:%u has wrong bi_subvol field: got %u, should be %llu",102 inode.bi_inum, inode.bi_snapshot,103 inode.bi_subvol, subvol.k->p.offset)) {104 inode.bi_subvol = subvol.k->p.offset;105 inode.bi_snapshot = le32_to_cpu(subvol.v->snapshot);106 ret = __bch2_fsck_write_inode(trans, &inode);107 if (ret)108 goto err;109 }110 } else if (bch2_err_matches(ret, ENOENT)) {111 if (fsck_err(trans, subvol_to_missing_root,112 "subvolume %llu points to missing subvolume root %llu:%u",113 k.k->p.offset, le64_to_cpu(subvol.v->inode),114 le32_to_cpu(subvol.v->snapshot))) {115 ret = bch2_subvolume_delete(trans, iter->pos.offset);116 bch_err_msg(c, ret, "deleting subvolume %llu", iter->pos.offset);117 ret = ret ?: -BCH_ERR_transaction_restart_nested;118 goto err;119 }120 } else {121 goto err;122 }123 124 if (!BCH_SUBVOLUME_SNAP(subvol.v)) {125 u32 snapshot_root = bch2_snapshot_root(c, le32_to_cpu(subvol.v->snapshot));126 u32 snapshot_tree;127 struct bch_snapshot_tree st;128 129 rcu_read_lock();130 snapshot_tree = snapshot_t(c, snapshot_root)->tree;131 rcu_read_unlock();132 133 ret = bch2_snapshot_tree_lookup(trans, snapshot_tree, &st);134 135 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,136 "%s: snapshot tree %u not found", __func__, snapshot_tree);137 138 if (ret)139 goto err;140 141 if (fsck_err_on(le32_to_cpu(st.master_subvol) != subvol.k->p.offset,142 trans, subvol_not_master_and_not_snapshot,143 "subvolume %llu is not set as snapshot but is not master subvolume",144 k.k->p.offset)) {145 struct bkey_i_subvolume *s =146 bch2_bkey_make_mut_typed(trans, iter, &subvol.s_c, 0, subvolume);147 ret = PTR_ERR_OR_ZERO(s);148 if (ret)149 goto err;150 151 SET_BCH_SUBVOLUME_SNAP(&s->v, true);152 }153 }154err:155fsck_err:156 bch2_trans_iter_exit(trans, &subvol_children_iter);157 printbuf_exit(&buf);158 return ret;159}160 161int bch2_check_subvols(struct bch_fs *c)162{163 int ret = bch2_trans_run(c,164 for_each_btree_key_commit(trans, iter,165 BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_prefetch, k,166 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,167 check_subvol(trans, &iter, k)));168 bch_err_fn(c, ret);169 return ret;170}171 172static int check_subvol_child(struct btree_trans *trans,173 struct btree_iter *child_iter,174 struct bkey_s_c child_k)175{176 struct bch_subvolume s;177 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, child_k.k->p.offset),178 0, subvolume, &s);179 if (ret && !bch2_err_matches(ret, ENOENT))180 return ret;181 182 if (fsck_err_on(ret ||183 le32_to_cpu(s.fs_path_parent) != child_k.k->p.inode,184 trans, subvol_children_bad,185 "incorrect entry in subvolume_children btree %llu:%llu",186 child_k.k->p.inode, child_k.k->p.offset)) {187 ret = bch2_btree_delete_at(trans, child_iter, 0);188 if (ret)189 goto err;190 }191err:192fsck_err:193 return ret;194}195 196int bch2_check_subvol_children(struct bch_fs *c)197{198 int ret = bch2_trans_run(c,199 for_each_btree_key_commit(trans, iter,200 BTREE_ID_subvolume_children, POS_MIN, BTREE_ITER_prefetch, k,201 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,202 check_subvol_child(trans, &iter, k)));203 bch_err_fn(c, ret);204 return 0;205}206 207/* Subvolumes: */208 209int bch2_subvolume_validate(struct bch_fs *c, struct bkey_s_c k,210 enum bch_validate_flags flags)211{212 struct bkey_s_c_subvolume subvol = bkey_s_c_to_subvolume(k);213 int ret = 0;214 215 bkey_fsck_err_on(bkey_lt(k.k->p, SUBVOL_POS_MIN) ||216 bkey_gt(k.k->p, SUBVOL_POS_MAX),217 c, subvol_pos_bad,218 "invalid pos");219 220 bkey_fsck_err_on(!subvol.v->snapshot,221 c, subvol_snapshot_bad,222 "invalid snapshot");223 224 bkey_fsck_err_on(!subvol.v->inode,225 c, subvol_inode_bad,226 "invalid inode");227fsck_err:228 return ret;229}230 231void bch2_subvolume_to_text(struct printbuf *out, struct bch_fs *c,232 struct bkey_s_c k)233{234 struct bkey_s_c_subvolume s = bkey_s_c_to_subvolume(k);235 236 prt_printf(out, "root %llu snapshot id %u",237 le64_to_cpu(s.v->inode),238 le32_to_cpu(s.v->snapshot));239 240 if (bkey_val_bytes(s.k) > offsetof(struct bch_subvolume, creation_parent)) {241 prt_printf(out, " creation_parent %u", le32_to_cpu(s.v->creation_parent));242 prt_printf(out, " fs_parent %u", le32_to_cpu(s.v->fs_path_parent));243 }244}245 246static int subvolume_children_mod(struct btree_trans *trans, struct bpos pos, bool set)247{248 return !bpos_eq(pos, POS_MIN)249 ? bch2_btree_bit_mod(trans, BTREE_ID_subvolume_children, pos, set)250 : 0;251}252 253int bch2_subvolume_trigger(struct btree_trans *trans,254 enum btree_id btree_id, unsigned level,255 struct bkey_s_c old, struct bkey_s new,256 enum btree_iter_update_trigger_flags flags)257{258 if (flags & BTREE_TRIGGER_transactional) {259 struct bpos children_pos_old = subvolume_children_pos(old);260 struct bpos children_pos_new = subvolume_children_pos(new.s_c);261 262 if (!bpos_eq(children_pos_old, children_pos_new)) {263 int ret = subvolume_children_mod(trans, children_pos_old, false) ?:264 subvolume_children_mod(trans, children_pos_new, true);265 if (ret)266 return ret;267 }268 }269 270 return 0;271}272 273int bch2_subvol_has_children(struct btree_trans *trans, u32 subvol)274{275 struct btree_iter iter;276 277 bch2_trans_iter_init(trans, &iter, BTREE_ID_subvolume_children, POS(subvol, 0), 0);278 struct bkey_s_c k = bch2_btree_iter_peek(&iter);279 bch2_trans_iter_exit(trans, &iter);280 281 return bkey_err(k) ?: k.k && k.k->p.inode == subvol282 ? -BCH_ERR_ENOTEMPTY_subvol_not_empty283 : 0;284}285 286static __always_inline int287bch2_subvolume_get_inlined(struct btree_trans *trans, unsigned subvol,288 bool inconsistent_if_not_found,289 int iter_flags,290 struct bch_subvolume *s)291{292 int ret = bch2_bkey_get_val_typed(trans, BTREE_ID_subvolumes, POS(0, subvol),293 iter_flags, subvolume, s);294 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT) &&295 inconsistent_if_not_found,296 trans->c, "missing subvolume %u", subvol);297 return ret;298}299 300int bch2_subvolume_get(struct btree_trans *trans, unsigned subvol,301 bool inconsistent_if_not_found,302 int iter_flags,303 struct bch_subvolume *s)304{305 return bch2_subvolume_get_inlined(trans, subvol, inconsistent_if_not_found, iter_flags, s);306}307 308int bch2_subvol_is_ro_trans(struct btree_trans *trans, u32 subvol)309{310 struct bch_subvolume s;311 int ret = bch2_subvolume_get_inlined(trans, subvol, true, 0, &s);312 if (ret)313 return ret;314 315 if (BCH_SUBVOLUME_RO(&s))316 return -EROFS;317 return 0;318}319 320int bch2_subvol_is_ro(struct bch_fs *c, u32 subvol)321{322 return bch2_trans_do(c, bch2_subvol_is_ro_trans(trans, subvol));323}324 325int bch2_snapshot_get_subvol(struct btree_trans *trans, u32 snapshot,326 struct bch_subvolume *subvol)327{328 struct bch_snapshot snap;329 330 return bch2_snapshot_lookup(trans, snapshot, &snap) ?:331 bch2_subvolume_get(trans, le32_to_cpu(snap.subvol), true, 0, subvol);332}333 334int __bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvolid,335 u32 *snapid, bool warn)336{337 struct btree_iter iter;338 struct bkey_s_c_subvolume subvol;339 int ret;340 341 subvol = bch2_bkey_get_iter_typed(trans, &iter,342 BTREE_ID_subvolumes, POS(0, subvolid),343 BTREE_ITER_cached|BTREE_ITER_with_updates,344 subvolume);345 ret = bkey_err(subvol);346 347 bch2_fs_inconsistent_on(warn && bch2_err_matches(ret, ENOENT), trans->c,348 "missing subvolume %u", subvolid);349 350 if (likely(!ret))351 *snapid = le32_to_cpu(subvol.v->snapshot);352 bch2_trans_iter_exit(trans, &iter);353 return ret;354}355 356int bch2_subvolume_get_snapshot(struct btree_trans *trans, u32 subvolid,357 u32 *snapid)358{359 return __bch2_subvolume_get_snapshot(trans, subvolid, snapid, true);360}361 362static int bch2_subvolume_reparent(struct btree_trans *trans,363 struct btree_iter *iter,364 struct bkey_s_c k,365 u32 old_parent, u32 new_parent)366{367 struct bkey_i_subvolume *s;368 int ret;369 370 if (k.k->type != KEY_TYPE_subvolume)371 return 0;372 373 if (bkey_val_bytes(k.k) > offsetof(struct bch_subvolume, creation_parent) &&374 le32_to_cpu(bkey_s_c_to_subvolume(k).v->creation_parent) != old_parent)375 return 0;376 377 s = bch2_bkey_make_mut_typed(trans, iter, &k, 0, subvolume);378 ret = PTR_ERR_OR_ZERO(s);379 if (ret)380 return ret;381 382 s->v.creation_parent = cpu_to_le32(new_parent);383 return 0;384}385 386/*387 * Separate from the snapshot tree in the snapshots btree, we record the tree388 * structure of how snapshot subvolumes were created - the parent subvolume of389 * each snapshot subvolume.390 *391 * When a subvolume is deleted, we scan for child subvolumes and reparant them,392 * to avoid dangling references:393 */394static int bch2_subvolumes_reparent(struct btree_trans *trans, u32 subvolid_to_delete)395{396 struct bch_subvolume s;397 398 return lockrestart_do(trans,399 bch2_subvolume_get(trans, subvolid_to_delete, true,400 BTREE_ITER_cached, &s)) ?:401 for_each_btree_key_commit(trans, iter,402 BTREE_ID_subvolumes, POS_MIN, BTREE_ITER_prefetch, k,403 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,404 bch2_subvolume_reparent(trans, &iter, k,405 subvolid_to_delete, le32_to_cpu(s.creation_parent)));406}407 408/*409 * Delete subvolume, mark snapshot ID as deleted, queue up snapshot410 * deletion/cleanup:411 */412static int __bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)413{414 struct btree_iter iter;415 struct bkey_s_c_subvolume subvol;416 u32 snapid;417 int ret = 0;418 419 subvol = bch2_bkey_get_iter_typed(trans, &iter,420 BTREE_ID_subvolumes, POS(0, subvolid),421 BTREE_ITER_cached|BTREE_ITER_intent,422 subvolume);423 ret = bkey_err(subvol);424 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,425 "missing subvolume %u", subvolid);426 if (ret)427 return ret;428 429 snapid = le32_to_cpu(subvol.v->snapshot);430 431 ret = bch2_btree_delete_at(trans, &iter, 0) ?:432 bch2_snapshot_node_set_deleted(trans, snapid);433 bch2_trans_iter_exit(trans, &iter);434 return ret;435}436 437static int bch2_subvolume_delete(struct btree_trans *trans, u32 subvolid)438{439 return bch2_subvolumes_reparent(trans, subvolid) ?:440 commit_do(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc,441 __bch2_subvolume_delete(trans, subvolid));442}443 444static void bch2_subvolume_wait_for_pagecache_and_delete(struct work_struct *work)445{446 struct bch_fs *c = container_of(work, struct bch_fs,447 snapshot_wait_for_pagecache_and_delete_work);448 snapshot_id_list s;449 u32 *id;450 int ret = 0;451 452 while (!ret) {453 mutex_lock(&c->snapshots_unlinked_lock);454 s = c->snapshots_unlinked;455 darray_init(&c->snapshots_unlinked);456 mutex_unlock(&c->snapshots_unlinked_lock);457 458 if (!s.nr)459 break;460 461 bch2_evict_subvolume_inodes(c, &s);462 463 for (id = s.data; id < s.data + s.nr; id++) {464 ret = bch2_trans_run(c, bch2_subvolume_delete(trans, *id));465 bch_err_msg(c, ret, "deleting subvolume %u", *id);466 if (ret)467 break;468 }469 470 darray_exit(&s);471 }472 473 bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);474}475 476struct subvolume_unlink_hook {477 struct btree_trans_commit_hook h;478 u32 subvol;479};480 481static int bch2_subvolume_wait_for_pagecache_and_delete_hook(struct btree_trans *trans,482 struct btree_trans_commit_hook *_h)483{484 struct subvolume_unlink_hook *h = container_of(_h, struct subvolume_unlink_hook, h);485 struct bch_fs *c = trans->c;486 int ret = 0;487 488 mutex_lock(&c->snapshots_unlinked_lock);489 if (!snapshot_list_has_id(&c->snapshots_unlinked, h->subvol))490 ret = snapshot_list_add(c, &c->snapshots_unlinked, h->subvol);491 mutex_unlock(&c->snapshots_unlinked_lock);492 493 if (ret)494 return ret;495 496 if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_snapshot_delete_pagecache))497 return -EROFS;498 499 if (!queue_work(c->write_ref_wq, &c->snapshot_wait_for_pagecache_and_delete_work))500 bch2_write_ref_put(c, BCH_WRITE_REF_snapshot_delete_pagecache);501 return 0;502}503 504int bch2_subvolume_unlink(struct btree_trans *trans, u32 subvolid)505{506 struct btree_iter iter;507 struct bkey_i_subvolume *n;508 struct subvolume_unlink_hook *h;509 int ret = 0;510 511 h = bch2_trans_kmalloc(trans, sizeof(*h));512 ret = PTR_ERR_OR_ZERO(h);513 if (ret)514 return ret;515 516 h->h.fn = bch2_subvolume_wait_for_pagecache_and_delete_hook;517 h->subvol = subvolid;518 bch2_trans_commit_hook(trans, &h->h);519 520 n = bch2_bkey_get_mut_typed(trans, &iter,521 BTREE_ID_subvolumes, POS(0, subvolid),522 BTREE_ITER_cached, subvolume);523 ret = PTR_ERR_OR_ZERO(n);524 if (unlikely(ret)) {525 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), trans->c,526 "missing subvolume %u", subvolid);527 return ret;528 }529 530 SET_BCH_SUBVOLUME_UNLINKED(&n->v, true);531 bch2_trans_iter_exit(trans, &iter);532 return ret;533}534 535int bch2_subvolume_create(struct btree_trans *trans, u64 inode,536 u32 parent_subvolid,537 u32 src_subvolid,538 u32 *new_subvolid,539 u32 *new_snapshotid,540 bool ro)541{542 struct bch_fs *c = trans->c;543 struct btree_iter dst_iter, src_iter = (struct btree_iter) { NULL };544 struct bkey_i_subvolume *new_subvol = NULL;545 struct bkey_i_subvolume *src_subvol = NULL;546 u32 parent = 0, new_nodes[2], snapshot_subvols[2];547 int ret = 0;548 549 ret = bch2_bkey_get_empty_slot(trans, &dst_iter,550 BTREE_ID_subvolumes, POS(0, U32_MAX));551 if (ret == -BCH_ERR_ENOSPC_btree_slot)552 ret = -BCH_ERR_ENOSPC_subvolume_create;553 if (ret)554 return ret;555 556 snapshot_subvols[0] = dst_iter.pos.offset;557 snapshot_subvols[1] = src_subvolid;558 559 if (src_subvolid) {560 /* Creating a snapshot: */561 562 src_subvol = bch2_bkey_get_mut_typed(trans, &src_iter,563 BTREE_ID_subvolumes, POS(0, src_subvolid),564 BTREE_ITER_cached, subvolume);565 ret = PTR_ERR_OR_ZERO(src_subvol);566 if (unlikely(ret)) {567 bch2_fs_inconsistent_on(bch2_err_matches(ret, ENOENT), c,568 "subvolume %u not found", src_subvolid);569 goto err;570 }571 572 parent = le32_to_cpu(src_subvol->v.snapshot);573 }574 575 ret = bch2_snapshot_node_create(trans, parent, new_nodes,576 snapshot_subvols,577 src_subvolid ? 2 : 1);578 if (ret)579 goto err;580 581 if (src_subvolid) {582 src_subvol->v.snapshot = cpu_to_le32(new_nodes[1]);583 ret = bch2_trans_update(trans, &src_iter, &src_subvol->k_i, 0);584 if (ret)585 goto err;586 }587 588 new_subvol = bch2_bkey_alloc(trans, &dst_iter, 0, subvolume);589 ret = PTR_ERR_OR_ZERO(new_subvol);590 if (ret)591 goto err;592 593 new_subvol->v.flags = 0;594 new_subvol->v.snapshot = cpu_to_le32(new_nodes[0]);595 new_subvol->v.inode = cpu_to_le64(inode);596 new_subvol->v.creation_parent = cpu_to_le32(src_subvolid);597 new_subvol->v.fs_path_parent = cpu_to_le32(parent_subvolid);598 new_subvol->v.otime.lo = cpu_to_le64(bch2_current_time(c));599 new_subvol->v.otime.hi = 0;600 601 SET_BCH_SUBVOLUME_RO(&new_subvol->v, ro);602 SET_BCH_SUBVOLUME_SNAP(&new_subvol->v, src_subvolid != 0);603 604 *new_subvolid = new_subvol->k.p.offset;605 *new_snapshotid = new_nodes[0];606err:607 bch2_trans_iter_exit(trans, &src_iter);608 bch2_trans_iter_exit(trans, &dst_iter);609 return ret;610}611 612int bch2_initialize_subvolumes(struct bch_fs *c)613{614 struct bkey_i_snapshot_tree root_tree;615 struct bkey_i_snapshot root_snapshot;616 struct bkey_i_subvolume root_volume;617 int ret;618 619 bkey_snapshot_tree_init(&root_tree.k_i);620 root_tree.k.p.offset = 1;621 root_tree.v.master_subvol = cpu_to_le32(1);622 root_tree.v.root_snapshot = cpu_to_le32(U32_MAX);623 624 bkey_snapshot_init(&root_snapshot.k_i);625 root_snapshot.k.p.offset = U32_MAX;626 root_snapshot.v.flags = 0;627 root_snapshot.v.parent = 0;628 root_snapshot.v.subvol = cpu_to_le32(BCACHEFS_ROOT_SUBVOL);629 root_snapshot.v.tree = cpu_to_le32(1);630 SET_BCH_SNAPSHOT_SUBVOL(&root_snapshot.v, true);631 632 bkey_subvolume_init(&root_volume.k_i);633 root_volume.k.p.offset = BCACHEFS_ROOT_SUBVOL;634 root_volume.v.flags = 0;635 root_volume.v.snapshot = cpu_to_le32(U32_MAX);636 root_volume.v.inode = cpu_to_le64(BCACHEFS_ROOT_INO);637 638 ret = bch2_btree_insert(c, BTREE_ID_snapshot_trees, &root_tree.k_i, NULL, 0, 0) ?:639 bch2_btree_insert(c, BTREE_ID_snapshots, &root_snapshot.k_i, NULL, 0, 0) ?:640 bch2_btree_insert(c, BTREE_ID_subvolumes, &root_volume.k_i, NULL, 0, 0);641 bch_err_fn(c, ret);642 return ret;643}644 645static int __bch2_fs_upgrade_for_subvolumes(struct btree_trans *trans)646{647 struct btree_iter iter;648 struct bkey_s_c k;649 struct bch_inode_unpacked inode;650 int ret;651 652 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_inodes,653 SPOS(0, BCACHEFS_ROOT_INO, U32_MAX), 0);654 ret = bkey_err(k);655 if (ret)656 return ret;657 658 if (!bkey_is_inode(k.k)) {659 bch_err(trans->c, "root inode not found");660 ret = -BCH_ERR_ENOENT_inode;661 goto err;662 }663 664 ret = bch2_inode_unpack(k, &inode);665 BUG_ON(ret);666 667 inode.bi_subvol = BCACHEFS_ROOT_SUBVOL;668 669 ret = bch2_inode_write(trans, &iter, &inode);670err:671 bch2_trans_iter_exit(trans, &iter);672 return ret;673}674 675/* set bi_subvol on root inode */676int bch2_fs_upgrade_for_subvolumes(struct bch_fs *c)677{678 int ret = bch2_trans_commit_do(c, NULL, NULL, BCH_TRANS_COMMIT_lazy_rw,679 __bch2_fs_upgrade_for_subvolumes(trans));680 bch_err_fn(c, ret);681 return ret;682}683 684int bch2_fs_subvolumes_init(struct bch_fs *c)685{686 INIT_WORK(&c->snapshot_delete_work, bch2_delete_dead_snapshots_work);687 INIT_WORK(&c->snapshot_wait_for_pagecache_and_delete_work,688 bch2_subvolume_wait_for_pagecache_and_delete);689 mutex_init(&c->snapshots_unlinked_lock);690 return 0;691}692