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