Lines Matching +full:- +full:c

1 // SPDX-License-Identifier: GPL-2.0
3 * bcachefs setup/teardown code, and some metadata io - read a superblock and
34 #include "fs-io.h"
35 #include "fs-io-buffered.h"
36 #include "fs-io-direct.h"
52 #include "sb-clean.h"
53 #include "sb-counters.h"
54 #include "sb-errors.h"
55 #include "sb-members.h"
59 #include "super-io.h"
64 #include <linux/backing-dev.h>
85 void bch2_print_str(struct bch_fs *c, const char *str) in bch2_print_str() argument
88 struct stdio_redirect *stdio = bch2_fs_stdio_redirect(c); in bch2_print_str()
115 struct stdio_redirect *stdio = (void *)(unsigned long)opts->stdio; in bch2_print_opts()
123 void __bch2_print(struct bch_fs *c, const char *fmt, ...) in __bch2_print() argument
125 struct stdio_redirect *stdio = bch2_fs_stdio_redirect(c); in __bch2_print()
189 struct bch_fs *c; in bch2_dev_to_fs() local
194 list_for_each_entry(c, &bch_fs_list, list) in bch2_dev_to_fs()
195 for_each_member_device_rcu(c, ca, NULL) in bch2_dev_to_fs()
196 if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) { in bch2_dev_to_fs()
197 closure_get(&c->cl); in bch2_dev_to_fs()
200 c = NULL; in bch2_dev_to_fs()
205 return c; in bch2_dev_to_fs()
210 struct bch_fs *c; in __bch2_uuid_to_fs() local
214 list_for_each_entry(c, &bch_fs_list, list) in __bch2_uuid_to_fs()
215 if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid))) in __bch2_uuid_to_fs()
216 return c; in __bch2_uuid_to_fs()
223 struct bch_fs *c; in bch2_uuid_to_fs() local
226 c = __bch2_uuid_to_fs(uuid); in bch2_uuid_to_fs()
227 if (c) in bch2_uuid_to_fs()
228 closure_get(&c->cl); in bch2_uuid_to_fs()
231 return c; in bch2_uuid_to_fs()
239 * - foreground writes depend on copygc and rebalance (to free up space)
241 * - copygc and rebalance depend on mark and sweep gc (they actually probably
246 * - all of the above depends on the allocator threads
248 * - allocator depends on the journal (when it rewrites prios and gens)
251 static void __bch2_fs_read_only(struct bch_fs *c) in __bch2_fs_read_only() argument
256 bch2_fs_ec_stop(c); in __bch2_fs_read_only()
257 bch2_open_buckets_stop(c, NULL, true); in __bch2_fs_read_only()
258 bch2_rebalance_stop(c); in __bch2_fs_read_only()
259 bch2_copygc_stop(c); in __bch2_fs_read_only()
260 bch2_fs_ec_flush(c); in __bch2_fs_read_only()
262 bch_verbose(c, "flushing journal and stopping allocators, journal seq %llu", in __bch2_fs_read_only()
263 journal_cur_seq(&c->journal)); in __bch2_fs_read_only()
268 if (bch2_btree_interior_updates_flush(c) || in __bch2_fs_read_only()
269 bch2_btree_write_buffer_flush_going_ro(c) || in __bch2_fs_read_only()
270 bch2_journal_flush_all_pins(&c->journal) || in __bch2_fs_read_only()
271 bch2_btree_flush_all_writes(c) || in __bch2_fs_read_only()
272 seq != atomic64_read(&c->journal.seq)) { in __bch2_fs_read_only()
273 seq = atomic64_read(&c->journal.seq); in __bch2_fs_read_only()
278 bch_verbose(c, "flushing journal and stopping allocators complete, journal seq %llu", in __bch2_fs_read_only()
279 journal_cur_seq(&c->journal)); in __bch2_fs_read_only()
281 if (test_bit(JOURNAL_replay_done, &c->journal.flags) && in __bch2_fs_read_only()
282 !test_bit(BCH_FS_emergency_ro, &c->flags)) in __bch2_fs_read_only()
283 set_bit(BCH_FS_clean_shutdown, &c->flags); in __bch2_fs_read_only()
285 bch2_fs_journal_stop(&c->journal); in __bch2_fs_read_only()
287 bch_info(c, "%sclean shutdown complete, journal seq %llu", in __bch2_fs_read_only()
288 test_bit(BCH_FS_clean_shutdown, &c->flags) ? "" : "un", in __bch2_fs_read_only()
289 c->journal.seq_ondisk); in __bch2_fs_read_only()
294 for_each_member_device(c, ca) { in __bch2_fs_read_only()
296 bch2_dev_allocator_remove(c, ca); in __bch2_fs_read_only()
303 struct bch_fs *c = container_of(writes, struct bch_fs, writes); in bch2_writes_disabled() local
305 set_bit(BCH_FS_write_disable_complete, &c->flags); in bch2_writes_disabled()
310 void bch2_fs_read_only(struct bch_fs *c) in bch2_fs_read_only() argument
312 if (!test_bit(BCH_FS_rw, &c->flags)) { in bch2_fs_read_only()
313 bch2_journal_reclaim_stop(&c->journal); in bch2_fs_read_only()
317 BUG_ON(test_bit(BCH_FS_write_disable_complete, &c->flags)); in bch2_fs_read_only()
319 bch_verbose(c, "going read-only"); in bch2_fs_read_only()
322 * Block new foreground-end write operations from starting - any new in bch2_fs_read_only()
323 * writes will return -EROFS: in bch2_fs_read_only()
325 set_bit(BCH_FS_going_ro, &c->flags); in bch2_fs_read_only()
327 percpu_ref_kill(&c->writes); in bch2_fs_read_only()
330 bch2_write_ref_put(c, i); in bch2_fs_read_only()
340 * on outstanding writes before shutting everything down - but in bch2_fs_read_only()
345 test_bit(BCH_FS_write_disable_complete, &c->flags) || in bch2_fs_read_only()
346 test_bit(BCH_FS_emergency_ro, &c->flags)); in bch2_fs_read_only()
348 bool writes_disabled = test_bit(BCH_FS_write_disable_complete, &c->flags); in bch2_fs_read_only()
350 bch_verbose(c, "finished waiting for writes to stop"); in bch2_fs_read_only()
352 __bch2_fs_read_only(c); in bch2_fs_read_only()
355 test_bit(BCH_FS_write_disable_complete, &c->flags)); in bch2_fs_read_only()
358 bch_verbose(c, "finished waiting for writes to stop"); in bch2_fs_read_only()
360 clear_bit(BCH_FS_write_disable_complete, &c->flags); in bch2_fs_read_only()
361 clear_bit(BCH_FS_going_ro, &c->flags); in bch2_fs_read_only()
362 clear_bit(BCH_FS_rw, &c->flags); in bch2_fs_read_only()
364 if (!bch2_journal_error(&c->journal) && in bch2_fs_read_only()
365 !test_bit(BCH_FS_error, &c->flags) && in bch2_fs_read_only()
366 !test_bit(BCH_FS_emergency_ro, &c->flags) && in bch2_fs_read_only()
367 test_bit(BCH_FS_started, &c->flags) && in bch2_fs_read_only()
368 test_bit(BCH_FS_clean_shutdown, &c->flags) && in bch2_fs_read_only()
369 c->recovery_pass_done >= BCH_RECOVERY_PASS_journal_replay) { in bch2_fs_read_only()
370 BUG_ON(c->journal.last_empty_seq != journal_cur_seq(&c->journal)); in bch2_fs_read_only()
371 BUG_ON(atomic_long_read(&c->btree_cache.nr_dirty)); in bch2_fs_read_only()
372 BUG_ON(atomic_long_read(&c->btree_key_cache.nr_dirty)); in bch2_fs_read_only()
373 BUG_ON(c->btree_write_buffer.inc.keys.nr); in bch2_fs_read_only()
374 BUG_ON(c->btree_write_buffer.flushing.keys.nr); in bch2_fs_read_only()
375 bch2_verify_accounting_clean(c); in bch2_fs_read_only()
377 bch_verbose(c, "marking filesystem clean"); in bch2_fs_read_only()
378 bch2_fs_mark_clean(c); in bch2_fs_read_only()
381 mutex_lock(&c->sb_lock); in bch2_fs_read_only()
382 bch2_write_super(c); in bch2_fs_read_only()
383 mutex_unlock(&c->sb_lock); in bch2_fs_read_only()
385 bch_verbose(c, "done going read-only, filesystem not clean"); in bch2_fs_read_only()
391 struct bch_fs *c = in bch2_fs_read_only_work() local
394 down_write(&c->state_lock); in bch2_fs_read_only_work()
395 bch2_fs_read_only(c); in bch2_fs_read_only_work()
396 up_write(&c->state_lock); in bch2_fs_read_only_work()
399 static void bch2_fs_read_only_async(struct bch_fs *c) in bch2_fs_read_only_async() argument
401 queue_work(system_long_wq, &c->read_only_work); in bch2_fs_read_only_async()
404 bool bch2_fs_emergency_read_only(struct bch_fs *c) in bch2_fs_emergency_read_only() argument
406 bool ret = !test_and_set_bit(BCH_FS_emergency_ro, &c->flags); in bch2_fs_emergency_read_only()
408 bch2_journal_halt(&c->journal); in bch2_fs_emergency_read_only()
409 bch2_fs_read_only_async(c); in bch2_fs_emergency_read_only()
415 bool bch2_fs_emergency_read_only_locked(struct bch_fs *c) in bch2_fs_emergency_read_only_locked() argument
417 bool ret = !test_and_set_bit(BCH_FS_emergency_ro, &c->flags); in bch2_fs_emergency_read_only_locked()
419 bch2_journal_halt_locked(&c->journal); in bch2_fs_emergency_read_only_locked()
420 bch2_fs_read_only_async(c); in bch2_fs_emergency_read_only_locked()
426 static int __bch2_fs_read_write(struct bch_fs *c, bool early) in __bch2_fs_read_write() argument
430 BUG_ON(!test_bit(BCH_FS_may_go_rw, &c->flags)); in __bch2_fs_read_write()
432 if (test_bit(BCH_FS_initial_gc_unfixed, &c->flags)) { in __bch2_fs_read_write()
433 bch_err(c, "cannot go rw, unfixed btree errors"); in __bch2_fs_read_write()
434 return -BCH_ERR_erofs_unfixed_errors; in __bch2_fs_read_write()
437 if (test_bit(BCH_FS_rw, &c->flags)) in __bch2_fs_read_write()
440 bch_info(c, "going read-write"); in __bch2_fs_read_write()
442 ret = bch2_sb_members_v2_init(c); in __bch2_fs_read_write()
446 clear_bit(BCH_FS_clean_shutdown, &c->flags); in __bch2_fs_read_write()
448 __for_each_online_member(c, ca, BIT(BCH_MEMBER_STATE_rw), READ) { in __bch2_fs_read_write()
449 bch2_dev_allocator_add(c, ca); in __bch2_fs_read_write()
450 percpu_ref_reinit(&ca->io_ref[WRITE]); in __bch2_fs_read_write()
452 bch2_recalc_capacity(c); in __bch2_fs_read_write()
458 * at least one non-flush write in the journal or recovery will fail: in __bch2_fs_read_write()
460 spin_lock(&c->journal.lock); in __bch2_fs_read_write()
461 set_bit(JOURNAL_need_flush_write, &c->journal.flags); in __bch2_fs_read_write()
462 set_bit(JOURNAL_running, &c->journal.flags); in __bch2_fs_read_write()
463 bch2_journal_space_available(&c->journal); in __bch2_fs_read_write()
464 spin_unlock(&c->journal.lock); in __bch2_fs_read_write()
466 ret = bch2_fs_mark_dirty(c); in __bch2_fs_read_write()
470 ret = bch2_journal_reclaim_start(&c->journal); in __bch2_fs_read_write()
474 set_bit(BCH_FS_rw, &c->flags); in __bch2_fs_read_write()
475 set_bit(BCH_FS_was_rw, &c->flags); in __bch2_fs_read_write()
478 percpu_ref_reinit(&c->writes); in __bch2_fs_read_write()
481 BUG_ON(atomic_long_read(&c->writes[i])); in __bch2_fs_read_write()
482 atomic_long_inc(&c->writes[i]); in __bch2_fs_read_write()
486 ret = bch2_copygc_start(c); in __bch2_fs_read_write()
488 bch_err_msg(c, ret, "error starting copygc thread"); in __bch2_fs_read_write()
492 ret = bch2_rebalance_start(c); in __bch2_fs_read_write()
494 bch_err_msg(c, ret, "error starting rebalance thread"); in __bch2_fs_read_write()
498 bch2_do_discards(c); in __bch2_fs_read_write()
499 bch2_do_invalidates(c); in __bch2_fs_read_write()
500 bch2_do_stripe_deletes(c); in __bch2_fs_read_write()
501 bch2_do_pending_node_rewrites(c); in __bch2_fs_read_write()
504 if (test_bit(BCH_FS_rw, &c->flags)) in __bch2_fs_read_write()
505 bch2_fs_read_only(c); in __bch2_fs_read_write()
507 __bch2_fs_read_only(c); in __bch2_fs_read_write()
511 int bch2_fs_read_write(struct bch_fs *c) in bch2_fs_read_write() argument
513 if (c->opts.recovery_pass_last && in bch2_fs_read_write()
514 c->opts.recovery_pass_last < BCH_RECOVERY_PASS_journal_replay) in bch2_fs_read_write()
515 return -BCH_ERR_erofs_norecovery; in bch2_fs_read_write()
517 if (c->opts.nochanges) in bch2_fs_read_write()
518 return -BCH_ERR_erofs_nochanges; in bch2_fs_read_write()
520 return __bch2_fs_read_write(c, false); in bch2_fs_read_write()
523 int bch2_fs_read_write_early(struct bch_fs *c) in bch2_fs_read_write_early() argument
525 down_write(&c->state_lock); in bch2_fs_read_write_early()
526 int ret = __bch2_fs_read_write(c, true); in bch2_fs_read_write_early()
527 up_write(&c->state_lock); in bch2_fs_read_write_early()
534 static void __bch2_fs_free(struct bch_fs *c) in __bch2_fs_free() argument
537 bch2_time_stats_exit(&c->times[i]); in __bch2_fs_free()
540 utf8_unload(c->cf_encoding); in __bch2_fs_free()
543 bch2_find_btree_nodes_exit(&c->found_btree_nodes); in __bch2_fs_free()
544 bch2_free_pending_node_rewrites(c); in __bch2_fs_free()
545 bch2_free_fsck_errs(c); in __bch2_fs_free()
546 bch2_fs_accounting_exit(c); in __bch2_fs_free()
547 bch2_fs_sb_errors_exit(c); in __bch2_fs_free()
548 bch2_fs_counters_exit(c); in __bch2_fs_free()
549 bch2_fs_snapshots_exit(c); in __bch2_fs_free()
550 bch2_fs_quota_exit(c); in __bch2_fs_free()
551 bch2_fs_fs_io_direct_exit(c); in __bch2_fs_free()
552 bch2_fs_fs_io_buffered_exit(c); in __bch2_fs_free()
553 bch2_fs_fsio_exit(c); in __bch2_fs_free()
554 bch2_fs_vfs_exit(c); in __bch2_fs_free()
555 bch2_fs_ec_exit(c); in __bch2_fs_free()
556 bch2_fs_encryption_exit(c); in __bch2_fs_free()
557 bch2_fs_nocow_locking_exit(c); in __bch2_fs_free()
558 bch2_fs_io_write_exit(c); in __bch2_fs_free()
559 bch2_fs_io_read_exit(c); in __bch2_fs_free()
560 bch2_fs_buckets_waiting_for_journal_exit(c); in __bch2_fs_free()
561 bch2_fs_btree_interior_update_exit(c); in __bch2_fs_free()
562 bch2_fs_btree_key_cache_exit(&c->btree_key_cache); in __bch2_fs_free()
563 bch2_fs_btree_cache_exit(c); in __bch2_fs_free()
564 bch2_fs_btree_iter_exit(c); in __bch2_fs_free()
565 bch2_fs_replicas_exit(c); in __bch2_fs_free()
566 bch2_fs_journal_exit(&c->journal); in __bch2_fs_free()
567 bch2_io_clock_exit(&c->io_clock[WRITE]); in __bch2_fs_free()
568 bch2_io_clock_exit(&c->io_clock[READ]); in __bch2_fs_free()
569 bch2_fs_compress_exit(c); in __bch2_fs_free()
570 bch2_fs_btree_gc_exit(c); in __bch2_fs_free()
571 bch2_journal_keys_put_initial(c); in __bch2_fs_free()
572 bch2_find_btree_nodes_exit(&c->found_btree_nodes); in __bch2_fs_free()
573 BUG_ON(atomic_read(&c->journal_keys.ref)); in __bch2_fs_free()
574 bch2_fs_btree_write_buffer_exit(c); in __bch2_fs_free()
575 percpu_free_rwsem(&c->mark_lock); in __bch2_fs_free()
576 if (c->online_reserved) { in __bch2_fs_free()
577 u64 v = percpu_u64_get(c->online_reserved); in __bch2_fs_free()
579 free_percpu(c->online_reserved); in __bch2_fs_free()
582 darray_exit(&c->incompat_versions_requested); in __bch2_fs_free()
583 darray_exit(&c->btree_roots_extra); in __bch2_fs_free()
584 free_percpu(c->pcpu); in __bch2_fs_free()
585 free_percpu(c->usage); in __bch2_fs_free()
586 mempool_exit(&c->large_bkey_pool); in __bch2_fs_free()
587 mempool_exit(&c->btree_bounce_pool); in __bch2_fs_free()
588 bioset_exit(&c->btree_bio); in __bch2_fs_free()
589 mempool_exit(&c->fill_iter); in __bch2_fs_free()
591 percpu_ref_exit(&c->writes); in __bch2_fs_free()
593 kfree(rcu_dereference_protected(c->disk_groups, 1)); in __bch2_fs_free()
594 kfree(c->journal_seq_blacklist_table); in __bch2_fs_free()
596 if (c->write_ref_wq) in __bch2_fs_free()
597 destroy_workqueue(c->write_ref_wq); in __bch2_fs_free()
598 if (c->btree_write_submit_wq) in __bch2_fs_free()
599 destroy_workqueue(c->btree_write_submit_wq); in __bch2_fs_free()
600 if (c->btree_read_complete_wq) in __bch2_fs_free()
601 destroy_workqueue(c->btree_read_complete_wq); in __bch2_fs_free()
602 if (c->copygc_wq) in __bch2_fs_free()
603 destroy_workqueue(c->copygc_wq); in __bch2_fs_free()
604 if (c->btree_io_complete_wq) in __bch2_fs_free()
605 destroy_workqueue(c->btree_io_complete_wq); in __bch2_fs_free()
606 if (c->btree_update_wq) in __bch2_fs_free()
607 destroy_workqueue(c->btree_update_wq); in __bch2_fs_free()
609 bch2_free_super(&c->disk_sb); in __bch2_fs_free()
610 kvfree(c); in __bch2_fs_free()
616 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); in bch2_fs_release() local
618 __bch2_fs_free(c); in bch2_fs_release()
621 void __bch2_fs_stop(struct bch_fs *c) in __bch2_fs_stop() argument
623 bch_verbose(c, "shutting down"); in __bch2_fs_stop()
625 set_bit(BCH_FS_stopping, &c->flags); in __bch2_fs_stop()
627 down_write(&c->state_lock); in __bch2_fs_stop()
628 bch2_fs_read_only(c); in __bch2_fs_stop()
629 up_write(&c->state_lock); in __bch2_fs_stop()
631 for_each_member_device(c, ca) in __bch2_fs_stop()
634 if (c->kobj.state_in_sysfs) in __bch2_fs_stop()
635 kobject_del(&c->kobj); in __bch2_fs_stop()
637 bch2_fs_debug_exit(c); in __bch2_fs_stop()
638 bch2_fs_chardev_exit(c); in __bch2_fs_stop()
640 bch2_ro_ref_put(c); in __bch2_fs_stop()
641 wait_event(c->ro_ref_wait, !refcount_read(&c->ro_ref)); in __bch2_fs_stop()
643 kobject_put(&c->counters_kobj); in __bch2_fs_stop()
644 kobject_put(&c->time_stats); in __bch2_fs_stop()
645 kobject_put(&c->opts_dir); in __bch2_fs_stop()
646 kobject_put(&c->internal); in __bch2_fs_stop()
649 bch2_btree_flush_all_reads(c); in __bch2_fs_stop()
651 for_each_member_device(c, ca) in __bch2_fs_stop()
652 cancel_work_sync(&ca->io_error_work); in __bch2_fs_stop()
654 cancel_work_sync(&c->read_only_work); in __bch2_fs_stop()
657 void bch2_fs_free(struct bch_fs *c) in bch2_fs_free() argument
662 list_del(&c->list); in bch2_fs_free()
665 closure_sync(&c->cl); in bch2_fs_free()
666 closure_debug_destroy(&c->cl); in bch2_fs_free()
668 for (i = 0; i < c->sb.nr_devices; i++) { in bch2_fs_free()
669 struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true); in bch2_fs_free()
672 EBUG_ON(atomic_long_read(&ca->ref) != 1); in bch2_fs_free()
674 bch2_free_super(&ca->disk_sb); in bch2_fs_free()
679 bch_verbose(c, "shutdown complete"); in bch2_fs_free()
681 kobject_put(&c->kobj); in bch2_fs_free()
684 void bch2_fs_stop(struct bch_fs *c) in bch2_fs_stop() argument
686 __bch2_fs_stop(c); in bch2_fs_stop()
687 bch2_fs_free(c); in bch2_fs_stop()
690 static int bch2_fs_online(struct bch_fs *c) in bch2_fs_online() argument
696 if (__bch2_uuid_to_fs(c->sb.uuid)) { in bch2_fs_online()
697 bch_err(c, "filesystem UUID already open"); in bch2_fs_online()
698 return -EINVAL; in bch2_fs_online()
701 ret = bch2_fs_chardev_init(c); in bch2_fs_online()
703 bch_err(c, "error creating character device"); in bch2_fs_online()
707 bch2_fs_debug_init(c); in bch2_fs_online()
709 ret = kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ?: in bch2_fs_online()
710 kobject_add(&c->internal, &c->kobj, "internal") ?: in bch2_fs_online()
711 kobject_add(&c->opts_dir, &c->kobj, "options") ?: in bch2_fs_online()
713 kobject_add(&c->time_stats, &c->kobj, "time_stats") ?: in bch2_fs_online()
715 kobject_add(&c->counters_kobj, &c->kobj, "counters") ?: in bch2_fs_online()
716 bch2_opts_create_sysfs_files(&c->opts_dir, OPT_FS); in bch2_fs_online()
718 bch_err(c, "error creating sysfs objects"); in bch2_fs_online()
722 down_write(&c->state_lock); in bch2_fs_online()
724 for_each_member_device(c, ca) { in bch2_fs_online()
725 ret = bch2_dev_sysfs_online(c, ca); in bch2_fs_online()
727 bch_err(c, "error creating sysfs objects"); in bch2_fs_online()
733 BUG_ON(!list_empty(&c->list)); in bch2_fs_online()
734 list_add(&c->list, &bch_fs_list); in bch2_fs_online()
736 up_write(&c->state_lock); in bch2_fs_online()
742 struct bch_fs *c; in bch2_fs_alloc() local
747 c = kvmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO); in bch2_fs_alloc()
748 if (!c) { in bch2_fs_alloc()
749 c = ERR_PTR(-BCH_ERR_ENOMEM_fs_alloc); in bch2_fs_alloc()
753 c->stdio = (void *)(unsigned long) opts.stdio; in bch2_fs_alloc()
757 closure_init(&c->cl, NULL); in bch2_fs_alloc()
759 c->kobj.kset = bcachefs_kset; in bch2_fs_alloc()
760 kobject_init(&c->kobj, &bch2_fs_ktype); in bch2_fs_alloc()
761 kobject_init(&c->internal, &bch2_fs_internal_ktype); in bch2_fs_alloc()
762 kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype); in bch2_fs_alloc()
763 kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype); in bch2_fs_alloc()
764 kobject_init(&c->counters_kobj, &bch2_fs_counters_ktype); in bch2_fs_alloc()
766 c->minor = -1; in bch2_fs_alloc()
767 c->disk_sb.fs_sb = true; in bch2_fs_alloc()
769 init_rwsem(&c->state_lock); in bch2_fs_alloc()
770 mutex_init(&c->sb_lock); in bch2_fs_alloc()
771 mutex_init(&c->replicas_gc_lock); in bch2_fs_alloc()
772 mutex_init(&c->btree_root_lock); in bch2_fs_alloc()
773 INIT_WORK(&c->read_only_work, bch2_fs_read_only_work); in bch2_fs_alloc()
775 refcount_set(&c->ro_ref, 1); in bch2_fs_alloc()
776 init_waitqueue_head(&c->ro_ref_wait); in bch2_fs_alloc()
777 spin_lock_init(&c->recovery_pass_lock); in bch2_fs_alloc()
778 sema_init(&c->online_fsck_mutex, 1); in bch2_fs_alloc()
781 bch2_time_stats_init(&c->times[i]); in bch2_fs_alloc()
783 bch2_fs_copygc_init(c); in bch2_fs_alloc()
784 bch2_fs_btree_key_cache_init_early(&c->btree_key_cache); in bch2_fs_alloc()
785 bch2_fs_btree_iter_init_early(c); in bch2_fs_alloc()
786 bch2_fs_btree_interior_update_init_early(c); in bch2_fs_alloc()
787 bch2_fs_journal_keys_init(c); in bch2_fs_alloc()
788 bch2_fs_allocator_background_init(c); in bch2_fs_alloc()
789 bch2_fs_allocator_foreground_init(c); in bch2_fs_alloc()
790 bch2_fs_rebalance_init(c); in bch2_fs_alloc()
791 bch2_fs_quota_init(c); in bch2_fs_alloc()
792 bch2_fs_ec_init_early(c); in bch2_fs_alloc()
793 bch2_fs_move_init(c); in bch2_fs_alloc()
794 bch2_fs_sb_errors_init_early(c); in bch2_fs_alloc()
796 INIT_LIST_HEAD(&c->list); in bch2_fs_alloc()
798 mutex_init(&c->bio_bounce_pages_lock); in bch2_fs_alloc()
799 mutex_init(&c->snapshot_table_lock); in bch2_fs_alloc()
800 init_rwsem(&c->snapshot_create_lock); in bch2_fs_alloc()
802 spin_lock_init(&c->btree_write_error_lock); in bch2_fs_alloc()
804 INIT_LIST_HEAD(&c->journal_iters); in bch2_fs_alloc()
806 INIT_LIST_HEAD(&c->fsck_error_msgs); in bch2_fs_alloc()
807 mutex_init(&c->fsck_error_msgs_lock); in bch2_fs_alloc()
809 seqcount_init(&c->usage_lock); in bch2_fs_alloc()
811 sema_init(&c->io_in_flight, 128); in bch2_fs_alloc()
813 INIT_LIST_HEAD(&c->vfs_inodes_list); in bch2_fs_alloc()
814 mutex_init(&c->vfs_inodes_lock); in bch2_fs_alloc()
816 c->journal.flush_write_time = &c->times[BCH_TIME_journal_flush_write]; in bch2_fs_alloc()
817 c->journal.noflush_write_time = &c->times[BCH_TIME_journal_noflush_write]; in bch2_fs_alloc()
818 c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq]; in bch2_fs_alloc()
820 bch2_fs_btree_cache_init_early(&c->btree_cache); in bch2_fs_alloc()
822 mutex_init(&c->sectors_available_lock); in bch2_fs_alloc()
824 ret = percpu_init_rwsem(&c->mark_lock); in bch2_fs_alloc()
828 mutex_lock(&c->sb_lock); in bch2_fs_alloc()
829 ret = bch2_sb_to_fs(c, sb); in bch2_fs_alloc()
830 mutex_unlock(&c->sb_lock); in bch2_fs_alloc()
835 pr_uuid(&name, c->sb.user_uuid.b); in bch2_fs_alloc()
836 ret = name.allocation_failure ? -BCH_ERR_ENOMEM_fs_name_alloc : 0; in bch2_fs_alloc()
840 strscpy(c->name, name.buf, sizeof(c->name)); in bch2_fs_alloc()
844 if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && in bch2_fs_alloc()
848 if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && in bch2_fs_alloc()
852 c->opts = bch2_opts_default; in bch2_fs_alloc()
853 ret = bch2_opts_from_sb(&c->opts, sb); in bch2_fs_alloc()
857 bch2_opts_apply(&c->opts, opts); in bch2_fs_alloc()
859 c->btree_key_cache_btrees |= 1U << BTREE_ID_alloc; in bch2_fs_alloc()
860 if (c->opts.inodes_use_key_cache) in bch2_fs_alloc()
861 c->btree_key_cache_btrees |= 1U << BTREE_ID_inodes; in bch2_fs_alloc()
862 c->btree_key_cache_btrees |= 1U << BTREE_ID_logged_ops; in bch2_fs_alloc()
864 c->block_bits = ilog2(block_sectors(c)); in bch2_fs_alloc()
865 c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c); in bch2_fs_alloc()
868 bch_err(c, "fs_alloc fault injected"); in bch2_fs_alloc()
869 ret = -EFAULT; in bch2_fs_alloc()
874 (btree_blocks(c) + 1) * 2 * in bch2_fs_alloc()
877 if (!(c->btree_update_wq = alloc_workqueue("bcachefs", in bch2_fs_alloc()
879 !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io", in bch2_fs_alloc()
881 !(c->copygc_wq = alloc_workqueue("bcachefs_copygc", in bch2_fs_alloc()
883 !(c->btree_read_complete_wq = alloc_workqueue("bcachefs_btree_read_complete", in bch2_fs_alloc()
885 !(c->btree_write_submit_wq = alloc_workqueue("bcachefs_btree_write_sumit", in bch2_fs_alloc()
887 !(c->write_ref_wq = alloc_workqueue("bcachefs_write_ref", in bch2_fs_alloc()
890 percpu_ref_init(&c->writes, bch2_writes_disabled, in bch2_fs_alloc()
893 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) || in bch2_fs_alloc()
894 bioset_init(&c->btree_bio, 1, in bch2_fs_alloc()
898 !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) || in bch2_fs_alloc()
899 !(c->usage = alloc_percpu(struct bch_fs_usage_base)) || in bch2_fs_alloc()
900 !(c->online_reserved = alloc_percpu(u64)) || in bch2_fs_alloc()
901 mempool_init_kvmalloc_pool(&c->btree_bounce_pool, 1, in bch2_fs_alloc()
902 c->opts.btree_node_size) || in bch2_fs_alloc()
903 mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048)) { in bch2_fs_alloc()
904 ret = -BCH_ERR_ENOMEM_fs_other_alloc; in bch2_fs_alloc()
908 ret = bch2_fs_counters_init(c) ?: in bch2_fs_alloc()
909 bch2_fs_sb_errors_init(c) ?: in bch2_fs_alloc()
910 bch2_io_clock_init(&c->io_clock[READ]) ?: in bch2_fs_alloc()
911 bch2_io_clock_init(&c->io_clock[WRITE]) ?: in bch2_fs_alloc()
912 bch2_fs_journal_init(&c->journal) ?: in bch2_fs_alloc()
913 bch2_fs_btree_iter_init(c) ?: in bch2_fs_alloc()
914 bch2_fs_btree_cache_init(c) ?: in bch2_fs_alloc()
915 bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?: in bch2_fs_alloc()
916 bch2_fs_btree_interior_update_init(c) ?: in bch2_fs_alloc()
917 bch2_fs_btree_gc_init(c) ?: in bch2_fs_alloc()
918 bch2_fs_buckets_waiting_for_journal_init(c) ?: in bch2_fs_alloc()
919 bch2_fs_btree_write_buffer_init(c) ?: in bch2_fs_alloc()
920 bch2_fs_subvolumes_init(c) ?: in bch2_fs_alloc()
921 bch2_fs_io_read_init(c) ?: in bch2_fs_alloc()
922 bch2_fs_io_write_init(c) ?: in bch2_fs_alloc()
923 bch2_fs_nocow_locking_init(c) ?: in bch2_fs_alloc()
924 bch2_fs_encryption_init(c) ?: in bch2_fs_alloc()
925 bch2_fs_compress_init(c) ?: in bch2_fs_alloc()
926 bch2_fs_ec_init(c) ?: in bch2_fs_alloc()
927 bch2_fs_vfs_init(c) ?: in bch2_fs_alloc()
928 bch2_fs_fsio_init(c) ?: in bch2_fs_alloc()
929 bch2_fs_fs_io_buffered_init(c) ?: in bch2_fs_alloc()
930 bch2_fs_fs_io_direct_init(c); in bch2_fs_alloc()
936 c->cf_encoding = utf8_load(BCH_FS_DEFAULT_UTF8_ENCODING); in bch2_fs_alloc()
937 if (IS_ERR(c->cf_encoding)) { in bch2_fs_alloc()
938 printk(KERN_ERR "Cannot load UTF-8 encoding for filesystem. Version: %u.%u.%u", in bch2_fs_alloc()
942 ret = -EINVAL; in bch2_fs_alloc()
945 bch_info(c, "Using encoding defined by superblock: utf8-%u.%u.%u", in bch2_fs_alloc()
950 if (c->sb.features & BIT_ULL(BCH_FEATURE_casefolding)) { in bch2_fs_alloc()
952 ret = -EINVAL; in bch2_fs_alloc()
957 for (i = 0; i < c->sb.nr_devices; i++) { in bch2_fs_alloc()
958 if (!bch2_member_exists(c->disk_sb.sb, i)) in bch2_fs_alloc()
960 ret = bch2_dev_alloc(c, i); in bch2_fs_alloc()
965 bch2_journal_entry_res_resize(&c->journal, in bch2_fs_alloc()
966 &c->btree_root_journal_res, in bch2_fs_alloc()
968 bch2_journal_entry_res_resize(&c->journal, in bch2_fs_alloc()
969 &c->clock_journal_res, in bch2_fs_alloc()
973 ret = bch2_fs_online(c); in bch2_fs_alloc()
979 return c; in bch2_fs_alloc()
981 bch2_fs_free(c); in bch2_fs_alloc()
982 c = ERR_PTR(ret); in bch2_fs_alloc()
987 static void print_mount_opts(struct bch_fs *c) in print_mount_opts() argument
994 bch2_version_to_text(&p, c->sb.version); in print_mount_opts()
998 u64 v = bch2_opt_get_by_id(&c->opts, i); in print_mount_opts()
1000 if (!(opt->flags & OPT_MOUNT)) in print_mount_opts()
1008 bch2_opt_to_text(&p, c, c->disk_sb.sb, opt, v, OPT_SHOW_MOUNT_STYLE); in print_mount_opts()
1011 if (c->sb.version_incompat_allowed != c->sb.version) { in print_mount_opts()
1013 bch2_version_to_text(&p, c->sb.version_incompat_allowed); in print_mount_opts()
1016 bch_info(c, "%s", p.buf); in print_mount_opts()
1020 static bool bch2_fs_may_start(struct bch_fs *c) in bch2_fs_may_start() argument
1025 if (c->opts.very_degraded) in bch2_fs_may_start()
1028 if (c->opts.degraded) in bch2_fs_may_start()
1031 if (!c->opts.degraded && in bch2_fs_may_start()
1032 !c->opts.very_degraded) { in bch2_fs_may_start()
1033 mutex_lock(&c->sb_lock); in bch2_fs_may_start()
1035 for (i = 0; i < c->disk_sb.sb->nr_devices; i++) { in bch2_fs_may_start()
1036 if (!bch2_member_exists(c->disk_sb.sb, i)) in bch2_fs_may_start()
1039 ca = bch2_dev_locked(c, i); in bch2_fs_may_start()
1042 (ca->mi.state == BCH_MEMBER_STATE_rw || in bch2_fs_may_start()
1043 ca->mi.state == BCH_MEMBER_STATE_ro)) { in bch2_fs_may_start()
1044 mutex_unlock(&c->sb_lock); in bch2_fs_may_start()
1048 mutex_unlock(&c->sb_lock); in bch2_fs_may_start()
1051 return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true); in bch2_fs_may_start()
1054 int bch2_fs_start(struct bch_fs *c) in bch2_fs_start() argument
1059 print_mount_opts(c); in bch2_fs_start()
1061 if (!bch2_fs_may_start(c)) in bch2_fs_start()
1062 return -BCH_ERR_insufficient_devices_to_start; in bch2_fs_start()
1064 down_write(&c->state_lock); in bch2_fs_start()
1065 mutex_lock(&c->sb_lock); in bch2_fs_start()
1067 BUG_ON(test_bit(BCH_FS_started, &c->flags)); in bch2_fs_start()
1069 if (!bch2_sb_field_get_minsize(&c->disk_sb, ext, in bch2_fs_start()
1071 mutex_unlock(&c->sb_lock); in bch2_fs_start()
1072 up_write(&c->state_lock); in bch2_fs_start()
1073 ret = -BCH_ERR_ENOSPC_sb; in bch2_fs_start()
1077 ret = bch2_sb_members_v2_init(c); in bch2_fs_start()
1079 mutex_unlock(&c->sb_lock); in bch2_fs_start()
1080 up_write(&c->state_lock); in bch2_fs_start()
1084 for_each_online_member(c, ca) in bch2_fs_start()
1085 bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = cpu_to_le64(now); in bch2_fs_start()
1087 mutex_unlock(&c->sb_lock); in bch2_fs_start()
1089 for_each_rw_member(c, ca) in bch2_fs_start()
1090 bch2_dev_allocator_add(c, ca); in bch2_fs_start()
1091 bch2_recalc_capacity(c); in bch2_fs_start()
1092 up_write(&c->state_lock); in bch2_fs_start()
1094 c->recovery_task = current; in bch2_fs_start()
1095 ret = BCH_SB_INITIALIZED(c->disk_sb.sb) in bch2_fs_start()
1096 ? bch2_fs_recovery(c) in bch2_fs_start()
1097 : bch2_fs_initialize(c); in bch2_fs_start()
1098 c->recovery_task = NULL; in bch2_fs_start()
1103 ret = bch2_opts_check_may_set(c); in bch2_fs_start()
1108 ret = -BCH_ERR_injected_fs_start; in bch2_fs_start()
1112 set_bit(BCH_FS_started, &c->flags); in bch2_fs_start()
1113 wake_up(&c->ro_ref_wait); in bch2_fs_start()
1115 down_write(&c->state_lock); in bch2_fs_start()
1116 if (c->opts.read_only) in bch2_fs_start()
1117 bch2_fs_read_only(c); in bch2_fs_start()
1118 else if (!test_bit(BCH_FS_rw, &c->flags)) in bch2_fs_start()
1119 ret = bch2_fs_read_write(c); in bch2_fs_start()
1120 up_write(&c->state_lock); in bch2_fs_start()
1124 bch_err_msg(c, ret, "starting filesystem"); in bch2_fs_start()
1126 bch_verbose(c, "done starting filesystem"); in bch2_fs_start()
1130 static int bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c) in bch2_dev_may_add() argument
1132 struct bch_member m = bch2_sb_member_get(sb, sb->dev_idx); in bch2_dev_may_add()
1134 if (le16_to_cpu(sb->block_size) != block_sectors(c)) in bch2_dev_may_add()
1135 return -BCH_ERR_mismatched_block_size; in bch2_dev_may_add()
1138 BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb)) in bch2_dev_may_add()
1139 return -BCH_ERR_bucket_size_too_small; in bch2_dev_may_add()
1151 if (!uuid_equal(&fs->sb->uuid, &sb->sb->uuid)) in bch2_dev_in_fs()
1152 return -BCH_ERR_device_not_a_member_of_filesystem; in bch2_dev_in_fs()
1154 if (!bch2_member_exists(fs->sb, sb->sb->dev_idx)) in bch2_dev_in_fs()
1155 return -BCH_ERR_device_has_been_removed; in bch2_dev_in_fs()
1157 if (fs->sb->block_size != sb->sb->block_size) in bch2_dev_in_fs()
1158 return -BCH_ERR_mismatched_block_size; in bch2_dev_in_fs()
1160 if (le16_to_cpu(fs->sb->version) < bcachefs_metadata_version_member_seq || in bch2_dev_in_fs()
1161 le16_to_cpu(sb->sb->version) < bcachefs_metadata_version_member_seq) in bch2_dev_in_fs()
1164 if (fs->sb->seq == sb->sb->seq && in bch2_dev_in_fs()
1165 fs->sb->write_time != sb->sb->write_time) { in bch2_dev_in_fs()
1169 prt_bdevname(&buf, sb->bdev); in bch2_dev_in_fs()
1171 prt_bdevname(&buf, fs->bdev); in bch2_dev_in_fs()
1174 prt_printf(&buf, "seq=%llu but write_time different, got", le64_to_cpu(sb->sb->seq)); in bch2_dev_in_fs()
1177 prt_bdevname(&buf, fs->bdev); in bch2_dev_in_fs()
1179 bch2_prt_datetime(&buf, le64_to_cpu(fs->sb->write_time)); in bch2_dev_in_fs()
1182 prt_bdevname(&buf, sb->bdev); in bch2_dev_in_fs()
1184 bch2_prt_datetime(&buf, le64_to_cpu(sb->sb->write_time)); in bch2_dev_in_fs()
1187 if (!opts->no_splitbrain_check) in bch2_dev_in_fs()
1193 if (!opts->no_splitbrain_check) in bch2_dev_in_fs()
1194 return -BCH_ERR_device_splitbrain; in bch2_dev_in_fs()
1197 struct bch_member m = bch2_sb_member_get(fs->sb, sb->sb->dev_idx); in bch2_dev_in_fs()
1199 u64 seq_from_member = le64_to_cpu(sb->sb->seq); in bch2_dev_in_fs()
1205 prt_bdevname(&buf, sb->bdev); in bch2_dev_in_fs()
1207 prt_bdevname(&buf, fs->bdev); in bch2_dev_in_fs()
1211 prt_bdevname(&buf, fs->bdev); in bch2_dev_in_fs()
1213 prt_bdevname(&buf, sb->bdev); in bch2_dev_in_fs()
1215 prt_bdevname(&buf, sb->bdev); in bch2_dev_in_fs()
1218 if (!opts->no_splitbrain_check) { in bch2_dev_in_fs()
1220 prt_bdevname(&buf, sb->bdev); in bch2_dev_in_fs()
1226 if (!opts->no_splitbrain_check) in bch2_dev_in_fs()
1227 return -BCH_ERR_device_splitbrain; in bch2_dev_in_fs()
1237 if (!percpu_ref_is_zero(&ca->io_ref[rw])) { in bch2_dev_io_ref_stop()
1238 reinit_completion(&ca->io_ref_completion[rw]); in bch2_dev_io_ref_stop()
1239 percpu_ref_kill(&ca->io_ref[rw]); in bch2_dev_io_ref_stop()
1240 wait_for_completion(&ca->io_ref_completion[rw]); in bch2_dev_io_ref_stop()
1253 WARN_ON(!percpu_ref_is_zero(&ca->io_ref[WRITE])); in bch2_dev_free()
1254 WARN_ON(!percpu_ref_is_zero(&ca->io_ref[READ])); in bch2_dev_free()
1256 cancel_work_sync(&ca->io_error_work); in bch2_dev_free()
1260 if (ca->kobj.state_in_sysfs) in bch2_dev_free()
1261 kobject_del(&ca->kobj); in bch2_dev_free()
1263 bch2_free_super(&ca->disk_sb); in bch2_dev_free()
1267 free_percpu(ca->io_done); in bch2_dev_free()
1269 kfree(ca->sb_read_scratch); in bch2_dev_free()
1271 bch2_time_stats_quantiles_exit(&ca->io_latency[WRITE]); in bch2_dev_free()
1272 bch2_time_stats_quantiles_exit(&ca->io_latency[READ]); in bch2_dev_free()
1274 percpu_ref_exit(&ca->io_ref[WRITE]); in bch2_dev_free()
1275 percpu_ref_exit(&ca->io_ref[READ]); in bch2_dev_free()
1277 percpu_ref_exit(&ca->ref); in bch2_dev_free()
1279 kobject_put(&ca->kobj); in bch2_dev_free()
1282 static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) in __bch2_dev_offline() argument
1285 lockdep_assert_held(&c->state_lock); in __bch2_dev_offline()
1287 if (percpu_ref_is_zero(&ca->io_ref[READ])) in __bch2_dev_offline()
1290 __bch2_dev_read_only(c, ca); in __bch2_dev_offline()
1296 bch2_free_super(&ca->disk_sb); in __bch2_dev_offline()
1305 complete(&ca->ref_completion); in bch2_dev_ref_complete()
1313 complete(&ca->io_ref_completion[READ]); in bch2_dev_io_ref_read_complete()
1320 complete(&ca->io_ref_completion[WRITE]); in bch2_dev_io_ref_write_complete()
1328 * This is racy w.r.t. the underlying block device being hot-removed, in bch2_dev_unlink()
1332 * code doesn't appear to provide a good method and block/holder.c is in bch2_dev_unlink()
1335 if (ca->kobj.state_in_sysfs && in bch2_dev_unlink()
1336 ca->disk_sb.bdev && in bch2_dev_unlink()
1337 (b = bdev_kobj(ca->disk_sb.bdev))->state_in_sysfs) { in bch2_dev_unlink()
1339 sysfs_remove_link(&ca->kobj, "block"); in bch2_dev_unlink()
1343 static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) in bch2_dev_sysfs_online() argument
1347 if (!c->kobj.state_in_sysfs) in bch2_dev_sysfs_online()
1350 if (!ca->kobj.state_in_sysfs) { in bch2_dev_sysfs_online()
1351 ret = kobject_add(&ca->kobj, &c->kobj, "dev-%u", ca->dev_idx) ?: in bch2_dev_sysfs_online()
1352 bch2_opts_create_sysfs_files(&ca->kobj, OPT_DEVICE); in bch2_dev_sysfs_online()
1357 if (ca->disk_sb.bdev) { in bch2_dev_sysfs_online()
1358 struct kobject *block = bdev_kobj(ca->disk_sb.bdev); in bch2_dev_sysfs_online()
1360 ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); in bch2_dev_sysfs_online()
1364 ret = sysfs_create_link(&ca->kobj, block, "block"); in bch2_dev_sysfs_online()
1372 static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c, in __bch2_dev_alloc() argument
1382 kobject_init(&ca->kobj, &bch2_dev_ktype); in __bch2_dev_alloc()
1383 init_completion(&ca->ref_completion); in __bch2_dev_alloc()
1384 init_completion(&ca->io_ref_completion[READ]); in __bch2_dev_alloc()
1385 init_completion(&ca->io_ref_completion[WRITE]); in __bch2_dev_alloc()
1387 INIT_WORK(&ca->io_error_work, bch2_io_error_work); in __bch2_dev_alloc()
1389 bch2_time_stats_quantiles_init(&ca->io_latency[READ]); in __bch2_dev_alloc()
1390 bch2_time_stats_quantiles_init(&ca->io_latency[WRITE]); in __bch2_dev_alloc()
1392 ca->mi = bch2_mi_to_cpu(member); in __bch2_dev_alloc()
1394 for (i = 0; i < ARRAY_SIZE(member->errors); i++) in __bch2_dev_alloc()
1395 atomic64_set(&ca->errors[i], le64_to_cpu(member->errors[i])); in __bch2_dev_alloc()
1397 ca->uuid = member->uuid; in __bch2_dev_alloc()
1399 ca->nr_btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE, in __bch2_dev_alloc()
1400 ca->mi.bucket_size / btree_sectors(c)); in __bch2_dev_alloc()
1403 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, 0, GFP_KERNEL)) in __bch2_dev_alloc()
1406 atomic_long_set(&ca->ref, 1); in __bch2_dev_alloc()
1411 if (percpu_ref_init(&ca->io_ref[READ], bch2_dev_io_ref_read_complete, in __bch2_dev_alloc()
1413 percpu_ref_init(&ca->io_ref[WRITE], bch2_dev_io_ref_write_complete, in __bch2_dev_alloc()
1415 !(ca->sb_read_scratch = kmalloc(BCH_SB_READ_SCRATCH_BUF_SIZE, GFP_KERNEL)) || in __bch2_dev_alloc()
1416 bch2_dev_buckets_alloc(c, ca) || in __bch2_dev_alloc()
1417 !(ca->io_done = alloc_percpu(*ca->io_done))) in __bch2_dev_alloc()
1426 static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, in bch2_dev_attach() argument
1429 ca->dev_idx = dev_idx; in bch2_dev_attach()
1430 __set_bit(ca->dev_idx, ca->self.d); in bch2_dev_attach()
1431 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); in bch2_dev_attach()
1433 ca->fs = c; in bch2_dev_attach()
1434 rcu_assign_pointer(c->devs[ca->dev_idx], ca); in bch2_dev_attach()
1436 if (bch2_dev_sysfs_online(c, ca)) in bch2_dev_attach()
1440 static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx) in bch2_dev_alloc() argument
1442 struct bch_member member = bch2_sb_member_get(c->disk_sb.sb, dev_idx); in bch2_dev_alloc()
1448 ca = __bch2_dev_alloc(c, &member); in bch2_dev_alloc()
1452 ca->fs = c; in bch2_dev_alloc()
1454 bch2_dev_attach(c, ca, dev_idx); in bch2_dev_alloc()
1457 return -BCH_ERR_ENOMEM_dev_alloc; in bch2_dev_alloc()
1466 sb->sb->dev_idx); in __bch2_dev_attach_bdev()
1467 return -BCH_ERR_device_already_online; in __bch2_dev_attach_bdev()
1470 if (get_capacity(sb->bdev->bd_disk) < in __bch2_dev_attach_bdev()
1471 ca->mi.bucket_size * ca->mi.nbuckets) { in __bch2_dev_attach_bdev()
1473 return -BCH_ERR_device_size_too_small; in __bch2_dev_attach_bdev()
1476 BUG_ON(!percpu_ref_is_zero(&ca->io_ref[READ])); in __bch2_dev_attach_bdev()
1477 BUG_ON(!percpu_ref_is_zero(&ca->io_ref[WRITE])); in __bch2_dev_attach_bdev()
1479 ret = bch2_dev_journal_init(ca, sb->sb); in __bch2_dev_attach_bdev()
1484 ca->disk_sb = *sb; in __bch2_dev_attach_bdev()
1488 * Stash pointer to the filesystem for blk_holder_ops - note that once in __bch2_dev_attach_bdev()
1492 ca->disk_sb.holder->c = ca->fs; in __bch2_dev_attach_bdev()
1494 ca->dev = ca->disk_sb.bdev->bd_dev; in __bch2_dev_attach_bdev()
1496 percpu_ref_reinit(&ca->io_ref[READ]); in __bch2_dev_attach_bdev()
1501 static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb) in bch2_dev_attach_bdev() argument
1506 lockdep_assert_held(&c->state_lock); in bch2_dev_attach_bdev()
1508 if (le64_to_cpu(sb->sb->seq) > in bch2_dev_attach_bdev()
1509 le64_to_cpu(c->disk_sb.sb->seq)) in bch2_dev_attach_bdev()
1510 bch2_sb_to_fs(c, sb->sb); in bch2_dev_attach_bdev()
1512 BUG_ON(!bch2_dev_exists(c, sb->sb->dev_idx)); in bch2_dev_attach_bdev()
1514 ca = bch2_dev_locked(c, sb->sb->dev_idx); in bch2_dev_attach_bdev()
1520 bch2_dev_sysfs_online(c, ca); in bch2_dev_attach_bdev()
1523 prt_bdevname(&name, ca->disk_sb.bdev); in bch2_dev_attach_bdev()
1525 if (c->sb.nr_devices == 1) in bch2_dev_attach_bdev()
1526 strscpy(c->name, name.buf, sizeof(c->name)); in bch2_dev_attach_bdev()
1527 strscpy(ca->name, name.buf, sizeof(ca->name)); in bch2_dev_attach_bdev()
1531 bch2_rebalance_wakeup(c); in bch2_dev_attach_bdev()
1538 * Note: this function is also used by the error paths - when a particular
1540 * device RO, or - if this function returns false - we'll set the whole
1546 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, in bch2_dev_state_allowed() argument
1552 lockdep_assert_held(&c->state_lock); in bch2_dev_state_allowed()
1558 if (ca->mi.state != BCH_MEMBER_STATE_rw) in bch2_dev_state_allowed()
1562 for_each_member_device(c, ca2) in bch2_dev_state_allowed()
1564 nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw; in bch2_dev_state_allowed()
1567 ? c->opts.metadata_replicas in bch2_dev_state_allowed()
1568 : metadata_replicas_required(c), in bch2_dev_state_allowed()
1570 ? c->opts.data_replicas in bch2_dev_state_allowed()
1571 : data_replicas_required(c)); in bch2_dev_state_allowed()
1576 if (ca->mi.state != BCH_MEMBER_STATE_rw && in bch2_dev_state_allowed()
1577 ca->mi.state != BCH_MEMBER_STATE_ro) in bch2_dev_state_allowed()
1581 new_online_devs = bch2_online_devs(c); in bch2_dev_state_allowed()
1582 __clear_bit(ca->dev_idx, new_online_devs.d); in bch2_dev_state_allowed()
1584 return bch2_have_enough_devs(c, new_online_devs, flags, false); in bch2_dev_state_allowed()
1590 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) in __bch2_dev_read_only() argument
1597 bch2_dev_allocator_remove(c, ca); in __bch2_dev_read_only()
1598 bch2_recalc_capacity(c); in __bch2_dev_read_only()
1599 bch2_dev_journal_stop(&c->journal, ca); in __bch2_dev_read_only()
1602 static void __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) in __bch2_dev_read_write() argument
1604 lockdep_assert_held(&c->state_lock); in __bch2_dev_read_write()
1606 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw); in __bch2_dev_read_write()
1608 bch2_dev_allocator_add(c, ca); in __bch2_dev_read_write()
1609 bch2_recalc_capacity(c); in __bch2_dev_read_write()
1611 if (percpu_ref_is_zero(&ca->io_ref[WRITE])) in __bch2_dev_read_write()
1612 percpu_ref_reinit(&ca->io_ref[WRITE]); in __bch2_dev_read_write()
1617 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, in __bch2_dev_set_state() argument
1623 if (ca->mi.state == new_state) in __bch2_dev_set_state()
1626 if (!bch2_dev_state_allowed(c, ca, new_state, flags)) in __bch2_dev_set_state()
1627 return -BCH_ERR_device_state_not_allowed; in __bch2_dev_set_state()
1630 __bch2_dev_read_only(c, ca); in __bch2_dev_set_state()
1634 mutex_lock(&c->sb_lock); in __bch2_dev_set_state()
1635 m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); in __bch2_dev_set_state()
1637 bch2_write_super(c); in __bch2_dev_set_state()
1638 mutex_unlock(&c->sb_lock); in __bch2_dev_set_state()
1641 __bch2_dev_read_write(c, ca); in __bch2_dev_set_state()
1643 bch2_rebalance_wakeup(c); in __bch2_dev_set_state()
1648 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, in bch2_dev_set_state() argument
1653 down_write(&c->state_lock); in bch2_dev_set_state()
1654 ret = __bch2_dev_set_state(c, ca, new_state, flags); in bch2_dev_set_state()
1655 up_write(&c->state_lock); in bch2_dev_set_state()
1662 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) in bch2_dev_remove() argument
1665 unsigned dev_idx = ca->dev_idx, data; in bch2_dev_remove()
1668 down_write(&c->state_lock); in bch2_dev_remove()
1671 * We consume a reference to ca->ref, regardless of whether we succeed in bch2_dev_remove()
1676 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { in bch2_dev_remove()
1678 ret = -BCH_ERR_device_state_not_allowed; in bch2_dev_remove()
1682 __bch2_dev_read_only(c, ca); in bch2_dev_remove()
1684 ret = bch2_dev_data_drop(c, ca->dev_idx, flags); in bch2_dev_remove()
1689 ret = bch2_dev_remove_alloc(c, ca); in bch2_dev_remove()
1698 bch2_journal_flush_all_pins(&c->journal); in bch2_dev_remove()
1704 ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); in bch2_dev_remove()
1709 ret = bch2_journal_flush(&c->journal); in bch2_dev_remove()
1714 ret = bch2_replicas_gc2(c); in bch2_dev_remove()
1719 data = bch2_dev_has_data(c, ca); in bch2_dev_remove()
1726 ret = -EBUSY; in bch2_dev_remove()
1730 __bch2_dev_offline(c, ca); in bch2_dev_remove()
1732 mutex_lock(&c->sb_lock); in bch2_dev_remove()
1733 rcu_assign_pointer(c->devs[ca->dev_idx], NULL); in bch2_dev_remove()
1734 mutex_unlock(&c->sb_lock); in bch2_dev_remove()
1737 percpu_ref_kill(&ca->ref); in bch2_dev_remove()
1739 ca->dying = true; in bch2_dev_remove()
1742 wait_for_completion(&ca->ref_completion); in bch2_dev_remove()
1747 * Free this device's slot in the bch_member array - all pointers to in bch2_dev_remove()
1750 mutex_lock(&c->sb_lock); in bch2_dev_remove()
1751 m = bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx); in bch2_dev_remove()
1752 memset(&m->uuid, 0, sizeof(m->uuid)); in bch2_dev_remove()
1754 bch2_write_super(c); in bch2_dev_remove()
1756 mutex_unlock(&c->sb_lock); in bch2_dev_remove()
1757 up_write(&c->state_lock); in bch2_dev_remove()
1760 if (test_bit(BCH_FS_rw, &c->flags) && in bch2_dev_remove()
1761 ca->mi.state == BCH_MEMBER_STATE_rw && in bch2_dev_remove()
1762 !percpu_ref_is_zero(&ca->io_ref[READ])) in bch2_dev_remove()
1763 __bch2_dev_read_write(c, ca); in bch2_dev_remove()
1764 up_write(&c->state_lock); in bch2_dev_remove()
1769 int bch2_dev_add(struct bch_fs *c, const char *path) in bch2_dev_add() argument
1779 bch_err_msg(c, ret, "reading super"); in bch2_dev_add()
1783 struct bch_member dev_mi = bch2_sb_member_get(sb.sb, sb.sb->dev_idx); in bch2_dev_add()
1786 bch2_disk_path_to_text_sb(&label, sb.sb, BCH_MEMBER_GROUP(&dev_mi) - 1); in bch2_dev_add()
1788 ret = -ENOMEM; in bch2_dev_add()
1793 ret = bch2_dev_may_add(sb.sb, c); in bch2_dev_add()
1797 ca = __bch2_dev_alloc(c, &dev_mi); in bch2_dev_add()
1799 ret = -ENOMEM; in bch2_dev_add()
1807 down_write(&c->state_lock); in bch2_dev_add()
1808 mutex_lock(&c->sb_lock); in bch2_dev_add()
1810 ret = bch2_sb_from_fs(c, ca); in bch2_dev_add()
1811 bch_err_msg(c, ret, "setting up new superblock"); in bch2_dev_add()
1818 ret = bch2_sb_member_alloc(c); in bch2_dev_add()
1820 bch_err_msg(c, ret, "setting up new superblock"); in bch2_dev_add()
1828 *bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx) = dev_mi; in bch2_dev_add()
1830 ca->disk_sb.sb->dev_idx = dev_idx; in bch2_dev_add()
1831 bch2_dev_attach(c, ca, dev_idx); in bch2_dev_add()
1834 ret = __bch2_dev_group_set(c, ca, label.buf); in bch2_dev_add()
1835 bch_err_msg(c, ret, "creating new label"); in bch2_dev_add()
1840 bch2_write_super(c); in bch2_dev_add()
1841 mutex_unlock(&c->sb_lock); in bch2_dev_add()
1847 ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); in bch2_dev_add()
1852 ret = bch2_fs_freespace_init(c); in bch2_dev_add()
1857 if (ca->mi.state == BCH_MEMBER_STATE_rw) in bch2_dev_add()
1858 __bch2_dev_read_write(c, ca); in bch2_dev_add()
1861 bch_err_msg(c, ret, "allocating journal"); in bch2_dev_add()
1865 up_write(&c->state_lock); in bch2_dev_add()
1869 bch_err_fn(c, ret); in bch2_dev_add()
1873 mutex_unlock(&c->sb_lock); in bch2_dev_add()
1874 up_write(&c->state_lock); in bch2_dev_add()
1881 up_write(&c->state_lock); in bch2_dev_add()
1887 int bch2_dev_online(struct bch_fs *c, const char *path) in bch2_dev_online() argument
1895 down_write(&c->state_lock); in bch2_dev_online()
1899 up_write(&c->state_lock); in bch2_dev_online()
1903 dev_idx = sb.sb->dev_idx; in bch2_dev_online()
1905 ret = bch2_dev_in_fs(&c->disk_sb, &sb, &c->opts); in bch2_dev_online()
1906 bch_err_msg(c, ret, "bringing %s online", path); in bch2_dev_online()
1910 ret = bch2_dev_attach_bdev(c, &sb); in bch2_dev_online()
1914 ca = bch2_dev_locked(c, dev_idx); in bch2_dev_online()
1916 ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); in bch2_dev_online()
1917 bch_err_msg(c, ret, "bringing %s online: error from bch2_trans_mark_dev_sb", path); in bch2_dev_online()
1921 if (ca->mi.state == BCH_MEMBER_STATE_rw) in bch2_dev_online()
1922 __bch2_dev_read_write(c, ca); in bch2_dev_online()
1924 if (!ca->mi.freespace_initialized) { in bch2_dev_online()
1925 ret = bch2_dev_freespace_init(c, ca, 0, ca->mi.nbuckets); in bch2_dev_online()
1931 if (!ca->journal.nr) { in bch2_dev_online()
1938 mutex_lock(&c->sb_lock); in bch2_dev_online()
1939 bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = in bch2_dev_online()
1941 bch2_write_super(c); in bch2_dev_online()
1942 mutex_unlock(&c->sb_lock); in bch2_dev_online()
1944 up_write(&c->state_lock); in bch2_dev_online()
1947 up_write(&c->state_lock); in bch2_dev_online()
1952 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) in bch2_dev_offline() argument
1954 down_write(&c->state_lock); in bch2_dev_offline()
1958 up_write(&c->state_lock); in bch2_dev_offline()
1962 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { in bch2_dev_offline()
1964 up_write(&c->state_lock); in bch2_dev_offline()
1965 return -BCH_ERR_device_state_not_allowed; in bch2_dev_offline()
1968 __bch2_dev_offline(c, ca); in bch2_dev_offline()
1970 up_write(&c->state_lock); in bch2_dev_offline()
1974 int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) in bch2_dev_resize() argument
1980 down_write(&c->state_lock); in bch2_dev_resize()
1981 old_nbuckets = ca->mi.nbuckets; in bch2_dev_resize()
1983 if (nbuckets < ca->mi.nbuckets) { in bch2_dev_resize()
1985 ret = -EINVAL; in bch2_dev_resize()
1992 ret = -BCH_ERR_device_size_too_big; in bch2_dev_resize()
1997 get_capacity(ca->disk_sb.bdev->bd_disk) < in bch2_dev_resize()
1998 ca->mi.bucket_size * nbuckets) { in bch2_dev_resize()
2000 ret = -BCH_ERR_device_size_too_small; in bch2_dev_resize()
2004 ret = bch2_dev_buckets_resize(c, ca, nbuckets); in bch2_dev_resize()
2009 ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); in bch2_dev_resize()
2013 mutex_lock(&c->sb_lock); in bch2_dev_resize()
2014 m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); in bch2_dev_resize()
2015 m->nbuckets = cpu_to_le64(nbuckets); in bch2_dev_resize()
2017 bch2_write_super(c); in bch2_dev_resize()
2018 mutex_unlock(&c->sb_lock); in bch2_dev_resize()
2020 if (ca->mi.freespace_initialized) { in bch2_dev_resize()
2021 u64 v[3] = { nbuckets - old_nbuckets, 0, 0 }; in bch2_dev_resize()
2023 ret = bch2_trans_commit_do(ca->fs, NULL, NULL, 0, in bch2_dev_resize()
2025 .dev = ca->dev_idx, in bch2_dev_resize()
2027 bch2_dev_freespace_init(c, ca, old_nbuckets, nbuckets); in bch2_dev_resize()
2032 bch2_recalc_capacity(c); in bch2_dev_resize()
2034 up_write(&c->state_lock); in bch2_dev_resize()
2038 /* return with ref on ca->ref: */
2039 struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name) in bch2_dev_lookup() argument
2044 for_each_member_device(c, ca) in bch2_dev_lookup()
2045 if (!strcmp(name, ca->name)) in bch2_dev_lookup()
2047 return ERR_PTR(-BCH_ERR_ENOENT_dev_not_found); in bch2_dev_lookup()
2053 __releases(&bdev->bd_holder_lock) in bdev_get_fs()
2055 struct bch_sb_handle_holder *holder = bdev->bd_holder; in bdev_get_fs()
2056 struct bch_fs *c = holder->c; in bdev_get_fs() local
2058 if (c && !bch2_ro_ref_tryget(c)) in bdev_get_fs()
2059 c = NULL; in bdev_get_fs()
2061 mutex_unlock(&bdev->bd_holder_lock); in bdev_get_fs()
2063 if (c) in bdev_get_fs()
2064 wait_event(c->ro_ref_wait, test_bit(BCH_FS_started, &c->flags)); in bdev_get_fs()
2065 return c; in bdev_get_fs()
2068 /* returns with ref on ca->ref */
2069 static struct bch_dev *bdev_to_bch_dev(struct bch_fs *c, struct block_device *bdev) in bdev_to_bch_dev() argument
2071 for_each_member_device(c, ca) in bdev_to_bch_dev()
2072 if (ca->disk_sb.bdev == bdev) in bdev_to_bch_dev()
2079 struct bch_fs *c = bdev_get_fs(bdev); in bch2_fs_bdev_mark_dead() local
2080 if (!c) in bch2_fs_bdev_mark_dead()
2083 struct super_block *sb = c->vfs_sb; in bch2_fs_bdev_mark_dead()
2086 * Not necessary, c->ro_ref guards against the filesystem being in bch2_fs_bdev_mark_dead()
2087 * unmounted - we only take this to avoid a warning in in bch2_fs_bdev_mark_dead()
2090 down_read(&sb->s_umount); in bch2_fs_bdev_mark_dead()
2093 down_write(&c->state_lock); in bch2_fs_bdev_mark_dead()
2094 struct bch_dev *ca = bdev_to_bch_dev(c, bdev); in bch2_fs_bdev_mark_dead()
2098 if (bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, BCH_FORCE_IF_DEGRADED)) { in bch2_fs_bdev_mark_dead()
2099 __bch2_dev_offline(c, ca); in bch2_fs_bdev_mark_dead()
2108 bch2_journal_flush(&c->journal); in bch2_fs_bdev_mark_dead()
2109 bch2_fs_emergency_read_only(c); in bch2_fs_bdev_mark_dead()
2115 up_read(&sb->s_umount); in bch2_fs_bdev_mark_dead()
2116 up_write(&c->state_lock); in bch2_fs_bdev_mark_dead()
2117 bch2_ro_ref_put(c); in bch2_fs_bdev_mark_dead()
2122 struct bch_fs *c = bdev_get_fs(bdev); in bch2_fs_bdev_sync() local
2123 if (!c) in bch2_fs_bdev_sync()
2126 struct super_block *sb = c->vfs_sb; in bch2_fs_bdev_sync()
2129 * Not necessary, c->ro_ref guards against the filesystem being in bch2_fs_bdev_sync()
2130 * unmounted - we only take this to avoid a warning in in bch2_fs_bdev_sync()
2133 down_read(&sb->s_umount); in bch2_fs_bdev_sync()
2135 up_read(&sb->s_umount); in bch2_fs_bdev_sync()
2138 bch2_ro_ref_put(c); in bch2_fs_bdev_sync()
2150 return cmp_int(le64_to_cpu(l->seq), le64_to_cpu(r->seq)) ?: in sb_cmp()
2151 cmp_int(le64_to_cpu(l->write_time), le64_to_cpu(r->write_time)); in sb_cmp()
2158 struct bch_fs *c = NULL; in bch2_fs_open() local
2164 return ERR_PTR(-ENODEV); in bch2_fs_open()
2167 ret = -EINVAL; in bch2_fs_open()
2186 ret = -BCH_ERR_erofs_nochanges; in bch2_fs_open()
2191 if (!best || sb_cmp(sb->sb, best->sb) > 0) in bch2_fs_open()
2197 if (ret == -BCH_ERR_device_has_been_removed || in bch2_fs_open()
2198 ret == -BCH_ERR_device_splitbrain) { in bch2_fs_open()
2201 best -= best > sb; in bch2_fs_open()
2210 c = bch2_fs_alloc(best->sb, opts); in bch2_fs_open()
2211 ret = PTR_ERR_OR_ZERO(c); in bch2_fs_open()
2215 down_write(&c->state_lock); in bch2_fs_open()
2217 ret = bch2_dev_attach_bdev(c, sb); in bch2_fs_open()
2219 up_write(&c->state_lock); in bch2_fs_open()
2223 up_write(&c->state_lock); in bch2_fs_open()
2225 if (!c->opts.nostart) { in bch2_fs_open()
2226 ret = bch2_fs_start(c); in bch2_fs_open()
2236 return c; in bch2_fs_open()
2241 if (!IS_ERR_OR_NULL(c)) in bch2_fs_open()
2242 bch2_fs_stop(c); in bch2_fs_open()
2243 c = ERR_PTR(ret); in bch2_fs_open()
2273 return -ENOMEM; in bcachefs_init()