1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or https://opensource.org/licenses/CDDL-1.0.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (C) 2016 Gvozden Neskovic <neskovic@gmail.com>.
24  * Copyright (c) 2020 by Delphix. All rights reserved.
25  */
26 
27 #ifndef _MOD_COMPAT_H
28 #define	_MOD_COMPAT_H
29 
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 
33 /*
34  * Despite constifying struct kernel_param_ops, some older kernels define a
35  * `__check_old_set_param()` function in their headers that checks for a
36  * non-constified `->set()`. This has long been fixed in Linux mainline, but
37  * since we support older kernels, we workaround it by using a preprocessor
38  * definition to disable it.
39  */
40 #define	__check_old_set_param(_) (0)
41 
42 typedef const struct kernel_param zfs_kernel_param_t;
43 
44 #define	ZMOD_RW 0644
45 #define	ZMOD_RD 0444
46 
47 enum scope_prefix_types {
48 	zfs,
49 	zfs_arc,
50 	zfs_brt,
51 	zfs_condense,
52 	zfs_dbuf,
53 	zfs_dbuf_cache,
54 	zfs_deadman,
55 	zfs_dedup,
56 	zfs_l2arc,
57 	zfs_livelist,
58 	zfs_livelist_condense,
59 	zfs_lua,
60 	zfs_metaslab,
61 	zfs_mg,
62 	zfs_multihost,
63 	zfs_prefetch,
64 	zfs_reconstruct,
65 	zfs_recv,
66 	zfs_send,
67 	zfs_spa,
68 	zfs_trim,
69 	zfs_txg,
70 	zfs_vdev,
71 	zfs_vdev_disk,
72 	zfs_vdev_file,
73 	zfs_vdev_mirror,
74 	zfs_vnops,
75 	zfs_zevent,
76 	zfs_zio,
77 	zfs_zil
78 };
79 
80 /*
81  * While we define our own s64/u64 types, there is no reason to reimplement the
82  * existing Linux kernel types, so we use the preprocessor to remap our
83  * "custom" implementations to the kernel ones. This is done because the CPP
84  * does not allow us to write conditional definitions. The fourth definition
85  * exists because the CPP will not allow us to replace things like INT with int
86  * before string concatenation.
87  */
88 
89 #define	spl_param_set_int param_set_int
90 #define	spl_param_get_int param_get_int
91 #define	spl_param_ops_int param_ops_int
92 #define	spl_param_ops_INT param_ops_int
93 
94 #define	spl_param_set_long param_set_long
95 #define	spl_param_get_long param_get_long
96 #define	spl_param_ops_long param_ops_long
97 #define	spl_param_ops_LONG param_ops_long
98 
99 #define	spl_param_set_uint param_set_uint
100 #define	spl_param_get_uint param_get_uint
101 #define	spl_param_ops_uint param_ops_uint
102 #define	spl_param_ops_UINT param_ops_uint
103 
104 #define	spl_param_set_ulong param_set_ulong
105 #define	spl_param_get_ulong param_get_ulong
106 #define	spl_param_ops_ulong param_ops_ulong
107 #define	spl_param_ops_ULONG param_ops_ulong
108 
109 #define	spl_param_set_charp param_set_charp
110 #define	spl_param_get_charp param_get_charp
111 #define	spl_param_ops_charp param_ops_charp
112 #define	spl_param_ops_STRING param_ops_charp
113 
114 int spl_param_set_s64(const char *val, zfs_kernel_param_t *kp);
115 extern int spl_param_get_s64(char *buffer, zfs_kernel_param_t *kp);
116 extern const struct kernel_param_ops spl_param_ops_s64;
117 #define	spl_param_ops_S64 spl_param_ops_s64
118 
119 extern int spl_param_set_u64(const char *val, zfs_kernel_param_t *kp);
120 extern int spl_param_get_u64(char *buffer, zfs_kernel_param_t *kp);
121 extern const struct kernel_param_ops spl_param_ops_u64;
122 #define	spl_param_ops_U64 spl_param_ops_u64
123 
124 /*
125  * Declare a module parameter / sysctl node
126  *
127  * "scope_prefix" the part of the sysctl / sysfs tree the node resides under
128  *   (currently a no-op on Linux)
129  * "name_prefix" the part of the variable name that will be excluded from the
130  *   exported names on platforms with a hierarchical namespace
131  * "name" the part of the variable that will be exposed on platforms with a
132  *    hierarchical namespace, or as name_prefix ## name on Linux
133  * "type" the variable type
134  * "perm" the permissions (read/write or read only)
135  * "desc" a brief description of the option
136  *
137  * Examples:
138  * ZFS_MODULE_PARAM(zfs_vdev_mirror, zfs_vdev_mirror_, rotating_inc, UINT,
139  * 	ZMOD_RW, "Rotating media load increment for non-seeking I/O's");
140  * on FreeBSD:
141  *   vfs.zfs.vdev.mirror.rotating_inc
142  * on Linux:
143  *   zfs_vdev_mirror_rotating_inc
144  *
145  * ZFS_MODULE_PARAM(zfs, , dmu_prefetch_max, UINT, ZMOD_RW,
146  * 	"Limit one prefetch call to this size");
147  * on FreeBSD:
148  *   vfs.zfs.dmu_prefetch_max
149  * on Linux:
150  *   dmu_prefetch_max
151  */
152 #define	ZFS_MODULE_PARAM(scope_prefix, name_prefix, name, type, perm, desc) \
153 	_Static_assert( \
154 	    sizeof (scope_prefix) == sizeof (enum scope_prefix_types), \
155 	    "" #scope_prefix " size mismatch with enum scope_prefix_types"); \
156 	module_param_cb(name_prefix ## name, &spl_param_ops_ ## type, \
157 	    &name_prefix ## name, perm); \
158 	MODULE_PARM_DESC(name_prefix ## name, desc)
159 
160 /*
161  * Declare a module parameter / sysctl node
162  *
163  * "scope_prefix" the part of the the sysctl / sysfs tree the node resides under
164  *   (currently a no-op on Linux)
165  * "name_prefix" the part of the variable name that will be excluded from the
166  *   exported names on platforms with a hierarchical namespace
167  * "name" the part of the variable that will be exposed on platforms with a
168  *    hierarchical namespace, or as name_prefix ## name on Linux
169  * "setfunc" setter function
170  * "getfunc" getter function
171  * "perm" the permissions (read/write or read only)
172  * "desc" a brief description of the option
173  *
174  * Examples:
175  * ZFS_MODULE_PARAM_CALL(zfs_spa, spa_, slop_shift, param_set_slop_shift,
176  * 	param_get_int, ZMOD_RW, "Reserved free space in pool");
177  * on FreeBSD:
178  *   vfs.zfs.spa_slop_shift
179  * on Linux:
180  *   spa_slop_shift
181  */
182 #define	ZFS_MODULE_PARAM_CALL( \
183     scope_prefix, name_prefix, name, setfunc, getfunc, perm, desc) \
184 	_Static_assert( \
185 	    sizeof (scope_prefix) == sizeof (enum scope_prefix_types), \
186 	    "" #scope_prefix " size mismatch with enum scope_prefix_types"); \
187 	module_param_call(name_prefix ## name, setfunc, getfunc, \
188 	    &name_prefix ## name, perm); \
189 	MODULE_PARM_DESC(name_prefix ## name, desc)
190 
191 /*
192  * As above, but there is no variable with the name name_prefix ## name,
193  * so NULL is passed to module_param_call instead.
194  */
195 #define	ZFS_MODULE_VIRTUAL_PARAM_CALL( \
196     scope_prefix, name_prefix, name, setfunc, getfunc, perm, desc) \
197 	_Static_assert( \
198 	    sizeof (scope_prefix) == sizeof (enum scope_prefix_types), \
199 	    "" #scope_prefix " size mismatch with enum scope_prefix_types"); \
200 	module_param_call(name_prefix ## name, setfunc, getfunc, NULL, perm); \
201 	MODULE_PARM_DESC(name_prefix ## name, desc)
202 
203 #define	ZFS_MODULE_PARAM_ARGS	const char *buf, zfs_kernel_param_t *kp
204 
205 #endif	/* _MOD_COMPAT_H */
206