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