flush: only detect lack of flush support in one place
[zfs.git] / module / os / freebsd / zfs / kmod_core.c
blobf883599f2dad2d11d7d6d1458d76e12a7e359844
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/param.h>
29 #include <sys/buf.h>
30 #include <sys/cmn_err.h>
31 #include <sys/conf.h>
32 #include <sys/dmu.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>
48 #include <sys/file.h>
49 #include <sys/fm/util.h>
50 #include <sys/fs/zfs.h>
51 #include <sys/kernel.h>
52 #include <sys/kmem.h>
53 #include <sys/lock.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>
59 #include <sys/proc.h>
60 #include <sys/sdt.h>
61 #include <sys/spa.h>
62 #include <sys/spa_impl.h>
63 #include <sys/stat.h>
64 #include <sys/sunddi.h>
65 #include <sys/sysctl.h>
66 #include <sys/systm.h>
67 #include <sys/taskqueue.h>
68 #include <sys/uio.h>
69 #include <sys/vdev.h>
70 #include <sys/vdev_removal.h>
71 #include <sys/zap.h>
72 #include <sys/zcp.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>
84 #include <sys/zone.h>
85 #include <sys/zvol.h>
87 #include "zfs_comutil.h"
88 #include "zfs_deleg.h"
89 #include "zfs_namecheck.h"
90 #include "zfs_prop.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
118 static int
119 zfsdev_ioctl(struct cdev *dev, ulong_t zcmd, caddr_t arg, int flag,
120 struct thread *td)
122 uint_t len;
123 int vecnum;
124 zfs_iocparm_t *zp;
125 zfs_cmd_t *zc;
126 #ifdef ZFS_LEGACY_SUPPORT
127 zfs_cmd_legacy_t *zcl;
128 #endif
129 int rc, error;
130 void *uaddr;
132 len = IOCPARM_LEN(zcmd);
133 vecnum = zcmd & 0xff;
134 zp = (void *)arg;
135 error = 0;
136 #ifdef ZFS_LEGACY_SUPPORT
137 zcl = NULL;
138 #endif
140 if (len != sizeof (zfs_iocparm_t))
141 return (EINVAL);
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);
151 if (vecnum < 0) {
152 vmem_free(zc, sizeof (zfs_cmd_t));
153 return (ENOTSUP);
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);
158 goto out;
160 zfs_cmd_legacy_to_ozfs(zcl, zc);
161 } else
162 #endif
163 if (copyin(uaddr, zc, sizeof (zfs_cmd_t))) {
164 error = SET_ERROR(EFAULT);
165 goto out;
167 error = zfsdev_ioctl_common(vecnum, zc, 0);
168 #ifdef ZFS_LEGACY_SUPPORT
169 if (zcl) {
170 zfs_cmd_ozfs_to_legacy(zc, zcl);
171 rc = copyout(zcl, uaddr, sizeof (*zcl));
172 } else
173 #endif
175 rc = copyout(zc, uaddr, sizeof (*zc));
177 if (error == 0 && rc != 0)
178 error = SET_ERROR(EFAULT);
179 out:
180 #ifdef ZFS_LEGACY_SUPPORT
181 if (zcl)
182 vmem_free(zcl, sizeof (zfs_cmd_legacy_t));
183 #endif
184 vmem_free(zc, sizeof (zfs_cmd_t));
185 MPASS(tsd_get(rrw_tsd_key) == NULL);
186 return (error);
189 static void
190 zfsdev_close(void *data)
192 zfsdev_state_destroy(data);
195 void
196 zfsdev_private_set_state(void *priv __unused, zfsdev_state_t *zs)
198 devfs_set_cdevpriv(zs, zfsdev_close);
201 zfsdev_state_t *
202 zfsdev_private_get_state(void *priv)
204 return (priv);
207 static int
208 zfsdev_open(struct cdev *devp __unused, int flag __unused, int mode __unused,
209 struct thread *td __unused)
211 int error;
213 mutex_enter(&zfsdev_state_lock);
214 error = zfsdev_state_init(NULL);
215 mutex_exit(&zfsdev_state_lock);
217 return (error);
220 static struct cdevsw zfs_cdevsw = {
221 .d_version = D_VERSION,
222 .d_open = zfsdev_open,
223 .d_ioctl = zfsdev_ioctl,
224 .d_name = ZFS_DRIVER
228 zfsdev_attach(void)
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;
235 args.mda_cr = NULL;
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));
242 void
243 zfsdev_detach(void)
245 if (zfsdev != NULL)
246 destroy_dev(zfsdev);
250 zfs__init(void)
252 int error;
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);
259 #endif
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);
265 return (error);
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);
274 ddi_sysevent_init();
275 return (0);
279 zfs__fini(void)
281 if (zfs_busy() || zvol_busy() ||
282 zio_injection_enabled) {
283 return (EBUSY);
285 zfs_kmod_fini();
286 tsd_destroy(&zfs_geom_probe_vdev_key);
287 return (0);
290 static void
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)
298 zfs__fini();
301 static int
302 zfs_modevent(module_t mod, int type, void *unused __unused)
304 int err;
306 switch (type) {
307 case MOD_LOAD:
308 err = zfs__init();
309 if (err == 0) {
310 zfs_shutdown_event_tag = EVENTHANDLER_REGISTER(
311 shutdown_post_sync, zfs_shutdown, NULL,
312 SHUTDOWN_PRI_FIRST);
313 zfs_mountroot_event_tag = EVENTHANDLER_REGISTER(
314 mountroot, spa_boot_init, NULL,
315 SI_ORDER_ANY);
317 return (err);
318 case MOD_UNLOAD:
319 err = zfs__fini();
320 if (err == 0) {
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);
328 return (err);
329 case MOD_SHUTDOWN:
330 return (0);
331 default:
332 break;
334 return (EOPNOTSUPP);
337 static moduledata_t zfs_mod = {
338 "zfsctrl",
339 zfs_modevent,
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);