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