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"
37 #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/idr.h>
50 #include <linux/kthread.h>
51 #include <linux/module.h>
52 #include <linux/percpu.h>
53 #include <linux/random.h>
54 #include <linux/sysfs.h>
55 #include <crypto/hash.h>
57 MODULE_LICENSE("GPL");
58 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
61 static const struct attribute_group type ## _group = { \
62 .attrs = type ## _files \
65 static const struct attribute_group *type ## _groups[] = { \
70 static const struct kobj_type type ## _ktype = { \
71 .release = type ## _release, \
72 .sysfs_ops = &type ## _sysfs_ops, \
73 .default_groups = type ## _groups \
76 static void bch2_fs_release(struct kobject *);
77 static void bch2_dev_release(struct kobject *);
79 static void bch2_fs_internal_release(struct kobject *k)
83 static void bch2_fs_opts_dir_release(struct kobject *k)
87 static void bch2_fs_time_stats_release(struct kobject *k)
92 KTYPE(bch2_fs_internal);
93 KTYPE(bch2_fs_opts_dir);
94 KTYPE(bch2_fs_time_stats);
97 static struct kset *bcachefs_kset;
98 static LIST_HEAD(bch_fs_list);
99 static DEFINE_MUTEX(bch_fs_list_lock);
101 static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
103 static void bch2_dev_free(struct bch_dev *);
104 static int bch2_dev_alloc(struct bch_fs *, unsigned);
105 static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *);
106 static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
108 struct bch_fs *bch2_dev_to_fs(dev_t dev)
114 mutex_lock(&bch_fs_list_lock);
117 list_for_each_entry(c, &bch_fs_list, list)
118 for_each_member_device_rcu(ca, c, i, NULL)
119 if (ca->disk_sb.bdev->bd_dev == dev) {
126 mutex_unlock(&bch_fs_list_lock);
131 static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid)
135 lockdep_assert_held(&bch_fs_list_lock);
137 list_for_each_entry(c, &bch_fs_list, list)
138 if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid)))
144 struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid)
148 mutex_lock(&bch_fs_list_lock);
149 c = __bch2_uuid_to_fs(uuid);
152 mutex_unlock(&bch_fs_list_lock);
157 /* Filesystem RO/RW: */
160 * For startup/shutdown of RW stuff, the dependencies are:
162 * - foreground writes depend on copygc and rebalance (to free up space)
164 * - copygc and rebalance depend on mark and sweep gc (they actually probably
165 * don't because they either reserve ahead of time or don't block if
166 * allocations fail, but allocations can require mark and sweep gc to run
167 * because of generation number wraparound)
169 * - all of the above depends on the allocator threads
171 * - allocator depends on the journal (when it rewrites prios and gens)
174 static void __bch2_fs_read_only(struct bch_fs *c)
181 bch2_rebalance_stop(c);
183 for_each_member_device(ca, c, i)
184 bch2_copygc_stop(ca);
186 bch2_gc_thread_stop(c);
189 * Flush journal before stopping allocators, because flushing journal
190 * blacklist entries involves allocating new btree nodes:
192 bch2_journal_flush_all_pins(&c->journal);
194 if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags))
195 goto allocator_not_running;
198 ret = bch2_alloc_write(c, false, &wrote);
200 bch2_fs_inconsistent(c, "error writing out alloc info %i", ret);
204 ret = bch2_stripes_write(c, &wrote);
206 bch2_fs_inconsistent(c, "error writing out stripes");
210 for_each_member_device(ca, c, i)
211 bch2_dev_allocator_quiesce(c, ca);
213 bch2_journal_flush_all_pins(&c->journal);
216 * We need to explicitly wait on btree interior updates to complete
217 * before stopping the journal, flushing all journal pins isn't
218 * sufficient, because in the BTREE_INTERIOR_UPDATING_ROOT case btree
219 * interior updates have to drop their journal pin before they're
222 closure_wait_event(&c->btree_interior_update_wait,
223 !bch2_btree_interior_updates_nr_pending(c));
225 allocator_not_running:
226 for_each_member_device(ca, c, i)
227 bch2_dev_allocator_stop(ca);
229 clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
231 bch2_fs_journal_stop(&c->journal);
233 /* XXX: mark super that alloc info is persistent */
236 * the journal kicks off btree writes via reclaim - wait for in flight
237 * writes after stopping journal:
239 if (test_bit(BCH_FS_EMERGENCY_RO, &c->flags))
240 bch2_btree_flush_all_writes(c);
242 bch2_btree_verify_flushed(c);
245 * After stopping journal:
247 for_each_member_device(ca, c, i)
248 bch2_dev_allocator_remove(c, ca);
251 static void bch2_writes_disabled(struct percpu_ref *writes)
253 struct bch_fs *c = container_of(writes, struct bch_fs, writes);
255 set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
256 wake_up(&bch_read_only_wait);
259 void bch2_fs_read_only(struct bch_fs *c)
261 if (c->state == BCH_FS_RO)
264 BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
267 * Block new foreground-end write operations from starting - any new
268 * writes will return -EROFS:
270 * (This is really blocking new _allocations_, writes to previously
271 * allocated space can still happen until stopping the allocator in
272 * bch2_dev_allocator_stop()).
274 percpu_ref_kill(&c->writes);
276 cancel_delayed_work(&c->pd_controllers_update);
279 * If we're not doing an emergency shutdown, we want to wait on
280 * outstanding writes to complete so they don't see spurious errors due
281 * to shutting down the allocator:
283 * If we are doing an emergency shutdown outstanding writes may
284 * hang until we shutdown the allocator so we don't want to wait
285 * on outstanding writes before shutting everything down - but
286 * we do need to wait on them before returning and signalling
287 * that going RO is complete:
289 wait_event(bch_read_only_wait,
290 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
291 test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
293 __bch2_fs_read_only(c);
295 wait_event(bch_read_only_wait,
296 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
298 clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
300 if (!bch2_journal_error(&c->journal) &&
301 !test_bit(BCH_FS_ERROR, &c->flags) &&
302 !test_bit(BCH_FS_EMERGENCY_RO, &c->flags))
303 bch2_fs_mark_clean(c, true);
305 if (c->state != BCH_FS_STOPPING)
306 c->state = BCH_FS_RO;
309 static void bch2_fs_read_only_work(struct work_struct *work)
312 container_of(work, struct bch_fs, read_only_work);
314 mutex_lock(&c->state_lock);
315 bch2_fs_read_only(c);
316 mutex_unlock(&c->state_lock);
319 static void bch2_fs_read_only_async(struct bch_fs *c)
321 queue_work(system_long_wq, &c->read_only_work);
324 bool bch2_fs_emergency_read_only(struct bch_fs *c)
326 bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
328 bch2_fs_read_only_async(c);
329 bch2_journal_halt(&c->journal);
331 wake_up(&bch_read_only_wait);
335 const char *bch2_fs_read_write(struct bch_fs *c)
338 const char *err = NULL;
341 if (c->state == BCH_FS_RW)
344 bch2_fs_mark_clean(c, false);
346 for_each_rw_member(ca, c, i)
347 bch2_dev_allocator_add(c, ca);
348 bch2_recalc_capacity(c);
350 err = "error starting allocator thread";
351 for_each_rw_member(ca, c, i)
352 if (bch2_dev_allocator_start(ca)) {
353 percpu_ref_put(&ca->io_ref);
357 set_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
359 err = "error starting btree GC thread";
360 if (bch2_gc_thread_start(c))
363 err = "error starting copygc thread";
364 for_each_rw_member(ca, c, i)
365 if (bch2_copygc_start(c, ca)) {
366 percpu_ref_put(&ca->io_ref);
370 err = "error starting rebalance thread";
371 if (bch2_rebalance_start(c))
374 schedule_delayed_work(&c->pd_controllers_update, 5 * HZ);
376 if (c->state != BCH_FS_STARTING)
377 percpu_ref_reinit(&c->writes);
379 c->state = BCH_FS_RW;
382 __bch2_fs_read_only(c);
386 /* Filesystem startup/shutdown: */
388 static void bch2_fs_free(struct bch_fs *c)
392 for (i = 0; i < BCH_TIME_STAT_NR; i++)
393 bch2_time_stats_exit(&c->times[i]);
395 bch2_fs_quota_exit(c);
396 bch2_fs_fsio_exit(c);
398 bch2_fs_encryption_exit(c);
400 bch2_fs_btree_cache_exit(c);
401 bch2_fs_journal_exit(&c->journal);
402 bch2_io_clock_exit(&c->io_clock[WRITE]);
403 bch2_io_clock_exit(&c->io_clock[READ]);
404 bch2_fs_compress_exit(c);
405 percpu_free_rwsem(&c->mark_lock);
406 free_percpu(c->usage_scratch);
407 free_percpu(c->usage[0]);
408 free_percpu(c->pcpu);
409 mempool_exit(&c->btree_iters_pool);
410 mempool_exit(&c->btree_bounce_pool);
411 bioset_exit(&c->btree_bio);
412 mempool_exit(&c->btree_interior_update_pool);
413 mempool_exit(&c->btree_reserve_pool);
414 mempool_exit(&c->fill_iter);
415 percpu_ref_exit(&c->writes);
416 kfree(c->replicas.entries);
417 kfree(c->replicas_gc.entries);
418 kfree(rcu_dereference_protected(c->disk_groups, 1));
420 if (c->journal_reclaim_wq)
421 destroy_workqueue(c->journal_reclaim_wq);
423 destroy_workqueue(c->copygc_wq);
425 destroy_workqueue(c->wq);
427 free_pages((unsigned long) c->disk_sb.sb,
428 c->disk_sb.page_order);
429 kvpfree(c, sizeof(*c));
430 module_put(THIS_MODULE);
433 static void bch2_fs_release(struct kobject *kobj)
435 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
440 void bch2_fs_stop(struct bch_fs *c)
445 bch_verbose(c, "shutting down");
447 for_each_member_device(ca, c, i)
448 if (ca->kobj.state_in_sysfs &&
450 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
452 if (c->kobj.state_in_sysfs)
453 kobject_del(&c->kobj);
455 bch2_fs_debug_exit(c);
456 bch2_fs_chardev_exit(c);
458 kobject_put(&c->time_stats);
459 kobject_put(&c->opts_dir);
460 kobject_put(&c->internal);
462 mutex_lock(&bch_fs_list_lock);
464 mutex_unlock(&bch_fs_list_lock);
466 closure_sync(&c->cl);
467 closure_debug_destroy(&c->cl);
469 mutex_lock(&c->state_lock);
470 bch2_fs_read_only(c);
471 mutex_unlock(&c->state_lock);
473 /* btree prefetch might have kicked off reads in the background: */
474 bch2_btree_flush_all_reads(c);
476 for_each_member_device(ca, c, i)
477 cancel_work_sync(&ca->io_error_work);
479 cancel_work_sync(&c->btree_write_error_work);
480 cancel_delayed_work_sync(&c->pd_controllers_update);
481 cancel_work_sync(&c->read_only_work);
483 for (i = 0; i < c->sb.nr_devices; i++)
485 bch2_dev_free(rcu_dereference_protected(c->devs[i], 1));
487 bch_verbose(c, "shutdown complete");
489 kobject_put(&c->kobj);
492 static const char *bch2_fs_online(struct bch_fs *c)
495 const char *err = NULL;
499 lockdep_assert_held(&bch_fs_list_lock);
501 if (!list_empty(&c->list))
504 if (__bch2_uuid_to_fs(c->sb.uuid))
505 return "filesystem UUID already open";
507 ret = bch2_fs_chardev_init(c);
509 return "error creating character device";
511 bch2_fs_debug_init(c);
513 if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ||
514 kobject_add(&c->internal, &c->kobj, "internal") ||
515 kobject_add(&c->opts_dir, &c->kobj, "options") ||
516 kobject_add(&c->time_stats, &c->kobj, "time_stats") ||
517 bch2_opts_create_sysfs_files(&c->opts_dir))
518 return "error creating sysfs objects";
520 mutex_lock(&c->state_lock);
522 err = "error creating sysfs objects";
523 __for_each_member_device(ca, c, i, NULL)
524 if (bch2_dev_sysfs_online(c, ca))
527 list_add(&c->list, &bch_fs_list);
530 mutex_unlock(&c->state_lock);
534 static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
536 struct bch_sb_field_members *mi;
538 unsigned i, iter_size, fs_usage_size;
541 pr_verbose_init(opts, "");
543 c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO);
547 __module_get(THIS_MODULE);
550 c->disk_sb.fs_sb = true;
552 mutex_init(&c->state_lock);
553 mutex_init(&c->sb_lock);
554 mutex_init(&c->replicas_gc_lock);
555 mutex_init(&c->btree_root_lock);
556 INIT_WORK(&c->read_only_work, bch2_fs_read_only_work);
558 init_rwsem(&c->gc_lock);
560 for (i = 0; i < BCH_TIME_STAT_NR; i++)
561 bch2_time_stats_init(&c->times[i]);
563 bch2_fs_allocator_background_init(c);
564 bch2_fs_allocator_foreground_init(c);
565 bch2_fs_rebalance_init(c);
566 bch2_fs_quota_init(c);
568 INIT_LIST_HEAD(&c->list);
570 INIT_LIST_HEAD(&c->btree_interior_update_list);
571 mutex_init(&c->btree_reserve_cache_lock);
572 mutex_init(&c->btree_interior_update_lock);
574 mutex_init(&c->bio_bounce_pages_lock);
576 bio_list_init(&c->btree_write_error_list);
577 spin_lock_init(&c->btree_write_error_lock);
578 INIT_WORK(&c->btree_write_error_work, bch2_btree_write_error_work);
580 INIT_LIST_HEAD(&c->fsck_errors);
581 mutex_init(&c->fsck_error_lock);
583 INIT_LIST_HEAD(&c->ec_new_stripe_list);
584 mutex_init(&c->ec_new_stripe_lock);
585 mutex_init(&c->ec_stripe_create_lock);
586 spin_lock_init(&c->ec_stripes_heap_lock);
588 seqcount_init(&c->gc_pos_lock);
590 c->copy_gc_enabled = 1;
591 c->rebalance.enabled = 1;
592 c->promote_whole_extents = true;
594 c->journal.write_time = &c->times[BCH_TIME_journal_write];
595 c->journal.delay_time = &c->times[BCH_TIME_journal_delay];
596 c->journal.blocked_time = &c->times[BCH_TIME_journal_blocked];
597 c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq];
599 bch2_fs_btree_cache_init_early(&c->btree_cache);
601 if (percpu_init_rwsem(&c->mark_lock))
604 mutex_lock(&c->sb_lock);
606 if (bch2_sb_to_fs(c, sb)) {
607 mutex_unlock(&c->sb_lock);
611 mutex_unlock(&c->sb_lock);
613 scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid);
615 c->opts = bch2_opts_default;
616 bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb));
617 bch2_opts_apply(&c->opts, opts);
619 c->block_bits = ilog2(c->opts.block_size);
620 c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
622 c->opts.nochanges |= c->opts.noreplay;
623 c->opts.read_only |= c->opts.nochanges;
625 if (bch2_fs_init_fault("fs_alloc"))
628 iter_size = sizeof(struct btree_node_iter_large) +
629 (btree_blocks(c) + 1) * 2 *
630 sizeof(struct btree_node_iter_set);
632 fs_usage_size = sizeof(struct bch_fs_usage) +
633 sizeof(u64) * c->replicas.nr;
635 if (!(c->wq = alloc_workqueue("bcachefs",
636 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
637 !(c->copygc_wq = alloc_workqueue("bcache_copygc",
638 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
639 !(c->journal_reclaim_wq = alloc_workqueue("bcache_journal",
640 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
641 percpu_ref_init(&c->writes, bch2_writes_disabled, 0, GFP_KERNEL) ||
642 mempool_init_kmalloc_pool(&c->btree_reserve_pool, 1,
643 sizeof(struct btree_reserve)) ||
644 mempool_init_kmalloc_pool(&c->btree_interior_update_pool, 1,
645 sizeof(struct btree_update)) ||
646 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
647 bioset_init(&c->btree_bio, 1,
648 max(offsetof(struct btree_read_bio, bio),
649 offsetof(struct btree_write_bio, wbio.bio)),
650 BIOSET_NEED_BVECS) ||
651 !(c->usage[0] = __alloc_percpu(fs_usage_size, sizeof(u64))) ||
652 !(c->usage_scratch = __alloc_percpu(fs_usage_size, sizeof(u64))) ||
653 !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) ||
654 mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1,
656 mempool_init_kmalloc_pool(&c->btree_iters_pool, 1,
657 sizeof(struct btree_iter) * BTREE_ITER_MAX) ||
658 bch2_io_clock_init(&c->io_clock[READ]) ||
659 bch2_io_clock_init(&c->io_clock[WRITE]) ||
660 bch2_fs_journal_init(&c->journal) ||
661 bch2_fs_btree_cache_init(c) ||
662 bch2_fs_io_init(c) ||
663 bch2_fs_encryption_init(c) ||
664 bch2_fs_compress_init(c) ||
665 bch2_fs_ec_init(c) ||
666 bch2_fs_fsio_init(c))
669 mi = bch2_sb_get_members(c->disk_sb.sb);
670 for (i = 0; i < c->sb.nr_devices; i++)
671 if (bch2_dev_exists(c->disk_sb.sb, mi, i) &&
672 bch2_dev_alloc(c, i))
676 * Now that all allocations have succeeded, init various refcounty
677 * things that let us shutdown:
679 closure_init(&c->cl, NULL);
681 c->kobj.kset = bcachefs_kset;
682 kobject_init(&c->kobj, &bch2_fs_ktype);
683 kobject_init(&c->internal, &bch2_fs_internal_ktype);
684 kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype);
685 kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype);
687 mutex_lock(&bch_fs_list_lock);
688 err = bch2_fs_online(c);
689 mutex_unlock(&bch_fs_list_lock);
691 bch_err(c, "bch2_fs_online() error: %s", err);
695 pr_verbose_init(opts, "ret %i", c ? 0 : -ENOMEM);
703 const char *bch2_fs_start(struct bch_fs *c)
705 const char *err = "cannot allocate memory";
706 struct bch_sb_field_members *mi;
708 time64_t now = ktime_get_real_seconds();
712 mutex_lock(&c->state_lock);
714 BUG_ON(c->state != BCH_FS_STARTING);
716 mutex_lock(&c->sb_lock);
718 for_each_online_member(ca, c, i)
719 bch2_sb_from_fs(c, ca);
721 mi = bch2_sb_get_members(c->disk_sb.sb);
722 for_each_online_member(ca, c, i)
723 mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
725 mutex_unlock(&c->sb_lock);
727 for_each_rw_member(ca, c, i)
728 bch2_dev_allocator_add(c, ca);
729 bch2_recalc_capacity(c);
731 ret = BCH_SB_INITIALIZED(c->disk_sb.sb)
732 ? bch2_fs_recovery(c)
733 : bch2_fs_initialize(c);
737 ret = bch2_opts_check_may_set(c);
741 err = "dynamic fault";
742 if (bch2_fs_init_fault("fs_start"))
745 if (c->opts.read_only) {
746 bch2_fs_read_only(c);
748 err = bch2_fs_read_write(c);
753 set_bit(BCH_FS_STARTED, &c->flags);
757 mutex_unlock(&c->state_lock);
761 case BCH_FSCK_ERRORS_NOT_FIXED:
762 bch_err(c, "filesystem contains errors: please report this to the developers");
763 pr_cont("mount with -o fix_errors to repair\n");
766 case BCH_FSCK_REPAIR_UNIMPLEMENTED:
767 bch_err(c, "filesystem contains errors: please report this to the developers");
768 pr_cont("repair unimplemented: inform the developers so that it can be added\n");
771 case BCH_FSCK_REPAIR_IMPOSSIBLE:
772 bch_err(c, "filesystem contains errors, but repair impossible");
775 case BCH_FSCK_UNKNOWN_VERSION:
776 err = "unknown metadata version";;
779 err = "cannot allocate memory";
787 set_bit(BCH_FS_ERROR, &c->flags);
791 static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
793 struct bch_sb_field_members *sb_mi;
795 sb_mi = bch2_sb_get_members(sb);
797 return "Invalid superblock: member info area missing";
799 if (le16_to_cpu(sb->block_size) != c->opts.block_size)
800 return "mismatched block size";
802 if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
803 BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
804 return "new cache bucket size is too small";
809 static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
811 struct bch_sb *newest =
812 le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
813 struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
815 if (!uuid_equal(&fs->uuid, &sb->uuid))
816 return "device not a member of filesystem";
818 if (!bch2_dev_exists(newest, mi, sb->dev_idx))
819 return "device has been removed";
821 if (fs->block_size != sb->block_size)
822 return "mismatched block size";
827 /* Device startup/shutdown: */
829 static void bch2_dev_release(struct kobject *kobj)
831 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
836 static void bch2_dev_free(struct bch_dev *ca)
838 cancel_work_sync(&ca->io_error_work);
840 if (ca->kobj.state_in_sysfs &&
842 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
844 if (ca->kobj.state_in_sysfs)
845 kobject_del(&ca->kobj);
847 bch2_free_super(&ca->disk_sb);
848 bch2_dev_journal_exit(ca);
850 free_percpu(ca->io_done);
851 bioset_exit(&ca->replica_set);
852 bch2_dev_buckets_free(ca);
854 bch2_time_stats_exit(&ca->io_latency[WRITE]);
855 bch2_time_stats_exit(&ca->io_latency[READ]);
857 percpu_ref_exit(&ca->io_ref);
858 percpu_ref_exit(&ca->ref);
859 kobject_put(&ca->kobj);
862 static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca)
865 lockdep_assert_held(&c->state_lock);
867 if (percpu_ref_is_zero(&ca->io_ref))
870 __bch2_dev_read_only(c, ca);
872 reinit_completion(&ca->io_ref_completion);
873 percpu_ref_kill(&ca->io_ref);
874 wait_for_completion(&ca->io_ref_completion);
876 if (ca->kobj.state_in_sysfs) {
877 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
878 sysfs_remove_link(&ca->kobj, "block");
881 bch2_free_super(&ca->disk_sb);
882 bch2_dev_journal_exit(ca);
885 static void bch2_dev_ref_complete(struct percpu_ref *ref)
887 struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
889 complete(&ca->ref_completion);
892 static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
894 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
896 complete(&ca->io_ref_completion);
899 static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca)
903 if (!c->kobj.state_in_sysfs)
906 if (!ca->kobj.state_in_sysfs) {
907 ret = kobject_add(&ca->kobj, &c->kobj,
908 "dev-%u", ca->dev_idx);
913 if (ca->disk_sb.bdev) {
914 struct kobject *block = bdev_kobj(ca->disk_sb.bdev);
916 ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
920 ret = sysfs_create_link(&ca->kobj, block, "block");
928 static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c,
929 struct bch_member *member)
933 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
937 kobject_init(&ca->kobj, &bch2_dev_ktype);
938 init_completion(&ca->ref_completion);
939 init_completion(&ca->io_ref_completion);
941 init_rwsem(&ca->bucket_lock);
943 writepoint_init(&ca->copygc_write_point, BCH_DATA_USER);
945 spin_lock_init(&ca->freelist_lock);
946 bch2_dev_copygc_init(ca);
948 INIT_WORK(&ca->io_error_work, bch2_io_error_work);
950 bch2_time_stats_init(&ca->io_latency[READ]);
951 bch2_time_stats_init(&ca->io_latency[WRITE]);
953 ca->mi = bch2_mi_to_cpu(member);
954 ca->uuid = member->uuid;
956 if (opt_defined(c->opts, discard))
957 ca->mi.discard = opt_get(c->opts, discard);
959 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
961 percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
962 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
963 bch2_dev_buckets_alloc(c, ca) ||
964 bioset_init(&ca->replica_set, 4,
965 offsetof(struct bch_write_bio, bio), 0) ||
966 !(ca->io_done = alloc_percpu(*ca->io_done)))
975 static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca,
978 ca->dev_idx = dev_idx;
979 __set_bit(ca->dev_idx, ca->self.d);
980 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
983 rcu_assign_pointer(c->devs[ca->dev_idx], ca);
985 if (bch2_dev_sysfs_online(c, ca))
986 pr_warn("error creating sysfs objects");
989 static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
991 struct bch_member *member =
992 bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx;
993 struct bch_dev *ca = NULL;
996 pr_verbose_init(c->opts, "");
998 if (bch2_fs_init_fault("dev_alloc"))
1001 ca = __bch2_dev_alloc(c, member);
1005 bch2_dev_attach(c, ca, dev_idx);
1007 pr_verbose_init(c->opts, "ret %i", ret);
1016 static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
1020 if (bch2_dev_is_online(ca)) {
1021 bch_err(ca, "already have device online in slot %u",
1026 if (get_capacity(sb->bdev->bd_disk) <
1027 ca->mi.bucket_size * ca->mi.nbuckets) {
1028 bch_err(ca, "cannot online: device too small");
1032 BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
1034 if (get_capacity(sb->bdev->bd_disk) <
1035 ca->mi.bucket_size * ca->mi.nbuckets) {
1036 bch_err(ca, "device too small");
1040 ret = bch2_dev_journal_init(ca, sb->sb);
1046 memset(sb, 0, sizeof(*sb));
1048 percpu_ref_reinit(&ca->io_ref);
1053 static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb)
1058 lockdep_assert_held(&c->state_lock);
1060 if (le64_to_cpu(sb->sb->seq) >
1061 le64_to_cpu(c->disk_sb.sb->seq))
1062 bch2_sb_to_fs(c, sb->sb);
1064 BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
1065 !c->devs[sb->sb->dev_idx]);
1067 ca = bch_dev_locked(c, sb->sb->dev_idx);
1069 ret = __bch2_dev_attach_bdev(ca, sb);
1073 mutex_lock(&c->sb_lock);
1074 bch2_mark_dev_superblock(ca->fs, ca, 0);
1075 mutex_unlock(&c->sb_lock);
1077 bch2_dev_sysfs_online(c, ca);
1079 if (c->sb.nr_devices == 1)
1080 snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev);
1081 snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev);
1083 rebalance_wakeup(c);
1087 /* Device management: */
1090 * Note: this function is also used by the error paths - when a particular
1091 * device sees an error, we call it to determine whether we can just set the
1092 * device RO, or - if this function returns false - we'll set the whole
1095 * XXX: maybe we should be more explicit about whether we're changing state
1096 * because we got an error or what have you?
1098 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
1099 enum bch_member_state new_state, int flags)
1101 struct bch_devs_mask new_online_devs;
1102 struct replicas_status s;
1103 struct bch_dev *ca2;
1104 int i, nr_rw = 0, required;
1106 lockdep_assert_held(&c->state_lock);
1108 switch (new_state) {
1109 case BCH_MEMBER_STATE_RW:
1111 case BCH_MEMBER_STATE_RO:
1112 if (ca->mi.state != BCH_MEMBER_STATE_RW)
1115 /* do we have enough devices to write to? */
1116 for_each_member_device(ca2, c, i)
1118 nr_rw += ca2->mi.state == BCH_MEMBER_STATE_RW;
1120 required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
1121 ? c->opts.metadata_replicas
1122 : c->opts.metadata_replicas_required,
1123 !(flags & BCH_FORCE_IF_DATA_DEGRADED)
1124 ? c->opts.data_replicas
1125 : c->opts.data_replicas_required);
1127 return nr_rw >= required;
1128 case BCH_MEMBER_STATE_FAILED:
1129 case BCH_MEMBER_STATE_SPARE:
1130 if (ca->mi.state != BCH_MEMBER_STATE_RW &&
1131 ca->mi.state != BCH_MEMBER_STATE_RO)
1134 /* do we have enough devices to read from? */
1135 new_online_devs = bch2_online_devs(c);
1136 __clear_bit(ca->dev_idx, new_online_devs.d);
1138 s = __bch2_replicas_status(c, new_online_devs);
1140 return bch2_have_enough_devs(s, flags);
1146 static bool bch2_fs_may_start(struct bch_fs *c)
1148 struct replicas_status s;
1149 struct bch_sb_field_members *mi;
1151 unsigned i, flags = c->opts.degraded
1152 ? BCH_FORCE_IF_DEGRADED
1155 if (!c->opts.degraded) {
1156 mutex_lock(&c->sb_lock);
1157 mi = bch2_sb_get_members(c->disk_sb.sb);
1159 for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
1160 if (!bch2_dev_exists(c->disk_sb.sb, mi, i))
1163 ca = bch_dev_locked(c, i);
1165 if (!bch2_dev_is_online(ca) &&
1166 (ca->mi.state == BCH_MEMBER_STATE_RW ||
1167 ca->mi.state == BCH_MEMBER_STATE_RO)) {
1168 mutex_unlock(&c->sb_lock);
1172 mutex_unlock(&c->sb_lock);
1175 s = bch2_replicas_status(c);
1177 return bch2_have_enough_devs(s, flags);
1180 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
1182 bch2_copygc_stop(ca);
1185 * The allocator thread itself allocates btree nodes, so stop it first:
1187 bch2_dev_allocator_stop(ca);
1188 bch2_dev_allocator_remove(c, ca);
1189 bch2_dev_journal_stop(&c->journal, ca);
1192 static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
1194 lockdep_assert_held(&c->state_lock);
1196 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_RW);
1198 bch2_dev_allocator_add(c, ca);
1199 bch2_recalc_capacity(c);
1201 if (bch2_dev_allocator_start(ca))
1202 return "error starting allocator thread";
1204 if (bch2_copygc_start(c, ca))
1205 return "error starting copygc thread";
1210 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1211 enum bch_member_state new_state, int flags)
1213 struct bch_sb_field_members *mi;
1216 if (ca->mi.state == new_state)
1219 if (!bch2_dev_state_allowed(c, ca, new_state, flags))
1222 if (new_state != BCH_MEMBER_STATE_RW)
1223 __bch2_dev_read_only(c, ca);
1225 bch_notice(ca, "%s", bch2_dev_state[new_state]);
1227 mutex_lock(&c->sb_lock);
1228 mi = bch2_sb_get_members(c->disk_sb.sb);
1229 SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
1230 bch2_write_super(c);
1231 mutex_unlock(&c->sb_lock);
1233 if (new_state == BCH_MEMBER_STATE_RW &&
1234 __bch2_dev_read_write(c, ca))
1237 rebalance_wakeup(c);
1242 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1243 enum bch_member_state new_state, int flags)
1247 mutex_lock(&c->state_lock);
1248 ret = __bch2_dev_set_state(c, ca, new_state, flags);
1249 mutex_unlock(&c->state_lock);
1254 /* Device add/removal: */
1256 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
1258 struct bch_sb_field_members *mi;
1259 unsigned dev_idx = ca->dev_idx, data;
1262 mutex_lock(&c->state_lock);
1264 percpu_ref_put(&ca->ref); /* XXX */
1266 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1267 bch_err(ca, "Cannot remove without losing data");
1271 __bch2_dev_read_only(c, ca);
1274 * XXX: verify that dev_idx is really not in use anymore, anywhere
1276 * flag_data_bad() does not check btree pointers
1278 ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
1280 bch_err(ca, "Remove failed: error %i dropping data", ret);
1284 ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx);
1286 bch_err(ca, "Remove failed: error %i flushing journal", ret);
1290 data = bch2_dev_has_data(c, ca);
1292 char data_has_str[100];
1294 bch2_flags_to_text(&PBUF(data_has_str),
1295 bch2_data_types, data);
1296 bch_err(ca, "Remove failed, still has data (%s)", data_has_str);
1301 ret = bch2_btree_delete_range(c, BTREE_ID_ALLOC,
1302 POS(ca->dev_idx, 0),
1303 POS(ca->dev_idx + 1, 0),
1306 bch_err(ca, "Remove failed, error deleting alloc info");
1311 * must flush all existing journal entries, they might have
1312 * (overwritten) keys that point to the device we're removing:
1314 bch2_journal_flush_all_pins(&c->journal);
1315 ret = bch2_journal_error(&c->journal);
1317 bch_err(ca, "Remove failed, journal error");
1321 __bch2_dev_offline(c, ca);
1323 mutex_lock(&c->sb_lock);
1324 rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
1325 mutex_unlock(&c->sb_lock);
1327 percpu_ref_kill(&ca->ref);
1328 wait_for_completion(&ca->ref_completion);
1333 * Free this device's slot in the bch_member array - all pointers to
1334 * this device must be gone:
1336 mutex_lock(&c->sb_lock);
1337 mi = bch2_sb_get_members(c->disk_sb.sb);
1338 memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
1340 bch2_write_super(c);
1342 mutex_unlock(&c->sb_lock);
1343 mutex_unlock(&c->state_lock);
1346 if (ca->mi.state == BCH_MEMBER_STATE_RW &&
1347 !percpu_ref_is_zero(&ca->io_ref))
1348 __bch2_dev_read_write(c, ca);
1349 mutex_unlock(&c->state_lock);
1353 static void dev_usage_clear(struct bch_dev *ca)
1355 struct bucket_array *buckets;
1358 for_each_possible_cpu(cpu) {
1359 struct bch_dev_usage *p =
1360 per_cpu_ptr(ca->usage[0], cpu);
1361 memset(p, 0, sizeof(*p));
1364 down_read(&ca->bucket_lock);
1365 buckets = bucket_array(ca);
1367 memset(buckets->b, 0, sizeof(buckets->b[0]) * buckets->nbuckets);
1368 up_read(&ca->bucket_lock);
1371 /* Add new device to running filesystem: */
1372 int bch2_dev_add(struct bch_fs *c, const char *path)
1374 struct bch_opts opts = bch2_opts_empty();
1375 struct bch_sb_handle sb;
1377 struct bch_dev *ca = NULL;
1378 struct bch_sb_field_members *mi;
1379 struct bch_member dev_mi;
1380 unsigned dev_idx, nr_devices, u64s;
1383 ret = bch2_read_super(path, &opts, &sb);
1387 err = bch2_sb_validate(&sb);
1391 dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx];
1393 err = bch2_dev_may_add(sb.sb, c);
1397 ca = __bch2_dev_alloc(c, &dev_mi);
1399 bch2_free_super(&sb);
1403 ret = __bch2_dev_attach_bdev(ca, &sb);
1410 * We want to allocate journal on the new device before adding the new
1411 * device to the filesystem because allocating after we attach requires
1412 * spinning up the allocator thread, and the allocator thread requires
1413 * doing btree writes, which if the existing devices are RO isn't going
1416 * So we have to mark where the superblocks are, but marking allocated
1417 * data normally updates the filesystem usage too, so we have to mark,
1418 * allocate the journal, reset all the marks, then remark after we
1421 bch2_mark_dev_superblock(ca->fs, ca, 0);
1423 err = "journal alloc failed";
1424 ret = bch2_dev_journal_alloc(ca);
1428 dev_usage_clear(ca);
1430 mutex_lock(&c->state_lock);
1431 mutex_lock(&c->sb_lock);
1433 err = "insufficient space in new superblock";
1434 ret = bch2_sb_from_fs(c, ca);
1438 mi = bch2_sb_get_members(ca->disk_sb.sb);
1440 if (!bch2_sb_resize_members(&ca->disk_sb,
1441 le32_to_cpu(mi->field.u64s) +
1442 sizeof(dev_mi) / sizeof(u64))) {
1447 if (dynamic_fault("bcachefs:add:no_slot"))
1450 mi = bch2_sb_get_members(c->disk_sb.sb);
1451 for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
1452 if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx))
1455 err = "no slots available in superblock";
1460 nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
1461 u64s = (sizeof(struct bch_sb_field_members) +
1462 sizeof(struct bch_member) * nr_devices) / sizeof(u64);
1464 err = "no space in superblock for member info";
1467 mi = bch2_sb_resize_members(&c->disk_sb, u64s);
1473 mi->members[dev_idx] = dev_mi;
1474 mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds());
1475 c->disk_sb.sb->nr_devices = nr_devices;
1477 ca->disk_sb.sb->dev_idx = dev_idx;
1478 bch2_dev_attach(c, ca, dev_idx);
1480 bch2_mark_dev_superblock(c, ca, 0);
1482 bch2_write_super(c);
1483 mutex_unlock(&c->sb_lock);
1485 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1486 err = __bch2_dev_read_write(c, ca);
1491 mutex_unlock(&c->state_lock);
1495 mutex_unlock(&c->sb_lock);
1496 mutex_unlock(&c->state_lock);
1500 bch2_free_super(&sb);
1501 bch_err(c, "Unable to add device: %s", err);
1504 bch_err(c, "Error going rw after adding device: %s", err);
1508 /* Hot add existing device to running filesystem: */
1509 int bch2_dev_online(struct bch_fs *c, const char *path)
1511 struct bch_opts opts = bch2_opts_empty();
1512 struct bch_sb_handle sb = { NULL };
1513 struct bch_sb_field_members *mi;
1519 mutex_lock(&c->state_lock);
1521 ret = bch2_read_super(path, &opts, &sb);
1523 mutex_unlock(&c->state_lock);
1527 dev_idx = sb.sb->dev_idx;
1529 err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
1533 if (bch2_dev_attach_bdev(c, &sb)) {
1534 err = "bch2_dev_attach_bdev() error";
1538 ca = bch_dev_locked(c, dev_idx);
1539 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1540 err = __bch2_dev_read_write(c, ca);
1545 mutex_lock(&c->sb_lock);
1546 mi = bch2_sb_get_members(c->disk_sb.sb);
1548 mi->members[ca->dev_idx].last_mount =
1549 cpu_to_le64(ktime_get_real_seconds());
1551 bch2_write_super(c);
1552 mutex_unlock(&c->sb_lock);
1554 mutex_unlock(&c->state_lock);
1557 mutex_unlock(&c->state_lock);
1558 bch2_free_super(&sb);
1559 bch_err(c, "error bringing %s online: %s", path, err);
1563 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
1565 mutex_lock(&c->state_lock);
1567 if (!bch2_dev_is_online(ca)) {
1568 bch_err(ca, "Already offline");
1569 mutex_unlock(&c->state_lock);
1573 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1574 bch_err(ca, "Cannot offline required disk");
1575 mutex_unlock(&c->state_lock);
1579 __bch2_dev_offline(c, ca);
1581 mutex_unlock(&c->state_lock);
1585 int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
1587 struct bch_member *mi;
1590 mutex_lock(&c->state_lock);
1592 if (nbuckets < ca->mi.nbuckets) {
1593 bch_err(ca, "Cannot shrink yet");
1598 if (bch2_dev_is_online(ca) &&
1599 get_capacity(ca->disk_sb.bdev->bd_disk) <
1600 ca->mi.bucket_size * nbuckets) {
1601 bch_err(ca, "New size larger than device");
1606 ret = bch2_dev_buckets_resize(c, ca, nbuckets);
1608 bch_err(ca, "Resize error: %i", ret);
1612 mutex_lock(&c->sb_lock);
1613 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1614 mi->nbuckets = cpu_to_le64(nbuckets);
1616 bch2_write_super(c);
1617 mutex_unlock(&c->sb_lock);
1619 bch2_recalc_capacity(c);
1621 mutex_unlock(&c->state_lock);
1625 /* return with ref on ca->ref: */
1626 struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path)
1634 ret = lookup_bdev(path, &dev);
1636 return ERR_PTR(ret);
1638 for_each_member_device(ca, c, i)
1639 if (ca->disk_sb.bdev->bd_dev == dev)
1642 ca = ERR_PTR(-ENOENT);
1647 /* Filesystem open: */
1649 struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
1650 struct bch_opts opts)
1652 struct bch_sb_handle *sb = NULL;
1653 struct bch_fs *c = NULL;
1654 unsigned i, best_sb = 0;
1658 pr_verbose_init(opts, "");
1661 c = ERR_PTR(-EINVAL);
1665 if (!try_module_get(THIS_MODULE)) {
1666 c = ERR_PTR(-ENODEV);
1670 sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
1674 for (i = 0; i < nr_devices; i++) {
1675 ret = bch2_read_super(devices[i], &opts, &sb[i]);
1679 err = bch2_sb_validate(&sb[i]);
1684 for (i = 1; i < nr_devices; i++)
1685 if (le64_to_cpu(sb[i].sb->seq) >
1686 le64_to_cpu(sb[best_sb].sb->seq))
1689 for (i = 0; i < nr_devices; i++) {
1690 err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
1696 c = bch2_fs_alloc(sb[best_sb].sb, opts);
1700 err = "bch2_dev_online() error";
1701 mutex_lock(&c->state_lock);
1702 for (i = 0; i < nr_devices; i++)
1703 if (bch2_dev_attach_bdev(c, &sb[i])) {
1704 mutex_unlock(&c->state_lock);
1707 mutex_unlock(&c->state_lock);
1709 err = "insufficient devices";
1710 if (!bch2_fs_may_start(c))
1713 if (!c->opts.nostart) {
1714 err = bch2_fs_start(c);
1720 module_put(THIS_MODULE);
1722 pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
1725 pr_err("bch_fs_open err opening %s: %s",
1731 for (i = 0; i < nr_devices; i++)
1732 bch2_free_super(&sb[i]);
1737 static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb,
1738 struct bch_opts opts)
1742 bool allocated_fs = false;
1744 err = bch2_sb_validate(sb);
1748 mutex_lock(&bch_fs_list_lock);
1749 c = __bch2_uuid_to_fs(sb->sb->uuid);
1751 closure_get(&c->cl);
1753 err = bch2_dev_in_fs(c->disk_sb.sb, sb->sb);
1757 c = bch2_fs_alloc(sb->sb, opts);
1758 err = "cannot allocate memory";
1762 allocated_fs = true;
1765 err = "bch2_dev_online() error";
1767 mutex_lock(&c->sb_lock);
1768 if (bch2_dev_attach_bdev(c, sb)) {
1769 mutex_unlock(&c->sb_lock);
1772 mutex_unlock(&c->sb_lock);
1774 if (!c->opts.nostart && bch2_fs_may_start(c)) {
1775 err = bch2_fs_start(c);
1780 closure_put(&c->cl);
1781 mutex_unlock(&bch_fs_list_lock);
1785 mutex_unlock(&bch_fs_list_lock);
1790 closure_put(&c->cl);
1795 const char *bch2_fs_open_incremental(const char *path)
1797 struct bch_sb_handle sb;
1798 struct bch_opts opts = bch2_opts_empty();
1801 if (bch2_read_super(path, &opts, &sb))
1802 return "error reading superblock";
1804 err = __bch2_fs_open_incremental(&sb, opts);
1805 bch2_free_super(&sb);
1810 /* Global interfaces/init */
1812 static void bcachefs_exit(void)
1816 bch2_chardev_exit();
1818 kset_unregister(bcachefs_kset);
1821 static int __init bcachefs_init(void)
1823 bch2_bkey_pack_test();
1824 bch2_inode_pack_test();
1826 if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
1827 bch2_chardev_init() ||
1838 #define BCH_DEBUG_PARAM(name, description) \
1840 module_param_named(name, bch2_##name, bool, 0644); \
1841 MODULE_PARM_DESC(name, description);
1843 #undef BCH_DEBUG_PARAM
1845 unsigned bch2_metadata_version = bcachefs_metadata_version_current;
1846 module_param_named(version, bch2_metadata_version, uint, 0400);
1848 module_exit(bcachefs_exit);
1849 module_init(bcachefs_init);