1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * bcachefs setup/teardown code, and some metadata io - read a superblock and 4 * figure out what to do with it. 5 * 6 * Copyright 2010, 2011 Kent Overstreet <[email protected]> 7 * Copyright 2012 Google, Inc. 8 */ 9 10 #include "bcachefs.h" 11 #include "alloc_background.h" 12 #include "alloc_foreground.h" 13 #include "bkey_sort.h" 14 #include "btree_cache.h" 15 #include "btree_gc.h" 16 #include "btree_journal_iter.h" 17 #include "btree_key_cache.h" 18 #include "btree_node_scan.h" 19 #include "btree_update_interior.h" 20 #include "btree_io.h" 21 #include "btree_write_buffer.h" 22 #include "buckets_waiting_for_journal.h" 23 #include "chardev.h" 24 #include "checksum.h" 25 #include "clock.h" 26 #include "compress.h" 27 #include "debug.h" 28 #include "disk_accounting.h" 29 #include "disk_groups.h" 30 #include "ec.h" 31 #include "errcode.h" 32 #include "error.h" 33 #include "fs.h" 34 #include "fs-io.h" 35 #include "fs-io-buffered.h" 36 #include "fs-io-direct.h" 37 #include "fsck.h" 38 #include "inode.h" 39 #include "io_read.h" 40 #include "io_write.h" 41 #include "journal.h" 42 #include "journal_reclaim.h" 43 #include "journal_seq_blacklist.h" 44 #include "move.h" 45 #include "migrate.h" 46 #include "movinggc.h" 47 #include "nocow_locking.h" 48 #include "quota.h" 49 #include "rebalance.h" 50 #include "recovery.h" 51 #include "replicas.h" 52 #include "sb-clean.h" 53 #include "sb-counters.h" 54 #include "sb-errors.h" 55 #include "sb-members.h" 56 #include "snapshot.h" 57 #include "subvolume.h" 58 #include "super.h" 59 #include "super-io.h" 60 #include "sysfs.h" 61 #include "thread_with_file.h" 62 #include "trace.h" 63 64 #include <linux/backing-dev.h> 65 #include <linux/blkdev.h> 66 #include <linux/debugfs.h> 67 #include <linux/device.h> 68 #include <linux/idr.h> 69 #include <linux/module.h> 70 #include <linux/percpu.h> 71 #include <linux/random.h> 72 #include <linux/sysfs.h> 73 74 MODULE_LICENSE("GPL"); 75 MODULE_AUTHOR("Kent Overstreet <[email protected]>"); 76 MODULE_DESCRIPTION("bcachefs filesystem"); 77 78 const char * const bch2_fs_flag_strs[] = { 79 #define x(n) #n, 80 BCH_FS_FLAGS() 81 #undef x 82 NULL 83 }; 84 85 void bch2_print_str(struct bch_fs *c, const char *str) 86 { 87 #ifdef __KERNEL__ 88 struct stdio_redirect *stdio = bch2_fs_stdio_redirect(c); 89 90 if (unlikely(stdio)) { 91 bch2_stdio_redirect_printf(stdio, true, "%s", str); 92 return; 93 } 94 #endif 95 bch2_print_string_as_lines(KERN_ERR, str); 96 } 97 98 __printf(2, 0) 99 static void bch2_print_maybe_redirect(struct stdio_redirect *stdio, const char *fmt, va_list args) 100 { 101 #ifdef __KERNEL__ 102 if (unlikely(stdio)) { 103 if (fmt[0] == KERN_SOH[0]) 104 fmt += 2; 105 106 bch2_stdio_redirect_vprintf(stdio, true, fmt, args); 107 return; 108 } 109 #endif 110 vprintk(fmt, args); 111 } 112 113 void bch2_print_opts(struct bch_opts *opts, const char *fmt, ...) 114 { 115 struct stdio_redirect *stdio = (void *)(unsigned long)opts->stdio; 116 117 va_list args; 118 va_start(args, fmt); 119 bch2_print_maybe_redirect(stdio, fmt, args); 120 va_end(args); 121 } 122 123 void __bch2_print(struct bch_fs *c, const char *fmt, ...) 124 { 125 struct stdio_redirect *stdio = bch2_fs_stdio_redirect(c); 126 127 va_list args; 128 va_start(args, fmt); 129 bch2_print_maybe_redirect(stdio, fmt, args); 130 va_end(args); 131 } 132 133 #define KTYPE(type) \ 134 static const struct attribute_group type ## _group = { \ 135 .attrs = type ## _files \ 136 }; \ 137 \ 138 static const struct attribute_group *type ## _groups[] = { \ 139 &type ## _group, \ 140 NULL \ 141 }; \ 142 \ 143 static const struct kobj_type type ## _ktype = { \ 144 .release = type ## _release, \ 145 .sysfs_ops = &type ## _sysfs_ops, \ 146 .default_groups = type ## _groups \ 147 } 148 149 static void bch2_fs_release(struct kobject *); 150 static void bch2_dev_release(struct kobject *); 151 static void bch2_fs_counters_release(struct kobject *k) 152 { 153 } 154 155 static void bch2_fs_internal_release(struct kobject *k) 156 { 157 } 158 159 static void bch2_fs_opts_dir_release(struct kobject *k) 160 { 161 } 162 163 static void bch2_fs_time_stats_release(struct kobject *k) 164 { 165 } 166 167 KTYPE(bch2_fs); 168 KTYPE(bch2_fs_counters); 169 KTYPE(bch2_fs_internal); 170 KTYPE(bch2_fs_opts_dir); 171 KTYPE(bch2_fs_time_stats); 172 KTYPE(bch2_dev); 173 174 static struct kset *bcachefs_kset; 175 static LIST_HEAD(bch_fs_list); 176 static DEFINE_MUTEX(bch_fs_list_lock); 177 178 DECLARE_WAIT_QUEUE_HEAD(bch2_read_only_wait); 179 180 static void bch2_dev_unlink(struct bch_dev *); 181 static void bch2_dev_free(struct bch_dev *); 182 static int bch2_dev_alloc(struct bch_fs *, unsigned); 183 static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *); 184 static void bch2_dev_io_ref_stop(struct bch_dev *, int); 185 static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *); 186 187 struct bch_fs *bch2_dev_to_fs(dev_t dev) 188 { 189 struct bch_fs *c; 190 191 mutex_lock(&bch_fs_list_lock); 192 rcu_read_lock(); 193 194 list_for_each_entry(c, &bch_fs_list, list) 195 for_each_member_device_rcu(c, ca, NULL) 196 if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) { 197 closure_get(&c->cl); 198 goto found; 199 } 200 c = NULL; 201 found: 202 rcu_read_unlock(); 203 mutex_unlock(&bch_fs_list_lock); 204 205 return c; 206 } 207 208 static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid) 209 { 210 struct bch_fs *c; 211 212 lockdep_assert_held(&bch_fs_list_lock); 213 214 list_for_each_entry(c, &bch_fs_list, list) 215 if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid))) 216 return c; 217 218 return NULL; 219 } 220 221 struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid) 222 { 223 struct bch_fs *c; 224 225 mutex_lock(&bch_fs_list_lock); 226 c = __bch2_uuid_to_fs(uuid); 227 if (c) 228 closure_get(&c->cl); 229 mutex_unlock(&bch_fs_list_lock); 230 231 return c; 232 } 233 234 /* Filesystem RO/RW: */ 235 236 /* 237 * For startup/shutdown of RW stuff, the dependencies are: 238 * 239 * - foreground writes depend on copygc and rebalance (to free up space) 240 * 241 * - copygc and rebalance depend on mark and sweep gc (they actually probably 242 * don't because they either reserve ahead of time or don't block if 243 * allocations fail, but allocations can require mark and sweep gc to run 244 * because of generation number wraparound) 245 * 246 * - all of the above depends on the allocator threads 247 * 248 * - allocator depends on the journal (when it rewrites prios and gens) 249 */ 250 251 static void __bch2_fs_read_only(struct bch_fs *c) 252 { 253 unsigned clean_passes = 0; 254 u64 seq = 0; 255 256 bch2_fs_ec_stop(c); 257 bch2_open_buckets_stop(c, NULL, true); 258 bch2_rebalance_stop(c); 259 bch2_copygc_stop(c); 260 bch2_fs_ec_flush(c); 261 262 bch_verbose(c, "flushing journal and stopping allocators, journal seq %llu", 263 journal_cur_seq(&c->journal)); 264 265 do { 266 clean_passes++; 267 268 if (bch2_btree_interior_updates_flush(c) || 269 bch2_btree_write_buffer_flush_going_ro(c) || 270 bch2_journal_flush_all_pins(&c->journal) || 271 bch2_btree_flush_all_writes(c) || 272 seq != atomic64_read(&c->journal.seq)) { 273 seq = atomic64_read(&c->journal.seq); 274 clean_passes = 0; 275 } 276 } while (clean_passes < 2); 277 278 bch_verbose(c, "flushing journal and stopping allocators complete, journal seq %llu", 279 journal_cur_seq(&c->journal)); 280 281 if (test_bit(JOURNAL_replay_done, &c->journal.flags) && 282 !test_bit(BCH_FS_emergency_ro, &c->flags)) 283 set_bit(BCH_FS_clean_shutdown, &c->flags); 284 285 bch2_fs_journal_stop(&c->journal); 286 287 bch_info(c, "%sclean shutdown complete, journal seq %llu", 288 test_bit(BCH_FS_clean_shutdown, &c->flags) ? "" : "un", 289 c->journal.seq_ondisk); 290 291 /* 292 * After stopping journal: 293 */ 294 for_each_member_device(c, ca) { 295 bch2_dev_io_ref_stop(ca, WRITE); 296 bch2_dev_allocator_remove(c, ca); 297 } 298 } 299 300 #ifndef BCH_WRITE_REF_DEBUG 301 static void bch2_writes_disabled(struct percpu_ref *writes) 302 { 303 struct bch_fs *c = container_of(writes, struct bch_fs, writes); 304 305 set_bit(BCH_FS_write_disable_complete, &c->flags); 306 wake_up(&bch2_read_only_wait); 307 } 308 #endif 309 310 void bch2_fs_read_only(struct bch_fs *c) 311 { 312 if (!test_bit(BCH_FS_rw, &c->flags)) { 313 bch2_journal_reclaim_stop(&c->journal); 314 return; 315 } 316 317 BUG_ON(test_bit(BCH_FS_write_disable_complete, &c->flags)); 318 319 bch_verbose(c, "going read-only"); 320 321 /* 322 * Block new foreground-end write operations from starting - any new 323 * writes will return -EROFS: 324 */ 325 set_bit(BCH_FS_going_ro, &c->flags); 326 #ifndef BCH_WRITE_REF_DEBUG 327 percpu_ref_kill(&c->writes); 328 #else 329 for (unsigned i = 0; i < BCH_WRITE_REF_NR; i++) 330 bch2_write_ref_put(c, i); 331 #endif 332 333 /* 334 * If we're not doing an emergency shutdown, we want to wait on 335 * outstanding writes to complete so they don't see spurious errors due 336 * to shutting down the allocator: 337 * 338 * If we are doing an emergency shutdown outstanding writes may 339 * hang until we shutdown the allocator so we don't want to wait 340 * on outstanding writes before shutting everything down - but 341 * we do need to wait on them before returning and signalling 342 * that going RO is complete: 343 */ 344 wait_event(bch2_read_only_wait, 345 test_bit(BCH_FS_write_disable_complete, &c->flags) || 346 test_bit(BCH_FS_emergency_ro, &c->flags)); 347 348 bool writes_disabled = test_bit(BCH_FS_write_disable_complete, &c->flags); 349 if (writes_disabled) 350 bch_verbose(c, "finished waiting for writes to stop"); 351 352 __bch2_fs_read_only(c); 353 354 wait_event(bch2_read_only_wait, 355 test_bit(BCH_FS_write_disable_complete, &c->flags)); 356 357 if (!writes_disabled) 358 bch_verbose(c, "finished waiting for writes to stop"); 359 360 clear_bit(BCH_FS_write_disable_complete, &c->flags); 361 clear_bit(BCH_FS_going_ro, &c->flags); 362 clear_bit(BCH_FS_rw, &c->flags); 363 364 if (!bch2_journal_error(&c->journal) && 365 !test_bit(BCH_FS_error, &c->flags) && 366 !test_bit(BCH_FS_emergency_ro, &c->flags) && 367 test_bit(BCH_FS_started, &c->flags) && 368 test_bit(BCH_FS_clean_shutdown, &c->flags) && 369 c->recovery_pass_done >= BCH_RECOVERY_PASS_journal_replay) { 370 BUG_ON(c->journal.last_empty_seq != journal_cur_seq(&c->journal)); 371 BUG_ON(atomic_long_read(&c->btree_cache.nr_dirty)); 372 BUG_ON(atomic_long_read(&c->btree_key_cache.nr_dirty)); 373 BUG_ON(c->btree_write_buffer.inc.keys.nr); 374 BUG_ON(c->btree_write_buffer.flushing.keys.nr); 375 bch2_verify_accounting_clean(c); 376 377 bch_verbose(c, "marking filesystem clean"); 378 bch2_fs_mark_clean(c); 379 } else { 380 bch_verbose(c, "done going read-only, filesystem not clean"); 381 } 382 } 383 384 static void bch2_fs_read_only_work(struct work_struct *work) 385 { 386 struct bch_fs *c = 387 container_of(work, struct bch_fs, read_only_work); 388 389 down_write(&c->state_lock); 390 bch2_fs_read_only(c); 391 up_write(&c->state_lock); 392 } 393 394 static void bch2_fs_read_only_async(struct bch_fs *c) 395 { 396 queue_work(system_long_wq, &c->read_only_work); 397 } 398 399 bool bch2_fs_emergency_read_only(struct bch_fs *c) 400 { 401 bool ret = !test_and_set_bit(BCH_FS_emergency_ro, &c->flags); 402 403 bch2_journal_halt(&c->journal); 404 bch2_fs_read_only_async(c); 405 406 wake_up(&bch2_read_only_wait); 407 return ret; 408 } 409 410 bool bch2_fs_emergency_read_only_locked(struct bch_fs *c) 411 { 412 bool ret = !test_and_set_bit(BCH_FS_emergency_ro, &c->flags); 413 414 bch2_journal_halt_locked(&c->journal); 415 bch2_fs_read_only_async(c); 416 417 wake_up(&bch2_read_only_wait); 418 return ret; 419 } 420 421 static int __bch2_fs_read_write(struct bch_fs *c, bool early) 422 { 423 int ret; 424 425 BUG_ON(!test_bit(BCH_FS_may_go_rw, &c->flags)); 426 427 if (test_bit(BCH_FS_initial_gc_unfixed, &c->flags)) { 428 bch_err(c, "cannot go rw, unfixed btree errors"); 429 return -BCH_ERR_erofs_unfixed_errors; 430 } 431 432 if (test_bit(BCH_FS_rw, &c->flags)) 433 return 0; 434 435 bch_info(c, "going read-write"); 436 437 ret = bch2_sb_members_v2_init(c); 438 if (ret) 439 goto err; 440 441 clear_bit(BCH_FS_clean_shutdown, &c->flags); 442 443 __for_each_online_member(c, ca, BIT(BCH_MEMBER_STATE_rw), READ) { 444 bch2_dev_allocator_add(c, ca); 445 percpu_ref_reinit(&ca->io_ref[WRITE]); 446 } 447 bch2_recalc_capacity(c); 448 449 /* 450 * First journal write must be a flush write: after a clean shutdown we 451 * don't read the journal, so the first journal write may end up 452 * overwriting whatever was there previously, and there must always be 453 * at least one non-flush write in the journal or recovery will fail: 454 */ 455 spin_lock(&c->journal.lock); 456 set_bit(JOURNAL_need_flush_write, &c->journal.flags); 457 set_bit(JOURNAL_running, &c->journal.flags); 458 bch2_journal_space_available(&c->journal); 459 spin_unlock(&c->journal.lock); 460 461 ret = bch2_fs_mark_dirty(c); 462 if (ret) 463 goto err; 464 465 ret = bch2_journal_reclaim_start(&c->journal); 466 if (ret) 467 goto err; 468 469 set_bit(BCH_FS_rw, &c->flags); 470 set_bit(BCH_FS_was_rw, &c->flags); 471 472 #ifndef BCH_WRITE_REF_DEBUG 473 percpu_ref_reinit(&c->writes); 474 #else 475 for (unsigned i = 0; i < BCH_WRITE_REF_NR; i++) { 476 BUG_ON(atomic_long_read(&c->writes[i])); 477 atomic_long_inc(&c->writes[i]); 478 } 479 #endif 480 481 ret = bch2_copygc_start(c); 482 if (ret) { 483 bch_err_msg(c, ret, "error starting copygc thread"); 484 goto err; 485 } 486 487 ret = bch2_rebalance_start(c); 488 if (ret) { 489 bch_err_msg(c, ret, "error starting rebalance thread"); 490 goto err; 491 } 492 493 bch2_do_discards(c); 494 bch2_do_invalidates(c); 495 bch2_do_stripe_deletes(c); 496 bch2_do_pending_node_rewrites(c); 497 return 0; 498 err: 499 if (test_bit(BCH_FS_rw, &c->flags)) 500 bch2_fs_read_only(c); 501 else 502 __bch2_fs_read_only(c); 503 return ret; 504 } 505 506 int bch2_fs_read_write(struct bch_fs *c) 507 { 508 if (c->opts.recovery_pass_last && 509 c->opts.recovery_pass_last < BCH_RECOVERY_PASS_journal_replay) 510 return -BCH_ERR_erofs_norecovery; 511 512 if (c->opts.nochanges) 513 return -BCH_ERR_erofs_nochanges; 514 515 return __bch2_fs_read_write(c, false); 516 } 517 518 int bch2_fs_read_write_early(struct bch_fs *c) 519 { 520 down_write(&c->state_lock); 521 int ret = __bch2_fs_read_write(c, true); 522 up_write(&c->state_lock); 523 524 return ret; 525 } 526 527 /* Filesystem startup/shutdown: */ 528 529 static void __bch2_fs_free(struct bch_fs *c) 530 { 531 for (unsigned i = 0; i < BCH_TIME_STAT_NR; i++) 532 bch2_time_stats_exit(&c->times[i]); 533 534 bch2_find_btree_nodes_exit(&c->found_btree_nodes); 535 bch2_free_pending_node_rewrites(c); 536 bch2_free_fsck_errs(c); 537 bch2_fs_accounting_exit(c); 538 bch2_fs_sb_errors_exit(c); 539 bch2_fs_counters_exit(c); 540 bch2_fs_snapshots_exit(c); 541 bch2_fs_quota_exit(c); 542 bch2_fs_fs_io_direct_exit(c); 543 bch2_fs_fs_io_buffered_exit(c); 544 bch2_fs_fsio_exit(c); 545 bch2_fs_vfs_exit(c); 546 bch2_fs_ec_exit(c); 547 bch2_fs_encryption_exit(c); 548 bch2_fs_nocow_locking_exit(c); 549 bch2_fs_io_write_exit(c); 550 bch2_fs_io_read_exit(c); 551 bch2_fs_buckets_waiting_for_journal_exit(c); 552 bch2_fs_btree_interior_update_exit(c); 553 bch2_fs_btree_key_cache_exit(&c->btree_key_cache); 554 bch2_fs_btree_cache_exit(c); 555 bch2_fs_btree_iter_exit(c); 556 bch2_fs_replicas_exit(c); 557 bch2_fs_journal_exit(&c->journal); 558 bch2_io_clock_exit(&c->io_clock[WRITE]); 559 bch2_io_clock_exit(&c->io_clock[READ]); 560 bch2_fs_compress_exit(c); 561 bch2_fs_btree_gc_exit(c); 562 bch2_journal_keys_put_initial(c); 563 bch2_find_btree_nodes_exit(&c->found_btree_nodes); 564 BUG_ON(atomic_read(&c->journal_keys.ref)); 565 bch2_fs_btree_write_buffer_exit(c); 566 percpu_free_rwsem(&c->mark_lock); 567 if (c->online_reserved) { 568 u64 v = percpu_u64_get(c->online_reserved); 569 WARN(v, "online_reserved not 0 at shutdown: %lli", v); 570 free_percpu(c->online_reserved); 571 } 572 573 darray_exit(&c->incompat_versions_requested); 574 darray_exit(&c->btree_roots_extra); 575 free_percpu(c->pcpu); 576 free_percpu(c->usage); 577 mempool_exit(&c->large_bkey_pool); 578 mempool_exit(&c->btree_bounce_pool); 579 bioset_exit(&c->btree_bio); 580 mempool_exit(&c->fill_iter); 581 #ifndef BCH_WRITE_REF_DEBUG 582 percpu_ref_exit(&c->writes); 583 #endif 584 kfree(rcu_dereference_protected(c->disk_groups, 1)); 585 kfree(c->journal_seq_blacklist_table); 586 587 if (c->write_ref_wq) 588 destroy_workqueue(c->write_ref_wq); 589 if (c->btree_write_submit_wq) 590 destroy_workqueue(c->btree_write_submit_wq); 591 if (c->btree_read_complete_wq) 592 destroy_workqueue(c->btree_read_complete_wq); 593 if (c->copygc_wq) 594 destroy_workqueue(c->copygc_wq); 595 if (c->btree_io_complete_wq) 596 destroy_workqueue(c->btree_io_complete_wq); 597 if (c->btree_update_wq) 598 destroy_workqueue(c->btree_update_wq); 599 600 bch2_free_super(&c->disk_sb); 601 kvfree(c); 602 module_put(THIS_MODULE); 603 } 604 605 static void bch2_fs_release(struct kobject *kobj) 606 { 607 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); 608 609 __bch2_fs_free(c); 610 } 611 612 void __bch2_fs_stop(struct bch_fs *c) 613 { 614 bch_verbose(c, "shutting down"); 615 616 set_bit(BCH_FS_stopping, &c->flags); 617 618 down_write(&c->state_lock); 619 bch2_fs_read_only(c); 620 up_write(&c->state_lock); 621 622 for_each_member_device(c, ca) 623 bch2_dev_unlink(ca); 624 625 if (c->kobj.state_in_sysfs) 626 kobject_del(&c->kobj); 627 628 bch2_fs_debug_exit(c); 629 bch2_fs_chardev_exit(c); 630 631 bch2_ro_ref_put(c); 632 wait_event(c->ro_ref_wait, !refcount_read(&c->ro_ref)); 633 634 kobject_put(&c->counters_kobj); 635 kobject_put(&c->time_stats); 636 kobject_put(&c->opts_dir); 637 kobject_put(&c->internal); 638 639 /* btree prefetch might have kicked off reads in the background: */ 640 bch2_btree_flush_all_reads(c); 641 642 for_each_member_device(c, ca) 643 cancel_work_sync(&ca->io_error_work); 644 645 cancel_work_sync(&c->read_only_work); 646 } 647 648 void bch2_fs_free(struct bch_fs *c) 649 { 650 unsigned i; 651 652 mutex_lock(&bch_fs_list_lock); 653 list_del(&c->list); 654 mutex_unlock(&bch_fs_list_lock); 655 656 closure_sync(&c->cl); 657 closure_debug_destroy(&c->cl); 658 659 for (i = 0; i < c->sb.nr_devices; i++) { 660 struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true); 661 662 if (ca) { 663 EBUG_ON(atomic_long_read(&ca->ref) != 1); 664 bch2_dev_io_ref_stop(ca, READ); 665 bch2_free_super(&ca->disk_sb); 666 bch2_dev_free(ca); 667 } 668 } 669 670 bch_verbose(c, "shutdown complete"); 671 672 kobject_put(&c->kobj); 673 } 674 675 void bch2_fs_stop(struct bch_fs *c) 676 { 677 __bch2_fs_stop(c); 678 bch2_fs_free(c); 679 } 680 681 static int bch2_fs_online(struct bch_fs *c) 682 { 683 int ret = 0; 684 685 lockdep_assert_held(&bch_fs_list_lock); 686 687 if (__bch2_uuid_to_fs(c->sb.uuid)) { 688 bch_err(c, "filesystem UUID already open"); 689 return -EINVAL; 690 } 691 692 ret = bch2_fs_chardev_init(c); 693 if (ret) { 694 bch_err(c, "error creating character device"); 695 return ret; 696 } 697 698 bch2_fs_debug_init(c); 699 700 ret = kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ?: 701 kobject_add(&c->internal, &c->kobj, "internal") ?: 702 kobject_add(&c->opts_dir, &c->kobj, "options") ?: 703 #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT 704 kobject_add(&c->time_stats, &c->kobj, "time_stats") ?: 705 #endif 706 kobject_add(&c->counters_kobj, &c->kobj, "counters") ?: 707 bch2_opts_create_sysfs_files(&c->opts_dir, OPT_FS); 708 if (ret) { 709 bch_err(c, "error creating sysfs objects"); 710 return ret; 711 } 712 713 down_write(&c->state_lock); 714 715 for_each_member_device(c, ca) { 716 ret = bch2_dev_sysfs_online(c, ca); 717 if (ret) { 718 bch_err(c, "error creating sysfs objects"); 719 bch2_dev_put(ca); 720 goto err; 721 } 722 } 723 724 BUG_ON(!list_empty(&c->list)); 725 list_add(&c->list, &bch_fs_list); 726 err: 727 up_write(&c->state_lock); 728 return ret; 729 } 730 731 static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts) 732 { 733 struct bch_fs *c; 734 struct printbuf name = PRINTBUF; 735 unsigned i, iter_size; 736 int ret = 0; 737 738 c = kvmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO); 739 if (!c) { 740 c = ERR_PTR(-BCH_ERR_ENOMEM_fs_alloc); 741 goto out; 742 } 743 744 c->stdio = (void *)(unsigned long) opts.stdio; 745 746 __module_get(THIS_MODULE); 747 748 closure_init(&c->cl, NULL); 749 750 c->kobj.kset = bcachefs_kset; 751 kobject_init(&c->kobj, &bch2_fs_ktype); 752 kobject_init(&c->internal, &bch2_fs_internal_ktype); 753 kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype); 754 kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype); 755 kobject_init(&c->counters_kobj, &bch2_fs_counters_ktype); 756 757 c->minor = -1; 758 c->disk_sb.fs_sb = true; 759 760 init_rwsem(&c->state_lock); 761 mutex_init(&c->sb_lock); 762 mutex_init(&c->replicas_gc_lock); 763 mutex_init(&c->btree_root_lock); 764 INIT_WORK(&c->read_only_work, bch2_fs_read_only_work); 765 766 refcount_set(&c->ro_ref, 1); 767 init_waitqueue_head(&c->ro_ref_wait); 768 spin_lock_init(&c->recovery_pass_lock); 769 sema_init(&c->online_fsck_mutex, 1); 770 771 for (i = 0; i < BCH_TIME_STAT_NR; i++) 772 bch2_time_stats_init(&c->times[i]); 773 774 bch2_fs_copygc_init(c); 775 bch2_fs_btree_key_cache_init_early(&c->btree_key_cache); 776 bch2_fs_btree_iter_init_early(c); 777 bch2_fs_btree_interior_update_init_early(c); 778 bch2_fs_journal_keys_init(c); 779 bch2_fs_allocator_background_init(c); 780 bch2_fs_allocator_foreground_init(c); 781 bch2_fs_rebalance_init(c); 782 bch2_fs_quota_init(c); 783 bch2_fs_ec_init_early(c); 784 bch2_fs_move_init(c); 785 bch2_fs_sb_errors_init_early(c); 786 787 INIT_LIST_HEAD(&c->list); 788 789 mutex_init(&c->bio_bounce_pages_lock); 790 mutex_init(&c->snapshot_table_lock); 791 init_rwsem(&c->snapshot_create_lock); 792 793 spin_lock_init(&c->btree_write_error_lock); 794 795 INIT_LIST_HEAD(&c->journal_iters); 796 797 INIT_LIST_HEAD(&c->fsck_error_msgs); 798 mutex_init(&c->fsck_error_msgs_lock); 799 800 seqcount_init(&c->usage_lock); 801 802 sema_init(&c->io_in_flight, 128); 803 804 INIT_LIST_HEAD(&c->vfs_inodes_list); 805 mutex_init(&c->vfs_inodes_lock); 806 807 c->journal.flush_write_time = &c->times[BCH_TIME_journal_flush_write]; 808 c->journal.noflush_write_time = &c->times[BCH_TIME_journal_noflush_write]; 809 c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq]; 810 811 bch2_fs_btree_cache_init_early(&c->btree_cache); 812 813 mutex_init(&c->sectors_available_lock); 814 815 ret = percpu_init_rwsem(&c->mark_lock); 816 if (ret) 817 goto err; 818 819 mutex_lock(&c->sb_lock); 820 ret = bch2_sb_to_fs(c, sb); 821 mutex_unlock(&c->sb_lock); 822 823 if (ret) 824 goto err; 825 826 #ifdef CONFIG_UNICODE 827 /* Default encoding until we can potentially have more as an option. */ 828 c->cf_encoding = utf8_load(BCH_FS_DEFAULT_UTF8_ENCODING); 829 if (IS_ERR(c->cf_encoding)) { 830 printk(KERN_ERR "Cannot load UTF-8 encoding for filesystem. Version: %u.%u.%u", 831 unicode_major(BCH_FS_DEFAULT_UTF8_ENCODING), 832 unicode_minor(BCH_FS_DEFAULT_UTF8_ENCODING), 833 unicode_rev(BCH_FS_DEFAULT_UTF8_ENCODING)); 834 ret = -EINVAL; 835 goto err; 836 } 837 #else 838 if (c->sb.features & BIT_ULL(BCH_FEATURE_casefolding)) { 839 printk(KERN_ERR "Cannot mount a filesystem with casefolding on a kernel without CONFIG_UNICODE\n"); 840 ret = -EINVAL; 841 goto err; 842 } 843 #endif 844 845 pr_uuid(&name, c->sb.user_uuid.b); 846 ret = name.allocation_failure ? -BCH_ERR_ENOMEM_fs_name_alloc : 0; 847 if (ret) 848 goto err; 849 850 strscpy(c->name, name.buf, sizeof(c->name)); 851 printbuf_exit(&name); 852 853 /* Compat: */ 854 if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && 855 !BCH_SB_JOURNAL_FLUSH_DELAY(sb)) 856 SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000); 857 858 if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && 859 !BCH_SB_JOURNAL_RECLAIM_DELAY(sb)) 860 SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 100); 861 862 c->opts = bch2_opts_default; 863 ret = bch2_opts_from_sb(&c->opts, sb); 864 if (ret) 865 goto err; 866 867 bch2_opts_apply(&c->opts, opts); 868 869 c->btree_key_cache_btrees |= 1U << BTREE_ID_alloc; 870 if (c->opts.inodes_use_key_cache) 871 c->btree_key_cache_btrees |= 1U << BTREE_ID_inodes; 872 c->btree_key_cache_btrees |= 1U << BTREE_ID_logged_ops; 873 874 c->block_bits = ilog2(block_sectors(c)); 875 c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c); 876 877 if (bch2_fs_init_fault("fs_alloc")) { 878 bch_err(c, "fs_alloc fault injected"); 879 ret = -EFAULT; 880 goto err; 881 } 882 883 iter_size = sizeof(struct sort_iter) + 884 (btree_blocks(c) + 1) * 2 * 885 sizeof(struct sort_iter_set); 886 887 if (!(c->btree_update_wq = alloc_workqueue("bcachefs", 888 WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_UNBOUND, 512)) || 889 !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io", 890 WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) || 891 !(c->copygc_wq = alloc_workqueue("bcachefs_copygc", 892 WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) || 893 !(c->btree_read_complete_wq = alloc_workqueue("bcachefs_btree_read_complete", 894 WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM, 512)) || 895 !(c->btree_write_submit_wq = alloc_workqueue("bcachefs_btree_write_sumit", 896 WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) || 897 !(c->write_ref_wq = alloc_workqueue("bcachefs_write_ref", 898 WQ_FREEZABLE, 0)) || 899 #ifndef BCH_WRITE_REF_DEBUG 900 percpu_ref_init(&c->writes, bch2_writes_disabled, 901 PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 902 #endif 903 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) || 904 bioset_init(&c->btree_bio, 1, 905 max(offsetof(struct btree_read_bio, bio), 906 offsetof(struct btree_write_bio, wbio.bio)), 907 BIOSET_NEED_BVECS) || 908 !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) || 909 !(c->usage = alloc_percpu(struct bch_fs_usage_base)) || 910 !(c->online_reserved = alloc_percpu(u64)) || 911 mempool_init_kvmalloc_pool(&c->btree_bounce_pool, 1, 912 c->opts.btree_node_size) || 913 mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048)) { 914 ret = -BCH_ERR_ENOMEM_fs_other_alloc; 915 goto err; 916 } 917 918 ret = bch2_fs_counters_init(c) ?: 919 bch2_fs_sb_errors_init(c) ?: 920 bch2_io_clock_init(&c->io_clock[READ]) ?: 921 bch2_io_clock_init(&c->io_clock[WRITE]) ?: 922 bch2_fs_journal_init(&c->journal) ?: 923 bch2_fs_btree_iter_init(c) ?: 924 bch2_fs_btree_cache_init(c) ?: 925 bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?: 926 bch2_fs_btree_interior_update_init(c) ?: 927 bch2_fs_btree_gc_init(c) ?: 928 bch2_fs_buckets_waiting_for_journal_init(c) ?: 929 bch2_fs_btree_write_buffer_init(c) ?: 930 bch2_fs_subvolumes_init(c) ?: 931 bch2_fs_io_read_init(c) ?: 932 bch2_fs_io_write_init(c) ?: 933 bch2_fs_nocow_locking_init(c) ?: 934 bch2_fs_encryption_init(c) ?: 935 bch2_fs_compress_init(c) ?: 936 bch2_fs_ec_init(c) ?: 937 bch2_fs_vfs_init(c) ?: 938 bch2_fs_fsio_init(c) ?: 939 bch2_fs_fs_io_buffered_init(c) ?: 940 bch2_fs_fs_io_direct_init(c); 941 if (ret) 942 goto err; 943 944 for (i = 0; i < c->sb.nr_devices; i++) { 945 if (!bch2_member_exists(c->disk_sb.sb, i)) 946 continue; 947 ret = bch2_dev_alloc(c, i); 948 if (ret) 949 goto err; 950 } 951 952 bch2_journal_entry_res_resize(&c->journal, 953 &c->btree_root_journal_res, 954 BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX)); 955 bch2_journal_entry_res_resize(&c->journal, 956 &c->clock_journal_res, 957 (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2); 958 959 mutex_lock(&bch_fs_list_lock); 960 ret = bch2_fs_online(c); 961 mutex_unlock(&bch_fs_list_lock); 962 963 if (ret) 964 goto err; 965 out: 966 return c; 967 err: 968 bch2_fs_free(c); 969 c = ERR_PTR(ret); 970 goto out; 971 } 972 973 noinline_for_stack 974 static void print_mount_opts(struct bch_fs *c) 975 { 976 enum bch_opt_id i; 977 struct printbuf p = PRINTBUF; 978 bool first = true; 979 980 prt_str(&p, "starting version "); 981 bch2_version_to_text(&p, c->sb.version); 982 983 for (i = 0; i < bch2_opts_nr; i++) { 984 const struct bch_option *opt = &bch2_opt_table[i]; 985 u64 v = bch2_opt_get_by_id(&c->opts, i); 986 987 if (!(opt->flags & OPT_MOUNT)) 988 continue; 989 990 if (v == bch2_opt_get_by_id(&bch2_opts_default, i)) 991 continue; 992 993 prt_str(&p, first ? " opts=" : ","); 994 first = false; 995 bch2_opt_to_text(&p, c, c->disk_sb.sb, opt, v, OPT_SHOW_MOUNT_STYLE); 996 } 997 998 if (c->sb.version_incompat_allowed != c->sb.version) { 999 prt_printf(&p, "\n allowing incompatible features above "); 1000 bch2_version_to_text(&p, c->sb.version_incompat_allowed); 1001 } 1002 1003 bch_info(c, "%s", p.buf); 1004 printbuf_exit(&p); 1005 } 1006 1007 static bool bch2_fs_may_start(struct bch_fs *c) 1008 { 1009 struct bch_dev *ca; 1010 unsigned i, flags = 0; 1011 1012 if (c->opts.very_degraded) 1013 flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST; 1014 1015 if (c->opts.degraded) 1016 flags |= BCH_FORCE_IF_DEGRADED; 1017 1018 if (!c->opts.degraded && 1019 !c->opts.very_degraded) { 1020 mutex_lock(&c->sb_lock); 1021 1022 for (i = 0; i < c->disk_sb.sb->nr_devices; i++) { 1023 if (!bch2_member_exists(c->disk_sb.sb, i)) 1024 continue; 1025 1026 ca = bch2_dev_locked(c, i); 1027 1028 if (!bch2_dev_is_online(ca) && 1029 (ca->mi.state == BCH_MEMBER_STATE_rw || 1030 ca->mi.state == BCH_MEMBER_STATE_ro)) { 1031 mutex_unlock(&c->sb_lock); 1032 return false; 1033 } 1034 } 1035 mutex_unlock(&c->sb_lock); 1036 } 1037 1038 return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true); 1039 } 1040 1041 int bch2_fs_start(struct bch_fs *c) 1042 { 1043 time64_t now = ktime_get_real_seconds(); 1044 int ret = 0; 1045 1046 print_mount_opts(c); 1047 1048 if (!bch2_fs_may_start(c)) 1049 return -BCH_ERR_insufficient_devices_to_start; 1050 1051 down_write(&c->state_lock); 1052 mutex_lock(&c->sb_lock); 1053 1054 BUG_ON(test_bit(BCH_FS_started, &c->flags)); 1055 1056 if (!bch2_sb_field_get_minsize(&c->disk_sb, ext, 1057 sizeof(struct bch_sb_field_ext) / sizeof(u64))) { 1058 mutex_unlock(&c->sb_lock); 1059 up_write(&c->state_lock); 1060 ret = -BCH_ERR_ENOSPC_sb; 1061 goto err; 1062 } 1063 1064 ret = bch2_sb_members_v2_init(c); 1065 if (ret) { 1066 mutex_unlock(&c->sb_lock); 1067 up_write(&c->state_lock); 1068 goto err; 1069 } 1070 1071 for_each_online_member(c, ca) 1072 bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = cpu_to_le64(now); 1073 1074 mutex_unlock(&c->sb_lock); 1075 1076 for_each_rw_member(c, ca) 1077 bch2_dev_allocator_add(c, ca); 1078 bch2_recalc_capacity(c); 1079 up_write(&c->state_lock); 1080 1081 c->recovery_task = current; 1082 ret = BCH_SB_INITIALIZED(c->disk_sb.sb) 1083 ? bch2_fs_recovery(c) 1084 : bch2_fs_initialize(c); 1085 c->recovery_task = NULL; 1086 1087 if (ret) 1088 goto err; 1089 1090 ret = bch2_opts_check_may_set(c); 1091 if (ret) 1092 goto err; 1093 1094 if (bch2_fs_init_fault("fs_start")) { 1095 ret = -BCH_ERR_injected_fs_start; 1096 goto err; 1097 } 1098 1099 set_bit(BCH_FS_started, &c->flags); 1100 wake_up(&c->ro_ref_wait); 1101 1102 down_write(&c->state_lock); 1103 if (c->opts.read_only) 1104 bch2_fs_read_only(c); 1105 else if (!test_bit(BCH_FS_rw, &c->flags)) 1106 ret = bch2_fs_read_write(c); 1107 up_write(&c->state_lock); 1108 1109 err: 1110 if (ret) 1111 bch_err_msg(c, ret, "starting filesystem"); 1112 else 1113 bch_verbose(c, "done starting filesystem"); 1114 return ret; 1115 } 1116 1117 static int bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c) 1118 { 1119 struct bch_member m = bch2_sb_member_get(sb, sb->dev_idx); 1120 1121 if (le16_to_cpu(sb->block_size) != block_sectors(c)) 1122 return -BCH_ERR_mismatched_block_size; 1123 1124 if (le16_to_cpu(m.bucket_size) < 1125 BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb)) 1126 return -BCH_ERR_bucket_size_too_small; 1127 1128 return 0; 1129 } 1130 1131 static int bch2_dev_in_fs(struct bch_sb_handle *fs, 1132 struct bch_sb_handle *sb, 1133 struct bch_opts *opts) 1134 { 1135 if (fs == sb) 1136 return 0; 1137 1138 if (!uuid_equal(&fs->sb->uuid, &sb->sb->uuid)) 1139 return -BCH_ERR_device_not_a_member_of_filesystem; 1140 1141 if (!bch2_member_exists(fs->sb, sb->sb->dev_idx)) 1142 return -BCH_ERR_device_has_been_removed; 1143 1144 if (fs->sb->block_size != sb->sb->block_size) 1145 return -BCH_ERR_mismatched_block_size; 1146 1147 if (le16_to_cpu(fs->sb->version) < bcachefs_metadata_version_member_seq || 1148 le16_to_cpu(sb->sb->version) < bcachefs_metadata_version_member_seq) 1149 return 0; 1150 1151 if (fs->sb->seq == sb->sb->seq && 1152 fs->sb->write_time != sb->sb->write_time) { 1153 struct printbuf buf = PRINTBUF; 1154 1155 prt_str(&buf, "Split brain detected between "); 1156 prt_bdevname(&buf, sb->bdev); 1157 prt_str(&buf, " and "); 1158 prt_bdevname(&buf, fs->bdev); 1159 prt_char(&buf, ':'); 1160 prt_newline(&buf); 1161 prt_printf(&buf, "seq=%llu but write_time different, got", le64_to_cpu(sb->sb->seq)); 1162 prt_newline(&buf); 1163 1164 prt_bdevname(&buf, fs->bdev); 1165 prt_char(&buf, ' '); 1166 bch2_prt_datetime(&buf, le64_to_cpu(fs->sb->write_time)); 1167 prt_newline(&buf); 1168 1169 prt_bdevname(&buf, sb->bdev); 1170 prt_char(&buf, ' '); 1171 bch2_prt_datetime(&buf, le64_to_cpu(sb->sb->write_time)); 1172 prt_newline(&buf); 1173 1174 if (!opts->no_splitbrain_check) 1175 prt_printf(&buf, "Not using older sb"); 1176 1177 pr_err("%s", buf.buf); 1178 printbuf_exit(&buf); 1179 1180 if (!opts->no_splitbrain_check) 1181 return -BCH_ERR_device_splitbrain; 1182 } 1183 1184 struct bch_member m = bch2_sb_member_get(fs->sb, sb->sb->dev_idx); 1185 u64 seq_from_fs = le64_to_cpu(m.seq); 1186 u64 seq_from_member = le64_to_cpu(sb->sb->seq); 1187 1188 if (seq_from_fs && seq_from_fs < seq_from_member) { 1189 struct printbuf buf = PRINTBUF; 1190 1191 prt_str(&buf, "Split brain detected between "); 1192 prt_bdevname(&buf, sb->bdev); 1193 prt_str(&buf, " and "); 1194 prt_bdevname(&buf, fs->bdev); 1195 prt_char(&buf, ':'); 1196 prt_newline(&buf); 1197 1198 prt_bdevname(&buf, fs->bdev); 1199 prt_str(&buf, " believes seq of "); 1200 prt_bdevname(&buf, sb->bdev); 1201 prt_printf(&buf, " to be %llu, but ", seq_from_fs); 1202 prt_bdevname(&buf, sb->bdev); 1203 prt_printf(&buf, " has %llu\n", seq_from_member); 1204 1205 if (!opts->no_splitbrain_check) { 1206 prt_str(&buf, "Not using "); 1207 prt_bdevname(&buf, sb->bdev); 1208 } 1209 1210 pr_err("%s", buf.buf); 1211 printbuf_exit(&buf); 1212 1213 if (!opts->no_splitbrain_check) 1214 return -BCH_ERR_device_splitbrain; 1215 } 1216 1217 return 0; 1218 } 1219 1220 /* Device startup/shutdown: */ 1221 1222 static void bch2_dev_io_ref_stop(struct bch_dev *ca, int rw) 1223 { 1224 if (!percpu_ref_is_zero(&ca->io_ref[rw])) { 1225 reinit_completion(&ca->io_ref_completion[rw]); 1226 percpu_ref_kill(&ca->io_ref[rw]); 1227 wait_for_completion(&ca->io_ref_completion[rw]); 1228 } 1229 } 1230 1231 static void bch2_dev_release(struct kobject *kobj) 1232 { 1233 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); 1234 1235 kfree(ca); 1236 } 1237 1238 static void bch2_dev_free(struct bch_dev *ca) 1239 { 1240 WARN_ON(!percpu_ref_is_zero(&ca->io_ref[WRITE])); 1241 WARN_ON(!percpu_ref_is_zero(&ca->io_ref[READ])); 1242 1243 cancel_work_sync(&ca->io_error_work); 1244 1245 bch2_dev_unlink(ca); 1246 1247 if (ca->kobj.state_in_sysfs) 1248 kobject_del(&ca->kobj); 1249 1250 bch2_free_super(&ca->disk_sb); 1251 bch2_dev_allocator_background_exit(ca); 1252 bch2_dev_journal_exit(ca); 1253 1254 free_percpu(ca->io_done); 1255 bch2_dev_buckets_free(ca); 1256 kfree(ca->sb_read_scratch); 1257 1258 bch2_time_stats_quantiles_exit(&ca->io_latency[WRITE]); 1259 bch2_time_stats_quantiles_exit(&ca->io_latency[READ]); 1260 1261 percpu_ref_exit(&ca->io_ref[WRITE]); 1262 percpu_ref_exit(&ca->io_ref[READ]); 1263 #ifndef CONFIG_BCACHEFS_DEBUG 1264 percpu_ref_exit(&ca->ref); 1265 #endif 1266 kobject_put(&ca->kobj); 1267 } 1268 1269 static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) 1270 { 1271 1272 lockdep_assert_held(&c->state_lock); 1273 1274 if (percpu_ref_is_zero(&ca->io_ref[READ])) 1275 return; 1276 1277 __bch2_dev_read_only(c, ca); 1278 1279 bch2_dev_io_ref_stop(ca, READ); 1280 1281 bch2_dev_unlink(ca); 1282 1283 bch2_free_super(&ca->disk_sb); 1284 bch2_dev_journal_exit(ca); 1285 } 1286 1287 #ifndef CONFIG_BCACHEFS_DEBUG 1288 static void bch2_dev_ref_complete(struct percpu_ref *ref) 1289 { 1290 struct bch_dev *ca = container_of(ref, struct bch_dev, ref); 1291 1292 complete(&ca->ref_completion); 1293 } 1294 #endif 1295 1296 static void bch2_dev_io_ref_read_complete(struct percpu_ref *ref) 1297 { 1298 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref[READ]); 1299 1300 complete(&ca->io_ref_completion[READ]); 1301 } 1302 1303 static void bch2_dev_io_ref_write_complete(struct percpu_ref *ref) 1304 { 1305 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref[WRITE]); 1306 1307 complete(&ca->io_ref_completion[WRITE]); 1308 } 1309 1310 static void bch2_dev_unlink(struct bch_dev *ca) 1311 { 1312 struct kobject *b; 1313 1314 /* 1315 * This is racy w.r.t. the underlying block device being hot-removed, 1316 * which removes it from sysfs. 1317 * 1318 * It'd be lovely if we had a way to handle this race, but the sysfs 1319 * code doesn't appear to provide a good method and block/holder.c is 1320 * susceptible as well: 1321 */ 1322 if (ca->kobj.state_in_sysfs && 1323 ca->disk_sb.bdev && 1324 (b = bdev_kobj(ca->disk_sb.bdev))->state_in_sysfs) { 1325 sysfs_remove_link(b, "bcachefs"); 1326 sysfs_remove_link(&ca->kobj, "block"); 1327 } 1328 } 1329 1330 static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) 1331 { 1332 int ret; 1333 1334 if (!c->kobj.state_in_sysfs) 1335 return 0; 1336 1337 if (!ca->kobj.state_in_sysfs) { 1338 ret = kobject_add(&ca->kobj, &c->kobj, "dev-%u", ca->dev_idx) ?: 1339 bch2_opts_create_sysfs_files(&ca->kobj, OPT_DEVICE); 1340 if (ret) 1341 return ret; 1342 } 1343 1344 if (ca->disk_sb.bdev) { 1345 struct kobject *block = bdev_kobj(ca->disk_sb.bdev); 1346 1347 ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); 1348 if (ret) 1349 return ret; 1350 1351 ret = sysfs_create_link(&ca->kobj, block, "block"); 1352 if (ret) 1353 return ret; 1354 } 1355 1356 return 0; 1357 } 1358 1359 static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c, 1360 struct bch_member *member) 1361 { 1362 struct bch_dev *ca; 1363 unsigned i; 1364 1365 ca = kzalloc(sizeof(*ca), GFP_KERNEL); 1366 if (!ca) 1367 return NULL; 1368 1369 kobject_init(&ca->kobj, &bch2_dev_ktype); 1370 init_completion(&ca->ref_completion); 1371 init_completion(&ca->io_ref_completion[READ]); 1372 init_completion(&ca->io_ref_completion[WRITE]); 1373 1374 INIT_WORK(&ca->io_error_work, bch2_io_error_work); 1375 1376 bch2_time_stats_quantiles_init(&ca->io_latency[READ]); 1377 bch2_time_stats_quantiles_init(&ca->io_latency[WRITE]); 1378 1379 ca->mi = bch2_mi_to_cpu(member); 1380 1381 for (i = 0; i < ARRAY_SIZE(member->errors); i++) 1382 atomic64_set(&ca->errors[i], le64_to_cpu(member->errors[i])); 1383 1384 ca->uuid = member->uuid; 1385 1386 ca->nr_btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE, 1387 ca->mi.bucket_size / btree_sectors(c)); 1388 1389 #ifndef CONFIG_BCACHEFS_DEBUG 1390 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, 0, GFP_KERNEL)) 1391 goto err; 1392 #else 1393 atomic_long_set(&ca->ref, 1); 1394 #endif 1395 1396 bch2_dev_allocator_background_init(ca); 1397 1398 if (percpu_ref_init(&ca->io_ref[READ], bch2_dev_io_ref_read_complete, 1399 PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 1400 percpu_ref_init(&ca->io_ref[WRITE], bch2_dev_io_ref_write_complete, 1401 PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 1402 !(ca->sb_read_scratch = kmalloc(BCH_SB_READ_SCRATCH_BUF_SIZE, GFP_KERNEL)) || 1403 bch2_dev_buckets_alloc(c, ca) || 1404 !(ca->io_done = alloc_percpu(*ca->io_done))) 1405 goto err; 1406 1407 return ca; 1408 err: 1409 bch2_dev_free(ca); 1410 return NULL; 1411 } 1412 1413 static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, 1414 unsigned dev_idx) 1415 { 1416 ca->dev_idx = dev_idx; 1417 __set_bit(ca->dev_idx, ca->self.d); 1418 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); 1419 1420 ca->fs = c; 1421 rcu_assign_pointer(c->devs[ca->dev_idx], ca); 1422 1423 if (bch2_dev_sysfs_online(c, ca)) 1424 pr_warn("error creating sysfs objects"); 1425 } 1426 1427 static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx) 1428 { 1429 struct bch_member member = bch2_sb_member_get(c->disk_sb.sb, dev_idx); 1430 struct bch_dev *ca = NULL; 1431 1432 if (bch2_fs_init_fault("dev_alloc")) 1433 goto err; 1434 1435 ca = __bch2_dev_alloc(c, &member); 1436 if (!ca) 1437 goto err; 1438 1439 ca->fs = c; 1440 1441 bch2_dev_attach(c, ca, dev_idx); 1442 return 0; 1443 err: 1444 return -BCH_ERR_ENOMEM_dev_alloc; 1445 } 1446 1447 static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb) 1448 { 1449 unsigned ret; 1450 1451 if (bch2_dev_is_online(ca)) { 1452 bch_err(ca, "already have device online in slot %u", 1453 sb->sb->dev_idx); 1454 return -BCH_ERR_device_already_online; 1455 } 1456 1457 if (get_capacity(sb->bdev->bd_disk) < 1458 ca->mi.bucket_size * ca->mi.nbuckets) { 1459 bch_err(ca, "cannot online: device too small"); 1460 return -BCH_ERR_device_size_too_small; 1461 } 1462 1463 BUG_ON(!percpu_ref_is_zero(&ca->io_ref[READ])); 1464 BUG_ON(!percpu_ref_is_zero(&ca->io_ref[WRITE])); 1465 1466 ret = bch2_dev_journal_init(ca, sb->sb); 1467 if (ret) 1468 return ret; 1469 1470 /* Commit: */ 1471 ca->disk_sb = *sb; 1472 memset(sb, 0, sizeof(*sb)); 1473 1474 /* 1475 * Stash pointer to the filesystem for blk_holder_ops - note that once 1476 * attached to a filesystem, we will always close the block device 1477 * before tearing down the filesystem object. 1478 */ 1479 ca->disk_sb.holder->c = ca->fs; 1480 1481 ca->dev = ca->disk_sb.bdev->bd_dev; 1482 1483 percpu_ref_reinit(&ca->io_ref[READ]); 1484 1485 return 0; 1486 } 1487 1488 static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb) 1489 { 1490 struct bch_dev *ca; 1491 int ret; 1492 1493 lockdep_assert_held(&c->state_lock); 1494 1495 if (le64_to_cpu(sb->sb->seq) > 1496 le64_to_cpu(c->disk_sb.sb->seq)) 1497 bch2_sb_to_fs(c, sb->sb); 1498 1499 BUG_ON(!bch2_dev_exists(c, sb->sb->dev_idx)); 1500 1501 ca = bch2_dev_locked(c, sb->sb->dev_idx); 1502 1503 ret = __bch2_dev_attach_bdev(ca, sb); 1504 if (ret) 1505 return ret; 1506 1507 bch2_dev_sysfs_online(c, ca); 1508 1509 struct printbuf name = PRINTBUF; 1510 prt_bdevname(&name, ca->disk_sb.bdev); 1511 1512 if (c->sb.nr_devices == 1) 1513 strscpy(c->name, name.buf, sizeof(c->name)); 1514 strscpy(ca->name, name.buf, sizeof(ca->name)); 1515 1516 printbuf_exit(&name); 1517 1518 bch2_rebalance_wakeup(c); 1519 return 0; 1520 } 1521 1522 /* Device management: */ 1523 1524 /* 1525 * Note: this function is also used by the error paths - when a particular 1526 * device sees an error, we call it to determine whether we can just set the 1527 * device RO, or - if this function returns false - we'll set the whole 1528 * filesystem RO: 1529 * 1530 * XXX: maybe we should be more explicit about whether we're changing state 1531 * because we got an error or what have you? 1532 */ 1533 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, 1534 enum bch_member_state new_state, int flags) 1535 { 1536 struct bch_devs_mask new_online_devs; 1537 int nr_rw = 0, required; 1538 1539 lockdep_assert_held(&c->state_lock); 1540 1541 switch (new_state) { 1542 case BCH_MEMBER_STATE_rw: 1543 return true; 1544 case BCH_MEMBER_STATE_ro: 1545 if (ca->mi.state != BCH_MEMBER_STATE_rw) 1546 return true; 1547 1548 /* do we have enough devices to write to? */ 1549 for_each_member_device(c, ca2) 1550 if (ca2 != ca) 1551 nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw; 1552 1553 required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED) 1554 ? c->opts.metadata_replicas 1555 : metadata_replicas_required(c), 1556 !(flags & BCH_FORCE_IF_DATA_DEGRADED) 1557 ? c->opts.data_replicas 1558 : data_replicas_required(c)); 1559 1560 return nr_rw >= required; 1561 case BCH_MEMBER_STATE_failed: 1562 case BCH_MEMBER_STATE_spare: 1563 if (ca->mi.state != BCH_MEMBER_STATE_rw && 1564 ca->mi.state != BCH_MEMBER_STATE_ro) 1565 return true; 1566 1567 /* do we have enough devices to read from? */ 1568 new_online_devs = bch2_online_devs(c); 1569 __clear_bit(ca->dev_idx, new_online_devs.d); 1570 1571 return bch2_have_enough_devs(c, new_online_devs, flags, false); 1572 default: 1573 BUG(); 1574 } 1575 } 1576 1577 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) 1578 { 1579 bch2_dev_io_ref_stop(ca, WRITE); 1580 1581 /* 1582 * The allocator thread itself allocates btree nodes, so stop it first: 1583 */ 1584 bch2_dev_allocator_remove(c, ca); 1585 bch2_recalc_capacity(c); 1586 bch2_dev_journal_stop(&c->journal, ca); 1587 } 1588 1589 static void __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) 1590 { 1591 lockdep_assert_held(&c->state_lock); 1592 1593 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw); 1594 1595 bch2_dev_allocator_add(c, ca); 1596 bch2_recalc_capacity(c); 1597 1598 if (percpu_ref_is_zero(&ca->io_ref[WRITE])) 1599 percpu_ref_reinit(&ca->io_ref[WRITE]); 1600 1601 bch2_dev_do_discards(ca); 1602 } 1603 1604 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 1605 enum bch_member_state new_state, int flags) 1606 { 1607 struct bch_member *m; 1608 int ret = 0; 1609 1610 if (ca->mi.state == new_state) 1611 return 0; 1612 1613 if (!bch2_dev_state_allowed(c, ca, new_state, flags)) 1614 return -BCH_ERR_device_state_not_allowed; 1615 1616 if (new_state != BCH_MEMBER_STATE_rw) 1617 __bch2_dev_read_only(c, ca); 1618 1619 bch_notice(ca, "%s", bch2_member_states[new_state]); 1620 1621 mutex_lock(&c->sb_lock); 1622 m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 1623 SET_BCH_MEMBER_STATE(m, new_state); 1624 bch2_write_super(c); 1625 mutex_unlock(&c->sb_lock); 1626 1627 if (new_state == BCH_MEMBER_STATE_rw) 1628 __bch2_dev_read_write(c, ca); 1629 1630 bch2_rebalance_wakeup(c); 1631 1632 return ret; 1633 } 1634 1635 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 1636 enum bch_member_state new_state, int flags) 1637 { 1638 int ret; 1639 1640 down_write(&c->state_lock); 1641 ret = __bch2_dev_set_state(c, ca, new_state, flags); 1642 up_write(&c->state_lock); 1643 1644 return ret; 1645 } 1646 1647 /* Device add/removal: */ 1648 1649 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) 1650 { 1651 struct bch_member *m; 1652 unsigned dev_idx = ca->dev_idx, data; 1653 int ret; 1654 1655 down_write(&c->state_lock); 1656 1657 /* 1658 * We consume a reference to ca->ref, regardless of whether we succeed 1659 * or fail: 1660 */ 1661 bch2_dev_put(ca); 1662 1663 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 1664 bch_err(ca, "Cannot remove without losing data"); 1665 ret = -BCH_ERR_device_state_not_allowed; 1666 goto err; 1667 } 1668 1669 __bch2_dev_read_only(c, ca); 1670 1671 ret = bch2_dev_data_drop(c, ca->dev_idx, flags); 1672 bch_err_msg(ca, ret, "bch2_dev_data_drop()"); 1673 if (ret) 1674 goto err; 1675 1676 ret = bch2_dev_remove_alloc(c, ca); 1677 bch_err_msg(ca, ret, "bch2_dev_remove_alloc()"); 1678 if (ret) 1679 goto err; 1680 1681 /* 1682 * We need to flush the entire journal to get rid of keys that reference 1683 * the device being removed before removing the superblock entry 1684 */ 1685 bch2_journal_flush_all_pins(&c->journal); 1686 1687 /* 1688 * this is really just needed for the bch2_replicas_gc_(start|end) 1689 * calls, and could be cleaned up: 1690 */ 1691 ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); 1692 bch_err_msg(ca, ret, "bch2_journal_flush_device_pins()"); 1693 if (ret) 1694 goto err; 1695 1696 ret = bch2_journal_flush(&c->journal); 1697 bch_err_msg(ca, ret, "bch2_journal_flush()"); 1698 if (ret) 1699 goto err; 1700 1701 ret = bch2_replicas_gc2(c); 1702 bch_err_msg(ca, ret, "bch2_replicas_gc2()"); 1703 if (ret) 1704 goto err; 1705 1706 data = bch2_dev_has_data(c, ca); 1707 if (data) { 1708 struct printbuf data_has = PRINTBUF; 1709 1710 prt_bitflags(&data_has, __bch2_data_types, data); 1711 bch_err(ca, "Remove failed, still has data (%s)", data_has.buf); 1712 printbuf_exit(&data_has); 1713 ret = -EBUSY; 1714 goto err; 1715 } 1716 1717 __bch2_dev_offline(c, ca); 1718 1719 mutex_lock(&c->sb_lock); 1720 rcu_assign_pointer(c->devs[ca->dev_idx], NULL); 1721 mutex_unlock(&c->sb_lock); 1722 1723 #ifndef CONFIG_BCACHEFS_DEBUG 1724 percpu_ref_kill(&ca->ref); 1725 #else 1726 ca->dying = true; 1727 bch2_dev_put(ca); 1728 #endif 1729 wait_for_completion(&ca->ref_completion); 1730 1731 bch2_dev_free(ca); 1732 1733 /* 1734 * Free this device's slot in the bch_member array - all pointers to 1735 * this device must be gone: 1736 */ 1737 mutex_lock(&c->sb_lock); 1738 m = bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx); 1739 memset(&m->uuid, 0, sizeof(m->uuid)); 1740 1741 bch2_write_super(c); 1742 1743 mutex_unlock(&c->sb_lock); 1744 up_write(&c->state_lock); 1745 return 0; 1746 err: 1747 if (test_bit(BCH_FS_rw, &c->flags) && 1748 ca->mi.state == BCH_MEMBER_STATE_rw && 1749 !percpu_ref_is_zero(&ca->io_ref[READ])) 1750 __bch2_dev_read_write(c, ca); 1751 up_write(&c->state_lock); 1752 return ret; 1753 } 1754 1755 /* Add new device to running filesystem: */ 1756 int bch2_dev_add(struct bch_fs *c, const char *path) 1757 { 1758 struct bch_opts opts = bch2_opts_empty(); 1759 struct bch_sb_handle sb; 1760 struct bch_dev *ca = NULL; 1761 struct printbuf errbuf = PRINTBUF; 1762 struct printbuf label = PRINTBUF; 1763 int ret; 1764 1765 ret = bch2_read_super(path, &opts, &sb); 1766 bch_err_msg(c, ret, "reading super"); 1767 if (ret) 1768 goto err; 1769 1770 struct bch_member dev_mi = bch2_sb_member_get(sb.sb, sb.sb->dev_idx); 1771 1772 if (BCH_MEMBER_GROUP(&dev_mi)) { 1773 bch2_disk_path_to_text_sb(&label, sb.sb, BCH_MEMBER_GROUP(&dev_mi) - 1); 1774 if (label.allocation_failure) { 1775 ret = -ENOMEM; 1776 goto err; 1777 } 1778 } 1779 1780 ret = bch2_dev_may_add(sb.sb, c); 1781 if (ret) 1782 goto err; 1783 1784 ca = __bch2_dev_alloc(c, &dev_mi); 1785 if (!ca) { 1786 ret = -ENOMEM; 1787 goto err; 1788 } 1789 1790 ret = __bch2_dev_attach_bdev(ca, &sb); 1791 if (ret) 1792 goto err; 1793 1794 down_write(&c->state_lock); 1795 mutex_lock(&c->sb_lock); 1796 1797 ret = bch2_sb_from_fs(c, ca); 1798 bch_err_msg(c, ret, "setting up new superblock"); 1799 if (ret) 1800 goto err_unlock; 1801 1802 if (dynamic_fault("bcachefs:add:no_slot")) 1803 goto err_unlock; 1804 1805 ret = bch2_sb_member_alloc(c); 1806 if (ret < 0) { 1807 bch_err_msg(c, ret, "setting up new superblock"); 1808 goto err_unlock; 1809 } 1810 unsigned dev_idx = ret; 1811 1812 /* success: */ 1813 1814 dev_mi.last_mount = cpu_to_le64(ktime_get_real_seconds()); 1815 *bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx) = dev_mi; 1816 1817 ca->disk_sb.sb->dev_idx = dev_idx; 1818 bch2_dev_attach(c, ca, dev_idx); 1819 1820 if (BCH_MEMBER_GROUP(&dev_mi)) { 1821 ret = __bch2_dev_group_set(c, ca, label.buf); 1822 bch_err_msg(c, ret, "creating new label"); 1823 if (ret) 1824 goto err_unlock; 1825 } 1826 1827 bch2_write_super(c); 1828 mutex_unlock(&c->sb_lock); 1829 1830 ret = bch2_dev_usage_init(ca, false); 1831 if (ret) 1832 goto err_late; 1833 1834 ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); 1835 bch_err_msg(ca, ret, "marking new superblock"); 1836 if (ret) 1837 goto err_late; 1838 1839 ret = bch2_fs_freespace_init(c); 1840 bch_err_msg(ca, ret, "initializing free space"); 1841 if (ret) 1842 goto err_late; 1843 1844 if (ca->mi.state == BCH_MEMBER_STATE_rw) 1845 __bch2_dev_read_write(c, ca); 1846 1847 ret = bch2_dev_journal_alloc(ca, false); 1848 bch_err_msg(c, ret, "allocating journal"); 1849 if (ret) 1850 goto err_late; 1851 1852 up_write(&c->state_lock); 1853 out: 1854 printbuf_exit(&label); 1855 printbuf_exit(&errbuf); 1856 bch_err_fn(c, ret); 1857 return ret; 1858 1859 err_unlock: 1860 mutex_unlock(&c->sb_lock); 1861 up_write(&c->state_lock); 1862 err: 1863 if (ca) 1864 bch2_dev_free(ca); 1865 bch2_free_super(&sb); 1866 goto out; 1867 err_late: 1868 up_write(&c->state_lock); 1869 ca = NULL; 1870 goto err; 1871 } 1872 1873 /* Hot add existing device to running filesystem: */ 1874 int bch2_dev_online(struct bch_fs *c, const char *path) 1875 { 1876 struct bch_opts opts = bch2_opts_empty(); 1877 struct bch_sb_handle sb = { NULL }; 1878 struct bch_dev *ca; 1879 unsigned dev_idx; 1880 int ret; 1881 1882 down_write(&c->state_lock); 1883 1884 ret = bch2_read_super(path, &opts, &sb); 1885 if (ret) { 1886 up_write(&c->state_lock); 1887 return ret; 1888 } 1889 1890 dev_idx = sb.sb->dev_idx; 1891 1892 ret = bch2_dev_in_fs(&c->disk_sb, &sb, &c->opts); 1893 bch_err_msg(c, ret, "bringing %s online", path); 1894 if (ret) 1895 goto err; 1896 1897 ret = bch2_dev_attach_bdev(c, &sb); 1898 if (ret) 1899 goto err; 1900 1901 ca = bch2_dev_locked(c, dev_idx); 1902 1903 ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); 1904 bch_err_msg(c, ret, "bringing %s online: error from bch2_trans_mark_dev_sb", path); 1905 if (ret) 1906 goto err; 1907 1908 if (ca->mi.state == BCH_MEMBER_STATE_rw) 1909 __bch2_dev_read_write(c, ca); 1910 1911 if (!ca->mi.freespace_initialized) { 1912 ret = bch2_dev_freespace_init(c, ca, 0, ca->mi.nbuckets); 1913 bch_err_msg(ca, ret, "initializing free space"); 1914 if (ret) 1915 goto err; 1916 } 1917 1918 if (!ca->journal.nr) { 1919 ret = bch2_dev_journal_alloc(ca, false); 1920 bch_err_msg(ca, ret, "allocating journal"); 1921 if (ret) 1922 goto err; 1923 } 1924 1925 mutex_lock(&c->sb_lock); 1926 bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = 1927 cpu_to_le64(ktime_get_real_seconds()); 1928 bch2_write_super(c); 1929 mutex_unlock(&c->sb_lock); 1930 1931 up_write(&c->state_lock); 1932 return 0; 1933 err: 1934 up_write(&c->state_lock); 1935 bch2_free_super(&sb); 1936 return ret; 1937 } 1938 1939 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) 1940 { 1941 down_write(&c->state_lock); 1942 1943 if (!bch2_dev_is_online(ca)) { 1944 bch_err(ca, "Already offline"); 1945 up_write(&c->state_lock); 1946 return 0; 1947 } 1948 1949 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 1950 bch_err(ca, "Cannot offline required disk"); 1951 up_write(&c->state_lock); 1952 return -BCH_ERR_device_state_not_allowed; 1953 } 1954 1955 __bch2_dev_offline(c, ca); 1956 1957 up_write(&c->state_lock); 1958 return 0; 1959 } 1960 1961 int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) 1962 { 1963 struct bch_member *m; 1964 u64 old_nbuckets; 1965 int ret = 0; 1966 1967 down_write(&c->state_lock); 1968 old_nbuckets = ca->mi.nbuckets; 1969 1970 if (nbuckets < ca->mi.nbuckets) { 1971 bch_err(ca, "Cannot shrink yet"); 1972 ret = -EINVAL; 1973 goto err; 1974 } 1975 1976 if (nbuckets > BCH_MEMBER_NBUCKETS_MAX) { 1977 bch_err(ca, "New device size too big (%llu greater than max %u)", 1978 nbuckets, BCH_MEMBER_NBUCKETS_MAX); 1979 ret = -BCH_ERR_device_size_too_big; 1980 goto err; 1981 } 1982 1983 if (bch2_dev_is_online(ca) && 1984 get_capacity(ca->disk_sb.bdev->bd_disk) < 1985 ca->mi.bucket_size * nbuckets) { 1986 bch_err(ca, "New size larger than device"); 1987 ret = -BCH_ERR_device_size_too_small; 1988 goto err; 1989 } 1990 1991 ret = bch2_dev_buckets_resize(c, ca, nbuckets); 1992 bch_err_msg(ca, ret, "resizing buckets"); 1993 if (ret) 1994 goto err; 1995 1996 ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); 1997 if (ret) 1998 goto err; 1999 2000 mutex_lock(&c->sb_lock); 2001 m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 2002 m->nbuckets = cpu_to_le64(nbuckets); 2003 2004 bch2_write_super(c); 2005 mutex_unlock(&c->sb_lock); 2006 2007 if (ca->mi.freespace_initialized) { 2008 u64 v[3] = { nbuckets - old_nbuckets, 0, 0 }; 2009 2010 ret = bch2_trans_commit_do(ca->fs, NULL, NULL, 0, 2011 bch2_disk_accounting_mod2(trans, false, v, dev_data_type, 2012 .dev = ca->dev_idx, 2013 .data_type = BCH_DATA_free)) ?: 2014 bch2_dev_freespace_init(c, ca, old_nbuckets, nbuckets); 2015 if (ret) 2016 goto err; 2017 } 2018 2019 bch2_recalc_capacity(c); 2020 err: 2021 up_write(&c->state_lock); 2022 return ret; 2023 } 2024 2025 /* return with ref on ca->ref: */ 2026 struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name) 2027 { 2028 if (!strncmp(name, "/dev/", strlen("/dev/"))) 2029 name += strlen("/dev/"); 2030 2031 for_each_member_device(c, ca) 2032 if (!strcmp(name, ca->name)) 2033 return ca; 2034 return ERR_PTR(-BCH_ERR_ENOENT_dev_not_found); 2035 } 2036 2037 /* blk_holder_ops: */ 2038 2039 static struct bch_fs *bdev_get_fs(struct block_device *bdev) 2040 __releases(&bdev->bd_holder_lock) 2041 { 2042 struct bch_sb_handle_holder *holder = bdev->bd_holder; 2043 struct bch_fs *c = holder->c; 2044 2045 if (c && !bch2_ro_ref_tryget(c)) 2046 c = NULL; 2047 2048 mutex_unlock(&bdev->bd_holder_lock); 2049 2050 if (c) 2051 wait_event(c->ro_ref_wait, test_bit(BCH_FS_started, &c->flags)); 2052 return c; 2053 } 2054 2055 /* returns with ref on ca->ref */ 2056 static struct bch_dev *bdev_to_bch_dev(struct bch_fs *c, struct block_device *bdev) 2057 { 2058 for_each_member_device(c, ca) 2059 if (ca->disk_sb.bdev == bdev) 2060 return ca; 2061 return NULL; 2062 } 2063 2064 static void bch2_fs_bdev_mark_dead(struct block_device *bdev, bool surprise) 2065 { 2066 struct bch_fs *c = bdev_get_fs(bdev); 2067 if (!c) 2068 return; 2069 2070 struct super_block *sb = c->vfs_sb; 2071 if (sb) { 2072 /* 2073 * Not necessary, c->ro_ref guards against the filesystem being 2074 * unmounted - we only take this to avoid a warning in 2075 * sync_filesystem: 2076 */ 2077 down_read(&sb->s_umount); 2078 } 2079 2080 down_write(&c->state_lock); 2081 struct bch_dev *ca = bdev_to_bch_dev(c, bdev); 2082 if (!ca) 2083 goto unlock; 2084 2085 if (bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, BCH_FORCE_IF_DEGRADED)) { 2086 __bch2_dev_offline(c, ca); 2087 } else { 2088 if (sb) { 2089 if (!surprise) 2090 sync_filesystem(sb); 2091 shrink_dcache_sb(sb); 2092 evict_inodes(sb); 2093 } 2094 2095 bch2_journal_flush(&c->journal); 2096 bch2_fs_emergency_read_only(c); 2097 } 2098 2099 bch2_dev_put(ca); 2100 unlock: 2101 if (sb) 2102 up_read(&sb->s_umount); 2103 up_write(&c->state_lock); 2104 bch2_ro_ref_put(c); 2105 } 2106 2107 static void bch2_fs_bdev_sync(struct block_device *bdev) 2108 { 2109 struct bch_fs *c = bdev_get_fs(bdev); 2110 if (!c) 2111 return; 2112 2113 struct super_block *sb = c->vfs_sb; 2114 if (sb) { 2115 /* 2116 * Not necessary, c->ro_ref guards against the filesystem being 2117 * unmounted - we only take this to avoid a warning in 2118 * sync_filesystem: 2119 */ 2120 down_read(&sb->s_umount); 2121 sync_filesystem(sb); 2122 up_read(&sb->s_umount); 2123 } 2124 2125 bch2_ro_ref_put(c); 2126 } 2127 2128 const struct blk_holder_ops bch2_sb_handle_bdev_ops = { 2129 .mark_dead = bch2_fs_bdev_mark_dead, 2130 .sync = bch2_fs_bdev_sync, 2131 }; 2132 2133 /* Filesystem open: */ 2134 2135 static inline int sb_cmp(struct bch_sb *l, struct bch_sb *r) 2136 { 2137 return cmp_int(le64_to_cpu(l->seq), le64_to_cpu(r->seq)) ?: 2138 cmp_int(le64_to_cpu(l->write_time), le64_to_cpu(r->write_time)); 2139 } 2140 2141 struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, 2142 struct bch_opts opts) 2143 { 2144 DARRAY(struct bch_sb_handle) sbs = { 0 }; 2145 struct bch_fs *c = NULL; 2146 struct bch_sb_handle *best = NULL; 2147 struct printbuf errbuf = PRINTBUF; 2148 int ret = 0; 2149 2150 if (!try_module_get(THIS_MODULE)) 2151 return ERR_PTR(-ENODEV); 2152 2153 if (!nr_devices) { 2154 ret = -EINVAL; 2155 goto err; 2156 } 2157 2158 ret = darray_make_room(&sbs, nr_devices); 2159 if (ret) 2160 goto err; 2161 2162 for (unsigned i = 0; i < nr_devices; i++) { 2163 struct bch_sb_handle sb = { NULL }; 2164 2165 ret = bch2_read_super(devices[i], &opts, &sb); 2166 if (ret) 2167 goto err; 2168 2169 BUG_ON(darray_push(&sbs, sb)); 2170 } 2171 2172 if (opts.nochanges && !opts.read_only) { 2173 ret = -BCH_ERR_erofs_nochanges; 2174 goto err_print; 2175 } 2176 2177 darray_for_each(sbs, sb) 2178 if (!best || sb_cmp(sb->sb, best->sb) > 0) 2179 best = sb; 2180 2181 darray_for_each_reverse(sbs, sb) { 2182 ret = bch2_dev_in_fs(best, sb, &opts); 2183 2184 if (ret == -BCH_ERR_device_has_been_removed || 2185 ret == -BCH_ERR_device_splitbrain) { 2186 bch2_free_super(sb); 2187 darray_remove_item(&sbs, sb); 2188 best -= best > sb; 2189 ret = 0; 2190 continue; 2191 } 2192 2193 if (ret) 2194 goto err_print; 2195 } 2196 2197 c = bch2_fs_alloc(best->sb, opts); 2198 ret = PTR_ERR_OR_ZERO(c); 2199 if (ret) 2200 goto err; 2201 2202 down_write(&c->state_lock); 2203 darray_for_each(sbs, sb) { 2204 ret = bch2_dev_attach_bdev(c, sb); 2205 if (ret) { 2206 up_write(&c->state_lock); 2207 goto err; 2208 } 2209 } 2210 up_write(&c->state_lock); 2211 2212 if (!c->opts.nostart) { 2213 ret = bch2_fs_start(c); 2214 if (ret) 2215 goto err; 2216 } 2217 out: 2218 darray_for_each(sbs, sb) 2219 bch2_free_super(sb); 2220 darray_exit(&sbs); 2221 printbuf_exit(&errbuf); 2222 module_put(THIS_MODULE); 2223 return c; 2224 err_print: 2225 pr_err("bch_fs_open err opening %s: %s", 2226 devices[0], bch2_err_str(ret)); 2227 err: 2228 if (!IS_ERR_OR_NULL(c)) 2229 bch2_fs_stop(c); 2230 c = ERR_PTR(ret); 2231 goto out; 2232 } 2233 2234 /* Global interfaces/init */ 2235 2236 static void bcachefs_exit(void) 2237 { 2238 bch2_debug_exit(); 2239 bch2_vfs_exit(); 2240 bch2_chardev_exit(); 2241 bch2_btree_key_cache_exit(); 2242 if (bcachefs_kset) 2243 kset_unregister(bcachefs_kset); 2244 } 2245 2246 static int __init bcachefs_init(void) 2247 { 2248 bch2_bkey_pack_test(); 2249 2250 if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) || 2251 bch2_btree_key_cache_init() || 2252 bch2_chardev_init() || 2253 bch2_vfs_init() || 2254 bch2_debug_init()) 2255 goto err; 2256 2257 return 0; 2258 err: 2259 bcachefs_exit(); 2260 return -ENOMEM; 2261 } 2262 2263 #define BCH_DEBUG_PARAM(name, description) \ 2264 bool bch2_##name; \ 2265 module_param_named(name, bch2_##name, bool, 0644); \ 2266 MODULE_PARM_DESC(name, description); 2267 BCH_DEBUG_PARAMS() 2268 #undef BCH_DEBUG_PARAM 2269 2270 __maybe_unused 2271 static unsigned bch2_metadata_version = bcachefs_metadata_version_current; 2272 module_param_named(version, bch2_metadata_version, uint, 0444); 2273 2274 module_exit(bcachefs_exit); 2275 module_init(bcachefs_init); 2276