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/geom_dbg.h>
58 #include <geom/gate/g_gate.h>
60 FEATURE(geom_gate, "GEOM Gate module");
62 static MALLOC_DEFINE(M_GATE, "gg_data", "GEOM Gate Data");
64 SYSCTL_DECL(_kern_geom);
65 static SYSCTL_NODE(_kern_geom, OID_AUTO, gate, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
66 "GEOM_GATE configuration");
67 static int g_gate_debug = 0;
68 SYSCTL_INT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RWTUN, &g_gate_debug, 0,
70 static u_int g_gate_maxunits = 256;
71 SYSCTL_UINT(_kern_geom_gate, OID_AUTO, maxunits, CTLFLAG_RDTUN,
72 &g_gate_maxunits, 0, "Maximum number of ggate devices");
74 struct g_class g_gate_class = {
75 .name = G_GATE_CLASS_NAME,
79 static struct cdev *status_dev;
80 static d_ioctl_t g_gate_ioctl;
81 static struct cdevsw g_gate_cdevsw = {
82 .d_version = D_VERSION,
83 .d_ioctl = g_gate_ioctl,
84 .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_detach(void *arg, int flags __unused)
94 struct g_consumer *cp = arg;
97 G_GATE_DEBUG(1, "Destroying read consumer on provider %s orphan.",
99 (void)g_access(cp, -1, 0, 0);
101 g_destroy_consumer(cp);
105 g_gate_destroy(struct g_gate_softc *sc, boolean_t force)
107 struct bio_queue_head queue;
108 struct g_provider *pp;
109 struct g_consumer *cp;
114 mtx_assert(&g_gate_units_lock, MA_OWNED);
115 pp = sc->sc_provider;
116 if (!force && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
117 mtx_unlock(&g_gate_units_lock);
120 mtx_unlock(&g_gate_units_lock);
121 mtx_lock(&sc->sc_queue_mtx);
122 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0)
123 sc->sc_flags |= G_GATE_FLAG_DESTROY;
125 mtx_unlock(&sc->sc_queue_mtx);
127 g_wither_provider(pp, ENXIO);
128 callout_drain(&sc->sc_callout);
130 mtx_lock(&sc->sc_queue_mtx);
131 while ((bp = bioq_takefirst(&sc->sc_inqueue)) != NULL) {
132 sc->sc_queue_count--;
133 bioq_insert_tail(&queue, bp);
135 while ((bp = bioq_takefirst(&sc->sc_outqueue)) != NULL) {
136 sc->sc_queue_count--;
137 bioq_insert_tail(&queue, bp);
139 mtx_unlock(&sc->sc_queue_mtx);
141 while ((bp = bioq_takefirst(&queue)) != NULL) {
142 G_GATE_LOGREQ(1, bp, "Request canceled.");
143 g_io_deliver(bp, ENXIO);
145 mtx_lock(&g_gate_units_lock);
146 /* One reference is ours. */
148 while (sc->sc_ref > 0)
149 msleep(&sc->sc_ref, &g_gate_units_lock, 0, "gg:destroy", 0);
150 g_gate_units[sc->sc_unit] = NULL;
151 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?"));
153 mtx_unlock(&g_gate_units_lock);
154 mtx_destroy(&sc->sc_queue_mtx);
155 mtx_destroy(&sc->sc_read_mtx);
157 if ((cp = sc->sc_readcons) != NULL) {
158 sc->sc_readcons = NULL;
159 (void)g_access(cp, -1, 0, 0);
161 g_destroy_consumer(cp);
163 G_GATE_DEBUG(1, "Device %s destroyed.", gp->name);
165 g_wither_geom(gp, ENXIO);
166 sc->sc_provider = NULL;
172 g_gate_access(struct g_provider *pp, int dr, int dw, int de)
174 struct g_gate_softc *sc;
176 if (dr <= 0 && dw <= 0 && de <= 0)
178 sc = pp->geom->softc;
179 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
181 /* XXX: Hack to allow read-only mounts. */
183 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0)
186 if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0)
192 g_gate_queue_io(struct bio *bp)
194 struct g_gate_softc *sc;
196 sc = bp->bio_to->geom->softc;
197 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
198 g_io_deliver(bp, ENXIO);
202 mtx_lock(&sc->sc_queue_mtx);
204 if (sc->sc_queue_size > 0 && sc->sc_queue_count > sc->sc_queue_size) {
205 mtx_unlock(&sc->sc_queue_mtx);
206 G_GATE_LOGREQ(1, bp, "Queue full, request canceled.");
207 g_io_deliver(bp, ENOMEM);
211 bp->bio_driver1 = (void *)sc->sc_seq;
213 sc->sc_queue_count++;
215 bioq_insert_tail(&sc->sc_inqueue, bp);
218 mtx_unlock(&sc->sc_queue_mtx);
222 g_gate_done(struct bio *cbp)
224 struct g_gate_softc *sc;
226 struct g_consumer *cp;
229 pbp = cbp->bio_parent;
230 if (cbp->bio_error == 0) {
231 pbp->bio_completed = cbp->bio_completed;
234 g_io_deliver(pbp, 0);
236 /* If direct read failed, pass it through userland daemon. */
239 g_gate_queue_io(pbp);
242 sc = cp->geom->softc;
243 mtx_lock(&sc->sc_read_mtx);
244 if (--cp->index == 0 && sc->sc_readcons != cp)
245 g_post_event(g_gate_detach, cp, M_NOWAIT, NULL);
246 mtx_unlock(&sc->sc_read_mtx);
250 g_gate_start(struct bio *pbp)
252 struct g_gate_softc *sc;
253 struct g_consumer *cp;
256 sc = pbp->bio_to->geom->softc;
257 if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
258 g_io_deliver(pbp, ENXIO);
261 G_GATE_LOGREQ(2, pbp, "Request received.");
262 switch (pbp->bio_cmd) {
264 if (sc->sc_readcons == NULL)
266 cbp = g_clone_bio(pbp);
268 g_io_deliver(pbp, ENOMEM);
271 mtx_lock(&sc->sc_read_mtx);
272 if ((cp = sc->sc_readcons) == NULL) {
273 mtx_unlock(&sc->sc_read_mtx);
279 cbp->bio_offset = pbp->bio_offset + sc->sc_readoffset;
280 mtx_unlock(&sc->sc_read_mtx);
281 cbp->bio_done = g_gate_done;
282 g_io_request(cbp, cp);
288 /* XXX: Hack to allow read-only mounts. */
289 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
290 g_io_deliver(pbp, EPERM);
296 G_GATE_LOGREQ(2, pbp, "Ignoring request.");
297 g_io_deliver(pbp, EOPNOTSUPP);
301 g_gate_queue_io(pbp);
304 static struct g_gate_softc *
305 g_gate_hold(int unit, const char *name)
307 struct g_gate_softc *sc = NULL;
309 mtx_lock(&g_gate_units_lock);
310 if (unit >= 0 && unit < g_gate_maxunits)
311 sc = g_gate_units[unit];
312 else if (unit == G_GATE_NAME_GIVEN) {
313 KASSERT(name != NULL, ("name is NULL"));
314 for (unit = 0; unit < g_gate_maxunits; unit++) {
315 if (g_gate_units[unit] == NULL)
318 g_gate_units[unit]->sc_provider->name) != 0) {
321 sc = g_gate_units[unit];
327 mtx_unlock(&g_gate_units_lock);
332 g_gate_release(struct g_gate_softc *sc)
335 g_topology_assert_not();
336 mtx_lock(&g_gate_units_lock);
338 KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name));
339 if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0)
341 mtx_unlock(&g_gate_units_lock);
345 g_gate_getunit(int unit, int *errorp)
348 mtx_assert(&g_gate_units_lock, MA_OWNED);
350 if (unit >= g_gate_maxunits)
352 else if (g_gate_units[unit] == NULL)
357 for (unit = 0; unit < g_gate_maxunits; unit++) {
358 if (g_gate_units[unit] == NULL)
367 g_gate_guard(void *arg)
369 struct bio_queue_head queue;
370 struct g_gate_softc *sc;
371 struct bintime curtime;
372 struct bio *bp, *bp2;
376 g_gate_hold(sc->sc_unit, NULL);
378 mtx_lock(&sc->sc_queue_mtx);
379 TAILQ_FOREACH_SAFE(bp, &sc->sc_inqueue.queue, bio_queue, bp2) {
380 if (curtime.sec - bp->bio_t0.sec < 5)
382 bioq_remove(&sc->sc_inqueue, bp);
383 sc->sc_queue_count--;
384 bioq_insert_tail(&queue, bp);
386 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, bp2) {
387 if (curtime.sec - bp->bio_t0.sec < 5)
389 bioq_remove(&sc->sc_outqueue, bp);
390 sc->sc_queue_count--;
391 bioq_insert_tail(&queue, bp);
393 mtx_unlock(&sc->sc_queue_mtx);
394 while ((bp = bioq_takefirst(&queue)) != NULL) {
395 G_GATE_LOGREQ(1, bp, "Request timeout.");
396 g_io_deliver(bp, EIO);
398 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) {
399 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
406 g_gate_orphan(struct g_consumer *cp)
408 struct g_gate_softc *sc;
415 mtx_lock(&sc->sc_read_mtx);
416 if (sc->sc_readcons == cp)
417 sc->sc_readcons = NULL;
418 done = (cp->index == 0);
419 mtx_unlock(&sc->sc_read_mtx);
421 g_gate_detach(cp, 0);
425 g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
426 struct g_consumer *cp, struct g_provider *pp)
428 struct g_gate_softc *sc;
431 if (sc == NULL || pp != NULL || cp != NULL)
433 sc = g_gate_hold(sc->sc_unit, NULL);
436 if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) {
437 sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only");
438 } else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) {
439 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
442 sbuf_printf(sb, "%s<access>%s</access>\n", indent,
445 if (sc->sc_readcons != NULL) {
446 sbuf_printf(sb, "%s<read_offset>%jd</read_offset>\n",
447 indent, (intmax_t)sc->sc_readoffset);
448 sbuf_printf(sb, "%s<read_provider>%s</read_provider>\n",
449 indent, sc->sc_readcons->provider->name);
451 sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout);
452 sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info);
453 sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent,
455 sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent,
457 sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref);
458 sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, sc->sc_unit);
465 g_gate_create(struct g_gate_ctl_create *ggio)
467 struct g_gate_softc *sc;
469 struct g_provider *pp, *ropp;
470 struct g_consumer *cp;
474 if (ggio->gctl_mediasize <= 0) {
475 G_GATE_DEBUG(1, "Invalid media size.");
478 if (ggio->gctl_sectorsize <= 0) {
479 G_GATE_DEBUG(1, "Invalid sector size.");
482 if (!powerof2(ggio->gctl_sectorsize)) {
483 G_GATE_DEBUG(1, "Invalid sector size.");
486 if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) {
487 G_GATE_DEBUG(1, "Invalid media size.");
490 if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 &&
491 (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) {
492 G_GATE_DEBUG(1, "Invalid flags.");
495 if (ggio->gctl_unit != G_GATE_UNIT_AUTO &&
496 ggio->gctl_unit != G_GATE_NAME_GIVEN &&
497 ggio->gctl_unit < 0) {
498 G_GATE_DEBUG(1, "Invalid unit number.");
501 if (ggio->gctl_unit == G_GATE_NAME_GIVEN &&
502 ggio->gctl_name[0] == '\0') {
503 G_GATE_DEBUG(1, "No device name.");
507 sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO);
508 sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS);
509 strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
511 bioq_init(&sc->sc_inqueue);
512 bioq_init(&sc->sc_outqueue);
513 mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF);
514 mtx_init(&sc->sc_read_mtx, "gg:read", NULL, MTX_DEF);
515 sc->sc_queue_count = 0;
516 sc->sc_queue_size = ggio->gctl_maxcount;
517 if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE)
518 sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE;
519 sc->sc_timeout = ggio->gctl_timeout;
520 callout_init(&sc->sc_callout, 1);
522 mtx_lock(&g_gate_units_lock);
523 sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error);
526 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
527 snprintf(name, sizeof(name), "%s", ggio->gctl_name);
529 snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME,
532 /* Check for name collision. */
533 for (unit = 0; unit < g_gate_maxunits; unit++) {
534 if (g_gate_units[unit] == NULL)
536 if (strcmp(name, g_gate_units[unit]->sc_name) != 0)
542 g_gate_units[sc->sc_unit] = sc;
544 mtx_unlock(&g_gate_units_lock);
548 if (ggio->gctl_readprov[0] == '\0') {
551 ropp = g_provider_by_name(ggio->gctl_readprov);
553 G_GATE_DEBUG(1, "Provider %s doesn't exist.",
554 ggio->gctl_readprov);
558 if ((ggio->gctl_readoffset % ggio->gctl_sectorsize) != 0) {
559 G_GATE_DEBUG(1, "Invalid read offset.");
563 if (ggio->gctl_mediasize + ggio->gctl_readoffset >
565 G_GATE_DEBUG(1, "Invalid read offset or media size.");
571 gp = g_new_geomf(&g_gate_class, "%s", name);
572 gp->start = g_gate_start;
573 gp->access = g_gate_access;
574 gp->orphan = g_gate_orphan;
575 gp->dumpconf = g_gate_dumpconf;
579 cp = g_new_consumer(gp);
580 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
581 error = g_attach(cp, ropp);
583 G_GATE_DEBUG(1, "Unable to attach to %s.", ropp->name);
586 error = g_access(cp, 1, 0, 0);
588 G_GATE_DEBUG(1, "Unable to access %s.", ropp->name);
592 sc->sc_readcons = cp;
593 sc->sc_readoffset = ggio->gctl_readoffset;
596 ggio->gctl_unit = sc->sc_unit;
598 pp = g_new_providerf(gp, "%s", name);
599 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
600 pp->mediasize = ggio->gctl_mediasize;
601 pp->sectorsize = ggio->gctl_sectorsize;
602 sc->sc_provider = pp;
603 g_error_provider(pp, 0);
606 mtx_lock(&g_gate_units_lock);
607 sc->sc_name = sc->sc_provider->name;
608 mtx_unlock(&g_gate_units_lock);
609 G_GATE_DEBUG(1, "Device %s created.", gp->name);
611 if (sc->sc_timeout > 0) {
612 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
617 g_destroy_consumer(cp);
621 mtx_lock(&g_gate_units_lock);
622 g_gate_units[sc->sc_unit] = NULL;
623 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?"));
626 mtx_unlock(&g_gate_units_lock);
627 mtx_destroy(&sc->sc_queue_mtx);
628 mtx_destroy(&sc->sc_read_mtx);
634 g_gate_modify(struct g_gate_softc *sc, struct g_gate_ctl_modify *ggio)
636 struct g_provider *pp;
637 struct g_consumer *cp;
640 if ((ggio->gctl_modify & GG_MODIFY_MEDIASIZE) != 0) {
641 if (ggio->gctl_mediasize <= 0) {
642 G_GATE_DEBUG(1, "Invalid media size.");
645 pp = sc->sc_provider;
646 if ((ggio->gctl_mediasize % pp->sectorsize) != 0) {
647 G_GATE_DEBUG(1, "Invalid media size.");
650 g_resize_provider(pp, ggio->gctl_mediasize);
654 if ((ggio->gctl_modify & GG_MODIFY_INFO) != 0)
655 (void)strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info));
659 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) {
661 mtx_lock(&sc->sc_read_mtx);
662 if ((cp = sc->sc_readcons) != NULL) {
663 sc->sc_readcons = NULL;
664 done = (cp->index == 0);
665 mtx_unlock(&sc->sc_read_mtx);
667 g_gate_detach(cp, 0);
669 mtx_unlock(&sc->sc_read_mtx);
670 if (ggio->gctl_readprov[0] != '\0') {
671 pp = g_provider_by_name(ggio->gctl_readprov);
674 G_GATE_DEBUG(1, "Provider %s doesn't exist.",
675 ggio->gctl_readprov);
678 cp = g_new_consumer(sc->sc_provider->geom);
679 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
680 error = g_attach(cp, pp);
682 G_GATE_DEBUG(1, "Unable to attach to %s.",
685 error = g_access(cp, 1, 0, 0);
687 G_GATE_DEBUG(1, "Unable to access %s.",
693 g_destroy_consumer(cp);
699 cp = sc->sc_readcons;
702 if ((ggio->gctl_modify & GG_MODIFY_READOFFSET) != 0) {
704 G_GATE_DEBUG(1, "No read provider.");
707 pp = sc->sc_provider;
708 if ((ggio->gctl_readoffset % pp->sectorsize) != 0) {
709 G_GATE_DEBUG(1, "Invalid read offset.");
712 if (pp->mediasize + ggio->gctl_readoffset >
713 cp->provider->mediasize) {
714 G_GATE_DEBUG(1, "Invalid read offset or media size.");
717 sc->sc_readoffset = ggio->gctl_readoffset;
720 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) {
721 sc->sc_readcons = cp;
728 #define G_GATE_CHECK_VERSION(ggio) do { \
729 if ((ggio)->gctl_version != G_GATE_VERSION) { \
730 printf("Version mismatch %d != %d.\n", \
731 ggio->gctl_version, G_GATE_VERSION); \
736 g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
738 struct g_gate_softc *sc;
742 G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr,
746 case G_GATE_CMD_CREATE:
748 struct g_gate_ctl_create *ggio = (void *)addr;
750 G_GATE_CHECK_VERSION(ggio);
751 error = g_gate_create(ggio);
753 * Reset TDP_GEOM flag.
754 * There are pending events for sure, because we just created
755 * new provider and other classes want to taste it, but we
756 * cannot answer on I/O requests until we're here.
758 td->td_pflags &= ~TDP_GEOM;
761 case G_GATE_CMD_MODIFY:
763 struct g_gate_ctl_modify *ggio = (void *)addr;
765 G_GATE_CHECK_VERSION(ggio);
766 sc = g_gate_hold(ggio->gctl_unit, NULL);
769 error = g_gate_modify(sc, ggio);
773 case G_GATE_CMD_DESTROY:
775 struct g_gate_ctl_destroy *ggio = (void *)addr;
777 G_GATE_CHECK_VERSION(ggio);
778 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
782 mtx_lock(&g_gate_units_lock);
783 error = g_gate_destroy(sc, ggio->gctl_force);
789 case G_GATE_CMD_CANCEL:
791 struct g_gate_ctl_cancel *ggio = (void *)addr;
792 struct bio *tbp, *lbp;
794 G_GATE_CHECK_VERSION(ggio);
795 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
799 mtx_lock(&sc->sc_queue_mtx);
800 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) {
801 if (ggio->gctl_seq == 0 ||
802 ggio->gctl_seq == (uintptr_t)bp->bio_driver1) {
803 G_GATE_LOGREQ(1, bp, "Request canceled.");
804 bioq_remove(&sc->sc_outqueue, bp);
806 * Be sure to put requests back onto incoming
807 * queue in the proper order.
810 bioq_insert_head(&sc->sc_inqueue, bp);
812 TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue,
817 * If only one request was canceled, leave now.
819 if (ggio->gctl_seq != 0)
823 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
824 ggio->gctl_unit = sc->sc_unit;
825 mtx_unlock(&sc->sc_queue_mtx);
829 case G_GATE_CMD_START:
831 struct g_gate_ctl_io *ggio = (void *)addr;
833 G_GATE_CHECK_VERSION(ggio);
834 sc = g_gate_hold(ggio->gctl_unit, NULL);
839 mtx_lock(&sc->sc_queue_mtx);
840 bp = bioq_first(&sc->sc_inqueue);
843 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
844 ggio->gctl_error = ECANCELED;
845 mtx_unlock(&sc->sc_queue_mtx);
848 if (msleep(sc, &sc->sc_queue_mtx,
849 PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) {
850 ggio->gctl_error = ECANCELED;
854 ggio->gctl_cmd = bp->bio_cmd;
855 if (bp->bio_cmd == BIO_WRITE &&
856 bp->bio_length > ggio->gctl_length) {
857 mtx_unlock(&sc->sc_queue_mtx);
858 ggio->gctl_length = bp->bio_length;
859 ggio->gctl_error = ENOMEM;
862 bioq_remove(&sc->sc_inqueue, bp);
863 bioq_insert_tail(&sc->sc_outqueue, bp);
864 mtx_unlock(&sc->sc_queue_mtx);
866 ggio->gctl_seq = (uintptr_t)bp->bio_driver1;
867 ggio->gctl_offset = bp->bio_offset;
868 ggio->gctl_length = bp->bio_length;
870 switch (bp->bio_cmd) {
877 error = copyout(bp->bio_data, ggio->gctl_data,
880 mtx_lock(&sc->sc_queue_mtx);
881 bioq_remove(&sc->sc_outqueue, bp);
882 bioq_insert_head(&sc->sc_inqueue, bp);
883 mtx_unlock(&sc->sc_queue_mtx);
892 case G_GATE_CMD_DONE:
894 struct g_gate_ctl_io *ggio = (void *)addr;
896 G_GATE_CHECK_VERSION(ggio);
897 sc = g_gate_hold(ggio->gctl_unit, NULL);
901 mtx_lock(&sc->sc_queue_mtx);
902 TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) {
903 if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1)
907 bioq_remove(&sc->sc_outqueue, bp);
908 sc->sc_queue_count--;
910 mtx_unlock(&sc->sc_queue_mtx);
913 * Request was probably canceled.
917 if (ggio->gctl_error == EAGAIN) {
919 G_GATE_LOGREQ(1, bp, "Request desisted.");
920 mtx_lock(&sc->sc_queue_mtx);
921 sc->sc_queue_count++;
922 bioq_insert_head(&sc->sc_inqueue, bp);
924 mtx_unlock(&sc->sc_queue_mtx);
926 bp->bio_error = ggio->gctl_error;
927 if (bp->bio_error == 0) {
928 bp->bio_completed = bp->bio_length;
929 switch (bp->bio_cmd) {
931 error = copyin(ggio->gctl_data,
932 bp->bio_data, bp->bio_length);
934 bp->bio_error = error;
943 G_GATE_LOGREQ(2, bp, "Request done.");
944 g_io_deliver(bp, bp->bio_error);
958 status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600,
963 g_gate_modevent(module_t mod, int type, void *data)
969 mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF);
970 g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]),
971 M_GATE, M_WAITOK | M_ZERO);
976 mtx_lock(&g_gate_units_lock);
977 if (g_gate_nunits > 0) {
978 mtx_unlock(&g_gate_units_lock);
982 mtx_unlock(&g_gate_units_lock);
983 mtx_destroy(&g_gate_units_lock);
984 if (status_dev != NULL)
985 destroy_dev(status_dev);
986 free(g_gate_units, M_GATE);
995 static moduledata_t g_gate_module = {
1000 DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1001 DECLARE_GEOM_CLASS(g_gate_class, g_gate);
1002 MODULE_VERSION(geom_gate, 0);