]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/opts.c
Update bcachefs sources to 96b991466a bcachefs: Improve error message in fsck
[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 const char * const bch2_error_actions[] = {
13         "continue",
14         "remount-ro",
15         "panic",
16         NULL
17 };
18
19 const char * const bch2_sb_features[] = {
20 #define x(f, n) #f,
21         BCH_SB_FEATURES()
22 #undef x
23         NULL
24 };
25
26 const char * const bch2_csum_opts[] = {
27         "none",
28         "crc32c",
29         "crc64",
30         NULL
31 };
32
33 const char * const bch2_compression_opts[] = {
34 #define x(t, n) #t,
35         BCH_COMPRESSION_OPTS()
36 #undef x
37         NULL
38 };
39
40 const char * const bch2_str_hash_types[] = {
41         "crc32c",
42         "crc64",
43         "siphash",
44         NULL
45 };
46
47 const char * const bch2_data_types[] = {
48         "none",
49         "sb",
50         "journal",
51         "btree",
52         "data",
53         "cached",
54         NULL
55 };
56
57 const char * const bch2_cache_replacement_policies[] = {
58         "lru",
59         "fifo",
60         "random",
61         NULL
62 };
63
64 /* Default is -1; we skip past it for struct cached_dev's cache mode */
65 const char * const bch2_cache_modes[] = {
66         "default",
67         "writethrough",
68         "writeback",
69         "writearound",
70         "none",
71         NULL
72 };
73
74 const char * const bch2_dev_state[] = {
75         "readwrite",
76         "readonly",
77         "failed",
78         "spare",
79         NULL
80 };
81
82 void bch2_opts_apply(struct bch_opts *dst, struct bch_opts src)
83 {
84 #define x(_name, ...)                                           \
85         if (opt_defined(src, _name))                                    \
86                 opt_set(*dst, _name, src._name);
87
88         BCH_OPTS()
89 #undef x
90 }
91
92 bool bch2_opt_defined_by_id(const struct bch_opts *opts, enum bch_opt_id id)
93 {
94         switch (id) {
95 #define x(_name, ...)                                           \
96         case Opt_##_name:                                               \
97                 return opt_defined(*opts, _name);
98         BCH_OPTS()
99 #undef x
100         default:
101                 BUG();
102         }
103 }
104
105 u64 bch2_opt_get_by_id(const struct bch_opts *opts, enum bch_opt_id id)
106 {
107         switch (id) {
108 #define x(_name, ...)                                           \
109         case Opt_##_name:                                               \
110                 return opts->_name;
111         BCH_OPTS()
112 #undef x
113         default:
114                 BUG();
115         }
116 }
117
118 void bch2_opt_set_by_id(struct bch_opts *opts, enum bch_opt_id id, u64 v)
119 {
120         switch (id) {
121 #define x(_name, ...)                                           \
122         case Opt_##_name:                                               \
123                 opt_set(*opts, _name, v);                               \
124                 break;
125         BCH_OPTS()
126 #undef x
127         default:
128                 BUG();
129         }
130 }
131
132 /*
133  * Initial options from superblock - here we don't want any options undefined,
134  * any options the superblock doesn't specify are set to 0:
135  */
136 struct bch_opts bch2_opts_from_sb(struct bch_sb *sb)
137 {
138         struct bch_opts opts = bch2_opts_empty();
139
140 #define x(_name, _bits, _mode, _type, _sb_opt, ...)                     \
141         if (_sb_opt != NO_SB_OPT)                                       \
142                 opt_set(opts, _name, _sb_opt(sb));
143         BCH_OPTS()
144 #undef x
145
146         return opts;
147 }
148
149 const struct bch_option bch2_opt_table[] = {
150 #define OPT_BOOL()              .type = BCH_OPT_BOOL
151 #define OPT_UINT(_min, _max)    .type = BCH_OPT_UINT, .min = _min, .max = _max
152 #define OPT_SECTORS(_min, _max) .type = BCH_OPT_SECTORS, .min = _min, .max = _max
153 #define OPT_STR(_choices)       .type = BCH_OPT_STR, .choices = _choices
154 #define OPT_FN(_fn)             .type = BCH_OPT_FN,                     \
155                                 .parse = _fn##_parse,                   \
156                                 .to_text = _fn##_to_text
157
158 #define x(_name, _bits, _mode, _type, _sb_opt, _default, _hint, _help)  \
159         [Opt_##_name] = {                                               \
160                 .attr   = {                                             \
161                         .name   = #_name,                               \
162                         .mode = (_mode) & OPT_RUNTIME ? 0644 : 0444,    \
163                 },                                                      \
164                 .mode   = _mode,                                        \
165                 .hint   = _hint,                                        \
166                 .help   = _help,                                        \
167                 .set_sb = SET_##_sb_opt,                                \
168                 _type                                                   \
169         },
170
171         BCH_OPTS()
172 #undef x
173 };
174
175 int bch2_opt_lookup(const char *name)
176 {
177         const struct bch_option *i;
178
179         for (i = bch2_opt_table;
180              i < bch2_opt_table + ARRAY_SIZE(bch2_opt_table);
181              i++)
182                 if (!strcmp(name, i->attr.name))
183                         return i - bch2_opt_table;
184
185         return -1;
186 }
187
188 struct synonym {
189         const char      *s1, *s2;
190 };
191
192 static const struct synonym bch_opt_synonyms[] = {
193         { "quota",      "usrquota" },
194 };
195
196 static int bch2_mount_opt_lookup(const char *name)
197 {
198         const struct synonym *i;
199
200         for (i = bch_opt_synonyms;
201              i < bch_opt_synonyms + ARRAY_SIZE(bch_opt_synonyms);
202              i++)
203                 if (!strcmp(name, i->s1))
204                         name = i->s2;
205
206         return bch2_opt_lookup(name);
207 }
208
209 int bch2_opt_parse(struct bch_fs *c, const struct bch_option *opt,
210                    const char *val, u64 *res)
211 {
212         ssize_t ret;
213
214         switch (opt->type) {
215         case BCH_OPT_BOOL:
216                 ret = kstrtou64(val, 10, res);
217                 if (ret < 0)
218                         return ret;
219
220                 if (*res > 1)
221                         return -ERANGE;
222                 break;
223         case BCH_OPT_UINT:
224                 ret = kstrtou64(val, 10, res);
225                 if (ret < 0)
226                         return ret;
227
228                 if (*res < opt->min || *res >= opt->max)
229                         return -ERANGE;
230                 break;
231         case BCH_OPT_SECTORS:
232                 ret = bch2_strtou64_h(val, res);
233                 if (ret < 0)
234                         return ret;
235
236                 if (*res & 511)
237                         return -EINVAL;
238
239                 *res >>= 9;
240
241                 if (*res < opt->min || *res >= opt->max)
242                         return -ERANGE;
243                 break;
244         case BCH_OPT_STR:
245                 ret = match_string(opt->choices, -1, val);
246                 if (ret < 0)
247                         return ret;
248
249                 *res = ret;
250                 break;
251         case BCH_OPT_FN:
252                 if (!c)
253                         return -EINVAL;
254
255                 return opt->parse(c, val, res);
256         }
257
258         return 0;
259 }
260
261 void bch2_opt_to_text(struct printbuf *out, struct bch_fs *c,
262                       const struct bch_option *opt, u64 v,
263                       unsigned flags)
264 {
265         if (flags & OPT_SHOW_MOUNT_STYLE) {
266                 if (opt->type == BCH_OPT_BOOL) {
267                         pr_buf(out, "%s%s",
268                                v ? "" : "no",
269                                opt->attr.name);
270                         return;
271                 }
272
273                 pr_buf(out, "%s=", opt->attr.name);
274         }
275
276         switch (opt->type) {
277         case BCH_OPT_BOOL:
278         case BCH_OPT_UINT:
279                 pr_buf(out, "%lli", v);
280                 break;
281         case BCH_OPT_SECTORS:
282                 bch2_hprint(out, v);
283                 break;
284         case BCH_OPT_STR:
285                 if (flags & OPT_SHOW_FULL_LIST)
286                         bch2_string_opt_to_text(out, opt->choices, v);
287                 else
288                         pr_buf(out, opt->choices[v]);
289                 break;
290         case BCH_OPT_FN:
291                 opt->to_text(out, c, v);
292                 break;
293         default:
294                 BUG();
295         }
296 }
297
298 int bch2_opt_check_may_set(struct bch_fs *c, int id, u64 v)
299 {
300         int ret = 0;
301
302         switch (id) {
303         case Opt_compression:
304         case Opt_background_compression:
305                 ret = bch2_check_set_has_compressed_data(c, v);
306                 break;
307         case Opt_erasure_code:
308                 if (v)
309                         bch2_check_set_feature(c, BCH_FEATURE_ec);
310                 break;
311         }
312
313         return ret;
314 }
315
316 int bch2_opts_check_may_set(struct bch_fs *c)
317 {
318         unsigned i;
319         int ret;
320
321         for (i = 0; i < bch2_opts_nr; i++) {
322                 ret = bch2_opt_check_may_set(c, i,
323                                 bch2_opt_get_by_id(&c->opts, i));
324                 if (ret)
325                         return ret;
326         }
327
328         return 0;
329 }
330
331 int bch2_parse_mount_opts(struct bch_opts *opts, char *options)
332 {
333         char *opt, *name, *val;
334         int ret, id;
335         u64 v;
336
337         while ((opt = strsep(&options, ",")) != NULL) {
338                 name    = strsep(&opt, "=");
339                 val     = opt;
340
341                 if (val) {
342                         id = bch2_mount_opt_lookup(name);
343                         if (id < 0)
344                                 goto bad_opt;
345
346                         ret = bch2_opt_parse(NULL, &bch2_opt_table[id], val, &v);
347                         if (ret < 0)
348                                 goto bad_val;
349                 } else {
350                         id = bch2_mount_opt_lookup(name);
351                         v = 1;
352
353                         if (id < 0 &&
354                             !strncmp("no", name, 2)) {
355                                 id = bch2_mount_opt_lookup(name + 2);
356                                 v = 0;
357                         }
358
359                         if (id < 0)
360                                 goto bad_opt;
361
362                         if (bch2_opt_table[id].type != BCH_OPT_BOOL)
363                                 goto no_val;
364                 }
365
366                 if (!(bch2_opt_table[id].mode & OPT_MOUNT))
367                         goto bad_opt;
368
369                 if (id == Opt_acl &&
370                     !IS_ENABLED(CONFIG_BCACHEFS_POSIX_ACL))
371                         goto bad_opt;
372
373                 if ((id == Opt_usrquota ||
374                      id == Opt_grpquota) &&
375                     !IS_ENABLED(CONFIG_BCACHEFS_QUOTA))
376                         goto bad_opt;
377
378                 bch2_opt_set_by_id(opts, id, v);
379         }
380
381         return 0;
382 bad_opt:
383         pr_err("Bad mount option %s", name);
384         return -1;
385 bad_val:
386         pr_err("Invalid value %s for mount option %s", val, name);
387         return -1;
388 no_val:
389         pr_err("Mount option %s requires a value", name);
390         return -1;
391 }
392
393 /* io opts: */
394
395 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
396 {
397         struct bch_io_opts ret = { 0 };
398 #define x(_name, _bits)                                 \
399         if (opt_defined(src, _name))                                    \
400                 opt_set(ret, _name, src._name);
401         BCH_INODE_OPTS()
402 #undef x
403         return ret;
404 }
405
406 struct bch_opts bch2_inode_opts_to_opts(struct bch_io_opts src)
407 {
408         struct bch_opts ret = { 0 };
409 #define x(_name, _bits)                                 \
410         if (opt_defined(src, _name))                                    \
411                 opt_set(ret, _name, src._name);
412         BCH_INODE_OPTS()
413 #undef x
414         return ret;
415 }
416
417 void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src)
418 {
419 #define x(_name, _bits)                                 \
420         if (opt_defined(src, _name))                                    \
421                 opt_set(*dst, _name, src._name);
422         BCH_INODE_OPTS()
423 #undef x
424 }
425
426 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
427 {
428         static const enum bch_opt_id inode_opt_list[] = {
429 #define x(_name, _bits) Opt_##_name,
430         BCH_INODE_OPTS()
431 #undef x
432         };
433         unsigned i;
434
435         for (i = 0; i < ARRAY_SIZE(inode_opt_list); i++)
436                 if (inode_opt_list[i] == id)
437                         return true;
438
439         return false;
440 }