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