xref: /freebsd/sys/geom/nop/g_nop.c (revision e430d1ed78d021db4e9760d9800393b627156745)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/bio.h>
39 #include <sys/sbuf.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
42 #include <geom/geom.h>
43 #include <geom/nop/g_nop.h>
44 
45 
46 SYSCTL_DECL(_kern_geom);
47 static SYSCTL_NODE(_kern_geom, OID_AUTO, nop, CTLFLAG_RW, 0, "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 void g_nop_dumpconf(struct sbuf *sb, const char *indent,
58     struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
59 
60 struct g_class g_nop_class = {
61 	.name = G_NOP_CLASS_NAME,
62 	.version = G_VERSION,
63 	.ctlreq = g_nop_config,
64 	.destroy_geom = g_nop_destroy_geom
65 };
66 
67 
68 static void
69 g_nop_orphan(struct g_consumer *cp)
70 {
71 
72 	g_topology_assert();
73 	g_nop_destroy(cp->geom, 1);
74 }
75 
76 static void
77 g_nop_resize(struct g_consumer *cp)
78 {
79 	struct g_nop_softc *sc;
80 	struct g_geom *gp;
81 	struct g_provider *pp;
82 	off_t size;
83 
84 	g_topology_assert();
85 
86 	gp = cp->geom;
87 	sc = gp->softc;
88 
89 	if (sc->sc_explicitsize != 0)
90 		return;
91 	if (cp->provider->mediasize < sc->sc_offset) {
92 		g_nop_destroy(gp, 1);
93 		return;
94 	}
95 	size = cp->provider->mediasize - sc->sc_offset;
96 	LIST_FOREACH(pp, &gp->provider, provider)
97 		g_resize_provider(pp, size);
98 }
99 
100 static int
101 g_nop_dumper(void *priv, void *virtual, vm_offset_t physical, off_t offset,
102     size_t length)
103 {
104 	return (0);
105 }
106 
107 static void
108 g_nop_kerneldump(struct bio *bp, struct g_nop_softc *sc)
109 {
110 	struct g_kerneldump *gkd;
111 	struct g_geom *gp;
112 	struct g_provider *pp;
113 
114 	gkd = (struct g_kerneldump *)bp->bio_data;
115 	gp = bp->bio_to->geom;
116 	g_trace(G_T_TOPOLOGY, "%s(%s, %jd, %jd)", __func__, gp->name,
117 	    (intmax_t)gkd->offset, (intmax_t)gkd->length);
118 
119 	pp = LIST_FIRST(&gp->provider);
120 
121 	gkd->di.dumper = g_nop_dumper;
122 	gkd->di.priv = sc;
123 	gkd->di.blocksize = pp->sectorsize;
124 	gkd->di.maxiosize = DFLTPHYS;
125 	gkd->di.mediaoffset = sc->sc_offset + gkd->offset;
126 	if (gkd->offset > sc->sc_explicitsize) {
127 		g_io_deliver(bp, ENODEV);
128 		return;
129 	}
130 	if (gkd->offset + gkd->length > sc->sc_explicitsize)
131 		gkd->length = sc->sc_explicitsize - gkd->offset;
132 	gkd->di.mediasize = gkd->length;
133 	g_io_deliver(bp, 0);
134 }
135 
136 static void
137 g_nop_start(struct bio *bp)
138 {
139 	struct g_nop_softc *sc;
140 	struct g_geom *gp;
141 	struct g_provider *pp;
142 	struct bio *cbp;
143 	u_int failprob = 0;
144 
145 	gp = bp->bio_to->geom;
146 	sc = gp->softc;
147 	G_NOP_LOGREQ(bp, "Request received.");
148 	mtx_lock(&sc->sc_lock);
149 	switch (bp->bio_cmd) {
150 	case BIO_READ:
151 		sc->sc_reads++;
152 		sc->sc_readbytes += bp->bio_length;
153 		failprob = sc->sc_rfailprob;
154 		break;
155 	case BIO_WRITE:
156 		sc->sc_writes++;
157 		sc->sc_wrotebytes += bp->bio_length;
158 		failprob = sc->sc_wfailprob;
159 		break;
160 	case BIO_DELETE:
161 		sc->sc_deletes++;
162 		break;
163 	case BIO_GETATTR:
164 		sc->sc_getattrs++;
165 		if (sc->sc_physpath &&
166 		    g_handleattr_str(bp, "GEOM::physpath", sc->sc_physpath))
167 			;
168 		else if (strcmp(bp->bio_attribute, "GEOM::kerneldump") == 0)
169 			g_nop_kerneldump(bp, sc);
170 		else
171 			/*
172 			 * Fallthrough to forwarding the GETATTR down to the
173 			 * lower level device.
174 			 */
175 			break;
176 		mtx_unlock(&sc->sc_lock);
177 		return;
178 	case BIO_FLUSH:
179 		sc->sc_flushes++;
180 		break;
181 	case BIO_CMD0:
182 		sc->sc_cmd0s++;
183 		break;
184 	case BIO_CMD1:
185 		sc->sc_cmd1s++;
186 		break;
187 	case BIO_CMD2:
188 		sc->sc_cmd2s++;
189 		break;
190 	}
191 	mtx_unlock(&sc->sc_lock);
192 	if (failprob > 0) {
193 		u_int rval;
194 
195 		rval = arc4random() % 100;
196 		if (rval < failprob) {
197 			G_NOP_LOGREQLVL(1, bp, "Returning error=%d.", sc->sc_error);
198 			g_io_deliver(bp, sc->sc_error);
199 			return;
200 		}
201 	}
202 	cbp = g_clone_bio(bp);
203 	if (cbp == NULL) {
204 		g_io_deliver(bp, ENOMEM);
205 		return;
206 	}
207 	cbp->bio_done = g_std_done;
208 	cbp->bio_offset = bp->bio_offset + sc->sc_offset;
209 	pp = LIST_FIRST(&gp->provider);
210 	KASSERT(pp != NULL, ("NULL pp"));
211 	cbp->bio_to = pp;
212 	G_NOP_LOGREQ(cbp, "Sending request.");
213 	g_io_request(cbp, LIST_FIRST(&gp->consumer));
214 }
215 
216 static int
217 g_nop_access(struct g_provider *pp, int dr, int dw, int de)
218 {
219 	struct g_geom *gp;
220 	struct g_consumer *cp;
221 	int error;
222 
223 	gp = pp->geom;
224 	cp = LIST_FIRST(&gp->consumer);
225 	error = g_access(cp, dr, dw, de);
226 
227 	return (error);
228 }
229 
230 static int
231 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
232     int ioerror, u_int rfailprob, u_int wfailprob, off_t offset, off_t size,
233     u_int secsize, off_t stripesize, off_t stripeoffset, const char *physpath)
234 {
235 	struct g_nop_softc *sc;
236 	struct g_geom *gp;
237 	struct g_provider *newpp;
238 	struct g_consumer *cp;
239 	char name[64];
240 	int error;
241 	off_t explicitsize;
242 
243 	g_topology_assert();
244 
245 	gp = NULL;
246 	newpp = NULL;
247 	cp = NULL;
248 
249 	if ((offset % pp->sectorsize) != 0) {
250 		gctl_error(req, "Invalid offset for provider %s.", pp->name);
251 		return (EINVAL);
252 	}
253 	if ((size % pp->sectorsize) != 0) {
254 		gctl_error(req, "Invalid size for provider %s.", pp->name);
255 		return (EINVAL);
256 	}
257 	if (offset >= pp->mediasize) {
258 		gctl_error(req, "Invalid offset for provider %s.", pp->name);
259 		return (EINVAL);
260 	}
261 	explicitsize = size;
262 	if (size == 0)
263 		size = pp->mediasize - offset;
264 	if (offset + size > pp->mediasize) {
265 		gctl_error(req, "Invalid size for provider %s.", pp->name);
266 		return (EINVAL);
267 	}
268 	if (secsize == 0)
269 		secsize = pp->sectorsize;
270 	else if ((secsize % pp->sectorsize) != 0) {
271 		gctl_error(req, "Invalid secsize for provider %s.", pp->name);
272 		return (EINVAL);
273 	}
274 	if (secsize > MAXPHYS) {
275 		gctl_error(req, "secsize is too big.");
276 		return (EINVAL);
277 	}
278 	size -= size % secsize;
279 	if ((stripesize % pp->sectorsize) != 0) {
280 		gctl_error(req, "Invalid stripesize for provider %s.", pp->name);
281 		return (EINVAL);
282 	}
283 	if ((stripeoffset % pp->sectorsize) != 0) {
284 		gctl_error(req, "Invalid stripeoffset for provider %s.", pp->name);
285 		return (EINVAL);
286 	}
287 	if (stripesize != 0 && stripeoffset >= stripesize) {
288 		gctl_error(req, "stripeoffset is too big.");
289 		return (EINVAL);
290 	}
291 	snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX);
292 	LIST_FOREACH(gp, &mp->geom, geom) {
293 		if (strcmp(gp->name, name) == 0) {
294 			gctl_error(req, "Provider %s already exists.", name);
295 			return (EEXIST);
296 		}
297 	}
298 	gp = g_new_geomf(mp, "%s", name);
299 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
300 	sc->sc_offset = offset;
301 	sc->sc_explicitsize = explicitsize;
302 	sc->sc_stripesize = stripesize;
303 	sc->sc_stripeoffset = stripeoffset;
304 	if (physpath && strcmp(physpath, G_NOP_PHYSPATH_PASSTHROUGH)) {
305 		sc->sc_physpath = strndup(physpath, MAXPATHLEN, M_GEOM);
306 	} else
307 		sc->sc_physpath = NULL;
308 	sc->sc_error = ioerror;
309 	sc->sc_rfailprob = rfailprob;
310 	sc->sc_wfailprob = wfailprob;
311 	sc->sc_reads = 0;
312 	sc->sc_writes = 0;
313 	sc->sc_deletes = 0;
314 	sc->sc_getattrs = 0;
315 	sc->sc_flushes = 0;
316 	sc->sc_cmd0s = 0;
317 	sc->sc_cmd1s = 0;
318 	sc->sc_cmd2s = 0;
319 	sc->sc_readbytes = 0;
320 	sc->sc_wrotebytes = 0;
321 	mtx_init(&sc->sc_lock, "gnop lock", NULL, MTX_DEF);
322 	gp->softc = sc;
323 	gp->start = g_nop_start;
324 	gp->orphan = g_nop_orphan;
325 	gp->resize = g_nop_resize;
326 	gp->access = g_nop_access;
327 	gp->dumpconf = g_nop_dumpconf;
328 
329 	newpp = g_new_providerf(gp, "%s", gp->name);
330 	newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
331 	newpp->mediasize = size;
332 	newpp->sectorsize = secsize;
333 	newpp->stripesize = stripesize;
334 	newpp->stripeoffset = stripeoffset;
335 
336 	cp = g_new_consumer(gp);
337 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
338 	error = g_attach(cp, pp);
339 	if (error != 0) {
340 		gctl_error(req, "Cannot attach to provider %s.", pp->name);
341 		goto fail;
342 	}
343 
344 	newpp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
345 	g_error_provider(newpp, 0);
346 	G_NOP_DEBUG(0, "Device %s created.", gp->name);
347 	return (0);
348 fail:
349 	if (cp->provider != NULL)
350 		g_detach(cp);
351 	g_destroy_consumer(cp);
352 	g_destroy_provider(newpp);
353 	mtx_destroy(&sc->sc_lock);
354 	free(sc->sc_physpath, M_GEOM);
355 	g_free(gp->softc);
356 	g_destroy_geom(gp);
357 	return (error);
358 }
359 
360 static int
361 g_nop_destroy(struct g_geom *gp, boolean_t force)
362 {
363 	struct g_nop_softc *sc;
364 	struct g_provider *pp;
365 
366 	g_topology_assert();
367 	sc = gp->softc;
368 	if (sc == NULL)
369 		return (ENXIO);
370 	free(sc->sc_physpath, M_GEOM);
371 	pp = LIST_FIRST(&gp->provider);
372 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
373 		if (force) {
374 			G_NOP_DEBUG(0, "Device %s is still open, so it "
375 			    "can't be definitely removed.", pp->name);
376 		} else {
377 			G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
378 			    pp->name, pp->acr, pp->acw, pp->ace);
379 			return (EBUSY);
380 		}
381 	} else {
382 		G_NOP_DEBUG(0, "Device %s removed.", gp->name);
383 	}
384 	gp->softc = NULL;
385 	mtx_destroy(&sc->sc_lock);
386 	g_free(sc);
387 	g_wither_geom(gp, ENXIO);
388 
389 	return (0);
390 }
391 
392 static int
393 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
394 {
395 
396 	return (g_nop_destroy(gp, 0));
397 }
398 
399 static void
400 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
401 {
402 	struct g_provider *pp;
403 	intmax_t *error, *rfailprob, *wfailprob, *offset, *secsize, *size,
404 	    *stripesize, *stripeoffset;
405 	const char *name, *physpath;
406 	char param[16];
407 	int i, *nargs;
408 
409 	g_topology_assert();
410 
411 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
412 	if (nargs == NULL) {
413 		gctl_error(req, "No '%s' argument", "nargs");
414 		return;
415 	}
416 	if (*nargs <= 0) {
417 		gctl_error(req, "Missing device(s).");
418 		return;
419 	}
420 	error = gctl_get_paraml(req, "error", sizeof(*error));
421 	if (error == NULL) {
422 		gctl_error(req, "No '%s' argument", "error");
423 		return;
424 	}
425 	rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
426 	if (rfailprob == NULL) {
427 		gctl_error(req, "No '%s' argument", "rfailprob");
428 		return;
429 	}
430 	if (*rfailprob < -1 || *rfailprob > 100) {
431 		gctl_error(req, "Invalid '%s' argument", "rfailprob");
432 		return;
433 	}
434 	wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
435 	if (wfailprob == NULL) {
436 		gctl_error(req, "No '%s' argument", "wfailprob");
437 		return;
438 	}
439 	if (*wfailprob < -1 || *wfailprob > 100) {
440 		gctl_error(req, "Invalid '%s' argument", "wfailprob");
441 		return;
442 	}
443 	offset = gctl_get_paraml(req, "offset", sizeof(*offset));
444 	if (offset == NULL) {
445 		gctl_error(req, "No '%s' argument", "offset");
446 		return;
447 	}
448 	if (*offset < 0) {
449 		gctl_error(req, "Invalid '%s' argument", "offset");
450 		return;
451 	}
452 	size = gctl_get_paraml(req, "size", sizeof(*size));
453 	if (size == NULL) {
454 		gctl_error(req, "No '%s' argument", "size");
455 		return;
456 	}
457 	if (*size < 0) {
458 		gctl_error(req, "Invalid '%s' argument", "size");
459 		return;
460 	}
461 	secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
462 	if (secsize == NULL) {
463 		gctl_error(req, "No '%s' argument", "secsize");
464 		return;
465 	}
466 	if (*secsize < 0) {
467 		gctl_error(req, "Invalid '%s' argument", "secsize");
468 		return;
469 	}
470 	stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
471 	if (stripesize == NULL) {
472 		gctl_error(req, "No '%s' argument", "stripesize");
473 		return;
474 	}
475 	if (*stripesize < 0) {
476 		gctl_error(req, "Invalid '%s' argument", "stripesize");
477 		return;
478 	}
479 	stripeoffset = gctl_get_paraml(req, "stripeoffset", sizeof(*stripeoffset));
480 	if (stripeoffset == NULL) {
481 		gctl_error(req, "No '%s' argument", "stripeoffset");
482 		return;
483 	}
484 	if (*stripeoffset < 0) {
485 		gctl_error(req, "Invalid '%s' argument", "stripeoffset");
486 		return;
487 	}
488 	physpath = gctl_get_asciiparam(req, "physpath");
489 
490 	for (i = 0; i < *nargs; i++) {
491 		snprintf(param, sizeof(param), "arg%d", i);
492 		name = gctl_get_asciiparam(req, param);
493 		if (name == NULL) {
494 			gctl_error(req, "No 'arg%d' argument", i);
495 			return;
496 		}
497 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
498 			name += strlen("/dev/");
499 		pp = g_provider_by_name(name);
500 		if (pp == NULL) {
501 			G_NOP_DEBUG(1, "Provider %s is invalid.", name);
502 			gctl_error(req, "Provider %s is invalid.", name);
503 			return;
504 		}
505 		if (g_nop_create(req, mp, pp,
506 		    *error == -1 ? EIO : (int)*error,
507 		    *rfailprob == -1 ? 0 : (u_int)*rfailprob,
508 		    *wfailprob == -1 ? 0 : (u_int)*wfailprob,
509 		    (off_t)*offset, (off_t)*size, (u_int)*secsize,
510 		    (off_t)*stripesize, (off_t)*stripeoffset,
511 		    physpath) != 0) {
512 			return;
513 		}
514 	}
515 }
516 
517 static void
518 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
519 {
520 	struct g_nop_softc *sc;
521 	struct g_provider *pp;
522 	intmax_t *error, *rfailprob, *wfailprob;
523 	const char *name;
524 	char param[16];
525 	int i, *nargs;
526 
527 	g_topology_assert();
528 
529 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
530 	if (nargs == NULL) {
531 		gctl_error(req, "No '%s' argument", "nargs");
532 		return;
533 	}
534 	if (*nargs <= 0) {
535 		gctl_error(req, "Missing device(s).");
536 		return;
537 	}
538 	error = gctl_get_paraml(req, "error", sizeof(*error));
539 	if (error == NULL) {
540 		gctl_error(req, "No '%s' argument", "error");
541 		return;
542 	}
543 	rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
544 	if (rfailprob == NULL) {
545 		gctl_error(req, "No '%s' argument", "rfailprob");
546 		return;
547 	}
548 	if (*rfailprob < -1 || *rfailprob > 100) {
549 		gctl_error(req, "Invalid '%s' argument", "rfailprob");
550 		return;
551 	}
552 	wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
553 	if (wfailprob == NULL) {
554 		gctl_error(req, "No '%s' argument", "wfailprob");
555 		return;
556 	}
557 	if (*wfailprob < -1 || *wfailprob > 100) {
558 		gctl_error(req, "Invalid '%s' argument", "wfailprob");
559 		return;
560 	}
561 
562 	for (i = 0; i < *nargs; i++) {
563 		snprintf(param, sizeof(param), "arg%d", i);
564 		name = gctl_get_asciiparam(req, param);
565 		if (name == NULL) {
566 			gctl_error(req, "No 'arg%d' argument", i);
567 			return;
568 		}
569 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
570 			name += strlen("/dev/");
571 		pp = g_provider_by_name(name);
572 		if (pp == NULL || pp->geom->class != mp) {
573 			G_NOP_DEBUG(1, "Provider %s is invalid.", name);
574 			gctl_error(req, "Provider %s is invalid.", name);
575 			return;
576 		}
577 		sc = pp->geom->softc;
578 		if (*error != -1)
579 			sc->sc_error = (int)*error;
580 		if (*rfailprob != -1)
581 			sc->sc_rfailprob = (u_int)*rfailprob;
582 		if (*wfailprob != -1)
583 			sc->sc_wfailprob = (u_int)*wfailprob;
584 	}
585 }
586 
587 static struct g_geom *
588 g_nop_find_geom(struct g_class *mp, const char *name)
589 {
590 	struct g_geom *gp;
591 
592 	LIST_FOREACH(gp, &mp->geom, geom) {
593 		if (strcmp(gp->name, name) == 0)
594 			return (gp);
595 	}
596 	return (NULL);
597 }
598 
599 static void
600 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
601 {
602 	int *nargs, *force, error, i;
603 	struct g_geom *gp;
604 	const char *name;
605 	char param[16];
606 
607 	g_topology_assert();
608 
609 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
610 	if (nargs == NULL) {
611 		gctl_error(req, "No '%s' argument", "nargs");
612 		return;
613 	}
614 	if (*nargs <= 0) {
615 		gctl_error(req, "Missing device(s).");
616 		return;
617 	}
618 	force = gctl_get_paraml(req, "force", sizeof(*force));
619 	if (force == NULL) {
620 		gctl_error(req, "No 'force' argument");
621 		return;
622 	}
623 
624 	for (i = 0; i < *nargs; i++) {
625 		snprintf(param, sizeof(param), "arg%d", i);
626 		name = gctl_get_asciiparam(req, param);
627 		if (name == NULL) {
628 			gctl_error(req, "No 'arg%d' argument", i);
629 			return;
630 		}
631 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
632 			name += strlen("/dev/");
633 		gp = g_nop_find_geom(mp, name);
634 		if (gp == NULL) {
635 			G_NOP_DEBUG(1, "Device %s is invalid.", name);
636 			gctl_error(req, "Device %s is invalid.", name);
637 			return;
638 		}
639 		error = g_nop_destroy(gp, *force);
640 		if (error != 0) {
641 			gctl_error(req, "Cannot destroy device %s (error=%d).",
642 			    gp->name, error);
643 			return;
644 		}
645 	}
646 }
647 
648 static void
649 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
650 {
651 	struct g_nop_softc *sc;
652 	struct g_provider *pp;
653 	const char *name;
654 	char param[16];
655 	int i, *nargs;
656 
657 	g_topology_assert();
658 
659 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
660 	if (nargs == NULL) {
661 		gctl_error(req, "No '%s' argument", "nargs");
662 		return;
663 	}
664 	if (*nargs <= 0) {
665 		gctl_error(req, "Missing device(s).");
666 		return;
667 	}
668 
669 	for (i = 0; i < *nargs; i++) {
670 		snprintf(param, sizeof(param), "arg%d", i);
671 		name = gctl_get_asciiparam(req, param);
672 		if (name == NULL) {
673 			gctl_error(req, "No 'arg%d' argument", i);
674 			return;
675 		}
676 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
677 			name += strlen("/dev/");
678 		pp = g_provider_by_name(name);
679 		if (pp == NULL || pp->geom->class != mp) {
680 			G_NOP_DEBUG(1, "Provider %s is invalid.", name);
681 			gctl_error(req, "Provider %s is invalid.", name);
682 			return;
683 		}
684 		sc = pp->geom->softc;
685 		sc->sc_reads = 0;
686 		sc->sc_writes = 0;
687 		sc->sc_deletes = 0;
688 		sc->sc_getattrs = 0;
689 		sc->sc_flushes = 0;
690 		sc->sc_cmd0s = 0;
691 		sc->sc_cmd1s = 0;
692 		sc->sc_cmd2s = 0;
693 		sc->sc_readbytes = 0;
694 		sc->sc_wrotebytes = 0;
695 	}
696 }
697 
698 static void
699 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
700 {
701 	uint32_t *version;
702 
703 	g_topology_assert();
704 
705 	version = gctl_get_paraml(req, "version", sizeof(*version));
706 	if (version == NULL) {
707 		gctl_error(req, "No '%s' argument.", "version");
708 		return;
709 	}
710 	if (*version != G_NOP_VERSION) {
711 		gctl_error(req, "Userland and kernel parts are out of sync.");
712 		return;
713 	}
714 
715 	if (strcmp(verb, "create") == 0) {
716 		g_nop_ctl_create(req, mp);
717 		return;
718 	} else if (strcmp(verb, "configure") == 0) {
719 		g_nop_ctl_configure(req, mp);
720 		return;
721 	} else if (strcmp(verb, "destroy") == 0) {
722 		g_nop_ctl_destroy(req, mp);
723 		return;
724 	} else if (strcmp(verb, "reset") == 0) {
725 		g_nop_ctl_reset(req, mp);
726 		return;
727 	}
728 
729 	gctl_error(req, "Unknown verb.");
730 }
731 
732 static void
733 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
734     struct g_consumer *cp, struct g_provider *pp)
735 {
736 	struct g_nop_softc *sc;
737 
738 	if (pp != NULL || cp != NULL)
739 		return;
740 	sc = gp->softc;
741 	sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
742 	    (intmax_t)sc->sc_offset);
743 	sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
744 	    sc->sc_rfailprob);
745 	sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
746 	    sc->sc_wfailprob);
747 	sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
748 	sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
749 	sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
750 	sbuf_printf(sb, "%s<Deletes>%ju</Deletes>\n", indent, sc->sc_deletes);
751 	sbuf_printf(sb, "%s<Getattrs>%ju</Getattrs>\n", indent, sc->sc_getattrs);
752 	sbuf_printf(sb, "%s<Flushes>%ju</Flushes>\n", indent, sc->sc_flushes);
753 	sbuf_printf(sb, "%s<Cmd0s>%ju</Cmd0s>\n", indent, sc->sc_cmd0s);
754 	sbuf_printf(sb, "%s<Cmd1s>%ju</Cmd1s>\n", indent, sc->sc_cmd1s);
755 	sbuf_printf(sb, "%s<Cmd2s>%ju</Cmd2s>\n", indent, sc->sc_cmd2s);
756 	sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
757 	    sc->sc_readbytes);
758 	sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
759 	    sc->sc_wrotebytes);
760 }
761 
762 DECLARE_GEOM_CLASS(g_nop_class, g_nop);
763 MODULE_VERSION(geom_nop, 0);
764