]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/opts.c
Update bcachefs sources to 259ff91605 bcachefs: Don't keep around btree_paths unneces...
[bcachefs-tools-debian] / libbcachefs / opts.c
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 "opts.h"
9 #include "super-io.h"
10 #include "util.h"
11
12 #define x(t, n) #t,
13
14 const char * const bch2_error_actions[] = {
15         BCH_ERROR_ACTIONS()
16         NULL
17 };
18
19 const char * const bch2_sb_features[] = {
20         BCH_SB_FEATURES()
21         NULL
22 };
23
24 const char * const bch2_sb_compat[] = {
25         BCH_SB_COMPAT()
26         NULL
27 };
28
29 const char * const bch2_btree_ids[] = {
30         BCH_BTREE_IDS()
31         NULL
32 };
33
34 const char * const bch2_csum_types[] = {
35         BCH_CSUM_TYPES()
36         NULL
37 };
38
39 const char * const bch2_csum_opts[] = {
40         BCH_CSUM_OPTS()
41         NULL
42 };
43
44 const char * const bch2_compression_types[] = {
45         BCH_COMPRESSION_TYPES()
46         NULL
47 };
48
49 const char * const bch2_compression_opts[] = {
50         BCH_COMPRESSION_OPTS()
51         NULL
52 };
53
54 const char * const bch2_str_hash_types[] = {
55         BCH_STR_HASH_TYPES()
56         NULL
57 };
58
59 const char * const bch2_str_hash_opts[] = {
60         BCH_STR_HASH_OPTS()
61         NULL
62 };
63
64 const char * const bch2_data_types[] = {
65         BCH_DATA_TYPES()
66         NULL
67 };
68
69 const char * const bch2_member_states[] = {
70         BCH_MEMBER_STATES()
71         NULL
72 };
73
74 const char * const bch2_jset_entry_types[] = {
75         BCH_JSET_ENTRY_TYPES()
76         NULL
77 };
78
79 const char * const bch2_fs_usage_types[] = {
80         BCH_FS_USAGE_TYPES()
81         NULL
82 };
83
84 #undef x
85
86 const char * const bch2_d_types[BCH_DT_MAX] = {
87         [DT_UNKNOWN]    = "unknown",
88         [DT_FIFO]       = "fifo",
89         [DT_CHR]        = "chr",
90         [DT_DIR]        = "dir",
91         [DT_BLK]        = "blk",
92         [DT_REG]        = "reg",
93         [DT_LNK]        = "lnk",
94         [DT_SOCK]       = "sock",
95         [DT_WHT]        = "whiteout",
96         [DT_SUBVOL]     = "subvol",
97 };
98
99 u64 BCH2_NO_SB_OPT(const struct bch_sb *sb)
100 {
101         BUG();
102 }
103
104 void SET_BCH2_NO_SB_OPT(struct bch_sb *sb, u64 v)
105 {
106         BUG();
107 }
108
109 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
110 {
111 #define x(_name, ...)                                           \
112         if (opt_defined(src, _name))                                    \
113                 opt_set(*dst, _name, src._name);
114
115         BCH_OPTS()
116 #undef x
117 }
118
119 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
120 {
121         switch (id) {
122 #define x(_name, ...)                                           \
123         case Opt_##_name:                                               \
124                 return opt_defined(*opts, _name);
125         BCH_OPTS()
126 #undef x
127         default:
128                 BUG();
129         }
130 }
131
132 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
133 {
134         switch (id) {
135 #define x(_name, ...)                                           \
136         case Opt_##_name:                                               \
137                 return opts->_name;
138         BCH_OPTS()
139 #undef x
140         default:
141                 BUG();
142         }
143 }
144
145 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
146 {
147         switch (id) {
148 #define x(_name, ...)                                           \
149         case Opt_##_name:                                               \
150                 opt_set(*opts, _name, v);                               \
151                 break;
152         BCH_OPTS()
153 #undef x
154         default:
155                 BUG();
156         }
157 }
158
159 const struct bch_option bch2_opt_table[] = {
160 #define OPT_BOOL()              .type = BCH_OPT_BOOL, .min = 0, .max = 2
161 #define OPT_UINT(_min, _max)    .type = BCH_OPT_UINT,                   \
162                                 .min = _min, .max = _max
163 #define OPT_STR(_choices)       .type = BCH_OPT_STR,                    \
164                                 .min = 0, .max = ARRAY_SIZE(_choices),\
165                                 .choices = _choices
166 #define OPT_FN(_fn)             .type = BCH_OPT_FN,                     \
167                                 .parse = _fn##_parse,                   \
168                                 .to_text = _fn##_to_text
169
170 #define x(_name, _bits, _flags, _type, _sb_opt, _default, _hint, _help) \
171         [Opt_##_name] = {                                               \
172                 .attr   = {                                             \
173                         .name   = #_name,                               \
174                         .mode = (_flags) & OPT_RUNTIME ? 0644 : 0444,   \
175                 },                                                      \
176                 .flags  = _flags,                                       \
177                 .hint   = _hint,                                        \
178                 .help   = _help,                                        \
179                 .get_sb = _sb_opt,                                      \
180                 .set_sb = SET_##_sb_opt,                                \
181                 _type                                                   \
182         },
183
184         BCH_OPTS()
185 #undef x
186 };
187
188 int bch2_opt_lookup(const char *name)
189 {
190         const struct bch_option *i;
191
192         for (i = bch2_opt_table;
193              i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
194              i++)
195                 if (!strcmp(name, i->attr.name))
196                         return i - bch2_opt_table;
197
198         return -1;
199 }
200
201 struct synonym {
202         const char      *s1, *s2;
203 };
204
205 static const struct synonym bch_opt_synonyms[] = {
206         { "quota",      "usrquota" },
207 };
208
209 static int bch2_mount_opt_lookup(const char *name)
210 {
211         const struct synonym *i;
212
213         for (i = bch_opt_synonyms;
214              i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
215              i++)
216                 if (!strcmp(name, i->s1))
217                         name = i->s2;
218
219         return bch2_opt_lookup(name);
220 }
221
222 static int bch2_opt_validate(const struct bch_option *opt, const char *msg, u64 v)
223 {
224         if (v < opt->min) {
225                 if (msg)
226                         pr_err("invalid %s%s: too small (min %llu)",
227                                msg, opt->attr.name, opt->min);
228                 return -ERANGE;
229         }
230
231         if (opt->max && v >= opt->max) {
232                 if (msg)
233                         pr_err("invalid %s%s: too big (max %llu)",
234                                msg, opt->attr.name, opt->max);
235                 return -ERANGE;
236         }
237
238         if ((opt->flags & OPT_SB_FIELD_SECTORS) && (v & 511)) {
239                 if (msg)
240                         pr_err("invalid %s %s: not a multiple of 512",
241                                msg, opt->attr.name);
242                 return -EINVAL;
243         }
244
245         if ((opt->flags & OPT_MUST_BE_POW_2) && !is_power_of_2(v)) {
246                 if (msg)
247                         pr_err("invalid %s%s: must be a power of two",
248                                msg, opt->attr.name);
249                 return -EINVAL;
250         }
251
252         return 0;
253 }
254
255 int bch2_opt_parse(struct bch_fs *c, const char *msg,
256                    const struct bch_option *opt,
257                    const char *val, u64 *res)
258 {
259         ssize_t ret;
260
261         switch (opt->type) {
262         case BCH_OPT_BOOL:
263                 ret = kstrtou64(val, 10, res);
264                 if (ret < 0)
265                         return ret;
266                 break;
267         case BCH_OPT_UINT:
268                 ret = opt->flags & OPT_HUMAN_READABLE
269                         ? bch2_strtou64_h(val, res)
270                         : kstrtou64(val, 10, res);
271                 if (ret < 0)
272                         return ret;
273                 break;
274         case BCH_OPT_STR:
275                 ret = match_string(opt->choices, -1, val);
276                 if (ret < 0)
277                         return ret;
278
279                 *res = ret;
280                 break;
281         case BCH_OPT_FN:
282                 if (!c)
283                         return 0;
284
285                 ret = opt->parse(c, val, res);
286                 if (ret < 0)
287                         return ret;
288         }
289
290         return bch2_opt_validate(opt, msg, *res);
291 }
292
293 void bch2_opt_to_text(struct printbuf *out,
294                       struct bch_fs *c, struct bch_sb *sb,
295                       const struct bch_option *opt, u64 v,
296                       unsigned flags)
297 {
298         if (flags & OPT_SHOW_MOUNT_STYLE) {
299                 if (opt->type == BCH_OPT_BOOL) {
300                         pr_buf(out, "%s%s",
301                                v ? "" : "no",
302                                opt->attr.name);
303                         return;
304                 }
305
306                 pr_buf(out, "%s=", opt->attr.name);
307         }
308
309         switch (opt->type) {
310         case BCH_OPT_BOOL:
311         case BCH_OPT_UINT:
312                 if (opt->flags & OPT_HUMAN_READABLE)
313                         bch2_hprint(out, v);
314                 else
315                         pr_buf(out, "%lli", v);
316                 break;
317         case BCH_OPT_STR:
318                 if (flags & OPT_SHOW_FULL_LIST)
319                         bch2_string_opt_to_text(out, opt->choices, v);
320                 else
321                         pr_buf(out, opt->choices[v]);
322                 break;
323         case BCH_OPT_FN:
324                 opt->to_text(out, c, sb, v);
325                 break;
326         default:
327                 BUG();
328         }
329 }
330
331 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
332 {
333         int ret = 0;
334
335         switch (id) {
336         case Opt_compression:
337         case Opt_background_compression:
338                 ret = bch2_check_set_has_compressed_data(c, v);
339                 break;
340         case Opt_erasure_code:
341                 if (v)
342                         bch2_check_set_feature(c, BCH_FEATURE_ec);
343                 break;
344         }
345
346         return ret;
347 }
348
349 int bch2_opts_check_may_set(struct bch_fs *c)
350 {
351         unsigned i;
352         int ret;
353
354         for (i = 0; i < bch2_opts_nr; i++) {
355                 ret = bch2_opt_check_may_set(c, i,
356                                 bch2_opt_get_by_id(&c->opts, i));
357                 if (ret)
358                         return ret;
359         }
360
361         return 0;
362 }
363
364 int bch2_parse_mount_opts(struct bch_fs *c, struct bch_opts *opts,
365                           char *options)
366 {
367         char *copied_opts, *copied_opts_start;
368         char *opt, *name, *val;
369         int ret, id;
370         u64 v;
371
372         if (!options)
373                 return 0;
374
375         copied_opts = kstrdup(options, GFP_KERNEL);
376         if (!copied_opts)
377                 return -1;
378         copied_opts_start = copied_opts;
379
380         while ((opt = strsep(&copied_opts, ",")) != NULL) {
381                 name    = strsep(&opt, "=");
382                 val     = opt;
383
384                 if (val) {
385                         id = bch2_mount_opt_lookup(name);
386                         if (id < 0)
387                                 goto bad_opt;
388
389                         ret = bch2_opt_parse(c, "mount option ",
390                                              &bch2_opt_table[id], val, &v);
391                         if (ret < 0)
392                                 goto bad_val;
393                 } else {
394                         id = bch2_mount_opt_lookup(name);
395                         v = 1;
396
397                         if (id < 0 &&
398                             !strncmp("no", name, 2)) {
399                                 id = bch2_mount_opt_lookup(name + 2);
400                                 v = 0;
401                         }
402
403                         if (id < 0)
404                                 goto bad_opt;
405
406                         if (bch2_opt_table[id].type != BCH_OPT_BOOL)
407                                 goto no_val;
408                 }
409
410                 if (!(bch2_opt_table[id].flags & OPT_MOUNT))
411                         goto bad_opt;
412
413                 if (id == Opt_acl &&
414                     !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
415                         goto bad_opt;
416
417                 if ((id == Opt_usrquota ||
418                      id == Opt_grpquota) &&
419                     !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
420                         goto bad_opt;
421
422                 bch2_opt_set_by_id(opts, id, v);
423         }
424
425         ret = 0;
426         goto out;
427
428 bad_opt:
429         pr_err("Bad mount option %s", name);
430         ret = -1;
431         goto out;
432 bad_val:
433         pr_err("Invalid value %s for mount option %s", val, name);
434         ret = -1;
435         goto out;
436 no_val:
437         pr_err("Mount option %s requires a value", name);
438         ret = -1;
439         goto out;
440 out:
441         kfree(copied_opts_start);
442         return ret;
443 }
444
445 u64 bch2_opt_from_sb(struct bch_sb *sb, enum bch_opt_id id)
446 {
447         const struct bch_option *opt = bch2_opt_table + id;
448         u64 v;
449
450         v = opt->get_sb(sb);
451
452         if (opt->flags & OPT_SB_FIELD_ILOG2)
453                 v = 1ULL << v;
454
455         if (opt->flags & OPT_SB_FIELD_SECTORS)
456                 v <<= 9;
457
458         return v;
459 }
460
461 /*
462  * Initial options from superblock - here we don't want any options undefined,
463  * any options the superblock doesn't specify are set to 0:
464  */
465 int bch2_opts_from_sb(struct bch_opts *opts, struct bch_sb *sb)
466 {
467         unsigned id;
468         int ret;
469
470         for (id = 0; id < bch2_opts_nr; id++) {
471                 const struct bch_option *opt = bch2_opt_table + id;
472                 u64 v;
473
474                 if (opt->get_sb == BCH2_NO_SB_OPT)
475                         continue;
476
477                 v = bch2_opt_from_sb(sb, id);
478
479                 ret = bch2_opt_validate(opt, "superblock option ", v);
480                 if (ret)
481                         return ret;
482
483                 bch2_opt_set_by_id(opts, id, v);
484         }
485
486         return 0;
487 }
488
489 void __bch2_opt_set_sb(struct bch_sb *sb, const struct bch_option *opt, u64 v)
490 {
491         if (opt->set_sb == SET_BCH2_NO_SB_OPT)
492                 return;
493
494         if (opt->flags & OPT_SB_FIELD_SECTORS)
495                 v >>= 9;
496
497         if (opt->flags & OPT_SB_FIELD_ILOG2)
498                 v = ilog2(v);
499
500         opt->set_sb(sb, v);
501 }
502
503 void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
504 {
505         if (opt->set_sb == SET_BCH2_NO_SB_OPT)
506                 return;
507
508         mutex_lock(&c->sb_lock);
509         __bch2_opt_set_sb(c->disk_sb.sb, opt, v);
510         bch2_write_super(c);
511         mutex_unlock(&c->sb_lock);
512 }
513
514 /* io opts: */
515
516 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
517 {
518         struct bch_io_opts ret = { 0 };
519 #define x(_name, _bits)                                 \
520         if (opt_defined(src, _name))                                    \
521                 opt_set(ret, _name, src._name);
522         BCH_INODE_OPTS()
523 #undef x
524         return ret;
525 }
526
527 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts src)
528 {
529         struct bch_opts ret = { 0 };
530 #define x(_name, _bits)                                 \
531         if (opt_defined(src, _name))                                    \
532                 opt_set(ret, _name, src._name);
533         BCH_INODE_OPTS()
534 #undef x
535         return ret;
536 }
537
538 void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src)
539 {
540 #define x(_name, _bits)                                 \
541         if (opt_defined(src, _name))                                    \
542                 opt_set(*dst, _name, src._name);
543         BCH_INODE_OPTS()
544 #undef x
545 }
546
547 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
548 {
549         static const enum bch_opt_id inode_opt_list[] = {
550 #define x(_name, _bits) Opt_##_name,
551         BCH_INODE_OPTS()
552 #undef x
553         };
554         unsigned i;
555
556         for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
557                 if (inode_opt_list[i] == id)
558                         return true;
559
560         return false;
561 }