]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/nop/g_nop.c
Import libxo-1.4.0:
[FreeBSD/FreeBSD.git] / sys / geom / nop / g_nop.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * Copyright (c) 2019 Mariusz Zaborski <oshogbo@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34 #include <sys/ctype.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/bio.h>
41 #include <sys/sbuf.h>
42 #include <sys/sysctl.h>
43 #include <sys/malloc.h>
44 #include <geom/geom.h>
45 #include <geom/geom_dbg.h>
46 #include <geom/nop/g_nop.h>
47
48
49 SYSCTL_DECL(_kern_geom);
50 static SYSCTL_NODE(_kern_geom, OID_AUTO, nop, CTLFLAG_RW, 0, "GEOM_NOP stuff");
51 static u_int g_nop_debug = 0;
52 SYSCTL_UINT(_kern_geom_nop, OID_AUTO, debug, CTLFLAG_RW, &g_nop_debug, 0,
53     "Debug level");
54
55 static int g_nop_destroy(struct g_geom *gp, boolean_t force);
56 static int g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp,
57     struct g_geom *gp);
58 static void g_nop_config(struct gctl_req *req, struct g_class *mp,
59     const char *verb);
60 static g_access_t g_nop_access;
61 static g_dumpconf_t g_nop_dumpconf;
62 static g_orphan_t g_nop_orphan;
63 static g_provgone_t g_nop_providergone;
64 static g_resize_t g_nop_resize;
65 static g_start_t g_nop_start;
66
67 struct g_class g_nop_class = {
68         .name = G_NOP_CLASS_NAME,
69         .version = G_VERSION,
70         .ctlreq = g_nop_config,
71         .destroy_geom = g_nop_destroy_geom,
72         .access = g_nop_access,
73         .dumpconf = g_nop_dumpconf,
74         .orphan = g_nop_orphan,
75         .providergone = g_nop_providergone,
76         .resize = g_nop_resize,
77         .start = g_nop_start,
78 };
79
80 struct g_nop_delay {
81         struct callout                   dl_cal;
82         struct bio                      *dl_bio;
83         TAILQ_ENTRY(g_nop_delay)         dl_next;
84 };
85
86 static bool
87 g_nop_verify_nprefix(const char *name)
88 {
89         int i;
90
91         for (i = 0; i < strlen(name); i++) {
92                 if (isalpha(name[i]) == 0 && isdigit(name[i]) == 0) {
93                         return (false);
94                 }
95         }
96
97         return (true);
98 }
99
100 static void
101 g_nop_orphan(struct g_consumer *cp)
102 {
103
104         g_topology_assert();
105         g_nop_destroy(cp->geom, 1);
106 }
107
108 static void
109 g_nop_resize(struct g_consumer *cp)
110 {
111         struct g_nop_softc *sc;
112         struct g_geom *gp;
113         struct g_provider *pp;
114         off_t size;
115
116         g_topology_assert();
117
118         gp = cp->geom;
119         sc = gp->softc;
120
121         if (sc->sc_explicitsize != 0)
122                 return;
123         if (cp->provider->mediasize < sc->sc_offset) {
124                 g_nop_destroy(gp, 1);
125                 return;
126         }
127         size = cp->provider->mediasize - sc->sc_offset;
128         LIST_FOREACH(pp, &gp->provider, provider)
129                 g_resize_provider(pp, size);
130 }
131
132 static int
133 g_nop_dumper(void *priv, void *virtual, vm_offset_t physical, off_t offset,
134     size_t length)
135 {
136
137         return (0);
138 }
139
140 static void
141 g_nop_kerneldump(struct bio *bp, struct g_nop_softc *sc)
142 {
143         struct g_kerneldump *gkd;
144         struct g_geom *gp;
145         struct g_provider *pp;
146
147         gkd = (struct g_kerneldump *)bp->bio_data;
148         gp = bp->bio_to->geom;
149         g_trace(G_T_TOPOLOGY, "%s(%s, %jd, %jd)", __func__, gp->name,
150             (intmax_t)gkd->offset, (intmax_t)gkd->length);
151
152         pp = LIST_FIRST(&gp->provider);
153
154         gkd->di.dumper = g_nop_dumper;
155         gkd->di.priv = sc;
156         gkd->di.blocksize = pp->sectorsize;
157         gkd->di.maxiosize = DFLTPHYS;
158         gkd->di.mediaoffset = sc->sc_offset + gkd->offset;
159         if (gkd->offset > sc->sc_explicitsize) {
160                 g_io_deliver(bp, ENODEV);
161                 return;
162         }
163         if (gkd->offset + gkd->length > sc->sc_explicitsize)
164                 gkd->length = sc->sc_explicitsize - gkd->offset;
165         gkd->di.mediasize = gkd->length;
166         g_io_deliver(bp, 0);
167 }
168
169 static void
170 g_nop_pass(struct bio *cbp, struct g_geom *gp)
171 {
172
173         G_NOP_LOGREQ(cbp, "Sending request.");
174         g_io_request(cbp, LIST_FIRST(&gp->consumer));
175 }
176
177 static void
178 g_nop_pass_timeout(void *data)
179 {
180         struct g_nop_softc *sc;
181         struct g_geom *gp;
182         struct g_nop_delay *gndelay;
183
184         gndelay = (struct g_nop_delay *)data;
185
186         gp = gndelay->dl_bio->bio_to->geom;
187         sc = gp->softc;
188
189         mtx_lock(&sc->sc_lock);
190         TAILQ_REMOVE(&sc->sc_head_delay, gndelay, dl_next);
191         mtx_unlock(&sc->sc_lock);
192
193         g_nop_pass(gndelay->dl_bio, gp);
194
195         g_free(data);
196 }
197
198 static void
199 g_nop_start(struct bio *bp)
200 {
201         struct g_nop_softc *sc;
202         struct g_geom *gp;
203         struct g_provider *pp;
204         struct bio *cbp;
205         u_int failprob, delayprob, delaytime;
206
207         failprob = delayprob = delaytime = 0;
208
209         gp = bp->bio_to->geom;
210         sc = gp->softc;
211
212         G_NOP_LOGREQ(bp, "Request received.");
213         mtx_lock(&sc->sc_lock);
214         switch (bp->bio_cmd) {
215         case BIO_READ:
216                 sc->sc_reads++;
217                 sc->sc_readbytes += bp->bio_length;
218                 if (sc->sc_count_until_fail != 0) {
219                         sc->sc_count_until_fail -= 1;
220                 } else {
221                         failprob = sc->sc_rfailprob;
222                         delayprob = sc->sc_rdelayprob;
223                         delaytime = sc->sc_delaymsec;
224                 }
225                 break;
226         case BIO_WRITE:
227                 sc->sc_writes++;
228                 sc->sc_wrotebytes += bp->bio_length;
229                 if (sc->sc_count_until_fail != 0) {
230                         sc->sc_count_until_fail -= 1;
231                 } else {
232                         failprob = sc->sc_wfailprob;
233                         delayprob = sc->sc_wdelayprob;
234                         delaytime = sc->sc_delaymsec;
235                 }
236                 break;
237         case BIO_DELETE:
238                 sc->sc_deletes++;
239                 break;
240         case BIO_GETATTR:
241                 sc->sc_getattrs++;
242                 if (sc->sc_physpath &&
243                     g_handleattr_str(bp, "GEOM::physpath", sc->sc_physpath))
244                         ;
245                 else if (strcmp(bp->bio_attribute, "GEOM::kerneldump") == 0)
246                         g_nop_kerneldump(bp, sc);
247                 else
248                         /*
249                          * Fallthrough to forwarding the GETATTR down to the
250                          * lower level device.
251                          */
252                         break;
253                 mtx_unlock(&sc->sc_lock);
254                 return;
255         case BIO_FLUSH:
256                 sc->sc_flushes++;
257                 break;
258         case BIO_SPEEDUP:
259                 sc->sc_speedups++;
260                 break;
261         case BIO_CMD0:
262                 sc->sc_cmd0s++;
263                 break;
264         case BIO_CMD1:
265                 sc->sc_cmd1s++;
266                 break;
267         case BIO_CMD2:
268                 sc->sc_cmd2s++;
269                 break;
270         }
271         mtx_unlock(&sc->sc_lock);
272
273         if (failprob > 0) {
274                 u_int rval;
275
276                 rval = arc4random() % 100;
277                 if (rval < failprob) {
278                         G_NOP_LOGREQLVL(1, bp, "Returning error=%d.", sc->sc_error);
279                         g_io_deliver(bp, sc->sc_error);
280                         return;
281                 }
282         }
283
284         cbp = g_clone_bio(bp);
285         if (cbp == NULL) {
286                 g_io_deliver(bp, ENOMEM);
287                 return;
288         }
289         cbp->bio_done = g_std_done;
290         cbp->bio_offset = bp->bio_offset + sc->sc_offset;
291         pp = LIST_FIRST(&gp->provider);
292         KASSERT(pp != NULL, ("NULL pp"));
293         cbp->bio_to = pp;
294
295         if (delayprob > 0) {
296                 struct g_nop_delay *gndelay;
297                 u_int rval;
298
299                 rval = arc4random() % 100;
300                 if (rval < delayprob) {
301                         gndelay = g_malloc(sizeof(*gndelay), M_NOWAIT | M_ZERO);
302                         if (gndelay != NULL) {
303                                 callout_init(&gndelay->dl_cal, 1);
304
305                                 gndelay->dl_bio = cbp;
306
307                                 mtx_lock(&sc->sc_lock);
308                                 TAILQ_INSERT_TAIL(&sc->sc_head_delay, gndelay,
309                                     dl_next);
310                                 mtx_unlock(&sc->sc_lock);
311
312                                 callout_reset(&gndelay->dl_cal,
313                                     MSEC_2_TICKS(delaytime), g_nop_pass_timeout,
314                                     gndelay);
315                                 return;
316                         }
317                 }
318         }
319
320         g_nop_pass(cbp, gp);
321 }
322
323 static int
324 g_nop_access(struct g_provider *pp, int dr, int dw, int de)
325 {
326         struct g_geom *gp;
327         struct g_consumer *cp;
328         int error;
329
330         gp = pp->geom;
331         cp = LIST_FIRST(&gp->consumer);
332         error = g_access(cp, dr, dw, de);
333
334         return (error);
335 }
336
337 static int
338 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
339     const char *gnopname, int ioerror, u_int count_until_fail,
340     u_int rfailprob, u_int wfailprob, u_int delaymsec, u_int rdelayprob,
341     u_int wdelayprob, off_t offset, off_t size, u_int secsize, off_t stripesize,
342     off_t stripeoffset, const char *physpath)
343 {
344         struct g_nop_softc *sc;
345         struct g_geom *gp;
346         struct g_provider *newpp;
347         struct g_consumer *cp;
348         char name[64];
349         int error, n;
350         off_t explicitsize;
351
352         g_topology_assert();
353
354         gp = NULL;
355         newpp = NULL;
356         cp = NULL;
357
358         if ((offset % pp->sectorsize) != 0) {
359                 gctl_error(req, "Invalid offset for provider %s.", pp->name);
360                 return (EINVAL);
361         }
362         if ((size % pp->sectorsize) != 0) {
363                 gctl_error(req, "Invalid size for provider %s.", pp->name);
364                 return (EINVAL);
365         }
366         if (offset >= pp->mediasize) {
367                 gctl_error(req, "Invalid offset for provider %s.", pp->name);
368                 return (EINVAL);
369         }
370         explicitsize = size;
371         if (size == 0)
372                 size = pp->mediasize - offset;
373         if (offset + size > pp->mediasize) {
374                 gctl_error(req, "Invalid size for provider %s.", pp->name);
375                 return (EINVAL);
376         }
377         if (secsize == 0)
378                 secsize = pp->sectorsize;
379         else if ((secsize % pp->sectorsize) != 0) {
380                 gctl_error(req, "Invalid secsize for provider %s.", pp->name);
381                 return (EINVAL);
382         }
383         if (secsize > MAXPHYS) {
384                 gctl_error(req, "secsize is too big.");
385                 return (EINVAL);
386         }
387         size -= size % secsize;
388         if ((stripesize % pp->sectorsize) != 0) {
389                 gctl_error(req, "Invalid stripesize for provider %s.", pp->name);
390                 return (EINVAL);
391         }
392         if ((stripeoffset % pp->sectorsize) != 0) {
393                 gctl_error(req, "Invalid stripeoffset for provider %s.", pp->name);
394                 return (EINVAL);
395         }
396         if (stripesize != 0 && stripeoffset >= stripesize) {
397                 gctl_error(req, "stripeoffset is too big.");
398                 return (EINVAL);
399         }
400         if (gnopname != NULL && !g_nop_verify_nprefix(gnopname)) {
401                 gctl_error(req, "Name %s is invalid.", gnopname);
402                 return (EINVAL);
403         }
404
405         if (gnopname != NULL) {
406                 n = snprintf(name, sizeof(name), "%s%s", gnopname,
407                     G_NOP_SUFFIX);
408         } else {
409                 n = snprintf(name, sizeof(name), "%s%s", pp->name,
410                     G_NOP_SUFFIX);
411         }
412         if (n <= 0 || n >= sizeof(name)) {
413                 gctl_error(req, "Invalid provider name.");
414                 return (EINVAL);
415         }
416         LIST_FOREACH(gp, &mp->geom, geom) {
417                 if (strcmp(gp->name, name) == 0) {
418                         gctl_error(req, "Provider %s already exists.", name);
419                         return (EEXIST);
420                 }
421         }
422         gp = g_new_geomf(mp, "%s", name);
423         sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
424         sc->sc_offset = offset;
425         sc->sc_explicitsize = explicitsize;
426         sc->sc_stripesize = stripesize;
427         sc->sc_stripeoffset = stripeoffset;
428         if (physpath && strcmp(physpath, G_NOP_PHYSPATH_PASSTHROUGH)) {
429                 sc->sc_physpath = strndup(physpath, MAXPATHLEN, M_GEOM);
430         } else
431                 sc->sc_physpath = NULL;
432         sc->sc_error = ioerror;
433         sc->sc_count_until_fail = count_until_fail;
434         sc->sc_rfailprob = rfailprob;
435         sc->sc_wfailprob = wfailprob;
436         sc->sc_delaymsec = delaymsec;
437         sc->sc_rdelayprob = rdelayprob;
438         sc->sc_wdelayprob = wdelayprob;
439         sc->sc_reads = 0;
440         sc->sc_writes = 0;
441         sc->sc_deletes = 0;
442         sc->sc_getattrs = 0;
443         sc->sc_flushes = 0;
444         sc->sc_speedups = 0;
445         sc->sc_cmd0s = 0;
446         sc->sc_cmd1s = 0;
447         sc->sc_cmd2s = 0;
448         sc->sc_readbytes = 0;
449         sc->sc_wrotebytes = 0;
450         TAILQ_INIT(&sc->sc_head_delay);
451         mtx_init(&sc->sc_lock, "gnop lock", NULL, MTX_DEF);
452         gp->softc = sc;
453
454         newpp = g_new_providerf(gp, "%s", gp->name);
455         newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
456         newpp->mediasize = size;
457         newpp->sectorsize = secsize;
458         newpp->stripesize = stripesize;
459         newpp->stripeoffset = stripeoffset;
460
461         cp = g_new_consumer(gp);
462         cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
463         error = g_attach(cp, pp);
464         if (error != 0) {
465                 gctl_error(req, "Cannot attach to provider %s.", pp->name);
466                 goto fail;
467         }
468
469         newpp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
470         g_error_provider(newpp, 0);
471         G_NOP_DEBUG(0, "Device %s created.", gp->name);
472         return (0);
473 fail:
474         if (cp->provider != NULL)
475                 g_detach(cp);
476         g_destroy_consumer(cp);
477         g_destroy_provider(newpp);
478         mtx_destroy(&sc->sc_lock);
479         free(sc->sc_physpath, M_GEOM);
480         g_free(gp->softc);
481         g_destroy_geom(gp);
482         return (error);
483 }
484
485 static void
486 g_nop_providergone(struct g_provider *pp)
487 {
488         struct g_geom *gp = pp->geom;
489         struct g_nop_softc *sc = gp->softc;
490
491         KASSERT(TAILQ_EMPTY(&sc->sc_head_delay),
492             ("delayed request list is not empty"));
493
494         gp->softc = NULL;
495         free(sc->sc_physpath, M_GEOM);
496         mtx_destroy(&sc->sc_lock);
497         g_free(sc);
498 }
499
500 static int
501 g_nop_destroy(struct g_geom *gp, boolean_t force)
502 {
503         struct g_nop_softc *sc;
504         struct g_provider *pp;
505
506         g_topology_assert();
507         sc = gp->softc;
508         if (sc == NULL)
509                 return (ENXIO);
510         pp = LIST_FIRST(&gp->provider);
511         if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
512                 if (force) {
513                         G_NOP_DEBUG(0, "Device %s is still open, so it "
514                             "can't be definitely removed.", pp->name);
515                 } else {
516                         G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
517                             pp->name, pp->acr, pp->acw, pp->ace);
518                         return (EBUSY);
519                 }
520         } else {
521                 G_NOP_DEBUG(0, "Device %s removed.", gp->name);
522         }
523
524         g_wither_geom(gp, ENXIO);
525
526         return (0);
527 }
528
529 static int
530 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
531 {
532
533         return (g_nop_destroy(gp, 0));
534 }
535
536 static void
537 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
538 {
539         struct g_provider *pp;
540         intmax_t *val, error, rfailprob, wfailprob, count_until_fail, offset,
541             secsize, size, stripesize, stripeoffset, delaymsec,
542             rdelayprob, wdelayprob;
543         const char *name, *physpath, *gnopname;
544         char param[16];
545         int i, *nargs;
546
547         g_topology_assert();
548
549         error = -1;
550         rfailprob = -1;
551         wfailprob = -1;
552         count_until_fail = -1;
553         offset = 0;
554         secsize = 0;
555         size = 0;
556         stripesize = 0;
557         stripeoffset = 0;
558         delaymsec = -1;
559         rdelayprob = -1;
560         wdelayprob = -1;
561
562         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
563         if (nargs == NULL) {
564                 gctl_error(req, "No '%s' argument", "nargs");
565                 return;
566         }
567         if (*nargs <= 0) {
568                 gctl_error(req, "Missing device(s).");
569                 return;
570         }
571         val = gctl_get_paraml_opt(req, "error", sizeof(*val));
572         if (val != NULL) {
573                 error = *val;
574         }
575         val = gctl_get_paraml_opt(req, "rfailprob", sizeof(*val));
576         if (val != NULL) {
577                 rfailprob = *val;
578                 if (rfailprob < -1 || rfailprob > 100) {
579                         gctl_error(req, "Invalid '%s' argument", "rfailprob");
580                         return;
581                 }
582         }
583         val = gctl_get_paraml_opt(req, "wfailprob", sizeof(*val));
584         if (val != NULL) {
585                 wfailprob = *val;
586                 if (wfailprob < -1 || wfailprob > 100) {
587                         gctl_error(req, "Invalid '%s' argument", "wfailprob");
588                         return;
589                 }
590         }
591         val = gctl_get_paraml_opt(req, "delaymsec", sizeof(*val));
592         if (val != NULL) {
593                 delaymsec = *val;
594                 if (delaymsec < 1 && delaymsec != -1) {
595                         gctl_error(req, "Invalid '%s' argument", "delaymsec");
596                         return;
597                 }
598         }
599         val = gctl_get_paraml_opt(req, "rdelayprob", sizeof(*val));
600         if (val != NULL) {
601                 rdelayprob = *val;
602                 if (rdelayprob < -1 || rdelayprob > 100) {
603                         gctl_error(req, "Invalid '%s' argument", "rdelayprob");
604                         return;
605                 }
606         }
607         val = gctl_get_paraml_opt(req, "wdelayprob", sizeof(*val));
608         if (val != NULL) {
609                 wdelayprob = *val;
610                 if (wdelayprob < -1 || wdelayprob > 100) {
611                         gctl_error(req, "Invalid '%s' argument", "wdelayprob");
612                         return;
613                 }
614         }
615         val = gctl_get_paraml_opt(req, "count_until_fail", sizeof(*val));
616         if (val != NULL) {
617                 count_until_fail = *val;
618                 if (count_until_fail < -1) {
619                         gctl_error(req, "Invalid '%s' argument",
620                             "count_until_fail");
621                         return;
622                 }
623         }
624         val = gctl_get_paraml_opt(req, "offset", sizeof(*val));
625         if (val != NULL) {
626                 offset = *val;
627                 if (offset < 0) {
628                         gctl_error(req, "Invalid '%s' argument", "offset");
629                         return;
630                 }
631         }
632         val = gctl_get_paraml_opt(req, "size", sizeof(*val));
633         if (val != NULL) {
634                 size = *val;
635                 if (size < 0) {
636                         gctl_error(req, "Invalid '%s' argument", "size");
637                         return;
638                 }
639         }
640         val = gctl_get_paraml_opt(req, "secsize", sizeof(*val));
641         if (val != NULL) {
642                 secsize = *val;
643                 if (secsize < 0) {
644                         gctl_error(req, "Invalid '%s' argument", "secsize");
645                         return;
646                 }
647         }
648         val = gctl_get_paraml_opt(req, "stripesize", sizeof(*val));
649         if (val != NULL) {
650                 stripesize = *val;
651                 if (stripesize < 0) {
652                         gctl_error(req, "Invalid '%s' argument", "stripesize");
653                         return;
654                 }
655         }
656         val = gctl_get_paraml_opt(req, "stripeoffset", sizeof(*val));
657         if (val != NULL) {
658                 stripeoffset = *val;
659                 if (stripeoffset < 0) {
660                         gctl_error(req, "Invalid '%s' argument",
661                             "stripeoffset");
662                         return;
663                 }
664         }
665         physpath = gctl_get_asciiparam(req, "physpath");
666         gnopname = gctl_get_asciiparam(req, "gnopname");
667
668         for (i = 0; i < *nargs; i++) {
669                 snprintf(param, sizeof(param), "arg%d", i);
670                 name = gctl_get_asciiparam(req, param);
671                 if (name == NULL) {
672                         gctl_error(req, "No 'arg%d' argument", i);
673                         return;
674                 }
675                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
676                         name += strlen("/dev/");
677                 pp = g_provider_by_name(name);
678                 if (pp == NULL) {
679                         G_NOP_DEBUG(1, "Provider %s is invalid.", name);
680                         gctl_error(req, "Provider %s is invalid.", name);
681                         return;
682                 }
683                 if (g_nop_create(req, mp, pp,
684                     gnopname,
685                     error == -1 ? EIO : (int)error,
686                     count_until_fail == -1 ? 0 : (u_int)count_until_fail,
687                     rfailprob == -1 ? 0 : (u_int)rfailprob,
688                     wfailprob == -1 ? 0 : (u_int)wfailprob,
689                     delaymsec == -1 ? 1 : (u_int)delaymsec,
690                     rdelayprob == -1 ? 0 : (u_int)rdelayprob,
691                     wdelayprob == -1 ? 0 : (u_int)wdelayprob,
692                     (off_t)offset, (off_t)size, (u_int)secsize,
693                     (off_t)stripesize, (off_t)stripeoffset,
694                     physpath) != 0) {
695                         return;
696                 }
697         }
698 }
699
700 static void
701 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
702 {
703         struct g_nop_softc *sc;
704         struct g_provider *pp;
705         intmax_t *val, delaymsec, error, rdelayprob, rfailprob, wdelayprob,
706             wfailprob, count_until_fail;
707         const char *name;
708         char param[16];
709         int i, *nargs;
710
711         g_topology_assert();
712
713         count_until_fail = -1;
714         delaymsec = -1;
715         error = -1;
716         rdelayprob = -1;
717         rfailprob = -1;
718         wdelayprob = -1;
719         wfailprob = -1;
720
721         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
722         if (nargs == NULL) {
723                 gctl_error(req, "No '%s' argument", "nargs");
724                 return;
725         }
726         if (*nargs <= 0) {
727                 gctl_error(req, "Missing device(s).");
728                 return;
729         }
730         val = gctl_get_paraml_opt(req, "error", sizeof(*val));
731         if (val != NULL) {
732                 error = *val;
733         }
734         val = gctl_get_paraml_opt(req, "count_until_fail", sizeof(*val));
735         if (val != NULL) {
736                 count_until_fail = *val;
737         }
738         val = gctl_get_paraml_opt(req, "rfailprob", sizeof(*val));
739         if (val != NULL) {
740                 rfailprob = *val;
741                 if (rfailprob < -1 || rfailprob > 100) {
742                         gctl_error(req, "Invalid '%s' argument", "rfailprob");
743                         return;
744                 }
745         }
746         val = gctl_get_paraml_opt(req, "wfailprob", sizeof(*val));
747         if (val != NULL) {
748                 wfailprob = *val;
749                 if (wfailprob < -1 || wfailprob > 100) {
750                         gctl_error(req, "Invalid '%s' argument", "wfailprob");
751                         return;
752                 }
753         }
754         val = gctl_get_paraml_opt(req, "delaymsec", sizeof(*val));
755         if (val != NULL) {
756                 delaymsec = *val;
757                 if (delaymsec < 1 && delaymsec != -1) {
758                         gctl_error(req, "Invalid '%s' argument", "delaymsec");
759                         return;
760                 }
761         }
762         val = gctl_get_paraml_opt(req, "rdelayprob", sizeof(*val));
763         if (val != NULL) {
764                 rdelayprob = *val;
765                 if (rdelayprob < -1 || rdelayprob > 100) {
766                         gctl_error(req, "Invalid '%s' argument", "rdelayprob");
767                         return;
768                 }
769         }
770         val = gctl_get_paraml_opt(req, "wdelayprob", sizeof(*val));
771         if (val != NULL) {
772                 wdelayprob = *val;
773                 if (wdelayprob < -1 || wdelayprob > 100) {
774                         gctl_error(req, "Invalid '%s' argument", "wdelayprob");
775                         return;
776                 }
777         }
778
779         for (i = 0; i < *nargs; i++) {
780                 snprintf(param, sizeof(param), "arg%d", i);
781                 name = gctl_get_asciiparam(req, param);
782                 if (name == NULL) {
783                         gctl_error(req, "No 'arg%d' argument", i);
784                         return;
785                 }
786                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
787                         name += strlen("/dev/");
788                 pp = g_provider_by_name(name);
789                 if (pp == NULL || pp->geom->class != mp) {
790                         G_NOP_DEBUG(1, "Provider %s is invalid.", name);
791                         gctl_error(req, "Provider %s is invalid.", name);
792                         return;
793                 }
794                 sc = pp->geom->softc;
795                 if (error != -1)
796                         sc->sc_error = (int)error;
797                 if (rfailprob != -1)
798                         sc->sc_rfailprob = (u_int)rfailprob;
799                 if (wfailprob != -1)
800                         sc->sc_wfailprob = (u_int)wfailprob;
801                 if (rdelayprob != -1)
802                         sc->sc_rdelayprob = (u_int)rdelayprob;
803                 if (wdelayprob != -1)
804                         sc->sc_wdelayprob = (u_int)wdelayprob;
805                 if (delaymsec != -1)
806                         sc->sc_delaymsec = (u_int)delaymsec;
807                 if (count_until_fail != -1)
808                         sc->sc_count_until_fail = (u_int)count_until_fail;
809         }
810 }
811
812 static struct g_geom *
813 g_nop_find_geom(struct g_class *mp, const char *name)
814 {
815         struct g_geom *gp;
816
817         LIST_FOREACH(gp, &mp->geom, geom) {
818                 if (strcmp(gp->name, name) == 0)
819                         return (gp);
820         }
821         return (NULL);
822 }
823
824 static void
825 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
826 {
827         int *nargs, *force, error, i;
828         struct g_geom *gp;
829         const char *name;
830         char param[16];
831
832         g_topology_assert();
833
834         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
835         if (nargs == NULL) {
836                 gctl_error(req, "No '%s' argument", "nargs");
837                 return;
838         }
839         if (*nargs <= 0) {
840                 gctl_error(req, "Missing device(s).");
841                 return;
842         }
843         force = gctl_get_paraml(req, "force", sizeof(*force));
844         if (force == NULL) {
845                 gctl_error(req, "No 'force' argument");
846                 return;
847         }
848
849         for (i = 0; i < *nargs; i++) {
850                 snprintf(param, sizeof(param), "arg%d", i);
851                 name = gctl_get_asciiparam(req, param);
852                 if (name == NULL) {
853                         gctl_error(req, "No 'arg%d' argument", i);
854                         return;
855                 }
856                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
857                         name += strlen("/dev/");
858                 gp = g_nop_find_geom(mp, name);
859                 if (gp == NULL) {
860                         G_NOP_DEBUG(1, "Device %s is invalid.", name);
861                         gctl_error(req, "Device %s is invalid.", name);
862                         return;
863                 }
864                 error = g_nop_destroy(gp, *force);
865                 if (error != 0) {
866                         gctl_error(req, "Cannot destroy device %s (error=%d).",
867                             gp->name, error);
868                         return;
869                 }
870         }
871 }
872
873 static void
874 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
875 {
876         struct g_nop_softc *sc;
877         struct g_provider *pp;
878         const char *name;
879         char param[16];
880         int i, *nargs;
881
882         g_topology_assert();
883
884         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
885         if (nargs == NULL) {
886                 gctl_error(req, "No '%s' argument", "nargs");
887                 return;
888         }
889         if (*nargs <= 0) {
890                 gctl_error(req, "Missing device(s).");
891                 return;
892         }
893
894         for (i = 0; i < *nargs; i++) {
895                 snprintf(param, sizeof(param), "arg%d", i);
896                 name = gctl_get_asciiparam(req, param);
897                 if (name == NULL) {
898                         gctl_error(req, "No 'arg%d' argument", i);
899                         return;
900                 }
901                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
902                         name += strlen("/dev/");
903                 pp = g_provider_by_name(name);
904                 if (pp == NULL || pp->geom->class != mp) {
905                         G_NOP_DEBUG(1, "Provider %s is invalid.", name);
906                         gctl_error(req, "Provider %s is invalid.", name);
907                         return;
908                 }
909                 sc = pp->geom->softc;
910                 sc->sc_reads = 0;
911                 sc->sc_writes = 0;
912                 sc->sc_deletes = 0;
913                 sc->sc_getattrs = 0;
914                 sc->sc_flushes = 0;
915                 sc->sc_speedups = 0;
916                 sc->sc_cmd0s = 0;
917                 sc->sc_cmd1s = 0;
918                 sc->sc_cmd2s = 0;
919                 sc->sc_readbytes = 0;
920                 sc->sc_wrotebytes = 0;
921         }
922 }
923
924 static void
925 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
926 {
927         uint32_t *version;
928
929         g_topology_assert();
930
931         version = gctl_get_paraml(req, "version", sizeof(*version));
932         if (version == NULL) {
933                 gctl_error(req, "No '%s' argument.", "version");
934                 return;
935         }
936         if (*version != G_NOP_VERSION) {
937                 gctl_error(req, "Userland and kernel parts are out of sync.");
938                 return;
939         }
940
941         if (strcmp(verb, "create") == 0) {
942                 g_nop_ctl_create(req, mp);
943                 return;
944         } else if (strcmp(verb, "configure") == 0) {
945                 g_nop_ctl_configure(req, mp);
946                 return;
947         } else if (strcmp(verb, "destroy") == 0) {
948                 g_nop_ctl_destroy(req, mp);
949                 return;
950         } else if (strcmp(verb, "reset") == 0) {
951                 g_nop_ctl_reset(req, mp);
952                 return;
953         }
954
955         gctl_error(req, "Unknown verb.");
956 }
957
958 static void
959 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
960     struct g_consumer *cp, struct g_provider *pp)
961 {
962         struct g_nop_softc *sc;
963
964         if (pp != NULL || cp != NULL)
965                 return;
966         sc = gp->softc;
967         sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
968             (intmax_t)sc->sc_offset);
969         sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
970             sc->sc_rfailprob);
971         sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
972             sc->sc_wfailprob);
973         sbuf_printf(sb, "%s<ReadDelayedProb>%u</ReadDelayedProb>\n", indent,
974             sc->sc_rdelayprob);
975         sbuf_printf(sb, "%s<WriteDelayedProb>%u</WriteDelayedProb>\n", indent,
976             sc->sc_wdelayprob);
977         sbuf_printf(sb, "%s<Delay>%d</Delay>\n", indent, sc->sc_delaymsec);
978         sbuf_printf(sb, "%s<CountUntilFail>%u</CountUntilFail>\n", indent,
979             sc->sc_count_until_fail);
980         sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
981         sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
982         sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
983         sbuf_printf(sb, "%s<Deletes>%ju</Deletes>\n", indent, sc->sc_deletes);
984         sbuf_printf(sb, "%s<Getattrs>%ju</Getattrs>\n", indent, sc->sc_getattrs);
985         sbuf_printf(sb, "%s<Flushes>%ju</Flushes>\n", indent, sc->sc_flushes);
986         sbuf_printf(sb, "%s<Speedups>%ju</Speedups>\n", indent, sc->sc_speedups);
987         sbuf_printf(sb, "%s<Cmd0s>%ju</Cmd0s>\n", indent, sc->sc_cmd0s);
988         sbuf_printf(sb, "%s<Cmd1s>%ju</Cmd1s>\n", indent, sc->sc_cmd1s);
989         sbuf_printf(sb, "%s<Cmd2s>%ju</Cmd2s>\n", indent, sc->sc_cmd2s);
990         sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
991             sc->sc_readbytes);
992         sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
993             sc->sc_wrotebytes);
994 }
995
996 DECLARE_GEOM_CLASS(g_nop_class, g_nop);
997 MODULE_VERSION(geom_nop, 0);