xref: /freebsd/sys/geom/geom_dev.c (revision 8ea2d22e6d54d492b6b169014841eb27d4617459)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2002 Poul-Henning Kamp
5  * Copyright (c) 2002 Networks Associates Technology, Inc.
6  * All rights reserved.
7  *
8  * This software was developed for the FreeBSD Project by Poul-Henning Kamp
9  * and NAI Labs, the Security Research Division of Network Associates, Inc.
10  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
11  * DARPA CHATS research program.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. The names of the authors may not be used to endorse or promote
22  *    products derived from this software without specific prior written
23  *    permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
45 #include <sys/conf.h>
46 #include <sys/ctype.h>
47 #include <sys/bio.h>
48 #include <sys/devctl.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/proc.h>
52 #include <sys/errno.h>
53 #include <sys/time.h>
54 #include <sys/disk.h>
55 #include <sys/fcntl.h>
56 #include <sys/limits.h>
57 #include <sys/selinfo.h>
58 #include <sys/sysctl.h>
59 #include <geom/geom.h>
60 #include <geom/geom_int.h>
61 #include <machine/stdarg.h>
62 
63 struct g_dev_softc {
64 	struct mtx	 sc_mtx;
65 	struct cdev	*sc_dev;
66 	struct cdev	*sc_alias;
67 	int		 sc_open;
68 	u_int		 sc_active;
69 	struct selinfo	 sc_selinfo;
70 #define	SC_A_DESTROY	(1 << 31)
71 #define	SC_A_OPEN	(1 << 30)
72 #define	SC_A_ACTIVE	(SC_A_OPEN - 1)
73 };
74 
75 static d_open_t		g_dev_open;
76 static d_close_t	g_dev_close;
77 static d_strategy_t	g_dev_strategy;
78 static d_ioctl_t	g_dev_ioctl;
79 static d_kqfilter_t	g_dev_kqfilter;
80 
81 static void		gdev_filter_detach(struct knote *kn);
82 static int		gdev_filter_vnode(struct knote *kn, long hint);
83 
84 static struct filterops gdev_filterops_vnode = {
85 	.f_isfd = 1,
86 	.f_detach = gdev_filter_detach,
87 	.f_event = gdev_filter_vnode,
88 };
89 
90 static struct cdevsw g_dev_cdevsw = {
91 	.d_version =	D_VERSION,
92 	.d_open =	g_dev_open,
93 	.d_close =	g_dev_close,
94 	.d_read =	physread,
95 	.d_write =	physwrite,
96 	.d_ioctl =	g_dev_ioctl,
97 	.d_strategy =	g_dev_strategy,
98 	.d_name =	"g_dev",
99 	.d_flags =	D_DISK | D_TRACKCLOSE,
100 	.d_kqfilter =	g_dev_kqfilter,
101 };
102 
103 static g_init_t g_dev_init;
104 static g_fini_t g_dev_fini;
105 static g_taste_t g_dev_taste;
106 static g_orphan_t g_dev_orphan;
107 static g_attrchanged_t g_dev_attrchanged;
108 static g_resize_t g_dev_resize;
109 
110 static struct g_class g_dev_class	= {
111 	.name = "DEV",
112 	.version = G_VERSION,
113 	.init = g_dev_init,
114 	.fini = g_dev_fini,
115 	.taste = g_dev_taste,
116 	.orphan = g_dev_orphan,
117 	.attrchanged = g_dev_attrchanged,
118 	.resize = g_dev_resize
119 };
120 
121 /*
122  * We target 262144 (8 x 32768) sectors by default as this significantly
123  * increases the throughput on commonly used SSD's with a marginal
124  * increase in non-interruptible request latency.
125  */
126 static uint64_t g_dev_del_max_sectors = 262144;
127 SYSCTL_DECL(_kern_geom);
128 SYSCTL_NODE(_kern_geom, OID_AUTO, dev, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
129     "GEOM_DEV stuff");
130 SYSCTL_QUAD(_kern_geom_dev, OID_AUTO, delete_max_sectors, CTLFLAG_RW,
131     &g_dev_del_max_sectors, 0, "Maximum number of sectors in a single "
132     "delete request sent to the provider. Larger requests are chunked "
133     "so they can be interrupted. (0 = disable chunking)");
134 
135 static char *dumpdev = NULL;
136 static void
137 g_dev_init(struct g_class *mp)
138 {
139 
140 	dumpdev = kern_getenv("dumpdev");
141 }
142 
143 static void
144 g_dev_fini(struct g_class *mp)
145 {
146 
147 	freeenv(dumpdev);
148 	dumpdev = NULL;
149 }
150 
151 static int
152 g_dev_setdumpdev(struct cdev *dev, struct diocskerneldump_arg *kda)
153 {
154 	struct g_kerneldump kd;
155 	struct g_consumer *cp;
156 	int error, len;
157 
158 	MPASS(dev != NULL && kda != NULL);
159 	MPASS(kda->kda_index != KDA_REMOVE);
160 
161 	cp = dev->si_drv2;
162 	len = sizeof(kd);
163 	memset(&kd, 0, len);
164 	kd.offset = 0;
165 	kd.length = OFF_MAX;
166 	error = g_io_getattr("GEOM::kerneldump", cp, &len, &kd);
167 	if (error != 0)
168 		return (error);
169 
170 	error = dumper_insert(&kd.di, devtoname(dev), kda);
171 	if (error == 0)
172 		dev->si_flags |= SI_DUMPDEV;
173 
174 	return (error);
175 }
176 
177 static int
178 init_dumpdev(struct cdev *dev)
179 {
180 	struct diocskerneldump_arg kda;
181 	struct g_consumer *cp;
182 	const char *devprefix = _PATH_DEV, *devname;
183 	int error;
184 	size_t len;
185 
186 	bzero(&kda, sizeof(kda));
187 	kda.kda_index = KDA_APPEND;
188 
189 	if (dumpdev == NULL)
190 		return (0);
191 
192 	len = strlen(devprefix);
193 	devname = devtoname(dev);
194 	if (strcmp(devname, dumpdev) != 0 &&
195 	   (strncmp(dumpdev, devprefix, len) != 0 ||
196 	    strcmp(devname, dumpdev + len) != 0))
197 		return (0);
198 
199 	cp = (struct g_consumer *)dev->si_drv2;
200 	error = g_access(cp, 1, 0, 0);
201 	if (error != 0)
202 		return (error);
203 
204 	error = g_dev_setdumpdev(dev, &kda);
205 	if (error == 0) {
206 		freeenv(dumpdev);
207 		dumpdev = NULL;
208 	}
209 
210 	(void)g_access(cp, -1, 0, 0);
211 
212 	return (error);
213 }
214 
215 static void
216 g_dev_destroy(void *arg, int flags __unused)
217 {
218 	struct g_consumer *cp;
219 	struct g_geom *gp;
220 	struct g_dev_softc *sc;
221 	char buf[SPECNAMELEN + 6];
222 
223 	g_topology_assert();
224 	cp = arg;
225 	gp = cp->geom;
226 	sc = cp->private;
227 	g_trace(G_T_TOPOLOGY, "g_dev_destroy(%p(%s))", cp, gp->name);
228 	snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
229 	devctl_notify("GEOM", "DEV", "DESTROY", buf);
230 	knlist_clear(&sc->sc_selinfo.si_note, 0);
231 	knlist_destroy(&sc->sc_selinfo.si_note);
232 	if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0)
233 		g_access(cp, -cp->acr, -cp->acw, -cp->ace);
234 	g_detach(cp);
235 	g_destroy_consumer(cp);
236 	g_destroy_geom(gp);
237 	mtx_destroy(&sc->sc_mtx);
238 	g_free(sc);
239 }
240 
241 void
242 g_dev_print(void)
243 {
244 	struct g_geom *gp;
245 	char const *p = "";
246 
247 	LIST_FOREACH(gp, &g_dev_class.geom, geom) {
248 		printf("%s%s", p, gp->name);
249 		p = " ";
250 	}
251 	printf("\n");
252 }
253 
254 static void
255 g_dev_set_physpath(struct g_consumer *cp)
256 {
257 	struct g_dev_softc *sc;
258 	char *physpath;
259 	int error, physpath_len;
260 
261 	if (g_access(cp, 1, 0, 0) != 0)
262 		return;
263 
264 	sc = cp->private;
265 	physpath_len = MAXPATHLEN;
266 	physpath = g_malloc(physpath_len, M_WAITOK|M_ZERO);
267 	error = g_io_getattr("GEOM::physpath", cp, &physpath_len, physpath);
268 	g_access(cp, -1, 0, 0);
269 	if (error == 0 && strlen(physpath) != 0) {
270 		struct cdev *dev, *old_alias_dev;
271 		struct cdev **alias_devp;
272 
273 		dev = sc->sc_dev;
274 		old_alias_dev = sc->sc_alias;
275 		alias_devp = (struct cdev **)&sc->sc_alias;
276 		make_dev_physpath_alias(MAKEDEV_WAITOK, alias_devp, dev,
277 		    old_alias_dev, physpath);
278 	} else if (sc->sc_alias) {
279 		destroy_dev((struct cdev *)sc->sc_alias);
280 		sc->sc_alias = NULL;
281 	}
282 	g_free(physpath);
283 }
284 
285 static void
286 g_dev_set_media(struct g_consumer *cp)
287 {
288 	struct g_dev_softc *sc;
289 	struct cdev *dev;
290 	char buf[SPECNAMELEN + 6];
291 
292 	sc = cp->private;
293 	dev = sc->sc_dev;
294 	snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
295 	devctl_notify("DEVFS", "CDEV", "MEDIACHANGE", buf);
296 	devctl_notify("GEOM", "DEV", "MEDIACHANGE", buf);
297 	dev = sc->sc_alias;
298 	if (dev != NULL) {
299 		snprintf(buf, sizeof(buf), "cdev=%s", dev->si_name);
300 		devctl_notify("DEVFS", "CDEV", "MEDIACHANGE", buf);
301 		devctl_notify("GEOM", "DEV", "MEDIACHANGE", buf);
302 	}
303 }
304 
305 static void
306 g_dev_attrchanged(struct g_consumer *cp, const char *attr)
307 {
308 
309 	if (strcmp(attr, "GEOM::media") == 0) {
310 		g_dev_set_media(cp);
311 		return;
312 	}
313 
314 	if (strcmp(attr, "GEOM::physpath") == 0) {
315 		g_dev_set_physpath(cp);
316 		return;
317 	}
318 }
319 
320 static void
321 g_dev_resize(struct g_consumer *cp)
322 {
323 	struct g_dev_softc *sc;
324 	char buf[SPECNAMELEN + 6];
325 
326 	sc = cp->private;
327 	KNOTE_UNLOCKED(&sc->sc_selinfo.si_note, NOTE_ATTRIB);
328 
329 	snprintf(buf, sizeof(buf), "cdev=%s", cp->provider->name);
330 	devctl_notify("GEOM", "DEV", "SIZECHANGE", buf);
331 }
332 
333 struct g_provider *
334 g_dev_getprovider(struct cdev *dev)
335 {
336 	struct g_consumer *cp;
337 
338 	g_topology_assert();
339 	if (dev == NULL)
340 		return (NULL);
341 	if (dev->si_devsw != &g_dev_cdevsw)
342 		return (NULL);
343 	cp = dev->si_drv2;
344 	return (cp->provider);
345 }
346 
347 static struct g_geom *
348 g_dev_taste(struct g_class *mp, struct g_provider *pp, int insist __unused)
349 {
350 	struct g_geom *gp;
351 	struct g_geom_alias *gap;
352 	struct g_consumer *cp;
353 	struct g_dev_softc *sc;
354 	int error;
355 	struct cdev *dev, *adev;
356 	char buf[SPECNAMELEN + 6];
357 	struct make_dev_args args;
358 
359 	g_trace(G_T_TOPOLOGY, "dev_taste(%s,%s)", mp->name, pp->name);
360 	g_topology_assert();
361 	gp = g_new_geomf(mp, "%s", pp->name);
362 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
363 	mtx_init(&sc->sc_mtx, "g_dev", NULL, MTX_DEF);
364 	cp = g_new_consumer(gp);
365 	cp->private = sc;
366 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
367 	error = g_attach(cp, pp);
368 	if (error != 0) {
369 		printf("%s: g_dev_taste(%s) failed to g_attach, error=%d\n",
370 		    __func__, pp->name, error);
371 		g_destroy_consumer(cp);
372 		g_destroy_geom(gp);
373 		mtx_destroy(&sc->sc_mtx);
374 		g_free(sc);
375 		return (NULL);
376 	}
377 	make_dev_args_init(&args);
378 	args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
379 	args.mda_devsw = &g_dev_cdevsw;
380 	args.mda_cr = NULL;
381 	args.mda_uid = UID_ROOT;
382 	args.mda_gid = GID_OPERATOR;
383 	args.mda_mode = 0640;
384 	args.mda_si_drv1 = sc;
385 	args.mda_si_drv2 = cp;
386 	error = make_dev_s(&args, &sc->sc_dev, "%s", gp->name);
387 	if (error != 0) {
388 		printf("%s: make_dev_p() failed (gp->name=%s, error=%d)\n",
389 		    __func__, gp->name, error);
390 		g_detach(cp);
391 		g_destroy_consumer(cp);
392 		g_destroy_geom(gp);
393 		mtx_destroy(&sc->sc_mtx);
394 		g_free(sc);
395 		return (NULL);
396 	}
397 	dev = sc->sc_dev;
398 	dev->si_flags |= SI_UNMAPPED;
399 	dev->si_iosize_max = maxphys;
400 	knlist_init_mtx(&sc->sc_selinfo.si_note, &sc->sc_mtx);
401 	error = init_dumpdev(dev);
402 	if (error != 0)
403 		printf("%s: init_dumpdev() failed (gp->name=%s, error=%d)\n",
404 		    __func__, gp->name, error);
405 
406 	g_dev_attrchanged(cp, "GEOM::physpath");
407 	snprintf(buf, sizeof(buf), "cdev=%s", gp->name);
408 	devctl_notify("GEOM", "DEV", "CREATE", buf);
409 	/*
410 	 * Now add all the aliases for this drive
411 	 */
412 	LIST_FOREACH(gap, &pp->aliases, ga_next) {
413 		error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, &adev, dev,
414 		    "%s", gap->ga_alias);
415 		if (error) {
416 			printf("%s: make_dev_alias_p() failed (name=%s, error=%d)\n",
417 			    __func__, gap->ga_alias, error);
418 			continue;
419 		}
420 		snprintf(buf, sizeof(buf), "cdev=%s", gap->ga_alias);
421 		devctl_notify("GEOM", "DEV", "CREATE", buf);
422 	}
423 
424 	return (gp);
425 }
426 
427 static int
428 g_dev_open(struct cdev *dev, int flags, int fmt, struct thread *td)
429 {
430 	struct g_consumer *cp;
431 	struct g_dev_softc *sc;
432 	int error, r, w, e;
433 
434 	cp = dev->si_drv2;
435 	g_trace(G_T_ACCESS, "g_dev_open(%s, %d, %d, %p)",
436 	    cp->geom->name, flags, fmt, td);
437 
438 	r = flags & FREAD ? 1 : 0;
439 	w = flags & FWRITE ? 1 : 0;
440 #ifdef notyet
441 	e = flags & O_EXCL ? 1 : 0;
442 #else
443 	e = 0;
444 #endif
445 
446 	/*
447 	 * This happens on attempt to open a device node with O_EXEC.
448 	 */
449 	if (r + w + e == 0)
450 		return (EINVAL);
451 
452 	if (w) {
453 		/*
454 		 * When running in very secure mode, do not allow
455 		 * opens for writing of any disks.
456 		 */
457 		error = securelevel_ge(td->td_ucred, 2);
458 		if (error)
459 			return (error);
460 	}
461 	g_topology_lock();
462 	error = g_access(cp, r, w, e);
463 	g_topology_unlock();
464 	if (error == 0) {
465 		sc = dev->si_drv1;
466 		mtx_lock(&sc->sc_mtx);
467 		if (sc->sc_open == 0 && (sc->sc_active & SC_A_ACTIVE) != 0)
468 			wakeup(&sc->sc_active);
469 		sc->sc_open += r + w + e;
470 		if (sc->sc_open == 0)
471 			atomic_clear_int(&sc->sc_active, SC_A_OPEN);
472 		else
473 			atomic_set_int(&sc->sc_active, SC_A_OPEN);
474 		mtx_unlock(&sc->sc_mtx);
475 	}
476 	return (error);
477 }
478 
479 static int
480 g_dev_close(struct cdev *dev, int flags, int fmt, struct thread *td)
481 {
482 	struct g_consumer *cp;
483 	struct g_dev_softc *sc;
484 	int error, r, w, e;
485 
486 	cp = dev->si_drv2;
487 	g_trace(G_T_ACCESS, "g_dev_close(%s, %d, %d, %p)",
488 	    cp->geom->name, flags, fmt, td);
489 
490 	r = flags & FREAD ? -1 : 0;
491 	w = flags & FWRITE ? -1 : 0;
492 #ifdef notyet
493 	e = flags & O_EXCL ? -1 : 0;
494 #else
495 	e = 0;
496 #endif
497 
498 	/*
499 	 * The vgonel(9) - caused by eg. forced unmount of devfs - calls
500 	 * VOP_CLOSE(9) on devfs vnode without any FREAD or FWRITE flags,
501 	 * which would result in zero deltas, which in turn would cause
502 	 * panic in g_access(9).
503 	 *
504 	 * Note that we cannot zero the counters (ie. do "r = cp->acr"
505 	 * etc) instead, because the consumer might be opened in another
506 	 * devfs instance.
507 	 */
508 	if (r + w + e == 0)
509 		return (EINVAL);
510 
511 	sc = dev->si_drv1;
512 	mtx_lock(&sc->sc_mtx);
513 	sc->sc_open += r + w + e;
514 	if (sc->sc_open == 0)
515 		atomic_clear_int(&sc->sc_active, SC_A_OPEN);
516 	else
517 		atomic_set_int(&sc->sc_active, SC_A_OPEN);
518 	while (sc->sc_open == 0 && (sc->sc_active & SC_A_ACTIVE) != 0)
519 		msleep(&sc->sc_active, &sc->sc_mtx, 0, "g_dev_close", hz / 10);
520 	mtx_unlock(&sc->sc_mtx);
521 	g_topology_lock();
522 	error = g_access(cp, r, w, e);
523 	g_topology_unlock();
524 	return (error);
525 }
526 
527 static int
528 g_dev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
529 {
530 	struct g_consumer *cp;
531 	struct g_provider *pp;
532 	off_t offset, length, chunk, odd;
533 	int i, error;
534 #ifdef COMPAT_FREEBSD12
535 	struct diocskerneldump_arg kda_copy;
536 #endif
537 
538 	cp = dev->si_drv2;
539 	pp = cp->provider;
540 
541 	/* If consumer or provider is dying, don't disturb. */
542 	if (cp->flags & G_CF_ORPHAN)
543 		return (ENXIO);
544 	if (pp->error)
545 		return (pp->error);
546 
547 	error = 0;
548 	KASSERT(cp->acr || cp->acw,
549 	    ("Consumer with zero access count in g_dev_ioctl"));
550 
551 	i = IOCPARM_LEN(cmd);
552 	switch (cmd) {
553 	case DIOCGSECTORSIZE:
554 		*(u_int *)data = pp->sectorsize;
555 		if (*(u_int *)data == 0)
556 			error = ENOENT;
557 		break;
558 	case DIOCGMEDIASIZE:
559 		*(off_t *)data = pp->mediasize;
560 		if (*(off_t *)data == 0)
561 			error = ENOENT;
562 		break;
563 	case DIOCGFWSECTORS:
564 		error = g_io_getattr("GEOM::fwsectors", cp, &i, data);
565 		if (error == 0 && *(u_int *)data == 0)
566 			error = ENOENT;
567 		break;
568 	case DIOCGFWHEADS:
569 		error = g_io_getattr("GEOM::fwheads", cp, &i, data);
570 		if (error == 0 && *(u_int *)data == 0)
571 			error = ENOENT;
572 		break;
573 #ifdef COMPAT_FREEBSD11
574 	case DIOCSKERNELDUMP_FREEBSD11:
575 	    {
576 		struct diocskerneldump_arg kda;
577 
578 		gone_in(13, "FreeBSD 11.x ABI compat");
579 
580 		bzero(&kda, sizeof(kda));
581 		kda.kda_encryption = KERNELDUMP_ENC_NONE;
582 		kda.kda_index = (*(u_int *)data ? 0 : KDA_REMOVE_ALL);
583 		if (kda.kda_index == KDA_REMOVE_ALL)
584 			error = dumper_remove(devtoname(dev), &kda);
585 		else
586 			error = g_dev_setdumpdev(dev, &kda);
587 		break;
588 	    }
589 #endif
590 #ifdef COMPAT_FREEBSD12
591 	case DIOCSKERNELDUMP_FREEBSD12:
592 	    {
593 		struct diocskerneldump_arg_freebsd12 *kda12;
594 
595 		gone_in(14, "FreeBSD 12.x ABI compat");
596 
597 		kda12 = (void *)data;
598 		memcpy(&kda_copy, kda12, sizeof(kda_copy));
599 		kda_copy.kda_index = (kda12->kda12_enable ?
600 		    0 : KDA_REMOVE_ALL);
601 
602 		explicit_bzero(kda12, sizeof(*kda12));
603 		/* Kludge to pass kda_copy to kda in fallthrough. */
604 		data = (void *)&kda_copy;
605 	    }
606 	    /* FALLTHROUGH */
607 #endif
608 	case DIOCSKERNELDUMP:
609 	    {
610 		struct diocskerneldump_arg *kda;
611 		uint8_t *encryptedkey;
612 
613 		kda = (struct diocskerneldump_arg *)data;
614 		if (kda->kda_index == KDA_REMOVE_ALL ||
615 		    kda->kda_index == KDA_REMOVE_DEV ||
616 		    kda->kda_index == KDA_REMOVE) {
617 			error = dumper_remove(devtoname(dev), kda);
618 			explicit_bzero(kda, sizeof(*kda));
619 			break;
620 		}
621 
622 		if (kda->kda_encryption != KERNELDUMP_ENC_NONE) {
623 			if (kda->kda_encryptedkeysize == 0 ||
624 			    kda->kda_encryptedkeysize >
625 			    KERNELDUMP_ENCKEY_MAX_SIZE) {
626 				explicit_bzero(kda, sizeof(*kda));
627 				return (EINVAL);
628 			}
629 			encryptedkey = malloc(kda->kda_encryptedkeysize, M_TEMP,
630 			    M_WAITOK);
631 			error = copyin(kda->kda_encryptedkey, encryptedkey,
632 			    kda->kda_encryptedkeysize);
633 		} else {
634 			encryptedkey = NULL;
635 		}
636 		if (error == 0) {
637 			kda->kda_encryptedkey = encryptedkey;
638 			error = g_dev_setdumpdev(dev, kda);
639 		}
640 		zfree(encryptedkey, M_TEMP);
641 		explicit_bzero(kda, sizeof(*kda));
642 		break;
643 	    }
644 	case DIOCGFLUSH:
645 		error = g_io_flush(cp);
646 		break;
647 	case DIOCGDELETE:
648 		offset = ((off_t *)data)[0];
649 		length = ((off_t *)data)[1];
650 		if ((offset % pp->sectorsize) != 0 ||
651 		    (length % pp->sectorsize) != 0 || length <= 0) {
652 			printf("%s: offset=%jd length=%jd\n", __func__, offset,
653 			    length);
654 			error = EINVAL;
655 			break;
656 		}
657 		if ((pp->mediasize > 0) && (offset >= pp->mediasize)) {
658 			/*
659 			 * Catch out-of-bounds requests here. The problem is
660 			 * that due to historical GEOM I/O implementation
661 			 * peculatities, g_delete_data() would always return
662 			 * success for requests starting just the next byte
663 			 * after providers media boundary. Condition check on
664 			 * non-zero media size, since that condition would
665 			 * (most likely) cause ENXIO instead.
666 			 */
667 			error = EIO;
668 			break;
669 		}
670 		while (length > 0) {
671 			chunk = length;
672 			if (g_dev_del_max_sectors != 0 &&
673 			    chunk > g_dev_del_max_sectors * pp->sectorsize) {
674 				chunk = g_dev_del_max_sectors * pp->sectorsize;
675 				if (pp->stripesize > 0) {
676 					odd = (offset + chunk +
677 					    pp->stripeoffset) % pp->stripesize;
678 					if (chunk > odd)
679 						chunk -= odd;
680 				}
681 			}
682 			error = g_delete_data(cp, offset, chunk);
683 			length -= chunk;
684 			offset += chunk;
685 			if (error)
686 				break;
687 			/*
688 			 * Since the request size can be large, the service
689 			 * time can be is likewise.  We make this ioctl
690 			 * interruptible by checking for signals for each bio.
691 			 */
692 			if (SIGPENDING(td))
693 				break;
694 		}
695 		break;
696 	case DIOCGIDENT:
697 		error = g_io_getattr("GEOM::ident", cp, &i, data);
698 		break;
699 	case DIOCGPROVIDERNAME:
700 		strlcpy(data, pp->name, i);
701 		break;
702 	case DIOCGSTRIPESIZE:
703 		*(off_t *)data = pp->stripesize;
704 		break;
705 	case DIOCGSTRIPEOFFSET:
706 		*(off_t *)data = pp->stripeoffset;
707 		break;
708 	case DIOCGPHYSPATH:
709 		error = g_io_getattr("GEOM::physpath", cp, &i, data);
710 		if (error == 0 && *(char *)data == '\0')
711 			error = ENOENT;
712 		break;
713 	case DIOCGATTR: {
714 		struct diocgattr_arg *arg = (struct diocgattr_arg *)data;
715 
716 		if (arg->len > sizeof(arg->value)) {
717 			error = EINVAL;
718 			break;
719 		}
720 		error = g_io_getattr(arg->name, cp, &arg->len, &arg->value);
721 		break;
722 	}
723 	case DIOCZONECMD: {
724 		struct disk_zone_args *zone_args =(struct disk_zone_args *)data;
725 		struct disk_zone_rep_entry *new_entries, *old_entries;
726 		struct disk_zone_report *rep;
727 		size_t alloc_size;
728 
729 		old_entries = NULL;
730 		new_entries = NULL;
731 		rep = NULL;
732 		alloc_size = 0;
733 
734 		if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES) {
735 			rep = &zone_args->zone_params.report;
736 #define	MAXENTRIES	(maxphys / sizeof(struct disk_zone_rep_entry))
737 			if (rep->entries_allocated > MAXENTRIES)
738 				rep->entries_allocated = MAXENTRIES;
739 			alloc_size = rep->entries_allocated *
740 			    sizeof(struct disk_zone_rep_entry);
741 			if (alloc_size != 0)
742 				new_entries = g_malloc(alloc_size,
743 				    M_WAITOK | M_ZERO);
744 			old_entries = rep->entries;
745 			rep->entries = new_entries;
746 		}
747 		error = g_io_zonecmd(zone_args, cp);
748 		if (zone_args->zone_cmd == DISK_ZONE_REPORT_ZONES &&
749 		    alloc_size != 0 && error == 0)
750 			error = copyout(new_entries, old_entries, alloc_size);
751 		if (old_entries != NULL && rep != NULL)
752 			rep->entries = old_entries;
753 		if (new_entries != NULL)
754 			g_free(new_entries);
755 		break;
756 	}
757 	default:
758 		if (pp->geom->ioctl != NULL) {
759 			error = pp->geom->ioctl(pp, cmd, data, fflag, td);
760 		} else {
761 			error = ENOIOCTL;
762 		}
763 	}
764 
765 	return (error);
766 }
767 
768 static void
769 g_dev_done(struct bio *bp2)
770 {
771 	struct g_consumer *cp;
772 	struct g_dev_softc *sc;
773 	struct bio *bp;
774 	int active;
775 
776 	cp = bp2->bio_from;
777 	sc = cp->private;
778 	bp = bp2->bio_parent;
779 	bp->bio_error = bp2->bio_error;
780 	bp->bio_completed = bp2->bio_completed;
781 	bp->bio_resid = bp->bio_length - bp2->bio_completed;
782 	if (bp2->bio_cmd == BIO_ZONE)
783 		bcopy(&bp2->bio_zone, &bp->bio_zone, sizeof(bp->bio_zone));
784 
785 	if (bp2->bio_error != 0) {
786 		g_trace(G_T_BIO, "g_dev_done(%p) had error %d",
787 		    bp2, bp2->bio_error);
788 		bp->bio_flags |= BIO_ERROR;
789 	} else {
790 		g_trace(G_T_BIO, "g_dev_done(%p/%p) resid %ld completed %jd",
791 		    bp2, bp, bp2->bio_resid, (intmax_t)bp2->bio_completed);
792 	}
793 	g_destroy_bio(bp2);
794 	active = atomic_fetchadd_int(&sc->sc_active, -1) - 1;
795 	if ((active & SC_A_ACTIVE) == 0) {
796 		if ((active & SC_A_OPEN) == 0)
797 			wakeup(&sc->sc_active);
798 		if (active & SC_A_DESTROY)
799 			g_post_event(g_dev_destroy, cp, M_NOWAIT, NULL);
800 	}
801 	biodone(bp);
802 }
803 
804 static void
805 g_dev_strategy(struct bio *bp)
806 {
807 	struct g_consumer *cp;
808 	struct bio *bp2;
809 	struct cdev *dev;
810 	struct g_dev_softc *sc;
811 
812 	KASSERT(bp->bio_cmd == BIO_READ ||
813 	        bp->bio_cmd == BIO_WRITE ||
814 	        bp->bio_cmd == BIO_DELETE ||
815 		bp->bio_cmd == BIO_FLUSH ||
816 		bp->bio_cmd == BIO_ZONE,
817 		("Wrong bio_cmd bio=%p cmd=%d", bp, bp->bio_cmd));
818 	dev = bp->bio_dev;
819 	cp = dev->si_drv2;
820 	KASSERT(cp->acr || cp->acw,
821 	    ("Consumer with zero access count in g_dev_strategy"));
822 	biotrack(bp, __func__);
823 #ifdef INVARIANTS
824 	if ((bp->bio_offset % cp->provider->sectorsize) != 0 ||
825 	    (bp->bio_bcount % cp->provider->sectorsize) != 0) {
826 		bp->bio_resid = bp->bio_bcount;
827 		biofinish(bp, NULL, EINVAL);
828 		return;
829 	}
830 #endif
831 	sc = dev->si_drv1;
832 	KASSERT(sc->sc_open > 0, ("Closed device in g_dev_strategy"));
833 	atomic_add_int(&sc->sc_active, 1);
834 
835 	for (;;) {
836 		/*
837 		 * XXX: This is not an ideal solution, but I believe it to
838 		 * XXX: deadlock safely, all things considered.
839 		 */
840 		bp2 = g_clone_bio(bp);
841 		if (bp2 != NULL)
842 			break;
843 		pause("gdstrat", hz / 10);
844 	}
845 	KASSERT(bp2 != NULL, ("XXX: ENOMEM in a bad place"));
846 	bp2->bio_done = g_dev_done;
847 	g_trace(G_T_BIO,
848 	    "g_dev_strategy(%p/%p) offset %jd length %jd data %p cmd %d",
849 	    bp, bp2, (intmax_t)bp->bio_offset, (intmax_t)bp2->bio_length,
850 	    bp2->bio_data, bp2->bio_cmd);
851 	g_io_request(bp2, cp);
852 	KASSERT(cp->acr || cp->acw,
853 	    ("g_dev_strategy raced with g_dev_close and lost"));
854 
855 }
856 
857 /*
858  * g_dev_callback()
859  *
860  * Called by devfs when asynchronous device destruction is completed.
861  * - Mark that we have no attached device any more.
862  * - If there are no outstanding requests, schedule geom destruction.
863  *   Otherwise destruction will be scheduled later by g_dev_done().
864  */
865 
866 static void
867 g_dev_callback(void *arg)
868 {
869 	struct g_consumer *cp;
870 	struct g_dev_softc *sc;
871 	int active;
872 
873 	cp = arg;
874 	sc = cp->private;
875 	g_trace(G_T_TOPOLOGY, "g_dev_callback(%p(%s))", cp, cp->geom->name);
876 
877 	sc->sc_dev = NULL;
878 	sc->sc_alias = NULL;
879 	active = atomic_fetchadd_int(&sc->sc_active, SC_A_DESTROY);
880 	if ((active & SC_A_ACTIVE) == 0)
881 		g_post_event(g_dev_destroy, cp, M_WAITOK, NULL);
882 }
883 
884 /*
885  * g_dev_orphan()
886  *
887  * Called from below when the provider orphaned us.
888  * - Clear any dump settings.
889  * - Request asynchronous device destruction to prevent any more requests
890  *   from coming in.  The provider is already marked with an error, so
891  *   anything which comes in the interim will be returned immediately.
892  */
893 
894 static void
895 g_dev_orphan(struct g_consumer *cp)
896 {
897 	struct cdev *dev;
898 	struct g_dev_softc *sc;
899 
900 	g_topology_assert();
901 	sc = cp->private;
902 	dev = sc->sc_dev;
903 	g_trace(G_T_TOPOLOGY, "g_dev_orphan(%p(%s))", cp, cp->geom->name);
904 
905 	/* Reset any dump-area set on this device */
906 	if (dev->si_flags & SI_DUMPDEV) {
907 		struct diocskerneldump_arg kda;
908 
909 		bzero(&kda, sizeof(kda));
910 		kda.kda_index = KDA_REMOVE_DEV;
911 		(void)dumper_remove(devtoname(dev), &kda);
912 	}
913 
914 	/* Destroy the struct cdev *so we get no more requests */
915 	delist_dev(dev);
916 	destroy_dev_sched_cb(dev, g_dev_callback, cp);
917 }
918 
919 static void
920 gdev_filter_detach(struct knote *kn)
921 {
922 	struct g_dev_softc *sc;
923 
924 	sc = kn->kn_hook;
925 
926 	knlist_remove(&sc->sc_selinfo.si_note, kn, 0);
927 }
928 
929 static int
930 gdev_filter_vnode(struct knote *kn, long hint)
931 {
932 	kn->kn_fflags |= kn->kn_sfflags & hint;
933 
934 	return (kn->kn_fflags != 0);
935 }
936 
937 static int
938 g_dev_kqfilter(struct cdev *dev, struct knote *kn)
939 {
940 	struct g_dev_softc *sc;
941 
942 	sc = dev->si_drv1;
943 
944 	if (kn->kn_filter != EVFILT_VNODE)
945 		return (EINVAL);
946 
947 	/* XXX: extend support for other NOTE_* events */
948 	if (kn->kn_sfflags != NOTE_ATTRIB)
949 		return (EINVAL);
950 
951 	kn->kn_fop = &gdev_filterops_vnode;
952 	kn->kn_hook = sc;
953 	knlist_add(&sc->sc_selinfo.si_note, kn, 0);
954 
955 	return (0);
956 }
957 
958 DECLARE_GEOM_CLASS(g_dev_class, g_dev);
959