2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
42 #include <geom/geom.h>
43 #include <geom/nop/g_nop.h>
46 SYSCTL_DECL(_kern_geom);
47 static SYSCTL_NODE(_kern_geom, OID_AUTO, nop, CTLFLAG_RW, 0, "GEOM_NOP stuff");
48 static u_int g_nop_debug = 0;
49 SYSCTL_UINT(_kern_geom_nop, OID_AUTO, debug, CTLFLAG_RW, &g_nop_debug, 0,
52 static int g_nop_destroy(struct g_geom *gp, boolean_t force);
53 static int g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp,
55 static void g_nop_config(struct gctl_req *req, struct g_class *mp,
57 static void g_nop_dumpconf(struct sbuf *sb, const char *indent,
58 struct g_geom *gp, struct g_consumer *cp, struct g_provider *pp);
60 struct g_class g_nop_class = {
61 .name = G_NOP_CLASS_NAME,
63 .ctlreq = g_nop_config,
64 .destroy_geom = g_nop_destroy_geom
69 g_nop_orphan(struct g_consumer *cp)
73 g_nop_destroy(cp->geom, 1);
77 g_nop_resize(struct g_consumer *cp)
79 struct g_nop_softc *sc;
81 struct g_provider *pp;
89 if (sc->sc_explicitsize != 0)
91 if (cp->provider->mediasize < sc->sc_offset) {
95 size = cp->provider->mediasize - sc->sc_offset;
96 LIST_FOREACH(pp, &gp->provider, provider)
97 g_resize_provider(pp, size);
101 g_nop_start(struct bio *bp)
103 struct g_nop_softc *sc;
105 struct g_provider *pp;
109 gp = bp->bio_to->geom;
111 G_NOP_LOGREQ(bp, "Request received.");
112 mtx_lock(&sc->sc_lock);
113 switch (bp->bio_cmd) {
116 sc->sc_readbytes += bp->bio_length;
117 failprob = sc->sc_rfailprob;
121 sc->sc_wrotebytes += bp->bio_length;
122 failprob = sc->sc_wfailprob;
143 mtx_unlock(&sc->sc_lock);
147 rval = arc4random() % 100;
148 if (rval < failprob) {
149 G_NOP_LOGREQLVL(1, bp, "Returning error=%d.", sc->sc_error);
150 g_io_deliver(bp, sc->sc_error);
154 cbp = g_clone_bio(bp);
156 g_io_deliver(bp, ENOMEM);
159 cbp->bio_done = g_std_done;
160 cbp->bio_offset = bp->bio_offset + sc->sc_offset;
161 pp = LIST_FIRST(&gp->provider);
162 KASSERT(pp != NULL, ("NULL pp"));
164 G_NOP_LOGREQ(cbp, "Sending request.");
165 g_io_request(cbp, LIST_FIRST(&gp->consumer));
169 g_nop_access(struct g_provider *pp, int dr, int dw, int de)
172 struct g_consumer *cp;
176 cp = LIST_FIRST(&gp->consumer);
177 error = g_access(cp, dr, dw, de);
183 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
184 int ioerror, u_int rfailprob, u_int wfailprob, off_t offset, off_t size,
185 u_int secsize, u_int stripesize, u_int stripeoffset)
187 struct g_nop_softc *sc;
189 struct g_provider *newpp;
190 struct g_consumer *cp;
201 if ((offset % pp->sectorsize) != 0) {
202 gctl_error(req, "Invalid offset for provider %s.", pp->name);
205 if ((size % pp->sectorsize) != 0) {
206 gctl_error(req, "Invalid size for provider %s.", pp->name);
209 if (offset >= pp->mediasize) {
210 gctl_error(req, "Invalid offset for provider %s.", pp->name);
215 size = pp->mediasize - offset;
216 if (offset + size > pp->mediasize) {
217 gctl_error(req, "Invalid size for provider %s.", pp->name);
221 secsize = pp->sectorsize;
222 else if ((secsize % pp->sectorsize) != 0) {
223 gctl_error(req, "Invalid secsize for provider %s.", pp->name);
226 if (secsize > MAXPHYS) {
227 gctl_error(req, "secsize is too big.");
230 size -= size % secsize;
231 if ((stripesize % pp->sectorsize) != 0) {
232 gctl_error(req, "Invalid stripesize for provider %s.", pp->name);
235 if ((stripeoffset % pp->sectorsize) != 0) {
236 gctl_error(req, "Invalid stripeoffset for provider %s.", pp->name);
239 if (stripesize != 0 && stripeoffset >= stripesize) {
240 gctl_error(req, "stripeoffset is too big.");
243 snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX);
244 LIST_FOREACH(gp, &mp->geom, geom) {
245 if (strcmp(gp->name, name) == 0) {
246 gctl_error(req, "Provider %s already exists.", name);
250 gp = g_new_geomf(mp, "%s", name);
251 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
252 sc->sc_offset = offset;
253 sc->sc_explicitsize = explicitsize;
254 sc->sc_stripesize = stripesize;
255 sc->sc_stripeoffset = stripeoffset;
256 sc->sc_error = ioerror;
257 sc->sc_rfailprob = rfailprob;
258 sc->sc_wfailprob = wfailprob;
267 sc->sc_readbytes = 0;
268 sc->sc_wrotebytes = 0;
269 mtx_init(&sc->sc_lock, "gnop lock", NULL, MTX_DEF);
271 gp->start = g_nop_start;
272 gp->orphan = g_nop_orphan;
273 gp->resize = g_nop_resize;
274 gp->access = g_nop_access;
275 gp->dumpconf = g_nop_dumpconf;
277 newpp = g_new_providerf(gp, "%s", gp->name);
278 newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
279 newpp->mediasize = size;
280 newpp->sectorsize = secsize;
281 newpp->stripesize = stripesize;
282 newpp->stripeoffset = stripeoffset;
284 cp = g_new_consumer(gp);
285 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
286 error = g_attach(cp, pp);
288 gctl_error(req, "Cannot attach to provider %s.", pp->name);
292 newpp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
293 g_error_provider(newpp, 0);
294 G_NOP_DEBUG(0, "Device %s created.", gp->name);
297 if (cp->provider != NULL)
299 g_destroy_consumer(cp);
300 g_destroy_provider(newpp);
301 mtx_destroy(&sc->sc_lock);
308 g_nop_destroy(struct g_geom *gp, boolean_t force)
310 struct g_nop_softc *sc;
311 struct g_provider *pp;
317 pp = LIST_FIRST(&gp->provider);
318 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
320 G_NOP_DEBUG(0, "Device %s is still open, so it "
321 "can't be definitely removed.", pp->name);
323 G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
324 pp->name, pp->acr, pp->acw, pp->ace);
328 G_NOP_DEBUG(0, "Device %s removed.", gp->name);
331 mtx_destroy(&sc->sc_lock);
333 g_wither_geom(gp, ENXIO);
339 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
342 return (g_nop_destroy(gp, 0));
346 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
348 struct g_provider *pp;
349 intmax_t *error, *rfailprob, *wfailprob, *offset, *secsize, *size,
350 *stripesize, *stripeoffset;
357 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
359 gctl_error(req, "No '%s' argument", "nargs");
363 gctl_error(req, "Missing device(s).");
366 error = gctl_get_paraml(req, "error", sizeof(*error));
368 gctl_error(req, "No '%s' argument", "error");
371 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
372 if (rfailprob == NULL) {
373 gctl_error(req, "No '%s' argument", "rfailprob");
376 if (*rfailprob < -1 || *rfailprob > 100) {
377 gctl_error(req, "Invalid '%s' argument", "rfailprob");
380 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
381 if (wfailprob == NULL) {
382 gctl_error(req, "No '%s' argument", "wfailprob");
385 if (*wfailprob < -1 || *wfailprob > 100) {
386 gctl_error(req, "Invalid '%s' argument", "wfailprob");
389 offset = gctl_get_paraml(req, "offset", sizeof(*offset));
390 if (offset == NULL) {
391 gctl_error(req, "No '%s' argument", "offset");
395 gctl_error(req, "Invalid '%s' argument", "offset");
398 size = gctl_get_paraml(req, "size", sizeof(*size));
400 gctl_error(req, "No '%s' argument", "size");
404 gctl_error(req, "Invalid '%s' argument", "size");
407 secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
408 if (secsize == NULL) {
409 gctl_error(req, "No '%s' argument", "secsize");
413 gctl_error(req, "Invalid '%s' argument", "secsize");
416 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
417 if (stripesize == NULL) {
418 gctl_error(req, "No '%s' argument", "stripesize");
421 if (*stripesize < 0) {
422 gctl_error(req, "Invalid '%s' argument", "stripesize");
425 stripeoffset = gctl_get_paraml(req, "stripeoffset", sizeof(*stripeoffset));
426 if (stripeoffset == NULL) {
427 gctl_error(req, "No '%s' argument", "stripeoffset");
430 if (*stripeoffset < 0) {
431 gctl_error(req, "Invalid '%s' argument", "stripeoffset");
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);
446 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
447 gctl_error(req, "Provider %s is invalid.", name);
450 if (g_nop_create(req, mp, pp,
451 *error == -1 ? EIO : (int)*error,
452 *rfailprob == -1 ? 0 : (u_int)*rfailprob,
453 *wfailprob == -1 ? 0 : (u_int)*wfailprob,
454 (off_t)*offset, (off_t)*size, (u_int)*secsize,
455 (u_int)*stripesize, (u_int)*stripeoffset) != 0) {
462 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
464 struct g_nop_softc *sc;
465 struct g_provider *pp;
466 intmax_t *error, *rfailprob, *wfailprob;
473 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
475 gctl_error(req, "No '%s' argument", "nargs");
479 gctl_error(req, "Missing device(s).");
482 error = gctl_get_paraml(req, "error", sizeof(*error));
484 gctl_error(req, "No '%s' argument", "error");
487 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
488 if (rfailprob == NULL) {
489 gctl_error(req, "No '%s' argument", "rfailprob");
492 if (*rfailprob < -1 || *rfailprob > 100) {
493 gctl_error(req, "Invalid '%s' argument", "rfailprob");
496 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
497 if (wfailprob == NULL) {
498 gctl_error(req, "No '%s' argument", "wfailprob");
501 if (*wfailprob < -1 || *wfailprob > 100) {
502 gctl_error(req, "Invalid '%s' argument", "wfailprob");
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 pp = g_provider_by_name(name);
516 if (pp == NULL || pp->geom->class != mp) {
517 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
518 gctl_error(req, "Provider %s is invalid.", name);
521 sc = pp->geom->softc;
523 sc->sc_error = (int)*error;
524 if (*rfailprob != -1)
525 sc->sc_rfailprob = (u_int)*rfailprob;
526 if (*wfailprob != -1)
527 sc->sc_wfailprob = (u_int)*wfailprob;
531 static struct g_geom *
532 g_nop_find_geom(struct g_class *mp, const char *name)
536 LIST_FOREACH(gp, &mp->geom, geom) {
537 if (strcmp(gp->name, name) == 0)
544 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
546 int *nargs, *force, error, i;
553 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
555 gctl_error(req, "No '%s' argument", "nargs");
559 gctl_error(req, "Missing device(s).");
562 force = gctl_get_paraml(req, "force", sizeof(*force));
564 gctl_error(req, "No 'force' argument");
568 for (i = 0; i < *nargs; i++) {
569 snprintf(param, sizeof(param), "arg%d", i);
570 name = gctl_get_asciiparam(req, param);
572 gctl_error(req, "No 'arg%d' argument", i);
575 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
576 name += strlen("/dev/");
577 gp = g_nop_find_geom(mp, name);
579 G_NOP_DEBUG(1, "Device %s is invalid.", name);
580 gctl_error(req, "Device %s is invalid.", name);
583 error = g_nop_destroy(gp, *force);
585 gctl_error(req, "Cannot destroy device %s (error=%d).",
593 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
595 struct g_nop_softc *sc;
596 struct g_provider *pp;
603 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
605 gctl_error(req, "No '%s' argument", "nargs");
609 gctl_error(req, "Missing device(s).");
613 for (i = 0; i < *nargs; i++) {
614 snprintf(param, sizeof(param), "arg%d", i);
615 name = gctl_get_asciiparam(req, param);
617 gctl_error(req, "No 'arg%d' argument", i);
620 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
621 name += strlen("/dev/");
622 pp = g_provider_by_name(name);
623 if (pp == NULL || pp->geom->class != mp) {
624 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
625 gctl_error(req, "Provider %s is invalid.", name);
628 sc = pp->geom->softc;
637 sc->sc_readbytes = 0;
638 sc->sc_wrotebytes = 0;
643 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
649 version = gctl_get_paraml(req, "version", sizeof(*version));
650 if (version == NULL) {
651 gctl_error(req, "No '%s' argument.", "version");
654 if (*version != G_NOP_VERSION) {
655 gctl_error(req, "Userland and kernel parts are out of sync.");
659 if (strcmp(verb, "create") == 0) {
660 g_nop_ctl_create(req, mp);
662 } else if (strcmp(verb, "configure") == 0) {
663 g_nop_ctl_configure(req, mp);
665 } else if (strcmp(verb, "destroy") == 0) {
666 g_nop_ctl_destroy(req, mp);
668 } else if (strcmp(verb, "reset") == 0) {
669 g_nop_ctl_reset(req, mp);
673 gctl_error(req, "Unknown verb.");
677 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
678 struct g_consumer *cp, struct g_provider *pp)
680 struct g_nop_softc *sc;
682 if (pp != NULL || cp != NULL)
685 sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
686 (intmax_t)sc->sc_offset);
687 sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
689 sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
691 sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
692 sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
693 sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
694 sbuf_printf(sb, "%s<Deletes>%ju</Deletes>\n", indent, sc->sc_deletes);
695 sbuf_printf(sb, "%s<Getattrs>%ju</Getattrs>\n", indent, sc->sc_getattrs);
696 sbuf_printf(sb, "%s<Flushes>%ju</Flushes>\n", indent, sc->sc_flushes);
697 sbuf_printf(sb, "%s<Cmd0s>%ju</Cmd0s>\n", indent, sc->sc_cmd0s);
698 sbuf_printf(sb, "%s<Cmd1s>%ju</Cmd1s>\n", indent, sc->sc_cmd1s);
699 sbuf_printf(sb, "%s<Cmd2s>%ju</Cmd2s>\n", indent, sc->sc_cmd2s);
700 sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
702 sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
706 DECLARE_GEOM_CLASS(g_nop_class, g_nop);