bcachefs-tools/libbcache/opts.c

242 lines
4.1 KiB
C
Raw Normal View History

2017-01-08 12:13:18 +03:00
#include <linux/kernel.h>
#include "opts.h"
#include "util.h"
const char * const bch_error_actions[] = {
"continue",
"remount-ro",
"panic",
NULL
};
const char * const bch_csum_types[] = {
"none",
"crc32c",
"crc64",
NULL
};
const char * const bch_compression_types[] = {
"none",
"lz4",
"gzip",
NULL
};
const char * const bch_str_hash_types[] = {
"crc32c",
"crc64",
"siphash",
NULL
};
2017-02-02 06:16:42 +03:00
const char * const bch_cache_replacement_policies[] = {
"lru",
"fifo",
"random",
NULL
};
/* Default is -1; we skip past it for struct cached_dev's cache mode */
const char * const bch_cache_modes[] = {
"default",
"writethrough",
"writeback",
"writearound",
"none",
NULL
};
const char * const bch_dev_state[] = {
2017-03-11 00:40:01 +03:00
"readwrite",
2017-02-02 06:16:42 +03:00
"readonly",
"failed",
"spare",
NULL
};
const struct bch_option bch_opt_table[] = {
#define OPT_BOOL() .type = BCH_OPT_BOOL
#define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, .min = _min, .max = _max
#define OPT_STR(_choices) .type = BCH_OPT_STR, .choices = _choices
#define BCH_OPT(_name, _mode, _sb_opt, _bits, _type) \
[Opt_##_name] = { \
.name = #_name, \
.set_sb = SET_##_sb_opt, \
_type \
},
2016-10-04 06:22:17 +03:00
BCH_VISIBLE_OPTS()
#undef BCH_OPT
2017-01-08 12:13:18 +03:00
};
static enum bch_opt_id bch_opt_lookup(const char *name)
2017-01-08 12:13:18 +03:00
{
const struct bch_option *i;
2017-01-08 12:13:18 +03:00
for (i = bch_opt_table;
i < bch_opt_table + ARRAY_SIZE(bch_opt_table);
i++)
if (!strcmp(name, i->name))
return i - bch_opt_table;
2017-01-08 12:13:18 +03:00
return -1;
}
static u64 bch_opt_get(struct bch_opts *opts, enum bch_opt_id id)
2017-01-08 12:13:18 +03:00
{
switch (id) {
#define BCH_OPT(_name, ...) \
case Opt_##_name: \
return opts->_name; \
2017-01-08 12:13:18 +03:00
BCH_VISIBLE_OPTS()
#undef BCH_OPT
2017-01-08 12:13:18 +03:00
default:
BUG();
}
}
2017-01-08 12:13:18 +03:00
void bch_opt_set(struct bch_opts *opts, enum bch_opt_id id, u64 v)
{
switch (id) {
#define BCH_OPT(_name, ...) \
case Opt_##_name: \
opts->_name = v; \
break;
2017-01-08 12:13:18 +03:00
BCH_VISIBLE_OPTS()
#undef BCH_OPT
2017-01-08 12:13:18 +03:00
default:
BUG();
}
2017-01-08 12:13:18 +03:00
}
/*
* 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 bch_sb_opts(struct bch_sb *sb)
2017-01-08 12:13:18 +03:00
{
struct bch_opts opts = bch_opts_empty();
2017-01-08 12:13:18 +03:00
#define BCH_OPT(_name, _mode, _sb_opt, ...) \
if (_sb_opt != NO_SB_OPT) \
opts._name = _sb_opt(sb);
2017-01-08 12:13:18 +03:00
BCH_OPTS()
#undef BCH_OPT
2017-01-08 12:13:18 +03:00
return opts;
2017-01-08 12:13:18 +03:00
}
int parse_one_opt(enum bch_opt_id id, const char *val, u64 *res)
2017-01-08 12:13:18 +03:00
{
const struct bch_option *opt = &bch_opt_table[id];
ssize_t ret;
switch (opt->type) {
case BCH_OPT_BOOL:
ret = kstrtou64(val, 10, res);
if (ret < 0)
return ret;
if (*res > 1)
return -ERANGE;
break;
case BCH_OPT_UINT:
ret = kstrtou64(val, 10, res);
if (ret < 0)
return ret;
if (*res < opt->min || *res >= opt->max)
return -ERANGE;
break;
case BCH_OPT_STR:
ret = bch_read_string_list(val, opt->choices);
if (ret < 0)
return ret;
*res = ret;
break;
2017-01-08 12:13:18 +03:00
}
return 0;
2017-01-08 12:13:18 +03:00
}
int bch_parse_mount_opts(struct bch_opts *opts, char *options)
2017-01-08 12:13:18 +03:00
{
char *opt, *name, *val;
2017-03-11 00:40:01 +03:00
int ret, id;
u64 v;
while ((opt = strsep(&options, ",")) != NULL) {
name = strsep(&opt, "=");
val = opt;
if (val) {
id = bch_opt_lookup(name);
if (id < 0)
return -EINVAL;
ret = parse_one_opt(id, val, &v);
if (ret < 0)
return ret;
} else {
id = bch_opt_lookup(name);
v = 1;
if (id < 0 &&
!strncmp("no", name, 2)) {
id = bch_opt_lookup(name + 2);
v = 0;
}
if (bch_opt_table[id].type != BCH_OPT_BOOL)
return -EINVAL;
}
2017-01-08 12:13:18 +03:00
bch_opt_set(opts, id, v);
}
2017-01-08 12:13:18 +03:00
return 0;
}
2017-01-08 12:13:18 +03:00
enum bch_opt_id bch_parse_sysfs_opt(const char *name, const char *val,
u64 *res)
{
enum bch_opt_id id = bch_opt_lookup(name);
int ret;
2017-01-08 12:13:18 +03:00
if (id < 0)
return -EINVAL;
2017-01-08 12:13:18 +03:00
ret = parse_one_opt(id, val, res);
if (ret < 0)
return ret;
2017-01-08 12:13:18 +03:00
return id;
}
2017-01-08 12:13:18 +03:00
ssize_t bch_opt_show(struct bch_opts *opts, const char *name,
char *buf, size_t size)
{
enum bch_opt_id id = bch_opt_lookup(name);
const struct bch_option *opt;
u64 v;
2017-01-08 12:13:18 +03:00
if (id < 0)
return -EINVAL;
v = bch_opt_get(opts, id);
opt = &bch_opt_table[id];
return opt->type == BCH_OPT_STR
? bch_snprint_string_list(buf, size, opt->choices, v)
: snprintf(buf, size, "%lli\n", v);
2017-01-08 12:13:18 +03:00
}