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 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
104 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
109 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
111 #define x(_name, ...) \
112 if (opt_defined(src, _name)) \
113 opt_set(*dst, _name, src._name);
119 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
122 #define x(_name, ...) \
124 return opt_defined(*opts, _name);
132 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
135 #define x(_name, ...) \
145 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
148 #define x(_name, ...) \
150 opt_set(*opts, _name, v); \
159 const struct bch_option bch2_opt_table[] = {
160 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
161 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
162 .min = _min, .max = _max
163 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
164 .min = 0, .max = ARRAY_SIZE(_choices),\
166 #define OPT_FN(_fn) .type = BCH_OPT_FN, \
167 .parse = _fn##_parse, \
168 .to_text = _fn##_to_text
170 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
174 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
180 .set_sb = SET_##_sb_opt, \
188 int bch2_opt_lookup(const char *name)
190 const struct bch_option *i;
192 for (i = bch2_opt_table;
193 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
195 if (!strcmp(name, i->attr.name))
196 return i - bch2_opt_table;
205 static const struct synonym bch_opt_synonyms[] = {
206 { "quota", "usrquota" },
209 static int bch2_mount_opt_lookup(const char *name)
211 const struct synonym *i;
213 for (i = bch_opt_synonyms;
214 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
216 if (!strcmp(name, i->s1))
219 return bch2_opt_lookup(name);
222 static int bch2_opt_validate(const struct bch_option *opt, const char *msg, u64 v)
226 pr_err("invalid %s%s: too small (min %llu)",
227 msg, opt->attr.name, opt->min);
231 if (opt->max && v >= opt->max) {
233 pr_err("invalid %s%s: too big (max %llu)",
234 msg, opt->attr.name, opt->max);
238 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
240 pr_err("invalid %s %s: not a multiple of 512",
241 msg, opt->attr.name);
245 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
247 pr_err("invalid %s%s: must be a power of two",
248 msg, opt->attr.name);
255 int bch2_opt_parse(struct bch_fs *c, const char *msg,
256 const struct bch_option *opt,
257 const char *val, u64 *res)
263 ret = kstrtou64(val, 10, res);
268 ret = opt->flags & OPT_HUMAN_READABLE
269 ? bch2_strtou64_h(val, res)
270 : kstrtou64(val, 10, res);
275 ret = match_string(opt->choices, -1, val);
285 ret = opt->parse(c, val, res);
290 return bch2_opt_validate(opt, msg, *res);
293 void bch2_opt_to_text(struct printbuf *out,
294 struct bch_fs *c, struct bch_sb *sb,
295 const struct bch_option *opt, u64 v,
298 if (flags & OPT_SHOW_MOUNT_STYLE) {
299 if (opt->type == BCH_OPT_BOOL) {
306 pr_buf(out, "%s=", opt->attr.name);
312 if (opt->flags & OPT_HUMAN_READABLE)
315 pr_buf(out, "%lli", v);
318 if (flags & OPT_SHOW_FULL_LIST)
319 bch2_string_opt_to_text(out, opt->choices, v);
321 pr_buf(out, opt->choices[v]);
324 opt->to_text(out, c, sb, v);
331 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
336 case Opt_compression:
337 case Opt_background_compression:
338 ret = bch2_check_set_has_compressed_data(c, v);
340 case Opt_erasure_code:
342 bch2_check_set_feature(c, BCH_FEATURE_ec);
349 int bch2_opts_check_may_set(struct bch_fs *c)
354 for (i = 0; i < bch2_opts_nr; i++) {
355 ret = bch2_opt_check_may_set(c, i,
356 bch2_opt_get_by_id(&c->opts, i));
364 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
367 char *copied_opts, *copied_opts_start;
368 char *opt, *name, *val;
375 copied_opts = kstrdup(options, GFP_KERNEL);
378 copied_opts_start = copied_opts;
380 while ((opt = strsep(&copied_opts, ",")) != NULL) {
381 name = strsep(&opt, "=");
385 id = bch2_mount_opt_lookup(name);
389 ret = bch2_opt_parse(c, "mount option ",
390 &bch2_opt_table[id], val, &v);
394 id = bch2_mount_opt_lookup(name);
398 !strncmp("no", name, 2)) {
399 id = bch2_mount_opt_lookup(name + 2);
406 if (bch2_opt_table[id].type != BCH_OPT_BOOL)
410 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
414 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
417 if ((id == Opt_usrquota ||
418 id == Opt_grpquota) &&
419 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
422 bch2_opt_set_by_id(opts, id, v);
429 pr_err("Bad mount option %s", name);
433 pr_err("Invalid value %s for mount option %s", val, name);
437 pr_err("Mount option %s requires a value", name);
441 kfree(copied_opts_start);
445 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
447 const struct bch_option *opt = bch2_opt_table + id;
452 if (opt->flags & OPT_SB_FIELD_ILOG2)
455 if (opt->flags & OPT_SB_FIELD_SECTORS)
462 * Initial options from superblock - here we don't want any options undefined,
463 * any options the superblock doesn't specify are set to 0:
465 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
470 for (id = 0; id < bch2_opts_nr; id++) {
471 const struct bch_option *opt = bch2_opt_table + id;
474 if (opt->get_sb == BCH2_NO_SB_OPT)
477 v = bch2_opt_from_sb(sb, id);
479 ret = bch2_opt_validate(opt, "superblock option ", v);
483 bch2_opt_set_by_id(opts, id, v);
489 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
491 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
494 if (opt->flags & OPT_SB_FIELD_SECTORS)
497 if (opt->flags & OPT_SB_FIELD_ILOG2)
503 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
505 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
508 mutex_lock(&c->sb_lock);
509 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
511 mutex_unlock(&c->sb_lock);
516 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
518 struct bch_io_opts ret = { 0 };
519 #define x(_name, _bits) \
520 if (opt_defined(src, _name)) \
521 opt_set(ret, _name, src._name);
527 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts src)
529 struct bch_opts ret = { 0 };
530 #define x(_name, _bits) \
531 if (opt_defined(src, _name)) \
532 opt_set(ret, _name, src._name);
538 void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src)
540 #define x(_name, _bits) \
541 if (opt_defined(src, _name)) \
542 opt_set(*dst, _name, src._name);
547 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
549 static const enum bch_opt_id inode_opt_list[] = {
550 #define x(_name, _bits) Opt_##_name,
556 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
557 if (inode_opt_list[i] == id)