xref: /linux/fs/bcachefs/opts.c (revision f858cc9eed5b05cbe38d7ffd2787c21e3718eb7d)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/kernel.h>
4 
5 #include "bcachefs.h"
6 #include "compress.h"
7 #include "disk_groups.h"
8 #include "error.h"
9 #include "opts.h"
10 #include "recovery_passes.h"
11 #include "super-io.h"
12 #include "util.h"
13 
14 #define x(t, n, ...) [n] = #t,
15 
16 const char * const bch2_error_actions[] = {
17 	BCH_ERROR_ACTIONS()
18 	NULL
19 };
20 
21 const char * const bch2_fsck_fix_opts[] = {
22 	BCH_FIX_ERRORS_OPTS()
23 	NULL
24 };
25 
26 const char * const bch2_version_upgrade_opts[] = {
27 	BCH_VERSION_UPGRADE_OPTS()
28 	NULL
29 };
30 
31 const char * const bch2_sb_features[] = {
32 	BCH_SB_FEATURES()
33 	NULL
34 };
35 
36 const char * const bch2_sb_compat[] = {
37 	BCH_SB_COMPAT()
38 	NULL
39 };
40 
41 const char * const __bch2_btree_ids[] = {
42 	BCH_BTREE_IDS()
43 	NULL
44 };
45 
46 static const char * const __bch2_csum_types[] = {
47 	BCH_CSUM_TYPES()
48 	NULL
49 };
50 
51 const char * const bch2_csum_opts[] = {
52 	BCH_CSUM_OPTS()
53 	NULL
54 };
55 
56 static const char * const __bch2_compression_types[] = {
57 	BCH_COMPRESSION_TYPES()
58 	NULL
59 };
60 
61 const char * const bch2_compression_opts[] = {
62 	BCH_COMPRESSION_OPTS()
63 	NULL
64 };
65 
66 const char * const bch2_str_hash_types[] = {
67 	BCH_STR_HASH_TYPES()
68 	NULL
69 };
70 
71 const char * const bch2_str_hash_opts[] = {
72 	BCH_STR_HASH_OPTS()
73 	NULL
74 };
75 
76 const char * const __bch2_data_types[] = {
77 	BCH_DATA_TYPES()
78 	NULL
79 };
80 
81 const char * const bch2_member_states[] = {
82 	BCH_MEMBER_STATES()
83 	NULL
84 };
85 
86 static const char * const __bch2_jset_entry_types[] = {
87 	BCH_JSET_ENTRY_TYPES()
88 	NULL
89 };
90 
91 static const char * const __bch2_fs_usage_types[] = {
92 	BCH_FS_USAGE_TYPES()
93 	NULL
94 };
95 
96 #undef x
97 
98 static void prt_str_opt_boundscheck(struct printbuf *out, const char * const opts[],
99 				    unsigned nr, const char *type, unsigned idx)
100 {
101 	if (idx < nr)
102 		prt_str(out, opts[idx]);
103 	else
104 		prt_printf(out, "(unknown %s %u)", type, idx);
105 }
106 
107 #define PRT_STR_OPT_BOUNDSCHECKED(name, type)					\
108 void bch2_prt_##name(struct printbuf *out, type t)				\
109 {										\
110 	prt_str_opt_boundscheck(out, __bch2_##name##s, ARRAY_SIZE(__bch2_##name##s) - 1, #name, t);\
111 }
112 
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 
119 static int bch2_opt_fix_errors_parse(struct bch_fs *c, const char *val, u64 *res,
120 				     struct printbuf *err)
121 {
122 	if (!val) {
123 		*res = FSCK_FIX_yes;
124 	} else {
125 		int ret = match_string(bch2_fsck_fix_opts, -1, val);
126 
127 		if (ret < 0 && err)
128 			prt_str(err, "fix_errors: invalid selection");
129 		if (ret < 0)
130 			return ret;
131 		*res = ret;
132 	}
133 
134 	return 0;
135 }
136 
137 static void bch2_opt_fix_errors_to_text(struct printbuf *out,
138 					struct bch_fs *c,
139 					struct bch_sb *sb,
140 					u64 v)
141 {
142 	prt_str(out, bch2_fsck_fix_opts[v]);
143 }
144 
145 #define bch2_opt_fix_errors (struct bch_opt_fn) {	\
146 	.parse = bch2_opt_fix_errors_parse,		\
147 	.to_text = bch2_opt_fix_errors_to_text,		\
148 }
149 
150 const char * const bch2_d_types[BCH_DT_MAX] = {
151 	[DT_UNKNOWN]	= "unknown",
152 	[DT_FIFO]	= "fifo",
153 	[DT_CHR]	= "chr",
154 	[DT_DIR]	= "dir",
155 	[DT_BLK]	= "blk",
156 	[DT_REG]	= "reg",
157 	[DT_LNK]	= "lnk",
158 	[DT_SOCK]	= "sock",
159 	[DT_WHT]	= "whiteout",
160 	[DT_SUBVOL]	= "subvol",
161 };
162 
163 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
164 {
165 	BUG();
166 }
167 
168 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
169 {
170 	BUG();
171 }
172 
173 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
174 {
175 #define x(_name, ...)						\
176 	if (opt_defined(src, _name))					\
177 		opt_set(*dst, _name, src._name);
178 
179 	BCH_OPTS()
180 #undef x
181 }
182 
183 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
184 {
185 	switch (id) {
186 #define x(_name, ...)						\
187 	case Opt_##_name:						\
188 		return opt_defined(*opts, _name);
189 	BCH_OPTS()
190 #undef x
191 	default:
192 		BUG();
193 	}
194 }
195 
196 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
197 {
198 	switch (id) {
199 #define x(_name, ...)						\
200 	case Opt_##_name:						\
201 		return opts->_name;
202 	BCH_OPTS()
203 #undef x
204 	default:
205 		BUG();
206 	}
207 }
208 
209 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
210 {
211 	switch (id) {
212 #define x(_name, ...)						\
213 	case Opt_##_name:						\
214 		opt_set(*opts, _name, v);				\
215 		break;
216 	BCH_OPTS()
217 #undef x
218 	default:
219 		BUG();
220 	}
221 }
222 
223 const struct bch_option bch2_opt_table[] = {
224 #define OPT_BOOL()		.type = BCH_OPT_BOOL, .min = 0, .max = 2
225 #define OPT_UINT(_min, _max)	.type = BCH_OPT_UINT,			\
226 				.min = _min, .max = _max
227 #define OPT_STR(_choices)	.type = BCH_OPT_STR,			\
228 				.min = 0, .max = ARRAY_SIZE(_choices),	\
229 				.choices = _choices
230 #define OPT_STR_NOLIMIT(_choices)	.type = BCH_OPT_STR,		\
231 				.min = 0, .max = U64_MAX,		\
232 				.choices = _choices
233 #define OPT_BITFIELD(_choices)	.type = BCH_OPT_BITFIELD,		\
234 				.choices = _choices
235 #define OPT_FN(_fn)		.type = BCH_OPT_FN, .fn	= _fn
236 
237 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help)	\
238 	[Opt_##_name] = {						\
239 		.attr	= {						\
240 			.name	= #_name,				\
241 			.mode = (_flags) & OPT_RUNTIME ? 0644 : 0444,	\
242 		},							\
243 		.flags	= _flags,					\
244 		.hint	= _hint,					\
245 		.help	= _help,					\
246 		.get_sb = _sb_opt,					\
247 		.set_sb	= SET_##_sb_opt,				\
248 		_type							\
249 	},
250 
251 	BCH_OPTS()
252 #undef x
253 };
254 
255 int bch2_opt_lookup(const char *name)
256 {
257 	const struct bch_option *i;
258 
259 	for (i = bch2_opt_table;
260 	     i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
261 	     i++)
262 		if (!strcmp(name, i->attr.name))
263 			return i - bch2_opt_table;
264 
265 	return -1;
266 }
267 
268 struct synonym {
269 	const char	*s1, *s2;
270 };
271 
272 static const struct synonym bch_opt_synonyms[] = {
273 	{ "quota",	"usrquota" },
274 };
275 
276 static int bch2_mount_opt_lookup(const char *name)
277 {
278 	const struct synonym *i;
279 
280 	for (i = bch_opt_synonyms;
281 	     i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
282 	     i++)
283 		if (!strcmp(name, i->s1))
284 			name = i->s2;
285 
286 	return bch2_opt_lookup(name);
287 }
288 
289 int bch2_opt_validate(const struct bch_option *opt, u64 v, struct printbuf *err)
290 {
291 	if (v < opt->min) {
292 		if (err)
293 			prt_printf(err, "%s: too small (min %llu)",
294 			       opt->attr.name, opt->min);
295 		return -BCH_ERR_ERANGE_option_too_small;
296 	}
297 
298 	if (opt->max && v >= opt->max) {
299 		if (err)
300 			prt_printf(err, "%s: too big (max %llu)",
301 			       opt->attr.name, opt->max);
302 		return -BCH_ERR_ERANGE_option_too_big;
303 	}
304 
305 	if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
306 		if (err)
307 			prt_printf(err, "%s: not a multiple of 512",
308 			       opt->attr.name);
309 		return -BCH_ERR_opt_parse_error;
310 	}
311 
312 	if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
313 		if (err)
314 			prt_printf(err, "%s: must be a power of two",
315 			       opt->attr.name);
316 		return -BCH_ERR_opt_parse_error;
317 	}
318 
319 	if (opt->fn.validate)
320 		return opt->fn.validate(v, err);
321 
322 	return 0;
323 }
324 
325 int bch2_opt_parse(struct bch_fs *c,
326 		   const struct bch_option *opt,
327 		   const char *val, u64 *res,
328 		   struct printbuf *err)
329 {
330 	ssize_t ret;
331 
332 	switch (opt->type) {
333 	case BCH_OPT_BOOL:
334 		if (val) {
335 			ret = kstrtou64(val, 10, res);
336 		} else {
337 			ret = 0;
338 			*res = 1;
339 		}
340 
341 		if (ret < 0 || (*res != 0 && *res != 1)) {
342 			if (err)
343 				prt_printf(err, "%s: must be bool", opt->attr.name);
344 			return ret < 0 ? ret : -BCH_ERR_option_not_bool;
345 		}
346 		break;
347 	case BCH_OPT_UINT:
348 		if (!val) {
349 			prt_printf(err, "%s: required value",
350 				   opt->attr.name);
351 			return -EINVAL;
352 		}
353 
354 		ret = opt->flags & OPT_HUMAN_READABLE
355 			? bch2_strtou64_h(val, res)
356 			: kstrtou64(val, 10, res);
357 		if (ret < 0) {
358 			if (err)
359 				prt_printf(err, "%s: must be a number",
360 					   opt->attr.name);
361 			return ret;
362 		}
363 		break;
364 	case BCH_OPT_STR:
365 		if (!val) {
366 			prt_printf(err, "%s: required value",
367 				   opt->attr.name);
368 			return -EINVAL;
369 		}
370 
371 		ret = match_string(opt->choices, -1, val);
372 		if (ret < 0) {
373 			if (err)
374 				prt_printf(err, "%s: invalid selection",
375 					   opt->attr.name);
376 			return ret;
377 		}
378 
379 		*res = ret;
380 		break;
381 	case BCH_OPT_BITFIELD: {
382 		s64 v = bch2_read_flag_list(val, opt->choices);
383 		if (v < 0)
384 			return v;
385 		*res = v;
386 		break;
387 	}
388 	case BCH_OPT_FN:
389 		ret = opt->fn.parse(c, val, res, err);
390 
391 		if (ret == -BCH_ERR_option_needs_open_fs)
392 			return ret;
393 
394 		if (ret < 0) {
395 			if (err)
396 				prt_printf(err, "%s: parse error",
397 					   opt->attr.name);
398 			return ret;
399 		}
400 	}
401 
402 	return bch2_opt_validate(opt, *res, err);
403 }
404 
405 void bch2_opt_to_text(struct printbuf *out,
406 		      struct bch_fs *c, struct bch_sb *sb,
407 		      const struct bch_option *opt, u64 v,
408 		      unsigned flags)
409 {
410 	if (flags & OPT_SHOW_MOUNT_STYLE) {
411 		if (opt->type == BCH_OPT_BOOL) {
412 			prt_printf(out, "%s%s",
413 			       v ? "" : "no",
414 			       opt->attr.name);
415 			return;
416 		}
417 
418 		prt_printf(out, "%s=", opt->attr.name);
419 	}
420 
421 	switch (opt->type) {
422 	case BCH_OPT_BOOL:
423 	case BCH_OPT_UINT:
424 		if (opt->flags & OPT_HUMAN_READABLE)
425 			prt_human_readable_u64(out, v);
426 		else
427 			prt_printf(out, "%lli", v);
428 		break;
429 	case BCH_OPT_STR:
430 		if (flags & OPT_SHOW_FULL_LIST)
431 			prt_string_option(out, opt->choices, v);
432 		else
433 			prt_str(out, opt->choices[v]);
434 		break;
435 	case BCH_OPT_BITFIELD:
436 		prt_bitflags(out, opt->choices, v);
437 		break;
438 	case BCH_OPT_FN:
439 		opt->fn.to_text(out, c, sb, v);
440 		break;
441 	default:
442 		BUG();
443 	}
444 }
445 
446 void bch2_opts_to_text(struct printbuf *out,
447 		       struct bch_opts opts,
448 		       struct bch_fs *c, struct bch_sb *sb,
449 		       unsigned show_mask, unsigned hide_mask,
450 		       unsigned flags)
451 {
452 	bool first = true;
453 
454 	for (enum bch_opt_id i = 0; i < bch2_opts_nr; i++) {
455 		const struct bch_option *opt = &bch2_opt_table[i];
456 
457 		if ((opt->flags & hide_mask) || !(opt->flags & show_mask))
458 			continue;
459 
460 		u64 v = bch2_opt_get_by_id(&opts, i);
461 		if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
462 			continue;
463 
464 		if (!first)
465 			prt_char(out, ',');
466 		first = false;
467 
468 		bch2_opt_to_text(out, c, sb, opt, v, flags);
469 	}
470 }
471 
472 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
473 {
474 	int ret = 0;
475 
476 	switch (id) {
477 	case Opt_compression:
478 	case Opt_background_compression:
479 		ret = bch2_check_set_has_compressed_data(c, v);
480 		break;
481 	case Opt_erasure_code:
482 		if (v)
483 			bch2_check_set_feature(c, BCH_FEATURE_ec);
484 		break;
485 	}
486 
487 	return ret;
488 }
489 
490 int bch2_opts_check_may_set(struct bch_fs *c)
491 {
492 	unsigned i;
493 	int ret;
494 
495 	for (i = 0; i < bch2_opts_nr; i++) {
496 		ret = bch2_opt_check_may_set(c, i,
497 				bch2_opt_get_by_id(&c->opts, i));
498 		if (ret)
499 			return ret;
500 	}
501 
502 	return 0;
503 }
504 
505 int bch2_parse_one_mount_opt(struct bch_fs *c, struct bch_opts *opts,
506 			     struct printbuf *parse_later,
507 			     const char *name, const char *val)
508 {
509 	struct printbuf err = PRINTBUF;
510 	u64 v;
511 	int ret, id;
512 
513 	id = bch2_mount_opt_lookup(name);
514 
515 	/* Check for the form "noopt", negation of a boolean opt: */
516 	if (id < 0 &&
517 	    !val &&
518 	    !strncmp("no", name, 2)) {
519 		id = bch2_mount_opt_lookup(name + 2);
520 		val = "0";
521 	}
522 
523 	/* Unknown options are ignored: */
524 	if (id < 0)
525 		return 0;
526 
527 	if (!(bch2_opt_table[id].flags & OPT_MOUNT))
528 		goto bad_opt;
529 
530 	if (id == Opt_acl &&
531 	    !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
532 		goto bad_opt;
533 
534 	if ((id == Opt_usrquota ||
535 	     id == Opt_grpquota) &&
536 	    !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
537 		goto bad_opt;
538 
539 	ret = bch2_opt_parse(c, &bch2_opt_table[id], val, &v, &err);
540 	if (ret == -BCH_ERR_option_needs_open_fs && parse_later) {
541 		prt_printf(parse_later, "%s=%s,", name, val);
542 		if (parse_later->allocation_failure) {
543 			ret = -ENOMEM;
544 			goto out;
545 		}
546 
547 		ret = 0;
548 		goto out;
549 	}
550 
551 	if (ret < 0)
552 		goto bad_val;
553 
554 	if (opts)
555 		bch2_opt_set_by_id(opts, id, v);
556 
557 	ret = 0;
558 	goto out;
559 
560 bad_opt:
561 	pr_err("Bad mount option %s", name);
562 	ret = -BCH_ERR_option_name;
563 	goto out;
564 
565 bad_val:
566 	pr_err("Invalid mount option %s", err.buf);
567 	ret = -BCH_ERR_option_value;
568 
569 out:
570 	printbuf_exit(&err);
571 	return ret;
572 }
573 
574 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
575 			  struct printbuf *parse_later, char *options)
576 {
577 	char *copied_opts, *copied_opts_start;
578 	char *opt, *name, *val;
579 	int ret;
580 
581 	if (!options)
582 		return 0;
583 
584 	/*
585 	 * sys_fsconfig() is now occasionally providing us with option lists
586 	 * starting with a comma - weird.
587 	 */
588 	if (*options == ',')
589 		options++;
590 
591 	copied_opts = kstrdup(options, GFP_KERNEL);
592 	if (!copied_opts)
593 		return -ENOMEM;
594 	copied_opts_start = copied_opts;
595 
596 	while ((opt = strsep(&copied_opts, ",")) != NULL) {
597 		name	= strsep(&opt, "=");
598 		val	= opt;
599 
600 		ret = bch2_parse_one_mount_opt(c, opts, parse_later, name, val);
601 		if (ret < 0)
602 			goto out;
603 	}
604 
605 	ret = 0;
606 	goto out;
607 
608 out:
609 	kfree(copied_opts_start);
610 	return ret;
611 }
612 
613 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
614 {
615 	const struct bch_option *opt = bch2_opt_table + id;
616 	u64 v;
617 
618 	v = opt->get_sb(sb);
619 
620 	if (opt->flags & OPT_SB_FIELD_ILOG2)
621 		v = 1ULL << v;
622 
623 	if (opt->flags & OPT_SB_FIELD_SECTORS)
624 		v <<= 9;
625 
626 	return v;
627 }
628 
629 /*
630  * Initial options from superblock - here we don't want any options undefined,
631  * any options the superblock doesn't specify are set to 0:
632  */
633 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
634 {
635 	unsigned id;
636 
637 	for (id = 0; id < bch2_opts_nr; id++) {
638 		const struct bch_option *opt = bch2_opt_table + id;
639 
640 		if (opt->get_sb == BCH2_NO_SB_OPT)
641 			continue;
642 
643 		bch2_opt_set_by_id(opts, id, bch2_opt_from_sb(sb, id));
644 	}
645 
646 	return 0;
647 }
648 
649 struct bch_dev_sb_opt_set {
650 	void			(*set_sb)(struct bch_member *, u64);
651 };
652 
653 static const struct bch_dev_sb_opt_set bch2_dev_sb_opt_setters [] = {
654 #define x(n, set)	[Opt_##n] = { .set_sb = SET_##set },
655 	BCH_DEV_OPT_SETTERS()
656 #undef x
657 };
658 
659 void __bch2_opt_set_sb(struct bch_sb *sb, int dev_idx,
660 		       const struct bch_option *opt, u64 v)
661 {
662 	enum bch_opt_id id = opt - bch2_opt_table;
663 
664 	if (opt->flags & OPT_SB_FIELD_SECTORS)
665 		v >>= 9;
666 
667 	if (opt->flags & OPT_SB_FIELD_ILOG2)
668 		v = ilog2(v);
669 
670 	if (opt->flags & OPT_SB_FIELD_ONE_BIAS)
671 		v++;
672 
673 	if (opt->flags & OPT_FS) {
674 		if (opt->set_sb != SET_BCH2_NO_SB_OPT)
675 			opt->set_sb(sb, v);
676 	}
677 
678 	if ((opt->flags & OPT_DEVICE) && dev_idx >= 0) {
679 		if (WARN(!bch2_member_exists(sb, dev_idx),
680 			 "tried to set device option %s on nonexistent device %i",
681 			 opt->attr.name, dev_idx))
682 			return;
683 
684 		struct bch_member *m = bch2_members_v2_get_mut(sb, dev_idx);
685 
686 		const struct bch_dev_sb_opt_set *set = bch2_dev_sb_opt_setters + id;
687 		if (set->set_sb)
688 			set->set_sb(m, v);
689 		else
690 			pr_err("option %s cannot be set via opt_set_sb()", opt->attr.name);
691 	}
692 }
693 
694 void bch2_opt_set_sb(struct bch_fs *c, struct bch_dev *ca,
695 		     const struct bch_option *opt, u64 v)
696 {
697 	mutex_lock(&c->sb_lock);
698 	__bch2_opt_set_sb(c->disk_sb.sb, ca ? ca->dev_idx : -1, opt, v);
699 	bch2_write_super(c);
700 	mutex_unlock(&c->sb_lock);
701 }
702 
703 /* io opts: */
704 
705 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
706 {
707 	return (struct bch_io_opts) {
708 #define x(_name, _bits)	._name = src._name,
709 	BCH_INODE_OPTS()
710 #undef x
711 	};
712 }
713 
714 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
715 {
716 	static const enum bch_opt_id inode_opt_list[] = {
717 #define x(_name, _bits)	Opt_##_name,
718 	BCH_INODE_OPTS()
719 #undef x
720 	};
721 	unsigned i;
722 
723 	for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
724 		if (inode_opt_list[i] == id)
725 			return true;
726 
727 	return false;
728 }
729