xref: /linux/fs/bcachefs/super.c (revision 3a58dfbc46c277b090f1b72c949e15da7e1290bf)
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"
16401585feSKent Overstreet #include "btree_journal_iter.h"
172ca88e5aSKent Overstreet #include "btree_key_cache.h"
181c6fdbd8SKent Overstreet #include "btree_update_interior.h"
191c6fdbd8SKent Overstreet #include "btree_io.h"
20920e69bcSKent Overstreet #include "btree_write_buffer.h"
2121aec962SKent Overstreet #include "buckets_waiting_for_journal.h"
221c6fdbd8SKent Overstreet #include "chardev.h"
231c6fdbd8SKent Overstreet #include "checksum.h"
241c6fdbd8SKent Overstreet #include "clock.h"
251c6fdbd8SKent Overstreet #include "compress.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"
33dbbfca9fSKent Overstreet #include "fs-io-buffered.h"
34dbbfca9fSKent Overstreet #include "fs-io-direct.h"
351c6fdbd8SKent Overstreet #include "fsck.h"
361c6fdbd8SKent Overstreet #include "inode.h"
371809b8cbSKent Overstreet #include "io_read.h"
381809b8cbSKent Overstreet #include "io_write.h"
391c6fdbd8SKent Overstreet #include "journal.h"
401c6fdbd8SKent Overstreet #include "journal_reclaim.h"
411dd7f9d9SKent Overstreet #include "journal_seq_blacklist.h"
421c6fdbd8SKent Overstreet #include "move.h"
431c6fdbd8SKent Overstreet #include "migrate.h"
441c6fdbd8SKent Overstreet #include "movinggc.h"
45350175bfSKent Overstreet #include "nocow_locking.h"
461c6fdbd8SKent Overstreet #include "quota.h"
471c6fdbd8SKent Overstreet #include "rebalance.h"
481c6fdbd8SKent Overstreet #include "recovery.h"
491c6fdbd8SKent Overstreet #include "replicas.h"
50a37ad1a3SKent Overstreet #include "sb-clean.h"
51*3a58dfbcSKent Overstreet #include "sb-counters.h"
52f5d26fa3SKent Overstreet #include "sb-errors.h"
531241df58SHunter Shaffer #include "sb-members.h"
548e877caaSKent Overstreet #include "snapshot.h"
5514b393eeSKent Overstreet #include "subvolume.h"
561c6fdbd8SKent Overstreet #include "super.h"
571c6fdbd8SKent Overstreet #include "super-io.h"
581c6fdbd8SKent Overstreet #include "sysfs.h"
591c6fdbd8SKent Overstreet #include "trace.h"
601c6fdbd8SKent Overstreet 
611c6fdbd8SKent Overstreet #include <linux/backing-dev.h>
621c6fdbd8SKent Overstreet #include <linux/blkdev.h>
631c6fdbd8SKent Overstreet #include <linux/debugfs.h>
641c6fdbd8SKent Overstreet #include <linux/device.h>
651c6fdbd8SKent Overstreet #include <linux/idr.h>
661c6fdbd8SKent Overstreet #include <linux/module.h>
671c6fdbd8SKent Overstreet #include <linux/percpu.h>
681c6fdbd8SKent Overstreet #include <linux/random.h>
691c6fdbd8SKent Overstreet #include <linux/sysfs.h>
701c6fdbd8SKent Overstreet #include <crypto/hash.h>
711c6fdbd8SKent Overstreet 
721c6fdbd8SKent Overstreet MODULE_LICENSE("GPL");
731c6fdbd8SKent Overstreet MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
74a9737e0bSBrian Foster MODULE_DESCRIPTION("bcachefs filesystem");
7585c6db98SDaniel Hill MODULE_SOFTDEP("pre: crc32c");
7685c6db98SDaniel Hill MODULE_SOFTDEP("pre: crc64");
7785c6db98SDaniel Hill MODULE_SOFTDEP("pre: sha256");
7885c6db98SDaniel Hill MODULE_SOFTDEP("pre: chacha20");
7985c6db98SDaniel Hill MODULE_SOFTDEP("pre: poly1305");
8085c6db98SDaniel Hill MODULE_SOFTDEP("pre: xxhash");
811c6fdbd8SKent Overstreet 
823c471b65SKent Overstreet const char * const bch2_fs_flag_strs[] = {
833c471b65SKent Overstreet #define x(n)		#n,
843c471b65SKent Overstreet 	BCH_FS_FLAGS()
853c471b65SKent Overstreet #undef x
863c471b65SKent Overstreet 	NULL
873c471b65SKent Overstreet };
883c471b65SKent Overstreet 
892b41226dSKent Overstreet void __bch2_print(struct bch_fs *c, const char *fmt, ...)
902b41226dSKent Overstreet {
9196f37eabSKent Overstreet 	struct stdio_redirect *stdio = bch2_fs_stdio_redirect(c);
9296f37eabSKent Overstreet 
932b41226dSKent Overstreet 	va_list args;
942b41226dSKent Overstreet 	va_start(args, fmt);
9596f37eabSKent Overstreet 	if (likely(!stdio)) {
962b41226dSKent Overstreet 		vprintk(fmt, args);
972b41226dSKent Overstreet 	} else {
982b41226dSKent Overstreet 		unsigned long flags;
992b41226dSKent Overstreet 
1002b41226dSKent Overstreet 		if (fmt[0] == KERN_SOH[0])
1012b41226dSKent Overstreet 			fmt += 2;
1022b41226dSKent Overstreet 
10396f37eabSKent Overstreet 		spin_lock_irqsave(&stdio->output_lock, flags);
10496f37eabSKent Overstreet 		prt_vprintf(&stdio->output_buf, fmt, args);
10596f37eabSKent Overstreet 		spin_unlock_irqrestore(&stdio->output_lock, flags);
1062b41226dSKent Overstreet 
10796f37eabSKent Overstreet 		wake_up(&stdio->output_wait);
1082b41226dSKent Overstreet 	}
1092b41226dSKent Overstreet 	va_end(args);
1102b41226dSKent Overstreet }
1112b41226dSKent Overstreet 
1121c6fdbd8SKent Overstreet #define KTYPE(type)							\
1131c6fdbd8SKent Overstreet static const struct attribute_group type ## _group = {			\
1141c6fdbd8SKent Overstreet 	.attrs = type ## _files						\
1151c6fdbd8SKent Overstreet };									\
1161c6fdbd8SKent Overstreet 									\
1171c6fdbd8SKent Overstreet static const struct attribute_group *type ## _groups[] = {		\
1181c6fdbd8SKent Overstreet 	&type ## _group,						\
1191c6fdbd8SKent Overstreet 	NULL								\
1201c6fdbd8SKent Overstreet };									\
1211c6fdbd8SKent Overstreet 									\
1221c6fdbd8SKent Overstreet static const struct kobj_type type ## _ktype = {			\
1231c6fdbd8SKent Overstreet 	.release	= type ## _release,				\
1241c6fdbd8SKent Overstreet 	.sysfs_ops	= &type ## _sysfs_ops,				\
1251c6fdbd8SKent Overstreet 	.default_groups = type ## _groups				\
1261c6fdbd8SKent Overstreet }
1271c6fdbd8SKent Overstreet 
1281c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *);
1291c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *);
130104c6974SDaniel Hill static void bch2_fs_counters_release(struct kobject *k)
131104c6974SDaniel Hill {
132104c6974SDaniel Hill }
1331c6fdbd8SKent Overstreet 
1341c6fdbd8SKent Overstreet static void bch2_fs_internal_release(struct kobject *k)
1351c6fdbd8SKent Overstreet {
1361c6fdbd8SKent Overstreet }
1371c6fdbd8SKent Overstreet 
1381c6fdbd8SKent Overstreet static void bch2_fs_opts_dir_release(struct kobject *k)
1391c6fdbd8SKent Overstreet {
1401c6fdbd8SKent Overstreet }
1411c6fdbd8SKent Overstreet 
1421c6fdbd8SKent Overstreet static void bch2_fs_time_stats_release(struct kobject *k)
1431c6fdbd8SKent Overstreet {
1441c6fdbd8SKent Overstreet }
1451c6fdbd8SKent Overstreet 
1461c6fdbd8SKent Overstreet KTYPE(bch2_fs);
147104c6974SDaniel Hill KTYPE(bch2_fs_counters);
1481c6fdbd8SKent Overstreet KTYPE(bch2_fs_internal);
1491c6fdbd8SKent Overstreet KTYPE(bch2_fs_opts_dir);
1501c6fdbd8SKent Overstreet KTYPE(bch2_fs_time_stats);
1511c6fdbd8SKent Overstreet KTYPE(bch2_dev);
1521c6fdbd8SKent Overstreet 
1531c6fdbd8SKent Overstreet static struct kset *bcachefs_kset;
1541c6fdbd8SKent Overstreet static LIST_HEAD(bch_fs_list);
1551c6fdbd8SKent Overstreet static DEFINE_MUTEX(bch_fs_list_lock);
1561c6fdbd8SKent Overstreet 
157d94189adSKent Overstreet DECLARE_WAIT_QUEUE_HEAD(bch2_read_only_wait);
1581c6fdbd8SKent Overstreet 
1591c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *);
1601c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *, unsigned);
1611c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *);
1621c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
1631c6fdbd8SKent Overstreet 
1641c6fdbd8SKent Overstreet struct bch_fs *bch2_dev_to_fs(dev_t dev)
1651c6fdbd8SKent Overstreet {
1661c6fdbd8SKent Overstreet 	struct bch_fs *c;
1671c6fdbd8SKent Overstreet 
1681c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
1691c6fdbd8SKent Overstreet 	rcu_read_lock();
1701c6fdbd8SKent Overstreet 
1711c6fdbd8SKent Overstreet 	list_for_each_entry(c, &bch_fs_list, list)
17241b84fb4SKent Overstreet 		for_each_member_device_rcu(c, ca, NULL)
173ec4ab9d2SDan Robertson 			if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) {
1741c6fdbd8SKent Overstreet 				closure_get(&c->cl);
1751c6fdbd8SKent Overstreet 				goto found;
1761c6fdbd8SKent Overstreet 			}
1771c6fdbd8SKent Overstreet 	c = NULL;
1781c6fdbd8SKent Overstreet found:
1791c6fdbd8SKent Overstreet 	rcu_read_unlock();
1801c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
1811c6fdbd8SKent Overstreet 
1821c6fdbd8SKent Overstreet 	return c;
1831c6fdbd8SKent Overstreet }
1841c6fdbd8SKent Overstreet 
1851c6fdbd8SKent Overstreet static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid)
1861c6fdbd8SKent Overstreet {
1871c6fdbd8SKent Overstreet 	struct bch_fs *c;
1881c6fdbd8SKent Overstreet 
1891c6fdbd8SKent Overstreet 	lockdep_assert_held(&bch_fs_list_lock);
1901c6fdbd8SKent Overstreet 
1911c6fdbd8SKent Overstreet 	list_for_each_entry(c, &bch_fs_list, list)
1921c6fdbd8SKent Overstreet 		if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid)))
1931c6fdbd8SKent Overstreet 			return c;
1941c6fdbd8SKent Overstreet 
1951c6fdbd8SKent Overstreet 	return NULL;
1961c6fdbd8SKent Overstreet }
1971c6fdbd8SKent Overstreet 
1981c6fdbd8SKent Overstreet struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid)
1991c6fdbd8SKent Overstreet {
2001c6fdbd8SKent Overstreet 	struct bch_fs *c;
2011c6fdbd8SKent Overstreet 
2021c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
2031c6fdbd8SKent Overstreet 	c = __bch2_uuid_to_fs(uuid);
2041c6fdbd8SKent Overstreet 	if (c)
2051c6fdbd8SKent Overstreet 		closure_get(&c->cl);
2061c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
2071c6fdbd8SKent Overstreet 
2081c6fdbd8SKent Overstreet 	return c;
2091c6fdbd8SKent Overstreet }
2101c6fdbd8SKent Overstreet 
211180fb49dSKent Overstreet static void bch2_dev_usage_journal_reserve(struct bch_fs *c)
212180fb49dSKent Overstreet {
21341b84fb4SKent Overstreet 	unsigned nr = 0, u64s =
2144b8f89afSKent Overstreet 		((sizeof(struct jset_entry_dev_usage) +
2154b8f89afSKent Overstreet 		  sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR)) /
2164b8f89afSKent Overstreet 		sizeof(u64);
217180fb49dSKent Overstreet 
218180fb49dSKent Overstreet 	rcu_read_lock();
21941b84fb4SKent Overstreet 	for_each_member_device_rcu(c, ca, NULL)
220180fb49dSKent Overstreet 		nr++;
221180fb49dSKent Overstreet 	rcu_read_unlock();
222180fb49dSKent Overstreet 
223180fb49dSKent Overstreet 	bch2_journal_entry_res_resize(&c->journal,
224180fb49dSKent Overstreet 			&c->dev_usage_journal_res, u64s * nr);
225180fb49dSKent Overstreet }
226180fb49dSKent Overstreet 
2271c6fdbd8SKent Overstreet /* Filesystem RO/RW: */
2281c6fdbd8SKent Overstreet 
2291c6fdbd8SKent Overstreet /*
2301c6fdbd8SKent Overstreet  * For startup/shutdown of RW stuff, the dependencies are:
2311c6fdbd8SKent Overstreet  *
2321c6fdbd8SKent Overstreet  * - foreground writes depend on copygc and rebalance (to free up space)
2331c6fdbd8SKent Overstreet  *
2341c6fdbd8SKent Overstreet  * - copygc and rebalance depend on mark and sweep gc (they actually probably
2351c6fdbd8SKent Overstreet  *   don't because they either reserve ahead of time or don't block if
2361c6fdbd8SKent Overstreet  *   allocations fail, but allocations can require mark and sweep gc to run
2371c6fdbd8SKent Overstreet  *   because of generation number wraparound)
2381c6fdbd8SKent Overstreet  *
2391c6fdbd8SKent Overstreet  * - all of the above depends on the allocator threads
2401c6fdbd8SKent Overstreet  *
2411c6fdbd8SKent Overstreet  * - allocator depends on the journal (when it rewrites prios and gens)
2421c6fdbd8SKent Overstreet  */
2431c6fdbd8SKent Overstreet 
2441c6fdbd8SKent Overstreet static void __bch2_fs_read_only(struct bch_fs *c)
2451c6fdbd8SKent Overstreet {
2469fea2274SKent Overstreet 	unsigned clean_passes = 0;
247c0960603SKent Overstreet 	u64 seq = 0;
2481c6fdbd8SKent Overstreet 
249b40901b0SKent Overstreet 	bch2_fs_ec_stop(c);
250b40901b0SKent Overstreet 	bch2_open_buckets_stop(c, NULL, true);
2511c6fdbd8SKent Overstreet 	bch2_rebalance_stop(c);
252e6d11615SKent Overstreet 	bch2_copygc_stop(c);
2531c6fdbd8SKent Overstreet 	bch2_gc_thread_stop(c);
254b40901b0SKent Overstreet 	bch2_fs_ec_flush(c);
2551c6fdbd8SKent Overstreet 
25683ec519aSKent Overstreet 	bch_verbose(c, "flushing journal and stopping allocators, journal seq %llu",
25783ec519aSKent Overstreet 		    journal_cur_seq(&c->journal));
2581c6fdbd8SKent Overstreet 
259039fc4c5SKent Overstreet 	do {
260039fc4c5SKent Overstreet 		clean_passes++;
261039fc4c5SKent Overstreet 
262c0960603SKent Overstreet 		if (bch2_btree_interior_updates_flush(c) ||
263c0960603SKent Overstreet 		    bch2_journal_flush_all_pins(&c->journal) ||
264c0960603SKent Overstreet 		    bch2_btree_flush_all_writes(c) ||
265c0960603SKent Overstreet 		    seq != atomic64_read(&c->journal.seq)) {
266c0960603SKent Overstreet 			seq = atomic64_read(&c->journal.seq);
267039fc4c5SKent Overstreet 			clean_passes = 0;
268039fc4c5SKent Overstreet 		}
269d5f70c1fSKent Overstreet 	} while (clean_passes < 2);
270c0960603SKent Overstreet 
27183ec519aSKent Overstreet 	bch_verbose(c, "flushing journal and stopping allocators complete, journal seq %llu",
27283ec519aSKent Overstreet 		    journal_cur_seq(&c->journal));
2732340fd9dSKent Overstreet 
274c0960603SKent Overstreet 	if (test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags) &&
2753c471b65SKent Overstreet 	    !test_bit(BCH_FS_emergency_ro, &c->flags))
2763c471b65SKent Overstreet 		set_bit(BCH_FS_clean_shutdown, &c->flags);
2771c6fdbd8SKent Overstreet 	bch2_fs_journal_stop(&c->journal);
2781c6fdbd8SKent Overstreet 
2791c6fdbd8SKent Overstreet 	/*
2801c6fdbd8SKent Overstreet 	 * After stopping journal:
2811c6fdbd8SKent Overstreet 	 */
2829fea2274SKent Overstreet 	for_each_member_device(c, ca)
2831c6fdbd8SKent Overstreet 		bch2_dev_allocator_remove(c, ca);
2841c6fdbd8SKent Overstreet }
2851c6fdbd8SKent Overstreet 
286d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG
2871c6fdbd8SKent Overstreet static void bch2_writes_disabled(struct percpu_ref *writes)
2881c6fdbd8SKent Overstreet {
2891c6fdbd8SKent Overstreet 	struct bch_fs *c = container_of(writes, struct bch_fs, writes);
2901c6fdbd8SKent Overstreet 
2913c471b65SKent Overstreet 	set_bit(BCH_FS_write_disable_complete, &c->flags);
292d94189adSKent Overstreet 	wake_up(&bch2_read_only_wait);
2931c6fdbd8SKent Overstreet }
294d94189adSKent Overstreet #endif
2951c6fdbd8SKent Overstreet 
2961c6fdbd8SKent Overstreet void bch2_fs_read_only(struct bch_fs *c)
2971c6fdbd8SKent Overstreet {
2983c471b65SKent Overstreet 	if (!test_bit(BCH_FS_rw, &c->flags)) {
2999ae28f82SKent Overstreet 		bch2_journal_reclaim_stop(&c->journal);
3001c6fdbd8SKent Overstreet 		return;
301134915f3SKent Overstreet 	}
3021c6fdbd8SKent Overstreet 
3033c471b65SKent Overstreet 	BUG_ON(test_bit(BCH_FS_write_disable_complete, &c->flags));
3041c6fdbd8SKent Overstreet 
305e7f7ddedSKent Overstreet 	bch_verbose(c, "going read-only");
306e7f7ddedSKent Overstreet 
3071c6fdbd8SKent Overstreet 	/*
3081c6fdbd8SKent Overstreet 	 * Block new foreground-end write operations from starting - any new
3091c6fdbd8SKent Overstreet 	 * writes will return -EROFS:
3101c6fdbd8SKent Overstreet 	 */
3113c471b65SKent Overstreet 	set_bit(BCH_FS_going_ro, &c->flags);
312d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG
3131c6fdbd8SKent Overstreet 	percpu_ref_kill(&c->writes);
314d94189adSKent Overstreet #else
315d94189adSKent Overstreet 	for (unsigned i = 0; i < BCH_WRITE_REF_NR; i++)
316d94189adSKent Overstreet 		bch2_write_ref_put(c, i);
317d94189adSKent Overstreet #endif
3181c6fdbd8SKent Overstreet 
3191c6fdbd8SKent Overstreet 	/*
3201c6fdbd8SKent Overstreet 	 * If we're not doing an emergency shutdown, we want to wait on
3211c6fdbd8SKent Overstreet 	 * outstanding writes to complete so they don't see spurious errors due
3221c6fdbd8SKent Overstreet 	 * to shutting down the allocator:
3231c6fdbd8SKent Overstreet 	 *
3241c6fdbd8SKent Overstreet 	 * If we are doing an emergency shutdown outstanding writes may
3251c6fdbd8SKent Overstreet 	 * hang until we shutdown the allocator so we don't want to wait
3261c6fdbd8SKent Overstreet 	 * on outstanding writes before shutting everything down - but
3271c6fdbd8SKent Overstreet 	 * we do need to wait on them before returning and signalling
3281c6fdbd8SKent Overstreet 	 * that going RO is complete:
3291c6fdbd8SKent Overstreet 	 */
330d94189adSKent Overstreet 	wait_event(bch2_read_only_wait,
3313c471b65SKent Overstreet 		   test_bit(BCH_FS_write_disable_complete, &c->flags) ||
3323c471b65SKent Overstreet 		   test_bit(BCH_FS_emergency_ro, &c->flags));
3331c6fdbd8SKent Overstreet 
3343c471b65SKent Overstreet 	bool writes_disabled = test_bit(BCH_FS_write_disable_complete, &c->flags);
335e7f7ddedSKent Overstreet 	if (writes_disabled)
336e7f7ddedSKent Overstreet 		bch_verbose(c, "finished waiting for writes to stop");
337e7f7ddedSKent Overstreet 
3381c6fdbd8SKent Overstreet 	__bch2_fs_read_only(c);
3391c6fdbd8SKent Overstreet 
340d94189adSKent Overstreet 	wait_event(bch2_read_only_wait,
3413c471b65SKent Overstreet 		   test_bit(BCH_FS_write_disable_complete, &c->flags));
3421c6fdbd8SKent Overstreet 
343e7f7ddedSKent Overstreet 	if (!writes_disabled)
344e7f7ddedSKent Overstreet 		bch_verbose(c, "finished waiting for writes to stop");
345e7f7ddedSKent Overstreet 
3463c471b65SKent Overstreet 	clear_bit(BCH_FS_write_disable_complete, &c->flags);
3473c471b65SKent Overstreet 	clear_bit(BCH_FS_going_ro, &c->flags);
3483c471b65SKent Overstreet 	clear_bit(BCH_FS_rw, &c->flags);
3491c6fdbd8SKent Overstreet 
3501c6fdbd8SKent Overstreet 	if (!bch2_journal_error(&c->journal) &&
3513c471b65SKent Overstreet 	    !test_bit(BCH_FS_error, &c->flags) &&
3523c471b65SKent Overstreet 	    !test_bit(BCH_FS_emergency_ro, &c->flags) &&
3533c471b65SKent Overstreet 	    test_bit(BCH_FS_started, &c->flags) &&
3543c471b65SKent Overstreet 	    test_bit(BCH_FS_clean_shutdown, &c->flags) &&
355b2930396SKent Overstreet 	    !c->opts.norecovery) {
3567724664fSKent Overstreet 		BUG_ON(c->journal.last_empty_seq != journal_cur_seq(&c->journal));
3577724664fSKent Overstreet 		BUG_ON(atomic_read(&c->btree_cache.dirty));
3587724664fSKent Overstreet 		BUG_ON(atomic_long_read(&c->btree_key_cache.nr_dirty));
35909caeabeSKent Overstreet 		BUG_ON(c->btree_write_buffer.inc.keys.nr);
36009caeabeSKent Overstreet 		BUG_ON(c->btree_write_buffer.flushing.keys.nr);
3617724664fSKent Overstreet 
362b2930396SKent Overstreet 		bch_verbose(c, "marking filesystem clean");
363134915f3SKent Overstreet 		bch2_fs_mark_clean(c);
364e7f7ddedSKent Overstreet 	} else {
365e7f7ddedSKent Overstreet 		bch_verbose(c, "done going read-only, filesystem not clean");
366b2930396SKent Overstreet 	}
3671c6fdbd8SKent Overstreet }
3681c6fdbd8SKent Overstreet 
3691c6fdbd8SKent Overstreet static void bch2_fs_read_only_work(struct work_struct *work)
3701c6fdbd8SKent Overstreet {
3711c6fdbd8SKent Overstreet 	struct bch_fs *c =
3721c6fdbd8SKent Overstreet 		container_of(work, struct bch_fs, read_only_work);
3731c6fdbd8SKent Overstreet 
3741ada1606SKent Overstreet 	down_write(&c->state_lock);
3751c6fdbd8SKent Overstreet 	bch2_fs_read_only(c);
3761ada1606SKent Overstreet 	up_write(&c->state_lock);
3771c6fdbd8SKent Overstreet }
3781c6fdbd8SKent Overstreet 
3791c6fdbd8SKent Overstreet static void bch2_fs_read_only_async(struct bch_fs *c)
3801c6fdbd8SKent Overstreet {
3811c6fdbd8SKent Overstreet 	queue_work(system_long_wq, &c->read_only_work);
3821c6fdbd8SKent Overstreet }
3831c6fdbd8SKent Overstreet 
3841c6fdbd8SKent Overstreet bool bch2_fs_emergency_read_only(struct bch_fs *c)
3851c6fdbd8SKent Overstreet {
3863c471b65SKent Overstreet 	bool ret = !test_and_set_bit(BCH_FS_emergency_ro, &c->flags);
3871c6fdbd8SKent Overstreet 
3881c6fdbd8SKent Overstreet 	bch2_journal_halt(&c->journal);
3899f115ce9SKent Overstreet 	bch2_fs_read_only_async(c);
3901c6fdbd8SKent Overstreet 
391d94189adSKent Overstreet 	wake_up(&bch2_read_only_wait);
3921c6fdbd8SKent Overstreet 	return ret;
3931c6fdbd8SKent Overstreet }
3941c6fdbd8SKent Overstreet 
395134915f3SKent Overstreet static int bch2_fs_read_write_late(struct bch_fs *c)
3961c6fdbd8SKent Overstreet {
397134915f3SKent Overstreet 	int ret;
3981c6fdbd8SKent Overstreet 
399ea28c867SKent Overstreet 	/*
400ea28c867SKent Overstreet 	 * Data move operations can't run until after check_snapshots has
401ea28c867SKent Overstreet 	 * completed, and bch2_snapshot_is_ancestor() is available.
402ea28c867SKent Overstreet 	 *
403ea28c867SKent Overstreet 	 * Ideally we'd start copygc/rebalance earlier instead of waiting for
404ea28c867SKent Overstreet 	 * all of recovery/fsck to complete:
405ea28c867SKent Overstreet 	 */
406ea28c867SKent Overstreet 	ret = bch2_copygc_start(c);
407ea28c867SKent Overstreet 	if (ret) {
408ea28c867SKent Overstreet 		bch_err(c, "error starting copygc thread");
409ea28c867SKent Overstreet 		return ret;
410ea28c867SKent Overstreet 	}
411ea28c867SKent Overstreet 
412134915f3SKent Overstreet 	ret = bch2_rebalance_start(c);
413134915f3SKent Overstreet 	if (ret) {
414134915f3SKent Overstreet 		bch_err(c, "error starting rebalance thread");
415134915f3SKent Overstreet 		return ret;
416134915f3SKent Overstreet 	}
417134915f3SKent Overstreet 
418134915f3SKent Overstreet 	return 0;
419134915f3SKent Overstreet }
420134915f3SKent Overstreet 
421e731d466SKent Overstreet static int __bch2_fs_read_write(struct bch_fs *c, bool early)
422134915f3SKent Overstreet {
423134915f3SKent Overstreet 	int ret;
424134915f3SKent Overstreet 
4253c471b65SKent Overstreet 	if (test_bit(BCH_FS_initial_gc_unfixed, &c->flags)) {
426aae15aafSKent Overstreet 		bch_err(c, "cannot go rw, unfixed btree errors");
4277c50140fSKent Overstreet 		return -BCH_ERR_erofs_unfixed_errors;
428aae15aafSKent Overstreet 	}
429aae15aafSKent Overstreet 
4303c471b65SKent Overstreet 	if (test_bit(BCH_FS_rw, &c->flags))
431134915f3SKent Overstreet 		return 0;
432134915f3SKent Overstreet 
4332c944fa1SKent Overstreet 	bch_info(c, "going read-write");
4342c944fa1SKent Overstreet 
435f5d26fa3SKent Overstreet 	ret = bch2_sb_members_v2_init(c);
4363f7b9713SHunter Shaffer 	if (ret)
4373f7b9713SHunter Shaffer 		goto err;
4383f7b9713SHunter Shaffer 
439134915f3SKent Overstreet 	ret = bch2_fs_mark_dirty(c);
440134915f3SKent Overstreet 	if (ret)
441134915f3SKent Overstreet 		goto err;
4421c6fdbd8SKent Overstreet 
4433c471b65SKent Overstreet 	clear_bit(BCH_FS_clean_shutdown, &c->flags);
4442340fd9dSKent Overstreet 
445b9004e85SKent Overstreet 	/*
446b9004e85SKent Overstreet 	 * First journal write must be a flush write: after a clean shutdown we
447b9004e85SKent Overstreet 	 * don't read the journal, so the first journal write may end up
448b9004e85SKent Overstreet 	 * overwriting whatever was there previously, and there must always be
449b9004e85SKent Overstreet 	 * at least one non-flush write in the journal or recovery will fail:
450b9004e85SKent Overstreet 	 */
451b9004e85SKent Overstreet 	set_bit(JOURNAL_NEED_FLUSH_WRITE, &c->journal.flags);
452b9004e85SKent Overstreet 
4539fea2274SKent Overstreet 	for_each_rw_member(c, ca)
4541c6fdbd8SKent Overstreet 		bch2_dev_allocator_add(c, ca);
4551c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
4561c6fdbd8SKent Overstreet 
4573c471b65SKent Overstreet 	set_bit(BCH_FS_rw, &c->flags);
4583c471b65SKent Overstreet 	set_bit(BCH_FS_was_rw, &c->flags);
459468035caSKent Overstreet 
460468035caSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG
461468035caSKent Overstreet 	percpu_ref_reinit(&c->writes);
462468035caSKent Overstreet #else
4639fea2274SKent Overstreet 	for (unsigned i = 0; i < BCH_WRITE_REF_NR; i++) {
464468035caSKent Overstreet 		BUG_ON(atomic_long_read(&c->writes[i]));
465468035caSKent Overstreet 		atomic_long_inc(&c->writes[i]);
466468035caSKent Overstreet 	}
467468035caSKent Overstreet #endif
468468035caSKent Overstreet 
4695f5c7466SKent Overstreet 	ret = bch2_gc_thread_start(c);
4705f5c7466SKent Overstreet 	if (ret) {
4715f5c7466SKent Overstreet 		bch_err(c, "error starting gc thread");
4725f5c7466SKent Overstreet 		return ret;
4735f5c7466SKent Overstreet 	}
4745f5c7466SKent Overstreet 
475197763a7SBrian Foster 	ret = bch2_journal_reclaim_start(&c->journal);
476197763a7SBrian Foster 	if (ret)
477197763a7SBrian Foster 		goto err;
478197763a7SBrian Foster 
479134915f3SKent Overstreet 	if (!early) {
480134915f3SKent Overstreet 		ret = bch2_fs_read_write_late(c);
481134915f3SKent Overstreet 		if (ret)
4821c6fdbd8SKent Overstreet 			goto err;
4831c6fdbd8SKent Overstreet 	}
4841c6fdbd8SKent Overstreet 
485dd81a060SKent Overstreet 	bch2_do_discards(c);
486dd81a060SKent Overstreet 	bch2_do_invalidates(c);
487dd81a060SKent Overstreet 	bch2_do_stripe_deletes(c);
488a1f26d70SKent Overstreet 	bch2_do_pending_node_rewrites(c);
489134915f3SKent Overstreet 	return 0;
4901c6fdbd8SKent Overstreet err:
4913c471b65SKent Overstreet 	if (test_bit(BCH_FS_rw, &c->flags))
492468035caSKent Overstreet 		bch2_fs_read_only(c);
493468035caSKent Overstreet 	else
4941c6fdbd8SKent Overstreet 		__bch2_fs_read_only(c);
495134915f3SKent Overstreet 	return ret;
496134915f3SKent Overstreet }
497134915f3SKent Overstreet 
498134915f3SKent Overstreet int bch2_fs_read_write(struct bch_fs *c)
499134915f3SKent Overstreet {
50062719cf3SKent Overstreet 	if (c->opts.norecovery)
50162719cf3SKent Overstreet 		return -BCH_ERR_erofs_norecovery;
50262719cf3SKent Overstreet 
50362719cf3SKent Overstreet 	if (c->opts.nochanges)
50462719cf3SKent Overstreet 		return -BCH_ERR_erofs_nochanges;
50562719cf3SKent Overstreet 
506134915f3SKent Overstreet 	return __bch2_fs_read_write(c, false);
507134915f3SKent Overstreet }
508134915f3SKent Overstreet 
509134915f3SKent Overstreet int bch2_fs_read_write_early(struct bch_fs *c)
510134915f3SKent Overstreet {
511134915f3SKent Overstreet 	lockdep_assert_held(&c->state_lock);
512134915f3SKent Overstreet 
513134915f3SKent Overstreet 	return __bch2_fs_read_write(c, true);
5141c6fdbd8SKent Overstreet }
5151c6fdbd8SKent Overstreet 
5161c6fdbd8SKent Overstreet /* Filesystem startup/shutdown: */
5171c6fdbd8SKent Overstreet 
518d5e4dcc2SKent Overstreet static void __bch2_fs_free(struct bch_fs *c)
5191c6fdbd8SKent Overstreet {
5201c6fdbd8SKent Overstreet 	unsigned i;
5211c6fdbd8SKent Overstreet 
5221c6fdbd8SKent Overstreet 	for (i = 0; i < BCH_TIME_STAT_NR; i++)
5231c6fdbd8SKent Overstreet 		bch2_time_stats_exit(&c->times[i]);
5241c6fdbd8SKent Overstreet 
525a1f26d70SKent Overstreet 	bch2_free_pending_node_rewrites(c);
526f5d26fa3SKent Overstreet 	bch2_fs_sb_errors_exit(c);
527104c6974SDaniel Hill 	bch2_fs_counters_exit(c);
52814b393eeSKent Overstreet 	bch2_fs_snapshots_exit(c);
5291c6fdbd8SKent Overstreet 	bch2_fs_quota_exit(c);
530dbbfca9fSKent Overstreet 	bch2_fs_fs_io_direct_exit(c);
531dbbfca9fSKent Overstreet 	bch2_fs_fs_io_buffered_exit(c);
5321c6fdbd8SKent Overstreet 	bch2_fs_fsio_exit(c);
533cd575ddfSKent Overstreet 	bch2_fs_ec_exit(c);
5341c6fdbd8SKent Overstreet 	bch2_fs_encryption_exit(c);
5351e3b4098SKent Overstreet 	bch2_fs_nocow_locking_exit(c);
5361809b8cbSKent Overstreet 	bch2_fs_io_write_exit(c);
5371809b8cbSKent Overstreet 	bch2_fs_io_read_exit(c);
53821aec962SKent Overstreet 	bch2_fs_buckets_waiting_for_journal_exit(c);
539c823c339SKent Overstreet 	bch2_fs_btree_interior_update_exit(c);
54036e9d698SKent Overstreet 	bch2_fs_btree_iter_exit(c);
5412ca88e5aSKent Overstreet 	bch2_fs_btree_key_cache_exit(&c->btree_key_cache);
5421c6fdbd8SKent Overstreet 	bch2_fs_btree_cache_exit(c);
5439620c3ecSKent Overstreet 	bch2_fs_replicas_exit(c);
5441c6fdbd8SKent Overstreet 	bch2_fs_journal_exit(&c->journal);
5451c6fdbd8SKent Overstreet 	bch2_io_clock_exit(&c->io_clock[WRITE]);
5461c6fdbd8SKent Overstreet 	bch2_io_clock_exit(&c->io_clock[READ]);
5471c6fdbd8SKent Overstreet 	bch2_fs_compress_exit(c);
5488a443d3eSKent Overstreet 	bch2_journal_keys_put_initial(c);
5498a443d3eSKent Overstreet 	BUG_ON(atomic_read(&c->journal_keys.ref));
550920e69bcSKent Overstreet 	bch2_fs_btree_write_buffer_exit(c);
5519166b41dSKent Overstreet 	percpu_free_rwsem(&c->mark_lock);
5525e82a9a1SKent Overstreet 	free_percpu(c->online_reserved);
5531a21bf98SKent Overstreet 
554faa6cb6cSKent Overstreet 	darray_exit(&c->btree_roots_extra);
5555663a415SKent Overstreet 	free_percpu(c->pcpu);
55635189e09SKent Overstreet 	mempool_exit(&c->large_bkey_pool);
5571c6fdbd8SKent Overstreet 	mempool_exit(&c->btree_bounce_pool);
5581c6fdbd8SKent Overstreet 	bioset_exit(&c->btree_bio);
5591c6fdbd8SKent Overstreet 	mempool_exit(&c->fill_iter);
560d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG
5611c6fdbd8SKent Overstreet 	percpu_ref_exit(&c->writes);
562d94189adSKent Overstreet #endif
5631c6fdbd8SKent Overstreet 	kfree(rcu_dereference_protected(c->disk_groups, 1));
5641dd7f9d9SKent Overstreet 	kfree(c->journal_seq_blacklist_table);
565b5e8a699SKent Overstreet 	kfree(c->unused_inode_hints);
5661c6fdbd8SKent Overstreet 
5678bff9875SBrian Foster 	if (c->write_ref_wq)
5688bff9875SBrian Foster 		destroy_workqueue(c->write_ref_wq);
569731bdd2eSKent Overstreet 	if (c->io_complete_wq)
570731bdd2eSKent Overstreet 		destroy_workqueue(c->io_complete_wq);
5711c6fdbd8SKent Overstreet 	if (c->copygc_wq)
5721c6fdbd8SKent Overstreet 		destroy_workqueue(c->copygc_wq);
5739f1833caSKent Overstreet 	if (c->btree_io_complete_wq)
5749f1833caSKent Overstreet 		destroy_workqueue(c->btree_io_complete_wq);
575731bdd2eSKent Overstreet 	if (c->btree_update_wq)
576731bdd2eSKent Overstreet 		destroy_workqueue(c->btree_update_wq);
5771c6fdbd8SKent Overstreet 
5789d8022dbSKent Overstreet 	bch2_free_super(&c->disk_sb);
5791c6fdbd8SKent Overstreet 	kvpfree(c, sizeof(*c));
5801c6fdbd8SKent Overstreet 	module_put(THIS_MODULE);
5811c6fdbd8SKent Overstreet }
5821c6fdbd8SKent Overstreet 
5831c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *kobj)
5841c6fdbd8SKent Overstreet {
5851c6fdbd8SKent Overstreet 	struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
5861c6fdbd8SKent Overstreet 
587d5e4dcc2SKent Overstreet 	__bch2_fs_free(c);
5881c6fdbd8SKent Overstreet }
5891c6fdbd8SKent Overstreet 
590d5e4dcc2SKent Overstreet void __bch2_fs_stop(struct bch_fs *c)
5911c6fdbd8SKent Overstreet {
592af1c6871SKent Overstreet 	bch_verbose(c, "shutting down");
593af1c6871SKent Overstreet 
5943c471b65SKent Overstreet 	set_bit(BCH_FS_stopping, &c->flags);
5951dd7f9d9SKent Overstreet 
5969b6e2f1eSKent Overstreet 	cancel_work_sync(&c->journal_seq_blacklist_gc_work);
5979b6e2f1eSKent Overstreet 
5981ada1606SKent Overstreet 	down_write(&c->state_lock);
599883f1a7cSKent Overstreet 	bch2_fs_read_only(c);
6001ada1606SKent Overstreet 	up_write(&c->state_lock);
601883f1a7cSKent Overstreet 
6029fea2274SKent Overstreet 	for_each_member_device(c, ca)
6031c6fdbd8SKent Overstreet 		if (ca->kobj.state_in_sysfs &&
6041c6fdbd8SKent Overstreet 		    ca->disk_sb.bdev)
6051c6fdbd8SKent Overstreet 			sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
6061c6fdbd8SKent Overstreet 
6071c6fdbd8SKent Overstreet 	if (c->kobj.state_in_sysfs)
6081c6fdbd8SKent Overstreet 		kobject_del(&c->kobj);
6091c6fdbd8SKent Overstreet 
6101c6fdbd8SKent Overstreet 	bch2_fs_debug_exit(c);
6111c6fdbd8SKent Overstreet 	bch2_fs_chardev_exit(c);
6121c6fdbd8SKent Overstreet 
61363508b75SKent Overstreet 	bch2_ro_ref_put(c);
61463508b75SKent Overstreet 	wait_event(c->ro_ref_wait, !refcount_read(&c->ro_ref));
61563508b75SKent Overstreet 
616104c6974SDaniel Hill 	kobject_put(&c->counters_kobj);
6171c6fdbd8SKent Overstreet 	kobject_put(&c->time_stats);
6181c6fdbd8SKent Overstreet 	kobject_put(&c->opts_dir);
6191c6fdbd8SKent Overstreet 	kobject_put(&c->internal);
6201c6fdbd8SKent Overstreet 
6211c6fdbd8SKent Overstreet 	/* btree prefetch might have kicked off reads in the background: */
6221c6fdbd8SKent Overstreet 	bch2_btree_flush_all_reads(c);
6231c6fdbd8SKent Overstreet 
6249fea2274SKent Overstreet 	for_each_member_device(c, ca)
6251c6fdbd8SKent Overstreet 		cancel_work_sync(&ca->io_error_work);
6261c6fdbd8SKent Overstreet 
6271c6fdbd8SKent Overstreet 	cancel_work_sync(&c->read_only_work);
628d5e4dcc2SKent Overstreet }
6291c6fdbd8SKent Overstreet 
630d5e4dcc2SKent Overstreet void bch2_fs_free(struct bch_fs *c)
631d5e4dcc2SKent Overstreet {
632d5e4dcc2SKent Overstreet 	unsigned i;
633d5e4dcc2SKent Overstreet 
634d5e4dcc2SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
635d5e4dcc2SKent Overstreet 	list_del(&c->list);
636d5e4dcc2SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
637d5e4dcc2SKent Overstreet 
638d5e4dcc2SKent Overstreet 	closure_sync(&c->cl);
639d5e4dcc2SKent Overstreet 	closure_debug_destroy(&c->cl);
640d5e4dcc2SKent Overstreet 
641d5e4dcc2SKent Overstreet 	for (i = 0; i < c->sb.nr_devices; i++) {
642d5e4dcc2SKent Overstreet 		struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true);
643d5e4dcc2SKent Overstreet 
644d5e4dcc2SKent Overstreet 		if (ca) {
645d5e4dcc2SKent Overstreet 			bch2_free_super(&ca->disk_sb);
646d5e4dcc2SKent Overstreet 			bch2_dev_free(ca);
647d5e4dcc2SKent Overstreet 		}
648d5e4dcc2SKent Overstreet 	}
6491c6fdbd8SKent Overstreet 
650af1c6871SKent Overstreet 	bch_verbose(c, "shutdown complete");
651af1c6871SKent Overstreet 
6521c6fdbd8SKent Overstreet 	kobject_put(&c->kobj);
6531c6fdbd8SKent Overstreet }
6541c6fdbd8SKent Overstreet 
655d5e4dcc2SKent Overstreet void bch2_fs_stop(struct bch_fs *c)
656d5e4dcc2SKent Overstreet {
657d5e4dcc2SKent Overstreet 	__bch2_fs_stop(c);
658d5e4dcc2SKent Overstreet 	bch2_fs_free(c);
659d5e4dcc2SKent Overstreet }
660d5e4dcc2SKent Overstreet 
661e2b60560SKent Overstreet static int bch2_fs_online(struct bch_fs *c)
6621c6fdbd8SKent Overstreet {
663e2b60560SKent Overstreet 	int ret = 0;
6641c6fdbd8SKent Overstreet 
6651c6fdbd8SKent Overstreet 	lockdep_assert_held(&bch_fs_list_lock);
6661c6fdbd8SKent Overstreet 
667e2b60560SKent Overstreet 	if (__bch2_uuid_to_fs(c->sb.uuid)) {
668e2b60560SKent Overstreet 		bch_err(c, "filesystem UUID already open");
669e2b60560SKent Overstreet 		return -EINVAL;
670e2b60560SKent Overstreet 	}
6711c6fdbd8SKent Overstreet 
6721c6fdbd8SKent Overstreet 	ret = bch2_fs_chardev_init(c);
673e2b60560SKent Overstreet 	if (ret) {
674e2b60560SKent Overstreet 		bch_err(c, "error creating character device");
675e2b60560SKent Overstreet 		return ret;
676e2b60560SKent Overstreet 	}
6771c6fdbd8SKent Overstreet 
6781c6fdbd8SKent Overstreet 	bch2_fs_debug_init(c);
6791c6fdbd8SKent Overstreet 
680e2b60560SKent Overstreet 	ret = kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ?:
681e2b60560SKent Overstreet 	    kobject_add(&c->internal, &c->kobj, "internal") ?:
682e2b60560SKent Overstreet 	    kobject_add(&c->opts_dir, &c->kobj, "options") ?:
683066a2646SKent Overstreet #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT
684e2b60560SKent Overstreet 	    kobject_add(&c->time_stats, &c->kobj, "time_stats") ?:
685066a2646SKent Overstreet #endif
686104c6974SDaniel Hill 	    kobject_add(&c->counters_kobj, &c->kobj, "counters") ?:
687e2b60560SKent Overstreet 	    bch2_opts_create_sysfs_files(&c->opts_dir);
688e2b60560SKent Overstreet 	if (ret) {
689e2b60560SKent Overstreet 		bch_err(c, "error creating sysfs objects");
690e2b60560SKent Overstreet 		return ret;
691e2b60560SKent Overstreet 	}
6921c6fdbd8SKent Overstreet 
6931ada1606SKent Overstreet 	down_write(&c->state_lock);
6941c6fdbd8SKent Overstreet 
6959fea2274SKent Overstreet 	for_each_member_device(c, ca) {
696e2b60560SKent Overstreet 		ret = bch2_dev_sysfs_online(c, ca);
697e2b60560SKent Overstreet 		if (ret) {
698e2b60560SKent Overstreet 			bch_err(c, "error creating sysfs objects");
6993a402c8dSKent Overstreet 			percpu_ref_put(&ca->ref);
7001c6fdbd8SKent Overstreet 			goto err;
7013a402c8dSKent Overstreet 		}
702e2b60560SKent Overstreet 	}
7031c6fdbd8SKent Overstreet 
704e2b60560SKent Overstreet 	BUG_ON(!list_empty(&c->list));
7051c6fdbd8SKent Overstreet 	list_add(&c->list, &bch_fs_list);
7061c6fdbd8SKent Overstreet err:
7071ada1606SKent Overstreet 	up_write(&c->state_lock);
708e2b60560SKent Overstreet 	return ret;
7091c6fdbd8SKent Overstreet }
7101c6fdbd8SKent Overstreet 
7111c6fdbd8SKent Overstreet static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
7121c6fdbd8SKent Overstreet {
7131c6fdbd8SKent Overstreet 	struct bch_fs *c;
714401ec4dbSKent Overstreet 	struct printbuf name = PRINTBUF;
715ecf37a4aSKent Overstreet 	unsigned i, iter_size;
7167be9ab63SChris Webb 	int ret = 0;
7171c6fdbd8SKent Overstreet 
7181c6fdbd8SKent Overstreet 	c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO);
7197be9ab63SChris Webb 	if (!c) {
72065d48e35SKent Overstreet 		c = ERR_PTR(-BCH_ERR_ENOMEM_fs_alloc);
7211c6fdbd8SKent Overstreet 		goto out;
7227be9ab63SChris Webb 	}
7231c6fdbd8SKent Overstreet 
72496f37eabSKent Overstreet 	c->stdio = (void *)(unsigned long) opts.stdio;
7252b41226dSKent Overstreet 
7261c6fdbd8SKent Overstreet 	__module_get(THIS_MODULE);
7271c6fdbd8SKent Overstreet 
728505b7a4cSKent Overstreet 	closure_init(&c->cl, NULL);
729505b7a4cSKent Overstreet 
730505b7a4cSKent Overstreet 	c->kobj.kset = bcachefs_kset;
731505b7a4cSKent Overstreet 	kobject_init(&c->kobj, &bch2_fs_ktype);
732505b7a4cSKent Overstreet 	kobject_init(&c->internal, &bch2_fs_internal_ktype);
733505b7a4cSKent Overstreet 	kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype);
734505b7a4cSKent Overstreet 	kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype);
735104c6974SDaniel Hill 	kobject_init(&c->counters_kobj, &bch2_fs_counters_ktype);
736505b7a4cSKent Overstreet 
7371c6fdbd8SKent Overstreet 	c->minor		= -1;
7381c6fdbd8SKent Overstreet 	c->disk_sb.fs_sb	= true;
7391c6fdbd8SKent Overstreet 
7401ada1606SKent Overstreet 	init_rwsem(&c->state_lock);
7411c6fdbd8SKent Overstreet 	mutex_init(&c->sb_lock);
7421c6fdbd8SKent Overstreet 	mutex_init(&c->replicas_gc_lock);
7431c6fdbd8SKent Overstreet 	mutex_init(&c->btree_root_lock);
7441c6fdbd8SKent Overstreet 	INIT_WORK(&c->read_only_work, bch2_fs_read_only_work);
7451c6fdbd8SKent Overstreet 
74663508b75SKent Overstreet 	refcount_set(&c->ro_ref, 1);
74763508b75SKent Overstreet 	init_waitqueue_head(&c->ro_ref_wait);
748267b801fSKent Overstreet 	sema_init(&c->online_fsck_mutex, 1);
74963508b75SKent Overstreet 
7501c6fdbd8SKent Overstreet 	init_rwsem(&c->gc_lock);
751c45c8667SKent Overstreet 	mutex_init(&c->gc_gens_lock);
7528a443d3eSKent Overstreet 	atomic_set(&c->journal_keys.ref, 1);
7538a443d3eSKent Overstreet 	c->journal_keys.initial_ref_held = true;
7541c6fdbd8SKent Overstreet 
7551c6fdbd8SKent Overstreet 	for (i = 0; i < BCH_TIME_STAT_NR; i++)
7561c6fdbd8SKent Overstreet 		bch2_time_stats_init(&c->times[i]);
7571c6fdbd8SKent Overstreet 
758e6d11615SKent Overstreet 	bch2_fs_copygc_init(c);
7592ca88e5aSKent Overstreet 	bch2_fs_btree_key_cache_init_early(&c->btree_key_cache);
76050a8a732SThomas Bertschinger 	bch2_fs_btree_iter_init_early(c);
76165db6049SKent Overstreet 	bch2_fs_btree_interior_update_init_early(c);
762b092daddSKent Overstreet 	bch2_fs_allocator_background_init(c);
763b092daddSKent Overstreet 	bch2_fs_allocator_foreground_init(c);
7641c6fdbd8SKent Overstreet 	bch2_fs_rebalance_init(c);
7651c6fdbd8SKent Overstreet 	bch2_fs_quota_init(c);
76684c72755SKent Overstreet 	bch2_fs_ec_init_early(c);
767b9fa375bSKent Overstreet 	bch2_fs_move_init(c);
768f5d26fa3SKent Overstreet 	bch2_fs_sb_errors_init_early(c);
7691c6fdbd8SKent Overstreet 
7701c6fdbd8SKent Overstreet 	INIT_LIST_HEAD(&c->list);
7711c6fdbd8SKent Overstreet 
7724d8100daSKent Overstreet 	mutex_init(&c->usage_scratch_lock);
7734d8100daSKent Overstreet 
7741c6fdbd8SKent Overstreet 	mutex_init(&c->bio_bounce_pages_lock);
77514b393eeSKent Overstreet 	mutex_init(&c->snapshot_table_lock);
77637fad949SKent Overstreet 	init_rwsem(&c->snapshot_create_lock);
7771c6fdbd8SKent Overstreet 
7781c6fdbd8SKent Overstreet 	spin_lock_init(&c->btree_write_error_lock);
7791c6fdbd8SKent Overstreet 
7809b6e2f1eSKent Overstreet 	INIT_WORK(&c->journal_seq_blacklist_gc_work,
7819b6e2f1eSKent Overstreet 		  bch2_blacklist_entries_gc);
7829b6e2f1eSKent Overstreet 
7835b593ee1SKent Overstreet 	INIT_LIST_HEAD(&c->journal_iters);
784f1d786a0SKent Overstreet 
785f5d26fa3SKent Overstreet 	INIT_LIST_HEAD(&c->fsck_error_msgs);
786f5d26fa3SKent Overstreet 	mutex_init(&c->fsck_error_msgs_lock);
7871c6fdbd8SKent Overstreet 
7881c6fdbd8SKent Overstreet 	seqcount_init(&c->gc_pos_lock);
7891c6fdbd8SKent Overstreet 
7905e82a9a1SKent Overstreet 	seqcount_init(&c->usage_lock);
7915e82a9a1SKent Overstreet 
792ef1b2092SKent Overstreet 	sema_init(&c->io_in_flight, 128);
793ef1b2092SKent Overstreet 
7949edbcc72SKent Overstreet 	INIT_LIST_HEAD(&c->vfs_inodes_list);
7959edbcc72SKent Overstreet 	mutex_init(&c->vfs_inodes_lock);
7969edbcc72SKent Overstreet 
7971c6fdbd8SKent Overstreet 	c->copy_gc_enabled		= 1;
7981c6fdbd8SKent Overstreet 	c->rebalance.enabled		= 1;
7991c6fdbd8SKent Overstreet 	c->promote_whole_extents	= true;
8001c6fdbd8SKent Overstreet 
801991ba021SKent Overstreet 	c->journal.flush_write_time	= &c->times[BCH_TIME_journal_flush_write];
802991ba021SKent Overstreet 	c->journal.noflush_write_time	= &c->times[BCH_TIME_journal_noflush_write];
8031c6fdbd8SKent Overstreet 	c->journal.flush_seq_time	= &c->times[BCH_TIME_journal_flush_seq];
8041c6fdbd8SKent Overstreet 
8051c6fdbd8SKent Overstreet 	bch2_fs_btree_cache_init_early(&c->btree_cache);
8061c6fdbd8SKent Overstreet 
807fca1223cSKent Overstreet 	mutex_init(&c->sectors_available_lock);
808fca1223cSKent Overstreet 
809e2b60560SKent Overstreet 	ret = percpu_init_rwsem(&c->mark_lock);
810e2b60560SKent Overstreet 	if (ret)
81173e6ab95SKent Overstreet 		goto err;
81273e6ab95SKent Overstreet 
8131c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
814e2b60560SKent Overstreet 	ret = bch2_sb_to_fs(c, sb);
8151c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
816e2b60560SKent Overstreet 
817e2b60560SKent Overstreet 	if (ret)
8181c6fdbd8SKent Overstreet 		goto err;
8191c6fdbd8SKent Overstreet 
820401ec4dbSKent Overstreet 	pr_uuid(&name, c->sb.user_uuid.b);
8213e3e02e6SKent Overstreet 	strscpy(c->name, name.buf, sizeof(c->name));
822401ec4dbSKent Overstreet 	printbuf_exit(&name);
823401ec4dbSKent Overstreet 
82465d48e35SKent Overstreet 	ret = name.allocation_failure ? -BCH_ERR_ENOMEM_fs_name_alloc : 0;
825401ec4dbSKent Overstreet 	if (ret)
826401ec4dbSKent Overstreet 		goto err;
8271c6fdbd8SKent Overstreet 
8282430e72fSKent Overstreet 	/* Compat: */
82973bd774dSKent Overstreet 	if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 &&
8302430e72fSKent Overstreet 	    !BCH_SB_JOURNAL_FLUSH_DELAY(sb))
8312430e72fSKent Overstreet 		SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000);
8322430e72fSKent Overstreet 
83373bd774dSKent Overstreet 	if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 &&
8342430e72fSKent Overstreet 	    !BCH_SB_JOURNAL_RECLAIM_DELAY(sb))
8352430e72fSKent Overstreet 		SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 100);
8362430e72fSKent Overstreet 
8371c6fdbd8SKent Overstreet 	c->opts = bch2_opts_default;
8388244f320SKent Overstreet 	ret = bch2_opts_from_sb(&c->opts, sb);
8398244f320SKent Overstreet 	if (ret)
8408244f320SKent Overstreet 		goto err;
8418244f320SKent Overstreet 
8421c6fdbd8SKent Overstreet 	bch2_opts_apply(&c->opts, opts);
8431c6fdbd8SKent Overstreet 
8447c8f6f98SKent Overstreet 	c->btree_key_cache_btrees |= 1U << BTREE_ID_alloc;
8457c8f6f98SKent Overstreet 	if (c->opts.inodes_use_key_cache)
8467c8f6f98SKent Overstreet 		c->btree_key_cache_btrees |= 1U << BTREE_ID_inodes;
847aaad530aSKent Overstreet 	c->btree_key_cache_btrees |= 1U << BTREE_ID_logged_ops;
8487c8f6f98SKent Overstreet 
8498244f320SKent Overstreet 	c->block_bits		= ilog2(block_sectors(c));
8501c6fdbd8SKent Overstreet 	c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
8511c6fdbd8SKent Overstreet 
8527be9ab63SChris Webb 	if (bch2_fs_init_fault("fs_alloc")) {
853e2b60560SKent Overstreet 		bch_err(c, "fs_alloc fault injected");
854e2b60560SKent Overstreet 		ret = -EFAULT;
8551c6fdbd8SKent Overstreet 		goto err;
8567be9ab63SChris Webb 	}
8571c6fdbd8SKent Overstreet 
858ae2f17d5SKent Overstreet 	iter_size = sizeof(struct sort_iter) +
8591c6fdbd8SKent Overstreet 		(btree_blocks(c) + 1) * 2 *
860ae2f17d5SKent Overstreet 		sizeof(struct sort_iter_set);
8611c6fdbd8SKent Overstreet 
862b5e8a699SKent Overstreet 	c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus()));
863b5e8a699SKent Overstreet 
864731bdd2eSKent Overstreet 	if (!(c->btree_update_wq = alloc_workqueue("bcachefs",
865b17d3cecSKent Overstreet 				WQ_FREEZABLE|WQ_UNBOUND|WQ_MEM_RECLAIM, 512)) ||
8669f1833caSKent Overstreet 	    !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io",
8679f2772c4SKent Overstreet 				WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) ||
8682f33ece9SKent Overstreet 	    !(c->copygc_wq = alloc_workqueue("bcachefs_copygc",
8692f33ece9SKent Overstreet 				WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
870731bdd2eSKent Overstreet 	    !(c->io_complete_wq = alloc_workqueue("bcachefs_io",
8714798bd24SKent Overstreet 				WQ_FREEZABLE|WQ_HIGHPRI|WQ_MEM_RECLAIM, 512)) ||
8728bff9875SBrian Foster 	    !(c->write_ref_wq = alloc_workqueue("bcachefs_write_ref",
8738bff9875SBrian Foster 				WQ_FREEZABLE, 0)) ||
874d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG
875134915f3SKent Overstreet 	    percpu_ref_init(&c->writes, bch2_writes_disabled,
876134915f3SKent Overstreet 			    PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
877d94189adSKent Overstreet #endif
8781c6fdbd8SKent Overstreet 	    mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
8791c6fdbd8SKent Overstreet 	    bioset_init(&c->btree_bio, 1,
8801c6fdbd8SKent Overstreet 			max(offsetof(struct btree_read_bio, bio),
8811c6fdbd8SKent Overstreet 			    offsetof(struct btree_write_bio, wbio.bio)),
8821c6fdbd8SKent Overstreet 			BIOSET_NEED_BVECS) ||
8835663a415SKent Overstreet 	    !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) ||
8845e82a9a1SKent Overstreet 	    !(c->online_reserved = alloc_percpu(u64)) ||
8851c6fdbd8SKent Overstreet 	    mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1,
886ec4edd7bSKent Overstreet 					c->opts.btree_node_size) ||
88735189e09SKent Overstreet 	    mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) ||
888b5e8a699SKent Overstreet 	    !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits,
889e2b60560SKent Overstreet 					      sizeof(u64), GFP_KERNEL))) {
89065d48e35SKent Overstreet 		ret = -BCH_ERR_ENOMEM_fs_other_alloc;
8917be9ab63SChris Webb 		goto err;
8927be9ab63SChris Webb 	}
8937be9ab63SChris Webb 
894f3b8403eSKent Overstreet 	ret = bch2_fs_counters_init(c) ?:
895f5d26fa3SKent Overstreet 	    bch2_fs_sb_errors_init(c) ?:
896f3b8403eSKent Overstreet 	    bch2_io_clock_init(&c->io_clock[READ]) ?:
897e2b60560SKent Overstreet 	    bch2_io_clock_init(&c->io_clock[WRITE]) ?:
898e2b60560SKent Overstreet 	    bch2_fs_journal_init(&c->journal) ?:
899e2b60560SKent Overstreet 	    bch2_fs_replicas_init(c) ?:
900e2b60560SKent Overstreet 	    bch2_fs_btree_cache_init(c) ?:
901e2b60560SKent Overstreet 	    bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?:
902e2b60560SKent Overstreet 	    bch2_fs_btree_iter_init(c) ?:
903e2b60560SKent Overstreet 	    bch2_fs_btree_interior_update_init(c) ?:
9049b688da3SKent Overstreet 	    bch2_fs_buckets_waiting_for_journal_init(c) ?:
905920e69bcSKent Overstreet 	    bch2_fs_btree_write_buffer_init(c) ?:
906e2b60560SKent Overstreet 	    bch2_fs_subvolumes_init(c) ?:
9071809b8cbSKent Overstreet 	    bch2_fs_io_read_init(c) ?:
9081809b8cbSKent Overstreet 	    bch2_fs_io_write_init(c) ?:
909350175bfSKent Overstreet 	    bch2_fs_nocow_locking_init(c) ?:
910e2b60560SKent Overstreet 	    bch2_fs_encryption_init(c) ?:
911e2b60560SKent Overstreet 	    bch2_fs_compress_init(c) ?:
912e2b60560SKent Overstreet 	    bch2_fs_ec_init(c) ?:
913dbbfca9fSKent Overstreet 	    bch2_fs_fsio_init(c) ?:
914867c1fe0SDan Carpenter 	    bch2_fs_fs_io_buffered_init(c) ?:
915dbbfca9fSKent Overstreet 	    bch2_fs_fs_io_direct_init(c);
9167be9ab63SChris Webb 	if (ret)
9171c6fdbd8SKent Overstreet 		goto err;
9181c6fdbd8SKent Overstreet 
9191c6fdbd8SKent Overstreet 	for (i = 0; i < c->sb.nr_devices; i++)
9201241df58SHunter Shaffer 		if (bch2_dev_exists(c->disk_sb.sb, i) &&
9217be9ab63SChris Webb 		    bch2_dev_alloc(c, i)) {
922e2b60560SKent Overstreet 			ret = -EEXIST;
9231c6fdbd8SKent Overstreet 			goto err;
9247be9ab63SChris Webb 		}
9251c6fdbd8SKent Overstreet 
9264b8f89afSKent Overstreet 	bch2_journal_entry_res_resize(&c->journal,
9274b8f89afSKent Overstreet 			&c->btree_root_journal_res,
9284b8f89afSKent Overstreet 			BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX));
9294b8f89afSKent Overstreet 	bch2_dev_usage_journal_reserve(c);
9304b8f89afSKent Overstreet 	bch2_journal_entry_res_resize(&c->journal,
9314b8f89afSKent Overstreet 			&c->clock_journal_res,
9324b8f89afSKent Overstreet 			(sizeof(struct jset_entry_clock) / sizeof(u64)) * 2);
9334b8f89afSKent Overstreet 
9341c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
935e2b60560SKent Overstreet 	ret = bch2_fs_online(c);
9361c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
937e2b60560SKent Overstreet 
938e2b60560SKent Overstreet 	if (ret)
9391c6fdbd8SKent Overstreet 		goto err;
9401c6fdbd8SKent Overstreet out:
9411c6fdbd8SKent Overstreet 	return c;
9421c6fdbd8SKent Overstreet err:
9431c6fdbd8SKent Overstreet 	bch2_fs_free(c);
9447be9ab63SChris Webb 	c = ERR_PTR(ret);
9451c6fdbd8SKent Overstreet 	goto out;
9461c6fdbd8SKent Overstreet }
9471c6fdbd8SKent Overstreet 
948619f5beeSKent Overstreet noinline_for_stack
949619f5beeSKent Overstreet static void print_mount_opts(struct bch_fs *c)
950619f5beeSKent Overstreet {
951619f5beeSKent Overstreet 	enum bch_opt_id i;
952fa8e94faSKent Overstreet 	struct printbuf p = PRINTBUF;
953619f5beeSKent Overstreet 	bool first = true;
954619f5beeSKent Overstreet 
955ef1634f0SKent Overstreet 	prt_str(&p, "mounting version ");
956e3804b55SKent Overstreet 	bch2_version_to_text(&p, c->sb.version);
95760573ff5SKent Overstreet 
958619f5beeSKent Overstreet 	if (c->opts.read_only) {
95960573ff5SKent Overstreet 		prt_str(&p, " opts=");
960619f5beeSKent Overstreet 		first = false;
96160573ff5SKent Overstreet 		prt_printf(&p, "ro");
962619f5beeSKent Overstreet 	}
963619f5beeSKent Overstreet 
964619f5beeSKent Overstreet 	for (i = 0; i < bch2_opts_nr; i++) {
965619f5beeSKent Overstreet 		const struct bch_option *opt = &bch2_opt_table[i];
966619f5beeSKent Overstreet 		u64 v = bch2_opt_get_by_id(&c->opts, i);
967619f5beeSKent Overstreet 
9688244f320SKent Overstreet 		if (!(opt->flags & OPT_MOUNT))
969619f5beeSKent Overstreet 			continue;
970619f5beeSKent Overstreet 
971619f5beeSKent Overstreet 		if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
972619f5beeSKent Overstreet 			continue;
973619f5beeSKent Overstreet 
97460573ff5SKent Overstreet 		prt_str(&p, first ? " opts=" : ",");
975619f5beeSKent Overstreet 		first = false;
9765521b1dfSKent Overstreet 		bch2_opt_to_text(&p, c, c->disk_sb.sb, opt, v, OPT_SHOW_MOUNT_STYLE);
977619f5beeSKent Overstreet 	}
978619f5beeSKent Overstreet 
97960573ff5SKent Overstreet 	bch_info(c, "%s", p.buf);
980fa8e94faSKent Overstreet 	printbuf_exit(&p);
981619f5beeSKent Overstreet }
982619f5beeSKent Overstreet 
983619f5beeSKent Overstreet int bch2_fs_start(struct bch_fs *c)
9841c6fdbd8SKent Overstreet {
985a420eea6STim Schlueter 	time64_t now = ktime_get_real_seconds();
98678c0b75cSKent Overstreet 	int ret;
9871c6fdbd8SKent Overstreet 
988ef1634f0SKent Overstreet 	print_mount_opts(c);
989ef1634f0SKent Overstreet 
9901ada1606SKent Overstreet 	down_write(&c->state_lock);
9911c6fdbd8SKent Overstreet 
9923c471b65SKent Overstreet 	BUG_ON(test_bit(BCH_FS_started, &c->flags));
9931c6fdbd8SKent Overstreet 
9941c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
9951c6fdbd8SKent Overstreet 
996f5d26fa3SKent Overstreet 	ret = bch2_sb_members_v2_init(c);
9973f7b9713SHunter Shaffer 	if (ret) {
9983f7b9713SHunter Shaffer 		mutex_unlock(&c->sb_lock);
9993f7b9713SHunter Shaffer 		goto err;
10003f7b9713SHunter Shaffer 	}
10013f7b9713SHunter Shaffer 
10029fea2274SKent Overstreet 	for_each_online_member(c, ca)
10039fea2274SKent Overstreet 		bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = cpu_to_le64(now);
10041c6fdbd8SKent Overstreet 
10051c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
10061c6fdbd8SKent Overstreet 
10079fea2274SKent Overstreet 	for_each_rw_member(c, ca)
10081c6fdbd8SKent Overstreet 		bch2_dev_allocator_add(c, ca);
10091c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
10101c6fdbd8SKent Overstreet 
10111c6fdbd8SKent Overstreet 	ret = BCH_SB_INITIALIZED(c->disk_sb.sb)
10121c6fdbd8SKent Overstreet 		? bch2_fs_recovery(c)
10131c6fdbd8SKent Overstreet 		: bch2_fs_initialize(c);
10141c6fdbd8SKent Overstreet 	if (ret)
10151c6fdbd8SKent Overstreet 		goto err;
10161c6fdbd8SKent Overstreet 
1017cd575ddfSKent Overstreet 	ret = bch2_opts_check_may_set(c);
1018cd575ddfSKent Overstreet 	if (ret)
1019cd575ddfSKent Overstreet 		goto err;
1020cd575ddfSKent Overstreet 
1021e2b60560SKent Overstreet 	if (bch2_fs_init_fault("fs_start")) {
1022e2b60560SKent Overstreet 		bch_err(c, "fs_start fault injected");
102378c0b75cSKent Overstreet 		ret = -EINVAL;
10241c6fdbd8SKent Overstreet 		goto err;
1025e2b60560SKent Overstreet 	}
10261c6fdbd8SKent Overstreet 
10273c471b65SKent Overstreet 	set_bit(BCH_FS_started, &c->flags);
1028a9310ab0SKent Overstreet 
102962719cf3SKent Overstreet 	if (c->opts.read_only) {
10301c6fdbd8SKent Overstreet 		bch2_fs_read_only(c);
10311c6fdbd8SKent Overstreet 	} else {
10323c471b65SKent Overstreet 		ret = !test_bit(BCH_FS_rw, &c->flags)
1033619f5beeSKent Overstreet 			? bch2_fs_read_write(c)
1034619f5beeSKent Overstreet 			: bch2_fs_read_write_late(c);
1035619f5beeSKent Overstreet 		if (ret)
10361c6fdbd8SKent Overstreet 			goto err;
10371c6fdbd8SKent Overstreet 	}
10381c6fdbd8SKent Overstreet 
1039619f5beeSKent Overstreet 	ret = 0;
1040e34ec13aSKent Overstreet err:
1041e34ec13aSKent Overstreet 	if (ret)
1042e34ec13aSKent Overstreet 		bch_err_msg(c, ret, "starting filesystem");
1043e34ec13aSKent Overstreet 	else
1044e34ec13aSKent Overstreet 		bch_verbose(c, "done starting filesystem");
10451ada1606SKent Overstreet 	up_write(&c->state_lock);
1046619f5beeSKent Overstreet 	return ret;
10471c6fdbd8SKent Overstreet }
10481c6fdbd8SKent Overstreet 
104978c0b75cSKent Overstreet static int bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
10501c6fdbd8SKent Overstreet {
10511241df58SHunter Shaffer 	struct bch_member m = bch2_sb_member_get(sb, sb->dev_idx);
10521c6fdbd8SKent Overstreet 
10538244f320SKent Overstreet 	if (le16_to_cpu(sb->block_size) != block_sectors(c))
105478c0b75cSKent Overstreet 		return -BCH_ERR_mismatched_block_size;
10551c6fdbd8SKent Overstreet 
10561241df58SHunter Shaffer 	if (le16_to_cpu(m.bucket_size) <
10571c6fdbd8SKent Overstreet 	    BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
105878c0b75cSKent Overstreet 		return -BCH_ERR_bucket_size_too_small;
10591c6fdbd8SKent Overstreet 
106078c0b75cSKent Overstreet 	return 0;
10611c6fdbd8SKent Overstreet }
10621c6fdbd8SKent Overstreet 
10630d529663SKent Overstreet static int bch2_dev_in_fs(struct bch_sb_handle *fs,
10640d529663SKent Overstreet 			  struct bch_sb_handle *sb)
10651c6fdbd8SKent Overstreet {
10660d529663SKent Overstreet 	if (fs == sb)
10670d529663SKent Overstreet 		return 0;
10681c6fdbd8SKent Overstreet 
10690d529663SKent Overstreet 	if (!uuid_equal(&fs->sb->uuid, &sb->sb->uuid))
107078c0b75cSKent Overstreet 		return -BCH_ERR_device_not_a_member_of_filesystem;
10711c6fdbd8SKent Overstreet 
10720d529663SKent Overstreet 	if (!bch2_dev_exists(fs->sb, sb->sb->dev_idx))
107378c0b75cSKent Overstreet 		return -BCH_ERR_device_has_been_removed;
10741c6fdbd8SKent Overstreet 
10750d529663SKent Overstreet 	if (fs->sb->block_size != sb->sb->block_size)
107678c0b75cSKent Overstreet 		return -BCH_ERR_mismatched_block_size;
10771c6fdbd8SKent Overstreet 
10780d529663SKent Overstreet 	if (le16_to_cpu(fs->sb->version) < bcachefs_metadata_version_member_seq ||
10790d529663SKent Overstreet 	    le16_to_cpu(sb->sb->version) < bcachefs_metadata_version_member_seq)
10800d529663SKent Overstreet 		return 0;
10810d529663SKent Overstreet 
10820d529663SKent Overstreet 	if (fs->sb->seq == sb->sb->seq &&
10830d529663SKent Overstreet 	    fs->sb->write_time != sb->sb->write_time) {
10840d529663SKent Overstreet 		struct printbuf buf = PRINTBUF;
10850d529663SKent Overstreet 
10861f5af5fcSKent Overstreet 		prt_str(&buf, "Split brain detected between ");
10871f5af5fcSKent Overstreet 		prt_bdevname(&buf, sb->bdev);
10881f5af5fcSKent Overstreet 		prt_str(&buf, " and ");
10891f5af5fcSKent Overstreet 		prt_bdevname(&buf, fs->bdev);
10901f5af5fcSKent Overstreet 		prt_char(&buf, ':');
10910d529663SKent Overstreet 		prt_newline(&buf);
10920d529663SKent Overstreet 		prt_printf(&buf, "seq=%llu but write_time different, got", le64_to_cpu(sb->sb->seq));
10930d529663SKent Overstreet 		prt_newline(&buf);
10940d529663SKent Overstreet 
10951f5af5fcSKent Overstreet 		prt_bdevname(&buf, fs->bdev);
10961f5af5fcSKent Overstreet 		prt_char(&buf, ' ');
10970d529663SKent Overstreet 		bch2_prt_datetime(&buf, le64_to_cpu(fs->sb->write_time));;
10980d529663SKent Overstreet 		prt_newline(&buf);
10990d529663SKent Overstreet 
11001f5af5fcSKent Overstreet 		prt_bdevname(&buf, sb->bdev);
11011f5af5fcSKent Overstreet 		prt_char(&buf, ' ');
11020d529663SKent Overstreet 		bch2_prt_datetime(&buf, le64_to_cpu(sb->sb->write_time));;
11030d529663SKent Overstreet 		prt_newline(&buf);
11040d529663SKent Overstreet 
11050d529663SKent Overstreet 		prt_printf(&buf, "Not using older sb");
11060d529663SKent Overstreet 
11070d529663SKent Overstreet 		pr_err("%s", buf.buf);
11080d529663SKent Overstreet 		printbuf_exit(&buf);
11090d529663SKent Overstreet 		return -BCH_ERR_device_splitbrain;
11100d529663SKent Overstreet 	}
11110d529663SKent Overstreet 
11120d529663SKent Overstreet 	struct bch_member m = bch2_sb_member_get(fs->sb, sb->sb->dev_idx);
11130d529663SKent Overstreet 	u64 seq_from_fs		= le64_to_cpu(m.seq);
11140d529663SKent Overstreet 	u64 seq_from_member	= le64_to_cpu(sb->sb->seq);
11150d529663SKent Overstreet 
11160d529663SKent Overstreet 	if (seq_from_fs && seq_from_fs < seq_from_member) {
11171f5af5fcSKent Overstreet 		struct printbuf buf = PRINTBUF;
11181f5af5fcSKent Overstreet 
11191f5af5fcSKent Overstreet 		prt_str(&buf, "Split brain detected between ");
11201f5af5fcSKent Overstreet 		prt_bdevname(&buf, sb->bdev);
11211f5af5fcSKent Overstreet 		prt_str(&buf, " and ");
11221f5af5fcSKent Overstreet 		prt_bdevname(&buf, fs->bdev);
11231f5af5fcSKent Overstreet 		prt_char(&buf, ':');
11241f5af5fcSKent Overstreet 		prt_newline(&buf);
11251f5af5fcSKent Overstreet 
11261f5af5fcSKent Overstreet 		prt_bdevname(&buf, fs->bdev);
11271f5af5fcSKent Overstreet 		prt_str(&buf, "believes seq of ");
11281f5af5fcSKent Overstreet 		prt_bdevname(&buf, sb->bdev);
11291f5af5fcSKent Overstreet 		prt_printf(&buf, " to be %llu, but ", seq_from_fs);
11301f5af5fcSKent Overstreet 		prt_bdevname(&buf, sb->bdev);
11311f5af5fcSKent Overstreet 		prt_printf(&buf, " has %llu\n", seq_from_member);
11321f5af5fcSKent Overstreet 		prt_str(&buf, "Not using ");
11331f5af5fcSKent Overstreet 		prt_bdevname(&buf, sb->bdev);
11341f5af5fcSKent Overstreet 
11351f5af5fcSKent Overstreet 		pr_err("%s", buf.buf);
11361f5af5fcSKent Overstreet 		printbuf_exit(&buf);
11370d529663SKent Overstreet 		return -BCH_ERR_device_splitbrain;
11380d529663SKent Overstreet 	}
11390d529663SKent Overstreet 
114078c0b75cSKent Overstreet 	return 0;
11411c6fdbd8SKent Overstreet }
11421c6fdbd8SKent Overstreet 
11431c6fdbd8SKent Overstreet /* Device startup/shutdown: */
11441c6fdbd8SKent Overstreet 
11451c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *kobj)
11461c6fdbd8SKent Overstreet {
11471c6fdbd8SKent Overstreet 	struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
11481c6fdbd8SKent Overstreet 
11491c6fdbd8SKent Overstreet 	kfree(ca);
11501c6fdbd8SKent Overstreet }
11511c6fdbd8SKent Overstreet 
11521c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *ca)
11531c6fdbd8SKent Overstreet {
11541c6fdbd8SKent Overstreet 	cancel_work_sync(&ca->io_error_work);
11551c6fdbd8SKent Overstreet 
11561c6fdbd8SKent Overstreet 	if (ca->kobj.state_in_sysfs &&
11571c6fdbd8SKent Overstreet 	    ca->disk_sb.bdev)
11581c6fdbd8SKent Overstreet 		sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
11591c6fdbd8SKent Overstreet 
11601c6fdbd8SKent Overstreet 	if (ca->kobj.state_in_sysfs)
11611c6fdbd8SKent Overstreet 		kobject_del(&ca->kobj);
11621c6fdbd8SKent Overstreet 
11631c6fdbd8SKent Overstreet 	bch2_free_super(&ca->disk_sb);
11641c6fdbd8SKent Overstreet 	bch2_dev_journal_exit(ca);
11651c6fdbd8SKent Overstreet 
11661c6fdbd8SKent Overstreet 	free_percpu(ca->io_done);
11671c6fdbd8SKent Overstreet 	bioset_exit(&ca->replica_set);
11681c6fdbd8SKent Overstreet 	bch2_dev_buckets_free(ca);
1169d1170ce5SKent Overstreet 	free_page((unsigned long) ca->sb_read_scratch);
11701c6fdbd8SKent Overstreet 
11711c6fdbd8SKent Overstreet 	bch2_time_stats_exit(&ca->io_latency[WRITE]);
11721c6fdbd8SKent Overstreet 	bch2_time_stats_exit(&ca->io_latency[READ]);
11731c6fdbd8SKent Overstreet 
11741c6fdbd8SKent Overstreet 	percpu_ref_exit(&ca->io_ref);
11751c6fdbd8SKent Overstreet 	percpu_ref_exit(&ca->ref);
11761c6fdbd8SKent Overstreet 	kobject_put(&ca->kobj);
11771c6fdbd8SKent Overstreet }
11781c6fdbd8SKent Overstreet 
11791c6fdbd8SKent Overstreet static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca)
11801c6fdbd8SKent Overstreet {
11811c6fdbd8SKent Overstreet 
11821c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
11831c6fdbd8SKent Overstreet 
11841c6fdbd8SKent Overstreet 	if (percpu_ref_is_zero(&ca->io_ref))
11851c6fdbd8SKent Overstreet 		return;
11861c6fdbd8SKent Overstreet 
11871c6fdbd8SKent Overstreet 	__bch2_dev_read_only(c, ca);
11881c6fdbd8SKent Overstreet 
11891c6fdbd8SKent Overstreet 	reinit_completion(&ca->io_ref_completion);
11901c6fdbd8SKent Overstreet 	percpu_ref_kill(&ca->io_ref);
11911c6fdbd8SKent Overstreet 	wait_for_completion(&ca->io_ref_completion);
11921c6fdbd8SKent Overstreet 
11931c6fdbd8SKent Overstreet 	if (ca->kobj.state_in_sysfs) {
11941c6fdbd8SKent Overstreet 		sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
11951c6fdbd8SKent Overstreet 		sysfs_remove_link(&ca->kobj, "block");
11961c6fdbd8SKent Overstreet 	}
11971c6fdbd8SKent Overstreet 
11981c6fdbd8SKent Overstreet 	bch2_free_super(&ca->disk_sb);
11991c6fdbd8SKent Overstreet 	bch2_dev_journal_exit(ca);
12001c6fdbd8SKent Overstreet }
12011c6fdbd8SKent Overstreet 
12021c6fdbd8SKent Overstreet static void bch2_dev_ref_complete(struct percpu_ref *ref)
12031c6fdbd8SKent Overstreet {
12041c6fdbd8SKent Overstreet 	struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
12051c6fdbd8SKent Overstreet 
12061c6fdbd8SKent Overstreet 	complete(&ca->ref_completion);
12071c6fdbd8SKent Overstreet }
12081c6fdbd8SKent Overstreet 
12091c6fdbd8SKent Overstreet static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
12101c6fdbd8SKent Overstreet {
12111c6fdbd8SKent Overstreet 	struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
12121c6fdbd8SKent Overstreet 
12131c6fdbd8SKent Overstreet 	complete(&ca->io_ref_completion);
12141c6fdbd8SKent Overstreet }
12151c6fdbd8SKent Overstreet 
12161c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca)
12171c6fdbd8SKent Overstreet {
12181c6fdbd8SKent Overstreet 	int ret;
12191c6fdbd8SKent Overstreet 
12201c6fdbd8SKent Overstreet 	if (!c->kobj.state_in_sysfs)
12211c6fdbd8SKent Overstreet 		return 0;
12221c6fdbd8SKent Overstreet 
12231c6fdbd8SKent Overstreet 	if (!ca->kobj.state_in_sysfs) {
12241c6fdbd8SKent Overstreet 		ret = kobject_add(&ca->kobj, &c->kobj,
12251c6fdbd8SKent Overstreet 				  "dev-%u", ca->dev_idx);
12261c6fdbd8SKent Overstreet 		if (ret)
12271c6fdbd8SKent Overstreet 			return ret;
12281c6fdbd8SKent Overstreet 	}
12291c6fdbd8SKent Overstreet 
12301c6fdbd8SKent Overstreet 	if (ca->disk_sb.bdev) {
12311c6fdbd8SKent Overstreet 		struct kobject *block = bdev_kobj(ca->disk_sb.bdev);
12321c6fdbd8SKent Overstreet 
12331c6fdbd8SKent Overstreet 		ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
12341c6fdbd8SKent Overstreet 		if (ret)
12351c6fdbd8SKent Overstreet 			return ret;
12361c6fdbd8SKent Overstreet 
12371c6fdbd8SKent Overstreet 		ret = sysfs_create_link(&ca->kobj, block, "block");
12381c6fdbd8SKent Overstreet 		if (ret)
12391c6fdbd8SKent Overstreet 			return ret;
12401c6fdbd8SKent Overstreet 	}
12411c6fdbd8SKent Overstreet 
12421c6fdbd8SKent Overstreet 	return 0;
12431c6fdbd8SKent Overstreet }
12441c6fdbd8SKent Overstreet 
12451c6fdbd8SKent Overstreet static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c,
12461c6fdbd8SKent Overstreet 					struct bch_member *member)
12471c6fdbd8SKent Overstreet {
12481c6fdbd8SKent Overstreet 	struct bch_dev *ca;
124994119eebSKent Overstreet 	unsigned i;
12501c6fdbd8SKent Overstreet 
12511c6fdbd8SKent Overstreet 	ca = kzalloc(sizeof(*ca), GFP_KERNEL);
12521c6fdbd8SKent Overstreet 	if (!ca)
12531c6fdbd8SKent Overstreet 		return NULL;
12541c6fdbd8SKent Overstreet 
12551c6fdbd8SKent Overstreet 	kobject_init(&ca->kobj, &bch2_dev_ktype);
12561c6fdbd8SKent Overstreet 	init_completion(&ca->ref_completion);
12571c6fdbd8SKent Overstreet 	init_completion(&ca->io_ref_completion);
12581c6fdbd8SKent Overstreet 
12591c6fdbd8SKent Overstreet 	init_rwsem(&ca->bucket_lock);
12601c6fdbd8SKent Overstreet 
12611c6fdbd8SKent Overstreet 	INIT_WORK(&ca->io_error_work, bch2_io_error_work);
12621c6fdbd8SKent Overstreet 
12631c6fdbd8SKent Overstreet 	bch2_time_stats_init(&ca->io_latency[READ]);
12641c6fdbd8SKent Overstreet 	bch2_time_stats_init(&ca->io_latency[WRITE]);
12651c6fdbd8SKent Overstreet 
12661c6fdbd8SKent Overstreet 	ca->mi = bch2_mi_to_cpu(member);
126794119eebSKent Overstreet 
126894119eebSKent Overstreet 	for (i = 0; i < ARRAY_SIZE(member->errors); i++)
126994119eebSKent Overstreet 		atomic64_set(&ca->errors[i], le64_to_cpu(member->errors[i]));
127094119eebSKent Overstreet 
12711c6fdbd8SKent Overstreet 	ca->uuid = member->uuid;
12721c6fdbd8SKent Overstreet 
1273f25d8215SKent Overstreet 	ca->nr_btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE,
1274f25d8215SKent Overstreet 			     ca->mi.bucket_size / btree_sectors(c));
1275f25d8215SKent Overstreet 
12761c6fdbd8SKent Overstreet 	if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
12771c6fdbd8SKent Overstreet 			    0, GFP_KERNEL) ||
12781c6fdbd8SKent Overstreet 	    percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
12791c6fdbd8SKent Overstreet 			    PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
1280d1170ce5SKent Overstreet 	    !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) ||
12811c6fdbd8SKent Overstreet 	    bch2_dev_buckets_alloc(c, ca) ||
12821c6fdbd8SKent Overstreet 	    bioset_init(&ca->replica_set, 4,
12831c6fdbd8SKent Overstreet 			offsetof(struct bch_write_bio, bio), 0) ||
12841c6fdbd8SKent Overstreet 	    !(ca->io_done	= alloc_percpu(*ca->io_done)))
12851c6fdbd8SKent Overstreet 		goto err;
12861c6fdbd8SKent Overstreet 
12871c6fdbd8SKent Overstreet 	return ca;
12881c6fdbd8SKent Overstreet err:
12891c6fdbd8SKent Overstreet 	bch2_dev_free(ca);
12901c6fdbd8SKent Overstreet 	return NULL;
12911c6fdbd8SKent Overstreet }
12921c6fdbd8SKent Overstreet 
12931c6fdbd8SKent Overstreet static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca,
12941c6fdbd8SKent Overstreet 			    unsigned dev_idx)
12951c6fdbd8SKent Overstreet {
12961c6fdbd8SKent Overstreet 	ca->dev_idx = dev_idx;
12971c6fdbd8SKent Overstreet 	__set_bit(ca->dev_idx, ca->self.d);
12981c6fdbd8SKent Overstreet 	scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
12991c6fdbd8SKent Overstreet 
13001c6fdbd8SKent Overstreet 	ca->fs = c;
13011c6fdbd8SKent Overstreet 	rcu_assign_pointer(c->devs[ca->dev_idx], ca);
13021c6fdbd8SKent Overstreet 
13031c6fdbd8SKent Overstreet 	if (bch2_dev_sysfs_online(c, ca))
13041c6fdbd8SKent Overstreet 		pr_warn("error creating sysfs objects");
13051c6fdbd8SKent Overstreet }
13061c6fdbd8SKent Overstreet 
13071c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
13081c6fdbd8SKent Overstreet {
13091241df58SHunter Shaffer 	struct bch_member member = bch2_sb_member_get(c->disk_sb.sb, dev_idx);
13101c6fdbd8SKent Overstreet 	struct bch_dev *ca = NULL;
13111c6fdbd8SKent Overstreet 	int ret = 0;
13121c6fdbd8SKent Overstreet 
13131c6fdbd8SKent Overstreet 	if (bch2_fs_init_fault("dev_alloc"))
13141c6fdbd8SKent Overstreet 		goto err;
13151c6fdbd8SKent Overstreet 
13161241df58SHunter Shaffer 	ca = __bch2_dev_alloc(c, &member);
13171c6fdbd8SKent Overstreet 	if (!ca)
13181c6fdbd8SKent Overstreet 		goto err;
13191c6fdbd8SKent Overstreet 
1320220d2062SKent Overstreet 	ca->fs = c;
1321220d2062SKent Overstreet 
13221c6fdbd8SKent Overstreet 	bch2_dev_attach(c, ca, dev_idx);
13231c6fdbd8SKent Overstreet 	return ret;
13241c6fdbd8SKent Overstreet err:
13251c6fdbd8SKent Overstreet 	if (ca)
13261c6fdbd8SKent Overstreet 		bch2_dev_free(ca);
1327c8b4534dSKent Overstreet 	return -BCH_ERR_ENOMEM_dev_alloc;
13281c6fdbd8SKent Overstreet }
13291c6fdbd8SKent Overstreet 
13301c6fdbd8SKent Overstreet static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
13311c6fdbd8SKent Overstreet {
13321c6fdbd8SKent Overstreet 	unsigned ret;
13331c6fdbd8SKent Overstreet 
13341c6fdbd8SKent Overstreet 	if (bch2_dev_is_online(ca)) {
13351c6fdbd8SKent Overstreet 		bch_err(ca, "already have device online in slot %u",
13361c6fdbd8SKent Overstreet 			sb->sb->dev_idx);
133778c0b75cSKent Overstreet 		return -BCH_ERR_device_already_online;
13381c6fdbd8SKent Overstreet 	}
13391c6fdbd8SKent Overstreet 
13401c6fdbd8SKent Overstreet 	if (get_capacity(sb->bdev->bd_disk) <
13411c6fdbd8SKent Overstreet 	    ca->mi.bucket_size * ca->mi.nbuckets) {
13421c6fdbd8SKent Overstreet 		bch_err(ca, "cannot online: device too small");
134378c0b75cSKent Overstreet 		return -BCH_ERR_device_size_too_small;
13441c6fdbd8SKent Overstreet 	}
13451c6fdbd8SKent Overstreet 
13461c6fdbd8SKent Overstreet 	BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
13471c6fdbd8SKent Overstreet 
13481c6fdbd8SKent Overstreet 	ret = bch2_dev_journal_init(ca, sb->sb);
13491c6fdbd8SKent Overstreet 	if (ret)
13501c6fdbd8SKent Overstreet 		return ret;
13511c6fdbd8SKent Overstreet 
13521c6fdbd8SKent Overstreet 	/* Commit: */
13531c6fdbd8SKent Overstreet 	ca->disk_sb = *sb;
13541c6fdbd8SKent Overstreet 	memset(sb, 0, sizeof(*sb));
13551c6fdbd8SKent Overstreet 
1356eacb2574SKent Overstreet 	ca->dev = ca->disk_sb.bdev->bd_dev;
1357eacb2574SKent Overstreet 
13581c6fdbd8SKent Overstreet 	percpu_ref_reinit(&ca->io_ref);
13591c6fdbd8SKent Overstreet 
13601c6fdbd8SKent Overstreet 	return 0;
13611c6fdbd8SKent Overstreet }
13621c6fdbd8SKent Overstreet 
13631c6fdbd8SKent Overstreet static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb)
13641c6fdbd8SKent Overstreet {
13651c6fdbd8SKent Overstreet 	struct bch_dev *ca;
13661c6fdbd8SKent Overstreet 	int ret;
13671c6fdbd8SKent Overstreet 
13681c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
13691c6fdbd8SKent Overstreet 
13701c6fdbd8SKent Overstreet 	if (le64_to_cpu(sb->sb->seq) >
13711c6fdbd8SKent Overstreet 	    le64_to_cpu(c->disk_sb.sb->seq))
13721c6fdbd8SKent Overstreet 		bch2_sb_to_fs(c, sb->sb);
13731c6fdbd8SKent Overstreet 
13741c6fdbd8SKent Overstreet 	BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
13751c6fdbd8SKent Overstreet 	       !c->devs[sb->sb->dev_idx]);
13761c6fdbd8SKent Overstreet 
13771c6fdbd8SKent Overstreet 	ca = bch_dev_locked(c, sb->sb->dev_idx);
13781c6fdbd8SKent Overstreet 
13791c6fdbd8SKent Overstreet 	ret = __bch2_dev_attach_bdev(ca, sb);
13801c6fdbd8SKent Overstreet 	if (ret)
13811c6fdbd8SKent Overstreet 		return ret;
13821c6fdbd8SKent Overstreet 
13831c6fdbd8SKent Overstreet 	bch2_dev_sysfs_online(c, ca);
13841c6fdbd8SKent Overstreet 
13851f5af5fcSKent Overstreet 	struct printbuf name = PRINTBUF;
13861f5af5fcSKent Overstreet 	prt_bdevname(&name, ca->disk_sb.bdev);
13871f5af5fcSKent Overstreet 
13881c6fdbd8SKent Overstreet 	if (c->sb.nr_devices == 1)
13891f5af5fcSKent Overstreet 		strlcpy(c->name, name.buf, sizeof(c->name));
13901f5af5fcSKent Overstreet 	strlcpy(ca->name, name.buf, sizeof(ca->name));
13911f5af5fcSKent Overstreet 
13921f5af5fcSKent Overstreet 	printbuf_exit(&name);
13931c6fdbd8SKent Overstreet 
13941c6fdbd8SKent Overstreet 	rebalance_wakeup(c);
13951c6fdbd8SKent Overstreet 	return 0;
13961c6fdbd8SKent Overstreet }
13971c6fdbd8SKent Overstreet 
13981c6fdbd8SKent Overstreet /* Device management: */
13991c6fdbd8SKent Overstreet 
14001c6fdbd8SKent Overstreet /*
14011c6fdbd8SKent Overstreet  * Note: this function is also used by the error paths - when a particular
14021c6fdbd8SKent Overstreet  * device sees an error, we call it to determine whether we can just set the
14031c6fdbd8SKent Overstreet  * device RO, or - if this function returns false - we'll set the whole
14041c6fdbd8SKent Overstreet  * filesystem RO:
14051c6fdbd8SKent Overstreet  *
14061c6fdbd8SKent Overstreet  * XXX: maybe we should be more explicit about whether we're changing state
14071c6fdbd8SKent Overstreet  * because we got an error or what have you?
14081c6fdbd8SKent Overstreet  */
14091c6fdbd8SKent Overstreet bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
14101c6fdbd8SKent Overstreet 			    enum bch_member_state new_state, int flags)
14111c6fdbd8SKent Overstreet {
14121c6fdbd8SKent Overstreet 	struct bch_devs_mask new_online_devs;
14139fea2274SKent Overstreet 	int nr_rw = 0, required;
14141c6fdbd8SKent Overstreet 
14151c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
14161c6fdbd8SKent Overstreet 
14171c6fdbd8SKent Overstreet 	switch (new_state) {
14182436cb9fSKent Overstreet 	case BCH_MEMBER_STATE_rw:
14191c6fdbd8SKent Overstreet 		return true;
14202436cb9fSKent Overstreet 	case BCH_MEMBER_STATE_ro:
14212436cb9fSKent Overstreet 		if (ca->mi.state != BCH_MEMBER_STATE_rw)
14221c6fdbd8SKent Overstreet 			return true;
14231c6fdbd8SKent Overstreet 
14241c6fdbd8SKent Overstreet 		/* do we have enough devices to write to?  */
14259fea2274SKent Overstreet 		for_each_member_device(c, ca2)
14261c6fdbd8SKent Overstreet 			if (ca2 != ca)
14272436cb9fSKent Overstreet 				nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw;
14281c6fdbd8SKent Overstreet 
14291c6fdbd8SKent Overstreet 		required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
14301c6fdbd8SKent Overstreet 			       ? c->opts.metadata_replicas
14311c6fdbd8SKent Overstreet 			       : c->opts.metadata_replicas_required,
14321c6fdbd8SKent Overstreet 			       !(flags & BCH_FORCE_IF_DATA_DEGRADED)
14331c6fdbd8SKent Overstreet 			       ? c->opts.data_replicas
14341c6fdbd8SKent Overstreet 			       : c->opts.data_replicas_required);
14351c6fdbd8SKent Overstreet 
14361c6fdbd8SKent Overstreet 		return nr_rw >= required;
14372436cb9fSKent Overstreet 	case BCH_MEMBER_STATE_failed:
14382436cb9fSKent Overstreet 	case BCH_MEMBER_STATE_spare:
14392436cb9fSKent Overstreet 		if (ca->mi.state != BCH_MEMBER_STATE_rw &&
14402436cb9fSKent Overstreet 		    ca->mi.state != BCH_MEMBER_STATE_ro)
14411c6fdbd8SKent Overstreet 			return true;
14421c6fdbd8SKent Overstreet 
14431c6fdbd8SKent Overstreet 		/* do we have enough devices to read from?  */
14441c6fdbd8SKent Overstreet 		new_online_devs = bch2_online_devs(c);
14451c6fdbd8SKent Overstreet 		__clear_bit(ca->dev_idx, new_online_devs.d);
14461c6fdbd8SKent Overstreet 
1447fcb3431bSKent Overstreet 		return bch2_have_enough_devs(c, new_online_devs, flags, false);
14481c6fdbd8SKent Overstreet 	default:
14491c6fdbd8SKent Overstreet 		BUG();
14501c6fdbd8SKent Overstreet 	}
14511c6fdbd8SKent Overstreet }
14521c6fdbd8SKent Overstreet 
14531c6fdbd8SKent Overstreet static bool bch2_fs_may_start(struct bch_fs *c)
14541c6fdbd8SKent Overstreet {
14551c6fdbd8SKent Overstreet 	struct bch_dev *ca;
1456fcb3431bSKent Overstreet 	unsigned i, flags = 0;
14571c6fdbd8SKent Overstreet 
1458fcb3431bSKent Overstreet 	if (c->opts.very_degraded)
1459fcb3431bSKent Overstreet 		flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST;
1460fcb3431bSKent Overstreet 
1461fcb3431bSKent Overstreet 	if (c->opts.degraded)
1462fcb3431bSKent Overstreet 		flags |= BCH_FORCE_IF_DEGRADED;
1463fcb3431bSKent Overstreet 
1464fcb3431bSKent Overstreet 	if (!c->opts.degraded &&
1465fcb3431bSKent Overstreet 	    !c->opts.very_degraded) {
14661c6fdbd8SKent Overstreet 		mutex_lock(&c->sb_lock);
14671c6fdbd8SKent Overstreet 
14681c6fdbd8SKent Overstreet 		for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
14691241df58SHunter Shaffer 			if (!bch2_dev_exists(c->disk_sb.sb, i))
14701c6fdbd8SKent Overstreet 				continue;
14711c6fdbd8SKent Overstreet 
14721c6fdbd8SKent Overstreet 			ca = bch_dev_locked(c, i);
14731c6fdbd8SKent Overstreet 
14741c6fdbd8SKent Overstreet 			if (!bch2_dev_is_online(ca) &&
14752436cb9fSKent Overstreet 			    (ca->mi.state == BCH_MEMBER_STATE_rw ||
14762436cb9fSKent Overstreet 			     ca->mi.state == BCH_MEMBER_STATE_ro)) {
14771c6fdbd8SKent Overstreet 				mutex_unlock(&c->sb_lock);
14781c6fdbd8SKent Overstreet 				return false;
14791c6fdbd8SKent Overstreet 			}
14801c6fdbd8SKent Overstreet 		}
14811c6fdbd8SKent Overstreet 		mutex_unlock(&c->sb_lock);
14821c6fdbd8SKent Overstreet 	}
14831c6fdbd8SKent Overstreet 
1484fcb3431bSKent Overstreet 	return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true);
14851c6fdbd8SKent Overstreet }
14861c6fdbd8SKent Overstreet 
14871c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
14881c6fdbd8SKent Overstreet {
14891c6fdbd8SKent Overstreet 	/*
14901c6fdbd8SKent Overstreet 	 * The allocator thread itself allocates btree nodes, so stop it first:
14911c6fdbd8SKent Overstreet 	 */
14921c6fdbd8SKent Overstreet 	bch2_dev_allocator_remove(c, ca);
14931c6fdbd8SKent Overstreet 	bch2_dev_journal_stop(&c->journal, ca);
14941c6fdbd8SKent Overstreet }
14951c6fdbd8SKent Overstreet 
1496f25d8215SKent Overstreet static void __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
14971c6fdbd8SKent Overstreet {
14981c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
14991c6fdbd8SKent Overstreet 
15002436cb9fSKent Overstreet 	BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw);
15011c6fdbd8SKent Overstreet 
15021c6fdbd8SKent Overstreet 	bch2_dev_allocator_add(c, ca);
15031c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
15041c6fdbd8SKent Overstreet }
15051c6fdbd8SKent Overstreet 
15061c6fdbd8SKent Overstreet int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
15071c6fdbd8SKent Overstreet 			 enum bch_member_state new_state, int flags)
15081c6fdbd8SKent Overstreet {
15093f7b9713SHunter Shaffer 	struct bch_member *m;
15101c6fdbd8SKent Overstreet 	int ret = 0;
15111c6fdbd8SKent Overstreet 
15121c6fdbd8SKent Overstreet 	if (ca->mi.state == new_state)
15131c6fdbd8SKent Overstreet 		return 0;
15141c6fdbd8SKent Overstreet 
15151c6fdbd8SKent Overstreet 	if (!bch2_dev_state_allowed(c, ca, new_state, flags))
151678c0b75cSKent Overstreet 		return -BCH_ERR_device_state_not_allowed;
15171c6fdbd8SKent Overstreet 
15182436cb9fSKent Overstreet 	if (new_state != BCH_MEMBER_STATE_rw)
15191c6fdbd8SKent Overstreet 		__bch2_dev_read_only(c, ca);
15201c6fdbd8SKent Overstreet 
15212436cb9fSKent Overstreet 	bch_notice(ca, "%s", bch2_member_states[new_state]);
15221c6fdbd8SKent Overstreet 
15231c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
15243f7b9713SHunter Shaffer 	m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
15253f7b9713SHunter Shaffer 	SET_BCH_MEMBER_STATE(m, new_state);
15261c6fdbd8SKent Overstreet 	bch2_write_super(c);
15271c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
15281c6fdbd8SKent Overstreet 
1529e2b60560SKent Overstreet 	if (new_state == BCH_MEMBER_STATE_rw)
1530f25d8215SKent Overstreet 		__bch2_dev_read_write(c, ca);
15311c6fdbd8SKent Overstreet 
15321c6fdbd8SKent Overstreet 	rebalance_wakeup(c);
15331c6fdbd8SKent Overstreet 
15341c6fdbd8SKent Overstreet 	return ret;
15351c6fdbd8SKent Overstreet }
15361c6fdbd8SKent Overstreet 
15371c6fdbd8SKent Overstreet int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
15381c6fdbd8SKent Overstreet 		       enum bch_member_state new_state, int flags)
15391c6fdbd8SKent Overstreet {
15401c6fdbd8SKent Overstreet 	int ret;
15411c6fdbd8SKent Overstreet 
15421ada1606SKent Overstreet 	down_write(&c->state_lock);
15431c6fdbd8SKent Overstreet 	ret = __bch2_dev_set_state(c, ca, new_state, flags);
15441ada1606SKent Overstreet 	up_write(&c->state_lock);
15451c6fdbd8SKent Overstreet 
15461c6fdbd8SKent Overstreet 	return ret;
15471c6fdbd8SKent Overstreet }
15481c6fdbd8SKent Overstreet 
15491c6fdbd8SKent Overstreet /* Device add/removal: */
15501c6fdbd8SKent Overstreet 
1551c0ebe3e4SKent Overstreet static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca)
15525d20ba48SKent Overstreet {
1553c6b2826cSKent Overstreet 	struct bpos start	= POS(ca->dev_idx, 0);
1554c6b2826cSKent Overstreet 	struct bpos end		= POS(ca->dev_idx, U64_MAX);
15555d20ba48SKent Overstreet 	int ret;
15565d20ba48SKent Overstreet 
1557a9c0a4cbSKent Overstreet 	/*
1558a9c0a4cbSKent Overstreet 	 * We clear the LRU and need_discard btrees first so that we don't race
1559a9c0a4cbSKent Overstreet 	 * with bch2_do_invalidates() and bch2_do_discards()
1560a9c0a4cbSKent Overstreet 	 */
1561a9c0a4cbSKent Overstreet 	ret =   bch2_btree_delete_range(c, BTREE_ID_lru, start, end,
1562a9c0a4cbSKent Overstreet 					BTREE_TRIGGER_NORUN, NULL) ?:
1563a9c0a4cbSKent Overstreet 		bch2_btree_delete_range(c, BTREE_ID_need_discard, start, end,
1564c6b2826cSKent Overstreet 					BTREE_TRIGGER_NORUN, NULL) ?:
1565c6b2826cSKent Overstreet 		bch2_btree_delete_range(c, BTREE_ID_freespace, start, end,
1566c6b2826cSKent Overstreet 					BTREE_TRIGGER_NORUN, NULL) ?:
1567a8c752bbSKent Overstreet 		bch2_btree_delete_range(c, BTREE_ID_backpointers, start, end,
1568a8c752bbSKent Overstreet 					BTREE_TRIGGER_NORUN, NULL) ?:
1569a9c0a4cbSKent Overstreet 		bch2_btree_delete_range(c, BTREE_ID_alloc, start, end,
157002d51bb9SBrian Foster 					BTREE_TRIGGER_NORUN, NULL) ?:
157102d51bb9SBrian Foster 		bch2_btree_delete_range(c, BTREE_ID_bucket_gens, start, end,
1572c6b2826cSKent Overstreet 					BTREE_TRIGGER_NORUN, NULL);
1573e46c181aSKent Overstreet 	bch_err_msg(c, ret, "removing dev alloc info");
1574c6b2826cSKent Overstreet 	return ret;
15755d20ba48SKent Overstreet }
15765d20ba48SKent Overstreet 
15771c6fdbd8SKent Overstreet int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
15781c6fdbd8SKent Overstreet {
15793f7b9713SHunter Shaffer 	struct bch_member *m;
15801c6fdbd8SKent Overstreet 	unsigned dev_idx = ca->dev_idx, data;
158178c0b75cSKent Overstreet 	int ret;
15821c6fdbd8SKent Overstreet 
15831ada1606SKent Overstreet 	down_write(&c->state_lock);
15841c6fdbd8SKent Overstreet 
158531ba2cd3SKent Overstreet 	/*
158631ba2cd3SKent Overstreet 	 * We consume a reference to ca->ref, regardless of whether we succeed
158731ba2cd3SKent Overstreet 	 * or fail:
158831ba2cd3SKent Overstreet 	 */
158931ba2cd3SKent Overstreet 	percpu_ref_put(&ca->ref);
15901c6fdbd8SKent Overstreet 
15912436cb9fSKent Overstreet 	if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
15921c6fdbd8SKent Overstreet 		bch_err(ca, "Cannot remove without losing data");
159378c0b75cSKent Overstreet 		ret = -BCH_ERR_device_state_not_allowed;
15941c6fdbd8SKent Overstreet 		goto err;
15951c6fdbd8SKent Overstreet 	}
15961c6fdbd8SKent Overstreet 
15971c6fdbd8SKent Overstreet 	__bch2_dev_read_only(c, ca);
15981c6fdbd8SKent Overstreet 
15991c6fdbd8SKent Overstreet 	ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
1600e46c181aSKent Overstreet 	bch_err_msg(ca, ret, "dropping data");
1601cf904c8dSKent Overstreet 	if (ret)
16021c6fdbd8SKent Overstreet 		goto err;
16031c6fdbd8SKent Overstreet 
16045d20ba48SKent Overstreet 	ret = bch2_dev_remove_alloc(c, ca);
1605e46c181aSKent Overstreet 	bch_err_msg(ca, ret, "deleting alloc info");
1606cf904c8dSKent Overstreet 	if (ret)
16071c6fdbd8SKent Overstreet 		goto err;
16081c6fdbd8SKent Overstreet 
1609a9c0a4cbSKent Overstreet 	ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx);
1610e46c181aSKent Overstreet 	bch_err_msg(ca, ret, "flushing journal");
1611cf904c8dSKent Overstreet 	if (ret)
1612a9c0a4cbSKent Overstreet 		goto err;
1613a9c0a4cbSKent Overstreet 
1614a9c0a4cbSKent Overstreet 	ret = bch2_journal_flush(&c->journal);
1615e46c181aSKent Overstreet 	bch_err(ca, "journal error");
1616cf904c8dSKent Overstreet 	if (ret)
16171c6fdbd8SKent Overstreet 		goto err;
16181c6fdbd8SKent Overstreet 
161931ba2cd3SKent Overstreet 	ret = bch2_replicas_gc2(c);
1620e46c181aSKent Overstreet 	bch_err_msg(ca, ret, "in replicas_gc2()");
1621cf904c8dSKent Overstreet 	if (ret)
162231ba2cd3SKent Overstreet 		goto err;
162331ba2cd3SKent Overstreet 
162431ba2cd3SKent Overstreet 	data = bch2_dev_has_data(c, ca);
162531ba2cd3SKent Overstreet 	if (data) {
1626fa8e94faSKent Overstreet 		struct printbuf data_has = PRINTBUF;
162731ba2cd3SKent Overstreet 
1628e58f963cSKent Overstreet 		prt_bitflags(&data_has, __bch2_data_types, data);
1629fa8e94faSKent Overstreet 		bch_err(ca, "Remove failed, still has data (%s)", data_has.buf);
1630fa8e94faSKent Overstreet 		printbuf_exit(&data_has);
163131ba2cd3SKent Overstreet 		ret = -EBUSY;
163231ba2cd3SKent Overstreet 		goto err;
163331ba2cd3SKent Overstreet 	}
163431ba2cd3SKent Overstreet 
16351c6fdbd8SKent Overstreet 	__bch2_dev_offline(c, ca);
16361c6fdbd8SKent Overstreet 
16371c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
16381c6fdbd8SKent Overstreet 	rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
16391c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
16401c6fdbd8SKent Overstreet 
16411c6fdbd8SKent Overstreet 	percpu_ref_kill(&ca->ref);
16421c6fdbd8SKent Overstreet 	wait_for_completion(&ca->ref_completion);
16431c6fdbd8SKent Overstreet 
16441c6fdbd8SKent Overstreet 	bch2_dev_free(ca);
16451c6fdbd8SKent Overstreet 
16461c6fdbd8SKent Overstreet 	/*
1647bc652905SBrian Foster 	 * At this point the device object has been removed in-core, but the
1648bc652905SBrian Foster 	 * on-disk journal might still refer to the device index via sb device
1649bc652905SBrian Foster 	 * usage entries. Recovery fails if it sees usage information for an
1650bc652905SBrian Foster 	 * invalid device. Flush journal pins to push the back of the journal
1651bc652905SBrian Foster 	 * past now invalid device index references before we update the
1652bc652905SBrian Foster 	 * superblock, but after the device object has been removed so any
1653bc652905SBrian Foster 	 * further journal writes elide usage info for the device.
1654bc652905SBrian Foster 	 */
1655bc652905SBrian Foster 	bch2_journal_flush_all_pins(&c->journal);
1656bc652905SBrian Foster 
1657bc652905SBrian Foster 	/*
16581c6fdbd8SKent Overstreet 	 * Free this device's slot in the bch_member array - all pointers to
16591c6fdbd8SKent Overstreet 	 * this device must be gone:
16601c6fdbd8SKent Overstreet 	 */
16611c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
16623f7b9713SHunter Shaffer 	m = bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx);
16633f7b9713SHunter Shaffer 	memset(&m->uuid, 0, sizeof(m->uuid));
16641c6fdbd8SKent Overstreet 
16651c6fdbd8SKent Overstreet 	bch2_write_super(c);
16661c6fdbd8SKent Overstreet 
16671c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
16681ada1606SKent Overstreet 	up_write(&c->state_lock);
1669180fb49dSKent Overstreet 
1670180fb49dSKent Overstreet 	bch2_dev_usage_journal_reserve(c);
16711c6fdbd8SKent Overstreet 	return 0;
16721c6fdbd8SKent Overstreet err:
16732436cb9fSKent Overstreet 	if (ca->mi.state == BCH_MEMBER_STATE_rw &&
1674d3bb629dSKent Overstreet 	    !percpu_ref_is_zero(&ca->io_ref))
16751c6fdbd8SKent Overstreet 		__bch2_dev_read_write(c, ca);
16761ada1606SKent Overstreet 	up_write(&c->state_lock);
16771c6fdbd8SKent Overstreet 	return ret;
16781c6fdbd8SKent Overstreet }
16791c6fdbd8SKent Overstreet 
16801c6fdbd8SKent Overstreet /* Add new device to running filesystem: */
16811c6fdbd8SKent Overstreet int bch2_dev_add(struct bch_fs *c, const char *path)
16821c6fdbd8SKent Overstreet {
16831c6fdbd8SKent Overstreet 	struct bch_opts opts = bch2_opts_empty();
16841c6fdbd8SKent Overstreet 	struct bch_sb_handle sb;
16851c6fdbd8SKent Overstreet 	struct bch_dev *ca = NULL;
16863f7b9713SHunter Shaffer 	struct bch_sb_field_members_v2 *mi;
16871c6fdbd8SKent Overstreet 	struct bch_member dev_mi;
16881c6fdbd8SKent Overstreet 	unsigned dev_idx, nr_devices, u64s;
1689fa8e94faSKent Overstreet 	struct printbuf errbuf = PRINTBUF;
169002afcb8cSKent Overstreet 	struct printbuf label = PRINTBUF;
16911c6fdbd8SKent Overstreet 	int ret;
16921c6fdbd8SKent Overstreet 
16931c6fdbd8SKent Overstreet 	ret = bch2_read_super(path, &opts, &sb);
1694e46c181aSKent Overstreet 	bch_err_msg(c, ret, "reading super");
1695cf904c8dSKent Overstreet 	if (ret)
1696efe68e1dSKent Overstreet 		goto err;
16971c6fdbd8SKent Overstreet 
16981241df58SHunter Shaffer 	dev_mi = bch2_sb_member_get(sb.sb, sb.sb->dev_idx);
16991c6fdbd8SKent Overstreet 
170002afcb8cSKent Overstreet 	if (BCH_MEMBER_GROUP(&dev_mi)) {
1701e677179bSKent Overstreet 		bch2_disk_path_to_text_sb(&label, sb.sb, BCH_MEMBER_GROUP(&dev_mi) - 1);
170202afcb8cSKent Overstreet 		if (label.allocation_failure) {
170302afcb8cSKent Overstreet 			ret = -ENOMEM;
170402afcb8cSKent Overstreet 			goto err;
170502afcb8cSKent Overstreet 		}
170602afcb8cSKent Overstreet 	}
170702afcb8cSKent Overstreet 
170878c0b75cSKent Overstreet 	ret = bch2_dev_may_add(sb.sb, c);
1709cf904c8dSKent Overstreet 	if (ret)
1710efe68e1dSKent Overstreet 		goto err;
17111c6fdbd8SKent Overstreet 
17121c6fdbd8SKent Overstreet 	ca = __bch2_dev_alloc(c, &dev_mi);
17131c6fdbd8SKent Overstreet 	if (!ca) {
1714efe68e1dSKent Overstreet 		ret = -ENOMEM;
1715efe68e1dSKent Overstreet 		goto err;
17161c6fdbd8SKent Overstreet 	}
17171c6fdbd8SKent Overstreet 
1718822835ffSKent Overstreet 	bch2_dev_usage_init(ca);
1719822835ffSKent Overstreet 
17201c6fdbd8SKent Overstreet 	ret = __bch2_dev_attach_bdev(ca, &sb);
172171933fb6SChristophe JAILLET 	if (ret)
1722efe68e1dSKent Overstreet 		goto err;
17231c6fdbd8SKent Overstreet 
17241c6fdbd8SKent Overstreet 	ret = bch2_dev_journal_alloc(ca);
1725e46c181aSKent Overstreet 	bch_err_msg(c, ret, "allocating journal");
1726cf904c8dSKent Overstreet 	if (ret)
17271c6fdbd8SKent Overstreet 		goto err;
17281c6fdbd8SKent Overstreet 
17291ada1606SKent Overstreet 	down_write(&c->state_lock);
17301c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
17311c6fdbd8SKent Overstreet 
17321c6fdbd8SKent Overstreet 	ret = bch2_sb_from_fs(c, ca);
1733e46c181aSKent Overstreet 	bch_err_msg(c, ret, "setting up new superblock");
1734cf904c8dSKent Overstreet 	if (ret)
17351c6fdbd8SKent Overstreet 		goto err_unlock;
17361c6fdbd8SKent Overstreet 
17371c6fdbd8SKent Overstreet 	if (dynamic_fault("bcachefs:add:no_slot"))
17381c6fdbd8SKent Overstreet 		goto no_slot;
17391c6fdbd8SKent Overstreet 
17401c6fdbd8SKent Overstreet 	for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
17411241df58SHunter Shaffer 		if (!bch2_dev_exists(c->disk_sb.sb, dev_idx))
17421c6fdbd8SKent Overstreet 			goto have_slot;
17431c6fdbd8SKent Overstreet no_slot:
1744098ef98dSKent Overstreet 	ret = -BCH_ERR_ENOSPC_sb_members;
1745e46c181aSKent Overstreet 	bch_err_msg(c, ret, "setting up new superblock");
17461c6fdbd8SKent Overstreet 	goto err_unlock;
17471c6fdbd8SKent Overstreet 
17481c6fdbd8SKent Overstreet have_slot:
17491c6fdbd8SKent Overstreet 	nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
1750e8484348SKent Overstreet 
1751e8484348SKent Overstreet 	mi = bch2_sb_field_get(c->disk_sb.sb, members_v2);
17523f7b9713SHunter Shaffer 	u64s = DIV_ROUND_UP(sizeof(struct bch_sb_field_members_v2) +
17533f7b9713SHunter Shaffer 			    le16_to_cpu(mi->member_bytes) * nr_devices, sizeof(u64));
17541c6fdbd8SKent Overstreet 
17554637429eSKent Overstreet 	mi = bch2_sb_field_resize(&c->disk_sb, members_v2, u64s);
1756e8536925SKent Overstreet 	if (!mi) {
1757098ef98dSKent Overstreet 		ret = -BCH_ERR_ENOSPC_sb_members;
1758e46c181aSKent Overstreet 		bch_err_msg(c, ret, "setting up new superblock");
17591c6fdbd8SKent Overstreet 		goto err_unlock;
1760e8536925SKent Overstreet 	}
17613f7b9713SHunter Shaffer 	struct bch_member *m = bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx);
17621c6fdbd8SKent Overstreet 
17631c6fdbd8SKent Overstreet 	/* success: */
17641c6fdbd8SKent Overstreet 
17653f7b9713SHunter Shaffer 	*m = dev_mi;
17663f7b9713SHunter Shaffer 	m->last_mount = cpu_to_le64(ktime_get_real_seconds());
17671c6fdbd8SKent Overstreet 	c->disk_sb.sb->nr_devices	= nr_devices;
17681c6fdbd8SKent Overstreet 
17691c6fdbd8SKent Overstreet 	ca->disk_sb.sb->dev_idx	= dev_idx;
17701c6fdbd8SKent Overstreet 	bch2_dev_attach(c, ca, dev_idx);
17711c6fdbd8SKent Overstreet 
177202afcb8cSKent Overstreet 	if (BCH_MEMBER_GROUP(&dev_mi)) {
177302afcb8cSKent Overstreet 		ret = __bch2_dev_group_set(c, ca, label.buf);
1774e46c181aSKent Overstreet 		bch_err_msg(c, ret, "creating new label");
1775cf904c8dSKent Overstreet 		if (ret)
177602afcb8cSKent Overstreet 			goto err_unlock;
177702afcb8cSKent Overstreet 	}
177802afcb8cSKent Overstreet 
17791c6fdbd8SKent Overstreet 	bch2_write_super(c);
17801c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
17811c6fdbd8SKent Overstreet 
1782180fb49dSKent Overstreet 	bch2_dev_usage_journal_reserve(c);
1783180fb49dSKent Overstreet 
1784d62ab355SKent Overstreet 	ret = bch2_trans_mark_dev_sb(c, ca);
1785bbe682c7SKent Overstreet 	bch_err_msg(ca, ret, "marking new superblock");
1786cf904c8dSKent Overstreet 	if (ret)
1787bfcf840dSKent Overstreet 		goto err_late;
17888d6b6222SKent Overstreet 
1789c6b2826cSKent Overstreet 	ret = bch2_fs_freespace_init(c);
1790bbe682c7SKent Overstreet 	bch_err_msg(ca, ret, "initializing free space");
1791cf904c8dSKent Overstreet 	if (ret)
1792c6b2826cSKent Overstreet 		goto err_late;
1793c6b2826cSKent Overstreet 
179409943313SKent Overstreet 	ca->new_fs_bucket_idx = 0;
179509943313SKent Overstreet 
1796f25d8215SKent Overstreet 	if (ca->mi.state == BCH_MEMBER_STATE_rw)
1797f25d8215SKent Overstreet 		__bch2_dev_read_write(c, ca);
17981c6fdbd8SKent Overstreet 
17991ada1606SKent Overstreet 	up_write(&c->state_lock);
18001c6fdbd8SKent Overstreet 	return 0;
18011c6fdbd8SKent Overstreet 
18021c6fdbd8SKent Overstreet err_unlock:
18031c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
18041ada1606SKent Overstreet 	up_write(&c->state_lock);
18051c6fdbd8SKent Overstreet err:
18061c6fdbd8SKent Overstreet 	if (ca)
18071c6fdbd8SKent Overstreet 		bch2_dev_free(ca);
18081c6fdbd8SKent Overstreet 	bch2_free_super(&sb);
180902afcb8cSKent Overstreet 	printbuf_exit(&label);
1810fa8e94faSKent Overstreet 	printbuf_exit(&errbuf);
1811cf904c8dSKent Overstreet 	bch_err_fn(c, ret);
18121c6fdbd8SKent Overstreet 	return ret;
18131c6fdbd8SKent Overstreet err_late:
1814bfcf840dSKent Overstreet 	up_write(&c->state_lock);
1815efe68e1dSKent Overstreet 	ca = NULL;
1816efe68e1dSKent Overstreet 	goto err;
18171c6fdbd8SKent Overstreet }
18181c6fdbd8SKent Overstreet 
18191c6fdbd8SKent Overstreet /* Hot add existing device to running filesystem: */
18201c6fdbd8SKent Overstreet int bch2_dev_online(struct bch_fs *c, const char *path)
18211c6fdbd8SKent Overstreet {
18221c6fdbd8SKent Overstreet 	struct bch_opts opts = bch2_opts_empty();
18231c6fdbd8SKent Overstreet 	struct bch_sb_handle sb = { NULL };
18241c6fdbd8SKent Overstreet 	struct bch_dev *ca;
18251c6fdbd8SKent Overstreet 	unsigned dev_idx;
18261c6fdbd8SKent Overstreet 	int ret;
18271c6fdbd8SKent Overstreet 
18281ada1606SKent Overstreet 	down_write(&c->state_lock);
18291c6fdbd8SKent Overstreet 
18301c6fdbd8SKent Overstreet 	ret = bch2_read_super(path, &opts, &sb);
18311c6fdbd8SKent Overstreet 	if (ret) {
18321ada1606SKent Overstreet 		up_write(&c->state_lock);
18331c6fdbd8SKent Overstreet 		return ret;
18341c6fdbd8SKent Overstreet 	}
18351c6fdbd8SKent Overstreet 
18361c6fdbd8SKent Overstreet 	dev_idx = sb.sb->dev_idx;
18371c6fdbd8SKent Overstreet 
18380d529663SKent Overstreet 	ret = bch2_dev_in_fs(&c->disk_sb, &sb);
1839e46c181aSKent Overstreet 	bch_err_msg(c, ret, "bringing %s online", path);
1840cf904c8dSKent Overstreet 	if (ret)
18411c6fdbd8SKent Overstreet 		goto err;
18421c6fdbd8SKent Overstreet 
1843e2b60560SKent Overstreet 	ret = bch2_dev_attach_bdev(c, &sb);
1844e2b60560SKent Overstreet 	if (ret)
1845e2b60560SKent Overstreet 		goto err;
1846e2b60560SKent Overstreet 
18471c6fdbd8SKent Overstreet 	ca = bch_dev_locked(c, dev_idx);
1848bfcf840dSKent Overstreet 
1849e2b60560SKent Overstreet 	ret = bch2_trans_mark_dev_sb(c, ca);
1850e46c181aSKent Overstreet 	bch_err_msg(c, ret, "bringing %s online: error from bch2_trans_mark_dev_sb", path);
1851cf904c8dSKent Overstreet 	if (ret)
1852bfcf840dSKent Overstreet 		goto err;
1853bfcf840dSKent Overstreet 
1854f25d8215SKent Overstreet 	if (ca->mi.state == BCH_MEMBER_STATE_rw)
1855f25d8215SKent Overstreet 		__bch2_dev_read_write(c, ca);
18561c6fdbd8SKent Overstreet 
1857bbe682c7SKent Overstreet 	if (!ca->mi.freespace_initialized) {
1858bbe682c7SKent Overstreet 		ret = bch2_dev_freespace_init(c, ca, 0, ca->mi.nbuckets);
1859bbe682c7SKent Overstreet 		bch_err_msg(ca, ret, "initializing free space");
1860bbe682c7SKent Overstreet 		if (ret)
1861bbe682c7SKent Overstreet 			goto err;
1862bbe682c7SKent Overstreet 	}
1863bbe682c7SKent Overstreet 
1864bbe682c7SKent Overstreet 	if (!ca->journal.nr) {
1865bbe682c7SKent Overstreet 		ret = bch2_dev_journal_alloc(ca);
1866bbe682c7SKent Overstreet 		bch_err_msg(ca, ret, "allocating journal");
1867bbe682c7SKent Overstreet 		if (ret)
1868bbe682c7SKent Overstreet 			goto err;
1869bbe682c7SKent Overstreet 	}
1870bbe682c7SKent Overstreet 
18711c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
1872bbe682c7SKent Overstreet 	bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount =
1873a420eea6STim Schlueter 		cpu_to_le64(ktime_get_real_seconds());
18741c6fdbd8SKent Overstreet 	bch2_write_super(c);
18751c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
18761c6fdbd8SKent Overstreet 
18771ada1606SKent Overstreet 	up_write(&c->state_lock);
18781c6fdbd8SKent Overstreet 	return 0;
18791c6fdbd8SKent Overstreet err:
18801ada1606SKent Overstreet 	up_write(&c->state_lock);
18811c6fdbd8SKent Overstreet 	bch2_free_super(&sb);
188278c0b75cSKent Overstreet 	return ret;
18831c6fdbd8SKent Overstreet }
18841c6fdbd8SKent Overstreet 
18851c6fdbd8SKent Overstreet int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
18861c6fdbd8SKent Overstreet {
18871ada1606SKent Overstreet 	down_write(&c->state_lock);
18881c6fdbd8SKent Overstreet 
18891c6fdbd8SKent Overstreet 	if (!bch2_dev_is_online(ca)) {
18901c6fdbd8SKent Overstreet 		bch_err(ca, "Already offline");
18911ada1606SKent Overstreet 		up_write(&c->state_lock);
18921c6fdbd8SKent Overstreet 		return 0;
18931c6fdbd8SKent Overstreet 	}
18941c6fdbd8SKent Overstreet 
18952436cb9fSKent Overstreet 	if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
18961c6fdbd8SKent Overstreet 		bch_err(ca, "Cannot offline required disk");
18971ada1606SKent Overstreet 		up_write(&c->state_lock);
189878c0b75cSKent Overstreet 		return -BCH_ERR_device_state_not_allowed;
18991c6fdbd8SKent Overstreet 	}
19001c6fdbd8SKent Overstreet 
19011c6fdbd8SKent Overstreet 	__bch2_dev_offline(c, ca);
19021c6fdbd8SKent Overstreet 
19031ada1606SKent Overstreet 	up_write(&c->state_lock);
19041c6fdbd8SKent Overstreet 	return 0;
19051c6fdbd8SKent Overstreet }
19061c6fdbd8SKent Overstreet 
19071c6fdbd8SKent Overstreet int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
19081c6fdbd8SKent Overstreet {
19093f7b9713SHunter Shaffer 	struct bch_member *m;
191069d1f052SKent Overstreet 	u64 old_nbuckets;
19111c6fdbd8SKent Overstreet 	int ret = 0;
19121c6fdbd8SKent Overstreet 
19131ada1606SKent Overstreet 	down_write(&c->state_lock);
191469d1f052SKent Overstreet 	old_nbuckets = ca->mi.nbuckets;
19151c6fdbd8SKent Overstreet 
19161c6fdbd8SKent Overstreet 	if (nbuckets < ca->mi.nbuckets) {
19171c6fdbd8SKent Overstreet 		bch_err(ca, "Cannot shrink yet");
19181c6fdbd8SKent Overstreet 		ret = -EINVAL;
19191c6fdbd8SKent Overstreet 		goto err;
19201c6fdbd8SKent Overstreet 	}
19211c6fdbd8SKent Overstreet 
19221c6fdbd8SKent Overstreet 	if (bch2_dev_is_online(ca) &&
19231c6fdbd8SKent Overstreet 	    get_capacity(ca->disk_sb.bdev->bd_disk) <
19241c6fdbd8SKent Overstreet 	    ca->mi.bucket_size * nbuckets) {
19251c6fdbd8SKent Overstreet 		bch_err(ca, "New size larger than device");
192678c0b75cSKent Overstreet 		ret = -BCH_ERR_device_size_too_small;
19271c6fdbd8SKent Overstreet 		goto err;
19281c6fdbd8SKent Overstreet 	}
19291c6fdbd8SKent Overstreet 
19301c6fdbd8SKent Overstreet 	ret = bch2_dev_buckets_resize(c, ca, nbuckets);
1931e46c181aSKent Overstreet 	bch_err_msg(ca, ret, "resizing buckets");
1932cf904c8dSKent Overstreet 	if (ret)
19331c6fdbd8SKent Overstreet 		goto err;
19341c6fdbd8SKent Overstreet 
1935224ec3e6SKent Overstreet 	ret = bch2_trans_mark_dev_sb(c, ca);
19363e3e02e6SKent Overstreet 	if (ret)
1937224ec3e6SKent Overstreet 		goto err;
1938224ec3e6SKent Overstreet 
19391c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
19403f7b9713SHunter Shaffer 	m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
19413f7b9713SHunter Shaffer 	m->nbuckets = cpu_to_le64(nbuckets);
19421c6fdbd8SKent Overstreet 
19431c6fdbd8SKent Overstreet 	bch2_write_super(c);
19441c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
19451c6fdbd8SKent Overstreet 
194669d1f052SKent Overstreet 	if (ca->mi.freespace_initialized) {
194769d1f052SKent Overstreet 		ret = bch2_dev_freespace_init(c, ca, old_nbuckets, nbuckets);
194869d1f052SKent Overstreet 		if (ret)
194969d1f052SKent Overstreet 			goto err;
195069d1f052SKent Overstreet 
195169d1f052SKent Overstreet 		/*
195269d1f052SKent Overstreet 		 * XXX: this is all wrong transactionally - we'll be able to do
195369d1f052SKent Overstreet 		 * this correctly after the disk space accounting rewrite
195469d1f052SKent Overstreet 		 */
195569d1f052SKent Overstreet 		ca->usage_base->d[BCH_DATA_free].buckets += nbuckets - old_nbuckets;
195669d1f052SKent Overstreet 	}
195769d1f052SKent Overstreet 
19581c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
19591c6fdbd8SKent Overstreet err:
19601ada1606SKent Overstreet 	up_write(&c->state_lock);
19611c6fdbd8SKent Overstreet 	return ret;
19621c6fdbd8SKent Overstreet }
19631c6fdbd8SKent Overstreet 
19641c6fdbd8SKent Overstreet /* return with ref on ca->ref: */
1965bf7e49a4SKent Overstreet struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name)
19661c6fdbd8SKent Overstreet {
19673a402c8dSKent Overstreet 	rcu_read_lock();
196841b84fb4SKent Overstreet 	for_each_member_device_rcu(c, ca, NULL)
196941b84fb4SKent Overstreet 		if (!strcmp(name, ca->name)) {
19703a402c8dSKent Overstreet 			rcu_read_unlock();
19711c6fdbd8SKent Overstreet 			return ca;
19721c6fdbd8SKent Overstreet 		}
197341b84fb4SKent Overstreet 	rcu_read_unlock();
197441b84fb4SKent Overstreet 	return ERR_PTR(-BCH_ERR_ENOENT_dev_not_found);
197541b84fb4SKent Overstreet }
19761c6fdbd8SKent Overstreet 
19771c6fdbd8SKent Overstreet /* Filesystem open: */
19781c6fdbd8SKent Overstreet 
19790d529663SKent Overstreet static inline int sb_cmp(struct bch_sb *l, struct bch_sb *r)
19800d529663SKent Overstreet {
19810d529663SKent Overstreet 	return  cmp_int(le64_to_cpu(l->seq), le64_to_cpu(r->seq)) ?:
19820d529663SKent Overstreet 		cmp_int(le64_to_cpu(l->write_time), le64_to_cpu(r->write_time));
19830d529663SKent Overstreet }
19840d529663SKent Overstreet 
19851c6fdbd8SKent Overstreet struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
19861c6fdbd8SKent Overstreet 			    struct bch_opts opts)
19871c6fdbd8SKent Overstreet {
1988d4c8bb69SKent Overstreet 	DARRAY(struct bch_sb_handle) sbs = { 0 };
19891c6fdbd8SKent Overstreet 	struct bch_fs *c = NULL;
1990defd9e39SKent Overstreet 	struct bch_sb_handle *best = NULL;
1991fa8e94faSKent Overstreet 	struct printbuf errbuf = PRINTBUF;
1992e2b60560SKent Overstreet 	int ret = 0;
19931c6fdbd8SKent Overstreet 
1994efe68e1dSKent Overstreet 	if (!try_module_get(THIS_MODULE))
1995efe68e1dSKent Overstreet 		return ERR_PTR(-ENODEV);
1996efe68e1dSKent Overstreet 
19971c6fdbd8SKent Overstreet 	if (!nr_devices) {
1998efe68e1dSKent Overstreet 		ret = -EINVAL;
1999efe68e1dSKent Overstreet 		goto err;
20001c6fdbd8SKent Overstreet 	}
20011c6fdbd8SKent Overstreet 
2002d4c8bb69SKent Overstreet 	ret = darray_make_room(&sbs, nr_devices);
20031c6fdbd8SKent Overstreet 	if (ret)
20041c6fdbd8SKent Overstreet 		goto err;
20051c6fdbd8SKent Overstreet 
2006d4c8bb69SKent Overstreet 	for (unsigned i = 0; i < nr_devices; i++) {
2007d4c8bb69SKent Overstreet 		struct bch_sb_handle sb = { NULL };
2008d4c8bb69SKent Overstreet 
2009d4c8bb69SKent Overstreet 		ret = bch2_read_super(devices[i], &opts, &sb);
2010d4c8bb69SKent Overstreet 		if (ret)
2011d4c8bb69SKent Overstreet 			goto err;
2012d4c8bb69SKent Overstreet 
2013d4c8bb69SKent Overstreet 		BUG_ON(darray_push(&sbs, sb));
20141c6fdbd8SKent Overstreet 	}
20151c6fdbd8SKent Overstreet 
201662719cf3SKent Overstreet 	if (opts.nochanges && !opts.read_only) {
201762719cf3SKent Overstreet 		ret = -BCH_ERR_erofs_nochanges;
201862719cf3SKent Overstreet 		goto err_print;
201962719cf3SKent Overstreet 	}
202062719cf3SKent Overstreet 
2021d4c8bb69SKent Overstreet 	darray_for_each(sbs, sb)
20220d529663SKent Overstreet 		if (!best || sb_cmp(sb->sb, best->sb) > 0)
2023d4c8bb69SKent Overstreet 			best = sb;
20241c6fdbd8SKent Overstreet 
2025d4c8bb69SKent Overstreet 	darray_for_each_reverse(sbs, sb) {
20260d529663SKent Overstreet 		ret = bch2_dev_in_fs(best, sb);
20270d529663SKent Overstreet 
20280d529663SKent Overstreet 		if (ret == -BCH_ERR_device_has_been_removed ||
20290d529663SKent Overstreet 		    ret == -BCH_ERR_device_splitbrain) {
2030d4c8bb69SKent Overstreet 			bch2_free_super(sb);
2031d4c8bb69SKent Overstreet 			darray_remove_item(&sbs, sb);
2032d4c8bb69SKent Overstreet 			best -= best > sb;
20330d529663SKent Overstreet 			ret = 0;
2034625104eaSKent Overstreet 			continue;
2035625104eaSKent Overstreet 		}
2036625104eaSKent Overstreet 
203778c0b75cSKent Overstreet 		if (ret)
20381c6fdbd8SKent Overstreet 			goto err_print;
20391c6fdbd8SKent Overstreet 	}
20401c6fdbd8SKent Overstreet 
2041d4c8bb69SKent Overstreet 	c = bch2_fs_alloc(best->sb, opts);
2042d4c8bb69SKent Overstreet 	ret = PTR_ERR_OR_ZERO(c);
2043d4c8bb69SKent Overstreet 	if (ret)
20441c6fdbd8SKent Overstreet 		goto err;
20451c6fdbd8SKent Overstreet 
20461ada1606SKent Overstreet 	down_write(&c->state_lock);
2047d4c8bb69SKent Overstreet 	darray_for_each(sbs, sb) {
2048d4c8bb69SKent Overstreet 		ret = bch2_dev_attach_bdev(c, sb);
2049e2b60560SKent Overstreet 		if (ret) {
20501ada1606SKent Overstreet 			up_write(&c->state_lock);
2051e2b60560SKent Overstreet 			goto err;
2052e2b60560SKent Overstreet 		}
20531c6fdbd8SKent Overstreet 	}
20541ada1606SKent Overstreet 	up_write(&c->state_lock);
20551c6fdbd8SKent Overstreet 
205678c0b75cSKent Overstreet 	if (!bch2_fs_may_start(c)) {
205778c0b75cSKent Overstreet 		ret = -BCH_ERR_insufficient_devices_to_start;
20581c6fdbd8SKent Overstreet 		goto err_print;
205978c0b75cSKent Overstreet 	}
20601c6fdbd8SKent Overstreet 
20611c6fdbd8SKent Overstreet 	if (!c->opts.nostart) {
2062619f5beeSKent Overstreet 		ret = bch2_fs_start(c);
2063619f5beeSKent Overstreet 		if (ret)
2064619f5beeSKent Overstreet 			goto err;
20651c6fdbd8SKent Overstreet 	}
20661c6fdbd8SKent Overstreet out:
2067d4c8bb69SKent Overstreet 	darray_for_each(sbs, sb)
2068d4c8bb69SKent Overstreet 		bch2_free_super(sb);
2069d4c8bb69SKent Overstreet 	darray_exit(&sbs);
2070fa8e94faSKent Overstreet 	printbuf_exit(&errbuf);
20711c6fdbd8SKent Overstreet 	module_put(THIS_MODULE);
20721c6fdbd8SKent Overstreet 	return c;
20731c6fdbd8SKent Overstreet err_print:
20741c6fdbd8SKent Overstreet 	pr_err("bch_fs_open err opening %s: %s",
207578c0b75cSKent Overstreet 	       devices[0], bch2_err_str(ret));
20761c6fdbd8SKent Overstreet err:
20777be9ab63SChris Webb 	if (!IS_ERR_OR_NULL(c))
20781c6fdbd8SKent Overstreet 		bch2_fs_stop(c);
20791c6fdbd8SKent Overstreet 	c = ERR_PTR(ret);
20801c6fdbd8SKent Overstreet 	goto out;
20811c6fdbd8SKent Overstreet }
20821c6fdbd8SKent Overstreet 
20831c6fdbd8SKent Overstreet /* Global interfaces/init */
20841c6fdbd8SKent Overstreet 
20851c6fdbd8SKent Overstreet static void bcachefs_exit(void)
20861c6fdbd8SKent Overstreet {
20871c6fdbd8SKent Overstreet 	bch2_debug_exit();
20881c6fdbd8SKent Overstreet 	bch2_vfs_exit();
20891c6fdbd8SKent Overstreet 	bch2_chardev_exit();
209014ba3706SKent Overstreet 	bch2_btree_key_cache_exit();
20911c6fdbd8SKent Overstreet 	if (bcachefs_kset)
20921c6fdbd8SKent Overstreet 		kset_unregister(bcachefs_kset);
20931c6fdbd8SKent Overstreet }
20941c6fdbd8SKent Overstreet 
20951c6fdbd8SKent Overstreet static int __init bcachefs_init(void)
20961c6fdbd8SKent Overstreet {
20971c6fdbd8SKent Overstreet 	bch2_bkey_pack_test();
20981c6fdbd8SKent Overstreet 
20991c6fdbd8SKent Overstreet 	if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
210014ba3706SKent Overstreet 	    bch2_btree_key_cache_init() ||
21011c6fdbd8SKent Overstreet 	    bch2_chardev_init() ||
21021c6fdbd8SKent Overstreet 	    bch2_vfs_init() ||
21031c6fdbd8SKent Overstreet 	    bch2_debug_init())
21041c6fdbd8SKent Overstreet 		goto err;
21051c6fdbd8SKent Overstreet 
21061c6fdbd8SKent Overstreet 	return 0;
21071c6fdbd8SKent Overstreet err:
21081c6fdbd8SKent Overstreet 	bcachefs_exit();
21091c6fdbd8SKent Overstreet 	return -ENOMEM;
21101c6fdbd8SKent Overstreet }
21111c6fdbd8SKent Overstreet 
21121c6fdbd8SKent Overstreet #define BCH_DEBUG_PARAM(name, description)			\
21131c6fdbd8SKent Overstreet 	bool bch2_##name;					\
21141c6fdbd8SKent Overstreet 	module_param_named(name, bch2_##name, bool, 0644);	\
21151c6fdbd8SKent Overstreet 	MODULE_PARM_DESC(name, description);
21161c6fdbd8SKent Overstreet BCH_DEBUG_PARAMS()
21171c6fdbd8SKent Overstreet #undef BCH_DEBUG_PARAM
21181c6fdbd8SKent Overstreet 
2119e08e63e4SKent Overstreet __maybe_unused
212073bd774dSKent Overstreet static unsigned bch2_metadata_version = bcachefs_metadata_version_current;
21211c6fdbd8SKent Overstreet module_param_named(version, bch2_metadata_version, uint, 0400);
21221c6fdbd8SKent Overstreet 
21231c6fdbd8SKent Overstreet module_exit(bcachefs_exit);
21241c6fdbd8SKent Overstreet module_init(bcachefs_init);
2125