1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
7 #include "disk_groups.h"
12 #define x(t, n) [n] = #t,
14 const char * const bch2_metadata_versions[] = {
15 BCH_METADATA_VERSIONS()
19 const char * const bch2_error_actions[] = {
24 const char * const bch2_sb_features[] = {
29 const char * const bch2_sb_compat[] = {
34 const char * const bch2_btree_ids[] = {
39 const char * const bch2_csum_types[] = {
44 const char * const bch2_csum_opts[] = {
49 const char * const bch2_compression_types[] = {
50 BCH_COMPRESSION_TYPES()
54 const char * const bch2_compression_opts[] = {
55 BCH_COMPRESSION_OPTS()
59 const char * const bch2_str_hash_types[] = {
64 const char * const bch2_str_hash_opts[] = {
69 const char * const bch2_data_types[] = {
74 const char * const bch2_member_states[] = {
79 const char * const bch2_jset_entry_types[] = {
80 BCH_JSET_ENTRY_TYPES()
84 const char * const bch2_fs_usage_types[] = {
91 const char * const bch2_d_types[BCH_DT_MAX] = {
92 [DT_UNKNOWN] = "unknown",
100 [DT_WHT] = "whiteout",
101 [DT_SUBVOL] = "subvol",
104 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
109 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
114 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
116 #define x(_name, ...) \
117 if (opt_defined(src, _name)) \
118 opt_set(*dst, _name, src._name);
124 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
127 #define x(_name, ...) \
129 return opt_defined(*opts, _name);
137 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
140 #define x(_name, ...) \
150 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
153 #define x(_name, ...) \
155 opt_set(*opts, _name, v); \
164 const struct bch_option bch2_opt_table[] = {
165 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
166 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
167 .min = _min, .max = _max
168 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
169 .min = 0, .max = ARRAY_SIZE(_choices),\
171 #define OPT_FN(_fn) .type = BCH_OPT_FN, \
172 .parse = _fn##_parse, \
173 .to_text = _fn##_to_text
175 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
179 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
185 .set_sb = SET_##_sb_opt, \
193 int bch2_opt_lookup(const char *name)
195 const struct bch_option *i;
197 for (i = bch2_opt_table;
198 i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
200 if (!strcmp(name, i->attr.name))
201 return i - bch2_opt_table;
210 static const struct synonym bch_opt_synonyms[] = {
211 { "quota", "usrquota" },
214 static int bch2_mount_opt_lookup(const char *name)
216 const struct synonym *i;
218 for (i = bch_opt_synonyms;
219 i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
221 if (!strcmp(name, i->s1))
224 return bch2_opt_lookup(name);
227 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
231 pr_buf(err, "%s: too small (min %llu)",
232 opt->attr.name, opt->min);
236 if (opt->max && v >= opt->max) {
238 pr_buf(err, "%s: too big (max %llu)",
239 opt->attr.name, opt->max);
243 if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
245 pr_buf(err, "%s: not a multiple of 512",
250 if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
252 pr_buf(err, "%s: must be a power of two",
260 int bch2_opt_parse(struct bch_fs *c,
261 const struct bch_option *opt,
262 const char *val, u64 *res,
263 struct printbuf *err)
269 ret = kstrtou64(val, 10, res);
274 ret = opt->flags & OPT_HUMAN_READABLE
275 ? bch2_strtou64_h(val, res)
276 : kstrtou64(val, 10, res);
281 ret = match_string(opt->choices, -1, val);
291 ret = opt->parse(c, val, res);
296 return bch2_opt_validate(opt, *res, err);
299 void bch2_opt_to_text(struct printbuf *out,
300 struct bch_fs *c, struct bch_sb *sb,
301 const struct bch_option *opt, u64 v,
304 if (flags & OPT_SHOW_MOUNT_STYLE) {
305 if (opt->type == BCH_OPT_BOOL) {
312 pr_buf(out, "%s=", opt->attr.name);
318 if (opt->flags & OPT_HUMAN_READABLE)
321 pr_buf(out, "%lli", v);
324 if (flags & OPT_SHOW_FULL_LIST)
325 bch2_string_opt_to_text(out, opt->choices, v);
327 pr_buf(out, "%s", opt->choices[v]);
330 opt->to_text(out, c, sb, v);
337 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
342 case Opt_compression:
343 case Opt_background_compression:
344 ret = bch2_check_set_has_compressed_data(c, v);
346 case Opt_erasure_code:
348 bch2_check_set_feature(c, BCH_FEATURE_ec);
355 int bch2_opts_check_may_set(struct bch_fs *c)
360 for (i = 0; i < bch2_opts_nr; i++) {
361 ret = bch2_opt_check_may_set(c, i,
362 bch2_opt_get_by_id(&c->opts, i));
370 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
373 char *copied_opts, *copied_opts_start;
374 char *opt, *name, *val;
376 struct printbuf err = PRINTBUF;
382 copied_opts = kstrdup(options, GFP_KERNEL);
385 copied_opts_start = copied_opts;
387 while ((opt = strsep(&copied_opts, ",")) != NULL) {
388 name = strsep(&opt, "=");
392 id = bch2_mount_opt_lookup(name);
396 ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
400 id = bch2_mount_opt_lookup(name);
404 !strncmp("no", name, 2)) {
405 id = bch2_mount_opt_lookup(name + 2);
412 if (bch2_opt_table[id].type != BCH_OPT_BOOL)
416 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
420 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
423 if ((id == Opt_usrquota ||
424 id == Opt_grpquota) &&
425 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
428 bch2_opt_set_by_id(opts, id, v);
435 pr_err("Bad mount option %s", name);
439 pr_err("Invalid mount option %s", err.buf);
443 pr_err("Mount option %s requires a value", name);
447 kfree(copied_opts_start);
452 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
454 const struct bch_option *opt = bch2_opt_table + id;
459 if (opt->flags & OPT_SB_FIELD_ILOG2)
462 if (opt->flags & OPT_SB_FIELD_SECTORS)
469 * Initial options from superblock - here we don't want any options undefined,
470 * any options the superblock doesn't specify are set to 0:
472 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
476 for (id = 0; id < bch2_opts_nr; id++) {
477 const struct bch_option *opt = bch2_opt_table + id;
479 if (opt->get_sb == BCH2_NO_SB_OPT)
482 bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id));
488 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
490 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
493 if (opt->flags & OPT_SB_FIELD_SECTORS)
496 if (opt->flags & OPT_SB_FIELD_ILOG2)
502 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
504 if (opt->set_sb == SET_BCH2_NO_SB_OPT)
507 mutex_lock(&c->sb_lock);
508 __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
510 mutex_unlock(&c->sb_lock);
515 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
517 struct bch_io_opts ret = { 0 };
518 #define x(_name, _bits) \
519 if (opt_defined(src, _name)) \
520 opt_set(ret, _name, src._name);
526 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts src)
528 struct bch_opts ret = { 0 };
529 #define x(_name, _bits) \
530 if (opt_defined(src, _name)) \
531 opt_set(ret, _name, src._name);
537 void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src)
539 #define x(_name, _bits) \
540 if (opt_defined(src, _name)) \
541 opt_set(*dst, _name, src._name);
546 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
548 static const enum bch_opt_id inode_opt_list[] = {
549 #define x(_name, _bits) Opt_##_name,
555 for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
556 if (inode_opt_list[i] == id)