11c6fdbd8SKent Overstreet // SPDX-License-Identifier: GPL-2.0 21c6fdbd8SKent Overstreet /* 31c6fdbd8SKent Overstreet * bcachefs setup/teardown code, and some metadata io - read a superblock and 41c6fdbd8SKent Overstreet * figure out what to do with it. 51c6fdbd8SKent Overstreet * 61c6fdbd8SKent Overstreet * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> 71c6fdbd8SKent Overstreet * Copyright 2012 Google, Inc. 81c6fdbd8SKent Overstreet */ 91c6fdbd8SKent Overstreet 101c6fdbd8SKent Overstreet #include "bcachefs.h" 117b3f84eaSKent Overstreet #include "alloc_background.h" 127b3f84eaSKent Overstreet #include "alloc_foreground.h" 135b8a9227SKent Overstreet #include "bkey_sort.h" 141c6fdbd8SKent Overstreet #include "btree_cache.h" 151c6fdbd8SKent Overstreet #include "btree_gc.h" 16401585feSKent Overstreet #include "btree_journal_iter.h" 172ca88e5aSKent Overstreet #include "btree_key_cache.h" 181c6fdbd8SKent Overstreet #include "btree_update_interior.h" 191c6fdbd8SKent Overstreet #include "btree_io.h" 20920e69bcSKent Overstreet #include "btree_write_buffer.h" 2121aec962SKent Overstreet #include "buckets_waiting_for_journal.h" 221c6fdbd8SKent Overstreet #include "chardev.h" 231c6fdbd8SKent Overstreet #include "checksum.h" 241c6fdbd8SKent Overstreet #include "clock.h" 251c6fdbd8SKent Overstreet #include "compress.h" 26104c6974SDaniel Hill #include "counters.h" 271c6fdbd8SKent Overstreet #include "debug.h" 281c6fdbd8SKent Overstreet #include "disk_groups.h" 29cd575ddfSKent Overstreet #include "ec.h" 30d4bf5eecSKent Overstreet #include "errcode.h" 311c6fdbd8SKent Overstreet #include "error.h" 321c6fdbd8SKent Overstreet #include "fs.h" 331c6fdbd8SKent Overstreet #include "fs-io.h" 34dbbfca9fSKent Overstreet #include "fs-io-buffered.h" 35dbbfca9fSKent Overstreet #include "fs-io-direct.h" 361c6fdbd8SKent Overstreet #include "fsck.h" 371c6fdbd8SKent Overstreet #include "inode.h" 381809b8cbSKent Overstreet #include "io_read.h" 391809b8cbSKent Overstreet #include "io_write.h" 401c6fdbd8SKent Overstreet #include "journal.h" 411c6fdbd8SKent Overstreet #include "journal_reclaim.h" 421dd7f9d9SKent Overstreet #include "journal_seq_blacklist.h" 431c6fdbd8SKent Overstreet #include "move.h" 441c6fdbd8SKent Overstreet #include "migrate.h" 451c6fdbd8SKent Overstreet #include "movinggc.h" 46350175bfSKent Overstreet #include "nocow_locking.h" 471c6fdbd8SKent Overstreet #include "quota.h" 481c6fdbd8SKent Overstreet #include "rebalance.h" 491c6fdbd8SKent Overstreet #include "recovery.h" 501c6fdbd8SKent Overstreet #include "replicas.h" 51a37ad1a3SKent Overstreet #include "sb-clean.h" 52f5d26fa3SKent Overstreet #include "sb-errors.h" 531241df58SHunter Shaffer #include "sb-members.h" 548e877caaSKent Overstreet #include "snapshot.h" 5514b393eeSKent Overstreet #include "subvolume.h" 561c6fdbd8SKent Overstreet #include "super.h" 571c6fdbd8SKent Overstreet #include "super-io.h" 581c6fdbd8SKent Overstreet #include "sysfs.h" 591c6fdbd8SKent Overstreet #include "trace.h" 601c6fdbd8SKent Overstreet 611c6fdbd8SKent Overstreet #include <linux/backing-dev.h> 621c6fdbd8SKent Overstreet #include <linux/blkdev.h> 631c6fdbd8SKent Overstreet #include <linux/debugfs.h> 641c6fdbd8SKent Overstreet #include <linux/device.h> 651c6fdbd8SKent Overstreet #include <linux/idr.h> 661c6fdbd8SKent Overstreet #include <linux/module.h> 671c6fdbd8SKent Overstreet #include <linux/percpu.h> 681c6fdbd8SKent Overstreet #include <linux/random.h> 691c6fdbd8SKent Overstreet #include <linux/sysfs.h> 701c6fdbd8SKent Overstreet #include <crypto/hash.h> 711c6fdbd8SKent Overstreet 721c6fdbd8SKent Overstreet MODULE_LICENSE("GPL"); 731c6fdbd8SKent Overstreet MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>"); 74a9737e0bSBrian Foster MODULE_DESCRIPTION("bcachefs filesystem"); 7585c6db98SDaniel Hill MODULE_SOFTDEP("pre: crc32c"); 7685c6db98SDaniel Hill MODULE_SOFTDEP("pre: crc64"); 7785c6db98SDaniel Hill MODULE_SOFTDEP("pre: sha256"); 7885c6db98SDaniel Hill MODULE_SOFTDEP("pre: chacha20"); 7985c6db98SDaniel Hill MODULE_SOFTDEP("pre: poly1305"); 8085c6db98SDaniel Hill MODULE_SOFTDEP("pre: xxhash"); 811c6fdbd8SKent Overstreet 823c471b65SKent Overstreet const char * const bch2_fs_flag_strs[] = { 833c471b65SKent Overstreet #define x(n) #n, 843c471b65SKent Overstreet BCH_FS_FLAGS() 853c471b65SKent Overstreet #undef x 863c471b65SKent Overstreet NULL 873c471b65SKent Overstreet }; 883c471b65SKent Overstreet 892b41226dSKent Overstreet void __bch2_print(struct bch_fs *c, const char *fmt, ...) 902b41226dSKent Overstreet { 912b41226dSKent Overstreet struct log_output *output = c->output; 922b41226dSKent Overstreet va_list args; 932b41226dSKent Overstreet 942b41226dSKent Overstreet if (c->output_filter && c->output_filter != current) 952b41226dSKent Overstreet output = NULL; 962b41226dSKent Overstreet 972b41226dSKent Overstreet va_start(args, fmt); 982b41226dSKent Overstreet if (likely(!output)) { 992b41226dSKent Overstreet vprintk(fmt, args); 1002b41226dSKent Overstreet } else { 1012b41226dSKent Overstreet unsigned long flags; 1022b41226dSKent Overstreet 1032b41226dSKent Overstreet if (fmt[0] == KERN_SOH[0]) 1042b41226dSKent Overstreet fmt += 2; 1052b41226dSKent Overstreet 1062b41226dSKent Overstreet spin_lock_irqsave(&output->lock, flags); 1072b41226dSKent Overstreet prt_vprintf(&output->buf, fmt, args); 1082b41226dSKent Overstreet spin_unlock_irqrestore(&output->lock, flags); 1092b41226dSKent Overstreet 1102b41226dSKent Overstreet wake_up(&output->wait); 1112b41226dSKent Overstreet } 1122b41226dSKent Overstreet va_end(args); 1132b41226dSKent Overstreet } 1142b41226dSKent Overstreet 1151c6fdbd8SKent Overstreet #define KTYPE(type) \ 1161c6fdbd8SKent Overstreet static const struct attribute_group type ## _group = { \ 1171c6fdbd8SKent Overstreet .attrs = type ## _files \ 1181c6fdbd8SKent Overstreet }; \ 1191c6fdbd8SKent Overstreet \ 1201c6fdbd8SKent Overstreet static const struct attribute_group *type ## _groups[] = { \ 1211c6fdbd8SKent Overstreet &type ## _group, \ 1221c6fdbd8SKent Overstreet NULL \ 1231c6fdbd8SKent Overstreet }; \ 1241c6fdbd8SKent Overstreet \ 1251c6fdbd8SKent Overstreet static const struct kobj_type type ## _ktype = { \ 1261c6fdbd8SKent Overstreet .release = type ## _release, \ 1271c6fdbd8SKent Overstreet .sysfs_ops = &type ## _sysfs_ops, \ 1281c6fdbd8SKent Overstreet .default_groups = type ## _groups \ 1291c6fdbd8SKent Overstreet } 1301c6fdbd8SKent Overstreet 1311c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *); 1321c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *); 133104c6974SDaniel Hill static void bch2_fs_counters_release(struct kobject *k) 134104c6974SDaniel Hill { 135104c6974SDaniel Hill } 1361c6fdbd8SKent Overstreet 1371c6fdbd8SKent Overstreet static void bch2_fs_internal_release(struct kobject *k) 1381c6fdbd8SKent Overstreet { 1391c6fdbd8SKent Overstreet } 1401c6fdbd8SKent Overstreet 1411c6fdbd8SKent Overstreet static void bch2_fs_opts_dir_release(struct kobject *k) 1421c6fdbd8SKent Overstreet { 1431c6fdbd8SKent Overstreet } 1441c6fdbd8SKent Overstreet 1451c6fdbd8SKent Overstreet static void bch2_fs_time_stats_release(struct kobject *k) 1461c6fdbd8SKent Overstreet { 1471c6fdbd8SKent Overstreet } 1481c6fdbd8SKent Overstreet 1491c6fdbd8SKent Overstreet KTYPE(bch2_fs); 150104c6974SDaniel Hill KTYPE(bch2_fs_counters); 1511c6fdbd8SKent Overstreet KTYPE(bch2_fs_internal); 1521c6fdbd8SKent Overstreet KTYPE(bch2_fs_opts_dir); 1531c6fdbd8SKent Overstreet KTYPE(bch2_fs_time_stats); 1541c6fdbd8SKent Overstreet KTYPE(bch2_dev); 1551c6fdbd8SKent Overstreet 1561c6fdbd8SKent Overstreet static struct kset *bcachefs_kset; 1571c6fdbd8SKent Overstreet static LIST_HEAD(bch_fs_list); 1581c6fdbd8SKent Overstreet static DEFINE_MUTEX(bch_fs_list_lock); 1591c6fdbd8SKent Overstreet 160d94189adSKent Overstreet DECLARE_WAIT_QUEUE_HEAD(bch2_read_only_wait); 1611c6fdbd8SKent Overstreet 1621c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *); 1631c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *, unsigned); 1641c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *); 1651c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *); 1661c6fdbd8SKent Overstreet 1671c6fdbd8SKent Overstreet struct bch_fs *bch2_dev_to_fs(dev_t dev) 1681c6fdbd8SKent Overstreet { 1691c6fdbd8SKent Overstreet struct bch_fs *c; 1701c6fdbd8SKent Overstreet 1711c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 1721c6fdbd8SKent Overstreet rcu_read_lock(); 1731c6fdbd8SKent Overstreet 1741c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 175*41b84fb4SKent Overstreet for_each_member_device_rcu(c, ca, NULL) 176ec4ab9d2SDan Robertson if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) { 1771c6fdbd8SKent Overstreet closure_get(&c->cl); 1781c6fdbd8SKent Overstreet goto found; 1791c6fdbd8SKent Overstreet } 1801c6fdbd8SKent Overstreet c = NULL; 1811c6fdbd8SKent Overstreet found: 1821c6fdbd8SKent Overstreet rcu_read_unlock(); 1831c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 1841c6fdbd8SKent Overstreet 1851c6fdbd8SKent Overstreet return c; 1861c6fdbd8SKent Overstreet } 1871c6fdbd8SKent Overstreet 1881c6fdbd8SKent Overstreet static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid) 1891c6fdbd8SKent Overstreet { 1901c6fdbd8SKent Overstreet struct bch_fs *c; 1911c6fdbd8SKent Overstreet 1921c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 1931c6fdbd8SKent Overstreet 1941c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 1951c6fdbd8SKent Overstreet if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid))) 1961c6fdbd8SKent Overstreet return c; 1971c6fdbd8SKent Overstreet 1981c6fdbd8SKent Overstreet return NULL; 1991c6fdbd8SKent Overstreet } 2001c6fdbd8SKent Overstreet 2011c6fdbd8SKent Overstreet struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid) 2021c6fdbd8SKent Overstreet { 2031c6fdbd8SKent Overstreet struct bch_fs *c; 2041c6fdbd8SKent Overstreet 2051c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 2061c6fdbd8SKent Overstreet c = __bch2_uuid_to_fs(uuid); 2071c6fdbd8SKent Overstreet if (c) 2081c6fdbd8SKent Overstreet closure_get(&c->cl); 2091c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 2101c6fdbd8SKent Overstreet 2111c6fdbd8SKent Overstreet return c; 2121c6fdbd8SKent Overstreet } 2131c6fdbd8SKent Overstreet 214180fb49dSKent Overstreet static void bch2_dev_usage_journal_reserve(struct bch_fs *c) 215180fb49dSKent Overstreet { 216*41b84fb4SKent Overstreet unsigned nr = 0, u64s = 2174b8f89afSKent Overstreet ((sizeof(struct jset_entry_dev_usage) + 2184b8f89afSKent Overstreet sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR)) / 2194b8f89afSKent Overstreet sizeof(u64); 220180fb49dSKent Overstreet 221180fb49dSKent Overstreet rcu_read_lock(); 222*41b84fb4SKent Overstreet for_each_member_device_rcu(c, ca, NULL) 223180fb49dSKent Overstreet nr++; 224180fb49dSKent Overstreet rcu_read_unlock(); 225180fb49dSKent Overstreet 226180fb49dSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 227180fb49dSKent Overstreet &c->dev_usage_journal_res, u64s * nr); 228180fb49dSKent Overstreet } 229180fb49dSKent Overstreet 2301c6fdbd8SKent Overstreet /* Filesystem RO/RW: */ 2311c6fdbd8SKent Overstreet 2321c6fdbd8SKent Overstreet /* 2331c6fdbd8SKent Overstreet * For startup/shutdown of RW stuff, the dependencies are: 2341c6fdbd8SKent Overstreet * 2351c6fdbd8SKent Overstreet * - foreground writes depend on copygc and rebalance (to free up space) 2361c6fdbd8SKent Overstreet * 2371c6fdbd8SKent Overstreet * - copygc and rebalance depend on mark and sweep gc (they actually probably 2381c6fdbd8SKent Overstreet * don't because they either reserve ahead of time or don't block if 2391c6fdbd8SKent Overstreet * allocations fail, but allocations can require mark and sweep gc to run 2401c6fdbd8SKent Overstreet * because of generation number wraparound) 2411c6fdbd8SKent Overstreet * 2421c6fdbd8SKent Overstreet * - all of the above depends on the allocator threads 2431c6fdbd8SKent Overstreet * 2441c6fdbd8SKent Overstreet * - allocator depends on the journal (when it rewrites prios and gens) 2451c6fdbd8SKent Overstreet */ 2461c6fdbd8SKent Overstreet 2471c6fdbd8SKent Overstreet static void __bch2_fs_read_only(struct bch_fs *c) 2481c6fdbd8SKent Overstreet { 2499fea2274SKent Overstreet unsigned clean_passes = 0; 250c0960603SKent Overstreet u64 seq = 0; 2511c6fdbd8SKent Overstreet 252b40901b0SKent Overstreet bch2_fs_ec_stop(c); 253b40901b0SKent Overstreet bch2_open_buckets_stop(c, NULL, true); 2541c6fdbd8SKent Overstreet bch2_rebalance_stop(c); 255e6d11615SKent Overstreet bch2_copygc_stop(c); 2561c6fdbd8SKent Overstreet bch2_gc_thread_stop(c); 257b40901b0SKent Overstreet bch2_fs_ec_flush(c); 2581c6fdbd8SKent Overstreet 25983ec519aSKent Overstreet bch_verbose(c, "flushing journal and stopping allocators, journal seq %llu", 26083ec519aSKent Overstreet journal_cur_seq(&c->journal)); 2611c6fdbd8SKent Overstreet 262039fc4c5SKent Overstreet do { 263039fc4c5SKent Overstreet clean_passes++; 264039fc4c5SKent Overstreet 265c0960603SKent Overstreet if (bch2_btree_interior_updates_flush(c) || 266c0960603SKent Overstreet bch2_journal_flush_all_pins(&c->journal) || 267c0960603SKent Overstreet bch2_btree_flush_all_writes(c) || 268c0960603SKent Overstreet seq != atomic64_read(&c->journal.seq)) { 269c0960603SKent Overstreet seq = atomic64_read(&c->journal.seq); 270039fc4c5SKent Overstreet clean_passes = 0; 271039fc4c5SKent Overstreet } 272d5f70c1fSKent Overstreet } while (clean_passes < 2); 273c0960603SKent Overstreet 27483ec519aSKent Overstreet bch_verbose(c, "flushing journal and stopping allocators complete, journal seq %llu", 27583ec519aSKent Overstreet journal_cur_seq(&c->journal)); 2762340fd9dSKent Overstreet 277c0960603SKent Overstreet if (test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags) && 2783c471b65SKent Overstreet !test_bit(BCH_FS_emergency_ro, &c->flags)) 2793c471b65SKent Overstreet set_bit(BCH_FS_clean_shutdown, &c->flags); 2801c6fdbd8SKent Overstreet bch2_fs_journal_stop(&c->journal); 2811c6fdbd8SKent Overstreet 2821c6fdbd8SKent Overstreet /* 2831c6fdbd8SKent Overstreet * After stopping journal: 2841c6fdbd8SKent Overstreet */ 2859fea2274SKent Overstreet for_each_member_device(c, ca) 2861c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 2871c6fdbd8SKent Overstreet } 2881c6fdbd8SKent Overstreet 289d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 2901c6fdbd8SKent Overstreet static void bch2_writes_disabled(struct percpu_ref *writes) 2911c6fdbd8SKent Overstreet { 2921c6fdbd8SKent Overstreet struct bch_fs *c = container_of(writes, struct bch_fs, writes); 2931c6fdbd8SKent Overstreet 2943c471b65SKent Overstreet set_bit(BCH_FS_write_disable_complete, &c->flags); 295d94189adSKent Overstreet wake_up(&bch2_read_only_wait); 2961c6fdbd8SKent Overstreet } 297d94189adSKent Overstreet #endif 2981c6fdbd8SKent Overstreet 2991c6fdbd8SKent Overstreet void bch2_fs_read_only(struct bch_fs *c) 3001c6fdbd8SKent Overstreet { 3013c471b65SKent Overstreet if (!test_bit(BCH_FS_rw, &c->flags)) { 3029ae28f82SKent Overstreet bch2_journal_reclaim_stop(&c->journal); 3031c6fdbd8SKent Overstreet return; 304134915f3SKent Overstreet } 3051c6fdbd8SKent Overstreet 3063c471b65SKent Overstreet BUG_ON(test_bit(BCH_FS_write_disable_complete, &c->flags)); 3071c6fdbd8SKent Overstreet 308e7f7ddedSKent Overstreet bch_verbose(c, "going read-only"); 309e7f7ddedSKent Overstreet 3101c6fdbd8SKent Overstreet /* 3111c6fdbd8SKent Overstreet * Block new foreground-end write operations from starting - any new 3121c6fdbd8SKent Overstreet * writes will return -EROFS: 3131c6fdbd8SKent Overstreet */ 3143c471b65SKent Overstreet set_bit(BCH_FS_going_ro, &c->flags); 315d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 3161c6fdbd8SKent Overstreet percpu_ref_kill(&c->writes); 317d94189adSKent Overstreet #else 318d94189adSKent Overstreet for (unsigned i = 0; i < BCH_WRITE_REF_NR; i++) 319d94189adSKent Overstreet bch2_write_ref_put(c, i); 320d94189adSKent Overstreet #endif 3211c6fdbd8SKent Overstreet 3221c6fdbd8SKent Overstreet /* 3231c6fdbd8SKent Overstreet * If we're not doing an emergency shutdown, we want to wait on 3241c6fdbd8SKent Overstreet * outstanding writes to complete so they don't see spurious errors due 3251c6fdbd8SKent Overstreet * to shutting down the allocator: 3261c6fdbd8SKent Overstreet * 3271c6fdbd8SKent Overstreet * If we are doing an emergency shutdown outstanding writes may 3281c6fdbd8SKent Overstreet * hang until we shutdown the allocator so we don't want to wait 3291c6fdbd8SKent Overstreet * on outstanding writes before shutting everything down - but 3301c6fdbd8SKent Overstreet * we do need to wait on them before returning and signalling 3311c6fdbd8SKent Overstreet * that going RO is complete: 3321c6fdbd8SKent Overstreet */ 333d94189adSKent Overstreet wait_event(bch2_read_only_wait, 3343c471b65SKent Overstreet test_bit(BCH_FS_write_disable_complete, &c->flags) || 3353c471b65SKent Overstreet test_bit(BCH_FS_emergency_ro, &c->flags)); 3361c6fdbd8SKent Overstreet 3373c471b65SKent Overstreet bool writes_disabled = test_bit(BCH_FS_write_disable_complete, &c->flags); 338e7f7ddedSKent Overstreet if (writes_disabled) 339e7f7ddedSKent Overstreet bch_verbose(c, "finished waiting for writes to stop"); 340e7f7ddedSKent Overstreet 3411c6fdbd8SKent Overstreet __bch2_fs_read_only(c); 3421c6fdbd8SKent Overstreet 343d94189adSKent Overstreet wait_event(bch2_read_only_wait, 3443c471b65SKent Overstreet test_bit(BCH_FS_write_disable_complete, &c->flags)); 3451c6fdbd8SKent Overstreet 346e7f7ddedSKent Overstreet if (!writes_disabled) 347e7f7ddedSKent Overstreet bch_verbose(c, "finished waiting for writes to stop"); 348e7f7ddedSKent Overstreet 3493c471b65SKent Overstreet clear_bit(BCH_FS_write_disable_complete, &c->flags); 3503c471b65SKent Overstreet clear_bit(BCH_FS_going_ro, &c->flags); 3513c471b65SKent Overstreet clear_bit(BCH_FS_rw, &c->flags); 3521c6fdbd8SKent Overstreet 3531c6fdbd8SKent Overstreet if (!bch2_journal_error(&c->journal) && 3543c471b65SKent Overstreet !test_bit(BCH_FS_error, &c->flags) && 3553c471b65SKent Overstreet !test_bit(BCH_FS_emergency_ro, &c->flags) && 3563c471b65SKent Overstreet test_bit(BCH_FS_started, &c->flags) && 3573c471b65SKent Overstreet test_bit(BCH_FS_clean_shutdown, &c->flags) && 358b2930396SKent Overstreet !c->opts.norecovery) { 3597724664fSKent Overstreet BUG_ON(c->journal.last_empty_seq != journal_cur_seq(&c->journal)); 3607724664fSKent Overstreet BUG_ON(atomic_read(&c->btree_cache.dirty)); 3617724664fSKent Overstreet BUG_ON(atomic_long_read(&c->btree_key_cache.nr_dirty)); 36209caeabeSKent Overstreet BUG_ON(c->btree_write_buffer.inc.keys.nr); 36309caeabeSKent Overstreet BUG_ON(c->btree_write_buffer.flushing.keys.nr); 3647724664fSKent Overstreet 365b2930396SKent Overstreet bch_verbose(c, "marking filesystem clean"); 366134915f3SKent Overstreet bch2_fs_mark_clean(c); 367e7f7ddedSKent Overstreet } else { 368e7f7ddedSKent Overstreet bch_verbose(c, "done going read-only, filesystem not clean"); 369b2930396SKent Overstreet } 3701c6fdbd8SKent Overstreet } 3711c6fdbd8SKent Overstreet 3721c6fdbd8SKent Overstreet static void bch2_fs_read_only_work(struct work_struct *work) 3731c6fdbd8SKent Overstreet { 3741c6fdbd8SKent Overstreet struct bch_fs *c = 3751c6fdbd8SKent Overstreet container_of(work, struct bch_fs, read_only_work); 3761c6fdbd8SKent Overstreet 3771ada1606SKent Overstreet down_write(&c->state_lock); 3781c6fdbd8SKent Overstreet bch2_fs_read_only(c); 3791ada1606SKent Overstreet up_write(&c->state_lock); 3801c6fdbd8SKent Overstreet } 3811c6fdbd8SKent Overstreet 3821c6fdbd8SKent Overstreet static void bch2_fs_read_only_async(struct bch_fs *c) 3831c6fdbd8SKent Overstreet { 3841c6fdbd8SKent Overstreet queue_work(system_long_wq, &c->read_only_work); 3851c6fdbd8SKent Overstreet } 3861c6fdbd8SKent Overstreet 3871c6fdbd8SKent Overstreet bool bch2_fs_emergency_read_only(struct bch_fs *c) 3881c6fdbd8SKent Overstreet { 3893c471b65SKent Overstreet bool ret = !test_and_set_bit(BCH_FS_emergency_ro, &c->flags); 3901c6fdbd8SKent Overstreet 3911c6fdbd8SKent Overstreet bch2_journal_halt(&c->journal); 3929f115ce9SKent Overstreet bch2_fs_read_only_async(c); 3931c6fdbd8SKent Overstreet 394d94189adSKent Overstreet wake_up(&bch2_read_only_wait); 3951c6fdbd8SKent Overstreet return ret; 3961c6fdbd8SKent Overstreet } 3971c6fdbd8SKent Overstreet 398134915f3SKent Overstreet static int bch2_fs_read_write_late(struct bch_fs *c) 3991c6fdbd8SKent Overstreet { 400134915f3SKent Overstreet int ret; 4011c6fdbd8SKent Overstreet 402ea28c867SKent Overstreet /* 403ea28c867SKent Overstreet * Data move operations can't run until after check_snapshots has 404ea28c867SKent Overstreet * completed, and bch2_snapshot_is_ancestor() is available. 405ea28c867SKent Overstreet * 406ea28c867SKent Overstreet * Ideally we'd start copygc/rebalance earlier instead of waiting for 407ea28c867SKent Overstreet * all of recovery/fsck to complete: 408ea28c867SKent Overstreet */ 409ea28c867SKent Overstreet ret = bch2_copygc_start(c); 410ea28c867SKent Overstreet if (ret) { 411ea28c867SKent Overstreet bch_err(c, "error starting copygc thread"); 412ea28c867SKent Overstreet return ret; 413ea28c867SKent Overstreet } 414ea28c867SKent Overstreet 415134915f3SKent Overstreet ret = bch2_rebalance_start(c); 416134915f3SKent Overstreet if (ret) { 417134915f3SKent Overstreet bch_err(c, "error starting rebalance thread"); 418134915f3SKent Overstreet return ret; 419134915f3SKent Overstreet } 420134915f3SKent Overstreet 421134915f3SKent Overstreet return 0; 422134915f3SKent Overstreet } 423134915f3SKent Overstreet 424e731d466SKent Overstreet static int __bch2_fs_read_write(struct bch_fs *c, bool early) 425134915f3SKent Overstreet { 426134915f3SKent Overstreet int ret; 427134915f3SKent Overstreet 4283c471b65SKent Overstreet if (test_bit(BCH_FS_initial_gc_unfixed, &c->flags)) { 429aae15aafSKent Overstreet bch_err(c, "cannot go rw, unfixed btree errors"); 4307c50140fSKent Overstreet return -BCH_ERR_erofs_unfixed_errors; 431aae15aafSKent Overstreet } 432aae15aafSKent Overstreet 4333c471b65SKent Overstreet if (test_bit(BCH_FS_rw, &c->flags)) 434134915f3SKent Overstreet return 0; 435134915f3SKent Overstreet 4367c50140fSKent Overstreet if (c->opts.norecovery) 4377c50140fSKent Overstreet return -BCH_ERR_erofs_norecovery; 4387c50140fSKent Overstreet 439619f5beeSKent Overstreet /* 440619f5beeSKent Overstreet * nochanges is used for fsck -n mode - we have to allow going rw 441619f5beeSKent Overstreet * during recovery for that to work: 442619f5beeSKent Overstreet */ 4437c50140fSKent Overstreet if (c->opts.nochanges && (!early || c->opts.read_only)) 4447c50140fSKent Overstreet return -BCH_ERR_erofs_nochanges; 445330581f1SKent Overstreet 4462c944fa1SKent Overstreet bch_info(c, "going read-write"); 4472c944fa1SKent Overstreet 448f5d26fa3SKent Overstreet ret = bch2_sb_members_v2_init(c); 4493f7b9713SHunter Shaffer if (ret) 4503f7b9713SHunter Shaffer goto err; 4513f7b9713SHunter Shaffer 452134915f3SKent Overstreet ret = bch2_fs_mark_dirty(c); 453134915f3SKent Overstreet if (ret) 454134915f3SKent Overstreet goto err; 4551c6fdbd8SKent Overstreet 4563c471b65SKent Overstreet clear_bit(BCH_FS_clean_shutdown, &c->flags); 4572340fd9dSKent Overstreet 458b9004e85SKent Overstreet /* 459b9004e85SKent Overstreet * First journal write must be a flush write: after a clean shutdown we 460b9004e85SKent Overstreet * don't read the journal, so the first journal write may end up 461b9004e85SKent Overstreet * overwriting whatever was there previously, and there must always be 462b9004e85SKent Overstreet * at least one non-flush write in the journal or recovery will fail: 463b9004e85SKent Overstreet */ 464b9004e85SKent Overstreet set_bit(JOURNAL_NEED_FLUSH_WRITE, &c->journal.flags); 465b9004e85SKent Overstreet 4669fea2274SKent Overstreet for_each_rw_member(c, ca) 4671c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 4681c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 4691c6fdbd8SKent Overstreet 4703c471b65SKent Overstreet set_bit(BCH_FS_rw, &c->flags); 4713c471b65SKent Overstreet set_bit(BCH_FS_was_rw, &c->flags); 472468035caSKent Overstreet 473468035caSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 474468035caSKent Overstreet percpu_ref_reinit(&c->writes); 475468035caSKent Overstreet #else 4769fea2274SKent Overstreet for (unsigned i = 0; i < BCH_WRITE_REF_NR; i++) { 477468035caSKent Overstreet BUG_ON(atomic_long_read(&c->writes[i])); 478468035caSKent Overstreet atomic_long_inc(&c->writes[i]); 479468035caSKent Overstreet } 480468035caSKent Overstreet #endif 481468035caSKent Overstreet 4825f5c7466SKent Overstreet ret = bch2_gc_thread_start(c); 4835f5c7466SKent Overstreet if (ret) { 4845f5c7466SKent Overstreet bch_err(c, "error starting gc thread"); 4855f5c7466SKent Overstreet return ret; 4865f5c7466SKent Overstreet } 4875f5c7466SKent Overstreet 488197763a7SBrian Foster ret = bch2_journal_reclaim_start(&c->journal); 489197763a7SBrian Foster if (ret) 490197763a7SBrian Foster goto err; 491197763a7SBrian Foster 492134915f3SKent Overstreet if (!early) { 493134915f3SKent Overstreet ret = bch2_fs_read_write_late(c); 494134915f3SKent Overstreet if (ret) 4951c6fdbd8SKent Overstreet goto err; 4961c6fdbd8SKent Overstreet } 4971c6fdbd8SKent Overstreet 498dd81a060SKent Overstreet bch2_do_discards(c); 499dd81a060SKent Overstreet bch2_do_invalidates(c); 500dd81a060SKent Overstreet bch2_do_stripe_deletes(c); 501a1f26d70SKent Overstreet bch2_do_pending_node_rewrites(c); 502134915f3SKent Overstreet return 0; 5031c6fdbd8SKent Overstreet err: 5043c471b65SKent Overstreet if (test_bit(BCH_FS_rw, &c->flags)) 505468035caSKent Overstreet bch2_fs_read_only(c); 506468035caSKent Overstreet else 5071c6fdbd8SKent Overstreet __bch2_fs_read_only(c); 508134915f3SKent Overstreet return ret; 509134915f3SKent Overstreet } 510134915f3SKent Overstreet 511134915f3SKent Overstreet int bch2_fs_read_write(struct bch_fs *c) 512134915f3SKent Overstreet { 513134915f3SKent Overstreet return __bch2_fs_read_write(c, false); 514134915f3SKent Overstreet } 515134915f3SKent Overstreet 516134915f3SKent Overstreet int bch2_fs_read_write_early(struct bch_fs *c) 517134915f3SKent Overstreet { 518134915f3SKent Overstreet lockdep_assert_held(&c->state_lock); 519134915f3SKent Overstreet 520134915f3SKent Overstreet return __bch2_fs_read_write(c, true); 5211c6fdbd8SKent Overstreet } 5221c6fdbd8SKent Overstreet 5231c6fdbd8SKent Overstreet /* Filesystem startup/shutdown: */ 5241c6fdbd8SKent Overstreet 525d5e4dcc2SKent Overstreet static void __bch2_fs_free(struct bch_fs *c) 5261c6fdbd8SKent Overstreet { 5271c6fdbd8SKent Overstreet unsigned i; 5281c6fdbd8SKent Overstreet 5291c6fdbd8SKent Overstreet for (i = 0; i < BCH_TIME_STAT_NR; i++) 5301c6fdbd8SKent Overstreet bch2_time_stats_exit(&c->times[i]); 5311c6fdbd8SKent Overstreet 532a1f26d70SKent Overstreet bch2_free_pending_node_rewrites(c); 533f5d26fa3SKent Overstreet bch2_fs_sb_errors_exit(c); 534104c6974SDaniel Hill bch2_fs_counters_exit(c); 53514b393eeSKent Overstreet bch2_fs_snapshots_exit(c); 5361c6fdbd8SKent Overstreet bch2_fs_quota_exit(c); 537dbbfca9fSKent Overstreet bch2_fs_fs_io_direct_exit(c); 538dbbfca9fSKent Overstreet bch2_fs_fs_io_buffered_exit(c); 5391c6fdbd8SKent Overstreet bch2_fs_fsio_exit(c); 540cd575ddfSKent Overstreet bch2_fs_ec_exit(c); 5411c6fdbd8SKent Overstreet bch2_fs_encryption_exit(c); 5421e3b4098SKent Overstreet bch2_fs_nocow_locking_exit(c); 5431809b8cbSKent Overstreet bch2_fs_io_write_exit(c); 5441809b8cbSKent Overstreet bch2_fs_io_read_exit(c); 54521aec962SKent Overstreet bch2_fs_buckets_waiting_for_journal_exit(c); 546c823c339SKent Overstreet bch2_fs_btree_interior_update_exit(c); 54736e9d698SKent Overstreet bch2_fs_btree_iter_exit(c); 5482ca88e5aSKent Overstreet bch2_fs_btree_key_cache_exit(&c->btree_key_cache); 5491c6fdbd8SKent Overstreet bch2_fs_btree_cache_exit(c); 5509620c3ecSKent Overstreet bch2_fs_replicas_exit(c); 5511c6fdbd8SKent Overstreet bch2_fs_journal_exit(&c->journal); 5521c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[WRITE]); 5531c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[READ]); 5541c6fdbd8SKent Overstreet bch2_fs_compress_exit(c); 5558a443d3eSKent Overstreet bch2_journal_keys_put_initial(c); 5568a443d3eSKent Overstreet BUG_ON(atomic_read(&c->journal_keys.ref)); 557920e69bcSKent Overstreet bch2_fs_btree_write_buffer_exit(c); 5589166b41dSKent Overstreet percpu_free_rwsem(&c->mark_lock); 5595e82a9a1SKent Overstreet free_percpu(c->online_reserved); 5601a21bf98SKent Overstreet 561faa6cb6cSKent Overstreet darray_exit(&c->btree_roots_extra); 5625663a415SKent Overstreet free_percpu(c->pcpu); 56335189e09SKent Overstreet mempool_exit(&c->large_bkey_pool); 5641c6fdbd8SKent Overstreet mempool_exit(&c->btree_bounce_pool); 5651c6fdbd8SKent Overstreet bioset_exit(&c->btree_bio); 5661c6fdbd8SKent Overstreet mempool_exit(&c->fill_iter); 567d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 5681c6fdbd8SKent Overstreet percpu_ref_exit(&c->writes); 569d94189adSKent Overstreet #endif 5701c6fdbd8SKent Overstreet kfree(rcu_dereference_protected(c->disk_groups, 1)); 5711dd7f9d9SKent Overstreet kfree(c->journal_seq_blacklist_table); 572b5e8a699SKent Overstreet kfree(c->unused_inode_hints); 5731c6fdbd8SKent Overstreet 5748bff9875SBrian Foster if (c->write_ref_wq) 5758bff9875SBrian Foster destroy_workqueue(c->write_ref_wq); 576731bdd2eSKent Overstreet if (c->io_complete_wq) 577731bdd2eSKent Overstreet destroy_workqueue(c->io_complete_wq); 5781c6fdbd8SKent Overstreet if (c->copygc_wq) 5791c6fdbd8SKent Overstreet destroy_workqueue(c->copygc_wq); 5809f1833caSKent Overstreet if (c->btree_io_complete_wq) 5819f1833caSKent Overstreet destroy_workqueue(c->btree_io_complete_wq); 582731bdd2eSKent Overstreet if (c->btree_update_wq) 583731bdd2eSKent Overstreet destroy_workqueue(c->btree_update_wq); 5841c6fdbd8SKent Overstreet 5859d8022dbSKent Overstreet bch2_free_super(&c->disk_sb); 5861c6fdbd8SKent Overstreet kvpfree(c, sizeof(*c)); 5871c6fdbd8SKent Overstreet module_put(THIS_MODULE); 5881c6fdbd8SKent Overstreet } 5891c6fdbd8SKent Overstreet 5901c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *kobj) 5911c6fdbd8SKent Overstreet { 5921c6fdbd8SKent Overstreet struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); 5931c6fdbd8SKent Overstreet 594d5e4dcc2SKent Overstreet __bch2_fs_free(c); 5951c6fdbd8SKent Overstreet } 5961c6fdbd8SKent Overstreet 597d5e4dcc2SKent Overstreet void __bch2_fs_stop(struct bch_fs *c) 5981c6fdbd8SKent Overstreet { 599af1c6871SKent Overstreet bch_verbose(c, "shutting down"); 600af1c6871SKent Overstreet 6013c471b65SKent Overstreet set_bit(BCH_FS_stopping, &c->flags); 6021dd7f9d9SKent Overstreet 6039b6e2f1eSKent Overstreet cancel_work_sync(&c->journal_seq_blacklist_gc_work); 6049b6e2f1eSKent Overstreet 6051ada1606SKent Overstreet down_write(&c->state_lock); 606883f1a7cSKent Overstreet bch2_fs_read_only(c); 6071ada1606SKent Overstreet up_write(&c->state_lock); 608883f1a7cSKent Overstreet 6099fea2274SKent Overstreet for_each_member_device(c, ca) 6101c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs && 6111c6fdbd8SKent Overstreet ca->disk_sb.bdev) 6121c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 6131c6fdbd8SKent Overstreet 6141c6fdbd8SKent Overstreet if (c->kobj.state_in_sysfs) 6151c6fdbd8SKent Overstreet kobject_del(&c->kobj); 6161c6fdbd8SKent Overstreet 6171c6fdbd8SKent Overstreet bch2_fs_debug_exit(c); 6181c6fdbd8SKent Overstreet bch2_fs_chardev_exit(c); 6191c6fdbd8SKent Overstreet 62063508b75SKent Overstreet bch2_ro_ref_put(c); 62163508b75SKent Overstreet wait_event(c->ro_ref_wait, !refcount_read(&c->ro_ref)); 62263508b75SKent Overstreet 623104c6974SDaniel Hill kobject_put(&c->counters_kobj); 6241c6fdbd8SKent Overstreet kobject_put(&c->time_stats); 6251c6fdbd8SKent Overstreet kobject_put(&c->opts_dir); 6261c6fdbd8SKent Overstreet kobject_put(&c->internal); 6271c6fdbd8SKent Overstreet 6281c6fdbd8SKent Overstreet /* btree prefetch might have kicked off reads in the background: */ 6291c6fdbd8SKent Overstreet bch2_btree_flush_all_reads(c); 6301c6fdbd8SKent Overstreet 6319fea2274SKent Overstreet for_each_member_device(c, ca) 6321c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 6331c6fdbd8SKent Overstreet 6341c6fdbd8SKent Overstreet cancel_work_sync(&c->read_only_work); 635d5e4dcc2SKent Overstreet } 6361c6fdbd8SKent Overstreet 637d5e4dcc2SKent Overstreet void bch2_fs_free(struct bch_fs *c) 638d5e4dcc2SKent Overstreet { 639d5e4dcc2SKent Overstreet unsigned i; 640d5e4dcc2SKent Overstreet 641d5e4dcc2SKent Overstreet mutex_lock(&bch_fs_list_lock); 642d5e4dcc2SKent Overstreet list_del(&c->list); 643d5e4dcc2SKent Overstreet mutex_unlock(&bch_fs_list_lock); 644d5e4dcc2SKent Overstreet 645d5e4dcc2SKent Overstreet closure_sync(&c->cl); 646d5e4dcc2SKent Overstreet closure_debug_destroy(&c->cl); 647d5e4dcc2SKent Overstreet 648d5e4dcc2SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) { 649d5e4dcc2SKent Overstreet struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true); 650d5e4dcc2SKent Overstreet 651d5e4dcc2SKent Overstreet if (ca) { 652d5e4dcc2SKent Overstreet bch2_free_super(&ca->disk_sb); 653d5e4dcc2SKent Overstreet bch2_dev_free(ca); 654d5e4dcc2SKent Overstreet } 655d5e4dcc2SKent Overstreet } 6561c6fdbd8SKent Overstreet 657af1c6871SKent Overstreet bch_verbose(c, "shutdown complete"); 658af1c6871SKent Overstreet 6591c6fdbd8SKent Overstreet kobject_put(&c->kobj); 6601c6fdbd8SKent Overstreet } 6611c6fdbd8SKent Overstreet 662d5e4dcc2SKent Overstreet void bch2_fs_stop(struct bch_fs *c) 663d5e4dcc2SKent Overstreet { 664d5e4dcc2SKent Overstreet __bch2_fs_stop(c); 665d5e4dcc2SKent Overstreet bch2_fs_free(c); 666d5e4dcc2SKent Overstreet } 667d5e4dcc2SKent Overstreet 668e2b60560SKent Overstreet static int bch2_fs_online(struct bch_fs *c) 6691c6fdbd8SKent Overstreet { 670e2b60560SKent Overstreet int ret = 0; 6711c6fdbd8SKent Overstreet 6721c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 6731c6fdbd8SKent Overstreet 674e2b60560SKent Overstreet if (__bch2_uuid_to_fs(c->sb.uuid)) { 675e2b60560SKent Overstreet bch_err(c, "filesystem UUID already open"); 676e2b60560SKent Overstreet return -EINVAL; 677e2b60560SKent Overstreet } 6781c6fdbd8SKent Overstreet 6791c6fdbd8SKent Overstreet ret = bch2_fs_chardev_init(c); 680e2b60560SKent Overstreet if (ret) { 681e2b60560SKent Overstreet bch_err(c, "error creating character device"); 682e2b60560SKent Overstreet return ret; 683e2b60560SKent Overstreet } 6841c6fdbd8SKent Overstreet 6851c6fdbd8SKent Overstreet bch2_fs_debug_init(c); 6861c6fdbd8SKent Overstreet 687e2b60560SKent Overstreet ret = kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ?: 688e2b60560SKent Overstreet kobject_add(&c->internal, &c->kobj, "internal") ?: 689e2b60560SKent Overstreet kobject_add(&c->opts_dir, &c->kobj, "options") ?: 690066a2646SKent Overstreet #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT 691e2b60560SKent Overstreet kobject_add(&c->time_stats, &c->kobj, "time_stats") ?: 692066a2646SKent Overstreet #endif 693104c6974SDaniel Hill kobject_add(&c->counters_kobj, &c->kobj, "counters") ?: 694e2b60560SKent Overstreet bch2_opts_create_sysfs_files(&c->opts_dir); 695e2b60560SKent Overstreet if (ret) { 696e2b60560SKent Overstreet bch_err(c, "error creating sysfs objects"); 697e2b60560SKent Overstreet return ret; 698e2b60560SKent Overstreet } 6991c6fdbd8SKent Overstreet 7001ada1606SKent Overstreet down_write(&c->state_lock); 7011c6fdbd8SKent Overstreet 7029fea2274SKent Overstreet for_each_member_device(c, ca) { 703e2b60560SKent Overstreet ret = bch2_dev_sysfs_online(c, ca); 704e2b60560SKent Overstreet if (ret) { 705e2b60560SKent Overstreet bch_err(c, "error creating sysfs objects"); 7063a402c8dSKent Overstreet percpu_ref_put(&ca->ref); 7071c6fdbd8SKent Overstreet goto err; 7083a402c8dSKent Overstreet } 709e2b60560SKent Overstreet } 7101c6fdbd8SKent Overstreet 711e2b60560SKent Overstreet BUG_ON(!list_empty(&c->list)); 7121c6fdbd8SKent Overstreet list_add(&c->list, &bch_fs_list); 7131c6fdbd8SKent Overstreet err: 7141ada1606SKent Overstreet up_write(&c->state_lock); 715e2b60560SKent Overstreet return ret; 7161c6fdbd8SKent Overstreet } 7171c6fdbd8SKent Overstreet 7181c6fdbd8SKent Overstreet static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts) 7191c6fdbd8SKent Overstreet { 7201c6fdbd8SKent Overstreet struct bch_fs *c; 721401ec4dbSKent Overstreet struct printbuf name = PRINTBUF; 722ecf37a4aSKent Overstreet unsigned i, iter_size; 7237be9ab63SChris Webb int ret = 0; 7241c6fdbd8SKent Overstreet 7251c6fdbd8SKent Overstreet c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO); 7267be9ab63SChris Webb if (!c) { 72765d48e35SKent Overstreet c = ERR_PTR(-BCH_ERR_ENOMEM_fs_alloc); 7281c6fdbd8SKent Overstreet goto out; 7297be9ab63SChris Webb } 7301c6fdbd8SKent Overstreet 7312b41226dSKent Overstreet c->output = (void *)(unsigned long) opts.log_output; 7322b41226dSKent Overstreet 7331c6fdbd8SKent Overstreet __module_get(THIS_MODULE); 7341c6fdbd8SKent Overstreet 735505b7a4cSKent Overstreet closure_init(&c->cl, NULL); 736505b7a4cSKent Overstreet 737505b7a4cSKent Overstreet c->kobj.kset = bcachefs_kset; 738505b7a4cSKent Overstreet kobject_init(&c->kobj, &bch2_fs_ktype); 739505b7a4cSKent Overstreet kobject_init(&c->internal, &bch2_fs_internal_ktype); 740505b7a4cSKent Overstreet kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype); 741505b7a4cSKent Overstreet kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype); 742104c6974SDaniel Hill kobject_init(&c->counters_kobj, &bch2_fs_counters_ktype); 743505b7a4cSKent Overstreet 7441c6fdbd8SKent Overstreet c->minor = -1; 7451c6fdbd8SKent Overstreet c->disk_sb.fs_sb = true; 7461c6fdbd8SKent Overstreet 7471ada1606SKent Overstreet init_rwsem(&c->state_lock); 7481c6fdbd8SKent Overstreet mutex_init(&c->sb_lock); 7491c6fdbd8SKent Overstreet mutex_init(&c->replicas_gc_lock); 7501c6fdbd8SKent Overstreet mutex_init(&c->btree_root_lock); 7511c6fdbd8SKent Overstreet INIT_WORK(&c->read_only_work, bch2_fs_read_only_work); 7521c6fdbd8SKent Overstreet 75363508b75SKent Overstreet refcount_set(&c->ro_ref, 1); 75463508b75SKent Overstreet init_waitqueue_head(&c->ro_ref_wait); 755267b801fSKent Overstreet sema_init(&c->online_fsck_mutex, 1); 75663508b75SKent Overstreet 7571c6fdbd8SKent Overstreet init_rwsem(&c->gc_lock); 758c45c8667SKent Overstreet mutex_init(&c->gc_gens_lock); 7598a443d3eSKent Overstreet atomic_set(&c->journal_keys.ref, 1); 7608a443d3eSKent Overstreet c->journal_keys.initial_ref_held = true; 7611c6fdbd8SKent Overstreet 7621c6fdbd8SKent Overstreet for (i = 0; i < BCH_TIME_STAT_NR; i++) 7631c6fdbd8SKent Overstreet bch2_time_stats_init(&c->times[i]); 7641c6fdbd8SKent Overstreet 765e6d11615SKent Overstreet bch2_fs_copygc_init(c); 7662ca88e5aSKent Overstreet bch2_fs_btree_key_cache_init_early(&c->btree_key_cache); 76750a8a732SThomas Bertschinger bch2_fs_btree_iter_init_early(c); 76865db6049SKent Overstreet bch2_fs_btree_interior_update_init_early(c); 769b092daddSKent Overstreet bch2_fs_allocator_background_init(c); 770b092daddSKent Overstreet bch2_fs_allocator_foreground_init(c); 7711c6fdbd8SKent Overstreet bch2_fs_rebalance_init(c); 7721c6fdbd8SKent Overstreet bch2_fs_quota_init(c); 77384c72755SKent Overstreet bch2_fs_ec_init_early(c); 774b9fa375bSKent Overstreet bch2_fs_move_init(c); 775f5d26fa3SKent Overstreet bch2_fs_sb_errors_init_early(c); 7761c6fdbd8SKent Overstreet 7771c6fdbd8SKent Overstreet INIT_LIST_HEAD(&c->list); 7781c6fdbd8SKent Overstreet 7794d8100daSKent Overstreet mutex_init(&c->usage_scratch_lock); 7804d8100daSKent Overstreet 7811c6fdbd8SKent Overstreet mutex_init(&c->bio_bounce_pages_lock); 78214b393eeSKent Overstreet mutex_init(&c->snapshot_table_lock); 78337fad949SKent Overstreet init_rwsem(&c->snapshot_create_lock); 7841c6fdbd8SKent Overstreet 7851c6fdbd8SKent Overstreet spin_lock_init(&c->btree_write_error_lock); 7861c6fdbd8SKent Overstreet 7879b6e2f1eSKent Overstreet INIT_WORK(&c->journal_seq_blacklist_gc_work, 7889b6e2f1eSKent Overstreet bch2_blacklist_entries_gc); 7899b6e2f1eSKent Overstreet 7905b593ee1SKent Overstreet INIT_LIST_HEAD(&c->journal_iters); 791f1d786a0SKent Overstreet 792f5d26fa3SKent Overstreet INIT_LIST_HEAD(&c->fsck_error_msgs); 793f5d26fa3SKent Overstreet mutex_init(&c->fsck_error_msgs_lock); 7941c6fdbd8SKent Overstreet 7951c6fdbd8SKent Overstreet seqcount_init(&c->gc_pos_lock); 7961c6fdbd8SKent Overstreet 7975e82a9a1SKent Overstreet seqcount_init(&c->usage_lock); 7985e82a9a1SKent Overstreet 799ef1b2092SKent Overstreet sema_init(&c->io_in_flight, 128); 800ef1b2092SKent Overstreet 8019edbcc72SKent Overstreet INIT_LIST_HEAD(&c->vfs_inodes_list); 8029edbcc72SKent Overstreet mutex_init(&c->vfs_inodes_lock); 8039edbcc72SKent Overstreet 8041c6fdbd8SKent Overstreet c->copy_gc_enabled = 1; 8051c6fdbd8SKent Overstreet c->rebalance.enabled = 1; 8061c6fdbd8SKent Overstreet c->promote_whole_extents = true; 8071c6fdbd8SKent Overstreet 808991ba021SKent Overstreet c->journal.flush_write_time = &c->times[BCH_TIME_journal_flush_write]; 809991ba021SKent Overstreet c->journal.noflush_write_time = &c->times[BCH_TIME_journal_noflush_write]; 8101c6fdbd8SKent Overstreet c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq]; 8111c6fdbd8SKent Overstreet 8121c6fdbd8SKent Overstreet bch2_fs_btree_cache_init_early(&c->btree_cache); 8131c6fdbd8SKent Overstreet 814fca1223cSKent Overstreet mutex_init(&c->sectors_available_lock); 815fca1223cSKent Overstreet 816e2b60560SKent Overstreet ret = percpu_init_rwsem(&c->mark_lock); 817e2b60560SKent Overstreet if (ret) 81873e6ab95SKent Overstreet goto err; 81973e6ab95SKent Overstreet 8201c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 821e2b60560SKent Overstreet ret = bch2_sb_to_fs(c, sb); 8221c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 823e2b60560SKent Overstreet 824e2b60560SKent Overstreet if (ret) 8251c6fdbd8SKent Overstreet goto err; 8261c6fdbd8SKent Overstreet 827401ec4dbSKent Overstreet pr_uuid(&name, c->sb.user_uuid.b); 8283e3e02e6SKent Overstreet strscpy(c->name, name.buf, sizeof(c->name)); 829401ec4dbSKent Overstreet printbuf_exit(&name); 830401ec4dbSKent Overstreet 83165d48e35SKent Overstreet ret = name.allocation_failure ? -BCH_ERR_ENOMEM_fs_name_alloc : 0; 832401ec4dbSKent Overstreet if (ret) 833401ec4dbSKent Overstreet goto err; 8341c6fdbd8SKent Overstreet 8352430e72fSKent Overstreet /* Compat: */ 83673bd774dSKent Overstreet if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && 8372430e72fSKent Overstreet !BCH_SB_JOURNAL_FLUSH_DELAY(sb)) 8382430e72fSKent Overstreet SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000); 8392430e72fSKent Overstreet 84073bd774dSKent Overstreet if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && 8412430e72fSKent Overstreet !BCH_SB_JOURNAL_RECLAIM_DELAY(sb)) 8422430e72fSKent Overstreet SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 100); 8432430e72fSKent Overstreet 8441c6fdbd8SKent Overstreet c->opts = bch2_opts_default; 8458244f320SKent Overstreet ret = bch2_opts_from_sb(&c->opts, sb); 8468244f320SKent Overstreet if (ret) 8478244f320SKent Overstreet goto err; 8488244f320SKent Overstreet 8491c6fdbd8SKent Overstreet bch2_opts_apply(&c->opts, opts); 8501c6fdbd8SKent Overstreet 8517c8f6f98SKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_alloc; 8527c8f6f98SKent Overstreet if (c->opts.inodes_use_key_cache) 8537c8f6f98SKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_inodes; 854aaad530aSKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_logged_ops; 8557c8f6f98SKent Overstreet 8568244f320SKent Overstreet c->block_bits = ilog2(block_sectors(c)); 8571c6fdbd8SKent Overstreet c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c); 8581c6fdbd8SKent Overstreet 8597be9ab63SChris Webb if (bch2_fs_init_fault("fs_alloc")) { 860e2b60560SKent Overstreet bch_err(c, "fs_alloc fault injected"); 861e2b60560SKent Overstreet ret = -EFAULT; 8621c6fdbd8SKent Overstreet goto err; 8637be9ab63SChris Webb } 8641c6fdbd8SKent Overstreet 865ae2f17d5SKent Overstreet iter_size = sizeof(struct sort_iter) + 8661c6fdbd8SKent Overstreet (btree_blocks(c) + 1) * 2 * 867ae2f17d5SKent Overstreet sizeof(struct sort_iter_set); 8681c6fdbd8SKent Overstreet 869b5e8a699SKent Overstreet c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus())); 870b5e8a699SKent Overstreet 871731bdd2eSKent Overstreet if (!(c->btree_update_wq = alloc_workqueue("bcachefs", 872b17d3cecSKent Overstreet WQ_FREEZABLE|WQ_UNBOUND|WQ_MEM_RECLAIM, 512)) || 8739f1833caSKent Overstreet !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io", 8749f2772c4SKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) || 8752f33ece9SKent Overstreet !(c->copygc_wq = alloc_workqueue("bcachefs_copygc", 8762f33ece9SKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) || 877731bdd2eSKent Overstreet !(c->io_complete_wq = alloc_workqueue("bcachefs_io", 878731bdd2eSKent Overstreet WQ_FREEZABLE|WQ_HIGHPRI|WQ_MEM_RECLAIM, 1)) || 8798bff9875SBrian Foster !(c->write_ref_wq = alloc_workqueue("bcachefs_write_ref", 8808bff9875SBrian Foster WQ_FREEZABLE, 0)) || 881d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 882134915f3SKent Overstreet percpu_ref_init(&c->writes, bch2_writes_disabled, 883134915f3SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 884d94189adSKent Overstreet #endif 8851c6fdbd8SKent Overstreet mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) || 8861c6fdbd8SKent Overstreet bioset_init(&c->btree_bio, 1, 8871c6fdbd8SKent Overstreet max(offsetof(struct btree_read_bio, bio), 8881c6fdbd8SKent Overstreet offsetof(struct btree_write_bio, wbio.bio)), 8891c6fdbd8SKent Overstreet BIOSET_NEED_BVECS) || 8905663a415SKent Overstreet !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) || 8915e82a9a1SKent Overstreet !(c->online_reserved = alloc_percpu(u64)) || 8921c6fdbd8SKent Overstreet mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1, 8931c6fdbd8SKent Overstreet btree_bytes(c)) || 89435189e09SKent Overstreet mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) || 895b5e8a699SKent Overstreet !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits, 896e2b60560SKent Overstreet sizeof(u64), GFP_KERNEL))) { 89765d48e35SKent Overstreet ret = -BCH_ERR_ENOMEM_fs_other_alloc; 8987be9ab63SChris Webb goto err; 8997be9ab63SChris Webb } 9007be9ab63SChris Webb 901f3b8403eSKent Overstreet ret = bch2_fs_counters_init(c) ?: 902f5d26fa3SKent Overstreet bch2_fs_sb_errors_init(c) ?: 903f3b8403eSKent Overstreet bch2_io_clock_init(&c->io_clock[READ]) ?: 904e2b60560SKent Overstreet bch2_io_clock_init(&c->io_clock[WRITE]) ?: 905e2b60560SKent Overstreet bch2_fs_journal_init(&c->journal) ?: 906e2b60560SKent Overstreet bch2_fs_replicas_init(c) ?: 907e2b60560SKent Overstreet bch2_fs_btree_cache_init(c) ?: 908e2b60560SKent Overstreet bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?: 909e2b60560SKent Overstreet bch2_fs_btree_iter_init(c) ?: 910e2b60560SKent Overstreet bch2_fs_btree_interior_update_init(c) ?: 9119b688da3SKent Overstreet bch2_fs_buckets_waiting_for_journal_init(c) ?: 912920e69bcSKent Overstreet bch2_fs_btree_write_buffer_init(c) ?: 913e2b60560SKent Overstreet bch2_fs_subvolumes_init(c) ?: 9141809b8cbSKent Overstreet bch2_fs_io_read_init(c) ?: 9151809b8cbSKent Overstreet bch2_fs_io_write_init(c) ?: 916350175bfSKent Overstreet bch2_fs_nocow_locking_init(c) ?: 917e2b60560SKent Overstreet bch2_fs_encryption_init(c) ?: 918e2b60560SKent Overstreet bch2_fs_compress_init(c) ?: 919e2b60560SKent Overstreet bch2_fs_ec_init(c) ?: 920dbbfca9fSKent Overstreet bch2_fs_fsio_init(c) ?: 921867c1fe0SDan Carpenter bch2_fs_fs_io_buffered_init(c) ?: 922dbbfca9fSKent Overstreet bch2_fs_fs_io_direct_init(c); 9237be9ab63SChris Webb if (ret) 9241c6fdbd8SKent Overstreet goto err; 9251c6fdbd8SKent Overstreet 9261c6fdbd8SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) 9271241df58SHunter Shaffer if (bch2_dev_exists(c->disk_sb.sb, i) && 9287be9ab63SChris Webb bch2_dev_alloc(c, i)) { 929e2b60560SKent Overstreet ret = -EEXIST; 9301c6fdbd8SKent Overstreet goto err; 9317be9ab63SChris Webb } 9321c6fdbd8SKent Overstreet 9334b8f89afSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 9344b8f89afSKent Overstreet &c->btree_root_journal_res, 9354b8f89afSKent Overstreet BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX)); 9364b8f89afSKent Overstreet bch2_dev_usage_journal_reserve(c); 9374b8f89afSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 9384b8f89afSKent Overstreet &c->clock_journal_res, 9394b8f89afSKent Overstreet (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2); 9404b8f89afSKent Overstreet 9411c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 942e2b60560SKent Overstreet ret = bch2_fs_online(c); 9431c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 944e2b60560SKent Overstreet 945e2b60560SKent Overstreet if (ret) 9461c6fdbd8SKent Overstreet goto err; 9471c6fdbd8SKent Overstreet out: 9481c6fdbd8SKent Overstreet return c; 9491c6fdbd8SKent Overstreet err: 9501c6fdbd8SKent Overstreet bch2_fs_free(c); 9517be9ab63SChris Webb c = ERR_PTR(ret); 9521c6fdbd8SKent Overstreet goto out; 9531c6fdbd8SKent Overstreet } 9541c6fdbd8SKent Overstreet 955619f5beeSKent Overstreet noinline_for_stack 956619f5beeSKent Overstreet static void print_mount_opts(struct bch_fs *c) 957619f5beeSKent Overstreet { 958619f5beeSKent Overstreet enum bch_opt_id i; 959fa8e94faSKent Overstreet struct printbuf p = PRINTBUF; 960619f5beeSKent Overstreet bool first = true; 961619f5beeSKent Overstreet 962ef1634f0SKent Overstreet prt_str(&p, "mounting version "); 963e3804b55SKent Overstreet bch2_version_to_text(&p, c->sb.version); 96460573ff5SKent Overstreet 965619f5beeSKent Overstreet if (c->opts.read_only) { 96660573ff5SKent Overstreet prt_str(&p, " opts="); 967619f5beeSKent Overstreet first = false; 96860573ff5SKent Overstreet prt_printf(&p, "ro"); 969619f5beeSKent Overstreet } 970619f5beeSKent Overstreet 971619f5beeSKent Overstreet for (i = 0; i < bch2_opts_nr; i++) { 972619f5beeSKent Overstreet const struct bch_option *opt = &bch2_opt_table[i]; 973619f5beeSKent Overstreet u64 v = bch2_opt_get_by_id(&c->opts, i); 974619f5beeSKent Overstreet 9758244f320SKent Overstreet if (!(opt->flags & OPT_MOUNT)) 976619f5beeSKent Overstreet continue; 977619f5beeSKent Overstreet 978619f5beeSKent Overstreet if (v == bch2_opt_get_by_id(&bch2_opts_default, i)) 979619f5beeSKent Overstreet continue; 980619f5beeSKent Overstreet 98160573ff5SKent Overstreet prt_str(&p, first ? " opts=" : ","); 982619f5beeSKent Overstreet first = false; 9835521b1dfSKent Overstreet bch2_opt_to_text(&p, c, c->disk_sb.sb, opt, v, OPT_SHOW_MOUNT_STYLE); 984619f5beeSKent Overstreet } 985619f5beeSKent Overstreet 98660573ff5SKent Overstreet bch_info(c, "%s", p.buf); 987fa8e94faSKent Overstreet printbuf_exit(&p); 988619f5beeSKent Overstreet } 989619f5beeSKent Overstreet 990619f5beeSKent Overstreet int bch2_fs_start(struct bch_fs *c) 9911c6fdbd8SKent Overstreet { 992a420eea6STim Schlueter time64_t now = ktime_get_real_seconds(); 99378c0b75cSKent Overstreet int ret; 9941c6fdbd8SKent Overstreet 995ef1634f0SKent Overstreet print_mount_opts(c); 996ef1634f0SKent Overstreet 9971ada1606SKent Overstreet down_write(&c->state_lock); 9981c6fdbd8SKent Overstreet 9993c471b65SKent Overstreet BUG_ON(test_bit(BCH_FS_started, &c->flags)); 10001c6fdbd8SKent Overstreet 10011c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 10021c6fdbd8SKent Overstreet 1003f5d26fa3SKent Overstreet ret = bch2_sb_members_v2_init(c); 10043f7b9713SHunter Shaffer if (ret) { 10053f7b9713SHunter Shaffer mutex_unlock(&c->sb_lock); 10063f7b9713SHunter Shaffer goto err; 10073f7b9713SHunter Shaffer } 10083f7b9713SHunter Shaffer 10099fea2274SKent Overstreet for_each_online_member(c, ca) 10109fea2274SKent Overstreet bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = cpu_to_le64(now); 10111c6fdbd8SKent Overstreet 10121c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 10131c6fdbd8SKent Overstreet 10149fea2274SKent Overstreet for_each_rw_member(c, ca) 10151c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 10161c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 10171c6fdbd8SKent Overstreet 10181c6fdbd8SKent Overstreet ret = BCH_SB_INITIALIZED(c->disk_sb.sb) 10191c6fdbd8SKent Overstreet ? bch2_fs_recovery(c) 10201c6fdbd8SKent Overstreet : bch2_fs_initialize(c); 10211c6fdbd8SKent Overstreet if (ret) 10221c6fdbd8SKent Overstreet goto err; 10231c6fdbd8SKent Overstreet 1024cd575ddfSKent Overstreet ret = bch2_opts_check_may_set(c); 1025cd575ddfSKent Overstreet if (ret) 1026cd575ddfSKent Overstreet goto err; 1027cd575ddfSKent Overstreet 1028e2b60560SKent Overstreet if (bch2_fs_init_fault("fs_start")) { 1029e2b60560SKent Overstreet bch_err(c, "fs_start fault injected"); 103078c0b75cSKent Overstreet ret = -EINVAL; 10311c6fdbd8SKent Overstreet goto err; 1032e2b60560SKent Overstreet } 10331c6fdbd8SKent Overstreet 10343c471b65SKent Overstreet set_bit(BCH_FS_started, &c->flags); 1035a9310ab0SKent Overstreet 1036619f5beeSKent Overstreet if (c->opts.read_only || c->opts.nochanges) { 10371c6fdbd8SKent Overstreet bch2_fs_read_only(c); 10381c6fdbd8SKent Overstreet } else { 10393c471b65SKent Overstreet ret = !test_bit(BCH_FS_rw, &c->flags) 1040619f5beeSKent Overstreet ? bch2_fs_read_write(c) 1041619f5beeSKent Overstreet : bch2_fs_read_write_late(c); 1042619f5beeSKent Overstreet if (ret) 10431c6fdbd8SKent Overstreet goto err; 10441c6fdbd8SKent Overstreet } 10451c6fdbd8SKent Overstreet 1046619f5beeSKent Overstreet ret = 0; 1047e34ec13aSKent Overstreet err: 1048e34ec13aSKent Overstreet if (ret) 1049e34ec13aSKent Overstreet bch_err_msg(c, ret, "starting filesystem"); 1050e34ec13aSKent Overstreet else 1051e34ec13aSKent Overstreet bch_verbose(c, "done starting filesystem"); 10521ada1606SKent Overstreet up_write(&c->state_lock); 1053619f5beeSKent Overstreet return ret; 10541c6fdbd8SKent Overstreet } 10551c6fdbd8SKent Overstreet 105678c0b75cSKent Overstreet static int bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c) 10571c6fdbd8SKent Overstreet { 10581241df58SHunter Shaffer struct bch_member m = bch2_sb_member_get(sb, sb->dev_idx); 10591c6fdbd8SKent Overstreet 10608244f320SKent Overstreet if (le16_to_cpu(sb->block_size) != block_sectors(c)) 106178c0b75cSKent Overstreet return -BCH_ERR_mismatched_block_size; 10621c6fdbd8SKent Overstreet 10631241df58SHunter Shaffer if (le16_to_cpu(m.bucket_size) < 10641c6fdbd8SKent Overstreet BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb)) 106578c0b75cSKent Overstreet return -BCH_ERR_bucket_size_too_small; 10661c6fdbd8SKent Overstreet 106778c0b75cSKent Overstreet return 0; 10681c6fdbd8SKent Overstreet } 10691c6fdbd8SKent Overstreet 107078c0b75cSKent Overstreet static int bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb) 10711c6fdbd8SKent Overstreet { 10721c6fdbd8SKent Overstreet struct bch_sb *newest = 10731c6fdbd8SKent Overstreet le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb; 10741c6fdbd8SKent Overstreet 10751c6fdbd8SKent Overstreet if (!uuid_equal(&fs->uuid, &sb->uuid)) 107678c0b75cSKent Overstreet return -BCH_ERR_device_not_a_member_of_filesystem; 10771c6fdbd8SKent Overstreet 10781241df58SHunter Shaffer if (!bch2_dev_exists(newest, sb->dev_idx)) 107978c0b75cSKent Overstreet return -BCH_ERR_device_has_been_removed; 10801c6fdbd8SKent Overstreet 10811c6fdbd8SKent Overstreet if (fs->block_size != sb->block_size) 108278c0b75cSKent Overstreet return -BCH_ERR_mismatched_block_size; 10831c6fdbd8SKent Overstreet 108478c0b75cSKent Overstreet return 0; 10851c6fdbd8SKent Overstreet } 10861c6fdbd8SKent Overstreet 10871c6fdbd8SKent Overstreet /* Device startup/shutdown: */ 10881c6fdbd8SKent Overstreet 10891c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *kobj) 10901c6fdbd8SKent Overstreet { 10911c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); 10921c6fdbd8SKent Overstreet 10931c6fdbd8SKent Overstreet kfree(ca); 10941c6fdbd8SKent Overstreet } 10951c6fdbd8SKent Overstreet 10961c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *ca) 10971c6fdbd8SKent Overstreet { 10981c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 10991c6fdbd8SKent Overstreet 11001c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs && 11011c6fdbd8SKent Overstreet ca->disk_sb.bdev) 11021c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 11031c6fdbd8SKent Overstreet 11041c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) 11051c6fdbd8SKent Overstreet kobject_del(&ca->kobj); 11061c6fdbd8SKent Overstreet 11071c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 11081c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 11091c6fdbd8SKent Overstreet 11101c6fdbd8SKent Overstreet free_percpu(ca->io_done); 11111c6fdbd8SKent Overstreet bioset_exit(&ca->replica_set); 11121c6fdbd8SKent Overstreet bch2_dev_buckets_free(ca); 1113d1170ce5SKent Overstreet free_page((unsigned long) ca->sb_read_scratch); 11141c6fdbd8SKent Overstreet 11151c6fdbd8SKent Overstreet bch2_time_stats_exit(&ca->io_latency[WRITE]); 11161c6fdbd8SKent Overstreet bch2_time_stats_exit(&ca->io_latency[READ]); 11171c6fdbd8SKent Overstreet 11181c6fdbd8SKent Overstreet percpu_ref_exit(&ca->io_ref); 11191c6fdbd8SKent Overstreet percpu_ref_exit(&ca->ref); 11201c6fdbd8SKent Overstreet kobject_put(&ca->kobj); 11211c6fdbd8SKent Overstreet } 11221c6fdbd8SKent Overstreet 11231c6fdbd8SKent Overstreet static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) 11241c6fdbd8SKent Overstreet { 11251c6fdbd8SKent Overstreet 11261c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 11271c6fdbd8SKent Overstreet 11281c6fdbd8SKent Overstreet if (percpu_ref_is_zero(&ca->io_ref)) 11291c6fdbd8SKent Overstreet return; 11301c6fdbd8SKent Overstreet 11311c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 11321c6fdbd8SKent Overstreet 11331c6fdbd8SKent Overstreet reinit_completion(&ca->io_ref_completion); 11341c6fdbd8SKent Overstreet percpu_ref_kill(&ca->io_ref); 11351c6fdbd8SKent Overstreet wait_for_completion(&ca->io_ref_completion); 11361c6fdbd8SKent Overstreet 11371c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) { 11381c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 11391c6fdbd8SKent Overstreet sysfs_remove_link(&ca->kobj, "block"); 11401c6fdbd8SKent Overstreet } 11411c6fdbd8SKent Overstreet 11421c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 11431c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 11441c6fdbd8SKent Overstreet } 11451c6fdbd8SKent Overstreet 11461c6fdbd8SKent Overstreet static void bch2_dev_ref_complete(struct percpu_ref *ref) 11471c6fdbd8SKent Overstreet { 11481c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, ref); 11491c6fdbd8SKent Overstreet 11501c6fdbd8SKent Overstreet complete(&ca->ref_completion); 11511c6fdbd8SKent Overstreet } 11521c6fdbd8SKent Overstreet 11531c6fdbd8SKent Overstreet static void bch2_dev_io_ref_complete(struct percpu_ref *ref) 11541c6fdbd8SKent Overstreet { 11551c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref); 11561c6fdbd8SKent Overstreet 11571c6fdbd8SKent Overstreet complete(&ca->io_ref_completion); 11581c6fdbd8SKent Overstreet } 11591c6fdbd8SKent Overstreet 11601c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) 11611c6fdbd8SKent Overstreet { 11621c6fdbd8SKent Overstreet int ret; 11631c6fdbd8SKent Overstreet 11641c6fdbd8SKent Overstreet if (!c->kobj.state_in_sysfs) 11651c6fdbd8SKent Overstreet return 0; 11661c6fdbd8SKent Overstreet 11671c6fdbd8SKent Overstreet if (!ca->kobj.state_in_sysfs) { 11681c6fdbd8SKent Overstreet ret = kobject_add(&ca->kobj, &c->kobj, 11691c6fdbd8SKent Overstreet "dev-%u", ca->dev_idx); 11701c6fdbd8SKent Overstreet if (ret) 11711c6fdbd8SKent Overstreet return ret; 11721c6fdbd8SKent Overstreet } 11731c6fdbd8SKent Overstreet 11741c6fdbd8SKent Overstreet if (ca->disk_sb.bdev) { 11751c6fdbd8SKent Overstreet struct kobject *block = bdev_kobj(ca->disk_sb.bdev); 11761c6fdbd8SKent Overstreet 11771c6fdbd8SKent Overstreet ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); 11781c6fdbd8SKent Overstreet if (ret) 11791c6fdbd8SKent Overstreet return ret; 11801c6fdbd8SKent Overstreet 11811c6fdbd8SKent Overstreet ret = sysfs_create_link(&ca->kobj, block, "block"); 11821c6fdbd8SKent Overstreet if (ret) 11831c6fdbd8SKent Overstreet return ret; 11841c6fdbd8SKent Overstreet } 11851c6fdbd8SKent Overstreet 11861c6fdbd8SKent Overstreet return 0; 11871c6fdbd8SKent Overstreet } 11881c6fdbd8SKent Overstreet 11891c6fdbd8SKent Overstreet static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c, 11901c6fdbd8SKent Overstreet struct bch_member *member) 11911c6fdbd8SKent Overstreet { 11921c6fdbd8SKent Overstreet struct bch_dev *ca; 119394119eebSKent Overstreet unsigned i; 11941c6fdbd8SKent Overstreet 11951c6fdbd8SKent Overstreet ca = kzalloc(sizeof(*ca), GFP_KERNEL); 11961c6fdbd8SKent Overstreet if (!ca) 11971c6fdbd8SKent Overstreet return NULL; 11981c6fdbd8SKent Overstreet 11991c6fdbd8SKent Overstreet kobject_init(&ca->kobj, &bch2_dev_ktype); 12001c6fdbd8SKent Overstreet init_completion(&ca->ref_completion); 12011c6fdbd8SKent Overstreet init_completion(&ca->io_ref_completion); 12021c6fdbd8SKent Overstreet 12031c6fdbd8SKent Overstreet init_rwsem(&ca->bucket_lock); 12041c6fdbd8SKent Overstreet 12051c6fdbd8SKent Overstreet INIT_WORK(&ca->io_error_work, bch2_io_error_work); 12061c6fdbd8SKent Overstreet 12071c6fdbd8SKent Overstreet bch2_time_stats_init(&ca->io_latency[READ]); 12081c6fdbd8SKent Overstreet bch2_time_stats_init(&ca->io_latency[WRITE]); 12091c6fdbd8SKent Overstreet 12101c6fdbd8SKent Overstreet ca->mi = bch2_mi_to_cpu(member); 121194119eebSKent Overstreet 121294119eebSKent Overstreet for (i = 0; i < ARRAY_SIZE(member->errors); i++) 121394119eebSKent Overstreet atomic64_set(&ca->errors[i], le64_to_cpu(member->errors[i])); 121494119eebSKent Overstreet 12151c6fdbd8SKent Overstreet ca->uuid = member->uuid; 12161c6fdbd8SKent Overstreet 1217f25d8215SKent Overstreet ca->nr_btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE, 1218f25d8215SKent Overstreet ca->mi.bucket_size / btree_sectors(c)); 1219f25d8215SKent Overstreet 12201c6fdbd8SKent Overstreet if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, 12211c6fdbd8SKent Overstreet 0, GFP_KERNEL) || 12221c6fdbd8SKent Overstreet percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete, 12231c6fdbd8SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 1224d1170ce5SKent Overstreet !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) || 12251c6fdbd8SKent Overstreet bch2_dev_buckets_alloc(c, ca) || 12261c6fdbd8SKent Overstreet bioset_init(&ca->replica_set, 4, 12271c6fdbd8SKent Overstreet offsetof(struct bch_write_bio, bio), 0) || 12281c6fdbd8SKent Overstreet !(ca->io_done = alloc_percpu(*ca->io_done))) 12291c6fdbd8SKent Overstreet goto err; 12301c6fdbd8SKent Overstreet 12311c6fdbd8SKent Overstreet return ca; 12321c6fdbd8SKent Overstreet err: 12331c6fdbd8SKent Overstreet bch2_dev_free(ca); 12341c6fdbd8SKent Overstreet return NULL; 12351c6fdbd8SKent Overstreet } 12361c6fdbd8SKent Overstreet 12371c6fdbd8SKent Overstreet static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, 12381c6fdbd8SKent Overstreet unsigned dev_idx) 12391c6fdbd8SKent Overstreet { 12401c6fdbd8SKent Overstreet ca->dev_idx = dev_idx; 12411c6fdbd8SKent Overstreet __set_bit(ca->dev_idx, ca->self.d); 12421c6fdbd8SKent Overstreet scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); 12431c6fdbd8SKent Overstreet 12441c6fdbd8SKent Overstreet ca->fs = c; 12451c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], ca); 12461c6fdbd8SKent Overstreet 12471c6fdbd8SKent Overstreet if (bch2_dev_sysfs_online(c, ca)) 12481c6fdbd8SKent Overstreet pr_warn("error creating sysfs objects"); 12491c6fdbd8SKent Overstreet } 12501c6fdbd8SKent Overstreet 12511c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx) 12521c6fdbd8SKent Overstreet { 12531241df58SHunter Shaffer struct bch_member member = bch2_sb_member_get(c->disk_sb.sb, dev_idx); 12541c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 12551c6fdbd8SKent Overstreet int ret = 0; 12561c6fdbd8SKent Overstreet 12571c6fdbd8SKent Overstreet if (bch2_fs_init_fault("dev_alloc")) 12581c6fdbd8SKent Overstreet goto err; 12591c6fdbd8SKent Overstreet 12601241df58SHunter Shaffer ca = __bch2_dev_alloc(c, &member); 12611c6fdbd8SKent Overstreet if (!ca) 12621c6fdbd8SKent Overstreet goto err; 12631c6fdbd8SKent Overstreet 1264220d2062SKent Overstreet ca->fs = c; 1265220d2062SKent Overstreet 12661c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 12671c6fdbd8SKent Overstreet return ret; 12681c6fdbd8SKent Overstreet err: 12691c6fdbd8SKent Overstreet if (ca) 12701c6fdbd8SKent Overstreet bch2_dev_free(ca); 1271c8b4534dSKent Overstreet return -BCH_ERR_ENOMEM_dev_alloc; 12721c6fdbd8SKent Overstreet } 12731c6fdbd8SKent Overstreet 12741c6fdbd8SKent Overstreet static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb) 12751c6fdbd8SKent Overstreet { 12761c6fdbd8SKent Overstreet unsigned ret; 12771c6fdbd8SKent Overstreet 12781c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca)) { 12791c6fdbd8SKent Overstreet bch_err(ca, "already have device online in slot %u", 12801c6fdbd8SKent Overstreet sb->sb->dev_idx); 128178c0b75cSKent Overstreet return -BCH_ERR_device_already_online; 12821c6fdbd8SKent Overstreet } 12831c6fdbd8SKent Overstreet 12841c6fdbd8SKent Overstreet if (get_capacity(sb->bdev->bd_disk) < 12851c6fdbd8SKent Overstreet ca->mi.bucket_size * ca->mi.nbuckets) { 12861c6fdbd8SKent Overstreet bch_err(ca, "cannot online: device too small"); 128778c0b75cSKent Overstreet return -BCH_ERR_device_size_too_small; 12881c6fdbd8SKent Overstreet } 12891c6fdbd8SKent Overstreet 12901c6fdbd8SKent Overstreet BUG_ON(!percpu_ref_is_zero(&ca->io_ref)); 12911c6fdbd8SKent Overstreet 12921c6fdbd8SKent Overstreet ret = bch2_dev_journal_init(ca, sb->sb); 12931c6fdbd8SKent Overstreet if (ret) 12941c6fdbd8SKent Overstreet return ret; 12951c6fdbd8SKent Overstreet 12961c6fdbd8SKent Overstreet /* Commit: */ 12971c6fdbd8SKent Overstreet ca->disk_sb = *sb; 12981c6fdbd8SKent Overstreet memset(sb, 0, sizeof(*sb)); 12991c6fdbd8SKent Overstreet 1300eacb2574SKent Overstreet ca->dev = ca->disk_sb.bdev->bd_dev; 1301eacb2574SKent Overstreet 13021c6fdbd8SKent Overstreet percpu_ref_reinit(&ca->io_ref); 13031c6fdbd8SKent Overstreet 13041c6fdbd8SKent Overstreet return 0; 13051c6fdbd8SKent Overstreet } 13061c6fdbd8SKent Overstreet 13071c6fdbd8SKent Overstreet static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb) 13081c6fdbd8SKent Overstreet { 13091c6fdbd8SKent Overstreet struct bch_dev *ca; 13101c6fdbd8SKent Overstreet int ret; 13111c6fdbd8SKent Overstreet 13121c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 13131c6fdbd8SKent Overstreet 13141c6fdbd8SKent Overstreet if (le64_to_cpu(sb->sb->seq) > 13151c6fdbd8SKent Overstreet le64_to_cpu(c->disk_sb.sb->seq)) 13161c6fdbd8SKent Overstreet bch2_sb_to_fs(c, sb->sb); 13171c6fdbd8SKent Overstreet 13181c6fdbd8SKent Overstreet BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices || 13191c6fdbd8SKent Overstreet !c->devs[sb->sb->dev_idx]); 13201c6fdbd8SKent Overstreet 13211c6fdbd8SKent Overstreet ca = bch_dev_locked(c, sb->sb->dev_idx); 13221c6fdbd8SKent Overstreet 13231c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, sb); 13241c6fdbd8SKent Overstreet if (ret) 13251c6fdbd8SKent Overstreet return ret; 13261c6fdbd8SKent Overstreet 13271c6fdbd8SKent Overstreet bch2_dev_sysfs_online(c, ca); 13281c6fdbd8SKent Overstreet 13291c6fdbd8SKent Overstreet if (c->sb.nr_devices == 1) 13301c6fdbd8SKent Overstreet snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev); 13311c6fdbd8SKent Overstreet snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev); 13321c6fdbd8SKent Overstreet 13331c6fdbd8SKent Overstreet rebalance_wakeup(c); 13341c6fdbd8SKent Overstreet return 0; 13351c6fdbd8SKent Overstreet } 13361c6fdbd8SKent Overstreet 13371c6fdbd8SKent Overstreet /* Device management: */ 13381c6fdbd8SKent Overstreet 13391c6fdbd8SKent Overstreet /* 13401c6fdbd8SKent Overstreet * Note: this function is also used by the error paths - when a particular 13411c6fdbd8SKent Overstreet * device sees an error, we call it to determine whether we can just set the 13421c6fdbd8SKent Overstreet * device RO, or - if this function returns false - we'll set the whole 13431c6fdbd8SKent Overstreet * filesystem RO: 13441c6fdbd8SKent Overstreet * 13451c6fdbd8SKent Overstreet * XXX: maybe we should be more explicit about whether we're changing state 13461c6fdbd8SKent Overstreet * because we got an error or what have you? 13471c6fdbd8SKent Overstreet */ 13481c6fdbd8SKent Overstreet bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, 13491c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 13501c6fdbd8SKent Overstreet { 13511c6fdbd8SKent Overstreet struct bch_devs_mask new_online_devs; 13529fea2274SKent Overstreet int nr_rw = 0, required; 13531c6fdbd8SKent Overstreet 13541c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 13551c6fdbd8SKent Overstreet 13561c6fdbd8SKent Overstreet switch (new_state) { 13572436cb9fSKent Overstreet case BCH_MEMBER_STATE_rw: 13581c6fdbd8SKent Overstreet return true; 13592436cb9fSKent Overstreet case BCH_MEMBER_STATE_ro: 13602436cb9fSKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_rw) 13611c6fdbd8SKent Overstreet return true; 13621c6fdbd8SKent Overstreet 13631c6fdbd8SKent Overstreet /* do we have enough devices to write to? */ 13649fea2274SKent Overstreet for_each_member_device(c, ca2) 13651c6fdbd8SKent Overstreet if (ca2 != ca) 13662436cb9fSKent Overstreet nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw; 13671c6fdbd8SKent Overstreet 13681c6fdbd8SKent Overstreet required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED) 13691c6fdbd8SKent Overstreet ? c->opts.metadata_replicas 13701c6fdbd8SKent Overstreet : c->opts.metadata_replicas_required, 13711c6fdbd8SKent Overstreet !(flags & BCH_FORCE_IF_DATA_DEGRADED) 13721c6fdbd8SKent Overstreet ? c->opts.data_replicas 13731c6fdbd8SKent Overstreet : c->opts.data_replicas_required); 13741c6fdbd8SKent Overstreet 13751c6fdbd8SKent Overstreet return nr_rw >= required; 13762436cb9fSKent Overstreet case BCH_MEMBER_STATE_failed: 13772436cb9fSKent Overstreet case BCH_MEMBER_STATE_spare: 13782436cb9fSKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_rw && 13792436cb9fSKent Overstreet ca->mi.state != BCH_MEMBER_STATE_ro) 13801c6fdbd8SKent Overstreet return true; 13811c6fdbd8SKent Overstreet 13821c6fdbd8SKent Overstreet /* do we have enough devices to read from? */ 13831c6fdbd8SKent Overstreet new_online_devs = bch2_online_devs(c); 13841c6fdbd8SKent Overstreet __clear_bit(ca->dev_idx, new_online_devs.d); 13851c6fdbd8SKent Overstreet 1386fcb3431bSKent Overstreet return bch2_have_enough_devs(c, new_online_devs, flags, false); 13871c6fdbd8SKent Overstreet default: 13881c6fdbd8SKent Overstreet BUG(); 13891c6fdbd8SKent Overstreet } 13901c6fdbd8SKent Overstreet } 13911c6fdbd8SKent Overstreet 13921c6fdbd8SKent Overstreet static bool bch2_fs_may_start(struct bch_fs *c) 13931c6fdbd8SKent Overstreet { 13941c6fdbd8SKent Overstreet struct bch_dev *ca; 1395fcb3431bSKent Overstreet unsigned i, flags = 0; 13961c6fdbd8SKent Overstreet 1397fcb3431bSKent Overstreet if (c->opts.very_degraded) 1398fcb3431bSKent Overstreet flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST; 1399fcb3431bSKent Overstreet 1400fcb3431bSKent Overstreet if (c->opts.degraded) 1401fcb3431bSKent Overstreet flags |= BCH_FORCE_IF_DEGRADED; 1402fcb3431bSKent Overstreet 1403fcb3431bSKent Overstreet if (!c->opts.degraded && 1404fcb3431bSKent Overstreet !c->opts.very_degraded) { 14051c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 14061c6fdbd8SKent Overstreet 14071c6fdbd8SKent Overstreet for (i = 0; i < c->disk_sb.sb->nr_devices; i++) { 14081241df58SHunter Shaffer if (!bch2_dev_exists(c->disk_sb.sb, i)) 14091c6fdbd8SKent Overstreet continue; 14101c6fdbd8SKent Overstreet 14111c6fdbd8SKent Overstreet ca = bch_dev_locked(c, i); 14121c6fdbd8SKent Overstreet 14131c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca) && 14142436cb9fSKent Overstreet (ca->mi.state == BCH_MEMBER_STATE_rw || 14152436cb9fSKent Overstreet ca->mi.state == BCH_MEMBER_STATE_ro)) { 14161c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 14171c6fdbd8SKent Overstreet return false; 14181c6fdbd8SKent Overstreet } 14191c6fdbd8SKent Overstreet } 14201c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 14211c6fdbd8SKent Overstreet } 14221c6fdbd8SKent Overstreet 1423fcb3431bSKent Overstreet return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true); 14241c6fdbd8SKent Overstreet } 14251c6fdbd8SKent Overstreet 14261c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) 14271c6fdbd8SKent Overstreet { 14281c6fdbd8SKent Overstreet /* 14291c6fdbd8SKent Overstreet * The allocator thread itself allocates btree nodes, so stop it first: 14301c6fdbd8SKent Overstreet */ 14311c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 14321c6fdbd8SKent Overstreet bch2_dev_journal_stop(&c->journal, ca); 14331c6fdbd8SKent Overstreet } 14341c6fdbd8SKent Overstreet 1435f25d8215SKent Overstreet static void __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) 14361c6fdbd8SKent Overstreet { 14371c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 14381c6fdbd8SKent Overstreet 14392436cb9fSKent Overstreet BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw); 14401c6fdbd8SKent Overstreet 14411c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 14421c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 14431c6fdbd8SKent Overstreet } 14441c6fdbd8SKent Overstreet 14451c6fdbd8SKent Overstreet int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 14461c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 14471c6fdbd8SKent Overstreet { 14483f7b9713SHunter Shaffer struct bch_member *m; 14491c6fdbd8SKent Overstreet int ret = 0; 14501c6fdbd8SKent Overstreet 14511c6fdbd8SKent Overstreet if (ca->mi.state == new_state) 14521c6fdbd8SKent Overstreet return 0; 14531c6fdbd8SKent Overstreet 14541c6fdbd8SKent Overstreet if (!bch2_dev_state_allowed(c, ca, new_state, flags)) 145578c0b75cSKent Overstreet return -BCH_ERR_device_state_not_allowed; 14561c6fdbd8SKent Overstreet 14572436cb9fSKent Overstreet if (new_state != BCH_MEMBER_STATE_rw) 14581c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 14591c6fdbd8SKent Overstreet 14602436cb9fSKent Overstreet bch_notice(ca, "%s", bch2_member_states[new_state]); 14611c6fdbd8SKent Overstreet 14621c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 14633f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 14643f7b9713SHunter Shaffer SET_BCH_MEMBER_STATE(m, new_state); 14651c6fdbd8SKent Overstreet bch2_write_super(c); 14661c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 14671c6fdbd8SKent Overstreet 1468e2b60560SKent Overstreet if (new_state == BCH_MEMBER_STATE_rw) 1469f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 14701c6fdbd8SKent Overstreet 14711c6fdbd8SKent Overstreet rebalance_wakeup(c); 14721c6fdbd8SKent Overstreet 14731c6fdbd8SKent Overstreet return ret; 14741c6fdbd8SKent Overstreet } 14751c6fdbd8SKent Overstreet 14761c6fdbd8SKent Overstreet int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 14771c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 14781c6fdbd8SKent Overstreet { 14791c6fdbd8SKent Overstreet int ret; 14801c6fdbd8SKent Overstreet 14811ada1606SKent Overstreet down_write(&c->state_lock); 14821c6fdbd8SKent Overstreet ret = __bch2_dev_set_state(c, ca, new_state, flags); 14831ada1606SKent Overstreet up_write(&c->state_lock); 14841c6fdbd8SKent Overstreet 14851c6fdbd8SKent Overstreet return ret; 14861c6fdbd8SKent Overstreet } 14871c6fdbd8SKent Overstreet 14881c6fdbd8SKent Overstreet /* Device add/removal: */ 14891c6fdbd8SKent Overstreet 1490c0ebe3e4SKent Overstreet static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca) 14915d20ba48SKent Overstreet { 1492c6b2826cSKent Overstreet struct bpos start = POS(ca->dev_idx, 0); 1493c6b2826cSKent Overstreet struct bpos end = POS(ca->dev_idx, U64_MAX); 14945d20ba48SKent Overstreet int ret; 14955d20ba48SKent Overstreet 1496a9c0a4cbSKent Overstreet /* 1497a9c0a4cbSKent Overstreet * We clear the LRU and need_discard btrees first so that we don't race 1498a9c0a4cbSKent Overstreet * with bch2_do_invalidates() and bch2_do_discards() 1499a9c0a4cbSKent Overstreet */ 1500a9c0a4cbSKent Overstreet ret = bch2_btree_delete_range(c, BTREE_ID_lru, start, end, 1501a9c0a4cbSKent Overstreet BTREE_TRIGGER_NORUN, NULL) ?: 1502a9c0a4cbSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_need_discard, start, end, 1503c6b2826cSKent Overstreet BTREE_TRIGGER_NORUN, NULL) ?: 1504c6b2826cSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_freespace, start, end, 1505c6b2826cSKent Overstreet BTREE_TRIGGER_NORUN, NULL) ?: 1506a8c752bbSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_backpointers, start, end, 1507a8c752bbSKent Overstreet BTREE_TRIGGER_NORUN, NULL) ?: 1508a9c0a4cbSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_alloc, start, end, 150902d51bb9SBrian Foster BTREE_TRIGGER_NORUN, NULL) ?: 151002d51bb9SBrian Foster bch2_btree_delete_range(c, BTREE_ID_bucket_gens, start, end, 1511c6b2826cSKent Overstreet BTREE_TRIGGER_NORUN, NULL); 1512e46c181aSKent Overstreet bch_err_msg(c, ret, "removing dev alloc info"); 1513c6b2826cSKent Overstreet return ret; 15145d20ba48SKent Overstreet } 15155d20ba48SKent Overstreet 15161c6fdbd8SKent Overstreet int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) 15171c6fdbd8SKent Overstreet { 15183f7b9713SHunter Shaffer struct bch_member *m; 15191c6fdbd8SKent Overstreet unsigned dev_idx = ca->dev_idx, data; 152078c0b75cSKent Overstreet int ret; 15211c6fdbd8SKent Overstreet 15221ada1606SKent Overstreet down_write(&c->state_lock); 15231c6fdbd8SKent Overstreet 152431ba2cd3SKent Overstreet /* 152531ba2cd3SKent Overstreet * We consume a reference to ca->ref, regardless of whether we succeed 152631ba2cd3SKent Overstreet * or fail: 152731ba2cd3SKent Overstreet */ 152831ba2cd3SKent Overstreet percpu_ref_put(&ca->ref); 15291c6fdbd8SKent Overstreet 15302436cb9fSKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 15311c6fdbd8SKent Overstreet bch_err(ca, "Cannot remove without losing data"); 153278c0b75cSKent Overstreet ret = -BCH_ERR_device_state_not_allowed; 15331c6fdbd8SKent Overstreet goto err; 15341c6fdbd8SKent Overstreet } 15351c6fdbd8SKent Overstreet 15361c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 15371c6fdbd8SKent Overstreet 15381c6fdbd8SKent Overstreet ret = bch2_dev_data_drop(c, ca->dev_idx, flags); 1539e46c181aSKent Overstreet bch_err_msg(ca, ret, "dropping data"); 1540cf904c8dSKent Overstreet if (ret) 15411c6fdbd8SKent Overstreet goto err; 15421c6fdbd8SKent Overstreet 15435d20ba48SKent Overstreet ret = bch2_dev_remove_alloc(c, ca); 1544e46c181aSKent Overstreet bch_err_msg(ca, ret, "deleting alloc info"); 1545cf904c8dSKent Overstreet if (ret) 15461c6fdbd8SKent Overstreet goto err; 15471c6fdbd8SKent Overstreet 1548a9c0a4cbSKent Overstreet ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); 1549e46c181aSKent Overstreet bch_err_msg(ca, ret, "flushing journal"); 1550cf904c8dSKent Overstreet if (ret) 1551a9c0a4cbSKent Overstreet goto err; 1552a9c0a4cbSKent Overstreet 1553a9c0a4cbSKent Overstreet ret = bch2_journal_flush(&c->journal); 1554e46c181aSKent Overstreet bch_err(ca, "journal error"); 1555cf904c8dSKent Overstreet if (ret) 15561c6fdbd8SKent Overstreet goto err; 15571c6fdbd8SKent Overstreet 155831ba2cd3SKent Overstreet ret = bch2_replicas_gc2(c); 1559e46c181aSKent Overstreet bch_err_msg(ca, ret, "in replicas_gc2()"); 1560cf904c8dSKent Overstreet if (ret) 156131ba2cd3SKent Overstreet goto err; 156231ba2cd3SKent Overstreet 156331ba2cd3SKent Overstreet data = bch2_dev_has_data(c, ca); 156431ba2cd3SKent Overstreet if (data) { 1565fa8e94faSKent Overstreet struct printbuf data_has = PRINTBUF; 156631ba2cd3SKent Overstreet 1567401ec4dbSKent Overstreet prt_bitflags(&data_has, bch2_data_types, data); 1568fa8e94faSKent Overstreet bch_err(ca, "Remove failed, still has data (%s)", data_has.buf); 1569fa8e94faSKent Overstreet printbuf_exit(&data_has); 157031ba2cd3SKent Overstreet ret = -EBUSY; 157131ba2cd3SKent Overstreet goto err; 157231ba2cd3SKent Overstreet } 157331ba2cd3SKent Overstreet 15741c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 15751c6fdbd8SKent Overstreet 15761c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15771c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], NULL); 15781c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15791c6fdbd8SKent Overstreet 15801c6fdbd8SKent Overstreet percpu_ref_kill(&ca->ref); 15811c6fdbd8SKent Overstreet wait_for_completion(&ca->ref_completion); 15821c6fdbd8SKent Overstreet 15831c6fdbd8SKent Overstreet bch2_dev_free(ca); 15841c6fdbd8SKent Overstreet 15851c6fdbd8SKent Overstreet /* 1586bc652905SBrian Foster * At this point the device object has been removed in-core, but the 1587bc652905SBrian Foster * on-disk journal might still refer to the device index via sb device 1588bc652905SBrian Foster * usage entries. Recovery fails if it sees usage information for an 1589bc652905SBrian Foster * invalid device. Flush journal pins to push the back of the journal 1590bc652905SBrian Foster * past now invalid device index references before we update the 1591bc652905SBrian Foster * superblock, but after the device object has been removed so any 1592bc652905SBrian Foster * further journal writes elide usage info for the device. 1593bc652905SBrian Foster */ 1594bc652905SBrian Foster bch2_journal_flush_all_pins(&c->journal); 1595bc652905SBrian Foster 1596bc652905SBrian Foster /* 15971c6fdbd8SKent Overstreet * Free this device's slot in the bch_member array - all pointers to 15981c6fdbd8SKent Overstreet * this device must be gone: 15991c6fdbd8SKent Overstreet */ 16001c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 16013f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx); 16023f7b9713SHunter Shaffer memset(&m->uuid, 0, sizeof(m->uuid)); 16031c6fdbd8SKent Overstreet 16041c6fdbd8SKent Overstreet bch2_write_super(c); 16051c6fdbd8SKent Overstreet 16061c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 16071ada1606SKent Overstreet up_write(&c->state_lock); 1608180fb49dSKent Overstreet 1609180fb49dSKent Overstreet bch2_dev_usage_journal_reserve(c); 16101c6fdbd8SKent Overstreet return 0; 16111c6fdbd8SKent Overstreet err: 16122436cb9fSKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw && 1613d3bb629dSKent Overstreet !percpu_ref_is_zero(&ca->io_ref)) 16141c6fdbd8SKent Overstreet __bch2_dev_read_write(c, ca); 16151ada1606SKent Overstreet up_write(&c->state_lock); 16161c6fdbd8SKent Overstreet return ret; 16171c6fdbd8SKent Overstreet } 16181c6fdbd8SKent Overstreet 16191c6fdbd8SKent Overstreet /* Add new device to running filesystem: */ 16201c6fdbd8SKent Overstreet int bch2_dev_add(struct bch_fs *c, const char *path) 16211c6fdbd8SKent Overstreet { 16221c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 16231c6fdbd8SKent Overstreet struct bch_sb_handle sb; 16241c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 16253f7b9713SHunter Shaffer struct bch_sb_field_members_v2 *mi; 16261c6fdbd8SKent Overstreet struct bch_member dev_mi; 16271c6fdbd8SKent Overstreet unsigned dev_idx, nr_devices, u64s; 1628fa8e94faSKent Overstreet struct printbuf errbuf = PRINTBUF; 162902afcb8cSKent Overstreet struct printbuf label = PRINTBUF; 16301c6fdbd8SKent Overstreet int ret; 16311c6fdbd8SKent Overstreet 16321c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 1633e46c181aSKent Overstreet bch_err_msg(c, ret, "reading super"); 1634cf904c8dSKent Overstreet if (ret) 1635efe68e1dSKent Overstreet goto err; 16361c6fdbd8SKent Overstreet 16371241df58SHunter Shaffer dev_mi = bch2_sb_member_get(sb.sb, sb.sb->dev_idx); 16381c6fdbd8SKent Overstreet 163902afcb8cSKent Overstreet if (BCH_MEMBER_GROUP(&dev_mi)) { 1640e677179bSKent Overstreet bch2_disk_path_to_text_sb(&label, sb.sb, BCH_MEMBER_GROUP(&dev_mi) - 1); 164102afcb8cSKent Overstreet if (label.allocation_failure) { 164202afcb8cSKent Overstreet ret = -ENOMEM; 164302afcb8cSKent Overstreet goto err; 164402afcb8cSKent Overstreet } 164502afcb8cSKent Overstreet } 164602afcb8cSKent Overstreet 164778c0b75cSKent Overstreet ret = bch2_dev_may_add(sb.sb, c); 1648cf904c8dSKent Overstreet if (ret) 1649efe68e1dSKent Overstreet goto err; 16501c6fdbd8SKent Overstreet 16511c6fdbd8SKent Overstreet ca = __bch2_dev_alloc(c, &dev_mi); 16521c6fdbd8SKent Overstreet if (!ca) { 1653efe68e1dSKent Overstreet ret = -ENOMEM; 1654efe68e1dSKent Overstreet goto err; 16551c6fdbd8SKent Overstreet } 16561c6fdbd8SKent Overstreet 1657822835ffSKent Overstreet bch2_dev_usage_init(ca); 1658822835ffSKent Overstreet 16591c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, &sb); 166071933fb6SChristophe JAILLET if (ret) 1661efe68e1dSKent Overstreet goto err; 16621c6fdbd8SKent Overstreet 16631c6fdbd8SKent Overstreet ret = bch2_dev_journal_alloc(ca); 1664e46c181aSKent Overstreet bch_err_msg(c, ret, "allocating journal"); 1665cf904c8dSKent Overstreet if (ret) 16661c6fdbd8SKent Overstreet goto err; 16671c6fdbd8SKent Overstreet 16681ada1606SKent Overstreet down_write(&c->state_lock); 16691c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 16701c6fdbd8SKent Overstreet 16711c6fdbd8SKent Overstreet ret = bch2_sb_from_fs(c, ca); 1672e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 1673cf904c8dSKent Overstreet if (ret) 16741c6fdbd8SKent Overstreet goto err_unlock; 16751c6fdbd8SKent Overstreet 16761c6fdbd8SKent Overstreet if (dynamic_fault("bcachefs:add:no_slot")) 16771c6fdbd8SKent Overstreet goto no_slot; 16781c6fdbd8SKent Overstreet 16791c6fdbd8SKent Overstreet for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++) 16801241df58SHunter Shaffer if (!bch2_dev_exists(c->disk_sb.sb, dev_idx)) 16811c6fdbd8SKent Overstreet goto have_slot; 16821c6fdbd8SKent Overstreet no_slot: 1683098ef98dSKent Overstreet ret = -BCH_ERR_ENOSPC_sb_members; 1684e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 16851c6fdbd8SKent Overstreet goto err_unlock; 16861c6fdbd8SKent Overstreet 16871c6fdbd8SKent Overstreet have_slot: 16881c6fdbd8SKent Overstreet nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices); 1689e8484348SKent Overstreet 1690e8484348SKent Overstreet mi = bch2_sb_field_get(c->disk_sb.sb, members_v2); 16913f7b9713SHunter Shaffer u64s = DIV_ROUND_UP(sizeof(struct bch_sb_field_members_v2) + 16923f7b9713SHunter Shaffer le16_to_cpu(mi->member_bytes) * nr_devices, sizeof(u64)); 16931c6fdbd8SKent Overstreet 16944637429eSKent Overstreet mi = bch2_sb_field_resize(&c->disk_sb, members_v2, u64s); 1695e8536925SKent Overstreet if (!mi) { 1696098ef98dSKent Overstreet ret = -BCH_ERR_ENOSPC_sb_members; 1697e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 16981c6fdbd8SKent Overstreet goto err_unlock; 1699e8536925SKent Overstreet } 17003f7b9713SHunter Shaffer struct bch_member *m = bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx); 17011c6fdbd8SKent Overstreet 17021c6fdbd8SKent Overstreet /* success: */ 17031c6fdbd8SKent Overstreet 17043f7b9713SHunter Shaffer *m = dev_mi; 17053f7b9713SHunter Shaffer m->last_mount = cpu_to_le64(ktime_get_real_seconds()); 17061c6fdbd8SKent Overstreet c->disk_sb.sb->nr_devices = nr_devices; 17071c6fdbd8SKent Overstreet 17081c6fdbd8SKent Overstreet ca->disk_sb.sb->dev_idx = dev_idx; 17091c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 17101c6fdbd8SKent Overstreet 171102afcb8cSKent Overstreet if (BCH_MEMBER_GROUP(&dev_mi)) { 171202afcb8cSKent Overstreet ret = __bch2_dev_group_set(c, ca, label.buf); 1713e46c181aSKent Overstreet bch_err_msg(c, ret, "creating new label"); 1714cf904c8dSKent Overstreet if (ret) 171502afcb8cSKent Overstreet goto err_unlock; 171602afcb8cSKent Overstreet } 171702afcb8cSKent Overstreet 17181c6fdbd8SKent Overstreet bch2_write_super(c); 17191c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17201c6fdbd8SKent Overstreet 1721180fb49dSKent Overstreet bch2_dev_usage_journal_reserve(c); 1722180fb49dSKent Overstreet 1723d62ab355SKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca); 1724bbe682c7SKent Overstreet bch_err_msg(ca, ret, "marking new superblock"); 1725cf904c8dSKent Overstreet if (ret) 1726bfcf840dSKent Overstreet goto err_late; 17278d6b6222SKent Overstreet 1728c6b2826cSKent Overstreet ret = bch2_fs_freespace_init(c); 1729bbe682c7SKent Overstreet bch_err_msg(ca, ret, "initializing free space"); 1730cf904c8dSKent Overstreet if (ret) 1731c6b2826cSKent Overstreet goto err_late; 1732c6b2826cSKent Overstreet 173309943313SKent Overstreet ca->new_fs_bucket_idx = 0; 173409943313SKent Overstreet 1735f25d8215SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw) 1736f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 17371c6fdbd8SKent Overstreet 17381ada1606SKent Overstreet up_write(&c->state_lock); 17391c6fdbd8SKent Overstreet return 0; 17401c6fdbd8SKent Overstreet 17411c6fdbd8SKent Overstreet err_unlock: 17421c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17431ada1606SKent Overstreet up_write(&c->state_lock); 17441c6fdbd8SKent Overstreet err: 17451c6fdbd8SKent Overstreet if (ca) 17461c6fdbd8SKent Overstreet bch2_dev_free(ca); 17471c6fdbd8SKent Overstreet bch2_free_super(&sb); 174802afcb8cSKent Overstreet printbuf_exit(&label); 1749fa8e94faSKent Overstreet printbuf_exit(&errbuf); 1750cf904c8dSKent Overstreet bch_err_fn(c, ret); 17511c6fdbd8SKent Overstreet return ret; 17521c6fdbd8SKent Overstreet err_late: 1753bfcf840dSKent Overstreet up_write(&c->state_lock); 1754efe68e1dSKent Overstreet ca = NULL; 1755efe68e1dSKent Overstreet goto err; 17561c6fdbd8SKent Overstreet } 17571c6fdbd8SKent Overstreet 17581c6fdbd8SKent Overstreet /* Hot add existing device to running filesystem: */ 17591c6fdbd8SKent Overstreet int bch2_dev_online(struct bch_fs *c, const char *path) 17601c6fdbd8SKent Overstreet { 17611c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 17621c6fdbd8SKent Overstreet struct bch_sb_handle sb = { NULL }; 17631c6fdbd8SKent Overstreet struct bch_dev *ca; 17641c6fdbd8SKent Overstreet unsigned dev_idx; 17651c6fdbd8SKent Overstreet int ret; 17661c6fdbd8SKent Overstreet 17671ada1606SKent Overstreet down_write(&c->state_lock); 17681c6fdbd8SKent Overstreet 17691c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 17701c6fdbd8SKent Overstreet if (ret) { 17711ada1606SKent Overstreet up_write(&c->state_lock); 17721c6fdbd8SKent Overstreet return ret; 17731c6fdbd8SKent Overstreet } 17741c6fdbd8SKent Overstreet 17751c6fdbd8SKent Overstreet dev_idx = sb.sb->dev_idx; 17761c6fdbd8SKent Overstreet 177778c0b75cSKent Overstreet ret = bch2_dev_in_fs(c->disk_sb.sb, sb.sb); 1778e46c181aSKent Overstreet bch_err_msg(c, ret, "bringing %s online", path); 1779cf904c8dSKent Overstreet if (ret) 17801c6fdbd8SKent Overstreet goto err; 17811c6fdbd8SKent Overstreet 1782e2b60560SKent Overstreet ret = bch2_dev_attach_bdev(c, &sb); 1783e2b60560SKent Overstreet if (ret) 1784e2b60560SKent Overstreet goto err; 1785e2b60560SKent Overstreet 17861c6fdbd8SKent Overstreet ca = bch_dev_locked(c, dev_idx); 1787bfcf840dSKent Overstreet 1788e2b60560SKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca); 1789e46c181aSKent Overstreet bch_err_msg(c, ret, "bringing %s online: error from bch2_trans_mark_dev_sb", path); 1790cf904c8dSKent Overstreet if (ret) 1791bfcf840dSKent Overstreet goto err; 1792bfcf840dSKent Overstreet 1793f25d8215SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw) 1794f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 17951c6fdbd8SKent Overstreet 1796bbe682c7SKent Overstreet if (!ca->mi.freespace_initialized) { 1797bbe682c7SKent Overstreet ret = bch2_dev_freespace_init(c, ca, 0, ca->mi.nbuckets); 1798bbe682c7SKent Overstreet bch_err_msg(ca, ret, "initializing free space"); 1799bbe682c7SKent Overstreet if (ret) 1800bbe682c7SKent Overstreet goto err; 1801bbe682c7SKent Overstreet } 1802bbe682c7SKent Overstreet 1803bbe682c7SKent Overstreet if (!ca->journal.nr) { 1804bbe682c7SKent Overstreet ret = bch2_dev_journal_alloc(ca); 1805bbe682c7SKent Overstreet bch_err_msg(ca, ret, "allocating journal"); 1806bbe682c7SKent Overstreet if (ret) 1807bbe682c7SKent Overstreet goto err; 1808bbe682c7SKent Overstreet } 1809bbe682c7SKent Overstreet 18101c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 1811bbe682c7SKent Overstreet bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = 1812a420eea6STim Schlueter cpu_to_le64(ktime_get_real_seconds()); 18131c6fdbd8SKent Overstreet bch2_write_super(c); 18141c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 18151c6fdbd8SKent Overstreet 18161ada1606SKent Overstreet up_write(&c->state_lock); 18171c6fdbd8SKent Overstreet return 0; 18181c6fdbd8SKent Overstreet err: 18191ada1606SKent Overstreet up_write(&c->state_lock); 18201c6fdbd8SKent Overstreet bch2_free_super(&sb); 182178c0b75cSKent Overstreet return ret; 18221c6fdbd8SKent Overstreet } 18231c6fdbd8SKent Overstreet 18241c6fdbd8SKent Overstreet int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) 18251c6fdbd8SKent Overstreet { 18261ada1606SKent Overstreet down_write(&c->state_lock); 18271c6fdbd8SKent Overstreet 18281c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca)) { 18291c6fdbd8SKent Overstreet bch_err(ca, "Already offline"); 18301ada1606SKent Overstreet up_write(&c->state_lock); 18311c6fdbd8SKent Overstreet return 0; 18321c6fdbd8SKent Overstreet } 18331c6fdbd8SKent Overstreet 18342436cb9fSKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 18351c6fdbd8SKent Overstreet bch_err(ca, "Cannot offline required disk"); 18361ada1606SKent Overstreet up_write(&c->state_lock); 183778c0b75cSKent Overstreet return -BCH_ERR_device_state_not_allowed; 18381c6fdbd8SKent Overstreet } 18391c6fdbd8SKent Overstreet 18401c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 18411c6fdbd8SKent Overstreet 18421ada1606SKent Overstreet up_write(&c->state_lock); 18431c6fdbd8SKent Overstreet return 0; 18441c6fdbd8SKent Overstreet } 18451c6fdbd8SKent Overstreet 18461c6fdbd8SKent Overstreet int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) 18471c6fdbd8SKent Overstreet { 18483f7b9713SHunter Shaffer struct bch_member *m; 184969d1f052SKent Overstreet u64 old_nbuckets; 18501c6fdbd8SKent Overstreet int ret = 0; 18511c6fdbd8SKent Overstreet 18521ada1606SKent Overstreet down_write(&c->state_lock); 185369d1f052SKent Overstreet old_nbuckets = ca->mi.nbuckets; 18541c6fdbd8SKent Overstreet 18551c6fdbd8SKent Overstreet if (nbuckets < ca->mi.nbuckets) { 18561c6fdbd8SKent Overstreet bch_err(ca, "Cannot shrink yet"); 18571c6fdbd8SKent Overstreet ret = -EINVAL; 18581c6fdbd8SKent Overstreet goto err; 18591c6fdbd8SKent Overstreet } 18601c6fdbd8SKent Overstreet 18611c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca) && 18621c6fdbd8SKent Overstreet get_capacity(ca->disk_sb.bdev->bd_disk) < 18631c6fdbd8SKent Overstreet ca->mi.bucket_size * nbuckets) { 18641c6fdbd8SKent Overstreet bch_err(ca, "New size larger than device"); 186578c0b75cSKent Overstreet ret = -BCH_ERR_device_size_too_small; 18661c6fdbd8SKent Overstreet goto err; 18671c6fdbd8SKent Overstreet } 18681c6fdbd8SKent Overstreet 18691c6fdbd8SKent Overstreet ret = bch2_dev_buckets_resize(c, ca, nbuckets); 1870e46c181aSKent Overstreet bch_err_msg(ca, ret, "resizing buckets"); 1871cf904c8dSKent Overstreet if (ret) 18721c6fdbd8SKent Overstreet goto err; 18731c6fdbd8SKent Overstreet 1874224ec3e6SKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca); 18753e3e02e6SKent Overstreet if (ret) 1876224ec3e6SKent Overstreet goto err; 1877224ec3e6SKent Overstreet 18781c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 18793f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 18803f7b9713SHunter Shaffer m->nbuckets = cpu_to_le64(nbuckets); 18811c6fdbd8SKent Overstreet 18821c6fdbd8SKent Overstreet bch2_write_super(c); 18831c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 18841c6fdbd8SKent Overstreet 188569d1f052SKent Overstreet if (ca->mi.freespace_initialized) { 188669d1f052SKent Overstreet ret = bch2_dev_freespace_init(c, ca, old_nbuckets, nbuckets); 188769d1f052SKent Overstreet if (ret) 188869d1f052SKent Overstreet goto err; 188969d1f052SKent Overstreet 189069d1f052SKent Overstreet /* 189169d1f052SKent Overstreet * XXX: this is all wrong transactionally - we'll be able to do 189269d1f052SKent Overstreet * this correctly after the disk space accounting rewrite 189369d1f052SKent Overstreet */ 189469d1f052SKent Overstreet ca->usage_base->d[BCH_DATA_free].buckets += nbuckets - old_nbuckets; 189569d1f052SKent Overstreet } 189669d1f052SKent Overstreet 18971c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 18981c6fdbd8SKent Overstreet err: 18991ada1606SKent Overstreet up_write(&c->state_lock); 19001c6fdbd8SKent Overstreet return ret; 19011c6fdbd8SKent Overstreet } 19021c6fdbd8SKent Overstreet 19031c6fdbd8SKent Overstreet /* return with ref on ca->ref: */ 1904bf7e49a4SKent Overstreet struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name) 19051c6fdbd8SKent Overstreet { 19063a402c8dSKent Overstreet rcu_read_lock(); 1907*41b84fb4SKent Overstreet for_each_member_device_rcu(c, ca, NULL) 1908*41b84fb4SKent Overstreet if (!strcmp(name, ca->name)) { 19093a402c8dSKent Overstreet rcu_read_unlock(); 19101c6fdbd8SKent Overstreet return ca; 19111c6fdbd8SKent Overstreet } 1912*41b84fb4SKent Overstreet rcu_read_unlock(); 1913*41b84fb4SKent Overstreet return ERR_PTR(-BCH_ERR_ENOENT_dev_not_found); 1914*41b84fb4SKent Overstreet } 19151c6fdbd8SKent Overstreet 19161c6fdbd8SKent Overstreet /* Filesystem open: */ 19171c6fdbd8SKent Overstreet 19181c6fdbd8SKent Overstreet struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, 19191c6fdbd8SKent Overstreet struct bch_opts opts) 19201c6fdbd8SKent Overstreet { 1921d4c8bb69SKent Overstreet DARRAY(struct bch_sb_handle) sbs = { 0 }; 19221c6fdbd8SKent Overstreet struct bch_fs *c = NULL; 1923defd9e39SKent Overstreet struct bch_sb_handle *best = NULL; 1924fa8e94faSKent Overstreet struct printbuf errbuf = PRINTBUF; 1925e2b60560SKent Overstreet int ret = 0; 19261c6fdbd8SKent Overstreet 1927efe68e1dSKent Overstreet if (!try_module_get(THIS_MODULE)) 1928efe68e1dSKent Overstreet return ERR_PTR(-ENODEV); 1929efe68e1dSKent Overstreet 19301c6fdbd8SKent Overstreet if (!nr_devices) { 1931efe68e1dSKent Overstreet ret = -EINVAL; 1932efe68e1dSKent Overstreet goto err; 19331c6fdbd8SKent Overstreet } 19341c6fdbd8SKent Overstreet 1935d4c8bb69SKent Overstreet ret = darray_make_room(&sbs, nr_devices); 19361c6fdbd8SKent Overstreet if (ret) 19371c6fdbd8SKent Overstreet goto err; 19381c6fdbd8SKent Overstreet 1939d4c8bb69SKent Overstreet for (unsigned i = 0; i < nr_devices; i++) { 1940d4c8bb69SKent Overstreet struct bch_sb_handle sb = { NULL }; 1941d4c8bb69SKent Overstreet 1942d4c8bb69SKent Overstreet ret = bch2_read_super(devices[i], &opts, &sb); 1943d4c8bb69SKent Overstreet if (ret) 1944d4c8bb69SKent Overstreet goto err; 1945d4c8bb69SKent Overstreet 1946d4c8bb69SKent Overstreet BUG_ON(darray_push(&sbs, sb)); 19471c6fdbd8SKent Overstreet } 19481c6fdbd8SKent Overstreet 1949d4c8bb69SKent Overstreet darray_for_each(sbs, sb) 1950d4c8bb69SKent Overstreet if (!best || le64_to_cpu(sb->sb->seq) > le64_to_cpu(best->sb->seq)) 1951d4c8bb69SKent Overstreet best = sb; 19521c6fdbd8SKent Overstreet 1953d4c8bb69SKent Overstreet darray_for_each_reverse(sbs, sb) { 1954d4c8bb69SKent Overstreet if (sb != best && !bch2_dev_exists(best->sb, sb->sb->dev_idx)) { 1955d4c8bb69SKent Overstreet pr_info("%pg has been removed, skipping", sb->bdev); 1956d4c8bb69SKent Overstreet bch2_free_super(sb); 1957d4c8bb69SKent Overstreet darray_remove_item(&sbs, sb); 1958d4c8bb69SKent Overstreet best -= best > sb; 1959625104eaSKent Overstreet continue; 1960625104eaSKent Overstreet } 1961625104eaSKent Overstreet 1962d4c8bb69SKent Overstreet ret = bch2_dev_in_fs(best->sb, sb->sb); 196378c0b75cSKent Overstreet if (ret) 19641c6fdbd8SKent Overstreet goto err_print; 19651c6fdbd8SKent Overstreet } 19661c6fdbd8SKent Overstreet 1967d4c8bb69SKent Overstreet c = bch2_fs_alloc(best->sb, opts); 1968d4c8bb69SKent Overstreet ret = PTR_ERR_OR_ZERO(c); 1969d4c8bb69SKent Overstreet if (ret) 19701c6fdbd8SKent Overstreet goto err; 19711c6fdbd8SKent Overstreet 19721ada1606SKent Overstreet down_write(&c->state_lock); 1973d4c8bb69SKent Overstreet darray_for_each(sbs, sb) { 1974d4c8bb69SKent Overstreet ret = bch2_dev_attach_bdev(c, sb); 1975e2b60560SKent Overstreet if (ret) { 19761ada1606SKent Overstreet up_write(&c->state_lock); 1977e2b60560SKent Overstreet goto err; 1978e2b60560SKent Overstreet } 19791c6fdbd8SKent Overstreet } 19801ada1606SKent Overstreet up_write(&c->state_lock); 19811c6fdbd8SKent Overstreet 198278c0b75cSKent Overstreet if (!bch2_fs_may_start(c)) { 198378c0b75cSKent Overstreet ret = -BCH_ERR_insufficient_devices_to_start; 19841c6fdbd8SKent Overstreet goto err_print; 198578c0b75cSKent Overstreet } 19861c6fdbd8SKent Overstreet 19871c6fdbd8SKent Overstreet if (!c->opts.nostart) { 1988619f5beeSKent Overstreet ret = bch2_fs_start(c); 1989619f5beeSKent Overstreet if (ret) 1990619f5beeSKent Overstreet goto err; 19911c6fdbd8SKent Overstreet } 19921c6fdbd8SKent Overstreet out: 1993d4c8bb69SKent Overstreet darray_for_each(sbs, sb) 1994d4c8bb69SKent Overstreet bch2_free_super(sb); 1995d4c8bb69SKent Overstreet darray_exit(&sbs); 1996fa8e94faSKent Overstreet printbuf_exit(&errbuf); 19971c6fdbd8SKent Overstreet module_put(THIS_MODULE); 19981c6fdbd8SKent Overstreet return c; 19991c6fdbd8SKent Overstreet err_print: 20001c6fdbd8SKent Overstreet pr_err("bch_fs_open err opening %s: %s", 200178c0b75cSKent Overstreet devices[0], bch2_err_str(ret)); 20021c6fdbd8SKent Overstreet err: 20037be9ab63SChris Webb if (!IS_ERR_OR_NULL(c)) 20041c6fdbd8SKent Overstreet bch2_fs_stop(c); 20051c6fdbd8SKent Overstreet c = ERR_PTR(ret); 20061c6fdbd8SKent Overstreet goto out; 20071c6fdbd8SKent Overstreet } 20081c6fdbd8SKent Overstreet 20091c6fdbd8SKent Overstreet /* Global interfaces/init */ 20101c6fdbd8SKent Overstreet 20111c6fdbd8SKent Overstreet static void bcachefs_exit(void) 20121c6fdbd8SKent Overstreet { 20131c6fdbd8SKent Overstreet bch2_debug_exit(); 20141c6fdbd8SKent Overstreet bch2_vfs_exit(); 20151c6fdbd8SKent Overstreet bch2_chardev_exit(); 201614ba3706SKent Overstreet bch2_btree_key_cache_exit(); 20171c6fdbd8SKent Overstreet if (bcachefs_kset) 20181c6fdbd8SKent Overstreet kset_unregister(bcachefs_kset); 20191c6fdbd8SKent Overstreet } 20201c6fdbd8SKent Overstreet 20211c6fdbd8SKent Overstreet static int __init bcachefs_init(void) 20221c6fdbd8SKent Overstreet { 20231c6fdbd8SKent Overstreet bch2_bkey_pack_test(); 20241c6fdbd8SKent Overstreet 20251c6fdbd8SKent Overstreet if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) || 202614ba3706SKent Overstreet bch2_btree_key_cache_init() || 20271c6fdbd8SKent Overstreet bch2_chardev_init() || 20281c6fdbd8SKent Overstreet bch2_vfs_init() || 20291c6fdbd8SKent Overstreet bch2_debug_init()) 20301c6fdbd8SKent Overstreet goto err; 20311c6fdbd8SKent Overstreet 20321c6fdbd8SKent Overstreet return 0; 20331c6fdbd8SKent Overstreet err: 20341c6fdbd8SKent Overstreet bcachefs_exit(); 20351c6fdbd8SKent Overstreet return -ENOMEM; 20361c6fdbd8SKent Overstreet } 20371c6fdbd8SKent Overstreet 20381c6fdbd8SKent Overstreet #define BCH_DEBUG_PARAM(name, description) \ 20391c6fdbd8SKent Overstreet bool bch2_##name; \ 20401c6fdbd8SKent Overstreet module_param_named(name, bch2_##name, bool, 0644); \ 20411c6fdbd8SKent Overstreet MODULE_PARM_DESC(name, description); 20421c6fdbd8SKent Overstreet BCH_DEBUG_PARAMS() 20431c6fdbd8SKent Overstreet #undef BCH_DEBUG_PARAM 20441c6fdbd8SKent Overstreet 2045e08e63e4SKent Overstreet __maybe_unused 204673bd774dSKent Overstreet static unsigned bch2_metadata_version = bcachefs_metadata_version_current; 20471c6fdbd8SKent Overstreet module_param_named(version, bch2_metadata_version, uint, 0400); 20481c6fdbd8SKent Overstreet 20491c6fdbd8SKent Overstreet module_exit(bcachefs_exit); 20501c6fdbd8SKent Overstreet module_init(bcachefs_init); 2051