2 * Copyright (c) 2020 iXsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
28 #include <sys/param.h>
30 #include <sys/cmn_err.h>
33 #include <sys/dmu_impl.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dmu_send.h>
36 #include <sys/dmu_tx.h>
37 #include <sys/dsl_bookmark.h>
38 #include <sys/dsl_crypt.h>
39 #include <sys/dsl_dataset.h>
40 #include <sys/dsl_deleg.h>
41 #include <sys/dsl_destroy.h>
42 #include <sys/dsl_dir.h>
43 #include <sys/dsl_prop.h>
44 #include <sys/dsl_scan.h>
45 #include <sys/dsl_userhold.h>
46 #include <sys/errno.h>
47 #include <sys/eventhandler.h>
49 #include <sys/fm/util.h>
50 #include <sys/fs/zfs.h>
51 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/mutex.h>
57 #include <sys/nvpair.h>
58 #include <sys/policy.h>
62 #include <sys/spa_impl.h>
64 #include <sys/sunddi.h>
65 #include <sys/sysctl.h>
66 #include <sys/systm.h>
67 #include <sys/taskqueue.h>
70 #include <sys/vdev_removal.h>
73 #include <sys/zfeature.h>
74 #include <sys/zfs_context.h>
75 #include <sys/zfs_ctldir.h>
76 #include <sys/zfs_dir.h>
77 #include <sys/zfs_ioctl.h>
78 #include <sys/zfs_ioctl_compat.h>
79 #include <sys/zfs_ioctl_impl.h>
80 #include <sys/zfs_onexit.h>
81 #include <sys/zfs_vfsops.h>
82 #include <sys/zfs_znode.h>
83 #include <sys/zio_checksum.h>
87 #include "zfs_comutil.h"
88 #include "zfs_deleg.h"
89 #include "zfs_namecheck.h"
92 SYSCTL_DECL(_vfs_zfs
);
93 SYSCTL_DECL(_vfs_zfs_vdev
);
95 extern uint_t rrw_tsd_key
;
96 static int zfs_version_ioctl
= ZFS_IOCVER_OZFS
;
97 SYSCTL_DECL(_vfs_zfs_version
);
98 SYSCTL_INT(_vfs_zfs_version
, OID_AUTO
, ioctl
, CTLFLAG_RD
, &zfs_version_ioctl
,
99 0, "ZFS_IOCTL_VERSION");
101 static struct cdev
*zfsdev
;
103 static struct root_hold_token
*zfs_root_token
;
105 extern uint_t rrw_tsd_key
;
106 extern uint_t zfs_allow_log_key
;
107 extern uint_t zfs_geom_probe_vdev_key
;
109 static int zfs__init(void);
110 static int zfs__fini(void);
111 static void zfs_shutdown(void *, int);
113 static eventhandler_tag zfs_shutdown_event_tag
;
114 static eventhandler_tag zfs_mountroot_event_tag
;
116 #define ZFS_MIN_KSTACK_PAGES 4
119 zfsdev_ioctl(struct cdev
*dev
, ulong_t zcmd
, caddr_t arg
, int flag
,
126 #ifdef ZFS_LEGACY_SUPPORT
127 zfs_cmd_legacy_t
*zcl
;
132 len
= IOCPARM_LEN(zcmd
);
133 vecnum
= zcmd
& 0xff;
136 #ifdef ZFS_LEGACY_SUPPORT
140 if (len
!= sizeof (zfs_iocparm_t
))
143 uaddr
= (void *)(uintptr_t)zp
->zfs_cmd
;
144 zc
= vmem_zalloc(sizeof (zfs_cmd_t
), KM_SLEEP
);
145 #ifdef ZFS_LEGACY_SUPPORT
147 * Remap ioctl code for legacy user binaries
149 if (zp
->zfs_ioctl_version
== ZFS_IOCVER_LEGACY
) {
150 vecnum
= zfs_ioctl_legacy_to_ozfs(vecnum
);
152 vmem_free(zc
, sizeof (zfs_cmd_t
));
155 zcl
= vmem_zalloc(sizeof (zfs_cmd_legacy_t
), KM_SLEEP
);
156 if (copyin(uaddr
, zcl
, sizeof (zfs_cmd_legacy_t
))) {
157 error
= SET_ERROR(EFAULT
);
160 zfs_cmd_legacy_to_ozfs(zcl
, zc
);
163 if (copyin(uaddr
, zc
, sizeof (zfs_cmd_t
))) {
164 error
= SET_ERROR(EFAULT
);
167 error
= zfsdev_ioctl_common(vecnum
, zc
, 0);
168 #ifdef ZFS_LEGACY_SUPPORT
170 zfs_cmd_ozfs_to_legacy(zc
, zcl
);
171 rc
= copyout(zcl
, uaddr
, sizeof (*zcl
));
175 rc
= copyout(zc
, uaddr
, sizeof (*zc
));
177 if (error
== 0 && rc
!= 0)
178 error
= SET_ERROR(EFAULT
);
180 #ifdef ZFS_LEGACY_SUPPORT
182 vmem_free(zcl
, sizeof (zfs_cmd_legacy_t
));
184 vmem_free(zc
, sizeof (zfs_cmd_t
));
185 MPASS(tsd_get(rrw_tsd_key
) == NULL
);
190 zfsdev_close(void *data
)
192 zfsdev_state_destroy(data
);
196 zfsdev_private_set_state(void *priv __unused
, zfsdev_state_t
*zs
)
198 devfs_set_cdevpriv(zs
, zfsdev_close
);
202 zfsdev_private_get_state(void *priv
)
208 zfsdev_open(struct cdev
*devp __unused
, int flag __unused
, int mode __unused
,
209 struct thread
*td __unused
)
213 mutex_enter(&zfsdev_state_lock
);
214 error
= zfsdev_state_init(NULL
);
215 mutex_exit(&zfsdev_state_lock
);
220 static struct cdevsw zfs_cdevsw
= {
221 .d_version
= D_VERSION
,
222 .d_open
= zfsdev_open
,
223 .d_ioctl
= zfsdev_ioctl
,
230 struct make_dev_args args
;
232 make_dev_args_init(&args
);
233 args
.mda_flags
= MAKEDEV_CHECKNAME
| MAKEDEV_WAITOK
;
234 args
.mda_devsw
= &zfs_cdevsw
;
236 args
.mda_uid
= UID_ROOT
;
237 args
.mda_gid
= GID_OPERATOR
;
238 args
.mda_mode
= 0666;
239 return (make_dev_s(&args
, &zfsdev
, ZFS_DRIVER
));
254 #if KSTACK_PAGES < ZFS_MIN_KSTACK_PAGES
255 printf("ZFS NOTICE: KSTACK_PAGES is %d which could result in stack "
256 "overflow panic!\nPlease consider adding "
257 "'options KSTACK_PAGES=%d' to your kernel config\n", KSTACK_PAGES
,
258 ZFS_MIN_KSTACK_PAGES
);
260 zfs_root_token
= root_mount_hold("ZFS");
261 if ((error
= zfs_kmod_init()) != 0) {
262 printf("ZFS: Failed to Load ZFS Filesystem"
263 ", rc = %d\n", error
);
264 root_mount_rel(zfs_root_token
);
269 tsd_create(&zfs_geom_probe_vdev_key
, NULL
);
271 printf("ZFS storage pool version: features support ("
272 SPA_VERSION_STRING
")\n");
273 root_mount_rel(zfs_root_token
);
281 if (zfs_busy() || zvol_busy() ||
282 zio_injection_enabled
) {
286 tsd_destroy(&zfs_geom_probe_vdev_key
);
291 zfs_shutdown(void *arg __unused
, int howto __unused
)
295 * ZFS fini routines can not properly work in a panic-ed system.
297 if (panicstr
== NULL
)
302 zfs_modevent(module_t mod
, int type
, void *unused __unused
)
310 zfs_shutdown_event_tag
= EVENTHANDLER_REGISTER(
311 shutdown_post_sync
, zfs_shutdown
, NULL
,
313 zfs_mountroot_event_tag
= EVENTHANDLER_REGISTER(
314 mountroot
, spa_boot_init
, NULL
,
321 if (zfs_shutdown_event_tag
!= NULL
)
322 EVENTHANDLER_DEREGISTER(shutdown_post_sync
,
323 zfs_shutdown_event_tag
);
324 if (zfs_mountroot_event_tag
!= NULL
)
325 EVENTHANDLER_DEREGISTER(mountroot
,
326 zfs_mountroot_event_tag
);
337 static moduledata_t zfs_mod
= {
344 FEATURE(zfs
, "OpenZFS support");
346 DECLARE_MODULE(zfsctrl
, zfs_mod
, SI_SUB_CLOCKS
, SI_ORDER_ANY
);
347 MODULE_VERSION(zfsctrl
, 1);
348 MODULE_DEPEND(zfsctrl
, xdr
, 1, 1, 1);
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);