1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/kernel.h>
7 #include "disk_groups.h"
10 #include "recovery_passes.h"
14 #define x(t, n, ...) [n] = #t,
16 const char * const bch2_error_actions
[] = {
21 const char * const bch2_fsck_fix_opts
[] = {
26 const char * const bch2_version_upgrade_opts
[] = {
27 BCH_VERSION_UPGRADE_OPTS()
31 const char * const bch2_sb_features
[] = {
36 const char * const bch2_sb_compat
[] = {
41 const char * const __bch2_btree_ids
[] = {
46 static const char * const __bch2_csum_types
[] = {
51 const char * const bch2_csum_opts
[] = {
56 static const char * const __bch2_compression_types
[] = {
57 BCH_COMPRESSION_TYPES()
61 const char * const bch2_compression_opts
[] = {
62 BCH_COMPRESSION_OPTS()
66 const char * const __bch2_str_hash_types
[] = {
71 const char * const bch2_str_hash_opts
[] = {
76 const char * const __bch2_data_types
[] = {
81 const char * const bch2_member_states
[] = {
86 static const char * const __bch2_jset_entry_types
[] = {
87 BCH_JSET_ENTRY_TYPES()
91 static const char * const __bch2_fs_usage_types
[] = {
98 static void prt_str_opt_boundscheck(struct printbuf
*out
, const char * const opts
[],
99 unsigned nr
, const char *type
, unsigned idx
)
102 prt_str(out
, opts
[idx
]);
104 prt_printf(out
, "(unknown %s %u)", type
, idx
);
107 #define PRT_STR_OPT_BOUNDSCHECKED(name, type) \
108 void bch2_prt_##name(struct printbuf *out, type t) \
110 prt_str_opt_boundscheck(out, __bch2_##name##s, ARRAY_SIZE(__bch2_##name##s) - 1, #name, t);\
113 PRT_STR_OPT_BOUNDSCHECKED(jset_entry_type
, enum bch_jset_entry_type
);
114 PRT_STR_OPT_BOUNDSCHECKED(fs_usage_type
, enum bch_fs_usage_type
);
115 PRT_STR_OPT_BOUNDSCHECKED(data_type
, enum bch_data_type
);
116 PRT_STR_OPT_BOUNDSCHECKED(csum_type
, enum bch_csum_type
);
117 PRT_STR_OPT_BOUNDSCHECKED(compression_type
, enum bch_compression_type
);
118 PRT_STR_OPT_BOUNDSCHECKED(str_hash_type
, enum bch_str_hash_type
);
120 static int bch2_opt_fix_errors_parse(struct bch_fs
*c
, const char *val
, u64
*res
,
121 struct printbuf
*err
)
126 int ret
= match_string(bch2_fsck_fix_opts
, -1, val
);
129 prt_str(err
, "fix_errors: invalid selection");
138 static void bch2_opt_fix_errors_to_text(struct printbuf
*out
,
143 prt_str(out
, bch2_fsck_fix_opts
[v
]);
146 #define bch2_opt_fix_errors (struct bch_opt_fn) { \
147 .parse = bch2_opt_fix_errors_parse, \
148 .to_text = bch2_opt_fix_errors_to_text, \
151 const char * const bch2_d_types
[BCH_DT_MAX
] = {
152 [DT_UNKNOWN
] = "unknown",
160 [DT_WHT
] = "whiteout",
161 [DT_SUBVOL
] = "subvol",
164 u64
BCH2_NO_SB_OPT(const struct bch_sb
*sb
)
169 void SET_BCH2_NO_SB_OPT(struct bch_sb
*sb
, u64 v
)
174 void bch2_opts_apply(struct bch_opts
*dst
, struct bch_opts src
)
176 #define x(_name, ...) \
177 if (opt_defined(src, _name)) \
178 opt_set(*dst, _name, src._name);
184 bool bch2_opt_defined_by_id(const struct bch_opts
*opts
, enum bch_opt_id id
)
187 #define x(_name, ...) \
189 return opt_defined(*opts, _name);
197 u64
bch2_opt_get_by_id(const struct bch_opts
*opts
, enum bch_opt_id id
)
200 #define x(_name, ...) \
210 void bch2_opt_set_by_id(struct bch_opts
*opts
, enum bch_opt_id id
, u64 v
)
213 #define x(_name, ...) \
215 opt_set(*opts, _name, v); \
224 const struct bch_option bch2_opt_table
[] = {
225 #define OPT_BOOL() .type = BCH_OPT_BOOL, .min = 0, .max = 2
226 #define OPT_UINT(_min, _max) .type = BCH_OPT_UINT, \
227 .min = _min, .max = _max
228 #define OPT_STR(_choices) .type = BCH_OPT_STR, \
229 .min = 0, .max = ARRAY_SIZE(_choices) - 1, \
231 #define OPT_STR_NOLIMIT(_choices) .type = BCH_OPT_STR, \
232 .min = 0, .max = U64_MAX, \
234 #define OPT_BITFIELD(_choices) .type = BCH_OPT_BITFIELD, \
236 #define OPT_FN(_fn) .type = BCH_OPT_FN, .fn = _fn
238 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
242 .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444, \
248 .set_sb = SET_##_sb_opt, \
256 int bch2_opt_lookup(const char *name
)
258 const struct bch_option
*i
;
260 for (i
= bch2_opt_table
;
261 i
< bch2_opt_table
+ ARRAY_SIZE(bch2_opt_table
);
263 if (!strcmp(name
, i
->attr
.name
))
264 return i
- bch2_opt_table
;
273 static const struct synonym bch_opt_synonyms
[] = {
274 { "quota", "usrquota" },
277 static int bch2_mount_opt_lookup(const char *name
)
279 const struct synonym
*i
;
281 for (i
= bch_opt_synonyms
;
282 i
< bch_opt_synonyms
+ ARRAY_SIZE(bch_opt_synonyms
);
284 if (!strcmp(name
, i
->s1
))
287 return bch2_opt_lookup(name
);
290 int bch2_opt_validate(const struct bch_option
*opt
, u64 v
, struct printbuf
*err
)
294 prt_printf(err
, "%s: too small (min %llu)",
295 opt
->attr
.name
, opt
->min
);
296 return -BCH_ERR_ERANGE_option_too_small
;
299 if (opt
->max
&& v
>= opt
->max
) {
301 prt_printf(err
, "%s: too big (max %llu)",
302 opt
->attr
.name
, opt
->max
);
303 return -BCH_ERR_ERANGE_option_too_big
;
306 if ((opt
->flags
& OPT_SB_FIELD_SECTORS
) && (v
& 511)) {
308 prt_printf(err
, "%s: not a multiple of 512",
310 return -BCH_ERR_opt_parse_error
;
313 if ((opt
->flags
& OPT_MUST_BE_POW_2
) && !is_power_of_2(v
)) {
315 prt_printf(err
, "%s: must be a power of two",
317 return -BCH_ERR_opt_parse_error
;
320 if (opt
->fn
.validate
)
321 return opt
->fn
.validate(v
, err
);
326 int bch2_opt_parse(struct bch_fs
*c
,
327 const struct bch_option
*opt
,
328 const char *val
, u64
*res
,
329 struct printbuf
*err
)
336 ret
= kstrtou64(val
, 10, res
);
342 if (ret
< 0 || (*res
!= 0 && *res
!= 1)) {
344 prt_printf(err
, "%s: must be bool", opt
->attr
.name
);
345 return ret
< 0 ? ret
: -BCH_ERR_option_not_bool
;
350 prt_printf(err
, "%s: required value",
355 ret
= opt
->flags
& OPT_HUMAN_READABLE
356 ? bch2_strtou64_h(val
, res
)
357 : kstrtou64(val
, 10, res
);
360 prt_printf(err
, "%s: must be a number",
367 prt_printf(err
, "%s: required value",
372 ret
= match_string(opt
->choices
, -1, val
);
375 prt_printf(err
, "%s: invalid selection",
382 case BCH_OPT_BITFIELD
: {
383 s64 v
= bch2_read_flag_list(val
, opt
->choices
);
390 ret
= opt
->fn
.parse(c
, val
, res
, err
);
392 if (ret
== -BCH_ERR_option_needs_open_fs
)
397 prt_printf(err
, "%s: parse error",
403 return bch2_opt_validate(opt
, *res
, err
);
406 void bch2_opt_to_text(struct printbuf
*out
,
407 struct bch_fs
*c
, struct bch_sb
*sb
,
408 const struct bch_option
*opt
, u64 v
,
411 if (flags
& OPT_SHOW_MOUNT_STYLE
) {
412 if (opt
->type
== BCH_OPT_BOOL
) {
413 prt_printf(out
, "%s%s",
419 prt_printf(out
, "%s=", opt
->attr
.name
);
425 if (opt
->flags
& OPT_HUMAN_READABLE
)
426 prt_human_readable_u64(out
, v
);
428 prt_printf(out
, "%lli", v
);
431 if (v
< opt
->min
|| v
>= opt
->max
)
432 prt_printf(out
, "(invalid option %lli)", v
);
433 else if (flags
& OPT_SHOW_FULL_LIST
)
434 prt_string_option(out
, opt
->choices
, v
);
436 prt_str(out
, opt
->choices
[v
]);
438 case BCH_OPT_BITFIELD
:
439 prt_bitflags(out
, opt
->choices
, v
);
442 opt
->fn
.to_text(out
, c
, sb
, v
);
449 void bch2_opts_to_text(struct printbuf
*out
,
450 struct bch_opts opts
,
451 struct bch_fs
*c
, struct bch_sb
*sb
,
452 unsigned show_mask
, unsigned hide_mask
,
457 for (enum bch_opt_id i
= 0; i
< bch2_opts_nr
; i
++) {
458 const struct bch_option
*opt
= &bch2_opt_table
[i
];
460 if ((opt
->flags
& hide_mask
) || !(opt
->flags
& show_mask
))
463 u64 v
= bch2_opt_get_by_id(&opts
, i
);
464 if (v
== bch2_opt_get_by_id(&bch2_opts_default
, i
))
471 bch2_opt_to_text(out
, c
, sb
, opt
, v
, flags
);
475 int bch2_opt_check_may_set(struct bch_fs
*c
, int id
, u64 v
)
480 case Opt_compression
:
481 case Opt_background_compression
:
482 ret
= bch2_check_set_has_compressed_data(c
, v
);
484 case Opt_erasure_code
:
486 bch2_check_set_feature(c
, BCH_FEATURE_ec
);
493 int bch2_opts_check_may_set(struct bch_fs
*c
)
498 for (i
= 0; i
< bch2_opts_nr
; i
++) {
499 ret
= bch2_opt_check_may_set(c
, i
,
500 bch2_opt_get_by_id(&c
->opts
, i
));
508 int bch2_parse_one_mount_opt(struct bch_fs
*c
, struct bch_opts
*opts
,
509 struct printbuf
*parse_later
,
510 const char *name
, const char *val
)
512 struct printbuf err
= PRINTBUF
;
516 id
= bch2_mount_opt_lookup(name
);
518 /* Check for the form "noopt", negation of a boolean opt: */
521 !strncmp("no", name
, 2)) {
522 id
= bch2_mount_opt_lookup(name
+ 2);
526 /* Unknown options are ignored: */
530 if (!(bch2_opt_table
[id
].flags
& OPT_MOUNT
))
534 !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL
))
537 if ((id
== Opt_usrquota
||
538 id
== Opt_grpquota
) &&
539 !IS_ENABLED(CONFIG_BCACHEFS_QUOTA
))
542 ret
= bch2_opt_parse(c
, &bch2_opt_table
[id
], val
, &v
, &err
);
543 if (ret
== -BCH_ERR_option_needs_open_fs
&& parse_later
) {
544 prt_printf(parse_later
, "%s=%s,", name
, val
);
545 if (parse_later
->allocation_failure
) {
558 bch2_opt_set_by_id(opts
, id
, v
);
564 pr_err("Bad mount option %s", name
);
565 ret
= -BCH_ERR_option_name
;
569 pr_err("Invalid mount option %s", err
.buf
);
570 ret
= -BCH_ERR_option_value
;
577 int bch2_parse_mount_opts(struct bch_fs
*c
, struct bch_opts
*opts
,
578 struct printbuf
*parse_later
, char *options
)
580 char *copied_opts
, *copied_opts_start
;
581 char *opt
, *name
, *val
;
588 * sys_fsconfig() is now occasionally providing us with option lists
589 * starting with a comma - weird.
594 copied_opts
= kstrdup(options
, GFP_KERNEL
);
597 copied_opts_start
= copied_opts
;
599 while ((opt
= strsep(&copied_opts
, ",")) != NULL
) {
603 name
= strsep(&opt
, "=");
606 ret
= bch2_parse_one_mount_opt(c
, opts
, parse_later
, name
, val
);
615 kfree(copied_opts_start
);
619 u64
bch2_opt_from_sb(struct bch_sb
*sb
, enum bch_opt_id id
)
621 const struct bch_option
*opt
= bch2_opt_table
+ id
;
626 if (opt
->flags
& OPT_SB_FIELD_ILOG2
)
629 if (opt
->flags
& OPT_SB_FIELD_SECTORS
)
636 * Initial options from superblock - here we don't want any options undefined,
637 * any options the superblock doesn't specify are set to 0:
639 int bch2_opts_from_sb(struct bch_opts
*opts
, struct bch_sb
*sb
)
643 for (id
= 0; id
< bch2_opts_nr
; id
++) {
644 const struct bch_option
*opt
= bch2_opt_table
+ id
;
646 if (opt
->get_sb
== BCH2_NO_SB_OPT
)
649 bch2_opt_set_by_id(opts
, id
, bch2_opt_from_sb(sb
, id
));
655 struct bch_dev_sb_opt_set
{
656 void (*set_sb
)(struct bch_member
*, u64
);
659 static const struct bch_dev_sb_opt_set bch2_dev_sb_opt_setters
[] = {
660 #define x(n, set) [Opt_##n] = { .set_sb = SET_##set },
661 BCH_DEV_OPT_SETTERS()
665 void __bch2_opt_set_sb(struct bch_sb
*sb
, int dev_idx
,
666 const struct bch_option
*opt
, u64 v
)
668 enum bch_opt_id id
= opt
- bch2_opt_table
;
670 if (opt
->flags
& OPT_SB_FIELD_SECTORS
)
673 if (opt
->flags
& OPT_SB_FIELD_ILOG2
)
676 if (opt
->flags
& OPT_SB_FIELD_ONE_BIAS
)
679 if (opt
->flags
& OPT_FS
) {
680 if (opt
->set_sb
!= SET_BCH2_NO_SB_OPT
)
684 if ((opt
->flags
& OPT_DEVICE
) && dev_idx
>= 0) {
685 if (WARN(!bch2_member_exists(sb
, dev_idx
),
686 "tried to set device option %s on nonexistent device %i",
687 opt
->attr
.name
, dev_idx
))
690 struct bch_member
*m
= bch2_members_v2_get_mut(sb
, dev_idx
);
692 const struct bch_dev_sb_opt_set
*set
= bch2_dev_sb_opt_setters
+ id
;
696 pr_err("option %s cannot be set via opt_set_sb()", opt
->attr
.name
);
700 void bch2_opt_set_sb(struct bch_fs
*c
, struct bch_dev
*ca
,
701 const struct bch_option
*opt
, u64 v
)
703 mutex_lock(&c
->sb_lock
);
704 __bch2_opt_set_sb(c
->disk_sb
.sb
, ca
? ca
->dev_idx
: -1, opt
, v
);
706 mutex_unlock(&c
->sb_lock
);
711 struct bch_io_opts
bch2_opts_to_inode_opts(struct bch_opts src
)
713 return (struct bch_io_opts
) {
714 #define x(_name, _bits) ._name = src._name,
720 bool bch2_opt_is_inode_opt(enum bch_opt_id id
)
722 static const enum bch_opt_id inode_opt_list
[] = {
723 #define x(_name, _bits) Opt_##_name,
729 for (i
= 0; i
< ARRAY_SIZE(inode_opt_list
); i
++)
730 if (inode_opt_list
[i
] == id
)