bcachefs: bch_opt_fn
authorKent Overstreet <kent.overstreet@linux.dev>
Thu, 13 Jul 2023 01:48:32 +0000 (21:48 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:10:07 +0000 (17:10 -0400)
Minor refactoring to get rid of some unneeded token pasting.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/disk_groups.c
fs/bcachefs/disk_groups.h
fs/bcachefs/opts.c
fs/bcachefs/opts.h

index 52b6400779704abad9f9f66ace56cecb9355a8ed..de14ca3a9895ddc2d756776252fde5dd0480fd03 100644 (file)
@@ -460,30 +460,37 @@ int bch2_dev_group_set(struct bch_fs *c, struct bch_dev *ca, const char *name)
        return ret;
 }
 
-int bch2_opt_target_parse(struct bch_fs *c, const char *buf, u64 *v)
+int bch2_opt_target_parse(struct bch_fs *c, const char *val, u64 *res,
+                         struct printbuf *err)
 {
        struct bch_dev *ca;
        int g;
 
-       if (!strlen(buf) || !strcmp(buf, "none")) {
-               *v = 0;
+       if (!val)
+               return -EINVAL;
+
+       if (!c)
+               return 0;
+
+       if (!strlen(val) || !strcmp(val, "none")) {
+               *res = 0;
                return 0;
        }
 
        /* Is it a device? */
-       ca = bch2_dev_lookup(c, buf);
+       ca = bch2_dev_lookup(c, val);
        if (!IS_ERR(ca)) {
-               *v = dev_to_target(ca->dev_idx);
+               *res = dev_to_target(ca->dev_idx);
                percpu_ref_put(&ca->ref);
                return 0;
        }
 
        mutex_lock(&c->sb_lock);
-       g = bch2_disk_path_find(&c->disk_sb, buf);
+       g = bch2_disk_path_find(&c->disk_sb, val);
        mutex_unlock(&c->sb_lock);
 
        if (g >= 0) {
-               *v = group_to_target(g);
+               *res = group_to_target(g);
                return 0;
        }
 
index ec12584ceee75fea8552ef83f4ad3df125b48dae..bd7711767fd4f95537fb2ed38d615fdf6aeec250 100644 (file)
@@ -85,9 +85,14 @@ int bch2_disk_path_find_or_create(struct bch_sb_handle *, const char *);
 
 void bch2_disk_path_to_text(struct printbuf *, struct bch_sb *, unsigned);
 
-int bch2_opt_target_parse(struct bch_fs *, const char *, u64 *);
+int bch2_opt_target_parse(struct bch_fs *, const char *, u64 *, struct printbuf *);
 void bch2_opt_target_to_text(struct printbuf *, struct bch_fs *, struct bch_sb *, u64);
 
+#define bch2_opt_target (struct bch_opt_fn) {          \
+       .parse          = bch2_opt_target_parse,        \
+       .to_text        = bch2_opt_target_to_text,      \
+}
+
 int bch2_sb_disk_groups_to_cpu(struct bch_fs *);
 
 int __bch2_dev_group_set(struct bch_fs *, struct bch_dev *, const char *);
index 0c0c83fa426407ee6beff0bd3b0b75e3c6a53cc3..96c2f3c2fbcea9f2ce61fe8a33334c67bc3509b4 100644 (file)
@@ -167,11 +167,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] = {                                               \
@@ -298,10 +296,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",
@@ -344,7 +339,7 @@ void bch2_opt_to_text(struct printbuf *out,
                        prt_printf(out, "%s", 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();
index e105a742fd443b17cecb271b526b25adcb3e3958..3be5095aa472a0e31b29257bda5e2257f23b9c9c 100644 (file)
@@ -8,6 +8,8 @@
 #include <linux/sysfs.h>
 #include "bcachefs_format.h"
 
+struct bch_fs;
+
 extern const char * const bch2_error_actions[];
 extern const char * const bch2_version_upgrade_opts[];
 extern const char * const bch2_sb_features[];
@@ -67,6 +69,11 @@ enum opt_type {
        BCH_OPT_FN,
 };
 
+struct bch_opt_fn {
+       int (*parse)(struct bch_fs *, const char *, u64 *, struct printbuf *);
+       void (*to_text)(struct printbuf *, struct bch_fs *, struct bch_sb *, u64);
+};
+
 /**
  * x(name, shortopt, type, in mem type, mode, sb_opt)
  *
@@ -495,8 +502,8 @@ struct bch_option {
        u64                     min, max;
 
        const char * const *choices;
-       int (*parse)(struct bch_fs *, const char *, u64 *);
-       void (*to_text)(struct printbuf *, struct bch_fs *, struct bch_sb *, u64);
+
+       struct bch_opt_fn       fn;
 
        const char              *hint;
        const char              *help;