2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Copyright (c) 2009-2010 The FreeBSD Foundation
8 * Portions of this software were developed by Pawel Jakub Dawidek
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
42 #include <sys/fcntl.h>
43 #include <sys/linker.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
48 #include <sys/limits.h>
49 #include <sys/queue.h>
51 #include <sys/sysctl.h>
52 #include <sys/signalvar.h>
54 #include <machine/atomic.h>
56 #include <geom/geom.h>
57 #include <geom/gate/g_gate.h>
59 FEATURE(geom_gate, "GEOM Gate module");
61 static MALLOC_DEFINE(M_GATE, "gg_data", "GEOM Gate Data");
63 SYSCTL_DECL(_kern_geom);
64 static SYSCTL_NODE(_kern_geom, OID_AUTO, gate, CTLFLAG_RW, 0,
65 "GEOM_GATE configuration");
66 static int g_gate_debug = 0;
67 SYSCTL_INT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RWTUN, &g_gate_debug, 0,
69 static u_int g_gate_maxunits = 256;
70 SYSCTL_UINT(_kern_geom_gate, OID_AUTO, maxunits, CTLFLAG_RDTUN,
71 &g_gate_maxunits, 0, "Maximum number of ggate devices");
73 struct g_class g_gate_class = {
74 .name = G_GATE_CLASS_NAME,
78 static struct cdev *status_dev;
79 static d_ioctl_t g_gate_ioctl;
80 static struct cdevsw g_gate_cdevsw = {
81 .d_version = D_VERSION,
82 .d_ioctl = g_gate_ioctl,
83 .d_name = G_GATE_CTL_NAME
87 static struct g_gate_softc **g_gate_units;
88 static u_int g_gate_nunits;
89 static struct mtx g_gate_units_lock;
92 g_gate_destroy(struct g_gate_softc *sc, boolean_t force)
94 struct bio_queue_head queue;
95 struct g_provider *pp;
96 struct g_consumer *cp;
101 mtx_assert(&g_gate_units_lock, MA_OWNED);
102 pp = sc->sc_provider;
103 if (!force && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
104 mtx_unlock(&g_gate_units_lock);
107 mtx_unlock(&g_gate_units_lock);
108 mtx_lock(&sc->sc_queue_mtx);
109 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0)
110 sc->sc_flags |= G_GATE_FLAG_DESTROY;
112 mtx_unlock(&sc->sc_queue_mtx);
114 g_wither_provider(pp, ENXIO);
115 callout_drain(&sc->sc_callout);
117 mtx_lock(&sc->sc_queue_mtx);
118 while ((bp = bioq_takefirst(&sc->sc_inqueue)) != NULL) {
119 sc->sc_queue_count--;
120 bioq_insert_tail(&queue, bp);
122 while ((bp = bioq_takefirst(&sc->sc_outqueue)) != NULL) {
123 sc->sc_queue_count--;
124 bioq_insert_tail(&queue, bp);
126 mtx_unlock(&sc->sc_queue_mtx);
128 while ((bp = bioq_takefirst(&queue)) != NULL) {
129 G_GATE_LOGREQ(1, bp, "Request canceled.");
130 g_io_deliver(bp, ENXIO);
132 mtx_lock(&g_gate_units_lock);
133 /* One reference is ours. */
135 while (sc->sc_ref > 0)
136 msleep(&sc->sc_ref, &g_gate_units_lock, 0, "gg:destroy", 0);
137 g_gate_units[sc->sc_unit] = NULL;
138 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?"));
140 mtx_unlock(&g_gate_units_lock);
141 mtx_destroy(&sc->sc_queue_mtx);
143 if ((cp = sc->sc_readcons) != NULL) {
144 sc->sc_readcons = NULL;
145 (void)g_access(cp, -1, 0, 0);
147 g_destroy_consumer(cp);
149 G_GATE_DEBUG(1, "Device %s destroyed.", gp->name);
151 g_wither_geom(gp, ENXIO);
152 sc->sc_provider = NULL;
158 g_gate_access(struct g_provider *pp, int dr, int dw, int de)
160 struct g_gate_softc *sc;
162 if (dr <= 0 && dw <= 0 && de <= 0)
164 sc = pp->geom->softc;
165 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
167 /* XXX: Hack to allow read-only mounts. */
169 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0)
172 if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0)
178 g_gate_queue_io(struct bio *bp)
180 struct g_gate_softc *sc;
182 sc = bp->bio_to->geom->softc;
183 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
184 g_io_deliver(bp, ENXIO);
188 mtx_lock(&sc->sc_queue_mtx);
190 if (sc->sc_queue_size > 0 && sc->sc_queue_count > sc->sc_queue_size) {
191 mtx_unlock(&sc->sc_queue_mtx);
192 G_GATE_LOGREQ(1, bp, "Queue full, request canceled.");
193 g_io_deliver(bp, ENOMEM);
197 bp->bio_driver1 = (void *)sc->sc_seq;
199 sc->sc_queue_count++;
201 bioq_insert_tail(&sc->sc_inqueue, bp);
204 mtx_unlock(&sc->sc_queue_mtx);
208 g_gate_done(struct bio *cbp)
212 pbp = cbp->bio_parent;
213 if (cbp->bio_error == 0) {
214 pbp->bio_completed = cbp->bio_completed;
217 g_io_deliver(pbp, 0);
219 /* If direct read failed, pass it through userland daemon. */
222 g_gate_queue_io(pbp);
227 g_gate_start(struct bio *pbp)
229 struct g_gate_softc *sc;
231 sc = pbp->bio_to->geom->softc;
232 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
233 g_io_deliver(pbp, ENXIO);
236 G_GATE_LOGREQ(2, pbp, "Request received.");
237 switch (pbp->bio_cmd) {
239 if (sc->sc_readcons != NULL) {
242 cbp = g_clone_bio(pbp);
244 g_io_deliver(pbp, ENOMEM);
247 cbp->bio_done = g_gate_done;
248 cbp->bio_offset = pbp->bio_offset + sc->sc_readoffset;
249 cbp->bio_to = sc->sc_readcons->provider;
250 g_io_request(cbp, sc->sc_readcons);
257 /* XXX: Hack to allow read-only mounts. */
258 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
259 g_io_deliver(pbp, EPERM);
265 G_GATE_LOGREQ(2, pbp, "Ignoring request.");
266 g_io_deliver(pbp, EOPNOTSUPP);
270 g_gate_queue_io(pbp);
273 static struct g_gate_softc *
274 g_gate_hold(int unit, const char *name)
276 struct g_gate_softc *sc = NULL;
278 mtx_lock(&g_gate_units_lock);
279 if (unit >= 0 && unit < g_gate_maxunits)
280 sc = g_gate_units[unit];
281 else if (unit == G_GATE_NAME_GIVEN) {
282 KASSERT(name != NULL, ("name is NULL"));
283 for (unit = 0; unit < g_gate_maxunits; unit++) {
284 if (g_gate_units[unit] == NULL)
287 g_gate_units[unit]->sc_provider->name) != 0) {
290 sc = g_gate_units[unit];
296 mtx_unlock(&g_gate_units_lock);
301 g_gate_release(struct g_gate_softc *sc)
304 g_topology_assert_not();
305 mtx_lock(&g_gate_units_lock);
307 KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name));
308 if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
310 mtx_unlock(&g_gate_units_lock);
314 g_gate_getunit(int unit, int *errorp)
317 mtx_assert(&g_gate_units_lock, MA_OWNED);
319 if (unit >= g_gate_maxunits)
321 else if (g_gate_units[unit] == NULL)
326 for (unit = 0; unit < g_gate_maxunits; unit++) {
327 if (g_gate_units[unit] == NULL)
336 g_gate_guard(void *arg)
338 struct bio_queue_head queue;
339 struct g_gate_softc *sc;
340 struct bintime curtime;
341 struct bio *bp, *bp2;
345 g_gate_hold(sc->sc_unit, NULL);
347 mtx_lock(&sc->sc_queue_mtx);
348 TAILQ_FOREACH_SAFE(bp, &sc->sc_inqueue.queue, bio_queue, bp2) {
349 if (curtime.sec - bp->bio_t0.sec < 5)
351 bioq_remove(&sc->sc_inqueue, bp);
352 sc->sc_queue_count--;
353 bioq_insert_tail(&queue, bp);
355 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, bp2) {
356 if (curtime.sec - bp->bio_t0.sec < 5)
358 bioq_remove(&sc->sc_outqueue, bp);
359 sc->sc_queue_count--;
360 bioq_insert_tail(&queue, bp);
362 mtx_unlock(&sc->sc_queue_mtx);
363 while ((bp = bioq_takefirst(&queue)) != NULL) {
364 G_GATE_LOGREQ(1, bp, "Request timeout.");
365 g_io_deliver(bp, EIO);
367 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) {
368 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
375 g_gate_orphan(struct g_consumer *cp)
377 struct g_gate_softc *sc;
385 KASSERT(cp == sc->sc_readcons, ("cp=%p sc_readcons=%p", cp,
387 sc->sc_readcons = NULL;
388 G_GATE_DEBUG(1, "Destroying read consumer on provider %s orphan.",
390 (void)g_access(cp, -1, 0, 0);
392 g_destroy_consumer(cp);
396 g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
397 struct g_consumer *cp, struct g_provider *pp)
399 struct g_gate_softc *sc;
402 if (sc == NULL || pp != NULL || cp != NULL)
404 sc = g_gate_hold(sc->sc_unit, NULL);
407 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
408 sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only");
409 } else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) {
410 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
413 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
416 if (sc->sc_readcons != NULL) {
417 sbuf_printf(sb, "%s<read_offset>%jd</read_offset>\n",
418 indent, (intmax_t)sc->sc_readoffset);
419 sbuf_printf(sb, "%s<read_provider>%s</read_provider>\n",
420 indent, sc->sc_readcons->provider->name);
422 sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout);
423 sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info);
424 sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent,
426 sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent,
428 sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref);
429 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, sc->sc_unit);
436 g_gate_create(struct g_gate_ctl_create *ggio)
438 struct g_gate_softc *sc;
440 struct g_provider *pp, *ropp;
441 struct g_consumer *cp;
445 if (ggio->gctl_mediasize <= 0) {
446 G_GATE_DEBUG(1, "Invalid media size.");
449 if (ggio->gctl_sectorsize <= 0) {
450 G_GATE_DEBUG(1, "Invalid sector size.");
453 if (!powerof2(ggio->gctl_sectorsize)) {
454 G_GATE_DEBUG(1, "Invalid sector size.");
457 if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) {
458 G_GATE_DEBUG(1, "Invalid media size.");
461 if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 &&
462 (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) {
463 G_GATE_DEBUG(1, "Invalid flags.");
466 if (ggio->gctl_unit != G_GATE_UNIT_AUTO &&
467 ggio->gctl_unit != G_GATE_NAME_GIVEN &&
468 ggio->gctl_unit < 0) {
469 G_GATE_DEBUG(1, "Invalid unit number.");
472 if (ggio->gctl_unit == G_GATE_NAME_GIVEN &&
473 ggio->gctl_name[0] == '\0') {
474 G_GATE_DEBUG(1, "No device name.");
478 sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO);
479 sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS);
480 strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
482 bioq_init(&sc->sc_inqueue);
483 bioq_init(&sc->sc_outqueue);
484 mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF);
485 sc->sc_queue_count = 0;
486 sc->sc_queue_size = ggio->gctl_maxcount;
487 if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE)
488 sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE;
489 sc->sc_timeout = ggio->gctl_timeout;
490 callout_init(&sc->sc_callout, 1);
492 mtx_lock(&g_gate_units_lock);
493 sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error);
496 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
497 snprintf(name, sizeof(name), "%s", ggio->gctl_name);
499 snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME,
502 /* Check for name collision. */
503 for (unit = 0; unit < g_gate_maxunits; unit++) {
504 if (g_gate_units[unit] == NULL)
506 if (strcmp(name, g_gate_units[unit]->sc_name) != 0)
512 g_gate_units[sc->sc_unit] = sc;
514 mtx_unlock(&g_gate_units_lock);
518 if (ggio->gctl_readprov[0] == '\0') {
521 ropp = g_provider_by_name(ggio->gctl_readprov);
523 G_GATE_DEBUG(1, "Provider %s doesn't exist.",
524 ggio->gctl_readprov);
528 if ((ggio->gctl_readoffset % ggio->gctl_sectorsize) != 0) {
529 G_GATE_DEBUG(1, "Invalid read offset.");
533 if (ggio->gctl_mediasize + ggio->gctl_readoffset >
535 G_GATE_DEBUG(1, "Invalid read offset or media size.");
541 gp = g_new_geomf(&g_gate_class, "%s", name);
542 gp->start = g_gate_start;
543 gp->access = g_gate_access;
544 gp->orphan = g_gate_orphan;
545 gp->dumpconf = g_gate_dumpconf;
549 cp = g_new_consumer(gp);
550 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
551 error = g_attach(cp, ropp);
553 G_GATE_DEBUG(1, "Unable to attach to %s.", ropp->name);
556 error = g_access(cp, 1, 0, 0);
558 G_GATE_DEBUG(1, "Unable to access %s.", ropp->name);
562 sc->sc_readcons = cp;
563 sc->sc_readoffset = ggio->gctl_readoffset;
566 ggio->gctl_unit = sc->sc_unit;
568 pp = g_new_providerf(gp, "%s", name);
569 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
570 pp->mediasize = ggio->gctl_mediasize;
571 pp->sectorsize = ggio->gctl_sectorsize;
572 sc->sc_provider = pp;
573 g_error_provider(pp, 0);
576 mtx_lock(&g_gate_units_lock);
577 sc->sc_name = sc->sc_provider->name;
578 mtx_unlock(&g_gate_units_lock);
579 G_GATE_DEBUG(1, "Device %s created.", gp->name);
581 if (sc->sc_timeout > 0) {
582 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
587 g_destroy_consumer(cp);
591 mtx_lock(&g_gate_units_lock);
592 g_gate_units[sc->sc_unit] = NULL;
593 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?"));
596 mtx_unlock(&g_gate_units_lock);
597 mtx_destroy(&sc->sc_queue_mtx);
603 g_gate_modify(struct g_gate_softc *sc, struct g_gate_ctl_modify *ggio)
605 struct g_provider *pp;
606 struct g_consumer *cp;
609 if ((ggio->gctl_modify & GG_MODIFY_MEDIASIZE) != 0) {
610 if (ggio->gctl_mediasize <= 0) {
611 G_GATE_DEBUG(1, "Invalid media size.");
614 pp = sc->sc_provider;
615 if ((ggio->gctl_mediasize % pp->sectorsize) != 0) {
616 G_GATE_DEBUG(1, "Invalid media size.");
623 if ((ggio->gctl_modify & GG_MODIFY_INFO) != 0)
624 (void)strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
628 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) {
630 if (sc->sc_readcons != NULL) {
631 cp = sc->sc_readcons;
632 sc->sc_readcons = NULL;
633 (void)g_access(cp, -1, 0, 0);
635 g_destroy_consumer(cp);
637 if (ggio->gctl_readprov[0] != '\0') {
638 pp = g_provider_by_name(ggio->gctl_readprov);
641 G_GATE_DEBUG(1, "Provider %s doesn't exist.",
642 ggio->gctl_readprov);
645 cp = g_new_consumer(sc->sc_provider->geom);
646 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
647 error = g_attach(cp, pp);
649 G_GATE_DEBUG(1, "Unable to attach to %s.",
652 error = g_access(cp, 1, 0, 0);
654 G_GATE_DEBUG(1, "Unable to access %s.",
660 g_destroy_consumer(cp);
666 cp = sc->sc_readcons;
669 if ((ggio->gctl_modify & GG_MODIFY_READOFFSET) != 0) {
671 G_GATE_DEBUG(1, "No read provider.");
674 pp = sc->sc_provider;
675 if ((ggio->gctl_readoffset % pp->sectorsize) != 0) {
676 G_GATE_DEBUG(1, "Invalid read offset.");
679 if (pp->mediasize + ggio->gctl_readoffset >
680 cp->provider->mediasize) {
681 G_GATE_DEBUG(1, "Invalid read offset or media size.");
684 sc->sc_readoffset = ggio->gctl_readoffset;
687 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) {
688 sc->sc_readcons = cp;
695 #define G_GATE_CHECK_VERSION(ggio) do { \
696 if ((ggio)->gctl_version != G_GATE_VERSION) { \
697 printf("Version mismatch %d != %d.\n", \
698 ggio->gctl_version, G_GATE_VERSION); \
703 g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
705 struct g_gate_softc *sc;
709 G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr,
713 case G_GATE_CMD_CREATE:
715 struct g_gate_ctl_create *ggio = (void *)addr;
717 G_GATE_CHECK_VERSION(ggio);
718 error = g_gate_create(ggio);
720 * Reset TDP_GEOM flag.
721 * There are pending events for sure, because we just created
722 * new provider and other classes want to taste it, but we
723 * cannot answer on I/O requests until we're here.
725 td->td_pflags &= ~TDP_GEOM;
728 case G_GATE_CMD_MODIFY:
730 struct g_gate_ctl_modify *ggio = (void *)addr;
732 G_GATE_CHECK_VERSION(ggio);
733 sc = g_gate_hold(ggio->gctl_unit, NULL);
736 error = g_gate_modify(sc, ggio);
740 case G_GATE_CMD_DESTROY:
742 struct g_gate_ctl_destroy *ggio = (void *)addr;
744 G_GATE_CHECK_VERSION(ggio);
745 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
749 mtx_lock(&g_gate_units_lock);
750 error = g_gate_destroy(sc, ggio->gctl_force);
756 case G_GATE_CMD_CANCEL:
758 struct g_gate_ctl_cancel *ggio = (void *)addr;
759 struct bio *tbp, *lbp;
761 G_GATE_CHECK_VERSION(ggio);
762 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
766 mtx_lock(&sc->sc_queue_mtx);
767 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) {
768 if (ggio->gctl_seq == 0 ||
769 ggio->gctl_seq == (uintptr_t)bp->bio_driver1) {
770 G_GATE_LOGREQ(1, bp, "Request canceled.");
771 bioq_remove(&sc->sc_outqueue, bp);
773 * Be sure to put requests back onto incoming
774 * queue in the proper order.
777 bioq_insert_head(&sc->sc_inqueue, bp);
779 TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue,
784 * If only one request was canceled, leave now.
786 if (ggio->gctl_seq != 0)
790 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
791 ggio->gctl_unit = sc->sc_unit;
792 mtx_unlock(&sc->sc_queue_mtx);
796 case G_GATE_CMD_START:
798 struct g_gate_ctl_io *ggio = (void *)addr;
800 G_GATE_CHECK_VERSION(ggio);
801 sc = g_gate_hold(ggio->gctl_unit, NULL);
806 mtx_lock(&sc->sc_queue_mtx);
807 bp = bioq_first(&sc->sc_inqueue);
810 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
811 ggio->gctl_error = ECANCELED;
812 mtx_unlock(&sc->sc_queue_mtx);
815 if (msleep(sc, &sc->sc_queue_mtx,
816 PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) {
817 ggio->gctl_error = ECANCELED;
821 ggio->gctl_cmd = bp->bio_cmd;
822 if (bp->bio_cmd == BIO_WRITE &&
823 bp->bio_length > ggio->gctl_length) {
824 mtx_unlock(&sc->sc_queue_mtx);
825 ggio->gctl_length = bp->bio_length;
826 ggio->gctl_error = ENOMEM;
829 bioq_remove(&sc->sc_inqueue, bp);
830 bioq_insert_tail(&sc->sc_outqueue, bp);
831 mtx_unlock(&sc->sc_queue_mtx);
833 ggio->gctl_seq = (uintptr_t)bp->bio_driver1;
834 ggio->gctl_offset = bp->bio_offset;
835 ggio->gctl_length = bp->bio_length;
837 switch (bp->bio_cmd) {
843 error = copyout(bp->bio_data, ggio->gctl_data,
846 mtx_lock(&sc->sc_queue_mtx);
847 bioq_remove(&sc->sc_outqueue, bp);
848 bioq_insert_head(&sc->sc_inqueue, bp);
849 mtx_unlock(&sc->sc_queue_mtx);
858 case G_GATE_CMD_DONE:
860 struct g_gate_ctl_io *ggio = (void *)addr;
862 G_GATE_CHECK_VERSION(ggio);
863 sc = g_gate_hold(ggio->gctl_unit, NULL);
867 mtx_lock(&sc->sc_queue_mtx);
868 TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) {
869 if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1)
873 bioq_remove(&sc->sc_outqueue, bp);
874 sc->sc_queue_count--;
876 mtx_unlock(&sc->sc_queue_mtx);
879 * Request was probably canceled.
883 if (ggio->gctl_error == EAGAIN) {
885 G_GATE_LOGREQ(1, bp, "Request desisted.");
886 mtx_lock(&sc->sc_queue_mtx);
887 sc->sc_queue_count++;
888 bioq_insert_head(&sc->sc_inqueue, bp);
890 mtx_unlock(&sc->sc_queue_mtx);
892 bp->bio_error = ggio->gctl_error;
893 if (bp->bio_error == 0) {
894 bp->bio_completed = bp->bio_length;
895 switch (bp->bio_cmd) {
897 error = copyin(ggio->gctl_data,
898 bp->bio_data, bp->bio_length);
900 bp->bio_error = error;
908 G_GATE_LOGREQ(2, bp, "Request done.");
909 g_io_deliver(bp, bp->bio_error);
923 status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600,
928 g_gate_modevent(module_t mod, int type, void *data)
934 mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF);
935 g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]),
936 M_GATE, M_WAITOK | M_ZERO);
941 mtx_lock(&g_gate_units_lock);
942 if (g_gate_nunits > 0) {
943 mtx_unlock(&g_gate_units_lock);
947 mtx_unlock(&g_gate_units_lock);
948 mtx_destroy(&g_gate_units_lock);
949 if (status_dev != NULL)
950 destroy_dev(status_dev);
951 free(g_gate_units, M_GATE);
960 static moduledata_t g_gate_module = {
965 DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
966 DECLARE_GEOM_CLASS(g_gate_class, g_gate);
967 MODULE_VERSION(geom_gate, 0);