1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
7 #include "disk_groups.h"
14 const char * const bch2_error_actions[] = {
19 const char * const bch2_sb_features[] = {
24 const char * const bch2_sb_compat[] = {
29 const char * const bch2_btree_ids[] = {
34 const char * const bch2_csum_types[] = {
39 const char * const bch2_csum_opts[] = {
44 const char * const bch2_compression_types[] = {
45 BCH_COMPRESSION_TYPES()
49 const char * const bch2_compression_opts[] = {
50 BCH_COMPRESSION_OPTS()
54 const char * const bch2_str_hash_types[] = {
59 const char * const bch2_str_hash_opts[] = {
64 const char * const bch2_data_types[] = {
69 const char * const bch2_member_states[] = {
74 const char * const bch2_jset_entry_types[] = {
75 BCH_JSET_ENTRY_TYPES()
79 const char * const bch2_fs_usage_types[] = {
86 const char * const bch2_d_types[BCH_DT_MAX] = {
87 [DT_UNKNOWN] = "unknown",
95 [DT_WHT] = "whiteout",
96 [DT_SUBVOL] = "subvol",
99 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
101 #define x(_name, ...) \
102 if (opt_defined(src, _name)) \
103 opt_set(*dst, _name, src._name);
109 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
112 #define x(_name, ...) \
114 return opt_defined(*opts, _name);
122 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
125 #define x(_name, ...) \
135 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
138 #define x(_name, ...) \
140 opt_set(*opts, _name, v); \
149 const struct bch_option bch2_opt_table[] = {
150 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
151 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
152 .min = _min, .max = _max
153 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
154 .min = 0, .max = ARRAY_SIZE(_choices),\
156 #define OPT_FN(_fn) .type = BCH_OPT_FN, \
157 .parse = _fn##_parse, \
158 .to_text = _fn##_to_text
160 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
164 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
170 .set_sb = SET_##_sb_opt, \
178 int bch2_opt_lookup(const char *name)
180 const struct bch_option *i;
182 for (i = bch2_opt_table;
183 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
185 if (!strcmp(name, i->attr.name))
186 return i - bch2_opt_table;
195 static const struct synonym bch_opt_synonyms[] = {
196 { "quota", "usrquota" },
199 static int bch2_mount_opt_lookup(const char *name)
201 const struct synonym *i;
203 for (i = bch_opt_synonyms;
204 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
206 if (!strcmp(name, i->s1))
209 return bch2_opt_lookup(name);
212 static int bch2_opt_validate(const struct bch_option *opt, const char *msg, u64 v)
216 pr_err("invalid %s%s: too small (min %llu)",
217 msg, opt->attr.name, opt->min);
221 if (opt->max && v >= opt->max) {
223 pr_err("invalid %s%s: too big (max %llu)",
224 msg, opt->attr.name, opt->max);
228 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
230 pr_err("invalid %s %s: not a multiple of 512",
231 msg, opt->attr.name);
235 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
237 pr_err("invalid %s%s: must be a power of two",
238 msg, opt->attr.name);
245 int bch2_opt_parse(struct bch_fs *c, const char *msg,
246 const struct bch_option *opt,
247 const char *val, u64 *res)
253 ret = kstrtou64(val, 10, res);
258 ret = opt->flags & OPT_HUMAN_READABLE
259 ? bch2_strtou64_h(val, res)
260 : kstrtou64(val, 10, res);
265 ret = match_string(opt->choices, -1, val);
275 ret = opt->parse(c, val, res);
280 return bch2_opt_validate(opt, msg, *res);
283 void bch2_opt_to_text(struct printbuf *out, struct bch_fs *c,
284 const struct bch_option *opt, u64 v,
287 if (flags & OPT_SHOW_MOUNT_STYLE) {
288 if (opt->type == BCH_OPT_BOOL) {
295 pr_buf(out, "%s=", opt->attr.name);
301 if (opt->flags & OPT_HUMAN_READABLE)
304 pr_buf(out, "%lli", v);
307 if (flags & OPT_SHOW_FULL_LIST)
308 bch2_string_opt_to_text(out, opt->choices, v);
310 pr_buf(out, opt->choices[v]);
313 opt->to_text(out, c, v);
320 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
325 case Opt_compression:
326 case Opt_background_compression:
327 ret = bch2_check_set_has_compressed_data(c, v);
329 case Opt_erasure_code:
331 bch2_check_set_feature(c, BCH_FEATURE_ec);
338 int bch2_opts_check_may_set(struct bch_fs *c)
343 for (i = 0; i < bch2_opts_nr; i++) {
344 ret = bch2_opt_check_may_set(c, i,
345 bch2_opt_get_by_id(&c->opts, i));
353 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
356 char *copied_opts, *copied_opts_start;
357 char *opt, *name, *val;
364 copied_opts = kstrdup(options, GFP_KERNEL);
367 copied_opts_start = copied_opts;
369 while ((opt = strsep(&copied_opts, ",")) != NULL) {
370 name = strsep(&opt, "=");
374 id = bch2_mount_opt_lookup(name);
378 ret = bch2_opt_parse(c, "mount option ",
379 &bch2_opt_table[id], val, &v);
383 id = bch2_mount_opt_lookup(name);
387 !strncmp("no", name, 2)) {
388 id = bch2_mount_opt_lookup(name + 2);
395 if (bch2_opt_table[id].type != BCH_OPT_BOOL)
399 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
403 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
406 if ((id == Opt_usrquota ||
407 id == Opt_grpquota) &&
408 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
411 bch2_opt_set_by_id(opts, id, v);
418 pr_err("Bad mount option %s", name);
422 pr_err("Invalid value %s for mount option %s", val, name);
426 pr_err("Mount option %s requires a value", name);
430 kfree(copied_opts_start);
435 * Initial options from superblock - here we don't want any options undefined,
436 * any options the superblock doesn't specify are set to 0:
438 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
443 for (id = 0; id < bch2_opts_nr; id++) {
444 const struct bch_option *opt = bch2_opt_table + id;
447 if (opt->get_sb == NO_SB_OPT)
452 if (opt->flags & OPT_SB_FIELD_ILOG2)
455 if (opt->flags & OPT_SB_FIELD_SECTORS)
458 ret = bch2_opt_validate(opt, "superblock option ", v);
462 bch2_opt_set_by_id(opts, id, v);
468 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
470 if (opt->set_sb == SET_NO_SB_OPT)
473 if (opt->flags & OPT_SB_FIELD_SECTORS)
476 if (opt->flags & OPT_SB_FIELD_ILOG2)
482 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
484 if (opt->set_sb == SET_NO_SB_OPT)
487 mutex_lock(&c->sb_lock);
488 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
490 mutex_unlock(&c->sb_lock);
495 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
497 struct bch_io_opts ret = { 0 };
498 #define x(_name, _bits) \
499 if (opt_defined(src, _name)) \
500 opt_set(ret, _name, src._name);
506 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts src)
508 struct bch_opts ret = { 0 };
509 #define x(_name, _bits) \
510 if (opt_defined(src, _name)) \
511 opt_set(ret, _name, src._name);
517 void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src)
519 #define x(_name, _bits) \
520 if (opt_defined(src, _name)) \
521 opt_set(*dst, _name, src._name);
526 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
528 static const enum bch_opt_id inode_opt_list[] = {
529 #define x(_name, _bits) Opt_##_name,
535 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
536 if (inode_opt_list[i] == id)