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"
47 #include <linux/backing-dev.h>
48 #include <linux/blkdev.h>
49 #include <linux/debugfs.h>
50 #include <linux/device.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 MODULE_LICENSE("GPL");
59 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
62 static const struct attribute_group type ## _group = { \
63 .attrs = type ## _files \
66 static const struct attribute_group *type ## _groups[] = { \
71 static const struct kobj_type type ## _ktype = { \
72 .release = type ## _release, \
73 .sysfs_ops = &type ## _sysfs_ops, \
74 .default_groups = type ## _groups \
77 static void bch2_fs_release(struct kobject *);
78 static void bch2_dev_release(struct kobject *);
80 static void bch2_fs_internal_release(struct kobject *k)
84 static void bch2_fs_opts_dir_release(struct kobject *k)
88 static void bch2_fs_time_stats_release(struct kobject *k)
93 KTYPE(bch2_fs_internal);
94 KTYPE(bch2_fs_opts_dir);
95 KTYPE(bch2_fs_time_stats);
98 static struct kset *bcachefs_kset;
99 static LIST_HEAD(bch_fs_list);
100 static DEFINE_MUTEX(bch_fs_list_lock);
102 static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
104 static void bch2_dev_free(struct bch_dev *);
105 static int bch2_dev_alloc(struct bch_fs *, unsigned);
106 static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *);
107 static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
109 struct bch_fs *bch2_dev_to_fs(dev_t dev)
115 mutex_lock(&bch_fs_list_lock);
118 list_for_each_entry(c, &bch_fs_list, list)
119 for_each_member_device_rcu(ca, c, i, NULL)
120 if (ca->disk_sb.bdev->bd_dev == dev) {
127 mutex_unlock(&bch_fs_list_lock);
132 static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid)
136 lockdep_assert_held(&bch_fs_list_lock);
138 list_for_each_entry(c, &bch_fs_list, list)
139 if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid)))
145 struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid)
149 mutex_lock(&bch_fs_list_lock);
150 c = __bch2_uuid_to_fs(uuid);
153 mutex_unlock(&bch_fs_list_lock);
158 static void bch2_dev_usage_journal_reserve(struct bch_fs *c)
161 unsigned i, nr = 0, u64s =
162 ((sizeof(struct jset_entry_dev_usage) +
163 sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR)) /
167 for_each_member_device_rcu(ca, c, i, NULL)
171 bch2_journal_entry_res_resize(&c->journal,
172 &c->dev_usage_journal_res, u64s * nr);
175 /* Filesystem RO/RW: */
178 * For startup/shutdown of RW stuff, the dependencies are:
180 * - foreground writes depend on copygc and rebalance (to free up space)
182 * - copygc and rebalance depend on mark and sweep gc (they actually probably
183 * don't because they either reserve ahead of time or don't block if
184 * allocations fail, but allocations can require mark and sweep gc to run
185 * because of generation number wraparound)
187 * - all of the above depends on the allocator threads
189 * - allocator depends on the journal (when it rewrites prios and gens)
192 static void __bch2_fs_read_only(struct bch_fs *c)
195 unsigned i, clean_passes = 0;
197 bch2_rebalance_stop(c);
199 bch2_gc_thread_stop(c);
202 * Flush journal before stopping allocators, because flushing journal
203 * blacklist entries involves allocating new btree nodes:
205 bch2_journal_flush_all_pins(&c->journal);
208 * If the allocator threads didn't all start up, the btree updates to
209 * write out alloc info aren't going to work:
211 if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags))
214 bch_verbose(c, "flushing journal and stopping allocators");
216 bch2_journal_flush_all_pins(&c->journal);
217 set_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags);
222 if (bch2_journal_flush_all_pins(&c->journal))
226 * In flight interior btree updates will generate more journal
227 * updates and btree updates (alloc btree):
229 if (bch2_btree_interior_updates_nr_pending(c)) {
230 closure_wait_event(&c->btree_interior_update_wait,
231 !bch2_btree_interior_updates_nr_pending(c));
234 flush_work(&c->btree_interior_update_work);
236 if (bch2_journal_flush_all_pins(&c->journal))
238 } while (clean_passes < 2);
239 bch_verbose(c, "flushing journal and stopping allocators complete");
241 set_bit(BCH_FS_ALLOC_CLEAN, &c->flags);
243 closure_wait_event(&c->btree_interior_update_wait,
244 !bch2_btree_interior_updates_nr_pending(c));
245 flush_work(&c->btree_interior_update_work);
247 for_each_member_device(ca, c, i)
248 bch2_dev_allocator_stop(ca);
250 clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
251 clear_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags);
253 bch2_fs_journal_stop(&c->journal);
256 * the journal kicks off btree writes via reclaim - wait for in flight
257 * writes after stopping journal:
259 bch2_btree_flush_all_writes(c);
262 * After stopping journal:
264 for_each_member_device(ca, c, i)
265 bch2_dev_allocator_remove(c, ca);
268 static void bch2_writes_disabled(struct percpu_ref *writes)
270 struct bch_fs *c = container_of(writes, struct bch_fs, writes);
272 set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
273 wake_up(&bch_read_only_wait);
276 void bch2_fs_read_only(struct bch_fs *c)
278 if (!test_bit(BCH_FS_RW, &c->flags)) {
279 bch2_journal_reclaim_stop(&c->journal);
283 BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
286 * Block new foreground-end write operations from starting - any new
287 * writes will return -EROFS:
289 * (This is really blocking new _allocations_, writes to previously
290 * allocated space can still happen until stopping the allocator in
291 * bch2_dev_allocator_stop()).
293 percpu_ref_kill(&c->writes);
295 cancel_work_sync(&c->ec_stripe_delete_work);
298 * If we're not doing an emergency shutdown, we want to wait on
299 * outstanding writes to complete so they don't see spurious errors due
300 * to shutting down the allocator:
302 * If we are doing an emergency shutdown outstanding writes may
303 * hang until we shutdown the allocator so we don't want to wait
304 * on outstanding writes before shutting everything down - but
305 * we do need to wait on them before returning and signalling
306 * that going RO is complete:
308 wait_event(bch_read_only_wait,
309 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
310 test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
312 __bch2_fs_read_only(c);
314 wait_event(bch_read_only_wait,
315 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
317 clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
319 if (!bch2_journal_error(&c->journal) &&
320 !test_bit(BCH_FS_ERROR, &c->flags) &&
321 !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) &&
322 test_bit(BCH_FS_STARTED, &c->flags) &&
323 test_bit(BCH_FS_ALLOC_CLEAN, &c->flags) &&
324 !c->opts.norecovery) {
325 bch_verbose(c, "marking filesystem clean");
326 bch2_fs_mark_clean(c);
329 clear_bit(BCH_FS_RW, &c->flags);
332 static void bch2_fs_read_only_work(struct work_struct *work)
335 container_of(work, struct bch_fs, read_only_work);
337 down_write(&c->state_lock);
338 bch2_fs_read_only(c);
339 up_write(&c->state_lock);
342 static void bch2_fs_read_only_async(struct bch_fs *c)
344 queue_work(system_long_wq, &c->read_only_work);
347 bool bch2_fs_emergency_read_only(struct bch_fs *c)
349 bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
351 bch2_journal_halt(&c->journal);
352 bch2_fs_read_only_async(c);
354 wake_up(&bch_read_only_wait);
358 static int bch2_fs_read_write_late(struct bch_fs *c)
362 ret = bch2_gc_thread_start(c);
364 bch_err(c, "error starting gc thread");
368 ret = bch2_copygc_start(c);
370 bch_err(c, "error starting copygc thread");
374 ret = bch2_rebalance_start(c);
376 bch_err(c, "error starting rebalance thread");
380 schedule_work(&c->ec_stripe_delete_work);
385 static int __bch2_fs_read_write(struct bch_fs *c, bool early)
391 if (test_bit(BCH_FS_RW, &c->flags))
395 * nochanges is used for fsck -n mode - we have to allow going rw
396 * during recovery for that to work:
398 if (c->opts.norecovery ||
399 (c->opts.nochanges &&
400 (!early || c->opts.read_only)))
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);
444 __bch2_fs_read_only(c);
448 int bch2_fs_read_write(struct bch_fs *c)
450 return __bch2_fs_read_write(c, false);
453 int bch2_fs_read_write_early(struct bch_fs *c)
455 lockdep_assert_held(&c->state_lock);
457 return __bch2_fs_read_write(c, true);
460 /* Filesystem startup/shutdown: */
462 static void __bch2_fs_free(struct bch_fs *c)
467 for (i = 0; i < BCH_TIME_STAT_NR; i++)
468 bch2_time_stats_exit(&c->times[i]);
470 bch2_fs_quota_exit(c);
471 bch2_fs_fsio_exit(c);
473 bch2_fs_encryption_exit(c);
475 bch2_fs_btree_interior_update_exit(c);
476 bch2_fs_btree_iter_exit(c);
477 bch2_fs_btree_key_cache_exit(&c->btree_key_cache);
478 bch2_fs_btree_cache_exit(c);
479 bch2_fs_journal_exit(&c->journal);
480 bch2_io_clock_exit(&c->io_clock[WRITE]);
481 bch2_io_clock_exit(&c->io_clock[READ]);
482 bch2_fs_compress_exit(c);
483 bch2_journal_keys_free(&c->journal_keys);
484 bch2_journal_entries_free(&c->journal_entries);
485 percpu_free_rwsem(&c->mark_lock);
486 free_percpu(c->online_reserved);
487 kfree(c->usage_scratch);
488 for (i = 0; i < ARRAY_SIZE(c->usage); i++)
489 free_percpu(c->usage[i]);
490 kfree(c->usage_base);
492 if (c->btree_iters_bufs)
493 for_each_possible_cpu(cpu)
494 kfree(per_cpu_ptr(c->btree_iters_bufs, cpu)->iter);
496 free_percpu(c->btree_iters_bufs);
497 free_percpu(c->pcpu);
498 mempool_exit(&c->large_bkey_pool);
499 mempool_exit(&c->btree_bounce_pool);
500 bioset_exit(&c->btree_bio);
501 mempool_exit(&c->fill_iter);
502 percpu_ref_exit(&c->writes);
503 kfree(c->replicas.entries);
504 kfree(c->replicas_gc.entries);
505 kfree(rcu_dereference_protected(c->disk_groups, 1));
506 kfree(c->journal_seq_blacklist_table);
507 kfree(c->unused_inode_hints);
508 free_heap(&c->copygc_heap);
511 destroy_workqueue(c->copygc_wq);
513 destroy_workqueue(c->wq);
515 free_pages((unsigned long) c->disk_sb.sb,
516 c->disk_sb.page_order);
517 kvpfree(c, sizeof(*c));
518 module_put(THIS_MODULE);
521 static void bch2_fs_release(struct kobject *kobj)
523 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
528 void __bch2_fs_stop(struct bch_fs *c)
533 bch_verbose(c, "shutting down");
535 set_bit(BCH_FS_STOPPING, &c->flags);
537 cancel_work_sync(&c->journal_seq_blacklist_gc_work);
539 down_write(&c->state_lock);
540 bch2_fs_read_only(c);
541 up_write(&c->state_lock);
543 for_each_member_device(ca, c, i)
544 if (ca->kobj.state_in_sysfs &&
546 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
548 if (c->kobj.state_in_sysfs)
549 kobject_del(&c->kobj);
551 bch2_fs_debug_exit(c);
552 bch2_fs_chardev_exit(c);
554 kobject_put(&c->time_stats);
555 kobject_put(&c->opts_dir);
556 kobject_put(&c->internal);
558 /* btree prefetch might have kicked off reads in the background: */
559 bch2_btree_flush_all_reads(c);
561 for_each_member_device(ca, c, i)
562 cancel_work_sync(&ca->io_error_work);
564 cancel_work_sync(&c->btree_write_error_work);
565 cancel_work_sync(&c->read_only_work);
568 void bch2_fs_free(struct bch_fs *c)
572 mutex_lock(&bch_fs_list_lock);
574 mutex_unlock(&bch_fs_list_lock);
576 closure_sync(&c->cl);
577 closure_debug_destroy(&c->cl);
579 for (i = 0; i < c->sb.nr_devices; i++) {
580 struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true);
583 bch2_free_super(&ca->disk_sb);
588 bch_verbose(c, "shutdown complete");
590 kobject_put(&c->kobj);
593 void bch2_fs_stop(struct bch_fs *c)
599 static const char *bch2_fs_online(struct bch_fs *c)
602 const char *err = NULL;
606 lockdep_assert_held(&bch_fs_list_lock);
608 if (!list_empty(&c->list))
611 if (__bch2_uuid_to_fs(c->sb.uuid))
612 return "filesystem UUID already open";
614 ret = bch2_fs_chardev_init(c);
616 return "error creating character device";
618 bch2_fs_debug_init(c);
620 if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ||
621 kobject_add(&c->internal, &c->kobj, "internal") ||
622 kobject_add(&c->opts_dir, &c->kobj, "options") ||
623 kobject_add(&c->time_stats, &c->kobj, "time_stats") ||
624 bch2_opts_create_sysfs_files(&c->opts_dir))
625 return "error creating sysfs objects";
627 down_write(&c->state_lock);
629 err = "error creating sysfs objects";
630 __for_each_member_device(ca, c, i, NULL)
631 if (bch2_dev_sysfs_online(c, ca))
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 c->copy_gc_enabled = 1;
717 c->rebalance.enabled = 1;
718 c->promote_whole_extents = true;
720 c->journal.write_time = &c->times[BCH_TIME_journal_write];
721 c->journal.delay_time = &c->times[BCH_TIME_journal_delay];
722 c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal];
723 c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq];
725 bch2_fs_btree_cache_init_early(&c->btree_cache);
727 mutex_init(&c->sectors_available_lock);
729 if (percpu_init_rwsem(&c->mark_lock))
732 mutex_lock(&c->sb_lock);
734 if (bch2_sb_to_fs(c, sb)) {
735 mutex_unlock(&c->sb_lock);
739 mutex_unlock(&c->sb_lock);
741 scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid);
743 c->opts = bch2_opts_default;
744 bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb));
745 bch2_opts_apply(&c->opts, opts);
747 c->block_bits = ilog2(c->opts.block_size);
748 c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
750 if (bch2_fs_init_fault("fs_alloc"))
753 iter_size = sizeof(struct sort_iter) +
754 (btree_blocks(c) + 1) * 2 *
755 sizeof(struct sort_iter_set);
757 c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus()));
759 if (!(c->wq = alloc_workqueue("bcachefs",
760 WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) ||
761 !(c->copygc_wq = alloc_workqueue("bcachefs_copygc",
762 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
763 percpu_ref_init(&c->writes, bch2_writes_disabled,
764 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
765 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
766 bioset_init(&c->btree_bio, 1,
767 max(offsetof(struct btree_read_bio, bio),
768 offsetof(struct btree_write_bio, wbio.bio)),
769 BIOSET_NEED_BVECS) ||
770 !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) ||
771 !(c->online_reserved = alloc_percpu(u64)) ||
772 !(c->btree_iters_bufs = alloc_percpu(struct btree_iter_buf)) ||
773 mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1,
775 mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) ||
776 !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits,
777 sizeof(u64), GFP_KERNEL)) ||
778 bch2_io_clock_init(&c->io_clock[READ]) ||
779 bch2_io_clock_init(&c->io_clock[WRITE]) ||
780 bch2_fs_journal_init(&c->journal) ||
781 bch2_fs_replicas_init(c) ||
782 bch2_fs_btree_cache_init(c) ||
783 bch2_fs_btree_key_cache_init(&c->btree_key_cache) ||
784 bch2_fs_btree_iter_init(c) ||
785 bch2_fs_btree_interior_update_init(c) ||
786 bch2_fs_io_init(c) ||
787 bch2_fs_encryption_init(c) ||
788 bch2_fs_compress_init(c) ||
789 bch2_fs_ec_init(c) ||
790 bch2_fs_fsio_init(c))
793 mi = bch2_sb_get_members(c->disk_sb.sb);
794 for (i = 0; i < c->sb.nr_devices; i++)
795 if (bch2_dev_exists(c->disk_sb.sb, mi, i) &&
796 bch2_dev_alloc(c, i))
799 bch2_journal_entry_res_resize(&c->journal,
800 &c->btree_root_journal_res,
801 BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX));
802 bch2_dev_usage_journal_reserve(c);
803 bch2_journal_entry_res_resize(&c->journal,
804 &c->clock_journal_res,
805 (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2);
807 mutex_lock(&bch_fs_list_lock);
808 err = bch2_fs_online(c);
809 mutex_unlock(&bch_fs_list_lock);
811 bch_err(c, "bch2_fs_online() error: %s", err);
815 pr_verbose_init(opts, "ret %i", c ? 0 : -ENOMEM);
824 static void print_mount_opts(struct bch_fs *c)
828 struct printbuf p = PBUF(buf);
831 strcpy(buf, "(null)");
833 if (c->opts.read_only) {
838 for (i = 0; i < bch2_opts_nr; i++) {
839 const struct bch_option *opt = &bch2_opt_table[i];
840 u64 v = bch2_opt_get_by_id(&c->opts, i);
842 if (!(opt->mode & OPT_MOUNT))
845 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
851 bch2_opt_to_text(&p, c, opt, v, OPT_SHOW_MOUNT_STYLE);
854 bch_info(c, "mounted with opts: %s", buf);
857 int bch2_fs_start(struct bch_fs *c)
859 const char *err = "cannot allocate memory";
860 struct bch_sb_field_members *mi;
862 time64_t now = ktime_get_real_seconds();
866 down_write(&c->state_lock);
868 BUG_ON(test_bit(BCH_FS_STARTED, &c->flags));
870 mutex_lock(&c->sb_lock);
872 for_each_online_member(ca, c, i)
873 bch2_sb_from_fs(c, ca);
875 mi = bch2_sb_get_members(c->disk_sb.sb);
876 for_each_online_member(ca, c, i)
877 mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
879 mutex_unlock(&c->sb_lock);
881 for_each_rw_member(ca, c, i)
882 bch2_dev_allocator_add(c, ca);
883 bch2_recalc_capacity(c);
885 ret = BCH_SB_INITIALIZED(c->disk_sb.sb)
886 ? bch2_fs_recovery(c)
887 : bch2_fs_initialize(c);
891 ret = bch2_opts_check_may_set(c);
895 err = "dynamic fault";
897 if (bch2_fs_init_fault("fs_start"))
900 set_bit(BCH_FS_STARTED, &c->flags);
903 * Allocator threads don't start filling copygc reserve until after we
904 * set BCH_FS_STARTED - wake them now:
907 * Need to set ca->allocator_state here instead of relying on the
908 * allocator threads to do it to avoid racing with the copygc threads
909 * checking it and thinking they have no alloc reserve:
911 for_each_online_member(ca, c, i) {
912 ca->allocator_state = ALLOCATOR_running;
913 bch2_wake_allocator(ca);
916 if (c->opts.read_only || c->opts.nochanges) {
917 bch2_fs_read_only(c);
919 err = "error going read write";
920 ret = !test_bit(BCH_FS_RW, &c->flags)
921 ? bch2_fs_read_write(c)
922 : bch2_fs_read_write_late(c);
930 up_write(&c->state_lock);
934 case BCH_FSCK_ERRORS_NOT_FIXED:
935 bch_err(c, "filesystem contains errors: please report this to the developers");
936 pr_cont("mount with -o fix_errors to repair\n");
939 case BCH_FSCK_REPAIR_UNIMPLEMENTED:
940 bch_err(c, "filesystem contains errors: please report this to the developers");
941 pr_cont("repair unimplemented: inform the developers so that it can be added\n");
944 case BCH_FSCK_REPAIR_IMPOSSIBLE:
945 bch_err(c, "filesystem contains errors, but repair impossible");
948 case BCH_FSCK_UNKNOWN_VERSION:
949 err = "unknown metadata version";;
952 err = "cannot allocate memory";
964 static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
966 struct bch_sb_field_members *sb_mi;
968 sb_mi = bch2_sb_get_members(sb);
970 return "Invalid superblock: member info area missing";
972 if (le16_to_cpu(sb->block_size) != c->opts.block_size)
973 return "mismatched block size";
975 if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
976 BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
977 return "new cache bucket size is too small";
982 static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
984 struct bch_sb *newest =
985 le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
986 struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
988 if (!uuid_equal(&fs->uuid, &sb->uuid))
989 return "device not a member of filesystem";
991 if (!bch2_dev_exists(newest, mi, sb->dev_idx))
992 return "device has been removed";
994 if (fs->block_size != sb->block_size)
995 return "mismatched block size";
1000 /* Device startup/shutdown: */
1002 static void bch2_dev_release(struct kobject *kobj)
1004 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
1009 static void bch2_dev_free(struct bch_dev *ca)
1011 bch2_dev_allocator_stop(ca);
1013 cancel_work_sync(&ca->io_error_work);
1015 if (ca->kobj.state_in_sysfs &&
1017 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
1019 if (ca->kobj.state_in_sysfs)
1020 kobject_del(&ca->kobj);
1022 bch2_free_super(&ca->disk_sb);
1023 bch2_dev_journal_exit(ca);
1025 free_percpu(ca->io_done);
1026 bioset_exit(&ca->replica_set);
1027 bch2_dev_buckets_free(ca);
1028 free_page((unsigned long) ca->sb_read_scratch);
1030 bch2_time_stats_exit(&ca->io_latency[WRITE]);
1031 bch2_time_stats_exit(&ca->io_latency[READ]);
1033 percpu_ref_exit(&ca->io_ref);
1034 percpu_ref_exit(&ca->ref);
1035 kobject_put(&ca->kobj);
1038 static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca)
1041 lockdep_assert_held(&c->state_lock);
1043 if (percpu_ref_is_zero(&ca->io_ref))
1046 __bch2_dev_read_only(c, ca);
1048 reinit_completion(&ca->io_ref_completion);
1049 percpu_ref_kill(&ca->io_ref);
1050 wait_for_completion(&ca->io_ref_completion);
1052 if (ca->kobj.state_in_sysfs) {
1053 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
1054 sysfs_remove_link(&ca->kobj, "block");
1057 bch2_free_super(&ca->disk_sb);
1058 bch2_dev_journal_exit(ca);
1061 static void bch2_dev_ref_complete(struct percpu_ref *ref)
1063 struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
1065 complete(&ca->ref_completion);
1068 static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
1070 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
1072 complete(&ca->io_ref_completion);
1075 static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca)
1079 if (!c->kobj.state_in_sysfs)
1082 if (!ca->kobj.state_in_sysfs) {
1083 ret = kobject_add(&ca->kobj, &c->kobj,
1084 "dev-%u", ca->dev_idx);
1089 if (ca->disk_sb.bdev) {
1090 struct kobject *block = bdev_kobj(ca->disk_sb.bdev);
1092 ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
1096 ret = sysfs_create_link(&ca->kobj, block, "block");
1104 static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c,
1105 struct bch_member *member)
1109 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1113 kobject_init(&ca->kobj, &bch2_dev_ktype);
1114 init_completion(&ca->ref_completion);
1115 init_completion(&ca->io_ref_completion);
1117 init_rwsem(&ca->bucket_lock);
1119 INIT_WORK(&ca->io_error_work, bch2_io_error_work);
1121 bch2_time_stats_init(&ca->io_latency[READ]);
1122 bch2_time_stats_init(&ca->io_latency[WRITE]);
1124 ca->mi = bch2_mi_to_cpu(member);
1125 ca->uuid = member->uuid;
1127 if (opt_defined(c->opts, discard))
1128 ca->mi.discard = opt_get(c->opts, discard);
1130 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
1132 percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
1133 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
1134 !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) ||
1135 bch2_dev_buckets_alloc(c, ca) ||
1136 bioset_init(&ca->replica_set, 4,
1137 offsetof(struct bch_write_bio, bio), 0) ||
1138 !(ca->io_done = alloc_percpu(*ca->io_done)))
1147 static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca,
1150 ca->dev_idx = dev_idx;
1151 __set_bit(ca->dev_idx, ca->self.d);
1152 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
1155 rcu_assign_pointer(c->devs[ca->dev_idx], ca);
1157 if (bch2_dev_sysfs_online(c, ca))
1158 pr_warn("error creating sysfs objects");
1161 static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
1163 struct bch_member *member =
1164 bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx;
1165 struct bch_dev *ca = NULL;
1168 pr_verbose_init(c->opts, "");
1170 if (bch2_fs_init_fault("dev_alloc"))
1173 ca = __bch2_dev_alloc(c, member);
1179 if (ca->mi.state == BCH_MEMBER_STATE_RW &&
1180 bch2_dev_allocator_start(ca)) {
1185 bch2_dev_attach(c, ca, dev_idx);
1187 pr_verbose_init(c->opts, "ret %i", ret);
1196 static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
1200 if (bch2_dev_is_online(ca)) {
1201 bch_err(ca, "already have device online in slot %u",
1206 if (get_capacity(sb->bdev->bd_disk) <
1207 ca->mi.bucket_size * ca->mi.nbuckets) {
1208 bch_err(ca, "cannot online: device too small");
1212 BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
1214 if (get_capacity(sb->bdev->bd_disk) <
1215 ca->mi.bucket_size * ca->mi.nbuckets) {
1216 bch_err(ca, "device too small");
1220 ret = bch2_dev_journal_init(ca, sb->sb);
1226 memset(sb, 0, sizeof(*sb));
1228 percpu_ref_reinit(&ca->io_ref);
1233 static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb)
1238 lockdep_assert_held(&c->state_lock);
1240 if (le64_to_cpu(sb->sb->seq) >
1241 le64_to_cpu(c->disk_sb.sb->seq))
1242 bch2_sb_to_fs(c, sb->sb);
1244 BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
1245 !c->devs[sb->sb->dev_idx]);
1247 ca = bch_dev_locked(c, sb->sb->dev_idx);
1249 ret = __bch2_dev_attach_bdev(ca, sb);
1253 bch2_dev_sysfs_online(c, ca);
1255 if (c->sb.nr_devices == 1)
1256 snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev);
1257 snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev);
1259 rebalance_wakeup(c);
1263 /* Device management: */
1266 * Note: this function is also used by the error paths - when a particular
1267 * device sees an error, we call it to determine whether we can just set the
1268 * device RO, or - if this function returns false - we'll set the whole
1271 * XXX: maybe we should be more explicit about whether we're changing state
1272 * because we got an error or what have you?
1274 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
1275 enum bch_member_state new_state, int flags)
1277 struct bch_devs_mask new_online_devs;
1278 struct bch_dev *ca2;
1279 int i, nr_rw = 0, required;
1281 lockdep_assert_held(&c->state_lock);
1283 switch (new_state) {
1284 case BCH_MEMBER_STATE_RW:
1286 case BCH_MEMBER_STATE_RO:
1287 if (ca->mi.state != BCH_MEMBER_STATE_RW)
1290 /* do we have enough devices to write to? */
1291 for_each_member_device(ca2, c, i)
1293 nr_rw += ca2->mi.state == BCH_MEMBER_STATE_RW;
1295 required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
1296 ? c->opts.metadata_replicas
1297 : c->opts.metadata_replicas_required,
1298 !(flags & BCH_FORCE_IF_DATA_DEGRADED)
1299 ? c->opts.data_replicas
1300 : c->opts.data_replicas_required);
1302 return nr_rw >= required;
1303 case BCH_MEMBER_STATE_FAILED:
1304 case BCH_MEMBER_STATE_SPARE:
1305 if (ca->mi.state != BCH_MEMBER_STATE_RW &&
1306 ca->mi.state != BCH_MEMBER_STATE_RO)
1309 /* do we have enough devices to read from? */
1310 new_online_devs = bch2_online_devs(c);
1311 __clear_bit(ca->dev_idx, new_online_devs.d);
1313 return bch2_have_enough_devs(c, new_online_devs, flags, false);
1319 static bool bch2_fs_may_start(struct bch_fs *c)
1321 struct bch_sb_field_members *mi;
1323 unsigned i, flags = 0;
1325 if (c->opts.very_degraded)
1326 flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST;
1328 if (c->opts.degraded)
1329 flags |= BCH_FORCE_IF_DEGRADED;
1331 if (!c->opts.degraded &&
1332 !c->opts.very_degraded) {
1333 mutex_lock(&c->sb_lock);
1334 mi = bch2_sb_get_members(c->disk_sb.sb);
1336 for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
1337 if (!bch2_dev_exists(c->disk_sb.sb, mi, i))
1340 ca = bch_dev_locked(c, i);
1342 if (!bch2_dev_is_online(ca) &&
1343 (ca->mi.state == BCH_MEMBER_STATE_RW ||
1344 ca->mi.state == BCH_MEMBER_STATE_RO)) {
1345 mutex_unlock(&c->sb_lock);
1349 mutex_unlock(&c->sb_lock);
1352 return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true);
1355 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
1358 * Device going read only means the copygc reserve get smaller, so we
1359 * don't want that happening while copygc is in progress:
1361 bch2_copygc_stop(c);
1364 * The allocator thread itself allocates btree nodes, so stop it first:
1366 bch2_dev_allocator_stop(ca);
1367 bch2_dev_allocator_remove(c, ca);
1368 bch2_dev_journal_stop(&c->journal, ca);
1370 bch2_copygc_start(c);
1373 static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
1375 lockdep_assert_held(&c->state_lock);
1377 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_RW);
1379 bch2_dev_allocator_add(c, ca);
1380 bch2_recalc_capacity(c);
1382 if (bch2_dev_allocator_start(ca))
1383 return "error starting allocator thread";
1388 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1389 enum bch_member_state new_state, int flags)
1391 struct bch_sb_field_members *mi;
1394 if (ca->mi.state == new_state)
1397 if (!bch2_dev_state_allowed(c, ca, new_state, flags))
1400 if (new_state != BCH_MEMBER_STATE_RW)
1401 __bch2_dev_read_only(c, ca);
1403 bch_notice(ca, "%s", bch2_dev_state[new_state]);
1405 mutex_lock(&c->sb_lock);
1406 mi = bch2_sb_get_members(c->disk_sb.sb);
1407 SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
1408 bch2_write_super(c);
1409 mutex_unlock(&c->sb_lock);
1411 if (new_state == BCH_MEMBER_STATE_RW &&
1412 __bch2_dev_read_write(c, ca))
1415 rebalance_wakeup(c);
1420 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1421 enum bch_member_state new_state, int flags)
1425 down_write(&c->state_lock);
1426 ret = __bch2_dev_set_state(c, ca, new_state, flags);
1427 up_write(&c->state_lock);
1432 /* Device add/removal: */
1434 int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca)
1436 struct btree_trans trans;
1440 bch2_trans_init(&trans, c, 0, 0);
1442 for (i = 0; i < ca->mi.nbuckets; i++) {
1443 ret = bch2_btree_key_cache_flush(&trans,
1444 BTREE_ID_ALLOC, POS(ca->dev_idx, i));
1448 bch2_trans_exit(&trans);
1453 return bch2_btree_delete_range(c, BTREE_ID_ALLOC,
1454 POS(ca->dev_idx, 0),
1455 POS(ca->dev_idx + 1, 0),
1459 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
1461 struct bch_sb_field_members *mi;
1462 unsigned dev_idx = ca->dev_idx, data;
1465 down_write(&c->state_lock);
1468 * We consume a reference to ca->ref, regardless of whether we succeed
1471 percpu_ref_put(&ca->ref);
1473 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1474 bch_err(ca, "Cannot remove without losing data");
1478 __bch2_dev_read_only(c, ca);
1480 ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
1482 bch_err(ca, "Remove failed: error %i dropping data", ret);
1486 ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx);
1488 bch_err(ca, "Remove failed: error %i flushing journal", ret);
1492 ret = bch2_dev_remove_alloc(c, ca);
1494 bch_err(ca, "Remove failed, error deleting alloc info");
1499 * must flush all existing journal entries, they might have
1500 * (overwritten) keys that point to the device we're removing:
1502 bch2_journal_flush_all_pins(&c->journal);
1504 * hack to ensure bch2_replicas_gc2() clears out entries to this device
1506 bch2_journal_meta(&c->journal);
1507 ret = bch2_journal_error(&c->journal);
1509 bch_err(ca, "Remove failed, journal error");
1513 ret = bch2_replicas_gc2(c);
1515 bch_err(ca, "Remove failed: error %i from replicas gc", ret);
1519 data = bch2_dev_has_data(c, ca);
1521 char data_has_str[100];
1523 bch2_flags_to_text(&PBUF(data_has_str),
1524 bch2_data_types, data);
1525 bch_err(ca, "Remove failed, still has data (%s)", data_has_str);
1530 __bch2_dev_offline(c, ca);
1532 mutex_lock(&c->sb_lock);
1533 rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
1534 mutex_unlock(&c->sb_lock);
1536 percpu_ref_kill(&ca->ref);
1537 wait_for_completion(&ca->ref_completion);
1542 * Free this device's slot in the bch_member array - all pointers to
1543 * this device must be gone:
1545 mutex_lock(&c->sb_lock);
1546 mi = bch2_sb_get_members(c->disk_sb.sb);
1547 memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
1549 bch2_write_super(c);
1551 mutex_unlock(&c->sb_lock);
1552 up_write(&c->state_lock);
1554 bch2_dev_usage_journal_reserve(c);
1557 if (ca->mi.state == BCH_MEMBER_STATE_RW &&
1558 !percpu_ref_is_zero(&ca->io_ref))
1559 __bch2_dev_read_write(c, ca);
1560 up_write(&c->state_lock);
1564 /* Add new device to running filesystem: */
1565 int bch2_dev_add(struct bch_fs *c, const char *path)
1567 struct bch_opts opts = bch2_opts_empty();
1568 struct bch_sb_handle sb;
1570 struct bch_dev *ca = NULL;
1571 struct bch_sb_field_members *mi;
1572 struct bch_member dev_mi;
1573 unsigned dev_idx, nr_devices, u64s;
1576 ret = bch2_read_super(path, &opts, &sb);
1580 err = bch2_sb_validate(&sb);
1584 dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx];
1586 err = bch2_dev_may_add(sb.sb, c);
1590 ca = __bch2_dev_alloc(c, &dev_mi);
1592 bch2_free_super(&sb);
1596 ret = __bch2_dev_attach_bdev(ca, &sb);
1603 * We want to allocate journal on the new device before adding the new
1604 * device to the filesystem because allocating after we attach requires
1605 * spinning up the allocator thread, and the allocator thread requires
1606 * doing btree writes, which if the existing devices are RO isn't going
1609 * So we have to mark where the superblocks are, but marking allocated
1610 * data normally updates the filesystem usage too, so we have to mark,
1611 * allocate the journal, reset all the marks, then remark after we
1614 bch2_mark_dev_superblock(NULL, ca, 0);
1616 err = "journal alloc failed";
1617 ret = bch2_dev_journal_alloc(ca);
1621 down_write(&c->state_lock);
1622 mutex_lock(&c->sb_lock);
1624 err = "insufficient space in new superblock";
1625 ret = bch2_sb_from_fs(c, ca);
1629 mi = bch2_sb_get_members(ca->disk_sb.sb);
1631 if (!bch2_sb_resize_members(&ca->disk_sb,
1632 le32_to_cpu(mi->field.u64s) +
1633 sizeof(dev_mi) / sizeof(u64))) {
1638 if (dynamic_fault("bcachefs:add:no_slot"))
1641 mi = bch2_sb_get_members(c->disk_sb.sb);
1642 for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
1643 if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx))
1646 err = "no slots available in superblock";
1651 nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
1652 u64s = (sizeof(struct bch_sb_field_members) +
1653 sizeof(struct bch_member) * nr_devices) / sizeof(u64);
1655 err = "no space in superblock for member info";
1658 mi = bch2_sb_resize_members(&c->disk_sb, u64s);
1664 mi->members[dev_idx] = dev_mi;
1665 mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds());
1666 c->disk_sb.sb->nr_devices = nr_devices;
1668 ca->disk_sb.sb->dev_idx = dev_idx;
1669 bch2_dev_attach(c, ca, dev_idx);
1671 bch2_write_super(c);
1672 mutex_unlock(&c->sb_lock);
1674 bch2_dev_usage_journal_reserve(c);
1676 err = "error marking superblock";
1677 ret = bch2_trans_mark_dev_sb(c, NULL, ca);
1681 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1682 err = __bch2_dev_read_write(c, ca);
1687 up_write(&c->state_lock);
1691 mutex_unlock(&c->sb_lock);
1692 up_write(&c->state_lock);
1696 bch2_free_super(&sb);
1697 bch_err(c, "Unable to add device: %s", err);
1700 up_write(&c->state_lock);
1701 bch_err(c, "Error going rw after adding device: %s", err);
1705 /* Hot add existing device to running filesystem: */
1706 int bch2_dev_online(struct bch_fs *c, const char *path)
1708 struct bch_opts opts = bch2_opts_empty();
1709 struct bch_sb_handle sb = { NULL };
1710 struct bch_sb_field_members *mi;
1716 down_write(&c->state_lock);
1718 ret = bch2_read_super(path, &opts, &sb);
1720 up_write(&c->state_lock);
1724 dev_idx = sb.sb->dev_idx;
1726 err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
1730 if (bch2_dev_attach_bdev(c, &sb)) {
1731 err = "bch2_dev_attach_bdev() error";
1735 ca = bch_dev_locked(c, dev_idx);
1737 if (bch2_trans_mark_dev_sb(c, NULL, ca)) {
1738 err = "bch2_trans_mark_dev_sb() error";
1742 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1743 err = __bch2_dev_read_write(c, ca);
1748 mutex_lock(&c->sb_lock);
1749 mi = bch2_sb_get_members(c->disk_sb.sb);
1751 mi->members[ca->dev_idx].last_mount =
1752 cpu_to_le64(ktime_get_real_seconds());
1754 bch2_write_super(c);
1755 mutex_unlock(&c->sb_lock);
1757 up_write(&c->state_lock);
1760 up_write(&c->state_lock);
1761 bch2_free_super(&sb);
1762 bch_err(c, "error bringing %s online: %s", path, err);
1766 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
1768 down_write(&c->state_lock);
1770 if (!bch2_dev_is_online(ca)) {
1771 bch_err(ca, "Already offline");
1772 up_write(&c->state_lock);
1776 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1777 bch_err(ca, "Cannot offline required disk");
1778 up_write(&c->state_lock);
1782 __bch2_dev_offline(c, ca);
1784 up_write(&c->state_lock);
1788 int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
1790 struct bch_member *mi;
1793 down_write(&c->state_lock);
1795 if (nbuckets < ca->mi.nbuckets) {
1796 bch_err(ca, "Cannot shrink yet");
1801 if (bch2_dev_is_online(ca) &&
1802 get_capacity(ca->disk_sb.bdev->bd_disk) <
1803 ca->mi.bucket_size * nbuckets) {
1804 bch_err(ca, "New size larger than device");
1809 ret = bch2_dev_buckets_resize(c, ca, nbuckets);
1811 bch_err(ca, "Resize error: %i", ret);
1815 mutex_lock(&c->sb_lock);
1816 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1817 mi->nbuckets = cpu_to_le64(nbuckets);
1819 bch2_write_super(c);
1820 mutex_unlock(&c->sb_lock);
1822 bch2_recalc_capacity(c);
1824 up_write(&c->state_lock);
1828 /* return with ref on ca->ref: */
1829 struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path)
1836 ret = lookup_bdev(path, &dev);
1838 return ERR_PTR(ret);
1840 for_each_member_device(ca, c, i)
1841 if (ca->disk_sb.bdev->bd_dev == dev)
1844 ca = ERR_PTR(-ENOENT);
1849 /* Filesystem open: */
1851 struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
1852 struct bch_opts opts)
1854 struct bch_sb_handle *sb = NULL;
1855 struct bch_fs *c = NULL;
1856 struct bch_sb_field_members *mi;
1857 unsigned i, best_sb = 0;
1861 pr_verbose_init(opts, "");
1864 c = ERR_PTR(-EINVAL);
1868 if (!try_module_get(THIS_MODULE)) {
1869 c = ERR_PTR(-ENODEV);
1873 sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
1877 for (i = 0; i < nr_devices; i++) {
1878 ret = bch2_read_super(devices[i], &opts, &sb[i]);
1882 err = bch2_sb_validate(&sb[i]);
1887 for (i = 1; i < nr_devices; i++)
1888 if (le64_to_cpu(sb[i].sb->seq) >
1889 le64_to_cpu(sb[best_sb].sb->seq))
1892 mi = bch2_sb_get_members(sb[best_sb].sb);
1895 while (i < nr_devices) {
1897 !bch2_dev_exists(sb[best_sb].sb, mi, sb[i].sb->dev_idx)) {
1898 pr_info("%pg has been removed, skipping", sb[i].bdev);
1899 bch2_free_super(&sb[i]);
1900 array_remove_item(sb, nr_devices, i);
1904 err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
1911 c = bch2_fs_alloc(sb[best_sb].sb, opts);
1915 err = "bch2_dev_online() error";
1916 down_write(&c->state_lock);
1917 for (i = 0; i < nr_devices; i++)
1918 if (bch2_dev_attach_bdev(c, &sb[i])) {
1919 up_write(&c->state_lock);
1922 up_write(&c->state_lock);
1924 err = "insufficient devices";
1925 if (!bch2_fs_may_start(c))
1928 if (!c->opts.nostart) {
1929 ret = bch2_fs_start(c);
1935 module_put(THIS_MODULE);
1937 pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
1940 pr_err("bch_fs_open err opening %s: %s",
1946 for (i = 0; i < nr_devices; i++)
1947 bch2_free_super(&sb[i]);
1952 static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb,
1953 struct bch_opts opts)
1957 bool allocated_fs = false;
1960 err = bch2_sb_validate(sb);
1964 mutex_lock(&bch_fs_list_lock);
1965 c = __bch2_uuid_to_fs(sb->sb->uuid);
1967 closure_get(&c->cl);
1969 err = bch2_dev_in_fs(c->disk_sb.sb, sb->sb);
1973 c = bch2_fs_alloc(sb->sb, opts);
1974 err = "cannot allocate memory";
1978 allocated_fs = true;
1981 err = "bch2_dev_online() error";
1983 mutex_lock(&c->sb_lock);
1984 if (bch2_dev_attach_bdev(c, sb)) {
1985 mutex_unlock(&c->sb_lock);
1988 mutex_unlock(&c->sb_lock);
1990 if (!c->opts.nostart && bch2_fs_may_start(c)) {
1991 err = "error starting filesystem";
1992 ret = bch2_fs_start(c);
1997 closure_put(&c->cl);
1998 mutex_unlock(&bch_fs_list_lock);
2002 mutex_unlock(&bch_fs_list_lock);
2007 closure_put(&c->cl);
2012 const char *bch2_fs_open_incremental(const char *path)
2014 struct bch_sb_handle sb;
2015 struct bch_opts opts = bch2_opts_empty();
2018 if (bch2_read_super(path, &opts, &sb))
2019 return "error reading superblock";
2021 err = __bch2_fs_open_incremental(&sb, opts);
2022 bch2_free_super(&sb);
2027 /* Global interfaces/init */
2029 static void bcachefs_exit(void)
2033 bch2_chardev_exit();
2034 bch2_btree_key_cache_exit();
2036 kset_unregister(bcachefs_kset);
2039 static int __init bcachefs_init(void)
2041 bch2_bkey_pack_test();
2043 if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
2044 bch2_btree_key_cache_init() ||
2045 bch2_chardev_init() ||
2056 #define BCH_DEBUG_PARAM(name, description) \
2058 module_param_named(name, bch2_##name, bool, 0644); \
2059 MODULE_PARM_DESC(name, description);
2061 #undef BCH_DEBUG_PARAM
2063 unsigned bch2_metadata_version = bcachefs_metadata_version_current;
2064 module_param_named(version, bch2_metadata_version, uint, 0400);
2066 module_exit(bcachefs_exit);
2067 module_init(bcachefs_init);