]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/nop/g_nop.c
MFV r348576: 9963 Seperate tunable for disabling ZIL vdev flush
[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/nop/g_nop.h>
44
45
46 SYSCTL_DECL(_kern_geom);
47 static SYSCTL_NODE(_kern_geom, OID_AUTO, nop, CTLFLAG_RW, 0, "GEOM_NOP stuff");
48 static u_int g_nop_debug = 0;
49 SYSCTL_UINT(_kern_geom_nop, OID_AUTO, debug, CTLFLAG_RW, &g_nop_debug, 0,
50     "Debug level");
51
52 static int g_nop_destroy(struct g_geom *gp, boolean_t force);
53 static int g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp,
54     struct g_geom *gp);
55 static void g_nop_config(struct gctl_req *req, struct g_class *mp,
56     const char *verb);
57 static g_access_t g_nop_access;
58 static g_dumpconf_t g_nop_dumpconf;
59 static g_orphan_t g_nop_orphan;
60 static g_provgone_t g_nop_providergone;
61 static g_resize_t g_nop_resize;
62 static g_start_t g_nop_start;
63
64 struct g_class g_nop_class = {
65         .name = G_NOP_CLASS_NAME,
66         .version = G_VERSION,
67         .ctlreq = g_nop_config,
68         .destroy_geom = g_nop_destroy_geom,
69         .access = g_nop_access,
70         .dumpconf = g_nop_dumpconf,
71         .orphan = g_nop_orphan,
72         .providergone = g_nop_providergone,
73         .resize = g_nop_resize,
74         .start = g_nop_start,
75 };
76
77 static void
78 g_nop_orphan(struct g_consumer *cp)
79 {
80
81         g_topology_assert();
82         g_nop_destroy(cp->geom, 1);
83 }
84
85 static void
86 g_nop_resize(struct g_consumer *cp)
87 {
88         struct g_nop_softc *sc;
89         struct g_geom *gp;
90         struct g_provider *pp;
91         off_t size;
92
93         g_topology_assert();
94
95         gp = cp->geom;
96         sc = gp->softc;
97
98         if (sc->sc_explicitsize != 0)
99                 return;
100         if (cp->provider->mediasize < sc->sc_offset) {
101                 g_nop_destroy(gp, 1);
102                 return;
103         }
104         size = cp->provider->mediasize - sc->sc_offset;
105         LIST_FOREACH(pp, &gp->provider, provider)
106                 g_resize_provider(pp, size);
107 }
108
109 static int
110 g_nop_dumper(void *priv, void *virtual, vm_offset_t physical, off_t offset,
111     size_t length)
112 {
113         return (0);
114 }
115
116 static void
117 g_nop_kerneldump(struct bio *bp, struct g_nop_softc *sc)
118 {
119         struct g_kerneldump *gkd;
120         struct g_geom *gp;
121         struct g_provider *pp;
122
123         gkd = (struct g_kerneldump *)bp->bio_data;
124         gp = bp->bio_to->geom;
125         g_trace(G_T_TOPOLOGY, "%s(%s, %jd, %jd)", __func__, gp->name,
126             (intmax_t)gkd->offset, (intmax_t)gkd->length);
127
128         pp = LIST_FIRST(&gp->provider);
129
130         gkd->di.dumper = g_nop_dumper;
131         gkd->di.priv = sc;
132         gkd->di.blocksize = pp->sectorsize;
133         gkd->di.maxiosize = DFLTPHYS;
134         gkd->di.mediaoffset = sc->sc_offset + gkd->offset;
135         if (gkd->offset > sc->sc_explicitsize) {
136                 g_io_deliver(bp, ENODEV);
137                 return;
138         }
139         if (gkd->offset + gkd->length > sc->sc_explicitsize)
140                 gkd->length = sc->sc_explicitsize - gkd->offset;
141         gkd->di.mediasize = gkd->length;
142         g_io_deliver(bp, 0);
143 }
144
145 static void
146 g_nop_start(struct bio *bp)
147 {
148         struct g_nop_softc *sc;
149         struct g_geom *gp;
150         struct g_provider *pp;
151         struct bio *cbp;
152         u_int failprob = 0;
153
154         gp = bp->bio_to->geom;
155         sc = gp->softc;
156         G_NOP_LOGREQ(bp, "Request received.");
157         mtx_lock(&sc->sc_lock);
158         switch (bp->bio_cmd) {
159         case BIO_READ:
160                 sc->sc_reads++;
161                 sc->sc_readbytes += bp->bio_length;
162                 failprob = sc->sc_rfailprob;
163                 break;
164         case BIO_WRITE:
165                 sc->sc_writes++;
166                 sc->sc_wrotebytes += bp->bio_length;
167                 failprob = sc->sc_wfailprob;
168                 break;
169         case BIO_DELETE:
170                 sc->sc_deletes++;
171                 break;
172         case BIO_GETATTR:
173                 sc->sc_getattrs++;
174                 if (sc->sc_physpath && 
175                     g_handleattr_str(bp, "GEOM::physpath", sc->sc_physpath))
176                         ;
177                 else if (strcmp(bp->bio_attribute, "GEOM::kerneldump") == 0)
178                         g_nop_kerneldump(bp, sc);
179                 else
180                         /*
181                          * Fallthrough to forwarding the GETATTR down to the
182                          * lower level device.
183                          */
184                         break;
185                 mtx_unlock(&sc->sc_lock);
186                 return;
187         case BIO_FLUSH:
188                 sc->sc_flushes++;
189                 break;
190         case BIO_CMD0:
191                 sc->sc_cmd0s++;
192                 break;
193         case BIO_CMD1:
194                 sc->sc_cmd1s++;
195                 break;
196         case BIO_CMD2:
197                 sc->sc_cmd2s++;
198                 break;
199         }
200         mtx_unlock(&sc->sc_lock);
201         if (failprob > 0) {
202                 u_int rval;
203
204                 rval = arc4random() % 100;
205                 if (rval < failprob) {
206                         G_NOP_LOGREQLVL(1, bp, "Returning error=%d.", sc->sc_error);
207                         g_io_deliver(bp, sc->sc_error);
208                         return;
209                 }
210         }
211         cbp = g_clone_bio(bp);
212         if (cbp == NULL) {
213                 g_io_deliver(bp, ENOMEM);
214                 return;
215         }
216         cbp->bio_done = g_std_done;
217         cbp->bio_offset = bp->bio_offset + sc->sc_offset;
218         pp = LIST_FIRST(&gp->provider);
219         KASSERT(pp != NULL, ("NULL pp"));
220         cbp->bio_to = pp;
221         G_NOP_LOGREQ(cbp, "Sending request.");
222         g_io_request(cbp, LIST_FIRST(&gp->consumer));
223 }
224
225 static int
226 g_nop_access(struct g_provider *pp, int dr, int dw, int de)
227 {
228         struct g_geom *gp;
229         struct g_consumer *cp;
230         int error;
231
232         gp = pp->geom;
233         cp = LIST_FIRST(&gp->consumer);
234         error = g_access(cp, dr, dw, de);
235
236         return (error);
237 }
238
239 static int
240 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
241     int ioerror, u_int rfailprob, u_int wfailprob, off_t offset, off_t size,
242     u_int secsize, off_t stripesize, off_t stripeoffset, const char *physpath)
243 {
244         struct g_nop_softc *sc;
245         struct g_geom *gp;
246         struct g_provider *newpp;
247         struct g_consumer *cp;
248         char name[64];
249         int error;
250         off_t explicitsize;
251
252         g_topology_assert();
253
254         gp = NULL;
255         newpp = NULL;
256         cp = NULL;
257
258         if ((offset % pp->sectorsize) != 0) {
259                 gctl_error(req, "Invalid offset for provider %s.", pp->name);
260                 return (EINVAL);
261         }
262         if ((size % pp->sectorsize) != 0) {
263                 gctl_error(req, "Invalid size for provider %s.", pp->name);
264                 return (EINVAL);
265         }
266         if (offset >= pp->mediasize) {
267                 gctl_error(req, "Invalid offset for provider %s.", pp->name);
268                 return (EINVAL);
269         }
270         explicitsize = size;
271         if (size == 0)
272                 size = pp->mediasize - offset;
273         if (offset + size > pp->mediasize) {
274                 gctl_error(req, "Invalid size for provider %s.", pp->name);
275                 return (EINVAL);
276         }
277         if (secsize == 0)
278                 secsize = pp->sectorsize;
279         else if ((secsize % pp->sectorsize) != 0) {
280                 gctl_error(req, "Invalid secsize for provider %s.", pp->name);
281                 return (EINVAL);
282         }
283         if (secsize > MAXPHYS) {
284                 gctl_error(req, "secsize is too big.");
285                 return (EINVAL);
286         }
287         size -= size % secsize;
288         if ((stripesize % pp->sectorsize) != 0) {
289                 gctl_error(req, "Invalid stripesize for provider %s.", pp->name);
290                 return (EINVAL);
291         }
292         if ((stripeoffset % pp->sectorsize) != 0) {
293                 gctl_error(req, "Invalid stripeoffset for provider %s.", pp->name);
294                 return (EINVAL);
295         }
296         if (stripesize != 0 && stripeoffset >= stripesize) {
297                 gctl_error(req, "stripeoffset is too big.");
298                 return (EINVAL);
299         }
300         snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX);
301         LIST_FOREACH(gp, &mp->geom, geom) {
302                 if (strcmp(gp->name, name) == 0) {
303                         gctl_error(req, "Provider %s already exists.", name);
304                         return (EEXIST);
305                 }
306         }
307         gp = g_new_geomf(mp, "%s", name);
308         sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
309         sc->sc_offset = offset;
310         sc->sc_explicitsize = explicitsize;
311         sc->sc_stripesize = stripesize;
312         sc->sc_stripeoffset = stripeoffset;
313         if (physpath && strcmp(physpath, G_NOP_PHYSPATH_PASSTHROUGH)) {
314                 sc->sc_physpath = strndup(physpath, MAXPATHLEN, M_GEOM);
315         } else
316                 sc->sc_physpath = NULL;
317         sc->sc_error = ioerror;
318         sc->sc_rfailprob = rfailprob;
319         sc->sc_wfailprob = wfailprob;
320         sc->sc_reads = 0;
321         sc->sc_writes = 0;
322         sc->sc_deletes = 0;
323         sc->sc_getattrs = 0;
324         sc->sc_flushes = 0;
325         sc->sc_cmd0s = 0;
326         sc->sc_cmd1s = 0;
327         sc->sc_cmd2s = 0;
328         sc->sc_readbytes = 0;
329         sc->sc_wrotebytes = 0;
330         mtx_init(&sc->sc_lock, "gnop lock", NULL, MTX_DEF);
331         gp->softc = sc;
332
333         newpp = g_new_providerf(gp, "%s", gp->name);
334         newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
335         newpp->mediasize = size;
336         newpp->sectorsize = secsize;
337         newpp->stripesize = stripesize;
338         newpp->stripeoffset = stripeoffset;
339
340         cp = g_new_consumer(gp);
341         cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
342         error = g_attach(cp, pp);
343         if (error != 0) {
344                 gctl_error(req, "Cannot attach to provider %s.", pp->name);
345                 goto fail;
346         }
347
348         newpp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
349         g_error_provider(newpp, 0);
350         G_NOP_DEBUG(0, "Device %s created.", gp->name);
351         return (0);
352 fail:
353         if (cp->provider != NULL)
354                 g_detach(cp);
355         g_destroy_consumer(cp);
356         g_destroy_provider(newpp);
357         mtx_destroy(&sc->sc_lock);
358         free(sc->sc_physpath, M_GEOM);
359         g_free(gp->softc);
360         g_destroy_geom(gp);
361         return (error);
362 }
363
364 static void
365 g_nop_providergone(struct g_provider *pp)
366 {
367         struct g_geom *gp = pp->geom;
368         struct g_nop_softc *sc = gp->softc;
369
370         gp->softc = NULL;
371         free(sc->sc_physpath, M_GEOM);
372         mtx_destroy(&sc->sc_lock);
373         g_free(sc);
374 }
375
376 static int
377 g_nop_destroy(struct g_geom *gp, boolean_t force)
378 {
379         struct g_nop_softc *sc;
380         struct g_provider *pp;
381
382         g_topology_assert();
383         sc = gp->softc;
384         if (sc == NULL)
385                 return (ENXIO);
386         pp = LIST_FIRST(&gp->provider);
387         if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
388                 if (force) {
389                         G_NOP_DEBUG(0, "Device %s is still open, so it "
390                             "can't be definitely removed.", pp->name);
391                 } else {
392                         G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
393                             pp->name, pp->acr, pp->acw, pp->ace);
394                         return (EBUSY);
395                 }
396         } else {
397                 G_NOP_DEBUG(0, "Device %s removed.", gp->name);
398         }
399         g_wither_geom(gp, ENXIO);
400
401         return (0);
402 }
403
404 static int
405 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
406 {
407
408         return (g_nop_destroy(gp, 0));
409 }
410
411 static void
412 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
413 {
414         struct g_provider *pp;
415         intmax_t *error, *rfailprob, *wfailprob, *offset, *secsize, *size,
416             *stripesize, *stripeoffset;
417         const char *name, *physpath;
418         char param[16];
419         int i, *nargs;
420
421         g_topology_assert();
422
423         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
424         if (nargs == NULL) {
425                 gctl_error(req, "No '%s' argument", "nargs");
426                 return;
427         }
428         if (*nargs <= 0) {
429                 gctl_error(req, "Missing device(s).");
430                 return;
431         }
432         error = gctl_get_paraml(req, "error", sizeof(*error));
433         if (error == NULL) {
434                 gctl_error(req, "No '%s' argument", "error");
435                 return;
436         }
437         rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
438         if (rfailprob == NULL) {
439                 gctl_error(req, "No '%s' argument", "rfailprob");
440                 return;
441         }
442         if (*rfailprob < -1 || *rfailprob > 100) {
443                 gctl_error(req, "Invalid '%s' argument", "rfailprob");
444                 return;
445         }
446         wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
447         if (wfailprob == NULL) {
448                 gctl_error(req, "No '%s' argument", "wfailprob");
449                 return;
450         }
451         if (*wfailprob < -1 || *wfailprob > 100) {
452                 gctl_error(req, "Invalid '%s' argument", "wfailprob");
453                 return;
454         }
455         offset = gctl_get_paraml(req, "offset", sizeof(*offset));
456         if (offset == NULL) {
457                 gctl_error(req, "No '%s' argument", "offset");
458                 return;
459         }
460         if (*offset < 0) {
461                 gctl_error(req, "Invalid '%s' argument", "offset");
462                 return;
463         }
464         size = gctl_get_paraml(req, "size", sizeof(*size));
465         if (size == NULL) {
466                 gctl_error(req, "No '%s' argument", "size");
467                 return;
468         }
469         if (*size < 0) {
470                 gctl_error(req, "Invalid '%s' argument", "size");
471                 return;
472         }
473         secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
474         if (secsize == NULL) {
475                 gctl_error(req, "No '%s' argument", "secsize");
476                 return;
477         }
478         if (*secsize < 0) {
479                 gctl_error(req, "Invalid '%s' argument", "secsize");
480                 return;
481         }
482         stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
483         if (stripesize == NULL) {
484                 gctl_error(req, "No '%s' argument", "stripesize");
485                 return;
486         }
487         if (*stripesize < 0) {
488                 gctl_error(req, "Invalid '%s' argument", "stripesize");
489                 return;
490         }
491         stripeoffset = gctl_get_paraml(req, "stripeoffset", sizeof(*stripeoffset));
492         if (stripeoffset == NULL) {
493                 gctl_error(req, "No '%s' argument", "stripeoffset");
494                 return;
495         }
496         if (*stripeoffset < 0) {
497                 gctl_error(req, "Invalid '%s' argument", "stripeoffset");
498                 return;
499         }
500         physpath = gctl_get_asciiparam(req, "physpath");
501
502         for (i = 0; i < *nargs; i++) {
503                 snprintf(param, sizeof(param), "arg%d", i);
504                 name = gctl_get_asciiparam(req, param);
505                 if (name == NULL) {
506                         gctl_error(req, "No 'arg%d' argument", i);
507                         return;
508                 }
509                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
510                         name += strlen("/dev/");
511                 pp = g_provider_by_name(name);
512                 if (pp == NULL) {
513                         G_NOP_DEBUG(1, "Provider %s is invalid.", name);
514                         gctl_error(req, "Provider %s is invalid.", name);
515                         return;
516                 }
517                 if (g_nop_create(req, mp, pp,
518                     *error == -1 ? EIO : (int)*error,
519                     *rfailprob == -1 ? 0 : (u_int)*rfailprob,
520                     *wfailprob == -1 ? 0 : (u_int)*wfailprob,
521                     (off_t)*offset, (off_t)*size, (u_int)*secsize,
522                     (off_t)*stripesize, (off_t)*stripeoffset,
523                     physpath) != 0) {
524                         return;
525                 }
526         }
527 }
528
529 static void
530 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
531 {
532         struct g_nop_softc *sc;
533         struct g_provider *pp;
534         intmax_t *error, *rfailprob, *wfailprob;
535         const char *name;
536         char param[16];
537         int i, *nargs;
538
539         g_topology_assert();
540
541         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
542         if (nargs == NULL) {
543                 gctl_error(req, "No '%s' argument", "nargs");
544                 return;
545         }
546         if (*nargs <= 0) {
547                 gctl_error(req, "Missing device(s).");
548                 return;
549         }
550         error = gctl_get_paraml(req, "error", sizeof(*error));
551         if (error == NULL) {
552                 gctl_error(req, "No '%s' argument", "error");
553                 return;
554         }
555         rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
556         if (rfailprob == NULL) {
557                 gctl_error(req, "No '%s' argument", "rfailprob");
558                 return;
559         }
560         if (*rfailprob < -1 || *rfailprob > 100) {
561                 gctl_error(req, "Invalid '%s' argument", "rfailprob");
562                 return;
563         }
564         wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
565         if (wfailprob == NULL) {
566                 gctl_error(req, "No '%s' argument", "wfailprob");
567                 return;
568         }
569         if (*wfailprob < -1 || *wfailprob > 100) {
570                 gctl_error(req, "Invalid '%s' argument", "wfailprob");
571                 return;
572         }
573
574         for (i = 0; i < *nargs; i++) {
575                 snprintf(param, sizeof(param), "arg%d", i);
576                 name = gctl_get_asciiparam(req, param);
577                 if (name == NULL) {
578                         gctl_error(req, "No 'arg%d' argument", i);
579                         return;
580                 }
581                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
582                         name += strlen("/dev/");
583                 pp = g_provider_by_name(name);
584                 if (pp == NULL || pp->geom->class != mp) {
585                         G_NOP_DEBUG(1, "Provider %s is invalid.", name);
586                         gctl_error(req, "Provider %s is invalid.", name);
587                         return;
588                 }
589                 sc = pp->geom->softc;
590                 if (*error != -1)
591                         sc->sc_error = (int)*error;
592                 if (*rfailprob != -1)
593                         sc->sc_rfailprob = (u_int)*rfailprob;
594                 if (*wfailprob != -1)
595                         sc->sc_wfailprob = (u_int)*wfailprob;
596         }
597 }
598
599 static struct g_geom *
600 g_nop_find_geom(struct g_class *mp, const char *name)
601 {
602         struct g_geom *gp;
603
604         LIST_FOREACH(gp, &mp->geom, geom) {
605                 if (strcmp(gp->name, name) == 0)
606                         return (gp);
607         }
608         return (NULL);
609 }
610
611 static void
612 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
613 {
614         int *nargs, *force, error, i;
615         struct g_geom *gp;
616         const char *name;
617         char param[16];
618
619         g_topology_assert();
620
621         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
622         if (nargs == NULL) {
623                 gctl_error(req, "No '%s' argument", "nargs");
624                 return;
625         }
626         if (*nargs <= 0) {
627                 gctl_error(req, "Missing device(s).");
628                 return;
629         }
630         force = gctl_get_paraml(req, "force", sizeof(*force));
631         if (force == NULL) {
632                 gctl_error(req, "No 'force' argument");
633                 return;
634         }
635
636         for (i = 0; i < *nargs; i++) {
637                 snprintf(param, sizeof(param), "arg%d", i);
638                 name = gctl_get_asciiparam(req, param);
639                 if (name == NULL) {
640                         gctl_error(req, "No 'arg%d' argument", i);
641                         return;
642                 }
643                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
644                         name += strlen("/dev/");
645                 gp = g_nop_find_geom(mp, name);
646                 if (gp == NULL) {
647                         G_NOP_DEBUG(1, "Device %s is invalid.", name);
648                         gctl_error(req, "Device %s is invalid.", name);
649                         return;
650                 }
651                 error = g_nop_destroy(gp, *force);
652                 if (error != 0) {
653                         gctl_error(req, "Cannot destroy device %s (error=%d).",
654                             gp->name, error);
655                         return;
656                 }
657         }
658 }
659
660 static void
661 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
662 {
663         struct g_nop_softc *sc;
664         struct g_provider *pp;
665         const char *name;
666         char param[16];
667         int i, *nargs;
668
669         g_topology_assert();
670
671         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
672         if (nargs == NULL) {
673                 gctl_error(req, "No '%s' argument", "nargs");
674                 return;
675         }
676         if (*nargs <= 0) {
677                 gctl_error(req, "Missing device(s).");
678                 return;
679         }
680
681         for (i = 0; i < *nargs; i++) {
682                 snprintf(param, sizeof(param), "arg%d", i);
683                 name = gctl_get_asciiparam(req, param);
684                 if (name == NULL) {
685                         gctl_error(req, "No 'arg%d' argument", i);
686                         return;
687                 }
688                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
689                         name += strlen("/dev/");
690                 pp = g_provider_by_name(name);
691                 if (pp == NULL || pp->geom->class != mp) {
692                         G_NOP_DEBUG(1, "Provider %s is invalid.", name);
693                         gctl_error(req, "Provider %s is invalid.", name);
694                         return;
695                 }
696                 sc = pp->geom->softc;
697                 sc->sc_reads = 0;
698                 sc->sc_writes = 0;
699                 sc->sc_deletes = 0;
700                 sc->sc_getattrs = 0;
701                 sc->sc_flushes = 0;
702                 sc->sc_cmd0s = 0;
703                 sc->sc_cmd1s = 0;
704                 sc->sc_cmd2s = 0;
705                 sc->sc_readbytes = 0;
706                 sc->sc_wrotebytes = 0;
707         }
708 }
709
710 static void
711 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
712 {
713         uint32_t *version;
714
715         g_topology_assert();
716
717         version = gctl_get_paraml(req, "version", sizeof(*version));
718         if (version == NULL) {
719                 gctl_error(req, "No '%s' argument.", "version");
720                 return;
721         }
722         if (*version != G_NOP_VERSION) {
723                 gctl_error(req, "Userland and kernel parts are out of sync.");
724                 return;
725         }
726
727         if (strcmp(verb, "create") == 0) {
728                 g_nop_ctl_create(req, mp);
729                 return;
730         } else if (strcmp(verb, "configure") == 0) {
731                 g_nop_ctl_configure(req, mp);
732                 return;
733         } else if (strcmp(verb, "destroy") == 0) {
734                 g_nop_ctl_destroy(req, mp);
735                 return;
736         } else if (strcmp(verb, "reset") == 0) {
737                 g_nop_ctl_reset(req, mp);
738                 return;
739         }
740
741         gctl_error(req, "Unknown verb.");
742 }
743
744 static void
745 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
746     struct g_consumer *cp, struct g_provider *pp)
747 {
748         struct g_nop_softc *sc;
749
750         if (pp != NULL || cp != NULL)
751                 return;
752         sc = gp->softc;
753         sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
754             (intmax_t)sc->sc_offset);
755         sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
756             sc->sc_rfailprob);
757         sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
758             sc->sc_wfailprob);
759         sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
760         sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
761         sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
762         sbuf_printf(sb, "%s<Deletes>%ju</Deletes>\n", indent, sc->sc_deletes);
763         sbuf_printf(sb, "%s<Getattrs>%ju</Getattrs>\n", indent, sc->sc_getattrs);
764         sbuf_printf(sb, "%s<Flushes>%ju</Flushes>\n", indent, sc->sc_flushes);
765         sbuf_printf(sb, "%s<Cmd0s>%ju</Cmd0s>\n", indent, sc->sc_cmd0s);
766         sbuf_printf(sb, "%s<Cmd1s>%ju</Cmd1s>\n", indent, sc->sc_cmd1s);
767         sbuf_printf(sb, "%s<Cmd2s>%ju</Cmd2s>\n", indent, sc->sc_cmd2s);
768         sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
769             sc->sc_readbytes);
770         sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
771             sc->sc_wrotebytes);
772 }
773
774 DECLARE_GEOM_CLASS(g_nop_class, g_nop);
775 MODULE_VERSION(geom_nop, 0);