g_gate.c revision 220173
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: head/sys/geom/gate/g_gate.c 220173 2011-03-30 21:40:14Z trociny $");
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
56FEATURE(geom_gate, "GEOM Gate module");
57
58static MALLOC_DEFINE(M_GATE, "gg_data", "GEOM Gate Data");
59
60SYSCTL_DECL(_kern_geom);
61SYSCTL_NODE(_kern_geom, OID_AUTO, gate, CTLFLAG_RW, 0, "GEOM_GATE stuff");
62static int g_gate_debug = 0;
63TUNABLE_INT("kern.geom.gate.debug", &g_gate_debug);
64SYSCTL_INT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RW, &g_gate_debug, 0,
65    "Debug level");
66static u_int g_gate_maxunits = 256;
67TUNABLE_INT("kern.geom.gate.maxunits", &g_gate_maxunits);
68SYSCTL_UINT(_kern_geom_gate, OID_AUTO, maxunits, CTLFLAG_RDTUN,
69    &g_gate_maxunits, 0, "Maximum number of ggate devices");
70
71struct g_class g_gate_class = {
72	.name = G_GATE_CLASS_NAME,
73	.version = G_VERSION,
74};
75
76static struct cdev *status_dev;
77static d_ioctl_t g_gate_ioctl;
78static 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
85static struct g_gate_softc **g_gate_units;
86static u_int g_gate_nunits;
87static struct mtx g_gate_units_lock;
88
89static int
90g_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(1, "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
147static int
148g_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
167static void
168g_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
214static struct g_gate_softc *
215g_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
241static void
242g_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
254static int
255g_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
276static void
277g_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
311static void
312g_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
343static int
344g_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_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	sc->sc_name = name;
420	g_gate_units[sc->sc_unit] = sc;
421	g_gate_nunits++;
422	mtx_unlock(&g_gate_units_lock);
423
424	ggio->gctl_unit = sc->sc_unit;
425
426	g_topology_lock();
427	gp = g_new_geomf(&g_gate_class, "%s", name);
428	gp->start = g_gate_start;
429	gp->access = g_gate_access;
430	gp->dumpconf = g_gate_dumpconf;
431	gp->softc = sc;
432	pp = g_new_providerf(gp, "%s", name);
433	pp->mediasize = ggio->gctl_mediasize;
434	pp->sectorsize = ggio->gctl_sectorsize;
435	sc->sc_provider = pp;
436	g_error_provider(pp, 0);
437	g_topology_unlock();
438	mtx_lock(&g_gate_units_lock);
439	sc->sc_name = sc->sc_provider->name;
440	mtx_unlock(&g_gate_units_lock);
441	G_GATE_DEBUG(1, "Device %s created.", gp->name);
442
443	if (sc->sc_timeout > 0) {
444		callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
445		    g_gate_guard, sc);
446	}
447	return (0);
448}
449
450#define	G_GATE_CHECK_VERSION(ggio)	do {				\
451	if ((ggio)->gctl_version != G_GATE_VERSION) {			\
452		printf("Version mismatch %d != %d.\n",			\
453		    ggio->gctl_version, G_GATE_VERSION);		\
454		return (EINVAL);					\
455	}								\
456} while (0)
457static int
458g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
459{
460	struct g_gate_softc *sc;
461	struct bio *bp;
462	int error = 0;
463
464	G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr,
465	    flags, td);
466
467	switch (cmd) {
468	case G_GATE_CMD_CREATE:
469	    {
470		struct g_gate_ctl_create *ggio = (void *)addr;
471
472		G_GATE_CHECK_VERSION(ggio);
473		error = g_gate_create(ggio);
474		/*
475		 * Reset TDP_GEOM flag.
476		 * There are pending events for sure, because we just created
477		 * new provider and other classes want to taste it, but we
478		 * cannot answer on I/O requests until we're here.
479		 */
480		td->td_pflags &= ~TDP_GEOM;
481		return (error);
482	    }
483	case G_GATE_CMD_DESTROY:
484	    {
485		struct g_gate_ctl_destroy *ggio = (void *)addr;
486
487		G_GATE_CHECK_VERSION(ggio);
488		sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
489		if (sc == NULL)
490			return (ENXIO);
491		g_topology_lock();
492		mtx_lock(&g_gate_units_lock);
493		error = g_gate_destroy(sc, ggio->gctl_force);
494		g_topology_unlock();
495		if (error != 0)
496			g_gate_release(sc);
497		return (error);
498	    }
499	case G_GATE_CMD_CANCEL:
500	    {
501		struct g_gate_ctl_cancel *ggio = (void *)addr;
502		struct bio *tbp, *lbp;
503
504		G_GATE_CHECK_VERSION(ggio);
505		sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
506		if (sc == NULL)
507			return (ENXIO);
508		lbp = NULL;
509		mtx_lock(&sc->sc_queue_mtx);
510		TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) {
511			if (ggio->gctl_seq == 0 ||
512			    ggio->gctl_seq == (uintptr_t)bp->bio_driver1) {
513				G_GATE_LOGREQ(1, bp, "Request canceled.");
514				bioq_remove(&sc->sc_outqueue, bp);
515				/*
516				 * Be sure to put requests back onto incoming
517				 * queue in the proper order.
518				 */
519				if (lbp == NULL)
520					bioq_insert_head(&sc->sc_inqueue, bp);
521				else {
522					TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue,
523					    lbp, bp, bio_queue);
524				}
525				lbp = bp;
526				/*
527				 * If only one request was canceled, leave now.
528				 */
529				if (ggio->gctl_seq != 0)
530					break;
531			}
532		}
533		if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
534			ggio->gctl_unit = sc->sc_unit;
535		mtx_unlock(&sc->sc_queue_mtx);
536		g_gate_release(sc);
537		return (error);
538	    }
539	case G_GATE_CMD_START:
540	    {
541		struct g_gate_ctl_io *ggio = (void *)addr;
542
543		G_GATE_CHECK_VERSION(ggio);
544		sc = g_gate_hold(ggio->gctl_unit, NULL);
545		if (sc == NULL)
546			return (ENXIO);
547		error = 0;
548		for (;;) {
549			mtx_lock(&sc->sc_queue_mtx);
550			bp = bioq_first(&sc->sc_inqueue);
551			if (bp != NULL)
552				break;
553			if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
554				ggio->gctl_error = ECANCELED;
555				mtx_unlock(&sc->sc_queue_mtx);
556				goto start_end;
557			}
558			if (msleep(sc, &sc->sc_queue_mtx,
559			    PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) {
560				ggio->gctl_error = ECANCELED;
561				goto start_end;
562			}
563		}
564		ggio->gctl_cmd = bp->bio_cmd;
565		if ((bp->bio_cmd == BIO_DELETE || bp->bio_cmd == BIO_WRITE) &&
566		    bp->bio_length > ggio->gctl_length) {
567			mtx_unlock(&sc->sc_queue_mtx);
568			ggio->gctl_length = bp->bio_length;
569			ggio->gctl_error = ENOMEM;
570			goto start_end;
571		}
572		bioq_remove(&sc->sc_inqueue, bp);
573		bioq_insert_tail(&sc->sc_outqueue, bp);
574		mtx_unlock(&sc->sc_queue_mtx);
575
576		ggio->gctl_seq = (uintptr_t)bp->bio_driver1;
577		ggio->gctl_offset = bp->bio_offset;
578		ggio->gctl_length = bp->bio_length;
579
580		switch (bp->bio_cmd) {
581		case BIO_READ:
582		case BIO_DELETE:
583			break;
584		case BIO_WRITE:
585			error = copyout(bp->bio_data, ggio->gctl_data,
586			    bp->bio_length);
587			if (error != 0) {
588				mtx_lock(&sc->sc_queue_mtx);
589				bioq_remove(&sc->sc_outqueue, bp);
590				bioq_insert_head(&sc->sc_inqueue, bp);
591				mtx_unlock(&sc->sc_queue_mtx);
592				goto start_end;
593			}
594			break;
595		}
596start_end:
597		g_gate_release(sc);
598		return (error);
599	    }
600	case G_GATE_CMD_DONE:
601	    {
602		struct g_gate_ctl_io *ggio = (void *)addr;
603
604		G_GATE_CHECK_VERSION(ggio);
605		sc = g_gate_hold(ggio->gctl_unit, NULL);
606		if (sc == NULL)
607			return (ENOENT);
608		error = 0;
609		mtx_lock(&sc->sc_queue_mtx);
610		TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) {
611			if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1)
612				break;
613		}
614		if (bp != NULL) {
615			bioq_remove(&sc->sc_outqueue, bp);
616			sc->sc_queue_count--;
617		}
618		mtx_unlock(&sc->sc_queue_mtx);
619		if (bp == NULL) {
620			/*
621			 * Request was probably canceled.
622			 */
623			goto done_end;
624		}
625		if (ggio->gctl_error == EAGAIN) {
626			bp->bio_error = 0;
627			G_GATE_LOGREQ(1, bp, "Request desisted.");
628			mtx_lock(&sc->sc_queue_mtx);
629			sc->sc_queue_count++;
630			bioq_insert_head(&sc->sc_inqueue, bp);
631			wakeup(sc);
632			mtx_unlock(&sc->sc_queue_mtx);
633		} else {
634			bp->bio_error = ggio->gctl_error;
635			if (bp->bio_error == 0) {
636				bp->bio_completed = bp->bio_length;
637				switch (bp->bio_cmd) {
638				case BIO_READ:
639					error = copyin(ggio->gctl_data,
640					    bp->bio_data, bp->bio_length);
641					if (error != 0)
642						bp->bio_error = error;
643					break;
644				case BIO_DELETE:
645				case BIO_WRITE:
646					break;
647				}
648			}
649			G_GATE_LOGREQ(2, bp, "Request done.");
650			g_io_deliver(bp, bp->bio_error);
651		}
652done_end:
653		g_gate_release(sc);
654		return (error);
655	    }
656	}
657	return (ENOIOCTL);
658}
659
660static void
661g_gate_device(void)
662{
663
664	status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600,
665	    G_GATE_CTL_NAME);
666}
667
668static int
669g_gate_modevent(module_t mod, int type, void *data)
670{
671	int error = 0;
672
673	switch (type) {
674	case MOD_LOAD:
675		mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF);
676		g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]),
677		    M_GATE, M_WAITOK | M_ZERO);
678		g_gate_nunits = 0;
679		g_gate_device();
680		break;
681	case MOD_UNLOAD:
682		mtx_lock(&g_gate_units_lock);
683		if (g_gate_nunits > 0) {
684			mtx_unlock(&g_gate_units_lock);
685			error = EBUSY;
686			break;
687		}
688		mtx_unlock(&g_gate_units_lock);
689		mtx_destroy(&g_gate_units_lock);
690		if (status_dev != 0)
691			destroy_dev(status_dev);
692		free(g_gate_units, M_GATE);
693		break;
694	default:
695		return (EOPNOTSUPP);
696		break;
697	}
698
699	return (error);
700}
701static moduledata_t g_gate_module = {
702	G_GATE_MOD_NAME,
703	g_gate_modevent,
704	NULL
705};
706DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
707DECLARE_GEOM_CLASS(g_gate_class, g_gate);
708