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 g_access_t g_nop_access;
58 static g_dumpconf_t g_nop_dumpconf;
59 static g_orphan_t g_nop_orphan;
60 static g_provgone_t g_nop_providergone;
61 static g_resize_t g_nop_resize;
62 static g_start_t g_nop_start;
64 struct g_class g_nop_class = {
65 .name = G_NOP_CLASS_NAME,
67 .ctlreq = g_nop_config,
68 .destroy_geom = g_nop_destroy_geom,
69 .access = g_nop_access,
70 .dumpconf = g_nop_dumpconf,
71 .orphan = g_nop_orphan,
72 .providergone = g_nop_providergone,
73 .resize = g_nop_resize,
78 g_nop_orphan(struct g_consumer *cp)
82 g_nop_destroy(cp->geom, 1);
86 g_nop_resize(struct g_consumer *cp)
88 struct g_nop_softc *sc;
90 struct g_provider *pp;
98 if (sc->sc_explicitsize != 0)
100 if (cp->provider->mediasize < sc->sc_offset) {
101 g_nop_destroy(gp, 1);
104 size = cp->provider->mediasize - sc->sc_offset;
105 LIST_FOREACH(pp, &gp->provider, provider)
106 g_resize_provider(pp, size);
110 g_nop_dumper(void *priv, void *virtual, vm_offset_t physical, off_t offset,
117 g_nop_kerneldump(struct bio *bp, struct g_nop_softc *sc)
119 struct g_kerneldump *gkd;
121 struct g_provider *pp;
123 gkd = (struct g_kerneldump *)bp->bio_data;
124 gp = bp->bio_to->geom;
125 g_trace(G_T_TOPOLOGY, "%s(%s, %jd, %jd)", __func__, gp->name,
126 (intmax_t)gkd->offset, (intmax_t)gkd->length);
128 pp = LIST_FIRST(&gp->provider);
130 gkd->di.dumper = g_nop_dumper;
132 gkd->di.blocksize = pp->sectorsize;
133 gkd->di.maxiosize = DFLTPHYS;
134 gkd->di.mediaoffset = sc->sc_offset + gkd->offset;
135 if (gkd->offset > sc->sc_explicitsize) {
136 g_io_deliver(bp, ENODEV);
139 if (gkd->offset + gkd->length > sc->sc_explicitsize)
140 gkd->length = sc->sc_explicitsize - gkd->offset;
141 gkd->di.mediasize = gkd->length;
146 g_nop_start(struct bio *bp)
148 struct g_nop_softc *sc;
150 struct g_provider *pp;
154 gp = bp->bio_to->geom;
156 G_NOP_LOGREQ(bp, "Request received.");
157 mtx_lock(&sc->sc_lock);
158 switch (bp->bio_cmd) {
161 sc->sc_readbytes += bp->bio_length;
162 failprob = sc->sc_rfailprob;
166 sc->sc_wrotebytes += bp->bio_length;
167 failprob = sc->sc_wfailprob;
174 if (sc->sc_physpath &&
175 g_handleattr_str(bp, "GEOM::physpath", sc->sc_physpath))
177 else if (strcmp(bp->bio_attribute, "GEOM::kerneldump") == 0)
178 g_nop_kerneldump(bp, sc);
181 * Fallthrough to forwarding the GETATTR down to the
182 * lower level device.
185 mtx_unlock(&sc->sc_lock);
200 mtx_unlock(&sc->sc_lock);
204 rval = arc4random() % 100;
205 if (rval < failprob) {
206 G_NOP_LOGREQLVL(1, bp, "Returning error=%d.", sc->sc_error);
207 g_io_deliver(bp, sc->sc_error);
211 cbp = g_clone_bio(bp);
213 g_io_deliver(bp, ENOMEM);
216 cbp->bio_done = g_std_done;
217 cbp->bio_offset = bp->bio_offset + sc->sc_offset;
218 pp = LIST_FIRST(&gp->provider);
219 KASSERT(pp != NULL, ("NULL pp"));
221 G_NOP_LOGREQ(cbp, "Sending request.");
222 g_io_request(cbp, LIST_FIRST(&gp->consumer));
226 g_nop_access(struct g_provider *pp, int dr, int dw, int de)
229 struct g_consumer *cp;
233 cp = LIST_FIRST(&gp->consumer);
234 error = g_access(cp, dr, dw, de);
240 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
241 int ioerror, u_int rfailprob, u_int wfailprob, off_t offset, off_t size,
242 u_int secsize, off_t stripesize, off_t stripeoffset, const char *physpath)
244 struct g_nop_softc *sc;
246 struct g_provider *newpp;
247 struct g_consumer *cp;
258 if ((offset % pp->sectorsize) != 0) {
259 gctl_error(req, "Invalid offset for provider %s.", pp->name);
262 if ((size % pp->sectorsize) != 0) {
263 gctl_error(req, "Invalid size for provider %s.", pp->name);
266 if (offset >= pp->mediasize) {
267 gctl_error(req, "Invalid offset for provider %s.", pp->name);
272 size = pp->mediasize - offset;
273 if (offset + size > pp->mediasize) {
274 gctl_error(req, "Invalid size for provider %s.", pp->name);
278 secsize = pp->sectorsize;
279 else if ((secsize % pp->sectorsize) != 0) {
280 gctl_error(req, "Invalid secsize for provider %s.", pp->name);
283 if (secsize > MAXPHYS) {
284 gctl_error(req, "secsize is too big.");
287 size -= size % secsize;
288 if ((stripesize % pp->sectorsize) != 0) {
289 gctl_error(req, "Invalid stripesize for provider %s.", pp->name);
292 if ((stripeoffset % pp->sectorsize) != 0) {
293 gctl_error(req, "Invalid stripeoffset for provider %s.", pp->name);
296 if (stripesize != 0 && stripeoffset >= stripesize) {
297 gctl_error(req, "stripeoffset is too big.");
300 snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX);
301 LIST_FOREACH(gp, &mp->geom, geom) {
302 if (strcmp(gp->name, name) == 0) {
303 gctl_error(req, "Provider %s already exists.", name);
307 gp = g_new_geomf(mp, "%s", name);
308 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
309 sc->sc_offset = offset;
310 sc->sc_explicitsize = explicitsize;
311 sc->sc_stripesize = stripesize;
312 sc->sc_stripeoffset = stripeoffset;
313 if (physpath && strcmp(physpath, G_NOP_PHYSPATH_PASSTHROUGH)) {
314 sc->sc_physpath = strndup(physpath, MAXPATHLEN, M_GEOM);
316 sc->sc_physpath = NULL;
317 sc->sc_error = ioerror;
318 sc->sc_rfailprob = rfailprob;
319 sc->sc_wfailprob = wfailprob;
328 sc->sc_readbytes = 0;
329 sc->sc_wrotebytes = 0;
330 mtx_init(&sc->sc_lock, "gnop lock", NULL, MTX_DEF);
333 newpp = g_new_providerf(gp, "%s", gp->name);
334 newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
335 newpp->mediasize = size;
336 newpp->sectorsize = secsize;
337 newpp->stripesize = stripesize;
338 newpp->stripeoffset = stripeoffset;
340 cp = g_new_consumer(gp);
341 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
342 error = g_attach(cp, pp);
344 gctl_error(req, "Cannot attach to provider %s.", pp->name);
348 newpp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
349 g_error_provider(newpp, 0);
350 G_NOP_DEBUG(0, "Device %s created.", gp->name);
353 if (cp->provider != NULL)
355 g_destroy_consumer(cp);
356 g_destroy_provider(newpp);
357 mtx_destroy(&sc->sc_lock);
358 free(sc->sc_physpath, M_GEOM);
365 g_nop_providergone(struct g_provider *pp)
367 struct g_geom *gp = pp->geom;
368 struct g_nop_softc *sc = gp->softc;
371 free(sc->sc_physpath, M_GEOM);
372 mtx_destroy(&sc->sc_lock);
377 g_nop_destroy(struct g_geom *gp, boolean_t force)
379 struct g_nop_softc *sc;
380 struct g_provider *pp;
386 pp = LIST_FIRST(&gp->provider);
387 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
389 G_NOP_DEBUG(0, "Device %s is still open, so it "
390 "can't be definitely removed.", pp->name);
392 G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
393 pp->name, pp->acr, pp->acw, pp->ace);
397 G_NOP_DEBUG(0, "Device %s removed.", gp->name);
399 g_wither_geom(gp, ENXIO);
405 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
408 return (g_nop_destroy(gp, 0));
412 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
414 struct g_provider *pp;
415 intmax_t *error, *rfailprob, *wfailprob, *offset, *secsize, *size,
416 *stripesize, *stripeoffset;
417 const char *name, *physpath;
423 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
425 gctl_error(req, "No '%s' argument", "nargs");
429 gctl_error(req, "Missing device(s).");
432 error = gctl_get_paraml(req, "error", sizeof(*error));
434 gctl_error(req, "No '%s' argument", "error");
437 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
438 if (rfailprob == NULL) {
439 gctl_error(req, "No '%s' argument", "rfailprob");
442 if (*rfailprob < -1 || *rfailprob > 100) {
443 gctl_error(req, "Invalid '%s' argument", "rfailprob");
446 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
447 if (wfailprob == NULL) {
448 gctl_error(req, "No '%s' argument", "wfailprob");
451 if (*wfailprob < -1 || *wfailprob > 100) {
452 gctl_error(req, "Invalid '%s' argument", "wfailprob");
455 offset = gctl_get_paraml(req, "offset", sizeof(*offset));
456 if (offset == NULL) {
457 gctl_error(req, "No '%s' argument", "offset");
461 gctl_error(req, "Invalid '%s' argument", "offset");
464 size = gctl_get_paraml(req, "size", sizeof(*size));
466 gctl_error(req, "No '%s' argument", "size");
470 gctl_error(req, "Invalid '%s' argument", "size");
473 secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
474 if (secsize == NULL) {
475 gctl_error(req, "No '%s' argument", "secsize");
479 gctl_error(req, "Invalid '%s' argument", "secsize");
482 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
483 if (stripesize == NULL) {
484 gctl_error(req, "No '%s' argument", "stripesize");
487 if (*stripesize < 0) {
488 gctl_error(req, "Invalid '%s' argument", "stripesize");
491 stripeoffset = gctl_get_paraml(req, "stripeoffset", sizeof(*stripeoffset));
492 if (stripeoffset == NULL) {
493 gctl_error(req, "No '%s' argument", "stripeoffset");
496 if (*stripeoffset < 0) {
497 gctl_error(req, "Invalid '%s' argument", "stripeoffset");
500 physpath = gctl_get_asciiparam(req, "physpath");
502 for (i = 0; i < *nargs; i++) {
503 snprintf(param, sizeof(param), "arg%d", i);
504 name = gctl_get_asciiparam(req, param);
506 gctl_error(req, "No 'arg%d' argument", i);
509 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
510 name += strlen("/dev/");
511 pp = g_provider_by_name(name);
513 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
514 gctl_error(req, "Provider %s is invalid.", name);
517 if (g_nop_create(req, mp, pp,
518 *error == -1 ? EIO : (int)*error,
519 *rfailprob == -1 ? 0 : (u_int)*rfailprob,
520 *wfailprob == -1 ? 0 : (u_int)*wfailprob,
521 (off_t)*offset, (off_t)*size, (u_int)*secsize,
522 (off_t)*stripesize, (off_t)*stripeoffset,
530 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
532 struct g_nop_softc *sc;
533 struct g_provider *pp;
534 intmax_t *error, *rfailprob, *wfailprob;
541 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
543 gctl_error(req, "No '%s' argument", "nargs");
547 gctl_error(req, "Missing device(s).");
550 error = gctl_get_paraml(req, "error", sizeof(*error));
552 gctl_error(req, "No '%s' argument", "error");
555 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
556 if (rfailprob == NULL) {
557 gctl_error(req, "No '%s' argument", "rfailprob");
560 if (*rfailprob < -1 || *rfailprob > 100) {
561 gctl_error(req, "Invalid '%s' argument", "rfailprob");
564 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
565 if (wfailprob == NULL) {
566 gctl_error(req, "No '%s' argument", "wfailprob");
569 if (*wfailprob < -1 || *wfailprob > 100) {
570 gctl_error(req, "Invalid '%s' argument", "wfailprob");
574 for (i = 0; i < *nargs; i++) {
575 snprintf(param, sizeof(param), "arg%d", i);
576 name = gctl_get_asciiparam(req, param);
578 gctl_error(req, "No 'arg%d' argument", i);
581 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
582 name += strlen("/dev/");
583 pp = g_provider_by_name(name);
584 if (pp == NULL || pp->geom->class != mp) {
585 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
586 gctl_error(req, "Provider %s is invalid.", name);
589 sc = pp->geom->softc;
591 sc->sc_error = (int)*error;
592 if (*rfailprob != -1)
593 sc->sc_rfailprob = (u_int)*rfailprob;
594 if (*wfailprob != -1)
595 sc->sc_wfailprob = (u_int)*wfailprob;
599 static struct g_geom *
600 g_nop_find_geom(struct g_class *mp, const char *name)
604 LIST_FOREACH(gp, &mp->geom, geom) {
605 if (strcmp(gp->name, name) == 0)
612 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
614 int *nargs, *force, error, i;
621 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
623 gctl_error(req, "No '%s' argument", "nargs");
627 gctl_error(req, "Missing device(s).");
630 force = gctl_get_paraml(req, "force", sizeof(*force));
632 gctl_error(req, "No 'force' argument");
636 for (i = 0; i < *nargs; i++) {
637 snprintf(param, sizeof(param), "arg%d", i);
638 name = gctl_get_asciiparam(req, param);
640 gctl_error(req, "No 'arg%d' argument", i);
643 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
644 name += strlen("/dev/");
645 gp = g_nop_find_geom(mp, name);
647 G_NOP_DEBUG(1, "Device %s is invalid.", name);
648 gctl_error(req, "Device %s is invalid.", name);
651 error = g_nop_destroy(gp, *force);
653 gctl_error(req, "Cannot destroy device %s (error=%d).",
661 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
663 struct g_nop_softc *sc;
664 struct g_provider *pp;
671 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
673 gctl_error(req, "No '%s' argument", "nargs");
677 gctl_error(req, "Missing device(s).");
681 for (i = 0; i < *nargs; i++) {
682 snprintf(param, sizeof(param), "arg%d", i);
683 name = gctl_get_asciiparam(req, param);
685 gctl_error(req, "No 'arg%d' argument", i);
688 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
689 name += strlen("/dev/");
690 pp = g_provider_by_name(name);
691 if (pp == NULL || pp->geom->class != mp) {
692 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
693 gctl_error(req, "Provider %s is invalid.", name);
696 sc = pp->geom->softc;
705 sc->sc_readbytes = 0;
706 sc->sc_wrotebytes = 0;
711 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
717 version = gctl_get_paraml(req, "version", sizeof(*version));
718 if (version == NULL) {
719 gctl_error(req, "No '%s' argument.", "version");
722 if (*version != G_NOP_VERSION) {
723 gctl_error(req, "Userland and kernel parts are out of sync.");
727 if (strcmp(verb, "create") == 0) {
728 g_nop_ctl_create(req, mp);
730 } else if (strcmp(verb, "configure") == 0) {
731 g_nop_ctl_configure(req, mp);
733 } else if (strcmp(verb, "destroy") == 0) {
734 g_nop_ctl_destroy(req, mp);
736 } else if (strcmp(verb, "reset") == 0) {
737 g_nop_ctl_reset(req, mp);
741 gctl_error(req, "Unknown verb.");
745 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
746 struct g_consumer *cp, struct g_provider *pp)
748 struct g_nop_softc *sc;
750 if (pp != NULL || cp != NULL)
753 sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
754 (intmax_t)sc->sc_offset);
755 sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
757 sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
759 sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
760 sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
761 sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
762 sbuf_printf(sb, "%s<Deletes>%ju</Deletes>\n", indent, sc->sc_deletes);
763 sbuf_printf(sb, "%s<Getattrs>%ju</Getattrs>\n", indent, sc->sc_getattrs);
764 sbuf_printf(sb, "%s<Flushes>%ju</Flushes>\n", indent, sc->sc_flushes);
765 sbuf_printf(sb, "%s<Cmd0s>%ju</Cmd0s>\n", indent, sc->sc_cmd0s);
766 sbuf_printf(sb, "%s<Cmd1s>%ju</Cmd1s>\n", indent, sc->sc_cmd1s);
767 sbuf_printf(sb, "%s<Cmd2s>%ju</Cmd2s>\n", indent, sc->sc_cmd2s);
768 sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
770 sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
774 DECLARE_GEOM_CLASS(g_nop_class, g_nop);
775 MODULE_VERSION(geom_nop, 0);