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_key_cache.h"
17 #include "btree_update_interior.h"
24 #include "disk_groups.h"
33 #include "journal_reclaim.h"
34 #include "journal_seq_blacklist.h"
39 #include "rebalance.h"
42 #include "subvolume.h"
47 #include <linux/backing-dev.h>
48 #include <linux/blkdev.h>
49 #include <linux/debugfs.h>
50 #include <linux/device.h>
51 #include <linux/genhd.h>
52 #include <linux/idr.h>
53 #include <linux/module.h>
54 #include <linux/percpu.h>
55 #include <linux/random.h>
56 #include <linux/sysfs.h>
57 #include <crypto/hash.h>
59 #include <trace/events/bcachefs.h>
61 MODULE_LICENSE("GPL");
62 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
65 struct kobj_type type ## _ktype = { \
66 .release = type ## _release, \
67 .sysfs_ops = &type ## _sysfs_ops, \
68 .default_attrs = type ## _files \
71 static void bch2_fs_release(struct kobject *);
72 static void bch2_dev_release(struct kobject *);
74 static void bch2_fs_internal_release(struct kobject *k)
78 static void bch2_fs_opts_dir_release(struct kobject *k)
82 static void bch2_fs_time_stats_release(struct kobject *k)
86 static KTYPE(bch2_fs);
87 static KTYPE(bch2_fs_internal);
88 static KTYPE(bch2_fs_opts_dir);
89 static KTYPE(bch2_fs_time_stats);
90 static KTYPE(bch2_dev);
92 static struct kset *bcachefs_kset;
93 static LIST_HEAD(bch_fs_list);
94 static DEFINE_MUTEX(bch_fs_list_lock);
96 static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
98 static void bch2_dev_free(struct bch_dev *);
99 static int bch2_dev_alloc(struct bch_fs *, unsigned);
100 static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *);
101 static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
103 struct bch_fs *bch2_dev_to_fs(dev_t dev)
109 mutex_lock(&bch_fs_list_lock);
112 list_for_each_entry(c, &bch_fs_list, list)
113 for_each_member_device_rcu(ca, c, i, NULL)
114 if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) {
121 mutex_unlock(&bch_fs_list_lock);
126 static struct bch_fs *__bch2_uuid_to_fs(uuid_le uuid)
130 lockdep_assert_held(&bch_fs_list_lock);
132 list_for_each_entry(c, &bch_fs_list, list)
133 if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid_le)))
139 struct bch_fs *bch2_uuid_to_fs(uuid_le uuid)
143 mutex_lock(&bch_fs_list_lock);
144 c = __bch2_uuid_to_fs(uuid);
147 mutex_unlock(&bch_fs_list_lock);
152 static void bch2_dev_usage_journal_reserve(struct bch_fs *c)
155 unsigned i, nr = 0, u64s =
156 ((sizeof(struct jset_entry_dev_usage) +
157 sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR)) /
161 for_each_member_device_rcu(ca, c, i, NULL)
165 bch2_journal_entry_res_resize(&c->journal,
166 &c->dev_usage_journal_res, u64s * nr);
169 /* Filesystem RO/RW: */
172 * For startup/shutdown of RW stuff, the dependencies are:
174 * - foreground writes depend on copygc and rebalance (to free up space)
176 * - copygc and rebalance depend on mark and sweep gc (they actually probably
177 * don't because they either reserve ahead of time or don't block if
178 * allocations fail, but allocations can require mark and sweep gc to run
179 * because of generation number wraparound)
181 * - all of the above depends on the allocator threads
183 * - allocator depends on the journal (when it rewrites prios and gens)
186 static void __bch2_fs_read_only(struct bch_fs *c)
189 unsigned i, clean_passes = 0;
191 bch2_rebalance_stop(c);
193 bch2_gc_thread_stop(c);
196 * Flush journal before stopping allocators, because flushing journal
197 * blacklist entries involves allocating new btree nodes:
199 bch2_journal_flush_all_pins(&c->journal);
202 * If the allocator threads didn't all start up, the btree updates to
203 * write out alloc info aren't going to work:
205 if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags))
208 bch_verbose(c, "flushing journal and stopping allocators");
210 bch2_journal_flush_all_pins(&c->journal);
211 set_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags);
216 if (bch2_journal_flush_all_pins(&c->journal))
220 * In flight interior btree updates will generate more journal
221 * updates and btree updates (alloc btree):
223 if (bch2_btree_interior_updates_nr_pending(c)) {
224 closure_wait_event(&c->btree_interior_update_wait,
225 !bch2_btree_interior_updates_nr_pending(c));
228 flush_work(&c->btree_interior_update_work);
230 if (bch2_journal_flush_all_pins(&c->journal))
232 } while (clean_passes < 2);
233 bch_verbose(c, "flushing journal and stopping allocators complete");
235 set_bit(BCH_FS_ALLOC_CLEAN, &c->flags);
237 closure_wait_event(&c->btree_interior_update_wait,
238 !bch2_btree_interior_updates_nr_pending(c));
239 flush_work(&c->btree_interior_update_work);
241 for_each_member_device(ca, c, i)
242 bch2_dev_allocator_stop(ca);
244 clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
245 clear_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags);
247 bch2_fs_journal_stop(&c->journal);
250 * the journal kicks off btree writes via reclaim - wait for in flight
251 * writes after stopping journal:
253 bch2_btree_flush_all_writes(c);
256 * After stopping journal:
258 for_each_member_device(ca, c, i)
259 bch2_dev_allocator_remove(c, ca);
262 static void bch2_writes_disabled(struct percpu_ref *writes)
264 struct bch_fs *c = container_of(writes, struct bch_fs, writes);
266 set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
267 wake_up(&bch_read_only_wait);
270 void bch2_fs_read_only(struct bch_fs *c)
272 if (!test_bit(BCH_FS_RW, &c->flags)) {
273 bch2_journal_reclaim_stop(&c->journal);
277 BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
280 * Block new foreground-end write operations from starting - any new
281 * writes will return -EROFS:
283 * (This is really blocking new _allocations_, writes to previously
284 * allocated space can still happen until stopping the allocator in
285 * bch2_dev_allocator_stop()).
287 percpu_ref_kill(&c->writes);
289 cancel_work_sync(&c->ec_stripe_delete_work);
292 * If we're not doing an emergency shutdown, we want to wait on
293 * outstanding writes to complete so they don't see spurious errors due
294 * to shutting down the allocator:
296 * If we are doing an emergency shutdown outstanding writes may
297 * hang until we shutdown the allocator so we don't want to wait
298 * on outstanding writes before shutting everything down - but
299 * we do need to wait on them before returning and signalling
300 * that going RO is complete:
302 wait_event(bch_read_only_wait,
303 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
304 test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
306 __bch2_fs_read_only(c);
308 wait_event(bch_read_only_wait,
309 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
311 clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
313 if (!bch2_journal_error(&c->journal) &&
314 !test_bit(BCH_FS_ERROR, &c->flags) &&
315 !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) &&
316 test_bit(BCH_FS_STARTED, &c->flags) &&
317 test_bit(BCH_FS_ALLOC_CLEAN, &c->flags) &&
318 !c->opts.norecovery) {
319 bch_verbose(c, "marking filesystem clean");
320 bch2_fs_mark_clean(c);
323 clear_bit(BCH_FS_RW, &c->flags);
326 static void bch2_fs_read_only_work(struct work_struct *work)
329 container_of(work, struct bch_fs, read_only_work);
331 down_write(&c->state_lock);
332 bch2_fs_read_only(c);
333 up_write(&c->state_lock);
336 static void bch2_fs_read_only_async(struct bch_fs *c)
338 queue_work(system_long_wq, &c->read_only_work);
341 bool bch2_fs_emergency_read_only(struct bch_fs *c)
343 bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
345 bch2_journal_halt(&c->journal);
346 bch2_fs_read_only_async(c);
348 wake_up(&bch_read_only_wait);
352 static int bch2_fs_read_write_late(struct bch_fs *c)
356 ret = bch2_gc_thread_start(c);
358 bch_err(c, "error starting gc thread");
362 ret = bch2_copygc_start(c);
364 bch_err(c, "error starting copygc thread");
368 ret = bch2_rebalance_start(c);
370 bch_err(c, "error starting rebalance thread");
374 schedule_work(&c->ec_stripe_delete_work);
379 static int __bch2_fs_read_write(struct bch_fs *c, bool early)
385 if (test_bit(BCH_FS_INITIAL_GC_UNFIXED, &c->flags)) {
386 bch_err(c, "cannot go rw, unfixed btree errors");
390 if (test_bit(BCH_FS_RW, &c->flags))
394 * nochanges is used for fsck -n mode - we have to allow going rw
395 * during recovery for that to work:
397 if (c->opts.norecovery ||
398 (c->opts.nochanges &&
399 (!early || c->opts.read_only)))
402 bch_info(c, "going read-write");
404 ret = bch2_fs_mark_dirty(c);
408 clear_bit(BCH_FS_ALLOC_CLEAN, &c->flags);
410 for_each_rw_member(ca, c, i)
411 bch2_dev_allocator_add(c, ca);
412 bch2_recalc_capacity(c);
414 for_each_rw_member(ca, c, i) {
415 ret = bch2_dev_allocator_start(ca);
417 bch_err(c, "error starting allocator threads");
418 percpu_ref_put(&ca->io_ref);
423 set_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
425 for_each_rw_member(ca, c, i)
426 bch2_wake_allocator(ca);
429 ret = bch2_fs_read_write_late(c);
434 percpu_ref_reinit(&c->writes);
435 set_bit(BCH_FS_RW, &c->flags);
436 set_bit(BCH_FS_WAS_RW, &c->flags);
439 __bch2_fs_read_only(c);
443 int bch2_fs_read_write(struct bch_fs *c)
445 return __bch2_fs_read_write(c, false);
448 int bch2_fs_read_write_early(struct bch_fs *c)
450 lockdep_assert_held(&c->state_lock);
452 return __bch2_fs_read_write(c, true);
455 /* Filesystem startup/shutdown: */
457 static void __bch2_fs_free(struct bch_fs *c)
462 for (i = 0; i < BCH_TIME_STAT_NR; i++)
463 bch2_time_stats_exit(&c->times[i]);
465 bch2_fs_snapshots_exit(c);
466 bch2_fs_quota_exit(c);
467 bch2_fs_fsio_exit(c);
469 bch2_fs_encryption_exit(c);
471 bch2_fs_btree_interior_update_exit(c);
472 bch2_fs_btree_iter_exit(c);
473 bch2_fs_btree_key_cache_exit(&c->btree_key_cache);
474 bch2_fs_btree_cache_exit(c);
475 bch2_fs_replicas_exit(c);
476 bch2_fs_journal_exit(&c->journal);
477 bch2_io_clock_exit(&c->io_clock[WRITE]);
478 bch2_io_clock_exit(&c->io_clock[READ]);
479 bch2_fs_compress_exit(c);
480 bch2_journal_keys_free(&c->journal_keys);
481 bch2_journal_entries_free(&c->journal_entries);
482 percpu_free_rwsem(&c->mark_lock);
484 if (c->btree_paths_bufs)
485 for_each_possible_cpu(cpu)
486 kfree(per_cpu_ptr(c->btree_paths_bufs, cpu)->path);
488 free_percpu(c->online_reserved);
489 free_percpu(c->btree_paths_bufs);
490 free_percpu(c->pcpu);
491 mempool_exit(&c->large_bkey_pool);
492 mempool_exit(&c->btree_bounce_pool);
493 bioset_exit(&c->btree_bio);
494 mempool_exit(&c->fill_iter);
495 percpu_ref_exit(&c->writes);
496 kfree(rcu_dereference_protected(c->disk_groups, 1));
497 kfree(c->journal_seq_blacklist_table);
498 kfree(c->unused_inode_hints);
499 free_heap(&c->copygc_heap);
501 if (c->io_complete_wq )
502 destroy_workqueue(c->io_complete_wq );
504 destroy_workqueue(c->copygc_wq);
505 if (c->btree_io_complete_wq)
506 destroy_workqueue(c->btree_io_complete_wq);
507 if (c->btree_update_wq)
508 destroy_workqueue(c->btree_update_wq);
510 bch2_free_super(&c->disk_sb);
511 kvpfree(c, sizeof(*c));
512 module_put(THIS_MODULE);
515 static void bch2_fs_release(struct kobject *kobj)
517 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
522 void __bch2_fs_stop(struct bch_fs *c)
527 bch_verbose(c, "shutting down");
529 set_bit(BCH_FS_STOPPING, &c->flags);
531 cancel_work_sync(&c->journal_seq_blacklist_gc_work);
533 down_write(&c->state_lock);
534 bch2_fs_read_only(c);
535 up_write(&c->state_lock);
537 for_each_member_device(ca, c, i)
538 if (ca->kobj.state_in_sysfs &&
540 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
542 if (c->kobj.state_in_sysfs)
543 kobject_del(&c->kobj);
545 bch2_fs_debug_exit(c);
546 bch2_fs_chardev_exit(c);
548 kobject_put(&c->time_stats);
549 kobject_put(&c->opts_dir);
550 kobject_put(&c->internal);
552 /* btree prefetch might have kicked off reads in the background: */
553 bch2_btree_flush_all_reads(c);
555 for_each_member_device(ca, c, i)
556 cancel_work_sync(&ca->io_error_work);
558 cancel_work_sync(&c->read_only_work);
560 for (i = 0; i < c->sb.nr_devices; i++)
562 bch2_free_super(&c->devs[i]->disk_sb);
565 void bch2_fs_free(struct bch_fs *c)
569 mutex_lock(&bch_fs_list_lock);
571 mutex_unlock(&bch_fs_list_lock);
573 closure_sync(&c->cl);
574 closure_debug_destroy(&c->cl);
576 for (i = 0; i < c->sb.nr_devices; i++)
578 bch2_dev_free(rcu_dereference_protected(c->devs[i], 1));
580 bch_verbose(c, "shutdown complete");
582 kobject_put(&c->kobj);
585 void bch2_fs_stop(struct bch_fs *c)
591 static int bch2_fs_online(struct bch_fs *c)
597 lockdep_assert_held(&bch_fs_list_lock);
599 if (__bch2_uuid_to_fs(c->sb.uuid)) {
600 bch_err(c, "filesystem UUID already open");
604 ret = bch2_fs_chardev_init(c);
606 bch_err(c, "error creating character device");
610 bch2_fs_debug_init(c);
612 ret = kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ?:
613 kobject_add(&c->internal, &c->kobj, "internal") ?:
614 kobject_add(&c->opts_dir, &c->kobj, "options") ?:
615 kobject_add(&c->time_stats, &c->kobj, "time_stats") ?:
616 bch2_opts_create_sysfs_files(&c->opts_dir);
618 bch_err(c, "error creating sysfs objects");
622 down_write(&c->state_lock);
624 for_each_member_device(ca, c, i) {
625 ret = bch2_dev_sysfs_online(c, ca);
627 bch_err(c, "error creating sysfs objects");
628 percpu_ref_put(&ca->ref);
633 BUG_ON(!list_empty(&c->list));
634 list_add(&c->list, &bch_fs_list);
636 up_write(&c->state_lock);
640 static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
642 struct bch_sb_field_members *mi;
644 unsigned i, iter_size;
647 pr_verbose_init(opts, "");
649 c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO);
651 c = ERR_PTR(-ENOMEM);
655 __module_get(THIS_MODULE);
657 closure_init(&c->cl, NULL);
659 c->kobj.kset = bcachefs_kset;
660 kobject_init(&c->kobj, &bch2_fs_ktype);
661 kobject_init(&c->internal, &bch2_fs_internal_ktype);
662 kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype);
663 kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype);
666 c->disk_sb.fs_sb = true;
668 init_rwsem(&c->state_lock);
669 mutex_init(&c->sb_lock);
670 mutex_init(&c->replicas_gc_lock);
671 mutex_init(&c->btree_root_lock);
672 INIT_WORK(&c->read_only_work, bch2_fs_read_only_work);
674 init_rwsem(&c->gc_lock);
676 for (i = 0; i < BCH_TIME_STAT_NR; i++)
677 bch2_time_stats_init(&c->times[i]);
679 bch2_fs_copygc_init(c);
680 bch2_fs_btree_key_cache_init_early(&c->btree_key_cache);
681 bch2_fs_allocator_background_init(c);
682 bch2_fs_allocator_foreground_init(c);
683 bch2_fs_rebalance_init(c);
684 bch2_fs_quota_init(c);
686 INIT_LIST_HEAD(&c->list);
688 mutex_init(&c->usage_scratch_lock);
690 mutex_init(&c->bio_bounce_pages_lock);
691 mutex_init(&c->snapshot_table_lock);
693 spin_lock_init(&c->btree_write_error_lock);
695 INIT_WORK(&c->journal_seq_blacklist_gc_work,
696 bch2_blacklist_entries_gc);
698 INIT_LIST_HEAD(&c->journal_entries);
699 INIT_LIST_HEAD(&c->journal_iters);
701 INIT_LIST_HEAD(&c->fsck_errors);
702 mutex_init(&c->fsck_error_lock);
704 INIT_LIST_HEAD(&c->ec_stripe_head_list);
705 mutex_init(&c->ec_stripe_head_lock);
707 INIT_LIST_HEAD(&c->ec_stripe_new_list);
708 mutex_init(&c->ec_stripe_new_lock);
710 INIT_LIST_HEAD(&c->data_progress_list);
711 mutex_init(&c->data_progress_lock);
713 spin_lock_init(&c->ec_stripes_heap_lock);
715 seqcount_init(&c->gc_pos_lock);
717 seqcount_init(&c->usage_lock);
719 sema_init(&c->io_in_flight, 64);
721 c->copy_gc_enabled = 1;
722 c->rebalance.enabled = 1;
723 c->promote_whole_extents = true;
725 c->journal.flush_write_time = &c->times[BCH_TIME_journal_flush_write];
726 c->journal.noflush_write_time = &c->times[BCH_TIME_journal_noflush_write];
727 c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal];
728 c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq];
730 bch2_fs_btree_cache_init_early(&c->btree_cache);
732 mutex_init(&c->sectors_available_lock);
734 ret = percpu_init_rwsem(&c->mark_lock);
738 mutex_lock(&c->sb_lock);
739 ret = bch2_sb_to_fs(c, sb);
740 mutex_unlock(&c->sb_lock);
745 uuid_unparse_lower(c->sb.user_uuid.b, c->name);
748 if (sb->version <= bcachefs_metadata_version_inode_v2 &&
749 !BCH_SB_JOURNAL_FLUSH_DELAY(sb))
750 SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000);
752 if (sb->version <= bcachefs_metadata_version_inode_v2 &&
753 !BCH_SB_JOURNAL_RECLAIM_DELAY(sb))
754 SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 100);
756 c->opts = bch2_opts_default;
757 ret = bch2_opts_from_sb(&c->opts, sb);
761 bch2_opts_apply(&c->opts, opts);
763 c->block_bits = ilog2(block_sectors(c));
764 c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
766 if (bch2_fs_init_fault("fs_alloc")) {
767 bch_err(c, "fs_alloc fault injected");
772 iter_size = sizeof(struct sort_iter) +
773 (btree_blocks(c) + 1) * 2 *
774 sizeof(struct sort_iter_set);
776 c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus()));
778 if (!(c->btree_update_wq = alloc_workqueue("bcachefs",
779 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
780 !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io",
781 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
782 !(c->copygc_wq = alloc_workqueue("bcachefs_copygc",
783 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
784 !(c->io_complete_wq = alloc_workqueue("bcachefs_io",
785 WQ_FREEZABLE|WQ_HIGHPRI|WQ_MEM_RECLAIM, 1)) ||
786 percpu_ref_init(&c->writes, bch2_writes_disabled,
787 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
788 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
789 bioset_init(&c->btree_bio, 1,
790 max(offsetof(struct btree_read_bio, bio),
791 offsetof(struct btree_write_bio, wbio.bio)),
792 BIOSET_NEED_BVECS) ||
793 !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) ||
794 !(c->btree_paths_bufs = alloc_percpu(struct btree_path_buf)) ||
795 !(c->online_reserved = alloc_percpu(u64)) ||
796 mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1,
798 mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) ||
799 !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits,
800 sizeof(u64), GFP_KERNEL))) {
805 ret = bch2_io_clock_init(&c->io_clock[READ]) ?:
806 bch2_io_clock_init(&c->io_clock[WRITE]) ?:
807 bch2_fs_journal_init(&c->journal) ?:
808 bch2_fs_replicas_init(c) ?:
809 bch2_fs_btree_cache_init(c) ?:
810 bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?:
811 bch2_fs_btree_iter_init(c) ?:
812 bch2_fs_btree_interior_update_init(c) ?:
813 bch2_fs_subvolumes_init(c) ?:
814 bch2_fs_io_init(c) ?:
815 bch2_fs_encryption_init(c) ?:
816 bch2_fs_compress_init(c) ?:
817 bch2_fs_ec_init(c) ?:
818 bch2_fs_fsio_init(c);
822 if (c->opts.nochanges)
823 set_bit(JOURNAL_NOCHANGES, &c->journal.flags);
825 mi = bch2_sb_get_members(c->disk_sb.sb);
826 for (i = 0; i < c->sb.nr_devices; i++)
827 if (bch2_dev_exists(c->disk_sb.sb, mi, i) &&
828 bch2_dev_alloc(c, i)) {
833 bch2_journal_entry_res_resize(&c->journal,
834 &c->btree_root_journal_res,
835 BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX));
836 bch2_dev_usage_journal_reserve(c);
837 bch2_journal_entry_res_resize(&c->journal,
838 &c->clock_journal_res,
839 (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2);
841 mutex_lock(&bch_fs_list_lock);
842 ret = bch2_fs_online(c);
843 mutex_unlock(&bch_fs_list_lock);
848 pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
857 static void print_mount_opts(struct bch_fs *c)
861 struct printbuf p = PBUF(buf);
864 strcpy(buf, "(null)");
866 if (c->opts.read_only) {
871 for (i = 0; i < bch2_opts_nr; i++) {
872 const struct bch_option *opt = &bch2_opt_table[i];
873 u64 v = bch2_opt_get_by_id(&c->opts, i);
875 if (!(opt->flags & OPT_MOUNT))
878 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
884 bch2_opt_to_text(&p, c, opt, v, OPT_SHOW_MOUNT_STYLE);
887 bch_info(c, "mounted with opts: %s", buf);
890 int bch2_fs_start(struct bch_fs *c)
892 struct bch_sb_field_members *mi;
894 time64_t now = ktime_get_real_seconds();
898 down_write(&c->state_lock);
900 BUG_ON(test_bit(BCH_FS_STARTED, &c->flags));
902 mutex_lock(&c->sb_lock);
904 for_each_online_member(ca, c, i)
905 bch2_sb_from_fs(c, ca);
907 mi = bch2_sb_get_members(c->disk_sb.sb);
908 for_each_online_member(ca, c, i)
909 mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
911 mutex_unlock(&c->sb_lock);
913 for_each_rw_member(ca, c, i)
914 bch2_dev_allocator_add(c, ca);
915 bch2_recalc_capacity(c);
917 ret = BCH_SB_INITIALIZED(c->disk_sb.sb)
918 ? bch2_fs_recovery(c)
919 : bch2_fs_initialize(c);
923 ret = bch2_opts_check_may_set(c);
928 if (bch2_fs_init_fault("fs_start")) {
929 bch_err(c, "fs_start fault injected");
933 set_bit(BCH_FS_STARTED, &c->flags);
936 * Allocator threads don't start filling copygc reserve until after we
937 * set BCH_FS_STARTED - wake them now:
940 * Need to set ca->allocator_state here instead of relying on the
941 * allocator threads to do it to avoid racing with the copygc threads
942 * checking it and thinking they have no alloc reserve:
944 for_each_online_member(ca, c, i) {
945 ca->allocator_state = ALLOCATOR_running;
946 bch2_wake_allocator(ca);
949 if (c->opts.read_only || c->opts.nochanges) {
950 bch2_fs_read_only(c);
952 ret = !test_bit(BCH_FS_RW, &c->flags)
953 ? bch2_fs_read_write(c)
954 : bch2_fs_read_write_late(c);
962 up_write(&c->state_lock);
966 case BCH_FSCK_ERRORS_NOT_FIXED:
967 bch_err(c, "filesystem contains errors: please report this to the developers");
968 pr_cont("mount with -o fix_errors to repair\n");
970 case BCH_FSCK_REPAIR_UNIMPLEMENTED:
971 bch_err(c, "filesystem contains errors: please report this to the developers");
972 pr_cont("repair unimplemented: inform the developers so that it can be added\n");
974 case BCH_FSCK_REPAIR_IMPOSSIBLE:
975 bch_err(c, "filesystem contains errors, but repair impossible");
977 case BCH_FSCK_UNKNOWN_VERSION:
978 bch_err(c, "unknown metadata version");
981 bch_err(c, "cannot allocate memory");
984 bch_err(c, "IO error");
993 static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
995 struct bch_sb_field_members *sb_mi;
997 sb_mi = bch2_sb_get_members(sb);
999 return "Invalid superblock: member info area missing";
1001 if (le16_to_cpu(sb->block_size) != block_sectors(c))
1002 return "mismatched block size";
1004 if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
1005 BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
1006 return "new cache bucket size is too small";
1011 static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
1013 struct bch_sb *newest =
1014 le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
1015 struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
1017 if (uuid_le_cmp(fs->uuid, sb->uuid))
1018 return "device not a member of filesystem";
1020 if (!bch2_dev_exists(newest, mi, sb->dev_idx))
1021 return "device has been removed";
1023 if (fs->block_size != sb->block_size)
1024 return "mismatched block size";
1029 /* Device startup/shutdown: */
1031 static void bch2_dev_release(struct kobject *kobj)
1033 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
1038 static void bch2_dev_free(struct bch_dev *ca)
1040 bch2_dev_allocator_stop(ca);
1042 cancel_work_sync(&ca->io_error_work);
1044 if (ca->kobj.state_in_sysfs &&
1046 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
1048 if (ca->kobj.state_in_sysfs)
1049 kobject_del(&ca->kobj);
1051 bch2_free_super(&ca->disk_sb);
1052 bch2_dev_journal_exit(ca);
1054 free_percpu(ca->io_done);
1055 bioset_exit(&ca->replica_set);
1056 bch2_dev_buckets_free(ca);
1057 free_page((unsigned long) ca->sb_read_scratch);
1059 bch2_time_stats_exit(&ca->io_latency[WRITE]);
1060 bch2_time_stats_exit(&ca->io_latency[READ]);
1062 percpu_ref_exit(&ca->io_ref);
1063 percpu_ref_exit(&ca->ref);
1064 kobject_put(&ca->kobj);
1067 static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca)
1070 lockdep_assert_held(&c->state_lock);
1072 if (percpu_ref_is_zero(&ca->io_ref))
1075 __bch2_dev_read_only(c, ca);
1077 reinit_completion(&ca->io_ref_completion);
1078 percpu_ref_kill(&ca->io_ref);
1079 wait_for_completion(&ca->io_ref_completion);
1081 if (ca->kobj.state_in_sysfs) {
1082 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
1083 sysfs_remove_link(&ca->kobj, "block");
1086 bch2_free_super(&ca->disk_sb);
1087 bch2_dev_journal_exit(ca);
1090 static void bch2_dev_ref_complete(struct percpu_ref *ref)
1092 struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
1094 complete(&ca->ref_completion);
1097 static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
1099 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
1101 complete(&ca->io_ref_completion);
1104 static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca)
1108 if (!c->kobj.state_in_sysfs)
1111 if (!ca->kobj.state_in_sysfs) {
1112 ret = kobject_add(&ca->kobj, &c->kobj,
1113 "dev-%u", ca->dev_idx);
1118 if (ca->disk_sb.bdev) {
1119 struct kobject *block = bdev_kobj(ca->disk_sb.bdev);
1121 ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
1125 ret = sysfs_create_link(&ca->kobj, block, "block");
1133 static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c,
1134 struct bch_member *member)
1138 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1142 kobject_init(&ca->kobj, &bch2_dev_ktype);
1143 init_completion(&ca->ref_completion);
1144 init_completion(&ca->io_ref_completion);
1146 init_rwsem(&ca->bucket_lock);
1148 INIT_WORK(&ca->io_error_work, bch2_io_error_work);
1150 bch2_time_stats_init(&ca->io_latency[READ]);
1151 bch2_time_stats_init(&ca->io_latency[WRITE]);
1153 ca->mi = bch2_mi_to_cpu(member);
1154 ca->uuid = member->uuid;
1156 if (opt_defined(c->opts, discard))
1157 ca->mi.discard = opt_get(c->opts, discard);
1159 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
1161 percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
1162 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
1163 !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) ||
1164 bch2_dev_buckets_alloc(c, ca) ||
1165 bioset_init(&ca->replica_set, 4,
1166 offsetof(struct bch_write_bio, bio), 0) ||
1167 !(ca->io_done = alloc_percpu(*ca->io_done)))
1176 static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca,
1179 ca->dev_idx = dev_idx;
1180 __set_bit(ca->dev_idx, ca->self.d);
1181 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
1184 rcu_assign_pointer(c->devs[ca->dev_idx], ca);
1186 if (bch2_dev_sysfs_online(c, ca))
1187 pr_warn("error creating sysfs objects");
1190 static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
1192 struct bch_member *member =
1193 bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx;
1194 struct bch_dev *ca = NULL;
1197 pr_verbose_init(c->opts, "");
1199 if (bch2_fs_init_fault("dev_alloc"))
1202 ca = __bch2_dev_alloc(c, member);
1208 if (ca->mi.state == BCH_MEMBER_STATE_rw &&
1209 bch2_dev_allocator_start(ca)) {
1214 bch2_dev_attach(c, ca, dev_idx);
1216 pr_verbose_init(c->opts, "ret %i", ret);
1225 static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
1229 if (bch2_dev_is_online(ca)) {
1230 bch_err(ca, "already have device online in slot %u",
1235 if (get_capacity(sb->bdev->bd_disk) <
1236 ca->mi.bucket_size * ca->mi.nbuckets) {
1237 bch_err(ca, "cannot online: device too small");
1241 BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
1243 if (get_capacity(sb->bdev->bd_disk) <
1244 ca->mi.bucket_size * ca->mi.nbuckets) {
1245 bch_err(ca, "device too small");
1249 ret = bch2_dev_journal_init(ca, sb->sb);
1255 if (sb->mode & FMODE_EXCL)
1256 ca->disk_sb.bdev->bd_holder = ca;
1257 memset(sb, 0, sizeof(*sb));
1259 ca->dev = ca->disk_sb.bdev->bd_dev;
1261 percpu_ref_reinit(&ca->io_ref);
1266 static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb)
1271 lockdep_assert_held(&c->state_lock);
1273 if (le64_to_cpu(sb->sb->seq) >
1274 le64_to_cpu(c->disk_sb.sb->seq))
1275 bch2_sb_to_fs(c, sb->sb);
1277 BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
1278 !c->devs[sb->sb->dev_idx]);
1280 ca = bch_dev_locked(c, sb->sb->dev_idx);
1282 ret = __bch2_dev_attach_bdev(ca, sb);
1286 bch2_dev_sysfs_online(c, ca);
1288 if (c->sb.nr_devices == 1)
1289 bdevname(ca->disk_sb.bdev, c->name);
1290 bdevname(ca->disk_sb.bdev, ca->name);
1292 rebalance_wakeup(c);
1296 /* Device management: */
1299 * Note: this function is also used by the error paths - when a particular
1300 * device sees an error, we call it to determine whether we can just set the
1301 * device RO, or - if this function returns false - we'll set the whole
1304 * XXX: maybe we should be more explicit about whether we're changing state
1305 * because we got an error or what have you?
1307 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
1308 enum bch_member_state new_state, int flags)
1310 struct bch_devs_mask new_online_devs;
1311 struct bch_dev *ca2;
1312 int i, nr_rw = 0, required;
1314 lockdep_assert_held(&c->state_lock);
1316 switch (new_state) {
1317 case BCH_MEMBER_STATE_rw:
1319 case BCH_MEMBER_STATE_ro:
1320 if (ca->mi.state != BCH_MEMBER_STATE_rw)
1323 /* do we have enough devices to write to? */
1324 for_each_member_device(ca2, c, i)
1326 nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw;
1328 required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
1329 ? c->opts.metadata_replicas
1330 : c->opts.metadata_replicas_required,
1331 !(flags & BCH_FORCE_IF_DATA_DEGRADED)
1332 ? c->opts.data_replicas
1333 : c->opts.data_replicas_required);
1335 return nr_rw >= required;
1336 case BCH_MEMBER_STATE_failed:
1337 case BCH_MEMBER_STATE_spare:
1338 if (ca->mi.state != BCH_MEMBER_STATE_rw &&
1339 ca->mi.state != BCH_MEMBER_STATE_ro)
1342 /* do we have enough devices to read from? */
1343 new_online_devs = bch2_online_devs(c);
1344 __clear_bit(ca->dev_idx, new_online_devs.d);
1346 return bch2_have_enough_devs(c, new_online_devs, flags, false);
1352 static bool bch2_fs_may_start(struct bch_fs *c)
1354 struct bch_sb_field_members *mi;
1356 unsigned i, flags = 0;
1358 if (c->opts.very_degraded)
1359 flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST;
1361 if (c->opts.degraded)
1362 flags |= BCH_FORCE_IF_DEGRADED;
1364 if (!c->opts.degraded &&
1365 !c->opts.very_degraded) {
1366 mutex_lock(&c->sb_lock);
1367 mi = bch2_sb_get_members(c->disk_sb.sb);
1369 for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
1370 if (!bch2_dev_exists(c->disk_sb.sb, mi, i))
1373 ca = bch_dev_locked(c, i);
1375 if (!bch2_dev_is_online(ca) &&
1376 (ca->mi.state == BCH_MEMBER_STATE_rw ||
1377 ca->mi.state == BCH_MEMBER_STATE_ro)) {
1378 mutex_unlock(&c->sb_lock);
1382 mutex_unlock(&c->sb_lock);
1385 return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true);
1388 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
1391 * Device going read only means the copygc reserve get smaller, so we
1392 * don't want that happening while copygc is in progress:
1394 bch2_copygc_stop(c);
1397 * The allocator thread itself allocates btree nodes, so stop it first:
1399 bch2_dev_allocator_stop(ca);
1400 bch2_dev_allocator_remove(c, ca);
1401 bch2_dev_journal_stop(&c->journal, ca);
1403 bch2_copygc_start(c);
1406 static int __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
1408 lockdep_assert_held(&c->state_lock);
1410 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw);
1412 bch2_dev_allocator_add(c, ca);
1413 bch2_recalc_capacity(c);
1415 return bch2_dev_allocator_start(ca);
1418 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1419 enum bch_member_state new_state, int flags)
1421 struct bch_sb_field_members *mi;
1424 if (ca->mi.state == new_state)
1427 if (!bch2_dev_state_allowed(c, ca, new_state, flags))
1430 if (new_state != BCH_MEMBER_STATE_rw)
1431 __bch2_dev_read_only(c, ca);
1433 bch_notice(ca, "%s", bch2_member_states[new_state]);
1435 mutex_lock(&c->sb_lock);
1436 mi = bch2_sb_get_members(c->disk_sb.sb);
1437 SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
1438 bch2_write_super(c);
1439 mutex_unlock(&c->sb_lock);
1441 if (new_state == BCH_MEMBER_STATE_rw)
1442 ret = __bch2_dev_read_write(c, ca);
1444 rebalance_wakeup(c);
1449 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1450 enum bch_member_state new_state, int flags)
1454 down_write(&c->state_lock);
1455 ret = __bch2_dev_set_state(c, ca, new_state, flags);
1456 up_write(&c->state_lock);
1461 /* Device add/removal: */
1463 static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca)
1465 struct btree_trans trans;
1469 bch2_trans_init(&trans, c, 0, 0);
1471 for (i = 0; i < ca->mi.nbuckets; i++) {
1472 ret = lockrestart_do(&trans,
1473 bch2_btree_key_cache_flush(&trans,
1474 BTREE_ID_alloc, POS(ca->dev_idx, i)));
1478 bch2_trans_exit(&trans);
1481 bch_err(c, "error %i removing dev alloc info", ret);
1485 return bch2_btree_delete_range(c, BTREE_ID_alloc,
1486 POS(ca->dev_idx, 0),
1487 POS(ca->dev_idx + 1, 0),
1491 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
1493 struct bch_sb_field_members *mi;
1494 unsigned dev_idx = ca->dev_idx, data;
1497 down_write(&c->state_lock);
1500 * We consume a reference to ca->ref, regardless of whether we succeed
1503 percpu_ref_put(&ca->ref);
1505 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
1506 bch_err(ca, "Cannot remove without losing data");
1510 __bch2_dev_read_only(c, ca);
1512 ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
1514 bch_err(ca, "Remove failed: error %i dropping data", ret);
1518 ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx);
1520 bch_err(ca, "Remove failed: error %i flushing journal", ret);
1524 ret = bch2_dev_remove_alloc(c, ca);
1526 bch_err(ca, "Remove failed, error deleting alloc info");
1531 * must flush all existing journal entries, they might have
1532 * (overwritten) keys that point to the device we're removing:
1534 bch2_journal_flush_all_pins(&c->journal);
1536 * hack to ensure bch2_replicas_gc2() clears out entries to this device
1538 bch2_journal_meta(&c->journal);
1539 ret = bch2_journal_error(&c->journal);
1541 bch_err(ca, "Remove failed, journal error");
1545 ret = bch2_replicas_gc2(c);
1547 bch_err(ca, "Remove failed: error %i from replicas gc", ret);
1551 data = bch2_dev_has_data(c, ca);
1553 char data_has_str[100];
1555 bch2_flags_to_text(&PBUF(data_has_str),
1556 bch2_data_types, data);
1557 bch_err(ca, "Remove failed, still has data (%s)", data_has_str);
1562 __bch2_dev_offline(c, ca);
1564 mutex_lock(&c->sb_lock);
1565 rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
1566 mutex_unlock(&c->sb_lock);
1568 percpu_ref_kill(&ca->ref);
1569 wait_for_completion(&ca->ref_completion);
1574 * Free this device's slot in the bch_member array - all pointers to
1575 * this device must be gone:
1577 mutex_lock(&c->sb_lock);
1578 mi = bch2_sb_get_members(c->disk_sb.sb);
1579 memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
1581 bch2_write_super(c);
1583 mutex_unlock(&c->sb_lock);
1584 up_write(&c->state_lock);
1586 bch2_dev_usage_journal_reserve(c);
1589 if (ca->mi.state == BCH_MEMBER_STATE_rw &&
1590 !percpu_ref_is_zero(&ca->io_ref))
1591 __bch2_dev_read_write(c, ca);
1592 up_write(&c->state_lock);
1596 /* Add new device to running filesystem: */
1597 int bch2_dev_add(struct bch_fs *c, const char *path)
1599 struct bch_opts opts = bch2_opts_empty();
1600 struct bch_sb_handle sb;
1602 struct bch_dev *ca = NULL;
1603 struct bch_sb_field_members *mi;
1604 struct bch_member dev_mi;
1605 unsigned dev_idx, nr_devices, u64s;
1607 struct printbuf errbuf;
1610 _errbuf = kmalloc(4096, GFP_KERNEL);
1614 errbuf = _PBUF(_errbuf, 4096);
1616 ret = bch2_read_super(path, &opts, &sb);
1618 bch_err(c, "device add error: error reading super: %i", ret);
1622 dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx];
1624 err = bch2_dev_may_add(sb.sb, c);
1626 bch_err(c, "device add error: %s", err);
1631 ca = __bch2_dev_alloc(c, &dev_mi);
1633 bch2_free_super(&sb);
1638 ret = __bch2_dev_attach_bdev(ca, &sb);
1644 ret = bch2_dev_journal_alloc(ca);
1646 bch_err(c, "device add error: journal alloc failed");
1650 down_write(&c->state_lock);
1651 mutex_lock(&c->sb_lock);
1653 ret = bch2_sb_from_fs(c, ca);
1655 bch_err(c, "device add error: new device superblock too small");
1659 mi = bch2_sb_get_members(ca->disk_sb.sb);
1661 if (!bch2_sb_resize_members(&ca->disk_sb,
1662 le32_to_cpu(mi->field.u64s) +
1663 sizeof(dev_mi) / sizeof(u64))) {
1664 bch_err(c, "device add error: new device superblock too small");
1669 if (dynamic_fault("bcachefs:add:no_slot"))
1672 mi = bch2_sb_get_members(c->disk_sb.sb);
1673 for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
1674 if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx))
1677 bch_err(c, "device add error: already have maximum number of devices");
1682 nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
1683 u64s = (sizeof(struct bch_sb_field_members) +
1684 sizeof(struct bch_member) * nr_devices) / sizeof(u64);
1686 mi = bch2_sb_resize_members(&c->disk_sb, u64s);
1688 bch_err(c, "device add error: no room in superblock for member info");
1695 mi->members[dev_idx] = dev_mi;
1696 mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds());
1697 c->disk_sb.sb->nr_devices = nr_devices;
1699 ca->disk_sb.sb->dev_idx = dev_idx;
1700 bch2_dev_attach(c, ca, dev_idx);
1702 bch2_write_super(c);
1703 mutex_unlock(&c->sb_lock);
1705 bch2_dev_usage_journal_reserve(c);
1707 ret = bch2_trans_mark_dev_sb(c, ca);
1709 bch_err(c, "device add error: error marking new superblock: %i", ret);
1713 ca->new_fs_bucket_idx = 0;
1715 if (ca->mi.state == BCH_MEMBER_STATE_rw) {
1716 ret = __bch2_dev_read_write(c, ca);
1718 bch_err(c, "device add error: error going RW on new device: %i", ret);
1723 up_write(&c->state_lock);
1727 mutex_unlock(&c->sb_lock);
1728 up_write(&c->state_lock);
1732 bch2_free_super(&sb);
1736 up_write(&c->state_lock);
1741 /* Hot add existing device to running filesystem: */
1742 int bch2_dev_online(struct bch_fs *c, const char *path)
1744 struct bch_opts opts = bch2_opts_empty();
1745 struct bch_sb_handle sb = { NULL };
1746 struct bch_sb_field_members *mi;
1752 down_write(&c->state_lock);
1754 ret = bch2_read_super(path, &opts, &sb);
1756 up_write(&c->state_lock);
1760 dev_idx = sb.sb->dev_idx;
1762 err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
1764 bch_err(c, "error bringing %s online: %s", path, err);
1768 ret = bch2_dev_attach_bdev(c, &sb);
1772 ca = bch_dev_locked(c, dev_idx);
1774 ret = bch2_trans_mark_dev_sb(c, ca);
1776 bch_err(c, "error bringing %s online: error %i from bch2_trans_mark_dev_sb",
1781 if (ca->mi.state == BCH_MEMBER_STATE_rw) {
1782 ret = __bch2_dev_read_write(c, ca);
1787 mutex_lock(&c->sb_lock);
1788 mi = bch2_sb_get_members(c->disk_sb.sb);
1790 mi->members[ca->dev_idx].last_mount =
1791 cpu_to_le64(ktime_get_real_seconds());
1793 bch2_write_super(c);
1794 mutex_unlock(&c->sb_lock);
1796 up_write(&c->state_lock);
1799 up_write(&c->state_lock);
1800 bch2_free_super(&sb);
1804 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
1806 down_write(&c->state_lock);
1808 if (!bch2_dev_is_online(ca)) {
1809 bch_err(ca, "Already offline");
1810 up_write(&c->state_lock);
1814 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
1815 bch_err(ca, "Cannot offline required disk");
1816 up_write(&c->state_lock);
1820 __bch2_dev_offline(c, ca);
1822 up_write(&c->state_lock);
1826 int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
1828 struct bch_member *mi;
1831 down_write(&c->state_lock);
1833 if (nbuckets < ca->mi.nbuckets) {
1834 bch_err(ca, "Cannot shrink yet");
1839 if (bch2_dev_is_online(ca) &&
1840 get_capacity(ca->disk_sb.bdev->bd_disk) <
1841 ca->mi.bucket_size * nbuckets) {
1842 bch_err(ca, "New size larger than device");
1847 ret = bch2_dev_buckets_resize(c, ca, nbuckets);
1849 bch_err(ca, "Resize error: %i", ret);
1853 ret = bch2_trans_mark_dev_sb(c, ca);
1858 mutex_lock(&c->sb_lock);
1859 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1860 mi->nbuckets = cpu_to_le64(nbuckets);
1862 bch2_write_super(c);
1863 mutex_unlock(&c->sb_lock);
1865 bch2_recalc_capacity(c);
1867 up_write(&c->state_lock);
1871 /* return with ref on ca->ref: */
1872 struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path)
1879 ret = lookup_bdev(path, &dev);
1881 return ERR_PTR(ret);
1884 for_each_member_device_rcu(ca, c, i, NULL)
1887 ca = ERR_PTR(-ENOENT);
1894 /* Filesystem open: */
1896 struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
1897 struct bch_opts opts)
1899 struct bch_sb_handle *sb = NULL;
1900 struct bch_fs *c = NULL;
1901 struct bch_sb_field_members *mi;
1902 unsigned i, best_sb = 0;
1904 char *_errbuf = NULL;
1905 struct printbuf errbuf;
1908 if (!try_module_get(THIS_MODULE))
1909 return ERR_PTR(-ENODEV);
1911 pr_verbose_init(opts, "");
1918 _errbuf = kmalloc(4096, GFP_KERNEL);
1924 errbuf = _PBUF(_errbuf, 4096);
1926 sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
1932 for (i = 0; i < nr_devices; i++) {
1933 ret = bch2_read_super(devices[i], &opts, &sb[i]);
1939 for (i = 1; i < nr_devices; i++)
1940 if (le64_to_cpu(sb[i].sb->seq) >
1941 le64_to_cpu(sb[best_sb].sb->seq))
1944 mi = bch2_sb_get_members(sb[best_sb].sb);
1947 while (i < nr_devices) {
1949 !bch2_dev_exists(sb[best_sb].sb, mi, sb[i].sb->dev_idx)) {
1950 char buf[BDEVNAME_SIZE];
1951 pr_info("%s has been removed, skipping",
1952 bdevname(sb[i].bdev, buf));
1953 bch2_free_super(&sb[i]);
1954 array_remove_item(sb, nr_devices, i);
1958 err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
1964 c = bch2_fs_alloc(sb[best_sb].sb, opts);
1970 down_write(&c->state_lock);
1971 for (i = 0; i < nr_devices; i++) {
1972 ret = bch2_dev_attach_bdev(c, &sb[i]);
1974 up_write(&c->state_lock);
1978 up_write(&c->state_lock);
1980 err = "insufficient devices";
1981 if (!bch2_fs_may_start(c))
1984 if (!c->opts.nostart) {
1985 ret = bch2_fs_start(c);
1992 module_put(THIS_MODULE);
1993 pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
1996 pr_err("bch_fs_open err opening %s: %s",
2000 if (!IS_ERR_OR_NULL(c))
2003 for (i = 0; i < nr_devices; i++)
2004 bch2_free_super(&sb[i]);
2009 /* Global interfaces/init */
2011 static void bcachefs_exit(void)
2015 bch2_chardev_exit();
2016 bch2_btree_key_cache_exit();
2018 kset_unregister(bcachefs_kset);
2021 static int __init bcachefs_init(void)
2023 bch2_bkey_pack_test();
2025 if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
2026 bch2_btree_key_cache_init() ||
2027 bch2_chardev_init() ||
2038 #define BCH_DEBUG_PARAM(name, description) \
2040 module_param_named(name, bch2_##name, bool, 0644); \
2041 MODULE_PARM_DESC(name, description);
2043 #undef BCH_DEBUG_PARAM
2045 module_exit(bcachefs_exit);
2046 module_init(bcachefs_init);