1 /*- 2 * Copyright (c) 2003 Pawel Jakub Dawidek <pjd@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/module.h> 34 #include <sys/lock.h> 35 #include <sys/mutex.h> 36 #include <sys/bio.h> 37 #include <sys/sysctl.h> 38 #include <sys/malloc.h> 39 #include <vm/uma.h> 40 #include <geom/geom.h> 41 #include <geom/stripe/g_stripe.h> 42 43 44 #define MAX_IO_SIZE (DFLTPHYS * 2) 45 static MALLOC_DEFINE(M_STRIPE, "stripe data", "GEOM_STRIPE Data"); 46 47 static uma_zone_t g_stripe_zone; 48 49 static int g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force); 50 static int g_stripe_destroy_geom(struct gctl_req *req, struct g_class *mp, 51 struct g_geom *gp); 52 53 static g_taste_t g_stripe_taste; 54 static g_ctl_req_t g_stripe_config; 55 static g_dumpconf_t g_stripe_dumpconf; 56 static g_init_t g_stripe_init; 57 static g_fini_t g_stripe_fini; 58 59 struct g_class g_stripe_class = { 60 .name = G_STRIPE_CLASS_NAME, 61 .version = G_VERSION, 62 .ctlreq = g_stripe_config, 63 .taste = g_stripe_taste, 64 .destroy_geom = g_stripe_destroy_geom, 65 .init = g_stripe_init, 66 .fini = g_stripe_fini 67 }; 68 69 SYSCTL_DECL(_kern_geom); 70 SYSCTL_NODE(_kern_geom, OID_AUTO, stripe, CTLFLAG_RW, 0, "GEOM_STRIPE stuff"); 71 static u_int g_stripe_debug = 0; 72 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, debug, CTLFLAG_RW, &g_stripe_debug, 0, 73 "Debug level"); 74 static int g_stripe_fast = 1; 75 TUNABLE_INT("kern.geom.stripe.fast", &g_stripe_fast); 76 static int 77 g_sysctl_stripe_fast(SYSCTL_HANDLER_ARGS) 78 { 79 int error, fast; 80 81 fast = g_stripe_fast; 82 error = sysctl_handle_int(oidp, &fast, sizeof(fast), req); 83 if (error == 0 && req->newptr != NULL) 84 g_stripe_fast = fast; 85 return (error); 86 } 87 SYSCTL_PROC(_kern_geom_stripe, OID_AUTO, fast, CTLTYPE_INT | CTLFLAG_RW, 88 NULL, 0, g_sysctl_stripe_fast, "I", "Fast, but memory-consuming, mode"); 89 static u_int g_stripe_maxmem = MAX_IO_SIZE * 10; 90 TUNABLE_INT("kern.geom.stripe.maxmem", &g_stripe_maxmem); 91 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, maxmem, CTLFLAG_RD, &g_stripe_maxmem, 92 0, "Maximum memory that can be allocated in \"fast\" mode (in bytes)"); 93 static u_int g_stripe_fast_failed = 0; 94 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, fast_failed, CTLFLAG_RD, 95 &g_stripe_fast_failed, 0, "How many times \"fast\" mode failed"); 96 97 /* 98 * Greatest Common Divisor. 99 */ 100 static u_int 101 gcd(u_int a, u_int b) 102 { 103 u_int c; 104 105 while (b != 0) { 106 c = a; 107 a = b; 108 b = (c % b); 109 } 110 return (a); 111 } 112 113 /* 114 * Least Common Multiple. 115 */ 116 static u_int 117 lcm(u_int a, u_int b) 118 { 119 120 return ((a * b) / gcd(a, b)); 121 } 122 123 static void 124 g_stripe_init(struct g_class *mp __unused) 125 { 126 127 g_stripe_zone = uma_zcreate("g_stripe_zone", MAX_IO_SIZE, NULL, NULL, 128 NULL, NULL, 0, 0); 129 g_stripe_maxmem -= g_stripe_maxmem % MAX_IO_SIZE; 130 uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / MAX_IO_SIZE); 131 } 132 133 static void 134 g_stripe_fini(struct g_class *mp __unused) 135 { 136 137 uma_zdestroy(g_stripe_zone); 138 } 139 140 /* 141 * Return the number of valid disks. 142 */ 143 static u_int 144 g_stripe_nvalid(struct g_stripe_softc *sc) 145 { 146 u_int i, no; 147 148 no = 0; 149 for (i = 0; i < sc->sc_ndisks; i++) { 150 if (sc->sc_disks[i] != NULL) 151 no++; 152 } 153 154 return (no); 155 } 156 157 static void 158 g_stripe_remove_disk(struct g_consumer *cp) 159 { 160 struct g_stripe_softc *sc; 161 u_int no; 162 163 KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__)); 164 sc = (struct g_stripe_softc *)cp->private; 165 KASSERT(sc != NULL, ("NULL sc in %s.", __func__)); 166 no = cp->index; 167 168 G_STRIPE_DEBUG(0, "Disk %s removed from %s.", cp->provider->name, 169 sc->sc_name); 170 171 sc->sc_disks[no] = NULL; 172 if (sc->sc_provider != NULL) { 173 g_orphan_provider(sc->sc_provider, ENXIO); 174 sc->sc_provider = NULL; 175 G_STRIPE_DEBUG(0, "Device %s removed.", sc->sc_name); 176 } 177 178 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 179 g_access(cp, -cp->acr, -cp->acw, -cp->ace); 180 g_detach(cp); 181 g_destroy_consumer(cp); 182 } 183 184 static void 185 g_stripe_orphan(struct g_consumer *cp) 186 { 187 struct g_stripe_softc *sc; 188 struct g_geom *gp; 189 190 g_topology_assert(); 191 gp = cp->geom; 192 sc = gp->softc; 193 if (sc == NULL) 194 return; 195 196 g_stripe_remove_disk(cp); 197 /* If there are no valid disks anymore, remove device. */ 198 if (g_stripe_nvalid(sc) == 0) 199 g_stripe_destroy(sc, 1); 200 } 201 202 static int 203 g_stripe_access(struct g_provider *pp, int dr, int dw, int de) 204 { 205 struct g_consumer *cp1, *cp2; 206 struct g_stripe_softc *sc; 207 struct g_geom *gp; 208 int error; 209 210 gp = pp->geom; 211 sc = gp->softc; 212 213 if (sc == NULL) { 214 /* 215 * It looks like geom is being withered. 216 * In that case we allow only negative requests. 217 */ 218 KASSERT(dr <= 0 && dw <= 0 && de <= 0, 219 ("Positive access request (device=%s).", pp->name)); 220 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && 221 (pp->ace + de) == 0) { 222 G_STRIPE_DEBUG(0, "Device %s definitely destroyed.", 223 gp->name); 224 } 225 return (0); 226 } 227 228 /* On first open, grab an extra "exclusive" bit */ 229 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) 230 de++; 231 /* ... and let go of it on last close */ 232 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0) 233 de--; 234 235 error = ENXIO; 236 LIST_FOREACH(cp1, &gp->consumer, consumer) { 237 error = g_access(cp1, dr, dw, de); 238 if (error == 0) 239 continue; 240 /* 241 * If we fail here, backout all previous changes. 242 */ 243 LIST_FOREACH(cp2, &gp->consumer, consumer) { 244 if (cp1 == cp2) 245 return (error); 246 g_access(cp2, -dr, -dw, -de); 247 } 248 /* NOTREACHED */ 249 } 250 251 return (error); 252 } 253 254 static void 255 g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset, 256 off_t length, int mode) 257 { 258 u_int stripesize; 259 size_t len; 260 261 stripesize = sc->sc_stripesize; 262 len = (size_t)(stripesize - (offset & (stripesize - 1))); 263 do { 264 bcopy(src, dst, len); 265 if (mode) { 266 dst += len + stripesize * (sc->sc_ndisks - 1); 267 src += len; 268 } else { 269 dst += len; 270 src += len + stripesize * (sc->sc_ndisks - 1); 271 } 272 length -= len; 273 KASSERT(length >= 0, 274 ("Length < 0 (stripesize=%zu, offset=%jd, length=%jd).", 275 (size_t)stripesize, (intmax_t)offset, (intmax_t)length)); 276 if (length > stripesize) 277 len = stripesize; 278 else 279 len = length; 280 } while (length > 0); 281 } 282 283 static void 284 g_stripe_done(struct bio *bp) 285 { 286 struct g_stripe_softc *sc; 287 struct bio *pbp; 288 289 pbp = bp->bio_parent; 290 sc = pbp->bio_to->geom->softc; 291 if (pbp->bio_error == 0) 292 pbp->bio_error = bp->bio_error; 293 pbp->bio_completed += bp->bio_completed; 294 if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) { 295 g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset, 296 bp->bio_length, 1); 297 bp->bio_data = bp->bio_caller1; 298 bp->bio_caller1 = NULL; 299 } 300 g_destroy_bio(bp); 301 pbp->bio_inbed++; 302 if (pbp->bio_children == pbp->bio_inbed) { 303 if (pbp->bio_driver1 != NULL) 304 uma_zfree(g_stripe_zone, pbp->bio_driver1); 305 g_io_deliver(pbp, pbp->bio_error); 306 } 307 } 308 309 static int 310 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length) 311 { 312 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 313 u_int nparts = 0, stripesize; 314 struct g_stripe_softc *sc; 315 char *addr, *data = NULL; 316 struct bio *cbp; 317 int error; 318 319 sc = bp->bio_to->geom->softc; 320 321 addr = bp->bio_data; 322 stripesize = sc->sc_stripesize; 323 324 cbp = g_clone_bio(bp); 325 if (cbp == NULL) { 326 error = ENOMEM; 327 goto failure; 328 } 329 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 330 nparts++; 331 /* 332 * Fill in the component buf structure. 333 */ 334 cbp->bio_done = g_stripe_done; 335 cbp->bio_offset = offset; 336 cbp->bio_data = addr; 337 cbp->bio_caller1 = NULL; 338 cbp->bio_length = length; 339 cbp->bio_caller2 = sc->sc_disks[no]; 340 341 /* offset -= offset % stripesize; */ 342 offset -= offset & (stripesize - 1); 343 addr += length; 344 length = bp->bio_length - length; 345 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) { 346 if (no > sc->sc_ndisks - 1) { 347 no = 0; 348 offset += stripesize; 349 } 350 if (nparts >= sc->sc_ndisks) { 351 cbp = TAILQ_NEXT(cbp, bio_queue); 352 if (cbp == NULL) 353 cbp = TAILQ_FIRST(&queue); 354 nparts++; 355 /* 356 * Update bio structure. 357 */ 358 /* 359 * MIN() is in case when 360 * (bp->bio_length % sc->sc_stripesize) != 0. 361 */ 362 cbp->bio_length += MIN(stripesize, length); 363 if (cbp->bio_caller1 == NULL) { 364 cbp->bio_caller1 = cbp->bio_data; 365 cbp->bio_data = NULL; 366 if (data == NULL) { 367 data = uma_zalloc(g_stripe_zone, 368 M_NOWAIT); 369 if (data == NULL) { 370 error = ENOMEM; 371 goto failure; 372 } 373 } 374 } 375 } else { 376 cbp = g_clone_bio(bp); 377 if (cbp == NULL) { 378 error = ENOMEM; 379 goto failure; 380 } 381 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 382 nparts++; 383 /* 384 * Fill in the component buf structure. 385 */ 386 cbp->bio_done = g_stripe_done; 387 cbp->bio_offset = offset; 388 cbp->bio_data = addr; 389 cbp->bio_caller1 = NULL; 390 /* 391 * MIN() is in case when 392 * (bp->bio_length % sc->sc_stripesize) != 0. 393 */ 394 cbp->bio_length = MIN(stripesize, length); 395 cbp->bio_caller2 = sc->sc_disks[no]; 396 } 397 } 398 if (data != NULL) 399 bp->bio_caller1 = data; 400 /* 401 * Fire off all allocated requests! 402 */ 403 while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 404 struct g_consumer *cp; 405 406 TAILQ_REMOVE(&queue, cbp, bio_queue); 407 cp = cbp->bio_caller2; 408 cbp->bio_caller2 = NULL; 409 cbp->bio_to = cp->provider; 410 if (cbp->bio_caller1 != NULL) { 411 cbp->bio_data = data; 412 if (bp->bio_cmd == BIO_WRITE) { 413 g_stripe_copy(sc, cbp->bio_caller1, data, 414 cbp->bio_offset, cbp->bio_length, 0); 415 } 416 data += cbp->bio_length; 417 } 418 G_STRIPE_LOGREQ(cbp, "Sending request."); 419 g_io_request(cbp, cp); 420 } 421 return (0); 422 failure: 423 if (data != NULL) 424 uma_zfree(g_stripe_zone, data); 425 while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 426 TAILQ_REMOVE(&queue, cbp, bio_queue); 427 if (cbp->bio_caller1 != NULL) { 428 cbp->bio_data = cbp->bio_caller1; 429 cbp->bio_caller1 = NULL; 430 } 431 bp->bio_children--; 432 g_destroy_bio(cbp); 433 } 434 return (error); 435 } 436 437 static int 438 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length) 439 { 440 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 441 struct g_stripe_softc *sc; 442 uint32_t stripesize; 443 struct bio *cbp; 444 char *addr; 445 int error; 446 447 sc = bp->bio_to->geom->softc; 448 449 addr = bp->bio_data; 450 stripesize = sc->sc_stripesize; 451 452 cbp = g_clone_bio(bp); 453 if (cbp == NULL) { 454 error = ENOMEM; 455 goto failure; 456 } 457 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 458 /* 459 * Fill in the component buf structure. 460 */ 461 cbp->bio_done = g_std_done; 462 cbp->bio_offset = offset; 463 cbp->bio_data = addr; 464 cbp->bio_length = length; 465 cbp->bio_caller2 = sc->sc_disks[no]; 466 467 /* offset -= offset % stripesize; */ 468 offset -= offset & (stripesize - 1); 469 addr += length; 470 length = bp->bio_length - length; 471 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) { 472 if (no > sc->sc_ndisks - 1) { 473 no = 0; 474 offset += stripesize; 475 } 476 cbp = g_clone_bio(bp); 477 if (cbp == NULL) { 478 error = ENOMEM; 479 goto failure; 480 } 481 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 482 483 /* 484 * Fill in the component buf structure. 485 */ 486 cbp->bio_done = g_std_done; 487 cbp->bio_offset = offset; 488 cbp->bio_data = addr; 489 /* 490 * MIN() is in case when 491 * (bp->bio_length % sc->sc_stripesize) != 0. 492 */ 493 cbp->bio_length = MIN(stripesize, length); 494 495 cbp->bio_caller2 = sc->sc_disks[no]; 496 } 497 /* 498 * Fire off all allocated requests! 499 */ 500 while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 501 struct g_consumer *cp; 502 503 TAILQ_REMOVE(&queue, cbp, bio_queue); 504 cp = cbp->bio_caller2; 505 cbp->bio_caller2 = NULL; 506 cbp->bio_to = cp->provider; 507 G_STRIPE_LOGREQ(cbp, "Sending request."); 508 g_io_request(cbp, cp); 509 } 510 return (0); 511 failure: 512 while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 513 TAILQ_REMOVE(&queue, cbp, bio_queue); 514 bp->bio_children--; 515 g_destroy_bio(cbp); 516 } 517 return (error); 518 } 519 520 static void 521 g_stripe_start(struct bio *bp) 522 { 523 off_t offset, start, length, nstripe; 524 struct g_stripe_softc *sc; 525 u_int no, stripesize; 526 int error, fast = 0; 527 528 sc = bp->bio_to->geom->softc; 529 /* 530 * If sc == NULL, provider's error should be set and g_stripe_start() 531 * should not be called at all. 532 */ 533 KASSERT(sc != NULL, 534 ("Provider's error should be set (error=%d)(device=%s).", 535 bp->bio_to->error, bp->bio_to->name)); 536 537 G_STRIPE_LOGREQ(bp, "Request received."); 538 539 switch (bp->bio_cmd) { 540 case BIO_READ: 541 case BIO_WRITE: 542 case BIO_DELETE: 543 /* 544 * Only those requests are supported. 545 */ 546 break; 547 case BIO_GETATTR: 548 /* To which provider it should be delivered? */ 549 default: 550 g_io_deliver(bp, EOPNOTSUPP); 551 return; 552 } 553 554 stripesize = sc->sc_stripesize; 555 556 /* 557 * Calculations are quite messy, but fast I hope. 558 */ 559 560 /* Stripe number. */ 561 /* nstripe = bp->bio_offset / stripesize; */ 562 nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits; 563 /* Disk number. */ 564 no = nstripe % sc->sc_ndisks; 565 /* Start position in stripe. */ 566 /* start = bp->bio_offset % stripesize; */ 567 start = bp->bio_offset & (stripesize - 1); 568 /* Start position in disk. */ 569 /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */ 570 offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start; 571 /* Length of data to operate. */ 572 length = MIN(bp->bio_length, stripesize - start); 573 574 /* 575 * Do use "fast" mode when: 576 * 1. "Fast" mode is ON. 577 * and 578 * 2. Request size is less than or equal to MAX_IO_SIZE (128kB), 579 * which should always be true. 580 * and 581 * 3. Request size is bigger than stripesize * ndisks. If it isn't, 582 * there will be no need to send more than one I/O request to 583 * a provider, so there is nothing to optmize. 584 */ 585 if (g_stripe_fast && bp->bio_length <= MAX_IO_SIZE && 586 bp->bio_length >= stripesize * sc->sc_ndisks) { 587 fast = 1; 588 } 589 error = 0; 590 if (fast) { 591 error = g_stripe_start_fast(bp, no, offset, length); 592 if (error != 0) 593 g_stripe_fast_failed++; 594 } 595 /* 596 * Do use "economic" when: 597 * 1. "Economic" mode is ON. 598 * or 599 * 2. "Fast" mode failed. It can only failed if there is no memory. 600 */ 601 if (!fast || error != 0) 602 error = g_stripe_start_economic(bp, no, offset, length); 603 if (error != 0) { 604 if (bp->bio_error == 0) 605 bp->bio_error = error; 606 g_io_deliver(bp, bp->bio_error); 607 } 608 } 609 610 static void 611 g_stripe_check_and_run(struct g_stripe_softc *sc) 612 { 613 off_t mediasize, ms; 614 u_int no, sectorsize = 0; 615 616 if (g_stripe_nvalid(sc) != sc->sc_ndisks) 617 return; 618 619 sc->sc_provider = g_new_providerf(sc->sc_geom, "stripe/%s", 620 sc->sc_name); 621 /* 622 * Find the smallest disk. 623 */ 624 mediasize = sc->sc_disks[0]->provider->mediasize; 625 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) 626 mediasize -= sc->sc_disks[0]->provider->sectorsize; 627 mediasize -= mediasize % sc->sc_stripesize; 628 sectorsize = sc->sc_disks[0]->provider->sectorsize; 629 for (no = 1; no < sc->sc_ndisks; no++) { 630 ms = sc->sc_disks[no]->provider->mediasize; 631 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) 632 ms -= sc->sc_disks[no]->provider->sectorsize; 633 ms -= ms % sc->sc_stripesize; 634 if (ms < mediasize) 635 mediasize = ms; 636 sectorsize = lcm(sectorsize, 637 sc->sc_disks[no]->provider->sectorsize); 638 } 639 sc->sc_provider->sectorsize = sectorsize; 640 sc->sc_provider->mediasize = mediasize * sc->sc_ndisks; 641 g_error_provider(sc->sc_provider, 0); 642 643 G_STRIPE_DEBUG(0, "Device %s activated.", sc->sc_name); 644 } 645 646 static int 647 g_stripe_read_metadata(struct g_consumer *cp, struct g_stripe_metadata *md) 648 { 649 struct g_provider *pp; 650 u_char *buf; 651 int error; 652 653 g_topology_assert(); 654 655 error = g_access(cp, 1, 0, 0); 656 if (error != 0) 657 return (error); 658 pp = cp->provider; 659 g_topology_unlock(); 660 buf = g_read_data(cp, pp->mediasize - pp->sectorsize, pp->sectorsize, 661 &error); 662 g_topology_lock(); 663 g_access(cp, -1, 0, 0); 664 if (buf == NULL) 665 return (error); 666 667 /* Decode metadata. */ 668 stripe_metadata_decode(buf, md); 669 g_free(buf); 670 671 return (0); 672 } 673 674 /* 675 * Add disk to given device. 676 */ 677 static int 678 g_stripe_add_disk(struct g_stripe_softc *sc, struct g_provider *pp, u_int no) 679 { 680 struct g_consumer *cp, *fcp; 681 struct g_geom *gp; 682 int error; 683 684 /* Metadata corrupted? */ 685 if (no >= sc->sc_ndisks) 686 return (EINVAL); 687 688 /* Check if disk is not already attached. */ 689 if (sc->sc_disks[no] != NULL) 690 return (EEXIST); 691 692 gp = sc->sc_geom; 693 fcp = LIST_FIRST(&gp->consumer); 694 695 cp = g_new_consumer(gp); 696 error = g_attach(cp, pp); 697 if (error != 0) { 698 g_destroy_consumer(cp); 699 return (error); 700 } 701 702 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) { 703 error = g_access(cp, fcp->acr, fcp->acw, fcp->ace); 704 if (error != 0) { 705 g_detach(cp); 706 g_destroy_consumer(cp); 707 return (error); 708 } 709 } 710 if (sc->sc_type == G_STRIPE_TYPE_AUTOMATIC) { 711 struct g_stripe_metadata md; 712 713 /* Reread metadata. */ 714 error = g_stripe_read_metadata(cp, &md); 715 if (error != 0) 716 goto fail; 717 718 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0 || 719 strcmp(md.md_name, sc->sc_name) != 0 || 720 md.md_id != sc->sc_id) { 721 G_STRIPE_DEBUG(0, "Metadata on %s changed.", pp->name); 722 goto fail; 723 } 724 } 725 726 cp->private = sc; 727 cp->index = no; 728 sc->sc_disks[no] = cp; 729 730 G_STRIPE_DEBUG(0, "Disk %s attached to %s.", pp->name, sc->sc_name); 731 732 g_stripe_check_and_run(sc); 733 734 return (0); 735 fail: 736 if (fcp != NULL && (fcp->acr > 0 || fcp->acw > 0 || fcp->ace > 0)) 737 g_access(cp, -fcp->acr, -fcp->acw, -fcp->ace); 738 g_detach(cp); 739 g_destroy_consumer(cp); 740 return (error); 741 } 742 743 static struct g_geom * 744 g_stripe_create(struct g_class *mp, const struct g_stripe_metadata *md, 745 u_int type) 746 { 747 struct g_stripe_softc *sc; 748 struct g_geom *gp; 749 u_int no; 750 751 G_STRIPE_DEBUG(1, "Creating device %s (id=%u).", md->md_name, 752 md->md_id); 753 754 /* Two disks is minimum. */ 755 if (md->md_all < 2) { 756 G_STRIPE_DEBUG(0, "Too few disks defined for %s.", md->md_name); 757 return (NULL); 758 } 759 #if 0 760 /* Stripe size have to be grater than or equal to sector size. */ 761 if (md->md_stripesize < sectorsize) { 762 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name); 763 return (NULL); 764 } 765 #endif 766 /* Stripe size have to be power of 2. */ 767 if (!powerof2(md->md_stripesize)) { 768 G_STRIPE_DEBUG(0, "Invalid stripe size for %s.", md->md_name); 769 return (NULL); 770 } 771 772 /* Check for duplicate unit */ 773 LIST_FOREACH(gp, &mp->geom, geom) { 774 sc = gp->softc; 775 if (sc != NULL && strcmp(sc->sc_name, md->md_name) == 0) { 776 G_STRIPE_DEBUG(0, "Device %s already configured.", 777 sc->sc_name); 778 return (NULL); 779 } 780 } 781 gp = g_new_geomf(mp, "%s", md->md_name); 782 gp->softc = NULL; /* for a moment */ 783 784 sc = malloc(sizeof(*sc), M_STRIPE, M_WAITOK | M_ZERO); 785 gp->start = g_stripe_start; 786 gp->spoiled = g_stripe_orphan; 787 gp->orphan = g_stripe_orphan; 788 gp->access = g_stripe_access; 789 gp->dumpconf = g_stripe_dumpconf; 790 791 sc->sc_id = md->md_id; 792 sc->sc_stripesize = md->md_stripesize; 793 sc->sc_stripebits = BITCOUNT(sc->sc_stripesize - 1); 794 sc->sc_ndisks = md->md_all; 795 sc->sc_disks = malloc(sizeof(struct g_consumer *) * sc->sc_ndisks, 796 M_STRIPE, M_WAITOK | M_ZERO); 797 for (no = 0; no < sc->sc_ndisks; no++) 798 sc->sc_disks[no] = NULL; 799 sc->sc_type = type; 800 801 gp->softc = sc; 802 sc->sc_geom = gp; 803 sc->sc_provider = NULL; 804 805 G_STRIPE_DEBUG(0, "Device %s created (id=%u).", sc->sc_name, sc->sc_id); 806 807 return (gp); 808 } 809 810 static int 811 g_stripe_destroy(struct g_stripe_softc *sc, boolean_t force) 812 { 813 struct g_provider *pp; 814 struct g_geom *gp; 815 u_int no; 816 817 g_topology_assert(); 818 819 if (sc == NULL) 820 return (ENXIO); 821 822 pp = sc->sc_provider; 823 if (pp != NULL && (pp->acr != 0 || pp->acw != 0 || pp->ace != 0)) { 824 if (force) { 825 G_STRIPE_DEBUG(0, "Device %s is still open, so it " 826 "can't be definitely removed.", pp->name); 827 } else { 828 G_STRIPE_DEBUG(1, 829 "Device %s is still open (r%dw%de%d).", pp->name, 830 pp->acr, pp->acw, pp->ace); 831 return (EBUSY); 832 } 833 } 834 835 for (no = 0; no < sc->sc_ndisks; no++) { 836 if (sc->sc_disks[no] != NULL) 837 g_stripe_remove_disk(sc->sc_disks[no]); 838 } 839 840 gp = sc->sc_geom; 841 gp->softc = NULL; 842 KASSERT(sc->sc_provider == NULL, ("Provider still exists? (device=%s)", 843 gp->name)); 844 free(sc->sc_disks, M_STRIPE); 845 free(sc, M_STRIPE); 846 847 pp = LIST_FIRST(&gp->provider); 848 if (pp == NULL || (pp->acr == 0 && pp->acw == 0 && pp->ace == 0)) 849 G_STRIPE_DEBUG(0, "Device %s destroyed.", gp->name); 850 851 g_wither_geom(gp, ENXIO); 852 853 return (0); 854 } 855 856 static int 857 g_stripe_destroy_geom(struct gctl_req *req __unused, 858 struct g_class *mp __unused, struct g_geom *gp) 859 { 860 struct g_stripe_softc *sc; 861 862 sc = gp->softc; 863 return (g_stripe_destroy(sc, 0)); 864 } 865 866 static struct g_geom * 867 g_stripe_taste(struct g_class *mp, struct g_provider *pp, int flags __unused) 868 { 869 struct g_stripe_metadata md; 870 struct g_stripe_softc *sc; 871 struct g_consumer *cp; 872 struct g_geom *gp; 873 int error; 874 875 g_trace(G_T_TOPOLOGY, "%s(%s, %s)", __func__, mp->name, pp->name); 876 g_topology_assert(); 877 878 G_STRIPE_DEBUG(3, "Tasting %s.", pp->name); 879 880 gp = g_new_geomf(mp, "stripe:taste"); 881 gp->start = g_stripe_start; 882 gp->access = g_stripe_access; 883 gp->orphan = g_stripe_orphan; 884 cp = g_new_consumer(gp); 885 g_attach(cp, pp); 886 error = g_stripe_read_metadata(cp, &md); 887 g_detach(cp); 888 g_destroy_consumer(cp); 889 g_destroy_geom(gp); 890 if (error != 0) 891 return (NULL); 892 gp = NULL; 893 894 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0) 895 return (NULL); 896 if (md.md_version > G_STRIPE_VERSION) { 897 printf("geom_stripe.ko module is too old to handle %s.\n", 898 pp->name); 899 return (NULL); 900 } 901 /* 902 * Backward compatibility: 903 * There was no md_provider field in earlier versions of metadata. 904 */ 905 if (md.md_version < 2) 906 bzero(md.md_provider, sizeof(md.md_provider)); 907 908 if (md.md_provider[0] != '\0' && strcmp(md.md_provider, pp->name) != 0) 909 return (NULL); 910 911 /* 912 * Let's check if device already exists. 913 */ 914 sc = NULL; 915 LIST_FOREACH(gp, &mp->geom, geom) { 916 sc = gp->softc; 917 if (sc == NULL) 918 continue; 919 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC) 920 continue; 921 if (strcmp(md.md_name, sc->sc_name) != 0) 922 continue; 923 if (md.md_id != sc->sc_id) 924 continue; 925 break; 926 } 927 if (gp != NULL) { 928 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 929 error = g_stripe_add_disk(sc, pp, md.md_no); 930 if (error != 0) { 931 G_STRIPE_DEBUG(0, 932 "Cannot add disk %s to %s (error=%d).", pp->name, 933 gp->name, error); 934 return (NULL); 935 } 936 } else { 937 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC); 938 if (gp == NULL) { 939 G_STRIPE_DEBUG(0, "Cannot create device %s.", 940 md.md_name); 941 return (NULL); 942 } 943 sc = gp->softc; 944 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 945 error = g_stripe_add_disk(sc, pp, md.md_no); 946 if (error != 0) { 947 G_STRIPE_DEBUG(0, 948 "Cannot add disk %s to %s (error=%d).", pp->name, 949 gp->name, error); 950 g_stripe_destroy(sc, 1); 951 return (NULL); 952 } 953 } 954 955 return (gp); 956 } 957 958 static void 959 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp) 960 { 961 u_int attached, no; 962 struct g_stripe_metadata md; 963 struct g_provider *pp; 964 struct g_stripe_softc *sc; 965 struct g_geom *gp; 966 struct sbuf *sb; 967 intmax_t *stripesize; 968 const char *name; 969 char param[16]; 970 int *nargs; 971 972 g_topology_assert(); 973 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 974 if (nargs == NULL) { 975 gctl_error(req, "No '%s' argument.", "nargs"); 976 return; 977 } 978 if (*nargs <= 2) { 979 gctl_error(req, "Too few arguments."); 980 return; 981 } 982 983 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic)); 984 md.md_version = G_STRIPE_VERSION; 985 name = gctl_get_asciiparam(req, "arg0"); 986 if (name == NULL) { 987 gctl_error(req, "No 'arg%u' argument.", 0); 988 return; 989 } 990 strlcpy(md.md_name, name, sizeof(md.md_name)); 991 md.md_id = arc4random(); 992 md.md_no = 0; 993 md.md_all = *nargs - 1; 994 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize)); 995 if (stripesize == NULL) { 996 gctl_error(req, "No '%s' argument.", "stripesize"); 997 return; 998 } 999 md.md_stripesize = *stripesize; 1000 bzero(md.md_provider, sizeof(md.md_provider)); 1001 1002 /* Check all providers are valid */ 1003 for (no = 1; no < *nargs; no++) { 1004 snprintf(param, sizeof(param), "arg%u", no); 1005 name = gctl_get_asciiparam(req, param); 1006 if (name == NULL) { 1007 gctl_error(req, "No 'arg%u' argument.", no); 1008 return; 1009 } 1010 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 1011 name += strlen("/dev/"); 1012 pp = g_provider_by_name(name); 1013 if (pp == NULL) { 1014 G_STRIPE_DEBUG(1, "Disk %s is invalid.", name); 1015 gctl_error(req, "Disk %s is invalid.", name); 1016 return; 1017 } 1018 } 1019 1020 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL); 1021 if (gp == NULL) { 1022 gctl_error(req, "Can't configure %s.", md.md_name); 1023 return; 1024 } 1025 1026 sc = gp->softc; 1027 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 1028 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name); 1029 for (attached = 0, no = 1; no < *nargs; no++) { 1030 snprintf(param, sizeof(param), "arg%u", no); 1031 name = gctl_get_asciiparam(req, param); 1032 if (strncmp(name, "/dev/", strlen("/dev/")) == 0) 1033 name += strlen("/dev/"); 1034 pp = g_provider_by_name(name); 1035 KASSERT(pp != NULL, ("Provider %s disappear?!", name)); 1036 if (g_stripe_add_disk(sc, pp, no - 1) != 0) { 1037 G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.", 1038 no, pp->name, gp->name); 1039 sbuf_printf(sb, " %s", pp->name); 1040 continue; 1041 } 1042 attached++; 1043 } 1044 sbuf_finish(sb); 1045 if (md.md_all != attached) { 1046 g_stripe_destroy(gp->softc, 1); 1047 gctl_error(req, "%s", sbuf_data(sb)); 1048 } 1049 sbuf_delete(sb); 1050 } 1051 1052 static struct g_stripe_softc * 1053 g_stripe_find_device(struct g_class *mp, const char *name) 1054 { 1055 struct g_stripe_softc *sc; 1056 struct g_geom *gp; 1057 1058 LIST_FOREACH(gp, &mp->geom, geom) { 1059 sc = gp->softc; 1060 if (sc == NULL) 1061 continue; 1062 if (strcmp(sc->sc_name, name) == 0) 1063 return (sc); 1064 } 1065 return (NULL); 1066 } 1067 1068 static void 1069 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp) 1070 { 1071 struct g_stripe_softc *sc; 1072 int *force, *nargs, error; 1073 const char *name; 1074 char param[16]; 1075 u_int i; 1076 1077 g_topology_assert(); 1078 1079 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1080 if (nargs == NULL) { 1081 gctl_error(req, "No '%s' argument.", "nargs"); 1082 return; 1083 } 1084 if (*nargs <= 0) { 1085 gctl_error(req, "Missing device(s)."); 1086 return; 1087 } 1088 force = gctl_get_paraml(req, "force", sizeof(*force)); 1089 if (force == NULL) { 1090 gctl_error(req, "No '%s' argument.", "force"); 1091 return; 1092 } 1093 1094 for (i = 0; i < (u_int)*nargs; i++) { 1095 snprintf(param, sizeof(param), "arg%u", i); 1096 name = gctl_get_asciiparam(req, param); 1097 if (name == NULL) { 1098 gctl_error(req, "No 'arg%u' argument.", i); 1099 return; 1100 } 1101 sc = g_stripe_find_device(mp, name); 1102 if (sc == NULL) { 1103 gctl_error(req, "No such device: %s.", name); 1104 return; 1105 } 1106 error = g_stripe_destroy(sc, *force); 1107 if (error != 0) { 1108 gctl_error(req, "Cannot destroy device %s (error=%d).", 1109 sc->sc_name, error); 1110 return; 1111 } 1112 } 1113 } 1114 1115 static void 1116 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb) 1117 { 1118 uint32_t *version; 1119 1120 g_topology_assert(); 1121 1122 version = gctl_get_paraml(req, "version", sizeof(*version)); 1123 if (version == NULL) { 1124 gctl_error(req, "No '%s' argument.", "version"); 1125 return; 1126 } 1127 if (*version != G_STRIPE_VERSION) { 1128 gctl_error(req, "Userland and kernel parts are out of sync."); 1129 return; 1130 } 1131 1132 if (strcmp(verb, "create") == 0) { 1133 g_stripe_ctl_create(req, mp); 1134 return; 1135 } else if (strcmp(verb, "destroy") == 0 || 1136 strcmp(verb, "stop") == 0) { 1137 g_stripe_ctl_destroy(req, mp); 1138 return; 1139 } 1140 1141 gctl_error(req, "Unknown verb."); 1142 } 1143 1144 static void 1145 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1146 struct g_consumer *cp, struct g_provider *pp) 1147 { 1148 struct g_stripe_softc *sc; 1149 1150 sc = gp->softc; 1151 if (sc == NULL) 1152 return; 1153 if (pp != NULL) { 1154 /* Nothing here. */ 1155 } else if (cp != NULL) { 1156 /* Nothing here. */ 1157 } else { 1158 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id); 1159 sbuf_printf(sb, "%s<Stripesize>%u</Stripesize>\n", indent, 1160 (u_int)sc->sc_stripesize); 1161 sbuf_printf(sb, "%s<Type>", indent); 1162 switch (sc->sc_type) { 1163 case G_STRIPE_TYPE_AUTOMATIC: 1164 sbuf_printf(sb, "AUTOMATIC"); 1165 break; 1166 case G_STRIPE_TYPE_MANUAL: 1167 sbuf_printf(sb, "MANUAL"); 1168 break; 1169 default: 1170 sbuf_printf(sb, "UNKNOWN"); 1171 break; 1172 } 1173 sbuf_printf(sb, "</Type>\n"); 1174 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n", 1175 indent, sc->sc_ndisks, g_stripe_nvalid(sc)); 1176 sbuf_printf(sb, "%s<State>", indent); 1177 if (sc->sc_provider != NULL && sc->sc_provider->error == 0) 1178 sbuf_printf(sb, "UP"); 1179 else 1180 sbuf_printf(sb, "DOWN"); 1181 sbuf_printf(sb, "</State>\n"); 1182 } 1183 } 1184 1185 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe); 1186