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/kthread.h>
53 #include <linux/module.h>
54 #include <linux/percpu.h>
55 #include <linux/random.h>
56 #include <linux/sysfs.h>
57 #include <crypto/hash.h>
59 MODULE_LICENSE("GPL");
60 MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
63 static const struct attribute_group type ## _group = { \
64 .attrs = type ## _files \
67 static const struct attribute_group *type ## _groups[] = { \
72 static const struct kobj_type type ## _ktype = { \
73 .release = type ## _release, \
74 .sysfs_ops = &type ## _sysfs_ops, \
75 .default_groups = type ## _groups \
78 static void bch2_fs_release(struct kobject *);
79 static void bch2_dev_release(struct kobject *);
81 static void bch2_fs_internal_release(struct kobject *k)
85 static void bch2_fs_opts_dir_release(struct kobject *k)
89 static void bch2_fs_time_stats_release(struct kobject *k)
94 KTYPE(bch2_fs_internal);
95 KTYPE(bch2_fs_opts_dir);
96 KTYPE(bch2_fs_time_stats);
99 static struct kset *bcachefs_kset;
100 static LIST_HEAD(bch_fs_list);
101 static DEFINE_MUTEX(bch_fs_list_lock);
103 static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
105 static void bch2_dev_free(struct bch_dev *);
106 static int bch2_dev_alloc(struct bch_fs *, unsigned);
107 static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *);
108 static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
110 struct bch_fs *bch2_dev_to_fs(dev_t dev)
116 mutex_lock(&bch_fs_list_lock);
119 list_for_each_entry(c, &bch_fs_list, list)
120 for_each_member_device_rcu(ca, c, i, NULL)
121 if (ca->disk_sb.bdev->bd_dev == dev) {
128 mutex_unlock(&bch_fs_list_lock);
133 static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid)
137 lockdep_assert_held(&bch_fs_list_lock);
139 list_for_each_entry(c, &bch_fs_list, list)
140 if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid)))
146 struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid)
150 mutex_lock(&bch_fs_list_lock);
151 c = __bch2_uuid_to_fs(uuid);
154 mutex_unlock(&bch_fs_list_lock);
159 /* Filesystem RO/RW: */
162 * For startup/shutdown of RW stuff, the dependencies are:
164 * - foreground writes depend on copygc and rebalance (to free up space)
166 * - copygc and rebalance depend on mark and sweep gc (they actually probably
167 * don't because they either reserve ahead of time or don't block if
168 * allocations fail, but allocations can require mark and sweep gc to run
169 * because of generation number wraparound)
171 * - all of the above depends on the allocator threads
173 * - allocator depends on the journal (when it rewrites prios and gens)
176 static void __bch2_fs_read_only(struct bch_fs *c)
179 unsigned i, clean_passes = 0;
181 bch2_rebalance_stop(c);
183 bch2_gc_thread_stop(c);
185 bch2_io_timer_del(&c->io_clock[READ], &c->bucket_clock[READ].rescale);
186 bch2_io_timer_del(&c->io_clock[WRITE], &c->bucket_clock[WRITE].rescale);
189 * Flush journal before stopping allocators, because flushing journal
190 * blacklist entries involves allocating new btree nodes:
192 bch2_journal_flush_all_pins(&c->journal);
195 * If the allocator threads didn't all start up, the btree updates to
196 * write out alloc info aren't going to work:
198 if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags))
201 bch_verbose(c, "flushing journal and stopping allocators");
203 bch2_journal_flush_all_pins(&c->journal);
204 set_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags);
209 if (bch2_journal_flush_all_pins(&c->journal))
213 * In flight interior btree updates will generate more journal
214 * updates and btree updates (alloc btree):
216 if (bch2_btree_interior_updates_nr_pending(c)) {
217 closure_wait_event(&c->btree_interior_update_wait,
218 !bch2_btree_interior_updates_nr_pending(c));
221 flush_work(&c->btree_interior_update_work);
223 if (bch2_journal_flush_all_pins(&c->journal))
225 } while (clean_passes < 2);
226 bch_verbose(c, "flushing journal and stopping allocators complete");
228 set_bit(BCH_FS_ALLOC_CLEAN, &c->flags);
230 closure_wait_event(&c->btree_interior_update_wait,
231 !bch2_btree_interior_updates_nr_pending(c));
232 flush_work(&c->btree_interior_update_work);
234 for_each_member_device(ca, c, i)
235 bch2_dev_allocator_stop(ca);
237 clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
238 clear_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags);
240 bch2_fs_journal_stop(&c->journal);
243 * the journal kicks off btree writes via reclaim - wait for in flight
244 * writes after stopping journal:
246 if (test_bit(BCH_FS_EMERGENCY_RO, &c->flags))
247 bch2_btree_flush_all_writes(c);
249 bch2_btree_verify_flushed(c);
252 * After stopping journal:
254 for_each_member_device(ca, c, i)
255 bch2_dev_allocator_remove(c, ca);
258 static void bch2_writes_disabled(struct percpu_ref *writes)
260 struct bch_fs *c = container_of(writes, struct bch_fs, writes);
262 set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
263 wake_up(&bch_read_only_wait);
266 void bch2_fs_read_only(struct bch_fs *c)
268 if (!test_bit(BCH_FS_RW, &c->flags)) {
269 cancel_delayed_work_sync(&c->journal.reclaim_work);
273 BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
276 * Block new foreground-end write operations from starting - any new
277 * writes will return -EROFS:
279 * (This is really blocking new _allocations_, writes to previously
280 * allocated space can still happen until stopping the allocator in
281 * bch2_dev_allocator_stop()).
283 percpu_ref_kill(&c->writes);
285 cancel_work_sync(&c->ec_stripe_delete_work);
286 cancel_delayed_work(&c->pd_controllers_update);
289 * If we're not doing an emergency shutdown, we want to wait on
290 * outstanding writes to complete so they don't see spurious errors due
291 * to shutting down the allocator:
293 * If we are doing an emergency shutdown outstanding writes may
294 * hang until we shutdown the allocator so we don't want to wait
295 * on outstanding writes before shutting everything down - but
296 * we do need to wait on them before returning and signalling
297 * that going RO is complete:
299 wait_event(bch_read_only_wait,
300 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
301 test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
303 __bch2_fs_read_only(c);
305 wait_event(bch_read_only_wait,
306 test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
308 clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
310 if (!bch2_journal_error(&c->journal) &&
311 !test_bit(BCH_FS_ERROR, &c->flags) &&
312 !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) &&
313 test_bit(BCH_FS_STARTED, &c->flags) &&
314 test_bit(BCH_FS_ALLOC_CLEAN, &c->flags) &&
315 !c->opts.norecovery) {
316 bch_verbose(c, "marking filesystem clean");
317 bch2_fs_mark_clean(c);
320 clear_bit(BCH_FS_RW, &c->flags);
323 static void bch2_fs_read_only_work(struct work_struct *work)
326 container_of(work, struct bch_fs, read_only_work);
328 down_write(&c->state_lock);
329 bch2_fs_read_only(c);
330 up_write(&c->state_lock);
333 static void bch2_fs_read_only_async(struct bch_fs *c)
335 queue_work(system_long_wq, &c->read_only_work);
338 bool bch2_fs_emergency_read_only(struct bch_fs *c)
340 bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
342 bch2_journal_halt(&c->journal);
343 bch2_fs_read_only_async(c);
345 wake_up(&bch_read_only_wait);
349 static int bch2_fs_read_write_late(struct bch_fs *c)
353 ret = bch2_gc_thread_start(c);
355 bch_err(c, "error starting gc thread");
359 ret = bch2_copygc_start(c);
361 bch_err(c, "error starting copygc thread");
365 ret = bch2_rebalance_start(c);
367 bch_err(c, "error starting rebalance thread");
371 schedule_delayed_work(&c->pd_controllers_update, 5 * HZ);
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_RW, &c->flags))
388 * nochanges is used for fsck -n mode - we have to allow going rw
389 * during recovery for that to work:
391 if (c->opts.norecovery ||
392 (c->opts.nochanges &&
393 (!early || c->opts.read_only)))
396 ret = bch2_fs_mark_dirty(c);
401 * We need to write out a journal entry before we start doing btree
402 * updates, to ensure that on unclean shutdown new journal blacklist
403 * entries are created:
405 bch2_journal_meta(&c->journal);
407 clear_bit(BCH_FS_ALLOC_CLEAN, &c->flags);
409 for_each_rw_member(ca, c, i)
410 bch2_dev_allocator_add(c, ca);
411 bch2_recalc_capacity(c);
413 bch2_io_timer_add(&c->io_clock[READ], &c->bucket_clock[READ].rescale);
414 bch2_io_timer_add(&c->io_clock[WRITE], &c->bucket_clock[WRITE].rescale);
416 for_each_rw_member(ca, c, i) {
417 ret = bch2_dev_allocator_start(ca);
419 bch_err(c, "error starting allocator threads");
420 percpu_ref_put(&ca->io_ref);
425 set_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
428 ret = bch2_fs_read_write_late(c);
433 percpu_ref_reinit(&c->writes);
434 set_bit(BCH_FS_RW, &c->flags);
436 queue_delayed_work(c->journal_reclaim_wq,
437 &c->journal.reclaim_work, 0);
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)
462 for (i = 0; i < BCH_TIME_STAT_NR; i++)
463 bch2_time_stats_exit(&c->times[i]);
465 bch2_fs_quota_exit(c);
466 bch2_fs_fsio_exit(c);
468 bch2_fs_encryption_exit(c);
470 bch2_fs_btree_interior_update_exit(c);
471 bch2_fs_btree_iter_exit(c);
472 bch2_fs_btree_key_cache_exit(&c->btree_key_cache);
473 bch2_fs_btree_cache_exit(c);
474 bch2_fs_journal_exit(&c->journal);
475 bch2_io_clock_exit(&c->io_clock[WRITE]);
476 bch2_io_clock_exit(&c->io_clock[READ]);
477 bch2_fs_compress_exit(c);
478 bch2_journal_keys_free(&c->journal_keys);
479 bch2_journal_entries_free(&c->journal_entries);
480 percpu_free_rwsem(&c->mark_lock);
481 free_percpu(c->online_reserved);
482 kfree(c->usage_scratch);
483 free_percpu(c->usage[1]);
484 free_percpu(c->usage[0]);
485 kfree(c->usage_base);
486 free_percpu(c->pcpu);
487 mempool_exit(&c->large_bkey_pool);
488 mempool_exit(&c->btree_bounce_pool);
489 bioset_exit(&c->btree_bio);
490 mempool_exit(&c->fill_iter);
491 percpu_ref_exit(&c->writes);
492 kfree(c->replicas.entries);
493 kfree(c->replicas_gc.entries);
494 kfree(rcu_dereference_protected(c->disk_groups, 1));
495 kfree(c->journal_seq_blacklist_table);
496 free_heap(&c->copygc_heap);
498 if (c->journal_reclaim_wq)
499 destroy_workqueue(c->journal_reclaim_wq);
501 destroy_workqueue(c->copygc_wq);
503 destroy_workqueue(c->wq);
505 free_pages((unsigned long) c->disk_sb.sb,
506 c->disk_sb.page_order);
507 kvpfree(c, sizeof(*c));
508 module_put(THIS_MODULE);
511 static void bch2_fs_release(struct kobject *kobj)
513 struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
518 void __bch2_fs_stop(struct bch_fs *c)
523 bch_verbose(c, "shutting down");
525 set_bit(BCH_FS_STOPPING, &c->flags);
527 cancel_work_sync(&c->journal_seq_blacklist_gc_work);
529 down_write(&c->state_lock);
530 bch2_fs_read_only(c);
531 up_write(&c->state_lock);
533 for_each_member_device(ca, c, i)
534 if (ca->kobj.state_in_sysfs &&
536 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
538 if (c->kobj.state_in_sysfs)
539 kobject_del(&c->kobj);
541 bch2_fs_debug_exit(c);
542 bch2_fs_chardev_exit(c);
544 kobject_put(&c->time_stats);
545 kobject_put(&c->opts_dir);
546 kobject_put(&c->internal);
548 /* btree prefetch might have kicked off reads in the background: */
549 bch2_btree_flush_all_reads(c);
551 for_each_member_device(ca, c, i)
552 cancel_work_sync(&ca->io_error_work);
554 cancel_work_sync(&c->btree_write_error_work);
555 cancel_delayed_work_sync(&c->pd_controllers_update);
556 cancel_work_sync(&c->read_only_work);
559 void bch2_fs_free(struct bch_fs *c)
563 mutex_lock(&bch_fs_list_lock);
565 mutex_unlock(&bch_fs_list_lock);
567 closure_sync(&c->cl);
568 closure_debug_destroy(&c->cl);
570 for (i = 0; i < c->sb.nr_devices; i++) {
571 struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true);
574 bch2_free_super(&ca->disk_sb);
579 bch_verbose(c, "shutdown complete");
581 kobject_put(&c->kobj);
584 void bch2_fs_stop(struct bch_fs *c)
590 static const char *bch2_fs_online(struct bch_fs *c)
593 const char *err = NULL;
597 lockdep_assert_held(&bch_fs_list_lock);
599 if (!list_empty(&c->list))
602 if (__bch2_uuid_to_fs(c->sb.uuid))
603 return "filesystem UUID already open";
605 ret = bch2_fs_chardev_init(c);
607 return "error creating character device";
609 bch2_fs_debug_init(c);
611 if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ||
612 kobject_add(&c->internal, &c->kobj, "internal") ||
613 kobject_add(&c->opts_dir, &c->kobj, "options") ||
614 kobject_add(&c->time_stats, &c->kobj, "time_stats") ||
615 bch2_opts_create_sysfs_files(&c->opts_dir))
616 return "error creating sysfs objects";
618 down_write(&c->state_lock);
620 err = "error creating sysfs objects";
621 __for_each_member_device(ca, c, i, NULL)
622 if (bch2_dev_sysfs_online(c, ca))
625 list_add(&c->list, &bch_fs_list);
628 up_write(&c->state_lock);
632 static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
634 struct bch_sb_field_members *mi;
636 unsigned i, iter_size;
639 pr_verbose_init(opts, "");
641 c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO);
645 __module_get(THIS_MODULE);
647 closure_init(&c->cl, NULL);
649 c->kobj.kset = bcachefs_kset;
650 kobject_init(&c->kobj, &bch2_fs_ktype);
651 kobject_init(&c->internal, &bch2_fs_internal_ktype);
652 kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype);
653 kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype);
656 c->disk_sb.fs_sb = true;
658 init_rwsem(&c->state_lock);
659 mutex_init(&c->sb_lock);
660 mutex_init(&c->replicas_gc_lock);
661 mutex_init(&c->btree_root_lock);
662 INIT_WORK(&c->read_only_work, bch2_fs_read_only_work);
664 init_rwsem(&c->gc_lock);
666 for (i = 0; i < BCH_TIME_STAT_NR; i++)
667 bch2_time_stats_init(&c->times[i]);
669 bch2_fs_copygc_init(c);
670 bch2_fs_btree_key_cache_init_early(&c->btree_key_cache);
671 bch2_fs_allocator_background_init(c);
672 bch2_fs_allocator_foreground_init(c);
673 bch2_fs_rebalance_init(c);
674 bch2_fs_quota_init(c);
676 INIT_LIST_HEAD(&c->list);
678 mutex_init(&c->usage_scratch_lock);
680 mutex_init(&c->bio_bounce_pages_lock);
682 bio_list_init(&c->btree_write_error_list);
683 spin_lock_init(&c->btree_write_error_lock);
684 INIT_WORK(&c->btree_write_error_work, bch2_btree_write_error_work);
686 INIT_WORK(&c->journal_seq_blacklist_gc_work,
687 bch2_blacklist_entries_gc);
689 INIT_LIST_HEAD(&c->journal_entries);
691 INIT_LIST_HEAD(&c->fsck_errors);
692 mutex_init(&c->fsck_error_lock);
694 INIT_LIST_HEAD(&c->ec_stripe_head_list);
695 mutex_init(&c->ec_stripe_head_lock);
697 INIT_LIST_HEAD(&c->ec_stripe_new_list);
698 mutex_init(&c->ec_stripe_new_lock);
700 spin_lock_init(&c->ec_stripes_heap_lock);
702 seqcount_init(&c->gc_pos_lock);
704 seqcount_init(&c->usage_lock);
706 mutex_init(&c->inode_create_lock);
708 c->copy_gc_enabled = 1;
709 c->rebalance.enabled = 1;
710 c->promote_whole_extents = true;
712 c->journal.write_time = &c->times[BCH_TIME_journal_write];
713 c->journal.delay_time = &c->times[BCH_TIME_journal_delay];
714 c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal];
715 c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq];
717 bch2_fs_btree_cache_init_early(&c->btree_cache);
719 mutex_init(&c->sectors_available_lock);
721 if (percpu_init_rwsem(&c->mark_lock))
724 mutex_lock(&c->sb_lock);
726 if (bch2_sb_to_fs(c, sb)) {
727 mutex_unlock(&c->sb_lock);
731 mutex_unlock(&c->sb_lock);
733 scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid);
735 c->opts = bch2_opts_default;
736 bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb));
737 bch2_opts_apply(&c->opts, opts);
739 c->block_bits = ilog2(c->opts.block_size);
740 c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
742 if (bch2_fs_init_fault("fs_alloc"))
745 iter_size = sizeof(struct sort_iter) +
746 (btree_blocks(c) + 1) * 2 *
747 sizeof(struct sort_iter_set);
749 if (!(c->wq = alloc_workqueue("bcachefs",
750 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
751 !(c->copygc_wq = alloc_workqueue("bcache_copygc",
752 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
753 !(c->journal_reclaim_wq = alloc_workqueue("bcache_journal",
754 WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
755 percpu_ref_init(&c->writes, bch2_writes_disabled,
756 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
757 mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
758 bioset_init(&c->btree_bio, 1,
759 max(offsetof(struct btree_read_bio, bio),
760 offsetof(struct btree_write_bio, wbio.bio)),
761 BIOSET_NEED_BVECS) ||
762 !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) ||
763 !(c->online_reserved = alloc_percpu(u64)) ||
764 mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1,
766 mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) ||
767 bch2_io_clock_init(&c->io_clock[READ]) ||
768 bch2_io_clock_init(&c->io_clock[WRITE]) ||
769 bch2_fs_journal_init(&c->journal) ||
770 bch2_fs_replicas_init(c) ||
771 bch2_fs_btree_cache_init(c) ||
772 bch2_fs_btree_key_cache_init(&c->btree_key_cache) ||
773 bch2_fs_btree_iter_init(c) ||
774 bch2_fs_btree_interior_update_init(c) ||
775 bch2_fs_io_init(c) ||
776 bch2_fs_encryption_init(c) ||
777 bch2_fs_compress_init(c) ||
778 bch2_fs_ec_init(c) ||
779 bch2_fs_fsio_init(c))
782 mi = bch2_sb_get_members(c->disk_sb.sb);
783 for (i = 0; i < c->sb.nr_devices; i++)
784 if (bch2_dev_exists(c->disk_sb.sb, mi, i) &&
785 bch2_dev_alloc(c, i))
788 mutex_lock(&bch_fs_list_lock);
789 err = bch2_fs_online(c);
790 mutex_unlock(&bch_fs_list_lock);
792 bch_err(c, "bch2_fs_online() error: %s", err);
796 pr_verbose_init(opts, "ret %i", c ? 0 : -ENOMEM);
805 static void print_mount_opts(struct bch_fs *c)
809 struct printbuf p = PBUF(buf);
812 strcpy(buf, "(null)");
814 if (c->opts.read_only) {
819 for (i = 0; i < bch2_opts_nr; i++) {
820 const struct bch_option *opt = &bch2_opt_table[i];
821 u64 v = bch2_opt_get_by_id(&c->opts, i);
823 if (!(opt->mode & OPT_MOUNT))
826 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
832 bch2_opt_to_text(&p, c, opt, v, OPT_SHOW_MOUNT_STYLE);
835 bch_info(c, "mounted with opts: %s", buf);
838 int bch2_fs_start(struct bch_fs *c)
840 const char *err = "cannot allocate memory";
841 struct bch_sb_field_members *mi;
843 time64_t now = ktime_get_real_seconds();
847 down_write(&c->state_lock);
849 BUG_ON(test_bit(BCH_FS_STARTED, &c->flags));
851 mutex_lock(&c->sb_lock);
853 for_each_online_member(ca, c, i)
854 bch2_sb_from_fs(c, ca);
856 mi = bch2_sb_get_members(c->disk_sb.sb);
857 for_each_online_member(ca, c, i)
858 mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
860 mutex_unlock(&c->sb_lock);
862 for_each_rw_member(ca, c, i)
863 bch2_dev_allocator_add(c, ca);
864 bch2_recalc_capacity(c);
866 ret = BCH_SB_INITIALIZED(c->disk_sb.sb)
867 ? bch2_fs_recovery(c)
868 : bch2_fs_initialize(c);
872 ret = bch2_opts_check_may_set(c);
876 err = "dynamic fault";
878 if (bch2_fs_init_fault("fs_start"))
881 set_bit(BCH_FS_STARTED, &c->flags);
884 * Allocator threads don't start filling copygc reserve until after we
885 * set BCH_FS_STARTED - wake them now:
887 for_each_online_member(ca, c, i)
888 bch2_wake_allocator(ca);
890 if (c->opts.read_only || c->opts.nochanges) {
891 bch2_fs_read_only(c);
893 err = "error going read write";
894 ret = !test_bit(BCH_FS_RW, &c->flags)
895 ? bch2_fs_read_write(c)
896 : bch2_fs_read_write_late(c);
904 up_write(&c->state_lock);
908 case BCH_FSCK_ERRORS_NOT_FIXED:
909 bch_err(c, "filesystem contains errors: please report this to the developers");
910 pr_cont("mount with -o fix_errors to repair\n");
913 case BCH_FSCK_REPAIR_UNIMPLEMENTED:
914 bch_err(c, "filesystem contains errors: please report this to the developers");
915 pr_cont("repair unimplemented: inform the developers so that it can be added\n");
918 case BCH_FSCK_REPAIR_IMPOSSIBLE:
919 bch_err(c, "filesystem contains errors, but repair impossible");
922 case BCH_FSCK_UNKNOWN_VERSION:
923 err = "unknown metadata version";;
926 err = "cannot allocate memory";
938 static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
940 struct bch_sb_field_members *sb_mi;
942 sb_mi = bch2_sb_get_members(sb);
944 return "Invalid superblock: member info area missing";
946 if (le16_to_cpu(sb->block_size) != c->opts.block_size)
947 return "mismatched block size";
949 if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
950 BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
951 return "new cache bucket size is too small";
956 static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
958 struct bch_sb *newest =
959 le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
960 struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
962 if (!uuid_equal(&fs->uuid, &sb->uuid))
963 return "device not a member of filesystem";
965 if (!bch2_dev_exists(newest, mi, sb->dev_idx))
966 return "device has been removed";
968 if (fs->block_size != sb->block_size)
969 return "mismatched block size";
974 /* Device startup/shutdown: */
976 static void bch2_dev_release(struct kobject *kobj)
978 struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
983 static void bch2_dev_free(struct bch_dev *ca)
985 cancel_work_sync(&ca->io_error_work);
987 if (ca->kobj.state_in_sysfs &&
989 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
991 if (ca->kobj.state_in_sysfs)
992 kobject_del(&ca->kobj);
994 bch2_free_super(&ca->disk_sb);
995 bch2_dev_journal_exit(ca);
997 free_percpu(ca->io_done);
998 bioset_exit(&ca->replica_set);
999 bch2_dev_buckets_free(ca);
1000 free_page((unsigned long) ca->sb_read_scratch);
1002 bch2_time_stats_exit(&ca->io_latency[WRITE]);
1003 bch2_time_stats_exit(&ca->io_latency[READ]);
1005 percpu_ref_exit(&ca->io_ref);
1006 percpu_ref_exit(&ca->ref);
1007 kobject_put(&ca->kobj);
1010 static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca)
1013 lockdep_assert_held(&c->state_lock);
1015 if (percpu_ref_is_zero(&ca->io_ref))
1018 __bch2_dev_read_only(c, ca);
1020 reinit_completion(&ca->io_ref_completion);
1021 percpu_ref_kill(&ca->io_ref);
1022 wait_for_completion(&ca->io_ref_completion);
1024 if (ca->kobj.state_in_sysfs) {
1025 sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
1026 sysfs_remove_link(&ca->kobj, "block");
1029 bch2_free_super(&ca->disk_sb);
1030 bch2_dev_journal_exit(ca);
1033 static void bch2_dev_ref_complete(struct percpu_ref *ref)
1035 struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
1037 complete(&ca->ref_completion);
1040 static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
1042 struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
1044 complete(&ca->io_ref_completion);
1047 static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca)
1051 if (!c->kobj.state_in_sysfs)
1054 if (!ca->kobj.state_in_sysfs) {
1055 ret = kobject_add(&ca->kobj, &c->kobj,
1056 "dev-%u", ca->dev_idx);
1061 if (ca->disk_sb.bdev) {
1062 struct kobject *block = bdev_kobj(ca->disk_sb.bdev);
1064 ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
1068 ret = sysfs_create_link(&ca->kobj, block, "block");
1076 static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c,
1077 struct bch_member *member)
1081 ca = kzalloc(sizeof(*ca), GFP_KERNEL);
1085 kobject_init(&ca->kobj, &bch2_dev_ktype);
1086 init_completion(&ca->ref_completion);
1087 init_completion(&ca->io_ref_completion);
1089 init_rwsem(&ca->bucket_lock);
1091 INIT_WORK(&ca->io_error_work, bch2_io_error_work);
1093 bch2_time_stats_init(&ca->io_latency[READ]);
1094 bch2_time_stats_init(&ca->io_latency[WRITE]);
1096 ca->mi = bch2_mi_to_cpu(member);
1097 ca->uuid = member->uuid;
1099 if (opt_defined(c->opts, discard))
1100 ca->mi.discard = opt_get(c->opts, discard);
1102 if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
1104 percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
1105 PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
1106 !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) ||
1107 bch2_dev_buckets_alloc(c, ca) ||
1108 bioset_init(&ca->replica_set, 4,
1109 offsetof(struct bch_write_bio, bio), 0) ||
1110 !(ca->io_done = alloc_percpu(*ca->io_done)))
1119 static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca,
1122 ca->dev_idx = dev_idx;
1123 __set_bit(ca->dev_idx, ca->self.d);
1124 scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
1127 rcu_assign_pointer(c->devs[ca->dev_idx], ca);
1129 if (bch2_dev_sysfs_online(c, ca))
1130 pr_warn("error creating sysfs objects");
1133 static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
1135 struct bch_member *member =
1136 bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx;
1137 struct bch_dev *ca = NULL;
1140 pr_verbose_init(c->opts, "");
1142 if (bch2_fs_init_fault("dev_alloc"))
1145 ca = __bch2_dev_alloc(c, member);
1149 bch2_dev_attach(c, ca, dev_idx);
1151 pr_verbose_init(c->opts, "ret %i", ret);
1160 static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
1164 if (bch2_dev_is_online(ca)) {
1165 bch_err(ca, "already have device online in slot %u",
1170 if (get_capacity(sb->bdev->bd_disk) <
1171 ca->mi.bucket_size * ca->mi.nbuckets) {
1172 bch_err(ca, "cannot online: device too small");
1176 BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
1178 if (get_capacity(sb->bdev->bd_disk) <
1179 ca->mi.bucket_size * ca->mi.nbuckets) {
1180 bch_err(ca, "device too small");
1184 ret = bch2_dev_journal_init(ca, sb->sb);
1190 memset(sb, 0, sizeof(*sb));
1192 percpu_ref_reinit(&ca->io_ref);
1197 static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb)
1202 lockdep_assert_held(&c->state_lock);
1204 if (le64_to_cpu(sb->sb->seq) >
1205 le64_to_cpu(c->disk_sb.sb->seq))
1206 bch2_sb_to_fs(c, sb->sb);
1208 BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
1209 !c->devs[sb->sb->dev_idx]);
1211 ca = bch_dev_locked(c, sb->sb->dev_idx);
1213 ret = __bch2_dev_attach_bdev(ca, sb);
1217 if (test_bit(BCH_FS_ALLOC_READ_DONE, &c->flags) &&
1218 !percpu_u64_get(&ca->usage[0]->buckets[BCH_DATA_sb])) {
1219 mutex_lock(&c->sb_lock);
1220 bch2_mark_dev_superblock(ca->fs, ca, 0);
1221 mutex_unlock(&c->sb_lock);
1224 bch2_dev_sysfs_online(c, ca);
1226 if (c->sb.nr_devices == 1)
1227 snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev);
1228 snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev);
1230 rebalance_wakeup(c);
1234 /* Device management: */
1237 * Note: this function is also used by the error paths - when a particular
1238 * device sees an error, we call it to determine whether we can just set the
1239 * device RO, or - if this function returns false - we'll set the whole
1242 * XXX: maybe we should be more explicit about whether we're changing state
1243 * because we got an error or what have you?
1245 bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
1246 enum bch_member_state new_state, int flags)
1248 struct bch_devs_mask new_online_devs;
1249 struct replicas_status s;
1250 struct bch_dev *ca2;
1251 int i, nr_rw = 0, required;
1253 lockdep_assert_held(&c->state_lock);
1255 switch (new_state) {
1256 case BCH_MEMBER_STATE_RW:
1258 case BCH_MEMBER_STATE_RO:
1259 if (ca->mi.state != BCH_MEMBER_STATE_RW)
1262 /* do we have enough devices to write to? */
1263 for_each_member_device(ca2, c, i)
1265 nr_rw += ca2->mi.state == BCH_MEMBER_STATE_RW;
1267 required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
1268 ? c->opts.metadata_replicas
1269 : c->opts.metadata_replicas_required,
1270 !(flags & BCH_FORCE_IF_DATA_DEGRADED)
1271 ? c->opts.data_replicas
1272 : c->opts.data_replicas_required);
1274 return nr_rw >= required;
1275 case BCH_MEMBER_STATE_FAILED:
1276 case BCH_MEMBER_STATE_SPARE:
1277 if (ca->mi.state != BCH_MEMBER_STATE_RW &&
1278 ca->mi.state != BCH_MEMBER_STATE_RO)
1281 /* do we have enough devices to read from? */
1282 new_online_devs = bch2_online_devs(c);
1283 __clear_bit(ca->dev_idx, new_online_devs.d);
1285 s = __bch2_replicas_status(c, new_online_devs);
1287 return bch2_have_enough_devs(s, flags);
1293 static bool bch2_fs_may_start(struct bch_fs *c)
1295 struct replicas_status s;
1296 struct bch_sb_field_members *mi;
1298 unsigned i, flags = c->opts.degraded
1299 ? BCH_FORCE_IF_DEGRADED
1302 if (!c->opts.degraded) {
1303 mutex_lock(&c->sb_lock);
1304 mi = bch2_sb_get_members(c->disk_sb.sb);
1306 for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
1307 if (!bch2_dev_exists(c->disk_sb.sb, mi, i))
1310 ca = bch_dev_locked(c, i);
1312 if (!bch2_dev_is_online(ca) &&
1313 (ca->mi.state == BCH_MEMBER_STATE_RW ||
1314 ca->mi.state == BCH_MEMBER_STATE_RO)) {
1315 mutex_unlock(&c->sb_lock);
1319 mutex_unlock(&c->sb_lock);
1322 s = bch2_replicas_status(c);
1324 return bch2_have_enough_devs(s, flags);
1327 static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
1330 * Device going read only means the copygc reserve get smaller, so we
1331 * don't want that happening while copygc is in progress:
1333 bch2_copygc_stop(c);
1336 * The allocator thread itself allocates btree nodes, so stop it first:
1338 bch2_dev_allocator_stop(ca);
1339 bch2_dev_allocator_remove(c, ca);
1340 bch2_dev_journal_stop(&c->journal, ca);
1342 bch2_copygc_start(c);
1345 static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
1347 lockdep_assert_held(&c->state_lock);
1349 BUG_ON(ca->mi.state != BCH_MEMBER_STATE_RW);
1351 bch2_dev_allocator_add(c, ca);
1352 bch2_recalc_capacity(c);
1354 if (bch2_dev_allocator_start(ca))
1355 return "error starting allocator thread";
1360 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1361 enum bch_member_state new_state, int flags)
1363 struct bch_sb_field_members *mi;
1366 if (ca->mi.state == new_state)
1369 if (!bch2_dev_state_allowed(c, ca, new_state, flags))
1372 if (new_state != BCH_MEMBER_STATE_RW)
1373 __bch2_dev_read_only(c, ca);
1375 bch_notice(ca, "%s", bch2_dev_state[new_state]);
1377 mutex_lock(&c->sb_lock);
1378 mi = bch2_sb_get_members(c->disk_sb.sb);
1379 SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
1380 bch2_write_super(c);
1381 mutex_unlock(&c->sb_lock);
1383 if (new_state == BCH_MEMBER_STATE_RW &&
1384 __bch2_dev_read_write(c, ca))
1387 rebalance_wakeup(c);
1392 int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
1393 enum bch_member_state new_state, int flags)
1397 down_write(&c->state_lock);
1398 ret = __bch2_dev_set_state(c, ca, new_state, flags);
1399 up_write(&c->state_lock);
1404 /* Device add/removal: */
1406 int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca)
1408 struct btree_trans trans;
1412 bch2_trans_init(&trans, c, 0, 0);
1414 for (i = 0; i < ca->mi.nbuckets; i++) {
1415 ret = bch2_btree_key_cache_flush(&trans,
1416 BTREE_ID_ALLOC, POS(ca->dev_idx, i));
1420 bch2_trans_exit(&trans);
1425 return bch2_btree_delete_range(c, BTREE_ID_ALLOC,
1426 POS(ca->dev_idx, 0),
1427 POS(ca->dev_idx + 1, 0),
1431 int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
1433 struct bch_sb_field_members *mi;
1434 unsigned dev_idx = ca->dev_idx, data;
1437 down_write(&c->state_lock);
1440 * We consume a reference to ca->ref, regardless of whether we succeed
1443 percpu_ref_put(&ca->ref);
1445 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1446 bch_err(ca, "Cannot remove without losing data");
1450 __bch2_dev_read_only(c, ca);
1452 ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
1454 bch_err(ca, "Remove failed: error %i dropping data", ret);
1458 ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx);
1460 bch_err(ca, "Remove failed: error %i flushing journal", ret);
1464 ret = bch2_dev_remove_alloc(c, ca);
1466 bch_err(ca, "Remove failed, error deleting alloc info");
1471 * must flush all existing journal entries, they might have
1472 * (overwritten) keys that point to the device we're removing:
1474 bch2_journal_flush_all_pins(&c->journal);
1476 * hack to ensure bch2_replicas_gc2() clears out entries to this device
1478 bch2_journal_meta(&c->journal);
1479 ret = bch2_journal_error(&c->journal);
1481 bch_err(ca, "Remove failed, journal error");
1485 ret = bch2_replicas_gc2(c);
1487 bch_err(ca, "Remove failed: error %i from replicas gc", ret);
1491 data = bch2_dev_has_data(c, ca);
1493 char data_has_str[100];
1495 bch2_flags_to_text(&PBUF(data_has_str),
1496 bch2_data_types, data);
1497 bch_err(ca, "Remove failed, still has data (%s)", data_has_str);
1502 __bch2_dev_offline(c, ca);
1504 mutex_lock(&c->sb_lock);
1505 rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
1506 mutex_unlock(&c->sb_lock);
1508 percpu_ref_kill(&ca->ref);
1509 wait_for_completion(&ca->ref_completion);
1514 * Free this device's slot in the bch_member array - all pointers to
1515 * this device must be gone:
1517 mutex_lock(&c->sb_lock);
1518 mi = bch2_sb_get_members(c->disk_sb.sb);
1519 memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
1521 bch2_write_super(c);
1523 mutex_unlock(&c->sb_lock);
1524 up_write(&c->state_lock);
1527 if (ca->mi.state == BCH_MEMBER_STATE_RW &&
1528 !percpu_ref_is_zero(&ca->io_ref))
1529 __bch2_dev_read_write(c, ca);
1530 up_write(&c->state_lock);
1534 static void dev_usage_clear(struct bch_dev *ca)
1536 struct bucket_array *buckets;
1538 percpu_memset(ca->usage[0], 0, sizeof(*ca->usage[0]));
1540 down_read(&ca->bucket_lock);
1541 buckets = bucket_array(ca);
1543 memset(buckets->b, 0, sizeof(buckets->b[0]) * buckets->nbuckets);
1544 up_read(&ca->bucket_lock);
1547 /* Add new device to running filesystem: */
1548 int bch2_dev_add(struct bch_fs *c, const char *path)
1550 struct bch_opts opts = bch2_opts_empty();
1551 struct bch_sb_handle sb;
1553 struct bch_dev *ca = NULL;
1554 struct bch_sb_field_members *mi;
1555 struct bch_member dev_mi;
1556 unsigned dev_idx, nr_devices, u64s;
1559 ret = bch2_read_super(path, &opts, &sb);
1563 err = bch2_sb_validate(&sb);
1567 dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx];
1569 err = bch2_dev_may_add(sb.sb, c);
1573 ca = __bch2_dev_alloc(c, &dev_mi);
1575 bch2_free_super(&sb);
1579 ret = __bch2_dev_attach_bdev(ca, &sb);
1586 * We want to allocate journal on the new device before adding the new
1587 * device to the filesystem because allocating after we attach requires
1588 * spinning up the allocator thread, and the allocator thread requires
1589 * doing btree writes, which if the existing devices are RO isn't going
1592 * So we have to mark where the superblocks are, but marking allocated
1593 * data normally updates the filesystem usage too, so we have to mark,
1594 * allocate the journal, reset all the marks, then remark after we
1597 bch2_mark_dev_superblock(ca->fs, ca, 0);
1599 err = "journal alloc failed";
1600 ret = bch2_dev_journal_alloc(ca);
1604 dev_usage_clear(ca);
1606 down_write(&c->state_lock);
1607 mutex_lock(&c->sb_lock);
1609 err = "insufficient space in new superblock";
1610 ret = bch2_sb_from_fs(c, ca);
1614 mi = bch2_sb_get_members(ca->disk_sb.sb);
1616 if (!bch2_sb_resize_members(&ca->disk_sb,
1617 le32_to_cpu(mi->field.u64s) +
1618 sizeof(dev_mi) / sizeof(u64))) {
1623 if (dynamic_fault("bcachefs:add:no_slot"))
1626 mi = bch2_sb_get_members(c->disk_sb.sb);
1627 for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
1628 if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx))
1631 err = "no slots available in superblock";
1636 nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
1637 u64s = (sizeof(struct bch_sb_field_members) +
1638 sizeof(struct bch_member) * nr_devices) / sizeof(u64);
1640 err = "no space in superblock for member info";
1643 mi = bch2_sb_resize_members(&c->disk_sb, u64s);
1649 mi->members[dev_idx] = dev_mi;
1650 mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds());
1651 c->disk_sb.sb->nr_devices = nr_devices;
1653 ca->disk_sb.sb->dev_idx = dev_idx;
1654 bch2_dev_attach(c, ca, dev_idx);
1656 bch2_mark_dev_superblock(c, ca, 0);
1658 bch2_write_super(c);
1659 mutex_unlock(&c->sb_lock);
1661 err = "alloc write failed";
1662 ret = bch2_dev_alloc_write(c, ca, 0);
1666 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1667 err = __bch2_dev_read_write(c, ca);
1672 up_write(&c->state_lock);
1676 mutex_unlock(&c->sb_lock);
1677 up_write(&c->state_lock);
1681 bch2_free_super(&sb);
1682 bch_err(c, "Unable to add device: %s", err);
1685 bch_err(c, "Error going rw after adding device: %s", err);
1689 /* Hot add existing device to running filesystem: */
1690 int bch2_dev_online(struct bch_fs *c, const char *path)
1692 struct bch_opts opts = bch2_opts_empty();
1693 struct bch_sb_handle sb = { NULL };
1694 struct bch_sb_field_members *mi;
1700 down_write(&c->state_lock);
1702 ret = bch2_read_super(path, &opts, &sb);
1704 up_write(&c->state_lock);
1708 dev_idx = sb.sb->dev_idx;
1710 err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
1714 if (bch2_dev_attach_bdev(c, &sb)) {
1715 err = "bch2_dev_attach_bdev() error";
1719 ca = bch_dev_locked(c, dev_idx);
1720 if (ca->mi.state == BCH_MEMBER_STATE_RW) {
1721 err = __bch2_dev_read_write(c, ca);
1726 mutex_lock(&c->sb_lock);
1727 mi = bch2_sb_get_members(c->disk_sb.sb);
1729 mi->members[ca->dev_idx].last_mount =
1730 cpu_to_le64(ktime_get_real_seconds());
1732 bch2_write_super(c);
1733 mutex_unlock(&c->sb_lock);
1735 up_write(&c->state_lock);
1738 up_write(&c->state_lock);
1739 bch2_free_super(&sb);
1740 bch_err(c, "error bringing %s online: %s", path, err);
1744 int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
1746 down_write(&c->state_lock);
1748 if (!bch2_dev_is_online(ca)) {
1749 bch_err(ca, "Already offline");
1750 up_write(&c->state_lock);
1754 if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
1755 bch_err(ca, "Cannot offline required disk");
1756 up_write(&c->state_lock);
1760 __bch2_dev_offline(c, ca);
1762 up_write(&c->state_lock);
1766 int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
1768 struct bch_member *mi;
1771 down_write(&c->state_lock);
1773 if (nbuckets < ca->mi.nbuckets) {
1774 bch_err(ca, "Cannot shrink yet");
1779 if (bch2_dev_is_online(ca) &&
1780 get_capacity(ca->disk_sb.bdev->bd_disk) <
1781 ca->mi.bucket_size * nbuckets) {
1782 bch_err(ca, "New size larger than device");
1787 ret = bch2_dev_buckets_resize(c, ca, nbuckets);
1789 bch_err(ca, "Resize error: %i", ret);
1793 mutex_lock(&c->sb_lock);
1794 mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
1795 mi->nbuckets = cpu_to_le64(nbuckets);
1797 bch2_write_super(c);
1798 mutex_unlock(&c->sb_lock);
1800 bch2_recalc_capacity(c);
1802 up_write(&c->state_lock);
1806 /* return with ref on ca->ref: */
1807 struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path)
1814 ret = lookup_bdev(path, &dev);
1816 return ERR_PTR(ret);
1818 for_each_member_device(ca, c, i)
1819 if (ca->disk_sb.bdev->bd_dev == dev)
1822 ca = ERR_PTR(-ENOENT);
1827 /* Filesystem open: */
1829 struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
1830 struct bch_opts opts)
1832 struct bch_sb_handle *sb = NULL;
1833 struct bch_fs *c = NULL;
1834 struct bch_sb_field_members *mi;
1835 unsigned i, best_sb = 0;
1839 pr_verbose_init(opts, "");
1842 c = ERR_PTR(-EINVAL);
1846 if (!try_module_get(THIS_MODULE)) {
1847 c = ERR_PTR(-ENODEV);
1851 sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
1855 for (i = 0; i < nr_devices; i++) {
1856 ret = bch2_read_super(devices[i], &opts, &sb[i]);
1860 err = bch2_sb_validate(&sb[i]);
1865 for (i = 1; i < nr_devices; i++)
1866 if (le64_to_cpu(sb[i].sb->seq) >
1867 le64_to_cpu(sb[best_sb].sb->seq))
1870 mi = bch2_sb_get_members(sb[best_sb].sb);
1873 while (i < nr_devices) {
1875 !bch2_dev_exists(sb[best_sb].sb, mi, sb[i].sb->dev_idx)) {
1876 pr_info("%pg has been removed, skipping", sb[i].bdev);
1877 bch2_free_super(&sb[i]);
1878 array_remove_item(sb, nr_devices, i);
1882 err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
1889 c = bch2_fs_alloc(sb[best_sb].sb, opts);
1893 err = "bch2_dev_online() error";
1894 down_write(&c->state_lock);
1895 for (i = 0; i < nr_devices; i++)
1896 if (bch2_dev_attach_bdev(c, &sb[i])) {
1897 up_write(&c->state_lock);
1900 up_write(&c->state_lock);
1902 err = "insufficient devices";
1903 if (!bch2_fs_may_start(c))
1906 if (!c->opts.nostart) {
1907 ret = bch2_fs_start(c);
1913 module_put(THIS_MODULE);
1915 pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
1918 pr_err("bch_fs_open err opening %s: %s",
1924 for (i = 0; i < nr_devices; i++)
1925 bch2_free_super(&sb[i]);
1930 static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb,
1931 struct bch_opts opts)
1935 bool allocated_fs = false;
1938 err = bch2_sb_validate(sb);
1942 mutex_lock(&bch_fs_list_lock);
1943 c = __bch2_uuid_to_fs(sb->sb->uuid);
1945 closure_get(&c->cl);
1947 err = bch2_dev_in_fs(c->disk_sb.sb, sb->sb);
1951 c = bch2_fs_alloc(sb->sb, opts);
1952 err = "cannot allocate memory";
1956 allocated_fs = true;
1959 err = "bch2_dev_online() error";
1961 mutex_lock(&c->sb_lock);
1962 if (bch2_dev_attach_bdev(c, sb)) {
1963 mutex_unlock(&c->sb_lock);
1966 mutex_unlock(&c->sb_lock);
1968 if (!c->opts.nostart && bch2_fs_may_start(c)) {
1969 err = "error starting filesystem";
1970 ret = bch2_fs_start(c);
1975 closure_put(&c->cl);
1976 mutex_unlock(&bch_fs_list_lock);
1980 mutex_unlock(&bch_fs_list_lock);
1985 closure_put(&c->cl);
1990 const char *bch2_fs_open_incremental(const char *path)
1992 struct bch_sb_handle sb;
1993 struct bch_opts opts = bch2_opts_empty();
1996 if (bch2_read_super(path, &opts, &sb))
1997 return "error reading superblock";
1999 err = __bch2_fs_open_incremental(&sb, opts);
2000 bch2_free_super(&sb);
2005 /* Global interfaces/init */
2007 static void bcachefs_exit(void)
2011 bch2_chardev_exit();
2013 kset_unregister(bcachefs_kset);
2016 static int __init bcachefs_init(void)
2018 bch2_bkey_pack_test();
2019 bch2_inode_pack_test();
2021 if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
2022 bch2_chardev_init() ||
2033 #define BCH_DEBUG_PARAM(name, description) \
2035 module_param_named(name, bch2_##name, bool, 0644); \
2036 MODULE_PARM_DESC(name, description);
2038 #undef BCH_DEBUG_PARAM
2040 unsigned bch2_metadata_version = bcachefs_metadata_version_current;
2041 module_param_named(version, bch2_metadata_version, uint, 0400);
2043 module_exit(bcachefs_exit);
2044 module_init(bcachefs_init);