1 /*- 2 * Copyright (c) 2004 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/module.h> 34 #include <sys/lock.h> 35 #include <sys/mutex.h> 36 #include <sys/bio.h> 37 #include <sys/sysctl.h> 38 #include <sys/malloc.h> 39 #include <geom/geom.h> 40 #include <geom/concat/g_concat.h> 41 42 43 static MALLOC_DEFINE(M_CONCAT, "concat data", "GEOM_CONCAT Data"); 44 45 SYSCTL_DECL(_kern_geom); 46 SYSCTL_NODE(_kern_geom, OID_AUTO, concat, CTLFLAG_RW, 0, "GEOM_CONCAT stuff"); 47 static u_int g_concat_debug = 0; 48 SYSCTL_UINT(_kern_geom_concat, OID_AUTO, debug, CTLFLAG_RW, &g_concat_debug, 0, 49 "Debug level"); 50 51 static int g_concat_destroy(struct g_concat_softc *sc, boolean_t force); 52 static int g_concat_destroy_geom(struct gctl_req *req, struct g_class *mp, 53 struct g_geom *gp); 54 55 static g_taste_t g_concat_taste; 56 static g_ctl_req_t g_concat_config; 57 static g_dumpconf_t g_concat_dumpconf; 58 59 struct g_class g_concat_class = { 60 .name = G_CONCAT_CLASS_NAME, 61 .version = G_VERSION, 62 .ctlreq = g_concat_config, 63 .taste = g_concat_taste, 64 .destroy_geom = g_concat_destroy_geom 65 }; 66 67 68 /* 69 * Greatest Common Divisor. 70 */ 71 static u_int 72 gcd(u_int a, u_int b) 73 { 74 u_int c; 75 76 while (b != 0) { 77 c = a; 78 a = b; 79 b = (c % b); 80 } 81 return (a); 82 } 83 84 /* 85 * Least Common Multiple. 86 */ 87 static u_int 88 lcm(u_int a, u_int b) 89 { 90 91 return ((a * b) / gcd(a, b)); 92 } 93 94 /* 95 * Return the number of valid disks. 96 */ 97 static u_int 98 g_concat_nvalid(struct g_concat_softc *sc) 99 { 100 u_int i, no; 101 102 no = 0; 103 for (i = 0; i < sc->sc_ndisks; i++) { 104 if (sc->sc_disks[i].d_consumer != NULL) 105 no++; 106 } 107 108 return (no); 109 } 110 111 static void 112 g_concat_remove_disk(struct g_concat_disk *disk) 113 { 114 struct g_consumer *cp; 115 struct g_concat_softc *sc; 116 117 KASSERT(disk->d_consumer != NULL, ("Non-valid disk in %s.", __func__)); 118 sc = disk->d_softc; 119 cp = disk->d_consumer; 120 121 G_CONCAT_DEBUG(0, "Disk %s removed from %s.", cp->provider->name, 122 sc->sc_name); 123 124 disk->d_consumer = NULL; 125 if (sc->sc_provider != NULL) { 126 g_orphan_provider(sc->sc_provider, ENXIO); 127 sc->sc_provider = NULL; 128 G_CONCAT_DEBUG(0, "Device %s removed.", sc->sc_name); 129 } 130 131 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 132 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 133 g_detach(cp); 134 g_destroy_consumer(cp); 135 } 136 137 static void 138 g_concat_orphan(struct g_consumer *cp) 139 { 140 struct g_concat_softc *sc; 141 struct g_concat_disk *disk; 142 struct g_geom *gp; 143 144 g_topology_assert(); 145 gp = cp->geom; 146 sc = gp->softc; 147 if (sc == NULL) 148 return; 149 150 disk = cp->private; 151 if (disk == NULL) /* Possible? */ 152 return; 153 g_concat_remove_disk(disk); 154 155 /* If there are no valid disks anymore, remove device. */ 156 if (g_concat_nvalid(sc) == 0) 157 g_concat_destroy(sc, 1); 158 } 159 160 static int 161 g_concat_access(struct g_provider *pp, int dr, int dw, int de) 162 { 163 struct g_consumer *cp1, *cp2; 164 struct g_concat_softc *sc; 165 struct g_geom *gp; 166 int error; 167 168 gp = pp->geom; 169 sc = gp->softc; 170 171 if (sc == NULL) { 172 /* 173 * It looks like geom is being withered. 174 * In that case we allow only negative requests. 175 */ 176 KASSERT(dr <= 0 && dw <= 0 && de <= 0, 177 ("Positive access request (device=%s).", pp->name)); 178 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && 179 (pp->ace + de) == 0) { 180 G_CONCAT_DEBUG(0, "Device %s definitely destroyed.", 181 gp->name); 182 } 183 return (0); 184 } 185 186 /* On first open, grab an extra "exclusive" bit */ 187 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) 188 de++; 189 /* ... and let go of it on last close */ 190 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0) 191 de--; 192 193 error = ENXIO; 194 LIST_FOREACH(cp1, &gp->consumer, consumer) { 195 error = g_access(cp1, dr, dw, de); 196 if (error == 0) 197 continue; 198 /* 199 * If we fail here, backout all previous changes. 200 */ 201 LIST_FOREACH(cp2, &gp->consumer, consumer) { 202 if (cp1 == cp2) 203 return (error); 204 g_access(cp2, -dr, -dw, -de); 205 } 206 /* NOTREACHED */ 207 } 208 209 return (error); 210 } 211 212 static void 213 g_concat_start(struct bio *bp) 214 { 215 struct g_concat_softc *sc; 216 struct g_concat_disk *disk; 217 struct g_provider *pp; 218 off_t offset, end, length, off, len; 219 struct bio *cbp; 220 char *addr; 221 u_int no; 222 223 pp = bp->bio_to; 224 sc = pp->geom->softc; 225 /* 226 * If sc == NULL, provider's error should be set and g_concat_start() 227 * should not be called at all. 228 */ 229 KASSERT(sc != NULL, 230 ("Provider's error should be set (error=%d)(device=%s).", 231 bp->bio_to->error, bp->bio_to->name)); 232 233 G_CONCAT_LOGREQ(bp, "Request received."); 234 235 switch (bp->bio_cmd) { 236 case BIO_READ: 237 case BIO_WRITE: 238 case BIO_DELETE: 239 break; 240 case BIO_GETATTR: 241 /* To which provider it should be delivered? */ 242 default: 243 g_io_deliver(bp, EOPNOTSUPP); 244 return; 245 } 246 247 offset = bp->bio_offset; 248 length = bp->bio_length; 249 addr = bp->bio_data; 250 end = offset + length; 251 252 for (no = 0; no < sc->sc_ndisks; no++) { 253 disk = &sc->sc_disks[no]; 254 if (disk->d_end <= offset) 255 continue; 256 if (disk->d_start >= end) 257 break; 258 259 off = offset - disk->d_start; 260 len = MIN(length, disk->d_end - offset); 261 length -= len; 262 offset += len; 263 264 cbp = g_clone_bio(bp); 265 if (cbp == NULL) { 266 if (bp->bio_error == 0) 267 bp->bio_error = ENOMEM; 268 return; 269 } 270 /* 271 * Fill in the component buf structure. 272 */ 273 cbp->bio_done = g_std_done; 274 cbp->bio_offset = off; 275 cbp->bio_data = addr; 276 addr += len; 277 cbp->bio_length = len; 278 cbp->bio_to = disk->d_consumer->provider; 279 G_CONCAT_LOGREQ(cbp, "Sending request."); 280 g_io_request(cbp, disk->d_consumer); 281 282 if (length == 0) 283 break; 284 } 285 286 KASSERT(length == 0, 287 ("Length is still greater than 0 (class=%s, name=%s).", 288 bp->bio_to->geom->class->name, bp->bio_to->geom->name)); 289 } 290 291 static void 292 g_concat_check_and_run(struct g_concat_softc *sc) 293 { 294 struct g_concat_disk *disk; 295 u_int no, sectorsize = 0; 296 off_t start; 297 298 if (g_concat_nvalid(sc) != sc->sc_ndisks) 299 return; 300 301 sc->sc_provider = g_new_providerf(sc->sc_geom, "concat/%s", 302 sc->sc_name); 303 start = 0; 304 for (no = 0; no < sc->sc_ndisks; no++) { 305 disk = &sc->sc_disks[no]; 306 disk->d_start = start; 307 disk->d_end = disk->d_start + 308 disk->d_consumer->provider->mediasize; 309 if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) 310 disk->d_end -= disk->d_consumer->provider->sectorsize; 311 start = disk->d_end; 312 if (no == 0) 313 sectorsize = disk->d_consumer->provider->sectorsize; 314 else { 315 sectorsize = lcm(sectorsize, 316 disk->d_consumer->provider->sectorsize); 317 } 318 } 319 sc->sc_provider->sectorsize = sectorsize; 320 /* We have sc->sc_disks[sc->sc_ndisks - 1].d_end in 'start'. */ 321 sc->sc_provider->mediasize = start; 322 g_error_provider(sc->sc_provider, 0); 323 324 G_CONCAT_DEBUG(0, "Device %s activated.", sc->sc_name); 325 } 326 327 static int 328 g_concat_read_metadata(struct g_consumer *cp, struct g_concat_metadata *md) 329 { 330 struct g_provider *pp; 331 u_char *buf; 332 int error; 333 334 g_topology_assert(); 335 336 error = g_access(cp, 1, 0, 0); 337 if (error != 0) 338 return (error); 339 pp = cp->provider; 340 g_topology_unlock(); 341 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 342 &error); 343 g_topology_lock(); 344 g_access(cp, -1, 0, 0); 345 if (buf == NULL) 346 return (error); 347 348 /* Decode metadata. */ 349 concat_metadata_decode(buf, md); 350 g_free(buf); 351 352 return (0); 353 } 354 355 /* 356 * Add disk to given device. 357 */ 358 static int 359 g_concat_add_disk(struct g_concat_softc *sc, struct g_provider *pp, u_int no) 360 { 361 struct g_concat_disk *disk; 362 struct g_consumer *cp, *fcp; 363 struct g_geom *gp; 364 int error; 365 366 /* Metadata corrupted? */ 367 if (no >= sc->sc_ndisks) 368 return (EINVAL); 369 370 disk = &sc->sc_disks[no]; 371 /* Check if disk is not already attached. */ 372 if (disk->d_consumer != NULL) 373 return (EEXIST); 374 375 gp = sc->sc_geom; 376 fcp = LIST_FIRST(&gp->consumer); 377 378 cp = g_new_consumer(gp); 379 error = g_attach(cp, pp); 380 if (error != 0) { 381 g_destroy_consumer(cp); 382 return (error); 383 } 384 385 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) { 386 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace); 387 if (error != 0) { 388 g_detach(cp); 389 g_destroy_consumer(cp); 390 return (error); 391 } 392 } 393 if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) { 394 struct g_concat_metadata md; 395 396 /* Re-read metadata. */ 397 error = g_concat_read_metadata(cp, &md); 398 if (error != 0) 399 goto fail; 400 401 if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0 || 402 strcmp(md.md_name, sc->sc_name) != 0 || 403 md.md_id != sc->sc_id) { 404 G_CONCAT_DEBUG(0, "Metadata on %s changed.", pp->name); 405 goto fail; 406 } 407 } 408 409 cp->private = disk; 410 disk->d_consumer = cp; 411 disk->d_softc = sc; 412 disk->d_start = 0; /* not yet */ 413 disk->d_end = 0; /* not yet */ 414 415 G_CONCAT_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name); 416 417 g_concat_check_and_run(sc); 418 419 return (0); 420 fail: 421 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) 422 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace); 423 g_detach(cp); 424 g_destroy_consumer(cp); 425 return (error); 426 } 427 428 static struct g_geom * 429 g_concat_create(struct g_class *mp, const struct g_concat_metadata *md, 430 u_int type) 431 { 432 struct g_concat_softc *sc; 433 struct g_geom *gp; 434 u_int no; 435 436 G_CONCAT_DEBUG(1, "Creating device %s (id=%u).", md->md_name, 437 md->md_id); 438 439 /* Two disks is minimum. */ 440 if (md->md_all <= 1) 441 return (NULL); 442 443 /* Check for duplicate unit */ 444 LIST_FOREACH(gp, &mp->geom, geom) { 445 sc = gp->softc; 446 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) { 447 G_CONCAT_DEBUG(0, "Device %s already configured.", 448 gp->name); 449 return (NULL); 450 } 451 } 452 gp = g_new_geomf(mp, "%s", md->md_name); 453 gp->softc = NULL; /* for a moment */ 454 455 sc = malloc(sizeof(*sc), M_CONCAT, M_WAITOK | M_ZERO); 456 gp->start = g_concat_start; 457 gp->spoiled = g_concat_orphan; 458 gp->orphan = g_concat_orphan; 459 gp->access = g_concat_access; 460 gp->dumpconf = g_concat_dumpconf; 461 462 sc->sc_id = md->md_id; 463 sc->sc_ndisks = md->md_all; 464 sc->sc_disks = malloc(sizeof(struct g_concat_disk) * sc->sc_ndisks, 465 M_CONCAT, M_WAITOK | M_ZERO); 466 for (no = 0; no < sc->sc_ndisks; no++) 467 sc->sc_disks[no].d_consumer = NULL; 468 sc->sc_type = type; 469 470 gp->softc = sc; 471 sc->sc_geom = gp; 472 sc->sc_provider = NULL; 473 474 G_CONCAT_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id); 475 476 return (gp); 477 } 478 479 static int 480 g_concat_destroy(struct g_concat_softc *sc, boolean_t force) 481 { 482 struct g_provider *pp; 483 struct g_geom *gp; 484 u_int no; 485 486 g_topology_assert(); 487 488 if (sc == NULL) 489 return (ENXIO); 490 491 pp = sc->sc_provider; 492 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 493 if (force) { 494 G_CONCAT_DEBUG(0, "Device %s is still open, so it " 495 "can't be definitely removed.", pp->name); 496 } else { 497 G_CONCAT_DEBUG(1, 498 "Device %s is still open (r%dw%de%d).", pp->name, 499 pp->acr, pp->acw, pp->ace); 500 return (EBUSY); 501 } 502 } 503 504 for (no = 0; no < sc->sc_ndisks; no++) { 505 if (sc->sc_disks[no].d_consumer != NULL) 506 g_concat_remove_disk(&sc->sc_disks[no]); 507 } 508 509 gp = sc->sc_geom; 510 gp->softc = NULL; 511 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)", 512 gp->name)); 513 free(sc->sc_disks, M_CONCAT); 514 free(sc, M_CONCAT); 515 516 pp = LIST_FIRST(&gp->provider); 517 if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)) 518 G_CONCAT_DEBUG(0, "Device %s destroyed.", gp->name); 519 520 g_wither_geom(gp, ENXIO); 521 522 return (0); 523 } 524 525 static int 526 g_concat_destroy_geom(struct gctl_req *req __unused, 527 struct g_class *mp __unused, struct g_geom *gp) 528 { 529 struct g_concat_softc *sc; 530 531 sc = gp->softc; 532 return (g_concat_destroy(sc, 0)); 533 } 534 535 static struct g_geom * 536 g_concat_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 537 { 538 struct g_concat_metadata md; 539 struct g_concat_softc *sc; 540 struct g_consumer *cp; 541 struct g_geom *gp; 542 int error; 543 544 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 545 g_topology_assert(); 546 547 G_CONCAT_DEBUG(3, "Tasting %s.", pp->name); 548 549 gp = g_new_geomf(mp, "concat:taste"); 550 gp->start = g_concat_start; 551 gp->access = g_concat_access; 552 gp->orphan = g_concat_orphan; 553 cp = g_new_consumer(gp); 554 g_attach(cp, pp); 555 error = g_concat_read_metadata(cp, &md); 556 g_detach(cp); 557 g_destroy_consumer(cp); 558 g_destroy_geom(gp); 559 if (error != 0) 560 return (NULL); 561 gp = NULL; 562 563 if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0) 564 return (NULL); 565 if (md.md_version > G_CONCAT_VERSION) { 566 printf("geom_concat.ko module is too old to handle %s.\n", 567 pp->name); 568 return (NULL); 569 } 570 /* 571 * Backward compatibility: 572 * There was no md_provider field in earlier versions of metadata. 573 */ 574 if (md.md_version < 3) 575 bzero(md.md_provider, sizeof(md.md_provider)); 576 577 if (md.md_provider[0] != '\0' && strcmp(md.md_provider, pp->name) != 0) 578 return (NULL); 579 580 /* 581 * Let's check if device already exists. 582 */ 583 sc = NULL; 584 LIST_FOREACH(gp, &mp->geom, geom) { 585 sc = gp->softc; 586 if (sc == NULL) 587 continue; 588 if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC) 589 continue; 590 if (strcmp(md.md_name, sc->sc_name) != 0) 591 continue; 592 if (md.md_id != sc->sc_id) 593 continue; 594 break; 595 } 596 if (gp != NULL) { 597 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 598 error = g_concat_add_disk(sc, pp, md.md_no); 599 if (error != 0) { 600 G_CONCAT_DEBUG(0, 601 "Cannot add disk %s to %s (error=%d).", pp->name, 602 gp->name, error); 603 return (NULL); 604 } 605 } else { 606 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC); 607 if (gp == NULL) { 608 G_CONCAT_DEBUG(0, "Cannot create device %s.", 609 md.md_name); 610 return (NULL); 611 } 612 sc = gp->softc; 613 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 614 error = g_concat_add_disk(sc, pp, md.md_no); 615 if (error != 0) { 616 G_CONCAT_DEBUG(0, 617 "Cannot add disk %s to %s (error=%d).", pp->name, 618 gp->name, error); 619 g_concat_destroy(sc, 1); 620 return (NULL); 621 } 622 } 623 624 return (gp); 625 } 626 627 static void 628 g_concat_ctl_create(struct gctl_req *req, struct g_class *mp) 629 { 630 u_int attached, no; 631 struct g_concat_metadata md; 632 struct g_provider *pp; 633 struct g_concat_softc *sc; 634 struct g_geom *gp; 635 struct sbuf *sb; 636 const char *name; 637 char param[16]; 638 int *nargs; 639 640 g_topology_assert(); 641 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 642 if (nargs == NULL) { 643 gctl_error(req, "No '%s' argument.", "nargs"); 644 return; 645 } 646 if (*nargs <= 2) { 647 gctl_error(req, "Too few arguments."); 648 return; 649 } 650 651 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic)); 652 md.md_version = G_CONCAT_VERSION; 653 name = gctl_get_asciiparam(req, "arg0"); 654 if (name == NULL) { 655 gctl_error(req, "No 'arg%u' argument.", 0); 656 return; 657 } 658 strlcpy(md.md_name, name, sizeof(md.md_name)); 659 md.md_id = arc4random(); 660 md.md_no = 0; 661 md.md_all = *nargs - 1; 662 bzero(md.md_provider, sizeof(md.md_provider)); 663 664 /* Check all providers are valid */ 665 for (no = 1; no < *nargs; no++) { 666 snprintf(param, sizeof(param), "arg%u", no); 667 name = gctl_get_asciiparam(req, param); 668 if (name == NULL) { 669 gctl_error(req, "No 'arg%u' argument.", no); 670 return; 671 } 672 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 673 name += strlen("/dev/"); 674 pp = g_provider_by_name(name); 675 if (pp == NULL) { 676 G_CONCAT_DEBUG(1, "Disk %s is invalid.", name); 677 gctl_error(req, "Disk %s is invalid.", name); 678 return; 679 } 680 } 681 682 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL); 683 if (gp == NULL) { 684 gctl_error(req, "Can't configure %s.", md.md_name); 685 return; 686 } 687 688 sc = gp->softc; 689 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 690 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name); 691 for (attached = 0, no = 1; no < *nargs; no++) { 692 snprintf(param, sizeof(param), "arg%u", no); 693 name = gctl_get_asciiparam(req, param); 694 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 695 name += strlen("/dev/"); 696 pp = g_provider_by_name(name); 697 KASSERT(pp != NULL, ("Provider %s disappear?!", name)); 698 if (g_concat_add_disk(sc, pp, no - 1) != 0) { 699 G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.", 700 no, pp->name, gp->name); 701 sbuf_printf(sb, " %s", pp->name); 702 continue; 703 } 704 attached++; 705 } 706 sbuf_finish(sb); 707 if (md.md_all != attached) { 708 g_concat_destroy(gp->softc, 1); 709 gctl_error(req, "%s", sbuf_data(sb)); 710 } 711 sbuf_delete(sb); 712 } 713 714 static struct g_concat_softc * 715 g_concat_find_device(struct g_class *mp, const char *name) 716 { 717 struct g_concat_softc *sc; 718 struct g_geom *gp; 719 720 LIST_FOREACH(gp, &mp->geom, geom) { 721 sc = gp->softc; 722 if (sc == NULL) 723 continue; 724 if (strcmp(sc->sc_name, name) == 0) 725 return (sc); 726 } 727 return (NULL); 728 } 729 730 static void 731 g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp) 732 { 733 struct g_concat_softc *sc; 734 int *force, *nargs, error; 735 const char *name; 736 char param[16]; 737 u_int i; 738 739 g_topology_assert(); 740 741 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 742 if (nargs == NULL) { 743 gctl_error(req, "No '%s' argument.", "nargs"); 744 return; 745 } 746 if (*nargs <= 0) { 747 gctl_error(req, "Missing device(s)."); 748 return; 749 } 750 force = gctl_get_paraml(req, "force", sizeof(*force)); 751 if (force == NULL) { 752 gctl_error(req, "No '%s' argument.", "force"); 753 return; 754 } 755 756 for (i = 0; i < (u_int)*nargs; i++) { 757 snprintf(param, sizeof(param), "arg%u", i); 758 name = gctl_get_asciiparam(req, param); 759 if (name == NULL) { 760 gctl_error(req, "No 'arg%u' argument.", i); 761 return; 762 } 763 sc = g_concat_find_device(mp, name); 764 if (sc == NULL) { 765 gctl_error(req, "No such device: %s.", name); 766 return; 767 } 768 error = g_concat_destroy(sc, *force); 769 if (error != 0) { 770 gctl_error(req, "Cannot destroy device %s (error=%d).", 771 sc->sc_name, error); 772 return; 773 } 774 } 775 } 776 777 static void 778 g_concat_config(struct gctl_req *req, struct g_class *mp, const char *verb) 779 { 780 uint32_t *version; 781 782 g_topology_assert(); 783 784 version = gctl_get_paraml(req, "version", sizeof(*version)); 785 if (version == NULL) { 786 gctl_error(req, "No '%s' argument.", "version"); 787 return; 788 } 789 if (*version != G_CONCAT_VERSION) { 790 gctl_error(req, "Userland and kernel parts are out of sync."); 791 return; 792 } 793 794 if (strcmp(verb, "create") == 0) { 795 g_concat_ctl_create(req, mp); 796 return; 797 } else if (strcmp(verb, "destroy") == 0 || 798 strcmp(verb, "stop") == 0) { 799 g_concat_ctl_destroy(req, mp); 800 return; 801 } 802 gctl_error(req, "Unknown verb."); 803 } 804 805 static void 806 g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 807 struct g_consumer *cp, struct g_provider *pp) 808 { 809 struct g_concat_softc *sc; 810 811 g_topology_assert(); 812 sc = gp->softc; 813 if (sc == NULL) 814 return; 815 if (pp != NULL) { 816 /* Nothing here. */ 817 } else if (cp != NULL) { 818 struct g_concat_disk *disk; 819 820 disk = cp->private; 821 if (disk == NULL) 822 return; 823 sbuf_printf(sb, "%s<End>%jd</End>\n", indent, 824 (intmax_t)disk->d_end); 825 sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent, 826 (intmax_t)disk->d_start); 827 } else { 828 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id); 829 sbuf_printf(sb, "%s<Type>", indent); 830 switch (sc->sc_type) { 831 case G_CONCAT_TYPE_AUTOMATIC: 832 sbuf_printf(sb, "AUTOMATIC"); 833 break; 834 case G_CONCAT_TYPE_MANUAL: 835 sbuf_printf(sb, "MANUAL"); 836 break; 837 default: 838 sbuf_printf(sb, "UNKNOWN"); 839 break; 840 } 841 sbuf_printf(sb, "</Type>\n"); 842 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n", 843 indent, sc->sc_ndisks, g_concat_nvalid(sc)); 844 sbuf_printf(sb, "%s<State>", indent); 845 if (sc->sc_provider != NULL && sc->sc_provider->error == 0) 846 sbuf_printf(sb, "UP"); 847 else 848 sbuf_printf(sb, "DOWN"); 849 sbuf_printf(sb, "</State>\n"); 850 } 851 } 852 853 DECLARE_GEOM_CLASS(g_concat_class, g_concat); 854