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 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 764 error = g_attach(cp, pp); 765 if (error == 0) { 766 error = g_concat_read_metadata(cp, &md); 767 g_detach(cp); 768 } 769 g_destroy_consumer(cp); 770 g_destroy_geom(gp); 771 if (error != 0) 772 return (NULL); 773 gp = NULL; 774 775 if (strcmp(md.md_magic, G_CONCAT_MAGIC) != 0) 776 return (NULL); 777 if (md.md_version > G_CONCAT_VERSION) { 778 printf("geom_concat.ko module is too old to handle %s.\n", 779 pp->name); 780 return (NULL); 781 } 782 /* 783 * Backward compatibility: 784 */ 785 /* There was no md_provider field in earlier versions of metadata. */ 786 if (md.md_version < 3) 787 bzero(md.md_provider, sizeof(md.md_provider)); 788 /* There was no md_provsize field in earlier versions of metadata. */ 789 if (md.md_version < 4) 790 md.md_provsize = pp->mediasize; 791 792 if (md.md_provider[0] != '\0' && 793 !g_compare_names(md.md_provider, pp->name)) 794 return (NULL); 795 if (md.md_provsize != pp->mediasize) 796 return (NULL); 797 798 /* 799 * Let's check if device already exists. 800 */ 801 sc = NULL; 802 LIST_FOREACH(gp, &mp->geom, geom) { 803 sc = gp->softc; 804 if (sc == NULL) 805 continue; 806 if (sc->sc_type != G_CONCAT_TYPE_AUTOMATIC) 807 continue; 808 if (strcmp(md.md_name, sc->sc_name) != 0) 809 continue; 810 if (md.md_id != sc->sc_id) 811 continue; 812 break; 813 } 814 if (gp != NULL) { 815 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 816 error = g_concat_add_disk(sc, pp, md.md_no); 817 if (error != 0) { 818 G_CONCAT_DEBUG(0, 819 "Cannot add disk %s to %s (error=%d).", pp->name, 820 gp->name, error); 821 return (NULL); 822 } 823 } else { 824 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_AUTOMATIC); 825 if (gp == NULL) { 826 G_CONCAT_DEBUG(0, "Cannot create device %s.", 827 md.md_name); 828 return (NULL); 829 } 830 sc = gp->softc; 831 G_CONCAT_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 832 error = g_concat_add_disk(sc, pp, md.md_no); 833 if (error != 0) { 834 G_CONCAT_DEBUG(0, 835 "Cannot add disk %s to %s (error=%d).", pp->name, 836 gp->name, error); 837 g_concat_destroy(sc, 1); 838 return (NULL); 839 } 840 } 841 842 return (gp); 843 } 844 845 static void 846 g_concat_ctl_create(struct gctl_req *req, struct g_class *mp) 847 { 848 u_int attached, no; 849 struct g_concat_metadata md; 850 struct g_provider *pp; 851 struct g_concat_softc *sc; 852 struct g_geom *gp; 853 struct sbuf *sb; 854 const char *name; 855 char param[16]; 856 int *nargs; 857 858 g_topology_assert(); 859 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 860 if (nargs == NULL) { 861 gctl_error(req, "No '%s' argument.", "nargs"); 862 return; 863 } 864 if (*nargs < 2) { 865 gctl_error(req, "Too few arguments."); 866 return; 867 } 868 869 bzero(&md, sizeof(md)); 870 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic)); 871 md.md_version = G_CONCAT_VERSION; 872 name = gctl_get_asciiparam(req, "arg0"); 873 if (name == NULL) { 874 gctl_error(req, "No 'arg%u' argument.", 0); 875 return; 876 } 877 strlcpy(md.md_name, name, sizeof(md.md_name)); 878 md.md_id = arc4random(); 879 md.md_no = 0; 880 md.md_all = *nargs - 1; 881 /* This field is not important here. */ 882 md.md_provsize = 0; 883 884 /* Check all providers are valid */ 885 for (no = 1; no < *nargs; no++) { 886 snprintf(param, sizeof(param), "arg%u", no); 887 pp = gctl_get_provider(req, param); 888 if (pp == NULL) 889 return; 890 } 891 892 gp = g_concat_create(mp, &md, G_CONCAT_TYPE_MANUAL); 893 if (gp == NULL) { 894 gctl_error(req, "Can't configure %s.", md.md_name); 895 return; 896 } 897 898 sc = gp->softc; 899 sb = sbuf_new_auto(); 900 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name); 901 for (attached = 0, no = 1; no < *nargs; no++) { 902 snprintf(param, sizeof(param), "arg%u", no); 903 pp = gctl_get_provider(req, param); 904 if (pp == NULL) { 905 name = gctl_get_asciiparam(req, param); 906 MPASS(name != NULL); 907 sbuf_printf(sb, " %s", name); 908 continue; 909 } 910 if (g_concat_add_disk(sc, pp, no - 1) != 0) { 911 G_CONCAT_DEBUG(1, "Disk %u (%s) not attached to %s.", 912 no, pp->name, gp->name); 913 sbuf_printf(sb, " %s", pp->name); 914 continue; 915 } 916 attached++; 917 } 918 sbuf_finish(sb); 919 if (md.md_all != attached) { 920 g_concat_destroy(gp->softc, 1); 921 gctl_error(req, "%s", sbuf_data(sb)); 922 } 923 sbuf_delete(sb); 924 } 925 926 static struct g_concat_softc * 927 g_concat_find_device(struct g_class *mp, const char *name) 928 { 929 struct g_concat_softc *sc; 930 struct g_geom *gp; 931 932 if (strncmp(name, _PATH_DEV, strlen(_PATH_DEV)) == 0) 933 name += strlen(_PATH_DEV); 934 935 LIST_FOREACH(gp, &mp->geom, geom) { 936 sc = gp->softc; 937 if (sc == NULL) 938 continue; 939 if (strcmp(sc->sc_name, name) == 0) 940 return (sc); 941 } 942 return (NULL); 943 } 944 945 static void 946 g_concat_ctl_destroy(struct gctl_req *req, struct g_class *mp) 947 { 948 struct g_concat_softc *sc; 949 int *force, *nargs, error; 950 const char *name; 951 char param[16]; 952 u_int i; 953 954 g_topology_assert(); 955 956 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 957 if (nargs == NULL) { 958 gctl_error(req, "No '%s' argument.", "nargs"); 959 return; 960 } 961 if (*nargs <= 0) { 962 gctl_error(req, "Missing device(s)."); 963 return; 964 } 965 force = gctl_get_paraml(req, "force", sizeof(*force)); 966 if (force == NULL) { 967 gctl_error(req, "No '%s' argument.", "force"); 968 return; 969 } 970 971 for (i = 0; i < (u_int)*nargs; i++) { 972 snprintf(param, sizeof(param), "arg%u", i); 973 name = gctl_get_asciiparam(req, param); 974 if (name == NULL) { 975 gctl_error(req, "No 'arg%u' argument.", i); 976 return; 977 } 978 sc = g_concat_find_device(mp, name); 979 if (sc == NULL) { 980 gctl_error(req, "No such device: %s.", name); 981 return; 982 } 983 error = g_concat_destroy(sc, *force); 984 if (error != 0) { 985 gctl_error(req, "Cannot destroy device %s (error=%d).", 986 sc->sc_name, error); 987 return; 988 } 989 } 990 } 991 992 static struct g_concat_disk * 993 g_concat_find_disk(struct g_concat_softc *sc, const char *name) 994 { 995 struct g_concat_disk *disk; 996 997 sx_assert(&sc->sc_disks_lock, SX_LOCKED); 998 if (strncmp(name, "/dev/", 5) == 0) 999 name += 5; 1000 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1001 if (disk->d_consumer == NULL) 1002 continue; 1003 if (disk->d_consumer->provider == NULL) 1004 continue; 1005 if (strcmp(disk->d_consumer->provider->name, name) == 0) 1006 return (disk); 1007 } 1008 return (NULL); 1009 } 1010 1011 static void 1012 g_concat_write_metadata(struct gctl_req *req, struct g_concat_softc *sc) 1013 { 1014 u_int no = 0; 1015 struct g_concat_disk *disk; 1016 struct g_concat_metadata md; 1017 struct g_provider *pp; 1018 u_char *sector; 1019 int error; 1020 1021 bzero(&md, sizeof(md)); 1022 strlcpy(md.md_magic, G_CONCAT_MAGIC, sizeof(md.md_magic)); 1023 md.md_version = G_CONCAT_VERSION; 1024 strlcpy(md.md_name, sc->sc_name, sizeof(md.md_name)); 1025 md.md_id = sc->sc_id; 1026 md.md_all = sc->sc_ndisks; 1027 TAILQ_FOREACH(disk, &sc->sc_disks, d_next) { 1028 pp = disk->d_consumer->provider; 1029 1030 md.md_no = no; 1031 if (disk->d_hardcoded) 1032 strlcpy(md.md_provider, pp->name, 1033 sizeof(md.md_provider)); 1034 md.md_provsize = disk->d_consumer->provider->mediasize; 1035 1036 sector = g_malloc(pp->sectorsize, M_WAITOK | M_ZERO); 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, 1041 pp->mediasize - pp->sectorsize, sector, 1042 pp->sectorsize); 1043 (void)g_access(disk->d_consumer, 0, -1, 0); 1044 } 1045 g_free(sector); 1046 if (error != 0) 1047 gctl_error(req, "Cannot store metadata on %s: %d", 1048 pp->name, error); 1049 1050 no++; 1051 } 1052 } 1053 1054 static void 1055 g_concat_ctl_append(struct gctl_req *req, struct g_class *mp) 1056 { 1057 struct g_concat_softc *sc; 1058 struct g_consumer *cp, *fcp; 1059 struct g_provider *pp; 1060 struct g_geom *gp; 1061 const char *name, *cname; 1062 struct g_concat_disk *disk; 1063 int *nargs, *hardcode; 1064 int error; 1065 int disk_candelete; 1066 1067 g_topology_assert(); 1068 1069 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1070 if (nargs == NULL) { 1071 gctl_error(req, "No '%s' argument.", "nargs"); 1072 return; 1073 } 1074 if (*nargs != 2) { 1075 gctl_error(req, "Invalid number of arguments."); 1076 return; 1077 } 1078 hardcode = gctl_get_paraml(req, "hardcode", sizeof(*hardcode)); 1079 if (hardcode == NULL) { 1080 gctl_error(req, "No '%s' argument.", "hardcode"); 1081 return; 1082 } 1083 1084 cname = gctl_get_asciiparam(req, "arg0"); 1085 if (cname == NULL) { 1086 gctl_error(req, "No 'arg%u' argument.", 0); 1087 return; 1088 } 1089 sc = g_concat_find_device(mp, cname); 1090 if (sc == NULL) { 1091 gctl_error(req, "No such device: %s.", cname); 1092 return; 1093 } 1094 if (sc->sc_provider == NULL) { 1095 /* 1096 * this won't race with g_concat_remove_disk as both 1097 * are holding the topology lock 1098 */ 1099 gctl_error(req, "Device not active, can't append: %s.", cname); 1100 return; 1101 } 1102 G_CONCAT_DEBUG(1, "Appending to %s:", cname); 1103 sx_xlock(&sc->sc_disks_lock); 1104 gp = sc->sc_geom; 1105 fcp = LIST_FIRST(&gp->consumer); 1106 1107 name = gctl_get_asciiparam(req, "arg1"); 1108 if (name == NULL) { 1109 gctl_error(req, "No 'arg%u' argument.", 1); 1110 goto fail; 1111 } 1112 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 1113 name += strlen("/dev/"); 1114 pp = g_provider_by_name(name); 1115 if (pp == NULL) { 1116 G_CONCAT_DEBUG(1, "Disk %s is invalid.", name); 1117 gctl_error(req, "Disk %s is invalid.", name); 1118 goto fail; 1119 } 1120 G_CONCAT_DEBUG(1, "Appending %s to this", name); 1121 1122 if (g_concat_find_disk(sc, name) != NULL) { 1123 gctl_error(req, "Disk %s already appended.", name); 1124 goto fail; 1125 } 1126 1127 if ((sc->sc_provider->sectorsize % pp->sectorsize) != 0) { 1128 gctl_error(req, "Providers sectorsize mismatch: %u vs %u", 1129 sc->sc_provider->sectorsize, pp->sectorsize); 1130 goto fail; 1131 } 1132 1133 cp = g_new_consumer(gp); 1134 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 1135 error = g_attach(cp, pp); 1136 if (error != 0) { 1137 g_destroy_consumer(cp); 1138 gctl_error(req, "Cannot open device %s (error=%d).", 1139 name, error); 1140 goto fail; 1141 } 1142 1143 error = g_access(cp, 1, 0, 0); 1144 if (error == 0) { 1145 error = g_getattr("GEOM::candelete", cp, &disk_candelete); 1146 if (error != 0) 1147 disk_candelete = 0; 1148 (void)g_access(cp, -1, 0, 0); 1149 } else 1150 G_CONCAT_DEBUG(1, "Failed to access disk %s, error %d.", name, error); 1151 1152 /* invoke g_access exactly as deep as all the other members currently are */ 1153 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) { 1154 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace); 1155 if (error != 0) { 1156 g_detach(cp); 1157 g_destroy_consumer(cp); 1158 gctl_error(req, "Failed to access disk %s (error=%d).", name, error); 1159 goto fail; 1160 } 1161 } 1162 1163 disk = malloc(sizeof(*disk), M_CONCAT, M_WAITOK | M_ZERO); 1164 disk->d_consumer = cp; 1165 disk->d_softc = sc; 1166 disk->d_start = TAILQ_LAST(&sc->sc_disks, g_concat_disks)->d_end; 1167 disk->d_end = disk->d_start + cp->provider->mediasize; 1168 disk->d_candelete = disk_candelete; 1169 disk->d_removed = 0; 1170 disk->d_hardcoded = *hardcode; 1171 cp->private = disk; 1172 TAILQ_INSERT_TAIL(&sc->sc_disks, disk, d_next); 1173 sc->sc_ndisks++; 1174 1175 if (sc->sc_type == G_CONCAT_TYPE_AUTOMATIC) { 1176 /* last sector is for metadata */ 1177 disk->d_end -= cp->provider->sectorsize; 1178 1179 /* update metadata on all parts */ 1180 g_concat_write_metadata(req, sc); 1181 } 1182 1183 g_resize_provider(sc->sc_provider, disk->d_end); 1184 1185 fail: 1186 sx_xunlock(&sc->sc_disks_lock); 1187 } 1188 1189 static void 1190 g_concat_config(struct gctl_req *req, struct g_class *mp, const char *verb) 1191 { 1192 uint32_t *version; 1193 1194 g_topology_assert(); 1195 1196 version = gctl_get_paraml(req, "version", sizeof(*version)); 1197 if (version == NULL) { 1198 gctl_error(req, "No '%s' argument.", "version"); 1199 return; 1200 } 1201 if (*version != G_CONCAT_VERSION) { 1202 gctl_error(req, "Userland and kernel parts are out of sync."); 1203 return; 1204 } 1205 1206 if (strcmp(verb, "create") == 0) { 1207 g_concat_ctl_create(req, mp); 1208 return; 1209 } else if (strcmp(verb, "destroy") == 0 || 1210 strcmp(verb, "stop") == 0) { 1211 g_concat_ctl_destroy(req, mp); 1212 return; 1213 } else if (strcmp(verb, "append") == 0) { 1214 g_concat_ctl_append(req, mp); 1215 return; 1216 } 1217 gctl_error(req, "Unknown verb."); 1218 } 1219 1220 static void 1221 g_concat_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1222 struct g_consumer *cp, struct g_provider *pp) 1223 { 1224 struct g_concat_softc *sc; 1225 1226 g_topology_assert(); 1227 sc = gp->softc; 1228 if (sc == NULL) 1229 return; 1230 1231 sx_slock(&sc->sc_disks_lock); 1232 if (pp != NULL) { 1233 /* Nothing here. */ 1234 } else if (cp != NULL) { 1235 struct g_concat_disk *disk; 1236 1237 disk = cp->private; 1238 if (disk == NULL) 1239 goto end; 1240 sbuf_printf(sb, "%s<End>%jd</End>\n", indent, 1241 (intmax_t)disk->d_end); 1242 sbuf_printf(sb, "%s<Start>%jd</Start>\n", indent, 1243 (intmax_t)disk->d_start); 1244 } else { 1245 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id); 1246 sbuf_printf(sb, "%s<Type>", indent); 1247 switch (sc->sc_type) { 1248 case G_CONCAT_TYPE_AUTOMATIC: 1249 sbuf_cat(sb, "AUTOMATIC"); 1250 break; 1251 case G_CONCAT_TYPE_MANUAL: 1252 sbuf_cat(sb, "MANUAL"); 1253 break; 1254 default: 1255 sbuf_cat(sb, "UNKNOWN"); 1256 break; 1257 } 1258 sbuf_cat(sb, "</Type>\n"); 1259 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n", 1260 indent, sc->sc_ndisks, g_concat_nvalid(sc)); 1261 sbuf_printf(sb, "%s<State>", indent); 1262 if (sc->sc_provider != NULL && sc->sc_provider->error == 0) 1263 sbuf_cat(sb, "UP"); 1264 else 1265 sbuf_cat(sb, "DOWN"); 1266 sbuf_cat(sb, "</State>\n"); 1267 } 1268 end: 1269 sx_sunlock(&sc->sc_disks_lock); 1270 } 1271 1272 DECLARE_GEOM_CLASS(g_concat_class, g_concat); 1273 MODULE_VERSION(geom_concat, 0); 1274