]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/opts.c
Merge pull request #190 from Dikay900/fs_free_space
[bcachefs-tools-debian] / libbcachefs / opts.c
index 0c0c83fa426407ee6beff0bd3b0b75e3c6a53cc3..8e6f230eac38155bf5d048367d6ebde35a4a15bd 100644 (file)
@@ -5,17 +5,23 @@
 #include "bcachefs.h"
 #include "compress.h"
 #include "disk_groups.h"
+#include "error.h"
 #include "opts.h"
 #include "super-io.h"
 #include "util.h"
 
-#define x(t, n) [n] = #t,
+#define x(t, n, ...) [n] = #t,
 
 const char * const bch2_error_actions[] = {
        BCH_ERROR_ACTIONS()
        NULL
 };
 
+const char * const bch2_fsck_fix_opts[] = {
+       BCH_FIX_ERRORS_OPTS()
+       NULL
+};
+
 const char * const bch2_version_upgrade_opts[] = {
        BCH_VERSION_UPGRADE_OPTS()
        NULL
@@ -31,9 +37,8 @@ const char * const bch2_sb_compat[] = {
        NULL
 };
 
-const char * const bch2_btree_ids[] = {
+const char * const __bch2_btree_ids[] = {
        BCH_BTREE_IDS()
-       "interior btree node",
        NULL
 };
 
@@ -89,6 +94,37 @@ const char * const bch2_fs_usage_types[] = {
 
 #undef x
 
+static int bch2_opt_fix_errors_parse(struct bch_fs *c, const char *val, u64 *res,
+                                    struct printbuf *err)
+{
+       if (!val) {
+               *res = FSCK_FIX_yes;
+       } else {
+               int ret = match_string(bch2_fsck_fix_opts, -1, val);
+
+               if (ret < 0 && err)
+                       prt_str(err, "fix_errors: invalid selection");
+               if (ret < 0)
+                       return ret;
+               *res = ret;
+       }
+
+       return 0;
+}
+
+static void bch2_opt_fix_errors_to_text(struct printbuf *out,
+                                       struct bch_fs *c,
+                                       struct bch_sb *sb,
+                                       u64 v)
+{
+       prt_str(out, bch2_fsck_fix_opts[v]);
+}
+
+#define bch2_opt_fix_errors (struct bch_opt_fn) {      \
+       .parse = bch2_opt_fix_errors_parse,             \
+       .to_text = bch2_opt_fix_errors_to_text,         \
+}
+
 const char * const bch2_d_types[BCH_DT_MAX] = {
        [DT_UNKNOWN]    = "unknown",
        [DT_FIFO]       = "fifo",
@@ -167,11 +203,9 @@ const struct bch_option bch2_opt_table[] = {
 #define OPT_UINT(_min, _max)   .type = BCH_OPT_UINT,                   \
                                .min = _min, .max = _max
 #define OPT_STR(_choices)      .type = BCH_OPT_STR,                    \
-                               .min = 0, .max = ARRAY_SIZE(_choices),\
+                               .min = 0, .max = ARRAY_SIZE(_choices),  \
                                .choices = _choices
-#define OPT_FN(_fn)            .type = BCH_OPT_FN,                     \
-                               .parse = _fn##_parse,                   \
-                               .to_text = _fn##_to_text
+#define OPT_FN(_fn)            .type = BCH_OPT_FN, .fn = _fn
 
 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help)        \
        [Opt_##_name] = {                                               \
@@ -231,30 +265,33 @@ int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
                if (err)
                        prt_printf(err, "%s: too small (min %llu)",
                               opt->attr.name, opt->min);
-               return -ERANGE;
+               return -BCH_ERR_ERANGE_option_too_small;
        }
 
        if (opt->max && v >= opt->max) {
                if (err)
                        prt_printf(err, "%s: too big (max %llu)",
                               opt->attr.name, opt->max);
-               return -ERANGE;
+               return -BCH_ERR_ERANGE_option_too_big;
        }
 
        if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
                if (err)
                        prt_printf(err, "%s: not a multiple of 512",
                               opt->attr.name);
-               return -EINVAL;
+               return -BCH_ERR_opt_parse_error;
        }
 
        if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
                if (err)
                        prt_printf(err, "%s: must be a power of two",
                               opt->attr.name);
-               return -EINVAL;
+               return -BCH_ERR_opt_parse_error;
        }
 
+       if (opt->fn.validate)
+               return opt->fn.validate(v, err);
+
        return 0;
 }
 
