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[] = {
50 const char * const bch2_csum_types[] = {
55 const char * const bch2_csum_opts[] = {
60 const char * const bch2_compression_types[] = {
61 BCH_COMPRESSION_TYPES()
65 const char * const bch2_compression_opts[] = {
66 BCH_COMPRESSION_OPTS()
70 const char * const bch2_str_hash_types[] = {
75 const char * const bch2_str_hash_opts[] = {
80 const char * const bch2_data_types[] = {
85 const char * const bch2_member_states[] = {
90 const char * const bch2_jset_entry_types[] = {
91 BCH_JSET_ENTRY_TYPES()
95 const char * const bch2_fs_usage_types[] = {
102 static int bch2_opt_fix_errors_parse(struct bch_fs *c, const char *val, u64 *res,
103 struct printbuf *err)
108 int ret = match_string(bch2_fsck_fix_opts, -1, val);
111 prt_str(err, "fix_errors: invalid selection");
120 static void bch2_opt_fix_errors_to_text(struct printbuf *out,
125 prt_str(out, bch2_fsck_fix_opts[v]);
128 #define bch2_opt_fix_errors (struct bch_opt_fn) { \
129 .parse = bch2_opt_fix_errors_parse, \
130 .to_text = bch2_opt_fix_errors_to_text, \
133 const char * const bch2_d_types[BCH_DT_MAX] = {
134 [DT_UNKNOWN] = "unknown",
142 [DT_WHT] = "whiteout",
143 [DT_SUBVOL] = "subvol",
146 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
151 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
156 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
158 #define x(_name, ...) \
159 if (opt_defined(src, _name)) \
160 opt_set(*dst, _name, src._name);
166 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
169 #define x(_name, ...) \
171 return opt_defined(*opts, _name);
179 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
182 #define x(_name, ...) \
192 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
195 #define x(_name, ...) \
197 opt_set(*opts, _name, v); \
206 const struct bch_option bch2_opt_table[] = {
207 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
208 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
209 .min = _min, .max = _max
210 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
211 .min = 0, .max = ARRAY_SIZE(_choices), \
213 #define OPT_FN(_fn) .type = BCH_OPT_FN, .fn = _fn
215 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
219 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
225 .set_sb = SET_##_sb_opt, \
233 int bch2_opt_lookup(const char *name)
235 const struct bch_option *i;
237 for (i = bch2_opt_table;
238 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
240 if (!strcmp(name, i->attr.name))
241 return i - bch2_opt_table;
250 static const struct synonym bch_opt_synonyms[] = {
251 { "quota", "usrquota" },
254 static int bch2_mount_opt_lookup(const char *name)
256 const struct synonym *i;
258 for (i = bch_opt_synonyms;
259 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
261 if (!strcmp(name, i->s1))
264 return bch2_opt_lookup(name);
267 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
271 prt_printf(err, "%s: too small (min %llu)",
272 opt->attr.name, opt->min);
276 if (opt->max && v >= opt->max) {
278 prt_printf(err, "%s: too big (max %llu)",
279 opt->attr.name, opt->max);
283 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
285 prt_printf(err, "%s: not a multiple of 512",
290 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
292 prt_printf(err, "%s: must be a power of two",
297 if (opt->fn.validate)
298 return opt->fn.validate(v, err);
303 int bch2_opt_parse(struct bch_fs *c,
304 const struct bch_option *opt,
305 const char *val, u64 *res,
306 struct printbuf *err)
313 ret = kstrtou64(val, 10, res);
319 if (ret < 0 || (*res != 0 && *res != 1)) {
321 prt_printf(err, "%s: must be bool", opt->attr.name);
327 prt_printf(err, "%s: required value",
332 ret = opt->flags & OPT_HUMAN_READABLE
333 ? bch2_strtou64_h(val, res)
334 : kstrtou64(val, 10, res);
337 prt_printf(err, "%s: must be a number",
344 prt_printf(err, "%s: required value",
349 ret = match_string(opt->choices, -1, val);
352 prt_printf(err, "%s: invalid selection",
360 ret = opt->fn.parse(c, val, res, err);
363 prt_printf(err, "%s: parse error",
369 return bch2_opt_validate(opt, *res, err);
372 void bch2_opt_to_text(struct printbuf *out,
373 struct bch_fs *c, struct bch_sb *sb,
374 const struct bch_option *opt, u64 v,
377 if (flags & OPT_SHOW_MOUNT_STYLE) {
378 if (opt->type == BCH_OPT_BOOL) {
379 prt_printf(out, "%s%s",
385 prt_printf(out, "%s=", opt->attr.name);
391 if (opt->flags & OPT_HUMAN_READABLE)
392 prt_human_readable_u64(out, v);
394 prt_printf(out, "%lli", v);
397 if (flags & OPT_SHOW_FULL_LIST)
398 prt_string_option(out, opt->choices, v);
400 prt_str(out, opt->choices[v]);
403 opt->fn.to_text(out, c, sb, v);
410 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
415 case Opt_compression:
416 case Opt_background_compression:
417 ret = bch2_check_set_has_compressed_data(c, v);
419 case Opt_erasure_code:
421 bch2_check_set_feature(c, BCH_FEATURE_ec);
428 int bch2_opts_check_may_set(struct bch_fs *c)
433 for (i = 0; i < bch2_opts_nr; i++) {
434 ret = bch2_opt_check_may_set(c, i,
435 bch2_opt_get_by_id(&c->opts, i));
443 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
446 char *copied_opts, *copied_opts_start;
447 char *opt, *name, *val;
449 struct printbuf err = PRINTBUF;
456 * sys_fsconfig() is now occasionally providing us with option lists
457 * starting with a comma - weird.
462 copied_opts = kstrdup(options, GFP_KERNEL);
465 copied_opts_start = copied_opts;
467 while ((opt = strsep(&copied_opts, ",")) != NULL) {
468 name = strsep(&opt, "=");
471 id = bch2_mount_opt_lookup(name);
473 /* Check for the form "noopt", negation of a boolean opt: */
476 !strncmp("no", name, 2)) {
477 id = bch2_mount_opt_lookup(name + 2);
481 /* Unknown options are ignored: */
485 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
489 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
492 if ((id == Opt_usrquota ||
493 id == Opt_grpquota) &&
494 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
497 ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
501 bch2_opt_set_by_id(opts, id, v);
508 pr_err("Bad mount option %s", name);
512 pr_err("Invalid mount option %s", err.buf);
516 kfree(copied_opts_start);
521 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
523 const struct bch_option *opt = bch2_opt_table + id;
528 if (opt->flags & OPT_SB_FIELD_ILOG2)
531 if (opt->flags & OPT_SB_FIELD_SECTORS)
538 * Initial options from superblock - here we don't want any options undefined,
539 * any options the superblock doesn't specify are set to 0:
541 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
545 for (id = 0; id < bch2_opts_nr; id++) {
546 const struct bch_option *opt = bch2_opt_table + id;
548 if (opt->get_sb == BCH2_NO_SB_OPT)
551 bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id));
557 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
559 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
562 if (opt->flags & OPT_SB_FIELD_SECTORS)
565 if (opt->flags & OPT_SB_FIELD_ILOG2)
571 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
573 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
576 mutex_lock(&c->sb_lock);
577 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
579 mutex_unlock(&c->sb_lock);
584 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
586 return (struct bch_io_opts) {
587 #define x(_name, _bits) ._name = src._name,
593 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
595 static const enum bch_opt_id inode_opt_list[] = {
596 #define x(_name, _bits) Opt_##_name,
602 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
603 if (inode_opt_list[i] == id)