]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/super.c
Delete old bcachefs.5 from makefile
[bcachefs-tools-debian] / libbcachefs / super.c
index 486a019900e36e3c19263679e6c09ca8a724500a..505e559b48a682c6177bb6c52f82e9877fde7305 100644 (file)
@@ -405,13 +405,6 @@ static int __bch2_fs_read_write(struct bch_fs *c, bool early)
        if (ret)
                goto err;
 
-       /*
-        * We need to write out a journal entry before we start doing btree
-        * updates, to ensure that on unclean shutdown new journal blacklist
-        * entries are created:
-        */
-       bch2_journal_meta(&c->journal);
-
        clear_bit(BCH_FS_ALLOC_CLEAN, &c->flags);
 
        for_each_rw_member(ca, c, i)
@@ -595,48 +588,53 @@ void bch2_fs_stop(struct bch_fs *c)
        bch2_fs_free(c);
 }
 
-static const char *bch2_fs_online(struct bch_fs *c)
+static int bch2_fs_online(struct bch_fs *c)
 {
        struct bch_dev *ca;
-       const char *err = NULL;
        unsigned i;
-       int ret;
+       int ret = 0;
 
        lockdep_assert_held(&bch_fs_list_lock);
 
-       if (!list_empty(&c->list))
-               return NULL;
-
-       if (__bch2_uuid_to_fs(c->sb.uuid))
-               return "filesystem UUID already open";
+       if (__bch2_uuid_to_fs(c->sb.uuid)) {
+               bch_err(c, "filesystem UUID already open");
+               return -EINVAL;
+       }
 
        ret = bch2_fs_chardev_init(c);
-       if (ret)
-               return "error creating character device";
+       if (ret) {
+               bch_err(c, "error creating character device");
+               return ret;
+       }
 
        bch2_fs_debug_init(c);
 
-       if (kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ||
-           kobject_add(&c->internal, &c->kobj, "internal") ||
-           kobject_add(&c->opts_dir, &c->kobj, "options") ||
-           kobject_add(&c->time_stats, &c->kobj, "time_stats") ||
-           bch2_opts_create_sysfs_files(&c->opts_dir))
-               return "error creating sysfs objects";
+       ret = kobject_add(&c->kobj, NULL, "%pU", c->sb.user_uuid.b) ?:
+           kobject_add(&c->internal, &c->kobj, "internal") ?:
+           kobject_add(&c->opts_dir, &c->kobj, "options") ?:
+           kobject_add(&c->time_stats, &c->kobj, "time_stats") ?:
+           bch2_opts_create_sysfs_files(&c->opts_dir);
+       if (ret) {
+               bch_err(c, "error creating sysfs objects");
+               return ret;
+       }
 
        down_write(&c->state_lock);
 
-       err = "error creating sysfs objects";
-       for_each_member_device(ca, c, i)
-               if (bch2_dev_sysfs_online(c, ca)) {
+       for_each_member_device(ca, c, i) {
+               ret = bch2_dev_sysfs_online(c, ca);
+               if (ret) {
+                       bch_err(c, "error creating sysfs objects");
                        percpu_ref_put(&ca->ref);
                        goto err;
                }
+       }
 
+       BUG_ON(!list_empty(&c->list));
        list_add(&c->list, &bch_fs_list);
-       err = NULL;
 err:
        up_write(&c->state_lock);
-       return err;
+       return ret;
 }
 
 static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
@@ -644,13 +642,15 @@ static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
        struct bch_sb_field_members *mi;
        struct bch_fs *c;
        unsigned i, iter_size;
-       const char *err;
+       int ret = 0;
 
        pr_verbose_init(opts, "");
 
        c = kvpmalloc(sizeof(struct bch_fs), GFP_KERNEL|__GFP_ZERO);
-       if (!c)
+       if (!c) {
+               c = ERR_PTR(-ENOMEM);
                goto out;
+       }
 
        __module_get(THIS_MODULE);
 
@@ -722,29 +722,37 @@ static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
        c->rebalance.enabled            = 1;
        c->promote_whole_extents        = true;
 
-       c->journal.write_time   = &c->times[BCH_TIME_journal_write];
-       c->journal.delay_time   = &c->times[BCH_TIME_journal_delay];
-       c->journal.blocked_time = &c->times[BCH_TIME_blocked_journal];
-       c->journal.flush_seq_time = &c->times[BCH_TIME_journal_flush_seq];
+       c->journal.flush_write_time     = &c->times[BCH_TIME_journal_flush_write];
+       c->journal.noflush_write_time   = &c->times[BCH_TIME_journal_noflush_write];
+       c->journal.blocked_time         = &c->times[BCH_TIME_blocked_journal];
+       c->journal.flush_seq_time       = &c->times[BCH_TIME_journal_flush_seq];
 
        bch2_fs_btree_cache_init_early(&c->btree_cache);
 
        mutex_init(&c->sectors_available_lock);
 
-       if (percpu_init_rwsem(&c->mark_lock))
+       ret = percpu_init_rwsem(&c->mark_lock);
+       if (ret)
                goto err;
 
        mutex_lock(&c->sb_lock);
+       ret = bch2_sb_to_fs(c, sb);
+       mutex_unlock(&c->sb_lock);
 
-       if (bch2_sb_to_fs(c, sb)) {
-               mutex_unlock(&c->sb_lock);
+       if (ret)
                goto err;
-       }
-
-       mutex_unlock(&c->sb_lock);
 
        scnprintf(c->name, sizeof(c->name), "%pU", &c->sb.user_uuid);
 
+       /* Compat: */
+       if (sb->version <= bcachefs_metadata_version_inode_v2 &&
+           !BCH_SB_JOURNAL_FLUSH_DELAY(sb))
+               SET_BCH_SB_JOURNAL_FLUSH_DELAY(sb, 1000);
+
+       if (sb->version <= bcachefs_metadata_version_inode_v2 &&
+           !BCH_SB_JOURNAL_RECLAIM_DELAY(sb))
+               SET_BCH_SB_JOURNAL_RECLAIM_DELAY(sb, 100);
+
        c->opts = bch2_opts_default;
        bch2_opts_apply(&c->opts, bch2_opts_from_sb(sb));
        bch2_opts_apply(&c->opts, opts);
@@ -752,8 +760,11 @@ static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
        c->block_bits           = ilog2(c->opts.block_size);
        c->btree_foreground_merge_threshold = BTREE_FOREGROUND_MERGE_THRESHOLD(c);
 
-       if (bch2_fs_init_fault("fs_alloc"))
+       if (bch2_fs_init_fault("fs_alloc")) {
+               bch_err(c, "fs_alloc fault injected");
+               ret = -EFAULT;
                goto err;
+       }
 
        iter_size = sizeof(struct sort_iter) +
                (btree_blocks(c) + 1) * 2 *
@@ -783,28 +794,38 @@ static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
                                        btree_bytes(c)) ||
            mempool_init_kmalloc_pool(&c->large_bkey_pool, 1, 2048) ||
            !(c->unused_inode_hints = kcalloc(1U << c->inode_shard_bits,
-                                             sizeof(u64), GFP_KERNEL)) ||
-           bch2_io_clock_init(&c->io_clock[READ]) ||
-           bch2_io_clock_init(&c->io_clock[WRITE]) ||
-           bch2_fs_journal_init(&c->journal) ||
-           bch2_fs_replicas_init(c) ||
-           bch2_fs_btree_cache_init(c) ||
-           bch2_fs_btree_key_cache_init(&c->btree_key_cache) ||
-           bch2_fs_btree_iter_init(c) ||
-           bch2_fs_btree_interior_update_init(c) ||
-           bch2_fs_subvolumes_init(c) ||
-           bch2_fs_io_init(c) ||
-           bch2_fs_encryption_init(c) ||
-           bch2_fs_compress_init(c) ||
-           bch2_fs_ec_init(c) ||
-           bch2_fs_fsio_init(c))
+                                             sizeof(u64), GFP_KERNEL))) {
+               ret = -ENOMEM;
                goto err;
+       }
+
+       ret = bch2_io_clock_init(&c->io_clock[READ]) ?:
+           bch2_io_clock_init(&c->io_clock[WRITE]) ?:
+           bch2_fs_journal_init(&c->journal) ?:
+           bch2_fs_replicas_init(c) ?:
+           bch2_fs_btree_cache_init(c) ?:
+           bch2_fs_btree_key_cache_init(&c->btree_key_cache) ?:
+           bch2_fs_btree_iter_init(c) ?:
+           bch2_fs_btree_interior_update_init(c) ?:
+           bch2_fs_subvolumes_init(c) ?:
+           bch2_fs_io_init(c) ?:
+           bch2_fs_encryption_init(c) ?:
+           bch2_fs_compress_init(c) ?:
+           bch2_fs_ec_init(c) ?:
+           bch2_fs_fsio_init(c);
+       if (ret)
+               goto err;
+
+       if (c->opts.nochanges)
+               set_bit(JOURNAL_NOCHANGES, &c->journal.flags);
 
        mi = bch2_sb_get_members(c->disk_sb.sb);
        for (i = 0; i < c->sb.nr_devices; i++)
                if (bch2_dev_exists(c->disk_sb.sb, mi, i) &&
-                   bch2_dev_alloc(c, i))
+                   bch2_dev_alloc(c, i)) {
+                       ret = -EEXIST;
                        goto err;
+               }
 
        bch2_journal_entry_res_resize(&c->journal,
                        &c->btree_root_journal_res,
@@ -815,18 +836,17 @@ static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts opts)
                        (sizeof(struct jset_entry_clock) / sizeof(u64)) * 2);
 
        mutex_lock(&bch_fs_list_lock);
