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 switch (bp->bio_cmd) {
113 sc->sc_readbytes += bp->bio_length;
114 failprob = sc->sc_rfailprob;
118 sc->sc_wrotebytes += bp->bio_length;
119 failprob = sc->sc_wfailprob;
125 rval = arc4random() % 100;
126 if (rval < failprob) {
127 G_NOP_LOGREQLVL(1, bp, "Returning error=%d.", sc->sc_error);
128 g_io_deliver(bp, sc->sc_error);
132 cbp = g_clone_bio(bp);
134 g_io_deliver(bp, ENOMEM);
137 cbp->bio_done = g_std_done;
138 cbp->bio_offset = bp->bio_offset + sc->sc_offset;
139 pp = LIST_FIRST(&gp->provider);
140 KASSERT(pp != NULL, ("NULL pp"));
142 G_NOP_LOGREQ(cbp, "Sending request.");
143 g_io_request(cbp, LIST_FIRST(&gp->consumer));
147 g_nop_access(struct g_provider *pp, int dr, int dw, int de)
150 struct g_consumer *cp;
154 cp = LIST_FIRST(&gp->consumer);
155 error = g_access(cp, dr, dw, de);
161 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
162 int ioerror, u_int rfailprob, u_int wfailprob, off_t offset, off_t size,
165 struct g_nop_softc *sc;
167 struct g_provider *newpp;
168 struct g_consumer *cp;
179 if ((offset % pp->sectorsize) != 0) {
180 gctl_error(req, "Invalid offset for provider %s.", pp->name);
183 if ((size % pp->sectorsize) != 0) {
184 gctl_error(req, "Invalid size for provider %s.", pp->name);
187 if (offset >= pp->mediasize) {
188 gctl_error(req, "Invalid offset for provider %s.", pp->name);
193 size = pp->mediasize - offset;
194 if (offset + size > pp->mediasize) {
195 gctl_error(req, "Invalid size for provider %s.", pp->name);
199 secsize = pp->sectorsize;
200 else if ((secsize % pp->sectorsize) != 0) {
201 gctl_error(req, "Invalid secsize for provider %s.", pp->name);
204 if (secsize > MAXPHYS) {
205 gctl_error(req, "secsize is too big.");
208 size -= size % secsize;
209 snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX);
210 LIST_FOREACH(gp, &mp->geom, geom) {
211 if (strcmp(gp->name, name) == 0) {
212 gctl_error(req, "Provider %s already exists.", name);
216 gp = g_new_geomf(mp, "%s", name);
217 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
218 sc->sc_offset = offset;
219 sc->sc_explicitsize = explicitsize;
220 sc->sc_error = ioerror;
221 sc->sc_rfailprob = rfailprob;
222 sc->sc_wfailprob = wfailprob;
225 sc->sc_readbytes = 0;
226 sc->sc_wrotebytes = 0;
228 gp->start = g_nop_start;
229 gp->orphan = g_nop_orphan;
230 gp->resize = g_nop_resize;
231 gp->access = g_nop_access;
232 gp->dumpconf = g_nop_dumpconf;
234 newpp = g_new_providerf(gp, "%s", gp->name);
235 newpp->mediasize = size;
236 newpp->sectorsize = secsize;
238 cp = g_new_consumer(gp);
239 error = g_attach(cp, pp);
241 gctl_error(req, "Cannot attach to provider %s.", pp->name);
245 newpp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
246 g_error_provider(newpp, 0);
247 G_NOP_DEBUG(0, "Device %s created.", gp->name);
250 if (cp->provider != NULL)
252 g_destroy_consumer(cp);
253 g_destroy_provider(newpp);
260 g_nop_destroy(struct g_geom *gp, boolean_t force)
262 struct g_provider *pp;
265 if (gp->softc == NULL)
267 pp = LIST_FIRST(&gp->provider);
268 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
270 G_NOP_DEBUG(0, "Device %s is still open, so it "
271 "can't be definitely removed.", pp->name);
273 G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
274 pp->name, pp->acr, pp->acw, pp->ace);
278 G_NOP_DEBUG(0, "Device %s removed.", gp->name);
282 g_wither_geom(gp, ENXIO);
288 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
291 return (g_nop_destroy(gp, 0));
295 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
297 struct g_provider *pp;
298 intmax_t *error, *rfailprob, *wfailprob, *offset, *secsize, *size;
305 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
307 gctl_error(req, "No '%s' argument", "nargs");
311 gctl_error(req, "Missing device(s).");
314 error = gctl_get_paraml(req, "error", sizeof(*error));
316 gctl_error(req, "No '%s' argument", "error");
319 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
320 if (rfailprob == NULL) {
321 gctl_error(req, "No '%s' argument", "rfailprob");
324 if (*rfailprob < -1 || *rfailprob > 100) {
325 gctl_error(req, "Invalid '%s' argument", "rfailprob");
328 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
329 if (wfailprob == NULL) {
330 gctl_error(req, "No '%s' argument", "wfailprob");
333 if (*wfailprob < -1 || *wfailprob > 100) {
334 gctl_error(req, "Invalid '%s' argument", "wfailprob");
337 offset = gctl_get_paraml(req, "offset", sizeof(*offset));
338 if (offset == NULL) {
339 gctl_error(req, "No '%s' argument", "offset");
343 gctl_error(req, "Invalid '%s' argument", "offset");
346 size = gctl_get_paraml(req, "size", sizeof(*size));
348 gctl_error(req, "No '%s' argument", "size");
352 gctl_error(req, "Invalid '%s' argument", "size");
355 secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
356 if (secsize == NULL) {
357 gctl_error(req, "No '%s' argument", "secsize");
361 gctl_error(req, "Invalid '%s' argument", "secsize");
365 for (i = 0; i < *nargs; i++) {
366 snprintf(param, sizeof(param), "arg%d", i);
367 name = gctl_get_asciiparam(req, param);
369 gctl_error(req, "No 'arg%d' argument", i);
372 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
373 name += strlen("/dev/");
374 pp = g_provider_by_name(name);
376 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
377 gctl_error(req, "Provider %s is invalid.", name);
380 if (g_nop_create(req, mp, pp,
381 *error == -1 ? EIO : (int)*error,
382 *rfailprob == -1 ? 0 : (u_int)*rfailprob,
383 *wfailprob == -1 ? 0 : (u_int)*wfailprob,
384 (off_t)*offset, (off_t)*size, (u_int)*secsize) != 0) {
391 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
393 struct g_nop_softc *sc;
394 struct g_provider *pp;
395 intmax_t *error, *rfailprob, *wfailprob;
402 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
404 gctl_error(req, "No '%s' argument", "nargs");
408 gctl_error(req, "Missing device(s).");
411 error = gctl_get_paraml(req, "error", sizeof(*error));
413 gctl_error(req, "No '%s' argument", "error");
416 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
417 if (rfailprob == NULL) {
418 gctl_error(req, "No '%s' argument", "rfailprob");
421 if (*rfailprob < -1 || *rfailprob > 100) {
422 gctl_error(req, "Invalid '%s' argument", "rfailprob");
425 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
426 if (wfailprob == NULL) {
427 gctl_error(req, "No '%s' argument", "wfailprob");
430 if (*wfailprob < -1 || *wfailprob > 100) {
431 gctl_error(req, "Invalid '%s' argument", "wfailprob");
435 for (i = 0; i < *nargs; i++) {
436 snprintf(param, sizeof(param), "arg%d", i);
437 name = gctl_get_asciiparam(req, param);
439 gctl_error(req, "No 'arg%d' argument", i);
442 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
443 name += strlen("/dev/");
444 pp = g_provider_by_name(name);
445 if (pp == NULL || pp->geom->class != mp) {
446 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
447 gctl_error(req, "Provider %s is invalid.", name);
450 sc = pp->geom->softc;
452 sc->sc_error = (int)*error;
453 if (*rfailprob != -1)
454 sc->sc_rfailprob = (u_int)*rfailprob;
455 if (*wfailprob != -1)
456 sc->sc_wfailprob = (u_int)*wfailprob;
460 static struct g_geom *
461 g_nop_find_geom(struct g_class *mp, const char *name)
465 LIST_FOREACH(gp, &mp->geom, geom) {
466 if (strcmp(gp->name, name) == 0)
473 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
475 int *nargs, *force, error, i;
482 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
484 gctl_error(req, "No '%s' argument", "nargs");
488 gctl_error(req, "Missing device(s).");
491 force = gctl_get_paraml(req, "force", sizeof(*force));
493 gctl_error(req, "No 'force' argument");
497 for (i = 0; i < *nargs; i++) {
498 snprintf(param, sizeof(param), "arg%d", i);
499 name = gctl_get_asciiparam(req, param);
501 gctl_error(req, "No 'arg%d' argument", i);
504 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
505 name += strlen("/dev/");
506 gp = g_nop_find_geom(mp, name);
508 G_NOP_DEBUG(1, "Device %s is invalid.", name);
509 gctl_error(req, "Device %s is invalid.", name);
512 error = g_nop_destroy(gp, *force);
514 gctl_error(req, "Cannot destroy device %s (error=%d).",
522 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
524 struct g_nop_softc *sc;
525 struct g_provider *pp;
532 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
534 gctl_error(req, "No '%s' argument", "nargs");
538 gctl_error(req, "Missing device(s).");
542 for (i = 0; i < *nargs; i++) {
543 snprintf(param, sizeof(param), "arg%d", i);
544 name = gctl_get_asciiparam(req, param);
546 gctl_error(req, "No 'arg%d' argument", i);
549 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
550 name += strlen("/dev/");
551 pp = g_provider_by_name(name);
552 if (pp == NULL || pp->geom->class != mp) {
553 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
554 gctl_error(req, "Provider %s is invalid.", name);
557 sc = pp->geom->softc;
560 sc->sc_readbytes = 0;
561 sc->sc_wrotebytes = 0;
566 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
572 version = gctl_get_paraml(req, "version", sizeof(*version));
573 if (version == NULL) {
574 gctl_error(req, "No '%s' argument.", "version");
577 if (*version != G_NOP_VERSION) {
578 gctl_error(req, "Userland and kernel parts are out of sync.");
582 if (strcmp(verb, "create") == 0) {
583 g_nop_ctl_create(req, mp);
585 } else if (strcmp(verb, "configure") == 0) {
586 g_nop_ctl_configure(req, mp);
588 } else if (strcmp(verb, "destroy") == 0) {
589 g_nop_ctl_destroy(req, mp);
591 } else if (strcmp(verb, "reset") == 0) {
592 g_nop_ctl_reset(req, mp);
596 gctl_error(req, "Unknown verb.");
600 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
601 struct g_consumer *cp, struct g_provider *pp)
603 struct g_nop_softc *sc;
605 if (pp != NULL || cp != NULL)
608 sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
609 (intmax_t)sc->sc_offset);
610 sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
612 sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
614 sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
615 sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
616 sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
617 sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
619 sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
623 DECLARE_GEOM_CLASS(g_nop_class, g_nop);