1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2004, 2007 Lukas Ertl 5 * Copyright (c) 2007, 2009 Ulf Lilleengen 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 AUTHOR 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 AUTHOR 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 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/bio.h> 36 #include <sys/kernel.h> 37 #include <sys/kthread.h> 38 #include <sys/lock.h> 39 #include <sys/malloc.h> 40 #include <sys/module.h> 41 #include <sys/mutex.h> 42 #include <sys/sbuf.h> 43 #include <sys/sysctl.h> 44 #include <sys/systm.h> 45 46 #include <geom/geom.h> 47 #include <geom/geom_dbg.h> 48 #include <geom/vinum/geom_vinum_var.h> 49 #include <geom/vinum/geom_vinum.h> 50 #include <geom/vinum/geom_vinum_raid5.h> 51 52 SYSCTL_DECL(_kern_geom); 53 static SYSCTL_NODE(_kern_geom, OID_AUTO, vinum, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 54 "GEOM_VINUM stuff"); 55 u_int g_vinum_debug = 0; 56 SYSCTL_UINT(_kern_geom_vinum, OID_AUTO, debug, CTLFLAG_RWTUN, &g_vinum_debug, 0, 57 "Debug level"); 58 59 static int gv_create(struct g_geom *, struct gctl_req *); 60 static void gv_attach(struct gv_softc *, struct gctl_req *); 61 static void gv_detach(struct gv_softc *, struct gctl_req *); 62 static void gv_parityop(struct gv_softc *, struct gctl_req *); 63 64 static void 65 gv_orphan(struct g_consumer *cp) 66 { 67 struct g_geom *gp; 68 struct gv_softc *sc; 69 struct gv_drive *d; 70 71 g_topology_assert(); 72 73 KASSERT(cp != NULL, ("gv_orphan: null cp")); 74 gp = cp->geom; 75 KASSERT(gp != NULL, ("gv_orphan: null gp")); 76 sc = gp->softc; 77 KASSERT(sc != NULL, ("gv_orphan: null sc")); 78 d = cp->private; 79 KASSERT(d != NULL, ("gv_orphan: null d")); 80 81 g_trace(G_T_TOPOLOGY, "gv_orphan(%s)", gp->name); 82 83 gv_post_event(sc, GV_EVENT_DRIVE_LOST, d, NULL, 0, 0); 84 } 85 86 void 87 gv_start(struct bio *bp) 88 { 89 struct g_geom *gp; 90 struct gv_softc *sc; 91 92 gp = bp->bio_to->geom; 93 sc = gp->softc; 94 95 switch (bp->bio_cmd) { 96 case BIO_READ: 97 case BIO_WRITE: 98 case BIO_DELETE: 99 break; 100 case BIO_GETATTR: 101 default: 102 g_io_deliver(bp, EOPNOTSUPP); 103 return; 104 } 105 mtx_lock(&sc->bqueue_mtx); 106 bioq_disksort(sc->bqueue_down, bp); 107 wakeup(sc); 108 mtx_unlock(&sc->bqueue_mtx); 109 } 110 111 void 112 gv_done(struct bio *bp) 113 { 114 struct g_geom *gp; 115 struct gv_softc *sc; 116 117 KASSERT(bp != NULL, ("NULL bp")); 118 119 gp = bp->bio_from->geom; 120 sc = gp->softc; 121 122 mtx_lock(&sc->bqueue_mtx); 123 bioq_disksort(sc->bqueue_up, bp); 124 wakeup(sc); 125 mtx_unlock(&sc->bqueue_mtx); 126 } 127 128 int 129 gv_access(struct g_provider *pp, int dr, int dw, int de) 130 { 131 struct g_geom *gp; 132 struct gv_softc *sc; 133 struct gv_drive *d, *d2; 134 int error; 135 136 gp = pp->geom; 137 sc = gp->softc; 138 /* 139 * We want to modify the read count with the write count in case we have 140 * plexes in a RAID-5 organization. 141 */ 142 dr += dw; 143 144 LIST_FOREACH(d, &sc->drives, drive) { 145 if (d->consumer == NULL) 146 continue; 147 error = g_access(d->consumer, dr, dw, de); 148 if (error) { 149 LIST_FOREACH(d2, &sc->drives, drive) { 150 if (d == d2) 151 break; 152 g_access(d2->consumer, -dr, -dw, -de); 153 } 154 G_VINUM_DEBUG(0, "g_access '%s' failed: %d", d->name, 155 error); 156 return (error); 157 } 158 } 159 return (0); 160 } 161 162 static void 163 gv_init(struct g_class *mp) 164 { 165 struct g_geom *gp; 166 struct gv_softc *sc; 167 168 g_trace(G_T_TOPOLOGY, "gv_init(%p)", mp); 169 170 gp = g_new_geomf(mp, "VINUM"); 171 gp->spoiled = gv_orphan; 172 gp->orphan = gv_orphan; 173 gp->access = gv_access; 174 gp->start = gv_start; 175 gp->softc = g_malloc(sizeof(struct gv_softc), M_WAITOK | M_ZERO); 176 sc = gp->softc; 177 sc->geom = gp; 178 sc->bqueue_down = g_malloc(sizeof(struct bio_queue_head), 179 M_WAITOK | M_ZERO); 180 sc->bqueue_up = g_malloc(sizeof(struct bio_queue_head), 181 M_WAITOK | M_ZERO); 182 bioq_init(sc->bqueue_down); 183 bioq_init(sc->bqueue_up); 184 LIST_INIT(&sc->drives); 185 LIST_INIT(&sc->subdisks); 186 LIST_INIT(&sc->plexes); 187 LIST_INIT(&sc->volumes); 188 TAILQ_INIT(&sc->equeue); 189 mtx_init(&sc->config_mtx, "gv_config", NULL, MTX_DEF); 190 mtx_init(&sc->equeue_mtx, "gv_equeue", NULL, MTX_DEF); 191 mtx_init(&sc->bqueue_mtx, "gv_bqueue", NULL, MTX_DEF); 192 kproc_create(gv_worker, sc, &sc->worker, 0, 0, "gv_worker"); 193 } 194 195 static int 196 gv_unload(struct gctl_req *req, struct g_class *mp, struct g_geom *gp) 197 { 198 struct gv_softc *sc; 199 200 g_trace(G_T_TOPOLOGY, "gv_unload(%p)", mp); 201 202 g_topology_assert(); 203 sc = gp->softc; 204 205 if (sc != NULL) { 206 gv_worker_exit(sc); 207 gp->softc = NULL; 208 g_wither_geom(gp, ENXIO); 209 } 210 211 return (0); 212 } 213 214 /* Handle userland request of attaching object. */ 215 static void 216 gv_attach(struct gv_softc *sc, struct gctl_req *req) 217 { 218 struct gv_volume *v; 219 struct gv_plex *p; 220 struct gv_sd *s; 221 off_t *offset; 222 int *rename, type_child, type_parent; 223 char *child, *parent; 224 225 child = gctl_get_param(req, "child", NULL); 226 if (child == NULL) { 227 gctl_error(req, "no child given"); 228 return; 229 } 230 parent = gctl_get_param(req, "parent", NULL); 231 if (parent == NULL) { 232 gctl_error(req, "no parent given"); 233 return; 234 } 235 offset = gctl_get_paraml(req, "offset", sizeof(*offset)); 236 if (offset == NULL) { 237 gctl_error(req, "no offset given"); 238 return; 239 } 240 rename = gctl_get_paraml(req, "rename", sizeof(*rename)); 241 if (rename == NULL) { 242 gctl_error(req, "no rename flag given"); 243 return; 244 } 245 246 type_child = gv_object_type(sc, child); 247 type_parent = gv_object_type(sc, parent); 248 249 switch (type_child) { 250 case GV_TYPE_PLEX: 251 if (type_parent != GV_TYPE_VOL) { 252 gctl_error(req, "no such volume to attach to"); 253 return; 254 } 255 v = gv_find_vol(sc, parent); 256 p = gv_find_plex(sc, child); 257 gv_post_event(sc, GV_EVENT_ATTACH_PLEX, p, v, *offset, *rename); 258 break; 259 case GV_TYPE_SD: 260 if (type_parent != GV_TYPE_PLEX) { 261 gctl_error(req, "no such plex to attach to"); 262 return; 263 } 264 p = gv_find_plex(sc, parent); 265 s = gv_find_sd(sc, child); 266 gv_post_event(sc, GV_EVENT_ATTACH_SD, s, p, *offset, *rename); 267 break; 268 default: 269 gctl_error(req, "invalid child type"); 270 break; 271 } 272 } 273 274 /* Handle userland request of detaching object. */ 275 static void 276 gv_detach(struct gv_softc *sc, struct gctl_req *req) 277 { 278 struct gv_plex *p; 279 struct gv_sd *s; 280 int *flags, type; 281 char *object; 282 283 object = gctl_get_param(req, "object", NULL); 284 if (object == NULL) { 285 gctl_error(req, "no argument given"); 286 return; 287 } 288 289 flags = gctl_get_paraml(req, "flags", sizeof(*flags)); 290 type = gv_object_type(sc, object); 291 switch (type) { 292 case GV_TYPE_PLEX: 293 p = gv_find_plex(sc, object); 294 gv_post_event(sc, GV_EVENT_DETACH_PLEX, p, NULL, *flags, 0); 295 break; 296 case GV_TYPE_SD: 297 s = gv_find_sd(sc, object); 298 gv_post_event(sc, GV_EVENT_DETACH_SD, s, NULL, *flags, 0); 299 break; 300 default: 301 gctl_error(req, "invalid object type"); 302 break; 303 } 304 } 305 306 /* Handle userland requests for creating new objects. */ 307 static int 308 gv_create(struct g_geom *gp, struct gctl_req *req) 309 { 310 struct gv_softc *sc; 311 struct gv_drive *d, *d2; 312 struct gv_plex *p, *p2; 313 struct gv_sd *s, *s2; 314 struct gv_volume *v, *v2; 315 struct g_provider *pp; 316 int i, *drives, *flags, *plexes, *subdisks, *volumes; 317 char buf[20]; 318 319 g_topology_assert(); 320 321 sc = gp->softc; 322 323 /* Find out how many of each object have been passed in. */ 324 volumes = gctl_get_paraml(req, "volumes", sizeof(*volumes)); 325 plexes = gctl_get_paraml(req, "plexes", sizeof(*plexes)); 326 subdisks = gctl_get_paraml(req, "subdisks", sizeof(*subdisks)); 327 drives = gctl_get_paraml(req, "drives", sizeof(*drives)); 328 if (volumes == NULL || plexes == NULL || subdisks == NULL || 329 drives == NULL) { 330 gctl_error(req, "number of objects not given"); 331 return (-1); 332 } 333 flags = gctl_get_paraml(req, "flags", sizeof(*flags)); 334 if (flags == NULL) { 335 gctl_error(req, "flags not given"); 336 return (-1); 337 } 338 339 /* First, handle drive definitions ... */ 340 for (i = 0; i < *drives; i++) { 341 snprintf(buf, sizeof(buf), "drive%d", i); 342 d2 = gctl_get_paraml(req, buf, sizeof(*d2)); 343 if (d2 == NULL) { 344 gctl_error(req, "no drive definition given"); 345 return (-1); 346 } 347 /* 348 * Make sure that the device specified in the drive config is 349 * an active GEOM provider. 350 */ 351 pp = g_provider_by_name(d2->device); 352 if (pp == NULL) { 353 gctl_error(req, "%s: device not found", d2->device); 354 goto error; 355 } 356 if (gv_find_drive(sc, d2->name) != NULL) { 357 /* Ignore error. */ 358 if (*flags & GV_FLAG_F) 359 continue; 360 gctl_error(req, "drive '%s' already exists", d2->name); 361 goto error; 362 } 363 if (gv_find_drive_device(sc, d2->device) != NULL) { 364 gctl_error(req, "device '%s' already configured in " 365 "gvinum", d2->device); 366 goto error; 367 } 368 369 d = g_malloc(sizeof(*d), M_WAITOK | M_ZERO); 370 bcopy(d2, d, sizeof(*d)); 371 372 gv_post_event(sc, GV_EVENT_CREATE_DRIVE, d, NULL, 0, 0); 373 } 374 375 /* ... then volume definitions ... */ 376 for (i = 0; i < *volumes; i++) { 377 snprintf(buf, sizeof(buf), "volume%d", i); 378 v2 = gctl_get_paraml(req, buf, sizeof(*v2)); 379 if (v2 == NULL) { 380 gctl_error(req, "no volume definition given"); 381 return (-1); 382 } 383 if (gv_find_vol(sc, v2->name) != NULL) { 384 /* Ignore error. */ 385 if (*flags & GV_FLAG_F) 386 continue; 387 gctl_error(req, "volume '%s' already exists", v2->name); 388 goto error; 389 } 390 391 v = g_malloc(sizeof(*v), M_WAITOK | M_ZERO); 392 bcopy(v2, v, sizeof(*v)); 393 394 gv_post_event(sc, GV_EVENT_CREATE_VOLUME, v, NULL, 0, 0); 395 } 396 397 /* ... then plex definitions ... */ 398 for (i = 0; i < *plexes; i++) { 399 snprintf(buf, sizeof(buf), "plex%d", i); 400 p2 = gctl_get_paraml(req, buf, sizeof(*p2)); 401 if (p2 == NULL) { 402 gctl_error(req, "no plex definition given"); 403 return (-1); 404 } 405 if (gv_find_plex(sc, p2->name) != NULL) { 406 /* Ignore error. */ 407 if (*flags & GV_FLAG_F) 408 continue; 409 gctl_error(req, "plex '%s' already exists", p2->name); 410 goto error; 411 } 412 413 p = g_malloc(sizeof(*p), M_WAITOK | M_ZERO); 414 bcopy(p2, p, sizeof(*p)); 415 416 gv_post_event(sc, GV_EVENT_CREATE_PLEX, p, NULL, 0, 0); 417 } 418 419 /* ... and, finally, subdisk definitions. */ 420 for (i = 0; i < *subdisks; i++) { 421 snprintf(buf, sizeof(buf), "sd%d", i); 422 s2 = gctl_get_paraml(req, buf, sizeof(*s2)); 423 if (s2 == NULL) { 424 gctl_error(req, "no subdisk definition given"); 425 return (-1); 426 } 427 if (gv_find_sd(sc, s2->name) != NULL) { 428 /* Ignore error. */ 429 if (*flags & GV_FLAG_F) 430 continue; 431 gctl_error(req, "sd '%s' already exists", s2->name); 432 goto error; 433 } 434 435 s = g_malloc(sizeof(*s), M_WAITOK | M_ZERO); 436 bcopy(s2, s, sizeof(*s)); 437 438 gv_post_event(sc, GV_EVENT_CREATE_SD, s, NULL, 0, 0); 439 } 440 441 error: 442 gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc, NULL, 0, 0); 443 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0); 444 445 return (0); 446 } 447 448 static void 449 gv_config(struct gctl_req *req, struct g_class *mp, char const *verb) 450 { 451 struct g_geom *gp; 452 struct gv_softc *sc; 453 struct sbuf *sb; 454 char *comment; 455 456 g_topology_assert(); 457 458 gp = LIST_FIRST(&mp->geom); 459 sc = gp->softc; 460 461 if (!strcmp(verb, "attach")) { 462 gv_attach(sc, req); 463 464 } else if (!strcmp(verb, "concat")) { 465 gv_concat(gp, req); 466 467 } else if (!strcmp(verb, "detach")) { 468 gv_detach(sc, req); 469 470 } else if (!strcmp(verb, "list")) { 471 gv_list(gp, req); 472 473 /* Save our configuration back to disk. */ 474 } else if (!strcmp(verb, "saveconfig")) { 475 gv_post_event(sc, GV_EVENT_SAVE_CONFIG, sc, NULL, 0, 0); 476 477 /* Return configuration in string form. */ 478 } else if (!strcmp(verb, "getconfig")) { 479 comment = gctl_get_param(req, "comment", NULL); 480 if (comment == NULL) { 481 gctl_error(req, "no comment parameter given"); 482 return; 483 } 484 sb = sbuf_new(NULL, NULL, GV_CFG_LEN, SBUF_FIXEDLEN); 485 gv_format_config(sc, sb, 0, comment); 486 sbuf_finish(sb); 487 gctl_set_param(req, "config", sbuf_data(sb), sbuf_len(sb) + 1); 488 sbuf_delete(sb); 489 490 } else if (!strcmp(verb, "create")) { 491 gv_create(gp, req); 492 493 } else if (!strcmp(verb, "mirror")) { 494 gv_mirror(gp, req); 495 496 } else if (!strcmp(verb, "move")) { 497 gv_move(gp, req); 498 499 } else if (!strcmp(verb, "raid5")) { 500 gv_raid5(gp, req); 501 502 } else if (!strcmp(verb, "rebuildparity") || 503 !strcmp(verb, "checkparity")) { 504 gv_parityop(sc, req); 505 506 } else if (!strcmp(verb, "remove")) { 507 gv_remove(gp, req); 508 509 } else if (!strcmp(verb, "rename")) { 510 gv_rename(gp, req); 511 512 } else if (!strcmp(verb, "resetconfig")) { 513 gv_post_event(sc, GV_EVENT_RESET_CONFIG, sc, NULL, 0, 0); 514 515 } else if (!strcmp(verb, "start")) { 516 gv_start_obj(gp, req); 517 518 } else if (!strcmp(verb, "stripe")) { 519 gv_stripe(gp, req); 520 521 } else if (!strcmp(verb, "setstate")) { 522 gv_setstate(gp, req); 523 } else 524 gctl_error(req, "Unknown verb parameter"); 525 } 526 527 static void 528 gv_parityop(struct gv_softc *sc, struct gctl_req *req) 529 { 530 struct gv_plex *p; 531 int *flags, *rebuild, type; 532 char *plex; 533 534 plex = gctl_get_param(req, "plex", NULL); 535 if (plex == NULL) { 536 gctl_error(req, "no plex given"); 537 return; 538 } 539 540 flags = gctl_get_paraml(req, "flags", sizeof(*flags)); 541 if (flags == NULL) { 542 gctl_error(req, "no flags given"); 543 return; 544 } 545 546 rebuild = gctl_get_paraml(req, "rebuild", sizeof(*rebuild)); 547 if (rebuild == NULL) { 548 gctl_error(req, "no operation given"); 549 return; 550 } 551 552 type = gv_object_type(sc, plex); 553 if (type != GV_TYPE_PLEX) { 554 gctl_error(req, "'%s' is not a plex", plex); 555 return; 556 } 557 p = gv_find_plex(sc, plex); 558 559 if (p->state != GV_PLEX_UP) { 560 gctl_error(req, "plex %s is not completely accessible", 561 p->name); 562 return; 563 } 564 565 if (p->org != GV_PLEX_RAID5) { 566 gctl_error(req, "plex %s is not a RAID5 plex", p->name); 567 return; 568 } 569 570 /* Put it in the event queue. */ 571 /* XXX: The state of the plex might have changed when this event is 572 * picked up ... We should perhaps check this afterwards. */ 573 if (*rebuild) 574 gv_post_event(sc, GV_EVENT_PARITY_REBUILD, p, NULL, 0, 0); 575 else 576 gv_post_event(sc, GV_EVENT_PARITY_CHECK, p, NULL, 0, 0); 577 } 578 579 static struct g_geom * 580 gv_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 581 { 582 struct g_geom *gp; 583 struct g_consumer *cp; 584 struct gv_softc *sc; 585 struct gv_hdr vhdr; 586 int error; 587 588 g_topology_assert(); 589 g_trace(G_T_TOPOLOGY, "gv_taste(%s, %s)", mp->name, pp->name); 590 591 gp = LIST_FIRST(&mp->geom); 592 if (gp == NULL) { 593 G_VINUM_DEBUG(0, "error: tasting, but not initialized?"); 594 return (NULL); 595 } 596 sc = gp->softc; 597 598 cp = g_new_consumer(gp); 599 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 600 if (g_attach(cp, pp) != 0) { 601 g_destroy_consumer(cp); 602 return (NULL); 603 } 604 if (g_access(cp, 1, 0, 0) != 0) { 605 g_detach(cp); 606 g_destroy_consumer(cp); 607 return (NULL); 608 } 609 g_topology_unlock(); 610 611 error = gv_read_header(cp, &vhdr); 612 613 g_topology_lock(); 614 g_access(cp, -1, 0, 0); 615 g_detach(cp); 616 g_destroy_consumer(cp); 617 618 /* Check if what we've been given is a valid vinum drive. */ 619 if (!error) 620 gv_post_event(sc, GV_EVENT_DRIVE_TASTED, pp, NULL, 0, 0); 621 622 return (NULL); 623 } 624 625 void 626 gv_worker(void *arg) 627 { 628 struct g_provider *pp; 629 struct gv_softc *sc; 630 struct gv_event *ev; 631 struct gv_volume *v; 632 struct gv_plex *p; 633 struct gv_sd *s; 634 struct gv_drive *d; 635 struct bio *bp; 636 int newstate, flags, err, rename; 637 char *newname; 638 off_t offset; 639 640 sc = arg; 641 KASSERT(sc != NULL, ("NULL sc")); 642 for (;;) { 643 /* Look at the events first... */ 644 ev = gv_get_event(sc); 645 if (ev != NULL) { 646 gv_remove_event(sc, ev); 647 648 switch (ev->type) { 649 case GV_EVENT_DRIVE_TASTED: 650 G_VINUM_DEBUG(2, "event 'drive tasted'"); 651 pp = ev->arg1; 652 gv_drive_tasted(sc, pp); 653 break; 654 655 case GV_EVENT_DRIVE_LOST: 656 G_VINUM_DEBUG(2, "event 'drive lost'"); 657 d = ev->arg1; 658 gv_drive_lost(sc, d); 659 break; 660 661 case GV_EVENT_CREATE_DRIVE: 662 G_VINUM_DEBUG(2, "event 'create drive'"); 663 d = ev->arg1; 664 gv_create_drive(sc, d); 665 break; 666 667 case GV_EVENT_CREATE_VOLUME: 668 G_VINUM_DEBUG(2, "event 'create volume'"); 669 v = ev->arg1; 670 gv_create_volume(sc, v); 671 break; 672 673 case GV_EVENT_CREATE_PLEX: 674 G_VINUM_DEBUG(2, "event 'create plex'"); 675 p = ev->arg1; 676 gv_create_plex(sc, p); 677 break; 678 679 case GV_EVENT_CREATE_SD: 680 G_VINUM_DEBUG(2, "event 'create sd'"); 681 s = ev->arg1; 682 gv_create_sd(sc, s); 683 break; 684 685 case GV_EVENT_RM_DRIVE: 686 G_VINUM_DEBUG(2, "event 'remove drive'"); 687 d = ev->arg1; 688 flags = ev->arg3; 689 gv_rm_drive(sc, d, flags); 690 /*gv_setup_objects(sc);*/ 691 break; 692 693 case GV_EVENT_RM_VOLUME: 694 G_VINUM_DEBUG(2, "event 'remove volume'"); 695 v = ev->arg1; 696 gv_rm_vol(sc, v); 697 /*gv_setup_objects(sc);*/ 698 break; 699 700 case GV_EVENT_RM_PLEX: 701 G_VINUM_DEBUG(2, "event 'remove plex'"); 702 p = ev->arg1; 703 gv_rm_plex(sc, p); 704 /*gv_setup_objects(sc);*/ 705 break; 706 707 case GV_EVENT_RM_SD: 708 G_VINUM_DEBUG(2, "event 'remove sd'"); 709 s = ev->arg1; 710 gv_rm_sd(sc, s); 711 /*gv_setup_objects(sc);*/ 712 break; 713 714 case GV_EVENT_SAVE_CONFIG: 715 G_VINUM_DEBUG(2, "event 'save config'"); 716 gv_save_config(sc); 717 break; 718 719 case GV_EVENT_SET_SD_STATE: 720 G_VINUM_DEBUG(2, "event 'setstate sd'"); 721 s = ev->arg1; 722 newstate = ev->arg3; 723 flags = ev->arg4; 724 err = gv_set_sd_state(s, newstate, flags); 725 if (err) 726 G_VINUM_DEBUG(0, "error setting subdisk" 727 " state: error code %d", err); 728 break; 729 730 case GV_EVENT_SET_DRIVE_STATE: 731 G_VINUM_DEBUG(2, "event 'setstate drive'"); 732 d = ev->arg1; 733 newstate = ev->arg3; 734 flags = ev->arg4; 735 err = gv_set_drive_state(d, newstate, flags); 736 if (err) 737 G_VINUM_DEBUG(0, "error setting drive " 738 "state: error code %d", err); 739 break; 740 741 case GV_EVENT_SET_VOL_STATE: 742 G_VINUM_DEBUG(2, "event 'setstate volume'"); 743 v = ev->arg1; 744 newstate = ev->arg3; 745 flags = ev->arg4; 746 err = gv_set_vol_state(v, newstate, flags); 747 if (err) 748 G_VINUM_DEBUG(0, "error setting volume " 749 "state: error code %d", err); 750 break; 751 752 case GV_EVENT_SET_PLEX_STATE: 753 G_VINUM_DEBUG(2, "event 'setstate plex'"); 754 p = ev->arg1; 755 newstate = ev->arg3; 756 flags = ev->arg4; 757 err = gv_set_plex_state(p, newstate, flags); 758 if (err) 759 G_VINUM_DEBUG(0, "error setting plex " 760 "state: error code %d", err); 761 break; 762 763 case GV_EVENT_SETUP_OBJECTS: 764 G_VINUM_DEBUG(2, "event 'setup objects'"); 765 gv_setup_objects(sc); 766 break; 767 768 case GV_EVENT_RESET_CONFIG: 769 G_VINUM_DEBUG(2, "event 'resetconfig'"); 770 err = gv_resetconfig(sc); 771 if (err) 772 G_VINUM_DEBUG(0, "error resetting " 773 "config: error code %d", err); 774 break; 775 776 case GV_EVENT_PARITY_REBUILD: 777 /* 778 * Start the rebuild. The gv_plex_done will 779 * handle issuing of the remaining rebuild bio's 780 * until it's finished. 781 */ 782 G_VINUM_DEBUG(2, "event 'rebuild'"); 783 p = ev->arg1; 784 if (p->state != GV_PLEX_UP) { 785 G_VINUM_DEBUG(0, "plex %s is not " 786 "completely accessible", p->name); 787 break; 788 } 789 if (p->flags & GV_PLEX_SYNCING || 790 p->flags & GV_PLEX_REBUILDING || 791 p->flags & GV_PLEX_GROWING) { 792 G_VINUM_DEBUG(0, "plex %s is busy with " 793 "syncing or parity build", p->name); 794 break; 795 } 796 p->synced = 0; 797 p->flags |= GV_PLEX_REBUILDING; 798 g_topology_assert_not(); 799 g_topology_lock(); 800 err = gv_access(p->vol_sc->provider, 1, 1, 0); 801 if (err) { 802 G_VINUM_DEBUG(0, "unable to access " 803 "provider"); 804 break; 805 } 806 g_topology_unlock(); 807 gv_parity_request(p, GV_BIO_CHECK | 808 GV_BIO_PARITY, 0); 809 break; 810 811 case GV_EVENT_PARITY_CHECK: 812 /* Start parity check. */ 813 G_VINUM_DEBUG(2, "event 'check'"); 814 p = ev->arg1; 815 if (p->state != GV_PLEX_UP) { 816 G_VINUM_DEBUG(0, "plex %s is not " 817 "completely accessible", p->name); 818 break; 819 } 820 if (p->flags & GV_PLEX_SYNCING || 821 p->flags & GV_PLEX_REBUILDING || 822 p->flags & GV_PLEX_GROWING) { 823 G_VINUM_DEBUG(0, "plex %s is busy with " 824 "syncing or parity build", p->name); 825 break; 826 } 827 p->synced = 0; 828 g_topology_assert_not(); 829 g_topology_lock(); 830 err = gv_access(p->vol_sc->provider, 1, 1, 0); 831 if (err) { 832 G_VINUM_DEBUG(0, "unable to access " 833 "provider"); 834 break; 835 } 836 g_topology_unlock(); 837 gv_parity_request(p, GV_BIO_CHECK, 0); 838 break; 839 840 case GV_EVENT_START_PLEX: 841 G_VINUM_DEBUG(2, "event 'start' plex"); 842 p = ev->arg1; 843 gv_start_plex(p); 844 break; 845 846 case GV_EVENT_START_VOLUME: 847 G_VINUM_DEBUG(2, "event 'start' volume"); 848 v = ev->arg1; 849 gv_start_vol(v); 850 break; 851 852 case GV_EVENT_ATTACH_PLEX: 853 G_VINUM_DEBUG(2, "event 'attach' plex"); 854 p = ev->arg1; 855 v = ev->arg2; 856 rename = ev->arg4; 857 err = gv_attach_plex(p, v, rename); 858 if (err) 859 G_VINUM_DEBUG(0, "error attaching %s to" 860 " %s: error code %d", p->name, 861 v->name, err); 862 break; 863 864 case GV_EVENT_ATTACH_SD: 865 G_VINUM_DEBUG(2, "event 'attach' sd"); 866 s = ev->arg1; 867 p = ev->arg2; 868 offset = ev->arg3; 869 rename = ev->arg4; 870 err = gv_attach_sd(s, p, offset, rename); 871 if (err) 872 G_VINUM_DEBUG(0, "error attaching %s to" 873 " %s: error code %d", s->name, 874 p->name, err); 875 break; 876 877 case GV_EVENT_DETACH_PLEX: 878 G_VINUM_DEBUG(2, "event 'detach' plex"); 879 p = ev->arg1; 880 flags = ev->arg3; 881 err = gv_detach_plex(p, flags); 882 if (err) 883 G_VINUM_DEBUG(0, "error detaching %s: " 884 "error code %d", p->name, err); 885 break; 886 887 case GV_EVENT_DETACH_SD: 888 G_VINUM_DEBUG(2, "event 'detach' sd"); 889 s = ev->arg1; 890 flags = ev->arg3; 891 err = gv_detach_sd(s, flags); 892 if (err) 893 G_VINUM_DEBUG(0, "error detaching %s: " 894 "error code %d", s->name, err); 895 break; 896 897 case GV_EVENT_RENAME_VOL: 898 G_VINUM_DEBUG(2, "event 'rename' volume"); 899 v = ev->arg1; 900 newname = ev->arg2; 901 flags = ev->arg3; 902 err = gv_rename_vol(sc, v, newname, flags); 903 if (err) 904 G_VINUM_DEBUG(0, "error renaming %s to " 905 "%s: error code %d", v->name, 906 newname, err); 907 g_free(newname); 908 /* Destroy and recreate the provider if we can. */ 909 if (gv_provider_is_open(v->provider)) { 910 G_VINUM_DEBUG(0, "unable to rename " 911 "provider to %s: provider in use", 912 v->name); 913 break; 914 } 915 g_topology_lock(); 916 g_wither_provider(v->provider, ENOENT); 917 g_topology_unlock(); 918 v->provider = NULL; 919 gv_post_event(sc, GV_EVENT_SETUP_OBJECTS, sc, 920 NULL, 0, 0); 921 break; 922 923 case GV_EVENT_RENAME_PLEX: 924 G_VINUM_DEBUG(2, "event 'rename' plex"); 925 p = ev->arg1; 926 newname = ev->arg2; 927 flags = ev->arg3; 928 err = gv_rename_plex(sc, p, newname, flags); 929 if (err) 930 G_VINUM_DEBUG(0, "error renaming %s to " 931 "%s: error code %d", p->name, 932 newname, err); 933 g_free(newname); 934 break; 935 936 case GV_EVENT_RENAME_SD: 937 G_VINUM_DEBUG(2, "event 'rename' sd"); 938 s = ev->arg1; 939 newname = ev->arg2; 940 flags = ev->arg3; 941 err = gv_rename_sd(sc, s, newname, flags); 942 if (err) 943 G_VINUM_DEBUG(0, "error renaming %s to " 944 "%s: error code %d", s->name, 945 newname, err); 946 g_free(newname); 947 break; 948 949 case GV_EVENT_RENAME_DRIVE: 950 G_VINUM_DEBUG(2, "event 'rename' drive"); 951 d = ev->arg1; 952 newname = ev->arg2; 953 flags = ev->arg3; 954 err = gv_rename_drive(sc, d, newname, flags); 955 if (err) 956 G_VINUM_DEBUG(0, "error renaming %s to " 957 "%s: error code %d", d->name, 958 newname, err); 959 g_free(newname); 960 break; 961 962 case GV_EVENT_MOVE_SD: 963 G_VINUM_DEBUG(2, "event 'move' sd"); 964 s = ev->arg1; 965 d = ev->arg2; 966 flags = ev->arg3; 967 err = gv_move_sd(sc, s, d, flags); 968 if (err) 969 G_VINUM_DEBUG(0, "error moving %s to " 970 "%s: error code %d", s->name, 971 d->name, err); 972 break; 973 974 case GV_EVENT_THREAD_EXIT: 975 G_VINUM_DEBUG(2, "event 'thread exit'"); 976 g_free(ev); 977 mtx_lock(&sc->equeue_mtx); 978 mtx_lock(&sc->bqueue_mtx); 979 gv_cleanup(sc); 980 mtx_destroy(&sc->bqueue_mtx); 981 mtx_destroy(&sc->equeue_mtx); 982 g_free(sc->bqueue_down); 983 g_free(sc->bqueue_up); 984 g_free(sc); 985 kproc_exit(0); 986 /* NOTREACHED */ 987 988 default: 989 G_VINUM_DEBUG(1, "unknown event %d", ev->type); 990 } 991 992 g_free(ev); 993 continue; 994 } 995 996 /* ... then do I/O processing. */ 997 mtx_lock(&sc->bqueue_mtx); 998 /* First do new requests. */ 999 bp = bioq_takefirst(sc->bqueue_down); 1000 if (bp != NULL) { 1001 mtx_unlock(&sc->bqueue_mtx); 1002 /* A bio that interfered with another bio. */ 1003 if (bp->bio_pflags & GV_BIO_ONHOLD) { 1004 s = bp->bio_caller1; 1005 p = s->plex_sc; 1006 /* Is it still locked out? */ 1007 if (gv_stripe_active(p, bp)) { 1008 /* Park the bio on the waiting queue. */ 1009 bioq_disksort(p->wqueue, bp); 1010 } else { 1011 bp->bio_pflags &= ~GV_BIO_ONHOLD; 1012 g_io_request(bp, s->drive_sc->consumer); 1013 } 1014 /* A special request requireing special handling. */ 1015 } else if (bp->bio_pflags & GV_BIO_INTERNAL) { 1016 p = bp->bio_caller1; 1017 gv_plex_start(p, bp); 1018 } else { 1019 gv_volume_start(sc, bp); 1020 } 1021 mtx_lock(&sc->bqueue_mtx); 1022 } 1023 /* Then do completed requests. */ 1024 bp = bioq_takefirst(sc->bqueue_up); 1025 if (bp == NULL) { 1026 msleep(sc, &sc->bqueue_mtx, PRIBIO, "-", hz/10); 1027 mtx_unlock(&sc->bqueue_mtx); 1028 continue; 1029 } 1030 mtx_unlock(&sc->bqueue_mtx); 1031 gv_bio_done(sc, bp); 1032 } 1033 } 1034 1035 #define VINUM_CLASS_NAME "VINUM" 1036 1037 static struct g_class g_vinum_class = { 1038 .name = VINUM_CLASS_NAME, 1039 .version = G_VERSION, 1040 .init = gv_init, 1041 .taste = gv_taste, 1042 .ctlreq = gv_config, 1043 .destroy_geom = gv_unload, 1044 }; 1045 1046 DECLARE_GEOM_CLASS(g_vinum_class, g_vinum); 1047 MODULE_VERSION(geom_vinum, 0); 1048