]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - module/os/freebsd/zfs/kmod_core.c
Vendor import of openzfs master @ 184df27eef0abdc7ab2105b21257f753834b936b
[FreeBSD/FreeBSD.git] / module / os / freebsd / zfs / kmod_core.c
1 /*
2  * Copyright (c) 2020 iXsystems, Inc.
3  * All rights reserved.
4  *
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.
13  *
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.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/conf.h>
35 #include <sys/eventhandler.h>
36 #include <sys/kernel.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/errno.h>
42 #include <sys/uio.h>
43 #include <sys/buf.h>
44 #include <sys/file.h>
45 #include <sys/kmem.h>
46 #include <sys/conf.h>
47 #include <sys/eventhandler.h>
48 #include <sys/cmn_err.h>
49 #include <sys/stat.h>
50 #include <sys/zfs_ioctl.h>
51 #include <sys/zfs_vfsops.h>
52 #include <sys/zfs_znode.h>
53 #include <sys/zap.h>
54 #include <sys/spa.h>
55 #include <sys/spa_impl.h>
56 #include <sys/vdev.h>
57 #include <sys/dmu.h>
58 #include <sys/dsl_dir.h>
59 #include <sys/dsl_dataset.h>
60 #include <sys/dsl_prop.h>
61 #include <sys/dsl_deleg.h>
62 #include <sys/dmu_objset.h>
63 #include <sys/dmu_impl.h>
64 #include <sys/dmu_tx.h>
65 #include <sys/fm/util.h>
66 #include <sys/sunddi.h>
67 #include <sys/policy.h>
68 #include <sys/zone.h>
69 #include <sys/nvpair.h>
70 #include <sys/mount.h>
71 #include <sys/taskqueue.h>
72 #include <sys/sdt.h>
73 #include <sys/fs/zfs.h>
74 #include <sys/zfs_ctldir.h>
75 #include <sys/zfs_dir.h>
76 #include <sys/zfs_onexit.h>
77 #include <sys/zvol.h>
78 #include <sys/dsl_scan.h>
79 #include <sys/dmu_objset.h>
80 #include <sys/dmu_send.h>
81 #include <sys/dsl_destroy.h>
82 #include <sys/dsl_bookmark.h>
83 #include <sys/dsl_userhold.h>
84 #include <sys/zfeature.h>
85 #include <sys/zcp.h>
86 #include <sys/zio_checksum.h>
87 #include <sys/vdev_removal.h>
88 #include <sys/dsl_crypt.h>
89
90 #include <sys/zfs_ioctl_compat.h>
91 #include <sys/zfs_ioctl_impl.h>
92
93 #include "zfs_namecheck.h"
94 #include "zfs_prop.h"
95 #include "zfs_deleg.h"
96 #include "zfs_comutil.h"
97
98 SYSCTL_DECL(_vfs_zfs);
99 SYSCTL_DECL(_vfs_zfs_vdev);
100
101
102 static int zfs_version_ioctl = ZFS_IOCVER_OZFS;
103 SYSCTL_DECL(_vfs_zfs_version);
104 SYSCTL_INT(_vfs_zfs_version, OID_AUTO, ioctl, CTLFLAG_RD, &zfs_version_ioctl,
105     0, "ZFS_IOCTL_VERSION");
106
107 static struct cdev *zfsdev;
108
109 static struct root_hold_token *zfs_root_token;
110
111 extern uint_t rrw_tsd_key;
112 extern uint_t zfs_allow_log_key;
113 extern uint_t zfs_geom_probe_vdev_key;
114
115 static int zfs__init(void);
116 static int zfs__fini(void);
117 static void zfs_shutdown(void *, int);
118
119 static eventhandler_tag zfs_shutdown_event_tag;
120 extern zfsdev_state_t *zfsdev_state_list;
121
122 #define ZFS_MIN_KSTACK_PAGES 4
123
124
125 static int
126 zfsdev_ioctl(struct cdev *dev, ulong_t zcmd, caddr_t arg, int flag,
127     struct thread *td)
128 {
129         uint_t len;
130         int vecnum;
131         zfs_iocparm_t *zp;
132         zfs_cmd_t *zc;
133         zfs_cmd_legacy_t *zcl;
134         int rc, error;
135         void *uaddr;
136
137         len = IOCPARM_LEN(zcmd);
138         vecnum = zcmd & 0xff;
139         zp = (void *)arg;
140         uaddr = (void *)zp->zfs_cmd;
141         error = 0;
142         zcl = NULL;
143
144         if (len != sizeof (zfs_iocparm_t)) {
145                 printf("len %d vecnum: %d sizeof (zfs_cmd_t) %ju\n",
146                     len, vecnum, (uintmax_t)sizeof (zfs_cmd_t));
147                 return (EINVAL);
148         }
149
150         zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP);
151         /*
152          * Remap ioctl code for legacy user binaries
153          */
154         if (zp->zfs_ioctl_version == ZFS_IOCVER_LEGACY) {
155                 vecnum = zfs_ioctl_legacy_to_ozfs(vecnum);
156                 if (vecnum < 0) {
157                         kmem_free(zc, sizeof (zfs_cmd_t));
158                         return (ENOTSUP);
159                 }
160                 zcl = kmem_zalloc(sizeof (zfs_cmd_legacy_t), KM_SLEEP);
161                 if (copyin(uaddr, zcl, sizeof (zfs_cmd_legacy_t))) {
162                         error = SET_ERROR(EFAULT);
163                         goto out;
164                 }
165                 zfs_cmd_legacy_to_ozfs(zcl, zc);
166         } else if (copyin(uaddr, zc, sizeof (zfs_cmd_t))) {
167                 error = SET_ERROR(EFAULT);
168                 goto out;
169         }
170         error = zfsdev_ioctl_common(vecnum, zc, 0);
171         if (zcl) {
172                 zfs_cmd_ozfs_to_legacy(zc, zcl);
173                 rc = copyout(zcl, uaddr, sizeof (*zcl));
174         } else {
175                 rc = copyout(zc, uaddr, sizeof (*zc));
176         }
177         if (error == 0 && rc != 0)
178                 error = SET_ERROR(EFAULT);
179 out:
180         if (zcl)
181                 kmem_free(zcl, sizeof (zfs_cmd_legacy_t));
182         kmem_free(zc, sizeof (zfs_cmd_t));
183         return (error);
184 }
185
186 static void
187 zfsdev_close(void *data)
188 {
189         zfsdev_state_t *zs, *zsp = data;
190
191         mutex_enter(&zfsdev_state_lock);
192         for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
193                 if (zs == zsp)
194                         break;
195         }
196         if (zs == NULL || zs->zs_minor <= 0) {
197                 mutex_exit(&zfsdev_state_lock);
198                 return;
199         }
200         zs->zs_minor = -1;
201         zfs_onexit_destroy(zs->zs_onexit);
202         zfs_zevent_destroy(zs->zs_zevent);
203         mutex_exit(&zfsdev_state_lock);
204         zs->zs_onexit = NULL;
205         zs->zs_zevent = NULL;
206 }
207
208 static int
209 zfs_ctldev_init(struct cdev *devp)
210 {
211         boolean_t newzs = B_FALSE;
212         minor_t minor;
213         zfsdev_state_t *zs, *zsprev = NULL;
214
215         ASSERT(MUTEX_HELD(&zfsdev_state_lock));
216
217         minor = zfsdev_minor_alloc();
218         if (minor == 0)
219                 return (SET_ERROR(ENXIO));
220
221         for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) {
222                 if (zs->zs_minor == -1)
223                         break;
224                 zsprev = zs;
225         }
226
227         if (!zs) {
228                 zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP);
229                 newzs = B_TRUE;
230         }
231
232         devfs_set_cdevpriv(zs, zfsdev_close);
233
234         zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit);
235         zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent);
236
237         if (newzs) {
238                 zs->zs_minor = minor;
239                 wmb();
240                 zsprev->zs_next = zs;
241         } else {
242                 wmb();
243                 zs->zs_minor = minor;
244         }
245         return (0);
246 }
247
248 static int
249 zfsdev_open(struct cdev *devp, int flag, int mode, struct thread *td)
250 {
251         int error;
252
253         mutex_enter(&zfsdev_state_lock);
254         error = zfs_ctldev_init(devp);
255         mutex_exit(&zfsdev_state_lock);
256
257         return (error);
258 }
259
260 static struct cdevsw zfs_cdevsw = {
261         .d_version =    D_VERSION,
262         .d_open =       zfsdev_open,
263         .d_ioctl =      zfsdev_ioctl,
264         .d_name =       ZFS_DRIVER
265 };
266
267 int
268 zfsdev_attach(void)
269 {
270         zfsdev = make_dev(&zfs_cdevsw, 0x0, UID_ROOT, GID_OPERATOR, 0666,
271             ZFS_DRIVER);
272         return (0);
273 }
274
275 void
276 zfsdev_detach(void)
277 {
278         if (zfsdev != NULL)
279                 destroy_dev(zfsdev);
280 }
281
282 int
283 zfs__init(void)
284 {
285         int error;
286
287 #if KSTACK_PAGES < ZFS_MIN_KSTACK_PAGES
288         printf("ZFS NOTICE: KSTACK_PAGES is %d which could result in stack "
289             "overflow panic!\nPlease consider adding "
290             "'options KSTACK_PAGES=%d' to your kernel config\n", KSTACK_PAGES,
291             ZFS_MIN_KSTACK_PAGES);
292 #endif
293         zfs_root_token = root_mount_hold("ZFS");
294         if ((error = zfs_kmod_init()) != 0) {
295                 printf("ZFS: Failed to Load ZFS Filesystem"
296                     ", rc = %d\n", error);
297                 root_mount_rel(zfs_root_token);
298                 return (error);
299         }
300
301
302         tsd_create(&zfs_geom_probe_vdev_key, NULL);
303
304         printf("ZFS storage pool version: features support ("
305             SPA_VERSION_STRING ")\n");
306         root_mount_rel(zfs_root_token);
307         ddi_sysevent_init();
308         return (0);
309 }
310
311 int
312 zfs__fini(void)
313 {
314         if (zfs_busy() || zvol_busy() ||
315             zio_injection_enabled) {
316                 return (EBUSY);
317         }
318         zfs_kmod_fini();
319         tsd_destroy(&zfs_geom_probe_vdev_key);
320         return (0);
321 }
322
323 static void
324 zfs_shutdown(void *arg __unused, int howto __unused)
325 {
326
327         /*
328          * ZFS fini routines can not properly work in a panic-ed system.
329          */
330         if (panicstr == NULL)
331                 zfs__fini();
332 }
333
334
335 static int
336 zfs_modevent(module_t mod, int type, void *unused __unused)
337 {
338         int err;
339
340         switch (type) {
341         case MOD_LOAD:
342                 err = zfs__init();
343                 if (err == 0)
344                         zfs_shutdown_event_tag = EVENTHANDLER_REGISTER(
345                             shutdown_post_sync, zfs_shutdown, NULL,
346                             SHUTDOWN_PRI_FIRST);
347                 return (err);
348         case MOD_UNLOAD:
349                 err = zfs__fini();
350                 if (err == 0 && zfs_shutdown_event_tag != NULL)
351                         EVENTHANDLER_DEREGISTER(shutdown_post_sync,
352                             zfs_shutdown_event_tag);
353                 return (err);
354         case MOD_SHUTDOWN:
355                 return (0);
356         default:
357                 break;
358         }
359         return (EOPNOTSUPP);
360 }
361
362 static moduledata_t zfs_mod = {
363         "zfsctrl",
364         zfs_modevent,
365         0
366 };
367
368 #ifdef _KERNEL
369 EVENTHANDLER_DEFINE(mountroot, spa_boot_init, NULL, 0);
370 #endif
371
372 DECLARE_MODULE(zfsctrl, zfs_mod, SI_SUB_CLOCKS, SI_ORDER_ANY);
373 MODULE_VERSION(zfsctrl, 1);
374 #if __FreeBSD_version > 1300092
375 MODULE_DEPEND(zfsctrl, xdr, 1, 1, 1);
376 #else
377 MODULE_DEPEND(zfsctrl, krpc, 1, 1, 1);
378 #endif
379 MODULE_DEPEND(zfsctrl, acl_nfs4, 1, 1, 1);
380 MODULE_DEPEND(zfsctrl, crypto, 1, 1, 1);
381 MODULE_DEPEND(zfsctrl, cryptodev, 1, 1, 1);