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