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