1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2004-2006 Pawel Jakub Dawidek <pjd@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #include <sys/bio.h> 39 #include <sys/sbuf.h> 40 #include <sys/sysctl.h> 41 #include <sys/malloc.h> 42 #include <geom/geom.h> 43 #include <geom/geom_dbg.h> 44 #include <geom/nop/g_nop.h> 45 46 47 SYSCTL_DECL(_kern_geom); 48 static SYSCTL_NODE(_kern_geom, OID_AUTO, nop, CTLFLAG_RW, 0, "GEOM_NOP stuff"); 49 static u_int g_nop_debug = 0; 50 SYSCTL_UINT(_kern_geom_nop, OID_AUTO, debug, CTLFLAG_RW, &g_nop_debug, 0, 51 "Debug level"); 52 53 static int g_nop_destroy(struct g_geom *gp, boolean_t force); 54 static int g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, 55 struct g_geom *gp); 56 static void g_nop_config(struct gctl_req *req, struct g_class *mp, 57 const char *verb); 58 static g_access_t g_nop_access; 59 static g_dumpconf_t g_nop_dumpconf; 60 static g_orphan_t g_nop_orphan; 61 static g_provgone_t g_nop_providergone; 62 static g_resize_t g_nop_resize; 63 static g_start_t g_nop_start; 64 65 struct g_class g_nop_class = { 66 .name = G_NOP_CLASS_NAME, 67 .version = G_VERSION, 68 .ctlreq = g_nop_config, 69 .destroy_geom = g_nop_destroy_geom, 70 .access = g_nop_access, 71 .dumpconf = g_nop_dumpconf, 72 .orphan = g_nop_orphan, 73 .providergone = g_nop_providergone, 74 .resize = g_nop_resize, 75 .start = g_nop_start, 76 }; 77 78 struct g_nop_delay { 79 struct callout dl_cal; 80 struct bio *dl_bio; 81 TAILQ_ENTRY(g_nop_delay) dl_next; 82 }; 83 84 static void 85 g_nop_orphan(struct g_consumer *cp) 86 { 87 88 g_topology_assert(); 89 g_nop_destroy(cp->geom, 1); 90 } 91 92 static void 93 g_nop_resize(struct g_consumer *cp) 94 { 95 struct g_nop_softc *sc; 96 struct g_geom *gp; 97 struct g_provider *pp; 98 off_t size; 99 100 g_topology_assert(); 101 102 gp = cp->geom; 103 sc = gp->softc; 104 105 if (sc->sc_explicitsize != 0) 106 return; 107 if (cp->provider->mediasize < sc->sc_offset) { 108 g_nop_destroy(gp, 1); 109 return; 110 } 111 size = cp->provider->mediasize - sc->sc_offset; 112 LIST_FOREACH(pp, &gp->provider, provider) 113 g_resize_provider(pp, size); 114 } 115 116 static int 117 g_nop_dumper(void *priv, void *virtual, vm_offset_t physical, off_t offset, 118 size_t length) 119 { 120 121 return (0); 122 } 123 124 static void 125 g_nop_kerneldump(struct bio *bp, struct g_nop_softc *sc) 126 { 127 struct g_kerneldump *gkd; 128 struct g_geom *gp; 129 struct g_provider *pp; 130 131 gkd = (struct g_kerneldump *)bp->bio_data; 132 gp = bp->bio_to->geom; 133 g_trace(G_T_TOPOLOGY, "%s(%s, %jd, %jd)", __func__, gp->name, 134 (intmax_t)gkd->offset, (intmax_t)gkd->length); 135 136 pp = LIST_FIRST(&gp->provider); 137 138 gkd->di.dumper = g_nop_dumper; 139 gkd->di.priv = sc; 140 gkd->di.blocksize = pp->sectorsize; 141 gkd->di.maxiosize = DFLTPHYS; 142 gkd->di.mediaoffset = sc->sc_offset + gkd->offset; 143 if (gkd->offset > sc->sc_explicitsize) { 144 g_io_deliver(bp, ENODEV); 145 return; 146 } 147 if (gkd->offset + gkd->length > sc->sc_explicitsize) 148 gkd->length = sc->sc_explicitsize - gkd->offset; 149 gkd->di.mediasize = gkd->length; 150 g_io_deliver(bp, 0); 151 } 152 153 static void 154 g_nop_pass(struct bio *cbp, struct g_geom *gp) 155 { 156 157 G_NOP_LOGREQ(cbp, "Sending request."); 158 g_io_request(cbp, LIST_FIRST(&gp->consumer)); 159 } 160 161 static void 162 g_nop_pass_timeout(void *data) 163 { 164 struct g_nop_softc *sc; 165 struct g_geom *gp; 166 struct g_nop_delay *gndelay; 167 168 gndelay = (struct g_nop_delay *)data; 169 170 gp = gndelay->dl_bio->bio_to->geom; 171 sc = gp->softc; 172 173 mtx_lock(&sc->sc_lock); 174 TAILQ_REMOVE(&sc->sc_head_delay, gndelay, dl_next); 175 mtx_unlock(&sc->sc_lock); 176 177 g_nop_pass(gndelay->dl_bio, gp); 178 179 g_free(data); 180 } 181 182 static void 183 g_nop_start(struct bio *bp) 184 { 185 struct g_nop_softc *sc; 186 struct g_geom *gp; 187 struct g_provider *pp; 188 struct bio *cbp; 189 u_int failprob, delayprob, delaytime; 190 191 failprob = delayprob = 0; 192 193 gp = bp->bio_to->geom; 194 sc = gp->softc; 195 196 G_NOP_LOGREQ(bp, "Request received."); 197 mtx_lock(&sc->sc_lock); 198 switch (bp->bio_cmd) { 199 case BIO_READ: 200 sc->sc_reads++; 201 sc->sc_readbytes += bp->bio_length; 202 failprob = sc->sc_rfailprob; 203 delayprob = sc->sc_rdelayprob; 204 delaytime = sc->sc_delaymsec; 205 break; 206 case BIO_WRITE: 207 sc->sc_writes++; 208 sc->sc_wrotebytes += bp->bio_length; 209 failprob = sc->sc_wfailprob; 210 delayprob = sc->sc_wdelayprob; 211 delaytime = sc->sc_delaymsec; 212 break; 213 case BIO_DELETE: 214 sc->sc_deletes++; 215 break; 216 case BIO_GETATTR: 217 sc->sc_getattrs++; 218 if (sc->sc_physpath && 219 g_handleattr_str(bp, "GEOM::physpath", sc->sc_physpath)) 220 ; 221 else if (strcmp(bp->bio_attribute, "GEOM::kerneldump") == 0) 222 g_nop_kerneldump(bp, sc); 223 else 224 /* 225 * Fallthrough to forwarding the GETATTR down to the 226 * lower level device. 227 */ 228 break; 229 mtx_unlock(&sc->sc_lock); 230 return; 231 case BIO_FLUSH: 232 sc->sc_flushes++; 233 break; 234 case BIO_CMD0: 235 sc->sc_cmd0s++; 236 break; 237 case BIO_CMD1: 238 sc->sc_cmd1s++; 239 break; 240 case BIO_CMD2: 241 sc->sc_cmd2s++; 242 break; 243 } 244 mtx_unlock(&sc->sc_lock); 245 if (failprob > 0) { 246 u_int rval; 247 248 rval = arc4random() % 100; 249 if (rval < failprob) { 250 G_NOP_LOGREQLVL(1, bp, "Returning error=%d.", sc->sc_error); 251 g_io_deliver(bp, sc->sc_error); 252 return; 253 } 254 } 255 256 cbp = g_clone_bio(bp); 257 if (cbp == NULL) { 258 g_io_deliver(bp, ENOMEM); 259 return; 260 } 261 cbp->bio_done = g_std_done; 262 cbp->bio_offset = bp->bio_offset + sc->sc_offset; 263 pp = LIST_FIRST(&gp->provider); 264 KASSERT(pp != NULL, ("NULL pp")); 265 cbp->bio_to = pp; 266 267 if (delayprob > 0) { 268 struct g_nop_delay *gndelay; 269 u_int rval; 270 271 rval = arc4random() % 100; 272 if (rval < delayprob) { 273 gndelay = g_malloc(sizeof(*gndelay), M_NOWAIT | M_ZERO); 274 if (gndelay != NULL) { 275 callout_init(&gndelay->dl_cal, 1); 276 277 gndelay->dl_bio = cbp; 278 279 mtx_lock(&sc->sc_lock); 280 TAILQ_INSERT_TAIL(&sc->sc_head_delay, gndelay, 281 dl_next); 282 mtx_unlock(&sc->sc_lock); 283 284 callout_reset(&gndelay->dl_cal, 285 MSEC_2_TICKS(delaytime), g_nop_pass_timeout, 286 gndelay); 287 return; 288 } 289 } 290 } 291 292 g_nop_pass(cbp, gp); 293 } 294 295 static int 296 g_nop_access(struct g_provider *pp, int dr, int dw, int de) 297 { 298 struct g_geom *gp; 299 struct g_consumer *cp; 300 int error; 301 302 gp = pp->geom; 303 cp = LIST_FIRST(&gp->consumer); 304 error = g_access(cp, dr, dw, de); 305 306 return (error); 307 } 308 309 static int 310 g_nop_create(struct gctl_req *req, struct g_class *mp, struct g_provider *pp, 311 int ioerror, u_int rfailprob, u_int wfailprob, u_int delaymsec, u_int rdelayprob, 312 u_int wdelayprob, off_t offset, off_t size, u_int secsize, off_t stripesize, 313 off_t stripeoffset, const char *physpath) 314 { 315 struct g_nop_softc *sc; 316 struct g_geom *gp; 317 struct g_provider *newpp; 318 struct g_consumer *cp; 319 char name[64]; 320 int error; 321 off_t explicitsize; 322 323 g_topology_assert(); 324 325 gp = NULL; 326 newpp = NULL; 327 cp = NULL; 328 329 if ((offset % pp->sectorsize) != 0) { 330 gctl_error(req, "Invalid offset for provider %s.", pp->name); 331 return (EINVAL); 332 } 333 if ((size % pp->sectorsize) != 0) { 334 gctl_error(req, "Invalid size for provider %s.", pp->name); 335 return (EINVAL); 336 } 337 if (offset >= pp->mediasize) { 338 gctl_error(req, "Invalid offset for provider %s.", pp->name); 339 return (EINVAL); 340 } 341 explicitsize = size; 342 if (size == 0) 343 size = pp->mediasize - offset; 344 if (offset + size > pp->mediasize) { 345 gctl_error(req, "Invalid size for provider %s.", pp->name); 346 return (EINVAL); 347 } 348 if (secsize == 0) 349 secsize = pp->sectorsize; 350 else if ((secsize % pp->sectorsize) != 0) { 351 gctl_error(req, "Invalid secsize for provider %s.", pp->name); 352 return (EINVAL); 353 } 354 if (secsize > MAXPHYS) { 355 gctl_error(req, "secsize is too big."); 356 return (EINVAL); 357 } 358 size -= size % secsize; 359 if ((stripesize % pp->sectorsize) != 0) { 360 gctl_error(req, "Invalid stripesize for provider %s.", pp->name); 361 return (EINVAL); 362 } 363 if ((stripeoffset % pp->sectorsize) != 0) { 364 gctl_error(req, "Invalid stripeoffset for provider %s.", pp->name); 365 return (EINVAL); 366 } 367 if (stripesize != 0 && stripeoffset >= stripesize) { 368 gctl_error(req, "stripeoffset is too big."); 369 return (EINVAL); 370 } 371 snprintf(name, sizeof(name), "%s%s", pp->name, G_NOP_SUFFIX); 372 LIST_FOREACH(gp, &mp->geom, geom) { 373 if (strcmp(gp->name, name) == 0) { 374 gctl_error(req, "Provider %s already exists.", name); 375 return (EEXIST); 376 } 377 } 378 gp = g_new_geomf(mp, "%s", name); 379 sc = g_malloc(sizeof(*sc), M_WAITOK | M_ZERO); 380 sc->sc_offset = offset; 381 sc->sc_explicitsize = explicitsize; 382 sc->sc_stripesize = stripesize; 383 sc->sc_stripeoffset = stripeoffset; 384 if (physpath && strcmp(physpath, G_NOP_PHYSPATH_PASSTHROUGH)) { 385 sc->sc_physpath = strndup(physpath, MAXPATHLEN, M_GEOM); 386 } else 387 sc->sc_physpath = NULL; 388 sc->sc_error = ioerror; 389 sc->sc_rfailprob = rfailprob; 390 sc->sc_wfailprob = wfailprob; 391 sc->sc_delaymsec = delaymsec; 392 sc->sc_rdelayprob = rdelayprob; 393 sc->sc_wdelayprob = wdelayprob; 394 sc->sc_reads = 0; 395 sc->sc_writes = 0; 396 sc->sc_deletes = 0; 397 sc->sc_getattrs = 0; 398 sc->sc_flushes = 0; 399 sc->sc_cmd0s = 0; 400 sc->sc_cmd1s = 0; 401 sc->sc_cmd2s = 0; 402 sc->sc_readbytes = 0; 403 sc->sc_wrotebytes = 0; 404 TAILQ_INIT(&sc->sc_head_delay); 405 mtx_init(&sc->sc_lock, "gnop lock", NULL, MTX_DEF); 406 gp->softc = sc; 407 408 newpp = g_new_providerf(gp, "%s", gp->name); 409 newpp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE; 410 newpp->mediasize = size; 411 newpp->sectorsize = secsize; 412 newpp->stripesize = stripesize; 413 newpp->stripeoffset = stripeoffset; 414 415 cp = g_new_consumer(gp); 416 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 417 error = g_attach(cp, pp); 418 if (error != 0) { 419 gctl_error(req, "Cannot attach to provider %s.", pp->name); 420 goto fail; 421 } 422 423 newpp->flags |= pp->flags & G_PF_ACCEPT_UNMAPPED; 424 g_error_provider(newpp, 0); 425 G_NOP_DEBUG(0, "Device %s created.", gp->name); 426 return (0); 427 fail: 428 if (cp->provider != NULL) 429 g_detach(cp); 430 g_destroy_consumer(cp); 431 g_destroy_provider(newpp); 432 mtx_destroy(&sc->sc_lock); 433 free(sc->sc_physpath, M_GEOM); 434 g_free(gp->softc); 435 g_destroy_geom(gp); 436 return (error); 437 } 438 439 static void 440 g_nop_providergone(struct g_provider *pp) 441 { 442 struct g_geom *gp = pp->geom; 443 struct g_nop_softc *sc = gp->softc; 444 445 KASSERT(TAILQ_EMPTY(&sc->sc_head_delay), 446 ("delayed request list is not empty")); 447 448 gp->softc = NULL; 449 free(sc->sc_physpath, M_GEOM); 450 mtx_destroy(&sc->sc_lock); 451 g_free(sc); 452 } 453 454 static int 455 g_nop_destroy(struct g_geom *gp, boolean_t force) 456 { 457 struct g_nop_softc *sc; 458 struct g_provider *pp; 459 460 g_topology_assert(); 461 sc = gp->softc; 462 if (sc == NULL) 463 return (ENXIO); 464 pp = LIST_FIRST(&gp->provider); 465 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 466 if (force) { 467 G_NOP_DEBUG(0, "Device %s is still open, so it " 468 "can't be definitely removed.", pp->name); 469 } else { 470 G_NOP_DEBUG(1, "Device %s is still open (r%dw%de%d).", 471 pp->name, pp->acr, pp->acw, pp->ace); 472 return (EBUSY); 473 } 474 } else { 475 G_NOP_DEBUG(0, "Device %s removed.", gp->name); 476 } 477 478 g_wither_geom(gp, ENXIO); 479 480 return (0); 481 } 482 483 static int 484 g_nop_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp) 485 { 486 487 return (g_nop_destroy(gp, 0)); 488 } 489 490 static void 491 g_nop_ctl_create(struct gctl_req *req, struct g_class *mp) 492 { 493 struct g_provider *pp; 494 intmax_t *error, *rfailprob, *wfailprob, *offset, *secsize, *size, 495 *stripesize, *stripeoffset, *delaymsec, *rdelayprob, *wdelayprob; 496 const char *name, *physpath; 497 char param[16]; 498 int i, *nargs; 499 500 g_topology_assert(); 501 502 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 503 if (nargs == NULL) { 504 gctl_error(req, "No '%s' argument", "nargs"); 505 return; 506 } 507 if (*nargs <= 0) { 508 gctl_error(req, "Missing device(s)."); 509 return; 510 } 511 error = gctl_get_paraml(req, "error", sizeof(*error)); 512 if (error == NULL) { 513 gctl_error(req, "No '%s' argument", "error"); 514 return; 515 } 516 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob)); 517 if (rfailprob == NULL) { 518 gctl_error(req, "No '%s' argument", "rfailprob"); 519 return; 520 } 521 if (*rfailprob < -1 || *rfailprob > 100) { 522 gctl_error(req, "Invalid '%s' argument", "rfailprob"); 523 return; 524 } 525 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob)); 526 if (wfailprob == NULL) { 527 gctl_error(req, "No '%s' argument", "wfailprob"); 528 return; 529 } 530 if (*wfailprob < -1 || *wfailprob > 100) { 531 gctl_error(req, "Invalid '%s' argument", "wfailprob"); 532 return; 533 } 534 delaymsec = gctl_get_paraml(req, "delaymsec", sizeof(*delaymsec)); 535 if (delaymsec == NULL) { 536 gctl_error(req, "No '%s' argument", "delaymsec"); 537 return; 538 } 539 if (*delaymsec < 1 && *delaymsec != -1) { 540 gctl_error(req, "Invalid '%s' argument", "delaymsec"); 541 return; 542 } 543 rdelayprob = gctl_get_paraml(req, "rdelayprob", sizeof(*rdelayprob)); 544 if (rdelayprob == NULL) { 545 gctl_error(req, "No '%s' argument", "rdelayprob"); 546 return; 547 } 548 if (*rdelayprob < -1 || *rdelayprob > 100) { 549 gctl_error(req, "Invalid '%s' argument", "rdelayprob"); 550 return; 551 } 552 wdelayprob = gctl_get_paraml(req, "wdelayprob", sizeof(*wdelayprob)); 553 if (wdelayprob == NULL) { 554 gctl_error(req, "No '%s' argument", "wdelayprob"); 555 return; 556 } 557 if (*wdelayprob < -1 || *wdelayprob > 100) { 558 gctl_error(req, "Invalid '%s' argument", "wdelayprob"); 559 return; 560 } 561 offset = gctl_get_paraml(req, "offset", sizeof(*offset)); 562 if (offset == NULL) { 563 gctl_error(req, "No '%s' argument", "offset"); 564 return; 565 } 566 if (*offset < 0) { 567 gctl_error(req, "Invalid '%s' argument", "offset"); 568 return; 569 } 570 size = gctl_get_paraml(req, "size", sizeof(*size)); 571 if (size == NULL) { 572 gctl_error(req, "No '%s' argument", "size"); 573 return; 574 } 575 if (*size < 0) { 576 gctl_error(req, "Invalid '%s' argument", "size"); 577 return; 578 } 579 secsize = gctl_get_paraml(req, "secsize", sizeof(*secsize)); 580 if (secsize == NULL) { 581 gctl_error(req, "No '%s' argument", "secsize"); 582 return; 583 } 584 if (*secsize < 0) { 585 gctl_error(req, "Invalid '%s' argument", "secsize"); 586 return; 587 } 588 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize)); 589 if (stripesize == NULL) { 590 gctl_error(req, "No '%s' argument", "stripesize"); 591 return; 592 } 593 if (*stripesize < 0) { 594 gctl_error(req, "Invalid '%s' argument", "stripesize"); 595 return; 596 } 597 stripeoffset = gctl_get_paraml(req, "stripeoffset", sizeof(*stripeoffset)); 598 if (stripeoffset == NULL) { 599 gctl_error(req, "No '%s' argument", "stripeoffset"); 600 return; 601 } 602 if (*stripeoffset < 0) { 603 gctl_error(req, "Invalid '%s' argument", "stripeoffset"); 604 return; 605 } 606 physpath = gctl_get_asciiparam(req, "physpath"); 607 608 for (i = 0; i < *nargs; i++) { 609 snprintf(param, sizeof(param), "arg%d", i); 610 name = gctl_get_asciiparam(req, param); 611 if (name == NULL) { 612 gctl_error(req, "No 'arg%d' argument", i); 613 return; 614 } 615 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 616 name += strlen("/dev/"); 617 pp = g_provider_by_name(name); 618 if (pp == NULL) { 619 G_NOP_DEBUG(1, "Provider %s is invalid.", name); 620 gctl_error(req, "Provider %s is invalid.", name); 621 return; 622 } 623 if (g_nop_create(req, mp, pp, 624 *error == -1 ? EIO : (int)*error, 625 *rfailprob == -1 ? 0 : (u_int)*rfailprob, 626 *wfailprob == -1 ? 0 : (u_int)*wfailprob, 627 *delaymsec == -1 ? 1 : (u_int)*delaymsec, 628 *rdelayprob == -1 ? 0 : (u_int)*rdelayprob, 629 *wdelayprob == -1 ? 0 : (u_int)*wdelayprob, 630 (off_t)*offset, (off_t)*size, (u_int)*secsize, 631 (off_t)*stripesize, (off_t)*stripeoffset, 632 physpath) != 0) { 633 return; 634 } 635 } 636 } 637 638 static void 639 g_nop_ctl_configure(struct gctl_req *req, struct g_class *mp) 640 { 641 struct g_nop_softc *sc; 642 struct g_provider *pp; 643 intmax_t *delaymsec, *error, *rdelayprob, *rfailprob, *wdelayprob, *wfailprob; 644 const char *name; 645 char param[16]; 646 int i, *nargs; 647 648 g_topology_assert(); 649 650 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 651 if (nargs == NULL) { 652 gctl_error(req, "No '%s' argument", "nargs"); 653 return; 654 } 655 if (*nargs <= 0) { 656 gctl_error(req, "Missing device(s)."); 657 return; 658 } 659 error = gctl_get_paraml(req, "error", sizeof(*error)); 660 if (error == NULL) { 661 gctl_error(req, "No '%s' argument", "error"); 662 return; 663 } 664 rfailprob = gctl_get_paraml(req, "rfailprob", sizeof(*rfailprob)); 665 if (rfailprob == NULL) { 666 gctl_error(req, "No '%s' argument", "rfailprob"); 667 return; 668 } 669 if (*rfailprob < -1 || *rfailprob > 100) { 670 gctl_error(req, "Invalid '%s' argument", "rfailprob"); 671 return; 672 } 673 wfailprob = gctl_get_paraml(req, "wfailprob", sizeof(*wfailprob)); 674 if (wfailprob == NULL) { 675 gctl_error(req, "No '%s' argument", "wfailprob"); 676 return; 677 } 678 if (*wfailprob < -1 || *wfailprob > 100) { 679 gctl_error(req, "Invalid '%s' argument", "wfailprob"); 680 return; 681 } 682 683 delaymsec = gctl_get_paraml(req, "delaymsec", sizeof(*delaymsec)); 684 if (delaymsec == NULL) { 685 gctl_error(req, "No '%s' argument", "delaymsec"); 686 return; 687 } 688 if (*delaymsec < 1 && *delaymsec != -1) { 689 gctl_error(req, "Invalid '%s' argument", "delaymsec"); 690 return; 691 } 692 rdelayprob = gctl_get_paraml(req, "rdelayprob", sizeof(*rdelayprob)); 693 if (rdelayprob == NULL) { 694 gctl_error(req, "No '%s' argument", "rdelayprob"); 695 return; 696 } 697 if (*rdelayprob < -1 || *rdelayprob > 100) { 698 gctl_error(req, "Invalid '%s' argument", "rdelayprob"); 699 return; 700 } 701 wdelayprob = gctl_get_paraml(req, "wdelayprob", sizeof(*wdelayprob)); 702 if (wdelayprob == NULL) { 703 gctl_error(req, "No '%s' argument", "wdelayprob"); 704 return; 705 } 706 if (*wdelayprob < -1 || *wdelayprob > 100) { 707 gctl_error(req, "Invalid '%s' argument", "wdelayprob"); 708 return; 709 } 710 711 for (i = 0; i < *nargs; i++) { 712 snprintf(param, sizeof(param), "arg%d", i); 713 name = gctl_get_asciiparam(req, param); 714 if (name == NULL) { 715 gctl_error(req, "No 'arg%d' argument", i); 716 return; 717 } 718 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 719 name += strlen("/dev/"); 720 pp = g_provider_by_name(name); 721 if (pp == NULL || pp->geom->class != mp) { 722 G_NOP_DEBUG(1, "Provider %s is invalid.", name); 723 gctl_error(req, "Provider %s is invalid.", name); 724 return; 725 } 726 sc = pp->geom->softc; 727 if (*error != -1) 728 sc->sc_error = (int)*error; 729 if (*rfailprob != -1) 730 sc->sc_rfailprob = (u_int)*rfailprob; 731 if (*wfailprob != -1) 732 sc->sc_wfailprob = (u_int)*wfailprob; 733 if (*rdelayprob != -1) 734 sc->sc_rdelayprob = (u_int)*rdelayprob; 735 if (*wdelayprob != -1) 736 sc->sc_wdelayprob = (u_int)*wdelayprob; 737 if (*delaymsec != -1) 738 sc->sc_delaymsec = (u_int)*delaymsec; 739 } 740 } 741 742 static struct g_geom * 743 g_nop_find_geom(struct g_class *mp, const char *name) 744 { 745 struct g_geom *gp; 746 747 LIST_FOREACH(gp, &mp->geom, geom) { 748 if (strcmp(gp->name, name) == 0) 749 return (gp); 750 } 751 return (NULL); 752 } 753 754 static void 755 g_nop_ctl_destroy(struct gctl_req *req, struct g_class *mp) 756 { 757 int *nargs, *force, error, i; 758 struct g_geom *gp; 759 const char *name; 760 char param[16]; 761 762 g_topology_assert(); 763 764 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 765 if (nargs == NULL) { 766 gctl_error(req, "No '%s' argument", "nargs"); 767 return; 768 } 769 if (*nargs <= 0) { 770 gctl_error(req, "Missing device(s)."); 771 return; 772 } 773 force = gctl_get_paraml(req, "force", sizeof(*force)); 774 if (force == NULL) { 775 gctl_error(req, "No 'force' argument"); 776 return; 777 } 778 779 for (i = 0; i < *nargs; i++) { 780 snprintf(param, sizeof(param), "arg%d", i); 781 name = gctl_get_asciiparam(req, param); 782 if (name == NULL) { 783 gctl_error(req, "No 'arg%d' argument", i); 784 return; 785 } 786 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 787 name += strlen("/dev/"); 788 gp = g_nop_find_geom(mp, name); 789 if (gp == NULL) { 790 G_NOP_DEBUG(1, "Device %s is invalid.", name); 791 gctl_error(req, "Device %s is invalid.", name); 792 return; 793 } 794 error = g_nop_destroy(gp, *force); 795 if (error != 0) { 796 gctl_error(req, "Cannot destroy device %s (error=%d).", 797 gp->name, error); 798 return; 799 } 800 } 801 } 802 803 static void 804 g_nop_ctl_reset(struct gctl_req *req, struct g_class *mp) 805 { 806 struct g_nop_softc *sc; 807 struct g_provider *pp; 808 const char *name; 809 char param[16]; 810 int i, *nargs; 811 812 g_topology_assert(); 813 814 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 815 if (nargs == NULL) { 816 gctl_error(req, "No '%s' argument", "nargs"); 817 return; 818 } 819 if (*nargs <= 0) { 820 gctl_error(req, "Missing device(s)."); 821 return; 822 } 823 824 for (i = 0; i < *nargs; i++) { 825 snprintf(param, sizeof(param), "arg%d", i); 826 name = gctl_get_asciiparam(req, param); 827 if (name == NULL) { 828 gctl_error(req, "No 'arg%d' argument", i); 829 return; 830 } 831 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 832 name += strlen("/dev/"); 833 pp = g_provider_by_name(name); 834 if (pp == NULL || pp->geom->class != mp) { 835 G_NOP_DEBUG(1, "Provider %s is invalid.", name); 836 gctl_error(req, "Provider %s is invalid.", name); 837 return; 838 } 839 sc = pp->geom->softc; 840 sc->sc_reads = 0; 841 sc->sc_writes = 0; 842 sc->sc_deletes = 0; 843 sc->sc_getattrs = 0; 844 sc->sc_flushes = 0; 845 sc->sc_cmd0s = 0; 846 sc->sc_cmd1s = 0; 847 sc->sc_cmd2s = 0; 848 sc->sc_readbytes = 0; 849 sc->sc_wrotebytes = 0; 850 } 851 } 852 853 static void 854 g_nop_config(struct gctl_req *req, struct g_class *mp, const char *verb) 855 { 856 uint32_t *version; 857 858 g_topology_assert(); 859 860 version = gctl_get_paraml(req, "version", sizeof(*version)); 861 if (version == NULL) { 862 gctl_error(req, "No '%s' argument.", "version"); 863 return; 864 } 865 if (*version != G_NOP_VERSION) { 866 gctl_error(req, "Userland and kernel parts are out of sync."); 867 return; 868 } 869 870 if (strcmp(verb, "create") == 0) { 871 g_nop_ctl_create(req, mp); 872 return; 873 } else if (strcmp(verb, "configure") == 0) { 874 g_nop_ctl_configure(req, mp); 875 return; 876 } else if (strcmp(verb, "destroy") == 0) { 877 g_nop_ctl_destroy(req, mp); 878 return; 879 } else if (strcmp(verb, "reset") == 0) { 880 g_nop_ctl_reset(req, mp); 881 return; 882 } 883 884 gctl_error(req, "Unknown verb."); 885 } 886 887 static void 888 g_nop_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 889 struct g_consumer *cp, struct g_provider *pp) 890 { 891 struct g_nop_softc *sc; 892 893 if (pp != NULL || cp != NULL) 894 return; 895 sc = gp->softc; 896 sbuf_printf(sb, "%s<Offset>%jd</Offset>\n", indent, 897 (intmax_t)sc->sc_offset); 898 sbuf_printf(sb, "%s<ReadFailProb>%u</ReadFailProb>\n", indent, 899 sc->sc_rfailprob); 900 sbuf_printf(sb, "%s<WriteFailProb>%u</WriteFailProb>\n", indent, 901 sc->sc_wfailprob); 902 sbuf_printf(sb, "%s<ReadDelayedProb>%u</ReadDelayedProb>\n", indent, 903 sc->sc_rdelayprob); 904 sbuf_printf(sb, "%s<WriteDelayedProb>%u</WriteDelayedProb>\n", indent, 905 sc->sc_wdelayprob); 906 sbuf_printf(sb, "%s<Delay>%d</Delay>\n", indent, sc->sc_delaymsec); 907 sbuf_printf(sb, "%s<Error>%d</Error>\n", indent, sc->sc_error); 908 sbuf_printf(sb, "%s<Reads>%ju</Reads>\n", indent, sc->sc_reads); 909 sbuf_printf(sb, "%s<Writes>%ju</Writes>\n", indent, sc->sc_writes); 910 sbuf_printf(sb, "%s<Deletes>%ju</Deletes>\n", indent, sc->sc_deletes); 911 sbuf_printf(sb, "%s<Getattrs>%ju</Getattrs>\n", indent, sc->sc_getattrs); 912 sbuf_printf(sb, "%s<Flushes>%ju</Flushes>\n", indent, sc->sc_flushes); 913 sbuf_printf(sb, "%s<Cmd0s>%ju</Cmd0s>\n", indent, sc->sc_cmd0s); 914 sbuf_printf(sb, "%s<Cmd1s>%ju</Cmd1s>\n", indent, sc->sc_cmd1s); 915 sbuf_printf(sb, "%s<Cmd2s>%ju</Cmd2s>\n", indent, sc->sc_cmd2s); 916 sbuf_printf(sb, "%s<ReadBytes>%ju</ReadBytes>\n", indent, 917 sc->sc_readbytes); 918 sbuf_printf(sb, "%s<WroteBytes>%ju</WroteBytes>\n", indent, 919 sc->sc_wrotebytes); 920 } 921 922 DECLARE_GEOM_CLASS(g_nop_class, g_nop); 923 MODULE_VERSION(geom_nop, 0); 924