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