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