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"
46 #include <linux/backing-dev.h>
47 #include <linux/blkdev.h>
48 #include <linux/debugfs.h>
49 #include <linux/device.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 MODULE_LICENSE("GPL");
59 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
62 static const struct attribute_group type ## _group = { \
63 .attrs = type ## _files \
66 static const struct attribute_group *type ## _groups[] = { \
71 static const struct kobj_type type ## _ktype = { \
72 .release = type ## _release, \
73 .sysfs_ops = &type ## _sysfs_ops, \
74 .default_groups = type ## _groups \
77 static void bch2_fs_release(struct kobject *);
78 static void bch2_dev_release(struct kobject *);
80 static void bch2_fs_internal_release(struct kobject *k)
84 static void bch2_fs_opts_dir_release(struct kobject *k)
88 static void bch2_fs_time_stats_release(struct kobject *k)
93 KTYPE(bch2_fs_internal);
94 KTYPE(bch2_fs_opts_dir);
95 KTYPE(bch2_fs_time_stats);
98 static struct kset *bcachefs_kset;
99 static LIST_HEAD(bch_fs_list);
100 static DEFINE_MUTEX(bch_fs_list_lock);
102 static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
104 static void bch2_dev_free(struct bch_dev *);
105 static int bch2_dev_alloc(struct bch_fs *, unsigned);
106 static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *);
107 static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
109 struct bch_fs *bch2_dev_to_fs(dev_t dev)
115 mutex_lock(&bch_fs_list_lock);
118 list_for_each_entry(c, &bch_fs_list, list)
119 for_each_member_device_rcu(ca, c, i, NULL)
120 if (ca->disk_sb.bdev->bd_dev == dev) {
127 mutex_unlock(&bch_fs_list_lock);
132 static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid)
136 lockdep_assert_held(&bch_fs_list_lock);
138 list_for_each_entry(c, &bch_fs_list, list)
139 if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid)))
145 struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid)
149 mutex_lock(&bch_fs_list_lock);
150 c = __bch2_uuid_to_fs(uuid);
153 mutex_unlock(&bch_fs_list_lock);
158 /* Filesystem RO/RW: */
161 * For startup/shutdown of RW stuff, the dependencies are:
163 * - foreground writes depend on copygc and rebalance (to free up space)
165 * - copygc and rebalance depend on mark and sweep gc (they actually probably
166 * don't because they either reserve ahead of time or don't block if
167 * allocations fail, but allocations can require mark and sweep gc to run
168 * because of generation number wraparound)
170 * - all of the above depends on the allocator threads
172 * - allocator depends on the journal (when it rewrites prios and gens)
175 static void __bch2_fs_read_only(struct bch_fs *c)
179 unsigned i, clean_passes = 0;
182 bch2_rebalance_stop(c);
184 for_each_member_device(ca, c, i)
185 bch2_copygc_stop(ca);
187 bch2_gc_thread_stop(c);
190 * Flush journal before stopping allocators, because flushing journal
191 * blacklist entries involves allocating new btree nodes:
193 bch2_journal_flush_all_pins(&c->journal);
195 if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags))
196 goto allocator_not_running;
199 ret = bch2_stripes_write(c, &wrote);
201 bch2_fs_inconsistent(c, "error writing out stripes");
205 ret = bch2_alloc_write(c, false, &wrote);
207 bch2_fs_inconsistent(c, "error writing out alloc info %i", ret);
211 for_each_member_device(ca, c, i)
212 bch2_dev_allocator_quiesce(c, ca);
214 bch2_journal_flush_all_pins(&c->journal);
217 * We need to explicitly wait on btree interior updates to complete
218 * before stopping the journal, flushing all journal pins isn't
219 * sufficient, because in the BTREE_INTERIOR_UPDATING_ROOT case btree
220 * interior updates have to drop their journal pin before they're
223 closure_wait_event(&c->btree_interior_update_wait,
224 !bch2_btree_interior_updates_nr_pending(c));
226 clean_passes = wrote ? 0 : clean_passes + 1;
227 } while (clean_passes < 2);
228 allocator_not_running:
229 for_each_member_device(ca, c, i)
230 bch2_dev_allocator_stop(ca);
232 clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
234 bch2_fs_journal_stop(&c->journal);
236 /* XXX: mark super that alloc info is persistent */
239 * the journal kicks off btree writes via reclaim - wait for in flight
240 * writes after stopping journal:
242 if (test_bit(BCH_FS_EMERGENCY_RO, &c->flags))
243 bch2_btree_flush_all_writes(c);
245 bch2_btree_verify_flushed(c);
248 * After stopping journal:
250 for_each_member_device(ca, c, i)
251 bch2_dev_allocator_remove(c, ca);
254 static void bch2_writes_disabled(struct percpu_ref *writes)
256 struct bch_fs *c = container_of(writes, struct bch_fs, writes);
258 set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
259 wake_up(&bch_read_only_wait);
262 void bch2_fs_read_only(struct bch_fs *c)
264 if (!test_bit(BCH_FS_RW, &c->flags)) {
265 cancel_delayed_work_sync(&c->journal.reclaim_work);
269 BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
272 * Block new foreground-end write operations from starting - any new
273 * writes will return -EROFS:
275 * (This is really blocking new _allocations_, writes to previously
276 * allocated space can still happen until stopping the allocator in
277 * bch2_dev_allocator_stop()).
279 percpu_ref_kill(&c->writes);
281 cancel_delayed_work(&c->pd_controllers_update);
284 * If we're not doing an emergency shutdown, we want to wait on
285 * outstanding writes to complete so they don't see spurious errors due
286 * to shutting down the allocator:
288 * If we are doing an emergency shutdown outstanding writes may
289 * hang until we shutdown the allocator so we don't want to wait
290 * on outstanding writes before shutting everything down - but
291 * we do need to wait on them before returning and signalling
292 * that going RO is complete:
294 wait_event(bch_read_only_wait,
295 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
296 test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
298 __bch2_fs_read_only(c);
300 wait_event(bch_read_only_wait,
301 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
303 clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
305 if (!bch2_journal_error(&c->journal) &&
306 !test_bit(BCH_FS_ERROR, &c->flags) &&
307 !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) &&
308 test_bit(BCH_FS_STARTED, &c->flags))
309 bch2_fs_mark_clean(c);
311 clear_bit(BCH_FS_RW, &c->flags);
314 static void bch2_fs_read_only_work(struct work_struct *work)
317 container_of(work, struct bch_fs, read_only_work);
319 mutex_lock(&c->state_lock);
320 bch2_fs_read_only(c);
321 mutex_unlock(&c->state_lock);
324 static void bch2_fs_read_only_async(struct bch_fs *c)
326 queue_work(system_long_wq, &c->read_only_work);
329 bool bch2_fs_emergency_read_only(struct bch_fs *c)
331 bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
333 bch2_fs_read_only_async(c);
334 bch2_journal_halt(&c->journal);
336 wake_up(&bch_read_only_wait);
340 static int bch2_fs_read_write_late(struct bch_fs *c)
346 ret = bch2_gc_thread_start(c);
348 bch_err(c, "error starting gc thread");
352 for_each_rw_member(ca, c, i) {
353 ret = bch2_copygc_start(c, ca);
355 bch_err(c, "error starting copygc threads");
356 percpu_ref_put(&ca->io_ref);
361 ret = bch2_rebalance_start(c);
363 bch_err(c, "error starting rebalance thread");
367 schedule_delayed_work(&c->pd_controllers_update, 5 * HZ);
372 int __bch2_fs_read_write(struct bch_fs *c, bool early)
378 if (test_bit(BCH_FS_RW, &c->flags))
381 if (c->opts.nochanges ||
385 ret = bch2_fs_mark_dirty(c);
389 for_each_rw_member(ca, c, i)
390 bch2_dev_allocator_add(c, ca);
391 bch2_recalc_capacity(c);
393 if (!test_bit(BCH_FS_ALLOCATOR_STARTED, &c->flags)) {
394 ret = bch2_fs_allocator_start(c);
396 bch_err(c, "error initializing allocator");
400 set_bit(BCH_FS_ALLOCATOR_STARTED, &c->flags);
403 for_each_rw_member(ca, c, i) {
404 ret = bch2_dev_allocator_start(ca);
406 bch_err(c, "error starting allocator threads");
407 percpu_ref_put(&ca->io_ref);
412 set_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
415 ret = bch2_fs_read_write_late(c);
420 percpu_ref_reinit(&c->writes);
421 set_bit(BCH_FS_RW, &c->flags);
423 queue_delayed_work(c->journal_reclaim_wq,
424 &c->journal.reclaim_work, 0);
427 __bch2_fs_read_only(c);
431 int bch2_fs_read_write(struct bch_fs *c)
433 return __bch2_fs_read_write(c, false);
436 int bch2_fs_read_write_early(struct bch_fs *c)
438 lockdep_assert_held(&c->state_lock);
440 return __bch2_fs_read_write(c, true);
443 /* Filesystem startup/shutdown: */
445 static void bch2_fs_free(struct bch_fs *c)
449 for (i = 0; i < BCH_TIME_STAT_NR; i++)
450 bch2_time_stats_exit(&c->times[i]);
452 bch2_fs_quota_exit(c);
453 bch2_fs_fsio_exit(c);
455 bch2_fs_encryption_exit(c);
457 bch2_fs_btree_cache_exit(c);
458 bch2_fs_journal_exit(&c->journal);
459 bch2_io_clock_exit(&c->io_clock[WRITE]);
460 bch2_io_clock_exit(&c->io_clock[READ]);
461 bch2_fs_compress_exit(c);
462 percpu_free_rwsem(&c->mark_lock);
463 kfree(c->usage_scratch);
464 free_percpu(c->usage[0]);
465 free_percpu(c->pcpu);
466 mempool_exit(&c->btree_iters_pool);
467 mempool_exit(&c->btree_bounce_pool);
468 bioset_exit(&c->btree_bio);
469 mempool_exit(&c->btree_interior_update_pool);
470 mempool_exit(&c->btree_reserve_pool);
471 mempool_exit(&c->fill_iter);
472 percpu_ref_exit(&c->writes);
473 kfree(c->replicas.entries);
474 kfree(c->replicas_gc.entries);
475 kfree(rcu_dereference_protected(c->disk_groups, 1));
476 kfree(c->journal_seq_blacklist_table);
478 if (c->journal_reclaim_wq)
479 destroy_workqueue(c->journal_reclaim_wq);
481 destroy_workqueue(c->copygc_wq);
483 destroy_workqueue(c->wq);
485 free_pages((unsigned long) c->disk_sb.sb,
486 c->disk_sb.page_order);
487 kvpfree(c, sizeof(*c));
488 module_put(THIS_MODULE);
491 static void bch2_fs_release(struct kobject *kobj)
493 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
498 void bch2_fs_stop(struct bch_fs *c)
503 bch_verbose(c, "shutting down");
505 set_bit(BCH_FS_STOPPING, &c->flags);
507 cancel_work_sync(&c->journal_seq_blacklist_gc_work);
509 for_each_member_device(ca, c, i)
510 if (ca->kobj.state_in_sysfs &&
512 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
514 if (c->kobj.state_in_sysfs)
515 kobject_del(&c->kobj);
517 bch2_fs_debug_exit(c);
518 bch2_fs_chardev_exit(c);
520 kobject_put(&c->time_stats);
521 kobject_put(&c->opts_dir);
522 kobject_put(&c->internal);
524 mutex_lock(&bch_fs_list_lock);
526 mutex_unlock(&bch_fs_list_lock);
528 closure_sync(&c->cl);
529 closure_debug_destroy(&c->cl);
531 mutex_lock(&c->state_lock);
532 bch2_fs_read_only(c);
533 mutex_unlock(&c->state_lock);
535 /* btree prefetch might have kicked off reads in the background: */
536 bch2_btree_flush_all_reads(c);
538 for_each_member_device(ca, c, i)
539 cancel_work_sync(&ca->io_error_work);
541 cancel_work_sync(&c->btree_write_error_work);
542 cancel_delayed_work_sync(&c->pd_controllers_update);
543 cancel_work_sync(&c->read_only_work);
545 for (i = 0; i < c->sb.nr_devices; i++)
547 bch2_dev_free(rcu_dereference_protected(c->devs[i], 1));
549 bch_verbose(c, "shutdown complete");
551 kobject_put(&c->kobj);
554 static const char *bch2_fs_online(struct bch_fs *c)
557 const char *err = NULL;
561 lockdep_assert_held(&bch_fs_list_lock);
563 if (!list_empty(&c->list))
566 if (__bch2_uuid_to_fs(c->sb.uuid))
567 return "filesystem UUID already open";
569 ret = bch2_fs_chardev_init(c);
571 return "error creating character device";
573 bch2_fs_debug_init(c);
575 if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ||
576 kobject_add(&c->internal, &c->kobj, "internal") ||
577 kobject_add(&c->opts_dir, &c->kobj, "options") ||
578 kobject_add(&c->time_stats, &c->kobj, "time_stats") ||
579 bch2_opts_create_sysfs_files(&c->opts_dir))
580 return "error creating sysfs objects";
582 mutex_lock(&c->state_lock);
584 err = "error creating sysfs objects";
585 __for_each_member_device(ca, c, i, NULL)
586 if (bch2_dev_sysfs_online(c, ca))
589 list_add(&c->list, &bch_fs_list);
592 mutex_unlock(&c->state_lock);
596 static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
598 struct bch_sb_field_members *mi;
600 unsigned i, iter_size;
603 pr_verbose_init(opts, "");
605 c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO);
609 __module_get(THIS_MODULE);
612 c->disk_sb.fs_sb = true;
614 mutex_init(&c->state_lock);
615 mutex_init(&c->sb_lock);
616 mutex_init(&c->replicas_gc_lock);
617 mutex_init(&c->btree_root_lock);
618 INIT_WORK(&c->read_only_work, bch2_fs_read_only_work);
620 init_rwsem(&c->gc_lock);
622 for (i = 0; i < BCH_TIME_STAT_NR; i++)
623 bch2_time_stats_init(&c->times[i]);
625 bch2_fs_allocator_background_init(c);
626 bch2_fs_allocator_foreground_init(c);
627 bch2_fs_rebalance_init(c);
628 bch2_fs_quota_init(c);
630 INIT_LIST_HEAD(&c->list);
632 INIT_LIST_HEAD(&c->btree_interior_update_list);
633 mutex_init(&c->btree_reserve_cache_lock);
634 mutex_init(&c->btree_interior_update_lock);
636 mutex_init(&c->usage_scratch_lock);
638 mutex_init(&c->bio_bounce_pages_lock);
640 bio_list_init(&c->btree_write_error_list);
641 spin_lock_init(&c->btree_write_error_lock);
642 INIT_WORK(&c->btree_write_error_work, bch2_btree_write_error_work);
644 INIT_WORK(&c->journal_seq_blacklist_gc_work,
645 bch2_blacklist_entries_gc);
647 INIT_LIST_HEAD(&c->fsck_errors);
648 mutex_init(&c->fsck_error_lock);
650 INIT_LIST_HEAD(&c->ec_new_stripe_list);
651 mutex_init(&c->ec_new_stripe_lock);
652 mutex_init(&c->ec_stripe_create_lock);
653 spin_lock_init(&c->ec_stripes_heap_lock);
655 seqcount_init(&c->gc_pos_lock);
657 c->copy_gc_enabled = 1;
658 c->rebalance.enabled = 1;
659 c->promote_whole_extents = true;
661 c->journal.write_time = &c->times[BCH_TIME_journal_write];
662 c->journal.delay_time = &c->times[BCH_TIME_journal_delay];
663 c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal];
664 c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq];
666 bch2_fs_btree_cache_init_early(&c->btree_cache);
668 if (percpu_init_rwsem(&c->mark_lock))
671 mutex_lock(&c->sb_lock);
673 if (bch2_sb_to_fs(c, sb)) {
674 mutex_unlock(&c->sb_lock);
678 mutex_unlock(&c->sb_lock);
680 scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid);
682 c->opts = bch2_opts_default;
683 bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb));
684 bch2_opts_apply(&c->opts, opts);
686 c->block_bits = ilog2(c->opts.block_size);
687 c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
689 c->opts.nochanges |= c->opts.noreplay;
690 c->opts.read_only |= c->opts.nochanges;
691 c->opts.read_only |= c->opts.noreplay;
693 if (bch2_fs_init_fault("fs_alloc"))
696 iter_size = sizeof(struct btree_node_iter_large) +
697 (btree_blocks(c) + 1) * 2 *
698 sizeof(struct btree_node_iter_set);
700 if (!(c->wq = alloc_workqueue("bcachefs",
701 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
702 !(c->copygc_wq = alloc_workqueue("bcache_copygc",
703 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
704 !(c->journal_reclaim_wq = alloc_workqueue("bcache_journal",
705 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
706 percpu_ref_init(&c->writes, bch2_writes_disabled,
707 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
708 mempool_init_kmalloc_pool(&c->btree_reserve_pool, 1,
709 sizeof(struct btree_reserve)) ||
710 mempool_init_kmalloc_pool(&c->btree_interior_update_pool, 1,
711 sizeof(struct btree_update)) ||
712 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
713 bioset_init(&c->btree_bio, 1,
714 max(offsetof(struct btree_read_bio, bio),
715 offsetof(struct btree_write_bio, wbio.bio)),
716 BIOSET_NEED_BVECS) ||
717 !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) ||
718 mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1,
720 mempool_init_kmalloc_pool(&c->btree_iters_pool, 1,
721 sizeof(struct btree_iter) * BTREE_ITER_MAX +
722 sizeof(struct btree_insert_entry) *
723 (BTREE_ITER_MAX + 4)) ||
724 bch2_io_clock_init(&c->io_clock[READ]) ||
725 bch2_io_clock_init(&c->io_clock[WRITE]) ||
726 bch2_fs_journal_init(&c->journal) ||
727 bch2_fs_replicas_init(c) ||
728 bch2_fs_btree_cache_init(c) ||
729 bch2_fs_io_init(c) ||
730 bch2_fs_encryption_init(c) ||
731 bch2_fs_compress_init(c) ||
732 bch2_fs_ec_init(c) ||
733 bch2_fs_fsio_init(c))
736 mi = bch2_sb_get_members(c->disk_sb.sb);
737 for (i = 0; i < c->sb.nr_devices; i++)
738 if (bch2_dev_exists(c->disk_sb.sb, mi, i) &&
739 bch2_dev_alloc(c, i))
743 * Now that all allocations have succeeded, init various refcounty
744 * things that let us shutdown:
746 closure_init(&c->cl, NULL);
748 c->kobj.kset = bcachefs_kset;
749 kobject_init(&c->kobj, &bch2_fs_ktype);
750 kobject_init(&c->internal, &bch2_fs_internal_ktype);
751 kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype);
752 kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype);
754 mutex_lock(&bch_fs_list_lock);
755 err = bch2_fs_online(c);
756 mutex_unlock(&bch_fs_list_lock);
758 bch_err(c, "bch2_fs_online() error: %s", err);
762 pr_verbose_init(opts, "ret %i", c ? 0 : -ENOMEM);
770 const char *bch2_fs_start(struct bch_fs *c)
772 const char *err = "cannot allocate memory";
773 struct bch_sb_field_members *mi;
775 time64_t now = ktime_get_real_seconds();
779 mutex_lock(&c->state_lock);
781 BUG_ON(test_bit(BCH_FS_STARTED, &c->flags));
783 mutex_lock(&c->sb_lock);
785 for_each_online_member(ca, c, i)
786 bch2_sb_from_fs(c, ca);
788 mi = bch2_sb_get_members(c->disk_sb.sb);
789 for_each_online_member(ca, c, i)
790 mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
792 mutex_unlock(&c->sb_lock);
794 for_each_rw_member(ca, c, i)
795 bch2_dev_allocator_add(c, ca);
796 bch2_recalc_capacity(c);
798 ret = BCH_SB_INITIALIZED(c->disk_sb.sb)
799 ? bch2_fs_recovery(c)
800 : bch2_fs_initialize(c);
804 ret = bch2_opts_check_may_set(c);
808 err = "dynamic fault";
809 if (bch2_fs_init_fault("fs_start"))
812 if (c->opts.read_only) {
813 bch2_fs_read_only(c);
815 if (!test_bit(BCH_FS_RW, &c->flags)
816 ? bch2_fs_read_write(c)
817 : bch2_fs_read_write_late(c)) {
818 err = "error going read write";
823 set_bit(BCH_FS_STARTED, &c->flags);
827 mutex_unlock(&c->state_lock);
831 case BCH_FSCK_ERRORS_NOT_FIXED:
832 bch_err(c, "filesystem contains errors: please report this to the developers");
833 pr_cont("mount with -o fix_errors to repair\n");
836 case BCH_FSCK_REPAIR_UNIMPLEMENTED:
837 bch_err(c, "filesystem contains errors: please report this to the developers");
838 pr_cont("repair unimplemented: inform the developers so that it can be added\n");
841 case BCH_FSCK_REPAIR_IMPOSSIBLE:
842 bch_err(c, "filesystem contains errors, but repair impossible");
845 case BCH_FSCK_UNKNOWN_VERSION:
846 err = "unknown metadata version";;
849 err = "cannot allocate memory";
860 static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
862 struct bch_sb_field_members *sb_mi;
864 sb_mi = bch2_sb_get_members(sb);
866 return "Invalid superblock: member info area missing";
868 if (le16_to_cpu(sb->block_size) != c->opts.block_size)
869 return "mismatched block size";
871 if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
872 BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
873 return "new cache bucket size is too small";
878 static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
880 struct bch_sb *newest =
881 le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
882 struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
884 if (!uuid_equal(&fs->uuid, &sb->uuid))
885 return "device not a member of filesystem";
887 if (!bch2_dev_exists(newest, mi, sb->dev_idx))
888 return "device has been removed";
890 if (fs->block_size != sb->block_size)
891 return "mismatched block size";
896 /* Device startup/shutdown: */
898 static void bch2_dev_release(struct kobject *kobj)
900 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
905 static void bch2_dev_free(struct bch_dev *ca)
907 cancel_work_sync(&ca->io_error_work);
909 if (ca->kobj.state_in_sysfs &&
911 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
913 if (ca->kobj.state_in_sysfs)
914 kobject_del(&ca->kobj);
916 bch2_free_super(&ca->disk_sb);
917 bch2_dev_journal_exit(ca);
919 free_percpu(ca->io_done);
920 bioset_exit(&ca->replica_set);
921 bch2_dev_buckets_free(ca);
922 free_page((unsigned long) ca->sb_read_scratch);
924 bch2_time_stats_exit(&ca->io_latency[WRITE]);
925 bch2_time_stats_exit(&ca->io_latency[READ]);
927 percpu_ref_exit(&ca->io_ref);
928 percpu_ref_exit(&ca->ref);
929 kobject_put(&ca->kobj);
932 static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca)
935 lockdep_assert_held(&c->state_lock);
937 if (percpu_ref_is_zero(&ca->io_ref))
940 __bch2_dev_read_only(c, ca);
942 reinit_completion(&ca->io_ref_completion);
943 percpu_ref_kill(&ca->io_ref);
944 wait_for_completion(&ca->io_ref_completion);
946 if (ca->kobj.state_in_sysfs) {
947 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
948 sysfs_remove_link(&ca->kobj, "block");
951 bch2_free_super(&ca->disk_sb);
952 bch2_dev_journal_exit(ca);
955 static void bch2_dev_ref_complete(struct percpu_ref *ref)
957 struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
959 complete(&ca->ref_completion);
962 static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
964 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
966 complete(&ca->io_ref_completion);
969 static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca)
973 if (!c->kobj.state_in_sysfs)
976 if (!ca->kobj.state_in_sysfs) {
977 ret = kobject_add(&ca->kobj, &c->kobj,
978 "dev-%u", ca->dev_idx);
983 if (ca->disk_sb.bdev) {
984 struct kobject *block = bdev_kobj(ca->disk_sb.bdev);
986 ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
990 ret = sysfs_create_link(&ca->kobj, block, "block");
998 static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c,
999 struct bch_member *member)
1003 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1007 kobject_init(&ca->kobj, &bch2_dev_ktype);
1008 init_completion(&ca->ref_completion);
1009 init_completion(&ca->io_ref_completion);
1011 init_rwsem(&ca->bucket_lock);
1013 writepoint_init(&ca->copygc_write_point, BCH_DATA_USER);
1015 spin_lock_init(&ca->freelist_lock);
1016 bch2_dev_copygc_init(ca);
1018 INIT_WORK(&ca->io_error_work, bch2_io_error_work);
1020 bch2_time_stats_init(&ca->io_latency[READ]);
1021 bch2_time_stats_init(&ca->io_latency[WRITE]);
1023 ca->mi = bch2_mi_to_cpu(member);
1024 ca->uuid = member->uuid;
1026 if (opt_defined(c->opts, discard))
1027 ca->mi.discard = opt_get(c->opts, discard);
1029 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
1031 percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
1032 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
1033 !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) ||
1034 bch2_dev_buckets_alloc(c, ca) ||
1035 bioset_init(&ca->replica_set, 4,
1036 offsetof(struct bch_write_bio, bio), 0) ||
1037 !(ca->io_done = alloc_percpu(*ca->io_done)))
1046 static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca,
1049 ca->dev_idx = dev_idx;
1050 __set_bit(ca->dev_idx, ca->self.d);
1051 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
1054 rcu_assign_pointer(c->devs[ca->dev_idx], ca);
1056 if (bch2_dev_sysfs_online(c, ca))
1057 pr_warn("error creating sysfs objects");
1060 static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
1062 struct bch_member *member =
1063 bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx;
1064 struct bch_dev *ca = NULL;
1067 pr_verbose_init(c->opts, "");
1069 if (bch2_fs_init_fault("dev_alloc"))
1072 ca = __bch2_dev_alloc(c, member);
1076 bch2_dev_attach(c, ca, dev_idx);
1078 pr_verbose_init(c->opts, "ret %i", ret);
1087 static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
1091 if (bch2_dev_is_online(ca)) {
1092 bch_err(ca, "already have device online in slot %u",
1097 if (get_capacity(sb->bdev->bd_disk) <
1098 ca->mi.bucket_size * ca->mi.nbuckets) {
1099 bch_err(ca, "cannot online: device too small");
1103 BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
1105 if (get_capacity(sb->bdev->bd_disk) <
1106 ca->mi.bucket_size * ca->mi.nbuckets) {
1107 bch_err(ca, "device too small");
1111 ret = bch2_dev_journal_init(ca, sb->sb);
1117 memset(sb, 0, sizeof(*sb));
1119 percpu_ref_reinit(&ca->io_ref);
1124 static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb)
1129 lockdep_assert_held(&c->state_lock);
1131 if (le64_to_cpu(sb->sb->seq) >
1132 le64_to_cpu(c->disk_sb.sb->seq))
1133 bch2_sb_to_fs(c, sb->sb);
1135 BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
1136 !c->devs[sb->sb->dev_idx]);
1138 ca = bch_dev_locked(c, sb->sb->dev_idx);
1140 ret = __bch2_dev_attach_bdev(ca, sb);
1144 if (test_bit(BCH_FS_ALLOC_READ_DONE, &c->flags) &&
1145 !percpu_u64_get(&ca->usage[0]->buckets[BCH_DATA_SB])) {
1146 mutex_lock(&c->sb_lock);
1147 bch2_mark_dev_superblock(ca->fs, ca, 0);
1148 mutex_unlock(&c->sb_lock);
1151 bch2_dev_sysfs_online(c, ca);
1153 if (c->sb.nr_devices == 1)
1154 snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev);
1155 snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev);
1157 rebalance_wakeup(c);
1161 /* Device management: */
1164 * Note: this function is also used by the error paths - when a particular
1165 * device sees an error, we call it to determine whether we can just set the
1166 * device RO, or - if this function returns false - we'll set the whole
1169 * XXX: maybe we should be more explicit about whether we're changing state
1170 * because we got an error or what have you?
1172 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
1173 enum bch_member_state new_state, int flags)
1175 struct bch_devs_mask new_online_devs;
1176 struct replicas_status s;
1177 struct bch_dev *ca2;
1178 int i, nr_rw = 0, required;
1180 lockdep_assert_held(&c->state_lock);
1182 switch (new_state) {
1183 case BCH_MEMBER_STATE_RW:
1185 case BCH_MEMBER_STATE_RO:
1186 if (ca->mi.state != BCH_MEMBER_STATE_RW)
1189 /* do we have enough devices to write to? */
1190 for_each_member_device(ca2, c, i)
1192 nr_rw += ca2->mi.state == BCH_MEMBER_STATE_RW;
1194 required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
1195 ? c->opts.metadata_replicas
1196 : c->opts.metadata_replicas_required,
1197 !(flags & BCH_FORCE_IF_DATA_DEGRADED)
1198 ? c->opts.data_replicas
1199 : c->opts.data_replicas_required);
1201 return nr_rw >= required;
1202 case BCH_MEMBER_STATE_FAILED:
1203 case BCH_MEMBER_STATE_SPARE:
1204 if (ca->mi.state != BCH_MEMBER_STATE_RW &&
1205 ca->mi.state != BCH_MEMBER_STATE_RO)
1208 /* do we have enough devices to read from? */
1209 new_online_devs = bch2_online_devs(c);
1210 __clear_bit(ca->dev_idx, new_online_devs.d);
1212 s = __bch2_replicas_status(c, new_online_devs);
1214 return bch2_have_enough_devs(s, flags);
1220 static bool bch2_fs_may_start(struct bch_fs *c)
1222 struct replicas_status s;
1223 struct bch_sb_field_members *mi;
1225 unsigned i, flags = c->opts.degraded
1226 ? BCH_FORCE_IF_DEGRADED
1229 if (!c->opts.degraded) {
1230 mutex_lock(&c->sb_lock);
1231 mi = bch2_sb_get_members(c->disk_sb.sb);
1233 for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
1234 if (!bch2_dev_exists(c->disk_sb.sb, mi, i))
1237 ca = bch_dev_locked(c, i);
1239 if (!bch2_dev_is_online(ca) &&
1240 (ca->mi.state == BCH_MEMBER_STATE_RW ||
1241 ca->mi.state == BCH_MEMBER_STATE_RO)) {
1242 mutex_unlock(&c->sb_lock);
1246 mutex_unlock(&c->sb_lock);
1249 s = bch2_replicas_status(c);
1251 return bch2_have_enough_devs(s, flags);
1254 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
1256 bch2_copygc_stop(ca);
1259 * The allocator thread itself allocates btree nodes, so stop it first:
1261 bch2_dev_allocator_stop(ca);
1262 bch2_dev_allocator_remove(c, ca);
1263 bch2_dev_journal_stop(&c->journal, ca);
1266 static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
1268 lockdep_assert_held(&c->state_lock);
1270 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_RW);
1272 bch2_dev_allocator_add(c, ca);
1273 bch2_recalc_capacity(c);
1275 if (bch2_dev_allocator_start(ca))
1276 return "error starting allocator thread";
1278 if (bch2_copygc_start(c, ca))
1279 return "error starting copygc thread";
1284 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1285 enum bch_member_state new_state, int flags)
1287 struct bch_sb_field_members *mi;
1290 if (ca->mi.state == new_state)
1293 if (!bch2_dev_state_allowed(c, ca, new_state, flags))
1296 if (new_state != BCH_MEMBER_STATE_RW)
1297 __bch2_dev_read_only(c, ca);
1299 bch_notice(ca, "%s", bch2_dev_state[new_state]);
1301 mutex_lock(&c->sb_lock);
1302 mi = bch2_sb_get_members(c->disk_sb.sb);
1303 SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
1304 bch2_write_super(c);
1305 mutex_unlock(&c->sb_lock);
1307 if (new_state == BCH_MEMBER_STATE_RW &&
1308 __bch2_dev_read_write(c, ca))
1311 rebalance_wakeup(c);
1316 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1317 enum bch_member_state new_state, int flags)
1321 mutex_lock(&c->state_lock);
1322 ret = __bch2_dev_set_state(c, ca, new_state, flags);
1323 mutex_unlock(&c->state_lock);
1328 /* Device add/removal: */
1330 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
1332 struct bch_sb_field_members *mi;
1333 unsigned dev_idx = ca->dev_idx, data;
1336 mutex_lock(&c->state_lock);
1338 percpu_ref_put(&ca->ref); /* XXX */
1340 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1341 bch_err(ca, "Cannot remove without losing data");
1345 __bch2_dev_read_only(c, ca);
1348 * XXX: verify that dev_idx is really not in use anymore, anywhere
1350 * flag_data_bad() does not check btree pointers
1352 ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
1354 bch_err(ca, "Remove failed: error %i dropping data", ret);
1358 ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx);
1360 bch_err(ca, "Remove failed: error %i flushing journal", ret);
1364 data = bch2_dev_has_data(c, ca);
1366 char data_has_str[100];
1368 bch2_flags_to_text(&PBUF(data_has_str),
1369 bch2_data_types, data);
1370 bch_err(ca, "Remove failed, still has data (%s)", data_has_str);
1375 ret = bch2_btree_delete_range(c, BTREE_ID_ALLOC,
1376 POS(ca->dev_idx, 0),
1377 POS(ca->dev_idx + 1, 0),
1380 bch_err(ca, "Remove failed, error deleting alloc info");
1385 * must flush all existing journal entries, they might have
1386 * (overwritten) keys that point to the device we're removing:
1388 bch2_journal_flush_all_pins(&c->journal);
1389 ret = bch2_journal_error(&c->journal);
1391 bch_err(ca, "Remove failed, journal error");
1395 __bch2_dev_offline(c, ca);
1397 mutex_lock(&c->sb_lock);
1398 rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
1399 mutex_unlock(&c->sb_lock);
1401 percpu_ref_kill(&ca->ref);
1402 wait_for_completion(&ca->ref_completion);
1407 * Free this device's slot in the bch_member array - all pointers to
1408 * this device must be gone:
1410 mutex_lock(&c->sb_lock);
1411 mi = bch2_sb_get_members(c->disk_sb.sb);
1412 memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
1414 bch2_write_super(c);
1416 mutex_unlock(&c->sb_lock);
1417 mutex_unlock(&c->state_lock);
1420 if (ca->mi.state == BCH_MEMBER_STATE_RW &&
1421 !percpu_ref_is_zero(&ca->io_ref))
1422 __bch2_dev_read_write(c, ca);
1423 mutex_unlock(&c->state_lock);
1427 static void dev_usage_clear(struct bch_dev *ca)
1429 struct bucket_array *buckets;
1432 for_each_possible_cpu(cpu) {
1433 struct bch_dev_usage *p =
1434 per_cpu_ptr(ca->usage[0], cpu);
1435 memset(p, 0, sizeof(*p));
1438 down_read(&ca->bucket_lock);
1439 buckets = bucket_array(ca);
1441 memset(buckets->b, 0, sizeof(buckets->b[0]) * buckets->nbuckets);
1442 up_read(&ca->bucket_lock);
1445 /* Add new device to running filesystem: */
1446 int bch2_dev_add(struct bch_fs *c, const char *path)
1448 struct bch_opts opts = bch2_opts_empty();
1449 struct bch_sb_handle sb;
1451 struct bch_dev *ca = NULL;
1452 struct bch_sb_field_members *mi;
1453 struct bch_member dev_mi;
1454 unsigned dev_idx, nr_devices, u64s;
1457 ret = bch2_read_super(path, &opts, &sb);
1461 err = bch2_sb_validate(&sb);
1465 dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx];
1467 err = bch2_dev_may_add(sb.sb, c);
1471 ca = __bch2_dev_alloc(c, &dev_mi);
1473 bch2_free_super(&sb);
1477 ret = __bch2_dev_attach_bdev(ca, &sb);
1484 * We want to allocate journal on the new device before adding the new
1485 * device to the filesystem because allocating after we attach requires
1486 * spinning up the allocator thread, and the allocator thread requires
1487 * doing btree writes, which if the existing devices are RO isn't going
1490 * So we have to mark where the superblocks are, but marking allocated
1491 * data normally updates the filesystem usage too, so we have to mark,
1492 * allocate the journal, reset all the marks, then remark after we
1495 bch2_mark_dev_superblock(ca->fs, ca, 0);
1497 err = "journal alloc failed";
1498 ret = bch2_dev_journal_alloc(ca);
1502 dev_usage_clear(ca);
1504 mutex_lock(&c->state_lock);
1505 mutex_lock(&c->sb_lock);
1507 err = "insufficient space in new superblock";
1508 ret = bch2_sb_from_fs(c, ca);
1512 mi = bch2_sb_get_members(ca->disk_sb.sb);
1514 if (!bch2_sb_resize_members(&ca->disk_sb,
1515 le32_to_cpu(mi->field.u64s) +
1516 sizeof(dev_mi) / sizeof(u64))) {
1521 if (dynamic_fault("bcachefs:add:no_slot"))
1524 mi = bch2_sb_get_members(c->disk_sb.sb);
1525 for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
1526 if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx))
1529 err = "no slots available in superblock";
1534 nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
1535 u64s = (sizeof(struct bch_sb_field_members) +
1536 sizeof(struct bch_member) * nr_devices) / sizeof(u64);
1538 err = "no space in superblock for member info";
1541 mi = bch2_sb_resize_members(&c->disk_sb, u64s);
1547 mi->members[dev_idx] = dev_mi;
1548 mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds());
1549 c->disk_sb.sb->nr_devices = nr_devices;
1551 ca->disk_sb.sb->dev_idx = dev_idx;
1552 bch2_dev_attach(c, ca, dev_idx);
1554 bch2_mark_dev_superblock(c, ca, 0);
1556 bch2_write_super(c);
1557 mutex_unlock(&c->sb_lock);
1559 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1560 err = __bch2_dev_read_write(c, ca);
1565 mutex_unlock(&c->state_lock);
1569 mutex_unlock(&c->sb_lock);
1570 mutex_unlock(&c->state_lock);
1574 bch2_free_super(&sb);
1575 bch_err(c, "Unable to add device: %s", err);
1578 bch_err(c, "Error going rw after adding device: %s", err);
1582 /* Hot add existing device to running filesystem: */
1583 int bch2_dev_online(struct bch_fs *c, const char *path)
1585 struct bch_opts opts = bch2_opts_empty();
1586 struct bch_sb_handle sb = { NULL };
1587 struct bch_sb_field_members *mi;
1593 mutex_lock(&c->state_lock);
1595 ret = bch2_read_super(path, &opts, &sb);
1597 mutex_unlock(&c->state_lock);
1601 dev_idx = sb.sb->dev_idx;
1603 err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
1607 if (bch2_dev_attach_bdev(c, &sb)) {
1608 err = "bch2_dev_attach_bdev() error";
1612 ca = bch_dev_locked(c, dev_idx);
1613 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1614 err = __bch2_dev_read_write(c, ca);
1619 mutex_lock(&c->sb_lock);
1620 mi = bch2_sb_get_members(c->disk_sb.sb);
1622 mi->members[ca->dev_idx].last_mount =
1623 cpu_to_le64(ktime_get_real_seconds());
1625 bch2_write_super(c);
1626 mutex_unlock(&c->sb_lock);
1628 mutex_unlock(&c->state_lock);
1631 mutex_unlock(&c->state_lock);
1632 bch2_free_super(&sb);
1633 bch_err(c, "error bringing %s online: %s", path, err);
1637 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
1639 mutex_lock(&c->state_lock);
1641 if (!bch2_dev_is_online(ca)) {
1642 bch_err(ca, "Already offline");
1643 mutex_unlock(&c->state_lock);
1647 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1648 bch_err(ca, "Cannot offline required disk");
1649 mutex_unlock(&c->state_lock);
1653 __bch2_dev_offline(c, ca);
1655 mutex_unlock(&c->state_lock);
1659 int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
1661 struct bch_member *mi;
1664 mutex_lock(&c->state_lock);
1666 if (nbuckets < ca->mi.nbuckets) {
1667 bch_err(ca, "Cannot shrink yet");
1672 if (bch2_dev_is_online(ca) &&
1673 get_capacity(ca->disk_sb.bdev->bd_disk) <
1674 ca->mi.bucket_size * nbuckets) {
1675 bch_err(ca, "New size larger than device");
1680 ret = bch2_dev_buckets_resize(c, ca, nbuckets);
1682 bch_err(ca, "Resize error: %i", ret);
1686 mutex_lock(&c->sb_lock);
1687 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1688 mi->nbuckets = cpu_to_le64(nbuckets);
1690 bch2_write_super(c);
1691 mutex_unlock(&c->sb_lock);
1693 bch2_recalc_capacity(c);
1695 mutex_unlock(&c->state_lock);
1699 /* return with ref on ca->ref: */
1700 struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path)
1708 ret = lookup_bdev(path, &dev);
1710 return ERR_PTR(ret);
1712 for_each_member_device(ca, c, i)
1713 if (ca->disk_sb.bdev->bd_dev == dev)
1716 ca = ERR_PTR(-ENOENT);
1721 /* Filesystem open: */
1723 struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
1724 struct bch_opts opts)
1726 struct bch_sb_handle *sb = NULL;
1727 struct bch_fs *c = NULL;
1728 unsigned i, best_sb = 0;
1732 pr_verbose_init(opts, "");
1735 c = ERR_PTR(-EINVAL);
1739 if (!try_module_get(THIS_MODULE)) {
1740 c = ERR_PTR(-ENODEV);
1744 sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
1748 for (i = 0; i < nr_devices; i++) {
1749 ret = bch2_read_super(devices[i], &opts, &sb[i]);
1753 err = bch2_sb_validate(&sb[i]);
1758 for (i = 1; i < nr_devices; i++)
1759 if (le64_to_cpu(sb[i].sb->seq) >
1760 le64_to_cpu(sb[best_sb].sb->seq))
1763 for (i = 0; i < nr_devices; i++) {
1764 err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
1770 c = bch2_fs_alloc(sb[best_sb].sb, opts);
1774 err = "bch2_dev_online() error";
1775 mutex_lock(&c->state_lock);
1776 for (i = 0; i < nr_devices; i++)
1777 if (bch2_dev_attach_bdev(c, &sb[i])) {
1778 mutex_unlock(&c->state_lock);
1781 mutex_unlock(&c->state_lock);
1783 err = "insufficient devices";
1784 if (!bch2_fs_may_start(c))
1787 if (!c->opts.nostart) {
1788 err = bch2_fs_start(c);
1794 module_put(THIS_MODULE);
1796 pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
1799 pr_err("bch_fs_open err opening %s: %s",
1805 for (i = 0; i < nr_devices; i++)
1806 bch2_free_super(&sb[i]);
1811 static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb,
1812 struct bch_opts opts)
1816 bool allocated_fs = false;
1818 err = bch2_sb_validate(sb);
1822 mutex_lock(&bch_fs_list_lock);
1823 c = __bch2_uuid_to_fs(sb->sb->uuid);
1825 closure_get(&c->cl);
1827 err = bch2_dev_in_fs(c->disk_sb.sb, sb->sb);
1831 c = bch2_fs_alloc(sb->sb, opts);
1832 err = "cannot allocate memory";
1836 allocated_fs = true;
1839 err = "bch2_dev_online() error";
1841 mutex_lock(&c->sb_lock);
1842 if (bch2_dev_attach_bdev(c, sb)) {
1843 mutex_unlock(&c->sb_lock);
1846 mutex_unlock(&c->sb_lock);
1848 if (!c->opts.nostart && bch2_fs_may_start(c)) {
1849 err = bch2_fs_start(c);
1854 closure_put(&c->cl);
1855 mutex_unlock(&bch_fs_list_lock);
1859 mutex_unlock(&bch_fs_list_lock);
1864 closure_put(&c->cl);
1869 const char *bch2_fs_open_incremental(const char *path)
1871 struct bch_sb_handle sb;
1872 struct bch_opts opts = bch2_opts_empty();
1875 if (bch2_read_super(path, &opts, &sb))
1876 return "error reading superblock";
1878 err = __bch2_fs_open_incremental(&sb, opts);
1879 bch2_free_super(&sb);
1884 /* Global interfaces/init */
1886 static void bcachefs_exit(void)
1890 bch2_chardev_exit();
1892 kset_unregister(bcachefs_kset);
1895 static int __init bcachefs_init(void)
1897 bch2_bkey_pack_test();
1898 bch2_inode_pack_test();
1900 if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
1901 bch2_chardev_init() ||
1912 #define BCH_DEBUG_PARAM(name, description) \
1914 module_param_named(name, bch2_##name, bool, 0644); \
1915 MODULE_PARM_DESC(name, description);
1917 #undef BCH_DEBUG_PARAM
1919 unsigned bch2_metadata_version = bcachefs_metadata_version_current;
1920 module_param_named(version, bch2_metadata_version, uint, 0400);
1922 module_exit(bcachefs_exit);
1923 module_init(bcachefs_init);