xref: /freebsd/sys/geom/nop/g_nop.c (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
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 void
101 g_nop_start(struct bio *bp)
102 {
103 	struct g_nop_softc *sc;
104 	struct g_geom *gp;
105 	struct g_provider *pp;
106 	struct bio *cbp;
107 	u_int failprob = 0;
108 
109 	gp = bp->bio_to->geom;
110 	sc = gp->softc;
111 	G_NOP_LOGREQ(bp, "Request received.");
112 	mtx_lock(&sc->sc_lock);
113 	switch (bp->bio_cmd) {
114 	case BIO_READ:
115 		sc->sc_reads++;
116 		sc->sc_readbytes += bp->bio_length;
117 		failprob = sc->sc_rfailprob;
118 		break;
119 	case BIO_WRITE:
120 		sc->sc_writes++;
121 		sc->sc_wrotebytes += bp->bio_length;
122 		failprob = sc->sc_wfailprob;
123 		break;
124 	case BIO_DELETE:
125 		sc->sc_deletes++;
126 		break;
127 	case BIO_GETATTR:
128 		sc->sc_getattrs++;
129 		break;
130 	case BIO_FLUSH:
131 		sc->sc_flushes++;
132 		break;
133 	case BIO_CMD0:
134 		sc->sc_cmd0s++;
135 		break;
136 	case BIO_CMD1:
137 		sc->sc_cmd1s++;
138 		break;
139 	case BIO_CMD2:
140 		sc->sc_cmd2s++;
141 		break;
142 	}
143 	mtx_unlock(&sc->sc_lock);
144 	if (failprob > 0) {
145 		u_int rval;
146 
147 		rval = arc4random() % 100;
148 		if (rval < failprob) {
149 			G_NOP_LOGREQLVL(1, bp, "Returning error=%d.", sc->sc_error);
150 			g_io_deliver(bp, sc->sc_error);
151 			return;
152 		}
153 	}
154 	cbp = g_clone_bio(bp);
155 	if (cbp == NULL) {
156 		g_io_deliver(bp, ENOMEM);
157 		return;
158 	}
159 	cbp->bio_done = g_std_done;
160 	cbp->bio_offset = bp->bio_offset + sc->sc_offset;
161 	pp = LIST_FIRST(&gp->provider);
162 	KASSERT(pp != NULL, ("NULL pp"));
163 	cbp->bio_to = pp;
164 	G_NOP_LOGREQ(cbp, "Sending request.");
165 	g_io_request(cbp, LIST_FIRST(&gp->consumer));
166 }
167 
168 static int
169 g_nop_access(struct g_provider *pp, int dr, int dw, int de)
170 {
171 	struct g_geom *gp;
172 	struct g_consumer *cp;
173 	int error;
174 
175 	gp = pp->geom;
176 	cp = LIST_FIRST(&gp->consumer);
177 	error = g_access(cp, dr, dw, de);
178 
179 	return (error);
180 }
181 
182 static int
183 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
184     int ioerror, u_int rfailprob, u_int wfailprob, off_t offset, off_t size,
185     u_int secsize, u_int stripesize, u_int stripeoffset)
186 {
187 	struct g_nop_softc *sc;
188 	struct g_geom *gp;
189 	struct g_provider *newpp;
190 	struct g_consumer *cp;
191 	char name[64];
192 	int error;
193 	off_t explicitsize;
194 
195 	g_topology_assert();
196 
197 	gp = NULL;
198 	newpp = NULL;
199 	cp = NULL;
200 
201 	if ((offset % pp->sectorsize) != 0) {
202 		gctl_error(req, "Invalid offset for provider %s.", pp->name);
203 		return (EINVAL);
204 	}
205 	if ((size % pp->sectorsize) != 0) {
206 		gctl_error(req, "Invalid size for provider %s.", pp->name);
207 		return (EINVAL);
208 	}
209 	if (offset >= pp->mediasize) {
210 		gctl_error(req, "Invalid offset for provider %s.", pp->name);
211 		return (EINVAL);
212 	}
213 	explicitsize = size;
214 	if (size == 0)
215 		size = pp->mediasize - offset;
216 	if (offset + size > pp->mediasize) {
217 		gctl_error(req, "Invalid size for provider %s.", pp->name);
218 		return (EINVAL);
219 	}
220 	if (secsize == 0)
221 		secsize = pp->sectorsize;
222 	else if ((secsize % pp->sectorsize) != 0) {
223 		gctl_error(req, "Invalid secsize for provider %s.", pp->name);
224 		return (EINVAL);
225 	}
226 	if (secsize > MAXPHYS) {
227 		gctl_error(req, "secsize is too big.");
228 		return (EINVAL);
229 	}
230 	size -= size % secsize;
231 	if ((stripesize % pp->sectorsize) != 0) {
232 		gctl_error(req, "Invalid stripesize for provider %s.", pp->name);
233 		return (EINVAL);
234 	}
235 	if ((stripeoffset % pp->sectorsize) != 0) {
236 		gctl_error(req, "Invalid stripeoffset for provider %s.", pp->name);
237 		return (EINVAL);
238 	}
239 	if (stripesize != 0 && stripeoffset >= stripesize) {
240 		gctl_error(req, "stripeoffset is too big.");
241 		return (EINVAL);
242 	}
243 	snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX);
244 	LIST_FOREACH(gp, &mp->geom, geom) {
245 		if (strcmp(gp->name, name) == 0) {
246 			gctl_error(req, "Provider %s already exists.", name);
247 			return (EEXIST);
248 		}
249 	}
250 	gp = g_new_geomf(mp, "%s", name);
251 	sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
252 	sc->sc_offset = offset;
253 	sc->sc_explicitsize = explicitsize;
254 	sc->sc_stripesize = stripesize;
255 	sc->sc_stripeoffset = stripeoffset;
256 	sc->sc_error = ioerror;
257 	sc->sc_rfailprob = rfailprob;
258 	sc->sc_wfailprob = wfailprob;
259 	sc->sc_reads = 0;
260 	sc->sc_writes = 0;
261 	sc->sc_deletes = 0;
262 	sc->sc_getattrs = 0;
263 	sc->sc_flushes = 0;
264 	sc->sc_cmd0s = 0;
265 	sc->sc_cmd1s = 0;
266 	sc->sc_cmd2s = 0;
267 	sc->sc_readbytes = 0;
268 	sc->sc_wrotebytes = 0;
269 	mtx_init(&sc->sc_lock, "gnop lock", NULL, MTX_DEF);
270 	gp->softc = sc;
271 	gp->start = g_nop_start;
272 	gp->orphan = g_nop_orphan;
273 	gp->resize = g_nop_resize;
274 	gp->access = g_nop_access;
275 	gp->dumpconf = g_nop_dumpconf;
276 
277 	newpp = g_new_providerf(gp, "%s", gp->name);
278 	newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
279 	newpp->mediasize = size;
280 	newpp->sectorsize = secsize;
281 	newpp->stripesize = stripesize;
282 	newpp->stripeoffset = stripeoffset;
283 
284 	cp = g_new_consumer(gp);
285 	cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
286 	error = g_attach(cp, pp);
287 	if (error != 0) {
288 		gctl_error(req, "Cannot attach to provider %s.", pp->name);
289 		goto fail;
290 	}
291 
292 	newpp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
293 	g_error_provider(newpp, 0);
294 	G_NOP_DEBUG(0, "Device %s created.", gp->name);
295 	return (0);
296 fail:
297 	if (cp->provider != NULL)
298 		g_detach(cp);
299 	g_destroy_consumer(cp);
300 	g_destroy_provider(newpp);
301 	mtx_destroy(&sc->sc_lock);
302 	g_free(gp->softc);
303 	g_destroy_geom(gp);
304 	return (error);
305 }
306 
307 static int
308 g_nop_destroy(struct g_geom *gp, boolean_t force)
309 {
310 	struct g_nop_softc *sc;
311 	struct g_provider *pp;
312 
313 	g_topology_assert();
314 	sc = gp->softc;
315 	if (sc == NULL)
316 		return (ENXIO);
317 	pp = LIST_FIRST(&gp->provider);
318 	if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
319 		if (force) {
320 			G_NOP_DEBUG(0, "Device %s is still open, so it "
321 			    "can't be definitely removed.", pp->name);
322 		} else {
323 			G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
324 			    pp->name, pp->acr, pp->acw, pp->ace);
325 			return (EBUSY);
326 		}
327 	} else {
328 		G_NOP_DEBUG(0, "Device %s removed.", gp->name);
329 	}
330 	gp->softc = NULL;
331 	mtx_destroy(&sc->sc_lock);
332 	g_free(sc);
333 	g_wither_geom(gp, ENXIO);
334 
335 	return (0);
336 }
337 
338 static int
339 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
340 {
341 
342 	return (g_nop_destroy(gp, 0));
343 }
344 
345 static void
346 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
347 {
348 	struct g_provider *pp;
349 	intmax_t *error, *rfailprob, *wfailprob, *offset, *secsize, *size,
350 	    *stripesize, *stripeoffset;
351 	const char *name;
352 	char param[16];
353 	int i, *nargs;
354 
355 	g_topology_assert();
356 
357 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
358 	if (nargs == NULL) {
359 		gctl_error(req, "No '%s' argument", "nargs");
360 		return;
361 	}
362 	if (*nargs <= 0) {
363 		gctl_error(req, "Missing device(s).");
364 		return;
365 	}
366 	error = gctl_get_paraml(req, "error", sizeof(*error));
367 	if (error == NULL) {
368 		gctl_error(req, "No '%s' argument", "error");
369 		return;
370 	}
371 	rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
372 	if (rfailprob == NULL) {
373 		gctl_error(req, "No '%s' argument", "rfailprob");
374 		return;
375 	}
376 	if (*rfailprob < -1 || *rfailprob > 100) {
377 		gctl_error(req, "Invalid '%s' argument", "rfailprob");
378 		return;
379 	}
380 	wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
381 	if (wfailprob == NULL) {
382 		gctl_error(req, "No '%s' argument", "wfailprob");
383 		return;
384 	}
385 	if (*wfailprob < -1 || *wfailprob > 100) {
386 		gctl_error(req, "Invalid '%s' argument", "wfailprob");
387 		return;
388 	}
389 	offset = gctl_get_paraml(req, "offset", sizeof(*offset));
390 	if (offset == NULL) {
391 		gctl_error(req, "No '%s' argument", "offset");
392 		return;
393 	}
394 	if (*offset < 0) {
395 		gctl_error(req, "Invalid '%s' argument", "offset");
396 		return;
397 	}
398 	size = gctl_get_paraml(req, "size", sizeof(*size));
399 	if (size == NULL) {
400 		gctl_error(req, "No '%s' argument", "size");
401 		return;
402 	}
403 	if (*size < 0) {
404 		gctl_error(req, "Invalid '%s' argument", "size");
405 		return;
406 	}
407 	secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
408 	if (secsize == NULL) {
409 		gctl_error(req, "No '%s' argument", "secsize");
410 		return;
411 	}
412 	if (*secsize < 0) {
413 		gctl_error(req, "Invalid '%s' argument", "secsize");
414 		return;
415 	}
416 	stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
417 	if (stripesize == NULL) {
418 		gctl_error(req, "No '%s' argument", "stripesize");
419 		return;
420 	}
421 	if (*stripesize < 0) {
422 		gctl_error(req, "Invalid '%s' argument", "stripesize");
423 		return;
424 	}
425 	stripeoffset = gctl_get_paraml(req, "stripeoffset", sizeof(*stripeoffset));
426 	if (stripeoffset == NULL) {
427 		gctl_error(req, "No '%s' argument", "stripeoffset");
428 		return;
429 	}
430 	if (*stripeoffset < 0) {
431 		gctl_error(req, "Invalid '%s' argument", "stripeoffset");
432 		return;
433 	}
434 
435 	for (i = 0; i < *nargs; i++) {
436 		snprintf(param, sizeof(param), "arg%d", i);
437 		name = gctl_get_asciiparam(req, param);
438 		if (name == NULL) {
439 			gctl_error(req, "No 'arg%d' argument", i);
440 			return;
441 		}
442 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
443 			name += strlen("/dev/");
444 		pp = g_provider_by_name(name);
445 		if (pp == NULL) {
446 			G_NOP_DEBUG(1, "Provider %s is invalid.", name);
447 			gctl_error(req, "Provider %s is invalid.", name);
448 			return;
449 		}
450 		if (g_nop_create(req, mp, pp,
451 		    *error == -1 ? EIO : (int)*error,
452 		    *rfailprob == -1 ? 0 : (u_int)*rfailprob,
453 		    *wfailprob == -1 ? 0 : (u_int)*wfailprob,
454 		    (off_t)*offset, (off_t)*size, (u_int)*secsize,
455 		    (u_int)*stripesize, (u_int)*stripeoffset) != 0) {
456 			return;
457 		}
458 	}
459 }
460 
461 static void
462 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
463 {
464 	struct g_nop_softc *sc;
465 	struct g_provider *pp;
466 	intmax_t *error, *rfailprob, *wfailprob;
467 	const char *name;
468 	char param[16];
469 	int i, *nargs;
470 
471 	g_topology_assert();
472 
473 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
474 	if (nargs == NULL) {
475 		gctl_error(req, "No '%s' argument", "nargs");
476 		return;
477 	}
478 	if (*nargs <= 0) {
479 		gctl_error(req, "Missing device(s).");
480 		return;
481 	}
482 	error = gctl_get_paraml(req, "error", sizeof(*error));
483 	if (error == NULL) {
484 		gctl_error(req, "No '%s' argument", "error");
485 		return;
486 	}
487 	rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
488 	if (rfailprob == NULL) {
489 		gctl_error(req, "No '%s' argument", "rfailprob");
490 		return;
491 	}
492 	if (*rfailprob < -1 || *rfailprob > 100) {
493 		gctl_error(req, "Invalid '%s' argument", "rfailprob");
494 		return;
495 	}
496 	wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
497 	if (wfailprob == NULL) {
498 		gctl_error(req, "No '%s' argument", "wfailprob");
499 		return;
500 	}
501 	if (*wfailprob < -1 || *wfailprob > 100) {
502 		gctl_error(req, "Invalid '%s' argument", "wfailprob");
503 		return;
504 	}
505 
506 	for (i = 0; i < *nargs; i++) {
507 		snprintf(param, sizeof(param), "arg%d", i);
508 		name = gctl_get_asciiparam(req, param);
509 		if (name == NULL) {
510 			gctl_error(req, "No 'arg%d' argument", i);
511 			return;
512 		}
513 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
514 			name += strlen("/dev/");
515 		pp = g_provider_by_name(name);
516 		if (pp == NULL || pp->geom->class != mp) {
517 			G_NOP_DEBUG(1, "Provider %s is invalid.", name);
518 			gctl_error(req, "Provider %s is invalid.", name);
519 			return;
520 		}
521 		sc = pp->geom->softc;
522 		if (*error != -1)
523 			sc->sc_error = (int)*error;
524 		if (*rfailprob != -1)
525 			sc->sc_rfailprob = (u_int)*rfailprob;
526 		if (*wfailprob != -1)
527 			sc->sc_wfailprob = (u_int)*wfailprob;
528 	}
529 }
530 
531 static struct g_geom *
532 g_nop_find_geom(struct g_class *mp, const char *name)
533 {
534 	struct g_geom *gp;
535 
536 	LIST_FOREACH(gp, &mp->geom, geom) {
537 		if (strcmp(gp->name, name) == 0)
538 			return (gp);
539 	}
540 	return (NULL);
541 }
542 
543 static void
544 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
545 {
546 	int *nargs, *force, error, i;
547 	struct g_geom *gp;
548 	const char *name;
549 	char param[16];
550 
551 	g_topology_assert();
552 
553 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
554 	if (nargs == NULL) {
555 		gctl_error(req, "No '%s' argument", "nargs");
556 		return;
557 	}
558 	if (*nargs <= 0) {
559 		gctl_error(req, "Missing device(s).");
560 		return;
561 	}
562 	force = gctl_get_paraml(req, "force", sizeof(*force));
563 	if (force == NULL) {
564 		gctl_error(req, "No 'force' argument");
565 		return;
566 	}
567 
568 	for (i = 0; i < *nargs; i++) {
569 		snprintf(param, sizeof(param), "arg%d", i);
570 		name = gctl_get_asciiparam(req, param);
571 		if (name == NULL) {
572 			gctl_error(req, "No 'arg%d' argument", i);
573 			return;
574 		}
575 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
576 			name += strlen("/dev/");
577 		gp = g_nop_find_geom(mp, name);
578 		if (gp == NULL) {
579 			G_NOP_DEBUG(1, "Device %s is invalid.", name);
580 			gctl_error(req, "Device %s is invalid.", name);
581 			return;
582 		}
583 		error = g_nop_destroy(gp, *force);
584 		if (error != 0) {
585 			gctl_error(req, "Cannot destroy device %s (error=%d).",
586 			    gp->name, error);
587 			return;
588 		}
589 	}
590 }
591 
592 static void
593 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
594 {
595 	struct g_nop_softc *sc;
596 	struct g_provider *pp;
597 	const char *name;
598 	char param[16];
599 	int i, *nargs;
600 
601 	g_topology_assert();
602 
603 	nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
604 	if (nargs == NULL) {
605 		gctl_error(req, "No '%s' argument", "nargs");
606 		return;
607 	}
608 	if (*nargs <= 0) {
609 		gctl_error(req, "Missing device(s).");
610 		return;
611 	}
612 
613 	for (i = 0; i < *nargs; i++) {
614 		snprintf(param, sizeof(param), "arg%d", i);
615 		name = gctl_get_asciiparam(req, param);
616 		if (name == NULL) {
617 			gctl_error(req, "No 'arg%d' argument", i);
618 			return;
619 		}
620 		if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
621 			name += strlen("/dev/");
622 		pp = g_provider_by_name(name);
623 		if (pp == NULL || pp->geom->class != mp) {
624 			G_NOP_DEBUG(1, "Provider %s is invalid.", name);
625 			gctl_error(req, "Provider %s is invalid.", name);
626 			return;
627 		}
628 		sc = pp->geom->softc;
629 		sc->sc_reads = 0;
630 		sc->sc_writes = 0;
631 		sc->sc_deletes = 0;
632 		sc->sc_getattrs = 0;
633 		sc->sc_flushes = 0;
634 		sc->sc_cmd0s = 0;
635 		sc->sc_cmd1s = 0;
636 		sc->sc_cmd2s = 0;
637 		sc->sc_readbytes = 0;
638 		sc->sc_wrotebytes = 0;
639 	}
640 }
641 
642 static void
643 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
644 {
645 	uint32_t *version;
646 
647 	g_topology_assert();
648 
649 	version = gctl_get_paraml(req, "version", sizeof(*version));
650 	if (version == NULL) {
651 		gctl_error(req, "No '%s' argument.", "version");
652 		return;
653 	}
654 	if (*version != G_NOP_VERSION) {
655 		gctl_error(req, "Userland and kernel parts are out of sync.");
656 		return;
657 	}
658 
659 	if (strcmp(verb, "create") == 0) {
660 		g_nop_ctl_create(req, mp);
661 		return;
662 	} else if (strcmp(verb, "configure") == 0) {
663 		g_nop_ctl_configure(req, mp);
664 		return;
665 	} else if (strcmp(verb, "destroy") == 0) {
666 		g_nop_ctl_destroy(req, mp);
667 		return;
668 	} else if (strcmp(verb, "reset") == 0) {
669 		g_nop_ctl_reset(req, mp);
670 		return;
671 	}
672 
673 	gctl_error(req, "Unknown verb.");
674 }
675 
676 static void
677 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
678     struct g_consumer *cp, struct g_provider *pp)
679 {
680 	struct g_nop_softc *sc;
681 
682 	if (pp != NULL || cp != NULL)
683 		return;
684 	sc = gp->softc;
685 	sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
686 	    (intmax_t)sc->sc_offset);
687 	sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
688 	    sc->sc_rfailprob);
689 	sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
690 	    sc->sc_wfailprob);
691 	sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
692 	sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
693 	sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
694 	sbuf_printf(sb, "%s<Deletes>%ju</Deletes>\n", indent, sc->sc_deletes);
695 	sbuf_printf(sb, "%s<Getattrs>%ju</Getattrs>\n", indent, sc->sc_getattrs);
696 	sbuf_printf(sb, "%s<Flushes>%ju</Flushes>\n", indent, sc->sc_flushes);
697 	sbuf_printf(sb, "%s<Cmd0s>%ju</Cmd0s>\n", indent, sc->sc_cmd0s);
698 	sbuf_printf(sb, "%s<Cmd1s>%ju</Cmd1s>\n", indent, sc->sc_cmd1s);
699 	sbuf_printf(sb, "%s<Cmd2s>%ju</Cmd2s>\n", indent, sc->sc_cmd2s);
700 	sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
701 	    sc->sc_readbytes);
702 	sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
703 	    sc->sc_wrotebytes);
704 }
705 
706 DECLARE_GEOM_CLASS(g_nop_class, g_nop);
707