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