xref: /freebsd-13-stable/sys/contrib/openzfs/include/os/linux/kernel/linux/mod_compat.h (revision 209ebfa26ec4f79a8322400cffdff1c3f3248c7d)
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 http://www.opensolaris.org/os/licensing.
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 /* BEGIN CSTYLED */
48 #define	INT int
49 #define	UINT uint
50 #define	ULONG ulong
51 #define	LONG long
52 #define	STRING charp
53 /* END CSTYLED */
54 
55 enum scope_prefix_types {
56 	zfs,
57 	zfs_arc,
58 	zfs_condense,
59 	zfs_dbuf,
60 	zfs_dbuf_cache,
61 	zfs_deadman,
62 	zfs_dedup,
63 	zfs_l2arc,
64 	zfs_livelist,
65 	zfs_livelist_condense,
66 	zfs_lua,
67 	zfs_metaslab,
68 	zfs_mg,
69 	zfs_multihost,
70 	zfs_prefetch,
71 	zfs_reconstruct,
72 	zfs_recv,
73 	zfs_send,
74 	zfs_spa,
75 	zfs_trim,
76 	zfs_txg,
77 	zfs_vdev,
78 	zfs_vdev_cache,
79 	zfs_vdev_file,
80 	zfs_vdev_mirror,
81 	zfs_vnops,
82 	zfs_zevent,
83 	zfs_zio,
84 	zfs_zil
85 };
86 
87 /*
88  * Declare a module parameter / sysctl node
89  *
90  * "scope_prefix" the part of the sysctl / sysfs tree the node resides under
91  *   (currently a no-op on Linux)
92  * "name_prefix" the part of the variable name that will be excluded from the
93  *   exported names on platforms with a hierarchical namespace
94  * "name" the part of the variable that will be exposed on platforms with a
95  *    hierarchical namespace, or as name_prefix ## name on Linux
96  * "type" the variable type
97  * "perm" the permissions (read/write or read only)
98  * "desc" a brief description of the option
99  *
100  * Examples:
101  * ZFS_MODULE_PARAM(zfs_vdev_mirror, zfs_vdev_mirror_, rotating_inc, UINT,
102  * 	ZMOD_RW, "Rotating media load increment for non-seeking I/O's");
103  * on FreeBSD:
104  *   vfs.zfs.vdev.mirror.rotating_inc
105  * on Linux:
106  *   zfs_vdev_mirror_rotating_inc
107  *
108  * ZFS_MODULE_PARAM(zfs, , dmu_prefetch_max, UINT, ZMOD_RW,
109  * 	"Limit one prefetch call to this size");
110  * on FreeBSD:
111  *   vfs.zfs.dmu_prefetch_max
112  * on Linux:
113  *   dmu_prefetch_max
114  */
115 /* BEGIN CSTYLED */
116 #define	ZFS_MODULE_PARAM(scope_prefix, name_prefix, name, type, perm, desc) \
117 	CTASSERT_GLOBAL((sizeof (scope_prefix) == sizeof (enum scope_prefix_types))); \
118 	module_param(name_prefix ## name, type, perm); \
119 	MODULE_PARM_DESC(name_prefix ## name, desc)
120 /* END CSTYLED */
121 
122 /*
123  * Declare a module parameter / sysctl node
124  *
125  * "scope_prefix" the part of the the sysctl / sysfs tree the node resides under
126  *   (currently a no-op on Linux)
127  * "name_prefix" the part of the variable name that will be excluded from the
128  *   exported names on platforms with a hierarchical namespace
129  * "name" the part of the variable that will be exposed on platforms with a
130  *    hierarchical namespace, or as name_prefix ## name on Linux
131  * "setfunc" setter function
132  * "getfunc" getter function
133  * "perm" the permissions (read/write or read only)
134  * "desc" a brief description of the option
135  *
136  * Examples:
137  * ZFS_MODULE_PARAM_CALL(zfs_spa, spa_, slop_shift, param_set_slop_shift,
138  * 	param_get_int, ZMOD_RW, "Reserved free space in pool");
139  * on FreeBSD:
140  *   vfs.zfs.spa_slop_shift
141  * on Linux:
142  *   spa_slop_shift
143  */
144 /* BEGIN CSTYLED */
145 #define	ZFS_MODULE_PARAM_CALL(scope_prefix, name_prefix, name, setfunc, getfunc, perm, desc) \
146 	CTASSERT_GLOBAL((sizeof (scope_prefix) == sizeof (enum scope_prefix_types))); \
147 	module_param_call(name_prefix ## name, setfunc, getfunc, &name_prefix ## name, perm); \
148 	MODULE_PARM_DESC(name_prefix ## name, desc)
149 /* END CSTYLED */
150 
151 /*
152  * As above, but there is no variable with the name name_prefix ## name,
153  * so NULL is passed to module_param_call instead.
154  */
155 /* BEGIN CSTYLED */
156 #define	ZFS_MODULE_VIRTUAL_PARAM_CALL(scope_prefix, name_prefix, name, setfunc, getfunc, perm, desc) \
157 	CTASSERT_GLOBAL((sizeof (scope_prefix) == sizeof (enum scope_prefix_types))); \
158 	module_param_call(name_prefix ## name, setfunc, getfunc, NULL, perm); \
159 	MODULE_PARM_DESC(name_prefix ## name, desc)
160 /* END CSTYLED */
161 
162 #define	ZFS_MODULE_PARAM_ARGS	const char *buf, zfs_kernel_param_t *kp
163 
164 #define	ZFS_MODULE_DESCRIPTION(s) MODULE_DESCRIPTION(s)
165 #define	ZFS_MODULE_AUTHOR(s) MODULE_AUTHOR(s)
166 #define	ZFS_MODULE_LICENSE(s) MODULE_LICENSE(s)
167 #define	ZFS_MODULE_VERSION(s) MODULE_VERSION(s)
168 
169 #define	module_init_early(fn) module_init(fn)
170 
171 #endif	/* _MOD_COMPAT_H */
172