1fe27e772SPawel Jakub Dawidek /*- 2fc024f7aSPawel Jakub Dawidek * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 332115b10SPawel Jakub Dawidek * Copyright (c) 2009-2010 The FreeBSD Foundation 4fe27e772SPawel Jakub Dawidek * All rights reserved. 5fe27e772SPawel Jakub Dawidek * 632115b10SPawel Jakub Dawidek * Portions of this software were developed by Pawel Jakub Dawidek 732115b10SPawel Jakub Dawidek * under sponsorship from the FreeBSD Foundation. 832115b10SPawel Jakub Dawidek * 9fe27e772SPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without 10fe27e772SPawel Jakub Dawidek * modification, are permitted provided that the following conditions 11fe27e772SPawel Jakub Dawidek * are met: 12fe27e772SPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright 13fe27e772SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer. 14fe27e772SPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright 15fe27e772SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer in the 16fe27e772SPawel Jakub Dawidek * documentation and/or other materials provided with the distribution. 17fe27e772SPawel Jakub Dawidek * 18fe27e772SPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 19fe27e772SPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20fe27e772SPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21fe27e772SPawel Jakub Dawidek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 22fe27e772SPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23fe27e772SPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24fe27e772SPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25fe27e772SPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26fe27e772SPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27fe27e772SPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28fe27e772SPawel Jakub Dawidek * SUCH DAMAGE. 29fe27e772SPawel Jakub Dawidek */ 30fe27e772SPawel Jakub Dawidek 31c0767902SPawel Jakub Dawidek #include <sys/cdefs.h> 32c0767902SPawel Jakub Dawidek __FBSDID("$FreeBSD$"); 33c0767902SPawel Jakub Dawidek 34fe27e772SPawel Jakub Dawidek #include <sys/param.h> 35fe27e772SPawel Jakub Dawidek #include <sys/systm.h> 36fe27e772SPawel Jakub Dawidek #include <sys/bio.h> 37fe27e772SPawel Jakub Dawidek #include <sys/conf.h> 38fe27e772SPawel Jakub Dawidek #include <sys/kernel.h> 39fe27e772SPawel Jakub Dawidek #include <sys/kthread.h> 40fe27e772SPawel Jakub Dawidek #include <sys/fcntl.h> 41fe27e772SPawel Jakub Dawidek #include <sys/linker.h> 42fe27e772SPawel Jakub Dawidek #include <sys/lock.h> 43fe27e772SPawel Jakub Dawidek #include <sys/malloc.h> 44fe27e772SPawel Jakub Dawidek #include <sys/mutex.h> 45fe27e772SPawel Jakub Dawidek #include <sys/proc.h> 46fe27e772SPawel Jakub Dawidek #include <sys/limits.h> 47fe27e772SPawel Jakub Dawidek #include <sys/queue.h> 485d807a0eSAndrey V. Elsukov #include <sys/sbuf.h> 49fe27e772SPawel Jakub Dawidek #include <sys/sysctl.h> 50fe27e772SPawel Jakub Dawidek #include <sys/signalvar.h> 51fe27e772SPawel Jakub Dawidek #include <sys/time.h> 52fe27e772SPawel Jakub Dawidek #include <machine/atomic.h> 53fe27e772SPawel Jakub Dawidek 54fe27e772SPawel Jakub Dawidek #include <geom/geom.h> 55fe27e772SPawel Jakub Dawidek #include <geom/gate/g_gate.h> 56fe27e772SPawel Jakub Dawidek 57cb08c2ccSAlexander Leidinger FEATURE(geom_gate, "GEOM Gate module"); 58cb08c2ccSAlexander Leidinger 595bb84bc8SRobert Watson static MALLOC_DEFINE(M_GATE, "gg_data", "GEOM Gate Data"); 60fe27e772SPawel Jakub Dawidek 61fe27e772SPawel Jakub Dawidek SYSCTL_DECL(_kern_geom); 626472ac3dSEd Schouten static SYSCTL_NODE(_kern_geom, OID_AUTO, gate, CTLFLAG_RW, 0, 63e08ec037SPawel Jakub Dawidek "GEOM_GATE configuration"); 6432115b10SPawel Jakub Dawidek static int g_gate_debug = 0; 6532115b10SPawel Jakub Dawidek TUNABLE_INT("kern.geom.gate.debug", &g_gate_debug); 6632115b10SPawel Jakub Dawidek SYSCTL_INT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RW, &g_gate_debug, 0, 67fe27e772SPawel Jakub Dawidek "Debug level"); 6832115b10SPawel Jakub Dawidek static u_int g_gate_maxunits = 256; 6932115b10SPawel Jakub Dawidek TUNABLE_INT("kern.geom.gate.maxunits", &g_gate_maxunits); 7032115b10SPawel Jakub Dawidek SYSCTL_UINT(_kern_geom_gate, OID_AUTO, maxunits, CTLFLAG_RDTUN, 7132115b10SPawel Jakub Dawidek &g_gate_maxunits, 0, "Maximum number of ggate devices"); 72fe27e772SPawel Jakub Dawidek 73fe27e772SPawel Jakub Dawidek struct g_class g_gate_class = { 74fe27e772SPawel Jakub Dawidek .name = G_GATE_CLASS_NAME, 755721c9c7SPoul-Henning Kamp .version = G_VERSION, 76fe27e772SPawel Jakub Dawidek }; 77fe27e772SPawel Jakub Dawidek 7889c9c53dSPoul-Henning Kamp static struct cdev *status_dev; 79fe27e772SPawel Jakub Dawidek static d_ioctl_t g_gate_ioctl; 80fe27e772SPawel Jakub Dawidek static struct cdevsw g_gate_cdevsw = { 81fe27e772SPawel Jakub Dawidek .d_version = D_VERSION, 82fe27e772SPawel Jakub Dawidek .d_ioctl = g_gate_ioctl, 83fe27e772SPawel Jakub Dawidek .d_name = G_GATE_CTL_NAME 84fe27e772SPawel Jakub Dawidek }; 85fe27e772SPawel Jakub Dawidek 86fe27e772SPawel Jakub Dawidek 8732115b10SPawel Jakub Dawidek static struct g_gate_softc **g_gate_units; 8832115b10SPawel Jakub Dawidek static u_int g_gate_nunits; 8932115b10SPawel Jakub Dawidek static struct mtx g_gate_units_lock; 90fe27e772SPawel Jakub Dawidek 91fe27e772SPawel Jakub Dawidek static int 92fe27e772SPawel Jakub Dawidek g_gate_destroy(struct g_gate_softc *sc, boolean_t force) 93fe27e772SPawel Jakub Dawidek { 94*40ea77a0SAlexander Motin struct bio_queue_head queue; 95fe27e772SPawel Jakub Dawidek struct g_provider *pp; 96e08ec037SPawel Jakub Dawidek struct g_consumer *cp; 977ffb6e0fSPawel Jakub Dawidek struct g_geom *gp; 98fe27e772SPawel Jakub Dawidek struct bio *bp; 99fe27e772SPawel Jakub Dawidek 100fe27e772SPawel Jakub Dawidek g_topology_assert(); 10132115b10SPawel Jakub Dawidek mtx_assert(&g_gate_units_lock, MA_OWNED); 102fe27e772SPawel Jakub Dawidek pp = sc->sc_provider; 103fe27e772SPawel Jakub Dawidek if (!force && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 10432115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 105fe27e772SPawel Jakub Dawidek return (EBUSY); 106fe27e772SPawel Jakub Dawidek } 10732115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 108e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 1097ffb6e0fSPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) 1107ffb6e0fSPawel Jakub Dawidek sc->sc_flags |= G_GATE_FLAG_DESTROY; 111fe27e772SPawel Jakub Dawidek wakeup(sc); 112e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 1137ffb6e0fSPawel Jakub Dawidek gp = pp->geom; 1147ffb6e0fSPawel Jakub Dawidek pp->flags |= G_PF_WITHER; 1157ffb6e0fSPawel Jakub Dawidek g_orphan_provider(pp, ENXIO); 116fe27e772SPawel Jakub Dawidek callout_drain(&sc->sc_callout); 117*40ea77a0SAlexander Motin bioq_init(&queue); 118e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 119*40ea77a0SAlexander Motin while ((bp = bioq_takefirst(&sc->sc_inqueue)) != NULL) { 120e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 121*40ea77a0SAlexander Motin bioq_insert_tail(&queue, bp); 122fe27e772SPawel Jakub Dawidek } 123*40ea77a0SAlexander Motin while ((bp = bioq_takefirst(&sc->sc_outqueue)) != NULL) { 124e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 125*40ea77a0SAlexander Motin bioq_insert_tail(&queue, bp); 126fe27e772SPawel Jakub Dawidek } 1277ffb6e0fSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 1287ffb6e0fSPawel Jakub Dawidek g_topology_unlock(); 129*40ea77a0SAlexander Motin while ((bp = bioq_takefirst(&queue)) != NULL) { 130*40ea77a0SAlexander Motin G_GATE_LOGREQ(1, bp, "Request canceled."); 131*40ea77a0SAlexander Motin g_io_deliver(bp, ENXIO); 132*40ea77a0SAlexander Motin } 13332115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 1347ffb6e0fSPawel Jakub Dawidek /* One reference is ours. */ 1357ffb6e0fSPawel Jakub Dawidek sc->sc_ref--; 13632115b10SPawel Jakub Dawidek while (sc->sc_ref > 0) 13732115b10SPawel Jakub Dawidek msleep(&sc->sc_ref, &g_gate_units_lock, 0, "gg:destroy", 0); 13832115b10SPawel Jakub Dawidek g_gate_units[sc->sc_unit] = NULL; 13932115b10SPawel Jakub Dawidek KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?")); 14032115b10SPawel Jakub Dawidek g_gate_nunits--; 14132115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 142e35d3a78SPawel Jakub Dawidek mtx_destroy(&sc->sc_queue_mtx); 1437ffb6e0fSPawel Jakub Dawidek g_topology_lock(); 144e08ec037SPawel Jakub Dawidek if ((cp = sc->sc_readcons) != NULL) { 145e08ec037SPawel Jakub Dawidek sc->sc_readcons = NULL; 146e08ec037SPawel Jakub Dawidek (void)g_access(cp, -1, 0, 0); 147e08ec037SPawel Jakub Dawidek g_detach(cp); 148e08ec037SPawel Jakub Dawidek g_destroy_consumer(cp); 149e08ec037SPawel Jakub Dawidek } 150bd119384SMikolaj Golub G_GATE_DEBUG(1, "Device %s destroyed.", gp->name); 1517ffb6e0fSPawel Jakub Dawidek gp->softc = NULL; 1527ffb6e0fSPawel Jakub Dawidek g_wither_geom(gp, ENXIO); 153fe27e772SPawel Jakub Dawidek sc->sc_provider = NULL; 154fe27e772SPawel Jakub Dawidek free(sc, M_GATE); 155fe27e772SPawel Jakub Dawidek return (0); 156fe27e772SPawel Jakub Dawidek } 157fe27e772SPawel Jakub Dawidek 158fe27e772SPawel Jakub Dawidek static int 159fe27e772SPawel Jakub Dawidek g_gate_access(struct g_provider *pp, int dr, int dw, int de) 160fe27e772SPawel Jakub Dawidek { 161fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 162fe27e772SPawel Jakub Dawidek 163fe27e772SPawel Jakub Dawidek if (dr <= 0 && dw <= 0 && de <= 0) 164fe27e772SPawel Jakub Dawidek return (0); 165fe27e772SPawel Jakub Dawidek sc = pp->geom->softc; 166fe27e772SPawel Jakub Dawidek if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) 167fe27e772SPawel Jakub Dawidek return (ENXIO); 16855336b83SPawel Jakub Dawidek /* XXX: Hack to allow read-only mounts. */ 16955336b83SPawel Jakub Dawidek #if 0 170fe27e772SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0) 171fe27e772SPawel Jakub Dawidek return (EPERM); 17255336b83SPawel Jakub Dawidek #endif 173fe27e772SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0) 174fe27e772SPawel Jakub Dawidek return (EPERM); 175fe27e772SPawel Jakub Dawidek return (0); 176fe27e772SPawel Jakub Dawidek } 177fe27e772SPawel Jakub Dawidek 178fe27e772SPawel Jakub Dawidek static void 179e08ec037SPawel Jakub Dawidek g_gate_queue_io(struct bio *bp) 180fe27e772SPawel Jakub Dawidek { 181fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 182fe27e772SPawel Jakub Dawidek 183fe27e772SPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 184fe27e772SPawel Jakub Dawidek if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 185fe27e772SPawel Jakub Dawidek g_io_deliver(bp, ENXIO); 186fe27e772SPawel Jakub Dawidek return; 187fe27e772SPawel Jakub Dawidek } 188fe27e772SPawel Jakub Dawidek 189e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 190e08ec037SPawel Jakub Dawidek 19163a6c5c1SPawel Jakub Dawidek if (sc->sc_queue_size > 0 && sc->sc_queue_count > sc->sc_queue_size) { 19235f855d9SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 193fe27e772SPawel Jakub Dawidek G_GATE_LOGREQ(1, bp, "Queue full, request canceled."); 19432115b10SPawel Jakub Dawidek g_io_deliver(bp, ENOMEM); 195fe27e772SPawel Jakub Dawidek return; 196fe27e772SPawel Jakub Dawidek } 197e35d3a78SPawel Jakub Dawidek 198fe27e772SPawel Jakub Dawidek bp->bio_driver1 = (void *)sc->sc_seq; 199fe27e772SPawel Jakub Dawidek sc->sc_seq++; 200e35d3a78SPawel Jakub Dawidek sc->sc_queue_count++; 201fe27e772SPawel Jakub Dawidek 202662a4e58SPawel Jakub Dawidek bioq_insert_tail(&sc->sc_inqueue, bp); 203fe27e772SPawel Jakub Dawidek wakeup(sc); 204e35d3a78SPawel Jakub Dawidek 205e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 206fe27e772SPawel Jakub Dawidek } 207fe27e772SPawel Jakub Dawidek 208e08ec037SPawel Jakub Dawidek static void 209e08ec037SPawel Jakub Dawidek g_gate_done(struct bio *cbp) 210e08ec037SPawel Jakub Dawidek { 211e08ec037SPawel Jakub Dawidek struct bio *pbp; 212e08ec037SPawel Jakub Dawidek 213e08ec037SPawel Jakub Dawidek pbp = cbp->bio_parent; 214e08ec037SPawel Jakub Dawidek if (cbp->bio_error == 0) { 215e08ec037SPawel Jakub Dawidek pbp->bio_completed = cbp->bio_completed; 216e08ec037SPawel Jakub Dawidek g_destroy_bio(cbp); 217e08ec037SPawel Jakub Dawidek pbp->bio_inbed++; 218e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, 0); 219e08ec037SPawel Jakub Dawidek } else { 220e08ec037SPawel Jakub Dawidek /* If direct read failed, pass it through userland daemon. */ 221e08ec037SPawel Jakub Dawidek g_destroy_bio(cbp); 222e08ec037SPawel Jakub Dawidek pbp->bio_children--; 223e08ec037SPawel Jakub Dawidek g_gate_queue_io(pbp); 224e08ec037SPawel Jakub Dawidek } 225e08ec037SPawel Jakub Dawidek } 226e08ec037SPawel Jakub Dawidek 227e08ec037SPawel Jakub Dawidek static void 228e08ec037SPawel Jakub Dawidek g_gate_start(struct bio *pbp) 229e08ec037SPawel Jakub Dawidek { 230e08ec037SPawel Jakub Dawidek struct g_gate_softc *sc; 231e08ec037SPawel Jakub Dawidek 232e08ec037SPawel Jakub Dawidek sc = pbp->bio_to->geom->softc; 233e08ec037SPawel Jakub Dawidek if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 234e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, ENXIO); 235e08ec037SPawel Jakub Dawidek return; 236e08ec037SPawel Jakub Dawidek } 237e08ec037SPawel Jakub Dawidek G_GATE_LOGREQ(2, pbp, "Request received."); 238e08ec037SPawel Jakub Dawidek switch (pbp->bio_cmd) { 239e08ec037SPawel Jakub Dawidek case BIO_READ: 240e08ec037SPawel Jakub Dawidek if (sc->sc_readcons != NULL) { 241e08ec037SPawel Jakub Dawidek struct bio *cbp; 242e08ec037SPawel Jakub Dawidek 243e08ec037SPawel Jakub Dawidek cbp = g_clone_bio(pbp); 244e08ec037SPawel Jakub Dawidek if (cbp == NULL) { 245e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, ENOMEM); 246e08ec037SPawel Jakub Dawidek return; 247e08ec037SPawel Jakub Dawidek } 248e08ec037SPawel Jakub Dawidek cbp->bio_done = g_gate_done; 249e08ec037SPawel Jakub Dawidek cbp->bio_offset = pbp->bio_offset + sc->sc_readoffset; 250e08ec037SPawel Jakub Dawidek cbp->bio_to = sc->sc_readcons->provider; 251e08ec037SPawel Jakub Dawidek g_io_request(cbp, sc->sc_readcons); 252e08ec037SPawel Jakub Dawidek return; 253e08ec037SPawel Jakub Dawidek } 254e08ec037SPawel Jakub Dawidek break; 255e08ec037SPawel Jakub Dawidek case BIO_DELETE: 256e08ec037SPawel Jakub Dawidek case BIO_WRITE: 257e08ec037SPawel Jakub Dawidek case BIO_FLUSH: 258e08ec037SPawel Jakub Dawidek /* XXX: Hack to allow read-only mounts. */ 259e08ec037SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) { 260e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, EPERM); 261e08ec037SPawel Jakub Dawidek return; 262e08ec037SPawel Jakub Dawidek } 263e08ec037SPawel Jakub Dawidek break; 264e08ec037SPawel Jakub Dawidek case BIO_GETATTR: 265e08ec037SPawel Jakub Dawidek default: 266e08ec037SPawel Jakub Dawidek G_GATE_LOGREQ(2, pbp, "Ignoring request."); 267e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, EOPNOTSUPP); 268e08ec037SPawel Jakub Dawidek return; 269e08ec037SPawel Jakub Dawidek } 270e08ec037SPawel Jakub Dawidek 271e08ec037SPawel Jakub Dawidek g_gate_queue_io(pbp); 272e08ec037SPawel Jakub Dawidek } 273e08ec037SPawel Jakub Dawidek 274fe27e772SPawel Jakub Dawidek static struct g_gate_softc * 2752aa15ffdSPawel Jakub Dawidek g_gate_hold(int unit, const char *name) 276fe27e772SPawel Jakub Dawidek { 27732115b10SPawel Jakub Dawidek struct g_gate_softc *sc = NULL; 278fe27e772SPawel Jakub Dawidek 27932115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 28032115b10SPawel Jakub Dawidek if (unit >= 0 && unit < g_gate_maxunits) 28132115b10SPawel Jakub Dawidek sc = g_gate_units[unit]; 28232115b10SPawel Jakub Dawidek else if (unit == G_GATE_NAME_GIVEN) { 28332115b10SPawel Jakub Dawidek KASSERT(name != NULL, ("name is NULL")); 28432115b10SPawel Jakub Dawidek for (unit = 0; unit < g_gate_maxunits; unit++) { 28532115b10SPawel Jakub Dawidek if (g_gate_units[unit] == NULL) 28632115b10SPawel Jakub Dawidek continue; 28732115b10SPawel Jakub Dawidek if (strcmp(name, 28832115b10SPawel Jakub Dawidek g_gate_units[unit]->sc_provider->name) != 0) { 28932115b10SPawel Jakub Dawidek continue; 29032115b10SPawel Jakub Dawidek } 29132115b10SPawel Jakub Dawidek sc = g_gate_units[unit]; 2927ffb6e0fSPawel Jakub Dawidek break; 293fe27e772SPawel Jakub Dawidek } 29432115b10SPawel Jakub Dawidek } 2957ffb6e0fSPawel Jakub Dawidek if (sc != NULL) 2967ffb6e0fSPawel Jakub Dawidek sc->sc_ref++; 29732115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 298fe27e772SPawel Jakub Dawidek return (sc); 299fe27e772SPawel Jakub Dawidek } 300fe27e772SPawel Jakub Dawidek 301fe27e772SPawel Jakub Dawidek static void 302fe27e772SPawel Jakub Dawidek g_gate_release(struct g_gate_softc *sc) 303fe27e772SPawel Jakub Dawidek { 304fe27e772SPawel Jakub Dawidek 305fe27e772SPawel Jakub Dawidek g_topology_assert_not(); 30632115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 307fe27e772SPawel Jakub Dawidek sc->sc_ref--; 308fe27e772SPawel Jakub Dawidek KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name)); 30932115b10SPawel Jakub Dawidek if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) 3107ffb6e0fSPawel Jakub Dawidek wakeup(&sc->sc_ref); 31132115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 312fe27e772SPawel Jakub Dawidek } 313fe27e772SPawel Jakub Dawidek 314fe27e772SPawel Jakub Dawidek static int 31532115b10SPawel Jakub Dawidek g_gate_getunit(int unit, int *errorp) 316fe27e772SPawel Jakub Dawidek { 317fe27e772SPawel Jakub Dawidek 31832115b10SPawel Jakub Dawidek mtx_assert(&g_gate_units_lock, MA_OWNED); 319fe27e772SPawel Jakub Dawidek if (unit >= 0) { 32032115b10SPawel Jakub Dawidek if (unit >= g_gate_maxunits) 32132115b10SPawel Jakub Dawidek *errorp = EINVAL; 32232115b10SPawel Jakub Dawidek else if (g_gate_units[unit] == NULL) 323fe27e772SPawel Jakub Dawidek return (unit); 32432115b10SPawel Jakub Dawidek else 32532115b10SPawel Jakub Dawidek *errorp = EEXIST; 32632115b10SPawel Jakub Dawidek } else { 32732115b10SPawel Jakub Dawidek for (unit = 0; unit < g_gate_maxunits; unit++) { 32832115b10SPawel Jakub Dawidek if (g_gate_units[unit] == NULL) 32932115b10SPawel Jakub Dawidek return (unit); 33032115b10SPawel Jakub Dawidek } 33132115b10SPawel Jakub Dawidek *errorp = ENFILE; 33232115b10SPawel Jakub Dawidek } 33332115b10SPawel Jakub Dawidek return (-1); 334fe27e772SPawel Jakub Dawidek } 335fe27e772SPawel Jakub Dawidek 336fe27e772SPawel Jakub Dawidek static void 337fe27e772SPawel Jakub Dawidek g_gate_guard(void *arg) 338fe27e772SPawel Jakub Dawidek { 339*40ea77a0SAlexander Motin struct bio_queue_head queue; 340fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 341fe27e772SPawel Jakub Dawidek struct bintime curtime; 342fe27e772SPawel Jakub Dawidek struct bio *bp, *bp2; 343fe27e772SPawel Jakub Dawidek 344fe27e772SPawel Jakub Dawidek sc = arg; 345fe27e772SPawel Jakub Dawidek binuptime(&curtime); 34632115b10SPawel Jakub Dawidek g_gate_hold(sc->sc_unit, NULL); 347*40ea77a0SAlexander Motin bioq_init(&queue); 348e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 349fe27e772SPawel Jakub Dawidek TAILQ_FOREACH_SAFE(bp, &sc->sc_inqueue.queue, bio_queue, bp2) { 350fe27e772SPawel Jakub Dawidek if (curtime.sec - bp->bio_t0.sec < 5) 351fe27e772SPawel Jakub Dawidek continue; 352fe27e772SPawel Jakub Dawidek bioq_remove(&sc->sc_inqueue, bp); 353e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 354*40ea77a0SAlexander Motin bioq_insert_tail(&queue, bp); 355fe27e772SPawel Jakub Dawidek } 356fe27e772SPawel Jakub Dawidek TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, bp2) { 357fe27e772SPawel Jakub Dawidek if (curtime.sec - bp->bio_t0.sec < 5) 358fe27e772SPawel Jakub Dawidek continue; 359fe27e772SPawel Jakub Dawidek bioq_remove(&sc->sc_outqueue, bp); 360e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 361*40ea77a0SAlexander Motin bioq_insert_tail(&queue, bp); 362*40ea77a0SAlexander Motin } 363*40ea77a0SAlexander Motin mtx_unlock(&sc->sc_queue_mtx); 364*40ea77a0SAlexander Motin while ((bp = bioq_takefirst(&queue)) != NULL) { 365fe27e772SPawel Jakub Dawidek G_GATE_LOGREQ(1, bp, "Request timeout."); 366fe27e772SPawel Jakub Dawidek g_io_deliver(bp, EIO); 367fe27e772SPawel Jakub Dawidek } 368fe27e772SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) { 369fe27e772SPawel Jakub Dawidek callout_reset(&sc->sc_callout, sc->sc_timeout * hz, 370fe27e772SPawel Jakub Dawidek g_gate_guard, sc); 371fe27e772SPawel Jakub Dawidek } 372fe27e772SPawel Jakub Dawidek g_gate_release(sc); 373fe27e772SPawel Jakub Dawidek } 374fe27e772SPawel Jakub Dawidek 375fe27e772SPawel Jakub Dawidek static void 376e08ec037SPawel Jakub Dawidek g_gate_orphan(struct g_consumer *cp) 377e08ec037SPawel Jakub Dawidek { 378e08ec037SPawel Jakub Dawidek struct g_gate_softc *sc; 379e08ec037SPawel Jakub Dawidek struct g_geom *gp; 380e08ec037SPawel Jakub Dawidek 381e08ec037SPawel Jakub Dawidek g_topology_assert(); 382e08ec037SPawel Jakub Dawidek gp = cp->geom; 383e08ec037SPawel Jakub Dawidek sc = gp->softc; 384e08ec037SPawel Jakub Dawidek if (sc == NULL) 385e08ec037SPawel Jakub Dawidek return; 386e08ec037SPawel Jakub Dawidek KASSERT(cp == sc->sc_readcons, ("cp=%p sc_readcons=%p", cp, 387e08ec037SPawel Jakub Dawidek sc->sc_readcons)); 388e08ec037SPawel Jakub Dawidek sc->sc_readcons = NULL; 389e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Destroying read consumer on provider %s orphan.", 390e08ec037SPawel Jakub Dawidek cp->provider->name); 391e08ec037SPawel Jakub Dawidek (void)g_access(cp, -1, 0, 0); 392e08ec037SPawel Jakub Dawidek g_detach(cp); 393e08ec037SPawel Jakub Dawidek g_destroy_consumer(cp); 394e08ec037SPawel Jakub Dawidek } 395e08ec037SPawel Jakub Dawidek 396e08ec037SPawel Jakub Dawidek static void 397fe27e772SPawel Jakub Dawidek g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 398fe27e772SPawel Jakub Dawidek struct g_consumer *cp, struct g_provider *pp) 399fe27e772SPawel Jakub Dawidek { 400fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 401fe27e772SPawel Jakub Dawidek 402fe27e772SPawel Jakub Dawidek sc = gp->softc; 403fe27e772SPawel Jakub Dawidek if (sc == NULL || pp != NULL || cp != NULL) 404fe27e772SPawel Jakub Dawidek return; 4051d9db37cSMikolaj Golub sc = g_gate_hold(sc->sc_unit, NULL); 4061d9db37cSMikolaj Golub if (sc == NULL) 4071d9db37cSMikolaj Golub return; 408fe27e772SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) { 409fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only"); 410fe27e772SPawel Jakub Dawidek } else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) { 411fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<access>%s</access>\n", indent, 412fe27e772SPawel Jakub Dawidek "write-only"); 413fe27e772SPawel Jakub Dawidek } else { 414fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<access>%s</access>\n", indent, 415fe27e772SPawel Jakub Dawidek "read-write"); 416fe27e772SPawel Jakub Dawidek } 417e08ec037SPawel Jakub Dawidek if (sc->sc_readcons != NULL) { 418e08ec037SPawel Jakub Dawidek sbuf_printf(sb, "%s<read_offset>%jd</read_offset>\n", 419e08ec037SPawel Jakub Dawidek indent, (intmax_t)sc->sc_readoffset); 420e08ec037SPawel Jakub Dawidek sbuf_printf(sb, "%s<read_provider>%s</read_provider>\n", 421e08ec037SPawel Jakub Dawidek indent, sc->sc_readcons->provider->name); 422e08ec037SPawel Jakub Dawidek } 423fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout); 424fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info); 425fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent, 426fe27e772SPawel Jakub Dawidek sc->sc_queue_count); 427fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent, 428fe27e772SPawel Jakub Dawidek sc->sc_queue_size); 429fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref); 43032115b10SPawel Jakub Dawidek sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, sc->sc_unit); 43147f44cb7SPawel Jakub Dawidek g_topology_unlock(); 432fe27e772SPawel Jakub Dawidek g_gate_release(sc); 43347f44cb7SPawel Jakub Dawidek g_topology_lock(); 434fe27e772SPawel Jakub Dawidek } 435fe27e772SPawel Jakub Dawidek 436fe27e772SPawel Jakub Dawidek static int 437fe27e772SPawel Jakub Dawidek g_gate_create(struct g_gate_ctl_create *ggio) 438fe27e772SPawel Jakub Dawidek { 439fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 440fe27e772SPawel Jakub Dawidek struct g_geom *gp; 441e08ec037SPawel Jakub Dawidek struct g_provider *pp, *ropp; 442e08ec037SPawel Jakub Dawidek struct g_consumer *cp; 44332115b10SPawel Jakub Dawidek char name[NAME_MAX]; 44432115b10SPawel Jakub Dawidek int error = 0, unit; 445fe27e772SPawel Jakub Dawidek 446e08ec037SPawel Jakub Dawidek if (ggio->gctl_mediasize <= 0) { 447fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid media size."); 448fe27e772SPawel Jakub Dawidek return (EINVAL); 449fe27e772SPawel Jakub Dawidek } 450e08ec037SPawel Jakub Dawidek if (ggio->gctl_sectorsize <= 0) { 451e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid sector size."); 452e08ec037SPawel Jakub Dawidek return (EINVAL); 453e08ec037SPawel Jakub Dawidek } 454e08ec037SPawel Jakub Dawidek if (!powerof2(ggio->gctl_sectorsize)) { 455fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid sector size."); 456fe27e772SPawel Jakub Dawidek return (EINVAL); 457fe27e772SPawel Jakub Dawidek } 458662a4e58SPawel Jakub Dawidek if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) { 459662a4e58SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid media size."); 460662a4e58SPawel Jakub Dawidek return (EINVAL); 461662a4e58SPawel Jakub Dawidek } 462fe27e772SPawel Jakub Dawidek if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 && 463fe27e772SPawel Jakub Dawidek (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) { 464fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid flags."); 465fe27e772SPawel Jakub Dawidek return (EINVAL); 466fe27e772SPawel Jakub Dawidek } 46732115b10SPawel Jakub Dawidek if (ggio->gctl_unit != G_GATE_UNIT_AUTO && 46832115b10SPawel Jakub Dawidek ggio->gctl_unit != G_GATE_NAME_GIVEN && 46932115b10SPawel Jakub Dawidek ggio->gctl_unit < 0) { 470fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid unit number."); 471fe27e772SPawel Jakub Dawidek return (EINVAL); 472fe27e772SPawel Jakub Dawidek } 47332115b10SPawel Jakub Dawidek if (ggio->gctl_unit == G_GATE_NAME_GIVEN && 47432115b10SPawel Jakub Dawidek ggio->gctl_name[0] == '\0') { 47532115b10SPawel Jakub Dawidek G_GATE_DEBUG(1, "No device name."); 47632115b10SPawel Jakub Dawidek return (EINVAL); 47732115b10SPawel Jakub Dawidek } 478fe27e772SPawel Jakub Dawidek 479fe27e772SPawel Jakub Dawidek sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO); 480fe27e772SPawel Jakub Dawidek sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS); 481fe27e772SPawel Jakub Dawidek strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info)); 48232115b10SPawel Jakub Dawidek sc->sc_seq = 1; 483fe27e772SPawel Jakub Dawidek bioq_init(&sc->sc_inqueue); 484fe27e772SPawel Jakub Dawidek bioq_init(&sc->sc_outqueue); 485e35d3a78SPawel Jakub Dawidek mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF); 486fe27e772SPawel Jakub Dawidek sc->sc_queue_count = 0; 487fe27e772SPawel Jakub Dawidek sc->sc_queue_size = ggio->gctl_maxcount; 488fe27e772SPawel Jakub Dawidek if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE) 489fe27e772SPawel Jakub Dawidek sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE; 490fe27e772SPawel Jakub Dawidek sc->sc_timeout = ggio->gctl_timeout; 491fe27e772SPawel Jakub Dawidek callout_init(&sc->sc_callout, CALLOUT_MPSAFE); 492e08ec037SPawel Jakub Dawidek 49332115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 49432115b10SPawel Jakub Dawidek sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error); 495a277f47bSMikolaj Golub if (sc->sc_unit < 0) 496a277f47bSMikolaj Golub goto fail1; 49732115b10SPawel Jakub Dawidek if (ggio->gctl_unit == G_GATE_NAME_GIVEN) 49832115b10SPawel Jakub Dawidek snprintf(name, sizeof(name), "%s", ggio->gctl_name); 49932115b10SPawel Jakub Dawidek else { 50032115b10SPawel Jakub Dawidek snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME, 50132115b10SPawel Jakub Dawidek sc->sc_unit); 50232115b10SPawel Jakub Dawidek } 50332115b10SPawel Jakub Dawidek /* Check for name collision. */ 50432115b10SPawel Jakub Dawidek for (unit = 0; unit < g_gate_maxunits; unit++) { 50532115b10SPawel Jakub Dawidek if (g_gate_units[unit] == NULL) 50632115b10SPawel Jakub Dawidek continue; 507baf63f65SMikolaj Golub if (strcmp(name, g_gate_units[unit]->sc_name) != 0) 50832115b10SPawel Jakub Dawidek continue; 509a277f47bSMikolaj Golub error = EEXIST; 510a277f47bSMikolaj Golub goto fail1; 51132115b10SPawel Jakub Dawidek } 512baf63f65SMikolaj Golub sc->sc_name = name; 51332115b10SPawel Jakub Dawidek g_gate_units[sc->sc_unit] = sc; 51432115b10SPawel Jakub Dawidek g_gate_nunits++; 51532115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 51632115b10SPawel Jakub Dawidek 517a277f47bSMikolaj Golub g_topology_lock(); 518a277f47bSMikolaj Golub 519a277f47bSMikolaj Golub if (ggio->gctl_readprov[0] == '\0') { 520a277f47bSMikolaj Golub ropp = NULL; 521a277f47bSMikolaj Golub } else { 522a277f47bSMikolaj Golub ropp = g_provider_by_name(ggio->gctl_readprov); 523a277f47bSMikolaj Golub if (ropp == NULL) { 524a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Provider %s doesn't exist.", 525a277f47bSMikolaj Golub ggio->gctl_readprov); 526a277f47bSMikolaj Golub error = EINVAL; 527a277f47bSMikolaj Golub goto fail2; 528a277f47bSMikolaj Golub } 529a277f47bSMikolaj Golub if ((ggio->gctl_readoffset % ggio->gctl_sectorsize) != 0) { 530a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Invalid read offset."); 531a277f47bSMikolaj Golub error = EINVAL; 532a277f47bSMikolaj Golub goto fail2; 533a277f47bSMikolaj Golub } 534a277f47bSMikolaj Golub if (ggio->gctl_mediasize + ggio->gctl_readoffset > 535a277f47bSMikolaj Golub ropp->mediasize) { 536a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Invalid read offset or media size."); 537a277f47bSMikolaj Golub error = EINVAL; 538a277f47bSMikolaj Golub goto fail2; 539a277f47bSMikolaj Golub } 540a277f47bSMikolaj Golub } 541a277f47bSMikolaj Golub 542a277f47bSMikolaj Golub gp = g_new_geomf(&g_gate_class, "%s", name); 543a277f47bSMikolaj Golub gp->start = g_gate_start; 544a277f47bSMikolaj Golub gp->access = g_gate_access; 545a277f47bSMikolaj Golub gp->orphan = g_gate_orphan; 546a277f47bSMikolaj Golub gp->dumpconf = g_gate_dumpconf; 547a277f47bSMikolaj Golub gp->softc = sc; 548a277f47bSMikolaj Golub 549a277f47bSMikolaj Golub if (ropp != NULL) { 550a277f47bSMikolaj Golub cp = g_new_consumer(gp); 551*40ea77a0SAlexander Motin cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 552a277f47bSMikolaj Golub error = g_attach(cp, ropp); 553a277f47bSMikolaj Golub if (error != 0) { 554a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Unable to attach to %s.", ropp->name); 555a277f47bSMikolaj Golub goto fail3; 556a277f47bSMikolaj Golub } 557a277f47bSMikolaj Golub error = g_access(cp, 1, 0, 0); 558a277f47bSMikolaj Golub if (error != 0) { 559a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Unable to access %s.", ropp->name); 560a277f47bSMikolaj Golub g_detach(cp); 561a277f47bSMikolaj Golub goto fail3; 562a277f47bSMikolaj Golub } 563a277f47bSMikolaj Golub sc->sc_readcons = cp; 564a277f47bSMikolaj Golub sc->sc_readoffset = ggio->gctl_readoffset; 565a277f47bSMikolaj Golub } 566a277f47bSMikolaj Golub 56732115b10SPawel Jakub Dawidek ggio->gctl_unit = sc->sc_unit; 568fe27e772SPawel Jakub Dawidek 56932115b10SPawel Jakub Dawidek pp = g_new_providerf(gp, "%s", name); 570*40ea77a0SAlexander Motin pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 571fe27e772SPawel Jakub Dawidek pp->mediasize = ggio->gctl_mediasize; 572fe27e772SPawel Jakub Dawidek pp->sectorsize = ggio->gctl_sectorsize; 573fe27e772SPawel Jakub Dawidek sc->sc_provider = pp; 574fe27e772SPawel Jakub Dawidek g_error_provider(pp, 0); 575e08ec037SPawel Jakub Dawidek 576fe27e772SPawel Jakub Dawidek g_topology_unlock(); 577baf63f65SMikolaj Golub mtx_lock(&g_gate_units_lock); 578baf63f65SMikolaj Golub sc->sc_name = sc->sc_provider->name; 579baf63f65SMikolaj Golub mtx_unlock(&g_gate_units_lock); 580bd119384SMikolaj Golub G_GATE_DEBUG(1, "Device %s created.", gp->name); 581fe27e772SPawel Jakub Dawidek 582fe27e772SPawel Jakub Dawidek if (sc->sc_timeout > 0) { 583fe27e772SPawel Jakub Dawidek callout_reset(&sc->sc_callout, sc->sc_timeout * hz, 584fe27e772SPawel Jakub Dawidek g_gate_guard, sc); 585fe27e772SPawel Jakub Dawidek } 586fe27e772SPawel Jakub Dawidek return (0); 587a277f47bSMikolaj Golub fail3: 588a277f47bSMikolaj Golub g_destroy_consumer(cp); 589a277f47bSMikolaj Golub g_destroy_geom(gp); 590a277f47bSMikolaj Golub fail2: 591a277f47bSMikolaj Golub g_topology_unlock(); 592a277f47bSMikolaj Golub mtx_lock(&g_gate_units_lock); 593a277f47bSMikolaj Golub g_gate_units[sc->sc_unit] = NULL; 594a277f47bSMikolaj Golub KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?")); 595a277f47bSMikolaj Golub g_gate_nunits--; 596a277f47bSMikolaj Golub fail1: 597a277f47bSMikolaj Golub mtx_unlock(&g_gate_units_lock); 598a277f47bSMikolaj Golub mtx_destroy(&sc->sc_queue_mtx); 599a277f47bSMikolaj Golub free(sc, M_GATE); 600a277f47bSMikolaj Golub return (error); 601fe27e772SPawel Jakub Dawidek } 602fe27e772SPawel Jakub Dawidek 603e08ec037SPawel Jakub Dawidek static int 604e08ec037SPawel Jakub Dawidek g_gate_modify(struct g_gate_softc *sc, struct g_gate_ctl_modify *ggio) 605e08ec037SPawel Jakub Dawidek { 606e08ec037SPawel Jakub Dawidek struct g_provider *pp; 607e08ec037SPawel Jakub Dawidek struct g_consumer *cp; 608e08ec037SPawel Jakub Dawidek int error; 609e08ec037SPawel Jakub Dawidek 610e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_MEDIASIZE) != 0) { 611e08ec037SPawel Jakub Dawidek if (ggio->gctl_mediasize <= 0) { 612e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid media size."); 613e08ec037SPawel Jakub Dawidek return (EINVAL); 614e08ec037SPawel Jakub Dawidek } 615e08ec037SPawel Jakub Dawidek pp = sc->sc_provider; 616e08ec037SPawel Jakub Dawidek if ((ggio->gctl_mediasize % pp->sectorsize) != 0) { 617e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid media size."); 618e08ec037SPawel Jakub Dawidek return (EINVAL); 619e08ec037SPawel Jakub Dawidek } 620e08ec037SPawel Jakub Dawidek /* TODO */ 621e08ec037SPawel Jakub Dawidek return (EOPNOTSUPP); 622e08ec037SPawel Jakub Dawidek } 623e08ec037SPawel Jakub Dawidek 624e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_INFO) != 0) 625e08ec037SPawel Jakub Dawidek (void)strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info)); 626e08ec037SPawel Jakub Dawidek 627e08ec037SPawel Jakub Dawidek cp = NULL; 628e08ec037SPawel Jakub Dawidek 629e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) { 630e08ec037SPawel Jakub Dawidek g_topology_lock(); 631e08ec037SPawel Jakub Dawidek if (sc->sc_readcons != NULL) { 632e08ec037SPawel Jakub Dawidek cp = sc->sc_readcons; 633e08ec037SPawel Jakub Dawidek sc->sc_readcons = NULL; 634e08ec037SPawel Jakub Dawidek (void)g_access(cp, -1, 0, 0); 635e08ec037SPawel Jakub Dawidek g_detach(cp); 636e08ec037SPawel Jakub Dawidek g_destroy_consumer(cp); 637e08ec037SPawel Jakub Dawidek } 638e08ec037SPawel Jakub Dawidek if (ggio->gctl_readprov[0] != '\0') { 639e08ec037SPawel Jakub Dawidek pp = g_provider_by_name(ggio->gctl_readprov); 640e08ec037SPawel Jakub Dawidek if (pp == NULL) { 641e08ec037SPawel Jakub Dawidek g_topology_unlock(); 642e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Provider %s doesn't exist.", 643e08ec037SPawel Jakub Dawidek ggio->gctl_readprov); 644e08ec037SPawel Jakub Dawidek return (EINVAL); 645e08ec037SPawel Jakub Dawidek } 646e08ec037SPawel Jakub Dawidek cp = g_new_consumer(sc->sc_provider->geom); 647*40ea77a0SAlexander Motin cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 648e08ec037SPawel Jakub Dawidek error = g_attach(cp, pp); 649e08ec037SPawel Jakub Dawidek if (error != 0) { 650e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Unable to attach to %s.", 651e08ec037SPawel Jakub Dawidek pp->name); 652e08ec037SPawel Jakub Dawidek } else { 653e08ec037SPawel Jakub Dawidek error = g_access(cp, 1, 0, 0); 654e08ec037SPawel Jakub Dawidek if (error != 0) { 655e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Unable to access %s.", 656e08ec037SPawel Jakub Dawidek pp->name); 657e08ec037SPawel Jakub Dawidek g_detach(cp); 658e08ec037SPawel Jakub Dawidek } 659e08ec037SPawel Jakub Dawidek } 660e08ec037SPawel Jakub Dawidek if (error != 0) { 661e08ec037SPawel Jakub Dawidek g_destroy_consumer(cp); 662e08ec037SPawel Jakub Dawidek g_topology_unlock(); 663e08ec037SPawel Jakub Dawidek return (error); 664e08ec037SPawel Jakub Dawidek } 665e08ec037SPawel Jakub Dawidek } 666e08ec037SPawel Jakub Dawidek } else { 667e08ec037SPawel Jakub Dawidek cp = sc->sc_readcons; 668e08ec037SPawel Jakub Dawidek } 669e08ec037SPawel Jakub Dawidek 670e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_READOFFSET) != 0) { 671e08ec037SPawel Jakub Dawidek if (cp == NULL) { 672e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "No read provider."); 673e08ec037SPawel Jakub Dawidek return (EINVAL); 674e08ec037SPawel Jakub Dawidek } 675e08ec037SPawel Jakub Dawidek pp = sc->sc_provider; 676e08ec037SPawel Jakub Dawidek if ((ggio->gctl_readoffset % pp->sectorsize) != 0) { 677e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid read offset."); 678e08ec037SPawel Jakub Dawidek return (EINVAL); 679e08ec037SPawel Jakub Dawidek } 680e08ec037SPawel Jakub Dawidek if (pp->mediasize + ggio->gctl_readoffset > 681e08ec037SPawel Jakub Dawidek cp->provider->mediasize) { 682e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid read offset or media size."); 683e08ec037SPawel Jakub Dawidek return (EINVAL); 684e08ec037SPawel Jakub Dawidek } 685e08ec037SPawel Jakub Dawidek sc->sc_readoffset = ggio->gctl_readoffset; 686e08ec037SPawel Jakub Dawidek } 687e08ec037SPawel Jakub Dawidek 688e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) { 689e08ec037SPawel Jakub Dawidek sc->sc_readcons = cp; 690e08ec037SPawel Jakub Dawidek g_topology_unlock(); 691e08ec037SPawel Jakub Dawidek } 692e08ec037SPawel Jakub Dawidek 693e08ec037SPawel Jakub Dawidek return (0); 694e08ec037SPawel Jakub Dawidek } 695e08ec037SPawel Jakub Dawidek 696fe27e772SPawel Jakub Dawidek #define G_GATE_CHECK_VERSION(ggio) do { \ 69784436f14SPawel Jakub Dawidek if ((ggio)->gctl_version != G_GATE_VERSION) { \ 69884436f14SPawel Jakub Dawidek printf("Version mismatch %d != %d.\n", \ 69984436f14SPawel Jakub Dawidek ggio->gctl_version, G_GATE_VERSION); \ 700fe27e772SPawel Jakub Dawidek return (EINVAL); \ 70184436f14SPawel Jakub Dawidek } \ 702fe27e772SPawel Jakub Dawidek } while (0) 703fe27e772SPawel Jakub Dawidek static int 70489c9c53dSPoul-Henning Kamp g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 705fe27e772SPawel Jakub Dawidek { 706fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 707fe27e772SPawel Jakub Dawidek struct bio *bp; 708fe27e772SPawel Jakub Dawidek int error = 0; 709fe27e772SPawel Jakub Dawidek 710fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr, 711fe27e772SPawel Jakub Dawidek flags, td); 712fe27e772SPawel Jakub Dawidek 713fe27e772SPawel Jakub Dawidek switch (cmd) { 714fe27e772SPawel Jakub Dawidek case G_GATE_CMD_CREATE: 715fe27e772SPawel Jakub Dawidek { 716fe27e772SPawel Jakub Dawidek struct g_gate_ctl_create *ggio = (void *)addr; 717fe27e772SPawel Jakub Dawidek 718fe27e772SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 719a17dd95fSPawel Jakub Dawidek error = g_gate_create(ggio); 720f9065812SPawel Jakub Dawidek /* 721f9065812SPawel Jakub Dawidek * Reset TDP_GEOM flag. 722f9065812SPawel Jakub Dawidek * There are pending events for sure, because we just created 723f9065812SPawel Jakub Dawidek * new provider and other classes want to taste it, but we 724f9065812SPawel Jakub Dawidek * cannot answer on I/O requests until we're here. 725f9065812SPawel Jakub Dawidek */ 726f9065812SPawel Jakub Dawidek td->td_pflags &= ~TDP_GEOM; 727a17dd95fSPawel Jakub Dawidek return (error); 728fe27e772SPawel Jakub Dawidek } 729e08ec037SPawel Jakub Dawidek case G_GATE_CMD_MODIFY: 730e08ec037SPawel Jakub Dawidek { 731e08ec037SPawel Jakub Dawidek struct g_gate_ctl_modify *ggio = (void *)addr; 732e08ec037SPawel Jakub Dawidek 733e08ec037SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 734e08ec037SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, NULL); 735e08ec037SPawel Jakub Dawidek if (sc == NULL) 736e08ec037SPawel Jakub Dawidek return (ENXIO); 737e08ec037SPawel Jakub Dawidek error = g_gate_modify(sc, ggio); 738e08ec037SPawel Jakub Dawidek g_gate_release(sc); 739e08ec037SPawel Jakub Dawidek return (error); 740e08ec037SPawel Jakub Dawidek } 741fe27e772SPawel Jakub Dawidek case G_GATE_CMD_DESTROY: 742fe27e772SPawel Jakub Dawidek { 743fe27e772SPawel Jakub Dawidek struct g_gate_ctl_destroy *ggio = (void *)addr; 744fe27e772SPawel Jakub Dawidek 745fe27e772SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 74632115b10SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name); 747fe27e772SPawel Jakub Dawidek if (sc == NULL) 748fe27e772SPawel Jakub Dawidek return (ENXIO); 749fe27e772SPawel Jakub Dawidek g_topology_lock(); 75032115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 751fe27e772SPawel Jakub Dawidek error = g_gate_destroy(sc, ggio->gctl_force); 752fe27e772SPawel Jakub Dawidek g_topology_unlock(); 7537ffb6e0fSPawel Jakub Dawidek if (error != 0) 754fe27e772SPawel Jakub Dawidek g_gate_release(sc); 755fe27e772SPawel Jakub Dawidek return (error); 756fe27e772SPawel Jakub Dawidek } 75784436f14SPawel Jakub Dawidek case G_GATE_CMD_CANCEL: 75884436f14SPawel Jakub Dawidek { 75984436f14SPawel Jakub Dawidek struct g_gate_ctl_cancel *ggio = (void *)addr; 76084436f14SPawel Jakub Dawidek struct bio *tbp, *lbp; 76184436f14SPawel Jakub Dawidek 76284436f14SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 76332115b10SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name); 76484436f14SPawel Jakub Dawidek if (sc == NULL) 76584436f14SPawel Jakub Dawidek return (ENXIO); 76684436f14SPawel Jakub Dawidek lbp = NULL; 76784436f14SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 76884436f14SPawel Jakub Dawidek TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) { 76984436f14SPawel Jakub Dawidek if (ggio->gctl_seq == 0 || 77084436f14SPawel Jakub Dawidek ggio->gctl_seq == (uintptr_t)bp->bio_driver1) { 77184436f14SPawel Jakub Dawidek G_GATE_LOGREQ(1, bp, "Request canceled."); 77284436f14SPawel Jakub Dawidek bioq_remove(&sc->sc_outqueue, bp); 77384436f14SPawel Jakub Dawidek /* 77484436f14SPawel Jakub Dawidek * Be sure to put requests back onto incoming 77584436f14SPawel Jakub Dawidek * queue in the proper order. 77684436f14SPawel Jakub Dawidek */ 77784436f14SPawel Jakub Dawidek if (lbp == NULL) 77884436f14SPawel Jakub Dawidek bioq_insert_head(&sc->sc_inqueue, bp); 77984436f14SPawel Jakub Dawidek else { 78084436f14SPawel Jakub Dawidek TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue, 78184436f14SPawel Jakub Dawidek lbp, bp, bio_queue); 78284436f14SPawel Jakub Dawidek } 78384436f14SPawel Jakub Dawidek lbp = bp; 78484436f14SPawel Jakub Dawidek /* 78584436f14SPawel Jakub Dawidek * If only one request was canceled, leave now. 78684436f14SPawel Jakub Dawidek */ 78784436f14SPawel Jakub Dawidek if (ggio->gctl_seq != 0) 78884436f14SPawel Jakub Dawidek break; 78984436f14SPawel Jakub Dawidek } 79084436f14SPawel Jakub Dawidek } 79132115b10SPawel Jakub Dawidek if (ggio->gctl_unit == G_GATE_NAME_GIVEN) 79232115b10SPawel Jakub Dawidek ggio->gctl_unit = sc->sc_unit; 79384436f14SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 79484436f14SPawel Jakub Dawidek g_gate_release(sc); 79584436f14SPawel Jakub Dawidek return (error); 79684436f14SPawel Jakub Dawidek } 797fe27e772SPawel Jakub Dawidek case G_GATE_CMD_START: 798fe27e772SPawel Jakub Dawidek { 799fe27e772SPawel Jakub Dawidek struct g_gate_ctl_io *ggio = (void *)addr; 800fe27e772SPawel Jakub Dawidek 801fe27e772SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 80232115b10SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, NULL); 803fe27e772SPawel Jakub Dawidek if (sc == NULL) 804fe27e772SPawel Jakub Dawidek return (ENXIO); 8057ffb6e0fSPawel Jakub Dawidek error = 0; 806fe27e772SPawel Jakub Dawidek for (;;) { 807e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 808fe27e772SPawel Jakub Dawidek bp = bioq_first(&sc->sc_inqueue); 809fe27e772SPawel Jakub Dawidek if (bp != NULL) 810fe27e772SPawel Jakub Dawidek break; 8117ffb6e0fSPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 8127ffb6e0fSPawel Jakub Dawidek ggio->gctl_error = ECANCELED; 8137ffb6e0fSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 8147ffb6e0fSPawel Jakub Dawidek goto start_end; 8157ffb6e0fSPawel Jakub Dawidek } 816e35d3a78SPawel Jakub Dawidek if (msleep(sc, &sc->sc_queue_mtx, 817fe27e772SPawel Jakub Dawidek PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) { 818fe27e772SPawel Jakub Dawidek ggio->gctl_error = ECANCELED; 8197ffb6e0fSPawel Jakub Dawidek goto start_end; 820fe27e772SPawel Jakub Dawidek } 821fe27e772SPawel Jakub Dawidek } 8224d1e1bf3SPawel Jakub Dawidek ggio->gctl_cmd = bp->bio_cmd; 823c4d2d401SPawel Jakub Dawidek if (bp->bio_cmd == BIO_WRITE && 824fe27e772SPawel Jakub Dawidek bp->bio_length > ggio->gctl_length) { 825e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 826fe27e772SPawel Jakub Dawidek ggio->gctl_length = bp->bio_length; 827fe27e772SPawel Jakub Dawidek ggio->gctl_error = ENOMEM; 8287ffb6e0fSPawel Jakub Dawidek goto start_end; 829fe27e772SPawel Jakub Dawidek } 830fe27e772SPawel Jakub Dawidek bioq_remove(&sc->sc_inqueue, bp); 831e35d3a78SPawel Jakub Dawidek bioq_insert_tail(&sc->sc_outqueue, bp); 832e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 833e35d3a78SPawel Jakub Dawidek 8340d785336SPawel Jakub Dawidek ggio->gctl_seq = (uintptr_t)bp->bio_driver1; 835fe27e772SPawel Jakub Dawidek ggio->gctl_offset = bp->bio_offset; 836fe27e772SPawel Jakub Dawidek ggio->gctl_length = bp->bio_length; 83784436f14SPawel Jakub Dawidek 838fe27e772SPawel Jakub Dawidek switch (bp->bio_cmd) { 839fe27e772SPawel Jakub Dawidek case BIO_READ: 840fe27e772SPawel Jakub Dawidek case BIO_DELETE: 841204a4e19SPawel Jakub Dawidek case BIO_FLUSH: 84215725379SPawel Jakub Dawidek break; 843fe27e772SPawel Jakub Dawidek case BIO_WRITE: 844fe27e772SPawel Jakub Dawidek error = copyout(bp->bio_data, ggio->gctl_data, 845fe27e772SPawel Jakub Dawidek bp->bio_length); 846fe27e772SPawel Jakub Dawidek if (error != 0) { 847e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 848e35d3a78SPawel Jakub Dawidek bioq_remove(&sc->sc_outqueue, bp); 849662a4e58SPawel Jakub Dawidek bioq_insert_head(&sc->sc_inqueue, bp); 850e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 8517ffb6e0fSPawel Jakub Dawidek goto start_end; 852fe27e772SPawel Jakub Dawidek } 853fe27e772SPawel Jakub Dawidek break; 854fe27e772SPawel Jakub Dawidek } 8557ffb6e0fSPawel Jakub Dawidek start_end: 8567ffb6e0fSPawel Jakub Dawidek g_gate_release(sc); 8577ffb6e0fSPawel Jakub Dawidek return (error); 858fe27e772SPawel Jakub Dawidek } 859fe27e772SPawel Jakub Dawidek case G_GATE_CMD_DONE: 860fe27e772SPawel Jakub Dawidek { 861fe27e772SPawel Jakub Dawidek struct g_gate_ctl_io *ggio = (void *)addr; 862fe27e772SPawel Jakub Dawidek 863fe27e772SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 86432115b10SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, NULL); 865fe27e772SPawel Jakub Dawidek if (sc == NULL) 866fe27e772SPawel Jakub Dawidek return (ENOENT); 8677ffb6e0fSPawel Jakub Dawidek error = 0; 868e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 869fe27e772SPawel Jakub Dawidek TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) { 8700d785336SPawel Jakub Dawidek if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1) 871fe27e772SPawel Jakub Dawidek break; 872fe27e772SPawel Jakub Dawidek } 873fe27e772SPawel Jakub Dawidek if (bp != NULL) { 874fe27e772SPawel Jakub Dawidek bioq_remove(&sc->sc_outqueue, bp); 875e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 876fe27e772SPawel Jakub Dawidek } 877e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 878fe27e772SPawel Jakub Dawidek if (bp == NULL) { 879fe27e772SPawel Jakub Dawidek /* 880fe27e772SPawel Jakub Dawidek * Request was probably canceled. 881fe27e772SPawel Jakub Dawidek */ 8827ffb6e0fSPawel Jakub Dawidek goto done_end; 883fe27e772SPawel Jakub Dawidek } 884fe27e772SPawel Jakub Dawidek if (ggio->gctl_error == EAGAIN) { 885fe27e772SPawel Jakub Dawidek bp->bio_error = 0; 886fe27e772SPawel Jakub Dawidek G_GATE_LOGREQ(1, bp, "Request desisted."); 887e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 888e35d3a78SPawel Jakub Dawidek sc->sc_queue_count++; 889662a4e58SPawel Jakub Dawidek bioq_insert_head(&sc->sc_inqueue, bp); 890fe27e772SPawel Jakub Dawidek wakeup(sc); 891e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 892fe27e772SPawel Jakub Dawidek } else { 893fe27e772SPawel Jakub Dawidek bp->bio_error = ggio->gctl_error; 894fe27e772SPawel Jakub Dawidek if (bp->bio_error == 0) { 895fe27e772SPawel Jakub Dawidek bp->bio_completed = bp->bio_length; 896fe27e772SPawel Jakub Dawidek switch (bp->bio_cmd) { 897fe27e772SPawel Jakub Dawidek case BIO_READ: 898fe27e772SPawel Jakub Dawidek error = copyin(ggio->gctl_data, 899fe27e772SPawel Jakub Dawidek bp->bio_data, bp->bio_length); 900fe27e772SPawel Jakub Dawidek if (error != 0) 901fe27e772SPawel Jakub Dawidek bp->bio_error = error; 902fe27e772SPawel Jakub Dawidek break; 903fe27e772SPawel Jakub Dawidek case BIO_DELETE: 904fe27e772SPawel Jakub Dawidek case BIO_WRITE: 905204a4e19SPawel Jakub Dawidek case BIO_FLUSH: 906fe27e772SPawel Jakub Dawidek break; 907fe27e772SPawel Jakub Dawidek } 908fe27e772SPawel Jakub Dawidek } 909fe27e772SPawel Jakub Dawidek G_GATE_LOGREQ(2, bp, "Request done."); 910fe27e772SPawel Jakub Dawidek g_io_deliver(bp, bp->bio_error); 911fe27e772SPawel Jakub Dawidek } 9127ffb6e0fSPawel Jakub Dawidek done_end: 9137ffb6e0fSPawel Jakub Dawidek g_gate_release(sc); 914fe27e772SPawel Jakub Dawidek return (error); 915fe27e772SPawel Jakub Dawidek } 916fe27e772SPawel Jakub Dawidek } 917fe27e772SPawel Jakub Dawidek return (ENOIOCTL); 918fe27e772SPawel Jakub Dawidek } 919fe27e772SPawel Jakub Dawidek 920fe27e772SPawel Jakub Dawidek static void 9219ecdd506SPawel Jakub Dawidek g_gate_device(void) 922fe27e772SPawel Jakub Dawidek { 923fe27e772SPawel Jakub Dawidek 924fe27e772SPawel Jakub Dawidek status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600, 925fe27e772SPawel Jakub Dawidek G_GATE_CTL_NAME); 926fe27e772SPawel Jakub Dawidek } 927fe27e772SPawel Jakub Dawidek 928fe27e772SPawel Jakub Dawidek static int 929fe27e772SPawel Jakub Dawidek g_gate_modevent(module_t mod, int type, void *data) 930fe27e772SPawel Jakub Dawidek { 931fe27e772SPawel Jakub Dawidek int error = 0; 932fe27e772SPawel Jakub Dawidek 933fe27e772SPawel Jakub Dawidek switch (type) { 934fe27e772SPawel Jakub Dawidek case MOD_LOAD: 93532115b10SPawel Jakub Dawidek mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF); 93632115b10SPawel Jakub Dawidek g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]), 93732115b10SPawel Jakub Dawidek M_GATE, M_WAITOK | M_ZERO); 93832115b10SPawel Jakub Dawidek g_gate_nunits = 0; 9399ecdd506SPawel Jakub Dawidek g_gate_device(); 940fe27e772SPawel Jakub Dawidek break; 941fe27e772SPawel Jakub Dawidek case MOD_UNLOAD: 94232115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 94332115b10SPawel Jakub Dawidek if (g_gate_nunits > 0) { 94432115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 945fe27e772SPawel Jakub Dawidek error = EBUSY; 946fe27e772SPawel Jakub Dawidek break; 947fe27e772SPawel Jakub Dawidek } 94832115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 94932115b10SPawel Jakub Dawidek mtx_destroy(&g_gate_units_lock); 950fe27e772SPawel Jakub Dawidek if (status_dev != 0) 951fe27e772SPawel Jakub Dawidek destroy_dev(status_dev); 95232115b10SPawel Jakub Dawidek free(g_gate_units, M_GATE); 953fe27e772SPawel Jakub Dawidek break; 954fe27e772SPawel Jakub Dawidek default: 9553e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 956fe27e772SPawel Jakub Dawidek break; 957fe27e772SPawel Jakub Dawidek } 958fe27e772SPawel Jakub Dawidek 959fe27e772SPawel Jakub Dawidek return (error); 960fe27e772SPawel Jakub Dawidek } 961fe27e772SPawel Jakub Dawidek static moduledata_t g_gate_module = { 962fe27e772SPawel Jakub Dawidek G_GATE_MOD_NAME, 963fe27e772SPawel Jakub Dawidek g_gate_modevent, 964fe27e772SPawel Jakub Dawidek NULL 965fe27e772SPawel Jakub Dawidek }; 966fe27e772SPawel Jakub Dawidek DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 967fe27e772SPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_gate_class, g_gate); 968