]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/geom/nop/g_nop.c
MFV r353615: 9485 Optimize possible split block search space
[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 *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         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
510         if (nargs == NULL) {
511                 gctl_error(req, "No '%s' argument", "nargs");
512                 return;
513         }
514         if (*nargs <= 0) {
515                 gctl_error(req, "Missing device(s).");
516                 return;
517         }
518         error = gctl_get_paraml(req, "error", sizeof(*error));
519         if (error == NULL) {
520                 gctl_error(req, "No '%s' argument", "error");
521                 return;
522         }
523         rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
524         if (rfailprob == NULL) {
525                 gctl_error(req, "No '%s' argument", "rfailprob");
526                 return;
527         }
528         if (*rfailprob < -1 || *rfailprob > 100) {
529                 gctl_error(req, "Invalid '%s' argument", "rfailprob");
530                 return;
531         }
532         wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
533         if (wfailprob == NULL) {
534                 gctl_error(req, "No '%s' argument", "wfailprob");
535                 return;
536         }
537         if (*wfailprob < -1 || *wfailprob > 100) {
538                 gctl_error(req, "Invalid '%s' argument", "wfailprob");
539                 return;
540         }
541         delaymsec = gctl_get_paraml(req, "delaymsec", sizeof(*delaymsec));
542         if (delaymsec == NULL) {
543                 gctl_error(req, "No '%s' argument", "delaymsec");
544                 return;
545         }
546         if (*delaymsec < 1 && *delaymsec != -1) {
547                 gctl_error(req, "Invalid '%s' argument", "delaymsec");
548                 return;
549         }
550         rdelayprob = gctl_get_paraml(req, "rdelayprob", sizeof(*rdelayprob));
551         if (rdelayprob == NULL) {
552                 gctl_error(req, "No '%s' argument", "rdelayprob");
553                 return;
554         }
555         if (*rdelayprob < -1 || *rdelayprob > 100) {
556                 gctl_error(req, "Invalid '%s' argument", "rdelayprob");
557                 return;
558         }
559         wdelayprob = gctl_get_paraml(req, "wdelayprob", sizeof(*wdelayprob));
560         if (wdelayprob == NULL) {
561                 gctl_error(req, "No '%s' argument", "wdelayprob");
562                 return;
563         }
564         if (*wdelayprob < -1 || *wdelayprob > 100) {
565                 gctl_error(req, "Invalid '%s' argument", "wdelayprob");
566                 return;
567         }
568         count_until_fail = gctl_get_paraml(req, "count_until_fail",
569             sizeof(*count_until_fail));
570         if (count_until_fail == NULL) {
571                 gctl_error(req, "No '%s' argument", "count_until_fail");
572                 return;
573         }
574         if (*count_until_fail < -1) {
575                 gctl_error(req, "Invalid '%s' argument", "count_until_fail");
576                 return;
577         }
578         offset = gctl_get_paraml(req, "offset", sizeof(*offset));
579         if (offset == NULL) {
580                 gctl_error(req, "No '%s' argument", "offset");
581                 return;
582         }
583         if (*offset < 0) {
584                 gctl_error(req, "Invalid '%s' argument", "offset");
585                 return;
586         }
587         size = gctl_get_paraml(req, "size", sizeof(*size));
588         if (size == NULL) {
589                 gctl_error(req, "No '%s' argument", "size");
590                 return;
591         }
592         if (*size < 0) {
593                 gctl_error(req, "Invalid '%s' argument", "size");
594                 return;
595         }
596         secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
597         if (secsize == NULL) {
598                 gctl_error(req, "No '%s' argument", "secsize");
599                 return;
600         }
601         if (*secsize < 0) {
602                 gctl_error(req, "Invalid '%s' argument", "secsize");
603                 return;
604         }
605         stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
606         if (stripesize == NULL) {
607                 gctl_error(req, "No '%s' argument", "stripesize");
608                 return;
609         }
610         if (*stripesize < 0) {
611                 gctl_error(req, "Invalid '%s' argument", "stripesize");
612                 return;
613         }
614         stripeoffset = gctl_get_paraml(req, "stripeoffset", sizeof(*stripeoffset));
615         if (stripeoffset == NULL) {
616                 gctl_error(req, "No '%s' argument", "stripeoffset");
617                 return;
618         }
619         if (*stripeoffset < 0) {
620                 gctl_error(req, "Invalid '%s' argument", "stripeoffset");
621                 return;
622         }
623         physpath = gctl_get_asciiparam(req, "physpath");
624
625         for (i = 0; i < *nargs; i++) {
626                 snprintf(param, sizeof(param), "arg%d", i);
627                 name = gctl_get_asciiparam(req, param);
628                 if (name == NULL) {
629                         gctl_error(req, "No 'arg%d' argument", i);
630                         return;
631                 }
632                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
633                         name += strlen("/dev/");
634                 pp = g_provider_by_name(name);
635                 if (pp == NULL) {
636                         G_NOP_DEBUG(1, "Provider %s is invalid.", name);
637                         gctl_error(req, "Provider %s is invalid.", name);
638                         return;
639                 }
640                 if (g_nop_create(req, mp, pp,
641                     *error == -1 ? EIO : (int)*error,
642                     *count_until_fail == -1 ? 0 : (u_int)*count_until_fail,
643                     *rfailprob == -1 ? 0 : (u_int)*rfailprob,
644                     *wfailprob == -1 ? 0 : (u_int)*wfailprob,
645                     *delaymsec == -1 ? 1 : (u_int)*delaymsec,
646                     *rdelayprob == -1 ? 0 : (u_int)*rdelayprob,
647                     *wdelayprob == -1 ? 0 : (u_int)*wdelayprob,
648                     (off_t)*offset, (off_t)*size, (u_int)*secsize,
649                     (off_t)*stripesize, (off_t)*stripeoffset,
650                     physpath) != 0) {
651                         return;
652                 }
653         }
654 }
655
656 static void
657 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
658 {
659         struct g_nop_softc *sc;
660         struct g_provider *pp;
661         intmax_t *delaymsec, *error, *rdelayprob, *rfailprob, *wdelayprob,
662             *wfailprob, *count_until_fail;
663         const char *name;
664         char param[16];
665         int i, *nargs;
666
667         g_topology_assert();
668
669         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
670         if (nargs == NULL) {
671                 gctl_error(req, "No '%s' argument", "nargs");
672                 return;
673         }
674         if (*nargs <= 0) {
675                 gctl_error(req, "Missing device(s).");
676                 return;
677         }
678         error = gctl_get_paraml(req, "error", sizeof(*error));
679         if (error == NULL) {
680                 gctl_error(req, "No '%s' argument", "error");
681                 return;
682         }
683         count_until_fail = gctl_get_paraml(req, "count_until_fail",
684             sizeof(*count_until_fail));
685         if (count_until_fail == NULL) {
686                 gctl_error(req, "No '%s' argument", "count_until_fail");
687                 return;
688         }
689         rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
690         if (rfailprob == NULL) {
691                 gctl_error(req, "No '%s' argument", "rfailprob");
692                 return;
693         }
694         if (*rfailprob < -1 || *rfailprob > 100) {
695                 gctl_error(req, "Invalid '%s' argument", "rfailprob");
696                 return;
697         }
698         wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
699         if (wfailprob == NULL) {
700                 gctl_error(req, "No '%s' argument", "wfailprob");
701                 return;
702         }
703         if (*wfailprob < -1 || *wfailprob > 100) {
704                 gctl_error(req, "Invalid '%s' argument", "wfailprob");
705                 return;
706         }
707
708         delaymsec = gctl_get_paraml(req, "delaymsec", sizeof(*delaymsec));
709         if (delaymsec == NULL) {
710                 gctl_error(req, "No '%s' argument", "delaymsec");
711                 return;
712         }
713         if (*delaymsec < 1 && *delaymsec != -1) {
714                 gctl_error(req, "Invalid '%s' argument", "delaymsec");
715                 return;
716         }
717         rdelayprob = gctl_get_paraml(req, "rdelayprob", sizeof(*rdelayprob));
718         if (rdelayprob == NULL) {
719                 gctl_error(req, "No '%s' argument", "rdelayprob");
720                 return;
721         }
722         if (*rdelayprob < -1 || *rdelayprob > 100) {
723                 gctl_error(req, "Invalid '%s' argument", "rdelayprob");
724                 return;
725         }
726         wdelayprob = gctl_get_paraml(req, "wdelayprob", sizeof(*wdelayprob));
727         if (wdelayprob == NULL) {
728                 gctl_error(req, "No '%s' argument", "wdelayprob");
729                 return;
730         }
731         if (*wdelayprob < -1 || *wdelayprob > 100) {
732                 gctl_error(req, "Invalid '%s' argument", "wdelayprob");
733                 return;
734         }
735
736         for (i = 0; i < *nargs; i++) {
737                 snprintf(param, sizeof(param), "arg%d", i);
738                 name = gctl_get_asciiparam(req, param);
739                 if (name == NULL) {
740                         gctl_error(req, "No 'arg%d' argument", i);
741                         return;
742                 }
743                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
744                         name += strlen("/dev/");
745                 pp = g_provider_by_name(name);
746                 if (pp == NULL || pp->geom->class != mp) {
747                         G_NOP_DEBUG(1, "Provider %s is invalid.", name);
748                         gctl_error(req, "Provider %s is invalid.", name);
749                         return;
750                 }
751                 sc = pp->geom->softc;
752                 if (*error != -1)
753                         sc->sc_error = (int)*error;
754                 if (*rfailprob != -1)
755                         sc->sc_rfailprob = (u_int)*rfailprob;
756                 if (*wfailprob != -1)
757                         sc->sc_wfailprob = (u_int)*wfailprob;
758                 if (*rdelayprob != -1)
759                         sc->sc_rdelayprob = (u_int)*rdelayprob;
760                 if (*wdelayprob != -1)
761                         sc->sc_wdelayprob = (u_int)*wdelayprob;
762                 if (*delaymsec != -1)
763                         sc->sc_delaymsec = (u_int)*delaymsec;
764                 if (*count_until_fail != -1)
765                         sc->sc_count_until_fail = (u_int)*count_until_fail;
766         }
767 }
768
769 static struct g_geom *
770 g_nop_find_geom(struct g_class *mp, const char *name)
771 {
772         struct g_geom *gp;
773
774         LIST_FOREACH(gp, &mp->geom, geom) {
775                 if (strcmp(gp->name, name) == 0)
776                         return (gp);
777         }
778         return (NULL);
779 }
780
781 static void
782 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
783 {
784         int *nargs, *force, error, i;
785         struct g_geom *gp;
786         const char *name;
787         char param[16];
788
789         g_topology_assert();
790
791         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
792         if (nargs == NULL) {
793                 gctl_error(req, "No '%s' argument", "nargs");
794                 return;
795         }
796         if (*nargs <= 0) {
797                 gctl_error(req, "Missing device(s).");
798                 return;
799         }
800         force = gctl_get_paraml(req, "force", sizeof(*force));
801         if (force == NULL) {
802                 gctl_error(req, "No 'force' argument");
803                 return;
804         }
805
806         for (i = 0; i < *nargs; i++) {
807                 snprintf(param, sizeof(param), "arg%d", i);
808                 name = gctl_get_asciiparam(req, param);
809                 if (name == NULL) {
810                         gctl_error(req, "No 'arg%d' argument", i);
811                         return;
812                 }
813                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
814                         name += strlen("/dev/");
815                 gp = g_nop_find_geom(mp, name);
816                 if (gp == NULL) {
817                         G_NOP_DEBUG(1, "Device %s is invalid.", name);
818                         gctl_error(req, "Device %s is invalid.", name);
819                         return;
820                 }
821                 error = g_nop_destroy(gp, *force);
822                 if (error != 0) {
823                         gctl_error(req, "Cannot destroy device %s (error=%d).",
824                             gp->name, error);
825                         return;
826                 }
827         }
828 }
829
830 static void
831 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
832 {
833         struct g_nop_softc *sc;
834         struct g_provider *pp;
835         const char *name;
836         char param[16];
837         int i, *nargs;
838
839         g_topology_assert();
840
841         nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
842         if (nargs == NULL) {
843                 gctl_error(req, "No '%s' argument", "nargs");
844                 return;
845         }
846         if (*nargs <= 0) {
847                 gctl_error(req, "Missing device(s).");
848                 return;
849         }
850
851         for (i = 0; i < *nargs; i++) {
852                 snprintf(param, sizeof(param), "arg%d", i);
853                 name = gctl_get_asciiparam(req, param);
854                 if (name == NULL) {
855                         gctl_error(req, "No 'arg%d' argument", i);
856                         return;
857                 }
858                 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
859                         name += strlen("/dev/");
860                 pp = g_provider_by_name(name);
861                 if (pp == NULL || pp->geom->class != mp) {
862                         G_NOP_DEBUG(1, "Provider %s is invalid.", name);
863                         gctl_error(req, "Provider %s is invalid.", name);
864                         return;
865                 }
866                 sc = pp->geom->softc;
867                 sc->sc_reads = 0;
868                 sc->sc_writes = 0;
869                 sc->sc_deletes = 0;
870                 sc->sc_getattrs = 0;
871                 sc->sc_flushes = 0;
872                 sc->sc_cmd0s = 0;
873                 sc->sc_cmd1s = 0;
874                 sc->sc_cmd2s = 0;
875                 sc->sc_readbytes = 0;
876                 sc->sc_wrotebytes = 0;
877         }
878 }
879
880 static void
881 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
882 {
883         uint32_t *version;
884
885         g_topology_assert();
886
887         version = gctl_get_paraml(req, "version", sizeof(*version));
888         if (version == NULL) {
889                 gctl_error(req, "No '%s' argument.", "version");
890                 return;
891         }
892         if (*version != G_NOP_VERSION) {
893                 gctl_error(req, "Userland and kernel parts are out of sync.");
894                 return;
895         }
896
897         if (strcmp(verb, "create") == 0) {
898                 g_nop_ctl_create(req, mp);
899                 return;
900         } else if (strcmp(verb, "configure") == 0) {
901                 g_nop_ctl_configure(req, mp);
902                 return;
903         } else if (strcmp(verb, "destroy") == 0) {
904                 g_nop_ctl_destroy(req, mp);
905                 return;
906         } else if (strcmp(verb, "reset") == 0) {
907                 g_nop_ctl_reset(req, mp);
908                 return;
909         }
910
911         gctl_error(req, "Unknown verb.");
912 }
913
914 static void
915 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
916     struct g_consumer *cp, struct g_provider *pp)
917 {
918         struct g_nop_softc *sc;
919
920         if (pp != NULL || cp != NULL)
921                 return;
922         sc = gp->softc;
923         sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
924             (intmax_t)sc->sc_offset);
925         sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
926             sc->sc_rfailprob);
927         sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
928             sc->sc_wfailprob);
929         sbuf_printf(sb, "%s<ReadDelayedProb>%u</ReadDelayedProb>\n", indent,
930             sc->sc_rdelayprob);
931         sbuf_printf(sb, "%s<WriteDelayedProb>%u</WriteDelayedProb>\n", indent,
932             sc->sc_wdelayprob);
933         sbuf_printf(sb, "%s<Delay>%d</Delay>\n", indent, sc->sc_delaymsec);
934         sbuf_printf(sb, "%s<CountUntilFail>%u</CountUntilFail>\n", indent,
935             sc->sc_count_until_fail);
936         sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
937         sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
938         sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
939         sbuf_printf(sb, "%s<Deletes>%ju</Deletes>\n", indent, sc->sc_deletes);
940         sbuf_printf(sb, "%s<Getattrs>%ju</Getattrs>\n", indent, sc->sc_getattrs);
941         sbuf_printf(sb, "%s<Flushes>%ju</Flushes>\n", indent, sc->sc_flushes);
942         sbuf_printf(sb, "%s<Cmd0s>%ju</Cmd0s>\n", indent, sc->sc_cmd0s);
943         sbuf_printf(sb, "%s<Cmd1s>%ju</Cmd1s>\n", indent, sc->sc_cmd1s);
944         sbuf_printf(sb, "%s<Cmd2s>%ju</Cmd2s>\n", indent, sc->sc_cmd2s);
945         sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
946             sc->sc_readbytes);
947         sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
948             sc->sc_wrotebytes);
949 }
950
951 DECLARE_GEOM_CLASS(g_nop_class, g_nop);
952 MODULE_VERSION(geom_nop, 0);