xref: /linux/fs/bcachefs/super.c (revision 8dd6ed9451894e2168328e2203d227303b472ca3)
11c6fdbd8SKent Overstreet // SPDX-License-Identifier: GPL-2.0
21c6fdbd8SKent Overstreet /*
31c6fdbd8SKent Overstreet  * bcachefs setup/teardown code, and some metadata io - read a superblock and
41c6fdbd8SKent Overstreet  * figure out what to do with it.
51c6fdbd8SKent Overstreet  *
61c6fdbd8SKent Overstreet  * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com>
71c6fdbd8SKent Overstreet  * Copyright 2012 Google, Inc.
81c6fdbd8SKent Overstreet  */
91c6fdbd8SKent Overstreet 
101c6fdbd8SKent Overstreet #include "bcachefs.h"
117b3f84eaSKent Overstreet #include "alloc_background.h"
127b3f84eaSKent Overstreet #include "alloc_foreground.h"
135b8a9227SKent Overstreet #include "bkey_sort.h"
141c6fdbd8SKent Overstreet #include "btree_cache.h"
151c6fdbd8SKent Overstreet #include "btree_gc.h"
162ca88e5aSKent Overstreet #include "btree_key_cache.h"
171c6fdbd8SKent Overstreet #include "btree_update_interior.h"
181c6fdbd8SKent Overstreet #include "btree_io.h"
191c6fdbd8SKent Overstreet #include "chardev.h"
201c6fdbd8SKent Overstreet #include "checksum.h"
211c6fdbd8SKent Overstreet #include "clock.h"
221c6fdbd8SKent Overstreet #include "compress.h"
231c6fdbd8SKent Overstreet #include "debug.h"
241c6fdbd8SKent Overstreet #include "disk_groups.h"
25cd575ddfSKent Overstreet #include "ec.h"
261c6fdbd8SKent Overstreet #include "error.h"
271c6fdbd8SKent Overstreet #include "fs.h"
281c6fdbd8SKent Overstreet #include "fs-io.h"
291c6fdbd8SKent Overstreet #include "fsck.h"
301c6fdbd8SKent Overstreet #include "inode.h"
311c6fdbd8SKent Overstreet #include "io.h"
321c6fdbd8SKent Overstreet #include "journal.h"
331c6fdbd8SKent Overstreet #include "journal_reclaim.h"
341dd7f9d9SKent Overstreet #include "journal_seq_blacklist.h"
351c6fdbd8SKent Overstreet #include "move.h"
361c6fdbd8SKent Overstreet #include "migrate.h"
371c6fdbd8SKent Overstreet #include "movinggc.h"
381c6fdbd8SKent Overstreet #include "quota.h"
391c6fdbd8SKent Overstreet #include "rebalance.h"
401c6fdbd8SKent Overstreet #include "recovery.h"
411c6fdbd8SKent Overstreet #include "replicas.h"
421c6fdbd8SKent Overstreet #include "super.h"
431c6fdbd8SKent Overstreet #include "super-io.h"
441c6fdbd8SKent Overstreet #include "sysfs.h"
451c6fdbd8SKent Overstreet #include "trace.h"
461c6fdbd8SKent Overstreet 
471c6fdbd8SKent Overstreet #include <linux/backing-dev.h>
481c6fdbd8SKent Overstreet #include <linux/blkdev.h>
491c6fdbd8SKent Overstreet #include <linux/debugfs.h>
501c6fdbd8SKent Overstreet #include <linux/device.h>
511c6fdbd8SKent Overstreet #include <linux/idr.h>
521c6fdbd8SKent Overstreet #include <linux/module.h>
531c6fdbd8SKent Overstreet #include <linux/percpu.h>
541c6fdbd8SKent Overstreet #include <linux/random.h>
551c6fdbd8SKent Overstreet #include <linux/sysfs.h>
561c6fdbd8SKent Overstreet #include <crypto/hash.h>
571c6fdbd8SKent Overstreet 
581c6fdbd8SKent Overstreet MODULE_LICENSE("GPL");
591c6fdbd8SKent Overstreet MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>");
601c6fdbd8SKent Overstreet 
611c6fdbd8SKent Overstreet #define KTYPE(type)							\
621c6fdbd8SKent Overstreet static const struct attribute_group type ## _group = {			\
631c6fdbd8SKent Overstreet 	.attrs = type ## _files						\
641c6fdbd8SKent Overstreet };									\
651c6fdbd8SKent Overstreet 									\
661c6fdbd8SKent Overstreet static const struct attribute_group *type ## _groups[] = {		\
671c6fdbd8SKent Overstreet 	&type ## _group,						\
681c6fdbd8SKent Overstreet 	NULL								\
691c6fdbd8SKent Overstreet };									\
701c6fdbd8SKent Overstreet 									\
711c6fdbd8SKent Overstreet static const struct kobj_type type ## _ktype = {			\
721c6fdbd8SKent Overstreet 	.release	= type ## _release,				\
731c6fdbd8SKent Overstreet 	.sysfs_ops	= &type ## _sysfs_ops,				\
741c6fdbd8SKent Overstreet 	.default_groups = type ## _groups				\
751c6fdbd8SKent Overstreet }
761c6fdbd8SKent Overstreet 
771c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *);
781c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *);
791c6fdbd8SKent Overstreet 
801c6fdbd8SKent Overstreet static void bch2_fs_internal_release(struct kobject *k)
811c6fdbd8SKent Overstreet {
821c6fdbd8SKent Overstreet }
831c6fdbd8SKent Overstreet 
841c6fdbd8SKent Overstreet static void bch2_fs_opts_dir_release(struct kobject *k)
851c6fdbd8SKent Overstreet {
861c6fdbd8SKent Overstreet }
871c6fdbd8SKent Overstreet 
881c6fdbd8SKent Overstreet static void bch2_fs_time_stats_release(struct kobject *k)
891c6fdbd8SKent Overstreet {
901c6fdbd8SKent Overstreet }
911c6fdbd8SKent Overstreet 
921c6fdbd8SKent Overstreet KTYPE(bch2_fs);
931c6fdbd8SKent Overstreet KTYPE(bch2_fs_internal);
941c6fdbd8SKent Overstreet KTYPE(bch2_fs_opts_dir);
951c6fdbd8SKent Overstreet KTYPE(bch2_fs_time_stats);
961c6fdbd8SKent Overstreet KTYPE(bch2_dev);
971c6fdbd8SKent Overstreet 
981c6fdbd8SKent Overstreet static struct kset *bcachefs_kset;
991c6fdbd8SKent Overstreet static LIST_HEAD(bch_fs_list);
1001c6fdbd8SKent Overstreet static DEFINE_MUTEX(bch_fs_list_lock);
1011c6fdbd8SKent Overstreet 
1021c6fdbd8SKent Overstreet static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
1031c6fdbd8SKent Overstreet 
1041c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *);
1051c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *, unsigned);
1061c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *);
1071c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *);
1081c6fdbd8SKent Overstreet 
1091c6fdbd8SKent Overstreet struct bch_fs *bch2_dev_to_fs(dev_t dev)
1101c6fdbd8SKent Overstreet {
1111c6fdbd8SKent Overstreet 	struct bch_fs *c;
1121c6fdbd8SKent Overstreet 	struct bch_dev *ca;
1131c6fdbd8SKent Overstreet 	unsigned i;
1141c6fdbd8SKent Overstreet 
1151c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
1161c6fdbd8SKent Overstreet 	rcu_read_lock();
1171c6fdbd8SKent Overstreet 
1181c6fdbd8SKent Overstreet 	list_for_each_entry(c, &bch_fs_list, list)
1191c6fdbd8SKent Overstreet 		for_each_member_device_rcu(ca, c, i, NULL)
120ec4ab9d2SDan Robertson 			if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) {
1211c6fdbd8SKent Overstreet 				closure_get(&c->cl);
1221c6fdbd8SKent Overstreet 				goto found;
1231c6fdbd8SKent Overstreet 			}
1241c6fdbd8SKent Overstreet 	c = NULL;
1251c6fdbd8SKent Overstreet found:
1261c6fdbd8SKent Overstreet 	rcu_read_unlock();
1271c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
1281c6fdbd8SKent Overstreet 
1291c6fdbd8SKent Overstreet 	return c;
1301c6fdbd8SKent Overstreet }
1311c6fdbd8SKent Overstreet 
1321c6fdbd8SKent Overstreet static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid)
1331c6fdbd8SKent Overstreet {
1341c6fdbd8SKent Overstreet 	struct bch_fs *c;
1351c6fdbd8SKent Overstreet 
1361c6fdbd8SKent Overstreet 	lockdep_assert_held(&bch_fs_list_lock);
1371c6fdbd8SKent Overstreet 
1381c6fdbd8SKent Overstreet 	list_for_each_entry(c, &bch_fs_list, list)
1391c6fdbd8SKent Overstreet 		if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid)))
1401c6fdbd8SKent Overstreet 			return c;
1411c6fdbd8SKent Overstreet 
1421c6fdbd8SKent Overstreet 	return NULL;
1431c6fdbd8SKent Overstreet }
1441c6fdbd8SKent Overstreet 
1451c6fdbd8SKent Overstreet struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid)
1461c6fdbd8SKent Overstreet {
1471c6fdbd8SKent Overstreet 	struct bch_fs *c;
1481c6fdbd8SKent Overstreet 
1491c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
1501c6fdbd8SKent Overstreet 	c = __bch2_uuid_to_fs(uuid);
1511c6fdbd8SKent Overstreet 	if (c)
1521c6fdbd8SKent Overstreet 		closure_get(&c->cl);
1531c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
1541c6fdbd8SKent Overstreet 
1551c6fdbd8SKent Overstreet 	return c;
1561c6fdbd8SKent Overstreet }
1571c6fdbd8SKent Overstreet 
158180fb49dSKent Overstreet static void bch2_dev_usage_journal_reserve(struct bch_fs *c)
159180fb49dSKent Overstreet {
160180fb49dSKent Overstreet 	struct bch_dev *ca;
161180fb49dSKent Overstreet 	unsigned i, nr = 0, u64s =
1624b8f89afSKent Overstreet 		((sizeof(struct jset_entry_dev_usage) +
1634b8f89afSKent Overstreet 		  sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR)) /
1644b8f89afSKent Overstreet 		sizeof(u64);
165180fb49dSKent Overstreet 
166180fb49dSKent Overstreet 	rcu_read_lock();
167180fb49dSKent Overstreet 	for_each_member_device_rcu(ca, c, i, NULL)
168180fb49dSKent Overstreet 		nr++;
169180fb49dSKent Overstreet 	rcu_read_unlock();
170180fb49dSKent Overstreet 
171180fb49dSKent Overstreet 	bch2_journal_entry_res_resize(&c->journal,
172180fb49dSKent Overstreet 			&c->dev_usage_journal_res, u64s * nr);
173180fb49dSKent Overstreet }
174180fb49dSKent Overstreet 
1751c6fdbd8SKent Overstreet /* Filesystem RO/RW: */
1761c6fdbd8SKent Overstreet 
1771c6fdbd8SKent Overstreet /*
1781c6fdbd8SKent Overstreet  * For startup/shutdown of RW stuff, the dependencies are:
1791c6fdbd8SKent Overstreet  *
1801c6fdbd8SKent Overstreet  * - foreground writes depend on copygc and rebalance (to free up space)
1811c6fdbd8SKent Overstreet  *
1821c6fdbd8SKent Overstreet  * - copygc and rebalance depend on mark and sweep gc (they actually probably
1831c6fdbd8SKent Overstreet  *   don't because they either reserve ahead of time or don't block if
1841c6fdbd8SKent Overstreet  *   allocations fail, but allocations can require mark and sweep gc to run
1851c6fdbd8SKent Overstreet  *   because of generation number wraparound)
1861c6fdbd8SKent Overstreet  *
1871c6fdbd8SKent Overstreet  * - all of the above depends on the allocator threads
1881c6fdbd8SKent Overstreet  *
1891c6fdbd8SKent Overstreet  * - allocator depends on the journal (when it rewrites prios and gens)
1901c6fdbd8SKent Overstreet  */
1911c6fdbd8SKent Overstreet 
1921c6fdbd8SKent Overstreet static void __bch2_fs_read_only(struct bch_fs *c)
1931c6fdbd8SKent Overstreet {
1941c6fdbd8SKent Overstreet 	struct bch_dev *ca;
195d5f70c1fSKent Overstreet 	unsigned i, clean_passes = 0;
1961c6fdbd8SKent Overstreet 
1971c6fdbd8SKent Overstreet 	bch2_rebalance_stop(c);
198e6d11615SKent Overstreet 	bch2_copygc_stop(c);
1991c6fdbd8SKent Overstreet 	bch2_gc_thread_stop(c);
2001c6fdbd8SKent Overstreet 
2011c6fdbd8SKent Overstreet 	/*
2021c6fdbd8SKent Overstreet 	 * Flush journal before stopping allocators, because flushing journal
2031c6fdbd8SKent Overstreet 	 * blacklist entries involves allocating new btree nodes:
2041c6fdbd8SKent Overstreet 	 */
2051c6fdbd8SKent Overstreet 	bch2_journal_flush_all_pins(&c->journal);
2061c6fdbd8SKent Overstreet 
2072340fd9dSKent Overstreet 	/*
2082340fd9dSKent Overstreet 	 * If the allocator threads didn't all start up, the btree updates to
2092340fd9dSKent Overstreet 	 * write out alloc info aren't going to work:
2102340fd9dSKent Overstreet 	 */
211b935a8a6SKent Overstreet 	if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags))
2122340fd9dSKent Overstreet 		goto nowrote_alloc;
213b935a8a6SKent Overstreet 
214039fc4c5SKent Overstreet 	bch_verbose(c, "flushing journal and stopping allocators");
2151c6fdbd8SKent Overstreet 
2161c6fdbd8SKent Overstreet 	bch2_journal_flush_all_pins(&c->journal);
217039fc4c5SKent Overstreet 	set_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags);
218039fc4c5SKent Overstreet 
219039fc4c5SKent Overstreet 	do {
220039fc4c5SKent Overstreet 		clean_passes++;
221039fc4c5SKent Overstreet 
222039fc4c5SKent Overstreet 		if (bch2_journal_flush_all_pins(&c->journal))
223039fc4c5SKent Overstreet 			clean_passes = 0;
2241c6fdbd8SKent Overstreet 
2251c6fdbd8SKent Overstreet 		/*
226039fc4c5SKent Overstreet 		 * In flight interior btree updates will generate more journal
227039fc4c5SKent Overstreet 		 * updates and btree updates (alloc btree):
2281c6fdbd8SKent Overstreet 		 */
229039fc4c5SKent Overstreet 		if (bch2_btree_interior_updates_nr_pending(c)) {
2301c6fdbd8SKent Overstreet 			closure_wait_event(&c->btree_interior_update_wait,
2311c6fdbd8SKent Overstreet 					   !bch2_btree_interior_updates_nr_pending(c));
232039fc4c5SKent Overstreet 			clean_passes = 0;
233039fc4c5SKent Overstreet 		}
23400b8ccf7SKent Overstreet 		flush_work(&c->btree_interior_update_work);
235d5f70c1fSKent Overstreet 
236039fc4c5SKent Overstreet 		if (bch2_journal_flush_all_pins(&c->journal))
237039fc4c5SKent Overstreet 			clean_passes = 0;
238d5f70c1fSKent Overstreet 	} while (clean_passes < 2);
239039fc4c5SKent Overstreet 	bch_verbose(c, "flushing journal and stopping allocators complete");
2402340fd9dSKent Overstreet 
2412340fd9dSKent Overstreet 	set_bit(BCH_FS_ALLOC_CLEAN, &c->flags);
2422340fd9dSKent Overstreet nowrote_alloc:
24300b8ccf7SKent Overstreet 	closure_wait_event(&c->btree_interior_update_wait,
24400b8ccf7SKent Overstreet 			   !bch2_btree_interior_updates_nr_pending(c));
24500b8ccf7SKent Overstreet 	flush_work(&c->btree_interior_update_work);
24600b8ccf7SKent Overstreet 
247430735cdSKent Overstreet 	for_each_member_device(ca, c, i)
248430735cdSKent Overstreet 		bch2_dev_allocator_stop(ca);
2491c6fdbd8SKent Overstreet 
250b935a8a6SKent Overstreet 	clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
251039fc4c5SKent Overstreet 	clear_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags);
252b935a8a6SKent Overstreet 
2531c6fdbd8SKent Overstreet 	bch2_fs_journal_stop(&c->journal);
2541c6fdbd8SKent Overstreet 
2551c6fdbd8SKent Overstreet 	/*
2561c6fdbd8SKent Overstreet 	 * the journal kicks off btree writes via reclaim - wait for in flight
2571c6fdbd8SKent Overstreet 	 * writes after stopping journal:
2581c6fdbd8SKent Overstreet 	 */
2591c6fdbd8SKent Overstreet 	bch2_btree_flush_all_writes(c);
2601c6fdbd8SKent Overstreet 
2611c6fdbd8SKent Overstreet 	/*
2621c6fdbd8SKent Overstreet 	 * After stopping journal:
2631c6fdbd8SKent Overstreet 	 */
2641c6fdbd8SKent Overstreet 	for_each_member_device(ca, c, i)
2651c6fdbd8SKent Overstreet 		bch2_dev_allocator_remove(c, ca);
2661c6fdbd8SKent Overstreet }
2671c6fdbd8SKent Overstreet 
2681c6fdbd8SKent Overstreet static void bch2_writes_disabled(struct percpu_ref *writes)
2691c6fdbd8SKent Overstreet {
2701c6fdbd8SKent Overstreet 	struct bch_fs *c = container_of(writes, struct bch_fs, writes);
2711c6fdbd8SKent Overstreet 
2721c6fdbd8SKent Overstreet 	set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
2731c6fdbd8SKent Overstreet 	wake_up(&bch_read_only_wait);
2741c6fdbd8SKent Overstreet }
2751c6fdbd8SKent Overstreet 
2761c6fdbd8SKent Overstreet void bch2_fs_read_only(struct bch_fs *c)
2771c6fdbd8SKent Overstreet {
278134915f3SKent Overstreet 	if (!test_bit(BCH_FS_RW, &c->flags)) {
2799ae28f82SKent Overstreet 		bch2_journal_reclaim_stop(&c->journal);
2801c6fdbd8SKent Overstreet 		return;
281134915f3SKent Overstreet 	}
2821c6fdbd8SKent Overstreet 
2831c6fdbd8SKent Overstreet 	BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
2841c6fdbd8SKent Overstreet 
2851c6fdbd8SKent Overstreet 	/*
2861c6fdbd8SKent Overstreet 	 * Block new foreground-end write operations from starting - any new
2871c6fdbd8SKent Overstreet 	 * writes will return -EROFS:
2881c6fdbd8SKent Overstreet 	 *
2891c6fdbd8SKent Overstreet 	 * (This is really blocking new _allocations_, writes to previously
2901c6fdbd8SKent Overstreet 	 * allocated space can still happen until stopping the allocator in
2911c6fdbd8SKent Overstreet 	 * bch2_dev_allocator_stop()).
2921c6fdbd8SKent Overstreet 	 */
2931c6fdbd8SKent Overstreet 	percpu_ref_kill(&c->writes);
2941c6fdbd8SKent Overstreet 
295f516c872SKent Overstreet 	cancel_work_sync(&c->ec_stripe_delete_work);
2961c6fdbd8SKent Overstreet 
2971c6fdbd8SKent Overstreet 	/*
2981c6fdbd8SKent Overstreet 	 * If we're not doing an emergency shutdown, we want to wait on
2991c6fdbd8SKent Overstreet 	 * outstanding writes to complete so they don't see spurious errors due
3001c6fdbd8SKent Overstreet 	 * to shutting down the allocator:
3011c6fdbd8SKent Overstreet 	 *
3021c6fdbd8SKent Overstreet 	 * If we are doing an emergency shutdown outstanding writes may
3031c6fdbd8SKent Overstreet 	 * hang until we shutdown the allocator so we don't want to wait
3041c6fdbd8SKent Overstreet 	 * on outstanding writes before shutting everything down - but
3051c6fdbd8SKent Overstreet 	 * we do need to wait on them before returning and signalling
3061c6fdbd8SKent Overstreet 	 * that going RO is complete:
3071c6fdbd8SKent Overstreet 	 */
3081c6fdbd8SKent Overstreet 	wait_event(bch_read_only_wait,
3091c6fdbd8SKent Overstreet 		   test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) ||
3101c6fdbd8SKent Overstreet 		   test_bit(BCH_FS_EMERGENCY_RO, &c->flags));
3111c6fdbd8SKent Overstreet 
3121c6fdbd8SKent Overstreet 	__bch2_fs_read_only(c);
3131c6fdbd8SKent Overstreet 
3141c6fdbd8SKent Overstreet 	wait_event(bch_read_only_wait,
3151c6fdbd8SKent Overstreet 		   test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags));
3161c6fdbd8SKent Overstreet 
3171c6fdbd8SKent Overstreet 	clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags);
3181c6fdbd8SKent Overstreet 
3191c6fdbd8SKent Overstreet 	if (!bch2_journal_error(&c->journal) &&
3201c6fdbd8SKent Overstreet 	    !test_bit(BCH_FS_ERROR, &c->flags) &&
3213aea4342SKent Overstreet 	    !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) &&
322a0e0bda1SKent Overstreet 	    test_bit(BCH_FS_STARTED, &c->flags) &&
3232340fd9dSKent Overstreet 	    test_bit(BCH_FS_ALLOC_CLEAN, &c->flags) &&
324b2930396SKent Overstreet 	    !c->opts.norecovery) {
325b2930396SKent Overstreet 		bch_verbose(c, "marking filesystem clean");
326134915f3SKent Overstreet 		bch2_fs_mark_clean(c);
327b2930396SKent Overstreet 	}
3281c6fdbd8SKent Overstreet 
329134915f3SKent Overstreet 	clear_bit(BCH_FS_RW, &c->flags);
3301c6fdbd8SKent Overstreet }
3311c6fdbd8SKent Overstreet 
3321c6fdbd8SKent Overstreet static void bch2_fs_read_only_work(struct work_struct *work)
3331c6fdbd8SKent Overstreet {
3341c6fdbd8SKent Overstreet 	struct bch_fs *c =
3351c6fdbd8SKent Overstreet 		container_of(work, struct bch_fs, read_only_work);
3361c6fdbd8SKent Overstreet 
3371ada1606SKent Overstreet 	down_write(&c->state_lock);
3381c6fdbd8SKent Overstreet 	bch2_fs_read_only(c);
3391ada1606SKent Overstreet 	up_write(&c->state_lock);
3401c6fdbd8SKent Overstreet }
3411c6fdbd8SKent Overstreet 
3421c6fdbd8SKent Overstreet static void bch2_fs_read_only_async(struct bch_fs *c)
3431c6fdbd8SKent Overstreet {
3441c6fdbd8SKent Overstreet 	queue_work(system_long_wq, &c->read_only_work);
3451c6fdbd8SKent Overstreet }
3461c6fdbd8SKent Overstreet 
3471c6fdbd8SKent Overstreet bool bch2_fs_emergency_read_only(struct bch_fs *c)
3481c6fdbd8SKent Overstreet {
3491c6fdbd8SKent Overstreet 	bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags);
3501c6fdbd8SKent Overstreet 
3511c6fdbd8SKent Overstreet 	bch2_journal_halt(&c->journal);
3529f115ce9SKent Overstreet 	bch2_fs_read_only_async(c);
3531c6fdbd8SKent Overstreet 
3541c6fdbd8SKent Overstreet 	wake_up(&bch_read_only_wait);
3551c6fdbd8SKent Overstreet 	return ret;
3561c6fdbd8SKent Overstreet }
3571c6fdbd8SKent Overstreet 
358134915f3SKent Overstreet static int bch2_fs_read_write_late(struct bch_fs *c)
3591c6fdbd8SKent Overstreet {
360134915f3SKent Overstreet 	int ret;
3611c6fdbd8SKent Overstreet 
362134915f3SKent Overstreet 	ret = bch2_gc_thread_start(c);
363134915f3SKent Overstreet 	if (ret) {
364134915f3SKent Overstreet 		bch_err(c, "error starting gc thread");
365134915f3SKent Overstreet 		return ret;
366134915f3SKent Overstreet 	}
3671c6fdbd8SKent Overstreet 
368e6d11615SKent Overstreet 	ret = bch2_copygc_start(c);
369134915f3SKent Overstreet 	if (ret) {
370e6d11615SKent Overstreet 		bch_err(c, "error starting copygc thread");
371134915f3SKent Overstreet 		return ret;
372134915f3SKent Overstreet 	}
373134915f3SKent Overstreet 
374134915f3SKent Overstreet 	ret = bch2_rebalance_start(c);
375134915f3SKent Overstreet 	if (ret) {
376134915f3SKent Overstreet 		bch_err(c, "error starting rebalance thread");
377134915f3SKent Overstreet 		return ret;
378134915f3SKent Overstreet 	}
379134915f3SKent Overstreet 
38097fd13adSKent Overstreet 	schedule_work(&c->ec_stripe_delete_work);
38197fd13adSKent Overstreet 
382134915f3SKent Overstreet 	return 0;
383134915f3SKent Overstreet }
384134915f3SKent Overstreet 
385e731d466SKent Overstreet static int __bch2_fs_read_write(struct bch_fs *c, bool early)
386134915f3SKent Overstreet {
387134915f3SKent Overstreet 	struct bch_dev *ca;
388134915f3SKent Overstreet 	unsigned i;
389134915f3SKent Overstreet 	int ret;
390134915f3SKent Overstreet 
391aae15aafSKent Overstreet 	if (test_bit(BCH_FS_INITIAL_GC_UNFIXED, &c->flags)) {
392aae15aafSKent Overstreet 		bch_err(c, "cannot go rw, unfixed btree errors");
393aae15aafSKent Overstreet 		return -EROFS;
394aae15aafSKent Overstreet 	}
395aae15aafSKent Overstreet 
396134915f3SKent Overstreet 	if (test_bit(BCH_FS_RW, &c->flags))
397134915f3SKent Overstreet 		return 0;
398134915f3SKent Overstreet 
399619f5beeSKent Overstreet 	/*
400619f5beeSKent Overstreet 	 * nochanges is used for fsck -n mode - we have to allow going rw
401619f5beeSKent Overstreet 	 * during recovery for that to work:
402619f5beeSKent Overstreet 	 */
403619f5beeSKent Overstreet 	if (c->opts.norecovery ||
404619f5beeSKent Overstreet 	    (c->opts.nochanges &&
405619f5beeSKent Overstreet 	     (!early || c->opts.read_only)))
406619f5beeSKent Overstreet 		return -EROFS;
407330581f1SKent Overstreet 
4082c944fa1SKent Overstreet 	bch_info(c, "going read-write");
4092c944fa1SKent Overstreet 
410134915f3SKent Overstreet 	ret = bch2_fs_mark_dirty(c);
411134915f3SKent Overstreet 	if (ret)
412134915f3SKent Overstreet 		goto err;
4131c6fdbd8SKent Overstreet 
4149f115ce9SKent Overstreet 	/*
4159f115ce9SKent Overstreet 	 * We need to write out a journal entry before we start doing btree
4169f115ce9SKent Overstreet 	 * updates, to ensure that on unclean shutdown new journal blacklist
4179f115ce9SKent Overstreet 	 * entries are created:
4189f115ce9SKent Overstreet 	 */
4199f115ce9SKent Overstreet 	bch2_journal_meta(&c->journal);
4209f115ce9SKent Overstreet 
4212340fd9dSKent Overstreet 	clear_bit(BCH_FS_ALLOC_CLEAN, &c->flags);
4222340fd9dSKent Overstreet 
4231c6fdbd8SKent Overstreet 	for_each_rw_member(ca, c, i)
4241c6fdbd8SKent Overstreet 		bch2_dev_allocator_add(c, ca);
4251c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
4261c6fdbd8SKent Overstreet 
427134915f3SKent Overstreet 	for_each_rw_member(ca, c, i) {
428134915f3SKent Overstreet 		ret = bch2_dev_allocator_start(ca);
429134915f3SKent Overstreet 		if (ret) {
430134915f3SKent Overstreet 			bch_err(c, "error starting allocator threads");
4311c6fdbd8SKent Overstreet 			percpu_ref_put(&ca->io_ref);
4321c6fdbd8SKent Overstreet 			goto err;
4331c6fdbd8SKent Overstreet 		}
434134915f3SKent Overstreet 	}
4351c6fdbd8SKent Overstreet 
436b935a8a6SKent Overstreet 	set_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags);
437b935a8a6SKent Overstreet 
43859a74051SKent Overstreet 	for_each_rw_member(ca, c, i)
43959a74051SKent Overstreet 		bch2_wake_allocator(ca);
44059a74051SKent Overstreet 
441134915f3SKent Overstreet 	if (!early) {
442134915f3SKent Overstreet 		ret = bch2_fs_read_write_late(c);
443134915f3SKent Overstreet 		if (ret)
4441c6fdbd8SKent Overstreet 			goto err;
4451c6fdbd8SKent Overstreet 	}
4461c6fdbd8SKent Overstreet 
4471c6fdbd8SKent Overstreet 	percpu_ref_reinit(&c->writes);
448134915f3SKent Overstreet 	set_bit(BCH_FS_RW, &c->flags);
4494932e07eSKent Overstreet 	set_bit(BCH_FS_WAS_RW, &c->flags);
450134915f3SKent Overstreet 	return 0;
4511c6fdbd8SKent Overstreet err:
4521c6fdbd8SKent Overstreet 	__bch2_fs_read_only(c);
453134915f3SKent Overstreet 	return ret;
454134915f3SKent Overstreet }
455134915f3SKent Overstreet 
456134915f3SKent Overstreet int bch2_fs_read_write(struct bch_fs *c)
457134915f3SKent Overstreet {
458134915f3SKent Overstreet 	return __bch2_fs_read_write(c, false);
459134915f3SKent Overstreet }
460134915f3SKent Overstreet 
461134915f3SKent Overstreet int bch2_fs_read_write_early(struct bch_fs *c)
462134915f3SKent Overstreet {
463134915f3SKent Overstreet 	lockdep_assert_held(&c->state_lock);
464134915f3SKent Overstreet 
465134915f3SKent Overstreet 	return __bch2_fs_read_write(c, true);
4661c6fdbd8SKent Overstreet }
4671c6fdbd8SKent Overstreet 
4681c6fdbd8SKent Overstreet /* Filesystem startup/shutdown: */
4691c6fdbd8SKent Overstreet 
470d5e4dcc2SKent Overstreet static void __bch2_fs_free(struct bch_fs *c)
4711c6fdbd8SKent Overstreet {
4721c6fdbd8SKent Overstreet 	unsigned i;
4731a21bf98SKent Overstreet 	int cpu;
4741c6fdbd8SKent Overstreet 
4751c6fdbd8SKent Overstreet 	for (i = 0; i < BCH_TIME_STAT_NR; i++)
4761c6fdbd8SKent Overstreet 		bch2_time_stats_exit(&c->times[i]);
4771c6fdbd8SKent Overstreet 
4781c6fdbd8SKent Overstreet 	bch2_fs_quota_exit(c);
4791c6fdbd8SKent Overstreet 	bch2_fs_fsio_exit(c);
480cd575ddfSKent Overstreet 	bch2_fs_ec_exit(c);
4811c6fdbd8SKent Overstreet 	bch2_fs_encryption_exit(c);
4821c6fdbd8SKent Overstreet 	bch2_fs_io_exit(c);
483c823c339SKent Overstreet 	bch2_fs_btree_interior_update_exit(c);
48436e9d698SKent Overstreet 	bch2_fs_btree_iter_exit(c);
4852ca88e5aSKent Overstreet 	bch2_fs_btree_key_cache_exit(&c->btree_key_cache);
4861c6fdbd8SKent Overstreet 	bch2_fs_btree_cache_exit(c);
4879620c3ecSKent Overstreet 	bch2_fs_replicas_exit(c);
4881c6fdbd8SKent Overstreet 	bch2_fs_journal_exit(&c->journal);
4891c6fdbd8SKent Overstreet 	bch2_io_clock_exit(&c->io_clock[WRITE]);
4901c6fdbd8SKent Overstreet 	bch2_io_clock_exit(&c->io_clock[READ]);
4911c6fdbd8SKent Overstreet 	bch2_fs_compress_exit(c);
492f1d786a0SKent Overstreet 	bch2_journal_keys_free(&c->journal_keys);
493f1d786a0SKent Overstreet 	bch2_journal_entries_free(&c->journal_entries);
4949166b41dSKent Overstreet 	percpu_free_rwsem(&c->mark_lock);
4955e82a9a1SKent Overstreet 	free_percpu(c->online_reserved);
4961a21bf98SKent Overstreet 
4971a21bf98SKent Overstreet 	if (c->btree_iters_bufs)
4981a21bf98SKent Overstreet 		for_each_possible_cpu(cpu)
4991a21bf98SKent Overstreet 			kfree(per_cpu_ptr(c->btree_iters_bufs, cpu)->iter);
5001a21bf98SKent Overstreet 
5011a21bf98SKent Overstreet 	free_percpu(c->btree_iters_bufs);
5025663a415SKent Overstreet 	free_percpu(c->pcpu);
50335189e09SKent Overstreet 	mempool_exit(&c->large_bkey_pool);
5041c6fdbd8SKent Overstreet 	mempool_exit(&c->btree_bounce_pool);
5051c6fdbd8SKent Overstreet 	bioset_exit(&c->btree_bio);
5061c6fdbd8SKent Overstreet 	mempool_exit(&c->fill_iter);
5071c6fdbd8SKent Overstreet 	percpu_ref_exit(&c->writes);
5081c6fdbd8SKent Overstreet 	kfree(rcu_dereference_protected(c->disk_groups, 1));
5091dd7f9d9SKent Overstreet 	kfree(c->journal_seq_blacklist_table);
510b5e8a699SKent Overstreet 	kfree(c->unused_inode_hints);
511e6d11615SKent Overstreet 	free_heap(&c->copygc_heap);
5121c6fdbd8SKent Overstreet 
513731bdd2eSKent Overstreet 	if (c->io_complete_wq )
514731bdd2eSKent Overstreet 		destroy_workqueue(c->io_complete_wq );
5151c6fdbd8SKent Overstreet 	if (c->copygc_wq)
5161c6fdbd8SKent Overstreet 		destroy_workqueue(c->copygc_wq);
5179f1833caSKent Overstreet 	if (c->btree_io_complete_wq)
5189f1833caSKent Overstreet 		destroy_workqueue(c->btree_io_complete_wq);
519731bdd2eSKent Overstreet 	if (c->btree_update_wq)
520731bdd2eSKent Overstreet 		destroy_workqueue(c->btree_update_wq);
5211c6fdbd8SKent Overstreet 
5229d8022dbSKent Overstreet 	bch2_free_super(&c->disk_sb);
5231c6fdbd8SKent Overstreet 	kvpfree(c, sizeof(*c));
5241c6fdbd8SKent Overstreet 	module_put(THIS_MODULE);
5251c6fdbd8SKent Overstreet }
5261c6fdbd8SKent Overstreet 
5271c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *kobj)
5281c6fdbd8SKent Overstreet {
5291c6fdbd8SKent Overstreet 	struct bch_fs *c = container_of(kobj, struct bch_fs, kobj);
5301c6fdbd8SKent Overstreet 
531d5e4dcc2SKent Overstreet 	__bch2_fs_free(c);
5321c6fdbd8SKent Overstreet }
5331c6fdbd8SKent Overstreet 
534d5e4dcc2SKent Overstreet void __bch2_fs_stop(struct bch_fs *c)
5351c6fdbd8SKent Overstreet {
5361c6fdbd8SKent Overstreet 	struct bch_dev *ca;
5371c6fdbd8SKent Overstreet 	unsigned i;
5381c6fdbd8SKent Overstreet 
539af1c6871SKent Overstreet 	bch_verbose(c, "shutting down");
540af1c6871SKent Overstreet 
5411dd7f9d9SKent Overstreet 	set_bit(BCH_FS_STOPPING, &c->flags);
5421dd7f9d9SKent Overstreet 
5431dd7f9d9SKent Overstreet 	cancel_work_sync(&c->journal_seq_blacklist_gc_work);
5441dd7f9d9SKent Overstreet 
5451ada1606SKent Overstreet 	down_write(&c->state_lock);
546883f1a7cSKent Overstreet 	bch2_fs_read_only(c);
5471ada1606SKent Overstreet 	up_write(&c->state_lock);
548883f1a7cSKent Overstreet 
5491c6fdbd8SKent Overstreet 	for_each_member_device(ca, c, i)
5501c6fdbd8SKent Overstreet 		if (ca->kobj.state_in_sysfs &&
5511c6fdbd8SKent Overstreet 		    ca->disk_sb.bdev)
5521c6fdbd8SKent Overstreet 			sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
5531c6fdbd8SKent Overstreet 
5541c6fdbd8SKent Overstreet 	if (c->kobj.state_in_sysfs)
5551c6fdbd8SKent Overstreet 		kobject_del(&c->kobj);
5561c6fdbd8SKent Overstreet 
5571c6fdbd8SKent Overstreet 	bch2_fs_debug_exit(c);
5581c6fdbd8SKent Overstreet 	bch2_fs_chardev_exit(c);
5591c6fdbd8SKent Overstreet 
5601c6fdbd8SKent Overstreet 	kobject_put(&c->time_stats);
5611c6fdbd8SKent Overstreet 	kobject_put(&c->opts_dir);
5621c6fdbd8SKent Overstreet 	kobject_put(&c->internal);
5631c6fdbd8SKent Overstreet 
5641c6fdbd8SKent Overstreet 	/* btree prefetch might have kicked off reads in the background: */
5651c6fdbd8SKent Overstreet 	bch2_btree_flush_all_reads(c);
5661c6fdbd8SKent Overstreet 
5671c6fdbd8SKent Overstreet 	for_each_member_device(ca, c, i)
5681c6fdbd8SKent Overstreet 		cancel_work_sync(&ca->io_error_work);
5691c6fdbd8SKent Overstreet 
5701c6fdbd8SKent Overstreet 	cancel_work_sync(&c->read_only_work);
571d5e4dcc2SKent Overstreet }
5721c6fdbd8SKent Overstreet 
573d5e4dcc2SKent Overstreet void bch2_fs_free(struct bch_fs *c)
574d5e4dcc2SKent Overstreet {
575d5e4dcc2SKent Overstreet 	unsigned i;
576d5e4dcc2SKent Overstreet 
577d5e4dcc2SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
578d5e4dcc2SKent Overstreet 	list_del(&c->list);
579d5e4dcc2SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
580d5e4dcc2SKent Overstreet 
581d5e4dcc2SKent Overstreet 	closure_sync(&c->cl);
582d5e4dcc2SKent Overstreet 	closure_debug_destroy(&c->cl);
583d5e4dcc2SKent Overstreet 
584d5e4dcc2SKent Overstreet 	for (i = 0; i < c->sb.nr_devices; i++) {
585d5e4dcc2SKent Overstreet 		struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true);
586d5e4dcc2SKent Overstreet 
587d5e4dcc2SKent Overstreet 		if (ca) {
588d5e4dcc2SKent Overstreet 			bch2_free_super(&ca->disk_sb);
589d5e4dcc2SKent Overstreet 			bch2_dev_free(ca);
590d5e4dcc2SKent Overstreet 		}
591d5e4dcc2SKent Overstreet 	}
5921c6fdbd8SKent Overstreet 
593af1c6871SKent Overstreet 	bch_verbose(c, "shutdown complete");
594af1c6871SKent Overstreet 
5951c6fdbd8SKent Overstreet 	kobject_put(&c->kobj);
5961c6fdbd8SKent Overstreet }
5971c6fdbd8SKent Overstreet 
598d5e4dcc2SKent Overstreet void bch2_fs_stop(struct bch_fs *c)
599d5e4dcc2SKent Overstreet {
600d5e4dcc2SKent Overstreet 	__bch2_fs_stop(c);
601d5e4dcc2SKent Overstreet 	bch2_fs_free(c);
602d5e4dcc2SKent Overstreet }
603d5e4dcc2SKent Overstreet 
6041c6fdbd8SKent Overstreet static const char *bch2_fs_online(struct bch_fs *c)
6051c6fdbd8SKent Overstreet {
6061c6fdbd8SKent Overstreet 	struct bch_dev *ca;
6071c6fdbd8SKent Overstreet 	const char *err = NULL;
6081c6fdbd8SKent Overstreet 	unsigned i;
6091c6fdbd8SKent Overstreet 	int ret;
6101c6fdbd8SKent Overstreet 
6111c6fdbd8SKent Overstreet 	lockdep_assert_held(&bch_fs_list_lock);
6121c6fdbd8SKent Overstreet 
6131c6fdbd8SKent Overstreet 	if (!list_empty(&c->list))
6141c6fdbd8SKent Overstreet 		return NULL;
6151c6fdbd8SKent Overstreet 
6161c6fdbd8SKent Overstreet 	if (__bch2_uuid_to_fs(c->sb.uuid))
6171c6fdbd8SKent Overstreet 		return "filesystem UUID already open";
6181c6fdbd8SKent Overstreet 
6191c6fdbd8SKent Overstreet 	ret = bch2_fs_chardev_init(c);
6201c6fdbd8SKent Overstreet 	if (ret)
6211c6fdbd8SKent Overstreet 		return "error creating character device";
6221c6fdbd8SKent Overstreet 
6231c6fdbd8SKent Overstreet 	bch2_fs_debug_init(c);
6241c6fdbd8SKent Overstreet 
6251c6fdbd8SKent Overstreet 	if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ||
6261c6fdbd8SKent Overstreet 	    kobject_add(&c->internal, &c->kobj, "internal") ||
6271c6fdbd8SKent Overstreet 	    kobject_add(&c->opts_dir, &c->kobj, "options") ||
6281c6fdbd8SKent Overstreet 	    kobject_add(&c->time_stats, &c->kobj, "time_stats") ||
6291c6fdbd8SKent Overstreet 	    bch2_opts_create_sysfs_files(&c->opts_dir))
6301c6fdbd8SKent Overstreet 		return "error creating sysfs objects";
6311c6fdbd8SKent Overstreet 
6321ada1606SKent Overstreet 	down_write(&c->state_lock);
6331c6fdbd8SKent Overstreet 
6341c6fdbd8SKent Overstreet 	err = "error creating sysfs objects";
6353a402c8dSKent Overstreet 	for_each_member_device(ca, c, i)
6363a402c8dSKent Overstreet 		if (bch2_dev_sysfs_online(c, ca)) {
6373a402c8dSKent Overstreet 			percpu_ref_put(&ca->ref);
6381c6fdbd8SKent Overstreet 			goto err;
6393a402c8dSKent Overstreet 		}
6401c6fdbd8SKent Overstreet 
6411c6fdbd8SKent Overstreet 	list_add(&c->list, &bch_fs_list);
6421c6fdbd8SKent Overstreet 	err = NULL;
6431c6fdbd8SKent Overstreet err:
6441ada1606SKent Overstreet 	up_write(&c->state_lock);
6451c6fdbd8SKent Overstreet 	return err;
6461c6fdbd8SKent Overstreet }
6471c6fdbd8SKent Overstreet 
6481c6fdbd8SKent Overstreet static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
6491c6fdbd8SKent Overstreet {
6501c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
6511c6fdbd8SKent Overstreet 	struct bch_fs *c;
652ecf37a4aSKent Overstreet 	unsigned i, iter_size;
6531c6fdbd8SKent Overstreet 	const char *err;
6541c6fdbd8SKent Overstreet 
6551c6fdbd8SKent Overstreet 	pr_verbose_init(opts, "");
6561c6fdbd8SKent Overstreet 
6571c6fdbd8SKent Overstreet 	c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO);
6581c6fdbd8SKent Overstreet 	if (!c)
6591c6fdbd8SKent Overstreet 		goto out;
6601c6fdbd8SKent Overstreet 
6611c6fdbd8SKent Overstreet 	__module_get(THIS_MODULE);
6621c6fdbd8SKent Overstreet 
663505b7a4cSKent Overstreet 	closure_init(&c->cl, NULL);
664505b7a4cSKent Overstreet 
665505b7a4cSKent Overstreet 	c->kobj.kset = bcachefs_kset;
666505b7a4cSKent Overstreet 	kobject_init(&c->kobj, &bch2_fs_ktype);
667505b7a4cSKent Overstreet 	kobject_init(&c->internal, &bch2_fs_internal_ktype);
668505b7a4cSKent Overstreet 	kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype);
669505b7a4cSKent Overstreet 	kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype);
670505b7a4cSKent Overstreet 
6711c6fdbd8SKent Overstreet 	c->minor		= -1;
6721c6fdbd8SKent Overstreet 	c->disk_sb.fs_sb	= true;
6731c6fdbd8SKent Overstreet 
6741ada1606SKent Overstreet 	init_rwsem(&c->state_lock);
6751c6fdbd8SKent Overstreet 	mutex_init(&c->sb_lock);
6761c6fdbd8SKent Overstreet 	mutex_init(&c->replicas_gc_lock);
6771c6fdbd8SKent Overstreet 	mutex_init(&c->btree_root_lock);
6781c6fdbd8SKent Overstreet 	INIT_WORK(&c->read_only_work, bch2_fs_read_only_work);
6791c6fdbd8SKent Overstreet 
6801c6fdbd8SKent Overstreet 	init_rwsem(&c->gc_lock);
6811c6fdbd8SKent Overstreet 
6821c6fdbd8SKent Overstreet 	for (i = 0; i < BCH_TIME_STAT_NR; i++)
6831c6fdbd8SKent Overstreet 		bch2_time_stats_init(&c->times[i]);
6841c6fdbd8SKent Overstreet 
685e6d11615SKent Overstreet 	bch2_fs_copygc_init(c);
6862ca88e5aSKent Overstreet 	bch2_fs_btree_key_cache_init_early(&c->btree_key_cache);
687b092daddSKent Overstreet 	bch2_fs_allocator_background_init(c);
688b092daddSKent Overstreet 	bch2_fs_allocator_foreground_init(c);
6891c6fdbd8SKent Overstreet 	bch2_fs_rebalance_init(c);
6901c6fdbd8SKent Overstreet 	bch2_fs_quota_init(c);
6911c6fdbd8SKent Overstreet 
6921c6fdbd8SKent Overstreet 	INIT_LIST_HEAD(&c->list);
6931c6fdbd8SKent Overstreet 
6944d8100daSKent Overstreet 	mutex_init(&c->usage_scratch_lock);
6954d8100daSKent Overstreet 
6961c6fdbd8SKent Overstreet 	mutex_init(&c->bio_bounce_pages_lock);
6971c6fdbd8SKent Overstreet 
6981c6fdbd8SKent Overstreet 	spin_lock_init(&c->btree_write_error_lock);
6991c6fdbd8SKent Overstreet 
7001dd7f9d9SKent Overstreet 	INIT_WORK(&c->journal_seq_blacklist_gc_work,
7011dd7f9d9SKent Overstreet 		  bch2_blacklist_entries_gc);
7021dd7f9d9SKent Overstreet 
703f1d786a0SKent Overstreet 	INIT_LIST_HEAD(&c->journal_entries);
7045b593ee1SKent Overstreet 	INIT_LIST_HEAD(&c->journal_iters);
705f1d786a0SKent Overstreet 
7061c6fdbd8SKent Overstreet 	INIT_LIST_HEAD(&c->fsck_errors);
7071c6fdbd8SKent Overstreet 	mutex_init(&c->fsck_error_lock);
7081c6fdbd8SKent Overstreet 
709703e2a43SKent Overstreet 	INIT_LIST_HEAD(&c->ec_stripe_head_list);
710703e2a43SKent Overstreet 	mutex_init(&c->ec_stripe_head_lock);
711703e2a43SKent Overstreet 
712703e2a43SKent Overstreet 	INIT_LIST_HEAD(&c->ec_stripe_new_list);
713703e2a43SKent Overstreet 	mutex_init(&c->ec_stripe_new_lock);
714703e2a43SKent Overstreet 
715*8dd6ed94SBrett Holman 	INIT_LIST_HEAD(&c->data_progress_list);
716*8dd6ed94SBrett Holman 	mutex_init(&c->data_progress_lock);
717*8dd6ed94SBrett Holman 
718cd575ddfSKent Overstreet 	spin_lock_init(&c->ec_stripes_heap_lock);
719cd575ddfSKent Overstreet 
7201c6fdbd8SKent Overstreet 	seqcount_init(&c->gc_pos_lock);
7211c6fdbd8SKent Overstreet 
7225e82a9a1SKent Overstreet 	seqcount_init(&c->usage_lock);
7235e82a9a1SKent Overstreet 
724ef1b2092SKent Overstreet 	sema_init(&c->io_in_flight, 128);
725ef1b2092SKent Overstreet 
7261c6fdbd8SKent Overstreet 	c->copy_gc_enabled		= 1;
7271c6fdbd8SKent Overstreet 	c->rebalance.enabled		= 1;
7281c6fdbd8SKent Overstreet 	c->promote_whole_extents	= true;
7291c6fdbd8SKent Overstreet 
7301c6fdbd8SKent Overstreet 	c->journal.write_time	= &c->times[BCH_TIME_journal_write];
7311c6fdbd8SKent Overstreet 	c->journal.delay_time	= &c->times[BCH_TIME_journal_delay];
73249a67206SKent Overstreet 	c->journal.blocked_time	= &c->times[BCH_TIME_blocked_journal];
7331c6fdbd8SKent Overstreet 	c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq];
7341c6fdbd8SKent Overstreet 
7351c6fdbd8SKent Overstreet 	bch2_fs_btree_cache_init_early(&c->btree_cache);
7361c6fdbd8SKent Overstreet 
737fca1223cSKent Overstreet 	mutex_init(&c->sectors_available_lock);
738fca1223cSKent Overstreet 
73973e6ab95SKent Overstreet 	if (percpu_init_rwsem(&c->mark_lock))
74073e6ab95SKent Overstreet 		goto err;
74173e6ab95SKent Overstreet 
7421c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
7431c6fdbd8SKent Overstreet 
7441c6fdbd8SKent Overstreet 	if (bch2_sb_to_fs(c, sb)) {
7451c6fdbd8SKent Overstreet 		mutex_unlock(&c->sb_lock);
7461c6fdbd8SKent Overstreet 		goto err;
7471c6fdbd8SKent Overstreet 	}
7481c6fdbd8SKent Overstreet 
7491c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
7501c6fdbd8SKent Overstreet 
7511c6fdbd8SKent Overstreet 	scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid);
7521c6fdbd8SKent Overstreet 
7531c6fdbd8SKent Overstreet 	c->opts = bch2_opts_default;
7541c6fdbd8SKent Overstreet 	bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb));
7551c6fdbd8SKent Overstreet 	bch2_opts_apply(&c->opts, opts);
7561c6fdbd8SKent Overstreet 
7571c6fdbd8SKent Overstreet 	c->block_bits		= ilog2(c->opts.block_size);
7581c6fdbd8SKent Overstreet 	c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
7591c6fdbd8SKent Overstreet 
7601c6fdbd8SKent Overstreet 	if (bch2_fs_init_fault("fs_alloc"))
7611c6fdbd8SKent Overstreet 		goto err;
7621c6fdbd8SKent Overstreet 
763ae2f17d5SKent Overstreet 	iter_size = sizeof(struct sort_iter) +
7641c6fdbd8SKent Overstreet 		(btree_blocks(c) + 1) * 2 *
765ae2f17d5SKent Overstreet 		sizeof(struct sort_iter_set);
7661c6fdbd8SKent Overstreet 
767b5e8a699SKent Overstreet 	c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus()));
768b5e8a699SKent Overstreet 
769731bdd2eSKent Overstreet 	if (!(c->btree_update_wq = alloc_workqueue("bcachefs",
7702f33ece9SKent Overstreet 				WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) ||
7719f1833caSKent Overstreet 	    !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io",
7729f2772c4SKent Overstreet 				WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) ||
7732f33ece9SKent Overstreet 	    !(c->copygc_wq = alloc_workqueue("bcachefs_copygc",
7742f33ece9SKent Overstreet 				WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) ||
775731bdd2eSKent Overstreet 	    !(c->io_complete_wq = alloc_workqueue("bcachefs_io",
776731bdd2eSKent Overstreet 				WQ_FREEZABLE|WQ_HIGHPRI|WQ_MEM_RECLAIM, 1)) ||
777134915f3SKent Overstreet 	    percpu_ref_init(&c->writes, bch2_writes_disabled,
778134915f3SKent Overstreet 			    PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
7791c6fdbd8SKent Overstreet 	    mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) ||
7801c6fdbd8SKent Overstreet 	    bioset_init(&c->btree_bio, 1,
7811c6fdbd8SKent Overstreet 			max(offsetof(struct btree_read_bio, bio),
7821c6fdbd8SKent Overstreet 			    offsetof(struct btree_write_bio, wbio.bio)),
7831c6fdbd8SKent Overstreet 			BIOSET_NEED_BVECS) ||
7845663a415SKent Overstreet 	    !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) ||
7855e82a9a1SKent Overstreet 	    !(c->online_reserved = alloc_percpu(u64)) ||
7861a21bf98SKent Overstreet 	    !(c->btree_iters_bufs = alloc_percpu(struct btree_iter_buf)) ||
7871c6fdbd8SKent Overstreet 	    mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1,
7881c6fdbd8SKent Overstreet 					btree_bytes(c)) ||
78935189e09SKent Overstreet 	    mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) ||
790b5e8a699SKent Overstreet 	    !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits,
791b5e8a699SKent Overstreet 					      sizeof(u64), GFP_KERNEL)) ||
7921c6fdbd8SKent Overstreet 	    bch2_io_clock_init(&c->io_clock[READ]) ||
7931c6fdbd8SKent Overstreet 	    bch2_io_clock_init(&c->io_clock[WRITE]) ||
7941c6fdbd8SKent Overstreet 	    bch2_fs_journal_init(&c->journal) ||
7952c5af169SKent Overstreet 	    bch2_fs_replicas_init(c) ||
7961c6fdbd8SKent Overstreet 	    bch2_fs_btree_cache_init(c) ||
7972ca88e5aSKent Overstreet 	    bch2_fs_btree_key_cache_init(&c->btree_key_cache) ||
79836e9d698SKent Overstreet 	    bch2_fs_btree_iter_init(c) ||
799c823c339SKent Overstreet 	    bch2_fs_btree_interior_update_init(c) ||
8001c6fdbd8SKent Overstreet 	    bch2_fs_io_init(c) ||
8011c6fdbd8SKent Overstreet 	    bch2_fs_encryption_init(c) ||
8021c6fdbd8SKent Overstreet 	    bch2_fs_compress_init(c) ||
803cd575ddfSKent Overstreet 	    bch2_fs_ec_init(c) ||
8041c6fdbd8SKent Overstreet 	    bch2_fs_fsio_init(c))
8051c6fdbd8SKent Overstreet 		goto err;
8061c6fdbd8SKent Overstreet 
8071c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
8081c6fdbd8SKent Overstreet 	for (i = 0; i < c->sb.nr_devices; i++)
8091c6fdbd8SKent Overstreet 		if (bch2_dev_exists(c->disk_sb.sb, mi, i) &&
8101c6fdbd8SKent Overstreet 		    bch2_dev_alloc(c, i))
8111c6fdbd8SKent Overstreet 			goto err;
8121c6fdbd8SKent Overstreet 
8134b8f89afSKent Overstreet 	bch2_journal_entry_res_resize(&c->journal,
8144b8f89afSKent Overstreet 			&c->btree_root_journal_res,
8154b8f89afSKent Overstreet 			BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX));
8164b8f89afSKent Overstreet 	bch2_dev_usage_journal_reserve(c);
8174b8f89afSKent Overstreet 	bch2_journal_entry_res_resize(&c->journal,
8184b8f89afSKent Overstreet 			&c->clock_journal_res,
8194b8f89afSKent Overstreet 			(sizeof(struct jset_entry_clock) / sizeof(u64)) * 2);
8204b8f89afSKent Overstreet 
8211c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
8221c6fdbd8SKent Overstreet 	err = bch2_fs_online(c);
8231c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
8241c6fdbd8SKent Overstreet 	if (err) {
8251c6fdbd8SKent Overstreet 		bch_err(c, "bch2_fs_online() error: %s", err);
8261c6fdbd8SKent Overstreet 		goto err;
8271c6fdbd8SKent Overstreet 	}
8281c6fdbd8SKent Overstreet out:
8291c6fdbd8SKent Overstreet 	pr_verbose_init(opts, "ret %i", c ? 0 : -ENOMEM);
8301c6fdbd8SKent Overstreet 	return c;
8311c6fdbd8SKent Overstreet err:
8321c6fdbd8SKent Overstreet 	bch2_fs_free(c);
8331c6fdbd8SKent Overstreet 	c = NULL;
8341c6fdbd8SKent Overstreet 	goto out;
8351c6fdbd8SKent Overstreet }
8361c6fdbd8SKent Overstreet 
837619f5beeSKent Overstreet noinline_for_stack
838619f5beeSKent Overstreet static void print_mount_opts(struct bch_fs *c)
839619f5beeSKent Overstreet {
840619f5beeSKent Overstreet 	enum bch_opt_id i;
841619f5beeSKent Overstreet 	char buf[512];
842619f5beeSKent Overstreet 	struct printbuf p = PBUF(buf);
843619f5beeSKent Overstreet 	bool first = true;
844619f5beeSKent Overstreet 
845619f5beeSKent Overstreet 	strcpy(buf, "(null)");
846619f5beeSKent Overstreet 
847619f5beeSKent Overstreet 	if (c->opts.read_only) {
848619f5beeSKent Overstreet 		pr_buf(&p, "ro");
849619f5beeSKent Overstreet 		first = false;
850619f5beeSKent Overstreet 	}
851619f5beeSKent Overstreet 
852619f5beeSKent Overstreet 	for (i = 0; i < bch2_opts_nr; i++) {
853619f5beeSKent Overstreet 		const struct bch_option *opt = &bch2_opt_table[i];
854619f5beeSKent Overstreet 		u64 v = bch2_opt_get_by_id(&c->opts, i);
855619f5beeSKent Overstreet 
856619f5beeSKent Overstreet 		if (!(opt->mode & OPT_MOUNT))
857619f5beeSKent Overstreet 			continue;
858619f5beeSKent Overstreet 
859619f5beeSKent Overstreet 		if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
860619f5beeSKent Overstreet 			continue;
861619f5beeSKent Overstreet 
862619f5beeSKent Overstreet 		if (!first)
863619f5beeSKent Overstreet 			pr_buf(&p, ",");
864619f5beeSKent Overstreet 		first = false;
865619f5beeSKent Overstreet 		bch2_opt_to_text(&p, c, opt, v, OPT_SHOW_MOUNT_STYLE);
866619f5beeSKent Overstreet 	}
867619f5beeSKent Overstreet 
868619f5beeSKent Overstreet 	bch_info(c, "mounted with opts: %s", buf);
869619f5beeSKent Overstreet }
870619f5beeSKent Overstreet 
871619f5beeSKent Overstreet int bch2_fs_start(struct bch_fs *c)
8721c6fdbd8SKent Overstreet {
8731c6fdbd8SKent Overstreet 	const char *err = "cannot allocate memory";
8741c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
8751c6fdbd8SKent Overstreet 	struct bch_dev *ca;
876a420eea6STim Schlueter 	time64_t now = ktime_get_real_seconds();
8771c6fdbd8SKent Overstreet 	unsigned i;
8781c6fdbd8SKent Overstreet 	int ret = -EINVAL;
8791c6fdbd8SKent Overstreet 
8801ada1606SKent Overstreet 	down_write(&c->state_lock);
8811c6fdbd8SKent Overstreet 
882134915f3SKent Overstreet 	BUG_ON(test_bit(BCH_FS_STARTED, &c->flags));
8831c6fdbd8SKent Overstreet 
8841c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
8851c6fdbd8SKent Overstreet 
8861c6fdbd8SKent Overstreet 	for_each_online_member(ca, c, i)
8871c6fdbd8SKent Overstreet 		bch2_sb_from_fs(c, ca);
8881c6fdbd8SKent Overstreet 
8891c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
8901c6fdbd8SKent Overstreet 	for_each_online_member(ca, c, i)
8911c6fdbd8SKent Overstreet 		mi->members[ca->dev_idx].last_mount = cpu_to_le64(now);
8921c6fdbd8SKent Overstreet 
8931c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
8941c6fdbd8SKent Overstreet 
8951c6fdbd8SKent Overstreet 	for_each_rw_member(ca, c, i)
8961c6fdbd8SKent Overstreet 		bch2_dev_allocator_add(c, ca);
8971c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
8981c6fdbd8SKent Overstreet 
8991c6fdbd8SKent Overstreet 	ret = BCH_SB_INITIALIZED(c->disk_sb.sb)
9001c6fdbd8SKent Overstreet 		? bch2_fs_recovery(c)
9011c6fdbd8SKent Overstreet 		: bch2_fs_initialize(c);
9021c6fdbd8SKent Overstreet 	if (ret)
9031c6fdbd8SKent Overstreet 		goto err;
9041c6fdbd8SKent Overstreet 
905cd575ddfSKent Overstreet 	ret = bch2_opts_check_may_set(c);
906cd575ddfSKent Overstreet 	if (ret)
907cd575ddfSKent Overstreet 		goto err;
908cd575ddfSKent Overstreet 
9091c6fdbd8SKent Overstreet 	err = "dynamic fault";
910619f5beeSKent Overstreet 	ret = -EINVAL;
9111c6fdbd8SKent Overstreet 	if (bch2_fs_init_fault("fs_start"))
9121c6fdbd8SKent Overstreet 		goto err;
9131c6fdbd8SKent Overstreet 
914a9310ab0SKent Overstreet 	set_bit(BCH_FS_STARTED, &c->flags);
915a9310ab0SKent Overstreet 
9169f20ed15SKent Overstreet 	/*
9179f20ed15SKent Overstreet 	 * Allocator threads don't start filling copygc reserve until after we
9189f20ed15SKent Overstreet 	 * set BCH_FS_STARTED - wake them now:
9192ee47eecSKent Overstreet 	 *
9202ee47eecSKent Overstreet 	 * XXX ugly hack:
9212ee47eecSKent Overstreet 	 * Need to set ca->allocator_state here instead of relying on the
9222ee47eecSKent Overstreet 	 * allocator threads to do it to avoid racing with the copygc threads
9232ee47eecSKent Overstreet 	 * checking it and thinking they have no alloc reserve:
9249f20ed15SKent Overstreet 	 */
9252ee47eecSKent Overstreet 	for_each_online_member(ca, c, i) {
9262ee47eecSKent Overstreet 		ca->allocator_state = ALLOCATOR_running;
9279f20ed15SKent Overstreet 		bch2_wake_allocator(ca);
9282ee47eecSKent Overstreet 	}
9299f20ed15SKent Overstreet 
930619f5beeSKent Overstreet 	if (c->opts.read_only || c->opts.nochanges) {
9311c6fdbd8SKent Overstreet 		bch2_fs_read_only(c);
9321c6fdbd8SKent Overstreet 	} else {
933134915f3SKent Overstreet 		err = "error going read write";
934619f5beeSKent Overstreet 		ret = !test_bit(BCH_FS_RW, &c->flags)
935619f5beeSKent Overstreet 			? bch2_fs_read_write(c)
936619f5beeSKent Overstreet 			: bch2_fs_read_write_late(c);
937619f5beeSKent Overstreet 		if (ret)
9381c6fdbd8SKent Overstreet 			goto err;
9391c6fdbd8SKent Overstreet 	}
9401c6fdbd8SKent Overstreet 
941619f5beeSKent Overstreet 	print_mount_opts(c);
942619f5beeSKent Overstreet 	ret = 0;
9431c6fdbd8SKent Overstreet out:
9441ada1606SKent Overstreet 	up_write(&c->state_lock);
945619f5beeSKent Overstreet 	return ret;
9461c6fdbd8SKent Overstreet err:
9471c6fdbd8SKent Overstreet 	switch (ret) {
9481c6fdbd8SKent Overstreet 	case BCH_FSCK_ERRORS_NOT_FIXED:
9491c6fdbd8SKent Overstreet 		bch_err(c, "filesystem contains errors: please report this to the developers");
9501c6fdbd8SKent Overstreet 		pr_cont("mount with -o fix_errors to repair\n");
9511c6fdbd8SKent Overstreet 		err = "fsck error";
9521c6fdbd8SKent Overstreet 		break;
9531c6fdbd8SKent Overstreet 	case BCH_FSCK_REPAIR_UNIMPLEMENTED:
9541c6fdbd8SKent Overstreet 		bch_err(c, "filesystem contains errors: please report this to the developers");
9551c6fdbd8SKent Overstreet 		pr_cont("repair unimplemented: inform the developers so that it can be added\n");
9561c6fdbd8SKent Overstreet 		err = "fsck error";
9571c6fdbd8SKent Overstreet 		break;
9581c6fdbd8SKent Overstreet 	case BCH_FSCK_REPAIR_IMPOSSIBLE:
9591c6fdbd8SKent Overstreet 		bch_err(c, "filesystem contains errors, but repair impossible");
9601c6fdbd8SKent Overstreet 		err = "fsck error";
9611c6fdbd8SKent Overstreet 		break;
9621c6fdbd8SKent Overstreet 	case BCH_FSCK_UNKNOWN_VERSION:
9631c6fdbd8SKent Overstreet 		err = "unknown metadata version";;
9641c6fdbd8SKent Overstreet 		break;
9651c6fdbd8SKent Overstreet 	case -ENOMEM:
9661c6fdbd8SKent Overstreet 		err = "cannot allocate memory";
9671c6fdbd8SKent Overstreet 		break;
9681c6fdbd8SKent Overstreet 	case -EIO:
9691c6fdbd8SKent Overstreet 		err = "IO error";
9701c6fdbd8SKent Overstreet 		break;
9711c6fdbd8SKent Overstreet 	}
9721c6fdbd8SKent Overstreet 
9739516950cSKent Overstreet 	if (ret >= 0)
9749516950cSKent Overstreet 		ret = -EIO;
9751c6fdbd8SKent Overstreet 	goto out;
9761c6fdbd8SKent Overstreet }
9771c6fdbd8SKent Overstreet 
9781c6fdbd8SKent Overstreet static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c)
9791c6fdbd8SKent Overstreet {
9801c6fdbd8SKent Overstreet 	struct bch_sb_field_members *sb_mi;
9811c6fdbd8SKent Overstreet 
9821c6fdbd8SKent Overstreet 	sb_mi = bch2_sb_get_members(sb);
9831c6fdbd8SKent Overstreet 	if (!sb_mi)
9841c6fdbd8SKent Overstreet 		return "Invalid superblock: member info area missing";
9851c6fdbd8SKent Overstreet 
9861c6fdbd8SKent Overstreet 	if (le16_to_cpu(sb->block_size) != c->opts.block_size)
9871c6fdbd8SKent Overstreet 		return "mismatched block size";
9881c6fdbd8SKent Overstreet 
9891c6fdbd8SKent Overstreet 	if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) <
9901c6fdbd8SKent Overstreet 	    BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb))
9911c6fdbd8SKent Overstreet 		return "new cache bucket size is too small";
9921c6fdbd8SKent Overstreet 
9931c6fdbd8SKent Overstreet 	return NULL;
9941c6fdbd8SKent Overstreet }
9951c6fdbd8SKent Overstreet 
9961c6fdbd8SKent Overstreet static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb)
9971c6fdbd8SKent Overstreet {
9981c6fdbd8SKent Overstreet 	struct bch_sb *newest =
9991c6fdbd8SKent Overstreet 		le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb;
10001c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi = bch2_sb_get_members(newest);
10011c6fdbd8SKent Overstreet 
10021c6fdbd8SKent Overstreet 	if (!uuid_equal(&fs->uuid, &sb->uuid))
10031c6fdbd8SKent Overstreet 		return "device not a member of filesystem";
10041c6fdbd8SKent Overstreet 
10051c6fdbd8SKent Overstreet 	if (!bch2_dev_exists(newest, mi, sb->dev_idx))
10061c6fdbd8SKent Overstreet 		return "device has been removed";
10071c6fdbd8SKent Overstreet 
10081c6fdbd8SKent Overstreet 	if (fs->block_size != sb->block_size)
10091c6fdbd8SKent Overstreet 		return "mismatched block size";
10101c6fdbd8SKent Overstreet 
10111c6fdbd8SKent Overstreet 	return NULL;
10121c6fdbd8SKent Overstreet }
10131c6fdbd8SKent Overstreet 
10141c6fdbd8SKent Overstreet /* Device startup/shutdown: */
10151c6fdbd8SKent Overstreet 
10161c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *kobj)
10171c6fdbd8SKent Overstreet {
10181c6fdbd8SKent Overstreet 	struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj);
10191c6fdbd8SKent Overstreet 
10201c6fdbd8SKent Overstreet 	kfree(ca);
10211c6fdbd8SKent Overstreet }
10221c6fdbd8SKent Overstreet 
10231c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *ca)
10241c6fdbd8SKent Overstreet {
102559a74051SKent Overstreet 	bch2_dev_allocator_stop(ca);
102659a74051SKent Overstreet 
10271c6fdbd8SKent Overstreet 	cancel_work_sync(&ca->io_error_work);
10281c6fdbd8SKent Overstreet 
10291c6fdbd8SKent Overstreet 	if (ca->kobj.state_in_sysfs &&
10301c6fdbd8SKent Overstreet 	    ca->disk_sb.bdev)
10311c6fdbd8SKent Overstreet 		sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
10321c6fdbd8SKent Overstreet 
10331c6fdbd8SKent Overstreet 	if (ca->kobj.state_in_sysfs)
10341c6fdbd8SKent Overstreet 		kobject_del(&ca->kobj);
10351c6fdbd8SKent Overstreet 
10361c6fdbd8SKent Overstreet 	bch2_free_super(&ca->disk_sb);
10371c6fdbd8SKent Overstreet 	bch2_dev_journal_exit(ca);
10381c6fdbd8SKent Overstreet 
10391c6fdbd8SKent Overstreet 	free_percpu(ca->io_done);
10401c6fdbd8SKent Overstreet 	bioset_exit(&ca->replica_set);
10411c6fdbd8SKent Overstreet 	bch2_dev_buckets_free(ca);
1042d1170ce5SKent Overstreet 	free_page((unsigned long) ca->sb_read_scratch);
10431c6fdbd8SKent Overstreet 
10441c6fdbd8SKent Overstreet 	bch2_time_stats_exit(&ca->io_latency[WRITE]);
10451c6fdbd8SKent Overstreet 	bch2_time_stats_exit(&ca->io_latency[READ]);
10461c6fdbd8SKent Overstreet 
10471c6fdbd8SKent Overstreet 	percpu_ref_exit(&ca->io_ref);
10481c6fdbd8SKent Overstreet 	percpu_ref_exit(&ca->ref);
10491c6fdbd8SKent Overstreet 	kobject_put(&ca->kobj);
10501c6fdbd8SKent Overstreet }
10511c6fdbd8SKent Overstreet 
10521c6fdbd8SKent Overstreet static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca)
10531c6fdbd8SKent Overstreet {
10541c6fdbd8SKent Overstreet 
10551c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
10561c6fdbd8SKent Overstreet 
10571c6fdbd8SKent Overstreet 	if (percpu_ref_is_zero(&ca->io_ref))
10581c6fdbd8SKent Overstreet 		return;
10591c6fdbd8SKent Overstreet 
10601c6fdbd8SKent Overstreet 	__bch2_dev_read_only(c, ca);
10611c6fdbd8SKent Overstreet 
10621c6fdbd8SKent Overstreet 	reinit_completion(&ca->io_ref_completion);
10631c6fdbd8SKent Overstreet 	percpu_ref_kill(&ca->io_ref);
10641c6fdbd8SKent Overstreet 	wait_for_completion(&ca->io_ref_completion);
10651c6fdbd8SKent Overstreet 
10661c6fdbd8SKent Overstreet 	if (ca->kobj.state_in_sysfs) {
10671c6fdbd8SKent Overstreet 		sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs");
10681c6fdbd8SKent Overstreet 		sysfs_remove_link(&ca->kobj, "block");
10691c6fdbd8SKent Overstreet 	}
10701c6fdbd8SKent Overstreet 
10711c6fdbd8SKent Overstreet 	bch2_free_super(&ca->disk_sb);
10721c6fdbd8SKent Overstreet 	bch2_dev_journal_exit(ca);
10731c6fdbd8SKent Overstreet }
10741c6fdbd8SKent Overstreet 
10751c6fdbd8SKent Overstreet static void bch2_dev_ref_complete(struct percpu_ref *ref)
10761c6fdbd8SKent Overstreet {
10771c6fdbd8SKent Overstreet 	struct bch_dev *ca = container_of(ref, struct bch_dev, ref);
10781c6fdbd8SKent Overstreet 
10791c6fdbd8SKent Overstreet 	complete(&ca->ref_completion);
10801c6fdbd8SKent Overstreet }
10811c6fdbd8SKent Overstreet 
10821c6fdbd8SKent Overstreet static void bch2_dev_io_ref_complete(struct percpu_ref *ref)
10831c6fdbd8SKent Overstreet {
10841c6fdbd8SKent Overstreet 	struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref);
10851c6fdbd8SKent Overstreet 
10861c6fdbd8SKent Overstreet 	complete(&ca->io_ref_completion);
10871c6fdbd8SKent Overstreet }
10881c6fdbd8SKent Overstreet 
10891c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca)
10901c6fdbd8SKent Overstreet {
10911c6fdbd8SKent Overstreet 	int ret;
10921c6fdbd8SKent Overstreet 
10931c6fdbd8SKent Overstreet 	if (!c->kobj.state_in_sysfs)
10941c6fdbd8SKent Overstreet 		return 0;
10951c6fdbd8SKent Overstreet 
10961c6fdbd8SKent Overstreet 	if (!ca->kobj.state_in_sysfs) {
10971c6fdbd8SKent Overstreet 		ret = kobject_add(&ca->kobj, &c->kobj,
10981c6fdbd8SKent Overstreet 				  "dev-%u", ca->dev_idx);
10991c6fdbd8SKent Overstreet 		if (ret)
11001c6fdbd8SKent Overstreet 			return ret;
11011c6fdbd8SKent Overstreet 	}
11021c6fdbd8SKent Overstreet 
11031c6fdbd8SKent Overstreet 	if (ca->disk_sb.bdev) {
11041c6fdbd8SKent Overstreet 		struct kobject *block = bdev_kobj(ca->disk_sb.bdev);
11051c6fdbd8SKent Overstreet 
11061c6fdbd8SKent Overstreet 		ret = sysfs_create_link(block, &ca->kobj, "bcachefs");
11071c6fdbd8SKent Overstreet 		if (ret)
11081c6fdbd8SKent Overstreet 			return ret;
11091c6fdbd8SKent Overstreet 
11101c6fdbd8SKent Overstreet 		ret = sysfs_create_link(&ca->kobj, block, "block");
11111c6fdbd8SKent Overstreet 		if (ret)
11121c6fdbd8SKent Overstreet 			return ret;
11131c6fdbd8SKent Overstreet 	}
11141c6fdbd8SKent Overstreet 
11151c6fdbd8SKent Overstreet 	return 0;
11161c6fdbd8SKent Overstreet }
11171c6fdbd8SKent Overstreet 
11181c6fdbd8SKent Overstreet static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c,
11191c6fdbd8SKent Overstreet 					struct bch_member *member)
11201c6fdbd8SKent Overstreet {
11211c6fdbd8SKent Overstreet 	struct bch_dev *ca;
11221c6fdbd8SKent Overstreet 
11231c6fdbd8SKent Overstreet 	ca = kzalloc(sizeof(*ca), GFP_KERNEL);
11241c6fdbd8SKent Overstreet 	if (!ca)
11251c6fdbd8SKent Overstreet 		return NULL;
11261c6fdbd8SKent Overstreet 
11271c6fdbd8SKent Overstreet 	kobject_init(&ca->kobj, &bch2_dev_ktype);
11281c6fdbd8SKent Overstreet 	init_completion(&ca->ref_completion);
11291c6fdbd8SKent Overstreet 	init_completion(&ca->io_ref_completion);
11301c6fdbd8SKent Overstreet 
11311c6fdbd8SKent Overstreet 	init_rwsem(&ca->bucket_lock);
11321c6fdbd8SKent Overstreet 
11331c6fdbd8SKent Overstreet 	INIT_WORK(&ca->io_error_work, bch2_io_error_work);
11341c6fdbd8SKent Overstreet 
11351c6fdbd8SKent Overstreet 	bch2_time_stats_init(&ca->io_latency[READ]);
11361c6fdbd8SKent Overstreet 	bch2_time_stats_init(&ca->io_latency[WRITE]);
11371c6fdbd8SKent Overstreet 
11381c6fdbd8SKent Overstreet 	ca->mi = bch2_mi_to_cpu(member);
11391c6fdbd8SKent Overstreet 	ca->uuid = member->uuid;
11401c6fdbd8SKent Overstreet 
11411c6fdbd8SKent Overstreet 	if (opt_defined(c->opts, discard))
11421c6fdbd8SKent Overstreet 		ca->mi.discard = opt_get(c->opts, discard);
11431c6fdbd8SKent Overstreet 
11441c6fdbd8SKent Overstreet 	if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete,
11451c6fdbd8SKent Overstreet 			    0, GFP_KERNEL) ||
11461c6fdbd8SKent Overstreet 	    percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete,
11471c6fdbd8SKent Overstreet 			    PERCPU_REF_INIT_DEAD, GFP_KERNEL) ||
1148d1170ce5SKent Overstreet 	    !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) ||
11491c6fdbd8SKent Overstreet 	    bch2_dev_buckets_alloc(c, ca) ||
11501c6fdbd8SKent Overstreet 	    bioset_init(&ca->replica_set, 4,
11511c6fdbd8SKent Overstreet 			offsetof(struct bch_write_bio, bio), 0) ||
11521c6fdbd8SKent Overstreet 	    !(ca->io_done	= alloc_percpu(*ca->io_done)))
11531c6fdbd8SKent Overstreet 		goto err;
11541c6fdbd8SKent Overstreet 
11551c6fdbd8SKent Overstreet 	return ca;
11561c6fdbd8SKent Overstreet err:
11571c6fdbd8SKent Overstreet 	bch2_dev_free(ca);
11581c6fdbd8SKent Overstreet 	return NULL;
11591c6fdbd8SKent Overstreet }
11601c6fdbd8SKent Overstreet 
11611c6fdbd8SKent Overstreet static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca,
11621c6fdbd8SKent Overstreet 			    unsigned dev_idx)
11631c6fdbd8SKent Overstreet {
11641c6fdbd8SKent Overstreet 	ca->dev_idx = dev_idx;
11651c6fdbd8SKent Overstreet 	__set_bit(ca->dev_idx, ca->self.d);
11661c6fdbd8SKent Overstreet 	scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx);
11671c6fdbd8SKent Overstreet 
11681c6fdbd8SKent Overstreet 	ca->fs = c;
11691c6fdbd8SKent Overstreet 	rcu_assign_pointer(c->devs[ca->dev_idx], ca);
11701c6fdbd8SKent Overstreet 
11711c6fdbd8SKent Overstreet 	if (bch2_dev_sysfs_online(c, ca))
11721c6fdbd8SKent Overstreet 		pr_warn("error creating sysfs objects");
11731c6fdbd8SKent Overstreet }
11741c6fdbd8SKent Overstreet 
11751c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx)
11761c6fdbd8SKent Overstreet {
11771c6fdbd8SKent Overstreet 	struct bch_member *member =
11781c6fdbd8SKent Overstreet 		bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx;
11791c6fdbd8SKent Overstreet 	struct bch_dev *ca = NULL;
11801c6fdbd8SKent Overstreet 	int ret = 0;
11811c6fdbd8SKent Overstreet 
11821c6fdbd8SKent Overstreet 	pr_verbose_init(c->opts, "");
11831c6fdbd8SKent Overstreet 
11841c6fdbd8SKent Overstreet 	if (bch2_fs_init_fault("dev_alloc"))
11851c6fdbd8SKent Overstreet 		goto err;
11861c6fdbd8SKent Overstreet 
11871c6fdbd8SKent Overstreet 	ca = __bch2_dev_alloc(c, member);
11881c6fdbd8SKent Overstreet 	if (!ca)
11891c6fdbd8SKent Overstreet 		goto err;
11901c6fdbd8SKent Overstreet 
1191220d2062SKent Overstreet 	ca->fs = c;
1192220d2062SKent Overstreet 
11932436cb9fSKent Overstreet 	if (ca->mi.state == BCH_MEMBER_STATE_rw &&
119459a74051SKent Overstreet 	    bch2_dev_allocator_start(ca)) {
119559a74051SKent Overstreet 		bch2_dev_free(ca);
119659a74051SKent Overstreet 		goto err;
119759a74051SKent Overstreet 	}
119859a74051SKent Overstreet 
11991c6fdbd8SKent Overstreet 	bch2_dev_attach(c, ca, dev_idx);
12001c6fdbd8SKent Overstreet out:
12011c6fdbd8SKent Overstreet 	pr_verbose_init(c->opts, "ret %i", ret);
12021c6fdbd8SKent Overstreet 	return ret;
12031c6fdbd8SKent Overstreet err:
12041c6fdbd8SKent Overstreet 	if (ca)
12051c6fdbd8SKent Overstreet 		bch2_dev_free(ca);
12061c6fdbd8SKent Overstreet 	ret = -ENOMEM;
12071c6fdbd8SKent Overstreet 	goto out;
12081c6fdbd8SKent Overstreet }
12091c6fdbd8SKent Overstreet 
12101c6fdbd8SKent Overstreet static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb)
12111c6fdbd8SKent Overstreet {
12121c6fdbd8SKent Overstreet 	unsigned ret;
12131c6fdbd8SKent Overstreet 
12141c6fdbd8SKent Overstreet 	if (bch2_dev_is_online(ca)) {
12151c6fdbd8SKent Overstreet 		bch_err(ca, "already have device online in slot %u",
12161c6fdbd8SKent Overstreet 			sb->sb->dev_idx);
12171c6fdbd8SKent Overstreet 		return -EINVAL;
12181c6fdbd8SKent Overstreet 	}
12191c6fdbd8SKent Overstreet 
12201c6fdbd8SKent Overstreet 	if (get_capacity(sb->bdev->bd_disk) <
12211c6fdbd8SKent Overstreet 	    ca->mi.bucket_size * ca->mi.nbuckets) {
12221c6fdbd8SKent Overstreet 		bch_err(ca, "cannot online: device too small");
12231c6fdbd8SKent Overstreet 		return -EINVAL;
12241c6fdbd8SKent Overstreet 	}
12251c6fdbd8SKent Overstreet 
12261c6fdbd8SKent Overstreet 	BUG_ON(!percpu_ref_is_zero(&ca->io_ref));
12271c6fdbd8SKent Overstreet 
12281c6fdbd8SKent Overstreet 	if (get_capacity(sb->bdev->bd_disk) <
12291c6fdbd8SKent Overstreet 	    ca->mi.bucket_size * ca->mi.nbuckets) {
12301c6fdbd8SKent Overstreet 		bch_err(ca, "device too small");
12311c6fdbd8SKent Overstreet 		return -EINVAL;
12321c6fdbd8SKent Overstreet 	}
12331c6fdbd8SKent Overstreet 
12341c6fdbd8SKent Overstreet 	ret = bch2_dev_journal_init(ca, sb->sb);
12351c6fdbd8SKent Overstreet 	if (ret)
12361c6fdbd8SKent Overstreet 		return ret;
12371c6fdbd8SKent Overstreet 
12381c6fdbd8SKent Overstreet 	/* Commit: */
12391c6fdbd8SKent Overstreet 	ca->disk_sb = *sb;
12401c6fdbd8SKent Overstreet 	memset(sb, 0, sizeof(*sb));
12411c6fdbd8SKent Overstreet 
12421c6fdbd8SKent Overstreet 	percpu_ref_reinit(&ca->io_ref);
12431c6fdbd8SKent Overstreet 
12441c6fdbd8SKent Overstreet 	return 0;
12451c6fdbd8SKent Overstreet }
12461c6fdbd8SKent Overstreet 
12471c6fdbd8SKent Overstreet static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb)
12481c6fdbd8SKent Overstreet {
12491c6fdbd8SKent Overstreet 	struct bch_dev *ca;
12501c6fdbd8SKent Overstreet 	int ret;
12511c6fdbd8SKent Overstreet 
12521c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
12531c6fdbd8SKent Overstreet 
12541c6fdbd8SKent Overstreet 	if (le64_to_cpu(sb->sb->seq) >
12551c6fdbd8SKent Overstreet 	    le64_to_cpu(c->disk_sb.sb->seq))
12561c6fdbd8SKent Overstreet 		bch2_sb_to_fs(c, sb->sb);
12571c6fdbd8SKent Overstreet 
12581c6fdbd8SKent Overstreet 	BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices ||
12591c6fdbd8SKent Overstreet 	       !c->devs[sb->sb->dev_idx]);
12601c6fdbd8SKent Overstreet 
12611c6fdbd8SKent Overstreet 	ca = bch_dev_locked(c, sb->sb->dev_idx);
12621c6fdbd8SKent Overstreet 
12631c6fdbd8SKent Overstreet 	ret = __bch2_dev_attach_bdev(ca, sb);
12641c6fdbd8SKent Overstreet 	if (ret)
12651c6fdbd8SKent Overstreet 		return ret;
12661c6fdbd8SKent Overstreet 
12671c6fdbd8SKent Overstreet 	bch2_dev_sysfs_online(c, ca);
12681c6fdbd8SKent Overstreet 
12691c6fdbd8SKent Overstreet 	if (c->sb.nr_devices == 1)
12701c6fdbd8SKent Overstreet 		snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev);
12711c6fdbd8SKent Overstreet 	snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev);
12721c6fdbd8SKent Overstreet 
12731c6fdbd8SKent Overstreet 	rebalance_wakeup(c);
12741c6fdbd8SKent Overstreet 	return 0;
12751c6fdbd8SKent Overstreet }
12761c6fdbd8SKent Overstreet 
12771c6fdbd8SKent Overstreet /* Device management: */
12781c6fdbd8SKent Overstreet 
12791c6fdbd8SKent Overstreet /*
12801c6fdbd8SKent Overstreet  * Note: this function is also used by the error paths - when a particular
12811c6fdbd8SKent Overstreet  * device sees an error, we call it to determine whether we can just set the
12821c6fdbd8SKent Overstreet  * device RO, or - if this function returns false - we'll set the whole
12831c6fdbd8SKent Overstreet  * filesystem RO:
12841c6fdbd8SKent Overstreet  *
12851c6fdbd8SKent Overstreet  * XXX: maybe we should be more explicit about whether we're changing state
12861c6fdbd8SKent Overstreet  * because we got an error or what have you?
12871c6fdbd8SKent Overstreet  */
12881c6fdbd8SKent Overstreet bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca,
12891c6fdbd8SKent Overstreet 			    enum bch_member_state new_state, int flags)
12901c6fdbd8SKent Overstreet {
12911c6fdbd8SKent Overstreet 	struct bch_devs_mask new_online_devs;
12921c6fdbd8SKent Overstreet 	struct bch_dev *ca2;
12931c6fdbd8SKent Overstreet 	int i, nr_rw = 0, required;
12941c6fdbd8SKent Overstreet 
12951c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
12961c6fdbd8SKent Overstreet 
12971c6fdbd8SKent Overstreet 	switch (new_state) {
12982436cb9fSKent Overstreet 	case BCH_MEMBER_STATE_rw:
12991c6fdbd8SKent Overstreet 		return true;
13002436cb9fSKent Overstreet 	case BCH_MEMBER_STATE_ro:
13012436cb9fSKent Overstreet 		if (ca->mi.state != BCH_MEMBER_STATE_rw)
13021c6fdbd8SKent Overstreet 			return true;
13031c6fdbd8SKent Overstreet 
13041c6fdbd8SKent Overstreet 		/* do we have enough devices to write to?  */
13051c6fdbd8SKent Overstreet 		for_each_member_device(ca2, c, i)
13061c6fdbd8SKent Overstreet 			if (ca2 != ca)
13072436cb9fSKent Overstreet 				nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw;
13081c6fdbd8SKent Overstreet 
13091c6fdbd8SKent Overstreet 		required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED)
13101c6fdbd8SKent Overstreet 			       ? c->opts.metadata_replicas
13111c6fdbd8SKent Overstreet 			       : c->opts.metadata_replicas_required,
13121c6fdbd8SKent Overstreet 			       !(flags & BCH_FORCE_IF_DATA_DEGRADED)
13131c6fdbd8SKent Overstreet 			       ? c->opts.data_replicas
13141c6fdbd8SKent Overstreet 			       : c->opts.data_replicas_required);
13151c6fdbd8SKent Overstreet 
13161c6fdbd8SKent Overstreet 		return nr_rw >= required;
13172436cb9fSKent Overstreet 	case BCH_MEMBER_STATE_failed:
13182436cb9fSKent Overstreet 	case BCH_MEMBER_STATE_spare:
13192436cb9fSKent Overstreet 		if (ca->mi.state != BCH_MEMBER_STATE_rw &&
13202436cb9fSKent Overstreet 		    ca->mi.state != BCH_MEMBER_STATE_ro)
13211c6fdbd8SKent Overstreet 			return true;
13221c6fdbd8SKent Overstreet 
13231c6fdbd8SKent Overstreet 		/* do we have enough devices to read from?  */
13241c6fdbd8SKent Overstreet 		new_online_devs = bch2_online_devs(c);
13251c6fdbd8SKent Overstreet 		__clear_bit(ca->dev_idx, new_online_devs.d);
13261c6fdbd8SKent Overstreet 
1327fcb3431bSKent Overstreet 		return bch2_have_enough_devs(c, new_online_devs, flags, false);
13281c6fdbd8SKent Overstreet 	default:
13291c6fdbd8SKent Overstreet 		BUG();
13301c6fdbd8SKent Overstreet 	}
13311c6fdbd8SKent Overstreet }
13321c6fdbd8SKent Overstreet 
13331c6fdbd8SKent Overstreet static bool bch2_fs_may_start(struct bch_fs *c)
13341c6fdbd8SKent Overstreet {
13351c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
13361c6fdbd8SKent Overstreet 	struct bch_dev *ca;
1337fcb3431bSKent Overstreet 	unsigned i, flags = 0;
13381c6fdbd8SKent Overstreet 
1339fcb3431bSKent Overstreet 	if (c->opts.very_degraded)
1340fcb3431bSKent Overstreet 		flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST;
1341fcb3431bSKent Overstreet 
1342fcb3431bSKent Overstreet 	if (c->opts.degraded)
1343fcb3431bSKent Overstreet 		flags |= BCH_FORCE_IF_DEGRADED;
1344fcb3431bSKent Overstreet 
1345fcb3431bSKent Overstreet 	if (!c->opts.degraded &&
1346fcb3431bSKent Overstreet 	    !c->opts.very_degraded) {
13471c6fdbd8SKent Overstreet 		mutex_lock(&c->sb_lock);
13481c6fdbd8SKent Overstreet 		mi = bch2_sb_get_members(c->disk_sb.sb);
13491c6fdbd8SKent Overstreet 
13501c6fdbd8SKent Overstreet 		for (i = 0; i < c->disk_sb.sb->nr_devices; i++) {
13511c6fdbd8SKent Overstreet 			if (!bch2_dev_exists(c->disk_sb.sb, mi, i))
13521c6fdbd8SKent Overstreet 				continue;
13531c6fdbd8SKent Overstreet 
13541c6fdbd8SKent Overstreet 			ca = bch_dev_locked(c, i);
13551c6fdbd8SKent Overstreet 
13561c6fdbd8SKent Overstreet 			if (!bch2_dev_is_online(ca) &&
13572436cb9fSKent Overstreet 			    (ca->mi.state == BCH_MEMBER_STATE_rw ||
13582436cb9fSKent Overstreet 			     ca->mi.state == BCH_MEMBER_STATE_ro)) {
13591c6fdbd8SKent Overstreet 				mutex_unlock(&c->sb_lock);
13601c6fdbd8SKent Overstreet 				return false;
13611c6fdbd8SKent Overstreet 			}
13621c6fdbd8SKent Overstreet 		}
13631c6fdbd8SKent Overstreet 		mutex_unlock(&c->sb_lock);
13641c6fdbd8SKent Overstreet 	}
13651c6fdbd8SKent Overstreet 
1366fcb3431bSKent Overstreet 	return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true);
13671c6fdbd8SKent Overstreet }
13681c6fdbd8SKent Overstreet 
13691c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
13701c6fdbd8SKent Overstreet {
13711c6fdbd8SKent Overstreet 	/*
137274ed7e56SKent Overstreet 	 * Device going read only means the copygc reserve get smaller, so we
137374ed7e56SKent Overstreet 	 * don't want that happening while copygc is in progress:
137474ed7e56SKent Overstreet 	 */
137574ed7e56SKent Overstreet 	bch2_copygc_stop(c);
137674ed7e56SKent Overstreet 
137774ed7e56SKent Overstreet 	/*
13781c6fdbd8SKent Overstreet 	 * The allocator thread itself allocates btree nodes, so stop it first:
13791c6fdbd8SKent Overstreet 	 */
13801c6fdbd8SKent Overstreet 	bch2_dev_allocator_stop(ca);
13811c6fdbd8SKent Overstreet 	bch2_dev_allocator_remove(c, ca);
13821c6fdbd8SKent Overstreet 	bch2_dev_journal_stop(&c->journal, ca);
138374ed7e56SKent Overstreet 
138474ed7e56SKent Overstreet 	bch2_copygc_start(c);
13851c6fdbd8SKent Overstreet }
13861c6fdbd8SKent Overstreet 
13871c6fdbd8SKent Overstreet static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
13881c6fdbd8SKent Overstreet {
13891c6fdbd8SKent Overstreet 	lockdep_assert_held(&c->state_lock);
13901c6fdbd8SKent Overstreet 
13912436cb9fSKent Overstreet 	BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw);
13921c6fdbd8SKent Overstreet 
13931c6fdbd8SKent Overstreet 	bch2_dev_allocator_add(c, ca);
13941c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
13951c6fdbd8SKent Overstreet 
13961c6fdbd8SKent Overstreet 	if (bch2_dev_allocator_start(ca))
13971c6fdbd8SKent Overstreet 		return "error starting allocator thread";
13981c6fdbd8SKent Overstreet 
13991c6fdbd8SKent Overstreet 	return NULL;
14001c6fdbd8SKent Overstreet }
14011c6fdbd8SKent Overstreet 
14021c6fdbd8SKent Overstreet int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
14031c6fdbd8SKent Overstreet 			 enum bch_member_state new_state, int flags)
14041c6fdbd8SKent Overstreet {
14051c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
14061c6fdbd8SKent Overstreet 	int ret = 0;
14071c6fdbd8SKent Overstreet 
14081c6fdbd8SKent Overstreet 	if (ca->mi.state == new_state)
14091c6fdbd8SKent Overstreet 		return 0;
14101c6fdbd8SKent Overstreet 
14111c6fdbd8SKent Overstreet 	if (!bch2_dev_state_allowed(c, ca, new_state, flags))
14121c6fdbd8SKent Overstreet 		return -EINVAL;
14131c6fdbd8SKent Overstreet 
14142436cb9fSKent Overstreet 	if (new_state != BCH_MEMBER_STATE_rw)
14151c6fdbd8SKent Overstreet 		__bch2_dev_read_only(c, ca);
14161c6fdbd8SKent Overstreet 
14172436cb9fSKent Overstreet 	bch_notice(ca, "%s", bch2_member_states[new_state]);
14181c6fdbd8SKent Overstreet 
14191c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
14201c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
14211c6fdbd8SKent Overstreet 	SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state);
14221c6fdbd8SKent Overstreet 	bch2_write_super(c);
14231c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
14241c6fdbd8SKent Overstreet 
14252436cb9fSKent Overstreet 	if (new_state == BCH_MEMBER_STATE_rw &&
14261c6fdbd8SKent Overstreet 	    __bch2_dev_read_write(c, ca))
14271c6fdbd8SKent Overstreet 		ret = -ENOMEM;
14281c6fdbd8SKent Overstreet 
14291c6fdbd8SKent Overstreet 	rebalance_wakeup(c);
14301c6fdbd8SKent Overstreet 
14311c6fdbd8SKent Overstreet 	return ret;
14321c6fdbd8SKent Overstreet }
14331c6fdbd8SKent Overstreet 
14341c6fdbd8SKent Overstreet int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
14351c6fdbd8SKent Overstreet 		       enum bch_member_state new_state, int flags)
14361c6fdbd8SKent Overstreet {
14371c6fdbd8SKent Overstreet 	int ret;
14381c6fdbd8SKent Overstreet 
14391ada1606SKent Overstreet 	down_write(&c->state_lock);
14401c6fdbd8SKent Overstreet 	ret = __bch2_dev_set_state(c, ca, new_state, flags);
14411ada1606SKent Overstreet 	up_write(&c->state_lock);
14421c6fdbd8SKent Overstreet 
14431c6fdbd8SKent Overstreet 	return ret;
14441c6fdbd8SKent Overstreet }
14451c6fdbd8SKent Overstreet 
14461c6fdbd8SKent Overstreet /* Device add/removal: */
14471c6fdbd8SKent Overstreet 
1448c0ebe3e4SKent Overstreet static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca)
14495d20ba48SKent Overstreet {
14505d20ba48SKent Overstreet 	struct btree_trans trans;
14515d20ba48SKent Overstreet 	size_t i;
14525d20ba48SKent Overstreet 	int ret;
14535d20ba48SKent Overstreet 
14545d20ba48SKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
14555d20ba48SKent Overstreet 
14565d20ba48SKent Overstreet 	for (i = 0; i < ca->mi.nbuckets; i++) {
14575d20ba48SKent Overstreet 		ret = bch2_btree_key_cache_flush(&trans,
145841f8b09eSKent Overstreet 				BTREE_ID_alloc, POS(ca->dev_idx, i));
14595d20ba48SKent Overstreet 		if (ret)
14605d20ba48SKent Overstreet 			break;
14615d20ba48SKent Overstreet 	}
14625d20ba48SKent Overstreet 	bch2_trans_exit(&trans);
14635d20ba48SKent Overstreet 
14645d20ba48SKent Overstreet 	if (ret)
14655d20ba48SKent Overstreet 		return ret;
14665d20ba48SKent Overstreet 
146741f8b09eSKent Overstreet 	return bch2_btree_delete_range(c, BTREE_ID_alloc,
14685d20ba48SKent Overstreet 				       POS(ca->dev_idx, 0),
14695d20ba48SKent Overstreet 				       POS(ca->dev_idx + 1, 0),
14705d20ba48SKent Overstreet 				       NULL);
14715d20ba48SKent Overstreet }
14725d20ba48SKent Overstreet 
14731c6fdbd8SKent Overstreet int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags)
14741c6fdbd8SKent Overstreet {
14751c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
14761c6fdbd8SKent Overstreet 	unsigned dev_idx = ca->dev_idx, data;
14771c6fdbd8SKent Overstreet 	int ret = -EINVAL;
14781c6fdbd8SKent Overstreet 
14791ada1606SKent Overstreet 	down_write(&c->state_lock);
14801c6fdbd8SKent Overstreet 
148131ba2cd3SKent Overstreet 	/*
148231ba2cd3SKent Overstreet 	 * We consume a reference to ca->ref, regardless of whether we succeed
148331ba2cd3SKent Overstreet 	 * or fail:
148431ba2cd3SKent Overstreet 	 */
148531ba2cd3SKent Overstreet 	percpu_ref_put(&ca->ref);
14861c6fdbd8SKent Overstreet 
14872436cb9fSKent Overstreet 	if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
14881c6fdbd8SKent Overstreet 		bch_err(ca, "Cannot remove without losing data");
14891c6fdbd8SKent Overstreet 		goto err;
14901c6fdbd8SKent Overstreet 	}
14911c6fdbd8SKent Overstreet 
14921c6fdbd8SKent Overstreet 	__bch2_dev_read_only(c, ca);
14931c6fdbd8SKent Overstreet 
14941c6fdbd8SKent Overstreet 	ret = bch2_dev_data_drop(c, ca->dev_idx, flags);
14951c6fdbd8SKent Overstreet 	if (ret) {
14961c6fdbd8SKent Overstreet 		bch_err(ca, "Remove failed: error %i dropping data", ret);
14971c6fdbd8SKent Overstreet 		goto err;
14981c6fdbd8SKent Overstreet 	}
14991c6fdbd8SKent Overstreet 
15001c6fdbd8SKent Overstreet 	ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx);
15011c6fdbd8SKent Overstreet 	if (ret) {
15021c6fdbd8SKent Overstreet 		bch_err(ca, "Remove failed: error %i flushing journal", ret);
15031c6fdbd8SKent Overstreet 		goto err;
15041c6fdbd8SKent Overstreet 	}
15051c6fdbd8SKent Overstreet 
15065d20ba48SKent Overstreet 	ret = bch2_dev_remove_alloc(c, ca);
15071c6fdbd8SKent Overstreet 	if (ret) {
15081c6fdbd8SKent Overstreet 		bch_err(ca, "Remove failed, error deleting alloc info");
15091c6fdbd8SKent Overstreet 		goto err;
15101c6fdbd8SKent Overstreet 	}
15111c6fdbd8SKent Overstreet 
15121c6fdbd8SKent Overstreet 	/*
15131c6fdbd8SKent Overstreet 	 * must flush all existing journal entries, they might have
15141c6fdbd8SKent Overstreet 	 * (overwritten) keys that point to the device we're removing:
15151c6fdbd8SKent Overstreet 	 */
15161c6fdbd8SKent Overstreet 	bch2_journal_flush_all_pins(&c->journal);
151731ba2cd3SKent Overstreet 	/*
151831ba2cd3SKent Overstreet 	 * hack to ensure bch2_replicas_gc2() clears out entries to this device
151931ba2cd3SKent Overstreet 	 */
152031ba2cd3SKent Overstreet 	bch2_journal_meta(&c->journal);
15211c6fdbd8SKent Overstreet 	ret = bch2_journal_error(&c->journal);
15221c6fdbd8SKent Overstreet 	if (ret) {
15231c6fdbd8SKent Overstreet 		bch_err(ca, "Remove failed, journal error");
15241c6fdbd8SKent Overstreet 		goto err;
15251c6fdbd8SKent Overstreet 	}
15261c6fdbd8SKent Overstreet 
152731ba2cd3SKent Overstreet 	ret = bch2_replicas_gc2(c);
152831ba2cd3SKent Overstreet 	if (ret) {
152931ba2cd3SKent Overstreet 		bch_err(ca, "Remove failed: error %i from replicas gc", ret);
153031ba2cd3SKent Overstreet 		goto err;
153131ba2cd3SKent Overstreet 	}
153231ba2cd3SKent Overstreet 
153331ba2cd3SKent Overstreet 	data = bch2_dev_has_data(c, ca);
153431ba2cd3SKent Overstreet 	if (data) {
153531ba2cd3SKent Overstreet 		char data_has_str[100];
153631ba2cd3SKent Overstreet 
153731ba2cd3SKent Overstreet 		bch2_flags_to_text(&PBUF(data_has_str),
153831ba2cd3SKent Overstreet 				   bch2_data_types, data);
153931ba2cd3SKent Overstreet 		bch_err(ca, "Remove failed, still has data (%s)", data_has_str);
154031ba2cd3SKent Overstreet 		ret = -EBUSY;
154131ba2cd3SKent Overstreet 		goto err;
154231ba2cd3SKent Overstreet 	}
154331ba2cd3SKent Overstreet 
15441c6fdbd8SKent Overstreet 	__bch2_dev_offline(c, ca);
15451c6fdbd8SKent Overstreet 
15461c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
15471c6fdbd8SKent Overstreet 	rcu_assign_pointer(c->devs[ca->dev_idx], NULL);
15481c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
15491c6fdbd8SKent Overstreet 
15501c6fdbd8SKent Overstreet 	percpu_ref_kill(&ca->ref);
15511c6fdbd8SKent Overstreet 	wait_for_completion(&ca->ref_completion);
15521c6fdbd8SKent Overstreet 
15531c6fdbd8SKent Overstreet 	bch2_dev_free(ca);
15541c6fdbd8SKent Overstreet 
15551c6fdbd8SKent Overstreet 	/*
15561c6fdbd8SKent Overstreet 	 * Free this device's slot in the bch_member array - all pointers to
15571c6fdbd8SKent Overstreet 	 * this device must be gone:
15581c6fdbd8SKent Overstreet 	 */
15591c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
15601c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
15611c6fdbd8SKent Overstreet 	memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid));
15621c6fdbd8SKent Overstreet 
15631c6fdbd8SKent Overstreet 	bch2_write_super(c);
15641c6fdbd8SKent Overstreet 
15651c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
15661ada1606SKent Overstreet 	up_write(&c->state_lock);
1567180fb49dSKent Overstreet 
1568180fb49dSKent Overstreet 	bch2_dev_usage_journal_reserve(c);
15691c6fdbd8SKent Overstreet 	return 0;
15701c6fdbd8SKent Overstreet err:
15712436cb9fSKent Overstreet 	if (ca->mi.state == BCH_MEMBER_STATE_rw &&
1572d3bb629dSKent Overstreet 	    !percpu_ref_is_zero(&ca->io_ref))
15731c6fdbd8SKent Overstreet 		__bch2_dev_read_write(c, ca);
15741ada1606SKent Overstreet 	up_write(&c->state_lock);
15751c6fdbd8SKent Overstreet 	return ret;
15761c6fdbd8SKent Overstreet }
15771c6fdbd8SKent Overstreet 
15781c6fdbd8SKent Overstreet /* Add new device to running filesystem: */
15791c6fdbd8SKent Overstreet int bch2_dev_add(struct bch_fs *c, const char *path)
15801c6fdbd8SKent Overstreet {
15811c6fdbd8SKent Overstreet 	struct bch_opts opts = bch2_opts_empty();
15821c6fdbd8SKent Overstreet 	struct bch_sb_handle sb;
15831c6fdbd8SKent Overstreet 	const char *err;
15841c6fdbd8SKent Overstreet 	struct bch_dev *ca = NULL;
15851c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
15861c6fdbd8SKent Overstreet 	struct bch_member dev_mi;
15871c6fdbd8SKent Overstreet 	unsigned dev_idx, nr_devices, u64s;
15881c6fdbd8SKent Overstreet 	int ret;
15891c6fdbd8SKent Overstreet 
15901c6fdbd8SKent Overstreet 	ret = bch2_read_super(path, &opts, &sb);
15911c6fdbd8SKent Overstreet 	if (ret)
15921c6fdbd8SKent Overstreet 		return ret;
15931c6fdbd8SKent Overstreet 
15941c6fdbd8SKent Overstreet 	err = bch2_sb_validate(&sb);
15951c6fdbd8SKent Overstreet 	if (err)
15961c6fdbd8SKent Overstreet 		return -EINVAL;
15971c6fdbd8SKent Overstreet 
15981c6fdbd8SKent Overstreet 	dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx];
15991c6fdbd8SKent Overstreet 
16001c6fdbd8SKent Overstreet 	err = bch2_dev_may_add(sb.sb, c);
16011c6fdbd8SKent Overstreet 	if (err)
16021c6fdbd8SKent Overstreet 		return -EINVAL;
16031c6fdbd8SKent Overstreet 
16041c6fdbd8SKent Overstreet 	ca = __bch2_dev_alloc(c, &dev_mi);
16051c6fdbd8SKent Overstreet 	if (!ca) {
16061c6fdbd8SKent Overstreet 		bch2_free_super(&sb);
16071c6fdbd8SKent Overstreet 		return -ENOMEM;
16081c6fdbd8SKent Overstreet 	}
16091c6fdbd8SKent Overstreet 
16101c6fdbd8SKent Overstreet 	ret = __bch2_dev_attach_bdev(ca, &sb);
16111c6fdbd8SKent Overstreet 	if (ret) {
16121c6fdbd8SKent Overstreet 		bch2_dev_free(ca);
16131c6fdbd8SKent Overstreet 		return ret;
16141c6fdbd8SKent Overstreet 	}
16151c6fdbd8SKent Overstreet 
16166eac2c2eSKent Overstreet 	/*
16176eac2c2eSKent Overstreet 	 * We want to allocate journal on the new device before adding the new
16186eac2c2eSKent Overstreet 	 * device to the filesystem because allocating after we attach requires
16196eac2c2eSKent Overstreet 	 * spinning up the allocator thread, and the allocator thread requires
16206eac2c2eSKent Overstreet 	 * doing btree writes, which if the existing devices are RO isn't going
16216eac2c2eSKent Overstreet 	 * to work
16226eac2c2eSKent Overstreet 	 *
16236eac2c2eSKent Overstreet 	 * So we have to mark where the superblocks are, but marking allocated
16246eac2c2eSKent Overstreet 	 * data normally updates the filesystem usage too, so we have to mark,
16256eac2c2eSKent Overstreet 	 * allocate the journal, reset all the marks, then remark after we
16266eac2c2eSKent Overstreet 	 * attach...
16276eac2c2eSKent Overstreet 	 */
1628bfcf840dSKent Overstreet 	bch2_mark_dev_superblock(NULL, ca, 0);
16296eac2c2eSKent Overstreet 
16301c6fdbd8SKent Overstreet 	err = "journal alloc failed";
16311c6fdbd8SKent Overstreet 	ret = bch2_dev_journal_alloc(ca);
16321c6fdbd8SKent Overstreet 	if (ret)
16331c6fdbd8SKent Overstreet 		goto err;
16341c6fdbd8SKent Overstreet 
16351ada1606SKent Overstreet 	down_write(&c->state_lock);
16361c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
16371c6fdbd8SKent Overstreet 
16381c6fdbd8SKent Overstreet 	err = "insufficient space in new superblock";
16391c6fdbd8SKent Overstreet 	ret = bch2_sb_from_fs(c, ca);
16401c6fdbd8SKent Overstreet 	if (ret)
16411c6fdbd8SKent Overstreet 		goto err_unlock;
16421c6fdbd8SKent Overstreet 
16431c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(ca->disk_sb.sb);
16441c6fdbd8SKent Overstreet 
16451c6fdbd8SKent Overstreet 	if (!bch2_sb_resize_members(&ca->disk_sb,
16461c6fdbd8SKent Overstreet 				le32_to_cpu(mi->field.u64s) +
16471c6fdbd8SKent Overstreet 				sizeof(dev_mi) / sizeof(u64))) {
16481c6fdbd8SKent Overstreet 		ret = -ENOSPC;
16491c6fdbd8SKent Overstreet 		goto err_unlock;
16501c6fdbd8SKent Overstreet 	}
16511c6fdbd8SKent Overstreet 
16521c6fdbd8SKent Overstreet 	if (dynamic_fault("bcachefs:add:no_slot"))
16531c6fdbd8SKent Overstreet 		goto no_slot;
16541c6fdbd8SKent Overstreet 
16551c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
16561c6fdbd8SKent Overstreet 	for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++)
16571c6fdbd8SKent Overstreet 		if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx))
16581c6fdbd8SKent Overstreet 			goto have_slot;
16591c6fdbd8SKent Overstreet no_slot:
16601c6fdbd8SKent Overstreet 	err = "no slots available in superblock";
16611c6fdbd8SKent Overstreet 	ret = -ENOSPC;
16621c6fdbd8SKent Overstreet 	goto err_unlock;
16631c6fdbd8SKent Overstreet 
16641c6fdbd8SKent Overstreet have_slot:
16651c6fdbd8SKent Overstreet 	nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices);
16661c6fdbd8SKent Overstreet 	u64s = (sizeof(struct bch_sb_field_members) +
16671c6fdbd8SKent Overstreet 		sizeof(struct bch_member) * nr_devices) / sizeof(u64);
16681c6fdbd8SKent Overstreet 
16691c6fdbd8SKent Overstreet 	err = "no space in superblock for member info";
16701c6fdbd8SKent Overstreet 	ret = -ENOSPC;
16711c6fdbd8SKent Overstreet 
16721c6fdbd8SKent Overstreet 	mi = bch2_sb_resize_members(&c->disk_sb, u64s);
16731c6fdbd8SKent Overstreet 	if (!mi)
16741c6fdbd8SKent Overstreet 		goto err_unlock;
16751c6fdbd8SKent Overstreet 
16761c6fdbd8SKent Overstreet 	/* success: */
16771c6fdbd8SKent Overstreet 
16781c6fdbd8SKent Overstreet 	mi->members[dev_idx] = dev_mi;
1679a420eea6STim Schlueter 	mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds());
16801c6fdbd8SKent Overstreet 	c->disk_sb.sb->nr_devices	= nr_devices;
16811c6fdbd8SKent Overstreet 
16821c6fdbd8SKent Overstreet 	ca->disk_sb.sb->dev_idx	= dev_idx;
16831c6fdbd8SKent Overstreet 	bch2_dev_attach(c, ca, dev_idx);
16841c6fdbd8SKent Overstreet 
16851c6fdbd8SKent Overstreet 	bch2_write_super(c);
16861c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
16871c6fdbd8SKent Overstreet 
1688180fb49dSKent Overstreet 	bch2_dev_usage_journal_reserve(c);
1689180fb49dSKent Overstreet 
1690bfcf840dSKent Overstreet 	err = "error marking superblock";
1691d62ab355SKent Overstreet 	ret = bch2_trans_mark_dev_sb(c, ca);
16928d6b6222SKent Overstreet 	if (ret)
1693bfcf840dSKent Overstreet 		goto err_late;
16948d6b6222SKent Overstreet 
16952436cb9fSKent Overstreet 	if (ca->mi.state == BCH_MEMBER_STATE_rw) {
16961c6fdbd8SKent Overstreet 		err = __bch2_dev_read_write(c, ca);
16971c6fdbd8SKent Overstreet 		if (err)
16981c6fdbd8SKent Overstreet 			goto err_late;
16991c6fdbd8SKent Overstreet 	}
17001c6fdbd8SKent Overstreet 
17011ada1606SKent Overstreet 	up_write(&c->state_lock);
17021c6fdbd8SKent Overstreet 	return 0;
17031c6fdbd8SKent Overstreet 
17041c6fdbd8SKent Overstreet err_unlock:
17051c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
17061ada1606SKent Overstreet 	up_write(&c->state_lock);
17071c6fdbd8SKent Overstreet err:
17081c6fdbd8SKent Overstreet 	if (ca)
17091c6fdbd8SKent Overstreet 		bch2_dev_free(ca);
17101c6fdbd8SKent Overstreet 	bch2_free_super(&sb);
17111c6fdbd8SKent Overstreet 	bch_err(c, "Unable to add device: %s", err);
17121c6fdbd8SKent Overstreet 	return ret;
17131c6fdbd8SKent Overstreet err_late:
1714bfcf840dSKent Overstreet 	up_write(&c->state_lock);
17151c6fdbd8SKent Overstreet 	bch_err(c, "Error going rw after adding device: %s", err);
17161c6fdbd8SKent Overstreet 	return -EINVAL;
17171c6fdbd8SKent Overstreet }
17181c6fdbd8SKent Overstreet 
17191c6fdbd8SKent Overstreet /* Hot add existing device to running filesystem: */
17201c6fdbd8SKent Overstreet int bch2_dev_online(struct bch_fs *c, const char *path)
17211c6fdbd8SKent Overstreet {
17221c6fdbd8SKent Overstreet 	struct bch_opts opts = bch2_opts_empty();
17231c6fdbd8SKent Overstreet 	struct bch_sb_handle sb = { NULL };
17241c6fdbd8SKent Overstreet 	struct bch_sb_field_members *mi;
17251c6fdbd8SKent Overstreet 	struct bch_dev *ca;
17261c6fdbd8SKent Overstreet 	unsigned dev_idx;
17271c6fdbd8SKent Overstreet 	const char *err;
17281c6fdbd8SKent Overstreet 	int ret;
17291c6fdbd8SKent Overstreet 
17301ada1606SKent Overstreet 	down_write(&c->state_lock);
17311c6fdbd8SKent Overstreet 
17321c6fdbd8SKent Overstreet 	ret = bch2_read_super(path, &opts, &sb);
17331c6fdbd8SKent Overstreet 	if (ret) {
17341ada1606SKent Overstreet 		up_write(&c->state_lock);
17351c6fdbd8SKent Overstreet 		return ret;
17361c6fdbd8SKent Overstreet 	}
17371c6fdbd8SKent Overstreet 
17381c6fdbd8SKent Overstreet 	dev_idx = sb.sb->dev_idx;
17391c6fdbd8SKent Overstreet 
17401c6fdbd8SKent Overstreet 	err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
17411c6fdbd8SKent Overstreet 	if (err)
17421c6fdbd8SKent Overstreet 		goto err;
17431c6fdbd8SKent Overstreet 
17441c6fdbd8SKent Overstreet 	if (bch2_dev_attach_bdev(c, &sb)) {
17451c6fdbd8SKent Overstreet 		err = "bch2_dev_attach_bdev() error";
17461c6fdbd8SKent Overstreet 		goto err;
17471c6fdbd8SKent Overstreet 	}
17481c6fdbd8SKent Overstreet 
17491c6fdbd8SKent Overstreet 	ca = bch_dev_locked(c, dev_idx);
1750bfcf840dSKent Overstreet 
1751d62ab355SKent Overstreet 	if (bch2_trans_mark_dev_sb(c, ca)) {
1752bfcf840dSKent Overstreet 		err = "bch2_trans_mark_dev_sb() error";
1753bfcf840dSKent Overstreet 		goto err;
1754bfcf840dSKent Overstreet 	}
1755bfcf840dSKent Overstreet 
17562436cb9fSKent Overstreet 	if (ca->mi.state == BCH_MEMBER_STATE_rw) {
17571c6fdbd8SKent Overstreet 		err = __bch2_dev_read_write(c, ca);
17581c6fdbd8SKent Overstreet 		if (err)
17591c6fdbd8SKent Overstreet 			goto err;
17601c6fdbd8SKent Overstreet 	}
17611c6fdbd8SKent Overstreet 
17621c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
17631c6fdbd8SKent Overstreet 	mi = bch2_sb_get_members(c->disk_sb.sb);
17641c6fdbd8SKent Overstreet 
17651c6fdbd8SKent Overstreet 	mi->members[ca->dev_idx].last_mount =
1766a420eea6STim Schlueter 		cpu_to_le64(ktime_get_real_seconds());
17671c6fdbd8SKent Overstreet 
17681c6fdbd8SKent Overstreet 	bch2_write_super(c);
17691c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
17701c6fdbd8SKent Overstreet 
17711ada1606SKent Overstreet 	up_write(&c->state_lock);
17721c6fdbd8SKent Overstreet 	return 0;
17731c6fdbd8SKent Overstreet err:
17741ada1606SKent Overstreet 	up_write(&c->state_lock);
17751c6fdbd8SKent Overstreet 	bch2_free_super(&sb);
17761c6fdbd8SKent Overstreet 	bch_err(c, "error bringing %s online: %s", path, err);
17771c6fdbd8SKent Overstreet 	return -EINVAL;
17781c6fdbd8SKent Overstreet }
17791c6fdbd8SKent Overstreet 
17801c6fdbd8SKent Overstreet int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags)
17811c6fdbd8SKent Overstreet {
17821ada1606SKent Overstreet 	down_write(&c->state_lock);
17831c6fdbd8SKent Overstreet 
17841c6fdbd8SKent Overstreet 	if (!bch2_dev_is_online(ca)) {
17851c6fdbd8SKent Overstreet 		bch_err(ca, "Already offline");
17861ada1606SKent Overstreet 		up_write(&c->state_lock);
17871c6fdbd8SKent Overstreet 		return 0;
17881c6fdbd8SKent Overstreet 	}
17891c6fdbd8SKent Overstreet 
17902436cb9fSKent Overstreet 	if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) {
17911c6fdbd8SKent Overstreet 		bch_err(ca, "Cannot offline required disk");
17921ada1606SKent Overstreet 		up_write(&c->state_lock);
17931c6fdbd8SKent Overstreet 		return -EINVAL;
17941c6fdbd8SKent Overstreet 	}
17951c6fdbd8SKent Overstreet 
17961c6fdbd8SKent Overstreet 	__bch2_dev_offline(c, ca);
17971c6fdbd8SKent Overstreet 
17981ada1606SKent Overstreet 	up_write(&c->state_lock);
17991c6fdbd8SKent Overstreet 	return 0;
18001c6fdbd8SKent Overstreet }
18011c6fdbd8SKent Overstreet 
18021c6fdbd8SKent Overstreet int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets)
18031c6fdbd8SKent Overstreet {
18041c6fdbd8SKent Overstreet 	struct bch_member *mi;
18051c6fdbd8SKent Overstreet 	int ret = 0;
18061c6fdbd8SKent Overstreet 
18071ada1606SKent Overstreet 	down_write(&c->state_lock);
18081c6fdbd8SKent Overstreet 
18091c6fdbd8SKent Overstreet 	if (nbuckets < ca->mi.nbuckets) {
18101c6fdbd8SKent Overstreet 		bch_err(ca, "Cannot shrink yet");
18111c6fdbd8SKent Overstreet 		ret = -EINVAL;
18121c6fdbd8SKent Overstreet 		goto err;
18131c6fdbd8SKent Overstreet 	}
18141c6fdbd8SKent Overstreet 
18151c6fdbd8SKent Overstreet 	if (bch2_dev_is_online(ca) &&
18161c6fdbd8SKent Overstreet 	    get_capacity(ca->disk_sb.bdev->bd_disk) <
18171c6fdbd8SKent Overstreet 	    ca->mi.bucket_size * nbuckets) {
18181c6fdbd8SKent Overstreet 		bch_err(ca, "New size larger than device");
18191c6fdbd8SKent Overstreet 		ret = -EINVAL;
18201c6fdbd8SKent Overstreet 		goto err;
18211c6fdbd8SKent Overstreet 	}
18221c6fdbd8SKent Overstreet 
18231c6fdbd8SKent Overstreet 	ret = bch2_dev_buckets_resize(c, ca, nbuckets);
18241c6fdbd8SKent Overstreet 	if (ret) {
18251c6fdbd8SKent Overstreet 		bch_err(ca, "Resize error: %i", ret);
18261c6fdbd8SKent Overstreet 		goto err;
18271c6fdbd8SKent Overstreet 	}
18281c6fdbd8SKent Overstreet 
1829224ec3e6SKent Overstreet 	ret = bch2_trans_mark_dev_sb(c, ca);
1830224ec3e6SKent Overstreet 	if (ret) {
1831224ec3e6SKent Overstreet 		goto err;
1832224ec3e6SKent Overstreet 	}
1833224ec3e6SKent Overstreet 
18341c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
18351c6fdbd8SKent Overstreet 	mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx];
18361c6fdbd8SKent Overstreet 	mi->nbuckets = cpu_to_le64(nbuckets);
18371c6fdbd8SKent Overstreet 
18381c6fdbd8SKent Overstreet 	bch2_write_super(c);
18391c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
18401c6fdbd8SKent Overstreet 
18411c6fdbd8SKent Overstreet 	bch2_recalc_capacity(c);
18421c6fdbd8SKent Overstreet err:
18431ada1606SKent Overstreet 	up_write(&c->state_lock);
18441c6fdbd8SKent Overstreet 	return ret;
18451c6fdbd8SKent Overstreet }
18461c6fdbd8SKent Overstreet 
18471c6fdbd8SKent Overstreet /* return with ref on ca->ref: */
18481c6fdbd8SKent Overstreet struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path)
18491c6fdbd8SKent Overstreet {
18501c6fdbd8SKent Overstreet 	struct bch_dev *ca;
18511c6fdbd8SKent Overstreet 	dev_t dev;
18521c6fdbd8SKent Overstreet 	unsigned i;
18531c6fdbd8SKent Overstreet 	int ret;
18541c6fdbd8SKent Overstreet 
18551c6fdbd8SKent Overstreet 	ret = lookup_bdev(path, &dev);
18561c6fdbd8SKent Overstreet 	if (ret)
18571c6fdbd8SKent Overstreet 		return ERR_PTR(ret);
18581c6fdbd8SKent Overstreet 
18593a402c8dSKent Overstreet 	rcu_read_lock();
18603a402c8dSKent Overstreet 	for_each_member_device_rcu(ca, c, i, NULL)
18611c6fdbd8SKent Overstreet 		if (ca->disk_sb.bdev->bd_dev == dev)
18621c6fdbd8SKent Overstreet 			goto found;
18631c6fdbd8SKent Overstreet 	ca = ERR_PTR(-ENOENT);
18641c6fdbd8SKent Overstreet found:
18653a402c8dSKent Overstreet 	rcu_read_unlock();
18663a402c8dSKent Overstreet 
18671c6fdbd8SKent Overstreet 	return ca;
18681c6fdbd8SKent Overstreet }
18691c6fdbd8SKent Overstreet 
18701c6fdbd8SKent Overstreet /* Filesystem open: */
18711c6fdbd8SKent Overstreet 
18721c6fdbd8SKent Overstreet struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
18731c6fdbd8SKent Overstreet 			    struct bch_opts opts)
18741c6fdbd8SKent Overstreet {
18751c6fdbd8SKent Overstreet 	struct bch_sb_handle *sb = NULL;
18761c6fdbd8SKent Overstreet 	struct bch_fs *c = NULL;
1877625104eaSKent Overstreet 	struct bch_sb_field_members *mi;
18781c6fdbd8SKent Overstreet 	unsigned i, best_sb = 0;
18791c6fdbd8SKent Overstreet 	const char *err;
18801c6fdbd8SKent Overstreet 	int ret = -ENOMEM;
18811c6fdbd8SKent Overstreet 
18821c6fdbd8SKent Overstreet 	pr_verbose_init(opts, "");
18831c6fdbd8SKent Overstreet 
18841c6fdbd8SKent Overstreet 	if (!nr_devices) {
18851c6fdbd8SKent Overstreet 		c = ERR_PTR(-EINVAL);
18861c6fdbd8SKent Overstreet 		goto out2;
18871c6fdbd8SKent Overstreet 	}
18881c6fdbd8SKent Overstreet 
18891c6fdbd8SKent Overstreet 	if (!try_module_get(THIS_MODULE)) {
18901c6fdbd8SKent Overstreet 		c = ERR_PTR(-ENODEV);
18911c6fdbd8SKent Overstreet 		goto out2;
18921c6fdbd8SKent Overstreet 	}
18931c6fdbd8SKent Overstreet 
18941c6fdbd8SKent Overstreet 	sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
18951c6fdbd8SKent Overstreet 	if (!sb)
18961c6fdbd8SKent Overstreet 		goto err;
18971c6fdbd8SKent Overstreet 
18981c6fdbd8SKent Overstreet 	for (i = 0; i < nr_devices; i++) {
18991c6fdbd8SKent Overstreet 		ret = bch2_read_super(devices[i], &opts, &sb[i]);
19001c6fdbd8SKent Overstreet 		if (ret)
19011c6fdbd8SKent Overstreet 			goto err;
19021c6fdbd8SKent Overstreet 
19031c6fdbd8SKent Overstreet 		err = bch2_sb_validate(&sb[i]);
19041c6fdbd8SKent Overstreet 		if (err)
19051c6fdbd8SKent Overstreet 			goto err_print;
19061c6fdbd8SKent Overstreet 	}
19071c6fdbd8SKent Overstreet 
19081c6fdbd8SKent Overstreet 	for (i = 1; i < nr_devices; i++)
19091c6fdbd8SKent Overstreet 		if (le64_to_cpu(sb[i].sb->seq) >
19101c6fdbd8SKent Overstreet 		    le64_to_cpu(sb[best_sb].sb->seq))
19111c6fdbd8SKent Overstreet 			best_sb = i;
19121c6fdbd8SKent Overstreet 
1913625104eaSKent Overstreet 	mi = bch2_sb_get_members(sb[best_sb].sb);
1914625104eaSKent Overstreet 
1915625104eaSKent Overstreet 	i = 0;
1916625104eaSKent Overstreet 	while (i < nr_devices) {
1917625104eaSKent Overstreet 		if (i != best_sb &&
1918625104eaSKent Overstreet 		    !bch2_dev_exists(sb[best_sb].sb, mi, sb[i].sb->dev_idx)) {
1919625104eaSKent Overstreet 			pr_info("%pg has been removed, skipping", sb[i].bdev);
1920625104eaSKent Overstreet 			bch2_free_super(&sb[i]);
1921625104eaSKent Overstreet 			array_remove_item(sb, nr_devices, i);
1922625104eaSKent Overstreet 			continue;
1923625104eaSKent Overstreet 		}
1924625104eaSKent Overstreet 
19251c6fdbd8SKent Overstreet 		err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb);
19261c6fdbd8SKent Overstreet 		if (err)
19271c6fdbd8SKent Overstreet 			goto err_print;
1928625104eaSKent Overstreet 		i++;
19291c6fdbd8SKent Overstreet 	}
19301c6fdbd8SKent Overstreet 
19311c6fdbd8SKent Overstreet 	ret = -ENOMEM;
19321c6fdbd8SKent Overstreet 	c = bch2_fs_alloc(sb[best_sb].sb, opts);
19331c6fdbd8SKent Overstreet 	if (!c)
19341c6fdbd8SKent Overstreet 		goto err;
19351c6fdbd8SKent Overstreet 
19361c6fdbd8SKent Overstreet 	err = "bch2_dev_online() error";
19371ada1606SKent Overstreet 	down_write(&c->state_lock);
19381c6fdbd8SKent Overstreet 	for (i = 0; i < nr_devices; i++)
19391c6fdbd8SKent Overstreet 		if (bch2_dev_attach_bdev(c, &sb[i])) {
19401ada1606SKent Overstreet 			up_write(&c->state_lock);
19411c6fdbd8SKent Overstreet 			goto err_print;
19421c6fdbd8SKent Overstreet 		}
19431ada1606SKent Overstreet 	up_write(&c->state_lock);
19441c6fdbd8SKent Overstreet 
19451c6fdbd8SKent Overstreet 	err = "insufficient devices";
19461c6fdbd8SKent Overstreet 	if (!bch2_fs_may_start(c))
19471c6fdbd8SKent Overstreet 		goto err_print;
19481c6fdbd8SKent Overstreet 
19491c6fdbd8SKent Overstreet 	if (!c->opts.nostart) {
1950619f5beeSKent Overstreet 		ret = bch2_fs_start(c);
1951619f5beeSKent Overstreet 		if (ret)
1952619f5beeSKent Overstreet 			goto err;
19531c6fdbd8SKent Overstreet 	}
19541c6fdbd8SKent Overstreet out:
19551c6fdbd8SKent Overstreet 	kfree(sb);
19561c6fdbd8SKent Overstreet 	module_put(THIS_MODULE);
19571c6fdbd8SKent Overstreet out2:
19581c6fdbd8SKent Overstreet 	pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
19591c6fdbd8SKent Overstreet 	return c;
19601c6fdbd8SKent Overstreet err_print:
19611c6fdbd8SKent Overstreet 	pr_err("bch_fs_open err opening %s: %s",
19621c6fdbd8SKent Overstreet 	       devices[0], err);
19631c6fdbd8SKent Overstreet 	ret = -EINVAL;
19641c6fdbd8SKent Overstreet err:
19651c6fdbd8SKent Overstreet 	if (c)
19661c6fdbd8SKent Overstreet 		bch2_fs_stop(c);
19671c6fdbd8SKent Overstreet 	for (i = 0; i < nr_devices; i++)
19681c6fdbd8SKent Overstreet 		bch2_free_super(&sb[i]);
19691c6fdbd8SKent Overstreet 	c = ERR_PTR(ret);
19701c6fdbd8SKent Overstreet 	goto out;
19711c6fdbd8SKent Overstreet }
19721c6fdbd8SKent Overstreet 
19731c6fdbd8SKent Overstreet static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb,
19741c6fdbd8SKent Overstreet 					      struct bch_opts opts)
19751c6fdbd8SKent Overstreet {
19761c6fdbd8SKent Overstreet 	const char *err;
19771c6fdbd8SKent Overstreet 	struct bch_fs *c;
19781c6fdbd8SKent Overstreet 	bool allocated_fs = false;
1979619f5beeSKent Overstreet 	int ret;
19801c6fdbd8SKent Overstreet 
19811c6fdbd8SKent Overstreet 	err = bch2_sb_validate(sb);
19821c6fdbd8SKent Overstreet 	if (err)
19831c6fdbd8SKent Overstreet 		return err;
19841c6fdbd8SKent Overstreet 
19851c6fdbd8SKent Overstreet 	mutex_lock(&bch_fs_list_lock);
19861c6fdbd8SKent Overstreet 	c = __bch2_uuid_to_fs(sb->sb->uuid);
19871c6fdbd8SKent Overstreet 	if (c) {
19881c6fdbd8SKent Overstreet 		closure_get(&c->cl);
19891c6fdbd8SKent Overstreet 
19901c6fdbd8SKent Overstreet 		err = bch2_dev_in_fs(c->disk_sb.sb, sb->sb);
19911c6fdbd8SKent Overstreet 		if (err)
19921c6fdbd8SKent Overstreet 			goto err;
19931c6fdbd8SKent Overstreet 	} else {
19941c6fdbd8SKent Overstreet 		c = bch2_fs_alloc(sb->sb, opts);
19951c6fdbd8SKent Overstreet 		err = "cannot allocate memory";
19961c6fdbd8SKent Overstreet 		if (!c)
19971c6fdbd8SKent Overstreet 			goto err;
19981c6fdbd8SKent Overstreet 
19991c6fdbd8SKent Overstreet 		allocated_fs = true;
20001c6fdbd8SKent Overstreet 	}
20011c6fdbd8SKent Overstreet 
20021c6fdbd8SKent Overstreet 	err = "bch2_dev_online() error";
20031c6fdbd8SKent Overstreet 
20041c6fdbd8SKent Overstreet 	mutex_lock(&c->sb_lock);
20051c6fdbd8SKent Overstreet 	if (bch2_dev_attach_bdev(c, sb)) {
20061c6fdbd8SKent Overstreet 		mutex_unlock(&c->sb_lock);
20071c6fdbd8SKent Overstreet 		goto err;
20081c6fdbd8SKent Overstreet 	}
20091c6fdbd8SKent Overstreet 	mutex_unlock(&c->sb_lock);
20101c6fdbd8SKent Overstreet 
20111c6fdbd8SKent Overstreet 	if (!c->opts.nostart && bch2_fs_may_start(c)) {
2012619f5beeSKent Overstreet 		err = "error starting filesystem";
2013619f5beeSKent Overstreet 		ret = bch2_fs_start(c);
2014619f5beeSKent Overstreet 		if (ret)
20151c6fdbd8SKent Overstreet 			goto err;
20161c6fdbd8SKent Overstreet 	}
20171c6fdbd8SKent Overstreet 
20181c6fdbd8SKent Overstreet 	closure_put(&c->cl);
20191c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
20201c6fdbd8SKent Overstreet 
20211c6fdbd8SKent Overstreet 	return NULL;
20221c6fdbd8SKent Overstreet err:
20231c6fdbd8SKent Overstreet 	mutex_unlock(&bch_fs_list_lock);
20241c6fdbd8SKent Overstreet 
20251c6fdbd8SKent Overstreet 	if (allocated_fs)
20261c6fdbd8SKent Overstreet 		bch2_fs_stop(c);
20271c6fdbd8SKent Overstreet 	else if (c)
20281c6fdbd8SKent Overstreet 		closure_put(&c->cl);
20291c6fdbd8SKent Overstreet 
20301c6fdbd8SKent Overstreet 	return err;
20311c6fdbd8SKent Overstreet }
20321c6fdbd8SKent Overstreet 
20331c6fdbd8SKent Overstreet const char *bch2_fs_open_incremental(const char *path)
20341c6fdbd8SKent Overstreet {
20351c6fdbd8SKent Overstreet 	struct bch_sb_handle sb;
20361c6fdbd8SKent Overstreet 	struct bch_opts opts = bch2_opts_empty();
20371c6fdbd8SKent Overstreet 	const char *err;
20381c6fdbd8SKent Overstreet 
20391c6fdbd8SKent Overstreet 	if (bch2_read_super(path, &opts, &sb))
20401c6fdbd8SKent Overstreet 		return "error reading superblock";
20411c6fdbd8SKent Overstreet 
20421c6fdbd8SKent Overstreet 	err = __bch2_fs_open_incremental(&sb, opts);
20431c6fdbd8SKent Overstreet 	bch2_free_super(&sb);
20441c6fdbd8SKent Overstreet 
20451c6fdbd8SKent Overstreet 	return err;
20461c6fdbd8SKent Overstreet }
20471c6fdbd8SKent Overstreet 
20481c6fdbd8SKent Overstreet /* Global interfaces/init */
20491c6fdbd8SKent Overstreet 
20501c6fdbd8SKent Overstreet static void bcachefs_exit(void)
20511c6fdbd8SKent Overstreet {
20521c6fdbd8SKent Overstreet 	bch2_debug_exit();
20531c6fdbd8SKent Overstreet 	bch2_vfs_exit();
20541c6fdbd8SKent Overstreet 	bch2_chardev_exit();
205514ba3706SKent Overstreet 	bch2_btree_key_cache_exit();
20561c6fdbd8SKent Overstreet 	if (bcachefs_kset)
20571c6fdbd8SKent Overstreet 		kset_unregister(bcachefs_kset);
20581c6fdbd8SKent Overstreet }
20591c6fdbd8SKent Overstreet 
20601c6fdbd8SKent Overstreet static int __init bcachefs_init(void)
20611c6fdbd8SKent Overstreet {
20621c6fdbd8SKent Overstreet 	bch2_bkey_pack_test();
20631c6fdbd8SKent Overstreet 
20641c6fdbd8SKent Overstreet 	if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) ||
206514ba3706SKent Overstreet 	    bch2_btree_key_cache_init() ||
20661c6fdbd8SKent Overstreet 	    bch2_chardev_init() ||
20671c6fdbd8SKent Overstreet 	    bch2_vfs_init() ||
20681c6fdbd8SKent Overstreet 	    bch2_debug_init())
20691c6fdbd8SKent Overstreet 		goto err;
20701c6fdbd8SKent Overstreet 
20711c6fdbd8SKent Overstreet 	return 0;
20721c6fdbd8SKent Overstreet err:
20731c6fdbd8SKent Overstreet 	bcachefs_exit();
20741c6fdbd8SKent Overstreet 	return -ENOMEM;
20751c6fdbd8SKent Overstreet }
20761c6fdbd8SKent Overstreet 
20771c6fdbd8SKent Overstreet #define BCH_DEBUG_PARAM(name, description)			\
20781c6fdbd8SKent Overstreet 	bool bch2_##name;					\
20791c6fdbd8SKent Overstreet 	module_param_named(name, bch2_##name, bool, 0644);	\
20801c6fdbd8SKent Overstreet 	MODULE_PARM_DESC(name, description);
20811c6fdbd8SKent Overstreet BCH_DEBUG_PARAMS()
20821c6fdbd8SKent Overstreet #undef BCH_DEBUG_PARAM
20831c6fdbd8SKent Overstreet 
208426609b61SKent Overstreet unsigned bch2_metadata_version = bcachefs_metadata_version_current;
20851c6fdbd8SKent Overstreet module_param_named(version, bch2_metadata_version, uint, 0400);
20861c6fdbd8SKent Overstreet 
20871c6fdbd8SKent Overstreet module_exit(bcachefs_exit);
20881c6fdbd8SKent Overstreet module_init(bcachefs_init);
2089