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