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"
19 #include "buckets_waiting_for_journal.h"
25 #include "disk_groups.h"
34 #include "journal_reclaim.h"
35 #include "journal_seq_blacklist.h"
40 #include "rebalance.h"
43 #include "subvolume.h"
48 #include <linux/backing-dev.h>
49 #include <linux/blkdev.h>
50 #include <linux/debugfs.h>
51 #include <linux/device.h>
52 #include <linux/genhd.h>
53 #include <linux/idr.h>
54 #include <linux/module.h>
55 #include <linux/percpu.h>
56 #include <linux/random.h>
57 #include <linux/sysfs.h>
58 #include <crypto/hash.h>
60 #include <trace/events/bcachefs.h>
62 MODULE_LICENSE("GPL");
63 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
66 struct kobj_type type ## _ktype = { \
67 .release = type ## _release, \
68 .sysfs_ops = &type ## _sysfs_ops, \
69 .default_attrs = type ## _files \
72 static void bch2_fs_release(struct kobject *);
73 static void bch2_dev_release(struct kobject *);
75 static void bch2_fs_internal_release(struct kobject *k)
79 static void bch2_fs_opts_dir_release(struct kobject *k)
83 static void bch2_fs_time_stats_release(struct kobject *k)
87 static KTYPE(bch2_fs);
88 static KTYPE(bch2_fs_internal);
89 static KTYPE(bch2_fs_opts_dir);
90 static KTYPE(bch2_fs_time_stats);
91 static KTYPE(bch2_dev);
93 static struct kset *bcachefs_kset;
94 static LIST_HEAD(bch_fs_list);
95 static DEFINE_MUTEX(bch_fs_list_lock);
97 static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
99 static void bch2_dev_free(struct bch_dev *);
100 static int bch2_dev_alloc(struct bch_fs *, unsigned);
101 static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *);
102 static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
104 struct bch_fs *bch2_dev_to_fs(dev_t dev)
110 mutex_lock(&bch_fs_list_lock);
113 list_for_each_entry(c, &bch_fs_list, list)
114 for_each_member_device_rcu(ca, c, i, NULL)
115 if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) {
122 mutex_unlock(&bch_fs_list_lock);
127 static struct bch_fs *__bch2_uuid_to_fs(uuid_le uuid)
131 lockdep_assert_held(&bch_fs_list_lock);
133 list_for_each_entry(c, &bch_fs_list, list)
134 if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid_le)))
140 struct bch_fs *bch2_uuid_to_fs(uuid_le uuid)
144 mutex_lock(&bch_fs_list_lock);
145 c = __bch2_uuid_to_fs(uuid);
148 mutex_unlock(&bch_fs_list_lock);
153 static void bch2_dev_usage_journal_reserve(struct bch_fs *c)
156 unsigned i, nr = 0, u64s =
157 ((sizeof(struct jset_entry_dev_usage) +
158 sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR)) /
162 for_each_member_device_rcu(ca, c, i, NULL)
166 bch2_journal_entry_res_resize(&c->journal,
167 &c->dev_usage_journal_res, u64s * nr);
170 /* Filesystem RO/RW: */
173 * For startup/shutdown of RW stuff, the dependencies are:
175 * - foreground writes depend on copygc and rebalance (to free up space)
177 * - copygc and rebalance depend on mark and sweep gc (they actually probably
178 * don't because they either reserve ahead of time or don't block if
179 * allocations fail, but allocations can require mark and sweep gc to run
180 * because of generation number wraparound)
182 * - all of the above depends on the allocator threads
184 * - allocator depends on the journal (when it rewrites prios and gens)
187 static void __bch2_fs_read_only(struct bch_fs *c)
190 unsigned i, clean_passes = 0;
192 bch2_rebalance_stop(c);
194 bch2_gc_thread_stop(c);
197 * Flush journal before stopping allocators, because flushing journal
198 * blacklist entries involves allocating new btree nodes:
200 bch2_journal_flush_all_pins(&c->journal);
203 * If the allocator threads didn't all start up, the btree updates to
204 * write out alloc info aren't going to work:
206 if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags))
209 bch_verbose(c, "flushing journal and stopping allocators");
211 bch2_journal_flush_all_pins(&c->journal);
212 set_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags);
217 if (bch2_journal_flush_all_pins(&c->journal))
221 * In flight interior btree updates will generate more journal
222 * updates and btree updates (alloc btree):
224 if (bch2_btree_interior_updates_nr_pending(c)) {
225 closure_wait_event(&c->btree_interior_update_wait,
226 !bch2_btree_interior_updates_nr_pending(c));
229 flush_work(&c->btree_interior_update_work);
231 if (bch2_journal_flush_all_pins(&c->journal))
233 } while (clean_passes < 2);
234 bch_verbose(c, "flushing journal and stopping allocators complete");
236 set_bit(BCH_FS_ALLOC_CLEAN, &c->flags);
238 closure_wait_event(&c->btree_interior_update_wait,
239 !bch2_btree_interior_updates_nr_pending(c));
240 flush_work(&c->btree_interior_update_work);
242 for_each_member_device(ca, c, i)
243 bch2_dev_allocator_stop(ca);
245 clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
246 clear_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags);
248 bch2_fs_journal_stop(&c->journal);
251 * the journal kicks off btree writes via reclaim - wait for in flight
252 * writes after stopping journal:
254 bch2_btree_flush_all_writes(c);
257 * After stopping journal:
259 for_each_member_device(ca, c, i)
260 bch2_dev_allocator_remove(c, ca);
263 static void bch2_writes_disabled(struct percpu_ref *writes)
265 struct bch_fs *c = container_of(writes, struct bch_fs, writes);
267 set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
268 wake_up(&bch_read_only_wait);
271 void bch2_fs_read_only(struct bch_fs *c)
273 if (!test_bit(BCH_FS_RW, &c->flags)) {
274 bch2_journal_reclaim_stop(&c->journal);
278 BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
281 * Block new foreground-end write operations from starting - any new
282 * writes will return -EROFS:
284 * (This is really blocking new _allocations_, writes to previously
285 * allocated space can still happen until stopping the allocator in
286 * bch2_dev_allocator_stop()).
288 percpu_ref_kill(&c->writes);
290 cancel_work_sync(&c->ec_stripe_delete_work);
293 * If we're not doing an emergency shutdown, we want to wait on
294 * outstanding writes to complete so they don't see spurious errors due
295 * to shutting down the allocator:
297 * If we are doing an emergency shutdown outstanding writes may
298 * hang until we shutdown the allocator so we don't want to wait
299 * on outstanding writes before shutting everything down - but
300 * we do need to wait on them before returning and signalling
301 * that going RO is complete:
303 wait_event(bch_read_only_wait,
304 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
305 test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
307 __bch2_fs_read_only(c);
309 wait_event(bch_read_only_wait,
310 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
312 clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
314 if (!bch2_journal_error(&c->journal) &&
315 !test_bit(BCH_FS_ERROR, &c->flags) &&
316 !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) &&
317 test_bit(BCH_FS_STARTED, &c->flags) &&
318 test_bit(BCH_FS_ALLOC_CLEAN, &c->flags) &&
319 !c->opts.norecovery) {
320 bch_verbose(c, "marking filesystem clean");
321 bch2_fs_mark_clean(c);
324 clear_bit(BCH_FS_RW, &c->flags);
327 static void bch2_fs_read_only_work(struct work_struct *work)
330 container_of(work, struct bch_fs, read_only_work);
332 down_write(&c->state_lock);
333 bch2_fs_read_only(c);
334 up_write(&c->state_lock);
337 static void bch2_fs_read_only_async(struct bch_fs *c)
339 queue_work(system_long_wq, &c->read_only_work);
342 bool bch2_fs_emergency_read_only(struct bch_fs *c)
344 bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
346 bch2_journal_halt(&c->journal);
347 bch2_fs_read_only_async(c);
349 wake_up(&bch_read_only_wait);
353 static int bch2_fs_read_write_late(struct bch_fs *c)
357 ret = bch2_gc_thread_start(c);
359 bch_err(c, "error starting gc thread");
363 ret = bch2_copygc_start(c);
365 bch_err(c, "error starting copygc thread");
369 ret = bch2_rebalance_start(c);
371 bch_err(c, "error starting rebalance thread");
375 schedule_work(&c->ec_stripe_delete_work);
380 static int __bch2_fs_read_write(struct bch_fs *c, bool early)
386 if (test_bit(BCH_FS_INITIAL_GC_UNFIXED, &c->flags)) {
387 bch_err(c, "cannot go rw, unfixed btree errors");
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 bch_info(c, "going read-write");
405 ret = bch2_fs_mark_dirty(c);
409 clear_bit(BCH_FS_ALLOC_CLEAN, &c->flags);
411 for_each_rw_member(ca, c, i)
412 bch2_dev_allocator_add(c, ca);
413 bch2_recalc_capacity(c);
415 for_each_rw_member(ca, c, i) {
416 ret = bch2_dev_allocator_start(ca);
418 bch_err(c, "error starting allocator threads");
419 percpu_ref_put(&ca->io_ref);
424 set_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
426 for_each_rw_member(ca, c, i)
427 bch2_wake_allocator(ca);
430 ret = bch2_fs_read_write_late(c);
435 percpu_ref_reinit(&c->writes);
436 set_bit(BCH_FS_RW, &c->flags);
437 set_bit(BCH_FS_WAS_RW, &c->flags);
440 __bch2_fs_read_only(c);
444 int bch2_fs_read_write(struct bch_fs *c)
446 return __bch2_fs_read_write(c, false);
449 int bch2_fs_read_write_early(struct bch_fs *c)
451 lockdep_assert_held(&c->state_lock);
453 return __bch2_fs_read_write(c, true);
456 /* Filesystem startup/shutdown: */
458 static void __bch2_fs_free(struct bch_fs *c)
463 for (i = 0; i < BCH_TIME_STAT_NR; i++)
464 bch2_time_stats_exit(&c->times[i]);
466 bch2_fs_snapshots_exit(c);
467 bch2_fs_quota_exit(c);
468 bch2_fs_fsio_exit(c);
470 bch2_fs_encryption_exit(c);
472 bch2_fs_buckets_waiting_for_journal_exit(c);
473 bch2_fs_btree_interior_update_exit(c);
474 bch2_fs_btree_iter_exit(c);
475 bch2_fs_btree_key_cache_exit(&c->btree_key_cache);
476 bch2_fs_btree_cache_exit(c);
477 bch2_fs_replicas_exit(c);
478 bch2_fs_journal_exit(&c->journal);
479 bch2_io_clock_exit(&c->io_clock[WRITE]);
480 bch2_io_clock_exit(&c->io_clock[READ]);
481 bch2_fs_compress_exit(c);
482 bch2_journal_keys_free(&c->journal_keys);
483 bch2_journal_entries_free(&c->journal_entries);
484 percpu_free_rwsem(&c->mark_lock);
486 if (c->btree_paths_bufs)
487 for_each_possible_cpu(cpu)
488 kfree(per_cpu_ptr(c->btree_paths_bufs, cpu)->path);
490 free_percpu(c->online_reserved);
491 free_percpu(c->btree_paths_bufs);
492 free_percpu(c->pcpu);
493 mempool_exit(&c->large_bkey_pool);
494 mempool_exit(&c->btree_bounce_pool);
495 bioset_exit(&c->btree_bio);
496 mempool_exit(&c->fill_iter);
497 percpu_ref_exit(&c->writes);
498 kfree(rcu_dereference_protected(c->disk_groups, 1));
499 kfree(c->journal_seq_blacklist_table);
500 kfree(c->unused_inode_hints);
501 free_heap(&c->copygc_heap);
503 if (c->io_complete_wq )
504 destroy_workqueue(c->io_complete_wq );
506 destroy_workqueue(c->copygc_wq);
507 if (c->btree_io_complete_wq)
508 destroy_workqueue(c->btree_io_complete_wq);
509 if (c->btree_update_wq)
510 destroy_workqueue(c->btree_update_wq);
512 bch2_free_super(&c->disk_sb);
513 kvpfree(c, sizeof(*c));
514 module_put(THIS_MODULE);
517 static void bch2_fs_release(struct kobject *kobj)
519 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
524 void __bch2_fs_stop(struct bch_fs *c)
529 bch_verbose(c, "shutting down");
531 set_bit(BCH_FS_STOPPING, &c->flags);
533 cancel_work_sync(&c->journal_seq_blacklist_gc_work);
535 down_write(&c->state_lock);
536 bch2_fs_read_only(c);
537 up_write(&c->state_lock);
539 for_each_member_device(ca, c, i)
540 if (ca->kobj.state_in_sysfs &&
542 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
544 if (c->kobj.state_in_sysfs)
545 kobject_del(&c->kobj);
547 bch2_fs_debug_exit(c);
548 bch2_fs_chardev_exit(c);
550 kobject_put(&c->time_stats);
551 kobject_put(&c->opts_dir);
552 kobject_put(&c->internal);
554 /* btree prefetch might have kicked off reads in the background: */
555 bch2_btree_flush_all_reads(c);
557 for_each_member_device(ca, c, i)
558 cancel_work_sync(&ca->io_error_work);
560 cancel_work_sync(&c->read_only_work);
562 for (i = 0; i < c->sb.nr_devices; i++)
564 bch2_free_super(&c->devs[i]->disk_sb);
567 void bch2_fs_free(struct bch_fs *c)
571 mutex_lock(&bch_fs_list_lock);
573 mutex_unlock(&bch_fs_list_lock);
575 closure_sync(&c->cl);
576 closure_debug_destroy(&c->cl);
578 for (i = 0; i < c->sb.nr_devices; i++)
580 bch2_dev_free(rcu_dereference_protected(c->devs[i], 1));
582 bch_verbose(c, "shutdown complete");
584 kobject_put(&c->kobj);
587 void bch2_fs_stop(struct bch_fs *c)
593 static int bch2_fs_online(struct bch_fs *c)
599 lockdep_assert_held(&bch_fs_list_lock);
601 if (__bch2_uuid_to_fs(c->sb.uuid)) {
602 bch_err(c, "filesystem UUID already open");
606 ret = bch2_fs_chardev_init(c);
608 bch_err(c, "error creating character device");
612 bch2_fs_debug_init(c);
614 ret = kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ?:
615 kobject_add(&c->internal, &c->kobj, "internal") ?:
616 kobject_add(&c->opts_dir, &c->kobj, "options") ?:
617 kobject_add(&c->time_stats, &c->kobj, "time_stats") ?:
618 bch2_opts_create_sysfs_files(&c->opts_dir);
620 bch_err(c, "error creating sysfs objects");
624 down_write(&c->state_lock);
626 for_each_member_device(ca, c, i) {
627 ret = bch2_dev_sysfs_online(c, ca);
629 bch_err(c, "error creating sysfs objects");
630 percpu_ref_put(&ca->ref);
635 BUG_ON(!list_empty(&c->list));
636 list_add(&c->list, &bch_fs_list);
638 up_write(&c->state_lock);
642 static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
644 struct bch_sb_field_members *mi;
646 unsigned i, iter_size;
649 pr_verbose_init(opts, "");
651 c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO);
653 c = ERR_PTR(-ENOMEM);
657 __module_get(THIS_MODULE);
659 closure_init(&c->cl, NULL);
661 c->kobj.kset = bcachefs_kset;
662 kobject_init(&c->kobj, &bch2_fs_ktype);
663 kobject_init(&c->internal, &bch2_fs_internal_ktype);
664 kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype);
665 kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype);
668 c->disk_sb.fs_sb = true;
670 init_rwsem(&c->state_lock);
671 mutex_init(&c->sb_lock);
672 mutex_init(&c->replicas_gc_lock);
673 mutex_init(&c->btree_root_lock);
674 INIT_WORK(&c->read_only_work, bch2_fs_read_only_work);
676 init_rwsem(&c->gc_lock);
677 mutex_init(&c->gc_gens_lock);
679 for (i = 0; i < BCH_TIME_STAT_NR; i++)
680 bch2_time_stats_init(&c->times[i]);
682 bch2_fs_copygc_init(c);
683 bch2_fs_btree_key_cache_init_early(&c->btree_key_cache);
684 bch2_fs_allocator_background_init(c);
685 bch2_fs_allocator_foreground_init(c);
686 bch2_fs_rebalance_init(c);
687 bch2_fs_quota_init(c);
689 INIT_LIST_HEAD(&c->list);
691 mutex_init(&c->usage_scratch_lock);
693 mutex_init(&c->bio_bounce_pages_lock);
694 mutex_init(&c->snapshot_table_lock);
696 spin_lock_init(&c->btree_write_error_lock);
698 INIT_WORK(&c->journal_seq_blacklist_gc_work,
699 bch2_blacklist_entries_gc);
701 INIT_LIST_HEAD(&c->journal_entries);
702 INIT_LIST_HEAD(&c->journal_iters);
704 INIT_LIST_HEAD(&c->fsck_errors);
705 mutex_init(&c->fsck_error_lock);
707 INIT_LIST_HEAD(&c->ec_stripe_head_list);
708 mutex_init(&c->ec_stripe_head_lock);
710 INIT_LIST_HEAD(&c->ec_stripe_new_list);
711 mutex_init(&c->ec_stripe_new_lock);
713 INIT_LIST_HEAD(&c->data_progress_list);
714 mutex_init(&c->data_progress_lock);
716 spin_lock_init(&c->ec_stripes_heap_lock);
718 seqcount_init(&c->gc_pos_lock);
720 seqcount_init(&c->usage_lock);
722 sema_init(&c->io_in_flight, 64);
724 c->copy_gc_enabled = 1;
725 c->rebalance.enabled = 1;
726 c->promote_whole_extents = true;
728 c->journal.flush_write_time = &c->times[BCH_TIME_journal_flush_write];
729 c->journal.noflush_write_time = &c->times[BCH_TIME_journal_noflush_write];
730 c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal];
731 c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq];
733 bch2_fs_btree_cache_init_early(&c->btree_cache);
735 mutex_init(&c->sectors_available_lock);
737 ret = percpu_init_rwsem(&c->mark_lock);
741 mutex_lock(&c->sb_lock);
742 ret = bch2_sb_to_fs(c, sb);
743 mutex_unlock(&c->sb_lock);
748 uuid_unparse_lower(c->sb.user_uuid.b, c->name);
751 if (sb->version <= bcachefs_metadata_version_inode_v2 &&
752 !BCH_SB_JOURNAL_FLUSH_DELAY(sb))
753 SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000);
755 if (sb->version <= bcachefs_metadata_version_inode_v2 &&
756 !BCH_SB_JOURNAL_RECLAIM_DELAY(sb))
757 SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 100);
759 c->opts = bch2_opts_default;
760 ret = bch2_opts_from_sb(&c->opts, sb);
764 bch2_opts_apply(&c->opts, opts);
766 /* key cache currently disabled for inodes, because of snapshots: */
767 c->opts.inodes_use_key_cache = 0;
769 c->btree_key_cache_btrees |= 1U << BTREE_ID_alloc;
770 if (c->opts.inodes_use_key_cache)
771 c->btree_key_cache_btrees |= 1U << BTREE_ID_inodes;
773 c->block_bits = ilog2(block_sectors(c));
774 c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
776 if (bch2_fs_init_fault("fs_alloc")) {
777 bch_err(c, "fs_alloc fault injected");
782 iter_size = sizeof(struct sort_iter) +
783 (btree_blocks(c) + 1) * 2 *
784 sizeof(struct sort_iter_set);
786 c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus()));
788 if (!(c->btree_update_wq = alloc_workqueue("bcachefs",
789 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
790 !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io",
791 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
792 !(c->copygc_wq = alloc_workqueue("bcachefs_copygc",
793 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
794 !(c->io_complete_wq = alloc_workqueue("bcachefs_io",
795 WQ_FREEZABLE|WQ_HIGHPRI|WQ_MEM_RECLAIM, 1)) ||
796 percpu_ref_init(&c->writes, bch2_writes_disabled,
797 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
798 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
799 bioset_init(&c->btree_bio, 1,
800 max(offsetof(struct btree_read_bio, bio),
801 offsetof(struct btree_write_bio, wbio.bio)),
802 BIOSET_NEED_BVECS) ||
803 !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) ||
804 !(c->btree_paths_bufs = alloc_percpu(struct btree_path_buf)) ||
805 !(c->online_reserved = alloc_percpu(u64)) ||
806 mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1,
808 mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) ||
809 !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits,
810 sizeof(u64), GFP_KERNEL))) {
815 ret = bch2_io_clock_init(&c->io_clock[READ]) ?:
816 bch2_io_clock_init(&c->io_clock[WRITE]) ?:
817 bch2_fs_journal_init(&c->journal) ?:
818 bch2_fs_replicas_init(c) ?:
819 bch2_fs_btree_cache_init(c) ?:
820 bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?:
821 bch2_fs_btree_iter_init(c) ?:
822 bch2_fs_btree_interior_update_init(c) ?:
823 bch2_fs_buckets_waiting_for_journal_init(c);
824 bch2_fs_subvolumes_init(c) ?:
825 bch2_fs_io_init(c) ?:
826 bch2_fs_encryption_init(c) ?:
827 bch2_fs_compress_init(c) ?:
828 bch2_fs_ec_init(c) ?:
829 bch2_fs_fsio_init(c);
833 if (c->opts.nochanges)
834 set_bit(JOURNAL_NOCHANGES, &c->journal.flags);
836 mi = bch2_sb_get_members(c->disk_sb.sb);
837 for (i = 0; i < c->sb.nr_devices; i++)
838 if (bch2_dev_exists(c->disk_sb.sb, mi, i) &&
839 bch2_dev_alloc(c, i)) {
844 bch2_journal_entry_res_resize(&c->journal,
845 &c->btree_root_journal_res,
846 BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX));
847 bch2_dev_usage_journal_reserve(c);
848 bch2_journal_entry_res_resize(&c->journal,
849 &c->clock_journal_res,
850 (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2);
852 mutex_lock(&bch_fs_list_lock);
853 ret = bch2_fs_online(c);
854 mutex_unlock(&bch_fs_list_lock);
859 pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
868 static void print_mount_opts(struct bch_fs *c)
872 struct printbuf p = PBUF(buf);
875 strcpy(buf, "(null)");
877 if (c->opts.read_only) {
882 for (i = 0; i < bch2_opts_nr; i++) {
883 const struct bch_option *opt = &bch2_opt_table[i];
884 u64 v = bch2_opt_get_by_id(&c->opts, i);
886 if (!(opt->flags & OPT_MOUNT))
889 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
895 bch2_opt_to_text(&p, c, opt, v, OPT_SHOW_MOUNT_STYLE);
898 bch_info(c, "mounted with opts: %s", buf);
901 int bch2_fs_start(struct bch_fs *c)
903 struct bch_sb_field_members *mi;
905 time64_t now = ktime_get_real_seconds();
909 down_write(&c->state_lock);
911 BUG_ON(test_bit(BCH_FS_STARTED, &c->flags));
913 mutex_lock(&c->sb_lock);
915 for_each_online_member(ca, c, i)
916 bch2_sb_from_fs(c, ca);
918 mi = bch2_sb_get_members(c->disk_sb.sb);
919 for_each_online_member(ca, c, i)
920 mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
922 mutex_unlock(&c->sb_lock);
924 for_each_rw_member(ca, c, i)
925 bch2_dev_allocator_add(c, ca);
926 bch2_recalc_capacity(c);
928 ret = BCH_SB_INITIALIZED(c->disk_sb.sb)
929 ? bch2_fs_recovery(c)
930 : bch2_fs_initialize(c);
934 ret = bch2_opts_check_may_set(c);
939 if (bch2_fs_init_fault("fs_start")) {
940 bch_err(c, "fs_start fault injected");
944 set_bit(BCH_FS_STARTED, &c->flags);
947 * Allocator threads don't start filling copygc reserve until after we
948 * set BCH_FS_STARTED - wake them now:
951 * Need to set ca->allocator_state here instead of relying on the
952 * allocator threads to do it to avoid racing with the copygc threads
953 * checking it and thinking they have no alloc reserve:
955 for_each_online_member(ca, c, i) {
956 ca->allocator_state = ALLOCATOR_running;
957 bch2_wake_allocator(ca);
960 if (c->opts.read_only || c->opts.nochanges) {
961 bch2_fs_read_only(c);
963 ret = !test_bit(BCH_FS_RW, &c->flags)
964 ? bch2_fs_read_write(c)
965 : bch2_fs_read_write_late(c);
973 up_write(&c->state_lock);
977 case BCH_FSCK_ERRORS_NOT_FIXED:
978 bch_err(c, "filesystem contains errors: please report this to the developers");
979 pr_cont("mount with -o fix_errors to repair\n");
981 case BCH_FSCK_REPAIR_UNIMPLEMENTED:
982 bch_err(c, "filesystem contains errors: please report this to the developers");
983 pr_cont("repair unimplemented: inform the developers so that it can be added\n");
985 case BCH_FSCK_REPAIR_IMPOSSIBLE:
986 bch_err(c, "filesystem contains errors, but repair impossible");
988 case BCH_FSCK_UNKNOWN_VERSION:
989 bch_err(c, "unknown metadata version");
992 bch_err(c, "cannot allocate memory");
995 bch_err(c, "IO error");
1004 static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
1006 struct bch_sb_field_members *sb_mi;
1008 sb_mi = bch2_sb_get_members(sb);
1010 return "Invalid superblock: member info area missing";
1012 if (le16_to_cpu(sb->block_size) != block_sectors(c))
1013 return "mismatched block size";
1015 if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
1016 BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
1017 return "new cache bucket size is too small";
1022 static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
1024 struct bch_sb *newest =
1025 le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
1026 struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
1028 if (uuid_le_cmp(fs->uuid, sb->uuid))
1029 return "device not a member of filesystem";
1031 if (!bch2_dev_exists(newest, mi, sb->dev_idx))
1032 return "device has been removed";
1034 if (fs->block_size != sb->block_size)
1035 return "mismatched block size";
1040 /* Device startup/shutdown: */
1042 static void bch2_dev_release(struct kobject *kobj)
1044 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
1049 static void bch2_dev_free(struct bch_dev *ca)
1051 bch2_dev_allocator_stop(ca);
1053 cancel_work_sync(&ca->io_error_work);
1055 if (ca->kobj.state_in_sysfs &&
1057 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
1059 if (ca->kobj.state_in_sysfs)
1060 kobject_del(&ca->kobj);
1062 bch2_free_super(&ca->disk_sb);
1063 bch2_dev_journal_exit(ca);
1065 free_percpu(ca->io_done);
1066 bioset_exit(&ca->replica_set);
1067 bch2_dev_buckets_free(ca);
1068 free_page((unsigned long) ca->sb_read_scratch);
1070 bch2_time_stats_exit(&ca->io_latency[WRITE]);
1071 bch2_time_stats_exit(&ca->io_latency[READ]);
1073 percpu_ref_exit(&ca->io_ref);
1074 percpu_ref_exit(&ca->ref);
1075 kobject_put(&ca->kobj);
1078 static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca)
1081 lockdep_assert_held(&c->state_lock);
1083 if (percpu_ref_is_zero(&ca->io_ref))
1086 __bch2_dev_read_only(c, ca);
1088 reinit_completion(&ca->io_ref_completion);
1089 percpu_ref_kill(&ca->io_ref);
1090 wait_for_completion(&ca->io_ref_completion);
1092 if (ca->kobj.state_in_sysfs) {
1093 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
1094 sysfs_remove_link(&ca->kobj, "block");
1097 bch2_free_super(&ca->disk_sb);
1098 bch2_dev_journal_exit(ca);
1101 static void bch2_dev_ref_complete(struct percpu_ref *ref)
1103 struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
1105 complete(&ca->ref_completion);
1108 static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
1110 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
1112 complete(&ca->io_ref_completion);
1115 static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca)
1119 if (!c->kobj.state_in_sysfs)
1122 if (!ca->kobj.state_in_sysfs) {
1123 ret = kobject_add(&ca->kobj, &c->kobj,
1124 "dev-%u", ca->dev_idx);
1129 if (ca->disk_sb.bdev) {
1130 struct kobject *block = bdev_kobj(ca->disk_sb.bdev);
1132 ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
1136 ret = sysfs_create_link(&ca->kobj, block, "block");
1144 static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c,
1145 struct bch_member *member)
1149 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1153 kobject_init(&ca->kobj, &bch2_dev_ktype);
1154 init_completion(&ca->ref_completion);
1155 init_completion(&ca->io_ref_completion);
1157 init_rwsem(&ca->bucket_lock);
1159 INIT_WORK(&ca->io_error_work, bch2_io_error_work);
1161 bch2_time_stats_init(&ca->io_latency[READ]);
1162 bch2_time_stats_init(&ca->io_latency[WRITE]);
1164 ca->mi = bch2_mi_to_cpu(member);
1165 ca->uuid = member->uuid;
1167 if (opt_defined(c->opts, discard))
1168 ca->mi.discard = opt_get(c->opts, discard);
1170 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
1172 percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
1173 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
1174 !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) ||
1175 bch2_dev_buckets_alloc(c, ca) ||
1176 bioset_init(&ca->replica_set, 4,
1177 offsetof(struct bch_write_bio, bio), 0) ||
1178 !(ca->io_done = alloc_percpu(*ca->io_done)))
1187 static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca,
1190 ca->dev_idx = dev_idx;
1191 __set_bit(ca->dev_idx, ca->self.d);
1192 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
1195 rcu_assign_pointer(c->devs[ca->dev_idx], ca);
1197 if (bch2_dev_sysfs_online(c, ca))
1198 pr_warn("error creating sysfs objects");
1201 static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
1203 struct bch_member *member =
1204 bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx;
1205 struct bch_dev *ca = NULL;
1208 pr_verbose_init(c->opts, "");
1210 if (bch2_fs_init_fault("dev_alloc"))
1213 ca = __bch2_dev_alloc(c, member);
1219 if (ca->mi.state == BCH_MEMBER_STATE_rw &&
1220 bch2_dev_allocator_start(ca)) {
1225 bch2_dev_attach(c, ca, dev_idx);
1227 pr_verbose_init(c->opts, "ret %i", ret);
1236 static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
1240 if (bch2_dev_is_online(ca)) {
1241 bch_err(ca, "already have device online in slot %u",
1246 if (get_capacity(sb->bdev->bd_disk) <
1247 ca->mi.bucket_size * ca->mi.nbuckets) {
1248 bch_err(ca, "cannot online: device too small");
1252 BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
1254 if (get_capacity(sb->bdev->bd_disk) <
1255 ca->mi.bucket_size * ca->mi.nbuckets) {
1256 bch_err(ca, "device too small");
1260 ret = bch2_dev_journal_init(ca, sb->sb);
1266 if (sb->mode & FMODE_EXCL)
1267 ca->disk_sb.bdev->bd_holder = ca;
1268 memset(sb, 0, sizeof(*sb));
1270 ca->dev = ca->disk_sb.bdev->bd_dev;
1272 percpu_ref_reinit(&ca->io_ref);
1277 static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb)
1282 lockdep_assert_held(&c->state_lock);
1284 if (le64_to_cpu(sb->sb->seq) >
1285 le64_to_cpu(c->disk_sb.sb->seq))
1286 bch2_sb_to_fs(c, sb->sb);
1288 BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
1289 !c->devs[sb->sb->dev_idx]);
1291 ca = bch_dev_locked(c, sb->sb->dev_idx);
1293 ret = __bch2_dev_attach_bdev(ca, sb);
1297 bch2_dev_sysfs_online(c, ca);
1299 if (c->sb.nr_devices == 1)
1300 bdevname(ca->disk_sb.bdev, c->name);
1301 bdevname(ca->disk_sb.bdev, ca->name);
1303 rebalance_wakeup(c);
1307 /* Device management: */
1310 * Note: this function is also used by the error paths - when a particular
1311 * device sees an error, we call it to determine whether we can just set the
1312 * device RO, or - if this function returns false - we'll set the whole
1315 * XXX: maybe we should be more explicit about whether we're changing state
1316 * because we got an error or what have you?
1318 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
1319 enum bch_member_state new_state, int flags)
1321 struct bch_devs_mask new_online_devs;
1322 struct bch_dev *ca2;
1323 int i, nr_rw = 0, required;
1325 lockdep_assert_held(&c->state_lock);
1327 switch (new_state) {
1328 case BCH_MEMBER_STATE_rw:
1330 case BCH_MEMBER_STATE_ro:
1331 if (ca->mi.state != BCH_MEMBER_STATE_rw)
1334 /* do we have enough devices to write to? */
1335 for_each_member_device(ca2, c, i)
1337 nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw;
1339 required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
1340 ? c->opts.metadata_replicas
1341 : c->opts.metadata_replicas_required,
1342 !(flags & BCH_FORCE_IF_DATA_DEGRADED)
1343 ? c->opts.data_replicas
1344 : c->opts.data_replicas_required);
1346 return nr_rw >= required;
1347 case BCH_MEMBER_STATE_failed:
1348 case BCH_MEMBER_STATE_spare:
1349 if (ca->mi.state != BCH_MEMBER_STATE_rw &&
1350 ca->mi.state != BCH_MEMBER_STATE_ro)
1353 /* do we have enough devices to read from? */
1354 new_online_devs = bch2_online_devs(c);
1355 __clear_bit(ca->dev_idx, new_online_devs.d);
1357 return bch2_have_enough_devs(c, new_online_devs, flags, false);
1363 static bool bch2_fs_may_start(struct bch_fs *c)
1365 struct bch_sb_field_members *mi;
1367 unsigned i, flags = 0;
1369 if (c->opts.very_degraded)
1370 flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST;
1372 if (c->opts.degraded)
1373 flags |= BCH_FORCE_IF_DEGRADED;
1375 if (!c->opts.degraded &&
1376 !c->opts.very_degraded) {
1377 mutex_lock(&c->sb_lock);
1378 mi = bch2_sb_get_members(c->disk_sb.sb);
1380 for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
1381 if (!bch2_dev_exists(c->disk_sb.sb, mi, i))
1384 ca = bch_dev_locked(c, i);
1386 if (!bch2_dev_is_online(ca) &&
1387 (ca->mi.state == BCH_MEMBER_STATE_rw ||
1388 ca->mi.state == BCH_MEMBER_STATE_ro)) {
1389 mutex_unlock(&c->sb_lock);
1393 mutex_unlock(&c->sb_lock);
1396 return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true);
1399 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
1402 * Device going read only means the copygc reserve get smaller, so we
1403 * don't want that happening while copygc is in progress:
1405 bch2_copygc_stop(c);
1408 * The allocator thread itself allocates btree nodes, so stop it first:
1410 bch2_dev_allocator_stop(ca);
1411 bch2_dev_allocator_remove(c, ca);
1412 bch2_dev_journal_stop(&c->journal, ca);
1414 bch2_copygc_start(c);
1417 static int __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
1419 lockdep_assert_held(&c->state_lock);
1421 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw);
1423 bch2_dev_allocator_add(c, ca);
1424 bch2_recalc_capacity(c);
1426 return bch2_dev_allocator_start(ca);
1429 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1430 enum bch_member_state new_state, int flags)
1432 struct bch_sb_field_members *mi;
1435 if (ca->mi.state == new_state)
1438 if (!bch2_dev_state_allowed(c, ca, new_state, flags))
1441 if (new_state != BCH_MEMBER_STATE_rw)
1442 __bch2_dev_read_only(c, ca);
1444 bch_notice(ca, "%s", bch2_member_states[new_state]);
1446 mutex_lock(&c->sb_lock);
1447 mi = bch2_sb_get_members(c->disk_sb.sb);
1448 SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
1449 bch2_write_super(c);
1450 mutex_unlock(&c->sb_lock);
1452 if (new_state == BCH_MEMBER_STATE_rw)
1453 ret = __bch2_dev_read_write(c, ca);
1455 rebalance_wakeup(c);
1460 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1461 enum bch_member_state new_state, int flags)
1465 down_write(&c->state_lock);
1466 ret = __bch2_dev_set_state(c, ca, new_state, flags);
1467 up_write(&c->state_lock);
1472 /* Device add/removal: */
1474 static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca)
1476 struct btree_trans trans;
1480 bch2_trans_init(&trans, c, 0, 0);
1482 for (i = 0; i < ca->mi.nbuckets; i++) {
1483 ret = lockrestart_do(&trans,
1484 bch2_btree_key_cache_flush(&trans,
1485 BTREE_ID_alloc, POS(ca->dev_idx, i)));
1489 bch2_trans_exit(&trans);
1492 bch_err(c, "error %i removing dev alloc info", ret);
1496 return bch2_btree_delete_range(c, BTREE_ID_alloc,
1497 POS(ca->dev_idx, 0),
1498 POS(ca->dev_idx + 1, 0),
1502 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
1504 struct bch_sb_field_members *mi;
1505 unsigned dev_idx = ca->dev_idx, data;
1508 down_write(&c->state_lock);
1511 * We consume a reference to ca->ref, regardless of whether we succeed
1514 percpu_ref_put(&ca->ref);
1516 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
1517 bch_err(ca, "Cannot remove without losing data");
1521 __bch2_dev_read_only(c, ca);
1523 ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
1525 bch_err(ca, "Remove failed: error %i dropping data", ret);
1529 ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx);
1531 bch_err(ca, "Remove failed: error %i flushing journal", ret);
1535 ret = bch2_dev_remove_alloc(c, ca);
1537 bch_err(ca, "Remove failed, error deleting alloc info");
1542 * must flush all existing journal entries, they might have
1543 * (overwritten) keys that point to the device we're removing:
1545 bch2_journal_flush_all_pins(&c->journal);
1547 * hack to ensure bch2_replicas_gc2() clears out entries to this device
1549 bch2_journal_meta(&c->journal);
1550 ret = bch2_journal_error(&c->journal);
1552 bch_err(ca, "Remove failed, journal error");
1556 ret = bch2_replicas_gc2(c);
1558 bch_err(ca, "Remove failed: error %i from replicas gc", ret);
1562 data = bch2_dev_has_data(c, ca);
1564 char data_has_str[100];
1566 bch2_flags_to_text(&PBUF(data_has_str),
1567 bch2_data_types, data);
1568 bch_err(ca, "Remove failed, still has data (%s)", data_has_str);
1573 __bch2_dev_offline(c, ca);
1575 mutex_lock(&c->sb_lock);
1576 rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
1577 mutex_unlock(&c->sb_lock);
1579 percpu_ref_kill(&ca->ref);
1580 wait_for_completion(&ca->ref_completion);
1585 * Free this device's slot in the bch_member array - all pointers to
1586 * this device must be gone:
1588 mutex_lock(&c->sb_lock);
1589 mi = bch2_sb_get_members(c->disk_sb.sb);
1590 memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
1592 bch2_write_super(c);
1594 mutex_unlock(&c->sb_lock);
1595 up_write(&c->state_lock);
1597 bch2_dev_usage_journal_reserve(c);
1600 if (ca->mi.state == BCH_MEMBER_STATE_rw &&
1601 !percpu_ref_is_zero(&ca->io_ref))
1602 __bch2_dev_read_write(c, ca);
1603 up_write(&c->state_lock);
1607 /* Add new device to running filesystem: */
1608 int bch2_dev_add(struct bch_fs *c, const char *path)
1610 struct bch_opts opts = bch2_opts_empty();
1611 struct bch_sb_handle sb;
1613 struct bch_dev *ca = NULL;
1614 struct bch_sb_field_members *mi;
1615 struct bch_member dev_mi;
1616 unsigned dev_idx, nr_devices, u64s;
1618 struct printbuf errbuf;
1621 _errbuf = kmalloc(4096, GFP_KERNEL);
1625 errbuf = _PBUF(_errbuf, 4096);
1627 ret = bch2_read_super(path, &opts, &sb);
1629 bch_err(c, "device add error: error reading super: %i", ret);
1633 dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx];
1635 err = bch2_dev_may_add(sb.sb, c);
1637 bch_err(c, "device add error: %s", err);
1642 ca = __bch2_dev_alloc(c, &dev_mi);
1644 bch2_free_super(&sb);
1649 ret = __bch2_dev_attach_bdev(ca, &sb);
1655 ret = bch2_dev_journal_alloc(ca);
1657 bch_err(c, "device add error: journal alloc failed");
1661 down_write(&c->state_lock);
1662 mutex_lock(&c->sb_lock);
1664 ret = bch2_sb_from_fs(c, ca);
1666 bch_err(c, "device add error: new device superblock too small");
1670 mi = bch2_sb_get_members(ca->disk_sb.sb);
1672 if (!bch2_sb_resize_members(&ca->disk_sb,
1673 le32_to_cpu(mi->field.u64s) +
1674 sizeof(dev_mi) / sizeof(u64))) {
1675 bch_err(c, "device add error: new device superblock too small");
1680 if (dynamic_fault("bcachefs:add:no_slot"))
1683 mi = bch2_sb_get_members(c->disk_sb.sb);
1684 for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
1685 if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx))
1688 bch_err(c, "device add error: already have maximum number of devices");
1693 nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
1694 u64s = (sizeof(struct bch_sb_field_members) +
1695 sizeof(struct bch_member) * nr_devices) / sizeof(u64);
1697 mi = bch2_sb_resize_members(&c->disk_sb, u64s);
1699 bch_err(c, "device add error: no room in superblock for member info");
1706 mi->members[dev_idx] = dev_mi;
1707 mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds());
1708 c->disk_sb.sb->nr_devices = nr_devices;
1710 ca->disk_sb.sb->dev_idx = dev_idx;
1711 bch2_dev_attach(c, ca, dev_idx);
1713 bch2_write_super(c);
1714 mutex_unlock(&c->sb_lock);
1716 bch2_dev_usage_journal_reserve(c);
1718 ret = bch2_trans_mark_dev_sb(c, ca);
1720 bch_err(c, "device add error: error marking new superblock: %i", ret);
1724 ca->new_fs_bucket_idx = 0;
1726 if (ca->mi.state == BCH_MEMBER_STATE_rw) {
1727 ret = __bch2_dev_read_write(c, ca);
1729 bch_err(c, "device add error: error going RW on new device: %i", ret);
1734 up_write(&c->state_lock);
1738 mutex_unlock(&c->sb_lock);
1739 up_write(&c->state_lock);
1743 bch2_free_super(&sb);
1747 up_write(&c->state_lock);
1752 /* Hot add existing device to running filesystem: */
1753 int bch2_dev_online(struct bch_fs *c, const char *path)
1755 struct bch_opts opts = bch2_opts_empty();
1756 struct bch_sb_handle sb = { NULL };
1757 struct bch_sb_field_members *mi;
1763 down_write(&c->state_lock);
1765 ret = bch2_read_super(path, &opts, &sb);
1767 up_write(&c->state_lock);
1771 dev_idx = sb.sb->dev_idx;
1773 err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
1775 bch_err(c, "error bringing %s online: %s", path, err);
1779 ret = bch2_dev_attach_bdev(c, &sb);
1783 ca = bch_dev_locked(c, dev_idx);
1785 ret = bch2_trans_mark_dev_sb(c, ca);
1787 bch_err(c, "error bringing %s online: error %i from bch2_trans_mark_dev_sb",
1792 if (ca->mi.state == BCH_MEMBER_STATE_rw) {
1793 ret = __bch2_dev_read_write(c, ca);
1798 mutex_lock(&c->sb_lock);
1799 mi = bch2_sb_get_members(c->disk_sb.sb);
1801 mi->members[ca->dev_idx].last_mount =
1802 cpu_to_le64(ktime_get_real_seconds());
1804 bch2_write_super(c);
1805 mutex_unlock(&c->sb_lock);
1807 up_write(&c->state_lock);
1810 up_write(&c->state_lock);
1811 bch2_free_super(&sb);
1815 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
1817 down_write(&c->state_lock);
1819 if (!bch2_dev_is_online(ca)) {
1820 bch_err(ca, "Already offline");
1821 up_write(&c->state_lock);
1825 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
1826 bch_err(ca, "Cannot offline required disk");
1827 up_write(&c->state_lock);
1831 __bch2_dev_offline(c, ca);
1833 up_write(&c->state_lock);
1837 int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
1839 struct bch_member *mi;
1842 down_write(&c->state_lock);
1844 if (nbuckets < ca->mi.nbuckets) {
1845 bch_err(ca, "Cannot shrink yet");
1850 if (bch2_dev_is_online(ca) &&
1851 get_capacity(ca->disk_sb.bdev->bd_disk) <
1852 ca->mi.bucket_size * nbuckets) {
1853 bch_err(ca, "New size larger than device");
1858 ret = bch2_dev_buckets_resize(c, ca, nbuckets);
1860 bch_err(ca, "Resize error: %i", ret);
1864 ret = bch2_trans_mark_dev_sb(c, ca);
1869 mutex_lock(&c->sb_lock);
1870 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1871 mi->nbuckets = cpu_to_le64(nbuckets);
1873 bch2_write_super(c);
1874 mutex_unlock(&c->sb_lock);
1876 bch2_recalc_capacity(c);
1878 up_write(&c->state_lock);
1882 /* return with ref on ca->ref: */
1883 struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name)
1889 for_each_member_device_rcu(ca, c, i, NULL)
1890 if (!strcmp(name, ca->name))
1892 ca = ERR_PTR(-ENOENT);
1899 /* Filesystem open: */
1901 struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
1902 struct bch_opts opts)
1904 struct bch_sb_handle *sb = NULL;
1905 struct bch_fs *c = NULL;
1906 struct bch_sb_field_members *mi;
1907 unsigned i, best_sb = 0;
1909 char *_errbuf = NULL;
1910 struct printbuf errbuf;
1913 if (!try_module_get(THIS_MODULE))
1914 return ERR_PTR(-ENODEV);
1916 pr_verbose_init(opts, "");
1923 _errbuf = kmalloc(4096, GFP_KERNEL);
1929 errbuf = _PBUF(_errbuf, 4096);
1931 sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
1937 for (i = 0; i < nr_devices; i++) {
1938 ret = bch2_read_super(devices[i], &opts, &sb[i]);
1944 for (i = 1; i < nr_devices; i++)
1945 if (le64_to_cpu(sb[i].sb->seq) >
1946 le64_to_cpu(sb[best_sb].sb->seq))
1949 mi = bch2_sb_get_members(sb[best_sb].sb);
1952 while (i < nr_devices) {
1954 !bch2_dev_exists(sb[best_sb].sb, mi, sb[i].sb->dev_idx)) {
1955 char buf[BDEVNAME_SIZE];
1956 pr_info("%s has been removed, skipping",
1957 bdevname(sb[i].bdev, buf));
1958 bch2_free_super(&sb[i]);
1959 array_remove_item(sb, nr_devices, i);
1963 err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
1969 c = bch2_fs_alloc(sb[best_sb].sb, opts);
1975 down_write(&c->state_lock);
1976 for (i = 0; i < nr_devices; i++) {
1977 ret = bch2_dev_attach_bdev(c, &sb[i]);
1979 up_write(&c->state_lock);
1983 up_write(&c->state_lock);
1985 err = "insufficient devices";
1986 if (!bch2_fs_may_start(c))
1989 if (!c->opts.nostart) {
1990 ret = bch2_fs_start(c);
1997 module_put(THIS_MODULE);
1998 pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
2001 pr_err("bch_fs_open err opening %s: %s",
2005 if (!IS_ERR_OR_NULL(c))
2008 for (i = 0; i < nr_devices; i++)
2009 bch2_free_super(&sb[i]);
2014 /* Global interfaces/init */
2016 static void bcachefs_exit(void)
2020 bch2_chardev_exit();
2021 bch2_btree_key_cache_exit();
2023 kset_unregister(bcachefs_kset);
2026 static int __init bcachefs_init(void)
2028 bch2_bkey_pack_test();
2030 if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
2031 bch2_btree_key_cache_init() ||
2032 bch2_chardev_init() ||
2043 #define BCH_DEBUG_PARAM(name, description) \
2045 module_param_named(name, bch2_##name, bool, 0644); \
2046 MODULE_PARM_DESC(name, description);
2048 #undef BCH_DEBUG_PARAM
2050 module_exit(bcachefs_exit);
2051 module_init(bcachefs_init);