xref: /linux/fs/bcachefs/super.c (revision dbbfca9f41e86903501dded3fd494e1a56f3c310)
11c6fdbd8SKent Overstreet // SPDX-License-Identifier: GPL-2.0
21c6fdbd8SKent Overstreet /*
31c6fdbd8SKent Overstreet  * bcachefs setup/teardown code, and some metadata io - read a superblock and
41c6fdbd8SKent Overstreet  * figure out what to do with it.
51c6fdbd8SKent Overstreet  *
61c6fdbd8SKent Overstreet  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
71c6fdbd8SKent Overstreet  * Copyright 2012 Google, Inc.
81c6fdbd8SKent Overstreet  */
91c6fdbd8SKent Overstreet 
101c6fdbd8SKent Overstreet #include "bcachefs.h"
117b3f84eaSKent Overstreet #include "alloc_background.h"
127b3f84eaSKent Overstreet #include "alloc_foreground.h"
135b8a9227SKent Overstreet #include "bkey_sort.h"
141c6fdbd8SKent Overstreet #include "btree_cache.h"
151c6fdbd8SKent Overstreet #include "btree_gc.h"
162ca88e5aSKent Overstreet #include "btree_key_cache.h"
171c6fdbd8SKent Overstreet #include "btree_update_interior.h"
181c6fdbd8SKent Overstreet #include "btree_io.h"
19920e69bcSKent Overstreet #include "btree_write_buffer.h"
2021aec962SKent Overstreet #include "buckets_waiting_for_journal.h"
211c6fdbd8SKent Overstreet #include "chardev.h"
221c6fdbd8SKent Overstreet #include "checksum.h"
231c6fdbd8SKent Overstreet #include "clock.h"
241c6fdbd8SKent Overstreet #include "compress.h"
25104c6974SDaniel Hill #include "counters.h"
261c6fdbd8SKent Overstreet #include "debug.h"
271c6fdbd8SKent Overstreet #include "disk_groups.h"
28cd575ddfSKent Overstreet #include "ec.h"
29d4bf5eecSKent Overstreet #include "errcode.h"
301c6fdbd8SKent Overstreet #include "error.h"
311c6fdbd8SKent Overstreet #include "fs.h"
321c6fdbd8SKent Overstreet #include "fs-io.h"
33*dbbfca9fSKent Overstreet #include "fs-io-buffered.h"
34*dbbfca9fSKent Overstreet #include "fs-io-direct.h"
351c6fdbd8SKent Overstreet #include "fsck.h"
361c6fdbd8SKent Overstreet #include "inode.h"
371c6fdbd8SKent Overstreet #include "io.h"
381c6fdbd8SKent Overstreet #include "journal.h"
391c6fdbd8SKent Overstreet #include "journal_reclaim.h"
401dd7f9d9SKent Overstreet #include "journal_seq_blacklist.h"
411c6fdbd8SKent Overstreet #include "move.h"
421c6fdbd8SKent Overstreet #include "migrate.h"
431c6fdbd8SKent Overstreet #include "movinggc.h"
44350175bfSKent Overstreet #include "nocow_locking.h"
451c6fdbd8SKent Overstreet #include "quota.h"
461c6fdbd8SKent Overstreet #include "rebalance.h"
471c6fdbd8SKent Overstreet #include "recovery.h"
481c6fdbd8SKent Overstreet #include "replicas.h"
4914b393eeSKent Overstreet #include "subvolume.h"
501c6fdbd8SKent Overstreet #include "super.h"
511c6fdbd8SKent Overstreet #include "super-io.h"
521c6fdbd8SKent Overstreet #include "sysfs.h"
531c6fdbd8SKent Overstreet #include "trace.h"
541c6fdbd8SKent Overstreet 
551c6fdbd8SKent Overstreet #include <linux/backing-dev.h>
561c6fdbd8SKent Overstreet #include <linux/blkdev.h>
571c6fdbd8SKent Overstreet #include <linux/debugfs.h>
581c6fdbd8SKent Overstreet #include <linux/device.h>
591c6fdbd8SKent Overstreet #include <linux/idr.h>
601c6fdbd8SKent Overstreet #include <linux/module.h>
611c6fdbd8SKent Overstreet #include <linux/percpu.h>
621c6fdbd8SKent Overstreet #include <linux/random.h>
631c6fdbd8SKent Overstreet #include <linux/sysfs.h>
641c6fdbd8SKent Overstreet #include <crypto/hash.h>
651c6fdbd8SKent Overstreet 
661c6fdbd8SKent Overstreet MODULE_LICENSE("GPL");
671c6fdbd8SKent Overstreet MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
681c6fdbd8SKent Overstreet 
691c6fdbd8SKent Overstreet #define KTYPE(type)							\
701c6fdbd8SKent Overstreet static const struct attribute_group type ## _group = {			\
711c6fdbd8SKent Overstreet 	.attrs = type ## _files						\
721c6fdbd8SKent Overstreet };									\
731c6fdbd8SKent Overstreet 									\
741c6fdbd8SKent Overstreet static const struct attribute_group *type ## _groups[] = {		\
751c6fdbd8SKent Overstreet 	&type ## _group,						\
761c6fdbd8SKent Overstreet 	NULL								\
771c6fdbd8SKent Overstreet };									\
781c6fdbd8SKent Overstreet 									\
791c6fdbd8SKent Overstreet static const struct kobj_type type ## _ktype = {			\
801c6fdbd8SKent Overstreet 	.release	= type ## _release,				\
811c6fdbd8SKent Overstreet 	.sysfs_ops	= &type ## _sysfs_ops,				\
821c6fdbd8SKent Overstreet 	.default_groups = type ## _groups				\
831c6fdbd8SKent Overstreet }
841c6fdbd8SKent Overstreet 
851c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *);
861c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *);
87104c6974SDaniel Hill static void bch2_fs_counters_release(struct kobject *k)
88104c6974SDaniel Hill {
89104c6974SDaniel Hill }
901c6fdbd8SKent Overstreet 
911c6fdbd8SKent Overstreet static void bch2_fs_internal_release(struct kobject *k)
921c6fdbd8SKent Overstreet {
931c6fdbd8SKent Overstreet }
941c6fdbd8SKent Overstreet 
951c6fdbd8SKent Overstreet static void bch2_fs_opts_dir_release(struct kobject *k)
961c6fdbd8SKent Overstreet {
971c6fdbd8SKent Overstreet }
981c6fdbd8SKent Overstreet 
991c6fdbd8SKent Overstreet static void bch2_fs_time_stats_release(struct kobject *k)
1001c6fdbd8SKent Overstreet {
1011c6fdbd8SKent Overstreet }
1021c6fdbd8SKent Overstreet 
1031c6fdbd8SKent Overstreet KTYPE(bch2_fs);
104104c6974SDaniel Hill KTYPE(bch2_fs_counters);
1051c6fdbd8SKent Overstreet KTYPE(bch2_fs_internal);
1061c6fdbd8SKent Overstreet KTYPE(bch2_fs_opts_dir);
1071c6fdbd8SKent Overstreet KTYPE(bch2_fs_time_stats);
1081c6fdbd8SKent Overstreet KTYPE(bch2_dev);
1091c6fdbd8SKent Overstreet 
1101c6fdbd8SKent Overstreet static struct kset *bcachefs_kset;
1111c6fdbd8SKent Overstreet static LIST_HEAD(bch_fs_list);
1121c6fdbd8SKent Overstreet static DEFINE_MUTEX(bch_fs_list_lock);
1131c6fdbd8SKent Overstreet 
114d94189adSKent Overstreet DECLARE_WAIT_QUEUE_HEAD(bch2_read_only_wait);
1151c6fdbd8SKent Overstreet 
1161c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *);
1171c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *, unsigned);
1181c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *);
1191c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
1201c6fdbd8SKent Overstreet 
1211c6fdbd8SKent Overstreet struct bch_fs *bch2_dev_to_fs(dev_t dev)
1221c6fdbd8SKent Overstreet {
1231c6fdbd8SKent Overstreet 	struct bch_fs *c;
1241c6fdbd8SKent Overstreet 	struct bch_dev *ca;
1251c6fdbd8SKent Overstreet 	unsigned i;
1261c6fdbd8SKent Overstreet 
1271c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
1281c6fdbd8SKent Overstreet 	rcu_read_lock();
1291c6fdbd8SKent Overstreet 
1301c6fdbd8SKent Overstreet 	list_for_each_entry(c, &bch_fs_list, list)
1311c6fdbd8SKent Overstreet 		for_each_member_device_rcu(ca, c, i, NULL)
132ec4ab9d2SDan Robertson 			if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) {
1331c6fdbd8SKent Overstreet 				closure_get(&c->cl);
1341c6fdbd8SKent Overstreet 				goto found;
1351c6fdbd8SKent Overstreet 			}
1361c6fdbd8SKent Overstreet 	c = NULL;
1371c6fdbd8SKent Overstreet found:
1381c6fdbd8SKent Overstreet 	rcu_read_unlock();
1391c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
1401c6fdbd8SKent Overstreet 
1411c6fdbd8SKent Overstreet 	return c;
1421c6fdbd8SKent Overstreet }
1431c6fdbd8SKent Overstreet 
1441c6fdbd8SKent Overstreet static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid)
1451c6fdbd8SKent Overstreet {
1461c6fdbd8SKent Overstreet 	struct bch_fs *c;
1471c6fdbd8SKent Overstreet 
1481c6fdbd8SKent Overstreet 	lockdep_assert_held(&bch_fs_list_lock);
1491c6fdbd8SKent Overstreet 
1501c6fdbd8SKent Overstreet 	list_for_each_entry(c, &bch_fs_list, list)
1511c6fdbd8SKent Overstreet 		if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid)))
1521c6fdbd8SKent Overstreet 			return c;
1531c6fdbd8SKent Overstreet 
1541c6fdbd8SKent Overstreet 	return NULL;
1551c6fdbd8SKent Overstreet }
1561c6fdbd8SKent Overstreet 
1571c6fdbd8SKent Overstreet struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid)
1581c6fdbd8SKent Overstreet {
1591c6fdbd8SKent Overstreet 	struct bch_fs *c;
1601c6fdbd8SKent Overstreet 
1611c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
1621c6fdbd8SKent Overstreet 	c = __bch2_uuid_to_fs(uuid);
1631c6fdbd8SKent Overstreet 	if (c)
1641c6fdbd8SKent Overstreet 		closure_get(&c->cl);
1651c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
1661c6fdbd8SKent Overstreet 
1671c6fdbd8SKent Overstreet 	return c;
1681c6fdbd8SKent Overstreet }
1691c6fdbd8SKent Overstreet 
170180fb49dSKent Overstreet static void bch2_dev_usage_journal_reserve(struct bch_fs *c)
171180fb49dSKent Overstreet {
172180fb49dSKent Overstreet 	struct bch_dev *ca;
173180fb49dSKent Overstreet 	unsigned i, nr = 0, u64s =
1744b8f89afSKent Overstreet 		((sizeof(struct jset_entry_dev_usage) +
1754b8f89afSKent Overstreet 		  sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR)) /
1764b8f89afSKent Overstreet 		sizeof(u64);
177180fb49dSKent Overstreet 
178180fb49dSKent Overstreet 	rcu_read_lock();
179180fb49dSKent Overstreet 	for_each_member_device_rcu(ca, c, i, NULL)
180180fb49dSKent Overstreet 		nr++;
181180fb49dSKent Overstreet 	rcu_read_unlock();
182180fb49dSKent Overstreet 
183180fb49dSKent Overstreet 	bch2_journal_entry_res_resize(&c->journal,
184180fb49dSKent Overstreet 			&c->dev_usage_journal_res, u64s * nr);
185180fb49dSKent Overstreet }
186180fb49dSKent Overstreet 
1871c6fdbd8SKent Overstreet /* Filesystem RO/RW: */
1881c6fdbd8SKent Overstreet 
1891c6fdbd8SKent Overstreet /*
1901c6fdbd8SKent Overstreet  * For startup/shutdown of RW stuff, the dependencies are:
1911c6fdbd8SKent Overstreet  *
1921c6fdbd8SKent Overstreet  * - foreground writes depend on copygc and rebalance (to free up space)
1931c6fdbd8SKent Overstreet  *
1941c6fdbd8SKent Overstreet  * - copygc and rebalance depend on mark and sweep gc (they actually probably
1951c6fdbd8SKent Overstreet  *   don't because they either reserve ahead of time or don't block if
1961c6fdbd8SKent Overstreet  *   allocations fail, but allocations can require mark and sweep gc to run
1971c6fdbd8SKent Overstreet  *   because of generation number wraparound)
1981c6fdbd8SKent Overstreet  *
1991c6fdbd8SKent Overstreet  * - all of the above depends on the allocator threads
2001c6fdbd8SKent Overstreet  *
2011c6fdbd8SKent Overstreet  * - allocator depends on the journal (when it rewrites prios and gens)
2021c6fdbd8SKent Overstreet  */
2031c6fdbd8SKent Overstreet 
2041c6fdbd8SKent Overstreet static void __bch2_fs_read_only(struct bch_fs *c)
2051c6fdbd8SKent Overstreet {
2061c6fdbd8SKent Overstreet 	struct bch_dev *ca;
207d5f70c1fSKent Overstreet 	unsigned i, clean_passes = 0;
208c0960603SKent Overstreet 	u64 seq = 0;
2091c6fdbd8SKent Overstreet 
210b40901b0SKent Overstreet 	bch2_fs_ec_stop(c);
211b40901b0SKent Overstreet 	bch2_open_buckets_stop(c, NULL, true);
2121c6fdbd8SKent Overstreet 	bch2_rebalance_stop(c);
213e6d11615SKent Overstreet 	bch2_copygc_stop(c);
2141c6fdbd8SKent Overstreet 	bch2_gc_thread_stop(c);
215b40901b0SKent Overstreet 	bch2_fs_ec_flush(c);
2161c6fdbd8SKent Overstreet 
21783ec519aSKent Overstreet 	bch_verbose(c, "flushing journal and stopping allocators, journal seq %llu",
21883ec519aSKent Overstreet 		    journal_cur_seq(&c->journal));
2191c6fdbd8SKent Overstreet 
220039fc4c5SKent Overstreet 	do {
221039fc4c5SKent Overstreet 		clean_passes++;
222039fc4c5SKent Overstreet 
223c0960603SKent Overstreet 		if (bch2_btree_interior_updates_flush(c) ||
224c0960603SKent Overstreet 		    bch2_journal_flush_all_pins(&c->journal) ||
225c0960603SKent Overstreet 		    bch2_btree_flush_all_writes(c) ||
226c0960603SKent Overstreet 		    seq != atomic64_read(&c->journal.seq)) {
227c0960603SKent Overstreet 			seq = atomic64_read(&c->journal.seq);
228039fc4c5SKent Overstreet 			clean_passes = 0;
229039fc4c5SKent Overstreet 		}
230d5f70c1fSKent Overstreet 	} while (clean_passes < 2);
231c0960603SKent Overstreet 
23283ec519aSKent Overstreet 	bch_verbose(c, "flushing journal and stopping allocators complete, journal seq %llu",
23383ec519aSKent Overstreet 		    journal_cur_seq(&c->journal));
2342340fd9dSKent Overstreet 
235c0960603SKent Overstreet 	if (test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags) &&
236c0960603SKent Overstreet 	    !test_bit(BCH_FS_EMERGENCY_RO, &c->flags))
237c0960603SKent Overstreet 		set_bit(BCH_FS_CLEAN_SHUTDOWN, &c->flags);
2381c6fdbd8SKent Overstreet 	bch2_fs_journal_stop(&c->journal);
2391c6fdbd8SKent Overstreet 
2401c6fdbd8SKent Overstreet 	/*
2411c6fdbd8SKent Overstreet 	 * After stopping journal:
2421c6fdbd8SKent Overstreet 	 */
2431c6fdbd8SKent Overstreet 	for_each_member_device(ca, c, i)
2441c6fdbd8SKent Overstreet 		bch2_dev_allocator_remove(c, ca);
2451c6fdbd8SKent Overstreet }
2461c6fdbd8SKent Overstreet 
247d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG
2481c6fdbd8SKent Overstreet static void bch2_writes_disabled(struct percpu_ref *writes)
2491c6fdbd8SKent Overstreet {
2501c6fdbd8SKent Overstreet 	struct bch_fs *c = container_of(writes, struct bch_fs, writes);
2511c6fdbd8SKent Overstreet 
2521c6fdbd8SKent Overstreet 	set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
253d94189adSKent Overstreet 	wake_up(&bch2_read_only_wait);
2541c6fdbd8SKent Overstreet }
255d94189adSKent Overstreet #endif
2561c6fdbd8SKent Overstreet 
2571c6fdbd8SKent Overstreet void bch2_fs_read_only(struct bch_fs *c)
2581c6fdbd8SKent Overstreet {
259134915f3SKent Overstreet 	if (!test_bit(BCH_FS_RW, &c->flags)) {
2609ae28f82SKent Overstreet 		bch2_journal_reclaim_stop(&c->journal);
2611c6fdbd8SKent Overstreet 		return;
262134915f3SKent Overstreet 	}
2631c6fdbd8SKent Overstreet 
2641c6fdbd8SKent Overstreet 	BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
2651c6fdbd8SKent Overstreet 
2661c6fdbd8SKent Overstreet 	/*
2671c6fdbd8SKent Overstreet 	 * Block new foreground-end write operations from starting - any new
2681c6fdbd8SKent Overstreet 	 * writes will return -EROFS:
2691c6fdbd8SKent Overstreet 	 */
270d94189adSKent Overstreet 	set_bit(BCH_FS_GOING_RO, &c->flags);
271d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG
2721c6fdbd8SKent Overstreet 	percpu_ref_kill(&c->writes);
273d94189adSKent Overstreet #else
274d94189adSKent Overstreet 	for (unsigned i = 0; i < BCH_WRITE_REF_NR; i++)
275d94189adSKent Overstreet 		bch2_write_ref_put(c, i);
276d94189adSKent Overstreet #endif
2771c6fdbd8SKent Overstreet 
2781c6fdbd8SKent Overstreet 	/*
2791c6fdbd8SKent Overstreet 	 * If we're not doing an emergency shutdown, we want to wait on
2801c6fdbd8SKent Overstreet 	 * outstanding writes to complete so they don't see spurious errors due
2811c6fdbd8SKent Overstreet 	 * to shutting down the allocator:
2821c6fdbd8SKent Overstreet 	 *
2831c6fdbd8SKent Overstreet 	 * If we are doing an emergency shutdown outstanding writes may
2841c6fdbd8SKent Overstreet 	 * hang until we shutdown the allocator so we don't want to wait
2851c6fdbd8SKent Overstreet 	 * on outstanding writes before shutting everything down - but
2861c6fdbd8SKent Overstreet 	 * we do need to wait on them before returning and signalling
2871c6fdbd8SKent Overstreet 	 * that going RO is complete:
2881c6fdbd8SKent Overstreet 	 */
289d94189adSKent Overstreet 	wait_event(bch2_read_only_wait,
2901c6fdbd8SKent Overstreet 		   test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
2911c6fdbd8SKent Overstreet 		   test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
2921c6fdbd8SKent Overstreet 
2931c6fdbd8SKent Overstreet 	__bch2_fs_read_only(c);
2941c6fdbd8SKent Overstreet 
295d94189adSKent Overstreet 	wait_event(bch2_read_only_wait,
2961c6fdbd8SKent Overstreet 		   test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
2971c6fdbd8SKent Overstreet 
2981c6fdbd8SKent Overstreet 	clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
299d94189adSKent Overstreet 	clear_bit(BCH_FS_GOING_RO, &c->flags);
3001c6fdbd8SKent Overstreet 
3011c6fdbd8SKent Overstreet 	if (!bch2_journal_error(&c->journal) &&
3021c6fdbd8SKent Overstreet 	    !test_bit(BCH_FS_ERROR, &c->flags) &&
3033aea4342SKent Overstreet 	    !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) &&
304a0e0bda1SKent Overstreet 	    test_bit(BCH_FS_STARTED, &c->flags) &&
305c0960603SKent Overstreet 	    test_bit(BCH_FS_CLEAN_SHUTDOWN, &c->flags) &&
306b2930396SKent Overstreet 	    !c->opts.norecovery) {
3077724664fSKent Overstreet 		BUG_ON(c->journal.last_empty_seq != journal_cur_seq(&c->journal));
3087724664fSKent Overstreet 		BUG_ON(atomic_read(&c->btree_cache.dirty));
3097724664fSKent Overstreet 		BUG_ON(atomic_long_read(&c->btree_key_cache.nr_dirty));
3107724664fSKent Overstreet 		BUG_ON(c->btree_write_buffer.state.nr);
3117724664fSKent Overstreet 
312b2930396SKent Overstreet 		bch_verbose(c, "marking filesystem clean");
313134915f3SKent Overstreet 		bch2_fs_mark_clean(c);
314b2930396SKent Overstreet 	}
3151c6fdbd8SKent Overstreet 
316134915f3SKent Overstreet 	clear_bit(BCH_FS_RW, &c->flags);
3171c6fdbd8SKent Overstreet }
3181c6fdbd8SKent Overstreet 
3191c6fdbd8SKent Overstreet static void bch2_fs_read_only_work(struct work_struct *work)
3201c6fdbd8SKent Overstreet {
3211c6fdbd8SKent Overstreet 	struct bch_fs *c =
3221c6fdbd8SKent Overstreet 		container_of(work, struct bch_fs, read_only_work);
3231c6fdbd8SKent Overstreet 
3241ada1606SKent Overstreet 	down_write(&c->state_lock);
3251c6fdbd8SKent Overstreet 	bch2_fs_read_only(c);
3261ada1606SKent Overstreet 	up_write(&c->state_lock);
3271c6fdbd8SKent Overstreet }
3281c6fdbd8SKent Overstreet 
3291c6fdbd8SKent Overstreet static void bch2_fs_read_only_async(struct bch_fs *c)
3301c6fdbd8SKent Overstreet {
3311c6fdbd8SKent Overstreet 	queue_work(system_long_wq, &c->read_only_work);
3321c6fdbd8SKent Overstreet }
3331c6fdbd8SKent Overstreet 
3341c6fdbd8SKent Overstreet bool bch2_fs_emergency_read_only(struct bch_fs *c)
3351c6fdbd8SKent Overstreet {
3361c6fdbd8SKent Overstreet 	bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
3371c6fdbd8SKent Overstreet 
3381c6fdbd8SKent Overstreet 	bch2_journal_halt(&c->journal);
3399f115ce9SKent Overstreet 	bch2_fs_read_only_async(c);
3401c6fdbd8SKent Overstreet 
341d94189adSKent Overstreet 	wake_up(&bch2_read_only_wait);
3421c6fdbd8SKent Overstreet 	return ret;
3431c6fdbd8SKent Overstreet }
3441c6fdbd8SKent Overstreet 
345134915f3SKent Overstreet static int bch2_fs_read_write_late(struct bch_fs *c)
3461c6fdbd8SKent Overstreet {
347134915f3SKent Overstreet 	int ret;
3481c6fdbd8SKent Overstreet 
349ea28c867SKent Overstreet 	/*
350ea28c867SKent Overstreet 	 * Data move operations can't run until after check_snapshots has
351ea28c867SKent Overstreet 	 * completed, and bch2_snapshot_is_ancestor() is available.
352ea28c867SKent Overstreet 	 *
353ea28c867SKent Overstreet 	 * Ideally we'd start copygc/rebalance earlier instead of waiting for
354ea28c867SKent Overstreet 	 * all of recovery/fsck to complete:
355ea28c867SKent Overstreet 	 */
356ea28c867SKent Overstreet 	ret = bch2_copygc_start(c);
357ea28c867SKent Overstreet 	if (ret) {
358ea28c867SKent Overstreet 		bch_err(c, "error starting copygc thread");
359ea28c867SKent Overstreet 		return ret;
360ea28c867SKent Overstreet 	}
361ea28c867SKent Overstreet 
362134915f3SKent Overstreet 	ret = bch2_rebalance_start(c);
363134915f3SKent Overstreet 	if (ret) {
364134915f3SKent Overstreet 		bch_err(c, "error starting rebalance thread");
365134915f3SKent Overstreet 		return ret;
366134915f3SKent Overstreet 	}
367134915f3SKent Overstreet 
368134915f3SKent Overstreet 	return 0;
369134915f3SKent Overstreet }
370134915f3SKent Overstreet 
371e731d466SKent Overstreet static int __bch2_fs_read_write(struct bch_fs *c, bool early)
372134915f3SKent Overstreet {
373134915f3SKent Overstreet 	struct bch_dev *ca;
374134915f3SKent Overstreet 	unsigned i;
375134915f3SKent Overstreet 	int ret;
376134915f3SKent Overstreet 
377aae15aafSKent Overstreet 	if (test_bit(BCH_FS_INITIAL_GC_UNFIXED, &c->flags)) {
378aae15aafSKent Overstreet 		bch_err(c, "cannot go rw, unfixed btree errors");
3797c50140fSKent Overstreet 		return -BCH_ERR_erofs_unfixed_errors;
380aae15aafSKent Overstreet 	}
381aae15aafSKent Overstreet 
382134915f3SKent Overstreet 	if (test_bit(BCH_FS_RW, &c->flags))
383134915f3SKent Overstreet 		return 0;
384134915f3SKent Overstreet 
3857c50140fSKent Overstreet 	if (c->opts.norecovery)
3867c50140fSKent Overstreet 		return -BCH_ERR_erofs_norecovery;
3877c50140fSKent Overstreet 
388619f5beeSKent Overstreet 	/*
389619f5beeSKent Overstreet 	 * nochanges is used for fsck -n mode - we have to allow going rw
390619f5beeSKent Overstreet 	 * during recovery for that to work:
391619f5beeSKent Overstreet 	 */
3927c50140fSKent Overstreet 	if (c->opts.nochanges && (!early || c->opts.read_only))
3937c50140fSKent Overstreet 		return -BCH_ERR_erofs_nochanges;
394330581f1SKent Overstreet 
3952c944fa1SKent Overstreet 	bch_info(c, "going read-write");
3962c944fa1SKent Overstreet 
397134915f3SKent Overstreet 	ret = bch2_fs_mark_dirty(c);
398134915f3SKent Overstreet 	if (ret)
399134915f3SKent Overstreet 		goto err;
4001c6fdbd8SKent Overstreet 
401c0960603SKent Overstreet 	clear_bit(BCH_FS_CLEAN_SHUTDOWN, &c->flags);
4022340fd9dSKent Overstreet 
403b9004e85SKent Overstreet 	/*
404b9004e85SKent Overstreet 	 * First journal write must be a flush write: after a clean shutdown we
405b9004e85SKent Overstreet 	 * don't read the journal, so the first journal write may end up
406b9004e85SKent Overstreet 	 * overwriting whatever was there previously, and there must always be
407b9004e85SKent Overstreet 	 * at least one non-flush write in the journal or recovery will fail:
408b9004e85SKent Overstreet 	 */
409b9004e85SKent Overstreet 	set_bit(JOURNAL_NEED_FLUSH_WRITE, &c->journal.flags);
410b9004e85SKent Overstreet 
4111c6fdbd8SKent Overstreet 	for_each_rw_member(ca, c, i)
4121c6fdbd8SKent Overstreet 		bch2_dev_allocator_add(c, ca);
4131c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
4141c6fdbd8SKent Overstreet 
4155f5c7466SKent Overstreet 	ret = bch2_gc_thread_start(c);
4165f5c7466SKent Overstreet 	if (ret) {
4175f5c7466SKent Overstreet 		bch_err(c, "error starting gc thread");
4185f5c7466SKent Overstreet 		return ret;
4195f5c7466SKent Overstreet 	}
4205f5c7466SKent Overstreet 
421134915f3SKent Overstreet 	if (!early) {
422134915f3SKent Overstreet 		ret = bch2_fs_read_write_late(c);
423134915f3SKent Overstreet 		if (ret)
4241c6fdbd8SKent Overstreet 			goto err;
4251c6fdbd8SKent Overstreet 	}
4261c6fdbd8SKent Overstreet 
427d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG
4281c6fdbd8SKent Overstreet 	percpu_ref_reinit(&c->writes);
429d94189adSKent Overstreet #else
430d94189adSKent Overstreet 	for (unsigned i = 0; i < BCH_WRITE_REF_NR; i++) {
431d94189adSKent Overstreet 		BUG_ON(atomic_long_read(&c->writes[i]));
432d94189adSKent Overstreet 		atomic_long_inc(&c->writes[i]);
433d94189adSKent Overstreet 	}
434d94189adSKent Overstreet #endif
435134915f3SKent Overstreet 	set_bit(BCH_FS_RW, &c->flags);
4364932e07eSKent Overstreet 	set_bit(BCH_FS_WAS_RW, &c->flags);
437dd81a060SKent Overstreet 
438dd81a060SKent Overstreet 	bch2_do_discards(c);
439dd81a060SKent Overstreet 	bch2_do_invalidates(c);
440dd81a060SKent Overstreet 	bch2_do_stripe_deletes(c);
441a1f26d70SKent Overstreet 	bch2_do_pending_node_rewrites(c);
442134915f3SKent Overstreet 	return 0;
4431c6fdbd8SKent Overstreet err:
4441c6fdbd8SKent Overstreet 	__bch2_fs_read_only(c);
445134915f3SKent Overstreet 	return ret;
446134915f3SKent Overstreet }
447134915f3SKent Overstreet 
448134915f3SKent Overstreet int bch2_fs_read_write(struct bch_fs *c)
449134915f3SKent Overstreet {
450134915f3SKent Overstreet 	return __bch2_fs_read_write(c, false);
451134915f3SKent Overstreet }
452134915f3SKent Overstreet 
453134915f3SKent Overstreet int bch2_fs_read_write_early(struct bch_fs *c)
454134915f3SKent Overstreet {
455134915f3SKent Overstreet 	lockdep_assert_held(&c->state_lock);
456134915f3SKent Overstreet 
457134915f3SKent Overstreet 	return __bch2_fs_read_write(c, true);
4581c6fdbd8SKent Overstreet }
4591c6fdbd8SKent Overstreet 
4601c6fdbd8SKent Overstreet /* Filesystem startup/shutdown: */
4611c6fdbd8SKent Overstreet 
462d5e4dcc2SKent Overstreet static void __bch2_fs_free(struct bch_fs *c)
4631c6fdbd8SKent Overstreet {
4641c6fdbd8SKent Overstreet 	unsigned i;
4651a21bf98SKent Overstreet 	int cpu;
4661c6fdbd8SKent Overstreet 
4671c6fdbd8SKent Overstreet 	for (i = 0; i < BCH_TIME_STAT_NR; i++)
4681c6fdbd8SKent Overstreet 		bch2_time_stats_exit(&c->times[i]);
4691c6fdbd8SKent Overstreet 
470a1f26d70SKent Overstreet 	bch2_free_pending_node_rewrites(c);
471104c6974SDaniel Hill 	bch2_fs_counters_exit(c);
47214b393eeSKent Overstreet 	bch2_fs_snapshots_exit(c);
4731c6fdbd8SKent Overstreet 	bch2_fs_quota_exit(c);
474*dbbfca9fSKent Overstreet 	bch2_fs_fs_io_direct_exit(c);
475*dbbfca9fSKent Overstreet 	bch2_fs_fs_io_buffered_exit(c);
4761c6fdbd8SKent Overstreet 	bch2_fs_fsio_exit(c);
477cd575ddfSKent Overstreet 	bch2_fs_ec_exit(c);
4781c6fdbd8SKent Overstreet 	bch2_fs_encryption_exit(c);
4791c6fdbd8SKent Overstreet 	bch2_fs_io_exit(c);
48021aec962SKent Overstreet 	bch2_fs_buckets_waiting_for_journal_exit(c);
481c823c339SKent Overstreet 	bch2_fs_btree_interior_update_exit(c);
48236e9d698SKent Overstreet 	bch2_fs_btree_iter_exit(c);
4832ca88e5aSKent Overstreet 	bch2_fs_btree_key_cache_exit(&c->btree_key_cache);
4841c6fdbd8SKent Overstreet 	bch2_fs_btree_cache_exit(c);
4859620c3ecSKent Overstreet 	bch2_fs_replicas_exit(c);
4861c6fdbd8SKent Overstreet 	bch2_fs_journal_exit(&c->journal);
4871c6fdbd8SKent Overstreet 	bch2_io_clock_exit(&c->io_clock[WRITE]);
4881c6fdbd8SKent Overstreet 	bch2_io_clock_exit(&c->io_clock[READ]);
4891c6fdbd8SKent Overstreet 	bch2_fs_compress_exit(c);
490f1d786a0SKent Overstreet 	bch2_journal_keys_free(&c->journal_keys);
491ce6201c4SKent Overstreet 	bch2_journal_entries_free(c);
492920e69bcSKent Overstreet 	bch2_fs_btree_write_buffer_exit(c);
4939166b41dSKent Overstreet 	percpu_free_rwsem(&c->mark_lock);
4945e82a9a1SKent Overstreet 	free_percpu(c->online_reserved);
4951a21bf98SKent Overstreet 
49667e0dd8fSKent Overstreet 	if (c->btree_paths_bufs)
4971a21bf98SKent Overstreet 		for_each_possible_cpu(cpu)
49867e0dd8fSKent Overstreet 			kfree(per_cpu_ptr(c->btree_paths_bufs, cpu)->path);
4991a21bf98SKent Overstreet 
500faa6cb6cSKent Overstreet 	darray_exit(&c->btree_roots_extra);
50167e0dd8fSKent Overstreet 	free_percpu(c->btree_paths_bufs);
5025663a415SKent Overstreet 	free_percpu(c->pcpu);
50335189e09SKent Overstreet 	mempool_exit(&c->large_bkey_pool);
5041c6fdbd8SKent Overstreet 	mempool_exit(&c->btree_bounce_pool);
5051c6fdbd8SKent Overstreet 	bioset_exit(&c->btree_bio);
5061c6fdbd8SKent Overstreet 	mempool_exit(&c->fill_iter);
507d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG
5081c6fdbd8SKent Overstreet 	percpu_ref_exit(&c->writes);
509d94189adSKent Overstreet #endif
5101c6fdbd8SKent Overstreet 	kfree(rcu_dereference_protected(c->disk_groups, 1));
5111dd7f9d9SKent Overstreet 	kfree(c->journal_seq_blacklist_table);
512b5e8a699SKent Overstreet 	kfree(c->unused_inode_hints);
5131c6fdbd8SKent Overstreet 
5148bff9875SBrian Foster 	if (c->write_ref_wq)
5158bff9875SBrian Foster 		destroy_workqueue(c->write_ref_wq);
516731bdd2eSKent Overstreet 	if (c->io_complete_wq)
517731bdd2eSKent Overstreet 		destroy_workqueue(c->io_complete_wq);
5181c6fdbd8SKent Overstreet 	if (c->copygc_wq)
5191c6fdbd8SKent Overstreet 		destroy_workqueue(c->copygc_wq);
5209f1833caSKent Overstreet 	if (c->btree_io_complete_wq)
5219f1833caSKent Overstreet 		destroy_workqueue(c->btree_io_complete_wq);
522731bdd2eSKent Overstreet 	if (c->btree_update_wq)
523731bdd2eSKent Overstreet 		destroy_workqueue(c->btree_update_wq);
5241c6fdbd8SKent Overstreet 
5259d8022dbSKent Overstreet 	bch2_free_super(&c->disk_sb);
5261c6fdbd8SKent Overstreet 	kvpfree(c, sizeof(*c));
5271c6fdbd8SKent Overstreet 	module_put(THIS_MODULE);
5281c6fdbd8SKent Overstreet }
5291c6fdbd8SKent Overstreet 
5301c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *kobj)
5311c6fdbd8SKent Overstreet {
5321c6fdbd8SKent Overstreet 	struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
5331c6fdbd8SKent Overstreet 
534d5e4dcc2SKent Overstreet 	__bch2_fs_free(c);
5351c6fdbd8SKent Overstreet }
5361c6fdbd8SKent Overstreet 
537d5e4dcc2SKent Overstreet void __bch2_fs_stop(struct bch_fs *c)
5381c6fdbd8SKent Overstreet {
5391c6fdbd8SKent Overstreet 	struct bch_dev *ca;
5401c6fdbd8SKent Overstreet 	unsigned i;
5411c6fdbd8SKent Overstreet 
542af1c6871SKent Overstreet 	bch_verbose(c, "shutting down");
543af1c6871SKent Overstreet 
5441dd7f9d9SKent Overstreet 	set_bit(BCH_FS_STOPPING, &c->flags);
5451dd7f9d9SKent Overstreet 
5469b6e2f1eSKent Overstreet 	cancel_work_sync(&c->journal_seq_blacklist_gc_work);
5479b6e2f1eSKent Overstreet 
5481ada1606SKent Overstreet 	down_write(&c->state_lock);
549883f1a7cSKent Overstreet 	bch2_fs_read_only(c);
5501ada1606SKent Overstreet 	up_write(&c->state_lock);
551883f1a7cSKent Overstreet 
5521c6fdbd8SKent Overstreet 	for_each_member_device(ca, c, i)
5531c6fdbd8SKent Overstreet 		if (ca->kobj.state_in_sysfs &&
5541c6fdbd8SKent Overstreet 		    ca->disk_sb.bdev)
5551c6fdbd8SKent Overstreet 			sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
5561c6fdbd8SKent Overstreet 
5571c6fdbd8SKent Overstreet 	if (c->kobj.state_in_sysfs)
5581c6fdbd8SKent Overstreet 		kobject_del(&c->kobj);
5591c6fdbd8SKent Overstreet 
5601c6fdbd8SKent Overstreet 	bch2_fs_debug_exit(c);
5611c6fdbd8SKent Overstreet 	bch2_fs_chardev_exit(c);
5621c6fdbd8SKent Overstreet 
563104c6974SDaniel Hill 	kobject_put(&c->counters_kobj);
5641c6fdbd8SKent Overstreet 	kobject_put(&c->time_stats);
5651c6fdbd8SKent Overstreet 	kobject_put(&c->opts_dir);
5661c6fdbd8SKent Overstreet 	kobject_put(&c->internal);
5671c6fdbd8SKent Overstreet 
5681c6fdbd8SKent Overstreet 	/* btree prefetch might have kicked off reads in the background: */
5691c6fdbd8SKent Overstreet 	bch2_btree_flush_all_reads(c);
5701c6fdbd8SKent Overstreet 
5711c6fdbd8SKent Overstreet 	for_each_member_device(ca, c, i)
5721c6fdbd8SKent Overstreet 		cancel_work_sync(&ca->io_error_work);
5731c6fdbd8SKent Overstreet 
5741c6fdbd8SKent Overstreet 	cancel_work_sync(&c->read_only_work);
575d5e4dcc2SKent Overstreet }
5761c6fdbd8SKent Overstreet 
577d5e4dcc2SKent Overstreet void bch2_fs_free(struct bch_fs *c)
578d5e4dcc2SKent Overstreet {
579d5e4dcc2SKent Overstreet 	unsigned i;
580d5e4dcc2SKent Overstreet 
581d5e4dcc2SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
582d5e4dcc2SKent Overstreet 	list_del(&c->list);
583d5e4dcc2SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
584d5e4dcc2SKent Overstreet 
585d5e4dcc2SKent Overstreet 	closure_sync(&c->cl);
586d5e4dcc2SKent Overstreet 	closure_debug_destroy(&c->cl);
587d5e4dcc2SKent Overstreet 
588d5e4dcc2SKent Overstreet 	for (i = 0; i < c->sb.nr_devices; i++) {
589d5e4dcc2SKent Overstreet 		struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true);
590d5e4dcc2SKent Overstreet 
591d5e4dcc2SKent Overstreet 		if (ca) {
592d5e4dcc2SKent Overstreet 			bch2_free_super(&ca->disk_sb);
593d5e4dcc2SKent Overstreet 			bch2_dev_free(ca);
594d5e4dcc2SKent Overstreet 		}
595d5e4dcc2SKent Overstreet 	}
5961c6fdbd8SKent Overstreet 
597af1c6871SKent Overstreet 	bch_verbose(c, "shutdown complete");
598af1c6871SKent Overstreet 
5991c6fdbd8SKent Overstreet 	kobject_put(&c->kobj);
6001c6fdbd8SKent Overstreet }
6011c6fdbd8SKent Overstreet 
602d5e4dcc2SKent Overstreet void bch2_fs_stop(struct bch_fs *c)
603d5e4dcc2SKent Overstreet {
604d5e4dcc2SKent Overstreet 	__bch2_fs_stop(c);
605d5e4dcc2SKent Overstreet 	bch2_fs_free(c);
606d5e4dcc2SKent Overstreet }
607d5e4dcc2SKent Overstreet 
608e2b60560SKent Overstreet static int bch2_fs_online(struct bch_fs *c)
6091c6fdbd8SKent Overstreet {
6101c6fdbd8SKent Overstreet 	struct bch_dev *ca;
6111c6fdbd8SKent Overstreet 	unsigned i;
612e2b60560SKent Overstreet 	int ret = 0;
6131c6fdbd8SKent Overstreet 
6141c6fdbd8SKent Overstreet 	lockdep_assert_held(&bch_fs_list_lock);
6151c6fdbd8SKent Overstreet 
616e2b60560SKent Overstreet 	if (__bch2_uuid_to_fs(c->sb.uuid)) {
617e2b60560SKent Overstreet 		bch_err(c, "filesystem UUID already open");
618e2b60560SKent Overstreet 		return -EINVAL;
619e2b60560SKent Overstreet 	}
6201c6fdbd8SKent Overstreet 
6211c6fdbd8SKent Overstreet 	ret = bch2_fs_chardev_init(c);
622e2b60560SKent Overstreet 	if (ret) {
623e2b60560SKent Overstreet 		bch_err(c, "error creating character device");
624e2b60560SKent Overstreet 		return ret;
625e2b60560SKent Overstreet 	}
6261c6fdbd8SKent Overstreet 
6271c6fdbd8SKent Overstreet 	bch2_fs_debug_init(c);
6281c6fdbd8SKent Overstreet 
629e2b60560SKent Overstreet 	ret = kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ?:
630e2b60560SKent Overstreet 	    kobject_add(&c->internal, &c->kobj, "internal") ?:
631e2b60560SKent Overstreet 	    kobject_add(&c->opts_dir, &c->kobj, "options") ?:
632e2b60560SKent Overstreet 	    kobject_add(&c->time_stats, &c->kobj, "time_stats") ?:
633104c6974SDaniel Hill 	    kobject_add(&c->counters_kobj, &c->kobj, "counters") ?:
634e2b60560SKent Overstreet 	    bch2_opts_create_sysfs_files(&c->opts_dir);
635e2b60560SKent Overstreet 	if (ret) {
636e2b60560SKent Overstreet 		bch_err(c, "error creating sysfs objects");
637e2b60560SKent Overstreet 		return ret;
638e2b60560SKent Overstreet 	}
6391c6fdbd8SKent Overstreet 
6401ada1606SKent Overstreet 	down_write(&c->state_lock);
6411c6fdbd8SKent Overstreet 
642e2b60560SKent Overstreet 	for_each_member_device(ca, c, i) {
643e2b60560SKent Overstreet 		ret = bch2_dev_sysfs_online(c, ca);
644e2b60560SKent Overstreet 		if (ret) {
645e2b60560SKent Overstreet 			bch_err(c, "error creating sysfs objects");
6463a402c8dSKent Overstreet 			percpu_ref_put(&ca->ref);
6471c6fdbd8SKent Overstreet 			goto err;
6483a402c8dSKent Overstreet 		}
649e2b60560SKent Overstreet 	}
6501c6fdbd8SKent Overstreet 
651e2b60560SKent Overstreet 	BUG_ON(!list_empty(&c->list));
6521c6fdbd8SKent Overstreet 	list_add(&c->list, &bch_fs_list);
6531c6fdbd8SKent Overstreet err:
6541ada1606SKent Overstreet 	up_write(&c->state_lock);
655e2b60560SKent Overstreet 	return ret;
6561c6fdbd8SKent Overstreet }
6571c6fdbd8SKent Overstreet 
6581c6fdbd8SKent Overstreet static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
6591c6fdbd8SKent Overstreet {
6601c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
6611c6fdbd8SKent Overstreet 	struct bch_fs *c;
662401ec4dbSKent Overstreet 	struct printbuf name = PRINTBUF;
663ecf37a4aSKent Overstreet 	unsigned i, iter_size;
6647be9ab63SChris Webb 	int ret = 0;
6651c6fdbd8SKent Overstreet 
6661c6fdbd8SKent Overstreet 	c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO);
6677be9ab63SChris Webb 	if (!c) {
66865d48e35SKent Overstreet 		c = ERR_PTR(-BCH_ERR_ENOMEM_fs_alloc);
6691c6fdbd8SKent Overstreet 		goto out;
6707be9ab63SChris Webb 	}
6711c6fdbd8SKent Overstreet 
6721c6fdbd8SKent Overstreet 	__module_get(THIS_MODULE);
6731c6fdbd8SKent Overstreet 
674505b7a4cSKent Overstreet 	closure_init(&c->cl, NULL);
675505b7a4cSKent Overstreet 
676505b7a4cSKent Overstreet 	c->kobj.kset = bcachefs_kset;
677505b7a4cSKent Overstreet 	kobject_init(&c->kobj, &bch2_fs_ktype);
678505b7a4cSKent Overstreet 	kobject_init(&c->internal, &bch2_fs_internal_ktype);
679505b7a4cSKent Overstreet 	kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype);
680505b7a4cSKent Overstreet 	kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype);
681104c6974SDaniel Hill 	kobject_init(&c->counters_kobj, &bch2_fs_counters_ktype);
682505b7a4cSKent Overstreet 
6831c6fdbd8SKent Overstreet 	c->minor		= -1;
6841c6fdbd8SKent Overstreet 	c->disk_sb.fs_sb	= true;
6851c6fdbd8SKent Overstreet 
6861ada1606SKent Overstreet 	init_rwsem(&c->state_lock);
6871c6fdbd8SKent Overstreet 	mutex_init(&c->sb_lock);
6881c6fdbd8SKent Overstreet 	mutex_init(&c->replicas_gc_lock);
6891c6fdbd8SKent Overstreet 	mutex_init(&c->btree_root_lock);
6901c6fdbd8SKent Overstreet 	INIT_WORK(&c->read_only_work, bch2_fs_read_only_work);
6911c6fdbd8SKent Overstreet 
6921c6fdbd8SKent Overstreet 	init_rwsem(&c->gc_lock);
693c45c8667SKent Overstreet 	mutex_init(&c->gc_gens_lock);
6941c6fdbd8SKent Overstreet 
6951c6fdbd8SKent Overstreet 	for (i = 0; i < BCH_TIME_STAT_NR; i++)
6961c6fdbd8SKent Overstreet 		bch2_time_stats_init(&c->times[i]);
6971c6fdbd8SKent Overstreet 
698e6d11615SKent Overstreet 	bch2_fs_copygc_init(c);
6992ca88e5aSKent Overstreet 	bch2_fs_btree_key_cache_init_early(&c->btree_key_cache);
70065db6049SKent Overstreet 	bch2_fs_btree_interior_update_init_early(c);
701b092daddSKent Overstreet 	bch2_fs_allocator_background_init(c);
702b092daddSKent Overstreet 	bch2_fs_allocator_foreground_init(c);
7031c6fdbd8SKent Overstreet 	bch2_fs_rebalance_init(c);
7041c6fdbd8SKent Overstreet 	bch2_fs_quota_init(c);
70584c72755SKent Overstreet 	bch2_fs_ec_init_early(c);
706b9fa375bSKent Overstreet 	bch2_fs_move_init(c);
7071c6fdbd8SKent Overstreet 
7081c6fdbd8SKent Overstreet 	INIT_LIST_HEAD(&c->list);
7091c6fdbd8SKent Overstreet 
7104d8100daSKent Overstreet 	mutex_init(&c->usage_scratch_lock);
7114d8100daSKent Overstreet 
7121c6fdbd8SKent Overstreet 	mutex_init(&c->bio_bounce_pages_lock);
71314b393eeSKent Overstreet 	mutex_init(&c->snapshot_table_lock);
7141c6fdbd8SKent Overstreet 
7151c6fdbd8SKent Overstreet 	spin_lock_init(&c->btree_write_error_lock);
7161c6fdbd8SKent Overstreet 
7179b6e2f1eSKent Overstreet 	INIT_WORK(&c->journal_seq_blacklist_gc_work,
7189b6e2f1eSKent Overstreet 		  bch2_blacklist_entries_gc);
7199b6e2f1eSKent Overstreet 
7205b593ee1SKent Overstreet 	INIT_LIST_HEAD(&c->journal_iters);
721f1d786a0SKent Overstreet 
7221c6fdbd8SKent Overstreet 	INIT_LIST_HEAD(&c->fsck_errors);
7231c6fdbd8SKent Overstreet 	mutex_init(&c->fsck_error_lock);
7241c6fdbd8SKent Overstreet 
7251c6fdbd8SKent Overstreet 	seqcount_init(&c->gc_pos_lock);
7261c6fdbd8SKent Overstreet 
7275e82a9a1SKent Overstreet 	seqcount_init(&c->usage_lock);
7285e82a9a1SKent Overstreet 
729ef1b2092SKent Overstreet 	sema_init(&c->io_in_flight, 128);
730ef1b2092SKent Overstreet 
7319edbcc72SKent Overstreet 	INIT_LIST_HEAD(&c->vfs_inodes_list);
7329edbcc72SKent Overstreet 	mutex_init(&c->vfs_inodes_lock);
7339edbcc72SKent Overstreet 
7341c6fdbd8SKent Overstreet 	c->copy_gc_enabled		= 1;
7351c6fdbd8SKent Overstreet 	c->rebalance.enabled		= 1;
7361c6fdbd8SKent Overstreet 	c->promote_whole_extents	= true;
7371c6fdbd8SKent Overstreet 
738991ba021SKent Overstreet 	c->journal.flush_write_time	= &c->times[BCH_TIME_journal_flush_write];
739991ba021SKent Overstreet 	c->journal.noflush_write_time	= &c->times[BCH_TIME_journal_noflush_write];
74049a67206SKent Overstreet 	c->journal.blocked_time		= &c->times[BCH_TIME_blocked_journal];
7411c6fdbd8SKent Overstreet 	c->journal.flush_seq_time	= &c->times[BCH_TIME_journal_flush_seq];
7421c6fdbd8SKent Overstreet 
7431c6fdbd8SKent Overstreet 	bch2_fs_btree_cache_init_early(&c->btree_cache);
7441c6fdbd8SKent Overstreet 
745fca1223cSKent Overstreet 	mutex_init(&c->sectors_available_lock);
746fca1223cSKent Overstreet 
747e2b60560SKent Overstreet 	ret = percpu_init_rwsem(&c->mark_lock);
748e2b60560SKent Overstreet 	if (ret)
74973e6ab95SKent Overstreet 		goto err;
75073e6ab95SKent Overstreet 
7511c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
752e2b60560SKent Overstreet 	ret = bch2_sb_to_fs(c, sb);
7531c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
754e2b60560SKent Overstreet 
755e2b60560SKent Overstreet 	if (ret)
7561c6fdbd8SKent Overstreet 		goto err;
7571c6fdbd8SKent Overstreet 
758401ec4dbSKent Overstreet 	pr_uuid(&name, c->sb.user_uuid.b);
7593e3e02e6SKent Overstreet 	strscpy(c->name, name.buf, sizeof(c->name));
760401ec4dbSKent Overstreet 	printbuf_exit(&name);
761401ec4dbSKent Overstreet 
76265d48e35SKent Overstreet 	ret = name.allocation_failure ? -BCH_ERR_ENOMEM_fs_name_alloc : 0;
763401ec4dbSKent Overstreet 	if (ret)
764401ec4dbSKent Overstreet 		goto err;
7651c6fdbd8SKent Overstreet 
7662430e72fSKent Overstreet 	/* Compat: */
76773bd774dSKent Overstreet 	if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 &&
7682430e72fSKent Overstreet 	    !BCH_SB_JOURNAL_FLUSH_DELAY(sb))
7692430e72fSKent Overstreet 		SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000);
7702430e72fSKent Overstreet 
77173bd774dSKent Overstreet 	if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 &&
7722430e72fSKent Overstreet 	    !BCH_SB_JOURNAL_RECLAIM_DELAY(sb))
7732430e72fSKent Overstreet 		SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 100);
7742430e72fSKent Overstreet 
7751c6fdbd8SKent Overstreet 	c->opts = bch2_opts_default;
7768244f320SKent Overstreet 	ret = bch2_opts_from_sb(&c->opts, sb);
7778244f320SKent Overstreet 	if (ret)
7788244f320SKent Overstreet 		goto err;
7798244f320SKent Overstreet 
7801c6fdbd8SKent Overstreet 	bch2_opts_apply(&c->opts, opts);
7811c6fdbd8SKent Overstreet 
7827c8f6f98SKent Overstreet 	c->btree_key_cache_btrees |= 1U << BTREE_ID_alloc;
7837c8f6f98SKent Overstreet 	if (c->opts.inodes_use_key_cache)
7847c8f6f98SKent Overstreet 		c->btree_key_cache_btrees |= 1U << BTREE_ID_inodes;
7857c8f6f98SKent Overstreet 
7868244f320SKent Overstreet 	c->block_bits		= ilog2(block_sectors(c));
7871c6fdbd8SKent Overstreet 	c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
7881c6fdbd8SKent Overstreet 
7897be9ab63SChris Webb 	if (bch2_fs_init_fault("fs_alloc")) {
790e2b60560SKent Overstreet 		bch_err(c, "fs_alloc fault injected");
791e2b60560SKent Overstreet 		ret = -EFAULT;
7921c6fdbd8SKent Overstreet 		goto err;
7937be9ab63SChris Webb 	}
7941c6fdbd8SKent Overstreet 
795ae2f17d5SKent Overstreet 	iter_size = sizeof(struct sort_iter) +
7961c6fdbd8SKent Overstreet 		(btree_blocks(c) + 1) * 2 *
797ae2f17d5SKent Overstreet 		sizeof(struct sort_iter_set);
7981c6fdbd8SKent Overstreet 
799b5e8a699SKent Overstreet 	c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus()));
800b5e8a699SKent Overstreet 
801731bdd2eSKent Overstreet 	if (!(c->btree_update_wq = alloc_workqueue("bcachefs",
802b17d3cecSKent Overstreet 				WQ_FREEZABLE|WQ_UNBOUND|WQ_MEM_RECLAIM, 512)) ||
8039f1833caSKent Overstreet 	    !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io",
8049f2772c4SKent Overstreet 				WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) ||
8052f33ece9SKent Overstreet 	    !(c->copygc_wq = alloc_workqueue("bcachefs_copygc",
8062f33ece9SKent Overstreet 				WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
807731bdd2eSKent Overstreet 	    !(c->io_complete_wq = alloc_workqueue("bcachefs_io",
808731bdd2eSKent Overstreet 				WQ_FREEZABLE|WQ_HIGHPRI|WQ_MEM_RECLAIM, 1)) ||
8098bff9875SBrian Foster 	    !(c->write_ref_wq = alloc_workqueue("bcachefs_write_ref",
8108bff9875SBrian Foster 				WQ_FREEZABLE, 0)) ||
811d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG
812134915f3SKent Overstreet 	    percpu_ref_init(&c->writes, bch2_writes_disabled,
813134915f3SKent Overstreet 			    PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
814d94189adSKent Overstreet #endif
8151c6fdbd8SKent Overstreet 	    mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
8161c6fdbd8SKent Overstreet 	    bioset_init(&c->btree_bio, 1,
8171c6fdbd8SKent Overstreet 			max(offsetof(struct btree_read_bio, bio),
8181c6fdbd8SKent Overstreet 			    offsetof(struct btree_write_bio, wbio.bio)),
8191c6fdbd8SKent Overstreet 			BIOSET_NEED_BVECS) ||
8205663a415SKent Overstreet 	    !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) ||
8215e82a9a1SKent Overstreet 	    !(c->online_reserved = alloc_percpu(u64)) ||
82267e0dd8fSKent Overstreet 	    !(c->btree_paths_bufs = alloc_percpu(struct btree_path_buf)) ||
8231c6fdbd8SKent Overstreet 	    mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1,
8241c6fdbd8SKent Overstreet 					btree_bytes(c)) ||
82535189e09SKent Overstreet 	    mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) ||
826b5e8a699SKent Overstreet 	    !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits,
827e2b60560SKent Overstreet 					      sizeof(u64), GFP_KERNEL))) {
82865d48e35SKent Overstreet 		ret = -BCH_ERR_ENOMEM_fs_other_alloc;
8297be9ab63SChris Webb 		goto err;
8307be9ab63SChris Webb 	}
8317be9ab63SChris Webb 
832f3b8403eSKent Overstreet 	ret = bch2_fs_counters_init(c) ?:
833f3b8403eSKent Overstreet 	    bch2_io_clock_init(&c->io_clock[READ]) ?:
834e2b60560SKent Overstreet 	    bch2_io_clock_init(&c->io_clock[WRITE]) ?:
835e2b60560SKent Overstreet 	    bch2_fs_journal_init(&c->journal) ?:
836e2b60560SKent Overstreet 	    bch2_fs_replicas_init(c) ?:
837e2b60560SKent Overstreet 	    bch2_fs_btree_cache_init(c) ?:
838e2b60560SKent Overstreet 	    bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?:
839e2b60560SKent Overstreet 	    bch2_fs_btree_iter_init(c) ?:
840e2b60560SKent Overstreet 	    bch2_fs_btree_interior_update_init(c) ?:
8419b688da3SKent Overstreet 	    bch2_fs_buckets_waiting_for_journal_init(c) ?:
842920e69bcSKent Overstreet 	    bch2_fs_btree_write_buffer_init(c) ?:
843e2b60560SKent Overstreet 	    bch2_fs_subvolumes_init(c) ?:
844e2b60560SKent Overstreet 	    bch2_fs_io_init(c) ?:
845350175bfSKent Overstreet 	    bch2_fs_nocow_locking_init(c) ?:
846e2b60560SKent Overstreet 	    bch2_fs_encryption_init(c) ?:
847e2b60560SKent Overstreet 	    bch2_fs_compress_init(c) ?:
848e2b60560SKent Overstreet 	    bch2_fs_ec_init(c) ?:
849*dbbfca9fSKent Overstreet 	    bch2_fs_fsio_init(c) ?:
850*dbbfca9fSKent Overstreet 	    bch2_fs_fs_io_buffered_init(c);
851*dbbfca9fSKent Overstreet 	    bch2_fs_fs_io_direct_init(c);
8527be9ab63SChris Webb 	if (ret)
8531c6fdbd8SKent Overstreet 		goto err;
8541c6fdbd8SKent Overstreet 
8551c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
8561c6fdbd8SKent Overstreet 	for (i = 0; i < c->sb.nr_devices; i++)
8571c6fdbd8SKent Overstreet 		if (bch2_dev_exists(c->disk_sb.sb, mi, i) &&
8587be9ab63SChris Webb 		    bch2_dev_alloc(c, i)) {
859e2b60560SKent Overstreet 			ret = -EEXIST;
8601c6fdbd8SKent Overstreet 			goto err;
8617be9ab63SChris Webb 		}
8621c6fdbd8SKent Overstreet 
8634b8f89afSKent Overstreet 	bch2_journal_entry_res_resize(&c->journal,
8644b8f89afSKent Overstreet 			&c->btree_root_journal_res,
8654b8f89afSKent Overstreet 			BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX));
8664b8f89afSKent Overstreet 	bch2_dev_usage_journal_reserve(c);
8674b8f89afSKent Overstreet 	bch2_journal_entry_res_resize(&c->journal,
8684b8f89afSKent Overstreet 			&c->clock_journal_res,
8694b8f89afSKent Overstreet 			(sizeof(struct jset_entry_clock) / sizeof(u64)) * 2);
8704b8f89afSKent Overstreet 
8711c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
872e2b60560SKent Overstreet 	ret = bch2_fs_online(c);
8731c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
874e2b60560SKent Overstreet 
875e2b60560SKent Overstreet 	if (ret)
8761c6fdbd8SKent Overstreet 		goto err;
8771c6fdbd8SKent Overstreet out:
8781c6fdbd8SKent Overstreet 	return c;
8791c6fdbd8SKent Overstreet err:
8801c6fdbd8SKent Overstreet 	bch2_fs_free(c);
8817be9ab63SChris Webb 	c = ERR_PTR(ret);
8821c6fdbd8SKent Overstreet 	goto out;
8831c6fdbd8SKent Overstreet }
8841c6fdbd8SKent Overstreet 
885619f5beeSKent Overstreet noinline_for_stack
886619f5beeSKent Overstreet static void print_mount_opts(struct bch_fs *c)
887619f5beeSKent Overstreet {
888619f5beeSKent Overstreet 	enum bch_opt_id i;
889fa8e94faSKent Overstreet 	struct printbuf p = PRINTBUF;
890619f5beeSKent Overstreet 	bool first = true;
891619f5beeSKent Overstreet 
892ef1634f0SKent Overstreet 	prt_str(&p, "mounting version ");
893e3804b55SKent Overstreet 	bch2_version_to_text(&p, c->sb.version);
89460573ff5SKent Overstreet 
895619f5beeSKent Overstreet 	if (c->opts.read_only) {
89660573ff5SKent Overstreet 		prt_str(&p, " opts=");
897619f5beeSKent Overstreet 		first = false;
89860573ff5SKent Overstreet 		prt_printf(&p, "ro");
899619f5beeSKent Overstreet 	}
900619f5beeSKent Overstreet 
901619f5beeSKent Overstreet 	for (i = 0; i < bch2_opts_nr; i++) {
902619f5beeSKent Overstreet 		const struct bch_option *opt = &bch2_opt_table[i];
903619f5beeSKent Overstreet 		u64 v = bch2_opt_get_by_id(&c->opts, i);
904619f5beeSKent Overstreet 
9058244f320SKent Overstreet 		if (!(opt->flags & OPT_MOUNT))
906619f5beeSKent Overstreet 			continue;
907619f5beeSKent Overstreet 
908619f5beeSKent Overstreet 		if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
909619f5beeSKent Overstreet 			continue;
910619f5beeSKent Overstreet 
91160573ff5SKent Overstreet 		prt_str(&p, first ? " opts=" : ",");
912619f5beeSKent Overstreet 		first = false;
9135521b1dfSKent Overstreet 		bch2_opt_to_text(&p, c, c->disk_sb.sb, opt, v, OPT_SHOW_MOUNT_STYLE);
914619f5beeSKent Overstreet 	}
915619f5beeSKent Overstreet 
91660573ff5SKent Overstreet 	bch_info(c, "%s", p.buf);
917fa8e94faSKent Overstreet 	printbuf_exit(&p);
918619f5beeSKent Overstreet }
919619f5beeSKent Overstreet 
920619f5beeSKent Overstreet int bch2_fs_start(struct bch_fs *c)
9211c6fdbd8SKent Overstreet {
9221c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
9231c6fdbd8SKent Overstreet 	struct bch_dev *ca;
924a420eea6STim Schlueter 	time64_t now = ktime_get_real_seconds();
9251c6fdbd8SKent Overstreet 	unsigned i;
92678c0b75cSKent Overstreet 	int ret;
9271c6fdbd8SKent Overstreet 
928ef1634f0SKent Overstreet 	print_mount_opts(c);
929ef1634f0SKent Overstreet 
9301ada1606SKent Overstreet 	down_write(&c->state_lock);
9311c6fdbd8SKent Overstreet 
932134915f3SKent Overstreet 	BUG_ON(test_bit(BCH_FS_STARTED, &c->flags));
9331c6fdbd8SKent Overstreet 
9341c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
9351c6fdbd8SKent Overstreet 
9361c6fdbd8SKent Overstreet 	for_each_online_member(ca, c, i)
9371c6fdbd8SKent Overstreet 		bch2_sb_from_fs(c, ca);
9381c6fdbd8SKent Overstreet 
9391c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
9401c6fdbd8SKent Overstreet 	for_each_online_member(ca, c, i)
9411c6fdbd8SKent Overstreet 		mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
9421c6fdbd8SKent Overstreet 
9431c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
9441c6fdbd8SKent Overstreet 
9451c6fdbd8SKent Overstreet 	for_each_rw_member(ca, c, i)
9461c6fdbd8SKent Overstreet 		bch2_dev_allocator_add(c, ca);
9471c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
9481c6fdbd8SKent Overstreet 
949bf8f8b20SDaniel Hill 	for (i = 0; i < BCH_TRANSACTIONS_NR; i++) {
950bf8f8b20SDaniel Hill 		mutex_lock(&c->btree_transaction_stats[i].lock);
951bf8f8b20SDaniel Hill 		bch2_time_stats_init(&c->btree_transaction_stats[i].lock_hold_times);
952bf8f8b20SDaniel Hill 		mutex_unlock(&c->btree_transaction_stats[i].lock);
953bf8f8b20SDaniel Hill 	}
954bf8f8b20SDaniel Hill 
9551c6fdbd8SKent Overstreet 	ret = BCH_SB_INITIALIZED(c->disk_sb.sb)
9561c6fdbd8SKent Overstreet 		? bch2_fs_recovery(c)
9571c6fdbd8SKent Overstreet 		: bch2_fs_initialize(c);
9581c6fdbd8SKent Overstreet 	if (ret)
9591c6fdbd8SKent Overstreet 		goto err;
9601c6fdbd8SKent Overstreet 
961cd575ddfSKent Overstreet 	ret = bch2_opts_check_may_set(c);
962cd575ddfSKent Overstreet 	if (ret)
963cd575ddfSKent Overstreet 		goto err;
964cd575ddfSKent Overstreet 
965e2b60560SKent Overstreet 	if (bch2_fs_init_fault("fs_start")) {
966e2b60560SKent Overstreet 		bch_err(c, "fs_start fault injected");
96778c0b75cSKent Overstreet 		ret = -EINVAL;
9681c6fdbd8SKent Overstreet 		goto err;
969e2b60560SKent Overstreet 	}
9701c6fdbd8SKent Overstreet 
971a9310ab0SKent Overstreet 	set_bit(BCH_FS_STARTED, &c->flags);
972a9310ab0SKent Overstreet 
973619f5beeSKent Overstreet 	if (c->opts.read_only || c->opts.nochanges) {
9741c6fdbd8SKent Overstreet 		bch2_fs_read_only(c);
9751c6fdbd8SKent Overstreet 	} else {
976619f5beeSKent Overstreet 		ret = !test_bit(BCH_FS_RW, &c->flags)
977619f5beeSKent Overstreet 			? bch2_fs_read_write(c)
978619f5beeSKent Overstreet 			: bch2_fs_read_write_late(c);
979619f5beeSKent Overstreet 		if (ret)
9801c6fdbd8SKent Overstreet 			goto err;
9811c6fdbd8SKent Overstreet 	}
9821c6fdbd8SKent Overstreet 
983619f5beeSKent Overstreet 	ret = 0;
9841c6fdbd8SKent Overstreet out:
9851ada1606SKent Overstreet 	up_write(&c->state_lock);
986619f5beeSKent Overstreet 	return ret;
9871c6fdbd8SKent Overstreet err:
9881ed0a5d2SKent Overstreet 	bch_err(c, "error starting filesystem: %s", bch2_err_str(ret));
9891c6fdbd8SKent Overstreet 	goto out;
9901c6fdbd8SKent Overstreet }
9911c6fdbd8SKent Overstreet 
99278c0b75cSKent Overstreet static int bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
9931c6fdbd8SKent Overstreet {
9941c6fdbd8SKent Overstreet 	struct bch_sb_field_members *sb_mi;
9951c6fdbd8SKent Overstreet 
9961c6fdbd8SKent Overstreet 	sb_mi = bch2_sb_get_members(sb);
9971c6fdbd8SKent Overstreet 	if (!sb_mi)
99878c0b75cSKent Overstreet 		return -BCH_ERR_member_info_missing;
9991c6fdbd8SKent Overstreet 
10008244f320SKent Overstreet 	if (le16_to_cpu(sb->block_size) != block_sectors(c))
100178c0b75cSKent Overstreet 		return -BCH_ERR_mismatched_block_size;
10021c6fdbd8SKent Overstreet 
10031c6fdbd8SKent Overstreet 	if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
10041c6fdbd8SKent Overstreet 	    BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
100578c0b75cSKent Overstreet 		return -BCH_ERR_bucket_size_too_small;
10061c6fdbd8SKent Overstreet 
100778c0b75cSKent Overstreet 	return 0;
10081c6fdbd8SKent Overstreet }
10091c6fdbd8SKent Overstreet 
101078c0b75cSKent Overstreet static int bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
10111c6fdbd8SKent Overstreet {
10121c6fdbd8SKent Overstreet 	struct bch_sb *newest =
10131c6fdbd8SKent Overstreet 		le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
10141c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
10151c6fdbd8SKent Overstreet 
10161c6fdbd8SKent Overstreet 	if (!uuid_equal(&fs->uuid, &sb->uuid))
101778c0b75cSKent Overstreet 		return -BCH_ERR_device_not_a_member_of_filesystem;
10181c6fdbd8SKent Overstreet 
10191c6fdbd8SKent Overstreet 	if (!bch2_dev_exists(newest, mi, sb->dev_idx))
102078c0b75cSKent Overstreet 		return -BCH_ERR_device_has_been_removed;
10211c6fdbd8SKent Overstreet 
10221c6fdbd8SKent Overstreet 	if (fs->block_size != sb->block_size)
102378c0b75cSKent Overstreet 		return -BCH_ERR_mismatched_block_size;
10241c6fdbd8SKent Overstreet 
102578c0b75cSKent Overstreet 	return 0;
10261c6fdbd8SKent Overstreet }
10271c6fdbd8SKent Overstreet 
10281c6fdbd8SKent Overstreet /* Device startup/shutdown: */
10291c6fdbd8SKent Overstreet 
10301c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *kobj)
10311c6fdbd8SKent Overstreet {
10321c6fdbd8SKent Overstreet 	struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
10331c6fdbd8SKent Overstreet 
10341c6fdbd8SKent Overstreet 	kfree(ca);
10351c6fdbd8SKent Overstreet }
10361c6fdbd8SKent Overstreet 
10371c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *ca)
10381c6fdbd8SKent Overstreet {
10391c6fdbd8SKent Overstreet 	cancel_work_sync(&ca->io_error_work);
10401c6fdbd8SKent Overstreet 
10411c6fdbd8SKent Overstreet 	if (ca->kobj.state_in_sysfs &&
10421c6fdbd8SKent Overstreet 	    ca->disk_sb.bdev)
10431c6fdbd8SKent Overstreet 		sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
10441c6fdbd8SKent Overstreet 
10451c6fdbd8SKent Overstreet 	if (ca->kobj.state_in_sysfs)
10461c6fdbd8SKent Overstreet 		kobject_del(&ca->kobj);
10471c6fdbd8SKent Overstreet 
10481c6fdbd8SKent Overstreet 	bch2_free_super(&ca->disk_sb);
10491c6fdbd8SKent Overstreet 	bch2_dev_journal_exit(ca);
10501c6fdbd8SKent Overstreet 
10511c6fdbd8SKent Overstreet 	free_percpu(ca->io_done);
10521c6fdbd8SKent Overstreet 	bioset_exit(&ca->replica_set);
10531c6fdbd8SKent Overstreet 	bch2_dev_buckets_free(ca);
1054d1170ce5SKent Overstreet 	free_page((unsigned long) ca->sb_read_scratch);
10551c6fdbd8SKent Overstreet 
10561c6fdbd8SKent Overstreet 	bch2_time_stats_exit(&ca->io_latency[WRITE]);
10571c6fdbd8SKent Overstreet 	bch2_time_stats_exit(&ca->io_latency[READ]);
10581c6fdbd8SKent Overstreet 
10591c6fdbd8SKent Overstreet 	percpu_ref_exit(&ca->io_ref);
10601c6fdbd8SKent Overstreet 	percpu_ref_exit(&ca->ref);
10611c6fdbd8SKent Overstreet 	kobject_put(&ca->kobj);
10621c6fdbd8SKent Overstreet }
10631c6fdbd8SKent Overstreet 
10641c6fdbd8SKent Overstreet static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca)
10651c6fdbd8SKent Overstreet {
10661c6fdbd8SKent Overstreet 
10671c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
10681c6fdbd8SKent Overstreet 
10691c6fdbd8SKent Overstreet 	if (percpu_ref_is_zero(&ca->io_ref))
10701c6fdbd8SKent Overstreet 		return;
10711c6fdbd8SKent Overstreet 
10721c6fdbd8SKent Overstreet 	__bch2_dev_read_only(c, ca);
10731c6fdbd8SKent Overstreet 
10741c6fdbd8SKent Overstreet 	reinit_completion(&ca->io_ref_completion);
10751c6fdbd8SKent Overstreet 	percpu_ref_kill(&ca->io_ref);
10761c6fdbd8SKent Overstreet 	wait_for_completion(&ca->io_ref_completion);
10771c6fdbd8SKent Overstreet 
10781c6fdbd8SKent Overstreet 	if (ca->kobj.state_in_sysfs) {
10791c6fdbd8SKent Overstreet 		sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
10801c6fdbd8SKent Overstreet 		sysfs_remove_link(&ca->kobj, "block");
10811c6fdbd8SKent Overstreet 	}
10821c6fdbd8SKent Overstreet 
10831c6fdbd8SKent Overstreet 	bch2_free_super(&ca->disk_sb);
10841c6fdbd8SKent Overstreet 	bch2_dev_journal_exit(ca);
10851c6fdbd8SKent Overstreet }
10861c6fdbd8SKent Overstreet 
10871c6fdbd8SKent Overstreet static void bch2_dev_ref_complete(struct percpu_ref *ref)
10881c6fdbd8SKent Overstreet {
10891c6fdbd8SKent Overstreet 	struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
10901c6fdbd8SKent Overstreet 
10911c6fdbd8SKent Overstreet 	complete(&ca->ref_completion);
10921c6fdbd8SKent Overstreet }
10931c6fdbd8SKent Overstreet 
10941c6fdbd8SKent Overstreet static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
10951c6fdbd8SKent Overstreet {
10961c6fdbd8SKent Overstreet 	struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
10971c6fdbd8SKent Overstreet 
10981c6fdbd8SKent Overstreet 	complete(&ca->io_ref_completion);
10991c6fdbd8SKent Overstreet }
11001c6fdbd8SKent Overstreet 
11011c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca)
11021c6fdbd8SKent Overstreet {
11031c6fdbd8SKent Overstreet 	int ret;
11041c6fdbd8SKent Overstreet 
11051c6fdbd8SKent Overstreet 	if (!c->kobj.state_in_sysfs)
11061c6fdbd8SKent Overstreet 		return 0;
11071c6fdbd8SKent Overstreet 
11081c6fdbd8SKent Overstreet 	if (!ca->kobj.state_in_sysfs) {
11091c6fdbd8SKent Overstreet 		ret = kobject_add(&ca->kobj, &c->kobj,
11101c6fdbd8SKent Overstreet 				  "dev-%u", ca->dev_idx);
11111c6fdbd8SKent Overstreet 		if (ret)
11121c6fdbd8SKent Overstreet 			return ret;
11131c6fdbd8SKent Overstreet 	}
11141c6fdbd8SKent Overstreet 
11151c6fdbd8SKent Overstreet 	if (ca->disk_sb.bdev) {
11161c6fdbd8SKent Overstreet 		struct kobject *block = bdev_kobj(ca->disk_sb.bdev);
11171c6fdbd8SKent Overstreet 
11181c6fdbd8SKent Overstreet 		ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
11191c6fdbd8SKent Overstreet 		if (ret)
11201c6fdbd8SKent Overstreet 			return ret;
11211c6fdbd8SKent Overstreet 
11221c6fdbd8SKent Overstreet 		ret = sysfs_create_link(&ca->kobj, block, "block");
11231c6fdbd8SKent Overstreet 		if (ret)
11241c6fdbd8SKent Overstreet 			return ret;
11251c6fdbd8SKent Overstreet 	}
11261c6fdbd8SKent Overstreet 
11271c6fdbd8SKent Overstreet 	return 0;
11281c6fdbd8SKent Overstreet }
11291c6fdbd8SKent Overstreet 
11301c6fdbd8SKent Overstreet static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c,
11311c6fdbd8SKent Overstreet 					struct bch_member *member)
11321c6fdbd8SKent Overstreet {
11331c6fdbd8SKent Overstreet 	struct bch_dev *ca;
11341c6fdbd8SKent Overstreet 
11351c6fdbd8SKent Overstreet 	ca = kzalloc(sizeof(*ca), GFP_KERNEL);
11361c6fdbd8SKent Overstreet 	if (!ca)
11371c6fdbd8SKent Overstreet 		return NULL;
11381c6fdbd8SKent Overstreet 
11391c6fdbd8SKent Overstreet 	kobject_init(&ca->kobj, &bch2_dev_ktype);
11401c6fdbd8SKent Overstreet 	init_completion(&ca->ref_completion);
11411c6fdbd8SKent Overstreet 	init_completion(&ca->io_ref_completion);
11421c6fdbd8SKent Overstreet 
11431c6fdbd8SKent Overstreet 	init_rwsem(&ca->bucket_lock);
11441c6fdbd8SKent Overstreet 
11451c6fdbd8SKent Overstreet 	INIT_WORK(&ca->io_error_work, bch2_io_error_work);
11461c6fdbd8SKent Overstreet 
11471c6fdbd8SKent Overstreet 	bch2_time_stats_init(&ca->io_latency[READ]);
11481c6fdbd8SKent Overstreet 	bch2_time_stats_init(&ca->io_latency[WRITE]);
11491c6fdbd8SKent Overstreet 
11501c6fdbd8SKent Overstreet 	ca->mi = bch2_mi_to_cpu(member);
11511c6fdbd8SKent Overstreet 	ca->uuid = member->uuid;
11521c6fdbd8SKent Overstreet 
1153f25d8215SKent Overstreet 	ca->nr_btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE,
1154f25d8215SKent Overstreet 			     ca->mi.bucket_size / btree_sectors(c));
1155f25d8215SKent Overstreet 
11561c6fdbd8SKent Overstreet 	if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
11571c6fdbd8SKent Overstreet 			    0, GFP_KERNEL) ||
11581c6fdbd8SKent Overstreet 	    percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
11591c6fdbd8SKent Overstreet 			    PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
1160d1170ce5SKent Overstreet 	    !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) ||
11611c6fdbd8SKent Overstreet 	    bch2_dev_buckets_alloc(c, ca) ||
11621c6fdbd8SKent Overstreet 	    bioset_init(&ca->replica_set, 4,
11631c6fdbd8SKent Overstreet 			offsetof(struct bch_write_bio, bio), 0) ||
11641c6fdbd8SKent Overstreet 	    !(ca->io_done	= alloc_percpu(*ca->io_done)))
11651c6fdbd8SKent Overstreet 		goto err;
11661c6fdbd8SKent Overstreet 
11671c6fdbd8SKent Overstreet 	return ca;
11681c6fdbd8SKent Overstreet err:
11691c6fdbd8SKent Overstreet 	bch2_dev_free(ca);
11701c6fdbd8SKent Overstreet 	return NULL;
11711c6fdbd8SKent Overstreet }
11721c6fdbd8SKent Overstreet 
11731c6fdbd8SKent Overstreet static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca,
11741c6fdbd8SKent Overstreet 			    unsigned dev_idx)
11751c6fdbd8SKent Overstreet {
11761c6fdbd8SKent Overstreet 	ca->dev_idx = dev_idx;
11771c6fdbd8SKent Overstreet 	__set_bit(ca->dev_idx, ca->self.d);
11781c6fdbd8SKent Overstreet 	scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
11791c6fdbd8SKent Overstreet 
11801c6fdbd8SKent Overstreet 	ca->fs = c;
11811c6fdbd8SKent Overstreet 	rcu_assign_pointer(c->devs[ca->dev_idx], ca);
11821c6fdbd8SKent Overstreet 
11831c6fdbd8SKent Overstreet 	if (bch2_dev_sysfs_online(c, ca))
11841c6fdbd8SKent Overstreet 		pr_warn("error creating sysfs objects");
11851c6fdbd8SKent Overstreet }
11861c6fdbd8SKent Overstreet 
11871c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
11881c6fdbd8SKent Overstreet {
11891c6fdbd8SKent Overstreet 	struct bch_member *member =
11901c6fdbd8SKent Overstreet 		bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx;
11911c6fdbd8SKent Overstreet 	struct bch_dev *ca = NULL;
11921c6fdbd8SKent Overstreet 	int ret = 0;
11931c6fdbd8SKent Overstreet 
11941c6fdbd8SKent Overstreet 	if (bch2_fs_init_fault("dev_alloc"))
11951c6fdbd8SKent Overstreet 		goto err;
11961c6fdbd8SKent Overstreet 
11971c6fdbd8SKent Overstreet 	ca = __bch2_dev_alloc(c, member);
11981c6fdbd8SKent Overstreet 	if (!ca)
11991c6fdbd8SKent Overstreet 		goto err;
12001c6fdbd8SKent Overstreet 
1201220d2062SKent Overstreet 	ca->fs = c;
1202220d2062SKent Overstreet 
12031c6fdbd8SKent Overstreet 	bch2_dev_attach(c, ca, dev_idx);
12041c6fdbd8SKent Overstreet 	return ret;
12051c6fdbd8SKent Overstreet err:
12061c6fdbd8SKent Overstreet 	if (ca)
12071c6fdbd8SKent Overstreet 		bch2_dev_free(ca);
1208c8b4534dSKent Overstreet 	return -BCH_ERR_ENOMEM_dev_alloc;
12091c6fdbd8SKent Overstreet }
12101c6fdbd8SKent Overstreet 
12111c6fdbd8SKent Overstreet static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
12121c6fdbd8SKent Overstreet {
12131c6fdbd8SKent Overstreet 	unsigned ret;
12141c6fdbd8SKent Overstreet 
12151c6fdbd8SKent Overstreet 	if (bch2_dev_is_online(ca)) {
12161c6fdbd8SKent Overstreet 		bch_err(ca, "already have device online in slot %u",
12171c6fdbd8SKent Overstreet 			sb->sb->dev_idx);
121878c0b75cSKent Overstreet 		return -BCH_ERR_device_already_online;
12191c6fdbd8SKent Overstreet 	}
12201c6fdbd8SKent Overstreet 
12211c6fdbd8SKent Overstreet 	if (get_capacity(sb->bdev->bd_disk) <
12221c6fdbd8SKent Overstreet 	    ca->mi.bucket_size * ca->mi.nbuckets) {
12231c6fdbd8SKent Overstreet 		bch_err(ca, "cannot online: device too small");
122478c0b75cSKent Overstreet 		return -BCH_ERR_device_size_too_small;
12251c6fdbd8SKent Overstreet 	}
12261c6fdbd8SKent Overstreet 
12271c6fdbd8SKent Overstreet 	BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
12281c6fdbd8SKent Overstreet 
12291c6fdbd8SKent Overstreet 	ret = bch2_dev_journal_init(ca, sb->sb);
12301c6fdbd8SKent Overstreet 	if (ret)
12311c6fdbd8SKent Overstreet 		return ret;
12321c6fdbd8SKent Overstreet 
12331c6fdbd8SKent Overstreet 	/* Commit: */
12341c6fdbd8SKent Overstreet 	ca->disk_sb = *sb;
12351c6fdbd8SKent Overstreet 	memset(sb, 0, sizeof(*sb));
12361c6fdbd8SKent Overstreet 
1237eacb2574SKent Overstreet 	ca->dev = ca->disk_sb.bdev->bd_dev;
1238eacb2574SKent Overstreet 
12391c6fdbd8SKent Overstreet 	percpu_ref_reinit(&ca->io_ref);
12401c6fdbd8SKent Overstreet 
12411c6fdbd8SKent Overstreet 	return 0;
12421c6fdbd8SKent Overstreet }
12431c6fdbd8SKent Overstreet 
12441c6fdbd8SKent Overstreet static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb)
12451c6fdbd8SKent Overstreet {
12461c6fdbd8SKent Overstreet 	struct bch_dev *ca;
12471c6fdbd8SKent Overstreet 	int ret;
12481c6fdbd8SKent Overstreet 
12491c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
12501c6fdbd8SKent Overstreet 
12511c6fdbd8SKent Overstreet 	if (le64_to_cpu(sb->sb->seq) >
12521c6fdbd8SKent Overstreet 	    le64_to_cpu(c->disk_sb.sb->seq))
12531c6fdbd8SKent Overstreet 		bch2_sb_to_fs(c, sb->sb);
12541c6fdbd8SKent Overstreet 
12551c6fdbd8SKent Overstreet 	BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
12561c6fdbd8SKent Overstreet 	       !c->devs[sb->sb->dev_idx]);
12571c6fdbd8SKent Overstreet 
12581c6fdbd8SKent Overstreet 	ca = bch_dev_locked(c, sb->sb->dev_idx);
12591c6fdbd8SKent Overstreet 
12601c6fdbd8SKent Overstreet 	ret = __bch2_dev_attach_bdev(ca, sb);
12611c6fdbd8SKent Overstreet 	if (ret)
12621c6fdbd8SKent Overstreet 		return ret;
12631c6fdbd8SKent Overstreet 
12641c6fdbd8SKent Overstreet 	bch2_dev_sysfs_online(c, ca);
12651c6fdbd8SKent Overstreet 
12661c6fdbd8SKent Overstreet 	if (c->sb.nr_devices == 1)
12671c6fdbd8SKent Overstreet 		snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev);
12681c6fdbd8SKent Overstreet 	snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev);
12691c6fdbd8SKent Overstreet 
12701c6fdbd8SKent Overstreet 	rebalance_wakeup(c);
12711c6fdbd8SKent Overstreet 	return 0;
12721c6fdbd8SKent Overstreet }
12731c6fdbd8SKent Overstreet 
12741c6fdbd8SKent Overstreet /* Device management: */
12751c6fdbd8SKent Overstreet 
12761c6fdbd8SKent Overstreet /*
12771c6fdbd8SKent Overstreet  * Note: this function is also used by the error paths - when a particular
12781c6fdbd8SKent Overstreet  * device sees an error, we call it to determine whether we can just set the
12791c6fdbd8SKent Overstreet  * device RO, or - if this function returns false - we'll set the whole
12801c6fdbd8SKent Overstreet  * filesystem RO:
12811c6fdbd8SKent Overstreet  *
12821c6fdbd8SKent Overstreet  * XXX: maybe we should be more explicit about whether we're changing state
12831c6fdbd8SKent Overstreet  * because we got an error or what have you?
12841c6fdbd8SKent Overstreet  */
12851c6fdbd8SKent Overstreet bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
12861c6fdbd8SKent Overstreet 			    enum bch_member_state new_state, int flags)
12871c6fdbd8SKent Overstreet {
12881c6fdbd8SKent Overstreet 	struct bch_devs_mask new_online_devs;
12891c6fdbd8SKent Overstreet 	struct bch_dev *ca2;
12901c6fdbd8SKent Overstreet 	int i, nr_rw = 0, required;
12911c6fdbd8SKent Overstreet 
12921c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
12931c6fdbd8SKent Overstreet 
12941c6fdbd8SKent Overstreet 	switch (new_state) {
12952436cb9fSKent Overstreet 	case BCH_MEMBER_STATE_rw:
12961c6fdbd8SKent Overstreet 		return true;
12972436cb9fSKent Overstreet 	case BCH_MEMBER_STATE_ro:
12982436cb9fSKent Overstreet 		if (ca->mi.state != BCH_MEMBER_STATE_rw)
12991c6fdbd8SKent Overstreet 			return true;
13001c6fdbd8SKent Overstreet 
13011c6fdbd8SKent Overstreet 		/* do we have enough devices to write to?  */
13021c6fdbd8SKent Overstreet 		for_each_member_device(ca2, c, i)
13031c6fdbd8SKent Overstreet 			if (ca2 != ca)
13042436cb9fSKent Overstreet 				nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw;
13051c6fdbd8SKent Overstreet 
13061c6fdbd8SKent Overstreet 		required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
13071c6fdbd8SKent Overstreet 			       ? c->opts.metadata_replicas
13081c6fdbd8SKent Overstreet 			       : c->opts.metadata_replicas_required,
13091c6fdbd8SKent Overstreet 			       !(flags & BCH_FORCE_IF_DATA_DEGRADED)
13101c6fdbd8SKent Overstreet 			       ? c->opts.data_replicas
13111c6fdbd8SKent Overstreet 			       : c->opts.data_replicas_required);
13121c6fdbd8SKent Overstreet 
13131c6fdbd8SKent Overstreet 		return nr_rw >= required;
13142436cb9fSKent Overstreet 	case BCH_MEMBER_STATE_failed:
13152436cb9fSKent Overstreet 	case BCH_MEMBER_STATE_spare:
13162436cb9fSKent Overstreet 		if (ca->mi.state != BCH_MEMBER_STATE_rw &&
13172436cb9fSKent Overstreet 		    ca->mi.state != BCH_MEMBER_STATE_ro)
13181c6fdbd8SKent Overstreet 			return true;
13191c6fdbd8SKent Overstreet 
13201c6fdbd8SKent Overstreet 		/* do we have enough devices to read from?  */
13211c6fdbd8SKent Overstreet 		new_online_devs = bch2_online_devs(c);
13221c6fdbd8SKent Overstreet 		__clear_bit(ca->dev_idx, new_online_devs.d);
13231c6fdbd8SKent Overstreet 
1324fcb3431bSKent Overstreet 		return bch2_have_enough_devs(c, new_online_devs, flags, false);
13251c6fdbd8SKent Overstreet 	default:
13261c6fdbd8SKent Overstreet 		BUG();
13271c6fdbd8SKent Overstreet 	}
13281c6fdbd8SKent Overstreet }
13291c6fdbd8SKent Overstreet 
13301c6fdbd8SKent Overstreet static bool bch2_fs_may_start(struct bch_fs *c)
13311c6fdbd8SKent Overstreet {
13321c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
13331c6fdbd8SKent Overstreet 	struct bch_dev *ca;
1334fcb3431bSKent Overstreet 	unsigned i, flags = 0;
13351c6fdbd8SKent Overstreet 
1336fcb3431bSKent Overstreet 	if (c->opts.very_degraded)
1337fcb3431bSKent Overstreet 		flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST;
1338fcb3431bSKent Overstreet 
1339fcb3431bSKent Overstreet 	if (c->opts.degraded)
1340fcb3431bSKent Overstreet 		flags |= BCH_FORCE_IF_DEGRADED;
1341fcb3431bSKent Overstreet 
1342fcb3431bSKent Overstreet 	if (!c->opts.degraded &&
1343fcb3431bSKent Overstreet 	    !c->opts.very_degraded) {
13441c6fdbd8SKent Overstreet 		mutex_lock(&c->sb_lock);
13451c6fdbd8SKent Overstreet 		mi = bch2_sb_get_members(c->disk_sb.sb);
13461c6fdbd8SKent Overstreet 
13471c6fdbd8SKent Overstreet 		for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
13481c6fdbd8SKent Overstreet 			if (!bch2_dev_exists(c->disk_sb.sb, mi, i))
13491c6fdbd8SKent Overstreet 				continue;
13501c6fdbd8SKent Overstreet 
13511c6fdbd8SKent Overstreet 			ca = bch_dev_locked(c, i);
13521c6fdbd8SKent Overstreet 
13531c6fdbd8SKent Overstreet 			if (!bch2_dev_is_online(ca) &&
13542436cb9fSKent Overstreet 			    (ca->mi.state == BCH_MEMBER_STATE_rw ||
13552436cb9fSKent Overstreet 			     ca->mi.state == BCH_MEMBER_STATE_ro)) {
13561c6fdbd8SKent Overstreet 				mutex_unlock(&c->sb_lock);
13571c6fdbd8SKent Overstreet 				return false;
13581c6fdbd8SKent Overstreet 			}
13591c6fdbd8SKent Overstreet 		}
13601c6fdbd8SKent Overstreet 		mutex_unlock(&c->sb_lock);
13611c6fdbd8SKent Overstreet 	}
13621c6fdbd8SKent Overstreet 
1363fcb3431bSKent Overstreet 	return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true);
13641c6fdbd8SKent Overstreet }
13651c6fdbd8SKent Overstreet 
13661c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
13671c6fdbd8SKent Overstreet {
13681c6fdbd8SKent Overstreet 	/*
13691c6fdbd8SKent Overstreet 	 * The allocator thread itself allocates btree nodes, so stop it first:
13701c6fdbd8SKent Overstreet 	 */
13711c6fdbd8SKent Overstreet 	bch2_dev_allocator_remove(c, ca);
13721c6fdbd8SKent Overstreet 	bch2_dev_journal_stop(&c->journal, ca);
13731c6fdbd8SKent Overstreet }
13741c6fdbd8SKent Overstreet 
1375f25d8215SKent Overstreet static void __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
13761c6fdbd8SKent Overstreet {
13771c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
13781c6fdbd8SKent Overstreet 
13792436cb9fSKent Overstreet 	BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw);
13801c6fdbd8SKent Overstreet 
13811c6fdbd8SKent Overstreet 	bch2_dev_allocator_add(c, ca);
13821c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
13831c6fdbd8SKent Overstreet }
13841c6fdbd8SKent Overstreet 
13851c6fdbd8SKent Overstreet int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
13861c6fdbd8SKent Overstreet 			 enum bch_member_state new_state, int flags)
13871c6fdbd8SKent Overstreet {
13881c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
13891c6fdbd8SKent Overstreet 	int ret = 0;
13901c6fdbd8SKent Overstreet 
13911c6fdbd8SKent Overstreet 	if (ca->mi.state == new_state)
13921c6fdbd8SKent Overstreet 		return 0;
13931c6fdbd8SKent Overstreet 
13941c6fdbd8SKent Overstreet 	if (!bch2_dev_state_allowed(c, ca, new_state, flags))
139578c0b75cSKent Overstreet 		return -BCH_ERR_device_state_not_allowed;
13961c6fdbd8SKent Overstreet 
13972436cb9fSKent Overstreet 	if (new_state != BCH_MEMBER_STATE_rw)
13981c6fdbd8SKent Overstreet 		__bch2_dev_read_only(c, ca);
13991c6fdbd8SKent Overstreet 
14002436cb9fSKent Overstreet 	bch_notice(ca, "%s", bch2_member_states[new_state]);
14011c6fdbd8SKent Overstreet 
14021c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
14031c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
14041c6fdbd8SKent Overstreet 	SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
14051c6fdbd8SKent Overstreet 	bch2_write_super(c);
14061c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
14071c6fdbd8SKent Overstreet 
1408e2b60560SKent Overstreet 	if (new_state == BCH_MEMBER_STATE_rw)
1409f25d8215SKent Overstreet 		__bch2_dev_read_write(c, ca);
14101c6fdbd8SKent Overstreet 
14111c6fdbd8SKent Overstreet 	rebalance_wakeup(c);
14121c6fdbd8SKent Overstreet 
14131c6fdbd8SKent Overstreet 	return ret;
14141c6fdbd8SKent Overstreet }
14151c6fdbd8SKent Overstreet 
14161c6fdbd8SKent Overstreet int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
14171c6fdbd8SKent Overstreet 		       enum bch_member_state new_state, int flags)
14181c6fdbd8SKent Overstreet {
14191c6fdbd8SKent Overstreet 	int ret;
14201c6fdbd8SKent Overstreet 
14211ada1606SKent Overstreet 	down_write(&c->state_lock);
14221c6fdbd8SKent Overstreet 	ret = __bch2_dev_set_state(c, ca, new_state, flags);
14231ada1606SKent Overstreet 	up_write(&c->state_lock);
14241c6fdbd8SKent Overstreet 
14251c6fdbd8SKent Overstreet 	return ret;
14261c6fdbd8SKent Overstreet }
14271c6fdbd8SKent Overstreet 
14281c6fdbd8SKent Overstreet /* Device add/removal: */
14291c6fdbd8SKent Overstreet 
1430c0ebe3e4SKent Overstreet static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca)
14315d20ba48SKent Overstreet {
1432c6b2826cSKent Overstreet 	struct bpos start	= POS(ca->dev_idx, 0);
1433c6b2826cSKent Overstreet 	struct bpos end		= POS(ca->dev_idx, U64_MAX);
14345d20ba48SKent Overstreet 	int ret;
14355d20ba48SKent Overstreet 
1436a9c0a4cbSKent Overstreet 	/*
1437a9c0a4cbSKent Overstreet 	 * We clear the LRU and need_discard btrees first so that we don't race
1438a9c0a4cbSKent Overstreet 	 * with bch2_do_invalidates() and bch2_do_discards()
1439a9c0a4cbSKent Overstreet 	 */
1440a9c0a4cbSKent Overstreet 	ret =   bch2_btree_delete_range(c, BTREE_ID_lru, start, end,
1441a9c0a4cbSKent Overstreet 					BTREE_TRIGGER_NORUN, NULL) ?:
1442a9c0a4cbSKent Overstreet 		bch2_btree_delete_range(c, BTREE_ID_need_discard, start, end,
1443c6b2826cSKent Overstreet 					BTREE_TRIGGER_NORUN, NULL) ?:
1444c6b2826cSKent Overstreet 		bch2_btree_delete_range(c, BTREE_ID_freespace, start, end,
1445c6b2826cSKent Overstreet 					BTREE_TRIGGER_NORUN, NULL) ?:
1446a8c752bbSKent Overstreet 		bch2_btree_delete_range(c, BTREE_ID_backpointers, start, end,
1447a8c752bbSKent Overstreet 					BTREE_TRIGGER_NORUN, NULL) ?:
1448a9c0a4cbSKent Overstreet 		bch2_btree_delete_range(c, BTREE_ID_alloc, start, end,
144902d51bb9SBrian Foster 					BTREE_TRIGGER_NORUN, NULL) ?:
145002d51bb9SBrian Foster 		bch2_btree_delete_range(c, BTREE_ID_bucket_gens, start, end,
1451c6b2826cSKent Overstreet 					BTREE_TRIGGER_NORUN, NULL);
14525d20ba48SKent Overstreet 	if (ret)
1453d4bf5eecSKent Overstreet 		bch_err(c, "error removing dev alloc info: %s", bch2_err_str(ret));
14545d20ba48SKent Overstreet 
1455c6b2826cSKent Overstreet 	return ret;
14565d20ba48SKent Overstreet }
14575d20ba48SKent Overstreet 
14581c6fdbd8SKent Overstreet int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
14591c6fdbd8SKent Overstreet {
14601c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
14611c6fdbd8SKent Overstreet 	unsigned dev_idx = ca->dev_idx, data;
146278c0b75cSKent Overstreet 	int ret;
14631c6fdbd8SKent Overstreet 
14641ada1606SKent Overstreet 	down_write(&c->state_lock);
14651c6fdbd8SKent Overstreet 
146631ba2cd3SKent Overstreet 	/*
146731ba2cd3SKent Overstreet 	 * We consume a reference to ca->ref, regardless of whether we succeed
146831ba2cd3SKent Overstreet 	 * or fail:
146931ba2cd3SKent Overstreet 	 */
147031ba2cd3SKent Overstreet 	percpu_ref_put(&ca->ref);
14711c6fdbd8SKent Overstreet 
14722436cb9fSKent Overstreet 	if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
14731c6fdbd8SKent Overstreet 		bch_err(ca, "Cannot remove without losing data");
147478c0b75cSKent Overstreet 		ret = -BCH_ERR_device_state_not_allowed;
14751c6fdbd8SKent Overstreet 		goto err;
14761c6fdbd8SKent Overstreet 	}
14771c6fdbd8SKent Overstreet 
14781c6fdbd8SKent Overstreet 	__bch2_dev_read_only(c, ca);
14791c6fdbd8SKent Overstreet 
14801c6fdbd8SKent Overstreet 	ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
14811c6fdbd8SKent Overstreet 	if (ret) {
1482d4bf5eecSKent Overstreet 		bch_err(ca, "Remove failed: error dropping data: %s", bch2_err_str(ret));
14831c6fdbd8SKent Overstreet 		goto err;
14841c6fdbd8SKent Overstreet 	}
14851c6fdbd8SKent Overstreet 
14865d20ba48SKent Overstreet 	ret = bch2_dev_remove_alloc(c, ca);
14871c6fdbd8SKent Overstreet 	if (ret) {
14881c6fdbd8SKent Overstreet 		bch_err(ca, "Remove failed, error deleting alloc info");
14891c6fdbd8SKent Overstreet 		goto err;
14901c6fdbd8SKent Overstreet 	}
14911c6fdbd8SKent Overstreet 
1492a9c0a4cbSKent Overstreet 	ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx);
1493a9c0a4cbSKent Overstreet 	if (ret) {
1494d4bf5eecSKent Overstreet 		bch_err(ca, "Remove failed: error flushing journal: %s", bch2_err_str(ret));
1495a9c0a4cbSKent Overstreet 		goto err;
1496a9c0a4cbSKent Overstreet 	}
1497a9c0a4cbSKent Overstreet 
1498a9c0a4cbSKent Overstreet 	ret = bch2_journal_flush(&c->journal);
14991c6fdbd8SKent Overstreet 	if (ret) {
15001c6fdbd8SKent Overstreet 		bch_err(ca, "Remove failed, journal error");
15011c6fdbd8SKent Overstreet 		goto err;
15021c6fdbd8SKent Overstreet 	}
15031c6fdbd8SKent Overstreet 
150431ba2cd3SKent Overstreet 	ret = bch2_replicas_gc2(c);
150531ba2cd3SKent Overstreet 	if (ret) {
1506d4bf5eecSKent Overstreet 		bch_err(ca, "Remove failed: error from replicas gc: %s", bch2_err_str(ret));
150731ba2cd3SKent Overstreet 		goto err;
150831ba2cd3SKent Overstreet 	}
150931ba2cd3SKent Overstreet 
151031ba2cd3SKent Overstreet 	data = bch2_dev_has_data(c, ca);
151131ba2cd3SKent Overstreet 	if (data) {
1512fa8e94faSKent Overstreet 		struct printbuf data_has = PRINTBUF;
151331ba2cd3SKent Overstreet 
1514401ec4dbSKent Overstreet 		prt_bitflags(&data_has, bch2_data_types, data);
1515fa8e94faSKent Overstreet 		bch_err(ca, "Remove failed, still has data (%s)", data_has.buf);
1516fa8e94faSKent Overstreet 		printbuf_exit(&data_has);
151731ba2cd3SKent Overstreet 		ret = -EBUSY;
151831ba2cd3SKent Overstreet 		goto err;
151931ba2cd3SKent Overstreet 	}
152031ba2cd3SKent Overstreet 
15211c6fdbd8SKent Overstreet 	__bch2_dev_offline(c, ca);
15221c6fdbd8SKent Overstreet 
15231c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
15241c6fdbd8SKent Overstreet 	rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
15251c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
15261c6fdbd8SKent Overstreet 
15271c6fdbd8SKent Overstreet 	percpu_ref_kill(&ca->ref);
15281c6fdbd8SKent Overstreet 	wait_for_completion(&ca->ref_completion);
15291c6fdbd8SKent Overstreet 
15301c6fdbd8SKent Overstreet 	bch2_dev_free(ca);
15311c6fdbd8SKent Overstreet 
15321c6fdbd8SKent Overstreet 	/*
1533bc652905SBrian Foster 	 * At this point the device object has been removed in-core, but the
1534bc652905SBrian Foster 	 * on-disk journal might still refer to the device index via sb device
1535bc652905SBrian Foster 	 * usage entries. Recovery fails if it sees usage information for an
1536bc652905SBrian Foster 	 * invalid device. Flush journal pins to push the back of the journal
1537bc652905SBrian Foster 	 * past now invalid device index references before we update the
1538bc652905SBrian Foster 	 * superblock, but after the device object has been removed so any
1539bc652905SBrian Foster 	 * further journal writes elide usage info for the device.
1540bc652905SBrian Foster 	 */
1541bc652905SBrian Foster 	bch2_journal_flush_all_pins(&c->journal);
1542bc652905SBrian Foster 
1543bc652905SBrian Foster 	/*
15441c6fdbd8SKent Overstreet 	 * Free this device's slot in the bch_member array - all pointers to
15451c6fdbd8SKent Overstreet 	 * this device must be gone:
15461c6fdbd8SKent Overstreet 	 */
15471c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
15481c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
15491c6fdbd8SKent Overstreet 	memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
15501c6fdbd8SKent Overstreet 
15511c6fdbd8SKent Overstreet 	bch2_write_super(c);
15521c6fdbd8SKent Overstreet 
15531c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
15541ada1606SKent Overstreet 	up_write(&c->state_lock);
1555180fb49dSKent Overstreet 
1556180fb49dSKent Overstreet 	bch2_dev_usage_journal_reserve(c);
15571c6fdbd8SKent Overstreet 	return 0;
15581c6fdbd8SKent Overstreet err:
15592436cb9fSKent Overstreet 	if (ca->mi.state == BCH_MEMBER_STATE_rw &&
1560d3bb629dSKent Overstreet 	    !percpu_ref_is_zero(&ca->io_ref))
15611c6fdbd8SKent Overstreet 		__bch2_dev_read_write(c, ca);
15621ada1606SKent Overstreet 	up_write(&c->state_lock);
15631c6fdbd8SKent Overstreet 	return ret;
15641c6fdbd8SKent Overstreet }
15651c6fdbd8SKent Overstreet 
15661c6fdbd8SKent Overstreet /* Add new device to running filesystem: */
15671c6fdbd8SKent Overstreet int bch2_dev_add(struct bch_fs *c, const char *path)
15681c6fdbd8SKent Overstreet {
15691c6fdbd8SKent Overstreet 	struct bch_opts opts = bch2_opts_empty();
15701c6fdbd8SKent Overstreet 	struct bch_sb_handle sb;
15711c6fdbd8SKent Overstreet 	struct bch_dev *ca = NULL;
15721c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
15731c6fdbd8SKent Overstreet 	struct bch_member dev_mi;
15741c6fdbd8SKent Overstreet 	unsigned dev_idx, nr_devices, u64s;
1575fa8e94faSKent Overstreet 	struct printbuf errbuf = PRINTBUF;
157602afcb8cSKent Overstreet 	struct printbuf label = PRINTBUF;
15771c6fdbd8SKent Overstreet 	int ret;
15781c6fdbd8SKent Overstreet 
15791c6fdbd8SKent Overstreet 	ret = bch2_read_super(path, &opts, &sb);
1580e8536925SKent Overstreet 	if (ret) {
1581d4bf5eecSKent Overstreet 		bch_err(c, "device add error: error reading super: %s", bch2_err_str(ret));
1582efe68e1dSKent Overstreet 		goto err;
1583e8536925SKent Overstreet 	}
15841c6fdbd8SKent Overstreet 
15851c6fdbd8SKent Overstreet 	dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx];
15861c6fdbd8SKent Overstreet 
158702afcb8cSKent Overstreet 	if (BCH_MEMBER_GROUP(&dev_mi)) {
158802afcb8cSKent Overstreet 		bch2_disk_path_to_text(&label, sb.sb, BCH_MEMBER_GROUP(&dev_mi) - 1);
158902afcb8cSKent Overstreet 		if (label.allocation_failure) {
159002afcb8cSKent Overstreet 			ret = -ENOMEM;
159102afcb8cSKent Overstreet 			goto err;
159202afcb8cSKent Overstreet 		}
159302afcb8cSKent Overstreet 	}
159402afcb8cSKent Overstreet 
159578c0b75cSKent Overstreet 	ret = bch2_dev_may_add(sb.sb, c);
159678c0b75cSKent Overstreet 	if (ret) {
159778c0b75cSKent Overstreet 		bch_err(c, "device add error: %s", bch2_err_str(ret));
1598efe68e1dSKent Overstreet 		goto err;
1599e8536925SKent Overstreet 	}
16001c6fdbd8SKent Overstreet 
16011c6fdbd8SKent Overstreet 	ca = __bch2_dev_alloc(c, &dev_mi);
16021c6fdbd8SKent Overstreet 	if (!ca) {
16031c6fdbd8SKent Overstreet 		bch2_free_super(&sb);
1604efe68e1dSKent Overstreet 		ret = -ENOMEM;
1605efe68e1dSKent Overstreet 		goto err;
16061c6fdbd8SKent Overstreet 	}
16071c6fdbd8SKent Overstreet 
1608822835ffSKent Overstreet 	bch2_dev_usage_init(ca);
1609822835ffSKent Overstreet 
16101c6fdbd8SKent Overstreet 	ret = __bch2_dev_attach_bdev(ca, &sb);
16111c6fdbd8SKent Overstreet 	if (ret) {
16121c6fdbd8SKent Overstreet 		bch2_dev_free(ca);
1613efe68e1dSKent Overstreet 		goto err;
16141c6fdbd8SKent Overstreet 	}
16151c6fdbd8SKent Overstreet 
16161c6fdbd8SKent Overstreet 	ret = bch2_dev_journal_alloc(ca);
1617e8536925SKent Overstreet 	if (ret) {
1618e8536925SKent Overstreet 		bch_err(c, "device add error: journal alloc failed");
16191c6fdbd8SKent Overstreet 		goto err;
1620e8536925SKent Overstreet 	}
16211c6fdbd8SKent Overstreet 
16221ada1606SKent Overstreet 	down_write(&c->state_lock);
16231c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
16241c6fdbd8SKent Overstreet 
16251c6fdbd8SKent Overstreet 	ret = bch2_sb_from_fs(c, ca);
1626e8536925SKent Overstreet 	if (ret) {
1627e8536925SKent Overstreet 		bch_err(c, "device add error: new device superblock too small");
16281c6fdbd8SKent Overstreet 		goto err_unlock;
1629e8536925SKent Overstreet 	}
16301c6fdbd8SKent Overstreet 
16311c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(ca->disk_sb.sb);
16321c6fdbd8SKent Overstreet 
16331c6fdbd8SKent Overstreet 	if (!bch2_sb_resize_members(&ca->disk_sb,
16341c6fdbd8SKent Overstreet 				le32_to_cpu(mi->field.u64s) +
16351c6fdbd8SKent Overstreet 				sizeof(dev_mi) / sizeof(u64))) {
1636e8536925SKent Overstreet 		bch_err(c, "device add error: new device superblock too small");
1637098ef98dSKent Overstreet 		ret = -BCH_ERR_ENOSPC_sb_members;
16381c6fdbd8SKent Overstreet 		goto err_unlock;
16391c6fdbd8SKent Overstreet 	}
16401c6fdbd8SKent Overstreet 
16411c6fdbd8SKent Overstreet 	if (dynamic_fault("bcachefs:add:no_slot"))
16421c6fdbd8SKent Overstreet 		goto no_slot;
16431c6fdbd8SKent Overstreet 
16441c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
16451c6fdbd8SKent Overstreet 	for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
16461c6fdbd8SKent Overstreet 		if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx))
16471c6fdbd8SKent Overstreet 			goto have_slot;
16481c6fdbd8SKent Overstreet no_slot:
1649e8536925SKent Overstreet 	bch_err(c, "device add error: already have maximum number of devices");
1650098ef98dSKent Overstreet 	ret = -BCH_ERR_ENOSPC_sb_members;
16511c6fdbd8SKent Overstreet 	goto err_unlock;
16521c6fdbd8SKent Overstreet 
16531c6fdbd8SKent Overstreet have_slot:
16541c6fdbd8SKent Overstreet 	nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
16551c6fdbd8SKent Overstreet 	u64s = (sizeof(struct bch_sb_field_members) +
16561c6fdbd8SKent Overstreet 		sizeof(struct bch_member) * nr_devices) / sizeof(u64);
16571c6fdbd8SKent Overstreet 
16581c6fdbd8SKent Overstreet 	mi = bch2_sb_resize_members(&c->disk_sb, u64s);
1659e8536925SKent Overstreet 	if (!mi) {
1660e8536925SKent Overstreet 		bch_err(c, "device add error: no room in superblock for member info");
1661098ef98dSKent Overstreet 		ret = -BCH_ERR_ENOSPC_sb_members;
16621c6fdbd8SKent Overstreet 		goto err_unlock;
1663e8536925SKent Overstreet 	}
16641c6fdbd8SKent Overstreet 
16651c6fdbd8SKent Overstreet 	/* success: */
16661c6fdbd8SKent Overstreet 
16671c6fdbd8SKent Overstreet 	mi->members[dev_idx] = dev_mi;
1668a420eea6STim Schlueter 	mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds());
16691c6fdbd8SKent Overstreet 	c->disk_sb.sb->nr_devices	= nr_devices;
16701c6fdbd8SKent Overstreet 
16711c6fdbd8SKent Overstreet 	ca->disk_sb.sb->dev_idx	= dev_idx;
16721c6fdbd8SKent Overstreet 	bch2_dev_attach(c, ca, dev_idx);
16731c6fdbd8SKent Overstreet 
167402afcb8cSKent Overstreet 	if (BCH_MEMBER_GROUP(&dev_mi)) {
167502afcb8cSKent Overstreet 		ret = __bch2_dev_group_set(c, ca, label.buf);
167602afcb8cSKent Overstreet 		if (ret) {
167702afcb8cSKent Overstreet 			bch_err(c, "device add error: error setting label");
167802afcb8cSKent Overstreet 			goto err_unlock;
167902afcb8cSKent Overstreet 		}
168002afcb8cSKent Overstreet 	}
168102afcb8cSKent Overstreet 
16821c6fdbd8SKent Overstreet 	bch2_write_super(c);
16831c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
16841c6fdbd8SKent Overstreet 
1685180fb49dSKent Overstreet 	bch2_dev_usage_journal_reserve(c);
1686180fb49dSKent Overstreet 
1687d62ab355SKent Overstreet 	ret = bch2_trans_mark_dev_sb(c, ca);
1688e8536925SKent Overstreet 	if (ret) {
1689d4bf5eecSKent Overstreet 		bch_err(c, "device add error: error marking new superblock: %s", bch2_err_str(ret));
1690bfcf840dSKent Overstreet 		goto err_late;
1691e8536925SKent Overstreet 	}
16928d6b6222SKent Overstreet 
1693c6b2826cSKent Overstreet 	ret = bch2_fs_freespace_init(c);
1694c6b2826cSKent Overstreet 	if (ret) {
1695d4bf5eecSKent Overstreet 		bch_err(c, "device add error: error initializing free space: %s", bch2_err_str(ret));
1696c6b2826cSKent Overstreet 		goto err_late;
1697c6b2826cSKent Overstreet 	}
1698c6b2826cSKent Overstreet 
169909943313SKent Overstreet 	ca->new_fs_bucket_idx = 0;
170009943313SKent Overstreet 
1701f25d8215SKent Overstreet 	if (ca->mi.state == BCH_MEMBER_STATE_rw)
1702f25d8215SKent Overstreet 		__bch2_dev_read_write(c, ca);
17031c6fdbd8SKent Overstreet 
17041ada1606SKent Overstreet 	up_write(&c->state_lock);
17051c6fdbd8SKent Overstreet 	return 0;
17061c6fdbd8SKent Overstreet 
17071c6fdbd8SKent Overstreet err_unlock:
17081c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
17091ada1606SKent Overstreet 	up_write(&c->state_lock);
17101c6fdbd8SKent Overstreet err:
17111c6fdbd8SKent Overstreet 	if (ca)
17121c6fdbd8SKent Overstreet 		bch2_dev_free(ca);
17131c6fdbd8SKent Overstreet 	bch2_free_super(&sb);
171402afcb8cSKent Overstreet 	printbuf_exit(&label);
1715fa8e94faSKent Overstreet 	printbuf_exit(&errbuf);
17161c6fdbd8SKent Overstreet 	return ret;
17171c6fdbd8SKent Overstreet err_late:
1718bfcf840dSKent Overstreet 	up_write(&c->state_lock);
1719efe68e1dSKent Overstreet 	ca = NULL;
1720efe68e1dSKent Overstreet 	goto err;
17211c6fdbd8SKent Overstreet }
17221c6fdbd8SKent Overstreet 
17231c6fdbd8SKent Overstreet /* Hot add existing device to running filesystem: */
17241c6fdbd8SKent Overstreet int bch2_dev_online(struct bch_fs *c, const char *path)
17251c6fdbd8SKent Overstreet {
17261c6fdbd8SKent Overstreet 	struct bch_opts opts = bch2_opts_empty();
17271c6fdbd8SKent Overstreet 	struct bch_sb_handle sb = { NULL };
17281c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
17291c6fdbd8SKent Overstreet 	struct bch_dev *ca;
17301c6fdbd8SKent Overstreet 	unsigned dev_idx;
17311c6fdbd8SKent Overstreet 	int ret;
17321c6fdbd8SKent Overstreet 
17331ada1606SKent Overstreet 	down_write(&c->state_lock);
17341c6fdbd8SKent Overstreet 
17351c6fdbd8SKent Overstreet 	ret = bch2_read_super(path, &opts, &sb);
17361c6fdbd8SKent Overstreet 	if (ret) {
17371ada1606SKent Overstreet 		up_write(&c->state_lock);
17381c6fdbd8SKent Overstreet 		return ret;
17391c6fdbd8SKent Overstreet 	}
17401c6fdbd8SKent Overstreet 
17411c6fdbd8SKent Overstreet 	dev_idx = sb.sb->dev_idx;
17421c6fdbd8SKent Overstreet 
174378c0b75cSKent Overstreet 	ret = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
174478c0b75cSKent Overstreet 	if (ret) {
174578c0b75cSKent Overstreet 		bch_err(c, "error bringing %s online: %s", path, bch2_err_str(ret));
17461c6fdbd8SKent Overstreet 		goto err;
17471c6fdbd8SKent Overstreet 	}
17481c6fdbd8SKent Overstreet 
1749e2b60560SKent Overstreet 	ret = bch2_dev_attach_bdev(c, &sb);
1750e2b60560SKent Overstreet 	if (ret)
1751e2b60560SKent Overstreet 		goto err;
1752e2b60560SKent Overstreet 
17531c6fdbd8SKent Overstreet 	ca = bch_dev_locked(c, dev_idx);
1754bfcf840dSKent Overstreet 
1755e2b60560SKent Overstreet 	ret = bch2_trans_mark_dev_sb(c, ca);
1756e2b60560SKent Overstreet 	if (ret) {
1757d4bf5eecSKent Overstreet 		bch_err(c, "error bringing %s online: error from bch2_trans_mark_dev_sb: %s",
1758d4bf5eecSKent Overstreet 			path, bch2_err_str(ret));
1759bfcf840dSKent Overstreet 		goto err;
1760bfcf840dSKent Overstreet 	}
1761bfcf840dSKent Overstreet 
1762f25d8215SKent Overstreet 	if (ca->mi.state == BCH_MEMBER_STATE_rw)
1763f25d8215SKent Overstreet 		__bch2_dev_read_write(c, ca);
17641c6fdbd8SKent Overstreet 
17651c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
17661c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
17671c6fdbd8SKent Overstreet 
17681c6fdbd8SKent Overstreet 	mi->members[ca->dev_idx].last_mount =
1769a420eea6STim Schlueter 		cpu_to_le64(ktime_get_real_seconds());
17701c6fdbd8SKent Overstreet 
17711c6fdbd8SKent Overstreet 	bch2_write_super(c);
17721c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
17731c6fdbd8SKent Overstreet 
1774b1c945b3SKent Overstreet 	ret = bch2_fs_freespace_init(c);
1775b1c945b3SKent Overstreet 	if (ret)
1776b1c945b3SKent Overstreet 		bch_err(c, "device add error: error initializing free space: %s", bch2_err_str(ret));
1777b1c945b3SKent Overstreet 
17781ada1606SKent Overstreet 	up_write(&c->state_lock);
17791c6fdbd8SKent Overstreet 	return 0;
17801c6fdbd8SKent Overstreet err:
17811ada1606SKent Overstreet 	up_write(&c->state_lock);
17821c6fdbd8SKent Overstreet 	bch2_free_super(&sb);
178378c0b75cSKent Overstreet 	return ret;
17841c6fdbd8SKent Overstreet }
17851c6fdbd8SKent Overstreet 
17861c6fdbd8SKent Overstreet int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
17871c6fdbd8SKent Overstreet {
17881ada1606SKent Overstreet 	down_write(&c->state_lock);
17891c6fdbd8SKent Overstreet 
17901c6fdbd8SKent Overstreet 	if (!bch2_dev_is_online(ca)) {
17911c6fdbd8SKent Overstreet 		bch_err(ca, "Already offline");
17921ada1606SKent Overstreet 		up_write(&c->state_lock);
17931c6fdbd8SKent Overstreet 		return 0;
17941c6fdbd8SKent Overstreet 	}
17951c6fdbd8SKent Overstreet 
17962436cb9fSKent Overstreet 	if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
17971c6fdbd8SKent Overstreet 		bch_err(ca, "Cannot offline required disk");
17981ada1606SKent Overstreet 		up_write(&c->state_lock);
179978c0b75cSKent Overstreet 		return -BCH_ERR_device_state_not_allowed;
18001c6fdbd8SKent Overstreet 	}
18011c6fdbd8SKent Overstreet 
18021c6fdbd8SKent Overstreet 	__bch2_dev_offline(c, ca);
18031c6fdbd8SKent Overstreet 
18041ada1606SKent Overstreet 	up_write(&c->state_lock);
18051c6fdbd8SKent Overstreet 	return 0;
18061c6fdbd8SKent Overstreet }
18071c6fdbd8SKent Overstreet 
18081c6fdbd8SKent Overstreet int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
18091c6fdbd8SKent Overstreet {
18101c6fdbd8SKent Overstreet 	struct bch_member *mi;
18111c6fdbd8SKent Overstreet 	int ret = 0;
18121c6fdbd8SKent Overstreet 
18131ada1606SKent Overstreet 	down_write(&c->state_lock);
18141c6fdbd8SKent Overstreet 
18151c6fdbd8SKent Overstreet 	if (nbuckets < ca->mi.nbuckets) {
18161c6fdbd8SKent Overstreet 		bch_err(ca, "Cannot shrink yet");
18171c6fdbd8SKent Overstreet 		ret = -EINVAL;
18181c6fdbd8SKent Overstreet 		goto err;
18191c6fdbd8SKent Overstreet 	}
18201c6fdbd8SKent Overstreet 
18211c6fdbd8SKent Overstreet 	if (bch2_dev_is_online(ca) &&
18221c6fdbd8SKent Overstreet 	    get_capacity(ca->disk_sb.bdev->bd_disk) <
18231c6fdbd8SKent Overstreet 	    ca->mi.bucket_size * nbuckets) {
18241c6fdbd8SKent Overstreet 		bch_err(ca, "New size larger than device");
182578c0b75cSKent Overstreet 		ret = -BCH_ERR_device_size_too_small;
18261c6fdbd8SKent Overstreet 		goto err;
18271c6fdbd8SKent Overstreet 	}
18281c6fdbd8SKent Overstreet 
18291c6fdbd8SKent Overstreet 	ret = bch2_dev_buckets_resize(c, ca, nbuckets);
18301c6fdbd8SKent Overstreet 	if (ret) {
1831d4bf5eecSKent Overstreet 		bch_err(ca, "Resize error: %s", bch2_err_str(ret));
18321c6fdbd8SKent Overstreet 		goto err;
18331c6fdbd8SKent Overstreet 	}
18341c6fdbd8SKent Overstreet 
1835224ec3e6SKent Overstreet 	ret = bch2_trans_mark_dev_sb(c, ca);
18363e3e02e6SKent Overstreet 	if (ret)
1837224ec3e6SKent Overstreet 		goto err;
1838224ec3e6SKent Overstreet 
18391c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
18401c6fdbd8SKent Overstreet 	mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
18411c6fdbd8SKent Overstreet 	mi->nbuckets = cpu_to_le64(nbuckets);
18421c6fdbd8SKent Overstreet 
18431c6fdbd8SKent Overstreet 	bch2_write_super(c);
18441c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
18451c6fdbd8SKent Overstreet 
18461c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
18471c6fdbd8SKent Overstreet err:
18481ada1606SKent Overstreet 	up_write(&c->state_lock);
18491c6fdbd8SKent Overstreet 	return ret;
18501c6fdbd8SKent Overstreet }
18511c6fdbd8SKent Overstreet 
18521c6fdbd8SKent Overstreet /* return with ref on ca->ref: */
1853bf7e49a4SKent Overstreet struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name)
18541c6fdbd8SKent Overstreet {
18551c6fdbd8SKent Overstreet 	struct bch_dev *ca;
18561c6fdbd8SKent Overstreet 	unsigned i;
18571c6fdbd8SKent Overstreet 
18583a402c8dSKent Overstreet 	rcu_read_lock();
18593a402c8dSKent Overstreet 	for_each_member_device_rcu(ca, c, i, NULL)
1860bf7e49a4SKent Overstreet 		if (!strcmp(name, ca->name))
18611c6fdbd8SKent Overstreet 			goto found;
1862e47a390aSKent Overstreet 	ca = ERR_PTR(-BCH_ERR_ENOENT_dev_not_found);
18631c6fdbd8SKent Overstreet found:
18643a402c8dSKent Overstreet 	rcu_read_unlock();
18653a402c8dSKent Overstreet 
18661c6fdbd8SKent Overstreet 	return ca;
18671c6fdbd8SKent Overstreet }
18681c6fdbd8SKent Overstreet 
18691c6fdbd8SKent Overstreet /* Filesystem open: */
18701c6fdbd8SKent Overstreet 
18711c6fdbd8SKent Overstreet struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
18721c6fdbd8SKent Overstreet 			    struct bch_opts opts)
18731c6fdbd8SKent Overstreet {
18741c6fdbd8SKent Overstreet 	struct bch_sb_handle *sb = NULL;
18751c6fdbd8SKent Overstreet 	struct bch_fs *c = NULL;
1876625104eaSKent Overstreet 	struct bch_sb_field_members *mi;
18771c6fdbd8SKent Overstreet 	unsigned i, best_sb = 0;
1878fa8e94faSKent Overstreet 	struct printbuf errbuf = PRINTBUF;
1879e2b60560SKent Overstreet 	int ret = 0;
18801c6fdbd8SKent Overstreet 
1881efe68e1dSKent Overstreet 	if (!try_module_get(THIS_MODULE))
1882efe68e1dSKent Overstreet 		return ERR_PTR(-ENODEV);
1883efe68e1dSKent Overstreet 
18841c6fdbd8SKent Overstreet 	if (!nr_devices) {
1885efe68e1dSKent Overstreet 		ret = -EINVAL;
1886efe68e1dSKent Overstreet 		goto err;
18871c6fdbd8SKent Overstreet 	}
18881c6fdbd8SKent Overstreet 
18891c6fdbd8SKent Overstreet 	sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
1890e2b60560SKent Overstreet 	if (!sb) {
1891e2b60560SKent Overstreet 		ret = -ENOMEM;
18921c6fdbd8SKent Overstreet 		goto err;
1893e2b60560SKent Overstreet 	}
18941c6fdbd8SKent Overstreet 
18951c6fdbd8SKent Overstreet 	for (i = 0; i < nr_devices; i++) {
18961c6fdbd8SKent Overstreet 		ret = bch2_read_super(devices[i], &opts, &sb[i]);
18971c6fdbd8SKent Overstreet 		if (ret)
18981c6fdbd8SKent Overstreet 			goto err;
18991c6fdbd8SKent Overstreet 
19001c6fdbd8SKent Overstreet 	}
19011c6fdbd8SKent Overstreet 
19021c6fdbd8SKent Overstreet 	for (i = 1; i < nr_devices; i++)
19031c6fdbd8SKent Overstreet 		if (le64_to_cpu(sb[i].sb->seq) >
19041c6fdbd8SKent Overstreet 		    le64_to_cpu(sb[best_sb].sb->seq))
19051c6fdbd8SKent Overstreet 			best_sb = i;
19061c6fdbd8SKent Overstreet 
1907625104eaSKent Overstreet 	mi = bch2_sb_get_members(sb[best_sb].sb);
1908625104eaSKent Overstreet 
1909625104eaSKent Overstreet 	i = 0;
1910625104eaSKent Overstreet 	while (i < nr_devices) {
1911625104eaSKent Overstreet 		if (i != best_sb &&
1912625104eaSKent Overstreet 		    !bch2_dev_exists(sb[best_sb].sb, mi, sb[i].sb->dev_idx)) {
1913625104eaSKent Overstreet 			pr_info("%pg has been removed, skipping", sb[i].bdev);
1914625104eaSKent Overstreet 			bch2_free_super(&sb[i]);
1915625104eaSKent Overstreet 			array_remove_item(sb, nr_devices, i);
1916625104eaSKent Overstreet 			continue;
1917625104eaSKent Overstreet 		}
1918625104eaSKent Overstreet 
191978c0b75cSKent Overstreet 		ret = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
192078c0b75cSKent Overstreet 		if (ret)
19211c6fdbd8SKent Overstreet 			goto err_print;
1922625104eaSKent Overstreet 		i++;
19231c6fdbd8SKent Overstreet 	}
19241c6fdbd8SKent Overstreet 
19251c6fdbd8SKent Overstreet 	c = bch2_fs_alloc(sb[best_sb].sb, opts);
19267be9ab63SChris Webb 	if (IS_ERR(c)) {
19277be9ab63SChris Webb 		ret = PTR_ERR(c);
19281c6fdbd8SKent Overstreet 		goto err;
19297be9ab63SChris Webb 	}
19301c6fdbd8SKent Overstreet 
19311ada1606SKent Overstreet 	down_write(&c->state_lock);
1932e2b60560SKent Overstreet 	for (i = 0; i < nr_devices; i++) {
1933e2b60560SKent Overstreet 		ret = bch2_dev_attach_bdev(c, &sb[i]);
1934e2b60560SKent Overstreet 		if (ret) {
19351ada1606SKent Overstreet 			up_write(&c->state_lock);
1936e2b60560SKent Overstreet 			goto err;
1937e2b60560SKent Overstreet 		}
19381c6fdbd8SKent Overstreet 	}
19391ada1606SKent Overstreet 	up_write(&c->state_lock);
19401c6fdbd8SKent Overstreet 
194178c0b75cSKent Overstreet 	if (!bch2_fs_may_start(c)) {
194278c0b75cSKent Overstreet 		ret = -BCH_ERR_insufficient_devices_to_start;
19431c6fdbd8SKent Overstreet 		goto err_print;
194478c0b75cSKent Overstreet 	}
19451c6fdbd8SKent Overstreet 
19461c6fdbd8SKent Overstreet 	if (!c->opts.nostart) {
1947619f5beeSKent Overstreet 		ret = bch2_fs_start(c);
1948619f5beeSKent Overstreet 		if (ret)
1949619f5beeSKent Overstreet 			goto err;
19501c6fdbd8SKent Overstreet 	}
19511c6fdbd8SKent Overstreet out:
19521c6fdbd8SKent Overstreet 	kfree(sb);
1953fa8e94faSKent Overstreet 	printbuf_exit(&errbuf);
19541c6fdbd8SKent Overstreet 	module_put(THIS_MODULE);
19551c6fdbd8SKent Overstreet 	return c;
19561c6fdbd8SKent Overstreet err_print:
19571c6fdbd8SKent Overstreet 	pr_err("bch_fs_open err opening %s: %s",
195878c0b75cSKent Overstreet 	       devices[0], bch2_err_str(ret));
19591c6fdbd8SKent Overstreet err:
19607be9ab63SChris Webb 	if (!IS_ERR_OR_NULL(c))
19611c6fdbd8SKent Overstreet 		bch2_fs_stop(c);
1962e2b60560SKent Overstreet 	if (sb)
19631c6fdbd8SKent Overstreet 		for (i = 0; i < nr_devices; i++)
19641c6fdbd8SKent Overstreet 			bch2_free_super(&sb[i]);
19651c6fdbd8SKent Overstreet 	c = ERR_PTR(ret);
19661c6fdbd8SKent Overstreet 	goto out;
19671c6fdbd8SKent Overstreet }
19681c6fdbd8SKent Overstreet 
19691c6fdbd8SKent Overstreet /* Global interfaces/init */
19701c6fdbd8SKent Overstreet 
19711c6fdbd8SKent Overstreet static void bcachefs_exit(void)
19721c6fdbd8SKent Overstreet {
19731c6fdbd8SKent Overstreet 	bch2_debug_exit();
19741c6fdbd8SKent Overstreet 	bch2_vfs_exit();
19751c6fdbd8SKent Overstreet 	bch2_chardev_exit();
197614ba3706SKent Overstreet 	bch2_btree_key_cache_exit();
19771c6fdbd8SKent Overstreet 	if (bcachefs_kset)
19781c6fdbd8SKent Overstreet 		kset_unregister(bcachefs_kset);
19791c6fdbd8SKent Overstreet }
19801c6fdbd8SKent Overstreet 
19811c6fdbd8SKent Overstreet static int __init bcachefs_init(void)
19821c6fdbd8SKent Overstreet {
19831c6fdbd8SKent Overstreet 	bch2_bkey_pack_test();
19841c6fdbd8SKent Overstreet 
19851c6fdbd8SKent Overstreet 	if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
198614ba3706SKent Overstreet 	    bch2_btree_key_cache_init() ||
19871c6fdbd8SKent Overstreet 	    bch2_chardev_init() ||
19881c6fdbd8SKent Overstreet 	    bch2_vfs_init() ||
19891c6fdbd8SKent Overstreet 	    bch2_debug_init())
19901c6fdbd8SKent Overstreet 		goto err;
19911c6fdbd8SKent Overstreet 
19921c6fdbd8SKent Overstreet 	return 0;
19931c6fdbd8SKent Overstreet err:
19941c6fdbd8SKent Overstreet 	bcachefs_exit();
19951c6fdbd8SKent Overstreet 	return -ENOMEM;
19961c6fdbd8SKent Overstreet }
19971c6fdbd8SKent Overstreet 
19981c6fdbd8SKent Overstreet #define BCH_DEBUG_PARAM(name, description)			\
19991c6fdbd8SKent Overstreet 	bool bch2_##name;					\
20001c6fdbd8SKent Overstreet 	module_param_named(name, bch2_##name, bool, 0644);	\
20011c6fdbd8SKent Overstreet 	MODULE_PARM_DESC(name, description);
20021c6fdbd8SKent Overstreet BCH_DEBUG_PARAMS()
20031c6fdbd8SKent Overstreet #undef BCH_DEBUG_PARAM
20041c6fdbd8SKent Overstreet 
2005e08e63e4SKent Overstreet __maybe_unused
200673bd774dSKent Overstreet static unsigned bch2_metadata_version = bcachefs_metadata_version_current;
20071c6fdbd8SKent Overstreet module_param_named(version, bch2_metadata_version, uint, 0400);
20081c6fdbd8SKent Overstreet 
20091c6fdbd8SKent Overstreet module_exit(bcachefs_exit);
20101c6fdbd8SKent Overstreet module_init(bcachefs_init);
2011