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