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"
37 #include <linux/backing-dev.h>
38 #include <linux/blkdev.h>
39 #include <linux/debugfs.h>
40 #include <linux/device.h>
41 #include <linux/genhd.h>
42 #include <linux/idr.h>
43 #include <linux/kthread.h>
44 #include <linux/module.h>
45 #include <linux/percpu.h>
46 #include <linux/random.h>
47 #include <linux/sysfs.h>
48 #include <crypto/hash.h>
50 #include <trace/events/bcachefs.h>
52 MODULE_LICENSE("GPL");
53 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
56 struct kobj_type type ## _ktype = { \
57 .release = type ## _release, \
58 .sysfs_ops = &type ## _sysfs_ops, \
59 .default_attrs = type ## _files \
62 static void bch2_fs_release(struct kobject *);
63 static void bch2_dev_release(struct kobject *);
65 static void bch2_fs_internal_release(struct kobject *k)
69 static void bch2_fs_opts_dir_release(struct kobject *k)
73 static void bch2_fs_time_stats_release(struct kobject *k)
77 static KTYPE(bch2_fs);
78 static KTYPE(bch2_fs_internal);
79 static KTYPE(bch2_fs_opts_dir);
80 static KTYPE(bch2_fs_time_stats);
81 static KTYPE(bch2_dev);
83 static struct kset *bcachefs_kset;
84 static LIST_HEAD(bch_fs_list);
85 static DEFINE_MUTEX(bch_fs_list_lock);
87 static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
89 static void bch2_dev_free(struct bch_dev *);
90 static int bch2_dev_alloc(struct bch_fs *, unsigned);
91 static int bch2_dev_sysfs_online(struct bch_dev *);
92 static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
94 struct bch_fs *bch2_bdev_to_fs(struct block_device *bdev)
100 mutex_lock(&bch_fs_list_lock);
103 list_for_each_entry(c, &bch_fs_list, list)
104 for_each_member_device_rcu(ca, c, i, NULL)
105 if (ca->disk_sb.bdev == bdev) {
112 mutex_unlock(&bch_fs_list_lock);
117 static struct bch_fs *__bch2_uuid_to_fs(uuid_le uuid)
121 lockdep_assert_held(&bch_fs_list_lock);
123 list_for_each_entry(c, &bch_fs_list, list)
124 if (!memcmp(&c->disk_sb->uuid, &uuid, sizeof(uuid_le)))
130 struct bch_fs *bch2_uuid_to_fs(uuid_le uuid)
134 mutex_lock(&bch_fs_list_lock);
135 c = __bch2_uuid_to_fs(uuid);
138 mutex_unlock(&bch_fs_list_lock);
143 int bch2_congested(void *data, int bdi_bits)
145 struct bch_fs *c = data;
146 struct backing_dev_info *bdi;
151 if (bdi_bits & (1 << WB_sync_congested)) {
152 /* Reads - check all devices: */
153 for_each_readable_member(ca, c, i) {
154 bdi = ca->disk_sb.bdev->bd_bdi;
156 if (bdi_congested(bdi, bdi_bits)) {
162 /* Writes prefer fastest tier: */
163 struct bch_tier *tier = READ_ONCE(c->fastest_tier);
164 struct bch_devs_mask *devs =
165 tier ? &tier->devs : &c->rw_devs[BCH_DATA_USER];
168 for_each_member_device_rcu(ca, c, i, devs) {
169 bdi = ca->disk_sb.bdev->bd_bdi;
171 if (bdi_congested(bdi, bdi_bits)) {
182 /* Filesystem RO/RW: */
185 * For startup/shutdown of RW stuff, the dependencies are:
187 * - foreground writes depend on copygc and tiering (to free up space)
189 * - copygc and tiering depend on mark and sweep gc (they actually probably
190 * don't because they either reserve ahead of time or don't block if
191 * allocations fail, but allocations can require mark and sweep gc to run
192 * because of generation number wraparound)
194 * - all of the above depends on the allocator threads
196 * - allocator depends on the journal (when it rewrites prios and gens)
199 static void __bch2_fs_read_only(struct bch_fs *c)
204 bch2_tiering_stop(c);
206 for_each_member_device(ca, c, i)
207 bch2_copygc_stop(ca);
209 bch2_gc_thread_stop(c);
212 * Flush journal before stopping allocators, because flushing journal
213 * blacklist entries involves allocating new btree nodes:
215 bch2_journal_flush_all_pins(&c->journal);
217 if (!bch2_journal_error(&c->journal))
218 bch2_btree_verify_flushed(c);
220 for_each_member_device(ca, c, i)
221 bch2_dev_allocator_stop(ca);
223 bch2_fs_journal_stop(&c->journal);
225 for_each_member_device(ca, c, i)
226 bch2_dev_allocator_remove(c, ca);
229 static void bch2_writes_disabled(struct percpu_ref *writes)
231 struct bch_fs *c = container_of(writes, struct bch_fs, writes);
233 set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
234 wake_up(&bch_read_only_wait);
237 void bch2_fs_read_only(struct bch_fs *c)
239 if (c->state != BCH_FS_STARTING &&
240 c->state != BCH_FS_RW)
243 if (test_bit(BCH_FS_ERROR, &c->flags))
247 * Block new foreground-end write operations from starting - any new
248 * writes will return -EROFS:
250 * (This is really blocking new _allocations_, writes to previously
251 * allocated space can still happen until stopping the allocator in
252 * bch2_dev_allocator_stop()).
254 percpu_ref_kill(&c->writes);
256 cancel_delayed_work(&c->pd_controllers_update);
259 * If we're not doing an emergency shutdown, we want to wait on
260 * outstanding writes to complete so they don't see spurious errors due
261 * to shutting down the allocator:
263 * If we are doing an emergency shutdown outstanding writes may
264 * hang until we shutdown the allocator so we don't want to wait
265 * on outstanding writes before shutting everything down - but
266 * we do need to wait on them before returning and signalling
267 * that going RO is complete:
269 wait_event(bch_read_only_wait,
270 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
271 test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
273 __bch2_fs_read_only(c);
275 wait_event(bch_read_only_wait,
276 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
278 clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
280 if (!bch2_journal_error(&c->journal) &&
281 !test_bit(BCH_FS_ERROR, &c->flags)) {
282 mutex_lock(&c->sb_lock);
283 SET_BCH_SB_CLEAN(c->disk_sb, true);
285 mutex_unlock(&c->sb_lock);
288 c->state = BCH_FS_RO;
291 static void bch2_fs_read_only_work(struct work_struct *work)
294 container_of(work, struct bch_fs, read_only_work);
296 mutex_lock(&c->state_lock);
297 bch2_fs_read_only(c);
298 mutex_unlock(&c->state_lock);
301 static void bch2_fs_read_only_async(struct bch_fs *c)
303 queue_work(system_long_wq, &c->read_only_work);
306 bool bch2_fs_emergency_read_only(struct bch_fs *c)
308 bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
310 bch2_fs_read_only_async(c);
311 bch2_journal_halt(&c->journal);
313 wake_up(&bch_read_only_wait);
317 const char *bch2_fs_read_write(struct bch_fs *c)
320 const char *err = NULL;
323 if (c->state != BCH_FS_STARTING &&
324 c->state != BCH_FS_RO)
327 for_each_rw_member(ca, c, i)
328 bch2_dev_allocator_add(c, ca);
329 bch2_recalc_capacity(c);
331 err = "error starting allocator thread";
332 for_each_rw_member(ca, c, i)
333 if (bch2_dev_allocator_start(ca)) {
334 percpu_ref_put(&ca->io_ref);
338 err = "error starting btree GC thread";
339 if (bch2_gc_thread_start(c))
342 err = "error starting copygc thread";
343 for_each_rw_member(ca, c, i)
344 if (bch2_copygc_start(c, ca)) {
345 percpu_ref_put(&ca->io_ref);
349 err = "error starting tiering thread";
350 if (bch2_tiering_start(c))
353 schedule_delayed_work(&c->pd_controllers_update, 5 * HZ);
355 if (c->state != BCH_FS_STARTING)
356 percpu_ref_reinit(&c->writes);
358 c->state = BCH_FS_RW;
361 __bch2_fs_read_only(c);
365 /* Filesystem startup/shutdown: */
367 static void bch2_fs_free(struct bch_fs *c)
369 bch2_fs_fsio_exit(c);
370 bch2_fs_encryption_exit(c);
371 bch2_fs_btree_cache_exit(c);
372 bch2_fs_journal_exit(&c->journal);
373 bch2_io_clock_exit(&c->io_clock[WRITE]);
374 bch2_io_clock_exit(&c->io_clock[READ]);
375 bch2_fs_compress_exit(c);
376 lg_lock_free(&c->usage_lock);
377 free_percpu(c->usage_percpu);
378 mempool_exit(&c->btree_bounce_pool);
379 mempool_exit(&c->bio_bounce_pages);
380 bioset_exit(&c->bio_write);
381 bioset_exit(&c->bio_read_split);
382 bioset_exit(&c->bio_read);
383 bioset_exit(&c->btree_read_bio);
384 mempool_exit(&c->btree_interior_update_pool);
385 mempool_exit(&c->btree_reserve_pool);
386 mempool_exit(&c->fill_iter);
387 percpu_ref_exit(&c->writes);
388 kfree(rcu_dereference_protected(c->replicas, 1));
391 destroy_workqueue(c->copygc_wq);
393 destroy_workqueue(c->wq);
395 free_pages((unsigned long) c->disk_sb, c->disk_sb_order);
397 module_put(THIS_MODULE);
400 static void bch2_fs_exit(struct bch_fs *c)
404 cancel_delayed_work_sync(&c->pd_controllers_update);
405 cancel_work_sync(&c->read_only_work);
407 for (i = 0; i < c->sb.nr_devices; i++)
409 bch2_dev_free(rcu_dereference_protected(c->devs[i], 1));
411 closure_debug_destroy(&c->cl);
412 kobject_put(&c->kobj);
415 static void bch2_fs_offline(struct bch_fs *c)
420 mutex_lock(&bch_fs_list_lock);
422 mutex_unlock(&bch_fs_list_lock);
424 for_each_member_device(ca, c, i)
425 if (ca->kobj.state_in_sysfs &&
427 sysfs_remove_link(&part_to_dev(ca->disk_sb.bdev->bd_part)->kobj,
430 if (c->kobj.state_in_sysfs)
431 kobject_del(&c->kobj);
433 bch2_fs_debug_exit(c);
434 bch2_fs_chardev_exit(c);
436 kobject_put(&c->time_stats);
437 kobject_put(&c->opts_dir);
438 kobject_put(&c->internal);
440 mutex_lock(&c->state_lock);
441 __bch2_fs_read_only(c);
442 mutex_unlock(&c->state_lock);
445 static void bch2_fs_release(struct kobject *kobj)
447 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
452 void bch2_fs_stop(struct bch_fs *c)
454 mutex_lock(&c->state_lock);
455 BUG_ON(c->state == BCH_FS_STOPPING);
456 c->state = BCH_FS_STOPPING;
457 mutex_unlock(&c->state_lock);
461 closure_sync(&c->cl);
466 static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
468 struct bch_sb_field_members *mi;
470 unsigned i, iter_size;
472 c = kzalloc(sizeof(struct bch_fs), GFP_KERNEL);
476 __module_get(THIS_MODULE);
480 mutex_init(&c->state_lock);
481 mutex_init(&c->sb_lock);
482 mutex_init(&c->replicas_gc_lock);
483 mutex_init(&c->bucket_lock);
484 mutex_init(&c->btree_root_lock);
485 INIT_WORK(&c->read_only_work, bch2_fs_read_only_work);
487 init_rwsem(&c->gc_lock);
489 #define BCH_TIME_STAT(name, frequency_units, duration_units) \
490 spin_lock_init(&c->name##_time.lock);
494 bch2_fs_allocator_init(c);
495 bch2_fs_tiering_init(c);
497 INIT_LIST_HEAD(&c->list);
499 INIT_LIST_HEAD(&c->btree_interior_update_list);
500 mutex_init(&c->btree_reserve_cache_lock);
501 mutex_init(&c->btree_interior_update_lock);
503 mutex_init(&c->bio_bounce_pages_lock);
504 mutex_init(&c->zlib_workspace_lock);
506 bio_list_init(&c->btree_write_error_list);
507 spin_lock_init(&c->btree_write_error_lock);
508 INIT_WORK(&c->btree_write_error_work, bch2_btree_write_error_work);
510 INIT_LIST_HEAD(&c->fsck_errors);
511 mutex_init(&c->fsck_error_lock);
513 seqcount_init(&c->gc_pos_lock);
515 c->prio_clock[READ].hand = 1;
516 c->prio_clock[READ].min_prio = 0;
517 c->prio_clock[WRITE].hand = 1;
518 c->prio_clock[WRITE].min_prio = 0;
520 init_waitqueue_head(&c->writeback_wait);
521 c->writeback_pages_max = (256 << 10) / PAGE_SIZE;
523 c->copy_gc_enabled = 1;
524 c->tiering_enabled = 1;
525 c->tiering_percent = 10;
527 c->journal.write_time = &c->journal_write_time;
528 c->journal.delay_time = &c->journal_delay_time;
529 c->journal.blocked_time = &c->journal_blocked_time;
530 c->journal.flush_seq_time = &c->journal_flush_seq_time;
532 bch2_fs_btree_cache_init_early(&c->btree_cache);
534 mutex_lock(&c->sb_lock);
536 if (bch2_sb_to_fs(c, sb)) {
537 mutex_unlock(&c->sb_lock);
541 mutex_unlock(&c->sb_lock);
543 scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid);
545 c->opts = bch2_opts_default;
546 bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb));
547 bch2_opts_apply(&c->opts, opts);
549 c->block_bits = ilog2(c->opts.block_size);
551 c->opts.nochanges |= c->opts.noreplay;
552 c->opts.read_only |= c->opts.nochanges;
554 if (bch2_fs_init_fault("fs_alloc"))
557 iter_size = (btree_blocks(c) + 1) * 2 *
558 sizeof(struct btree_node_iter_set);
560 if (!(c->wq = alloc_workqueue("bcachefs",
561 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
562 !(c->copygc_wq = alloc_workqueue("bcache_copygc",
563 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
564 percpu_ref_init(&c->writes, bch2_writes_disabled, 0, GFP_KERNEL) ||
565 mempool_init_kmalloc_pool(&c->btree_reserve_pool, 1,
566 sizeof(struct btree_reserve)) ||
567 mempool_init_kmalloc_pool(&c->btree_interior_update_pool, 1,
568 sizeof(struct btree_update)) ||
569 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
570 bioset_init(&c->btree_read_bio, 1,
571 offsetof(struct btree_read_bio, bio),
572 BIOSET_NEED_BVECS) ||
573 bioset_init(&c->bio_read, 1, offsetof(struct bch_read_bio, bio),
574 BIOSET_NEED_BVECS) ||
575 bioset_init(&c->bio_read_split, 1, offsetof(struct bch_read_bio, bio),
576 BIOSET_NEED_BVECS) ||
577 bioset_init(&c->bio_write, 1, offsetof(struct bch_write_bio, bio),
578 BIOSET_NEED_BVECS) ||
579 mempool_init_page_pool(&c->bio_bounce_pages,
581 c->opts.btree_node_size,
582 c->sb.encoded_extent_max) /
584 !(c->usage_percpu = alloc_percpu(struct bch_fs_usage)) ||
585 lg_lock_init(&c->usage_lock) ||
586 mempool_init_vp_pool(&c->btree_bounce_pool, 1, btree_bytes(c)) ||
587 bch2_io_clock_init(&c->io_clock[READ]) ||
588 bch2_io_clock_init(&c->io_clock[WRITE]) ||
589 bch2_fs_journal_init(&c->journal) ||
590 bch2_fs_btree_cache_init(c) ||
591 bch2_fs_encryption_init(c) ||
592 bch2_fs_compress_init(c) ||
593 bch2_check_set_has_compressed_data(c, c->opts.compression) ||
594 bch2_fs_fsio_init(c))
597 mi = bch2_sb_get_members(c->disk_sb);
598 for (i = 0; i < c->sb.nr_devices; i++)
599 if (bch2_dev_exists(c->disk_sb, mi, i) &&
600 bch2_dev_alloc(c, i))
604 * Now that all allocations have succeeded, init various refcounty
605 * things that let us shutdown:
607 closure_init(&c->cl, NULL);
609 c->kobj.kset = bcachefs_kset;
610 kobject_init(&c->kobj, &bch2_fs_ktype);
611 kobject_init(&c->internal, &bch2_fs_internal_ktype);
612 kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype);
613 kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype);
620 static const char *__bch2_fs_online(struct bch_fs *c)
623 const char *err = NULL;
627 lockdep_assert_held(&bch_fs_list_lock);
629 if (!list_empty(&c->list))
632 if (__bch2_uuid_to_fs(c->sb.uuid))
633 return "filesystem UUID already open";
635 ret = bch2_fs_chardev_init(c);
637 return "error creating character device";
639 bch2_fs_debug_init(c);
641 if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ||
642 kobject_add(&c->internal, &c->kobj, "internal") ||
643 kobject_add(&c->opts_dir, &c->kobj, "options") ||
644 kobject_add(&c->time_stats, &c->kobj, "time_stats") ||
645 bch2_opts_create_sysfs_files(&c->opts_dir))
646 return "error creating sysfs objects";
648 mutex_lock(&c->state_lock);
650 err = "error creating sysfs objects";
651 __for_each_member_device(ca, c, i, NULL)
652 if (bch2_dev_sysfs_online(ca))
655 list_add(&c->list, &bch_fs_list);
658 mutex_unlock(&c->state_lock);
662 static const char *bch2_fs_online(struct bch_fs *c)
666 mutex_lock(&bch_fs_list_lock);
667 err = __bch2_fs_online(c);
668 mutex_unlock(&bch_fs_list_lock);
673 static const char *__bch2_fs_start(struct bch_fs *c)
675 const char *err = "cannot allocate memory";
676 struct bch_sb_field_members *mi;
685 closure_init_stack(&cl);
687 mutex_lock(&c->state_lock);
689 BUG_ON(c->state != BCH_FS_STARTING);
691 mutex_lock(&c->sb_lock);
692 for_each_online_member(ca, c, i)
693 bch2_sb_from_fs(c, ca);
694 mutex_unlock(&c->sb_lock);
696 for_each_rw_member(ca, c, i)
697 bch2_dev_allocator_add(c, ca);
698 bch2_recalc_capacity(c);
700 if (BCH_SB_INITIALIZED(c->disk_sb)) {
701 ret = bch2_journal_read(c, &journal);
705 j = &list_entry(journal.prev, struct journal_replay, list)->j;
707 c->prio_clock[READ].hand = le16_to_cpu(j->read_clock);
708 c->prio_clock[WRITE].hand = le16_to_cpu(j->write_clock);
710 for (i = 0; i < BTREE_ID_NR; i++) {
714 err = "missing btree root";
715 k = bch2_journal_find_btree_root(c, j, i, &level);
716 if (!k && i < BTREE_ID_ALLOC)
722 err = "error reading btree root";
723 if (bch2_btree_root_read(c, i, k, level)) {
724 if (i != BTREE_ID_ALLOC)
727 mustfix_fsck_err(c, "error reading btree root");
731 err = "error reading allocation information";
732 ret = bch2_alloc_read(c, &journal);
736 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
738 bch_verbose(c, "starting mark and sweep:");
739 err = "error in recovery";
740 ret = bch2_initial_gc(c, &journal);
743 bch_verbose(c, "mark and sweep done");
745 if (c->opts.noreplay)
748 err = "cannot allocate new btree root";
749 for (i = 0; i < BTREE_ID_NR; i++)
750 if (!c->btree_roots[i].b &&
751 bch2_btree_root_alloc(c, i, &cl))
757 * bch2_journal_start() can't happen sooner, or btree_gc_finish()
758 * will give spurious errors about oldest_gen > bucket_gen -
759 * this is a hack but oh well.
761 bch2_journal_start(c);
763 err = "error starting allocator thread";
764 for_each_rw_member(ca, c, i)
765 if (bch2_dev_allocator_start(ca)) {
766 percpu_ref_put(&ca->io_ref);
770 bch_verbose(c, "starting journal replay:");
771 err = "journal replay failed";
772 ret = bch2_journal_replay(c, &journal);
775 bch_verbose(c, "journal replay done");
777 if (c->opts.norecovery)
780 bch_verbose(c, "starting fsck:");
781 err = "error in fsck";
782 ret = bch2_fsck(c, !c->opts.nofsck);
785 bch_verbose(c, "fsck done");
787 struct bch_inode_unpacked inode;
788 struct bkey_inode_buf packed_inode;
790 bch_notice(c, "initializing new filesystem");
792 set_bit(BCH_FS_ALLOC_READ_DONE, &c->flags);
794 ret = bch2_initial_gc(c, &journal);
798 err = "unable to allocate journal buckets";
799 for_each_rw_member(ca, c, i)
800 if (bch2_dev_journal_alloc(ca)) {
801 percpu_ref_put(&ca->io_ref);
805 err = "cannot allocate new btree root";
806 for (i = 0; i < BTREE_ID_NR; i++)
807 if (bch2_btree_root_alloc(c, i, &cl))
811 * journal_res_get() will crash if called before this has
812 * set up the journal.pin FIFO and journal.cur pointer:
814 bch2_journal_start(c);
815 bch2_journal_set_replay_done(&c->journal);
817 err = "error starting allocator thread";
818 for_each_rw_member(ca, c, i)
819 if (bch2_dev_allocator_start(ca)) {
820 percpu_ref_put(&ca->io_ref);
824 /* Wait for new btree roots to be written: */
827 bch2_inode_init(c, &inode, 0, 0,
828 S_IFDIR|S_IRWXU|S_IRUGO|S_IXUGO, 0, NULL);
829 inode.bi_inum = BCACHEFS_ROOT_INO;
831 bch2_inode_pack(&packed_inode, &inode);
833 err = "error creating root directory";
834 if (bch2_btree_insert(c, BTREE_ID_INODES,
835 &packed_inode.inode.k_i,
836 NULL, NULL, NULL, 0))
839 err = "error writing first journal entry";
840 if (bch2_journal_meta(&c->journal))
844 err = "dynamic fault";
845 if (bch2_fs_init_fault("fs_start"))
848 if (c->opts.read_only) {
849 bch2_fs_read_only(c);
851 err = bch2_fs_read_write(c);
856 mutex_lock(&c->sb_lock);
857 mi = bch2_sb_get_members(c->disk_sb);
858 now = ktime_get_seconds();
860 for_each_member_device(ca, c, i)
861 mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
863 SET_BCH_SB_INITIALIZED(c->disk_sb, true);
864 SET_BCH_SB_CLEAN(c->disk_sb, false);
867 mutex_unlock(&c->sb_lock);
871 mutex_unlock(&c->state_lock);
872 bch2_journal_entries_free(&journal);
879 case BCH_FSCK_ERRORS_NOT_FIXED:
880 bch_err(c, "filesystem contains errors: please report this to the developers");
881 pr_cont("mount with -o fix_errors to repair\n");
884 case BCH_FSCK_REPAIR_UNIMPLEMENTED:
885 bch_err(c, "filesystem contains errors: please report this to the developers");
886 pr_cont("repair unimplemented: inform the developers so that it can be added\n");
889 case BCH_FSCK_REPAIR_IMPOSSIBLE:
890 bch_err(c, "filesystem contains errors, but repair impossible");
893 case BCH_FSCK_UNKNOWN_VERSION:
894 err = "unknown metadata version";;
897 err = "cannot allocate memory";
905 set_bit(BCH_FS_ERROR, &c->flags);
909 const char *bch2_fs_start(struct bch_fs *c)
911 return __bch2_fs_start(c) ?: bch2_fs_online(c);
914 static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
916 struct bch_sb_field_members *sb_mi;
918 sb_mi = bch2_sb_get_members(sb);
920 return "Invalid superblock: member info area missing";
922 if (le16_to_cpu(sb->block_size) != c->opts.block_size)
923 return "mismatched block size";
925 if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
926 BCH_SB_BTREE_NODE_SIZE(c->disk_sb))
927 return "new cache bucket size is too small";
932 static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
934 struct bch_sb *newest =
935 le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
936 struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
938 if (uuid_le_cmp(fs->uuid, sb->uuid))
939 return "device not a member of filesystem";
941 if (!bch2_dev_exists(newest, mi, sb->dev_idx))
942 return "device has been removed";
944 if (fs->block_size != sb->block_size)
945 return "mismatched block size";
950 /* Device startup/shutdown: */
952 static void bch2_dev_release(struct kobject *kobj)
954 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
959 static void bch2_dev_free(struct bch_dev *ca)
963 cancel_work_sync(&ca->io_error_work);
965 if (ca->kobj.state_in_sysfs &&
967 sysfs_remove_link(&part_to_dev(ca->disk_sb.bdev->bd_part)->kobj,
970 if (ca->kobj.state_in_sysfs)
971 kobject_del(&ca->kobj);
973 bch2_free_super(&ca->disk_sb);
974 bch2_dev_journal_exit(ca);
976 free_percpu(ca->io_done);
977 bioset_exit(&ca->replica_set);
978 free_percpu(ca->usage_percpu);
979 kvpfree(ca->bucket_dirty, BITS_TO_LONGS(ca->mi.nbuckets) * sizeof(unsigned long));
980 kvpfree(ca->buckets, ca->mi.nbuckets * sizeof(struct bucket));
981 kvpfree(ca->oldest_gens, ca->mi.nbuckets * sizeof(u8));
982 free_heap(&ca->copygc_heap);
983 free_heap(&ca->alloc_heap);
984 free_fifo(&ca->free_inc);
986 for (i = 0; i < RESERVE_NR; i++)
987 free_fifo(&ca->free[i]);
989 percpu_ref_exit(&ca->io_ref);
990 percpu_ref_exit(&ca->ref);
991 kobject_put(&ca->kobj);
994 static void __bch2_dev_offline(struct bch_dev *ca)
996 struct bch_fs *c = ca->fs;
998 lockdep_assert_held(&c->state_lock);
1000 if (percpu_ref_is_zero(&ca->io_ref))
1003 __bch2_dev_read_only(c, ca);
1005 reinit_completion(&ca->io_ref_completion);
1006 percpu_ref_kill(&ca->io_ref);
1007 wait_for_completion(&ca->io_ref_completion);
1009 if (ca->kobj.state_in_sysfs) {
1010 struct kobject *block =
1011 &part_to_dev(ca->disk_sb.bdev->bd_part)->kobj;
1013 sysfs_remove_link(block, "bcachefs");
1014 sysfs_remove_link(&ca->kobj, "block");
1017 bch2_free_super(&ca->disk_sb);
1018 bch2_dev_journal_exit(ca);
1021 static void bch2_dev_ref_complete(struct percpu_ref *ref)
1023 struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
1025 complete(&ca->ref_completion);
1028 static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
1030 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
1032 complete(&ca->io_ref_completion);
1035 static int bch2_dev_sysfs_online(struct bch_dev *ca)
1037 struct bch_fs *c = ca->fs;
1040 if (!c->kobj.state_in_sysfs)
1043 if (!ca->kobj.state_in_sysfs) {
1044 ret = kobject_add(&ca->kobj, &c->kobj,
1045 "dev-%u", ca->dev_idx);
1050 if (ca->disk_sb.bdev) {
1051 struct kobject *block =
1052 &part_to_dev(ca->disk_sb.bdev->bd_part)->kobj;
1054 ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
1057 ret = sysfs_create_link(&ca->kobj, block, "block");
1065 static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
1067 struct bch_member *member;
1068 size_t reserve_none, movinggc_reserve, free_inc_reserve, total_reserve;
1070 unsigned i, btree_node_reserve_buckets;
1073 if (bch2_fs_init_fault("dev_alloc"))
1076 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1080 kobject_init(&ca->kobj, &bch2_dev_ktype);
1081 init_completion(&ca->ref_completion);
1082 init_completion(&ca->io_ref_completion);
1084 ca->dev_idx = dev_idx;
1085 __set_bit(ca->dev_idx, ca->self.d);
1087 writepoint_init(&ca->copygc_write_point, BCH_DATA_USER);
1089 spin_lock_init(&ca->freelist_lock);
1090 bch2_dev_copygc_init(ca);
1092 INIT_WORK(&ca->io_error_work, bch2_io_error_work);
1094 if (bch2_fs_init_fault("dev_alloc"))
1097 member = bch2_sb_get_members(c->disk_sb)->members + dev_idx;
1099 ca->mi = bch2_mi_to_cpu(member);
1100 ca->uuid = member->uuid;
1101 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
1103 /* XXX: tune these */
1104 movinggc_reserve = max_t(size_t, 16, ca->mi.nbuckets >> 7);
1105 reserve_none = max_t(size_t, 4, ca->mi.nbuckets >> 9);
1107 * free_inc must be smaller than the copygc reserve: if it was bigger,
1108 * one copygc iteration might not make enough buckets available to fill
1109 * up free_inc and allow the allocator to make forward progress
1111 free_inc_reserve = movinggc_reserve / 2;
1112 heap_size = movinggc_reserve * 8;
1114 btree_node_reserve_buckets =
1115 DIV_ROUND_UP(BTREE_NODE_RESERVE,
1116 ca->mi.bucket_size / c->opts.btree_node_size);
1118 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
1120 percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
1121 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
1122 !init_fifo(&ca->free[RESERVE_BTREE], btree_node_reserve_buckets,
1124 !init_fifo(&ca->free[RESERVE_MOVINGGC],
1125 movinggc_reserve, GFP_KERNEL) ||
1126 !init_fifo(&ca->free[RESERVE_NONE], reserve_none, GFP_KERNEL) ||
1127 !init_fifo(&ca->free_inc, free_inc_reserve, GFP_KERNEL) ||
1128 !init_heap(&ca->alloc_heap, free_inc_reserve, GFP_KERNEL) ||
1129 !init_heap(&ca->copygc_heap,heap_size, GFP_KERNEL) ||
1130 !(ca->oldest_gens = kvpmalloc(ca->mi.nbuckets *
1132 GFP_KERNEL|__GFP_ZERO)) ||
1133 !(ca->buckets = kvpmalloc(ca->mi.nbuckets *
1134 sizeof(struct bucket),
1135 GFP_KERNEL|__GFP_ZERO)) ||
1136 !(ca->bucket_dirty = kvpmalloc(BITS_TO_LONGS(ca->mi.nbuckets) *
1137 sizeof(unsigned long),
1138 GFP_KERNEL|__GFP_ZERO)) ||
1139 !(ca->usage_percpu = alloc_percpu(struct bch_dev_usage)) ||
1140 bioset_init(&ca->replica_set, 4,
1141 offsetof(struct bch_write_bio, bio), 0) ||
1142 !(ca->io_done = alloc_percpu(*ca->io_done)))
1145 total_reserve = ca->free_inc.size;
1146 for (i = 0; i < RESERVE_NR; i++)
1147 total_reserve += ca->free[i].size;
1150 rcu_assign_pointer(c->devs[ca->dev_idx], ca);
1152 if (bch2_dev_sysfs_online(ca))
1153 pr_warn("error creating sysfs objects");
1161 static int __bch2_dev_online(struct bch_fs *c, struct bch_sb_handle *sb)
1166 if (le64_to_cpu(sb->sb->seq) >
1167 le64_to_cpu(c->disk_sb->seq))
1168 bch2_sb_to_fs(c, sb->sb);
1170 BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
1171 !c->devs[sb->sb->dev_idx]);
1173 ca = bch_dev_locked(c, sb->sb->dev_idx);
1174 if (ca->disk_sb.bdev) {
1175 bch_err(c, "already have device online in slot %u",
1180 BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
1182 ret = bch2_dev_journal_init(ca, sb->sb);
1187 * Increase journal write timeout if flushes to this device are
1190 if (!blk_queue_nonrot(bdev_get_queue(sb->bdev)) &&
1191 journal_flushes_device(ca))
1192 c->journal.write_delay_ms =
1193 max(c->journal.write_delay_ms, 1000U);
1197 if (sb->mode & FMODE_EXCL)
1198 ca->disk_sb.bdev->bd_holder = ca;
1199 memset(sb, 0, sizeof(*sb));
1201 if (c->sb.nr_devices == 1)
1202 bdevname(ca->disk_sb.bdev, c->name);
1203 bdevname(ca->disk_sb.bdev, ca->name);
1205 if (bch2_dev_sysfs_online(ca))
1206 pr_warn("error creating sysfs objects");
1208 bch2_mark_dev_superblock(c, ca, BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE);
1210 if (ca->mi.state == BCH_MEMBER_STATE_RW)
1211 bch2_dev_allocator_add(c, ca);
1213 percpu_ref_reinit(&ca->io_ref);
1217 /* Device management: */
1220 * Note: this function is also used by the error paths - when a particular
1221 * device sees an error, we call it to determine whether we can just set the
1222 * device RO, or - if this function returns false - we'll set the whole
1225 * XXX: maybe we should be more explicit about whether we're changing state
1226 * because we got an error or what have you?
1228 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
1229 enum bch_member_state new_state, int flags)
1231 struct bch_devs_mask new_online_devs;
1232 struct replicas_status s;
1233 struct bch_dev *ca2;
1234 int i, nr_rw = 0, required;
1236 lockdep_assert_held(&c->state_lock);
1238 switch (new_state) {
1239 case BCH_MEMBER_STATE_RW:
1241 case BCH_MEMBER_STATE_RO:
1242 if (ca->mi.state != BCH_MEMBER_STATE_RW)
1245 /* do we have enough devices to write to? */
1246 for_each_member_device(ca2, c, i)
1247 nr_rw += ca2->mi.state == BCH_MEMBER_STATE_RW;
1249 required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
1250 ? c->opts.metadata_replicas
1251 : c->opts.metadata_replicas_required,
1252 !(flags & BCH_FORCE_IF_DATA_DEGRADED)
1253 ? c->opts.data_replicas
1254 : c->opts.data_replicas_required);
1256 return nr_rw - 1 <= required;
1257 case BCH_MEMBER_STATE_FAILED:
1258 case BCH_MEMBER_STATE_SPARE:
1259 if (ca->mi.state != BCH_MEMBER_STATE_RW &&
1260 ca->mi.state != BCH_MEMBER_STATE_RO)
1263 /* do we have enough devices to read from? */
1264 new_online_devs = bch2_online_devs(c);
1265 __clear_bit(ca->dev_idx, new_online_devs.d);
1267 s = __bch2_replicas_status(c, new_online_devs);
1269 return bch2_have_enough_devs(c, s, flags);
1275 static bool bch2_fs_may_start(struct bch_fs *c)
1277 struct replicas_status s;
1278 struct bch_sb_field_members *mi;
1280 unsigned i, flags = c->opts.degraded
1281 ? BCH_FORCE_IF_DEGRADED
1284 if (!c->opts.degraded) {
1285 mutex_lock(&c->sb_lock);
1286 mi = bch2_sb_get_members(c->disk_sb);
1288 for (i = 0; i < c->disk_sb->nr_devices; i++) {
1289 if (!bch2_dev_exists(c->disk_sb, mi, i))
1292 ca = bch_dev_locked(c, i);
1294 if (!bch2_dev_is_online(ca) &&
1295 (ca->mi.state == BCH_MEMBER_STATE_RW ||
1296 ca->mi.state == BCH_MEMBER_STATE_RO)) {
1297 mutex_unlock(&c->sb_lock);
1301 mutex_unlock(&c->sb_lock);
1304 s = bch2_replicas_status(c);
1306 return bch2_have_enough_devs(c, s, flags);
1309 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
1311 bch2_copygc_stop(ca);
1314 * This stops new data writes (e.g. to existing open data
1315 * buckets) and then waits for all existing writes to
1318 bch2_dev_allocator_stop(ca);
1319 bch2_dev_allocator_remove(c, ca);
1322 static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
1324 lockdep_assert_held(&c->state_lock);
1326 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_RW);
1328 bch2_dev_allocator_add(c, ca);
1329 bch2_recalc_capacity(c);
1331 if (bch2_dev_allocator_start(ca))
1332 return "error starting allocator thread";
1334 if (bch2_copygc_start(c, ca))
1335 return "error starting copygc thread";
1337 if (bch2_tiering_start(c))
1338 return "error starting tiering thread";
1343 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1344 enum bch_member_state new_state, int flags)
1346 struct bch_sb_field_members *mi;
1348 if (ca->mi.state == new_state)
1351 if (!bch2_dev_state_allowed(c, ca, new_state, flags))
1354 if (new_state == BCH_MEMBER_STATE_RW) {
1355 if (__bch2_dev_read_write(c, ca))
1358 __bch2_dev_read_only(c, ca);
1361 bch_notice(ca, "%s", bch2_dev_state[new_state]);
1363 mutex_lock(&c->sb_lock);
1364 mi = bch2_sb_get_members(c->disk_sb);
1365 SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
1366 bch2_write_super(c);
1367 mutex_unlock(&c->sb_lock);
1372 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1373 enum bch_member_state new_state, int flags)
1377 mutex_lock(&c->state_lock);
1378 ret = __bch2_dev_set_state(c, ca, new_state, flags);
1379 mutex_unlock(&c->state_lock);
1384 /* Device add/removal: */
1386 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
1388 struct bch_sb_field_members *mi;
1389 unsigned dev_idx = ca->dev_idx, data;
1392 mutex_lock(&c->state_lock);
1394 percpu_ref_put(&ca->ref); /* XXX */
1396 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1397 bch_err(ca, "Cannot remove RW device");
1401 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1402 bch_err(ca, "Cannot remove without losing data");
1407 * XXX: verify that dev_idx is really not in use anymore, anywhere
1409 * flag_data_bad() does not check btree pointers
1411 ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
1413 bch_err(ca, "Remove failed: error %i dropping data", ret);
1417 ret = bch2_journal_flush_device(&c->journal, ca->dev_idx);
1419 bch_err(ca, "Remove failed: error %i flushing journal", ret);
1423 data = bch2_dev_has_data(c, ca);
1425 char data_has_str[100];
1426 bch2_scnprint_flag_list(data_has_str,
1427 sizeof(data_has_str),
1430 bch_err(ca, "Remove failed, still has data (%s)", data_has_str);
1435 ret = bch2_btree_delete_range(c, BTREE_ID_ALLOC,
1436 POS(ca->dev_idx, 0),
1437 POS(ca->dev_idx + 1, 0),
1441 bch_err(ca, "Remove failed, error deleting alloc info");
1446 * must flush all existing journal entries, they might have
1447 * (overwritten) keys that point to the device we're removing:
1449 ret = bch2_journal_flush_all_pins(&c->journal);
1451 bch_err(ca, "Remove failed, journal error");
1455 __bch2_dev_offline(ca);
1457 mutex_lock(&c->sb_lock);
1458 rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
1459 mutex_unlock(&c->sb_lock);
1461 percpu_ref_kill(&ca->ref);
1462 wait_for_completion(&ca->ref_completion);
1467 * Free this device's slot in the bch_member array - all pointers to
1468 * this device must be gone:
1470 mutex_lock(&c->sb_lock);
1471 mi = bch2_sb_get_members(c->disk_sb);
1472 memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
1474 bch2_write_super(c);
1476 mutex_unlock(&c->sb_lock);
1477 mutex_unlock(&c->state_lock);
1480 mutex_unlock(&c->state_lock);
1484 /* Add new device to running filesystem: */
1485 int bch2_dev_add(struct bch_fs *c, const char *path)
1487 struct bch_sb_handle sb;
1489 struct bch_dev *ca = NULL;
1490 struct bch_sb_field_members *mi, *dev_mi;
1491 struct bch_member saved_mi;
1492 unsigned dev_idx, nr_devices, u64s;
1495 err = bch2_read_super(path, bch2_opts_empty(), &sb);
1499 err = bch2_sb_validate(&sb);
1503 err = bch2_dev_may_add(sb.sb, c);
1507 mutex_lock(&c->state_lock);
1508 mutex_lock(&c->sb_lock);
1511 * Preserve the old cache member information (esp. tier)
1512 * before we start bashing the disk stuff.
1514 dev_mi = bch2_sb_get_members(sb.sb);
1515 saved_mi = dev_mi->members[sb.sb->dev_idx];
1516 saved_mi.last_mount = cpu_to_le64(ktime_get_seconds());
1518 if (dynamic_fault("bcachefs:add:no_slot"))
1521 mi = bch2_sb_get_members(c->disk_sb);
1522 for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
1523 if (!bch2_dev_exists(c->disk_sb, mi, dev_idx))
1526 err = "no slots available in superblock";
1531 nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
1532 u64s = (sizeof(struct bch_sb_field_members) +
1533 sizeof(struct bch_member) * nr_devices) / sizeof(u64);
1534 err = "no space in superblock for member info";
1536 mi = bch2_fs_sb_resize_members(c, u64s);
1540 dev_mi = bch2_sb_resize_members(&sb, u64s);
1544 memcpy(dev_mi, mi, u64s * sizeof(u64));
1545 dev_mi->members[dev_idx] = saved_mi;
1547 sb.sb->uuid = c->disk_sb->uuid;
1548 sb.sb->dev_idx = dev_idx;
1549 sb.sb->nr_devices = nr_devices;
1551 /* commit new member info */
1552 memcpy(mi, dev_mi, u64s * sizeof(u64));
1553 c->disk_sb->nr_devices = nr_devices;
1554 c->sb.nr_devices = nr_devices;
1556 if (bch2_dev_alloc(c, dev_idx)) {
1557 err = "cannot allocate memory";
1562 if (__bch2_dev_online(c, &sb)) {
1563 err = "bch2_dev_online() error";
1568 bch2_write_super(c);
1569 mutex_unlock(&c->sb_lock);
1571 ca = bch_dev_locked(c, dev_idx);
1572 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1573 err = "journal alloc failed";
1574 if (bch2_dev_journal_alloc(ca))
1577 err = __bch2_dev_read_write(c, ca);
1582 mutex_unlock(&c->state_lock);
1585 mutex_unlock(&c->sb_lock);
1587 mutex_unlock(&c->state_lock);
1588 bch2_free_super(&sb);
1590 bch_err(c, "Unable to add device: %s", err);
1591 return ret ?: -EINVAL;
1594 /* Hot add existing device to running filesystem: */
1595 int bch2_dev_online(struct bch_fs *c, const char *path)
1597 struct bch_sb_handle sb = { NULL };
1602 mutex_lock(&c->state_lock);
1604 err = bch2_read_super(path, bch2_opts_empty(), &sb);
1608 dev_idx = sb.sb->dev_idx;
1610 err = bch2_dev_in_fs(c->disk_sb, sb.sb);
1614 mutex_lock(&c->sb_lock);
1615 if (__bch2_dev_online(c, &sb)) {
1616 err = "__bch2_dev_online() error";
1617 mutex_unlock(&c->sb_lock);
1620 mutex_unlock(&c->sb_lock);
1622 ca = bch_dev_locked(c, dev_idx);
1623 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1624 err = __bch2_dev_read_write(c, ca);
1629 mutex_unlock(&c->state_lock);
1632 mutex_unlock(&c->state_lock);
1633 bch2_free_super(&sb);
1634 bch_err(c, "error bringing %s online: %s", path, err);
1638 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
1640 mutex_lock(&c->state_lock);
1642 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1643 bch_err(ca, "Cannot offline required disk");
1644 mutex_unlock(&c->state_lock);
1648 __bch2_dev_offline(ca);
1650 mutex_unlock(&c->state_lock);
1654 int bch2_dev_evacuate(struct bch_fs *c, struct bch_dev *ca)
1659 mutex_lock(&c->state_lock);
1661 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1662 bch_err(ca, "Cannot migrate data off RW device");
1667 ret = bch2_dev_data_migrate(c, ca, 0);
1669 bch_err(ca, "Error migrating data: %i", ret);
1673 data = bch2_dev_has_data(c, ca);
1675 bch_err(ca, "Migrate error: data still present (%x)", data);
1680 mutex_unlock(&c->state_lock);
1684 /* Filesystem open: */
1686 const char *bch2_fs_open(char * const *devices, unsigned nr_devices,
1687 struct bch_opts opts, struct bch_fs **ret)
1690 struct bch_fs *c = NULL;
1691 struct bch_sb_handle *sb;
1692 unsigned i, best_sb = 0;
1695 return "need at least one device";
1697 if (!try_module_get(THIS_MODULE))
1698 return "module unloading";
1700 err = "cannot allocate memory";
1701 sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
1705 for (i = 0; i < nr_devices; i++) {
1706 err = bch2_read_super(devices[i], opts, &sb[i]);
1710 err = bch2_sb_validate(&sb[i]);
1715 for (i = 1; i < nr_devices; i++)
1716 if (le64_to_cpu(sb[i].sb->seq) >
1717 le64_to_cpu(sb[best_sb].sb->seq))
1720 for (i = 0; i < nr_devices; i++) {
1721 err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
1726 err = "cannot allocate memory";
1727 c = bch2_fs_alloc(sb[best_sb].sb, opts);
1731 err = "bch2_dev_online() error";
1732 mutex_lock(&c->sb_lock);
1733 for (i = 0; i < nr_devices; i++)
1734 if (__bch2_dev_online(c, &sb[i])) {
1735 mutex_unlock(&c->sb_lock);
1738 mutex_unlock(&c->sb_lock);
1740 err = "insufficient devices";
1741 if (!bch2_fs_may_start(c))
1744 if (!c->opts.nostart) {
1745 err = __bch2_fs_start(c);
1750 err = bch2_fs_online(c);
1757 closure_put(&c->cl);
1762 module_put(THIS_MODULE);
1770 for (i = 0; i < nr_devices; i++)
1771 bch2_free_super(&sb[i]);
1775 static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb,
1776 struct bch_opts opts)
1780 bool allocated_fs = false;
1782 err = bch2_sb_validate(sb);
1786 mutex_lock(&bch_fs_list_lock);
1787 c = __bch2_uuid_to_fs(sb->sb->uuid);
1789 closure_get(&c->cl);
1791 err = bch2_dev_in_fs(c->disk_sb, sb->sb);
1795 c = bch2_fs_alloc(sb->sb, opts);
1796 err = "cannot allocate memory";
1800 allocated_fs = true;
1803 err = "bch2_dev_online() error";
1805 mutex_lock(&c->sb_lock);
1806 if (__bch2_dev_online(c, sb)) {
1807 mutex_unlock(&c->sb_lock);
1810 mutex_unlock(&c->sb_lock);
1812 if (!c->opts.nostart && bch2_fs_may_start(c)) {
1813 err = __bch2_fs_start(c);
1818 err = __bch2_fs_online(c);
1822 closure_put(&c->cl);
1823 mutex_unlock(&bch_fs_list_lock);
1827 mutex_unlock(&bch_fs_list_lock);
1832 closure_put(&c->cl);
1837 const char *bch2_fs_open_incremental(const char *path)
1839 struct bch_sb_handle sb;
1840 struct bch_opts opts = bch2_opts_empty();
1843 err = bch2_read_super(path, opts, &sb);
1847 err = __bch2_fs_open_incremental(&sb, opts);
1848 bch2_free_super(&sb);
1853 /* Global interfaces/init */
1855 static void bcachefs_exit(void)
1859 bch2_chardev_exit();
1861 kset_unregister(bcachefs_kset);
1864 static int __init bcachefs_init(void)
1866 bch2_bkey_pack_test();
1867 bch2_inode_pack_test();
1869 if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
1870 bch2_chardev_init() ||
1881 #define BCH_DEBUG_PARAM(name, description) \
1883 module_param_named(name, bch2_##name, bool, 0644); \
1884 MODULE_PARM_DESC(name, description);
1886 #undef BCH_DEBUG_PARAM
1888 module_exit(bcachefs_exit);
1889 module_init(bcachefs_init);