1fe27e772SPawel Jakub Dawidek /*- 23728855aSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 33728855aSPedro F. Giffuni * 4fc024f7aSPawel Jakub Dawidek * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 532115b10SPawel Jakub Dawidek * Copyright (c) 2009-2010 The FreeBSD Foundation 6fe27e772SPawel Jakub Dawidek * All rights reserved. 7fe27e772SPawel Jakub Dawidek * 832115b10SPawel Jakub Dawidek * Portions of this software were developed by Pawel Jakub Dawidek 932115b10SPawel Jakub Dawidek * under sponsorship from the FreeBSD Foundation. 1032115b10SPawel Jakub Dawidek * 11fe27e772SPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without 12fe27e772SPawel Jakub Dawidek * modification, are permitted provided that the following conditions 13fe27e772SPawel Jakub Dawidek * are met: 14fe27e772SPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright 15fe27e772SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer. 16fe27e772SPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright 17fe27e772SPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer in the 18fe27e772SPawel Jakub Dawidek * documentation and/or other materials provided with the distribution. 19fe27e772SPawel Jakub Dawidek * 20fe27e772SPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 21fe27e772SPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22fe27e772SPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23fe27e772SPawel Jakub Dawidek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 24fe27e772SPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25fe27e772SPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26fe27e772SPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27fe27e772SPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28fe27e772SPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29fe27e772SPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30fe27e772SPawel Jakub Dawidek * SUCH DAMAGE. 31fe27e772SPawel Jakub Dawidek */ 32fe27e772SPawel Jakub Dawidek 33c0767902SPawel Jakub Dawidek #include <sys/cdefs.h> 34c0767902SPawel Jakub Dawidek __FBSDID("$FreeBSD$"); 35c0767902SPawel Jakub Dawidek 36fe27e772SPawel Jakub Dawidek #include <sys/param.h> 37fe27e772SPawel Jakub Dawidek #include <sys/systm.h> 38fe27e772SPawel Jakub Dawidek #include <sys/bio.h> 39fe27e772SPawel Jakub Dawidek #include <sys/conf.h> 40fe27e772SPawel Jakub Dawidek #include <sys/kernel.h> 41fe27e772SPawel Jakub Dawidek #include <sys/kthread.h> 42fe27e772SPawel Jakub Dawidek #include <sys/fcntl.h> 43fe27e772SPawel Jakub Dawidek #include <sys/linker.h> 44fe27e772SPawel Jakub Dawidek #include <sys/lock.h> 45fe27e772SPawel Jakub Dawidek #include <sys/malloc.h> 46fe27e772SPawel Jakub Dawidek #include <sys/mutex.h> 47fe27e772SPawel Jakub Dawidek #include <sys/proc.h> 48fe27e772SPawel Jakub Dawidek #include <sys/limits.h> 49fe27e772SPawel Jakub Dawidek #include <sys/queue.h> 505d807a0eSAndrey V. Elsukov #include <sys/sbuf.h> 51fe27e772SPawel Jakub Dawidek #include <sys/sysctl.h> 52fe27e772SPawel Jakub Dawidek #include <sys/signalvar.h> 53fe27e772SPawel Jakub Dawidek #include <sys/time.h> 54fe27e772SPawel Jakub Dawidek #include <machine/atomic.h> 55fe27e772SPawel Jakub Dawidek 56fe27e772SPawel Jakub Dawidek #include <geom/geom.h> 57fe27e772SPawel Jakub Dawidek #include <geom/gate/g_gate.h> 58fe27e772SPawel Jakub Dawidek 59cb08c2ccSAlexander Leidinger FEATURE(geom_gate, "GEOM Gate module"); 60cb08c2ccSAlexander Leidinger 615bb84bc8SRobert Watson static MALLOC_DEFINE(M_GATE, "gg_data", "GEOM Gate Data"); 62fe27e772SPawel Jakub Dawidek 63fe27e772SPawel Jakub Dawidek SYSCTL_DECL(_kern_geom); 646472ac3dSEd Schouten static SYSCTL_NODE(_kern_geom, OID_AUTO, gate, CTLFLAG_RW, 0, 65e08ec037SPawel Jakub Dawidek "GEOM_GATE configuration"); 6632115b10SPawel Jakub Dawidek static int g_gate_debug = 0; 67af3b2549SHans Petter Selasky SYSCTL_INT(_kern_geom_gate, OID_AUTO, debug, CTLFLAG_RWTUN, &g_gate_debug, 0, 68fe27e772SPawel Jakub Dawidek "Debug level"); 6932115b10SPawel Jakub Dawidek static u_int g_gate_maxunits = 256; 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 { 9440ea77a0SAlexander 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; 1148b64f3caSAlexander Motin g_wither_provider(pp, ENXIO); 115fe27e772SPawel Jakub Dawidek callout_drain(&sc->sc_callout); 11640ea77a0SAlexander Motin bioq_init(&queue); 117e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 11840ea77a0SAlexander Motin while ((bp = bioq_takefirst(&sc->sc_inqueue)) != NULL) { 119e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 12040ea77a0SAlexander Motin bioq_insert_tail(&queue, bp); 121fe27e772SPawel Jakub Dawidek } 12240ea77a0SAlexander Motin while ((bp = bioq_takefirst(&sc->sc_outqueue)) != NULL) { 123e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 12440ea77a0SAlexander Motin bioq_insert_tail(&queue, bp); 125fe27e772SPawel Jakub Dawidek } 1267ffb6e0fSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 1277ffb6e0fSPawel Jakub Dawidek g_topology_unlock(); 12840ea77a0SAlexander Motin while ((bp = bioq_takefirst(&queue)) != NULL) { 12940ea77a0SAlexander Motin G_GATE_LOGREQ(1, bp, "Request canceled."); 13040ea77a0SAlexander Motin g_io_deliver(bp, ENXIO); 13140ea77a0SAlexander Motin } 13232115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 1337ffb6e0fSPawel Jakub Dawidek /* One reference is ours. */ 1347ffb6e0fSPawel Jakub Dawidek sc->sc_ref--; 13532115b10SPawel Jakub Dawidek while (sc->sc_ref > 0) 13632115b10SPawel Jakub Dawidek msleep(&sc->sc_ref, &g_gate_units_lock, 0, "gg:destroy", 0); 13732115b10SPawel Jakub Dawidek g_gate_units[sc->sc_unit] = NULL; 13832115b10SPawel Jakub Dawidek KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?")); 13932115b10SPawel Jakub Dawidek g_gate_nunits--; 14032115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 141e35d3a78SPawel Jakub Dawidek mtx_destroy(&sc->sc_queue_mtx); 1427ffb6e0fSPawel Jakub Dawidek g_topology_lock(); 143e08ec037SPawel Jakub Dawidek if ((cp = sc->sc_readcons) != NULL) { 144e08ec037SPawel Jakub Dawidek sc->sc_readcons = NULL; 145e08ec037SPawel Jakub Dawidek (void)g_access(cp, -1, 0, 0); 146e08ec037SPawel Jakub Dawidek g_detach(cp); 147e08ec037SPawel Jakub Dawidek g_destroy_consumer(cp); 148e08ec037SPawel Jakub Dawidek } 149bd119384SMikolaj Golub G_GATE_DEBUG(1, "Device %s destroyed.", gp->name); 1507ffb6e0fSPawel Jakub Dawidek gp->softc = NULL; 1517ffb6e0fSPawel Jakub Dawidek g_wither_geom(gp, ENXIO); 152fe27e772SPawel Jakub Dawidek sc->sc_provider = NULL; 153fe27e772SPawel Jakub Dawidek free(sc, M_GATE); 154fe27e772SPawel Jakub Dawidek return (0); 155fe27e772SPawel Jakub Dawidek } 156fe27e772SPawel Jakub Dawidek 157fe27e772SPawel Jakub Dawidek static int 158fe27e772SPawel Jakub Dawidek g_gate_access(struct g_provider *pp, int dr, int dw, int de) 159fe27e772SPawel Jakub Dawidek { 160fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 161fe27e772SPawel Jakub Dawidek 162fe27e772SPawel Jakub Dawidek if (dr <= 0 && dw <= 0 && de <= 0) 163fe27e772SPawel Jakub Dawidek return (0); 164fe27e772SPawel Jakub Dawidek sc = pp->geom->softc; 165fe27e772SPawel Jakub Dawidek if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) 166fe27e772SPawel Jakub Dawidek return (ENXIO); 16755336b83SPawel Jakub Dawidek /* XXX: Hack to allow read-only mounts. */ 16855336b83SPawel Jakub Dawidek #if 0 169fe27e772SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0 && dw > 0) 170fe27e772SPawel Jakub Dawidek return (EPERM); 17155336b83SPawel Jakub Dawidek #endif 172fe27e772SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0 && dr > 0) 173fe27e772SPawel Jakub Dawidek return (EPERM); 174fe27e772SPawel Jakub Dawidek return (0); 175fe27e772SPawel Jakub Dawidek } 176fe27e772SPawel Jakub Dawidek 177fe27e772SPawel Jakub Dawidek static void 178e08ec037SPawel Jakub Dawidek g_gate_queue_io(struct bio *bp) 179fe27e772SPawel Jakub Dawidek { 180fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 181fe27e772SPawel Jakub Dawidek 182fe27e772SPawel Jakub Dawidek sc = bp->bio_to->geom->softc; 183fe27e772SPawel Jakub Dawidek if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 184fe27e772SPawel Jakub Dawidek g_io_deliver(bp, ENXIO); 185fe27e772SPawel Jakub Dawidek return; 186fe27e772SPawel Jakub Dawidek } 187fe27e772SPawel Jakub Dawidek 188e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 189e08ec037SPawel Jakub Dawidek 19063a6c5c1SPawel Jakub Dawidek if (sc->sc_queue_size > 0 && sc->sc_queue_count > sc->sc_queue_size) { 19135f855d9SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 192fe27e772SPawel Jakub Dawidek G_GATE_LOGREQ(1, bp, "Queue full, request canceled."); 19332115b10SPawel Jakub Dawidek g_io_deliver(bp, ENOMEM); 194fe27e772SPawel Jakub Dawidek return; 195fe27e772SPawel Jakub Dawidek } 196e35d3a78SPawel Jakub Dawidek 197fe27e772SPawel Jakub Dawidek bp->bio_driver1 = (void *)sc->sc_seq; 198fe27e772SPawel Jakub Dawidek sc->sc_seq++; 199e35d3a78SPawel Jakub Dawidek sc->sc_queue_count++; 200fe27e772SPawel Jakub Dawidek 201662a4e58SPawel Jakub Dawidek bioq_insert_tail(&sc->sc_inqueue, bp); 202fe27e772SPawel Jakub Dawidek wakeup(sc); 203e35d3a78SPawel Jakub Dawidek 204e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 205fe27e772SPawel Jakub Dawidek } 206fe27e772SPawel Jakub Dawidek 207e08ec037SPawel Jakub Dawidek static void 208e08ec037SPawel Jakub Dawidek g_gate_done(struct bio *cbp) 209e08ec037SPawel Jakub Dawidek { 210e08ec037SPawel Jakub Dawidek struct bio *pbp; 211e08ec037SPawel Jakub Dawidek 212e08ec037SPawel Jakub Dawidek pbp = cbp->bio_parent; 213e08ec037SPawel Jakub Dawidek if (cbp->bio_error == 0) { 214e08ec037SPawel Jakub Dawidek pbp->bio_completed = cbp->bio_completed; 215e08ec037SPawel Jakub Dawidek g_destroy_bio(cbp); 216e08ec037SPawel Jakub Dawidek pbp->bio_inbed++; 217e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, 0); 218e08ec037SPawel Jakub Dawidek } else { 219e08ec037SPawel Jakub Dawidek /* If direct read failed, pass it through userland daemon. */ 220e08ec037SPawel Jakub Dawidek g_destroy_bio(cbp); 221e08ec037SPawel Jakub Dawidek pbp->bio_children--; 222e08ec037SPawel Jakub Dawidek g_gate_queue_io(pbp); 223e08ec037SPawel Jakub Dawidek } 224e08ec037SPawel Jakub Dawidek } 225e08ec037SPawel Jakub Dawidek 226e08ec037SPawel Jakub Dawidek static void 227e08ec037SPawel Jakub Dawidek g_gate_start(struct bio *pbp) 228e08ec037SPawel Jakub Dawidek { 229e08ec037SPawel Jakub Dawidek struct g_gate_softc *sc; 230e08ec037SPawel Jakub Dawidek 231e08ec037SPawel Jakub Dawidek sc = pbp->bio_to->geom->softc; 232e08ec037SPawel Jakub Dawidek if (sc == NULL || (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 233e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, ENXIO); 234e08ec037SPawel Jakub Dawidek return; 235e08ec037SPawel Jakub Dawidek } 236e08ec037SPawel Jakub Dawidek G_GATE_LOGREQ(2, pbp, "Request received."); 237e08ec037SPawel Jakub Dawidek switch (pbp->bio_cmd) { 238e08ec037SPawel Jakub Dawidek case BIO_READ: 239e08ec037SPawel Jakub Dawidek if (sc->sc_readcons != NULL) { 240e08ec037SPawel Jakub Dawidek struct bio *cbp; 241e08ec037SPawel Jakub Dawidek 242e08ec037SPawel Jakub Dawidek cbp = g_clone_bio(pbp); 243e08ec037SPawel Jakub Dawidek if (cbp == NULL) { 244e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, ENOMEM); 245e08ec037SPawel Jakub Dawidek return; 246e08ec037SPawel Jakub Dawidek } 247e08ec037SPawel Jakub Dawidek cbp->bio_done = g_gate_done; 248e08ec037SPawel Jakub Dawidek cbp->bio_offset = pbp->bio_offset + sc->sc_readoffset; 249e08ec037SPawel Jakub Dawidek cbp->bio_to = sc->sc_readcons->provider; 250e08ec037SPawel Jakub Dawidek g_io_request(cbp, sc->sc_readcons); 251e08ec037SPawel Jakub Dawidek return; 252e08ec037SPawel Jakub Dawidek } 253e08ec037SPawel Jakub Dawidek break; 254e08ec037SPawel Jakub Dawidek case BIO_DELETE: 255e08ec037SPawel Jakub Dawidek case BIO_WRITE: 256e08ec037SPawel Jakub Dawidek case BIO_FLUSH: 257e08ec037SPawel Jakub Dawidek /* XXX: Hack to allow read-only mounts. */ 258e08ec037SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) { 259e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, EPERM); 260e08ec037SPawel Jakub Dawidek return; 261e08ec037SPawel Jakub Dawidek } 262e08ec037SPawel Jakub Dawidek break; 263e08ec037SPawel Jakub Dawidek case BIO_GETATTR: 264e08ec037SPawel Jakub Dawidek default: 265e08ec037SPawel Jakub Dawidek G_GATE_LOGREQ(2, pbp, "Ignoring request."); 266e08ec037SPawel Jakub Dawidek g_io_deliver(pbp, EOPNOTSUPP); 267e08ec037SPawel Jakub Dawidek return; 268e08ec037SPawel Jakub Dawidek } 269e08ec037SPawel Jakub Dawidek 270e08ec037SPawel Jakub Dawidek g_gate_queue_io(pbp); 271e08ec037SPawel Jakub Dawidek } 272e08ec037SPawel Jakub Dawidek 273fe27e772SPawel Jakub Dawidek static struct g_gate_softc * 2742aa15ffdSPawel Jakub Dawidek g_gate_hold(int unit, const char *name) 275fe27e772SPawel Jakub Dawidek { 27632115b10SPawel Jakub Dawidek struct g_gate_softc *sc = NULL; 277fe27e772SPawel Jakub Dawidek 27832115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 27932115b10SPawel Jakub Dawidek if (unit >= 0 && unit < g_gate_maxunits) 28032115b10SPawel Jakub Dawidek sc = g_gate_units[unit]; 28132115b10SPawel Jakub Dawidek else if (unit == G_GATE_NAME_GIVEN) { 28232115b10SPawel Jakub Dawidek KASSERT(name != NULL, ("name is NULL")); 28332115b10SPawel Jakub Dawidek for (unit = 0; unit < g_gate_maxunits; unit++) { 28432115b10SPawel Jakub Dawidek if (g_gate_units[unit] == NULL) 28532115b10SPawel Jakub Dawidek continue; 28632115b10SPawel Jakub Dawidek if (strcmp(name, 28732115b10SPawel Jakub Dawidek g_gate_units[unit]->sc_provider->name) != 0) { 28832115b10SPawel Jakub Dawidek continue; 28932115b10SPawel Jakub Dawidek } 29032115b10SPawel Jakub Dawidek sc = g_gate_units[unit]; 2917ffb6e0fSPawel Jakub Dawidek break; 292fe27e772SPawel Jakub Dawidek } 29332115b10SPawel Jakub Dawidek } 2947ffb6e0fSPawel Jakub Dawidek if (sc != NULL) 2957ffb6e0fSPawel Jakub Dawidek sc->sc_ref++; 29632115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 297fe27e772SPawel Jakub Dawidek return (sc); 298fe27e772SPawel Jakub Dawidek } 299fe27e772SPawel Jakub Dawidek 300fe27e772SPawel Jakub Dawidek static void 301fe27e772SPawel Jakub Dawidek g_gate_release(struct g_gate_softc *sc) 302fe27e772SPawel Jakub Dawidek { 303fe27e772SPawel Jakub Dawidek 304fe27e772SPawel Jakub Dawidek g_topology_assert_not(); 30532115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 306fe27e772SPawel Jakub Dawidek sc->sc_ref--; 307fe27e772SPawel Jakub Dawidek KASSERT(sc->sc_ref >= 0, ("Negative sc_ref for %s.", sc->sc_name)); 30832115b10SPawel Jakub Dawidek if (sc->sc_ref == 0 && (sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) 3097ffb6e0fSPawel Jakub Dawidek wakeup(&sc->sc_ref); 31032115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 311fe27e772SPawel Jakub Dawidek } 312fe27e772SPawel Jakub Dawidek 313fe27e772SPawel Jakub Dawidek static int 31432115b10SPawel Jakub Dawidek g_gate_getunit(int unit, int *errorp) 315fe27e772SPawel Jakub Dawidek { 316fe27e772SPawel Jakub Dawidek 31732115b10SPawel Jakub Dawidek mtx_assert(&g_gate_units_lock, MA_OWNED); 318fe27e772SPawel Jakub Dawidek if (unit >= 0) { 31932115b10SPawel Jakub Dawidek if (unit >= g_gate_maxunits) 32032115b10SPawel Jakub Dawidek *errorp = EINVAL; 32132115b10SPawel Jakub Dawidek else if (g_gate_units[unit] == NULL) 322fe27e772SPawel Jakub Dawidek return (unit); 32332115b10SPawel Jakub Dawidek else 32432115b10SPawel Jakub Dawidek *errorp = EEXIST; 32532115b10SPawel Jakub Dawidek } else { 32632115b10SPawel Jakub Dawidek for (unit = 0; unit < g_gate_maxunits; unit++) { 32732115b10SPawel Jakub Dawidek if (g_gate_units[unit] == NULL) 32832115b10SPawel Jakub Dawidek return (unit); 32932115b10SPawel Jakub Dawidek } 33032115b10SPawel Jakub Dawidek *errorp = ENFILE; 33132115b10SPawel Jakub Dawidek } 33232115b10SPawel Jakub Dawidek return (-1); 333fe27e772SPawel Jakub Dawidek } 334fe27e772SPawel Jakub Dawidek 335fe27e772SPawel Jakub Dawidek static void 336fe27e772SPawel Jakub Dawidek g_gate_guard(void *arg) 337fe27e772SPawel Jakub Dawidek { 33840ea77a0SAlexander Motin struct bio_queue_head queue; 339fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 340fe27e772SPawel Jakub Dawidek struct bintime curtime; 341fe27e772SPawel Jakub Dawidek struct bio *bp, *bp2; 342fe27e772SPawel Jakub Dawidek 343fe27e772SPawel Jakub Dawidek sc = arg; 344fe27e772SPawel Jakub Dawidek binuptime(&curtime); 34532115b10SPawel Jakub Dawidek g_gate_hold(sc->sc_unit, NULL); 34640ea77a0SAlexander Motin bioq_init(&queue); 347e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 348fe27e772SPawel Jakub Dawidek TAILQ_FOREACH_SAFE(bp, &sc->sc_inqueue.queue, bio_queue, bp2) { 349fe27e772SPawel Jakub Dawidek if (curtime.sec - bp->bio_t0.sec < 5) 350fe27e772SPawel Jakub Dawidek continue; 351fe27e772SPawel Jakub Dawidek bioq_remove(&sc->sc_inqueue, bp); 352e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 35340ea77a0SAlexander Motin bioq_insert_tail(&queue, bp); 354fe27e772SPawel Jakub Dawidek } 355fe27e772SPawel Jakub Dawidek TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, bp2) { 356fe27e772SPawel Jakub Dawidek if (curtime.sec - bp->bio_t0.sec < 5) 357fe27e772SPawel Jakub Dawidek continue; 358fe27e772SPawel Jakub Dawidek bioq_remove(&sc->sc_outqueue, bp); 359e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 36040ea77a0SAlexander Motin bioq_insert_tail(&queue, bp); 36140ea77a0SAlexander Motin } 36240ea77a0SAlexander Motin mtx_unlock(&sc->sc_queue_mtx); 36340ea77a0SAlexander Motin while ((bp = bioq_takefirst(&queue)) != NULL) { 364fe27e772SPawel Jakub Dawidek G_GATE_LOGREQ(1, bp, "Request timeout."); 365fe27e772SPawel Jakub Dawidek g_io_deliver(bp, EIO); 366fe27e772SPawel Jakub Dawidek } 367fe27e772SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_DESTROY) == 0) { 368fe27e772SPawel Jakub Dawidek callout_reset(&sc->sc_callout, sc->sc_timeout * hz, 369fe27e772SPawel Jakub Dawidek g_gate_guard, sc); 370fe27e772SPawel Jakub Dawidek } 371fe27e772SPawel Jakub Dawidek g_gate_release(sc); 372fe27e772SPawel Jakub Dawidek } 373fe27e772SPawel Jakub Dawidek 374fe27e772SPawel Jakub Dawidek static void 375e08ec037SPawel Jakub Dawidek g_gate_orphan(struct g_consumer *cp) 376e08ec037SPawel Jakub Dawidek { 377e08ec037SPawel Jakub Dawidek struct g_gate_softc *sc; 378e08ec037SPawel Jakub Dawidek struct g_geom *gp; 379e08ec037SPawel Jakub Dawidek 380e08ec037SPawel Jakub Dawidek g_topology_assert(); 381e08ec037SPawel Jakub Dawidek gp = cp->geom; 382e08ec037SPawel Jakub Dawidek sc = gp->softc; 383e08ec037SPawel Jakub Dawidek if (sc == NULL) 384e08ec037SPawel Jakub Dawidek return; 385e08ec037SPawel Jakub Dawidek KASSERT(cp == sc->sc_readcons, ("cp=%p sc_readcons=%p", cp, 386e08ec037SPawel Jakub Dawidek sc->sc_readcons)); 387e08ec037SPawel Jakub Dawidek sc->sc_readcons = NULL; 388e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Destroying read consumer on provider %s orphan.", 389e08ec037SPawel Jakub Dawidek cp->provider->name); 390e08ec037SPawel Jakub Dawidek (void)g_access(cp, -1, 0, 0); 391e08ec037SPawel Jakub Dawidek g_detach(cp); 392e08ec037SPawel Jakub Dawidek g_destroy_consumer(cp); 393e08ec037SPawel Jakub Dawidek } 394e08ec037SPawel Jakub Dawidek 395e08ec037SPawel Jakub Dawidek static void 396fe27e772SPawel Jakub Dawidek g_gate_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 397fe27e772SPawel Jakub Dawidek struct g_consumer *cp, struct g_provider *pp) 398fe27e772SPawel Jakub Dawidek { 399fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 400fe27e772SPawel Jakub Dawidek 401fe27e772SPawel Jakub Dawidek sc = gp->softc; 402fe27e772SPawel Jakub Dawidek if (sc == NULL || pp != NULL || cp != NULL) 403fe27e772SPawel Jakub Dawidek return; 4041d9db37cSMikolaj Golub sc = g_gate_hold(sc->sc_unit, NULL); 4051d9db37cSMikolaj Golub if (sc == NULL) 4061d9db37cSMikolaj Golub return; 407fe27e772SPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_READONLY) != 0) { 408fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<access>%s</access>\n", indent, "read-only"); 409fe27e772SPawel Jakub Dawidek } else if ((sc->sc_flags & G_GATE_FLAG_WRITEONLY) != 0) { 410fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<access>%s</access>\n", indent, 411fe27e772SPawel Jakub Dawidek "write-only"); 412fe27e772SPawel Jakub Dawidek } else { 413fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<access>%s</access>\n", indent, 414fe27e772SPawel Jakub Dawidek "read-write"); 415fe27e772SPawel Jakub Dawidek } 416e08ec037SPawel Jakub Dawidek if (sc->sc_readcons != NULL) { 417e08ec037SPawel Jakub Dawidek sbuf_printf(sb, "%s<read_offset>%jd</read_offset>\n", 418e08ec037SPawel Jakub Dawidek indent, (intmax_t)sc->sc_readoffset); 419e08ec037SPawel Jakub Dawidek sbuf_printf(sb, "%s<read_provider>%s</read_provider>\n", 420e08ec037SPawel Jakub Dawidek indent, sc->sc_readcons->provider->name); 421e08ec037SPawel Jakub Dawidek } 422fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<timeout>%u</timeout>\n", indent, sc->sc_timeout); 423fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<info>%s</info>\n", indent, sc->sc_info); 424fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<queue_count>%u</queue_count>\n", indent, 425fe27e772SPawel Jakub Dawidek sc->sc_queue_count); 426fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<queue_size>%u</queue_size>\n", indent, 427fe27e772SPawel Jakub Dawidek sc->sc_queue_size); 428fe27e772SPawel Jakub Dawidek sbuf_printf(sb, "%s<ref>%u</ref>\n", indent, sc->sc_ref); 42932115b10SPawel Jakub Dawidek sbuf_printf(sb, "%s<unit>%d</unit>\n", indent, sc->sc_unit); 43047f44cb7SPawel Jakub Dawidek g_topology_unlock(); 431fe27e772SPawel Jakub Dawidek g_gate_release(sc); 43247f44cb7SPawel Jakub Dawidek g_topology_lock(); 433fe27e772SPawel Jakub Dawidek } 434fe27e772SPawel Jakub Dawidek 435fe27e772SPawel Jakub Dawidek static int 436fe27e772SPawel Jakub Dawidek g_gate_create(struct g_gate_ctl_create *ggio) 437fe27e772SPawel Jakub Dawidek { 438fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 439fe27e772SPawel Jakub Dawidek struct g_geom *gp; 440e08ec037SPawel Jakub Dawidek struct g_provider *pp, *ropp; 441e08ec037SPawel Jakub Dawidek struct g_consumer *cp; 44232115b10SPawel Jakub Dawidek char name[NAME_MAX]; 44332115b10SPawel Jakub Dawidek int error = 0, unit; 444fe27e772SPawel Jakub Dawidek 445e08ec037SPawel Jakub Dawidek if (ggio->gctl_mediasize <= 0) { 446fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid media size."); 447fe27e772SPawel Jakub Dawidek return (EINVAL); 448fe27e772SPawel Jakub Dawidek } 449e08ec037SPawel Jakub Dawidek if (ggio->gctl_sectorsize <= 0) { 450e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid sector size."); 451e08ec037SPawel Jakub Dawidek return (EINVAL); 452e08ec037SPawel Jakub Dawidek } 453e08ec037SPawel Jakub Dawidek if (!powerof2(ggio->gctl_sectorsize)) { 454fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid sector size."); 455fe27e772SPawel Jakub Dawidek return (EINVAL); 456fe27e772SPawel Jakub Dawidek } 457662a4e58SPawel Jakub Dawidek if ((ggio->gctl_mediasize % ggio->gctl_sectorsize) != 0) { 458662a4e58SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid media size."); 459662a4e58SPawel Jakub Dawidek return (EINVAL); 460662a4e58SPawel Jakub Dawidek } 461fe27e772SPawel Jakub Dawidek if ((ggio->gctl_flags & G_GATE_FLAG_READONLY) != 0 && 462fe27e772SPawel Jakub Dawidek (ggio->gctl_flags & G_GATE_FLAG_WRITEONLY) != 0) { 463fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid flags."); 464fe27e772SPawel Jakub Dawidek return (EINVAL); 465fe27e772SPawel Jakub Dawidek } 46632115b10SPawel Jakub Dawidek if (ggio->gctl_unit != G_GATE_UNIT_AUTO && 46732115b10SPawel Jakub Dawidek ggio->gctl_unit != G_GATE_NAME_GIVEN && 46832115b10SPawel Jakub Dawidek ggio->gctl_unit < 0) { 469fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid unit number."); 470fe27e772SPawel Jakub Dawidek return (EINVAL); 471fe27e772SPawel Jakub Dawidek } 47232115b10SPawel Jakub Dawidek if (ggio->gctl_unit == G_GATE_NAME_GIVEN && 47332115b10SPawel Jakub Dawidek ggio->gctl_name[0] == '\0') { 47432115b10SPawel Jakub Dawidek G_GATE_DEBUG(1, "No device name."); 47532115b10SPawel Jakub Dawidek return (EINVAL); 47632115b10SPawel Jakub Dawidek } 477fe27e772SPawel Jakub Dawidek 478fe27e772SPawel Jakub Dawidek sc = malloc(sizeof(*sc), M_GATE, M_WAITOK | M_ZERO); 479fe27e772SPawel Jakub Dawidek sc->sc_flags = (ggio->gctl_flags & G_GATE_USERFLAGS); 480fe27e772SPawel Jakub Dawidek strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info)); 48132115b10SPawel Jakub Dawidek sc->sc_seq = 1; 482fe27e772SPawel Jakub Dawidek bioq_init(&sc->sc_inqueue); 483fe27e772SPawel Jakub Dawidek bioq_init(&sc->sc_outqueue); 484e35d3a78SPawel Jakub Dawidek mtx_init(&sc->sc_queue_mtx, "gg:queue", NULL, MTX_DEF); 485fe27e772SPawel Jakub Dawidek sc->sc_queue_count = 0; 486fe27e772SPawel Jakub Dawidek sc->sc_queue_size = ggio->gctl_maxcount; 487fe27e772SPawel Jakub Dawidek if (sc->sc_queue_size > G_GATE_MAX_QUEUE_SIZE) 488fe27e772SPawel Jakub Dawidek sc->sc_queue_size = G_GATE_MAX_QUEUE_SIZE; 489fe27e772SPawel Jakub Dawidek sc->sc_timeout = ggio->gctl_timeout; 490fd90e2edSJung-uk Kim callout_init(&sc->sc_callout, 1); 491e08ec037SPawel Jakub Dawidek 49232115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 49332115b10SPawel Jakub Dawidek sc->sc_unit = g_gate_getunit(ggio->gctl_unit, &error); 494a277f47bSMikolaj Golub if (sc->sc_unit < 0) 495a277f47bSMikolaj Golub goto fail1; 49632115b10SPawel Jakub Dawidek if (ggio->gctl_unit == G_GATE_NAME_GIVEN) 49732115b10SPawel Jakub Dawidek snprintf(name, sizeof(name), "%s", ggio->gctl_name); 49832115b10SPawel Jakub Dawidek else { 49932115b10SPawel Jakub Dawidek snprintf(name, sizeof(name), "%s%d", G_GATE_PROVIDER_NAME, 50032115b10SPawel Jakub Dawidek sc->sc_unit); 50132115b10SPawel Jakub Dawidek } 50232115b10SPawel Jakub Dawidek /* Check for name collision. */ 50332115b10SPawel Jakub Dawidek for (unit = 0; unit < g_gate_maxunits; unit++) { 50432115b10SPawel Jakub Dawidek if (g_gate_units[unit] == NULL) 50532115b10SPawel Jakub Dawidek continue; 506baf63f65SMikolaj Golub if (strcmp(name, g_gate_units[unit]->sc_name) != 0) 50732115b10SPawel Jakub Dawidek continue; 508a277f47bSMikolaj Golub error = EEXIST; 509a277f47bSMikolaj Golub goto fail1; 51032115b10SPawel Jakub Dawidek } 511baf63f65SMikolaj Golub sc->sc_name = name; 51232115b10SPawel Jakub Dawidek g_gate_units[sc->sc_unit] = sc; 51332115b10SPawel Jakub Dawidek g_gate_nunits++; 51432115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 51532115b10SPawel Jakub Dawidek 516a277f47bSMikolaj Golub g_topology_lock(); 517a277f47bSMikolaj Golub 518a277f47bSMikolaj Golub if (ggio->gctl_readprov[0] == '\0') { 519a277f47bSMikolaj Golub ropp = NULL; 520a277f47bSMikolaj Golub } else { 521a277f47bSMikolaj Golub ropp = g_provider_by_name(ggio->gctl_readprov); 522a277f47bSMikolaj Golub if (ropp == NULL) { 523a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Provider %s doesn't exist.", 524a277f47bSMikolaj Golub ggio->gctl_readprov); 525a277f47bSMikolaj Golub error = EINVAL; 526a277f47bSMikolaj Golub goto fail2; 527a277f47bSMikolaj Golub } 528a277f47bSMikolaj Golub if ((ggio->gctl_readoffset % ggio->gctl_sectorsize) != 0) { 529a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Invalid read offset."); 530a277f47bSMikolaj Golub error = EINVAL; 531a277f47bSMikolaj Golub goto fail2; 532a277f47bSMikolaj Golub } 533a277f47bSMikolaj Golub if (ggio->gctl_mediasize + ggio->gctl_readoffset > 534a277f47bSMikolaj Golub ropp->mediasize) { 535a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Invalid read offset or media size."); 536a277f47bSMikolaj Golub error = EINVAL; 537a277f47bSMikolaj Golub goto fail2; 538a277f47bSMikolaj Golub } 539a277f47bSMikolaj Golub } 540a277f47bSMikolaj Golub 541a277f47bSMikolaj Golub gp = g_new_geomf(&g_gate_class, "%s", name); 542a277f47bSMikolaj Golub gp->start = g_gate_start; 543a277f47bSMikolaj Golub gp->access = g_gate_access; 544a277f47bSMikolaj Golub gp->orphan = g_gate_orphan; 545a277f47bSMikolaj Golub gp->dumpconf = g_gate_dumpconf; 546a277f47bSMikolaj Golub gp->softc = sc; 547a277f47bSMikolaj Golub 548a277f47bSMikolaj Golub if (ropp != NULL) { 549a277f47bSMikolaj Golub cp = g_new_consumer(gp); 55040ea77a0SAlexander Motin cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 551a277f47bSMikolaj Golub error = g_attach(cp, ropp); 552a277f47bSMikolaj Golub if (error != 0) { 553a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Unable to attach to %s.", ropp->name); 554a277f47bSMikolaj Golub goto fail3; 555a277f47bSMikolaj Golub } 556a277f47bSMikolaj Golub error = g_access(cp, 1, 0, 0); 557a277f47bSMikolaj Golub if (error != 0) { 558a277f47bSMikolaj Golub G_GATE_DEBUG(1, "Unable to access %s.", ropp->name); 559a277f47bSMikolaj Golub g_detach(cp); 560a277f47bSMikolaj Golub goto fail3; 561a277f47bSMikolaj Golub } 562a277f47bSMikolaj Golub sc->sc_readcons = cp; 563a277f47bSMikolaj Golub sc->sc_readoffset = ggio->gctl_readoffset; 564a277f47bSMikolaj Golub } 565a277f47bSMikolaj Golub 56632115b10SPawel Jakub Dawidek ggio->gctl_unit = sc->sc_unit; 567fe27e772SPawel Jakub Dawidek 56832115b10SPawel Jakub Dawidek pp = g_new_providerf(gp, "%s", name); 56940ea77a0SAlexander Motin pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 570fe27e772SPawel Jakub Dawidek pp->mediasize = ggio->gctl_mediasize; 571fe27e772SPawel Jakub Dawidek pp->sectorsize = ggio->gctl_sectorsize; 572fe27e772SPawel Jakub Dawidek sc->sc_provider = pp; 573fe27e772SPawel Jakub Dawidek g_error_provider(pp, 0); 574e08ec037SPawel Jakub Dawidek 575fe27e772SPawel Jakub Dawidek g_topology_unlock(); 576baf63f65SMikolaj Golub mtx_lock(&g_gate_units_lock); 577baf63f65SMikolaj Golub sc->sc_name = sc->sc_provider->name; 578baf63f65SMikolaj Golub mtx_unlock(&g_gate_units_lock); 579bd119384SMikolaj Golub G_GATE_DEBUG(1, "Device %s created.", gp->name); 580fe27e772SPawel Jakub Dawidek 581fe27e772SPawel Jakub Dawidek if (sc->sc_timeout > 0) { 582fe27e772SPawel Jakub Dawidek callout_reset(&sc->sc_callout, sc->sc_timeout * hz, 583fe27e772SPawel Jakub Dawidek g_gate_guard, sc); 584fe27e772SPawel Jakub Dawidek } 585fe27e772SPawel Jakub Dawidek return (0); 586a277f47bSMikolaj Golub fail3: 587a277f47bSMikolaj Golub g_destroy_consumer(cp); 588a277f47bSMikolaj Golub g_destroy_geom(gp); 589a277f47bSMikolaj Golub fail2: 590a277f47bSMikolaj Golub g_topology_unlock(); 591a277f47bSMikolaj Golub mtx_lock(&g_gate_units_lock); 592a277f47bSMikolaj Golub g_gate_units[sc->sc_unit] = NULL; 593a277f47bSMikolaj Golub KASSERT(g_gate_nunits > 0, ("negative g_gate_nunits?")); 594a277f47bSMikolaj Golub g_gate_nunits--; 595a277f47bSMikolaj Golub fail1: 596a277f47bSMikolaj Golub mtx_unlock(&g_gate_units_lock); 597a277f47bSMikolaj Golub mtx_destroy(&sc->sc_queue_mtx); 598a277f47bSMikolaj Golub free(sc, M_GATE); 599a277f47bSMikolaj Golub return (error); 600fe27e772SPawel Jakub Dawidek } 601fe27e772SPawel Jakub Dawidek 602e08ec037SPawel Jakub Dawidek static int 603e08ec037SPawel Jakub Dawidek g_gate_modify(struct g_gate_softc *sc, struct g_gate_ctl_modify *ggio) 604e08ec037SPawel Jakub Dawidek { 605e08ec037SPawel Jakub Dawidek struct g_provider *pp; 606e08ec037SPawel Jakub Dawidek struct g_consumer *cp; 607e08ec037SPawel Jakub Dawidek int error; 608e08ec037SPawel Jakub Dawidek 609e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_MEDIASIZE) != 0) { 610e08ec037SPawel Jakub Dawidek if (ggio->gctl_mediasize <= 0) { 611e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid media size."); 612e08ec037SPawel Jakub Dawidek return (EINVAL); 613e08ec037SPawel Jakub Dawidek } 614e08ec037SPawel Jakub Dawidek pp = sc->sc_provider; 615e08ec037SPawel Jakub Dawidek if ((ggio->gctl_mediasize % pp->sectorsize) != 0) { 616e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid media size."); 617e08ec037SPawel Jakub Dawidek return (EINVAL); 618e08ec037SPawel Jakub Dawidek } 619*874774c5SMikolaj Golub g_resize_provider(pp, ggio->gctl_mediasize); 620*874774c5SMikolaj Golub return (0); 621e08ec037SPawel Jakub Dawidek } 622e08ec037SPawel Jakub Dawidek 623e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_INFO) != 0) 624e08ec037SPawel Jakub Dawidek (void)strlcpy(sc->sc_info, ggio->gctl_info, sizeof(sc->sc_info)); 625e08ec037SPawel Jakub Dawidek 626e08ec037SPawel Jakub Dawidek cp = NULL; 627e08ec037SPawel Jakub Dawidek 628e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) { 629e08ec037SPawel Jakub Dawidek g_topology_lock(); 630e08ec037SPawel Jakub Dawidek if (sc->sc_readcons != NULL) { 631e08ec037SPawel Jakub Dawidek cp = sc->sc_readcons; 632e08ec037SPawel Jakub Dawidek sc->sc_readcons = NULL; 633e08ec037SPawel Jakub Dawidek (void)g_access(cp, -1, 0, 0); 634e08ec037SPawel Jakub Dawidek g_detach(cp); 635e08ec037SPawel Jakub Dawidek g_destroy_consumer(cp); 636e08ec037SPawel Jakub Dawidek } 637e08ec037SPawel Jakub Dawidek if (ggio->gctl_readprov[0] != '\0') { 638e08ec037SPawel Jakub Dawidek pp = g_provider_by_name(ggio->gctl_readprov); 639e08ec037SPawel Jakub Dawidek if (pp == NULL) { 640e08ec037SPawel Jakub Dawidek g_topology_unlock(); 641e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Provider %s doesn't exist.", 642e08ec037SPawel Jakub Dawidek ggio->gctl_readprov); 643e08ec037SPawel Jakub Dawidek return (EINVAL); 644e08ec037SPawel Jakub Dawidek } 645e08ec037SPawel Jakub Dawidek cp = g_new_consumer(sc->sc_provider->geom); 64640ea77a0SAlexander Motin cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 647e08ec037SPawel Jakub Dawidek error = g_attach(cp, pp); 648e08ec037SPawel Jakub Dawidek if (error != 0) { 649e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Unable to attach to %s.", 650e08ec037SPawel Jakub Dawidek pp->name); 651e08ec037SPawel Jakub Dawidek } else { 652e08ec037SPawel Jakub Dawidek error = g_access(cp, 1, 0, 0); 653e08ec037SPawel Jakub Dawidek if (error != 0) { 654e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Unable to access %s.", 655e08ec037SPawel Jakub Dawidek pp->name); 656e08ec037SPawel Jakub Dawidek g_detach(cp); 657e08ec037SPawel Jakub Dawidek } 658e08ec037SPawel Jakub Dawidek } 659e08ec037SPawel Jakub Dawidek if (error != 0) { 660e08ec037SPawel Jakub Dawidek g_destroy_consumer(cp); 661e08ec037SPawel Jakub Dawidek g_topology_unlock(); 662e08ec037SPawel Jakub Dawidek return (error); 663e08ec037SPawel Jakub Dawidek } 664e08ec037SPawel Jakub Dawidek } 665e08ec037SPawel Jakub Dawidek } else { 666e08ec037SPawel Jakub Dawidek cp = sc->sc_readcons; 667e08ec037SPawel Jakub Dawidek } 668e08ec037SPawel Jakub Dawidek 669e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_READOFFSET) != 0) { 670e08ec037SPawel Jakub Dawidek if (cp == NULL) { 671e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "No read provider."); 672e08ec037SPawel Jakub Dawidek return (EINVAL); 673e08ec037SPawel Jakub Dawidek } 674e08ec037SPawel Jakub Dawidek pp = sc->sc_provider; 675e08ec037SPawel Jakub Dawidek if ((ggio->gctl_readoffset % pp->sectorsize) != 0) { 676e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid read offset."); 677e08ec037SPawel Jakub Dawidek return (EINVAL); 678e08ec037SPawel Jakub Dawidek } 679e08ec037SPawel Jakub Dawidek if (pp->mediasize + ggio->gctl_readoffset > 680e08ec037SPawel Jakub Dawidek cp->provider->mediasize) { 681e08ec037SPawel Jakub Dawidek G_GATE_DEBUG(1, "Invalid read offset or media size."); 682e08ec037SPawel Jakub Dawidek return (EINVAL); 683e08ec037SPawel Jakub Dawidek } 684e08ec037SPawel Jakub Dawidek sc->sc_readoffset = ggio->gctl_readoffset; 685e08ec037SPawel Jakub Dawidek } 686e08ec037SPawel Jakub Dawidek 687e08ec037SPawel Jakub Dawidek if ((ggio->gctl_modify & GG_MODIFY_READPROV) != 0) { 688e08ec037SPawel Jakub Dawidek sc->sc_readcons = cp; 689e08ec037SPawel Jakub Dawidek g_topology_unlock(); 690e08ec037SPawel Jakub Dawidek } 691e08ec037SPawel Jakub Dawidek 692e08ec037SPawel Jakub Dawidek return (0); 693e08ec037SPawel Jakub Dawidek } 694e08ec037SPawel Jakub Dawidek 695fe27e772SPawel Jakub Dawidek #define G_GATE_CHECK_VERSION(ggio) do { \ 69684436f14SPawel Jakub Dawidek if ((ggio)->gctl_version != G_GATE_VERSION) { \ 69784436f14SPawel Jakub Dawidek printf("Version mismatch %d != %d.\n", \ 69884436f14SPawel Jakub Dawidek ggio->gctl_version, G_GATE_VERSION); \ 699fe27e772SPawel Jakub Dawidek return (EINVAL); \ 70084436f14SPawel Jakub Dawidek } \ 701fe27e772SPawel Jakub Dawidek } while (0) 702fe27e772SPawel Jakub Dawidek static int 70389c9c53dSPoul-Henning Kamp g_gate_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags, struct thread *td) 704fe27e772SPawel Jakub Dawidek { 705fe27e772SPawel Jakub Dawidek struct g_gate_softc *sc; 706fe27e772SPawel Jakub Dawidek struct bio *bp; 707fe27e772SPawel Jakub Dawidek int error = 0; 708fe27e772SPawel Jakub Dawidek 709fe27e772SPawel Jakub Dawidek G_GATE_DEBUG(4, "ioctl(%s, %lx, %p, %x, %p)", devtoname(dev), cmd, addr, 710fe27e772SPawel Jakub Dawidek flags, td); 711fe27e772SPawel Jakub Dawidek 712fe27e772SPawel Jakub Dawidek switch (cmd) { 713fe27e772SPawel Jakub Dawidek case G_GATE_CMD_CREATE: 714fe27e772SPawel Jakub Dawidek { 715fe27e772SPawel Jakub Dawidek struct g_gate_ctl_create *ggio = (void *)addr; 716fe27e772SPawel Jakub Dawidek 717fe27e772SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 718a17dd95fSPawel Jakub Dawidek error = g_gate_create(ggio); 719f9065812SPawel Jakub Dawidek /* 720f9065812SPawel Jakub Dawidek * Reset TDP_GEOM flag. 721f9065812SPawel Jakub Dawidek * There are pending events for sure, because we just created 722f9065812SPawel Jakub Dawidek * new provider and other classes want to taste it, but we 723f9065812SPawel Jakub Dawidek * cannot answer on I/O requests until we're here. 724f9065812SPawel Jakub Dawidek */ 725f9065812SPawel Jakub Dawidek td->td_pflags &= ~TDP_GEOM; 726a17dd95fSPawel Jakub Dawidek return (error); 727fe27e772SPawel Jakub Dawidek } 728e08ec037SPawel Jakub Dawidek case G_GATE_CMD_MODIFY: 729e08ec037SPawel Jakub Dawidek { 730e08ec037SPawel Jakub Dawidek struct g_gate_ctl_modify *ggio = (void *)addr; 731e08ec037SPawel Jakub Dawidek 732e08ec037SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 733e08ec037SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, NULL); 734e08ec037SPawel Jakub Dawidek if (sc == NULL) 735e08ec037SPawel Jakub Dawidek return (ENXIO); 736e08ec037SPawel Jakub Dawidek error = g_gate_modify(sc, ggio); 737e08ec037SPawel Jakub Dawidek g_gate_release(sc); 738e08ec037SPawel Jakub Dawidek return (error); 739e08ec037SPawel Jakub Dawidek } 740fe27e772SPawel Jakub Dawidek case G_GATE_CMD_DESTROY: 741fe27e772SPawel Jakub Dawidek { 742fe27e772SPawel Jakub Dawidek struct g_gate_ctl_destroy *ggio = (void *)addr; 743fe27e772SPawel Jakub Dawidek 744fe27e772SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 74532115b10SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name); 746fe27e772SPawel Jakub Dawidek if (sc == NULL) 747fe27e772SPawel Jakub Dawidek return (ENXIO); 748fe27e772SPawel Jakub Dawidek g_topology_lock(); 74932115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 750fe27e772SPawel Jakub Dawidek error = g_gate_destroy(sc, ggio->gctl_force); 751fe27e772SPawel Jakub Dawidek g_topology_unlock(); 7527ffb6e0fSPawel Jakub Dawidek if (error != 0) 753fe27e772SPawel Jakub Dawidek g_gate_release(sc); 754fe27e772SPawel Jakub Dawidek return (error); 755fe27e772SPawel Jakub Dawidek } 75684436f14SPawel Jakub Dawidek case G_GATE_CMD_CANCEL: 75784436f14SPawel Jakub Dawidek { 75884436f14SPawel Jakub Dawidek struct g_gate_ctl_cancel *ggio = (void *)addr; 75984436f14SPawel Jakub Dawidek struct bio *tbp, *lbp; 76084436f14SPawel Jakub Dawidek 76184436f14SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 76232115b10SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, ggio->gctl_name); 76384436f14SPawel Jakub Dawidek if (sc == NULL) 76484436f14SPawel Jakub Dawidek return (ENXIO); 76584436f14SPawel Jakub Dawidek lbp = NULL; 76684436f14SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 76784436f14SPawel Jakub Dawidek TAILQ_FOREACH_SAFE(bp, &sc->sc_outqueue.queue, bio_queue, tbp) { 76884436f14SPawel Jakub Dawidek if (ggio->gctl_seq == 0 || 76984436f14SPawel Jakub Dawidek ggio->gctl_seq == (uintptr_t)bp->bio_driver1) { 77084436f14SPawel Jakub Dawidek G_GATE_LOGREQ(1, bp, "Request canceled."); 77184436f14SPawel Jakub Dawidek bioq_remove(&sc->sc_outqueue, bp); 77284436f14SPawel Jakub Dawidek /* 77384436f14SPawel Jakub Dawidek * Be sure to put requests back onto incoming 77484436f14SPawel Jakub Dawidek * queue in the proper order. 77584436f14SPawel Jakub Dawidek */ 77684436f14SPawel Jakub Dawidek if (lbp == NULL) 77784436f14SPawel Jakub Dawidek bioq_insert_head(&sc->sc_inqueue, bp); 77884436f14SPawel Jakub Dawidek else { 77984436f14SPawel Jakub Dawidek TAILQ_INSERT_AFTER(&sc->sc_inqueue.queue, 78084436f14SPawel Jakub Dawidek lbp, bp, bio_queue); 78184436f14SPawel Jakub Dawidek } 78284436f14SPawel Jakub Dawidek lbp = bp; 78384436f14SPawel Jakub Dawidek /* 78484436f14SPawel Jakub Dawidek * If only one request was canceled, leave now. 78584436f14SPawel Jakub Dawidek */ 78684436f14SPawel Jakub Dawidek if (ggio->gctl_seq != 0) 78784436f14SPawel Jakub Dawidek break; 78884436f14SPawel Jakub Dawidek } 78984436f14SPawel Jakub Dawidek } 79032115b10SPawel Jakub Dawidek if (ggio->gctl_unit == G_GATE_NAME_GIVEN) 79132115b10SPawel Jakub Dawidek ggio->gctl_unit = sc->sc_unit; 79284436f14SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 79384436f14SPawel Jakub Dawidek g_gate_release(sc); 79484436f14SPawel Jakub Dawidek return (error); 79584436f14SPawel Jakub Dawidek } 796fe27e772SPawel Jakub Dawidek case G_GATE_CMD_START: 797fe27e772SPawel Jakub Dawidek { 798fe27e772SPawel Jakub Dawidek struct g_gate_ctl_io *ggio = (void *)addr; 799fe27e772SPawel Jakub Dawidek 800fe27e772SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 80132115b10SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, NULL); 802fe27e772SPawel Jakub Dawidek if (sc == NULL) 803fe27e772SPawel Jakub Dawidek return (ENXIO); 8047ffb6e0fSPawel Jakub Dawidek error = 0; 805fe27e772SPawel Jakub Dawidek for (;;) { 806e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 807fe27e772SPawel Jakub Dawidek bp = bioq_first(&sc->sc_inqueue); 808fe27e772SPawel Jakub Dawidek if (bp != NULL) 809fe27e772SPawel Jakub Dawidek break; 8107ffb6e0fSPawel Jakub Dawidek if ((sc->sc_flags & G_GATE_FLAG_DESTROY) != 0) { 8117ffb6e0fSPawel Jakub Dawidek ggio->gctl_error = ECANCELED; 8127ffb6e0fSPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 8137ffb6e0fSPawel Jakub Dawidek goto start_end; 8147ffb6e0fSPawel Jakub Dawidek } 815e35d3a78SPawel Jakub Dawidek if (msleep(sc, &sc->sc_queue_mtx, 816fe27e772SPawel Jakub Dawidek PPAUSE | PDROP | PCATCH, "ggwait", 0) != 0) { 817fe27e772SPawel Jakub Dawidek ggio->gctl_error = ECANCELED; 8187ffb6e0fSPawel Jakub Dawidek goto start_end; 819fe27e772SPawel Jakub Dawidek } 820fe27e772SPawel Jakub Dawidek } 8214d1e1bf3SPawel Jakub Dawidek ggio->gctl_cmd = bp->bio_cmd; 822c4d2d401SPawel Jakub Dawidek if (bp->bio_cmd == BIO_WRITE && 823fe27e772SPawel Jakub Dawidek bp->bio_length > ggio->gctl_length) { 824e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 825fe27e772SPawel Jakub Dawidek ggio->gctl_length = bp->bio_length; 826fe27e772SPawel Jakub Dawidek ggio->gctl_error = ENOMEM; 8277ffb6e0fSPawel Jakub Dawidek goto start_end; 828fe27e772SPawel Jakub Dawidek } 829fe27e772SPawel Jakub Dawidek bioq_remove(&sc->sc_inqueue, bp); 830e35d3a78SPawel Jakub Dawidek bioq_insert_tail(&sc->sc_outqueue, bp); 831e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 832e35d3a78SPawel Jakub Dawidek 8330d785336SPawel Jakub Dawidek ggio->gctl_seq = (uintptr_t)bp->bio_driver1; 834fe27e772SPawel Jakub Dawidek ggio->gctl_offset = bp->bio_offset; 835fe27e772SPawel Jakub Dawidek ggio->gctl_length = bp->bio_length; 83684436f14SPawel Jakub Dawidek 837fe27e772SPawel Jakub Dawidek switch (bp->bio_cmd) { 838fe27e772SPawel Jakub Dawidek case BIO_READ: 839fe27e772SPawel Jakub Dawidek case BIO_DELETE: 840204a4e19SPawel Jakub Dawidek case BIO_FLUSH: 84115725379SPawel Jakub Dawidek break; 842fe27e772SPawel Jakub Dawidek case BIO_WRITE: 843fe27e772SPawel Jakub Dawidek error = copyout(bp->bio_data, ggio->gctl_data, 844fe27e772SPawel Jakub Dawidek bp->bio_length); 845fe27e772SPawel Jakub Dawidek if (error != 0) { 846e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 847e35d3a78SPawel Jakub Dawidek bioq_remove(&sc->sc_outqueue, bp); 848662a4e58SPawel Jakub Dawidek bioq_insert_head(&sc->sc_inqueue, bp); 849e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 8507ffb6e0fSPawel Jakub Dawidek goto start_end; 851fe27e772SPawel Jakub Dawidek } 852fe27e772SPawel Jakub Dawidek break; 853fe27e772SPawel Jakub Dawidek } 8547ffb6e0fSPawel Jakub Dawidek start_end: 8557ffb6e0fSPawel Jakub Dawidek g_gate_release(sc); 8567ffb6e0fSPawel Jakub Dawidek return (error); 857fe27e772SPawel Jakub Dawidek } 858fe27e772SPawel Jakub Dawidek case G_GATE_CMD_DONE: 859fe27e772SPawel Jakub Dawidek { 860fe27e772SPawel Jakub Dawidek struct g_gate_ctl_io *ggio = (void *)addr; 861fe27e772SPawel Jakub Dawidek 862fe27e772SPawel Jakub Dawidek G_GATE_CHECK_VERSION(ggio); 86332115b10SPawel Jakub Dawidek sc = g_gate_hold(ggio->gctl_unit, NULL); 864fe27e772SPawel Jakub Dawidek if (sc == NULL) 865fe27e772SPawel Jakub Dawidek return (ENOENT); 8667ffb6e0fSPawel Jakub Dawidek error = 0; 867e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 868fe27e772SPawel Jakub Dawidek TAILQ_FOREACH(bp, &sc->sc_outqueue.queue, bio_queue) { 8690d785336SPawel Jakub Dawidek if (ggio->gctl_seq == (uintptr_t)bp->bio_driver1) 870fe27e772SPawel Jakub Dawidek break; 871fe27e772SPawel Jakub Dawidek } 872fe27e772SPawel Jakub Dawidek if (bp != NULL) { 873fe27e772SPawel Jakub Dawidek bioq_remove(&sc->sc_outqueue, bp); 874e35d3a78SPawel Jakub Dawidek sc->sc_queue_count--; 875fe27e772SPawel Jakub Dawidek } 876e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 877fe27e772SPawel Jakub Dawidek if (bp == NULL) { 878fe27e772SPawel Jakub Dawidek /* 879fe27e772SPawel Jakub Dawidek * Request was probably canceled. 880fe27e772SPawel Jakub Dawidek */ 8817ffb6e0fSPawel Jakub Dawidek goto done_end; 882fe27e772SPawel Jakub Dawidek } 883fe27e772SPawel Jakub Dawidek if (ggio->gctl_error == EAGAIN) { 884fe27e772SPawel Jakub Dawidek bp->bio_error = 0; 885fe27e772SPawel Jakub Dawidek G_GATE_LOGREQ(1, bp, "Request desisted."); 886e35d3a78SPawel Jakub Dawidek mtx_lock(&sc->sc_queue_mtx); 887e35d3a78SPawel Jakub Dawidek sc->sc_queue_count++; 888662a4e58SPawel Jakub Dawidek bioq_insert_head(&sc->sc_inqueue, bp); 889fe27e772SPawel Jakub Dawidek wakeup(sc); 890e35d3a78SPawel Jakub Dawidek mtx_unlock(&sc->sc_queue_mtx); 891fe27e772SPawel Jakub Dawidek } else { 892fe27e772SPawel Jakub Dawidek bp->bio_error = ggio->gctl_error; 893fe27e772SPawel Jakub Dawidek if (bp->bio_error == 0) { 894fe27e772SPawel Jakub Dawidek bp->bio_completed = bp->bio_length; 895fe27e772SPawel Jakub Dawidek switch (bp->bio_cmd) { 896fe27e772SPawel Jakub Dawidek case BIO_READ: 897fe27e772SPawel Jakub Dawidek error = copyin(ggio->gctl_data, 898fe27e772SPawel Jakub Dawidek bp->bio_data, bp->bio_length); 899fe27e772SPawel Jakub Dawidek if (error != 0) 900fe27e772SPawel Jakub Dawidek bp->bio_error = error; 901fe27e772SPawel Jakub Dawidek break; 902fe27e772SPawel Jakub Dawidek case BIO_DELETE: 903fe27e772SPawel Jakub Dawidek case BIO_WRITE: 904204a4e19SPawel Jakub Dawidek case BIO_FLUSH: 905fe27e772SPawel Jakub Dawidek break; 906fe27e772SPawel Jakub Dawidek } 907fe27e772SPawel Jakub Dawidek } 908fe27e772SPawel Jakub Dawidek G_GATE_LOGREQ(2, bp, "Request done."); 909fe27e772SPawel Jakub Dawidek g_io_deliver(bp, bp->bio_error); 910fe27e772SPawel Jakub Dawidek } 9117ffb6e0fSPawel Jakub Dawidek done_end: 9127ffb6e0fSPawel Jakub Dawidek g_gate_release(sc); 913fe27e772SPawel Jakub Dawidek return (error); 914fe27e772SPawel Jakub Dawidek } 915fe27e772SPawel Jakub Dawidek } 916fe27e772SPawel Jakub Dawidek return (ENOIOCTL); 917fe27e772SPawel Jakub Dawidek } 918fe27e772SPawel Jakub Dawidek 919fe27e772SPawel Jakub Dawidek static void 9209ecdd506SPawel Jakub Dawidek g_gate_device(void) 921fe27e772SPawel Jakub Dawidek { 922fe27e772SPawel Jakub Dawidek 923fe27e772SPawel Jakub Dawidek status_dev = make_dev(&g_gate_cdevsw, 0x0, UID_ROOT, GID_WHEEL, 0600, 924fe27e772SPawel Jakub Dawidek G_GATE_CTL_NAME); 925fe27e772SPawel Jakub Dawidek } 926fe27e772SPawel Jakub Dawidek 927fe27e772SPawel Jakub Dawidek static int 928fe27e772SPawel Jakub Dawidek g_gate_modevent(module_t mod, int type, void *data) 929fe27e772SPawel Jakub Dawidek { 930fe27e772SPawel Jakub Dawidek int error = 0; 931fe27e772SPawel Jakub Dawidek 932fe27e772SPawel Jakub Dawidek switch (type) { 933fe27e772SPawel Jakub Dawidek case MOD_LOAD: 93432115b10SPawel Jakub Dawidek mtx_init(&g_gate_units_lock, "gg_units_lock", NULL, MTX_DEF); 93532115b10SPawel Jakub Dawidek g_gate_units = malloc(g_gate_maxunits * sizeof(g_gate_units[0]), 93632115b10SPawel Jakub Dawidek M_GATE, M_WAITOK | M_ZERO); 93732115b10SPawel Jakub Dawidek g_gate_nunits = 0; 9389ecdd506SPawel Jakub Dawidek g_gate_device(); 939fe27e772SPawel Jakub Dawidek break; 940fe27e772SPawel Jakub Dawidek case MOD_UNLOAD: 94132115b10SPawel Jakub Dawidek mtx_lock(&g_gate_units_lock); 94232115b10SPawel Jakub Dawidek if (g_gate_nunits > 0) { 94332115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 944fe27e772SPawel Jakub Dawidek error = EBUSY; 945fe27e772SPawel Jakub Dawidek break; 946fe27e772SPawel Jakub Dawidek } 94732115b10SPawel Jakub Dawidek mtx_unlock(&g_gate_units_lock); 94832115b10SPawel Jakub Dawidek mtx_destroy(&g_gate_units_lock); 94901b5c6f7SPedro F. Giffuni if (status_dev != NULL) 950fe27e772SPawel Jakub Dawidek destroy_dev(status_dev); 95132115b10SPawel Jakub Dawidek free(g_gate_units, M_GATE); 952fe27e772SPawel Jakub Dawidek break; 953fe27e772SPawel Jakub Dawidek default: 9543e019deaSPoul-Henning Kamp return (EOPNOTSUPP); 955fe27e772SPawel Jakub Dawidek break; 956fe27e772SPawel Jakub Dawidek } 957fe27e772SPawel Jakub Dawidek 958fe27e772SPawel Jakub Dawidek return (error); 959fe27e772SPawel Jakub Dawidek } 960fe27e772SPawel Jakub Dawidek static moduledata_t g_gate_module = { 961fe27e772SPawel Jakub Dawidek G_GATE_MOD_NAME, 962fe27e772SPawel Jakub Dawidek g_gate_modevent, 963fe27e772SPawel Jakub Dawidek NULL 964fe27e772SPawel Jakub Dawidek }; 965fe27e772SPawel Jakub Dawidek DECLARE_MODULE(geom_gate, g_gate_module, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 966fe27e772SPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_gate_class, g_gate); 96774d6c131SKyle Evans MODULE_VERSION(geom_gate, 0); 968