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); 72337fad949SKent Overstreet init_rwsem(&c->snapshot_create_lock); 7241c6fdbd8SKent Overstreet 7251c6fdbd8SKent Overstreet spin_lock_init(&c->btree_write_error_lock); 7261c6fdbd8SKent Overstreet 7279b6e2f1eSKent Overstreet INIT_WORK(&c->journal_seq_blacklist_gc_work, 7289b6e2f1eSKent Overstreet bch2_blacklist_entries_gc); 7299b6e2f1eSKent Overstreet 7305b593ee1SKent Overstreet INIT_LIST_HEAD(&c->journal_iters); 731f1d786a0SKent Overstreet 7321c6fdbd8SKent Overstreet INIT_LIST_HEAD(&c->fsck_errors); 7331c6fdbd8SKent Overstreet mutex_init(&c->fsck_error_lock); 7341c6fdbd8SKent Overstreet 7351c6fdbd8SKent Overstreet seqcount_init(&c->gc_pos_lock); 7361c6fdbd8SKent Overstreet 7375e82a9a1SKent Overstreet seqcount_init(&c->usage_lock); 7385e82a9a1SKent Overstreet 739ef1b2092SKent Overstreet sema_init(&c->io_in_flight, 128); 740ef1b2092SKent Overstreet 7419edbcc72SKent Overstreet INIT_LIST_HEAD(&c->vfs_inodes_list); 7429edbcc72SKent Overstreet mutex_init(&c->vfs_inodes_lock); 7439edbcc72SKent Overstreet 7441c6fdbd8SKent Overstreet c->copy_gc_enabled = 1; 7451c6fdbd8SKent Overstreet c->rebalance.enabled = 1; 7461c6fdbd8SKent Overstreet c->promote_whole_extents = true; 7471c6fdbd8SKent Overstreet 748991ba021SKent Overstreet c->journal.flush_write_time = &c->times[BCH_TIME_journal_flush_write]; 749991ba021SKent Overstreet c->journal.noflush_write_time = &c->times[BCH_TIME_journal_noflush_write]; 75049a67206SKent Overstreet c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal]; 7511c6fdbd8SKent Overstreet c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq]; 7521c6fdbd8SKent Overstreet 7531c6fdbd8SKent Overstreet bch2_fs_btree_cache_init_early(&c->btree_cache); 7541c6fdbd8SKent Overstreet 755fca1223cSKent Overstreet mutex_init(&c->sectors_available_lock); 756fca1223cSKent Overstreet 757e2b60560SKent Overstreet ret = percpu_init_rwsem(&c->mark_lock); 758e2b60560SKent Overstreet if (ret) 75973e6ab95SKent Overstreet goto err; 76073e6ab95SKent Overstreet 7611c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 762e2b60560SKent Overstreet ret = bch2_sb_to_fs(c, sb); 7631c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 764e2b60560SKent Overstreet 765e2b60560SKent Overstreet if (ret) 7661c6fdbd8SKent Overstreet goto err; 7671c6fdbd8SKent Overstreet 768401ec4dbSKent Overstreet pr_uuid(&name, c->sb.user_uuid.b); 7693e3e02e6SKent Overstreet strscpy(c->name, name.buf, sizeof(c->name)); 770401ec4dbSKent Overstreet printbuf_exit(&name); 771401ec4dbSKent Overstreet 77265d48e35SKent Overstreet ret = name.allocation_failure ? -BCH_ERR_ENOMEM_fs_name_alloc : 0; 773401ec4dbSKent Overstreet if (ret) 774401ec4dbSKent Overstreet goto err; 7751c6fdbd8SKent Overstreet 7762430e72fSKent Overstreet /* Compat: */ 77773bd774dSKent Overstreet if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && 7782430e72fSKent Overstreet !BCH_SB_JOURNAL_FLUSH_DELAY(sb)) 7792430e72fSKent Overstreet SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000); 7802430e72fSKent Overstreet 78173bd774dSKent Overstreet if (le16_to_cpu(sb->version) <= bcachefs_metadata_version_inode_v2 && 7822430e72fSKent Overstreet !BCH_SB_JOURNAL_RECLAIM_DELAY(sb)) 7832430e72fSKent Overstreet SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 100); 7842430e72fSKent Overstreet 7851c6fdbd8SKent Overstreet c->opts = bch2_opts_default; 7868244f320SKent Overstreet ret = bch2_opts_from_sb(&c->opts, sb); 7878244f320SKent Overstreet if (ret) 7888244f320SKent Overstreet goto err; 7898244f320SKent Overstreet 7901c6fdbd8SKent Overstreet bch2_opts_apply(&c->opts, opts); 7911c6fdbd8SKent Overstreet 7927c8f6f98SKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_alloc; 7937c8f6f98SKent Overstreet if (c->opts.inodes_use_key_cache) 7947c8f6f98SKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_inodes; 795aaad530aSKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_logged_ops; 7967c8f6f98SKent Overstreet 7978244f320SKent Overstreet c->block_bits = ilog2(block_sectors(c)); 7981c6fdbd8SKent Overstreet c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c); 7991c6fdbd8SKent Overstreet 8007be9ab63SChris Webb if (bch2_fs_init_fault("fs_alloc")) { 801e2b60560SKent Overstreet bch_err(c, "fs_alloc fault injected"); 802e2b60560SKent Overstreet ret = -EFAULT; 8031c6fdbd8SKent Overstreet goto err; 8047be9ab63SChris Webb } 8051c6fdbd8SKent Overstreet 806ae2f17d5SKent Overstreet iter_size = sizeof(struct sort_iter) + 8071c6fdbd8SKent Overstreet (btree_blocks(c) + 1) * 2 * 808ae2f17d5SKent Overstreet sizeof(struct sort_iter_set); 8091c6fdbd8SKent Overstreet 810b5e8a699SKent Overstreet c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus())); 811b5e8a699SKent Overstreet 812731bdd2eSKent Overstreet if (!(c->btree_update_wq = alloc_workqueue("bcachefs", 813b17d3cecSKent Overstreet WQ_FREEZABLE|WQ_UNBOUND|WQ_MEM_RECLAIM, 512)) || 8149f1833caSKent Overstreet !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io", 8159f2772c4SKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) || 8162f33ece9SKent Overstreet !(c->copygc_wq = alloc_workqueue("bcachefs_copygc", 8172f33ece9SKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) || 818731bdd2eSKent Overstreet !(c->io_complete_wq = alloc_workqueue("bcachefs_io", 819731bdd2eSKent Overstreet WQ_FREEZABLE|WQ_HIGHPRI|WQ_MEM_RECLAIM, 1)) || 8208bff9875SBrian Foster !(c->write_ref_wq = alloc_workqueue("bcachefs_write_ref", 8218bff9875SBrian Foster WQ_FREEZABLE, 0)) || 822d94189adSKent Overstreet #ifndef BCH_WRITE_REF_DEBUG 823134915f3SKent Overstreet percpu_ref_init(&c->writes, bch2_writes_disabled, 824134915f3SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 825d94189adSKent Overstreet #endif 8261c6fdbd8SKent Overstreet mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) || 8271c6fdbd8SKent Overstreet bioset_init(&c->btree_bio, 1, 8281c6fdbd8SKent Overstreet max(offsetof(struct btree_read_bio, bio), 8291c6fdbd8SKent Overstreet offsetof(struct btree_write_bio, wbio.bio)), 8301c6fdbd8SKent Overstreet BIOSET_NEED_BVECS) || 8315663a415SKent Overstreet !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) || 8325e82a9a1SKent Overstreet !(c->online_reserved = alloc_percpu(u64)) || 8331c6fdbd8SKent Overstreet mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1, 8341c6fdbd8SKent Overstreet btree_bytes(c)) || 83535189e09SKent Overstreet mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) || 836b5e8a699SKent Overstreet !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits, 837e2b60560SKent Overstreet sizeof(u64), GFP_KERNEL))) { 83865d48e35SKent Overstreet ret = -BCH_ERR_ENOMEM_fs_other_alloc; 8397be9ab63SChris Webb goto err; 8407be9ab63SChris Webb } 8417be9ab63SChris Webb 842f3b8403eSKent Overstreet ret = bch2_fs_counters_init(c) ?: 843f3b8403eSKent Overstreet bch2_io_clock_init(&c->io_clock[READ]) ?: 844e2b60560SKent Overstreet bch2_io_clock_init(&c->io_clock[WRITE]) ?: 845e2b60560SKent Overstreet bch2_fs_journal_init(&c->journal) ?: 846e2b60560SKent Overstreet bch2_fs_replicas_init(c) ?: 847e2b60560SKent Overstreet bch2_fs_btree_cache_init(c) ?: 848e2b60560SKent Overstreet bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?: 849e2b60560SKent Overstreet bch2_fs_btree_iter_init(c) ?: 850e2b60560SKent Overstreet bch2_fs_btree_interior_update_init(c) ?: 8519b688da3SKent Overstreet bch2_fs_buckets_waiting_for_journal_init(c) ?: 852920e69bcSKent Overstreet bch2_fs_btree_write_buffer_init(c) ?: 853e2b60560SKent Overstreet bch2_fs_subvolumes_init(c) ?: 8541809b8cbSKent Overstreet bch2_fs_io_read_init(c) ?: 8551809b8cbSKent Overstreet bch2_fs_io_write_init(c) ?: 856350175bfSKent Overstreet bch2_fs_nocow_locking_init(c) ?: 857e2b60560SKent Overstreet bch2_fs_encryption_init(c) ?: 858e2b60560SKent Overstreet bch2_fs_compress_init(c) ?: 859e2b60560SKent Overstreet bch2_fs_ec_init(c) ?: 860dbbfca9fSKent Overstreet bch2_fs_fsio_init(c) ?: 861867c1fe0SDan Carpenter bch2_fs_fs_io_buffered_init(c) ?: 862dbbfca9fSKent Overstreet bch2_fs_fs_io_direct_init(c); 8637be9ab63SChris Webb if (ret) 8641c6fdbd8SKent Overstreet goto err; 8651c6fdbd8SKent Overstreet 8661c6fdbd8SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) 8671241df58SHunter Shaffer if (bch2_dev_exists(c->disk_sb.sb, i) && 8687be9ab63SChris Webb bch2_dev_alloc(c, i)) { 869e2b60560SKent Overstreet ret = -EEXIST; 8701c6fdbd8SKent Overstreet goto err; 8717be9ab63SChris Webb } 8721c6fdbd8SKent Overstreet 8734b8f89afSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 8744b8f89afSKent Overstreet &c->btree_root_journal_res, 8754b8f89afSKent Overstreet BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX)); 8764b8f89afSKent Overstreet bch2_dev_usage_journal_reserve(c); 8774b8f89afSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 8784b8f89afSKent Overstreet &c->clock_journal_res, 8794b8f89afSKent Overstreet (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2); 8804b8f89afSKent Overstreet 8811c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 882e2b60560SKent Overstreet ret = bch2_fs_online(c); 8831c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 884e2b60560SKent Overstreet 885e2b60560SKent Overstreet if (ret) 8861c6fdbd8SKent Overstreet goto err; 8871c6fdbd8SKent Overstreet out: 8881c6fdbd8SKent Overstreet return c; 8891c6fdbd8SKent Overstreet err: 8901c6fdbd8SKent Overstreet bch2_fs_free(c); 8917be9ab63SChris Webb c = ERR_PTR(ret); 8921c6fdbd8SKent Overstreet goto out; 8931c6fdbd8SKent Overstreet } 8941c6fdbd8SKent Overstreet 895619f5beeSKent Overstreet noinline_for_stack 896619f5beeSKent Overstreet static void print_mount_opts(struct bch_fs *c) 897619f5beeSKent Overstreet { 898619f5beeSKent Overstreet enum bch_opt_id i; 899fa8e94faSKent Overstreet struct printbuf p = PRINTBUF; 900619f5beeSKent Overstreet bool first = true; 901619f5beeSKent Overstreet 902ef1634f0SKent Overstreet prt_str(&p, "mounting version "); 903e3804b55SKent Overstreet bch2_version_to_text(&p, c->sb.version); 90460573ff5SKent Overstreet 905619f5beeSKent Overstreet if (c->opts.read_only) { 90660573ff5SKent Overstreet prt_str(&p, " opts="); 907619f5beeSKent Overstreet first = false; 90860573ff5SKent Overstreet prt_printf(&p, "ro"); 909619f5beeSKent Overstreet } 910619f5beeSKent Overstreet 911619f5beeSKent Overstreet for (i = 0; i < bch2_opts_nr; i++) { 912619f5beeSKent Overstreet const struct bch_option *opt = &bch2_opt_table[i]; 913619f5beeSKent Overstreet u64 v = bch2_opt_get_by_id(&c->opts, i); 914619f5beeSKent Overstreet 9158244f320SKent Overstreet if (!(opt->flags & OPT_MOUNT)) 916619f5beeSKent Overstreet continue; 917619f5beeSKent Overstreet 918619f5beeSKent Overstreet if (v == bch2_opt_get_by_id(&bch2_opts_default, i)) 919619f5beeSKent Overstreet continue; 920619f5beeSKent Overstreet 92160573ff5SKent Overstreet prt_str(&p, first ? " opts=" : ","); 922619f5beeSKent Overstreet first = false; 9235521b1dfSKent Overstreet bch2_opt_to_text(&p, c, c->disk_sb.sb, opt, v, OPT_SHOW_MOUNT_STYLE); 924619f5beeSKent Overstreet } 925619f5beeSKent Overstreet 92660573ff5SKent Overstreet bch_info(c, "%s", p.buf); 927fa8e94faSKent Overstreet printbuf_exit(&p); 928619f5beeSKent Overstreet } 929619f5beeSKent Overstreet 930619f5beeSKent Overstreet int bch2_fs_start(struct bch_fs *c) 9311c6fdbd8SKent Overstreet { 9321c6fdbd8SKent Overstreet struct bch_dev *ca; 933a420eea6STim Schlueter time64_t now = ktime_get_real_seconds(); 9341c6fdbd8SKent Overstreet unsigned i; 93578c0b75cSKent Overstreet int ret; 9361c6fdbd8SKent Overstreet 937ef1634f0SKent Overstreet print_mount_opts(c); 938ef1634f0SKent Overstreet 9391ada1606SKent Overstreet down_write(&c->state_lock); 9401c6fdbd8SKent Overstreet 941134915f3SKent Overstreet BUG_ON(test_bit(BCH_FS_STARTED, &c->flags)); 9421c6fdbd8SKent Overstreet 9431c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 9441c6fdbd8SKent Overstreet 9453f7b9713SHunter Shaffer ret = bch2_members_v2_init(c); 9463f7b9713SHunter Shaffer if (ret) { 9473f7b9713SHunter Shaffer mutex_unlock(&c->sb_lock); 9483f7b9713SHunter Shaffer goto err; 9493f7b9713SHunter Shaffer } 9503f7b9713SHunter Shaffer 9511c6fdbd8SKent Overstreet for_each_online_member(ca, c, i) 9523f7b9713SHunter Shaffer bch2_members_v2_get_mut(c->disk_sb.sb, i)->last_mount = cpu_to_le64(now); 9531c6fdbd8SKent Overstreet 9541c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 9551c6fdbd8SKent Overstreet 9561c6fdbd8SKent Overstreet for_each_rw_member(ca, c, i) 9571c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 9581c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 9591c6fdbd8SKent Overstreet 9601c6fdbd8SKent Overstreet ret = BCH_SB_INITIALIZED(c->disk_sb.sb) 9611c6fdbd8SKent Overstreet ? bch2_fs_recovery(c) 9621c6fdbd8SKent Overstreet : bch2_fs_initialize(c); 9631c6fdbd8SKent Overstreet if (ret) 9641c6fdbd8SKent Overstreet goto err; 9651c6fdbd8SKent Overstreet 966cd575ddfSKent Overstreet ret = bch2_opts_check_may_set(c); 967cd575ddfSKent Overstreet if (ret) 968cd575ddfSKent Overstreet goto err; 969cd575ddfSKent Overstreet 970e2b60560SKent Overstreet if (bch2_fs_init_fault("fs_start")) { 971e2b60560SKent Overstreet bch_err(c, "fs_start fault injected"); 97278c0b75cSKent Overstreet ret = -EINVAL; 9731c6fdbd8SKent Overstreet goto err; 974e2b60560SKent Overstreet } 9751c6fdbd8SKent Overstreet 976a9310ab0SKent Overstreet set_bit(BCH_FS_STARTED, &c->flags); 977a9310ab0SKent Overstreet 978619f5beeSKent Overstreet if (c->opts.read_only || c->opts.nochanges) { 9791c6fdbd8SKent Overstreet bch2_fs_read_only(c); 9801c6fdbd8SKent Overstreet } else { 981619f5beeSKent Overstreet ret = !test_bit(BCH_FS_RW, &c->flags) 982619f5beeSKent Overstreet ? bch2_fs_read_write(c) 983619f5beeSKent Overstreet : bch2_fs_read_write_late(c); 984619f5beeSKent Overstreet if (ret) 9851c6fdbd8SKent Overstreet goto err; 9861c6fdbd8SKent Overstreet } 9871c6fdbd8SKent Overstreet 988619f5beeSKent Overstreet ret = 0; 9891c6fdbd8SKent Overstreet out: 9901ada1606SKent Overstreet up_write(&c->state_lock); 991619f5beeSKent Overstreet return ret; 9921c6fdbd8SKent Overstreet err: 993e46c181aSKent Overstreet bch_err_msg(c, ret, "starting filesystem"); 9941c6fdbd8SKent Overstreet goto out; 9951c6fdbd8SKent Overstreet } 9961c6fdbd8SKent Overstreet 99778c0b75cSKent Overstreet static int bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c) 9981c6fdbd8SKent Overstreet { 9991241df58SHunter Shaffer struct bch_member m = bch2_sb_member_get(sb, sb->dev_idx); 10001c6fdbd8SKent Overstreet 10018244f320SKent Overstreet if (le16_to_cpu(sb->block_size) != block_sectors(c)) 100278c0b75cSKent Overstreet return -BCH_ERR_mismatched_block_size; 10031c6fdbd8SKent Overstreet 10041241df58SHunter Shaffer if (le16_to_cpu(m.bucket_size) < 10051c6fdbd8SKent Overstreet BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb)) 100678c0b75cSKent Overstreet return -BCH_ERR_bucket_size_too_small; 10071c6fdbd8SKent Overstreet 100878c0b75cSKent Overstreet return 0; 10091c6fdbd8SKent Overstreet } 10101c6fdbd8SKent Overstreet 101178c0b75cSKent Overstreet static int bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb) 10121c6fdbd8SKent Overstreet { 10131c6fdbd8SKent Overstreet struct bch_sb *newest = 10141c6fdbd8SKent Overstreet le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb; 10151c6fdbd8SKent Overstreet 10161c6fdbd8SKent Overstreet if (!uuid_equal(&fs->uuid, &sb->uuid)) 101778c0b75cSKent Overstreet return -BCH_ERR_device_not_a_member_of_filesystem; 10181c6fdbd8SKent Overstreet 10191241df58SHunter Shaffer if (!bch2_dev_exists(newest, sb->dev_idx)) 102078c0b75cSKent Overstreet return -BCH_ERR_device_has_been_removed; 10211c6fdbd8SKent Overstreet 10221c6fdbd8SKent Overstreet if (fs->block_size != sb->block_size) 102378c0b75cSKent Overstreet return -BCH_ERR_mismatched_block_size; 10241c6fdbd8SKent Overstreet 102578c0b75cSKent Overstreet return 0; 10261c6fdbd8SKent Overstreet } 10271c6fdbd8SKent Overstreet 10281c6fdbd8SKent Overstreet /* Device startup/shutdown: */ 10291c6fdbd8SKent Overstreet 10301c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *kobj) 10311c6fdbd8SKent Overstreet { 10321c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); 10331c6fdbd8SKent Overstreet 10341c6fdbd8SKent Overstreet kfree(ca); 10351c6fdbd8SKent Overstreet } 10361c6fdbd8SKent Overstreet 10371c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *ca) 10381c6fdbd8SKent Overstreet { 10391c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 10401c6fdbd8SKent Overstreet 10411c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs && 10421c6fdbd8SKent Overstreet ca->disk_sb.bdev) 10431c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 10441c6fdbd8SKent Overstreet 10451c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) 10461c6fdbd8SKent Overstreet kobject_del(&ca->kobj); 10471c6fdbd8SKent Overstreet 10481c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 10491c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 10501c6fdbd8SKent Overstreet 10511c6fdbd8SKent Overstreet free_percpu(ca->io_done); 10521c6fdbd8SKent Overstreet bioset_exit(&ca->replica_set); 10531c6fdbd8SKent Overstreet bch2_dev_buckets_free(ca); 1054d1170ce5SKent Overstreet free_page((unsigned long) ca->sb_read_scratch); 10551c6fdbd8SKent Overstreet 10561c6fdbd8SKent Overstreet bch2_time_stats_exit(&ca->io_latency[WRITE]); 10571c6fdbd8SKent Overstreet bch2_time_stats_exit(&ca->io_latency[READ]); 10581c6fdbd8SKent Overstreet 10591c6fdbd8SKent Overstreet percpu_ref_exit(&ca->io_ref); 10601c6fdbd8SKent Overstreet percpu_ref_exit(&ca->ref); 10611c6fdbd8SKent Overstreet kobject_put(&ca->kobj); 10621c6fdbd8SKent Overstreet } 10631c6fdbd8SKent Overstreet 10641c6fdbd8SKent Overstreet static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) 10651c6fdbd8SKent Overstreet { 10661c6fdbd8SKent Overstreet 10671c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 10681c6fdbd8SKent Overstreet 10691c6fdbd8SKent Overstreet if (percpu_ref_is_zero(&ca->io_ref)) 10701c6fdbd8SKent Overstreet return; 10711c6fdbd8SKent Overstreet 10721c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 10731c6fdbd8SKent Overstreet 10741c6fdbd8SKent Overstreet reinit_completion(&ca->io_ref_completion); 10751c6fdbd8SKent Overstreet percpu_ref_kill(&ca->io_ref); 10761c6fdbd8SKent Overstreet wait_for_completion(&ca->io_ref_completion); 10771c6fdbd8SKent Overstreet 10781c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) { 10791c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 10801c6fdbd8SKent Overstreet sysfs_remove_link(&ca->kobj, "block"); 10811c6fdbd8SKent Overstreet } 10821c6fdbd8SKent Overstreet 10831c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 10841c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 10851c6fdbd8SKent Overstreet } 10861c6fdbd8SKent Overstreet 10871c6fdbd8SKent Overstreet static void bch2_dev_ref_complete(struct percpu_ref *ref) 10881c6fdbd8SKent Overstreet { 10891c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, ref); 10901c6fdbd8SKent Overstreet 10911c6fdbd8SKent Overstreet complete(&ca->ref_completion); 10921c6fdbd8SKent Overstreet } 10931c6fdbd8SKent Overstreet 10941c6fdbd8SKent Overstreet static void bch2_dev_io_ref_complete(struct percpu_ref *ref) 10951c6fdbd8SKent Overstreet { 10961c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref); 10971c6fdbd8SKent Overstreet 10981c6fdbd8SKent Overstreet complete(&ca->io_ref_completion); 10991c6fdbd8SKent Overstreet } 11001c6fdbd8SKent Overstreet 11011c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) 11021c6fdbd8SKent Overstreet { 11031c6fdbd8SKent Overstreet int ret; 11041c6fdbd8SKent Overstreet 11051c6fdbd8SKent Overstreet if (!c->kobj.state_in_sysfs) 11061c6fdbd8SKent Overstreet return 0; 11071c6fdbd8SKent Overstreet 11081c6fdbd8SKent Overstreet if (!ca->kobj.state_in_sysfs) { 11091c6fdbd8SKent Overstreet ret = kobject_add(&ca->kobj, &c->kobj, 11101c6fdbd8SKent Overstreet "dev-%u", ca->dev_idx); 11111c6fdbd8SKent Overstreet if (ret) 11121c6fdbd8SKent Overstreet return ret; 11131c6fdbd8SKent Overstreet } 11141c6fdbd8SKent Overstreet 11151c6fdbd8SKent Overstreet if (ca->disk_sb.bdev) { 11161c6fdbd8SKent Overstreet struct kobject *block = bdev_kobj(ca->disk_sb.bdev); 11171c6fdbd8SKent Overstreet 11181c6fdbd8SKent Overstreet ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); 11191c6fdbd8SKent Overstreet if (ret) 11201c6fdbd8SKent Overstreet return ret; 11211c6fdbd8SKent Overstreet 11221c6fdbd8SKent Overstreet ret = sysfs_create_link(&ca->kobj, block, "block"); 11231c6fdbd8SKent Overstreet if (ret) 11241c6fdbd8SKent Overstreet return ret; 11251c6fdbd8SKent Overstreet } 11261c6fdbd8SKent Overstreet 11271c6fdbd8SKent Overstreet return 0; 11281c6fdbd8SKent Overstreet } 11291c6fdbd8SKent Overstreet 11301c6fdbd8SKent Overstreet static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c, 11311c6fdbd8SKent Overstreet struct bch_member *member) 11321c6fdbd8SKent Overstreet { 11331c6fdbd8SKent Overstreet struct bch_dev *ca; 11341c6fdbd8SKent Overstreet 11351c6fdbd8SKent Overstreet ca = kzalloc(sizeof(*ca), GFP_KERNEL); 11361c6fdbd8SKent Overstreet if (!ca) 11371c6fdbd8SKent Overstreet return NULL; 11381c6fdbd8SKent Overstreet 11391c6fdbd8SKent Overstreet kobject_init(&ca->kobj, &bch2_dev_ktype); 11401c6fdbd8SKent Overstreet init_completion(&ca->ref_completion); 11411c6fdbd8SKent Overstreet init_completion(&ca->io_ref_completion); 11421c6fdbd8SKent Overstreet 11431c6fdbd8SKent Overstreet init_rwsem(&ca->bucket_lock); 11441c6fdbd8SKent Overstreet 11451c6fdbd8SKent Overstreet INIT_WORK(&ca->io_error_work, bch2_io_error_work); 11461c6fdbd8SKent Overstreet 11471c6fdbd8SKent Overstreet bch2_time_stats_init(&ca->io_latency[READ]); 11481c6fdbd8SKent Overstreet bch2_time_stats_init(&ca->io_latency[WRITE]); 11491c6fdbd8SKent Overstreet 11501c6fdbd8SKent Overstreet ca->mi = bch2_mi_to_cpu(member); 11511c6fdbd8SKent Overstreet ca->uuid = member->uuid; 11521c6fdbd8SKent Overstreet 1153f25d8215SKent Overstreet ca->nr_btree_reserve = DIV_ROUND_UP(BTREE_NODE_RESERVE, 1154f25d8215SKent Overstreet ca->mi.bucket_size / btree_sectors(c)); 1155f25d8215SKent Overstreet 11561c6fdbd8SKent Overstreet if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, 11571c6fdbd8SKent Overstreet 0, GFP_KERNEL) || 11581c6fdbd8SKent Overstreet percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete, 11591c6fdbd8SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 1160d1170ce5SKent Overstreet !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) || 11611c6fdbd8SKent Overstreet bch2_dev_buckets_alloc(c, ca) || 11621c6fdbd8SKent Overstreet bioset_init(&ca->replica_set, 4, 11631c6fdbd8SKent Overstreet offsetof(struct bch_write_bio, bio), 0) || 11641c6fdbd8SKent Overstreet !(ca->io_done = alloc_percpu(*ca->io_done))) 11651c6fdbd8SKent Overstreet goto err; 11661c6fdbd8SKent Overstreet 11671c6fdbd8SKent Overstreet return ca; 11681c6fdbd8SKent Overstreet err: 11691c6fdbd8SKent Overstreet bch2_dev_free(ca); 11701c6fdbd8SKent Overstreet return NULL; 11711c6fdbd8SKent Overstreet } 11721c6fdbd8SKent Overstreet 11731c6fdbd8SKent Overstreet static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, 11741c6fdbd8SKent Overstreet unsigned dev_idx) 11751c6fdbd8SKent Overstreet { 11761c6fdbd8SKent Overstreet ca->dev_idx = dev_idx; 11771c6fdbd8SKent Overstreet __set_bit(ca->dev_idx, ca->self.d); 11781c6fdbd8SKent Overstreet scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); 11791c6fdbd8SKent Overstreet 11801c6fdbd8SKent Overstreet ca->fs = c; 11811c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], ca); 11821c6fdbd8SKent Overstreet 11831c6fdbd8SKent Overstreet if (bch2_dev_sysfs_online(c, ca)) 11841c6fdbd8SKent Overstreet pr_warn("error creating sysfs objects"); 11851c6fdbd8SKent Overstreet } 11861c6fdbd8SKent Overstreet 11871c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx) 11881c6fdbd8SKent Overstreet { 11891241df58SHunter Shaffer struct bch_member member = bch2_sb_member_get(c->disk_sb.sb, dev_idx); 11901c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 11911c6fdbd8SKent Overstreet int ret = 0; 11921c6fdbd8SKent Overstreet 11931c6fdbd8SKent Overstreet if (bch2_fs_init_fault("dev_alloc")) 11941c6fdbd8SKent Overstreet goto err; 11951c6fdbd8SKent Overstreet 11961241df58SHunter Shaffer ca = __bch2_dev_alloc(c, &member); 11971c6fdbd8SKent Overstreet if (!ca) 11981c6fdbd8SKent Overstreet goto err; 11991c6fdbd8SKent Overstreet 1200220d2062SKent Overstreet ca->fs = c; 1201220d2062SKent Overstreet 12021c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 12031c6fdbd8SKent Overstreet return ret; 12041c6fdbd8SKent Overstreet err: 12051c6fdbd8SKent Overstreet if (ca) 12061c6fdbd8SKent Overstreet bch2_dev_free(ca); 1207c8b4534dSKent Overstreet return -BCH_ERR_ENOMEM_dev_alloc; 12081c6fdbd8SKent Overstreet } 12091c6fdbd8SKent Overstreet 12101c6fdbd8SKent Overstreet static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb) 12111c6fdbd8SKent Overstreet { 12121c6fdbd8SKent Overstreet unsigned ret; 12131c6fdbd8SKent Overstreet 12141c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca)) { 12151c6fdbd8SKent Overstreet bch_err(ca, "already have device online in slot %u", 12161c6fdbd8SKent Overstreet sb->sb->dev_idx); 121778c0b75cSKent Overstreet return -BCH_ERR_device_already_online; 12181c6fdbd8SKent Overstreet } 12191c6fdbd8SKent Overstreet 12201c6fdbd8SKent Overstreet if (get_capacity(sb->bdev->bd_disk) < 12211c6fdbd8SKent Overstreet ca->mi.bucket_size * ca->mi.nbuckets) { 12221c6fdbd8SKent Overstreet bch_err(ca, "cannot online: device too small"); 122378c0b75cSKent Overstreet return -BCH_ERR_device_size_too_small; 12241c6fdbd8SKent Overstreet } 12251c6fdbd8SKent Overstreet 12261c6fdbd8SKent Overstreet BUG_ON(!percpu_ref_is_zero(&ca->io_ref)); 12271c6fdbd8SKent Overstreet 12281c6fdbd8SKent Overstreet ret = bch2_dev_journal_init(ca, sb->sb); 12291c6fdbd8SKent Overstreet if (ret) 12301c6fdbd8SKent Overstreet return ret; 12311c6fdbd8SKent Overstreet 12321c6fdbd8SKent Overstreet /* Commit: */ 12331c6fdbd8SKent Overstreet ca->disk_sb = *sb; 12341c6fdbd8SKent Overstreet memset(sb, 0, sizeof(*sb)); 12351c6fdbd8SKent Overstreet 1236eacb2574SKent Overstreet ca->dev = ca->disk_sb.bdev->bd_dev; 1237eacb2574SKent Overstreet 12381c6fdbd8SKent Overstreet percpu_ref_reinit(&ca->io_ref); 12391c6fdbd8SKent Overstreet 12401c6fdbd8SKent Overstreet return 0; 12411c6fdbd8SKent Overstreet } 12421c6fdbd8SKent Overstreet 12431c6fdbd8SKent Overstreet static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb) 12441c6fdbd8SKent Overstreet { 12451c6fdbd8SKent Overstreet struct bch_dev *ca; 12461c6fdbd8SKent Overstreet int ret; 12471c6fdbd8SKent Overstreet 12481c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 12491c6fdbd8SKent Overstreet 12501c6fdbd8SKent Overstreet if (le64_to_cpu(sb->sb->seq) > 12511c6fdbd8SKent Overstreet le64_to_cpu(c->disk_sb.sb->seq)) 12521c6fdbd8SKent Overstreet bch2_sb_to_fs(c, sb->sb); 12531c6fdbd8SKent Overstreet 12541c6fdbd8SKent Overstreet BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices || 12551c6fdbd8SKent Overstreet !c->devs[sb->sb->dev_idx]); 12561c6fdbd8SKent Overstreet 12571c6fdbd8SKent Overstreet ca = bch_dev_locked(c, sb->sb->dev_idx); 12581c6fdbd8SKent Overstreet 12591c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, sb); 12601c6fdbd8SKent Overstreet if (ret) 12611c6fdbd8SKent Overstreet return ret; 12621c6fdbd8SKent Overstreet 12631c6fdbd8SKent Overstreet bch2_dev_sysfs_online(c, ca); 12641c6fdbd8SKent Overstreet 12651c6fdbd8SKent Overstreet if (c->sb.nr_devices == 1) 12661c6fdbd8SKent Overstreet snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev); 12671c6fdbd8SKent Overstreet snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev); 12681c6fdbd8SKent Overstreet 12691c6fdbd8SKent Overstreet rebalance_wakeup(c); 12701c6fdbd8SKent Overstreet return 0; 12711c6fdbd8SKent Overstreet } 12721c6fdbd8SKent Overstreet 12731c6fdbd8SKent Overstreet /* Device management: */ 12741c6fdbd8SKent Overstreet 12751c6fdbd8SKent Overstreet /* 12761c6fdbd8SKent Overstreet * Note: this function is also used by the error paths - when a particular 12771c6fdbd8SKent Overstreet * device sees an error, we call it to determine whether we can just set the 12781c6fdbd8SKent Overstreet * device RO, or - if this function returns false - we'll set the whole 12791c6fdbd8SKent Overstreet * filesystem RO: 12801c6fdbd8SKent Overstreet * 12811c6fdbd8SKent Overstreet * XXX: maybe we should be more explicit about whether we're changing state 12821c6fdbd8SKent Overstreet * because we got an error or what have you? 12831c6fdbd8SKent Overstreet */ 12841c6fdbd8SKent Overstreet bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, 12851c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 12861c6fdbd8SKent Overstreet { 12871c6fdbd8SKent Overstreet struct bch_devs_mask new_online_devs; 12881c6fdbd8SKent Overstreet struct bch_dev *ca2; 12891c6fdbd8SKent Overstreet int i, nr_rw = 0, required; 12901c6fdbd8SKent Overstreet 12911c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 12921c6fdbd8SKent Overstreet 12931c6fdbd8SKent Overstreet switch (new_state) { 12942436cb9fSKent Overstreet case BCH_MEMBER_STATE_rw: 12951c6fdbd8SKent Overstreet return true; 12962436cb9fSKent Overstreet case BCH_MEMBER_STATE_ro: 12972436cb9fSKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_rw) 12981c6fdbd8SKent Overstreet return true; 12991c6fdbd8SKent Overstreet 13001c6fdbd8SKent Overstreet /* do we have enough devices to write to? */ 13011c6fdbd8SKent Overstreet for_each_member_device(ca2, c, i) 13021c6fdbd8SKent Overstreet if (ca2 != ca) 13032436cb9fSKent Overstreet nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw; 13041c6fdbd8SKent Overstreet 13051c6fdbd8SKent Overstreet required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED) 13061c6fdbd8SKent Overstreet ? c->opts.metadata_replicas 13071c6fdbd8SKent Overstreet : c->opts.metadata_replicas_required, 13081c6fdbd8SKent Overstreet !(flags & BCH_FORCE_IF_DATA_DEGRADED) 13091c6fdbd8SKent Overstreet ? c->opts.data_replicas 13101c6fdbd8SKent Overstreet : c->opts.data_replicas_required); 13111c6fdbd8SKent Overstreet 13121c6fdbd8SKent Overstreet return nr_rw >= required; 13132436cb9fSKent Overstreet case BCH_MEMBER_STATE_failed: 13142436cb9fSKent Overstreet case BCH_MEMBER_STATE_spare: 13152436cb9fSKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_rw && 13162436cb9fSKent Overstreet ca->mi.state != BCH_MEMBER_STATE_ro) 13171c6fdbd8SKent Overstreet return true; 13181c6fdbd8SKent Overstreet 13191c6fdbd8SKent Overstreet /* do we have enough devices to read from? */ 13201c6fdbd8SKent Overstreet new_online_devs = bch2_online_devs(c); 13211c6fdbd8SKent Overstreet __clear_bit(ca->dev_idx, new_online_devs.d); 13221c6fdbd8SKent Overstreet 1323fcb3431bSKent Overstreet return bch2_have_enough_devs(c, new_online_devs, flags, false); 13241c6fdbd8SKent Overstreet default: 13251c6fdbd8SKent Overstreet BUG(); 13261c6fdbd8SKent Overstreet } 13271c6fdbd8SKent Overstreet } 13281c6fdbd8SKent Overstreet 13291c6fdbd8SKent Overstreet static bool bch2_fs_may_start(struct bch_fs *c) 13301c6fdbd8SKent Overstreet { 13311c6fdbd8SKent Overstreet struct bch_dev *ca; 1332fcb3431bSKent Overstreet unsigned i, flags = 0; 13331c6fdbd8SKent Overstreet 1334fcb3431bSKent Overstreet if (c->opts.very_degraded) 1335fcb3431bSKent Overstreet flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST; 1336fcb3431bSKent Overstreet 1337fcb3431bSKent Overstreet if (c->opts.degraded) 1338fcb3431bSKent Overstreet flags |= BCH_FORCE_IF_DEGRADED; 1339fcb3431bSKent Overstreet 1340fcb3431bSKent Overstreet if (!c->opts.degraded && 1341fcb3431bSKent Overstreet !c->opts.very_degraded) { 13421c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 13431c6fdbd8SKent Overstreet 13441c6fdbd8SKent Overstreet for (i = 0; i < c->disk_sb.sb->nr_devices; i++) { 13451241df58SHunter Shaffer if (!bch2_dev_exists(c->disk_sb.sb, i)) 13461c6fdbd8SKent Overstreet continue; 13471c6fdbd8SKent Overstreet 13481c6fdbd8SKent Overstreet ca = bch_dev_locked(c, i); 13491c6fdbd8SKent Overstreet 13501c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca) && 13512436cb9fSKent Overstreet (ca->mi.state == BCH_MEMBER_STATE_rw || 13522436cb9fSKent Overstreet ca->mi.state == BCH_MEMBER_STATE_ro)) { 13531c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 13541c6fdbd8SKent Overstreet return false; 13551c6fdbd8SKent Overstreet } 13561c6fdbd8SKent Overstreet } 13571c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 13581c6fdbd8SKent Overstreet } 13591c6fdbd8SKent Overstreet 1360fcb3431bSKent Overstreet return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true); 13611c6fdbd8SKent Overstreet } 13621c6fdbd8SKent Overstreet 13631c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) 13641c6fdbd8SKent Overstreet { 13651c6fdbd8SKent Overstreet /* 13661c6fdbd8SKent Overstreet * The allocator thread itself allocates btree nodes, so stop it first: 13671c6fdbd8SKent Overstreet */ 13681c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 13691c6fdbd8SKent Overstreet bch2_dev_journal_stop(&c->journal, ca); 13701c6fdbd8SKent Overstreet } 13711c6fdbd8SKent Overstreet 1372f25d8215SKent Overstreet static void __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) 13731c6fdbd8SKent Overstreet { 13741c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 13751c6fdbd8SKent Overstreet 13762436cb9fSKent Overstreet BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw); 13771c6fdbd8SKent Overstreet 13781c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 13791c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 13801c6fdbd8SKent Overstreet } 13811c6fdbd8SKent Overstreet 13821c6fdbd8SKent Overstreet int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 13831c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 13841c6fdbd8SKent Overstreet { 13853f7b9713SHunter Shaffer struct bch_member *m; 13861c6fdbd8SKent Overstreet int ret = 0; 13871c6fdbd8SKent Overstreet 13881c6fdbd8SKent Overstreet if (ca->mi.state == new_state) 13891c6fdbd8SKent Overstreet return 0; 13901c6fdbd8SKent Overstreet 13911c6fdbd8SKent Overstreet if (!bch2_dev_state_allowed(c, ca, new_state, flags)) 139278c0b75cSKent Overstreet return -BCH_ERR_device_state_not_allowed; 13931c6fdbd8SKent Overstreet 13942436cb9fSKent Overstreet if (new_state != BCH_MEMBER_STATE_rw) 13951c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 13961c6fdbd8SKent Overstreet 13972436cb9fSKent Overstreet bch_notice(ca, "%s", bch2_member_states[new_state]); 13981c6fdbd8SKent Overstreet 13991c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 14003f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 14013f7b9713SHunter Shaffer SET_BCH_MEMBER_STATE(m, new_state); 14021c6fdbd8SKent Overstreet bch2_write_super(c); 14031c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 14041c6fdbd8SKent Overstreet 1405e2b60560SKent Overstreet if (new_state == BCH_MEMBER_STATE_rw) 1406f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 14071c6fdbd8SKent Overstreet 14081c6fdbd8SKent Overstreet rebalance_wakeup(c); 14091c6fdbd8SKent Overstreet 14101c6fdbd8SKent Overstreet return ret; 14111c6fdbd8SKent Overstreet } 14121c6fdbd8SKent Overstreet 14131c6fdbd8SKent Overstreet int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 14141c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 14151c6fdbd8SKent Overstreet { 14161c6fdbd8SKent Overstreet int ret; 14171c6fdbd8SKent Overstreet 14181ada1606SKent Overstreet down_write(&c->state_lock); 14191c6fdbd8SKent Overstreet ret = __bch2_dev_set_state(c, ca, new_state, flags); 14201ada1606SKent Overstreet up_write(&c->state_lock); 14211c6fdbd8SKent Overstreet 14221c6fdbd8SKent Overstreet return ret; 14231c6fdbd8SKent Overstreet } 14241c6fdbd8SKent Overstreet 14251c6fdbd8SKent Overstreet /* Device add/removal: */ 14261c6fdbd8SKent Overstreet 1427c0ebe3e4SKent Overstreet static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca) 14285d20ba48SKent Overstreet { 1429c6b2826cSKent Overstreet struct bpos start = POS(ca->dev_idx, 0); 1430c6b2826cSKent Overstreet struct bpos end = POS(ca->dev_idx, U64_MAX); 14315d20ba48SKent Overstreet int ret; 14325d20ba48SKent Overstreet 1433a9c0a4cbSKent Overstreet /* 1434a9c0a4cbSKent Overstreet * We clear the LRU and need_discard btrees first so that we don't race 1435a9c0a4cbSKent Overstreet * with bch2_do_invalidates() and bch2_do_discards() 1436a9c0a4cbSKent Overstreet */ 1437a9c0a4cbSKent Overstreet ret = bch2_btree_delete_range(c, BTREE_ID_lru, start, end, 1438a9c0a4cbSKent Overstreet BTREE_TRIGGER_NORUN, NULL) ?: 1439a9c0a4cbSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_need_discard, start, end, 1440c6b2826cSKent Overstreet BTREE_TRIGGER_NORUN, NULL) ?: 1441c6b2826cSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_freespace, start, end, 1442c6b2826cSKent Overstreet BTREE_TRIGGER_NORUN, NULL) ?: 1443a8c752bbSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_backpointers, start, end, 1444a8c752bbSKent Overstreet BTREE_TRIGGER_NORUN, NULL) ?: 1445a9c0a4cbSKent Overstreet bch2_btree_delete_range(c, BTREE_ID_alloc, start, end, 144602d51bb9SBrian Foster BTREE_TRIGGER_NORUN, NULL) ?: 144702d51bb9SBrian Foster bch2_btree_delete_range(c, BTREE_ID_bucket_gens, start, end, 1448c6b2826cSKent Overstreet BTREE_TRIGGER_NORUN, NULL); 14495d20ba48SKent Overstreet if (ret) 1450e46c181aSKent Overstreet bch_err_msg(c, ret, "removing dev alloc info"); 14515d20ba48SKent Overstreet 1452c6b2826cSKent Overstreet return ret; 14535d20ba48SKent Overstreet } 14545d20ba48SKent Overstreet 14551c6fdbd8SKent Overstreet int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) 14561c6fdbd8SKent Overstreet { 14573f7b9713SHunter Shaffer struct bch_member *m; 14581c6fdbd8SKent Overstreet unsigned dev_idx = ca->dev_idx, data; 145978c0b75cSKent Overstreet int ret; 14601c6fdbd8SKent Overstreet 14611ada1606SKent Overstreet down_write(&c->state_lock); 14621c6fdbd8SKent Overstreet 146331ba2cd3SKent Overstreet /* 146431ba2cd3SKent Overstreet * We consume a reference to ca->ref, regardless of whether we succeed 146531ba2cd3SKent Overstreet * or fail: 146631ba2cd3SKent Overstreet */ 146731ba2cd3SKent Overstreet percpu_ref_put(&ca->ref); 14681c6fdbd8SKent Overstreet 14692436cb9fSKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 14701c6fdbd8SKent Overstreet bch_err(ca, "Cannot remove without losing data"); 147178c0b75cSKent Overstreet ret = -BCH_ERR_device_state_not_allowed; 14721c6fdbd8SKent Overstreet goto err; 14731c6fdbd8SKent Overstreet } 14741c6fdbd8SKent Overstreet 14751c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 14761c6fdbd8SKent Overstreet 14771c6fdbd8SKent Overstreet ret = bch2_dev_data_drop(c, ca->dev_idx, flags); 14781c6fdbd8SKent Overstreet if (ret) { 1479e46c181aSKent Overstreet bch_err_msg(ca, ret, "dropping data"); 14801c6fdbd8SKent Overstreet goto err; 14811c6fdbd8SKent Overstreet } 14821c6fdbd8SKent Overstreet 14835d20ba48SKent Overstreet ret = bch2_dev_remove_alloc(c, ca); 14841c6fdbd8SKent Overstreet if (ret) { 1485e46c181aSKent Overstreet bch_err_msg(ca, ret, "deleting alloc info"); 14861c6fdbd8SKent Overstreet goto err; 14871c6fdbd8SKent Overstreet } 14881c6fdbd8SKent Overstreet 1489a9c0a4cbSKent Overstreet ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); 1490a9c0a4cbSKent Overstreet if (ret) { 1491e46c181aSKent Overstreet bch_err_msg(ca, ret, "flushing journal"); 1492a9c0a4cbSKent Overstreet goto err; 1493a9c0a4cbSKent Overstreet } 1494a9c0a4cbSKent Overstreet 1495a9c0a4cbSKent Overstreet ret = bch2_journal_flush(&c->journal); 14961c6fdbd8SKent Overstreet if (ret) { 1497e46c181aSKent Overstreet bch_err(ca, "journal error"); 14981c6fdbd8SKent Overstreet goto err; 14991c6fdbd8SKent Overstreet } 15001c6fdbd8SKent Overstreet 150131ba2cd3SKent Overstreet ret = bch2_replicas_gc2(c); 150231ba2cd3SKent Overstreet if (ret) { 1503e46c181aSKent Overstreet bch_err_msg(ca, ret, "in replicas_gc2()"); 150431ba2cd3SKent Overstreet goto err; 150531ba2cd3SKent Overstreet } 150631ba2cd3SKent Overstreet 150731ba2cd3SKent Overstreet data = bch2_dev_has_data(c, ca); 150831ba2cd3SKent Overstreet if (data) { 1509fa8e94faSKent Overstreet struct printbuf data_has = PRINTBUF; 151031ba2cd3SKent Overstreet 1511401ec4dbSKent Overstreet prt_bitflags(&data_has, bch2_data_types, data); 1512fa8e94faSKent Overstreet bch_err(ca, "Remove failed, still has data (%s)", data_has.buf); 1513fa8e94faSKent Overstreet printbuf_exit(&data_has); 151431ba2cd3SKent Overstreet ret = -EBUSY; 151531ba2cd3SKent Overstreet goto err; 151631ba2cd3SKent Overstreet } 151731ba2cd3SKent Overstreet 15181c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 15191c6fdbd8SKent Overstreet 15201c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15211c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], NULL); 15221c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15231c6fdbd8SKent Overstreet 15241c6fdbd8SKent Overstreet percpu_ref_kill(&ca->ref); 15251c6fdbd8SKent Overstreet wait_for_completion(&ca->ref_completion); 15261c6fdbd8SKent Overstreet 15271c6fdbd8SKent Overstreet bch2_dev_free(ca); 15281c6fdbd8SKent Overstreet 15291c6fdbd8SKent Overstreet /* 1530bc652905SBrian Foster * At this point the device object has been removed in-core, but the 1531bc652905SBrian Foster * on-disk journal might still refer to the device index via sb device 1532bc652905SBrian Foster * usage entries. Recovery fails if it sees usage information for an 1533bc652905SBrian Foster * invalid device. Flush journal pins to push the back of the journal 1534bc652905SBrian Foster * past now invalid device index references before we update the 1535bc652905SBrian Foster * superblock, but after the device object has been removed so any 1536bc652905SBrian Foster * further journal writes elide usage info for the device. 1537bc652905SBrian Foster */ 1538bc652905SBrian Foster bch2_journal_flush_all_pins(&c->journal); 1539bc652905SBrian Foster 1540bc652905SBrian Foster /* 15411c6fdbd8SKent Overstreet * Free this device's slot in the bch_member array - all pointers to 15421c6fdbd8SKent Overstreet * this device must be gone: 15431c6fdbd8SKent Overstreet */ 15441c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15453f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx); 15463f7b9713SHunter Shaffer memset(&m->uuid, 0, sizeof(m->uuid)); 15471c6fdbd8SKent Overstreet 15481c6fdbd8SKent Overstreet bch2_write_super(c); 15491c6fdbd8SKent Overstreet 15501c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15511ada1606SKent Overstreet up_write(&c->state_lock); 1552180fb49dSKent Overstreet 1553180fb49dSKent Overstreet bch2_dev_usage_journal_reserve(c); 15541c6fdbd8SKent Overstreet return 0; 15551c6fdbd8SKent Overstreet err: 15562436cb9fSKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw && 1557d3bb629dSKent Overstreet !percpu_ref_is_zero(&ca->io_ref)) 15581c6fdbd8SKent Overstreet __bch2_dev_read_write(c, ca); 15591ada1606SKent Overstreet up_write(&c->state_lock); 15601c6fdbd8SKent Overstreet return ret; 15611c6fdbd8SKent Overstreet } 15621c6fdbd8SKent Overstreet 15631c6fdbd8SKent Overstreet /* Add new device to running filesystem: */ 15641c6fdbd8SKent Overstreet int bch2_dev_add(struct bch_fs *c, const char *path) 15651c6fdbd8SKent Overstreet { 15661c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 15671c6fdbd8SKent Overstreet struct bch_sb_handle sb; 15681c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 15693f7b9713SHunter Shaffer struct bch_sb_field_members_v2 *mi; 15701c6fdbd8SKent Overstreet struct bch_member dev_mi; 15711c6fdbd8SKent Overstreet unsigned dev_idx, nr_devices, u64s; 1572fa8e94faSKent Overstreet struct printbuf errbuf = PRINTBUF; 157302afcb8cSKent Overstreet struct printbuf label = PRINTBUF; 15741c6fdbd8SKent Overstreet int ret; 15751c6fdbd8SKent Overstreet 15761c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 1577e8536925SKent Overstreet if (ret) { 1578e46c181aSKent Overstreet bch_err_msg(c, ret, "reading super"); 1579efe68e1dSKent Overstreet goto err; 1580e8536925SKent Overstreet } 15811c6fdbd8SKent Overstreet 15821241df58SHunter Shaffer dev_mi = bch2_sb_member_get(sb.sb, sb.sb->dev_idx); 15831c6fdbd8SKent Overstreet 158402afcb8cSKent Overstreet if (BCH_MEMBER_GROUP(&dev_mi)) { 1585*e677179bSKent Overstreet bch2_disk_path_to_text_sb(&label, sb.sb, BCH_MEMBER_GROUP(&dev_mi) - 1); 158602afcb8cSKent Overstreet if (label.allocation_failure) { 158702afcb8cSKent Overstreet ret = -ENOMEM; 158802afcb8cSKent Overstreet goto err; 158902afcb8cSKent Overstreet } 159002afcb8cSKent Overstreet } 159102afcb8cSKent Overstreet 159278c0b75cSKent Overstreet ret = bch2_dev_may_add(sb.sb, c); 159378c0b75cSKent Overstreet if (ret) { 1594e46c181aSKent Overstreet bch_err_fn(c, ret); 1595efe68e1dSKent Overstreet goto err; 1596e8536925SKent Overstreet } 15971c6fdbd8SKent Overstreet 15981c6fdbd8SKent Overstreet ca = __bch2_dev_alloc(c, &dev_mi); 15991c6fdbd8SKent Overstreet if (!ca) { 1600efe68e1dSKent Overstreet ret = -ENOMEM; 1601efe68e1dSKent Overstreet goto err; 16021c6fdbd8SKent Overstreet } 16031c6fdbd8SKent Overstreet 1604822835ffSKent Overstreet bch2_dev_usage_init(ca); 1605822835ffSKent Overstreet 16061c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, &sb); 160771933fb6SChristophe JAILLET if (ret) 1608efe68e1dSKent Overstreet goto err; 16091c6fdbd8SKent Overstreet 16101c6fdbd8SKent Overstreet ret = bch2_dev_journal_alloc(ca); 1611e8536925SKent Overstreet if (ret) { 1612e46c181aSKent Overstreet bch_err_msg(c, ret, "allocating journal"); 16131c6fdbd8SKent Overstreet goto err; 1614e8536925SKent Overstreet } 16151c6fdbd8SKent Overstreet 16161ada1606SKent Overstreet down_write(&c->state_lock); 16171c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 16181c6fdbd8SKent Overstreet 16191c6fdbd8SKent Overstreet ret = bch2_sb_from_fs(c, ca); 1620e8536925SKent Overstreet if (ret) { 1621e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 16221c6fdbd8SKent Overstreet goto err_unlock; 1623e8536925SKent Overstreet } 16241c6fdbd8SKent Overstreet 16254637429eSKent Overstreet mi = bch2_sb_field_get(ca->disk_sb.sb, members_v2); 16261c6fdbd8SKent Overstreet 16274637429eSKent Overstreet if (!bch2_sb_field_resize(&ca->disk_sb, members_v2, 16281c6fdbd8SKent Overstreet le32_to_cpu(mi->field.u64s) + 16291c6fdbd8SKent Overstreet sizeof(dev_mi) / sizeof(u64))) { 1630098ef98dSKent Overstreet ret = -BCH_ERR_ENOSPC_sb_members; 1631e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 16321c6fdbd8SKent Overstreet goto err_unlock; 16331c6fdbd8SKent Overstreet } 16341c6fdbd8SKent Overstreet 16351c6fdbd8SKent Overstreet if (dynamic_fault("bcachefs:add:no_slot")) 16361c6fdbd8SKent Overstreet goto no_slot; 16371c6fdbd8SKent Overstreet 16381c6fdbd8SKent Overstreet for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++) 16391241df58SHunter Shaffer if (!bch2_dev_exists(c->disk_sb.sb, dev_idx)) 16401c6fdbd8SKent Overstreet goto have_slot; 16411c6fdbd8SKent Overstreet no_slot: 1642098ef98dSKent Overstreet ret = -BCH_ERR_ENOSPC_sb_members; 1643e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 16441c6fdbd8SKent Overstreet goto err_unlock; 16451c6fdbd8SKent Overstreet 16461c6fdbd8SKent Overstreet have_slot: 16471c6fdbd8SKent Overstreet nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices); 16483f7b9713SHunter Shaffer u64s = DIV_ROUND_UP(sizeof(struct bch_sb_field_members_v2) + 16493f7b9713SHunter Shaffer le16_to_cpu(mi->member_bytes) * nr_devices, sizeof(u64)); 16501c6fdbd8SKent Overstreet 16514637429eSKent Overstreet mi = bch2_sb_field_resize(&c->disk_sb, members_v2, u64s); 1652e8536925SKent Overstreet if (!mi) { 1653098ef98dSKent Overstreet ret = -BCH_ERR_ENOSPC_sb_members; 1654e46c181aSKent Overstreet bch_err_msg(c, ret, "setting up new superblock"); 16551c6fdbd8SKent Overstreet goto err_unlock; 1656e8536925SKent Overstreet } 16573f7b9713SHunter Shaffer struct bch_member *m = bch2_members_v2_get_mut(c->disk_sb.sb, dev_idx); 16581c6fdbd8SKent Overstreet 16591c6fdbd8SKent Overstreet /* success: */ 16601c6fdbd8SKent Overstreet 16613f7b9713SHunter Shaffer *m = dev_mi; 16623f7b9713SHunter Shaffer m->last_mount = cpu_to_le64(ktime_get_real_seconds()); 16631c6fdbd8SKent Overstreet c->disk_sb.sb->nr_devices = nr_devices; 16641c6fdbd8SKent Overstreet 16651c6fdbd8SKent Overstreet ca->disk_sb.sb->dev_idx = dev_idx; 16661c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 16671c6fdbd8SKent Overstreet 166802afcb8cSKent Overstreet if (BCH_MEMBER_GROUP(&dev_mi)) { 166902afcb8cSKent Overstreet ret = __bch2_dev_group_set(c, ca, label.buf); 167002afcb8cSKent Overstreet if (ret) { 1671e46c181aSKent Overstreet bch_err_msg(c, ret, "creating new label"); 167202afcb8cSKent Overstreet goto err_unlock; 167302afcb8cSKent Overstreet } 167402afcb8cSKent Overstreet } 167502afcb8cSKent Overstreet 16761c6fdbd8SKent Overstreet bch2_write_super(c); 16771c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 16781c6fdbd8SKent Overstreet 1679180fb49dSKent Overstreet bch2_dev_usage_journal_reserve(c); 1680180fb49dSKent Overstreet 1681d62ab355SKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca); 1682e8536925SKent Overstreet if (ret) { 1683bbe682c7SKent Overstreet bch_err_msg(ca, ret, "marking new superblock"); 1684bfcf840dSKent Overstreet goto err_late; 1685e8536925SKent Overstreet } 16868d6b6222SKent Overstreet 1687c6b2826cSKent Overstreet ret = bch2_fs_freespace_init(c); 1688c6b2826cSKent Overstreet if (ret) { 1689bbe682c7SKent Overstreet bch_err_msg(ca, ret, "initializing free space"); 1690c6b2826cSKent Overstreet goto err_late; 1691c6b2826cSKent Overstreet } 1692c6b2826cSKent Overstreet 169309943313SKent Overstreet ca->new_fs_bucket_idx = 0; 169409943313SKent Overstreet 1695f25d8215SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw) 1696f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 16971c6fdbd8SKent Overstreet 16981ada1606SKent Overstreet up_write(&c->state_lock); 16991c6fdbd8SKent Overstreet return 0; 17001c6fdbd8SKent Overstreet 17011c6fdbd8SKent Overstreet err_unlock: 17021c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17031ada1606SKent Overstreet up_write(&c->state_lock); 17041c6fdbd8SKent Overstreet err: 17051c6fdbd8SKent Overstreet if (ca) 17061c6fdbd8SKent Overstreet bch2_dev_free(ca); 17071c6fdbd8SKent Overstreet bch2_free_super(&sb); 170802afcb8cSKent Overstreet printbuf_exit(&label); 1709fa8e94faSKent Overstreet printbuf_exit(&errbuf); 17101c6fdbd8SKent Overstreet return ret; 17111c6fdbd8SKent Overstreet err_late: 1712bfcf840dSKent Overstreet up_write(&c->state_lock); 1713efe68e1dSKent Overstreet ca = NULL; 1714efe68e1dSKent Overstreet goto err; 17151c6fdbd8SKent Overstreet } 17161c6fdbd8SKent Overstreet 17171c6fdbd8SKent Overstreet /* Hot add existing device to running filesystem: */ 17181c6fdbd8SKent Overstreet int bch2_dev_online(struct bch_fs *c, const char *path) 17191c6fdbd8SKent Overstreet { 17201c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 17211c6fdbd8SKent Overstreet struct bch_sb_handle sb = { NULL }; 17221c6fdbd8SKent Overstreet struct bch_dev *ca; 17231c6fdbd8SKent Overstreet unsigned dev_idx; 17241c6fdbd8SKent Overstreet int ret; 17251c6fdbd8SKent Overstreet 17261ada1606SKent Overstreet down_write(&c->state_lock); 17271c6fdbd8SKent Overstreet 17281c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 17291c6fdbd8SKent Overstreet if (ret) { 17301ada1606SKent Overstreet up_write(&c->state_lock); 17311c6fdbd8SKent Overstreet return ret; 17321c6fdbd8SKent Overstreet } 17331c6fdbd8SKent Overstreet 17341c6fdbd8SKent Overstreet dev_idx = sb.sb->dev_idx; 17351c6fdbd8SKent Overstreet 173678c0b75cSKent Overstreet ret = bch2_dev_in_fs(c->disk_sb.sb, sb.sb); 173778c0b75cSKent Overstreet if (ret) { 1738e46c181aSKent Overstreet bch_err_msg(c, ret, "bringing %s online", path); 17391c6fdbd8SKent Overstreet goto err; 17401c6fdbd8SKent Overstreet } 17411c6fdbd8SKent Overstreet 1742e2b60560SKent Overstreet ret = bch2_dev_attach_bdev(c, &sb); 1743e2b60560SKent Overstreet if (ret) 1744e2b60560SKent Overstreet goto err; 1745e2b60560SKent Overstreet 17461c6fdbd8SKent Overstreet ca = bch_dev_locked(c, dev_idx); 1747bfcf840dSKent Overstreet 1748e2b60560SKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca); 1749e2b60560SKent Overstreet if (ret) { 1750e46c181aSKent Overstreet bch_err_msg(c, ret, "bringing %s online: error from bch2_trans_mark_dev_sb", path); 1751bfcf840dSKent Overstreet goto err; 1752bfcf840dSKent Overstreet } 1753bfcf840dSKent Overstreet 1754f25d8215SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw) 1755f25d8215SKent Overstreet __bch2_dev_read_write(c, ca); 17561c6fdbd8SKent Overstreet 1757bbe682c7SKent Overstreet if (!ca->mi.freespace_initialized) { 1758bbe682c7SKent Overstreet ret = bch2_dev_freespace_init(c, ca, 0, ca->mi.nbuckets); 1759bbe682c7SKent Overstreet bch_err_msg(ca, ret, "initializing free space"); 1760bbe682c7SKent Overstreet if (ret) 1761bbe682c7SKent Overstreet goto err; 1762bbe682c7SKent Overstreet } 1763bbe682c7SKent Overstreet 1764bbe682c7SKent Overstreet if (!ca->journal.nr) { 1765bbe682c7SKent Overstreet ret = bch2_dev_journal_alloc(ca); 1766bbe682c7SKent Overstreet bch_err_msg(ca, ret, "allocating journal"); 1767bbe682c7SKent Overstreet if (ret) 1768bbe682c7SKent Overstreet goto err; 1769bbe682c7SKent Overstreet } 1770bbe682c7SKent Overstreet 17711c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 1772bbe682c7SKent Overstreet bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx)->last_mount = 1773a420eea6STim Schlueter cpu_to_le64(ktime_get_real_seconds()); 17741c6fdbd8SKent Overstreet bch2_write_super(c); 17751c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17761c6fdbd8SKent Overstreet 17771ada1606SKent Overstreet up_write(&c->state_lock); 17781c6fdbd8SKent Overstreet return 0; 17791c6fdbd8SKent Overstreet err: 17801ada1606SKent Overstreet up_write(&c->state_lock); 17811c6fdbd8SKent Overstreet bch2_free_super(&sb); 178278c0b75cSKent Overstreet return ret; 17831c6fdbd8SKent Overstreet } 17841c6fdbd8SKent Overstreet 17851c6fdbd8SKent Overstreet int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) 17861c6fdbd8SKent Overstreet { 17871ada1606SKent Overstreet down_write(&c->state_lock); 17881c6fdbd8SKent Overstreet 17891c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca)) { 17901c6fdbd8SKent Overstreet bch_err(ca, "Already offline"); 17911ada1606SKent Overstreet up_write(&c->state_lock); 17921c6fdbd8SKent Overstreet return 0; 17931c6fdbd8SKent Overstreet } 17941c6fdbd8SKent Overstreet 17952436cb9fSKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 17961c6fdbd8SKent Overstreet bch_err(ca, "Cannot offline required disk"); 17971ada1606SKent Overstreet up_write(&c->state_lock); 179878c0b75cSKent Overstreet return -BCH_ERR_device_state_not_allowed; 17991c6fdbd8SKent Overstreet } 18001c6fdbd8SKent Overstreet 18011c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 18021c6fdbd8SKent Overstreet 18031ada1606SKent Overstreet up_write(&c->state_lock); 18041c6fdbd8SKent Overstreet return 0; 18051c6fdbd8SKent Overstreet } 18061c6fdbd8SKent Overstreet 18071c6fdbd8SKent Overstreet int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) 18081c6fdbd8SKent Overstreet { 18093f7b9713SHunter Shaffer struct bch_member *m; 181069d1f052SKent Overstreet u64 old_nbuckets; 18111c6fdbd8SKent Overstreet int ret = 0; 18121c6fdbd8SKent Overstreet 18131ada1606SKent Overstreet down_write(&c->state_lock); 181469d1f052SKent Overstreet old_nbuckets = ca->mi.nbuckets; 18151c6fdbd8SKent Overstreet 18161c6fdbd8SKent Overstreet if (nbuckets < ca->mi.nbuckets) { 18171c6fdbd8SKent Overstreet bch_err(ca, "Cannot shrink yet"); 18181c6fdbd8SKent Overstreet ret = -EINVAL; 18191c6fdbd8SKent Overstreet goto err; 18201c6fdbd8SKent Overstreet } 18211c6fdbd8SKent Overstreet 18221c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca) && 18231c6fdbd8SKent Overstreet get_capacity(ca->disk_sb.bdev->bd_disk) < 18241c6fdbd8SKent Overstreet ca->mi.bucket_size * nbuckets) { 18251c6fdbd8SKent Overstreet bch_err(ca, "New size larger than device"); 182678c0b75cSKent Overstreet ret = -BCH_ERR_device_size_too_small; 18271c6fdbd8SKent Overstreet goto err; 18281c6fdbd8SKent Overstreet } 18291c6fdbd8SKent Overstreet 18301c6fdbd8SKent Overstreet ret = bch2_dev_buckets_resize(c, ca, nbuckets); 18311c6fdbd8SKent Overstreet if (ret) { 1832e46c181aSKent Overstreet bch_err_msg(ca, ret, "resizing buckets"); 18331c6fdbd8SKent Overstreet goto err; 18341c6fdbd8SKent Overstreet } 18351c6fdbd8SKent Overstreet 1836224ec3e6SKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca); 18373e3e02e6SKent Overstreet if (ret) 1838224ec3e6SKent Overstreet goto err; 1839224ec3e6SKent Overstreet 18401c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 18413f7b9713SHunter Shaffer m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx); 18423f7b9713SHunter Shaffer m->nbuckets = cpu_to_le64(nbuckets); 18431c6fdbd8SKent Overstreet 18441c6fdbd8SKent Overstreet bch2_write_super(c); 18451c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 18461c6fdbd8SKent Overstreet 184769d1f052SKent Overstreet if (ca->mi.freespace_initialized) { 184869d1f052SKent Overstreet ret = bch2_dev_freespace_init(c, ca, old_nbuckets, nbuckets); 184969d1f052SKent Overstreet if (ret) 185069d1f052SKent Overstreet goto err; 185169d1f052SKent Overstreet 185269d1f052SKent Overstreet /* 185369d1f052SKent Overstreet * XXX: this is all wrong transactionally - we'll be able to do 185469d1f052SKent Overstreet * this correctly after the disk space accounting rewrite 185569d1f052SKent Overstreet */ 185669d1f052SKent Overstreet ca->usage_base->d[BCH_DATA_free].buckets += nbuckets - old_nbuckets; 185769d1f052SKent Overstreet } 185869d1f052SKent Overstreet 18591c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 18601c6fdbd8SKent Overstreet err: 18611ada1606SKent Overstreet up_write(&c->state_lock); 18621c6fdbd8SKent Overstreet return ret; 18631c6fdbd8SKent Overstreet } 18641c6fdbd8SKent Overstreet 18651c6fdbd8SKent Overstreet /* return with ref on ca->ref: */ 1866bf7e49a4SKent Overstreet struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name) 18671c6fdbd8SKent Overstreet { 18681c6fdbd8SKent Overstreet struct bch_dev *ca; 18691c6fdbd8SKent Overstreet unsigned i; 18701c6fdbd8SKent Overstreet 18713a402c8dSKent Overstreet rcu_read_lock(); 18723a402c8dSKent Overstreet for_each_member_device_rcu(ca, c, i, NULL) 1873bf7e49a4SKent Overstreet if (!strcmp(name, ca->name)) 18741c6fdbd8SKent Overstreet goto found; 1875e47a390aSKent Overstreet ca = ERR_PTR(-BCH_ERR_ENOENT_dev_not_found); 18761c6fdbd8SKent Overstreet found: 18773a402c8dSKent Overstreet rcu_read_unlock(); 18783a402c8dSKent Overstreet 18791c6fdbd8SKent Overstreet return ca; 18801c6fdbd8SKent Overstreet } 18811c6fdbd8SKent Overstreet 18821c6fdbd8SKent Overstreet /* Filesystem open: */ 18831c6fdbd8SKent Overstreet 18841c6fdbd8SKent Overstreet struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, 18851c6fdbd8SKent Overstreet struct bch_opts opts) 18861c6fdbd8SKent Overstreet { 18871c6fdbd8SKent Overstreet struct bch_sb_handle *sb = NULL; 18881c6fdbd8SKent Overstreet struct bch_fs *c = NULL; 18891c6fdbd8SKent Overstreet unsigned i, best_sb = 0; 1890fa8e94faSKent Overstreet struct printbuf errbuf = PRINTBUF; 1891e2b60560SKent Overstreet int ret = 0; 18921c6fdbd8SKent Overstreet 1893efe68e1dSKent Overstreet if (!try_module_get(THIS_MODULE)) 1894efe68e1dSKent Overstreet return ERR_PTR(-ENODEV); 1895efe68e1dSKent Overstreet 18961c6fdbd8SKent Overstreet if (!nr_devices) { 1897efe68e1dSKent Overstreet ret = -EINVAL; 1898efe68e1dSKent Overstreet goto err; 18991c6fdbd8SKent Overstreet } 19001c6fdbd8SKent Overstreet 19011c6fdbd8SKent Overstreet sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL); 1902e2b60560SKent Overstreet if (!sb) { 1903e2b60560SKent Overstreet ret = -ENOMEM; 19041c6fdbd8SKent Overstreet goto err; 1905e2b60560SKent Overstreet } 19061c6fdbd8SKent Overstreet 19071c6fdbd8SKent Overstreet for (i = 0; i < nr_devices; i++) { 19081c6fdbd8SKent Overstreet ret = bch2_read_super(devices[i], &opts, &sb[i]); 19091c6fdbd8SKent Overstreet if (ret) 19101c6fdbd8SKent Overstreet goto err; 19111c6fdbd8SKent Overstreet 19121c6fdbd8SKent Overstreet } 19131c6fdbd8SKent Overstreet 19141c6fdbd8SKent Overstreet for (i = 1; i < nr_devices; i++) 19151c6fdbd8SKent Overstreet if (le64_to_cpu(sb[i].sb->seq) > 19161c6fdbd8SKent Overstreet le64_to_cpu(sb[best_sb].sb->seq)) 19171c6fdbd8SKent Overstreet best_sb = i; 19181c6fdbd8SKent Overstreet 1919625104eaSKent Overstreet i = 0; 1920625104eaSKent Overstreet while (i < nr_devices) { 1921625104eaSKent Overstreet if (i != best_sb && 19221241df58SHunter Shaffer !bch2_dev_exists(sb[best_sb].sb, sb[i].sb->dev_idx)) { 1923625104eaSKent Overstreet pr_info("%pg has been removed, skipping", sb[i].bdev); 1924625104eaSKent Overstreet bch2_free_super(&sb[i]); 1925625104eaSKent Overstreet array_remove_item(sb, nr_devices, i); 1926625104eaSKent Overstreet continue; 1927625104eaSKent Overstreet } 1928625104eaSKent Overstreet 192978c0b75cSKent Overstreet ret = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb); 193078c0b75cSKent Overstreet if (ret) 19311c6fdbd8SKent Overstreet goto err_print; 1932625104eaSKent Overstreet i++; 19331c6fdbd8SKent Overstreet } 19341c6fdbd8SKent Overstreet 19351c6fdbd8SKent Overstreet c = bch2_fs_alloc(sb[best_sb].sb, opts); 19367be9ab63SChris Webb if (IS_ERR(c)) { 19377be9ab63SChris Webb ret = PTR_ERR(c); 19381c6fdbd8SKent Overstreet goto err; 19397be9ab63SChris Webb } 19401c6fdbd8SKent Overstreet 19411ada1606SKent Overstreet down_write(&c->state_lock); 1942e2b60560SKent Overstreet for (i = 0; i < nr_devices; i++) { 1943e2b60560SKent Overstreet ret = bch2_dev_attach_bdev(c, &sb[i]); 1944e2b60560SKent Overstreet if (ret) { 19451ada1606SKent Overstreet up_write(&c->state_lock); 1946e2b60560SKent Overstreet goto err; 1947e2b60560SKent Overstreet } 19481c6fdbd8SKent Overstreet } 19491ada1606SKent Overstreet up_write(&c->state_lock); 19501c6fdbd8SKent Overstreet 195178c0b75cSKent Overstreet if (!bch2_fs_may_start(c)) { 195278c0b75cSKent Overstreet ret = -BCH_ERR_insufficient_devices_to_start; 19531c6fdbd8SKent Overstreet goto err_print; 195478c0b75cSKent Overstreet } 19551c6fdbd8SKent Overstreet 19561c6fdbd8SKent Overstreet if (!c->opts.nostart) { 1957619f5beeSKent Overstreet ret = bch2_fs_start(c); 1958619f5beeSKent Overstreet if (ret) 1959619f5beeSKent Overstreet goto err; 19601c6fdbd8SKent Overstreet } 19611c6fdbd8SKent Overstreet out: 19621c6fdbd8SKent Overstreet kfree(sb); 1963fa8e94faSKent Overstreet printbuf_exit(&errbuf); 19641c6fdbd8SKent Overstreet module_put(THIS_MODULE); 19651c6fdbd8SKent Overstreet return c; 19661c6fdbd8SKent Overstreet err_print: 19671c6fdbd8SKent Overstreet pr_err("bch_fs_open err opening %s: %s", 196878c0b75cSKent Overstreet devices[0], bch2_err_str(ret)); 19691c6fdbd8SKent Overstreet err: 19707be9ab63SChris Webb if (!IS_ERR_OR_NULL(c)) 19711c6fdbd8SKent Overstreet bch2_fs_stop(c); 1972e2b60560SKent Overstreet if (sb) 19731c6fdbd8SKent Overstreet for (i = 0; i < nr_devices; i++) 19741c6fdbd8SKent Overstreet bch2_free_super(&sb[i]); 19751c6fdbd8SKent Overstreet c = ERR_PTR(ret); 19761c6fdbd8SKent Overstreet goto out; 19771c6fdbd8SKent Overstreet } 19781c6fdbd8SKent Overstreet 19791c6fdbd8SKent Overstreet /* Global interfaces/init */ 19801c6fdbd8SKent Overstreet 19811c6fdbd8SKent Overstreet static void bcachefs_exit(void) 19821c6fdbd8SKent Overstreet { 19831c6fdbd8SKent Overstreet bch2_debug_exit(); 19841c6fdbd8SKent Overstreet bch2_vfs_exit(); 19851c6fdbd8SKent Overstreet bch2_chardev_exit(); 198614ba3706SKent Overstreet bch2_btree_key_cache_exit(); 19871c6fdbd8SKent Overstreet if (bcachefs_kset) 19881c6fdbd8SKent Overstreet kset_unregister(bcachefs_kset); 19891c6fdbd8SKent Overstreet } 19901c6fdbd8SKent Overstreet 19911c6fdbd8SKent Overstreet static int __init bcachefs_init(void) 19921c6fdbd8SKent Overstreet { 19931c6fdbd8SKent Overstreet bch2_bkey_pack_test(); 19941c6fdbd8SKent Overstreet 19951c6fdbd8SKent Overstreet if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) || 199614ba3706SKent Overstreet bch2_btree_key_cache_init() || 19971c6fdbd8SKent Overstreet bch2_chardev_init() || 19981c6fdbd8SKent Overstreet bch2_vfs_init() || 19991c6fdbd8SKent Overstreet bch2_debug_init()) 20001c6fdbd8SKent Overstreet goto err; 20011c6fdbd8SKent Overstreet 20021c6fdbd8SKent Overstreet return 0; 20031c6fdbd8SKent Overstreet err: 20041c6fdbd8SKent Overstreet bcachefs_exit(); 20051c6fdbd8SKent Overstreet return -ENOMEM; 20061c6fdbd8SKent Overstreet } 20071c6fdbd8SKent Overstreet 20081c6fdbd8SKent Overstreet #define BCH_DEBUG_PARAM(name, description) \ 20091c6fdbd8SKent Overstreet bool bch2_##name; \ 20101c6fdbd8SKent Overstreet module_param_named(name, bch2_##name, bool, 0644); \ 20111c6fdbd8SKent Overstreet MODULE_PARM_DESC(name, description); 20121c6fdbd8SKent Overstreet BCH_DEBUG_PARAMS() 20131c6fdbd8SKent Overstreet #undef BCH_DEBUG_PARAM 20141c6fdbd8SKent Overstreet 2015e08e63e4SKent Overstreet __maybe_unused 201673bd774dSKent Overstreet static unsigned bch2_metadata_version = bcachefs_metadata_version_current; 20171c6fdbd8SKent Overstreet module_param_named(version, bch2_metadata_version, uint, 0400); 20181c6fdbd8SKent Overstreet 20191c6fdbd8SKent Overstreet module_exit(bcachefs_exit); 20201c6fdbd8SKent Overstreet module_init(bcachefs_init); 2021