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"
46 #include <linux/backing-dev.h>
47 #include <linux/blkdev.h>
48 #include <linux/debugfs.h>
49 #include <linux/device.h>
50 #include <linux/genhd.h>
51 #include <linux/idr.h>
52 #include <linux/module.h>
53 #include <linux/percpu.h>
54 #include <linux/random.h>
55 #include <linux/sysfs.h>
56 #include <crypto/hash.h>
58 #include <trace/events/bcachefs.h>
60 MODULE_LICENSE("GPL");
61 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
64 struct kobj_type type ## _ktype = { \
65 .release = type ## _release, \
66 .sysfs_ops = &type ## _sysfs_ops, \
67 .default_attrs = type ## _files \
70 static void bch2_fs_release(struct kobject *);
71 static void bch2_dev_release(struct kobject *);
73 static void bch2_fs_internal_release(struct kobject *k)
77 static void bch2_fs_opts_dir_release(struct kobject *k)
81 static void bch2_fs_time_stats_release(struct kobject *k)
85 static KTYPE(bch2_fs);
86 static KTYPE(bch2_fs_internal);
87 static KTYPE(bch2_fs_opts_dir);
88 static KTYPE(bch2_fs_time_stats);
89 static KTYPE(bch2_dev);
91 static struct kset *bcachefs_kset;
92 static LIST_HEAD(bch_fs_list);
93 static DEFINE_MUTEX(bch_fs_list_lock);
95 static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
97 static void bch2_dev_free(struct bch_dev *);
98 static int bch2_dev_alloc(struct bch_fs *, unsigned);
99 static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *);
100 static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
102 struct bch_fs *bch2_dev_to_fs(dev_t dev)
108 mutex_lock(&bch_fs_list_lock);
111 list_for_each_entry(c, &bch_fs_list, list)
112 for_each_member_device_rcu(ca, c, i, NULL)
113 if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) {
120 mutex_unlock(&bch_fs_list_lock);
125 static struct bch_fs *__bch2_uuid_to_fs(uuid_le uuid)
129 lockdep_assert_held(&bch_fs_list_lock);
131 list_for_each_entry(c, &bch_fs_list, list)
132 if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid_le)))
138 struct bch_fs *bch2_uuid_to_fs(uuid_le uuid)
142 mutex_lock(&bch_fs_list_lock);
143 c = __bch2_uuid_to_fs(uuid);
146 mutex_unlock(&bch_fs_list_lock);
151 static void bch2_dev_usage_journal_reserve(struct bch_fs *c)
154 unsigned i, nr = 0, u64s =
155 ((sizeof(struct jset_entry_dev_usage) +
156 sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR)) /
160 for_each_member_device_rcu(ca, c, i, NULL)
164 bch2_journal_entry_res_resize(&c->journal,
165 &c->dev_usage_journal_res, u64s * nr);
168 /* Filesystem RO/RW: */
171 * For startup/shutdown of RW stuff, the dependencies are:
173 * - foreground writes depend on copygc and rebalance (to free up space)
175 * - copygc and rebalance depend on mark and sweep gc (they actually probably
176 * don't because they either reserve ahead of time or don't block if
177 * allocations fail, but allocations can require mark and sweep gc to run
178 * because of generation number wraparound)
180 * - all of the above depends on the allocator threads
182 * - allocator depends on the journal (when it rewrites prios and gens)
185 static void __bch2_fs_read_only(struct bch_fs *c)
188 unsigned i, clean_passes = 0;
190 bch2_rebalance_stop(c);
192 bch2_gc_thread_stop(c);
195 * Flush journal before stopping allocators, because flushing journal
196 * blacklist entries involves allocating new btree nodes:
198 bch2_journal_flush_all_pins(&c->journal);
201 * If the allocator threads didn't all start up, the btree updates to
202 * write out alloc info aren't going to work:
204 if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags))
207 bch_verbose(c, "flushing journal and stopping allocators");
209 bch2_journal_flush_all_pins(&c->journal);
210 set_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags);
215 if (bch2_journal_flush_all_pins(&c->journal))
219 * In flight interior btree updates will generate more journal
220 * updates and btree updates (alloc btree):
222 if (bch2_btree_interior_updates_nr_pending(c)) {
223 closure_wait_event(&c->btree_interior_update_wait,
224 !bch2_btree_interior_updates_nr_pending(c));
227 flush_work(&c->btree_interior_update_work);
229 if (bch2_journal_flush_all_pins(&c->journal))
231 } while (clean_passes < 2);
232 bch_verbose(c, "flushing journal and stopping allocators complete");
234 set_bit(BCH_FS_ALLOC_CLEAN, &c->flags);
236 closure_wait_event(&c->btree_interior_update_wait,
237 !bch2_btree_interior_updates_nr_pending(c));
238 flush_work(&c->btree_interior_update_work);
240 for_each_member_device(ca, c, i)
241 bch2_dev_allocator_stop(ca);
243 clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
244 clear_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags);
246 bch2_fs_journal_stop(&c->journal);
249 * the journal kicks off btree writes via reclaim - wait for in flight
250 * writes after stopping journal:
252 bch2_btree_flush_all_writes(c);
255 * After stopping journal:
257 for_each_member_device(ca, c, i)
258 bch2_dev_allocator_remove(c, ca);
261 static void bch2_writes_disabled(struct percpu_ref *writes)
263 struct bch_fs *c = container_of(writes, struct bch_fs, writes);
265 set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
266 wake_up(&bch_read_only_wait);
269 void bch2_fs_read_only(struct bch_fs *c)
271 if (!test_bit(BCH_FS_RW, &c->flags)) {
272 bch2_journal_reclaim_stop(&c->journal);
276 BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
279 * Block new foreground-end write operations from starting - any new
280 * writes will return -EROFS:
282 * (This is really blocking new _allocations_, writes to previously
283 * allocated space can still happen until stopping the allocator in
284 * bch2_dev_allocator_stop()).
286 percpu_ref_kill(&c->writes);
288 cancel_work_sync(&c->ec_stripe_delete_work);
291 * If we're not doing an emergency shutdown, we want to wait on
292 * outstanding writes to complete so they don't see spurious errors due
293 * to shutting down the allocator:
295 * If we are doing an emergency shutdown outstanding writes may
296 * hang until we shutdown the allocator so we don't want to wait
297 * on outstanding writes before shutting everything down - but
298 * we do need to wait on them before returning and signalling
299 * that going RO is complete:
301 wait_event(bch_read_only_wait,
302 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
303 test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
305 __bch2_fs_read_only(c);
307 wait_event(bch_read_only_wait,
308 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
310 clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
312 if (!bch2_journal_error(&c->journal) &&
313 !test_bit(BCH_FS_ERROR, &c->flags) &&
314 !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) &&
315 test_bit(BCH_FS_STARTED, &c->flags) &&
316 test_bit(BCH_FS_ALLOC_CLEAN, &c->flags) &&
317 !c->opts.norecovery) {
318 bch_verbose(c, "marking filesystem clean");
319 bch2_fs_mark_clean(c);
322 clear_bit(BCH_FS_RW, &c->flags);
325 static void bch2_fs_read_only_work(struct work_struct *work)
328 container_of(work, struct bch_fs, read_only_work);
330 down_write(&c->state_lock);
331 bch2_fs_read_only(c);
332 up_write(&c->state_lock);
335 static void bch2_fs_read_only_async(struct bch_fs *c)
337 queue_work(system_long_wq, &c->read_only_work);
340 bool bch2_fs_emergency_read_only(struct bch_fs *c)
342 bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
344 bch2_journal_halt(&c->journal);
345 bch2_fs_read_only_async(c);
347 wake_up(&bch_read_only_wait);
351 static int bch2_fs_read_write_late(struct bch_fs *c)
355 ret = bch2_gc_thread_start(c);
357 bch_err(c, "error starting gc thread");
361 ret = bch2_copygc_start(c);
363 bch_err(c, "error starting copygc thread");
367 ret = bch2_rebalance_start(c);
369 bch_err(c, "error starting rebalance thread");
373 schedule_work(&c->ec_stripe_delete_work);
378 static int __bch2_fs_read_write(struct bch_fs *c, bool early)
384 if (test_bit(BCH_FS_INITIAL_GC_UNFIXED, &c->flags)) {
385 bch_err(c, "cannot go rw, unfixed btree errors");
389 if (test_bit(BCH_FS_RW, &c->flags))
393 * nochanges is used for fsck -n mode - we have to allow going rw
394 * during recovery for that to work:
396 if (c->opts.norecovery ||
397 (c->opts.nochanges &&
398 (!early || c->opts.read_only)))
401 bch_info(c, "going read-write");
403 ret = bch2_fs_mark_dirty(c);
408 * We need to write out a journal entry before we start doing btree
409 * updates, to ensure that on unclean shutdown new journal blacklist
410 * entries are created:
412 bch2_journal_meta(&c->journal);
414 clear_bit(BCH_FS_ALLOC_CLEAN, &c->flags);
416 for_each_rw_member(ca, c, i)
417 bch2_dev_allocator_add(c, ca);
418 bch2_recalc_capacity(c);
420 for_each_rw_member(ca, c, i) {
421 ret = bch2_dev_allocator_start(ca);
423 bch_err(c, "error starting allocator threads");
424 percpu_ref_put(&ca->io_ref);
429 set_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
431 for_each_rw_member(ca, c, i)
432 bch2_wake_allocator(ca);
435 ret = bch2_fs_read_write_late(c);
440 percpu_ref_reinit(&c->writes);
441 set_bit(BCH_FS_RW, &c->flags);
442 set_bit(BCH_FS_WAS_RW, &c->flags);
445 __bch2_fs_read_only(c);
449 int bch2_fs_read_write(struct bch_fs *c)
451 return __bch2_fs_read_write(c, false);
454 int bch2_fs_read_write_early(struct bch_fs *c)
456 lockdep_assert_held(&c->state_lock);
458 return __bch2_fs_read_write(c, true);
461 /* Filesystem startup/shutdown: */
463 static void __bch2_fs_free(struct bch_fs *c)
468 for (i = 0; i < BCH_TIME_STAT_NR; i++)
469 bch2_time_stats_exit(&c->times[i]);
471 bch2_fs_quota_exit(c);
472 bch2_fs_fsio_exit(c);
474 bch2_fs_encryption_exit(c);
476 bch2_fs_btree_interior_update_exit(c);
477 bch2_fs_btree_iter_exit(c);
478 bch2_fs_btree_key_cache_exit(&c->btree_key_cache);
479 bch2_fs_btree_cache_exit(c);
480 bch2_fs_replicas_exit(c);
481 bch2_fs_journal_exit(&c->journal);
482 bch2_io_clock_exit(&c->io_clock[WRITE]);
483 bch2_io_clock_exit(&c->io_clock[READ]);
484 bch2_fs_compress_exit(c);
485 bch2_journal_keys_free(&c->journal_keys);
486 bch2_journal_entries_free(&c->journal_entries);
487 percpu_free_rwsem(&c->mark_lock);
489 if (c->btree_iters_bufs)
490 for_each_possible_cpu(cpu)
491 kfree(per_cpu_ptr(c->btree_iters_bufs, cpu)->iter);
493 free_percpu(c->online_reserved);
494 free_percpu(c->btree_iters_bufs);
495 free_percpu(c->pcpu);
496 mempool_exit(&c->large_bkey_pool);
497 mempool_exit(&c->btree_bounce_pool);
498 bioset_exit(&c->btree_bio);
499 mempool_exit(&c->fill_iter);
500 percpu_ref_exit(&c->writes);
501 kfree(rcu_dereference_protected(c->disk_groups, 1));
502 kfree(c->journal_seq_blacklist_table);
503 kfree(c->unused_inode_hints);
504 free_heap(&c->copygc_heap);
506 if (c->io_complete_wq )
507 destroy_workqueue(c->io_complete_wq );
509 destroy_workqueue(c->copygc_wq);
510 if (c->btree_error_wq)
511 destroy_workqueue(c->btree_error_wq);
512 if (c->btree_update_wq)
513 destroy_workqueue(c->btree_update_wq);
515 bch2_free_super(&c->disk_sb);
516 kvpfree(c, sizeof(*c));
517 module_put(THIS_MODULE);
520 static void bch2_fs_release(struct kobject *kobj)
522 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
527 void __bch2_fs_stop(struct bch_fs *c)
532 bch_verbose(c, "shutting down");
534 set_bit(BCH_FS_STOPPING, &c->flags);
536 cancel_work_sync(&c->journal_seq_blacklist_gc_work);
538 down_write(&c->state_lock);
539 bch2_fs_read_only(c);
540 up_write(&c->state_lock);
542 for_each_member_device(ca, c, i)
543 if (ca->kobj.state_in_sysfs &&
545 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
547 if (c->kobj.state_in_sysfs)
548 kobject_del(&c->kobj);
550 bch2_fs_debug_exit(c);
551 bch2_fs_chardev_exit(c);
553 kobject_put(&c->time_stats);
554 kobject_put(&c->opts_dir);
555 kobject_put(&c->internal);
557 /* btree prefetch might have kicked off reads in the background: */
558 bch2_btree_flush_all_reads(c);
560 for_each_member_device(ca, c, i)
561 cancel_work_sync(&ca->io_error_work);
563 cancel_work_sync(&c->btree_write_error_work);
564 cancel_work_sync(&c->read_only_work);
566 for (i = 0; i < c->sb.nr_devices; i++)
568 bch2_free_super(&c->devs[i]->disk_sb);
571 void bch2_fs_free(struct bch_fs *c)
575 mutex_lock(&bch_fs_list_lock);
577 mutex_unlock(&bch_fs_list_lock);
579 closure_sync(&c->cl);
580 closure_debug_destroy(&c->cl);
582 for (i = 0; i < c->sb.nr_devices; i++)
584 bch2_dev_free(rcu_dereference_protected(c->devs[i], 1));
586 bch_verbose(c, "shutdown complete");
588 kobject_put(&c->kobj);
591 void bch2_fs_stop(struct bch_fs *c)
597 static const char *bch2_fs_online(struct bch_fs *c)
600 const char *err = NULL;
604 lockdep_assert_held(&bch_fs_list_lock);
606 if (!list_empty(&c->list))
609 if (__bch2_uuid_to_fs(c->sb.uuid))
610 return "filesystem UUID already open";
612 ret = bch2_fs_chardev_init(c);
614 return "error creating character device";
616 bch2_fs_debug_init(c);
618 if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ||
619 kobject_add(&c->internal, &c->kobj, "internal") ||
620 kobject_add(&c->opts_dir, &c->kobj, "options") ||
621 kobject_add(&c->time_stats, &c->kobj, "time_stats") ||
622 bch2_opts_create_sysfs_files(&c->opts_dir))
623 return "error creating sysfs objects";
625 down_write(&c->state_lock);
627 err = "error creating sysfs objects";
628 for_each_member_device(ca, c, i)
629 if (bch2_dev_sysfs_online(c, ca)) {
630 percpu_ref_put(&ca->ref);
634 list_add(&c->list, &bch_fs_list);
637 up_write(&c->state_lock);
641 static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
643 struct bch_sb_field_members *mi;
645 unsigned i, iter_size;
648 pr_verbose_init(opts, "");
650 c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO);
654 __module_get(THIS_MODULE);
656 closure_init(&c->cl, NULL);
658 c->kobj.kset = bcachefs_kset;
659 kobject_init(&c->kobj, &bch2_fs_ktype);
660 kobject_init(&c->internal, &bch2_fs_internal_ktype);
661 kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype);
662 kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype);
665 c->disk_sb.fs_sb = true;
667 init_rwsem(&c->state_lock);
668 mutex_init(&c->sb_lock);
669 mutex_init(&c->replicas_gc_lock);
670 mutex_init(&c->btree_root_lock);
671 INIT_WORK(&c->read_only_work, bch2_fs_read_only_work);
673 init_rwsem(&c->gc_lock);
675 for (i = 0; i < BCH_TIME_STAT_NR; i++)
676 bch2_time_stats_init(&c->times[i]);
678 bch2_fs_copygc_init(c);
679 bch2_fs_btree_key_cache_init_early(&c->btree_key_cache);
680 bch2_fs_allocator_background_init(c);
681 bch2_fs_allocator_foreground_init(c);
682 bch2_fs_rebalance_init(c);
683 bch2_fs_quota_init(c);
685 INIT_LIST_HEAD(&c->list);
687 mutex_init(&c->usage_scratch_lock);
689 mutex_init(&c->bio_bounce_pages_lock);
691 bio_list_init(&c->btree_write_error_list);
692 spin_lock_init(&c->btree_write_error_lock);
693 INIT_WORK(&c->btree_write_error_work, bch2_btree_write_error_work);
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 spin_lock_init(&c->ec_stripes_heap_lock);
712 seqcount_init(&c->gc_pos_lock);
714 seqcount_init(&c->usage_lock);
716 sema_init(&c->io_in_flight, 64);
718 c->copy_gc_enabled = 1;
719 c->rebalance.enabled = 1;
720 c->promote_whole_extents = true;
722 c->journal.write_time = &c->times[BCH_TIME_journal_write];
723 c->journal.delay_time = &c->times[BCH_TIME_journal_delay];
724 c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal];
725 c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq];
727 bch2_fs_btree_cache_init_early(&c->btree_cache);
729 mutex_init(&c->sectors_available_lock);
731 if (percpu_init_rwsem(&c->mark_lock))
734 mutex_lock(&c->sb_lock);
736 if (bch2_sb_to_fs(c, sb)) {
737 mutex_unlock(&c->sb_lock);
741 mutex_unlock(&c->sb_lock);
743 scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid);
745 c->opts = bch2_opts_default;
746 bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb));
747 bch2_opts_apply(&c->opts, opts);
749 c->block_bits = ilog2(c->opts.block_size);
750 c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
752 if (bch2_fs_init_fault("fs_alloc"))
755 iter_size = sizeof(struct sort_iter) +
756 (btree_blocks(c) + 1) * 2 *
757 sizeof(struct sort_iter_set);
759 c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus()));
761 if (!(c->btree_update_wq = alloc_workqueue("bcachefs",
762 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
763 !(c->btree_error_wq = alloc_workqueue("bcachefs_error",
764 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
765 !(c->copygc_wq = alloc_workqueue("bcachefs_copygc",
766 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
767 !(c->io_complete_wq = alloc_workqueue("bcachefs_io",
768 WQ_FREEZABLE|WQ_HIGHPRI|WQ_MEM_RECLAIM, 1)) ||
769 percpu_ref_init(&c->writes, bch2_writes_disabled,
770 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
771 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
772 bioset_init(&c->btree_bio, 1,
773 max(offsetof(struct btree_read_bio, bio),
774 offsetof(struct btree_write_bio, wbio.bio)),
775 BIOSET_NEED_BVECS) ||
776 !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) ||
777 !(c->btree_iters_bufs = alloc_percpu(struct btree_iter_buf)) ||
778 !(c->online_reserved = alloc_percpu(u64)) ||
779 mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1,
781 mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) ||
782 !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits,
783 sizeof(u64), GFP_KERNEL)) ||
784 bch2_io_clock_init(&c->io_clock[READ]) ||
785 bch2_io_clock_init(&c->io_clock[WRITE]) ||
786 bch2_fs_journal_init(&c->journal) ||
787 bch2_fs_replicas_init(c) ||
788 bch2_fs_btree_cache_init(c) ||
789 bch2_fs_btree_key_cache_init(&c->btree_key_cache) ||
790 bch2_fs_btree_iter_init(c) ||
791 bch2_fs_btree_interior_update_init(c) ||
792 bch2_fs_io_init(c) ||
793 bch2_fs_encryption_init(c) ||
794 bch2_fs_compress_init(c) ||
795 bch2_fs_ec_init(c) ||
796 bch2_fs_fsio_init(c))
799 mi = bch2_sb_get_members(c->disk_sb.sb);
800 for (i = 0; i < c->sb.nr_devices; i++)
801 if (bch2_dev_exists(c->disk_sb.sb, mi, i) &&
802 bch2_dev_alloc(c, i))
805 bch2_journal_entry_res_resize(&c->journal,
806 &c->btree_root_journal_res,
807 BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX));
808 bch2_dev_usage_journal_reserve(c);
809 bch2_journal_entry_res_resize(&c->journal,
810 &c->clock_journal_res,
811 (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2);
813 mutex_lock(&bch_fs_list_lock);
814 err = bch2_fs_online(c);
815 mutex_unlock(&bch_fs_list_lock);
817 bch_err(c, "bch2_fs_online() error: %s", err);
821 pr_verbose_init(opts, "ret %i", c ? 0 : -ENOMEM);
830 static void print_mount_opts(struct bch_fs *c)
834 struct printbuf p = PBUF(buf);
837 strcpy(buf, "(null)");
839 if (c->opts.read_only) {
844 for (i = 0; i < bch2_opts_nr; i++) {
845 const struct bch_option *opt = &bch2_opt_table[i];
846 u64 v = bch2_opt_get_by_id(&c->opts, i);
848 if (!(opt->mode & OPT_MOUNT))
851 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
857 bch2_opt_to_text(&p, c, opt, v, OPT_SHOW_MOUNT_STYLE);
860 bch_info(c, "mounted with opts: %s", buf);
863 int bch2_fs_start(struct bch_fs *c)
865 const char *err = "cannot allocate memory";
866 struct bch_sb_field_members *mi;
868 time64_t now = ktime_get_real_seconds();
872 down_write(&c->state_lock);
874 BUG_ON(test_bit(BCH_FS_STARTED, &c->flags));
876 mutex_lock(&c->sb_lock);
878 for_each_online_member(ca, c, i)
879 bch2_sb_from_fs(c, ca);
881 mi = bch2_sb_get_members(c->disk_sb.sb);
882 for_each_online_member(ca, c, i)
883 mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
885 mutex_unlock(&c->sb_lock);
887 for_each_rw_member(ca, c, i)
888 bch2_dev_allocator_add(c, ca);
889 bch2_recalc_capacity(c);
891 ret = BCH_SB_INITIALIZED(c->disk_sb.sb)
892 ? bch2_fs_recovery(c)
893 : bch2_fs_initialize(c);
897 ret = bch2_opts_check_may_set(c);
901 err = "dynamic fault";
903 if (bch2_fs_init_fault("fs_start"))
906 set_bit(BCH_FS_STARTED, &c->flags);
909 * Allocator threads don't start filling copygc reserve until after we
910 * set BCH_FS_STARTED - wake them now:
913 * Need to set ca->allocator_state here instead of relying on the
914 * allocator threads to do it to avoid racing with the copygc threads
915 * checking it and thinking they have no alloc reserve:
917 for_each_online_member(ca, c, i) {
918 ca->allocator_state = ALLOCATOR_running;
919 bch2_wake_allocator(ca);
922 if (c->opts.read_only || c->opts.nochanges) {
923 bch2_fs_read_only(c);
925 err = "error going read write";
926 ret = !test_bit(BCH_FS_RW, &c->flags)
927 ? bch2_fs_read_write(c)
928 : bch2_fs_read_write_late(c);
936 up_write(&c->state_lock);
940 case BCH_FSCK_ERRORS_NOT_FIXED:
941 bch_err(c, "filesystem contains errors: please report this to the developers");
942 pr_cont("mount with -o fix_errors to repair\n");
945 case BCH_FSCK_REPAIR_UNIMPLEMENTED:
946 bch_err(c, "filesystem contains errors: please report this to the developers");
947 pr_cont("repair unimplemented: inform the developers so that it can be added\n");
950 case BCH_FSCK_REPAIR_IMPOSSIBLE:
951 bch_err(c, "filesystem contains errors, but repair impossible");
954 case BCH_FSCK_UNKNOWN_VERSION:
955 err = "unknown metadata version";;
958 err = "cannot allocate memory";
970 static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
972 struct bch_sb_field_members *sb_mi;
974 sb_mi = bch2_sb_get_members(sb);
976 return "Invalid superblock: member info area missing";
978 if (le16_to_cpu(sb->block_size) != c->opts.block_size)
979 return "mismatched block size";
981 if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
982 BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
983 return "new cache bucket size is too small";
988 static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
990 struct bch_sb *newest =
991 le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
992 struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
994 if (uuid_le_cmp(fs->uuid, sb->uuid))
995 return "device not a member of filesystem";
997 if (!bch2_dev_exists(newest, mi, sb->dev_idx))
998 return "device has been removed";
1000 if (fs->block_size != sb->block_size)
1001 return "mismatched block size";
1006 /* Device startup/shutdown: */
1008 static void bch2_dev_release(struct kobject *kobj)
1010 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
1015 static void bch2_dev_free(struct bch_dev *ca)
1017 bch2_dev_allocator_stop(ca);
1019 cancel_work_sync(&ca->io_error_work);
1021 if (ca->kobj.state_in_sysfs &&
1023 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
1025 if (ca->kobj.state_in_sysfs)
1026 kobject_del(&ca->kobj);
1028 bch2_free_super(&ca->disk_sb);
1029 bch2_dev_journal_exit(ca);
1031 free_percpu(ca->io_done);
1032 bioset_exit(&ca->replica_set);
1033 bch2_dev_buckets_free(ca);
1034 free_page((unsigned long) ca->sb_read_scratch);
1036 bch2_time_stats_exit(&ca->io_latency[WRITE]);
1037 bch2_time_stats_exit(&ca->io_latency[READ]);
1039 percpu_ref_exit(&ca->io_ref);
1040 percpu_ref_exit(&ca->ref);
1041 kobject_put(&ca->kobj);
1044 static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca)
1047 lockdep_assert_held(&c->state_lock);
1049 if (percpu_ref_is_zero(&ca->io_ref))
1052 __bch2_dev_read_only(c, ca);
1054 reinit_completion(&ca->io_ref_completion);
1055 percpu_ref_kill(&ca->io_ref);
1056 wait_for_completion(&ca->io_ref_completion);
1058 if (ca->kobj.state_in_sysfs) {
1059 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
1060 sysfs_remove_link(&ca->kobj, "block");
1063 bch2_free_super(&ca->disk_sb);
1064 bch2_dev_journal_exit(ca);
1067 static void bch2_dev_ref_complete(struct percpu_ref *ref)
1069 struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
1071 complete(&ca->ref_completion);
1074 static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
1076 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
1078 complete(&ca->io_ref_completion);
1081 static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca)
1085 if (!c->kobj.state_in_sysfs)
1088 if (!ca->kobj.state_in_sysfs) {
1089 ret = kobject_add(&ca->kobj, &c->kobj,
1090 "dev-%u", ca->dev_idx);
1095 if (ca->disk_sb.bdev) {
1096 struct kobject *block = bdev_kobj(ca->disk_sb.bdev);
1098 ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
1102 ret = sysfs_create_link(&ca->kobj, block, "block");
1110 static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c,
1111 struct bch_member *member)
1115 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1119 kobject_init(&ca->kobj, &bch2_dev_ktype);
1120 init_completion(&ca->ref_completion);
1121 init_completion(&ca->io_ref_completion);
1123 init_rwsem(&ca->bucket_lock);
1125 INIT_WORK(&ca->io_error_work, bch2_io_error_work);
1127 bch2_time_stats_init(&ca->io_latency[READ]);
1128 bch2_time_stats_init(&ca->io_latency[WRITE]);
1130 ca->mi = bch2_mi_to_cpu(member);
1131 ca->uuid = member->uuid;
1133 if (opt_defined(c->opts, discard))
1134 ca->mi.discard = opt_get(c->opts, discard);
1136 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
1138 percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
1139 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
1140 !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) ||
1141 bch2_dev_buckets_alloc(c, ca) ||
1142 bioset_init(&ca->replica_set, 4,
1143 offsetof(struct bch_write_bio, bio), 0) ||
1144 !(ca->io_done = alloc_percpu(*ca->io_done)))
1153 static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca,
1156 ca->dev_idx = dev_idx;
1157 __set_bit(ca->dev_idx, ca->self.d);
1158 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
1161 rcu_assign_pointer(c->devs[ca->dev_idx], ca);
1163 if (bch2_dev_sysfs_online(c, ca))
1164 pr_warn("error creating sysfs objects");
1167 static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
1169 struct bch_member *member =
1170 bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx;
1171 struct bch_dev *ca = NULL;
1174 pr_verbose_init(c->opts, "");
1176 if (bch2_fs_init_fault("dev_alloc"))
1179 ca = __bch2_dev_alloc(c, member);
1185 if (ca->mi.state == BCH_MEMBER_STATE_rw &&
1186 bch2_dev_allocator_start(ca)) {
1191 bch2_dev_attach(c, ca, dev_idx);
1193 pr_verbose_init(c->opts, "ret %i", ret);
1202 static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
1206 if (bch2_dev_is_online(ca)) {
1207 bch_err(ca, "already have device online in slot %u",
1212 if (get_capacity(sb->bdev->bd_disk) <
1213 ca->mi.bucket_size * ca->mi.nbuckets) {
1214 bch_err(ca, "cannot online: device too small");
1218 BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
1220 if (get_capacity(sb->bdev->bd_disk) <
1221 ca->mi.bucket_size * ca->mi.nbuckets) {
1222 bch_err(ca, "device too small");
1226 ret = bch2_dev_journal_init(ca, sb->sb);
1232 if (sb->mode & FMODE_EXCL)
1233 ca->disk_sb.bdev->bd_holder = ca;
1234 memset(sb, 0, sizeof(*sb));
1236 percpu_ref_reinit(&ca->io_ref);
1241 static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb)
1246 lockdep_assert_held(&c->state_lock);
1248 if (le64_to_cpu(sb->sb->seq) >
1249 le64_to_cpu(c->disk_sb.sb->seq))
1250 bch2_sb_to_fs(c, sb->sb);
1252 BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
1253 !c->devs[sb->sb->dev_idx]);
1255 ca = bch_dev_locked(c, sb->sb->dev_idx);
1257 ret = __bch2_dev_attach_bdev(ca, sb);
1261 bch2_dev_sysfs_online(c, ca);
1263 if (c->sb.nr_devices == 1)
1264 bdevname(ca->disk_sb.bdev, c->name);
1265 bdevname(ca->disk_sb.bdev, ca->name);
1267 rebalance_wakeup(c);
1271 /* Device management: */
1274 * Note: this function is also used by the error paths - when a particular
1275 * device sees an error, we call it to determine whether we can just set the
1276 * device RO, or - if this function returns false - we'll set the whole
1279 * XXX: maybe we should be more explicit about whether we're changing state
1280 * because we got an error or what have you?
1282 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
1283 enum bch_member_state new_state, int flags)
1285 struct bch_devs_mask new_online_devs;
1286 struct bch_dev *ca2;
1287 int i, nr_rw = 0, required;
1289 lockdep_assert_held(&c->state_lock);
1291 switch (new_state) {
1292 case BCH_MEMBER_STATE_rw:
1294 case BCH_MEMBER_STATE_ro:
1295 if (ca->mi.state != BCH_MEMBER_STATE_rw)
1298 /* do we have enough devices to write to? */
1299 for_each_member_device(ca2, c, i)
1301 nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw;
1303 required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
1304 ? c->opts.metadata_replicas
1305 : c->opts.metadata_replicas_required,
1306 !(flags & BCH_FORCE_IF_DATA_DEGRADED)
1307 ? c->opts.data_replicas
1308 : c->opts.data_replicas_required);
1310 return nr_rw >= required;
1311 case BCH_MEMBER_STATE_failed:
1312 case BCH_MEMBER_STATE_spare:
1313 if (ca->mi.state != BCH_MEMBER_STATE_rw &&
1314 ca->mi.state != BCH_MEMBER_STATE_ro)
1317 /* do we have enough devices to read from? */
1318 new_online_devs = bch2_online_devs(c);
1319 __clear_bit(ca->dev_idx, new_online_devs.d);
1321 return bch2_have_enough_devs(c, new_online_devs, flags, false);
1327 static bool bch2_fs_may_start(struct bch_fs *c)
1329 struct bch_sb_field_members *mi;
1331 unsigned i, flags = 0;
1333 if (c->opts.very_degraded)
1334 flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST;
1336 if (c->opts.degraded)
1337 flags |= BCH_FORCE_IF_DEGRADED;
1339 if (!c->opts.degraded &&
1340 !c->opts.very_degraded) {
1341 mutex_lock(&c->sb_lock);
1342 mi = bch2_sb_get_members(c->disk_sb.sb);
1344 for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
1345 if (!bch2_dev_exists(c->disk_sb.sb, mi, i))
1348 ca = bch_dev_locked(c, i);
1350 if (!bch2_dev_is_online(ca) &&
1351 (ca->mi.state == BCH_MEMBER_STATE_rw ||
1352 ca->mi.state == BCH_MEMBER_STATE_ro)) {
1353 mutex_unlock(&c->sb_lock);
1357 mutex_unlock(&c->sb_lock);
1360 return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true);
1363 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
1366 * Device going read only means the copygc reserve get smaller, so we
1367 * don't want that happening while copygc is in progress:
1369 bch2_copygc_stop(c);
1372 * The allocator thread itself allocates btree nodes, so stop it first:
1374 bch2_dev_allocator_stop(ca);
1375 bch2_dev_allocator_remove(c, ca);
1376 bch2_dev_journal_stop(&c->journal, ca);
1378 bch2_copygc_start(c);
1381 static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
1383 lockdep_assert_held(&c->state_lock);
1385 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw);
1387 bch2_dev_allocator_add(c, ca);
1388 bch2_recalc_capacity(c);
1390 if (bch2_dev_allocator_start(ca))
1391 return "error starting allocator thread";
1396 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1397 enum bch_member_state new_state, int flags)
1399 struct bch_sb_field_members *mi;
1402 if (ca->mi.state == new_state)
1405 if (!bch2_dev_state_allowed(c, ca, new_state, flags))
1408 if (new_state != BCH_MEMBER_STATE_rw)
1409 __bch2_dev_read_only(c, ca);
1411 bch_notice(ca, "%s", bch2_member_states[new_state]);
1413 mutex_lock(&c->sb_lock);
1414 mi = bch2_sb_get_members(c->disk_sb.sb);
1415 SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
1416 bch2_write_super(c);
1417 mutex_unlock(&c->sb_lock);
1419 if (new_state == BCH_MEMBER_STATE_rw &&
1420 __bch2_dev_read_write(c, ca))
1423 rebalance_wakeup(c);
1428 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1429 enum bch_member_state new_state, int flags)
1433 down_write(&c->state_lock);
1434 ret = __bch2_dev_set_state(c, ca, new_state, flags);
1435 up_write(&c->state_lock);
1440 /* Device add/removal: */
1442 static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca)
1444 struct btree_trans trans;
1448 bch2_trans_init(&trans, c, 0, 0);
1450 for (i = 0; i < ca->mi.nbuckets; i++) {
1451 ret = bch2_btree_key_cache_flush(&trans,
1452 BTREE_ID_alloc, POS(ca->dev_idx, i));
1456 bch2_trans_exit(&trans);
1461 return bch2_btree_delete_range(c, BTREE_ID_alloc,
1462 POS(ca->dev_idx, 0),
1463 POS(ca->dev_idx + 1, 0),
1467 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
1469 struct bch_sb_field_members *mi;
1470 unsigned dev_idx = ca->dev_idx, data;
1473 down_write(&c->state_lock);
1476 * We consume a reference to ca->ref, regardless of whether we succeed
1479 percpu_ref_put(&ca->ref);
1481 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
1482 bch_err(ca, "Cannot remove without losing data");
1486 __bch2_dev_read_only(c, ca);
1488 ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
1490 bch_err(ca, "Remove failed: error %i dropping data", ret);
1494 ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx);
1496 bch_err(ca, "Remove failed: error %i flushing journal", ret);
1500 ret = bch2_dev_remove_alloc(c, ca);
1502 bch_err(ca, "Remove failed, error deleting alloc info");
1507 * must flush all existing journal entries, they might have
1508 * (overwritten) keys that point to the device we're removing:
1510 bch2_journal_flush_all_pins(&c->journal);
1512 * hack to ensure bch2_replicas_gc2() clears out entries to this device
1514 bch2_journal_meta(&c->journal);
1515 ret = bch2_journal_error(&c->journal);
1517 bch_err(ca, "Remove failed, journal error");
1521 ret = bch2_replicas_gc2(c);
1523 bch_err(ca, "Remove failed: error %i from replicas gc", ret);
1527 data = bch2_dev_has_data(c, ca);
1529 char data_has_str[100];
1531 bch2_flags_to_text(&PBUF(data_has_str),
1532 bch2_data_types, data);
1533 bch_err(ca, "Remove failed, still has data (%s)", data_has_str);
1538 __bch2_dev_offline(c, ca);
1540 mutex_lock(&c->sb_lock);
1541 rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
1542 mutex_unlock(&c->sb_lock);
1544 percpu_ref_kill(&ca->ref);
1545 wait_for_completion(&ca->ref_completion);
1550 * Free this device's slot in the bch_member array - all pointers to
1551 * this device must be gone:
1553 mutex_lock(&c->sb_lock);
1554 mi = bch2_sb_get_members(c->disk_sb.sb);
1555 memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
1557 bch2_write_super(c);
1559 mutex_unlock(&c->sb_lock);
1560 up_write(&c->state_lock);
1562 bch2_dev_usage_journal_reserve(c);
1565 if (ca->mi.state == BCH_MEMBER_STATE_rw &&
1566 !percpu_ref_is_zero(&ca->io_ref))
1567 __bch2_dev_read_write(c, ca);
1568 up_write(&c->state_lock);
1572 /* Add new device to running filesystem: */
1573 int bch2_dev_add(struct bch_fs *c, const char *path)
1575 struct bch_opts opts = bch2_opts_empty();
1576 struct bch_sb_handle sb;
1578 struct bch_dev *ca = NULL;
1579 struct bch_sb_field_members *mi;
1580 struct bch_member dev_mi;
1581 unsigned dev_idx, nr_devices, u64s;
1584 ret = bch2_read_super(path, &opts, &sb);
1588 err = bch2_sb_validate(&sb);
1592 dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx];
1594 err = bch2_dev_may_add(sb.sb, c);
1598 ca = __bch2_dev_alloc(c, &dev_mi);
1600 bch2_free_super(&sb);
1604 ret = __bch2_dev_attach_bdev(ca, &sb);
1611 * We want to allocate journal on the new device before adding the new
1612 * device to the filesystem because allocating after we attach requires
1613 * spinning up the allocator thread, and the allocator thread requires
1614 * doing btree writes, which if the existing devices are RO isn't going
1617 * So we have to mark where the superblocks are, but marking allocated
1618 * data normally updates the filesystem usage too, so we have to mark,
1619 * allocate the journal, reset all the marks, then remark after we
1622 bch2_mark_dev_superblock(NULL, ca, 0);
1624 err = "journal alloc failed";
1625 ret = bch2_dev_journal_alloc(ca);
1629 down_write(&c->state_lock);
1630 mutex_lock(&c->sb_lock);
1632 err = "insufficient space in new superblock";
1633 ret = bch2_sb_from_fs(c, ca);
1637 mi = bch2_sb_get_members(ca->disk_sb.sb);
1639 if (!bch2_sb_resize_members(&ca->disk_sb,
1640 le32_to_cpu(mi->field.u64s) +
1641 sizeof(dev_mi) / sizeof(u64))) {
1646 if (dynamic_fault("bcachefs:add:no_slot"))
1649 mi = bch2_sb_get_members(c->disk_sb.sb);
1650 for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
1651 if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx))
1654 err = "no slots available in superblock";
1659 nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
1660 u64s = (sizeof(struct bch_sb_field_members) +
1661 sizeof(struct bch_member) * nr_devices) / sizeof(u64);
1663 err = "no space in superblock for member info";
1666 mi = bch2_sb_resize_members(&c->disk_sb, u64s);
1672 mi->members[dev_idx] = dev_mi;
1673 mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds());
1674 c->disk_sb.sb->nr_devices = nr_devices;
1676 ca->disk_sb.sb->dev_idx = dev_idx;
1677 bch2_dev_attach(c, ca, dev_idx);
1679 bch2_write_super(c);
1680 mutex_unlock(&c->sb_lock);
1682 bch2_dev_usage_journal_reserve(c);
1684 err = "error marking superblock";
1685 ret = bch2_trans_mark_dev_sb(c, ca);
1689 if (ca->mi.state == BCH_MEMBER_STATE_rw) {
1690 err = __bch2_dev_read_write(c, ca);
1695 up_write(&c->state_lock);
1699 mutex_unlock(&c->sb_lock);
1700 up_write(&c->state_lock);
1704 bch2_free_super(&sb);
1705 bch_err(c, "Unable to add device: %s", err);
1708 up_write(&c->state_lock);
1709 bch_err(c, "Error going rw after adding device: %s", err);
1713 /* Hot add existing device to running filesystem: */
1714 int bch2_dev_online(struct bch_fs *c, const char *path)
1716 struct bch_opts opts = bch2_opts_empty();
1717 struct bch_sb_handle sb = { NULL };
1718 struct bch_sb_field_members *mi;
1724 down_write(&c->state_lock);
1726 ret = bch2_read_super(path, &opts, &sb);
1728 up_write(&c->state_lock);
1732 dev_idx = sb.sb->dev_idx;
1734 err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
1738 if (bch2_dev_attach_bdev(c, &sb)) {
1739 err = "bch2_dev_attach_bdev() error";
1743 ca = bch_dev_locked(c, dev_idx);
1745 if (bch2_trans_mark_dev_sb(c, ca)) {
1746 err = "bch2_trans_mark_dev_sb() error";
1750 if (ca->mi.state == BCH_MEMBER_STATE_rw) {
1751 err = __bch2_dev_read_write(c, ca);
1756 mutex_lock(&c->sb_lock);
1757 mi = bch2_sb_get_members(c->disk_sb.sb);
1759 mi->members[ca->dev_idx].last_mount =
1760 cpu_to_le64(ktime_get_real_seconds());
1762 bch2_write_super(c);
1763 mutex_unlock(&c->sb_lock);
1765 up_write(&c->state_lock);
1768 up_write(&c->state_lock);
1769 bch2_free_super(&sb);
1770 bch_err(c, "error bringing %s online: %s", path, err);
1774 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
1776 down_write(&c->state_lock);
1778 if (!bch2_dev_is_online(ca)) {
1779 bch_err(ca, "Already offline");
1780 up_write(&c->state_lock);
1784 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
1785 bch_err(ca, "Cannot offline required disk");
1786 up_write(&c->state_lock);
1790 __bch2_dev_offline(c, ca);
1792 up_write(&c->state_lock);
1796 int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
1798 struct bch_member *mi;
1801 down_write(&c->state_lock);
1803 if (nbuckets < ca->mi.nbuckets) {
1804 bch_err(ca, "Cannot shrink yet");
1809 if (bch2_dev_is_online(ca) &&
1810 get_capacity(ca->disk_sb.bdev->bd_disk) <
1811 ca->mi.bucket_size * nbuckets) {
1812 bch_err(ca, "New size larger than device");
1817 ret = bch2_dev_buckets_resize(c, ca, nbuckets);
1819 bch_err(ca, "Resize error: %i", ret);
1823 ret = bch2_trans_mark_dev_sb(c, ca);
1828 mutex_lock(&c->sb_lock);
1829 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1830 mi->nbuckets = cpu_to_le64(nbuckets);
1832 bch2_write_super(c);
1833 mutex_unlock(&c->sb_lock);
1835 bch2_recalc_capacity(c);
1837 up_write(&c->state_lock);
1841 /* return with ref on ca->ref: */
1842 struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path)
1849 ret = lookup_bdev(path, &dev);
1851 return ERR_PTR(ret);
1854 for_each_member_device_rcu(ca, c, i, NULL)
1855 if (ca->disk_sb.bdev->bd_dev == dev)
1857 ca = ERR_PTR(-ENOENT);
1864 /* Filesystem open: */
1866 struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
1867 struct bch_opts opts)
1869 struct bch_sb_handle *sb = NULL;
1870 struct bch_fs *c = NULL;
1871 struct bch_sb_field_members *mi;
1872 unsigned i, best_sb = 0;
1876 pr_verbose_init(opts, "");
1879 c = ERR_PTR(-EINVAL);
1883 if (!try_module_get(THIS_MODULE)) {
1884 c = ERR_PTR(-ENODEV);
1888 sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
1892 for (i = 0; i < nr_devices; i++) {
1893 ret = bch2_read_super(devices[i], &opts, &sb[i]);
1897 err = bch2_sb_validate(&sb[i]);
1902 for (i = 1; i < nr_devices; i++)
1903 if (le64_to_cpu(sb[i].sb->seq) >
1904 le64_to_cpu(sb[best_sb].sb->seq))
1907 mi = bch2_sb_get_members(sb[best_sb].sb);
1910 while (i < nr_devices) {
1912 !bch2_dev_exists(sb[best_sb].sb, mi, sb[i].sb->dev_idx)) {
1913 char buf[BDEVNAME_SIZE];
1914 pr_info("%s has been removed, skipping",
1915 bdevname(sb[i].bdev, buf));
1916 bch2_free_super(&sb[i]);
1917 array_remove_item(sb, nr_devices, i);
1921 err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
1928 c = bch2_fs_alloc(sb[best_sb].sb, opts);
1932 err = "bch2_dev_online() error";
1933 down_write(&c->state_lock);
1934 for (i = 0; i < nr_devices; i++)
1935 if (bch2_dev_attach_bdev(c, &sb[i])) {
1936 up_write(&c->state_lock);
1939 up_write(&c->state_lock);
1941 err = "insufficient devices";
1942 if (!bch2_fs_may_start(c))
1945 if (!c->opts.nostart) {
1946 ret = bch2_fs_start(c);
1952 module_put(THIS_MODULE);
1954 pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
1957 pr_err("bch_fs_open err opening %s: %s",
1963 for (i = 0; i < nr_devices; i++)
1964 bch2_free_super(&sb[i]);
1969 static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb,
1970 struct bch_opts opts)
1974 bool allocated_fs = false;
1977 err = bch2_sb_validate(sb);
1981 mutex_lock(&bch_fs_list_lock);
1982 c = __bch2_uuid_to_fs(sb->sb->uuid);
1984 closure_get(&c->cl);
1986 err = bch2_dev_in_fs(c->disk_sb.sb, sb->sb);
1990 c = bch2_fs_alloc(sb->sb, opts);
1991 err = "cannot allocate memory";
1995 allocated_fs = true;
1998 err = "bch2_dev_online() error";
2000 mutex_lock(&c->sb_lock);
2001 if (bch2_dev_attach_bdev(c, sb)) {
2002 mutex_unlock(&c->sb_lock);
2005 mutex_unlock(&c->sb_lock);
2007 if (!c->opts.nostart && bch2_fs_may_start(c)) {
2008 err = "error starting filesystem";
2009 ret = bch2_fs_start(c);
2014 closure_put(&c->cl);
2015 mutex_unlock(&bch_fs_list_lock);
2019 mutex_unlock(&bch_fs_list_lock);
2024 closure_put(&c->cl);
2029 const char *bch2_fs_open_incremental(const char *path)
2031 struct bch_sb_handle sb;
2032 struct bch_opts opts = bch2_opts_empty();
2035 if (bch2_read_super(path, &opts, &sb))
2036 return "error reading superblock";
2038 err = __bch2_fs_open_incremental(&sb, opts);
2039 bch2_free_super(&sb);
2044 /* Global interfaces/init */
2046 static void bcachefs_exit(void)
2050 bch2_chardev_exit();
2051 bch2_btree_key_cache_exit();
2053 kset_unregister(bcachefs_kset);
2056 static int __init bcachefs_init(void)
2058 bch2_bkey_pack_test();
2060 if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
2061 bch2_btree_key_cache_init() ||
2062 bch2_chardev_init() ||
2073 #define BCH_DEBUG_PARAM(name, description) \
2075 module_param_named(name, bch2_##name, bool, 0644); \
2076 MODULE_PARM_DESC(name, description);
2078 #undef BCH_DEBUG_PARAM
2080 module_exit(bcachefs_exit);
2081 module_init(bcachefs_init);