xref: /freebsd/sys/geom/gate/g_gate.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*-
2  * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org>
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  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bio.h>
32 #include <sys/conf.h>
33 #include <sys/kernel.h>
34 #include <sys/kthread.h>
35 #include <sys/fcntl.h>
36 #include <sys/linker.h>
37 #include <sys/lock.h>
38 #include <sys/malloc.h>
39 #include <sys/mutex.h>
40 #include <sys/proc.h>
41 #include <sys/limits.h>
42 #include <sys/queue.h>
43 #include <sys/sysctl.h>
44 #include <sys/signalvar.h>
45 #include <sys/time.h>
46 #include <machine/atomic.h>
47 
48 #include <geom/geom.h>
49 #include <geom/gate/g_gate.h>
50 
51 static MALLOC_DEFINE(M_GATE, "gg data", "GEOM Gate Data");
52 
53 SYSCTL_DECL(_kern_geom);
54 SYSCTL_NODE(_kern_geom, OID_AUTO, gate, CTLFLAG_RW, 0, "GEOM_GATE stuff");
55 static u_int g_gate_debug = 0;
56 SYSCTL_UINT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RW, &g_gate_debug, 0,
57     "Debug level");
58 
59 static int g_gate_destroy_geom(struct gctl_req *, struct g_class *,
60     struct g_geom *);
61 struct g_class g_gate_class = {
62 	.name = G_GATE_CLASS_NAME,
63 	.version = G_VERSION,
64 	.destroy_geom = g_gate_destroy_geom
65 };
66 
67 static struct cdev *status_dev;
68 static d_ioctl_t g_gate_ioctl;
69 static struct cdevsw g_gate_cdevsw = {
70 	.d_version =	D_VERSION,
71 	.d_ioctl =	g_gate_ioctl,
72 	.d_name =	G_GATE_CTL_NAME
73 };
74 
75 
76 static LIST_HEAD(, g_gate_softc) g_gate_list =
77     LIST_HEAD_INITIALIZER(&g_gate_list);
78 static struct mtx g_gate_list_mtx;
79 
80 
81 static void
82 g_gate_wither(struct g_gate_softc *sc)
83 {
84 
85 	atomic_set_32(&sc->sc_flags, G_GATE_FLAG_DESTROY);
86 }
87 
88 static int
89 g_gate_destroy(struct g_gate_softc *sc, boolean_t force)
90 {
91 	struct g_provider *pp;
92 	struct bio *bp;
93 
94 	g_topology_assert();
95 	mtx_assert(&g_gate_list_mtx, MA_OWNED);
96 	pp = sc->sc_provider;
97 	if (!force && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
98 		mtx_unlock(&g_gate_list_mtx);
99 		return (EBUSY);
100 	}
101 	if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) {
102 		g_gate_wither(sc);
103 		LIST_REMOVE(sc, sc_next);
104 	}
105 	mtx_unlock(&g_gate_list_mtx);
106 	mtx_lock(&sc->sc_queue_mtx);
107 	wakeup(sc);
108 	mtx_unlock(&sc->sc_queue_mtx);
109 	if (sc->sc_ref > 0) {
110 		G_GATE_DEBUG(1, "Cannot destroy %s yet.", sc->sc_name);
111 		return (0);
112 	}
113 	callout_drain(&sc->sc_callout);
114 	mtx_lock(&sc->sc_queue_mtx);
115 	for (;;) {
116 		bp = bioq_first(&sc->sc_inqueue);
117 		if (bp != NULL) {
118 			bioq_remove(&sc->sc_inqueue, bp);
119 			sc->sc_queue_count--;
120 			G_GATE_LOGREQ(1, bp, "Request canceled.");
121 			g_io_deliver(bp, ENXIO);
122 		} else {
123 			break;
124 		}
125 	}
126 	for (;;) {
127 		bp = bioq_first(&sc->sc_outqueue);
128 		if (bp != NULL) {
129 			bioq_remove(&sc->sc_outqueue, bp);
130 			sc->sc_queue_count--;
131 			G_GATE_LOGREQ(1, bp, "Request canceled.");
132 			g_io_deliver(bp, ENXIO);
133 		} else {
134 			break;
135 		}
136 	}
137 	mtx_destroy(&sc->sc_queue_mtx);
138 	G_GATE_DEBUG(0, "Device %s destroyed.", sc->sc_name);
139 	pp->geom->softc = NULL;
140 	g_wither_geom(pp->geom, ENXIO);
141 	sc->sc_provider = NULL;
142 	free(sc, M_GATE);
143 	return (0);
144 }
145 
146 static void
147 g_gate_destroy_it(void *arg, int flag __unused)
148 {
149 	struct g_gate_softc *sc;
150 
151 	g_topology_assert();
152 	sc = arg;
153 	mtx_lock(&g_gate_list_mtx);
154 	g_gate_destroy(sc, 1);
155 }
156 
157 static int
158 g_gate_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
159 {
160 
161 	g_topology_assert();
162 	mtx_lock(&g_gate_list_mtx);
163 	return (g_gate_destroy(gp->softc, 0));
164 }
165 
166 static int
167 g_gate_access(struct g_provider *pp, int dr, int dw, int de)
168 {
169 	struct g_gate_softc *sc;
170 
171 	if (dr <= 0 && dw <= 0 && de <= 0)
172 		return (0);
173 	sc = pp->geom->softc;
174 	if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
175 		return (ENXIO);
176 	/* XXX: Hack to allow read-only mounts. */
177 #if 0
178 	if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0)
179 		return (EPERM);
180 #endif
181 	if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0)
182 		return (EPERM);
183 	return (0);
184 }
185 
186 static void
187 g_gate_start(struct bio *bp)
188 {
189 	struct g_gate_softc *sc;
190 
191 	sc = bp->bio_to->geom->softc;
192 	if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
193 		g_io_deliver(bp, ENXIO);
194 		return;
195 	}
196 	G_GATE_LOGREQ(2, bp, "Request received.");
197 	switch (bp->bio_cmd) {
198 	case BIO_READ:
199 		break;
200 	case BIO_DELETE:
201 	case BIO_WRITE:
202 		/* XXX: Hack to allow read-only mounts. */
203 		if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
204 			g_io_deliver(bp, EPERM);
205 			return;
206 		}
207 		break;
208 	case BIO_GETATTR:
209 	default:
210 		G_GATE_LOGREQ(2, bp, "Ignoring request.");
211 		g_io_deliver(bp, EOPNOTSUPP);
212 		return;
213 	}
214 
215 	mtx_lock(&sc->sc_queue_mtx);
216 	if (sc->sc_queue_count > sc->sc_queue_size) {
217 		mtx_unlock(&sc->sc_queue_mtx);
218 		G_GATE_LOGREQ(1, bp, "Queue full, request canceled.");
219 		g_io_deliver(bp, EIO);
220 		return;
221 	}
222 
223 	bp->bio_driver1 = (void *)sc->sc_seq;
224 	sc->sc_seq++;
225 	sc->sc_queue_count++;
226 
227 	bioq_insert_tail(&sc->sc_inqueue, bp);
228 	wakeup(sc);
229 
230 	mtx_unlock(&sc->sc_queue_mtx);
231 }
232 
233 static struct g_gate_softc *
234 g_gate_find(u_int unit)
235 {
236 	struct g_gate_softc *sc;
237 
238 	mtx_assert(&g_gate_list_mtx, MA_OWNED);
239 	LIST_FOREACH(sc, &g_gate_list, sc_next) {
240 		if (sc->sc_unit == unit)
241 			break;
242 	}
243 	return (sc);
244 }
245 
246 static struct g_gate_softc *
247 g_gate_hold(u_int unit)
248 {
249 	struct g_gate_softc *sc;
250 
251 	mtx_lock(&g_gate_list_mtx);
252 	sc = g_gate_find(unit);
253 	if (sc != NULL) {
254 		if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
255 			sc = NULL;
256 		else
257 			sc->sc_ref++;
258 	}
259 	mtx_unlock(&g_gate_list_mtx);
260 	return (sc);
261 }
262 
263 static void
264 g_gate_release(struct g_gate_softc *sc)
265 {
266 
267 	g_topology_assert_not();
268 	mtx_lock(&g_gate_list_mtx);
269 	sc->sc_ref--;
270 	KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name));
271 	if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
272 		mtx_unlock(&g_gate_list_mtx);
273 		g_waitfor_event(g_gate_destroy_it, sc, M_WAITOK, NULL);
274 	} else {
275 		mtx_unlock(&g_gate_list_mtx);
276 	}
277 }
278 
279 static int
280 g_gate_getunit(int unit)
281 {
282 	struct g_gate_softc *sc;
283 
284 	mtx_assert(&g_gate_list_mtx, MA_OWNED);
285 	if (unit >= 0) {
286 		LIST_FOREACH(sc, &g_gate_list, sc_next) {
287 			if (sc->sc_unit == unit)
288 				return (-1);
289 		}
290 	} else {
291 		unit = 0;
292 once_again:
293 		LIST_FOREACH(sc, &g_gate_list, sc_next) {
294 			if (sc->sc_unit == unit) {
295 				if (++unit > 666)
296 					return (-1);
297 				goto once_again;
298 			}
299 		}
300 	}
301 	return (unit);
302 }
303 
304 static void
305 g_gate_guard(void *arg)
306 {
307 	struct g_gate_softc *sc;
308 	struct bintime curtime;
309 	struct bio *bp, *bp2;
310 
311 	sc = arg;
312 	binuptime(&curtime);
313 	g_gate_hold(sc->sc_unit);
314 	mtx_lock(&sc->sc_queue_mtx);
315 	TAILQ_FOREACH_SAFE(bp, &sc->sc_inqueue.queue, bio_queue, bp2) {
316 		if (curtime.sec - bp->bio_t0.sec < 5)
317 			continue;
318 		bioq_remove(&sc->sc_inqueue, bp);
319 		sc->sc_queue_count--;
320 		G_GATE_LOGREQ(1, bp, "Request timeout.");
321 		g_io_deliver(bp, EIO);
322 	}
323 	TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, bp2) {
324 		if (curtime.sec - bp->bio_t0.sec < 5)
325 			continue;
326 		bioq_remove(&sc->sc_outqueue, bp);
327 		sc->sc_queue_count--;
328 		G_GATE_LOGREQ(1, bp, "Request timeout.");
329 		g_io_deliver(bp, EIO);
330 	}
331 	mtx_unlock(&sc->sc_queue_mtx);
332 	if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) {
333 		callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
334 		    g_gate_guard, sc);
335 	}
336 	g_gate_release(sc);
337 }
338 
339 static void
340 g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
341     struct g_consumer *cp, struct g_provider *pp)
342 {
343 	struct g_gate_softc *sc;
344 
345 	sc = gp->softc;
346 	if (sc == NULL || pp != NULL || cp != NULL)
347 		return;
348 	g_gate_hold(sc->sc_unit);
349 	if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
350 		sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only");
351 	} else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) {
352 		sbuf_printf(sb, "%s<access>%s</access>\n", indent,
353 		    "write-only");
354 	} else {
355 		sbuf_printf(sb, "%s<access>%s</access>\n", indent,
356 		    "read-write");
357 	}
358 	sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout);
359 	sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info);
360 	sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent,
361 	    sc->sc_queue_count);
362 	sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent,
363 	    sc->sc_queue_size);
364 	sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref);
365 	g_topology_unlock();
366 	g_gate_release(sc);
367 	g_topology_lock();
368 }
369 
370 static int
371 g_gate_create(struct g_gate_ctl_create *ggio)
372 {
373 	struct g_gate_softc *sc;
374 	struct g_geom *gp;
375 	struct g_provider *pp;
376 
377 	if (ggio->gctl_mediasize == 0) {
378 		G_GATE_DEBUG(1, "Invalid media size.");
379 		return (EINVAL);
380 	}
381 	if (ggio->gctl_sectorsize > 0 && !powerof2(ggio->gctl_sectorsize)) {
382 		G_GATE_DEBUG(1, "Invalid sector size.");
383 		return (EINVAL);
384 	}
385 	if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) {
386 		G_GATE_DEBUG(1, "Invalid media size.");
387 		return (EINVAL);
388 	}
389 	if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 &&
390 	    (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) {
391 		G_GATE_DEBUG(1, "Invalid flags.");
392 		return (EINVAL);
393 	}
394 	if (ggio->gctl_unit < -1) {
395 		G_GATE_DEBUG(1, "Invalid unit number.");
396 		return (EINVAL);
397 	}
398 
399 	sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO);
400 	sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS);
401 	strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
402 	sc->sc_seq = 0;
403 	bioq_init(&sc->sc_inqueue);
404 	bioq_init(&sc->sc_outqueue);
405 	mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF);
406 	sc->sc_queue_count = 0;
407 	sc->sc_queue_size = ggio->gctl_maxcount;
408 	if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE)
409 		sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE;
410 	sc->sc_timeout = ggio->gctl_timeout;
411 	callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
412 	mtx_lock(&g_gate_list_mtx);
413 	ggio->gctl_unit = g_gate_getunit(ggio->gctl_unit);
414 	if (ggio->gctl_unit == -1) {
415 		mtx_unlock(&g_gate_list_mtx);
416 		mtx_destroy(&sc->sc_queue_mtx);
417 		free(sc, M_GATE);
418 		return (EBUSY);
419 	}
420 	sc->sc_unit = ggio->gctl_unit;
421 	LIST_INSERT_HEAD(&g_gate_list, sc, sc_next);
422 	mtx_unlock(&g_gate_list_mtx);
423 
424 	g_topology_lock();
425 	gp = g_new_geomf(&g_gate_class, "%s%d", G_GATE_PROVIDER_NAME,
426 	    sc->sc_unit);
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%d", G_GATE_PROVIDER_NAME, sc->sc_unit);
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 		return (EINVAL);					\
448 } while (0)
449 static int
450 g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
451 {
452 	struct g_gate_softc *sc;
453 	struct bio *bp;
454 	int error = 0;
455 
456 	G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr,
457 	    flags, td);
458 
459 	switch (cmd) {
460 	case G_GATE_CMD_CREATE:
461 	    {
462 		struct g_gate_ctl_create *ggio = (void *)addr;
463 
464 		G_GATE_CHECK_VERSION(ggio);
465 		error = g_gate_create(ggio);
466 		return (error);
467 	    }
468 	case G_GATE_CMD_DESTROY:
469 	    {
470 		struct g_gate_ctl_destroy *ggio = (void *)addr;
471 
472 		G_GATE_CHECK_VERSION(ggio);
473 		sc = g_gate_hold(ggio->gctl_unit);
474 		if (sc == NULL)
475 			return (ENXIO);
476 		g_topology_lock();
477 		mtx_lock(&g_gate_list_mtx);
478 		error = g_gate_destroy(sc, ggio->gctl_force);
479 		if (error == 0)
480 			g_gate_wither(sc);
481 		g_topology_unlock();
482 		g_gate_release(sc);
483 		return (error);
484 	    }
485 	case G_GATE_CMD_START:
486 	    {
487 		struct g_gate_ctl_io *ggio = (void *)addr;
488 
489 		G_GATE_CHECK_VERSION(ggio);
490 		sc = g_gate_find(ggio->gctl_unit);
491 		if (sc == NULL)
492 			return (ENXIO);
493 		for (;;) {
494 			mtx_lock(&sc->sc_queue_mtx);
495 			bp = bioq_first(&sc->sc_inqueue);
496 			if (bp != NULL)
497 				break;
498 			if (msleep(sc, &sc->sc_queue_mtx,
499 			    PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) {
500 				ggio->gctl_error = ECANCELED;
501 				return (0);
502 			}
503 			if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
504 				ggio->gctl_error = ECANCELED;
505 				return (0);
506 			}
507 		}
508 		ggio->gctl_cmd = bp->bio_cmd;
509 		if ((bp->bio_cmd == BIO_DELETE || bp->bio_cmd == BIO_WRITE) &&
510 		    bp->bio_length > ggio->gctl_length) {
511 			mtx_unlock(&sc->sc_queue_mtx);
512 			ggio->gctl_length = bp->bio_length;
513 			ggio->gctl_error = ENOMEM;
514 			return (0);
515 		}
516 		bioq_remove(&sc->sc_inqueue, bp);
517 		bioq_insert_tail(&sc->sc_outqueue, bp);
518 		mtx_unlock(&sc->sc_queue_mtx);
519 
520 		ggio->gctl_seq = (uintptr_t)bp->bio_driver1;
521 		ggio->gctl_offset = bp->bio_offset;
522 		ggio->gctl_length = bp->bio_length;
523 		switch (bp->bio_cmd) {
524 		case BIO_READ:
525 			break;
526 		case BIO_DELETE:
527 		case BIO_WRITE:
528 			error = copyout(bp->bio_data, ggio->gctl_data,
529 			    bp->bio_length);
530 			if (error != 0) {
531 				mtx_lock(&sc->sc_queue_mtx);
532 				bioq_remove(&sc->sc_outqueue, bp);
533 				bioq_insert_head(&sc->sc_inqueue, bp);
534 				mtx_unlock(&sc->sc_queue_mtx);
535 				return (error);
536 			}
537 			break;
538 		}
539 		return (0);
540 	    }
541 	case G_GATE_CMD_DONE:
542 	    {
543 		struct g_gate_ctl_io *ggio = (void *)addr;
544 
545 		G_GATE_CHECK_VERSION(ggio);
546 		sc = g_gate_find(ggio->gctl_unit);
547 		if (sc == NULL)
548 			return (ENOENT);
549 		mtx_lock(&sc->sc_queue_mtx);
550 		TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) {
551 			if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1)
552 				break;
553 		}
554 		if (bp != NULL) {
555 			bioq_remove(&sc->sc_outqueue, bp);
556 			sc->sc_queue_count--;
557 		}
558 		mtx_unlock(&sc->sc_queue_mtx);
559 		if (bp == NULL) {
560 			/*
561 			 * Request was probably canceled.
562 			 */
563 			return (0);
564 		}
565 		if (ggio->gctl_error == EAGAIN) {
566 			bp->bio_error = 0;
567 			G_GATE_LOGREQ(1, bp, "Request desisted.");
568 			mtx_lock(&sc->sc_queue_mtx);
569 			sc->sc_queue_count++;
570 			bioq_insert_head(&sc->sc_inqueue, bp);
571 			wakeup(sc);
572 			mtx_unlock(&sc->sc_queue_mtx);
573 		} else {
574 			bp->bio_error = ggio->gctl_error;
575 			if (bp->bio_error == 0) {
576 				bp->bio_completed = bp->bio_length;
577 				switch (bp->bio_cmd) {
578 				case BIO_READ:
579 					error = copyin(ggio->gctl_data,
580 					    bp->bio_data, bp->bio_length);
581 					if (error != 0)
582 						bp->bio_error = error;
583 					break;
584 				case BIO_DELETE:
585 				case BIO_WRITE:
586 					break;
587 				}
588 			}
589 			G_GATE_LOGREQ(2, bp, "Request done.");
590 			g_io_deliver(bp, bp->bio_error);
591 		}
592 		return (error);
593 	    }
594 	}
595 	return (ENOIOCTL);
596 }
597 
598 static void
599 g_gate_device(void)
600 {
601 
602 	status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600,
603 	    G_GATE_CTL_NAME);
604 }
605 
606 static int
607 g_gate_modevent(module_t mod, int type, void *data)
608 {
609 	int error = 0;
610 
611 	switch (type) {
612 	case MOD_LOAD:
613 		mtx_init(&g_gate_list_mtx, "gg_list_lock", NULL, MTX_DEF);
614 		g_gate_device();
615 		break;
616 	case MOD_UNLOAD:
617 		mtx_lock(&g_gate_list_mtx);
618 		if (!LIST_EMPTY(&g_gate_list)) {
619 			mtx_unlock(&g_gate_list_mtx);
620 			error = EBUSY;
621 			break;
622 		}
623 		mtx_unlock(&g_gate_list_mtx);
624 		mtx_destroy(&g_gate_list_mtx);
625 		if (status_dev != 0)
626 			destroy_dev(status_dev);
627 		break;
628 	default:
629 		return (EOPNOTSUPP);
630 		break;
631 	}
632 
633 	return (error);
634 }
635 static moduledata_t g_gate_module = {
636 	G_GATE_MOD_NAME,
637 	g_gate_modevent,
638 	NULL
639 };
640 DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
641 DECLARE_GEOM_CLASS(g_gate_class, g_gate);
642