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/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/cmn_err.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>
52 #include <sys/fm/util.h>
53 #include <sys/fs/zfs.h>
54 #include <sys/kernel.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>
65 #include <sys/spa_impl.h>
67 #include <sys/sunddi.h>
68 #include <sys/sysctl.h>
69 #include <sys/systm.h>
70 #include <sys/taskqueue.h>
73 #include <sys/vdev_removal.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>
90 #include "zfs_comutil.h"
91 #include "zfs_deleg.h"
92 #include "zfs_namecheck.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
121 zfsdev_ioctl(struct cdev
*dev
, ulong_t zcmd
, caddr_t arg
, int flag
,
128 #ifdef ZFS_LEGACY_SUPPORT
129 zfs_cmd_legacy_t
*zcl
;
134 len
= IOCPARM_LEN(zcmd
);
135 vecnum
= zcmd
& 0xff;
138 #ifdef ZFS_LEGACY_SUPPORT
142 if (len
!= sizeof (zfs_iocparm_t
))
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
);
154 vmem_free(zc
, sizeof (zfs_cmd_t
));
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
);
162 zfs_cmd_legacy_to_ozfs(zcl
, zc
);
165 if (copyin(uaddr
, zc
, sizeof (zfs_cmd_t
))) {
166 error
= SET_ERROR(EFAULT
);
169 error
= zfsdev_ioctl_common(vecnum
, zc
, 0);
170 #ifdef ZFS_LEGACY_SUPPORT
172 zfs_cmd_ozfs_to_legacy(zc
, zcl
);
173 rc
= copyout(zcl
, uaddr
, sizeof (*zcl
));
177 rc
= copyout(zc
, uaddr
, sizeof (*zc
));
179 if (error
== 0 && rc
!= 0)
180 error
= SET_ERROR(EFAULT
);
182 #ifdef ZFS_LEGACY_SUPPORT
184 vmem_free(zcl
, sizeof (zfs_cmd_legacy_t
));
186 vmem_free(zc
, sizeof (zfs_cmd_t
));
187 MPASS(tsd_get(rrw_tsd_key
) == NULL
);
192 zfsdev_close(void *data
)
194 zfsdev_state_destroy(data
);
198 zfsdev_private_set_state(void *priv __unused
, zfsdev_state_t
*zs
)
200 devfs_set_cdevpriv(zs
, zfsdev_close
);
204 zfsdev_private_get_state(void *priv
)
210 zfsdev_open(struct cdev
*devp __unused
, int flag __unused
, int mode __unused
,
211 struct thread
*td __unused
)
215 mutex_enter(&zfsdev_state_lock
);
216 error
= zfsdev_state_init(NULL
);
217 mutex_exit(&zfsdev_state_lock
);
222 static struct cdevsw zfs_cdevsw
= {
223 .d_version
= D_VERSION
,
224 .d_open
= zfsdev_open
,
225 .d_ioctl
= zfsdev_ioctl
,
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
;
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
));
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
);
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
);
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
);
283 if (zfs_busy() || zvol_busy() ||
284 zio_injection_enabled
) {
288 tsd_destroy(&zfs_geom_probe_vdev_key
);
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
)
304 zfs_modevent(module_t mod
, int type
, void *unused __unused
)
312 zfs_shutdown_event_tag
= EVENTHANDLER_REGISTER(
313 shutdown_post_sync
, zfs_shutdown
, NULL
,
318 if (err
== 0 && zfs_shutdown_event_tag
!= NULL
)
319 EVENTHANDLER_DEREGISTER(shutdown_post_sync
,
320 zfs_shutdown_event_tag
);
330 static moduledata_t zfs_mod
= {
337 EVENTHANDLER_DEFINE(mountroot
, spa_boot_init
, NULL
, 0);
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);
347 MODULE_DEPEND(zfsctrl
, krpc
, 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);