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;
123 mtx_unlock(&sc->sc_lock);
127 rval = arc4random() % 100;
128 if (rval < failprob) {
129 G_NOP_LOGREQLVL(1, bp, "Returning error=%d.", sc->sc_error);
130 g_io_deliver(bp, sc->sc_error);
134 cbp = g_clone_bio(bp);
136 g_io_deliver(bp, ENOMEM);
139 cbp->bio_done = g_std_done;
140 cbp->bio_offset = bp->bio_offset + sc->sc_offset;
141 pp = LIST_FIRST(&gp->provider);
142 KASSERT(pp != NULL, ("NULL pp"));
144 G_NOP_LOGREQ(cbp, "Sending request.");
145 g_io_request(cbp, LIST_FIRST(&gp->consumer));
149 g_nop_access(struct g_provider *pp, int dr, int dw, int de)
152 struct g_consumer *cp;
156 cp = LIST_FIRST(&gp->consumer);
157 error = g_access(cp, dr, dw, de);
163 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
164 int ioerror, u_int rfailprob, u_int wfailprob, off_t offset, off_t size,
165 u_int secsize, u_int stripesize, u_int stripeoffset)
167 struct g_nop_softc *sc;
169 struct g_provider *newpp;
170 struct g_consumer *cp;
181 if ((offset % pp->sectorsize) != 0) {
182 gctl_error(req, "Invalid offset for provider %s.", pp->name);
185 if ((size % pp->sectorsize) != 0) {
186 gctl_error(req, "Invalid size for provider %s.", pp->name);
189 if (offset >= pp->mediasize) {
190 gctl_error(req, "Invalid offset for provider %s.", pp->name);
195 size = pp->mediasize - offset;
196 if (offset + size > pp->mediasize) {
197 gctl_error(req, "Invalid size for provider %s.", pp->name);
201 secsize = pp->sectorsize;
202 else if ((secsize % pp->sectorsize) != 0) {
203 gctl_error(req, "Invalid secsize for provider %s.", pp->name);
206 if (secsize > MAXPHYS) {
207 gctl_error(req, "secsize is too big.");
210 size -= size % secsize;
211 if ((stripesize % pp->sectorsize) != 0) {
212 gctl_error(req, "Invalid stripesize for provider %s.", pp->name);
215 if ((stripeoffset % pp->sectorsize) != 0) {
216 gctl_error(req, "Invalid stripeoffset for provider %s.", pp->name);
219 if (stripesize != 0 && stripeoffset >= stripesize) {
220 gctl_error(req, "stripeoffset is too big.");
223 snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX);
224 LIST_FOREACH(gp, &mp->geom, geom) {
225 if (strcmp(gp->name, name) == 0) {
226 gctl_error(req, "Provider %s already exists.", name);
230 gp = g_new_geomf(mp, "%s", name);
231 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
232 sc->sc_offset = offset;
233 sc->sc_explicitsize = explicitsize;
234 sc->sc_stripesize = stripesize;
235 sc->sc_stripeoffset = stripeoffset;
236 sc->sc_error = ioerror;
237 sc->sc_rfailprob = rfailprob;
238 sc->sc_wfailprob = wfailprob;
241 sc->sc_readbytes = 0;
242 sc->sc_wrotebytes = 0;
243 mtx_init(&sc->sc_lock, "gnop lock", NULL, MTX_DEF);
245 gp->start = g_nop_start;
246 gp->orphan = g_nop_orphan;
247 gp->resize = g_nop_resize;
248 gp->access = g_nop_access;
249 gp->dumpconf = g_nop_dumpconf;
251 newpp = g_new_providerf(gp, "%s", gp->name);
252 newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
253 newpp->mediasize = size;
254 newpp->sectorsize = secsize;
255 newpp->stripesize = stripesize;
256 newpp->stripeoffset = stripeoffset;
258 cp = g_new_consumer(gp);
259 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
260 error = g_attach(cp, pp);
262 gctl_error(req, "Cannot attach to provider %s.", pp->name);
266 newpp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
267 g_error_provider(newpp, 0);
268 G_NOP_DEBUG(0, "Device %s created.", gp->name);
271 if (cp->provider != NULL)
273 g_destroy_consumer(cp);
274 g_destroy_provider(newpp);
275 mtx_destroy(&sc->sc_lock);
282 g_nop_destroy(struct g_geom *gp, boolean_t force)
284 struct g_nop_softc *sc;
285 struct g_provider *pp;
291 pp = LIST_FIRST(&gp->provider);
292 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
294 G_NOP_DEBUG(0, "Device %s is still open, so it "
295 "can't be definitely removed.", pp->name);
297 G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
298 pp->name, pp->acr, pp->acw, pp->ace);
302 G_NOP_DEBUG(0, "Device %s removed.", gp->name);
305 mtx_destroy(&sc->sc_lock);
307 g_wither_geom(gp, ENXIO);
313 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
316 return (g_nop_destroy(gp, 0));
320 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
322 struct g_provider *pp;
323 intmax_t *error, *rfailprob, *wfailprob, *offset, *secsize, *size,
324 *stripesize, *stripeoffset;
331 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
333 gctl_error(req, "No '%s' argument", "nargs");
337 gctl_error(req, "Missing device(s).");
340 error = gctl_get_paraml(req, "error", sizeof(*error));
342 gctl_error(req, "No '%s' argument", "error");
345 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
346 if (rfailprob == NULL) {
347 gctl_error(req, "No '%s' argument", "rfailprob");
350 if (*rfailprob < -1 || *rfailprob > 100) {
351 gctl_error(req, "Invalid '%s' argument", "rfailprob");
354 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
355 if (wfailprob == NULL) {
356 gctl_error(req, "No '%s' argument", "wfailprob");
359 if (*wfailprob < -1 || *wfailprob > 100) {
360 gctl_error(req, "Invalid '%s' argument", "wfailprob");
363 offset = gctl_get_paraml(req, "offset", sizeof(*offset));
364 if (offset == NULL) {
365 gctl_error(req, "No '%s' argument", "offset");
369 gctl_error(req, "Invalid '%s' argument", "offset");
372 size = gctl_get_paraml(req, "size", sizeof(*size));
374 gctl_error(req, "No '%s' argument", "size");
378 gctl_error(req, "Invalid '%s' argument", "size");
381 secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
382 if (secsize == NULL) {
383 gctl_error(req, "No '%s' argument", "secsize");
387 gctl_error(req, "Invalid '%s' argument", "secsize");
390 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
391 if (stripesize == NULL) {
392 gctl_error(req, "No '%s' argument", "stripesize");
395 if (*stripesize < 0) {
396 gctl_error(req, "Invalid '%s' argument", "stripesize");
399 stripeoffset = gctl_get_paraml(req, "stripeoffset", sizeof(*stripeoffset));
400 if (stripeoffset == NULL) {
401 gctl_error(req, "No '%s' argument", "stripeoffset");
404 if (*stripeoffset < 0) {
405 gctl_error(req, "Invalid '%s' argument", "stripeoffset");
409 for (i = 0; i < *nargs; i++) {
410 snprintf(param, sizeof(param), "arg%d", i);
411 name = gctl_get_asciiparam(req, param);
413 gctl_error(req, "No 'arg%d' argument", i);
416 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
417 name += strlen("/dev/");
418 pp = g_provider_by_name(name);
420 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
421 gctl_error(req, "Provider %s is invalid.", name);
424 if (g_nop_create(req, mp, pp,
425 *error == -1 ? EIO : (int)*error,
426 *rfailprob == -1 ? 0 : (u_int)*rfailprob,
427 *wfailprob == -1 ? 0 : (u_int)*wfailprob,
428 (off_t)*offset, (off_t)*size, (u_int)*secsize,
429 (u_int)*stripesize, (u_int)*stripeoffset) != 0) {
436 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
438 struct g_nop_softc *sc;
439 struct g_provider *pp;
440 intmax_t *error, *rfailprob, *wfailprob;
447 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
449 gctl_error(req, "No '%s' argument", "nargs");
453 gctl_error(req, "Missing device(s).");
456 error = gctl_get_paraml(req, "error", sizeof(*error));
458 gctl_error(req, "No '%s' argument", "error");
461 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
462 if (rfailprob == NULL) {
463 gctl_error(req, "No '%s' argument", "rfailprob");
466 if (*rfailprob < -1 || *rfailprob > 100) {
467 gctl_error(req, "Invalid '%s' argument", "rfailprob");
470 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
471 if (wfailprob == NULL) {
472 gctl_error(req, "No '%s' argument", "wfailprob");
475 if (*wfailprob < -1 || *wfailprob > 100) {
476 gctl_error(req, "Invalid '%s' argument", "wfailprob");
480 for (i = 0; i < *nargs; i++) {
481 snprintf(param, sizeof(param), "arg%d", i);
482 name = gctl_get_asciiparam(req, param);
484 gctl_error(req, "No 'arg%d' argument", i);
487 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
488 name += strlen("/dev/");
489 pp = g_provider_by_name(name);
490 if (pp == NULL || pp->geom->class != mp) {
491 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
492 gctl_error(req, "Provider %s is invalid.", name);
495 sc = pp->geom->softc;
497 sc->sc_error = (int)*error;
498 if (*rfailprob != -1)
499 sc->sc_rfailprob = (u_int)*rfailprob;
500 if (*wfailprob != -1)
501 sc->sc_wfailprob = (u_int)*wfailprob;
505 static struct g_geom *
506 g_nop_find_geom(struct g_class *mp, const char *name)
510 LIST_FOREACH(gp, &mp->geom, geom) {
511 if (strcmp(gp->name, name) == 0)
518 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
520 int *nargs, *force, error, i;
527 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
529 gctl_error(req, "No '%s' argument", "nargs");
533 gctl_error(req, "Missing device(s).");
536 force = gctl_get_paraml(req, "force", sizeof(*force));
538 gctl_error(req, "No 'force' argument");
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 gp = g_nop_find_geom(mp, name);
553 G_NOP_DEBUG(1, "Device %s is invalid.", name);
554 gctl_error(req, "Device %s is invalid.", name);
557 error = g_nop_destroy(gp, *force);
559 gctl_error(req, "Cannot destroy device %s (error=%d).",
567 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
569 struct g_nop_softc *sc;
570 struct g_provider *pp;
577 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
579 gctl_error(req, "No '%s' argument", "nargs");
583 gctl_error(req, "Missing device(s).");
587 for (i = 0; i < *nargs; i++) {
588 snprintf(param, sizeof(param), "arg%d", i);
589 name = gctl_get_asciiparam(req, param);
591 gctl_error(req, "No 'arg%d' argument", i);
594 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
595 name += strlen("/dev/");
596 pp = g_provider_by_name(name);
597 if (pp == NULL || pp->geom->class != mp) {
598 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
599 gctl_error(req, "Provider %s is invalid.", name);
602 sc = pp->geom->softc;
605 sc->sc_readbytes = 0;
606 sc->sc_wrotebytes = 0;
611 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
617 version = gctl_get_paraml(req, "version", sizeof(*version));
618 if (version == NULL) {
619 gctl_error(req, "No '%s' argument.", "version");
622 if (*version != G_NOP_VERSION) {
623 gctl_error(req, "Userland and kernel parts are out of sync.");
627 if (strcmp(verb, "create") == 0) {
628 g_nop_ctl_create(req, mp);
630 } else if (strcmp(verb, "configure") == 0) {
631 g_nop_ctl_configure(req, mp);
633 } else if (strcmp(verb, "destroy") == 0) {
634 g_nop_ctl_destroy(req, mp);
636 } else if (strcmp(verb, "reset") == 0) {
637 g_nop_ctl_reset(req, mp);
641 gctl_error(req, "Unknown verb.");
645 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
646 struct g_consumer *cp, struct g_provider *pp)
648 struct g_nop_softc *sc;
650 if (pp != NULL || cp != NULL)
653 sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
654 (intmax_t)sc->sc_offset);
655 sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
657 sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
659 sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
660 sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
661 sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
662 sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
664 sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
668 DECLARE_GEOM_CLASS(g_nop_class, g_nop);