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