189aaffecSPawel Jakub Dawidek /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
33728855aSPedro F. Giffuni *
46a146a19SPawel Jakub Dawidek * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
580e63e0aSMariusz Zaborski * Copyright (c) 2019 Mariusz Zaborski <oshogbo@FreeBSD.org>
689aaffecSPawel Jakub Dawidek * All rights reserved.
789aaffecSPawel Jakub Dawidek *
889aaffecSPawel Jakub Dawidek * Redistribution and use in source and binary forms, with or without
989aaffecSPawel Jakub Dawidek * modification, are permitted provided that the following conditions
1089aaffecSPawel Jakub Dawidek * are met:
1189aaffecSPawel Jakub Dawidek * 1. Redistributions of source code must retain the above copyright
1289aaffecSPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer.
1389aaffecSPawel Jakub Dawidek * 2. Redistributions in binary form must reproduce the above copyright
1489aaffecSPawel Jakub Dawidek * notice, this list of conditions and the following disclaimer in the
1589aaffecSPawel Jakub Dawidek * documentation and/or other materials provided with the distribution.
1689aaffecSPawel Jakub Dawidek *
1789aaffecSPawel Jakub Dawidek * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
1889aaffecSPawel Jakub Dawidek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1989aaffecSPawel Jakub Dawidek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2089aaffecSPawel Jakub Dawidek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2189aaffecSPawel Jakub Dawidek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2289aaffecSPawel Jakub Dawidek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2389aaffecSPawel Jakub Dawidek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2489aaffecSPawel Jakub Dawidek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2589aaffecSPawel Jakub Dawidek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2689aaffecSPawel Jakub Dawidek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2789aaffecSPawel Jakub Dawidek * SUCH DAMAGE.
2889aaffecSPawel Jakub Dawidek */
2989aaffecSPawel Jakub Dawidek
3089aaffecSPawel Jakub Dawidek #include <sys/param.h>
3180e63e0aSMariusz Zaborski #include <sys/ctype.h>
3289aaffecSPawel Jakub Dawidek #include <sys/systm.h>
3389aaffecSPawel Jakub Dawidek #include <sys/kernel.h>
3489aaffecSPawel Jakub Dawidek #include <sys/module.h>
3589aaffecSPawel Jakub Dawidek #include <sys/lock.h>
3689aaffecSPawel Jakub Dawidek #include <sys/mutex.h>
3789aaffecSPawel Jakub Dawidek #include <sys/bio.h>
385d807a0eSAndrey V. Elsukov #include <sys/sbuf.h>
3989aaffecSPawel Jakub Dawidek #include <sys/sysctl.h>
4089aaffecSPawel Jakub Dawidek #include <sys/malloc.h>
4189aaffecSPawel Jakub Dawidek #include <geom/geom.h>
42ac03832eSConrad Meyer #include <geom/geom_dbg.h>
4389aaffecSPawel Jakub Dawidek #include <geom/nop/g_nop.h>
4489aaffecSPawel Jakub Dawidek
4589aaffecSPawel Jakub Dawidek SYSCTL_DECL(_kern_geom);
467029da5cSPawel Biernacki static SYSCTL_NODE(_kern_geom, OID_AUTO, nop, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
477029da5cSPawel Biernacki "GEOM_NOP stuff");
4889aaffecSPawel Jakub Dawidek static u_int g_nop_debug = 0;
4989aaffecSPawel Jakub Dawidek SYSCTL_UINT(_kern_geom_nop, OID_AUTO, debug, CTLFLAG_RW, &g_nop_debug, 0,
5089aaffecSPawel Jakub Dawidek "Debug level");
5189aaffecSPawel Jakub Dawidek
5289aaffecSPawel Jakub Dawidek static int g_nop_destroy(struct g_geom *gp, boolean_t force);
5389aaffecSPawel Jakub Dawidek static int g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp,
5489aaffecSPawel Jakub Dawidek struct g_geom *gp);
5589aaffecSPawel Jakub Dawidek static void g_nop_config(struct gctl_req *req, struct g_class *mp,
5689aaffecSPawel Jakub Dawidek const char *verb);
5710519e13SKirk McKusick static g_access_t g_nop_access;
5810519e13SKirk McKusick static g_dumpconf_t g_nop_dumpconf;
5910519e13SKirk McKusick static g_orphan_t g_nop_orphan;
6010519e13SKirk McKusick static g_provgone_t g_nop_providergone;
6110519e13SKirk McKusick static g_resize_t g_nop_resize;
6210519e13SKirk McKusick static g_start_t g_nop_start;
6389aaffecSPawel Jakub Dawidek
6489aaffecSPawel Jakub Dawidek struct g_class g_nop_class = {
6589aaffecSPawel Jakub Dawidek .name = G_NOP_CLASS_NAME,
665721c9c7SPoul-Henning Kamp .version = G_VERSION,
6789aaffecSPawel Jakub Dawidek .ctlreq = g_nop_config,
6810519e13SKirk McKusick .destroy_geom = g_nop_destroy_geom,
6910519e13SKirk McKusick .access = g_nop_access,
7010519e13SKirk McKusick .dumpconf = g_nop_dumpconf,
7110519e13SKirk McKusick .orphan = g_nop_orphan,
7210519e13SKirk McKusick .providergone = g_nop_providergone,
7310519e13SKirk McKusick .resize = g_nop_resize,
7410519e13SKirk McKusick .start = g_nop_start,
7589aaffecSPawel Jakub Dawidek };
7689aaffecSPawel Jakub Dawidek
774f80c855SMariusz Zaborski struct g_nop_delay {
784f80c855SMariusz Zaborski struct callout dl_cal;
794f80c855SMariusz Zaborski struct bio *dl_bio;
804f80c855SMariusz Zaborski TAILQ_ENTRY(g_nop_delay) dl_next;
814f80c855SMariusz Zaborski };
824f80c855SMariusz Zaborski
8380e63e0aSMariusz Zaborski static bool
g_nop_verify_nprefix(const char * name)8480e63e0aSMariusz Zaborski g_nop_verify_nprefix(const char *name)
8580e63e0aSMariusz Zaborski {
8680e63e0aSMariusz Zaborski int i;
8780e63e0aSMariusz Zaborski
8880e63e0aSMariusz Zaborski for (i = 0; i < strlen(name); i++) {
8980e63e0aSMariusz Zaborski if (isalpha(name[i]) == 0 && isdigit(name[i]) == 0) {
9080e63e0aSMariusz Zaborski return (false);
9180e63e0aSMariusz Zaborski }
9280e63e0aSMariusz Zaborski }
9380e63e0aSMariusz Zaborski
9480e63e0aSMariusz Zaborski return (true);
9580e63e0aSMariusz Zaborski }
9680e63e0aSMariusz Zaborski
9789aaffecSPawel Jakub Dawidek static void
g_nop_orphan(struct g_consumer * cp)9889aaffecSPawel Jakub Dawidek g_nop_orphan(struct g_consumer *cp)
9989aaffecSPawel Jakub Dawidek {
10089aaffecSPawel Jakub Dawidek
10189aaffecSPawel Jakub Dawidek g_topology_assert();
10289aaffecSPawel Jakub Dawidek g_nop_destroy(cp->geom, 1);
10389aaffecSPawel Jakub Dawidek }
10489aaffecSPawel Jakub Dawidek
10589aaffecSPawel Jakub Dawidek static void
g_nop_resize(struct g_consumer * cp)1067027e4daSEdward Tomasz Napierala g_nop_resize(struct g_consumer *cp)
1077027e4daSEdward Tomasz Napierala {
1087027e4daSEdward Tomasz Napierala struct g_nop_softc *sc;
1097027e4daSEdward Tomasz Napierala struct g_geom *gp;
1107027e4daSEdward Tomasz Napierala struct g_provider *pp;
1117027e4daSEdward Tomasz Napierala off_t size;
1127027e4daSEdward Tomasz Napierala
1137027e4daSEdward Tomasz Napierala g_topology_assert();
1147027e4daSEdward Tomasz Napierala
1157027e4daSEdward Tomasz Napierala gp = cp->geom;
1167027e4daSEdward Tomasz Napierala sc = gp->softc;
1177027e4daSEdward Tomasz Napierala
1187027e4daSEdward Tomasz Napierala if (sc->sc_explicitsize != 0)
1197027e4daSEdward Tomasz Napierala return;
1207027e4daSEdward Tomasz Napierala if (cp->provider->mediasize < sc->sc_offset) {
1217027e4daSEdward Tomasz Napierala g_nop_destroy(gp, 1);
1227027e4daSEdward Tomasz Napierala return;
1237027e4daSEdward Tomasz Napierala }
1247027e4daSEdward Tomasz Napierala size = cp->provider->mediasize - sc->sc_offset;
1257027e4daSEdward Tomasz Napierala LIST_FOREACH(pp, &gp->provider, provider)
1267027e4daSEdward Tomasz Napierala g_resize_provider(pp, size);
1277027e4daSEdward Tomasz Napierala }
1287027e4daSEdward Tomasz Napierala
12983efd288SConrad Meyer static int
g_nop_dumper(void * priv,void * virtual,off_t offset,size_t length)130489ba222SMitchell Horne g_nop_dumper(void *priv, void *virtual, off_t offset, size_t length)
13183efd288SConrad Meyer {
1324d7486c3SMariusz Zaborski
13383efd288SConrad Meyer return (0);
13483efd288SConrad Meyer }
13583efd288SConrad Meyer
13683efd288SConrad Meyer static void
g_nop_kerneldump(struct bio * bp,struct g_nop_softc * sc)13783efd288SConrad Meyer g_nop_kerneldump(struct bio *bp, struct g_nop_softc *sc)
13883efd288SConrad Meyer {
13983efd288SConrad Meyer struct g_kerneldump *gkd;
14083efd288SConrad Meyer struct g_geom *gp;
14183efd288SConrad Meyer struct g_provider *pp;
14283efd288SConrad Meyer
14383efd288SConrad Meyer gkd = (struct g_kerneldump *)bp->bio_data;
14483efd288SConrad Meyer gp = bp->bio_to->geom;
14583efd288SConrad Meyer g_trace(G_T_TOPOLOGY, "%s(%s, %jd, %jd)", __func__, gp->name,
14683efd288SConrad Meyer (intmax_t)gkd->offset, (intmax_t)gkd->length);
14783efd288SConrad Meyer
14883efd288SConrad Meyer pp = LIST_FIRST(&gp->provider);
14983efd288SConrad Meyer
15083efd288SConrad Meyer gkd->di.dumper = g_nop_dumper;
15183efd288SConrad Meyer gkd->di.priv = sc;
15283efd288SConrad Meyer gkd->di.blocksize = pp->sectorsize;
15383efd288SConrad Meyer gkd->di.maxiosize = DFLTPHYS;
15483efd288SConrad Meyer gkd->di.mediaoffset = sc->sc_offset + gkd->offset;
15583efd288SConrad Meyer if (gkd->offset > sc->sc_explicitsize) {
15683efd288SConrad Meyer g_io_deliver(bp, ENODEV);
15783efd288SConrad Meyer return;
15883efd288SConrad Meyer }
15983efd288SConrad Meyer if (gkd->offset + gkd->length > sc->sc_explicitsize)
16083efd288SConrad Meyer gkd->length = sc->sc_explicitsize - gkd->offset;
16183efd288SConrad Meyer gkd->di.mediasize = gkd->length;
16283efd288SConrad Meyer g_io_deliver(bp, 0);
16383efd288SConrad Meyer }
16483efd288SConrad Meyer
1657027e4daSEdward Tomasz Napierala static void
g_nop_pass(struct bio * cbp,struct g_geom * gp)1664f80c855SMariusz Zaborski g_nop_pass(struct bio *cbp, struct g_geom *gp)
1674f80c855SMariusz Zaborski {
1684f80c855SMariusz Zaborski
1694f80c855SMariusz Zaborski G_NOP_LOGREQ(cbp, "Sending request.");
1704f80c855SMariusz Zaborski g_io_request(cbp, LIST_FIRST(&gp->consumer));
1714f80c855SMariusz Zaborski }
1724f80c855SMariusz Zaborski
1734f80c855SMariusz Zaborski static void
g_nop_pass_timeout(void * data)1744f80c855SMariusz Zaborski g_nop_pass_timeout(void *data)
1754f80c855SMariusz Zaborski {
1764f80c855SMariusz Zaborski struct g_nop_softc *sc;
1774f80c855SMariusz Zaborski struct g_geom *gp;
1784f80c855SMariusz Zaborski struct g_nop_delay *gndelay;
1794f80c855SMariusz Zaborski
1804f80c855SMariusz Zaborski gndelay = (struct g_nop_delay *)data;
1814f80c855SMariusz Zaborski
1824f80c855SMariusz Zaborski gp = gndelay->dl_bio->bio_to->geom;
1834f80c855SMariusz Zaborski sc = gp->softc;
1844f80c855SMariusz Zaborski
1854f80c855SMariusz Zaborski mtx_lock(&sc->sc_lock);
1864f80c855SMariusz Zaborski TAILQ_REMOVE(&sc->sc_head_delay, gndelay, dl_next);
1874f80c855SMariusz Zaborski mtx_unlock(&sc->sc_lock);
1884f80c855SMariusz Zaborski
1894f80c855SMariusz Zaborski g_nop_pass(gndelay->dl_bio, gp);
1904f80c855SMariusz Zaborski
1914f80c855SMariusz Zaborski g_free(data);
1924f80c855SMariusz Zaborski }
1934f80c855SMariusz Zaborski
1944f80c855SMariusz Zaborski static void
g_nop_start(struct bio * bp)19589aaffecSPawel Jakub Dawidek g_nop_start(struct bio *bp)
19689aaffecSPawel Jakub Dawidek {
197e370e911SPawel Jakub Dawidek struct g_nop_softc *sc;
19889aaffecSPawel Jakub Dawidek struct g_geom *gp;
19989aaffecSPawel Jakub Dawidek struct g_provider *pp;
20089aaffecSPawel Jakub Dawidek struct bio *cbp;
2014f80c855SMariusz Zaborski u_int failprob, delayprob, delaytime;
2024f80c855SMariusz Zaborski
203645532a4SMariusz Zaborski failprob = delayprob = delaytime = 0;
20489aaffecSPawel Jakub Dawidek
20589aaffecSPawel Jakub Dawidek gp = bp->bio_to->geom;
206e370e911SPawel Jakub Dawidek sc = gp->softc;
2074f80c855SMariusz Zaborski
20889aaffecSPawel Jakub Dawidek G_NOP_LOGREQ(bp, "Request received.");
20940ea77a0SAlexander Motin mtx_lock(&sc->sc_lock);
210df3d5a19SPawel Jakub Dawidek switch (bp->bio_cmd) {
211df3d5a19SPawel Jakub Dawidek case BIO_READ:
212df3d5a19SPawel Jakub Dawidek sc->sc_reads++;
213df3d5a19SPawel Jakub Dawidek sc->sc_readbytes += bp->bio_length;
214645532a4SMariusz Zaborski if (sc->sc_count_until_fail != 0) {
215645532a4SMariusz Zaborski sc->sc_count_until_fail -= 1;
216645532a4SMariusz Zaborski } else {
2176a146a19SPawel Jakub Dawidek failprob = sc->sc_rfailprob;
2184f80c855SMariusz Zaborski delayprob = sc->sc_rdelayprob;
2194f80c855SMariusz Zaborski delaytime = sc->sc_delaymsec;
220645532a4SMariusz Zaborski }
221df3d5a19SPawel Jakub Dawidek break;
222df3d5a19SPawel Jakub Dawidek case BIO_WRITE:
223df3d5a19SPawel Jakub Dawidek sc->sc_writes++;
224df3d5a19SPawel Jakub Dawidek sc->sc_wrotebytes += bp->bio_length;
225645532a4SMariusz Zaborski if (sc->sc_count_until_fail != 0) {
226645532a4SMariusz Zaborski sc->sc_count_until_fail -= 1;
227645532a4SMariusz Zaborski } else {
2286a146a19SPawel Jakub Dawidek failprob = sc->sc_wfailprob;
2294f80c855SMariusz Zaborski delayprob = sc->sc_wdelayprob;
2304f80c855SMariusz Zaborski delaytime = sc->sc_delaymsec;
231645532a4SMariusz Zaborski }
232df3d5a19SPawel Jakub Dawidek break;
23345d7de1dSEdward Tomasz Napierala case BIO_DELETE:
23445d7de1dSEdward Tomasz Napierala sc->sc_deletes++;
23545d7de1dSEdward Tomasz Napierala break;
23645d7de1dSEdward Tomasz Napierala case BIO_GETATTR:
23745d7de1dSEdward Tomasz Napierala sc->sc_getattrs++;
2386f7f85e0SAlan Somers if (sc->sc_physpath &&
23983efd288SConrad Meyer g_handleattr_str(bp, "GEOM::physpath", sc->sc_physpath))
24083efd288SConrad Meyer ;
24183efd288SConrad Meyer else if (strcmp(bp->bio_attribute, "GEOM::kerneldump") == 0)
24283efd288SConrad Meyer g_nop_kerneldump(bp, sc);
24383efd288SConrad Meyer else
24483efd288SConrad Meyer /*
24583efd288SConrad Meyer * Fallthrough to forwarding the GETATTR down to the
24683efd288SConrad Meyer * lower level device.
24783efd288SConrad Meyer */
24883efd288SConrad Meyer break;
2496f7f85e0SAlan Somers mtx_unlock(&sc->sc_lock);
2506f7f85e0SAlan Somers return;
25145d7de1dSEdward Tomasz Napierala case BIO_FLUSH:
25245d7de1dSEdward Tomasz Napierala sc->sc_flushes++;
25345d7de1dSEdward Tomasz Napierala break;
2548b522bdaSWarner Losh case BIO_SPEEDUP:
2558b522bdaSWarner Losh sc->sc_speedups++;
2568b522bdaSWarner Losh break;
25745d7de1dSEdward Tomasz Napierala case BIO_CMD0:
25845d7de1dSEdward Tomasz Napierala sc->sc_cmd0s++;
25945d7de1dSEdward Tomasz Napierala break;
26045d7de1dSEdward Tomasz Napierala case BIO_CMD1:
26145d7de1dSEdward Tomasz Napierala sc->sc_cmd1s++;
26245d7de1dSEdward Tomasz Napierala break;
26345d7de1dSEdward Tomasz Napierala case BIO_CMD2:
26445d7de1dSEdward Tomasz Napierala sc->sc_cmd2s++;
26545d7de1dSEdward Tomasz Napierala break;
266df3d5a19SPawel Jakub Dawidek }
26740ea77a0SAlexander Motin mtx_unlock(&sc->sc_lock);
268645532a4SMariusz Zaborski
2696a146a19SPawel Jakub Dawidek if (failprob > 0) {
27089aaffecSPawel Jakub Dawidek u_int rval;
27189aaffecSPawel Jakub Dawidek
27289aaffecSPawel Jakub Dawidek rval = arc4random() % 100;
2736a146a19SPawel Jakub Dawidek if (rval < failprob) {
274d7078f3bSJohn-Mark Gurney G_NOP_LOGREQLVL(1, bp, "Returning error=%d.", sc->sc_error);
2756a146a19SPawel Jakub Dawidek g_io_deliver(bp, sc->sc_error);
27689aaffecSPawel Jakub Dawidek return;
27789aaffecSPawel Jakub Dawidek }
27889aaffecSPawel Jakub Dawidek }
2794f80c855SMariusz Zaborski
28054df0743SPawel Jakub Dawidek cbp = g_clone_bio(bp);
28154df0743SPawel Jakub Dawidek if (cbp == NULL) {
28254df0743SPawel Jakub Dawidek g_io_deliver(bp, ENOMEM);
28354df0743SPawel Jakub Dawidek return;
28454df0743SPawel Jakub Dawidek }
28589aaffecSPawel Jakub Dawidek cbp->bio_done = g_std_done;
286e370e911SPawel Jakub Dawidek cbp->bio_offset = bp->bio_offset + sc->sc_offset;
287969ff54dSPawel Jakub Dawidek pp = LIST_FIRST(&gp->provider);
288969ff54dSPawel Jakub Dawidek KASSERT(pp != NULL, ("NULL pp"));
289969ff54dSPawel Jakub Dawidek cbp->bio_to = pp;
2904f80c855SMariusz Zaborski
2914f80c855SMariusz Zaborski if (delayprob > 0) {
2924f80c855SMariusz Zaborski struct g_nop_delay *gndelay;
2934f80c855SMariusz Zaborski u_int rval;
2944f80c855SMariusz Zaborski
2954f80c855SMariusz Zaborski rval = arc4random() % 100;
2964f80c855SMariusz Zaborski if (rval < delayprob) {
2974f80c855SMariusz Zaborski gndelay = g_malloc(sizeof(*gndelay), M_NOWAIT | M_ZERO);
2984f80c855SMariusz Zaborski if (gndelay != NULL) {
2994f80c855SMariusz Zaborski callout_init(&gndelay->dl_cal, 1);
3004f80c855SMariusz Zaborski
3014f80c855SMariusz Zaborski gndelay->dl_bio = cbp;
3024f80c855SMariusz Zaborski
3034f80c855SMariusz Zaborski mtx_lock(&sc->sc_lock);
3044f80c855SMariusz Zaborski TAILQ_INSERT_TAIL(&sc->sc_head_delay, gndelay,
3054f80c855SMariusz Zaborski dl_next);
3064f80c855SMariusz Zaborski mtx_unlock(&sc->sc_lock);
3074f80c855SMariusz Zaborski
3084f80c855SMariusz Zaborski callout_reset(&gndelay->dl_cal,
3094f80c855SMariusz Zaborski MSEC_2_TICKS(delaytime), g_nop_pass_timeout,
3104f80c855SMariusz Zaborski gndelay);
3114f80c855SMariusz Zaborski return;
3124f80c855SMariusz Zaborski }
3134f80c855SMariusz Zaborski }
3144f80c855SMariusz Zaborski }
3154f80c855SMariusz Zaborski
3164f80c855SMariusz Zaborski g_nop_pass(cbp, gp);
31789aaffecSPawel Jakub Dawidek }
31889aaffecSPawel Jakub Dawidek
31989aaffecSPawel Jakub Dawidek static int
g_nop_access(struct g_provider * pp,int dr,int dw,int de)32089aaffecSPawel Jakub Dawidek g_nop_access(struct g_provider *pp, int dr, int dw, int de)
32189aaffecSPawel Jakub Dawidek {
32289aaffecSPawel Jakub Dawidek struct g_geom *gp;
32389aaffecSPawel Jakub Dawidek struct g_consumer *cp;
32489aaffecSPawel Jakub Dawidek int error;
32589aaffecSPawel Jakub Dawidek
32689aaffecSPawel Jakub Dawidek gp = pp->geom;
32789aaffecSPawel Jakub Dawidek cp = LIST_FIRST(&gp->consumer);
32889aaffecSPawel Jakub Dawidek error = g_access(cp, dr, dw, de);
32989aaffecSPawel Jakub Dawidek
33089aaffecSPawel Jakub Dawidek return (error);
33189aaffecSPawel Jakub Dawidek }
33289aaffecSPawel Jakub Dawidek
33389aaffecSPawel Jakub Dawidek static int
g_nop_create(struct gctl_req * req,struct g_class * mp,struct g_provider * pp,const char * gnopname,int ioerror,u_int count_until_fail,u_int rfailprob,u_int wfailprob,u_int delaymsec,u_int rdelayprob,u_int wdelayprob,off_t offset,off_t size,u_int secsize,off_t stripesize,off_t stripeoffset,const char * physpath)33489aaffecSPawel Jakub Dawidek g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
33580e63e0aSMariusz Zaborski const char *gnopname, int ioerror, u_int count_until_fail,
33680e63e0aSMariusz Zaborski u_int rfailprob, u_int wfailprob, u_int delaymsec, u_int rdelayprob,
33780e63e0aSMariusz Zaborski u_int wdelayprob, off_t offset, off_t size, u_int secsize, off_t stripesize,
33880e63e0aSMariusz Zaborski off_t stripeoffset, const char *physpath)
33989aaffecSPawel Jakub Dawidek {
340e370e911SPawel Jakub Dawidek struct g_nop_softc *sc;
34189aaffecSPawel Jakub Dawidek struct g_geom *gp;
34289aaffecSPawel Jakub Dawidek struct g_provider *newpp;
34389aaffecSPawel Jakub Dawidek struct g_consumer *cp;
344ae1cce52SWarner Losh struct g_geom_alias *gap;
3450e11f0a9SPawel Jakub Dawidek char name[64];
34680e63e0aSMariusz Zaborski int error, n;
3477027e4daSEdward Tomasz Napierala off_t explicitsize;
34889aaffecSPawel Jakub Dawidek
34989aaffecSPawel Jakub Dawidek g_topology_assert();
35089aaffecSPawel Jakub Dawidek
35189aaffecSPawel Jakub Dawidek gp = NULL;
35289aaffecSPawel Jakub Dawidek newpp = NULL;
35389aaffecSPawel Jakub Dawidek cp = NULL;
35489aaffecSPawel Jakub Dawidek
355e370e911SPawel Jakub Dawidek if ((offset % pp->sectorsize) != 0) {
356e370e911SPawel Jakub Dawidek gctl_error(req, "Invalid offset for provider %s.", pp->name);
357e370e911SPawel Jakub Dawidek return (EINVAL);
358e370e911SPawel Jakub Dawidek }
359e370e911SPawel Jakub Dawidek if ((size % pp->sectorsize) != 0) {
360e370e911SPawel Jakub Dawidek gctl_error(req, "Invalid size for provider %s.", pp->name);
361e370e911SPawel Jakub Dawidek return (EINVAL);
362e370e911SPawel Jakub Dawidek }
363e370e911SPawel Jakub Dawidek if (offset >= pp->mediasize) {
364e370e911SPawel Jakub Dawidek gctl_error(req, "Invalid offset for provider %s.", pp->name);
365e370e911SPawel Jakub Dawidek return (EINVAL);
366e370e911SPawel Jakub Dawidek }
3677027e4daSEdward Tomasz Napierala explicitsize = size;
368e370e911SPawel Jakub Dawidek if (size == 0)
369e370e911SPawel Jakub Dawidek size = pp->mediasize - offset;
370e370e911SPawel Jakub Dawidek if (offset + size > pp->mediasize) {
371e370e911SPawel Jakub Dawidek gctl_error(req, "Invalid size for provider %s.", pp->name);
372e370e911SPawel Jakub Dawidek return (EINVAL);
373e370e911SPawel Jakub Dawidek }
374d5c96d38SPawel Jakub Dawidek if (secsize == 0)
375d5c96d38SPawel Jakub Dawidek secsize = pp->sectorsize;
376d5c96d38SPawel Jakub Dawidek else if ((secsize % pp->sectorsize) != 0) {
377d5c96d38SPawel Jakub Dawidek gctl_error(req, "Invalid secsize for provider %s.", pp->name);
378d5c96d38SPawel Jakub Dawidek return (EINVAL);
379d5c96d38SPawel Jakub Dawidek }
380cd853791SKonstantin Belousov if (secsize > maxphys) {
381e76dc512SAndrey V. Elsukov gctl_error(req, "secsize is too big.");
382e76dc512SAndrey V. Elsukov return (EINVAL);
383e76dc512SAndrey V. Elsukov }
384f2b3e9e8SAndrey V. Elsukov size -= size % secsize;
385bb27d7edSEdward Tomasz Napierala if ((stripesize % pp->sectorsize) != 0) {
386bb27d7edSEdward Tomasz Napierala gctl_error(req, "Invalid stripesize for provider %s.", pp->name);
387bb27d7edSEdward Tomasz Napierala return (EINVAL);
388bb27d7edSEdward Tomasz Napierala }
389bb27d7edSEdward Tomasz Napierala if ((stripeoffset % pp->sectorsize) != 0) {
390bb27d7edSEdward Tomasz Napierala gctl_error(req, "Invalid stripeoffset for provider %s.", pp->name);
391bb27d7edSEdward Tomasz Napierala return (EINVAL);
392bb27d7edSEdward Tomasz Napierala }
393bb27d7edSEdward Tomasz Napierala if (stripesize != 0 && stripeoffset >= stripesize) {
394bb27d7edSEdward Tomasz Napierala gctl_error(req, "stripeoffset is too big.");
395bb27d7edSEdward Tomasz Napierala return (EINVAL);
396bb27d7edSEdward Tomasz Napierala }
39780e63e0aSMariusz Zaborski if (gnopname != NULL && !g_nop_verify_nprefix(gnopname)) {
39880e63e0aSMariusz Zaborski gctl_error(req, "Name %s is invalid.", gnopname);
39980e63e0aSMariusz Zaborski return (EINVAL);
40080e63e0aSMariusz Zaborski }
40180e63e0aSMariusz Zaborski
40280e63e0aSMariusz Zaborski if (gnopname != NULL) {
40380e63e0aSMariusz Zaborski n = snprintf(name, sizeof(name), "%s%s", gnopname,
40480e63e0aSMariusz Zaborski G_NOP_SUFFIX);
40580e63e0aSMariusz Zaborski } else {
40680e63e0aSMariusz Zaborski n = snprintf(name, sizeof(name), "%s%s", pp->name,
40780e63e0aSMariusz Zaborski G_NOP_SUFFIX);
40880e63e0aSMariusz Zaborski }
40980e63e0aSMariusz Zaborski if (n <= 0 || n >= sizeof(name)) {
41080e63e0aSMariusz Zaborski gctl_error(req, "Invalid provider name.");
41180e63e0aSMariusz Zaborski return (EINVAL);
41280e63e0aSMariusz Zaborski }
4130e11f0a9SPawel Jakub Dawidek LIST_FOREACH(gp, &mp->geom, geom) {
4140e11f0a9SPawel Jakub Dawidek if (strcmp(gp->name, name) == 0) {
4150e11f0a9SPawel Jakub Dawidek gctl_error(req, "Provider %s already exists.", name);
4160e11f0a9SPawel Jakub Dawidek return (EEXIST);
4170e11f0a9SPawel Jakub Dawidek }
4180e11f0a9SPawel Jakub Dawidek }
41902c62349SJaakko Heinonen gp = g_new_geomf(mp, "%s", name);
420aa25ccfaSMateusz Guzik sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
421e370e911SPawel Jakub Dawidek sc->sc_offset = offset;
4227027e4daSEdward Tomasz Napierala sc->sc_explicitsize = explicitsize;
423bb27d7edSEdward Tomasz Napierala sc->sc_stripesize = stripesize;
424bb27d7edSEdward Tomasz Napierala sc->sc_stripeoffset = stripeoffset;
4256f7f85e0SAlan Somers if (physpath && strcmp(physpath, G_NOP_PHYSPATH_PASSTHROUGH)) {
4266f7f85e0SAlan Somers sc->sc_physpath = strndup(physpath, MAXPATHLEN, M_GEOM);
4276f7f85e0SAlan Somers } else
4286f7f85e0SAlan Somers sc->sc_physpath = NULL;
4296a146a19SPawel Jakub Dawidek sc->sc_error = ioerror;
430090a3ea3SChuck Silvers sc->sc_count_until_fail = count_until_fail;
4316a146a19SPawel Jakub Dawidek sc->sc_rfailprob = rfailprob;
4326a146a19SPawel Jakub Dawidek sc->sc_wfailprob = wfailprob;
4334f80c855SMariusz Zaborski sc->sc_delaymsec = delaymsec;
4344f80c855SMariusz Zaborski sc->sc_rdelayprob = rdelayprob;
4354f80c855SMariusz Zaborski sc->sc_wdelayprob = wdelayprob;
436df3d5a19SPawel Jakub Dawidek sc->sc_reads = 0;
437df3d5a19SPawel Jakub Dawidek sc->sc_writes = 0;
43845d7de1dSEdward Tomasz Napierala sc->sc_deletes = 0;
43945d7de1dSEdward Tomasz Napierala sc->sc_getattrs = 0;
44045d7de1dSEdward Tomasz Napierala sc->sc_flushes = 0;
4418b522bdaSWarner Losh sc->sc_speedups = 0;
44245d7de1dSEdward Tomasz Napierala sc->sc_cmd0s = 0;
44345d7de1dSEdward Tomasz Napierala sc->sc_cmd1s = 0;
44445d7de1dSEdward Tomasz Napierala sc->sc_cmd2s = 0;
445df3d5a19SPawel Jakub Dawidek sc->sc_readbytes = 0;
446df3d5a19SPawel Jakub Dawidek sc->sc_wrotebytes = 0;
4474f80c855SMariusz Zaborski TAILQ_INIT(&sc->sc_head_delay);
44840ea77a0SAlexander Motin mtx_init(&sc->sc_lock, "gnop lock", NULL, MTX_DEF);
449e370e911SPawel Jakub Dawidek gp->softc = sc;
45089aaffecSPawel Jakub Dawidek
45102c62349SJaakko Heinonen newpp = g_new_providerf(gp, "%s", gp->name);
45240ea77a0SAlexander Motin newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
453e370e911SPawel Jakub Dawidek newpp->mediasize = size;
454d5c96d38SPawel Jakub Dawidek newpp->sectorsize = secsize;
455bb27d7edSEdward Tomasz Napierala newpp->stripesize = stripesize;
456bb27d7edSEdward Tomasz Napierala newpp->stripeoffset = stripeoffset;
457ae1cce52SWarner Losh LIST_FOREACH(gap, &pp->aliases, ga_next)
458ae1cce52SWarner Losh g_provider_add_alias(newpp, "%s%s", gap->ga_alias, G_NOP_SUFFIX);
45989aaffecSPawel Jakub Dawidek
46089aaffecSPawel Jakub Dawidek cp = g_new_consumer(gp);
46140ea77a0SAlexander Motin cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
46289aaffecSPawel Jakub Dawidek error = g_attach(cp, pp);
46389aaffecSPawel Jakub Dawidek if (error != 0) {
46489aaffecSPawel Jakub Dawidek gctl_error(req, "Cannot attach to provider %s.", pp->name);
46589aaffecSPawel Jakub Dawidek goto fail;
46689aaffecSPawel Jakub Dawidek }
46789aaffecSPawel Jakub Dawidek
4686c6e13b6SAlexander Motin newpp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
46989aaffecSPawel Jakub Dawidek g_error_provider(newpp, 0);
47089aaffecSPawel Jakub Dawidek G_NOP_DEBUG(0, "Device %s created.", gp->name);
47189aaffecSPawel Jakub Dawidek return (0);
47289aaffecSPawel Jakub Dawidek fail:
47389aaffecSPawel Jakub Dawidek if (cp->provider != NULL)
47489aaffecSPawel Jakub Dawidek g_detach(cp);
47589aaffecSPawel Jakub Dawidek g_destroy_consumer(cp);
476b3f05a2eSMax Khon g_destroy_provider(newpp);
47740ea77a0SAlexander Motin mtx_destroy(&sc->sc_lock);
4786f7f85e0SAlan Somers free(sc->sc_physpath, M_GEOM);
479e370e911SPawel Jakub Dawidek g_free(gp->softc);
48089aaffecSPawel Jakub Dawidek g_destroy_geom(gp);
48189aaffecSPawel Jakub Dawidek return (error);
48289aaffecSPawel Jakub Dawidek }
48389aaffecSPawel Jakub Dawidek
48410519e13SKirk McKusick static void
g_nop_providergone(struct g_provider * pp)48510519e13SKirk McKusick g_nop_providergone(struct g_provider *pp)
48610519e13SKirk McKusick {
48710519e13SKirk McKusick struct g_geom *gp = pp->geom;
48810519e13SKirk McKusick struct g_nop_softc *sc = gp->softc;
48910519e13SKirk McKusick
4904f80c855SMariusz Zaborski KASSERT(TAILQ_EMPTY(&sc->sc_head_delay),
4914f80c855SMariusz Zaborski ("delayed request list is not empty"));
4924f80c855SMariusz Zaborski
49310519e13SKirk McKusick gp->softc = NULL;
49410519e13SKirk McKusick free(sc->sc_physpath, M_GEOM);
49510519e13SKirk McKusick mtx_destroy(&sc->sc_lock);
49610519e13SKirk McKusick g_free(sc);
49710519e13SKirk McKusick }
49810519e13SKirk McKusick
49989aaffecSPawel Jakub Dawidek static int
g_nop_destroy(struct g_geom * gp,boolean_t force)50089aaffecSPawel Jakub Dawidek g_nop_destroy(struct g_geom *gp, boolean_t force)
50189aaffecSPawel Jakub Dawidek {
50240ea77a0SAlexander Motin struct g_nop_softc *sc;
50389aaffecSPawel Jakub Dawidek struct g_provider *pp;
50489aaffecSPawel Jakub Dawidek
50589aaffecSPawel Jakub Dawidek g_topology_assert();
50640ea77a0SAlexander Motin sc = gp->softc;
50740ea77a0SAlexander Motin if (sc == NULL)
508969ff54dSPawel Jakub Dawidek return (ENXIO);
50989aaffecSPawel Jakub Dawidek pp = LIST_FIRST(&gp->provider);
51089aaffecSPawel Jakub Dawidek if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
51189aaffecSPawel Jakub Dawidek if (force) {
51289aaffecSPawel Jakub Dawidek G_NOP_DEBUG(0, "Device %s is still open, so it "
51389aaffecSPawel Jakub Dawidek "can't be definitely removed.", pp->name);
51489aaffecSPawel Jakub Dawidek } else {
51589aaffecSPawel Jakub Dawidek G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
51689aaffecSPawel Jakub Dawidek pp->name, pp->acr, pp->acw, pp->ace);
51789aaffecSPawel Jakub Dawidek return (EBUSY);
51889aaffecSPawel Jakub Dawidek }
51989aaffecSPawel Jakub Dawidek } else {
52089aaffecSPawel Jakub Dawidek G_NOP_DEBUG(0, "Device %s removed.", gp->name);
52189aaffecSPawel Jakub Dawidek }
5224f80c855SMariusz Zaborski
52389aaffecSPawel Jakub Dawidek g_wither_geom(gp, ENXIO);
52489aaffecSPawel Jakub Dawidek
52589aaffecSPawel Jakub Dawidek return (0);
52689aaffecSPawel Jakub Dawidek }
52789aaffecSPawel Jakub Dawidek
52889aaffecSPawel Jakub Dawidek static int
g_nop_destroy_geom(struct gctl_req * req,struct g_class * mp,struct g_geom * gp)52989aaffecSPawel Jakub Dawidek g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
53089aaffecSPawel Jakub Dawidek {
53189aaffecSPawel Jakub Dawidek
53289aaffecSPawel Jakub Dawidek return (g_nop_destroy(gp, 0));
53389aaffecSPawel Jakub Dawidek }
53489aaffecSPawel Jakub Dawidek
53589aaffecSPawel Jakub Dawidek static void
g_nop_ctl_create(struct gctl_req * req,struct g_class * mp)53689aaffecSPawel Jakub Dawidek g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
53789aaffecSPawel Jakub Dawidek {
53889aaffecSPawel Jakub Dawidek struct g_provider *pp;
53930738a34SChuck Silvers intmax_t *val, error, rfailprob, wfailprob, count_until_fail, offset,
54030738a34SChuck Silvers secsize, size, stripesize, stripeoffset, delaymsec,
54130738a34SChuck Silvers rdelayprob, wdelayprob;
542fcf69f3dSXin LI const char *physpath, *gnopname;
54389aaffecSPawel Jakub Dawidek char param[16];
54489aaffecSPawel Jakub Dawidek int i, *nargs;
54589aaffecSPawel Jakub Dawidek
54689aaffecSPawel Jakub Dawidek g_topology_assert();
54789aaffecSPawel Jakub Dawidek
54830738a34SChuck Silvers error = -1;
54930738a34SChuck Silvers rfailprob = -1;
55030738a34SChuck Silvers wfailprob = -1;
55130738a34SChuck Silvers count_until_fail = -1;
55230738a34SChuck Silvers offset = 0;
55330738a34SChuck Silvers secsize = 0;
55430738a34SChuck Silvers size = 0;
55530738a34SChuck Silvers stripesize = 0;
55630738a34SChuck Silvers stripeoffset = 0;
55730738a34SChuck Silvers delaymsec = -1;
55830738a34SChuck Silvers rdelayprob = -1;
55930738a34SChuck Silvers wdelayprob = -1;
56030738a34SChuck Silvers
56189aaffecSPawel Jakub Dawidek nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
56289aaffecSPawel Jakub Dawidek if (nargs == NULL) {
56389aaffecSPawel Jakub Dawidek gctl_error(req, "No '%s' argument", "nargs");
56489aaffecSPawel Jakub Dawidek return;
56589aaffecSPawel Jakub Dawidek }
56689aaffecSPawel Jakub Dawidek if (*nargs <= 0) {
56789aaffecSPawel Jakub Dawidek gctl_error(req, "Missing device(s).");
56889aaffecSPawel Jakub Dawidek return;
56989aaffecSPawel Jakub Dawidek }
57030738a34SChuck Silvers val = gctl_get_paraml_opt(req, "error", sizeof(*val));
57130738a34SChuck Silvers if (val != NULL) {
57230738a34SChuck Silvers error = *val;
57389aaffecSPawel Jakub Dawidek }
57430738a34SChuck Silvers val = gctl_get_paraml_opt(req, "rfailprob", sizeof(*val));
57530738a34SChuck Silvers if (val != NULL) {
57630738a34SChuck Silvers rfailprob = *val;
57730738a34SChuck Silvers if (rfailprob < -1 || rfailprob > 100) {
5786a146a19SPawel Jakub Dawidek gctl_error(req, "Invalid '%s' argument", "rfailprob");
5796a146a19SPawel Jakub Dawidek return;
5806a146a19SPawel Jakub Dawidek }
5816a146a19SPawel Jakub Dawidek }
58230738a34SChuck Silvers val = gctl_get_paraml_opt(req, "wfailprob", sizeof(*val));
58330738a34SChuck Silvers if (val != NULL) {
58430738a34SChuck Silvers wfailprob = *val;
58530738a34SChuck Silvers if (wfailprob < -1 || wfailprob > 100) {
5866a146a19SPawel Jakub Dawidek gctl_error(req, "Invalid '%s' argument", "wfailprob");
58789aaffecSPawel Jakub Dawidek return;
58889aaffecSPawel Jakub Dawidek }
5894f80c855SMariusz Zaborski }
59030738a34SChuck Silvers val = gctl_get_paraml_opt(req, "delaymsec", sizeof(*val));
59130738a34SChuck Silvers if (val != NULL) {
59230738a34SChuck Silvers delaymsec = *val;
59330738a34SChuck Silvers if (delaymsec < 1 && delaymsec != -1) {
5944f80c855SMariusz Zaborski gctl_error(req, "Invalid '%s' argument", "delaymsec");
5954f80c855SMariusz Zaborski return;
5964f80c855SMariusz Zaborski }
5974f80c855SMariusz Zaborski }
59830738a34SChuck Silvers val = gctl_get_paraml_opt(req, "rdelayprob", sizeof(*val));
59930738a34SChuck Silvers if (val != NULL) {
60030738a34SChuck Silvers rdelayprob = *val;
60130738a34SChuck Silvers if (rdelayprob < -1 || rdelayprob > 100) {
6024f80c855SMariusz Zaborski gctl_error(req, "Invalid '%s' argument", "rdelayprob");
6034f80c855SMariusz Zaborski return;
6044f80c855SMariusz Zaborski }
6054f80c855SMariusz Zaborski }
60630738a34SChuck Silvers val = gctl_get_paraml_opt(req, "wdelayprob", sizeof(*val));
60730738a34SChuck Silvers if (val != NULL) {
60830738a34SChuck Silvers wdelayprob = *val;
60930738a34SChuck Silvers if (wdelayprob < -1 || wdelayprob > 100) {
6104f80c855SMariusz Zaborski gctl_error(req, "Invalid '%s' argument", "wdelayprob");
6114f80c855SMariusz Zaborski return;
6124f80c855SMariusz Zaborski }
61330738a34SChuck Silvers }
61430738a34SChuck Silvers val = gctl_get_paraml_opt(req, "count_until_fail", sizeof(*val));
61530738a34SChuck Silvers if (val != NULL) {
61630738a34SChuck Silvers count_until_fail = *val;
61730738a34SChuck Silvers if (count_until_fail < -1) {
61830738a34SChuck Silvers gctl_error(req, "Invalid '%s' argument",
61930738a34SChuck Silvers "count_until_fail");
620090a3ea3SChuck Silvers return;
621090a3ea3SChuck Silvers }
622090a3ea3SChuck Silvers }
62330738a34SChuck Silvers val = gctl_get_paraml_opt(req, "offset", sizeof(*val));
62430738a34SChuck Silvers if (val != NULL) {
62530738a34SChuck Silvers offset = *val;
62630738a34SChuck Silvers if (offset < 0) {
627e370e911SPawel Jakub Dawidek gctl_error(req, "Invalid '%s' argument", "offset");
628e370e911SPawel Jakub Dawidek return;
629e370e911SPawel Jakub Dawidek }
630e370e911SPawel Jakub Dawidek }
63130738a34SChuck Silvers val = gctl_get_paraml_opt(req, "size", sizeof(*val));
63230738a34SChuck Silvers if (val != NULL) {
63330738a34SChuck Silvers size = *val;
63430738a34SChuck Silvers if (size < 0) {
635e370e911SPawel Jakub Dawidek gctl_error(req, "Invalid '%s' argument", "size");
636e370e911SPawel Jakub Dawidek return;
637e370e911SPawel Jakub Dawidek }
638d5c96d38SPawel Jakub Dawidek }
63930738a34SChuck Silvers val = gctl_get_paraml_opt(req, "secsize", sizeof(*val));
64030738a34SChuck Silvers if (val != NULL) {
64130738a34SChuck Silvers secsize = *val;
64230738a34SChuck Silvers if (secsize < 0) {
643d5c96d38SPawel Jakub Dawidek gctl_error(req, "Invalid '%s' argument", "secsize");
644d5c96d38SPawel Jakub Dawidek return;
645d5c96d38SPawel Jakub Dawidek }
646bb27d7edSEdward Tomasz Napierala }
64730738a34SChuck Silvers val = gctl_get_paraml_opt(req, "stripesize", sizeof(*val));
64830738a34SChuck Silvers if (val != NULL) {
64930738a34SChuck Silvers stripesize = *val;
65030738a34SChuck Silvers if (stripesize < 0) {
651bb27d7edSEdward Tomasz Napierala gctl_error(req, "Invalid '%s' argument", "stripesize");
652bb27d7edSEdward Tomasz Napierala return;
653bb27d7edSEdward Tomasz Napierala }
65430738a34SChuck Silvers }
65530738a34SChuck Silvers val = gctl_get_paraml_opt(req, "stripeoffset", sizeof(*val));
65630738a34SChuck Silvers if (val != NULL) {
65730738a34SChuck Silvers stripeoffset = *val;
65830738a34SChuck Silvers if (stripeoffset < 0) {
65930738a34SChuck Silvers gctl_error(req, "Invalid '%s' argument",
66030738a34SChuck Silvers "stripeoffset");
661bb27d7edSEdward Tomasz Napierala return;
662bb27d7edSEdward Tomasz Napierala }
663bb27d7edSEdward Tomasz Napierala }
6646f7f85e0SAlan Somers physpath = gctl_get_asciiparam(req, "physpath");
66580e63e0aSMariusz Zaborski gnopname = gctl_get_asciiparam(req, "gnopname");
66689aaffecSPawel Jakub Dawidek
66789aaffecSPawel Jakub Dawidek for (i = 0; i < *nargs; i++) {
66889aaffecSPawel Jakub Dawidek snprintf(param, sizeof(param), "arg%d", i);
669fcf69f3dSXin LI pp = gctl_get_provider(req, param);
670fcf69f3dSXin LI if (pp == NULL)
67189aaffecSPawel Jakub Dawidek return;
6726a146a19SPawel Jakub Dawidek if (g_nop_create(req, mp, pp,
67380e63e0aSMariusz Zaborski gnopname,
67430738a34SChuck Silvers error == -1 ? EIO : (int)error,
67530738a34SChuck Silvers count_until_fail == -1 ? 0 : (u_int)count_until_fail,
67630738a34SChuck Silvers rfailprob == -1 ? 0 : (u_int)rfailprob,
67730738a34SChuck Silvers wfailprob == -1 ? 0 : (u_int)wfailprob,
67830738a34SChuck Silvers delaymsec == -1 ? 1 : (u_int)delaymsec,
67930738a34SChuck Silvers rdelayprob == -1 ? 0 : (u_int)rdelayprob,
68030738a34SChuck Silvers wdelayprob == -1 ? 0 : (u_int)wdelayprob,
68130738a34SChuck Silvers (off_t)offset, (off_t)size, (u_int)secsize,
68230738a34SChuck Silvers (off_t)stripesize, (off_t)stripeoffset,
6836f7f85e0SAlan Somers physpath) != 0) {
68489aaffecSPawel Jakub Dawidek return;
68589aaffecSPawel Jakub Dawidek }
68689aaffecSPawel Jakub Dawidek }
687e370e911SPawel Jakub Dawidek }
68889aaffecSPawel Jakub Dawidek
68989aaffecSPawel Jakub Dawidek static void
g_nop_ctl_configure(struct gctl_req * req,struct g_class * mp)69002692c51SPawel Jakub Dawidek g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
69189aaffecSPawel Jakub Dawidek {
692e370e911SPawel Jakub Dawidek struct g_nop_softc *sc;
69389aaffecSPawel Jakub Dawidek struct g_provider *pp;
69430738a34SChuck Silvers intmax_t *val, delaymsec, error, rdelayprob, rfailprob, wdelayprob,
69530738a34SChuck Silvers wfailprob, count_until_fail;
69689aaffecSPawel Jakub Dawidek char param[16];
69789aaffecSPawel Jakub Dawidek int i, *nargs;
69889aaffecSPawel Jakub Dawidek
69989aaffecSPawel Jakub Dawidek g_topology_assert();
70089aaffecSPawel Jakub Dawidek
70130738a34SChuck Silvers count_until_fail = -1;
70230738a34SChuck Silvers delaymsec = -1;
70330738a34SChuck Silvers error = -1;
70430738a34SChuck Silvers rdelayprob = -1;
70530738a34SChuck Silvers rfailprob = -1;
70630738a34SChuck Silvers wdelayprob = -1;
70730738a34SChuck Silvers wfailprob = -1;
70830738a34SChuck Silvers
70989aaffecSPawel Jakub Dawidek nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
71089aaffecSPawel Jakub Dawidek if (nargs == NULL) {
71189aaffecSPawel Jakub Dawidek gctl_error(req, "No '%s' argument", "nargs");
71289aaffecSPawel Jakub Dawidek return;
71389aaffecSPawel Jakub Dawidek }
71489aaffecSPawel Jakub Dawidek if (*nargs <= 0) {
71589aaffecSPawel Jakub Dawidek gctl_error(req, "Missing device(s).");
71689aaffecSPawel Jakub Dawidek return;
71789aaffecSPawel Jakub Dawidek }
71830738a34SChuck Silvers val = gctl_get_paraml_opt(req, "error", sizeof(*val));
71930738a34SChuck Silvers if (val != NULL) {
72030738a34SChuck Silvers error = *val;
72189aaffecSPawel Jakub Dawidek }
72230738a34SChuck Silvers val = gctl_get_paraml_opt(req, "count_until_fail", sizeof(*val));
72330738a34SChuck Silvers if (val != NULL) {
72430738a34SChuck Silvers count_until_fail = *val;
725090a3ea3SChuck Silvers }
72630738a34SChuck Silvers val = gctl_get_paraml_opt(req, "rfailprob", sizeof(*val));
72730738a34SChuck Silvers if (val != NULL) {
72830738a34SChuck Silvers rfailprob = *val;
72930738a34SChuck Silvers if (rfailprob < -1 || rfailprob > 100) {
7306a146a19SPawel Jakub Dawidek gctl_error(req, "Invalid '%s' argument", "rfailprob");
7316a146a19SPawel Jakub Dawidek return;
7326a146a19SPawel Jakub Dawidek }
7336a146a19SPawel Jakub Dawidek }
73430738a34SChuck Silvers val = gctl_get_paraml_opt(req, "wfailprob", sizeof(*val));
73530738a34SChuck Silvers if (val != NULL) {
73630738a34SChuck Silvers wfailprob = *val;
73730738a34SChuck Silvers if (wfailprob < -1 || wfailprob > 100) {
7386a146a19SPawel Jakub Dawidek gctl_error(req, "Invalid '%s' argument", "wfailprob");
73989aaffecSPawel Jakub Dawidek return;
74089aaffecSPawel Jakub Dawidek }
7414f80c855SMariusz Zaborski }
74230738a34SChuck Silvers val = gctl_get_paraml_opt(req, "delaymsec", sizeof(*val));
74330738a34SChuck Silvers if (val != NULL) {
74430738a34SChuck Silvers delaymsec = *val;
74530738a34SChuck Silvers if (delaymsec < 1 && delaymsec != -1) {
7464f80c855SMariusz Zaborski gctl_error(req, "Invalid '%s' argument", "delaymsec");
7474f80c855SMariusz Zaborski return;
7484f80c855SMariusz Zaborski }
7494f80c855SMariusz Zaborski }
75030738a34SChuck Silvers val = gctl_get_paraml_opt(req, "rdelayprob", sizeof(*val));
75130738a34SChuck Silvers if (val != NULL) {
75230738a34SChuck Silvers rdelayprob = *val;
75330738a34SChuck Silvers if (rdelayprob < -1 || rdelayprob > 100) {
7544f80c855SMariusz Zaborski gctl_error(req, "Invalid '%s' argument", "rdelayprob");
7554f80c855SMariusz Zaborski return;
7564f80c855SMariusz Zaborski }
7574f80c855SMariusz Zaborski }
75830738a34SChuck Silvers val = gctl_get_paraml_opt(req, "wdelayprob", sizeof(*val));
75930738a34SChuck Silvers if (val != NULL) {
76030738a34SChuck Silvers wdelayprob = *val;
76130738a34SChuck Silvers if (wdelayprob < -1 || wdelayprob > 100) {
7624f80c855SMariusz Zaborski gctl_error(req, "Invalid '%s' argument", "wdelayprob");
7634f80c855SMariusz Zaborski return;
7644f80c855SMariusz Zaborski }
76530738a34SChuck Silvers }
7664f80c855SMariusz Zaborski
76789aaffecSPawel Jakub Dawidek for (i = 0; i < *nargs; i++) {
76889aaffecSPawel Jakub Dawidek snprintf(param, sizeof(param), "arg%d", i);
769fcf69f3dSXin LI pp = gctl_get_provider(req, param);
770fcf69f3dSXin LI if (pp == NULL)
77189aaffecSPawel Jakub Dawidek return;
772fcf69f3dSXin LI if (pp->geom->class != mp) {
773fcf69f3dSXin LI G_NOP_DEBUG(1, "Provider %s is invalid.", pp->name);
774fcf69f3dSXin LI gctl_error(req, "Provider %s is invalid.", pp->name);
77589aaffecSPawel Jakub Dawidek return;
77689aaffecSPawel Jakub Dawidek }
777e370e911SPawel Jakub Dawidek sc = pp->geom->softc;
77830738a34SChuck Silvers if (error != -1)
77930738a34SChuck Silvers sc->sc_error = (int)error;
78030738a34SChuck Silvers if (rfailprob != -1)
78130738a34SChuck Silvers sc->sc_rfailprob = (u_int)rfailprob;
78230738a34SChuck Silvers if (wfailprob != -1)
78330738a34SChuck Silvers sc->sc_wfailprob = (u_int)wfailprob;
78430738a34SChuck Silvers if (rdelayprob != -1)
78530738a34SChuck Silvers sc->sc_rdelayprob = (u_int)rdelayprob;
78630738a34SChuck Silvers if (wdelayprob != -1)
78730738a34SChuck Silvers sc->sc_wdelayprob = (u_int)wdelayprob;
78830738a34SChuck Silvers if (delaymsec != -1)
78930738a34SChuck Silvers sc->sc_delaymsec = (u_int)delaymsec;
79030738a34SChuck Silvers if (count_until_fail != -1)
79130738a34SChuck Silvers sc->sc_count_until_fail = (u_int)count_until_fail;
79289aaffecSPawel Jakub Dawidek }
79389aaffecSPawel Jakub Dawidek }
79489aaffecSPawel Jakub Dawidek
79589aaffecSPawel Jakub Dawidek static struct g_geom *
g_nop_find_geom(struct g_class * mp,const char * name)79689aaffecSPawel Jakub Dawidek g_nop_find_geom(struct g_class *mp, const char *name)
79789aaffecSPawel Jakub Dawidek {
79889aaffecSPawel Jakub Dawidek struct g_geom *gp;
79989aaffecSPawel Jakub Dawidek
80089aaffecSPawel Jakub Dawidek LIST_FOREACH(gp, &mp->geom, geom) {
80189aaffecSPawel Jakub Dawidek if (strcmp(gp->name, name) == 0)
80289aaffecSPawel Jakub Dawidek return (gp);
80389aaffecSPawel Jakub Dawidek }
80489aaffecSPawel Jakub Dawidek return (NULL);
80589aaffecSPawel Jakub Dawidek }
80689aaffecSPawel Jakub Dawidek
80789aaffecSPawel Jakub Dawidek static void
g_nop_ctl_destroy(struct gctl_req * req,struct g_class * mp)80889aaffecSPawel Jakub Dawidek g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
80989aaffecSPawel Jakub Dawidek {
81089aaffecSPawel Jakub Dawidek int *nargs, *force, error, i;
81189aaffecSPawel Jakub Dawidek struct g_geom *gp;
81289aaffecSPawel Jakub Dawidek const char *name;
81389aaffecSPawel Jakub Dawidek char param[16];
81489aaffecSPawel Jakub Dawidek
81589aaffecSPawel Jakub Dawidek g_topology_assert();
81689aaffecSPawel Jakub Dawidek
81789aaffecSPawel Jakub Dawidek nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
81889aaffecSPawel Jakub Dawidek if (nargs == NULL) {
81989aaffecSPawel Jakub Dawidek gctl_error(req, "No '%s' argument", "nargs");
82089aaffecSPawel Jakub Dawidek return;
82189aaffecSPawel Jakub Dawidek }
82289aaffecSPawel Jakub Dawidek if (*nargs <= 0) {
82389aaffecSPawel Jakub Dawidek gctl_error(req, "Missing device(s).");
82489aaffecSPawel Jakub Dawidek return;
82589aaffecSPawel Jakub Dawidek }
82689aaffecSPawel Jakub Dawidek force = gctl_get_paraml(req, "force", sizeof(*force));
82789aaffecSPawel Jakub Dawidek if (force == NULL) {
82889aaffecSPawel Jakub Dawidek gctl_error(req, "No 'force' argument");
82989aaffecSPawel Jakub Dawidek return;
83089aaffecSPawel Jakub Dawidek }
83189aaffecSPawel Jakub Dawidek
83289aaffecSPawel Jakub Dawidek for (i = 0; i < *nargs; i++) {
83389aaffecSPawel Jakub Dawidek snprintf(param, sizeof(param), "arg%d", i);
83489aaffecSPawel Jakub Dawidek name = gctl_get_asciiparam(req, param);
83589aaffecSPawel Jakub Dawidek if (name == NULL) {
83689aaffecSPawel Jakub Dawidek gctl_error(req, "No 'arg%d' argument", i);
83789aaffecSPawel Jakub Dawidek return;
83889aaffecSPawel Jakub Dawidek }
8398510f61aSXin LI if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0)
8408510f61aSXin LI name += strlen(_PATH_DEV);
84189aaffecSPawel Jakub Dawidek gp = g_nop_find_geom(mp, name);
84289aaffecSPawel Jakub Dawidek if (gp == NULL) {
84389aaffecSPawel Jakub Dawidek G_NOP_DEBUG(1, "Device %s is invalid.", name);
84489aaffecSPawel Jakub Dawidek gctl_error(req, "Device %s is invalid.", name);
84589aaffecSPawel Jakub Dawidek return;
84689aaffecSPawel Jakub Dawidek }
84789aaffecSPawel Jakub Dawidek error = g_nop_destroy(gp, *force);
84889aaffecSPawel Jakub Dawidek if (error != 0) {
84989aaffecSPawel Jakub Dawidek gctl_error(req, "Cannot destroy device %s (error=%d).",
85089aaffecSPawel Jakub Dawidek gp->name, error);
85189aaffecSPawel Jakub Dawidek return;
85289aaffecSPawel Jakub Dawidek }
85389aaffecSPawel Jakub Dawidek }
85489aaffecSPawel Jakub Dawidek }
85589aaffecSPawel Jakub Dawidek
85689aaffecSPawel Jakub Dawidek static void
g_nop_ctl_reset(struct gctl_req * req,struct g_class * mp)857df3d5a19SPawel Jakub Dawidek g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
858df3d5a19SPawel Jakub Dawidek {
859df3d5a19SPawel Jakub Dawidek struct g_nop_softc *sc;
860df3d5a19SPawel Jakub Dawidek struct g_provider *pp;
861df3d5a19SPawel Jakub Dawidek char param[16];
862df3d5a19SPawel Jakub Dawidek int i, *nargs;
863df3d5a19SPawel Jakub Dawidek
864df3d5a19SPawel Jakub Dawidek g_topology_assert();
865df3d5a19SPawel Jakub Dawidek
866df3d5a19SPawel Jakub Dawidek nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
867df3d5a19SPawel Jakub Dawidek if (nargs == NULL) {
868df3d5a19SPawel Jakub Dawidek gctl_error(req, "No '%s' argument", "nargs");
869df3d5a19SPawel Jakub Dawidek return;
870df3d5a19SPawel Jakub Dawidek }
871df3d5a19SPawel Jakub Dawidek if (*nargs <= 0) {
872df3d5a19SPawel Jakub Dawidek gctl_error(req, "Missing device(s).");
873df3d5a19SPawel Jakub Dawidek return;
874df3d5a19SPawel Jakub Dawidek }
875df3d5a19SPawel Jakub Dawidek
876df3d5a19SPawel Jakub Dawidek for (i = 0; i < *nargs; i++) {
877df3d5a19SPawel Jakub Dawidek snprintf(param, sizeof(param), "arg%d", i);
878fcf69f3dSXin LI pp = gctl_get_provider(req, param);
879fcf69f3dSXin LI if (pp == NULL)
880df3d5a19SPawel Jakub Dawidek return;
881fcf69f3dSXin LI if (pp->geom->class != mp) {
882fcf69f3dSXin LI G_NOP_DEBUG(1, "Provider %s is invalid.", pp->name);
883fcf69f3dSXin LI gctl_error(req, "Provider %s is invalid.", pp->name);
884df3d5a19SPawel Jakub Dawidek return;
885df3d5a19SPawel Jakub Dawidek }
886df3d5a19SPawel Jakub Dawidek sc = pp->geom->softc;
887df3d5a19SPawel Jakub Dawidek sc->sc_reads = 0;
888df3d5a19SPawel Jakub Dawidek sc->sc_writes = 0;
88945d7de1dSEdward Tomasz Napierala sc->sc_deletes = 0;
89045d7de1dSEdward Tomasz Napierala sc->sc_getattrs = 0;
89145d7de1dSEdward Tomasz Napierala sc->sc_flushes = 0;
8928b522bdaSWarner Losh sc->sc_speedups = 0;
89345d7de1dSEdward Tomasz Napierala sc->sc_cmd0s = 0;
89445d7de1dSEdward Tomasz Napierala sc->sc_cmd1s = 0;
89545d7de1dSEdward Tomasz Napierala sc->sc_cmd2s = 0;
896df3d5a19SPawel Jakub Dawidek sc->sc_readbytes = 0;
897df3d5a19SPawel Jakub Dawidek sc->sc_wrotebytes = 0;
898df3d5a19SPawel Jakub Dawidek }
899df3d5a19SPawel Jakub Dawidek }
900df3d5a19SPawel Jakub Dawidek
901df3d5a19SPawel Jakub Dawidek static void
g_nop_config(struct gctl_req * req,struct g_class * mp,const char * verb)90289aaffecSPawel Jakub Dawidek g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
90389aaffecSPawel Jakub Dawidek {
90489aaffecSPawel Jakub Dawidek uint32_t *version;
90589aaffecSPawel Jakub Dawidek
90689aaffecSPawel Jakub Dawidek g_topology_assert();
90789aaffecSPawel Jakub Dawidek
90889aaffecSPawel Jakub Dawidek version = gctl_get_paraml(req, "version", sizeof(*version));
90989aaffecSPawel Jakub Dawidek if (version == NULL) {
91089aaffecSPawel Jakub Dawidek gctl_error(req, "No '%s' argument.", "version");
91189aaffecSPawel Jakub Dawidek return;
91289aaffecSPawel Jakub Dawidek }
91389aaffecSPawel Jakub Dawidek if (*version != G_NOP_VERSION) {
91489aaffecSPawel Jakub Dawidek gctl_error(req, "Userland and kernel parts are out of sync.");
91589aaffecSPawel Jakub Dawidek return;
91689aaffecSPawel Jakub Dawidek }
91789aaffecSPawel Jakub Dawidek
91889aaffecSPawel Jakub Dawidek if (strcmp(verb, "create") == 0) {
91989aaffecSPawel Jakub Dawidek g_nop_ctl_create(req, mp);
92089aaffecSPawel Jakub Dawidek return;
92102692c51SPawel Jakub Dawidek } else if (strcmp(verb, "configure") == 0) {
92202692c51SPawel Jakub Dawidek g_nop_ctl_configure(req, mp);
92389aaffecSPawel Jakub Dawidek return;
92489aaffecSPawel Jakub Dawidek } else if (strcmp(verb, "destroy") == 0) {
92589aaffecSPawel Jakub Dawidek g_nop_ctl_destroy(req, mp);
92689aaffecSPawel Jakub Dawidek return;
927df3d5a19SPawel Jakub Dawidek } else if (strcmp(verb, "reset") == 0) {
928df3d5a19SPawel Jakub Dawidek g_nop_ctl_reset(req, mp);
929df3d5a19SPawel Jakub Dawidek return;
93089aaffecSPawel Jakub Dawidek }
93189aaffecSPawel Jakub Dawidek
93289aaffecSPawel Jakub Dawidek gctl_error(req, "Unknown verb.");
93389aaffecSPawel Jakub Dawidek }
93489aaffecSPawel Jakub Dawidek
93589aaffecSPawel Jakub Dawidek static void
g_nop_dumpconf(struct sbuf * sb,const char * indent,struct g_geom * gp,struct g_consumer * cp,struct g_provider * pp)93689aaffecSPawel Jakub Dawidek g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
93789aaffecSPawel Jakub Dawidek struct g_consumer *cp, struct g_provider *pp)
93889aaffecSPawel Jakub Dawidek {
939e370e911SPawel Jakub Dawidek struct g_nop_softc *sc;
94089aaffecSPawel Jakub Dawidek
9411d723f1dSPawel Jakub Dawidek if (pp != NULL || cp != NULL)
942e370e911SPawel Jakub Dawidek return;
943e370e911SPawel Jakub Dawidek sc = gp->softc;
9441d723f1dSPawel Jakub Dawidek sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
945e370e911SPawel Jakub Dawidek (intmax_t)sc->sc_offset);
9466a146a19SPawel Jakub Dawidek sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
9476a146a19SPawel Jakub Dawidek sc->sc_rfailprob);
9486a146a19SPawel Jakub Dawidek sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
9496a146a19SPawel Jakub Dawidek sc->sc_wfailprob);
9504f80c855SMariusz Zaborski sbuf_printf(sb, "%s<ReadDelayedProb>%u</ReadDelayedProb>\n", indent,
9514f80c855SMariusz Zaborski sc->sc_rdelayprob);
9524f80c855SMariusz Zaborski sbuf_printf(sb, "%s<WriteDelayedProb>%u</WriteDelayedProb>\n", indent,
9534f80c855SMariusz Zaborski sc->sc_wdelayprob);
9544f80c855SMariusz Zaborski sbuf_printf(sb, "%s<Delay>%d</Delay>\n", indent, sc->sc_delaymsec);
955090a3ea3SChuck Silvers sbuf_printf(sb, "%s<CountUntilFail>%u</CountUntilFail>\n", indent,
956090a3ea3SChuck Silvers sc->sc_count_until_fail);
95746ee0837SPawel Jakub Dawidek sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
958df3d5a19SPawel Jakub Dawidek sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
959df3d5a19SPawel Jakub Dawidek sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
96045d7de1dSEdward Tomasz Napierala sbuf_printf(sb, "%s<Deletes>%ju</Deletes>\n", indent, sc->sc_deletes);
96145d7de1dSEdward Tomasz Napierala sbuf_printf(sb, "%s<Getattrs>%ju</Getattrs>\n", indent, sc->sc_getattrs);
96245d7de1dSEdward Tomasz Napierala sbuf_printf(sb, "%s<Flushes>%ju</Flushes>\n", indent, sc->sc_flushes);
9638b522bdaSWarner Losh sbuf_printf(sb, "%s<Speedups>%ju</Speedups>\n", indent, sc->sc_speedups);
96445d7de1dSEdward Tomasz Napierala sbuf_printf(sb, "%s<Cmd0s>%ju</Cmd0s>\n", indent, sc->sc_cmd0s);
96545d7de1dSEdward Tomasz Napierala sbuf_printf(sb, "%s<Cmd1s>%ju</Cmd1s>\n", indent, sc->sc_cmd1s);
96645d7de1dSEdward Tomasz Napierala sbuf_printf(sb, "%s<Cmd2s>%ju</Cmd2s>\n", indent, sc->sc_cmd2s);
967b91df0e2SPawel Jakub Dawidek sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
968df3d5a19SPawel Jakub Dawidek sc->sc_readbytes);
969df3d5a19SPawel Jakub Dawidek sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
97064806a73SPawel Jakub Dawidek sc->sc_wrotebytes);
97189aaffecSPawel Jakub Dawidek }
97289aaffecSPawel Jakub Dawidek
97389aaffecSPawel Jakub Dawidek DECLARE_GEOM_CLASS(g_nop_class, g_nop);
97474d6c131SKyle Evans MODULE_VERSION(geom_nop, 0);
975