1 /* Filesystem parameter parser.
3 * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #include <linux/export.h>
13 #include <linux/fs_context.h>
14 #include <linux/fs_parser.h>
15 #include <linux/slab.h>
16 #include <linux/security.h>
17 #include <linux/namei.h>
20 static const struct constant_table bool_names[] = {
30 * lookup_constant - Look up a constant by name in an ordered table
31 * @tbl: The table of constants to search.
32 * @tbl_size: The size of the table.
33 * @name: The name to look up.
34 * @not_found: The value to return if the name is not found.
36 int __lookup_constant(const struct constant_table *tbl, size_t tbl_size,
37 const char *name, int not_found)
41 for (i = 0; i < tbl_size; i++)
42 if (strcmp(name, tbl[i].name) == 0)
47 EXPORT_SYMBOL(__lookup_constant);
49 static const struct fs_parameter_spec *fs_lookup_key(
50 const struct fs_parameter_description *desc,
53 const struct fs_parameter_spec *p;
58 for (p = desc->specs; p->name; p++)
59 if (strcmp(p->name, name) == 0)
66 * fs_parse - Parse a filesystem configuration parameter
67 * @fc: The filesystem context to log errors through.
68 * @desc: The parameter description to use.
69 * @param: The parameter.
70 * @result: Where to place the result of the parse
72 * Parse a filesystem configuration parameter and attempt a conversion for a
73 * simple parameter for which this is requested. If successful, the determined
74 * parameter ID is placed into @result->key, the desired type is indicated in
75 * @result->t and any converted value is placed into an appropriate member of
76 * the union in @result.
78 * The function returns the parameter number if the parameter was matched,
79 * -ENOPARAM if it wasn't matched and @desc->ignore_unknown indicated that
80 * unknown parameters are okay and -EINVAL if there was a conversion issue or
81 * the parameter wasn't recognised and unknowns aren't okay.
83 int fs_parse(struct fs_context *fc,
84 const struct fs_parameter_description *desc,
85 struct fs_parameter *param,
86 struct fs_parse_result *result)
88 const struct fs_parameter_spec *p;
89 const struct fs_parameter_enum *e;
90 int ret = -ENOPARAM, b;
92 result->has_value = !!param->string;
93 result->negated = false;
96 p = fs_lookup_key(desc, param->key);
98 /* If we didn't find something that looks like "noxxx", see if
99 * "xxx" takes the "no"-form negative - but only if there
102 if (result->has_value)
103 goto unknown_parameter;
104 if (param->key[0] != 'n' || param->key[1] != 'o' || !param->key[2])
105 goto unknown_parameter;
107 p = fs_lookup_key(desc, param->key + 2);
109 goto unknown_parameter;
110 if (!(p->flags & fs_param_neg_with_no))
111 goto unknown_parameter;
112 result->boolean = false;
113 result->negated = true;
116 if (p->flags & fs_param_deprecated)
117 warnf(fc, "%s: Deprecated parameter '%s'",
118 desc->name, param->key);
123 /* Certain parameter types only take a string and convert it. */
125 case __fs_param_wasnt_defined:
127 case fs_param_is_u32:
128 case fs_param_is_u32_octal:
129 case fs_param_is_u32_hex:
130 case fs_param_is_s32:
131 case fs_param_is_u64:
132 case fs_param_is_enum:
133 case fs_param_is_string:
134 if (param->type != fs_value_is_string)
136 if (!result->has_value) {
137 if (p->flags & fs_param_v_optional)
146 /* Try to turn the type we were given into the type desired by the
147 * parameter and give an error if we can't.
150 case fs_param_is_flag:
151 if (param->type != fs_value_is_flag &&
152 (param->type != fs_value_is_string || result->has_value))
153 return invalf(fc, "%s: Unexpected value for '%s'",
154 desc->name, param->key);
155 result->boolean = true;
158 case fs_param_is_bool:
159 switch (param->type) {
160 case fs_value_is_flag:
161 result->boolean = true;
163 case fs_value_is_string:
164 if (param->size == 0) {
165 result->boolean = true;
168 b = lookup_constant(bool_names, param->string, -1);
177 case fs_param_is_u32:
178 ret = kstrtouint(param->string, 0, &result->uint_32);
180 case fs_param_is_u32_octal:
181 ret = kstrtouint(param->string, 8, &result->uint_32);
183 case fs_param_is_u32_hex:
184 ret = kstrtouint(param->string, 16, &result->uint_32);
186 case fs_param_is_s32:
187 ret = kstrtoint(param->string, 0, &result->int_32);
189 case fs_param_is_u64:
190 ret = kstrtoull(param->string, 0, &result->uint_64);
193 case fs_param_is_enum:
194 for (e = desc->enums; e->name[0]; e++) {
195 if (e->opt == p->opt &&
196 strcmp(e->name, param->string) == 0) {
197 result->uint_32 = e->value;
203 case fs_param_is_string:
205 case fs_param_is_blob:
206 if (param->type != fs_value_is_blob)
210 case fs_param_is_fd: {
211 if (param->type != fs_value_is_file)
216 case fs_param_is_blockdev:
217 case fs_param_is_path:
230 return invalf(fc, "%s: Bad value for '%s'", desc->name, param->key);
234 EXPORT_SYMBOL(fs_parse);
237 * fs_lookup_param - Look up a path referred to by a parameter
238 * @fc: The filesystem context to log errors through.
239 * @param: The parameter.
240 * @want_bdev: T if want a blockdev
241 * @_path: The result of the lookup
243 int fs_lookup_param(struct fs_context *fc,
244 struct fs_parameter *param,
249 unsigned int flags = 0;
253 switch (param->type) {
254 case fs_value_is_string:
255 f = getname_kernel(param->string);
260 case fs_value_is_filename_empty:
261 flags = LOOKUP_EMPTY;
263 case fs_value_is_filename:
268 return invalf(fc, "%s: not usable as path", param->key);
271 ret = filename_lookup(param->dirfd, f, flags, _path, NULL);
273 errorf(fc, "%s: Lookup failure for '%s'", param->key, f->name);
278 !S_ISBLK(d_backing_inode(_path->dentry)->i_mode)) {
280 _path->dentry = NULL;
282 errorf(fc, "%s: Non-blockdev passed as '%s'",
283 param->key, f->name);
292 EXPORT_SYMBOL(fs_lookup_param);
294 #ifdef CONFIG_VALIDATE_FS_PARSER
296 * validate_constant_table - Validate a constant table
297 * @name: Name to use in reporting
298 * @tbl: The constant table to validate.
299 * @tbl_size: The size of the table.
300 * @low: The lowest permissible value.
301 * @high: The highest permissible value.
302 * @special: One special permissible value outside of the range.
304 bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
305 int low, int high, int special)
311 pr_warn("VALIDATE C-TBL: Empty\n");
315 for (i = 0; i < tbl_size; i++) {
317 pr_err("VALIDATE C-TBL[%zu]: Null\n", i);
319 } else if (i > 0 && tbl[i - 1].name) {
320 int c = strcmp(tbl[i-1].name, tbl[i].name);
323 pr_err("VALIDATE C-TBL[%zu]: Duplicate %s\n",
328 pr_err("VALIDATE C-TBL[%zu]: Missorted %s>=%s\n",
329 i, tbl[i-1].name, tbl[i].name);
334 if (tbl[i].value != special &&
335 (tbl[i].value < low || tbl[i].value > high)) {
336 pr_err("VALIDATE C-TBL[%zu]: %s->%d const out of range (%d-%d)\n",
337 i, tbl[i].name, tbl[i].value, low, high);
346 * fs_validate_description - Validate a parameter description
347 * @desc: The parameter description to validate.
349 bool fs_validate_description(const struct fs_parameter_description *desc)
351 const struct fs_parameter_spec *param, *p2;
352 const struct fs_parameter_enum *e;
353 const char *name = desc->name;
354 unsigned int nr_params = 0;
355 bool good = true, enums = false;
357 pr_notice("*** VALIDATE %s ***\n", name);
360 pr_err("VALIDATE Parser: No name\n");
366 for (param = desc->specs; param->name; param++) {
367 enum fs_parameter_type t = param->type;
369 /* Check that the type is in range */
370 if (t == __fs_param_wasnt_defined ||
371 t >= nr__fs_parameter_type) {
372 pr_err("VALIDATE %s: PARAM[%s] Bad type %u\n",
373 name, param->name, t);
375 } else if (t == fs_param_is_enum) {
379 /* Check for duplicate parameter names */
380 for (p2 = desc->specs; p2 < param; p2++) {
381 if (strcmp(param->name, p2->name) == 0) {
382 pr_err("VALIDATE %s: PARAM[%s]: Duplicate\n",
389 nr_params = param - desc->specs;
394 pr_err("VALIDATE %s: Enum table but no parameters\n",
400 pr_err("VALIDATE %s: Enum table but no enum-type values\n",
406 for (e = desc->enums; e->name[0]; e++) {
407 /* Check that all entries in the enum table have at
408 * least one parameter that uses them.
410 for (param = desc->specs; param->name; param++) {
411 if (param->opt == e->opt &&
412 param->type != fs_param_is_enum) {
413 pr_err("VALIDATE %s: e[%lu] enum val for %s\n",
414 name, e - desc->enums, param->name);
420 /* Check that all enum-type parameters have at least one enum
421 * value in the enum table.
423 for (param = desc->specs; param->name; param++) {
424 if (param->type != fs_param_is_enum)
426 for (e = desc->enums; e->name[0]; e++)
427 if (e->opt == param->opt)
430 pr_err("VALIDATE %s: PARAM[%s] enum with no values\n",
437 pr_err("VALIDATE %s: enum-type values, but no enum table\n",
447 #endif /* CONFIG_VALIDATE_FS_PARSER */