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 pp->flags |= G_PF_WITHER;
113 g_orphan_provider(pp, ENXIO);
114 callout_drain(&sc->sc_callout);
116 mtx_lock(&sc->sc_queue_mtx);
117 while ((bp = bioq_takefirst(&sc->sc_inqueue)) != NULL) {
118 sc->sc_queue_count--;
119 bioq_insert_tail(&queue, bp);
121 while ((bp = bioq_takefirst(&sc->sc_outqueue)) != NULL) {
122 sc->sc_queue_count--;
123 bioq_insert_tail(&queue, bp);
125 mtx_unlock(&sc->sc_queue_mtx);
127 while ((bp = bioq_takefirst(&queue)) != NULL) {
128 G_GATE_LOGREQ(1, bp, "Request canceled.");
129 g_io_deliver(bp, ENXIO);
131 mtx_lock(&g_gate_units_lock);
132 /* One reference is ours. */
134 while (sc->sc_ref > 0)
135 msleep(&sc->sc_ref, &g_gate_units_lock, 0, "gg:destroy", 0);
136 g_gate_units[sc->sc_unit] = NULL;
137 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?"));
139 mtx_unlock(&g_gate_units_lock);
140 mtx_destroy(&sc->sc_queue_mtx);
142 if ((cp = sc->sc_readcons) != NULL) {
143 sc->sc_readcons = NULL;
144 (void)g_access(cp, -1, 0, 0);
146 g_destroy_consumer(cp);
148 G_GATE_DEBUG(1, "Device %s destroyed.", gp->name);
150 g_wither_geom(gp, ENXIO);
151 sc->sc_provider = NULL;
157 g_gate_access(struct g_provider *pp, int dr, int dw, int de)
159 struct g_gate_softc *sc;
161 if (dr <= 0 && dw <= 0 && de <= 0)
163 sc = pp->geom->softc;
164 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
166 /* XXX: Hack to allow read-only mounts. */
168 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0)
171 if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0)
177 g_gate_queue_io(struct bio *bp)
179 struct g_gate_softc *sc;
181 sc = bp->bio_to->geom->softc;
182 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
183 g_io_deliver(bp, ENXIO);
187 mtx_lock(&sc->sc_queue_mtx);
189 if (sc->sc_queue_size > 0 && sc->sc_queue_count > sc->sc_queue_size) {
190 mtx_unlock(&sc->sc_queue_mtx);
191 G_GATE_LOGREQ(1, bp, "Queue full, request canceled.");
192 g_io_deliver(bp, ENOMEM);
196 bp->bio_driver1 = (void *)sc->sc_seq;
198 sc->sc_queue_count++;
200 bioq_insert_tail(&sc->sc_inqueue, bp);
203 mtx_unlock(&sc->sc_queue_mtx);
207 g_gate_done(struct bio *cbp)
211 pbp = cbp->bio_parent;
212 if (cbp->bio_error == 0) {
213 pbp->bio_completed = cbp->bio_completed;
216 g_io_deliver(pbp, 0);
218 /* If direct read failed, pass it through userland daemon. */
221 g_gate_queue_io(pbp);
226 g_gate_start(struct bio *pbp)
228 struct g_gate_softc *sc;
230 sc = pbp->bio_to->geom->softc;
231 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
232 g_io_deliver(pbp, ENXIO);
235 G_GATE_LOGREQ(2, pbp, "Request received.");
236 switch (pbp->bio_cmd) {
238 if (sc->sc_readcons != NULL) {
241 cbp = g_clone_bio(pbp);
243 g_io_deliver(pbp, ENOMEM);
246 cbp->bio_done = g_gate_done;
247 cbp->bio_offset = pbp->bio_offset + sc->sc_readoffset;
248 cbp->bio_to = sc->sc_readcons->provider;
249 g_io_request(cbp, sc->sc_readcons);
256 /* XXX: Hack to allow read-only mounts. */
257 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
258 g_io_deliver(pbp, EPERM);
264 G_GATE_LOGREQ(2, pbp, "Ignoring request.");
265 g_io_deliver(pbp, EOPNOTSUPP);
269 g_gate_queue_io(pbp);
272 static struct g_gate_softc *
273 g_gate_hold(int unit, const char *name)
275 struct g_gate_softc *sc = NULL;
277 mtx_lock(&g_gate_units_lock);
278 if (unit >= 0 && unit < g_gate_maxunits)
279 sc = g_gate_units[unit];
280 else if (unit == G_GATE_NAME_GIVEN) {
281 KASSERT(name != NULL, ("name is NULL"));
282 for (unit = 0; unit < g_gate_maxunits; unit++) {
283 if (g_gate_units[unit] == NULL)
286 g_gate_units[unit]->sc_provider->name) != 0) {
289 sc = g_gate_units[unit];
295 mtx_unlock(&g_gate_units_lock);
300 g_gate_release(struct g_gate_softc *sc)
303 g_topology_assert_not();
304 mtx_lock(&g_gate_units_lock);
306 KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name));
307 if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
309 mtx_unlock(&g_gate_units_lock);
313 g_gate_getunit(int unit, int *errorp)
316 mtx_assert(&g_gate_units_lock, MA_OWNED);
318 if (unit >= g_gate_maxunits)
320 else if (g_gate_units[unit] == NULL)
325 for (unit = 0; unit < g_gate_maxunits; unit++) {
326 if (g_gate_units[unit] == NULL)
335 g_gate_guard(void *arg)
337 struct bio_queue_head queue;
338 struct g_gate_softc *sc;
339 struct bintime curtime;
340 struct bio *bp, *bp2;
344 g_gate_hold(sc->sc_unit, NULL);
346 mtx_lock(&sc->sc_queue_mtx);
347 TAILQ_FOREACH_SAFE(bp, &sc->sc_inqueue.queue, bio_queue, bp2) {
348 if (curtime.sec - bp->bio_t0.sec < 5)
350 bioq_remove(&sc->sc_inqueue, bp);
351 sc->sc_queue_count--;
352 bioq_insert_tail(&queue, bp);
354 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, bp2) {
355 if (curtime.sec - bp->bio_t0.sec < 5)
357 bioq_remove(&sc->sc_outqueue, bp);
358 sc->sc_queue_count--;
359 bioq_insert_tail(&queue, bp);
361 mtx_unlock(&sc->sc_queue_mtx);
362 while ((bp = bioq_takefirst(&queue)) != NULL) {
363 G_GATE_LOGREQ(1, bp, "Request timeout.");
364 g_io_deliver(bp, EIO);
366 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) {
367 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
374 g_gate_orphan(struct g_consumer *cp)
376 struct g_gate_softc *sc;
384 KASSERT(cp == sc->sc_readcons, ("cp=%p sc_readcons=%p", cp,
386 sc->sc_readcons = NULL;
387 G_GATE_DEBUG(1, "Destroying read consumer on provider %s orphan.",
389 (void)g_access(cp, -1, 0, 0);
391 g_destroy_consumer(cp);
395 g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
396 struct g_consumer *cp, struct g_provider *pp)
398 struct g_gate_softc *sc;
401 if (sc == NULL || pp != NULL || cp != NULL)
403 sc = g_gate_hold(sc->sc_unit, NULL);
406 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
407 sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only");
408 } else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) {
409 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
412 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
415 if (sc->sc_readcons != NULL) {
416 sbuf_printf(sb, "%s<read_offset>%jd</read_offset>\n",
417 indent, (intmax_t)sc->sc_readoffset);
418 sbuf_printf(sb, "%s<read_provider>%s</read_provider>\n",
419 indent, sc->sc_readcons->provider->name);
421 sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout);
422 sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info);
423 sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent,
425 sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent,
427 sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref);
428 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, sc->sc_unit);
435 g_gate_create(struct g_gate_ctl_create *ggio)
437 struct g_gate_softc *sc;
439 struct g_provider *pp, *ropp;
440 struct g_consumer *cp;
444 if (ggio->gctl_mediasize <= 0) {
445 G_GATE_DEBUG(1, "Invalid media size.");
448 if (ggio->gctl_sectorsize <= 0) {
449 G_GATE_DEBUG(1, "Invalid sector size.");
452 if (!powerof2(ggio->gctl_sectorsize)) {
453 G_GATE_DEBUG(1, "Invalid sector size.");
456 if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) {
457 G_GATE_DEBUG(1, "Invalid media size.");
460 if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 &&
461 (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) {
462 G_GATE_DEBUG(1, "Invalid flags.");
465 if (ggio->gctl_unit != G_GATE_UNIT_AUTO &&
466 ggio->gctl_unit != G_GATE_NAME_GIVEN &&
467 ggio->gctl_unit < 0) {
468 G_GATE_DEBUG(1, "Invalid unit number.");
471 if (ggio->gctl_unit == G_GATE_NAME_GIVEN &&
472 ggio->gctl_name[0] == '\0') {
473 G_GATE_DEBUG(1, "No device name.");
477 sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO);
478 sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS);
479 strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
481 bioq_init(&sc->sc_inqueue);
482 bioq_init(&sc->sc_outqueue);
483 mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF);
484 sc->sc_queue_count = 0;
485 sc->sc_queue_size = ggio->gctl_maxcount;
486 if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE)
487 sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE;
488 sc->sc_timeout = ggio->gctl_timeout;
489 callout_init(&sc->sc_callout, 1);
491 mtx_lock(&g_gate_units_lock);
492 sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error);
495 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
496 snprintf(name, sizeof(name), "%s", ggio->gctl_name);
498 snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME,
501 /* Check for name collision. */
502 for (unit = 0; unit < g_gate_maxunits; unit++) {
503 if (g_gate_units[unit] == NULL)
505 if (strcmp(name, g_gate_units[unit]->sc_name) != 0)
511 g_gate_units[sc->sc_unit] = sc;
513 mtx_unlock(&g_gate_units_lock);
517 if (ggio->gctl_readprov[0] == '\0') {
520 ropp = g_provider_by_name(ggio->gctl_readprov);
522 G_GATE_DEBUG(1, "Provider %s doesn't exist.",
523 ggio->gctl_readprov);
527 if ((ggio->gctl_readoffset % ggio->gctl_sectorsize) != 0) {
528 G_GATE_DEBUG(1, "Invalid read offset.");
532 if (ggio->gctl_mediasize + ggio->gctl_readoffset >
534 G_GATE_DEBUG(1, "Invalid read offset or media size.");
540 gp = g_new_geomf(&g_gate_class, "%s", name);
541 gp->start = g_gate_start;
542 gp->access = g_gate_access;
543 gp->orphan = g_gate_orphan;
544 gp->dumpconf = g_gate_dumpconf;
548 cp = g_new_consumer(gp);
549 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
550 error = g_attach(cp, ropp);
552 G_GATE_DEBUG(1, "Unable to attach to %s.", ropp->name);
555 error = g_access(cp, 1, 0, 0);
557 G_GATE_DEBUG(1, "Unable to access %s.", ropp->name);
561 sc->sc_readcons = cp;
562 sc->sc_readoffset = ggio->gctl_readoffset;
565 ggio->gctl_unit = sc->sc_unit;
567 pp = g_new_providerf(gp, "%s", name);
568 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
569 pp->mediasize = ggio->gctl_mediasize;
570 pp->sectorsize = ggio->gctl_sectorsize;
571 sc->sc_provider = pp;
572 g_error_provider(pp, 0);
575 mtx_lock(&g_gate_units_lock);
576 sc->sc_name = sc->sc_provider->name;
577 mtx_unlock(&g_gate_units_lock);
578 G_GATE_DEBUG(1, "Device %s created.", gp->name);
580 if (sc->sc_timeout > 0) {
581 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
586 g_destroy_consumer(cp);
590 mtx_lock(&g_gate_units_lock);
591 g_gate_units[sc->sc_unit] = NULL;
592 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?"));
595 mtx_unlock(&g_gate_units_lock);
596 mtx_destroy(&sc->sc_queue_mtx);
602 g_gate_modify(struct g_gate_softc *sc, struct g_gate_ctl_modify *ggio)
604 struct g_provider *pp;
605 struct g_consumer *cp;
608 if ((ggio->gctl_modify & GG_MODIFY_MEDIASIZE) != 0) {
609 if (ggio->gctl_mediasize <= 0) {
610 G_GATE_DEBUG(1, "Invalid media size.");
613 pp = sc->sc_provider;
614 if ((ggio->gctl_mediasize % pp->sectorsize) != 0) {
615 G_GATE_DEBUG(1, "Invalid media size.");
622 if ((ggio->gctl_modify & GG_MODIFY_INFO) != 0)
623 (void)strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
627 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) {
629 if (sc->sc_readcons != NULL) {
630 cp = sc->sc_readcons;
631 sc->sc_readcons = NULL;
632 (void)g_access(cp, -1, 0, 0);
634 g_destroy_consumer(cp);
636 if (ggio->gctl_readprov[0] != '\0') {
637 pp = g_provider_by_name(ggio->gctl_readprov);
640 G_GATE_DEBUG(1, "Provider %s doesn't exist.",
641 ggio->gctl_readprov);
644 cp = g_new_consumer(sc->sc_provider->geom);
645 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
646 error = g_attach(cp, pp);
648 G_GATE_DEBUG(1, "Unable to attach to %s.",
651 error = g_access(cp, 1, 0, 0);
653 G_GATE_DEBUG(1, "Unable to access %s.",
659 g_destroy_consumer(cp);
665 cp = sc->sc_readcons;
668 if ((ggio->gctl_modify & GG_MODIFY_READOFFSET) != 0) {
670 G_GATE_DEBUG(1, "No read provider.");
673 pp = sc->sc_provider;
674 if ((ggio->gctl_readoffset % pp->sectorsize) != 0) {
675 G_GATE_DEBUG(1, "Invalid read offset.");
678 if (pp->mediasize + ggio->gctl_readoffset >
679 cp->provider->mediasize) {
680 G_GATE_DEBUG(1, "Invalid read offset or media size.");
683 sc->sc_readoffset = ggio->gctl_readoffset;
686 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) {
687 sc->sc_readcons = cp;
694 #define G_GATE_CHECK_VERSION(ggio) do { \
695 if ((ggio)->gctl_version != G_GATE_VERSION) { \
696 printf("Version mismatch %d != %d.\n", \
697 ggio->gctl_version, G_GATE_VERSION); \
702 g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
704 struct g_gate_softc *sc;
708 G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr,
712 case G_GATE_CMD_CREATE:
714 struct g_gate_ctl_create *ggio = (void *)addr;
716 G_GATE_CHECK_VERSION(ggio);
717 error = g_gate_create(ggio);
719 * Reset TDP_GEOM flag.
720 * There are pending events for sure, because we just created
721 * new provider and other classes want to taste it, but we
722 * cannot answer on I/O requests until we're here.
724 td->td_pflags &= ~TDP_GEOM;
727 case G_GATE_CMD_MODIFY:
729 struct g_gate_ctl_modify *ggio = (void *)addr;
731 G_GATE_CHECK_VERSION(ggio);
732 sc = g_gate_hold(ggio->gctl_unit, NULL);
735 error = g_gate_modify(sc, ggio);
739 case G_GATE_CMD_DESTROY:
741 struct g_gate_ctl_destroy *ggio = (void *)addr;
743 G_GATE_CHECK_VERSION(ggio);
744 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
748 mtx_lock(&g_gate_units_lock);
749 error = g_gate_destroy(sc, ggio->gctl_force);
755 case G_GATE_CMD_CANCEL:
757 struct g_gate_ctl_cancel *ggio = (void *)addr;
758 struct bio *tbp, *lbp;
760 G_GATE_CHECK_VERSION(ggio);
761 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
765 mtx_lock(&sc->sc_queue_mtx);
766 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) {
767 if (ggio->gctl_seq == 0 ||
768 ggio->gctl_seq == (uintptr_t)bp->bio_driver1) {
769 G_GATE_LOGREQ(1, bp, "Request canceled.");
770 bioq_remove(&sc->sc_outqueue, bp);
772 * Be sure to put requests back onto incoming
773 * queue in the proper order.
776 bioq_insert_head(&sc->sc_inqueue, bp);
778 TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue,
783 * If only one request was canceled, leave now.
785 if (ggio->gctl_seq != 0)
789 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
790 ggio->gctl_unit = sc->sc_unit;
791 mtx_unlock(&sc->sc_queue_mtx);
795 case G_GATE_CMD_START:
797 struct g_gate_ctl_io *ggio = (void *)addr;
799 G_GATE_CHECK_VERSION(ggio);
800 sc = g_gate_hold(ggio->gctl_unit, NULL);
805 mtx_lock(&sc->sc_queue_mtx);
806 bp = bioq_first(&sc->sc_inqueue);
809 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
810 ggio->gctl_error = ECANCELED;
811 mtx_unlock(&sc->sc_queue_mtx);
814 if (msleep(sc, &sc->sc_queue_mtx,
815 PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) {
816 ggio->gctl_error = ECANCELED;
820 ggio->gctl_cmd = bp->bio_cmd;
821 if (bp->bio_cmd == BIO_WRITE &&
822 bp->bio_length > ggio->gctl_length) {
823 mtx_unlock(&sc->sc_queue_mtx);
824 ggio->gctl_length = bp->bio_length;
825 ggio->gctl_error = ENOMEM;
828 bioq_remove(&sc->sc_inqueue, bp);
829 bioq_insert_tail(&sc->sc_outqueue, bp);
830 mtx_unlock(&sc->sc_queue_mtx);
832 ggio->gctl_seq = (uintptr_t)bp->bio_driver1;
833 ggio->gctl_offset = bp->bio_offset;
834 ggio->gctl_length = bp->bio_length;
836 switch (bp->bio_cmd) {
842 error = copyout(bp->bio_data, ggio->gctl_data,
845 mtx_lock(&sc->sc_queue_mtx);
846 bioq_remove(&sc->sc_outqueue, bp);
847 bioq_insert_head(&sc->sc_inqueue, bp);
848 mtx_unlock(&sc->sc_queue_mtx);
857 case G_GATE_CMD_DONE:
859 struct g_gate_ctl_io *ggio = (void *)addr;
861 G_GATE_CHECK_VERSION(ggio);
862 sc = g_gate_hold(ggio->gctl_unit, NULL);
866 mtx_lock(&sc->sc_queue_mtx);
867 TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) {
868 if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1)
872 bioq_remove(&sc->sc_outqueue, bp);
873 sc->sc_queue_count--;
875 mtx_unlock(&sc->sc_queue_mtx);
878 * Request was probably canceled.
882 if (ggio->gctl_error == EAGAIN) {
884 G_GATE_LOGREQ(1, bp, "Request desisted.");
885 mtx_lock(&sc->sc_queue_mtx);
886 sc->sc_queue_count++;
887 bioq_insert_head(&sc->sc_inqueue, bp);
889 mtx_unlock(&sc->sc_queue_mtx);
891 bp->bio_error = ggio->gctl_error;
892 if (bp->bio_error == 0) {
893 bp->bio_completed = bp->bio_length;
894 switch (bp->bio_cmd) {
896 error = copyin(ggio->gctl_data,
897 bp->bio_data, bp->bio_length);
899 bp->bio_error = error;
907 G_GATE_LOGREQ(2, bp, "Request done.");
908 g_io_deliver(bp, bp->bio_error);
922 status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600,
927 g_gate_modevent(module_t mod, int type, void *data)
933 mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF);
934 g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]),
935 M_GATE, M_WAITOK | M_ZERO);
940 mtx_lock(&g_gate_units_lock);
941 if (g_gate_nunits > 0) {
942 mtx_unlock(&g_gate_units_lock);
946 mtx_unlock(&g_gate_units_lock);
947 mtx_destroy(&g_gate_units_lock);
949 destroy_dev(status_dev);
950 free(g_gate_units, M_GATE);
959 static moduledata_t g_gate_module = {
964 DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
965 DECLARE_GEOM_CLASS(g_gate_class, g_gate);