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" 162ca88e5aSKent Overstreet #include "btree_key_cache.h" 171c6fdbd8SKent Overstreet #include "btree_update_interior.h" 181c6fdbd8SKent Overstreet #include "btree_io.h" 191c6fdbd8SKent Overstreet #include "chardev.h" 201c6fdbd8SKent Overstreet #include "checksum.h" 211c6fdbd8SKent Overstreet #include "clock.h" 221c6fdbd8SKent Overstreet #include "compress.h" 231c6fdbd8SKent Overstreet #include "debug.h" 241c6fdbd8SKent Overstreet #include "disk_groups.h" 25cd575ddfSKent Overstreet #include "ec.h" 261c6fdbd8SKent Overstreet #include "error.h" 271c6fdbd8SKent Overstreet #include "fs.h" 281c6fdbd8SKent Overstreet #include "fs-io.h" 291c6fdbd8SKent Overstreet #include "fsck.h" 301c6fdbd8SKent Overstreet #include "inode.h" 311c6fdbd8SKent Overstreet #include "io.h" 321c6fdbd8SKent Overstreet #include "journal.h" 331c6fdbd8SKent Overstreet #include "journal_reclaim.h" 341dd7f9d9SKent Overstreet #include "journal_seq_blacklist.h" 351c6fdbd8SKent Overstreet #include "move.h" 361c6fdbd8SKent Overstreet #include "migrate.h" 371c6fdbd8SKent Overstreet #include "movinggc.h" 381c6fdbd8SKent Overstreet #include "quota.h" 391c6fdbd8SKent Overstreet #include "rebalance.h" 401c6fdbd8SKent Overstreet #include "recovery.h" 411c6fdbd8SKent Overstreet #include "replicas.h" 421c6fdbd8SKent Overstreet #include "super.h" 431c6fdbd8SKent Overstreet #include "super-io.h" 441c6fdbd8SKent Overstreet #include "sysfs.h" 451c6fdbd8SKent Overstreet #include "trace.h" 461c6fdbd8SKent Overstreet 471c6fdbd8SKent Overstreet #include <linux/backing-dev.h> 481c6fdbd8SKent Overstreet #include <linux/blkdev.h> 491c6fdbd8SKent Overstreet #include <linux/debugfs.h> 501c6fdbd8SKent Overstreet #include <linux/device.h> 511c6fdbd8SKent Overstreet #include <linux/idr.h> 521c6fdbd8SKent Overstreet #include <linux/kthread.h> 531c6fdbd8SKent Overstreet #include <linux/module.h> 541c6fdbd8SKent Overstreet #include <linux/percpu.h> 551c6fdbd8SKent Overstreet #include <linux/random.h> 561c6fdbd8SKent Overstreet #include <linux/sysfs.h> 571c6fdbd8SKent Overstreet #include <crypto/hash.h> 581c6fdbd8SKent Overstreet 591c6fdbd8SKent Overstreet MODULE_LICENSE("GPL"); 601c6fdbd8SKent Overstreet MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>"); 611c6fdbd8SKent Overstreet 621c6fdbd8SKent Overstreet #define KTYPE(type) \ 631c6fdbd8SKent Overstreet static const struct attribute_group type ## _group = { \ 641c6fdbd8SKent Overstreet .attrs = type ## _files \ 651c6fdbd8SKent Overstreet }; \ 661c6fdbd8SKent Overstreet \ 671c6fdbd8SKent Overstreet static const struct attribute_group *type ## _groups[] = { \ 681c6fdbd8SKent Overstreet &type ## _group, \ 691c6fdbd8SKent Overstreet NULL \ 701c6fdbd8SKent Overstreet }; \ 711c6fdbd8SKent Overstreet \ 721c6fdbd8SKent Overstreet static const struct kobj_type type ## _ktype = { \ 731c6fdbd8SKent Overstreet .release = type ## _release, \ 741c6fdbd8SKent Overstreet .sysfs_ops = &type ## _sysfs_ops, \ 751c6fdbd8SKent Overstreet .default_groups = type ## _groups \ 761c6fdbd8SKent Overstreet } 771c6fdbd8SKent Overstreet 781c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *); 791c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *); 801c6fdbd8SKent Overstreet 811c6fdbd8SKent Overstreet static void bch2_fs_internal_release(struct kobject *k) 821c6fdbd8SKent Overstreet { 831c6fdbd8SKent Overstreet } 841c6fdbd8SKent Overstreet 851c6fdbd8SKent Overstreet static void bch2_fs_opts_dir_release(struct kobject *k) 861c6fdbd8SKent Overstreet { 871c6fdbd8SKent Overstreet } 881c6fdbd8SKent Overstreet 891c6fdbd8SKent Overstreet static void bch2_fs_time_stats_release(struct kobject *k) 901c6fdbd8SKent Overstreet { 911c6fdbd8SKent Overstreet } 921c6fdbd8SKent Overstreet 931c6fdbd8SKent Overstreet KTYPE(bch2_fs); 941c6fdbd8SKent Overstreet KTYPE(bch2_fs_internal); 951c6fdbd8SKent Overstreet KTYPE(bch2_fs_opts_dir); 961c6fdbd8SKent Overstreet KTYPE(bch2_fs_time_stats); 971c6fdbd8SKent Overstreet KTYPE(bch2_dev); 981c6fdbd8SKent Overstreet 991c6fdbd8SKent Overstreet static struct kset *bcachefs_kset; 1001c6fdbd8SKent Overstreet static LIST_HEAD(bch_fs_list); 1011c6fdbd8SKent Overstreet static DEFINE_MUTEX(bch_fs_list_lock); 1021c6fdbd8SKent Overstreet 1031c6fdbd8SKent Overstreet static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait); 1041c6fdbd8SKent Overstreet 1051c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *); 1061c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *, unsigned); 1071c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *); 1081c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *); 1091c6fdbd8SKent Overstreet 1101c6fdbd8SKent Overstreet struct bch_fs *bch2_dev_to_fs(dev_t dev) 1111c6fdbd8SKent Overstreet { 1121c6fdbd8SKent Overstreet struct bch_fs *c; 1131c6fdbd8SKent Overstreet struct bch_dev *ca; 1141c6fdbd8SKent Overstreet unsigned i; 1151c6fdbd8SKent Overstreet 1161c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 1171c6fdbd8SKent Overstreet rcu_read_lock(); 1181c6fdbd8SKent Overstreet 1191c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 1201c6fdbd8SKent Overstreet for_each_member_device_rcu(ca, c, i, NULL) 1211c6fdbd8SKent Overstreet if (ca->disk_sb.bdev->bd_dev == dev) { 1221c6fdbd8SKent Overstreet closure_get(&c->cl); 1231c6fdbd8SKent Overstreet goto found; 1241c6fdbd8SKent Overstreet } 1251c6fdbd8SKent Overstreet c = NULL; 1261c6fdbd8SKent Overstreet found: 1271c6fdbd8SKent Overstreet rcu_read_unlock(); 1281c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 1291c6fdbd8SKent Overstreet 1301c6fdbd8SKent Overstreet return c; 1311c6fdbd8SKent Overstreet } 1321c6fdbd8SKent Overstreet 1331c6fdbd8SKent Overstreet static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid) 1341c6fdbd8SKent Overstreet { 1351c6fdbd8SKent Overstreet struct bch_fs *c; 1361c6fdbd8SKent Overstreet 1371c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 1381c6fdbd8SKent Overstreet 1391c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 1401c6fdbd8SKent Overstreet if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid))) 1411c6fdbd8SKent Overstreet return c; 1421c6fdbd8SKent Overstreet 1431c6fdbd8SKent Overstreet return NULL; 1441c6fdbd8SKent Overstreet } 1451c6fdbd8SKent Overstreet 1461c6fdbd8SKent Overstreet struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid) 1471c6fdbd8SKent Overstreet { 1481c6fdbd8SKent Overstreet struct bch_fs *c; 1491c6fdbd8SKent Overstreet 1501c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 1511c6fdbd8SKent Overstreet c = __bch2_uuid_to_fs(uuid); 1521c6fdbd8SKent Overstreet if (c) 1531c6fdbd8SKent Overstreet closure_get(&c->cl); 1541c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 1551c6fdbd8SKent Overstreet 1561c6fdbd8SKent Overstreet return c; 1571c6fdbd8SKent Overstreet } 1581c6fdbd8SKent Overstreet 1591c6fdbd8SKent Overstreet /* Filesystem RO/RW: */ 1601c6fdbd8SKent Overstreet 1611c6fdbd8SKent Overstreet /* 1621c6fdbd8SKent Overstreet * For startup/shutdown of RW stuff, the dependencies are: 1631c6fdbd8SKent Overstreet * 1641c6fdbd8SKent Overstreet * - foreground writes depend on copygc and rebalance (to free up space) 1651c6fdbd8SKent Overstreet * 1661c6fdbd8SKent Overstreet * - copygc and rebalance depend on mark and sweep gc (they actually probably 1671c6fdbd8SKent Overstreet * don't because they either reserve ahead of time or don't block if 1681c6fdbd8SKent Overstreet * allocations fail, but allocations can require mark and sweep gc to run 1691c6fdbd8SKent Overstreet * because of generation number wraparound) 1701c6fdbd8SKent Overstreet * 1711c6fdbd8SKent Overstreet * - all of the above depends on the allocator threads 1721c6fdbd8SKent Overstreet * 1731c6fdbd8SKent Overstreet * - allocator depends on the journal (when it rewrites prios and gens) 1741c6fdbd8SKent Overstreet */ 1751c6fdbd8SKent Overstreet 1761c6fdbd8SKent Overstreet static void __bch2_fs_read_only(struct bch_fs *c) 1771c6fdbd8SKent Overstreet { 1781c6fdbd8SKent Overstreet struct bch_dev *ca; 179039fc4c5SKent Overstreet bool wrote = false; 180d5f70c1fSKent Overstreet unsigned i, clean_passes = 0; 181430735cdSKent Overstreet int ret; 1821c6fdbd8SKent Overstreet 1831c6fdbd8SKent Overstreet bch2_rebalance_stop(c); 184e6d11615SKent Overstreet bch2_copygc_stop(c); 1851c6fdbd8SKent Overstreet bch2_gc_thread_stop(c); 1861c6fdbd8SKent Overstreet 1871c6fdbd8SKent Overstreet /* 1881c6fdbd8SKent Overstreet * Flush journal before stopping allocators, because flushing journal 1891c6fdbd8SKent Overstreet * blacklist entries involves allocating new btree nodes: 1901c6fdbd8SKent Overstreet */ 1911c6fdbd8SKent Overstreet bch2_journal_flush_all_pins(&c->journal); 1921c6fdbd8SKent Overstreet 1932340fd9dSKent Overstreet /* 1942340fd9dSKent Overstreet * If the allocator threads didn't all start up, the btree updates to 1952340fd9dSKent Overstreet * write out alloc info aren't going to work: 1962340fd9dSKent Overstreet */ 197b935a8a6SKent Overstreet if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags)) 1982340fd9dSKent Overstreet goto nowrote_alloc; 199b935a8a6SKent Overstreet 200b2930396SKent Overstreet bch_verbose(c, "writing alloc info"); 201039fc4c5SKent Overstreet /* 202039fc4c5SKent Overstreet * This should normally just be writing the bucket read/write clocks: 203039fc4c5SKent Overstreet */ 204460651eeSKent Overstreet ret = bch2_stripes_write(c, BTREE_INSERT_NOCHECK_RW, &wrote) ?: 205460651eeSKent Overstreet bch2_alloc_write(c, BTREE_INSERT_NOCHECK_RW, &wrote); 206039fc4c5SKent Overstreet bch_verbose(c, "writing alloc info complete"); 20761c8d7c8SKent Overstreet 208460651eeSKent Overstreet if (ret && !test_bit(BCH_FS_EMERGENCY_RO, &c->flags)) 209d5f70c1fSKent Overstreet bch2_fs_inconsistent(c, "error writing out alloc info %i", ret); 210460651eeSKent Overstreet 211460651eeSKent Overstreet if (ret) 2122340fd9dSKent Overstreet goto nowrote_alloc; 213d5f70c1fSKent Overstreet 214039fc4c5SKent Overstreet bch_verbose(c, "flushing journal and stopping allocators"); 2151c6fdbd8SKent Overstreet 2161c6fdbd8SKent Overstreet bch2_journal_flush_all_pins(&c->journal); 217039fc4c5SKent Overstreet set_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags); 218039fc4c5SKent Overstreet 219039fc4c5SKent Overstreet do { 220039fc4c5SKent Overstreet clean_passes++; 221039fc4c5SKent Overstreet 222039fc4c5SKent Overstreet if (bch2_journal_flush_all_pins(&c->journal)) 223039fc4c5SKent Overstreet clean_passes = 0; 2241c6fdbd8SKent Overstreet 2251c6fdbd8SKent Overstreet /* 226039fc4c5SKent Overstreet * In flight interior btree updates will generate more journal 227039fc4c5SKent Overstreet * updates and btree updates (alloc btree): 2281c6fdbd8SKent Overstreet */ 229039fc4c5SKent Overstreet if (bch2_btree_interior_updates_nr_pending(c)) { 2301c6fdbd8SKent Overstreet closure_wait_event(&c->btree_interior_update_wait, 2311c6fdbd8SKent Overstreet !bch2_btree_interior_updates_nr_pending(c)); 232039fc4c5SKent Overstreet clean_passes = 0; 233039fc4c5SKent Overstreet } 23400b8ccf7SKent Overstreet flush_work(&c->btree_interior_update_work); 235d5f70c1fSKent Overstreet 236039fc4c5SKent Overstreet if (bch2_journal_flush_all_pins(&c->journal)) 237039fc4c5SKent Overstreet clean_passes = 0; 238d5f70c1fSKent Overstreet } while (clean_passes < 2); 239039fc4c5SKent Overstreet bch_verbose(c, "flushing journal and stopping allocators complete"); 2402340fd9dSKent Overstreet 2412340fd9dSKent Overstreet set_bit(BCH_FS_ALLOC_CLEAN, &c->flags); 2422340fd9dSKent Overstreet nowrote_alloc: 24300b8ccf7SKent Overstreet closure_wait_event(&c->btree_interior_update_wait, 24400b8ccf7SKent Overstreet !bch2_btree_interior_updates_nr_pending(c)); 24500b8ccf7SKent Overstreet flush_work(&c->btree_interior_update_work); 24600b8ccf7SKent Overstreet 247430735cdSKent Overstreet for_each_member_device(ca, c, i) 248430735cdSKent Overstreet bch2_dev_allocator_stop(ca); 2491c6fdbd8SKent Overstreet 250b935a8a6SKent Overstreet clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags); 251039fc4c5SKent Overstreet clear_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags); 252b935a8a6SKent Overstreet 2531c6fdbd8SKent Overstreet bch2_fs_journal_stop(&c->journal); 2541c6fdbd8SKent Overstreet 2551c6fdbd8SKent Overstreet /* 2561c6fdbd8SKent Overstreet * the journal kicks off btree writes via reclaim - wait for in flight 2571c6fdbd8SKent Overstreet * writes after stopping journal: 2581c6fdbd8SKent Overstreet */ 2591c6fdbd8SKent Overstreet if (test_bit(BCH_FS_EMERGENCY_RO, &c->flags)) 2601c6fdbd8SKent Overstreet bch2_btree_flush_all_writes(c); 2611c6fdbd8SKent Overstreet else 2621c6fdbd8SKent Overstreet bch2_btree_verify_flushed(c); 2631c6fdbd8SKent Overstreet 2641c6fdbd8SKent Overstreet /* 2651c6fdbd8SKent Overstreet * After stopping journal: 2661c6fdbd8SKent Overstreet */ 2671c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 2681c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 2691c6fdbd8SKent Overstreet } 2701c6fdbd8SKent Overstreet 2711c6fdbd8SKent Overstreet static void bch2_writes_disabled(struct percpu_ref *writes) 2721c6fdbd8SKent Overstreet { 2731c6fdbd8SKent Overstreet struct bch_fs *c = container_of(writes, struct bch_fs, writes); 2741c6fdbd8SKent Overstreet 2751c6fdbd8SKent Overstreet set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags); 2761c6fdbd8SKent Overstreet wake_up(&bch_read_only_wait); 2771c6fdbd8SKent Overstreet } 2781c6fdbd8SKent Overstreet 2791c6fdbd8SKent Overstreet void bch2_fs_read_only(struct bch_fs *c) 2801c6fdbd8SKent Overstreet { 281134915f3SKent Overstreet if (!test_bit(BCH_FS_RW, &c->flags)) { 282134915f3SKent Overstreet cancel_delayed_work_sync(&c->journal.reclaim_work); 2831c6fdbd8SKent Overstreet return; 284134915f3SKent Overstreet } 2851c6fdbd8SKent Overstreet 2861c6fdbd8SKent Overstreet BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags)); 2871c6fdbd8SKent Overstreet 2881c6fdbd8SKent Overstreet /* 2891c6fdbd8SKent Overstreet * Block new foreground-end write operations from starting - any new 2901c6fdbd8SKent Overstreet * writes will return -EROFS: 2911c6fdbd8SKent Overstreet * 2921c6fdbd8SKent Overstreet * (This is really blocking new _allocations_, writes to previously 2931c6fdbd8SKent Overstreet * allocated space can still happen until stopping the allocator in 2941c6fdbd8SKent Overstreet * bch2_dev_allocator_stop()). 2951c6fdbd8SKent Overstreet */ 2961c6fdbd8SKent Overstreet percpu_ref_kill(&c->writes); 2971c6fdbd8SKent Overstreet 298f516c872SKent Overstreet cancel_work_sync(&c->ec_stripe_delete_work); 2991c6fdbd8SKent Overstreet cancel_delayed_work(&c->pd_controllers_update); 3001c6fdbd8SKent Overstreet 3011c6fdbd8SKent Overstreet /* 3021c6fdbd8SKent Overstreet * If we're not doing an emergency shutdown, we want to wait on 3031c6fdbd8SKent Overstreet * outstanding writes to complete so they don't see spurious errors due 3041c6fdbd8SKent Overstreet * to shutting down the allocator: 3051c6fdbd8SKent Overstreet * 3061c6fdbd8SKent Overstreet * If we are doing an emergency shutdown outstanding writes may 3071c6fdbd8SKent Overstreet * hang until we shutdown the allocator so we don't want to wait 3081c6fdbd8SKent Overstreet * on outstanding writes before shutting everything down - but 3091c6fdbd8SKent Overstreet * we do need to wait on them before returning and signalling 3101c6fdbd8SKent Overstreet * that going RO is complete: 3111c6fdbd8SKent Overstreet */ 3121c6fdbd8SKent Overstreet wait_event(bch_read_only_wait, 3131c6fdbd8SKent Overstreet test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) || 3141c6fdbd8SKent Overstreet test_bit(BCH_FS_EMERGENCY_RO, &c->flags)); 3151c6fdbd8SKent Overstreet 3161c6fdbd8SKent Overstreet __bch2_fs_read_only(c); 3171c6fdbd8SKent Overstreet 3181c6fdbd8SKent Overstreet wait_event(bch_read_only_wait, 3191c6fdbd8SKent Overstreet test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags)); 3201c6fdbd8SKent Overstreet 3211c6fdbd8SKent Overstreet clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags); 3221c6fdbd8SKent Overstreet 3231c6fdbd8SKent Overstreet if (!bch2_journal_error(&c->journal) && 3241c6fdbd8SKent Overstreet !test_bit(BCH_FS_ERROR, &c->flags) && 3253aea4342SKent Overstreet !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) && 326a0e0bda1SKent Overstreet test_bit(BCH_FS_STARTED, &c->flags) && 3272340fd9dSKent Overstreet test_bit(BCH_FS_ALLOC_CLEAN, &c->flags) && 328b2930396SKent Overstreet !c->opts.norecovery) { 329b2930396SKent Overstreet bch_verbose(c, "marking filesystem clean"); 330134915f3SKent Overstreet bch2_fs_mark_clean(c); 331b2930396SKent Overstreet } 3321c6fdbd8SKent Overstreet 333134915f3SKent Overstreet clear_bit(BCH_FS_RW, &c->flags); 3341c6fdbd8SKent Overstreet } 3351c6fdbd8SKent Overstreet 3361c6fdbd8SKent Overstreet static void bch2_fs_read_only_work(struct work_struct *work) 3371c6fdbd8SKent Overstreet { 3381c6fdbd8SKent Overstreet struct bch_fs *c = 3391c6fdbd8SKent Overstreet container_of(work, struct bch_fs, read_only_work); 3401c6fdbd8SKent Overstreet 3411ada1606SKent Overstreet down_write(&c->state_lock); 3421c6fdbd8SKent Overstreet bch2_fs_read_only(c); 3431ada1606SKent Overstreet up_write(&c->state_lock); 3441c6fdbd8SKent Overstreet } 3451c6fdbd8SKent Overstreet 3461c6fdbd8SKent Overstreet static void bch2_fs_read_only_async(struct bch_fs *c) 3471c6fdbd8SKent Overstreet { 3481c6fdbd8SKent Overstreet queue_work(system_long_wq, &c->read_only_work); 3491c6fdbd8SKent Overstreet } 3501c6fdbd8SKent Overstreet 3511c6fdbd8SKent Overstreet bool bch2_fs_emergency_read_only(struct bch_fs *c) 3521c6fdbd8SKent Overstreet { 3531c6fdbd8SKent Overstreet bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags); 3541c6fdbd8SKent Overstreet 3551c6fdbd8SKent Overstreet bch2_journal_halt(&c->journal); 3569f115ce9SKent Overstreet bch2_fs_read_only_async(c); 3571c6fdbd8SKent Overstreet 3581c6fdbd8SKent Overstreet wake_up(&bch_read_only_wait); 3591c6fdbd8SKent Overstreet return ret; 3601c6fdbd8SKent Overstreet } 3611c6fdbd8SKent Overstreet 362134915f3SKent Overstreet static int bch2_fs_read_write_late(struct bch_fs *c) 3631c6fdbd8SKent Overstreet { 364134915f3SKent Overstreet int ret; 3651c6fdbd8SKent Overstreet 366134915f3SKent Overstreet ret = bch2_gc_thread_start(c); 367134915f3SKent Overstreet if (ret) { 368134915f3SKent Overstreet bch_err(c, "error starting gc thread"); 369134915f3SKent Overstreet return ret; 370134915f3SKent Overstreet } 3711c6fdbd8SKent Overstreet 372e6d11615SKent Overstreet ret = bch2_copygc_start(c); 373134915f3SKent Overstreet if (ret) { 374e6d11615SKent Overstreet bch_err(c, "error starting copygc thread"); 375134915f3SKent Overstreet return ret; 376134915f3SKent Overstreet } 377134915f3SKent Overstreet 378134915f3SKent Overstreet ret = bch2_rebalance_start(c); 379134915f3SKent Overstreet if (ret) { 380134915f3SKent Overstreet bch_err(c, "error starting rebalance thread"); 381134915f3SKent Overstreet return ret; 382134915f3SKent Overstreet } 383134915f3SKent Overstreet 384134915f3SKent Overstreet schedule_delayed_work(&c->pd_controllers_update, 5 * HZ); 385134915f3SKent Overstreet 38697fd13adSKent Overstreet schedule_work(&c->ec_stripe_delete_work); 38797fd13adSKent Overstreet 388134915f3SKent Overstreet return 0; 389134915f3SKent Overstreet } 390134915f3SKent Overstreet 391e731d466SKent Overstreet static int __bch2_fs_read_write(struct bch_fs *c, bool early) 392134915f3SKent Overstreet { 393134915f3SKent Overstreet struct bch_dev *ca; 394134915f3SKent Overstreet unsigned i; 395134915f3SKent Overstreet int ret; 396134915f3SKent Overstreet 397134915f3SKent Overstreet if (test_bit(BCH_FS_RW, &c->flags)) 398134915f3SKent Overstreet return 0; 399134915f3SKent Overstreet 400619f5beeSKent Overstreet /* 401619f5beeSKent Overstreet * nochanges is used for fsck -n mode - we have to allow going rw 402619f5beeSKent Overstreet * during recovery for that to work: 403619f5beeSKent Overstreet */ 404619f5beeSKent Overstreet if (c->opts.norecovery || 405619f5beeSKent Overstreet (c->opts.nochanges && 406619f5beeSKent Overstreet (!early || c->opts.read_only))) 407619f5beeSKent Overstreet return -EROFS; 408330581f1SKent Overstreet 409134915f3SKent Overstreet ret = bch2_fs_mark_dirty(c); 410134915f3SKent Overstreet if (ret) 411134915f3SKent Overstreet goto err; 4121c6fdbd8SKent Overstreet 4139f115ce9SKent Overstreet /* 4149f115ce9SKent Overstreet * We need to write out a journal entry before we start doing btree 4159f115ce9SKent Overstreet * updates, to ensure that on unclean shutdown new journal blacklist 4169f115ce9SKent Overstreet * entries are created: 4179f115ce9SKent Overstreet */ 4189f115ce9SKent Overstreet bch2_journal_meta(&c->journal); 4199f115ce9SKent Overstreet 4202340fd9dSKent Overstreet clear_bit(BCH_FS_ALLOC_CLEAN, &c->flags); 4212340fd9dSKent Overstreet 4221c6fdbd8SKent Overstreet for_each_rw_member(ca, c, i) 4231c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 4241c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 4251c6fdbd8SKent Overstreet 426134915f3SKent Overstreet for_each_rw_member(ca, c, i) { 427134915f3SKent Overstreet ret = bch2_dev_allocator_start(ca); 428134915f3SKent Overstreet if (ret) { 429134915f3SKent Overstreet bch_err(c, "error starting allocator threads"); 4301c6fdbd8SKent Overstreet percpu_ref_put(&ca->io_ref); 4311c6fdbd8SKent Overstreet goto err; 4321c6fdbd8SKent Overstreet } 433134915f3SKent Overstreet } 4341c6fdbd8SKent Overstreet 435b935a8a6SKent Overstreet set_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags); 436b935a8a6SKent Overstreet 437134915f3SKent Overstreet if (!early) { 438134915f3SKent Overstreet ret = bch2_fs_read_write_late(c); 439134915f3SKent Overstreet if (ret) 4401c6fdbd8SKent Overstreet goto err; 4411c6fdbd8SKent Overstreet } 4421c6fdbd8SKent Overstreet 4431c6fdbd8SKent Overstreet percpu_ref_reinit(&c->writes); 444134915f3SKent Overstreet set_bit(BCH_FS_RW, &c->flags); 4451c6fdbd8SKent Overstreet 446134915f3SKent Overstreet queue_delayed_work(c->journal_reclaim_wq, 447134915f3SKent Overstreet &c->journal.reclaim_work, 0); 448134915f3SKent Overstreet return 0; 4491c6fdbd8SKent Overstreet err: 4501c6fdbd8SKent Overstreet __bch2_fs_read_only(c); 451134915f3SKent Overstreet return ret; 452134915f3SKent Overstreet } 453134915f3SKent Overstreet 454134915f3SKent Overstreet int bch2_fs_read_write(struct bch_fs *c) 455134915f3SKent Overstreet { 456134915f3SKent Overstreet return __bch2_fs_read_write(c, false); 457134915f3SKent Overstreet } 458134915f3SKent Overstreet 459134915f3SKent Overstreet int bch2_fs_read_write_early(struct bch_fs *c) 460134915f3SKent Overstreet { 461134915f3SKent Overstreet lockdep_assert_held(&c->state_lock); 462134915f3SKent Overstreet 463134915f3SKent Overstreet return __bch2_fs_read_write(c, true); 4641c6fdbd8SKent Overstreet } 4651c6fdbd8SKent Overstreet 4661c6fdbd8SKent Overstreet /* Filesystem startup/shutdown: */ 4671c6fdbd8SKent Overstreet 468d5e4dcc2SKent Overstreet static void __bch2_fs_free(struct bch_fs *c) 4691c6fdbd8SKent Overstreet { 4701c6fdbd8SKent Overstreet unsigned i; 4711c6fdbd8SKent Overstreet 4721c6fdbd8SKent Overstreet for (i = 0; i < BCH_TIME_STAT_NR; i++) 4731c6fdbd8SKent Overstreet bch2_time_stats_exit(&c->times[i]); 4741c6fdbd8SKent Overstreet 4751c6fdbd8SKent Overstreet bch2_fs_quota_exit(c); 4761c6fdbd8SKent Overstreet bch2_fs_fsio_exit(c); 477cd575ddfSKent Overstreet bch2_fs_ec_exit(c); 4781c6fdbd8SKent Overstreet bch2_fs_encryption_exit(c); 4791c6fdbd8SKent Overstreet bch2_fs_io_exit(c); 480c823c339SKent Overstreet bch2_fs_btree_interior_update_exit(c); 48136e9d698SKent Overstreet bch2_fs_btree_iter_exit(c); 4822ca88e5aSKent Overstreet bch2_fs_btree_key_cache_exit(&c->btree_key_cache); 4831c6fdbd8SKent Overstreet bch2_fs_btree_cache_exit(c); 4841c6fdbd8SKent Overstreet bch2_fs_journal_exit(&c->journal); 4851c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[WRITE]); 4861c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[READ]); 4871c6fdbd8SKent Overstreet bch2_fs_compress_exit(c); 488f1d786a0SKent Overstreet bch2_journal_keys_free(&c->journal_keys); 489f1d786a0SKent Overstreet bch2_journal_entries_free(&c->journal_entries); 4909166b41dSKent Overstreet percpu_free_rwsem(&c->mark_lock); 4915e82a9a1SKent Overstreet free_percpu(c->online_reserved); 4924d8100daSKent Overstreet kfree(c->usage_scratch); 4935e82a9a1SKent Overstreet free_percpu(c->usage[1]); 4949ca53b55SKent Overstreet free_percpu(c->usage[0]); 4955e82a9a1SKent Overstreet kfree(c->usage_base); 4965663a415SKent Overstreet free_percpu(c->pcpu); 49735189e09SKent Overstreet mempool_exit(&c->large_bkey_pool); 4981c6fdbd8SKent Overstreet mempool_exit(&c->btree_bounce_pool); 4991c6fdbd8SKent Overstreet bioset_exit(&c->btree_bio); 5001c6fdbd8SKent Overstreet mempool_exit(&c->fill_iter); 5011c6fdbd8SKent Overstreet percpu_ref_exit(&c->writes); 50273e6ab95SKent Overstreet kfree(c->replicas.entries); 50373e6ab95SKent Overstreet kfree(c->replicas_gc.entries); 5041c6fdbd8SKent Overstreet kfree(rcu_dereference_protected(c->disk_groups, 1)); 5051dd7f9d9SKent Overstreet kfree(c->journal_seq_blacklist_table); 506e6d11615SKent Overstreet free_heap(&c->copygc_heap); 5071c6fdbd8SKent Overstreet 5080519b72dSKent Overstreet if (c->journal_reclaim_wq) 5090519b72dSKent Overstreet destroy_workqueue(c->journal_reclaim_wq); 5101c6fdbd8SKent Overstreet if (c->copygc_wq) 5111c6fdbd8SKent Overstreet destroy_workqueue(c->copygc_wq); 5121c6fdbd8SKent Overstreet if (c->wq) 5131c6fdbd8SKent Overstreet destroy_workqueue(c->wq); 5141c6fdbd8SKent Overstreet 5151c6fdbd8SKent Overstreet free_pages((unsigned long) c->disk_sb.sb, 5161c6fdbd8SKent Overstreet c->disk_sb.page_order); 5171c6fdbd8SKent Overstreet kvpfree(c, sizeof(*c)); 5181c6fdbd8SKent Overstreet module_put(THIS_MODULE); 5191c6fdbd8SKent Overstreet } 5201c6fdbd8SKent Overstreet 5211c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *kobj) 5221c6fdbd8SKent Overstreet { 5231c6fdbd8SKent Overstreet struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); 5241c6fdbd8SKent Overstreet 525d5e4dcc2SKent Overstreet __bch2_fs_free(c); 5261c6fdbd8SKent Overstreet } 5271c6fdbd8SKent Overstreet 528d5e4dcc2SKent Overstreet void __bch2_fs_stop(struct bch_fs *c) 5291c6fdbd8SKent Overstreet { 5301c6fdbd8SKent Overstreet struct bch_dev *ca; 5311c6fdbd8SKent Overstreet unsigned i; 5321c6fdbd8SKent Overstreet 533af1c6871SKent Overstreet bch_verbose(c, "shutting down"); 534af1c6871SKent Overstreet 5351dd7f9d9SKent Overstreet set_bit(BCH_FS_STOPPING, &c->flags); 5361dd7f9d9SKent Overstreet 5371dd7f9d9SKent Overstreet cancel_work_sync(&c->journal_seq_blacklist_gc_work); 5381dd7f9d9SKent Overstreet 5391ada1606SKent Overstreet down_write(&c->state_lock); 540883f1a7cSKent Overstreet bch2_fs_read_only(c); 5411ada1606SKent Overstreet up_write(&c->state_lock); 542883f1a7cSKent Overstreet 5431c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 5441c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs && 5451c6fdbd8SKent Overstreet ca->disk_sb.bdev) 5461c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 5471c6fdbd8SKent Overstreet 5481c6fdbd8SKent Overstreet if (c->kobj.state_in_sysfs) 5491c6fdbd8SKent Overstreet kobject_del(&c->kobj); 5501c6fdbd8SKent Overstreet 5511c6fdbd8SKent Overstreet bch2_fs_debug_exit(c); 5521c6fdbd8SKent Overstreet bch2_fs_chardev_exit(c); 5531c6fdbd8SKent Overstreet 5541c6fdbd8SKent Overstreet kobject_put(&c->time_stats); 5551c6fdbd8SKent Overstreet kobject_put(&c->opts_dir); 5561c6fdbd8SKent Overstreet kobject_put(&c->internal); 5571c6fdbd8SKent Overstreet 5581c6fdbd8SKent Overstreet /* btree prefetch might have kicked off reads in the background: */ 5591c6fdbd8SKent Overstreet bch2_btree_flush_all_reads(c); 5601c6fdbd8SKent Overstreet 5611c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 5621c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 5631c6fdbd8SKent Overstreet 5641c6fdbd8SKent Overstreet cancel_work_sync(&c->btree_write_error_work); 5651c6fdbd8SKent Overstreet cancel_delayed_work_sync(&c->pd_controllers_update); 5661c6fdbd8SKent Overstreet cancel_work_sync(&c->read_only_work); 567d5e4dcc2SKent Overstreet } 5681c6fdbd8SKent Overstreet 569d5e4dcc2SKent Overstreet void bch2_fs_free(struct bch_fs *c) 570d5e4dcc2SKent Overstreet { 571d5e4dcc2SKent Overstreet unsigned i; 572d5e4dcc2SKent Overstreet 573d5e4dcc2SKent Overstreet mutex_lock(&bch_fs_list_lock); 574d5e4dcc2SKent Overstreet list_del(&c->list); 575d5e4dcc2SKent Overstreet mutex_unlock(&bch_fs_list_lock); 576d5e4dcc2SKent Overstreet 577d5e4dcc2SKent Overstreet closure_sync(&c->cl); 578d5e4dcc2SKent Overstreet closure_debug_destroy(&c->cl); 579d5e4dcc2SKent Overstreet 580d5e4dcc2SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) { 581d5e4dcc2SKent Overstreet struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true); 582d5e4dcc2SKent Overstreet 583d5e4dcc2SKent Overstreet if (ca) { 584d5e4dcc2SKent Overstreet bch2_free_super(&ca->disk_sb); 585d5e4dcc2SKent Overstreet bch2_dev_free(ca); 586d5e4dcc2SKent Overstreet } 587d5e4dcc2SKent Overstreet } 5881c6fdbd8SKent Overstreet 589af1c6871SKent Overstreet bch_verbose(c, "shutdown complete"); 590af1c6871SKent Overstreet 5911c6fdbd8SKent Overstreet kobject_put(&c->kobj); 5921c6fdbd8SKent Overstreet } 5931c6fdbd8SKent Overstreet 594d5e4dcc2SKent Overstreet void bch2_fs_stop(struct bch_fs *c) 595d5e4dcc2SKent Overstreet { 596d5e4dcc2SKent Overstreet __bch2_fs_stop(c); 597d5e4dcc2SKent Overstreet bch2_fs_free(c); 598d5e4dcc2SKent Overstreet } 599d5e4dcc2SKent Overstreet 6001c6fdbd8SKent Overstreet static const char *bch2_fs_online(struct bch_fs *c) 6011c6fdbd8SKent Overstreet { 6021c6fdbd8SKent Overstreet struct bch_dev *ca; 6031c6fdbd8SKent Overstreet const char *err = NULL; 6041c6fdbd8SKent Overstreet unsigned i; 6051c6fdbd8SKent Overstreet int ret; 6061c6fdbd8SKent Overstreet 6071c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 6081c6fdbd8SKent Overstreet 6091c6fdbd8SKent Overstreet if (!list_empty(&c->list)) 6101c6fdbd8SKent Overstreet return NULL; 6111c6fdbd8SKent Overstreet 6121c6fdbd8SKent Overstreet if (__bch2_uuid_to_fs(c->sb.uuid)) 6131c6fdbd8SKent Overstreet return "filesystem UUID already open"; 6141c6fdbd8SKent Overstreet 6151c6fdbd8SKent Overstreet ret = bch2_fs_chardev_init(c); 6161c6fdbd8SKent Overstreet if (ret) 6171c6fdbd8SKent Overstreet return "error creating character device"; 6181c6fdbd8SKent Overstreet 6191c6fdbd8SKent Overstreet bch2_fs_debug_init(c); 6201c6fdbd8SKent Overstreet 6211c6fdbd8SKent Overstreet if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) || 6221c6fdbd8SKent Overstreet kobject_add(&c->internal, &c->kobj, "internal") || 6231c6fdbd8SKent Overstreet kobject_add(&c->opts_dir, &c->kobj, "options") || 6241c6fdbd8SKent Overstreet kobject_add(&c->time_stats, &c->kobj, "time_stats") || 6251c6fdbd8SKent Overstreet bch2_opts_create_sysfs_files(&c->opts_dir)) 6261c6fdbd8SKent Overstreet return "error creating sysfs objects"; 6271c6fdbd8SKent Overstreet 6281ada1606SKent Overstreet down_write(&c->state_lock); 6291c6fdbd8SKent Overstreet 6301c6fdbd8SKent Overstreet err = "error creating sysfs objects"; 6311c6fdbd8SKent Overstreet __for_each_member_device(ca, c, i, NULL) 6321c6fdbd8SKent Overstreet if (bch2_dev_sysfs_online(c, ca)) 6331c6fdbd8SKent Overstreet goto err; 6341c6fdbd8SKent Overstreet 6351c6fdbd8SKent Overstreet list_add(&c->list, &bch_fs_list); 6361c6fdbd8SKent Overstreet err = NULL; 6371c6fdbd8SKent Overstreet err: 6381ada1606SKent Overstreet up_write(&c->state_lock); 6391c6fdbd8SKent Overstreet return err; 6401c6fdbd8SKent Overstreet } 6411c6fdbd8SKent Overstreet 6421c6fdbd8SKent Overstreet static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts) 6431c6fdbd8SKent Overstreet { 6441c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 6451c6fdbd8SKent Overstreet struct bch_fs *c; 646ecf37a4aSKent Overstreet unsigned i, iter_size; 6471c6fdbd8SKent Overstreet const char *err; 6481c6fdbd8SKent Overstreet 6491c6fdbd8SKent Overstreet pr_verbose_init(opts, ""); 6501c6fdbd8SKent Overstreet 6511c6fdbd8SKent Overstreet c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO); 6521c6fdbd8SKent Overstreet if (!c) 6531c6fdbd8SKent Overstreet goto out; 6541c6fdbd8SKent Overstreet 6551c6fdbd8SKent Overstreet __module_get(THIS_MODULE); 6561c6fdbd8SKent Overstreet 657505b7a4cSKent Overstreet closure_init(&c->cl, NULL); 658505b7a4cSKent Overstreet 659505b7a4cSKent Overstreet c->kobj.kset = bcachefs_kset; 660505b7a4cSKent Overstreet kobject_init(&c->kobj, &bch2_fs_ktype); 661505b7a4cSKent Overstreet kobject_init(&c->internal, &bch2_fs_internal_ktype); 662505b7a4cSKent Overstreet kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype); 663505b7a4cSKent Overstreet kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype); 664505b7a4cSKent Overstreet 6651c6fdbd8SKent Overstreet c->minor = -1; 6661c6fdbd8SKent Overstreet c->disk_sb.fs_sb = true; 6671c6fdbd8SKent Overstreet 6681ada1606SKent Overstreet init_rwsem(&c->state_lock); 6691c6fdbd8SKent Overstreet mutex_init(&c->sb_lock); 6701c6fdbd8SKent Overstreet mutex_init(&c->replicas_gc_lock); 6711c6fdbd8SKent Overstreet mutex_init(&c->btree_root_lock); 6721c6fdbd8SKent Overstreet INIT_WORK(&c->read_only_work, bch2_fs_read_only_work); 6731c6fdbd8SKent Overstreet 6741c6fdbd8SKent Overstreet init_rwsem(&c->gc_lock); 6751c6fdbd8SKent Overstreet 6761c6fdbd8SKent Overstreet for (i = 0; i < BCH_TIME_STAT_NR; i++) 6771c6fdbd8SKent Overstreet bch2_time_stats_init(&c->times[i]); 6781c6fdbd8SKent Overstreet 679e6d11615SKent Overstreet bch2_fs_copygc_init(c); 6802ca88e5aSKent Overstreet bch2_fs_btree_key_cache_init_early(&c->btree_key_cache); 681b092daddSKent Overstreet bch2_fs_allocator_background_init(c); 682b092daddSKent Overstreet bch2_fs_allocator_foreground_init(c); 6831c6fdbd8SKent Overstreet bch2_fs_rebalance_init(c); 6841c6fdbd8SKent Overstreet bch2_fs_quota_init(c); 6851c6fdbd8SKent Overstreet 6861c6fdbd8SKent Overstreet INIT_LIST_HEAD(&c->list); 6871c6fdbd8SKent Overstreet 6884d8100daSKent Overstreet mutex_init(&c->usage_scratch_lock); 6894d8100daSKent Overstreet 6901c6fdbd8SKent Overstreet mutex_init(&c->bio_bounce_pages_lock); 6911c6fdbd8SKent Overstreet 6921c6fdbd8SKent Overstreet bio_list_init(&c->btree_write_error_list); 6931c6fdbd8SKent Overstreet spin_lock_init(&c->btree_write_error_lock); 6941c6fdbd8SKent Overstreet INIT_WORK(&c->btree_write_error_work, bch2_btree_write_error_work); 6951c6fdbd8SKent Overstreet 6961dd7f9d9SKent Overstreet INIT_WORK(&c->journal_seq_blacklist_gc_work, 6971dd7f9d9SKent Overstreet bch2_blacklist_entries_gc); 6981dd7f9d9SKent Overstreet 699f1d786a0SKent Overstreet INIT_LIST_HEAD(&c->journal_entries); 700f1d786a0SKent Overstreet 7011c6fdbd8SKent Overstreet INIT_LIST_HEAD(&c->fsck_errors); 7021c6fdbd8SKent Overstreet mutex_init(&c->fsck_error_lock); 7031c6fdbd8SKent Overstreet 704703e2a43SKent Overstreet INIT_LIST_HEAD(&c->ec_stripe_head_list); 705703e2a43SKent Overstreet mutex_init(&c->ec_stripe_head_lock); 706703e2a43SKent Overstreet 707703e2a43SKent Overstreet INIT_LIST_HEAD(&c->ec_stripe_new_list); 708703e2a43SKent Overstreet mutex_init(&c->ec_stripe_new_lock); 709703e2a43SKent Overstreet 710cd575ddfSKent Overstreet spin_lock_init(&c->ec_stripes_heap_lock); 711cd575ddfSKent Overstreet 7121c6fdbd8SKent Overstreet seqcount_init(&c->gc_pos_lock); 7131c6fdbd8SKent Overstreet 7145e82a9a1SKent Overstreet seqcount_init(&c->usage_lock); 7155e82a9a1SKent Overstreet 7161c6fdbd8SKent Overstreet c->copy_gc_enabled = 1; 7171c6fdbd8SKent Overstreet c->rebalance.enabled = 1; 7181c6fdbd8SKent Overstreet c->promote_whole_extents = true; 7191c6fdbd8SKent Overstreet 7201c6fdbd8SKent Overstreet c->journal.write_time = &c->times[BCH_TIME_journal_write]; 7211c6fdbd8SKent Overstreet c->journal.delay_time = &c->times[BCH_TIME_journal_delay]; 72249a67206SKent Overstreet c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal]; 7231c6fdbd8SKent Overstreet c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq]; 7241c6fdbd8SKent Overstreet 7251c6fdbd8SKent Overstreet bch2_fs_btree_cache_init_early(&c->btree_cache); 7261c6fdbd8SKent Overstreet 727fca1223cSKent Overstreet mutex_init(&c->sectors_available_lock); 728fca1223cSKent Overstreet 72973e6ab95SKent Overstreet if (percpu_init_rwsem(&c->mark_lock)) 73073e6ab95SKent Overstreet goto err; 73173e6ab95SKent Overstreet 7321c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 7331c6fdbd8SKent Overstreet 7341c6fdbd8SKent Overstreet if (bch2_sb_to_fs(c, sb)) { 7351c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 7361c6fdbd8SKent Overstreet goto err; 7371c6fdbd8SKent Overstreet } 7381c6fdbd8SKent Overstreet 7391c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 7401c6fdbd8SKent Overstreet 7411c6fdbd8SKent Overstreet scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid); 7421c6fdbd8SKent Overstreet 7431c6fdbd8SKent Overstreet c->opts = bch2_opts_default; 7441c6fdbd8SKent Overstreet bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb)); 7451c6fdbd8SKent Overstreet bch2_opts_apply(&c->opts, opts); 7461c6fdbd8SKent Overstreet 7471c6fdbd8SKent Overstreet c->block_bits = ilog2(c->opts.block_size); 7481c6fdbd8SKent Overstreet c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c); 7491c6fdbd8SKent Overstreet 7501c6fdbd8SKent Overstreet if (bch2_fs_init_fault("fs_alloc")) 7511c6fdbd8SKent Overstreet goto err; 7521c6fdbd8SKent Overstreet 753ae2f17d5SKent Overstreet iter_size = sizeof(struct sort_iter) + 7541c6fdbd8SKent Overstreet (btree_blocks(c) + 1) * 2 * 755ae2f17d5SKent Overstreet sizeof(struct sort_iter_set); 7561c6fdbd8SKent Overstreet 7571c6fdbd8SKent Overstreet if (!(c->wq = alloc_workqueue("bcachefs", 7581c6fdbd8SKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) || 7591c6fdbd8SKent Overstreet !(c->copygc_wq = alloc_workqueue("bcache_copygc", 7601c6fdbd8SKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) || 7610519b72dSKent Overstreet !(c->journal_reclaim_wq = alloc_workqueue("bcache_journal", 7620519b72dSKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) || 763134915f3SKent Overstreet percpu_ref_init(&c->writes, bch2_writes_disabled, 764134915f3SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 7651c6fdbd8SKent Overstreet mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) || 7661c6fdbd8SKent Overstreet bioset_init(&c->btree_bio, 1, 7671c6fdbd8SKent Overstreet max(offsetof(struct btree_read_bio, bio), 7681c6fdbd8SKent Overstreet offsetof(struct btree_write_bio, wbio.bio)), 7691c6fdbd8SKent Overstreet BIOSET_NEED_BVECS) || 7705663a415SKent Overstreet !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) || 7715e82a9a1SKent Overstreet !(c->online_reserved = alloc_percpu(u64)) || 7721c6fdbd8SKent Overstreet mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1, 7731c6fdbd8SKent Overstreet btree_bytes(c)) || 77435189e09SKent Overstreet mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) || 7751c6fdbd8SKent Overstreet bch2_io_clock_init(&c->io_clock[READ]) || 7761c6fdbd8SKent Overstreet bch2_io_clock_init(&c->io_clock[WRITE]) || 7771c6fdbd8SKent Overstreet bch2_fs_journal_init(&c->journal) || 7782c5af169SKent Overstreet bch2_fs_replicas_init(c) || 7791c6fdbd8SKent Overstreet bch2_fs_btree_cache_init(c) || 7802ca88e5aSKent Overstreet bch2_fs_btree_key_cache_init(&c->btree_key_cache) || 78136e9d698SKent Overstreet bch2_fs_btree_iter_init(c) || 782c823c339SKent Overstreet bch2_fs_btree_interior_update_init(c) || 7831c6fdbd8SKent Overstreet bch2_fs_io_init(c) || 7841c6fdbd8SKent Overstreet bch2_fs_encryption_init(c) || 7851c6fdbd8SKent Overstreet bch2_fs_compress_init(c) || 786cd575ddfSKent Overstreet bch2_fs_ec_init(c) || 7871c6fdbd8SKent Overstreet bch2_fs_fsio_init(c)) 7881c6fdbd8SKent Overstreet goto err; 7891c6fdbd8SKent Overstreet 7901c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 7911c6fdbd8SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) 7921c6fdbd8SKent Overstreet if (bch2_dev_exists(c->disk_sb.sb, mi, i) && 7931c6fdbd8SKent Overstreet bch2_dev_alloc(c, i)) 7941c6fdbd8SKent Overstreet goto err; 7951c6fdbd8SKent Overstreet 7961c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 7971c6fdbd8SKent Overstreet err = bch2_fs_online(c); 7981c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 7991c6fdbd8SKent Overstreet if (err) { 8001c6fdbd8SKent Overstreet bch_err(c, "bch2_fs_online() error: %s", err); 8011c6fdbd8SKent Overstreet goto err; 8021c6fdbd8SKent Overstreet } 8031c6fdbd8SKent Overstreet out: 8041c6fdbd8SKent Overstreet pr_verbose_init(opts, "ret %i", c ? 0 : -ENOMEM); 8051c6fdbd8SKent Overstreet return c; 8061c6fdbd8SKent Overstreet err: 8071c6fdbd8SKent Overstreet bch2_fs_free(c); 8081c6fdbd8SKent Overstreet c = NULL; 8091c6fdbd8SKent Overstreet goto out; 8101c6fdbd8SKent Overstreet } 8111c6fdbd8SKent Overstreet 812619f5beeSKent Overstreet noinline_for_stack 813619f5beeSKent Overstreet static void print_mount_opts(struct bch_fs *c) 814619f5beeSKent Overstreet { 815619f5beeSKent Overstreet enum bch_opt_id i; 816619f5beeSKent Overstreet char buf[512]; 817619f5beeSKent Overstreet struct printbuf p = PBUF(buf); 818619f5beeSKent Overstreet bool first = true; 819619f5beeSKent Overstreet 820619f5beeSKent Overstreet strcpy(buf, "(null)"); 821619f5beeSKent Overstreet 822619f5beeSKent Overstreet if (c->opts.read_only) { 823619f5beeSKent Overstreet pr_buf(&p, "ro"); 824619f5beeSKent Overstreet first = false; 825619f5beeSKent Overstreet } 826619f5beeSKent Overstreet 827619f5beeSKent Overstreet for (i = 0; i < bch2_opts_nr; i++) { 828619f5beeSKent Overstreet const struct bch_option *opt = &bch2_opt_table[i]; 829619f5beeSKent Overstreet u64 v = bch2_opt_get_by_id(&c->opts, i); 830619f5beeSKent Overstreet 831619f5beeSKent Overstreet if (!(opt->mode & OPT_MOUNT)) 832619f5beeSKent Overstreet continue; 833619f5beeSKent Overstreet 834619f5beeSKent Overstreet if (v == bch2_opt_get_by_id(&bch2_opts_default, i)) 835619f5beeSKent Overstreet continue; 836619f5beeSKent Overstreet 837619f5beeSKent Overstreet if (!first) 838619f5beeSKent Overstreet pr_buf(&p, ","); 839619f5beeSKent Overstreet first = false; 840619f5beeSKent Overstreet bch2_opt_to_text(&p, c, opt, v, OPT_SHOW_MOUNT_STYLE); 841619f5beeSKent Overstreet } 842619f5beeSKent Overstreet 843619f5beeSKent Overstreet bch_info(c, "mounted with opts: %s", buf); 844619f5beeSKent Overstreet } 845619f5beeSKent Overstreet 846619f5beeSKent Overstreet int bch2_fs_start(struct bch_fs *c) 8471c6fdbd8SKent Overstreet { 8481c6fdbd8SKent Overstreet const char *err = "cannot allocate memory"; 8491c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 8501c6fdbd8SKent Overstreet struct bch_dev *ca; 851a420eea6STim Schlueter time64_t now = ktime_get_real_seconds(); 8521c6fdbd8SKent Overstreet unsigned i; 8531c6fdbd8SKent Overstreet int ret = -EINVAL; 8541c6fdbd8SKent Overstreet 8551ada1606SKent Overstreet down_write(&c->state_lock); 8561c6fdbd8SKent Overstreet 857134915f3SKent Overstreet BUG_ON(test_bit(BCH_FS_STARTED, &c->flags)); 8581c6fdbd8SKent Overstreet 8591c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 8601c6fdbd8SKent Overstreet 8611c6fdbd8SKent Overstreet for_each_online_member(ca, c, i) 8621c6fdbd8SKent Overstreet bch2_sb_from_fs(c, ca); 8631c6fdbd8SKent Overstreet 8641c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 8651c6fdbd8SKent Overstreet for_each_online_member(ca, c, i) 8661c6fdbd8SKent Overstreet mi->members[ca->dev_idx].last_mount = cpu_to_le64(now); 8671c6fdbd8SKent Overstreet 8681c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 8691c6fdbd8SKent Overstreet 8701c6fdbd8SKent Overstreet for_each_rw_member(ca, c, i) 8711c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 8721c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 8731c6fdbd8SKent Overstreet 8741c6fdbd8SKent Overstreet ret = BCH_SB_INITIALIZED(c->disk_sb.sb) 8751c6fdbd8SKent Overstreet ? bch2_fs_recovery(c) 8761c6fdbd8SKent Overstreet : bch2_fs_initialize(c); 8771c6fdbd8SKent Overstreet if (ret) 8781c6fdbd8SKent Overstreet goto err; 8791c6fdbd8SKent Overstreet 880cd575ddfSKent Overstreet ret = bch2_opts_check_may_set(c); 881cd575ddfSKent Overstreet if (ret) 882cd575ddfSKent Overstreet goto err; 883cd575ddfSKent Overstreet 8841c6fdbd8SKent Overstreet err = "dynamic fault"; 885619f5beeSKent Overstreet ret = -EINVAL; 8861c6fdbd8SKent Overstreet if (bch2_fs_init_fault("fs_start")) 8871c6fdbd8SKent Overstreet goto err; 8881c6fdbd8SKent Overstreet 889a9310ab0SKent Overstreet set_bit(BCH_FS_STARTED, &c->flags); 890a9310ab0SKent Overstreet 891*9f20ed15SKent Overstreet /* 892*9f20ed15SKent Overstreet * Allocator threads don't start filling copygc reserve until after we 893*9f20ed15SKent Overstreet * set BCH_FS_STARTED - wake them now: 894*9f20ed15SKent Overstreet */ 895*9f20ed15SKent Overstreet for_each_online_member(ca, c, i) 896*9f20ed15SKent Overstreet bch2_wake_allocator(ca); 897*9f20ed15SKent Overstreet 898619f5beeSKent Overstreet if (c->opts.read_only || c->opts.nochanges) { 8991c6fdbd8SKent Overstreet bch2_fs_read_only(c); 9001c6fdbd8SKent Overstreet } else { 901134915f3SKent Overstreet err = "error going read write"; 902619f5beeSKent Overstreet ret = !test_bit(BCH_FS_RW, &c->flags) 903619f5beeSKent Overstreet ? bch2_fs_read_write(c) 904619f5beeSKent Overstreet : bch2_fs_read_write_late(c); 905619f5beeSKent Overstreet if (ret) 9061c6fdbd8SKent Overstreet goto err; 9071c6fdbd8SKent Overstreet } 9081c6fdbd8SKent Overstreet 909619f5beeSKent Overstreet print_mount_opts(c); 910619f5beeSKent Overstreet ret = 0; 9111c6fdbd8SKent Overstreet out: 9121ada1606SKent Overstreet up_write(&c->state_lock); 913619f5beeSKent Overstreet return ret; 9141c6fdbd8SKent Overstreet err: 9151c6fdbd8SKent Overstreet switch (ret) { 9161c6fdbd8SKent Overstreet case BCH_FSCK_ERRORS_NOT_FIXED: 9171c6fdbd8SKent Overstreet bch_err(c, "filesystem contains errors: please report this to the developers"); 9181c6fdbd8SKent Overstreet pr_cont("mount with -o fix_errors to repair\n"); 9191c6fdbd8SKent Overstreet err = "fsck error"; 9201c6fdbd8SKent Overstreet break; 9211c6fdbd8SKent Overstreet case BCH_FSCK_REPAIR_UNIMPLEMENTED: 9221c6fdbd8SKent Overstreet bch_err(c, "filesystem contains errors: please report this to the developers"); 9231c6fdbd8SKent Overstreet pr_cont("repair unimplemented: inform the developers so that it can be added\n"); 9241c6fdbd8SKent Overstreet err = "fsck error"; 9251c6fdbd8SKent Overstreet break; 9261c6fdbd8SKent Overstreet case BCH_FSCK_REPAIR_IMPOSSIBLE: 9271c6fdbd8SKent Overstreet bch_err(c, "filesystem contains errors, but repair impossible"); 9281c6fdbd8SKent Overstreet err = "fsck error"; 9291c6fdbd8SKent Overstreet break; 9301c6fdbd8SKent Overstreet case BCH_FSCK_UNKNOWN_VERSION: 9311c6fdbd8SKent Overstreet err = "unknown metadata version";; 9321c6fdbd8SKent Overstreet break; 9331c6fdbd8SKent Overstreet case -ENOMEM: 9341c6fdbd8SKent Overstreet err = "cannot allocate memory"; 9351c6fdbd8SKent Overstreet break; 9361c6fdbd8SKent Overstreet case -EIO: 9371c6fdbd8SKent Overstreet err = "IO error"; 9381c6fdbd8SKent Overstreet break; 9391c6fdbd8SKent Overstreet } 9401c6fdbd8SKent Overstreet 9419516950cSKent Overstreet if (ret >= 0) 9429516950cSKent Overstreet ret = -EIO; 9431c6fdbd8SKent Overstreet goto out; 9441c6fdbd8SKent Overstreet } 9451c6fdbd8SKent Overstreet 9461c6fdbd8SKent Overstreet static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c) 9471c6fdbd8SKent Overstreet { 9481c6fdbd8SKent Overstreet struct bch_sb_field_members *sb_mi; 9491c6fdbd8SKent Overstreet 9501c6fdbd8SKent Overstreet sb_mi = bch2_sb_get_members(sb); 9511c6fdbd8SKent Overstreet if (!sb_mi) 9521c6fdbd8SKent Overstreet return "Invalid superblock: member info area missing"; 9531c6fdbd8SKent Overstreet 9541c6fdbd8SKent Overstreet if (le16_to_cpu(sb->block_size) != c->opts.block_size) 9551c6fdbd8SKent Overstreet return "mismatched block size"; 9561c6fdbd8SKent Overstreet 9571c6fdbd8SKent Overstreet if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) < 9581c6fdbd8SKent Overstreet BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb)) 9591c6fdbd8SKent Overstreet return "new cache bucket size is too small"; 9601c6fdbd8SKent Overstreet 9611c6fdbd8SKent Overstreet return NULL; 9621c6fdbd8SKent Overstreet } 9631c6fdbd8SKent Overstreet 9641c6fdbd8SKent Overstreet static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb) 9651c6fdbd8SKent Overstreet { 9661c6fdbd8SKent Overstreet struct bch_sb *newest = 9671c6fdbd8SKent Overstreet le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb; 9681c6fdbd8SKent Overstreet struct bch_sb_field_members *mi = bch2_sb_get_members(newest); 9691c6fdbd8SKent Overstreet 9701c6fdbd8SKent Overstreet if (!uuid_equal(&fs->uuid, &sb->uuid)) 9711c6fdbd8SKent Overstreet return "device not a member of filesystem"; 9721c6fdbd8SKent Overstreet 9731c6fdbd8SKent Overstreet if (!bch2_dev_exists(newest, mi, sb->dev_idx)) 9741c6fdbd8SKent Overstreet return "device has been removed"; 9751c6fdbd8SKent Overstreet 9761c6fdbd8SKent Overstreet if (fs->block_size != sb->block_size) 9771c6fdbd8SKent Overstreet return "mismatched block size"; 9781c6fdbd8SKent Overstreet 9791c6fdbd8SKent Overstreet return NULL; 9801c6fdbd8SKent Overstreet } 9811c6fdbd8SKent Overstreet 9821c6fdbd8SKent Overstreet /* Device startup/shutdown: */ 9831c6fdbd8SKent Overstreet 9841c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *kobj) 9851c6fdbd8SKent Overstreet { 9861c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); 9871c6fdbd8SKent Overstreet 9881c6fdbd8SKent Overstreet kfree(ca); 9891c6fdbd8SKent Overstreet } 9901c6fdbd8SKent Overstreet 9911c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *ca) 9921c6fdbd8SKent Overstreet { 9931c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 9941c6fdbd8SKent Overstreet 9951c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs && 9961c6fdbd8SKent Overstreet ca->disk_sb.bdev) 9971c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 9981c6fdbd8SKent Overstreet 9991c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) 10001c6fdbd8SKent Overstreet kobject_del(&ca->kobj); 10011c6fdbd8SKent Overstreet 10021c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 10031c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 10041c6fdbd8SKent Overstreet 10051c6fdbd8SKent Overstreet free_percpu(ca->io_done); 10061c6fdbd8SKent Overstreet bioset_exit(&ca->replica_set); 10071c6fdbd8SKent Overstreet bch2_dev_buckets_free(ca); 1008d1170ce5SKent Overstreet free_page((unsigned long) ca->sb_read_scratch); 10091c6fdbd8SKent Overstreet 10101c6fdbd8SKent Overstreet bch2_time_stats_exit(&ca->io_latency[WRITE]); 10111c6fdbd8SKent Overstreet bch2_time_stats_exit(&ca->io_latency[READ]); 10121c6fdbd8SKent Overstreet 10131c6fdbd8SKent Overstreet percpu_ref_exit(&ca->io_ref); 10141c6fdbd8SKent Overstreet percpu_ref_exit(&ca->ref); 10151c6fdbd8SKent Overstreet kobject_put(&ca->kobj); 10161c6fdbd8SKent Overstreet } 10171c6fdbd8SKent Overstreet 10181c6fdbd8SKent Overstreet static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) 10191c6fdbd8SKent Overstreet { 10201c6fdbd8SKent Overstreet 10211c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 10221c6fdbd8SKent Overstreet 10231c6fdbd8SKent Overstreet if (percpu_ref_is_zero(&ca->io_ref)) 10241c6fdbd8SKent Overstreet return; 10251c6fdbd8SKent Overstreet 10261c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 10271c6fdbd8SKent Overstreet 10281c6fdbd8SKent Overstreet reinit_completion(&ca->io_ref_completion); 10291c6fdbd8SKent Overstreet percpu_ref_kill(&ca->io_ref); 10301c6fdbd8SKent Overstreet wait_for_completion(&ca->io_ref_completion); 10311c6fdbd8SKent Overstreet 10321c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) { 10331c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 10341c6fdbd8SKent Overstreet sysfs_remove_link(&ca->kobj, "block"); 10351c6fdbd8SKent Overstreet } 10361c6fdbd8SKent Overstreet 10371c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 10381c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 10391c6fdbd8SKent Overstreet } 10401c6fdbd8SKent Overstreet 10411c6fdbd8SKent Overstreet static void bch2_dev_ref_complete(struct percpu_ref *ref) 10421c6fdbd8SKent Overstreet { 10431c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, ref); 10441c6fdbd8SKent Overstreet 10451c6fdbd8SKent Overstreet complete(&ca->ref_completion); 10461c6fdbd8SKent Overstreet } 10471c6fdbd8SKent Overstreet 10481c6fdbd8SKent Overstreet static void bch2_dev_io_ref_complete(struct percpu_ref *ref) 10491c6fdbd8SKent Overstreet { 10501c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref); 10511c6fdbd8SKent Overstreet 10521c6fdbd8SKent Overstreet complete(&ca->io_ref_completion); 10531c6fdbd8SKent Overstreet } 10541c6fdbd8SKent Overstreet 10551c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) 10561c6fdbd8SKent Overstreet { 10571c6fdbd8SKent Overstreet int ret; 10581c6fdbd8SKent Overstreet 10591c6fdbd8SKent Overstreet if (!c->kobj.state_in_sysfs) 10601c6fdbd8SKent Overstreet return 0; 10611c6fdbd8SKent Overstreet 10621c6fdbd8SKent Overstreet if (!ca->kobj.state_in_sysfs) { 10631c6fdbd8SKent Overstreet ret = kobject_add(&ca->kobj, &c->kobj, 10641c6fdbd8SKent Overstreet "dev-%u", ca->dev_idx); 10651c6fdbd8SKent Overstreet if (ret) 10661c6fdbd8SKent Overstreet return ret; 10671c6fdbd8SKent Overstreet } 10681c6fdbd8SKent Overstreet 10691c6fdbd8SKent Overstreet if (ca->disk_sb.bdev) { 10701c6fdbd8SKent Overstreet struct kobject *block = bdev_kobj(ca->disk_sb.bdev); 10711c6fdbd8SKent Overstreet 10721c6fdbd8SKent Overstreet ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); 10731c6fdbd8SKent Overstreet if (ret) 10741c6fdbd8SKent Overstreet return ret; 10751c6fdbd8SKent Overstreet 10761c6fdbd8SKent Overstreet ret = sysfs_create_link(&ca->kobj, block, "block"); 10771c6fdbd8SKent Overstreet if (ret) 10781c6fdbd8SKent Overstreet return ret; 10791c6fdbd8SKent Overstreet } 10801c6fdbd8SKent Overstreet 10811c6fdbd8SKent Overstreet return 0; 10821c6fdbd8SKent Overstreet } 10831c6fdbd8SKent Overstreet 10841c6fdbd8SKent Overstreet static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c, 10851c6fdbd8SKent Overstreet struct bch_member *member) 10861c6fdbd8SKent Overstreet { 10871c6fdbd8SKent Overstreet struct bch_dev *ca; 10881c6fdbd8SKent Overstreet 10891c6fdbd8SKent Overstreet ca = kzalloc(sizeof(*ca), GFP_KERNEL); 10901c6fdbd8SKent Overstreet if (!ca) 10911c6fdbd8SKent Overstreet return NULL; 10921c6fdbd8SKent Overstreet 10931c6fdbd8SKent Overstreet kobject_init(&ca->kobj, &bch2_dev_ktype); 10941c6fdbd8SKent Overstreet init_completion(&ca->ref_completion); 10951c6fdbd8SKent Overstreet init_completion(&ca->io_ref_completion); 10961c6fdbd8SKent Overstreet 10971c6fdbd8SKent Overstreet init_rwsem(&ca->bucket_lock); 10981c6fdbd8SKent Overstreet 10991c6fdbd8SKent Overstreet INIT_WORK(&ca->io_error_work, bch2_io_error_work); 11001c6fdbd8SKent Overstreet 11011c6fdbd8SKent Overstreet bch2_time_stats_init(&ca->io_latency[READ]); 11021c6fdbd8SKent Overstreet bch2_time_stats_init(&ca->io_latency[WRITE]); 11031c6fdbd8SKent Overstreet 11041c6fdbd8SKent Overstreet ca->mi = bch2_mi_to_cpu(member); 11051c6fdbd8SKent Overstreet ca->uuid = member->uuid; 11061c6fdbd8SKent Overstreet 11071c6fdbd8SKent Overstreet if (opt_defined(c->opts, discard)) 11081c6fdbd8SKent Overstreet ca->mi.discard = opt_get(c->opts, discard); 11091c6fdbd8SKent Overstreet 11101c6fdbd8SKent Overstreet if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, 11111c6fdbd8SKent Overstreet 0, GFP_KERNEL) || 11121c6fdbd8SKent Overstreet percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete, 11131c6fdbd8SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 1114d1170ce5SKent Overstreet !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) || 11151c6fdbd8SKent Overstreet bch2_dev_buckets_alloc(c, ca) || 11161c6fdbd8SKent Overstreet bioset_init(&ca->replica_set, 4, 11171c6fdbd8SKent Overstreet offsetof(struct bch_write_bio, bio), 0) || 11181c6fdbd8SKent Overstreet !(ca->io_done = alloc_percpu(*ca->io_done))) 11191c6fdbd8SKent Overstreet goto err; 11201c6fdbd8SKent Overstreet 11211c6fdbd8SKent Overstreet return ca; 11221c6fdbd8SKent Overstreet err: 11231c6fdbd8SKent Overstreet bch2_dev_free(ca); 11241c6fdbd8SKent Overstreet return NULL; 11251c6fdbd8SKent Overstreet } 11261c6fdbd8SKent Overstreet 11271c6fdbd8SKent Overstreet static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, 11281c6fdbd8SKent Overstreet unsigned dev_idx) 11291c6fdbd8SKent Overstreet { 11301c6fdbd8SKent Overstreet ca->dev_idx = dev_idx; 11311c6fdbd8SKent Overstreet __set_bit(ca->dev_idx, ca->self.d); 11321c6fdbd8SKent Overstreet scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); 11331c6fdbd8SKent Overstreet 11341c6fdbd8SKent Overstreet ca->fs = c; 11351c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], ca); 11361c6fdbd8SKent Overstreet 11371c6fdbd8SKent Overstreet if (bch2_dev_sysfs_online(c, ca)) 11381c6fdbd8SKent Overstreet pr_warn("error creating sysfs objects"); 11391c6fdbd8SKent Overstreet } 11401c6fdbd8SKent Overstreet 11411c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx) 11421c6fdbd8SKent Overstreet { 11431c6fdbd8SKent Overstreet struct bch_member *member = 11441c6fdbd8SKent Overstreet bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx; 11451c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 11461c6fdbd8SKent Overstreet int ret = 0; 11471c6fdbd8SKent Overstreet 11481c6fdbd8SKent Overstreet pr_verbose_init(c->opts, ""); 11491c6fdbd8SKent Overstreet 11501c6fdbd8SKent Overstreet if (bch2_fs_init_fault("dev_alloc")) 11511c6fdbd8SKent Overstreet goto err; 11521c6fdbd8SKent Overstreet 11531c6fdbd8SKent Overstreet ca = __bch2_dev_alloc(c, member); 11541c6fdbd8SKent Overstreet if (!ca) 11551c6fdbd8SKent Overstreet goto err; 11561c6fdbd8SKent Overstreet 11571c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 11581c6fdbd8SKent Overstreet out: 11591c6fdbd8SKent Overstreet pr_verbose_init(c->opts, "ret %i", ret); 11601c6fdbd8SKent Overstreet return ret; 11611c6fdbd8SKent Overstreet err: 11621c6fdbd8SKent Overstreet if (ca) 11631c6fdbd8SKent Overstreet bch2_dev_free(ca); 11641c6fdbd8SKent Overstreet ret = -ENOMEM; 11651c6fdbd8SKent Overstreet goto out; 11661c6fdbd8SKent Overstreet } 11671c6fdbd8SKent Overstreet 11681c6fdbd8SKent Overstreet static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb) 11691c6fdbd8SKent Overstreet { 11701c6fdbd8SKent Overstreet unsigned ret; 11711c6fdbd8SKent Overstreet 11721c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca)) { 11731c6fdbd8SKent Overstreet bch_err(ca, "already have device online in slot %u", 11741c6fdbd8SKent Overstreet sb->sb->dev_idx); 11751c6fdbd8SKent Overstreet return -EINVAL; 11761c6fdbd8SKent Overstreet } 11771c6fdbd8SKent Overstreet 11781c6fdbd8SKent Overstreet if (get_capacity(sb->bdev->bd_disk) < 11791c6fdbd8SKent Overstreet ca->mi.bucket_size * ca->mi.nbuckets) { 11801c6fdbd8SKent Overstreet bch_err(ca, "cannot online: device too small"); 11811c6fdbd8SKent Overstreet return -EINVAL; 11821c6fdbd8SKent Overstreet } 11831c6fdbd8SKent Overstreet 11841c6fdbd8SKent Overstreet BUG_ON(!percpu_ref_is_zero(&ca->io_ref)); 11851c6fdbd8SKent Overstreet 11861c6fdbd8SKent Overstreet if (get_capacity(sb->bdev->bd_disk) < 11871c6fdbd8SKent Overstreet ca->mi.bucket_size * ca->mi.nbuckets) { 11881c6fdbd8SKent Overstreet bch_err(ca, "device too small"); 11891c6fdbd8SKent Overstreet return -EINVAL; 11901c6fdbd8SKent Overstreet } 11911c6fdbd8SKent Overstreet 11921c6fdbd8SKent Overstreet ret = bch2_dev_journal_init(ca, sb->sb); 11931c6fdbd8SKent Overstreet if (ret) 11941c6fdbd8SKent Overstreet return ret; 11951c6fdbd8SKent Overstreet 11961c6fdbd8SKent Overstreet /* Commit: */ 11971c6fdbd8SKent Overstreet ca->disk_sb = *sb; 11981c6fdbd8SKent Overstreet memset(sb, 0, sizeof(*sb)); 11991c6fdbd8SKent Overstreet 12001c6fdbd8SKent Overstreet percpu_ref_reinit(&ca->io_ref); 12011c6fdbd8SKent Overstreet 12021c6fdbd8SKent Overstreet return 0; 12031c6fdbd8SKent Overstreet } 12041c6fdbd8SKent Overstreet 12051c6fdbd8SKent Overstreet static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb) 12061c6fdbd8SKent Overstreet { 12071c6fdbd8SKent Overstreet struct bch_dev *ca; 12081c6fdbd8SKent Overstreet int ret; 12091c6fdbd8SKent Overstreet 12101c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 12111c6fdbd8SKent Overstreet 12121c6fdbd8SKent Overstreet if (le64_to_cpu(sb->sb->seq) > 12131c6fdbd8SKent Overstreet le64_to_cpu(c->disk_sb.sb->seq)) 12141c6fdbd8SKent Overstreet bch2_sb_to_fs(c, sb->sb); 12151c6fdbd8SKent Overstreet 12161c6fdbd8SKent Overstreet BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices || 12171c6fdbd8SKent Overstreet !c->devs[sb->sb->dev_idx]); 12181c6fdbd8SKent Overstreet 12191c6fdbd8SKent Overstreet ca = bch_dev_locked(c, sb->sb->dev_idx); 12201c6fdbd8SKent Overstreet 12211c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, sb); 12221c6fdbd8SKent Overstreet if (ret) 12231c6fdbd8SKent Overstreet return ret; 12241c6fdbd8SKent Overstreet 12253e0745e2SKent Overstreet if (test_bit(BCH_FS_ALLOC_READ_DONE, &c->flags) && 122689fd25beSKent Overstreet !percpu_u64_get(&ca->usage[0]->buckets[BCH_DATA_sb])) { 12276eac2c2eSKent Overstreet mutex_lock(&c->sb_lock); 12289ca53b55SKent Overstreet bch2_mark_dev_superblock(ca->fs, ca, 0); 12296eac2c2eSKent Overstreet mutex_unlock(&c->sb_lock); 12303e0745e2SKent Overstreet } 12316eac2c2eSKent Overstreet 12321c6fdbd8SKent Overstreet bch2_dev_sysfs_online(c, ca); 12331c6fdbd8SKent Overstreet 12341c6fdbd8SKent Overstreet if (c->sb.nr_devices == 1) 12351c6fdbd8SKent Overstreet snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev); 12361c6fdbd8SKent Overstreet snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev); 12371c6fdbd8SKent Overstreet 12381c6fdbd8SKent Overstreet rebalance_wakeup(c); 12391c6fdbd8SKent Overstreet return 0; 12401c6fdbd8SKent Overstreet } 12411c6fdbd8SKent Overstreet 12421c6fdbd8SKent Overstreet /* Device management: */ 12431c6fdbd8SKent Overstreet 12441c6fdbd8SKent Overstreet /* 12451c6fdbd8SKent Overstreet * Note: this function is also used by the error paths - when a particular 12461c6fdbd8SKent Overstreet * device sees an error, we call it to determine whether we can just set the 12471c6fdbd8SKent Overstreet * device RO, or - if this function returns false - we'll set the whole 12481c6fdbd8SKent Overstreet * filesystem RO: 12491c6fdbd8SKent Overstreet * 12501c6fdbd8SKent Overstreet * XXX: maybe we should be more explicit about whether we're changing state 12511c6fdbd8SKent Overstreet * because we got an error or what have you? 12521c6fdbd8SKent Overstreet */ 12531c6fdbd8SKent Overstreet bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, 12541c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 12551c6fdbd8SKent Overstreet { 12561c6fdbd8SKent Overstreet struct bch_devs_mask new_online_devs; 12571c6fdbd8SKent Overstreet struct replicas_status s; 12581c6fdbd8SKent Overstreet struct bch_dev *ca2; 12591c6fdbd8SKent Overstreet int i, nr_rw = 0, required; 12601c6fdbd8SKent Overstreet 12611c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 12621c6fdbd8SKent Overstreet 12631c6fdbd8SKent Overstreet switch (new_state) { 12641c6fdbd8SKent Overstreet case BCH_MEMBER_STATE_RW: 12651c6fdbd8SKent Overstreet return true; 12661c6fdbd8SKent Overstreet case BCH_MEMBER_STATE_RO: 12671c6fdbd8SKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_RW) 12681c6fdbd8SKent Overstreet return true; 12691c6fdbd8SKent Overstreet 12701c6fdbd8SKent Overstreet /* do we have enough devices to write to? */ 12711c6fdbd8SKent Overstreet for_each_member_device(ca2, c, i) 12721c6fdbd8SKent Overstreet if (ca2 != ca) 12731c6fdbd8SKent Overstreet nr_rw += ca2->mi.state == BCH_MEMBER_STATE_RW; 12741c6fdbd8SKent Overstreet 12751c6fdbd8SKent Overstreet required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED) 12761c6fdbd8SKent Overstreet ? c->opts.metadata_replicas 12771c6fdbd8SKent Overstreet : c->opts.metadata_replicas_required, 12781c6fdbd8SKent Overstreet !(flags & BCH_FORCE_IF_DATA_DEGRADED) 12791c6fdbd8SKent Overstreet ? c->opts.data_replicas 12801c6fdbd8SKent Overstreet : c->opts.data_replicas_required); 12811c6fdbd8SKent Overstreet 12821c6fdbd8SKent Overstreet return nr_rw >= required; 12831c6fdbd8SKent Overstreet case BCH_MEMBER_STATE_FAILED: 12841c6fdbd8SKent Overstreet case BCH_MEMBER_STATE_SPARE: 12851c6fdbd8SKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_RW && 12861c6fdbd8SKent Overstreet ca->mi.state != BCH_MEMBER_STATE_RO) 12871c6fdbd8SKent Overstreet return true; 12881c6fdbd8SKent Overstreet 12891c6fdbd8SKent Overstreet /* do we have enough devices to read from? */ 12901c6fdbd8SKent Overstreet new_online_devs = bch2_online_devs(c); 12911c6fdbd8SKent Overstreet __clear_bit(ca->dev_idx, new_online_devs.d); 12921c6fdbd8SKent Overstreet 12931c6fdbd8SKent Overstreet s = __bch2_replicas_status(c, new_online_devs); 12941c6fdbd8SKent Overstreet 12951c6fdbd8SKent Overstreet return bch2_have_enough_devs(s, flags); 12961c6fdbd8SKent Overstreet default: 12971c6fdbd8SKent Overstreet BUG(); 12981c6fdbd8SKent Overstreet } 12991c6fdbd8SKent Overstreet } 13001c6fdbd8SKent Overstreet 13011c6fdbd8SKent Overstreet static bool bch2_fs_may_start(struct bch_fs *c) 13021c6fdbd8SKent Overstreet { 13031c6fdbd8SKent Overstreet struct replicas_status s; 13041c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 13051c6fdbd8SKent Overstreet struct bch_dev *ca; 13061c6fdbd8SKent Overstreet unsigned i, flags = c->opts.degraded 13071c6fdbd8SKent Overstreet ? BCH_FORCE_IF_DEGRADED 13081c6fdbd8SKent Overstreet : 0; 13091c6fdbd8SKent Overstreet 13101c6fdbd8SKent Overstreet if (!c->opts.degraded) { 13111c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 13121c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 13131c6fdbd8SKent Overstreet 13141c6fdbd8SKent Overstreet for (i = 0; i < c->disk_sb.sb->nr_devices; i++) { 13151c6fdbd8SKent Overstreet if (!bch2_dev_exists(c->disk_sb.sb, mi, i)) 13161c6fdbd8SKent Overstreet continue; 13171c6fdbd8SKent Overstreet 13181c6fdbd8SKent Overstreet ca = bch_dev_locked(c, i); 13191c6fdbd8SKent Overstreet 13201c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca) && 13211c6fdbd8SKent Overstreet (ca->mi.state == BCH_MEMBER_STATE_RW || 13221c6fdbd8SKent Overstreet ca->mi.state == BCH_MEMBER_STATE_RO)) { 13231c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 13241c6fdbd8SKent Overstreet return false; 13251c6fdbd8SKent Overstreet } 13261c6fdbd8SKent Overstreet } 13271c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 13281c6fdbd8SKent Overstreet } 13291c6fdbd8SKent Overstreet 13301c6fdbd8SKent Overstreet s = bch2_replicas_status(c); 13311c6fdbd8SKent Overstreet 13321c6fdbd8SKent Overstreet return bch2_have_enough_devs(s, flags); 13331c6fdbd8SKent Overstreet } 13341c6fdbd8SKent Overstreet 13351c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) 13361c6fdbd8SKent Overstreet { 13371c6fdbd8SKent Overstreet /* 133874ed7e56SKent Overstreet * Device going read only means the copygc reserve get smaller, so we 133974ed7e56SKent Overstreet * don't want that happening while copygc is in progress: 134074ed7e56SKent Overstreet */ 134174ed7e56SKent Overstreet bch2_copygc_stop(c); 134274ed7e56SKent Overstreet 134374ed7e56SKent Overstreet /* 13441c6fdbd8SKent Overstreet * The allocator thread itself allocates btree nodes, so stop it first: 13451c6fdbd8SKent Overstreet */ 13461c6fdbd8SKent Overstreet bch2_dev_allocator_stop(ca); 13471c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 13481c6fdbd8SKent Overstreet bch2_dev_journal_stop(&c->journal, ca); 134974ed7e56SKent Overstreet 135074ed7e56SKent Overstreet bch2_copygc_start(c); 13511c6fdbd8SKent Overstreet } 13521c6fdbd8SKent Overstreet 13531c6fdbd8SKent Overstreet static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) 13541c6fdbd8SKent Overstreet { 13551c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 13561c6fdbd8SKent Overstreet 13571c6fdbd8SKent Overstreet BUG_ON(ca->mi.state != BCH_MEMBER_STATE_RW); 13581c6fdbd8SKent Overstreet 13591c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 13601c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 13611c6fdbd8SKent Overstreet 13621c6fdbd8SKent Overstreet if (bch2_dev_allocator_start(ca)) 13631c6fdbd8SKent Overstreet return "error starting allocator thread"; 13641c6fdbd8SKent Overstreet 13651c6fdbd8SKent Overstreet return NULL; 13661c6fdbd8SKent Overstreet } 13671c6fdbd8SKent Overstreet 13681c6fdbd8SKent Overstreet int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 13691c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 13701c6fdbd8SKent Overstreet { 13711c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 13721c6fdbd8SKent Overstreet int ret = 0; 13731c6fdbd8SKent Overstreet 13741c6fdbd8SKent Overstreet if (ca->mi.state == new_state) 13751c6fdbd8SKent Overstreet return 0; 13761c6fdbd8SKent Overstreet 13771c6fdbd8SKent Overstreet if (!bch2_dev_state_allowed(c, ca, new_state, flags)) 13781c6fdbd8SKent Overstreet return -EINVAL; 13791c6fdbd8SKent Overstreet 13801c6fdbd8SKent Overstreet if (new_state != BCH_MEMBER_STATE_RW) 13811c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 13821c6fdbd8SKent Overstreet 13831c6fdbd8SKent Overstreet bch_notice(ca, "%s", bch2_dev_state[new_state]); 13841c6fdbd8SKent Overstreet 13851c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 13861c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 13871c6fdbd8SKent Overstreet SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state); 13881c6fdbd8SKent Overstreet bch2_write_super(c); 13891c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 13901c6fdbd8SKent Overstreet 13911c6fdbd8SKent Overstreet if (new_state == BCH_MEMBER_STATE_RW && 13921c6fdbd8SKent Overstreet __bch2_dev_read_write(c, ca)) 13931c6fdbd8SKent Overstreet ret = -ENOMEM; 13941c6fdbd8SKent Overstreet 13951c6fdbd8SKent Overstreet rebalance_wakeup(c); 13961c6fdbd8SKent Overstreet 13971c6fdbd8SKent Overstreet return ret; 13981c6fdbd8SKent Overstreet } 13991c6fdbd8SKent Overstreet 14001c6fdbd8SKent Overstreet int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 14011c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 14021c6fdbd8SKent Overstreet { 14031c6fdbd8SKent Overstreet int ret; 14041c6fdbd8SKent Overstreet 14051ada1606SKent Overstreet down_write(&c->state_lock); 14061c6fdbd8SKent Overstreet ret = __bch2_dev_set_state(c, ca, new_state, flags); 14071ada1606SKent Overstreet up_write(&c->state_lock); 14081c6fdbd8SKent Overstreet 14091c6fdbd8SKent Overstreet return ret; 14101c6fdbd8SKent Overstreet } 14111c6fdbd8SKent Overstreet 14121c6fdbd8SKent Overstreet /* Device add/removal: */ 14131c6fdbd8SKent Overstreet 14145d20ba48SKent Overstreet int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca) 14155d20ba48SKent Overstreet { 14165d20ba48SKent Overstreet struct btree_trans trans; 14175d20ba48SKent Overstreet size_t i; 14185d20ba48SKent Overstreet int ret; 14195d20ba48SKent Overstreet 14205d20ba48SKent Overstreet bch2_trans_init(&trans, c, 0, 0); 14215d20ba48SKent Overstreet 14225d20ba48SKent Overstreet for (i = 0; i < ca->mi.nbuckets; i++) { 14235d20ba48SKent Overstreet ret = bch2_btree_key_cache_flush(&trans, 14245d20ba48SKent Overstreet BTREE_ID_ALLOC, POS(ca->dev_idx, i)); 14255d20ba48SKent Overstreet if (ret) 14265d20ba48SKent Overstreet break; 14275d20ba48SKent Overstreet } 14285d20ba48SKent Overstreet bch2_trans_exit(&trans); 14295d20ba48SKent Overstreet 14305d20ba48SKent Overstreet if (ret) 14315d20ba48SKent Overstreet return ret; 14325d20ba48SKent Overstreet 14335d20ba48SKent Overstreet return bch2_btree_delete_range(c, BTREE_ID_ALLOC, 14345d20ba48SKent Overstreet POS(ca->dev_idx, 0), 14355d20ba48SKent Overstreet POS(ca->dev_idx + 1, 0), 14365d20ba48SKent Overstreet NULL); 14375d20ba48SKent Overstreet } 14385d20ba48SKent Overstreet 14391c6fdbd8SKent Overstreet int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) 14401c6fdbd8SKent Overstreet { 14411c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 14421c6fdbd8SKent Overstreet unsigned dev_idx = ca->dev_idx, data; 14431c6fdbd8SKent Overstreet int ret = -EINVAL; 14441c6fdbd8SKent Overstreet 14451ada1606SKent Overstreet down_write(&c->state_lock); 14461c6fdbd8SKent Overstreet 144731ba2cd3SKent Overstreet /* 144831ba2cd3SKent Overstreet * We consume a reference to ca->ref, regardless of whether we succeed 144931ba2cd3SKent Overstreet * or fail: 145031ba2cd3SKent Overstreet */ 145131ba2cd3SKent Overstreet percpu_ref_put(&ca->ref); 14521c6fdbd8SKent Overstreet 14531c6fdbd8SKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) { 14541c6fdbd8SKent Overstreet bch_err(ca, "Cannot remove without losing data"); 14551c6fdbd8SKent Overstreet goto err; 14561c6fdbd8SKent Overstreet } 14571c6fdbd8SKent Overstreet 14581c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 14591c6fdbd8SKent Overstreet 14601c6fdbd8SKent Overstreet ret = bch2_dev_data_drop(c, ca->dev_idx, flags); 14611c6fdbd8SKent Overstreet if (ret) { 14621c6fdbd8SKent Overstreet bch_err(ca, "Remove failed: error %i dropping data", ret); 14631c6fdbd8SKent Overstreet goto err; 14641c6fdbd8SKent Overstreet } 14651c6fdbd8SKent Overstreet 14661c6fdbd8SKent Overstreet ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); 14671c6fdbd8SKent Overstreet if (ret) { 14681c6fdbd8SKent Overstreet bch_err(ca, "Remove failed: error %i flushing journal", ret); 14691c6fdbd8SKent Overstreet goto err; 14701c6fdbd8SKent Overstreet } 14711c6fdbd8SKent Overstreet 14725d20ba48SKent Overstreet ret = bch2_dev_remove_alloc(c, ca); 14731c6fdbd8SKent Overstreet if (ret) { 14741c6fdbd8SKent Overstreet bch_err(ca, "Remove failed, error deleting alloc info"); 14751c6fdbd8SKent Overstreet goto err; 14761c6fdbd8SKent Overstreet } 14771c6fdbd8SKent Overstreet 14781c6fdbd8SKent Overstreet /* 14791c6fdbd8SKent Overstreet * must flush all existing journal entries, they might have 14801c6fdbd8SKent Overstreet * (overwritten) keys that point to the device we're removing: 14811c6fdbd8SKent Overstreet */ 14821c6fdbd8SKent Overstreet bch2_journal_flush_all_pins(&c->journal); 148331ba2cd3SKent Overstreet /* 148431ba2cd3SKent Overstreet * hack to ensure bch2_replicas_gc2() clears out entries to this device 148531ba2cd3SKent Overstreet */ 148631ba2cd3SKent Overstreet bch2_journal_meta(&c->journal); 14871c6fdbd8SKent Overstreet ret = bch2_journal_error(&c->journal); 14881c6fdbd8SKent Overstreet if (ret) { 14891c6fdbd8SKent Overstreet bch_err(ca, "Remove failed, journal error"); 14901c6fdbd8SKent Overstreet goto err; 14911c6fdbd8SKent Overstreet } 14921c6fdbd8SKent Overstreet 149331ba2cd3SKent Overstreet ret = bch2_replicas_gc2(c); 149431ba2cd3SKent Overstreet if (ret) { 149531ba2cd3SKent Overstreet bch_err(ca, "Remove failed: error %i from replicas gc", ret); 149631ba2cd3SKent Overstreet goto err; 149731ba2cd3SKent Overstreet } 149831ba2cd3SKent Overstreet 149931ba2cd3SKent Overstreet data = bch2_dev_has_data(c, ca); 150031ba2cd3SKent Overstreet if (data) { 150131ba2cd3SKent Overstreet char data_has_str[100]; 150231ba2cd3SKent Overstreet 150331ba2cd3SKent Overstreet bch2_flags_to_text(&PBUF(data_has_str), 150431ba2cd3SKent Overstreet bch2_data_types, data); 150531ba2cd3SKent Overstreet bch_err(ca, "Remove failed, still has data (%s)", data_has_str); 150631ba2cd3SKent Overstreet ret = -EBUSY; 150731ba2cd3SKent Overstreet goto err; 150831ba2cd3SKent Overstreet } 150931ba2cd3SKent Overstreet 15101c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 15111c6fdbd8SKent Overstreet 15121c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15131c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], NULL); 15141c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15151c6fdbd8SKent Overstreet 15161c6fdbd8SKent Overstreet percpu_ref_kill(&ca->ref); 15171c6fdbd8SKent Overstreet wait_for_completion(&ca->ref_completion); 15181c6fdbd8SKent Overstreet 15191c6fdbd8SKent Overstreet bch2_dev_free(ca); 15201c6fdbd8SKent Overstreet 15211c6fdbd8SKent Overstreet /* 15221c6fdbd8SKent Overstreet * Free this device's slot in the bch_member array - all pointers to 15231c6fdbd8SKent Overstreet * this device must be gone: 15241c6fdbd8SKent Overstreet */ 15251c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15261c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 15271c6fdbd8SKent Overstreet memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid)); 15281c6fdbd8SKent Overstreet 15291c6fdbd8SKent Overstreet bch2_write_super(c); 15301c6fdbd8SKent Overstreet 15311c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15321ada1606SKent Overstreet up_write(&c->state_lock); 15331c6fdbd8SKent Overstreet return 0; 15341c6fdbd8SKent Overstreet err: 1535d3bb629dSKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_RW && 1536d3bb629dSKent Overstreet !percpu_ref_is_zero(&ca->io_ref)) 15371c6fdbd8SKent Overstreet __bch2_dev_read_write(c, ca); 15381ada1606SKent Overstreet up_write(&c->state_lock); 15391c6fdbd8SKent Overstreet return ret; 15401c6fdbd8SKent Overstreet } 15411c6fdbd8SKent Overstreet 15426eac2c2eSKent Overstreet static void dev_usage_clear(struct bch_dev *ca) 15436eac2c2eSKent Overstreet { 15446eac2c2eSKent Overstreet struct bucket_array *buckets; 15456eac2c2eSKent Overstreet 15465e82a9a1SKent Overstreet percpu_memset(ca->usage[0], 0, sizeof(*ca->usage[0])); 15476eac2c2eSKent Overstreet 15486eac2c2eSKent Overstreet down_read(&ca->bucket_lock); 15496eac2c2eSKent Overstreet buckets = bucket_array(ca); 15506eac2c2eSKent Overstreet 15516eac2c2eSKent Overstreet memset(buckets->b, 0, sizeof(buckets->b[0]) * buckets->nbuckets); 15526eac2c2eSKent Overstreet up_read(&ca->bucket_lock); 15536eac2c2eSKent Overstreet } 15546eac2c2eSKent Overstreet 15551c6fdbd8SKent Overstreet /* Add new device to running filesystem: */ 15561c6fdbd8SKent Overstreet int bch2_dev_add(struct bch_fs *c, const char *path) 15571c6fdbd8SKent Overstreet { 15581c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 15591c6fdbd8SKent Overstreet struct bch_sb_handle sb; 15601c6fdbd8SKent Overstreet const char *err; 15611c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 15621c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 15631c6fdbd8SKent Overstreet struct bch_member dev_mi; 15641c6fdbd8SKent Overstreet unsigned dev_idx, nr_devices, u64s; 15651c6fdbd8SKent Overstreet int ret; 15661c6fdbd8SKent Overstreet 15671c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 15681c6fdbd8SKent Overstreet if (ret) 15691c6fdbd8SKent Overstreet return ret; 15701c6fdbd8SKent Overstreet 15711c6fdbd8SKent Overstreet err = bch2_sb_validate(&sb); 15721c6fdbd8SKent Overstreet if (err) 15731c6fdbd8SKent Overstreet return -EINVAL; 15741c6fdbd8SKent Overstreet 15751c6fdbd8SKent Overstreet dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx]; 15761c6fdbd8SKent Overstreet 15771c6fdbd8SKent Overstreet err = bch2_dev_may_add(sb.sb, c); 15781c6fdbd8SKent Overstreet if (err) 15791c6fdbd8SKent Overstreet return -EINVAL; 15801c6fdbd8SKent Overstreet 15811c6fdbd8SKent Overstreet ca = __bch2_dev_alloc(c, &dev_mi); 15821c6fdbd8SKent Overstreet if (!ca) { 15831c6fdbd8SKent Overstreet bch2_free_super(&sb); 15841c6fdbd8SKent Overstreet return -ENOMEM; 15851c6fdbd8SKent Overstreet } 15861c6fdbd8SKent Overstreet 15871c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, &sb); 15881c6fdbd8SKent Overstreet if (ret) { 15891c6fdbd8SKent Overstreet bch2_dev_free(ca); 15901c6fdbd8SKent Overstreet return ret; 15911c6fdbd8SKent Overstreet } 15921c6fdbd8SKent Overstreet 15936eac2c2eSKent Overstreet /* 15946eac2c2eSKent Overstreet * We want to allocate journal on the new device before adding the new 15956eac2c2eSKent Overstreet * device to the filesystem because allocating after we attach requires 15966eac2c2eSKent Overstreet * spinning up the allocator thread, and the allocator thread requires 15976eac2c2eSKent Overstreet * doing btree writes, which if the existing devices are RO isn't going 15986eac2c2eSKent Overstreet * to work 15996eac2c2eSKent Overstreet * 16006eac2c2eSKent Overstreet * So we have to mark where the superblocks are, but marking allocated 16016eac2c2eSKent Overstreet * data normally updates the filesystem usage too, so we have to mark, 16026eac2c2eSKent Overstreet * allocate the journal, reset all the marks, then remark after we 16036eac2c2eSKent Overstreet * attach... 16046eac2c2eSKent Overstreet */ 16059ca53b55SKent Overstreet bch2_mark_dev_superblock(ca->fs, ca, 0); 16066eac2c2eSKent Overstreet 16071c6fdbd8SKent Overstreet err = "journal alloc failed"; 16081c6fdbd8SKent Overstreet ret = bch2_dev_journal_alloc(ca); 16091c6fdbd8SKent Overstreet if (ret) 16101c6fdbd8SKent Overstreet goto err; 16111c6fdbd8SKent Overstreet 16126eac2c2eSKent Overstreet dev_usage_clear(ca); 16136eac2c2eSKent Overstreet 16141ada1606SKent Overstreet down_write(&c->state_lock); 16151c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 16161c6fdbd8SKent Overstreet 16171c6fdbd8SKent Overstreet err = "insufficient space in new superblock"; 16181c6fdbd8SKent Overstreet ret = bch2_sb_from_fs(c, ca); 16191c6fdbd8SKent Overstreet if (ret) 16201c6fdbd8SKent Overstreet goto err_unlock; 16211c6fdbd8SKent Overstreet 16221c6fdbd8SKent Overstreet mi = bch2_sb_get_members(ca->disk_sb.sb); 16231c6fdbd8SKent Overstreet 16241c6fdbd8SKent Overstreet if (!bch2_sb_resize_members(&ca->disk_sb, 16251c6fdbd8SKent Overstreet le32_to_cpu(mi->field.u64s) + 16261c6fdbd8SKent Overstreet sizeof(dev_mi) / sizeof(u64))) { 16271c6fdbd8SKent Overstreet ret = -ENOSPC; 16281c6fdbd8SKent Overstreet goto err_unlock; 16291c6fdbd8SKent Overstreet } 16301c6fdbd8SKent Overstreet 16311c6fdbd8SKent Overstreet if (dynamic_fault("bcachefs:add:no_slot")) 16321c6fdbd8SKent Overstreet goto no_slot; 16331c6fdbd8SKent Overstreet 16341c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 16351c6fdbd8SKent Overstreet for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++) 16361c6fdbd8SKent Overstreet if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx)) 16371c6fdbd8SKent Overstreet goto have_slot; 16381c6fdbd8SKent Overstreet no_slot: 16391c6fdbd8SKent Overstreet err = "no slots available in superblock"; 16401c6fdbd8SKent Overstreet ret = -ENOSPC; 16411c6fdbd8SKent Overstreet goto err_unlock; 16421c6fdbd8SKent Overstreet 16431c6fdbd8SKent Overstreet have_slot: 16441c6fdbd8SKent Overstreet nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices); 16451c6fdbd8SKent Overstreet u64s = (sizeof(struct bch_sb_field_members) + 16461c6fdbd8SKent Overstreet sizeof(struct bch_member) * nr_devices) / sizeof(u64); 16471c6fdbd8SKent Overstreet 16481c6fdbd8SKent Overstreet err = "no space in superblock for member info"; 16491c6fdbd8SKent Overstreet ret = -ENOSPC; 16501c6fdbd8SKent Overstreet 16511c6fdbd8SKent Overstreet mi = bch2_sb_resize_members(&c->disk_sb, u64s); 16521c6fdbd8SKent Overstreet if (!mi) 16531c6fdbd8SKent Overstreet goto err_unlock; 16541c6fdbd8SKent Overstreet 16551c6fdbd8SKent Overstreet /* success: */ 16561c6fdbd8SKent Overstreet 16571c6fdbd8SKent Overstreet mi->members[dev_idx] = dev_mi; 1658a420eea6STim Schlueter mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds()); 16591c6fdbd8SKent Overstreet c->disk_sb.sb->nr_devices = nr_devices; 16601c6fdbd8SKent Overstreet 16611c6fdbd8SKent Overstreet ca->disk_sb.sb->dev_idx = dev_idx; 16621c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 16631c6fdbd8SKent Overstreet 16649ca53b55SKent Overstreet bch2_mark_dev_superblock(c, ca, 0); 16656eac2c2eSKent Overstreet 16661c6fdbd8SKent Overstreet bch2_write_super(c); 16671c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 16681c6fdbd8SKent Overstreet 16691c6fdbd8SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_RW) { 16701c6fdbd8SKent Overstreet err = __bch2_dev_read_write(c, ca); 16711c6fdbd8SKent Overstreet if (err) 16721c6fdbd8SKent Overstreet goto err_late; 16731c6fdbd8SKent Overstreet } 16741c6fdbd8SKent Overstreet 16751ada1606SKent Overstreet up_write(&c->state_lock); 16761c6fdbd8SKent Overstreet return 0; 16771c6fdbd8SKent Overstreet 16781c6fdbd8SKent Overstreet err_unlock: 16791c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 16801ada1606SKent Overstreet up_write(&c->state_lock); 16811c6fdbd8SKent Overstreet err: 16821c6fdbd8SKent Overstreet if (ca) 16831c6fdbd8SKent Overstreet bch2_dev_free(ca); 16841c6fdbd8SKent Overstreet bch2_free_super(&sb); 16851c6fdbd8SKent Overstreet bch_err(c, "Unable to add device: %s", err); 16861c6fdbd8SKent Overstreet return ret; 16871c6fdbd8SKent Overstreet err_late: 16881c6fdbd8SKent Overstreet bch_err(c, "Error going rw after adding device: %s", err); 16891c6fdbd8SKent Overstreet return -EINVAL; 16901c6fdbd8SKent Overstreet } 16911c6fdbd8SKent Overstreet 16921c6fdbd8SKent Overstreet /* Hot add existing device to running filesystem: */ 16931c6fdbd8SKent Overstreet int bch2_dev_online(struct bch_fs *c, const char *path) 16941c6fdbd8SKent Overstreet { 16951c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 16961c6fdbd8SKent Overstreet struct bch_sb_handle sb = { NULL }; 16971c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 16981c6fdbd8SKent Overstreet struct bch_dev *ca; 16991c6fdbd8SKent Overstreet unsigned dev_idx; 17001c6fdbd8SKent Overstreet const char *err; 17011c6fdbd8SKent Overstreet int ret; 17021c6fdbd8SKent Overstreet 17031ada1606SKent Overstreet down_write(&c->state_lock); 17041c6fdbd8SKent Overstreet 17051c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 17061c6fdbd8SKent Overstreet if (ret) { 17071ada1606SKent Overstreet up_write(&c->state_lock); 17081c6fdbd8SKent Overstreet return ret; 17091c6fdbd8SKent Overstreet } 17101c6fdbd8SKent Overstreet 17111c6fdbd8SKent Overstreet dev_idx = sb.sb->dev_idx; 17121c6fdbd8SKent Overstreet 17131c6fdbd8SKent Overstreet err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb); 17141c6fdbd8SKent Overstreet if (err) 17151c6fdbd8SKent Overstreet goto err; 17161c6fdbd8SKent Overstreet 17171c6fdbd8SKent Overstreet if (bch2_dev_attach_bdev(c, &sb)) { 17181c6fdbd8SKent Overstreet err = "bch2_dev_attach_bdev() error"; 17191c6fdbd8SKent Overstreet goto err; 17201c6fdbd8SKent Overstreet } 17211c6fdbd8SKent Overstreet 17221c6fdbd8SKent Overstreet ca = bch_dev_locked(c, dev_idx); 17231c6fdbd8SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_RW) { 17241c6fdbd8SKent Overstreet err = __bch2_dev_read_write(c, ca); 17251c6fdbd8SKent Overstreet if (err) 17261c6fdbd8SKent Overstreet goto err; 17271c6fdbd8SKent Overstreet } 17281c6fdbd8SKent Overstreet 17291c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 17301c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 17311c6fdbd8SKent Overstreet 17321c6fdbd8SKent Overstreet mi->members[ca->dev_idx].last_mount = 1733a420eea6STim Schlueter cpu_to_le64(ktime_get_real_seconds()); 17341c6fdbd8SKent Overstreet 17351c6fdbd8SKent Overstreet bch2_write_super(c); 17361c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17371c6fdbd8SKent Overstreet 17381ada1606SKent Overstreet up_write(&c->state_lock); 17391c6fdbd8SKent Overstreet return 0; 17401c6fdbd8SKent Overstreet err: 17411ada1606SKent Overstreet up_write(&c->state_lock); 17421c6fdbd8SKent Overstreet bch2_free_super(&sb); 17431c6fdbd8SKent Overstreet bch_err(c, "error bringing %s online: %s", path, err); 17441c6fdbd8SKent Overstreet return -EINVAL; 17451c6fdbd8SKent Overstreet } 17461c6fdbd8SKent Overstreet 17471c6fdbd8SKent Overstreet int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) 17481c6fdbd8SKent Overstreet { 17491ada1606SKent Overstreet down_write(&c->state_lock); 17501c6fdbd8SKent Overstreet 17511c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca)) { 17521c6fdbd8SKent Overstreet bch_err(ca, "Already offline"); 17531ada1606SKent Overstreet up_write(&c->state_lock); 17541c6fdbd8SKent Overstreet return 0; 17551c6fdbd8SKent Overstreet } 17561c6fdbd8SKent Overstreet 17571c6fdbd8SKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) { 17581c6fdbd8SKent Overstreet bch_err(ca, "Cannot offline required disk"); 17591ada1606SKent Overstreet up_write(&c->state_lock); 17601c6fdbd8SKent Overstreet return -EINVAL; 17611c6fdbd8SKent Overstreet } 17621c6fdbd8SKent Overstreet 17631c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 17641c6fdbd8SKent Overstreet 17651ada1606SKent Overstreet up_write(&c->state_lock); 17661c6fdbd8SKent Overstreet return 0; 17671c6fdbd8SKent Overstreet } 17681c6fdbd8SKent Overstreet 17691c6fdbd8SKent Overstreet int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) 17701c6fdbd8SKent Overstreet { 17711c6fdbd8SKent Overstreet struct bch_member *mi; 17721c6fdbd8SKent Overstreet int ret = 0; 17731c6fdbd8SKent Overstreet 17741ada1606SKent Overstreet down_write(&c->state_lock); 17751c6fdbd8SKent Overstreet 17761c6fdbd8SKent Overstreet if (nbuckets < ca->mi.nbuckets) { 17771c6fdbd8SKent Overstreet bch_err(ca, "Cannot shrink yet"); 17781c6fdbd8SKent Overstreet ret = -EINVAL; 17791c6fdbd8SKent Overstreet goto err; 17801c6fdbd8SKent Overstreet } 17811c6fdbd8SKent Overstreet 17821c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca) && 17831c6fdbd8SKent Overstreet get_capacity(ca->disk_sb.bdev->bd_disk) < 17841c6fdbd8SKent Overstreet ca->mi.bucket_size * nbuckets) { 17851c6fdbd8SKent Overstreet bch_err(ca, "New size larger than device"); 17861c6fdbd8SKent Overstreet ret = -EINVAL; 17871c6fdbd8SKent Overstreet goto err; 17881c6fdbd8SKent Overstreet } 17891c6fdbd8SKent Overstreet 17901c6fdbd8SKent Overstreet ret = bch2_dev_buckets_resize(c, ca, nbuckets); 17911c6fdbd8SKent Overstreet if (ret) { 17921c6fdbd8SKent Overstreet bch_err(ca, "Resize error: %i", ret); 17931c6fdbd8SKent Overstreet goto err; 17941c6fdbd8SKent Overstreet } 17951c6fdbd8SKent Overstreet 17961c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 17971c6fdbd8SKent Overstreet mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx]; 17981c6fdbd8SKent Overstreet mi->nbuckets = cpu_to_le64(nbuckets); 17991c6fdbd8SKent Overstreet 18001c6fdbd8SKent Overstreet bch2_write_super(c); 18011c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 18021c6fdbd8SKent Overstreet 18031c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 18041c6fdbd8SKent Overstreet err: 18051ada1606SKent Overstreet up_write(&c->state_lock); 18061c6fdbd8SKent Overstreet return ret; 18071c6fdbd8SKent Overstreet } 18081c6fdbd8SKent Overstreet 18091c6fdbd8SKent Overstreet /* return with ref on ca->ref: */ 18101c6fdbd8SKent Overstreet struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path) 18111c6fdbd8SKent Overstreet { 18121c6fdbd8SKent Overstreet struct bch_dev *ca; 18131c6fdbd8SKent Overstreet dev_t dev; 18141c6fdbd8SKent Overstreet unsigned i; 18151c6fdbd8SKent Overstreet int ret; 18161c6fdbd8SKent Overstreet 18171c6fdbd8SKent Overstreet ret = lookup_bdev(path, &dev); 18181c6fdbd8SKent Overstreet if (ret) 18191c6fdbd8SKent Overstreet return ERR_PTR(ret); 18201c6fdbd8SKent Overstreet 18211c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 18221c6fdbd8SKent Overstreet if (ca->disk_sb.bdev->bd_dev == dev) 18231c6fdbd8SKent Overstreet goto found; 18241c6fdbd8SKent Overstreet 18251c6fdbd8SKent Overstreet ca = ERR_PTR(-ENOENT); 18261c6fdbd8SKent Overstreet found: 18271c6fdbd8SKent Overstreet return ca; 18281c6fdbd8SKent Overstreet } 18291c6fdbd8SKent Overstreet 18301c6fdbd8SKent Overstreet /* Filesystem open: */ 18311c6fdbd8SKent Overstreet 18321c6fdbd8SKent Overstreet struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, 18331c6fdbd8SKent Overstreet struct bch_opts opts) 18341c6fdbd8SKent Overstreet { 18351c6fdbd8SKent Overstreet struct bch_sb_handle *sb = NULL; 18361c6fdbd8SKent Overstreet struct bch_fs *c = NULL; 1837625104eaSKent Overstreet struct bch_sb_field_members *mi; 18381c6fdbd8SKent Overstreet unsigned i, best_sb = 0; 18391c6fdbd8SKent Overstreet const char *err; 18401c6fdbd8SKent Overstreet int ret = -ENOMEM; 18411c6fdbd8SKent Overstreet 18421c6fdbd8SKent Overstreet pr_verbose_init(opts, ""); 18431c6fdbd8SKent Overstreet 18441c6fdbd8SKent Overstreet if (!nr_devices) { 18451c6fdbd8SKent Overstreet c = ERR_PTR(-EINVAL); 18461c6fdbd8SKent Overstreet goto out2; 18471c6fdbd8SKent Overstreet } 18481c6fdbd8SKent Overstreet 18491c6fdbd8SKent Overstreet if (!try_module_get(THIS_MODULE)) { 18501c6fdbd8SKent Overstreet c = ERR_PTR(-ENODEV); 18511c6fdbd8SKent Overstreet goto out2; 18521c6fdbd8SKent Overstreet } 18531c6fdbd8SKent Overstreet 18541c6fdbd8SKent Overstreet sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL); 18551c6fdbd8SKent Overstreet if (!sb) 18561c6fdbd8SKent Overstreet goto err; 18571c6fdbd8SKent Overstreet 18581c6fdbd8SKent Overstreet for (i = 0; i < nr_devices; i++) { 18591c6fdbd8SKent Overstreet ret = bch2_read_super(devices[i], &opts, &sb[i]); 18601c6fdbd8SKent Overstreet if (ret) 18611c6fdbd8SKent Overstreet goto err; 18621c6fdbd8SKent Overstreet 18631c6fdbd8SKent Overstreet err = bch2_sb_validate(&sb[i]); 18641c6fdbd8SKent Overstreet if (err) 18651c6fdbd8SKent Overstreet goto err_print; 18661c6fdbd8SKent Overstreet } 18671c6fdbd8SKent Overstreet 18681c6fdbd8SKent Overstreet for (i = 1; i < nr_devices; i++) 18691c6fdbd8SKent Overstreet if (le64_to_cpu(sb[i].sb->seq) > 18701c6fdbd8SKent Overstreet le64_to_cpu(sb[best_sb].sb->seq)) 18711c6fdbd8SKent Overstreet best_sb = i; 18721c6fdbd8SKent Overstreet 1873625104eaSKent Overstreet mi = bch2_sb_get_members(sb[best_sb].sb); 1874625104eaSKent Overstreet 1875625104eaSKent Overstreet i = 0; 1876625104eaSKent Overstreet while (i < nr_devices) { 1877625104eaSKent Overstreet if (i != best_sb && 1878625104eaSKent Overstreet !bch2_dev_exists(sb[best_sb].sb, mi, sb[i].sb->dev_idx)) { 1879625104eaSKent Overstreet pr_info("%pg has been removed, skipping", sb[i].bdev); 1880625104eaSKent Overstreet bch2_free_super(&sb[i]); 1881625104eaSKent Overstreet array_remove_item(sb, nr_devices, i); 1882625104eaSKent Overstreet continue; 1883625104eaSKent Overstreet } 1884625104eaSKent Overstreet 18851c6fdbd8SKent Overstreet err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb); 18861c6fdbd8SKent Overstreet if (err) 18871c6fdbd8SKent Overstreet goto err_print; 1888625104eaSKent Overstreet i++; 18891c6fdbd8SKent Overstreet } 18901c6fdbd8SKent Overstreet 18911c6fdbd8SKent Overstreet ret = -ENOMEM; 18921c6fdbd8SKent Overstreet c = bch2_fs_alloc(sb[best_sb].sb, opts); 18931c6fdbd8SKent Overstreet if (!c) 18941c6fdbd8SKent Overstreet goto err; 18951c6fdbd8SKent Overstreet 18961c6fdbd8SKent Overstreet err = "bch2_dev_online() error"; 18971ada1606SKent Overstreet down_write(&c->state_lock); 18981c6fdbd8SKent Overstreet for (i = 0; i < nr_devices; i++) 18991c6fdbd8SKent Overstreet if (bch2_dev_attach_bdev(c, &sb[i])) { 19001ada1606SKent Overstreet up_write(&c->state_lock); 19011c6fdbd8SKent Overstreet goto err_print; 19021c6fdbd8SKent Overstreet } 19031ada1606SKent Overstreet up_write(&c->state_lock); 19041c6fdbd8SKent Overstreet 19051c6fdbd8SKent Overstreet err = "insufficient devices"; 19061c6fdbd8SKent Overstreet if (!bch2_fs_may_start(c)) 19071c6fdbd8SKent Overstreet goto err_print; 19081c6fdbd8SKent Overstreet 19091c6fdbd8SKent Overstreet if (!c->opts.nostart) { 1910619f5beeSKent Overstreet ret = bch2_fs_start(c); 1911619f5beeSKent Overstreet if (ret) 1912619f5beeSKent Overstreet goto err; 19131c6fdbd8SKent Overstreet } 19141c6fdbd8SKent Overstreet out: 19151c6fdbd8SKent Overstreet kfree(sb); 19161c6fdbd8SKent Overstreet module_put(THIS_MODULE); 19171c6fdbd8SKent Overstreet out2: 19181c6fdbd8SKent Overstreet pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c)); 19191c6fdbd8SKent Overstreet return c; 19201c6fdbd8SKent Overstreet err_print: 19211c6fdbd8SKent Overstreet pr_err("bch_fs_open err opening %s: %s", 19221c6fdbd8SKent Overstreet devices[0], err); 19231c6fdbd8SKent Overstreet ret = -EINVAL; 19241c6fdbd8SKent Overstreet err: 19251c6fdbd8SKent Overstreet if (c) 19261c6fdbd8SKent Overstreet bch2_fs_stop(c); 19271c6fdbd8SKent Overstreet for (i = 0; i < nr_devices; i++) 19281c6fdbd8SKent Overstreet bch2_free_super(&sb[i]); 19291c6fdbd8SKent Overstreet c = ERR_PTR(ret); 19301c6fdbd8SKent Overstreet goto out; 19311c6fdbd8SKent Overstreet } 19321c6fdbd8SKent Overstreet 19331c6fdbd8SKent Overstreet static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb, 19341c6fdbd8SKent Overstreet struct bch_opts opts) 19351c6fdbd8SKent Overstreet { 19361c6fdbd8SKent Overstreet const char *err; 19371c6fdbd8SKent Overstreet struct bch_fs *c; 19381c6fdbd8SKent Overstreet bool allocated_fs = false; 1939619f5beeSKent Overstreet int ret; 19401c6fdbd8SKent Overstreet 19411c6fdbd8SKent Overstreet err = bch2_sb_validate(sb); 19421c6fdbd8SKent Overstreet if (err) 19431c6fdbd8SKent Overstreet return err; 19441c6fdbd8SKent Overstreet 19451c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 19461c6fdbd8SKent Overstreet c = __bch2_uuid_to_fs(sb->sb->uuid); 19471c6fdbd8SKent Overstreet if (c) { 19481c6fdbd8SKent Overstreet closure_get(&c->cl); 19491c6fdbd8SKent Overstreet 19501c6fdbd8SKent Overstreet err = bch2_dev_in_fs(c->disk_sb.sb, sb->sb); 19511c6fdbd8SKent Overstreet if (err) 19521c6fdbd8SKent Overstreet goto err; 19531c6fdbd8SKent Overstreet } else { 19541c6fdbd8SKent Overstreet c = bch2_fs_alloc(sb->sb, opts); 19551c6fdbd8SKent Overstreet err = "cannot allocate memory"; 19561c6fdbd8SKent Overstreet if (!c) 19571c6fdbd8SKent Overstreet goto err; 19581c6fdbd8SKent Overstreet 19591c6fdbd8SKent Overstreet allocated_fs = true; 19601c6fdbd8SKent Overstreet } 19611c6fdbd8SKent Overstreet 19621c6fdbd8SKent Overstreet err = "bch2_dev_online() error"; 19631c6fdbd8SKent Overstreet 19641c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 19651c6fdbd8SKent Overstreet if (bch2_dev_attach_bdev(c, sb)) { 19661c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 19671c6fdbd8SKent Overstreet goto err; 19681c6fdbd8SKent Overstreet } 19691c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 19701c6fdbd8SKent Overstreet 19711c6fdbd8SKent Overstreet if (!c->opts.nostart && bch2_fs_may_start(c)) { 1972619f5beeSKent Overstreet err = "error starting filesystem"; 1973619f5beeSKent Overstreet ret = bch2_fs_start(c); 1974619f5beeSKent Overstreet if (ret) 19751c6fdbd8SKent Overstreet goto err; 19761c6fdbd8SKent Overstreet } 19771c6fdbd8SKent Overstreet 19781c6fdbd8SKent Overstreet closure_put(&c->cl); 19791c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 19801c6fdbd8SKent Overstreet 19811c6fdbd8SKent Overstreet return NULL; 19821c6fdbd8SKent Overstreet err: 19831c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 19841c6fdbd8SKent Overstreet 19851c6fdbd8SKent Overstreet if (allocated_fs) 19861c6fdbd8SKent Overstreet bch2_fs_stop(c); 19871c6fdbd8SKent Overstreet else if (c) 19881c6fdbd8SKent Overstreet closure_put(&c->cl); 19891c6fdbd8SKent Overstreet 19901c6fdbd8SKent Overstreet return err; 19911c6fdbd8SKent Overstreet } 19921c6fdbd8SKent Overstreet 19931c6fdbd8SKent Overstreet const char *bch2_fs_open_incremental(const char *path) 19941c6fdbd8SKent Overstreet { 19951c6fdbd8SKent Overstreet struct bch_sb_handle sb; 19961c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 19971c6fdbd8SKent Overstreet const char *err; 19981c6fdbd8SKent Overstreet 19991c6fdbd8SKent Overstreet if (bch2_read_super(path, &opts, &sb)) 20001c6fdbd8SKent Overstreet return "error reading superblock"; 20011c6fdbd8SKent Overstreet 20021c6fdbd8SKent Overstreet err = __bch2_fs_open_incremental(&sb, opts); 20031c6fdbd8SKent Overstreet bch2_free_super(&sb); 20041c6fdbd8SKent Overstreet 20051c6fdbd8SKent Overstreet return err; 20061c6fdbd8SKent Overstreet } 20071c6fdbd8SKent Overstreet 20081c6fdbd8SKent Overstreet /* Global interfaces/init */ 20091c6fdbd8SKent Overstreet 20101c6fdbd8SKent Overstreet static void bcachefs_exit(void) 20111c6fdbd8SKent Overstreet { 20121c6fdbd8SKent Overstreet bch2_debug_exit(); 20131c6fdbd8SKent Overstreet bch2_vfs_exit(); 20141c6fdbd8SKent Overstreet bch2_chardev_exit(); 20151c6fdbd8SKent Overstreet if (bcachefs_kset) 20161c6fdbd8SKent Overstreet kset_unregister(bcachefs_kset); 20171c6fdbd8SKent Overstreet } 20181c6fdbd8SKent Overstreet 20191c6fdbd8SKent Overstreet static int __init bcachefs_init(void) 20201c6fdbd8SKent Overstreet { 20211c6fdbd8SKent Overstreet bch2_bkey_pack_test(); 20221c6fdbd8SKent Overstreet bch2_inode_pack_test(); 20231c6fdbd8SKent Overstreet 20241c6fdbd8SKent Overstreet if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) || 20251c6fdbd8SKent Overstreet bch2_chardev_init() || 20261c6fdbd8SKent Overstreet bch2_vfs_init() || 20271c6fdbd8SKent Overstreet bch2_debug_init()) 20281c6fdbd8SKent Overstreet goto err; 20291c6fdbd8SKent Overstreet 20301c6fdbd8SKent Overstreet return 0; 20311c6fdbd8SKent Overstreet err: 20321c6fdbd8SKent Overstreet bcachefs_exit(); 20331c6fdbd8SKent Overstreet return -ENOMEM; 20341c6fdbd8SKent Overstreet } 20351c6fdbd8SKent Overstreet 20361c6fdbd8SKent Overstreet #define BCH_DEBUG_PARAM(name, description) \ 20371c6fdbd8SKent Overstreet bool bch2_##name; \ 20381c6fdbd8SKent Overstreet module_param_named(name, bch2_##name, bool, 0644); \ 20391c6fdbd8SKent Overstreet MODULE_PARM_DESC(name, description); 20401c6fdbd8SKent Overstreet BCH_DEBUG_PARAMS() 20411c6fdbd8SKent Overstreet #undef BCH_DEBUG_PARAM 20421c6fdbd8SKent Overstreet 204326609b61SKent Overstreet unsigned bch2_metadata_version = bcachefs_metadata_version_current; 20441c6fdbd8SKent Overstreet module_param_named(version, bch2_metadata_version, uint, 0400); 20451c6fdbd8SKent Overstreet 20461c6fdbd8SKent Overstreet module_exit(bcachefs_exit); 20471c6fdbd8SKent Overstreet module_init(bcachefs_init); 2048