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