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