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,
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 snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX);
212 LIST_FOREACH(gp, &mp->geom, geom) {
213 if (strcmp(gp->name, name) == 0) {
214 gctl_error(req, "Provider %s already exists.", name);
218 gp = g_new_geomf(mp, "%s", name);
219 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
220 sc->sc_offset = offset;
221 sc->sc_explicitsize = explicitsize;
222 sc->sc_error = ioerror;
223 sc->sc_rfailprob = rfailprob;
224 sc->sc_wfailprob = wfailprob;
227 sc->sc_readbytes = 0;
228 sc->sc_wrotebytes = 0;
229 mtx_init(&sc->sc_lock, "gnop lock", NULL, MTX_DEF);
231 gp->start = g_nop_start;
232 gp->orphan = g_nop_orphan;
233 gp->resize = g_nop_resize;
234 gp->access = g_nop_access;
235 gp->dumpconf = g_nop_dumpconf;
237 newpp = g_new_providerf(gp, "%s", gp->name);
238 newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
239 newpp->mediasize = size;
240 newpp->sectorsize = secsize;
242 cp = g_new_consumer(gp);
243 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
244 error = g_attach(cp, pp);
246 gctl_error(req, "Cannot attach to provider %s.", pp->name);
250 newpp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
251 g_error_provider(newpp, 0);
252 G_NOP_DEBUG(0, "Device %s created.", gp->name);
255 if (cp->provider != NULL)
257 g_destroy_consumer(cp);
258 g_destroy_provider(newpp);
259 mtx_destroy(&sc->sc_lock);
266 g_nop_destroy(struct g_geom *gp, boolean_t force)
268 struct g_nop_softc *sc;
269 struct g_provider *pp;
275 pp = LIST_FIRST(&gp->provider);
276 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
278 G_NOP_DEBUG(0, "Device %s is still open, so it "
279 "can't be definitely removed.", pp->name);
281 G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
282 pp->name, pp->acr, pp->acw, pp->ace);
286 G_NOP_DEBUG(0, "Device %s removed.", gp->name);
289 mtx_destroy(&sc->sc_lock);
291 g_wither_geom(gp, ENXIO);
297 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
300 return (g_nop_destroy(gp, 0));
304 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
306 struct g_provider *pp;
307 intmax_t *error, *rfailprob, *wfailprob, *offset, *secsize, *size;
314 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
316 gctl_error(req, "No '%s' argument", "nargs");
320 gctl_error(req, "Missing device(s).");
323 error = gctl_get_paraml(req, "error", sizeof(*error));
325 gctl_error(req, "No '%s' argument", "error");
328 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
329 if (rfailprob == NULL) {
330 gctl_error(req, "No '%s' argument", "rfailprob");
333 if (*rfailprob < -1 || *rfailprob > 100) {
334 gctl_error(req, "Invalid '%s' argument", "rfailprob");
337 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
338 if (wfailprob == NULL) {
339 gctl_error(req, "No '%s' argument", "wfailprob");
342 if (*wfailprob < -1 || *wfailprob > 100) {
343 gctl_error(req, "Invalid '%s' argument", "wfailprob");
346 offset = gctl_get_paraml(req, "offset", sizeof(*offset));
347 if (offset == NULL) {
348 gctl_error(req, "No '%s' argument", "offset");
352 gctl_error(req, "Invalid '%s' argument", "offset");
355 size = gctl_get_paraml(req, "size", sizeof(*size));
357 gctl_error(req, "No '%s' argument", "size");
361 gctl_error(req, "Invalid '%s' argument", "size");
364 secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
365 if (secsize == NULL) {
366 gctl_error(req, "No '%s' argument", "secsize");
370 gctl_error(req, "Invalid '%s' argument", "secsize");
374 for (i = 0; i < *nargs; i++) {
375 snprintf(param, sizeof(param), "arg%d", i);
376 name = gctl_get_asciiparam(req, param);
378 gctl_error(req, "No 'arg%d' argument", i);
381 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
382 name += strlen("/dev/");
383 pp = g_provider_by_name(name);
385 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
386 gctl_error(req, "Provider %s is invalid.", name);
389 if (g_nop_create(req, mp, pp,
390 *error == -1 ? EIO : (int)*error,
391 *rfailprob == -1 ? 0 : (u_int)*rfailprob,
392 *wfailprob == -1 ? 0 : (u_int)*wfailprob,
393 (off_t)*offset, (off_t)*size, (u_int)*secsize) != 0) {
400 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
402 struct g_nop_softc *sc;
403 struct g_provider *pp;
404 intmax_t *error, *rfailprob, *wfailprob;
411 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
413 gctl_error(req, "No '%s' argument", "nargs");
417 gctl_error(req, "Missing device(s).");
420 error = gctl_get_paraml(req, "error", sizeof(*error));
422 gctl_error(req, "No '%s' argument", "error");
425 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
426 if (rfailprob == NULL) {
427 gctl_error(req, "No '%s' argument", "rfailprob");
430 if (*rfailprob < -1 || *rfailprob > 100) {
431 gctl_error(req, "Invalid '%s' argument", "rfailprob");
434 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
435 if (wfailprob == NULL) {
436 gctl_error(req, "No '%s' argument", "wfailprob");
439 if (*wfailprob < -1 || *wfailprob > 100) {
440 gctl_error(req, "Invalid '%s' argument", "wfailprob");
444 for (i = 0; i < *nargs; i++) {
445 snprintf(param, sizeof(param), "arg%d", i);
446 name = gctl_get_asciiparam(req, param);
448 gctl_error(req, "No 'arg%d' argument", i);
451 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
452 name += strlen("/dev/");
453 pp = g_provider_by_name(name);
454 if (pp == NULL || pp->geom->class != mp) {
455 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
456 gctl_error(req, "Provider %s is invalid.", name);
459 sc = pp->geom->softc;
461 sc->sc_error = (int)*error;
462 if (*rfailprob != -1)
463 sc->sc_rfailprob = (u_int)*rfailprob;
464 if (*wfailprob != -1)
465 sc->sc_wfailprob = (u_int)*wfailprob;
469 static struct g_geom *
470 g_nop_find_geom(struct g_class *mp, const char *name)
474 LIST_FOREACH(gp, &mp->geom, geom) {
475 if (strcmp(gp->name, name) == 0)
482 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
484 int *nargs, *force, error, i;
491 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
493 gctl_error(req, "No '%s' argument", "nargs");
497 gctl_error(req, "Missing device(s).");
500 force = gctl_get_paraml(req, "force", sizeof(*force));
502 gctl_error(req, "No 'force' argument");
506 for (i = 0; i < *nargs; i++) {
507 snprintf(param, sizeof(param), "arg%d", i);
508 name = gctl_get_asciiparam(req, param);
510 gctl_error(req, "No 'arg%d' argument", i);
513 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
514 name += strlen("/dev/");
515 gp = g_nop_find_geom(mp, name);
517 G_NOP_DEBUG(1, "Device %s is invalid.", name);
518 gctl_error(req, "Device %s is invalid.", name);
521 error = g_nop_destroy(gp, *force);
523 gctl_error(req, "Cannot destroy device %s (error=%d).",
531 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
533 struct g_nop_softc *sc;
534 struct g_provider *pp;
541 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
543 gctl_error(req, "No '%s' argument", "nargs");
547 gctl_error(req, "Missing device(s).");
551 for (i = 0; i < *nargs; i++) {
552 snprintf(param, sizeof(param), "arg%d", i);
553 name = gctl_get_asciiparam(req, param);
555 gctl_error(req, "No 'arg%d' argument", i);
558 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
559 name += strlen("/dev/");
560 pp = g_provider_by_name(name);
561 if (pp == NULL || pp->geom->class != mp) {
562 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
563 gctl_error(req, "Provider %s is invalid.", name);
566 sc = pp->geom->softc;
569 sc->sc_readbytes = 0;
570 sc->sc_wrotebytes = 0;
575 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
581 version = gctl_get_paraml(req, "version", sizeof(*version));
582 if (version == NULL) {
583 gctl_error(req, "No '%s' argument.", "version");
586 if (*version != G_NOP_VERSION) {
587 gctl_error(req, "Userland and kernel parts are out of sync.");
591 if (strcmp(verb, "create") == 0) {
592 g_nop_ctl_create(req, mp);
594 } else if (strcmp(verb, "configure") == 0) {
595 g_nop_ctl_configure(req, mp);
597 } else if (strcmp(verb, "destroy") == 0) {
598 g_nop_ctl_destroy(req, mp);
600 } else if (strcmp(verb, "reset") == 0) {
601 g_nop_ctl_reset(req, mp);
605 gctl_error(req, "Unknown verb.");
609 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
610 struct g_consumer *cp, struct g_provider *pp)
612 struct g_nop_softc *sc;
614 if (pp != NULL || cp != NULL)
617 sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
618 (intmax_t)sc->sc_offset);
619 sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
621 sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
623 sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
624 sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
625 sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
626 sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
628 sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
632 DECLARE_GEOM_CLASS(g_nop_class, g_nop);