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