X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libbcachefs%2Fopts.c;h=8294f56e45d5a503821e1bf1d15539f3bee8ae89;hb=2252eecec7e072dfdc66cfea6da0ee6ed648a858;hp=13a9a2fcd575853e07bcde406a364f92aa741495;hpb=f96ba8e0aac91f2650270e9639359243cb9ac2d1;p=bcachefs-tools-debian diff --git a/libbcachefs/opts.c b/libbcachefs/opts.c index 13a9a2f..8294f56 100644 --- a/libbcachefs/opts.c +++ b/libbcachefs/opts.c @@ -5,74 +5,154 @@ #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, + +const char * const bch2_iops_measurements[] = { + BCH_IOPS_MEASUREMENTS() + NULL +}; + const char * const bch2_error_actions[] = { - "continue", - "remount-ro", - "panic", + 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 +}; + +const char * const bch2_sb_compat[] = { + BCH_SB_COMPAT() + NULL +}; + +const char * const __bch2_btree_ids[] = { + BCH_BTREE_IDS() NULL }; const char * const bch2_csum_types[] = { - "none", - "crc32c", - "crc64", + BCH_CSUM_TYPES() + NULL +}; + +const char * const bch2_csum_opts[] = { + BCH_CSUM_OPTS() NULL }; const char * const bch2_compression_types[] = { - "none", - "lz4", - "gzip", - "zstd", + BCH_COMPRESSION_TYPES() + NULL +}; + +const char * const bch2_compression_opts[] = { + BCH_COMPRESSION_OPTS() NULL }; const char * const bch2_str_hash_types[] = { - "crc32c", - "crc64", - "siphash", + BCH_STR_HASH_TYPES() + NULL +}; + +const char * const bch2_str_hash_opts[] = { + BCH_STR_HASH_OPTS() NULL }; const char * const bch2_data_types[] = { - "none", - "sb", - "journal", - "btree", - "data", - "cached", + BCH_DATA_TYPES() NULL }; -const char * const bch2_cache_replacement_policies[] = { - "lru", - "fifo", - "random", +const char * const bch2_member_states[] = { + BCH_MEMBER_STATES() NULL }; -/* Default is -1; we skip past it for struct cached_dev's cache mode */ -const char * const bch2_cache_modes[] = { - "default", - "writethrough", - "writeback", - "writearound", - "none", +const char * const bch2_jset_entry_types[] = { + BCH_JSET_ENTRY_TYPES() NULL }; -const char * const bch2_dev_state[] = { - "readwrite", - "readonly", - "failed", - "spare", +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", + [DT_CHR] = "chr", + [DT_DIR] = "dir", + [DT_BLK] = "blk", + [DT_REG] = "reg", + [DT_LNK] = "lnk", + [DT_SOCK] = "sock", + [DT_WHT] = "whiteout", + [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, ...) \ @@ -123,41 +203,25 @@ void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v) } } -/* - * Initial options from superblock - here we don't want any options undefined, - * any options the superblock doesn't specify are set to 0: - */ -struct bch_opts bch2_opts_from_sb(struct bch_sb *sb) -{ - struct bch_opts opts = bch2_opts_empty(); - -#define x(_name, _bits, _mode, _type, _sb_opt, ...) \ - if (_sb_opt != NO_SB_OPT) \ - opt_set(opts, _name, _sb_opt(sb)); - BCH_OPTS() -#undef x - - return opts; -} - const struct bch_option bch2_opt_table[] = { -#define OPT_BOOL() .type = BCH_OPT_BOOL -#define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, .min = _min, .max = _max -#define OPT_SECTORS(_min, _max) .type = BCH_OPT_SECTORS, .min = _min, .max = _max -#define OPT_STR(_choices) .type = BCH_OPT_STR, .choices = _choices -#define OPT_FN(_fn) .type = BCH_OPT_FN, \ - .parse = _fn##_parse, \ - .to_text = _fn##_to_text - -#define x(_name, _bits, _mode, _type, _sb_opt, _default, _hint, _help) \ +#define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2 +#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), \ + .choices = _choices +#define OPT_FN(_fn) .type = BCH_OPT_FN, .fn = _fn + +#define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \ [Opt_##_name] = { \ .attr = { \ .name = #_name, \ - .mode = (_mode) & OPT_RUNTIME ? 0644 : 0444, \ + .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \ }, \ - .mode = _mode, \ + .flags = _flags, \ .hint = _hint, \ .help = _help, \ + .get_sb = _sb_opt, \ .set_sb = SET_##_sb_opt, \ _type \ }, @@ -200,89 +264,140 @@ static int bch2_mount_opt_lookup(const char *name) return bch2_opt_lookup(name); } -int bch2_opt_parse(struct bch_fs *c, const struct bch_option *opt, - const char *val, u64 *res) +int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err) +{ + if (v < opt->min) { + if (err) + prt_printf(err, "%s: too small (min %llu)", + opt->attr.name, opt->min); + return -ERANGE; + } + + if (opt->max && v >= opt->max) { + if (err) + prt_printf(err, "%s: too big (max %llu)", + opt->attr.name, opt->max); + return -ERANGE; + } + + 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; + } + + 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 0; +} + +int bch2_opt_parse(struct bch_fs *c, + const struct bch_option *opt, + 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) - return ret; + if (val) { + ret = kstrtou64(val, 10, res); + } else { + ret = 0; + *res = 1; + } - if (*res > 1) - return -ERANGE; - break; - case BCH_OPT_UINT: - ret = kstrtou64(val, 10, res); - if (ret < 0) + if (ret < 0 || (*res != 0 && *res != 1)) { + if (err) + prt_printf(err, "%s: must be bool", opt->attr.name); return ret; - - if (*res < opt->min || *res >= opt->max) - return -ERANGE; + } break; - case BCH_OPT_SECTORS: - ret = bch2_strtou64_h(val, res); - if (ret < 0) - return ret; - - if (*res & 511) + case BCH_OPT_UINT: + if (!val) { + prt_printf(err, "%s: required value", + opt->attr.name); return -EINVAL; + } - *res >>= 9; - - if (*res < opt->min || *res >= opt->max) - return -ERANGE; + ret = opt->flags & OPT_HUMAN_READABLE + ? bch2_strtou64_h(val, res) + : kstrtou64(val, 10, res); + 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 -EINVAL; - - return opt->parse(c, val, res); + 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 0; + 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: - pr_buf(out, "%lli", v); - break; - case BCH_OPT_SECTORS: - bch2_hprint(out, v); + if (opt->flags & OPT_HUMAN_READABLE) + prt_human_readable_u64(out, v); + else + 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(); @@ -299,15 +414,8 @@ int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v) ret = bch2_check_set_has_compressed_data(c, v); break; case Opt_erasure_code: - if (v && - !(c->sb.features & (1ULL << BCH_FEATURE_EC))) { - mutex_lock(&c->sb_lock); - c->disk_sb.sb->features[0] |= - cpu_to_le64(1ULL << BCH_FEATURE_EC); - - bch2_write_super(c); - mutex_unlock(&c->sb_lock); - } + if (v) + bch2_check_set_feature(c, BCH_FEATURE_ec); break; } @@ -329,42 +437,49 @@ int bch2_opts_check_may_set(struct bch_fs *c) return 0; } -int bch2_parse_mount_opts(struct bch_opts *opts, char *options) +int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts, + char *options) { + char *copied_opts, *copied_opts_start; char *opt, *name, *val; int ret, id; + struct printbuf err = PRINTBUF; u64 v; - while ((opt = strsep(&options, ",")) != NULL) { - name = strsep(&opt, "="); - val = opt; + if (!options) + return 0; - if (val) { - id = bch2_mount_opt_lookup(name); - if (id < 0) - goto bad_opt; + /* + * sys_fsconfig() is now occasionally providing us with option lists + * starting with a comma - weird. + */ + if (*options == ',') + options++; - ret = bch2_opt_parse(NULL, &bch2_opt_table[id], val, &v); - if (ret < 0) - goto bad_val; - } else { - id = bch2_mount_opt_lookup(name); - v = 1; + copied_opts = kstrdup(options, GFP_KERNEL); + if (!copied_opts) + return -1; + copied_opts_start = copied_opts; - if (id < 0 && - !strncmp("no", name, 2)) { - id = bch2_mount_opt_lookup(name + 2); - v = 0; - } + while ((opt = strsep(&copied_opts, ",")) != NULL) { + name = strsep(&opt, "="); + val = opt; - if (id < 0) - goto bad_opt; + id = bch2_mount_opt_lookup(name); - 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"; } - if (!(bch2_opt_table[id].mode & OPT_MOUNT)) + /* Unknown options are ignored: */ + if (id < 0) + continue; + + if (!(bch2_opt_table[id].flags & OPT_MOUNT)) goto bad_opt; if (id == Opt_acl && @@ -376,52 +491,100 @@ int bch2_parse_mount_opts(struct bch_opts *opts, char *options) !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); } - return 0; + ret = 0; + goto out; + bad_opt: pr_err("Bad mount option %s", name); - return -1; + ret = -1; + goto out; bad_val: - pr_err("Invalid value %s for mount option %s", val, name); - return -1; -no_val: - pr_err("Mount option %s requires a value", name); - return -1; + pr_err("Invalid mount option %s", err.buf); + ret = -1; + goto out; +out: + kfree(copied_opts_start); + printbuf_exit(&err); + return ret; } -/* io opts: */ +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; -struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src) + 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: + */ +int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb) { - 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; + unsigned id; + + for (id = 0; id < bch2_opts_nr; id++) { + const struct bch_option *opt = bch2_opt_table + id; + + if (opt->get_sb == BCH2_NO_SB_OPT) + continue; + + bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id)); + } + + return 0; } -struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts src) +void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v) { - 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; + if (opt->set_sb == SET_BCH2_NO_SB_OPT) + return; + + if (opt->flags & OPT_SB_FIELD_SECTORS) + v >>= 9; + + if (opt->flags & OPT_SB_FIELD_ILOG2) + v = ilog2(v); + + opt->set_sb(sb, v); } -void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src) +void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v) { -#define x(_name, _bits) \ - if (opt_defined(src, _name)) \ - opt_set(*dst, _name, src._name); + if (opt->set_sb == SET_BCH2_NO_SB_OPT) + return; + + mutex_lock(&c->sb_lock); + __bch2_opt_set_sb(c->disk_sb.sb, opt, v); + bch2_write_super(c); + mutex_unlock(&c->sb_lock); +} + +/* io opts: */ + +struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src) +{ + 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)