xref: /freebsd/sys/geom/gate/g_gate.c (revision aa0a1e58f0189b0fde359a8bda032887e72057fa)
1 /*-
2  * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * Copyright (c) 2009-2010 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Pawel Jakub Dawidek
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bio.h>
37 #include <sys/conf.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
40 #include <sys/fcntl.h>
41 #include <sys/linker.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
45 #include <sys/proc.h>
46 #include <sys/limits.h>
47 #include <sys/queue.h>
48 #include <sys/sysctl.h>
49 #include <sys/signalvar.h>
50 #include <sys/time.h>
51 #include <machine/atomic.h>
52 
53 #include <geom/geom.h>
54 #include <geom/gate/g_gate.h>
55 
56 FEATURE(geom_gate, "GEOM Gate module");
57 
58 static MALLOC_DEFINE(M_GATE, "gg_data", "GEOM Gate Data");
59 
60 SYSCTL_DECL(_kern_geom);
61 SYSCTL_NODE(_kern_geom, OID_AUTO, gate, CTLFLAG_RW, 0, "GEOM_GATE stuff");
62 static int g_gate_debug = 0;
63 TUNABLE_INT("kern.geom.gate.debug", &g_gate_debug);
64 SYSCTL_INT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RW, &g_gate_debug, 0,
65     "Debug level");
66 static u_int g_gate_maxunits = 256;
67 TUNABLE_INT("kern.geom.gate.maxunits", &g_gate_maxunits);
68 SYSCTL_UINT(_kern_geom_gate, OID_AUTO, maxunits, CTLFLAG_RDTUN,
69     &g_gate_maxunits, 0, "Maximum number of ggate devices");
70 
71 struct g_class g_gate_class = {
72 	.name = G_GATE_CLASS_NAME,
73 	.version = G_VERSION,
74 };
75 
76 static struct cdev *status_dev;
77 static d_ioctl_t g_gate_ioctl;
78 static struct cdevsw g_gate_cdevsw = {
79 	.d_version =	D_VERSION,
80 	.d_ioctl =	g_gate_ioctl,
81 	.d_name =	G_GATE_CTL_NAME
82 };
83 
84 
85 static struct g_gate_softc **g_gate_units;
86 static u_int g_gate_nunits;
87 static struct mtx g_gate_units_lock;
88 
89 static int
90 g_gate_destroy(struct g_gate_softc *sc, boolean_t force)
91 {
92 	struct g_provider *pp;
93 	struct g_geom *gp;
94 	struct bio *bp;
95 
96 	g_topology_assert();
97 	mtx_assert(&g_gate_units_lock, MA_OWNED);
98 	pp = sc->sc_provider;
99 	if (!force && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
100 		mtx_unlock(&g_gate_units_lock);
101 		return (EBUSY);
102 	}
103 	mtx_unlock(&g_gate_units_lock);
104 	mtx_lock(&sc->sc_queue_mtx);
105 	if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0)
106 		sc->sc_flags |= G_GATE_FLAG_DESTROY;
107 	wakeup(sc);
108 	mtx_unlock(&sc->sc_queue_mtx);
109 	gp = pp->geom;
110 	pp->flags |= G_PF_WITHER;
111 	g_orphan_provider(pp, ENXIO);
112 	callout_drain(&sc->sc_callout);
113 	mtx_lock(&sc->sc_queue_mtx);
114 	while ((bp = bioq_first(&sc->sc_inqueue)) != NULL) {
115 		bioq_remove(&sc->sc_inqueue, bp);
116 		sc->sc_queue_count--;
117 		G_GATE_LOGREQ(1, bp, "Request canceled.");
118 		g_io_deliver(bp, ENXIO);
119 	}
120 	while ((bp = bioq_first(&sc->sc_outqueue)) != NULL) {
121 		bioq_remove(&sc->sc_outqueue, bp);
122 		sc->sc_queue_count--;
123 		G_GATE_LOGREQ(1, bp, "Request canceled.");
124 		g_io_deliver(bp, ENXIO);
125 	}
126 	mtx_unlock(&sc->sc_queue_mtx);
127 	g_topology_unlock();
128 	mtx_lock(&g_gate_units_lock);
129 	/* One reference is ours. */
130 	sc->sc_ref--;
131 	while (sc->sc_ref > 0)
132 		msleep(&sc->sc_ref, &g_gate_units_lock, 0, "gg:destroy", 0);
133 	g_gate_units[sc->sc_unit] = NULL;
134 	KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?"));
135 	g_gate_nunits--;
136 	mtx_unlock(&g_gate_units_lock);
137 	mtx_destroy(&sc->sc_queue_mtx);
138 	g_topology_lock();
139 	G_GATE_DEBUG(0, "Device %s destroyed.", gp->name);
140 	gp->softc = NULL;
141 	g_wither_geom(gp, ENXIO);
142 	sc->sc_provider = NULL;
143 	free(sc, M_GATE);
144 	return (0);
145 }
146 
147 static int
148 g_gate_access(struct g_provider *pp, int dr, int dw, int de)
149 {
150 	struct g_gate_softc *sc;
151 
152 	if (dr <= 0 && dw <= 0 && de <= 0)
153 		return (0);
154 	sc = pp->geom->softc;
155 	if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
156 		return (ENXIO);
157 	/* XXX: Hack to allow read-only mounts. */
158 #if 0
159 	if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0)
160 		return (EPERM);
161 #endif
162 	if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0)
163 		return (EPERM);
164 	return (0);
165 }
166 
167 static void
168 g_gate_start(struct bio *bp)
169 {
170 	struct g_gate_softc *sc;
171 
172 	sc = bp->bio_to->geom->softc;
173 	if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
174 		g_io_deliver(bp, ENXIO);
175 		return;
176 	}
177 	G_GATE_LOGREQ(2, bp, "Request received.");
178 	switch (bp->bio_cmd) {
179 	case BIO_READ:
180 		break;
181 	case BIO_DELETE:
182 	case BIO_WRITE:
183 		/* XXX: Hack to allow read-only mounts. */
184 		if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
185 			g_io_deliver(bp, EPERM);
186 			return;
187 		}
188 		break;
189 	case BIO_GETATTR:
190 	default:
191 		G_GATE_LOGREQ(2, bp, "Ignoring request.");
192 		g_io_deliver(bp, EOPNOTSUPP);
193 		return;
194 	}
195 
196 	mtx_lock(&sc->sc_queue_mtx);
197 	if (sc->sc_queue_count > sc->sc_queue_size) {
198 		mtx_unlock(&sc->sc_queue_mtx);
199 		G_GATE_LOGREQ(1, bp, "Queue full, request canceled.");
200 		g_io_deliver(bp, ENOMEM);
201 		return;
202 	}
203 
204 	bp->bio_driver1 = (void *)sc->sc_seq;
205 	sc->sc_seq++;
206 	sc->sc_queue_count++;
207 
208 	bioq_insert_tail(&sc->sc_inqueue, bp);
209 	wakeup(sc);
210 
211 	mtx_unlock(&sc->sc_queue_mtx);
212 }
213 
214 static struct g_gate_softc *
215 g_gate_hold(int unit, const char *name)
216 {
217 	struct g_gate_softc *sc = NULL;
218 
219 	mtx_lock(&g_gate_units_lock);
220 	if (unit >= 0 && unit < g_gate_maxunits)
221 		sc = g_gate_units[unit];
222 	else if (unit == G_GATE_NAME_GIVEN) {
223 		KASSERT(name != NULL, ("name is NULL"));
224 		for (unit = 0; unit < g_gate_maxunits; unit++) {
225 			if (g_gate_units[unit] == NULL)
226 				continue;
227 			if (strcmp(name,
228 			    g_gate_units[unit]->sc_provider->name) != 0) {
229 				continue;
230 			}
231 			sc = g_gate_units[unit];
232 			break;
233 		}
234 	}
235 	if (sc != NULL)
236 		sc->sc_ref++;
237 	mtx_unlock(&g_gate_units_lock);
238 	return (sc);
239 }
240 
241 static void
242 g_gate_release(struct g_gate_softc *sc)
243 {
244 
245 	g_topology_assert_not();
246 	mtx_lock(&g_gate_units_lock);
247 	sc->sc_ref--;
248 	KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name));
249 	if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
250 		wakeup(&sc->sc_ref);
251 	mtx_unlock(&g_gate_units_lock);
252 }
253 
254 static int
255 g_gate_getunit(int unit, int *errorp)
256 {
257 
258 	mtx_assert(&g_gate_units_lock, MA_OWNED);
259 	if (unit >= 0) {
260 		if (unit >= g_gate_maxunits)
261 			*errorp = EINVAL;
262 		else if (g_gate_units[unit] == NULL)
263 			return (unit);
264 		else
265 			*errorp = EEXIST;
266 	} else {
267 		for (unit = 0; unit < g_gate_maxunits; unit++) {
268 			if (g_gate_units[unit] == NULL)
269 				return (unit);
270 		}
271 		*errorp = ENFILE;
272 	}
273 	return (-1);
274 }
275 
276 static void
277 g_gate_guard(void *arg)
278 {
279 	struct g_gate_softc *sc;
280 	struct bintime curtime;
281 	struct bio *bp, *bp2;
282 
283 	sc = arg;
284 	binuptime(&curtime);
285 	g_gate_hold(sc->sc_unit, NULL);
286 	mtx_lock(&sc->sc_queue_mtx);
287 	TAILQ_FOREACH_SAFE(bp, &sc->sc_inqueue.queue, bio_queue, bp2) {
288 		if (curtime.sec - bp->bio_t0.sec < 5)
289 			continue;
290 		bioq_remove(&sc->sc_inqueue, bp);
291 		sc->sc_queue_count--;
292 		G_GATE_LOGREQ(1, bp, "Request timeout.");
293 		g_io_deliver(bp, EIO);
294 	}
295 	TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, bp2) {
296 		if (curtime.sec - bp->bio_t0.sec < 5)
297 			continue;
298 		bioq_remove(&sc->sc_outqueue, bp);
299 		sc->sc_queue_count--;
300 		G_GATE_LOGREQ(1, bp, "Request timeout.");
301 		g_io_deliver(bp, EIO);
302 	}
303 	mtx_unlock(&sc->sc_queue_mtx);
304 	if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) {
305 		callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
306 		    g_gate_guard, sc);
307 	}
308 	g_gate_release(sc);
309 }
310 
311 static void
312 g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
313     struct g_consumer *cp, struct g_provider *pp)
314 {
315 	struct g_gate_softc *sc;
316 
317 	sc = gp->softc;
318 	if (sc == NULL || pp != NULL || cp != NULL)
319 		return;
320 	g_gate_hold(sc->sc_unit, NULL);
321 	if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
322 		sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only");
323 	} else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) {
324 		sbuf_printf(sb, "%s<access>%s</access>\n", indent,
325 		    "write-only");
326 	} else {
327 		sbuf_printf(sb, "%s<access>%s</access>\n", indent,
328 		    "read-write");
329 	}
330 	sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout);
331 	sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info);
332 	sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent,
333 	    sc->sc_queue_count);
334 	sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent,
335 	    sc->sc_queue_size);
336 	sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref);
337 	sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, sc->sc_unit);
338 	g_topology_unlock();
339 	g_gate_release(sc);
340 	g_topology_lock();
341 }
342 
343 static int
344 g_gate_create(struct g_gate_ctl_create *ggio)
345 {
346 	struct g_gate_softc *sc;
347 	struct g_geom *gp;
348 	struct g_provider *pp;
349 	char name[NAME_MAX];
350 	int error = 0, unit;
351 
352 	if (ggio->gctl_mediasize == 0) {
353 		G_GATE_DEBUG(1, "Invalid media size.");
354 		return (EINVAL);
355 	}
356 	if (ggio->gctl_sectorsize > 0 && !powerof2(ggio->gctl_sectorsize)) {
357 		G_GATE_DEBUG(1, "Invalid sector size.");
358 		return (EINVAL);
359 	}
360 	if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) {
361 		G_GATE_DEBUG(1, "Invalid media size.");
362 		return (EINVAL);
363 	}
364 	if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 &&
365 	    (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) {
366 		G_GATE_DEBUG(1, "Invalid flags.");
367 		return (EINVAL);
368 	}
369 	if (ggio->gctl_unit != G_GATE_UNIT_AUTO &&
370 	    ggio->gctl_unit != G_GATE_NAME_GIVEN &&
371 	    ggio->gctl_unit < 0) {
372 		G_GATE_DEBUG(1, "Invalid unit number.");
373 		return (EINVAL);
374 	}
375 	if (ggio->gctl_unit == G_GATE_NAME_GIVEN &&
376 	    ggio->gctl_name[0] == '\0') {
377 		G_GATE_DEBUG(1, "No device name.");
378 		return (EINVAL);
379 	}
380 
381 	sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO);
382 	sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS);
383 	strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
384 	sc->sc_seq = 1;
385 	bioq_init(&sc->sc_inqueue);
386 	bioq_init(&sc->sc_outqueue);
387 	mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF);
388 	sc->sc_queue_count = 0;
389 	sc->sc_queue_size = ggio->gctl_maxcount;
390 	if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE)
391 		sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE;
392 	sc->sc_timeout = ggio->gctl_timeout;
393 	callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
394 	mtx_lock(&g_gate_units_lock);
395 	sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error);
396 	if (sc->sc_unit < 0) {
397 		mtx_unlock(&g_gate_units_lock);
398 		mtx_destroy(&sc->sc_queue_mtx);
399 		free(sc, M_GATE);
400 		return (error);
401 	}
402 	if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
403 		snprintf(name, sizeof(name), "%s", ggio->gctl_name);
404 	else {
405 		snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME,
406 		    sc->sc_unit);
407 	}
408 	/* Check for name collision. */
409 	for (unit = 0; unit < g_gate_maxunits; unit++) {
410 		if (g_gate_units[unit] == NULL)
411 			continue;
412 		if (strcmp(name, g_gate_units[unit]->sc_provider->name) != 0)
413 			continue;
414 		mtx_unlock(&g_gate_units_lock);
415 		mtx_destroy(&sc->sc_queue_mtx);
416 		free(sc, M_GATE);
417 		return (EEXIST);
418 	}
419 	g_gate_units[sc->sc_unit] = sc;
420 	g_gate_nunits++;
421 	mtx_unlock(&g_gate_units_lock);
422 
423 	ggio->gctl_unit = sc->sc_unit;
424 
425 	g_topology_lock();
426 	gp = g_new_geomf(&g_gate_class, "%s", name);
427 	gp->start = g_gate_start;
428 	gp->access = g_gate_access;
429 	gp->dumpconf = g_gate_dumpconf;
430 	gp->softc = sc;
431 	pp = g_new_providerf(gp, "%s", name);
432 	pp->mediasize = ggio->gctl_mediasize;
433 	pp->sectorsize = ggio->gctl_sectorsize;
434 	sc->sc_provider = pp;
435 	g_error_provider(pp, 0);
436 	g_topology_unlock();
437 
438 	if (sc->sc_timeout > 0) {
439 		callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
440 		    g_gate_guard, sc);
441 	}
442 	return (0);
443 }
444 
445 #define	G_GATE_CHECK_VERSION(ggio)	do {				\
446 	if ((ggio)->gctl_version != G_GATE_VERSION) {			\
447 		printf("Version mismatch %d != %d.\n",			\
448 		    ggio->gctl_version, G_GATE_VERSION);		\
449 		return (EINVAL);					\
450 	}								\
451 } while (0)
452 static int
453 g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
454 {
455 	struct g_gate_softc *sc;
456 	struct bio *bp;
457 	int error = 0;
458 
459 	G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr,
460 	    flags, td);
461 
462 	switch (cmd) {
463 	case G_GATE_CMD_CREATE:
464 	    {
465 		struct g_gate_ctl_create *ggio = (void *)addr;
466 
467 		G_GATE_CHECK_VERSION(ggio);
468 		error = g_gate_create(ggio);
469 		/*
470 		 * Reset TDP_GEOM flag.
471 		 * There are pending events for sure, because we just created
472 		 * new provider and other classes want to taste it, but we
473 		 * cannot answer on I/O requests until we're here.
474 		 */
475 		td->td_pflags &= ~TDP_GEOM;
476 		return (error);
477 	    }
478 	case G_GATE_CMD_DESTROY:
479 	    {
480 		struct g_gate_ctl_destroy *ggio = (void *)addr;
481 
482 		G_GATE_CHECK_VERSION(ggio);
483 		sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
484 		if (sc == NULL)
485 			return (ENXIO);
486 		g_topology_lock();
487 		mtx_lock(&g_gate_units_lock);
488 		error = g_gate_destroy(sc, ggio->gctl_force);
489 		g_topology_unlock();
490 		if (error != 0)
491 			g_gate_release(sc);
492 		return (error);
493 	    }
494 	case G_GATE_CMD_CANCEL:
495 	    {
496 		struct g_gate_ctl_cancel *ggio = (void *)addr;
497 		struct bio *tbp, *lbp;
498 
499 		G_GATE_CHECK_VERSION(ggio);
500 		sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
501 		if (sc == NULL)
502 			return (ENXIO);
503 		lbp = NULL;
504 		mtx_lock(&sc->sc_queue_mtx);
505 		TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) {
506 			if (ggio->gctl_seq == 0 ||
507 			    ggio->gctl_seq == (uintptr_t)bp->bio_driver1) {
508 				G_GATE_LOGREQ(1, bp, "Request canceled.");
509 				bioq_remove(&sc->sc_outqueue, bp);
510 				/*
511 				 * Be sure to put requests back onto incoming
512 				 * queue in the proper order.
513 				 */
514 				if (lbp == NULL)
515 					bioq_insert_head(&sc->sc_inqueue, bp);
516 				else {
517 					TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue,
518 					    lbp, bp, bio_queue);
519 				}
520 				lbp = bp;
521 				/*
522 				 * If only one request was canceled, leave now.
523 				 */
524 				if (ggio->gctl_seq != 0)
525 					break;
526 			}
527 		}
528 		if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
529 			ggio->gctl_unit = sc->sc_unit;
530 		mtx_unlock(&sc->sc_queue_mtx);
531 		g_gate_release(sc);
532 		return (error);
533 	    }
534 	case G_GATE_CMD_START:
535 	    {
536 		struct g_gate_ctl_io *ggio = (void *)addr;
537 
538 		G_GATE_CHECK_VERSION(ggio);
539 		sc = g_gate_hold(ggio->gctl_unit, NULL);
540 		if (sc == NULL)
541 			return (ENXIO);
542 		error = 0;
543 		for (;;) {
544 			mtx_lock(&sc->sc_queue_mtx);
545 			bp = bioq_first(&sc->sc_inqueue);
546 			if (bp != NULL)
547 				break;
548 			if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
549 				ggio->gctl_error = ECANCELED;
550 				mtx_unlock(&sc->sc_queue_mtx);
551 				goto start_end;
552 			}
553 			if (msleep(sc, &sc->sc_queue_mtx,
554 			    PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) {
555 				ggio->gctl_error = ECANCELED;
556 				goto start_end;
557 			}
558 		}
559 		ggio->gctl_cmd = bp->bio_cmd;
560 		if ((bp->bio_cmd == BIO_DELETE || bp->bio_cmd == BIO_WRITE) &&
561 		    bp->bio_length > ggio->gctl_length) {
562 			mtx_unlock(&sc->sc_queue_mtx);
563 			ggio->gctl_length = bp->bio_length;
564 			ggio->gctl_error = ENOMEM;
565 			goto start_end;
566 		}
567 		bioq_remove(&sc->sc_inqueue, bp);
568 		bioq_insert_tail(&sc->sc_outqueue, bp);
569 		mtx_unlock(&sc->sc_queue_mtx);
570 
571 		ggio->gctl_seq = (uintptr_t)bp->bio_driver1;
572 		ggio->gctl_offset = bp->bio_offset;
573 		ggio->gctl_length = bp->bio_length;
574 
575 		switch (bp->bio_cmd) {
576 		case BIO_READ:
577 		case BIO_DELETE:
578 			break;
579 		case BIO_WRITE:
580 			error = copyout(bp->bio_data, ggio->gctl_data,
581 			    bp->bio_length);
582 			if (error != 0) {
583 				mtx_lock(&sc->sc_queue_mtx);
584 				bioq_remove(&sc->sc_outqueue, bp);
585 				bioq_insert_head(&sc->sc_inqueue, bp);
586 				mtx_unlock(&sc->sc_queue_mtx);
587 				goto start_end;
588 			}
589 			break;
590 		}
591 start_end:
592 		g_gate_release(sc);
593 		return (error);
594 	    }
595 	case G_GATE_CMD_DONE:
596 	    {
597 		struct g_gate_ctl_io *ggio = (void *)addr;
598 
599 		G_GATE_CHECK_VERSION(ggio);
600 		sc = g_gate_hold(ggio->gctl_unit, NULL);
601 		if (sc == NULL)
602 			return (ENOENT);
603 		error = 0;
604 		mtx_lock(&sc->sc_queue_mtx);
605 		TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) {
606 			if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1)
607 				break;
608 		}
609 		if (bp != NULL) {
610 			bioq_remove(&sc->sc_outqueue, bp);
611 			sc->sc_queue_count--;
612 		}
613 		mtx_unlock(&sc->sc_queue_mtx);
614 		if (bp == NULL) {
615 			/*
616 			 * Request was probably canceled.
617 			 */
618 			goto done_end;
619 		}
620 		if (ggio->gctl_error == EAGAIN) {
621 			bp->bio_error = 0;
622 			G_GATE_LOGREQ(1, bp, "Request desisted.");
623 			mtx_lock(&sc->sc_queue_mtx);
624 			sc->sc_queue_count++;
625 			bioq_insert_head(&sc->sc_inqueue, bp);
626 			wakeup(sc);
627 			mtx_unlock(&sc->sc_queue_mtx);
628 		} else {
629 			bp->bio_error = ggio->gctl_error;
630 			if (bp->bio_error == 0) {
631 				bp->bio_completed = bp->bio_length;
632 				switch (bp->bio_cmd) {
633 				case BIO_READ:
634 					error = copyin(ggio->gctl_data,
635 					    bp->bio_data, bp->bio_length);
636 					if (error != 0)
637 						bp->bio_error = error;
638 					break;
639 				case BIO_DELETE:
640 				case BIO_WRITE:
641 					break;
642 				}
643 			}
644 			G_GATE_LOGREQ(2, bp, "Request done.");
645 			g_io_deliver(bp, bp->bio_error);
646 		}
647 done_end:
648 		g_gate_release(sc);
649 		return (error);
650 	    }
651 	}
652 	return (ENOIOCTL);
653 }
654 
655 static void
656 g_gate_device(void)
657 {
658 
659 	status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600,
660 	    G_GATE_CTL_NAME);
661 }
662 
663 static int
664 g_gate_modevent(module_t mod, int type, void *data)
665 {
666 	int error = 0;
667 
668 	switch (type) {
669 	case MOD_LOAD:
670 		mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF);
671 		g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]),
672 		    M_GATE, M_WAITOK | M_ZERO);
673 		g_gate_nunits = 0;
674 		g_gate_device();
675 		break;
676 	case MOD_UNLOAD:
677 		mtx_lock(&g_gate_units_lock);
678 		if (g_gate_nunits > 0) {
679 			mtx_unlock(&g_gate_units_lock);
680 			error = EBUSY;
681 			break;
682 		}
683 		mtx_unlock(&g_gate_units_lock);
684 		mtx_destroy(&g_gate_units_lock);
685 		if (status_dev != 0)
686 			destroy_dev(status_dev);
687 		free(g_gate_units, M_GATE);
688 		break;
689 	default:
690 		return (EOPNOTSUPP);
691 		break;
692 	}
693 
694 	return (error);
695 }
696 static moduledata_t g_gate_module = {
697 	G_GATE_MOD_NAME,
698 	g_gate_modevent,
699 	NULL
700 };
701 DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
702 DECLARE_GEOM_CLASS(g_gate_class, g_gate);
703