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" 1921aec962SKent Overstreet #include "buckets_waiting_for_journal.h" 201c6fdbd8SKent Overstreet #include "chardev.h" 211c6fdbd8SKent Overstreet #include "checksum.h" 221c6fdbd8SKent Overstreet #include "clock.h" 231c6fdbd8SKent Overstreet #include "compress.h" 241c6fdbd8SKent Overstreet #include "debug.h" 251c6fdbd8SKent Overstreet #include "disk_groups.h" 26cd575ddfSKent Overstreet #include "ec.h" 271c6fdbd8SKent Overstreet #include "error.h" 281c6fdbd8SKent Overstreet #include "fs.h" 291c6fdbd8SKent Overstreet #include "fs-io.h" 301c6fdbd8SKent Overstreet #include "fsck.h" 311c6fdbd8SKent Overstreet #include "inode.h" 321c6fdbd8SKent Overstreet #include "io.h" 331c6fdbd8SKent Overstreet #include "journal.h" 341c6fdbd8SKent Overstreet #include "journal_reclaim.h" 351dd7f9d9SKent Overstreet #include "journal_seq_blacklist.h" 361c6fdbd8SKent Overstreet #include "move.h" 371c6fdbd8SKent Overstreet #include "migrate.h" 381c6fdbd8SKent Overstreet #include "movinggc.h" 391c6fdbd8SKent Overstreet #include "quota.h" 401c6fdbd8SKent Overstreet #include "rebalance.h" 411c6fdbd8SKent Overstreet #include "recovery.h" 421c6fdbd8SKent Overstreet #include "replicas.h" 4314b393eeSKent Overstreet #include "subvolume.h" 441c6fdbd8SKent Overstreet #include "super.h" 451c6fdbd8SKent Overstreet #include "super-io.h" 461c6fdbd8SKent Overstreet #include "sysfs.h" 471c6fdbd8SKent Overstreet #include "trace.h" 481c6fdbd8SKent Overstreet 491c6fdbd8SKent Overstreet #include <linux/backing-dev.h> 501c6fdbd8SKent Overstreet #include <linux/blkdev.h> 511c6fdbd8SKent Overstreet #include <linux/debugfs.h> 521c6fdbd8SKent Overstreet #include <linux/device.h> 531c6fdbd8SKent Overstreet #include <linux/idr.h> 541c6fdbd8SKent Overstreet #include <linux/module.h> 551c6fdbd8SKent Overstreet #include <linux/percpu.h> 561c6fdbd8SKent Overstreet #include <linux/random.h> 571c6fdbd8SKent Overstreet #include <linux/sysfs.h> 581c6fdbd8SKent Overstreet #include <crypto/hash.h> 591c6fdbd8SKent Overstreet 601c6fdbd8SKent Overstreet MODULE_LICENSE("GPL"); 611c6fdbd8SKent Overstreet MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>"); 621c6fdbd8SKent Overstreet 631c6fdbd8SKent Overstreet #define KTYPE(type) \ 641c6fdbd8SKent Overstreet static const struct attribute_group type ## _group = { \ 651c6fdbd8SKent Overstreet .attrs = type ## _files \ 661c6fdbd8SKent Overstreet }; \ 671c6fdbd8SKent Overstreet \ 681c6fdbd8SKent Overstreet static const struct attribute_group *type ## _groups[] = { \ 691c6fdbd8SKent Overstreet &type ## _group, \ 701c6fdbd8SKent Overstreet NULL \ 711c6fdbd8SKent Overstreet }; \ 721c6fdbd8SKent Overstreet \ 731c6fdbd8SKent Overstreet static const struct kobj_type type ## _ktype = { \ 741c6fdbd8SKent Overstreet .release = type ## _release, \ 751c6fdbd8SKent Overstreet .sysfs_ops = &type ## _sysfs_ops, \ 761c6fdbd8SKent Overstreet .default_groups = type ## _groups \ 771c6fdbd8SKent Overstreet } 781c6fdbd8SKent Overstreet 791c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *); 801c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *); 811c6fdbd8SKent Overstreet 821c6fdbd8SKent Overstreet static void bch2_fs_internal_release(struct kobject *k) 831c6fdbd8SKent Overstreet { 841c6fdbd8SKent Overstreet } 851c6fdbd8SKent Overstreet 861c6fdbd8SKent Overstreet static void bch2_fs_opts_dir_release(struct kobject *k) 871c6fdbd8SKent Overstreet { 881c6fdbd8SKent Overstreet } 891c6fdbd8SKent Overstreet 901c6fdbd8SKent Overstreet static void bch2_fs_time_stats_release(struct kobject *k) 911c6fdbd8SKent Overstreet { 921c6fdbd8SKent Overstreet } 931c6fdbd8SKent Overstreet 941c6fdbd8SKent Overstreet KTYPE(bch2_fs); 951c6fdbd8SKent Overstreet KTYPE(bch2_fs_internal); 961c6fdbd8SKent Overstreet KTYPE(bch2_fs_opts_dir); 971c6fdbd8SKent Overstreet KTYPE(bch2_fs_time_stats); 981c6fdbd8SKent Overstreet KTYPE(bch2_dev); 991c6fdbd8SKent Overstreet 1001c6fdbd8SKent Overstreet static struct kset *bcachefs_kset; 1011c6fdbd8SKent Overstreet static LIST_HEAD(bch_fs_list); 1021c6fdbd8SKent Overstreet static DEFINE_MUTEX(bch_fs_list_lock); 1031c6fdbd8SKent Overstreet 1041c6fdbd8SKent Overstreet static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait); 1051c6fdbd8SKent Overstreet 1061c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *); 1071c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *, unsigned); 1081c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *); 1091c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *); 1101c6fdbd8SKent Overstreet 1111c6fdbd8SKent Overstreet struct bch_fs *bch2_dev_to_fs(dev_t dev) 1121c6fdbd8SKent Overstreet { 1131c6fdbd8SKent Overstreet struct bch_fs *c; 1141c6fdbd8SKent Overstreet struct bch_dev *ca; 1151c6fdbd8SKent Overstreet unsigned i; 1161c6fdbd8SKent Overstreet 1171c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 1181c6fdbd8SKent Overstreet rcu_read_lock(); 1191c6fdbd8SKent Overstreet 1201c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 1211c6fdbd8SKent Overstreet for_each_member_device_rcu(ca, c, i, NULL) 122ec4ab9d2SDan Robertson if (ca->disk_sb.bdev && ca->disk_sb.bdev->bd_dev == dev) { 1231c6fdbd8SKent Overstreet closure_get(&c->cl); 1241c6fdbd8SKent Overstreet goto found; 1251c6fdbd8SKent Overstreet } 1261c6fdbd8SKent Overstreet c = NULL; 1271c6fdbd8SKent Overstreet found: 1281c6fdbd8SKent Overstreet rcu_read_unlock(); 1291c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 1301c6fdbd8SKent Overstreet 1311c6fdbd8SKent Overstreet return c; 1321c6fdbd8SKent Overstreet } 1331c6fdbd8SKent Overstreet 1341c6fdbd8SKent Overstreet static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid) 1351c6fdbd8SKent Overstreet { 1361c6fdbd8SKent Overstreet struct bch_fs *c; 1371c6fdbd8SKent Overstreet 1381c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 1391c6fdbd8SKent Overstreet 1401c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 1411c6fdbd8SKent Overstreet if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid))) 1421c6fdbd8SKent Overstreet return c; 1431c6fdbd8SKent Overstreet 1441c6fdbd8SKent Overstreet return NULL; 1451c6fdbd8SKent Overstreet } 1461c6fdbd8SKent Overstreet 1471c6fdbd8SKent Overstreet struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid) 1481c6fdbd8SKent Overstreet { 1491c6fdbd8SKent Overstreet struct bch_fs *c; 1501c6fdbd8SKent Overstreet 1511c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 1521c6fdbd8SKent Overstreet c = __bch2_uuid_to_fs(uuid); 1531c6fdbd8SKent Overstreet if (c) 1541c6fdbd8SKent Overstreet closure_get(&c->cl); 1551c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 1561c6fdbd8SKent Overstreet 1571c6fdbd8SKent Overstreet return c; 1581c6fdbd8SKent Overstreet } 1591c6fdbd8SKent Overstreet 160180fb49dSKent Overstreet static void bch2_dev_usage_journal_reserve(struct bch_fs *c) 161180fb49dSKent Overstreet { 162180fb49dSKent Overstreet struct bch_dev *ca; 163180fb49dSKent Overstreet unsigned i, nr = 0, u64s = 1644b8f89afSKent Overstreet ((sizeof(struct jset_entry_dev_usage) + 1654b8f89afSKent Overstreet sizeof(struct jset_entry_dev_usage_type) * BCH_DATA_NR)) / 1664b8f89afSKent Overstreet sizeof(u64); 167180fb49dSKent Overstreet 168180fb49dSKent Overstreet rcu_read_lock(); 169180fb49dSKent Overstreet for_each_member_device_rcu(ca, c, i, NULL) 170180fb49dSKent Overstreet nr++; 171180fb49dSKent Overstreet rcu_read_unlock(); 172180fb49dSKent Overstreet 173180fb49dSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 174180fb49dSKent Overstreet &c->dev_usage_journal_res, u64s * nr); 175180fb49dSKent Overstreet } 176180fb49dSKent Overstreet 1771c6fdbd8SKent Overstreet /* Filesystem RO/RW: */ 1781c6fdbd8SKent Overstreet 1791c6fdbd8SKent Overstreet /* 1801c6fdbd8SKent Overstreet * For startup/shutdown of RW stuff, the dependencies are: 1811c6fdbd8SKent Overstreet * 1821c6fdbd8SKent Overstreet * - foreground writes depend on copygc and rebalance (to free up space) 1831c6fdbd8SKent Overstreet * 1841c6fdbd8SKent Overstreet * - copygc and rebalance depend on mark and sweep gc (they actually probably 1851c6fdbd8SKent Overstreet * don't because they either reserve ahead of time or don't block if 1861c6fdbd8SKent Overstreet * allocations fail, but allocations can require mark and sweep gc to run 1871c6fdbd8SKent Overstreet * because of generation number wraparound) 1881c6fdbd8SKent Overstreet * 1891c6fdbd8SKent Overstreet * - all of the above depends on the allocator threads 1901c6fdbd8SKent Overstreet * 1911c6fdbd8SKent Overstreet * - allocator depends on the journal (when it rewrites prios and gens) 1921c6fdbd8SKent Overstreet */ 1931c6fdbd8SKent Overstreet 1941c6fdbd8SKent Overstreet static void __bch2_fs_read_only(struct bch_fs *c) 1951c6fdbd8SKent Overstreet { 1961c6fdbd8SKent Overstreet struct bch_dev *ca; 197d5f70c1fSKent Overstreet unsigned i, clean_passes = 0; 1981c6fdbd8SKent Overstreet 1991c6fdbd8SKent Overstreet bch2_rebalance_stop(c); 200e6d11615SKent Overstreet bch2_copygc_stop(c); 2011c6fdbd8SKent Overstreet bch2_gc_thread_stop(c); 2021c6fdbd8SKent Overstreet 2031c6fdbd8SKent Overstreet /* 2041c6fdbd8SKent Overstreet * Flush journal before stopping allocators, because flushing journal 2051c6fdbd8SKent Overstreet * blacklist entries involves allocating new btree nodes: 2061c6fdbd8SKent Overstreet */ 2071c6fdbd8SKent Overstreet bch2_journal_flush_all_pins(&c->journal); 2081c6fdbd8SKent Overstreet 2092340fd9dSKent Overstreet /* 2102340fd9dSKent Overstreet * If the allocator threads didn't all start up, the btree updates to 2112340fd9dSKent Overstreet * write out alloc info aren't going to work: 2122340fd9dSKent Overstreet */ 213b935a8a6SKent Overstreet if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags)) 2142340fd9dSKent Overstreet goto nowrote_alloc; 215b935a8a6SKent Overstreet 216039fc4c5SKent Overstreet bch_verbose(c, "flushing journal and stopping allocators"); 2171c6fdbd8SKent Overstreet 2181c6fdbd8SKent Overstreet bch2_journal_flush_all_pins(&c->journal); 219039fc4c5SKent Overstreet set_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags); 220039fc4c5SKent Overstreet 221039fc4c5SKent Overstreet do { 222039fc4c5SKent Overstreet clean_passes++; 223039fc4c5SKent Overstreet 224039fc4c5SKent Overstreet if (bch2_journal_flush_all_pins(&c->journal)) 225039fc4c5SKent Overstreet clean_passes = 0; 2261c6fdbd8SKent Overstreet 2271c6fdbd8SKent Overstreet /* 228039fc4c5SKent Overstreet * In flight interior btree updates will generate more journal 229039fc4c5SKent Overstreet * updates and btree updates (alloc btree): 2301c6fdbd8SKent Overstreet */ 231039fc4c5SKent Overstreet if (bch2_btree_interior_updates_nr_pending(c)) { 2321c6fdbd8SKent Overstreet closure_wait_event(&c->btree_interior_update_wait, 2331c6fdbd8SKent Overstreet !bch2_btree_interior_updates_nr_pending(c)); 234039fc4c5SKent Overstreet clean_passes = 0; 235039fc4c5SKent Overstreet } 23600b8ccf7SKent Overstreet flush_work(&c->btree_interior_update_work); 237d5f70c1fSKent Overstreet 238039fc4c5SKent Overstreet if (bch2_journal_flush_all_pins(&c->journal)) 239039fc4c5SKent Overstreet clean_passes = 0; 240d5f70c1fSKent Overstreet } while (clean_passes < 2); 241039fc4c5SKent Overstreet bch_verbose(c, "flushing journal and stopping allocators complete"); 2422340fd9dSKent Overstreet 2432340fd9dSKent Overstreet set_bit(BCH_FS_ALLOC_CLEAN, &c->flags); 2442340fd9dSKent Overstreet nowrote_alloc: 24500b8ccf7SKent Overstreet closure_wait_event(&c->btree_interior_update_wait, 24600b8ccf7SKent Overstreet !bch2_btree_interior_updates_nr_pending(c)); 24700b8ccf7SKent Overstreet flush_work(&c->btree_interior_update_work); 24800b8ccf7SKent Overstreet 249430735cdSKent Overstreet for_each_member_device(ca, c, i) 250430735cdSKent Overstreet bch2_dev_allocator_stop(ca); 2511c6fdbd8SKent Overstreet 252b935a8a6SKent Overstreet clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags); 253039fc4c5SKent Overstreet clear_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags); 254b935a8a6SKent Overstreet 2551c6fdbd8SKent Overstreet bch2_fs_journal_stop(&c->journal); 2561c6fdbd8SKent Overstreet 2571c6fdbd8SKent Overstreet /* 2581c6fdbd8SKent Overstreet * the journal kicks off btree writes via reclaim - wait for in flight 2591c6fdbd8SKent Overstreet * writes after stopping journal: 2601c6fdbd8SKent Overstreet */ 2611c6fdbd8SKent Overstreet bch2_btree_flush_all_writes(c); 2621c6fdbd8SKent Overstreet 2631c6fdbd8SKent Overstreet /* 2641c6fdbd8SKent Overstreet * After stopping journal: 2651c6fdbd8SKent Overstreet */ 2661c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 2671c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 2681c6fdbd8SKent Overstreet } 2691c6fdbd8SKent Overstreet 2701c6fdbd8SKent Overstreet static void bch2_writes_disabled(struct percpu_ref *writes) 2711c6fdbd8SKent Overstreet { 2721c6fdbd8SKent Overstreet struct bch_fs *c = container_of(writes, struct bch_fs, writes); 2731c6fdbd8SKent Overstreet 2741c6fdbd8SKent Overstreet set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags); 2751c6fdbd8SKent Overstreet wake_up(&bch_read_only_wait); 2761c6fdbd8SKent Overstreet } 2771c6fdbd8SKent Overstreet 2781c6fdbd8SKent Overstreet void bch2_fs_read_only(struct bch_fs *c) 2791c6fdbd8SKent Overstreet { 280134915f3SKent Overstreet if (!test_bit(BCH_FS_RW, &c->flags)) { 2819ae28f82SKent Overstreet bch2_journal_reclaim_stop(&c->journal); 2821c6fdbd8SKent Overstreet return; 283134915f3SKent Overstreet } 2841c6fdbd8SKent Overstreet 2851c6fdbd8SKent Overstreet BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags)); 2861c6fdbd8SKent Overstreet 2871c6fdbd8SKent Overstreet /* 2881c6fdbd8SKent Overstreet * Block new foreground-end write operations from starting - any new 2891c6fdbd8SKent Overstreet * writes will return -EROFS: 2901c6fdbd8SKent Overstreet * 2911c6fdbd8SKent Overstreet * (This is really blocking new _allocations_, writes to previously 2921c6fdbd8SKent Overstreet * allocated space can still happen until stopping the allocator in 2931c6fdbd8SKent Overstreet * bch2_dev_allocator_stop()). 2941c6fdbd8SKent Overstreet */ 2951c6fdbd8SKent Overstreet percpu_ref_kill(&c->writes); 2961c6fdbd8SKent Overstreet 297f516c872SKent Overstreet cancel_work_sync(&c->ec_stripe_delete_work); 2981c6fdbd8SKent Overstreet 2991c6fdbd8SKent Overstreet /* 3001c6fdbd8SKent Overstreet * If we're not doing an emergency shutdown, we want to wait on 3011c6fdbd8SKent Overstreet * outstanding writes to complete so they don't see spurious errors due 3021c6fdbd8SKent Overstreet * to shutting down the allocator: 3031c6fdbd8SKent Overstreet * 3041c6fdbd8SKent Overstreet * If we are doing an emergency shutdown outstanding writes may 3051c6fdbd8SKent Overstreet * hang until we shutdown the allocator so we don't want to wait 3061c6fdbd8SKent Overstreet * on outstanding writes before shutting everything down - but 3071c6fdbd8SKent Overstreet * we do need to wait on them before returning and signalling 3081c6fdbd8SKent Overstreet * that going RO is complete: 3091c6fdbd8SKent Overstreet */ 3101c6fdbd8SKent Overstreet wait_event(bch_read_only_wait, 3111c6fdbd8SKent Overstreet test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) || 3121c6fdbd8SKent Overstreet test_bit(BCH_FS_EMERGENCY_RO, &c->flags)); 3131c6fdbd8SKent Overstreet 3141c6fdbd8SKent Overstreet __bch2_fs_read_only(c); 3151c6fdbd8SKent Overstreet 3161c6fdbd8SKent Overstreet wait_event(bch_read_only_wait, 3171c6fdbd8SKent Overstreet test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags)); 3181c6fdbd8SKent Overstreet 3191c6fdbd8SKent Overstreet clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags); 3201c6fdbd8SKent Overstreet 3211c6fdbd8SKent Overstreet if (!bch2_journal_error(&c->journal) && 3221c6fdbd8SKent Overstreet !test_bit(BCH_FS_ERROR, &c->flags) && 3233aea4342SKent Overstreet !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) && 324a0e0bda1SKent Overstreet test_bit(BCH_FS_STARTED, &c->flags) && 3252340fd9dSKent Overstreet test_bit(BCH_FS_ALLOC_CLEAN, &c->flags) && 326b2930396SKent Overstreet !c->opts.norecovery) { 327b2930396SKent Overstreet bch_verbose(c, "marking filesystem clean"); 328134915f3SKent Overstreet bch2_fs_mark_clean(c); 329b2930396SKent Overstreet } 3301c6fdbd8SKent Overstreet 331134915f3SKent Overstreet clear_bit(BCH_FS_RW, &c->flags); 3321c6fdbd8SKent Overstreet } 3331c6fdbd8SKent Overstreet 3341c6fdbd8SKent Overstreet static void bch2_fs_read_only_work(struct work_struct *work) 3351c6fdbd8SKent Overstreet { 3361c6fdbd8SKent Overstreet struct bch_fs *c = 3371c6fdbd8SKent Overstreet container_of(work, struct bch_fs, read_only_work); 3381c6fdbd8SKent Overstreet 3391ada1606SKent Overstreet down_write(&c->state_lock); 3401c6fdbd8SKent Overstreet bch2_fs_read_only(c); 3411ada1606SKent Overstreet up_write(&c->state_lock); 3421c6fdbd8SKent Overstreet } 3431c6fdbd8SKent Overstreet 3441c6fdbd8SKent Overstreet static void bch2_fs_read_only_async(struct bch_fs *c) 3451c6fdbd8SKent Overstreet { 3461c6fdbd8SKent Overstreet queue_work(system_long_wq, &c->read_only_work); 3471c6fdbd8SKent Overstreet } 3481c6fdbd8SKent Overstreet 3491c6fdbd8SKent Overstreet bool bch2_fs_emergency_read_only(struct bch_fs *c) 3501c6fdbd8SKent Overstreet { 3511c6fdbd8SKent Overstreet bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags); 3521c6fdbd8SKent Overstreet 3531c6fdbd8SKent Overstreet bch2_journal_halt(&c->journal); 3549f115ce9SKent Overstreet bch2_fs_read_only_async(c); 3551c6fdbd8SKent Overstreet 3561c6fdbd8SKent Overstreet wake_up(&bch_read_only_wait); 3571c6fdbd8SKent Overstreet return ret; 3581c6fdbd8SKent Overstreet } 3591c6fdbd8SKent Overstreet 360134915f3SKent Overstreet static int bch2_fs_read_write_late(struct bch_fs *c) 3611c6fdbd8SKent Overstreet { 362134915f3SKent Overstreet int ret; 3631c6fdbd8SKent Overstreet 364134915f3SKent Overstreet ret = bch2_gc_thread_start(c); 365134915f3SKent Overstreet if (ret) { 366134915f3SKent Overstreet bch_err(c, "error starting gc thread"); 367134915f3SKent Overstreet return ret; 368134915f3SKent Overstreet } 3691c6fdbd8SKent Overstreet 370e6d11615SKent Overstreet ret = bch2_copygc_start(c); 371134915f3SKent Overstreet if (ret) { 372e6d11615SKent Overstreet bch_err(c, "error starting copygc thread"); 373134915f3SKent Overstreet return ret; 374134915f3SKent Overstreet } 375134915f3SKent Overstreet 376134915f3SKent Overstreet ret = bch2_rebalance_start(c); 377134915f3SKent Overstreet if (ret) { 378134915f3SKent Overstreet bch_err(c, "error starting rebalance thread"); 379134915f3SKent Overstreet return ret; 380134915f3SKent Overstreet } 381134915f3SKent Overstreet 38297fd13adSKent Overstreet schedule_work(&c->ec_stripe_delete_work); 38397fd13adSKent Overstreet 384134915f3SKent Overstreet return 0; 385134915f3SKent Overstreet } 386134915f3SKent Overstreet 387e731d466SKent Overstreet static int __bch2_fs_read_write(struct bch_fs *c, bool early) 388134915f3SKent Overstreet { 389134915f3SKent Overstreet struct bch_dev *ca; 390134915f3SKent Overstreet unsigned i; 391134915f3SKent Overstreet int ret; 392134915f3SKent Overstreet 393aae15aafSKent Overstreet if (test_bit(BCH_FS_INITIAL_GC_UNFIXED, &c->flags)) { 394aae15aafSKent Overstreet bch_err(c, "cannot go rw, unfixed btree errors"); 395aae15aafSKent Overstreet return -EROFS; 396aae15aafSKent Overstreet } 397aae15aafSKent Overstreet 398134915f3SKent Overstreet if (test_bit(BCH_FS_RW, &c->flags)) 399134915f3SKent Overstreet return 0; 400134915f3SKent Overstreet 401619f5beeSKent Overstreet /* 402619f5beeSKent Overstreet * nochanges is used for fsck -n mode - we have to allow going rw 403619f5beeSKent Overstreet * during recovery for that to work: 404619f5beeSKent Overstreet */ 405619f5beeSKent Overstreet if (c->opts.norecovery || 406619f5beeSKent Overstreet (c->opts.nochanges && 407619f5beeSKent Overstreet (!early || c->opts.read_only))) 408619f5beeSKent Overstreet return -EROFS; 409330581f1SKent Overstreet 4102c944fa1SKent Overstreet bch_info(c, "going read-write"); 4112c944fa1SKent Overstreet 412134915f3SKent Overstreet ret = bch2_fs_mark_dirty(c); 413134915f3SKent Overstreet if (ret) 414134915f3SKent Overstreet goto err; 4151c6fdbd8SKent Overstreet 4162340fd9dSKent Overstreet clear_bit(BCH_FS_ALLOC_CLEAN, &c->flags); 4172340fd9dSKent Overstreet 4181c6fdbd8SKent Overstreet for_each_rw_member(ca, c, i) 4191c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 4201c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 4211c6fdbd8SKent Overstreet 422134915f3SKent Overstreet for_each_rw_member(ca, c, i) { 423134915f3SKent Overstreet ret = bch2_dev_allocator_start(ca); 424134915f3SKent Overstreet if (ret) { 425134915f3SKent Overstreet bch_err(c, "error starting allocator threads"); 4261c6fdbd8SKent Overstreet percpu_ref_put(&ca->io_ref); 4271c6fdbd8SKent Overstreet goto err; 4281c6fdbd8SKent Overstreet } 429134915f3SKent Overstreet } 4301c6fdbd8SKent Overstreet 431b935a8a6SKent Overstreet set_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags); 432b935a8a6SKent Overstreet 43359a74051SKent Overstreet for_each_rw_member(ca, c, i) 43459a74051SKent Overstreet bch2_wake_allocator(ca); 43559a74051SKent Overstreet 436134915f3SKent Overstreet if (!early) { 437134915f3SKent Overstreet ret = bch2_fs_read_write_late(c); 438134915f3SKent Overstreet if (ret) 4391c6fdbd8SKent Overstreet goto err; 4401c6fdbd8SKent Overstreet } 4411c6fdbd8SKent Overstreet 4421c6fdbd8SKent Overstreet percpu_ref_reinit(&c->writes); 443134915f3SKent Overstreet set_bit(BCH_FS_RW, &c->flags); 4444932e07eSKent Overstreet set_bit(BCH_FS_WAS_RW, &c->flags); 445134915f3SKent Overstreet return 0; 4461c6fdbd8SKent Overstreet err: 4471c6fdbd8SKent Overstreet __bch2_fs_read_only(c); 448134915f3SKent Overstreet return ret; 449134915f3SKent Overstreet } 450134915f3SKent Overstreet 451134915f3SKent Overstreet int bch2_fs_read_write(struct bch_fs *c) 452134915f3SKent Overstreet { 453134915f3SKent Overstreet return __bch2_fs_read_write(c, false); 454134915f3SKent Overstreet } 455134915f3SKent Overstreet 456134915f3SKent Overstreet int bch2_fs_read_write_early(struct bch_fs *c) 457134915f3SKent Overstreet { 458134915f3SKent Overstreet lockdep_assert_held(&c->state_lock); 459134915f3SKent Overstreet 460134915f3SKent Overstreet return __bch2_fs_read_write(c, true); 4611c6fdbd8SKent Overstreet } 4621c6fdbd8SKent Overstreet 4631c6fdbd8SKent Overstreet /* Filesystem startup/shutdown: */ 4641c6fdbd8SKent Overstreet 465d5e4dcc2SKent Overstreet static void __bch2_fs_free(struct bch_fs *c) 4661c6fdbd8SKent Overstreet { 4671c6fdbd8SKent Overstreet unsigned i; 4681a21bf98SKent Overstreet int cpu; 4691c6fdbd8SKent Overstreet 4701c6fdbd8SKent Overstreet for (i = 0; i < BCH_TIME_STAT_NR; i++) 4711c6fdbd8SKent Overstreet bch2_time_stats_exit(&c->times[i]); 4721c6fdbd8SKent Overstreet 47314b393eeSKent Overstreet bch2_fs_snapshots_exit(c); 4741c6fdbd8SKent Overstreet bch2_fs_quota_exit(c); 4751c6fdbd8SKent Overstreet bch2_fs_fsio_exit(c); 476cd575ddfSKent Overstreet bch2_fs_ec_exit(c); 4771c6fdbd8SKent Overstreet bch2_fs_encryption_exit(c); 4781c6fdbd8SKent Overstreet bch2_fs_io_exit(c); 47921aec962SKent Overstreet bch2_fs_buckets_waiting_for_journal_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); 4849620c3ecSKent Overstreet bch2_fs_replicas_exit(c); 4851c6fdbd8SKent Overstreet bch2_fs_journal_exit(&c->journal); 4861c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[WRITE]); 4871c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[READ]); 4881c6fdbd8SKent Overstreet bch2_fs_compress_exit(c); 489f1d786a0SKent Overstreet bch2_journal_keys_free(&c->journal_keys); 490f1d786a0SKent Overstreet bch2_journal_entries_free(&c->journal_entries); 4919166b41dSKent Overstreet percpu_free_rwsem(&c->mark_lock); 4925e82a9a1SKent Overstreet free_percpu(c->online_reserved); 4931a21bf98SKent Overstreet 49467e0dd8fSKent Overstreet if (c->btree_paths_bufs) 4951a21bf98SKent Overstreet for_each_possible_cpu(cpu) 49667e0dd8fSKent Overstreet kfree(per_cpu_ptr(c->btree_paths_bufs, cpu)->path); 4971a21bf98SKent Overstreet 49867e0dd8fSKent Overstreet free_percpu(c->btree_paths_bufs); 4995663a415SKent Overstreet free_percpu(c->pcpu); 50035189e09SKent Overstreet mempool_exit(&c->large_bkey_pool); 5011c6fdbd8SKent Overstreet mempool_exit(&c->btree_bounce_pool); 5021c6fdbd8SKent Overstreet bioset_exit(&c->btree_bio); 5031c6fdbd8SKent Overstreet mempool_exit(&c->fill_iter); 5041c6fdbd8SKent Overstreet percpu_ref_exit(&c->writes); 5051c6fdbd8SKent Overstreet kfree(rcu_dereference_protected(c->disk_groups, 1)); 5061dd7f9d9SKent Overstreet kfree(c->journal_seq_blacklist_table); 507b5e8a699SKent Overstreet kfree(c->unused_inode_hints); 508e6d11615SKent Overstreet free_heap(&c->copygc_heap); 5091c6fdbd8SKent Overstreet 510731bdd2eSKent Overstreet if (c->io_complete_wq ) 511731bdd2eSKent Overstreet destroy_workqueue(c->io_complete_wq ); 5121c6fdbd8SKent Overstreet if (c->copygc_wq) 5131c6fdbd8SKent Overstreet destroy_workqueue(c->copygc_wq); 5149f1833caSKent Overstreet if (c->btree_io_complete_wq) 5159f1833caSKent Overstreet destroy_workqueue(c->btree_io_complete_wq); 516731bdd2eSKent Overstreet if (c->btree_update_wq) 517731bdd2eSKent Overstreet destroy_workqueue(c->btree_update_wq); 5181c6fdbd8SKent Overstreet 5199d8022dbSKent Overstreet bch2_free_super(&c->disk_sb); 5201c6fdbd8SKent Overstreet kvpfree(c, sizeof(*c)); 5211c6fdbd8SKent Overstreet module_put(THIS_MODULE); 5221c6fdbd8SKent Overstreet } 5231c6fdbd8SKent Overstreet 5241c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *kobj) 5251c6fdbd8SKent Overstreet { 5261c6fdbd8SKent Overstreet struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); 5271c6fdbd8SKent Overstreet 528d5e4dcc2SKent Overstreet __bch2_fs_free(c); 5291c6fdbd8SKent Overstreet } 5301c6fdbd8SKent Overstreet 531d5e4dcc2SKent Overstreet void __bch2_fs_stop(struct bch_fs *c) 5321c6fdbd8SKent Overstreet { 5331c6fdbd8SKent Overstreet struct bch_dev *ca; 5341c6fdbd8SKent Overstreet unsigned i; 5351c6fdbd8SKent Overstreet 536af1c6871SKent Overstreet bch_verbose(c, "shutting down"); 537af1c6871SKent Overstreet 5381dd7f9d9SKent Overstreet set_bit(BCH_FS_STOPPING, &c->flags); 5391dd7f9d9SKent Overstreet 5409b6e2f1eSKent Overstreet cancel_work_sync(&c->journal_seq_blacklist_gc_work); 5419b6e2f1eSKent Overstreet 5421ada1606SKent Overstreet down_write(&c->state_lock); 543883f1a7cSKent Overstreet bch2_fs_read_only(c); 5441ada1606SKent Overstreet up_write(&c->state_lock); 545883f1a7cSKent Overstreet 5461c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 5471c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs && 5481c6fdbd8SKent Overstreet ca->disk_sb.bdev) 5491c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 5501c6fdbd8SKent Overstreet 5511c6fdbd8SKent Overstreet if (c->kobj.state_in_sysfs) 5521c6fdbd8SKent Overstreet kobject_del(&c->kobj); 5531c6fdbd8SKent Overstreet 5541c6fdbd8SKent Overstreet bch2_fs_debug_exit(c); 5551c6fdbd8SKent Overstreet bch2_fs_chardev_exit(c); 5561c6fdbd8SKent Overstreet 5571c6fdbd8SKent Overstreet kobject_put(&c->time_stats); 5581c6fdbd8SKent Overstreet kobject_put(&c->opts_dir); 5591c6fdbd8SKent Overstreet kobject_put(&c->internal); 5601c6fdbd8SKent Overstreet 5611c6fdbd8SKent Overstreet /* btree prefetch might have kicked off reads in the background: */ 5621c6fdbd8SKent Overstreet bch2_btree_flush_all_reads(c); 5631c6fdbd8SKent Overstreet 5641c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 5651c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 5661c6fdbd8SKent Overstreet 5671c6fdbd8SKent Overstreet cancel_work_sync(&c->read_only_work); 568d5e4dcc2SKent Overstreet } 5691c6fdbd8SKent Overstreet 570d5e4dcc2SKent Overstreet void bch2_fs_free(struct bch_fs *c) 571d5e4dcc2SKent Overstreet { 572d5e4dcc2SKent Overstreet unsigned i; 573d5e4dcc2SKent Overstreet 574d5e4dcc2SKent Overstreet mutex_lock(&bch_fs_list_lock); 575d5e4dcc2SKent Overstreet list_del(&c->list); 576d5e4dcc2SKent Overstreet mutex_unlock(&bch_fs_list_lock); 577d5e4dcc2SKent Overstreet 578d5e4dcc2SKent Overstreet closure_sync(&c->cl); 579d5e4dcc2SKent Overstreet closure_debug_destroy(&c->cl); 580d5e4dcc2SKent Overstreet 581d5e4dcc2SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) { 582d5e4dcc2SKent Overstreet struct bch_dev *ca = rcu_dereference_protected(c->devs[i], true); 583d5e4dcc2SKent Overstreet 584d5e4dcc2SKent Overstreet if (ca) { 585d5e4dcc2SKent Overstreet bch2_free_super(&ca->disk_sb); 586d5e4dcc2SKent Overstreet bch2_dev_free(ca); 587d5e4dcc2SKent Overstreet } 588d5e4dcc2SKent Overstreet } 5891c6fdbd8SKent Overstreet 590af1c6871SKent Overstreet bch_verbose(c, "shutdown complete"); 591af1c6871SKent Overstreet 5921c6fdbd8SKent Overstreet kobject_put(&c->kobj); 5931c6fdbd8SKent Overstreet } 5941c6fdbd8SKent Overstreet 595d5e4dcc2SKent Overstreet void bch2_fs_stop(struct bch_fs *c) 596d5e4dcc2SKent Overstreet { 597d5e4dcc2SKent Overstreet __bch2_fs_stop(c); 598d5e4dcc2SKent Overstreet bch2_fs_free(c); 599d5e4dcc2SKent Overstreet } 600d5e4dcc2SKent Overstreet 601e2b60560SKent Overstreet static int bch2_fs_online(struct bch_fs *c) 6021c6fdbd8SKent Overstreet { 6031c6fdbd8SKent Overstreet struct bch_dev *ca; 6041c6fdbd8SKent Overstreet unsigned i; 605e2b60560SKent Overstreet int ret = 0; 6061c6fdbd8SKent Overstreet 6071c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 6081c6fdbd8SKent Overstreet 609e2b60560SKent Overstreet if (__bch2_uuid_to_fs(c->sb.uuid)) { 610e2b60560SKent Overstreet bch_err(c, "filesystem UUID already open"); 611e2b60560SKent Overstreet return -EINVAL; 612e2b60560SKent Overstreet } 6131c6fdbd8SKent Overstreet 6141c6fdbd8SKent Overstreet ret = bch2_fs_chardev_init(c); 615e2b60560SKent Overstreet if (ret) { 616e2b60560SKent Overstreet bch_err(c, "error creating character device"); 617e2b60560SKent Overstreet return ret; 618e2b60560SKent Overstreet } 6191c6fdbd8SKent Overstreet 6201c6fdbd8SKent Overstreet bch2_fs_debug_init(c); 6211c6fdbd8SKent Overstreet 622e2b60560SKent Overstreet ret = kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ?: 623e2b60560SKent Overstreet kobject_add(&c->internal, &c->kobj, "internal") ?: 624e2b60560SKent Overstreet kobject_add(&c->opts_dir, &c->kobj, "options") ?: 625e2b60560SKent Overstreet kobject_add(&c->time_stats, &c->kobj, "time_stats") ?: 626e2b60560SKent Overstreet bch2_opts_create_sysfs_files(&c->opts_dir); 627e2b60560SKent Overstreet if (ret) { 628e2b60560SKent Overstreet bch_err(c, "error creating sysfs objects"); 629e2b60560SKent Overstreet return ret; 630e2b60560SKent Overstreet } 6311c6fdbd8SKent Overstreet 6321ada1606SKent Overstreet down_write(&c->state_lock); 6331c6fdbd8SKent Overstreet 634e2b60560SKent Overstreet for_each_member_device(ca, c, i) { 635e2b60560SKent Overstreet ret = bch2_dev_sysfs_online(c, ca); 636e2b60560SKent Overstreet if (ret) { 637e2b60560SKent Overstreet bch_err(c, "error creating sysfs objects"); 6383a402c8dSKent Overstreet percpu_ref_put(&ca->ref); 6391c6fdbd8SKent Overstreet goto err; 6403a402c8dSKent Overstreet } 641e2b60560SKent Overstreet } 6421c6fdbd8SKent Overstreet 643e2b60560SKent Overstreet BUG_ON(!list_empty(&c->list)); 6441c6fdbd8SKent Overstreet list_add(&c->list, &bch_fs_list); 6451c6fdbd8SKent Overstreet err: 6461ada1606SKent Overstreet up_write(&c->state_lock); 647e2b60560SKent Overstreet return ret; 6481c6fdbd8SKent Overstreet } 6491c6fdbd8SKent Overstreet 6501c6fdbd8SKent Overstreet static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts) 6511c6fdbd8SKent Overstreet { 6521c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 6531c6fdbd8SKent Overstreet struct bch_fs *c; 654ecf37a4aSKent Overstreet unsigned i, iter_size; 6557be9ab63SChris Webb int ret = 0; 6561c6fdbd8SKent Overstreet 6571c6fdbd8SKent Overstreet pr_verbose_init(opts, ""); 6581c6fdbd8SKent Overstreet 6591c6fdbd8SKent Overstreet c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO); 6607be9ab63SChris Webb if (!c) { 6617be9ab63SChris Webb c = ERR_PTR(-ENOMEM); 6621c6fdbd8SKent Overstreet goto out; 6637be9ab63SChris Webb } 6641c6fdbd8SKent Overstreet 6651c6fdbd8SKent Overstreet __module_get(THIS_MODULE); 6661c6fdbd8SKent Overstreet 667505b7a4cSKent Overstreet closure_init(&c->cl, NULL); 668505b7a4cSKent Overstreet 669505b7a4cSKent Overstreet c->kobj.kset = bcachefs_kset; 670505b7a4cSKent Overstreet kobject_init(&c->kobj, &bch2_fs_ktype); 671505b7a4cSKent Overstreet kobject_init(&c->internal, &bch2_fs_internal_ktype); 672505b7a4cSKent Overstreet kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype); 673505b7a4cSKent Overstreet kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype); 674505b7a4cSKent Overstreet 6751c6fdbd8SKent Overstreet c->minor = -1; 6761c6fdbd8SKent Overstreet c->disk_sb.fs_sb = true; 6771c6fdbd8SKent Overstreet 6781ada1606SKent Overstreet init_rwsem(&c->state_lock); 6791c6fdbd8SKent Overstreet mutex_init(&c->sb_lock); 6801c6fdbd8SKent Overstreet mutex_init(&c->replicas_gc_lock); 6811c6fdbd8SKent Overstreet mutex_init(&c->btree_root_lock); 6821c6fdbd8SKent Overstreet INIT_WORK(&c->read_only_work, bch2_fs_read_only_work); 6831c6fdbd8SKent Overstreet 6841c6fdbd8SKent Overstreet init_rwsem(&c->gc_lock); 685c45c8667SKent Overstreet mutex_init(&c->gc_gens_lock); 6861c6fdbd8SKent Overstreet 6871c6fdbd8SKent Overstreet for (i = 0; i < BCH_TIME_STAT_NR; i++) 6881c6fdbd8SKent Overstreet bch2_time_stats_init(&c->times[i]); 6891c6fdbd8SKent Overstreet 690e6d11615SKent Overstreet bch2_fs_copygc_init(c); 6912ca88e5aSKent Overstreet bch2_fs_btree_key_cache_init_early(&c->btree_key_cache); 692b092daddSKent Overstreet bch2_fs_allocator_background_init(c); 693b092daddSKent Overstreet bch2_fs_allocator_foreground_init(c); 6941c6fdbd8SKent Overstreet bch2_fs_rebalance_init(c); 6951c6fdbd8SKent Overstreet bch2_fs_quota_init(c); 6961c6fdbd8SKent Overstreet 6971c6fdbd8SKent Overstreet INIT_LIST_HEAD(&c->list); 6981c6fdbd8SKent Overstreet 6994d8100daSKent Overstreet mutex_init(&c->usage_scratch_lock); 7004d8100daSKent Overstreet 7011c6fdbd8SKent Overstreet mutex_init(&c->bio_bounce_pages_lock); 70214b393eeSKent Overstreet mutex_init(&c->snapshot_table_lock); 7031c6fdbd8SKent Overstreet 7041c6fdbd8SKent Overstreet spin_lock_init(&c->btree_write_error_lock); 7051c6fdbd8SKent Overstreet 7069b6e2f1eSKent Overstreet INIT_WORK(&c->journal_seq_blacklist_gc_work, 7079b6e2f1eSKent Overstreet bch2_blacklist_entries_gc); 7089b6e2f1eSKent Overstreet 709f1d786a0SKent Overstreet INIT_LIST_HEAD(&c->journal_entries); 7105b593ee1SKent Overstreet INIT_LIST_HEAD(&c->journal_iters); 711f1d786a0SKent Overstreet 7121c6fdbd8SKent Overstreet INIT_LIST_HEAD(&c->fsck_errors); 7131c6fdbd8SKent Overstreet mutex_init(&c->fsck_error_lock); 7141c6fdbd8SKent Overstreet 715703e2a43SKent Overstreet INIT_LIST_HEAD(&c->ec_stripe_head_list); 716703e2a43SKent Overstreet mutex_init(&c->ec_stripe_head_lock); 717703e2a43SKent Overstreet 718703e2a43SKent Overstreet INIT_LIST_HEAD(&c->ec_stripe_new_list); 719703e2a43SKent Overstreet mutex_init(&c->ec_stripe_new_lock); 720703e2a43SKent Overstreet 7218dd6ed94SBrett Holman INIT_LIST_HEAD(&c->data_progress_list); 7228dd6ed94SBrett Holman mutex_init(&c->data_progress_lock); 7238dd6ed94SBrett Holman 724cd575ddfSKent Overstreet spin_lock_init(&c->ec_stripes_heap_lock); 725cd575ddfSKent Overstreet 7261c6fdbd8SKent Overstreet seqcount_init(&c->gc_pos_lock); 7271c6fdbd8SKent Overstreet 7285e82a9a1SKent Overstreet seqcount_init(&c->usage_lock); 7295e82a9a1SKent Overstreet 730ef1b2092SKent Overstreet sema_init(&c->io_in_flight, 128); 731ef1b2092SKent Overstreet 7321c6fdbd8SKent Overstreet c->copy_gc_enabled = 1; 7331c6fdbd8SKent Overstreet c->rebalance.enabled = 1; 7341c6fdbd8SKent Overstreet c->promote_whole_extents = true; 7351c6fdbd8SKent Overstreet 736991ba021SKent Overstreet c->journal.flush_write_time = &c->times[BCH_TIME_journal_flush_write]; 737991ba021SKent Overstreet c->journal.noflush_write_time = &c->times[BCH_TIME_journal_noflush_write]; 73849a67206SKent Overstreet c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal]; 7391c6fdbd8SKent Overstreet c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq]; 7401c6fdbd8SKent Overstreet 7411c6fdbd8SKent Overstreet bch2_fs_btree_cache_init_early(&c->btree_cache); 7421c6fdbd8SKent Overstreet 743fca1223cSKent Overstreet mutex_init(&c->sectors_available_lock); 744fca1223cSKent Overstreet 745e2b60560SKent Overstreet ret = percpu_init_rwsem(&c->mark_lock); 746e2b60560SKent Overstreet if (ret) 74773e6ab95SKent Overstreet goto err; 74873e6ab95SKent Overstreet 7491c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 750e2b60560SKent Overstreet ret = bch2_sb_to_fs(c, sb); 7511c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 752e2b60560SKent Overstreet 753e2b60560SKent Overstreet if (ret) 7541c6fdbd8SKent Overstreet goto err; 7551c6fdbd8SKent Overstreet 75603ea3962SKent Overstreet uuid_unparse_lower(c->sb.user_uuid.b, c->name); 7571c6fdbd8SKent Overstreet 7582430e72fSKent Overstreet /* Compat: */ 7592430e72fSKent Overstreet if (sb->version <= bcachefs_metadata_version_inode_v2 && 7602430e72fSKent Overstreet !BCH_SB_JOURNAL_FLUSH_DELAY(sb)) 7612430e72fSKent Overstreet SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000); 7622430e72fSKent Overstreet 7632430e72fSKent Overstreet if (sb->version <= bcachefs_metadata_version_inode_v2 && 7642430e72fSKent Overstreet !BCH_SB_JOURNAL_RECLAIM_DELAY(sb)) 7652430e72fSKent Overstreet SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 100); 7662430e72fSKent Overstreet 7671c6fdbd8SKent Overstreet c->opts = bch2_opts_default; 7688244f320SKent Overstreet ret = bch2_opts_from_sb(&c->opts, sb); 7698244f320SKent Overstreet if (ret) 7708244f320SKent Overstreet goto err; 7718244f320SKent Overstreet 7721c6fdbd8SKent Overstreet bch2_opts_apply(&c->opts, opts); 7731c6fdbd8SKent Overstreet 7747c8f6f98SKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_alloc; 7757c8f6f98SKent Overstreet if (c->opts.inodes_use_key_cache) 7767c8f6f98SKent Overstreet c->btree_key_cache_btrees |= 1U << BTREE_ID_inodes; 7777c8f6f98SKent Overstreet 7788244f320SKent Overstreet c->block_bits = ilog2(block_sectors(c)); 7791c6fdbd8SKent Overstreet c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c); 7801c6fdbd8SKent Overstreet 7817be9ab63SChris Webb if (bch2_fs_init_fault("fs_alloc")) { 782e2b60560SKent Overstreet bch_err(c, "fs_alloc fault injected"); 783e2b60560SKent Overstreet ret = -EFAULT; 7841c6fdbd8SKent Overstreet goto err; 7857be9ab63SChris Webb } 7861c6fdbd8SKent Overstreet 787ae2f17d5SKent Overstreet iter_size = sizeof(struct sort_iter) + 7881c6fdbd8SKent Overstreet (btree_blocks(c) + 1) * 2 * 789ae2f17d5SKent Overstreet sizeof(struct sort_iter_set); 7901c6fdbd8SKent Overstreet 791b5e8a699SKent Overstreet c->inode_shard_bits = ilog2(roundup_pow_of_two(num_possible_cpus())); 792b5e8a699SKent Overstreet 793731bdd2eSKent Overstreet if (!(c->btree_update_wq = alloc_workqueue("bcachefs", 794*b17d3cecSKent Overstreet WQ_FREEZABLE|WQ_UNBOUND|WQ_MEM_RECLAIM, 512)) || 7959f1833caSKent Overstreet !(c->btree_io_complete_wq = alloc_workqueue("bcachefs_btree_io", 7969f2772c4SKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM, 1)) || 7972f33ece9SKent Overstreet !(c->copygc_wq = alloc_workqueue("bcachefs_copygc", 7982f33ece9SKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE, 1)) || 799731bdd2eSKent Overstreet !(c->io_complete_wq = alloc_workqueue("bcachefs_io", 800731bdd2eSKent Overstreet WQ_FREEZABLE|WQ_HIGHPRI|WQ_MEM_RECLAIM, 1)) || 801134915f3SKent Overstreet percpu_ref_init(&c->writes, bch2_writes_disabled, 802134915f3SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 8031c6fdbd8SKent Overstreet mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) || 8041c6fdbd8SKent Overstreet bioset_init(&c->btree_bio, 1, 8051c6fdbd8SKent Overstreet max(offsetof(struct btree_read_bio, bio), 8061c6fdbd8SKent Overstreet offsetof(struct btree_write_bio, wbio.bio)), 8071c6fdbd8SKent Overstreet BIOSET_NEED_BVECS) || 8085663a415SKent Overstreet !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) || 8095e82a9a1SKent Overstreet !(c->online_reserved = alloc_percpu(u64)) || 81067e0dd8fSKent Overstreet !(c->btree_paths_bufs = alloc_percpu(struct btree_path_buf)) || 8111c6fdbd8SKent Overstreet mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1, 8121c6fdbd8SKent Overstreet btree_bytes(c)) || 81335189e09SKent Overstreet mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) || 814b5e8a699SKent Overstreet !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits, 815e2b60560SKent Overstreet sizeof(u64), GFP_KERNEL))) { 8167be9ab63SChris Webb ret = -ENOMEM; 8177be9ab63SChris Webb goto err; 8187be9ab63SChris Webb } 8197be9ab63SChris Webb 820e2b60560SKent Overstreet ret = bch2_io_clock_init(&c->io_clock[READ]) ?: 821e2b60560SKent Overstreet bch2_io_clock_init(&c->io_clock[WRITE]) ?: 822e2b60560SKent Overstreet bch2_fs_journal_init(&c->journal) ?: 823e2b60560SKent Overstreet bch2_fs_replicas_init(c) ?: 824e2b60560SKent Overstreet bch2_fs_btree_cache_init(c) ?: 825e2b60560SKent Overstreet bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?: 826e2b60560SKent Overstreet bch2_fs_btree_iter_init(c) ?: 827e2b60560SKent Overstreet bch2_fs_btree_interior_update_init(c) ?: 82821aec962SKent Overstreet bch2_fs_buckets_waiting_for_journal_init(c); 829e2b60560SKent Overstreet bch2_fs_subvolumes_init(c) ?: 830e2b60560SKent Overstreet bch2_fs_io_init(c) ?: 831e2b60560SKent Overstreet bch2_fs_encryption_init(c) ?: 832e2b60560SKent Overstreet bch2_fs_compress_init(c) ?: 833e2b60560SKent Overstreet bch2_fs_ec_init(c) ?: 834e2b60560SKent Overstreet bch2_fs_fsio_init(c); 8357be9ab63SChris Webb if (ret) 8361c6fdbd8SKent Overstreet goto err; 8371c6fdbd8SKent Overstreet 8381c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 8391c6fdbd8SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) 8401c6fdbd8SKent Overstreet if (bch2_dev_exists(c->disk_sb.sb, mi, i) && 8417be9ab63SChris Webb bch2_dev_alloc(c, i)) { 842e2b60560SKent Overstreet ret = -EEXIST; 8431c6fdbd8SKent Overstreet goto err; 8447be9ab63SChris Webb } 8451c6fdbd8SKent Overstreet 8464b8f89afSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 8474b8f89afSKent Overstreet &c->btree_root_journal_res, 8484b8f89afSKent Overstreet BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_BTREE_PTR_U64s_MAX)); 8494b8f89afSKent Overstreet bch2_dev_usage_journal_reserve(c); 8504b8f89afSKent Overstreet bch2_journal_entry_res_resize(&c->journal, 8514b8f89afSKent Overstreet &c->clock_journal_res, 8524b8f89afSKent Overstreet (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2); 8534b8f89afSKent Overstreet 8541c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 855e2b60560SKent Overstreet ret = bch2_fs_online(c); 8561c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 857e2b60560SKent Overstreet 858e2b60560SKent Overstreet if (ret) 8591c6fdbd8SKent Overstreet goto err; 8601c6fdbd8SKent Overstreet out: 8617be9ab63SChris Webb pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c)); 8621c6fdbd8SKent Overstreet return c; 8631c6fdbd8SKent Overstreet err: 8641c6fdbd8SKent Overstreet bch2_fs_free(c); 8657be9ab63SChris Webb c = ERR_PTR(ret); 8661c6fdbd8SKent Overstreet goto out; 8671c6fdbd8SKent Overstreet } 8681c6fdbd8SKent Overstreet 869619f5beeSKent Overstreet noinline_for_stack 870619f5beeSKent Overstreet static void print_mount_opts(struct bch_fs *c) 871619f5beeSKent Overstreet { 872619f5beeSKent Overstreet enum bch_opt_id i; 873fa8e94faSKent Overstreet struct printbuf p = PRINTBUF; 874619f5beeSKent Overstreet bool first = true; 875619f5beeSKent Overstreet 876619f5beeSKent Overstreet if (c->opts.read_only) { 877619f5beeSKent Overstreet pr_buf(&p, "ro"); 878619f5beeSKent Overstreet first = false; 879619f5beeSKent Overstreet } 880619f5beeSKent Overstreet 881619f5beeSKent Overstreet for (i = 0; i < bch2_opts_nr; i++) { 882619f5beeSKent Overstreet const struct bch_option *opt = &bch2_opt_table[i]; 883619f5beeSKent Overstreet u64 v = bch2_opt_get_by_id(&c->opts, i); 884619f5beeSKent Overstreet 8858244f320SKent Overstreet if (!(opt->flags & OPT_MOUNT)) 886619f5beeSKent Overstreet continue; 887619f5beeSKent Overstreet 888619f5beeSKent Overstreet if (v == bch2_opt_get_by_id(&bch2_opts_default, i)) 889619f5beeSKent Overstreet continue; 890619f5beeSKent Overstreet 891619f5beeSKent Overstreet if (!first) 892619f5beeSKent Overstreet pr_buf(&p, ","); 893619f5beeSKent Overstreet first = false; 8945521b1dfSKent Overstreet bch2_opt_to_text(&p, c, c->disk_sb.sb, opt, v, OPT_SHOW_MOUNT_STYLE); 895619f5beeSKent Overstreet } 896619f5beeSKent Overstreet 897fa8e94faSKent Overstreet if (!p.pos) 898fa8e94faSKent Overstreet pr_buf(&p, "(null)"); 899fa8e94faSKent Overstreet 90074b33393SKent Overstreet bch_info(c, "mounted version=%s opts=%s", bch2_metadata_versions[c->sb.version], p.buf); 901fa8e94faSKent Overstreet printbuf_exit(&p); 902619f5beeSKent Overstreet } 903619f5beeSKent Overstreet 904619f5beeSKent Overstreet int bch2_fs_start(struct bch_fs *c) 9051c6fdbd8SKent Overstreet { 9061c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 9071c6fdbd8SKent Overstreet struct bch_dev *ca; 908a420eea6STim Schlueter time64_t now = ktime_get_real_seconds(); 9091c6fdbd8SKent Overstreet unsigned i; 9101c6fdbd8SKent Overstreet int ret = -EINVAL; 9111c6fdbd8SKent Overstreet 9121ada1606SKent Overstreet down_write(&c->state_lock); 9131c6fdbd8SKent Overstreet 914134915f3SKent Overstreet BUG_ON(test_bit(BCH_FS_STARTED, &c->flags)); 9151c6fdbd8SKent Overstreet 9161c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 9171c6fdbd8SKent Overstreet 9181c6fdbd8SKent Overstreet for_each_online_member(ca, c, i) 9191c6fdbd8SKent Overstreet bch2_sb_from_fs(c, ca); 9201c6fdbd8SKent Overstreet 9211c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 9221c6fdbd8SKent Overstreet for_each_online_member(ca, c, i) 9231c6fdbd8SKent Overstreet mi->members[ca->dev_idx].last_mount = cpu_to_le64(now); 9241c6fdbd8SKent Overstreet 9251c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 9261c6fdbd8SKent Overstreet 9271c6fdbd8SKent Overstreet for_each_rw_member(ca, c, i) 9281c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 9291c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 9301c6fdbd8SKent Overstreet 9311c6fdbd8SKent Overstreet ret = BCH_SB_INITIALIZED(c->disk_sb.sb) 9321c6fdbd8SKent Overstreet ? bch2_fs_recovery(c) 9331c6fdbd8SKent Overstreet : bch2_fs_initialize(c); 9341c6fdbd8SKent Overstreet if (ret) 9351c6fdbd8SKent Overstreet goto err; 9361c6fdbd8SKent Overstreet 937cd575ddfSKent Overstreet ret = bch2_opts_check_may_set(c); 938cd575ddfSKent Overstreet if (ret) 939cd575ddfSKent Overstreet goto err; 940cd575ddfSKent Overstreet 941619f5beeSKent Overstreet ret = -EINVAL; 942e2b60560SKent Overstreet if (bch2_fs_init_fault("fs_start")) { 943e2b60560SKent Overstreet bch_err(c, "fs_start fault injected"); 9441c6fdbd8SKent Overstreet goto err; 945e2b60560SKent Overstreet } 9461c6fdbd8SKent Overstreet 947a9310ab0SKent Overstreet set_bit(BCH_FS_STARTED, &c->flags); 948a9310ab0SKent Overstreet 9499f20ed15SKent Overstreet /* 9509f20ed15SKent Overstreet * Allocator threads don't start filling copygc reserve until after we 9519f20ed15SKent Overstreet * set BCH_FS_STARTED - wake them now: 9522ee47eecSKent Overstreet * 9532ee47eecSKent Overstreet * XXX ugly hack: 9542ee47eecSKent Overstreet * Need to set ca->allocator_state here instead of relying on the 9552ee47eecSKent Overstreet * allocator threads to do it to avoid racing with the copygc threads 9562ee47eecSKent Overstreet * checking it and thinking they have no alloc reserve: 9579f20ed15SKent Overstreet */ 9582ee47eecSKent Overstreet for_each_online_member(ca, c, i) { 9592ee47eecSKent Overstreet ca->allocator_state = ALLOCATOR_running; 9609f20ed15SKent Overstreet bch2_wake_allocator(ca); 9612ee47eecSKent Overstreet } 9629f20ed15SKent Overstreet 963619f5beeSKent Overstreet if (c->opts.read_only || c->opts.nochanges) { 9641c6fdbd8SKent Overstreet bch2_fs_read_only(c); 9651c6fdbd8SKent Overstreet } else { 966619f5beeSKent Overstreet ret = !test_bit(BCH_FS_RW, &c->flags) 967619f5beeSKent Overstreet ? bch2_fs_read_write(c) 968619f5beeSKent Overstreet : bch2_fs_read_write_late(c); 969619f5beeSKent Overstreet if (ret) 9701c6fdbd8SKent Overstreet goto err; 9711c6fdbd8SKent Overstreet } 9721c6fdbd8SKent Overstreet 973619f5beeSKent Overstreet print_mount_opts(c); 974619f5beeSKent Overstreet ret = 0; 9751c6fdbd8SKent Overstreet out: 9761ada1606SKent Overstreet up_write(&c->state_lock); 977619f5beeSKent Overstreet return ret; 9781c6fdbd8SKent Overstreet err: 9791c6fdbd8SKent Overstreet switch (ret) { 9801c6fdbd8SKent Overstreet case BCH_FSCK_ERRORS_NOT_FIXED: 9811c6fdbd8SKent Overstreet bch_err(c, "filesystem contains errors: please report this to the developers"); 9821c6fdbd8SKent Overstreet pr_cont("mount with -o fix_errors to repair\n"); 9831c6fdbd8SKent Overstreet break; 9841c6fdbd8SKent Overstreet case BCH_FSCK_REPAIR_UNIMPLEMENTED: 9851c6fdbd8SKent Overstreet bch_err(c, "filesystem contains errors: please report this to the developers"); 9861c6fdbd8SKent Overstreet pr_cont("repair unimplemented: inform the developers so that it can be added\n"); 9871c6fdbd8SKent Overstreet break; 9881c6fdbd8SKent Overstreet case BCH_FSCK_REPAIR_IMPOSSIBLE: 9891c6fdbd8SKent Overstreet bch_err(c, "filesystem contains errors, but repair impossible"); 9901c6fdbd8SKent Overstreet break; 9911c6fdbd8SKent Overstreet case BCH_FSCK_UNKNOWN_VERSION: 992e2b60560SKent Overstreet bch_err(c, "unknown metadata version"); 9931c6fdbd8SKent Overstreet break; 9941c6fdbd8SKent Overstreet case -ENOMEM: 995e2b60560SKent Overstreet bch_err(c, "cannot allocate memory"); 9961c6fdbd8SKent Overstreet break; 9971c6fdbd8SKent Overstreet case -EIO: 998e2b60560SKent Overstreet bch_err(c, "IO error"); 9991c6fdbd8SKent Overstreet break; 10001c6fdbd8SKent Overstreet } 10011c6fdbd8SKent Overstreet 10029516950cSKent Overstreet if (ret >= 0) 10039516950cSKent Overstreet ret = -EIO; 10041c6fdbd8SKent Overstreet goto out; 10051c6fdbd8SKent Overstreet } 10061c6fdbd8SKent Overstreet 10071c6fdbd8SKent Overstreet static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c) 10081c6fdbd8SKent Overstreet { 10091c6fdbd8SKent Overstreet struct bch_sb_field_members *sb_mi; 10101c6fdbd8SKent Overstreet 10111c6fdbd8SKent Overstreet sb_mi = bch2_sb_get_members(sb); 10121c6fdbd8SKent Overstreet if (!sb_mi) 10131c6fdbd8SKent Overstreet return "Invalid superblock: member info area missing"; 10141c6fdbd8SKent Overstreet 10158244f320SKent Overstreet if (le16_to_cpu(sb->block_size) != block_sectors(c)) 10161c6fdbd8SKent Overstreet return "mismatched block size"; 10171c6fdbd8SKent Overstreet 10181c6fdbd8SKent Overstreet if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) < 10191c6fdbd8SKent Overstreet BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb)) 10201c6fdbd8SKent Overstreet return "new cache bucket size is too small"; 10211c6fdbd8SKent Overstreet 10221c6fdbd8SKent Overstreet return NULL; 10231c6fdbd8SKent Overstreet } 10241c6fdbd8SKent Overstreet 10251c6fdbd8SKent Overstreet static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb) 10261c6fdbd8SKent Overstreet { 10271c6fdbd8SKent Overstreet struct bch_sb *newest = 10281c6fdbd8SKent Overstreet le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb; 10291c6fdbd8SKent Overstreet struct bch_sb_field_members *mi = bch2_sb_get_members(newest); 10301c6fdbd8SKent Overstreet 10311c6fdbd8SKent Overstreet if (!uuid_equal(&fs->uuid, &sb->uuid)) 10321c6fdbd8SKent Overstreet return "device not a member of filesystem"; 10331c6fdbd8SKent Overstreet 10341c6fdbd8SKent Overstreet if (!bch2_dev_exists(newest, mi, sb->dev_idx)) 10351c6fdbd8SKent Overstreet return "device has been removed"; 10361c6fdbd8SKent Overstreet 10371c6fdbd8SKent Overstreet if (fs->block_size != sb->block_size) 10381c6fdbd8SKent Overstreet return "mismatched block size"; 10391c6fdbd8SKent Overstreet 10401c6fdbd8SKent Overstreet return NULL; 10411c6fdbd8SKent Overstreet } 10421c6fdbd8SKent Overstreet 10431c6fdbd8SKent Overstreet /* Device startup/shutdown: */ 10441c6fdbd8SKent Overstreet 10451c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *kobj) 10461c6fdbd8SKent Overstreet { 10471c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); 10481c6fdbd8SKent Overstreet 10491c6fdbd8SKent Overstreet kfree(ca); 10501c6fdbd8SKent Overstreet } 10511c6fdbd8SKent Overstreet 10521c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *ca) 10531c6fdbd8SKent Overstreet { 105459a74051SKent Overstreet bch2_dev_allocator_stop(ca); 105559a74051SKent Overstreet 10561c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 10571c6fdbd8SKent Overstreet 10581c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs && 10591c6fdbd8SKent Overstreet ca->disk_sb.bdev) 10601c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 10611c6fdbd8SKent Overstreet 10621c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) 10631c6fdbd8SKent Overstreet kobject_del(&ca->kobj); 10641c6fdbd8SKent Overstreet 10651c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 10661c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 10671c6fdbd8SKent Overstreet 10681c6fdbd8SKent Overstreet free_percpu(ca->io_done); 10691c6fdbd8SKent Overstreet bioset_exit(&ca->replica_set); 10701c6fdbd8SKent Overstreet bch2_dev_buckets_free(ca); 1071d1170ce5SKent Overstreet free_page((unsigned long) ca->sb_read_scratch); 10721c6fdbd8SKent Overstreet 10731c6fdbd8SKent Overstreet bch2_time_stats_exit(&ca->io_latency[WRITE]); 10741c6fdbd8SKent Overstreet bch2_time_stats_exit(&ca->io_latency[READ]); 10751c6fdbd8SKent Overstreet 10761c6fdbd8SKent Overstreet percpu_ref_exit(&ca->io_ref); 10771c6fdbd8SKent Overstreet percpu_ref_exit(&ca->ref); 10781c6fdbd8SKent Overstreet kobject_put(&ca->kobj); 10791c6fdbd8SKent Overstreet } 10801c6fdbd8SKent Overstreet 10811c6fdbd8SKent Overstreet static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) 10821c6fdbd8SKent Overstreet { 10831c6fdbd8SKent Overstreet 10841c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 10851c6fdbd8SKent Overstreet 10861c6fdbd8SKent Overstreet if (percpu_ref_is_zero(&ca->io_ref)) 10871c6fdbd8SKent Overstreet return; 10881c6fdbd8SKent Overstreet 10891c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 10901c6fdbd8SKent Overstreet 10911c6fdbd8SKent Overstreet reinit_completion(&ca->io_ref_completion); 10921c6fdbd8SKent Overstreet percpu_ref_kill(&ca->io_ref); 10931c6fdbd8SKent Overstreet wait_for_completion(&ca->io_ref_completion); 10941c6fdbd8SKent Overstreet 10951c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) { 10961c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 10971c6fdbd8SKent Overstreet sysfs_remove_link(&ca->kobj, "block"); 10981c6fdbd8SKent Overstreet } 10991c6fdbd8SKent Overstreet 11001c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 11011c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 11021c6fdbd8SKent Overstreet } 11031c6fdbd8SKent Overstreet 11041c6fdbd8SKent Overstreet static void bch2_dev_ref_complete(struct percpu_ref *ref) 11051c6fdbd8SKent Overstreet { 11061c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, ref); 11071c6fdbd8SKent Overstreet 11081c6fdbd8SKent Overstreet complete(&ca->ref_completion); 11091c6fdbd8SKent Overstreet } 11101c6fdbd8SKent Overstreet 11111c6fdbd8SKent Overstreet static void bch2_dev_io_ref_complete(struct percpu_ref *ref) 11121c6fdbd8SKent Overstreet { 11131c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref); 11141c6fdbd8SKent Overstreet 11151c6fdbd8SKent Overstreet complete(&ca->io_ref_completion); 11161c6fdbd8SKent Overstreet } 11171c6fdbd8SKent Overstreet 11181c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) 11191c6fdbd8SKent Overstreet { 11201c6fdbd8SKent Overstreet int ret; 11211c6fdbd8SKent Overstreet 11221c6fdbd8SKent Overstreet if (!c->kobj.state_in_sysfs) 11231c6fdbd8SKent Overstreet return 0; 11241c6fdbd8SKent Overstreet 11251c6fdbd8SKent Overstreet if (!ca->kobj.state_in_sysfs) { 11261c6fdbd8SKent Overstreet ret = kobject_add(&ca->kobj, &c->kobj, 11271c6fdbd8SKent Overstreet "dev-%u", ca->dev_idx); 11281c6fdbd8SKent Overstreet if (ret) 11291c6fdbd8SKent Overstreet return ret; 11301c6fdbd8SKent Overstreet } 11311c6fdbd8SKent Overstreet 11321c6fdbd8SKent Overstreet if (ca->disk_sb.bdev) { 11331c6fdbd8SKent Overstreet struct kobject *block = bdev_kobj(ca->disk_sb.bdev); 11341c6fdbd8SKent Overstreet 11351c6fdbd8SKent Overstreet ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); 11361c6fdbd8SKent Overstreet if (ret) 11371c6fdbd8SKent Overstreet return ret; 11381c6fdbd8SKent Overstreet 11391c6fdbd8SKent Overstreet ret = sysfs_create_link(&ca->kobj, block, "block"); 11401c6fdbd8SKent Overstreet if (ret) 11411c6fdbd8SKent Overstreet return ret; 11421c6fdbd8SKent Overstreet } 11431c6fdbd8SKent Overstreet 11441c6fdbd8SKent Overstreet return 0; 11451c6fdbd8SKent Overstreet } 11461c6fdbd8SKent Overstreet 11471c6fdbd8SKent Overstreet static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c, 11481c6fdbd8SKent Overstreet struct bch_member *member) 11491c6fdbd8SKent Overstreet { 11501c6fdbd8SKent Overstreet struct bch_dev *ca; 11511c6fdbd8SKent Overstreet 11521c6fdbd8SKent Overstreet ca = kzalloc(sizeof(*ca), GFP_KERNEL); 11531c6fdbd8SKent Overstreet if (!ca) 11541c6fdbd8SKent Overstreet return NULL; 11551c6fdbd8SKent Overstreet 11561c6fdbd8SKent Overstreet kobject_init(&ca->kobj, &bch2_dev_ktype); 11571c6fdbd8SKent Overstreet init_completion(&ca->ref_completion); 11581c6fdbd8SKent Overstreet init_completion(&ca->io_ref_completion); 11591c6fdbd8SKent Overstreet 11601c6fdbd8SKent Overstreet init_rwsem(&ca->bucket_lock); 11611c6fdbd8SKent Overstreet 11621c6fdbd8SKent Overstreet INIT_WORK(&ca->io_error_work, bch2_io_error_work); 11631c6fdbd8SKent Overstreet 11641c6fdbd8SKent Overstreet bch2_time_stats_init(&ca->io_latency[READ]); 11651c6fdbd8SKent Overstreet bch2_time_stats_init(&ca->io_latency[WRITE]); 11661c6fdbd8SKent Overstreet 11671c6fdbd8SKent Overstreet ca->mi = bch2_mi_to_cpu(member); 11681c6fdbd8SKent Overstreet ca->uuid = member->uuid; 11691c6fdbd8SKent Overstreet 11701c6fdbd8SKent Overstreet if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, 11711c6fdbd8SKent Overstreet 0, GFP_KERNEL) || 11721c6fdbd8SKent Overstreet percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete, 11731c6fdbd8SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 1174d1170ce5SKent Overstreet !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) || 11751c6fdbd8SKent Overstreet bch2_dev_buckets_alloc(c, ca) || 11761c6fdbd8SKent Overstreet bioset_init(&ca->replica_set, 4, 11771c6fdbd8SKent Overstreet offsetof(struct bch_write_bio, bio), 0) || 11781c6fdbd8SKent Overstreet !(ca->io_done = alloc_percpu(*ca->io_done))) 11791c6fdbd8SKent Overstreet goto err; 11801c6fdbd8SKent Overstreet 11811c6fdbd8SKent Overstreet return ca; 11821c6fdbd8SKent Overstreet err: 11831c6fdbd8SKent Overstreet bch2_dev_free(ca); 11841c6fdbd8SKent Overstreet return NULL; 11851c6fdbd8SKent Overstreet } 11861c6fdbd8SKent Overstreet 11871c6fdbd8SKent Overstreet static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, 11881c6fdbd8SKent Overstreet unsigned dev_idx) 11891c6fdbd8SKent Overstreet { 11901c6fdbd8SKent Overstreet ca->dev_idx = dev_idx; 11911c6fdbd8SKent Overstreet __set_bit(ca->dev_idx, ca->self.d); 11921c6fdbd8SKent Overstreet scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); 11931c6fdbd8SKent Overstreet 11941c6fdbd8SKent Overstreet ca->fs = c; 11951c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], ca); 11961c6fdbd8SKent Overstreet 11971c6fdbd8SKent Overstreet if (bch2_dev_sysfs_online(c, ca)) 11981c6fdbd8SKent Overstreet pr_warn("error creating sysfs objects"); 11991c6fdbd8SKent Overstreet } 12001c6fdbd8SKent Overstreet 12011c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx) 12021c6fdbd8SKent Overstreet { 12031c6fdbd8SKent Overstreet struct bch_member *member = 12041c6fdbd8SKent Overstreet bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx; 12051c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 12061c6fdbd8SKent Overstreet int ret = 0; 12071c6fdbd8SKent Overstreet 12081c6fdbd8SKent Overstreet pr_verbose_init(c->opts, ""); 12091c6fdbd8SKent Overstreet 12101c6fdbd8SKent Overstreet if (bch2_fs_init_fault("dev_alloc")) 12111c6fdbd8SKent Overstreet goto err; 12121c6fdbd8SKent Overstreet 12131c6fdbd8SKent Overstreet ca = __bch2_dev_alloc(c, member); 12141c6fdbd8SKent Overstreet if (!ca) 12151c6fdbd8SKent Overstreet goto err; 12161c6fdbd8SKent Overstreet 1217220d2062SKent Overstreet ca->fs = c; 1218220d2062SKent Overstreet 12192436cb9fSKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw && 122059a74051SKent Overstreet bch2_dev_allocator_start(ca)) { 122159a74051SKent Overstreet bch2_dev_free(ca); 122259a74051SKent Overstreet goto err; 122359a74051SKent Overstreet } 122459a74051SKent Overstreet 12251c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 12261c6fdbd8SKent Overstreet out: 12271c6fdbd8SKent Overstreet pr_verbose_init(c->opts, "ret %i", ret); 12281c6fdbd8SKent Overstreet return ret; 12291c6fdbd8SKent Overstreet err: 12301c6fdbd8SKent Overstreet if (ca) 12311c6fdbd8SKent Overstreet bch2_dev_free(ca); 12321c6fdbd8SKent Overstreet ret = -ENOMEM; 12331c6fdbd8SKent Overstreet goto out; 12341c6fdbd8SKent Overstreet } 12351c6fdbd8SKent Overstreet 12361c6fdbd8SKent Overstreet static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb) 12371c6fdbd8SKent Overstreet { 12381c6fdbd8SKent Overstreet unsigned ret; 12391c6fdbd8SKent Overstreet 12401c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca)) { 12411c6fdbd8SKent Overstreet bch_err(ca, "already have device online in slot %u", 12421c6fdbd8SKent Overstreet sb->sb->dev_idx); 12431c6fdbd8SKent Overstreet return -EINVAL; 12441c6fdbd8SKent Overstreet } 12451c6fdbd8SKent Overstreet 12461c6fdbd8SKent Overstreet if (get_capacity(sb->bdev->bd_disk) < 12471c6fdbd8SKent Overstreet ca->mi.bucket_size * ca->mi.nbuckets) { 12481c6fdbd8SKent Overstreet bch_err(ca, "cannot online: device too small"); 12491c6fdbd8SKent Overstreet return -EINVAL; 12501c6fdbd8SKent Overstreet } 12511c6fdbd8SKent Overstreet 12521c6fdbd8SKent Overstreet BUG_ON(!percpu_ref_is_zero(&ca->io_ref)); 12531c6fdbd8SKent Overstreet 12541c6fdbd8SKent Overstreet if (get_capacity(sb->bdev->bd_disk) < 12551c6fdbd8SKent Overstreet ca->mi.bucket_size * ca->mi.nbuckets) { 12561c6fdbd8SKent Overstreet bch_err(ca, "device too small"); 12571c6fdbd8SKent Overstreet return -EINVAL; 12581c6fdbd8SKent Overstreet } 12591c6fdbd8SKent Overstreet 12601c6fdbd8SKent Overstreet ret = bch2_dev_journal_init(ca, sb->sb); 12611c6fdbd8SKent Overstreet if (ret) 12621c6fdbd8SKent Overstreet return ret; 12631c6fdbd8SKent Overstreet 12641c6fdbd8SKent Overstreet /* Commit: */ 12651c6fdbd8SKent Overstreet ca->disk_sb = *sb; 12661c6fdbd8SKent Overstreet memset(sb, 0, sizeof(*sb)); 12671c6fdbd8SKent Overstreet 1268eacb2574SKent Overstreet ca->dev = ca->disk_sb.bdev->bd_dev; 1269eacb2574SKent Overstreet 12701c6fdbd8SKent Overstreet percpu_ref_reinit(&ca->io_ref); 12711c6fdbd8SKent Overstreet 12721c6fdbd8SKent Overstreet return 0; 12731c6fdbd8SKent Overstreet } 12741c6fdbd8SKent Overstreet 12751c6fdbd8SKent Overstreet static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb) 12761c6fdbd8SKent Overstreet { 12771c6fdbd8SKent Overstreet struct bch_dev *ca; 12781c6fdbd8SKent Overstreet int ret; 12791c6fdbd8SKent Overstreet 12801c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 12811c6fdbd8SKent Overstreet 12821c6fdbd8SKent Overstreet if (le64_to_cpu(sb->sb->seq) > 12831c6fdbd8SKent Overstreet le64_to_cpu(c->disk_sb.sb->seq)) 12841c6fdbd8SKent Overstreet bch2_sb_to_fs(c, sb->sb); 12851c6fdbd8SKent Overstreet 12861c6fdbd8SKent Overstreet BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices || 12871c6fdbd8SKent Overstreet !c->devs[sb->sb->dev_idx]); 12881c6fdbd8SKent Overstreet 12891c6fdbd8SKent Overstreet ca = bch_dev_locked(c, sb->sb->dev_idx); 12901c6fdbd8SKent Overstreet 12911c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, sb); 12921c6fdbd8SKent Overstreet if (ret) 12931c6fdbd8SKent Overstreet return ret; 12941c6fdbd8SKent Overstreet 12951c6fdbd8SKent Overstreet bch2_dev_sysfs_online(c, ca); 12961c6fdbd8SKent Overstreet 12971c6fdbd8SKent Overstreet if (c->sb.nr_devices == 1) 12981c6fdbd8SKent Overstreet snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev); 12991c6fdbd8SKent Overstreet snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev); 13001c6fdbd8SKent Overstreet 13011c6fdbd8SKent Overstreet rebalance_wakeup(c); 13021c6fdbd8SKent Overstreet return 0; 13031c6fdbd8SKent Overstreet } 13041c6fdbd8SKent Overstreet 13051c6fdbd8SKent Overstreet /* Device management: */ 13061c6fdbd8SKent Overstreet 13071c6fdbd8SKent Overstreet /* 13081c6fdbd8SKent Overstreet * Note: this function is also used by the error paths - when a particular 13091c6fdbd8SKent Overstreet * device sees an error, we call it to determine whether we can just set the 13101c6fdbd8SKent Overstreet * device RO, or - if this function returns false - we'll set the whole 13111c6fdbd8SKent Overstreet * filesystem RO: 13121c6fdbd8SKent Overstreet * 13131c6fdbd8SKent Overstreet * XXX: maybe we should be more explicit about whether we're changing state 13141c6fdbd8SKent Overstreet * because we got an error or what have you? 13151c6fdbd8SKent Overstreet */ 13161c6fdbd8SKent Overstreet bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, 13171c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 13181c6fdbd8SKent Overstreet { 13191c6fdbd8SKent Overstreet struct bch_devs_mask new_online_devs; 13201c6fdbd8SKent Overstreet struct bch_dev *ca2; 13211c6fdbd8SKent Overstreet int i, nr_rw = 0, required; 13221c6fdbd8SKent Overstreet 13231c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 13241c6fdbd8SKent Overstreet 13251c6fdbd8SKent Overstreet switch (new_state) { 13262436cb9fSKent Overstreet case BCH_MEMBER_STATE_rw: 13271c6fdbd8SKent Overstreet return true; 13282436cb9fSKent Overstreet case BCH_MEMBER_STATE_ro: 13292436cb9fSKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_rw) 13301c6fdbd8SKent Overstreet return true; 13311c6fdbd8SKent Overstreet 13321c6fdbd8SKent Overstreet /* do we have enough devices to write to? */ 13331c6fdbd8SKent Overstreet for_each_member_device(ca2, c, i) 13341c6fdbd8SKent Overstreet if (ca2 != ca) 13352436cb9fSKent Overstreet nr_rw += ca2->mi.state == BCH_MEMBER_STATE_rw; 13361c6fdbd8SKent Overstreet 13371c6fdbd8SKent Overstreet required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED) 13381c6fdbd8SKent Overstreet ? c->opts.metadata_replicas 13391c6fdbd8SKent Overstreet : c->opts.metadata_replicas_required, 13401c6fdbd8SKent Overstreet !(flags & BCH_FORCE_IF_DATA_DEGRADED) 13411c6fdbd8SKent Overstreet ? c->opts.data_replicas 13421c6fdbd8SKent Overstreet : c->opts.data_replicas_required); 13431c6fdbd8SKent Overstreet 13441c6fdbd8SKent Overstreet return nr_rw >= required; 13452436cb9fSKent Overstreet case BCH_MEMBER_STATE_failed: 13462436cb9fSKent Overstreet case BCH_MEMBER_STATE_spare: 13472436cb9fSKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_rw && 13482436cb9fSKent Overstreet ca->mi.state != BCH_MEMBER_STATE_ro) 13491c6fdbd8SKent Overstreet return true; 13501c6fdbd8SKent Overstreet 13511c6fdbd8SKent Overstreet /* do we have enough devices to read from? */ 13521c6fdbd8SKent Overstreet new_online_devs = bch2_online_devs(c); 13531c6fdbd8SKent Overstreet __clear_bit(ca->dev_idx, new_online_devs.d); 13541c6fdbd8SKent Overstreet 1355fcb3431bSKent Overstreet return bch2_have_enough_devs(c, new_online_devs, flags, false); 13561c6fdbd8SKent Overstreet default: 13571c6fdbd8SKent Overstreet BUG(); 13581c6fdbd8SKent Overstreet } 13591c6fdbd8SKent Overstreet } 13601c6fdbd8SKent Overstreet 13611c6fdbd8SKent Overstreet static bool bch2_fs_may_start(struct bch_fs *c) 13621c6fdbd8SKent Overstreet { 13631c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 13641c6fdbd8SKent Overstreet struct bch_dev *ca; 1365fcb3431bSKent Overstreet unsigned i, flags = 0; 13661c6fdbd8SKent Overstreet 1367fcb3431bSKent Overstreet if (c->opts.very_degraded) 1368fcb3431bSKent Overstreet flags |= BCH_FORCE_IF_DEGRADED|BCH_FORCE_IF_LOST; 1369fcb3431bSKent Overstreet 1370fcb3431bSKent Overstreet if (c->opts.degraded) 1371fcb3431bSKent Overstreet flags |= BCH_FORCE_IF_DEGRADED; 1372fcb3431bSKent Overstreet 1373fcb3431bSKent Overstreet if (!c->opts.degraded && 1374fcb3431bSKent Overstreet !c->opts.very_degraded) { 13751c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 13761c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 13771c6fdbd8SKent Overstreet 13781c6fdbd8SKent Overstreet for (i = 0; i < c->disk_sb.sb->nr_devices; i++) { 13791c6fdbd8SKent Overstreet if (!bch2_dev_exists(c->disk_sb.sb, mi, i)) 13801c6fdbd8SKent Overstreet continue; 13811c6fdbd8SKent Overstreet 13821c6fdbd8SKent Overstreet ca = bch_dev_locked(c, i); 13831c6fdbd8SKent Overstreet 13841c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca) && 13852436cb9fSKent Overstreet (ca->mi.state == BCH_MEMBER_STATE_rw || 13862436cb9fSKent Overstreet ca->mi.state == BCH_MEMBER_STATE_ro)) { 13871c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 13881c6fdbd8SKent Overstreet return false; 13891c6fdbd8SKent Overstreet } 13901c6fdbd8SKent Overstreet } 13911c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 13921c6fdbd8SKent Overstreet } 13931c6fdbd8SKent Overstreet 1394fcb3431bSKent Overstreet return bch2_have_enough_devs(c, bch2_online_devs(c), flags, true); 13951c6fdbd8SKent Overstreet } 13961c6fdbd8SKent Overstreet 13971c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) 13981c6fdbd8SKent Overstreet { 13991c6fdbd8SKent Overstreet /* 140074ed7e56SKent Overstreet * Device going read only means the copygc reserve get smaller, so we 140174ed7e56SKent Overstreet * don't want that happening while copygc is in progress: 140274ed7e56SKent Overstreet */ 140374ed7e56SKent Overstreet bch2_copygc_stop(c); 140474ed7e56SKent Overstreet 140574ed7e56SKent Overstreet /* 14061c6fdbd8SKent Overstreet * The allocator thread itself allocates btree nodes, so stop it first: 14071c6fdbd8SKent Overstreet */ 14081c6fdbd8SKent Overstreet bch2_dev_allocator_stop(ca); 14091c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 14101c6fdbd8SKent Overstreet bch2_dev_journal_stop(&c->journal, ca); 141174ed7e56SKent Overstreet 141274ed7e56SKent Overstreet bch2_copygc_start(c); 14131c6fdbd8SKent Overstreet } 14141c6fdbd8SKent Overstreet 1415e2b60560SKent Overstreet static int __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) 14161c6fdbd8SKent Overstreet { 14171c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 14181c6fdbd8SKent Overstreet 14192436cb9fSKent Overstreet BUG_ON(ca->mi.state != BCH_MEMBER_STATE_rw); 14201c6fdbd8SKent Overstreet 14211c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 14221c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 14231c6fdbd8SKent Overstreet 1424e2b60560SKent Overstreet return bch2_dev_allocator_start(ca); 14251c6fdbd8SKent Overstreet } 14261c6fdbd8SKent Overstreet 14271c6fdbd8SKent Overstreet int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 14281c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 14291c6fdbd8SKent Overstreet { 14301c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 14311c6fdbd8SKent Overstreet int ret = 0; 14321c6fdbd8SKent Overstreet 14331c6fdbd8SKent Overstreet if (ca->mi.state == new_state) 14341c6fdbd8SKent Overstreet return 0; 14351c6fdbd8SKent Overstreet 14361c6fdbd8SKent Overstreet if (!bch2_dev_state_allowed(c, ca, new_state, flags)) 14371c6fdbd8SKent Overstreet return -EINVAL; 14381c6fdbd8SKent Overstreet 14392436cb9fSKent Overstreet if (new_state != BCH_MEMBER_STATE_rw) 14401c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 14411c6fdbd8SKent Overstreet 14422436cb9fSKent Overstreet bch_notice(ca, "%s", bch2_member_states[new_state]); 14431c6fdbd8SKent Overstreet 14441c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 14451c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 14461c6fdbd8SKent Overstreet SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state); 14471c6fdbd8SKent Overstreet bch2_write_super(c); 14481c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 14491c6fdbd8SKent Overstreet 1450e2b60560SKent Overstreet if (new_state == BCH_MEMBER_STATE_rw) 1451e2b60560SKent Overstreet ret = __bch2_dev_read_write(c, ca); 14521c6fdbd8SKent Overstreet 14531c6fdbd8SKent Overstreet rebalance_wakeup(c); 14541c6fdbd8SKent Overstreet 14551c6fdbd8SKent Overstreet return ret; 14561c6fdbd8SKent Overstreet } 14571c6fdbd8SKent Overstreet 14581c6fdbd8SKent Overstreet int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 14591c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 14601c6fdbd8SKent Overstreet { 14611c6fdbd8SKent Overstreet int ret; 14621c6fdbd8SKent Overstreet 14631ada1606SKent Overstreet down_write(&c->state_lock); 14641c6fdbd8SKent Overstreet ret = __bch2_dev_set_state(c, ca, new_state, flags); 14651ada1606SKent Overstreet up_write(&c->state_lock); 14661c6fdbd8SKent Overstreet 14671c6fdbd8SKent Overstreet return ret; 14681c6fdbd8SKent Overstreet } 14691c6fdbd8SKent Overstreet 14701c6fdbd8SKent Overstreet /* Device add/removal: */ 14711c6fdbd8SKent Overstreet 1472c0ebe3e4SKent Overstreet static int bch2_dev_remove_alloc(struct bch_fs *c, struct bch_dev *ca) 14735d20ba48SKent Overstreet { 14745d20ba48SKent Overstreet struct btree_trans trans; 14755d20ba48SKent Overstreet size_t i; 14765d20ba48SKent Overstreet int ret; 14775d20ba48SKent Overstreet 14785d20ba48SKent Overstreet bch2_trans_init(&trans, c, 0, 0); 14795d20ba48SKent Overstreet 14805d20ba48SKent Overstreet for (i = 0; i < ca->mi.nbuckets; i++) { 1481a9cb0a67SKent Overstreet ret = lockrestart_do(&trans, 1482a9cb0a67SKent Overstreet bch2_btree_key_cache_flush(&trans, 1483a9cb0a67SKent Overstreet BTREE_ID_alloc, POS(ca->dev_idx, i))); 14845d20ba48SKent Overstreet if (ret) 14855d20ba48SKent Overstreet break; 14865d20ba48SKent Overstreet } 14875d20ba48SKent Overstreet bch2_trans_exit(&trans); 14885d20ba48SKent Overstreet 1489a9cb0a67SKent Overstreet if (ret) { 1490a9cb0a67SKent Overstreet bch_err(c, "error %i removing dev alloc info", ret); 14915d20ba48SKent Overstreet return ret; 1492a9cb0a67SKent Overstreet } 14935d20ba48SKent Overstreet 149441f8b09eSKent Overstreet return bch2_btree_delete_range(c, BTREE_ID_alloc, 14955d20ba48SKent Overstreet POS(ca->dev_idx, 0), 14965d20ba48SKent Overstreet POS(ca->dev_idx + 1, 0), 1497d248ee56SKent Overstreet 0, NULL); 14985d20ba48SKent Overstreet } 14995d20ba48SKent Overstreet 15001c6fdbd8SKent Overstreet int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) 15011c6fdbd8SKent Overstreet { 15021c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 15031c6fdbd8SKent Overstreet unsigned dev_idx = ca->dev_idx, data; 15041c6fdbd8SKent Overstreet int ret = -EINVAL; 15051c6fdbd8SKent Overstreet 15061ada1606SKent Overstreet down_write(&c->state_lock); 15071c6fdbd8SKent Overstreet 150831ba2cd3SKent Overstreet /* 150931ba2cd3SKent Overstreet * We consume a reference to ca->ref, regardless of whether we succeed 151031ba2cd3SKent Overstreet * or fail: 151131ba2cd3SKent Overstreet */ 151231ba2cd3SKent Overstreet percpu_ref_put(&ca->ref); 15131c6fdbd8SKent Overstreet 15142436cb9fSKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 15151c6fdbd8SKent Overstreet bch_err(ca, "Cannot remove without losing data"); 15161c6fdbd8SKent Overstreet goto err; 15171c6fdbd8SKent Overstreet } 15181c6fdbd8SKent Overstreet 15191c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 15201c6fdbd8SKent Overstreet 15211c6fdbd8SKent Overstreet ret = bch2_dev_data_drop(c, ca->dev_idx, flags); 15221c6fdbd8SKent Overstreet if (ret) { 15231c6fdbd8SKent Overstreet bch_err(ca, "Remove failed: error %i dropping data", ret); 15241c6fdbd8SKent Overstreet goto err; 15251c6fdbd8SKent Overstreet } 15261c6fdbd8SKent Overstreet 15271c6fdbd8SKent Overstreet ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); 15281c6fdbd8SKent Overstreet if (ret) { 15291c6fdbd8SKent Overstreet bch_err(ca, "Remove failed: error %i flushing journal", ret); 15301c6fdbd8SKent Overstreet goto err; 15311c6fdbd8SKent Overstreet } 15321c6fdbd8SKent Overstreet 15335d20ba48SKent Overstreet ret = bch2_dev_remove_alloc(c, ca); 15341c6fdbd8SKent Overstreet if (ret) { 15351c6fdbd8SKent Overstreet bch_err(ca, "Remove failed, error deleting alloc info"); 15361c6fdbd8SKent Overstreet goto err; 15371c6fdbd8SKent Overstreet } 15381c6fdbd8SKent Overstreet 15391c6fdbd8SKent Overstreet /* 15401c6fdbd8SKent Overstreet * must flush all existing journal entries, they might have 15411c6fdbd8SKent Overstreet * (overwritten) keys that point to the device we're removing: 15421c6fdbd8SKent Overstreet */ 15431c6fdbd8SKent Overstreet bch2_journal_flush_all_pins(&c->journal); 154431ba2cd3SKent Overstreet /* 154531ba2cd3SKent Overstreet * hack to ensure bch2_replicas_gc2() clears out entries to this device 154631ba2cd3SKent Overstreet */ 154731ba2cd3SKent Overstreet bch2_journal_meta(&c->journal); 15481c6fdbd8SKent Overstreet ret = bch2_journal_error(&c->journal); 15491c6fdbd8SKent Overstreet if (ret) { 15501c6fdbd8SKent Overstreet bch_err(ca, "Remove failed, journal error"); 15511c6fdbd8SKent Overstreet goto err; 15521c6fdbd8SKent Overstreet } 15531c6fdbd8SKent Overstreet 155431ba2cd3SKent Overstreet ret = bch2_replicas_gc2(c); 155531ba2cd3SKent Overstreet if (ret) { 155631ba2cd3SKent Overstreet bch_err(ca, "Remove failed: error %i from replicas gc", ret); 155731ba2cd3SKent Overstreet goto err; 155831ba2cd3SKent Overstreet } 155931ba2cd3SKent Overstreet 156031ba2cd3SKent Overstreet data = bch2_dev_has_data(c, ca); 156131ba2cd3SKent Overstreet if (data) { 1562fa8e94faSKent Overstreet struct printbuf data_has = PRINTBUF; 156331ba2cd3SKent Overstreet 1564fa8e94faSKent Overstreet bch2_flags_to_text(&data_has, bch2_data_types, data); 1565fa8e94faSKent Overstreet bch_err(ca, "Remove failed, still has data (%s)", data_has.buf); 1566fa8e94faSKent Overstreet printbuf_exit(&data_has); 156731ba2cd3SKent Overstreet ret = -EBUSY; 156831ba2cd3SKent Overstreet goto err; 156931ba2cd3SKent Overstreet } 157031ba2cd3SKent Overstreet 15711c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 15721c6fdbd8SKent Overstreet 15731c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15741c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], NULL); 15751c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15761c6fdbd8SKent Overstreet 15771c6fdbd8SKent Overstreet percpu_ref_kill(&ca->ref); 15781c6fdbd8SKent Overstreet wait_for_completion(&ca->ref_completion); 15791c6fdbd8SKent Overstreet 15801c6fdbd8SKent Overstreet bch2_dev_free(ca); 15811c6fdbd8SKent Overstreet 15821c6fdbd8SKent Overstreet /* 15831c6fdbd8SKent Overstreet * Free this device's slot in the bch_member array - all pointers to 15841c6fdbd8SKent Overstreet * this device must be gone: 15851c6fdbd8SKent Overstreet */ 15861c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15871c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 15881c6fdbd8SKent Overstreet memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid)); 15891c6fdbd8SKent Overstreet 15901c6fdbd8SKent Overstreet bch2_write_super(c); 15911c6fdbd8SKent Overstreet 15921c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 15931ada1606SKent Overstreet up_write(&c->state_lock); 1594180fb49dSKent Overstreet 1595180fb49dSKent Overstreet bch2_dev_usage_journal_reserve(c); 15961c6fdbd8SKent Overstreet return 0; 15971c6fdbd8SKent Overstreet err: 15982436cb9fSKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw && 1599d3bb629dSKent Overstreet !percpu_ref_is_zero(&ca->io_ref)) 16001c6fdbd8SKent Overstreet __bch2_dev_read_write(c, ca); 16011ada1606SKent Overstreet up_write(&c->state_lock); 16021c6fdbd8SKent Overstreet return ret; 16031c6fdbd8SKent Overstreet } 16041c6fdbd8SKent Overstreet 16051c6fdbd8SKent Overstreet /* Add new device to running filesystem: */ 16061c6fdbd8SKent Overstreet int bch2_dev_add(struct bch_fs *c, const char *path) 16071c6fdbd8SKent Overstreet { 16081c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 16091c6fdbd8SKent Overstreet struct bch_sb_handle sb; 16101c6fdbd8SKent Overstreet const char *err; 16111c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 16121c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 16131c6fdbd8SKent Overstreet struct bch_member dev_mi; 16141c6fdbd8SKent Overstreet unsigned dev_idx, nr_devices, u64s; 1615fa8e94faSKent Overstreet struct printbuf errbuf = PRINTBUF; 16161c6fdbd8SKent Overstreet int ret; 16171c6fdbd8SKent Overstreet 16181c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 1619e8536925SKent Overstreet if (ret) { 1620e8536925SKent Overstreet bch_err(c, "device add error: error reading super: %i", ret); 1621efe68e1dSKent Overstreet goto err; 1622e8536925SKent Overstreet } 16231c6fdbd8SKent Overstreet 16241c6fdbd8SKent Overstreet dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx]; 16251c6fdbd8SKent Overstreet 16261c6fdbd8SKent Overstreet err = bch2_dev_may_add(sb.sb, c); 1627e8536925SKent Overstreet if (err) { 1628e8536925SKent Overstreet bch_err(c, "device add error: %s", err); 1629efe68e1dSKent Overstreet ret = -EINVAL; 1630efe68e1dSKent Overstreet goto err; 1631e8536925SKent Overstreet } 16321c6fdbd8SKent Overstreet 16331c6fdbd8SKent Overstreet ca = __bch2_dev_alloc(c, &dev_mi); 16341c6fdbd8SKent Overstreet if (!ca) { 16351c6fdbd8SKent Overstreet bch2_free_super(&sb); 1636efe68e1dSKent Overstreet ret = -ENOMEM; 1637efe68e1dSKent Overstreet goto err; 16381c6fdbd8SKent Overstreet } 16391c6fdbd8SKent Overstreet 16401c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, &sb); 16411c6fdbd8SKent Overstreet if (ret) { 16421c6fdbd8SKent Overstreet bch2_dev_free(ca); 1643efe68e1dSKent Overstreet goto err; 16441c6fdbd8SKent Overstreet } 16451c6fdbd8SKent Overstreet 16461c6fdbd8SKent Overstreet ret = bch2_dev_journal_alloc(ca); 1647e8536925SKent Overstreet if (ret) { 1648e8536925SKent Overstreet bch_err(c, "device add error: journal alloc failed"); 16491c6fdbd8SKent Overstreet goto err; 1650e8536925SKent Overstreet } 16511c6fdbd8SKent Overstreet 16521ada1606SKent Overstreet down_write(&c->state_lock); 16531c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 16541c6fdbd8SKent Overstreet 16551c6fdbd8SKent Overstreet ret = bch2_sb_from_fs(c, ca); 1656e8536925SKent Overstreet if (ret) { 1657e8536925SKent Overstreet bch_err(c, "device add error: new device superblock too small"); 16581c6fdbd8SKent Overstreet goto err_unlock; 1659e8536925SKent Overstreet } 16601c6fdbd8SKent Overstreet 16611c6fdbd8SKent Overstreet mi = bch2_sb_get_members(ca->disk_sb.sb); 16621c6fdbd8SKent Overstreet 16631c6fdbd8SKent Overstreet if (!bch2_sb_resize_members(&ca->disk_sb, 16641c6fdbd8SKent Overstreet le32_to_cpu(mi->field.u64s) + 16651c6fdbd8SKent Overstreet sizeof(dev_mi) / sizeof(u64))) { 1666e8536925SKent Overstreet bch_err(c, "device add error: new device superblock too small"); 16671c6fdbd8SKent Overstreet ret = -ENOSPC; 16681c6fdbd8SKent Overstreet goto err_unlock; 16691c6fdbd8SKent Overstreet } 16701c6fdbd8SKent Overstreet 16711c6fdbd8SKent Overstreet if (dynamic_fault("bcachefs:add:no_slot")) 16721c6fdbd8SKent Overstreet goto no_slot; 16731c6fdbd8SKent Overstreet 16741c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 16751c6fdbd8SKent Overstreet for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++) 16761c6fdbd8SKent Overstreet if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx)) 16771c6fdbd8SKent Overstreet goto have_slot; 16781c6fdbd8SKent Overstreet no_slot: 1679e8536925SKent Overstreet bch_err(c, "device add error: already have maximum number of devices"); 16801c6fdbd8SKent Overstreet ret = -ENOSPC; 16811c6fdbd8SKent Overstreet goto err_unlock; 16821c6fdbd8SKent Overstreet 16831c6fdbd8SKent Overstreet have_slot: 16841c6fdbd8SKent Overstreet nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices); 16851c6fdbd8SKent Overstreet u64s = (sizeof(struct bch_sb_field_members) + 16861c6fdbd8SKent Overstreet sizeof(struct bch_member) * nr_devices) / sizeof(u64); 16871c6fdbd8SKent Overstreet 16881c6fdbd8SKent Overstreet mi = bch2_sb_resize_members(&c->disk_sb, u64s); 1689e8536925SKent Overstreet if (!mi) { 1690e8536925SKent Overstreet bch_err(c, "device add error: no room in superblock for member info"); 1691e8536925SKent Overstreet ret = -ENOSPC; 16921c6fdbd8SKent Overstreet goto err_unlock; 1693e8536925SKent Overstreet } 16941c6fdbd8SKent Overstreet 16951c6fdbd8SKent Overstreet /* success: */ 16961c6fdbd8SKent Overstreet 16971c6fdbd8SKent Overstreet mi->members[dev_idx] = dev_mi; 1698a420eea6STim Schlueter mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds()); 16991c6fdbd8SKent Overstreet c->disk_sb.sb->nr_devices = nr_devices; 17001c6fdbd8SKent Overstreet 17011c6fdbd8SKent Overstreet ca->disk_sb.sb->dev_idx = dev_idx; 17021c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 17031c6fdbd8SKent Overstreet 17041c6fdbd8SKent Overstreet bch2_write_super(c); 17051c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17061c6fdbd8SKent Overstreet 1707180fb49dSKent Overstreet bch2_dev_usage_journal_reserve(c); 1708180fb49dSKent Overstreet 1709d62ab355SKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca); 1710e8536925SKent Overstreet if (ret) { 1711e8536925SKent Overstreet bch_err(c, "device add error: error marking new superblock: %i", ret); 1712bfcf840dSKent Overstreet goto err_late; 1713e8536925SKent Overstreet } 17148d6b6222SKent Overstreet 171509943313SKent Overstreet ca->new_fs_bucket_idx = 0; 171609943313SKent Overstreet 17172436cb9fSKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw) { 1718e2b60560SKent Overstreet ret = __bch2_dev_read_write(c, ca); 1719e8536925SKent Overstreet if (ret) { 1720e8536925SKent Overstreet bch_err(c, "device add error: error going RW on new device: %i", ret); 17211c6fdbd8SKent Overstreet goto err_late; 17221c6fdbd8SKent Overstreet } 1723e8536925SKent Overstreet } 17241c6fdbd8SKent Overstreet 17251ada1606SKent Overstreet up_write(&c->state_lock); 17261c6fdbd8SKent Overstreet return 0; 17271c6fdbd8SKent Overstreet 17281c6fdbd8SKent Overstreet err_unlock: 17291c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17301ada1606SKent Overstreet up_write(&c->state_lock); 17311c6fdbd8SKent Overstreet err: 17321c6fdbd8SKent Overstreet if (ca) 17331c6fdbd8SKent Overstreet bch2_dev_free(ca); 17341c6fdbd8SKent Overstreet bch2_free_super(&sb); 1735fa8e94faSKent Overstreet printbuf_exit(&errbuf); 17361c6fdbd8SKent Overstreet return ret; 17371c6fdbd8SKent Overstreet err_late: 1738bfcf840dSKent Overstreet up_write(&c->state_lock); 1739efe68e1dSKent Overstreet ca = NULL; 1740efe68e1dSKent Overstreet goto err; 17411c6fdbd8SKent Overstreet } 17421c6fdbd8SKent Overstreet 17431c6fdbd8SKent Overstreet /* Hot add existing device to running filesystem: */ 17441c6fdbd8SKent Overstreet int bch2_dev_online(struct bch_fs *c, const char *path) 17451c6fdbd8SKent Overstreet { 17461c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 17471c6fdbd8SKent Overstreet struct bch_sb_handle sb = { NULL }; 17481c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 17491c6fdbd8SKent Overstreet struct bch_dev *ca; 17501c6fdbd8SKent Overstreet unsigned dev_idx; 17511c6fdbd8SKent Overstreet const char *err; 17521c6fdbd8SKent Overstreet int ret; 17531c6fdbd8SKent Overstreet 17541ada1606SKent Overstreet down_write(&c->state_lock); 17551c6fdbd8SKent Overstreet 17561c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 17571c6fdbd8SKent Overstreet if (ret) { 17581ada1606SKent Overstreet up_write(&c->state_lock); 17591c6fdbd8SKent Overstreet return ret; 17601c6fdbd8SKent Overstreet } 17611c6fdbd8SKent Overstreet 17621c6fdbd8SKent Overstreet dev_idx = sb.sb->dev_idx; 17631c6fdbd8SKent Overstreet 17641c6fdbd8SKent Overstreet err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb); 1765e2b60560SKent Overstreet if (err) { 1766e2b60560SKent Overstreet bch_err(c, "error bringing %s online: %s", path, err); 17671c6fdbd8SKent Overstreet goto err; 17681c6fdbd8SKent Overstreet } 17691c6fdbd8SKent Overstreet 1770e2b60560SKent Overstreet ret = bch2_dev_attach_bdev(c, &sb); 1771e2b60560SKent Overstreet if (ret) 1772e2b60560SKent Overstreet goto err; 1773e2b60560SKent Overstreet 17741c6fdbd8SKent Overstreet ca = bch_dev_locked(c, dev_idx); 1775bfcf840dSKent Overstreet 1776e2b60560SKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca); 1777e2b60560SKent Overstreet if (ret) { 1778e2b60560SKent Overstreet bch_err(c, "error bringing %s online: error %i from bch2_trans_mark_dev_sb", 1779e2b60560SKent Overstreet path, ret); 1780bfcf840dSKent Overstreet goto err; 1781bfcf840dSKent Overstreet } 1782bfcf840dSKent Overstreet 17832436cb9fSKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_rw) { 1784e2b60560SKent Overstreet ret = __bch2_dev_read_write(c, ca); 1785e2b60560SKent Overstreet if (ret) 17861c6fdbd8SKent Overstreet goto err; 17871c6fdbd8SKent Overstreet } 17881c6fdbd8SKent Overstreet 17891c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 17901c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 17911c6fdbd8SKent Overstreet 17921c6fdbd8SKent Overstreet mi->members[ca->dev_idx].last_mount = 1793a420eea6STim Schlueter cpu_to_le64(ktime_get_real_seconds()); 17941c6fdbd8SKent Overstreet 17951c6fdbd8SKent Overstreet bch2_write_super(c); 17961c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17971c6fdbd8SKent Overstreet 17981ada1606SKent Overstreet up_write(&c->state_lock); 17991c6fdbd8SKent Overstreet return 0; 18001c6fdbd8SKent Overstreet err: 18011ada1606SKent Overstreet up_write(&c->state_lock); 18021c6fdbd8SKent Overstreet bch2_free_super(&sb); 18031c6fdbd8SKent Overstreet return -EINVAL; 18041c6fdbd8SKent Overstreet } 18051c6fdbd8SKent Overstreet 18061c6fdbd8SKent Overstreet int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) 18071c6fdbd8SKent Overstreet { 18081ada1606SKent Overstreet down_write(&c->state_lock); 18091c6fdbd8SKent Overstreet 18101c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca)) { 18111c6fdbd8SKent Overstreet bch_err(ca, "Already offline"); 18121ada1606SKent Overstreet up_write(&c->state_lock); 18131c6fdbd8SKent Overstreet return 0; 18141c6fdbd8SKent Overstreet } 18151c6fdbd8SKent Overstreet 18162436cb9fSKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_failed, flags)) { 18171c6fdbd8SKent Overstreet bch_err(ca, "Cannot offline required disk"); 18181ada1606SKent Overstreet up_write(&c->state_lock); 18191c6fdbd8SKent Overstreet return -EINVAL; 18201c6fdbd8SKent Overstreet } 18211c6fdbd8SKent Overstreet 18221c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 18231c6fdbd8SKent Overstreet 18241ada1606SKent Overstreet up_write(&c->state_lock); 18251c6fdbd8SKent Overstreet return 0; 18261c6fdbd8SKent Overstreet } 18271c6fdbd8SKent Overstreet 18281c6fdbd8SKent Overstreet int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) 18291c6fdbd8SKent Overstreet { 18301c6fdbd8SKent Overstreet struct bch_member *mi; 18311c6fdbd8SKent Overstreet int ret = 0; 18321c6fdbd8SKent Overstreet 18331ada1606SKent Overstreet down_write(&c->state_lock); 18341c6fdbd8SKent Overstreet 18351c6fdbd8SKent Overstreet if (nbuckets < ca->mi.nbuckets) { 18361c6fdbd8SKent Overstreet bch_err(ca, "Cannot shrink yet"); 18371c6fdbd8SKent Overstreet ret = -EINVAL; 18381c6fdbd8SKent Overstreet goto err; 18391c6fdbd8SKent Overstreet } 18401c6fdbd8SKent Overstreet 18411c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca) && 18421c6fdbd8SKent Overstreet get_capacity(ca->disk_sb.bdev->bd_disk) < 18431c6fdbd8SKent Overstreet ca->mi.bucket_size * nbuckets) { 18441c6fdbd8SKent Overstreet bch_err(ca, "New size larger than device"); 18451c6fdbd8SKent Overstreet ret = -EINVAL; 18461c6fdbd8SKent Overstreet goto err; 18471c6fdbd8SKent Overstreet } 18481c6fdbd8SKent Overstreet 18491c6fdbd8SKent Overstreet ret = bch2_dev_buckets_resize(c, ca, nbuckets); 18501c6fdbd8SKent Overstreet if (ret) { 18511c6fdbd8SKent Overstreet bch_err(ca, "Resize error: %i", ret); 18521c6fdbd8SKent Overstreet goto err; 18531c6fdbd8SKent Overstreet } 18541c6fdbd8SKent Overstreet 1855224ec3e6SKent Overstreet ret = bch2_trans_mark_dev_sb(c, ca); 1856224ec3e6SKent Overstreet if (ret) { 1857224ec3e6SKent Overstreet goto err; 1858224ec3e6SKent Overstreet } 1859224ec3e6SKent Overstreet 18601c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 18611c6fdbd8SKent Overstreet mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx]; 18621c6fdbd8SKent Overstreet mi->nbuckets = cpu_to_le64(nbuckets); 18631c6fdbd8SKent Overstreet 18641c6fdbd8SKent Overstreet bch2_write_super(c); 18651c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 18661c6fdbd8SKent Overstreet 18671c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 18681c6fdbd8SKent Overstreet err: 18691ada1606SKent Overstreet up_write(&c->state_lock); 18701c6fdbd8SKent Overstreet return ret; 18711c6fdbd8SKent Overstreet } 18721c6fdbd8SKent Overstreet 18731c6fdbd8SKent Overstreet /* return with ref on ca->ref: */ 1874bf7e49a4SKent Overstreet struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name) 18751c6fdbd8SKent Overstreet { 18761c6fdbd8SKent Overstreet struct bch_dev *ca; 18771c6fdbd8SKent Overstreet unsigned i; 18781c6fdbd8SKent Overstreet 18793a402c8dSKent Overstreet rcu_read_lock(); 18803a402c8dSKent Overstreet for_each_member_device_rcu(ca, c, i, NULL) 1881bf7e49a4SKent Overstreet if (!strcmp(name, ca->name)) 18821c6fdbd8SKent Overstreet goto found; 18831c6fdbd8SKent Overstreet ca = ERR_PTR(-ENOENT); 18841c6fdbd8SKent Overstreet found: 18853a402c8dSKent Overstreet rcu_read_unlock(); 18863a402c8dSKent Overstreet 18871c6fdbd8SKent Overstreet return ca; 18881c6fdbd8SKent Overstreet } 18891c6fdbd8SKent Overstreet 18901c6fdbd8SKent Overstreet /* Filesystem open: */ 18911c6fdbd8SKent Overstreet 18921c6fdbd8SKent Overstreet struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, 18931c6fdbd8SKent Overstreet struct bch_opts opts) 18941c6fdbd8SKent Overstreet { 18951c6fdbd8SKent Overstreet struct bch_sb_handle *sb = NULL; 18961c6fdbd8SKent Overstreet struct bch_fs *c = NULL; 1897625104eaSKent Overstreet struct bch_sb_field_members *mi; 18981c6fdbd8SKent Overstreet unsigned i, best_sb = 0; 18991c6fdbd8SKent Overstreet const char *err; 1900fa8e94faSKent Overstreet struct printbuf errbuf = PRINTBUF; 1901e2b60560SKent Overstreet int ret = 0; 19021c6fdbd8SKent Overstreet 1903efe68e1dSKent Overstreet if (!try_module_get(THIS_MODULE)) 1904efe68e1dSKent Overstreet return ERR_PTR(-ENODEV); 1905efe68e1dSKent Overstreet 19061c6fdbd8SKent Overstreet pr_verbose_init(opts, ""); 19071c6fdbd8SKent Overstreet 19081c6fdbd8SKent Overstreet if (!nr_devices) { 1909efe68e1dSKent Overstreet ret = -EINVAL; 1910efe68e1dSKent Overstreet goto err; 19111c6fdbd8SKent Overstreet } 19121c6fdbd8SKent Overstreet 19131c6fdbd8SKent Overstreet sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL); 1914e2b60560SKent Overstreet if (!sb) { 1915e2b60560SKent Overstreet ret = -ENOMEM; 19161c6fdbd8SKent Overstreet goto err; 1917e2b60560SKent Overstreet } 19181c6fdbd8SKent Overstreet 19191c6fdbd8SKent Overstreet for (i = 0; i < nr_devices; i++) { 19201c6fdbd8SKent Overstreet ret = bch2_read_super(devices[i], &opts, &sb[i]); 19211c6fdbd8SKent Overstreet if (ret) 19221c6fdbd8SKent Overstreet goto err; 19231c6fdbd8SKent Overstreet 19241c6fdbd8SKent Overstreet } 19251c6fdbd8SKent Overstreet 19261c6fdbd8SKent Overstreet for (i = 1; i < nr_devices; i++) 19271c6fdbd8SKent Overstreet if (le64_to_cpu(sb[i].sb->seq) > 19281c6fdbd8SKent Overstreet le64_to_cpu(sb[best_sb].sb->seq)) 19291c6fdbd8SKent Overstreet best_sb = i; 19301c6fdbd8SKent Overstreet 1931625104eaSKent Overstreet mi = bch2_sb_get_members(sb[best_sb].sb); 1932625104eaSKent Overstreet 1933625104eaSKent Overstreet i = 0; 1934625104eaSKent Overstreet while (i < nr_devices) { 1935625104eaSKent Overstreet if (i != best_sb && 1936625104eaSKent Overstreet !bch2_dev_exists(sb[best_sb].sb, mi, sb[i].sb->dev_idx)) { 1937625104eaSKent Overstreet pr_info("%pg has been removed, skipping", sb[i].bdev); 1938625104eaSKent Overstreet bch2_free_super(&sb[i]); 1939625104eaSKent Overstreet array_remove_item(sb, nr_devices, i); 1940625104eaSKent Overstreet continue; 1941625104eaSKent Overstreet } 1942625104eaSKent Overstreet 19431c6fdbd8SKent Overstreet err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb); 19441c6fdbd8SKent Overstreet if (err) 19451c6fdbd8SKent Overstreet goto err_print; 1946625104eaSKent Overstreet i++; 19471c6fdbd8SKent Overstreet } 19481c6fdbd8SKent Overstreet 19491c6fdbd8SKent Overstreet c = bch2_fs_alloc(sb[best_sb].sb, opts); 19507be9ab63SChris Webb if (IS_ERR(c)) { 19517be9ab63SChris Webb ret = PTR_ERR(c); 19521c6fdbd8SKent Overstreet goto err; 19537be9ab63SChris Webb } 19541c6fdbd8SKent Overstreet 19551ada1606SKent Overstreet down_write(&c->state_lock); 1956e2b60560SKent Overstreet for (i = 0; i < nr_devices; i++) { 1957e2b60560SKent Overstreet ret = bch2_dev_attach_bdev(c, &sb[i]); 1958e2b60560SKent Overstreet if (ret) { 19591ada1606SKent Overstreet up_write(&c->state_lock); 1960e2b60560SKent Overstreet goto err; 1961e2b60560SKent Overstreet } 19621c6fdbd8SKent Overstreet } 19631ada1606SKent Overstreet up_write(&c->state_lock); 19641c6fdbd8SKent Overstreet 19651c6fdbd8SKent Overstreet err = "insufficient devices"; 19661c6fdbd8SKent Overstreet if (!bch2_fs_may_start(c)) 19671c6fdbd8SKent Overstreet goto err_print; 19681c6fdbd8SKent Overstreet 19691c6fdbd8SKent Overstreet if (!c->opts.nostart) { 1970619f5beeSKent Overstreet ret = bch2_fs_start(c); 1971619f5beeSKent Overstreet if (ret) 1972619f5beeSKent Overstreet goto err; 19731c6fdbd8SKent Overstreet } 19741c6fdbd8SKent Overstreet out: 19751c6fdbd8SKent Overstreet kfree(sb); 1976fa8e94faSKent Overstreet printbuf_exit(&errbuf); 19771c6fdbd8SKent Overstreet module_put(THIS_MODULE); 19781c6fdbd8SKent Overstreet pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c)); 19791c6fdbd8SKent Overstreet return c; 19801c6fdbd8SKent Overstreet err_print: 19811c6fdbd8SKent Overstreet pr_err("bch_fs_open err opening %s: %s", 19821c6fdbd8SKent Overstreet devices[0], err); 19831c6fdbd8SKent Overstreet ret = -EINVAL; 19841c6fdbd8SKent Overstreet err: 19857be9ab63SChris Webb if (!IS_ERR_OR_NULL(c)) 19861c6fdbd8SKent Overstreet bch2_fs_stop(c); 1987e2b60560SKent Overstreet if (sb) 19881c6fdbd8SKent Overstreet for (i = 0; i < nr_devices; i++) 19891c6fdbd8SKent Overstreet bch2_free_super(&sb[i]); 19901c6fdbd8SKent Overstreet c = ERR_PTR(ret); 19911c6fdbd8SKent Overstreet goto out; 19921c6fdbd8SKent Overstreet } 19931c6fdbd8SKent Overstreet 19941c6fdbd8SKent Overstreet /* Global interfaces/init */ 19951c6fdbd8SKent Overstreet 19961c6fdbd8SKent Overstreet static void bcachefs_exit(void) 19971c6fdbd8SKent Overstreet { 19981c6fdbd8SKent Overstreet bch2_debug_exit(); 19991c6fdbd8SKent Overstreet bch2_vfs_exit(); 20001c6fdbd8SKent Overstreet bch2_chardev_exit(); 200114ba3706SKent Overstreet bch2_btree_key_cache_exit(); 20021c6fdbd8SKent Overstreet if (bcachefs_kset) 20031c6fdbd8SKent Overstreet kset_unregister(bcachefs_kset); 20041c6fdbd8SKent Overstreet } 20051c6fdbd8SKent Overstreet 20061c6fdbd8SKent Overstreet static int __init bcachefs_init(void) 20071c6fdbd8SKent Overstreet { 20081c6fdbd8SKent Overstreet bch2_bkey_pack_test(); 20091c6fdbd8SKent Overstreet 20101c6fdbd8SKent Overstreet if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) || 201114ba3706SKent Overstreet bch2_btree_key_cache_init() || 20121c6fdbd8SKent Overstreet bch2_chardev_init() || 20131c6fdbd8SKent Overstreet bch2_vfs_init() || 20141c6fdbd8SKent Overstreet bch2_debug_init()) 20151c6fdbd8SKent Overstreet goto err; 20161c6fdbd8SKent Overstreet 20171c6fdbd8SKent Overstreet return 0; 20181c6fdbd8SKent Overstreet err: 20191c6fdbd8SKent Overstreet bcachefs_exit(); 20201c6fdbd8SKent Overstreet return -ENOMEM; 20211c6fdbd8SKent Overstreet } 20221c6fdbd8SKent Overstreet 20231c6fdbd8SKent Overstreet #define BCH_DEBUG_PARAM(name, description) \ 20241c6fdbd8SKent Overstreet bool bch2_##name; \ 20251c6fdbd8SKent Overstreet module_param_named(name, bch2_##name, bool, 0644); \ 20261c6fdbd8SKent Overstreet MODULE_PARM_DESC(name, description); 20271c6fdbd8SKent Overstreet BCH_DEBUG_PARAMS() 20281c6fdbd8SKent Overstreet #undef BCH_DEBUG_PARAM 20291c6fdbd8SKent Overstreet 203026609b61SKent Overstreet unsigned bch2_metadata_version = bcachefs_metadata_version_current; 20311c6fdbd8SKent Overstreet module_param_named(version, bch2_metadata_version, uint, 0400); 20321c6fdbd8SKent Overstreet 20331c6fdbd8SKent Overstreet module_exit(bcachefs_exit); 20341c6fdbd8SKent Overstreet module_init(bcachefs_init); 2035