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