blob: 37459124c1c154453aa2b56e78c3c7944ece80c1 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
David Howells31d921c2018-11-01 23:07:24 +00002/* Filesystem parameter description and parser
3 *
4 * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
David Howells31d921c2018-11-01 23:07:24 +00006 */
7
8#ifndef _LINUX_FS_PARSER_H
9#define _LINUX_FS_PARSER_H
10
11#include <linux/fs_context.h>
12
13struct path;
14
15struct constant_table {
16 const char *name;
17 int value;
18};
19
20/*
21 * The type of parameter expected.
22 */
23enum fs_parameter_type {
24 __fs_param_wasnt_defined,
25 fs_param_is_flag,
26 fs_param_is_bool,
27 fs_param_is_u32,
28 fs_param_is_u32_octal,
29 fs_param_is_u32_hex,
30 fs_param_is_s32,
31 fs_param_is_u64,
32 fs_param_is_enum,
33 fs_param_is_string,
34 fs_param_is_blob,
35 fs_param_is_blockdev,
36 fs_param_is_path,
37 fs_param_is_fd,
38 nr__fs_parameter_type,
39};
40
41/*
42 * Specification of the type of value a parameter wants.
43 *
44 * Note that the fsparam_flag(), fsparam_string(), fsparam_u32(), ... macros
45 * should be used to generate elements of this type.
46 */
47struct fs_parameter_spec {
48 const char *name;
49 u8 opt; /* Option number (returned by fs_parse()) */
50 enum fs_parameter_type type:8; /* The desired parameter type */
51 unsigned short flags;
52#define fs_param_v_optional 0x0001 /* The value is optional */
53#define fs_param_neg_with_no 0x0002 /* "noxxx" is negative param */
54#define fs_param_neg_with_empty 0x0004 /* "xxx=" is negative param */
55#define fs_param_deprecated 0x0008 /* The param is deprecated */
Al Viro2710c957a2019-09-06 22:12:08 -040056 const void *data;
David Howells31d921c2018-11-01 23:07:24 +000057};
58
David Howells31d921c2018-11-01 23:07:24 +000059struct fs_parameter_description {
60 const char name[16]; /* Name for logging purposes */
61 const struct fs_parameter_spec *specs; /* List of param specifications */
David Howells31d921c2018-11-01 23:07:24 +000062};
63
64/*
65 * Result of parse.
66 */
67struct fs_parse_result {
68 bool negated; /* T if param was "noxxx" */
David Howells31d921c2018-11-01 23:07:24 +000069 union {
70 bool boolean; /* For spec_bool */
71 int int_32; /* For spec_s32/spec_enum */
72 unsigned int uint_32; /* For spec_u32{,_octal,_hex}/spec_enum */
73 u64 uint_64; /* For spec_u64 */
74 };
75};
76
Al Viro7f5d3812019-12-20 23:52:55 -050077extern int __fs_parse(struct p_log *log,
78 const struct fs_parameter_description *desc,
79 struct fs_parameter *value,
80 struct fs_parse_result *result);
Al Virocc3c0b52019-12-21 00:16:49 -050081
82static inline int fs_parse(struct fs_context *fc,
83 const struct fs_parameter_description *desc,
84 struct fs_parameter *param,
85 struct fs_parse_result *result)
86{
87 return __fs_parse(&fc->log, desc, param, result);
88}
89
David Howells31d921c2018-11-01 23:07:24 +000090extern int fs_lookup_param(struct fs_context *fc,
91 struct fs_parameter *param,
92 bool want_bdev,
93 struct path *_path);
94
Al Viro34264ae2019-12-16 13:45:41 -050095extern int lookup_constant(const struct constant_table tbl[], const char *name, int not_found);
David Howells31d921c2018-11-01 23:07:24 +000096
97#ifdef CONFIG_VALIDATE_FS_PARSER
98extern bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
99 int low, int high, int special);
100extern bool fs_validate_description(const struct fs_parameter_description *desc);
101#else
102static inline bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
103 int low, int high, int special)
104{ return true; }
105static inline bool fs_validate_description(const struct fs_parameter_description *desc)
106{ return true; }
107#endif
108
109/*
110 * Parameter type, name, index and flags element constructors. Use as:
111 *
112 * fsparam_xxxx("foo", Opt_foo)
113 *
114 * If existing helpers are not enough, direct use of __fsparam() would
115 * work, but any such case is probably a sign that new helper is needed.
116 * Helpers will remain stable; low-level implementation may change.
117 */
Al Viro2710c957a2019-09-06 22:12:08 -0400118#define __fsparam(TYPE, NAME, OPT, FLAGS, DATA) \
David Howells31d921c2018-11-01 23:07:24 +0000119 { \
120 .name = NAME, \
121 .opt = OPT, \
122 .type = TYPE, \
Al Viro2710c957a2019-09-06 22:12:08 -0400123 .flags = FLAGS, \
124 .data = DATA \
David Howells31d921c2018-11-01 23:07:24 +0000125 }
126
Al Viro2710c957a2019-09-06 22:12:08 -0400127#define fsparam_flag(NAME, OPT) __fsparam(fs_param_is_flag, NAME, OPT, 0, NULL)
David Howells31d921c2018-11-01 23:07:24 +0000128#define fsparam_flag_no(NAME, OPT) \
129 __fsparam(fs_param_is_flag, NAME, OPT, \
Al Viro2710c957a2019-09-06 22:12:08 -0400130 fs_param_neg_with_no, NULL)
131#define fsparam_bool(NAME, OPT) __fsparam(fs_param_is_bool, NAME, OPT, 0, NULL)
132#define fsparam_u32(NAME, OPT) __fsparam(fs_param_is_u32, NAME, OPT, 0, NULL)
David Howells31d921c2018-11-01 23:07:24 +0000133#define fsparam_u32oct(NAME, OPT) \
Al Viro2710c957a2019-09-06 22:12:08 -0400134 __fsparam(fs_param_is_u32_octal, NAME, OPT, 0, NULL)
David Howells31d921c2018-11-01 23:07:24 +0000135#define fsparam_u32hex(NAME, OPT) \
Al Viro2710c957a2019-09-06 22:12:08 -0400136 __fsparam(fs_param_is_u32_hex, NAME, OPT, 0, NULL)
137#define fsparam_s32(NAME, OPT) __fsparam(fs_param_is_s32, NAME, OPT, 0, NULL)
138#define fsparam_u64(NAME, OPT) __fsparam(fs_param_is_u64, NAME, OPT, 0, NULL)
139#define fsparam_enum(NAME, OPT, array) __fsparam(fs_param_is_enum, NAME, OPT, 0, array)
David Howells31d921c2018-11-01 23:07:24 +0000140#define fsparam_string(NAME, OPT) \
Al Viro2710c957a2019-09-06 22:12:08 -0400141 __fsparam(fs_param_is_string, NAME, OPT, 0, NULL)
142#define fsparam_blob(NAME, OPT) __fsparam(fs_param_is_blob, NAME, OPT, 0, NULL)
143#define fsparam_bdev(NAME, OPT) __fsparam(fs_param_is_blockdev, NAME, OPT, 0, NULL)
144#define fsparam_path(NAME, OPT) __fsparam(fs_param_is_path, NAME, OPT, 0, NULL)
145#define fsparam_fd(NAME, OPT) __fsparam(fs_param_is_fd, NAME, OPT, 0, NULL)
David Howells31d921c2018-11-01 23:07:24 +0000146
147
148#endif /* _LINUX_FS_PARSER_H */