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