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