2 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * Copyright (c) 2009-2010 The FreeBSD Foundation
6 * Portions of this software were developed by Pawel Jakub Dawidek
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
40 #include <sys/fcntl.h>
41 #include <sys/linker.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
46 #include <sys/limits.h>
47 #include <sys/queue.h>
49 #include <sys/sysctl.h>
50 #include <sys/signalvar.h>
52 #include <machine/atomic.h>
54 #include <geom/geom.h>
55 #include <geom/gate/g_gate.h>
57 FEATURE(geom_gate, "GEOM Gate module");
59 static MALLOC_DEFINE(M_GATE, "gg_data", "GEOM Gate Data");
61 SYSCTL_DECL(_kern_geom);
62 static SYSCTL_NODE(_kern_geom, OID_AUTO, gate, CTLFLAG_RW, 0,
63 "GEOM_GATE configuration");
64 static int g_gate_debug = 0;
65 SYSCTL_INT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RWTUN, &g_gate_debug, 0,
67 static u_int g_gate_maxunits = 256;
68 SYSCTL_UINT(_kern_geom_gate, OID_AUTO, maxunits, CTLFLAG_RDTUN,
69 &g_gate_maxunits, 0, "Maximum number of ggate devices");
71 struct g_class g_gate_class = {
72 .name = G_GATE_CLASS_NAME,
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
85 static struct g_gate_softc **g_gate_units;
86 static u_int g_gate_nunits;
87 static struct mtx g_gate_units_lock;
90 g_gate_destroy(struct g_gate_softc *sc, boolean_t force)
92 struct bio_queue_head queue;
93 struct g_provider *pp;
94 struct g_consumer *cp;
99 mtx_assert(&g_gate_units_lock, MA_OWNED);
100 pp = sc->sc_provider;
101 if (!force && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
102 mtx_unlock(&g_gate_units_lock);
105 mtx_unlock(&g_gate_units_lock);
106 mtx_lock(&sc->sc_queue_mtx);
107 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0)
108 sc->sc_flags |= G_GATE_FLAG_DESTROY;
110 mtx_unlock(&sc->sc_queue_mtx);
112 g_wither_provider(pp, ENXIO);
113 callout_drain(&sc->sc_callout);
115 mtx_lock(&sc->sc_queue_mtx);
116 while ((bp = bioq_takefirst(&sc->sc_inqueue)) != NULL) {
117 sc->sc_queue_count--;
118 bioq_insert_tail(&queue, bp);
120 while ((bp = bioq_takefirst(&sc->sc_outqueue)) != NULL) {
121 sc->sc_queue_count--;
122 bioq_insert_tail(&queue, bp);
124 mtx_unlock(&sc->sc_queue_mtx);
126 while ((bp = bioq_takefirst(&queue)) != NULL) {
127 G_GATE_LOGREQ(1, bp, "Request canceled.");
128 g_io_deliver(bp, ENXIO);
130 mtx_lock(&g_gate_units_lock);
131 /* One reference is ours. */
133 while (sc->sc_ref > 0)
134 msleep(&sc->sc_ref, &g_gate_units_lock, 0, "gg:destroy", 0);
135 g_gate_units[sc->sc_unit] = NULL;
136 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?"));
138 mtx_unlock(&g_gate_units_lock);
139 mtx_destroy(&sc->sc_queue_mtx);
141 if ((cp = sc->sc_readcons) != NULL) {
142 sc->sc_readcons = NULL;
143 (void)g_access(cp, -1, 0, 0);
145 g_destroy_consumer(cp);
147 G_GATE_DEBUG(1, "Device %s destroyed.", gp->name);
149 g_wither_geom(gp, ENXIO);
150 sc->sc_provider = NULL;
156 g_gate_access(struct g_provider *pp, int dr, int dw, int de)
158 struct g_gate_softc *sc;
160 if (dr <= 0 && dw <= 0 && de <= 0)
162 sc = pp->geom->softc;
163 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
165 /* XXX: Hack to allow read-only mounts. */
167 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0)
170 if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0)
176 g_gate_queue_io(struct bio *bp)
178 struct g_gate_softc *sc;
180 sc = bp->bio_to->geom->softc;
181 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
182 g_io_deliver(bp, ENXIO);
186 mtx_lock(&sc->sc_queue_mtx);
188 if (sc->sc_queue_size > 0 && sc->sc_queue_count > sc->sc_queue_size) {
189 mtx_unlock(&sc->sc_queue_mtx);
190 G_GATE_LOGREQ(1, bp, "Queue full, request canceled.");
191 g_io_deliver(bp, ENOMEM);
195 bp->bio_driver1 = (void *)sc->sc_seq;
197 sc->sc_queue_count++;
199 bioq_insert_tail(&sc->sc_inqueue, bp);
202 mtx_unlock(&sc->sc_queue_mtx);
206 g_gate_done(struct bio *cbp)
210 pbp = cbp->bio_parent;
211 if (cbp->bio_error == 0) {
212 pbp->bio_completed = cbp->bio_completed;
215 g_io_deliver(pbp, 0);
217 /* If direct read failed, pass it through userland daemon. */
220 g_gate_queue_io(pbp);
225 g_gate_start(struct bio *pbp)
227 struct g_gate_softc *sc;
229 sc = pbp->bio_to->geom->softc;
230 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
231 g_io_deliver(pbp, ENXIO);
234 G_GATE_LOGREQ(2, pbp, "Request received.");
235 switch (pbp->bio_cmd) {
237 if (sc->sc_readcons != NULL) {
240 cbp = g_clone_bio(pbp);
242 g_io_deliver(pbp, ENOMEM);
245 cbp->bio_done = g_gate_done;
246 cbp->bio_offset = pbp->bio_offset + sc->sc_readoffset;
247 cbp->bio_to = sc->sc_readcons->provider;
248 g_io_request(cbp, sc->sc_readcons);
255 /* XXX: Hack to allow read-only mounts. */
256 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
257 g_io_deliver(pbp, EPERM);
263 G_GATE_LOGREQ(2, pbp, "Ignoring request.");
264 g_io_deliver(pbp, EOPNOTSUPP);
268 g_gate_queue_io(pbp);
271 static struct g_gate_softc *
272 g_gate_hold(int unit, const char *name)
274 struct g_gate_softc *sc = NULL;
276 mtx_lock(&g_gate_units_lock);
277 if (unit >= 0 && unit < g_gate_maxunits)
278 sc = g_gate_units[unit];
279 else if (unit == G_GATE_NAME_GIVEN) {
280 KASSERT(name != NULL, ("name is NULL"));
281 for (unit = 0; unit < g_gate_maxunits; unit++) {
282 if (g_gate_units[unit] == NULL)
285 g_gate_units[unit]->sc_provider->name) != 0) {
288 sc = g_gate_units[unit];
294 mtx_unlock(&g_gate_units_lock);
299 g_gate_release(struct g_gate_softc *sc)
302 g_topology_assert_not();
303 mtx_lock(&g_gate_units_lock);
305 KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name));
306 if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
308 mtx_unlock(&g_gate_units_lock);
312 g_gate_getunit(int unit, int *errorp)
315 mtx_assert(&g_gate_units_lock, MA_OWNED);
317 if (unit >= g_gate_maxunits)
319 else if (g_gate_units[unit] == NULL)
324 for (unit = 0; unit < g_gate_maxunits; unit++) {
325 if (g_gate_units[unit] == NULL)
334 g_gate_guard(void *arg)
336 struct bio_queue_head queue;
337 struct g_gate_softc *sc;
338 struct bintime curtime;
339 struct bio *bp, *bp2;
343 g_gate_hold(sc->sc_unit, NULL);
345 mtx_lock(&sc->sc_queue_mtx);
346 TAILQ_FOREACH_SAFE(bp, &sc->sc_inqueue.queue, bio_queue, bp2) {
347 if (curtime.sec - bp->bio_t0.sec < 5)
349 bioq_remove(&sc->sc_inqueue, bp);
350 sc->sc_queue_count--;
351 bioq_insert_tail(&queue, bp);
353 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, bp2) {
354 if (curtime.sec - bp->bio_t0.sec < 5)
356 bioq_remove(&sc->sc_outqueue, bp);
357 sc->sc_queue_count--;
358 bioq_insert_tail(&queue, bp);
360 mtx_unlock(&sc->sc_queue_mtx);
361 while ((bp = bioq_takefirst(&queue)) != NULL) {
362 G_GATE_LOGREQ(1, bp, "Request timeout.");
363 g_io_deliver(bp, EIO);
365 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) {
366 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
373 g_gate_orphan(struct g_consumer *cp)
375 struct g_gate_softc *sc;
383 KASSERT(cp == sc->sc_readcons, ("cp=%p sc_readcons=%p", cp,
385 sc->sc_readcons = NULL;
386 G_GATE_DEBUG(1, "Destroying read consumer on provider %s orphan.",
388 (void)g_access(cp, -1, 0, 0);
390 g_destroy_consumer(cp);
394 g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
395 struct g_consumer *cp, struct g_provider *pp)
397 struct g_gate_softc *sc;
400 if (sc == NULL || pp != NULL || cp != NULL)
402 sc = g_gate_hold(sc->sc_unit, NULL);
405 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
406 sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only");
407 } else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) {
408 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
411 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
414 if (sc->sc_readcons != NULL) {
415 sbuf_printf(sb, "%s<read_offset>%jd</read_offset>\n",
416 indent, (intmax_t)sc->sc_readoffset);
417 sbuf_printf(sb, "%s<read_provider>%s</read_provider>\n",
418 indent, sc->sc_readcons->provider->name);
420 sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout);
421 sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info);
422 sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent,
424 sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent,
426 sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref);
427 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, sc->sc_unit);
434 g_gate_create(struct g_gate_ctl_create *ggio)
436 struct g_gate_softc *sc;
438 struct g_provider *pp, *ropp;
439 struct g_consumer *cp;
443 if (ggio->gctl_mediasize <= 0) {
444 G_GATE_DEBUG(1, "Invalid media size.");
447 if (ggio->gctl_sectorsize <= 0) {
448 G_GATE_DEBUG(1, "Invalid sector size.");
451 if (!powerof2(ggio->gctl_sectorsize)) {
452 G_GATE_DEBUG(1, "Invalid sector size.");
455 if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) {
456 G_GATE_DEBUG(1, "Invalid media size.");
459 if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 &&
460 (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) {
461 G_GATE_DEBUG(1, "Invalid flags.");
464 if (ggio->gctl_unit != G_GATE_UNIT_AUTO &&
465 ggio->gctl_unit != G_GATE_NAME_GIVEN &&
466 ggio->gctl_unit < 0) {
467 G_GATE_DEBUG(1, "Invalid unit number.");
470 if (ggio->gctl_unit == G_GATE_NAME_GIVEN &&
471 ggio->gctl_name[0] == '\0') {
472 G_GATE_DEBUG(1, "No device name.");
476 sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO);
477 sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS);
478 strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
480 bioq_init(&sc->sc_inqueue);
481 bioq_init(&sc->sc_outqueue);
482 mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF);
483 sc->sc_queue_count = 0;
484 sc->sc_queue_size = ggio->gctl_maxcount;
485 if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE)
486 sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE;
487 sc->sc_timeout = ggio->gctl_timeout;
488 callout_init(&sc->sc_callout, 1);
490 mtx_lock(&g_gate_units_lock);
491 sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error);
494 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
495 snprintf(name, sizeof(name), "%s", ggio->gctl_name);
497 snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME,
500 /* Check for name collision. */
501 for (unit = 0; unit < g_gate_maxunits; unit++) {
502 if (g_gate_units[unit] == NULL)
504 if (strcmp(name, g_gate_units[unit]->sc_name) != 0)
510 g_gate_units[sc->sc_unit] = sc;
512 mtx_unlock(&g_gate_units_lock);
516 if (ggio->gctl_readprov[0] == '\0') {
519 ropp = g_provider_by_name(ggio->gctl_readprov);
521 G_GATE_DEBUG(1, "Provider %s doesn't exist.",
522 ggio->gctl_readprov);
526 if ((ggio->gctl_readoffset % ggio->gctl_sectorsize) != 0) {
527 G_GATE_DEBUG(1, "Invalid read offset.");
531 if (ggio->gctl_mediasize + ggio->gctl_readoffset >
533 G_GATE_DEBUG(1, "Invalid read offset or media size.");
539 gp = g_new_geomf(&g_gate_class, "%s", name);
540 gp->start = g_gate_start;
541 gp->access = g_gate_access;
542 gp->orphan = g_gate_orphan;
543 gp->dumpconf = g_gate_dumpconf;
547 cp = g_new_consumer(gp);
548 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
549 error = g_attach(cp, ropp);
551 G_GATE_DEBUG(1, "Unable to attach to %s.", ropp->name);
554 error = g_access(cp, 1, 0, 0);
556 G_GATE_DEBUG(1, "Unable to access %s.", ropp->name);
560 sc->sc_readcons = cp;
561 sc->sc_readoffset = ggio->gctl_readoffset;
564 ggio->gctl_unit = sc->sc_unit;
566 pp = g_new_providerf(gp, "%s", name);
567 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
568 pp->mediasize = ggio->gctl_mediasize;
569 pp->sectorsize = ggio->gctl_sectorsize;
570 sc->sc_provider = pp;
571 g_error_provider(pp, 0);
574 mtx_lock(&g_gate_units_lock);
575 sc->sc_name = sc->sc_provider->name;
576 mtx_unlock(&g_gate_units_lock);
577 G_GATE_DEBUG(1, "Device %s created.", gp->name);
579 if (sc->sc_timeout > 0) {
580 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
585 g_destroy_consumer(cp);
589 mtx_lock(&g_gate_units_lock);
590 g_gate_units[sc->sc_unit] = NULL;
591 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?"));
594 mtx_unlock(&g_gate_units_lock);
595 mtx_destroy(&sc->sc_queue_mtx);
601 g_gate_modify(struct g_gate_softc *sc, struct g_gate_ctl_modify *ggio)
603 struct g_provider *pp;
604 struct g_consumer *cp;
607 if ((ggio->gctl_modify & GG_MODIFY_MEDIASIZE) != 0) {
608 if (ggio->gctl_mediasize <= 0) {
609 G_GATE_DEBUG(1, "Invalid media size.");
612 pp = sc->sc_provider;
613 if ((ggio->gctl_mediasize % pp->sectorsize) != 0) {
614 G_GATE_DEBUG(1, "Invalid media size.");
621 if ((ggio->gctl_modify & GG_MODIFY_INFO) != 0)
622 (void)strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
626 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) {
628 if (sc->sc_readcons != NULL) {
629 cp = sc->sc_readcons;
630 sc->sc_readcons = NULL;
631 (void)g_access(cp, -1, 0, 0);
633 g_destroy_consumer(cp);
635 if (ggio->gctl_readprov[0] != '\0') {
636 pp = g_provider_by_name(ggio->gctl_readprov);
639 G_GATE_DEBUG(1, "Provider %s doesn't exist.",
640 ggio->gctl_readprov);
643 cp = g_new_consumer(sc->sc_provider->geom);
644 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
645 error = g_attach(cp, pp);
647 G_GATE_DEBUG(1, "Unable to attach to %s.",
650 error = g_access(cp, 1, 0, 0);
652 G_GATE_DEBUG(1, "Unable to access %s.",
658 g_destroy_consumer(cp);
664 cp = sc->sc_readcons;
667 if ((ggio->gctl_modify & GG_MODIFY_READOFFSET) != 0) {
669 G_GATE_DEBUG(1, "No read provider.");
672 pp = sc->sc_provider;
673 if ((ggio->gctl_readoffset % pp->sectorsize) != 0) {
674 G_GATE_DEBUG(1, "Invalid read offset.");
677 if (pp->mediasize + ggio->gctl_readoffset >
678 cp->provider->mediasize) {
679 G_GATE_DEBUG(1, "Invalid read offset or media size.");
682 sc->sc_readoffset = ggio->gctl_readoffset;
685 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) {
686 sc->sc_readcons = cp;
693 #define G_GATE_CHECK_VERSION(ggio) do { \
694 if ((ggio)->gctl_version != G_GATE_VERSION) { \
695 printf("Version mismatch %d != %d.\n", \
696 ggio->gctl_version, G_GATE_VERSION); \
701 g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
703 struct g_gate_softc *sc;
707 G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr,
711 case G_GATE_CMD_CREATE:
713 struct g_gate_ctl_create *ggio = (void *)addr;
715 G_GATE_CHECK_VERSION(ggio);
716 error = g_gate_create(ggio);
718 * Reset TDP_GEOM flag.
719 * There are pending events for sure, because we just created
720 * new provider and other classes want to taste it, but we
721 * cannot answer on I/O requests until we're here.
723 td->td_pflags &= ~TDP_GEOM;
726 case G_GATE_CMD_MODIFY:
728 struct g_gate_ctl_modify *ggio = (void *)addr;
730 G_GATE_CHECK_VERSION(ggio);
731 sc = g_gate_hold(ggio->gctl_unit, NULL);
734 error = g_gate_modify(sc, ggio);
738 case G_GATE_CMD_DESTROY:
740 struct g_gate_ctl_destroy *ggio = (void *)addr;
742 G_GATE_CHECK_VERSION(ggio);
743 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
747 mtx_lock(&g_gate_units_lock);
748 error = g_gate_destroy(sc, ggio->gctl_force);
754 case G_GATE_CMD_CANCEL:
756 struct g_gate_ctl_cancel *ggio = (void *)addr;
757 struct bio *tbp, *lbp;
759 G_GATE_CHECK_VERSION(ggio);
760 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
764 mtx_lock(&sc->sc_queue_mtx);
765 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) {
766 if (ggio->gctl_seq == 0 ||
767 ggio->gctl_seq == (uintptr_t)bp->bio_driver1) {
768 G_GATE_LOGREQ(1, bp, "Request canceled.");
769 bioq_remove(&sc->sc_outqueue, bp);
771 * Be sure to put requests back onto incoming
772 * queue in the proper order.
775 bioq_insert_head(&sc->sc_inqueue, bp);
777 TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue,
782 * If only one request was canceled, leave now.
784 if (ggio->gctl_seq != 0)
788 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
789 ggio->gctl_unit = sc->sc_unit;
790 mtx_unlock(&sc->sc_queue_mtx);
794 case G_GATE_CMD_START:
796 struct g_gate_ctl_io *ggio = (void *)addr;
798 G_GATE_CHECK_VERSION(ggio);
799 sc = g_gate_hold(ggio->gctl_unit, NULL);
804 mtx_lock(&sc->sc_queue_mtx);
805 bp = bioq_first(&sc->sc_inqueue);
808 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
809 ggio->gctl_error = ECANCELED;
810 mtx_unlock(&sc->sc_queue_mtx);
813 if (msleep(sc, &sc->sc_queue_mtx,
814 PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) {
815 ggio->gctl_error = ECANCELED;
819 ggio->gctl_cmd = bp->bio_cmd;
820 if (bp->bio_cmd == BIO_WRITE &&
821 bp->bio_length > ggio->gctl_length) {
822 mtx_unlock(&sc->sc_queue_mtx);
823 ggio->gctl_length = bp->bio_length;
824 ggio->gctl_error = ENOMEM;
827 bioq_remove(&sc->sc_inqueue, bp);
828 bioq_insert_tail(&sc->sc_outqueue, bp);
829 mtx_unlock(&sc->sc_queue_mtx);
831 ggio->gctl_seq = (uintptr_t)bp->bio_driver1;
832 ggio->gctl_offset = bp->bio_offset;
833 ggio->gctl_length = bp->bio_length;
835 switch (bp->bio_cmd) {
841 error = copyout(bp->bio_data, ggio->gctl_data,
844 mtx_lock(&sc->sc_queue_mtx);
845 bioq_remove(&sc->sc_outqueue, bp);
846 bioq_insert_head(&sc->sc_inqueue, bp);
847 mtx_unlock(&sc->sc_queue_mtx);
856 case G_GATE_CMD_DONE:
858 struct g_gate_ctl_io *ggio = (void *)addr;
860 G_GATE_CHECK_VERSION(ggio);
861 sc = g_gate_hold(ggio->gctl_unit, NULL);
865 mtx_lock(&sc->sc_queue_mtx);
866 TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) {
867 if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1)
871 bioq_remove(&sc->sc_outqueue, bp);
872 sc->sc_queue_count--;
874 mtx_unlock(&sc->sc_queue_mtx);
877 * Request was probably canceled.
881 if (ggio->gctl_error == EAGAIN) {
883 G_GATE_LOGREQ(1, bp, "Request desisted.");
884 mtx_lock(&sc->sc_queue_mtx);
885 sc->sc_queue_count++;
886 bioq_insert_head(&sc->sc_inqueue, bp);
888 mtx_unlock(&sc->sc_queue_mtx);
890 bp->bio_error = ggio->gctl_error;
891 if (bp->bio_error == 0) {
892 bp->bio_completed = bp->bio_length;
893 switch (bp->bio_cmd) {
895 error = copyin(ggio->gctl_data,
896 bp->bio_data, bp->bio_length);
898 bp->bio_error = error;
906 G_GATE_LOGREQ(2, bp, "Request done.");
907 g_io_deliver(bp, bp->bio_error);
921 status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600,
926 g_gate_modevent(module_t mod, int type, void *data)
932 mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF);
933 g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]),
934 M_GATE, M_WAITOK | M_ZERO);
939 mtx_lock(&g_gate_units_lock);
940 if (g_gate_nunits > 0) {
941 mtx_unlock(&g_gate_units_lock);
945 mtx_unlock(&g_gate_units_lock);
946 mtx_destroy(&g_gate_units_lock);
947 if (status_dev != NULL)
948 destroy_dev(status_dev);
949 free(g_gate_units, M_GATE);
958 static moduledata_t g_gate_module = {
963 DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
964 DECLARE_GEOM_CLASS(g_gate_class, g_gate);