Add kern.features.zfs
[zfs.git] / module / os / freebsd / zfs / kmod_core.c
blob00c1acf577101f0a245a24250ef642cc59830c4a
1 /*
2 * Copyright (c) 2020 iXsystems, Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/buf.h>
33 #include <sys/cmn_err.h>
34 #include <sys/conf.h>
35 #include <sys/dmu.h>
36 #include <sys/dmu_impl.h>
37 #include <sys/dmu_objset.h>
38 #include <sys/dmu_send.h>
39 #include <sys/dmu_tx.h>
40 #include <sys/dsl_bookmark.h>
41 #include <sys/dsl_crypt.h>
42 #include <sys/dsl_dataset.h>
43 #include <sys/dsl_deleg.h>
44 #include <sys/dsl_destroy.h>
45 #include <sys/dsl_dir.h>
46 #include <sys/dsl_prop.h>
47 #include <sys/dsl_scan.h>
48 #include <sys/dsl_userhold.h>
49 #include <sys/errno.h>
50 #include <sys/eventhandler.h>
51 #include <sys/file.h>
52 #include <sys/fm/util.h>
53 #include <sys/fs/zfs.h>
54 #include <sys/kernel.h>
55 #include <sys/kmem.h>
56 #include <sys/lock.h>
57 #include <sys/malloc.h>
58 #include <sys/mount.h>
59 #include <sys/mutex.h>
60 #include <sys/nvpair.h>
61 #include <sys/policy.h>
62 #include <sys/proc.h>
63 #include <sys/sdt.h>
64 #include <sys/spa.h>
65 #include <sys/spa_impl.h>
66 #include <sys/stat.h>
67 #include <sys/sunddi.h>
68 #include <sys/sysctl.h>
69 #include <sys/systm.h>
70 #include <sys/taskqueue.h>
71 #include <sys/uio.h>
72 #include <sys/vdev.h>
73 #include <sys/vdev_removal.h>
74 #include <sys/zap.h>
75 #include <sys/zcp.h>
76 #include <sys/zfeature.h>
77 #include <sys/zfs_context.h>
78 #include <sys/zfs_ctldir.h>
79 #include <sys/zfs_dir.h>
80 #include <sys/zfs_ioctl.h>
81 #include <sys/zfs_ioctl_compat.h>
82 #include <sys/zfs_ioctl_impl.h>
83 #include <sys/zfs_onexit.h>
84 #include <sys/zfs_vfsops.h>
85 #include <sys/zfs_znode.h>
86 #include <sys/zio_checksum.h>
87 #include <sys/zone.h>
88 #include <sys/zvol.h>
90 #include "zfs_comutil.h"
91 #include "zfs_deleg.h"
92 #include "zfs_namecheck.h"
93 #include "zfs_prop.h"
95 SYSCTL_DECL(_vfs_zfs);
96 SYSCTL_DECL(_vfs_zfs_vdev);
98 extern uint_t rrw_tsd_key;
99 static int zfs_version_ioctl = ZFS_IOCVER_OZFS;
100 SYSCTL_DECL(_vfs_zfs_version);
101 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, ioctl, CTLFLAG_RD, &zfs_version_ioctl,
102 0, "ZFS_IOCTL_VERSION");
104 static struct cdev *zfsdev;
106 static struct root_hold_token *zfs_root_token;
108 extern uint_t rrw_tsd_key;
109 extern uint_t zfs_allow_log_key;
110 extern uint_t zfs_geom_probe_vdev_key;
112 static int zfs__init(void);
113 static int zfs__fini(void);
114 static void zfs_shutdown(void *, int);
116 static eventhandler_tag zfs_shutdown_event_tag;
118 #define ZFS_MIN_KSTACK_PAGES 4
120 static int
121 zfsdev_ioctl(struct cdev *dev, ulong_t zcmd, caddr_t arg, int flag,
122 struct thread *td)
124 uint_t len;
125 int vecnum;
126 zfs_iocparm_t *zp;
127 zfs_cmd_t *zc;
128 #ifdef ZFS_LEGACY_SUPPORT
129 zfs_cmd_legacy_t *zcl;
130 #endif
131 int rc, error;
132 void *uaddr;
134 len = IOCPARM_LEN(zcmd);
135 vecnum = zcmd & 0xff;
136 zp = (void *)arg;
137 error = 0;
138 #ifdef ZFS_LEGACY_SUPPORT
139 zcl = NULL;
140 #endif
142 if (len != sizeof (zfs_iocparm_t))
143 return (EINVAL);
145 uaddr = (void *)(uintptr_t)zp->zfs_cmd;
146 zc = vmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
147 #ifdef ZFS_LEGACY_SUPPORT
149 * Remap ioctl code for legacy user binaries
151 if (zp->zfs_ioctl_version == ZFS_IOCVER_LEGACY) {
152 vecnum = zfs_ioctl_legacy_to_ozfs(vecnum);
153 if (vecnum < 0) {
154 vmem_free(zc, sizeof (zfs_cmd_t));
155 return (ENOTSUP);
157 zcl = vmem_zalloc(sizeof (zfs_cmd_legacy_t), KM_SLEEP);
158 if (copyin(uaddr, zcl, sizeof (zfs_cmd_legacy_t))) {
159 error = SET_ERROR(EFAULT);
160 goto out;
162 zfs_cmd_legacy_to_ozfs(zcl, zc);
163 } else
164 #endif
165 if (copyin(uaddr, zc, sizeof (zfs_cmd_t))) {
166 error = SET_ERROR(EFAULT);
167 goto out;
169 error = zfsdev_ioctl_common(vecnum, zc, 0);
170 #ifdef ZFS_LEGACY_SUPPORT
171 if (zcl) {
172 zfs_cmd_ozfs_to_legacy(zc, zcl);
173 rc = copyout(zcl, uaddr, sizeof (*zcl));
174 } else
175 #endif
177 rc = copyout(zc, uaddr, sizeof (*zc));
179 if (error == 0 && rc != 0)
180 error = SET_ERROR(EFAULT);
181 out:
182 #ifdef ZFS_LEGACY_SUPPORT
183 if (zcl)
184 vmem_free(zcl, sizeof (zfs_cmd_legacy_t));
185 #endif
186 vmem_free(zc, sizeof (zfs_cmd_t));
187 MPASS(tsd_get(rrw_tsd_key) == NULL);
188 return (error);
191 static void
192 zfsdev_close(void *data)
194 zfsdev_state_destroy(data);
197 void
198 zfsdev_private_set_state(void *priv __unused, zfsdev_state_t *zs)
200 devfs_set_cdevpriv(zs, zfsdev_close);
203 zfsdev_state_t *
204 zfsdev_private_get_state(void *priv)
206 return (priv);
209 static int
210 zfsdev_open(struct cdev *devp __unused, int flag __unused, int mode __unused,
211 struct thread *td __unused)
213 int error;
215 mutex_enter(&zfsdev_state_lock);
216 error = zfsdev_state_init(NULL);
217 mutex_exit(&zfsdev_state_lock);
219 return (error);
222 static struct cdevsw zfs_cdevsw = {
223 .d_version = D_VERSION,
224 .d_open = zfsdev_open,
225 .d_ioctl = zfsdev_ioctl,
226 .d_name = ZFS_DRIVER
230 zfsdev_attach(void)
232 struct make_dev_args args;
234 make_dev_args_init(&args);
235 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
236 args.mda_devsw = &zfs_cdevsw;
237 args.mda_cr = NULL;
238 args.mda_uid = UID_ROOT;
239 args.mda_gid = GID_OPERATOR;
240 args.mda_mode = 0666;
241 return (make_dev_s(&args, &zfsdev, ZFS_DRIVER));
244 void
245 zfsdev_detach(void)
247 if (zfsdev != NULL)
248 destroy_dev(zfsdev);
252 zfs__init(void)
254 int error;
256 #if KSTACK_PAGES < ZFS_MIN_KSTACK_PAGES
257 printf("ZFS NOTICE: KSTACK_PAGES is %d which could result in stack "
258 "overflow panic!\nPlease consider adding "
259 "'options KSTACK_PAGES=%d' to your kernel config\n", KSTACK_PAGES,
260 ZFS_MIN_KSTACK_PAGES);
261 #endif
262 zfs_root_token = root_mount_hold("ZFS");
263 if ((error = zfs_kmod_init()) != 0) {
264 printf("ZFS: Failed to Load ZFS Filesystem"
265 ", rc = %d\n", error);
266 root_mount_rel(zfs_root_token);
267 return (error);
271 tsd_create(&zfs_geom_probe_vdev_key, NULL);
273 printf("ZFS storage pool version: features support ("
274 SPA_VERSION_STRING ")\n");
275 root_mount_rel(zfs_root_token);
276 ddi_sysevent_init();
277 return (0);
281 zfs__fini(void)
283 if (zfs_busy() || zvol_busy() ||
284 zio_injection_enabled) {
285 return (EBUSY);
287 zfs_kmod_fini();
288 tsd_destroy(&zfs_geom_probe_vdev_key);
289 return (0);
292 static void
293 zfs_shutdown(void *arg __unused, int howto __unused)
297 * ZFS fini routines can not properly work in a panic-ed system.
299 if (panicstr == NULL)
300 zfs__fini();
303 static int
304 zfs_modevent(module_t mod, int type, void *unused __unused)
306 int err;
308 switch (type) {
309 case MOD_LOAD:
310 err = zfs__init();
311 if (err == 0)
312 zfs_shutdown_event_tag = EVENTHANDLER_REGISTER(
313 shutdown_post_sync, zfs_shutdown, NULL,
314 SHUTDOWN_PRI_FIRST);
315 return (err);
316 case MOD_UNLOAD:
317 err = zfs__fini();
318 if (err == 0 && zfs_shutdown_event_tag != NULL)
319 EVENTHANDLER_DEREGISTER(shutdown_post_sync,
320 zfs_shutdown_event_tag);
321 return (err);
322 case MOD_SHUTDOWN:
323 return (0);
324 default:
325 break;
327 return (EOPNOTSUPP);
330 static moduledata_t zfs_mod = {
331 "zfsctrl",
332 zfs_modevent,
336 #ifdef _KERNEL
337 EVENTHANDLER_DEFINE(mountroot, spa_boot_init, NULL, 0);
338 #endif
340 FEATURE(zfs, "OpenZFS support");
342 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_CLOCKS, SI_ORDER_ANY);
343 MODULE_VERSION(zfsctrl, 1);
344 #if __FreeBSD_version > 1300092
345 MODULE_DEPEND(zfsctrl, xdr, 1, 1, 1);
346 #else
347 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);
348 #endif
349 MODULE_DEPEND(zfsctrl, acl_nfs4, 1, 1, 1);
350 MODULE_DEPEND(zfsctrl, crypto, 1, 1, 1);
351 MODULE_DEPEND(zfsctrl, zlib, 1, 1, 1);