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 TUNABLE_INT("kern.geom.gate.debug", &g_gate_debug);
66 SYSCTL_INT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RW, &g_gate_debug, 0,
68 static u_int g_gate_maxunits = 256;
69 TUNABLE_INT("kern.geom.gate.maxunits", &g_gate_maxunits);
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 g_provider *pp;
95 struct g_consumer *cp;
100 mtx_assert(&g_gate_units_lock, MA_OWNED);
101 pp = sc->sc_provider;
102 if (!force && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
103 mtx_unlock(&g_gate_units_lock);
106 mtx_unlock(&g_gate_units_lock);
107 mtx_lock(&sc->sc_queue_mtx);
108 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0)
109 sc->sc_flags |= G_GATE_FLAG_DESTROY;
111 mtx_unlock(&sc->sc_queue_mtx);
113 pp->flags |= G_PF_WITHER;
114 g_orphan_provider(pp, ENXIO);
115 callout_drain(&sc->sc_callout);
116 mtx_lock(&sc->sc_queue_mtx);
117 while ((bp = bioq_first(&sc->sc_inqueue)) != 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);
123 while ((bp = bioq_first(&sc->sc_outqueue)) != NULL) {
124 bioq_remove(&sc->sc_outqueue, bp);
125 sc->sc_queue_count--;
126 G_GATE_LOGREQ(1, bp, "Request canceled.");
127 g_io_deliver(bp, ENXIO);
129 mtx_unlock(&sc->sc_queue_mtx);
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_data = pbp->bio_data;
249 cbp->bio_length = pbp->bio_length;
250 cbp->bio_to = sc->sc_readcons->provider;
251 g_io_request(cbp, sc->sc_readcons);
258 /* XXX: Hack to allow read-only mounts. */
259 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
260 g_io_deliver(pbp, EPERM);
266 G_GATE_LOGREQ(2, pbp, "Ignoring request.");
267 g_io_deliver(pbp, EOPNOTSUPP);
271 g_gate_queue_io(pbp);
274 static struct g_gate_softc *
275 g_gate_hold(int unit, const char *name)
277 struct g_gate_softc *sc = NULL;
279 mtx_lock(&g_gate_units_lock);
280 if (unit >= 0 && unit < g_gate_maxunits)
281 sc = g_gate_units[unit];
282 else if (unit == G_GATE_NAME_GIVEN) {
283 KASSERT(name != NULL, ("name is NULL"));
284 for (unit = 0; unit < g_gate_maxunits; unit++) {
285 if (g_gate_units[unit] == NULL)
288 g_gate_units[unit]->sc_provider->name) != 0) {
291 sc = g_gate_units[unit];
297 mtx_unlock(&g_gate_units_lock);
302 g_gate_release(struct g_gate_softc *sc)
305 g_topology_assert_not();
306 mtx_lock(&g_gate_units_lock);
308 KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name));
309 if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
311 mtx_unlock(&g_gate_units_lock);
315 g_gate_getunit(int unit, int *errorp)
318 mtx_assert(&g_gate_units_lock, MA_OWNED);
320 if (unit >= g_gate_maxunits)
322 else if (g_gate_units[unit] == NULL)
327 for (unit = 0; unit < g_gate_maxunits; unit++) {
328 if (g_gate_units[unit] == NULL)
337 g_gate_guard(void *arg)
339 struct g_gate_softc *sc;
340 struct bintime curtime;
341 struct bio *bp, *bp2;
345 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 G_GATE_LOGREQ(1, bp, "Request timeout.");
353 g_io_deliver(bp, EIO);
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 G_GATE_LOGREQ(1, bp, "Request timeout.");
361 g_io_deliver(bp, EIO);
363 mtx_unlock(&sc->sc_queue_mtx);
364 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) {
365 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
372 g_gate_orphan(struct g_consumer *cp)
374 struct g_gate_softc *sc;
382 KASSERT(cp == sc->sc_readcons, ("cp=%p sc_readcons=%p", cp,
384 sc->sc_readcons = NULL;
385 G_GATE_DEBUG(1, "Destroying read consumer on provider %s orphan.",
387 (void)g_access(cp, -1, 0, 0);
389 g_destroy_consumer(cp);
393 g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
394 struct g_consumer *cp, struct g_provider *pp)
396 struct g_gate_softc *sc;
399 if (sc == NULL || pp != NULL || cp != NULL)
401 g_gate_hold(sc->sc_unit, NULL);
402 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
403 sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only");
404 } else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) {
405 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
408 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
411 if (sc->sc_readcons != NULL) {
412 sbuf_printf(sb, "%s<read_offset>%jd</read_offset>\n",
413 indent, (intmax_t)sc->sc_readoffset);
414 sbuf_printf(sb, "%s<read_provider>%s</read_provider>\n",
415 indent, sc->sc_readcons->provider->name);
417 sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout);
418 sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info);
419 sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent,
421 sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent,
423 sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref);
424 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, sc->sc_unit);
431 g_gate_create(struct g_gate_ctl_create *ggio)
433 struct g_gate_softc *sc;
435 struct g_provider *pp, *ropp;
436 struct g_consumer *cp;
440 if (ggio->gctl_mediasize <= 0) {
441 G_GATE_DEBUG(1, "Invalid media size.");
444 if (ggio->gctl_sectorsize <= 0) {
445 G_GATE_DEBUG(1, "Invalid sector size.");
448 if (!powerof2(ggio->gctl_sectorsize)) {
449 G_GATE_DEBUG(1, "Invalid sector size.");
452 if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) {
453 G_GATE_DEBUG(1, "Invalid media size.");
456 if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 &&
457 (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) {
458 G_GATE_DEBUG(1, "Invalid flags.");
461 if (ggio->gctl_unit != G_GATE_UNIT_AUTO &&
462 ggio->gctl_unit != G_GATE_NAME_GIVEN &&
463 ggio->gctl_unit < 0) {
464 G_GATE_DEBUG(1, "Invalid unit number.");
467 if (ggio->gctl_unit == G_GATE_NAME_GIVEN &&
468 ggio->gctl_name[0] == '\0') {
469 G_GATE_DEBUG(1, "No device name.");
475 if (ggio->gctl_readprov[0] == '\0') {
478 ropp = g_provider_by_name(ggio->gctl_readprov);
481 G_GATE_DEBUG(1, "Provider %s doesn't exist.",
482 ggio->gctl_readprov);
485 if ((ggio->gctl_readoffset % ggio->gctl_sectorsize) != 0) {
487 G_GATE_DEBUG(1, "Invalid read offset.");
490 if (ggio->gctl_mediasize + ggio->gctl_readoffset >
493 G_GATE_DEBUG(1, "Invalid read offset or media size.");
498 sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO);
499 sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS);
500 strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
502 bioq_init(&sc->sc_inqueue);
503 bioq_init(&sc->sc_outqueue);
504 mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF);
505 sc->sc_queue_count = 0;
506 sc->sc_queue_size = ggio->gctl_maxcount;
507 if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE)
508 sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE;
509 sc->sc_timeout = ggio->gctl_timeout;
510 callout_init(&sc->sc_callout, CALLOUT_MPSAFE);
512 gp = g_new_geomf(&g_gate_class, "%s", name);
513 gp->start = g_gate_start;
514 gp->access = g_gate_access;
515 gp->orphan = g_gate_orphan;
516 gp->dumpconf = g_gate_dumpconf;
520 cp = g_new_consumer(gp);
521 error = g_attach(cp, ropp);
523 G_GATE_DEBUG(1, "Unable to attach to %s.", ropp->name);
525 error = g_access(cp, 1, 0, 0);
527 G_GATE_DEBUG(1, "Unable to access %s.",
533 g_destroy_consumer(cp);
536 mtx_destroy(&sc->sc_queue_mtx);
540 sc->sc_readcons = cp;
541 sc->sc_readoffset = ggio->gctl_readoffset;
544 mtx_lock(&g_gate_units_lock);
545 sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error);
546 if (sc->sc_unit < 0) {
547 mtx_unlock(&g_gate_units_lock);
548 if (sc->sc_readcons != NULL) {
549 (void)g_access(sc->sc_readcons, -1, 0, 0);
550 g_detach(sc->sc_readcons);
551 g_destroy_consumer(sc->sc_readcons);
555 mtx_destroy(&sc->sc_queue_mtx);
559 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
560 snprintf(name, sizeof(name), "%s", ggio->gctl_name);
562 snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME,
565 /* Check for name collision. */
566 for (unit = 0; unit < g_gate_maxunits; unit++) {
567 if (g_gate_units[unit] == NULL)
569 if (strcmp(name, g_gate_units[unit]->sc_name) != 0)
571 mtx_unlock(&g_gate_units_lock);
572 if (sc->sc_readcons != NULL) {
573 (void)g_access(sc->sc_readcons, -1, 0, 0);
574 g_detach(sc->sc_readcons);
575 g_destroy_consumer(sc->sc_readcons);
579 mtx_destroy(&sc->sc_queue_mtx);
584 g_gate_units[sc->sc_unit] = sc;
586 mtx_unlock(&g_gate_units_lock);
588 ggio->gctl_unit = sc->sc_unit;
590 pp = g_new_providerf(gp, "%s", name);
591 pp->mediasize = ggio->gctl_mediasize;
592 pp->sectorsize = ggio->gctl_sectorsize;
593 sc->sc_provider = pp;
594 g_error_provider(pp, 0);
597 mtx_lock(&g_gate_units_lock);
598 sc->sc_name = sc->sc_provider->name;
599 mtx_unlock(&g_gate_units_lock);
600 G_GATE_DEBUG(1, "Device %s created.", gp->name);
602 if (sc->sc_timeout > 0) {
603 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
610 g_gate_modify(struct g_gate_softc *sc, struct g_gate_ctl_modify *ggio)
612 struct g_provider *pp;
613 struct g_consumer *cp;
616 if ((ggio->gctl_modify & GG_MODIFY_MEDIASIZE) != 0) {
617 if (ggio->gctl_mediasize <= 0) {
618 G_GATE_DEBUG(1, "Invalid media size.");
621 pp = sc->sc_provider;
622 if ((ggio->gctl_mediasize % pp->sectorsize) != 0) {
623 G_GATE_DEBUG(1, "Invalid media size.");
630 if ((ggio->gctl_modify & GG_MODIFY_INFO) != 0)
631 (void)strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
635 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) {
637 if (sc->sc_readcons != NULL) {
638 cp = sc->sc_readcons;
639 sc->sc_readcons = NULL;
640 (void)g_access(cp, -1, 0, 0);
642 g_destroy_consumer(cp);
644 if (ggio->gctl_readprov[0] != '\0') {
645 pp = g_provider_by_name(ggio->gctl_readprov);
648 G_GATE_DEBUG(1, "Provider %s doesn't exist.",
649 ggio->gctl_readprov);
652 cp = g_new_consumer(sc->sc_provider->geom);
653 error = g_attach(cp, pp);
655 G_GATE_DEBUG(1, "Unable to attach to %s.",
658 error = g_access(cp, 1, 0, 0);
660 G_GATE_DEBUG(1, "Unable to access %s.",
666 g_destroy_consumer(cp);
672 cp = sc->sc_readcons;
675 if ((ggio->gctl_modify & GG_MODIFY_READOFFSET) != 0) {
677 G_GATE_DEBUG(1, "No read provider.");
680 pp = sc->sc_provider;
681 if ((ggio->gctl_readoffset % pp->sectorsize) != 0) {
682 G_GATE_DEBUG(1, "Invalid read offset.");
685 if (pp->mediasize + ggio->gctl_readoffset >
686 cp->provider->mediasize) {
687 G_GATE_DEBUG(1, "Invalid read offset or media size.");
690 sc->sc_readoffset = ggio->gctl_readoffset;
693 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) {
694 sc->sc_readcons = cp;
701 #define G_GATE_CHECK_VERSION(ggio) do { \
702 if ((ggio)->gctl_version != G_GATE_VERSION) { \
703 printf("Version mismatch %d != %d.\n", \
704 ggio->gctl_version, G_GATE_VERSION); \
709 g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
711 struct g_gate_softc *sc;
715 G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr,
719 case G_GATE_CMD_CREATE:
721 struct g_gate_ctl_create *ggio = (void *)addr;
723 G_GATE_CHECK_VERSION(ggio);
724 error = g_gate_create(ggio);
726 * Reset TDP_GEOM flag.
727 * There are pending events for sure, because we just created
728 * new provider and other classes want to taste it, but we
729 * cannot answer on I/O requests until we're here.
731 td->td_pflags &= ~TDP_GEOM;
734 case G_GATE_CMD_MODIFY:
736 struct g_gate_ctl_modify *ggio = (void *)addr;
738 G_GATE_CHECK_VERSION(ggio);
739 sc = g_gate_hold(ggio->gctl_unit, NULL);
742 error = g_gate_modify(sc, ggio);
746 case G_GATE_CMD_DESTROY:
748 struct g_gate_ctl_destroy *ggio = (void *)addr;
750 G_GATE_CHECK_VERSION(ggio);
751 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
755 mtx_lock(&g_gate_units_lock);
756 error = g_gate_destroy(sc, ggio->gctl_force);
762 case G_GATE_CMD_CANCEL:
764 struct g_gate_ctl_cancel *ggio = (void *)addr;
765 struct bio *tbp, *lbp;
767 G_GATE_CHECK_VERSION(ggio);
768 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
772 mtx_lock(&sc->sc_queue_mtx);
773 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) {
774 if (ggio->gctl_seq == 0 ||
775 ggio->gctl_seq == (uintptr_t)bp->bio_driver1) {
776 G_GATE_LOGREQ(1, bp, "Request canceled.");
777 bioq_remove(&sc->sc_outqueue, bp);
779 * Be sure to put requests back onto incoming
780 * queue in the proper order.
783 bioq_insert_head(&sc->sc_inqueue, bp);
785 TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue,
790 * If only one request was canceled, leave now.
792 if (ggio->gctl_seq != 0)
796 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
797 ggio->gctl_unit = sc->sc_unit;
798 mtx_unlock(&sc->sc_queue_mtx);
802 case G_GATE_CMD_START:
804 struct g_gate_ctl_io *ggio = (void *)addr;
806 G_GATE_CHECK_VERSION(ggio);
807 sc = g_gate_hold(ggio->gctl_unit, NULL);
812 mtx_lock(&sc->sc_queue_mtx);
813 bp = bioq_first(&sc->sc_inqueue);
816 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
817 ggio->gctl_error = ECANCELED;
818 mtx_unlock(&sc->sc_queue_mtx);
821 if (msleep(sc, &sc->sc_queue_mtx,
822 PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) {
823 ggio->gctl_error = ECANCELED;
827 ggio->gctl_cmd = bp->bio_cmd;
828 if ((bp->bio_cmd == BIO_DELETE || bp->bio_cmd == BIO_WRITE) &&
829 bp->bio_length > ggio->gctl_length) {
830 mtx_unlock(&sc->sc_queue_mtx);
831 ggio->gctl_length = bp->bio_length;
832 ggio->gctl_error = ENOMEM;
835 bioq_remove(&sc->sc_inqueue, bp);
836 bioq_insert_tail(&sc->sc_outqueue, bp);
837 mtx_unlock(&sc->sc_queue_mtx);
839 ggio->gctl_seq = (uintptr_t)bp->bio_driver1;
840 ggio->gctl_offset = bp->bio_offset;
841 ggio->gctl_length = bp->bio_length;
843 switch (bp->bio_cmd) {
849 error = copyout(bp->bio_data, ggio->gctl_data,
852 mtx_lock(&sc->sc_queue_mtx);
853 bioq_remove(&sc->sc_outqueue, bp);
854 bioq_insert_head(&sc->sc_inqueue, bp);
855 mtx_unlock(&sc->sc_queue_mtx);
864 case G_GATE_CMD_DONE:
866 struct g_gate_ctl_io *ggio = (void *)addr;
868 G_GATE_CHECK_VERSION(ggio);
869 sc = g_gate_hold(ggio->gctl_unit, NULL);
873 mtx_lock(&sc->sc_queue_mtx);
874 TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) {
875 if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1)
879 bioq_remove(&sc->sc_outqueue, bp);
880 sc->sc_queue_count--;
882 mtx_unlock(&sc->sc_queue_mtx);
885 * Request was probably canceled.
889 if (ggio->gctl_error == EAGAIN) {
891 G_GATE_LOGREQ(1, bp, "Request desisted.");
892 mtx_lock(&sc->sc_queue_mtx);
893 sc->sc_queue_count++;
894 bioq_insert_head(&sc->sc_inqueue, bp);
896 mtx_unlock(&sc->sc_queue_mtx);
898 bp->bio_error = ggio->gctl_error;
899 if (bp->bio_error == 0) {
900 bp->bio_completed = bp->bio_length;
901 switch (bp->bio_cmd) {
903 error = copyin(ggio->gctl_data,
904 bp->bio_data, bp->bio_length);
906 bp->bio_error = error;
914 G_GATE_LOGREQ(2, bp, "Request done.");
915 g_io_deliver(bp, bp->bio_error);
929 status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600,
934 g_gate_modevent(module_t mod, int type, void *data)
940 mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF);
941 g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]),
942 M_GATE, M_WAITOK | M_ZERO);
947 mtx_lock(&g_gate_units_lock);
948 if (g_gate_nunits > 0) {
949 mtx_unlock(&g_gate_units_lock);
953 mtx_unlock(&g_gate_units_lock);
954 mtx_destroy(&g_gate_units_lock);
956 destroy_dev(status_dev);
957 free(g_gate_units, M_GATE);
966 static moduledata_t g_gate_module = {
971 DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
972 DECLARE_GEOM_CLASS(g_gate_class, g_gate);