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[] = {
54 const char * const bch2_cache_replacement_policies[] = {
61 /* Default is -1; we skip past it for struct cached_dev's cache mode */
62 const char * const bch2_cache_modes[] = {
71 const char * const bch2_dev_state[] = {
79 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
81 #define x(_name, ...) \
82 if (opt_defined(src, _name)) \
83 opt_set(*dst, _name, src._name);
89 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
92 #define x(_name, ...) \
94 return opt_defined(*opts, _name);
102 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
105 #define x(_name, ...) \
115 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
118 #define x(_name, ...) \
120 opt_set(*opts, _name, v); \
130 * Initial options from superblock - here we don't want any options undefined,
131 * any options the superblock doesn't specify are set to 0:
133 struct bch_opts bch2_opts_from_sb(struct bch_sb *sb)
135 struct bch_opts opts = bch2_opts_empty();
137 #define x(_name, _bits, _mode, _type, _sb_opt, ...) \
138 if (_sb_opt != NO_SB_OPT) \
139 opt_set(opts, _name, _sb_opt(sb));
146 const struct bch_option bch2_opt_table[] = {
147 #define OPT_BOOL() .type = BCH_OPT_BOOL
148 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, .min = _min, .max = _max
149 #define OPT_SECTORS(_min, _max) .type = BCH_OPT_SECTORS, .min = _min, .max = _max
150 #define OPT_STR(_choices) .type = BCH_OPT_STR, .choices = _choices
151 #define OPT_FN(_fn) .type = BCH_OPT_FN, \
152 .parse = _fn##_parse, \
153 .to_text = _fn##_to_text
155 #define x(_name, _bits, _mode, _type, _sb_opt, _default, _hint, _help) \
159 .mode = (_mode) & OPT_RUNTIME ? 0644 : 0444, \
164 .set_sb = SET_##_sb_opt, \
172 int bch2_opt_lookup(const char *name)
174 const struct bch_option *i;
176 for (i = bch2_opt_table;
177 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
179 if (!strcmp(name, i->attr.name))
180 return i - bch2_opt_table;
189 static const struct synonym bch_opt_synonyms[] = {
190 { "quota", "usrquota" },
193 static int bch2_mount_opt_lookup(const char *name)
195 const struct synonym *i;
197 for (i = bch_opt_synonyms;
198 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
200 if (!strcmp(name, i->s1))
203 return bch2_opt_lookup(name);
206 int bch2_opt_parse(struct bch_fs *c, const struct bch_option *opt,
207 const char *val, u64 *res)
213 ret = kstrtou64(val, 10, res);
221 ret = kstrtou64(val, 10, res);
225 if (*res < opt->min || *res >= opt->max)
228 case BCH_OPT_SECTORS:
229 ret = bch2_strtou64_h(val, res);
238 if (*res < opt->min || *res >= opt->max)
242 ret = match_string(opt->choices, -1, val);
252 return opt->parse(c, val, res);
258 void bch2_opt_to_text(struct printbuf *out, struct bch_fs *c,
259 const struct bch_option *opt, u64 v,
262 if (flags & OPT_SHOW_MOUNT_STYLE) {
263 if (opt->type == BCH_OPT_BOOL) {
270 pr_buf(out, "%s=", opt->attr.name);
276 pr_buf(out, "%lli", v);
278 case BCH_OPT_SECTORS:
282 if (flags & OPT_SHOW_FULL_LIST)
283 bch2_string_opt_to_text(out, opt->choices, v);
285 pr_buf(out, opt->choices[v]);
288 opt->to_text(out, c, v);
295 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
300 case Opt_compression:
301 case Opt_background_compression:
302 ret = bch2_check_set_has_compressed_data(c, v);
304 case Opt_erasure_code:
306 bch2_check_set_feature(c, BCH_FEATURE_ec);
313 int bch2_opts_check_may_set(struct bch_fs *c)
318 for (i = 0; i < bch2_opts_nr; i++) {
319 ret = bch2_opt_check_may_set(c, i,
320 bch2_opt_get_by_id(&c->opts, i));
328 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
331 char *opt, *name, *val;
335 while ((opt = strsep(&options, ",")) != NULL) {
336 name = strsep(&opt, "=");
340 id = bch2_mount_opt_lookup(name);
344 ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v);
348 id = bch2_mount_opt_lookup(name);
352 !strncmp("no", name, 2)) {
353 id = bch2_mount_opt_lookup(name + 2);
360 if (bch2_opt_table[id].type != BCH_OPT_BOOL)
364 if (!(bch2_opt_table[id].mode & OPT_MOUNT))
368 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
371 if ((id == Opt_usrquota ||
372 id == Opt_grpquota) &&
373 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
376 bch2_opt_set_by_id(opts, id, v);
381 pr_err("Bad mount option %s", name);
384 pr_err("Invalid value %s for mount option %s", val, name);
387 pr_err("Mount option %s requires a value", name);
393 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
395 struct bch_io_opts ret = { 0 };
396 #define x(_name, _bits) \
397 if (opt_defined(src, _name)) \
398 opt_set(ret, _name, src._name);
404 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts src)
406 struct bch_opts ret = { 0 };
407 #define x(_name, _bits) \
408 if (opt_defined(src, _name)) \
409 opt_set(ret, _name, src._name);
415 void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src)
417 #define x(_name, _bits) \
418 if (opt_defined(src, _name)) \
419 opt_set(*dst, _name, src._name);
424 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
426 static const enum bch_opt_id inode_opt_list[] = {
427 #define x(_name, _bits) Opt_##_name,
433 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
434 if (inode_opt_list[i] == id)