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