-       err = bch2_fs_online(c);
+       ret = bch2_fs_online(c);
        mutex_unlock(&bch_fs_list_lock);
-       if (err) {
-               bch_err(c, "bch2_fs_online() error: %s", err);
+
+       if (ret)
                goto err;
-       }
 out:
-       pr_verbose_init(opts, "ret %i", c ? 0 : -ENOMEM);
+       pr_verbose_init(opts, "ret %i", PTR_ERR_OR_ZERO(c));
        return c;
 err:
        bch2_fs_free(c);
-       c = NULL;
+       c = ERR_PTR(ret);
        goto out;
 }
 
@@ -866,7 +886,6 @@ static void print_mount_opts(struct bch_fs *c)
 
 int bch2_fs_start(struct bch_fs *c)
 {
-       const char *err = "cannot allocate memory";
        struct bch_sb_field_members *mi;
        struct bch_dev *ca;
        time64_t now = ktime_get_real_seconds();
@@ -902,10 +921,11 @@ int bch2_fs_start(struct bch_fs *c)
        if (ret)
                goto err;
 
-       err = "dynamic fault";
        ret = -EINVAL;
-       if (bch2_fs_init_fault("fs_start"))
+       if (bch2_fs_init_fault("fs_start")) {
+               bch_err(c, "fs_start fault injected");
                goto err;
+       }
 
        set_bit(BCH_FS_STARTED, &c->flags);
 
@@ -926,7 +946,6 @@ int bch2_fs_start(struct bch_fs *c)
        if (c->opts.read_only || c->opts.nochanges) {
                bch2_fs_read_only(c);
        } else {
-               err = "error going read write";
                ret = !test_bit(BCH_FS_RW, &c->flags)
                        ? bch2_fs_read_write(c)
                        : bch2_fs_read_write_late(c);
@@ -944,25 +963,22 @@ err:
        case BCH_FSCK_ERRORS_NOT_FIXED:
                bch_err(c, "filesystem contains errors: please report this to the developers");
                pr_cont("mount with -o fix_errors to repair\n");
-               err = "fsck error";
                break;
        case BCH_FSCK_REPAIR_UNIMPLEMENTED:
                bch_err(c, "filesystem contains errors: please report this to the developers");
                pr_cont("repair unimplemented: inform the developers so that it can be added\n");
-               err = "fsck error";
                break;
        case BCH_FSCK_REPAIR_IMPOSSIBLE:
                bch_err(c, "filesystem contains errors, but repair impossible");
-               err = "fsck error";
                break;
        case BCH_FSCK_UNKNOWN_VERSION:
-               err = "unknown metadata version";;
+               bch_err(c, "unknown metadata version");
                break;
        case -ENOMEM:
-               err = "cannot allocate memory";
+               bch_err(c, "cannot allocate memory");
                break;
        case -EIO:
-               err = "IO error";
+               bch_err(c, "IO error");
                break;
        }
 
@@ -1382,7 +1398,7 @@ static void __bch2_dev_read_only(struct bch_fs *c, struct bch_dev *ca)
        bch2_copygc_start(c);
 }
 
