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" 161c6fdbd8SKent Overstreet #include "btree_update_interior.h" 171c6fdbd8SKent Overstreet #include "btree_io.h" 181c6fdbd8SKent Overstreet #include "chardev.h" 191c6fdbd8SKent Overstreet #include "checksum.h" 201c6fdbd8SKent Overstreet #include "clock.h" 211c6fdbd8SKent Overstreet #include "compress.h" 221c6fdbd8SKent Overstreet #include "debug.h" 231c6fdbd8SKent Overstreet #include "disk_groups.h" 24cd575ddfSKent Overstreet #include "ec.h" 251c6fdbd8SKent Overstreet #include "error.h" 261c6fdbd8SKent Overstreet #include "fs.h" 271c6fdbd8SKent Overstreet #include "fs-io.h" 281c6fdbd8SKent Overstreet #include "fsck.h" 291c6fdbd8SKent Overstreet #include "inode.h" 301c6fdbd8SKent Overstreet #include "io.h" 311c6fdbd8SKent Overstreet #include "journal.h" 321c6fdbd8SKent Overstreet #include "journal_reclaim.h" 331dd7f9d9SKent Overstreet #include "journal_seq_blacklist.h" 341c6fdbd8SKent Overstreet #include "move.h" 351c6fdbd8SKent Overstreet #include "migrate.h" 361c6fdbd8SKent Overstreet #include "movinggc.h" 371c6fdbd8SKent Overstreet #include "quota.h" 381c6fdbd8SKent Overstreet #include "rebalance.h" 391c6fdbd8SKent Overstreet #include "recovery.h" 401c6fdbd8SKent Overstreet #include "replicas.h" 411c6fdbd8SKent Overstreet #include "super.h" 421c6fdbd8SKent Overstreet #include "super-io.h" 431c6fdbd8SKent Overstreet #include "sysfs.h" 441c6fdbd8SKent Overstreet #include "trace.h" 451c6fdbd8SKent Overstreet 461c6fdbd8SKent Overstreet #include <linux/backing-dev.h> 471c6fdbd8SKent Overstreet #include <linux/blkdev.h> 481c6fdbd8SKent Overstreet #include <linux/debugfs.h> 491c6fdbd8SKent Overstreet #include <linux/device.h> 501c6fdbd8SKent Overstreet #include <linux/idr.h> 511c6fdbd8SKent Overstreet #include <linux/kthread.h> 521c6fdbd8SKent Overstreet #include <linux/module.h> 531c6fdbd8SKent Overstreet #include <linux/percpu.h> 541c6fdbd8SKent Overstreet #include <linux/random.h> 551c6fdbd8SKent Overstreet #include <linux/sysfs.h> 561c6fdbd8SKent Overstreet #include <crypto/hash.h> 571c6fdbd8SKent Overstreet 581c6fdbd8SKent Overstreet MODULE_LICENSE("GPL"); 591c6fdbd8SKent Overstreet MODULE_AUTHOR("Kent Overstreet <kent.overstreet@gmail.com>"); 601c6fdbd8SKent Overstreet 611c6fdbd8SKent Overstreet #define KTYPE(type) \ 621c6fdbd8SKent Overstreet static const struct attribute_group type ## _group = { \ 631c6fdbd8SKent Overstreet .attrs = type ## _files \ 641c6fdbd8SKent Overstreet }; \ 651c6fdbd8SKent Overstreet \ 661c6fdbd8SKent Overstreet static const struct attribute_group *type ## _groups[] = { \ 671c6fdbd8SKent Overstreet &type ## _group, \ 681c6fdbd8SKent Overstreet NULL \ 691c6fdbd8SKent Overstreet }; \ 701c6fdbd8SKent Overstreet \ 711c6fdbd8SKent Overstreet static const struct kobj_type type ## _ktype = { \ 721c6fdbd8SKent Overstreet .release = type ## _release, \ 731c6fdbd8SKent Overstreet .sysfs_ops = &type ## _sysfs_ops, \ 741c6fdbd8SKent Overstreet .default_groups = type ## _groups \ 751c6fdbd8SKent Overstreet } 761c6fdbd8SKent Overstreet 771c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *); 781c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *); 791c6fdbd8SKent Overstreet 801c6fdbd8SKent Overstreet static void bch2_fs_internal_release(struct kobject *k) 811c6fdbd8SKent Overstreet { 821c6fdbd8SKent Overstreet } 831c6fdbd8SKent Overstreet 841c6fdbd8SKent Overstreet static void bch2_fs_opts_dir_release(struct kobject *k) 851c6fdbd8SKent Overstreet { 861c6fdbd8SKent Overstreet } 871c6fdbd8SKent Overstreet 881c6fdbd8SKent Overstreet static void bch2_fs_time_stats_release(struct kobject *k) 891c6fdbd8SKent Overstreet { 901c6fdbd8SKent Overstreet } 911c6fdbd8SKent Overstreet 921c6fdbd8SKent Overstreet KTYPE(bch2_fs); 931c6fdbd8SKent Overstreet KTYPE(bch2_fs_internal); 941c6fdbd8SKent Overstreet KTYPE(bch2_fs_opts_dir); 951c6fdbd8SKent Overstreet KTYPE(bch2_fs_time_stats); 961c6fdbd8SKent Overstreet KTYPE(bch2_dev); 971c6fdbd8SKent Overstreet 981c6fdbd8SKent Overstreet static struct kset *bcachefs_kset; 991c6fdbd8SKent Overstreet static LIST_HEAD(bch_fs_list); 1001c6fdbd8SKent Overstreet static DEFINE_MUTEX(bch_fs_list_lock); 1011c6fdbd8SKent Overstreet 1021c6fdbd8SKent Overstreet static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait); 1031c6fdbd8SKent Overstreet 1041c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *); 1051c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *, unsigned); 1061c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *, struct bch_dev *); 1071c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *, struct bch_dev *); 1081c6fdbd8SKent Overstreet 1091c6fdbd8SKent Overstreet struct bch_fs *bch2_dev_to_fs(dev_t dev) 1101c6fdbd8SKent Overstreet { 1111c6fdbd8SKent Overstreet struct bch_fs *c; 1121c6fdbd8SKent Overstreet struct bch_dev *ca; 1131c6fdbd8SKent Overstreet unsigned i; 1141c6fdbd8SKent Overstreet 1151c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 1161c6fdbd8SKent Overstreet rcu_read_lock(); 1171c6fdbd8SKent Overstreet 1181c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 1191c6fdbd8SKent Overstreet for_each_member_device_rcu(ca, c, i, NULL) 1201c6fdbd8SKent Overstreet if (ca->disk_sb.bdev->bd_dev == dev) { 1211c6fdbd8SKent Overstreet closure_get(&c->cl); 1221c6fdbd8SKent Overstreet goto found; 1231c6fdbd8SKent Overstreet } 1241c6fdbd8SKent Overstreet c = NULL; 1251c6fdbd8SKent Overstreet found: 1261c6fdbd8SKent Overstreet rcu_read_unlock(); 1271c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 1281c6fdbd8SKent Overstreet 1291c6fdbd8SKent Overstreet return c; 1301c6fdbd8SKent Overstreet } 1311c6fdbd8SKent Overstreet 1321c6fdbd8SKent Overstreet static struct bch_fs *__bch2_uuid_to_fs(__uuid_t uuid) 1331c6fdbd8SKent Overstreet { 1341c6fdbd8SKent Overstreet struct bch_fs *c; 1351c6fdbd8SKent Overstreet 1361c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 1371c6fdbd8SKent Overstreet 1381c6fdbd8SKent Overstreet list_for_each_entry(c, &bch_fs_list, list) 1391c6fdbd8SKent Overstreet if (!memcmp(&c->disk_sb.sb->uuid, &uuid, sizeof(uuid))) 1401c6fdbd8SKent Overstreet return c; 1411c6fdbd8SKent Overstreet 1421c6fdbd8SKent Overstreet return NULL; 1431c6fdbd8SKent Overstreet } 1441c6fdbd8SKent Overstreet 1451c6fdbd8SKent Overstreet struct bch_fs *bch2_uuid_to_fs(__uuid_t uuid) 1461c6fdbd8SKent Overstreet { 1471c6fdbd8SKent Overstreet struct bch_fs *c; 1481c6fdbd8SKent Overstreet 1491c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 1501c6fdbd8SKent Overstreet c = __bch2_uuid_to_fs(uuid); 1511c6fdbd8SKent Overstreet if (c) 1521c6fdbd8SKent Overstreet closure_get(&c->cl); 1531c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 1541c6fdbd8SKent Overstreet 1551c6fdbd8SKent Overstreet return c; 1561c6fdbd8SKent Overstreet } 1571c6fdbd8SKent Overstreet 1581c6fdbd8SKent Overstreet /* Filesystem RO/RW: */ 1591c6fdbd8SKent Overstreet 1601c6fdbd8SKent Overstreet /* 1611c6fdbd8SKent Overstreet * For startup/shutdown of RW stuff, the dependencies are: 1621c6fdbd8SKent Overstreet * 1631c6fdbd8SKent Overstreet * - foreground writes depend on copygc and rebalance (to free up space) 1641c6fdbd8SKent Overstreet * 1651c6fdbd8SKent Overstreet * - copygc and rebalance depend on mark and sweep gc (they actually probably 1661c6fdbd8SKent Overstreet * don't because they either reserve ahead of time or don't block if 1671c6fdbd8SKent Overstreet * allocations fail, but allocations can require mark and sweep gc to run 1681c6fdbd8SKent Overstreet * because of generation number wraparound) 1691c6fdbd8SKent Overstreet * 1701c6fdbd8SKent Overstreet * - all of the above depends on the allocator threads 1711c6fdbd8SKent Overstreet * 1721c6fdbd8SKent Overstreet * - allocator depends on the journal (when it rewrites prios and gens) 1731c6fdbd8SKent Overstreet */ 1741c6fdbd8SKent Overstreet 1751c6fdbd8SKent Overstreet static void __bch2_fs_read_only(struct bch_fs *c) 1761c6fdbd8SKent Overstreet { 1771c6fdbd8SKent Overstreet struct bch_dev *ca; 178430735cdSKent Overstreet bool wrote; 179d5f70c1fSKent Overstreet unsigned i, clean_passes = 0; 180430735cdSKent Overstreet int ret; 1811c6fdbd8SKent Overstreet 1821c6fdbd8SKent Overstreet bch2_rebalance_stop(c); 1831c6fdbd8SKent Overstreet 1841c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 1851c6fdbd8SKent Overstreet bch2_copygc_stop(ca); 1861c6fdbd8SKent Overstreet 1871c6fdbd8SKent Overstreet bch2_gc_thread_stop(c); 1881c6fdbd8SKent Overstreet 1891c6fdbd8SKent Overstreet /* 1901c6fdbd8SKent Overstreet * Flush journal before stopping allocators, because flushing journal 1911c6fdbd8SKent Overstreet * blacklist entries involves allocating new btree nodes: 1921c6fdbd8SKent Overstreet */ 1931c6fdbd8SKent Overstreet bch2_journal_flush_all_pins(&c->journal); 1941c6fdbd8SKent Overstreet 195b935a8a6SKent Overstreet if (!test_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags)) 196b935a8a6SKent Overstreet goto allocator_not_running; 197b935a8a6SKent Overstreet 198430735cdSKent Overstreet do { 199a0e0bda1SKent Overstreet wrote = false; 200a0e0bda1SKent Overstreet 201460651eeSKent Overstreet ret = bch2_stripes_write(c, BTREE_INSERT_NOCHECK_RW, &wrote) ?: 202460651eeSKent Overstreet bch2_alloc_write(c, BTREE_INSERT_NOCHECK_RW, &wrote); 20361c8d7c8SKent Overstreet 204460651eeSKent Overstreet if (ret && !test_bit(BCH_FS_EMERGENCY_RO, &c->flags)) 205d5f70c1fSKent Overstreet bch2_fs_inconsistent(c, "error writing out alloc info %i", ret); 206460651eeSKent Overstreet 207460651eeSKent Overstreet if (ret) 208d5f70c1fSKent Overstreet break; 209d5f70c1fSKent Overstreet 2101c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 211430735cdSKent Overstreet bch2_dev_allocator_quiesce(c, ca); 2121c6fdbd8SKent Overstreet 2131c6fdbd8SKent Overstreet bch2_journal_flush_all_pins(&c->journal); 2141c6fdbd8SKent Overstreet 2151c6fdbd8SKent Overstreet /* 2161c6fdbd8SKent Overstreet * We need to explicitly wait on btree interior updates to complete 2171c6fdbd8SKent Overstreet * before stopping the journal, flushing all journal pins isn't 2181c6fdbd8SKent Overstreet * sufficient, because in the BTREE_INTERIOR_UPDATING_ROOT case btree 2191c6fdbd8SKent Overstreet * interior updates have to drop their journal pin before they're 2201c6fdbd8SKent Overstreet * fully complete: 2211c6fdbd8SKent Overstreet */ 2221c6fdbd8SKent Overstreet closure_wait_event(&c->btree_interior_update_wait, 2231c6fdbd8SKent Overstreet !bch2_btree_interior_updates_nr_pending(c)); 224d5f70c1fSKent Overstreet 225d5f70c1fSKent Overstreet clean_passes = wrote ? 0 : clean_passes + 1; 226d5f70c1fSKent Overstreet } while (clean_passes < 2); 227b935a8a6SKent Overstreet allocator_not_running: 228430735cdSKent Overstreet for_each_member_device(ca, c, i) 229430735cdSKent Overstreet bch2_dev_allocator_stop(ca); 2301c6fdbd8SKent Overstreet 231b935a8a6SKent Overstreet clear_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags); 232b935a8a6SKent Overstreet 2331c6fdbd8SKent Overstreet bch2_fs_journal_stop(&c->journal); 2341c6fdbd8SKent Overstreet 235430735cdSKent Overstreet /* XXX: mark super that alloc info is persistent */ 236430735cdSKent Overstreet 2371c6fdbd8SKent Overstreet /* 2381c6fdbd8SKent Overstreet * the journal kicks off btree writes via reclaim - wait for in flight 2391c6fdbd8SKent Overstreet * writes after stopping journal: 2401c6fdbd8SKent Overstreet */ 2411c6fdbd8SKent Overstreet if (test_bit(BCH_FS_EMERGENCY_RO, &c->flags)) 2421c6fdbd8SKent Overstreet bch2_btree_flush_all_writes(c); 2431c6fdbd8SKent Overstreet else 2441c6fdbd8SKent Overstreet bch2_btree_verify_flushed(c); 2451c6fdbd8SKent Overstreet 2461c6fdbd8SKent Overstreet /* 2471c6fdbd8SKent Overstreet * After stopping journal: 2481c6fdbd8SKent Overstreet */ 2491c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 2501c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 2511c6fdbd8SKent Overstreet } 2521c6fdbd8SKent Overstreet 2531c6fdbd8SKent Overstreet static void bch2_writes_disabled(struct percpu_ref *writes) 2541c6fdbd8SKent Overstreet { 2551c6fdbd8SKent Overstreet struct bch_fs *c = container_of(writes, struct bch_fs, writes); 2561c6fdbd8SKent Overstreet 2571c6fdbd8SKent Overstreet set_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags); 2581c6fdbd8SKent Overstreet wake_up(&bch_read_only_wait); 2591c6fdbd8SKent Overstreet } 2601c6fdbd8SKent Overstreet 2611c6fdbd8SKent Overstreet void bch2_fs_read_only(struct bch_fs *c) 2621c6fdbd8SKent Overstreet { 263134915f3SKent Overstreet if (!test_bit(BCH_FS_RW, &c->flags)) { 264134915f3SKent Overstreet cancel_delayed_work_sync(&c->journal.reclaim_work); 2651c6fdbd8SKent Overstreet return; 266134915f3SKent Overstreet } 2671c6fdbd8SKent Overstreet 2681c6fdbd8SKent Overstreet BUG_ON(test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags)); 2691c6fdbd8SKent Overstreet 2701c6fdbd8SKent Overstreet /* 2711c6fdbd8SKent Overstreet * Block new foreground-end write operations from starting - any new 2721c6fdbd8SKent Overstreet * writes will return -EROFS: 2731c6fdbd8SKent Overstreet * 2741c6fdbd8SKent Overstreet * (This is really blocking new _allocations_, writes to previously 2751c6fdbd8SKent Overstreet * allocated space can still happen until stopping the allocator in 2761c6fdbd8SKent Overstreet * bch2_dev_allocator_stop()). 2771c6fdbd8SKent Overstreet */ 2781c6fdbd8SKent Overstreet percpu_ref_kill(&c->writes); 2791c6fdbd8SKent Overstreet 2801c6fdbd8SKent Overstreet cancel_delayed_work(&c->pd_controllers_update); 2811c6fdbd8SKent Overstreet 2821c6fdbd8SKent Overstreet /* 2831c6fdbd8SKent Overstreet * If we're not doing an emergency shutdown, we want to wait on 2841c6fdbd8SKent Overstreet * outstanding writes to complete so they don't see spurious errors due 2851c6fdbd8SKent Overstreet * to shutting down the allocator: 2861c6fdbd8SKent Overstreet * 2871c6fdbd8SKent Overstreet * If we are doing an emergency shutdown outstanding writes may 2881c6fdbd8SKent Overstreet * hang until we shutdown the allocator so we don't want to wait 2891c6fdbd8SKent Overstreet * on outstanding writes before shutting everything down - but 2901c6fdbd8SKent Overstreet * we do need to wait on them before returning and signalling 2911c6fdbd8SKent Overstreet * that going RO is complete: 2921c6fdbd8SKent Overstreet */ 2931c6fdbd8SKent Overstreet wait_event(bch_read_only_wait, 2941c6fdbd8SKent Overstreet test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags) || 2951c6fdbd8SKent Overstreet test_bit(BCH_FS_EMERGENCY_RO, &c->flags)); 2961c6fdbd8SKent Overstreet 2971c6fdbd8SKent Overstreet __bch2_fs_read_only(c); 2981c6fdbd8SKent Overstreet 2991c6fdbd8SKent Overstreet wait_event(bch_read_only_wait, 3001c6fdbd8SKent Overstreet test_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags)); 3011c6fdbd8SKent Overstreet 3021c6fdbd8SKent Overstreet clear_bit(BCH_FS_WRITE_DISABLE_COMPLETE, &c->flags); 3031c6fdbd8SKent Overstreet 3041c6fdbd8SKent Overstreet if (!bch2_journal_error(&c->journal) && 3051c6fdbd8SKent Overstreet !test_bit(BCH_FS_ERROR, &c->flags) && 3063aea4342SKent Overstreet !test_bit(BCH_FS_EMERGENCY_RO, &c->flags) && 307a0e0bda1SKent Overstreet test_bit(BCH_FS_STARTED, &c->flags) && 308a0e0bda1SKent Overstreet !c->opts.norecovery) 309134915f3SKent Overstreet bch2_fs_mark_clean(c); 3101c6fdbd8SKent Overstreet 311134915f3SKent Overstreet clear_bit(BCH_FS_RW, &c->flags); 3121c6fdbd8SKent Overstreet } 3131c6fdbd8SKent Overstreet 3141c6fdbd8SKent Overstreet static void bch2_fs_read_only_work(struct work_struct *work) 3151c6fdbd8SKent Overstreet { 3161c6fdbd8SKent Overstreet struct bch_fs *c = 3171c6fdbd8SKent Overstreet container_of(work, struct bch_fs, read_only_work); 3181c6fdbd8SKent Overstreet 3191c6fdbd8SKent Overstreet mutex_lock(&c->state_lock); 3201c6fdbd8SKent Overstreet bch2_fs_read_only(c); 3211c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 3221c6fdbd8SKent Overstreet } 3231c6fdbd8SKent Overstreet 3241c6fdbd8SKent Overstreet static void bch2_fs_read_only_async(struct bch_fs *c) 3251c6fdbd8SKent Overstreet { 3261c6fdbd8SKent Overstreet queue_work(system_long_wq, &c->read_only_work); 3271c6fdbd8SKent Overstreet } 3281c6fdbd8SKent Overstreet 3291c6fdbd8SKent Overstreet bool bch2_fs_emergency_read_only(struct bch_fs *c) 3301c6fdbd8SKent Overstreet { 3311c6fdbd8SKent Overstreet bool ret = !test_and_set_bit(BCH_FS_EMERGENCY_RO, &c->flags); 3321c6fdbd8SKent Overstreet 3331c6fdbd8SKent Overstreet bch2_fs_read_only_async(c); 3341c6fdbd8SKent Overstreet bch2_journal_halt(&c->journal); 3351c6fdbd8SKent Overstreet 3361c6fdbd8SKent Overstreet wake_up(&bch_read_only_wait); 3371c6fdbd8SKent Overstreet return ret; 3381c6fdbd8SKent Overstreet } 3391c6fdbd8SKent Overstreet 340134915f3SKent Overstreet static int bch2_fs_read_write_late(struct bch_fs *c) 3411c6fdbd8SKent Overstreet { 3421c6fdbd8SKent Overstreet struct bch_dev *ca; 3431c6fdbd8SKent Overstreet unsigned i; 344134915f3SKent Overstreet int ret; 3451c6fdbd8SKent Overstreet 346134915f3SKent Overstreet ret = bch2_gc_thread_start(c); 347134915f3SKent Overstreet if (ret) { 348134915f3SKent Overstreet bch_err(c, "error starting gc thread"); 349134915f3SKent Overstreet return ret; 350134915f3SKent Overstreet } 3511c6fdbd8SKent Overstreet 352134915f3SKent Overstreet for_each_rw_member(ca, c, i) { 353134915f3SKent Overstreet ret = bch2_copygc_start(c, ca); 354134915f3SKent Overstreet if (ret) { 355134915f3SKent Overstreet bch_err(c, "error starting copygc threads"); 356134915f3SKent Overstreet percpu_ref_put(&ca->io_ref); 357134915f3SKent Overstreet return ret; 358134915f3SKent Overstreet } 359134915f3SKent Overstreet } 360134915f3SKent Overstreet 361134915f3SKent Overstreet ret = bch2_rebalance_start(c); 362134915f3SKent Overstreet if (ret) { 363134915f3SKent Overstreet bch_err(c, "error starting rebalance thread"); 364134915f3SKent Overstreet return ret; 365134915f3SKent Overstreet } 366134915f3SKent Overstreet 367134915f3SKent Overstreet schedule_delayed_work(&c->pd_controllers_update, 5 * HZ); 368134915f3SKent Overstreet 369*97fd13adSKent Overstreet schedule_work(&c->ec_stripe_delete_work); 370*97fd13adSKent Overstreet 371134915f3SKent Overstreet return 0; 372134915f3SKent Overstreet } 373134915f3SKent Overstreet 37403e183cbSKent Overstreet int __bch2_fs_read_write(struct bch_fs *c, bool early) 375134915f3SKent Overstreet { 376134915f3SKent Overstreet struct bch_dev *ca; 377134915f3SKent Overstreet unsigned i; 378134915f3SKent Overstreet int ret; 379134915f3SKent Overstreet 380134915f3SKent Overstreet if (test_bit(BCH_FS_RW, &c->flags)) 381134915f3SKent Overstreet return 0; 382134915f3SKent Overstreet 383619f5beeSKent Overstreet /* 384619f5beeSKent Overstreet * nochanges is used for fsck -n mode - we have to allow going rw 385619f5beeSKent Overstreet * during recovery for that to work: 386619f5beeSKent Overstreet */ 387619f5beeSKent Overstreet if (c->opts.norecovery || 388619f5beeSKent Overstreet (c->opts.nochanges && 389619f5beeSKent Overstreet (!early || c->opts.read_only))) 390619f5beeSKent Overstreet return -EROFS; 391330581f1SKent Overstreet 392134915f3SKent Overstreet ret = bch2_fs_mark_dirty(c); 393134915f3SKent Overstreet if (ret) 394134915f3SKent Overstreet goto err; 3951c6fdbd8SKent Overstreet 3961c6fdbd8SKent Overstreet for_each_rw_member(ca, c, i) 3971c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 3981c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 3991c6fdbd8SKent Overstreet 400134915f3SKent Overstreet if (!test_bit(BCH_FS_ALLOCATOR_STARTED, &c->flags)) { 401134915f3SKent Overstreet ret = bch2_fs_allocator_start(c); 402134915f3SKent Overstreet if (ret) { 403134915f3SKent Overstreet bch_err(c, "error initializing allocator"); 404134915f3SKent Overstreet goto err; 405134915f3SKent Overstreet } 406134915f3SKent Overstreet 407134915f3SKent Overstreet set_bit(BCH_FS_ALLOCATOR_STARTED, &c->flags); 408134915f3SKent Overstreet } 409134915f3SKent Overstreet 410134915f3SKent Overstreet for_each_rw_member(ca, c, i) { 411134915f3SKent Overstreet ret = bch2_dev_allocator_start(ca); 412134915f3SKent Overstreet if (ret) { 413134915f3SKent Overstreet bch_err(c, "error starting allocator threads"); 4141c6fdbd8SKent Overstreet percpu_ref_put(&ca->io_ref); 4151c6fdbd8SKent Overstreet goto err; 4161c6fdbd8SKent Overstreet } 417134915f3SKent Overstreet } 4181c6fdbd8SKent Overstreet 419b935a8a6SKent Overstreet set_bit(BCH_FS_ALLOCATOR_RUNNING, &c->flags); 420b935a8a6SKent Overstreet 421134915f3SKent Overstreet if (!early) { 422134915f3SKent Overstreet ret = bch2_fs_read_write_late(c); 423134915f3SKent Overstreet if (ret) 4241c6fdbd8SKent Overstreet goto err; 4251c6fdbd8SKent Overstreet } 4261c6fdbd8SKent Overstreet 4271c6fdbd8SKent Overstreet percpu_ref_reinit(&c->writes); 428134915f3SKent Overstreet set_bit(BCH_FS_RW, &c->flags); 4291c6fdbd8SKent Overstreet 430134915f3SKent Overstreet queue_delayed_work(c->journal_reclaim_wq, 431134915f3SKent Overstreet &c->journal.reclaim_work, 0); 432134915f3SKent Overstreet return 0; 4331c6fdbd8SKent Overstreet err: 4341c6fdbd8SKent Overstreet __bch2_fs_read_only(c); 435134915f3SKent Overstreet return ret; 436134915f3SKent Overstreet } 437134915f3SKent Overstreet 438134915f3SKent Overstreet int bch2_fs_read_write(struct bch_fs *c) 439134915f3SKent Overstreet { 440134915f3SKent Overstreet return __bch2_fs_read_write(c, false); 441134915f3SKent Overstreet } 442134915f3SKent Overstreet 443134915f3SKent Overstreet int bch2_fs_read_write_early(struct bch_fs *c) 444134915f3SKent Overstreet { 445134915f3SKent Overstreet lockdep_assert_held(&c->state_lock); 446134915f3SKent Overstreet 447134915f3SKent Overstreet return __bch2_fs_read_write(c, true); 4481c6fdbd8SKent Overstreet } 4491c6fdbd8SKent Overstreet 4501c6fdbd8SKent Overstreet /* Filesystem startup/shutdown: */ 4511c6fdbd8SKent Overstreet 4521c6fdbd8SKent Overstreet static void bch2_fs_free(struct bch_fs *c) 4531c6fdbd8SKent Overstreet { 4541c6fdbd8SKent Overstreet unsigned i; 4551c6fdbd8SKent Overstreet 4561c6fdbd8SKent Overstreet for (i = 0; i < BCH_TIME_STAT_NR; i++) 4571c6fdbd8SKent Overstreet bch2_time_stats_exit(&c->times[i]); 4581c6fdbd8SKent Overstreet 4591c6fdbd8SKent Overstreet bch2_fs_quota_exit(c); 4601c6fdbd8SKent Overstreet bch2_fs_fsio_exit(c); 461cd575ddfSKent Overstreet bch2_fs_ec_exit(c); 4621c6fdbd8SKent Overstreet bch2_fs_encryption_exit(c); 4631c6fdbd8SKent Overstreet bch2_fs_io_exit(c); 4641c6fdbd8SKent Overstreet bch2_fs_btree_cache_exit(c); 4651c6fdbd8SKent Overstreet bch2_fs_journal_exit(&c->journal); 4661c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[WRITE]); 4671c6fdbd8SKent Overstreet bch2_io_clock_exit(&c->io_clock[READ]); 4681c6fdbd8SKent Overstreet bch2_fs_compress_exit(c); 4699166b41dSKent Overstreet percpu_free_rwsem(&c->mark_lock); 4705e82a9a1SKent Overstreet free_percpu(c->online_reserved); 4714d8100daSKent Overstreet kfree(c->usage_scratch); 4725e82a9a1SKent Overstreet free_percpu(c->usage[1]); 4739ca53b55SKent Overstreet free_percpu(c->usage[0]); 4745e82a9a1SKent Overstreet kfree(c->usage_base); 4755663a415SKent Overstreet free_percpu(c->pcpu); 476581edb63SKent Overstreet mempool_exit(&c->btree_iters_pool); 4771c6fdbd8SKent Overstreet mempool_exit(&c->btree_bounce_pool); 4781c6fdbd8SKent Overstreet bioset_exit(&c->btree_bio); 4791c6fdbd8SKent Overstreet mempool_exit(&c->btree_interior_update_pool); 4801c6fdbd8SKent Overstreet mempool_exit(&c->btree_reserve_pool); 4811c6fdbd8SKent Overstreet mempool_exit(&c->fill_iter); 4821c6fdbd8SKent Overstreet percpu_ref_exit(&c->writes); 48373e6ab95SKent Overstreet kfree(c->replicas.entries); 48473e6ab95SKent Overstreet kfree(c->replicas_gc.entries); 4851c6fdbd8SKent Overstreet kfree(rcu_dereference_protected(c->disk_groups, 1)); 4861dd7f9d9SKent Overstreet kfree(c->journal_seq_blacklist_table); 4871c6fdbd8SKent Overstreet 4880519b72dSKent Overstreet if (c->journal_reclaim_wq) 4890519b72dSKent Overstreet destroy_workqueue(c->journal_reclaim_wq); 4901c6fdbd8SKent Overstreet if (c->copygc_wq) 4911c6fdbd8SKent Overstreet destroy_workqueue(c->copygc_wq); 4921c6fdbd8SKent Overstreet if (c->wq) 4931c6fdbd8SKent Overstreet destroy_workqueue(c->wq); 4941c6fdbd8SKent Overstreet 4951c6fdbd8SKent Overstreet free_pages((unsigned long) c->disk_sb.sb, 4961c6fdbd8SKent Overstreet c->disk_sb.page_order); 4971c6fdbd8SKent Overstreet kvpfree(c, sizeof(*c)); 4981c6fdbd8SKent Overstreet module_put(THIS_MODULE); 4991c6fdbd8SKent Overstreet } 5001c6fdbd8SKent Overstreet 5011c6fdbd8SKent Overstreet static void bch2_fs_release(struct kobject *kobj) 5021c6fdbd8SKent Overstreet { 5031c6fdbd8SKent Overstreet struct bch_fs *c = container_of(kobj, struct bch_fs, kobj); 5041c6fdbd8SKent Overstreet 5051c6fdbd8SKent Overstreet bch2_fs_free(c); 5061c6fdbd8SKent Overstreet } 5071c6fdbd8SKent Overstreet 5081c6fdbd8SKent Overstreet void bch2_fs_stop(struct bch_fs *c) 5091c6fdbd8SKent Overstreet { 5101c6fdbd8SKent Overstreet struct bch_dev *ca; 5111c6fdbd8SKent Overstreet unsigned i; 5121c6fdbd8SKent Overstreet 513af1c6871SKent Overstreet bch_verbose(c, "shutting down"); 514af1c6871SKent Overstreet 5151dd7f9d9SKent Overstreet set_bit(BCH_FS_STOPPING, &c->flags); 5161dd7f9d9SKent Overstreet 5171dd7f9d9SKent Overstreet cancel_work_sync(&c->journal_seq_blacklist_gc_work); 5181dd7f9d9SKent Overstreet 5191c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 5201c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs && 5211c6fdbd8SKent Overstreet ca->disk_sb.bdev) 5221c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 5231c6fdbd8SKent Overstreet 5241c6fdbd8SKent Overstreet if (c->kobj.state_in_sysfs) 5251c6fdbd8SKent Overstreet kobject_del(&c->kobj); 5261c6fdbd8SKent Overstreet 5271c6fdbd8SKent Overstreet bch2_fs_debug_exit(c); 5281c6fdbd8SKent Overstreet bch2_fs_chardev_exit(c); 5291c6fdbd8SKent Overstreet 5301c6fdbd8SKent Overstreet kobject_put(&c->time_stats); 5311c6fdbd8SKent Overstreet kobject_put(&c->opts_dir); 5321c6fdbd8SKent Overstreet kobject_put(&c->internal); 5331c6fdbd8SKent Overstreet 5341c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 5351c6fdbd8SKent Overstreet list_del(&c->list); 5361c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 5371c6fdbd8SKent Overstreet 5381c6fdbd8SKent Overstreet closure_sync(&c->cl); 5391c6fdbd8SKent Overstreet closure_debug_destroy(&c->cl); 5401c6fdbd8SKent Overstreet 5411c6fdbd8SKent Overstreet mutex_lock(&c->state_lock); 5421c6fdbd8SKent Overstreet bch2_fs_read_only(c); 5431c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 5441c6fdbd8SKent Overstreet 5451c6fdbd8SKent Overstreet /* btree prefetch might have kicked off reads in the background: */ 5461c6fdbd8SKent Overstreet bch2_btree_flush_all_reads(c); 5471c6fdbd8SKent Overstreet 5481c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 5491c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 5501c6fdbd8SKent Overstreet 5511c6fdbd8SKent Overstreet cancel_work_sync(&c->btree_write_error_work); 5521c6fdbd8SKent Overstreet cancel_delayed_work_sync(&c->pd_controllers_update); 5531c6fdbd8SKent Overstreet cancel_work_sync(&c->read_only_work); 5541c6fdbd8SKent Overstreet 5551c6fdbd8SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) 5561c6fdbd8SKent Overstreet if (c->devs[i]) 5571c6fdbd8SKent Overstreet bch2_dev_free(rcu_dereference_protected(c->devs[i], 1)); 5581c6fdbd8SKent Overstreet 559af1c6871SKent Overstreet bch_verbose(c, "shutdown complete"); 560af1c6871SKent Overstreet 5611c6fdbd8SKent Overstreet kobject_put(&c->kobj); 5621c6fdbd8SKent Overstreet } 5631c6fdbd8SKent Overstreet 5641c6fdbd8SKent Overstreet static const char *bch2_fs_online(struct bch_fs *c) 5651c6fdbd8SKent Overstreet { 5661c6fdbd8SKent Overstreet struct bch_dev *ca; 5671c6fdbd8SKent Overstreet const char *err = NULL; 5681c6fdbd8SKent Overstreet unsigned i; 5691c6fdbd8SKent Overstreet int ret; 5701c6fdbd8SKent Overstreet 5711c6fdbd8SKent Overstreet lockdep_assert_held(&bch_fs_list_lock); 5721c6fdbd8SKent Overstreet 5731c6fdbd8SKent Overstreet if (!list_empty(&c->list)) 5741c6fdbd8SKent Overstreet return NULL; 5751c6fdbd8SKent Overstreet 5761c6fdbd8SKent Overstreet if (__bch2_uuid_to_fs(c->sb.uuid)) 5771c6fdbd8SKent Overstreet return "filesystem UUID already open"; 5781c6fdbd8SKent Overstreet 5791c6fdbd8SKent Overstreet ret = bch2_fs_chardev_init(c); 5801c6fdbd8SKent Overstreet if (ret) 5811c6fdbd8SKent Overstreet return "error creating character device"; 5821c6fdbd8SKent Overstreet 5831c6fdbd8SKent Overstreet bch2_fs_debug_init(c); 5841c6fdbd8SKent Overstreet 5851c6fdbd8SKent Overstreet if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) || 5861c6fdbd8SKent Overstreet kobject_add(&c->internal, &c->kobj, "internal") || 5871c6fdbd8SKent Overstreet kobject_add(&c->opts_dir, &c->kobj, "options") || 5881c6fdbd8SKent Overstreet kobject_add(&c->time_stats, &c->kobj, "time_stats") || 5891c6fdbd8SKent Overstreet bch2_opts_create_sysfs_files(&c->opts_dir)) 5901c6fdbd8SKent Overstreet return "error creating sysfs objects"; 5911c6fdbd8SKent Overstreet 5921c6fdbd8SKent Overstreet mutex_lock(&c->state_lock); 5931c6fdbd8SKent Overstreet 5941c6fdbd8SKent Overstreet err = "error creating sysfs objects"; 5951c6fdbd8SKent Overstreet __for_each_member_device(ca, c, i, NULL) 5961c6fdbd8SKent Overstreet if (bch2_dev_sysfs_online(c, ca)) 5971c6fdbd8SKent Overstreet goto err; 5981c6fdbd8SKent Overstreet 5991c6fdbd8SKent Overstreet list_add(&c->list, &bch_fs_list); 6001c6fdbd8SKent Overstreet err = NULL; 6011c6fdbd8SKent Overstreet err: 6021c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 6031c6fdbd8SKent Overstreet return err; 6041c6fdbd8SKent Overstreet } 6051c6fdbd8SKent Overstreet 6061c6fdbd8SKent Overstreet static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts) 6071c6fdbd8SKent Overstreet { 6081c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 6091c6fdbd8SKent Overstreet struct bch_fs *c; 610ecf37a4aSKent Overstreet unsigned i, iter_size; 6111c6fdbd8SKent Overstreet const char *err; 6121c6fdbd8SKent Overstreet 6131c6fdbd8SKent Overstreet pr_verbose_init(opts, ""); 6141c6fdbd8SKent Overstreet 6151c6fdbd8SKent Overstreet c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO); 6161c6fdbd8SKent Overstreet if (!c) 6171c6fdbd8SKent Overstreet goto out; 6181c6fdbd8SKent Overstreet 6191c6fdbd8SKent Overstreet __module_get(THIS_MODULE); 6201c6fdbd8SKent Overstreet 6211c6fdbd8SKent Overstreet c->minor = -1; 6221c6fdbd8SKent Overstreet c->disk_sb.fs_sb = true; 6231c6fdbd8SKent Overstreet 6241c6fdbd8SKent Overstreet mutex_init(&c->state_lock); 6251c6fdbd8SKent Overstreet mutex_init(&c->sb_lock); 6261c6fdbd8SKent Overstreet mutex_init(&c->replicas_gc_lock); 6271c6fdbd8SKent Overstreet mutex_init(&c->btree_root_lock); 6281c6fdbd8SKent Overstreet INIT_WORK(&c->read_only_work, bch2_fs_read_only_work); 6291c6fdbd8SKent Overstreet 6301c6fdbd8SKent Overstreet init_rwsem(&c->gc_lock); 6311c6fdbd8SKent Overstreet 6321c6fdbd8SKent Overstreet for (i = 0; i < BCH_TIME_STAT_NR; i++) 6331c6fdbd8SKent Overstreet bch2_time_stats_init(&c->times[i]); 6341c6fdbd8SKent Overstreet 635b092daddSKent Overstreet bch2_fs_allocator_background_init(c); 636b092daddSKent Overstreet bch2_fs_allocator_foreground_init(c); 6371c6fdbd8SKent Overstreet bch2_fs_rebalance_init(c); 6381c6fdbd8SKent Overstreet bch2_fs_quota_init(c); 6391c6fdbd8SKent Overstreet 6401c6fdbd8SKent Overstreet INIT_LIST_HEAD(&c->list); 6411c6fdbd8SKent Overstreet 6421c6fdbd8SKent Overstreet INIT_LIST_HEAD(&c->btree_interior_update_list); 6431c6fdbd8SKent Overstreet mutex_init(&c->btree_reserve_cache_lock); 6441c6fdbd8SKent Overstreet mutex_init(&c->btree_interior_update_lock); 6451c6fdbd8SKent Overstreet 6464d8100daSKent Overstreet mutex_init(&c->usage_scratch_lock); 6474d8100daSKent Overstreet 6481c6fdbd8SKent Overstreet mutex_init(&c->bio_bounce_pages_lock); 6491c6fdbd8SKent Overstreet 6501c6fdbd8SKent Overstreet bio_list_init(&c->btree_write_error_list); 6511c6fdbd8SKent Overstreet spin_lock_init(&c->btree_write_error_lock); 6521c6fdbd8SKent Overstreet INIT_WORK(&c->btree_write_error_work, bch2_btree_write_error_work); 6531c6fdbd8SKent Overstreet 6541dd7f9d9SKent Overstreet INIT_WORK(&c->journal_seq_blacklist_gc_work, 6551dd7f9d9SKent Overstreet bch2_blacklist_entries_gc); 6561dd7f9d9SKent Overstreet 6571c6fdbd8SKent Overstreet INIT_LIST_HEAD(&c->fsck_errors); 6581c6fdbd8SKent Overstreet mutex_init(&c->fsck_error_lock); 6591c6fdbd8SKent Overstreet 660cd575ddfSKent Overstreet INIT_LIST_HEAD(&c->ec_new_stripe_list); 661cd575ddfSKent Overstreet mutex_init(&c->ec_new_stripe_lock); 662dfe9bfb3SKent Overstreet mutex_init(&c->ec_stripe_create_lock); 663cd575ddfSKent Overstreet spin_lock_init(&c->ec_stripes_heap_lock); 664cd575ddfSKent Overstreet 6651c6fdbd8SKent Overstreet seqcount_init(&c->gc_pos_lock); 6661c6fdbd8SKent Overstreet 6675e82a9a1SKent Overstreet seqcount_init(&c->usage_lock); 6685e82a9a1SKent Overstreet 6691c6fdbd8SKent Overstreet c->copy_gc_enabled = 1; 6701c6fdbd8SKent Overstreet c->rebalance.enabled = 1; 6711c6fdbd8SKent Overstreet c->promote_whole_extents = true; 6721c6fdbd8SKent Overstreet 6731c6fdbd8SKent Overstreet c->journal.write_time = &c->times[BCH_TIME_journal_write]; 6741c6fdbd8SKent Overstreet c->journal.delay_time = &c->times[BCH_TIME_journal_delay]; 67549a67206SKent Overstreet c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal]; 6761c6fdbd8SKent Overstreet c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq]; 6771c6fdbd8SKent Overstreet 6781c6fdbd8SKent Overstreet bch2_fs_btree_cache_init_early(&c->btree_cache); 6791c6fdbd8SKent Overstreet 680fca1223cSKent Overstreet mutex_init(&c->sectors_available_lock); 681fca1223cSKent Overstreet 68273e6ab95SKent Overstreet if (percpu_init_rwsem(&c->mark_lock)) 68373e6ab95SKent Overstreet goto err; 68473e6ab95SKent Overstreet 6851c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 6861c6fdbd8SKent Overstreet 6871c6fdbd8SKent Overstreet if (bch2_sb_to_fs(c, sb)) { 6881c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 6891c6fdbd8SKent Overstreet goto err; 6901c6fdbd8SKent Overstreet } 6911c6fdbd8SKent Overstreet 6921c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 6931c6fdbd8SKent Overstreet 6941c6fdbd8SKent Overstreet scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid); 6951c6fdbd8SKent Overstreet 6961c6fdbd8SKent Overstreet c->opts = bch2_opts_default; 6971c6fdbd8SKent Overstreet bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb)); 6981c6fdbd8SKent Overstreet bch2_opts_apply(&c->opts, opts); 6991c6fdbd8SKent Overstreet 7001c6fdbd8SKent Overstreet c->block_bits = ilog2(c->opts.block_size); 7011c6fdbd8SKent Overstreet c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c); 7021c6fdbd8SKent Overstreet 7031c6fdbd8SKent Overstreet if (bch2_fs_init_fault("fs_alloc")) 7041c6fdbd8SKent Overstreet goto err; 7051c6fdbd8SKent Overstreet 7061c6fdbd8SKent Overstreet iter_size = sizeof(struct btree_node_iter_large) + 7071c6fdbd8SKent Overstreet (btree_blocks(c) + 1) * 2 * 7081c6fdbd8SKent Overstreet sizeof(struct btree_node_iter_set); 7091c6fdbd8SKent Overstreet 7101c6fdbd8SKent Overstreet if (!(c->wq = alloc_workqueue("bcachefs", 7111c6fdbd8SKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) || 7121c6fdbd8SKent Overstreet !(c->copygc_wq = alloc_workqueue("bcache_copygc", 7131c6fdbd8SKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) || 7140519b72dSKent Overstreet !(c->journal_reclaim_wq = alloc_workqueue("bcache_journal", 7150519b72dSKent Overstreet WQ_FREEZABLE|WQ_MEM_RECLAIM|WQ_HIGHPRI, 1)) || 716134915f3SKent Overstreet percpu_ref_init(&c->writes, bch2_writes_disabled, 717134915f3SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 7181c6fdbd8SKent Overstreet mempool_init_kmalloc_pool(&c->btree_reserve_pool, 1, 7191c6fdbd8SKent Overstreet sizeof(struct btree_reserve)) || 7201c6fdbd8SKent Overstreet mempool_init_kmalloc_pool(&c->btree_interior_update_pool, 1, 7211c6fdbd8SKent Overstreet sizeof(struct btree_update)) || 7221c6fdbd8SKent Overstreet mempool_init_kmalloc_pool(&c->fill_iter, 1, iter_size) || 7231c6fdbd8SKent Overstreet bioset_init(&c->btree_bio, 1, 7241c6fdbd8SKent Overstreet max(offsetof(struct btree_read_bio, bio), 7251c6fdbd8SKent Overstreet offsetof(struct btree_write_bio, wbio.bio)), 7261c6fdbd8SKent Overstreet BIOSET_NEED_BVECS) || 7275663a415SKent Overstreet !(c->pcpu = alloc_percpu(struct bch_fs_pcpu)) || 7285e82a9a1SKent Overstreet !(c->online_reserved = alloc_percpu(u64)) || 7291c6fdbd8SKent Overstreet mempool_init_kvpmalloc_pool(&c->btree_bounce_pool, 1, 7301c6fdbd8SKent Overstreet btree_bytes(c)) || 731581edb63SKent Overstreet mempool_init_kmalloc_pool(&c->btree_iters_pool, 1, 732a8e00bd4SKent Overstreet sizeof(struct btree_iter) * BTREE_ITER_MAX + 733a8e00bd4SKent Overstreet sizeof(struct btree_insert_entry) * 734a8e00bd4SKent Overstreet (BTREE_ITER_MAX + 4)) || 7351c6fdbd8SKent Overstreet bch2_io_clock_init(&c->io_clock[READ]) || 7361c6fdbd8SKent Overstreet bch2_io_clock_init(&c->io_clock[WRITE]) || 7371c6fdbd8SKent Overstreet bch2_fs_journal_init(&c->journal) || 7382c5af169SKent Overstreet bch2_fs_replicas_init(c) || 7391c6fdbd8SKent Overstreet bch2_fs_btree_cache_init(c) || 7401c6fdbd8SKent Overstreet bch2_fs_io_init(c) || 7411c6fdbd8SKent Overstreet bch2_fs_encryption_init(c) || 7421c6fdbd8SKent Overstreet bch2_fs_compress_init(c) || 743cd575ddfSKent Overstreet bch2_fs_ec_init(c) || 7441c6fdbd8SKent Overstreet bch2_fs_fsio_init(c)) 7451c6fdbd8SKent Overstreet goto err; 7461c6fdbd8SKent Overstreet 7471c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 7481c6fdbd8SKent Overstreet for (i = 0; i < c->sb.nr_devices; i++) 7491c6fdbd8SKent Overstreet if (bch2_dev_exists(c->disk_sb.sb, mi, i) && 7501c6fdbd8SKent Overstreet bch2_dev_alloc(c, i)) 7511c6fdbd8SKent Overstreet goto err; 7521c6fdbd8SKent Overstreet 7531c6fdbd8SKent Overstreet /* 7541c6fdbd8SKent Overstreet * Now that all allocations have succeeded, init various refcounty 7551c6fdbd8SKent Overstreet * things that let us shutdown: 7561c6fdbd8SKent Overstreet */ 7571c6fdbd8SKent Overstreet closure_init(&c->cl, NULL); 7581c6fdbd8SKent Overstreet 7591c6fdbd8SKent Overstreet c->kobj.kset = bcachefs_kset; 7601c6fdbd8SKent Overstreet kobject_init(&c->kobj, &bch2_fs_ktype); 7611c6fdbd8SKent Overstreet kobject_init(&c->internal, &bch2_fs_internal_ktype); 7621c6fdbd8SKent Overstreet kobject_init(&c->opts_dir, &bch2_fs_opts_dir_ktype); 7631c6fdbd8SKent Overstreet kobject_init(&c->time_stats, &bch2_fs_time_stats_ktype); 7641c6fdbd8SKent Overstreet 7651c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 7661c6fdbd8SKent Overstreet err = bch2_fs_online(c); 7671c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 7681c6fdbd8SKent Overstreet if (err) { 7691c6fdbd8SKent Overstreet bch_err(c, "bch2_fs_online() error: %s", err); 7701c6fdbd8SKent Overstreet goto err; 7711c6fdbd8SKent Overstreet } 7721c6fdbd8SKent Overstreet out: 7731c6fdbd8SKent Overstreet pr_verbose_init(opts, "ret %i", c ? 0 : -ENOMEM); 7741c6fdbd8SKent Overstreet return c; 7751c6fdbd8SKent Overstreet err: 7761c6fdbd8SKent Overstreet bch2_fs_free(c); 7771c6fdbd8SKent Overstreet c = NULL; 7781c6fdbd8SKent Overstreet goto out; 7791c6fdbd8SKent Overstreet } 7801c6fdbd8SKent Overstreet 781619f5beeSKent Overstreet noinline_for_stack 782619f5beeSKent Overstreet static void print_mount_opts(struct bch_fs *c) 783619f5beeSKent Overstreet { 784619f5beeSKent Overstreet enum bch_opt_id i; 785619f5beeSKent Overstreet char buf[512]; 786619f5beeSKent Overstreet struct printbuf p = PBUF(buf); 787619f5beeSKent Overstreet bool first = true; 788619f5beeSKent Overstreet 789619f5beeSKent Overstreet strcpy(buf, "(null)"); 790619f5beeSKent Overstreet 791619f5beeSKent Overstreet if (c->opts.read_only) { 792619f5beeSKent Overstreet pr_buf(&p, "ro"); 793619f5beeSKent Overstreet first = false; 794619f5beeSKent Overstreet } 795619f5beeSKent Overstreet 796619f5beeSKent Overstreet for (i = 0; i < bch2_opts_nr; i++) { 797619f5beeSKent Overstreet const struct bch_option *opt = &bch2_opt_table[i]; 798619f5beeSKent Overstreet u64 v = bch2_opt_get_by_id(&c->opts, i); 799619f5beeSKent Overstreet 800619f5beeSKent Overstreet if (!(opt->mode & OPT_MOUNT)) 801619f5beeSKent Overstreet continue; 802619f5beeSKent Overstreet 803619f5beeSKent Overstreet if (v == bch2_opt_get_by_id(&bch2_opts_default, i)) 804619f5beeSKent Overstreet continue; 805619f5beeSKent Overstreet 806619f5beeSKent Overstreet if (!first) 807619f5beeSKent Overstreet pr_buf(&p, ","); 808619f5beeSKent Overstreet first = false; 809619f5beeSKent Overstreet bch2_opt_to_text(&p, c, opt, v, OPT_SHOW_MOUNT_STYLE); 810619f5beeSKent Overstreet } 811619f5beeSKent Overstreet 812619f5beeSKent Overstreet bch_info(c, "mounted with opts: %s", buf); 813619f5beeSKent Overstreet } 814619f5beeSKent Overstreet 815619f5beeSKent Overstreet int bch2_fs_start(struct bch_fs *c) 8161c6fdbd8SKent Overstreet { 8171c6fdbd8SKent Overstreet const char *err = "cannot allocate memory"; 8181c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 8191c6fdbd8SKent Overstreet struct bch_dev *ca; 820a420eea6STim Schlueter time64_t now = ktime_get_real_seconds(); 8211c6fdbd8SKent Overstreet unsigned i; 8221c6fdbd8SKent Overstreet int ret = -EINVAL; 8231c6fdbd8SKent Overstreet 8241c6fdbd8SKent Overstreet mutex_lock(&c->state_lock); 8251c6fdbd8SKent Overstreet 826134915f3SKent Overstreet BUG_ON(test_bit(BCH_FS_STARTED, &c->flags)); 8271c6fdbd8SKent Overstreet 8281c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 8291c6fdbd8SKent Overstreet 8301c6fdbd8SKent Overstreet for_each_online_member(ca, c, i) 8311c6fdbd8SKent Overstreet bch2_sb_from_fs(c, ca); 8321c6fdbd8SKent Overstreet 8331c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 8341c6fdbd8SKent Overstreet for_each_online_member(ca, c, i) 8351c6fdbd8SKent Overstreet mi->members[ca->dev_idx].last_mount = cpu_to_le64(now); 8361c6fdbd8SKent Overstreet 8371c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 8381c6fdbd8SKent Overstreet 8391c6fdbd8SKent Overstreet for_each_rw_member(ca, c, i) 8401c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 8411c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 8421c6fdbd8SKent Overstreet 8431c6fdbd8SKent Overstreet ret = BCH_SB_INITIALIZED(c->disk_sb.sb) 8441c6fdbd8SKent Overstreet ? bch2_fs_recovery(c) 8451c6fdbd8SKent Overstreet : bch2_fs_initialize(c); 8461c6fdbd8SKent Overstreet if (ret) 8471c6fdbd8SKent Overstreet goto err; 8481c6fdbd8SKent Overstreet 849cd575ddfSKent Overstreet ret = bch2_opts_check_may_set(c); 850cd575ddfSKent Overstreet if (ret) 851cd575ddfSKent Overstreet goto err; 852cd575ddfSKent Overstreet 8531c6fdbd8SKent Overstreet err = "dynamic fault"; 854619f5beeSKent Overstreet ret = -EINVAL; 8551c6fdbd8SKent Overstreet if (bch2_fs_init_fault("fs_start")) 8561c6fdbd8SKent Overstreet goto err; 8571c6fdbd8SKent Overstreet 858619f5beeSKent Overstreet if (c->opts.read_only || c->opts.nochanges) { 8591c6fdbd8SKent Overstreet bch2_fs_read_only(c); 8601c6fdbd8SKent Overstreet } else { 861134915f3SKent Overstreet err = "error going read write"; 862619f5beeSKent Overstreet ret = !test_bit(BCH_FS_RW, &c->flags) 863619f5beeSKent Overstreet ? bch2_fs_read_write(c) 864619f5beeSKent Overstreet : bch2_fs_read_write_late(c); 865619f5beeSKent Overstreet if (ret) 8661c6fdbd8SKent Overstreet goto err; 8671c6fdbd8SKent Overstreet } 8681c6fdbd8SKent Overstreet 8691c6fdbd8SKent Overstreet set_bit(BCH_FS_STARTED, &c->flags); 870619f5beeSKent Overstreet print_mount_opts(c); 871619f5beeSKent Overstreet ret = 0; 8721c6fdbd8SKent Overstreet out: 8731c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 874619f5beeSKent Overstreet return ret; 8751c6fdbd8SKent Overstreet err: 8761c6fdbd8SKent Overstreet switch (ret) { 8771c6fdbd8SKent Overstreet case BCH_FSCK_ERRORS_NOT_FIXED: 8781c6fdbd8SKent Overstreet bch_err(c, "filesystem contains errors: please report this to the developers"); 8791c6fdbd8SKent Overstreet pr_cont("mount with -o fix_errors to repair\n"); 8801c6fdbd8SKent Overstreet err = "fsck error"; 8811c6fdbd8SKent Overstreet break; 8821c6fdbd8SKent Overstreet case BCH_FSCK_REPAIR_UNIMPLEMENTED: 8831c6fdbd8SKent Overstreet bch_err(c, "filesystem contains errors: please report this to the developers"); 8841c6fdbd8SKent Overstreet pr_cont("repair unimplemented: inform the developers so that it can be added\n"); 8851c6fdbd8SKent Overstreet err = "fsck error"; 8861c6fdbd8SKent Overstreet break; 8871c6fdbd8SKent Overstreet case BCH_FSCK_REPAIR_IMPOSSIBLE: 8881c6fdbd8SKent Overstreet bch_err(c, "filesystem contains errors, but repair impossible"); 8891c6fdbd8SKent Overstreet err = "fsck error"; 8901c6fdbd8SKent Overstreet break; 8911c6fdbd8SKent Overstreet case BCH_FSCK_UNKNOWN_VERSION: 8921c6fdbd8SKent Overstreet err = "unknown metadata version";; 8931c6fdbd8SKent Overstreet break; 8941c6fdbd8SKent Overstreet case -ENOMEM: 8951c6fdbd8SKent Overstreet err = "cannot allocate memory"; 8961c6fdbd8SKent Overstreet break; 8971c6fdbd8SKent Overstreet case -EIO: 8981c6fdbd8SKent Overstreet err = "IO error"; 8991c6fdbd8SKent Overstreet break; 9001c6fdbd8SKent Overstreet } 9011c6fdbd8SKent Overstreet 9029516950cSKent Overstreet if (ret >= 0) 9039516950cSKent Overstreet ret = -EIO; 9041c6fdbd8SKent Overstreet goto out; 9051c6fdbd8SKent Overstreet } 9061c6fdbd8SKent Overstreet 9071c6fdbd8SKent Overstreet static const char *bch2_dev_may_add(struct bch_sb *sb, struct bch_fs *c) 9081c6fdbd8SKent Overstreet { 9091c6fdbd8SKent Overstreet struct bch_sb_field_members *sb_mi; 9101c6fdbd8SKent Overstreet 9111c6fdbd8SKent Overstreet sb_mi = bch2_sb_get_members(sb); 9121c6fdbd8SKent Overstreet if (!sb_mi) 9131c6fdbd8SKent Overstreet return "Invalid superblock: member info area missing"; 9141c6fdbd8SKent Overstreet 9151c6fdbd8SKent Overstreet if (le16_to_cpu(sb->block_size) != c->opts.block_size) 9161c6fdbd8SKent Overstreet return "mismatched block size"; 9171c6fdbd8SKent Overstreet 9181c6fdbd8SKent Overstreet if (le16_to_cpu(sb_mi->members[sb->dev_idx].bucket_size) < 9191c6fdbd8SKent Overstreet BCH_SB_BTREE_NODE_SIZE(c->disk_sb.sb)) 9201c6fdbd8SKent Overstreet return "new cache bucket size is too small"; 9211c6fdbd8SKent Overstreet 9221c6fdbd8SKent Overstreet return NULL; 9231c6fdbd8SKent Overstreet } 9241c6fdbd8SKent Overstreet 9251c6fdbd8SKent Overstreet static const char *bch2_dev_in_fs(struct bch_sb *fs, struct bch_sb *sb) 9261c6fdbd8SKent Overstreet { 9271c6fdbd8SKent Overstreet struct bch_sb *newest = 9281c6fdbd8SKent Overstreet le64_to_cpu(fs->seq) > le64_to_cpu(sb->seq) ? fs : sb; 9291c6fdbd8SKent Overstreet struct bch_sb_field_members *mi = bch2_sb_get_members(newest); 9301c6fdbd8SKent Overstreet 9311c6fdbd8SKent Overstreet if (!uuid_equal(&fs->uuid, &sb->uuid)) 9321c6fdbd8SKent Overstreet return "device not a member of filesystem"; 9331c6fdbd8SKent Overstreet 9341c6fdbd8SKent Overstreet if (!bch2_dev_exists(newest, mi, sb->dev_idx)) 9351c6fdbd8SKent Overstreet return "device has been removed"; 9361c6fdbd8SKent Overstreet 9371c6fdbd8SKent Overstreet if (fs->block_size != sb->block_size) 9381c6fdbd8SKent Overstreet return "mismatched block size"; 9391c6fdbd8SKent Overstreet 9401c6fdbd8SKent Overstreet return NULL; 9411c6fdbd8SKent Overstreet } 9421c6fdbd8SKent Overstreet 9431c6fdbd8SKent Overstreet /* Device startup/shutdown: */ 9441c6fdbd8SKent Overstreet 9451c6fdbd8SKent Overstreet static void bch2_dev_release(struct kobject *kobj) 9461c6fdbd8SKent Overstreet { 9471c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(kobj, struct bch_dev, kobj); 9481c6fdbd8SKent Overstreet 9491c6fdbd8SKent Overstreet kfree(ca); 9501c6fdbd8SKent Overstreet } 9511c6fdbd8SKent Overstreet 9521c6fdbd8SKent Overstreet static void bch2_dev_free(struct bch_dev *ca) 9531c6fdbd8SKent Overstreet { 9541c6fdbd8SKent Overstreet cancel_work_sync(&ca->io_error_work); 9551c6fdbd8SKent Overstreet 9561c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs && 9571c6fdbd8SKent Overstreet ca->disk_sb.bdev) 9581c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 9591c6fdbd8SKent Overstreet 9601c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) 9611c6fdbd8SKent Overstreet kobject_del(&ca->kobj); 9621c6fdbd8SKent Overstreet 9631c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 9641c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 9651c6fdbd8SKent Overstreet 9661c6fdbd8SKent Overstreet free_percpu(ca->io_done); 9671c6fdbd8SKent Overstreet bioset_exit(&ca->replica_set); 9681c6fdbd8SKent Overstreet bch2_dev_buckets_free(ca); 969d1170ce5SKent Overstreet free_page((unsigned long) ca->sb_read_scratch); 9701c6fdbd8SKent Overstreet 9711c6fdbd8SKent Overstreet bch2_time_stats_exit(&ca->io_latency[WRITE]); 9721c6fdbd8SKent Overstreet bch2_time_stats_exit(&ca->io_latency[READ]); 9731c6fdbd8SKent Overstreet 9741c6fdbd8SKent Overstreet percpu_ref_exit(&ca->io_ref); 9751c6fdbd8SKent Overstreet percpu_ref_exit(&ca->ref); 9761c6fdbd8SKent Overstreet kobject_put(&ca->kobj); 9771c6fdbd8SKent Overstreet } 9781c6fdbd8SKent Overstreet 9791c6fdbd8SKent Overstreet static void __bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca) 9801c6fdbd8SKent Overstreet { 9811c6fdbd8SKent Overstreet 9821c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 9831c6fdbd8SKent Overstreet 9841c6fdbd8SKent Overstreet if (percpu_ref_is_zero(&ca->io_ref)) 9851c6fdbd8SKent Overstreet return; 9861c6fdbd8SKent Overstreet 9871c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 9881c6fdbd8SKent Overstreet 9891c6fdbd8SKent Overstreet reinit_completion(&ca->io_ref_completion); 9901c6fdbd8SKent Overstreet percpu_ref_kill(&ca->io_ref); 9911c6fdbd8SKent Overstreet wait_for_completion(&ca->io_ref_completion); 9921c6fdbd8SKent Overstreet 9931c6fdbd8SKent Overstreet if (ca->kobj.state_in_sysfs) { 9941c6fdbd8SKent Overstreet sysfs_remove_link(bdev_kobj(ca->disk_sb.bdev), "bcachefs"); 9951c6fdbd8SKent Overstreet sysfs_remove_link(&ca->kobj, "block"); 9961c6fdbd8SKent Overstreet } 9971c6fdbd8SKent Overstreet 9981c6fdbd8SKent Overstreet bch2_free_super(&ca->disk_sb); 9991c6fdbd8SKent Overstreet bch2_dev_journal_exit(ca); 10001c6fdbd8SKent Overstreet } 10011c6fdbd8SKent Overstreet 10021c6fdbd8SKent Overstreet static void bch2_dev_ref_complete(struct percpu_ref *ref) 10031c6fdbd8SKent Overstreet { 10041c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, ref); 10051c6fdbd8SKent Overstreet 10061c6fdbd8SKent Overstreet complete(&ca->ref_completion); 10071c6fdbd8SKent Overstreet } 10081c6fdbd8SKent Overstreet 10091c6fdbd8SKent Overstreet static void bch2_dev_io_ref_complete(struct percpu_ref *ref) 10101c6fdbd8SKent Overstreet { 10111c6fdbd8SKent Overstreet struct bch_dev *ca = container_of(ref, struct bch_dev, io_ref); 10121c6fdbd8SKent Overstreet 10131c6fdbd8SKent Overstreet complete(&ca->io_ref_completion); 10141c6fdbd8SKent Overstreet } 10151c6fdbd8SKent Overstreet 10161c6fdbd8SKent Overstreet static int bch2_dev_sysfs_online(struct bch_fs *c, struct bch_dev *ca) 10171c6fdbd8SKent Overstreet { 10181c6fdbd8SKent Overstreet int ret; 10191c6fdbd8SKent Overstreet 10201c6fdbd8SKent Overstreet if (!c->kobj.state_in_sysfs) 10211c6fdbd8SKent Overstreet return 0; 10221c6fdbd8SKent Overstreet 10231c6fdbd8SKent Overstreet if (!ca->kobj.state_in_sysfs) { 10241c6fdbd8SKent Overstreet ret = kobject_add(&ca->kobj, &c->kobj, 10251c6fdbd8SKent Overstreet "dev-%u", ca->dev_idx); 10261c6fdbd8SKent Overstreet if (ret) 10271c6fdbd8SKent Overstreet return ret; 10281c6fdbd8SKent Overstreet } 10291c6fdbd8SKent Overstreet 10301c6fdbd8SKent Overstreet if (ca->disk_sb.bdev) { 10311c6fdbd8SKent Overstreet struct kobject *block = bdev_kobj(ca->disk_sb.bdev); 10321c6fdbd8SKent Overstreet 10331c6fdbd8SKent Overstreet ret = sysfs_create_link(block, &ca->kobj, "bcachefs"); 10341c6fdbd8SKent Overstreet if (ret) 10351c6fdbd8SKent Overstreet return ret; 10361c6fdbd8SKent Overstreet 10371c6fdbd8SKent Overstreet ret = sysfs_create_link(&ca->kobj, block, "block"); 10381c6fdbd8SKent Overstreet if (ret) 10391c6fdbd8SKent Overstreet return ret; 10401c6fdbd8SKent Overstreet } 10411c6fdbd8SKent Overstreet 10421c6fdbd8SKent Overstreet return 0; 10431c6fdbd8SKent Overstreet } 10441c6fdbd8SKent Overstreet 10451c6fdbd8SKent Overstreet static struct bch_dev *__bch2_dev_alloc(struct bch_fs *c, 10461c6fdbd8SKent Overstreet struct bch_member *member) 10471c6fdbd8SKent Overstreet { 10481c6fdbd8SKent Overstreet struct bch_dev *ca; 10491c6fdbd8SKent Overstreet 10501c6fdbd8SKent Overstreet ca = kzalloc(sizeof(*ca), GFP_KERNEL); 10511c6fdbd8SKent Overstreet if (!ca) 10521c6fdbd8SKent Overstreet return NULL; 10531c6fdbd8SKent Overstreet 10541c6fdbd8SKent Overstreet kobject_init(&ca->kobj, &bch2_dev_ktype); 10551c6fdbd8SKent Overstreet init_completion(&ca->ref_completion); 10561c6fdbd8SKent Overstreet init_completion(&ca->io_ref_completion); 10571c6fdbd8SKent Overstreet 10581c6fdbd8SKent Overstreet init_rwsem(&ca->bucket_lock); 10591c6fdbd8SKent Overstreet 10601c6fdbd8SKent Overstreet writepoint_init(&ca->copygc_write_point, BCH_DATA_USER); 10611c6fdbd8SKent Overstreet 10621c6fdbd8SKent Overstreet spin_lock_init(&ca->freelist_lock); 10631c6fdbd8SKent Overstreet bch2_dev_copygc_init(ca); 10641c6fdbd8SKent Overstreet 10651c6fdbd8SKent Overstreet INIT_WORK(&ca->io_error_work, bch2_io_error_work); 10661c6fdbd8SKent Overstreet 10671c6fdbd8SKent Overstreet bch2_time_stats_init(&ca->io_latency[READ]); 10681c6fdbd8SKent Overstreet bch2_time_stats_init(&ca->io_latency[WRITE]); 10691c6fdbd8SKent Overstreet 10701c6fdbd8SKent Overstreet ca->mi = bch2_mi_to_cpu(member); 10711c6fdbd8SKent Overstreet ca->uuid = member->uuid; 10721c6fdbd8SKent Overstreet 10731c6fdbd8SKent Overstreet if (opt_defined(c->opts, discard)) 10741c6fdbd8SKent Overstreet ca->mi.discard = opt_get(c->opts, discard); 10751c6fdbd8SKent Overstreet 10761c6fdbd8SKent Overstreet if (percpu_ref_init(&ca->ref, bch2_dev_ref_complete, 10771c6fdbd8SKent Overstreet 0, GFP_KERNEL) || 10781c6fdbd8SKent Overstreet percpu_ref_init(&ca->io_ref, bch2_dev_io_ref_complete, 10791c6fdbd8SKent Overstreet PERCPU_REF_INIT_DEAD, GFP_KERNEL) || 1080d1170ce5SKent Overstreet !(ca->sb_read_scratch = (void *) __get_free_page(GFP_KERNEL)) || 10811c6fdbd8SKent Overstreet bch2_dev_buckets_alloc(c, ca) || 10821c6fdbd8SKent Overstreet bioset_init(&ca->replica_set, 4, 10831c6fdbd8SKent Overstreet offsetof(struct bch_write_bio, bio), 0) || 10841c6fdbd8SKent Overstreet !(ca->io_done = alloc_percpu(*ca->io_done))) 10851c6fdbd8SKent Overstreet goto err; 10861c6fdbd8SKent Overstreet 10871c6fdbd8SKent Overstreet return ca; 10881c6fdbd8SKent Overstreet err: 10891c6fdbd8SKent Overstreet bch2_dev_free(ca); 10901c6fdbd8SKent Overstreet return NULL; 10911c6fdbd8SKent Overstreet } 10921c6fdbd8SKent Overstreet 10931c6fdbd8SKent Overstreet static void bch2_dev_attach(struct bch_fs *c, struct bch_dev *ca, 10941c6fdbd8SKent Overstreet unsigned dev_idx) 10951c6fdbd8SKent Overstreet { 10961c6fdbd8SKent Overstreet ca->dev_idx = dev_idx; 10971c6fdbd8SKent Overstreet __set_bit(ca->dev_idx, ca->self.d); 10981c6fdbd8SKent Overstreet scnprintf(ca->name, sizeof(ca->name), "dev-%u", dev_idx); 10991c6fdbd8SKent Overstreet 11001c6fdbd8SKent Overstreet ca->fs = c; 11011c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], ca); 11021c6fdbd8SKent Overstreet 11031c6fdbd8SKent Overstreet if (bch2_dev_sysfs_online(c, ca)) 11041c6fdbd8SKent Overstreet pr_warn("error creating sysfs objects"); 11051c6fdbd8SKent Overstreet } 11061c6fdbd8SKent Overstreet 11071c6fdbd8SKent Overstreet static int bch2_dev_alloc(struct bch_fs *c, unsigned dev_idx) 11081c6fdbd8SKent Overstreet { 11091c6fdbd8SKent Overstreet struct bch_member *member = 11101c6fdbd8SKent Overstreet bch2_sb_get_members(c->disk_sb.sb)->members + dev_idx; 11111c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 11121c6fdbd8SKent Overstreet int ret = 0; 11131c6fdbd8SKent Overstreet 11141c6fdbd8SKent Overstreet pr_verbose_init(c->opts, ""); 11151c6fdbd8SKent Overstreet 11161c6fdbd8SKent Overstreet if (bch2_fs_init_fault("dev_alloc")) 11171c6fdbd8SKent Overstreet goto err; 11181c6fdbd8SKent Overstreet 11191c6fdbd8SKent Overstreet ca = __bch2_dev_alloc(c, member); 11201c6fdbd8SKent Overstreet if (!ca) 11211c6fdbd8SKent Overstreet goto err; 11221c6fdbd8SKent Overstreet 11231c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 11241c6fdbd8SKent Overstreet out: 11251c6fdbd8SKent Overstreet pr_verbose_init(c->opts, "ret %i", ret); 11261c6fdbd8SKent Overstreet return ret; 11271c6fdbd8SKent Overstreet err: 11281c6fdbd8SKent Overstreet if (ca) 11291c6fdbd8SKent Overstreet bch2_dev_free(ca); 11301c6fdbd8SKent Overstreet ret = -ENOMEM; 11311c6fdbd8SKent Overstreet goto out; 11321c6fdbd8SKent Overstreet } 11331c6fdbd8SKent Overstreet 11341c6fdbd8SKent Overstreet static int __bch2_dev_attach_bdev(struct bch_dev *ca, struct bch_sb_handle *sb) 11351c6fdbd8SKent Overstreet { 11361c6fdbd8SKent Overstreet unsigned ret; 11371c6fdbd8SKent Overstreet 11381c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca)) { 11391c6fdbd8SKent Overstreet bch_err(ca, "already have device online in slot %u", 11401c6fdbd8SKent Overstreet sb->sb->dev_idx); 11411c6fdbd8SKent Overstreet return -EINVAL; 11421c6fdbd8SKent Overstreet } 11431c6fdbd8SKent Overstreet 11441c6fdbd8SKent Overstreet if (get_capacity(sb->bdev->bd_disk) < 11451c6fdbd8SKent Overstreet ca->mi.bucket_size * ca->mi.nbuckets) { 11461c6fdbd8SKent Overstreet bch_err(ca, "cannot online: device too small"); 11471c6fdbd8SKent Overstreet return -EINVAL; 11481c6fdbd8SKent Overstreet } 11491c6fdbd8SKent Overstreet 11501c6fdbd8SKent Overstreet BUG_ON(!percpu_ref_is_zero(&ca->io_ref)); 11511c6fdbd8SKent Overstreet 11521c6fdbd8SKent Overstreet if (get_capacity(sb->bdev->bd_disk) < 11531c6fdbd8SKent Overstreet ca->mi.bucket_size * ca->mi.nbuckets) { 11541c6fdbd8SKent Overstreet bch_err(ca, "device too small"); 11551c6fdbd8SKent Overstreet return -EINVAL; 11561c6fdbd8SKent Overstreet } 11571c6fdbd8SKent Overstreet 11581c6fdbd8SKent Overstreet ret = bch2_dev_journal_init(ca, sb->sb); 11591c6fdbd8SKent Overstreet if (ret) 11601c6fdbd8SKent Overstreet return ret; 11611c6fdbd8SKent Overstreet 11621c6fdbd8SKent Overstreet /* Commit: */ 11631c6fdbd8SKent Overstreet ca->disk_sb = *sb; 11641c6fdbd8SKent Overstreet memset(sb, 0, sizeof(*sb)); 11651c6fdbd8SKent Overstreet 11661c6fdbd8SKent Overstreet percpu_ref_reinit(&ca->io_ref); 11671c6fdbd8SKent Overstreet 11681c6fdbd8SKent Overstreet return 0; 11691c6fdbd8SKent Overstreet } 11701c6fdbd8SKent Overstreet 11711c6fdbd8SKent Overstreet static int bch2_dev_attach_bdev(struct bch_fs *c, struct bch_sb_handle *sb) 11721c6fdbd8SKent Overstreet { 11731c6fdbd8SKent Overstreet struct bch_dev *ca; 11741c6fdbd8SKent Overstreet int ret; 11751c6fdbd8SKent Overstreet 11761c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 11771c6fdbd8SKent Overstreet 11781c6fdbd8SKent Overstreet if (le64_to_cpu(sb->sb->seq) > 11791c6fdbd8SKent Overstreet le64_to_cpu(c->disk_sb.sb->seq)) 11801c6fdbd8SKent Overstreet bch2_sb_to_fs(c, sb->sb); 11811c6fdbd8SKent Overstreet 11821c6fdbd8SKent Overstreet BUG_ON(sb->sb->dev_idx >= c->sb.nr_devices || 11831c6fdbd8SKent Overstreet !c->devs[sb->sb->dev_idx]); 11841c6fdbd8SKent Overstreet 11851c6fdbd8SKent Overstreet ca = bch_dev_locked(c, sb->sb->dev_idx); 11861c6fdbd8SKent Overstreet 11871c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, sb); 11881c6fdbd8SKent Overstreet if (ret) 11891c6fdbd8SKent Overstreet return ret; 11901c6fdbd8SKent Overstreet 11913e0745e2SKent Overstreet if (test_bit(BCH_FS_ALLOC_READ_DONE, &c->flags) && 11923e0745e2SKent Overstreet !percpu_u64_get(&ca->usage[0]->buckets[BCH_DATA_SB])) { 11936eac2c2eSKent Overstreet mutex_lock(&c->sb_lock); 11949ca53b55SKent Overstreet bch2_mark_dev_superblock(ca->fs, ca, 0); 11956eac2c2eSKent Overstreet mutex_unlock(&c->sb_lock); 11963e0745e2SKent Overstreet } 11976eac2c2eSKent Overstreet 11981c6fdbd8SKent Overstreet bch2_dev_sysfs_online(c, ca); 11991c6fdbd8SKent Overstreet 12001c6fdbd8SKent Overstreet if (c->sb.nr_devices == 1) 12011c6fdbd8SKent Overstreet snprintf(c->name, sizeof(c->name), "%pg", ca->disk_sb.bdev); 12021c6fdbd8SKent Overstreet snprintf(ca->name, sizeof(ca->name), "%pg", ca->disk_sb.bdev); 12031c6fdbd8SKent Overstreet 12041c6fdbd8SKent Overstreet rebalance_wakeup(c); 12051c6fdbd8SKent Overstreet return 0; 12061c6fdbd8SKent Overstreet } 12071c6fdbd8SKent Overstreet 12081c6fdbd8SKent Overstreet /* Device management: */ 12091c6fdbd8SKent Overstreet 12101c6fdbd8SKent Overstreet /* 12111c6fdbd8SKent Overstreet * Note: this function is also used by the error paths - when a particular 12121c6fdbd8SKent Overstreet * device sees an error, we call it to determine whether we can just set the 12131c6fdbd8SKent Overstreet * device RO, or - if this function returns false - we'll set the whole 12141c6fdbd8SKent Overstreet * filesystem RO: 12151c6fdbd8SKent Overstreet * 12161c6fdbd8SKent Overstreet * XXX: maybe we should be more explicit about whether we're changing state 12171c6fdbd8SKent Overstreet * because we got an error or what have you? 12181c6fdbd8SKent Overstreet */ 12191c6fdbd8SKent Overstreet bool bch2_dev_state_allowed(struct bch_fs *c, struct bch_dev *ca, 12201c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 12211c6fdbd8SKent Overstreet { 12221c6fdbd8SKent Overstreet struct bch_devs_mask new_online_devs; 12231c6fdbd8SKent Overstreet struct replicas_status s; 12241c6fdbd8SKent Overstreet struct bch_dev *ca2; 12251c6fdbd8SKent Overstreet int i, nr_rw = 0, required; 12261c6fdbd8SKent Overstreet 12271c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 12281c6fdbd8SKent Overstreet 12291c6fdbd8SKent Overstreet switch (new_state) { 12301c6fdbd8SKent Overstreet case BCH_MEMBER_STATE_RW: 12311c6fdbd8SKent Overstreet return true; 12321c6fdbd8SKent Overstreet case BCH_MEMBER_STATE_RO: 12331c6fdbd8SKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_RW) 12341c6fdbd8SKent Overstreet return true; 12351c6fdbd8SKent Overstreet 12361c6fdbd8SKent Overstreet /* do we have enough devices to write to? */ 12371c6fdbd8SKent Overstreet for_each_member_device(ca2, c, i) 12381c6fdbd8SKent Overstreet if (ca2 != ca) 12391c6fdbd8SKent Overstreet nr_rw += ca2->mi.state == BCH_MEMBER_STATE_RW; 12401c6fdbd8SKent Overstreet 12411c6fdbd8SKent Overstreet required = max(!(flags & BCH_FORCE_IF_METADATA_DEGRADED) 12421c6fdbd8SKent Overstreet ? c->opts.metadata_replicas 12431c6fdbd8SKent Overstreet : c->opts.metadata_replicas_required, 12441c6fdbd8SKent Overstreet !(flags & BCH_FORCE_IF_DATA_DEGRADED) 12451c6fdbd8SKent Overstreet ? c->opts.data_replicas 12461c6fdbd8SKent Overstreet : c->opts.data_replicas_required); 12471c6fdbd8SKent Overstreet 12481c6fdbd8SKent Overstreet return nr_rw >= required; 12491c6fdbd8SKent Overstreet case BCH_MEMBER_STATE_FAILED: 12501c6fdbd8SKent Overstreet case BCH_MEMBER_STATE_SPARE: 12511c6fdbd8SKent Overstreet if (ca->mi.state != BCH_MEMBER_STATE_RW && 12521c6fdbd8SKent Overstreet ca->mi.state != BCH_MEMBER_STATE_RO) 12531c6fdbd8SKent Overstreet return true; 12541c6fdbd8SKent Overstreet 12551c6fdbd8SKent Overstreet /* do we have enough devices to read from? */ 12561c6fdbd8SKent Overstreet new_online_devs = bch2_online_devs(c); 12571c6fdbd8SKent Overstreet __clear_bit(ca->dev_idx, new_online_devs.d); 12581c6fdbd8SKent Overstreet 12591c6fdbd8SKent Overstreet s = __bch2_replicas_status(c, new_online_devs); 12601c6fdbd8SKent Overstreet 12611c6fdbd8SKent Overstreet return bch2_have_enough_devs(s, flags); 12621c6fdbd8SKent Overstreet default: 12631c6fdbd8SKent Overstreet BUG(); 12641c6fdbd8SKent Overstreet } 12651c6fdbd8SKent Overstreet } 12661c6fdbd8SKent Overstreet 12671c6fdbd8SKent Overstreet static bool bch2_fs_may_start(struct bch_fs *c) 12681c6fdbd8SKent Overstreet { 12691c6fdbd8SKent Overstreet struct replicas_status s; 12701c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 12711c6fdbd8SKent Overstreet struct bch_dev *ca; 12721c6fdbd8SKent Overstreet unsigned i, flags = c->opts.degraded 12731c6fdbd8SKent Overstreet ? BCH_FORCE_IF_DEGRADED 12741c6fdbd8SKent Overstreet : 0; 12751c6fdbd8SKent Overstreet 12761c6fdbd8SKent Overstreet if (!c->opts.degraded) { 12771c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 12781c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 12791c6fdbd8SKent Overstreet 12801c6fdbd8SKent Overstreet for (i = 0; i < c->disk_sb.sb->nr_devices; i++) { 12811c6fdbd8SKent Overstreet if (!bch2_dev_exists(c->disk_sb.sb, mi, i)) 12821c6fdbd8SKent Overstreet continue; 12831c6fdbd8SKent Overstreet 12841c6fdbd8SKent Overstreet ca = bch_dev_locked(c, i); 12851c6fdbd8SKent Overstreet 12861c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca) && 12871c6fdbd8SKent Overstreet (ca->mi.state == BCH_MEMBER_STATE_RW || 12881c6fdbd8SKent Overstreet ca->mi.state == BCH_MEMBER_STATE_RO)) { 12891c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 12901c6fdbd8SKent Overstreet return false; 12911c6fdbd8SKent Overstreet } 12921c6fdbd8SKent Overstreet } 12931c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 12941c6fdbd8SKent Overstreet } 12951c6fdbd8SKent Overstreet 12961c6fdbd8SKent Overstreet s = bch2_replicas_status(c); 12971c6fdbd8SKent Overstreet 12981c6fdbd8SKent Overstreet return bch2_have_enough_devs(s, flags); 12991c6fdbd8SKent Overstreet } 13001c6fdbd8SKent Overstreet 13011c6fdbd8SKent Overstreet static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca) 13021c6fdbd8SKent Overstreet { 13031c6fdbd8SKent Overstreet bch2_copygc_stop(ca); 13041c6fdbd8SKent Overstreet 13051c6fdbd8SKent Overstreet /* 13061c6fdbd8SKent Overstreet * The allocator thread itself allocates btree nodes, so stop it first: 13071c6fdbd8SKent Overstreet */ 13081c6fdbd8SKent Overstreet bch2_dev_allocator_stop(ca); 13091c6fdbd8SKent Overstreet bch2_dev_allocator_remove(c, ca); 13101c6fdbd8SKent Overstreet bch2_dev_journal_stop(&c->journal, ca); 13111c6fdbd8SKent Overstreet } 13121c6fdbd8SKent Overstreet 13131c6fdbd8SKent Overstreet static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca) 13141c6fdbd8SKent Overstreet { 13151c6fdbd8SKent Overstreet lockdep_assert_held(&c->state_lock); 13161c6fdbd8SKent Overstreet 13171c6fdbd8SKent Overstreet BUG_ON(ca->mi.state != BCH_MEMBER_STATE_RW); 13181c6fdbd8SKent Overstreet 13191c6fdbd8SKent Overstreet bch2_dev_allocator_add(c, ca); 13201c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 13211c6fdbd8SKent Overstreet 13221c6fdbd8SKent Overstreet if (bch2_dev_allocator_start(ca)) 13231c6fdbd8SKent Overstreet return "error starting allocator thread"; 13241c6fdbd8SKent Overstreet 13251c6fdbd8SKent Overstreet if (bch2_copygc_start(c, ca)) 13261c6fdbd8SKent Overstreet return "error starting copygc thread"; 13271c6fdbd8SKent Overstreet 13281c6fdbd8SKent Overstreet return NULL; 13291c6fdbd8SKent Overstreet } 13301c6fdbd8SKent Overstreet 13311c6fdbd8SKent Overstreet int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 13321c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 13331c6fdbd8SKent Overstreet { 13341c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 13351c6fdbd8SKent Overstreet int ret = 0; 13361c6fdbd8SKent Overstreet 13371c6fdbd8SKent Overstreet if (ca->mi.state == new_state) 13381c6fdbd8SKent Overstreet return 0; 13391c6fdbd8SKent Overstreet 13401c6fdbd8SKent Overstreet if (!bch2_dev_state_allowed(c, ca, new_state, flags)) 13411c6fdbd8SKent Overstreet return -EINVAL; 13421c6fdbd8SKent Overstreet 13431c6fdbd8SKent Overstreet if (new_state != BCH_MEMBER_STATE_RW) 13441c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 13451c6fdbd8SKent Overstreet 13461c6fdbd8SKent Overstreet bch_notice(ca, "%s", bch2_dev_state[new_state]); 13471c6fdbd8SKent Overstreet 13481c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 13491c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 13501c6fdbd8SKent Overstreet SET_BCH_MEMBER_STATE(&mi->members[ca->dev_idx], new_state); 13511c6fdbd8SKent Overstreet bch2_write_super(c); 13521c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 13531c6fdbd8SKent Overstreet 13541c6fdbd8SKent Overstreet if (new_state == BCH_MEMBER_STATE_RW && 13551c6fdbd8SKent Overstreet __bch2_dev_read_write(c, ca)) 13561c6fdbd8SKent Overstreet ret = -ENOMEM; 13571c6fdbd8SKent Overstreet 13581c6fdbd8SKent Overstreet rebalance_wakeup(c); 13591c6fdbd8SKent Overstreet 13601c6fdbd8SKent Overstreet return ret; 13611c6fdbd8SKent Overstreet } 13621c6fdbd8SKent Overstreet 13631c6fdbd8SKent Overstreet int bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca, 13641c6fdbd8SKent Overstreet enum bch_member_state new_state, int flags) 13651c6fdbd8SKent Overstreet { 13661c6fdbd8SKent Overstreet int ret; 13671c6fdbd8SKent Overstreet 13681c6fdbd8SKent Overstreet mutex_lock(&c->state_lock); 13691c6fdbd8SKent Overstreet ret = __bch2_dev_set_state(c, ca, new_state, flags); 13701c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 13711c6fdbd8SKent Overstreet 13721c6fdbd8SKent Overstreet return ret; 13731c6fdbd8SKent Overstreet } 13741c6fdbd8SKent Overstreet 13751c6fdbd8SKent Overstreet /* Device add/removal: */ 13761c6fdbd8SKent Overstreet 13771c6fdbd8SKent Overstreet int bch2_dev_remove(struct bch_fs *c, struct bch_dev *ca, int flags) 13781c6fdbd8SKent Overstreet { 13791c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 13801c6fdbd8SKent Overstreet unsigned dev_idx = ca->dev_idx, data; 13811c6fdbd8SKent Overstreet int ret = -EINVAL; 13821c6fdbd8SKent Overstreet 13831c6fdbd8SKent Overstreet mutex_lock(&c->state_lock); 13841c6fdbd8SKent Overstreet 13851c6fdbd8SKent Overstreet percpu_ref_put(&ca->ref); /* XXX */ 13861c6fdbd8SKent Overstreet 13871c6fdbd8SKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) { 13881c6fdbd8SKent Overstreet bch_err(ca, "Cannot remove without losing data"); 13891c6fdbd8SKent Overstreet goto err; 13901c6fdbd8SKent Overstreet } 13911c6fdbd8SKent Overstreet 13921c6fdbd8SKent Overstreet __bch2_dev_read_only(c, ca); 13931c6fdbd8SKent Overstreet 13941c6fdbd8SKent Overstreet /* 13951c6fdbd8SKent Overstreet * XXX: verify that dev_idx is really not in use anymore, anywhere 13961c6fdbd8SKent Overstreet * 13971c6fdbd8SKent Overstreet * flag_data_bad() does not check btree pointers 13981c6fdbd8SKent Overstreet */ 13991c6fdbd8SKent Overstreet ret = bch2_dev_data_drop(c, ca->dev_idx, flags); 14001c6fdbd8SKent Overstreet if (ret) { 14011c6fdbd8SKent Overstreet bch_err(ca, "Remove failed: error %i dropping data", ret); 14021c6fdbd8SKent Overstreet goto err; 14031c6fdbd8SKent Overstreet } 14041c6fdbd8SKent Overstreet 14051c6fdbd8SKent Overstreet ret = bch2_journal_flush_device_pins(&c->journal, ca->dev_idx); 14061c6fdbd8SKent Overstreet if (ret) { 14071c6fdbd8SKent Overstreet bch_err(ca, "Remove failed: error %i flushing journal", ret); 14081c6fdbd8SKent Overstreet goto err; 14091c6fdbd8SKent Overstreet } 14101c6fdbd8SKent Overstreet 14111c6fdbd8SKent Overstreet data = bch2_dev_has_data(c, ca); 14121c6fdbd8SKent Overstreet if (data) { 14131c6fdbd8SKent Overstreet char data_has_str[100]; 1414319f9ac3SKent Overstreet 1415dbaee468SKent Overstreet bch2_flags_to_text(&PBUF(data_has_str), 1416319f9ac3SKent Overstreet bch2_data_types, data); 14171c6fdbd8SKent Overstreet bch_err(ca, "Remove failed, still has data (%s)", data_has_str); 14181c6fdbd8SKent Overstreet ret = -EBUSY; 14191c6fdbd8SKent Overstreet goto err; 14201c6fdbd8SKent Overstreet } 14211c6fdbd8SKent Overstreet 14221c6fdbd8SKent Overstreet ret = bch2_btree_delete_range(c, BTREE_ID_ALLOC, 14231c6fdbd8SKent Overstreet POS(ca->dev_idx, 0), 14241c6fdbd8SKent Overstreet POS(ca->dev_idx + 1, 0), 1425fc3268c1SKent Overstreet NULL); 14261c6fdbd8SKent Overstreet if (ret) { 14271c6fdbd8SKent Overstreet bch_err(ca, "Remove failed, error deleting alloc info"); 14281c6fdbd8SKent Overstreet goto err; 14291c6fdbd8SKent Overstreet } 14301c6fdbd8SKent Overstreet 14311c6fdbd8SKent Overstreet /* 14321c6fdbd8SKent Overstreet * must flush all existing journal entries, they might have 14331c6fdbd8SKent Overstreet * (overwritten) keys that point to the device we're removing: 14341c6fdbd8SKent Overstreet */ 14351c6fdbd8SKent Overstreet bch2_journal_flush_all_pins(&c->journal); 14361c6fdbd8SKent Overstreet ret = bch2_journal_error(&c->journal); 14371c6fdbd8SKent Overstreet if (ret) { 14381c6fdbd8SKent Overstreet bch_err(ca, "Remove failed, journal error"); 14391c6fdbd8SKent Overstreet goto err; 14401c6fdbd8SKent Overstreet } 14411c6fdbd8SKent Overstreet 14421c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 14431c6fdbd8SKent Overstreet 14441c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 14451c6fdbd8SKent Overstreet rcu_assign_pointer(c->devs[ca->dev_idx], NULL); 14461c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 14471c6fdbd8SKent Overstreet 14481c6fdbd8SKent Overstreet percpu_ref_kill(&ca->ref); 14491c6fdbd8SKent Overstreet wait_for_completion(&ca->ref_completion); 14501c6fdbd8SKent Overstreet 14511c6fdbd8SKent Overstreet bch2_dev_free(ca); 14521c6fdbd8SKent Overstreet 14531c6fdbd8SKent Overstreet /* 14541c6fdbd8SKent Overstreet * Free this device's slot in the bch_member array - all pointers to 14551c6fdbd8SKent Overstreet * this device must be gone: 14561c6fdbd8SKent Overstreet */ 14571c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 14581c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 14591c6fdbd8SKent Overstreet memset(&mi->members[dev_idx].uuid, 0, sizeof(mi->members[dev_idx].uuid)); 14601c6fdbd8SKent Overstreet 14611c6fdbd8SKent Overstreet bch2_write_super(c); 14621c6fdbd8SKent Overstreet 14631c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 14641c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 14651c6fdbd8SKent Overstreet return 0; 14661c6fdbd8SKent Overstreet err: 1467d3bb629dSKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_RW && 1468d3bb629dSKent Overstreet !percpu_ref_is_zero(&ca->io_ref)) 14691c6fdbd8SKent Overstreet __bch2_dev_read_write(c, ca); 14701c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 14711c6fdbd8SKent Overstreet return ret; 14721c6fdbd8SKent Overstreet } 14731c6fdbd8SKent Overstreet 14746eac2c2eSKent Overstreet static void dev_usage_clear(struct bch_dev *ca) 14756eac2c2eSKent Overstreet { 14766eac2c2eSKent Overstreet struct bucket_array *buckets; 14776eac2c2eSKent Overstreet 14785e82a9a1SKent Overstreet percpu_memset(ca->usage[0], 0, sizeof(*ca->usage[0])); 14796eac2c2eSKent Overstreet 14806eac2c2eSKent Overstreet down_read(&ca->bucket_lock); 14816eac2c2eSKent Overstreet buckets = bucket_array(ca); 14826eac2c2eSKent Overstreet 14836eac2c2eSKent Overstreet memset(buckets->b, 0, sizeof(buckets->b[0]) * buckets->nbuckets); 14846eac2c2eSKent Overstreet up_read(&ca->bucket_lock); 14856eac2c2eSKent Overstreet } 14866eac2c2eSKent Overstreet 14871c6fdbd8SKent Overstreet /* Add new device to running filesystem: */ 14881c6fdbd8SKent Overstreet int bch2_dev_add(struct bch_fs *c, const char *path) 14891c6fdbd8SKent Overstreet { 14901c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 14911c6fdbd8SKent Overstreet struct bch_sb_handle sb; 14921c6fdbd8SKent Overstreet const char *err; 14931c6fdbd8SKent Overstreet struct bch_dev *ca = NULL; 14941c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 14951c6fdbd8SKent Overstreet struct bch_member dev_mi; 14961c6fdbd8SKent Overstreet unsigned dev_idx, nr_devices, u64s; 14971c6fdbd8SKent Overstreet int ret; 14981c6fdbd8SKent Overstreet 14991c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 15001c6fdbd8SKent Overstreet if (ret) 15011c6fdbd8SKent Overstreet return ret; 15021c6fdbd8SKent Overstreet 15031c6fdbd8SKent Overstreet err = bch2_sb_validate(&sb); 15041c6fdbd8SKent Overstreet if (err) 15051c6fdbd8SKent Overstreet return -EINVAL; 15061c6fdbd8SKent Overstreet 15071c6fdbd8SKent Overstreet dev_mi = bch2_sb_get_members(sb.sb)->members[sb.sb->dev_idx]; 15081c6fdbd8SKent Overstreet 15091c6fdbd8SKent Overstreet err = bch2_dev_may_add(sb.sb, c); 15101c6fdbd8SKent Overstreet if (err) 15111c6fdbd8SKent Overstreet return -EINVAL; 15121c6fdbd8SKent Overstreet 15131c6fdbd8SKent Overstreet ca = __bch2_dev_alloc(c, &dev_mi); 15141c6fdbd8SKent Overstreet if (!ca) { 15151c6fdbd8SKent Overstreet bch2_free_super(&sb); 15161c6fdbd8SKent Overstreet return -ENOMEM; 15171c6fdbd8SKent Overstreet } 15181c6fdbd8SKent Overstreet 15191c6fdbd8SKent Overstreet ret = __bch2_dev_attach_bdev(ca, &sb); 15201c6fdbd8SKent Overstreet if (ret) { 15211c6fdbd8SKent Overstreet bch2_dev_free(ca); 15221c6fdbd8SKent Overstreet return ret; 15231c6fdbd8SKent Overstreet } 15241c6fdbd8SKent Overstreet 15256eac2c2eSKent Overstreet /* 15266eac2c2eSKent Overstreet * We want to allocate journal on the new device before adding the new 15276eac2c2eSKent Overstreet * device to the filesystem because allocating after we attach requires 15286eac2c2eSKent Overstreet * spinning up the allocator thread, and the allocator thread requires 15296eac2c2eSKent Overstreet * doing btree writes, which if the existing devices are RO isn't going 15306eac2c2eSKent Overstreet * to work 15316eac2c2eSKent Overstreet * 15326eac2c2eSKent Overstreet * So we have to mark where the superblocks are, but marking allocated 15336eac2c2eSKent Overstreet * data normally updates the filesystem usage too, so we have to mark, 15346eac2c2eSKent Overstreet * allocate the journal, reset all the marks, then remark after we 15356eac2c2eSKent Overstreet * attach... 15366eac2c2eSKent Overstreet */ 15379ca53b55SKent Overstreet bch2_mark_dev_superblock(ca->fs, ca, 0); 15386eac2c2eSKent Overstreet 15391c6fdbd8SKent Overstreet err = "journal alloc failed"; 15401c6fdbd8SKent Overstreet ret = bch2_dev_journal_alloc(ca); 15411c6fdbd8SKent Overstreet if (ret) 15421c6fdbd8SKent Overstreet goto err; 15431c6fdbd8SKent Overstreet 15446eac2c2eSKent Overstreet dev_usage_clear(ca); 15456eac2c2eSKent Overstreet 15461c6fdbd8SKent Overstreet mutex_lock(&c->state_lock); 15471c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 15481c6fdbd8SKent Overstreet 15491c6fdbd8SKent Overstreet err = "insufficient space in new superblock"; 15501c6fdbd8SKent Overstreet ret = bch2_sb_from_fs(c, ca); 15511c6fdbd8SKent Overstreet if (ret) 15521c6fdbd8SKent Overstreet goto err_unlock; 15531c6fdbd8SKent Overstreet 15541c6fdbd8SKent Overstreet mi = bch2_sb_get_members(ca->disk_sb.sb); 15551c6fdbd8SKent Overstreet 15561c6fdbd8SKent Overstreet if (!bch2_sb_resize_members(&ca->disk_sb, 15571c6fdbd8SKent Overstreet le32_to_cpu(mi->field.u64s) + 15581c6fdbd8SKent Overstreet sizeof(dev_mi) / sizeof(u64))) { 15591c6fdbd8SKent Overstreet ret = -ENOSPC; 15601c6fdbd8SKent Overstreet goto err_unlock; 15611c6fdbd8SKent Overstreet } 15621c6fdbd8SKent Overstreet 15631c6fdbd8SKent Overstreet if (dynamic_fault("bcachefs:add:no_slot")) 15641c6fdbd8SKent Overstreet goto no_slot; 15651c6fdbd8SKent Overstreet 15661c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 15671c6fdbd8SKent Overstreet for (dev_idx = 0; dev_idx < BCH_SB_MEMBERS_MAX; dev_idx++) 15681c6fdbd8SKent Overstreet if (!bch2_dev_exists(c->disk_sb.sb, mi, dev_idx)) 15691c6fdbd8SKent Overstreet goto have_slot; 15701c6fdbd8SKent Overstreet no_slot: 15711c6fdbd8SKent Overstreet err = "no slots available in superblock"; 15721c6fdbd8SKent Overstreet ret = -ENOSPC; 15731c6fdbd8SKent Overstreet goto err_unlock; 15741c6fdbd8SKent Overstreet 15751c6fdbd8SKent Overstreet have_slot: 15761c6fdbd8SKent Overstreet nr_devices = max_t(unsigned, dev_idx + 1, c->sb.nr_devices); 15771c6fdbd8SKent Overstreet u64s = (sizeof(struct bch_sb_field_members) + 15781c6fdbd8SKent Overstreet sizeof(struct bch_member) * nr_devices) / sizeof(u64); 15791c6fdbd8SKent Overstreet 15801c6fdbd8SKent Overstreet err = "no space in superblock for member info"; 15811c6fdbd8SKent Overstreet ret = -ENOSPC; 15821c6fdbd8SKent Overstreet 15831c6fdbd8SKent Overstreet mi = bch2_sb_resize_members(&c->disk_sb, u64s); 15841c6fdbd8SKent Overstreet if (!mi) 15851c6fdbd8SKent Overstreet goto err_unlock; 15861c6fdbd8SKent Overstreet 15871c6fdbd8SKent Overstreet /* success: */ 15881c6fdbd8SKent Overstreet 15891c6fdbd8SKent Overstreet mi->members[dev_idx] = dev_mi; 1590a420eea6STim Schlueter mi->members[dev_idx].last_mount = cpu_to_le64(ktime_get_real_seconds()); 15911c6fdbd8SKent Overstreet c->disk_sb.sb->nr_devices = nr_devices; 15921c6fdbd8SKent Overstreet 15931c6fdbd8SKent Overstreet ca->disk_sb.sb->dev_idx = dev_idx; 15941c6fdbd8SKent Overstreet bch2_dev_attach(c, ca, dev_idx); 15951c6fdbd8SKent Overstreet 15969ca53b55SKent Overstreet bch2_mark_dev_superblock(c, ca, 0); 15976eac2c2eSKent Overstreet 15981c6fdbd8SKent Overstreet bch2_write_super(c); 15991c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 16001c6fdbd8SKent Overstreet 16011c6fdbd8SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_RW) { 16021c6fdbd8SKent Overstreet err = __bch2_dev_read_write(c, ca); 16031c6fdbd8SKent Overstreet if (err) 16041c6fdbd8SKent Overstreet goto err_late; 16051c6fdbd8SKent Overstreet } 16061c6fdbd8SKent Overstreet 16071c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 16081c6fdbd8SKent Overstreet return 0; 16091c6fdbd8SKent Overstreet 16101c6fdbd8SKent Overstreet err_unlock: 16111c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 16121c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 16131c6fdbd8SKent Overstreet err: 16141c6fdbd8SKent Overstreet if (ca) 16151c6fdbd8SKent Overstreet bch2_dev_free(ca); 16161c6fdbd8SKent Overstreet bch2_free_super(&sb); 16171c6fdbd8SKent Overstreet bch_err(c, "Unable to add device: %s", err); 16181c6fdbd8SKent Overstreet return ret; 16191c6fdbd8SKent Overstreet err_late: 16201c6fdbd8SKent Overstreet bch_err(c, "Error going rw after adding device: %s", err); 16211c6fdbd8SKent Overstreet return -EINVAL; 16221c6fdbd8SKent Overstreet } 16231c6fdbd8SKent Overstreet 16241c6fdbd8SKent Overstreet /* Hot add existing device to running filesystem: */ 16251c6fdbd8SKent Overstreet int bch2_dev_online(struct bch_fs *c, const char *path) 16261c6fdbd8SKent Overstreet { 16271c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 16281c6fdbd8SKent Overstreet struct bch_sb_handle sb = { NULL }; 16291c6fdbd8SKent Overstreet struct bch_sb_field_members *mi; 16301c6fdbd8SKent Overstreet struct bch_dev *ca; 16311c6fdbd8SKent Overstreet unsigned dev_idx; 16321c6fdbd8SKent Overstreet const char *err; 16331c6fdbd8SKent Overstreet int ret; 16341c6fdbd8SKent Overstreet 16351c6fdbd8SKent Overstreet mutex_lock(&c->state_lock); 16361c6fdbd8SKent Overstreet 16371c6fdbd8SKent Overstreet ret = bch2_read_super(path, &opts, &sb); 16381c6fdbd8SKent Overstreet if (ret) { 16391c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 16401c6fdbd8SKent Overstreet return ret; 16411c6fdbd8SKent Overstreet } 16421c6fdbd8SKent Overstreet 16431c6fdbd8SKent Overstreet dev_idx = sb.sb->dev_idx; 16441c6fdbd8SKent Overstreet 16451c6fdbd8SKent Overstreet err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb); 16461c6fdbd8SKent Overstreet if (err) 16471c6fdbd8SKent Overstreet goto err; 16481c6fdbd8SKent Overstreet 16491c6fdbd8SKent Overstreet if (bch2_dev_attach_bdev(c, &sb)) { 16501c6fdbd8SKent Overstreet err = "bch2_dev_attach_bdev() error"; 16511c6fdbd8SKent Overstreet goto err; 16521c6fdbd8SKent Overstreet } 16531c6fdbd8SKent Overstreet 16541c6fdbd8SKent Overstreet ca = bch_dev_locked(c, dev_idx); 16551c6fdbd8SKent Overstreet if (ca->mi.state == BCH_MEMBER_STATE_RW) { 16561c6fdbd8SKent Overstreet err = __bch2_dev_read_write(c, ca); 16571c6fdbd8SKent Overstreet if (err) 16581c6fdbd8SKent Overstreet goto err; 16591c6fdbd8SKent Overstreet } 16601c6fdbd8SKent Overstreet 16611c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 16621c6fdbd8SKent Overstreet mi = bch2_sb_get_members(c->disk_sb.sb); 16631c6fdbd8SKent Overstreet 16641c6fdbd8SKent Overstreet mi->members[ca->dev_idx].last_mount = 1665a420eea6STim Schlueter cpu_to_le64(ktime_get_real_seconds()); 16661c6fdbd8SKent Overstreet 16671c6fdbd8SKent Overstreet bch2_write_super(c); 16681c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 16691c6fdbd8SKent Overstreet 16701c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 16711c6fdbd8SKent Overstreet return 0; 16721c6fdbd8SKent Overstreet err: 16731c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 16741c6fdbd8SKent Overstreet bch2_free_super(&sb); 16751c6fdbd8SKent Overstreet bch_err(c, "error bringing %s online: %s", path, err); 16761c6fdbd8SKent Overstreet return -EINVAL; 16771c6fdbd8SKent Overstreet } 16781c6fdbd8SKent Overstreet 16791c6fdbd8SKent Overstreet int bch2_dev_offline(struct bch_fs *c, struct bch_dev *ca, int flags) 16801c6fdbd8SKent Overstreet { 16811c6fdbd8SKent Overstreet mutex_lock(&c->state_lock); 16821c6fdbd8SKent Overstreet 16831c6fdbd8SKent Overstreet if (!bch2_dev_is_online(ca)) { 16841c6fdbd8SKent Overstreet bch_err(ca, "Already offline"); 16851c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 16861c6fdbd8SKent Overstreet return 0; 16871c6fdbd8SKent Overstreet } 16881c6fdbd8SKent Overstreet 16891c6fdbd8SKent Overstreet if (!bch2_dev_state_allowed(c, ca, BCH_MEMBER_STATE_FAILED, flags)) { 16901c6fdbd8SKent Overstreet bch_err(ca, "Cannot offline required disk"); 16911c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 16921c6fdbd8SKent Overstreet return -EINVAL; 16931c6fdbd8SKent Overstreet } 16941c6fdbd8SKent Overstreet 16951c6fdbd8SKent Overstreet __bch2_dev_offline(c, ca); 16961c6fdbd8SKent Overstreet 16971c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 16981c6fdbd8SKent Overstreet return 0; 16991c6fdbd8SKent Overstreet } 17001c6fdbd8SKent Overstreet 17011c6fdbd8SKent Overstreet int bch2_dev_resize(struct bch_fs *c, struct bch_dev *ca, u64 nbuckets) 17021c6fdbd8SKent Overstreet { 17031c6fdbd8SKent Overstreet struct bch_member *mi; 17041c6fdbd8SKent Overstreet int ret = 0; 17051c6fdbd8SKent Overstreet 17061c6fdbd8SKent Overstreet mutex_lock(&c->state_lock); 17071c6fdbd8SKent Overstreet 17081c6fdbd8SKent Overstreet if (nbuckets < ca->mi.nbuckets) { 17091c6fdbd8SKent Overstreet bch_err(ca, "Cannot shrink yet"); 17101c6fdbd8SKent Overstreet ret = -EINVAL; 17111c6fdbd8SKent Overstreet goto err; 17121c6fdbd8SKent Overstreet } 17131c6fdbd8SKent Overstreet 17141c6fdbd8SKent Overstreet if (bch2_dev_is_online(ca) && 17151c6fdbd8SKent Overstreet get_capacity(ca->disk_sb.bdev->bd_disk) < 17161c6fdbd8SKent Overstreet ca->mi.bucket_size * nbuckets) { 17171c6fdbd8SKent Overstreet bch_err(ca, "New size larger than device"); 17181c6fdbd8SKent Overstreet ret = -EINVAL; 17191c6fdbd8SKent Overstreet goto err; 17201c6fdbd8SKent Overstreet } 17211c6fdbd8SKent Overstreet 17221c6fdbd8SKent Overstreet ret = bch2_dev_buckets_resize(c, ca, nbuckets); 17231c6fdbd8SKent Overstreet if (ret) { 17241c6fdbd8SKent Overstreet bch_err(ca, "Resize error: %i", ret); 17251c6fdbd8SKent Overstreet goto err; 17261c6fdbd8SKent Overstreet } 17271c6fdbd8SKent Overstreet 17281c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 17291c6fdbd8SKent Overstreet mi = &bch2_sb_get_members(c->disk_sb.sb)->members[ca->dev_idx]; 17301c6fdbd8SKent Overstreet mi->nbuckets = cpu_to_le64(nbuckets); 17311c6fdbd8SKent Overstreet 17321c6fdbd8SKent Overstreet bch2_write_super(c); 17331c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 17341c6fdbd8SKent Overstreet 17351c6fdbd8SKent Overstreet bch2_recalc_capacity(c); 17361c6fdbd8SKent Overstreet err: 17371c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 17381c6fdbd8SKent Overstreet return ret; 17391c6fdbd8SKent Overstreet } 17401c6fdbd8SKent Overstreet 17411c6fdbd8SKent Overstreet /* return with ref on ca->ref: */ 17421c6fdbd8SKent Overstreet struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *path) 17431c6fdbd8SKent Overstreet { 17441c6fdbd8SKent Overstreet 17451c6fdbd8SKent Overstreet struct bch_dev *ca; 17461c6fdbd8SKent Overstreet dev_t dev; 17471c6fdbd8SKent Overstreet unsigned i; 17481c6fdbd8SKent Overstreet int ret; 17491c6fdbd8SKent Overstreet 17501c6fdbd8SKent Overstreet ret = lookup_bdev(path, &dev); 17511c6fdbd8SKent Overstreet if (ret) 17521c6fdbd8SKent Overstreet return ERR_PTR(ret); 17531c6fdbd8SKent Overstreet 17541c6fdbd8SKent Overstreet for_each_member_device(ca, c, i) 17551c6fdbd8SKent Overstreet if (ca->disk_sb.bdev->bd_dev == dev) 17561c6fdbd8SKent Overstreet goto found; 17571c6fdbd8SKent Overstreet 17581c6fdbd8SKent Overstreet ca = ERR_PTR(-ENOENT); 17591c6fdbd8SKent Overstreet found: 17601c6fdbd8SKent Overstreet return ca; 17611c6fdbd8SKent Overstreet } 17621c6fdbd8SKent Overstreet 17631c6fdbd8SKent Overstreet /* Filesystem open: */ 17641c6fdbd8SKent Overstreet 17651c6fdbd8SKent Overstreet struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, 17661c6fdbd8SKent Overstreet struct bch_opts opts) 17671c6fdbd8SKent Overstreet { 17681c6fdbd8SKent Overstreet struct bch_sb_handle *sb = NULL; 17691c6fdbd8SKent Overstreet struct bch_fs *c = NULL; 17701c6fdbd8SKent Overstreet unsigned i, best_sb = 0; 17711c6fdbd8SKent Overstreet const char *err; 17721c6fdbd8SKent Overstreet int ret = -ENOMEM; 17731c6fdbd8SKent Overstreet 17741c6fdbd8SKent Overstreet pr_verbose_init(opts, ""); 17751c6fdbd8SKent Overstreet 17761c6fdbd8SKent Overstreet if (!nr_devices) { 17771c6fdbd8SKent Overstreet c = ERR_PTR(-EINVAL); 17781c6fdbd8SKent Overstreet goto out2; 17791c6fdbd8SKent Overstreet } 17801c6fdbd8SKent Overstreet 17811c6fdbd8SKent Overstreet if (!try_module_get(THIS_MODULE)) { 17821c6fdbd8SKent Overstreet c = ERR_PTR(-ENODEV); 17831c6fdbd8SKent Overstreet goto out2; 17841c6fdbd8SKent Overstreet } 17851c6fdbd8SKent Overstreet 17861c6fdbd8SKent Overstreet sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL); 17871c6fdbd8SKent Overstreet if (!sb) 17881c6fdbd8SKent Overstreet goto err; 17891c6fdbd8SKent Overstreet 17901c6fdbd8SKent Overstreet for (i = 0; i < nr_devices; i++) { 17911c6fdbd8SKent Overstreet ret = bch2_read_super(devices[i], &opts, &sb[i]); 17921c6fdbd8SKent Overstreet if (ret) 17931c6fdbd8SKent Overstreet goto err; 17941c6fdbd8SKent Overstreet 17951c6fdbd8SKent Overstreet err = bch2_sb_validate(&sb[i]); 17961c6fdbd8SKent Overstreet if (err) 17971c6fdbd8SKent Overstreet goto err_print; 17981c6fdbd8SKent Overstreet } 17991c6fdbd8SKent Overstreet 18001c6fdbd8SKent Overstreet for (i = 1; i < nr_devices; i++) 18011c6fdbd8SKent Overstreet if (le64_to_cpu(sb[i].sb->seq) > 18021c6fdbd8SKent Overstreet le64_to_cpu(sb[best_sb].sb->seq)) 18031c6fdbd8SKent Overstreet best_sb = i; 18041c6fdbd8SKent Overstreet 18051c6fdbd8SKent Overstreet for (i = 0; i < nr_devices; i++) { 18061c6fdbd8SKent Overstreet err = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb); 18071c6fdbd8SKent Overstreet if (err) 18081c6fdbd8SKent Overstreet goto err_print; 18091c6fdbd8SKent Overstreet } 18101c6fdbd8SKent Overstreet 18111c6fdbd8SKent Overstreet ret = -ENOMEM; 18121c6fdbd8SKent Overstreet c = bch2_fs_alloc(sb[best_sb].sb, opts); 18131c6fdbd8SKent Overstreet if (!c) 18141c6fdbd8SKent Overstreet goto err; 18151c6fdbd8SKent Overstreet 18161c6fdbd8SKent Overstreet err = "bch2_dev_online() error"; 18171c6fdbd8SKent Overstreet mutex_lock(&c->state_lock); 18181c6fdbd8SKent Overstreet for (i = 0; i < nr_devices; i++) 18191c6fdbd8SKent Overstreet if (bch2_dev_attach_bdev(c, &sb[i])) { 18201c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 18211c6fdbd8SKent Overstreet goto err_print; 18221c6fdbd8SKent Overstreet } 18231c6fdbd8SKent Overstreet mutex_unlock(&c->state_lock); 18241c6fdbd8SKent Overstreet 18251c6fdbd8SKent Overstreet err = "insufficient devices"; 18261c6fdbd8SKent Overstreet if (!bch2_fs_may_start(c)) 18271c6fdbd8SKent Overstreet goto err_print; 18281c6fdbd8SKent Overstreet 18291c6fdbd8SKent Overstreet if (!c->opts.nostart) { 1830619f5beeSKent Overstreet ret = bch2_fs_start(c); 1831619f5beeSKent Overstreet if (ret) 1832619f5beeSKent Overstreet goto err; 18331c6fdbd8SKent Overstreet } 18341c6fdbd8SKent Overstreet out: 18351c6fdbd8SKent Overstreet kfree(sb); 18361c6fdbd8SKent Overstreet module_put(THIS_MODULE); 18371c6fdbd8SKent Overstreet out2: 18381c6fdbd8SKent Overstreet pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c)); 18391c6fdbd8SKent Overstreet return c; 18401c6fdbd8SKent Overstreet err_print: 18411c6fdbd8SKent Overstreet pr_err("bch_fs_open err opening %s: %s", 18421c6fdbd8SKent Overstreet devices[0], err); 18431c6fdbd8SKent Overstreet ret = -EINVAL; 18441c6fdbd8SKent Overstreet err: 18451c6fdbd8SKent Overstreet if (c) 18461c6fdbd8SKent Overstreet bch2_fs_stop(c); 18471c6fdbd8SKent Overstreet for (i = 0; i < nr_devices; i++) 18481c6fdbd8SKent Overstreet bch2_free_super(&sb[i]); 18491c6fdbd8SKent Overstreet c = ERR_PTR(ret); 18501c6fdbd8SKent Overstreet goto out; 18511c6fdbd8SKent Overstreet } 18521c6fdbd8SKent Overstreet 18531c6fdbd8SKent Overstreet static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb, 18541c6fdbd8SKent Overstreet struct bch_opts opts) 18551c6fdbd8SKent Overstreet { 18561c6fdbd8SKent Overstreet const char *err; 18571c6fdbd8SKent Overstreet struct bch_fs *c; 18581c6fdbd8SKent Overstreet bool allocated_fs = false; 1859619f5beeSKent Overstreet int ret; 18601c6fdbd8SKent Overstreet 18611c6fdbd8SKent Overstreet err = bch2_sb_validate(sb); 18621c6fdbd8SKent Overstreet if (err) 18631c6fdbd8SKent Overstreet return err; 18641c6fdbd8SKent Overstreet 18651c6fdbd8SKent Overstreet mutex_lock(&bch_fs_list_lock); 18661c6fdbd8SKent Overstreet c = __bch2_uuid_to_fs(sb->sb->uuid); 18671c6fdbd8SKent Overstreet if (c) { 18681c6fdbd8SKent Overstreet closure_get(&c->cl); 18691c6fdbd8SKent Overstreet 18701c6fdbd8SKent Overstreet err = bch2_dev_in_fs(c->disk_sb.sb, sb->sb); 18711c6fdbd8SKent Overstreet if (err) 18721c6fdbd8SKent Overstreet goto err; 18731c6fdbd8SKent Overstreet } else { 18741c6fdbd8SKent Overstreet c = bch2_fs_alloc(sb->sb, opts); 18751c6fdbd8SKent Overstreet err = "cannot allocate memory"; 18761c6fdbd8SKent Overstreet if (!c) 18771c6fdbd8SKent Overstreet goto err; 18781c6fdbd8SKent Overstreet 18791c6fdbd8SKent Overstreet allocated_fs = true; 18801c6fdbd8SKent Overstreet } 18811c6fdbd8SKent Overstreet 18821c6fdbd8SKent Overstreet err = "bch2_dev_online() error"; 18831c6fdbd8SKent Overstreet 18841c6fdbd8SKent Overstreet mutex_lock(&c->sb_lock); 18851c6fdbd8SKent Overstreet if (bch2_dev_attach_bdev(c, sb)) { 18861c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 18871c6fdbd8SKent Overstreet goto err; 18881c6fdbd8SKent Overstreet } 18891c6fdbd8SKent Overstreet mutex_unlock(&c->sb_lock); 18901c6fdbd8SKent Overstreet 18911c6fdbd8SKent Overstreet if (!c->opts.nostart && bch2_fs_may_start(c)) { 1892619f5beeSKent Overstreet err = "error starting filesystem"; 1893619f5beeSKent Overstreet ret = bch2_fs_start(c); 1894619f5beeSKent Overstreet if (ret) 18951c6fdbd8SKent Overstreet goto err; 18961c6fdbd8SKent Overstreet } 18971c6fdbd8SKent Overstreet 18981c6fdbd8SKent Overstreet closure_put(&c->cl); 18991c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 19001c6fdbd8SKent Overstreet 19011c6fdbd8SKent Overstreet return NULL; 19021c6fdbd8SKent Overstreet err: 19031c6fdbd8SKent Overstreet mutex_unlock(&bch_fs_list_lock); 19041c6fdbd8SKent Overstreet 19051c6fdbd8SKent Overstreet if (allocated_fs) 19061c6fdbd8SKent Overstreet bch2_fs_stop(c); 19071c6fdbd8SKent Overstreet else if (c) 19081c6fdbd8SKent Overstreet closure_put(&c->cl); 19091c6fdbd8SKent Overstreet 19101c6fdbd8SKent Overstreet return err; 19111c6fdbd8SKent Overstreet } 19121c6fdbd8SKent Overstreet 19131c6fdbd8SKent Overstreet const char *bch2_fs_open_incremental(const char *path) 19141c6fdbd8SKent Overstreet { 19151c6fdbd8SKent Overstreet struct bch_sb_handle sb; 19161c6fdbd8SKent Overstreet struct bch_opts opts = bch2_opts_empty(); 19171c6fdbd8SKent Overstreet const char *err; 19181c6fdbd8SKent Overstreet 19191c6fdbd8SKent Overstreet if (bch2_read_super(path, &opts, &sb)) 19201c6fdbd8SKent Overstreet return "error reading superblock"; 19211c6fdbd8SKent Overstreet 19221c6fdbd8SKent Overstreet err = __bch2_fs_open_incremental(&sb, opts); 19231c6fdbd8SKent Overstreet bch2_free_super(&sb); 19241c6fdbd8SKent Overstreet 19251c6fdbd8SKent Overstreet return err; 19261c6fdbd8SKent Overstreet } 19271c6fdbd8SKent Overstreet 19281c6fdbd8SKent Overstreet /* Global interfaces/init */ 19291c6fdbd8SKent Overstreet 19301c6fdbd8SKent Overstreet static void bcachefs_exit(void) 19311c6fdbd8SKent Overstreet { 19321c6fdbd8SKent Overstreet bch2_debug_exit(); 19331c6fdbd8SKent Overstreet bch2_vfs_exit(); 19341c6fdbd8SKent Overstreet bch2_chardev_exit(); 19351c6fdbd8SKent Overstreet if (bcachefs_kset) 19361c6fdbd8SKent Overstreet kset_unregister(bcachefs_kset); 19371c6fdbd8SKent Overstreet } 19381c6fdbd8SKent Overstreet 19391c6fdbd8SKent Overstreet static int __init bcachefs_init(void) 19401c6fdbd8SKent Overstreet { 19411c6fdbd8SKent Overstreet bch2_bkey_pack_test(); 19421c6fdbd8SKent Overstreet bch2_inode_pack_test(); 19431c6fdbd8SKent Overstreet 19441c6fdbd8SKent Overstreet if (!(bcachefs_kset = kset_create_and_add("bcachefs", NULL, fs_kobj)) || 19451c6fdbd8SKent Overstreet bch2_chardev_init() || 19461c6fdbd8SKent Overstreet bch2_vfs_init() || 19471c6fdbd8SKent Overstreet bch2_debug_init()) 19481c6fdbd8SKent Overstreet goto err; 19491c6fdbd8SKent Overstreet 19501c6fdbd8SKent Overstreet return 0; 19511c6fdbd8SKent Overstreet err: 19521c6fdbd8SKent Overstreet bcachefs_exit(); 19531c6fdbd8SKent Overstreet return -ENOMEM; 19541c6fdbd8SKent Overstreet } 19551c6fdbd8SKent Overstreet 19561c6fdbd8SKent Overstreet #define BCH_DEBUG_PARAM(name, description) \ 19571c6fdbd8SKent Overstreet bool bch2_##name; \ 19581c6fdbd8SKent Overstreet module_param_named(name, bch2_##name, bool, 0644); \ 19591c6fdbd8SKent Overstreet MODULE_PARM_DESC(name, description); 19601c6fdbd8SKent Overstreet BCH_DEBUG_PARAMS() 19611c6fdbd8SKent Overstreet #undef BCH_DEBUG_PARAM 19621c6fdbd8SKent Overstreet 196326609b61SKent Overstreet unsigned bch2_metadata_version = bcachefs_metadata_version_current; 19641c6fdbd8SKent Overstreet module_param_named(version, bch2_metadata_version, uint, 0400); 19651c6fdbd8SKent Overstreet 19661c6fdbd8SKent Overstreet module_exit(bcachefs_exit); 19671c6fdbd8SKent Overstreet module_init(bcachefs_init); 1968