X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libbcachefs%2Fopts.c;h=8dd4046cca41ef23b061f4aeac1892f82a504d65;hb=3a0cc86e767b95366b9cbdba7a1453454f1b5a41;hp=9b75c852bac87170341767f48f4cd95aa0df7428;hpb=ad031731727d4239351afeebf675f069a289beb3;p=bcachefs-tools-debian diff --git a/libbcachefs/opts.c b/libbcachefs/opts.c index 9b75c85..8dd4046 100644 --- a/libbcachefs/opts.c +++ b/libbcachefs/opts.c @@ -5,17 +5,28 @@ #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) #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 +}; + const char * const bch2_sb_features[] = { BCH_SB_FEATURES() NULL @@ -26,7 +37,7 @@ const char * const bch2_sb_compat[] = { NULL }; -const char * const bch2_btree_ids[] = { +const char * const __bch2_btree_ids[] = { BCH_BTREE_IDS() NULL }; @@ -66,18 +77,54 @@ const char * const bch2_data_types[] = { NULL }; -const char * const bch2_cache_replacement_policies[] = { - BCH_CACHE_REPLACEMENT_POLICIES() +const char * const bch2_member_states[] = { + BCH_MEMBER_STATES() NULL }; -const char * const bch2_member_states[] = { - BCH_MEMBER_STATES() +const char * const bch2_jset_entry_types[] = { + BCH_JSET_ENTRY_TYPES() + NULL +}; + +const char * const bch2_fs_usage_types[] = { + BCH_FS_USAGE_TYPES() NULL }; #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", @@ -91,6 +138,16 @@ const char * const bch2_d_types[BCH_DT_MAX] = { [DT_SUBVOL] = "subvol", }; +u64 BCH2_NO_SB_OPT(const struct bch_sb *sb) +{ + BUG(); +} + +void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v) +{ + BUG(); +} + void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src) { #define x(_name, ...) \ @@ -146,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] = { \ @@ -204,108 +259,143 @@ static int bch2_mount_opt_lookup(const char *name) return bch2_opt_lookup(name); } -static int bch2_opt_validate(const struct bch_option *opt, const char *msg, u64 v) +int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err) { if (v < opt->min) { - if (msg) - pr_err("invalid %s%s: too small (min %llu)", - msg, opt->attr.name, opt->min); - return -ERANGE; + if (err) + prt_printf(err, "%s: too small (min %llu)", + opt->attr.name, opt->min); + return -BCH_ERR_ERANGE_option_too_small; } if (opt->max && v >= opt->max) { - if (msg) - pr_err("invalid %s%s: too big (max %llu)", - msg, opt->attr.name, opt->max); - return -ERANGE; + if (err) + prt_printf(err, "%s: too big (max %llu)", + opt->attr.name, opt->max); + return -BCH_ERR_ERANGE_option_too_big; } if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) { - if (msg) - pr_err("invalid %s %s: not a multiple of 512", - msg, opt->attr.name); + if (err) + prt_printf(err, "%s: not a multiple of 512", + opt->attr.name); return -EINVAL; } if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) { - if (msg) - pr_err("invalid %s%s: must be a power of two", - msg, opt->attr.name); + if (err) + prt_printf(err, "%s: must be a power of two", + opt->attr.name); return -EINVAL; } + if (opt->fn.validate) + return opt->fn.validate(v, err); + return 0; } -int bch2_opt_parse(struct bch_fs *c, const char *msg, +int bch2_opt_parse(struct bch_fs *c, const struct bch_option *opt, - const char *val, u64 *res) + const char *val, u64 *res, + struct printbuf *err) { ssize_t ret; switch (opt->type) { case BCH_OPT_BOOL: - ret = kstrtou64(val, 10, res); - if (ret < 0) + 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); 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); - if (ret < 0) + if (ret < 0) { + if (err) + prt_printf(err, "%s: must be a number", + opt->attr.name); return ret; + } 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 (ret < 0) { + if (err) + prt_printf(err, "%s: invalid selection", + opt->attr.name); return ret; + } *res = ret; break; case BCH_OPT_FN: - if (!c) - return 0; - - ret = opt->parse(c, val, res); - if (ret < 0) + ret = opt->fn.parse(c, val, res, err); + if (ret < 0) { + if (err) + prt_printf(err, "%s: parse error", + opt->attr.name); return ret; + } } - return bch2_opt_validate(opt, msg, *res); + return bch2_opt_validate(opt, *res, err); } -void bch2_opt_to_text(struct printbuf *out, struct bch_fs *c, +void bch2_opt_to_text(struct printbuf *out, + struct bch_fs *c, struct bch_sb *sb, const struct bch_option *opt, u64 v, unsigned flags) { if (flags & OPT_SHOW_MOUNT_STYLE) { if (opt->type == BCH_OPT_BOOL) { - pr_buf(out, "%s%s", + prt_printf(out, "%s%s", v ? "" : "no", opt->attr.name); return; } - pr_buf(out, "%s=", opt->attr.name); + prt_printf(out, "%s=", opt->attr.name); } switch (opt->type) { case BCH_OPT_BOOL: case BCH_OPT_UINT: if (opt->flags & OPT_HUMAN_READABLE) - bch2_hprint(out, v); + prt_human_readable_u64(out, v); else - pr_buf(out, "%lli", v); + prt_printf(out, "%lli", v); break; case BCH_OPT_STR: if (flags & OPT_SHOW_FULL_LIST) - bch2_string_opt_to_text(out, opt->choices, v); + prt_string_option(out, opt->choices, v); else - pr_buf(out, opt->choices[v]); + prt_str(out, opt->choices[v]); break; case BCH_OPT_FN: - opt->to_text(out, c, v); + opt->fn.to_text(out, c, sb, v); break; default: BUG(); @@ -351,11 +441,19 @@ int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts, char *copied_opts, *copied_opts_start; char *opt, *name, *val; int ret, id; + struct printbuf err = PRINTBUF; u64 v; 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; @@ -365,32 +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; - - ret = bch2_opt_parse(c, "mount option ", - &bch2_opt_table[id], val, &v); - if (ret < 0) - goto bad_val; - } else { - id = bch2_mount_opt_lookup(name); - v = 1; + id = bch2_mount_opt_lookup(name); - 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; @@ -403,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); } @@ -414,18 +504,31 @@ bad_opt: ret = -1; goto out; bad_val: - pr_err("Invalid value %s for mount option %s", val, name); - ret = -1; - goto out; -no_val: - pr_err("Mount option %s requires a value", name); + pr_err("Invalid mount option %s", err.buf); ret = -1; goto out; out: kfree(copied_opts_start); + printbuf_exit(&err); return ret; } +u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id) +{ + const struct bch_option *opt = bch2_opt_table + id; + u64 v; + + v = opt->get_sb(sb); + + if (opt->flags & OPT_SB_FIELD_ILOG2) + v = 1ULL << v; + + if (opt->flags & OPT_SB_FIELD_SECTORS) + v <<= 9; + + return v; +} + /* * Initial options from superblock - here we don't want any options undefined, * any options the superblock doesn't specify are set to 0: @@ -433,28 +536,14 @@ out: int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb) { unsigned id; - int ret; for (id = 0; id < bch2_opts_nr; id++) { const struct bch_option *opt = bch2_opt_table + id; - u64 v; - if (opt->get_sb == NO_SB_OPT) + if (opt->get_sb == BCH2_NO_SB_OPT) continue; - v = opt->get_sb(sb); - - if (opt->flags & OPT_SB_FIELD_ILOG2) - v = 1ULL << v; - - if (opt->flags & OPT_SB_FIELD_SECTORS) - v <<= 9; - - ret = bch2_opt_validate(opt, "superblock option ", v); - if (ret) - return ret; - - bch2_opt_set_by_id(opts, id, v); + bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id)); } return 0; @@ -462,7 +551,7 @@ int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb) void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v) { - if (opt->set_sb == SET_NO_SB_OPT) + if (opt->set_sb == SET_BCH2_NO_SB_OPT) return; if (opt->flags & OPT_SB_FIELD_SECTORS) @@ -476,7 +565,7 @@ void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v) void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v) { - if (opt->set_sb == SET_NO_SB_OPT) + if (opt->set_sb == SET_BCH2_NO_SB_OPT) return; mutex_lock(&c->sb_lock); @@ -489,33 +578,11 @@ void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v) struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src) { - struct bch_io_opts ret = { 0 }; -#define x(_name, _bits) \ - if (opt_defined(src, _name)) \ - opt_set(ret, _name, src._name); - BCH_INODE_OPTS() -#undef x - return ret; -} - -struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts src) -{ - struct bch_opts ret = { 0 }; -#define x(_name, _bits) \ - if (opt_defined(src, _name)) \ - opt_set(ret, _name, src._name); - BCH_INODE_OPTS() -#undef x - return ret; -} - -void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src) -{ -#define x(_name, _bits) \ - if (opt_defined(src, _name)) \ - opt_set(*dst, _name, src._name); + return (struct bch_io_opts) { +#define x(_name, _bits) ._name = src._name, BCH_INODE_OPTS() #undef x + }; } bool bch2_opt_is_inode_opt(enum bch_opt_id id)