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