1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2004-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/kernel.h> 32 #include <sys/module.h> 33 #include <sys/lock.h> 34 #include <sys/mutex.h> 35 #include <sys/bio.h> 36 #include <sys/sbuf.h> 37 #include <sys/sysctl.h> 38 #include <sys/malloc.h> 39 #include <vm/uma.h> 40 #include <geom/geom.h> 41 #include <geom/geom_dbg.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 | CTLFLAG_MPSAFE, 0, 72 "GEOM_STRIPE stuff"); 73 static u_int g_stripe_debug = 0; 74 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, debug, CTLFLAG_RWTUN, &g_stripe_debug, 0, 75 "Debug level"); 76 static int g_stripe_fast = 0; 77 SYSCTL_INT(_kern_geom_stripe, OID_AUTO, fast, 78 CTLFLAG_RWTUN, &g_stripe_fast, 0, 79 "Fast, but memory-consuming, mode"); 80 static u_long g_stripe_maxmem; 81 SYSCTL_ULONG(_kern_geom_stripe, OID_AUTO, maxmem, 82 CTLFLAG_RDTUN | CTLFLAG_NOFETCH, &g_stripe_maxmem, 0, 83 "Maximum memory that can be allocated in \"fast\" mode (in bytes)"); 84 static u_int g_stripe_fast_failed = 0; 85 SYSCTL_UINT(_kern_geom_stripe, OID_AUTO, fast_failed, CTLFLAG_RD, 86 &g_stripe_fast_failed, 0, "How many times \"fast\" mode failed"); 87 88 /* 89 * Greatest Common Divisor. 90 */ 91 static u_int 92 gcd(u_int a, u_int b) 93 { 94 u_int c; 95 96 while (b != 0) { 97 c = a; 98 a = b; 99 b = (c % b); 100 } 101 return (a); 102 } 103 104 /* 105 * Least Common Multiple. 106 */ 107 static u_int 108 lcm(u_int a, u_int b) 109 { 110 111 return ((a * b) / gcd(a, b)); 112 } 113 114 static void 115 g_stripe_init(struct g_class *mp __unused) 116 { 117 118 g_stripe_maxmem = maxphys * 100; 119 TUNABLE_ULONG_FETCH("kern.geom.stripe.maxmem,", &g_stripe_maxmem); 120 g_stripe_zone = uma_zcreate("g_stripe_zone", maxphys, NULL, NULL, 121 NULL, NULL, 0, 0); 122 g_stripe_maxmem -= g_stripe_maxmem % maxphys; 123 uma_zone_set_max(g_stripe_zone, g_stripe_maxmem / maxphys); 124 } 125 126 static void 127 g_stripe_fini(struct g_class *mp __unused) 128 { 129 130 uma_zdestroy(g_stripe_zone); 131 } 132 133 /* 134 * Return the number of valid disks. 135 */ 136 static u_int 137 g_stripe_nvalid(struct g_stripe_softc *sc) 138 { 139 u_int i, no; 140 141 no = 0; 142 for (i = 0; i < sc->sc_ndisks; i++) { 143 if (sc->sc_disks[i] != NULL) 144 no++; 145 } 146 147 return (no); 148 } 149 150 static void 151 g_stripe_remove_disk(struct g_consumer *cp) 152 { 153 struct g_stripe_softc *sc; 154 155 g_topology_assert(); 156 KASSERT(cp != NULL, ("Non-valid disk in %s.", __func__)); 157 sc = (struct g_stripe_softc *)cp->geom->softc; 158 KASSERT(sc != NULL, ("NULL sc in %s.", __func__)); 159 160 if (cp->private == NULL) { 161 G_STRIPE_DEBUG(0, "Disk %s removed from %s.", 162 cp->provider->name, sc->sc_name); 163 cp->private = (void *)(uintptr_t)-1; 164 } 165 166 if (sc->sc_provider != NULL) { 167 G_STRIPE_DEBUG(0, "Device %s deactivated.", 168 sc->sc_provider->name); 169 g_wither_provider(sc->sc_provider, ENXIO); 170 sc->sc_provider = NULL; 171 } 172 173 if (cp->acr > 0 || cp->acw > 0 || cp->ace > 0) 174 return; 175 sc->sc_disks[cp->index] = NULL; 176 cp->index = 0; 177 g_detach(cp); 178 g_destroy_consumer(cp); 179 /* If there are no valid disks anymore, remove device. */ 180 if (LIST_EMPTY(&sc->sc_geom->consumer)) 181 g_stripe_destroy(sc, 1); 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 } 198 199 static int 200 g_stripe_access(struct g_provider *pp, int dr, int dw, int de) 201 { 202 struct g_consumer *cp1, *cp2, *tmp; 203 struct g_stripe_softc *sc __diagused; 204 struct g_geom *gp; 205 int error; 206 207 g_topology_assert(); 208 gp = pp->geom; 209 sc = gp->softc; 210 KASSERT(sc != NULL, ("NULL sc in %s.", __func__)); 211 212 /* On first open, grab an extra "exclusive" bit */ 213 if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) 214 de++; 215 /* ... and let go of it on last close */ 216 if ((pp->acr + dr) == 0 && (pp->acw + dw) == 0 && (pp->ace + de) == 0) 217 de--; 218 219 LIST_FOREACH_SAFE(cp1, &gp->consumer, consumer, tmp) { 220 error = g_access(cp1, dr, dw, de); 221 if (error != 0) 222 goto fail; 223 if (cp1->acr == 0 && cp1->acw == 0 && cp1->ace == 0 && 224 cp1->private != NULL) { 225 g_stripe_remove_disk(cp1); /* May destroy geom. */ 226 } 227 } 228 return (0); 229 230 fail: 231 LIST_FOREACH(cp2, &gp->consumer, consumer) { 232 if (cp1 == cp2) 233 break; 234 g_access(cp2, -dr, -dw, -de); 235 } 236 return (error); 237 } 238 239 static void 240 g_stripe_copy(struct g_stripe_softc *sc, char *src, char *dst, off_t offset, 241 off_t length, int mode) 242 { 243 off_t stripesize; 244 size_t len; 245 246 stripesize = sc->sc_stripesize; 247 len = (size_t)(stripesize - (offset & (stripesize - 1))); 248 do { 249 bcopy(src, dst, len); 250 if (mode) { 251 dst += len + stripesize * (sc->sc_ndisks - 1); 252 src += len; 253 } else { 254 dst += len; 255 src += len + stripesize * (sc->sc_ndisks - 1); 256 } 257 length -= len; 258 KASSERT(length >= 0, 259 ("Length < 0 (stripesize=%ju, offset=%ju, length=%jd).", 260 (uintmax_t)stripesize, (uintmax_t)offset, (intmax_t)length)); 261 if (length > stripesize) 262 len = stripesize; 263 else 264 len = length; 265 } while (length > 0); 266 } 267 268 static void 269 g_stripe_done(struct bio *bp) 270 { 271 struct g_stripe_softc *sc; 272 struct bio *pbp; 273 274 pbp = bp->bio_parent; 275 sc = pbp->bio_to->geom->softc; 276 if (bp->bio_cmd == BIO_READ && bp->bio_caller1 != NULL) { 277 g_stripe_copy(sc, bp->bio_data, bp->bio_caller1, bp->bio_offset, 278 bp->bio_length, 1); 279 bp->bio_data = bp->bio_caller1; 280 bp->bio_caller1 = NULL; 281 } 282 mtx_lock(&sc->sc_lock); 283 if (pbp->bio_error == 0) 284 pbp->bio_error = bp->bio_error; 285 pbp->bio_completed += bp->bio_completed; 286 pbp->bio_inbed++; 287 if (pbp->bio_children == pbp->bio_inbed) { 288 mtx_unlock(&sc->sc_lock); 289 if (pbp->bio_driver1 != NULL) 290 uma_zfree(g_stripe_zone, pbp->bio_driver1); 291 if (bp->bio_cmd == BIO_SPEEDUP) 292 pbp->bio_completed = pbp->bio_length; 293 g_io_deliver(pbp, pbp->bio_error); 294 } else 295 mtx_unlock(&sc->sc_lock); 296 g_destroy_bio(bp); 297 } 298 299 static int 300 g_stripe_start_fast(struct bio *bp, u_int no, off_t offset, off_t length) 301 { 302 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 303 struct g_stripe_softc *sc; 304 char *addr, *data = NULL; 305 struct bio *cbp; 306 off_t stripesize; 307 u_int nparts = 0; 308 int error; 309 310 sc = bp->bio_to->geom->softc; 311 312 addr = bp->bio_data; 313 stripesize = sc->sc_stripesize; 314 315 cbp = g_clone_bio(bp); 316 if (cbp == NULL) { 317 error = ENOMEM; 318 goto failure; 319 } 320 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 321 nparts++; 322 /* 323 * Fill in the component buf structure. 324 */ 325 cbp->bio_done = g_stripe_done; 326 cbp->bio_offset = offset; 327 cbp->bio_data = addr; 328 cbp->bio_caller1 = NULL; 329 cbp->bio_length = length; 330 cbp->bio_caller2 = sc->sc_disks[no]; 331 332 /* offset -= offset % stripesize; */ 333 offset -= offset & (stripesize - 1); 334 addr += length; 335 length = bp->bio_length - length; 336 for (no++; length > 0; no++, length -= stripesize, addr += stripesize) { 337 if (no > sc->sc_ndisks - 1) { 338 no = 0; 339 offset += stripesize; 340 } 341 if (nparts >= sc->sc_ndisks) { 342 cbp = TAILQ_NEXT(cbp, bio_queue); 343 if (cbp == NULL) 344 cbp = TAILQ_FIRST(&queue); 345 nparts++; 346 /* 347 * Update bio structure. 348 */ 349 /* 350 * MIN() is in case when 351 * (bp->bio_length % sc->sc_stripesize) != 0. 352 */ 353 cbp->bio_length += MIN(stripesize, length); 354 if (cbp->bio_caller1 == NULL) { 355 cbp->bio_caller1 = cbp->bio_data; 356 cbp->bio_data = NULL; 357 if (data == NULL) { 358 data = uma_zalloc(g_stripe_zone, 359 M_NOWAIT); 360 if (data == NULL) { 361 error = ENOMEM; 362 goto failure; 363 } 364 } 365 } 366 } else { 367 cbp = g_clone_bio(bp); 368 if (cbp == NULL) { 369 error = ENOMEM; 370 goto failure; 371 } 372 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 373 nparts++; 374 /* 375 * Fill in the component buf structure. 376 */ 377 cbp->bio_done = g_stripe_done; 378 cbp->bio_offset = offset; 379 cbp->bio_data = addr; 380 cbp->bio_caller1 = NULL; 381 /* 382 * MIN() is in case when 383 * (bp->bio_length % sc->sc_stripesize) != 0. 384 */ 385 cbp->bio_length = MIN(stripesize, length); 386 cbp->bio_caller2 = sc->sc_disks[no]; 387 } 388 } 389 if (data != NULL) 390 bp->bio_driver1 = data; 391 /* 392 * Fire off all allocated requests! 393 */ 394 while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 395 struct g_consumer *cp; 396 397 TAILQ_REMOVE(&queue, cbp, bio_queue); 398 cp = cbp->bio_caller2; 399 cbp->bio_caller2 = NULL; 400 cbp->bio_to = cp->provider; 401 if (cbp->bio_caller1 != NULL) { 402 cbp->bio_data = data; 403 if (bp->bio_cmd == BIO_WRITE) { 404 g_stripe_copy(sc, cbp->bio_caller1, data, 405 cbp->bio_offset, cbp->bio_length, 0); 406 } 407 data += cbp->bio_length; 408 } 409 G_STRIPE_LOGREQ(cbp, "Sending request."); 410 g_io_request(cbp, cp); 411 } 412 return (0); 413 failure: 414 if (data != NULL) 415 uma_zfree(g_stripe_zone, data); 416 while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 417 TAILQ_REMOVE(&queue, cbp, bio_queue); 418 if (cbp->bio_caller1 != NULL) { 419 cbp->bio_data = cbp->bio_caller1; 420 cbp->bio_caller1 = NULL; 421 } 422 bp->bio_children--; 423 g_destroy_bio(cbp); 424 } 425 return (error); 426 } 427 428 static int 429 g_stripe_start_economic(struct bio *bp, u_int no, off_t offset, off_t length) 430 { 431 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 432 struct g_stripe_softc *sc; 433 off_t stripesize; 434 struct bio *cbp; 435 char *addr; 436 int error; 437 438 sc = bp->bio_to->geom->softc; 439 440 stripesize = sc->sc_stripesize; 441 442 cbp = g_clone_bio(bp); 443 if (cbp == NULL) { 444 error = ENOMEM; 445 goto failure; 446 } 447 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 448 /* 449 * Fill in the component buf structure. 450 */ 451 if (bp->bio_length == length) 452 cbp->bio_done = g_std_done; /* Optimized lockless case. */ 453 else 454 cbp->bio_done = g_stripe_done; 455 cbp->bio_offset = offset; 456 cbp->bio_length = length; 457 if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 458 bp->bio_ma_n = round_page(bp->bio_ma_offset + 459 bp->bio_length) / PAGE_SIZE; 460 addr = NULL; 461 } else 462 addr = bp->bio_data; 463 cbp->bio_caller2 = sc->sc_disks[no]; 464 465 /* offset -= offset % stripesize; */ 466 offset -= offset & (stripesize - 1); 467 if (bp->bio_cmd != BIO_DELETE) 468 addr += length; 469 length = bp->bio_length - length; 470 for (no++; length > 0; no++, length -= stripesize) { 471 if (no > sc->sc_ndisks - 1) { 472 no = 0; 473 offset += stripesize; 474 } 475 cbp = g_clone_bio(bp); 476 if (cbp == NULL) { 477 error = ENOMEM; 478 goto failure; 479 } 480 TAILQ_INSERT_TAIL(&queue, cbp, bio_queue); 481 482 /* 483 * Fill in the component buf structure. 484 */ 485 cbp->bio_done = g_stripe_done; 486 cbp->bio_offset = offset; 487 /* 488 * MIN() is in case when 489 * (bp->bio_length % sc->sc_stripesize) != 0. 490 */ 491 cbp->bio_length = MIN(stripesize, length); 492 if ((bp->bio_flags & BIO_UNMAPPED) != 0) { 493 cbp->bio_ma_offset += (uintptr_t)addr; 494 cbp->bio_ma += cbp->bio_ma_offset / PAGE_SIZE; 495 cbp->bio_ma_offset %= PAGE_SIZE; 496 cbp->bio_ma_n = round_page(cbp->bio_ma_offset + 497 cbp->bio_length) / PAGE_SIZE; 498 } else 499 cbp->bio_data = addr; 500 501 cbp->bio_caller2 = sc->sc_disks[no]; 502 503 if (bp->bio_cmd != BIO_DELETE) 504 addr += stripesize; 505 } 506 /* 507 * Fire off all allocated requests! 508 */ 509 while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 510 struct g_consumer *cp; 511 512 TAILQ_REMOVE(&queue, cbp, bio_queue); 513 cp = cbp->bio_caller2; 514 cbp->bio_caller2 = NULL; 515 cbp->bio_to = cp->provider; 516 G_STRIPE_LOGREQ(cbp, "Sending request."); 517 g_io_request(cbp, cp); 518 } 519 return (0); 520 failure: 521 while ((cbp = TAILQ_FIRST(&queue)) != NULL) { 522 TAILQ_REMOVE(&queue, cbp, bio_queue); 523 bp->bio_children--; 524 g_destroy_bio(cbp); 525 } 526 return (error); 527 } 528 529 static void 530 g_stripe_pushdown(struct g_stripe_softc *sc, struct bio *bp) 531 { 532 struct bio_queue_head queue; 533 struct g_consumer *cp; 534 struct bio *cbp; 535 u_int no; 536 537 bioq_init(&queue); 538 for (no = 0; no < sc->sc_ndisks; no++) { 539 cbp = g_clone_bio(bp); 540 if (cbp == NULL) { 541 for (cbp = bioq_first(&queue); cbp != NULL; 542 cbp = bioq_first(&queue)) { 543 bioq_remove(&queue, cbp); 544 g_destroy_bio(cbp); 545 } 546 if (bp->bio_error == 0) 547 bp->bio_error = ENOMEM; 548 g_io_deliver(bp, bp->bio_error); 549 return; 550 } 551 bioq_insert_tail(&queue, cbp); 552 cbp->bio_done = g_stripe_done; 553 cbp->bio_caller2 = sc->sc_disks[no]; 554 cbp->bio_to = sc->sc_disks[no]->provider; 555 } 556 for (cbp = bioq_first(&queue); cbp != NULL; cbp = bioq_first(&queue)) { 557 bioq_remove(&queue, cbp); 558 G_STRIPE_LOGREQ(cbp, "Sending request."); 559 cp = cbp->bio_caller2; 560 cbp->bio_caller2 = NULL; 561 g_io_request(cbp, cp); 562 } 563 } 564 565 static void 566 g_stripe_start(struct bio *bp) 567 { 568 off_t offset, start, length, nstripe, stripesize; 569 struct g_stripe_softc *sc; 570 u_int no; 571 int error, fast = 0; 572 573 sc = bp->bio_to->geom->softc; 574 /* 575 * If sc == NULL, provider's error should be set and g_stripe_start() 576 * should not be called at all. 577 */ 578 KASSERT(sc != NULL, 579 ("Provider's error should be set (error=%d)(device=%s).", 580 bp->bio_to->error, bp->bio_to->name)); 581 582 G_STRIPE_LOGREQ(bp, "Request received."); 583 584 switch (bp->bio_cmd) { 585 case BIO_READ: 586 case BIO_WRITE: 587 case BIO_DELETE: 588 break; 589 case BIO_SPEEDUP: 590 case BIO_FLUSH: 591 g_stripe_pushdown(sc, bp); 592 return; 593 case BIO_GETATTR: 594 /* To which provider it should be delivered? */ 595 default: 596 g_io_deliver(bp, EOPNOTSUPP); 597 return; 598 } 599 600 stripesize = sc->sc_stripesize; 601 602 /* 603 * Calculations are quite messy, but fast I hope. 604 */ 605 606 /* Stripe number. */ 607 /* nstripe = bp->bio_offset / stripesize; */ 608 nstripe = bp->bio_offset >> (off_t)sc->sc_stripebits; 609 /* Disk number. */ 610 no = nstripe % sc->sc_ndisks; 611 /* Start position in stripe. */ 612 /* start = bp->bio_offset % stripesize; */ 613 start = bp->bio_offset & (stripesize - 1); 614 /* Start position in disk. */ 615 /* offset = (nstripe / sc->sc_ndisks) * stripesize + start; */ 616 offset = ((nstripe / sc->sc_ndisks) << sc->sc_stripebits) + start; 617 /* Length of data to operate. */ 618 length = MIN(bp->bio_length, stripesize - start); 619 620 /* 621 * Do use "fast" mode when: 622 * 1. "Fast" mode is ON. 623 * and 624 * 2. Request size is less than or equal to maxphys, 625 * which should always be true. 626 * and 627 * 3. Request size is bigger than stripesize * ndisks. If it isn't, 628 * there will be no need to send more than one I/O request to 629 * a provider, so there is nothing to optmize. 630 * and 631 * 4. Request is not unmapped. 632 * and 633 * 5. It is not a BIO_DELETE. 634 */ 635 if (g_stripe_fast && bp->bio_length <= maxphys && 636 bp->bio_length >= stripesize * sc->sc_ndisks && 637 (bp->bio_flags & BIO_UNMAPPED) == 0 && 638 bp->bio_cmd != BIO_DELETE) { 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 cp->flags |= G_CF_DIRECT_SEND | G_CF_DIRECT_RECEIVE; 955 error = g_attach(cp, pp); 956 if (error == 0) { 957 error = g_stripe_read_metadata(cp, &md); 958 g_detach(cp); 959 } 960 g_destroy_consumer(cp); 961 g_destroy_geom(gp); 962 if (error != 0) 963 return (NULL); 964 gp = NULL; 965 966 if (strcmp(md.md_magic, G_STRIPE_MAGIC) != 0) 967 return (NULL); 968 if (md.md_version > G_STRIPE_VERSION) { 969 printf("geom_stripe.ko module is too old to handle %s.\n", 970 pp->name); 971 return (NULL); 972 } 973 /* 974 * Backward compatibility: 975 */ 976 /* There was no md_provider field in earlier versions of metadata. */ 977 if (md.md_version < 2) 978 bzero(md.md_provider, sizeof(md.md_provider)); 979 /* There was no md_provsize field in earlier versions of metadata. */ 980 if (md.md_version < 3) 981 md.md_provsize = pp->mediasize; 982 983 if (md.md_provider[0] != '\0' && 984 !g_compare_names(md.md_provider, pp->name)) 985 return (NULL); 986 if (md.md_provsize != pp->mediasize) 987 return (NULL); 988 989 /* 990 * Let's check if device already exists. 991 */ 992 sc = NULL; 993 LIST_FOREACH(gp, &mp->geom, geom) { 994 sc = gp->softc; 995 if (sc == NULL) 996 continue; 997 if (sc->sc_type != G_STRIPE_TYPE_AUTOMATIC) 998 continue; 999 if (strcmp(md.md_name, sc->sc_name) != 0) 1000 continue; 1001 if (md.md_id != sc->sc_id) 1002 continue; 1003 break; 1004 } 1005 if (gp != NULL) { 1006 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 1007 error = g_stripe_add_disk(sc, pp, md.md_no); 1008 if (error != 0) { 1009 G_STRIPE_DEBUG(0, 1010 "Cannot add disk %s to %s (error=%d).", pp->name, 1011 gp->name, error); 1012 return (NULL); 1013 } 1014 } else { 1015 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_AUTOMATIC); 1016 if (gp == NULL) { 1017 G_STRIPE_DEBUG(0, "Cannot create device %s.", 1018 md.md_name); 1019 return (NULL); 1020 } 1021 sc = gp->softc; 1022 G_STRIPE_DEBUG(1, "Adding disk %s to %s.", pp->name, gp->name); 1023 error = g_stripe_add_disk(sc, pp, md.md_no); 1024 if (error != 0) { 1025 G_STRIPE_DEBUG(0, 1026 "Cannot add disk %s to %s (error=%d).", pp->name, 1027 gp->name, error); 1028 g_stripe_destroy(sc, 1); 1029 return (NULL); 1030 } 1031 } 1032 1033 return (gp); 1034 } 1035 1036 static void 1037 g_stripe_ctl_create(struct gctl_req *req, struct g_class *mp) 1038 { 1039 u_int attached, no; 1040 struct g_stripe_metadata md; 1041 struct g_provider *pp; 1042 struct g_stripe_softc *sc; 1043 struct g_geom *gp; 1044 struct sbuf *sb; 1045 off_t *stripesize; 1046 const char *name; 1047 char param[16]; 1048 int *nargs; 1049 1050 g_topology_assert(); 1051 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1052 if (nargs == NULL) { 1053 gctl_error(req, "No '%s' argument.", "nargs"); 1054 return; 1055 } 1056 if (*nargs <= 2) { 1057 gctl_error(req, "Too few arguments."); 1058 return; 1059 } 1060 1061 strlcpy(md.md_magic, G_STRIPE_MAGIC, sizeof(md.md_magic)); 1062 md.md_version = G_STRIPE_VERSION; 1063 name = gctl_get_asciiparam(req, "arg0"); 1064 if (name == NULL) { 1065 gctl_error(req, "No 'arg%u' argument.", 0); 1066 return; 1067 } 1068 strlcpy(md.md_name, name, sizeof(md.md_name)); 1069 md.md_id = arc4random(); 1070 md.md_no = 0; 1071 md.md_all = *nargs - 1; 1072 stripesize = gctl_get_paraml(req, "stripesize", sizeof(*stripesize)); 1073 if (stripesize == NULL) { 1074 gctl_error(req, "No '%s' argument.", "stripesize"); 1075 return; 1076 } 1077 md.md_stripesize = (uint32_t)*stripesize; 1078 bzero(md.md_provider, sizeof(md.md_provider)); 1079 /* This field is not important here. */ 1080 md.md_provsize = 0; 1081 1082 /* Check all providers are valid */ 1083 for (no = 1; no < *nargs; no++) { 1084 snprintf(param, sizeof(param), "arg%u", no); 1085 pp = gctl_get_provider(req, param); 1086 if (pp == NULL) 1087 return; 1088 } 1089 1090 gp = g_stripe_create(mp, &md, G_STRIPE_TYPE_MANUAL); 1091 if (gp == NULL) { 1092 gctl_error(req, "Can't configure %s.", md.md_name); 1093 return; 1094 } 1095 1096 sc = gp->softc; 1097 sb = sbuf_new_auto(); 1098 sbuf_printf(sb, "Can't attach disk(s) to %s:", gp->name); 1099 for (attached = 0, no = 1; no < *nargs; no++) { 1100 snprintf(param, sizeof(param), "arg%u", no); 1101 pp = gctl_get_provider(req, param); 1102 if (pp == NULL) { 1103 name = gctl_get_asciiparam(req, param); 1104 MPASS(name != NULL); 1105 sbuf_printf(sb, " %s", name); 1106 continue; 1107 } 1108 if (g_stripe_add_disk(sc, pp, no - 1) != 0) { 1109 G_STRIPE_DEBUG(1, "Disk %u (%s) not attached to %s.", 1110 no, pp->name, gp->name); 1111 sbuf_printf(sb, " %s", pp->name); 1112 continue; 1113 } 1114 attached++; 1115 } 1116 sbuf_finish(sb); 1117 if (md.md_all != attached) { 1118 g_stripe_destroy(gp->softc, 1); 1119 gctl_error(req, "%s", sbuf_data(sb)); 1120 } 1121 sbuf_delete(sb); 1122 } 1123 1124 static struct g_stripe_softc * 1125 g_stripe_find_device(struct g_class *mp, const char *name) 1126 { 1127 struct g_stripe_softc *sc; 1128 struct g_geom *gp; 1129 1130 LIST_FOREACH(gp, &mp->geom, geom) { 1131 sc = gp->softc; 1132 if (sc == NULL) 1133 continue; 1134 if (strcmp(sc->sc_name, name) == 0) 1135 return (sc); 1136 } 1137 return (NULL); 1138 } 1139 1140 static void 1141 g_stripe_ctl_destroy(struct gctl_req *req, struct g_class *mp) 1142 { 1143 struct g_stripe_softc *sc; 1144 int *force, *nargs, error; 1145 const char *name; 1146 char param[16]; 1147 u_int i; 1148 1149 g_topology_assert(); 1150 1151 nargs = gctl_get_paraml(req, "nargs", sizeof(*nargs)); 1152 if (nargs == NULL) { 1153 gctl_error(req, "No '%s' argument.", "nargs"); 1154 return; 1155 } 1156 if (*nargs <= 0) { 1157 gctl_error(req, "Missing device(s)."); 1158 return; 1159 } 1160 force = gctl_get_paraml(req, "force", sizeof(*force)); 1161 if (force == NULL) { 1162 gctl_error(req, "No '%s' argument.", "force"); 1163 return; 1164 } 1165 1166 for (i = 0; i < (u_int)*nargs; i++) { 1167 snprintf(param, sizeof(param), "arg%u", i); 1168 name = gctl_get_asciiparam(req, param); 1169 if (name == NULL) { 1170 gctl_error(req, "No 'arg%u' argument.", i); 1171 return; 1172 } 1173 sc = g_stripe_find_device(mp, name); 1174 if (sc == NULL) { 1175 gctl_error(req, "No such device: %s.", name); 1176 return; 1177 } 1178 error = g_stripe_destroy(sc, *force); 1179 if (error != 0) { 1180 gctl_error(req, "Cannot destroy device %s (error=%d).", 1181 sc->sc_name, error); 1182 return; 1183 } 1184 } 1185 } 1186 1187 static void 1188 g_stripe_config(struct gctl_req *req, struct g_class *mp, const char *verb) 1189 { 1190 uint32_t *version; 1191 1192 g_topology_assert(); 1193 1194 version = gctl_get_paraml(req, "version", sizeof(*version)); 1195 if (version == NULL) { 1196 gctl_error(req, "No '%s' argument.", "version"); 1197 return; 1198 } 1199 if (*version != G_STRIPE_VERSION) { 1200 gctl_error(req, "Userland and kernel parts are out of sync."); 1201 return; 1202 } 1203 1204 if (strcmp(verb, "create") == 0) { 1205 g_stripe_ctl_create(req, mp); 1206 return; 1207 } else if (strcmp(verb, "destroy") == 0 || 1208 strcmp(verb, "stop") == 0) { 1209 g_stripe_ctl_destroy(req, mp); 1210 return; 1211 } 1212 1213 gctl_error(req, "Unknown verb."); 1214 } 1215 1216 static void 1217 g_stripe_dumpconf(struct sbuf *sb, const char *indent, struct g_geom *gp, 1218 struct g_consumer *cp, struct g_provider *pp) 1219 { 1220 struct g_stripe_softc *sc; 1221 1222 sc = gp->softc; 1223 if (sc == NULL) 1224 return; 1225 if (pp != NULL) { 1226 /* Nothing here. */ 1227 } else if (cp != NULL) { 1228 sbuf_printf(sb, "%s<Number>%u</Number>\n", indent, 1229 (u_int)cp->index); 1230 } else { 1231 sbuf_printf(sb, "%s<ID>%u</ID>\n", indent, (u_int)sc->sc_id); 1232 sbuf_printf(sb, "%s<Stripesize>%ju</Stripesize>\n", indent, 1233 (uintmax_t)sc->sc_stripesize); 1234 sbuf_printf(sb, "%s<Type>", indent); 1235 switch (sc->sc_type) { 1236 case G_STRIPE_TYPE_AUTOMATIC: 1237 sbuf_cat(sb, "AUTOMATIC"); 1238 break; 1239 case G_STRIPE_TYPE_MANUAL: 1240 sbuf_cat(sb, "MANUAL"); 1241 break; 1242 default: 1243 sbuf_cat(sb, "UNKNOWN"); 1244 break; 1245 } 1246 sbuf_cat(sb, "</Type>\n"); 1247 sbuf_printf(sb, "%s<Status>Total=%u, Online=%u</Status>\n", 1248 indent, sc->sc_ndisks, g_stripe_nvalid(sc)); 1249 sbuf_printf(sb, "%s<State>", indent); 1250 if (sc->sc_provider != NULL && sc->sc_provider->error == 0) 1251 sbuf_cat(sb, "UP"); 1252 else 1253 sbuf_cat(sb, "DOWN"); 1254 sbuf_cat(sb, "</State>\n"); 1255 } 1256 } 1257 1258 DECLARE_GEOM_CLASS(g_stripe_class, g_stripe); 1259 MODULE_VERSION(geom_stripe, 0); 1260