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