11c6fdbd8SKent Overstreet // SPDX-License-Identifier: GPL-2.0 21c6fdbd8SKent Overstreet /* 31c6fdbd8SKent Overstreet * bcachefs setup/teardown code, and some metadata io - read a superblock and 41c6fdbd8SKent Overstreet * figure out what to do with it. 51c6fdbd8SKent Overstreet * 61c6fdbd8SKent Overstreet * Copyright 2010, 2011 Kent Overstreet <kent.overstreet@gmail.com> 71c6fdbd8SKent Overstreet * Copyright 2012 Google, Inc. 81c6fdbd8SKent Overstreet */ 91c6fdbd8SKent Overstreet 101c6fdbd8SKent Overstreet #include "bcachefs.h" 117b3f84eaSKent Overstreet #include "alloc_background.h" 127b3f84eaSKent Overstreet #include "alloc_foreground.h" 135b8a9227SKent Overstreet #include "bkey_sort.h" 141c6fdbd8SKent Overstreet #include "btree_cache.h" 151c6fdbd8SKent Overstreet #include "btree_gc.h" 16401585feSKent Overstreet #include "btree_journal_iter.h" 172ca88e5aSKent Overstreet #include "btree_key_cache.h" 181c6fdbd8SKent Overstreet #include "btree_update_interior.h" 191c6fdbd8SKent Overstreet #include "btree_io.h" 20920e69bcSKent Overstreet #include "btree_write_buffer.h" 2121aec962SKent Overstreet #include "buckets_waiting_for_journal.h" 221c6fdbd8SKent Overstreet #include "chardev.h" 231c6fdbd8SKent Overstreet #include "checksum.h" 241c6fdbd8SKent Overstreet #include "clock.h" 251c6fdbd8SKent Overstreet #include "compress.h" 26104c6974SDaniel Hill #include "counters.h" 271c6fdbd8SKent Overstreet #include "debug.h" 281c6fdbd8SKent Overstreet #include "disk_groups.h" 29cd575ddfSKent Overstreet #include "ec.h" 30d4bf5eecSKent Overstreet #include "errcode.h" 311c6fdbd8SKent Overstreet #include "error.h" 321c6fdbd8SKent Overstreet #include "fs.h" 331c6fdbd8SKent Overstreet #include "fs-io.h" 34dbbfca9fSKent Overstreet #include "fs-io-buffered.h" 35dbbfca9fSKent Overstreet #include "fs-io-direct.h" 361c6fdbd8SKent Overstreet #include "fsck.h" 371c6fdbd8SKent Overstreet #include "inode.h" 381809b8cbSKent Overstreet #include "io_read.h" 391809b8cbSKent Overstreet #include "io_write.h" 401c6fdbd8SKent Overstreet #include "journal.h" 411c6fdbd8SKent Overstreet #include "journal_reclaim.h" 421dd7f9d9SKent Overstreet #include "journal_seq_blacklist.h" 431c6fdbd8SKent Overstreet #include "move.h" 441c6fdbd8SKent Overstreet #include "migrate.h" 451c6fdbd8SKent Overstreet #include "movinggc.h" 46350175bfSKent Overstreet #include "nocow_locking.h" 471c6fdbd8SKent Overstreet #include "quota.h" 481c6fdbd8SKent Overstreet #include "rebalance.h" 491c6fdbd8SKent Overstreet #include "recovery.h" 501c6fdbd8SKent Overstreet #include "replicas.h" 51a37ad1a3SKent Overstreet #include "sb-clean.h" 521241df58SHunter Shaffer #include "sb-members.h" 538e877caaSKent Overstreet #include "snapshot.h" 5414b393eeSKent Overstreet #include "subvolume.h" 551c6fdbd8SKent Overstreet #include "super.h" 561c6fdbd8SKent Overstreet #include "super-io.h" 571c6fdbd8SKent Overstreet #include "sysfs.h" 581c6fdbd8SKent Overstreet #include "trace.h" 591c6fdbd8SKent Overstreet 601c6fdbd8SKent Overstreet #include <linux/backing-dev.h> 611c6fdbd8SKent Overstreet #include <linux/blkdev.h> 621c6fdbd8SKent Overstreet #include <linux/debugfs.h> 631c6fdbd8SKent Overstreet #include <linux/device.h> 641c6fdbd8SKent Overstreet #include <linux/idr.h> 651c6fdbd8SKent Overstreet #include <linux/module.h> 661c6fdbd8SKent Overstreet #include <linux/percpu.h> 671c6fdbd8SKent Overstreet #include <linux/random.h> 681c6fdbd8SKent Overstreet #include <linux/sysfs.h> 691c6fdbd8SKent Overstreet #include <crypto/hash.h> 701c6fdbd8SKent Overstreet 711c6fdbd8SKent Overstreet MODULE_LICENSE("GPL"); 721c6fdbd8SKent Overstreet MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>"); 73a9737e0bSBrian Foster MODULE_DESCRIPTION("bcachefs filesystem"); 741c6fdbd8SKent Overstreet 751c6fdbd8SKent Overstreet #define KTYPE(type) \ 761c6fdbd8SKent Overstreet static const struct attribute_group type ## _group = { \ 771c6fdbd8SKent Overstreet .attrs = type ## _files \ 781c6fdbd8SKent Overstreet }; \ 791c6fdbd8SKent Overstreet \ 801c6fdbd8SKent Overstreet static const struct attribute_group *type ## _groups[] = { \ 811c6fdbd8SKent Overstreet &type ## _group, \ 821c6fdbd8SKent Overstreet NULL \ 831c6fdbd8SKent Overstreet }; \ 841c6fdbd8SKent Overstreet \ 851c6fdbd8SKent Overstreet static const struct kobj_type type ## _ktype = { \ 861c6fdbd8SKent Overstreet .release = type ## _release, \ 871c6fdbd8SKent Overstreet .sysfs_ops = &type ## _sysfs_ops, \ 881c6fdbd8SKent Overstreet .default_groups = type ## _groups \ 891c6fdbd8SKent Overstreet } 901c6fdbd8SKent Overstreet 911c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *); 921c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *); 93104c6974SDaniel Hill static void bch2_fs_counters_release(struct kobject *k) 94104c6974SDaniel Hill { 95104c6974SDaniel Hill } 961c6fdbd8SKent Overstreet 971c6fdbd8SKent Overstreet static void bch2_fs_internal_release(struct kobject *k) 981c6fdbd8SKent Overstreet { 991c6fdbd8SKent Overstreet } 1001c6fdbd8SKent Overstreet 1011c6fdbd8SKent Overstreet static void bch2_fs_opts_dir_release(struct kobject *k) 1021c6fdbd8SKent Overstreet { 1031c6fdbd8SKent Overstreet } 1041c6fdbd8SKent Overstreet 1051c6fdbd8SKent Overstreet static void bch2_fs_time_stats_release(struct kobject *k) 1061c6fdbd8SKent Overstreet { 1071c6fdbd8SKent Overstreet } 1081c6fdbd8SKent Overstreet 1091c6fdbd8SKent Overstreet KTYPE(bch2_fs); 110104c6974SDaniel Hill KTYPE(bch2_fs_counters); 1111c6fdbd8SKent Overstreet KTYPE(bch2_fs_internal); 1121c6fdbd8SKent Overstreet KTYPE(bch2_fs_opts_dir); 1131c6fdbd8SKent Overstreet KTYPE(bch2_fs_time_stats); 1141c6fdbd8SKent Overstreet KTYPE(bch2_dev); 1151c6fdbd8SKent Overstreet 1161c6fdbd8SKent Overstreet static struct kset *bcachefs_kset; 1171c6fdbd8SKent Overstreet static LIST_HEAD(bch_fs_list); 1181c6fdbd8SKent Overstreet static DEFINE_MUTEX(bch_fs_list_lock); 1191c6fdbd8SKent Overstreet 120d94189adSKent Overstreet DECLARE_WAIT_QUEUE_HEAD(bch2_read_only_wait); 1211c6fdbd8SKent Overstreet 1221c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *); 1231c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *, unsigned); 1241c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *); 1251c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *); 1261c6fdbd8SKent Overstreet 1271c6fdbd8SKent Overstreet struct bch_fs *bch2_dev_to_fs(dev_t dev) 1281c6fdbd8SKent Overstreet { 1291c6fdbd8SKent Overstreet struct bch_fs *c; 1301c6fdbd8SKent Overstreet struct bch_dev *ca; 1311c6fdbd8SKent Overstreet unsigned i; 1321c6fdbd8SKent Overstreet 1331c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 1341c6fdbd8SKent Overstreet rcu_read_lock(); 1351c6fdbd8SKent Overstreet 1361c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 1371c6fdbd8SKent Overstreet for_each_member_device_rcu(ca, c, i, NULL) 138ec4ab9d2SDan Robertson if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) { 1391c6fdbd8SKent Overstreet closure_get(&c->cl); 1401c6fdbd8SKent Overstreet goto found; 1411c6fdbd8SKent Overstreet } 1421c6fdbd8SKent Overstreet c = NULL; 1431c6fdbd8SKent Overstreet found: 1441c6fdbd8SKent Overstreet rcu_read_unlock(); 1451c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 1461c6fdbd8SKent Overstreet 1471c6fdbd8SKent Overstreet return c; 1481c6fdbd8SKent Overstreet } 1491c6fdbd8SKent Overstreet 1501c6fdbd8SKent Overstreet static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid) 1511c6fdbd8SKent Overstreet { 1521c6fdbd8SKent Overstreet struct bch_fs *c; 1531c6fdbd8SKent Overstreet 1541c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 1551c6fdbd8SKent Overstreet 1561c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 1571c6fdbd8SKent Overstreet if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid))) 1581c6fdbd8SKent Overstreet return c; 1591c6fdbd8SKent Overstreet 1601c6fdbd8SKent Overstreet return NULL; 1611c6fdbd8SKent Overstreet } 1621c6fdbd8SKent Overstreet 1631c6fdbd8SKent Overstreet struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid) 1641c6fdbd8SKent Overstreet { 1651c6fdbd8SKent Overstreet struct bch_fs *c; 1661c6fdbd8SKent Overstreet 1671c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 1681c6fdbd8SKent Overstreet c = __bch2_uuid_to_fs(uuid); 1691c6fdbd8SKent Overstreet if (c) 1701c6fdbd8SKent Overstreet closure_get(&c->cl); 1711c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 1721c6fdbd8SKent Overstreet 1731c6fdbd8SKent Overstreet return c; 1741c6fdbd8SKent Overstreet } 1751c6fdbd8SKent Overstreet 176180fb49dSKent Overstreet static void bch2_dev_usage_journal_reserve(struct bch_fs *c) 177180fb49dSKent Overstreet { 178180fb49dSKent Overstreet struct bch_dev *ca; 179180fb49dSKent Overstreet unsigned i, nr = 0, u64s = 1804b8f89afSKent Overstreet ((sizeof(struct jset_entry_dev_usage) + 1814b8f89afSKent Overstreet sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR)) / 1824b8f89afSKent Overstreet sizeof(u64); 183180fb49dSKent Overstreet 184180fb49dSKent Overstreet rcu_read_lock(); 185180fb49dSKent Overstreet for_each_member_device_rcu(ca, c, i, NULL) 186180fb49dSKent Overstreet nr++; 187180fb49dSKent Overstreet rcu_read_unlock(); 188180fb49dSKent Overstreet 189180fb49dSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 190180fb49dSKent Overstreet &c->dev_usage_journal_res, u64s * nr); 191180fb49dSKent Overstreet } 192180fb49dSKent Overstreet 1931c6fdbd8SKent Overstreet /* Filesystem RO/RW: */ 1941c6fdbd8SKent Overstreet 1951c6fdbd8SKent Overstreet /* 1961c6fdbd8SKent Overstreet * For startup/shutdown of RW stuff, the dependencies are: 1971c6fdbd8SKent Overstreet * 1981c6fdbd8SKent Overstreet * - foreground writes depend on copygc and rebalance (to free up space) 1991c6fdbd8SKent Overstreet * 2001c6fdbd8SKent Overstreet * - copygc and rebalance depend on mark and sweep gc (they actually probably 2011c6fdbd8SKent Overstreet * don't because they either reserve ahead of time or don't block if 2021c6fdbd8SKent Overstreet * allocations fail, but allocations can require mark and sweep gc to run 2031c6fdbd8SKent Overstreet * because of generation number wraparound) 2041c6fdbd8SKent Overstreet * 2051c6fdbd8SKent Overstreet * - all of the above depends on the allocator threads 2061c6fdbd8SKent Overstreet * 2071c6fdbd8SKent Overstreet * - allocator depends on the journal (when it rewrites prios and gens) 2081c6fdbd8SKent Overstreet */ 2091c6fdbd8SKent Overstreet 2101c6fdbd8SKent Overstreet static void __bch2_fs_read_only(struct bch_fs *c) 2111c6fdbd8SKent Overstreet { 2121c6fdbd8SKent Overstreet struct bch_dev *ca; 213d5f70c1fSKent Overstreet unsigned i, clean_passes = 0; 214c0960603SKent Overstreet u64 seq = 0; 2151c6fdbd8SKent Overstreet 216b40901b0SKent Overstreet bch2_fs_ec_stop(c); 217b40901b0SKent Overstreet bch2_open_buckets_stop(c, NULL, true); 2181c6fdbd8SKent Overstreet bch2_rebalance_stop(c); 219e6d11615SKent Overstreet bch2_copygc_stop(c); 2201c6fdbd8SKent Overstreet bch2_gc_thread_stop(c); 221b40901b0SKent Overstreet bch2_fs_ec_flush(c); 2221c6fdbd8SKent Overstreet 22383ec519aSKent Overstreet bch_verbose(c, "flushing journal and stopping allocators, journal seq %llu", 22483ec519aSKent Overstreet journal_cur_seq(&c->journal)); 2251c6fdbd8SKent Overstreet 226039fc4c5SKent Overstreet do { 227039fc4c5SKent Overstreet clean_passes++; 228039fc4c5SKent Overstreet 229c0960603SKent Overstreet if (bch2_btree_interior_updates_flush(c) || 230c0960603SKent Overstreet bch2_journal_flush_all_pins(&c->journal) || 231c0960603SKent Overstreet bch2_btree_flush_all_writes(c) || 232c0960603SKent Overstreet seq != atomic64_read(&c->journal.seq)) { 233c0960603SKent Overstreet seq = atomic64_read(&c->journal.seq); 234039fc4c5SKent Overstreet clean_passes = 0; 235039fc4c5SKent Overstreet } 236d5f70c1fSKent Overstreet } while (clean_passes < 2); 237c0960603SKent Overstreet 23883ec519aSKent Overstreet bch_verbose(c, "flushing journal and stopping allocators complete, journal seq %llu", 23983ec519aSKent Overstreet journal_cur_seq(&c->journal)); 2402340fd9dSKent Overstreet 241c0960603SKent Overstreet if (test_bit(JOURNAL_REPLAY_DONE, &c->journal.flags) && 242c0960603SKent Overstreet !test_bit(BCH_FS_EMERGENCY_RO, &c->flags)) 243c0960603SKent Overstreet set_bit(BCH_FS_CLEAN_SHUTDOWN, &c->flags); 2441c6fdbd8SKent Overstreet bch2_fs_journal_stop(&c->journal); 2451c6fdbd8SKent Overstreet 2461c6fdbd8SKent Overstreet /* 2471c6fdbd8SKent Overstreet * After stopping journal: 2481c6fdbd8SKent Overstreet */ 2491c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 2501c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 2511c6fdbd8SKent Overstreet } 2521c6fdbd8SKent Overstreet 253d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 2541c6fdbd8SKent Overstreet static void bch2_writes_disabled(struct percpu_ref *writes) 2551c6fdbd8SKent Overstreet { 2561c6fdbd8SKent Overstreet struct bch_fs *c = container_of(writes, struct bch_fs, writes); 2571c6fdbd8SKent Overstreet 2581c6fdbd8SKent Overstreet set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags); 259d94189adSKent Overstreet wake_up(&bch2_read_only_wait); 2601c6fdbd8SKent Overstreet } 261d94189adSKent Overstreet #endif 2621c6fdbd8SKent Overstreet 2631c6fdbd8SKent Overstreet void bch2_fs_read_only(struct bch_fs *c) 2641c6fdbd8SKent Overstreet { 265134915f3SKent Overstreet if (!test_bit(BCH_FS_RW, &c->flags)) { 2669ae28f82SKent Overstreet bch2_journal_reclaim_stop(&c->journal); 2671c6fdbd8SKent Overstreet return; 268134915f3SKent Overstreet } 2691c6fdbd8SKent Overstreet 2701c6fdbd8SKent Overstreet BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags)); 2711c6fdbd8SKent Overstreet 2721c6fdbd8SKent Overstreet /* 2731c6fdbd8SKent Overstreet * Block new foreground-end write operations from starting - any new 2741c6fdbd8SKent Overstreet * writes will return -EROFS: 2751c6fdbd8SKent Overstreet */ 276d94189adSKent Overstreet set_bit(BCH_FS_GOING_RO, &c->flags); 277d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 2781c6fdbd8SKent Overstreet percpu_ref_kill(&c->writes); 279d94189adSKent Overstreet #else 280d94189adSKent Overstreet for (unsigned i = 0; i < BCH_WRITE_REF_NR; i++) 281d94189adSKent Overstreet bch2_write_ref_put(c, i); 282d94189adSKent Overstreet #endif 2831c6fdbd8SKent Overstreet 2841c6fdbd8SKent Overstreet /* 2851c6fdbd8SKent Overstreet * If we're not doing an emergency shutdown, we want to wait on 2861c6fdbd8SKent Overstreet * outstanding writes to complete so they don't see spurious errors due 2871c6fdbd8SKent Overstreet * to shutting down the allocator: 2881c6fdbd8SKent Overstreet * 2891c6fdbd8SKent Overstreet * If we are doing an emergency shutdown outstanding writes may 2901c6fdbd8SKent Overstreet * hang until we shutdown the allocator so we don't want to wait 2911c6fdbd8SKent Overstreet * on outstanding writes before shutting everything down - but 2921c6fdbd8SKent Overstreet * we do need to wait on them before returning and signalling 2931c6fdbd8SKent Overstreet * that going RO is complete: 2941c6fdbd8SKent Overstreet */ 295d94189adSKent Overstreet wait_event(bch2_read_only_wait, 2961c6fdbd8SKent Overstreet test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) || 2971c6fdbd8SKent Overstreet test_bit(BCH_FS_EMERGENCY_RO, &c->flags)); 2981c6fdbd8SKent Overstreet 2991c6fdbd8SKent Overstreet __bch2_fs_read_only(c); 3001c6fdbd8SKent Overstreet 301d94189adSKent Overstreet wait_event(bch2_read_only_wait, 3021c6fdbd8SKent Overstreet test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags)); 3031c6fdbd8SKent Overstreet 3041c6fdbd8SKent Overstreet clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags); 305d94189adSKent Overstreet clear_bit(BCH_FS_GOING_RO, &c->flags); 3061c6fdbd8SKent Overstreet 3071c6fdbd8SKent Overstreet if (!bch2_journal_error(&c->journal) && 3081c6fdbd8SKent Overstreet !test_bit(BCH_FS_ERROR, &c->flags) && 3093aea4342SKent Overstreet !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) && 310a0e0bda1SKent Overstreet test_bit(BCH_FS_STARTED, &c->flags) && 311c0960603SKent Overstreet test_bit(BCH_FS_CLEAN_SHUTDOWN, &c->flags) && 312b2930396SKent Overstreet !c->opts.norecovery) { 3137724664fSKent Overstreet BUG_ON(c->journal.last_empty_seq != journal_cur_seq(&c->journal)); 3147724664fSKent Overstreet BUG_ON(atomic_read(&c->btree_cache.dirty)); 3157724664fSKent Overstreet BUG_ON(atomic_long_read(&c->btree_key_cache.nr_dirty)); 3167724664fSKent Overstreet BUG_ON(c->btree_write_buffer.state.nr); 3177724664fSKent Overstreet 318b2930396SKent Overstreet bch_verbose(c, "marking filesystem clean"); 319134915f3SKent Overstreet bch2_fs_mark_clean(c); 320b2930396SKent Overstreet } 3211c6fdbd8SKent Overstreet 322134915f3SKent Overstreet clear_bit(BCH_FS_RW, &c->flags); 3231c6fdbd8SKent Overstreet } 3241c6fdbd8SKent Overstreet 3251c6fdbd8SKent Overstreet static void bch2_fs_read_only_work(struct work_struct *work) 3261c6fdbd8SKent Overstreet { 3271c6fdbd8SKent Overstreet struct bch_fs *c = 3281c6fdbd8SKent Overstreet container_of(work, struct bch_fs, read_only_work); 3291c6fdbd8SKent Overstreet 3301ada1606SKent Overstreet down_write(&c->state_lock); 3311c6fdbd8SKent Overstreet bch2_fs_read_only(c); 3321ada1606SKent Overstreet up_write(&c->state_lock); 3331c6fdbd8SKent Overstreet } 3341c6fdbd8SKent Overstreet 3351c6fdbd8SKent Overstreet static void bch2_fs_read_only_async(struct bch_fs *c) 3361c6fdbd8SKent Overstreet { 3371c6fdbd8SKent Overstreet queue_work(system_long_wq, &c->read_only_work); 3381c6fdbd8SKent Overstreet } 3391c6fdbd8SKent Overstreet 3401c6fdbd8SKent Overstreet bool bch2_fs_emergency_read_only(struct bch_fs *c) 3411c6fdbd8SKent Overstreet { 3421c6fdbd8SKent Overstreet bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags); 3431c6fdbd8SKent Overstreet 3441c6fdbd8SKent Overstreet bch2_journal_halt(&c->journal); 3459f115ce9SKent Overstreet bch2_fs_read_only_async(c); 3461c6fdbd8SKent Overstreet 347d94189adSKent Overstreet wake_up(&bch2_read_only_wait); 3481c6fdbd8SKent Overstreet return ret; 3491c6fdbd8SKent Overstreet } 3501c6fdbd8SKent Overstreet 351134915f3SKent Overstreet static int bch2_fs_read_write_late(struct bch_fs *c) 3521c6fdbd8SKent Overstreet { 353134915f3SKent Overstreet int ret; 3541c6fdbd8SKent Overstreet 355ea28c867SKent Overstreet /* 356ea28c867SKent Overstreet * Data move operations can't run until after check_snapshots has 357ea28c867SKent Overstreet * completed, and bch2_snapshot_is_ancestor() is available. 358ea28c867SKent Overstreet * 359ea28c867SKent Overstreet * Ideally we'd start copygc/rebalance earlier instead of waiting for 360ea28c867SKent Overstreet * all of recovery/fsck to complete: 361ea28c867SKent Overstreet */ 362ea28c867SKent Overstreet ret = bch2_copygc_start(c); 363ea28c867SKent Overstreet if (ret) { 364ea28c867SKent Overstreet bch_err(c, "error starting copygc thread"); 365ea28c867SKent Overstreet return ret; 366ea28c867SKent Overstreet } 367ea28c867SKent Overstreet 368134915f3SKent Overstreet ret = bch2_rebalance_start(c); 369134915f3SKent Overstreet if (ret) { 370134915f3SKent Overstreet bch_err(c, "error starting rebalance thread"); 371134915f3SKent Overstreet return ret; 372134915f3SKent Overstreet } 373134915f3SKent Overstreet 374134915f3SKent Overstreet return 0; 375134915f3SKent Overstreet } 376134915f3SKent Overstreet 377e731d466SKent Overstreet static int __bch2_fs_read_write(struct bch_fs *c, bool early) 378134915f3SKent Overstreet { 379134915f3SKent Overstreet struct bch_dev *ca; 380134915f3SKent Overstreet unsigned i; 381134915f3SKent Overstreet int ret; 382134915f3SKent Overstreet 383aae15aafSKent Overstreet if (test_bit(BCH_FS_INITIAL_GC_UNFIXED, &c->flags)) { 384aae15aafSKent Overstreet bch_err(c, "cannot go rw, unfixed btree errors"); 3857c50140fSKent Overstreet return -BCH_ERR_erofs_unfixed_errors; 386aae15aafSKent Overstreet } 387aae15aafSKent Overstreet 388134915f3SKent Overstreet if (test_bit(BCH_FS_RW, &c->flags)) 389134915f3SKent Overstreet return 0; 390134915f3SKent Overstreet 3917c50140fSKent Overstreet if (c->opts.norecovery) 3927c50140fSKent Overstreet return -BCH_ERR_erofs_norecovery; 3937c50140fSKent Overstreet 394619f5beeSKent Overstreet /* 395619f5beeSKent Overstreet * nochanges is used for fsck -n mode - we have to allow going rw 396619f5beeSKent Overstreet * during recovery for that to work: 397619f5beeSKent Overstreet */ 3987c50140fSKent Overstreet if (c->opts.nochanges && (!early || c->opts.read_only)) 3997c50140fSKent Overstreet return -BCH_ERR_erofs_nochanges; 400330581f1SKent Overstreet 4012c944fa1SKent Overstreet bch_info(c, "going read-write"); 4022c944fa1SKent Overstreet 4033f7b9713SHunter Shaffer ret = bch2_members_v2_init(c); 4043f7b9713SHunter Shaffer if (ret) 4053f7b9713SHunter Shaffer goto err; 4063f7b9713SHunter Shaffer 407134915f3SKent Overstreet ret = bch2_fs_mark_dirty(c); 408134915f3SKent Overstreet if (ret) 409134915f3SKent Overstreet goto err; 4101c6fdbd8SKent Overstreet 411c0960603SKent Overstreet clear_bit(BCH_FS_CLEAN_SHUTDOWN, &c->flags); 4122340fd9dSKent Overstreet 413b9004e85SKent Overstreet /* 414b9004e85SKent Overstreet * First journal write must be a flush write: after a clean shutdown we 415b9004e85SKent Overstreet * don't read the journal, so the first journal write may end up 416b9004e85SKent Overstreet * overwriting whatever was there previously, and there must always be 417b9004e85SKent Overstreet * at least one non-flush write in the journal or recovery will fail: 418b9004e85SKent Overstreet */ 419b9004e85SKent Overstreet set_bit(JOURNAL_NEED_FLUSH_WRITE, &c->journal.flags); 420b9004e85SKent Overstreet 4211c6fdbd8SKent Overstreet for_each_rw_member(ca, c, i) 4221c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 4231c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 4241c6fdbd8SKent Overstreet 4255f5c7466SKent Overstreet ret = bch2_gc_thread_start(c); 4265f5c7466SKent Overstreet if (ret) { 4275f5c7466SKent Overstreet bch_err(c, "error starting gc thread"); 4285f5c7466SKent Overstreet return ret; 4295f5c7466SKent Overstreet } 4305f5c7466SKent Overstreet 431197763a7SBrian Foster ret = bch2_journal_reclaim_start(&c->journal); 432197763a7SBrian Foster if (ret) 433197763a7SBrian Foster goto err; 434197763a7SBrian Foster 435134915f3SKent Overstreet if (!early) { 436134915f3SKent Overstreet ret = bch2_fs_read_write_late(c); 437134915f3SKent Overstreet if (ret) 4381c6fdbd8SKent Overstreet goto err; 4391c6fdbd8SKent Overstreet } 4401c6fdbd8SKent Overstreet 441d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 4421c6fdbd8SKent Overstreet percpu_ref_reinit(&c->writes); 443d94189adSKent Overstreet #else 44496dea3d5SKent Overstreet for (i = 0; i < BCH_WRITE_REF_NR; i++) { 445d94189adSKent Overstreet BUG_ON(atomic_long_read(&c->writes[i])); 446d94189adSKent Overstreet atomic_long_inc(&c->writes[i]); 447d94189adSKent Overstreet } 448d94189adSKent Overstreet #endif 449134915f3SKent Overstreet set_bit(BCH_FS_RW, &c->flags); 4504932e07eSKent Overstreet set_bit(BCH_FS_WAS_RW, &c->flags); 451dd81a060SKent Overstreet 452dd81a060SKent Overstreet bch2_do_discards(c); 453dd81a060SKent Overstreet bch2_do_invalidates(c); 454dd81a060SKent Overstreet bch2_do_stripe_deletes(c); 455a1f26d70SKent Overstreet bch2_do_pending_node_rewrites(c); 456134915f3SKent Overstreet return 0; 4571c6fdbd8SKent Overstreet err: 4581c6fdbd8SKent Overstreet __bch2_fs_read_only(c); 459134915f3SKent Overstreet return ret; 460134915f3SKent Overstreet } 461134915f3SKent Overstreet 462134915f3SKent Overstreet int bch2_fs_read_write(struct bch_fs *c) 463134915f3SKent Overstreet { 464134915f3SKent Overstreet return __bch2_fs_read_write(c, false); 465134915f3SKent Overstreet } 466134915f3SKent Overstreet 467134915f3SKent Overstreet int bch2_fs_read_write_early(struct bch_fs *c) 468134915f3SKent Overstreet { 469134915f3SKent Overstreet lockdep_assert_held(&c->state_lock); 470134915f3SKent Overstreet 471134915f3SKent Overstreet return __bch2_fs_read_write(c, true); 4721c6fdbd8SKent Overstreet } 4731c6fdbd8SKent Overstreet 4741c6fdbd8SKent Overstreet /* Filesystem startup/shutdown: */ 4751c6fdbd8SKent Overstreet 476d5e4dcc2SKent Overstreet static void __bch2_fs_free(struct bch_fs *c) 4771c6fdbd8SKent Overstreet { 4781c6fdbd8SKent Overstreet unsigned i; 4791c6fdbd8SKent Overstreet 4801c6fdbd8SKent Overstreet for (i = 0; i < BCH_TIME_STAT_NR; i++) 4811c6fdbd8SKent Overstreet bch2_time_stats_exit(&c->times[i]); 4821c6fdbd8SKent Overstreet 483a1f26d70SKent Overstreet bch2_free_pending_node_rewrites(c); 484104c6974SDaniel Hill bch2_fs_counters_exit(c); 48514b393eeSKent Overstreet bch2_fs_snapshots_exit(c); 4861c6fdbd8SKent Overstreet bch2_fs_quota_exit(c); 487dbbfca9fSKent Overstreet bch2_fs_fs_io_direct_exit(c); 488dbbfca9fSKent Overstreet bch2_fs_fs_io_buffered_exit(c); 4891c6fdbd8SKent Overstreet bch2_fs_fsio_exit(c); 490cd575ddfSKent Overstreet bch2_fs_ec_exit(c); 4911c6fdbd8SKent Overstreet bch2_fs_encryption_exit(c); 4921e3b4098SKent Overstreet bch2_fs_nocow_locking_exit(c); 4931809b8cbSKent Overstreet bch2_fs_io_write_exit(c); 4941809b8cbSKent Overstreet bch2_fs_io_read_exit(c); 49521aec962SKent Overstreet bch2_fs_buckets_waiting_for_journal_exit(c); 496c823c339SKent Overstreet bch2_fs_btree_interior_update_exit(c); 49736e9d698SKent Overstreet bch2_fs_btree_iter_exit(c); 4982ca88e5aSKent Overstreet bch2_fs_btree_key_cache_exit(&c->btree_key_cache); 4991c6fdbd8SKent Overstreet bch2_fs_btree_cache_exit(c); 5009620c3ecSKent Overstreet bch2_fs_replicas_exit(c); 5011c6fdbd8SKent Overstreet bch2_fs_journal_exit(&c->journal); 5021c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[WRITE]); 5031c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[READ]); 5041c6fdbd8SKent Overstreet bch2_fs_compress_exit(c); 505f1d786a0SKent Overstreet bch2_journal_keys_free(&c->journal_keys); 506ce6201c4SKent Overstreet bch2_journal_entries_free(c); 507920e69bcSKent Overstreet bch2_fs_btree_write_buffer_exit(c); 5089166b41dSKent Overstreet percpu_free_rwsem(&c->mark_lock); 5095e82a9a1SKent Overstreet free_percpu(c->online_reserved); 5101a21bf98SKent Overstreet 511faa6cb6cSKent Overstreet darray_exit(&c->btree_roots_extra); 5125663a415SKent Overstreet free_percpu(c->pcpu); 51335189e09SKent Overstreet mempool_exit(&c->large_bkey_pool); 5141c6fdbd8SKent Overstreet mempool_exit(&c->btree_bounce_pool); 5151c6fdbd8SKent Overstreet bioset_exit(&c->btree_bio); 5161c6fdbd8SKent Overstreet mempool_exit(&c->fill_iter); 517d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 5181c6fdbd8SKent Overstreet percpu_ref_exit(&c->writes); 519d94189adSKent Overstreet #endif 5201c6fdbd8SKent Overstreet kfree(rcu_dereference_protected(c->disk_groups, 1)); 5211dd7f9d9SKent Overstreet kfree(c->journal_seq_blacklist_table); 522b5e8a699SKent Overstreet kfree(c->unused_inode_hints); 5231c6fdbd8SKent Overstreet 5248bff9875SBrian Foster if (c->write_ref_wq) 5258bff9875SBrian Foster destroy_workqueue(c->write_ref_wq); 526731bdd2eSKent Overstreet if (c->io_complete_wq) 527731bdd2eSKent Overstreet destroy_workqueue(c->io_complete_wq); 5281c6fdbd8SKent Overstreet if (c->copygc_wq) 5291c6fdbd8SKent Overstreet destroy_workqueue(c->copygc_wq); 5309f1833caSKent Overstreet if (c->btree_io_complete_wq) 5319f1833caSKent Overstreet destroy_workqueue(c->btree_io_complete_wq); 532731bdd2eSKent Overstreet if (c->btree_update_wq) 533731bdd2eSKent Overstreet destroy_workqueue(c->btree_update_wq); 5341c6fdbd8SKent Overstreet 5359d8022dbSKent Overstreet bch2_free_super(&c->disk_sb); 5361c6fdbd8SKent Overstreet kvpfree(c, sizeof(*c)); 5371c6fdbd8SKent Overstreet module_put(THIS_MODULE); 5381c6fdbd8SKent Overstreet } 5391c6fdbd8SKent Overstreet 5401c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *kobj) 5411c6fdbd8SKent Overstreet { 5421c6fdbd8SKent Overstreet struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); 5431c6fdbd8SKent Overstreet 544d5e4dcc2SKent Overstreet __bch2_fs_free(c); 5451c6fdbd8SKent Overstreet } 5461c6fdbd8SKent Overstreet 547d5e4dcc2SKent Overstreet void __bch2_fs_stop(struct bch_fs *c) 5481c6fdbd8SKent Overstreet { 5491c6fdbd8SKent Overstreet struct bch_dev *ca; 5501c6fdbd8SKent Overstreet unsigned i; 5511c6fdbd8SKent Overstreet 552af1c6871SKent Overstreet bch_verbose(c, "shutting down"); 553af1c6871SKent Overstreet 5541dd7f9d9SKent Overstreet set_bit(BCH_FS_STOPPING, &c->flags); 5551dd7f9d9SKent Overstreet 5569b6e2f1eSKent Overstreet cancel_work_sync(&c->journal_seq_blacklist_gc_work); 5579b6e2f1eSKent Overstreet 5581ada1606SKent Overstreet down_write(&c->state_lock); 559883f1a7cSKent Overstreet bch2_fs_read_only(c); 5601ada1606SKent Overstreet up_write(&c->state_lock); 561883f1a7cSKent Overstreet 5621c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 5631c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs && 5641c6fdbd8SKent Overstreet ca->disk_sb.bdev) 5651c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 5661c6fdbd8SKent Overstreet 5671c6fdbd8SKent Overstreet if (c->kobj.state_in_sysfs) 5681c6fdbd8SKent Overstreet kobject_del(&c->kobj); 5691c6fdbd8SKent Overstreet 5701c6fdbd8SKent Overstreet bch2_fs_debug_exit(c); 5711c6fdbd8SKent Overstreet bch2_fs_chardev_exit(c); 5721c6fdbd8SKent Overstreet 573104c6974SDaniel Hill kobject_put(&c->counters_kobj); 5741c6fdbd8SKent Overstreet kobject_put(&c->time_stats); 5751c6fdbd8SKent Overstreet kobject_put(&c->opts_dir); 5761c6fdbd8SKent Overstreet kobject_put(&c->internal); 5771c6fdbd8SKent Overstreet 5781c6fdbd8SKent Overstreet /* btree prefetch might have kicked off reads in the background: */ 5791c6fdbd8SKent Overstreet bch2_btree_flush_all_reads(c); 5801c6fdbd8SKent Overstreet 5811c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 5821c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 5831c6fdbd8SKent Overstreet 5841c6fdbd8SKent Overstreet cancel_work_sync(&c->read_only_work); 585d5e4dcc2SKent Overstreet } 5861c6fdbd8SKent Overstreet 587d5e4dcc2SKent Overstreet void bch2_fs_free(struct bch_fs *c) 588d5e4dcc2SKent Overstreet { 589d5e4dcc2SKent Overstreet unsigned i; 590d5e4dcc2SKent Overstreet 591d5e4dcc2SKent Overstreet mutex_lock(&bch_fs_list_lock); 592d5e4dcc2SKent Overstreet list_del(&c->list); 593d5e4dcc2SKent Overstreet mutex_unlock(&bch_fs_list_lock); 594d5e4dcc2SKent Overstreet 595d5e4dcc2SKent Overstreet closure_sync(&c->cl); 596d5e4dcc2SKent Overstreet closure_debug_destroy(&c->cl); 597d5e4dcc2SKent Overstreet 598d5e4dcc2SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) { 599d5e4dcc2SKent Overstreet struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true); 600d5e4dcc2SKent Overstreet 601d5e4dcc2SKent Overstreet if (ca) { 602d5e4dcc2SKent Overstreet bch2_free_super(&ca->disk_sb); 603d5e4dcc2SKent Overstreet bch2_dev_free(ca); 604d5e4dcc2SKent Overstreet } 605d5e4dcc2SKent Overstreet } 6061c6fdbd8SKent Overstreet 607af1c6871SKent Overstreet bch_verbose(c, "shutdown complete"); 608af1c6871SKent Overstreet 6091c6fdbd8SKent Overstreet kobject_put(&c->kobj); 6101c6fdbd8SKent Overstreet } 6111c6fdbd8SKent Overstreet 612d5e4dcc2SKent Overstreet void bch2_fs_stop(struct bch_fs *c) 613d5e4dcc2SKent Overstreet { 614d5e4dcc2SKent Overstreet __bch2_fs_stop(c); 615d5e4dcc2SKent Overstreet bch2_fs_free(c); 616d5e4dcc2SKent Overstreet } 617d5e4dcc2SKent Overstreet 618e2b60560SKent Overstreet static int bch2_fs_online(struct bch_fs *c) 6191c6fdbd8SKent Overstreet { 6201c6fdbd8SKent Overstreet struct bch_dev *ca; 6211c6fdbd8SKent Overstreet unsigned i; 622e2b60560SKent Overstreet int ret = 0; 6231c6fdbd8SKent Overstreet 6241c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 6251c6fdbd8SKent Overstreet 626e2b60560SKent Overstreet if (__bch2_uuid_to_fs(c->sb.uuid)) { 627e2b60560SKent Overstreet bch_err(c, "filesystem UUID already open"); 628e2b60560SKent Overstreet return -EINVAL; 629e2b60560SKent Overstreet } 6301c6fdbd8SKent Overstreet 6311c6fdbd8SKent Overstreet ret = bch2_fs_chardev_init(c); 632e2b60560SKent Overstreet if (ret) { 633e2b60560SKent Overstreet bch_err(c, "error creating character device"); 634e2b60560SKent Overstreet return ret; 635e2b60560SKent Overstreet } 6361c6fdbd8SKent Overstreet 6371c6fdbd8SKent Overstreet bch2_fs_debug_init(c); 6381c6fdbd8SKent Overstreet 639e2b60560SKent Overstreet ret = kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ?: 640e2b60560SKent Overstreet kobject_add(&c->internal, &c->kobj, "internal") ?: 641e2b60560SKent Overstreet kobject_add(&c->opts_dir, &c->kobj, "options") ?: 642e2b60560SKent Overstreet kobject_add(&c->time_stats, &c->kobj, "time_stats") ?: 643104c6974SDaniel Hill kobject_add(&c->counters_kobj, &c->kobj, "counters") ?: 644e2b60560SKent Overstreet bch2_opts_create_sysfs_files(&c->opts_dir); 645e2b60560SKent Overstreet if (ret) { 646e2b60560SKent Overstreet bch_err(c, "error creating sysfs objects"); 647e2b60560SKent Overstreet return ret; 648e2b60560SKent Overstreet } 6491c6fdbd8SKent Overstreet 6501ada1606SKent Overstreet down_write(&c->state_lock); 6511c6fdbd8SKent Overstreet 652e2b60560SKent Overstreet for_each_member_device(ca, c, i) { 653e2b60560SKent Overstreet ret = bch2_dev_sysfs_online(c, ca); 654e2b60560SKent Overstreet if (ret) { 655e2b60560SKent Overstreet bch_err(c, "error creating sysfs objects"); 6563a402c8dSKent Overstreet percpu_ref_put(&ca->ref); 6571c6fdbd8SKent Overstreet goto err; 6583a402c8dSKent Overstreet } 659e2b60560SKent Overstreet } 6601c6fdbd8SKent Overstreet 661e2b60560SKent Overstreet BUG_ON(!list_empty(&c->list)); 6621c6fdbd8SKent Overstreet list_add(&c->list, &bch_fs_list); 6631c6fdbd8SKent Overstreet err: 6641ada1606SKent Overstreet up_write(&c->state_lock); 665e2b60560SKent Overstreet return ret; 6661c6fdbd8SKent Overstreet } 6671c6fdbd8SKent Overstreet 6681c6fdbd8SKent Overstreet static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts) 6691c6fdbd8SKent Overstreet { 6701c6fdbd8SKent Overstreet struct bch_fs *c; 671401ec4dbSKent Overstreet struct printbuf name = PRINTBUF; 672ecf37a4aSKent Overstreet unsigned i, iter_size; 6737be9ab63SChris Webb int ret = 0; 6741c6fdbd8SKent Overstreet 6751c6fdbd8SKent Overstreet c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO); 6767be9ab63SChris Webb if (!c) { 67765d48e35SKent Overstreet c = ERR_PTR(-BCH_ERR_ENOMEM_fs_alloc); 6781c6fdbd8SKent Overstreet goto out; 6797be9ab63SChris Webb } 6801c6fdbd8SKent Overstreet 6811c6fdbd8SKent Overstreet __module_get(THIS_MODULE); 6821c6fdbd8SKent Overstreet 683505b7a4cSKent Overstreet closure_init(&c->cl, NULL); 684505b7a4cSKent Overstreet 685505b7a4cSKent Overstreet c->kobj.kset = bcachefs_kset; 686505b7a4cSKent Overstreet kobject_init(&c->kobj, &bch2_fs_ktype); 687505b7a4cSKent Overstreet kobject_init(&c->internal, &bch2_fs_internal_ktype); 688505b7a4cSKent Overstreet kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype); 689505b7a4cSKent Overstreet kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype); 690104c6974SDaniel Hill kobject_init(&c->counters_kobj, &bch2_fs_counters_ktype); 691505b7a4cSKent Overstreet 6921c6fdbd8SKent Overstreet c->minor = -1; 6931c6fdbd8SKent Overstreet c->disk_sb.fs_sb = true; 6941c6fdbd8SKent Overstreet 6951ada1606SKent Overstreet init_rwsem(&c->state_lock); 6961c6fdbd8SKent Overstreet mutex_init(&c->sb_lock); 6971c6fdbd8SKent Overstreet mutex_init(&c->replicas_gc_lock); 6981c6fdbd8SKent Overstreet mutex_init(&c->btree_root_lock); 6991c6fdbd8SKent Overstreet INIT_WORK(&c->read_only_work, bch2_fs_read_only_work); 7001c6fdbd8SKent Overstreet 7011c6fdbd8SKent Overstreet init_rwsem(&c->gc_lock); 702c45c8667SKent Overstreet mutex_init(&c->gc_gens_lock); 7031c6fdbd8SKent Overstreet 7041c6fdbd8SKent Overstreet for (i = 0; i < BCH_TIME_STAT_NR; i++) 7051c6fdbd8SKent Overstreet bch2_time_stats_init(&c->times[i]); 7061c6fdbd8SKent Overstreet 707e6d11615SKent Overstreet bch2_fs_copygc_init(c); 7082ca88e5aSKent Overstreet bch2_fs_btree_key_cache_init_early(&c->btree_key_cache); 70965db6049SKent Overstreet bch2_fs_btree_interior_update_init_early(c); 710b092daddSKent Overstreet bch2_fs_allocator_background_init(c); 711b092daddSKent Overstreet bch2_fs_allocator_foreground_init(c); 7121c6fdbd8SKent Overstreet bch2_fs_rebalance_init(c); 7131c6fdbd8SKent Overstreet bch2_fs_quota_init(c); 71484c72755SKent Overstreet bch2_fs_ec_init_early(c); 715b9fa375bSKent Overstreet bch2_fs_move_init(c); 7161c6fdbd8SKent Overstreet 7171c6fdbd8SKent Overstreet INIT_LIST_HEAD(&c->list); 7181c6fdbd8SKent Overstreet 7194d8100daSKent Overstreet mutex_init(&c->usage_scratch_lock); 7204d8100daSKent Overstreet 7211c6fdbd8SKent Overstreet mutex_init(&c->bio_bounce_pages_lock); 72214b393eeSKent Overstreet mutex_init(&c->snapshot_table_lock); 7231c6fdbd8SKent Overstreet 7241c6fdbd8SKent Overstreet spin_lock_init(&c->btree_write_error_lock); 7251c6fdbd8SKent Overstreet 7269b6e2f1eSKent Overstreet INIT_WORK(&c->journal_seq_blacklist_gc_work, 7279b6e2f1eSKent Overstreet bch2_blacklist_entries_gc); 7289b6e2f1eSKent Overstreet 7295b593ee1SKent Overstreet INIT_LIST_HEAD(&c->journal_iters); 730f1d786a0SKent Overstreet 7311c6fdbd8SKent Overstreet INIT_LIST_HEAD(&c->fsck_errors); 7321c6fdbd8SKent Overstreet mutex_init(&c->fsck_error_lock); 7331c6fdbd8SKent Overstreet 7341c6fdbd8SKent Overstreet seqcount_init(&c->gc_pos_lock); 7351c6fdbd8SKent Overstreet 7365e82a9a1SKent Overstreet seqcount_init(&c->usage_lock); 7375e82a9a1SKent Overstreet 738ef1b2092SKent Overstreet sema_init(&c->io_in_flight, 128); 739ef1b2092SKent Overstreet 7409edbcc72SKent Overstreet INIT_LIST_HEAD(&c->vfs_inodes_list); 7419edbcc72SKent Overstreet mutex_init(&c->vfs_inodes_lock); 7429edbcc72SKent Overstreet 7431c6fdbd8SKent Overstreet c->copy_gc_enabled = 1; 7441c6fdbd8SKent Overstreet c->rebalance.enabled = 1; 7451c6fdbd8SKent Overstreet c->promote_whole_extents = true; 7461c6fdbd8SKent Overstreet 747991ba021SKent Overstreet c->journal.flush_write_time = &c->times[BCH_TIME_journal_flush_write]; 748991ba021SKent Overstreet c->journal.noflush_write_time = &c->times[BCH_TIME_journal_noflush_write]; 74949a67206SKent Overstreet c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal]; 7501c6fdbd8SKent Overstreet c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq]; 7511c6fdbd8SKent Overstreet 7521c6fdbd8SKent Overstreet bch2_fs_btree_cache_init_early(&c->btree_cache); 7531c6fdbd8SKent Overstreet 754fca1223cSKent Overstreet mutex_init(&c->sectors_available_lock); 755fca1223cSKent Overstreet 756e2b60560SKent Overstreet ret = percpu_init_rwsem(&c->mark_lock); 757e2b60560SKent Overstreet if (ret) 75873e6ab95SKent Overstreet goto err; 75973e6ab95SKent Overstreet 7601c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 761e2b60560SKent Overstreet ret = bch2_sb_to_fs(c, sb); 7621c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 763e2b60560SKent Overstreet 764e2b60560SKent Overstreet if (ret) 7651c6fdbd8SKent Overstreet goto err; 7661c6fdbd8SKent Overstreet 767401ec4dbSKent Overstreet pr_uuid(&name, c->sb.user_uuid.b); 7683e3e02e6SKent Overstreet strscpy(c->name, name.buf, sizeof(c->name)); 769401ec4dbSKent Overstreet printbuf_exit(&name); 770401ec4dbSKent Overstreet 77165d48e35SKent Overstreet ret = name.allocation_failure ? -BCH_ERR_ENOMEM_fs_name_alloc : 0; 772401ec4dbSKent Overstreet if (ret) 773401ec4dbSKent Overstreet goto err; 7741c6fdbd8SKent Overstreet 7752430e72fSKent Overstreet /* Compat: */ 77673bd774dSKent Overstreet if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && 7772430e72fSKent Overstreet !BCH_SB_JOURNAL_FLUSH_DELAY(sb)) 7782430e72fSKent Overstreet SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000); 7792430e72fSKent Overstreet 78073bd774dSKent Overstreet if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && 7812430e72fSKent Overstreet !BCH_SB_JOURNAL_RECLAIM_DELAY(sb)) 7822430e72fSKent Overstreet SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 100); 7832430e72fSKent Overstreet 7841c6fdbd8SKent Overstreet c->opts = bch2_opts_default; 7858244f320SKent Overstreet ret = bch2_opts_from_sb(&c->opts, sb); 7868244f320SKent Overstreet if (ret) 7878244f320SKent Overstreet goto err; 7888244f320SKent Overstreet 7891c6fdbd8SKent Overstreet bch2_opts_apply(&c->opts, opts); 7901c6fdbd8SKent Overstreet 7917c8f6f98SKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_alloc; 7927c8f6f98SKent Overstreet if (c->opts.inodes_use_key_cache) 7937c8f6f98SKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_inodes; 794aaad530aSKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_logged_ops; 7957c8f6f98SKent Overstreet 7968244f320SKent Overstreet c->block_bits = ilog2(block_sectors(c)); 7971c6fdbd8SKent Overstreet c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c); 7981c6fdbd8SKent Overstreet 7997be9ab63SChris Webb if (bch2_fs_init_fault("fs_alloc")) { 800e2b60560SKent Overstreet bch_err(c, "fs_alloc fault injected"); 801e2b60560SKent Overstreet ret = -EFAULT; 8021c6fdbd8SKent Overstreet goto err; 8037be9ab63SChris Webb } 8041c6fdbd8SKent Overstreet 805ae2f17d5SKent Overstreet iter_size = sizeof(struct sort_iter) + 8061c6fdbd8SKent Overstreet (btree_blocks(c) + 1) * 2 * 807ae2f17d5SKent Overstreet sizeof(struct sort_iter_set); 8081c6fdbd8SKent Overstreet 809b5e8a699SKent Overstreet c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus())); 810b5e8a699SKent Overstreet 811731bdd2eSKent Overstreet if (!(c->btree_update_wq = alloc_workqueue("bcachefs", 812b17d3cecSKent Overstreet WQ_FREEZABLE|WQ_UNBOUND|WQ_MEM_RECLAIM, 512)) || 8139f1833caSKent Overstreet !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io", 8149f2772c4SKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) || 8152f33ece9SKent Overstreet !(c->copygc_wq = alloc_workqueue("bcachefs_copygc", 8162f33ece9SKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) || 817731bdd2eSKent Overstreet !(c->io_complete_wq = alloc_workqueue("bcachefs_io", 818731bdd2eSKent Overstreet WQ_FREEZABLE|WQ_HIGHPRI|WQ_MEM_RECLAIM, 1)) || 8198bff9875SBrian Foster !(c->write_ref_wq = alloc_workqueue("bcachefs_write_ref", 8208bff9875SBrian Foster WQ_FREEZABLE, 0)) || 821d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 822134915f3SKent Overstreet percpu_ref_init(&c->writes, bch2_writes_disabled, 823134915f3SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 824d94189adSKent Overstreet #endif 8251c6fdbd8SKent Overstreet mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) || 8261c6fdbd8SKent Overstreet bioset_init(&c->btree_bio, 1, 8271c6fdbd8SKent Overstreet max(offsetof(struct btree_read_bio, bio), 8281c6fdbd8SKent Overstreet offsetof(struct btree_write_bio, wbio.bio)), 8291c6fdbd8SKent Overstreet BIOSET_NEED_BVECS) || 8305663a415SKent Overstreet !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) || 8315e82a9a1SKent Overstreet !(c->online_reserved = alloc_percpu(u64)) || 8321c6fdbd8SKent Overstreet mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1, 8331c6fdbd8SKent Overstreet btree_bytes(c)) || 83435189e09SKent Overstreet mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) || 835b5e8a699SKent Overstreet !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits, 836e2b60560SKent Overstreet sizeof(u64), GFP_KERNEL))) { 83765d48e35SKent Overstreet ret = -BCH_ERR_ENOMEM_fs_other_alloc; 8387be9ab63SChris Webb goto err; 8397be9ab63SChris Webb } 8407be9ab63SChris Webb 841f3b8403eSKent Overstreet ret = bch2_fs_counters_init(c) ?: 842f3b8403eSKent Overstreet bch2_io_clock_init(&c->io_clock[READ]) ?: 843e2b60560SKent Overstreet bch2_io_clock_init(&c->io_clock[WRITE]) ?: 844e2b60560SKent Overstreet bch2_fs_journal_init(&c->journal) ?: 845e2b60560SKent Overstreet bch2_fs_replicas_init(c) ?: 846e2b60560SKent Overstreet bch2_fs_btree_cache_init(c) ?: 847e2b60560SKent Overstreet bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?: 848e2b60560SKent Overstreet bch2_fs_btree_iter_init(c) ?: 849e2b60560SKent Overstreet bch2_fs_btree_interior_update_init(c) ?: 8509b688da3SKent Overstreet bch2_fs_buckets_waiting_for_journal_init(c) ?: 851920e69bcSKent Overstreet bch2_fs_btree_write_buffer_init(c) ?: 852e2b60560SKent Overstreet bch2_fs_subvolumes_init(c) ?: 8531809b8cbSKent Overstreet bch2_fs_io_read_init(c) ?: 8541809b8cbSKent Overstreet bch2_fs_io_write_init(c) ?: 855350175bfSKent Overstreet bch2_fs_nocow_locking_init(c) ?: 856e2b60560SKent Overstreet bch2_fs_encryption_init(c) ?: 857e2b60560SKent Overstreet bch2_fs_compress_init(c) ?: 858e2b60560SKent Overstreet bch2_fs_ec_init(c) ?: 859dbbfca9fSKent Overstreet bch2_fs_fsio_init(c) ?: 860867c1fe0SDan Carpenter bch2_fs_fs_io_buffered_init(c) ?: 861dbbfca9fSKent Overstreet bch2_fs_fs_io_direct_init(c); 8627be9ab63SChris Webb if (ret) 8631c6fdbd8SKent Overstreet goto err; 8641c6fdbd8SKent Overstreet 8651c6fdbd8SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) 8661241df58SHunter Shaffer if (bch2_dev_exists(c->disk_sb.sb, i) && 8677be9ab63SChris Webb bch2_dev_alloc(c, i)) { 868e2b60560SKent Overstreet ret = -EEXIST; 8691c6fdbd8SKent Overstreet goto err; 8707be9ab63SChris Webb } 8711c6fdbd8SKent Overstreet 8724b8f89afSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 8734b8f89afSKent Overstreet &c->btree_root_journal_res, 8744b8f89afSKent Overstreet BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX)); 8754b8f89afSKent Overstreet bch2_dev_usage_journal_reserve(c); 8764b8f89afSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 8774b8f89afSKent Overstreet &c->clock_journal_res, 8784b8f89afSKent Overstreet (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2); 8794b8f89afSKent Overstreet 8801c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 881e2b60560SKent Overstreet ret = bch2_fs_online(c); 8821c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 883e2b60560SKent Overstreet 884e2b60560SKent Overstreet if (ret) 8851c6fdbd8SKent Overstreet goto err; 8861c6fdbd8SKent Overstreet out: 8871c6fdbd8SKent Overstreet return c; 8881c6fdbd8SKent Overstreet err: 8891c6fdbd8SKent Overstreet bch2_fs_free(c); 8907be9ab63SChris Webb c = ERR_PTR(ret); 8911c6fdbd8SKent Overstreet goto out; 8921c6fdbd8SKent Overstreet } 8931c6fdbd8SKent Overstreet 894619f5beeSKent Overstreet noinline_for_stack 895619f5beeSKent Overstreet static void print_mount_opts(struct bch_fs *c) 896619f5beeSKent Overstreet { 897619f5beeSKent Overstreet enum bch_opt_id i; 898fa8e94faSKent Overstreet struct printbuf p = PRINTBUF; 899619f5beeSKent Overstreet bool first = true; 900619f5beeSKent Overstreet 901ef1634f0SKent Overstreet prt_str(&p, "mounting version "); 902e3804b55SKent Overstreet bch2_version_to_text(&p, c->sb.version); 90360573ff5SKent Overstreet 904619f5beeSKent Overstreet if (c->opts.read_only) { 90560573ff5SKent Overstreet prt_str(&p, " opts="); 906619f5beeSKent Overstreet first = false; 90760573ff5SKent Overstreet prt_printf(&p, "ro"); 908619f5beeSKent Overstreet } 909619f5beeSKent Overstreet 910619f5beeSKent Overstreet for (i = 0; i < bch2_opts_nr; i++) { 911619f5beeSKent Overstreet const struct bch_option *opt = &bch2_opt_table[i]; 912619f5beeSKent Overstreet u64 v = bch2_opt_get_by_id(&c->opts, i); 913619f5beeSKent Overstreet 9148244f320SKent Overstreet if (!(opt->flags & OPT_MOUNT)) 915619f5beeSKent Overstreet continue; 916619f5beeSKent Overstreet 917619f5beeSKent Overstreet if (v == bch2_opt_get_by_id(&bch2_opts_default, i)) 918619f5beeSKent Overstreet continue; 919619f5beeSKent Overstreet 92060573ff5SKent Overstreet prt_str(&p, first ? " opts=" : ","); 921619f5beeSKent Overstreet first = false; 9225521b1dfSKent Overstreet bch2_opt_to_text(&p, c, c->disk_sb.sb, opt, v, OPT_SHOW_MOUNT_STYLE); 923619f5beeSKent Overstreet } 924619f5beeSKent Overstreet 92560573ff5SKent Overstreet bch_info(c, "%s", p.buf); 926fa8e94faSKent Overstreet printbuf_exit(&p); 927619f5beeSKent Overstreet } 928619f5beeSKent Overstreet 929619f5beeSKent Overstreet int bch2_fs_start(struct bch_fs *c) 9301c6fdbd8SKent Overstreet { 9311c6fdbd8SKent Overstreet struct bch_dev *ca; 932a420eea6STim Schlueter time64_t now = ktime_get_real_seconds(); 9331c6fdbd8SKent Overstreet unsigned i; 93478c0b75cSKent Overstreet int ret; 9351c6fdbd8SKent Overstreet 936ef1634f0SKent Overstreet print_mount_opts(c); 937ef1634f0SKent Overstreet 9381ada1606SKent Overstreet down_write(&c->state_lock); 9391c6fdbd8SKent Overstreet 940134915f3SKent Overstreet BUG_ON(test_bit(BCH_FS_STARTED, &c->flags)); 9411c6fdbd8SKent Overstreet 9421c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 9431c6fdbd8SKent Overstreet 9443f7b9713SHunter Shaffer ret = bch2_members_v2_init(c); 9453f7b9713SHunter Shaffer if (ret) { 9463f7b9713SHunter Shaffer mutex_unlock(&c->sb_lock); 9473f7b9713SHunter Shaffer goto err; 9483f7b9713SHunter Shaffer } 9493f7b9713SHunter Shaffer 9501c6fdbd8SKent Overstreet for_each_online_member(ca, c, i) 9511c6fdbd8SKent Overstreet bch2_sb_from_fs(c, ca); 9521c6fdbd8SKent Overstreet 9531c6fdbd8SKent Overstreet for_each_online_member(ca, c, i) 9543f7b9713SHunter Shaffer bch2_members_v2_get_mut(c->disk_sb.sb, i)->last_mount = cpu_to_le64(now); 9551c6fdbd8SKent Overstreet 9561c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 9571c6fdbd8SKent Overstreet 9581c6fdbd8SKent Overstreet for_each_rw_member(ca, c, i) 9591c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 9601c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 9611c6fdbd8SKent Overstreet 962bf8f8b20SDaniel Hill for (i = 0; i < BCH_TRANSACTIONS_NR; i++) { 963bf8f8b20SDaniel Hill mutex_lock(&c->btree_transaction_stats[i].lock); 964bf8f8b20SDaniel Hill bch2_time_stats_init(&c->btree_transaction_stats[i].lock_hold_times); 965bf8f8b20SDaniel Hill mutex_unlock(&c->btree_transaction_stats[i].lock); 966bf8f8b20SDaniel Hill } 967bf8f8b20SDaniel Hill 9681c6fdbd8SKent Overstreet ret = BCH_SB_INITIALIZED(c->disk_sb.sb) 9691c6fdbd8SKent Overstreet ? bch2_fs_recovery(c) 9701c6fdbd8SKent Overstreet : bch2_fs_initialize(c); 9711c6fdbd8SKent Overstreet if (ret) 9721c6fdbd8SKent Overstreet goto err; 9731c6fdbd8SKent Overstreet 974cd575ddfSKent Overstreet ret = bch2_opts_check_may_set(c); 975cd575ddfSKent Overstreet if (ret) 976cd575ddfSKent Overstreet goto err; 977cd575ddfSKent Overstreet 978e2b60560SKent Overstreet if (bch2_fs_init_fault("fs_start")) { 979e2b60560SKent Overstreet bch_err(c, "fs_start fault injected"); 98078c0b75cSKent Overstreet ret = -EINVAL; 9811c6fdbd8SKent Overstreet goto err; 982e2b60560SKent Overstreet } 9831c6fdbd8SKent Overstreet 984a9310ab0SKent Overstreet set_bit(BCH_FS_STARTED, &c->flags); 985a9310ab0SKent Overstreet 986619f5beeSKent Overstreet if (c->opts.read_only || c->opts.nochanges) { 9871c6fdbd8SKent Overstreet bch2_fs_read_only(c); 9881c6fdbd8SKent Overstreet } else { 989619f5beeSKent Overstreet ret = !test_bit(BCH_FS_RW, &c->flags) 990619f5beeSKent Overstreet ? bch2_fs_read_write(c) 991619f5beeSKent Overstreet : bch2_fs_read_write_late(c); 992619f5beeSKent Overstreet if (ret) 9931c6fdbd8SKent Overstreet goto err; 9941c6fdbd8SKent Overstreet } 9951c6fdbd8SKent Overstreet 996619f5beeSKent Overstreet ret = 0; 9971c6fdbd8SKent Overstreet out: 9981ada1606SKent Overstreet up_write(&c->state_lock); 999619f5beeSKent Overstreet return ret; 10001c6fdbd8SKent Overstreet err: 1001e46c181aSKent Overstreet bch_err_msg(c, ret, "starting filesystem"); 10021c6fdbd8SKent Overstreet goto out; 10031c6fdbd8SKent Overstreet } 10041c6fdbd8SKent Overstreet 100578c0b75cSKent Overstreet static int bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c) 10061c6fdbd8SKent Overstreet { 10071241df58SHunter Shaffer struct bch_member m = bch2_sb_member_get(sb, sb->dev_idx); 10081c6fdbd8SKent Overstreet 10098244f320SKent Overstreet if (le16_to_cpu(sb->block_size) != block_sectors(c)) 101078c0b75cSKent Overstreet return -BCH_ERR_mismatched_block_size; 10111c6fdbd8SKent Overstreet 10121241df58SHunter Shaffer if (le16_to_cpu(m.bucket_size) < 10131c6fdbd8SKent Overstreet BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb)) 101478c0b75cSKent Overstreet return -BCH_ERR_bucket_size_too_small; 10151c6fdbd8SKent Overstreet 101678c0b75cSKent Overstreet return 0; 10171c6fdbd8SKent Overstreet } 10181c6fdbd8SKent Overstreet 101978c0b75cSKent Overstreet static int bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb) 10201c6fdbd8SKent Overstreet { 10211c6fdbd8SKent Overstreet struct bch_sb *newest = 10221c6fdbd8SKent Overstreet le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb; 10231c6fdbd8SKent Overstreet 10241c6fdbd8SKent Overstreet if (!uuid_equal(&fs->uuid, &sb->uuid)) 102578c0b75cSKent Overstreet return -BCH_ERR_device_not_a_member_of_filesystem; 10261c6fdbd8SKent Overstreet 10271241df58SHunter Shaffer if (!bch2_dev_exists(newest, sb->dev_idx)) 102878c0b75cSKent Overstreet return -BCH_ERR_device_has_been_removed; 10291c6fdbd8SKent Overstreet 10301c6fdbd8SKent Overstreet if (fs->block_size != sb->block_size) 103178c0b75cSKent Overstreet return -BCH_ERR_mismatched_block_size; 10321c6fdbd8SKent Overstreet 103378c0b75cSKent Overstreet return 0; 10341c6fdbd8SKent Overstreet } 10351c6fdbd8SKent Overstreet 10361c6fdbd8SKent Overstreet /* Device startup/shutdown: */ 10371c6fdbd8SKent Overstreet 10381c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *kobj) 10391c6fdbd8SKent Overstreet { 10401c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); 10411c6fdbd8SKent Overstreet 10421c6fdbd8SKent Overstreet kfree(ca); 10431c6fdbd8SKent Overstreet } 10441c6fdbd8SKent Overstreet 10451c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *ca) 10461c6fdbd8SKent Overstreet { 10471c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 10481c6fdbd8SKent Overstreet 10491c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs && 10501c6fdbd8SKent Overstreet ca->disk_sb.bdev) 10511c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 10521c6fdbd8SKent Overstreet 10531c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) 10541c6fdbd8SKent Overstreet kobject_del(&ca->kobj); 10551c6fdbd8SKent Overstreet 10561c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 10571c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 10581c6fdbd8SKent Overstreet 10591c6fdbd8SKent Overstreet free_percpu(ca->io_done); 10601c6fdbd8SKent Overstreet bioset_exit(&ca->replica_set); 10611c6fdbd8SKent Overstreet bch2_dev_buckets_free(ca); 1062d1170ce5SKent Overstreet free_page((unsigned long) ca->sb_read_scratch); 10631c6fdbd8SKent Overstreet 10641c6fdbd8SKent Overstreet bch2_time_stats_exit(&ca->io_latency[WRITE]); 10651c6fdbd8SKent Overstreet bch2_time_stats_exit(&ca->io_latency[READ]); 10661c6fdbd8SKent Overstreet 10671c6fdbd8SKent Overstreet percpu_ref_exit(&ca->io_ref); 10681c6fdbd8SKent Overstreet percpu_ref_exit(&ca->ref); 10691c6fdbd8SKent Overstreet kobject_put(&ca->kobj); 10701c6fdbd8SKent Overstreet } 10711c6fdbd8SKent Overstreet 10721c6fdbd8SKent Overstreet static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) 10731c6fdbd8SKent Overstreet { 10741c6fdbd8SKent Overstreet 10751c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 10761c6fdbd8SKent Overstreet 10771c6fdbd8SKent Overstreet if (percpu_ref_is_zero(&ca->io_ref)) 10781c6fdbd8SKent Overstreet return; 10791c6fdbd8SKent Overstreet 10801c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 10811c6fdbd8SKent Overstreet 10821c6fdbd8SKent Overstreet reinit_completion(&ca->io_ref_completion); 10831c6fdbd8SKent Overstreet percpu_ref_kill(&ca->io_ref); 10841c6fdbd8SKent Overstreet wait_for_completion(&ca->io_ref_completion); 10851c6fdbd8SKent Overstreet 10861c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) { 10871c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 10881c6fdbd8SKent Overstreet sysfs_remove_link(&ca->kobj, "block"); 10891c6fdbd8SKent Overstreet } 10901c6fdbd8SKent Overstreet 10911c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 10921c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 10931c6fdbd8SKent Overstreet } 10941c6fdbd8SKent Overstreet 10951c6fdbd8SKent Overstreet static void bch2_dev_ref_complete(struct percpu_ref *ref) 10961c6fdbd8SKent Overstreet { 10971c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, ref); 10981c6fdbd8SKent Overstreet 10991c6fdbd8SKent Overstreet complete(&ca->ref_completion); 11001c6fdbd8SKent Overstreet } 11011c6fdbd8SKent Overstreet 11021c6fdbd8SKent Overstreet static void bch2_dev_io_ref_complete(struct percpu_ref *ref) 11031c6fdbd8SKent Overstreet { 11041c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref); 11051c6fdbd8SKent Overstreet 11061c6fdbd8SKent Overstreet complete(&ca->io_ref_completion); 11071c6fdbd8SKent Overstreet } 11081c6fdbd8SKent Overstreet 11091c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) 11101c6fdbd8SKent Overstreet { 11111c6fdbd8SKent Overstreet int ret; 11121c6fdbd8SKent Overstreet 11131c6fdbd8SKent Overstreet if (!c->kobj.state_in_sysfs) 11141c6fdbd8SKent Overstreet return 0; 11151c6fdbd8SKent Overstreet 11161c6fdbd8SKent Overstreet if (!ca->kobj.state_in_sysfs) { 11171c6fdbd8SKent Overstreet ret = kobject_add(&ca->kobj, &c->kobj, 11181c6fdbd8SKent Overstreet "dev-%u", ca->dev_idx); 11191c6fdbd8SKent Overstreet if (ret) 11201c6fdbd8SKent Overstreet return ret; 11211c6fdbd8SKent Overstreet } 11221c6fdbd8SKent Overstreet 11231c6fdbd8SKent Overstreet if (ca->disk_sb.bdev) { 11241c6fdbd8SKent Overstreet struct kobject *block = bdev_kobj(ca->disk_sb.bdev); 11251c6fdbd8SKent Overstreet 11261c6fdbd8SKent Overstreet ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); 11271c6fdbd8SKent Overstreet if (ret) 11281c6fdbd8SKent Overstreet return ret; 11291c6fdbd8SKent Overstreet 11301c6fdbd8SKent Overstreet ret = sysfs_create_link(&ca->kobj, block, "block"); 11311c6fdbd8SKent Overstreet if (ret) 11321c6fdbd8SKent Overstreet return ret; 11331c6fdbd8SKent Overstreet } 11341c6fdbd8SKent Overstreet 11351c6fdbd8SKent Overstreet return 0; 11361c6fdbd8SKent Overstreet } 11371c6fdbd8SKent Overstreet 11381c6fdbd8SKent Overstreet static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c, 11391c6fdbd8SKent Overstreet struct bch_member *member) 11401c6fdbd8SKent Overstreet { 11411c6fdbd8SKent Overstreet struct bch_dev *ca; 11421c6fdbd8SKent Overstreet 11431c6fdbd8SKent Overstreet ca = kzalloc(sizeof(*ca), GFP_KERNEL); 11441c6fdbd8SKent Overstreet if (!ca) 11451c6fdbd8SKent Overstreet return NULL; 11461c6fdbd8SKent Overstreet 11471c6fdbd8SKent Overstreet kobject_init(&ca->kobj, &bch2_dev_ktype); 11481c6fdbd8SKent Overstreet init_completion(&ca->ref_completion); 11491c6fdbd8SKent Overstreet init_completion(&ca->io_ref_completion); 11501c6fdbd8SKent Overstreet 11511c6fdbd8SKent Overstreet init_rwsem(&ca->bucket_lock); 11521c6fdbd8SKent Overstreet 11531c6fdbd8SKent Overstreet INIT_WORK(&ca->io_error_work, bch2_io_error_work); 11541c6fdbd8SKent Overstreet 11551c6fdbd8SKent Overstreet bch2_time_stats_init(&ca->io_latency[READ]); 11561c6fdbd8SKent Overstreet bch2_time_stats_init(&ca->io_latency[WRITE]); 11571c6fdbd8SKent Overstreet 11581c6fdbd8SKent Overstreet ca->mi = bch2_mi_to_cpu(member); 11591c6fdbd8SKent Overstreet ca->uuid = member->uuid; 11601c6fdbd8SKent Overstreet 1161f25d8215SKent Overstreet ca->nr_btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE, 1162f25d8215SKent Overstreet ca->mi.bucket_size / btree_sectors(c)); 1163f25d8215SKent Overstreet 11641c6fdbd8SKent Overstreet if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, 11651c6fdbd8SKent Overstreet 0, GFP_KERNEL) || 11661c6fdbd8SKent Overstreet percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete, 11671c6fdbd8SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 1168d1170ce5SKent Overstreet !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) || 11691c6fdbd8SKent Overstreet bch2_dev_buckets_alloc(c, ca) || 11701c6fdbd8SKent Overstreet bioset_init(&ca->replica_set, 4, 11711c6fdbd8SKent Overstreet offsetof(struct bch_write_bio, bio), 0) || 11721c6fdbd8SKent Overstreet !(ca->io_done = alloc_percpu(*ca->io_done))) 11731c6fdbd8SKent Overstreet goto err; 11741c6fdbd8SKent Overstreet 11751c6fdbd8SKent Overstreet return ca; 11761c6fdbd8SKent Overstreet err: 11771c6fdbd8SKent Overstreet bch2_dev_free(ca); 11781c6fdbd8SKent Overstreet return NULL; 11791c6fdbd8SKent Overstreet } 11801c6fdbd8SKent Overstreet 11811c6fdbd8SKent Overstreet static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, 11821c6fdbd8SKent Overstreet unsigned dev_idx) 11831c6fdbd8SKent Overstreet { 11841c6fdbd8SKent Overstreet ca->dev_idx = dev_idx; 11851c6fdbd8SKent Overstreet __set_bit(ca->dev_idx, ca->self.d); 11861c6fdbd8SKent Overstreet scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); 11871c6fdbd8SKent Overstreet 11881c6fdbd8SKent Overstreet ca->fs = c; 11891c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], ca); 11901c6fdbd8SKent Overstreet 11911c6fdbd8SKent Overstreet if (bch2_dev_sysfs_online(c, ca)) 11921c6fdbd8SKent Overstreet pr_warn("error creating sysfs objects"); 11931c6fdbd8SKent Overstreet } 11941c6fdbd8SKent Overstreet 11951c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx) 11961c6fdbd8SKent Overstreet { 11971241df58SHunter Shaffer struct bch_member member = bch2_sb_member_get(c->disk_sb.sb, dev_idx); 11981c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 11991c6fdbd8SKent Overstreet int ret = 0; 12001c6fdbd8SKent Overstreet 12011c6fdbd8SKent Overstreet if (bch2_fs_init_fault("dev_alloc")) 12021c6fdbd8SKent Overstreet goto err; 12031c6fdbd8SKent Overstreet 12041241df58SHunter Shaffer ca = __bch2_dev_alloc(c, &member); 12051c6fdbd8SKent Overstreet if (!ca) 12061c6fdbd8SKent Overstreet goto err; 12071c6fdbd8SKent Overstreet 1208220d2062SKent Overstreet ca->fs = c; 1209220d2062SKent Overstreet 12101c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 12111c6fdbd8SKent Overstreet return ret; 12121c6fdbd8SKent Overstreet err: 12131c6fdbd8SKent Overstreet if (ca) 12141c6fdbd8SKent Overstreet bch2_dev_free(ca); 1215c8b4534dSKent Overstreet return -BCH_ERR_ENOMEM_dev_alloc; 12161c6fdbd8SKent Overstreet } 12171c6fdbd8SKent Overstreet 12181c6fdbd8SKent Overstreet static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb) 12191c6fdbd8SKent Overstreet { 12201c6fdbd8SKent Overstreet unsigned ret; 12211c6fdbd8SKent Overstreet 12221c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca)) { 12231c6fdbd8SKent Overstreet bch_err(ca, "already have device online in slot %u", 12241c6fdbd8SKent Overstreet sb->sb->dev_idx); 122578c0b75cSKent Overstreet return -BCH_ERR_device_already_online; 12261c6fdbd8SKent Overstreet } 12271c6fdbd8SKent Overstreet 12281c6fdbd8SKent Overstreet if (get_capacity(sb->bdev->bd_disk) < 12291c6fdbd8SKent Overstreet ca->mi.bucket_size * ca->mi.nbuckets) { 12301c6fdbd8SKent Overstreet bch_err(ca, "cannot online: device too small"); 123178c0b75cSKent Overstreet return -BCH_ERR_device_size_too_small; 12321c6fdbd8SKent Overstreet } 12331c6fdbd8SKent Overstreet 12341c6fdbd8SKent Overstreet BUG_ON(!percpu_ref_is_zero(&ca->io_ref)); 12351c6fdbd8SKent Overstreet 12361c6fdbd8SKent Overstreet ret = bch2_dev_journal_init(ca, sb->sb); 12371c6fdbd8SKent Overstreet if (ret) 12381c6fdbd8SKent Overstreet return ret; 12391c6fdbd8SKent Overstreet 12401c6fdbd8SKent Overstreet /* Commit: */ 12411c6fdbd8SKent Overstreet ca->disk_sb = *sb; 12421c6fdbd8SKent Overstreet memset(sb, 0, sizeof(*sb)); 12431c6fdbd8SKent Overstreet 1244eacb2574SKent Overstreet ca->dev = ca->disk_sb.bdev->bd_dev; 1245eacb2574SKent Overstreet 12461c6fdbd8SKent Overstreet percpu_ref_reinit(&ca->io_ref); 12471c6fdbd8SKent Overstreet 12481c6fdbd8SKent Overstreet return 0; 12491c6fdbd8SKent Overstreet } 12501c6fdbd8SKent Overstreet 12511c6fdbd8SKent Overstreet static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb) 12521c6fdbd8SKent Overstreet { 12531c6fdbd8SKent Overstreet struct bch_dev *ca; 12541c6fdbd8SKent Overstreet int ret; 12551c6fdbd8SKent Overstreet 12561c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 12571c6fdbd8SKent Overstreet 12581c6fdbd8SKent Overstreet if (le64_to_cpu(sb->sb->seq) > 12591c6fdbd8SKent Overstreet le64_to_cpu(c->disk_sb.sb->seq)) 12601c6fdbd8SKent Overstreet bch2_sb_to_fs(c, sb->sb); 12611c6fdbd8SKent Overstreet 12621c6fdbd8SKent Overstreet BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices || 12631c6fdbd8SKent Overstreet !c->devs[sb->sb->dev_idx]); 12641c6fdbd8SKent Overstreet 12651c6fdbd8SKent Overstreet ca = bch_dev_locked(c, sb->sb->dev_idx); 12661c6fdbd8SKent Overstreet 12671c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, sb); 12681c6fdbd8SKent Overstreet if (ret) 12691c6fdbd8SKent Overstreet return ret; 12701c6fdbd8SKent Overstreet 12711c6fdbd8SKent Overstreet bch2_dev_sysfs_online(c, ca); 12721c6fdbd8SKent Overstreet 12731c6fdbd8SKent Overstreet if (c->sb.nr_devices == 1) 12741c6fdbd8SKent Overstreet snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev); 12751c6fdbd8SKent Overstreet snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev); 12761c6fdbd8SKent Overstreet 12771c6fdbd8SKent Overstreet rebalance_wakeup(c); 12781c6fdbd8SKent Overstreet return 0; 12791c6fdbd8SKent Overstreet } 12801c6fdbd8SKent Overstreet 12811c6fdbd8SKent Overstreet /* Device management: */ 12821c6fdbd8SKent Overstreet 12831c6fdbd8SKent Overstreet /* 12841c6fdbd8SKent Overstreet * Note: this function is also used by the error paths - when a particular 12851c6fdbd8SKent Overstreet * device sees an error, we call it to determine whether we can just set the 12861c6fdbd8SKent Overstreet * device RO, or - if this function returns false - we'll set the whole 12871c6fdbd8SKent Overstreet * filesystem RO: 12881c6fdbd8SKent Overstreet * 12891c6fdbd8SKent Overstreet * XXX: maybe we should be more explicit about whether we're changing state 12901c6fdbd8SKent Overstreet * because we got an error or what have you? 12911c6fdbd8SKent Overstreet */ 12921c6fdbd8SKent Overstreet bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, 12931c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 12941c6fdbd8SKent Overstreet { 12951c6fdbd8SKent Overstreet struct bch_devs_mask new_online_devs; 12961c6fdbd8SKent Overstreet struct bch_dev *ca2; 12971c6fdbd8SKent Overstreet int i, nr_rw = 0, required; 12981c6fdbd8SKent Overstreet 12991c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 13001c6fdbd8SKent Overstreet 13011c6fdbd8SKent Overstreet switch (new_state) { 13022436cb9fSKent Overstreet case BCH_MEMBER_STATE_rw: 13031c6fdbd8SKent Overstreet return true; 13042436cb9fSKent Overstreet case BCH_MEMBER_STATE_ro: 13052436cb9fSKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_rw) 13061c6fdbd8SKent Overstreet return true; 13071c6fdbd8SKent Overstreet 13081c6fdbd8SKent Overstreet /* do we have enough devices to write to? */ 13091c6fdbd8SKent Overstreet for_each_member_device(ca2, c, i) 13101c6fdbd8SKent Overstreet if (ca2 != ca) 13112436cb9fSKent Overstreet nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw; 13121c6fdbd8SKent Overstreet 13131c6fdbd8SKent Overstreet required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED) 13141c6fdbd8SKent Overstreet ? c->opts.metadata_replicas 13151c6fdbd8SKent Overstreet : c->opts.metadata_replicas_required, 13161c6fdbd8SKent Overstreet !(flags & BCH_FORCE_IF_DATA_DEGRADED) 13171c6fdbd8SKent Overstreet ? c->opts.data_replicas 13181c6fdbd8SKent Overstreet : c->opts.data_replicas_required); 13191c6fdbd8SKent Overstreet 13201c6fdbd8SKent Overstreet return nr_rw >= required; 13212436cb9fSKent Overstreet case BCH_MEMBER_STATE_failed: 13222436cb9fSKent Overstreet case BCH_MEMBER_STATE_spare: 13232436cb9fSKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_rw && 13242436cb9fSKent Overstreet ca->mi.state != BCH_MEMBER_STATE_ro) 13251c6fdbd8SKent Overstreet return true; 13261c6fdbd8SKent Overstreet 13271c6fdbd8SKent Overstreet /* do we have enough devices to read from? */ 13281c6fdbd8SKent Overstreet new_online_devs = bch2_online_devs(c); 13291c6fdbd8SKent Overstreet __clear_bit(ca->dev_idx, new_online_devs.d); 13301c6fdbd8SKent Overstreet 1331fcb3431bSKent Overstreet return bch2_have_enough_devs(c, new_online_devs, flags, false); 13321c6fdbd8SKent Overstreet default: 13331c6fdbd8SKent Overstreet BUG(); 13341c6fdbd8SKent Overstreet } 13351c6fdbd8SKent Overstreet } 13361c6fdbd8SKent Overstreet 13371c6fdbd8SKent Overstreet static bool bch2_fs_may_start(struct bch_fs *c) 13381c6fdbd8SKent Overstreet { 13391c6fdbd8SKent Overstreet struct bch_dev *ca; 1340fcb3431bSKent Overstreet unsigned i, flags = 0; 13411c6fdbd8SKent Overstreet 1342fcb3431bSKent Overstreet if (c->opts.very_degraded) 1343fcb3431bSKent Overstreet flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST; 1344fcb3431bSKent Overstreet 1345fcb3431bSKent Overstreet if (c->opts.degraded) 1346fcb3431bSKent Overstreet flags |= BCH_FORCE_IF_DEGRADED; 1347fcb3431bSKent Overstreet 1348fcb3431bSKent Overstreet if (!c->opts.degraded && 1349fcb3431bSKent Overstreet !c->opts.very_degraded) { 13501c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 13511c6fdbd8SKent Overstreet 13521c6fdbd8SKent Overstreet for (i = 0; i < c->disk_sb.sb->nr_devices; i++) { 13531241df58SHunter Shaffer if (!bch2_dev_exists(c->disk_sb.sb, i)) 13541c6fdbd8SKent Overstreet continue; 13551c6fdbd8SKent Overstreet 13561c6fdbd8SKent Overstreet ca = bch_dev_locked(c, i); 13571c6fdbd8SKent Overstreet 13581c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca) && 13592436cb9fSKent Overstreet (ca->mi.state == BCH_MEMBER_STATE_rw || 13602436cb9fSKent Overstreet ca->mi.state == BCH_MEMBER_STATE_ro)) { 13611c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 13621c6fdbd8SKent Overstreet return false; 13631c6fdbd8SKent Overstreet } 13641c6fdbd8SKent Overstreet } 13651c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 13661c6fdbd8SKent Overstreet } 13671c6fdbd8SKent Overstreet 1368fcb3431bSKent Overstreet return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true); 13691c6fdbd8SKent Overstreet } 13701c6fdbd8SKent Overstreet 13711c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) 13721c6fdbd8SKent Overstreet { 13731c6fdbd8SKent Overstreet /* 13741c6fdbd8SKent Overstreet * The allocator thread itself allocates btree nodes, so stop it first: 13751c6fdbd8SKent Overstreet */ 13761c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 13771c6fdbd8SKent Overstreet bch2_dev_journal_stop(&c->journal, ca); 13781c6fdbd8SKent Overstreet } 13791c6fdbd8SKent Overstreet 1380f25d8215SKent Overstreet static void __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) 13811c6fdbd8SKent Overstreet { 13821c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 13831c6fdbd8SKent Overstreet 13842436cb9fSKent Overstreet BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw); 13851c6fdbd8SKent Overstreet 13861c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 13871c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 13881c6fdbd8SKent Overstreet } 13891c6fdbd8SKent Overstreet 13901c6fdbd8SKent Overstreet int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 13911c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 13921c6fdbd8SKent Overstreet { 13933f7b9713SHunter Shaffer struct bch_member *m; 13941c6fdbd8SKent Overstreet int ret = 0; 13951c6fdbd8SKent Overstreet 13961c6fdbd8SKent Overstreet if (ca->mi.state == new_state) 13971c6fdbd8SKent Overstreet return 0; 13981c6fdbd8SKent Overstreet 13991c6fdbd8SKent Overstreet if (!bch2_dev_state_allowed(c, ca, new_state, flags)) 140078c0b75cSKent Overstreet return -BCH_ERR_device_state_not_allowed; 14011c6fdbd8SKent Overstreet 14022436cb9fSKent Overstreet if (new_state != BCH_MEMBER_STATE_rw) 14031c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 14041c6fdbd8SKent Overstreet 14052436cb9fSKent Overstreet bch_notice(ca, "%s", bch2_member_states[new_state]); 14061c6fdbd8SKent Overstreet 14071c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 14083f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 14093f7b9713SHunter Shaffer SET_BCH_MEMBER_STATE(m, new_state); 14101c6fdbd8SKent Overstreet bch2_write_super(c); 14111c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 14121c6fdbd8SKent Overstreet 1413e2b60560SKent Overstreet if (new_state == BCH_MEMBER_STATE_rw) 1414f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 14151c6fdbd8SKent Overstreet 14161c6fdbd8SKent Overstreet rebalance_wakeup(c); 14171c6fdbd8SKent Overstreet 14181c6fdbd8SKent Overstreet return ret; 14191c6fdbd8SKent Overstreet } 14201c6fdbd8SKent Overstreet 14211c6fdbd8SKent Overstreet int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 14221c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 14231c6fdbd8SKent Overstreet { 14241c6fdbd8SKent Overstreet int ret; 14251c6fdbd8SKent Overstreet 14261ada1606SKent Overstreet down_write(&c->state_lock); 14271c6fdbd8SKent Overstreet ret = __bch2_dev_set_state(c, ca, new_state, flags); 14281ada1606SKent Overstreet up_write(&c->state_lock); 14291c6fdbd8SKent Overstreet 14301c6fdbd8SKent Overstreet return ret; 14311c6fdbd8SKent Overstreet } 14321c6fdbd8SKent Overstreet 14331c6fdbd8SKent Overstreet /* Device add/removal: */ 14341c6fdbd8SKent Overstreet 1435c0ebe3e4SKent Overstreet static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca) 14365d20ba48SKent Overstreet { 1437c6b2826cSKent Overstreet struct bpos start = POS(ca->dev_idx, 0); 1438c6b2826cSKent Overstreet struct bpos end = POS(ca->dev_idx, U64_MAX); 14395d20ba48SKent Overstreet int ret; 14405d20ba48SKent Overstreet 1441a9c0a4cbSKent Overstreet /* 1442a9c0a4cbSKent Overstreet * We clear the LRU and need_discard btrees first so that we don't race 1443a9c0a4cbSKent Overstreet * with bch2_do_invalidates() and bch2_do_discards() 1444a9c0a4cbSKent Overstreet */ 1445a9c0a4cbSKent Overstreet ret = bch2_btree_delete_range(c, BTREE_ID_lru, start, end, 1446a9c0a4cbSKent Overstreet BTREE_TRIGGER_NORUN, NULL) ?: 1447a9c0a4cbSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_need_discard, start, end, 1448c6b2826cSKent Overstreet BTREE_TRIGGER_NORUN, NULL) ?: 1449c6b2826cSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_freespace, start, end, 1450c6b2826cSKent Overstreet BTREE_TRIGGER_NORUN, NULL) ?: 1451a8c752bbSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_backpointers, start, end, 1452a8c752bbSKent Overstreet BTREE_TRIGGER_NORUN, NULL) ?: 1453a9c0a4cbSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_alloc, start, end, 145402d51bb9SBrian Foster BTREE_TRIGGER_NORUN, NULL) ?: 145502d51bb9SBrian Foster bch2_btree_delete_range(c, BTREE_ID_bucket_gens, start, end, 1456c6b2826cSKent Overstreet BTREE_TRIGGER_NORUN, NULL); 14575d20ba48SKent Overstreet if (ret) 1458e46c181aSKent Overstreet bch_err_msg(c, ret, "removing dev alloc info"); 14595d20ba48SKent Overstreet 1460c6b2826cSKent Overstreet return ret; 14615d20ba48SKent Overstreet } 14625d20ba48SKent Overstreet 14631c6fdbd8SKent Overstreet int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) 14641c6fdbd8SKent Overstreet { 14653f7b9713SHunter Shaffer struct bch_member *m; 14661c6fdbd8SKent Overstreet unsigned dev_idx = ca->dev_idx, data; 146778c0b75cSKent Overstreet int ret; 14681c6fdbd8SKent Overstreet 14691ada1606SKent Overstreet down_write(&c->state_lock); 14701c6fdbd8SKent Overstreet 147131ba2cd3SKent Overstreet /* 147231ba2cd3SKent Overstreet * We consume a reference to ca->ref, regardless of whether we succeed 147331ba2cd3SKent Overstreet * or fail: 147431ba2cd3SKent Overstreet */ 147531ba2cd3SKent Overstreet percpu_ref_put(&ca->ref); 14761c6fdbd8SKent Overstreet 14772436cb9fSKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 14781c6fdbd8SKent Overstreet bch_err(ca, "Cannot remove without losing data"); 147978c0b75cSKent Overstreet ret = -BCH_ERR_device_state_not_allowed; 14801c6fdbd8SKent Overstreet goto err; 14811c6fdbd8SKent Overstreet } 14821c6fdbd8SKent Overstreet 14831c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 14841c6fdbd8SKent Overstreet 14851c6fdbd8SKent Overstreet ret = bch2_dev_data_drop(c, ca->dev_idx, flags); 14861c6fdbd8SKent Overstreet if (ret) { 1487e46c181aSKent Overstreet bch_err_msg(ca, ret, "dropping data"); 14881c6fdbd8SKent Overstreet goto err; 14891c6fdbd8SKent Overstreet } 14901c6fdbd8SKent Overstreet 14915d20ba48SKent Overstreet ret = bch2_dev_remove_alloc(c, ca); 14921c6fdbd8SKent Overstreet if (ret) { 1493e46c181aSKent Overstreet bch_err_msg(ca, ret, "deleting alloc info"); 14941c6fdbd8SKent Overstreet goto err; 14951c6fdbd8SKent Overstreet } 14961c6fdbd8SKent Overstreet 1497a9c0a4cbSKent Overstreet ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); 1498a9c0a4cbSKent Overstreet if (ret) { 1499e46c181aSKent Overstreet bch_err_msg(ca, ret, "flushing journal"); 1500a9c0a4cbSKent Overstreet goto err; 1501a9c0a4cbSKent Overstreet } 1502a9c0a4cbSKent Overstreet 1503a9c0a4cbSKent Overstreet ret = bch2_journal_flush(&c->journal); 15041c6fdbd8SKent Overstreet if (ret) { 1505e46c181aSKent Overstreet bch_err(ca, "journal error"); 15061c6fdbd8SKent Overstreet goto err; 15071c6fdbd8SKent Overstreet } 15081c6fdbd8SKent Overstreet 150931ba2cd3SKent Overstreet ret = bch2_replicas_gc2(c); 151031ba2cd3SKent Overstreet if (ret) { 1511e46c181aSKent Overstreet bch_err_msg(ca, ret, "in replicas_gc2()"); 151231ba2cd3SKent Overstreet goto err; 151331ba2cd3SKent Overstreet } 151431ba2cd3SKent Overstreet 151531ba2cd3SKent Overstreet data = bch2_dev_has_data(c, ca); 151631ba2cd3SKent Overstreet if (data) { 1517fa8e94faSKent Overstreet struct printbuf data_has = PRINTBUF; 151831ba2cd3SKent Overstreet 1519401ec4dbSKent Overstreet prt_bitflags(&data_has, bch2_data_types, data); 1520fa8e94faSKent Overstreet bch_err(ca, "Remove failed, still has data (%s)", data_has.buf); 1521fa8e94faSKent Overstreet printbuf_exit(&data_has); 152231ba2cd3SKent Overstreet ret = -EBUSY; 152331ba2cd3SKent Overstreet goto err; 152431ba2cd3SKent Overstreet } 152531ba2cd3SKent Overstreet 15261c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 15271c6fdbd8SKent Overstreet 15281c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15291c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], NULL); 15301c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15311c6fdbd8SKent Overstreet 15321c6fdbd8SKent Overstreet percpu_ref_kill(&ca->ref); 15331c6fdbd8SKent Overstreet wait_for_completion(&ca->ref_completion); 15341c6fdbd8SKent Overstreet 15351c6fdbd8SKent Overstreet bch2_dev_free(ca); 15361c6fdbd8SKent Overstreet 15371c6fdbd8SKent Overstreet /* 1538bc652905SBrian Foster * At this point the device object has been removed in-core, but the 1539bc652905SBrian Foster * on-disk journal might still refer to the device index via sb device 1540bc652905SBrian Foster * usage entries. Recovery fails if it sees usage information for an 1541bc652905SBrian Foster * invalid device. Flush journal pins to push the back of the journal 1542bc652905SBrian Foster * past now invalid device index references before we update the 1543bc652905SBrian Foster * superblock, but after the device object has been removed so any 1544bc652905SBrian Foster * further journal writes elide usage info for the device. 1545bc652905SBrian Foster */ 1546bc652905SBrian Foster bch2_journal_flush_all_pins(&c->journal); 1547bc652905SBrian Foster 1548bc652905SBrian Foster /* 15491c6fdbd8SKent Overstreet * Free this device's slot in the bch_member array - all pointers to 15501c6fdbd8SKent Overstreet * this device must be gone: 15511c6fdbd8SKent Overstreet */ 15521c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15533f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx); 15543f7b9713SHunter Shaffer memset(&m->uuid, 0, sizeof(m->uuid)); 15551c6fdbd8SKent Overstreet 15561c6fdbd8SKent Overstreet bch2_write_super(c); 15571c6fdbd8SKent Overstreet 15581c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15591ada1606SKent Overstreet up_write(&c->state_lock); 1560180fb49dSKent Overstreet 1561180fb49dSKent Overstreet bch2_dev_usage_journal_reserve(c); 15621c6fdbd8SKent Overstreet return 0; 15631c6fdbd8SKent Overstreet err: 15642436cb9fSKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw && 1565d3bb629dSKent Overstreet !percpu_ref_is_zero(&ca->io_ref)) 15661c6fdbd8SKent Overstreet __bch2_dev_read_write(c, ca); 15671ada1606SKent Overstreet up_write(&c->state_lock); 15681c6fdbd8SKent Overstreet return ret; 15691c6fdbd8SKent Overstreet } 15701c6fdbd8SKent Overstreet 15711c6fdbd8SKent Overstreet /* Add new device to running filesystem: */ 15721c6fdbd8SKent Overstreet int bch2_dev_add(struct bch_fs *c, const char *path) 15731c6fdbd8SKent Overstreet { 15741c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 15751c6fdbd8SKent Overstreet struct bch_sb_handle sb; 15761c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 15773f7b9713SHunter Shaffer struct bch_sb_field_members_v2 *mi; 15781c6fdbd8SKent Overstreet struct bch_member dev_mi; 15791c6fdbd8SKent Overstreet unsigned dev_idx, nr_devices, u64s; 1580fa8e94faSKent Overstreet struct printbuf errbuf = PRINTBUF; 158102afcb8cSKent Overstreet struct printbuf label = PRINTBUF; 15821c6fdbd8SKent Overstreet int ret; 15831c6fdbd8SKent Overstreet 15841c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 1585e8536925SKent Overstreet if (ret) { 1586e46c181aSKent Overstreet bch_err_msg(c, ret, "reading super"); 1587efe68e1dSKent Overstreet goto err; 1588e8536925SKent Overstreet } 15891c6fdbd8SKent Overstreet 15901241df58SHunter Shaffer dev_mi = bch2_sb_member_get(sb.sb, sb.sb->dev_idx); 15911c6fdbd8SKent Overstreet 159202afcb8cSKent Overstreet if (BCH_MEMBER_GROUP(&dev_mi)) { 159302afcb8cSKent Overstreet bch2_disk_path_to_text(&label, sb.sb, BCH_MEMBER_GROUP(&dev_mi) - 1); 159402afcb8cSKent Overstreet if (label.allocation_failure) { 159502afcb8cSKent Overstreet ret = -ENOMEM; 159602afcb8cSKent Overstreet goto err; 159702afcb8cSKent Overstreet } 159802afcb8cSKent Overstreet } 159902afcb8cSKent Overstreet 160078c0b75cSKent Overstreet ret = bch2_dev_may_add(sb.sb, c); 160178c0b75cSKent Overstreet if (ret) { 1602e46c181aSKent Overstreet bch_err_fn(c, ret); 1603efe68e1dSKent Overstreet goto err; 1604e8536925SKent Overstreet } 16051c6fdbd8SKent Overstreet 16061c6fdbd8SKent Overstreet ca = __bch2_dev_alloc(c, &dev_mi); 16071c6fdbd8SKent Overstreet if (!ca) { 1608efe68e1dSKent Overstreet ret = -ENOMEM; 1609efe68e1dSKent Overstreet goto err; 16101c6fdbd8SKent Overstreet } 16111c6fdbd8SKent Overstreet 1612822835ffSKent Overstreet bch2_dev_usage_init(ca); 1613822835ffSKent Overstreet 16141c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, &sb); 161571933fb6SChristophe JAILLET if (ret) 1616efe68e1dSKent Overstreet goto err; 16171c6fdbd8SKent Overstreet 16181c6fdbd8SKent Overstreet ret = bch2_dev_journal_alloc(ca); 1619e8536925SKent Overstreet if (ret) { 1620e46c181aSKent Overstreet bch_err_msg(c, ret, "allocating journal"); 16211c6fdbd8SKent Overstreet goto err; 1622e8536925SKent Overstreet } 16231c6fdbd8SKent Overstreet 16241ada1606SKent Overstreet down_write(&c->state_lock); 16251c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 16261c6fdbd8SKent Overstreet 16271c6fdbd8SKent Overstreet ret = bch2_sb_from_fs(c, ca); 1628e8536925SKent Overstreet if (ret) { 1629e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 16301c6fdbd8SKent Overstreet goto err_unlock; 1631e8536925SKent Overstreet } 16321c6fdbd8SKent Overstreet 1633*4637429eSKent Overstreet mi = bch2_sb_field_get(ca->disk_sb.sb, members_v2); 16341c6fdbd8SKent Overstreet 1635*4637429eSKent Overstreet if (!bch2_sb_field_resize(&ca->disk_sb, members_v2, 16361c6fdbd8SKent Overstreet le32_to_cpu(mi->field.u64s) + 16371c6fdbd8SKent Overstreet sizeof(dev_mi) / sizeof(u64))) { 1638098ef98dSKent Overstreet ret = -BCH_ERR_ENOSPC_sb_members; 1639e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 16401c6fdbd8SKent Overstreet goto err_unlock; 16411c6fdbd8SKent Overstreet } 16421c6fdbd8SKent Overstreet 16431c6fdbd8SKent Overstreet if (dynamic_fault("bcachefs:add:no_slot")) 16441c6fdbd8SKent Overstreet goto no_slot; 16451c6fdbd8SKent Overstreet 16461c6fdbd8SKent Overstreet for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++) 16471241df58SHunter Shaffer if (!bch2_dev_exists(c->disk_sb.sb, dev_idx)) 16481c6fdbd8SKent Overstreet goto have_slot; 16491c6fdbd8SKent Overstreet no_slot: 1650098ef98dSKent Overstreet ret = -BCH_ERR_ENOSPC_sb_members; 1651e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 16521c6fdbd8SKent Overstreet goto err_unlock; 16531c6fdbd8SKent Overstreet 16541c6fdbd8SKent Overstreet have_slot: 16551c6fdbd8SKent Overstreet nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices); 16563f7b9713SHunter Shaffer u64s = DIV_ROUND_UP(sizeof(struct bch_sb_field_members_v2) + 16573f7b9713SHunter Shaffer le16_to_cpu(mi->member_bytes) * nr_devices, sizeof(u64)); 16581c6fdbd8SKent Overstreet 1659*4637429eSKent Overstreet mi = bch2_sb_field_resize(&c->disk_sb, members_v2, u64s); 1660e8536925SKent Overstreet if (!mi) { 1661098ef98dSKent Overstreet ret = -BCH_ERR_ENOSPC_sb_members; 1662e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 16631c6fdbd8SKent Overstreet goto err_unlock; 1664e8536925SKent Overstreet } 16653f7b9713SHunter Shaffer struct bch_member *m = bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx); 16661c6fdbd8SKent Overstreet 16671c6fdbd8SKent Overstreet /* success: */ 16681c6fdbd8SKent Overstreet 16693f7b9713SHunter Shaffer *m = dev_mi; 16703f7b9713SHunter Shaffer m->last_mount = cpu_to_le64(ktime_get_real_seconds()); 16711c6fdbd8SKent Overstreet c->disk_sb.sb->nr_devices = nr_devices; 16721c6fdbd8SKent Overstreet 16731c6fdbd8SKent Overstreet ca->disk_sb.sb->dev_idx = dev_idx; 16741c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 16751c6fdbd8SKent Overstreet 167602afcb8cSKent Overstreet if (BCH_MEMBER_GROUP(&dev_mi)) { 167702afcb8cSKent Overstreet ret = __bch2_dev_group_set(c, ca, label.buf); 167802afcb8cSKent Overstreet if (ret) { 1679e46c181aSKent Overstreet bch_err_msg(c, ret, "creating new label"); 168002afcb8cSKent Overstreet goto err_unlock; 168102afcb8cSKent Overstreet } 168202afcb8cSKent Overstreet } 168302afcb8cSKent Overstreet 16841c6fdbd8SKent Overstreet bch2_write_super(c); 16851c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 16861c6fdbd8SKent Overstreet 1687180fb49dSKent Overstreet bch2_dev_usage_journal_reserve(c); 1688180fb49dSKent Overstreet 1689d62ab355SKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca); 1690e8536925SKent Overstreet if (ret) { 1691e46c181aSKent Overstreet bch_err_msg(c, ret, "marking new superblock"); 1692bfcf840dSKent Overstreet goto err_late; 1693e8536925SKent Overstreet } 16948d6b6222SKent Overstreet 1695c6b2826cSKent Overstreet ret = bch2_fs_freespace_init(c); 1696c6b2826cSKent Overstreet if (ret) { 1697e46c181aSKent Overstreet bch_err_msg(c, ret, "initializing free space"); 1698c6b2826cSKent Overstreet goto err_late; 1699c6b2826cSKent Overstreet } 1700c6b2826cSKent Overstreet 170109943313SKent Overstreet ca->new_fs_bucket_idx = 0; 170209943313SKent Overstreet 1703f25d8215SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw) 1704f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 17051c6fdbd8SKent Overstreet 17061ada1606SKent Overstreet up_write(&c->state_lock); 17071c6fdbd8SKent Overstreet return 0; 17081c6fdbd8SKent Overstreet 17091c6fdbd8SKent Overstreet err_unlock: 17101c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17111ada1606SKent Overstreet up_write(&c->state_lock); 17121c6fdbd8SKent Overstreet err: 17131c6fdbd8SKent Overstreet if (ca) 17141c6fdbd8SKent Overstreet bch2_dev_free(ca); 17151c6fdbd8SKent Overstreet bch2_free_super(&sb); 171602afcb8cSKent Overstreet printbuf_exit(&label); 1717fa8e94faSKent Overstreet printbuf_exit(&errbuf); 17181c6fdbd8SKent Overstreet return ret; 17191c6fdbd8SKent Overstreet err_late: 1720bfcf840dSKent Overstreet up_write(&c->state_lock); 1721efe68e1dSKent Overstreet ca = NULL; 1722efe68e1dSKent Overstreet goto err; 17231c6fdbd8SKent Overstreet } 17241c6fdbd8SKent Overstreet 17251c6fdbd8SKent Overstreet /* Hot add existing device to running filesystem: */ 17261c6fdbd8SKent Overstreet int bch2_dev_online(struct bch_fs *c, const char *path) 17271c6fdbd8SKent Overstreet { 17281c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 17291c6fdbd8SKent Overstreet struct bch_sb_handle sb = { NULL }; 17301c6fdbd8SKent Overstreet struct bch_dev *ca; 17311c6fdbd8SKent Overstreet unsigned dev_idx; 17321c6fdbd8SKent Overstreet int ret; 17331c6fdbd8SKent Overstreet 17341ada1606SKent Overstreet down_write(&c->state_lock); 17351c6fdbd8SKent Overstreet 17361c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 17371c6fdbd8SKent Overstreet if (ret) { 17381ada1606SKent Overstreet up_write(&c->state_lock); 17391c6fdbd8SKent Overstreet return ret; 17401c6fdbd8SKent Overstreet } 17411c6fdbd8SKent Overstreet 17421c6fdbd8SKent Overstreet dev_idx = sb.sb->dev_idx; 17431c6fdbd8SKent Overstreet 174478c0b75cSKent Overstreet ret = bch2_dev_in_fs(c->disk_sb.sb, sb.sb); 174578c0b75cSKent Overstreet if (ret) { 1746e46c181aSKent Overstreet bch_err_msg(c, ret, "bringing %s online", path); 17471c6fdbd8SKent Overstreet goto err; 17481c6fdbd8SKent Overstreet } 17491c6fdbd8SKent Overstreet 1750e2b60560SKent Overstreet ret = bch2_dev_attach_bdev(c, &sb); 1751e2b60560SKent Overstreet if (ret) 1752e2b60560SKent Overstreet goto err; 1753e2b60560SKent Overstreet 17541c6fdbd8SKent Overstreet ca = bch_dev_locked(c, dev_idx); 1755bfcf840dSKent Overstreet 1756e2b60560SKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca); 1757e2b60560SKent Overstreet if (ret) { 1758e46c181aSKent Overstreet bch_err_msg(c, ret, "bringing %s online: error from bch2_trans_mark_dev_sb", path); 1759bfcf840dSKent Overstreet goto err; 1760bfcf840dSKent Overstreet } 1761bfcf840dSKent Overstreet 1762f25d8215SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw) 1763f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 17641c6fdbd8SKent Overstreet 17651c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 17663f7b9713SHunter Shaffer struct bch_member *m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 17671c6fdbd8SKent Overstreet 17683f7b9713SHunter Shaffer m->last_mount = 1769a420eea6STim Schlueter cpu_to_le64(ktime_get_real_seconds()); 17701c6fdbd8SKent Overstreet 17711c6fdbd8SKent Overstreet bch2_write_super(c); 17721c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17731c6fdbd8SKent Overstreet 1774b1c945b3SKent Overstreet ret = bch2_fs_freespace_init(c); 1775b1c945b3SKent Overstreet if (ret) 1776e46c181aSKent Overstreet bch_err_msg(c, ret, "initializing free space"); 1777b1c945b3SKent Overstreet 17781ada1606SKent Overstreet up_write(&c->state_lock); 17791c6fdbd8SKent Overstreet return 0; 17801c6fdbd8SKent Overstreet err: 17811ada1606SKent Overstreet up_write(&c->state_lock); 17821c6fdbd8SKent Overstreet bch2_free_super(&sb); 178378c0b75cSKent Overstreet return ret; 17841c6fdbd8SKent Overstreet } 17851c6fdbd8SKent Overstreet 17861c6fdbd8SKent Overstreet int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) 17871c6fdbd8SKent Overstreet { 17881ada1606SKent Overstreet down_write(&c->state_lock); 17891c6fdbd8SKent Overstreet 17901c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca)) { 17911c6fdbd8SKent Overstreet bch_err(ca, "Already offline"); 17921ada1606SKent Overstreet up_write(&c->state_lock); 17931c6fdbd8SKent Overstreet return 0; 17941c6fdbd8SKent Overstreet } 17951c6fdbd8SKent Overstreet 17962436cb9fSKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 17971c6fdbd8SKent Overstreet bch_err(ca, "Cannot offline required disk"); 17981ada1606SKent Overstreet up_write(&c->state_lock); 179978c0b75cSKent Overstreet return -BCH_ERR_device_state_not_allowed; 18001c6fdbd8SKent Overstreet } 18011c6fdbd8SKent Overstreet 18021c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 18031c6fdbd8SKent Overstreet 18041ada1606SKent Overstreet up_write(&c->state_lock); 18051c6fdbd8SKent Overstreet return 0; 18061c6fdbd8SKent Overstreet } 18071c6fdbd8SKent Overstreet 18081c6fdbd8SKent Overstreet int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) 18091c6fdbd8SKent Overstreet { 18103f7b9713SHunter Shaffer struct bch_member *m; 181169d1f052SKent Overstreet u64 old_nbuckets; 18121c6fdbd8SKent Overstreet int ret = 0; 18131c6fdbd8SKent Overstreet 18141ada1606SKent Overstreet down_write(&c->state_lock); 181569d1f052SKent Overstreet old_nbuckets = ca->mi.nbuckets; 18161c6fdbd8SKent Overstreet 18171c6fdbd8SKent Overstreet if (nbuckets < ca->mi.nbuckets) { 18181c6fdbd8SKent Overstreet bch_err(ca, "Cannot shrink yet"); 18191c6fdbd8SKent Overstreet ret = -EINVAL; 18201c6fdbd8SKent Overstreet goto err; 18211c6fdbd8SKent Overstreet } 18221c6fdbd8SKent Overstreet 18231c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca) && 18241c6fdbd8SKent Overstreet get_capacity(ca->disk_sb.bdev->bd_disk) < 18251c6fdbd8SKent Overstreet ca->mi.bucket_size * nbuckets) { 18261c6fdbd8SKent Overstreet bch_err(ca, "New size larger than device"); 182778c0b75cSKent Overstreet ret = -BCH_ERR_device_size_too_small; 18281c6fdbd8SKent Overstreet goto err; 18291c6fdbd8SKent Overstreet } 18301c6fdbd8SKent Overstreet 18311c6fdbd8SKent Overstreet ret = bch2_dev_buckets_resize(c, ca, nbuckets); 18321c6fdbd8SKent Overstreet if (ret) { 1833e46c181aSKent Overstreet bch_err_msg(ca, ret, "resizing buckets"); 18341c6fdbd8SKent Overstreet goto err; 18351c6fdbd8SKent Overstreet } 18361c6fdbd8SKent Overstreet 1837224ec3e6SKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca); 18383e3e02e6SKent Overstreet if (ret) 1839224ec3e6SKent Overstreet goto err; 1840224ec3e6SKent Overstreet 18411c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 18423f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 18433f7b9713SHunter Shaffer m->nbuckets = cpu_to_le64(nbuckets); 18441c6fdbd8SKent Overstreet 18451c6fdbd8SKent Overstreet bch2_write_super(c); 18461c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 18471c6fdbd8SKent Overstreet 184869d1f052SKent Overstreet if (ca->mi.freespace_initialized) { 184969d1f052SKent Overstreet ret = bch2_dev_freespace_init(c, ca, old_nbuckets, nbuckets); 185069d1f052SKent Overstreet if (ret) 185169d1f052SKent Overstreet goto err; 185269d1f052SKent Overstreet 185369d1f052SKent Overstreet /* 185469d1f052SKent Overstreet * XXX: this is all wrong transactionally - we'll be able to do 185569d1f052SKent Overstreet * this correctly after the disk space accounting rewrite 185669d1f052SKent Overstreet */ 185769d1f052SKent Overstreet ca->usage_base->d[BCH_DATA_free].buckets += nbuckets - old_nbuckets; 185869d1f052SKent Overstreet } 185969d1f052SKent Overstreet 18601c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 18611c6fdbd8SKent Overstreet err: 18621ada1606SKent Overstreet up_write(&c->state_lock); 18631c6fdbd8SKent Overstreet return ret; 18641c6fdbd8SKent Overstreet } 18651c6fdbd8SKent Overstreet 18661c6fdbd8SKent Overstreet /* return with ref on ca->ref: */ 1867bf7e49a4SKent Overstreet struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name) 18681c6fdbd8SKent Overstreet { 18691c6fdbd8SKent Overstreet struct bch_dev *ca; 18701c6fdbd8SKent Overstreet unsigned i; 18711c6fdbd8SKent Overstreet 18723a402c8dSKent Overstreet rcu_read_lock(); 18733a402c8dSKent Overstreet for_each_member_device_rcu(ca, c, i, NULL) 1874bf7e49a4SKent Overstreet if (!strcmp(name, ca->name)) 18751c6fdbd8SKent Overstreet goto found; 1876e47a390aSKent Overstreet ca = ERR_PTR(-BCH_ERR_ENOENT_dev_not_found); 18771c6fdbd8SKent Overstreet found: 18783a402c8dSKent Overstreet rcu_read_unlock(); 18793a402c8dSKent Overstreet 18801c6fdbd8SKent Overstreet return ca; 18811c6fdbd8SKent Overstreet } 18821c6fdbd8SKent Overstreet 18831c6fdbd8SKent Overstreet /* Filesystem open: */ 18841c6fdbd8SKent Overstreet 18851c6fdbd8SKent Overstreet struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, 18861c6fdbd8SKent Overstreet struct bch_opts opts) 18871c6fdbd8SKent Overstreet { 18881c6fdbd8SKent Overstreet struct bch_sb_handle *sb = NULL; 18891c6fdbd8SKent Overstreet struct bch_fs *c = NULL; 18901c6fdbd8SKent Overstreet unsigned i, best_sb = 0; 1891fa8e94faSKent Overstreet struct printbuf errbuf = PRINTBUF; 1892e2b60560SKent Overstreet int ret = 0; 18931c6fdbd8SKent Overstreet 1894efe68e1dSKent Overstreet if (!try_module_get(THIS_MODULE)) 1895efe68e1dSKent Overstreet return ERR_PTR(-ENODEV); 1896efe68e1dSKent Overstreet 18971c6fdbd8SKent Overstreet if (!nr_devices) { 1898efe68e1dSKent Overstreet ret = -EINVAL; 1899efe68e1dSKent Overstreet goto err; 19001c6fdbd8SKent Overstreet } 19011c6fdbd8SKent Overstreet 19021c6fdbd8SKent Overstreet sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL); 1903e2b60560SKent Overstreet if (!sb) { 1904e2b60560SKent Overstreet ret = -ENOMEM; 19051c6fdbd8SKent Overstreet goto err; 1906e2b60560SKent Overstreet } 19071c6fdbd8SKent Overstreet 19081c6fdbd8SKent Overstreet for (i = 0; i < nr_devices; i++) { 19091c6fdbd8SKent Overstreet ret = bch2_read_super(devices[i], &opts, &sb[i]); 19101c6fdbd8SKent Overstreet if (ret) 19111c6fdbd8SKent Overstreet goto err; 19121c6fdbd8SKent Overstreet 19131c6fdbd8SKent Overstreet } 19141c6fdbd8SKent Overstreet 19151c6fdbd8SKent Overstreet for (i = 1; i < nr_devices; i++) 19161c6fdbd8SKent Overstreet if (le64_to_cpu(sb[i].sb->seq) > 19171c6fdbd8SKent Overstreet le64_to_cpu(sb[best_sb].sb->seq)) 19181c6fdbd8SKent Overstreet best_sb = i; 19191c6fdbd8SKent Overstreet 1920625104eaSKent Overstreet i = 0; 1921625104eaSKent Overstreet while (i < nr_devices) { 1922625104eaSKent Overstreet if (i != best_sb && 19231241df58SHunter Shaffer !bch2_dev_exists(sb[best_sb].sb, sb[i].sb->dev_idx)) { 1924625104eaSKent Overstreet pr_info("%pg has been removed, skipping", sb[i].bdev); 1925625104eaSKent Overstreet bch2_free_super(&sb[i]); 1926625104eaSKent Overstreet array_remove_item(sb, nr_devices, i); 1927625104eaSKent Overstreet continue; 1928625104eaSKent Overstreet } 1929625104eaSKent Overstreet 193078c0b75cSKent Overstreet ret = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb); 193178c0b75cSKent Overstreet if (ret) 19321c6fdbd8SKent Overstreet goto err_print; 1933625104eaSKent Overstreet i++; 19341c6fdbd8SKent Overstreet } 19351c6fdbd8SKent Overstreet 19361c6fdbd8SKent Overstreet c = bch2_fs_alloc(sb[best_sb].sb, opts); 19377be9ab63SChris Webb if (IS_ERR(c)) { 19387be9ab63SChris Webb ret = PTR_ERR(c); 19391c6fdbd8SKent Overstreet goto err; 19407be9ab63SChris Webb } 19411c6fdbd8SKent Overstreet 19421ada1606SKent Overstreet down_write(&c->state_lock); 1943e2b60560SKent Overstreet for (i = 0; i < nr_devices; i++) { 1944e2b60560SKent Overstreet ret = bch2_dev_attach_bdev(c, &sb[i]); 1945e2b60560SKent Overstreet if (ret) { 19461ada1606SKent Overstreet up_write(&c->state_lock); 1947e2b60560SKent Overstreet goto err; 1948e2b60560SKent Overstreet } 19491c6fdbd8SKent Overstreet } 19501ada1606SKent Overstreet up_write(&c->state_lock); 19511c6fdbd8SKent Overstreet 195278c0b75cSKent Overstreet if (!bch2_fs_may_start(c)) { 195378c0b75cSKent Overstreet ret = -BCH_ERR_insufficient_devices_to_start; 19541c6fdbd8SKent Overstreet goto err_print; 195578c0b75cSKent Overstreet } 19561c6fdbd8SKent Overstreet 19571c6fdbd8SKent Overstreet if (!c->opts.nostart) { 1958619f5beeSKent Overstreet ret = bch2_fs_start(c); 1959619f5beeSKent Overstreet if (ret) 1960619f5beeSKent Overstreet goto err; 19611c6fdbd8SKent Overstreet } 19621c6fdbd8SKent Overstreet out: 19631c6fdbd8SKent Overstreet kfree(sb); 1964fa8e94faSKent Overstreet printbuf_exit(&errbuf); 19651c6fdbd8SKent Overstreet module_put(THIS_MODULE); 19661c6fdbd8SKent Overstreet return c; 19671c6fdbd8SKent Overstreet err_print: 19681c6fdbd8SKent Overstreet pr_err("bch_fs_open err opening %s: %s", 196978c0b75cSKent Overstreet devices[0], bch2_err_str(ret)); 19701c6fdbd8SKent Overstreet err: 19717be9ab63SChris Webb if (!IS_ERR_OR_NULL(c)) 19721c6fdbd8SKent Overstreet bch2_fs_stop(c); 1973e2b60560SKent Overstreet if (sb) 19741c6fdbd8SKent Overstreet for (i = 0; i < nr_devices; i++) 19751c6fdbd8SKent Overstreet bch2_free_super(&sb[i]); 19761c6fdbd8SKent Overstreet c = ERR_PTR(ret); 19771c6fdbd8SKent Overstreet goto out; 19781c6fdbd8SKent Overstreet } 19791c6fdbd8SKent Overstreet 19801c6fdbd8SKent Overstreet /* Global interfaces/init */ 19811c6fdbd8SKent Overstreet 19821c6fdbd8SKent Overstreet static void bcachefs_exit(void) 19831c6fdbd8SKent Overstreet { 19841c6fdbd8SKent Overstreet bch2_debug_exit(); 19851c6fdbd8SKent Overstreet bch2_vfs_exit(); 19861c6fdbd8SKent Overstreet bch2_chardev_exit(); 198714ba3706SKent Overstreet bch2_btree_key_cache_exit(); 19881c6fdbd8SKent Overstreet if (bcachefs_kset) 19891c6fdbd8SKent Overstreet kset_unregister(bcachefs_kset); 19901c6fdbd8SKent Overstreet } 19911c6fdbd8SKent Overstreet 19921c6fdbd8SKent Overstreet static int __init bcachefs_init(void) 19931c6fdbd8SKent Overstreet { 19941c6fdbd8SKent Overstreet bch2_bkey_pack_test(); 19951c6fdbd8SKent Overstreet 19961c6fdbd8SKent Overstreet if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) || 199714ba3706SKent Overstreet bch2_btree_key_cache_init() || 19981c6fdbd8SKent Overstreet bch2_chardev_init() || 19991c6fdbd8SKent Overstreet bch2_vfs_init() || 20001c6fdbd8SKent Overstreet bch2_debug_init()) 20011c6fdbd8SKent Overstreet goto err; 20021c6fdbd8SKent Overstreet 20031c6fdbd8SKent Overstreet return 0; 20041c6fdbd8SKent Overstreet err: 20051c6fdbd8SKent Overstreet bcachefs_exit(); 20061c6fdbd8SKent Overstreet return -ENOMEM; 20071c6fdbd8SKent Overstreet } 20081c6fdbd8SKent Overstreet 20091c6fdbd8SKent Overstreet #define BCH_DEBUG_PARAM(name, description) \ 20101c6fdbd8SKent Overstreet bool bch2_##name; \ 20111c6fdbd8SKent Overstreet module_param_named(name, bch2_##name, bool, 0644); \ 20121c6fdbd8SKent Overstreet MODULE_PARM_DESC(name, description); 20131c6fdbd8SKent Overstreet BCH_DEBUG_PARAMS() 20141c6fdbd8SKent Overstreet #undef BCH_DEBUG_PARAM 20151c6fdbd8SKent Overstreet 2016e08e63e4SKent Overstreet __maybe_unused 201773bd774dSKent Overstreet static unsigned bch2_metadata_version = bcachefs_metadata_version_current; 20181c6fdbd8SKent Overstreet module_param_named(version, bch2_metadata_version, uint, 0400); 20191c6fdbd8SKent Overstreet 20201c6fdbd8SKent Overstreet module_exit(bcachefs_exit); 20211c6fdbd8SKent Overstreet module_init(bcachefs_init); 2022