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;
471 char name[NAME_MAX + 1];
472 char readprov[NAME_MAX + 1];
475 if (ggio->gctl_mediasize <= 0) {
476 G_GATE_DEBUG(1, "Invalid media size.");
479 if (ggio->gctl_sectorsize <= 0) {
480 G_GATE_DEBUG(1, "Invalid sector size.");
483 if (!powerof2(ggio->gctl_sectorsize)) {
484 G_GATE_DEBUG(1, "Invalid sector size.");
487 if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) {
488 G_GATE_DEBUG(1, "Invalid media size.");
491 if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 &&
492 (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) {
493 G_GATE_DEBUG(1, "Invalid flags.");
496 if (ggio->gctl_unit != G_GATE_UNIT_AUTO &&
497 ggio->gctl_unit != G_GATE_NAME_GIVEN &&
498 ggio->gctl_unit < 0) {
499 G_GATE_DEBUG(1, "Invalid unit number.");
502 if (ggio->gctl_unit == G_GATE_NAME_GIVEN &&
503 ggio->gctl_name[0] == '\0') {
504 G_GATE_DEBUG(1, "No device name.");
508 sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO);
509 sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS);
510 memset(sc->sc_info, 0, sizeof(sc->sc_info));
511 strncpy(sc->sc_info, ggio->gctl_info,
512 MIN(sizeof(sc->sc_info) - 1, sizeof(ggio->gctl_info)));
514 bioq_init(&sc->sc_inqueue);
515 bioq_init(&sc->sc_outqueue);
516 mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF);
517 mtx_init(&sc->sc_read_mtx, "gg:read", NULL, MTX_DEF);
518 sc->sc_queue_count = 0;
519 sc->sc_queue_size = ggio->gctl_maxcount;
520 if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE)
521 sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE;
522 sc->sc_timeout = ggio->gctl_timeout;
523 callout_init(&sc->sc_callout, 1);
525 mtx_lock(&g_gate_units_lock);
526 sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error);
529 if (ggio->gctl_unit == G_GATE_NAME_GIVEN) {
530 memset(name, 0, sizeof(name));
531 strncpy(name, ggio->gctl_name,
532 MIN(sizeof(name) - 1, sizeof(ggio->gctl_name)));
534 snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME,
537 /* Check for name collision. */
538 for (unit = 0; unit < g_gate_maxunits; unit++) {
539 if (g_gate_units[unit] == NULL)
541 if (strcmp(name, g_gate_units[unit]->sc_name) != 0)
546 // local stack buffer 'name' assigned here temporarily only.
547 // the real provider name is assigned below.
549 g_gate_units[sc->sc_unit] = sc;
551 mtx_unlock(&g_gate_units_lock);
555 if (ggio->gctl_readprov[0] == '\0') {
558 memset(readprov, 0, sizeof(readprov));
559 strncpy(readprov, ggio->gctl_readprov,
560 MIN(sizeof(readprov) - 1, sizeof(ggio->gctl_readprov)));
561 ropp = g_provider_by_name(readprov);
563 G_GATE_DEBUG(1, "Provider %s doesn't exist.", readprov);
567 if ((ggio->gctl_readoffset % ggio->gctl_sectorsize) != 0) {
568 G_GATE_DEBUG(1, "Invalid read offset.");
572 if (ggio->gctl_mediasize + ggio->gctl_readoffset >
574 G_GATE_DEBUG(1, "Invalid read offset or media size.");
580 gp = g_new_geomf(&g_gate_class, "%s", name);
581 gp->start = g_gate_start;
582 gp->access = g_gate_access;
583 gp->orphan = g_gate_orphan;
584 gp->dumpconf = g_gate_dumpconf;
588 cp = g_new_consumer(gp);
589 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
590 error = g_attach(cp, ropp);
592 G_GATE_DEBUG(1, "Unable to attach to %s.", ropp->name);
595 error = g_access(cp, 1, 0, 0);
597 G_GATE_DEBUG(1, "Unable to access %s.", ropp->name);
601 sc->sc_readcons = cp;
602 sc->sc_readoffset = ggio->gctl_readoffset;
605 ggio->gctl_unit = sc->sc_unit;
607 pp = g_new_providerf(gp, "%s", name);
608 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
609 pp->mediasize = ggio->gctl_mediasize;
610 pp->sectorsize = ggio->gctl_sectorsize;
611 sc->sc_provider = pp;
612 g_error_provider(pp, 0);
615 mtx_lock(&g_gate_units_lock);
616 sc->sc_name = sc->sc_provider->name;
617 mtx_unlock(&g_gate_units_lock);
618 G_GATE_DEBUG(1, "Device %s created.", gp->name);
620 if (sc->sc_timeout > 0) {
621 callout_reset(&sc->sc_callout, sc->sc_timeout * hz,
626 g_destroy_consumer(cp);
630 mtx_lock(&g_gate_units_lock);
631 g_gate_units[sc->sc_unit] = NULL;
632 KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?"));
635 mtx_unlock(&g_gate_units_lock);
636 mtx_destroy(&sc->sc_queue_mtx);
637 mtx_destroy(&sc->sc_read_mtx);
643 g_gate_modify(struct g_gate_softc *sc, struct g_gate_ctl_modify *ggio)
645 char readprov[NAME_MAX + 1];
646 struct g_provider *pp;
647 struct g_consumer *cp;
650 if ((ggio->gctl_modify & GG_MODIFY_MEDIASIZE) != 0) {
651 if (ggio->gctl_mediasize <= 0) {
652 G_GATE_DEBUG(1, "Invalid media size.");
655 pp = sc->sc_provider;
656 if ((ggio->gctl_mediasize % pp->sectorsize) != 0) {
657 G_GATE_DEBUG(1, "Invalid media size.");
660 g_resize_provider(pp, ggio->gctl_mediasize);
664 if ((ggio->gctl_modify & GG_MODIFY_INFO) != 0) {
665 memset(sc->sc_info, 0, sizeof(sc->sc_info));
666 strncpy(sc->sc_info, ggio->gctl_info,
667 MIN(sizeof(sc->sc_info) - 1, sizeof(ggio->gctl_info)));
671 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) {
673 mtx_lock(&sc->sc_read_mtx);
674 if ((cp = sc->sc_readcons) != NULL) {
675 sc->sc_readcons = NULL;
676 done = (cp->index == 0);
677 mtx_unlock(&sc->sc_read_mtx);
679 g_gate_detach(cp, 0);
681 mtx_unlock(&sc->sc_read_mtx);
682 if (ggio->gctl_readprov[0] != '\0') {
683 memset(readprov, 0, sizeof(readprov));
684 strncpy(readprov, ggio->gctl_readprov,
685 MIN(sizeof(readprov) - 1,
686 sizeof(ggio->gctl_readprov)));
687 pp = g_provider_by_name(readprov);
690 G_GATE_DEBUG(1, "Provider %s doesn't exist.",
694 cp = g_new_consumer(sc->sc_provider->geom);
695 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
696 error = g_attach(cp, pp);
698 G_GATE_DEBUG(1, "Unable to attach to %s.",
701 error = g_access(cp, 1, 0, 0);
703 G_GATE_DEBUG(1, "Unable to access %s.",
709 g_destroy_consumer(cp);
715 cp = sc->sc_readcons;
718 if ((ggio->gctl_modify & GG_MODIFY_READOFFSET) != 0) {
720 G_GATE_DEBUG(1, "No read provider.");
723 pp = sc->sc_provider;
724 if ((ggio->gctl_readoffset % pp->sectorsize) != 0) {
725 G_GATE_DEBUG(1, "Invalid read offset.");
728 if (pp->mediasize + ggio->gctl_readoffset >
729 cp->provider->mediasize) {
730 G_GATE_DEBUG(1, "Invalid read offset or media size.");
733 sc->sc_readoffset = ggio->gctl_readoffset;
736 if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) {
737 sc->sc_readcons = cp;
744 #define G_GATE_CHECK_VERSION(ggio) do { \
745 if ((ggio)->gctl_version != G_GATE_VERSION) { \
746 printf("Version mismatch %d != %d.\n", \
747 ggio->gctl_version, G_GATE_VERSION); \
752 g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td)
754 struct g_gate_softc *sc;
758 G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr,
762 case G_GATE_CMD_CREATE:
764 struct g_gate_ctl_create *ggio = (void *)addr;
766 G_GATE_CHECK_VERSION(ggio);
767 error = g_gate_create(ggio);
769 * Reset TDP_GEOM flag.
770 * There are pending events for sure, because we just created
771 * new provider and other classes want to taste it, but we
772 * cannot answer on I/O requests until we're here.
774 td->td_pflags &= ~TDP_GEOM;
777 case G_GATE_CMD_MODIFY:
779 struct g_gate_ctl_modify *ggio = (void *)addr;
781 G_GATE_CHECK_VERSION(ggio);
782 sc = g_gate_hold(ggio->gctl_unit, NULL);
785 error = g_gate_modify(sc, ggio);
789 case G_GATE_CMD_DESTROY:
791 struct g_gate_ctl_destroy *ggio = (void *)addr;
793 G_GATE_CHECK_VERSION(ggio);
794 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
798 mtx_lock(&g_gate_units_lock);
799 error = g_gate_destroy(sc, ggio->gctl_force);
805 case G_GATE_CMD_CANCEL:
807 struct g_gate_ctl_cancel *ggio = (void *)addr;
808 struct bio *tbp, *lbp;
810 G_GATE_CHECK_VERSION(ggio);
811 sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name);
815 mtx_lock(&sc->sc_queue_mtx);
816 TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) {
817 if (ggio->gctl_seq == 0 ||
818 ggio->gctl_seq == (uintptr_t)bp->bio_driver1) {
819 G_GATE_LOGREQ(1, bp, "Request canceled.");
820 bioq_remove(&sc->sc_outqueue, bp);
822 * Be sure to put requests back onto incoming
823 * queue in the proper order.
826 bioq_insert_head(&sc->sc_inqueue, bp);
828 TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue,
833 * If only one request was canceled, leave now.
835 if (ggio->gctl_seq != 0)
839 if (ggio->gctl_unit == G_GATE_NAME_GIVEN)
840 ggio->gctl_unit = sc->sc_unit;
841 mtx_unlock(&sc->sc_queue_mtx);
845 case G_GATE_CMD_START:
847 struct g_gate_ctl_io *ggio = (void *)addr;
849 G_GATE_CHECK_VERSION(ggio);
850 sc = g_gate_hold(ggio->gctl_unit, NULL);
855 mtx_lock(&sc->sc_queue_mtx);
856 bp = bioq_first(&sc->sc_inqueue);
859 if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) {
860 ggio->gctl_error = ECANCELED;
861 mtx_unlock(&sc->sc_queue_mtx);
864 error = msleep(sc, &sc->sc_queue_mtx,
865 PPAUSE | PDROP | PCATCH, "ggwait", 0);
869 ggio->gctl_cmd = bp->bio_cmd;
870 if (bp->bio_cmd == BIO_WRITE &&
871 bp->bio_length > ggio->gctl_length) {
872 mtx_unlock(&sc->sc_queue_mtx);
873 ggio->gctl_length = bp->bio_length;
874 ggio->gctl_error = ENOMEM;
877 bioq_remove(&sc->sc_inqueue, bp);
878 bioq_insert_tail(&sc->sc_outqueue, bp);
879 mtx_unlock(&sc->sc_queue_mtx);
881 ggio->gctl_seq = (uintptr_t)bp->bio_driver1;
882 ggio->gctl_offset = bp->bio_offset;
883 ggio->gctl_length = bp->bio_length;
885 switch (bp->bio_cmd) {
892 error = copyout(bp->bio_data, ggio->gctl_data,
895 mtx_lock(&sc->sc_queue_mtx);
896 bioq_remove(&sc->sc_outqueue, bp);
897 bioq_insert_head(&sc->sc_inqueue, bp);
898 mtx_unlock(&sc->sc_queue_mtx);
907 case G_GATE_CMD_DONE:
909 struct g_gate_ctl_io *ggio = (void *)addr;
911 G_GATE_CHECK_VERSION(ggio);
912 sc = g_gate_hold(ggio->gctl_unit, NULL);
916 mtx_lock(&sc->sc_queue_mtx);
917 TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) {
918 if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1)
922 bioq_remove(&sc->sc_outqueue, bp);
923 sc->sc_queue_count--;
925 mtx_unlock(&sc->sc_queue_mtx);
928 * Request was probably canceled.
932 if (ggio->gctl_error == EAGAIN) {
934 G_GATE_LOGREQ(1, bp, "Request desisted.");
935 mtx_lock(&sc->sc_queue_mtx);
936 sc->sc_queue_count++;
937 bioq_insert_head(&sc->sc_inqueue, bp);
939 mtx_unlock(&sc->sc_queue_mtx);
941 bp->bio_error = ggio->gctl_error;
942 if (bp->bio_error == 0) {
943 bp->bio_completed = bp->bio_length;
944 switch (bp->bio_cmd) {
946 error = copyin(ggio->gctl_data,
947 bp->bio_data, bp->bio_length);
949 bp->bio_error = error;
958 G_GATE_LOGREQ(2, bp, "Request done.");
959 g_io_deliver(bp, bp->bio_error);
973 status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600,
978 g_gate_modevent(module_t mod, int type, void *data)
984 mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF);
985 g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]),
986 M_GATE, M_WAITOK | M_ZERO);
991 mtx_lock(&g_gate_units_lock);
992 if (g_gate_nunits > 0) {
993 mtx_unlock(&g_gate_units_lock);
997 mtx_unlock(&g_gate_units_lock);
998 mtx_destroy(&g_gate_units_lock);
999 if (status_dev != NULL)
1000 destroy_dev(status_dev);
1001 free(g_gate_units, M_GATE);
1004 return (EOPNOTSUPP);
1010 static moduledata_t g_gate_module = {
1015 DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1016 DECLARE_GEOM_CLASS(g_gate_class, g_gate);
1017 MODULE_VERSION(geom_gate, 0);