bcachefs-tools/libbcachefs/opts.c

250 lines
4.3 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 bch2_error_actions[] = {
2017-01-08 12:13:18 +03:00
"continue",
"remount-ro",
"panic",
NULL
};
const char * const bch2_csum_types[] = {
2017-01-08 12:13:18 +03:00
"none",
"crc32c",
"crc64",
NULL
};
const char * const bch2_compression_types[] = {
2017-01-08 12:13:18 +03:00
"none",
"lz4",
"gzip",
NULL
};
const char * const bch2_str_hash_types[] = {
2017-01-08 12:13:18 +03:00
"crc32c",
"crc64",
"siphash",
NULL
};
const char * const bch2_data_types[] = {
"none",
"sb",
"journal",
"btree",
"data",
NULL
};
const char * const bch2_cache_replacement_policies[] = {
2017-02-02 06:16:42 +03:00
"lru",
"fifo",
"random",
NULL
};
/* Default is -1; we skip past it for struct cached_dev's cache mode */
const char * const bch2_cache_modes[] = {
2017-02-02 06:16:42 +03:00
"default",
"writethrough",
"writeback",
"writearound",
"none",
NULL
};
const char * const bch2_dev_state[] = {
2017-03-11 00:40:01 +03:00
"readwrite",
2017-02-02 06:16:42 +03:00
"readonly",
"failed",
"spare",
NULL
};
void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
2017-01-08 12:13:18 +03:00
{
#define BCH_OPT(_name, ...) \
if (opt_defined(src, _name)) \
opt_set(*dst, _name, src._name);
2017-01-08 12:13:18 +03:00
BCH_OPTS()
#undef BCH_OPT
2017-01-08 12:13:18 +03:00
}
u64 bch2_opt_get_by_id(const 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_OPTS()
#undef BCH_OPT
2017-01-08 12:13:18 +03:00
default:
BUG();
}
}
2017-01-08 12:13:18 +03:00
void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
{
switch (id) {
#define BCH_OPT(_name, ...) \
case Opt_##_name: \
opt_set(*opts, _name, v); \
break;
2017-01-08 12:13:18 +03:00
BCH_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 bch2_opts_from_sb(struct bch_sb *sb)
2017-01-08 12:13:18 +03:00
{
struct bch_opts opts = bch2_opts_empty();
2017-01-08 12:13:18 +03:00
#define BCH_OPT(_name, _bits, _mode, _type, _sb_opt, _default) \
if (_sb_opt != NO_SB_OPT) \
opt_set(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
}
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_STR(_choices) .type = BCH_OPT_STR, .choices = _choices
#define BCH_OPT(_name, _bits, _mode, _type, _sb_opt, _default) \
[Opt_##_name] = { \
.attr = { \
.name = #_name, \
.mode = _mode == OPT_RUNTIME ? 0644 : 0444, \
}, \
.mode = _mode, \
.set_sb = SET_##_sb_opt, \
_type \
},
BCH_OPTS()
#undef BCH_OPT
};
static int bch2_opt_lookup(const char *name)
{
const struct bch_option *i;
for (i = bch2_opt_table;
i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
i++)
if (!strcmp(name, i->attr.name))
return i - bch2_opt_table;
return -1;
}
int bch2_opt_parse(const struct bch_option *opt, const char *val, u64 *res)
2017-01-08 12:13:18 +03:00
{
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 = bch2_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 bch2_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 = bch2_opt_lookup(name);
if (id < 0)
goto bad_opt;
ret = bch2_opt_parse(&bch2_opt_table[id], val, &v);
if (ret < 0)
goto bad_val;
} else {
id = bch2_opt_lookup(name);
v = 1;
if (id < 0 &&
!strncmp("no", name, 2)) {
id = bch2_opt_lookup(name + 2);
v = 0;
}
if (id < 0)
goto bad_opt;
2017-01-08 12:13:18 +03:00
if (bch2_opt_table[id].type != BCH_OPT_BOOL)
goto no_val;
}
2017-01-08 12:13:18 +03:00
if (bch2_opt_table[id].mode < OPT_MOUNT)
goto bad_opt;
2017-01-08 12:13:18 +03:00
if (id == Opt_acl &&
!IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
goto bad_opt;
bch2_opt_set_by_id(opts, id, v);
}
return 0;
bad_opt:
pr_err("Bad mount option %s", name);
return -1;
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;
2017-01-08 12:13:18 +03:00
}