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" 281c6fdbd8SKent Overstreet #include "disk_groups.h" 29cd575ddfSKent Overstreet #include "ec.h" 30d4bf5eecSKent Overstreet #include "errcode.h" 311c6fdbd8SKent Overstreet #include "error.h" 321c6fdbd8SKent Overstreet #include "fs.h" 331c6fdbd8SKent Overstreet #include "fs-io.h" 34dbbfca9fSKent Overstreet #include "fs-io-buffered.h" 35dbbfca9fSKent Overstreet #include "fs-io-direct.h" 361c6fdbd8SKent Overstreet #include "fsck.h" 371c6fdbd8SKent Overstreet #include "inode.h" 381809b8cbSKent Overstreet #include "io_read.h" 391809b8cbSKent Overstreet #include "io_write.h" 401c6fdbd8SKent Overstreet #include "journal.h" 411c6fdbd8SKent Overstreet #include "journal_reclaim.h" 421dd7f9d9SKent Overstreet #include "journal_seq_blacklist.h" 431c6fdbd8SKent Overstreet #include "move.h" 441c6fdbd8SKent Overstreet #include "migrate.h" 451c6fdbd8SKent Overstreet #include "movinggc.h" 46350175bfSKent Overstreet #include "nocow_locking.h" 471c6fdbd8SKent Overstreet #include "quota.h" 481c6fdbd8SKent Overstreet #include "rebalance.h" 491c6fdbd8SKent Overstreet #include "recovery.h" 501c6fdbd8SKent Overstreet #include "replicas.h" 51a37ad1a3SKent Overstreet #include "sb-clean.h" 523a58dfbcSKent Overstreet #include "sb-counters.h" 53f5d26fa3SKent Overstreet #include "sb-errors.h" 541241df58SHunter Shaffer #include "sb-members.h" 558e877caaSKent Overstreet #include "snapshot.h" 5614b393eeSKent Overstreet #include "subvolume.h" 571c6fdbd8SKent Overstreet #include "super.h" 581c6fdbd8SKent Overstreet #include "super-io.h" 591c6fdbd8SKent Overstreet #include "sysfs.h" 6060e1baa8SKent Overstreet #include "thread_with_file.h" 611c6fdbd8SKent Overstreet #include "trace.h" 621c6fdbd8SKent Overstreet 631c6fdbd8SKent Overstreet #include <linux/backing-dev.h> 641c6fdbd8SKent Overstreet #include <linux/blkdev.h> 651c6fdbd8SKent Overstreet #include <linux/debugfs.h> 661c6fdbd8SKent Overstreet #include <linux/device.h> 671c6fdbd8SKent Overstreet #include <linux/idr.h> 681c6fdbd8SKent Overstreet #include <linux/module.h> 691c6fdbd8SKent Overstreet #include <linux/percpu.h> 701c6fdbd8SKent Overstreet #include <linux/random.h> 711c6fdbd8SKent Overstreet #include <linux/sysfs.h> 721c6fdbd8SKent Overstreet #include <crypto/hash.h> 731c6fdbd8SKent Overstreet 741c6fdbd8SKent Overstreet MODULE_LICENSE("GPL"); 751c6fdbd8SKent Overstreet MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>"); 76a9737e0bSBrian Foster MODULE_DESCRIPTION("bcachefs filesystem"); 7785c6db98SDaniel Hill MODULE_SOFTDEP("pre: crc32c"); 7885c6db98SDaniel Hill MODULE_SOFTDEP("pre: crc64"); 7985c6db98SDaniel Hill MODULE_SOFTDEP("pre: sha256"); 8085c6db98SDaniel Hill MODULE_SOFTDEP("pre: chacha20"); 8185c6db98SDaniel Hill MODULE_SOFTDEP("pre: poly1305"); 8285c6db98SDaniel Hill MODULE_SOFTDEP("pre: xxhash"); 831c6fdbd8SKent Overstreet 843c471b65SKent Overstreet const char * const bch2_fs_flag_strs[] = { 853c471b65SKent Overstreet #define x(n) #n, 863c471b65SKent Overstreet BCH_FS_FLAGS() 873c471b65SKent Overstreet #undef x 883c471b65SKent Overstreet NULL 893c471b65SKent Overstreet }; 903c471b65SKent Overstreet 913ed94062SKent Overstreet __printf(2, 0) 92f3589bfaSKent Overstreet static void bch2_print_maybe_redirect(struct stdio_redirect *stdio, const char *fmt, va_list args) 93f3589bfaSKent Overstreet { 94f3589bfaSKent Overstreet #ifdef __KERNEL__ 95f3589bfaSKent Overstreet if (unlikely(stdio)) { 96f3589bfaSKent Overstreet if (fmt[0] == KERN_SOH[0]) 97f3589bfaSKent Overstreet fmt += 2; 98f3589bfaSKent Overstreet 99f3589bfaSKent Overstreet bch2_stdio_redirect_vprintf(stdio, true, fmt, args); 100f3589bfaSKent Overstreet return; 101f3589bfaSKent Overstreet } 102f3589bfaSKent Overstreet #endif 103f3589bfaSKent Overstreet vprintk(fmt, args); 104f3589bfaSKent Overstreet } 105f3589bfaSKent Overstreet 106b63570f7SKent Overstreet void bch2_print_opts(struct bch_opts *opts, const char *fmt, ...) 107b63570f7SKent Overstreet { 108b63570f7SKent Overstreet struct stdio_redirect *stdio = (void *)(unsigned long)opts->stdio; 109b63570f7SKent Overstreet 110b63570f7SKent Overstreet va_list args; 111b63570f7SKent Overstreet va_start(args, fmt); 112f3589bfaSKent Overstreet bch2_print_maybe_redirect(stdio, fmt, args); 113b63570f7SKent Overstreet va_end(args); 114b63570f7SKent Overstreet } 115b63570f7SKent Overstreet 1162b41226dSKent Overstreet void __bch2_print(struct bch_fs *c, const char *fmt, ...) 1172b41226dSKent Overstreet { 11896f37eabSKent Overstreet struct stdio_redirect *stdio = bch2_fs_stdio_redirect(c); 11996f37eabSKent Overstreet 1202b41226dSKent Overstreet va_list args; 1212b41226dSKent Overstreet va_start(args, fmt); 122f3589bfaSKent Overstreet bch2_print_maybe_redirect(stdio, fmt, args); 1232b41226dSKent Overstreet va_end(args); 1242b41226dSKent Overstreet } 1252b41226dSKent Overstreet 1261c6fdbd8SKent Overstreet #define KTYPE(type) \ 1271c6fdbd8SKent Overstreet static const struct attribute_group type ## _group = { \ 1281c6fdbd8SKent Overstreet .attrs = type ## _files \ 1291c6fdbd8SKent Overstreet }; \ 1301c6fdbd8SKent Overstreet \ 1311c6fdbd8SKent Overstreet static const struct attribute_group *type ## _groups[] = { \ 1321c6fdbd8SKent Overstreet &type ## _group, \ 1331c6fdbd8SKent Overstreet NULL \ 1341c6fdbd8SKent Overstreet }; \ 1351c6fdbd8SKent Overstreet \ 1361c6fdbd8SKent Overstreet static const struct kobj_type type ## _ktype = { \ 1371c6fdbd8SKent Overstreet .release = type ## _release, \ 1381c6fdbd8SKent Overstreet .sysfs_ops = &type ## _sysfs_ops, \ 1391c6fdbd8SKent Overstreet .default_groups = type ## _groups \ 1401c6fdbd8SKent Overstreet } 1411c6fdbd8SKent Overstreet 1421c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *); 1431c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *); 144104c6974SDaniel Hill static void bch2_fs_counters_release(struct kobject *k) 145104c6974SDaniel Hill { 146104c6974SDaniel Hill } 1471c6fdbd8SKent Overstreet 1481c6fdbd8SKent Overstreet static void bch2_fs_internal_release(struct kobject *k) 1491c6fdbd8SKent Overstreet { 1501c6fdbd8SKent Overstreet } 1511c6fdbd8SKent Overstreet 1521c6fdbd8SKent Overstreet static void bch2_fs_opts_dir_release(struct kobject *k) 1531c6fdbd8SKent Overstreet { 1541c6fdbd8SKent Overstreet } 1551c6fdbd8SKent Overstreet 1561c6fdbd8SKent Overstreet static void bch2_fs_time_stats_release(struct kobject *k) 1571c6fdbd8SKent Overstreet { 1581c6fdbd8SKent Overstreet } 1591c6fdbd8SKent Overstreet 1601c6fdbd8SKent Overstreet KTYPE(bch2_fs); 161104c6974SDaniel Hill KTYPE(bch2_fs_counters); 1621c6fdbd8SKent Overstreet KTYPE(bch2_fs_internal); 1631c6fdbd8SKent Overstreet KTYPE(bch2_fs_opts_dir); 1641c6fdbd8SKent Overstreet KTYPE(bch2_fs_time_stats); 1651c6fdbd8SKent Overstreet KTYPE(bch2_dev); 1661c6fdbd8SKent Overstreet 1671c6fdbd8SKent Overstreet static struct kset *bcachefs_kset; 1681c6fdbd8SKent Overstreet static LIST_HEAD(bch_fs_list); 1691c6fdbd8SKent Overstreet static DEFINE_MUTEX(bch_fs_list_lock); 1701c6fdbd8SKent Overstreet 171d94189adSKent Overstreet DECLARE_WAIT_QUEUE_HEAD(bch2_read_only_wait); 1721c6fdbd8SKent Overstreet 1731c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *); 1741c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *, unsigned); 1751c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *); 1761c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *); 1771c6fdbd8SKent Overstreet 1781c6fdbd8SKent Overstreet struct bch_fs *bch2_dev_to_fs(dev_t dev) 1791c6fdbd8SKent Overstreet { 1801c6fdbd8SKent Overstreet struct bch_fs *c; 1811c6fdbd8SKent Overstreet 1821c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 1831c6fdbd8SKent Overstreet rcu_read_lock(); 1841c6fdbd8SKent Overstreet 1851c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 18641b84fb4SKent Overstreet for_each_member_device_rcu(c, ca, NULL) 187ec4ab9d2SDan Robertson if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) { 1881c6fdbd8SKent Overstreet closure_get(&c->cl); 1891c6fdbd8SKent Overstreet goto found; 1901c6fdbd8SKent Overstreet } 1911c6fdbd8SKent Overstreet c = NULL; 1921c6fdbd8SKent Overstreet found: 1931c6fdbd8SKent Overstreet rcu_read_unlock(); 1941c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 1951c6fdbd8SKent Overstreet 1961c6fdbd8SKent Overstreet return c; 1971c6fdbd8SKent Overstreet } 1981c6fdbd8SKent Overstreet 1991c6fdbd8SKent Overstreet static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid) 2001c6fdbd8SKent Overstreet { 2011c6fdbd8SKent Overstreet struct bch_fs *c; 2021c6fdbd8SKent Overstreet 2031c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 2041c6fdbd8SKent Overstreet 2051c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 2061c6fdbd8SKent Overstreet if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid))) 2071c6fdbd8SKent Overstreet return c; 2081c6fdbd8SKent Overstreet 2091c6fdbd8SKent Overstreet return NULL; 2101c6fdbd8SKent Overstreet } 2111c6fdbd8SKent Overstreet 2121c6fdbd8SKent Overstreet struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid) 2131c6fdbd8SKent Overstreet { 2141c6fdbd8SKent Overstreet struct bch_fs *c; 2151c6fdbd8SKent Overstreet 2161c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 2171c6fdbd8SKent Overstreet c = __bch2_uuid_to_fs(uuid); 2181c6fdbd8SKent Overstreet if (c) 2191c6fdbd8SKent Overstreet closure_get(&c->cl); 2201c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 2211c6fdbd8SKent Overstreet 2221c6fdbd8SKent Overstreet return c; 2231c6fdbd8SKent Overstreet } 2241c6fdbd8SKent Overstreet 225180fb49dSKent Overstreet static void bch2_dev_usage_journal_reserve(struct bch_fs *c) 226180fb49dSKent Overstreet { 22741b84fb4SKent Overstreet unsigned nr = 0, u64s = 2284b8f89afSKent Overstreet ((sizeof(struct jset_entry_dev_usage) + 2294b8f89afSKent Overstreet sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR)) / 2304b8f89afSKent Overstreet sizeof(u64); 231180fb49dSKent Overstreet 232180fb49dSKent Overstreet rcu_read_lock(); 23341b84fb4SKent Overstreet for_each_member_device_rcu(c, ca, NULL) 234180fb49dSKent Overstreet nr++; 235180fb49dSKent Overstreet rcu_read_unlock(); 236180fb49dSKent Overstreet 237180fb49dSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 238180fb49dSKent Overstreet &c->dev_usage_journal_res, u64s * nr); 239180fb49dSKent Overstreet } 240180fb49dSKent Overstreet 2411c6fdbd8SKent Overstreet /* Filesystem RO/RW: */ 2421c6fdbd8SKent Overstreet 2431c6fdbd8SKent Overstreet /* 2441c6fdbd8SKent Overstreet * For startup/shutdown of RW stuff, the dependencies are: 2451c6fdbd8SKent Overstreet * 2461c6fdbd8SKent Overstreet * - foreground writes depend on copygc and rebalance (to free up space) 2471c6fdbd8SKent Overstreet * 2481c6fdbd8SKent Overstreet * - copygc and rebalance depend on mark and sweep gc (they actually probably 2491c6fdbd8SKent Overstreet * don't because they either reserve ahead of time or don't block if 2501c6fdbd8SKent Overstreet * allocations fail, but allocations can require mark and sweep gc to run 2511c6fdbd8SKent Overstreet * because of generation number wraparound) 2521c6fdbd8SKent Overstreet * 2531c6fdbd8SKent Overstreet * - all of the above depends on the allocator threads 2541c6fdbd8SKent Overstreet * 2551c6fdbd8SKent Overstreet * - allocator depends on the journal (when it rewrites prios and gens) 2561c6fdbd8SKent Overstreet */ 2571c6fdbd8SKent Overstreet 2581c6fdbd8SKent Overstreet static void __bch2_fs_read_only(struct bch_fs *c) 2591c6fdbd8SKent Overstreet { 2609fea2274SKent Overstreet unsigned clean_passes = 0; 261c0960603SKent Overstreet u64 seq = 0; 2621c6fdbd8SKent Overstreet 263b40901b0SKent Overstreet bch2_fs_ec_stop(c); 264b40901b0SKent Overstreet bch2_open_buckets_stop(c, NULL, true); 2651c6fdbd8SKent Overstreet bch2_rebalance_stop(c); 266e6d11615SKent Overstreet bch2_copygc_stop(c); 2671c6fdbd8SKent Overstreet bch2_gc_thread_stop(c); 268b40901b0SKent Overstreet bch2_fs_ec_flush(c); 2691c6fdbd8SKent Overstreet 27083ec519aSKent Overstreet bch_verbose(c, "flushing journal and stopping allocators, journal seq %llu", 27183ec519aSKent Overstreet journal_cur_seq(&c->journal)); 2721c6fdbd8SKent Overstreet 273039fc4c5SKent Overstreet do { 274039fc4c5SKent Overstreet clean_passes++; 275039fc4c5SKent Overstreet 276c0960603SKent Overstreet if (bch2_btree_interior_updates_flush(c) || 277c0960603SKent Overstreet bch2_journal_flush_all_pins(&c->journal) || 278c0960603SKent Overstreet bch2_btree_flush_all_writes(c) || 279c0960603SKent Overstreet seq != atomic64_read(&c->journal.seq)) { 280c0960603SKent Overstreet seq = atomic64_read(&c->journal.seq); 281039fc4c5SKent Overstreet clean_passes = 0; 282039fc4c5SKent Overstreet } 283d5f70c1fSKent Overstreet } while (clean_passes < 2); 284c0960603SKent Overstreet 28583ec519aSKent Overstreet bch_verbose(c, "flushing journal and stopping allocators complete, journal seq %llu", 28683ec519aSKent Overstreet journal_cur_seq(&c->journal)); 2872340fd9dSKent Overstreet 288c0960603SKent Overstreet if (test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags) && 2893c471b65SKent Overstreet !test_bit(BCH_FS_emergency_ro, &c->flags)) 2903c471b65SKent Overstreet set_bit(BCH_FS_clean_shutdown, &c->flags); 2919802ff48SKent Overstreet 2921c6fdbd8SKent Overstreet bch2_fs_journal_stop(&c->journal); 2931c6fdbd8SKent Overstreet 2949802ff48SKent Overstreet bch_info(c, "%sshutdown complete, journal seq %llu", 2959802ff48SKent Overstreet test_bit(BCH_FS_clean_shutdown, &c->flags) ? "" : "un", 2969802ff48SKent Overstreet c->journal.seq_ondisk); 2979802ff48SKent Overstreet 2981c6fdbd8SKent Overstreet /* 2991c6fdbd8SKent Overstreet * After stopping journal: 3001c6fdbd8SKent Overstreet */ 3019fea2274SKent Overstreet for_each_member_device(c, ca) 3021c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 3031c6fdbd8SKent Overstreet } 3041c6fdbd8SKent Overstreet 305d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 3061c6fdbd8SKent Overstreet static void bch2_writes_disabled(struct percpu_ref *writes) 3071c6fdbd8SKent Overstreet { 3081c6fdbd8SKent Overstreet struct bch_fs *c = container_of(writes, struct bch_fs, writes); 3091c6fdbd8SKent Overstreet 3103c471b65SKent Overstreet set_bit(BCH_FS_write_disable_complete, &c->flags); 311d94189adSKent Overstreet wake_up(&bch2_read_only_wait); 3121c6fdbd8SKent Overstreet } 313d94189adSKent Overstreet #endif 3141c6fdbd8SKent Overstreet 3151c6fdbd8SKent Overstreet void bch2_fs_read_only(struct bch_fs *c) 3161c6fdbd8SKent Overstreet { 3173c471b65SKent Overstreet if (!test_bit(BCH_FS_rw, &c->flags)) { 3189ae28f82SKent Overstreet bch2_journal_reclaim_stop(&c->journal); 3191c6fdbd8SKent Overstreet return; 320134915f3SKent Overstreet } 3211c6fdbd8SKent Overstreet 3223c471b65SKent Overstreet BUG_ON(test_bit(BCH_FS_write_disable_complete, &c->flags)); 3231c6fdbd8SKent Overstreet 324e7f7ddedSKent Overstreet bch_verbose(c, "going read-only"); 325e7f7ddedSKent Overstreet 3261c6fdbd8SKent Overstreet /* 3271c6fdbd8SKent Overstreet * Block new foreground-end write operations from starting - any new 3281c6fdbd8SKent Overstreet * writes will return -EROFS: 3291c6fdbd8SKent Overstreet */ 3303c471b65SKent Overstreet set_bit(BCH_FS_going_ro, &c->flags); 331d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 3321c6fdbd8SKent Overstreet percpu_ref_kill(&c->writes); 333d94189adSKent Overstreet #else 334d94189adSKent Overstreet for (unsigned i = 0; i < BCH_WRITE_REF_NR; i++) 335d94189adSKent Overstreet bch2_write_ref_put(c, i); 336d94189adSKent Overstreet #endif 3371c6fdbd8SKent Overstreet 3381c6fdbd8SKent Overstreet /* 3391c6fdbd8SKent Overstreet * If we're not doing an emergency shutdown, we want to wait on 3401c6fdbd8SKent Overstreet * outstanding writes to complete so they don't see spurious errors due 3411c6fdbd8SKent Overstreet * to shutting down the allocator: 3421c6fdbd8SKent Overstreet * 3431c6fdbd8SKent Overstreet * If we are doing an emergency shutdown outstanding writes may 3441c6fdbd8SKent Overstreet * hang until we shutdown the allocator so we don't want to wait 3451c6fdbd8SKent Overstreet * on outstanding writes before shutting everything down - but 3461c6fdbd8SKent Overstreet * we do need to wait on them before returning and signalling 3471c6fdbd8SKent Overstreet * that going RO is complete: 3481c6fdbd8SKent Overstreet */ 349d94189adSKent Overstreet wait_event(bch2_read_only_wait, 3503c471b65SKent Overstreet test_bit(BCH_FS_write_disable_complete, &c->flags) || 3513c471b65SKent Overstreet test_bit(BCH_FS_emergency_ro, &c->flags)); 3521c6fdbd8SKent Overstreet 3533c471b65SKent Overstreet bool writes_disabled = test_bit(BCH_FS_write_disable_complete, &c->flags); 354e7f7ddedSKent Overstreet if (writes_disabled) 355e7f7ddedSKent Overstreet bch_verbose(c, "finished waiting for writes to stop"); 356e7f7ddedSKent Overstreet 3571c6fdbd8SKent Overstreet __bch2_fs_read_only(c); 3581c6fdbd8SKent Overstreet 359d94189adSKent Overstreet wait_event(bch2_read_only_wait, 3603c471b65SKent Overstreet test_bit(BCH_FS_write_disable_complete, &c->flags)); 3611c6fdbd8SKent Overstreet 362e7f7ddedSKent Overstreet if (!writes_disabled) 363e7f7ddedSKent Overstreet bch_verbose(c, "finished waiting for writes to stop"); 364e7f7ddedSKent Overstreet 3653c471b65SKent Overstreet clear_bit(BCH_FS_write_disable_complete, &c->flags); 3663c471b65SKent Overstreet clear_bit(BCH_FS_going_ro, &c->flags); 3673c471b65SKent Overstreet clear_bit(BCH_FS_rw, &c->flags); 3681c6fdbd8SKent Overstreet 3691c6fdbd8SKent Overstreet if (!bch2_journal_error(&c->journal) && 3703c471b65SKent Overstreet !test_bit(BCH_FS_error, &c->flags) && 3713c471b65SKent Overstreet !test_bit(BCH_FS_emergency_ro, &c->flags) && 3723c471b65SKent Overstreet test_bit(BCH_FS_started, &c->flags) && 3733c471b65SKent Overstreet test_bit(BCH_FS_clean_shutdown, &c->flags) && 37413c1e583SKent Overstreet c->recovery_pass_done >= BCH_RECOVERY_PASS_journal_replay) { 3757724664fSKent Overstreet BUG_ON(c->journal.last_empty_seq != journal_cur_seq(&c->journal)); 3767724664fSKent Overstreet BUG_ON(atomic_read(&c->btree_cache.dirty)); 3777724664fSKent Overstreet BUG_ON(atomic_long_read(&c->btree_key_cache.nr_dirty)); 37809caeabeSKent Overstreet BUG_ON(c->btree_write_buffer.inc.keys.nr); 37909caeabeSKent Overstreet BUG_ON(c->btree_write_buffer.flushing.keys.nr); 3807724664fSKent Overstreet 381b2930396SKent Overstreet bch_verbose(c, "marking filesystem clean"); 382134915f3SKent Overstreet bch2_fs_mark_clean(c); 383e7f7ddedSKent Overstreet } else { 384e7f7ddedSKent Overstreet bch_verbose(c, "done going read-only, filesystem not clean"); 385b2930396SKent Overstreet } 3861c6fdbd8SKent Overstreet } 3871c6fdbd8SKent Overstreet 3881c6fdbd8SKent Overstreet static void bch2_fs_read_only_work(struct work_struct *work) 3891c6fdbd8SKent Overstreet { 3901c6fdbd8SKent Overstreet struct bch_fs *c = 3911c6fdbd8SKent Overstreet container_of(work, struct bch_fs, read_only_work); 3921c6fdbd8SKent Overstreet 3931ada1606SKent Overstreet down_write(&c->state_lock); 3941c6fdbd8SKent Overstreet bch2_fs_read_only(c); 3951ada1606SKent Overstreet up_write(&c->state_lock); 3961c6fdbd8SKent Overstreet } 3971c6fdbd8SKent Overstreet 3981c6fdbd8SKent Overstreet static void bch2_fs_read_only_async(struct bch_fs *c) 3991c6fdbd8SKent Overstreet { 4001c6fdbd8SKent Overstreet queue_work(system_long_wq, &c->read_only_work); 4011c6fdbd8SKent Overstreet } 4021c6fdbd8SKent Overstreet 4031c6fdbd8SKent Overstreet bool bch2_fs_emergency_read_only(struct bch_fs *c) 4041c6fdbd8SKent Overstreet { 4053c471b65SKent Overstreet bool ret = !test_and_set_bit(BCH_FS_emergency_ro, &c->flags); 4061c6fdbd8SKent Overstreet 4071c6fdbd8SKent Overstreet bch2_journal_halt(&c->journal); 4089f115ce9SKent Overstreet bch2_fs_read_only_async(c); 4091c6fdbd8SKent Overstreet 410d94189adSKent Overstreet wake_up(&bch2_read_only_wait); 4111c6fdbd8SKent Overstreet return ret; 4121c6fdbd8SKent Overstreet } 4131c6fdbd8SKent Overstreet 414134915f3SKent Overstreet static int bch2_fs_read_write_late(struct bch_fs *c) 4151c6fdbd8SKent Overstreet { 416134915f3SKent Overstreet int ret; 4171c6fdbd8SKent Overstreet 418ea28c867SKent Overstreet /* 419ea28c867SKent Overstreet * Data move operations can't run until after check_snapshots has 420ea28c867SKent Overstreet * completed, and bch2_snapshot_is_ancestor() is available. 421ea28c867SKent Overstreet * 422ea28c867SKent Overstreet * Ideally we'd start copygc/rebalance earlier instead of waiting for 423ea28c867SKent Overstreet * all of recovery/fsck to complete: 424ea28c867SKent Overstreet */ 425ea28c867SKent Overstreet ret = bch2_copygc_start(c); 426ea28c867SKent Overstreet if (ret) { 427ea28c867SKent Overstreet bch_err(c, "error starting copygc thread"); 428ea28c867SKent Overstreet return ret; 429ea28c867SKent Overstreet } 430ea28c867SKent Overstreet 431134915f3SKent Overstreet ret = bch2_rebalance_start(c); 432134915f3SKent Overstreet if (ret) { 433134915f3SKent Overstreet bch_err(c, "error starting rebalance thread"); 434134915f3SKent Overstreet return ret; 435134915f3SKent Overstreet } 436134915f3SKent Overstreet 437134915f3SKent Overstreet return 0; 438134915f3SKent Overstreet } 439134915f3SKent Overstreet 440e731d466SKent Overstreet static int __bch2_fs_read_write(struct bch_fs *c, bool early) 441134915f3SKent Overstreet { 442134915f3SKent Overstreet int ret; 443134915f3SKent Overstreet 4443c471b65SKent Overstreet if (test_bit(BCH_FS_initial_gc_unfixed, &c->flags)) { 445aae15aafSKent Overstreet bch_err(c, "cannot go rw, unfixed btree errors"); 4467c50140fSKent Overstreet return -BCH_ERR_erofs_unfixed_errors; 447aae15aafSKent Overstreet } 448aae15aafSKent Overstreet 4493c471b65SKent Overstreet if (test_bit(BCH_FS_rw, &c->flags)) 450134915f3SKent Overstreet return 0; 451134915f3SKent Overstreet 4522c944fa1SKent Overstreet bch_info(c, "going read-write"); 4532c944fa1SKent Overstreet 454f5d26fa3SKent Overstreet ret = bch2_sb_members_v2_init(c); 4553f7b9713SHunter Shaffer if (ret) 4563f7b9713SHunter Shaffer goto err; 4573f7b9713SHunter Shaffer 458134915f3SKent Overstreet ret = bch2_fs_mark_dirty(c); 459134915f3SKent Overstreet if (ret) 460134915f3SKent Overstreet goto err; 4611c6fdbd8SKent Overstreet 4623c471b65SKent Overstreet clear_bit(BCH_FS_clean_shutdown, &c->flags); 4632340fd9dSKent Overstreet 464b9004e85SKent Overstreet /* 465b9004e85SKent Overstreet * First journal write must be a flush write: after a clean shutdown we 466b9004e85SKent Overstreet * don't read the journal, so the first journal write may end up 467b9004e85SKent Overstreet * overwriting whatever was there previously, and there must always be 468b9004e85SKent Overstreet * at least one non-flush write in the journal or recovery will fail: 469b9004e85SKent Overstreet */ 470b9004e85SKent Overstreet set_bit(JOURNAL_NEED_FLUSH_WRITE, &c->journal.flags); 471497c982fSKent Overstreet set_bit(JOURNAL_RUNNING, &c->journal.flags); 472b9004e85SKent Overstreet 4739fea2274SKent Overstreet for_each_rw_member(c, ca) 4741c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 4751c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 4761c6fdbd8SKent Overstreet 4773c471b65SKent Overstreet set_bit(BCH_FS_rw, &c->flags); 4783c471b65SKent Overstreet set_bit(BCH_FS_was_rw, &c->flags); 479468035caSKent Overstreet 480468035caSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 481468035caSKent Overstreet percpu_ref_reinit(&c->writes); 482468035caSKent Overstreet #else 4839fea2274SKent Overstreet for (unsigned i = 0; i < BCH_WRITE_REF_NR; i++) { 484468035caSKent Overstreet BUG_ON(atomic_long_read(&c->writes[i])); 485468035caSKent Overstreet atomic_long_inc(&c->writes[i]); 486468035caSKent Overstreet } 487468035caSKent Overstreet #endif 488468035caSKent Overstreet 4895f5c7466SKent Overstreet ret = bch2_gc_thread_start(c); 4905f5c7466SKent Overstreet if (ret) { 4915f5c7466SKent Overstreet bch_err(c, "error starting gc thread"); 4925f5c7466SKent Overstreet return ret; 4935f5c7466SKent Overstreet } 4945f5c7466SKent Overstreet 495197763a7SBrian Foster ret = bch2_journal_reclaim_start(&c->journal); 496197763a7SBrian Foster if (ret) 497197763a7SBrian Foster goto err; 498197763a7SBrian Foster 499134915f3SKent Overstreet if (!early) { 500134915f3SKent Overstreet ret = bch2_fs_read_write_late(c); 501134915f3SKent Overstreet if (ret) 5021c6fdbd8SKent Overstreet goto err; 5031c6fdbd8SKent Overstreet } 5041c6fdbd8SKent Overstreet 505dd81a060SKent Overstreet bch2_do_discards(c); 506dd81a060SKent Overstreet bch2_do_invalidates(c); 507dd81a060SKent Overstreet bch2_do_stripe_deletes(c); 508a1f26d70SKent Overstreet bch2_do_pending_node_rewrites(c); 509134915f3SKent Overstreet return 0; 5101c6fdbd8SKent Overstreet err: 5113c471b65SKent Overstreet if (test_bit(BCH_FS_rw, &c->flags)) 512468035caSKent Overstreet bch2_fs_read_only(c); 513468035caSKent Overstreet else 5141c6fdbd8SKent Overstreet __bch2_fs_read_only(c); 515134915f3SKent Overstreet return ret; 516134915f3SKent Overstreet } 517134915f3SKent Overstreet 518134915f3SKent Overstreet int bch2_fs_read_write(struct bch_fs *c) 519134915f3SKent Overstreet { 52013c1e583SKent Overstreet if (c->opts.recovery_pass_last && 52113c1e583SKent Overstreet c->opts.recovery_pass_last < BCH_RECOVERY_PASS_journal_replay) 52262719cf3SKent Overstreet return -BCH_ERR_erofs_norecovery; 52362719cf3SKent Overstreet 52462719cf3SKent Overstreet if (c->opts.nochanges) 52562719cf3SKent Overstreet return -BCH_ERR_erofs_nochanges; 52662719cf3SKent Overstreet 527134915f3SKent Overstreet return __bch2_fs_read_write(c, false); 528134915f3SKent Overstreet } 529134915f3SKent Overstreet 530134915f3SKent Overstreet int bch2_fs_read_write_early(struct bch_fs *c) 531134915f3SKent Overstreet { 532134915f3SKent Overstreet lockdep_assert_held(&c->state_lock); 533134915f3SKent Overstreet 534134915f3SKent Overstreet return __bch2_fs_read_write(c, true); 5351c6fdbd8SKent Overstreet } 5361c6fdbd8SKent Overstreet 5371c6fdbd8SKent Overstreet /* Filesystem startup/shutdown: */ 5381c6fdbd8SKent Overstreet 539d5e4dcc2SKent Overstreet static void __bch2_fs_free(struct bch_fs *c) 5401c6fdbd8SKent Overstreet { 5411c6fdbd8SKent Overstreet unsigned i; 5421c6fdbd8SKent Overstreet 5431c6fdbd8SKent Overstreet for (i = 0; i < BCH_TIME_STAT_NR; i++) 5441c6fdbd8SKent Overstreet bch2_time_stats_exit(&c->times[i]); 5451c6fdbd8SKent Overstreet 5464409b808SKent Overstreet bch2_find_btree_nodes_exit(&c->found_btree_nodes); 547a1f26d70SKent Overstreet bch2_free_pending_node_rewrites(c); 548ec438ac5SKent Overstreet bch2_fs_allocator_background_exit(c); 549f5d26fa3SKent Overstreet bch2_fs_sb_errors_exit(c); 550104c6974SDaniel Hill bch2_fs_counters_exit(c); 55114b393eeSKent Overstreet bch2_fs_snapshots_exit(c); 5521c6fdbd8SKent Overstreet bch2_fs_quota_exit(c); 553dbbfca9fSKent Overstreet bch2_fs_fs_io_direct_exit(c); 554dbbfca9fSKent Overstreet bch2_fs_fs_io_buffered_exit(c); 5551c6fdbd8SKent Overstreet bch2_fs_fsio_exit(c); 556cd575ddfSKent Overstreet bch2_fs_ec_exit(c); 5571c6fdbd8SKent Overstreet bch2_fs_encryption_exit(c); 5581e3b4098SKent Overstreet bch2_fs_nocow_locking_exit(c); 5591809b8cbSKent Overstreet bch2_fs_io_write_exit(c); 5601809b8cbSKent Overstreet bch2_fs_io_read_exit(c); 56121aec962SKent Overstreet bch2_fs_buckets_waiting_for_journal_exit(c); 562c823c339SKent Overstreet bch2_fs_btree_interior_update_exit(c); 56336e9d698SKent Overstreet bch2_fs_btree_iter_exit(c); 5642ca88e5aSKent Overstreet bch2_fs_btree_key_cache_exit(&c->btree_key_cache); 5651c6fdbd8SKent Overstreet bch2_fs_btree_cache_exit(c); 5669620c3ecSKent Overstreet bch2_fs_replicas_exit(c); 5671c6fdbd8SKent Overstreet bch2_fs_journal_exit(&c->journal); 5681c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[WRITE]); 5691c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[READ]); 5701c6fdbd8SKent Overstreet bch2_fs_compress_exit(c); 5718a443d3eSKent Overstreet bch2_journal_keys_put_initial(c); 5724409b808SKent Overstreet bch2_find_btree_nodes_exit(&c->found_btree_nodes); 5738a443d3eSKent Overstreet BUG_ON(atomic_read(&c->journal_keys.ref)); 574920e69bcSKent Overstreet bch2_fs_btree_write_buffer_exit(c); 5759166b41dSKent Overstreet percpu_free_rwsem(&c->mark_lock); 576*feb25553SKent Overstreet EBUG_ON(percpu_u64_get(c->online_reserved)); 5775e82a9a1SKent Overstreet free_percpu(c->online_reserved); 5781a21bf98SKent Overstreet 579faa6cb6cSKent Overstreet darray_exit(&c->btree_roots_extra); 5805663a415SKent Overstreet free_percpu(c->pcpu); 58135189e09SKent Overstreet mempool_exit(&c->large_bkey_pool); 5821c6fdbd8SKent Overstreet mempool_exit(&c->btree_bounce_pool); 5831c6fdbd8SKent Overstreet bioset_exit(&c->btree_bio); 5841c6fdbd8SKent Overstreet mempool_exit(&c->fill_iter); 585d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 5861c6fdbd8SKent Overstreet percpu_ref_exit(&c->writes); 587d94189adSKent Overstreet #endif 5881c6fdbd8SKent Overstreet kfree(rcu_dereference_protected(c->disk_groups, 1)); 5891dd7f9d9SKent Overstreet kfree(c->journal_seq_blacklist_table); 590b5e8a699SKent Overstreet kfree(c->unused_inode_hints); 5911c6fdbd8SKent Overstreet 5928bff9875SBrian Foster if (c->write_ref_wq) 5938bff9875SBrian Foster destroy_workqueue(c->write_ref_wq); 594731bdd2eSKent Overstreet if (c->io_complete_wq) 595731bdd2eSKent Overstreet destroy_workqueue(c->io_complete_wq); 5961c6fdbd8SKent Overstreet if (c->copygc_wq) 5971c6fdbd8SKent Overstreet destroy_workqueue(c->copygc_wq); 5989f1833caSKent Overstreet if (c->btree_io_complete_wq) 5999f1833caSKent Overstreet destroy_workqueue(c->btree_io_complete_wq); 600731bdd2eSKent Overstreet if (c->btree_update_wq) 601731bdd2eSKent Overstreet destroy_workqueue(c->btree_update_wq); 6021c6fdbd8SKent Overstreet 6039d8022dbSKent Overstreet bch2_free_super(&c->disk_sb); 604cb6fc943SKent Overstreet kvfree(c); 6051c6fdbd8SKent Overstreet module_put(THIS_MODULE); 6061c6fdbd8SKent Overstreet } 6071c6fdbd8SKent Overstreet 6081c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *kobj) 6091c6fdbd8SKent Overstreet { 6101c6fdbd8SKent Overstreet struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); 6111c6fdbd8SKent Overstreet 612d5e4dcc2SKent Overstreet __bch2_fs_free(c); 6131c6fdbd8SKent Overstreet } 6141c6fdbd8SKent Overstreet 615d5e4dcc2SKent Overstreet void __bch2_fs_stop(struct bch_fs *c) 6161c6fdbd8SKent Overstreet { 617af1c6871SKent Overstreet bch_verbose(c, "shutting down"); 618af1c6871SKent Overstreet 6193c471b65SKent Overstreet set_bit(BCH_FS_stopping, &c->flags); 6201dd7f9d9SKent Overstreet 6219b6e2f1eSKent Overstreet cancel_work_sync(&c->journal_seq_blacklist_gc_work); 6229b6e2f1eSKent Overstreet 6231ada1606SKent Overstreet down_write(&c->state_lock); 624883f1a7cSKent Overstreet bch2_fs_read_only(c); 6251ada1606SKent Overstreet up_write(&c->state_lock); 626883f1a7cSKent Overstreet 6279fea2274SKent Overstreet for_each_member_device(c, ca) 6281c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs && 6291c6fdbd8SKent Overstreet ca->disk_sb.bdev) 6301c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 6311c6fdbd8SKent Overstreet 6321c6fdbd8SKent Overstreet if (c->kobj.state_in_sysfs) 6331c6fdbd8SKent Overstreet kobject_del(&c->kobj); 6341c6fdbd8SKent Overstreet 6351c6fdbd8SKent Overstreet bch2_fs_debug_exit(c); 6361c6fdbd8SKent Overstreet bch2_fs_chardev_exit(c); 6371c6fdbd8SKent Overstreet 63863508b75SKent Overstreet bch2_ro_ref_put(c); 63963508b75SKent Overstreet wait_event(c->ro_ref_wait, !refcount_read(&c->ro_ref)); 64063508b75SKent Overstreet 641104c6974SDaniel Hill kobject_put(&c->counters_kobj); 6421c6fdbd8SKent Overstreet kobject_put(&c->time_stats); 6431c6fdbd8SKent Overstreet kobject_put(&c->opts_dir); 6441c6fdbd8SKent Overstreet kobject_put(&c->internal); 6451c6fdbd8SKent Overstreet 6461c6fdbd8SKent Overstreet /* btree prefetch might have kicked off reads in the background: */ 6471c6fdbd8SKent Overstreet bch2_btree_flush_all_reads(c); 6481c6fdbd8SKent Overstreet 6499fea2274SKent Overstreet for_each_member_device(c, ca) 6501c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 6511c6fdbd8SKent Overstreet 6521c6fdbd8SKent Overstreet cancel_work_sync(&c->read_only_work); 653d5e4dcc2SKent Overstreet } 6541c6fdbd8SKent Overstreet 655d5e4dcc2SKent Overstreet void bch2_fs_free(struct bch_fs *c) 656d5e4dcc2SKent Overstreet { 657d5e4dcc2SKent Overstreet unsigned i; 658d5e4dcc2SKent Overstreet 659d5e4dcc2SKent Overstreet mutex_lock(&bch_fs_list_lock); 660d5e4dcc2SKent Overstreet list_del(&c->list); 661d5e4dcc2SKent Overstreet mutex_unlock(&bch_fs_list_lock); 662d5e4dcc2SKent Overstreet 663d5e4dcc2SKent Overstreet closure_sync(&c->cl); 664d5e4dcc2SKent Overstreet closure_debug_destroy(&c->cl); 665d5e4dcc2SKent Overstreet 666d5e4dcc2SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) { 667d5e4dcc2SKent Overstreet struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true); 668d5e4dcc2SKent Overstreet 669d5e4dcc2SKent Overstreet if (ca) { 670d5e4dcc2SKent Overstreet bch2_free_super(&ca->disk_sb); 671d5e4dcc2SKent Overstreet bch2_dev_free(ca); 672d5e4dcc2SKent Overstreet } 673d5e4dcc2SKent Overstreet } 6741c6fdbd8SKent Overstreet 675af1c6871SKent Overstreet bch_verbose(c, "shutdown complete"); 676af1c6871SKent Overstreet 6771c6fdbd8SKent Overstreet kobject_put(&c->kobj); 6781c6fdbd8SKent Overstreet } 6791c6fdbd8SKent Overstreet 680d5e4dcc2SKent Overstreet void bch2_fs_stop(struct bch_fs *c) 681d5e4dcc2SKent Overstreet { 682d5e4dcc2SKent Overstreet __bch2_fs_stop(c); 683d5e4dcc2SKent Overstreet bch2_fs_free(c); 684d5e4dcc2SKent Overstreet } 685d5e4dcc2SKent Overstreet 686e2b60560SKent Overstreet static int bch2_fs_online(struct bch_fs *c) 6871c6fdbd8SKent Overstreet { 688e2b60560SKent Overstreet int ret = 0; 6891c6fdbd8SKent Overstreet 6901c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 6911c6fdbd8SKent Overstreet 692e2b60560SKent Overstreet if (__bch2_uuid_to_fs(c->sb.uuid)) { 693e2b60560SKent Overstreet bch_err(c, "filesystem UUID already open"); 694e2b60560SKent Overstreet return -EINVAL; 695e2b60560SKent Overstreet } 6961c6fdbd8SKent Overstreet 6971c6fdbd8SKent Overstreet ret = bch2_fs_chardev_init(c); 698e2b60560SKent Overstreet if (ret) { 699e2b60560SKent Overstreet bch_err(c, "error creating character device"); 700e2b60560SKent Overstreet return ret; 701e2b60560SKent Overstreet } 7021c6fdbd8SKent Overstreet 7031c6fdbd8SKent Overstreet bch2_fs_debug_init(c); 7041c6fdbd8SKent Overstreet 705e2b60560SKent Overstreet ret = kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ?: 706e2b60560SKent Overstreet kobject_add(&c->internal, &c->kobj, "internal") ?: 707e2b60560SKent Overstreet kobject_add(&c->opts_dir, &c->kobj, "options") ?: 708066a2646SKent Overstreet #ifndef CONFIG_BCACHEFS_NO_LATENCY_ACCT 709e2b60560SKent Overstreet kobject_add(&c->time_stats, &c->kobj, "time_stats") ?: 710066a2646SKent Overstreet #endif 711104c6974SDaniel Hill kobject_add(&c->counters_kobj, &c->kobj, "counters") ?: 712e2b60560SKent Overstreet bch2_opts_create_sysfs_files(&c->opts_dir); 713e2b60560SKent Overstreet if (ret) { 714e2b60560SKent Overstreet bch_err(c, "error creating sysfs objects"); 715e2b60560SKent Overstreet return ret; 716e2b60560SKent Overstreet } 7171c6fdbd8SKent Overstreet 7181ada1606SKent Overstreet down_write(&c->state_lock); 7191c6fdbd8SKent Overstreet 7209fea2274SKent Overstreet for_each_member_device(c, ca) { 721e2b60560SKent Overstreet ret = bch2_dev_sysfs_online(c, ca); 722e2b60560SKent Overstreet if (ret) { 723e2b60560SKent Overstreet bch_err(c, "error creating sysfs objects"); 7243a402c8dSKent Overstreet percpu_ref_put(&ca->ref); 7251c6fdbd8SKent Overstreet goto err; 7263a402c8dSKent Overstreet } 727e2b60560SKent Overstreet } 7281c6fdbd8SKent Overstreet 729e2b60560SKent Overstreet BUG_ON(!list_empty(&c->list)); 7301c6fdbd8SKent Overstreet list_add(&c->list, &bch_fs_list); 7311c6fdbd8SKent Overstreet err: 7321ada1606SKent Overstreet up_write(&c->state_lock); 733e2b60560SKent Overstreet return ret; 7341c6fdbd8SKent Overstreet } 7351c6fdbd8SKent Overstreet 7361c6fdbd8SKent Overstreet static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts) 7371c6fdbd8SKent Overstreet { 7381c6fdbd8SKent Overstreet struct bch_fs *c; 739401ec4dbSKent Overstreet struct printbuf name = PRINTBUF; 740ecf37a4aSKent Overstreet unsigned i, iter_size; 7417be9ab63SChris Webb int ret = 0; 7421c6fdbd8SKent Overstreet 743cb6fc943SKent Overstreet c = kvmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO); 7447be9ab63SChris Webb if (!c) { 74565d48e35SKent Overstreet c = ERR_PTR(-BCH_ERR_ENOMEM_fs_alloc); 7461c6fdbd8SKent Overstreet goto out; 7477be9ab63SChris Webb } 7481c6fdbd8SKent Overstreet 74996f37eabSKent Overstreet c->stdio = (void *)(unsigned long) opts.stdio; 7502b41226dSKent Overstreet 7511c6fdbd8SKent Overstreet __module_get(THIS_MODULE); 7521c6fdbd8SKent Overstreet 753505b7a4cSKent Overstreet closure_init(&c->cl, NULL); 754505b7a4cSKent Overstreet 755505b7a4cSKent Overstreet c->kobj.kset = bcachefs_kset; 756505b7a4cSKent Overstreet kobject_init(&c->kobj, &bch2_fs_ktype); 757505b7a4cSKent Overstreet kobject_init(&c->internal, &bch2_fs_internal_ktype); 758505b7a4cSKent Overstreet kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype); 759505b7a4cSKent Overstreet kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype); 760104c6974SDaniel Hill kobject_init(&c->counters_kobj, &bch2_fs_counters_ktype); 761505b7a4cSKent Overstreet 7621c6fdbd8SKent Overstreet c->minor = -1; 7631c6fdbd8SKent Overstreet c->disk_sb.fs_sb = true; 7641c6fdbd8SKent Overstreet 7651ada1606SKent Overstreet init_rwsem(&c->state_lock); 7661c6fdbd8SKent Overstreet mutex_init(&c->sb_lock); 7671c6fdbd8SKent Overstreet mutex_init(&c->replicas_gc_lock); 7681c6fdbd8SKent Overstreet mutex_init(&c->btree_root_lock); 7691c6fdbd8SKent Overstreet INIT_WORK(&c->read_only_work, bch2_fs_read_only_work); 7701c6fdbd8SKent Overstreet 77163508b75SKent Overstreet refcount_set(&c->ro_ref, 1); 77263508b75SKent Overstreet init_waitqueue_head(&c->ro_ref_wait); 773267b801fSKent Overstreet sema_init(&c->online_fsck_mutex, 1); 77463508b75SKent Overstreet 7751c6fdbd8SKent Overstreet init_rwsem(&c->gc_lock); 776c45c8667SKent Overstreet mutex_init(&c->gc_gens_lock); 7778a443d3eSKent Overstreet atomic_set(&c->journal_keys.ref, 1); 7788a443d3eSKent Overstreet c->journal_keys.initial_ref_held = true; 7791c6fdbd8SKent Overstreet 7801c6fdbd8SKent Overstreet for (i = 0; i < BCH_TIME_STAT_NR; i++) 7811c6fdbd8SKent Overstreet bch2_time_stats_init(&c->times[i]); 7821c6fdbd8SKent Overstreet 783e6d11615SKent Overstreet bch2_fs_copygc_init(c); 7842ca88e5aSKent Overstreet bch2_fs_btree_key_cache_init_early(&c->btree_key_cache); 78550a8a732SThomas Bertschinger bch2_fs_btree_iter_init_early(c); 78665db6049SKent Overstreet bch2_fs_btree_interior_update_init_early(c); 787b092daddSKent Overstreet bch2_fs_allocator_background_init(c); 788b092daddSKent Overstreet bch2_fs_allocator_foreground_init(c); 7891c6fdbd8SKent Overstreet bch2_fs_rebalance_init(c); 7901c6fdbd8SKent Overstreet bch2_fs_quota_init(c); 79184c72755SKent Overstreet bch2_fs_ec_init_early(c); 792b9fa375bSKent Overstreet bch2_fs_move_init(c); 793f5d26fa3SKent Overstreet bch2_fs_sb_errors_init_early(c); 7941c6fdbd8SKent Overstreet 7951c6fdbd8SKent Overstreet INIT_LIST_HEAD(&c->list); 7961c6fdbd8SKent Overstreet 7974d8100daSKent Overstreet mutex_init(&c->usage_scratch_lock); 7984d8100daSKent Overstreet 7991c6fdbd8SKent Overstreet mutex_init(&c->bio_bounce_pages_lock); 80014b393eeSKent Overstreet mutex_init(&c->snapshot_table_lock); 80137fad949SKent Overstreet init_rwsem(&c->snapshot_create_lock); 8021c6fdbd8SKent Overstreet 8031c6fdbd8SKent Overstreet spin_lock_init(&c->btree_write_error_lock); 8041c6fdbd8SKent Overstreet 8059b6e2f1eSKent Overstreet INIT_WORK(&c->journal_seq_blacklist_gc_work, 8069b6e2f1eSKent Overstreet bch2_blacklist_entries_gc); 8079b6e2f1eSKent Overstreet 8085b593ee1SKent Overstreet INIT_LIST_HEAD(&c->journal_iters); 809f1d786a0SKent Overstreet 810f5d26fa3SKent Overstreet INIT_LIST_HEAD(&c->fsck_error_msgs); 811f5d26fa3SKent Overstreet mutex_init(&c->fsck_error_msgs_lock); 8121c6fdbd8SKent Overstreet 8131c6fdbd8SKent Overstreet seqcount_init(&c->gc_pos_lock); 8141c6fdbd8SKent Overstreet 8155e82a9a1SKent Overstreet seqcount_init(&c->usage_lock); 8165e82a9a1SKent Overstreet 817ef1b2092SKent Overstreet sema_init(&c->io_in_flight, 128); 818ef1b2092SKent Overstreet 8199edbcc72SKent Overstreet INIT_LIST_HEAD(&c->vfs_inodes_list); 8209edbcc72SKent Overstreet mutex_init(&c->vfs_inodes_lock); 8219edbcc72SKent Overstreet 8221c6fdbd8SKent Overstreet c->copy_gc_enabled = 1; 8231c6fdbd8SKent Overstreet c->rebalance.enabled = 1; 8241c6fdbd8SKent Overstreet c->promote_whole_extents = true; 8251c6fdbd8SKent Overstreet 826991ba021SKent Overstreet c->journal.flush_write_time = &c->times[BCH_TIME_journal_flush_write]; 827991ba021SKent Overstreet c->journal.noflush_write_time = &c->times[BCH_TIME_journal_noflush_write]; 8281c6fdbd8SKent Overstreet c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq]; 8291c6fdbd8SKent Overstreet 8301c6fdbd8SKent Overstreet bch2_fs_btree_cache_init_early(&c->btree_cache); 8311c6fdbd8SKent Overstreet 832fca1223cSKent Overstreet mutex_init(&c->sectors_available_lock); 833fca1223cSKent Overstreet 834e2b60560SKent Overstreet ret = percpu_init_rwsem(&c->mark_lock); 835e2b60560SKent Overstreet if (ret) 83673e6ab95SKent Overstreet goto err; 83773e6ab95SKent Overstreet 8381c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 839e2b60560SKent Overstreet ret = bch2_sb_to_fs(c, sb); 8401c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 841e2b60560SKent Overstreet 842e2b60560SKent Overstreet if (ret) 8431c6fdbd8SKent Overstreet goto err; 8441c6fdbd8SKent Overstreet 845401ec4dbSKent Overstreet pr_uuid(&name, c->sb.user_uuid.b); 84665d48e35SKent Overstreet ret = name.allocation_failure ? -BCH_ERR_ENOMEM_fs_name_alloc : 0; 847401ec4dbSKent Overstreet if (ret) 848401ec4dbSKent Overstreet goto err; 8491c6fdbd8SKent Overstreet 850f8cdf65bSLi Zetao strscpy(c->name, name.buf, sizeof(c->name)); 851f8cdf65bSLi Zetao printbuf_exit(&name); 852f8cdf65bSLi Zetao 8532430e72fSKent Overstreet /* Compat: */ 85473bd774dSKent Overstreet if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && 8552430e72fSKent Overstreet !BCH_SB_JOURNAL_FLUSH_DELAY(sb)) 8562430e72fSKent Overstreet SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000); 8572430e72fSKent Overstreet 85873bd774dSKent Overstreet if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && 8592430e72fSKent Overstreet !BCH_SB_JOURNAL_RECLAIM_DELAY(sb)) 8602430e72fSKent Overstreet SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 100); 8612430e72fSKent Overstreet 8621c6fdbd8SKent Overstreet c->opts = bch2_opts_default; 8638244f320SKent Overstreet ret = bch2_opts_from_sb(&c->opts, sb); 8648244f320SKent Overstreet if (ret) 8658244f320SKent Overstreet goto err; 8668244f320SKent Overstreet 8671c6fdbd8SKent Overstreet bch2_opts_apply(&c->opts, opts); 8681c6fdbd8SKent Overstreet 8697c8f6f98SKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_alloc; 8707c8f6f98SKent Overstreet if (c->opts.inodes_use_key_cache) 8717c8f6f98SKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_inodes; 872aaad530aSKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_logged_ops; 8737c8f6f98SKent Overstreet 8748244f320SKent Overstreet c->block_bits = ilog2(block_sectors(c)); 8751c6fdbd8SKent Overstreet c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c); 8761c6fdbd8SKent Overstreet 8777be9ab63SChris Webb if (bch2_fs_init_fault("fs_alloc")) { 878e2b60560SKent Overstreet bch_err(c, "fs_alloc fault injected"); 879e2b60560SKent Overstreet ret = -EFAULT; 8801c6fdbd8SKent Overstreet goto err; 8817be9ab63SChris Webb } 8821c6fdbd8SKent Overstreet 883ae2f17d5SKent Overstreet iter_size = sizeof(struct sort_iter) + 8841c6fdbd8SKent Overstreet (btree_blocks(c) + 1) * 2 * 885ae2f17d5SKent Overstreet sizeof(struct sort_iter_set); 8861c6fdbd8SKent Overstreet 887b5e8a699SKent Overstreet c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus())); 888b5e8a699SKent Overstreet 889731bdd2eSKent Overstreet if (!(c->btree_update_wq = alloc_workqueue("bcachefs", 8906b83aee8SKent Overstreet WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_UNBOUND, 512)) || 8919f1833caSKent Overstreet !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io", 8926b83aee8SKent Overstreet WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) || 8932f33ece9SKent Overstreet !(c->copygc_wq = alloc_workqueue("bcachefs_copygc", 8946b83aee8SKent Overstreet WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) || 895731bdd2eSKent Overstreet !(c->io_complete_wq = alloc_workqueue("bcachefs_io", 8966b83aee8SKent Overstreet WQ_HIGHPRI|WQ_FREEZABLE|WQ_MEM_RECLAIM, 512)) || 8978bff9875SBrian Foster !(c->write_ref_wq = alloc_workqueue("bcachefs_write_ref", 8988bff9875SBrian Foster WQ_FREEZABLE, 0)) || 899d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 900134915f3SKent Overstreet percpu_ref_init(&c->writes, bch2_writes_disabled, 901134915f3SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 902d94189adSKent Overstreet #endif 9031c6fdbd8SKent Overstreet mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) || 9041c6fdbd8SKent Overstreet bioset_init(&c->btree_bio, 1, 9051c6fdbd8SKent Overstreet max(offsetof(struct btree_read_bio, bio), 9061c6fdbd8SKent Overstreet offsetof(struct btree_write_bio, wbio.bio)), 9071c6fdbd8SKent Overstreet BIOSET_NEED_BVECS) || 9085663a415SKent Overstreet !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) || 9095e82a9a1SKent Overstreet !(c->online_reserved = alloc_percpu(u64)) || 910cb6fc943SKent Overstreet mempool_init_kvmalloc_pool(&c->btree_bounce_pool, 1, 911ec4edd7bSKent Overstreet c->opts.btree_node_size) || 91235189e09SKent Overstreet mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) || 913b5e8a699SKent Overstreet !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits, 914e2b60560SKent Overstreet sizeof(u64), GFP_KERNEL))) { 91565d48e35SKent Overstreet ret = -BCH_ERR_ENOMEM_fs_other_alloc; 9167be9ab63SChris Webb goto err; 9177be9ab63SChris Webb } 9187be9ab63SChris Webb 919f3b8403eSKent Overstreet ret = bch2_fs_counters_init(c) ?: 920f5d26fa3SKent Overstreet bch2_fs_sb_errors_init(c) ?: 921f3b8403eSKent Overstreet bch2_io_clock_init(&c->io_clock[READ]) ?: 922e2b60560SKent Overstreet bch2_io_clock_init(&c->io_clock[WRITE]) ?: 923e2b60560SKent Overstreet bch2_fs_journal_init(&c->journal) ?: 924e2b60560SKent Overstreet bch2_fs_replicas_init(c) ?: 925e2b60560SKent Overstreet bch2_fs_btree_cache_init(c) ?: 926e2b60560SKent Overstreet bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?: 927e2b60560SKent Overstreet bch2_fs_btree_iter_init(c) ?: 928e2b60560SKent Overstreet bch2_fs_btree_interior_update_init(c) ?: 9299b688da3SKent Overstreet bch2_fs_buckets_waiting_for_journal_init(c) ?: 930920e69bcSKent Overstreet bch2_fs_btree_write_buffer_init(c) ?: 931e2b60560SKent Overstreet bch2_fs_subvolumes_init(c) ?: 9321809b8cbSKent Overstreet bch2_fs_io_read_init(c) ?: 9331809b8cbSKent Overstreet bch2_fs_io_write_init(c) ?: 934350175bfSKent Overstreet bch2_fs_nocow_locking_init(c) ?: 935e2b60560SKent Overstreet bch2_fs_encryption_init(c) ?: 936e2b60560SKent Overstreet bch2_fs_compress_init(c) ?: 937e2b60560SKent Overstreet bch2_fs_ec_init(c) ?: 938dbbfca9fSKent Overstreet bch2_fs_fsio_init(c) ?: 939867c1fe0SDan Carpenter bch2_fs_fs_io_buffered_init(c) ?: 940dbbfca9fSKent Overstreet bch2_fs_fs_io_direct_init(c); 9417be9ab63SChris Webb if (ret) 9421c6fdbd8SKent Overstreet goto err; 9431c6fdbd8SKent Overstreet 9441c6fdbd8SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) 9452f724563SKent Overstreet if (bch2_member_exists(c->disk_sb.sb, i) && 9467be9ab63SChris Webb bch2_dev_alloc(c, i)) { 947e2b60560SKent Overstreet ret = -EEXIST; 9481c6fdbd8SKent Overstreet goto err; 9497be9ab63SChris Webb } 9501c6fdbd8SKent Overstreet 9514b8f89afSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 9524b8f89afSKent Overstreet &c->btree_root_journal_res, 9534b8f89afSKent Overstreet BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX)); 9544b8f89afSKent Overstreet bch2_dev_usage_journal_reserve(c); 9554b8f89afSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 9564b8f89afSKent Overstreet &c->clock_journal_res, 9574b8f89afSKent Overstreet (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2); 9584b8f89afSKent Overstreet 9591c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 960e2b60560SKent Overstreet ret = bch2_fs_online(c); 9611c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 962e2b60560SKent Overstreet 963e2b60560SKent Overstreet if (ret) 9641c6fdbd8SKent Overstreet goto err; 9651c6fdbd8SKent Overstreet out: 9661c6fdbd8SKent Overstreet return c; 9671c6fdbd8SKent Overstreet err: 9681c6fdbd8SKent Overstreet bch2_fs_free(c); 9697be9ab63SChris Webb c = ERR_PTR(ret); 9701c6fdbd8SKent Overstreet goto out; 9711c6fdbd8SKent Overstreet } 9721c6fdbd8SKent Overstreet 973619f5beeSKent Overstreet noinline_for_stack 974619f5beeSKent Overstreet static void print_mount_opts(struct bch_fs *c) 975619f5beeSKent Overstreet { 976619f5beeSKent Overstreet enum bch_opt_id i; 977fa8e94faSKent Overstreet struct printbuf p = PRINTBUF; 978619f5beeSKent Overstreet bool first = true; 979619f5beeSKent Overstreet 980ef1634f0SKent Overstreet prt_str(&p, "mounting version "); 981e3804b55SKent Overstreet bch2_version_to_text(&p, c->sb.version); 98260573ff5SKent Overstreet 983619f5beeSKent Overstreet if (c->opts.read_only) { 98460573ff5SKent Overstreet prt_str(&p, " opts="); 985619f5beeSKent Overstreet first = false; 98660573ff5SKent Overstreet prt_printf(&p, "ro"); 987619f5beeSKent Overstreet } 988619f5beeSKent Overstreet 989619f5beeSKent Overstreet for (i = 0; i < bch2_opts_nr; i++) { 990619f5beeSKent Overstreet const struct bch_option *opt = &bch2_opt_table[i]; 991619f5beeSKent Overstreet u64 v = bch2_opt_get_by_id(&c->opts, i); 992619f5beeSKent Overstreet 9938244f320SKent Overstreet if (!(opt->flags & OPT_MOUNT)) 994619f5beeSKent Overstreet continue; 995619f5beeSKent Overstreet 996619f5beeSKent Overstreet if (v == bch2_opt_get_by_id(&bch2_opts_default, i)) 997619f5beeSKent Overstreet continue; 998619f5beeSKent Overstreet 99960573ff5SKent Overstreet prt_str(&p, first ? " opts=" : ","); 1000619f5beeSKent Overstreet first = false; 10015521b1dfSKent Overstreet bch2_opt_to_text(&p, c, c->disk_sb.sb, opt, v, OPT_SHOW_MOUNT_STYLE); 1002619f5beeSKent Overstreet } 1003619f5beeSKent Overstreet 100460573ff5SKent Overstreet bch_info(c, "%s", p.buf); 1005fa8e94faSKent Overstreet printbuf_exit(&p); 1006619f5beeSKent Overstreet } 1007619f5beeSKent Overstreet 1008619f5beeSKent Overstreet int bch2_fs_start(struct bch_fs *c) 10091c6fdbd8SKent Overstreet { 1010a420eea6STim Schlueter time64_t now = ktime_get_real_seconds(); 101178c0b75cSKent Overstreet int ret; 10121c6fdbd8SKent Overstreet 1013ef1634f0SKent Overstreet print_mount_opts(c); 1014ef1634f0SKent Overstreet 10151ada1606SKent Overstreet down_write(&c->state_lock); 10161c6fdbd8SKent Overstreet 10173c471b65SKent Overstreet BUG_ON(test_bit(BCH_FS_started, &c->flags)); 10181c6fdbd8SKent Overstreet 10191c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 10201c6fdbd8SKent Overstreet 1021f5d26fa3SKent Overstreet ret = bch2_sb_members_v2_init(c); 10223f7b9713SHunter Shaffer if (ret) { 10233f7b9713SHunter Shaffer mutex_unlock(&c->sb_lock); 10243f7b9713SHunter Shaffer goto err; 10253f7b9713SHunter Shaffer } 10263f7b9713SHunter Shaffer 10279fea2274SKent Overstreet for_each_online_member(c, ca) 10289fea2274SKent Overstreet bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = cpu_to_le64(now); 10291c6fdbd8SKent Overstreet 10300a34c058SKent Overstreet struct bch_sb_field_ext *ext = 10310a34c058SKent Overstreet bch2_sb_field_get_minsize(&c->disk_sb, ext, sizeof(*ext) / sizeof(u64)); 10321c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 10331c6fdbd8SKent Overstreet 10340a34c058SKent Overstreet if (!ext) { 10350a34c058SKent Overstreet bch_err(c, "insufficient space in superblock for sb_field_ext"); 10360a34c058SKent Overstreet ret = -BCH_ERR_ENOSPC_sb; 10370a34c058SKent Overstreet goto err; 10380a34c058SKent Overstreet } 10390a34c058SKent Overstreet 10409fea2274SKent Overstreet for_each_rw_member(c, ca) 10411c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 10421c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 10431c6fdbd8SKent Overstreet 10441c6fdbd8SKent Overstreet ret = BCH_SB_INITIALIZED(c->disk_sb.sb) 10451c6fdbd8SKent Overstreet ? bch2_fs_recovery(c) 10461c6fdbd8SKent Overstreet : bch2_fs_initialize(c); 10471c6fdbd8SKent Overstreet if (ret) 10481c6fdbd8SKent Overstreet goto err; 10491c6fdbd8SKent Overstreet 1050cd575ddfSKent Overstreet ret = bch2_opts_check_may_set(c); 1051cd575ddfSKent Overstreet if (ret) 1052cd575ddfSKent Overstreet goto err; 1053cd575ddfSKent Overstreet 1054e2b60560SKent Overstreet if (bch2_fs_init_fault("fs_start")) { 1055e2b60560SKent Overstreet bch_err(c, "fs_start fault injected"); 105678c0b75cSKent Overstreet ret = -EINVAL; 10571c6fdbd8SKent Overstreet goto err; 1058e2b60560SKent Overstreet } 10591c6fdbd8SKent Overstreet 10603c471b65SKent Overstreet set_bit(BCH_FS_started, &c->flags); 1061a9310ab0SKent Overstreet 106262719cf3SKent Overstreet if (c->opts.read_only) { 10631c6fdbd8SKent Overstreet bch2_fs_read_only(c); 10641c6fdbd8SKent Overstreet } else { 10653c471b65SKent Overstreet ret = !test_bit(BCH_FS_rw, &c->flags) 1066619f5beeSKent Overstreet ? bch2_fs_read_write(c) 1067619f5beeSKent Overstreet : bch2_fs_read_write_late(c); 1068619f5beeSKent Overstreet if (ret) 10691c6fdbd8SKent Overstreet goto err; 10701c6fdbd8SKent Overstreet } 10711c6fdbd8SKent Overstreet 1072619f5beeSKent Overstreet ret = 0; 1073e34ec13aSKent Overstreet err: 1074e34ec13aSKent Overstreet if (ret) 1075e34ec13aSKent Overstreet bch_err_msg(c, ret, "starting filesystem"); 1076e34ec13aSKent Overstreet else 1077e34ec13aSKent Overstreet bch_verbose(c, "done starting filesystem"); 10781ada1606SKent Overstreet up_write(&c->state_lock); 1079619f5beeSKent Overstreet return ret; 10801c6fdbd8SKent Overstreet } 10811c6fdbd8SKent Overstreet 108278c0b75cSKent Overstreet static int bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c) 10831c6fdbd8SKent Overstreet { 10841241df58SHunter Shaffer struct bch_member m = bch2_sb_member_get(sb, sb->dev_idx); 10851c6fdbd8SKent Overstreet 10868244f320SKent Overstreet if (le16_to_cpu(sb->block_size) != block_sectors(c)) 108778c0b75cSKent Overstreet return -BCH_ERR_mismatched_block_size; 10881c6fdbd8SKent Overstreet 10891241df58SHunter Shaffer if (le16_to_cpu(m.bucket_size) < 10901c6fdbd8SKent Overstreet BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb)) 109178c0b75cSKent Overstreet return -BCH_ERR_bucket_size_too_small; 10921c6fdbd8SKent Overstreet 109378c0b75cSKent Overstreet return 0; 10941c6fdbd8SKent Overstreet } 10951c6fdbd8SKent Overstreet 10960d529663SKent Overstreet static int bch2_dev_in_fs(struct bch_sb_handle *fs, 10972f300f09SKent Overstreet struct bch_sb_handle *sb, 10982f300f09SKent Overstreet struct bch_opts *opts) 10991c6fdbd8SKent Overstreet { 11000d529663SKent Overstreet if (fs == sb) 11010d529663SKent Overstreet return 0; 11021c6fdbd8SKent Overstreet 11030d529663SKent Overstreet if (!uuid_equal(&fs->sb->uuid, &sb->sb->uuid)) 110478c0b75cSKent Overstreet return -BCH_ERR_device_not_a_member_of_filesystem; 11051c6fdbd8SKent Overstreet 11062f724563SKent Overstreet if (!bch2_member_exists(fs->sb, sb->sb->dev_idx)) 110778c0b75cSKent Overstreet return -BCH_ERR_device_has_been_removed; 11081c6fdbd8SKent Overstreet 11090d529663SKent Overstreet if (fs->sb->block_size != sb->sb->block_size) 111078c0b75cSKent Overstreet return -BCH_ERR_mismatched_block_size; 11111c6fdbd8SKent Overstreet 11120d529663SKent Overstreet if (le16_to_cpu(fs->sb->version) < bcachefs_metadata_version_member_seq || 11130d529663SKent Overstreet le16_to_cpu(sb->sb->version) < bcachefs_metadata_version_member_seq) 11140d529663SKent Overstreet return 0; 11150d529663SKent Overstreet 11160d529663SKent Overstreet if (fs->sb->seq == sb->sb->seq && 11170d529663SKent Overstreet fs->sb->write_time != sb->sb->write_time) { 11180d529663SKent Overstreet struct printbuf buf = PRINTBUF; 11190d529663SKent Overstreet 11201f5af5fcSKent Overstreet prt_str(&buf, "Split brain detected between "); 11211f5af5fcSKent Overstreet prt_bdevname(&buf, sb->bdev); 11221f5af5fcSKent Overstreet prt_str(&buf, " and "); 11231f5af5fcSKent Overstreet prt_bdevname(&buf, fs->bdev); 11241f5af5fcSKent Overstreet prt_char(&buf, ':'); 11250d529663SKent Overstreet prt_newline(&buf); 11260d529663SKent Overstreet prt_printf(&buf, "seq=%llu but write_time different, got", le64_to_cpu(sb->sb->seq)); 11270d529663SKent Overstreet prt_newline(&buf); 11280d529663SKent Overstreet 11291f5af5fcSKent Overstreet prt_bdevname(&buf, fs->bdev); 11301f5af5fcSKent Overstreet prt_char(&buf, ' '); 11310d529663SKent Overstreet bch2_prt_datetime(&buf, le64_to_cpu(fs->sb->write_time));; 11320d529663SKent Overstreet prt_newline(&buf); 11330d529663SKent Overstreet 11341f5af5fcSKent Overstreet prt_bdevname(&buf, sb->bdev); 11351f5af5fcSKent Overstreet prt_char(&buf, ' '); 11360d529663SKent Overstreet bch2_prt_datetime(&buf, le64_to_cpu(sb->sb->write_time));; 11370d529663SKent Overstreet prt_newline(&buf); 11380d529663SKent Overstreet 11392f300f09SKent Overstreet if (!opts->no_splitbrain_check) 11400d529663SKent Overstreet prt_printf(&buf, "Not using older sb"); 11410d529663SKent Overstreet 11420d529663SKent Overstreet pr_err("%s", buf.buf); 11430d529663SKent Overstreet printbuf_exit(&buf); 11442f300f09SKent Overstreet 11452f300f09SKent Overstreet if (!opts->no_splitbrain_check) 11460d529663SKent Overstreet return -BCH_ERR_device_splitbrain; 11470d529663SKent Overstreet } 11480d529663SKent Overstreet 11490d529663SKent Overstreet struct bch_member m = bch2_sb_member_get(fs->sb, sb->sb->dev_idx); 11500d529663SKent Overstreet u64 seq_from_fs = le64_to_cpu(m.seq); 11510d529663SKent Overstreet u64 seq_from_member = le64_to_cpu(sb->sb->seq); 11520d529663SKent Overstreet 11530d529663SKent Overstreet if (seq_from_fs && seq_from_fs < seq_from_member) { 11541f5af5fcSKent Overstreet struct printbuf buf = PRINTBUF; 11551f5af5fcSKent Overstreet 11561f5af5fcSKent Overstreet prt_str(&buf, "Split brain detected between "); 11571f5af5fcSKent Overstreet prt_bdevname(&buf, sb->bdev); 11581f5af5fcSKent Overstreet prt_str(&buf, " and "); 11591f5af5fcSKent Overstreet prt_bdevname(&buf, fs->bdev); 11601f5af5fcSKent Overstreet prt_char(&buf, ':'); 11611f5af5fcSKent Overstreet prt_newline(&buf); 11621f5af5fcSKent Overstreet 11631f5af5fcSKent Overstreet prt_bdevname(&buf, fs->bdev); 11641f5af5fcSKent Overstreet prt_str(&buf, " believes seq of "); 11651f5af5fcSKent Overstreet prt_bdevname(&buf, sb->bdev); 11661f5af5fcSKent Overstreet prt_printf(&buf, " to be %llu, but ", seq_from_fs); 11671f5af5fcSKent Overstreet prt_bdevname(&buf, sb->bdev); 11681f5af5fcSKent Overstreet prt_printf(&buf, " has %llu\n", seq_from_member); 11692f300f09SKent Overstreet 11702f300f09SKent Overstreet if (!opts->no_splitbrain_check) { 11711f5af5fcSKent Overstreet prt_str(&buf, "Not using "); 11721f5af5fcSKent Overstreet prt_bdevname(&buf, sb->bdev); 11732f300f09SKent Overstreet } 11741f5af5fcSKent Overstreet 11751f5af5fcSKent Overstreet pr_err("%s", buf.buf); 11761f5af5fcSKent Overstreet printbuf_exit(&buf); 11772f300f09SKent Overstreet 11782f300f09SKent Overstreet if (!opts->no_splitbrain_check) 11790d529663SKent Overstreet return -BCH_ERR_device_splitbrain; 11800d529663SKent Overstreet } 11810d529663SKent Overstreet 118278c0b75cSKent Overstreet return 0; 11831c6fdbd8SKent Overstreet } 11841c6fdbd8SKent Overstreet 11851c6fdbd8SKent Overstreet /* Device startup/shutdown: */ 11861c6fdbd8SKent Overstreet 11871c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *kobj) 11881c6fdbd8SKent Overstreet { 11891c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); 11901c6fdbd8SKent Overstreet 11911c6fdbd8SKent Overstreet kfree(ca); 11921c6fdbd8SKent Overstreet } 11931c6fdbd8SKent Overstreet 11941c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *ca) 11951c6fdbd8SKent Overstreet { 11961c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 11971c6fdbd8SKent Overstreet 11981c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs && 11991c6fdbd8SKent Overstreet ca->disk_sb.bdev) 12001c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 12011c6fdbd8SKent Overstreet 12021c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) 12031c6fdbd8SKent Overstreet kobject_del(&ca->kobj); 12041c6fdbd8SKent Overstreet 12051c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 12061c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 12071c6fdbd8SKent Overstreet 12081c6fdbd8SKent Overstreet free_percpu(ca->io_done); 12091c6fdbd8SKent Overstreet bioset_exit(&ca->replica_set); 12101c6fdbd8SKent Overstreet bch2_dev_buckets_free(ca); 1211d1170ce5SKent Overstreet free_page((unsigned long) ca->sb_read_scratch); 12121c6fdbd8SKent Overstreet 1213273960b8SDarrick J. Wong bch2_time_stats_quantiles_exit(&ca->io_latency[WRITE]); 1214273960b8SDarrick J. Wong bch2_time_stats_quantiles_exit(&ca->io_latency[READ]); 12151c6fdbd8SKent Overstreet 12161c6fdbd8SKent Overstreet percpu_ref_exit(&ca->io_ref); 12171c6fdbd8SKent Overstreet percpu_ref_exit(&ca->ref); 12181c6fdbd8SKent Overstreet kobject_put(&ca->kobj); 12191c6fdbd8SKent Overstreet } 12201c6fdbd8SKent Overstreet 12211c6fdbd8SKent Overstreet static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) 12221c6fdbd8SKent Overstreet { 12231c6fdbd8SKent Overstreet 12241c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 12251c6fdbd8SKent Overstreet 12261c6fdbd8SKent Overstreet if (percpu_ref_is_zero(&ca->io_ref)) 12271c6fdbd8SKent Overstreet return; 12281c6fdbd8SKent Overstreet 12291c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 12301c6fdbd8SKent Overstreet 12311c6fdbd8SKent Overstreet reinit_completion(&ca->io_ref_completion); 12321c6fdbd8SKent Overstreet percpu_ref_kill(&ca->io_ref); 12331c6fdbd8SKent Overstreet wait_for_completion(&ca->io_ref_completion); 12341c6fdbd8SKent Overstreet 12351c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) { 12361c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 12371c6fdbd8SKent Overstreet sysfs_remove_link(&ca->kobj, "block"); 12381c6fdbd8SKent Overstreet } 12391c6fdbd8SKent Overstreet 12401c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 12411c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 12421c6fdbd8SKent Overstreet } 12431c6fdbd8SKent Overstreet 12441c6fdbd8SKent Overstreet static void bch2_dev_ref_complete(struct percpu_ref *ref) 12451c6fdbd8SKent Overstreet { 12461c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, ref); 12471c6fdbd8SKent Overstreet 12481c6fdbd8SKent Overstreet complete(&ca->ref_completion); 12491c6fdbd8SKent Overstreet } 12501c6fdbd8SKent Overstreet 12511c6fdbd8SKent Overstreet static void bch2_dev_io_ref_complete(struct percpu_ref *ref) 12521c6fdbd8SKent Overstreet { 12531c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref); 12541c6fdbd8SKent Overstreet 12551c6fdbd8SKent Overstreet complete(&ca->io_ref_completion); 12561c6fdbd8SKent Overstreet } 12571c6fdbd8SKent Overstreet 12581c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) 12591c6fdbd8SKent Overstreet { 12601c6fdbd8SKent Overstreet int ret; 12611c6fdbd8SKent Overstreet 12621c6fdbd8SKent Overstreet if (!c->kobj.state_in_sysfs) 12631c6fdbd8SKent Overstreet return 0; 12641c6fdbd8SKent Overstreet 12651c6fdbd8SKent Overstreet if (!ca->kobj.state_in_sysfs) { 12661c6fdbd8SKent Overstreet ret = kobject_add(&ca->kobj, &c->kobj, 12671c6fdbd8SKent Overstreet "dev-%u", ca->dev_idx); 12681c6fdbd8SKent Overstreet if (ret) 12691c6fdbd8SKent Overstreet return ret; 12701c6fdbd8SKent Overstreet } 12711c6fdbd8SKent Overstreet 12721c6fdbd8SKent Overstreet if (ca->disk_sb.bdev) { 12731c6fdbd8SKent Overstreet struct kobject *block = bdev_kobj(ca->disk_sb.bdev); 12741c6fdbd8SKent Overstreet 12751c6fdbd8SKent Overstreet ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); 12761c6fdbd8SKent Overstreet if (ret) 12771c6fdbd8SKent Overstreet return ret; 12781c6fdbd8SKent Overstreet 12791c6fdbd8SKent Overstreet ret = sysfs_create_link(&ca->kobj, block, "block"); 12801c6fdbd8SKent Overstreet if (ret) 12811c6fdbd8SKent Overstreet return ret; 12821c6fdbd8SKent Overstreet } 12831c6fdbd8SKent Overstreet 12841c6fdbd8SKent Overstreet return 0; 12851c6fdbd8SKent Overstreet } 12861c6fdbd8SKent Overstreet 12871c6fdbd8SKent Overstreet static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c, 12881c6fdbd8SKent Overstreet struct bch_member *member) 12891c6fdbd8SKent Overstreet { 12901c6fdbd8SKent Overstreet struct bch_dev *ca; 129194119eebSKent Overstreet unsigned i; 12921c6fdbd8SKent Overstreet 12931c6fdbd8SKent Overstreet ca = kzalloc(sizeof(*ca), GFP_KERNEL); 12941c6fdbd8SKent Overstreet if (!ca) 12951c6fdbd8SKent Overstreet return NULL; 12961c6fdbd8SKent Overstreet 12971c6fdbd8SKent Overstreet kobject_init(&ca->kobj, &bch2_dev_ktype); 12981c6fdbd8SKent Overstreet init_completion(&ca->ref_completion); 12991c6fdbd8SKent Overstreet init_completion(&ca->io_ref_completion); 13001c6fdbd8SKent Overstreet 13011c6fdbd8SKent Overstreet init_rwsem(&ca->bucket_lock); 13021c6fdbd8SKent Overstreet 13031c6fdbd8SKent Overstreet INIT_WORK(&ca->io_error_work, bch2_io_error_work); 13041c6fdbd8SKent Overstreet 1305273960b8SDarrick J. Wong bch2_time_stats_quantiles_init(&ca->io_latency[READ]); 1306273960b8SDarrick J. Wong bch2_time_stats_quantiles_init(&ca->io_latency[WRITE]); 13071c6fdbd8SKent Overstreet 13081c6fdbd8SKent Overstreet ca->mi = bch2_mi_to_cpu(member); 130994119eebSKent Overstreet 131094119eebSKent Overstreet for (i = 0; i < ARRAY_SIZE(member->errors); i++) 131194119eebSKent Overstreet atomic64_set(&ca->errors[i], le64_to_cpu(member->errors[i])); 131294119eebSKent Overstreet 13131c6fdbd8SKent Overstreet ca->uuid = member->uuid; 13141c6fdbd8SKent Overstreet 1315f25d8215SKent Overstreet ca->nr_btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE, 1316f25d8215SKent Overstreet ca->mi.bucket_size / btree_sectors(c)); 1317f25d8215SKent Overstreet 13181c6fdbd8SKent Overstreet if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, 13191c6fdbd8SKent Overstreet 0, GFP_KERNEL) || 13201c6fdbd8SKent Overstreet percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete, 13211c6fdbd8SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 1322d1170ce5SKent Overstreet !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) || 13231c6fdbd8SKent Overstreet bch2_dev_buckets_alloc(c, ca) || 13241c6fdbd8SKent Overstreet bioset_init(&ca->replica_set, 4, 13251c6fdbd8SKent Overstreet offsetof(struct bch_write_bio, bio), 0) || 13261c6fdbd8SKent Overstreet !(ca->io_done = alloc_percpu(*ca->io_done))) 13271c6fdbd8SKent Overstreet goto err; 13281c6fdbd8SKent Overstreet 13291c6fdbd8SKent Overstreet return ca; 13301c6fdbd8SKent Overstreet err: 13311c6fdbd8SKent Overstreet bch2_dev_free(ca); 13321c6fdbd8SKent Overstreet return NULL; 13331c6fdbd8SKent Overstreet } 13341c6fdbd8SKent Overstreet 13351c6fdbd8SKent Overstreet static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, 13361c6fdbd8SKent Overstreet unsigned dev_idx) 13371c6fdbd8SKent Overstreet { 13381c6fdbd8SKent Overstreet ca->dev_idx = dev_idx; 13391c6fdbd8SKent Overstreet __set_bit(ca->dev_idx, ca->self.d); 13401c6fdbd8SKent Overstreet scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); 13411c6fdbd8SKent Overstreet 13421c6fdbd8SKent Overstreet ca->fs = c; 13431c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], ca); 13441c6fdbd8SKent Overstreet 13451c6fdbd8SKent Overstreet if (bch2_dev_sysfs_online(c, ca)) 13461c6fdbd8SKent Overstreet pr_warn("error creating sysfs objects"); 13471c6fdbd8SKent Overstreet } 13481c6fdbd8SKent Overstreet 13491c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx) 13501c6fdbd8SKent Overstreet { 13511241df58SHunter Shaffer struct bch_member member = bch2_sb_member_get(c->disk_sb.sb, dev_idx); 13521c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 13531c6fdbd8SKent Overstreet int ret = 0; 13541c6fdbd8SKent Overstreet 13551c6fdbd8SKent Overstreet if (bch2_fs_init_fault("dev_alloc")) 13561c6fdbd8SKent Overstreet goto err; 13571c6fdbd8SKent Overstreet 13581241df58SHunter Shaffer ca = __bch2_dev_alloc(c, &member); 13591c6fdbd8SKent Overstreet if (!ca) 13601c6fdbd8SKent Overstreet goto err; 13611c6fdbd8SKent Overstreet 1362220d2062SKent Overstreet ca->fs = c; 1363220d2062SKent Overstreet 13641c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 13651c6fdbd8SKent Overstreet return ret; 13661c6fdbd8SKent Overstreet err: 13671c6fdbd8SKent Overstreet if (ca) 13681c6fdbd8SKent Overstreet bch2_dev_free(ca); 1369c8b4534dSKent Overstreet return -BCH_ERR_ENOMEM_dev_alloc; 13701c6fdbd8SKent Overstreet } 13711c6fdbd8SKent Overstreet 13721c6fdbd8SKent Overstreet static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb) 13731c6fdbd8SKent Overstreet { 13741c6fdbd8SKent Overstreet unsigned ret; 13751c6fdbd8SKent Overstreet 13761c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca)) { 13771c6fdbd8SKent Overstreet bch_err(ca, "already have device online in slot %u", 13781c6fdbd8SKent Overstreet sb->sb->dev_idx); 137978c0b75cSKent Overstreet return -BCH_ERR_device_already_online; 13801c6fdbd8SKent Overstreet } 13811c6fdbd8SKent Overstreet 13821c6fdbd8SKent Overstreet if (get_capacity(sb->bdev->bd_disk) < 13831c6fdbd8SKent Overstreet ca->mi.bucket_size * ca->mi.nbuckets) { 13841c6fdbd8SKent Overstreet bch_err(ca, "cannot online: device too small"); 138578c0b75cSKent Overstreet return -BCH_ERR_device_size_too_small; 13861c6fdbd8SKent Overstreet } 13871c6fdbd8SKent Overstreet 13881c6fdbd8SKent Overstreet BUG_ON(!percpu_ref_is_zero(&ca->io_ref)); 13891c6fdbd8SKent Overstreet 13901c6fdbd8SKent Overstreet ret = bch2_dev_journal_init(ca, sb->sb); 13911c6fdbd8SKent Overstreet if (ret) 13921c6fdbd8SKent Overstreet return ret; 13931c6fdbd8SKent Overstreet 13941c6fdbd8SKent Overstreet /* Commit: */ 13951c6fdbd8SKent Overstreet ca->disk_sb = *sb; 13961c6fdbd8SKent Overstreet memset(sb, 0, sizeof(*sb)); 13971c6fdbd8SKent Overstreet 1398eacb2574SKent Overstreet ca->dev = ca->disk_sb.bdev->bd_dev; 1399eacb2574SKent Overstreet 14001c6fdbd8SKent Overstreet percpu_ref_reinit(&ca->io_ref); 14011c6fdbd8SKent Overstreet 14021c6fdbd8SKent Overstreet return 0; 14031c6fdbd8SKent Overstreet } 14041c6fdbd8SKent Overstreet 14051c6fdbd8SKent Overstreet static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb) 14061c6fdbd8SKent Overstreet { 14071c6fdbd8SKent Overstreet struct bch_dev *ca; 14081c6fdbd8SKent Overstreet int ret; 14091c6fdbd8SKent Overstreet 14101c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 14111c6fdbd8SKent Overstreet 14121c6fdbd8SKent Overstreet if (le64_to_cpu(sb->sb->seq) > 14131c6fdbd8SKent Overstreet le64_to_cpu(c->disk_sb.sb->seq)) 14141c6fdbd8SKent Overstreet bch2_sb_to_fs(c, sb->sb); 14151c6fdbd8SKent Overstreet 14162f724563SKent Overstreet BUG_ON(!bch2_dev_exists(c, sb->sb->dev_idx)); 14171c6fdbd8SKent Overstreet 14182f724563SKent Overstreet ca = bch2_dev_locked(c, sb->sb->dev_idx); 14191c6fdbd8SKent Overstreet 14201c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, sb); 14211c6fdbd8SKent Overstreet if (ret) 14221c6fdbd8SKent Overstreet return ret; 14231c6fdbd8SKent Overstreet 14241c6fdbd8SKent Overstreet bch2_dev_sysfs_online(c, ca); 14251c6fdbd8SKent Overstreet 14261f5af5fcSKent Overstreet struct printbuf name = PRINTBUF; 14271f5af5fcSKent Overstreet prt_bdevname(&name, ca->disk_sb.bdev); 14281f5af5fcSKent Overstreet 14291c6fdbd8SKent Overstreet if (c->sb.nr_devices == 1) 1430e28b0359SKees Cook strscpy(c->name, name.buf, sizeof(c->name)); 1431e28b0359SKees Cook strscpy(ca->name, name.buf, sizeof(ca->name)); 14321f5af5fcSKent Overstreet 14331f5af5fcSKent Overstreet printbuf_exit(&name); 14341c6fdbd8SKent Overstreet 14351c6fdbd8SKent Overstreet rebalance_wakeup(c); 14361c6fdbd8SKent Overstreet return 0; 14371c6fdbd8SKent Overstreet } 14381c6fdbd8SKent Overstreet 14391c6fdbd8SKent Overstreet /* Device management: */ 14401c6fdbd8SKent Overstreet 14411c6fdbd8SKent Overstreet /* 14421c6fdbd8SKent Overstreet * Note: this function is also used by the error paths - when a particular 14431c6fdbd8SKent Overstreet * device sees an error, we call it to determine whether we can just set the 14441c6fdbd8SKent Overstreet * device RO, or - if this function returns false - we'll set the whole 14451c6fdbd8SKent Overstreet * filesystem RO: 14461c6fdbd8SKent Overstreet * 14471c6fdbd8SKent Overstreet * XXX: maybe we should be more explicit about whether we're changing state 14481c6fdbd8SKent Overstreet * because we got an error or what have you? 14491c6fdbd8SKent Overstreet */ 14501c6fdbd8SKent Overstreet bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, 14511c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 14521c6fdbd8SKent Overstreet { 14531c6fdbd8SKent Overstreet struct bch_devs_mask new_online_devs; 14549fea2274SKent Overstreet int nr_rw = 0, required; 14551c6fdbd8SKent Overstreet 14561c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 14571c6fdbd8SKent Overstreet 14581c6fdbd8SKent Overstreet switch (new_state) { 14592436cb9fSKent Overstreet case BCH_MEMBER_STATE_rw: 14601c6fdbd8SKent Overstreet return true; 14612436cb9fSKent Overstreet case BCH_MEMBER_STATE_ro: 14622436cb9fSKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_rw) 14631c6fdbd8SKent Overstreet return true; 14641c6fdbd8SKent Overstreet 14651c6fdbd8SKent Overstreet /* do we have enough devices to write to? */ 14669fea2274SKent Overstreet for_each_member_device(c, ca2) 14671c6fdbd8SKent Overstreet if (ca2 != ca) 14682436cb9fSKent Overstreet nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw; 14691c6fdbd8SKent Overstreet 14701c6fdbd8SKent Overstreet required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED) 14711c6fdbd8SKent Overstreet ? c->opts.metadata_replicas 14724e074475SKent Overstreet : metadata_replicas_required(c), 14731c6fdbd8SKent Overstreet !(flags & BCH_FORCE_IF_DATA_DEGRADED) 14741c6fdbd8SKent Overstreet ? c->opts.data_replicas 14754e074475SKent Overstreet : data_replicas_required(c)); 14761c6fdbd8SKent Overstreet 14771c6fdbd8SKent Overstreet return nr_rw >= required; 14782436cb9fSKent Overstreet case BCH_MEMBER_STATE_failed: 14792436cb9fSKent Overstreet case BCH_MEMBER_STATE_spare: 14802436cb9fSKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_rw && 14812436cb9fSKent Overstreet ca->mi.state != BCH_MEMBER_STATE_ro) 14821c6fdbd8SKent Overstreet return true; 14831c6fdbd8SKent Overstreet 14841c6fdbd8SKent Overstreet /* do we have enough devices to read from? */ 14851c6fdbd8SKent Overstreet new_online_devs = bch2_online_devs(c); 14861c6fdbd8SKent Overstreet __clear_bit(ca->dev_idx, new_online_devs.d); 14871c6fdbd8SKent Overstreet 1488fcb3431bSKent Overstreet return bch2_have_enough_devs(c, new_online_devs, flags, false); 14891c6fdbd8SKent Overstreet default: 14901c6fdbd8SKent Overstreet BUG(); 14911c6fdbd8SKent Overstreet } 14921c6fdbd8SKent Overstreet } 14931c6fdbd8SKent Overstreet 14941c6fdbd8SKent Overstreet static bool bch2_fs_may_start(struct bch_fs *c) 14951c6fdbd8SKent Overstreet { 14961c6fdbd8SKent Overstreet struct bch_dev *ca; 1497fcb3431bSKent Overstreet unsigned i, flags = 0; 14981c6fdbd8SKent Overstreet 1499fcb3431bSKent Overstreet if (c->opts.very_degraded) 1500fcb3431bSKent Overstreet flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST; 1501fcb3431bSKent Overstreet 1502fcb3431bSKent Overstreet if (c->opts.degraded) 1503fcb3431bSKent Overstreet flags |= BCH_FORCE_IF_DEGRADED; 1504fcb3431bSKent Overstreet 1505fcb3431bSKent Overstreet if (!c->opts.degraded && 1506fcb3431bSKent Overstreet !c->opts.very_degraded) { 15071c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15081c6fdbd8SKent Overstreet 15091c6fdbd8SKent Overstreet for (i = 0; i < c->disk_sb.sb->nr_devices; i++) { 15102f724563SKent Overstreet if (!bch2_member_exists(c->disk_sb.sb, i)) 15111c6fdbd8SKent Overstreet continue; 15121c6fdbd8SKent Overstreet 15132f724563SKent Overstreet ca = bch2_dev_locked(c, i); 15141c6fdbd8SKent Overstreet 15151c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca) && 15162436cb9fSKent Overstreet (ca->mi.state == BCH_MEMBER_STATE_rw || 15172436cb9fSKent Overstreet ca->mi.state == BCH_MEMBER_STATE_ro)) { 15181c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15191c6fdbd8SKent Overstreet return false; 15201c6fdbd8SKent Overstreet } 15211c6fdbd8SKent Overstreet } 15221c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15231c6fdbd8SKent Overstreet } 15241c6fdbd8SKent Overstreet 1525fcb3431bSKent Overstreet return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true); 15261c6fdbd8SKent Overstreet } 15271c6fdbd8SKent Overstreet 15281c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) 15291c6fdbd8SKent Overstreet { 15301c6fdbd8SKent Overstreet /* 15311c6fdbd8SKent Overstreet * The allocator thread itself allocates btree nodes, so stop it first: 15321c6fdbd8SKent Overstreet */ 15331c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 15341c6fdbd8SKent Overstreet bch2_dev_journal_stop(&c->journal, ca); 15351c6fdbd8SKent Overstreet } 15361c6fdbd8SKent Overstreet 1537f25d8215SKent Overstreet static void __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) 15381c6fdbd8SKent Overstreet { 15391c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 15401c6fdbd8SKent Overstreet 15412436cb9fSKent Overstreet BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw); 15421c6fdbd8SKent Overstreet 15431c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 15441c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 15451c6fdbd8SKent Overstreet } 15461c6fdbd8SKent Overstreet 15471c6fdbd8SKent Overstreet int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 15481c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 15491c6fdbd8SKent Overstreet { 15503f7b9713SHunter Shaffer struct bch_member *m; 15511c6fdbd8SKent Overstreet int ret = 0; 15521c6fdbd8SKent Overstreet 15531c6fdbd8SKent Overstreet if (ca->mi.state == new_state) 15541c6fdbd8SKent Overstreet return 0; 15551c6fdbd8SKent Overstreet 15561c6fdbd8SKent Overstreet if (!bch2_dev_state_allowed(c, ca, new_state, flags)) 155778c0b75cSKent Overstreet return -BCH_ERR_device_state_not_allowed; 15581c6fdbd8SKent Overstreet 15592436cb9fSKent Overstreet if (new_state != BCH_MEMBER_STATE_rw) 15601c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 15611c6fdbd8SKent Overstreet 15622436cb9fSKent Overstreet bch_notice(ca, "%s", bch2_member_states[new_state]); 15631c6fdbd8SKent Overstreet 15641c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15653f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 15663f7b9713SHunter Shaffer SET_BCH_MEMBER_STATE(m, new_state); 15671c6fdbd8SKent Overstreet bch2_write_super(c); 15681c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15691c6fdbd8SKent Overstreet 1570e2b60560SKent Overstreet if (new_state == BCH_MEMBER_STATE_rw) 1571f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 15721c6fdbd8SKent Overstreet 15731c6fdbd8SKent Overstreet rebalance_wakeup(c); 15741c6fdbd8SKent Overstreet 15751c6fdbd8SKent Overstreet return ret; 15761c6fdbd8SKent Overstreet } 15771c6fdbd8SKent Overstreet 15781c6fdbd8SKent Overstreet int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 15791c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 15801c6fdbd8SKent Overstreet { 15811c6fdbd8SKent Overstreet int ret; 15821c6fdbd8SKent Overstreet 15831ada1606SKent Overstreet down_write(&c->state_lock); 15841c6fdbd8SKent Overstreet ret = __bch2_dev_set_state(c, ca, new_state, flags); 15851ada1606SKent Overstreet up_write(&c->state_lock); 15861c6fdbd8SKent Overstreet 15871c6fdbd8SKent Overstreet return ret; 15881c6fdbd8SKent Overstreet } 15891c6fdbd8SKent Overstreet 15901c6fdbd8SKent Overstreet /* Device add/removal: */ 15911c6fdbd8SKent Overstreet 1592c0ebe3e4SKent Overstreet static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca) 15935d20ba48SKent Overstreet { 1594c6b2826cSKent Overstreet struct bpos start = POS(ca->dev_idx, 0); 1595c6b2826cSKent Overstreet struct bpos end = POS(ca->dev_idx, U64_MAX); 15965d20ba48SKent Overstreet int ret; 15975d20ba48SKent Overstreet 1598a9c0a4cbSKent Overstreet /* 1599a9c0a4cbSKent Overstreet * We clear the LRU and need_discard btrees first so that we don't race 1600a9c0a4cbSKent Overstreet * with bch2_do_invalidates() and bch2_do_discards() 1601a9c0a4cbSKent Overstreet */ 1602a9c0a4cbSKent Overstreet ret = bch2_btree_delete_range(c, BTREE_ID_lru, start, end, 16035dd8c60eSKent Overstreet BTREE_TRIGGER_norun, NULL) ?: 1604a9c0a4cbSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_need_discard, start, end, 16055dd8c60eSKent Overstreet BTREE_TRIGGER_norun, NULL) ?: 1606c6b2826cSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_freespace, start, end, 16075dd8c60eSKent Overstreet BTREE_TRIGGER_norun, NULL) ?: 1608a8c752bbSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_backpointers, start, end, 16095dd8c60eSKent Overstreet BTREE_TRIGGER_norun, NULL) ?: 1610a9c0a4cbSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_alloc, start, end, 16115dd8c60eSKent Overstreet BTREE_TRIGGER_norun, NULL) ?: 161202d51bb9SBrian Foster bch2_btree_delete_range(c, BTREE_ID_bucket_gens, start, end, 16135dd8c60eSKent Overstreet BTREE_TRIGGER_norun, NULL); 1614e46c181aSKent Overstreet bch_err_msg(c, ret, "removing dev alloc info"); 1615c6b2826cSKent Overstreet return ret; 16165d20ba48SKent Overstreet } 16175d20ba48SKent Overstreet 16181c6fdbd8SKent Overstreet int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) 16191c6fdbd8SKent Overstreet { 16203f7b9713SHunter Shaffer struct bch_member *m; 16211c6fdbd8SKent Overstreet unsigned dev_idx = ca->dev_idx, data; 162278c0b75cSKent Overstreet int ret; 16231c6fdbd8SKent Overstreet 16241ada1606SKent Overstreet down_write(&c->state_lock); 16251c6fdbd8SKent Overstreet 162631ba2cd3SKent Overstreet /* 162731ba2cd3SKent Overstreet * We consume a reference to ca->ref, regardless of whether we succeed 162831ba2cd3SKent Overstreet * or fail: 162931ba2cd3SKent Overstreet */ 163031ba2cd3SKent Overstreet percpu_ref_put(&ca->ref); 16311c6fdbd8SKent Overstreet 16322436cb9fSKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 16331c6fdbd8SKent Overstreet bch_err(ca, "Cannot remove without losing data"); 163478c0b75cSKent Overstreet ret = -BCH_ERR_device_state_not_allowed; 16351c6fdbd8SKent Overstreet goto err; 16361c6fdbd8SKent Overstreet } 16371c6fdbd8SKent Overstreet 16381c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 16391c6fdbd8SKent Overstreet 16401c6fdbd8SKent Overstreet ret = bch2_dev_data_drop(c, ca->dev_idx, flags); 1641130d229fSKent Overstreet bch_err_msg(ca, ret, "bch2_dev_data_drop()"); 1642cf904c8dSKent Overstreet if (ret) 16431c6fdbd8SKent Overstreet goto err; 16441c6fdbd8SKent Overstreet 16455d20ba48SKent Overstreet ret = bch2_dev_remove_alloc(c, ca); 1646130d229fSKent Overstreet bch_err_msg(ca, ret, "bch2_dev_remove_alloc()"); 1647cf904c8dSKent Overstreet if (ret) 16481c6fdbd8SKent Overstreet goto err; 16491c6fdbd8SKent Overstreet 1650a9c0a4cbSKent Overstreet ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); 1651130d229fSKent Overstreet bch_err_msg(ca, ret, "bch2_journal_flush_device_pins()"); 1652cf904c8dSKent Overstreet if (ret) 1653a9c0a4cbSKent Overstreet goto err; 1654a9c0a4cbSKent Overstreet 1655a9c0a4cbSKent Overstreet ret = bch2_journal_flush(&c->journal); 1656130d229fSKent Overstreet bch_err_msg(ca, ret, "bch2_journal_flush()"); 1657cf904c8dSKent Overstreet if (ret) 16581c6fdbd8SKent Overstreet goto err; 16591c6fdbd8SKent Overstreet 166031ba2cd3SKent Overstreet ret = bch2_replicas_gc2(c); 1661130d229fSKent Overstreet bch_err_msg(ca, ret, "bch2_replicas_gc2()"); 1662cf904c8dSKent Overstreet if (ret) 166331ba2cd3SKent Overstreet goto err; 166431ba2cd3SKent Overstreet 166531ba2cd3SKent Overstreet data = bch2_dev_has_data(c, ca); 166631ba2cd3SKent Overstreet if (data) { 1667fa8e94faSKent Overstreet struct printbuf data_has = PRINTBUF; 166831ba2cd3SKent Overstreet 1669e58f963cSKent Overstreet prt_bitflags(&data_has, __bch2_data_types, data); 1670fa8e94faSKent Overstreet bch_err(ca, "Remove failed, still has data (%s)", data_has.buf); 1671fa8e94faSKent Overstreet printbuf_exit(&data_has); 167231ba2cd3SKent Overstreet ret = -EBUSY; 167331ba2cd3SKent Overstreet goto err; 167431ba2cd3SKent Overstreet } 167531ba2cd3SKent Overstreet 16761c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 16771c6fdbd8SKent Overstreet 16781c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 16791c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], NULL); 16801c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 16811c6fdbd8SKent Overstreet 16821c6fdbd8SKent Overstreet percpu_ref_kill(&ca->ref); 16831c6fdbd8SKent Overstreet wait_for_completion(&ca->ref_completion); 16841c6fdbd8SKent Overstreet 16851c6fdbd8SKent Overstreet bch2_dev_free(ca); 16861c6fdbd8SKent Overstreet 16871c6fdbd8SKent Overstreet /* 1688bc652905SBrian Foster * At this point the device object has been removed in-core, but the 1689bc652905SBrian Foster * on-disk journal might still refer to the device index via sb device 1690bc652905SBrian Foster * usage entries. Recovery fails if it sees usage information for an 1691bc652905SBrian Foster * invalid device. Flush journal pins to push the back of the journal 1692bc652905SBrian Foster * past now invalid device index references before we update the 1693bc652905SBrian Foster * superblock, but after the device object has been removed so any 1694bc652905SBrian Foster * further journal writes elide usage info for the device. 1695bc652905SBrian Foster */ 1696bc652905SBrian Foster bch2_journal_flush_all_pins(&c->journal); 1697bc652905SBrian Foster 1698bc652905SBrian Foster /* 16991c6fdbd8SKent Overstreet * Free this device's slot in the bch_member array - all pointers to 17001c6fdbd8SKent Overstreet * this device must be gone: 17011c6fdbd8SKent Overstreet */ 17021c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 17033f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx); 17043f7b9713SHunter Shaffer memset(&m->uuid, 0, sizeof(m->uuid)); 17051c6fdbd8SKent Overstreet 17061c6fdbd8SKent Overstreet bch2_write_super(c); 17071c6fdbd8SKent Overstreet 17081c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17091ada1606SKent Overstreet up_write(&c->state_lock); 1710180fb49dSKent Overstreet 1711180fb49dSKent Overstreet bch2_dev_usage_journal_reserve(c); 17121c6fdbd8SKent Overstreet return 0; 17131c6fdbd8SKent Overstreet err: 17142436cb9fSKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw && 1715d3bb629dSKent Overstreet !percpu_ref_is_zero(&ca->io_ref)) 17161c6fdbd8SKent Overstreet __bch2_dev_read_write(c, ca); 17171ada1606SKent Overstreet up_write(&c->state_lock); 17181c6fdbd8SKent Overstreet return ret; 17191c6fdbd8SKent Overstreet } 17201c6fdbd8SKent Overstreet 17211c6fdbd8SKent Overstreet /* Add new device to running filesystem: */ 17221c6fdbd8SKent Overstreet int bch2_dev_add(struct bch_fs *c, const char *path) 17231c6fdbd8SKent Overstreet { 17241c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 17251c6fdbd8SKent Overstreet struct bch_sb_handle sb; 17261c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 17273f7b9713SHunter Shaffer struct bch_sb_field_members_v2 *mi; 17281c6fdbd8SKent Overstreet struct bch_member dev_mi; 17291c6fdbd8SKent Overstreet unsigned dev_idx, nr_devices, u64s; 1730fa8e94faSKent Overstreet struct printbuf errbuf = PRINTBUF; 173102afcb8cSKent Overstreet struct printbuf label = PRINTBUF; 17321c6fdbd8SKent Overstreet int ret; 17331c6fdbd8SKent Overstreet 17341c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 1735e46c181aSKent Overstreet bch_err_msg(c, ret, "reading super"); 1736cf904c8dSKent Overstreet if (ret) 1737efe68e1dSKent Overstreet goto err; 17381c6fdbd8SKent Overstreet 17391241df58SHunter Shaffer dev_mi = bch2_sb_member_get(sb.sb, sb.sb->dev_idx); 17401c6fdbd8SKent Overstreet 174102afcb8cSKent Overstreet if (BCH_MEMBER_GROUP(&dev_mi)) { 1742e677179bSKent Overstreet bch2_disk_path_to_text_sb(&label, sb.sb, BCH_MEMBER_GROUP(&dev_mi) - 1); 174302afcb8cSKent Overstreet if (label.allocation_failure) { 174402afcb8cSKent Overstreet ret = -ENOMEM; 174502afcb8cSKent Overstreet goto err; 174602afcb8cSKent Overstreet } 174702afcb8cSKent Overstreet } 174802afcb8cSKent Overstreet 174978c0b75cSKent Overstreet ret = bch2_dev_may_add(sb.sb, c); 1750cf904c8dSKent Overstreet if (ret) 1751efe68e1dSKent Overstreet goto err; 17521c6fdbd8SKent Overstreet 17531c6fdbd8SKent Overstreet ca = __bch2_dev_alloc(c, &dev_mi); 17541c6fdbd8SKent Overstreet if (!ca) { 1755efe68e1dSKent Overstreet ret = -ENOMEM; 1756efe68e1dSKent Overstreet goto err; 17571c6fdbd8SKent Overstreet } 17581c6fdbd8SKent Overstreet 1759822835ffSKent Overstreet bch2_dev_usage_init(ca); 1760822835ffSKent Overstreet 17611c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, &sb); 176271933fb6SChristophe JAILLET if (ret) 1763efe68e1dSKent Overstreet goto err; 17641c6fdbd8SKent Overstreet 17651c6fdbd8SKent Overstreet ret = bch2_dev_journal_alloc(ca); 1766e46c181aSKent Overstreet bch_err_msg(c, ret, "allocating journal"); 1767cf904c8dSKent Overstreet if (ret) 17681c6fdbd8SKent Overstreet goto err; 17691c6fdbd8SKent Overstreet 17701ada1606SKent Overstreet down_write(&c->state_lock); 17711c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 17721c6fdbd8SKent Overstreet 17731c6fdbd8SKent Overstreet ret = bch2_sb_from_fs(c, ca); 1774e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 1775cf904c8dSKent Overstreet if (ret) 17761c6fdbd8SKent Overstreet goto err_unlock; 17771c6fdbd8SKent Overstreet 17781c6fdbd8SKent Overstreet if (dynamic_fault("bcachefs:add:no_slot")) 17791c6fdbd8SKent Overstreet goto no_slot; 17801c6fdbd8SKent Overstreet 17811c6fdbd8SKent Overstreet for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++) 17822f724563SKent Overstreet if (!bch2_member_exists(c->disk_sb.sb, dev_idx)) 17831c6fdbd8SKent Overstreet goto have_slot; 17841c6fdbd8SKent Overstreet no_slot: 1785098ef98dSKent Overstreet ret = -BCH_ERR_ENOSPC_sb_members; 1786e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 17871c6fdbd8SKent Overstreet goto err_unlock; 17881c6fdbd8SKent Overstreet 17891c6fdbd8SKent Overstreet have_slot: 17901c6fdbd8SKent Overstreet nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices); 1791e8484348SKent Overstreet 1792e8484348SKent Overstreet mi = bch2_sb_field_get(c->disk_sb.sb, members_v2); 17933f7b9713SHunter Shaffer u64s = DIV_ROUND_UP(sizeof(struct bch_sb_field_members_v2) + 17943f7b9713SHunter Shaffer le16_to_cpu(mi->member_bytes) * nr_devices, sizeof(u64)); 17951c6fdbd8SKent Overstreet 17964637429eSKent Overstreet mi = bch2_sb_field_resize(&c->disk_sb, members_v2, u64s); 1797e8536925SKent Overstreet if (!mi) { 1798098ef98dSKent Overstreet ret = -BCH_ERR_ENOSPC_sb_members; 1799e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 18001c6fdbd8SKent Overstreet goto err_unlock; 1801e8536925SKent Overstreet } 18023f7b9713SHunter Shaffer struct bch_member *m = bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx); 18031c6fdbd8SKent Overstreet 18041c6fdbd8SKent Overstreet /* success: */ 18051c6fdbd8SKent Overstreet 18063f7b9713SHunter Shaffer *m = dev_mi; 18073f7b9713SHunter Shaffer m->last_mount = cpu_to_le64(ktime_get_real_seconds()); 18081c6fdbd8SKent Overstreet c->disk_sb.sb->nr_devices = nr_devices; 18091c6fdbd8SKent Overstreet 18101c6fdbd8SKent Overstreet ca->disk_sb.sb->dev_idx = dev_idx; 18111c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 18121c6fdbd8SKent Overstreet 181302afcb8cSKent Overstreet if (BCH_MEMBER_GROUP(&dev_mi)) { 181402afcb8cSKent Overstreet ret = __bch2_dev_group_set(c, ca, label.buf); 1815e46c181aSKent Overstreet bch_err_msg(c, ret, "creating new label"); 1816cf904c8dSKent Overstreet if (ret) 181702afcb8cSKent Overstreet goto err_unlock; 181802afcb8cSKent Overstreet } 181902afcb8cSKent Overstreet 18201c6fdbd8SKent Overstreet bch2_write_super(c); 18211c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 18221c6fdbd8SKent Overstreet 1823180fb49dSKent Overstreet bch2_dev_usage_journal_reserve(c); 1824180fb49dSKent Overstreet 18255dd8c60eSKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); 1826bbe682c7SKent Overstreet bch_err_msg(ca, ret, "marking new superblock"); 1827cf904c8dSKent Overstreet if (ret) 1828bfcf840dSKent Overstreet goto err_late; 18298d6b6222SKent Overstreet 1830c6b2826cSKent Overstreet ret = bch2_fs_freespace_init(c); 1831bbe682c7SKent Overstreet bch_err_msg(ca, ret, "initializing free space"); 1832cf904c8dSKent Overstreet if (ret) 1833c6b2826cSKent Overstreet goto err_late; 1834c6b2826cSKent Overstreet 183509943313SKent Overstreet ca->new_fs_bucket_idx = 0; 183609943313SKent Overstreet 1837f25d8215SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw) 1838f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 18391c6fdbd8SKent Overstreet 18401ada1606SKent Overstreet up_write(&c->state_lock); 18411c6fdbd8SKent Overstreet return 0; 18421c6fdbd8SKent Overstreet 18431c6fdbd8SKent Overstreet err_unlock: 18441c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 18451ada1606SKent Overstreet up_write(&c->state_lock); 18461c6fdbd8SKent Overstreet err: 18471c6fdbd8SKent Overstreet if (ca) 18481c6fdbd8SKent Overstreet bch2_dev_free(ca); 18491c6fdbd8SKent Overstreet bch2_free_super(&sb); 185002afcb8cSKent Overstreet printbuf_exit(&label); 1851fa8e94faSKent Overstreet printbuf_exit(&errbuf); 1852cf904c8dSKent Overstreet bch_err_fn(c, ret); 18531c6fdbd8SKent Overstreet return ret; 18541c6fdbd8SKent Overstreet err_late: 1855bfcf840dSKent Overstreet up_write(&c->state_lock); 1856efe68e1dSKent Overstreet ca = NULL; 1857efe68e1dSKent Overstreet goto err; 18581c6fdbd8SKent Overstreet } 18591c6fdbd8SKent Overstreet 18601c6fdbd8SKent Overstreet /* Hot add existing device to running filesystem: */ 18611c6fdbd8SKent Overstreet int bch2_dev_online(struct bch_fs *c, const char *path) 18621c6fdbd8SKent Overstreet { 18631c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 18641c6fdbd8SKent Overstreet struct bch_sb_handle sb = { NULL }; 18651c6fdbd8SKent Overstreet struct bch_dev *ca; 18661c6fdbd8SKent Overstreet unsigned dev_idx; 18671c6fdbd8SKent Overstreet int ret; 18681c6fdbd8SKent Overstreet 18691ada1606SKent Overstreet down_write(&c->state_lock); 18701c6fdbd8SKent Overstreet 18711c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 18721c6fdbd8SKent Overstreet if (ret) { 18731ada1606SKent Overstreet up_write(&c->state_lock); 18741c6fdbd8SKent Overstreet return ret; 18751c6fdbd8SKent Overstreet } 18761c6fdbd8SKent Overstreet 18771c6fdbd8SKent Overstreet dev_idx = sb.sb->dev_idx; 18781c6fdbd8SKent Overstreet 18792f300f09SKent Overstreet ret = bch2_dev_in_fs(&c->disk_sb, &sb, &c->opts); 1880e46c181aSKent Overstreet bch_err_msg(c, ret, "bringing %s online", path); 1881cf904c8dSKent Overstreet if (ret) 18821c6fdbd8SKent Overstreet goto err; 18831c6fdbd8SKent Overstreet 1884e2b60560SKent Overstreet ret = bch2_dev_attach_bdev(c, &sb); 1885e2b60560SKent Overstreet if (ret) 1886e2b60560SKent Overstreet goto err; 1887e2b60560SKent Overstreet 18882f724563SKent Overstreet ca = bch2_dev_locked(c, dev_idx); 1889bfcf840dSKent Overstreet 18905dd8c60eSKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); 1891e46c181aSKent Overstreet bch_err_msg(c, ret, "bringing %s online: error from bch2_trans_mark_dev_sb", path); 1892cf904c8dSKent Overstreet if (ret) 1893bfcf840dSKent Overstreet goto err; 1894bfcf840dSKent Overstreet 1895f25d8215SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw) 1896f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 18971c6fdbd8SKent Overstreet 1898bbe682c7SKent Overstreet if (!ca->mi.freespace_initialized) { 1899bbe682c7SKent Overstreet ret = bch2_dev_freespace_init(c, ca, 0, ca->mi.nbuckets); 1900bbe682c7SKent Overstreet bch_err_msg(ca, ret, "initializing free space"); 1901bbe682c7SKent Overstreet if (ret) 1902bbe682c7SKent Overstreet goto err; 1903bbe682c7SKent Overstreet } 1904bbe682c7SKent Overstreet 1905bbe682c7SKent Overstreet if (!ca->journal.nr) { 1906bbe682c7SKent Overstreet ret = bch2_dev_journal_alloc(ca); 1907bbe682c7SKent Overstreet bch_err_msg(ca, ret, "allocating journal"); 1908bbe682c7SKent Overstreet if (ret) 1909bbe682c7SKent Overstreet goto err; 1910bbe682c7SKent Overstreet } 1911bbe682c7SKent Overstreet 19121c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 1913bbe682c7SKent Overstreet bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = 1914a420eea6STim Schlueter cpu_to_le64(ktime_get_real_seconds()); 19151c6fdbd8SKent Overstreet bch2_write_super(c); 19161c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 19171c6fdbd8SKent Overstreet 19181ada1606SKent Overstreet up_write(&c->state_lock); 19191c6fdbd8SKent Overstreet return 0; 19201c6fdbd8SKent Overstreet err: 19211ada1606SKent Overstreet up_write(&c->state_lock); 19221c6fdbd8SKent Overstreet bch2_free_super(&sb); 192378c0b75cSKent Overstreet return ret; 19241c6fdbd8SKent Overstreet } 19251c6fdbd8SKent Overstreet 19261c6fdbd8SKent Overstreet int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) 19271c6fdbd8SKent Overstreet { 19281ada1606SKent Overstreet down_write(&c->state_lock); 19291c6fdbd8SKent Overstreet 19301c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca)) { 19311c6fdbd8SKent Overstreet bch_err(ca, "Already offline"); 19321ada1606SKent Overstreet up_write(&c->state_lock); 19331c6fdbd8SKent Overstreet return 0; 19341c6fdbd8SKent Overstreet } 19351c6fdbd8SKent Overstreet 19362436cb9fSKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 19371c6fdbd8SKent Overstreet bch_err(ca, "Cannot offline required disk"); 19381ada1606SKent Overstreet up_write(&c->state_lock); 193978c0b75cSKent Overstreet return -BCH_ERR_device_state_not_allowed; 19401c6fdbd8SKent Overstreet } 19411c6fdbd8SKent Overstreet 19421c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 19431c6fdbd8SKent Overstreet 19441ada1606SKent Overstreet up_write(&c->state_lock); 19451c6fdbd8SKent Overstreet return 0; 19461c6fdbd8SKent Overstreet } 19471c6fdbd8SKent Overstreet 19481c6fdbd8SKent Overstreet int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) 19491c6fdbd8SKent Overstreet { 19503f7b9713SHunter Shaffer struct bch_member *m; 195169d1f052SKent Overstreet u64 old_nbuckets; 19521c6fdbd8SKent Overstreet int ret = 0; 19531c6fdbd8SKent Overstreet 19541ada1606SKent Overstreet down_write(&c->state_lock); 195569d1f052SKent Overstreet old_nbuckets = ca->mi.nbuckets; 19561c6fdbd8SKent Overstreet 19571c6fdbd8SKent Overstreet if (nbuckets < ca->mi.nbuckets) { 19581c6fdbd8SKent Overstreet bch_err(ca, "Cannot shrink yet"); 19591c6fdbd8SKent Overstreet ret = -EINVAL; 19601c6fdbd8SKent Overstreet goto err; 19611c6fdbd8SKent Overstreet } 19621c6fdbd8SKent Overstreet 1963db42549dSKent Overstreet if (nbuckets > BCH_MEMBER_NBUCKETS_MAX) { 1964db42549dSKent Overstreet bch_err(ca, "New device size too big (%llu greater than max %u)", 1965db42549dSKent Overstreet nbuckets, BCH_MEMBER_NBUCKETS_MAX); 1966db42549dSKent Overstreet ret = -BCH_ERR_device_size_too_big; 1967db42549dSKent Overstreet goto err; 1968db42549dSKent Overstreet } 1969db42549dSKent Overstreet 19701c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca) && 19711c6fdbd8SKent Overstreet get_capacity(ca->disk_sb.bdev->bd_disk) < 19721c6fdbd8SKent Overstreet ca->mi.bucket_size * nbuckets) { 19731c6fdbd8SKent Overstreet bch_err(ca, "New size larger than device"); 197478c0b75cSKent Overstreet ret = -BCH_ERR_device_size_too_small; 19751c6fdbd8SKent Overstreet goto err; 19761c6fdbd8SKent Overstreet } 19771c6fdbd8SKent Overstreet 19781c6fdbd8SKent Overstreet ret = bch2_dev_buckets_resize(c, ca, nbuckets); 1979e46c181aSKent Overstreet bch_err_msg(ca, ret, "resizing buckets"); 1980cf904c8dSKent Overstreet if (ret) 19811c6fdbd8SKent Overstreet goto err; 19821c6fdbd8SKent Overstreet 19835dd8c60eSKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca, BTREE_TRIGGER_transactional); 19843e3e02e6SKent Overstreet if (ret) 1985224ec3e6SKent Overstreet goto err; 1986224ec3e6SKent Overstreet 19871c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 19883f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 19893f7b9713SHunter Shaffer m->nbuckets = cpu_to_le64(nbuckets); 19901c6fdbd8SKent Overstreet 19911c6fdbd8SKent Overstreet bch2_write_super(c); 19921c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 19931c6fdbd8SKent Overstreet 199469d1f052SKent Overstreet if (ca->mi.freespace_initialized) { 199569d1f052SKent Overstreet ret = bch2_dev_freespace_init(c, ca, old_nbuckets, nbuckets); 199669d1f052SKent Overstreet if (ret) 199769d1f052SKent Overstreet goto err; 199869d1f052SKent Overstreet 199969d1f052SKent Overstreet /* 200069d1f052SKent Overstreet * XXX: this is all wrong transactionally - we'll be able to do 200169d1f052SKent Overstreet * this correctly after the disk space accounting rewrite 200269d1f052SKent Overstreet */ 200369d1f052SKent Overstreet ca->usage_base->d[BCH_DATA_free].buckets += nbuckets - old_nbuckets; 200469d1f052SKent Overstreet } 200569d1f052SKent Overstreet 20061c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 20071c6fdbd8SKent Overstreet err: 20081ada1606SKent Overstreet up_write(&c->state_lock); 20091c6fdbd8SKent Overstreet return ret; 20101c6fdbd8SKent Overstreet } 20111c6fdbd8SKent Overstreet 20121c6fdbd8SKent Overstreet /* return with ref on ca->ref: */ 2013bf7e49a4SKent Overstreet struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name) 20141c6fdbd8SKent Overstreet { 20153a2d0259SKent Overstreet for_each_member_device(c, ca) 20163a2d0259SKent Overstreet if (!strcmp(name, ca->name)) 20171c6fdbd8SKent Overstreet return ca; 201841b84fb4SKent Overstreet return ERR_PTR(-BCH_ERR_ENOENT_dev_not_found); 201941b84fb4SKent Overstreet } 20201c6fdbd8SKent Overstreet 20211c6fdbd8SKent Overstreet /* Filesystem open: */ 20221c6fdbd8SKent Overstreet 20230d529663SKent Overstreet static inline int sb_cmp(struct bch_sb *l, struct bch_sb *r) 20240d529663SKent Overstreet { 20250d529663SKent Overstreet return cmp_int(le64_to_cpu(l->seq), le64_to_cpu(r->seq)) ?: 20260d529663SKent Overstreet cmp_int(le64_to_cpu(l->write_time), le64_to_cpu(r->write_time)); 20270d529663SKent Overstreet } 20280d529663SKent Overstreet 20291c6fdbd8SKent Overstreet struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, 20301c6fdbd8SKent Overstreet struct bch_opts opts) 20311c6fdbd8SKent Overstreet { 2032d4c8bb69SKent Overstreet DARRAY(struct bch_sb_handle) sbs = { 0 }; 20331c6fdbd8SKent Overstreet struct bch_fs *c = NULL; 2034defd9e39SKent Overstreet struct bch_sb_handle *best = NULL; 2035fa8e94faSKent Overstreet struct printbuf errbuf = PRINTBUF; 2036e2b60560SKent Overstreet int ret = 0; 20371c6fdbd8SKent Overstreet 2038efe68e1dSKent Overstreet if (!try_module_get(THIS_MODULE)) 2039efe68e1dSKent Overstreet return ERR_PTR(-ENODEV); 2040efe68e1dSKent Overstreet 20411c6fdbd8SKent Overstreet if (!nr_devices) { 2042efe68e1dSKent Overstreet ret = -EINVAL; 2043efe68e1dSKent Overstreet goto err; 20441c6fdbd8SKent Overstreet } 20451c6fdbd8SKent Overstreet 2046d4c8bb69SKent Overstreet ret = darray_make_room(&sbs, nr_devices); 20471c6fdbd8SKent Overstreet if (ret) 20481c6fdbd8SKent Overstreet goto err; 20491c6fdbd8SKent Overstreet 2050d4c8bb69SKent Overstreet for (unsigned i = 0; i < nr_devices; i++) { 2051d4c8bb69SKent Overstreet struct bch_sb_handle sb = { NULL }; 2052d4c8bb69SKent Overstreet 2053d4c8bb69SKent Overstreet ret = bch2_read_super(devices[i], &opts, &sb); 2054d4c8bb69SKent Overstreet if (ret) 2055d4c8bb69SKent Overstreet goto err; 2056d4c8bb69SKent Overstreet 2057d4c8bb69SKent Overstreet BUG_ON(darray_push(&sbs, sb)); 20581c6fdbd8SKent Overstreet } 20591c6fdbd8SKent Overstreet 206062719cf3SKent Overstreet if (opts.nochanges && !opts.read_only) { 206162719cf3SKent Overstreet ret = -BCH_ERR_erofs_nochanges; 206262719cf3SKent Overstreet goto err_print; 206362719cf3SKent Overstreet } 206462719cf3SKent Overstreet 2065d4c8bb69SKent Overstreet darray_for_each(sbs, sb) 20660d529663SKent Overstreet if (!best || sb_cmp(sb->sb, best->sb) > 0) 2067d4c8bb69SKent Overstreet best = sb; 20681c6fdbd8SKent Overstreet 2069d4c8bb69SKent Overstreet darray_for_each_reverse(sbs, sb) { 20702f300f09SKent Overstreet ret = bch2_dev_in_fs(best, sb, &opts); 20710d529663SKent Overstreet 20720d529663SKent Overstreet if (ret == -BCH_ERR_device_has_been_removed || 20730d529663SKent Overstreet ret == -BCH_ERR_device_splitbrain) { 2074d4c8bb69SKent Overstreet bch2_free_super(sb); 2075d4c8bb69SKent Overstreet darray_remove_item(&sbs, sb); 2076d4c8bb69SKent Overstreet best -= best > sb; 20770d529663SKent Overstreet ret = 0; 2078625104eaSKent Overstreet continue; 2079625104eaSKent Overstreet } 2080625104eaSKent Overstreet 208178c0b75cSKent Overstreet if (ret) 20821c6fdbd8SKent Overstreet goto err_print; 20831c6fdbd8SKent Overstreet } 20841c6fdbd8SKent Overstreet 2085d4c8bb69SKent Overstreet c = bch2_fs_alloc(best->sb, opts); 2086d4c8bb69SKent Overstreet ret = PTR_ERR_OR_ZERO(c); 2087d4c8bb69SKent Overstreet if (ret) 20881c6fdbd8SKent Overstreet goto err; 20891c6fdbd8SKent Overstreet 20901ada1606SKent Overstreet down_write(&c->state_lock); 2091d4c8bb69SKent Overstreet darray_for_each(sbs, sb) { 2092d4c8bb69SKent Overstreet ret = bch2_dev_attach_bdev(c, sb); 2093e2b60560SKent Overstreet if (ret) { 20941ada1606SKent Overstreet up_write(&c->state_lock); 2095e2b60560SKent Overstreet goto err; 2096e2b60560SKent Overstreet } 20971c6fdbd8SKent Overstreet } 20981ada1606SKent Overstreet up_write(&c->state_lock); 20991c6fdbd8SKent Overstreet 210078c0b75cSKent Overstreet if (!bch2_fs_may_start(c)) { 210178c0b75cSKent Overstreet ret = -BCH_ERR_insufficient_devices_to_start; 21021c6fdbd8SKent Overstreet goto err_print; 210378c0b75cSKent Overstreet } 21041c6fdbd8SKent Overstreet 21051c6fdbd8SKent Overstreet if (!c->opts.nostart) { 2106619f5beeSKent Overstreet ret = bch2_fs_start(c); 2107619f5beeSKent Overstreet if (ret) 2108619f5beeSKent Overstreet goto err; 21091c6fdbd8SKent Overstreet } 21101c6fdbd8SKent Overstreet out: 2111d4c8bb69SKent Overstreet darray_for_each(sbs, sb) 2112d4c8bb69SKent Overstreet bch2_free_super(sb); 2113d4c8bb69SKent Overstreet darray_exit(&sbs); 2114fa8e94faSKent Overstreet printbuf_exit(&errbuf); 21151c6fdbd8SKent Overstreet module_put(THIS_MODULE); 21161c6fdbd8SKent Overstreet return c; 21171c6fdbd8SKent Overstreet err_print: 21181c6fdbd8SKent Overstreet pr_err("bch_fs_open err opening %s: %s", 211978c0b75cSKent Overstreet devices[0], bch2_err_str(ret)); 21201c6fdbd8SKent Overstreet err: 21217be9ab63SChris Webb if (!IS_ERR_OR_NULL(c)) 21221c6fdbd8SKent Overstreet bch2_fs_stop(c); 21231c6fdbd8SKent Overstreet c = ERR_PTR(ret); 21241c6fdbd8SKent Overstreet goto out; 21251c6fdbd8SKent Overstreet } 21261c6fdbd8SKent Overstreet 21271c6fdbd8SKent Overstreet /* Global interfaces/init */ 21281c6fdbd8SKent Overstreet 21291c6fdbd8SKent Overstreet static void bcachefs_exit(void) 21301c6fdbd8SKent Overstreet { 21311c6fdbd8SKent Overstreet bch2_debug_exit(); 21321c6fdbd8SKent Overstreet bch2_vfs_exit(); 21331c6fdbd8SKent Overstreet bch2_chardev_exit(); 213414ba3706SKent Overstreet bch2_btree_key_cache_exit(); 21351c6fdbd8SKent Overstreet if (bcachefs_kset) 21361c6fdbd8SKent Overstreet kset_unregister(bcachefs_kset); 21371c6fdbd8SKent Overstreet } 21381c6fdbd8SKent Overstreet 21391c6fdbd8SKent Overstreet static int __init bcachefs_init(void) 21401c6fdbd8SKent Overstreet { 21411c6fdbd8SKent Overstreet bch2_bkey_pack_test(); 21421c6fdbd8SKent Overstreet 21431c6fdbd8SKent Overstreet if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) || 214414ba3706SKent Overstreet bch2_btree_key_cache_init() || 21451c6fdbd8SKent Overstreet bch2_chardev_init() || 21461c6fdbd8SKent Overstreet bch2_vfs_init() || 21471c6fdbd8SKent Overstreet bch2_debug_init()) 21481c6fdbd8SKent Overstreet goto err; 21491c6fdbd8SKent Overstreet 21501c6fdbd8SKent Overstreet return 0; 21511c6fdbd8SKent Overstreet err: 21521c6fdbd8SKent Overstreet bcachefs_exit(); 21531c6fdbd8SKent Overstreet return -ENOMEM; 21541c6fdbd8SKent Overstreet } 21551c6fdbd8SKent Overstreet 21561c6fdbd8SKent Overstreet #define BCH_DEBUG_PARAM(name, description) \ 21571c6fdbd8SKent Overstreet bool bch2_##name; \ 21581c6fdbd8SKent Overstreet module_param_named(name, bch2_##name, bool, 0644); \ 21591c6fdbd8SKent Overstreet MODULE_PARM_DESC(name, description); 21601c6fdbd8SKent Overstreet BCH_DEBUG_PARAMS() 21611c6fdbd8SKent Overstreet #undef BCH_DEBUG_PARAM 21621c6fdbd8SKent Overstreet 2163e08e63e4SKent Overstreet __maybe_unused 216473bd774dSKent Overstreet static unsigned bch2_metadata_version = bcachefs_metadata_version_current; 21651c6fdbd8SKent Overstreet module_param_named(version, bch2_metadata_version, uint, 0400); 21661c6fdbd8SKent Overstreet 21671c6fdbd8SKent Overstreet module_exit(bcachefs_exit); 21681c6fdbd8SKent Overstreet module_init(bcachefs_init); 2169