1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 #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/sx.h> 37 #include <sys/bio.h> 38 #include <sys/sbuf.h> 39 #include <sys/sysctl.h> 40 #include <sys/malloc.h> 41 #include <geom/geom.h> 42 #include <geom/geom_dbg.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 | CTLFLAG_MPSAFE, 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 * Greatest Common Divisor. 74 */ 75 static u_int 76 gcd(u_int a, u_int b) 77 { 78 u_int c; 79 80 while (b != 0) { 81 c = a; 82 a = b; 83 b = (c % b); 84 } 85 return (a); 86 } 87 88 /* 89 * Least Common Multiple. 90 */ 91 static u_int 92 lcm(u_int a, u_int b) 93 { 94 95 return ((a * b) / gcd(a, b)); 96 } 97 98 /* 99 * Return the number of valid disks. 100 */ 101 static u_int 102 g_concat_nvalid(struct g_concat_softc *sc) 103 { 104 u_int no; 105 struct g_concat_disk *disk; 106 107 sx_assert(&sc->sc_disks_lock, SA_LOCKED); 108 109 no = 0; 110 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 111 if (disk->d_consumer != NULL) 112 no++; 113 } 114 115 return (no); 116 } 117 118 static void 119 g_concat_remove_disk(struct g_concat_disk *disk) 120 { 121 struct g_consumer *cp; 122 struct g_concat_softc *sc; 123 124 g_topology_assert(); 125 KASSERT(disk->d_consumer != NULL, ("Non-valid disk in %s.", __func__)); 126 sc = disk->d_softc; 127 cp = disk->d_consumer; 128 129 if (!disk->d_removed) { 130 G_CONCAT_DEBUG(0, "Disk %s removed from %s.", 131 cp->provider->name, sc->sc_name); 132 disk->d_removed = 1; 133 } 134 135 if (sc->sc_provider != NULL) { 136 G_CONCAT_DEBUG(0, "Device %s deactivated.", 137 sc->sc_provider->name); 138 g_wither_provider(sc->sc_provider, ENXIO); 139 sc->sc_provider = NULL; 140 } 141 142 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 143 return; 144 disk->d_consumer = NULL; 145 g_detach(cp); 146 g_destroy_consumer(cp); 147 /* If there are no valid disks anymore, remove device. */ 148 if (LIST_EMPTY(&sc->sc_geom->consumer)) 149 g_concat_destroy(sc, 1); 150 } 151 152 static void 153 g_concat_orphan(struct g_consumer *cp) 154 { 155 struct g_concat_softc *sc; 156 struct g_concat_disk *disk; 157 struct g_geom *gp; 158 159 g_topology_assert(); 160 gp = cp->geom; 161 sc = gp->softc; 162 if (sc == NULL) 163 return; 164 165 disk = cp->private; 166 if (disk == NULL) /* Possible? */ 167 return; 168 g_concat_remove_disk(disk); 169 } 170 171 static int 172 g_concat_access(struct g_provider *pp, int dr, int dw, int de) 173 { 174 struct g_consumer *cp1, *cp2, *tmp; 175 struct g_concat_disk *disk; 176 struct g_geom *gp; 177 struct g_concat_softc *sc; 178 int error; 179 180 g_topology_assert(); 181 gp = pp->geom; 182 sc = gp->softc; 183 184 /* On first open, grab an extra "exclusive" bit */ 185 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) 186 de++; 187 /* ... and let go of it on last close */ 188 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0) 189 de--; 190 191 sx_slock(&sc->sc_disks_lock); 192 LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) { 193 error = g_access(cp1, dr, dw, de); 194 if (error != 0) 195 goto fail; 196 disk = cp1->private; 197 if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 && 198 disk->d_removed) { 199 g_concat_remove_disk(disk); /* May destroy geom. */ 200 } 201 } 202 sx_sunlock(&sc->sc_disks_lock); 203 return (0); 204 205 fail: 206 sx_sunlock(&sc->sc_disks_lock); 207 LIST_FOREACH(cp2, &gp->consumer, consumer) { 208 if (cp1 == cp2) 209 break; 210 g_access(cp2, -dr, -dw, -de); 211 } 212 return (error); 213 } 214 215 static void 216 g_concat_candelete(struct bio *bp) 217 { 218 struct g_concat_softc *sc; 219 struct g_concat_disk *disk; 220 int val; 221 222 sc = bp->bio_to->geom->softc; 223 sx_assert(&sc->sc_disks_lock, SX_LOCKED); 224 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 225 if (!disk->d_removed && disk->d_candelete) 226 break; 227 } 228 val = disk != NULL; 229 g_handleattr(bp, "GEOM::candelete", &val, sizeof(val)); 230 } 231 232 static void 233 g_concat_kernel_dump(struct bio *bp) 234 { 235 struct g_concat_softc *sc; 236 struct g_concat_disk *disk; 237 struct bio *cbp; 238 struct g_kerneldump *gkd; 239 240 sc = bp->bio_to->geom->softc; 241 gkd = (struct g_kerneldump *)bp->bio_data; 242 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 243 if (disk->d_start <= gkd->offset && 244 disk->d_end > gkd->offset) 245 break; 246 } 247 if (disk == NULL) { 248 g_io_deliver(bp, EOPNOTSUPP); 249 return; 250 } 251 252 gkd->offset -= disk->d_start; 253 if (gkd->length > disk->d_end - disk->d_start - gkd->offset) 254 gkd->length = disk->d_end - disk->d_start - gkd->offset; 255 cbp = g_clone_bio(bp); 256 if (cbp == NULL) { 257 g_io_deliver(bp, ENOMEM); 258 return; 259 } 260 cbp->bio_done = g_std_done; 261 g_io_request(cbp, disk->d_consumer); 262 G_CONCAT_DEBUG(1, "Kernel dump will go to %s.", 263 disk->d_consumer->provider->name); 264 } 265 266 static void 267 g_concat_done(struct bio *bp) 268 { 269 struct g_concat_softc *sc; 270 struct bio *pbp; 271 272 pbp = bp->bio_parent; 273 sc = pbp->bio_to->geom->softc; 274 mtx_lock(&sc->sc_completion_lock); 275 if (pbp->bio_error == 0) 276 pbp->bio_error = bp->bio_error; 277 pbp->bio_completed += bp->bio_completed; 278 pbp->bio_inbed++; 279 if (pbp->bio_children == pbp->bio_inbed) { 280 mtx_unlock(&sc->sc_completion_lock); 281 g_io_deliver(pbp, pbp->bio_error); 282 } else 283 mtx_unlock(&sc->sc_completion_lock); 284 g_destroy_bio(bp); 285 } 286 287 /* 288 * Called for both BIO_FLUSH and BIO_SPEEDUP. Just pass the call down 289 */ 290 static void 291 g_concat_passdown(struct g_concat_softc *sc, struct bio *bp) 292 { 293 struct bio_queue_head queue; 294 struct g_consumer *cp; 295 struct bio *cbp; 296 struct g_concat_disk *disk; 297 298 sx_assert(&sc->sc_disks_lock, SX_LOCKED); 299 300 bioq_init(&queue); 301 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 302 cbp = g_clone_bio(bp); 303 if (cbp == NULL) { 304 while ((cbp = bioq_takefirst(&queue)) != NULL) 305 g_destroy_bio(cbp); 306 if (bp->bio_error == 0) 307 bp->bio_error = ENOMEM; 308 g_io_deliver(bp, bp->bio_error); 309 return; 310 } 311 bioq_insert_tail(&queue, cbp); 312 cbp->bio_done = g_concat_done; 313 cbp->bio_caller1 = disk->d_consumer; 314 cbp->bio_to = disk->d_consumer->provider; 315 } 316 while ((cbp = bioq_takefirst(&queue)) != NULL) { 317 G_CONCAT_LOGREQ(cbp, "Sending request."); 318 cp = cbp->bio_caller1; 319 cbp->bio_caller1 = NULL; 320 g_io_request(cbp, cp); 321 } 322 } 323 324 static void 325 g_concat_start(struct bio *bp) 326 { 327 struct bio_queue_head queue; 328 struct g_concat_softc *sc; 329 struct g_concat_disk *disk; 330 struct g_provider *pp; 331 off_t offset, end, length, off, len; 332 struct bio *cbp; 333 char *addr; 334 335 pp = bp->bio_to; 336 sc = pp->geom->softc; 337 /* 338 * If sc == NULL, provider's error should be set and g_concat_start() 339 * should not be called at all. 340 */ 341 KASSERT(sc != NULL, 342 ("Provider's error should be set (error=%d)(device=%s).", 343 bp->bio_to->error, bp->bio_to->name)); 344 345 G_CONCAT_LOGREQ(bp, "Request received."); 346 sx_slock(&sc->sc_disks_lock); 347 348 switch (bp->bio_cmd) { 349 case BIO_READ: 350 case BIO_WRITE: 351 case BIO_DELETE: 352 break; 353 case BIO_SPEEDUP: 354 case BIO_FLUSH: 355 g_concat_passdown(sc, bp); 356 goto end; 357 case BIO_GETATTR: 358 if (strcmp("GEOM::kerneldump", bp->bio_attribute) == 0) { 359 g_concat_kernel_dump(bp); 360 goto end; 361 } else if (strcmp("GEOM::candelete", bp->bio_attribute) == 0) { 362 g_concat_candelete(bp); 363 goto end; 364 } 365 /* To which provider it should be delivered? */ 366 /* FALLTHROUGH */ 367 default: 368 g_io_deliver(bp, EOPNOTSUPP); 369 goto end; 370 } 371 372 offset = bp->bio_offset; 373 length = bp->bio_length; 374 if ((bp->bio_flags & BIO_UNMAPPED) != 0) 375 addr = NULL; 376 else 377 addr = bp->bio_data; 378 end = offset + length; 379 380 bioq_init(&queue); 381 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 382 if (disk->d_end <= offset) 383 continue; 384 if (disk->d_start >= end) 385 break; 386 387 off = offset - disk->d_start; 388 len = MIN(length, disk->d_end - offset); 389 length -= len; 390 offset += len; 391 392 cbp = g_clone_bio(bp); 393 if (cbp == NULL) { 394 while ((cbp = bioq_takefirst(&queue)) != NULL) 395 g_destroy_bio(cbp); 396 if (bp->bio_error == 0) 397 bp->bio_error = ENOMEM; 398 g_io_deliver(bp, bp->bio_error); 399 goto end; 400 } 401 bioq_insert_tail(&queue, cbp); 402 /* 403 * Fill in the component buf structure. 404 */ 405 if (len == bp->bio_length) 406 cbp->bio_done = g_std_done; 407 else 408 cbp->bio_done = g_concat_done; 409 cbp->bio_offset = off; 410 cbp->bio_length = len; 411 if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 412 cbp->bio_ma_offset += (uintptr_t)addr; 413 cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE; 414 cbp->bio_ma_offset %= PAGE_SIZE; 415 cbp->bio_ma_n = round_page(cbp->bio_ma_offset + 416 cbp->bio_length) / PAGE_SIZE; 417 } else 418 cbp->bio_data = addr; 419 addr += len; 420 cbp->bio_to = disk->d_consumer->provider; 421 cbp->bio_caller1 = disk; 422 423 if (length == 0) 424 break; 425 } 426 KASSERT(length == 0, 427 ("Length is still greater than 0 (class=%s, name=%s).", 428 bp->bio_to->geom->class->name, bp->bio_to->geom->name)); 429 while ((cbp = bioq_takefirst(&queue)) != NULL) { 430 G_CONCAT_LOGREQ(cbp, "Sending request."); 431 disk = cbp->bio_caller1; 432 cbp->bio_caller1 = NULL; 433 g_io_request(cbp, disk->d_consumer); 434 } 435 end: 436 sx_sunlock(&sc->sc_disks_lock); 437 } 438 439 static void 440 g_concat_check_and_run(struct g_concat_softc *sc) 441 { 442 struct g_concat_disk *disk; 443 struct g_provider *dp, *pp; 444 u_int sectorsize = 0; 445 off_t start; 446 int error; 447 448 g_topology_assert(); 449 if (g_concat_nvalid(sc) != sc->sc_ndisks) 450 return; 451 452 pp = g_new_providerf(sc->sc_geom, "concat/%s", sc->sc_name); 453 pp->flags |= G_PF_DIRECT_SEND | G_PF_DIRECT_RECEIVE | 454 G_PF_ACCEPT_UNMAPPED; 455 start = 0; 456 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 457 dp = disk->d_consumer->provider; 458 disk->d_start = start; 459 disk->d_end = disk->d_start + dp->mediasize; 460 if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) 461 disk->d_end -= dp->sectorsize; 462 start = disk->d_end; 463 error = g_access(disk->d_consumer, 1, 0, 0); 464 if (error == 0) { 465 error = g_getattr("GEOM::candelete", disk->d_consumer, 466 &disk->d_candelete); 467 if (error != 0) 468 disk->d_candelete = 0; 469 (void)g_access(disk->d_consumer, -1, 0, 0); 470 } else 471 G_CONCAT_DEBUG(1, "Failed to access disk %s, error %d.", 472 dp->name, error); 473 if (disk == TAILQ_FIRST(&sc->sc_disks)) 474 sectorsize = dp->sectorsize; 475 else 476 sectorsize = lcm(sectorsize, dp->sectorsize); 477 478 /* A provider underneath us doesn't support unmapped */ 479 if ((dp->flags & G_PF_ACCEPT_UNMAPPED) == 0) { 480 G_CONCAT_DEBUG(1, "Cancelling unmapped " 481 "because of %s.", dp->name); 482 pp->flags &= ~G_PF_ACCEPT_UNMAPPED; 483 } 484 } 485 pp->sectorsize = sectorsize; 486 /* We have sc->sc_disks[sc->sc_ndisks - 1].d_end in 'start'. */ 487 pp->mediasize = start; 488 dp = TAILQ_FIRST(&sc->sc_disks)->d_consumer->provider; 489 pp->stripesize = dp->stripesize; 490 pp->stripeoffset = dp->stripeoffset; 491 sc->sc_provider = pp; 492 g_error_provider(pp, 0); 493 494 G_CONCAT_DEBUG(0, "Device %s activated.", sc->sc_provider->name); 495 } 496 497 static int 498 g_concat_read_metadata(struct g_consumer *cp, struct g_concat_metadata *md) 499 { 500 struct g_provider *pp; 501 u_char *buf; 502 int error; 503 504 g_topology_assert(); 505 506 error = g_access(cp, 1, 0, 0); 507 if (error != 0) 508 return (error); 509 pp = cp->provider; 510 g_topology_unlock(); 511 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 512 &error); 513 g_topology_lock(); 514 g_access(cp, -1, 0, 0); 515 if (buf == NULL) 516 return (error); 517 518 /* Decode metadata. */ 519 concat_metadata_decode(buf, md); 520 g_free(buf); 521 522 return (0); 523 } 524 525 /* 526 * Add disk to given device. 527 */ 528 static int 529 g_concat_add_disk(struct g_concat_softc *sc, struct g_provider *pp, u_int no) 530 { 531 struct g_concat_disk *disk; 532 struct g_consumer *cp, *fcp; 533 struct g_geom *gp; 534 int error; 535 536 g_topology_assert(); 537 538 sx_slock(&sc->sc_disks_lock); 539 540 /* Metadata corrupted? */ 541 if (no >= sc->sc_ndisks) { 542 sx_sunlock(&sc->sc_disks_lock); 543 return (EINVAL); 544 } 545 546 for (disk = TAILQ_FIRST(&sc->sc_disks); no > 0; no--) { 547 disk = TAILQ_NEXT(disk, d_next); 548 } 549 550 /* Check if disk is not already attached. */ 551 if (disk->d_consumer != NULL) { 552 sx_sunlock(&sc->sc_disks_lock); 553 return (EEXIST); 554 } 555 556 gp = sc->sc_geom; 557 fcp = LIST_FIRST(&gp->consumer); 558 559 cp = g_new_consumer(gp); 560 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 561 error = g_attach(cp, pp); 562 if (error != 0) { 563 sx_sunlock(&sc->sc_disks_lock); 564 g_destroy_consumer(cp); 565 return (error); 566 } 567 568 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) { 569 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace); 570 if (error != 0) { 571 sx_sunlock(&sc->sc_disks_lock); 572 g_detach(cp); 573 g_destroy_consumer(cp); 574 return (error); 575 } 576 } 577 if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) { 578 struct g_concat_metadata md; 579 580 // temporarily give up the lock to avoid lock order violation 581 // due to topology unlock in g_concat_read_metadata 582 sx_sunlock(&sc->sc_disks_lock); 583 /* Re-read metadata. */ 584 error = g_concat_read_metadata(cp, &md); 585 sx_slock(&sc->sc_disks_lock); 586 587 if (error != 0) 588 goto fail; 589 590 if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0 || 591 strcmp(md.md_name, sc->sc_name) != 0 || 592 md.md_id != sc->sc_id) { 593 G_CONCAT_DEBUG(0, "Metadata on %s changed.", pp->name); 594 goto fail; 595 } 596 597 disk->d_hardcoded = md.md_provider[0] != '\0'; 598 } else { 599 disk->d_hardcoded = false; 600 } 601 602 cp->private = disk; 603 disk->d_consumer = cp; 604 disk->d_softc = sc; 605 disk->d_start = 0; /* not yet */ 606 disk->d_end = 0; /* not yet */ 607 disk->d_removed = 0; 608 609 G_CONCAT_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name); 610 611 g_concat_check_and_run(sc); 612 sx_sunlock(&sc->sc_disks_lock); // need lock for check_and_run 613 614 return (0); 615 fail: 616 sx_sunlock(&sc->sc_disks_lock); 617 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) 618 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace); 619 g_detach(cp); 620 g_destroy_consumer(cp); 621 return (error); 622 } 623 624 static struct g_geom * 625 g_concat_create(struct g_class *mp, const struct g_concat_metadata *md, 626 u_int type) 627 { 628 struct g_concat_softc *sc; 629 struct g_concat_disk *disk; 630 struct g_geom *gp; 631 u_int no; 632 633 G_CONCAT_DEBUG(1, "Creating device %s (id=%u).", md->md_name, 634 md->md_id); 635 636 /* One disks is minimum. */ 637 if (md->md_all < 1) 638 return (NULL); 639 640 /* Check for duplicate unit */ 641 LIST_FOREACH(gp, &mp->geom, geom) { 642 sc = gp->softc; 643 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) { 644 G_CONCAT_DEBUG(0, "Device %s already configured.", 645 gp->name); 646 return (NULL); 647 } 648 } 649 gp = g_new_geomf(mp, "%s", md->md_name); 650 sc = malloc(sizeof(*sc), M_CONCAT, M_WAITOK | M_ZERO); 651 gp->start = g_concat_start; 652 gp->spoiled = g_concat_orphan; 653 gp->orphan = g_concat_orphan; 654 gp->access = g_concat_access; 655 gp->dumpconf = g_concat_dumpconf; 656 657 sc->sc_id = md->md_id; 658 sc->sc_ndisks = md->md_all; 659 TAILQ_INIT(&sc->sc_disks); 660 for (no = 0; no < sc->sc_ndisks; no++) { 661 disk = malloc(sizeof(*disk), M_CONCAT, M_WAITOK | M_ZERO); 662 TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next); 663 } 664 sc->sc_type = type; 665 mtx_init(&sc->sc_completion_lock, "gconcat lock", NULL, MTX_DEF); 666 sx_init(&sc->sc_disks_lock, "gconcat append lock"); 667 668 gp->softc = sc; 669 sc->sc_geom = gp; 670 sc->sc_provider = NULL; 671 672 G_CONCAT_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id); 673 674 return (gp); 675 } 676 677 static int 678 g_concat_destroy(struct g_concat_softc *sc, boolean_t force) 679 { 680 struct g_provider *pp; 681 struct g_consumer *cp, *cp1; 682 struct g_geom *gp; 683 struct g_concat_disk *disk; 684 685 g_topology_assert(); 686 687 if (sc == NULL) 688 return (ENXIO); 689 690 pp = sc->sc_provider; 691 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 692 if (force) { 693 G_CONCAT_DEBUG(0, "Device %s is still open, so it " 694 "can't be definitely removed.", pp->name); 695 } else { 696 G_CONCAT_DEBUG(1, 697 "Device %s is still open (r%dw%de%d).", pp->name, 698 pp->acr, pp->acw, pp->ace); 699 return (EBUSY); 700 } 701 } 702 703 gp = sc->sc_geom; 704 LIST_FOREACH_SAFE(cp, &gp->consumer, consumer, cp1) { 705 g_concat_remove_disk(cp->private); 706 if (cp1 == NULL) 707 return (0); /* Recursion happened. */ 708 } 709 if (!LIST_EMPTY(&gp->consumer)) 710 return (EINPROGRESS); 711 712 gp->softc = NULL; 713 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)", 714 gp->name)); 715 while ((disk = TAILQ_FIRST(&sc->sc_disks)) != NULL) { 716 TAILQ_REMOVE(&sc->sc_disks, disk, d_next); 717 free(disk, M_CONCAT); 718 } 719 mtx_destroy(&sc->sc_completion_lock); 720 sx_destroy(&sc->sc_disks_lock); 721 free(sc, M_CONCAT); 722 723 G_CONCAT_DEBUG(0, "Device %s destroyed.", gp->name); 724 g_wither_geom(gp, ENXIO); 725 return (0); 726 } 727 728 static int 729 g_concat_destroy_geom(struct gctl_req *req __unused, 730 struct g_class *mp __unused, struct g_geom *gp) 731 { 732 struct g_concat_softc *sc; 733 734 sc = gp->softc; 735 return (g_concat_destroy(sc, 0)); 736 } 737 738 static struct g_geom * 739 g_concat_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 740 { 741 struct g_concat_metadata md; 742 struct g_concat_softc *sc; 743 struct g_consumer *cp; 744 struct g_geom *gp; 745 int error; 746 747 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 748 g_topology_assert(); 749 750 /* Skip providers that are already open for writing. */ 751 if (pp->acw > 0) 752 return (NULL); 753 754 G_CONCAT_DEBUG(3, "Tasting %s.", pp->name); 755 756 gp = g_new_geomf(mp, "concat:taste"); 757 gp->start = g_concat_start; 758 gp->access = g_concat_access; 759 gp->orphan = g_concat_orphan; 760 cp = g_new_consumer(gp); 761 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 762 error = g_attach(cp, pp); 763 if (error == 0) { 764 error = g_concat_read_metadata(cp, &md); 765 g_detach(cp); 766 } 767 g_destroy_consumer(cp); 768 g_destroy_geom(gp); 769 if (error != 0) 770 return (NULL); 771 gp = NULL; 772 773 if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0) 774 return (NULL); 775 if (md.md_version > G_CONCAT_VERSION) { 776 printf("geom_concat.ko module is too old to handle %s.\n", 777 pp->name); 778 return (NULL); 779 } 780 /* 781 * Backward compatibility: 782 */ 783 /* There was no md_provider field in earlier versions of metadata. */ 784 if (md.md_version < 3) 785 bzero(md.md_provider, sizeof(md.md_provider)); 786 /* There was no md_provsize field in earlier versions of metadata. */ 787 if (md.md_version < 4) 788 md.md_provsize = pp->mediasize; 789 790 if (md.md_provider[0] != '\0' && 791 !g_compare_names(md.md_provider, pp->name)) 792 return (NULL); 793 if (md.md_provsize != pp->mediasize) 794 return (NULL); 795 796 /* 797 * Let's check if device already exists. 798 */ 799 sc = NULL; 800 LIST_FOREACH(gp, &mp->geom, geom) { 801 sc = gp->softc; 802 if (sc == NULL) 803 continue; 804 if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC) 805 continue; 806 if (strcmp(md.md_name, sc->sc_name) != 0) 807 continue; 808 if (md.md_id != sc->sc_id) 809 continue; 810 break; 811 } 812 if (gp != NULL) { 813 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 814 error = g_concat_add_disk(sc, pp, md.md_no); 815 if (error != 0) { 816 G_CONCAT_DEBUG(0, 817 "Cannot add disk %s to %s (error=%d).", pp->name, 818 gp->name, error); 819 return (NULL); 820 } 821 } else { 822 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC); 823 if (gp == NULL) { 824 G_CONCAT_DEBUG(0, "Cannot create device %s.", 825 md.md_name); 826 return (NULL); 827 } 828 sc = gp->softc; 829 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 830 error = g_concat_add_disk(sc, pp, md.md_no); 831 if (error != 0) { 832 G_CONCAT_DEBUG(0, 833 "Cannot add disk %s to %s (error=%d).", pp->name, 834 gp->name, error); 835 g_concat_destroy(sc, 1); 836 return (NULL); 837 } 838 } 839 840 return (gp); 841 } 842 843 static void 844 g_concat_ctl_create(struct gctl_req *req, struct g_class *mp) 845 { 846 u_int attached, no; 847 struct g_concat_metadata md; 848 struct g_provider *pp; 849 struct g_concat_softc *sc; 850 struct g_geom *gp; 851 struct sbuf *sb; 852 const char *name; 853 char param[16]; 854 int *nargs; 855 856 g_topology_assert(); 857 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 858 if (nargs == NULL) { 859 gctl_error(req, "No '%s' argument.", "nargs"); 860 return; 861 } 862 if (*nargs < 2) { 863 gctl_error(req, "Too few arguments."); 864 return; 865 } 866 867 bzero(&md, sizeof(md)); 868 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic)); 869 md.md_version = G_CONCAT_VERSION; 870 name = gctl_get_asciiparam(req, "arg0"); 871 if (name == NULL) { 872 gctl_error(req, "No 'arg%u' argument.", 0); 873 return; 874 } 875 strlcpy(md.md_name, name, sizeof(md.md_name)); 876 md.md_id = arc4random(); 877 md.md_no = 0; 878 md.md_all = *nargs - 1; 879 /* This field is not important here. */ 880 md.md_provsize = 0; 881 882 /* Check all providers are valid */ 883 for (no = 1; no < *nargs; no++) { 884 snprintf(param, sizeof(param), "arg%u", no); 885 pp = gctl_get_provider(req, param); 886 if (pp == NULL) 887 return; 888 } 889 890 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL); 891 if (gp == NULL) { 892 gctl_error(req, "Can't configure %s.", md.md_name); 893 return; 894 } 895 896 sc = gp->softc; 897 sb = sbuf_new_auto(); 898 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name); 899 for (attached = 0, no = 1; no < *nargs; no++) { 900 snprintf(param, sizeof(param), "arg%u", no); 901 pp = gctl_get_provider(req, param); 902 if (pp == NULL) { 903 name = gctl_get_asciiparam(req, param); 904 MPASS(name != NULL); 905 sbuf_printf(sb, " %s", name); 906 continue; 907 } 908 if (g_concat_add_disk(sc, pp, no - 1) != 0) { 909 G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.", 910 no, pp->name, gp->name); 911 sbuf_printf(sb, " %s", pp->name); 912 continue; 913 } 914 attached++; 915 } 916 sbuf_finish(sb); 917 if (md.md_all != attached) { 918 g_concat_destroy(gp->softc, 1); 919 gctl_error(req, "%s", sbuf_data(sb)); 920 } 921 sbuf_delete(sb); 922 } 923 924 static struct g_concat_softc * 925 g_concat_find_device(struct g_class *mp, const char *name) 926 { 927 struct g_concat_softc *sc; 928 struct g_geom *gp; 929 930 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0) 931 name += strlen(_PATH_DEV); 932 933 LIST_FOREACH(gp, &mp->geom, geom) { 934 sc = gp->softc; 935 if (sc == NULL) 936 continue; 937 if (strcmp(sc->sc_name, name) == 0) 938 return (sc); 939 } 940 return (NULL); 941 } 942 943 static void 944 g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp) 945 { 946 struct g_concat_softc *sc; 947 int *force, *nargs, error; 948 const char *name; 949 char param[16]; 950 u_int i; 951 952 g_topology_assert(); 953 954 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 955 if (nargs == NULL) { 956 gctl_error(req, "No '%s' argument.", "nargs"); 957 return; 958 } 959 if (*nargs <= 0) { 960 gctl_error(req, "Missing device(s)."); 961 return; 962 } 963 force = gctl_get_paraml(req, "force", sizeof(*force)); 964 if (force == NULL) { 965 gctl_error(req, "No '%s' argument.", "force"); 966 return; 967 } 968 969 for (i = 0; i < (u_int)*nargs; i++) { 970 snprintf(param, sizeof(param), "arg%u", i); 971 name = gctl_get_asciiparam(req, param); 972 if (name == NULL) { 973 gctl_error(req, "No 'arg%u' argument.", i); 974 return; 975 } 976 sc = g_concat_find_device(mp, name); 977 if (sc == NULL) { 978 gctl_error(req, "No such device: %s.", name); 979 return; 980 } 981 error = g_concat_destroy(sc, *force); 982 if (error != 0) { 983 gctl_error(req, "Cannot destroy device %s (error=%d).", 984 sc->sc_name, error); 985 return; 986 } 987 } 988 } 989 990 static struct g_concat_disk * 991 g_concat_find_disk(struct g_concat_softc *sc, const char *name) 992 { 993 struct g_concat_disk *disk; 994 995 sx_assert(&sc->sc_disks_lock, SX_LOCKED); 996 if (strncmp(name, "/dev/", 5) == 0) 997 name += 5; 998 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 999 if (disk->d_consumer == NULL) 1000 continue; 1001 if (disk->d_consumer->provider == NULL) 1002 continue; 1003 if (strcmp(disk->d_consumer->provider->name, name) == 0) 1004 return (disk); 1005 } 1006 return (NULL); 1007 } 1008 1009 static void 1010 g_concat_write_metadata(struct gctl_req *req, struct g_concat_softc *sc) 1011 { 1012 u_int no = 0; 1013 struct g_concat_disk *disk; 1014 struct g_concat_metadata md; 1015 struct g_provider *pp; 1016 u_char *sector; 1017 int error; 1018 1019 bzero(&md, sizeof(md)); 1020 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic)); 1021 md.md_version = G_CONCAT_VERSION; 1022 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name)); 1023 md.md_id = sc->sc_id; 1024 md.md_all = sc->sc_ndisks; 1025 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1026 pp = disk->d_consumer->provider; 1027 1028 md.md_no = no; 1029 if (disk->d_hardcoded) 1030 strlcpy(md.md_provider, pp->name, 1031 sizeof(md.md_provider)); 1032 md.md_provsize = disk->d_consumer->provider->mediasize; 1033 1034 sector = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 1035 concat_metadata_encode(&md, sector); 1036 error = g_access(disk->d_consumer, 0, 1, 0); 1037 if (error == 0) { 1038 error = g_write_data(disk->d_consumer, 1039 pp->mediasize - pp->sectorsize, sector, 1040 pp->sectorsize); 1041 (void)g_access(disk->d_consumer, 0, -1, 0); 1042 } 1043 g_free(sector); 1044 if (error != 0) 1045 gctl_error(req, "Cannot store metadata on %s: %d", 1046 pp->name, error); 1047 1048 no++; 1049 } 1050 } 1051 1052 static void 1053 g_concat_ctl_append(struct gctl_req *req, struct g_class *mp) 1054 { 1055 struct g_concat_softc *sc; 1056 struct g_consumer *cp, *fcp; 1057 struct g_provider *pp; 1058 struct g_geom *gp; 1059 const char *name, *cname; 1060 struct g_concat_disk *disk; 1061 int *nargs, *hardcode; 1062 int error; 1063 int disk_candelete; 1064 1065 g_topology_assert(); 1066 1067 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1068 if (nargs == NULL) { 1069 gctl_error(req, "No '%s' argument.", "nargs"); 1070 return; 1071 } 1072 if (*nargs != 2) { 1073 gctl_error(req, "Invalid number of arguments."); 1074 return; 1075 } 1076 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode)); 1077 if (hardcode == NULL) { 1078 gctl_error(req, "No '%s' argument.", "hardcode"); 1079 return; 1080 } 1081 1082 cname = gctl_get_asciiparam(req, "arg0"); 1083 if (cname == NULL) { 1084 gctl_error(req, "No 'arg%u' argument.", 0); 1085 return; 1086 } 1087 sc = g_concat_find_device(mp, cname); 1088 if (sc == NULL) { 1089 gctl_error(req, "No such device: %s.", cname); 1090 return; 1091 } 1092 if (sc->sc_provider == NULL) { 1093 /* 1094 * this won't race with g_concat_remove_disk as both 1095 * are holding the topology lock 1096 */ 1097 gctl_error(req, "Device not active, can't append: %s.", cname); 1098 return; 1099 } 1100 G_CONCAT_DEBUG(1, "Appending to %s:", cname); 1101 sx_xlock(&sc->sc_disks_lock); 1102 gp = sc->sc_geom; 1103 fcp = LIST_FIRST(&gp->consumer); 1104 1105 name = gctl_get_asciiparam(req, "arg1"); 1106 if (name == NULL) { 1107 gctl_error(req, "No 'arg%u' argument.", 1); 1108 goto fail; 1109 } 1110 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 1111 name += strlen("/dev/"); 1112 pp = g_provider_by_name(name); 1113 if (pp == NULL) { 1114 G_CONCAT_DEBUG(1, "Disk %s is invalid.", name); 1115 gctl_error(req, "Disk %s is invalid.", name); 1116 goto fail; 1117 } 1118 G_CONCAT_DEBUG(1, "Appending %s to this", name); 1119 1120 if (g_concat_find_disk(sc, name) != NULL) { 1121 gctl_error(req, "Disk %s already appended.", name); 1122 goto fail; 1123 } 1124 1125 if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) { 1126 gctl_error(req, "Providers sectorsize mismatch: %u vs %u", 1127 sc->sc_provider->sectorsize, pp->sectorsize); 1128 goto fail; 1129 } 1130 1131 cp = g_new_consumer(gp); 1132 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 1133 error = g_attach(cp, pp); 1134 if (error != 0) { 1135 g_destroy_consumer(cp); 1136 gctl_error(req, "Cannot open device %s (error=%d).", 1137 name, error); 1138 goto fail; 1139 } 1140 1141 error = g_access(cp, 1, 0, 0); 1142 if (error == 0) { 1143 error = g_getattr("GEOM::candelete", cp, &disk_candelete); 1144 if (error != 0) 1145 disk_candelete = 0; 1146 (void)g_access(cp, -1, 0, 0); 1147 } else 1148 G_CONCAT_DEBUG(1, "Failed to access disk %s, error %d.", name, error); 1149 1150 /* invoke g_access exactly as deep as all the other members currently are */ 1151 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) { 1152 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace); 1153 if (error != 0) { 1154 g_detach(cp); 1155 g_destroy_consumer(cp); 1156 gctl_error(req, "Failed to access disk %s (error=%d).", name, error); 1157 goto fail; 1158 } 1159 } 1160 1161 disk = malloc(sizeof(*disk), M_CONCAT, M_WAITOK | M_ZERO); 1162 disk->d_consumer = cp; 1163 disk->d_softc = sc; 1164 disk->d_start = TAILQ_LAST(&sc->sc_disks, g_concat_disks)->d_end; 1165 disk->d_end = disk->d_start + cp->provider->mediasize; 1166 disk->d_candelete = disk_candelete; 1167 disk->d_removed = 0; 1168 disk->d_hardcoded = *hardcode; 1169 cp->private = disk; 1170 TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next); 1171 sc->sc_ndisks++; 1172 1173 if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) { 1174 /* last sector is for metadata */ 1175 disk->d_end -= cp->provider->sectorsize; 1176 1177 /* update metadata on all parts */ 1178 g_concat_write_metadata(req, sc); 1179 } 1180 1181 g_resize_provider(sc->sc_provider, disk->d_end); 1182 1183 fail: 1184 sx_xunlock(&sc->sc_disks_lock); 1185 } 1186 1187 static void 1188 g_concat_config(struct gctl_req *req, struct g_class *mp, const char *verb) 1189 { 1190 uint32_t *version; 1191 1192 g_topology_assert(); 1193 1194 version = gctl_get_paraml(req, "version", sizeof(*version)); 1195 if (version == NULL) { 1196 gctl_error(req, "No '%s' argument.", "version"); 1197 return; 1198 } 1199 if (*version != G_CONCAT_VERSION) { 1200 gctl_error(req, "Userland and kernel parts are out of sync."); 1201 return; 1202 } 1203 1204 if (strcmp(verb, "create") == 0) { 1205 g_concat_ctl_create(req, mp); 1206 return; 1207 } else if (strcmp(verb, "destroy") == 0 || 1208 strcmp(verb, "stop") == 0) { 1209 g_concat_ctl_destroy(req, mp); 1210 return; 1211 } else if (strcmp(verb, "append") == 0) { 1212 g_concat_ctl_append(req, mp); 1213 return; 1214 } 1215 gctl_error(req, "Unknown verb."); 1216 } 1217 1218 static void 1219 g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1220 struct g_consumer *cp, struct g_provider *pp) 1221 { 1222 struct g_concat_softc *sc; 1223 1224 g_topology_assert(); 1225 sc = gp->softc; 1226 if (sc == NULL) 1227 return; 1228 1229 sx_slock(&sc->sc_disks_lock); 1230 if (pp != NULL) { 1231 /* Nothing here. */ 1232 } else if (cp != NULL) { 1233 struct g_concat_disk *disk; 1234 1235 disk = cp->private; 1236 if (disk == NULL) 1237 goto end; 1238 sbuf_printf(sb, "%s<End>%jd</End>\n", indent, 1239 (intmax_t)disk->d_end); 1240 sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent, 1241 (intmax_t)disk->d_start); 1242 } else { 1243 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id); 1244 sbuf_printf(sb, "%s<Type>", indent); 1245 switch (sc->sc_type) { 1246 case G_CONCAT_TYPE_AUTOMATIC: 1247 sbuf_cat(sb, "AUTOMATIC"); 1248 break; 1249 case G_CONCAT_TYPE_MANUAL: 1250 sbuf_cat(sb, "MANUAL"); 1251 break; 1252 default: 1253 sbuf_cat(sb, "UNKNOWN"); 1254 break; 1255 } 1256 sbuf_cat(sb, "</Type>\n"); 1257 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n", 1258 indent, sc->sc_ndisks, g_concat_nvalid(sc)); 1259 sbuf_printf(sb, "%s<State>", indent); 1260 if (sc->sc_provider != NULL && sc->sc_provider->error == 0) 1261 sbuf_cat(sb, "UP"); 1262 else 1263 sbuf_cat(sb, "DOWN"); 1264 sbuf_cat(sb, "</State>\n"); 1265 } 1266 end: 1267 sx_sunlock(&sc->sc_disks_lock); 1268 } 1269 1270 DECLARE_GEOM_CLASS(g_concat_class, g_concat); 1271 MODULE_VERSION(geom_concat, 0); 1272