bcachefs: fix the error code when mounting with incorrect options.
authorHongbo Li <lihongbo22@huawei.com>
Mon, 19 Feb 2024 12:24:32 +0000 (20:24 +0800)
committerKent Overstreet <kent.overstreet@linux.dev>
Thu, 14 Mar 2024 01:22:25 +0000 (21:22 -0400)
When mount with incorrect options such as:
"mount -t bcachefs -o errors=back /dev/loop1 /mnt/bcachefs/".
It rebacks the error "mount: /mnt/bcachefs: permission denied."
 cause bch2_parse_mount_opts returns -1 and bch2_mount throws
it up. This is unreasonable.

The real error message should be like this:
"mount: /mnt/bcachefs: wrong fs type, bad option, bad
superblock on /dev/loop1, missing codepage or helper program,
or other error."

Adding three private error codes for mounting error. Here are:
  - BCH_ERR_mount_option as the parent class for option error.
  - BCH_ERR_option_name represents the invalid option name.
  - BCH_ERR_option_value represents the invalid option value.

Signed-off-by: Hongbo Li <lihongbo22@huawei.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/errcode.h
fs/bcachefs/fs.c
fs/bcachefs/opts.c

index e960a6eae66abd7d187d8f99a50bc39e2eed18db..a8f1a3976305c645ce016b88ad0f14c8f8ecc10d 100644 (file)
@@ -5,6 +5,9 @@
 #define BCH_ERRCODES()                                                         \
        x(ERANGE,                       ERANGE_option_too_small)                \
        x(ERANGE,                       ERANGE_option_too_big)                  \
+       x(EINVAL,                       mount_option)                           \
+       x(BCH_ERR_mount_option,         option_name)                            \
+       x(BCH_ERR_mount_option,         option_value)                           \
        x(ENOMEM,                       ENOMEM_stripe_buf)                      \
        x(ENOMEM,                       ENOMEM_replicas_table)                  \
        x(ENOMEM,                       ENOMEM_cpu_replicas)                    \
index 093f5404a655a0b5c7a445f4e99cab851efe5fb8..3f073845bbd77391306a55c6ac7a87771f7e5890 100644 (file)
@@ -1870,8 +1870,10 @@ static struct dentry *bch2_mount(struct file_system_type *fs_type,
        opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
 
        ret = bch2_parse_mount_opts(NULL, &opts, data);
-       if (ret)
+       if (ret) {
+               ret = bch2_err_class(ret);
                return ERR_PTR(ret);
+       }
 
        if (!dev_name || strlen(dev_name) == 0)
                return ERR_PTR(-EINVAL);
index b1ed0b9a20d35d61491ce0cff28b4bb2c7be42c3..1db11c15b2b96d3dec19a19c77ee00e2c4863df3 100644 (file)
@@ -456,7 +456,7 @@ int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
 
        copied_opts = kstrdup(options, GFP_KERNEL);
        if (!copied_opts)
-               return -1;
+               return -ENOMEM;
        copied_opts_start = copied_opts;
 
        while ((opt = strsep(&copied_opts, ",")) != NULL) {
@@ -501,11 +501,11 @@ int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
 
 bad_opt:
        pr_err("Bad mount option %s", name);
-       ret = -1;
+       ret = -BCH_ERR_option_name;
        goto out;
 bad_val:
        pr_err("Invalid mount option %s", err.buf);
-       ret = -1;
+       ret = -BCH_ERR_option_value;
        goto out;
 out:
        kfree(copied_opts_start);