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