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