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" 184409b808SKent Overstreet #include "btree_node_scan.h" 191c6fdbd8SKent Overstreet #include "btree_update_interior.h" 201c6fdbd8SKent Overstreet #include "btree_io.h" 21920e69bcSKent Overstreet #include "btree_write_buffer.h" 2221aec962SKent Overstreet #include "buckets_waiting_for_journal.h" 231c6fdbd8SKent Overstreet #include "chardev.h" 241c6fdbd8SKent Overstreet #include "checksum.h" 251c6fdbd8SKent Overstreet #include "clock.h" 261c6fdbd8SKent Overstreet #include "compress.h" 271c6fdbd8SKent Overstreet #include "debug.h" 281d16c605SKent Overstreet #include "disk_accounting.h" 291c6fdbd8SKent Overstreet #include "disk_groups.h" 30cd575ddfSKent Overstreet #include "ec.h" 31d4bf5eecSKent Overstreet #include "errcode.h" 321c6fdbd8SKent Overstreet #include "error.h" 331c6fdbd8SKent Overstreet #include "fs.h" 341c6fdbd8SKent Overstreet #include "fs-io.h" 35dbbfca9fSKent Overstreet #include "fs-io-buffered.h" 36dbbfca9fSKent Overstreet #include "fs-io-direct.h" 371c6fdbd8SKent Overstreet #include "fsck.h" 381c6fdbd8SKent Overstreet #include "inode.h" 391809b8cbSKent Overstreet #include "io_read.h" 401809b8cbSKent Overstreet #include "io_write.h" 411c6fdbd8SKent Overstreet #include "journal.h" 421c6fdbd8SKent Overstreet #include "journal_reclaim.h" 431dd7f9d9SKent Overstreet #include "journal_seq_blacklist.h" 441c6fdbd8SKent Overstreet #include "move.h" 451c6fdbd8SKent Overstreet #include "migrate.h" 461c6fdbd8SKent Overstreet #include "movinggc.h" 47350175bfSKent Overstreet #include "nocow_locking.h" 481c6fdbd8SKent Overstreet #include "quota.h" 491c6fdbd8SKent Overstreet #include "rebalance.h" 501c6fdbd8SKent Overstreet #include "recovery.h" 511c6fdbd8SKent Overstreet #include "replicas.h" 52a37ad1a3SKent Overstreet #include "sb-clean.h" 533a58dfbcSKent Overstreet #include "sb-counters.h" 54f5d26fa3SKent Overstreet #include "sb-errors.h" 551241df58SHunter Shaffer #include "sb-members.h" 568e877caaSKent Overstreet #include "snapshot.h" 5714b393eeSKent Overstreet #include "subvolume.h" 581c6fdbd8SKent Overstreet #include "super.h" 591c6fdbd8SKent Overstreet #include "super-io.h" 601c6fdbd8SKent Overstreet #include "sysfs.h" 6160e1baa8SKent Overstreet #include "thread_with_file.h" 621c6fdbd8SKent Overstreet #include "trace.h" 631c6fdbd8SKent Overstreet 641c6fdbd8SKent Overstreet #include <linux/backing-dev.h> 651c6fdbd8SKent Overstreet #include <linux/blkdev.h> 661c6fdbd8SKent Overstreet #include <linux/debugfs.h> 671c6fdbd8SKent Overstreet #include <linux/device.h> 681c6fdbd8SKent Overstreet #include <linux/idr.h> 691c6fdbd8SKent Overstreet #include <linux/module.h> 701c6fdbd8SKent Overstreet #include <linux/percpu.h> 711c6fdbd8SKent Overstreet #include <linux/random.h> 721c6fdbd8SKent Overstreet #include <linux/sysfs.h> 731c6fdbd8SKent Overstreet #include <crypto/hash.h> 741c6fdbd8SKent Overstreet 751c6fdbd8SKent Overstreet MODULE_LICENSE("GPL"); 761c6fdbd8SKent Overstreet MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>"); 77a9737e0bSBrian Foster MODULE_DESCRIPTION("bcachefs filesystem"); 7885c6db98SDaniel Hill MODULE_SOFTDEP("pre: crc32c"); 7985c6db98SDaniel Hill MODULE_SOFTDEP("pre: crc64"); 8085c6db98SDaniel Hill MODULE_SOFTDEP("pre: sha256"); 8185c6db98SDaniel Hill MODULE_SOFTDEP("pre: chacha20"); 8285c6db98SDaniel Hill MODULE_SOFTDEP("pre: poly1305"); 8385c6db98SDaniel Hill MODULE_SOFTDEP("pre: xxhash"); 841c6fdbd8SKent Overstreet 853c471b65SKent Overstreet const char * const bch2_fs_flag_strs[] = { 863c471b65SKent Overstreet #define x(n) #n, 873c471b65SKent Overstreet BCH_FS_FLAGS() 883c471b65SKent Overstreet #undef x 893c471b65SKent Overstreet NULL 903c471b65SKent Overstreet }; 913c471b65SKent Overstreet 9236008d5dSKent Overstreet void bch2_print_str(struct bch_fs *c, const char *str) 9336008d5dSKent Overstreet { 9436008d5dSKent Overstreet #ifdef __KERNEL__ 9536008d5dSKent Overstreet struct stdio_redirect *stdio = bch2_fs_stdio_redirect(c); 9636008d5dSKent Overstreet 9736008d5dSKent Overstreet if (unlikely(stdio)) { 9836008d5dSKent Overstreet bch2_stdio_redirect_printf(stdio, true, "%s", str); 9936008d5dSKent Overstreet return; 10036008d5dSKent Overstreet } 10136008d5dSKent Overstreet #endif 10236008d5dSKent Overstreet bch2_print_string_as_lines(KERN_ERR, str); 10336008d5dSKent Overstreet } 10436008d5dSKent Overstreet 1053ed94062SKent Overstreet __printf(2, 0) 106f3589bfaSKent Overstreet static void bch2_print_maybe_redirect(struct stdio_redirect *stdio, const char *fmt, va_list args) 107f3589bfaSKent Overstreet { 108f3589bfaSKent Overstreet #ifdef __KERNEL__ 109f3589bfaSKent Overstreet if (unlikely(stdio)) { 110f3589bfaSKent Overstreet if (fmt[0] == KERN_SOH[0]) 111f3589bfaSKent Overstreet fmt += 2; 112f3589bfaSKent Overstreet 113f3589bfaSKent Overstreet bch2_stdio_redirect_vprintf(stdio, true, fmt, args); 114f3589bfaSKent Overstreet return; 115f3589bfaSKent Overstreet } 116f3589bfaSKent Overstreet #endif 117f3589bfaSKent Overstreet vprintk(fmt, args); 118f3589bfaSKent Overstreet } 119f3589bfaSKent Overstreet 120b63570f7SKent Overstreet void bch2_print_opts(struct bch_opts *opts, const char *fmt, ...) 121b63570f7SKent Overstreet { 122b63570f7SKent Overstreet struct stdio_redirect *stdio = (void *)(unsigned long)opts->stdio; 123b63570f7SKent Overstreet 124b63570f7SKent Overstreet va_list args; 125b63570f7SKent Overstreet va_start(args, fmt); 126f3589bfaSKent Overstreet bch2_print_maybe_redirect(stdio, fmt, args); 127b63570f7SKent Overstreet va_end(args); 128b63570f7SKent Overstreet } 129b63570f7SKent Overstreet 1302b41226dSKent Overstreet void __bch2_print(struct bch_fs *c, const char *fmt, ...) 1312b41226dSKent Overstreet { 13296f37eabSKent Overstreet struct stdio_redirect *stdio = bch2_fs_stdio_redirect(c); 13396f37eabSKent Overstreet 1342b41226dSKent Overstreet va_list args; 1352b41226dSKent Overstreet va_start(args, fmt); 136f3589bfaSKent Overstreet bch2_print_maybe_redirect(stdio, fmt, args); 1372b41226dSKent Overstreet va_end(args); 1382b41226dSKent Overstreet } 1392b41226dSKent Overstreet 1401c6fdbd8SKent Overstreet #define KTYPE(type) \ 1411c6fdbd8SKent Overstreet static const struct attribute_group type ## _group = { \ 1421c6fdbd8SKent Overstreet .attrs = type ## _files \ 1431c6fdbd8SKent Overstreet }; \ 1441c6fdbd8SKent Overstreet \ 1451c6fdbd8SKent Overstreet static const struct attribute_group *type ## _groups[] = { \ 1461c6fdbd8SKent Overstreet &type ## _group, \ 1471c6fdbd8SKent Overstreet NULL \ 1481c6fdbd8SKent Overstreet }; \ 1491c6fdbd8SKent Overstreet \ 1501c6fdbd8SKent Overstreet static const struct kobj_type type ## _ktype = { \ 1511c6fdbd8SKent Overstreet .release = type ## _release, \ 1521c6fdbd8SKent Overstreet .sysfs_ops = &type ## _sysfs_ops, \ 1531c6fdbd8SKent Overstreet .default_groups = type ## _groups \ 1541c6fdbd8SKent Overstreet } 1551c6fdbd8SKent Overstreet 1561c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *); 1571c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *); 158104c6974SDaniel Hill static void bch2_fs_counters_release(struct kobject *k) 159104c6974SDaniel Hill { 160104c6974SDaniel Hill } 1611c6fdbd8SKent Overstreet 1621c6fdbd8SKent Overstreet static void bch2_fs_internal_release(struct kobject *k) 1631c6fdbd8SKent Overstreet { 1641c6fdbd8SKent Overstreet } 1651c6fdbd8SKent Overstreet 1661c6fdbd8SKent Overstreet static void bch2_fs_opts_dir_release(struct kobject *k) 1671c6fdbd8SKent Overstreet { 1681c6fdbd8SKent Overstreet } 1691c6fdbd8SKent Overstreet 1701c6fdbd8SKent Overstreet static void bch2_fs_time_stats_release(struct kobject *k) 1711c6fdbd8SKent Overstreet { 1721c6fdbd8SKent Overstreet } 1731c6fdbd8SKent Overstreet 1741c6fdbd8SKent Overstreet KTYPE(bch2_fs); 175104c6974SDaniel Hill KTYPE(bch2_fs_counters); 1761c6fdbd8SKent Overstreet KTYPE(bch2_fs_internal); 1771c6fdbd8SKent Overstreet KTYPE(bch2_fs_opts_dir); 1781c6fdbd8SKent Overstreet KTYPE(bch2_fs_time_stats); 1791c6fdbd8SKent Overstreet KTYPE(bch2_dev); 1801c6fdbd8SKent Overstreet 1811c6fdbd8SKent Overstreet static struct kset *bcachefs_kset; 1821c6fdbd8SKent Overstreet static LIST_HEAD(bch_fs_list); 1831c6fdbd8SKent Overstreet static DEFINE_MUTEX(bch_fs_list_lock); 1841c6fdbd8SKent Overstreet 185d94189adSKent Overstreet DECLARE_WAIT_QUEUE_HEAD(bch2_read_only_wait); 1861c6fdbd8SKent Overstreet 187*5e3b7232SKent Overstreet static void bch2_dev_unlink(struct bch_dev *); 1881c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *); 1891c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *, unsigned); 1901c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *); 1911c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *); 1921c6fdbd8SKent Overstreet 1931c6fdbd8SKent Overstreet struct bch_fs *bch2_dev_to_fs(dev_t dev) 1941c6fdbd8SKent Overstreet { 1951c6fdbd8SKent Overstreet struct bch_fs *c; 1961c6fdbd8SKent Overstreet 1971c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 1981c6fdbd8SKent Overstreet rcu_read_lock(); 1991c6fdbd8SKent Overstreet 2001c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 20141b84fb4SKent Overstreet for_each_member_device_rcu(c, ca, NULL) 202ec4ab9d2SDan Robertson if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) { 2031c6fdbd8SKent Overstreet closure_get(&c->cl); 2041c6fdbd8SKent Overstreet goto found; 2051c6fdbd8SKent Overstreet } 2061c6fdbd8SKent Overstreet c = NULL; 2071c6fdbd8SKent Overstreet found: 2081c6fdbd8SKent Overstreet rcu_read_unlock(); 2091c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 2101c6fdbd8SKent Overstreet 2111c6fdbd8SKent Overstreet return c; 2121c6fdbd8SKent Overstreet } 2131c6fdbd8SKent Overstreet 2141c6fdbd8SKent Overstreet static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid) 2151c6fdbd8SKent Overstreet { 2161c6fdbd8SKent Overstreet struct bch_fs *c; 2171c6fdbd8SKent Overstreet 2181c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 2191c6fdbd8SKent Overstreet 2201c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 2211c6fdbd8SKent Overstreet if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid))) 2221c6fdbd8SKent Overstreet return c; 2231c6fdbd8SKent Overstreet 2241c6fdbd8SKent Overstreet return NULL; 2251c6fdbd8SKent Overstreet } 2261c6fdbd8SKent Overstreet 2271c6fdbd8SKent Overstreet struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid) 2281c6fdbd8SKent Overstreet { 2291c6fdbd8SKent Overstreet struct bch_fs *c; 2301c6fdbd8SKent Overstreet 2311c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 2321c6fdbd8SKent Overstreet c = __bch2_uuid_to_fs(uuid); 2331c6fdbd8SKent Overstreet if (c) 2341c6fdbd8SKent Overstreet closure_get(&c->cl); 2351c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 2361c6fdbd8SKent Overstreet 2371c6fdbd8SKent Overstreet return c; 2381c6fdbd8SKent Overstreet } 2391c6fdbd8SKent Overstreet 2401c6fdbd8SKent Overstreet /* Filesystem RO/RW: */ 2411c6fdbd8SKent Overstreet 2421c6fdbd8SKent Overstreet /* 2431c6fdbd8SKent Overstreet * For startup/shutdown of RW stuff, the dependencies are: 2441c6fdbd8SKent Overstreet * 2451c6fdbd8SKent Overstreet * - foreground writes depend on copygc and rebalance (to free up space) 2461c6fdbd8SKent Overstreet * 2471c6fdbd8SKent Overstreet * - copygc and rebalance depend on mark and sweep gc (they actually probably 2481c6fdbd8SKent Overstreet * don't because they either reserve ahead of time or don't block if 2491c6fdbd8SKent Overstreet * allocations fail, but allocations can require mark and sweep gc to run 2501c6fdbd8SKent Overstreet * because of generation number wraparound) 2511c6fdbd8SKent Overstreet * 2521c6fdbd8SKent Overstreet * - all of the above depends on the allocator threads 2531c6fdbd8SKent Overstreet * 2541c6fdbd8SKent Overstreet * - allocator depends on the journal (when it rewrites prios and gens) 2551c6fdbd8SKent Overstreet */ 2561c6fdbd8SKent Overstreet 2571c6fdbd8SKent Overstreet static void __bch2_fs_read_only(struct bch_fs *c) 2581c6fdbd8SKent Overstreet { 2599fea2274SKent Overstreet unsigned clean_passes = 0; 260c0960603SKent Overstreet u64 seq = 0; 2611c6fdbd8SKent Overstreet 262b40901b0SKent Overstreet bch2_fs_ec_stop(c); 263b40901b0SKent Overstreet bch2_open_buckets_stop(c, NULL, true); 2641c6fdbd8SKent Overstreet bch2_rebalance_stop(c); 265e6d11615SKent Overstreet bch2_copygc_stop(c); 266b40901b0SKent Overstreet bch2_fs_ec_flush(c); 2671c6fdbd8SKent Overstreet 26883ec519aSKent Overstreet bch_verbose(c, "flushing journal and stopping allocators, journal seq %llu", 26983ec519aSKent Overstreet journal_cur_seq(&c->journal)); 2701c6fdbd8SKent Overstreet 271039fc4c5SKent Overstreet do { 272039fc4c5SKent Overstreet clean_passes++; 273039fc4c5SKent Overstreet 274c0960603SKent Overstreet if (bch2_btree_interior_updates_flush(c) || 275c0960603SKent Overstreet bch2_journal_flush_all_pins(&c->journal) || 276c0960603SKent Overstreet bch2_btree_flush_all_writes(c) || 277c0960603SKent Overstreet seq != atomic64_read(&c->journal.seq)) { 278c0960603SKent Overstreet seq = atomic64_read(&c->journal.seq); 279039fc4c5SKent Overstreet clean_passes = 0; 280039fc4c5SKent Overstreet } 281d5f70c1fSKent Overstreet } while (clean_passes < 2); 282c0960603SKent Overstreet 28383ec519aSKent Overstreet bch_verbose(c, "flushing journal and stopping allocators complete, journal seq %llu", 28483ec519aSKent Overstreet journal_cur_seq(&c->journal)); 2852340fd9dSKent Overstreet 286b895c703SKent Overstreet if (test_bit(JOURNAL_replay_done, &c->journal.flags) && 2873c471b65SKent Overstreet !test_bit(BCH_FS_emergency_ro, &c->flags)) 2883c471b65SKent Overstreet set_bit(BCH_FS_clean_shutdown, &c->flags); 2899802ff48SKent Overstreet 2901c6fdbd8SKent Overstreet bch2_fs_journal_stop(&c->journal); 2911c6fdbd8SKent Overstreet 2929802ff48SKent Overstreet bch_info(c, "%sshutdown complete, journal seq %llu", 2939802ff48SKent Overstreet test_bit(BCH_FS_clean_shutdown, &c->flags) ? "" : "un", 2949802ff48SKent Overstreet c->journal.seq_ondisk); 2959802ff48SKent Overstreet 2961c6fdbd8SKent Overstreet /* 2971c6fdbd8SKent Overstreet * After stopping journal: 2981c6fdbd8SKent Overstreet */ 2999fea2274SKent Overstreet for_each_member_device(c, ca) 3001c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 3011c6fdbd8SKent Overstreet } 3021c6fdbd8SKent Overstreet 303d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 3041c6fdbd8SKent Overstreet static void bch2_writes_disabled(struct percpu_ref *writes) 3051c6fdbd8SKent Overstreet { 3061c6fdbd8SKent Overstreet struct bch_fs *c = container_of(writes, struct bch_fs, writes); 3071c6fdbd8SKent Overstreet 3083c471b65SKent Overstreet set_bit(BCH_FS_write_disable_complete, &c->flags); 309d94189adSKent Overstreet wake_up(&bch2_read_only_wait); 3101c6fdbd8SKent Overstreet } 311d94189adSKent Overstreet #endif 3121c6fdbd8SKent Overstreet 3131c6fdbd8SKent Overstreet void bch2_fs_read_only(struct bch_fs *c) 3141c6fdbd8SKent Overstreet { 3153c471b65SKent Overstreet if (!test_bit(BCH_FS_rw, &c->flags)) { 3169ae28f82SKent Overstreet bch2_journal_reclaim_stop(&c->journal); 3171c6fdbd8SKent Overstreet return; 318134915f3SKent Overstreet } 3191c6fdbd8SKent Overstreet 3203c471b65SKent Overstreet BUG_ON(test_bit(BCH_FS_write_disable_complete, &c->flags)); 3211c6fdbd8SKent Overstreet 322e7f7ddedSKent Overstreet bch_verbose(c, "going read-only"); 323e7f7ddedSKent Overstreet 3241c6fdbd8SKent Overstreet /* 3251c6fdbd8SKent Overstreet * Block new foreground-end write operations from starting - any new 3261c6fdbd8SKent Overstreet * writes will return -EROFS: 3271c6fdbd8SKent Overstreet */ 3283c471b65SKent Overstreet set_bit(BCH_FS_going_ro, &c->flags); 329d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 3301c6fdbd8SKent Overstreet percpu_ref_kill(&c->writes); 331d94189adSKent Overstreet #else 332d94189adSKent Overstreet for (unsigned i = 0; i < BCH_WRITE_REF_NR; i++) 333d94189adSKent Overstreet bch2_write_ref_put(c, i); 334d94189adSKent Overstreet #endif 3351c6fdbd8SKent Overstreet 3361c6fdbd8SKent Overstreet /* 3371c6fdbd8SKent Overstreet * If we're not doing an emergency shutdown, we want to wait on 3381c6fdbd8SKent Overstreet * outstanding writes to complete so they don't see spurious errors due 3391c6fdbd8SKent Overstreet * to shutting down the allocator: 3401c6fdbd8SKent Overstreet * 3411c6fdbd8SKent Overstreet * If we are doing an emergency shutdown outstanding writes may 3421c6fdbd8SKent Overstreet * hang until we shutdown the allocator so we don't want to wait 3431c6fdbd8SKent Overstreet * on outstanding writes before shutting everything down - but 3441c6fdbd8SKent Overstreet * we do need to wait on them before returning and signalling 3451c6fdbd8SKent Overstreet * that going RO is complete: 3461c6fdbd8SKent Overstreet */ 347d94189adSKent Overstreet wait_event(bch2_read_only_wait, 3483c471b65SKent Overstreet test_bit(BCH_FS_write_disable_complete, &c->flags) || 3493c471b65SKent Overstreet test_bit(BCH_FS_emergency_ro, &c->flags)); 3501c6fdbd8SKent Overstreet 3513c471b65SKent Overstreet bool writes_disabled = test_bit(BCH_FS_write_disable_complete, &c->flags); 352e7f7ddedSKent Overstreet if (writes_disabled) 353e7f7ddedSKent Overstreet bch_verbose(c, "finished waiting for writes to stop"); 354e7f7ddedSKent Overstreet 3551c6fdbd8SKent Overstreet __bch2_fs_read_only(c); 3561c6fdbd8SKent Overstreet 357d94189adSKent Overstreet wait_event(bch2_read_only_wait, 3583c471b65SKent Overstreet test_bit(BCH_FS_write_disable_complete, &c->flags)); 3591c6fdbd8SKent Overstreet 360e7f7ddedSKent Overstreet if (!writes_disabled) 361e7f7ddedSKent Overstreet bch_verbose(c, "finished waiting for writes to stop"); 362e7f7ddedSKent Overstreet 3633c471b65SKent Overstreet clear_bit(BCH_FS_write_disable_complete, &c->flags); 3643c471b65SKent Overstreet clear_bit(BCH_FS_going_ro, &c->flags); 3653c471b65SKent Overstreet clear_bit(BCH_FS_rw, &c->flags); 3661c6fdbd8SKent Overstreet 3671c6fdbd8SKent Overstreet if (!bch2_journal_error(&c->journal) && 3683c471b65SKent Overstreet !test_bit(BCH_FS_error, &c->flags) && 3693c471b65SKent Overstreet !test_bit(BCH_FS_emergency_ro, &c->flags) && 3703c471b65SKent Overstreet test_bit(BCH_FS_started, &c->flags) && 3713c471b65SKent Overstreet test_bit(BCH_FS_clean_shutdown, &c->flags) && 37213c1e583SKent Overstreet c->recovery_pass_done >= BCH_RECOVERY_PASS_journal_replay) { 3737724664fSKent Overstreet BUG_ON(c->journal.last_empty_seq != journal_cur_seq(&c->journal)); 374691f2cbaSKent Overstreet BUG_ON(atomic_long_read(&c->btree_cache.nr_dirty)); 3757724664fSKent Overstreet BUG_ON(atomic_long_read(&c->btree_key_cache.nr_dirty)); 37609caeabeSKent Overstreet BUG_ON(c->btree_write_buffer.inc.keys.nr); 37709caeabeSKent Overstreet BUG_ON(c->btree_write_buffer.flushing.keys.nr); 3785668e5deSKent Overstreet bch2_verify_accounting_clean(c); 3797724664fSKent Overstreet 380b2930396SKent Overstreet bch_verbose(c, "marking filesystem clean"); 381134915f3SKent Overstreet bch2_fs_mark_clean(c); 382e7f7ddedSKent Overstreet } else { 383e7f7ddedSKent Overstreet bch_verbose(c, "done going read-only, filesystem not clean"); 384b2930396SKent Overstreet } 3851c6fdbd8SKent Overstreet } 3861c6fdbd8SKent Overstreet 3871c6fdbd8SKent Overstreet static void bch2_fs_read_only_work(struct work_struct *work) 3881c6fdbd8SKent Overstreet { 3891c6fdbd8SKent Overstreet struct bch_fs *c = 3901c6fdbd8SKent Overstreet container_of(work, struct bch_fs, read_only_work); 3911c6fdbd8SKent Overstreet 3921ada1606SKent Overstreet down_write(&c->state_lock); 3931c6fdbd8SKent Overstreet bch2_fs_read_only(c); 3941ada1606SKent Overstreet up_write(&c->state_lock); 3951c6fdbd8SKent Overstreet } 3961c6fdbd8SKent Overstreet 3971c6fdbd8SKent Overstreet static void bch2_fs_read_only_async(struct bch_fs *c) 3981c6fdbd8SKent Overstreet { 3991c6fdbd8SKent Overstreet queue_work(system_long_wq, &c->read_only_work); 4001c6fdbd8SKent Overstreet } 4011c6fdbd8SKent Overstreet 4021c6fdbd8SKent Overstreet bool bch2_fs_emergency_read_only(struct bch_fs *c) 4031c6fdbd8SKent Overstreet { 4043c471b65SKent Overstreet bool ret = !test_and_set_bit(BCH_FS_emergency_ro, &c->flags); 4051c6fdbd8SKent Overstreet 4061c6fdbd8SKent Overstreet bch2_journal_halt(&c->journal); 4079f115ce9SKent Overstreet bch2_fs_read_only_async(c); 4081c6fdbd8SKent Overstreet 409d94189adSKent Overstreet wake_up(&bch2_read_only_wait); 4101c6fdbd8SKent Overstreet return ret; 4111c6fdbd8SKent Overstreet } 4121c6fdbd8SKent Overstreet 413134915f3SKent Overstreet static int bch2_fs_read_write_late(struct bch_fs *c) 4141c6fdbd8SKent Overstreet { 415134915f3SKent Overstreet int ret; 4161c6fdbd8SKent Overstreet 417ea28c867SKent Overstreet /* 418ea28c867SKent Overstreet * Data move operations can't run until after check_snapshots has 419ea28c867SKent Overstreet * completed, and bch2_snapshot_is_ancestor() is available. 420ea28c867SKent Overstreet * 421ea28c867SKent Overstreet * Ideally we'd start copygc/rebalance earlier instead of waiting for 422ea28c867SKent Overstreet * all of recovery/fsck to complete: 423ea28c867SKent Overstreet */ 424ea28c867SKent Overstreet ret = bch2_copygc_start(c); 425ea28c867SKent Overstreet if (ret) { 426ea28c867SKent Overstreet bch_err(c, "error starting copygc thread"); 427ea28c867SKent Overstreet return ret; 428ea28c867SKent Overstreet } 429ea28c867SKent Overstreet 430134915f3SKent Overstreet ret = bch2_rebalance_start(c); 431134915f3SKent Overstreet if (ret) { 432134915f3SKent Overstreet bch_err(c, "error starting rebalance thread"); 433134915f3SKent Overstreet return ret; 434134915f3SKent Overstreet } 435134915f3SKent Overstreet 436134915f3SKent Overstreet return 0; 437134915f3SKent Overstreet } 438134915f3SKent Overstreet 439e731d466SKent Overstreet static int __bch2_fs_read_write(struct bch_fs *c, bool early) 440134915f3SKent Overstreet { 441134915f3SKent Overstreet int ret; 442134915f3SKent Overstreet 4433c471b65SKent Overstreet if (test_bit(BCH_FS_initial_gc_unfixed, &c->flags)) { 444aae15aafSKent Overstreet bch_err(c, "cannot go rw, unfixed btree errors"); 4457c50140fSKent Overstreet return -BCH_ERR_erofs_unfixed_errors; 446aae15aafSKent Overstreet } 447aae15aafSKent Overstreet 4483c471b65SKent Overstreet if (test_bit(BCH_FS_rw, &c->flags)) 449134915f3SKent Overstreet return 0; 450134915f3SKent Overstreet 4512c944fa1SKent Overstreet bch_info(c, "going read-write"); 4522c944fa1SKent Overstreet 453f5d26fa3SKent Overstreet ret = bch2_sb_members_v2_init(c); 4543f7b9713SHunter Shaffer if (ret) 4553f7b9713SHunter Shaffer goto err; 4563f7b9713SHunter Shaffer 457134915f3SKent Overstreet ret = bch2_fs_mark_dirty(c); 458134915f3SKent Overstreet if (ret) 459134915f3SKent Overstreet goto err; 4601c6fdbd8SKent Overstreet 4613c471b65SKent Overstreet clear_bit(BCH_FS_clean_shutdown, &c->flags); 4622340fd9dSKent Overstreet 463b9004e85SKent Overstreet /* 464b9004e85SKent Overstreet * First journal write must be a flush write: after a clean shutdown we 465b9004e85SKent Overstreet * don't read the journal, so the first journal write may end up 466b9004e85SKent Overstreet * overwriting whatever was there previously, and there must always be 467b9004e85SKent Overstreet * at least one non-flush write in the journal or recovery will fail: 468b9004e85SKent Overstreet */ 469b895c703SKent Overstreet set_bit(JOURNAL_need_flush_write, &c->journal.flags); 470b895c703SKent Overstreet set_bit(JOURNAL_running, &c->journal.flags); 471b9004e85SKent Overstreet 4729fea2274SKent Overstreet for_each_rw_member(c, ca) 4731c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 4741c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 4751c6fdbd8SKent Overstreet 4763c471b65SKent Overstreet set_bit(BCH_FS_rw, &c->flags); 4773c471b65SKent Overstreet set_bit(BCH_FS_was_rw, &c->flags); 478468035caSKent Overstreet 479468035caSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 480468035caSKent Overstreet percpu_ref_reinit(&c->writes); 481468035caSKent Overstreet #else 4829fea2274SKent Overstreet for (unsigned i = 0; i < BCH_WRITE_REF_NR; i++) { 483468035caSKent Overstreet BUG_ON(atomic_long_read(&c->writes[i])); 484468035caSKent Overstreet atomic_long_inc(&c->writes[i]); 485468035caSKent Overstreet } 486468035caSKent Overstreet #endif 487468035caSKent 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 { 51313c1e583SKent Overstreet if (c->opts.recovery_pass_last && 51413c1e583SKent Overstreet c->opts.recovery_pass_last < BCH_RECOVERY_PASS_journal_replay) 51562719cf3SKent Overstreet return -BCH_ERR_erofs_norecovery; 51662719cf3SKent Overstreet 51762719cf3SKent Overstreet if (c->opts.nochanges) 51862719cf3SKent Overstreet return -BCH_ERR_erofs_nochanges; 51962719cf3SKent Overstreet 520134915f3SKent Overstreet return __bch2_fs_read_write(c, false); 521134915f3SKent Overstreet } 522134915f3SKent Overstreet 523134915f3SKent Overstreet int bch2_fs_read_write_early(struct bch_fs *c) 524134915f3SKent Overstreet { 525134915f3SKent Overstreet lockdep_assert_held(&c->state_lock); 526134915f3SKent Overstreet 527134915f3SKent Overstreet return __bch2_fs_read_write(c, true); 5281c6fdbd8SKent Overstreet } 5291c6fdbd8SKent Overstreet 5301c6fdbd8SKent Overstreet /* Filesystem startup/shutdown: */ 5311c6fdbd8SKent Overstreet 532d5e4dcc2SKent Overstreet static void __bch2_fs_free(struct bch_fs *c) 5331c6fdbd8SKent Overstreet { 534ffcbec60SKent Overstreet for (unsigned i = 0; i < BCH_TIME_STAT_NR; i++) 5351c6fdbd8SKent Overstreet bch2_time_stats_exit(&c->times[i]); 5361c6fdbd8SKent Overstreet 5374409b808SKent Overstreet bch2_find_btree_nodes_exit(&c->found_btree_nodes); 538a1f26d70SKent Overstreet bch2_free_pending_node_rewrites(c); 5391d16c605SKent Overstreet bch2_fs_accounting_exit(c); 540f5d26fa3SKent Overstreet bch2_fs_sb_errors_exit(c); 541104c6974SDaniel Hill bch2_fs_counters_exit(c); 54214b393eeSKent Overstreet bch2_fs_snapshots_exit(c); 5431c6fdbd8SKent Overstreet bch2_fs_quota_exit(c); 544dbbfca9fSKent Overstreet bch2_fs_fs_io_direct_exit(c); 545dbbfca9fSKent Overstreet bch2_fs_fs_io_buffered_exit(c); 5461c6fdbd8SKent Overstreet bch2_fs_fsio_exit(c); 547112d21fdSKent Overstreet bch2_fs_vfs_exit(c); 548cd575ddfSKent Overstreet bch2_fs_ec_exit(c); 5491c6fdbd8SKent Overstreet bch2_fs_encryption_exit(c); 5501e3b4098SKent Overstreet bch2_fs_nocow_locking_exit(c); 5511809b8cbSKent Overstreet bch2_fs_io_write_exit(c); 5521809b8cbSKent Overstreet bch2_fs_io_read_exit(c); 55321aec962SKent Overstreet bch2_fs_buckets_waiting_for_journal_exit(c); 554c823c339SKent Overstreet bch2_fs_btree_interior_update_exit(c); 5552ca88e5aSKent Overstreet bch2_fs_btree_key_cache_exit(&c->btree_key_cache); 5561c6fdbd8SKent Overstreet bch2_fs_btree_cache_exit(c); 557d293ece1SKent Overstreet bch2_fs_btree_iter_exit(c); 5589620c3ecSKent Overstreet bch2_fs_replicas_exit(c); 5591c6fdbd8SKent Overstreet bch2_fs_journal_exit(&c->journal); 5601c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[WRITE]); 5611c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[READ]); 5621c6fdbd8SKent Overstreet bch2_fs_compress_exit(c); 5638a443d3eSKent Overstreet bch2_journal_keys_put_initial(c); 5644409b808SKent Overstreet bch2_find_btree_nodes_exit(&c->found_btree_nodes); 5658a443d3eSKent Overstreet BUG_ON(atomic_read(&c->journal_keys.ref)); 566920e69bcSKent Overstreet bch2_fs_btree_write_buffer_exit(c); 5679166b41dSKent Overstreet percpu_free_rwsem(&c->mark_lock); 568759b2e80SKent Overstreet if (c->online_reserved) { 569759b2e80SKent Overstreet u64 v = percpu_u64_get(c->online_reserved); 570759b2e80SKent Overstreet WARN(v, "online_reserved not 0 at shutdown: %lli", v); 5715e82a9a1SKent Overstreet free_percpu(c->online_reserved); 572759b2e80SKent Overstreet } 5731a21bf98SKent Overstreet 574faa6cb6cSKent Overstreet darray_exit(&c->btree_roots_extra); 5755663a415SKent Overstreet free_percpu(c->pcpu); 5768bb8d683SKent Overstreet free_percpu(c->usage); 57735189e09SKent Overstreet mempool_exit(&c->large_bkey_pool); 5781c6fdbd8SKent Overstreet mempool_exit(&c->btree_bounce_pool); 5791c6fdbd8SKent Overstreet bioset_exit(&c->btree_bio); 5801c6fdbd8SKent Overstreet mempool_exit(&c->fill_iter); 581d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 5821c6fdbd8SKent Overstreet percpu_ref_exit(&c->writes); 583d94189adSKent Overstreet #endif 5841c6fdbd8SKent Overstreet kfree(rcu_dereference_protected(c->disk_groups, 1)); 5851dd7f9d9SKent Overstreet kfree(c->journal_seq_blacklist_table); 586b5e8a699SKent Overstreet kfree(c->unused_inode_hints); 5871c6fdbd8SKent Overstreet 5888bff9875SBrian Foster if (c->write_ref_wq) 5898bff9875SBrian Foster destroy_workqueue(c->write_ref_wq); 590161f73c2SKent Overstreet if (c->btree_write_submit_wq) 591161f73c2SKent Overstreet destroy_workqueue(c->btree_write_submit_wq); 592161f73c2SKent Overstreet if (c->btree_read_complete_wq) 593161f73c2SKent Overstreet destroy_workqueue(c->btree_read_complete_wq); 5941c6fdbd8SKent Overstreet if (c->copygc_wq) 5951c6fdbd8SKent Overstreet destroy_workqueue(c->copygc_wq); 5969f1833caSKent Overstreet if (c->btree_io_complete_wq) 5979f1833caSKent Overstreet destroy_workqueue(c->btree_io_complete_wq); 598731bdd2eSKent Overstreet if (c->btree_update_wq) 599731bdd2eSKent Overstreet destroy_workqueue(c->btree_update_wq); 6001c6fdbd8SKent Overstreet 6019d8022dbSKent Overstreet bch2_free_super(&c->disk_sb); 602cb6fc943SKent Overstreet kvfree(c); 6031c6fdbd8SKent Overstreet module_put(THIS_MODULE); 6041c6fdbd8SKent Overstreet } 6051c6fdbd8SKent Overstreet 6061c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *kobj) 6071c6fdbd8SKent Overstreet { 6081c6fdbd8SKent Overstreet struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); 6091c6fdbd8SKent Overstreet 610d5e4dcc2SKent Overstreet __bch2_fs_free(c); 6111c6fdbd8SKent Overstreet } 6121c6fdbd8SKent Overstreet 613d5e4dcc2SKent Overstreet void __bch2_fs_stop(struct bch_fs *c) 6141c6fdbd8SKent Overstreet { 615af1c6871SKent Overstreet bch_verbose(c, "shutting down"); 616af1c6871SKent Overstreet 6173c471b65SKent Overstreet set_bit(BCH_FS_stopping, &c->flags); 6181dd7f9d9SKent Overstreet 6191ada1606SKent Overstreet down_write(&c->state_lock); 620883f1a7cSKent Overstreet bch2_fs_read_only(c); 6211ada1606SKent Overstreet up_write(&c->state_lock); 622883f1a7cSKent Overstreet 6239fea2274SKent Overstreet for_each_member_device(c, ca) 624*5e3b7232SKent Overstreet bch2_dev_unlink(ca); 6251c6fdbd8SKent Overstreet 6261c6fdbd8SKent Overstreet if (c->kobj.state_in_sysfs) 6271c6fdbd8SKent Overstreet kobject_del(&c->kobj); 6281c6fdbd8SKent Overstreet 6291c6fdbd8SKent Overstreet bch2_fs_debug_exit(c); 6301c6fdbd8SKent Overstreet bch2_fs_chardev_exit(c); 6311c6fdbd8SKent Overstreet 63263508b75SKent Overstreet bch2_ro_ref_put(c); 63363508b75SKent Overstreet wait_event(c->ro_ref_wait, !refcount_read(&c->ro_ref)); 63463508b75SKent Overstreet 635104c6974SDaniel Hill kobject_put(&c->counters_kobj); 6361c6fdbd8SKent Overstreet kobject_put(&c->time_stats); 6371c6fdbd8SKent Overstreet kobject_put(&c->opts_dir); 6381c6fdbd8SKent Overstreet kobject_put(&c->internal); 6391c6fdbd8SKent Overstreet 6401c6fdbd8SKent Overstreet /* btree prefetch might have kicked off reads in the background: */ 6411c6fdbd8SKent Overstreet bch2_btree_flush_all_reads(c); 6421c6fdbd8SKent Overstreet 6439fea2274SKent Overstreet for_each_member_device(c, ca) 6441c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 6451c6fdbd8SKent Overstreet 6461c6fdbd8SKent Overstreet cancel_work_sync(&c->read_only_work); 647d5e4dcc2SKent Overstreet } 6481c6fdbd8SKent Overstreet 649d5e4dcc2SKent Overstreet void bch2_fs_free(struct bch_fs *c) 650d5e4dcc2SKent Overstreet { 651d5e4dcc2SKent Overstreet unsigned i; 652d5e4dcc2SKent Overstreet 653d5e4dcc2SKent Overstreet mutex_lock(&bch_fs_list_lock); 654d5e4dcc2SKent Overstreet list_del(&c->list); 655d5e4dcc2SKent Overstreet mutex_unlock(&bch_fs_list_lock); 656d5e4dcc2SKent Overstreet 657d5e4dcc2SKent Overstreet closure_sync(&c->cl); 658d5e4dcc2SKent Overstreet closure_debug_destroy(&c->cl); 659d5e4dcc2SKent Overstreet 660d5e4dcc2SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) { 661d5e4dcc2SKent Overstreet struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true); 662d5e4dcc2SKent Overstreet 663d5e4dcc2SKent Overstreet if (ca) { 664552aa548SKent Overstreet EBUG_ON(atomic_long_read(&ca->ref) != 1); 665d5e4dcc2SKent Overstreet bch2_free_super(&ca->disk_sb); 666d5e4dcc2SKent Overstreet bch2_dev_free(ca); 667d5e4dcc2SKent Overstreet } 668d5e4dcc2SKent Overstreet } 6691c6fdbd8SKent Overstreet 670af1c6871SKent Overstreet bch_verbose(c, "shutdown complete"); 671af1c6871SKent Overstreet 6721c6fdbd8SKent Overstreet kobject_put(&c->kobj); 6731c6fdbd8SKent Overstreet } 6741c6fdbd8SKent Overstreet 675d5e4dcc2SKent Overstreet void bch2_fs_stop(struct bch_fs *c) 676d5e4dcc2SKent Overstreet { 677d5e4dcc2SKent Overstreet __bch2_fs_stop(c); 678d5e4dcc2SKent Overstreet bch2_fs_free(c); 679d5e4dcc2SKent Overstreet } 680d5e4dcc2SKent Overstreet 681e2b60560SKent Overstreet static int bch2_fs_online(struct bch_fs *c) 6821c6fdbd8SKent Overstreet { 683e2b60560SKent Overstreet int ret = 0; 6841c6fdbd8SKent Overstreet 6851c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 6861c6fdbd8SKent Overstreet 687e2b60560SKent Overstreet if (__bch2_uuid_to_fs(c->sb.uuid)) { 688e2b60560SKent Overstreet bch_err(c, "filesystem UUID already open"); 689e2b60560SKent Overstreet return -EINVAL; 690e2b60560SKent Overstreet } 6911c6fdbd8SKent Overstreet 6921c6fdbd8SKent Overstreet ret = bch2_fs_chardev_init(c); 693e2b60560SKent Overstreet if (ret) { 694e2b60560SKent Overstreet bch_err(c, "error creating character device"); 695e2b60560SKent Overstreet return ret; 696e2b60560SKent Overstreet } 6971c6fdbd8SKent Overstreet 6981c6fdbd8SKent Overstreet bch2_fs_debug_init(c); 6991c6fdbd8SKent Overstreet 700e2b60560SKent Overstreet ret = kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ?: 701e2b60560SKent Overstreet kobject_add(&c->internal, &c->kobj, "internal") ?: 702e2b60560SKent Overstreet kobject_add(&c->opts_dir, &c->kobj, "options") ?: 703066a2646SKent Overstreet #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT 704e2b60560SKent Overstreet kobject_add(&c->time_stats, &c->kobj, "time_stats") ?: 705066a2646SKent Overstreet #endif 706104c6974SDaniel Hill kobject_add(&c->counters_kobj, &c->kobj, "counters") ?: 707e2b60560SKent Overstreet bch2_opts_create_sysfs_files(&c->opts_dir); 708e2b60560SKent Overstreet if (ret) { 709e2b60560SKent Overstreet bch_err(c, "error creating sysfs objects"); 710e2b60560SKent Overstreet return ret; 711e2b60560SKent Overstreet } 7121c6fdbd8SKent Overstreet 7131ada1606SKent Overstreet down_write(&c->state_lock); 7141c6fdbd8SKent Overstreet 7159fea2274SKent Overstreet for_each_member_device(c, ca) { 716e2b60560SKent Overstreet ret = bch2_dev_sysfs_online(c, ca); 717e2b60560SKent Overstreet if (ret) { 718e2b60560SKent Overstreet bch_err(c, "error creating sysfs objects"); 719f295298bSKent Overstreet bch2_dev_put(ca); 7201c6fdbd8SKent Overstreet goto err; 7213a402c8dSKent Overstreet } 722e2b60560SKent Overstreet } 7231c6fdbd8SKent Overstreet 724e2b60560SKent Overstreet BUG_ON(!list_empty(&c->list)); 7251c6fdbd8SKent Overstreet list_add(&c->list, &bch_fs_list); 7261c6fdbd8SKent Overstreet err: 7271ada1606SKent Overstreet up_write(&c->state_lock); 728e2b60560SKent Overstreet return ret; 7291c6fdbd8SKent Overstreet } 7301c6fdbd8SKent Overstreet 7311c6fdbd8SKent Overstreet static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts) 7321c6fdbd8SKent Overstreet { 7331c6fdbd8SKent Overstreet struct bch_fs *c; 734401ec4dbSKent Overstreet struct printbuf name = PRINTBUF; 735ecf37a4aSKent Overstreet unsigned i, iter_size; 7367be9ab63SChris Webb int ret = 0; 7371c6fdbd8SKent Overstreet 738cb6fc943SKent Overstreet c = kvmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO); 7397be9ab63SChris Webb if (!c) { 74065d48e35SKent Overstreet c = ERR_PTR(-BCH_ERR_ENOMEM_fs_alloc); 7411c6fdbd8SKent Overstreet goto out; 7427be9ab63SChris Webb } 7431c6fdbd8SKent Overstreet 74496f37eabSKent Overstreet c->stdio = (void *)(unsigned long) opts.stdio; 7452b41226dSKent Overstreet 7461c6fdbd8SKent Overstreet __module_get(THIS_MODULE); 7471c6fdbd8SKent Overstreet 748505b7a4cSKent Overstreet closure_init(&c->cl, NULL); 749505b7a4cSKent Overstreet 750505b7a4cSKent Overstreet c->kobj.kset = bcachefs_kset; 751505b7a4cSKent Overstreet kobject_init(&c->kobj, &bch2_fs_ktype); 752505b7a4cSKent Overstreet kobject_init(&c->internal, &bch2_fs_internal_ktype); 753505b7a4cSKent Overstreet kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype); 754505b7a4cSKent Overstreet kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype); 755104c6974SDaniel Hill kobject_init(&c->counters_kobj, &bch2_fs_counters_ktype); 756505b7a4cSKent Overstreet 7571c6fdbd8SKent Overstreet c->minor = -1; 7581c6fdbd8SKent Overstreet c->disk_sb.fs_sb = true; 7591c6fdbd8SKent Overstreet 7601ada1606SKent Overstreet init_rwsem(&c->state_lock); 7611c6fdbd8SKent Overstreet mutex_init(&c->sb_lock); 7621c6fdbd8SKent Overstreet mutex_init(&c->replicas_gc_lock); 7631c6fdbd8SKent Overstreet mutex_init(&c->btree_root_lock); 7641c6fdbd8SKent Overstreet INIT_WORK(&c->read_only_work, bch2_fs_read_only_work); 7651c6fdbd8SKent Overstreet 76663508b75SKent Overstreet refcount_set(&c->ro_ref, 1); 76763508b75SKent Overstreet init_waitqueue_head(&c->ro_ref_wait); 768267b801fSKent Overstreet sema_init(&c->online_fsck_mutex, 1); 76963508b75SKent Overstreet 7701c6fdbd8SKent Overstreet init_rwsem(&c->gc_lock); 771c45c8667SKent Overstreet mutex_init(&c->gc_gens_lock); 7728a443d3eSKent Overstreet atomic_set(&c->journal_keys.ref, 1); 7738a443d3eSKent Overstreet c->journal_keys.initial_ref_held = true; 7741c6fdbd8SKent Overstreet 7751c6fdbd8SKent Overstreet for (i = 0; i < BCH_TIME_STAT_NR; i++) 7761c6fdbd8SKent Overstreet bch2_time_stats_init(&c->times[i]); 7771c6fdbd8SKent Overstreet 77810330402SKent Overstreet bch2_fs_gc_init(c); 779e6d11615SKent Overstreet bch2_fs_copygc_init(c); 7802ca88e5aSKent Overstreet bch2_fs_btree_key_cache_init_early(&c->btree_key_cache); 78150a8a732SThomas Bertschinger bch2_fs_btree_iter_init_early(c); 78265db6049SKent Overstreet bch2_fs_btree_interior_update_init_early(c); 783b092daddSKent Overstreet bch2_fs_allocator_background_init(c); 784b092daddSKent Overstreet bch2_fs_allocator_foreground_init(c); 7851c6fdbd8SKent Overstreet bch2_fs_rebalance_init(c); 7861c6fdbd8SKent Overstreet bch2_fs_quota_init(c); 78784c72755SKent Overstreet bch2_fs_ec_init_early(c); 788b9fa375bSKent Overstreet bch2_fs_move_init(c); 789f5d26fa3SKent Overstreet bch2_fs_sb_errors_init_early(c); 7901c6fdbd8SKent Overstreet 7911c6fdbd8SKent Overstreet INIT_LIST_HEAD(&c->list); 7921c6fdbd8SKent Overstreet 7931c6fdbd8SKent Overstreet mutex_init(&c->bio_bounce_pages_lock); 79414b393eeSKent Overstreet mutex_init(&c->snapshot_table_lock); 79537fad949SKent Overstreet init_rwsem(&c->snapshot_create_lock); 7961c6fdbd8SKent Overstreet 7971c6fdbd8SKent Overstreet spin_lock_init(&c->btree_write_error_lock); 7981c6fdbd8SKent Overstreet 7995b593ee1SKent Overstreet INIT_LIST_HEAD(&c->journal_iters); 800f1d786a0SKent Overstreet 801f5d26fa3SKent Overstreet INIT_LIST_HEAD(&c->fsck_error_msgs); 802f5d26fa3SKent Overstreet mutex_init(&c->fsck_error_msgs_lock); 8031c6fdbd8SKent Overstreet 8045e82a9a1SKent Overstreet seqcount_init(&c->usage_lock); 8055e82a9a1SKent Overstreet 806ef1b2092SKent Overstreet sema_init(&c->io_in_flight, 128); 807ef1b2092SKent Overstreet 8089edbcc72SKent Overstreet INIT_LIST_HEAD(&c->vfs_inodes_list); 8099edbcc72SKent Overstreet mutex_init(&c->vfs_inodes_lock); 8109edbcc72SKent Overstreet 8111c6fdbd8SKent Overstreet c->copy_gc_enabled = 1; 8121c6fdbd8SKent Overstreet c->rebalance.enabled = 1; 8131c6fdbd8SKent Overstreet 814991ba021SKent Overstreet c->journal.flush_write_time = &c->times[BCH_TIME_journal_flush_write]; 815991ba021SKent Overstreet c->journal.noflush_write_time = &c->times[BCH_TIME_journal_noflush_write]; 8161c6fdbd8SKent Overstreet c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq]; 8171c6fdbd8SKent Overstreet 8181c6fdbd8SKent Overstreet bch2_fs_btree_cache_init_early(&c->btree_cache); 8191c6fdbd8SKent Overstreet 820fca1223cSKent Overstreet mutex_init(&c->sectors_available_lock); 821fca1223cSKent Overstreet 822e2b60560SKent Overstreet ret = percpu_init_rwsem(&c->mark_lock); 823e2b60560SKent Overstreet if (ret) 82473e6ab95SKent Overstreet goto err; 82573e6ab95SKent Overstreet 8261c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 827e2b60560SKent Overstreet ret = bch2_sb_to_fs(c, sb); 8281c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 829e2b60560SKent Overstreet 830e2b60560SKent Overstreet if (ret) 8311c6fdbd8SKent Overstreet goto err; 8321c6fdbd8SKent Overstreet 833401ec4dbSKent Overstreet pr_uuid(&name, c->sb.user_uuid.b); 83465d48e35SKent Overstreet ret = name.allocation_failure ? -BCH_ERR_ENOMEM_fs_name_alloc : 0; 835401ec4dbSKent Overstreet if (ret) 836401ec4dbSKent Overstreet goto err; 8371c6fdbd8SKent Overstreet 838f8cdf65bSLi Zetao strscpy(c->name, name.buf, sizeof(c->name)); 839f8cdf65bSLi Zetao printbuf_exit(&name); 840f8cdf65bSLi Zetao 8412430e72fSKent Overstreet /* Compat: */ 84273bd774dSKent Overstreet if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && 8432430e72fSKent Overstreet !BCH_SB_JOURNAL_FLUSH_DELAY(sb)) 8442430e72fSKent Overstreet SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000); 8452430e72fSKent Overstreet 84673bd774dSKent Overstreet if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && 8472430e72fSKent Overstreet !BCH_SB_JOURNAL_RECLAIM_DELAY(sb)) 8482430e72fSKent Overstreet SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 100); 8492430e72fSKent Overstreet 8501c6fdbd8SKent Overstreet c->opts = bch2_opts_default; 8518244f320SKent Overstreet ret = bch2_opts_from_sb(&c->opts, sb); 8528244f320SKent Overstreet if (ret) 8538244f320SKent Overstreet goto err; 8548244f320SKent Overstreet 8551c6fdbd8SKent Overstreet bch2_opts_apply(&c->opts, opts); 8561c6fdbd8SKent Overstreet 8577c8f6f98SKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_alloc; 8587c8f6f98SKent Overstreet if (c->opts.inodes_use_key_cache) 8597c8f6f98SKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_inodes; 860aaad530aSKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_logged_ops; 8617c8f6f98SKent Overstreet 8628244f320SKent Overstreet c->block_bits = ilog2(block_sectors(c)); 8631c6fdbd8SKent Overstreet c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c); 8641c6fdbd8SKent Overstreet 8657be9ab63SChris Webb if (bch2_fs_init_fault("fs_alloc")) { 866e2b60560SKent Overstreet bch_err(c, "fs_alloc fault injected"); 867e2b60560SKent Overstreet ret = -EFAULT; 8681c6fdbd8SKent Overstreet goto err; 8697be9ab63SChris Webb } 8701c6fdbd8SKent Overstreet 871ae2f17d5SKent Overstreet iter_size = sizeof(struct sort_iter) + 8721c6fdbd8SKent Overstreet (btree_blocks(c) + 1) * 2 * 873ae2f17d5SKent Overstreet sizeof(struct sort_iter_set); 8741c6fdbd8SKent Overstreet 875b5e8a699SKent Overstreet c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus())); 876b5e8a699SKent Overstreet 877731bdd2eSKent Overstreet if (!(c->btree_update_wq = alloc_workqueue("bcachefs", 8786b83aee8SKent Overstreet WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_UNBOUND, 512)) || 8799f1833caSKent Overstreet !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io", 8806b83aee8SKent Overstreet WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) || 8812f33ece9SKent Overstreet !(c->copygc_wq = alloc_workqueue("bcachefs_copygc", 8826b83aee8SKent Overstreet WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) || 883161f73c2SKent Overstreet !(c->btree_read_complete_wq = alloc_workqueue("bcachefs_btree_read_complete", 8846b83aee8SKent Overstreet WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM, 512)) || 885161f73c2SKent Overstreet !(c->btree_write_submit_wq = alloc_workqueue("bcachefs_btree_write_sumit", 886161f73c2SKent Overstreet WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) || 8878bff9875SBrian Foster !(c->write_ref_wq = alloc_workqueue("bcachefs_write_ref", 8888bff9875SBrian Foster WQ_FREEZABLE, 0)) || 889d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 890134915f3SKent Overstreet percpu_ref_init(&c->writes, bch2_writes_disabled, 891134915f3SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 892d94189adSKent Overstreet #endif 8931c6fdbd8SKent Overstreet mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) || 8941c6fdbd8SKent Overstreet bioset_init(&c->btree_bio, 1, 8951c6fdbd8SKent Overstreet max(offsetof(struct btree_read_bio, bio), 8961c6fdbd8SKent Overstreet offsetof(struct btree_write_bio, wbio.bio)), 8971c6fdbd8SKent Overstreet BIOSET_NEED_BVECS) || 8985663a415SKent Overstreet !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) || 8998bb8d683SKent Overstreet !(c->usage = alloc_percpu(struct bch_fs_usage_base)) || 9005e82a9a1SKent Overstreet !(c->online_reserved = alloc_percpu(u64)) || 901cb6fc943SKent Overstreet mempool_init_kvmalloc_pool(&c->btree_bounce_pool, 1, 902ec4edd7bSKent Overstreet c->opts.btree_node_size) || 90335189e09SKent Overstreet mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) || 904b5e8a699SKent Overstreet !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits, 905e2b60560SKent Overstreet sizeof(u64), GFP_KERNEL))) { 90665d48e35SKent Overstreet ret = -BCH_ERR_ENOMEM_fs_other_alloc; 9077be9ab63SChris Webb goto err; 9087be9ab63SChris Webb } 9097be9ab63SChris Webb 910f3b8403eSKent Overstreet ret = bch2_fs_counters_init(c) ?: 911f5d26fa3SKent Overstreet bch2_fs_sb_errors_init(c) ?: 912f3b8403eSKent Overstreet bch2_io_clock_init(&c->io_clock[READ]) ?: 913e2b60560SKent Overstreet bch2_io_clock_init(&c->io_clock[WRITE]) ?: 914e2b60560SKent Overstreet bch2_fs_journal_init(&c->journal) ?: 915f770a6e9SKent Overstreet bch2_fs_btree_iter_init(c) ?: 916e2b60560SKent Overstreet bch2_fs_btree_cache_init(c) ?: 917e2b60560SKent Overstreet bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?: 918e2b60560SKent Overstreet bch2_fs_btree_interior_update_init(c) ?: 9199b688da3SKent Overstreet bch2_fs_buckets_waiting_for_journal_init(c) ?: 920920e69bcSKent Overstreet bch2_fs_btree_write_buffer_init(c) ?: 921e2b60560SKent Overstreet bch2_fs_subvolumes_init(c) ?: 9221809b8cbSKent Overstreet bch2_fs_io_read_init(c) ?: 9231809b8cbSKent Overstreet bch2_fs_io_write_init(c) ?: 924350175bfSKent Overstreet bch2_fs_nocow_locking_init(c) ?: 925e2b60560SKent Overstreet bch2_fs_encryption_init(c) ?: 926e2b60560SKent Overstreet bch2_fs_compress_init(c) ?: 927e2b60560SKent Overstreet bch2_fs_ec_init(c) ?: 928112d21fdSKent Overstreet bch2_fs_vfs_init(c) ?: 929dbbfca9fSKent Overstreet bch2_fs_fsio_init(c) ?: 930867c1fe0SDan Carpenter bch2_fs_fs_io_buffered_init(c) ?: 931dbbfca9fSKent Overstreet bch2_fs_fs_io_direct_init(c); 9327be9ab63SChris Webb if (ret) 9331c6fdbd8SKent Overstreet goto err; 9341c6fdbd8SKent Overstreet 93550479406SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) { 93650479406SKent Overstreet if (!bch2_member_exists(c->disk_sb.sb, i)) 93750479406SKent Overstreet continue; 93850479406SKent Overstreet ret = bch2_dev_alloc(c, i); 93950479406SKent Overstreet if (ret) 9401c6fdbd8SKent Overstreet goto err; 9417be9ab63SChris Webb } 9421c6fdbd8SKent Overstreet 9434b8f89afSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 9444b8f89afSKent Overstreet &c->btree_root_journal_res, 9454b8f89afSKent Overstreet BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX)); 9464b8f89afSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 9474b8f89afSKent Overstreet &c->clock_journal_res, 9484b8f89afSKent Overstreet (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2); 9494b8f89afSKent Overstreet 9501c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 951e2b60560SKent Overstreet ret = bch2_fs_online(c); 9521c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 953e2b60560SKent Overstreet 954e2b60560SKent Overstreet if (ret) 9551c6fdbd8SKent Overstreet goto err; 9561c6fdbd8SKent Overstreet out: 9571c6fdbd8SKent Overstreet return c; 9581c6fdbd8SKent Overstreet err: 9591c6fdbd8SKent Overstreet bch2_fs_free(c); 9607be9ab63SChris Webb c = ERR_PTR(ret); 9611c6fdbd8SKent Overstreet goto out; 9621c6fdbd8SKent Overstreet } 9631c6fdbd8SKent Overstreet 964619f5beeSKent Overstreet noinline_for_stack 965619f5beeSKent Overstreet static void print_mount_opts(struct bch_fs *c) 966619f5beeSKent Overstreet { 967619f5beeSKent Overstreet enum bch_opt_id i; 968fa8e94faSKent Overstreet struct printbuf p = PRINTBUF; 969619f5beeSKent Overstreet bool first = true; 970619f5beeSKent Overstreet 971ec8bf491SKent Overstreet prt_str(&p, "starting version "); 972e3804b55SKent Overstreet bch2_version_to_text(&p, c->sb.version); 97360573ff5SKent Overstreet 974619f5beeSKent Overstreet if (c->opts.read_only) { 97560573ff5SKent Overstreet prt_str(&p, " opts="); 976619f5beeSKent Overstreet first = false; 97760573ff5SKent Overstreet prt_printf(&p, "ro"); 978619f5beeSKent Overstreet } 979619f5beeSKent Overstreet 980619f5beeSKent Overstreet for (i = 0; i < bch2_opts_nr; i++) { 981619f5beeSKent Overstreet const struct bch_option *opt = &bch2_opt_table[i]; 982619f5beeSKent Overstreet u64 v = bch2_opt_get_by_id(&c->opts, i); 983619f5beeSKent Overstreet 9848244f320SKent Overstreet if (!(opt->flags & OPT_MOUNT)) 985619f5beeSKent Overstreet continue; 986619f5beeSKent Overstreet 987619f5beeSKent Overstreet if (v == bch2_opt_get_by_id(&bch2_opts_default, i)) 988619f5beeSKent Overstreet continue; 989619f5beeSKent Overstreet 99060573ff5SKent Overstreet prt_str(&p, first ? " opts=" : ","); 991619f5beeSKent Overstreet first = false; 9925521b1dfSKent Overstreet bch2_opt_to_text(&p, c, c->disk_sb.sb, opt, v, OPT_SHOW_MOUNT_STYLE); 993619f5beeSKent Overstreet } 994619f5beeSKent Overstreet 99560573ff5SKent Overstreet bch_info(c, "%s", p.buf); 996fa8e94faSKent Overstreet printbuf_exit(&p); 997619f5beeSKent Overstreet } 998619f5beeSKent Overstreet 999619f5beeSKent Overstreet int bch2_fs_start(struct bch_fs *c) 10001c6fdbd8SKent Overstreet { 1001a420eea6STim Schlueter time64_t now = ktime_get_real_seconds(); 100278c0b75cSKent Overstreet int ret; 10031c6fdbd8SKent Overstreet 1004ef1634f0SKent Overstreet print_mount_opts(c); 1005ef1634f0SKent Overstreet 10061ada1606SKent Overstreet down_write(&c->state_lock); 10071c6fdbd8SKent Overstreet 10083c471b65SKent Overstreet BUG_ON(test_bit(BCH_FS_started, &c->flags)); 10091c6fdbd8SKent Overstreet 10101c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 10111c6fdbd8SKent Overstreet 1012f5d26fa3SKent Overstreet ret = bch2_sb_members_v2_init(c); 10133f7b9713SHunter Shaffer if (ret) { 10143f7b9713SHunter Shaffer mutex_unlock(&c->sb_lock); 10153f7b9713SHunter Shaffer goto err; 10163f7b9713SHunter Shaffer } 10173f7b9713SHunter Shaffer 10189fea2274SKent Overstreet for_each_online_member(c, ca) 10199fea2274SKent Overstreet bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = cpu_to_le64(now); 10201c6fdbd8SKent Overstreet 10210a34c058SKent Overstreet struct bch_sb_field_ext *ext = 10220a34c058SKent Overstreet bch2_sb_field_get_minsize(&c->disk_sb, ext, sizeof(*ext) / sizeof(u64)); 10231c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 10241c6fdbd8SKent Overstreet 10250a34c058SKent Overstreet if (!ext) { 10260a34c058SKent Overstreet bch_err(c, "insufficient space in superblock for sb_field_ext"); 10270a34c058SKent Overstreet ret = -BCH_ERR_ENOSPC_sb; 10280a34c058SKent Overstreet goto err; 10290a34c058SKent Overstreet } 10300a34c058SKent Overstreet 10319fea2274SKent Overstreet for_each_rw_member(c, ca) 10321c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 10331c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 10341c6fdbd8SKent Overstreet 10351c6fdbd8SKent Overstreet ret = BCH_SB_INITIALIZED(c->disk_sb.sb) 10361c6fdbd8SKent Overstreet ? bch2_fs_recovery(c) 10371c6fdbd8SKent Overstreet : bch2_fs_initialize(c); 10381c6fdbd8SKent Overstreet if (ret) 10391c6fdbd8SKent Overstreet goto err; 10401c6fdbd8SKent Overstreet 1041cd575ddfSKent Overstreet ret = bch2_opts_check_may_set(c); 1042cd575ddfSKent Overstreet if (ret) 1043cd575ddfSKent Overstreet goto err; 1044cd575ddfSKent Overstreet 1045e2b60560SKent Overstreet if (bch2_fs_init_fault("fs_start")) { 1046e2b60560SKent Overstreet bch_err(c, "fs_start fault injected"); 104778c0b75cSKent Overstreet ret = -EINVAL; 10481c6fdbd8SKent Overstreet goto err; 1049e2b60560SKent Overstreet } 10501c6fdbd8SKent Overstreet 10513c471b65SKent Overstreet set_bit(BCH_FS_started, &c->flags); 1052a9310ab0SKent Overstreet 105362719cf3SKent Overstreet if (c->opts.read_only) { 10541c6fdbd8SKent Overstreet bch2_fs_read_only(c); 10551c6fdbd8SKent Overstreet } else { 10563c471b65SKent Overstreet ret = !test_bit(BCH_FS_rw, &c->flags) 1057619f5beeSKent Overstreet ? bch2_fs_read_write(c) 1058619f5beeSKent Overstreet : bch2_fs_read_write_late(c); 1059619f5beeSKent Overstreet if (ret) 10601c6fdbd8SKent Overstreet goto err; 10611c6fdbd8SKent Overstreet } 10621c6fdbd8SKent Overstreet 1063619f5beeSKent Overstreet ret = 0; 1064e34ec13aSKent Overstreet err: 1065e34ec13aSKent Overstreet if (ret) 1066e34ec13aSKent Overstreet bch_err_msg(c, ret, "starting filesystem"); 1067e34ec13aSKent Overstreet else 1068e34ec13aSKent Overstreet bch_verbose(c, "done starting filesystem"); 10691ada1606SKent Overstreet up_write(&c->state_lock); 1070619f5beeSKent Overstreet return ret; 10711c6fdbd8SKent Overstreet } 10721c6fdbd8SKent Overstreet 107378c0b75cSKent Overstreet static int bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c) 10741c6fdbd8SKent Overstreet { 10751241df58SHunter Shaffer struct bch_member m = bch2_sb_member_get(sb, sb->dev_idx); 10761c6fdbd8SKent Overstreet 10778244f320SKent Overstreet if (le16_to_cpu(sb->block_size) != block_sectors(c)) 107878c0b75cSKent Overstreet return -BCH_ERR_mismatched_block_size; 10791c6fdbd8SKent Overstreet 10801241df58SHunter Shaffer if (le16_to_cpu(m.bucket_size) < 10811c6fdbd8SKent Overstreet BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb)) 108278c0b75cSKent Overstreet return -BCH_ERR_bucket_size_too_small; 10831c6fdbd8SKent Overstreet 108478c0b75cSKent Overstreet return 0; 10851c6fdbd8SKent Overstreet } 10861c6fdbd8SKent Overstreet 10870d529663SKent Overstreet static int bch2_dev_in_fs(struct bch_sb_handle *fs, 10882f300f09SKent Overstreet struct bch_sb_handle *sb, 10892f300f09SKent Overstreet struct bch_opts *opts) 10901c6fdbd8SKent Overstreet { 10910d529663SKent Overstreet if (fs == sb) 10920d529663SKent Overstreet return 0; 10931c6fdbd8SKent Overstreet 10940d529663SKent Overstreet if (!uuid_equal(&fs->sb->uuid, &sb->sb->uuid)) 109578c0b75cSKent Overstreet return -BCH_ERR_device_not_a_member_of_filesystem; 10961c6fdbd8SKent Overstreet 10972f724563SKent Overstreet if (!bch2_member_exists(fs->sb, sb->sb->dev_idx)) 109878c0b75cSKent Overstreet return -BCH_ERR_device_has_been_removed; 10991c6fdbd8SKent Overstreet 11000d529663SKent Overstreet if (fs->sb->block_size != sb->sb->block_size) 110178c0b75cSKent Overstreet return -BCH_ERR_mismatched_block_size; 11021c6fdbd8SKent Overstreet 11030d529663SKent Overstreet if (le16_to_cpu(fs->sb->version) < bcachefs_metadata_version_member_seq || 11040d529663SKent Overstreet le16_to_cpu(sb->sb->version) < bcachefs_metadata_version_member_seq) 11050d529663SKent Overstreet return 0; 11060d529663SKent Overstreet 11070d529663SKent Overstreet if (fs->sb->seq == sb->sb->seq && 11080d529663SKent Overstreet fs->sb->write_time != sb->sb->write_time) { 11090d529663SKent Overstreet struct printbuf buf = PRINTBUF; 11100d529663SKent Overstreet 11111f5af5fcSKent Overstreet prt_str(&buf, "Split brain detected between "); 11121f5af5fcSKent Overstreet prt_bdevname(&buf, sb->bdev); 11131f5af5fcSKent Overstreet prt_str(&buf, " and "); 11141f5af5fcSKent Overstreet prt_bdevname(&buf, fs->bdev); 11151f5af5fcSKent Overstreet prt_char(&buf, ':'); 11160d529663SKent Overstreet prt_newline(&buf); 11170d529663SKent Overstreet prt_printf(&buf, "seq=%llu but write_time different, got", le64_to_cpu(sb->sb->seq)); 11180d529663SKent Overstreet prt_newline(&buf); 11190d529663SKent Overstreet 11201f5af5fcSKent Overstreet prt_bdevname(&buf, fs->bdev); 11211f5af5fcSKent Overstreet prt_char(&buf, ' '); 11220d529663SKent Overstreet bch2_prt_datetime(&buf, le64_to_cpu(fs->sb->write_time));; 11230d529663SKent Overstreet prt_newline(&buf); 11240d529663SKent Overstreet 11251f5af5fcSKent Overstreet prt_bdevname(&buf, sb->bdev); 11261f5af5fcSKent Overstreet prt_char(&buf, ' '); 11270d529663SKent Overstreet bch2_prt_datetime(&buf, le64_to_cpu(sb->sb->write_time));; 11280d529663SKent Overstreet prt_newline(&buf); 11290d529663SKent Overstreet 11302f300f09SKent Overstreet if (!opts->no_splitbrain_check) 11310d529663SKent Overstreet prt_printf(&buf, "Not using older sb"); 11320d529663SKent Overstreet 11330d529663SKent Overstreet pr_err("%s", buf.buf); 11340d529663SKent Overstreet printbuf_exit(&buf); 11352f300f09SKent Overstreet 11362f300f09SKent Overstreet if (!opts->no_splitbrain_check) 11370d529663SKent Overstreet return -BCH_ERR_device_splitbrain; 11380d529663SKent Overstreet } 11390d529663SKent Overstreet 11400d529663SKent Overstreet struct bch_member m = bch2_sb_member_get(fs->sb, sb->sb->dev_idx); 11410d529663SKent Overstreet u64 seq_from_fs = le64_to_cpu(m.seq); 11420d529663SKent Overstreet u64 seq_from_member = le64_to_cpu(sb->sb->seq); 11430d529663SKent Overstreet 11440d529663SKent Overstreet if (seq_from_fs && seq_from_fs < seq_from_member) { 11451f5af5fcSKent Overstreet struct printbuf buf = PRINTBUF; 11461f5af5fcSKent Overstreet 11471f5af5fcSKent Overstreet prt_str(&buf, "Split brain detected between "); 11481f5af5fcSKent Overstreet prt_bdevname(&buf, sb->bdev); 11491f5af5fcSKent Overstreet prt_str(&buf, " and "); 11501f5af5fcSKent Overstreet prt_bdevname(&buf, fs->bdev); 11511f5af5fcSKent Overstreet prt_char(&buf, ':'); 11521f5af5fcSKent Overstreet prt_newline(&buf); 11531f5af5fcSKent Overstreet 11541f5af5fcSKent Overstreet prt_bdevname(&buf, fs->bdev); 11551f5af5fcSKent Overstreet prt_str(&buf, " believes seq of "); 11561f5af5fcSKent Overstreet prt_bdevname(&buf, sb->bdev); 11571f5af5fcSKent Overstreet prt_printf(&buf, " to be %llu, but ", seq_from_fs); 11581f5af5fcSKent Overstreet prt_bdevname(&buf, sb->bdev); 11591f5af5fcSKent Overstreet prt_printf(&buf, " has %llu\n", seq_from_member); 11602f300f09SKent Overstreet 11612f300f09SKent Overstreet if (!opts->no_splitbrain_check) { 11621f5af5fcSKent Overstreet prt_str(&buf, "Not using "); 11631f5af5fcSKent Overstreet prt_bdevname(&buf, sb->bdev); 11642f300f09SKent Overstreet } 11651f5af5fcSKent Overstreet 11661f5af5fcSKent Overstreet pr_err("%s", buf.buf); 11671f5af5fcSKent Overstreet printbuf_exit(&buf); 11682f300f09SKent Overstreet 11692f300f09SKent Overstreet if (!opts->no_splitbrain_check) 11700d529663SKent Overstreet return -BCH_ERR_device_splitbrain; 11710d529663SKent Overstreet } 11720d529663SKent Overstreet 117378c0b75cSKent Overstreet return 0; 11741c6fdbd8SKent Overstreet } 11751c6fdbd8SKent Overstreet 11761c6fdbd8SKent Overstreet /* Device startup/shutdown: */ 11771c6fdbd8SKent Overstreet 11781c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *kobj) 11791c6fdbd8SKent Overstreet { 11801c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); 11811c6fdbd8SKent Overstreet 11821c6fdbd8SKent Overstreet kfree(ca); 11831c6fdbd8SKent Overstreet } 11841c6fdbd8SKent Overstreet 11851c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *ca) 11861c6fdbd8SKent Overstreet { 11871c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 11881c6fdbd8SKent Overstreet 1189*5e3b7232SKent Overstreet bch2_dev_unlink(ca); 11901c6fdbd8SKent Overstreet 11911c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) 11921c6fdbd8SKent Overstreet kobject_del(&ca->kobj); 11931c6fdbd8SKent Overstreet 11941c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 119564ee1431SKent Overstreet bch2_dev_allocator_background_exit(ca); 11961c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 11971c6fdbd8SKent Overstreet 11981c6fdbd8SKent Overstreet free_percpu(ca->io_done); 11991c6fdbd8SKent Overstreet bch2_dev_buckets_free(ca); 1200d1170ce5SKent Overstreet free_page((unsigned long) ca->sb_read_scratch); 12011c6fdbd8SKent Overstreet 1202273960b8SDarrick J. Wong bch2_time_stats_quantiles_exit(&ca->io_latency[WRITE]); 1203273960b8SDarrick J. Wong bch2_time_stats_quantiles_exit(&ca->io_latency[READ]); 12041c6fdbd8SKent Overstreet 12051c6fdbd8SKent Overstreet percpu_ref_exit(&ca->io_ref); 1206552aa548SKent Overstreet #ifndef CONFIG_BCACHEFS_DEBUG 12071c6fdbd8SKent Overstreet percpu_ref_exit(&ca->ref); 1208552aa548SKent Overstreet #endif 12091c6fdbd8SKent Overstreet kobject_put(&ca->kobj); 12101c6fdbd8SKent Overstreet } 12111c6fdbd8SKent Overstreet 12121c6fdbd8SKent Overstreet static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) 12131c6fdbd8SKent Overstreet { 12141c6fdbd8SKent Overstreet 12151c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 12161c6fdbd8SKent Overstreet 12171c6fdbd8SKent Overstreet if (percpu_ref_is_zero(&ca->io_ref)) 12181c6fdbd8SKent Overstreet return; 12191c6fdbd8SKent Overstreet 12201c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 12211c6fdbd8SKent Overstreet 12221c6fdbd8SKent Overstreet reinit_completion(&ca->io_ref_completion); 12231c6fdbd8SKent Overstreet percpu_ref_kill(&ca->io_ref); 12241c6fdbd8SKent Overstreet wait_for_completion(&ca->io_ref_completion); 12251c6fdbd8SKent Overstreet 1226*5e3b7232SKent Overstreet bch2_dev_unlink(ca); 12271c6fdbd8SKent Overstreet 12281c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 12291c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 12301c6fdbd8SKent Overstreet } 12311c6fdbd8SKent Overstreet 1232552aa548SKent Overstreet #ifndef CONFIG_BCACHEFS_DEBUG 12331c6fdbd8SKent Overstreet static void bch2_dev_ref_complete(struct percpu_ref *ref) 12341c6fdbd8SKent Overstreet { 12351c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, ref); 12361c6fdbd8SKent Overstreet 12371c6fdbd8SKent Overstreet complete(&ca->ref_completion); 12381c6fdbd8SKent Overstreet } 1239552aa548SKent Overstreet #endif 12401c6fdbd8SKent Overstreet 12411c6fdbd8SKent Overstreet static void bch2_dev_io_ref_complete(struct percpu_ref *ref) 12421c6fdbd8SKent Overstreet { 12431c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref); 12441c6fdbd8SKent Overstreet 12451c6fdbd8SKent Overstreet complete(&ca->io_ref_completion); 12461c6fdbd8SKent Overstreet } 12471c6fdbd8SKent Overstreet 1248*5e3b7232SKent Overstreet static void bch2_dev_unlink(struct bch_dev *ca) 1249*5e3b7232SKent Overstreet { 1250*5e3b7232SKent Overstreet struct kobject *b; 1251*5e3b7232SKent Overstreet 1252*5e3b7232SKent Overstreet /* 1253*5e3b7232SKent Overstreet * This is racy w.r.t. the underlying block device being hot-removed, 1254*5e3b7232SKent Overstreet * which removes it from sysfs. 1255*5e3b7232SKent Overstreet * 1256*5e3b7232SKent Overstreet * It'd be lovely if we had a way to handle this race, but the sysfs 1257*5e3b7232SKent Overstreet * code doesn't appear to provide a good method and block/holder.c is 1258*5e3b7232SKent Overstreet * susceptible as well: 1259*5e3b7232SKent Overstreet */ 1260*5e3b7232SKent Overstreet if (ca->kobj.state_in_sysfs && 1261*5e3b7232SKent Overstreet ca->disk_sb.bdev && 1262*5e3b7232SKent Overstreet (b = bdev_kobj(ca->disk_sb.bdev))->state_in_sysfs) { 1263*5e3b7232SKent Overstreet sysfs_remove_link(b, "bcachefs"); 1264*5e3b7232SKent Overstreet sysfs_remove_link(&ca->kobj, "block"); 1265*5e3b7232SKent Overstreet } 1266*5e3b7232SKent Overstreet } 1267*5e3b7232SKent Overstreet 12681c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) 12691c6fdbd8SKent Overstreet { 12701c6fdbd8SKent Overstreet int ret; 12711c6fdbd8SKent Overstreet 12721c6fdbd8SKent Overstreet if (!c->kobj.state_in_sysfs) 12731c6fdbd8SKent Overstreet return 0; 12741c6fdbd8SKent Overstreet 12751c6fdbd8SKent Overstreet if (!ca->kobj.state_in_sysfs) { 12761c6fdbd8SKent Overstreet ret = kobject_add(&ca->kobj, &c->kobj, 12771c6fdbd8SKent Overstreet "dev-%u", ca->dev_idx); 12781c6fdbd8SKent Overstreet if (ret) 12791c6fdbd8SKent Overstreet return ret; 12801c6fdbd8SKent Overstreet } 12811c6fdbd8SKent Overstreet 12821c6fdbd8SKent Overstreet if (ca->disk_sb.bdev) { 12831c6fdbd8SKent Overstreet struct kobject *block = bdev_kobj(ca->disk_sb.bdev); 12841c6fdbd8SKent Overstreet 12851c6fdbd8SKent Overstreet ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); 12861c6fdbd8SKent Overstreet if (ret) 12871c6fdbd8SKent Overstreet return ret; 12881c6fdbd8SKent Overstreet 12891c6fdbd8SKent Overstreet ret = sysfs_create_link(&ca->kobj, block, "block"); 12901c6fdbd8SKent Overstreet if (ret) 12911c6fdbd8SKent Overstreet return ret; 12921c6fdbd8SKent Overstreet } 12931c6fdbd8SKent Overstreet 12941c6fdbd8SKent Overstreet return 0; 12951c6fdbd8SKent Overstreet } 12961c6fdbd8SKent Overstreet 12971c6fdbd8SKent Overstreet static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c, 12981c6fdbd8SKent Overstreet struct bch_member *member) 12991c6fdbd8SKent Overstreet { 13001c6fdbd8SKent Overstreet struct bch_dev *ca; 130194119eebSKent Overstreet unsigned i; 13021c6fdbd8SKent Overstreet 13031c6fdbd8SKent Overstreet ca = kzalloc(sizeof(*ca), GFP_KERNEL); 13041c6fdbd8SKent Overstreet if (!ca) 13051c6fdbd8SKent Overstreet return NULL; 13061c6fdbd8SKent Overstreet 13071c6fdbd8SKent Overstreet kobject_init(&ca->kobj, &bch2_dev_ktype); 13081c6fdbd8SKent Overstreet init_completion(&ca->ref_completion); 13091c6fdbd8SKent Overstreet init_completion(&ca->io_ref_completion); 13101c6fdbd8SKent Overstreet 13111c6fdbd8SKent Overstreet init_rwsem(&ca->bucket_lock); 13121c6fdbd8SKent Overstreet 13131c6fdbd8SKent Overstreet INIT_WORK(&ca->io_error_work, bch2_io_error_work); 13141c6fdbd8SKent Overstreet 1315273960b8SDarrick J. Wong bch2_time_stats_quantiles_init(&ca->io_latency[READ]); 1316273960b8SDarrick J. Wong bch2_time_stats_quantiles_init(&ca->io_latency[WRITE]); 13171c6fdbd8SKent Overstreet 13181c6fdbd8SKent Overstreet ca->mi = bch2_mi_to_cpu(member); 131994119eebSKent Overstreet 132094119eebSKent Overstreet for (i = 0; i < ARRAY_SIZE(member->errors); i++) 132194119eebSKent Overstreet atomic64_set(&ca->errors[i], le64_to_cpu(member->errors[i])); 132294119eebSKent Overstreet 13231c6fdbd8SKent Overstreet ca->uuid = member->uuid; 13241c6fdbd8SKent Overstreet 1325f25d8215SKent Overstreet ca->nr_btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE, 1326f25d8215SKent Overstreet ca->mi.bucket_size / btree_sectors(c)); 1327f25d8215SKent Overstreet 1328552aa548SKent Overstreet #ifndef CONFIG_BCACHEFS_DEBUG 1329552aa548SKent Overstreet if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, 0, GFP_KERNEL)) 1330552aa548SKent Overstreet goto err; 1331552aa548SKent Overstreet #else 1332552aa548SKent Overstreet atomic_long_set(&ca->ref, 1); 1333552aa548SKent Overstreet #endif 1334552aa548SKent Overstreet 133564ee1431SKent Overstreet bch2_dev_allocator_background_init(ca); 133664ee1431SKent Overstreet 1337552aa548SKent Overstreet if (percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete, 13381c6fdbd8SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 1339d1170ce5SKent Overstreet !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) || 13401c6fdbd8SKent Overstreet bch2_dev_buckets_alloc(c, ca) || 13411c6fdbd8SKent Overstreet !(ca->io_done = alloc_percpu(*ca->io_done))) 13421c6fdbd8SKent Overstreet goto err; 13431c6fdbd8SKent Overstreet 13441c6fdbd8SKent Overstreet return ca; 13451c6fdbd8SKent Overstreet err: 13461c6fdbd8SKent Overstreet bch2_dev_free(ca); 13471c6fdbd8SKent Overstreet return NULL; 13481c6fdbd8SKent Overstreet } 13491c6fdbd8SKent Overstreet 13501c6fdbd8SKent Overstreet static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, 13511c6fdbd8SKent Overstreet unsigned dev_idx) 13521c6fdbd8SKent Overstreet { 13531c6fdbd8SKent Overstreet ca->dev_idx = dev_idx; 13541c6fdbd8SKent Overstreet __set_bit(ca->dev_idx, ca->self.d); 13551c6fdbd8SKent Overstreet scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); 13561c6fdbd8SKent Overstreet 13571c6fdbd8SKent Overstreet ca->fs = c; 13581c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], ca); 13591c6fdbd8SKent Overstreet 13601c6fdbd8SKent Overstreet if (bch2_dev_sysfs_online(c, ca)) 13611c6fdbd8SKent Overstreet pr_warn("error creating sysfs objects"); 13621c6fdbd8SKent Overstreet } 13631c6fdbd8SKent Overstreet 13641c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx) 13651c6fdbd8SKent Overstreet { 13661241df58SHunter Shaffer struct bch_member member = bch2_sb_member_get(c->disk_sb.sb, dev_idx); 13671c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 13681c6fdbd8SKent Overstreet int ret = 0; 13691c6fdbd8SKent Overstreet 13701c6fdbd8SKent Overstreet if (bch2_fs_init_fault("dev_alloc")) 13711c6fdbd8SKent Overstreet goto err; 13721c6fdbd8SKent Overstreet 13731241df58SHunter Shaffer ca = __bch2_dev_alloc(c, &member); 13741c6fdbd8SKent Overstreet if (!ca) 13751c6fdbd8SKent Overstreet goto err; 13761c6fdbd8SKent Overstreet 1377220d2062SKent Overstreet ca->fs = c; 1378220d2062SKent Overstreet 13791c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 13801c6fdbd8SKent Overstreet return ret; 13811c6fdbd8SKent Overstreet err: 13821c6fdbd8SKent Overstreet if (ca) 13831c6fdbd8SKent Overstreet bch2_dev_free(ca); 1384c8b4534dSKent Overstreet return -BCH_ERR_ENOMEM_dev_alloc; 13851c6fdbd8SKent Overstreet } 13861c6fdbd8SKent Overstreet 13871c6fdbd8SKent Overstreet static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb) 13881c6fdbd8SKent Overstreet { 13891c6fdbd8SKent Overstreet unsigned ret; 13901c6fdbd8SKent Overstreet 13911c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca)) { 13921c6fdbd8SKent Overstreet bch_err(ca, "already have device online in slot %u", 13931c6fdbd8SKent Overstreet sb->sb->dev_idx); 139478c0b75cSKent Overstreet return -BCH_ERR_device_already_online; 13951c6fdbd8SKent Overstreet } 13961c6fdbd8SKent Overstreet 13971c6fdbd8SKent Overstreet if (get_capacity(sb->bdev->bd_disk) < 13981c6fdbd8SKent Overstreet ca->mi.bucket_size * ca->mi.nbuckets) { 13991c6fdbd8SKent Overstreet bch_err(ca, "cannot online: device too small"); 140078c0b75cSKent Overstreet return -BCH_ERR_device_size_too_small; 14011c6fdbd8SKent Overstreet } 14021c6fdbd8SKent Overstreet 14031c6fdbd8SKent Overstreet BUG_ON(!percpu_ref_is_zero(&ca->io_ref)); 14041c6fdbd8SKent Overstreet 14051c6fdbd8SKent Overstreet ret = bch2_dev_journal_init(ca, sb->sb); 14061c6fdbd8SKent Overstreet if (ret) 14071c6fdbd8SKent Overstreet return ret; 14081c6fdbd8SKent Overstreet 14091c6fdbd8SKent Overstreet /* Commit: */ 14101c6fdbd8SKent Overstreet ca->disk_sb = *sb; 14111c6fdbd8SKent Overstreet memset(sb, 0, sizeof(*sb)); 14121c6fdbd8SKent Overstreet 1413eacb2574SKent Overstreet ca->dev = ca->disk_sb.bdev->bd_dev; 1414eacb2574SKent Overstreet 14151c6fdbd8SKent Overstreet percpu_ref_reinit(&ca->io_ref); 14161c6fdbd8SKent Overstreet 14171c6fdbd8SKent Overstreet return 0; 14181c6fdbd8SKent Overstreet } 14191c6fdbd8SKent Overstreet 14201c6fdbd8SKent Overstreet static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb) 14211c6fdbd8SKent Overstreet { 14221c6fdbd8SKent Overstreet struct bch_dev *ca; 14231c6fdbd8SKent Overstreet int ret; 14241c6fdbd8SKent Overstreet 14251c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 14261c6fdbd8SKent Overstreet 14271c6fdbd8SKent Overstreet if (le64_to_cpu(sb->sb->seq) > 14281c6fdbd8SKent Overstreet le64_to_cpu(c->disk_sb.sb->seq)) 14291c6fdbd8SKent Overstreet bch2_sb_to_fs(c, sb->sb); 14301c6fdbd8SKent Overstreet 14312f724563SKent Overstreet BUG_ON(!bch2_dev_exists(c, sb->sb->dev_idx)); 14321c6fdbd8SKent Overstreet 14332f724563SKent Overstreet ca = bch2_dev_locked(c, sb->sb->dev_idx); 14341c6fdbd8SKent Overstreet 14351c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, sb); 14361c6fdbd8SKent Overstreet if (ret) 14371c6fdbd8SKent Overstreet return ret; 14381c6fdbd8SKent Overstreet 14391c6fdbd8SKent Overstreet bch2_dev_sysfs_online(c, ca); 14401c6fdbd8SKent Overstreet 14411f5af5fcSKent Overstreet struct printbuf name = PRINTBUF; 14421f5af5fcSKent Overstreet prt_bdevname(&name, ca->disk_sb.bdev); 14431f5af5fcSKent Overstreet 14441c6fdbd8SKent Overstreet if (c->sb.nr_devices == 1) 1445e28b0359SKees Cook strscpy(c->name, name.buf, sizeof(c->name)); 1446e28b0359SKees Cook strscpy(ca->name, name.buf, sizeof(ca->name)); 14471f5af5fcSKent Overstreet 14481f5af5fcSKent Overstreet printbuf_exit(&name); 14491c6fdbd8SKent Overstreet 14501c6fdbd8SKent Overstreet rebalance_wakeup(c); 14511c6fdbd8SKent Overstreet return 0; 14521c6fdbd8SKent Overstreet } 14531c6fdbd8SKent Overstreet 14541c6fdbd8SKent Overstreet /* Device management: */ 14551c6fdbd8SKent Overstreet 14561c6fdbd8SKent Overstreet /* 14571c6fdbd8SKent Overstreet * Note: this function is also used by the error paths - when a particular 14581c6fdbd8SKent Overstreet * device sees an error, we call it to determine whether we can just set the 14591c6fdbd8SKent Overstreet * device RO, or - if this function returns false - we'll set the whole 14601c6fdbd8SKent Overstreet * filesystem RO: 14611c6fdbd8SKent Overstreet * 14621c6fdbd8SKent Overstreet * XXX: maybe we should be more explicit about whether we're changing state 14631c6fdbd8SKent Overstreet * because we got an error or what have you? 14641c6fdbd8SKent Overstreet */ 14651c6fdbd8SKent Overstreet bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, 14661c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 14671c6fdbd8SKent Overstreet { 14681c6fdbd8SKent Overstreet struct bch_devs_mask new_online_devs; 14699fea2274SKent Overstreet int nr_rw = 0, required; 14701c6fdbd8SKent Overstreet 14711c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 14721c6fdbd8SKent Overstreet 14731c6fdbd8SKent Overstreet switch (new_state) { 14742436cb9fSKent Overstreet case BCH_MEMBER_STATE_rw: 14751c6fdbd8SKent Overstreet return true; 14762436cb9fSKent Overstreet case BCH_MEMBER_STATE_ro: 14772436cb9fSKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_rw) 14781c6fdbd8SKent Overstreet return true; 14791c6fdbd8SKent Overstreet 14801c6fdbd8SKent Overstreet /* do we have enough devices to write to? */ 14819fea2274SKent Overstreet for_each_member_device(c, ca2) 14821c6fdbd8SKent Overstreet if (ca2 != ca) 14832436cb9fSKent Overstreet nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw; 14841c6fdbd8SKent Overstreet 14851c6fdbd8SKent Overstreet required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED) 14861c6fdbd8SKent Overstreet ? c->opts.metadata_replicas 14874e074475SKent Overstreet : metadata_replicas_required(c), 14881c6fdbd8SKent Overstreet !(flags & BCH_FORCE_IF_DATA_DEGRADED) 14891c6fdbd8SKent Overstreet ? c->opts.data_replicas 14904e074475SKent Overstreet : data_replicas_required(c)); 14911c6fdbd8SKent Overstreet 14921c6fdbd8SKent Overstreet return nr_rw >= required; 14932436cb9fSKent Overstreet case BCH_MEMBER_STATE_failed: 14942436cb9fSKent Overstreet case BCH_MEMBER_STATE_spare: 14952436cb9fSKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_rw && 14962436cb9fSKent Overstreet ca->mi.state != BCH_MEMBER_STATE_ro) 14971c6fdbd8SKent Overstreet return true; 14981c6fdbd8SKent Overstreet 14991c6fdbd8SKent Overstreet /* do we have enough devices to read from? */ 15001c6fdbd8SKent Overstreet new_online_devs = bch2_online_devs(c); 15011c6fdbd8SKent Overstreet __clear_bit(ca->dev_idx, new_online_devs.d); 15021c6fdbd8SKent Overstreet 1503fcb3431bSKent Overstreet return bch2_have_enough_devs(c, new_online_devs, flags, false); 15041c6fdbd8SKent Overstreet default: 15051c6fdbd8SKent Overstreet BUG(); 15061c6fdbd8SKent Overstreet } 15071c6fdbd8SKent Overstreet } 15081c6fdbd8SKent Overstreet 15091c6fdbd8SKent Overstreet static bool bch2_fs_may_start(struct bch_fs *c) 15101c6fdbd8SKent Overstreet { 15111c6fdbd8SKent Overstreet struct bch_dev *ca; 1512fcb3431bSKent Overstreet unsigned i, flags = 0; 15131c6fdbd8SKent Overstreet 1514fcb3431bSKent Overstreet if (c->opts.very_degraded) 1515fcb3431bSKent Overstreet flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST; 1516fcb3431bSKent Overstreet 1517fcb3431bSKent Overstreet if (c->opts.degraded) 1518fcb3431bSKent Overstreet flags |= BCH_FORCE_IF_DEGRADED; 1519fcb3431bSKent Overstreet 1520fcb3431bSKent Overstreet if (!c->opts.degraded && 1521fcb3431bSKent Overstreet !c->opts.very_degraded) { 15221c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15231c6fdbd8SKent Overstreet 15241c6fdbd8SKent Overstreet for (i = 0; i < c->disk_sb.sb->nr_devices; i++) { 15252f724563SKent Overstreet if (!bch2_member_exists(c->disk_sb.sb, i)) 15261c6fdbd8SKent Overstreet continue; 15271c6fdbd8SKent Overstreet 15282f724563SKent Overstreet ca = bch2_dev_locked(c, i); 15291c6fdbd8SKent Overstreet 15301c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca) && 15312436cb9fSKent Overstreet (ca->mi.state == BCH_MEMBER_STATE_rw || 15322436cb9fSKent Overstreet ca->mi.state == BCH_MEMBER_STATE_ro)) { 15331c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15341c6fdbd8SKent Overstreet return false; 15351c6fdbd8SKent Overstreet } 15361c6fdbd8SKent Overstreet } 15371c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15381c6fdbd8SKent Overstreet } 15391c6fdbd8SKent Overstreet 1540fcb3431bSKent Overstreet return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true); 15411c6fdbd8SKent Overstreet } 15421c6fdbd8SKent Overstreet 15431c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) 15441c6fdbd8SKent Overstreet { 15451c6fdbd8SKent Overstreet /* 15461c6fdbd8SKent Overstreet * The allocator thread itself allocates btree nodes, so stop it first: 15471c6fdbd8SKent Overstreet */ 15481c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 154936da8e38SKent Overstreet bch2_recalc_capacity(c); 15501c6fdbd8SKent Overstreet bch2_dev_journal_stop(&c->journal, ca); 15511c6fdbd8SKent Overstreet } 15521c6fdbd8SKent Overstreet 1553f25d8215SKent Overstreet static void __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) 15541c6fdbd8SKent Overstreet { 15551c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 15561c6fdbd8SKent Overstreet 15572436cb9fSKent Overstreet BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw); 15581c6fdbd8SKent Overstreet 15591c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 15601c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 156164ee1431SKent Overstreet bch2_dev_do_discards(ca); 15621c6fdbd8SKent Overstreet } 15631c6fdbd8SKent Overstreet 15641c6fdbd8SKent Overstreet int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 15651c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 15661c6fdbd8SKent Overstreet { 15673f7b9713SHunter Shaffer struct bch_member *m; 15681c6fdbd8SKent Overstreet int ret = 0; 15691c6fdbd8SKent Overstreet 15701c6fdbd8SKent Overstreet if (ca->mi.state == new_state) 15711c6fdbd8SKent Overstreet return 0; 15721c6fdbd8SKent Overstreet 15731c6fdbd8SKent Overstreet if (!bch2_dev_state_allowed(c, ca, new_state, flags)) 157478c0b75cSKent Overstreet return -BCH_ERR_device_state_not_allowed; 15751c6fdbd8SKent Overstreet 15762436cb9fSKent Overstreet if (new_state != BCH_MEMBER_STATE_rw) 15771c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 15781c6fdbd8SKent Overstreet 15792436cb9fSKent Overstreet bch_notice(ca, "%s", bch2_member_states[new_state]); 15801c6fdbd8SKent Overstreet 15811c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15823f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 15833f7b9713SHunter Shaffer SET_BCH_MEMBER_STATE(m, new_state); 15841c6fdbd8SKent Overstreet bch2_write_super(c); 15851c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15861c6fdbd8SKent Overstreet 1587e2b60560SKent Overstreet if (new_state == BCH_MEMBER_STATE_rw) 1588f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 15891c6fdbd8SKent Overstreet 15901c6fdbd8SKent Overstreet rebalance_wakeup(c); 15911c6fdbd8SKent Overstreet 15921c6fdbd8SKent Overstreet return ret; 15931c6fdbd8SKent Overstreet } 15941c6fdbd8SKent Overstreet 15951c6fdbd8SKent Overstreet int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 15961c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 15971c6fdbd8SKent Overstreet { 15981c6fdbd8SKent Overstreet int ret; 15991c6fdbd8SKent Overstreet 16001ada1606SKent Overstreet down_write(&c->state_lock); 16011c6fdbd8SKent Overstreet ret = __bch2_dev_set_state(c, ca, new_state, flags); 16021ada1606SKent Overstreet up_write(&c->state_lock); 16031c6fdbd8SKent Overstreet 16041c6fdbd8SKent Overstreet return ret; 16051c6fdbd8SKent Overstreet } 16061c6fdbd8SKent Overstreet 16071c6fdbd8SKent Overstreet /* Device add/removal: */ 16081c6fdbd8SKent Overstreet 16091c6fdbd8SKent Overstreet int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) 16101c6fdbd8SKent Overstreet { 16113f7b9713SHunter Shaffer struct bch_member *m; 16121c6fdbd8SKent Overstreet unsigned dev_idx = ca->dev_idx, data; 161378c0b75cSKent Overstreet int ret; 16141c6fdbd8SKent Overstreet 16151ada1606SKent Overstreet down_write(&c->state_lock); 16161c6fdbd8SKent Overstreet 161731ba2cd3SKent Overstreet /* 161831ba2cd3SKent Overstreet * We consume a reference to ca->ref, regardless of whether we succeed 161931ba2cd3SKent Overstreet * or fail: 162031ba2cd3SKent Overstreet */ 1621f295298bSKent Overstreet bch2_dev_put(ca); 16221c6fdbd8SKent Overstreet 16232436cb9fSKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 16241c6fdbd8SKent Overstreet bch_err(ca, "Cannot remove without losing data"); 162578c0b75cSKent Overstreet ret = -BCH_ERR_device_state_not_allowed; 16261c6fdbd8SKent Overstreet goto err; 16271c6fdbd8SKent Overstreet } 16281c6fdbd8SKent Overstreet 16291c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 16301c6fdbd8SKent Overstreet 16311c6fdbd8SKent Overstreet ret = bch2_dev_data_drop(c, ca->dev_idx, flags); 1632130d229fSKent Overstreet bch_err_msg(ca, ret, "bch2_dev_data_drop()"); 1633cf904c8dSKent Overstreet if (ret) 16341c6fdbd8SKent Overstreet goto err; 16351c6fdbd8SKent Overstreet 16365d20ba48SKent Overstreet ret = bch2_dev_remove_alloc(c, ca); 1637130d229fSKent Overstreet bch_err_msg(ca, ret, "bch2_dev_remove_alloc()"); 1638cf904c8dSKent Overstreet if (ret) 16391c6fdbd8SKent Overstreet goto err; 16401c6fdbd8SKent Overstreet 16411d16c605SKent Overstreet /* 16421d16c605SKent Overstreet * We need to flush the entire journal to get rid of keys that reference 16431d16c605SKent Overstreet * the device being removed before removing the superblock entry 16441d16c605SKent Overstreet */ 16451d16c605SKent Overstreet bch2_journal_flush_all_pins(&c->journal); 16461d16c605SKent Overstreet 16471d16c605SKent Overstreet /* 16481d16c605SKent Overstreet * this is really just needed for the bch2_replicas_gc_(start|end) 16491d16c605SKent Overstreet * calls, and could be cleaned up: 16501d16c605SKent Overstreet */ 1651a9c0a4cbSKent Overstreet ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); 1652130d229fSKent Overstreet bch_err_msg(ca, ret, "bch2_journal_flush_device_pins()"); 1653cf904c8dSKent Overstreet if (ret) 1654a9c0a4cbSKent Overstreet goto err; 1655a9c0a4cbSKent Overstreet 1656a9c0a4cbSKent Overstreet ret = bch2_journal_flush(&c->journal); 1657130d229fSKent Overstreet bch_err_msg(ca, ret, "bch2_journal_flush()"); 1658cf904c8dSKent Overstreet if (ret) 16591c6fdbd8SKent Overstreet goto err; 16601c6fdbd8SKent Overstreet 166131ba2cd3SKent Overstreet ret = bch2_replicas_gc2(c); 1662130d229fSKent Overstreet bch_err_msg(ca, ret, "bch2_replicas_gc2()"); 1663cf904c8dSKent Overstreet if (ret) 166431ba2cd3SKent Overstreet goto err; 166531ba2cd3SKent Overstreet 166631ba2cd3SKent Overstreet data = bch2_dev_has_data(c, ca); 166731ba2cd3SKent Overstreet if (data) { 1668fa8e94faSKent Overstreet struct printbuf data_has = PRINTBUF; 166931ba2cd3SKent Overstreet 1670e58f963cSKent Overstreet prt_bitflags(&data_has, __bch2_data_types, data); 1671fa8e94faSKent Overstreet bch_err(ca, "Remove failed, still has data (%s)", data_has.buf); 1672fa8e94faSKent Overstreet printbuf_exit(&data_has); 167331ba2cd3SKent Overstreet ret = -EBUSY; 167431ba2cd3SKent Overstreet goto err; 167531ba2cd3SKent Overstreet } 167631ba2cd3SKent Overstreet 16771c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 16781c6fdbd8SKent Overstreet 16791c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 16801c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], NULL); 16811c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 16821c6fdbd8SKent Overstreet 1683552aa548SKent Overstreet #ifndef CONFIG_BCACHEFS_DEBUG 16841c6fdbd8SKent Overstreet percpu_ref_kill(&ca->ref); 1685552aa548SKent Overstreet #else 1686552aa548SKent Overstreet ca->dying = true; 1687552aa548SKent Overstreet bch2_dev_put(ca); 1688552aa548SKent Overstreet #endif 16891c6fdbd8SKent Overstreet wait_for_completion(&ca->ref_completion); 16901c6fdbd8SKent Overstreet 16911c6fdbd8SKent Overstreet bch2_dev_free(ca); 16921c6fdbd8SKent Overstreet 16931c6fdbd8SKent Overstreet /* 16941c6fdbd8SKent Overstreet * Free this device's slot in the bch_member array - all pointers to 16951c6fdbd8SKent Overstreet * this device must be gone: 16961c6fdbd8SKent Overstreet */ 16971c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 16983f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx); 16993f7b9713SHunter Shaffer memset(&m->uuid, 0, sizeof(m->uuid)); 17001c6fdbd8SKent Overstreet 17011c6fdbd8SKent Overstreet bch2_write_super(c); 17021c6fdbd8SKent Overstreet 17031c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17041ada1606SKent Overstreet up_write(&c->state_lock); 17051c6fdbd8SKent Overstreet return 0; 17061c6fdbd8SKent Overstreet err: 17072436cb9fSKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw && 1708d3bb629dSKent Overstreet !percpu_ref_is_zero(&ca->io_ref)) 17091c6fdbd8SKent Overstreet __bch2_dev_read_write(c, ca); 17101ada1606SKent Overstreet up_write(&c->state_lock); 17111c6fdbd8SKent Overstreet return ret; 17121c6fdbd8SKent Overstreet } 17131c6fdbd8SKent Overstreet 17141c6fdbd8SKent Overstreet /* Add new device to running filesystem: */ 17151c6fdbd8SKent Overstreet int bch2_dev_add(struct bch_fs *c, const char *path) 17161c6fdbd8SKent Overstreet { 17171c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 17181c6fdbd8SKent Overstreet struct bch_sb_handle sb; 17191c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 1720fa8e94faSKent Overstreet struct printbuf errbuf = PRINTBUF; 172102afcb8cSKent Overstreet struct printbuf label = PRINTBUF; 17221c6fdbd8SKent Overstreet int ret; 17231c6fdbd8SKent Overstreet 17241c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 1725e46c181aSKent Overstreet bch_err_msg(c, ret, "reading super"); 1726cf904c8dSKent Overstreet if (ret) 1727efe68e1dSKent Overstreet goto err; 17281c6fdbd8SKent Overstreet 172917405279SKent Overstreet struct bch_member dev_mi = bch2_sb_member_get(sb.sb, sb.sb->dev_idx); 17301c6fdbd8SKent Overstreet 173102afcb8cSKent Overstreet if (BCH_MEMBER_GROUP(&dev_mi)) { 1732e677179bSKent Overstreet bch2_disk_path_to_text_sb(&label, sb.sb, BCH_MEMBER_GROUP(&dev_mi) - 1); 173302afcb8cSKent Overstreet if (label.allocation_failure) { 173402afcb8cSKent Overstreet ret = -ENOMEM; 173502afcb8cSKent Overstreet goto err; 173602afcb8cSKent Overstreet } 173702afcb8cSKent Overstreet } 173802afcb8cSKent Overstreet 173978c0b75cSKent Overstreet ret = bch2_dev_may_add(sb.sb, c); 1740cf904c8dSKent Overstreet if (ret) 1741efe68e1dSKent Overstreet goto err; 17421c6fdbd8SKent Overstreet 17431c6fdbd8SKent Overstreet ca = __bch2_dev_alloc(c, &dev_mi); 17441c6fdbd8SKent Overstreet if (!ca) { 1745efe68e1dSKent Overstreet ret = -ENOMEM; 1746efe68e1dSKent Overstreet goto err; 17471c6fdbd8SKent Overstreet } 17481c6fdbd8SKent Overstreet 17491c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, &sb); 175071933fb6SChristophe JAILLET if (ret) 1751efe68e1dSKent Overstreet goto err; 17521c6fdbd8SKent Overstreet 175344ec5990SKent Overstreet ret = bch2_dev_journal_alloc(ca, true); 1754e46c181aSKent Overstreet bch_err_msg(c, ret, "allocating journal"); 1755cf904c8dSKent Overstreet if (ret) 17561c6fdbd8SKent Overstreet goto err; 17571c6fdbd8SKent Overstreet 17581ada1606SKent Overstreet down_write(&c->state_lock); 17591c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 17601c6fdbd8SKent Overstreet 17611c6fdbd8SKent Overstreet ret = bch2_sb_from_fs(c, ca); 1762e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 1763cf904c8dSKent Overstreet if (ret) 17641c6fdbd8SKent Overstreet goto err_unlock; 17651c6fdbd8SKent Overstreet 17661c6fdbd8SKent Overstreet if (dynamic_fault("bcachefs:add:no_slot")) 17671c6fdbd8SKent Overstreet goto err_unlock; 17681c6fdbd8SKent Overstreet 176917405279SKent Overstreet ret = bch2_sb_member_alloc(c); 177017405279SKent Overstreet if (ret < 0) { 1771e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 17721c6fdbd8SKent Overstreet goto err_unlock; 1773e8536925SKent Overstreet } 177417405279SKent Overstreet unsigned dev_idx = ret; 17751c6fdbd8SKent Overstreet 17761c6fdbd8SKent Overstreet /* success: */ 17771c6fdbd8SKent Overstreet 177817405279SKent Overstreet dev_mi.last_mount = cpu_to_le64(ktime_get_real_seconds()); 177917405279SKent Overstreet *bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx) = dev_mi; 17801c6fdbd8SKent Overstreet 17811c6fdbd8SKent Overstreet ca->disk_sb.sb->dev_idx = dev_idx; 17821c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 17831c6fdbd8SKent Overstreet 178402afcb8cSKent Overstreet if (BCH_MEMBER_GROUP(&dev_mi)) { 178502afcb8cSKent Overstreet ret = __bch2_dev_group_set(c, ca, label.buf); 1786e46c181aSKent Overstreet bch_err_msg(c, ret, "creating new label"); 1787cf904c8dSKent Overstreet if (ret) 178802afcb8cSKent Overstreet goto err_unlock; 178902afcb8cSKent Overstreet } 179002afcb8cSKent Overstreet 17911c6fdbd8SKent Overstreet bch2_write_super(c); 17921c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17931c6fdbd8SKent Overstreet 1794fb23d57aSKent Overstreet ret = bch2_dev_usage_init(ca, false); 17951d16c605SKent Overstreet if (ret) 17961d16c605SKent Overstreet goto err_late; 17971d16c605SKent Overstreet 17985dd8c60eSKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); 1799bbe682c7SKent Overstreet bch_err_msg(ca, ret, "marking new superblock"); 1800cf904c8dSKent Overstreet if (ret) 1801bfcf840dSKent Overstreet goto err_late; 18028d6b6222SKent Overstreet 1803c6b2826cSKent Overstreet ret = bch2_fs_freespace_init(c); 1804bbe682c7SKent Overstreet bch_err_msg(ca, ret, "initializing free space"); 1805cf904c8dSKent Overstreet if (ret) 1806c6b2826cSKent Overstreet goto err_late; 1807c6b2826cSKent Overstreet 180809943313SKent Overstreet ca->new_fs_bucket_idx = 0; 180909943313SKent Overstreet 1810f25d8215SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw) 1811f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 18121c6fdbd8SKent Overstreet 18131ada1606SKent Overstreet up_write(&c->state_lock); 18141c6fdbd8SKent Overstreet return 0; 18151c6fdbd8SKent Overstreet 18161c6fdbd8SKent Overstreet err_unlock: 18171c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 18181ada1606SKent Overstreet up_write(&c->state_lock); 18191c6fdbd8SKent Overstreet err: 18201c6fdbd8SKent Overstreet if (ca) 18211c6fdbd8SKent Overstreet bch2_dev_free(ca); 18221c6fdbd8SKent Overstreet bch2_free_super(&sb); 182302afcb8cSKent Overstreet printbuf_exit(&label); 1824fa8e94faSKent Overstreet printbuf_exit(&errbuf); 1825cf904c8dSKent Overstreet bch_err_fn(c, ret); 18261c6fdbd8SKent Overstreet return ret; 18271c6fdbd8SKent Overstreet err_late: 1828bfcf840dSKent Overstreet up_write(&c->state_lock); 1829efe68e1dSKent Overstreet ca = NULL; 1830efe68e1dSKent Overstreet goto err; 18311c6fdbd8SKent Overstreet } 18321c6fdbd8SKent Overstreet 18331c6fdbd8SKent Overstreet /* Hot add existing device to running filesystem: */ 18341c6fdbd8SKent Overstreet int bch2_dev_online(struct bch_fs *c, const char *path) 18351c6fdbd8SKent Overstreet { 18361c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 18371c6fdbd8SKent Overstreet struct bch_sb_handle sb = { NULL }; 18381c6fdbd8SKent Overstreet struct bch_dev *ca; 18391c6fdbd8SKent Overstreet unsigned dev_idx; 18401c6fdbd8SKent Overstreet int ret; 18411c6fdbd8SKent Overstreet 18421ada1606SKent Overstreet down_write(&c->state_lock); 18431c6fdbd8SKent Overstreet 18441c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 18451c6fdbd8SKent Overstreet if (ret) { 18461ada1606SKent Overstreet up_write(&c->state_lock); 18471c6fdbd8SKent Overstreet return ret; 18481c6fdbd8SKent Overstreet } 18491c6fdbd8SKent Overstreet 18501c6fdbd8SKent Overstreet dev_idx = sb.sb->dev_idx; 18511c6fdbd8SKent Overstreet 18522f300f09SKent Overstreet ret = bch2_dev_in_fs(&c->disk_sb, &sb, &c->opts); 1853e46c181aSKent Overstreet bch_err_msg(c, ret, "bringing %s online", path); 1854cf904c8dSKent Overstreet if (ret) 18551c6fdbd8SKent Overstreet goto err; 18561c6fdbd8SKent Overstreet 1857e2b60560SKent Overstreet ret = bch2_dev_attach_bdev(c, &sb); 1858e2b60560SKent Overstreet if (ret) 1859e2b60560SKent Overstreet goto err; 1860e2b60560SKent Overstreet 18612f724563SKent Overstreet ca = bch2_dev_locked(c, dev_idx); 1862bfcf840dSKent Overstreet 18635dd8c60eSKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); 1864e46c181aSKent Overstreet bch_err_msg(c, ret, "bringing %s online: error from bch2_trans_mark_dev_sb", path); 1865cf904c8dSKent Overstreet if (ret) 1866bfcf840dSKent Overstreet goto err; 1867bfcf840dSKent Overstreet 1868f25d8215SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw) 1869f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 18701c6fdbd8SKent Overstreet 1871bbe682c7SKent Overstreet if (!ca->mi.freespace_initialized) { 1872bbe682c7SKent Overstreet ret = bch2_dev_freespace_init(c, ca, 0, ca->mi.nbuckets); 1873bbe682c7SKent Overstreet bch_err_msg(ca, ret, "initializing free space"); 1874bbe682c7SKent Overstreet if (ret) 1875bbe682c7SKent Overstreet goto err; 1876bbe682c7SKent Overstreet } 1877bbe682c7SKent Overstreet 1878bbe682c7SKent Overstreet if (!ca->journal.nr) { 187944ec5990SKent Overstreet ret = bch2_dev_journal_alloc(ca, false); 1880bbe682c7SKent Overstreet bch_err_msg(ca, ret, "allocating journal"); 1881bbe682c7SKent Overstreet if (ret) 1882bbe682c7SKent Overstreet goto err; 1883bbe682c7SKent Overstreet } 1884bbe682c7SKent Overstreet 18851c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 1886bbe682c7SKent Overstreet bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = 1887a420eea6STim Schlueter cpu_to_le64(ktime_get_real_seconds()); 18881c6fdbd8SKent Overstreet bch2_write_super(c); 18891c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 18901c6fdbd8SKent Overstreet 18911ada1606SKent Overstreet up_write(&c->state_lock); 18921c6fdbd8SKent Overstreet return 0; 18931c6fdbd8SKent Overstreet err: 18941ada1606SKent Overstreet up_write(&c->state_lock); 18951c6fdbd8SKent Overstreet bch2_free_super(&sb); 189678c0b75cSKent Overstreet return ret; 18971c6fdbd8SKent Overstreet } 18981c6fdbd8SKent Overstreet 18991c6fdbd8SKent Overstreet int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) 19001c6fdbd8SKent Overstreet { 19011ada1606SKent Overstreet down_write(&c->state_lock); 19021c6fdbd8SKent Overstreet 19031c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca)) { 19041c6fdbd8SKent Overstreet bch_err(ca, "Already offline"); 19051ada1606SKent Overstreet up_write(&c->state_lock); 19061c6fdbd8SKent Overstreet return 0; 19071c6fdbd8SKent Overstreet } 19081c6fdbd8SKent Overstreet 19092436cb9fSKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 19101c6fdbd8SKent Overstreet bch_err(ca, "Cannot offline required disk"); 19111ada1606SKent Overstreet up_write(&c->state_lock); 191278c0b75cSKent Overstreet return -BCH_ERR_device_state_not_allowed; 19131c6fdbd8SKent Overstreet } 19141c6fdbd8SKent Overstreet 19151c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 19161c6fdbd8SKent Overstreet 19171ada1606SKent Overstreet up_write(&c->state_lock); 19181c6fdbd8SKent Overstreet return 0; 19191c6fdbd8SKent Overstreet } 19201c6fdbd8SKent Overstreet 19211c6fdbd8SKent Overstreet int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) 19221c6fdbd8SKent Overstreet { 19233f7b9713SHunter Shaffer struct bch_member *m; 192469d1f052SKent Overstreet u64 old_nbuckets; 19251c6fdbd8SKent Overstreet int ret = 0; 19261c6fdbd8SKent Overstreet 19271ada1606SKent Overstreet down_write(&c->state_lock); 192869d1f052SKent Overstreet old_nbuckets = ca->mi.nbuckets; 19291c6fdbd8SKent Overstreet 19301c6fdbd8SKent Overstreet if (nbuckets < ca->mi.nbuckets) { 19311c6fdbd8SKent Overstreet bch_err(ca, "Cannot shrink yet"); 19321c6fdbd8SKent Overstreet ret = -EINVAL; 19331c6fdbd8SKent Overstreet goto err; 19341c6fdbd8SKent Overstreet } 19351c6fdbd8SKent Overstreet 1936db42549dSKent Overstreet if (nbuckets > BCH_MEMBER_NBUCKETS_MAX) { 1937db42549dSKent Overstreet bch_err(ca, "New device size too big (%llu greater than max %u)", 1938db42549dSKent Overstreet nbuckets, BCH_MEMBER_NBUCKETS_MAX); 1939db42549dSKent Overstreet ret = -BCH_ERR_device_size_too_big; 1940db42549dSKent Overstreet goto err; 1941db42549dSKent Overstreet } 1942db42549dSKent Overstreet 19431c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca) && 19441c6fdbd8SKent Overstreet get_capacity(ca->disk_sb.bdev->bd_disk) < 19451c6fdbd8SKent Overstreet ca->mi.bucket_size * nbuckets) { 19461c6fdbd8SKent Overstreet bch_err(ca, "New size larger than device"); 194778c0b75cSKent Overstreet ret = -BCH_ERR_device_size_too_small; 19481c6fdbd8SKent Overstreet goto err; 19491c6fdbd8SKent Overstreet } 19501c6fdbd8SKent Overstreet 19511c6fdbd8SKent Overstreet ret = bch2_dev_buckets_resize(c, ca, nbuckets); 1952e46c181aSKent Overstreet bch_err_msg(ca, ret, "resizing buckets"); 1953cf904c8dSKent Overstreet if (ret) 19541c6fdbd8SKent Overstreet goto err; 19551c6fdbd8SKent Overstreet 19565dd8c60eSKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); 19573e3e02e6SKent Overstreet if (ret) 1958224ec3e6SKent Overstreet goto err; 1959224ec3e6SKent Overstreet 19601c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 19613f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 19623f7b9713SHunter Shaffer m->nbuckets = cpu_to_le64(nbuckets); 19631c6fdbd8SKent Overstreet 19641c6fdbd8SKent Overstreet bch2_write_super(c); 19651c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 19661c6fdbd8SKent Overstreet 196769d1f052SKent Overstreet if (ca->mi.freespace_initialized) { 19681d16c605SKent Overstreet struct disk_accounting_pos acc = { 19691d16c605SKent Overstreet .type = BCH_DISK_ACCOUNTING_dev_data_type, 19701d16c605SKent Overstreet .dev_data_type.dev = ca->dev_idx, 19711d16c605SKent Overstreet .dev_data_type.data_type = BCH_DATA_free, 19721d16c605SKent Overstreet }; 19731d16c605SKent Overstreet u64 v[3] = { nbuckets - old_nbuckets, 0, 0 }; 19741d16c605SKent Overstreet 1975fb23d57aSKent Overstreet ret = bch2_trans_do(ca->fs, NULL, NULL, 0, 1976fb23d57aSKent Overstreet bch2_disk_accounting_mod(trans, &acc, v, ARRAY_SIZE(v), false)) ?: 1977fb23d57aSKent Overstreet bch2_dev_freespace_init(c, ca, old_nbuckets, nbuckets); 197869d1f052SKent Overstreet if (ret) 197969d1f052SKent Overstreet goto err; 198069d1f052SKent Overstreet } 198169d1f052SKent Overstreet 19821c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 19831c6fdbd8SKent Overstreet err: 19841ada1606SKent Overstreet up_write(&c->state_lock); 19851c6fdbd8SKent Overstreet return ret; 19861c6fdbd8SKent Overstreet } 19871c6fdbd8SKent Overstreet 19881c6fdbd8SKent Overstreet /* return with ref on ca->ref: */ 1989bf7e49a4SKent Overstreet struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name) 19901c6fdbd8SKent Overstreet { 199151fc436cSThomas Bertschinger if (!strncmp(name, "/dev/", strlen("/dev/"))) 199251fc436cSThomas Bertschinger name += strlen("/dev/"); 199351fc436cSThomas Bertschinger 19943a2d0259SKent Overstreet for_each_member_device(c, ca) 19953a2d0259SKent Overstreet if (!strcmp(name, ca->name)) 19961c6fdbd8SKent Overstreet return ca; 199741b84fb4SKent Overstreet return ERR_PTR(-BCH_ERR_ENOENT_dev_not_found); 199841b84fb4SKent Overstreet } 19991c6fdbd8SKent Overstreet 20001c6fdbd8SKent Overstreet /* Filesystem open: */ 20011c6fdbd8SKent Overstreet 20020d529663SKent Overstreet static inline int sb_cmp(struct bch_sb *l, struct bch_sb *r) 20030d529663SKent Overstreet { 20040d529663SKent Overstreet return cmp_int(le64_to_cpu(l->seq), le64_to_cpu(r->seq)) ?: 20050d529663SKent Overstreet cmp_int(le64_to_cpu(l->write_time), le64_to_cpu(r->write_time)); 20060d529663SKent Overstreet } 20070d529663SKent Overstreet 20081c6fdbd8SKent Overstreet struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, 20091c6fdbd8SKent Overstreet struct bch_opts opts) 20101c6fdbd8SKent Overstreet { 2011d4c8bb69SKent Overstreet DARRAY(struct bch_sb_handle) sbs = { 0 }; 20121c6fdbd8SKent Overstreet struct bch_fs *c = NULL; 2013defd9e39SKent Overstreet struct bch_sb_handle *best = NULL; 2014fa8e94faSKent Overstreet struct printbuf errbuf = PRINTBUF; 2015e2b60560SKent Overstreet int ret = 0; 20161c6fdbd8SKent Overstreet 2017efe68e1dSKent Overstreet if (!try_module_get(THIS_MODULE)) 2018efe68e1dSKent Overstreet return ERR_PTR(-ENODEV); 2019efe68e1dSKent Overstreet 20201c6fdbd8SKent Overstreet if (!nr_devices) { 2021efe68e1dSKent Overstreet ret = -EINVAL; 2022efe68e1dSKent Overstreet goto err; 20231c6fdbd8SKent Overstreet } 20241c6fdbd8SKent Overstreet 2025d4c8bb69SKent Overstreet ret = darray_make_room(&sbs, nr_devices); 20261c6fdbd8SKent Overstreet if (ret) 20271c6fdbd8SKent Overstreet goto err; 20281c6fdbd8SKent Overstreet 2029d4c8bb69SKent Overstreet for (unsigned i = 0; i < nr_devices; i++) { 2030d4c8bb69SKent Overstreet struct bch_sb_handle sb = { NULL }; 2031d4c8bb69SKent Overstreet 2032d4c8bb69SKent Overstreet ret = bch2_read_super(devices[i], &opts, &sb); 2033d4c8bb69SKent Overstreet if (ret) 2034d4c8bb69SKent Overstreet goto err; 2035d4c8bb69SKent Overstreet 2036d4c8bb69SKent Overstreet BUG_ON(darray_push(&sbs, sb)); 20371c6fdbd8SKent Overstreet } 20381c6fdbd8SKent Overstreet 203962719cf3SKent Overstreet if (opts.nochanges && !opts.read_only) { 204062719cf3SKent Overstreet ret = -BCH_ERR_erofs_nochanges; 204162719cf3SKent Overstreet goto err_print; 204262719cf3SKent Overstreet } 204362719cf3SKent Overstreet 2044d4c8bb69SKent Overstreet darray_for_each(sbs, sb) 20450d529663SKent Overstreet if (!best || sb_cmp(sb->sb, best->sb) > 0) 2046d4c8bb69SKent Overstreet best = sb; 20471c6fdbd8SKent Overstreet 2048d4c8bb69SKent Overstreet darray_for_each_reverse(sbs, sb) { 20492f300f09SKent Overstreet ret = bch2_dev_in_fs(best, sb, &opts); 20500d529663SKent Overstreet 20510d529663SKent Overstreet if (ret == -BCH_ERR_device_has_been_removed || 20520d529663SKent Overstreet ret == -BCH_ERR_device_splitbrain) { 2053d4c8bb69SKent Overstreet bch2_free_super(sb); 2054d4c8bb69SKent Overstreet darray_remove_item(&sbs, sb); 2055d4c8bb69SKent Overstreet best -= best > sb; 20560d529663SKent Overstreet ret = 0; 2057625104eaSKent Overstreet continue; 2058625104eaSKent Overstreet } 2059625104eaSKent Overstreet 206078c0b75cSKent Overstreet if (ret) 20611c6fdbd8SKent Overstreet goto err_print; 20621c6fdbd8SKent Overstreet } 20631c6fdbd8SKent Overstreet 2064d4c8bb69SKent Overstreet c = bch2_fs_alloc(best->sb, opts); 2065d4c8bb69SKent Overstreet ret = PTR_ERR_OR_ZERO(c); 2066d4c8bb69SKent Overstreet if (ret) 20671c6fdbd8SKent Overstreet goto err; 20681c6fdbd8SKent Overstreet 20691ada1606SKent Overstreet down_write(&c->state_lock); 2070d4c8bb69SKent Overstreet darray_for_each(sbs, sb) { 2071d4c8bb69SKent Overstreet ret = bch2_dev_attach_bdev(c, sb); 2072e2b60560SKent Overstreet if (ret) { 20731ada1606SKent Overstreet up_write(&c->state_lock); 2074e2b60560SKent Overstreet goto err; 2075e2b60560SKent Overstreet } 20761c6fdbd8SKent Overstreet } 20771ada1606SKent Overstreet up_write(&c->state_lock); 20781c6fdbd8SKent Overstreet 207978c0b75cSKent Overstreet if (!bch2_fs_may_start(c)) { 208078c0b75cSKent Overstreet ret = -BCH_ERR_insufficient_devices_to_start; 20811c6fdbd8SKent Overstreet goto err_print; 208278c0b75cSKent Overstreet } 20831c6fdbd8SKent Overstreet 20841c6fdbd8SKent Overstreet if (!c->opts.nostart) { 2085619f5beeSKent Overstreet ret = bch2_fs_start(c); 2086619f5beeSKent Overstreet if (ret) 2087619f5beeSKent Overstreet goto err; 20881c6fdbd8SKent Overstreet } 20891c6fdbd8SKent Overstreet out: 2090d4c8bb69SKent Overstreet darray_for_each(sbs, sb) 2091d4c8bb69SKent Overstreet bch2_free_super(sb); 2092d4c8bb69SKent Overstreet darray_exit(&sbs); 2093fa8e94faSKent Overstreet printbuf_exit(&errbuf); 20941c6fdbd8SKent Overstreet module_put(THIS_MODULE); 20951c6fdbd8SKent Overstreet return c; 20961c6fdbd8SKent Overstreet err_print: 20971c6fdbd8SKent Overstreet pr_err("bch_fs_open err opening %s: %s", 209878c0b75cSKent Overstreet devices[0], bch2_err_str(ret)); 20991c6fdbd8SKent Overstreet err: 21007be9ab63SChris Webb if (!IS_ERR_OR_NULL(c)) 21011c6fdbd8SKent Overstreet bch2_fs_stop(c); 21021c6fdbd8SKent Overstreet c = ERR_PTR(ret); 21031c6fdbd8SKent Overstreet goto out; 21041c6fdbd8SKent Overstreet } 21051c6fdbd8SKent Overstreet 21061c6fdbd8SKent Overstreet /* Global interfaces/init */ 21071c6fdbd8SKent Overstreet 21081c6fdbd8SKent Overstreet static void bcachefs_exit(void) 21091c6fdbd8SKent Overstreet { 21101c6fdbd8SKent Overstreet bch2_debug_exit(); 21111c6fdbd8SKent Overstreet bch2_vfs_exit(); 21121c6fdbd8SKent Overstreet bch2_chardev_exit(); 211314ba3706SKent Overstreet bch2_btree_key_cache_exit(); 21141c6fdbd8SKent Overstreet if (bcachefs_kset) 21151c6fdbd8SKent Overstreet kset_unregister(bcachefs_kset); 21161c6fdbd8SKent Overstreet } 21171c6fdbd8SKent Overstreet 21181c6fdbd8SKent Overstreet static int __init bcachefs_init(void) 21191c6fdbd8SKent Overstreet { 21201c6fdbd8SKent Overstreet bch2_bkey_pack_test(); 21211c6fdbd8SKent Overstreet 21221c6fdbd8SKent Overstreet if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) || 212314ba3706SKent Overstreet bch2_btree_key_cache_init() || 21241c6fdbd8SKent Overstreet bch2_chardev_init() || 21251c6fdbd8SKent Overstreet bch2_vfs_init() || 21261c6fdbd8SKent Overstreet bch2_debug_init()) 21271c6fdbd8SKent Overstreet goto err; 21281c6fdbd8SKent Overstreet 21291c6fdbd8SKent Overstreet return 0; 21301c6fdbd8SKent Overstreet err: 21311c6fdbd8SKent Overstreet bcachefs_exit(); 21321c6fdbd8SKent Overstreet return -ENOMEM; 21331c6fdbd8SKent Overstreet } 21341c6fdbd8SKent Overstreet 21351c6fdbd8SKent Overstreet #define BCH_DEBUG_PARAM(name, description) \ 21361c6fdbd8SKent Overstreet bool bch2_##name; \ 21371c6fdbd8SKent Overstreet module_param_named(name, bch2_##name, bool, 0644); \ 21381c6fdbd8SKent Overstreet MODULE_PARM_DESC(name, description); 21391c6fdbd8SKent Overstreet BCH_DEBUG_PARAMS() 21401c6fdbd8SKent Overstreet #undef BCH_DEBUG_PARAM 21411c6fdbd8SKent Overstreet 2142e08e63e4SKent Overstreet __maybe_unused 214373bd774dSKent Overstreet static unsigned bch2_metadata_version = bcachefs_metadata_version_current; 21441c6fdbd8SKent Overstreet module_param_named(version, bch2_metadata_version, uint, 0400); 21451c6fdbd8SKent Overstreet 21461c6fdbd8SKent Overstreet module_exit(bcachefs_exit); 21471c6fdbd8SKent Overstreet module_init(bcachefs_init); 2148