-static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
+static int __bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
 {
        lockdep_assert_held(&c->state_lock);
 
@@ -1391,10 +1407,7 @@ static const char *__bch2_dev_read_write(struct bch_fs *c, struct bch_dev *ca)
        bch2_dev_allocator_add(c, ca);
        bch2_recalc_capacity(c);
 
-       if (bch2_dev_allocator_start(ca))
-               return "error starting allocator thread";
-
-       return NULL;
+       return bch2_dev_allocator_start(ca);
 }
 
 int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
@@ -1420,9 +1433,8 @@ int __bch2_dev_set_state(struct bch_fs *c, struct bch_dev *ca,
        bch2_write_super(c);
        mutex_unlock(&c->sb_lock);
 
-       if (new_state == BCH_MEMBER_STATE_rw &&
-           __bch2_dev_read_write(c, ca))
-               ret = -ENOMEM;
+       if (new_state == BCH_MEMBER_STATE_rw)
+               ret = __bch2_dev_read_write(c, ca);
 
        rebalance_wakeup(c);
 
@@ -1706,8 +1718,8 @@ have_slot:
                goto err_late;
 
        if (ca->mi.state == BCH_MEMBER_STATE_rw) {
-               err = __bch2_dev_read_write(c, ca);
-               if (err)
+               ret = __bch2_dev_read_write(c, ca);
+               if (ret)
                        goto err_late;
        }
 
@@ -1751,24 +1763,27 @@ int bch2_dev_online(struct bch_fs *c, const char *path)
        dev_idx = sb.sb->dev_idx;
 
        err = bch2_dev_in_fs(c->disk_sb.sb, sb.sb);
-       if (err)
+       if (err) {
+               bch_err(c, "error bringing %s online: %s", path, err);
                goto err;
+       }
 
-       if (bch2_dev_attach_bdev(c, &sb)) {
-               err = "bch2_dev_attach_bdev() error";
+       ret = bch2_dev_attach_bdev(c, &sb);
+       if (ret)
                goto err;
-       }
 
        ca = bch_dev_locked(c, dev_idx);
 
-       if (bch2_trans_mark_dev_sb(c, ca)) {
-               err = "bch2_trans_mark_dev_sb() error";
+       ret = bch2_trans_mark_dev_sb(c, ca);
+       if (ret) {
+               bch_err(c, "error bringing %s online: error %i from bch2_trans_mark_dev_sb",
+                       path, ret);
                goto err;
        }
 
        if (ca->mi.state == BCH_MEMBER_STATE_rw) {
-               err = __bch2_dev_read_write(c, ca);
-               if (err)
+               ret = __bch2_dev_read_write(c, ca);
+               if (ret)
                        goto err;
        }
 
@@ -1786,7 +1801,6 @@ int bch2_dev_online(struct bch_fs *c, const char *path)
 err:
        up_write(&c->state_lock);
        bch2_free_super(&sb);
-       bch_err(c, "error bringing %s online: %s", path, err);
        return -EINVAL;
 }
 
@@ -1890,7 +1904,7 @@ struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
        struct bch_sb_field_members *mi;
        unsigned i, best_sb = 0;
        const char *err;
-       int ret = -ENOMEM;
+       int ret = 0;
 
        pr_verbose_init(opts, "");
 
@@ -1905,8 +1919,10 @@ struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
        }
 
        sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL);
