1 // SPDX-License-Identifier: GPL-2.0
3 * bcachefs setup/teardown code, and some metadata io - read a superblock and
4 * figure out what to do with it.
6 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
7 * Copyright 2012 Google, Inc.
11 #include "alloc_background.h"
12 #include "alloc_foreground.h"
13 #include "bkey_sort.h"
14 #include "btree_cache.h"
16 #include "btree_update_interior.h"
23 #include "disk_groups.h"
32 #include "journal_reclaim.h"
33 #include "journal_seq_blacklist.h"
38 #include "rebalance.h"
45 #include <linux/backing-dev.h>
46 #include <linux/blkdev.h>
47 #include <linux/debugfs.h>
48 #include <linux/device.h>
49 #include <linux/genhd.h>
50 #include <linux/idr.h>
51 #include <linux/kthread.h>
52 #include <linux/module.h>
53 #include <linux/percpu.h>
54 #include <linux/random.h>
55 #include <linux/sysfs.h>
56 #include <crypto/hash.h>
58 #include <trace/events/bcachefs.h>
60 MODULE_LICENSE("GPL");
61 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
64 struct kobj_type type ## _ktype = { \
65 .release = type ## _release, \
66 .sysfs_ops = &type ## _sysfs_ops, \
67 .default_attrs = type ## _files \
70 static void bch2_fs_release(struct kobject *);
71 static void bch2_dev_release(struct kobject *);
73 static void bch2_fs_internal_release(struct kobject *k)
77 static void bch2_fs_opts_dir_release(struct kobject *k)
81 static void bch2_fs_time_stats_release(struct kobject *k)
85 static KTYPE(bch2_fs);
86 static KTYPE(bch2_fs_internal);
87 static KTYPE(bch2_fs_opts_dir);
88 static KTYPE(bch2_fs_time_stats);
89 static KTYPE(bch2_dev);
91 static struct kset *bcachefs_kset;
92 static LIST_HEAD(bch_fs_list);
93 static DEFINE_MUTEX(bch_fs_list_lock);
95 static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
97 static void bch2_dev_free(struct bch_dev *);
98 static int bch2_dev_alloc(struct bch_fs *, unsigned);
99 static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *);
100 static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
102 struct bch_fs *bch2_bdev_to_fs(struct block_device *bdev)
108 mutex_lock(&bch_fs_list_lock);
111 list_for_each_entry(c, &bch_fs_list, list)
112 for_each_member_device_rcu(ca, c, i, NULL)
113 if (ca->disk_sb.bdev == bdev) {
120 mutex_unlock(&bch_fs_list_lock);
125 static struct bch_fs *__bch2_uuid_to_fs(uuid_le uuid)
129 lockdep_assert_held(&bch_fs_list_lock);
131 list_for_each_entry(c, &bch_fs_list, list)
132 if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid_le)))
138 struct bch_fs *bch2_uuid_to_fs(uuid_le uuid)
142 mutex_lock(&bch_fs_list_lock);
143 c = __bch2_uuid_to_fs(uuid);
146 mutex_unlock(&bch_fs_list_lock);
151 int bch2_congested(void *data, int bdi_bits)
153 struct bch_fs *c = data;
154 struct backing_dev_info *bdi;
160 if (bdi_bits & (1 << WB_sync_congested)) {
161 /* Reads - check all devices: */
162 for_each_readable_member(ca, c, i) {
163 bdi = ca->disk_sb.bdev->bd_bdi;
165 if (bdi_congested(bdi, bdi_bits)) {
171 unsigned target = READ_ONCE(c->opts.foreground_target);
172 const struct bch_devs_mask *devs = target
173 ? bch2_target_to_mask(c, target)
174 : &c->rw_devs[BCH_DATA_USER];
176 for_each_member_device_rcu(ca, c, i, devs) {
177 bdi = ca->disk_sb.bdev->bd_bdi;
179 if (bdi_congested(bdi, bdi_bits)) {
190 /* Filesystem RO/RW: */
193 * For startup/shutdown of RW stuff, the dependencies are:
195 * - foreground writes depend on copygc and rebalance (to free up space)
197 * - copygc and rebalance depend on mark and sweep gc (they actually probably
198 * don't because they either reserve ahead of time or don't block if
199 * allocations fail, but allocations can require mark and sweep gc to run
200 * because of generation number wraparound)
202 * - all of the above depends on the allocator threads
204 * - allocator depends on the journal (when it rewrites prios and gens)
207 static void __bch2_fs_read_only(struct bch_fs *c)
211 unsigned i, clean_passes = 0;
214 bch2_rebalance_stop(c);
216 for_each_member_device(ca, c, i)
217 bch2_copygc_stop(ca);
219 bch2_gc_thread_stop(c);
222 * Flush journal before stopping allocators, because flushing journal
223 * blacklist entries involves allocating new btree nodes:
225 bch2_journal_flush_all_pins(&c->journal);
227 if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags))
228 goto allocator_not_running;
233 ret = bch2_stripes_write(c, BTREE_INSERT_NOCHECK_RW, &wrote) ?:
234 bch2_alloc_write(c, BTREE_INSERT_NOCHECK_RW, &wrote);
236 if (ret && !test_bit(BCH_FS_EMERGENCY_RO, &c->flags))
237 bch2_fs_inconsistent(c, "error writing out alloc info %i", ret);
242 for_each_member_device(ca, c, i)
243 bch2_dev_allocator_quiesce(c, ca);
245 bch2_journal_flush_all_pins(&c->journal);
248 * We need to explicitly wait on btree interior updates to complete
249 * before stopping the journal, flushing all journal pins isn't
250 * sufficient, because in the BTREE_INTERIOR_UPDATING_ROOT case btree
251 * interior updates have to drop their journal pin before they're
254 closure_wait_event(&c->btree_interior_update_wait,
255 !bch2_btree_interior_updates_nr_pending(c));
257 clean_passes = wrote ? 0 : clean_passes + 1;
258 } while (clean_passes < 2);
259 allocator_not_running:
260 for_each_member_device(ca, c, i)
261 bch2_dev_allocator_stop(ca);
263 clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
265 bch2_fs_journal_stop(&c->journal);
267 /* XXX: mark super that alloc info is persistent */
270 * the journal kicks off btree writes via reclaim - wait for in flight
271 * writes after stopping journal:
273 if (test_bit(BCH_FS_EMERGENCY_RO, &c->flags))
274 bch2_btree_flush_all_writes(c);
276 bch2_btree_verify_flushed(c);
279 * After stopping journal:
281 for_each_member_device(ca, c, i)
282 bch2_dev_allocator_remove(c, ca);
285 static void bch2_writes_disabled(struct percpu_ref *writes)
287 struct bch_fs *c = container_of(writes, struct bch_fs, writes);
289 set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
290 wake_up(&bch_read_only_wait);
293 void bch2_fs_read_only(struct bch_fs *c)
295 if (!test_bit(BCH_FS_RW, &c->flags)) {
296 cancel_delayed_work_sync(&c->journal.reclaim_work);
300 BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
303 * Block new foreground-end write operations from starting - any new
304 * writes will return -EROFS:
306 * (This is really blocking new _allocations_, writes to previously
307 * allocated space can still happen until stopping the allocator in
308 * bch2_dev_allocator_stop()).
310 percpu_ref_kill(&c->writes);
312 cancel_work_sync(&c->ec_stripe_delete_work);
313 cancel_delayed_work(&c->pd_controllers_update);
316 * If we're not doing an emergency shutdown, we want to wait on
317 * outstanding writes to complete so they don't see spurious errors due
318 * to shutting down the allocator:
320 * If we are doing an emergency shutdown outstanding writes may
321 * hang until we shutdown the allocator so we don't want to wait
322 * on outstanding writes before shutting everything down - but
323 * we do need to wait on them before returning and signalling
324 * that going RO is complete:
326 wait_event(bch_read_only_wait,
327 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
328 test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
330 __bch2_fs_read_only(c);
332 wait_event(bch_read_only_wait,
333 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
335 clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
337 if (!bch2_journal_error(&c->journal) &&
338 !test_bit(BCH_FS_ERROR, &c->flags) &&
339 !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) &&
340 test_bit(BCH_FS_STARTED, &c->flags) &&
342 bch2_fs_mark_clean(c);
344 clear_bit(BCH_FS_RW, &c->flags);
347 static void bch2_fs_read_only_work(struct work_struct *work)
350 container_of(work, struct bch_fs, read_only_work);
352 mutex_lock(&c->state_lock);
353 bch2_fs_read_only(c);
354 mutex_unlock(&c->state_lock);
357 static void bch2_fs_read_only_async(struct bch_fs *c)
359 queue_work(system_long_wq, &c->read_only_work);
362 bool bch2_fs_emergency_read_only(struct bch_fs *c)
364 bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
366 bch2_fs_read_only_async(c);
367 bch2_journal_halt(&c->journal);
369 wake_up(&bch_read_only_wait);
373 static int bch2_fs_read_write_late(struct bch_fs *c)
379 ret = bch2_gc_thread_start(c);
381 bch_err(c, "error starting gc thread");
385 for_each_rw_member(ca, c, i) {
386 ret = bch2_copygc_start(c, ca);
388 bch_err(c, "error starting copygc threads");
389 percpu_ref_put(&ca->io_ref);
394 ret = bch2_rebalance_start(c);
396 bch_err(c, "error starting rebalance thread");
400 schedule_delayed_work(&c->pd_controllers_update, 5 * HZ);
402 schedule_work(&c->ec_stripe_delete_work);
407 static int __bch2_fs_read_write(struct bch_fs *c, bool early)
413 if (test_bit(BCH_FS_RW, &c->flags))
417 * nochanges is used for fsck -n mode - we have to allow going rw
418 * during recovery for that to work:
420 if (c->opts.norecovery ||
421 (c->opts.nochanges &&
422 (!early || c->opts.read_only)))
425 ret = bch2_fs_mark_dirty(c);
429 for_each_rw_member(ca, c, i)
430 bch2_dev_allocator_add(c, ca);
431 bch2_recalc_capacity(c);
433 if (!test_bit(BCH_FS_ALLOCATOR_STARTED, &c->flags)) {
434 ret = bch2_fs_allocator_start(c);
436 bch_err(c, "error initializing allocator");
440 set_bit(BCH_FS_ALLOCATOR_STARTED, &c->flags);
443 for_each_rw_member(ca, c, i) {
444 ret = bch2_dev_allocator_start(ca);
446 bch_err(c, "error starting allocator threads");
447 percpu_ref_put(&ca->io_ref);
452 set_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
455 ret = bch2_fs_read_write_late(c);
460 percpu_ref_reinit(&c->writes);
461 set_bit(BCH_FS_RW, &c->flags);
463 queue_delayed_work(c->journal_reclaim_wq,
464 &c->journal.reclaim_work, 0);
467 __bch2_fs_read_only(c);
471 int bch2_fs_read_write(struct bch_fs *c)
473 return __bch2_fs_read_write(c, false);
476 int bch2_fs_read_write_early(struct bch_fs *c)
478 lockdep_assert_held(&c->state_lock);
480 return __bch2_fs_read_write(c, true);
483 /* Filesystem startup/shutdown: */
485 static void bch2_fs_free(struct bch_fs *c)
489 for (i = 0; i < BCH_TIME_STAT_NR; i++)
490 bch2_time_stats_exit(&c->times[i]);
492 bch2_fs_quota_exit(c);
493 bch2_fs_fsio_exit(c);
495 bch2_fs_encryption_exit(c);
497 bch2_fs_btree_iter_exit(c);
498 bch2_fs_btree_cache_exit(c);
499 bch2_fs_journal_exit(&c->journal);
500 bch2_io_clock_exit(&c->io_clock[WRITE]);
501 bch2_io_clock_exit(&c->io_clock[READ]);
502 bch2_fs_compress_exit(c);
503 percpu_free_rwsem(&c->mark_lock);
504 kfree(c->usage_scratch);
505 free_percpu(c->usage[1]);
506 free_percpu(c->usage[0]);
507 kfree(c->usage_base);
508 free_percpu(c->pcpu);
509 mempool_exit(&c->large_bkey_pool);
510 mempool_exit(&c->btree_bounce_pool);
511 bioset_exit(&c->btree_bio);
512 mempool_exit(&c->btree_interior_update_pool);
513 mempool_exit(&c->btree_reserve_pool);
514 mempool_exit(&c->fill_iter);
515 percpu_ref_exit(&c->writes);
516 kfree(c->replicas.entries);
517 kfree(c->replicas_gc.entries);
518 kfree(rcu_dereference_protected(c->disk_groups, 1));
519 kfree(c->journal_seq_blacklist_table);
521 if (c->journal_reclaim_wq)
522 destroy_workqueue(c->journal_reclaim_wq);
524 destroy_workqueue(c->copygc_wq);
526 destroy_workqueue(c->wq);
528 free_pages((unsigned long) c->disk_sb.sb,
529 c->disk_sb.page_order);
530 kvpfree(c, sizeof(*c));
531 module_put(THIS_MODULE);
534 static void bch2_fs_release(struct kobject *kobj)
536 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
541 void bch2_fs_stop(struct bch_fs *c)
546 bch_verbose(c, "shutting down");
548 set_bit(BCH_FS_STOPPING, &c->flags);
550 cancel_work_sync(&c->journal_seq_blacklist_gc_work);
552 for_each_member_device(ca, c, i)
553 if (ca->kobj.state_in_sysfs &&
555 sysfs_remove_link(&part_to_dev(ca->disk_sb.bdev->bd_part)->kobj,
558 if (c->kobj.state_in_sysfs)
559 kobject_del(&c->kobj);
561 bch2_fs_debug_exit(c);
562 bch2_fs_chardev_exit(c);
564 kobject_put(&c->time_stats);
565 kobject_put(&c->opts_dir);
566 kobject_put(&c->internal);
568 mutex_lock(&bch_fs_list_lock);
570 mutex_unlock(&bch_fs_list_lock);
572 closure_sync(&c->cl);
573 closure_debug_destroy(&c->cl);
575 mutex_lock(&c->state_lock);
576 bch2_fs_read_only(c);
577 mutex_unlock(&c->state_lock);
579 /* btree prefetch might have kicked off reads in the background: */
580 bch2_btree_flush_all_reads(c);
582 for_each_member_device(ca, c, i)
583 cancel_work_sync(&ca->io_error_work);
585 cancel_work_sync(&c->btree_write_error_work);
586 cancel_delayed_work_sync(&c->pd_controllers_update);
587 cancel_work_sync(&c->read_only_work);
589 for (i = 0; i < c->sb.nr_devices; i++)
591 bch2_dev_free(rcu_dereference_protected(c->devs[i], 1));
593 bch_verbose(c, "shutdown complete");
595 kobject_put(&c->kobj);
598 static const char *bch2_fs_online(struct bch_fs *c)
601 const char *err = NULL;
605 lockdep_assert_held(&bch_fs_list_lock);
607 if (!list_empty(&c->list))
610 if (__bch2_uuid_to_fs(c->sb.uuid))
611 return "filesystem UUID already open";
613 ret = bch2_fs_chardev_init(c);
615 return "error creating character device";
617 bch2_fs_debug_init(c);
619 if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ||
620 kobject_add(&c->internal, &c->kobj, "internal") ||
621 kobject_add(&c->opts_dir, &c->kobj, "options") ||
622 kobject_add(&c->time_stats, &c->kobj, "time_stats") ||
623 bch2_opts_create_sysfs_files(&c->opts_dir))
624 return "error creating sysfs objects";
626 mutex_lock(&c->state_lock);
628 err = "error creating sysfs objects";
629 __for_each_member_device(ca, c, i, NULL)
630 if (bch2_dev_sysfs_online(c, ca))
633 list_add(&c->list, &bch_fs_list);
636 mutex_unlock(&c->state_lock);
640 static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
642 struct bch_sb_field_members *mi;
644 unsigned i, iter_size;
647 pr_verbose_init(opts, "");
649 c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO);
653 __module_get(THIS_MODULE);
656 c->disk_sb.fs_sb = true;
658 mutex_init(&c->state_lock);
659 mutex_init(&c->sb_lock);
660 mutex_init(&c->replicas_gc_lock);
661 mutex_init(&c->btree_root_lock);
662 INIT_WORK(&c->read_only_work, bch2_fs_read_only_work);
664 init_rwsem(&c->gc_lock);
666 for (i = 0; i < BCH_TIME_STAT_NR; i++)
667 bch2_time_stats_init(&c->times[i]);
669 bch2_fs_allocator_background_init(c);
670 bch2_fs_allocator_foreground_init(c);
671 bch2_fs_rebalance_init(c);
672 bch2_fs_quota_init(c);
674 INIT_LIST_HEAD(&c->list);
676 INIT_LIST_HEAD(&c->btree_interior_update_list);
677 mutex_init(&c->btree_reserve_cache_lock);
678 mutex_init(&c->btree_interior_update_lock);
680 mutex_init(&c->usage_scratch_lock);
682 mutex_init(&c->bio_bounce_pages_lock);
684 bio_list_init(&c->btree_write_error_list);
685 spin_lock_init(&c->btree_write_error_lock);
686 INIT_WORK(&c->btree_write_error_work, bch2_btree_write_error_work);
688 INIT_WORK(&c->journal_seq_blacklist_gc_work,
689 bch2_blacklist_entries_gc);
691 INIT_LIST_HEAD(&c->fsck_errors);
692 mutex_init(&c->fsck_error_lock);
694 INIT_LIST_HEAD(&c->ec_new_stripe_list);
695 mutex_init(&c->ec_new_stripe_lock);
696 mutex_init(&c->ec_stripe_create_lock);
697 spin_lock_init(&c->ec_stripes_heap_lock);
699 seqcount_init(&c->gc_pos_lock);
701 seqcount_init(&c->usage_lock);
703 c->copy_gc_enabled = 1;
704 c->rebalance.enabled = 1;
705 c->promote_whole_extents = true;
707 c->journal.write_time = &c->times[BCH_TIME_journal_write];
708 c->journal.delay_time = &c->times[BCH_TIME_journal_delay];
709 c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal];
710 c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq];
712 bch2_fs_btree_cache_init_early(&c->btree_cache);
714 if (percpu_init_rwsem(&c->mark_lock))
717 mutex_lock(&c->sb_lock);
719 if (bch2_sb_to_fs(c, sb)) {
720 mutex_unlock(&c->sb_lock);
724 mutex_unlock(&c->sb_lock);
726 scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid);
728 c->opts = bch2_opts_default;
729 bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb));
730 bch2_opts_apply(&c->opts, opts);
732 c->block_bits = ilog2(c->opts.block_size);
733 c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
735 if (bch2_fs_init_fault("fs_alloc"))
738 iter_size = sizeof(struct sort_iter) +
739 (btree_blocks(c) + 1) * 2 *
740 sizeof(struct sort_iter_set);
742 if (!(c->wq = alloc_workqueue("bcachefs",
743 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
744 !(c->copygc_wq = alloc_workqueue("bcache_copygc",
745 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
746 !(c->journal_reclaim_wq = alloc_workqueue("bcache_journal",
747 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
748 percpu_ref_init(&c->writes, bch2_writes_disabled,
749 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
750 mempool_init_kmalloc_pool(&c->btree_reserve_pool, 1,
751 sizeof(struct btree_reserve)) ||
752 mempool_init_kmalloc_pool(&c->btree_interior_update_pool, 1,
753 sizeof(struct btree_update)) ||
754 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
755 bioset_init(&c->btree_bio, 1,
756 max(offsetof(struct btree_read_bio, bio),
757 offsetof(struct btree_write_bio, wbio.bio)),
758 BIOSET_NEED_BVECS) ||
759 !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) ||
760 mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1,
762 mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) ||
763 bch2_io_clock_init(&c->io_clock[READ]) ||
764 bch2_io_clock_init(&c->io_clock[WRITE]) ||
765 bch2_fs_journal_init(&c->journal) ||
766 bch2_fs_replicas_init(c) ||
767 bch2_fs_btree_cache_init(c) ||
768 bch2_fs_btree_iter_init(c) ||
769 bch2_fs_io_init(c) ||
770 bch2_fs_encryption_init(c) ||
771 bch2_fs_compress_init(c) ||
772 bch2_fs_ec_init(c) ||
773 bch2_fs_fsio_init(c))
776 mi = bch2_sb_get_members(c->disk_sb.sb);
777 for (i = 0; i < c->sb.nr_devices; i++)
778 if (bch2_dev_exists(c->disk_sb.sb, mi, i) &&
779 bch2_dev_alloc(c, i))
783 * Now that all allocations have succeeded, init various refcounty
784 * things that let us shutdown:
786 closure_init(&c->cl, NULL);
788 c->kobj.kset = bcachefs_kset;
789 kobject_init(&c->kobj, &bch2_fs_ktype);
790 kobject_init(&c->internal, &bch2_fs_internal_ktype);
791 kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype);
792 kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype);
794 mutex_lock(&bch_fs_list_lock);
795 err = bch2_fs_online(c);
796 mutex_unlock(&bch_fs_list_lock);
798 bch_err(c, "bch2_fs_online() error: %s", err);
802 pr_verbose_init(opts, "ret %i", c ? 0 : -ENOMEM);
811 static void print_mount_opts(struct bch_fs *c)
815 struct printbuf p = PBUF(buf);
818 strcpy(buf, "(null)");
820 if (c->opts.read_only) {
825 for (i = 0; i < bch2_opts_nr; i++) {
826 const struct bch_option *opt = &bch2_opt_table[i];
827 u64 v = bch2_opt_get_by_id(&c->opts, i);
829 if (!(opt->mode & OPT_MOUNT))
832 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
838 bch2_opt_to_text(&p, c, opt, v, OPT_SHOW_MOUNT_STYLE);
841 bch_info(c, "mounted with opts: %s", buf);
844 int bch2_fs_start(struct bch_fs *c)
846 const char *err = "cannot allocate memory";
847 struct bch_sb_field_members *mi;
849 time64_t now = ktime_get_real_seconds();
853 mutex_lock(&c->state_lock);
855 BUG_ON(test_bit(BCH_FS_STARTED, &c->flags));
857 mutex_lock(&c->sb_lock);
859 for_each_online_member(ca, c, i)
860 bch2_sb_from_fs(c, ca);
862 mi = bch2_sb_get_members(c->disk_sb.sb);
863 for_each_online_member(ca, c, i)
864 mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
866 mutex_unlock(&c->sb_lock);
868 for_each_rw_member(ca, c, i)
869 bch2_dev_allocator_add(c, ca);
870 bch2_recalc_capacity(c);
872 ret = BCH_SB_INITIALIZED(c->disk_sb.sb)
873 ? bch2_fs_recovery(c)
874 : bch2_fs_initialize(c);
878 ret = bch2_opts_check_may_set(c);
882 err = "dynamic fault";
884 if (bch2_fs_init_fault("fs_start"))
887 if (c->opts.read_only || c->opts.nochanges) {
888 bch2_fs_read_only(c);
890 err = "error going read write";
891 ret = !test_bit(BCH_FS_RW, &c->flags)
892 ? bch2_fs_read_write(c)
893 : bch2_fs_read_write_late(c);
898 set_bit(BCH_FS_STARTED, &c->flags);
902 mutex_unlock(&c->state_lock);
906 case BCH_FSCK_ERRORS_NOT_FIXED:
907 bch_err(c, "filesystem contains errors: please report this to the developers");
908 pr_cont("mount with -o fix_errors to repair\n");
911 case BCH_FSCK_REPAIR_UNIMPLEMENTED:
912 bch_err(c, "filesystem contains errors: please report this to the developers");
913 pr_cont("repair unimplemented: inform the developers so that it can be added\n");
916 case BCH_FSCK_REPAIR_IMPOSSIBLE:
917 bch_err(c, "filesystem contains errors, but repair impossible");
920 case BCH_FSCK_UNKNOWN_VERSION:
921 err = "unknown metadata version";;
924 err = "cannot allocate memory";
936 static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
938 struct bch_sb_field_members *sb_mi;
940 sb_mi = bch2_sb_get_members(sb);
942 return "Invalid superblock: member info area missing";
944 if (le16_to_cpu(sb->block_size) != c->opts.block_size)
945 return "mismatched block size";
947 if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
948 BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
949 return "new cache bucket size is too small";
954 static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
956 struct bch_sb *newest =
957 le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
958 struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
960 if (uuid_le_cmp(fs->uuid, sb->uuid))
961 return "device not a member of filesystem";
963 if (!bch2_dev_exists(newest, mi, sb->dev_idx))
964 return "device has been removed";
966 if (fs->block_size != sb->block_size)
967 return "mismatched block size";
972 /* Device startup/shutdown: */
974 static void bch2_dev_release(struct kobject *kobj)
976 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
981 static void bch2_dev_free(struct bch_dev *ca)
983 cancel_work_sync(&ca->io_error_work);
985 if (ca->kobj.state_in_sysfs &&
987 sysfs_remove_link(&part_to_dev(ca->disk_sb.bdev->bd_part)->kobj,
990 if (ca->kobj.state_in_sysfs)
991 kobject_del(&ca->kobj);
993 bch2_free_super(&ca->disk_sb);
994 bch2_dev_journal_exit(ca);
996 free_percpu(ca->io_done);
997 bioset_exit(&ca->replica_set);
998 bch2_dev_buckets_free(ca);
999 free_page((unsigned long) ca->sb_read_scratch);
1001 bch2_time_stats_exit(&ca->io_latency[WRITE]);
1002 bch2_time_stats_exit(&ca->io_latency[READ]);
1004 percpu_ref_exit(&ca->io_ref);
1005 percpu_ref_exit(&ca->ref);
1006 kobject_put(&ca->kobj);
1009 static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca)
1012 lockdep_assert_held(&c->state_lock);
1014 if (percpu_ref_is_zero(&ca->io_ref))
1017 __bch2_dev_read_only(c, ca);
1019 reinit_completion(&ca->io_ref_completion);
1020 percpu_ref_kill(&ca->io_ref);
1021 wait_for_completion(&ca->io_ref_completion);
1023 if (ca->kobj.state_in_sysfs) {
1024 struct kobject *block =
1025 &part_to_dev(ca->disk_sb.bdev->bd_part)->kobj;
1027 sysfs_remove_link(block, "bcachefs");
1028 sysfs_remove_link(&ca->kobj, "block");
1031 bch2_free_super(&ca->disk_sb);
1032 bch2_dev_journal_exit(ca);
1035 static void bch2_dev_ref_complete(struct percpu_ref *ref)
1037 struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
1039 complete(&ca->ref_completion);
1042 static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
1044 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
1046 complete(&ca->io_ref_completion);
1049 static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca)
1053 if (!c->kobj.state_in_sysfs)
1056 if (!ca->kobj.state_in_sysfs) {
1057 ret = kobject_add(&ca->kobj, &c->kobj,
1058 "dev-%u", ca->dev_idx);
1063 if (ca->disk_sb.bdev) {
1064 struct kobject *block =
1065 &part_to_dev(ca->disk_sb.bdev->bd_part)->kobj;
1067 ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
1070 ret = sysfs_create_link(&ca->kobj, block, "block");
1078 static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c,
1079 struct bch_member *member)
1083 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1087 kobject_init(&ca->kobj, &bch2_dev_ktype);
1088 init_completion(&ca->ref_completion);
1089 init_completion(&ca->io_ref_completion);
1091 init_rwsem(&ca->bucket_lock);
1093 writepoint_init(&ca->copygc_write_point, BCH_DATA_USER);
1095 bch2_dev_copygc_init(ca);
1097 INIT_WORK(&ca->io_error_work, bch2_io_error_work);
1099 bch2_time_stats_init(&ca->io_latency[READ]);
1100 bch2_time_stats_init(&ca->io_latency[WRITE]);
1102 ca->mi = bch2_mi_to_cpu(member);
1103 ca->uuid = member->uuid;
1105 if (opt_defined(c->opts, discard))
1106 ca->mi.discard = opt_get(c->opts, discard);
1108 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
1110 percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
1111 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
1112 !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) ||
1113 bch2_dev_buckets_alloc(c, ca) ||
1114 bioset_init(&ca->replica_set, 4,
1115 offsetof(struct bch_write_bio, bio), 0) ||
1116 !(ca->io_done = alloc_percpu(*ca->io_done)))
1125 static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca,
1128 ca->dev_idx = dev_idx;
1129 __set_bit(ca->dev_idx, ca->self.d);
1130 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
1133 rcu_assign_pointer(c->devs[ca->dev_idx], ca);
1135 if (bch2_dev_sysfs_online(c, ca))
1136 pr_warn("error creating sysfs objects");
1139 static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
1141 struct bch_member *member =
1142 bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx;
1143 struct bch_dev *ca = NULL;
1146 pr_verbose_init(c->opts, "");
1148 if (bch2_fs_init_fault("dev_alloc"))
1151 ca = __bch2_dev_alloc(c, member);
1155 bch2_dev_attach(c, ca, dev_idx);
1157 pr_verbose_init(c->opts, "ret %i", ret);
1166 static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
1170 if (bch2_dev_is_online(ca)) {
1171 bch_err(ca, "already have device online in slot %u",
1176 if (get_capacity(sb->bdev->bd_disk) <
1177 ca->mi.bucket_size * ca->mi.nbuckets) {
1178 bch_err(ca, "cannot online: device too small");
1182 BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
1184 if (get_capacity(sb->bdev->bd_disk) <
1185 ca->mi.bucket_size * ca->mi.nbuckets) {
1186 bch_err(ca, "device too small");
1190 ret = bch2_dev_journal_init(ca, sb->sb);
1196 if (sb->mode & FMODE_EXCL)
1197 ca->disk_sb.bdev->bd_holder = ca;
1198 memset(sb, 0, sizeof(*sb));
1200 percpu_ref_reinit(&ca->io_ref);
1205 static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb)
1210 lockdep_assert_held(&c->state_lock);
1212 if (le64_to_cpu(sb->sb->seq) >
1213 le64_to_cpu(c->disk_sb.sb->seq))
1214 bch2_sb_to_fs(c, sb->sb);
1216 BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
1217 !c->devs[sb->sb->dev_idx]);
1219 ca = bch_dev_locked(c, sb->sb->dev_idx);
1221 ret = __bch2_dev_attach_bdev(ca, sb);
1225 if (test_bit(BCH_FS_ALLOC_READ_DONE, &c->flags) &&
1226 !percpu_u64_get(&ca->usage[0]->buckets[BCH_DATA_SB])) {
1227 mutex_lock(&c->sb_lock);
1228 bch2_mark_dev_superblock(ca->fs, ca, 0);
1229 mutex_unlock(&c->sb_lock);
1232 bch2_dev_sysfs_online(c, ca);
1234 if (c->sb.nr_devices == 1)
1235 bdevname(ca->disk_sb.bdev, c->name);
1236 bdevname(ca->disk_sb.bdev, ca->name);
1238 rebalance_wakeup(c);
1242 /* Device management: */
1245 * Note: this function is also used by the error paths - when a particular
1246 * device sees an error, we call it to determine whether we can just set the
1247 * device RO, or - if this function returns false - we'll set the whole
1250 * XXX: maybe we should be more explicit about whether we're changing state
1251 * because we got an error or what have you?
1253 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
1254 enum bch_member_state new_state, int flags)
1256 struct bch_devs_mask new_online_devs;
1257 struct replicas_status s;
1258 struct bch_dev *ca2;
1259 int i, nr_rw = 0, required;
1261 lockdep_assert_held(&c->state_lock);
1263 switch (new_state) {
1264 case BCH_MEMBER_STATE_RW:
1266 case BCH_MEMBER_STATE_RO:
1267 if (ca->mi.state != BCH_MEMBER_STATE_RW)
1270 /* do we have enough devices to write to? */
1271 for_each_member_device(ca2, c, i)
1273 nr_rw += ca2->mi.state == BCH_MEMBER_STATE_RW;
1275 required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
1276 ? c->opts.metadata_replicas
1277 : c->opts.metadata_replicas_required,
1278 !(flags & BCH_FORCE_IF_DATA_DEGRADED)
1279 ? c->opts.data_replicas
1280 : c->opts.data_replicas_required);
1282 return nr_rw >= required;
1283 case BCH_MEMBER_STATE_FAILED:
1284 case BCH_MEMBER_STATE_SPARE:
1285 if (ca->mi.state != BCH_MEMBER_STATE_RW &&
1286 ca->mi.state != BCH_MEMBER_STATE_RO)
1289 /* do we have enough devices to read from? */
1290 new_online_devs = bch2_online_devs(c);
1291 __clear_bit(ca->dev_idx, new_online_devs.d);
1293 s = __bch2_replicas_status(c, new_online_devs);
1295 return bch2_have_enough_devs(s, flags);
1301 static bool bch2_fs_may_start(struct bch_fs *c)
1303 struct replicas_status s;
1304 struct bch_sb_field_members *mi;
1306 unsigned i, flags = c->opts.degraded
1307 ? BCH_FORCE_IF_DEGRADED
1310 if (!c->opts.degraded) {
1311 mutex_lock(&c->sb_lock);
1312 mi = bch2_sb_get_members(c->disk_sb.sb);
1314 for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
1315 if (!bch2_dev_exists(c->disk_sb.sb, mi, i))
1318 ca = bch_dev_locked(c, i);
1320 if (!bch2_dev_is_online(ca) &&
1321 (ca->mi.state == BCH_MEMBER_STATE_RW ||
1322 ca->mi.state == BCH_MEMBER_STATE_RO)) {
1323 mutex_unlock(&c->sb_lock);
1327 mutex_unlock(&c->sb_lock);
1330 s = bch2_replicas_status(c);
1332 return bch2_have_enough_devs(s, flags);
1335 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
1337 bch2_copygc_stop(ca);
1340 * The allocator thread itself allocates btree nodes, so stop it first:
1342 bch2_dev_allocator_stop(ca);
1343 bch2_dev_allocator_remove(c, ca);
1344 bch2_dev_journal_stop(&c->journal, ca);
1347 static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
1349 lockdep_assert_held(&c->state_lock);
1351 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_RW);
1353 bch2_dev_allocator_add(c, ca);
1354 bch2_recalc_capacity(c);
1356 if (bch2_dev_allocator_start(ca))
1357 return "error starting allocator thread";
1359 if (bch2_copygc_start(c, ca))
1360 return "error starting copygc thread";
1365 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1366 enum bch_member_state new_state, int flags)
1368 struct bch_sb_field_members *mi;
1371 if (ca->mi.state == new_state)
1374 if (!bch2_dev_state_allowed(c, ca, new_state, flags))
1377 if (new_state != BCH_MEMBER_STATE_RW)
1378 __bch2_dev_read_only(c, ca);
1380 bch_notice(ca, "%s", bch2_dev_state[new_state]);
1382 mutex_lock(&c->sb_lock);
1383 mi = bch2_sb_get_members(c->disk_sb.sb);
1384 SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
1385 bch2_write_super(c);
1386 mutex_unlock(&c->sb_lock);
1388 if (new_state == BCH_MEMBER_STATE_RW &&
1389 __bch2_dev_read_write(c, ca))
1392 rebalance_wakeup(c);
1397 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1398 enum bch_member_state new_state, int flags)
1402 mutex_lock(&c->state_lock);
1403 ret = __bch2_dev_set_state(c, ca, new_state, flags);
1404 mutex_unlock(&c->state_lock);
1409 /* Device add/removal: */
1411 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
1413 struct bch_sb_field_members *mi;
1414 unsigned dev_idx = ca->dev_idx, data;
1417 mutex_lock(&c->state_lock);
1419 percpu_ref_put(&ca->ref); /* XXX */
1421 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1422 bch_err(ca, "Cannot remove without losing data");
1426 __bch2_dev_read_only(c, ca);
1429 * XXX: verify that dev_idx is really not in use anymore, anywhere
1431 * flag_data_bad() does not check btree pointers
1433 ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
1435 bch_err(ca, "Remove failed: error %i dropping data", ret);
1439 ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx);
1441 bch_err(ca, "Remove failed: error %i flushing journal", ret);
1445 data = bch2_dev_has_data(c, ca);
1447 char data_has_str[100];
1449 bch2_flags_to_text(&PBUF(data_has_str),
1450 bch2_data_types, data);
1451 bch_err(ca, "Remove failed, still has data (%s)", data_has_str);
1456 ret = bch2_btree_delete_range(c, BTREE_ID_ALLOC,
1457 POS(ca->dev_idx, 0),
1458 POS(ca->dev_idx + 1, 0),
1461 bch_err(ca, "Remove failed, error deleting alloc info");
1466 * must flush all existing journal entries, they might have
1467 * (overwritten) keys that point to the device we're removing:
1469 bch2_journal_flush_all_pins(&c->journal);
1470 ret = bch2_journal_error(&c->journal);
1472 bch_err(ca, "Remove failed, journal error");
1476 __bch2_dev_offline(c, ca);
1478 mutex_lock(&c->sb_lock);
1479 rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
1480 mutex_unlock(&c->sb_lock);
1482 percpu_ref_kill(&ca->ref);
1483 wait_for_completion(&ca->ref_completion);
1488 * Free this device's slot in the bch_member array - all pointers to
1489 * this device must be gone:
1491 mutex_lock(&c->sb_lock);
1492 mi = bch2_sb_get_members(c->disk_sb.sb);
1493 memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
1495 bch2_write_super(c);
1497 mutex_unlock(&c->sb_lock);
1498 mutex_unlock(&c->state_lock);
1501 if (ca->mi.state == BCH_MEMBER_STATE_RW &&
1502 !percpu_ref_is_zero(&ca->io_ref))
1503 __bch2_dev_read_write(c, ca);
1504 mutex_unlock(&c->state_lock);
1508 static void dev_usage_clear(struct bch_dev *ca)
1510 struct bucket_array *buckets;
1512 percpu_memset(ca->usage[0], 0, sizeof(*ca->usage[0]));
1514 down_read(&ca->bucket_lock);
1515 buckets = bucket_array(ca);
1517 memset(buckets->b, 0, sizeof(buckets->b[0]) * buckets->nbuckets);
1518 up_read(&ca->bucket_lock);
1521 /* Add new device to running filesystem: */
1522 int bch2_dev_add(struct bch_fs *c, const char *path)
1524 struct bch_opts opts = bch2_opts_empty();
1525 struct bch_sb_handle sb;
1527 struct bch_dev *ca = NULL;
1528 struct bch_sb_field_members *mi;
1529 struct bch_member dev_mi;
1530 unsigned dev_idx, nr_devices, u64s;
1533 ret = bch2_read_super(path, &opts, &sb);
1537 err = bch2_sb_validate(&sb);
1541 dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx];
1543 err = bch2_dev_may_add(sb.sb, c);
1547 ca = __bch2_dev_alloc(c, &dev_mi);
1549 bch2_free_super(&sb);
1553 ret = __bch2_dev_attach_bdev(ca, &sb);
1560 * We want to allocate journal on the new device before adding the new
1561 * device to the filesystem because allocating after we attach requires
1562 * spinning up the allocator thread, and the allocator thread requires
1563 * doing btree writes, which if the existing devices are RO isn't going
1566 * So we have to mark where the superblocks are, but marking allocated
1567 * data normally updates the filesystem usage too, so we have to mark,
1568 * allocate the journal, reset all the marks, then remark after we
1571 bch2_mark_dev_superblock(ca->fs, ca, 0);
1573 err = "journal alloc failed";
1574 ret = bch2_dev_journal_alloc(ca);
1578 dev_usage_clear(ca);
1580 mutex_lock(&c->state_lock);
1581 mutex_lock(&c->sb_lock);
1583 err = "insufficient space in new superblock";
1584 ret = bch2_sb_from_fs(c, ca);
1588 mi = bch2_sb_get_members(ca->disk_sb.sb);
1590 if (!bch2_sb_resize_members(&ca->disk_sb,
1591 le32_to_cpu(mi->field.u64s) +
1592 sizeof(dev_mi) / sizeof(u64))) {
1597 if (dynamic_fault("bcachefs:add:no_slot"))
1600 mi = bch2_sb_get_members(c->disk_sb.sb);
1601 for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
1602 if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx))
1605 err = "no slots available in superblock";
1610 nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
1611 u64s = (sizeof(struct bch_sb_field_members) +
1612 sizeof(struct bch_member) * nr_devices) / sizeof(u64);
1614 err = "no space in superblock for member info";
1617 mi = bch2_sb_resize_members(&c->disk_sb, u64s);
1623 mi->members[dev_idx] = dev_mi;
1624 mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds());
1625 c->disk_sb.sb->nr_devices = nr_devices;
1627 ca->disk_sb.sb->dev_idx = dev_idx;
1628 bch2_dev_attach(c, ca, dev_idx);
1630 bch2_mark_dev_superblock(c, ca, 0);
1632 bch2_write_super(c);
1633 mutex_unlock(&c->sb_lock);
1635 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1636 err = __bch2_dev_read_write(c, ca);
1641 mutex_unlock(&c->state_lock);
1645 mutex_unlock(&c->sb_lock);
1646 mutex_unlock(&c->state_lock);
1650 bch2_free_super(&sb);
1651 bch_err(c, "Unable to add device: %s", err);
1654 bch_err(c, "Error going rw after adding device: %s", err);
1658 /* Hot add existing device to running filesystem: */
1659 int bch2_dev_online(struct bch_fs *c, const char *path)
1661 struct bch_opts opts = bch2_opts_empty();
1662 struct bch_sb_handle sb = { NULL };
1663 struct bch_sb_field_members *mi;
1669 mutex_lock(&c->state_lock);
1671 ret = bch2_read_super(path, &opts, &sb);
1673 mutex_unlock(&c->state_lock);
1677 dev_idx = sb.sb->dev_idx;
1679 err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
1683 if (bch2_dev_attach_bdev(c, &sb)) {
1684 err = "bch2_dev_attach_bdev() error";
1688 ca = bch_dev_locked(c, dev_idx);
1689 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1690 err = __bch2_dev_read_write(c, ca);
1695 mutex_lock(&c->sb_lock);
1696 mi = bch2_sb_get_members(c->disk_sb.sb);
1698 mi->members[ca->dev_idx].last_mount =
1699 cpu_to_le64(ktime_get_real_seconds());
1701 bch2_write_super(c);
1702 mutex_unlock(&c->sb_lock);
1704 mutex_unlock(&c->state_lock);
1707 mutex_unlock(&c->state_lock);
1708 bch2_free_super(&sb);
1709 bch_err(c, "error bringing %s online: %s", path, err);
1713 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
1715 mutex_lock(&c->state_lock);
1717 if (!bch2_dev_is_online(ca)) {
1718 bch_err(ca, "Already offline");
1719 mutex_unlock(&c->state_lock);
1723 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1724 bch_err(ca, "Cannot offline required disk");
1725 mutex_unlock(&c->state_lock);
1729 __bch2_dev_offline(c, ca);
1731 mutex_unlock(&c->state_lock);
1735 int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
1737 struct bch_member *mi;
1740 mutex_lock(&c->state_lock);
1742 if (nbuckets < ca->mi.nbuckets) {
1743 bch_err(ca, "Cannot shrink yet");
1748 if (bch2_dev_is_online(ca) &&
1749 get_capacity(ca->disk_sb.bdev->bd_disk) <
1750 ca->mi.bucket_size * nbuckets) {
1751 bch_err(ca, "New size larger than device");
1756 ret = bch2_dev_buckets_resize(c, ca, nbuckets);
1758 bch_err(ca, "Resize error: %i", ret);
1762 mutex_lock(&c->sb_lock);
1763 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1764 mi->nbuckets = cpu_to_le64(nbuckets);
1766 bch2_write_super(c);
1767 mutex_unlock(&c->sb_lock);
1769 bch2_recalc_capacity(c);
1771 mutex_unlock(&c->state_lock);
1775 /* return with ref on ca->ref: */
1776 struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path)
1779 struct block_device *bdev = lookup_bdev(path);
1784 return ERR_CAST(bdev);
1786 for_each_member_device(ca, c, i)
1787 if (ca->disk_sb.bdev == bdev)
1790 ca = ERR_PTR(-ENOENT);
1796 /* Filesystem open: */
1798 struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
1799 struct bch_opts opts)
1801 struct bch_sb_handle *sb = NULL;
1802 struct bch_fs *c = NULL;
1803 unsigned i, best_sb = 0;
1807 pr_verbose_init(opts, "");
1810 c = ERR_PTR(-EINVAL);
1814 if (!try_module_get(THIS_MODULE)) {
1815 c = ERR_PTR(-ENODEV);
1819 sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
1823 for (i = 0; i < nr_devices; i++) {
1824 ret = bch2_read_super(devices[i], &opts, &sb[i]);
1828 err = bch2_sb_validate(&sb[i]);
1833 for (i = 1; i < nr_devices; i++)
1834 if (le64_to_cpu(sb[i].sb->seq) >
1835 le64_to_cpu(sb[best_sb].sb->seq))
1838 for (i = 0; i < nr_devices; i++) {
1839 err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
1845 c = bch2_fs_alloc(sb[best_sb].sb, opts);
1849 err = "bch2_dev_online() error";
1850 mutex_lock(&c->state_lock);
1851 for (i = 0; i < nr_devices; i++)
1852 if (bch2_dev_attach_bdev(c, &sb[i])) {
1853 mutex_unlock(&c->state_lock);
1856 mutex_unlock(&c->state_lock);
1858 err = "insufficient devices";
1859 if (!bch2_fs_may_start(c))
1862 if (!c->opts.nostart) {
1863 ret = bch2_fs_start(c);
1869 module_put(THIS_MODULE);
1871 pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
1874 pr_err("bch_fs_open err opening %s: %s",
1880 for (i = 0; i < nr_devices; i++)
1881 bch2_free_super(&sb[i]);
1886 static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb,
1887 struct bch_opts opts)
1891 bool allocated_fs = false;
1894 err = bch2_sb_validate(sb);
1898 mutex_lock(&bch_fs_list_lock);
1899 c = __bch2_uuid_to_fs(sb->sb->uuid);
1901 closure_get(&c->cl);
1903 err = bch2_dev_in_fs(c->disk_sb.sb, sb->sb);
1907 c = bch2_fs_alloc(sb->sb, opts);
1908 err = "cannot allocate memory";
1912 allocated_fs = true;
1915 err = "bch2_dev_online() error";
1917 mutex_lock(&c->sb_lock);
1918 if (bch2_dev_attach_bdev(c, sb)) {
1919 mutex_unlock(&c->sb_lock);
1922 mutex_unlock(&c->sb_lock);
1924 if (!c->opts.nostart && bch2_fs_may_start(c)) {
1925 err = "error starting filesystem";
1926 ret = bch2_fs_start(c);
1931 closure_put(&c->cl);
1932 mutex_unlock(&bch_fs_list_lock);
1936 mutex_unlock(&bch_fs_list_lock);
1941 closure_put(&c->cl);
1946 const char *bch2_fs_open_incremental(const char *path)
1948 struct bch_sb_handle sb;
1949 struct bch_opts opts = bch2_opts_empty();
1952 if (bch2_read_super(path, &opts, &sb))
1953 return "error reading superblock";
1955 err = __bch2_fs_open_incremental(&sb, opts);
1956 bch2_free_super(&sb);
1961 /* Global interfaces/init */
1963 static void bcachefs_exit(void)
1967 bch2_chardev_exit();
1969 kset_unregister(bcachefs_kset);
1972 static int __init bcachefs_init(void)
1974 bch2_bkey_pack_test();
1975 bch2_inode_pack_test();
1977 if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
1978 bch2_chardev_init() ||
1989 #define BCH_DEBUG_PARAM(name, description) \
1991 module_param_named(name, bch2_##name, bool, 0644); \
1992 MODULE_PARM_DESC(name, description);
1994 #undef BCH_DEBUG_PARAM
1996 module_exit(bcachefs_exit);
1997 module_init(bcachefs_init);