blob: aab0ffc6bac67a074a99590ca348f001d2db4e03 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-or-later */
2/* Filesystem parameter description and parser
3 *
4 * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
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
Olivier Deprez157378f2022-04-04 15:47:50 +020020struct fs_parameter_spec;
21struct fs_parse_result;
22typedef int fs_param_type(struct p_log *,
23 const struct fs_parameter_spec *,
24 struct fs_parameter *,
25 struct fs_parse_result *);
David Brazdil0f672f62019-12-10 10:32:29 +000026/*
27 * The type of parameter expected.
28 */
Olivier Deprez157378f2022-04-04 15:47:50 +020029fs_param_type fs_param_is_bool, fs_param_is_u32, fs_param_is_s32, fs_param_is_u64,
30 fs_param_is_enum, fs_param_is_string, fs_param_is_blob, fs_param_is_blockdev,
31 fs_param_is_path, fs_param_is_fd;
David Brazdil0f672f62019-12-10 10:32:29 +000032
33/*
34 * Specification of the type of value a parameter wants.
35 *
36 * Note that the fsparam_flag(), fsparam_string(), fsparam_u32(), ... macros
37 * should be used to generate elements of this type.
38 */
39struct fs_parameter_spec {
40 const char *name;
Olivier Deprez157378f2022-04-04 15:47:50 +020041 fs_param_type *type; /* The desired parameter type */
David Brazdil0f672f62019-12-10 10:32:29 +000042 u8 opt; /* Option number (returned by fs_parse()) */
David Brazdil0f672f62019-12-10 10:32:29 +000043 unsigned short flags;
David Brazdil0f672f62019-12-10 10:32:29 +000044#define fs_param_neg_with_no 0x0002 /* "noxxx" is negative param */
45#define fs_param_neg_with_empty 0x0004 /* "xxx=" is negative param */
46#define fs_param_deprecated 0x0008 /* The param is deprecated */
Olivier Deprez157378f2022-04-04 15:47:50 +020047 const void *data;
David Brazdil0f672f62019-12-10 10:32:29 +000048};
49
50/*
51 * Result of parse.
52 */
53struct fs_parse_result {
54 bool negated; /* T if param was "noxxx" */
David Brazdil0f672f62019-12-10 10:32:29 +000055 union {
56 bool boolean; /* For spec_bool */
57 int int_32; /* For spec_s32/spec_enum */
58 unsigned int uint_32; /* For spec_u32{,_octal,_hex}/spec_enum */
59 u64 uint_64; /* For spec_u64 */
60 };
61};
62
Olivier Deprez157378f2022-04-04 15:47:50 +020063extern int __fs_parse(struct p_log *log,
64 const struct fs_parameter_spec *desc,
David Brazdil0f672f62019-12-10 10:32:29 +000065 struct fs_parameter *value,
66 struct fs_parse_result *result);
Olivier Deprez157378f2022-04-04 15:47:50 +020067
68static inline int fs_parse(struct fs_context *fc,
69 const struct fs_parameter_spec *desc,
70 struct fs_parameter *param,
71 struct fs_parse_result *result)
72{
73 return __fs_parse(&fc->log, desc, param, result);
74}
75
David Brazdil0f672f62019-12-10 10:32:29 +000076extern int fs_lookup_param(struct fs_context *fc,
77 struct fs_parameter *param,
78 bool want_bdev,
79 struct path *_path);
80
Olivier Deprez157378f2022-04-04 15:47:50 +020081extern int lookup_constant(const struct constant_table tbl[], const char *name, int not_found);
David Brazdil0f672f62019-12-10 10:32:29 +000082
83#ifdef CONFIG_VALIDATE_FS_PARSER
84extern bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
85 int low, int high, int special);
Olivier Deprez157378f2022-04-04 15:47:50 +020086extern bool fs_validate_description(const char *name,
87 const struct fs_parameter_spec *desc);
David Brazdil0f672f62019-12-10 10:32:29 +000088#else
89static inline bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
90 int low, int high, int special)
91{ return true; }
Olivier Deprez157378f2022-04-04 15:47:50 +020092static inline bool fs_validate_description(const char *name,
93 const struct fs_parameter_spec *desc)
David Brazdil0f672f62019-12-10 10:32:29 +000094{ return true; }
95#endif
96
97/*
98 * Parameter type, name, index and flags element constructors. Use as:
99 *
100 * fsparam_xxxx("foo", Opt_foo)
101 *
102 * If existing helpers are not enough, direct use of __fsparam() would
103 * work, but any such case is probably a sign that new helper is needed.
104 * Helpers will remain stable; low-level implementation may change.
105 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200106#define __fsparam(TYPE, NAME, OPT, FLAGS, DATA) \
David Brazdil0f672f62019-12-10 10:32:29 +0000107 { \
108 .name = NAME, \
109 .opt = OPT, \
110 .type = TYPE, \
Olivier Deprez157378f2022-04-04 15:47:50 +0200111 .flags = FLAGS, \
112 .data = DATA \
David Brazdil0f672f62019-12-10 10:32:29 +0000113 }
114
Olivier Deprez157378f2022-04-04 15:47:50 +0200115#define fsparam_flag(NAME, OPT) __fsparam(NULL, NAME, OPT, 0, NULL)
David Brazdil0f672f62019-12-10 10:32:29 +0000116#define fsparam_flag_no(NAME, OPT) \
Olivier Deprez157378f2022-04-04 15:47:50 +0200117 __fsparam(NULL, NAME, OPT, fs_param_neg_with_no, NULL)
118#define fsparam_bool(NAME, OPT) __fsparam(fs_param_is_bool, NAME, OPT, 0, NULL)
119#define fsparam_u32(NAME, OPT) __fsparam(fs_param_is_u32, NAME, OPT, 0, NULL)
David Brazdil0f672f62019-12-10 10:32:29 +0000120#define fsparam_u32oct(NAME, OPT) \
Olivier Deprez157378f2022-04-04 15:47:50 +0200121 __fsparam(fs_param_is_u32, NAME, OPT, 0, (void *)8)
David Brazdil0f672f62019-12-10 10:32:29 +0000122#define fsparam_u32hex(NAME, OPT) \
Olivier Deprez157378f2022-04-04 15:47:50 +0200123 __fsparam(fs_param_is_u32_hex, NAME, OPT, 0, (void *)16)
124#define fsparam_s32(NAME, OPT) __fsparam(fs_param_is_s32, NAME, OPT, 0, NULL)
125#define fsparam_u64(NAME, OPT) __fsparam(fs_param_is_u64, NAME, OPT, 0, NULL)
126#define fsparam_enum(NAME, OPT, array) __fsparam(fs_param_is_enum, NAME, OPT, 0, array)
David Brazdil0f672f62019-12-10 10:32:29 +0000127#define fsparam_string(NAME, OPT) \
Olivier Deprez157378f2022-04-04 15:47:50 +0200128 __fsparam(fs_param_is_string, NAME, OPT, 0, NULL)
129#define fsparam_blob(NAME, OPT) __fsparam(fs_param_is_blob, NAME, OPT, 0, NULL)
130#define fsparam_bdev(NAME, OPT) __fsparam(fs_param_is_blockdev, NAME, OPT, 0, NULL)
131#define fsparam_path(NAME, OPT) __fsparam(fs_param_is_path, NAME, OPT, 0, NULL)
132#define fsparam_fd(NAME, OPT) __fsparam(fs_param_is_fd, NAME, OPT, 0, NULL)
David Brazdil0f672f62019-12-10 10:32:29 +0000133
134#endif /* _LINUX_FS_PARSER_H */