2 * Copyright (c) 2004 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>
37 #include <sys/sysctl.h>
38 #include <sys/malloc.h>
39 #include <geom/geom.h>
40 #include <geom/nop/g_nop.h>
43 SYSCTL_DECL(_kern_geom);
44 SYSCTL_NODE(_kern_geom, OID_AUTO, nop, CTLFLAG_RW, 0, "GEOM_NOP stuff");
45 static u_int g_nop_debug = 0;
46 SYSCTL_UINT(_kern_geom_nop, OID_AUTO, debug, CTLFLAG_RW, &g_nop_debug, 0,
49 static int g_nop_destroy(struct g_geom *gp, boolean_t force);
50 static int g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp,
52 static void g_nop_config(struct gctl_req *req, struct g_class *mp,
54 static void g_nop_dumpconf(struct sbuf *sb, const char *indent,
55 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
57 struct g_class g_nop_class = {
58 .name = G_NOP_CLASS_NAME,
60 .ctlreq = g_nop_config,
61 .destroy_geom = g_nop_destroy_geom
66 g_nop_orphan(struct g_consumer *cp)
70 g_nop_destroy(cp->geom, 1);
74 g_nop_start(struct bio *bp)
76 struct g_nop_softc *sc;
78 struct g_provider *pp;
81 gp = bp->bio_to->geom;
83 G_NOP_LOGREQ(bp, "Request received.");
84 switch (bp->bio_cmd) {
87 sc->sc_readbytes += bp->bio_length;
91 sc->sc_wrotebytes += bp->bio_length;
94 if (sc->sc_failprob > 0) {
97 rval = arc4random() % 100;
98 if (rval < sc->sc_failprob) {
99 G_NOP_LOGREQ(bp, "Returning EIO.");
100 g_io_deliver(bp, EIO);
104 cbp = g_clone_bio(bp);
106 g_io_deliver(bp, ENOMEM);
109 cbp->bio_done = g_std_done;
110 cbp->bio_offset = bp->bio_offset + sc->sc_offset;
111 cbp->bio_data = bp->bio_data;
112 cbp->bio_length = bp->bio_length;
113 pp = LIST_FIRST(&gp->provider);
114 KASSERT(pp != NULL, ("NULL pp"));
116 G_NOP_LOGREQ(cbp, "Sending request.");
117 g_io_request(cbp, LIST_FIRST(&gp->consumer));
121 g_nop_access(struct g_provider *pp, int dr, int dw, int de)
124 struct g_consumer *cp;
128 cp = LIST_FIRST(&gp->consumer);
129 error = g_access(cp, dr, dw, de);
135 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
136 u_int failprob, off_t offset, off_t size, u_int secsize)
138 struct g_nop_softc *sc;
140 struct g_provider *newpp;
141 struct g_consumer *cp;
151 if ((offset % pp->sectorsize) != 0) {
152 gctl_error(req, "Invalid offset for provider %s.", pp->name);
155 if ((size % pp->sectorsize) != 0) {
156 gctl_error(req, "Invalid size for provider %s.", pp->name);
159 if (offset >= pp->mediasize) {
160 gctl_error(req, "Invalid offset for provider %s.", pp->name);
164 size = pp->mediasize - offset;
165 if (offset + size > pp->mediasize) {
166 gctl_error(req, "Invalid size for provider %s.", pp->name);
170 secsize = pp->sectorsize;
171 else if ((secsize % pp->sectorsize) != 0) {
172 gctl_error(req, "Invalid secsize for provider %s.", pp->name);
175 snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX);
176 LIST_FOREACH(gp, &mp->geom, geom) {
177 if (strcmp(gp->name, name) == 0) {
178 gctl_error(req, "Provider %s already exists.", name);
182 gp = g_new_geomf(mp, name);
184 gctl_error(req, "Cannot create geom %s.", name);
187 sc = g_malloc(sizeof(*sc), M_WAITOK);
188 sc->sc_offset = offset;
189 sc->sc_failprob = failprob;
192 sc->sc_readbytes = 0;
193 sc->sc_wrotebytes = 0;
195 gp->start = g_nop_start;
196 gp->orphan = g_nop_orphan;
197 gp->access = g_nop_access;
198 gp->dumpconf = g_nop_dumpconf;
200 newpp = g_new_providerf(gp, gp->name);
202 gctl_error(req, "Cannot create provider %s.", name);
206 newpp->mediasize = size;
207 newpp->sectorsize = secsize;
209 cp = g_new_consumer(gp);
211 gctl_error(req, "Cannot create consumer for %s.", gp->name);
215 error = g_attach(cp, pp);
217 gctl_error(req, "Cannot attach to provider %s.", pp->name);
221 g_error_provider(newpp, 0);
222 G_NOP_DEBUG(0, "Device %s created.", gp->name);
226 if (cp->provider != NULL)
228 g_destroy_consumer(cp);
231 g_destroy_provider(newpp);
233 if (gp->softc != NULL)
241 g_nop_destroy(struct g_geom *gp, boolean_t force)
243 struct g_provider *pp;
246 if (gp->softc == NULL)
248 pp = LIST_FIRST(&gp->provider);
249 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
251 G_NOP_DEBUG(0, "Device %s is still open, so it "
252 "can't be definitely removed.", pp->name);
254 G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
255 pp->name, pp->acr, pp->acw, pp->ace);
259 G_NOP_DEBUG(0, "Device %s removed.", gp->name);
263 g_wither_geom(gp, ENXIO);
269 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
272 return (g_nop_destroy(gp, 0));
276 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
278 struct g_provider *pp;
279 intmax_t *failprob, *offset, *secsize, *size;
286 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
288 gctl_error(req, "No '%s' argument", "nargs");
292 gctl_error(req, "Missing device(s).");
295 failprob = gctl_get_paraml(req, "failprob", sizeof(*failprob));
296 if (failprob == NULL) {
297 gctl_error(req, "No '%s' argument", "failprob");
300 if (*failprob < 0 || *failprob > 100) {
301 gctl_error(req, "Invalid '%s' argument", "failprob");
304 offset = gctl_get_paraml(req, "offset", sizeof(*offset));
305 if (offset == NULL) {
306 gctl_error(req, "No '%s' argument", "offset");
310 gctl_error(req, "Invalid '%s' argument", "offset");
313 size = gctl_get_paraml(req, "size", sizeof(*size));
315 gctl_error(req, "No '%s' argument", "size");
319 gctl_error(req, "Invalid '%s' argument", "size");
322 secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
323 if (secsize == NULL) {
324 gctl_error(req, "No '%s' argument", "secsize");
328 gctl_error(req, "Invalid '%s' argument", "secsize");
332 for (i = 0; i < *nargs; i++) {
333 snprintf(param, sizeof(param), "arg%d", i);
334 name = gctl_get_asciiparam(req, param);
336 gctl_error(req, "No 'arg%d' argument", i);
339 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
340 name += strlen("/dev/");
341 pp = g_provider_by_name(name);
343 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
344 gctl_error(req, "Provider %s is invalid.", name);
347 if (g_nop_create(req, mp, pp, (u_int)*failprob, (off_t)*offset,
348 (off_t)*size, (u_int)*secsize) != 0) {
355 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
357 struct g_nop_softc *sc;
358 struct g_provider *pp;
366 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
368 gctl_error(req, "No '%s' argument", "nargs");
372 gctl_error(req, "Missing device(s).");
375 failprob = gctl_get_paraml(req, "failprob", sizeof(*failprob));
376 if (failprob == NULL) {
377 gctl_error(req, "No '%s' argument", "failprob");
380 if (*failprob < 0 || *failprob > 100) {
381 gctl_error(req, "Invalid '%s' argument", "failprob");
385 for (i = 0; i < *nargs; i++) {
386 snprintf(param, sizeof(param), "arg%d", i);
387 name = gctl_get_asciiparam(req, param);
389 gctl_error(req, "No 'arg%d' argument", i);
392 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
393 name += strlen("/dev/");
394 pp = g_provider_by_name(name);
395 if (pp == NULL || pp->geom->class != mp) {
396 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
397 gctl_error(req, "Provider %s is invalid.", name);
400 sc = pp->geom->softc;
401 sc->sc_failprob = (u_int)*failprob;
405 static struct g_geom *
406 g_nop_find_geom(struct g_class *mp, const char *name)
410 LIST_FOREACH(gp, &mp->geom, geom) {
411 if (strcmp(gp->name, name) == 0)
418 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
420 int *nargs, *force, error, i;
427 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
429 gctl_error(req, "No '%s' argument", "nargs");
433 gctl_error(req, "Missing device(s).");
436 force = gctl_get_paraml(req, "force", sizeof(*force));
438 gctl_error(req, "No 'force' argument");
442 for (i = 0; i < *nargs; i++) {
443 snprintf(param, sizeof(param), "arg%d", i);
444 name = gctl_get_asciiparam(req, param);
446 gctl_error(req, "No 'arg%d' argument", i);
449 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
450 name += strlen("/dev/");
451 gp = g_nop_find_geom(mp, name);
453 G_NOP_DEBUG(1, "Device %s is invalid.", name);
454 gctl_error(req, "Device %s is invalid.", name);
457 error = g_nop_destroy(gp, *force);
459 gctl_error(req, "Cannot destroy device %s (error=%d).",
467 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
469 struct g_nop_softc *sc;
470 struct g_provider *pp;
477 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
479 gctl_error(req, "No '%s' argument", "nargs");
483 gctl_error(req, "Missing device(s).");
487 for (i = 0; i < *nargs; i++) {
488 snprintf(param, sizeof(param), "arg%d", i);
489 name = gctl_get_asciiparam(req, param);
491 gctl_error(req, "No 'arg%d' argument", i);
494 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
495 name += strlen("/dev/");
496 pp = g_provider_by_name(name);
497 if (pp == NULL || pp->geom->class != mp) {
498 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
499 gctl_error(req, "Provider %s is invalid.", name);
502 sc = pp->geom->softc;
505 sc->sc_readbytes = 0;
506 sc->sc_wrotebytes = 0;
511 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
517 version = gctl_get_paraml(req, "version", sizeof(*version));
518 if (version == NULL) {
519 gctl_error(req, "No '%s' argument.", "version");
522 if (*version != G_NOP_VERSION) {
523 gctl_error(req, "Userland and kernel parts are out of sync.");
527 if (strcmp(verb, "create") == 0) {
528 g_nop_ctl_create(req, mp);
530 } else if (strcmp(verb, "configure") == 0) {
531 g_nop_ctl_configure(req, mp);
533 } else if (strcmp(verb, "destroy") == 0) {
534 g_nop_ctl_destroy(req, mp);
536 } else if (strcmp(verb, "reset") == 0) {
537 g_nop_ctl_reset(req, mp);
541 gctl_error(req, "Unknown verb.");
545 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
546 struct g_consumer *cp, struct g_provider *pp)
548 struct g_nop_softc *sc;
550 if (pp != NULL || cp != NULL)
553 sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
554 (intmax_t)sc->sc_offset);
555 sbuf_printf(sb, "%s<FailProb>%u</FailProb>\n", indent, sc->sc_failprob);
556 sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
557 sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
558 sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
560 sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
564 DECLARE_GEOM_CLASS(g_nop_class, g_nop);