bcachefs: bch_opt_fn

Minor refactoring to get rid of some unneeded token pasting.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2023-07-12 21:48:32 -04:00
parent 8479938d7a
commit 9f343e24f5
4 changed files with 33 additions and 19 deletions

View 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;
}

View 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 *);

View 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();

View 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;