2 * bcachefs setup/teardown code, and some metadata io - read a superblock and
3 * figure out what to do with it.
5 * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
6 * Copyright 2012 Google, Inc.
11 #include "btree_cache.h"
13 #include "btree_update.h"
14 #include "btree_update_interior.h"
21 #include "disk_groups.h"
29 #include "journal_io.h"
30 #include "journal_reclaim.h"
42 #include <linux/backing-dev.h>
43 #include <linux/blkdev.h>
44 #include <linux/debugfs.h>
45 #include <linux/device.h>
46 #include <linux/genhd.h>
47 #include <linux/idr.h>
48 #include <linux/kthread.h>
49 #include <linux/module.h>
50 #include <linux/percpu.h>
51 #include <linux/random.h>
52 #include <linux/sysfs.h>
53 #include <crypto/hash.h>
55 #include <trace/events/bcachefs.h>
57 MODULE_LICENSE("GPL");
58 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
61 struct kobj_type type ## _ktype = { \
62 .release = type ## _release, \
63 .sysfs_ops = &type ## _sysfs_ops, \
64 .default_attrs = type ## _files \
67 static void bch2_fs_release(struct kobject *);
68 static void bch2_dev_release(struct kobject *);
70 static void bch2_fs_internal_release(struct kobject *k)
74 static void bch2_fs_opts_dir_release(struct kobject *k)
78 static void bch2_fs_time_stats_release(struct kobject *k)
82 static KTYPE(bch2_fs);
83 static KTYPE(bch2_fs_internal);
84 static KTYPE(bch2_fs_opts_dir);
85 static KTYPE(bch2_fs_time_stats);
86 static KTYPE(bch2_dev);
88 static struct kset *bcachefs_kset;
89 static LIST_HEAD(bch_fs_list);
90 static DEFINE_MUTEX(bch_fs_list_lock);
92 static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
94 static void bch2_dev_free(struct bch_dev *);
95 static int bch2_dev_alloc(struct bch_fs *, unsigned);
96 static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *);
97 static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
99 struct bch_fs *bch2_bdev_to_fs(struct block_device *bdev)
105 mutex_lock(&bch_fs_list_lock);
108 list_for_each_entry(c, &bch_fs_list, list)
109 for_each_member_device_rcu(ca, c, i, NULL)
110 if (ca->disk_sb.bdev == bdev) {
117 mutex_unlock(&bch_fs_list_lock);
122 static struct bch_fs *__bch2_uuid_to_fs(uuid_le uuid)
126 lockdep_assert_held(&bch_fs_list_lock);
128 list_for_each_entry(c, &bch_fs_list, list)
129 if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid_le)))
135 struct bch_fs *bch2_uuid_to_fs(uuid_le uuid)
139 mutex_lock(&bch_fs_list_lock);
140 c = __bch2_uuid_to_fs(uuid);
143 mutex_unlock(&bch_fs_list_lock);
148 int bch2_congested(void *data, int bdi_bits)
150 struct bch_fs *c = data;
151 struct backing_dev_info *bdi;
157 if (bdi_bits & (1 << WB_sync_congested)) {
158 /* Reads - check all devices: */
159 for_each_readable_member(ca, c, i) {
160 bdi = ca->disk_sb.bdev->bd_bdi;
162 if (bdi_congested(bdi, bdi_bits)) {
168 unsigned target = READ_ONCE(c->opts.foreground_target);
169 const struct bch_devs_mask *devs = target
170 ? bch2_target_to_mask(c, target)
171 : &c->rw_devs[BCH_DATA_USER];
173 for_each_member_device_rcu(ca, c, i, devs) {
174 bdi = ca->disk_sb.bdev->bd_bdi;
176 if (bdi_congested(bdi, bdi_bits)) {
187 /* Filesystem RO/RW: */
190 * For startup/shutdown of RW stuff, the dependencies are:
192 * - foreground writes depend on copygc and rebalance (to free up space)
194 * - copygc and rebalance depend on mark and sweep gc (they actually probably
195 * don't because they either reserve ahead of time or don't block if
196 * allocations fail, but allocations can require mark and sweep gc to run
197 * because of generation number wraparound)
199 * - all of the above depends on the allocator threads
201 * - allocator depends on the journal (when it rewrites prios and gens)
204 static void bch_fs_mark_clean(struct bch_fs *c)
206 if (!bch2_journal_error(&c->journal) &&
207 !test_bit(BCH_FS_ERROR, &c->flags) &&
208 !test_bit(BCH_FS_EMERGENCY_RO, &c->flags)) {
209 mutex_lock(&c->sb_lock);
210 SET_BCH_SB_CLEAN(c->disk_sb.sb, true);
212 mutex_unlock(&c->sb_lock);
216 static void __bch2_fs_read_only(struct bch_fs *c)
221 bch2_rebalance_stop(c);
223 for_each_member_device(ca, c, i)
224 bch2_copygc_stop(ca);
226 bch2_gc_thread_stop(c);
229 * Flush journal before stopping allocators, because flushing journal
230 * blacklist entries involves allocating new btree nodes:
232 bch2_journal_flush_pins(&c->journal, U64_MAX - 1);
234 for_each_member_device(ca, c, i)
235 bch2_dev_allocator_stop(ca);
237 bch2_journal_flush_all_pins(&c->journal);
240 * We need to explicitly wait on btree interior updates to complete
241 * before stopping the journal, flushing all journal pins isn't
242 * sufficient, because in the BTREE_INTERIOR_UPDATING_ROOT case btree
243 * interior updates have to drop their journal pin before they're
246 closure_wait_event(&c->btree_interior_update_wait,
247 !bch2_btree_interior_updates_nr_pending(c));
249 if (!test_bit(BCH_FS_EMERGENCY_RO, &c->flags))
250 bch2_btree_verify_flushed(c);
252 bch2_fs_journal_stop(&c->journal);
255 * the journal kicks off btree writes via reclaim - wait for in flight
256 * writes after stopping journal:
258 if (test_bit(BCH_FS_EMERGENCY_RO, &c->flags))
259 bch2_btree_flush_all_writes(c);
262 * After stopping journal:
264 for_each_member_device(ca, c, i)
265 bch2_dev_allocator_remove(c, ca);
268 static void bch2_writes_disabled(struct percpu_ref *writes)
270 struct bch_fs *c = container_of(writes, struct bch_fs, writes);
272 set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
273 wake_up(&bch_read_only_wait);
276 void bch2_fs_read_only(struct bch_fs *c)
278 if (c->state != BCH_FS_STARTING &&
279 c->state != BCH_FS_RW)
282 if (test_bit(BCH_FS_ERROR, &c->flags))
286 * Block new foreground-end write operations from starting - any new
287 * writes will return -EROFS:
289 * (This is really blocking new _allocations_, writes to previously
290 * allocated space can still happen until stopping the allocator in
291 * bch2_dev_allocator_stop()).
293 percpu_ref_kill(&c->writes);
295 cancel_delayed_work(&c->pd_controllers_update);
298 * If we're not doing an emergency shutdown, we want to wait on
299 * outstanding writes to complete so they don't see spurious errors due
300 * to shutting down the allocator:
302 * If we are doing an emergency shutdown outstanding writes may
303 * hang until we shutdown the allocator so we don't want to wait
304 * on outstanding writes before shutting everything down - but
305 * we do need to wait on them before returning and signalling
306 * that going RO is complete:
308 wait_event(bch_read_only_wait,
309 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
310 test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
312 __bch2_fs_read_only(c);
314 bch_fs_mark_clean(c);
316 wait_event(bch_read_only_wait,
317 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
319 clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
320 c->state = BCH_FS_RO;
323 static void bch2_fs_read_only_work(struct work_struct *work)
326 container_of(work, struct bch_fs, read_only_work);
328 mutex_lock(&c->state_lock);
329 bch2_fs_read_only(c);
330 mutex_unlock(&c->state_lock);
333 static void bch2_fs_read_only_async(struct bch_fs *c)
335 queue_work(system_long_wq, &c->read_only_work);
338 bool bch2_fs_emergency_read_only(struct bch_fs *c)
340 bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
342 bch2_fs_read_only_async(c);
343 bch2_journal_halt(&c->journal);
345 wake_up(&bch_read_only_wait);
349 const char *bch2_fs_read_write(struct bch_fs *c)
352 const char *err = NULL;
355 if (c->state != BCH_FS_STARTING &&
356 c->state != BCH_FS_RO)
359 for_each_rw_member(ca, c, i)
360 bch2_dev_allocator_add(c, ca);
361 bch2_recalc_capacity(c);
363 err = "error starting allocator thread";
364 for_each_rw_member(ca, c, i)
365 if (bch2_dev_allocator_start(ca)) {
366 percpu_ref_put(&ca->io_ref);
370 err = "error starting btree GC thread";
371 if (bch2_gc_thread_start(c))
374 err = "error starting copygc thread";
375 for_each_rw_member(ca, c, i)
376 if (bch2_copygc_start(c, ca)) {
377 percpu_ref_put(&ca->io_ref);
381 err = "error starting rebalance thread";
382 if (bch2_rebalance_start(c))
385 schedule_delayed_work(&c->pd_controllers_update, 5 * HZ);
387 if (c->state != BCH_FS_STARTING)
388 percpu_ref_reinit(&c->writes);
390 c->state = BCH_FS_RW;
393 __bch2_fs_read_only(c);
397 /* Filesystem startup/shutdown: */
399 static void bch2_fs_free(struct bch_fs *c)
401 #define BCH_TIME_STAT(name) \
402 bch2_time_stats_exit(&c->name##_time);
406 bch2_fs_quota_exit(c);
407 bch2_fs_fsio_exit(c);
408 bch2_fs_encryption_exit(c);
410 bch2_fs_btree_cache_exit(c);
411 bch2_fs_journal_exit(&c->journal);
412 bch2_io_clock_exit(&c->io_clock[WRITE]);
413 bch2_io_clock_exit(&c->io_clock[READ]);
414 bch2_fs_compress_exit(c);
415 lg_lock_free(&c->usage_lock);
416 free_percpu(c->usage_percpu);
417 mempool_exit(&c->btree_bounce_pool);
418 bioset_exit(&c->btree_bio);
419 mempool_exit(&c->btree_interior_update_pool);
420 mempool_exit(&c->btree_reserve_pool);
421 mempool_exit(&c->fill_iter);
422 percpu_ref_exit(&c->writes);
423 kfree(rcu_dereference_protected(c->replicas, 1));
424 kfree(rcu_dereference_protected(c->disk_groups, 1));
427 destroy_workqueue(c->copygc_wq);
429 destroy_workqueue(c->wq);
431 free_pages((unsigned long) c->disk_sb.sb,
432 c->disk_sb.page_order);
433 kvpfree(c, sizeof(*c));
434 module_put(THIS_MODULE);
437 static void bch2_fs_release(struct kobject *kobj)
439 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
444 void bch2_fs_stop(struct bch_fs *c)
449 mutex_lock(&c->state_lock);
450 BUG_ON(c->state == BCH_FS_STOPPING);
451 c->state = BCH_FS_STOPPING;
452 mutex_unlock(&c->state_lock);
454 for_each_member_device(ca, c, i)
455 if (ca->kobj.state_in_sysfs &&
457 sysfs_remove_link(&part_to_dev(ca->disk_sb.bdev->bd_part)->kobj,
460 if (c->kobj.state_in_sysfs)
461 kobject_del(&c->kobj);
463 bch2_fs_debug_exit(c);
464 bch2_fs_chardev_exit(c);
466 kobject_put(&c->time_stats);
467 kobject_put(&c->opts_dir);
468 kobject_put(&c->internal);
470 mutex_lock(&bch_fs_list_lock);
472 mutex_unlock(&bch_fs_list_lock);
474 closure_sync(&c->cl);
475 closure_debug_destroy(&c->cl);
477 mutex_lock(&c->state_lock);
478 __bch2_fs_read_only(c);
479 mutex_unlock(&c->state_lock);
481 bch_fs_mark_clean(c);
483 /* btree prefetch might have kicked off reads in the background: */
484 bch2_btree_flush_all_reads(c);
486 for_each_member_device(ca, c, i)
487 cancel_work_sync(&ca->io_error_work);
489 cancel_work_sync(&c->btree_write_error_work);
490 cancel_delayed_work_sync(&c->pd_controllers_update);
491 cancel_work_sync(&c->read_only_work);
493 for (i = 0; i < c->sb.nr_devices; i++)
495 bch2_dev_free(rcu_dereference_protected(c->devs[i], 1));
497 kobject_put(&c->kobj);
500 static const char *bch2_fs_online(struct bch_fs *c)
503 const char *err = NULL;
507 lockdep_assert_held(&bch_fs_list_lock);
509 if (!list_empty(&c->list))
512 if (__bch2_uuid_to_fs(c->sb.uuid))
513 return "filesystem UUID already open";
515 ret = bch2_fs_chardev_init(c);
517 return "error creating character device";
519 bch2_fs_debug_init(c);
521 if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ||
522 kobject_add(&c->internal, &c->kobj, "internal") ||
523 kobject_add(&c->opts_dir, &c->kobj, "options") ||
524 kobject_add(&c->time_stats, &c->kobj, "time_stats") ||
525 bch2_opts_create_sysfs_files(&c->opts_dir))
526 return "error creating sysfs objects";
528 mutex_lock(&c->state_lock);
530 err = "error creating sysfs objects";
531 __for_each_member_device(ca, c, i, NULL)
532 if (bch2_dev_sysfs_online(c, ca))
535 list_add(&c->list, &bch_fs_list);
538 mutex_unlock(&c->state_lock);
542 static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
544 struct bch_sb_field_members *mi;
546 unsigned i, iter_size;
549 pr_verbose_init(opts, "");
551 c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO);
555 __module_get(THIS_MODULE);
558 c->disk_sb.fs_sb = true;
560 mutex_init(&c->state_lock);
561 mutex_init(&c->sb_lock);
562 mutex_init(&c->replicas_gc_lock);
563 mutex_init(&c->btree_root_lock);
564 INIT_WORK(&c->read_only_work, bch2_fs_read_only_work);
566 init_rwsem(&c->gc_lock);
568 #define BCH_TIME_STAT(name) \
569 bch2_time_stats_init(&c->name##_time);
573 bch2_fs_allocator_init(c);
574 bch2_fs_rebalance_init(c);
575 bch2_fs_quota_init(c);
577 INIT_LIST_HEAD(&c->list);
579 INIT_LIST_HEAD(&c->btree_interior_update_list);
580 mutex_init(&c->btree_reserve_cache_lock);
581 mutex_init(&c->btree_interior_update_lock);
583 mutex_init(&c->bio_bounce_pages_lock);
585 bio_list_init(&c->btree_write_error_list);
586 spin_lock_init(&c->btree_write_error_lock);
587 INIT_WORK(&c->btree_write_error_work, bch2_btree_write_error_work);
589 INIT_LIST_HEAD(&c->fsck_errors);
590 mutex_init(&c->fsck_error_lock);
592 seqcount_init(&c->gc_pos_lock);
594 c->copy_gc_enabled = 1;
595 c->rebalance_enabled = 1;
596 c->rebalance_percent = 10;
597 c->promote_whole_extents = true;
599 c->journal.write_time = &c->journal_write_time;
600 c->journal.delay_time = &c->journal_delay_time;
601 c->journal.blocked_time = &c->journal_blocked_time;
602 c->journal.flush_seq_time = &c->journal_flush_seq_time;
604 bch2_fs_btree_cache_init_early(&c->btree_cache);
606 mutex_lock(&c->sb_lock);
608 if (bch2_sb_to_fs(c, sb)) {
609 mutex_unlock(&c->sb_lock);
613 mutex_unlock(&c->sb_lock);
615 scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid);
617 c->opts = bch2_opts_default;
618 bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb));
619 bch2_opts_apply(&c->opts, opts);
621 c->block_bits = ilog2(c->opts.block_size);
622 c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
624 c->opts.nochanges |= c->opts.noreplay;
625 c->opts.read_only |= c->opts.nochanges;
627 if (bch2_fs_init_fault("fs_alloc"))
630 iter_size = sizeof(struct btree_node_iter_large) +
631 (btree_blocks(c) + 1) * 2 *
632 sizeof(struct btree_node_iter_set);
634 if (!(c->wq = alloc_workqueue("bcachefs",
635 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
636 !(c->copygc_wq = alloc_workqueue("bcache_copygc",
637 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
638 percpu_ref_init(&c->writes, bch2_writes_disabled, 0, GFP_KERNEL) ||
639 mempool_init_kmalloc_pool(&c->btree_reserve_pool, 1,
640 sizeof(struct btree_reserve)) ||
641 mempool_init_kmalloc_pool(&c->btree_interior_update_pool, 1,
642 sizeof(struct btree_update)) ||
643 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
644 bioset_init(&c->btree_bio, 1,
645 max(offsetof(struct btree_read_bio, bio),
646 offsetof(struct btree_write_bio, wbio.bio)),
647 BIOSET_NEED_BVECS) ||
648 !(c->usage_percpu = alloc_percpu(struct bch_fs_usage)) ||
649 lg_lock_init(&c->usage_lock) ||
650 mempool_init_vp_pool(&c->btree_bounce_pool, 1, btree_bytes(c)) ||
651 bch2_io_clock_init(&c->io_clock[READ]) ||
652 bch2_io_clock_init(&c->io_clock[WRITE]) ||
653 bch2_fs_journal_init(&c->journal) ||
654 bch2_fs_btree_cache_init(c) ||
655 bch2_fs_io_init(c) ||
656 bch2_fs_encryption_init(c) ||
657 bch2_fs_compress_init(c) ||
658 bch2_fs_fsio_init(c))
661 mi = bch2_sb_get_members(c->disk_sb.sb);
662 for (i = 0; i < c->sb.nr_devices; i++)
663 if (bch2_dev_exists(c->disk_sb.sb, mi, i) &&
664 bch2_dev_alloc(c, i))
668 * Now that all allocations have succeeded, init various refcounty
669 * things that let us shutdown:
671 closure_init(&c->cl, NULL);
673 c->kobj.kset = bcachefs_kset;
674 kobject_init(&c->kobj, &bch2_fs_ktype);
675 kobject_init(&c->internal, &bch2_fs_internal_ktype);
676 kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype);
677 kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype);
679 mutex_lock(&bch_fs_list_lock);
680 err = bch2_fs_online(c);
681 mutex_unlock(&bch_fs_list_lock);
683 bch_err(c, "bch2_fs_online() error: %s", err);
687 pr_verbose_init(opts, "ret %i", c ? 0 : -ENOMEM);
695 const char *bch2_fs_start(struct bch_fs *c)
697 const char *err = "cannot allocate memory";
698 struct bch_sb_field_members *mi;
706 mutex_lock(&c->state_lock);
708 BUG_ON(c->state != BCH_FS_STARTING);
710 mutex_lock(&c->sb_lock);
711 for_each_online_member(ca, c, i)
712 bch2_sb_from_fs(c, ca);
713 mutex_unlock(&c->sb_lock);
715 for_each_rw_member(ca, c, i)
716 bch2_dev_allocator_add(c, ca);
717 bch2_recalc_capacity(c);
719 if (BCH_SB_INITIALIZED(c->disk_sb.sb)) {
720 ret = bch2_journal_read(c, &journal);
724 j = &list_entry(journal.prev, struct journal_replay, list)->j;
726 c->bucket_clock[READ].hand = le16_to_cpu(j->read_clock);
727 c->bucket_clock[WRITE].hand = le16_to_cpu(j->write_clock);
729 for (i = 0; i < BTREE_ID_NR; i++) {
733 k = bch2_journal_find_btree_root(c, j, i, &level);
737 err = "invalid btree root pointer";
741 err = "error reading btree root";
742 if (bch2_btree_root_read(c, i, k, level)) {
743 if (i != BTREE_ID_ALLOC)
746 mustfix_fsck_err(c, "error reading btree root");
750 for (i = 0; i < BTREE_ID_NR; i++)
751 if (!c->btree_roots[i].b)
752 bch2_btree_root_alloc(c, i);
754 err = "error reading allocation information";
755 ret = bch2_alloc_read(c, &journal);
759 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
761 bch_verbose(c, "starting mark and sweep:");
762 err = "error in recovery";
763 ret = bch2_initial_gc(c, &journal);
766 bch_verbose(c, "mark and sweep done");
768 if (c->opts.noreplay)
772 * bch2_fs_journal_start() can't happen sooner, or btree_gc_finish()
773 * will give spurious errors about oldest_gen > bucket_gen -
774 * this is a hack but oh well.
776 bch2_fs_journal_start(&c->journal);
778 err = "error starting allocator";
779 if (bch2_fs_allocator_start(c))
782 bch_verbose(c, "starting journal replay:");
783 err = "journal replay failed";
784 ret = bch2_journal_replay(c, &journal);
787 bch_verbose(c, "journal replay done");
789 if (c->opts.norecovery)
792 bch_verbose(c, "starting fsck:");
793 err = "error in fsck";
794 ret = bch2_fsck(c, !c->opts.nofsck);
797 bch_verbose(c, "fsck done");
799 if (enabled_qtypes(c)) {
800 bch_verbose(c, "reading quotas:");
801 ret = bch2_fs_quota_read(c);
804 bch_verbose(c, "quotas done");
807 struct bch_inode_unpacked inode;
808 struct bkey_inode_buf packed_inode;
810 bch_notice(c, "initializing new filesystem");
812 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
814 ret = bch2_initial_gc(c, &journal);
818 err = "unable to allocate journal buckets";
819 for_each_online_member(ca, c, i)
820 if (bch2_dev_journal_alloc(ca)) {
821 percpu_ref_put(&ca->io_ref);
825 for (i = 0; i < BTREE_ID_NR; i++)
826 bch2_btree_root_alloc(c, i);
829 * journal_res_get() will crash if called before this has
830 * set up the journal.pin FIFO and journal.cur pointer:
832 bch2_fs_journal_start(&c->journal);
833 bch2_journal_set_replay_done(&c->journal);
835 err = "error starting allocator";
836 if (bch2_fs_allocator_start(c))
839 bch2_inode_init(c, &inode, 0, 0,
840 S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO, 0, NULL);
841 inode.bi_inum = BCACHEFS_ROOT_INO;
843 bch2_inode_pack(&packed_inode, &inode);
845 err = "error creating root directory";
846 if (bch2_btree_insert(c, BTREE_ID_INODES,
847 &packed_inode.inode.k_i,
848 NULL, NULL, NULL, 0))
851 if (enabled_qtypes(c)) {
852 ret = bch2_fs_quota_read(c);
857 err = "error writing first journal entry";
858 if (bch2_journal_meta(&c->journal))
862 err = "dynamic fault";
863 if (bch2_fs_init_fault("fs_start"))
866 if (c->opts.read_only) {
867 bch2_fs_read_only(c);
869 err = bch2_fs_read_write(c);
874 mutex_lock(&c->sb_lock);
875 mi = bch2_sb_get_members(c->disk_sb.sb);
876 now = ktime_get_seconds();
878 for_each_member_device(ca, c, i)
879 mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
881 SET_BCH_SB_INITIALIZED(c->disk_sb.sb, true);
882 SET_BCH_SB_CLEAN(c->disk_sb.sb, false);
885 mutex_unlock(&c->sb_lock);
887 set_bit(BCH_FS_STARTED, &c->flags);
891 mutex_unlock(&c->state_lock);
892 bch2_journal_entries_free(&journal);
897 case BCH_FSCK_ERRORS_NOT_FIXED:
898 bch_err(c, "filesystem contains errors: please report this to the developers");
899 pr_cont("mount with -o fix_errors to repair\n");
902 case BCH_FSCK_REPAIR_UNIMPLEMENTED:
903 bch_err(c, "filesystem contains errors: please report this to the developers");
904 pr_cont("repair unimplemented: inform the developers so that it can be added\n");
907 case BCH_FSCK_REPAIR_IMPOSSIBLE:
908 bch_err(c, "filesystem contains errors, but repair impossible");
911 case BCH_FSCK_UNKNOWN_VERSION:
912 err = "unknown metadata version";;
915 err = "cannot allocate memory";
923 set_bit(BCH_FS_ERROR, &c->flags);
927 static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
929 struct bch_sb_field_members *sb_mi;
931 sb_mi = bch2_sb_get_members(sb);
933 return "Invalid superblock: member info area missing";
935 if (le16_to_cpu(sb->block_size) != c->opts.block_size)
936 return "mismatched block size";
938 if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
939 BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
940 return "new cache bucket size is too small";
945 static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
947 struct bch_sb *newest =
948 le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
949 struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
951 if (uuid_le_cmp(fs->uuid, sb->uuid))
952 return "device not a member of filesystem";
954 if (!bch2_dev_exists(newest, mi, sb->dev_idx))
955 return "device has been removed";
957 if (fs->block_size != sb->block_size)
958 return "mismatched block size";
963 /* Device startup/shutdown: */
965 static void bch2_dev_release(struct kobject *kobj)
967 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
972 static void bch2_dev_free(struct bch_dev *ca)
974 cancel_work_sync(&ca->io_error_work);
976 if (ca->kobj.state_in_sysfs &&
978 sysfs_remove_link(&part_to_dev(ca->disk_sb.bdev->bd_part)->kobj,
981 if (ca->kobj.state_in_sysfs)
982 kobject_del(&ca->kobj);
984 bch2_free_super(&ca->disk_sb);
985 bch2_dev_journal_exit(ca);
987 free_percpu(ca->io_done);
988 bioset_exit(&ca->replica_set);
989 bch2_dev_buckets_free(ca);
991 bch2_time_stats_exit(&ca->io_latency[WRITE]);
992 bch2_time_stats_exit(&ca->io_latency[READ]);
994 percpu_ref_exit(&ca->io_ref);
995 percpu_ref_exit(&ca->ref);
996 kobject_put(&ca->kobj);
999 static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca)
1002 lockdep_assert_held(&c->state_lock);
1004 if (percpu_ref_is_zero(&ca->io_ref))
1007 __bch2_dev_read_only(c, ca);
1009 reinit_completion(&ca->io_ref_completion);
1010 percpu_ref_kill(&ca->io_ref);
1011 wait_for_completion(&ca->io_ref_completion);
1013 if (ca->kobj.state_in_sysfs) {
1014 struct kobject *block =
1015 &part_to_dev(ca->disk_sb.bdev->bd_part)->kobj;
1017 sysfs_remove_link(block, "bcachefs");
1018 sysfs_remove_link(&ca->kobj, "block");
1021 bch2_free_super(&ca->disk_sb);
1022 bch2_dev_journal_exit(ca);
1025 static void bch2_dev_ref_complete(struct percpu_ref *ref)
1027 struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
1029 complete(&ca->ref_completion);
1032 static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
1034 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
1036 complete(&ca->io_ref_completion);
1039 static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca)
1043 if (!c->kobj.state_in_sysfs)
1046 if (!ca->kobj.state_in_sysfs) {
1047 ret = kobject_add(&ca->kobj, &c->kobj,
1048 "dev-%u", ca->dev_idx);
1053 if (ca->disk_sb.bdev) {
1054 struct kobject *block =
1055 &part_to_dev(ca->disk_sb.bdev->bd_part)->kobj;
1057 ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
1060 ret = sysfs_create_link(&ca->kobj, block, "block");
1068 static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c,
1069 struct bch_member *member)
1073 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1077 kobject_init(&ca->kobj, &bch2_dev_ktype);
1078 init_completion(&ca->ref_completion);
1079 init_completion(&ca->io_ref_completion);
1081 init_rwsem(&ca->bucket_lock);
1083 writepoint_init(&ca->copygc_write_point, BCH_DATA_USER);
1085 spin_lock_init(&ca->freelist_lock);
1086 bch2_dev_copygc_init(ca);
1088 INIT_WORK(&ca->io_error_work, bch2_io_error_work);
1090 bch2_time_stats_init(&ca->io_latency[READ]);
1091 bch2_time_stats_init(&ca->io_latency[WRITE]);
1093 ca->mi = bch2_mi_to_cpu(member);
1094 ca->uuid = member->uuid;
1096 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
1098 percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
1099 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
1100 bch2_dev_buckets_alloc(c, ca) ||
1101 bioset_init(&ca->replica_set, 4,
1102 offsetof(struct bch_write_bio, bio), 0) ||
1103 !(ca->io_done = alloc_percpu(*ca->io_done)))
1112 static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca,
1115 ca->dev_idx = dev_idx;
1116 __set_bit(ca->dev_idx, ca->self.d);
1117 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
1120 rcu_assign_pointer(c->devs[ca->dev_idx], ca);
1122 if (bch2_dev_sysfs_online(c, ca))
1123 pr_warn("error creating sysfs objects");
1126 static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
1128 struct bch_member *member =
1129 bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx;
1130 struct bch_dev *ca = NULL;
1133 pr_verbose_init(c->opts, "");
1135 if (bch2_fs_init_fault("dev_alloc"))
1138 ca = __bch2_dev_alloc(c, member);
1142 bch2_dev_attach(c, ca, dev_idx);
1144 pr_verbose_init(c->opts, "ret %i", ret);
1153 static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
1157 if (bch2_dev_is_online(ca)) {
1158 bch_err(ca, "already have device online in slot %u",
1163 if (get_capacity(sb->bdev->bd_disk) <
1164 ca->mi.bucket_size * ca->mi.nbuckets) {
1165 bch_err(ca, "cannot online: device too small");
1169 BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
1171 if (get_capacity(sb->bdev->bd_disk) <
1172 ca->mi.bucket_size * ca->mi.nbuckets) {
1173 bch_err(ca, "device too small");
1177 ret = bch2_dev_journal_init(ca, sb->sb);
1183 if (sb->mode & FMODE_EXCL)
1184 ca->disk_sb.bdev->bd_holder = ca;
1185 memset(sb, 0, sizeof(*sb));
1188 mutex_lock(&ca->fs->sb_lock);
1190 bch2_mark_dev_superblock(ca->fs, ca, BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE);
1193 mutex_unlock(&ca->fs->sb_lock);
1195 percpu_ref_reinit(&ca->io_ref);
1200 static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb)
1205 lockdep_assert_held(&c->state_lock);
1207 if (le64_to_cpu(sb->sb->seq) >
1208 le64_to_cpu(c->disk_sb.sb->seq))
1209 bch2_sb_to_fs(c, sb->sb);
1211 BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
1212 !c->devs[sb->sb->dev_idx]);
1214 ca = bch_dev_locked(c, sb->sb->dev_idx);
1216 ret = __bch2_dev_attach_bdev(ca, sb);
1220 if (c->sb.nr_devices == 1)
1221 bdevname(ca->disk_sb.bdev, c->name);
1222 bdevname(ca->disk_sb.bdev, ca->name);
1224 rebalance_wakeup(c);
1228 /* Device management: */
1231 * Note: this function is also used by the error paths - when a particular
1232 * device sees an error, we call it to determine whether we can just set the
1233 * device RO, or - if this function returns false - we'll set the whole
1236 * XXX: maybe we should be more explicit about whether we're changing state
1237 * because we got an error or what have you?
1239 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
1240 enum bch_member_state new_state, int flags)
1242 struct bch_devs_mask new_online_devs;
1243 struct replicas_status s;
1244 struct bch_dev *ca2;
1245 int i, nr_rw = 0, required;
1247 lockdep_assert_held(&c->state_lock);
1249 switch (new_state) {
1250 case BCH_MEMBER_STATE_RW:
1252 case BCH_MEMBER_STATE_RO:
1253 if (ca->mi.state != BCH_MEMBER_STATE_RW)
1256 /* do we have enough devices to write to? */
1257 for_each_member_device(ca2, c, i)
1259 nr_rw += ca2->mi.state == BCH_MEMBER_STATE_RW;
1261 required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
1262 ? c->opts.metadata_replicas
1263 : c->opts.metadata_replicas_required,
1264 !(flags & BCH_FORCE_IF_DATA_DEGRADED)
1265 ? c->opts.data_replicas
1266 : c->opts.data_replicas_required);
1268 return nr_rw >= required;
1269 case BCH_MEMBER_STATE_FAILED:
1270 case BCH_MEMBER_STATE_SPARE:
1271 if (ca->mi.state != BCH_MEMBER_STATE_RW &&
1272 ca->mi.state != BCH_MEMBER_STATE_RO)
1275 /* do we have enough devices to read from? */
1276 new_online_devs = bch2_online_devs(c);
1277 __clear_bit(ca->dev_idx, new_online_devs.d);
1279 s = __bch2_replicas_status(c, new_online_devs);
1281 return bch2_have_enough_devs(s, flags);
1287 static bool bch2_fs_may_start(struct bch_fs *c)
1289 struct replicas_status s;
1290 struct bch_sb_field_members *mi;
1292 unsigned i, flags = c->opts.degraded
1293 ? BCH_FORCE_IF_DEGRADED
1296 if (!c->opts.degraded) {
1297 mutex_lock(&c->sb_lock);
1298 mi = bch2_sb_get_members(c->disk_sb.sb);
1300 for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
1301 if (!bch2_dev_exists(c->disk_sb.sb, mi, i))
1304 ca = bch_dev_locked(c, i);
1306 if (!bch2_dev_is_online(ca) &&
1307 (ca->mi.state == BCH_MEMBER_STATE_RW ||
1308 ca->mi.state == BCH_MEMBER_STATE_RO)) {
1309 mutex_unlock(&c->sb_lock);
1313 mutex_unlock(&c->sb_lock);
1316 s = bch2_replicas_status(c);
1318 return bch2_have_enough_devs(s, flags);
1321 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
1323 bch2_copygc_stop(ca);
1326 * The allocator thread itself allocates btree nodes, so stop it first:
1328 bch2_dev_allocator_stop(ca);
1329 bch2_dev_allocator_remove(c, ca);
1330 bch2_dev_journal_stop(&c->journal, ca);
1333 static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
1335 lockdep_assert_held(&c->state_lock);
1337 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_RW);
1339 bch2_dev_allocator_add(c, ca);
1340 bch2_recalc_capacity(c);
1342 if (bch2_dev_allocator_start(ca))
1343 return "error starting allocator thread";
1345 if (bch2_copygc_start(c, ca))
1346 return "error starting copygc thread";
1351 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1352 enum bch_member_state new_state, int flags)
1354 struct bch_sb_field_members *mi;
1357 if (ca->mi.state == new_state)
1360 if (!bch2_dev_state_allowed(c, ca, new_state, flags))
1363 if (new_state != BCH_MEMBER_STATE_RW)
1364 __bch2_dev_read_only(c, ca);
1366 bch_notice(ca, "%s", bch2_dev_state[new_state]);
1368 mutex_lock(&c->sb_lock);
1369 mi = bch2_sb_get_members(c->disk_sb.sb);
1370 SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
1371 bch2_write_super(c);
1372 mutex_unlock(&c->sb_lock);
1374 if (new_state == BCH_MEMBER_STATE_RW &&
1375 __bch2_dev_read_write(c, ca))
1378 rebalance_wakeup(c);
1383 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1384 enum bch_member_state new_state, int flags)
1388 mutex_lock(&c->state_lock);
1389 ret = __bch2_dev_set_state(c, ca, new_state, flags);
1390 mutex_unlock(&c->state_lock);
1395 /* Device add/removal: */
1397 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
1399 struct bch_sb_field_members *mi;
1400 unsigned dev_idx = ca->dev_idx, data;
1403 mutex_lock(&c->state_lock);
1405 percpu_ref_put(&ca->ref); /* XXX */
1407 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1408 bch_err(ca, "Cannot remove without losing data");
1412 __bch2_dev_read_only(c, ca);
1415 * XXX: verify that dev_idx is really not in use anymore, anywhere
1417 * flag_data_bad() does not check btree pointers
1419 ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
1421 bch_err(ca, "Remove failed: error %i dropping data", ret);
1425 ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx);
1427 bch_err(ca, "Remove failed: error %i flushing journal", ret);
1431 data = bch2_dev_has_data(c, ca);
1433 char data_has_str[100];
1434 bch2_scnprint_flag_list(data_has_str,
1435 sizeof(data_has_str),
1438 bch_err(ca, "Remove failed, still has data (%s)", data_has_str);
1443 ret = bch2_btree_delete_range(c, BTREE_ID_ALLOC,
1444 POS(ca->dev_idx, 0),
1445 POS(ca->dev_idx + 1, 0),
1449 bch_err(ca, "Remove failed, error deleting alloc info");
1454 * must flush all existing journal entries, they might have
1455 * (overwritten) keys that point to the device we're removing:
1457 ret = bch2_journal_flush_all_pins(&c->journal);
1459 bch_err(ca, "Remove failed, journal error");
1463 __bch2_dev_offline(c, ca);
1465 mutex_lock(&c->sb_lock);
1466 rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
1467 mutex_unlock(&c->sb_lock);
1469 percpu_ref_kill(&ca->ref);
1470 wait_for_completion(&ca->ref_completion);
1475 * Free this device's slot in the bch_member array - all pointers to
1476 * this device must be gone:
1478 mutex_lock(&c->sb_lock);
1479 mi = bch2_sb_get_members(c->disk_sb.sb);
1480 memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
1482 bch2_write_super(c);
1484 mutex_unlock(&c->sb_lock);
1485 mutex_unlock(&c->state_lock);
1488 if (ca->mi.state == BCH_MEMBER_STATE_RW)
1489 __bch2_dev_read_write(c, ca);
1490 mutex_unlock(&c->state_lock);
1494 /* Add new device to running filesystem: */
1495 int bch2_dev_add(struct bch_fs *c, const char *path)
1497 struct bch_opts opts = bch2_opts_empty();
1498 struct bch_sb_handle sb;
1500 struct bch_dev *ca = NULL;
1501 struct bch_sb_field_members *mi;
1502 struct bch_member dev_mi;
1503 unsigned dev_idx, nr_devices, u64s;
1506 ret = bch2_read_super(path, &opts, &sb);
1510 err = bch2_sb_validate(&sb);
1514 dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx];
1516 err = bch2_dev_may_add(sb.sb, c);
1520 ca = __bch2_dev_alloc(c, &dev_mi);
1522 bch2_free_super(&sb);
1526 ret = __bch2_dev_attach_bdev(ca, &sb);
1532 err = "journal alloc failed";
1533 ret = bch2_dev_journal_alloc(ca);
1537 mutex_lock(&c->state_lock);
1538 mutex_lock(&c->sb_lock);
1540 err = "insufficient space in new superblock";
1541 ret = bch2_sb_from_fs(c, ca);
1545 mi = bch2_sb_get_members(ca->disk_sb.sb);
1547 if (!bch2_sb_resize_members(&ca->disk_sb,
1548 le32_to_cpu(mi->field.u64s) +
1549 sizeof(dev_mi) / sizeof(u64))) {
1554 if (dynamic_fault("bcachefs:add:no_slot"))
1557 mi = bch2_sb_get_members(c->disk_sb.sb);
1558 for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
1559 if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx))
1562 err = "no slots available in superblock";
1567 nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
1568 u64s = (sizeof(struct bch_sb_field_members) +
1569 sizeof(struct bch_member) * nr_devices) / sizeof(u64);
1571 err = "no space in superblock for member info";
1574 mi = bch2_sb_resize_members(&c->disk_sb, u64s);
1580 mi->members[dev_idx] = dev_mi;
1581 mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_seconds());
1582 c->disk_sb.sb->nr_devices = nr_devices;
1584 ca->disk_sb.sb->dev_idx = dev_idx;
1585 bch2_dev_attach(c, ca, dev_idx);
1587 bch2_write_super(c);
1588 mutex_unlock(&c->sb_lock);
1590 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1591 err = __bch2_dev_read_write(c, ca);
1596 mutex_unlock(&c->state_lock);
1600 mutex_unlock(&c->sb_lock);
1601 mutex_unlock(&c->state_lock);
1605 bch2_free_super(&sb);
1606 bch_err(c, "Unable to add device: %s", err);
1609 bch_err(c, "Error going rw after adding device: %s", err);
1613 /* Hot add existing device to running filesystem: */
1614 int bch2_dev_online(struct bch_fs *c, const char *path)
1616 struct bch_opts opts = bch2_opts_empty();
1617 struct bch_sb_handle sb = { NULL };
1623 mutex_lock(&c->state_lock);
1625 ret = bch2_read_super(path, &opts, &sb);
1627 mutex_unlock(&c->state_lock);
1631 dev_idx = sb.sb->dev_idx;
1633 err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
1637 if (bch2_dev_attach_bdev(c, &sb)) {
1638 err = "bch2_dev_attach_bdev() error";
1642 ca = bch_dev_locked(c, dev_idx);
1643 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1644 err = __bch2_dev_read_write(c, ca);
1649 mutex_unlock(&c->state_lock);
1652 mutex_unlock(&c->state_lock);
1653 bch2_free_super(&sb);
1654 bch_err(c, "error bringing %s online: %s", path, err);
1658 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
1660 mutex_lock(&c->state_lock);
1662 if (!bch2_dev_is_online(ca)) {
1663 bch_err(ca, "Already offline");
1664 mutex_unlock(&c->state_lock);
1668 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1669 bch_err(ca, "Cannot offline required disk");
1670 mutex_unlock(&c->state_lock);
1674 __bch2_dev_offline(c, ca);
1676 mutex_unlock(&c->state_lock);
1680 int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
1682 struct bch_member *mi;
1685 mutex_lock(&c->state_lock);
1687 if (nbuckets < ca->mi.nbuckets) {
1688 bch_err(ca, "Cannot shrink yet");
1693 if (bch2_dev_is_online(ca) &&
1694 get_capacity(ca->disk_sb.bdev->bd_disk) <
1695 ca->mi.bucket_size * nbuckets) {
1696 bch_err(ca, "New size larger than device");
1701 ret = bch2_dev_buckets_resize(c, ca, nbuckets);
1703 bch_err(ca, "Resize error: %i", ret);
1707 mutex_lock(&c->sb_lock);
1708 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1709 mi->nbuckets = cpu_to_le64(nbuckets);
1711 bch2_write_super(c);
1712 mutex_unlock(&c->sb_lock);
1714 bch2_recalc_capacity(c);
1716 mutex_unlock(&c->state_lock);
1720 /* return with ref on ca->ref: */
1721 struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path)
1724 struct block_device *bdev = lookup_bdev(path);
1729 return ERR_CAST(bdev);
1731 for_each_member_device(ca, c, i)
1732 if (ca->disk_sb.bdev == bdev)
1735 ca = ERR_PTR(-ENOENT);
1741 /* Filesystem open: */
1743 struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
1744 struct bch_opts opts)
1746 struct bch_sb_handle *sb = NULL;
1747 struct bch_fs *c = NULL;
1748 unsigned i, best_sb = 0;
1752 pr_verbose_init(opts, "");
1755 c = ERR_PTR(-EINVAL);
1759 if (!try_module_get(THIS_MODULE)) {
1760 c = ERR_PTR(-ENODEV);
1764 sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
1768 for (i = 0; i < nr_devices; i++) {
1769 ret = bch2_read_super(devices[i], &opts, &sb[i]);
1773 err = bch2_sb_validate(&sb[i]);
1778 for (i = 1; i < nr_devices; i++)
1779 if (le64_to_cpu(sb[i].sb->seq) >
1780 le64_to_cpu(sb[best_sb].sb->seq))
1783 for (i = 0; i < nr_devices; i++) {
1784 err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
1790 c = bch2_fs_alloc(sb[best_sb].sb, opts);
1794 err = "bch2_dev_online() error";
1795 mutex_lock(&c->state_lock);
1796 for (i = 0; i < nr_devices; i++)
1797 if (bch2_dev_attach_bdev(c, &sb[i])) {
1798 mutex_unlock(&c->state_lock);
1801 mutex_unlock(&c->state_lock);
1803 err = "insufficient devices";
1804 if (!bch2_fs_may_start(c))
1807 if (!c->opts.nostart) {
1808 err = bch2_fs_start(c);
1814 module_put(THIS_MODULE);
1816 pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
1819 pr_err("bch_fs_open err opening %s: %s",
1825 for (i = 0; i < nr_devices; i++)
1826 bch2_free_super(&sb[i]);
1831 static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb,
1832 struct bch_opts opts)
1836 bool allocated_fs = false;
1838 err = bch2_sb_validate(sb);
1842 mutex_lock(&bch_fs_list_lock);
1843 c = __bch2_uuid_to_fs(sb->sb->uuid);
1845 closure_get(&c->cl);
1847 err = bch2_dev_in_fs(c->disk_sb.sb, sb->sb);
1851 c = bch2_fs_alloc(sb->sb, opts);
1852 err = "cannot allocate memory";
1856 allocated_fs = true;
1859 err = "bch2_dev_online() error";
1861 mutex_lock(&c->sb_lock);
1862 if (bch2_dev_attach_bdev(c, sb)) {
1863 mutex_unlock(&c->sb_lock);
1866 mutex_unlock(&c->sb_lock);
1868 if (!c->opts.nostart && bch2_fs_may_start(c)) {
1869 err = bch2_fs_start(c);
1874 closure_put(&c->cl);
1875 mutex_unlock(&bch_fs_list_lock);
1879 mutex_unlock(&bch_fs_list_lock);
1884 closure_put(&c->cl);
1889 const char *bch2_fs_open_incremental(const char *path)
1891 struct bch_sb_handle sb;
1892 struct bch_opts opts = bch2_opts_empty();
1895 if (bch2_read_super(path, &opts, &sb))
1896 return "error reading superblock";
1898 err = __bch2_fs_open_incremental(&sb, opts);
1899 bch2_free_super(&sb);
1904 /* Global interfaces/init */
1906 static void bcachefs_exit(void)
1910 bch2_chardev_exit();
1912 kset_unregister(bcachefs_kset);
1915 static int __init bcachefs_init(void)
1917 bch2_bkey_pack_test();
1918 bch2_inode_pack_test();
1920 if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
1921 bch2_chardev_init() ||
1932 #define BCH_DEBUG_PARAM(name, description) \
1934 module_param_named(name, bch2_##name, bool, 0644); \
1935 MODULE_PARM_DESC(name, description);
1937 #undef BCH_DEBUG_PARAM
1939 module_exit(bcachefs_exit);
1940 module_init(bcachefs_init);