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