@@ -267,15 +304,26 @@ int bch2_opt_parse(struct bch_fs *c,
 
        switch (opt->type) {
        case BCH_OPT_BOOL:
-               ret = kstrtou64(val, 10, res);
+               if (val) {
+                       ret = kstrtou64(val, 10, res);
+               } else {
+                       ret = 0;
+                       *res = 1;
+               }
+
                if (ret < 0 || (*res != 0 && *res != 1)) {
                        if (err)
-                               prt_printf(err, "%s: must be bool",
-                                          opt->attr.name);
+                               prt_printf(err, "%s: must be bool", opt->attr.name);
                        return ret;
                }
                break;
        case BCH_OPT_UINT:
+               if (!val) {
+                       prt_printf(err, "%s: required value",
+                                  opt->attr.name);
+                       return -EINVAL;
+               }
+
                ret = opt->flags & OPT_HUMAN_READABLE
                        ? bch2_strtou64_h(val, res)
                        : kstrtou64(val, 10, res);
@@ -287,6 +335,12 @@ int bch2_opt_parse(struct bch_fs *c,
                }
                break;
        case BCH_OPT_STR:
+               if (!val) {
+                       prt_printf(err, "%s: required value",
+                                  opt->attr.name);
+                       return -EINVAL;
+               }
+
                ret = match_string(opt->choices, -1, val);
                if (ret < 0) {
                        if (err)
@@ -298,10 +352,7 @@ int bch2_opt_parse(struct bch_fs *c,
                *res = ret;
                break;
        case BCH_OPT_FN:
-               if (!c)
-                       return 0;
-
-               ret = opt->parse(c, val, res);
+               ret = opt->fn.parse(c, val, res, err);
                if (ret < 0) {
                        if (err)
                                prt_printf(err, "%s: parse error",
@@ -341,10 +392,10 @@ void bch2_opt_to_text(struct printbuf *out,
                if (flags & OPT_SHOW_FULL_LIST)
                        prt_string_option(out, opt->choices, v);
                else
-                       prt_printf(out, "%s", opt->choices[v]);
+                       prt_str(out, opt->choices[v]);
                break;
        case BCH_OPT_FN:
-               opt->to_text(out, c, sb, v);
+               opt->fn.to_text(out, c, sb, v);
                break;
        default:
                BUG();
@@ -396,6 +447,13 @@ int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
        if (!options)
                return 0;
 
+       /*
+        * sys_fsconfig() is now occasionally providing us with option lists
+        * starting with a comma - weird.
+        */
+       if (*options == ',')
+               options++;
+
        copied_opts = kstrdup(options, GFP_KERNEL);
        if (!copied_opts)
                return -1;
@@ -405,31 +463,20 @@ int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
                name    = strsep(&opt, "=");
                val     = opt;
 
-               if (val) {
-                       id = bch2_mount_opt_lookup(name);
-                       if (id < 0)
-                               goto bad_opt;
+               id = bch2_mount_opt_lookup(name);
 
-                       ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
-                       if (ret < 0)
-                               goto bad_val;
-               } else {
-                       id = bch2_mount_opt_lookup(name);
-                       v = 1;
-
-                       if (id < 0 &&
-                           !strncmp("no", name, 2)) {
-                               id = bch2_mount_opt_lookup(name + 2);
-                               v = 0;
-                       }
-
-                       if (id < 0)
-                               goto bad_opt;
-
-                       if (bch2_opt_table[id].type != BCH_OPT_BOOL)
-                               goto no_val;
+               /* Check for the form "noopt", negation of a boolean opt: */
+               if (id < 0 &&
+                   !val &&
+                   !strncmp("no", name, 2)) {
+                       id = bch2_mount_opt_lookup(name + 2);
+                       val = "0";
                }
 
+               /* Unknown options are ignored: */
+               if (id < 0)
+                       continue;
+
                if (!(bch2_opt_table[id].flags & OPT_MOUNT))
                        goto bad_opt;
 
@@ -442,6 +489,10 @@ int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
                    !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
                        goto bad_opt;
 
+               ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
+               if (ret < 0)
+                       goto bad_val;
+
                bch2_opt_set_by_id(opts, id, v);
        }
 
@@ -456,10 +507,6 @@ bad_val:
        pr_err("Invalid mount option %s", err.buf);
        ret = -1;
        goto out;
-no_val:
-       pr_err("Mount option %s requires a value", name);
-       ret = -1;
-       goto out;
 out:
        kfree(copied_opts_start);
        printbuf_exit(&err);