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