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;
129 if (sc->sc_physpath &&
130 g_handleattr_str(bp, "GEOM::physpath", sc->sc_physpath)) {
131 mtx_unlock(&sc->sc_lock);
148 mtx_unlock(&sc->sc_lock);
152 rval = arc4random() % 100;
153 if (rval < failprob) {
154 G_NOP_LOGREQLVL(1, bp, "Returning error=%d.", sc->sc_error);
155 g_io_deliver(bp, sc->sc_error);
159 cbp = g_clone_bio(bp);
161 g_io_deliver(bp, ENOMEM);
164 cbp->bio_done = g_std_done;
165 cbp->bio_offset = bp->bio_offset + sc->sc_offset;
166 pp = LIST_FIRST(&gp->provider);
167 KASSERT(pp != NULL, ("NULL pp"));
169 G_NOP_LOGREQ(cbp, "Sending request.");
170 g_io_request(cbp, LIST_FIRST(&gp->consumer));
174 g_nop_access(struct g_provider *pp, int dr, int dw, int de)
177 struct g_consumer *cp;
181 cp = LIST_FIRST(&gp->consumer);
182 error = g_access(cp, dr, dw, de);
188 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp,
189 int ioerror, u_int rfailprob, u_int wfailprob, off_t offset, off_t size,
190 u_int secsize, off_t stripesize, off_t stripeoffset, const char *physpath)
192 struct g_nop_softc *sc;
194 struct g_provider *newpp;
195 struct g_consumer *cp;
206 if ((offset % pp->sectorsize) != 0) {
207 gctl_error(req, "Invalid offset for provider %s.", pp->name);
210 if ((size % pp->sectorsize) != 0) {
211 gctl_error(req, "Invalid size for provider %s.", pp->name);
214 if (offset >= pp->mediasize) {
215 gctl_error(req, "Invalid offset for provider %s.", pp->name);
220 size = pp->mediasize - offset;
221 if (offset + size > pp->mediasize) {
222 gctl_error(req, "Invalid size for provider %s.", pp->name);
226 secsize = pp->sectorsize;
227 else if ((secsize % pp->sectorsize) != 0) {
228 gctl_error(req, "Invalid secsize for provider %s.", pp->name);
231 if (secsize > MAXPHYS) {
232 gctl_error(req, "secsize is too big.");
235 size -= size % secsize;
236 if ((stripesize % pp->sectorsize) != 0) {
237 gctl_error(req, "Invalid stripesize for provider %s.", pp->name);
240 if ((stripeoffset % pp->sectorsize) != 0) {
241 gctl_error(req, "Invalid stripeoffset for provider %s.", pp->name);
244 if (stripesize != 0 && stripeoffset >= stripesize) {
245 gctl_error(req, "stripeoffset is too big.");
248 snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX);
249 LIST_FOREACH(gp, &mp->geom, geom) {
250 if (strcmp(gp->name, name) == 0) {
251 gctl_error(req, "Provider %s already exists.", name);
255 gp = g_new_geomf(mp, "%s", name);
256 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO);
257 sc->sc_offset = offset;
258 sc->sc_explicitsize = explicitsize;
259 sc->sc_stripesize = stripesize;
260 sc->sc_stripeoffset = stripeoffset;
261 if (physpath && strcmp(physpath, G_NOP_PHYSPATH_PASSTHROUGH)) {
262 sc->sc_physpath = strndup(physpath, MAXPATHLEN, M_GEOM);
264 sc->sc_physpath = NULL;
265 sc->sc_error = ioerror;
266 sc->sc_rfailprob = rfailprob;
267 sc->sc_wfailprob = wfailprob;
276 sc->sc_readbytes = 0;
277 sc->sc_wrotebytes = 0;
278 mtx_init(&sc->sc_lock, "gnop lock", NULL, MTX_DEF);
280 gp->start = g_nop_start;
281 gp->orphan = g_nop_orphan;
282 gp->resize = g_nop_resize;
283 gp->access = g_nop_access;
284 gp->dumpconf = g_nop_dumpconf;
286 newpp = g_new_providerf(gp, "%s", gp->name);
287 newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE;
288 newpp->mediasize = size;
289 newpp->sectorsize = secsize;
290 newpp->stripesize = stripesize;
291 newpp->stripeoffset = stripeoffset;
293 cp = g_new_consumer(gp);
294 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE;
295 error = g_attach(cp, pp);
297 gctl_error(req, "Cannot attach to provider %s.", pp->name);
301 newpp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED;
302 g_error_provider(newpp, 0);
303 G_NOP_DEBUG(0, "Device %s created.", gp->name);
306 if (cp->provider != NULL)
308 g_destroy_consumer(cp);
309 g_destroy_provider(newpp);
310 mtx_destroy(&sc->sc_lock);
311 free(sc->sc_physpath, M_GEOM);
318 g_nop_destroy(struct g_geom *gp, boolean_t force)
320 struct g_nop_softc *sc;
321 struct g_provider *pp;
327 free(sc->sc_physpath, M_GEOM);
328 pp = LIST_FIRST(&gp->provider);
329 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) {
331 G_NOP_DEBUG(0, "Device %s is still open, so it "
332 "can't be definitely removed.", pp->name);
334 G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).",
335 pp->name, pp->acr, pp->acw, pp->ace);
339 G_NOP_DEBUG(0, "Device %s removed.", gp->name);
342 mtx_destroy(&sc->sc_lock);
344 g_wither_geom(gp, ENXIO);
350 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
353 return (g_nop_destroy(gp, 0));
357 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp)
359 struct g_provider *pp;
360 intmax_t *error, *rfailprob, *wfailprob, *offset, *secsize, *size,
361 *stripesize, *stripeoffset;
362 const char *name, *physpath;
368 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
370 gctl_error(req, "No '%s' argument", "nargs");
374 gctl_error(req, "Missing device(s).");
377 error = gctl_get_paraml(req, "error", sizeof(*error));
379 gctl_error(req, "No '%s' argument", "error");
382 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
383 if (rfailprob == NULL) {
384 gctl_error(req, "No '%s' argument", "rfailprob");
387 if (*rfailprob < -1 || *rfailprob > 100) {
388 gctl_error(req, "Invalid '%s' argument", "rfailprob");
391 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
392 if (wfailprob == NULL) {
393 gctl_error(req, "No '%s' argument", "wfailprob");
396 if (*wfailprob < -1 || *wfailprob > 100) {
397 gctl_error(req, "Invalid '%s' argument", "wfailprob");
400 offset = gctl_get_paraml(req, "offset", sizeof(*offset));
401 if (offset == NULL) {
402 gctl_error(req, "No '%s' argument", "offset");
406 gctl_error(req, "Invalid '%s' argument", "offset");
409 size = gctl_get_paraml(req, "size", sizeof(*size));
411 gctl_error(req, "No '%s' argument", "size");
415 gctl_error(req, "Invalid '%s' argument", "size");
418 secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize));
419 if (secsize == NULL) {
420 gctl_error(req, "No '%s' argument", "secsize");
424 gctl_error(req, "Invalid '%s' argument", "secsize");
427 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize));
428 if (stripesize == NULL) {
429 gctl_error(req, "No '%s' argument", "stripesize");
432 if (*stripesize < 0) {
433 gctl_error(req, "Invalid '%s' argument", "stripesize");
436 stripeoffset = gctl_get_paraml(req, "stripeoffset", sizeof(*stripeoffset));
437 if (stripeoffset == NULL) {
438 gctl_error(req, "No '%s' argument", "stripeoffset");
441 if (*stripeoffset < 0) {
442 gctl_error(req, "Invalid '%s' argument", "stripeoffset");
445 physpath = gctl_get_asciiparam(req, "physpath");
447 for (i = 0; i < *nargs; i++) {
448 snprintf(param, sizeof(param), "arg%d", i);
449 name = gctl_get_asciiparam(req, param);
451 gctl_error(req, "No 'arg%d' argument", i);
454 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
455 name += strlen("/dev/");
456 pp = g_provider_by_name(name);
458 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
459 gctl_error(req, "Provider %s is invalid.", name);
462 if (g_nop_create(req, mp, pp,
463 *error == -1 ? EIO : (int)*error,
464 *rfailprob == -1 ? 0 : (u_int)*rfailprob,
465 *wfailprob == -1 ? 0 : (u_int)*wfailprob,
466 (off_t)*offset, (off_t)*size, (u_int)*secsize,
467 (off_t)*stripesize, (off_t)*stripeoffset,
475 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp)
477 struct g_nop_softc *sc;
478 struct g_provider *pp;
479 intmax_t *error, *rfailprob, *wfailprob;
486 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
488 gctl_error(req, "No '%s' argument", "nargs");
492 gctl_error(req, "Missing device(s).");
495 error = gctl_get_paraml(req, "error", sizeof(*error));
497 gctl_error(req, "No '%s' argument", "error");
500 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob));
501 if (rfailprob == NULL) {
502 gctl_error(req, "No '%s' argument", "rfailprob");
505 if (*rfailprob < -1 || *rfailprob > 100) {
506 gctl_error(req, "Invalid '%s' argument", "rfailprob");
509 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob));
510 if (wfailprob == NULL) {
511 gctl_error(req, "No '%s' argument", "wfailprob");
514 if (*wfailprob < -1 || *wfailprob > 100) {
515 gctl_error(req, "Invalid '%s' argument", "wfailprob");
519 for (i = 0; i < *nargs; i++) {
520 snprintf(param, sizeof(param), "arg%d", i);
521 name = gctl_get_asciiparam(req, param);
523 gctl_error(req, "No 'arg%d' argument", i);
526 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
527 name += strlen("/dev/");
528 pp = g_provider_by_name(name);
529 if (pp == NULL || pp->geom->class != mp) {
530 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
531 gctl_error(req, "Provider %s is invalid.", name);
534 sc = pp->geom->softc;
536 sc->sc_error = (int)*error;
537 if (*rfailprob != -1)
538 sc->sc_rfailprob = (u_int)*rfailprob;
539 if (*wfailprob != -1)
540 sc->sc_wfailprob = (u_int)*wfailprob;
544 static struct g_geom *
545 g_nop_find_geom(struct g_class *mp, const char *name)
549 LIST_FOREACH(gp, &mp->geom, geom) {
550 if (strcmp(gp->name, name) == 0)
557 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp)
559 int *nargs, *force, error, i;
566 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
568 gctl_error(req, "No '%s' argument", "nargs");
572 gctl_error(req, "Missing device(s).");
575 force = gctl_get_paraml(req, "force", sizeof(*force));
577 gctl_error(req, "No 'force' argument");
581 for (i = 0; i < *nargs; i++) {
582 snprintf(param, sizeof(param), "arg%d", i);
583 name = gctl_get_asciiparam(req, param);
585 gctl_error(req, "No 'arg%d' argument", i);
588 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
589 name += strlen("/dev/");
590 gp = g_nop_find_geom(mp, name);
592 G_NOP_DEBUG(1, "Device %s is invalid.", name);
593 gctl_error(req, "Device %s is invalid.", name);
596 error = g_nop_destroy(gp, *force);
598 gctl_error(req, "Cannot destroy device %s (error=%d).",
606 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp)
608 struct g_nop_softc *sc;
609 struct g_provider *pp;
616 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs));
618 gctl_error(req, "No '%s' argument", "nargs");
622 gctl_error(req, "Missing device(s).");
626 for (i = 0; i < *nargs; i++) {
627 snprintf(param, sizeof(param), "arg%d", i);
628 name = gctl_get_asciiparam(req, param);
630 gctl_error(req, "No 'arg%d' argument", i);
633 if (strncmp(name, "/dev/", strlen("/dev/")) == 0)
634 name += strlen("/dev/");
635 pp = g_provider_by_name(name);
636 if (pp == NULL || pp->geom->class != mp) {
637 G_NOP_DEBUG(1, "Provider %s is invalid.", name);
638 gctl_error(req, "Provider %s is invalid.", name);
641 sc = pp->geom->softc;
650 sc->sc_readbytes = 0;
651 sc->sc_wrotebytes = 0;
656 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb)
662 version = gctl_get_paraml(req, "version", sizeof(*version));
663 if (version == NULL) {
664 gctl_error(req, "No '%s' argument.", "version");
667 if (*version != G_NOP_VERSION) {
668 gctl_error(req, "Userland and kernel parts are out of sync.");
672 if (strcmp(verb, "create") == 0) {
673 g_nop_ctl_create(req, mp);
675 } else if (strcmp(verb, "configure") == 0) {
676 g_nop_ctl_configure(req, mp);
678 } else if (strcmp(verb, "destroy") == 0) {
679 g_nop_ctl_destroy(req, mp);
681 } else if (strcmp(verb, "reset") == 0) {
682 g_nop_ctl_reset(req, mp);
686 gctl_error(req, "Unknown verb.");
690 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp,
691 struct g_consumer *cp, struct g_provider *pp)
693 struct g_nop_softc *sc;
695 if (pp != NULL || cp != NULL)
698 sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent,
699 (intmax_t)sc->sc_offset);
700 sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent,
702 sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent,
704 sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error);
705 sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads);
706 sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes);
707 sbuf_printf(sb, "%s<Deletes>%ju</Deletes>\n", indent, sc->sc_deletes);
708 sbuf_printf(sb, "%s<Getattrs>%ju</Getattrs>\n", indent, sc->sc_getattrs);
709 sbuf_printf(sb, "%s<Flushes>%ju</Flushes>\n", indent, sc->sc_flushes);
710 sbuf_printf(sb, "%s<Cmd0s>%ju</Cmd0s>\n", indent, sc->sc_cmd0s);
711 sbuf_printf(sb, "%s<Cmd1s>%ju</Cmd1s>\n", indent, sc->sc_cmd1s);
712 sbuf_printf(sb, "%s<Cmd2s>%ju</Cmd2s>\n", indent, sc->sc_cmd2s);
713 sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent,
715 sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent,
719 DECLARE_GEOM_CLASS(g_nop_class, g_nop);
720 MODULE_VERSION(geom_nop, 0);