summaryrefslogtreecommitdiff
path: root/libbcache/opts.c
blob: 60a2a4d1a5aefbcb373dc63326202d60bac1be41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

#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",
	"sha1",
	NULL
};

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_cache_state[] = {
	"active",
	"readonly",
	"failed",
	"spare",
	NULL
};


const char * const bch_bool_opt[] = {
	"0",
	"1",
	NULL
};

const char * const bch_uint_opt[] = {
	NULL
};

enum bch_opts {
#define CACHE_SET_OPT(_name, _choices, _min, _max, _sb_opt, _perm)	\
	Opt_##_name,

	CACHE_SET_VISIBLE_OPTS()
#undef CACHE_SET_OPT

	Opt_bad_opt,
};

struct bch_option {
	const char		*name;
	const char * const	*opts;
	unsigned long		min, max;
};

struct bch_opt_result {
	enum bch_opts		opt;
	unsigned		val;
};

static int parse_bool_opt(const struct bch_option *opt, const char *s)
{
	if (!strcmp(opt->name, s))
		return true;

	if (!strncmp("no", s, 2) && !strcmp(opt->name, s + 2))
		return false;

	return -1;
}

static int parse_uint_opt(const struct bch_option *opt, const char *s)
{
	unsigned long v;
	int ret;

	if (strncmp(opt->name, s, strlen(opt->name)))
		return -1;

	s += strlen(opt->name);

	if (*s != '=')
		return -1;

	s++;

	ret = kstrtoul(s, 10, &v);
	if (ret)
		return ret;

	if (v < opt->min || v >= opt->max)
		return -ERANGE;

	return 0;
}

static int parse_string_opt(const struct bch_option *opt, const char *s)
{
	if (strncmp(opt->name, s, strlen(opt->name)))
		return -1;

	s += strlen(opt->name);

	if (*s != '=')
		return -1;

	s++;

	return bch_read_string_list(s, opt->opts);
}

static struct bch_opt_result parse_one_opt(const char *opt)
{
	static const struct bch_option opt_table[] = {
#define CACHE_SET_OPT(_name, _choices, _min, _max, _sb_opt, _perm)	\
		[Opt_##_name] = {					\
			.name = #_name,					\
			.opts = _choices,				\
			.min = _min,					\
			.max = _max,					\
		},
		CACHE_SET_VISIBLE_OPTS()
#undef CACHE_SET_OPT
	}, *i;

	for (i = opt_table;
	     i < opt_table + ARRAY_SIZE(opt_table);
	     i++) {
		int res = i->opts == bch_bool_opt ? parse_bool_opt(i, opt)
			: i->opts == bch_uint_opt ? parse_uint_opt(i, opt)
			: parse_string_opt(i, opt);

		if (res >= 0)
			return (struct bch_opt_result) {
				i - opt_table, res
			};
	}

	return (struct bch_opt_result) { Opt_bad_opt };
}

int bch_parse_options(struct cache_set_opts *opts, int flags, char *options)
{
	char *p;

	*opts = cache_set_opts_empty();

	opts->read_only = (flags & MS_RDONLY) != 0;

	if (!options)
		return 0;

	while ((p = strsep(&options, ",")) != NULL) {
		struct bch_opt_result res = parse_one_opt(p);

		switch (res.opt) {
#define CACHE_SET_OPT(_name, _choices, _min, _max, _sb_opt, _perm)	\
		case Opt_##_name:					\
			opts->_name = res.val;				\
			break;

		CACHE_SET_VISIBLE_OPTS()
#undef CACHE_SET_OPT

		case Opt_bad_opt:
			return -EINVAL;
		default:
			BUG();
		}
	}

	return 0;
}