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