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