1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
7 #include "disk_groups.h"
13 #define x(t, n, ...) [n] = #t,
15 const char * const bch2_iops_measurements[] = {
16 BCH_IOPS_MEASUREMENTS()
20 const char * const bch2_error_actions[] = {
25 const char * const bch2_fsck_fix_opts[] = {
30 const char * const bch2_version_upgrade_opts[] = {
31 BCH_VERSION_UPGRADE_OPTS()
35 const char * const bch2_sb_features[] = {
40 const char * const bch2_sb_compat[] = {
45 const char * const bch2_btree_ids[] = {
47 "interior btree node",
51 const char * const bch2_csum_types[] = {
56 const char * const bch2_csum_opts[] = {
61 const char * const bch2_compression_types[] = {
62 BCH_COMPRESSION_TYPES()
66 const char * const bch2_compression_opts[] = {
67 BCH_COMPRESSION_OPTS()
71 const char * const bch2_str_hash_types[] = {
76 const char * const bch2_str_hash_opts[] = {
81 const char * const bch2_data_types[] = {
86 const char * const bch2_member_states[] = {
91 const char * const bch2_jset_entry_types[] = {
92 BCH_JSET_ENTRY_TYPES()
96 const char * const bch2_fs_usage_types[] = {
103 static int bch2_opt_fix_errors_parse(struct bch_fs *c, const char *val, u64 *res,
104 struct printbuf *err)
109 int ret = match_string(bch2_fsck_fix_opts, -1, val);
112 prt_str(err, "fix_errors: invalid selection");
121 static void bch2_opt_fix_errors_to_text(struct printbuf *out,
126 prt_str(out, bch2_fsck_fix_opts[v]);
129 #define bch2_opt_fix_errors (struct bch_opt_fn) { \
130 .parse = bch2_opt_fix_errors_parse, \
131 .to_text = bch2_opt_fix_errors_to_text, \
134 const char * const bch2_d_types[BCH_DT_MAX] = {
135 [DT_UNKNOWN] = "unknown",
143 [DT_WHT] = "whiteout",
144 [DT_SUBVOL] = "subvol",
147 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
152 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
157 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
159 #define x(_name, ...) \
160 if (opt_defined(src, _name)) \
161 opt_set(*dst, _name, src._name);
167 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
170 #define x(_name, ...) \
172 return opt_defined(*opts, _name);
180 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
183 #define x(_name, ...) \
193 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
196 #define x(_name, ...) \
198 opt_set(*opts, _name, v); \
207 const struct bch_option bch2_opt_table[] = {
208 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
209 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
210 .min = _min, .max = _max
211 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
212 .min = 0, .max = ARRAY_SIZE(_choices), \
214 #define OPT_FN(_fn) .type = BCH_OPT_FN, .fn = _fn
216 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
220 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
226 .set_sb = SET_##_sb_opt, \
234 int bch2_opt_lookup(const char *name)
236 const struct bch_option *i;
238 for (i = bch2_opt_table;
239 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
241 if (!strcmp(name, i->attr.name))
242 return i - bch2_opt_table;
251 static const struct synonym bch_opt_synonyms[] = {
252 { "quota", "usrquota" },
255 static int bch2_mount_opt_lookup(const char *name)
257 const struct synonym *i;
259 for (i = bch_opt_synonyms;
260 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
262 if (!strcmp(name, i->s1))
265 return bch2_opt_lookup(name);
268 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
272 prt_printf(err, "%s: too small (min %llu)",
273 opt->attr.name, opt->min);
277 if (opt->max && v >= opt->max) {
279 prt_printf(err, "%s: too big (max %llu)",
280 opt->attr.name, opt->max);
284 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
286 prt_printf(err, "%s: not a multiple of 512",
291 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
293 prt_printf(err, "%s: must be a power of two",
301 int bch2_opt_parse(struct bch_fs *c,
302 const struct bch_option *opt,
303 const char *val, u64 *res,
304 struct printbuf *err)
311 ret = kstrtou64(val, 10, res);
317 if (ret < 0 || (*res != 0 && *res != 1)) {
319 prt_printf(err, "%s: must be bool", opt->attr.name);
325 prt_printf(err, "%s: required value",
330 ret = opt->flags & OPT_HUMAN_READABLE
331 ? bch2_strtou64_h(val, res)
332 : kstrtou64(val, 10, res);
335 prt_printf(err, "%s: must be a number",
342 prt_printf(err, "%s: required value",
347 ret = match_string(opt->choices, -1, val);
350 prt_printf(err, "%s: invalid selection",
358 ret = opt->fn.parse(c, val, res, err);
361 prt_printf(err, "%s: parse error",
367 return bch2_opt_validate(opt, *res, err);
370 void bch2_opt_to_text(struct printbuf *out,
371 struct bch_fs *c, struct bch_sb *sb,
372 const struct bch_option *opt, u64 v,
375 if (flags & OPT_SHOW_MOUNT_STYLE) {
376 if (opt->type == BCH_OPT_BOOL) {
377 prt_printf(out, "%s%s",
383 prt_printf(out, "%s=", opt->attr.name);
389 if (opt->flags & OPT_HUMAN_READABLE)
390 prt_human_readable_u64(out, v);
392 prt_printf(out, "%lli", v);
395 if (flags & OPT_SHOW_FULL_LIST)
396 prt_string_option(out, opt->choices, v);
398 prt_str(out, opt->choices[v]);
401 opt->fn.to_text(out, c, sb, v);
408 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
413 case Opt_compression:
414 case Opt_background_compression:
415 ret = bch2_check_set_has_compressed_data(c, v);
417 case Opt_erasure_code:
419 bch2_check_set_feature(c, BCH_FEATURE_ec);
426 int bch2_opts_check_may_set(struct bch_fs *c)
431 for (i = 0; i < bch2_opts_nr; i++) {
432 ret = bch2_opt_check_may_set(c, i,
433 bch2_opt_get_by_id(&c->opts, i));
441 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
444 char *copied_opts, *copied_opts_start;
445 char *opt, *name, *val;
447 struct printbuf err = PRINTBUF;
454 * sys_fsconfig() is now occasionally providing us with option lists
455 * starting with a comma - weird.
460 copied_opts = kstrdup(options, GFP_KERNEL);
463 copied_opts_start = copied_opts;
465 while ((opt = strsep(&copied_opts, ",")) != NULL) {
466 name = strsep(&opt, "=");
469 id = bch2_mount_opt_lookup(name);
471 /* Check for the form "noopt", negation of a boolean opt: */
474 !strncmp("no", name, 2)) {
475 id = bch2_mount_opt_lookup(name + 2);
479 /* Unknown options are ignored: */
483 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
487 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
490 if ((id == Opt_usrquota ||
491 id == Opt_grpquota) &&
492 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
495 ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
499 bch2_opt_set_by_id(opts, id, v);
506 pr_err("Bad mount option %s", name);
510 pr_err("Invalid mount option %s", err.buf);
514 kfree(copied_opts_start);
519 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
521 const struct bch_option *opt = bch2_opt_table + id;
526 if (opt->flags & OPT_SB_FIELD_ILOG2)
529 if (opt->flags & OPT_SB_FIELD_SECTORS)
536 * Initial options from superblock - here we don't want any options undefined,
537 * any options the superblock doesn't specify are set to 0:
539 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
543 for (id = 0; id < bch2_opts_nr; id++) {
544 const struct bch_option *opt = bch2_opt_table + id;
546 if (opt->get_sb == BCH2_NO_SB_OPT)
549 bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id));
555 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
557 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
560 if (opt->flags & OPT_SB_FIELD_SECTORS)
563 if (opt->flags & OPT_SB_FIELD_ILOG2)
569 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
571 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
574 mutex_lock(&c->sb_lock);
575 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
577 mutex_unlock(&c->sb_lock);
582 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
584 return (struct bch_io_opts) {
585 #define x(_name, _bits) ._name = src._name,
591 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
593 static const enum bch_opt_id inode_opt_list[] = {
594 #define x(_name, _bits) Opt_##_name,
600 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
601 if (inode_opt_list[i] == id)