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; 65*af3b2549SHans Petter Selasky SYSCTL_INT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RWTUN, &g_gate_debug, 0, 66fe27e772SPawel Jakub Dawidek "Debug level"); 6732115b10SPawel Jakub Dawidek static u_int g_gate_maxunits = 256; 6832115b10SPawel Jakub Dawidek SYSCTL_UINT(_kern_geom_gate, OID_AUTO, maxunits, CTLFLAG_RDTUN, 6932115b10SPawel Jakub Dawidek &g_gate_maxunits, 0, "Maximum number of ggate devices"); 70fe27e772SPawel Jakub Dawidek 71fe27e772SPawel Jakub Dawidek struct g_class g_gate_class = { 72fe27e772SPawel Jakub Dawidek .name = G_GATE_CLASS_NAME, 735721c9c7SPoul-Henning Kamp .version = G_VERSION, 74fe27e772SPawel Jakub Dawidek }; 75fe27e772SPawel Jakub Dawidek 7689c9c53dSPoul-Henning Kamp static struct cdev *status_dev; 77fe27e772SPawel Jakub Dawidek static d_ioctl_t g_gate_ioctl; 78fe27e772SPawel Jakub Dawidek static struct cdevsw g_gate_cdevsw = { 79fe27e772SPawel Jakub Dawidek .d_version = D_VERSION, 80fe27e772SPawel Jakub Dawidek .d_ioctl = g_gate_ioctl, 81fe27e772SPawel Jakub Dawidek .d_name = G_GATE_CTL_NAME 82fe27e772SPawel Jakub Dawidek }; 83fe27e772SPawel Jakub Dawidek 84fe27e772SPawel Jakub Dawidek 8532115b10SPawel Jakub Dawidek static struct g_gate_softc **g_gate_units; 8632115b10SPawel Jakub Dawidek static u_int g_gate_nunits; 8732115b10SPawel Jakub Dawidek static struct mtx g_gate_units_lock; 88fe27e772SPawel Jakub Dawidek 89fe27e772SPawel Jakub Dawidek static int 90fe27e772SPawel Jakub Dawidek g_gate_destroy(struct g_gate_softc *sc, boolean_t force) 91fe27e772SPawel Jakub Dawidek { 9240ea77a0SAlexander Motin struct bio_queue_head queue; 93fe27e772SPawel Jakub Dawidek struct g_provider *pp; 94e08ec037SPawel Jakub Dawidek struct g_consumer *cp; 957ffb6e0fSPawel Jakub Dawidek struct g_geom *gp; 96fe27e772SPawel Jakub Dawidek struct bio *bp; 97fe27e772SPawel Jakub Dawidek 98fe27e772SPawel Jakub Dawidek g_topology_assert(); 9932115b10SPawel Jakub Dawidek mtx_assert(&g_gate_units_lock, MA_OWNED); 100fe27e772SPawel Jakub Dawidek pp = sc->sc_provider; 101fe27e772SPawel Jakub Dawidek if (!force && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 10232115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 103fe27e772SPawel Jakub Dawidek return (EBUSY); 104fe27e772SPawel Jakub Dawidek } 10532115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 106e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 1077ffb6e0fSPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) 1087ffb6e0fSPawel Jakub Dawidek sc->sc_flags |= G_GATE_FLAG_DESTROY; 109fe27e772SPawel Jakub Dawidek wakeup(sc); 110e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 1117ffb6e0fSPawel Jakub Dawidek gp = pp->geom; 1127ffb6e0fSPawel Jakub Dawidek pp->flags |= G_PF_WITHER; 1137ffb6e0fSPawel Jakub Dawidek g_orphan_provider(pp, ENXIO); 114fe27e772SPawel Jakub Dawidek callout_drain(&sc->sc_callout); 11540ea77a0SAlexander Motin bioq_init(&queue); 116e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 11740ea77a0SAlexander Motin while ((bp = bioq_takefirst(&sc->sc_inqueue)) != NULL) { 118e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 11940ea77a0SAlexander Motin bioq_insert_tail(&queue, bp); 120fe27e772SPawel Jakub Dawidek } 12140ea77a0SAlexander Motin while ((bp = bioq_takefirst(&sc->sc_outqueue)) != NULL) { 122e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 12340ea77a0SAlexander Motin bioq_insert_tail(&queue, bp); 124fe27e772SPawel Jakub Dawidek } 1257ffb6e0fSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 1267ffb6e0fSPawel Jakub Dawidek g_topology_unlock(); 12740ea77a0SAlexander Motin while ((bp = bioq_takefirst(&queue)) != NULL) { 12840ea77a0SAlexander Motin G_GATE_LOGREQ(1, bp, "Request canceled."); 12940ea77a0SAlexander Motin g_io_deliver(bp, ENXIO); 13040ea77a0SAlexander Motin } 13132115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 1327ffb6e0fSPawel Jakub Dawidek /* One reference is ours. */ 1337ffb6e0fSPawel Jakub Dawidek sc->sc_ref--; 13432115b10SPawel Jakub Dawidek while (sc->sc_ref > 0) 13532115b10SPawel Jakub Dawidek msleep(&sc->sc_ref, &g_gate_units_lock, 0, "gg:destroy", 0); 13632115b10SPawel Jakub Dawidek g_gate_units[sc->sc_unit] = NULL; 13732115b10SPawel Jakub Dawidek KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?")); 13832115b10SPawel Jakub Dawidek g_gate_nunits--; 13932115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 140e35d3a78SPawel Jakub Dawidek mtx_destroy(&sc->sc_queue_mtx); 1417ffb6e0fSPawel Jakub Dawidek g_topology_lock(); 142e08ec037SPawel Jakub Dawidek if ((cp = sc->sc_readcons) != NULL) { 143e08ec037SPawel Jakub Dawidek sc->sc_readcons = NULL; 144e08ec037SPawel Jakub Dawidek (void)g_access(cp, -1, 0, 0); 145e08ec037SPawel Jakub Dawidek g_detach(cp); 146e08ec037SPawel Jakub Dawidek g_destroy_consumer(cp); 147e08ec037SPawel Jakub Dawidek } 148bd119384SMikolaj Golub G_GATE_DEBUG(1, "Device %s destroyed.", gp->name); 1497ffb6e0fSPawel Jakub Dawidek gp->softc = NULL; 1507ffb6e0fSPawel Jakub Dawidek g_wither_geom(gp, ENXIO); 151fe27e772SPawel Jakub Dawidek sc->sc_provider = NULL; 152fe27e772SPawel Jakub Dawidek free(sc, M_GATE); 153fe27e772SPawel Jakub Dawidek return (0); 154fe27e772SPawel Jakub Dawidek } 155fe27e772SPawel Jakub Dawidek 156fe27e772SPawel Jakub Dawidek static int 157fe27e772SPawel Jakub Dawidek g_gate_access(struct g_provider *pp, int dr, int dw, int de) 158fe27e772SPawel Jakub Dawidek { 159fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 160fe27e772SPawel Jakub Dawidek 161fe27e772SPawel Jakub Dawidek if (dr <= 0 && dw <= 0 && de <= 0) 162fe27e772SPawel Jakub Dawidek return (0); 163fe27e772SPawel Jakub Dawidek sc = pp->geom->softc; 164fe27e772SPawel Jakub Dawidek if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) 165fe27e772SPawel Jakub Dawidek return (ENXIO); 16655336b83SPawel Jakub Dawidek /* XXX: Hack to allow read-only mounts. */ 16755336b83SPawel Jakub Dawidek #if 0 168fe27e772SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0) 169fe27e772SPawel Jakub Dawidek return (EPERM); 17055336b83SPawel Jakub Dawidek #endif 171fe27e772SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0) 172fe27e772SPawel Jakub Dawidek return (EPERM); 173fe27e772SPawel Jakub Dawidek return (0); 174fe27e772SPawel Jakub Dawidek } 175fe27e772SPawel Jakub Dawidek 176fe27e772SPawel Jakub Dawidek static void 177e08ec037SPawel Jakub Dawidek g_gate_queue_io(struct bio *bp) 178fe27e772SPawel Jakub Dawidek { 179fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 180fe27e772SPawel Jakub Dawidek 181fe27e772SPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 182fe27e772SPawel Jakub Dawidek if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 183fe27e772SPawel Jakub Dawidek g_io_deliver(bp, ENXIO); 184fe27e772SPawel Jakub Dawidek return; 185fe27e772SPawel Jakub Dawidek } 186fe27e772SPawel Jakub Dawidek 187e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 188e08ec037SPawel Jakub Dawidek 18963a6c5c1SPawel Jakub Dawidek if (sc->sc_queue_size > 0 && sc->sc_queue_count > sc->sc_queue_size) { 19035f855d9SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 191fe27e772SPawel Jakub Dawidek G_GATE_LOGREQ(1, bp, "Queue full, request canceled."); 19232115b10SPawel Jakub Dawidek g_io_deliver(bp, ENOMEM); 193fe27e772SPawel Jakub Dawidek return; 194fe27e772SPawel Jakub Dawidek } 195e35d3a78SPawel Jakub Dawidek 196fe27e772SPawel Jakub Dawidek bp->bio_driver1 = (void *)sc->sc_seq; 197fe27e772SPawel Jakub Dawidek sc->sc_seq++; 198e35d3a78SPawel Jakub Dawidek sc->sc_queue_count++; 199fe27e772SPawel Jakub Dawidek 200662a4e58SPawel Jakub Dawidek bioq_insert_tail(&sc->sc_inqueue, bp); 201fe27e772SPawel Jakub Dawidek wakeup(sc); 202e35d3a78SPawel Jakub Dawidek 203e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 204fe27e772SPawel Jakub Dawidek } 205fe27e772SPawel Jakub Dawidek 206e08ec037SPawel Jakub Dawidek static void 207e08ec037SPawel Jakub Dawidek g_gate_done(struct bio *cbp) 208e08ec037SPawel Jakub Dawidek { 209e08ec037SPawel Jakub Dawidek struct bio *pbp; 210e08ec037SPawel Jakub Dawidek 211e08ec037SPawel Jakub Dawidek pbp = cbp->bio_parent; 212e08ec037SPawel Jakub Dawidek if (cbp->bio_error == 0) { 213e08ec037SPawel Jakub Dawidek pbp->bio_completed = cbp->bio_completed; 214e08ec037SPawel Jakub Dawidek g_destroy_bio(cbp); 215e08ec037SPawel Jakub Dawidek pbp->bio_inbed++; 216e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, 0); 217e08ec037SPawel Jakub Dawidek } else { 218e08ec037SPawel Jakub Dawidek /* If direct read failed, pass it through userland daemon. */ 219e08ec037SPawel Jakub Dawidek g_destroy_bio(cbp); 220e08ec037SPawel Jakub Dawidek pbp->bio_children--; 221e08ec037SPawel Jakub Dawidek g_gate_queue_io(pbp); 222e08ec037SPawel Jakub Dawidek } 223e08ec037SPawel Jakub Dawidek } 224e08ec037SPawel Jakub Dawidek 225e08ec037SPawel Jakub Dawidek static void 226e08ec037SPawel Jakub Dawidek g_gate_start(struct bio *pbp) 227e08ec037SPawel Jakub Dawidek { 228e08ec037SPawel Jakub Dawidek struct g_gate_softc *sc; 229e08ec037SPawel Jakub Dawidek 230e08ec037SPawel Jakub Dawidek sc = pbp->bio_to->geom->softc; 231e08ec037SPawel Jakub Dawidek if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 232e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, ENXIO); 233e08ec037SPawel Jakub Dawidek return; 234e08ec037SPawel Jakub Dawidek } 235e08ec037SPawel Jakub Dawidek G_GATE_LOGREQ(2, pbp, "Request received."); 236e08ec037SPawel Jakub Dawidek switch (pbp->bio_cmd) { 237e08ec037SPawel Jakub Dawidek case BIO_READ: 238e08ec037SPawel Jakub Dawidek if (sc->sc_readcons != NULL) { 239e08ec037SPawel Jakub Dawidek struct bio *cbp; 240e08ec037SPawel Jakub Dawidek 241e08ec037SPawel Jakub Dawidek cbp = g_clone_bio(pbp); 242e08ec037SPawel Jakub Dawidek if (cbp == NULL) { 243e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, ENOMEM); 244e08ec037SPawel Jakub Dawidek return; 245e08ec037SPawel Jakub Dawidek } 246e08ec037SPawel Jakub Dawidek cbp->bio_done = g_gate_done; 247e08ec037SPawel Jakub Dawidek cbp->bio_offset = pbp->bio_offset + sc->sc_readoffset; 248e08ec037SPawel Jakub Dawidek cbp->bio_to = sc->sc_readcons->provider; 249e08ec037SPawel Jakub Dawidek g_io_request(cbp, sc->sc_readcons); 250e08ec037SPawel Jakub Dawidek return; 251e08ec037SPawel Jakub Dawidek } 252e08ec037SPawel Jakub Dawidek break; 253e08ec037SPawel Jakub Dawidek case BIO_DELETE: 254e08ec037SPawel Jakub Dawidek case BIO_WRITE: 255e08ec037SPawel Jakub Dawidek case BIO_FLUSH: 256e08ec037SPawel Jakub Dawidek /* XXX: Hack to allow read-only mounts. */ 257e08ec037SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) { 258e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, EPERM); 259e08ec037SPawel Jakub Dawidek return; 260e08ec037SPawel Jakub Dawidek } 261e08ec037SPawel Jakub Dawidek break; 262e08ec037SPawel Jakub Dawidek case BIO_GETATTR: 263e08ec037SPawel Jakub Dawidek default: 264e08ec037SPawel Jakub Dawidek G_GATE_LOGREQ(2, pbp, "Ignoring request."); 265e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, EOPNOTSUPP); 266e08ec037SPawel Jakub Dawidek return; 267e08ec037SPawel Jakub Dawidek } 268e08ec037SPawel Jakub Dawidek 269e08ec037SPawel Jakub Dawidek g_gate_queue_io(pbp); 270e08ec037SPawel Jakub Dawidek } 271e08ec037SPawel Jakub Dawidek 272fe27e772SPawel Jakub Dawidek static struct g_gate_softc * 2732aa15ffdSPawel Jakub Dawidek g_gate_hold(int unit, const char *name) 274fe27e772SPawel Jakub Dawidek { 27532115b10SPawel Jakub Dawidek struct g_gate_softc *sc = NULL; 276fe27e772SPawel Jakub Dawidek 27732115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 27832115b10SPawel Jakub Dawidek if (unit >= 0 && unit < g_gate_maxunits) 27932115b10SPawel Jakub Dawidek sc = g_gate_units[unit]; 28032115b10SPawel Jakub Dawidek else if (unit == G_GATE_NAME_GIVEN) { 28132115b10SPawel Jakub Dawidek KASSERT(name != NULL, ("name is NULL")); 28232115b10SPawel Jakub Dawidek for (unit = 0; unit < g_gate_maxunits; unit++) { 28332115b10SPawel Jakub Dawidek if (g_gate_units[unit] == NULL) 28432115b10SPawel Jakub Dawidek continue; 28532115b10SPawel Jakub Dawidek if (strcmp(name, 28632115b10SPawel Jakub Dawidek g_gate_units[unit]->sc_provider->name) != 0) { 28732115b10SPawel Jakub Dawidek continue; 28832115b10SPawel Jakub Dawidek } 28932115b10SPawel Jakub Dawidek sc = g_gate_units[unit]; 2907ffb6e0fSPawel Jakub Dawidek break; 291fe27e772SPawel Jakub Dawidek } 29232115b10SPawel Jakub Dawidek } 2937ffb6e0fSPawel Jakub Dawidek if (sc != NULL) 2947ffb6e0fSPawel Jakub Dawidek sc->sc_ref++; 29532115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 296fe27e772SPawel Jakub Dawidek return (sc); 297fe27e772SPawel Jakub Dawidek } 298fe27e772SPawel Jakub Dawidek 299fe27e772SPawel Jakub Dawidek static void 300fe27e772SPawel Jakub Dawidek g_gate_release(struct g_gate_softc *sc) 301fe27e772SPawel Jakub Dawidek { 302fe27e772SPawel Jakub Dawidek 303fe27e772SPawel Jakub Dawidek g_topology_assert_not(); 30432115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 305fe27e772SPawel Jakub Dawidek sc->sc_ref--; 306fe27e772SPawel Jakub Dawidek KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name)); 30732115b10SPawel Jakub Dawidek if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) 3087ffb6e0fSPawel Jakub Dawidek wakeup(&sc->sc_ref); 30932115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 310fe27e772SPawel Jakub Dawidek } 311fe27e772SPawel Jakub Dawidek 312fe27e772SPawel Jakub Dawidek static int 31332115b10SPawel Jakub Dawidek g_gate_getunit(int unit, int *errorp) 314fe27e772SPawel Jakub Dawidek { 315fe27e772SPawel Jakub Dawidek 31632115b10SPawel Jakub Dawidek mtx_assert(&g_gate_units_lock, MA_OWNED); 317fe27e772SPawel Jakub Dawidek if (unit >= 0) { 31832115b10SPawel Jakub Dawidek if (unit >= g_gate_maxunits) 31932115b10SPawel Jakub Dawidek *errorp = EINVAL; 32032115b10SPawel Jakub Dawidek else if (g_gate_units[unit] == NULL) 321fe27e772SPawel Jakub Dawidek return (unit); 32232115b10SPawel Jakub Dawidek else 32332115b10SPawel Jakub Dawidek *errorp = EEXIST; 32432115b10SPawel Jakub Dawidek } else { 32532115b10SPawel Jakub Dawidek for (unit = 0; unit < g_gate_maxunits; unit++) { 32632115b10SPawel Jakub Dawidek if (g_gate_units[unit] == NULL) 32732115b10SPawel Jakub Dawidek return (unit); 32832115b10SPawel Jakub Dawidek } 32932115b10SPawel Jakub Dawidek *errorp = ENFILE; 33032115b10SPawel Jakub Dawidek } 33132115b10SPawel Jakub Dawidek return (-1); 332fe27e772SPawel Jakub Dawidek } 333fe27e772SPawel Jakub Dawidek 334fe27e772SPawel Jakub Dawidek static void 335fe27e772SPawel Jakub Dawidek g_gate_guard(void *arg) 336fe27e772SPawel Jakub Dawidek { 33740ea77a0SAlexander Motin struct bio_queue_head queue; 338fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 339fe27e772SPawel Jakub Dawidek struct bintime curtime; 340fe27e772SPawel Jakub Dawidek struct bio *bp, *bp2; 341fe27e772SPawel Jakub Dawidek 342fe27e772SPawel Jakub Dawidek sc = arg; 343fe27e772SPawel Jakub Dawidek binuptime(&curtime); 34432115b10SPawel Jakub Dawidek g_gate_hold(sc->sc_unit, NULL); 34540ea77a0SAlexander Motin bioq_init(&queue); 346e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 347fe27e772SPawel Jakub Dawidek TAILQ_FOREACH_SAFE(bp, &sc->sc_inqueue.queue, bio_queue, bp2) { 348fe27e772SPawel Jakub Dawidek if (curtime.sec - bp->bio_t0.sec < 5) 349fe27e772SPawel Jakub Dawidek continue; 350fe27e772SPawel Jakub Dawidek bioq_remove(&sc->sc_inqueue, bp); 351e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 35240ea77a0SAlexander Motin bioq_insert_tail(&queue, bp); 353fe27e772SPawel Jakub Dawidek } 354fe27e772SPawel Jakub Dawidek TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, bp2) { 355fe27e772SPawel Jakub Dawidek if (curtime.sec - bp->bio_t0.sec < 5) 356fe27e772SPawel Jakub Dawidek continue; 357fe27e772SPawel Jakub Dawidek bioq_remove(&sc->sc_outqueue, bp); 358e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 35940ea77a0SAlexander Motin bioq_insert_tail(&queue, bp); 36040ea77a0SAlexander Motin } 36140ea77a0SAlexander Motin mtx_unlock(&sc->sc_queue_mtx); 36240ea77a0SAlexander Motin while ((bp = bioq_takefirst(&queue)) != NULL) { 363fe27e772SPawel Jakub Dawidek G_GATE_LOGREQ(1, bp, "Request timeout."); 364fe27e772SPawel Jakub Dawidek g_io_deliver(bp, EIO); 365fe27e772SPawel Jakub Dawidek } 366fe27e772SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) { 367fe27e772SPawel Jakub Dawidek callout_reset(&sc->sc_callout, sc->sc_timeout * hz, 368fe27e772SPawel Jakub Dawidek g_gate_guard, sc); 369fe27e772SPawel Jakub Dawidek } 370fe27e772SPawel Jakub Dawidek g_gate_release(sc); 371fe27e772SPawel Jakub Dawidek } 372fe27e772SPawel Jakub Dawidek 373fe27e772SPawel Jakub Dawidek static void 374e08ec037SPawel Jakub Dawidek g_gate_orphan(struct g_consumer *cp) 375e08ec037SPawel Jakub Dawidek { 376e08ec037SPawel Jakub Dawidek struct g_gate_softc *sc; 377e08ec037SPawel Jakub Dawidek struct g_geom *gp; 378e08ec037SPawel Jakub Dawidek 379e08ec037SPawel Jakub Dawidek g_topology_assert(); 380e08ec037SPawel Jakub Dawidek gp = cp->geom; 381e08ec037SPawel Jakub Dawidek sc = gp->softc; 382e08ec037SPawel Jakub Dawidek if (sc == NULL) 383e08ec037SPawel Jakub Dawidek return; 384e08ec037SPawel Jakub Dawidek KASSERT(cp == sc->sc_readcons, ("cp=%p sc_readcons=%p", cp, 385e08ec037SPawel Jakub Dawidek sc->sc_readcons)); 386e08ec037SPawel Jakub Dawidek sc->sc_readcons = NULL; 387e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Destroying read consumer on provider %s orphan.", 388e08ec037SPawel Jakub Dawidek cp->provider->name); 389e08ec037SPawel Jakub Dawidek (void)g_access(cp, -1, 0, 0); 390e08ec037SPawel Jakub Dawidek g_detach(cp); 391e08ec037SPawel Jakub Dawidek g_destroy_consumer(cp); 392e08ec037SPawel Jakub Dawidek } 393e08ec037SPawel Jakub Dawidek 394e08ec037SPawel Jakub Dawidek static void 395fe27e772SPawel Jakub Dawidek g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 396fe27e772SPawel Jakub Dawidek struct g_consumer *cp, struct g_provider *pp) 397fe27e772SPawel Jakub Dawidek { 398fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 399fe27e772SPawel Jakub Dawidek 400fe27e772SPawel Jakub Dawidek sc = gp->softc; 401fe27e772SPawel Jakub Dawidek if (sc == NULL || pp != NULL || cp != NULL) 402fe27e772SPawel Jakub Dawidek return; 4031d9db37cSMikolaj Golub sc = g_gate_hold(sc->sc_unit, NULL); 4041d9db37cSMikolaj Golub if (sc == NULL) 4051d9db37cSMikolaj Golub return; 406fe27e772SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) { 407fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only"); 408fe27e772SPawel Jakub Dawidek } else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) { 409fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<access>%s</access>\n", indent, 410fe27e772SPawel Jakub Dawidek "write-only"); 411fe27e772SPawel Jakub Dawidek } else { 412fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<access>%s</access>\n", indent, 413fe27e772SPawel Jakub Dawidek "read-write"); 414fe27e772SPawel Jakub Dawidek } 415e08ec037SPawel Jakub Dawidek if (sc->sc_readcons != NULL) { 416e08ec037SPawel Jakub Dawidek sbuf_printf(sb, "%s<read_offset>%jd</read_offset>\n", 417e08ec037SPawel Jakub Dawidek indent, (intmax_t)sc->sc_readoffset); 418e08ec037SPawel Jakub Dawidek sbuf_printf(sb, "%s<read_provider>%s</read_provider>\n", 419e08ec037SPawel Jakub Dawidek indent, sc->sc_readcons->provider->name); 420e08ec037SPawel Jakub Dawidek } 421fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout); 422fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info); 423fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent, 424fe27e772SPawel Jakub Dawidek sc->sc_queue_count); 425fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent, 426fe27e772SPawel Jakub Dawidek sc->sc_queue_size); 427fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref); 42832115b10SPawel Jakub Dawidek sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, sc->sc_unit); 42947f44cb7SPawel Jakub Dawidek g_topology_unlock(); 430fe27e772SPawel Jakub Dawidek g_gate_release(sc); 43147f44cb7SPawel Jakub Dawidek g_topology_lock(); 432fe27e772SPawel Jakub Dawidek } 433fe27e772SPawel Jakub Dawidek 434fe27e772SPawel Jakub Dawidek static int 435fe27e772SPawel Jakub Dawidek g_gate_create(struct g_gate_ctl_create *ggio) 436fe27e772SPawel Jakub Dawidek { 437fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 438fe27e772SPawel Jakub Dawidek struct g_geom *gp; 439e08ec037SPawel Jakub Dawidek struct g_provider *pp, *ropp; 440e08ec037SPawel Jakub Dawidek struct g_consumer *cp; 44132115b10SPawel Jakub Dawidek char name[NAME_MAX]; 44232115b10SPawel Jakub Dawidek int error = 0, unit; 443fe27e772SPawel Jakub Dawidek 444e08ec037SPawel Jakub Dawidek if (ggio->gctl_mediasize <= 0) { 445fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid media size."); 446fe27e772SPawel Jakub Dawidek return (EINVAL); 447fe27e772SPawel Jakub Dawidek } 448e08ec037SPawel Jakub Dawidek if (ggio->gctl_sectorsize <= 0) { 449e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid sector size."); 450e08ec037SPawel Jakub Dawidek return (EINVAL); 451e08ec037SPawel Jakub Dawidek } 452e08ec037SPawel Jakub Dawidek if (!powerof2(ggio->gctl_sectorsize)) { 453fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid sector size."); 454fe27e772SPawel Jakub Dawidek return (EINVAL); 455fe27e772SPawel Jakub Dawidek } 456662a4e58SPawel Jakub Dawidek if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) { 457662a4e58SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid media size."); 458662a4e58SPawel Jakub Dawidek return (EINVAL); 459662a4e58SPawel Jakub Dawidek } 460fe27e772SPawel Jakub Dawidek if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 && 461fe27e772SPawel Jakub Dawidek (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) { 462fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid flags."); 463fe27e772SPawel Jakub Dawidek return (EINVAL); 464fe27e772SPawel Jakub Dawidek } 46532115b10SPawel Jakub Dawidek if (ggio->gctl_unit != G_GATE_UNIT_AUTO && 46632115b10SPawel Jakub Dawidek ggio->gctl_unit != G_GATE_NAME_GIVEN && 46732115b10SPawel Jakub Dawidek ggio->gctl_unit < 0) { 468fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid unit number."); 469fe27e772SPawel Jakub Dawidek return (EINVAL); 470fe27e772SPawel Jakub Dawidek } 47132115b10SPawel Jakub Dawidek if (ggio->gctl_unit == G_GATE_NAME_GIVEN && 47232115b10SPawel Jakub Dawidek ggio->gctl_name[0] == '\0') { 47332115b10SPawel Jakub Dawidek G_GATE_DEBUG(1, "No device name."); 47432115b10SPawel Jakub Dawidek return (EINVAL); 47532115b10SPawel Jakub Dawidek } 476fe27e772SPawel Jakub Dawidek 477fe27e772SPawel Jakub Dawidek sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO); 478fe27e772SPawel Jakub Dawidek sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS); 479fe27e772SPawel Jakub Dawidek strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info)); 48032115b10SPawel Jakub Dawidek sc->sc_seq = 1; 481fe27e772SPawel Jakub Dawidek bioq_init(&sc->sc_inqueue); 482fe27e772SPawel Jakub Dawidek bioq_init(&sc->sc_outqueue); 483e35d3a78SPawel Jakub Dawidek mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF); 484fe27e772SPawel Jakub Dawidek sc->sc_queue_count = 0; 485fe27e772SPawel Jakub Dawidek sc->sc_queue_size = ggio->gctl_maxcount; 486fe27e772SPawel Jakub Dawidek if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE) 487fe27e772SPawel Jakub Dawidek sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE; 488fe27e772SPawel Jakub Dawidek sc->sc_timeout = ggio->gctl_timeout; 489fe27e772SPawel Jakub Dawidek callout_init(&sc->sc_callout, CALLOUT_MPSAFE); 490e08ec037SPawel Jakub Dawidek 49132115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 49232115b10SPawel Jakub Dawidek sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error); 493a277f47bSMikolaj Golub if (sc->sc_unit < 0) 494a277f47bSMikolaj Golub goto fail1; 49532115b10SPawel Jakub Dawidek if (ggio->gctl_unit == G_GATE_NAME_GIVEN) 49632115b10SPawel Jakub Dawidek snprintf(name, sizeof(name), "%s", ggio->gctl_name); 49732115b10SPawel Jakub Dawidek else { 49832115b10SPawel Jakub Dawidek snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME, 49932115b10SPawel Jakub Dawidek sc->sc_unit); 50032115b10SPawel Jakub Dawidek } 50132115b10SPawel Jakub Dawidek /* Check for name collision. */ 50232115b10SPawel Jakub Dawidek for (unit = 0; unit < g_gate_maxunits; unit++) { 50332115b10SPawel Jakub Dawidek if (g_gate_units[unit] == NULL) 50432115b10SPawel Jakub Dawidek continue; 505baf63f65SMikolaj Golub if (strcmp(name, g_gate_units[unit]->sc_name) != 0) 50632115b10SPawel Jakub Dawidek continue; 507a277f47bSMikolaj Golub error = EEXIST; 508a277f47bSMikolaj Golub goto fail1; 50932115b10SPawel Jakub Dawidek } 510baf63f65SMikolaj Golub sc->sc_name = name; 51132115b10SPawel Jakub Dawidek g_gate_units[sc->sc_unit] = sc; 51232115b10SPawel Jakub Dawidek g_gate_nunits++; 51332115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 51432115b10SPawel Jakub Dawidek 515a277f47bSMikolaj Golub g_topology_lock(); 516a277f47bSMikolaj Golub 517a277f47bSMikolaj Golub if (ggio->gctl_readprov[0] == '\0') { 518a277f47bSMikolaj Golub ropp = NULL; 519a277f47bSMikolaj Golub } else { 520a277f47bSMikolaj Golub ropp = g_provider_by_name(ggio->gctl_readprov); 521a277f47bSMikolaj Golub if (ropp == NULL) { 522a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Provider %s doesn't exist.", 523a277f47bSMikolaj Golub ggio->gctl_readprov); 524a277f47bSMikolaj Golub error = EINVAL; 525a277f47bSMikolaj Golub goto fail2; 526a277f47bSMikolaj Golub } 527a277f47bSMikolaj Golub if ((ggio->gctl_readoffset % ggio->gctl_sectorsize) != 0) { 528a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Invalid read offset."); 529a277f47bSMikolaj Golub error = EINVAL; 530a277f47bSMikolaj Golub goto fail2; 531a277f47bSMikolaj Golub } 532a277f47bSMikolaj Golub if (ggio->gctl_mediasize + ggio->gctl_readoffset > 533a277f47bSMikolaj Golub ropp->mediasize) { 534a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Invalid read offset or media size."); 535a277f47bSMikolaj Golub error = EINVAL; 536a277f47bSMikolaj Golub goto fail2; 537a277f47bSMikolaj Golub } 538a277f47bSMikolaj Golub } 539a277f47bSMikolaj Golub 540a277f47bSMikolaj Golub gp = g_new_geomf(&g_gate_class, "%s", name); 541a277f47bSMikolaj Golub gp->start = g_gate_start; 542a277f47bSMikolaj Golub gp->access = g_gate_access; 543a277f47bSMikolaj Golub gp->orphan = g_gate_orphan; 544a277f47bSMikolaj Golub gp->dumpconf = g_gate_dumpconf; 545a277f47bSMikolaj Golub gp->softc = sc; 546a277f47bSMikolaj Golub 547a277f47bSMikolaj Golub if (ropp != NULL) { 548a277f47bSMikolaj Golub cp = g_new_consumer(gp); 54940ea77a0SAlexander Motin cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 550a277f47bSMikolaj Golub error = g_attach(cp, ropp); 551a277f47bSMikolaj Golub if (error != 0) { 552a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Unable to attach to %s.", ropp->name); 553a277f47bSMikolaj Golub goto fail3; 554a277f47bSMikolaj Golub } 555a277f47bSMikolaj Golub error = g_access(cp, 1, 0, 0); 556a277f47bSMikolaj Golub if (error != 0) { 557a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Unable to access %s.", ropp->name); 558a277f47bSMikolaj Golub g_detach(cp); 559a277f47bSMikolaj Golub goto fail3; 560a277f47bSMikolaj Golub } 561a277f47bSMikolaj Golub sc->sc_readcons = cp; 562a277f47bSMikolaj Golub sc->sc_readoffset = ggio->gctl_readoffset; 563a277f47bSMikolaj Golub } 564a277f47bSMikolaj Golub 56532115b10SPawel Jakub Dawidek ggio->gctl_unit = sc->sc_unit; 566fe27e772SPawel Jakub Dawidek 56732115b10SPawel Jakub Dawidek pp = g_new_providerf(gp, "%s", name); 56840ea77a0SAlexander Motin pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 569fe27e772SPawel Jakub Dawidek pp->mediasize = ggio->gctl_mediasize; 570fe27e772SPawel Jakub Dawidek pp->sectorsize = ggio->gctl_sectorsize; 571fe27e772SPawel Jakub Dawidek sc->sc_provider = pp; 572fe27e772SPawel Jakub Dawidek g_error_provider(pp, 0); 573e08ec037SPawel Jakub Dawidek 574fe27e772SPawel Jakub Dawidek g_topology_unlock(); 575baf63f65SMikolaj Golub mtx_lock(&g_gate_units_lock); 576baf63f65SMikolaj Golub sc->sc_name = sc->sc_provider->name; 577baf63f65SMikolaj Golub mtx_unlock(&g_gate_units_lock); 578bd119384SMikolaj Golub G_GATE_DEBUG(1, "Device %s created.", gp->name); 579fe27e772SPawel Jakub Dawidek 580fe27e772SPawel Jakub Dawidek if (sc->sc_timeout > 0) { 581fe27e772SPawel Jakub Dawidek callout_reset(&sc->sc_callout, sc->sc_timeout * hz, 582fe27e772SPawel Jakub Dawidek g_gate_guard, sc); 583fe27e772SPawel Jakub Dawidek } 584fe27e772SPawel Jakub Dawidek return (0); 585a277f47bSMikolaj Golub fail3: 586a277f47bSMikolaj Golub g_destroy_consumer(cp); 587a277f47bSMikolaj Golub g_destroy_geom(gp); 588a277f47bSMikolaj Golub fail2: 589a277f47bSMikolaj Golub g_topology_unlock(); 590a277f47bSMikolaj Golub mtx_lock(&g_gate_units_lock); 591a277f47bSMikolaj Golub g_gate_units[sc->sc_unit] = NULL; 592a277f47bSMikolaj Golub KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?")); 593a277f47bSMikolaj Golub g_gate_nunits--; 594a277f47bSMikolaj Golub fail1: 595a277f47bSMikolaj Golub mtx_unlock(&g_gate_units_lock); 596a277f47bSMikolaj Golub mtx_destroy(&sc->sc_queue_mtx); 597a277f47bSMikolaj Golub free(sc, M_GATE); 598a277f47bSMikolaj Golub return (error); 599fe27e772SPawel Jakub Dawidek } 600fe27e772SPawel Jakub Dawidek 601e08ec037SPawel Jakub Dawidek static int 602e08ec037SPawel Jakub Dawidek g_gate_modify(struct g_gate_softc *sc, struct g_gate_ctl_modify *ggio) 603e08ec037SPawel Jakub Dawidek { 604e08ec037SPawel Jakub Dawidek struct g_provider *pp; 605e08ec037SPawel Jakub Dawidek struct g_consumer *cp; 606e08ec037SPawel Jakub Dawidek int error; 607e08ec037SPawel Jakub Dawidek 608e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_MEDIASIZE) != 0) { 609e08ec037SPawel Jakub Dawidek if (ggio->gctl_mediasize <= 0) { 610e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid media size."); 611e08ec037SPawel Jakub Dawidek return (EINVAL); 612e08ec037SPawel Jakub Dawidek } 613e08ec037SPawel Jakub Dawidek pp = sc->sc_provider; 614e08ec037SPawel Jakub Dawidek if ((ggio->gctl_mediasize % pp->sectorsize) != 0) { 615e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid media size."); 616e08ec037SPawel Jakub Dawidek return (EINVAL); 617e08ec037SPawel Jakub Dawidek } 618e08ec037SPawel Jakub Dawidek /* TODO */ 619e08ec037SPawel Jakub Dawidek return (EOPNOTSUPP); 620e08ec037SPawel Jakub Dawidek } 621e08ec037SPawel Jakub Dawidek 622e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_INFO) != 0) 623e08ec037SPawel Jakub Dawidek (void)strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info)); 624e08ec037SPawel Jakub Dawidek 625e08ec037SPawel Jakub Dawidek cp = NULL; 626e08ec037SPawel Jakub Dawidek 627e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) { 628e08ec037SPawel Jakub Dawidek g_topology_lock(); 629e08ec037SPawel Jakub Dawidek if (sc->sc_readcons != NULL) { 630e08ec037SPawel Jakub Dawidek cp = sc->sc_readcons; 631e08ec037SPawel Jakub Dawidek sc->sc_readcons = NULL; 632e08ec037SPawel Jakub Dawidek (void)g_access(cp, -1, 0, 0); 633e08ec037SPawel Jakub Dawidek g_detach(cp); 634e08ec037SPawel Jakub Dawidek g_destroy_consumer(cp); 635e08ec037SPawel Jakub Dawidek } 636e08ec037SPawel Jakub Dawidek if (ggio->gctl_readprov[0] != '\0') { 637e08ec037SPawel Jakub Dawidek pp = g_provider_by_name(ggio->gctl_readprov); 638e08ec037SPawel Jakub Dawidek if (pp == NULL) { 639e08ec037SPawel Jakub Dawidek g_topology_unlock(); 640e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Provider %s doesn't exist.", 641e08ec037SPawel Jakub Dawidek ggio->gctl_readprov); 642e08ec037SPawel Jakub Dawidek return (EINVAL); 643e08ec037SPawel Jakub Dawidek } 644e08ec037SPawel Jakub Dawidek cp = g_new_consumer(sc->sc_provider->geom); 64540ea77a0SAlexander Motin cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 646e08ec037SPawel Jakub Dawidek error = g_attach(cp, pp); 647e08ec037SPawel Jakub Dawidek if (error != 0) { 648e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Unable to attach to %s.", 649e08ec037SPawel Jakub Dawidek pp->name); 650e08ec037SPawel Jakub Dawidek } else { 651e08ec037SPawel Jakub Dawidek error = g_access(cp, 1, 0, 0); 652e08ec037SPawel Jakub Dawidek if (error != 0) { 653e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Unable to access %s.", 654e08ec037SPawel Jakub Dawidek pp->name); 655e08ec037SPawel Jakub Dawidek g_detach(cp); 656e08ec037SPawel Jakub Dawidek } 657e08ec037SPawel Jakub Dawidek } 658e08ec037SPawel Jakub Dawidek if (error != 0) { 659e08ec037SPawel Jakub Dawidek g_destroy_consumer(cp); 660e08ec037SPawel Jakub Dawidek g_topology_unlock(); 661e08ec037SPawel Jakub Dawidek return (error); 662e08ec037SPawel Jakub Dawidek } 663e08ec037SPawel Jakub Dawidek } 664e08ec037SPawel Jakub Dawidek } else { 665e08ec037SPawel Jakub Dawidek cp = sc->sc_readcons; 666e08ec037SPawel Jakub Dawidek } 667e08ec037SPawel Jakub Dawidek 668e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_READOFFSET) != 0) { 669e08ec037SPawel Jakub Dawidek if (cp == NULL) { 670e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "No read provider."); 671e08ec037SPawel Jakub Dawidek return (EINVAL); 672e08ec037SPawel Jakub Dawidek } 673e08ec037SPawel Jakub Dawidek pp = sc->sc_provider; 674e08ec037SPawel Jakub Dawidek if ((ggio->gctl_readoffset % pp->sectorsize) != 0) { 675e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid read offset."); 676e08ec037SPawel Jakub Dawidek return (EINVAL); 677e08ec037SPawel Jakub Dawidek } 678e08ec037SPawel Jakub Dawidek if (pp->mediasize + ggio->gctl_readoffset > 679e08ec037SPawel Jakub Dawidek cp->provider->mediasize) { 680e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid read offset or media size."); 681e08ec037SPawel Jakub Dawidek return (EINVAL); 682e08ec037SPawel Jakub Dawidek } 683e08ec037SPawel Jakub Dawidek sc->sc_readoffset = ggio->gctl_readoffset; 684e08ec037SPawel Jakub Dawidek } 685e08ec037SPawel Jakub Dawidek 686e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) { 687e08ec037SPawel Jakub Dawidek sc->sc_readcons = cp; 688e08ec037SPawel Jakub Dawidek g_topology_unlock(); 689e08ec037SPawel Jakub Dawidek } 690e08ec037SPawel Jakub Dawidek 691e08ec037SPawel Jakub Dawidek return (0); 692e08ec037SPawel Jakub Dawidek } 693e08ec037SPawel Jakub Dawidek 694fe27e772SPawel Jakub Dawidek #define G_GATE_CHECK_VERSION(ggio) do { \ 69584436f14SPawel Jakub Dawidek if ((ggio)->gctl_version != G_GATE_VERSION) { \ 69684436f14SPawel Jakub Dawidek printf("Version mismatch %d != %d.\n", \ 69784436f14SPawel Jakub Dawidek ggio->gctl_version, G_GATE_VERSION); \ 698fe27e772SPawel Jakub Dawidek return (EINVAL); \ 69984436f14SPawel Jakub Dawidek } \ 700fe27e772SPawel Jakub Dawidek } while (0) 701fe27e772SPawel Jakub Dawidek static int 70289c9c53dSPoul-Henning Kamp g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 703fe27e772SPawel Jakub Dawidek { 704fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 705fe27e772SPawel Jakub Dawidek struct bio *bp; 706fe27e772SPawel Jakub Dawidek int error = 0; 707fe27e772SPawel Jakub Dawidek 708fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr, 709fe27e772SPawel Jakub Dawidek flags, td); 710fe27e772SPawel Jakub Dawidek 711fe27e772SPawel Jakub Dawidek switch (cmd) { 712fe27e772SPawel Jakub Dawidek case G_GATE_CMD_CREATE: 713fe27e772SPawel Jakub Dawidek { 714fe27e772SPawel Jakub Dawidek struct g_gate_ctl_create *ggio = (void *)addr; 715fe27e772SPawel Jakub Dawidek 716fe27e772SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 717a17dd95fSPawel Jakub Dawidek error = g_gate_create(ggio); 718f9065812SPawel Jakub Dawidek /* 719f9065812SPawel Jakub Dawidek * Reset TDP_GEOM flag. 720f9065812SPawel Jakub Dawidek * There are pending events for sure, because we just created 721f9065812SPawel Jakub Dawidek * new provider and other classes want to taste it, but we 722f9065812SPawel Jakub Dawidek * cannot answer on I/O requests until we're here. 723f9065812SPawel Jakub Dawidek */ 724f9065812SPawel Jakub Dawidek td->td_pflags &= ~TDP_GEOM; 725a17dd95fSPawel Jakub Dawidek return (error); 726fe27e772SPawel Jakub Dawidek } 727e08ec037SPawel Jakub Dawidek case G_GATE_CMD_MODIFY: 728e08ec037SPawel Jakub Dawidek { 729e08ec037SPawel Jakub Dawidek struct g_gate_ctl_modify *ggio = (void *)addr; 730e08ec037SPawel Jakub Dawidek 731e08ec037SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 732e08ec037SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, NULL); 733e08ec037SPawel Jakub Dawidek if (sc == NULL) 734e08ec037SPawel Jakub Dawidek return (ENXIO); 735e08ec037SPawel Jakub Dawidek error = g_gate_modify(sc, ggio); 736e08ec037SPawel Jakub Dawidek g_gate_release(sc); 737e08ec037SPawel Jakub Dawidek return (error); 738e08ec037SPawel Jakub Dawidek } 739fe27e772SPawel Jakub Dawidek case G_GATE_CMD_DESTROY: 740fe27e772SPawel Jakub Dawidek { 741fe27e772SPawel Jakub Dawidek struct g_gate_ctl_destroy *ggio = (void *)addr; 742fe27e772SPawel Jakub Dawidek 743fe27e772SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 74432115b10SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name); 745fe27e772SPawel Jakub Dawidek if (sc == NULL) 746fe27e772SPawel Jakub Dawidek return (ENXIO); 747fe27e772SPawel Jakub Dawidek g_topology_lock(); 74832115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 749fe27e772SPawel Jakub Dawidek error = g_gate_destroy(sc, ggio->gctl_force); 750fe27e772SPawel Jakub Dawidek g_topology_unlock(); 7517ffb6e0fSPawel Jakub Dawidek if (error != 0) 752fe27e772SPawel Jakub Dawidek g_gate_release(sc); 753fe27e772SPawel Jakub Dawidek return (error); 754fe27e772SPawel Jakub Dawidek } 75584436f14SPawel Jakub Dawidek case G_GATE_CMD_CANCEL: 75684436f14SPawel Jakub Dawidek { 75784436f14SPawel Jakub Dawidek struct g_gate_ctl_cancel *ggio = (void *)addr; 75884436f14SPawel Jakub Dawidek struct bio *tbp, *lbp; 75984436f14SPawel Jakub Dawidek 76084436f14SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 76132115b10SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name); 76284436f14SPawel Jakub Dawidek if (sc == NULL) 76384436f14SPawel Jakub Dawidek return (ENXIO); 76484436f14SPawel Jakub Dawidek lbp = NULL; 76584436f14SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 76684436f14SPawel Jakub Dawidek TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) { 76784436f14SPawel Jakub Dawidek if (ggio->gctl_seq == 0 || 76884436f14SPawel Jakub Dawidek ggio->gctl_seq == (uintptr_t)bp->bio_driver1) { 76984436f14SPawel Jakub Dawidek G_GATE_LOGREQ(1, bp, "Request canceled."); 77084436f14SPawel Jakub Dawidek bioq_remove(&sc->sc_outqueue, bp); 77184436f14SPawel Jakub Dawidek /* 77284436f14SPawel Jakub Dawidek * Be sure to put requests back onto incoming 77384436f14SPawel Jakub Dawidek * queue in the proper order. 77484436f14SPawel Jakub Dawidek */ 77584436f14SPawel Jakub Dawidek if (lbp == NULL) 77684436f14SPawel Jakub Dawidek bioq_insert_head(&sc->sc_inqueue, bp); 77784436f14SPawel Jakub Dawidek else { 77884436f14SPawel Jakub Dawidek TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue, 77984436f14SPawel Jakub Dawidek lbp, bp, bio_queue); 78084436f14SPawel Jakub Dawidek } 78184436f14SPawel Jakub Dawidek lbp = bp; 78284436f14SPawel Jakub Dawidek /* 78384436f14SPawel Jakub Dawidek * If only one request was canceled, leave now. 78484436f14SPawel Jakub Dawidek */ 78584436f14SPawel Jakub Dawidek if (ggio->gctl_seq != 0) 78684436f14SPawel Jakub Dawidek break; 78784436f14SPawel Jakub Dawidek } 78884436f14SPawel Jakub Dawidek } 78932115b10SPawel Jakub Dawidek if (ggio->gctl_unit == G_GATE_NAME_GIVEN) 79032115b10SPawel Jakub Dawidek ggio->gctl_unit = sc->sc_unit; 79184436f14SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 79284436f14SPawel Jakub Dawidek g_gate_release(sc); 79384436f14SPawel Jakub Dawidek return (error); 79484436f14SPawel Jakub Dawidek } 795fe27e772SPawel Jakub Dawidek case G_GATE_CMD_START: 796fe27e772SPawel Jakub Dawidek { 797fe27e772SPawel Jakub Dawidek struct g_gate_ctl_io *ggio = (void *)addr; 798fe27e772SPawel Jakub Dawidek 799fe27e772SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 80032115b10SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, NULL); 801fe27e772SPawel Jakub Dawidek if (sc == NULL) 802fe27e772SPawel Jakub Dawidek return (ENXIO); 8037ffb6e0fSPawel Jakub Dawidek error = 0; 804fe27e772SPawel Jakub Dawidek for (;;) { 805e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 806fe27e772SPawel Jakub Dawidek bp = bioq_first(&sc->sc_inqueue); 807fe27e772SPawel Jakub Dawidek if (bp != NULL) 808fe27e772SPawel Jakub Dawidek break; 8097ffb6e0fSPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 8107ffb6e0fSPawel Jakub Dawidek ggio->gctl_error = ECANCELED; 8117ffb6e0fSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 8127ffb6e0fSPawel Jakub Dawidek goto start_end; 8137ffb6e0fSPawel Jakub Dawidek } 814e35d3a78SPawel Jakub Dawidek if (msleep(sc, &sc->sc_queue_mtx, 815fe27e772SPawel Jakub Dawidek PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) { 816fe27e772SPawel Jakub Dawidek ggio->gctl_error = ECANCELED; 8177ffb6e0fSPawel Jakub Dawidek goto start_end; 818fe27e772SPawel Jakub Dawidek } 819fe27e772SPawel Jakub Dawidek } 8204d1e1bf3SPawel Jakub Dawidek ggio->gctl_cmd = bp->bio_cmd; 821c4d2d401SPawel Jakub Dawidek if (bp->bio_cmd == BIO_WRITE && 822fe27e772SPawel Jakub Dawidek bp->bio_length > ggio->gctl_length) { 823e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 824fe27e772SPawel Jakub Dawidek ggio->gctl_length = bp->bio_length; 825fe27e772SPawel Jakub Dawidek ggio->gctl_error = ENOMEM; 8267ffb6e0fSPawel Jakub Dawidek goto start_end; 827fe27e772SPawel Jakub Dawidek } 828fe27e772SPawel Jakub Dawidek bioq_remove(&sc->sc_inqueue, bp); 829e35d3a78SPawel Jakub Dawidek bioq_insert_tail(&sc->sc_outqueue, bp); 830e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 831e35d3a78SPawel Jakub Dawidek 8320d785336SPawel Jakub Dawidek ggio->gctl_seq = (uintptr_t)bp->bio_driver1; 833fe27e772SPawel Jakub Dawidek ggio->gctl_offset = bp->bio_offset; 834fe27e772SPawel Jakub Dawidek ggio->gctl_length = bp->bio_length; 83584436f14SPawel Jakub Dawidek 836fe27e772SPawel Jakub Dawidek switch (bp->bio_cmd) { 837fe27e772SPawel Jakub Dawidek case BIO_READ: 838fe27e772SPawel Jakub Dawidek case BIO_DELETE: 839204a4e19SPawel Jakub Dawidek case BIO_FLUSH: 84015725379SPawel Jakub Dawidek break; 841fe27e772SPawel Jakub Dawidek case BIO_WRITE: 842fe27e772SPawel Jakub Dawidek error = copyout(bp->bio_data, ggio->gctl_data, 843fe27e772SPawel Jakub Dawidek bp->bio_length); 844fe27e772SPawel Jakub Dawidek if (error != 0) { 845e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 846e35d3a78SPawel Jakub Dawidek bioq_remove(&sc->sc_outqueue, bp); 847662a4e58SPawel Jakub Dawidek bioq_insert_head(&sc->sc_inqueue, bp); 848e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 8497ffb6e0fSPawel Jakub Dawidek goto start_end; 850fe27e772SPawel Jakub Dawidek } 851fe27e772SPawel Jakub Dawidek break; 852fe27e772SPawel Jakub Dawidek } 8537ffb6e0fSPawel Jakub Dawidek start_end: 8547ffb6e0fSPawel Jakub Dawidek g_gate_release(sc); 8557ffb6e0fSPawel Jakub Dawidek return (error); 856fe27e772SPawel Jakub Dawidek } 857fe27e772SPawel Jakub Dawidek case G_GATE_CMD_DONE: 858fe27e772SPawel Jakub Dawidek { 859fe27e772SPawel Jakub Dawidek struct g_gate_ctl_io *ggio = (void *)addr; 860fe27e772SPawel Jakub Dawidek 861fe27e772SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 86232115b10SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, NULL); 863fe27e772SPawel Jakub Dawidek if (sc == NULL) 864fe27e772SPawel Jakub Dawidek return (ENOENT); 8657ffb6e0fSPawel Jakub Dawidek error = 0; 866e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 867fe27e772SPawel Jakub Dawidek TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) { 8680d785336SPawel Jakub Dawidek if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1) 869fe27e772SPawel Jakub Dawidek break; 870fe27e772SPawel Jakub Dawidek } 871fe27e772SPawel Jakub Dawidek if (bp != NULL) { 872fe27e772SPawel Jakub Dawidek bioq_remove(&sc->sc_outqueue, bp); 873e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 874fe27e772SPawel Jakub Dawidek } 875e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 876fe27e772SPawel Jakub Dawidek if (bp == NULL) { 877fe27e772SPawel Jakub Dawidek /* 878fe27e772SPawel Jakub Dawidek * Request was probably canceled. 879fe27e772SPawel Jakub Dawidek */ 8807ffb6e0fSPawel Jakub Dawidek goto done_end; 881fe27e772SPawel Jakub Dawidek } 882fe27e772SPawel Jakub Dawidek if (ggio->gctl_error == EAGAIN) { 883fe27e772SPawel Jakub Dawidek bp->bio_error = 0; 884fe27e772SPawel Jakub Dawidek G_GATE_LOGREQ(1, bp, "Request desisted."); 885e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 886e35d3a78SPawel Jakub Dawidek sc->sc_queue_count++; 887662a4e58SPawel Jakub Dawidek bioq_insert_head(&sc->sc_inqueue, bp); 888fe27e772SPawel Jakub Dawidek wakeup(sc); 889e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 890fe27e772SPawel Jakub Dawidek } else { 891fe27e772SPawel Jakub Dawidek bp->bio_error = ggio->gctl_error; 892fe27e772SPawel Jakub Dawidek if (bp->bio_error == 0) { 893fe27e772SPawel Jakub Dawidek bp->bio_completed = bp->bio_length; 894fe27e772SPawel Jakub Dawidek switch (bp->bio_cmd) { 895fe27e772SPawel Jakub Dawidek case BIO_READ: 896fe27e772SPawel Jakub Dawidek error = copyin(ggio->gctl_data, 897fe27e772SPawel Jakub Dawidek bp->bio_data, bp->bio_length); 898fe27e772SPawel Jakub Dawidek if (error != 0) 899fe27e772SPawel Jakub Dawidek bp->bio_error = error; 900fe27e772SPawel Jakub Dawidek break; 901fe27e772SPawel Jakub Dawidek case BIO_DELETE: 902fe27e772SPawel Jakub Dawidek case BIO_WRITE: 903204a4e19SPawel Jakub Dawidek case BIO_FLUSH: 904fe27e772SPawel Jakub Dawidek break; 905fe27e772SPawel Jakub Dawidek } 906fe27e772SPawel Jakub Dawidek } 907fe27e772SPawel Jakub Dawidek G_GATE_LOGREQ(2, bp, "Request done."); 908fe27e772SPawel Jakub Dawidek g_io_deliver(bp, bp->bio_error); 909fe27e772SPawel Jakub Dawidek } 9107ffb6e0fSPawel Jakub Dawidek done_end: 9117ffb6e0fSPawel Jakub Dawidek g_gate_release(sc); 912fe27e772SPawel Jakub Dawidek return (error); 913fe27e772SPawel Jakub Dawidek } 914fe27e772SPawel Jakub Dawidek } 915fe27e772SPawel Jakub Dawidek return (ENOIOCTL); 916fe27e772SPawel Jakub Dawidek } 917fe27e772SPawel Jakub Dawidek 918fe27e772SPawel Jakub Dawidek static void 9199ecdd506SPawel Jakub Dawidek g_gate_device(void) 920fe27e772SPawel Jakub Dawidek { 921fe27e772SPawel Jakub Dawidek 922fe27e772SPawel Jakub Dawidek status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600, 923fe27e772SPawel Jakub Dawidek G_GATE_CTL_NAME); 924fe27e772SPawel Jakub Dawidek } 925fe27e772SPawel Jakub Dawidek 926fe27e772SPawel Jakub Dawidek static int 927fe27e772SPawel Jakub Dawidek g_gate_modevent(module_t mod, int type, void *data) 928fe27e772SPawel Jakub Dawidek { 929fe27e772SPawel Jakub Dawidek int error = 0; 930fe27e772SPawel Jakub Dawidek 931fe27e772SPawel Jakub Dawidek switch (type) { 932fe27e772SPawel Jakub Dawidek case MOD_LOAD: 93332115b10SPawel Jakub Dawidek mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF); 93432115b10SPawel Jakub Dawidek g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]), 93532115b10SPawel Jakub Dawidek M_GATE, M_WAITOK | M_ZERO); 93632115b10SPawel Jakub Dawidek g_gate_nunits = 0; 9379ecdd506SPawel Jakub Dawidek g_gate_device(); 938fe27e772SPawel Jakub Dawidek break; 939fe27e772SPawel Jakub Dawidek case MOD_UNLOAD: 94032115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 94132115b10SPawel Jakub Dawidek if (g_gate_nunits > 0) { 94232115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 943fe27e772SPawel Jakub Dawidek error = EBUSY; 944fe27e772SPawel Jakub Dawidek break; 945fe27e772SPawel Jakub Dawidek } 94632115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 94732115b10SPawel Jakub Dawidek mtx_destroy(&g_gate_units_lock); 948fe27e772SPawel Jakub Dawidek if (status_dev != 0) 949fe27e772SPawel Jakub Dawidek destroy_dev(status_dev); 95032115b10SPawel Jakub Dawidek free(g_gate_units, M_GATE); 951fe27e772SPawel Jakub Dawidek break; 952fe27e772SPawel Jakub Dawidek default: 9533e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 954fe27e772SPawel Jakub Dawidek break; 955fe27e772SPawel Jakub Dawidek } 956fe27e772SPawel Jakub Dawidek 957fe27e772SPawel Jakub Dawidek return (error); 958fe27e772SPawel Jakub Dawidek } 959fe27e772SPawel Jakub Dawidek static moduledata_t g_gate_module = { 960fe27e772SPawel Jakub Dawidek G_GATE_MOD_NAME, 961fe27e772SPawel Jakub Dawidek g_gate_modevent, 962fe27e772SPawel Jakub Dawidek NULL 963fe27e772SPawel Jakub Dawidek }; 964fe27e772SPawel Jakub Dawidek DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 965fe27e772SPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_gate_class, g_gate); 966