1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
7 #include "disk_groups.h"
12 const char * const bch2_error_actions[] = {
19 const char * const bch2_sb_features[] = {
26 const char * const bch2_csum_opts[] = {
33 const char * const bch2_compression_opts[] = {
35 BCH_COMPRESSION_OPTS()
40 const char * const bch2_str_hash_types[] = {
47 const char * const bch2_data_types[] = {
57 const char * const bch2_cache_replacement_policies[] = {
64 /* Default is -1; we skip past it for struct cached_dev's cache mode */
65 const char * const bch2_cache_modes[] = {
74 const char * const bch2_dev_state[] = {
82 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
84 #define x(_name, ...) \
85 if (opt_defined(src, _name)) \
86 opt_set(*dst, _name, src._name);
92 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
95 #define x(_name, ...) \
97 return opt_defined(*opts, _name);
105 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
108 #define x(_name, ...) \
118 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
121 #define x(_name, ...) \
123 opt_set(*opts, _name, v); \
133 * Initial options from superblock - here we don't want any options undefined,
134 * any options the superblock doesn't specify are set to 0:
136 struct bch_opts bch2_opts_from_sb(struct bch_sb *sb)
138 struct bch_opts opts = bch2_opts_empty();
140 #define x(_name, _bits, _mode, _type, _sb_opt, ...) \
141 if (_sb_opt != NO_SB_OPT) \
142 opt_set(opts, _name, _sb_opt(sb));
149 const struct bch_option bch2_opt_table[] = {
150 #define OPT_BOOL() .type = BCH_OPT_BOOL
151 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, .min = _min, .max = _max
152 #define OPT_SECTORS(_min, _max) .type = BCH_OPT_SECTORS, .min = _min, .max = _max
153 #define OPT_STR(_choices) .type = BCH_OPT_STR, .choices = _choices
154 #define OPT_FN(_fn) .type = BCH_OPT_FN, \
155 .parse = _fn##_parse, \
156 .to_text = _fn##_to_text
158 #define x(_name, _bits, _mode, _type, _sb_opt, _default, _hint, _help) \
162 .mode = (_mode) & OPT_RUNTIME ? 0644 : 0444, \
167 .set_sb = SET_##_sb_opt, \
175 int bch2_opt_lookup(const char *name)
177 const struct bch_option *i;
179 for (i = bch2_opt_table;
180 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
182 if (!strcmp(name, i->attr.name))
183 return i - bch2_opt_table;
192 static const struct synonym bch_opt_synonyms[] = {
193 { "quota", "usrquota" },
196 static int bch2_mount_opt_lookup(const char *name)
198 const struct synonym *i;
200 for (i = bch_opt_synonyms;
201 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
203 if (!strcmp(name, i->s1))
206 return bch2_opt_lookup(name);
209 int bch2_opt_parse(struct bch_fs *c, const struct bch_option *opt,
210 const char *val, u64 *res)
216 ret = kstrtou64(val, 10, res);
224 ret = kstrtou64(val, 10, res);
228 if (*res < opt->min || *res >= opt->max)
231 case BCH_OPT_SECTORS:
232 ret = bch2_strtou64_h(val, res);
241 if (*res < opt->min || *res >= opt->max)
245 ret = match_string(opt->choices, -1, val);
255 return opt->parse(c, val, res);
261 void bch2_opt_to_text(struct printbuf *out, struct bch_fs *c,
262 const struct bch_option *opt, u64 v,
265 if (flags & OPT_SHOW_MOUNT_STYLE) {
266 if (opt->type == BCH_OPT_BOOL) {
273 pr_buf(out, "%s=", opt->attr.name);
279 pr_buf(out, "%lli", v);
281 case BCH_OPT_SECTORS:
285 if (flags & OPT_SHOW_FULL_LIST)
286 bch2_string_opt_to_text(out, opt->choices, v);
288 pr_buf(out, opt->choices[v]);
291 opt->to_text(out, c, v);
298 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
303 case Opt_compression:
304 case Opt_background_compression:
305 ret = bch2_check_set_has_compressed_data(c, v);
307 case Opt_erasure_code:
309 bch2_check_set_feature(c, BCH_FEATURE_ec);
316 int bch2_opts_check_may_set(struct bch_fs *c)
321 for (i = 0; i < bch2_opts_nr; i++) {
322 ret = bch2_opt_check_may_set(c, i,
323 bch2_opt_get_by_id(&c->opts, i));
331 int bch2_parse_mount_opts(struct bch_opts *opts, char *options)
333 char *opt, *name, *val;
337 while ((opt = strsep(&options, ",")) != NULL) {
338 name = strsep(&opt, "=");
342 id = bch2_mount_opt_lookup(name);
346 ret = bch2_opt_parse(NULL, &bch2_opt_table[id], val, &v);
350 id = bch2_mount_opt_lookup(name);
354 !strncmp("no", name, 2)) {
355 id = bch2_mount_opt_lookup(name + 2);
362 if (bch2_opt_table[id].type != BCH_OPT_BOOL)
366 if (!(bch2_opt_table[id].mode & OPT_MOUNT))
370 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
373 if ((id == Opt_usrquota ||
374 id == Opt_grpquota) &&
375 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
378 bch2_opt_set_by_id(opts, id, v);
383 pr_err("Bad mount option %s", name);
386 pr_err("Invalid value %s for mount option %s", val, name);
389 pr_err("Mount option %s requires a value", name);
395 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
397 struct bch_io_opts ret = { 0 };
398 #define x(_name, _bits) \
399 if (opt_defined(src, _name)) \
400 opt_set(ret, _name, src._name);
406 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts src)
408 struct bch_opts ret = { 0 };
409 #define x(_name, _bits) \
410 if (opt_defined(src, _name)) \
411 opt_set(ret, _name, src._name);
417 void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src)
419 #define x(_name, _bits) \
420 if (opt_defined(src, _name)) \
421 opt_set(*dst, _name, src._name);
426 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
428 static const enum bch_opt_id inode_opt_list[] = {
429 #define x(_name, _bits) Opt_##_name,
435 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
436 if (inode_opt_list[i] == id)