g_concat.c revision 133371
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: head/sys/geom/concat/g_concat.c 133371 2004-08-09 11:14:25Z pjd $"); 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 43static MALLOC_DEFINE(M_CONCAT, "concat data", "GEOM_CONCAT Data"); 44 45SYSCTL_DECL(_kern_geom); 46SYSCTL_NODE(_kern_geom, OID_AUTO, concat, CTLFLAG_RW, 0, "GEOM_CONCAT stuff"); 47static u_int g_concat_debug = 0; 48SYSCTL_UINT(_kern_geom_concat, OID_AUTO, debug, CTLFLAG_RW, &g_concat_debug, 0, 49 "Debug level"); 50 51static int g_concat_destroy(struct g_concat_softc *sc, boolean_t force); 52static int g_concat_destroy_geom(struct gctl_req *req, struct g_class *mp, 53 struct g_geom *gp); 54 55static g_taste_t g_concat_taste; 56static g_ctl_req_t g_concat_config; 57static g_dumpconf_t g_concat_dumpconf; 58 59struct 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 */ 71static u_int 72gcd(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 */ 87static u_int 88lcm(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 */ 97static u_int 98g_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 111static void 112g_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 137static void 138g_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 160static int 161g_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 212static void 213g_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 291static void 292g_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 327static int 328g_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 */ 358static int 359g_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); 420fail: 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 428static struct g_geom * 429g_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 479static int 480g_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 525static int 526g_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 535static struct g_geom * 536g_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 /* 572 * Let's check if device already exists. 573 */ 574 sc = NULL; 575 LIST_FOREACH(gp, &mp->geom, geom) { 576 sc = gp->softc; 577 if (sc == NULL) 578 continue; 579 if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC) 580 continue; 581 if (strcmp(md.md_name, sc->sc_name) != 0) 582 continue; 583 if (md.md_id != sc->sc_id) 584 continue; 585 break; 586 } 587 if (gp != NULL) { 588 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 589 error = g_concat_add_disk(sc, pp, md.md_no); 590 if (error != 0) { 591 G_CONCAT_DEBUG(0, 592 "Cannot add disk %s to %s (error=%d).", pp->name, 593 gp->name, error); 594 return (NULL); 595 } 596 } else { 597 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC); 598 if (gp == NULL) { 599 G_CONCAT_DEBUG(0, "Cannot create device %s.", 600 md.md_name); 601 return (NULL); 602 } 603 sc = gp->softc; 604 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 605 error = g_concat_add_disk(sc, pp, md.md_no); 606 if (error != 0) { 607 G_CONCAT_DEBUG(0, 608 "Cannot add disk %s to %s (error=%d).", pp->name, 609 gp->name, error); 610 g_concat_destroy(sc, 1); 611 return (NULL); 612 } 613 } 614 615 return (gp); 616} 617 618static void 619g_concat_ctl_create(struct gctl_req *req, struct g_class *mp) 620{ 621 u_int attached, no; 622 struct g_concat_metadata md; 623 struct g_provider *pp; 624 struct g_concat_softc *sc; 625 struct g_geom *gp; 626 struct sbuf *sb; 627 const char *name; 628 char param[16]; 629 int *nargs; 630 631 g_topology_assert(); 632 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 633 if (nargs == NULL) { 634 gctl_error(req, "No '%s' argument.", "nargs"); 635 return; 636 } 637 if (*nargs <= 2) { 638 gctl_error(req, "Too few arguments."); 639 return; 640 } 641 642 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic)); 643 md.md_version = G_CONCAT_VERSION; 644 name = gctl_get_asciiparam(req, "arg0"); 645 if (name == NULL) { 646 gctl_error(req, "No 'arg%u' argument.", 0); 647 return; 648 } 649 strlcpy(md.md_name, name, sizeof(md.md_name)); 650 md.md_id = arc4random(); 651 md.md_no = 0; 652 md.md_all = *nargs - 1; 653 654 /* Check all providers are valid */ 655 for (no = 1; no < *nargs; no++) { 656 snprintf(param, sizeof(param), "arg%u", no); 657 name = gctl_get_asciiparam(req, param); 658 if (name == NULL) { 659 gctl_error(req, "No 'arg%u' argument.", no); 660 return; 661 } 662 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 663 name += strlen("/dev/"); 664 pp = g_provider_by_name(name); 665 if (pp == NULL) { 666 G_CONCAT_DEBUG(1, "Disk %s is invalid.", name); 667 gctl_error(req, "Disk %s is invalid.", name); 668 return; 669 } 670 } 671 672 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL); 673 if (gp == NULL) { 674 gctl_error(req, "Can't configure %s.", md.md_name); 675 return; 676 } 677 678 sc = gp->softc; 679 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 680 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name); 681 for (attached = 0, no = 1; no < *nargs; no++) { 682 snprintf(param, sizeof(param), "arg%u", no); 683 name = gctl_get_asciiparam(req, param); 684 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 685 name += strlen("/dev/"); 686 pp = g_provider_by_name(name); 687 KASSERT(pp != NULL, ("Provider %s disappear?!", name)); 688 if (g_concat_add_disk(sc, pp, no - 1) != 0) { 689 G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.", 690 no, pp->name, gp->name); 691 sbuf_printf(sb, " %s", pp->name); 692 continue; 693 } 694 attached++; 695 } 696 sbuf_finish(sb); 697 if (md.md_all != attached) { 698 g_concat_destroy(gp->softc, 1); 699 gctl_error(req, "%s", sbuf_data(sb)); 700 } 701 sbuf_delete(sb); 702} 703 704static struct g_concat_softc * 705g_concat_find_device(struct g_class *mp, const char *name) 706{ 707 struct g_concat_softc *sc; 708 struct g_geom *gp; 709 710 LIST_FOREACH(gp, &mp->geom, geom) { 711 sc = gp->softc; 712 if (sc == NULL) 713 continue; 714 if (strcmp(sc->sc_name, name) == 0) 715 return (sc); 716 } 717 return (NULL); 718} 719 720static void 721g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp) 722{ 723 struct g_concat_softc *sc; 724 int *force, *nargs, error; 725 const char *name; 726 char param[16]; 727 u_int i; 728 729 g_topology_assert(); 730 731 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 732 if (nargs == NULL) { 733 gctl_error(req, "No '%s' argument.", "nargs"); 734 return; 735 } 736 if (*nargs <= 0) { 737 gctl_error(req, "Missing device(s)."); 738 return; 739 } 740 force = gctl_get_paraml(req, "force", sizeof(*force)); 741 if (force == NULL) { 742 gctl_error(req, "No '%s' argument.", "force"); 743 return; 744 } 745 746 for (i = 0; i < (u_int)*nargs; i++) { 747 snprintf(param, sizeof(param), "arg%u", i); 748 name = gctl_get_asciiparam(req, param); 749 if (name == NULL) { 750 gctl_error(req, "No 'arg%u' argument.", i); 751 return; 752 } 753 sc = g_concat_find_device(mp, name); 754 if (sc == NULL) { 755 gctl_error(req, "No such device: %s.", name); 756 return; 757 } 758 error = g_concat_destroy(sc, *force); 759 if (error != 0) { 760 gctl_error(req, "Cannot destroy device %s (error=%d).", 761 sc->sc_name, error); 762 return; 763 } 764 } 765} 766 767static void 768g_concat_config(struct gctl_req *req, struct g_class *mp, const char *verb) 769{ 770 uint32_t *version; 771 772 g_topology_assert(); 773 774 version = gctl_get_paraml(req, "version", sizeof(*version)); 775 if (version == NULL) { 776 gctl_error(req, "No '%s' argument.", "version"); 777 return; 778 } 779 if (*version != G_CONCAT_VERSION) { 780 gctl_error(req, "Userland and kernel parts are out of sync."); 781 return; 782 } 783 784 if (strcmp(verb, "create") == 0) { 785 g_concat_ctl_create(req, mp); 786 return; 787 } else if (strcmp(verb, "destroy") == 0 || 788 strcmp(verb, "stop") == 0) { 789 g_concat_ctl_destroy(req, mp); 790 return; 791 } 792 gctl_error(req, "Unknown verb."); 793} 794 795static void 796g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 797 struct g_consumer *cp, struct g_provider *pp) 798{ 799 struct g_concat_softc *sc; 800 801 g_topology_assert(); 802 sc = gp->softc; 803 if (sc == NULL) 804 return; 805 if (pp != NULL) { 806 /* Nothing here. */ 807 } else if (cp != NULL) { 808 struct g_concat_disk *disk; 809 810 disk = cp->private; 811 if (disk == NULL) 812 return; 813 sbuf_printf(sb, "%s<End>%jd</End>\n", indent, 814 (intmax_t)disk->d_end); 815 sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent, 816 (intmax_t)disk->d_start); 817 } else { 818 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id); 819 sbuf_printf(sb, "%s<Type>", indent); 820 switch (sc->sc_type) { 821 case G_CONCAT_TYPE_AUTOMATIC: 822 sbuf_printf(sb, "AUTOMATIC"); 823 break; 824 case G_CONCAT_TYPE_MANUAL: 825 sbuf_printf(sb, "MANUAL"); 826 break; 827 default: 828 sbuf_printf(sb, "UNKNOWN"); 829 break; 830 } 831 sbuf_printf(sb, "</Type>\n"); 832 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n", 833 indent, sc->sc_ndisks, g_concat_nvalid(sc)); 834 sbuf_printf(sb, "%s<State>", indent); 835 if (sc->sc_provider != NULL && sc->sc_provider->error == 0) 836 sbuf_printf(sb, "UP"); 837 else 838 sbuf_printf(sb, "DOWN"); 839 sbuf_printf(sb, "</State>\n"); 840 } 841} 842 843DECLARE_GEOM_CLASS(g_concat_class, g_concat); 844