2 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
35 #include <sys/mutex.h>
38 #include <sys/sysctl.h>
39 #include <sys/malloc.h>
40 #include <geom/geom.h>
41 #include <geom/nop/g_nop.h>
44 SYSCTL_DECL(_kern_geom);
45 static SYSCTL_NODE(_kern_geom, OID_AUTO, nop, CTLFLAG_RW, 0, "GEOM_NOP stuff");
46 static u_int g_nop_debug = 0;
47 SYSCTL_UINT(_kern_geom_nop, OID_AUTO, debug, CTLFLAG_RW, &g_nop_debug, 0,
50 static int g_nop_destroy(struct g_geom *gp, boolean_t force);
51 static int g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp,
53 static void g_nop_config(struct gctl_req *req, struct g_class *mp,
55 static void g_nop_dumpconf(struct sbuf *sb, const char *indent,
56 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
58 struct g_class g_nop_class = {
59 .name = G_NOP_CLASS_NAME,
61 .ctlreq = g_nop_config,
62 .destroy_geom = g_nop_destroy_geom
67 g_nop_orphan(struct g_consumer *cp)
71 g_nop_destroy(cp->geom, 1);
75 g_nop_resize(struct g_consumer *cp)
77 struct g_nop_softc *sc;
79 struct g_provider *pp;
87 if (sc->sc_explicitsize != 0)
89 if (cp->provider->mediasize < sc->sc_offset) {
93 size = cp->provider->mediasize - sc->sc_offset;
94 LIST_FOREACH(pp, &gp->provider, provider)
95 g_resize_provider(pp, size);
99 g_nop_start(struct bio *bp)
101 struct g_nop_softc *sc;
103 struct g_provider *pp;
107 gp = bp->bio_to->geom;
109 G_NOP_LOGREQ(bp, "Request received.");
110 mtx_lock(&sc->sc_lock);
111 switch (bp->bio_cmd) {
114 sc->sc_readbytes += bp->bio_length;
115 failprob = sc->sc_rfailprob;
119 sc->sc_wrotebytes += bp->bio_length;
120 failprob = sc->sc_wfailprob;
141 mtx_unlock(&sc->sc_lock);
145 rval = arc4random() % 100;
146 if (rval < failprob) {
147 G_NOP_LOGREQLVL(1, bp, "Returning error=%d.", sc->sc_error);
148 g_io_deliver(bp, sc->sc_error);
152 cbp = g_clone_bio(bp);
154 g_io_deliver(bp, ENOMEM);
157 cbp->bio_done = g_std_done;
158 cbp->bio_offset = bp->bio_offset + sc->sc_offset;
159 pp = LIST_FIRST(&gp->provider);
160 KASSERT(pp != NULL, ("NULL pp"));
162 G_NOP_LOGREQ(cbp, "Sending request.");
163 g_io_request(cbp, LIST_FIRST(&gp->consumer));
167 g_nop_access(struct g_provider *pp, int dr, int dw, int de)
170 struct g_consumer *cp;
174 cp = LIST_FIRST(&gp->consumer);
175 error = g_access(cp, dr, dw, de);
181 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
182 int ioerror, u_int rfailprob, u_int wfailprob, off_t offset, off_t size,
183 u_int secsize, u_int stripesize, u_int stripeoffset)
185 struct g_nop_softc *sc;
187 struct g_provider *newpp;
188 struct g_consumer *cp;
199 if ((offset % pp->sectorsize) != 0) {
200 gctl_error(req, "Invalid offset for provider %s.", pp->name);
203 if ((size % pp->sectorsize) != 0) {
204 gctl_error(req, "Invalid size for provider %s.", pp->name);
207 if (offset >= pp->mediasize) {
208 gctl_error(req, "Invalid offset for provider %s.", pp->name);
213 size = pp->mediasize - offset;
214 if (offset + size > pp->mediasize) {
215 gctl_error(req, "Invalid size for provider %s.", pp->name);
219 secsize = pp->sectorsize;
220 else if ((secsize % pp->sectorsize) != 0) {
221 gctl_error(req, "Invalid secsize for provider %s.", pp->name);
224 if (secsize > MAXPHYS) {
225 gctl_error(req, "secsize is too big.");
228 size -= size % secsize;
229 if ((stripesize % pp->sectorsize) != 0) {
230 gctl_error(req, "Invalid stripesize for provider %s.", pp->name);
233 if ((stripeoffset % pp->sectorsize) != 0) {
234 gctl_error(req, "Invalid stripeoffset for provider %s.", pp->name);
237 if (stripesize != 0 && stripeoffset >= stripesize) {
238 gctl_error(req, "stripeoffset is too big.");
241 snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX);
242 LIST_FOREACH(gp, &mp->geom, geom) {
243 if (strcmp(gp->name, name) == 0) {
244 gctl_error(req, "Provider %s already exists.", name);
248 gp = g_new_geomf(mp, "%s", name);
249 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
250 sc->sc_offset = offset;
251 sc->sc_explicitsize = explicitsize;
252 sc->sc_stripesize = stripesize;
253 sc->sc_stripeoffset = stripeoffset;
254 sc->sc_error = ioerror;
255 sc->sc_rfailprob = rfailprob;
256 sc->sc_wfailprob = wfailprob;
265 sc->sc_readbytes = 0;
266 sc->sc_wrotebytes = 0;
267 mtx_init(&sc->sc_lock, "gnop lock", NULL, MTX_DEF);
269 gp->start = g_nop_start;
270 gp->orphan = g_nop_orphan;
271 gp->resize = g_nop_resize;
272 gp->access = g_nop_access;
273 gp->dumpconf = g_nop_dumpconf;
275 newpp = g_new_providerf(gp, "%s", gp->name);
276 newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
277 newpp->mediasize = size;
278 newpp->sectorsize = secsize;
279 newpp->stripesize = stripesize;
280 newpp->stripeoffset = stripeoffset;
282 cp = g_new_consumer(gp);
283 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
284 error = g_attach(cp, pp);
286 gctl_error(req, "Cannot attach to provider %s.", pp->name);
290 newpp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
291 g_error_provider(newpp, 0);
292 G_NOP_DEBUG(0, "Device %s created.", gp->name);
295 if (cp->provider != NULL)
297 g_destroy_consumer(cp);
298 g_destroy_provider(newpp);
299 mtx_destroy(&sc->sc_lock);
306 g_nop_destroy(struct g_geom *gp, boolean_t force)
308 struct g_nop_softc *sc;
309 struct g_provider *pp;
315 pp = LIST_FIRST(&gp->provider);
316 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
318 G_NOP_DEBUG(0, "Device %s is still open, so it "
319 "can't be definitely removed.", pp->name);
321 G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
322 pp->name, pp->acr, pp->acw, pp->ace);
326 G_NOP_DEBUG(0, "Device %s removed.", gp->name);
329 mtx_destroy(&sc->sc_lock);
331 g_wither_geom(gp, ENXIO);
337 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
340 return (g_nop_destroy(gp, 0));
344 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
346 struct g_provider *pp;
347 intmax_t *error, *rfailprob, *wfailprob, *offset, *secsize, *size,
348 *stripesize, *stripeoffset;
355 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
357 gctl_error(req, "No '%s' argument", "nargs");
361 gctl_error(req, "Missing device(s).");
364 error = gctl_get_paraml(req, "error", sizeof(*error));
366 gctl_error(req, "No '%s' argument", "error");
369 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
370 if (rfailprob == NULL) {
371 gctl_error(req, "No '%s' argument", "rfailprob");
374 if (*rfailprob < -1 || *rfailprob > 100) {
375 gctl_error(req, "Invalid '%s' argument", "rfailprob");
378 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
379 if (wfailprob == NULL) {
380 gctl_error(req, "No '%s' argument", "wfailprob");
383 if (*wfailprob < -1 || *wfailprob > 100) {
384 gctl_error(req, "Invalid '%s' argument", "wfailprob");
387 offset = gctl_get_paraml(req, "offset", sizeof(*offset));
388 if (offset == NULL) {
389 gctl_error(req, "No '%s' argument", "offset");
393 gctl_error(req, "Invalid '%s' argument", "offset");
396 size = gctl_get_paraml(req, "size", sizeof(*size));
398 gctl_error(req, "No '%s' argument", "size");
402 gctl_error(req, "Invalid '%s' argument", "size");
405 secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
406 if (secsize == NULL) {
407 gctl_error(req, "No '%s' argument", "secsize");
411 gctl_error(req, "Invalid '%s' argument", "secsize");
414 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
415 if (stripesize == NULL) {
416 gctl_error(req, "No '%s' argument", "stripesize");
419 if (*stripesize < 0) {
420 gctl_error(req, "Invalid '%s' argument", "stripesize");
423 stripeoffset = gctl_get_paraml(req, "stripeoffset", sizeof(*stripeoffset));
424 if (stripeoffset == NULL) {
425 gctl_error(req, "No '%s' argument", "stripeoffset");
428 if (*stripeoffset < 0) {
429 gctl_error(req, "Invalid '%s' argument", "stripeoffset");
433 for (i = 0; i < *nargs; i++) {
434 snprintf(param, sizeof(param), "arg%d", i);
435 name = gctl_get_asciiparam(req, param);
437 gctl_error(req, "No 'arg%d' argument", i);
440 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
441 name += strlen("/dev/");
442 pp = g_provider_by_name(name);
444 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
445 gctl_error(req, "Provider %s is invalid.", name);
448 if (g_nop_create(req, mp, pp,
449 *error == -1 ? EIO : (int)*error,
450 *rfailprob == -1 ? 0 : (u_int)*rfailprob,
451 *wfailprob == -1 ? 0 : (u_int)*wfailprob,
452 (off_t)*offset, (off_t)*size, (u_int)*secsize,
453 (u_int)*stripesize, (u_int)*stripeoffset) != 0) {
460 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
462 struct g_nop_softc *sc;
463 struct g_provider *pp;
464 intmax_t *error, *rfailprob, *wfailprob;
471 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
473 gctl_error(req, "No '%s' argument", "nargs");
477 gctl_error(req, "Missing device(s).");
480 error = gctl_get_paraml(req, "error", sizeof(*error));
482 gctl_error(req, "No '%s' argument", "error");
485 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
486 if (rfailprob == NULL) {
487 gctl_error(req, "No '%s' argument", "rfailprob");
490 if (*rfailprob < -1 || *rfailprob > 100) {
491 gctl_error(req, "Invalid '%s' argument", "rfailprob");
494 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
495 if (wfailprob == NULL) {
496 gctl_error(req, "No '%s' argument", "wfailprob");
499 if (*wfailprob < -1 || *wfailprob > 100) {
500 gctl_error(req, "Invalid '%s' argument", "wfailprob");
504 for (i = 0; i < *nargs; i++) {
505 snprintf(param, sizeof(param), "arg%d", i);
506 name = gctl_get_asciiparam(req, param);
508 gctl_error(req, "No 'arg%d' argument", i);
511 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
512 name += strlen("/dev/");
513 pp = g_provider_by_name(name);
514 if (pp == NULL || pp->geom->class != mp) {
515 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
516 gctl_error(req, "Provider %s is invalid.", name);
519 sc = pp->geom->softc;
521 sc->sc_error = (int)*error;
522 if (*rfailprob != -1)
523 sc->sc_rfailprob = (u_int)*rfailprob;
524 if (*wfailprob != -1)
525 sc->sc_wfailprob = (u_int)*wfailprob;
529 static struct g_geom *
530 g_nop_find_geom(struct g_class *mp, const char *name)
534 LIST_FOREACH(gp, &mp->geom, geom) {
535 if (strcmp(gp->name, name) == 0)
542 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
544 int *nargs, *force, error, i;
551 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
553 gctl_error(req, "No '%s' argument", "nargs");
557 gctl_error(req, "Missing device(s).");
560 force = gctl_get_paraml(req, "force", sizeof(*force));
562 gctl_error(req, "No 'force' argument");
566 for (i = 0; i < *nargs; i++) {
567 snprintf(param, sizeof(param), "arg%d", i);
568 name = gctl_get_asciiparam(req, param);
570 gctl_error(req, "No 'arg%d' argument", i);
573 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
574 name += strlen("/dev/");
575 gp = g_nop_find_geom(mp, name);
577 G_NOP_DEBUG(1, "Device %s is invalid.", name);
578 gctl_error(req, "Device %s is invalid.", name);
581 error = g_nop_destroy(gp, *force);
583 gctl_error(req, "Cannot destroy device %s (error=%d).",
591 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
593 struct g_nop_softc *sc;
594 struct g_provider *pp;
601 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
603 gctl_error(req, "No '%s' argument", "nargs");
607 gctl_error(req, "Missing device(s).");
611 for (i = 0; i < *nargs; i++) {
612 snprintf(param, sizeof(param), "arg%d", i);
613 name = gctl_get_asciiparam(req, param);
615 gctl_error(req, "No 'arg%d' argument", i);
618 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
619 name += strlen("/dev/");
620 pp = g_provider_by_name(name);
621 if (pp == NULL || pp->geom->class != mp) {
622 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
623 gctl_error(req, "Provider %s is invalid.", name);
626 sc = pp->geom->softc;
635 sc->sc_readbytes = 0;
636 sc->sc_wrotebytes = 0;
641 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
647 version = gctl_get_paraml(req, "version", sizeof(*version));
648 if (version == NULL) {
649 gctl_error(req, "No '%s' argument.", "version");
652 if (*version != G_NOP_VERSION) {
653 gctl_error(req, "Userland and kernel parts are out of sync.");
657 if (strcmp(verb, "create") == 0) {
658 g_nop_ctl_create(req, mp);
660 } else if (strcmp(verb, "configure") == 0) {
661 g_nop_ctl_configure(req, mp);
663 } else if (strcmp(verb, "destroy") == 0) {
664 g_nop_ctl_destroy(req, mp);
666 } else if (strcmp(verb, "reset") == 0) {
667 g_nop_ctl_reset(req, mp);
671 gctl_error(req, "Unknown verb.");
675 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
676 struct g_consumer *cp, struct g_provider *pp)
678 struct g_nop_softc *sc;
680 if (pp != NULL || cp != NULL)
683 sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
684 (intmax_t)sc->sc_offset);
685 sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
687 sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
689 sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
690 sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
691 sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
692 sbuf_printf(sb, "%s<Deletes>%ju</Deletes>\n", indent, sc->sc_deletes);
693 sbuf_printf(sb, "%s<Getattrs>%ju</Getattrs>\n", indent, sc->sc_getattrs);
694 sbuf_printf(sb, "%s<Flushes>%ju</Flushes>\n", indent, sc->sc_flushes);
695 sbuf_printf(sb, "%s<Cmd0s>%ju</Cmd0s>\n", indent, sc->sc_cmd0s);
696 sbuf_printf(sb, "%s<Cmd1s>%ju</Cmd1s>\n", indent, sc->sc_cmd1s);
697 sbuf_printf(sb, "%s<Cmd2s>%ju</Cmd2s>\n", indent, sc->sc_cmd2s);
698 sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
700 sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
704 DECLARE_GEOM_CLASS(g_nop_class, g_nop);