xref: /linux/fs/bcachefs/super.c (revision 581edb634140fe3da0bc3c8430116e735ebff897)
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"
111c6fdbd8SKent Overstreet #include "alloc.h"
121c6fdbd8SKent Overstreet #include "btree_cache.h"
131c6fdbd8SKent Overstreet #include "btree_gc.h"
141c6fdbd8SKent Overstreet #include "btree_update_interior.h"
151c6fdbd8SKent Overstreet #include "btree_io.h"
161c6fdbd8SKent Overstreet #include "chardev.h"
171c6fdbd8SKent Overstreet #include "checksum.h"
181c6fdbd8SKent Overstreet #include "clock.h"
191c6fdbd8SKent Overstreet #include "compress.h"
201c6fdbd8SKent Overstreet #include "debug.h"
211c6fdbd8SKent Overstreet #include "disk_groups.h"
221c6fdbd8SKent Overstreet #include "error.h"
231c6fdbd8SKent Overstreet #include "fs.h"
241c6fdbd8SKent Overstreet #include "fs-io.h"
251c6fdbd8SKent Overstreet #include "fsck.h"
261c6fdbd8SKent Overstreet #include "inode.h"
271c6fdbd8SKent Overstreet #include "io.h"
281c6fdbd8SKent Overstreet #include "journal.h"
291c6fdbd8SKent Overstreet #include "journal_reclaim.h"
301c6fdbd8SKent Overstreet #include "move.h"
311c6fdbd8SKent Overstreet #include "migrate.h"
321c6fdbd8SKent Overstreet #include "movinggc.h"
331c6fdbd8SKent Overstreet #include "quota.h"
341c6fdbd8SKent Overstreet #include "rebalance.h"
351c6fdbd8SKent Overstreet #include "recovery.h"
361c6fdbd8SKent Overstreet #include "replicas.h"
371c6fdbd8SKent Overstreet #include "super.h"
381c6fdbd8SKent Overstreet #include "super-io.h"
391c6fdbd8SKent Overstreet #include "sysfs.h"
401c6fdbd8SKent Overstreet #include "trace.h"
411c6fdbd8SKent Overstreet 
421c6fdbd8SKent Overstreet #include <linux/backing-dev.h>
431c6fdbd8SKent Overstreet #include <linux/blkdev.h>
441c6fdbd8SKent Overstreet #include <linux/debugfs.h>
451c6fdbd8SKent Overstreet #include <linux/device.h>
461c6fdbd8SKent Overstreet #include <linux/idr.h>
471c6fdbd8SKent Overstreet #include <linux/kthread.h>
481c6fdbd8SKent Overstreet #include <linux/module.h>
491c6fdbd8SKent Overstreet #include <linux/percpu.h>
501c6fdbd8SKent Overstreet #include <linux/random.h>
511c6fdbd8SKent Overstreet #include <linux/sysfs.h>
521c6fdbd8SKent Overstreet #include <crypto/hash.h>
531c6fdbd8SKent Overstreet 
541c6fdbd8SKent Overstreet MODULE_LICENSE("GPL");
551c6fdbd8SKent Overstreet MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
561c6fdbd8SKent Overstreet 
571c6fdbd8SKent Overstreet #define KTYPE(type)							\
581c6fdbd8SKent Overstreet static const struct attribute_group type ## _group = {			\
591c6fdbd8SKent Overstreet 	.attrs = type ## _files						\
601c6fdbd8SKent Overstreet };									\
611c6fdbd8SKent Overstreet 									\
621c6fdbd8SKent Overstreet static const struct attribute_group *type ## _groups[] = {		\
631c6fdbd8SKent Overstreet 	&type ## _group,						\
641c6fdbd8SKent Overstreet 	NULL								\
651c6fdbd8SKent Overstreet };									\
661c6fdbd8SKent Overstreet 									\
671c6fdbd8SKent Overstreet static const struct kobj_type type ## _ktype = {			\
681c6fdbd8SKent Overstreet 	.release	= type ## _release,				\
691c6fdbd8SKent Overstreet 	.sysfs_ops	= &type ## _sysfs_ops,				\
701c6fdbd8SKent Overstreet 	.default_groups = type ## _groups				\
711c6fdbd8SKent Overstreet }
721c6fdbd8SKent Overstreet 
731c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *);
741c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *);
751c6fdbd8SKent Overstreet 
761c6fdbd8SKent Overstreet static void bch2_fs_internal_release(struct kobject *k)
771c6fdbd8SKent Overstreet {
781c6fdbd8SKent Overstreet }
791c6fdbd8SKent Overstreet 
801c6fdbd8SKent Overstreet static void bch2_fs_opts_dir_release(struct kobject *k)
811c6fdbd8SKent Overstreet {
821c6fdbd8SKent Overstreet }
831c6fdbd8SKent Overstreet 
841c6fdbd8SKent Overstreet static void bch2_fs_time_stats_release(struct kobject *k)
851c6fdbd8SKent Overstreet {
861c6fdbd8SKent Overstreet }
871c6fdbd8SKent Overstreet 
881c6fdbd8SKent Overstreet KTYPE(bch2_fs);
891c6fdbd8SKent Overstreet KTYPE(bch2_fs_internal);
901c6fdbd8SKent Overstreet KTYPE(bch2_fs_opts_dir);
911c6fdbd8SKent Overstreet KTYPE(bch2_fs_time_stats);
921c6fdbd8SKent Overstreet KTYPE(bch2_dev);
931c6fdbd8SKent Overstreet 
941c6fdbd8SKent Overstreet static struct kset *bcachefs_kset;
951c6fdbd8SKent Overstreet static LIST_HEAD(bch_fs_list);
961c6fdbd8SKent Overstreet static DEFINE_MUTEX(bch_fs_list_lock);
971c6fdbd8SKent Overstreet 
981c6fdbd8SKent Overstreet static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
991c6fdbd8SKent Overstreet 
1001c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *);
1011c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *, unsigned);
1021c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *);
1031c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
1041c6fdbd8SKent Overstreet 
1051c6fdbd8SKent Overstreet struct bch_fs *bch2_dev_to_fs(dev_t dev)
1061c6fdbd8SKent Overstreet {
1071c6fdbd8SKent Overstreet 	struct bch_fs *c;
1081c6fdbd8SKent Overstreet 	struct bch_dev *ca;
1091c6fdbd8SKent Overstreet 	unsigned i;
1101c6fdbd8SKent Overstreet 
1111c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
1121c6fdbd8SKent Overstreet 	rcu_read_lock();
1131c6fdbd8SKent Overstreet 
1141c6fdbd8SKent Overstreet 	list_for_each_entry(c, &bch_fs_list, list)
1151c6fdbd8SKent Overstreet 		for_each_member_device_rcu(ca, c, i, NULL)
1161c6fdbd8SKent Overstreet 			if (ca->disk_sb.bdev->bd_dev == dev) {
1171c6fdbd8SKent Overstreet 				closure_get(&c->cl);
1181c6fdbd8SKent Overstreet 				goto found;
1191c6fdbd8SKent Overstreet 			}
1201c6fdbd8SKent Overstreet 	c = NULL;
1211c6fdbd8SKent Overstreet found:
1221c6fdbd8SKent Overstreet 	rcu_read_unlock();
1231c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
1241c6fdbd8SKent Overstreet 
1251c6fdbd8SKent Overstreet 	return c;
1261c6fdbd8SKent Overstreet }
1271c6fdbd8SKent Overstreet 
1281c6fdbd8SKent Overstreet static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid)
1291c6fdbd8SKent Overstreet {
1301c6fdbd8SKent Overstreet 	struct bch_fs *c;
1311c6fdbd8SKent Overstreet 
1321c6fdbd8SKent Overstreet 	lockdep_assert_held(&bch_fs_list_lock);
1331c6fdbd8SKent Overstreet 
1341c6fdbd8SKent Overstreet 	list_for_each_entry(c, &bch_fs_list, list)
1351c6fdbd8SKent Overstreet 		if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid)))
1361c6fdbd8SKent Overstreet 			return c;
1371c6fdbd8SKent Overstreet 
1381c6fdbd8SKent Overstreet 	return NULL;
1391c6fdbd8SKent Overstreet }
1401c6fdbd8SKent Overstreet 
1411c6fdbd8SKent Overstreet struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid)
1421c6fdbd8SKent Overstreet {
1431c6fdbd8SKent Overstreet 	struct bch_fs *c;
1441c6fdbd8SKent Overstreet 
1451c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
1461c6fdbd8SKent Overstreet 	c = __bch2_uuid_to_fs(uuid);
1471c6fdbd8SKent Overstreet 	if (c)
1481c6fdbd8SKent Overstreet 		closure_get(&c->cl);
1491c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
1501c6fdbd8SKent Overstreet 
1511c6fdbd8SKent Overstreet 	return c;
1521c6fdbd8SKent Overstreet }
1531c6fdbd8SKent Overstreet 
1541c6fdbd8SKent Overstreet /* Filesystem RO/RW: */
1551c6fdbd8SKent Overstreet 
1561c6fdbd8SKent Overstreet /*
1571c6fdbd8SKent Overstreet  * For startup/shutdown of RW stuff, the dependencies are:
1581c6fdbd8SKent Overstreet  *
1591c6fdbd8SKent Overstreet  * - foreground writes depend on copygc and rebalance (to free up space)
1601c6fdbd8SKent Overstreet  *
1611c6fdbd8SKent Overstreet  * - copygc and rebalance depend on mark and sweep gc (they actually probably
1621c6fdbd8SKent Overstreet  *   don't because they either reserve ahead of time or don't block if
1631c6fdbd8SKent Overstreet  *   allocations fail, but allocations can require mark and sweep gc to run
1641c6fdbd8SKent Overstreet  *   because of generation number wraparound)
1651c6fdbd8SKent Overstreet  *
1661c6fdbd8SKent Overstreet  * - all of the above depends on the allocator threads
1671c6fdbd8SKent Overstreet  *
1681c6fdbd8SKent Overstreet  * - allocator depends on the journal (when it rewrites prios and gens)
1691c6fdbd8SKent Overstreet  */
1701c6fdbd8SKent Overstreet 
1711c6fdbd8SKent Overstreet static void __bch2_fs_read_only(struct bch_fs *c)
1721c6fdbd8SKent Overstreet {
1731c6fdbd8SKent Overstreet 	struct bch_dev *ca;
1741c6fdbd8SKent Overstreet 	unsigned i;
1751c6fdbd8SKent Overstreet 
1761c6fdbd8SKent Overstreet 	bch2_rebalance_stop(c);
1771c6fdbd8SKent Overstreet 
1781c6fdbd8SKent Overstreet 	for_each_member_device(ca, c, i)
1791c6fdbd8SKent Overstreet 		bch2_copygc_stop(ca);
1801c6fdbd8SKent Overstreet 
1811c6fdbd8SKent Overstreet 	bch2_gc_thread_stop(c);
1821c6fdbd8SKent Overstreet 
1831c6fdbd8SKent Overstreet 	/*
1841c6fdbd8SKent Overstreet 	 * Flush journal before stopping allocators, because flushing journal
1851c6fdbd8SKent Overstreet 	 * blacklist entries involves allocating new btree nodes:
1861c6fdbd8SKent Overstreet 	 */
1871c6fdbd8SKent Overstreet 	bch2_journal_flush_all_pins(&c->journal);
1881c6fdbd8SKent Overstreet 
1891c6fdbd8SKent Overstreet 	for_each_member_device(ca, c, i)
1901c6fdbd8SKent Overstreet 		bch2_dev_allocator_stop(ca);
1911c6fdbd8SKent Overstreet 
1921c6fdbd8SKent Overstreet 	bch2_journal_flush_all_pins(&c->journal);
1931c6fdbd8SKent Overstreet 
1941c6fdbd8SKent Overstreet 	/*
1951c6fdbd8SKent Overstreet 	 * We need to explicitly wait on btree interior updates to complete
1961c6fdbd8SKent Overstreet 	 * before stopping the journal, flushing all journal pins isn't
1971c6fdbd8SKent Overstreet 	 * sufficient, because in the BTREE_INTERIOR_UPDATING_ROOT case btree
1981c6fdbd8SKent Overstreet 	 * interior updates have to drop their journal pin before they're
1991c6fdbd8SKent Overstreet 	 * fully complete:
2001c6fdbd8SKent Overstreet 	 */
2011c6fdbd8SKent Overstreet 	closure_wait_event(&c->btree_interior_update_wait,
2021c6fdbd8SKent Overstreet 			   !bch2_btree_interior_updates_nr_pending(c));
2031c6fdbd8SKent Overstreet 
2041c6fdbd8SKent Overstreet 	bch2_fs_journal_stop(&c->journal);
2051c6fdbd8SKent Overstreet 
2061c6fdbd8SKent Overstreet 	/*
2071c6fdbd8SKent Overstreet 	 * the journal kicks off btree writes via reclaim - wait for in flight
2081c6fdbd8SKent Overstreet 	 * writes after stopping journal:
2091c6fdbd8SKent Overstreet 	 */
2101c6fdbd8SKent Overstreet 	if (test_bit(BCH_FS_EMERGENCY_RO, &c->flags))
2111c6fdbd8SKent Overstreet 		bch2_btree_flush_all_writes(c);
2121c6fdbd8SKent Overstreet 	else
2131c6fdbd8SKent Overstreet 		bch2_btree_verify_flushed(c);
2141c6fdbd8SKent Overstreet 
2151c6fdbd8SKent Overstreet 	/*
2161c6fdbd8SKent Overstreet 	 * After stopping journal:
2171c6fdbd8SKent Overstreet 	 */
2181c6fdbd8SKent Overstreet 	for_each_member_device(ca, c, i)
2191c6fdbd8SKent Overstreet 		bch2_dev_allocator_remove(c, ca);
2201c6fdbd8SKent Overstreet }
2211c6fdbd8SKent Overstreet 
2221c6fdbd8SKent Overstreet static void bch2_writes_disabled(struct percpu_ref *writes)
2231c6fdbd8SKent Overstreet {
2241c6fdbd8SKent Overstreet 	struct bch_fs *c = container_of(writes, struct bch_fs, writes);
2251c6fdbd8SKent Overstreet 
2261c6fdbd8SKent Overstreet 	set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
2271c6fdbd8SKent Overstreet 	wake_up(&bch_read_only_wait);
2281c6fdbd8SKent Overstreet }
2291c6fdbd8SKent Overstreet 
2301c6fdbd8SKent Overstreet void bch2_fs_read_only(struct bch_fs *c)
2311c6fdbd8SKent Overstreet {
2321c6fdbd8SKent Overstreet 	if (c->state == BCH_FS_RO)
2331c6fdbd8SKent Overstreet 		return;
2341c6fdbd8SKent Overstreet 
2351c6fdbd8SKent Overstreet 	BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
2361c6fdbd8SKent Overstreet 
2371c6fdbd8SKent Overstreet 	/*
2381c6fdbd8SKent Overstreet 	 * Block new foreground-end write operations from starting - any new
2391c6fdbd8SKent Overstreet 	 * writes will return -EROFS:
2401c6fdbd8SKent Overstreet 	 *
2411c6fdbd8SKent Overstreet 	 * (This is really blocking new _allocations_, writes to previously
2421c6fdbd8SKent Overstreet 	 * allocated space can still happen until stopping the allocator in
2431c6fdbd8SKent Overstreet 	 * bch2_dev_allocator_stop()).
2441c6fdbd8SKent Overstreet 	 */
2451c6fdbd8SKent Overstreet 	percpu_ref_kill(&c->writes);
2461c6fdbd8SKent Overstreet 
2471c6fdbd8SKent Overstreet 	cancel_delayed_work(&c->pd_controllers_update);
2481c6fdbd8SKent Overstreet 
2491c6fdbd8SKent Overstreet 	/*
2501c6fdbd8SKent Overstreet 	 * If we're not doing an emergency shutdown, we want to wait on
2511c6fdbd8SKent Overstreet 	 * outstanding writes to complete so they don't see spurious errors due
2521c6fdbd8SKent Overstreet 	 * to shutting down the allocator:
2531c6fdbd8SKent Overstreet 	 *
2541c6fdbd8SKent Overstreet 	 * If we are doing an emergency shutdown outstanding writes may
2551c6fdbd8SKent Overstreet 	 * hang until we shutdown the allocator so we don't want to wait
2561c6fdbd8SKent Overstreet 	 * on outstanding writes before shutting everything down - but
2571c6fdbd8SKent Overstreet 	 * we do need to wait on them before returning and signalling
2581c6fdbd8SKent Overstreet 	 * that going RO is complete:
2591c6fdbd8SKent Overstreet 	 */
2601c6fdbd8SKent Overstreet 	wait_event(bch_read_only_wait,
2611c6fdbd8SKent Overstreet 		   test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
2621c6fdbd8SKent Overstreet 		   test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
2631c6fdbd8SKent Overstreet 
2641c6fdbd8SKent Overstreet 	__bch2_fs_read_only(c);
2651c6fdbd8SKent Overstreet 
2661c6fdbd8SKent Overstreet 	wait_event(bch_read_only_wait,
2671c6fdbd8SKent Overstreet 		   test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
2681c6fdbd8SKent Overstreet 
2691c6fdbd8SKent Overstreet 	clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
2701c6fdbd8SKent Overstreet 
2711c6fdbd8SKent Overstreet 	if (!bch2_journal_error(&c->journal) &&
2721c6fdbd8SKent Overstreet 	    !test_bit(BCH_FS_ERROR, &c->flags) &&
2731c6fdbd8SKent Overstreet 	    !test_bit(BCH_FS_EMERGENCY_RO, &c->flags))
2741c6fdbd8SKent Overstreet 		bch2_fs_mark_clean(c, true);
2751c6fdbd8SKent Overstreet 
2761c6fdbd8SKent Overstreet 	if (c->state != BCH_FS_STOPPING)
2771c6fdbd8SKent Overstreet 		c->state = BCH_FS_RO;
2781c6fdbd8SKent Overstreet }
2791c6fdbd8SKent Overstreet 
2801c6fdbd8SKent Overstreet static void bch2_fs_read_only_work(struct work_struct *work)
2811c6fdbd8SKent Overstreet {
2821c6fdbd8SKent Overstreet 	struct bch_fs *c =
2831c6fdbd8SKent Overstreet 		container_of(work, struct bch_fs, read_only_work);
2841c6fdbd8SKent Overstreet 
2851c6fdbd8SKent Overstreet 	mutex_lock(&c->state_lock);
2861c6fdbd8SKent Overstreet 	bch2_fs_read_only(c);
2871c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
2881c6fdbd8SKent Overstreet }
2891c6fdbd8SKent Overstreet 
2901c6fdbd8SKent Overstreet static void bch2_fs_read_only_async(struct bch_fs *c)
2911c6fdbd8SKent Overstreet {
2921c6fdbd8SKent Overstreet 	queue_work(system_long_wq, &c->read_only_work);
2931c6fdbd8SKent Overstreet }
2941c6fdbd8SKent Overstreet 
2951c6fdbd8SKent Overstreet bool bch2_fs_emergency_read_only(struct bch_fs *c)
2961c6fdbd8SKent Overstreet {
2971c6fdbd8SKent Overstreet 	bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
2981c6fdbd8SKent Overstreet 
2991c6fdbd8SKent Overstreet 	bch2_fs_read_only_async(c);
3001c6fdbd8SKent Overstreet 	bch2_journal_halt(&c->journal);
3011c6fdbd8SKent Overstreet 
3021c6fdbd8SKent Overstreet 	wake_up(&bch_read_only_wait);
3031c6fdbd8SKent Overstreet 	return ret;
3041c6fdbd8SKent Overstreet }
3051c6fdbd8SKent Overstreet 
3061c6fdbd8SKent Overstreet const char *bch2_fs_read_write(struct bch_fs *c)
3071c6fdbd8SKent Overstreet {
3081c6fdbd8SKent Overstreet 	struct bch_dev *ca;
3091c6fdbd8SKent Overstreet 	const char *err = NULL;
3101c6fdbd8SKent Overstreet 	unsigned i;
3111c6fdbd8SKent Overstreet 
3121c6fdbd8SKent Overstreet 	if (c->state == BCH_FS_RW)
3131c6fdbd8SKent Overstreet 		return NULL;
3141c6fdbd8SKent Overstreet 
3151c6fdbd8SKent Overstreet 	bch2_fs_mark_clean(c, false);
3161c6fdbd8SKent Overstreet 
3171c6fdbd8SKent Overstreet 	for_each_rw_member(ca, c, i)
3181c6fdbd8SKent Overstreet 		bch2_dev_allocator_add(c, ca);
3191c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
3201c6fdbd8SKent Overstreet 
3211c6fdbd8SKent Overstreet 	err = "error starting allocator thread";
3221c6fdbd8SKent Overstreet 	for_each_rw_member(ca, c, i)
3231c6fdbd8SKent Overstreet 		if (bch2_dev_allocator_start(ca)) {
3241c6fdbd8SKent Overstreet 			percpu_ref_put(&ca->io_ref);
3251c6fdbd8SKent Overstreet 			goto err;
3261c6fdbd8SKent Overstreet 		}
3271c6fdbd8SKent Overstreet 
3281c6fdbd8SKent Overstreet 	err = "error starting btree GC thread";
3291c6fdbd8SKent Overstreet 	if (bch2_gc_thread_start(c))
3301c6fdbd8SKent Overstreet 		goto err;
3311c6fdbd8SKent Overstreet 
3321c6fdbd8SKent Overstreet 	err = "error starting copygc thread";
3331c6fdbd8SKent Overstreet 	for_each_rw_member(ca, c, i)
3341c6fdbd8SKent Overstreet 		if (bch2_copygc_start(c, ca)) {
3351c6fdbd8SKent Overstreet 			percpu_ref_put(&ca->io_ref);
3361c6fdbd8SKent Overstreet 			goto err;
3371c6fdbd8SKent Overstreet 		}
3381c6fdbd8SKent Overstreet 
3391c6fdbd8SKent Overstreet 	err = "error starting rebalance thread";
3401c6fdbd8SKent Overstreet 	if (bch2_rebalance_start(c))
3411c6fdbd8SKent Overstreet 		goto err;
3421c6fdbd8SKent Overstreet 
3431c6fdbd8SKent Overstreet 	schedule_delayed_work(&c->pd_controllers_update, 5 * HZ);
3441c6fdbd8SKent Overstreet 
3451c6fdbd8SKent Overstreet 	if (c->state != BCH_FS_STARTING)
3461c6fdbd8SKent Overstreet 		percpu_ref_reinit(&c->writes);
3471c6fdbd8SKent Overstreet 
3481c6fdbd8SKent Overstreet 	c->state = BCH_FS_RW;
3491c6fdbd8SKent Overstreet 	return NULL;
3501c6fdbd8SKent Overstreet err:
3511c6fdbd8SKent Overstreet 	__bch2_fs_read_only(c);
3521c6fdbd8SKent Overstreet 	return err;
3531c6fdbd8SKent Overstreet }
3541c6fdbd8SKent Overstreet 
3551c6fdbd8SKent Overstreet /* Filesystem startup/shutdown: */
3561c6fdbd8SKent Overstreet 
3571c6fdbd8SKent Overstreet static void bch2_fs_free(struct bch_fs *c)
3581c6fdbd8SKent Overstreet {
3591c6fdbd8SKent Overstreet 	unsigned i;
3601c6fdbd8SKent Overstreet 
3611c6fdbd8SKent Overstreet 	for (i = 0; i < BCH_TIME_STAT_NR; i++)
3621c6fdbd8SKent Overstreet 		bch2_time_stats_exit(&c->times[i]);
3631c6fdbd8SKent Overstreet 
3641c6fdbd8SKent Overstreet 	bch2_fs_quota_exit(c);
3651c6fdbd8SKent Overstreet 	bch2_fs_fsio_exit(c);
3661c6fdbd8SKent Overstreet 	bch2_fs_encryption_exit(c);
3671c6fdbd8SKent Overstreet 	bch2_fs_io_exit(c);
3681c6fdbd8SKent Overstreet 	bch2_fs_btree_cache_exit(c);
3691c6fdbd8SKent Overstreet 	bch2_fs_journal_exit(&c->journal);
3701c6fdbd8SKent Overstreet 	bch2_io_clock_exit(&c->io_clock[WRITE]);
3711c6fdbd8SKent Overstreet 	bch2_io_clock_exit(&c->io_clock[READ]);
3721c6fdbd8SKent Overstreet 	bch2_fs_compress_exit(c);
3731c6fdbd8SKent Overstreet 	percpu_free_rwsem(&c->usage_lock);
3741c6fdbd8SKent Overstreet 	free_percpu(c->usage_percpu);
375*581edb63SKent Overstreet 	mempool_exit(&c->btree_iters_pool);
3761c6fdbd8SKent Overstreet 	mempool_exit(&c->btree_bounce_pool);
3771c6fdbd8SKent Overstreet 	bioset_exit(&c->btree_bio);
3781c6fdbd8SKent Overstreet 	mempool_exit(&c->btree_interior_update_pool);
3791c6fdbd8SKent Overstreet 	mempool_exit(&c->btree_reserve_pool);
3801c6fdbd8SKent Overstreet 	mempool_exit(&c->fill_iter);
3811c6fdbd8SKent Overstreet 	percpu_ref_exit(&c->writes);
3821c6fdbd8SKent Overstreet 	kfree(rcu_dereference_protected(c->replicas, 1));
3831c6fdbd8SKent Overstreet 	kfree(rcu_dereference_protected(c->disk_groups, 1));
3841c6fdbd8SKent Overstreet 
3851c6fdbd8SKent Overstreet 	if (c->copygc_wq)
3861c6fdbd8SKent Overstreet 		destroy_workqueue(c->copygc_wq);
3871c6fdbd8SKent Overstreet 	if (c->wq)
3881c6fdbd8SKent Overstreet 		destroy_workqueue(c->wq);
3891c6fdbd8SKent Overstreet 
3901c6fdbd8SKent Overstreet 	free_pages((unsigned long) c->disk_sb.sb,
3911c6fdbd8SKent Overstreet 		   c->disk_sb.page_order);
3921c6fdbd8SKent Overstreet 	kvpfree(c, sizeof(*c));
3931c6fdbd8SKent Overstreet 	module_put(THIS_MODULE);
3941c6fdbd8SKent Overstreet }
3951c6fdbd8SKent Overstreet 
3961c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *kobj)
3971c6fdbd8SKent Overstreet {
3981c6fdbd8SKent Overstreet 	struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
3991c6fdbd8SKent Overstreet 
4001c6fdbd8SKent Overstreet 	bch2_fs_free(c);
4011c6fdbd8SKent Overstreet }
4021c6fdbd8SKent Overstreet 
4031c6fdbd8SKent Overstreet void bch2_fs_stop(struct bch_fs *c)
4041c6fdbd8SKent Overstreet {
4051c6fdbd8SKent Overstreet 	struct bch_dev *ca;
4061c6fdbd8SKent Overstreet 	unsigned i;
4071c6fdbd8SKent Overstreet 
408af1c6871SKent Overstreet 	bch_verbose(c, "shutting down");
409af1c6871SKent Overstreet 
4101c6fdbd8SKent Overstreet 	for_each_member_device(ca, c, i)
4111c6fdbd8SKent Overstreet 		if (ca->kobj.state_in_sysfs &&
4121c6fdbd8SKent Overstreet 		    ca->disk_sb.bdev)
4131c6fdbd8SKent Overstreet 			sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
4141c6fdbd8SKent Overstreet 
4151c6fdbd8SKent Overstreet 	if (c->kobj.state_in_sysfs)
4161c6fdbd8SKent Overstreet 		kobject_del(&c->kobj);
4171c6fdbd8SKent Overstreet 
4181c6fdbd8SKent Overstreet 	bch2_fs_debug_exit(c);
4191c6fdbd8SKent Overstreet 	bch2_fs_chardev_exit(c);
4201c6fdbd8SKent Overstreet 
4211c6fdbd8SKent Overstreet 	kobject_put(&c->time_stats);
4221c6fdbd8SKent Overstreet 	kobject_put(&c->opts_dir);
4231c6fdbd8SKent Overstreet 	kobject_put(&c->internal);
4241c6fdbd8SKent Overstreet 
4251c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
4261c6fdbd8SKent Overstreet 	list_del(&c->list);
4271c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
4281c6fdbd8SKent Overstreet 
4291c6fdbd8SKent Overstreet 	closure_sync(&c->cl);
4301c6fdbd8SKent Overstreet 	closure_debug_destroy(&c->cl);
4311c6fdbd8SKent Overstreet 
4321c6fdbd8SKent Overstreet 	mutex_lock(&c->state_lock);
4331c6fdbd8SKent Overstreet 	bch2_fs_read_only(c);
4341c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
4351c6fdbd8SKent Overstreet 
4361c6fdbd8SKent Overstreet 	/* btree prefetch might have kicked off reads in the background: */
4371c6fdbd8SKent Overstreet 	bch2_btree_flush_all_reads(c);
4381c6fdbd8SKent Overstreet 
4391c6fdbd8SKent Overstreet 	for_each_member_device(ca, c, i)
4401c6fdbd8SKent Overstreet 		cancel_work_sync(&ca->io_error_work);
4411c6fdbd8SKent Overstreet 
4421c6fdbd8SKent Overstreet 	cancel_work_sync(&c->btree_write_error_work);
4431c6fdbd8SKent Overstreet 	cancel_delayed_work_sync(&c->pd_controllers_update);
4441c6fdbd8SKent Overstreet 	cancel_work_sync(&c->read_only_work);
4451c6fdbd8SKent Overstreet 
4461c6fdbd8SKent Overstreet 	for (i = 0; i < c->sb.nr_devices; i++)
4471c6fdbd8SKent Overstreet 		if (c->devs[i])
4481c6fdbd8SKent Overstreet 			bch2_dev_free(rcu_dereference_protected(c->devs[i], 1));
4491c6fdbd8SKent Overstreet 
450af1c6871SKent Overstreet 	bch_verbose(c, "shutdown complete");
451af1c6871SKent Overstreet 
4521c6fdbd8SKent Overstreet 	kobject_put(&c->kobj);
4531c6fdbd8SKent Overstreet }
4541c6fdbd8SKent Overstreet 
4551c6fdbd8SKent Overstreet static const char *bch2_fs_online(struct bch_fs *c)
4561c6fdbd8SKent Overstreet {
4571c6fdbd8SKent Overstreet 	struct bch_dev *ca;
4581c6fdbd8SKent Overstreet 	const char *err = NULL;
4591c6fdbd8SKent Overstreet 	unsigned i;
4601c6fdbd8SKent Overstreet 	int ret;
4611c6fdbd8SKent Overstreet 
4621c6fdbd8SKent Overstreet 	lockdep_assert_held(&bch_fs_list_lock);
4631c6fdbd8SKent Overstreet 
4641c6fdbd8SKent Overstreet 	if (!list_empty(&c->list))
4651c6fdbd8SKent Overstreet 		return NULL;
4661c6fdbd8SKent Overstreet 
4671c6fdbd8SKent Overstreet 	if (__bch2_uuid_to_fs(c->sb.uuid))
4681c6fdbd8SKent Overstreet 		return "filesystem UUID already open";
4691c6fdbd8SKent Overstreet 
4701c6fdbd8SKent Overstreet 	ret = bch2_fs_chardev_init(c);
4711c6fdbd8SKent Overstreet 	if (ret)
4721c6fdbd8SKent Overstreet 		return "error creating character device";
4731c6fdbd8SKent Overstreet 
4741c6fdbd8SKent Overstreet 	bch2_fs_debug_init(c);
4751c6fdbd8SKent Overstreet 
4761c6fdbd8SKent Overstreet 	if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ||
4771c6fdbd8SKent Overstreet 	    kobject_add(&c->internal, &c->kobj, "internal") ||
4781c6fdbd8SKent Overstreet 	    kobject_add(&c->opts_dir, &c->kobj, "options") ||
4791c6fdbd8SKent Overstreet 	    kobject_add(&c->time_stats, &c->kobj, "time_stats") ||
4801c6fdbd8SKent Overstreet 	    bch2_opts_create_sysfs_files(&c->opts_dir))
4811c6fdbd8SKent Overstreet 		return "error creating sysfs objects";
4821c6fdbd8SKent Overstreet 
4831c6fdbd8SKent Overstreet 	mutex_lock(&c->state_lock);
4841c6fdbd8SKent Overstreet 
4851c6fdbd8SKent Overstreet 	err = "error creating sysfs objects";
4861c6fdbd8SKent Overstreet 	__for_each_member_device(ca, c, i, NULL)
4871c6fdbd8SKent Overstreet 		if (bch2_dev_sysfs_online(c, ca))
4881c6fdbd8SKent Overstreet 			goto err;
4891c6fdbd8SKent Overstreet 
4901c6fdbd8SKent Overstreet 	list_add(&c->list, &bch_fs_list);
4911c6fdbd8SKent Overstreet 	err = NULL;
4921c6fdbd8SKent Overstreet err:
4931c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
4941c6fdbd8SKent Overstreet 	return err;
4951c6fdbd8SKent Overstreet }
4961c6fdbd8SKent Overstreet 
4971c6fdbd8SKent Overstreet static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
4981c6fdbd8SKent Overstreet {
4991c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
5001c6fdbd8SKent Overstreet 	struct bch_fs *c;
5011c6fdbd8SKent Overstreet 	unsigned i, iter_size;
5021c6fdbd8SKent Overstreet 	const char *err;
5031c6fdbd8SKent Overstreet 
5041c6fdbd8SKent Overstreet 	pr_verbose_init(opts, "");
5051c6fdbd8SKent Overstreet 
5061c6fdbd8SKent Overstreet 	c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO);
5071c6fdbd8SKent Overstreet 	if (!c)
5081c6fdbd8SKent Overstreet 		goto out;
5091c6fdbd8SKent Overstreet 
5101c6fdbd8SKent Overstreet 	__module_get(THIS_MODULE);
5111c6fdbd8SKent Overstreet 
5121c6fdbd8SKent Overstreet 	c->minor		= -1;
5131c6fdbd8SKent Overstreet 	c->disk_sb.fs_sb	= true;
5141c6fdbd8SKent Overstreet 
5151c6fdbd8SKent Overstreet 	mutex_init(&c->state_lock);
5161c6fdbd8SKent Overstreet 	mutex_init(&c->sb_lock);
5171c6fdbd8SKent Overstreet 	mutex_init(&c->replicas_gc_lock);
5181c6fdbd8SKent Overstreet 	mutex_init(&c->btree_root_lock);
5191c6fdbd8SKent Overstreet 	INIT_WORK(&c->read_only_work, bch2_fs_read_only_work);
5201c6fdbd8SKent Overstreet 
5211c6fdbd8SKent Overstreet 	init_rwsem(&c->gc_lock);
5221c6fdbd8SKent Overstreet 
5231c6fdbd8SKent Overstreet 	for (i = 0; i < BCH_TIME_STAT_NR; i++)
5241c6fdbd8SKent Overstreet 		bch2_time_stats_init(&c->times[i]);
5251c6fdbd8SKent Overstreet 
5261c6fdbd8SKent Overstreet 	bch2_fs_allocator_init(c);
5271c6fdbd8SKent Overstreet 	bch2_fs_rebalance_init(c);
5281c6fdbd8SKent Overstreet 	bch2_fs_quota_init(c);
5291c6fdbd8SKent Overstreet 
5301c6fdbd8SKent Overstreet 	INIT_LIST_HEAD(&c->list);
5311c6fdbd8SKent Overstreet 
5321c6fdbd8SKent Overstreet 	INIT_LIST_HEAD(&c->btree_interior_update_list);
5331c6fdbd8SKent Overstreet 	mutex_init(&c->btree_reserve_cache_lock);
5341c6fdbd8SKent Overstreet 	mutex_init(&c->btree_interior_update_lock);
5351c6fdbd8SKent Overstreet 
5361c6fdbd8SKent Overstreet 	mutex_init(&c->bio_bounce_pages_lock);
5371c6fdbd8SKent Overstreet 
5381c6fdbd8SKent Overstreet 	bio_list_init(&c->btree_write_error_list);
5391c6fdbd8SKent Overstreet 	spin_lock_init(&c->btree_write_error_lock);
5401c6fdbd8SKent Overstreet 	INIT_WORK(&c->btree_write_error_work, bch2_btree_write_error_work);
5411c6fdbd8SKent Overstreet 
5421c6fdbd8SKent Overstreet 	INIT_LIST_HEAD(&c->fsck_errors);
5431c6fdbd8SKent Overstreet 	mutex_init(&c->fsck_error_lock);
5441c6fdbd8SKent Overstreet 
5451c6fdbd8SKent Overstreet 	seqcount_init(&c->gc_pos_lock);
5461c6fdbd8SKent Overstreet 
5471c6fdbd8SKent Overstreet 	c->copy_gc_enabled		= 1;
5481c6fdbd8SKent Overstreet 	c->rebalance.enabled		= 1;
5491c6fdbd8SKent Overstreet 	c->promote_whole_extents	= true;
5501c6fdbd8SKent Overstreet 
5511c6fdbd8SKent Overstreet 	c->journal.write_time	= &c->times[BCH_TIME_journal_write];
5521c6fdbd8SKent Overstreet 	c->journal.delay_time	= &c->times[BCH_TIME_journal_delay];
5531c6fdbd8SKent Overstreet 	c->journal.blocked_time	= &c->times[BCH_TIME_journal_blocked];
5541c6fdbd8SKent Overstreet 	c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq];
5551c6fdbd8SKent Overstreet 
5561c6fdbd8SKent Overstreet 	bch2_fs_btree_cache_init_early(&c->btree_cache);
5571c6fdbd8SKent Overstreet 
5581c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
5591c6fdbd8SKent Overstreet 
5601c6fdbd8SKent Overstreet 	if (bch2_sb_to_fs(c, sb)) {
5611c6fdbd8SKent Overstreet 		mutex_unlock(&c->sb_lock);
5621c6fdbd8SKent Overstreet 		goto err;
5631c6fdbd8SKent Overstreet 	}
5641c6fdbd8SKent Overstreet 
5651c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
5661c6fdbd8SKent Overstreet 
5671c6fdbd8SKent Overstreet 	scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid);
5681c6fdbd8SKent Overstreet 
5691c6fdbd8SKent Overstreet 	c->opts = bch2_opts_default;
5701c6fdbd8SKent Overstreet 	bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb));
5711c6fdbd8SKent Overstreet 	bch2_opts_apply(&c->opts, opts);
5721c6fdbd8SKent Overstreet 
5731c6fdbd8SKent Overstreet 	c->block_bits		= ilog2(c->opts.block_size);
5741c6fdbd8SKent Overstreet 	c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
5751c6fdbd8SKent Overstreet 
5761c6fdbd8SKent Overstreet 	c->opts.nochanges	|= c->opts.noreplay;
5771c6fdbd8SKent Overstreet 	c->opts.read_only	|= c->opts.nochanges;
5781c6fdbd8SKent Overstreet 
5791c6fdbd8SKent Overstreet 	if (bch2_fs_init_fault("fs_alloc"))
5801c6fdbd8SKent Overstreet 		goto err;
5811c6fdbd8SKent Overstreet 
5821c6fdbd8SKent Overstreet 	iter_size = sizeof(struct btree_node_iter_large) +
5831c6fdbd8SKent Overstreet 		(btree_blocks(c) + 1) * 2 *
5841c6fdbd8SKent Overstreet 		sizeof(struct btree_node_iter_set);
5851c6fdbd8SKent Overstreet 
5861c6fdbd8SKent Overstreet 	if (!(c->wq = alloc_workqueue("bcachefs",
5871c6fdbd8SKent Overstreet 				WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
5881c6fdbd8SKent Overstreet 	    !(c->copygc_wq = alloc_workqueue("bcache_copygc",
5891c6fdbd8SKent Overstreet 				WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) ||
5901c6fdbd8SKent Overstreet 	    percpu_ref_init(&c->writes, bch2_writes_disabled, 0, GFP_KERNEL) ||
5911c6fdbd8SKent Overstreet 	    mempool_init_kmalloc_pool(&c->btree_reserve_pool, 1,
5921c6fdbd8SKent Overstreet 				      sizeof(struct btree_reserve)) ||
5931c6fdbd8SKent Overstreet 	    mempool_init_kmalloc_pool(&c->btree_interior_update_pool, 1,
5941c6fdbd8SKent Overstreet 				      sizeof(struct btree_update)) ||
5951c6fdbd8SKent Overstreet 	    mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
5961c6fdbd8SKent Overstreet 	    bioset_init(&c->btree_bio, 1,
5971c6fdbd8SKent Overstreet 			max(offsetof(struct btree_read_bio, bio),
5981c6fdbd8SKent Overstreet 			    offsetof(struct btree_write_bio, wbio.bio)),
5991c6fdbd8SKent Overstreet 			BIOSET_NEED_BVECS) ||
6001c6fdbd8SKent Overstreet 	    !(c->usage_percpu = alloc_percpu(struct bch_fs_usage)) ||
6011c6fdbd8SKent Overstreet 	    percpu_init_rwsem(&c->usage_lock) ||
6021c6fdbd8SKent Overstreet 	    mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1,
6031c6fdbd8SKent Overstreet 					btree_bytes(c)) ||
604*581edb63SKent Overstreet 	    mempool_init_kmalloc_pool(&c->btree_iters_pool, 1,
605*581edb63SKent Overstreet 			sizeof(struct btree_iter) * BTREE_ITER_MAX) ||
6061c6fdbd8SKent Overstreet 	    bch2_io_clock_init(&c->io_clock[READ]) ||
6071c6fdbd8SKent Overstreet 	    bch2_io_clock_init(&c->io_clock[WRITE]) ||
6081c6fdbd8SKent Overstreet 	    bch2_fs_journal_init(&c->journal) ||
6091c6fdbd8SKent Overstreet 	    bch2_fs_btree_cache_init(c) ||
6101c6fdbd8SKent Overstreet 	    bch2_fs_io_init(c) ||
6111c6fdbd8SKent Overstreet 	    bch2_fs_encryption_init(c) ||
6121c6fdbd8SKent Overstreet 	    bch2_fs_compress_init(c) ||
6131c6fdbd8SKent Overstreet 	    bch2_fs_fsio_init(c))
6141c6fdbd8SKent Overstreet 		goto err;
6151c6fdbd8SKent Overstreet 
6161c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
6171c6fdbd8SKent Overstreet 	for (i = 0; i < c->sb.nr_devices; i++)
6181c6fdbd8SKent Overstreet 		if (bch2_dev_exists(c->disk_sb.sb, mi, i) &&
6191c6fdbd8SKent Overstreet 		    bch2_dev_alloc(c, i))
6201c6fdbd8SKent Overstreet 			goto err;
6211c6fdbd8SKent Overstreet 
6221c6fdbd8SKent Overstreet 	/*
6231c6fdbd8SKent Overstreet 	 * Now that all allocations have succeeded, init various refcounty
6241c6fdbd8SKent Overstreet 	 * things that let us shutdown:
6251c6fdbd8SKent Overstreet 	 */
6261c6fdbd8SKent Overstreet 	closure_init(&c->cl, NULL);
6271c6fdbd8SKent Overstreet 
6281c6fdbd8SKent Overstreet 	c->kobj.kset = bcachefs_kset;
6291c6fdbd8SKent Overstreet 	kobject_init(&c->kobj, &bch2_fs_ktype);
6301c6fdbd8SKent Overstreet 	kobject_init(&c->internal, &bch2_fs_internal_ktype);
6311c6fdbd8SKent Overstreet 	kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype);
6321c6fdbd8SKent Overstreet 	kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype);
6331c6fdbd8SKent Overstreet 
6341c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
6351c6fdbd8SKent Overstreet 	err = bch2_fs_online(c);
6361c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
6371c6fdbd8SKent Overstreet 	if (err) {
6381c6fdbd8SKent Overstreet 		bch_err(c, "bch2_fs_online() error: %s", err);
6391c6fdbd8SKent Overstreet 		goto err;
6401c6fdbd8SKent Overstreet 	}
6411c6fdbd8SKent Overstreet out:
6421c6fdbd8SKent Overstreet 	pr_verbose_init(opts, "ret %i", c ? 0 : -ENOMEM);
6431c6fdbd8SKent Overstreet 	return c;
6441c6fdbd8SKent Overstreet err:
6451c6fdbd8SKent Overstreet 	bch2_fs_free(c);
6461c6fdbd8SKent Overstreet 	c = NULL;
6471c6fdbd8SKent Overstreet 	goto out;
6481c6fdbd8SKent Overstreet }
6491c6fdbd8SKent Overstreet 
6501c6fdbd8SKent Overstreet const char *bch2_fs_start(struct bch_fs *c)
6511c6fdbd8SKent Overstreet {
6521c6fdbd8SKent Overstreet 	const char *err = "cannot allocate memory";
6531c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
6541c6fdbd8SKent Overstreet 	struct bch_dev *ca;
6551c6fdbd8SKent Overstreet 	time64_t now = ktime_get_seconds();
6561c6fdbd8SKent Overstreet 	unsigned i;
6571c6fdbd8SKent Overstreet 	int ret = -EINVAL;
6581c6fdbd8SKent Overstreet 
6591c6fdbd8SKent Overstreet 	mutex_lock(&c->state_lock);
6601c6fdbd8SKent Overstreet 
6611c6fdbd8SKent Overstreet 	BUG_ON(c->state != BCH_FS_STARTING);
6621c6fdbd8SKent Overstreet 
6631c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
6641c6fdbd8SKent Overstreet 
6651c6fdbd8SKent Overstreet 	for_each_online_member(ca, c, i)
6661c6fdbd8SKent Overstreet 		bch2_sb_from_fs(c, ca);
6671c6fdbd8SKent Overstreet 
6681c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
6691c6fdbd8SKent Overstreet 	for_each_online_member(ca, c, i)
6701c6fdbd8SKent Overstreet 		mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
6711c6fdbd8SKent Overstreet 
6721c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
6731c6fdbd8SKent Overstreet 
6741c6fdbd8SKent Overstreet 	for_each_rw_member(ca, c, i)
6751c6fdbd8SKent Overstreet 		bch2_dev_allocator_add(c, ca);
6761c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
6771c6fdbd8SKent Overstreet 
6781c6fdbd8SKent Overstreet 	ret = BCH_SB_INITIALIZED(c->disk_sb.sb)
6791c6fdbd8SKent Overstreet 		? bch2_fs_recovery(c)
6801c6fdbd8SKent Overstreet 		: bch2_fs_initialize(c);
6811c6fdbd8SKent Overstreet 	if (ret)
6821c6fdbd8SKent Overstreet 		goto err;
6831c6fdbd8SKent Overstreet 
6841c6fdbd8SKent Overstreet 	err = "dynamic fault";
6851c6fdbd8SKent Overstreet 	if (bch2_fs_init_fault("fs_start"))
6861c6fdbd8SKent Overstreet 		goto err;
6871c6fdbd8SKent Overstreet 
6881c6fdbd8SKent Overstreet 	if (c->opts.read_only) {
6891c6fdbd8SKent Overstreet 		bch2_fs_read_only(c);
6901c6fdbd8SKent Overstreet 	} else {
6911c6fdbd8SKent Overstreet 		err = bch2_fs_read_write(c);
6921c6fdbd8SKent Overstreet 		if (err)
6931c6fdbd8SKent Overstreet 			goto err;
6941c6fdbd8SKent Overstreet 	}
6951c6fdbd8SKent Overstreet 
6961c6fdbd8SKent Overstreet 	set_bit(BCH_FS_STARTED, &c->flags);
6971c6fdbd8SKent Overstreet 
6981c6fdbd8SKent Overstreet 	err = NULL;
6991c6fdbd8SKent Overstreet out:
7001c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
7011c6fdbd8SKent Overstreet 	return err;
7021c6fdbd8SKent Overstreet err:
7031c6fdbd8SKent Overstreet 	switch (ret) {
7041c6fdbd8SKent Overstreet 	case BCH_FSCK_ERRORS_NOT_FIXED:
7051c6fdbd8SKent Overstreet 		bch_err(c, "filesystem contains errors: please report this to the developers");
7061c6fdbd8SKent Overstreet 		pr_cont("mount with -o fix_errors to repair\n");
7071c6fdbd8SKent Overstreet 		err = "fsck error";
7081c6fdbd8SKent Overstreet 		break;
7091c6fdbd8SKent Overstreet 	case BCH_FSCK_REPAIR_UNIMPLEMENTED:
7101c6fdbd8SKent Overstreet 		bch_err(c, "filesystem contains errors: please report this to the developers");
7111c6fdbd8SKent Overstreet 		pr_cont("repair unimplemented: inform the developers so that it can be added\n");
7121c6fdbd8SKent Overstreet 		err = "fsck error";
7131c6fdbd8SKent Overstreet 		break;
7141c6fdbd8SKent Overstreet 	case BCH_FSCK_REPAIR_IMPOSSIBLE:
7151c6fdbd8SKent Overstreet 		bch_err(c, "filesystem contains errors, but repair impossible");
7161c6fdbd8SKent Overstreet 		err = "fsck error";
7171c6fdbd8SKent Overstreet 		break;
7181c6fdbd8SKent Overstreet 	case BCH_FSCK_UNKNOWN_VERSION:
7191c6fdbd8SKent Overstreet 		err = "unknown metadata version";;
7201c6fdbd8SKent Overstreet 		break;
7211c6fdbd8SKent Overstreet 	case -ENOMEM:
7221c6fdbd8SKent Overstreet 		err = "cannot allocate memory";
7231c6fdbd8SKent Overstreet 		break;
7241c6fdbd8SKent Overstreet 	case -EIO:
7251c6fdbd8SKent Overstreet 		err = "IO error";
7261c6fdbd8SKent Overstreet 		break;
7271c6fdbd8SKent Overstreet 	}
7281c6fdbd8SKent Overstreet 
7291c6fdbd8SKent Overstreet 	BUG_ON(!err);
7301c6fdbd8SKent Overstreet 	set_bit(BCH_FS_ERROR, &c->flags);
7311c6fdbd8SKent Overstreet 	goto out;
7321c6fdbd8SKent Overstreet }
7331c6fdbd8SKent Overstreet 
7341c6fdbd8SKent Overstreet static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
7351c6fdbd8SKent Overstreet {
7361c6fdbd8SKent Overstreet 	struct bch_sb_field_members *sb_mi;
7371c6fdbd8SKent Overstreet 
7381c6fdbd8SKent Overstreet 	sb_mi = bch2_sb_get_members(sb);
7391c6fdbd8SKent Overstreet 	if (!sb_mi)
7401c6fdbd8SKent Overstreet 		return "Invalid superblock: member info area missing";
7411c6fdbd8SKent Overstreet 
7421c6fdbd8SKent Overstreet 	if (le16_to_cpu(sb->block_size) != c->opts.block_size)
7431c6fdbd8SKent Overstreet 		return "mismatched block size";
7441c6fdbd8SKent Overstreet 
7451c6fdbd8SKent Overstreet 	if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
7461c6fdbd8SKent Overstreet 	    BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
7471c6fdbd8SKent Overstreet 		return "new cache bucket size is too small";
7481c6fdbd8SKent Overstreet 
7491c6fdbd8SKent Overstreet 	return NULL;
7501c6fdbd8SKent Overstreet }
7511c6fdbd8SKent Overstreet 
7521c6fdbd8SKent Overstreet static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
7531c6fdbd8SKent Overstreet {
7541c6fdbd8SKent Overstreet 	struct bch_sb *newest =
7551c6fdbd8SKent Overstreet 		le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
7561c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
7571c6fdbd8SKent Overstreet 
7581c6fdbd8SKent Overstreet 	if (!uuid_equal(&fs->uuid, &sb->uuid))
7591c6fdbd8SKent Overstreet 		return "device not a member of filesystem";
7601c6fdbd8SKent Overstreet 
7611c6fdbd8SKent Overstreet 	if (!bch2_dev_exists(newest, mi, sb->dev_idx))
7621c6fdbd8SKent Overstreet 		return "device has been removed";
7631c6fdbd8SKent Overstreet 
7641c6fdbd8SKent Overstreet 	if (fs->block_size != sb->block_size)
7651c6fdbd8SKent Overstreet 		return "mismatched block size";
7661c6fdbd8SKent Overstreet 
7671c6fdbd8SKent Overstreet 	return NULL;
7681c6fdbd8SKent Overstreet }
7691c6fdbd8SKent Overstreet 
7701c6fdbd8SKent Overstreet /* Device startup/shutdown: */
7711c6fdbd8SKent Overstreet 
7721c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *kobj)
7731c6fdbd8SKent Overstreet {
7741c6fdbd8SKent Overstreet 	struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
7751c6fdbd8SKent Overstreet 
7761c6fdbd8SKent Overstreet 	kfree(ca);
7771c6fdbd8SKent Overstreet }
7781c6fdbd8SKent Overstreet 
7791c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *ca)
7801c6fdbd8SKent Overstreet {
7811c6fdbd8SKent Overstreet 	cancel_work_sync(&ca->io_error_work);
7821c6fdbd8SKent Overstreet 
7831c6fdbd8SKent Overstreet 	if (ca->kobj.state_in_sysfs &&
7841c6fdbd8SKent Overstreet 	    ca->disk_sb.bdev)
7851c6fdbd8SKent Overstreet 		sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
7861c6fdbd8SKent Overstreet 
7871c6fdbd8SKent Overstreet 	if (ca->kobj.state_in_sysfs)
7881c6fdbd8SKent Overstreet 		kobject_del(&ca->kobj);
7891c6fdbd8SKent Overstreet 
7901c6fdbd8SKent Overstreet 	bch2_free_super(&ca->disk_sb);
7911c6fdbd8SKent Overstreet 	bch2_dev_journal_exit(ca);
7921c6fdbd8SKent Overstreet 
7931c6fdbd8SKent Overstreet 	free_percpu(ca->io_done);
7941c6fdbd8SKent Overstreet 	bioset_exit(&ca->replica_set);
7951c6fdbd8SKent Overstreet 	bch2_dev_buckets_free(ca);
7961c6fdbd8SKent Overstreet 
7971c6fdbd8SKent Overstreet 	bch2_time_stats_exit(&ca->io_latency[WRITE]);
7981c6fdbd8SKent Overstreet 	bch2_time_stats_exit(&ca->io_latency[READ]);
7991c6fdbd8SKent Overstreet 
8001c6fdbd8SKent Overstreet 	percpu_ref_exit(&ca->io_ref);
8011c6fdbd8SKent Overstreet 	percpu_ref_exit(&ca->ref);
8021c6fdbd8SKent Overstreet 	kobject_put(&ca->kobj);
8031c6fdbd8SKent Overstreet }
8041c6fdbd8SKent Overstreet 
8051c6fdbd8SKent Overstreet static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca)
8061c6fdbd8SKent Overstreet {
8071c6fdbd8SKent Overstreet 
8081c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
8091c6fdbd8SKent Overstreet 
8101c6fdbd8SKent Overstreet 	if (percpu_ref_is_zero(&ca->io_ref))
8111c6fdbd8SKent Overstreet 		return;
8121c6fdbd8SKent Overstreet 
8131c6fdbd8SKent Overstreet 	__bch2_dev_read_only(c, ca);
8141c6fdbd8SKent Overstreet 
8151c6fdbd8SKent Overstreet 	reinit_completion(&ca->io_ref_completion);
8161c6fdbd8SKent Overstreet 	percpu_ref_kill(&ca->io_ref);
8171c6fdbd8SKent Overstreet 	wait_for_completion(&ca->io_ref_completion);
8181c6fdbd8SKent Overstreet 
8191c6fdbd8SKent Overstreet 	if (ca->kobj.state_in_sysfs) {
8201c6fdbd8SKent Overstreet 		sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
8211c6fdbd8SKent Overstreet 		sysfs_remove_link(&ca->kobj, "block");
8221c6fdbd8SKent Overstreet 	}
8231c6fdbd8SKent Overstreet 
8241c6fdbd8SKent Overstreet 	bch2_free_super(&ca->disk_sb);
8251c6fdbd8SKent Overstreet 	bch2_dev_journal_exit(ca);
8261c6fdbd8SKent Overstreet }
8271c6fdbd8SKent Overstreet 
8281c6fdbd8SKent Overstreet static void bch2_dev_ref_complete(struct percpu_ref *ref)
8291c6fdbd8SKent Overstreet {
8301c6fdbd8SKent Overstreet 	struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
8311c6fdbd8SKent Overstreet 
8321c6fdbd8SKent Overstreet 	complete(&ca->ref_completion);
8331c6fdbd8SKent Overstreet }
8341c6fdbd8SKent Overstreet 
8351c6fdbd8SKent Overstreet static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
8361c6fdbd8SKent Overstreet {
8371c6fdbd8SKent Overstreet 	struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
8381c6fdbd8SKent Overstreet 
8391c6fdbd8SKent Overstreet 	complete(&ca->io_ref_completion);
8401c6fdbd8SKent Overstreet }
8411c6fdbd8SKent Overstreet 
8421c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca)
8431c6fdbd8SKent Overstreet {
8441c6fdbd8SKent Overstreet 	int ret;
8451c6fdbd8SKent Overstreet 
8461c6fdbd8SKent Overstreet 	if (!c->kobj.state_in_sysfs)
8471c6fdbd8SKent Overstreet 		return 0;
8481c6fdbd8SKent Overstreet 
8491c6fdbd8SKent Overstreet 	if (!ca->kobj.state_in_sysfs) {
8501c6fdbd8SKent Overstreet 		ret = kobject_add(&ca->kobj, &c->kobj,
8511c6fdbd8SKent Overstreet 				  "dev-%u", ca->dev_idx);
8521c6fdbd8SKent Overstreet 		if (ret)
8531c6fdbd8SKent Overstreet 			return ret;
8541c6fdbd8SKent Overstreet 	}
8551c6fdbd8SKent Overstreet 
8561c6fdbd8SKent Overstreet 	if (ca->disk_sb.bdev) {
8571c6fdbd8SKent Overstreet 		struct kobject *block = bdev_kobj(ca->disk_sb.bdev);
8581c6fdbd8SKent Overstreet 
8591c6fdbd8SKent Overstreet 		ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
8601c6fdbd8SKent Overstreet 		if (ret)
8611c6fdbd8SKent Overstreet 			return ret;
8621c6fdbd8SKent Overstreet 
8631c6fdbd8SKent Overstreet 		ret = sysfs_create_link(&ca->kobj, block, "block");
8641c6fdbd8SKent Overstreet 		if (ret)
8651c6fdbd8SKent Overstreet 			return ret;
8661c6fdbd8SKent Overstreet 	}
8671c6fdbd8SKent Overstreet 
8681c6fdbd8SKent Overstreet 	return 0;
8691c6fdbd8SKent Overstreet }
8701c6fdbd8SKent Overstreet 
8711c6fdbd8SKent Overstreet static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c,
8721c6fdbd8SKent Overstreet 					struct bch_member *member)
8731c6fdbd8SKent Overstreet {
8741c6fdbd8SKent Overstreet 	struct bch_dev *ca;
8751c6fdbd8SKent Overstreet 
8761c6fdbd8SKent Overstreet 	ca = kzalloc(sizeof(*ca), GFP_KERNEL);
8771c6fdbd8SKent Overstreet 	if (!ca)
8781c6fdbd8SKent Overstreet 		return NULL;
8791c6fdbd8SKent Overstreet 
8801c6fdbd8SKent Overstreet 	kobject_init(&ca->kobj, &bch2_dev_ktype);
8811c6fdbd8SKent Overstreet 	init_completion(&ca->ref_completion);
8821c6fdbd8SKent Overstreet 	init_completion(&ca->io_ref_completion);
8831c6fdbd8SKent Overstreet 
8841c6fdbd8SKent Overstreet 	init_rwsem(&ca->bucket_lock);
8851c6fdbd8SKent Overstreet 
8861c6fdbd8SKent Overstreet 	writepoint_init(&ca->copygc_write_point, BCH_DATA_USER);
8871c6fdbd8SKent Overstreet 
8881c6fdbd8SKent Overstreet 	spin_lock_init(&ca->freelist_lock);
8891c6fdbd8SKent Overstreet 	bch2_dev_copygc_init(ca);
8901c6fdbd8SKent Overstreet 
8911c6fdbd8SKent Overstreet 	INIT_WORK(&ca->io_error_work, bch2_io_error_work);
8921c6fdbd8SKent Overstreet 
8931c6fdbd8SKent Overstreet 	bch2_time_stats_init(&ca->io_latency[READ]);
8941c6fdbd8SKent Overstreet 	bch2_time_stats_init(&ca->io_latency[WRITE]);
8951c6fdbd8SKent Overstreet 
8961c6fdbd8SKent Overstreet 	ca->mi = bch2_mi_to_cpu(member);
8971c6fdbd8SKent Overstreet 	ca->uuid = member->uuid;
8981c6fdbd8SKent Overstreet 
8991c6fdbd8SKent Overstreet 	if (opt_defined(c->opts, discard))
9001c6fdbd8SKent Overstreet 		ca->mi.discard = opt_get(c->opts, discard);
9011c6fdbd8SKent Overstreet 
9021c6fdbd8SKent Overstreet 	if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
9031c6fdbd8SKent Overstreet 			    0, GFP_KERNEL) ||
9041c6fdbd8SKent Overstreet 	    percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
9051c6fdbd8SKent Overstreet 			    PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
9061c6fdbd8SKent Overstreet 	    bch2_dev_buckets_alloc(c, ca) ||
9071c6fdbd8SKent Overstreet 	    bioset_init(&ca->replica_set, 4,
9081c6fdbd8SKent Overstreet 			offsetof(struct bch_write_bio, bio), 0) ||
9091c6fdbd8SKent Overstreet 	    !(ca->io_done	= alloc_percpu(*ca->io_done)))
9101c6fdbd8SKent Overstreet 		goto err;
9111c6fdbd8SKent Overstreet 
9121c6fdbd8SKent Overstreet 	return ca;
9131c6fdbd8SKent Overstreet err:
9141c6fdbd8SKent Overstreet 	bch2_dev_free(ca);
9151c6fdbd8SKent Overstreet 	return NULL;
9161c6fdbd8SKent Overstreet }
9171c6fdbd8SKent Overstreet 
9181c6fdbd8SKent Overstreet static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca,
9191c6fdbd8SKent Overstreet 			    unsigned dev_idx)
9201c6fdbd8SKent Overstreet {
9211c6fdbd8SKent Overstreet 	ca->dev_idx = dev_idx;
9221c6fdbd8SKent Overstreet 	__set_bit(ca->dev_idx, ca->self.d);
9231c6fdbd8SKent Overstreet 	scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
9241c6fdbd8SKent Overstreet 
9251c6fdbd8SKent Overstreet 	ca->fs = c;
9261c6fdbd8SKent Overstreet 	rcu_assign_pointer(c->devs[ca->dev_idx], ca);
9271c6fdbd8SKent Overstreet 
9281c6fdbd8SKent Overstreet 	if (bch2_dev_sysfs_online(c, ca))
9291c6fdbd8SKent Overstreet 		pr_warn("error creating sysfs objects");
9301c6fdbd8SKent Overstreet }
9311c6fdbd8SKent Overstreet 
9321c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
9331c6fdbd8SKent Overstreet {
9341c6fdbd8SKent Overstreet 	struct bch_member *member =
9351c6fdbd8SKent Overstreet 		bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx;
9361c6fdbd8SKent Overstreet 	struct bch_dev *ca = NULL;
9371c6fdbd8SKent Overstreet 	int ret = 0;
9381c6fdbd8SKent Overstreet 
9391c6fdbd8SKent Overstreet 	pr_verbose_init(c->opts, "");
9401c6fdbd8SKent Overstreet 
9411c6fdbd8SKent Overstreet 	if (bch2_fs_init_fault("dev_alloc"))
9421c6fdbd8SKent Overstreet 		goto err;
9431c6fdbd8SKent Overstreet 
9441c6fdbd8SKent Overstreet 	ca = __bch2_dev_alloc(c, member);
9451c6fdbd8SKent Overstreet 	if (!ca)
9461c6fdbd8SKent Overstreet 		goto err;
9471c6fdbd8SKent Overstreet 
9481c6fdbd8SKent Overstreet 	bch2_dev_attach(c, ca, dev_idx);
9491c6fdbd8SKent Overstreet out:
9501c6fdbd8SKent Overstreet 	pr_verbose_init(c->opts, "ret %i", ret);
9511c6fdbd8SKent Overstreet 	return ret;
9521c6fdbd8SKent Overstreet err:
9531c6fdbd8SKent Overstreet 	if (ca)
9541c6fdbd8SKent Overstreet 		bch2_dev_free(ca);
9551c6fdbd8SKent Overstreet 	ret = -ENOMEM;
9561c6fdbd8SKent Overstreet 	goto out;
9571c6fdbd8SKent Overstreet }
9581c6fdbd8SKent Overstreet 
9591c6fdbd8SKent Overstreet static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
9601c6fdbd8SKent Overstreet {
9611c6fdbd8SKent Overstreet 	unsigned ret;
9621c6fdbd8SKent Overstreet 
9631c6fdbd8SKent Overstreet 	if (bch2_dev_is_online(ca)) {
9641c6fdbd8SKent Overstreet 		bch_err(ca, "already have device online in slot %u",
9651c6fdbd8SKent Overstreet 			sb->sb->dev_idx);
9661c6fdbd8SKent Overstreet 		return -EINVAL;
9671c6fdbd8SKent Overstreet 	}
9681c6fdbd8SKent Overstreet 
9691c6fdbd8SKent Overstreet 	if (get_capacity(sb->bdev->bd_disk) <
9701c6fdbd8SKent Overstreet 	    ca->mi.bucket_size * ca->mi.nbuckets) {
9711c6fdbd8SKent Overstreet 		bch_err(ca, "cannot online: device too small");
9721c6fdbd8SKent Overstreet 		return -EINVAL;
9731c6fdbd8SKent Overstreet 	}
9741c6fdbd8SKent Overstreet 
9751c6fdbd8SKent Overstreet 	BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
9761c6fdbd8SKent Overstreet 
9771c6fdbd8SKent Overstreet 	if (get_capacity(sb->bdev->bd_disk) <
9781c6fdbd8SKent Overstreet 	    ca->mi.bucket_size * ca->mi.nbuckets) {
9791c6fdbd8SKent Overstreet 		bch_err(ca, "device too small");
9801c6fdbd8SKent Overstreet 		return -EINVAL;
9811c6fdbd8SKent Overstreet 	}
9821c6fdbd8SKent Overstreet 
9831c6fdbd8SKent Overstreet 	ret = bch2_dev_journal_init(ca, sb->sb);
9841c6fdbd8SKent Overstreet 	if (ret)
9851c6fdbd8SKent Overstreet 		return ret;
9861c6fdbd8SKent Overstreet 
9871c6fdbd8SKent Overstreet 	/* Commit: */
9881c6fdbd8SKent Overstreet 	ca->disk_sb = *sb;
9891c6fdbd8SKent Overstreet 	memset(sb, 0, sizeof(*sb));
9901c6fdbd8SKent Overstreet 
9911c6fdbd8SKent Overstreet 	percpu_ref_reinit(&ca->io_ref);
9921c6fdbd8SKent Overstreet 
9931c6fdbd8SKent Overstreet 	return 0;
9941c6fdbd8SKent Overstreet }
9951c6fdbd8SKent Overstreet 
9961c6fdbd8SKent Overstreet static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb)
9971c6fdbd8SKent Overstreet {
9981c6fdbd8SKent Overstreet 	struct bch_dev *ca;
9991c6fdbd8SKent Overstreet 	int ret;
10001c6fdbd8SKent Overstreet 
10011c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
10021c6fdbd8SKent Overstreet 
10031c6fdbd8SKent Overstreet 	if (le64_to_cpu(sb->sb->seq) >
10041c6fdbd8SKent Overstreet 	    le64_to_cpu(c->disk_sb.sb->seq))
10051c6fdbd8SKent Overstreet 		bch2_sb_to_fs(c, sb->sb);
10061c6fdbd8SKent Overstreet 
10071c6fdbd8SKent Overstreet 	BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
10081c6fdbd8SKent Overstreet 	       !c->devs[sb->sb->dev_idx]);
10091c6fdbd8SKent Overstreet 
10101c6fdbd8SKent Overstreet 	ca = bch_dev_locked(c, sb->sb->dev_idx);
10111c6fdbd8SKent Overstreet 
10121c6fdbd8SKent Overstreet 	ret = __bch2_dev_attach_bdev(ca, sb);
10131c6fdbd8SKent Overstreet 	if (ret)
10141c6fdbd8SKent Overstreet 		return ret;
10151c6fdbd8SKent Overstreet 
10166eac2c2eSKent Overstreet 	mutex_lock(&c->sb_lock);
10176eac2c2eSKent Overstreet 	bch2_mark_dev_superblock(ca->fs, ca,
10186eac2c2eSKent Overstreet 			BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE);
10196eac2c2eSKent Overstreet 	mutex_unlock(&c->sb_lock);
10206eac2c2eSKent Overstreet 
10211c6fdbd8SKent Overstreet 	bch2_dev_sysfs_online(c, ca);
10221c6fdbd8SKent Overstreet 
10231c6fdbd8SKent Overstreet 	if (c->sb.nr_devices == 1)
10241c6fdbd8SKent Overstreet 		snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev);
10251c6fdbd8SKent Overstreet 	snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev);
10261c6fdbd8SKent Overstreet 
10271c6fdbd8SKent Overstreet 	rebalance_wakeup(c);
10281c6fdbd8SKent Overstreet 	return 0;
10291c6fdbd8SKent Overstreet }
10301c6fdbd8SKent Overstreet 
10311c6fdbd8SKent Overstreet /* Device management: */
10321c6fdbd8SKent Overstreet 
10331c6fdbd8SKent Overstreet /*
10341c6fdbd8SKent Overstreet  * Note: this function is also used by the error paths - when a particular
10351c6fdbd8SKent Overstreet  * device sees an error, we call it to determine whether we can just set the
10361c6fdbd8SKent Overstreet  * device RO, or - if this function returns false - we'll set the whole
10371c6fdbd8SKent Overstreet  * filesystem RO:
10381c6fdbd8SKent Overstreet  *
10391c6fdbd8SKent Overstreet  * XXX: maybe we should be more explicit about whether we're changing state
10401c6fdbd8SKent Overstreet  * because we got an error or what have you?
10411c6fdbd8SKent Overstreet  */
10421c6fdbd8SKent Overstreet bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
10431c6fdbd8SKent Overstreet 			    enum bch_member_state new_state, int flags)
10441c6fdbd8SKent Overstreet {
10451c6fdbd8SKent Overstreet 	struct bch_devs_mask new_online_devs;
10461c6fdbd8SKent Overstreet 	struct replicas_status s;
10471c6fdbd8SKent Overstreet 	struct bch_dev *ca2;
10481c6fdbd8SKent Overstreet 	int i, nr_rw = 0, required;
10491c6fdbd8SKent Overstreet 
10501c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
10511c6fdbd8SKent Overstreet 
10521c6fdbd8SKent Overstreet 	switch (new_state) {
10531c6fdbd8SKent Overstreet 	case BCH_MEMBER_STATE_RW:
10541c6fdbd8SKent Overstreet 		return true;
10551c6fdbd8SKent Overstreet 	case BCH_MEMBER_STATE_RO:
10561c6fdbd8SKent Overstreet 		if (ca->mi.state != BCH_MEMBER_STATE_RW)
10571c6fdbd8SKent Overstreet 			return true;
10581c6fdbd8SKent Overstreet 
10591c6fdbd8SKent Overstreet 		/* do we have enough devices to write to?  */
10601c6fdbd8SKent Overstreet 		for_each_member_device(ca2, c, i)
10611c6fdbd8SKent Overstreet 			if (ca2 != ca)
10621c6fdbd8SKent Overstreet 				nr_rw += ca2->mi.state == BCH_MEMBER_STATE_RW;
10631c6fdbd8SKent Overstreet 
10641c6fdbd8SKent Overstreet 		required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
10651c6fdbd8SKent Overstreet 			       ? c->opts.metadata_replicas
10661c6fdbd8SKent Overstreet 			       : c->opts.metadata_replicas_required,
10671c6fdbd8SKent Overstreet 			       !(flags & BCH_FORCE_IF_DATA_DEGRADED)
10681c6fdbd8SKent Overstreet 			       ? c->opts.data_replicas
10691c6fdbd8SKent Overstreet 			       : c->opts.data_replicas_required);
10701c6fdbd8SKent Overstreet 
10711c6fdbd8SKent Overstreet 		return nr_rw >= required;
10721c6fdbd8SKent Overstreet 	case BCH_MEMBER_STATE_FAILED:
10731c6fdbd8SKent Overstreet 	case BCH_MEMBER_STATE_SPARE:
10741c6fdbd8SKent Overstreet 		if (ca->mi.state != BCH_MEMBER_STATE_RW &&
10751c6fdbd8SKent Overstreet 		    ca->mi.state != BCH_MEMBER_STATE_RO)
10761c6fdbd8SKent Overstreet 			return true;
10771c6fdbd8SKent Overstreet 
10781c6fdbd8SKent Overstreet 		/* do we have enough devices to read from?  */
10791c6fdbd8SKent Overstreet 		new_online_devs = bch2_online_devs(c);
10801c6fdbd8SKent Overstreet 		__clear_bit(ca->dev_idx, new_online_devs.d);
10811c6fdbd8SKent Overstreet 
10821c6fdbd8SKent Overstreet 		s = __bch2_replicas_status(c, new_online_devs);
10831c6fdbd8SKent Overstreet 
10841c6fdbd8SKent Overstreet 		return bch2_have_enough_devs(s, flags);
10851c6fdbd8SKent Overstreet 	default:
10861c6fdbd8SKent Overstreet 		BUG();
10871c6fdbd8SKent Overstreet 	}
10881c6fdbd8SKent Overstreet }
10891c6fdbd8SKent Overstreet 
10901c6fdbd8SKent Overstreet static bool bch2_fs_may_start(struct bch_fs *c)
10911c6fdbd8SKent Overstreet {
10921c6fdbd8SKent Overstreet 	struct replicas_status s;
10931c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
10941c6fdbd8SKent Overstreet 	struct bch_dev *ca;
10951c6fdbd8SKent Overstreet 	unsigned i, flags = c->opts.degraded
10961c6fdbd8SKent Overstreet 		? BCH_FORCE_IF_DEGRADED
10971c6fdbd8SKent Overstreet 		: 0;
10981c6fdbd8SKent Overstreet 
10991c6fdbd8SKent Overstreet 	if (!c->opts.degraded) {
11001c6fdbd8SKent Overstreet 		mutex_lock(&c->sb_lock);
11011c6fdbd8SKent Overstreet 		mi = bch2_sb_get_members(c->disk_sb.sb);
11021c6fdbd8SKent Overstreet 
11031c6fdbd8SKent Overstreet 		for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
11041c6fdbd8SKent Overstreet 			if (!bch2_dev_exists(c->disk_sb.sb, mi, i))
11051c6fdbd8SKent Overstreet 				continue;
11061c6fdbd8SKent Overstreet 
11071c6fdbd8SKent Overstreet 			ca = bch_dev_locked(c, i);
11081c6fdbd8SKent Overstreet 
11091c6fdbd8SKent Overstreet 			if (!bch2_dev_is_online(ca) &&
11101c6fdbd8SKent Overstreet 			    (ca->mi.state == BCH_MEMBER_STATE_RW ||
11111c6fdbd8SKent Overstreet 			     ca->mi.state == BCH_MEMBER_STATE_RO)) {
11121c6fdbd8SKent Overstreet 				mutex_unlock(&c->sb_lock);
11131c6fdbd8SKent Overstreet 				return false;
11141c6fdbd8SKent Overstreet 			}
11151c6fdbd8SKent Overstreet 		}
11161c6fdbd8SKent Overstreet 		mutex_unlock(&c->sb_lock);
11171c6fdbd8SKent Overstreet 	}
11181c6fdbd8SKent Overstreet 
11191c6fdbd8SKent Overstreet 	s = bch2_replicas_status(c);
11201c6fdbd8SKent Overstreet 
11211c6fdbd8SKent Overstreet 	return bch2_have_enough_devs(s, flags);
11221c6fdbd8SKent Overstreet }
11231c6fdbd8SKent Overstreet 
11241c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
11251c6fdbd8SKent Overstreet {
11261c6fdbd8SKent Overstreet 	bch2_copygc_stop(ca);
11271c6fdbd8SKent Overstreet 
11281c6fdbd8SKent Overstreet 	/*
11291c6fdbd8SKent Overstreet 	 * The allocator thread itself allocates btree nodes, so stop it first:
11301c6fdbd8SKent Overstreet 	 */
11311c6fdbd8SKent Overstreet 	bch2_dev_allocator_stop(ca);
11321c6fdbd8SKent Overstreet 	bch2_dev_allocator_remove(c, ca);
11331c6fdbd8SKent Overstreet 	bch2_dev_journal_stop(&c->journal, ca);
11341c6fdbd8SKent Overstreet }
11351c6fdbd8SKent Overstreet 
11361c6fdbd8SKent Overstreet static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
11371c6fdbd8SKent Overstreet {
11381c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
11391c6fdbd8SKent Overstreet 
11401c6fdbd8SKent Overstreet 	BUG_ON(ca->mi.state != BCH_MEMBER_STATE_RW);
11411c6fdbd8SKent Overstreet 
11421c6fdbd8SKent Overstreet 	bch2_dev_allocator_add(c, ca);
11431c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
11441c6fdbd8SKent Overstreet 
11451c6fdbd8SKent Overstreet 	if (bch2_dev_allocator_start(ca))
11461c6fdbd8SKent Overstreet 		return "error starting allocator thread";
11471c6fdbd8SKent Overstreet 
11481c6fdbd8SKent Overstreet 	if (bch2_copygc_start(c, ca))
11491c6fdbd8SKent Overstreet 		return "error starting copygc thread";
11501c6fdbd8SKent Overstreet 
11511c6fdbd8SKent Overstreet 	return NULL;
11521c6fdbd8SKent Overstreet }
11531c6fdbd8SKent Overstreet 
11541c6fdbd8SKent Overstreet int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
11551c6fdbd8SKent Overstreet 			 enum bch_member_state new_state, int flags)
11561c6fdbd8SKent Overstreet {
11571c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
11581c6fdbd8SKent Overstreet 	int ret = 0;
11591c6fdbd8SKent Overstreet 
11601c6fdbd8SKent Overstreet 	if (ca->mi.state == new_state)
11611c6fdbd8SKent Overstreet 		return 0;
11621c6fdbd8SKent Overstreet 
11631c6fdbd8SKent Overstreet 	if (!bch2_dev_state_allowed(c, ca, new_state, flags))
11641c6fdbd8SKent Overstreet 		return -EINVAL;
11651c6fdbd8SKent Overstreet 
11661c6fdbd8SKent Overstreet 	if (new_state != BCH_MEMBER_STATE_RW)
11671c6fdbd8SKent Overstreet 		__bch2_dev_read_only(c, ca);
11681c6fdbd8SKent Overstreet 
11691c6fdbd8SKent Overstreet 	bch_notice(ca, "%s", bch2_dev_state[new_state]);
11701c6fdbd8SKent Overstreet 
11711c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
11721c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
11731c6fdbd8SKent Overstreet 	SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
11741c6fdbd8SKent Overstreet 	bch2_write_super(c);
11751c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
11761c6fdbd8SKent Overstreet 
11771c6fdbd8SKent Overstreet 	if (new_state == BCH_MEMBER_STATE_RW &&
11781c6fdbd8SKent Overstreet 	    __bch2_dev_read_write(c, ca))
11791c6fdbd8SKent Overstreet 		ret = -ENOMEM;
11801c6fdbd8SKent Overstreet 
11811c6fdbd8SKent Overstreet 	rebalance_wakeup(c);
11821c6fdbd8SKent Overstreet 
11831c6fdbd8SKent Overstreet 	return ret;
11841c6fdbd8SKent Overstreet }
11851c6fdbd8SKent Overstreet 
11861c6fdbd8SKent Overstreet int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
11871c6fdbd8SKent Overstreet 		       enum bch_member_state new_state, int flags)
11881c6fdbd8SKent Overstreet {
11891c6fdbd8SKent Overstreet 	int ret;
11901c6fdbd8SKent Overstreet 
11911c6fdbd8SKent Overstreet 	mutex_lock(&c->state_lock);
11921c6fdbd8SKent Overstreet 	ret = __bch2_dev_set_state(c, ca, new_state, flags);
11931c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
11941c6fdbd8SKent Overstreet 
11951c6fdbd8SKent Overstreet 	return ret;
11961c6fdbd8SKent Overstreet }
11971c6fdbd8SKent Overstreet 
11981c6fdbd8SKent Overstreet /* Device add/removal: */
11991c6fdbd8SKent Overstreet 
12001c6fdbd8SKent Overstreet int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
12011c6fdbd8SKent Overstreet {
12021c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
12031c6fdbd8SKent Overstreet 	unsigned dev_idx = ca->dev_idx, data;
12041c6fdbd8SKent Overstreet 	int ret = -EINVAL;
12051c6fdbd8SKent Overstreet 
12061c6fdbd8SKent Overstreet 	mutex_lock(&c->state_lock);
12071c6fdbd8SKent Overstreet 
12081c6fdbd8SKent Overstreet 	percpu_ref_put(&ca->ref); /* XXX */
12091c6fdbd8SKent Overstreet 
12101c6fdbd8SKent Overstreet 	if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
12111c6fdbd8SKent Overstreet 		bch_err(ca, "Cannot remove without losing data");
12121c6fdbd8SKent Overstreet 		goto err;
12131c6fdbd8SKent Overstreet 	}
12141c6fdbd8SKent Overstreet 
12151c6fdbd8SKent Overstreet 	__bch2_dev_read_only(c, ca);
12161c6fdbd8SKent Overstreet 
12171c6fdbd8SKent Overstreet 	/*
12181c6fdbd8SKent Overstreet 	 * XXX: verify that dev_idx is really not in use anymore, anywhere
12191c6fdbd8SKent Overstreet 	 *
12201c6fdbd8SKent Overstreet 	 * flag_data_bad() does not check btree pointers
12211c6fdbd8SKent Overstreet 	 */
12221c6fdbd8SKent Overstreet 	ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
12231c6fdbd8SKent Overstreet 	if (ret) {
12241c6fdbd8SKent Overstreet 		bch_err(ca, "Remove failed: error %i dropping data", ret);
12251c6fdbd8SKent Overstreet 		goto err;
12261c6fdbd8SKent Overstreet 	}
12271c6fdbd8SKent Overstreet 
12281c6fdbd8SKent Overstreet 	ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx);
12291c6fdbd8SKent Overstreet 	if (ret) {
12301c6fdbd8SKent Overstreet 		bch_err(ca, "Remove failed: error %i flushing journal", ret);
12311c6fdbd8SKent Overstreet 		goto err;
12321c6fdbd8SKent Overstreet 	}
12331c6fdbd8SKent Overstreet 
12341c6fdbd8SKent Overstreet 	data = bch2_dev_has_data(c, ca);
12351c6fdbd8SKent Overstreet 	if (data) {
12361c6fdbd8SKent Overstreet 		char data_has_str[100];
12371c6fdbd8SKent Overstreet 		bch2_scnprint_flag_list(data_has_str,
12381c6fdbd8SKent Overstreet 					sizeof(data_has_str),
12391c6fdbd8SKent Overstreet 					bch2_data_types,
12401c6fdbd8SKent Overstreet 					data);
12411c6fdbd8SKent Overstreet 		bch_err(ca, "Remove failed, still has data (%s)", data_has_str);
12421c6fdbd8SKent Overstreet 		ret = -EBUSY;
12431c6fdbd8SKent Overstreet 		goto err;
12441c6fdbd8SKent Overstreet 	}
12451c6fdbd8SKent Overstreet 
12461c6fdbd8SKent Overstreet 	ret = bch2_btree_delete_range(c, BTREE_ID_ALLOC,
12471c6fdbd8SKent Overstreet 				      POS(ca->dev_idx, 0),
12481c6fdbd8SKent Overstreet 				      POS(ca->dev_idx + 1, 0),
12491c6fdbd8SKent Overstreet 				      ZERO_VERSION,
12501c6fdbd8SKent Overstreet 				      NULL, NULL, NULL);
12511c6fdbd8SKent Overstreet 	if (ret) {
12521c6fdbd8SKent Overstreet 		bch_err(ca, "Remove failed, error deleting alloc info");
12531c6fdbd8SKent Overstreet 		goto err;
12541c6fdbd8SKent Overstreet 	}
12551c6fdbd8SKent Overstreet 
12561c6fdbd8SKent Overstreet 	/*
12571c6fdbd8SKent Overstreet 	 * must flush all existing journal entries, they might have
12581c6fdbd8SKent Overstreet 	 * (overwritten) keys that point to the device we're removing:
12591c6fdbd8SKent Overstreet 	 */
12601c6fdbd8SKent Overstreet 	bch2_journal_flush_all_pins(&c->journal);
12611c6fdbd8SKent Overstreet 	ret = bch2_journal_error(&c->journal);
12621c6fdbd8SKent Overstreet 	if (ret) {
12631c6fdbd8SKent Overstreet 		bch_err(ca, "Remove failed, journal error");
12641c6fdbd8SKent Overstreet 		goto err;
12651c6fdbd8SKent Overstreet 	}
12661c6fdbd8SKent Overstreet 
12671c6fdbd8SKent Overstreet 	__bch2_dev_offline(c, ca);
12681c6fdbd8SKent Overstreet 
12691c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
12701c6fdbd8SKent Overstreet 	rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
12711c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
12721c6fdbd8SKent Overstreet 
12731c6fdbd8SKent Overstreet 	percpu_ref_kill(&ca->ref);
12741c6fdbd8SKent Overstreet 	wait_for_completion(&ca->ref_completion);
12751c6fdbd8SKent Overstreet 
12761c6fdbd8SKent Overstreet 	bch2_dev_free(ca);
12771c6fdbd8SKent Overstreet 
12781c6fdbd8SKent Overstreet 	/*
12791c6fdbd8SKent Overstreet 	 * Free this device's slot in the bch_member array - all pointers to
12801c6fdbd8SKent Overstreet 	 * this device must be gone:
12811c6fdbd8SKent Overstreet 	 */
12821c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
12831c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
12841c6fdbd8SKent Overstreet 	memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
12851c6fdbd8SKent Overstreet 
12861c6fdbd8SKent Overstreet 	bch2_write_super(c);
12871c6fdbd8SKent Overstreet 
12881c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
12891c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
12901c6fdbd8SKent Overstreet 	return 0;
12911c6fdbd8SKent Overstreet err:
12921c6fdbd8SKent Overstreet 	if (ca->mi.state == BCH_MEMBER_STATE_RW)
12931c6fdbd8SKent Overstreet 		__bch2_dev_read_write(c, ca);
12941c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
12951c6fdbd8SKent Overstreet 	return ret;
12961c6fdbd8SKent Overstreet }
12971c6fdbd8SKent Overstreet 
12986eac2c2eSKent Overstreet static void dev_usage_clear(struct bch_dev *ca)
12996eac2c2eSKent Overstreet {
13006eac2c2eSKent Overstreet 	struct bucket_array *buckets;
13016eac2c2eSKent Overstreet 	int cpu;
13026eac2c2eSKent Overstreet 
13036eac2c2eSKent Overstreet 	for_each_possible_cpu(cpu) {
13046eac2c2eSKent Overstreet 		struct bch_dev_usage *p =
13056eac2c2eSKent Overstreet 			per_cpu_ptr(ca->usage_percpu, cpu);
13066eac2c2eSKent Overstreet 		memset(p, 0, sizeof(*p));
13076eac2c2eSKent Overstreet 	}
13086eac2c2eSKent Overstreet 
13096eac2c2eSKent Overstreet 	down_read(&ca->bucket_lock);
13106eac2c2eSKent Overstreet 	buckets = bucket_array(ca);
13116eac2c2eSKent Overstreet 
13126eac2c2eSKent Overstreet 	memset(buckets->b, 0, sizeof(buckets->b[0]) * buckets->nbuckets);
13136eac2c2eSKent Overstreet 	up_read(&ca->bucket_lock);
13146eac2c2eSKent Overstreet }
13156eac2c2eSKent Overstreet 
13161c6fdbd8SKent Overstreet /* Add new device to running filesystem: */
13171c6fdbd8SKent Overstreet int bch2_dev_add(struct bch_fs *c, const char *path)
13181c6fdbd8SKent Overstreet {
13191c6fdbd8SKent Overstreet 	struct bch_opts opts = bch2_opts_empty();
13201c6fdbd8SKent Overstreet 	struct bch_sb_handle sb;
13211c6fdbd8SKent Overstreet 	const char *err;
13221c6fdbd8SKent Overstreet 	struct bch_dev *ca = NULL;
13231c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
13241c6fdbd8SKent Overstreet 	struct bch_member dev_mi;
13251c6fdbd8SKent Overstreet 	unsigned dev_idx, nr_devices, u64s;
13261c6fdbd8SKent Overstreet 	int ret;
13271c6fdbd8SKent Overstreet 
13281c6fdbd8SKent Overstreet 	ret = bch2_read_super(path, &opts, &sb);
13291c6fdbd8SKent Overstreet 	if (ret)
13301c6fdbd8SKent Overstreet 		return ret;
13311c6fdbd8SKent Overstreet 
13321c6fdbd8SKent Overstreet 	err = bch2_sb_validate(&sb);
13331c6fdbd8SKent Overstreet 	if (err)
13341c6fdbd8SKent Overstreet 		return -EINVAL;
13351c6fdbd8SKent Overstreet 
13361c6fdbd8SKent Overstreet 	dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx];
13371c6fdbd8SKent Overstreet 
13381c6fdbd8SKent Overstreet 	err = bch2_dev_may_add(sb.sb, c);
13391c6fdbd8SKent Overstreet 	if (err)
13401c6fdbd8SKent Overstreet 		return -EINVAL;
13411c6fdbd8SKent Overstreet 
13421c6fdbd8SKent Overstreet 	ca = __bch2_dev_alloc(c, &dev_mi);
13431c6fdbd8SKent Overstreet 	if (!ca) {
13441c6fdbd8SKent Overstreet 		bch2_free_super(&sb);
13451c6fdbd8SKent Overstreet 		return -ENOMEM;
13461c6fdbd8SKent Overstreet 	}
13471c6fdbd8SKent Overstreet 
13481c6fdbd8SKent Overstreet 	ret = __bch2_dev_attach_bdev(ca, &sb);
13491c6fdbd8SKent Overstreet 	if (ret) {
13501c6fdbd8SKent Overstreet 		bch2_dev_free(ca);
13511c6fdbd8SKent Overstreet 		return ret;
13521c6fdbd8SKent Overstreet 	}
13531c6fdbd8SKent Overstreet 
13546eac2c2eSKent Overstreet 	/*
13556eac2c2eSKent Overstreet 	 * We want to allocate journal on the new device before adding the new
13566eac2c2eSKent Overstreet 	 * device to the filesystem because allocating after we attach requires
13576eac2c2eSKent Overstreet 	 * spinning up the allocator thread, and the allocator thread requires
13586eac2c2eSKent Overstreet 	 * doing btree writes, which if the existing devices are RO isn't going
13596eac2c2eSKent Overstreet 	 * to work
13606eac2c2eSKent Overstreet 	 *
13616eac2c2eSKent Overstreet 	 * So we have to mark where the superblocks are, but marking allocated
13626eac2c2eSKent Overstreet 	 * data normally updates the filesystem usage too, so we have to mark,
13636eac2c2eSKent Overstreet 	 * allocate the journal, reset all the marks, then remark after we
13646eac2c2eSKent Overstreet 	 * attach...
13656eac2c2eSKent Overstreet 	 */
13666eac2c2eSKent Overstreet 	bch2_mark_dev_superblock(ca->fs, ca,
13676eac2c2eSKent Overstreet 			BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE);
13686eac2c2eSKent Overstreet 
13691c6fdbd8SKent Overstreet 	err = "journal alloc failed";
13701c6fdbd8SKent Overstreet 	ret = bch2_dev_journal_alloc(ca);
13711c6fdbd8SKent Overstreet 	if (ret)
13721c6fdbd8SKent Overstreet 		goto err;
13731c6fdbd8SKent Overstreet 
13746eac2c2eSKent Overstreet 	dev_usage_clear(ca);
13756eac2c2eSKent Overstreet 
13761c6fdbd8SKent Overstreet 	mutex_lock(&c->state_lock);
13771c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
13781c6fdbd8SKent Overstreet 
13791c6fdbd8SKent Overstreet 	err = "insufficient space in new superblock";
13801c6fdbd8SKent Overstreet 	ret = bch2_sb_from_fs(c, ca);
13811c6fdbd8SKent Overstreet 	if (ret)
13821c6fdbd8SKent Overstreet 		goto err_unlock;
13831c6fdbd8SKent Overstreet 
13841c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(ca->disk_sb.sb);
13851c6fdbd8SKent Overstreet 
13861c6fdbd8SKent Overstreet 	if (!bch2_sb_resize_members(&ca->disk_sb,
13871c6fdbd8SKent Overstreet 				le32_to_cpu(mi->field.u64s) +
13881c6fdbd8SKent Overstreet 				sizeof(dev_mi) / sizeof(u64))) {
13891c6fdbd8SKent Overstreet 		ret = -ENOSPC;
13901c6fdbd8SKent Overstreet 		goto err_unlock;
13911c6fdbd8SKent Overstreet 	}
13921c6fdbd8SKent Overstreet 
13931c6fdbd8SKent Overstreet 	if (dynamic_fault("bcachefs:add:no_slot"))
13941c6fdbd8SKent Overstreet 		goto no_slot;
13951c6fdbd8SKent Overstreet 
13961c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
13971c6fdbd8SKent Overstreet 	for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
13981c6fdbd8SKent Overstreet 		if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx))
13991c6fdbd8SKent Overstreet 			goto have_slot;
14001c6fdbd8SKent Overstreet no_slot:
14011c6fdbd8SKent Overstreet 	err = "no slots available in superblock";
14021c6fdbd8SKent Overstreet 	ret = -ENOSPC;
14031c6fdbd8SKent Overstreet 	goto err_unlock;
14041c6fdbd8SKent Overstreet 
14051c6fdbd8SKent Overstreet have_slot:
14061c6fdbd8SKent Overstreet 	nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
14071c6fdbd8SKent Overstreet 	u64s = (sizeof(struct bch_sb_field_members) +
14081c6fdbd8SKent Overstreet 		sizeof(struct bch_member) * nr_devices) / sizeof(u64);
14091c6fdbd8SKent Overstreet 
14101c6fdbd8SKent Overstreet 	err = "no space in superblock for member info";
14111c6fdbd8SKent Overstreet 	ret = -ENOSPC;
14121c6fdbd8SKent Overstreet 
14131c6fdbd8SKent Overstreet 	mi = bch2_sb_resize_members(&c->disk_sb, u64s);
14141c6fdbd8SKent Overstreet 	if (!mi)
14151c6fdbd8SKent Overstreet 		goto err_unlock;
14161c6fdbd8SKent Overstreet 
14171c6fdbd8SKent Overstreet 	/* success: */
14181c6fdbd8SKent Overstreet 
14191c6fdbd8SKent Overstreet 	mi->members[dev_idx] = dev_mi;
14201c6fdbd8SKent Overstreet 	mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_seconds());
14211c6fdbd8SKent Overstreet 	c->disk_sb.sb->nr_devices	= nr_devices;
14221c6fdbd8SKent Overstreet 
14231c6fdbd8SKent Overstreet 	ca->disk_sb.sb->dev_idx	= dev_idx;
14241c6fdbd8SKent Overstreet 	bch2_dev_attach(c, ca, dev_idx);
14251c6fdbd8SKent Overstreet 
14266eac2c2eSKent Overstreet 	bch2_mark_dev_superblock(c, ca,
14276eac2c2eSKent Overstreet 			BCH_BUCKET_MARK_MAY_MAKE_UNAVAILABLE);
14286eac2c2eSKent Overstreet 
14291c6fdbd8SKent Overstreet 	bch2_write_super(c);
14301c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
14311c6fdbd8SKent Overstreet 
14321c6fdbd8SKent Overstreet 	if (ca->mi.state == BCH_MEMBER_STATE_RW) {
14331c6fdbd8SKent Overstreet 		err = __bch2_dev_read_write(c, ca);
14341c6fdbd8SKent Overstreet 		if (err)
14351c6fdbd8SKent Overstreet 			goto err_late;
14361c6fdbd8SKent Overstreet 	}
14371c6fdbd8SKent Overstreet 
14381c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
14391c6fdbd8SKent Overstreet 	return 0;
14401c6fdbd8SKent Overstreet 
14411c6fdbd8SKent Overstreet err_unlock:
14421c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
14431c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
14441c6fdbd8SKent Overstreet err:
14451c6fdbd8SKent Overstreet 	if (ca)
14461c6fdbd8SKent Overstreet 		bch2_dev_free(ca);
14471c6fdbd8SKent Overstreet 	bch2_free_super(&sb);
14481c6fdbd8SKent Overstreet 	bch_err(c, "Unable to add device: %s", err);
14491c6fdbd8SKent Overstreet 	return ret;
14501c6fdbd8SKent Overstreet err_late:
14511c6fdbd8SKent Overstreet 	bch_err(c, "Error going rw after adding device: %s", err);
14521c6fdbd8SKent Overstreet 	return -EINVAL;
14531c6fdbd8SKent Overstreet }
14541c6fdbd8SKent Overstreet 
14551c6fdbd8SKent Overstreet /* Hot add existing device to running filesystem: */
14561c6fdbd8SKent Overstreet int bch2_dev_online(struct bch_fs *c, const char *path)
14571c6fdbd8SKent Overstreet {
14581c6fdbd8SKent Overstreet 	struct bch_opts opts = bch2_opts_empty();
14591c6fdbd8SKent Overstreet 	struct bch_sb_handle sb = { NULL };
14601c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
14611c6fdbd8SKent Overstreet 	struct bch_dev *ca;
14621c6fdbd8SKent Overstreet 	unsigned dev_idx;
14631c6fdbd8SKent Overstreet 	const char *err;
14641c6fdbd8SKent Overstreet 	int ret;
14651c6fdbd8SKent Overstreet 
14661c6fdbd8SKent Overstreet 	mutex_lock(&c->state_lock);
14671c6fdbd8SKent Overstreet 
14681c6fdbd8SKent Overstreet 	ret = bch2_read_super(path, &opts, &sb);
14691c6fdbd8SKent Overstreet 	if (ret) {
14701c6fdbd8SKent Overstreet 		mutex_unlock(&c->state_lock);
14711c6fdbd8SKent Overstreet 		return ret;
14721c6fdbd8SKent Overstreet 	}
14731c6fdbd8SKent Overstreet 
14741c6fdbd8SKent Overstreet 	dev_idx = sb.sb->dev_idx;
14751c6fdbd8SKent Overstreet 
14761c6fdbd8SKent Overstreet 	err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
14771c6fdbd8SKent Overstreet 	if (err)
14781c6fdbd8SKent Overstreet 		goto err;
14791c6fdbd8SKent Overstreet 
14801c6fdbd8SKent Overstreet 	if (bch2_dev_attach_bdev(c, &sb)) {
14811c6fdbd8SKent Overstreet 		err = "bch2_dev_attach_bdev() error";
14821c6fdbd8SKent Overstreet 		goto err;
14831c6fdbd8SKent Overstreet 	}
14841c6fdbd8SKent Overstreet 
14851c6fdbd8SKent Overstreet 	ca = bch_dev_locked(c, dev_idx);
14861c6fdbd8SKent Overstreet 	if (ca->mi.state == BCH_MEMBER_STATE_RW) {
14871c6fdbd8SKent Overstreet 		err = __bch2_dev_read_write(c, ca);
14881c6fdbd8SKent Overstreet 		if (err)
14891c6fdbd8SKent Overstreet 			goto err;
14901c6fdbd8SKent Overstreet 	}
14911c6fdbd8SKent Overstreet 
14921c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
14931c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
14941c6fdbd8SKent Overstreet 
14951c6fdbd8SKent Overstreet 	mi->members[ca->dev_idx].last_mount =
14961c6fdbd8SKent Overstreet 		cpu_to_le64(ktime_get_seconds());
14971c6fdbd8SKent Overstreet 
14981c6fdbd8SKent Overstreet 	bch2_write_super(c);
14991c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
15001c6fdbd8SKent Overstreet 
15011c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
15021c6fdbd8SKent Overstreet 	return 0;
15031c6fdbd8SKent Overstreet err:
15041c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
15051c6fdbd8SKent Overstreet 	bch2_free_super(&sb);
15061c6fdbd8SKent Overstreet 	bch_err(c, "error bringing %s online: %s", path, err);
15071c6fdbd8SKent Overstreet 	return -EINVAL;
15081c6fdbd8SKent Overstreet }
15091c6fdbd8SKent Overstreet 
15101c6fdbd8SKent Overstreet int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
15111c6fdbd8SKent Overstreet {
15121c6fdbd8SKent Overstreet 	mutex_lock(&c->state_lock);
15131c6fdbd8SKent Overstreet 
15141c6fdbd8SKent Overstreet 	if (!bch2_dev_is_online(ca)) {
15151c6fdbd8SKent Overstreet 		bch_err(ca, "Already offline");
15161c6fdbd8SKent Overstreet 		mutex_unlock(&c->state_lock);
15171c6fdbd8SKent Overstreet 		return 0;
15181c6fdbd8SKent Overstreet 	}
15191c6fdbd8SKent Overstreet 
15201c6fdbd8SKent Overstreet 	if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) {
15211c6fdbd8SKent Overstreet 		bch_err(ca, "Cannot offline required disk");
15221c6fdbd8SKent Overstreet 		mutex_unlock(&c->state_lock);
15231c6fdbd8SKent Overstreet 		return -EINVAL;
15241c6fdbd8SKent Overstreet 	}
15251c6fdbd8SKent Overstreet 
15261c6fdbd8SKent Overstreet 	__bch2_dev_offline(c, ca);
15271c6fdbd8SKent Overstreet 
15281c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
15291c6fdbd8SKent Overstreet 	return 0;
15301c6fdbd8SKent Overstreet }
15311c6fdbd8SKent Overstreet 
15321c6fdbd8SKent Overstreet int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
15331c6fdbd8SKent Overstreet {
15341c6fdbd8SKent Overstreet 	struct bch_member *mi;
15351c6fdbd8SKent Overstreet 	int ret = 0;
15361c6fdbd8SKent Overstreet 
15371c6fdbd8SKent Overstreet 	mutex_lock(&c->state_lock);
15381c6fdbd8SKent Overstreet 
15391c6fdbd8SKent Overstreet 	if (nbuckets < ca->mi.nbuckets) {
15401c6fdbd8SKent Overstreet 		bch_err(ca, "Cannot shrink yet");
15411c6fdbd8SKent Overstreet 		ret = -EINVAL;
15421c6fdbd8SKent Overstreet 		goto err;
15431c6fdbd8SKent Overstreet 	}
15441c6fdbd8SKent Overstreet 
15451c6fdbd8SKent Overstreet 	if (bch2_dev_is_online(ca) &&
15461c6fdbd8SKent Overstreet 	    get_capacity(ca->disk_sb.bdev->bd_disk) <
15471c6fdbd8SKent Overstreet 	    ca->mi.bucket_size * nbuckets) {
15481c6fdbd8SKent Overstreet 		bch_err(ca, "New size larger than device");
15491c6fdbd8SKent Overstreet 		ret = -EINVAL;
15501c6fdbd8SKent Overstreet 		goto err;
15511c6fdbd8SKent Overstreet 	}
15521c6fdbd8SKent Overstreet 
15531c6fdbd8SKent Overstreet 	ret = bch2_dev_buckets_resize(c, ca, nbuckets);
15541c6fdbd8SKent Overstreet 	if (ret) {
15551c6fdbd8SKent Overstreet 		bch_err(ca, "Resize error: %i", ret);
15561c6fdbd8SKent Overstreet 		goto err;
15571c6fdbd8SKent Overstreet 	}
15581c6fdbd8SKent Overstreet 
15591c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
15601c6fdbd8SKent Overstreet 	mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
15611c6fdbd8SKent Overstreet 	mi->nbuckets = cpu_to_le64(nbuckets);
15621c6fdbd8SKent Overstreet 
15631c6fdbd8SKent Overstreet 	bch2_write_super(c);
15641c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
15651c6fdbd8SKent Overstreet 
15661c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
15671c6fdbd8SKent Overstreet err:
15681c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
15691c6fdbd8SKent Overstreet 	return ret;
15701c6fdbd8SKent Overstreet }
15711c6fdbd8SKent Overstreet 
15721c6fdbd8SKent Overstreet /* return with ref on ca->ref: */
15731c6fdbd8SKent Overstreet struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path)
15741c6fdbd8SKent Overstreet {
15751c6fdbd8SKent Overstreet 
15761c6fdbd8SKent Overstreet 	struct bch_dev *ca;
15771c6fdbd8SKent Overstreet 	dev_t dev;
15781c6fdbd8SKent Overstreet 	unsigned i;
15791c6fdbd8SKent Overstreet 	int ret;
15801c6fdbd8SKent Overstreet 
15811c6fdbd8SKent Overstreet 	ret = lookup_bdev(path, &dev);
15821c6fdbd8SKent Overstreet 	if (ret)
15831c6fdbd8SKent Overstreet 		return ERR_PTR(ret);
15841c6fdbd8SKent Overstreet 
15851c6fdbd8SKent Overstreet 	for_each_member_device(ca, c, i)
15861c6fdbd8SKent Overstreet 		if (ca->disk_sb.bdev->bd_dev == dev)
15871c6fdbd8SKent Overstreet 			goto found;
15881c6fdbd8SKent Overstreet 
15891c6fdbd8SKent Overstreet 	ca = ERR_PTR(-ENOENT);
15901c6fdbd8SKent Overstreet found:
15911c6fdbd8SKent Overstreet 	return ca;
15921c6fdbd8SKent Overstreet }
15931c6fdbd8SKent Overstreet 
15941c6fdbd8SKent Overstreet /* Filesystem open: */
15951c6fdbd8SKent Overstreet 
15961c6fdbd8SKent Overstreet struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
15971c6fdbd8SKent Overstreet 			    struct bch_opts opts)
15981c6fdbd8SKent Overstreet {
15991c6fdbd8SKent Overstreet 	struct bch_sb_handle *sb = NULL;
16001c6fdbd8SKent Overstreet 	struct bch_fs *c = NULL;
16011c6fdbd8SKent Overstreet 	unsigned i, best_sb = 0;
16021c6fdbd8SKent Overstreet 	const char *err;
16031c6fdbd8SKent Overstreet 	int ret = -ENOMEM;
16041c6fdbd8SKent Overstreet 
16051c6fdbd8SKent Overstreet 	pr_verbose_init(opts, "");
16061c6fdbd8SKent Overstreet 
16071c6fdbd8SKent Overstreet 	if (!nr_devices) {
16081c6fdbd8SKent Overstreet 		c = ERR_PTR(-EINVAL);
16091c6fdbd8SKent Overstreet 		goto out2;
16101c6fdbd8SKent Overstreet 	}
16111c6fdbd8SKent Overstreet 
16121c6fdbd8SKent Overstreet 	if (!try_module_get(THIS_MODULE)) {
16131c6fdbd8SKent Overstreet 		c = ERR_PTR(-ENODEV);
16141c6fdbd8SKent Overstreet 		goto out2;
16151c6fdbd8SKent Overstreet 	}
16161c6fdbd8SKent Overstreet 
16171c6fdbd8SKent Overstreet 	sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
16181c6fdbd8SKent Overstreet 	if (!sb)
16191c6fdbd8SKent Overstreet 		goto err;
16201c6fdbd8SKent Overstreet 
16211c6fdbd8SKent Overstreet 	for (i = 0; i < nr_devices; i++) {
16221c6fdbd8SKent Overstreet 		ret = bch2_read_super(devices[i], &opts, &sb[i]);
16231c6fdbd8SKent Overstreet 		if (ret)
16241c6fdbd8SKent Overstreet 			goto err;
16251c6fdbd8SKent Overstreet 
16261c6fdbd8SKent Overstreet 		err = bch2_sb_validate(&sb[i]);
16271c6fdbd8SKent Overstreet 		if (err)
16281c6fdbd8SKent Overstreet 			goto err_print;
16291c6fdbd8SKent Overstreet 	}
16301c6fdbd8SKent Overstreet 
16311c6fdbd8SKent Overstreet 	for (i = 1; i < nr_devices; i++)
16321c6fdbd8SKent Overstreet 		if (le64_to_cpu(sb[i].sb->seq) >
16331c6fdbd8SKent Overstreet 		    le64_to_cpu(sb[best_sb].sb->seq))
16341c6fdbd8SKent Overstreet 			best_sb = i;
16351c6fdbd8SKent Overstreet 
16361c6fdbd8SKent Overstreet 	for (i = 0; i < nr_devices; i++) {
16371c6fdbd8SKent Overstreet 		err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
16381c6fdbd8SKent Overstreet 		if (err)
16391c6fdbd8SKent Overstreet 			goto err_print;
16401c6fdbd8SKent Overstreet 	}
16411c6fdbd8SKent Overstreet 
16421c6fdbd8SKent Overstreet 	ret = -ENOMEM;
16431c6fdbd8SKent Overstreet 	c = bch2_fs_alloc(sb[best_sb].sb, opts);
16441c6fdbd8SKent Overstreet 	if (!c)
16451c6fdbd8SKent Overstreet 		goto err;
16461c6fdbd8SKent Overstreet 
16471c6fdbd8SKent Overstreet 	err = "bch2_dev_online() error";
16481c6fdbd8SKent Overstreet 	mutex_lock(&c->state_lock);
16491c6fdbd8SKent Overstreet 	for (i = 0; i < nr_devices; i++)
16501c6fdbd8SKent Overstreet 		if (bch2_dev_attach_bdev(c, &sb[i])) {
16511c6fdbd8SKent Overstreet 			mutex_unlock(&c->state_lock);
16521c6fdbd8SKent Overstreet 			goto err_print;
16531c6fdbd8SKent Overstreet 		}
16541c6fdbd8SKent Overstreet 	mutex_unlock(&c->state_lock);
16551c6fdbd8SKent Overstreet 
16561c6fdbd8SKent Overstreet 	err = "insufficient devices";
16571c6fdbd8SKent Overstreet 	if (!bch2_fs_may_start(c))
16581c6fdbd8SKent Overstreet 		goto err_print;
16591c6fdbd8SKent Overstreet 
16601c6fdbd8SKent Overstreet 	if (!c->opts.nostart) {
16611c6fdbd8SKent Overstreet 		err = bch2_fs_start(c);
16621c6fdbd8SKent Overstreet 		if (err)
16631c6fdbd8SKent Overstreet 			goto err_print;
16641c6fdbd8SKent Overstreet 	}
16651c6fdbd8SKent Overstreet out:
16661c6fdbd8SKent Overstreet 	kfree(sb);
16671c6fdbd8SKent Overstreet 	module_put(THIS_MODULE);
16681c6fdbd8SKent Overstreet out2:
16691c6fdbd8SKent Overstreet 	pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
16701c6fdbd8SKent Overstreet 	return c;
16711c6fdbd8SKent Overstreet err_print:
16721c6fdbd8SKent Overstreet 	pr_err("bch_fs_open err opening %s: %s",
16731c6fdbd8SKent Overstreet 	       devices[0], err);
16741c6fdbd8SKent Overstreet 	ret = -EINVAL;
16751c6fdbd8SKent Overstreet err:
16761c6fdbd8SKent Overstreet 	if (c)
16771c6fdbd8SKent Overstreet 		bch2_fs_stop(c);
16781c6fdbd8SKent Overstreet 	for (i = 0; i < nr_devices; i++)
16791c6fdbd8SKent Overstreet 		bch2_free_super(&sb[i]);
16801c6fdbd8SKent Overstreet 	c = ERR_PTR(ret);
16811c6fdbd8SKent Overstreet 	goto out;
16821c6fdbd8SKent Overstreet }
16831c6fdbd8SKent Overstreet 
16841c6fdbd8SKent Overstreet static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb,
16851c6fdbd8SKent Overstreet 					      struct bch_opts opts)
16861c6fdbd8SKent Overstreet {
16871c6fdbd8SKent Overstreet 	const char *err;
16881c6fdbd8SKent Overstreet 	struct bch_fs *c;
16891c6fdbd8SKent Overstreet 	bool allocated_fs = false;
16901c6fdbd8SKent Overstreet 
16911c6fdbd8SKent Overstreet 	err = bch2_sb_validate(sb);
16921c6fdbd8SKent Overstreet 	if (err)
16931c6fdbd8SKent Overstreet 		return err;
16941c6fdbd8SKent Overstreet 
16951c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
16961c6fdbd8SKent Overstreet 	c = __bch2_uuid_to_fs(sb->sb->uuid);
16971c6fdbd8SKent Overstreet 	if (c) {
16981c6fdbd8SKent Overstreet 		closure_get(&c->cl);
16991c6fdbd8SKent Overstreet 
17001c6fdbd8SKent Overstreet 		err = bch2_dev_in_fs(c->disk_sb.sb, sb->sb);
17011c6fdbd8SKent Overstreet 		if (err)
17021c6fdbd8SKent Overstreet 			goto err;
17031c6fdbd8SKent Overstreet 	} else {
17041c6fdbd8SKent Overstreet 		c = bch2_fs_alloc(sb->sb, opts);
17051c6fdbd8SKent Overstreet 		err = "cannot allocate memory";
17061c6fdbd8SKent Overstreet 		if (!c)
17071c6fdbd8SKent Overstreet 			goto err;
17081c6fdbd8SKent Overstreet 
17091c6fdbd8SKent Overstreet 		allocated_fs = true;
17101c6fdbd8SKent Overstreet 	}
17111c6fdbd8SKent Overstreet 
17121c6fdbd8SKent Overstreet 	err = "bch2_dev_online() error";
17131c6fdbd8SKent Overstreet 
17141c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
17151c6fdbd8SKent Overstreet 	if (bch2_dev_attach_bdev(c, sb)) {
17161c6fdbd8SKent Overstreet 		mutex_unlock(&c->sb_lock);
17171c6fdbd8SKent Overstreet 		goto err;
17181c6fdbd8SKent Overstreet 	}
17191c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
17201c6fdbd8SKent Overstreet 
17211c6fdbd8SKent Overstreet 	if (!c->opts.nostart && bch2_fs_may_start(c)) {
17221c6fdbd8SKent Overstreet 		err = bch2_fs_start(c);
17231c6fdbd8SKent Overstreet 		if (err)
17241c6fdbd8SKent Overstreet 			goto err;
17251c6fdbd8SKent Overstreet 	}
17261c6fdbd8SKent Overstreet 
17271c6fdbd8SKent Overstreet 	closure_put(&c->cl);
17281c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
17291c6fdbd8SKent Overstreet 
17301c6fdbd8SKent Overstreet 	return NULL;
17311c6fdbd8SKent Overstreet err:
17321c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
17331c6fdbd8SKent Overstreet 
17341c6fdbd8SKent Overstreet 	if (allocated_fs)
17351c6fdbd8SKent Overstreet 		bch2_fs_stop(c);
17361c6fdbd8SKent Overstreet 	else if (c)
17371c6fdbd8SKent Overstreet 		closure_put(&c->cl);
17381c6fdbd8SKent Overstreet 
17391c6fdbd8SKent Overstreet 	return err;
17401c6fdbd8SKent Overstreet }
17411c6fdbd8SKent Overstreet 
17421c6fdbd8SKent Overstreet const char *bch2_fs_open_incremental(const char *path)
17431c6fdbd8SKent Overstreet {
17441c6fdbd8SKent Overstreet 	struct bch_sb_handle sb;
17451c6fdbd8SKent Overstreet 	struct bch_opts opts = bch2_opts_empty();
17461c6fdbd8SKent Overstreet 	const char *err;
17471c6fdbd8SKent Overstreet 
17481c6fdbd8SKent Overstreet 	if (bch2_read_super(path, &opts, &sb))
17491c6fdbd8SKent Overstreet 		return "error reading superblock";
17501c6fdbd8SKent Overstreet 
17511c6fdbd8SKent Overstreet 	err = __bch2_fs_open_incremental(&sb, opts);
17521c6fdbd8SKent Overstreet 	bch2_free_super(&sb);
17531c6fdbd8SKent Overstreet 
17541c6fdbd8SKent Overstreet 	return err;
17551c6fdbd8SKent Overstreet }
17561c6fdbd8SKent Overstreet 
17571c6fdbd8SKent Overstreet /* Global interfaces/init */
17581c6fdbd8SKent Overstreet 
17591c6fdbd8SKent Overstreet static void bcachefs_exit(void)
17601c6fdbd8SKent Overstreet {
17611c6fdbd8SKent Overstreet 	bch2_debug_exit();
17621c6fdbd8SKent Overstreet 	bch2_vfs_exit();
17631c6fdbd8SKent Overstreet 	bch2_chardev_exit();
17641c6fdbd8SKent Overstreet 	if (bcachefs_kset)
17651c6fdbd8SKent Overstreet 		kset_unregister(bcachefs_kset);
17661c6fdbd8SKent Overstreet }
17671c6fdbd8SKent Overstreet 
17681c6fdbd8SKent Overstreet static int __init bcachefs_init(void)
17691c6fdbd8SKent Overstreet {
17701c6fdbd8SKent Overstreet 	bch2_bkey_pack_test();
17711c6fdbd8SKent Overstreet 	bch2_inode_pack_test();
17721c6fdbd8SKent Overstreet 
17731c6fdbd8SKent Overstreet 	if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
17741c6fdbd8SKent Overstreet 	    bch2_chardev_init() ||
17751c6fdbd8SKent Overstreet 	    bch2_vfs_init() ||
17761c6fdbd8SKent Overstreet 	    bch2_debug_init())
17771c6fdbd8SKent Overstreet 		goto err;
17781c6fdbd8SKent Overstreet 
17791c6fdbd8SKent Overstreet 	return 0;
17801c6fdbd8SKent Overstreet err:
17811c6fdbd8SKent Overstreet 	bcachefs_exit();
17821c6fdbd8SKent Overstreet 	return -ENOMEM;
17831c6fdbd8SKent Overstreet }
17841c6fdbd8SKent Overstreet 
17851c6fdbd8SKent Overstreet #define BCH_DEBUG_PARAM(name, description)			\
17861c6fdbd8SKent Overstreet 	bool bch2_##name;					\
17871c6fdbd8SKent Overstreet 	module_param_named(name, bch2_##name, bool, 0644);	\
17881c6fdbd8SKent Overstreet 	MODULE_PARM_DESC(name, description);
17891c6fdbd8SKent Overstreet BCH_DEBUG_PARAMS()
17901c6fdbd8SKent Overstreet #undef BCH_DEBUG_PARAM
17911c6fdbd8SKent Overstreet 
17921c6fdbd8SKent Overstreet unsigned bch2_metadata_version = BCH_SB_VERSION_MAX;
17931c6fdbd8SKent Overstreet module_param_named(version, bch2_metadata_version, uint, 0400);
17941c6fdbd8SKent Overstreet 
17951c6fdbd8SKent Overstreet module_exit(bcachefs_exit);
17961c6fdbd8SKent Overstreet module_init(bcachefs_init);
1797