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