b4d0d230ccfb5 (Thomas Gleixner 2019-05-20 19:08:01 +0200 1) // SPDX-License-Identifier: GPL-2.0-or-later
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 2) /* Filesystem parameter parser.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 3) *
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 4) * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 5) * Written by David Howells (dhowells@redhat.com)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 6) */
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 7)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 8) #include <linux/export.h>
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 9) #include <linux/fs_context.h>
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 10) #include <linux/fs_parser.h>
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 11) #include <linux/slab.h>
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 12) #include <linux/security.h>
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 13) #include <linux/namei.h>
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 14) #include "internal.h"
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 15)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 16) static const struct constant_table bool_names[] = {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 17) { "0", false },
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 18) { "1", true },
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 19) { "false", false },
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 20) { "no", false },
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 21) { "true", true },
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 22) { "yes", true },
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 23) { },
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 24) };
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 25)
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 26) static const struct constant_table *
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 27) __lookup_constant(const struct constant_table *tbl, const char *name)
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 28) {
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 29) for ( ; tbl->name; tbl++)
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 30) if (strcmp(name, tbl->name) == 0)
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 31) return tbl;
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 32) return NULL;
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 33) }
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 34)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 35) /**
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 36) * lookup_constant - Look up a constant by name in an ordered table
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 37) * @tbl: The table of constants to search.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 38) * @name: The name to look up.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 39) * @not_found: The value to return if the name is not found.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 40) */
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 41) int lookup_constant(const struct constant_table *tbl, const char *name, int not_found)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 42) {
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 43) const struct constant_table *p = __lookup_constant(tbl, name);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 44)
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 45) return p ? p->value : not_found;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 46) }
34264ae3fa224 (Al Viro 2019-12-16 13:45:41 -0500 47) EXPORT_SYMBOL(lookup_constant);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 48)
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 49) static inline bool is_flag(const struct fs_parameter_spec *p)
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 50) {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 51) return p->type == NULL;
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 52) }
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 53)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 54) static const struct fs_parameter_spec *fs_lookup_key(
d7167b149943e (Al Viro 2019-09-07 07:23:15 -0400 55) const struct fs_parameter_spec *desc,
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 56) struct fs_parameter *param, bool *negated)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 57) {
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 58) const struct fs_parameter_spec *p, *other = NULL;
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 59) const char *name = param->key;
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 60) bool want_flag = param->type == fs_value_is_flag;
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 61)
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 62) *negated = false;
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 63) for (p = desc; p->name; p++) {
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 64) if (strcmp(p->name, name) != 0)
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 65) continue;
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 66) if (likely(is_flag(p) == want_flag))
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 67) return p;
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 68) other = p;
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 69) }
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 70) if (want_flag) {
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 71) if (name[0] == 'n' && name[1] == 'o' && name[2]) {
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 72) for (p = desc; p->name; p++) {
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 73) if (strcmp(p->name, name + 2) != 0)
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 74) continue;
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 75) if (!(p->flags & fs_param_neg_with_no))
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 76) continue;
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 77) *negated = true;
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 78) return p;
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 79) }
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 80) }
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 81) }
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 82) return other;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 83) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 84)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 85) /*
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 86) * fs_parse - Parse a filesystem configuration parameter
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 87) * @fc: The filesystem context to log errors through.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 88) * @desc: The parameter description to use.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 89) * @param: The parameter.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 90) * @result: Where to place the result of the parse
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 91) *
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 92) * Parse a filesystem configuration parameter and attempt a conversion for a
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 93) * simple parameter for which this is requested. If successful, the determined
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 94) * parameter ID is placed into @result->key, the desired type is indicated in
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 95) * @result->t and any converted value is placed into an appropriate member of
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 96) * the union in @result.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 97) *
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 98) * The function returns the parameter number if the parameter was matched,
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 99) * -ENOPARAM if it wasn't matched and @desc->ignore_unknown indicated that
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 100) * unknown parameters are okay and -EINVAL if there was a conversion issue or
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 101) * the parameter wasn't recognised and unknowns aren't okay.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 102) */
7f5d38141e309 (Al Viro 2019-12-20 23:52:55 -0500 103) int __fs_parse(struct p_log *log,
d7167b149943e (Al Viro 2019-09-07 07:23:15 -0400 104) const struct fs_parameter_spec *desc,
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 105) struct fs_parameter *param,
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 106) struct fs_parse_result *result)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 107) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 108) const struct fs_parameter_spec *p;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 109)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 110) result->uint_64 = 0;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 111)
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 112) p = fs_lookup_key(desc, param, &result->negated);
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 113) if (!p)
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 114) return -ENOPARAM;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 115)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 116) if (p->flags & fs_param_deprecated)
7f5d38141e309 (Al Viro 2019-12-20 23:52:55 -0500 117) warn_plog(log, "Deprecated parameter '%s'", param->key);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 118)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 119) /* Try to turn the type we were given into the type desired by the
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 120) * parameter and give an error if we can't.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 121) */
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 122) if (is_flag(p)) {
0f89589a8c6f1 (Al Viro 2019-12-17 14:15:04 -0500 123) if (param->type != fs_value_is_flag)
7f5d38141e309 (Al Viro 2019-12-20 23:52:55 -0500 124) return inval_plog(log, "Unexpected value for '%s'",
7f5d38141e309 (Al Viro 2019-12-20 23:52:55 -0500 125) param->key);
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 126) result->boolean = !result->negated;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 127) } else {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 128) int ret = p->type(log, p, param, result);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 129) if (ret)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 130) return ret;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 131) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 132) return p->opt;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 133) }
7f5d38141e309 (Al Viro 2019-12-20 23:52:55 -0500 134) EXPORT_SYMBOL(__fs_parse);
7f5d38141e309 (Al Viro 2019-12-20 23:52:55 -0500 135)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 136) /**
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 137) * fs_lookup_param - Look up a path referred to by a parameter
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 138) * @fc: The filesystem context to log errors through.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 139) * @param: The parameter.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 140) * @want_bdev: T if want a blockdev
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 141) * @_path: The result of the lookup
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 142) */
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 143) int fs_lookup_param(struct fs_context *fc,
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 144) struct fs_parameter *param,
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 145) bool want_bdev,
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 146) struct path *_path)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 147) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 148) struct filename *f;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 149) unsigned int flags = 0;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 150) bool put_f;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 151) int ret;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 152)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 153) switch (param->type) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 154) case fs_value_is_string:
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 155) f = getname_kernel(param->string);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 156) if (IS_ERR(f))
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 157) return PTR_ERR(f);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 158) put_f = true;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 159) break;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 160) case fs_value_is_filename:
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 161) f = param->name;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 162) put_f = false;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 163) break;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 164) default:
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 165) return invalf(fc, "%s: not usable as path", param->key);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 166) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 167)
7cdfa44227b0d (David Howells 2019-03-25 16:38:22 +0000 168) f->refcnt++; /* filename_lookup() drops our ref. */
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 169) ret = filename_lookup(param->dirfd, f, flags, _path, NULL);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 170) if (ret < 0) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 171) errorf(fc, "%s: Lookup failure for '%s'", param->key, f->name);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 172) goto out;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 173) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 174)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 175) if (want_bdev &&
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 176) !S_ISBLK(d_backing_inode(_path->dentry)->i_mode)) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 177) path_put(_path);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 178) _path->dentry = NULL;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 179) _path->mnt = NULL;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 180) errorf(fc, "%s: Non-blockdev passed as '%s'",
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 181) param->key, f->name);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 182) ret = -ENOTBLK;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 183) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 184)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 185) out:
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 186) if (put_f)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 187) putname(f);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 188) return ret;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 189) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 190) EXPORT_SYMBOL(fs_lookup_param);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 191)
97383c741b061 (Luo Jiaxing 2020-10-13 16:48:30 -0700 192) static int fs_param_bad_value(struct p_log *log, struct fs_parameter *param)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 193) {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 194) return inval_plog(log, "Bad value for '%s'", param->key);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 195) }
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 196)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 197) int fs_param_is_bool(struct p_log *log, const struct fs_parameter_spec *p,
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 198) struct fs_parameter *param, struct fs_parse_result *result)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 199) {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 200) int b;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 201) if (param->type != fs_value_is_string)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 202) return fs_param_bad_value(log, param);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 203) b = lookup_constant(bool_names, param->string, -1);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 204) if (b == -1)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 205) return fs_param_bad_value(log, param);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 206) result->boolean = b;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 207) return 0;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 208) }
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 209) EXPORT_SYMBOL(fs_param_is_bool);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 210)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 211) int fs_param_is_u32(struct p_log *log, const struct fs_parameter_spec *p,
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 212) struct fs_parameter *param, struct fs_parse_result *result)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 213) {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 214) int base = (unsigned long)p->data;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 215) if (param->type != fs_value_is_string ||
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 216) kstrtouint(param->string, base, &result->uint_32) < 0)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 217) return fs_param_bad_value(log, param);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 218) return 0;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 219) }
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 220) EXPORT_SYMBOL(fs_param_is_u32);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 221)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 222) int fs_param_is_s32(struct p_log *log, const struct fs_parameter_spec *p,
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 223) struct fs_parameter *param, struct fs_parse_result *result)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 224) {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 225) if (param->type != fs_value_is_string ||
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 226) kstrtoint(param->string, 0, &result->int_32) < 0)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 227) return fs_param_bad_value(log, param);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 228) return 0;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 229) }
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 230) EXPORT_SYMBOL(fs_param_is_s32);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 231)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 232) int fs_param_is_u64(struct p_log *log, const struct fs_parameter_spec *p,
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 233) struct fs_parameter *param, struct fs_parse_result *result)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 234) {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 235) if (param->type != fs_value_is_string ||
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 236) kstrtoull(param->string, 0, &result->uint_64) < 0)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 237) return fs_param_bad_value(log, param);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 238) return 0;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 239) }
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 240) EXPORT_SYMBOL(fs_param_is_u64);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 241)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 242) int fs_param_is_enum(struct p_log *log, const struct fs_parameter_spec *p,
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 243) struct fs_parameter *param, struct fs_parse_result *result)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 244) {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 245) const struct constant_table *c;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 246) if (param->type != fs_value_is_string)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 247) return fs_param_bad_value(log, param);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 248) c = __lookup_constant(p->data, param->string);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 249) if (!c)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 250) return fs_param_bad_value(log, param);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 251) result->uint_32 = c->value;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 252) return 0;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 253) }
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 254) EXPORT_SYMBOL(fs_param_is_enum);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 255)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 256) int fs_param_is_string(struct p_log *log, const struct fs_parameter_spec *p,
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 257) struct fs_parameter *param, struct fs_parse_result *result)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 258) {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 259) if (param->type != fs_value_is_string || !*param->string)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 260) return fs_param_bad_value(log, param);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 261) return 0;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 262) }
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 263) EXPORT_SYMBOL(fs_param_is_string);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 264)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 265) int fs_param_is_blob(struct p_log *log, const struct fs_parameter_spec *p,
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 266) struct fs_parameter *param, struct fs_parse_result *result)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 267) {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 268) if (param->type != fs_value_is_blob)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 269) return fs_param_bad_value(log, param);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 270) return 0;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 271) }
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 272) EXPORT_SYMBOL(fs_param_is_blob);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 273)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 274) int fs_param_is_fd(struct p_log *log, const struct fs_parameter_spec *p,
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 275) struct fs_parameter *param, struct fs_parse_result *result)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 276) {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 277) switch (param->type) {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 278) case fs_value_is_string:
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 279) if (kstrtouint(param->string, 0, &result->uint_32) < 0)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 280) break;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 281) if (result->uint_32 <= INT_MAX)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 282) return 0;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 283) break;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 284) case fs_value_is_file:
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 285) result->uint_32 = param->dirfd;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 286) if (result->uint_32 <= INT_MAX)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 287) return 0;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 288) break;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 289) default:
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 290) break;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 291) }
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 292) return fs_param_bad_value(log, param);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 293) }
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 294) EXPORT_SYMBOL(fs_param_is_fd);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 295)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 296) int fs_param_is_blockdev(struct p_log *log, const struct fs_parameter_spec *p,
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 297) struct fs_parameter *param, struct fs_parse_result *result)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 298) {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 299) return 0;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 300) }
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 301) EXPORT_SYMBOL(fs_param_is_blockdev);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 302)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 303) int fs_param_is_path(struct p_log *log, const struct fs_parameter_spec *p,
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 304) struct fs_parameter *param, struct fs_parse_result *result)
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 305) {
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 306) return 0;
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 307) }
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 308) EXPORT_SYMBOL(fs_param_is_path);
328de5287b10a (Al Viro 2019-12-18 00:02:31 -0500 309)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 310) #ifdef CONFIG_VALIDATE_FS_PARSER
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 311) /**
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 312) * validate_constant_table - Validate a constant table
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 313) * @tbl: The constant table to validate.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 314) * @tbl_size: The size of the table.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 315) * @low: The lowest permissible value.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 316) * @high: The highest permissible value.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 317) * @special: One special permissible value outside of the range.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 318) */
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 319) bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 320) int low, int high, int special)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 321) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 322) size_t i;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 323) bool good = true;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 324)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 325) if (tbl_size == 0) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 326) pr_warn("VALIDATE C-TBL: Empty\n");
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 327) return true;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 328) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 329)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 330) for (i = 0; i < tbl_size; i++) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 331) if (!tbl[i].name) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 332) pr_err("VALIDATE C-TBL[%zu]: Null\n", i);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 333) good = false;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 334) } else if (i > 0 && tbl[i - 1].name) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 335) int c = strcmp(tbl[i-1].name, tbl[i].name);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 336)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 337) if (c == 0) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 338) pr_err("VALIDATE C-TBL[%zu]: Duplicate %s\n",
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 339) i, tbl[i].name);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 340) good = false;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 341) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 342) if (c > 0) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 343) pr_err("VALIDATE C-TBL[%zu]: Missorted %s>=%s\n",
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 344) i, tbl[i-1].name, tbl[i].name);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 345) good = false;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 346) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 347) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 348)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 349) if (tbl[i].value != special &&
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 350) (tbl[i].value < low || tbl[i].value > high)) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 351) pr_err("VALIDATE C-TBL[%zu]: %s->%d const out of range (%d-%d)\n",
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 352) i, tbl[i].name, tbl[i].value, low, high);
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 353) good = false;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 354) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 355) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 356)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 357) return good;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 358) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 359)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 360) /**
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 361) * fs_validate_description - Validate a parameter description
21ae3ad1632cb (Randy Dunlap 2021-04-29 22:54:17 -0700 362) * @name: The parameter name to search for.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 363) * @desc: The parameter description to validate.
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 364) */
96cafb9ccb153 (Eric Sandeen 2019-12-06 10:45:01 -0600 365) bool fs_validate_description(const char *name,
d7167b149943e (Al Viro 2019-09-07 07:23:15 -0400 366) const struct fs_parameter_spec *desc)
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 367) {
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 368) const struct fs_parameter_spec *param, *p2;
2710c957a8ef4 (Al Viro 2019-09-06 22:12:08 -0400 369) bool good = true;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 370)
d7167b149943e (Al Viro 2019-09-07 07:23:15 -0400 371) for (param = desc; param->name; param++) {
d7167b149943e (Al Viro 2019-09-07 07:23:15 -0400 372) /* Check for duplicate parameter names */
d7167b149943e (Al Viro 2019-09-07 07:23:15 -0400 373) for (p2 = desc; p2 < param; p2++) {
d7167b149943e (Al Viro 2019-09-07 07:23:15 -0400 374) if (strcmp(param->name, p2->name) == 0) {
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 375) if (is_flag(param) != is_flag(p2))
48ce73b1bef20 (Al Viro 2019-12-17 20:03:59 -0500 376) continue;
d7167b149943e (Al Viro 2019-09-07 07:23:15 -0400 377) pr_err("VALIDATE %s: PARAM[%s]: Duplicate\n",
d7167b149943e (Al Viro 2019-09-07 07:23:15 -0400 378) name, param->name);
d7167b149943e (Al Viro 2019-09-07 07:23:15 -0400 379) good = false;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 380) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 381) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 382) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 383) return good;
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 384) }
31d921c7fb969 (David Howells 2018-11-01 23:07:24 +0000 385) #endif /* CONFIG_VALIDATE_FS_PARSER */