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