-       if (!sb)
+       if (!sb) {
+               ret = -ENOMEM;
                goto err;
+       }
 
        for (i = 0; i < nr_devices; i++) {
                ret = bch2_read_super(devices[i], &opts, &sb[i]);
@@ -1943,18 +1959,20 @@ struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
                i++;
        }
 
-       ret = -ENOMEM;
        c = bch2_fs_alloc(sb[best_sb].sb, opts);
-       if (!c)
+       if (IS_ERR(c)) {
+               ret = PTR_ERR(c);
                goto err;
+       }
 
-       err = "bch2_dev_online() error";
        down_write(&c->state_lock);
-       for (i = 0; i < nr_devices; i++)
-               if (bch2_dev_attach_bdev(c, &sb[i])) {
+       for (i = 0; i < nr_devices; i++) {
+               ret = bch2_dev_attach_bdev(c, &sb[i]);
+               if (ret) {
                        up_write(&c->state_lock);
-                       goto err_print;
+                       goto err;
                }
+       }
        up_write(&c->state_lock);
 
        err = "insufficient devices";
@@ -1977,10 +1995,11 @@ err_print:
               devices[0], err);
        ret = -EINVAL;
 err:
-       if (c)
+       if (!IS_ERR_OR_NULL(c))
                bch2_fs_stop(c);
-       for (i = 0; i < nr_devices; i++)
-               bch2_free_super(&sb[i]);
+       if (sb)
+               for (i = 0; i < nr_devices; i++)
+                       bch2_free_super(&sb[i]);
        c = ERR_PTR(ret);
        goto out;
 }
@@ -2006,12 +2025,12 @@ static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb,
                if (err)
                        goto err;
        } else {
+               allocated_fs = true;
                c = bch2_fs_alloc(sb->sb, opts);
-               err = "cannot allocate memory";
-               if (!c)
-                       goto err;
 
-               allocated_fs = true;
+               err = "bch2_fs_alloc() error";
+               if (IS_ERR(c))
+                       goto err;
        }
 
        err = "bch2_dev_online() error";
@@ -2037,7 +2056,7 @@ static const char *__bch2_fs_open_incremental(struct bch_sb_handle *sb,
 err:
        mutex_unlock(&bch_fs_list_lock);
 
-       if (allocated_fs)
+       if (allocated_fs && !IS_ERR(c))
                bch2_fs_stop(c);
        else if (c)
                closure_put(&c->cl);