Commit 9f343e24 authored by Kent Overstreet's avatar Kent Overstreet

bcachefs: bch_opt_fn

Minor refactoring to get rid of some unneeded token pasting.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent 8479938d
...@@ -460,30 +460,37 @@ int bch2_dev_group_set(struct bch_fs *c, struct bch_dev *ca, const char *name) ...@@ -460,30 +460,37 @@ int bch2_dev_group_set(struct bch_fs *c, struct bch_dev *ca, const char *name)
return ret; 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; struct bch_dev *ca;
int g; int g;
if (!strlen(buf) || !strcmp(buf, "none")) { if (!val)
*v = 0; return -EINVAL;
if (!c)
return 0;
if (!strlen(val) || !strcmp(val, "none")) {
*res = 0;
return 0; return 0;
} }
/* Is it a device? */ /* Is it a device? */
ca = bch2_dev_lookup(c, buf); ca = bch2_dev_lookup(c, val);
if (!IS_ERR(ca)) { if (!IS_ERR(ca)) {
*v = dev_to_target(ca->dev_idx); *res = dev_to_target(ca->dev_idx);
percpu_ref_put(&ca->ref); percpu_ref_put(&ca->ref);
return 0; return 0;
} }
mutex_lock(&c->sb_lock); 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); mutex_unlock(&c->sb_lock);
if (g >= 0) { if (g >= 0) {
*v = group_to_target(g); *res = group_to_target(g);
return 0; return 0;
} }
......
...@@ -85,9 +85,14 @@ int bch2_disk_path_find_or_create(struct bch_sb_handle *, const char *); ...@@ -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); 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); 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_sb_disk_groups_to_cpu(struct bch_fs *);
int __bch2_dev_group_set(struct bch_fs *, struct bch_dev *, const char *); int __bch2_dev_group_set(struct bch_fs *, struct bch_dev *, const char *);
......
...@@ -167,11 +167,9 @@ const struct bch_option bch2_opt_table[] = { ...@@ -167,11 +167,9 @@ const struct bch_option bch2_opt_table[] = {
#define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \ #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
.min = _min, .max = _max .min = _min, .max = _max
#define OPT_STR(_choices) .type = BCH_OPT_STR, \ #define OPT_STR(_choices) .type = BCH_OPT_STR, \
.min = 0, .max = ARRAY_SIZE(_choices),\ .min = 0, .max = ARRAY_SIZE(_choices), \
.choices = _choices .choices = _choices
#define OPT_FN(_fn) .type = BCH_OPT_FN, \ #define OPT_FN(_fn) .type = BCH_OPT_FN, .fn = _fn
.parse = _fn##_parse, \
.to_text = _fn##_to_text
#define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \ #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
[Opt_##_name] = { \ [Opt_##_name] = { \
...@@ -298,10 +296,7 @@ int bch2_opt_parse(struct bch_fs *c, ...@@ -298,10 +296,7 @@ int bch2_opt_parse(struct bch_fs *c,
*res = ret; *res = ret;
break; break;
case BCH_OPT_FN: case BCH_OPT_FN:
if (!c) ret = opt->fn.parse(c, val, res, err);
return 0;
ret = opt->parse(c, val, res);
if (ret < 0) { if (ret < 0) {
if (err) if (err)
prt_printf(err, "%s: parse error", prt_printf(err, "%s: parse error",
...@@ -344,7 +339,7 @@ void bch2_opt_to_text(struct printbuf *out, ...@@ -344,7 +339,7 @@ void bch2_opt_to_text(struct printbuf *out,
prt_printf(out, "%s", opt->choices[v]); prt_printf(out, "%s", opt->choices[v]);
break; break;
case BCH_OPT_FN: case BCH_OPT_FN:
opt->to_text(out, c, sb, v); opt->fn.to_text(out, c, sb, v);
break; break;
default: default:
BUG(); BUG();
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include <linux/sysfs.h> #include <linux/sysfs.h>
#include "bcachefs_format.h" #include "bcachefs_format.h"
struct bch_fs;
extern const char * const bch2_error_actions[]; extern const char * const bch2_error_actions[];
extern const char * const bch2_version_upgrade_opts[]; extern const char * const bch2_version_upgrade_opts[];
extern const char * const bch2_sb_features[]; extern const char * const bch2_sb_features[];
...@@ -67,6 +69,11 @@ enum opt_type { ...@@ -67,6 +69,11 @@ enum opt_type {
BCH_OPT_FN, 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) * x(name, shortopt, type, in mem type, mode, sb_opt)
* *
...@@ -495,8 +502,8 @@ struct bch_option { ...@@ -495,8 +502,8 @@ struct bch_option {
u64 min, max; u64 min, max;
const char * const *choices; 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 *hint;
const char *help; const char *help;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment