1 /* 2 * Copyright (c) 2003 Poul-Henning Kamp. 3 * Copyright (c) 1995 Jason R. Thorpe. 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * All rights reserved. 7 * Copyright (c) 1988 University of Utah. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * the Systems Programming Group of the University of Utah Computer 11 * Science Department. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed for the NetBSD Project 24 * by Jason R. Thorpe. 25 * 4. The names of the authors may not be used to endorse or promote products 26 * derived from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 30 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 31 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 35 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * Dynamic configuration and disklabel support by: 41 * Jason R. Thorpe <thorpej@nas.nasa.gov> 42 * Numerical Aerodynamic Simulation Facility 43 * Mail Stop 258-6 44 * NASA Ames Research Center 45 * Moffett Field, CA 94035 46 * 47 * from: Utah $Hdr: cd.c 1.6 90/11/28$ 48 * @(#)cd.c 8.2 (Berkeley) 11/16/93 49 * $NetBSD: ccd.c,v 1.22 1995/12/08 19:13:26 thorpej Exp $ 50 */ 51 52 #include <sys/cdefs.h> 53 __FBSDID("$FreeBSD$"); 54 55 #include <sys/param.h> 56 #include <sys/systm.h> 57 #include <sys/kernel.h> 58 #include <sys/module.h> 59 #include <sys/bio.h> 60 #include <sys/malloc.h> 61 #include <geom/geom.h> 62 63 /* 64 * Number of blocks to untouched in front of a component partition. 65 * This is to avoid violating its disklabel area when it starts at the 66 * beginning of the slice. 67 */ 68 #if !defined(CCD_OFFSET) 69 #define CCD_OFFSET 16 70 #endif 71 72 /* sc_flags */ 73 #define CCDF_UNIFORM 0x02 /* use LCCD of sizes for uniform interleave */ 74 #define CCDF_MIRROR 0x04 /* use mirroring */ 75 76 /* Mask of user-settable ccd flags. */ 77 #define CCDF_USERMASK (CCDF_UNIFORM|CCDF_MIRROR) 78 79 /* 80 * Interleave description table. 81 * Computed at boot time to speed irregular-interleave lookups. 82 * The idea is that we interleave in "groups". First we interleave 83 * evenly over all component disks up to the size of the smallest 84 * component (the first group), then we interleave evenly over all 85 * remaining disks up to the size of the next-smallest (second group), 86 * and so on. 87 * 88 * Each table entry describes the interleave characteristics of one 89 * of these groups. For example if a concatenated disk consisted of 90 * three components of 5, 3, and 7 DEV_BSIZE blocks interleaved at 91 * DEV_BSIZE (1), the table would have three entries: 92 * 93 * ndisk startblk startoff dev 94 * 3 0 0 0, 1, 2 95 * 2 9 3 0, 2 96 * 1 13 5 2 97 * 0 - - - 98 * 99 * which says that the first nine blocks (0-8) are interleaved over 100 * 3 disks (0, 1, 2) starting at block offset 0 on any component disk, 101 * the next 4 blocks (9-12) are interleaved over 2 disks (0, 2) starting 102 * at component block 3, and the remaining blocks (13-14) are on disk 103 * 2 starting at offset 5. 104 */ 105 struct ccdiinfo { 106 int ii_ndisk; /* # of disks range is interleaved over */ 107 daddr_t ii_startblk; /* starting scaled block # for range */ 108 daddr_t ii_startoff; /* starting component offset (block #) */ 109 int *ii_index; /* ordered list of components in range */ 110 }; 111 112 /* 113 * Component info table. 114 * Describes a single component of a concatenated disk. 115 */ 116 struct ccdcinfo { 117 size_t ci_size; /* size */ 118 struct g_provider *ci_provider; /* provider */ 119 struct g_consumer *ci_consumer; /* consumer */ 120 }; 121 122 /* 123 * A concatenated disk is described by this structure. 124 */ 125 126 struct ccd_s { 127 LIST_ENTRY(ccd_s) list; 128 129 int sc_unit; /* logical unit number */ 130 int sc_flags; /* flags */ 131 size_t sc_size; /* size of ccd */ 132 int sc_ileave; /* interleave */ 133 u_int sc_ndisks; /* number of components */ 134 struct ccdcinfo *sc_cinfo; /* component info */ 135 struct ccdiinfo *sc_itable; /* interleave table */ 136 u_int32_t sc_secsize; /* # bytes per sector */ 137 int sc_pick; /* side of mirror picked */ 138 daddr_t sc_blk[2]; /* mirror localization */ 139 }; 140 141 static g_start_t g_ccd_start; 142 static void ccdiodone(struct bio *bp); 143 static void ccdinterleave(struct ccd_s *); 144 static int ccdinit(struct gctl_req *req, struct ccd_s *); 145 static int ccdbuffer(struct bio **ret, struct ccd_s *, 146 struct bio *, daddr_t, caddr_t, long); 147 148 static void 149 g_ccd_orphan(struct g_consumer *cp) 150 { 151 /* 152 * XXX: We don't do anything here. It is not obvious 153 * XXX: what DTRT would be, so we do what the previous 154 * XXX: code did: ignore it and let the user cope. 155 */ 156 } 157 158 static int 159 g_ccd_access(struct g_provider *pp, int dr, int dw, int de) 160 { 161 struct g_geom *gp; 162 struct g_consumer *cp1, *cp2; 163 int error; 164 165 de += dr; 166 de += dw; 167 168 gp = pp->geom; 169 error = ENXIO; 170 LIST_FOREACH(cp1, &gp->consumer, consumer) { 171 error = g_access_rel(cp1, dr, dw, de); 172 if (error) { 173 LIST_FOREACH(cp2, &gp->consumer, consumer) { 174 if (cp1 == cp2) 175 break; 176 g_access_rel(cp1, -dr, -dw, -de); 177 } 178 break; 179 } 180 } 181 return (error); 182 } 183 184 /* 185 * Free the softc and its substructures. 186 */ 187 static void 188 g_ccd_freesc(struct ccd_s *sc) 189 { 190 struct ccdiinfo *ii; 191 192 g_free(sc->sc_cinfo); 193 if (sc->sc_itable != NULL) { 194 for (ii = sc->sc_itable; ii->ii_ndisk > 0; ii++) 195 if (ii->ii_index != NULL) 196 g_free(ii->ii_index); 197 g_free(sc->sc_itable); 198 } 199 g_free(sc); 200 } 201 202 203 static int 204 ccdinit(struct gctl_req *req, struct ccd_s *cs) 205 { 206 struct ccdcinfo *ci; 207 size_t size; 208 int ix; 209 size_t minsize; 210 int maxsecsize; 211 off_t mediasize; 212 u_int sectorsize; 213 214 cs->sc_size = 0; 215 216 maxsecsize = 0; 217 minsize = 0; 218 for (ix = 0; ix < cs->sc_ndisks; ix++) { 219 ci = &cs->sc_cinfo[ix]; 220 221 mediasize = ci->ci_provider->mediasize; 222 sectorsize = ci->ci_provider->sectorsize; 223 if (sectorsize > maxsecsize) 224 maxsecsize = sectorsize; 225 size = mediasize / DEV_BSIZE - CCD_OFFSET; 226 227 /* Truncate to interleave boundary */ 228 229 if (cs->sc_ileave > 1) 230 size -= size % cs->sc_ileave; 231 232 if (size == 0) { 233 gctl_error(req, "Component %s has effective size zero", 234 ci->ci_provider->name); 235 return(ENODEV); 236 } 237 238 if (minsize == 0 || size < minsize) 239 minsize = size; 240 ci->ci_size = size; 241 cs->sc_size += size; 242 } 243 244 /* 245 * Don't allow the interleave to be smaller than 246 * the biggest component sector. 247 */ 248 if ((cs->sc_ileave > 0) && 249 (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) { 250 gctl_error(req, "Interleave to small for sector size"); 251 return(EINVAL); 252 } 253 254 /* 255 * If uniform interleave is desired set all sizes to that of 256 * the smallest component. This will guarentee that a single 257 * interleave table is generated. 258 * 259 * Lost space must be taken into account when calculating the 260 * overall size. Half the space is lost when CCDF_MIRROR is 261 * specified. 262 */ 263 if (cs->sc_flags & CCDF_UNIFORM) { 264 for (ix = 0; ix < cs->sc_ndisks; ix++) { 265 ci = &cs->sc_cinfo[ix]; 266 ci->ci_size = minsize; 267 } 268 cs->sc_size = cs->sc_ndisks * minsize; 269 } 270 271 if (cs->sc_flags & CCDF_MIRROR) { 272 /* 273 * Check to see if an even number of components 274 * have been specified. The interleave must also 275 * be non-zero in order for us to be able to 276 * guarentee the topology. 277 */ 278 if (cs->sc_ndisks % 2) { 279 gctl_error(req, 280 "Mirroring requires an even number of disks"); 281 return(EINVAL); 282 } 283 if (cs->sc_ileave == 0) { 284 gctl_error(req, 285 "An interleave must be specified when mirroring"); 286 return(EINVAL); 287 } 288 cs->sc_size = (cs->sc_ndisks/2) * minsize; 289 } 290 291 /* 292 * Construct the interleave table. 293 */ 294 ccdinterleave(cs); 295 296 /* 297 * Create pseudo-geometry based on 1MB cylinders. It's 298 * pretty close. 299 */ 300 cs->sc_secsize = maxsecsize; 301 302 return (0); 303 } 304 305 static void 306 ccdinterleave(struct ccd_s *cs) 307 { 308 struct ccdcinfo *ci, *smallci; 309 struct ccdiinfo *ii; 310 daddr_t bn, lbn; 311 int ix; 312 u_long size; 313 314 315 /* 316 * Allocate an interleave table. The worst case occurs when each 317 * of N disks is of a different size, resulting in N interleave 318 * tables. 319 * 320 * Chances are this is too big, but we don't care. 321 */ 322 size = (cs->sc_ndisks + 1) * sizeof(struct ccdiinfo); 323 cs->sc_itable = g_malloc(size, M_WAITOK | M_ZERO); 324 325 /* 326 * Trivial case: no interleave (actually interleave of disk size). 327 * Each table entry represents a single component in its entirety. 328 * 329 * An interleave of 0 may not be used with a mirror setup. 330 */ 331 if (cs->sc_ileave == 0) { 332 bn = 0; 333 ii = cs->sc_itable; 334 335 for (ix = 0; ix < cs->sc_ndisks; ix++) { 336 /* Allocate space for ii_index. */ 337 ii->ii_index = g_malloc(sizeof(int), M_WAITOK); 338 ii->ii_ndisk = 1; 339 ii->ii_startblk = bn; 340 ii->ii_startoff = 0; 341 ii->ii_index[0] = ix; 342 bn += cs->sc_cinfo[ix].ci_size; 343 ii++; 344 } 345 ii->ii_ndisk = 0; 346 return; 347 } 348 349 /* 350 * The following isn't fast or pretty; it doesn't have to be. 351 */ 352 size = 0; 353 bn = lbn = 0; 354 for (ii = cs->sc_itable; ; ii++) { 355 /* 356 * Allocate space for ii_index. We might allocate more then 357 * we use. 358 */ 359 ii->ii_index = g_malloc((sizeof(int) * cs->sc_ndisks), 360 M_WAITOK); 361 362 /* 363 * Locate the smallest of the remaining components 364 */ 365 smallci = NULL; 366 for (ci = cs->sc_cinfo; ci < &cs->sc_cinfo[cs->sc_ndisks]; 367 ci++) { 368 if (ci->ci_size > size && 369 (smallci == NULL || 370 ci->ci_size < smallci->ci_size)) { 371 smallci = ci; 372 } 373 } 374 375 /* 376 * Nobody left, all done 377 */ 378 if (smallci == NULL) { 379 ii->ii_ndisk = 0; 380 g_free(ii->ii_index); 381 ii->ii_index = NULL; 382 break; 383 } 384 385 /* 386 * Record starting logical block using an sc_ileave blocksize. 387 */ 388 ii->ii_startblk = bn / cs->sc_ileave; 389 390 /* 391 * Record starting component block using an sc_ileave 392 * blocksize. This value is relative to the beginning of 393 * a component disk. 394 */ 395 ii->ii_startoff = lbn; 396 397 /* 398 * Determine how many disks take part in this interleave 399 * and record their indices. 400 */ 401 ix = 0; 402 for (ci = cs->sc_cinfo; 403 ci < &cs->sc_cinfo[cs->sc_ndisks]; ci++) { 404 if (ci->ci_size >= smallci->ci_size) { 405 ii->ii_index[ix++] = ci - cs->sc_cinfo; 406 } 407 } 408 ii->ii_ndisk = ix; 409 bn += ix * (smallci->ci_size - size); 410 lbn = smallci->ci_size / cs->sc_ileave; 411 size = smallci->ci_size; 412 } 413 } 414 415 static void 416 g_ccd_start(struct bio *bp) 417 { 418 long bcount, rcount; 419 struct bio *cbp[2]; 420 caddr_t addr; 421 daddr_t bn; 422 int err; 423 struct ccd_s *cs; 424 425 cs = bp->bio_to->geom->softc; 426 427 /* 428 * Translate the partition-relative block number to an absolute. 429 */ 430 bn = bp->bio_offset / cs->sc_secsize; 431 432 /* 433 * Allocate component buffers and fire off the requests 434 */ 435 addr = bp->bio_data; 436 for (bcount = bp->bio_length; bcount > 0; bcount -= rcount) { 437 err = ccdbuffer(cbp, cs, bp, bn, addr, bcount); 438 if (err) { 439 bp->bio_completed += bcount; 440 if (bp->bio_error != 0) 441 bp->bio_error = err; 442 if (bp->bio_completed == bp->bio_length) 443 g_io_deliver(bp, bp->bio_error); 444 return; 445 } 446 rcount = cbp[0]->bio_length; 447 448 if (cs->sc_flags & CCDF_MIRROR) { 449 /* 450 * Mirroring. Writes go to both disks, reads are 451 * taken from whichever disk seems most appropriate. 452 * 453 * We attempt to localize reads to the disk whos arm 454 * is nearest the read request. We ignore seeks due 455 * to writes when making this determination and we 456 * also try to avoid hogging. 457 */ 458 if (cbp[0]->bio_cmd != BIO_READ) { 459 g_io_request(cbp[0], cbp[0]->bio_from); 460 g_io_request(cbp[1], cbp[1]->bio_from); 461 } else { 462 int pick = cs->sc_pick; 463 daddr_t range = cs->sc_size / 16; 464 465 if (bn < cs->sc_blk[pick] - range || 466 bn > cs->sc_blk[pick] + range 467 ) { 468 cs->sc_pick = pick = 1 - pick; 469 } 470 cs->sc_blk[pick] = bn + btodb(rcount); 471 g_io_request(cbp[pick], cbp[pick]->bio_from); 472 } 473 } else { 474 /* 475 * Not mirroring 476 */ 477 g_io_request(cbp[0], cbp[0]->bio_from); 478 } 479 bn += btodb(rcount); 480 addr += rcount; 481 } 482 } 483 484 /* 485 * Build a component buffer header. 486 */ 487 static int 488 ccdbuffer(struct bio **cb, struct ccd_s *cs, struct bio *bp, daddr_t bn, caddr_t addr, long bcount) 489 { 490 struct ccdcinfo *ci, *ci2 = NULL; 491 struct bio *cbp; 492 daddr_t cbn, cboff; 493 off_t cbc; 494 495 /* 496 * Determine which component bn falls in. 497 */ 498 cbn = bn; 499 cboff = 0; 500 501 if (cs->sc_ileave == 0) { 502 /* 503 * Serially concatenated and neither a mirror nor a parity 504 * config. This is a special case. 505 */ 506 daddr_t sblk; 507 508 sblk = 0; 509 for (ci = cs->sc_cinfo; cbn >= sblk + ci->ci_size; ci++) 510 sblk += ci->ci_size; 511 cbn -= sblk; 512 } else { 513 struct ccdiinfo *ii; 514 int ccdisk, off; 515 516 /* 517 * Calculate cbn, the logical superblock (sc_ileave chunks), 518 * and cboff, a normal block offset (DEV_BSIZE chunks) relative 519 * to cbn. 520 */ 521 cboff = cbn % cs->sc_ileave; /* DEV_BSIZE gran */ 522 cbn = cbn / cs->sc_ileave; /* DEV_BSIZE * ileave gran */ 523 524 /* 525 * Figure out which interleave table to use. 526 */ 527 for (ii = cs->sc_itable; ii->ii_ndisk; ii++) { 528 if (ii->ii_startblk > cbn) 529 break; 530 } 531 ii--; 532 533 /* 534 * off is the logical superblock relative to the beginning 535 * of this interleave block. 536 */ 537 off = cbn - ii->ii_startblk; 538 539 /* 540 * We must calculate which disk component to use (ccdisk), 541 * and recalculate cbn to be the superblock relative to 542 * the beginning of the component. This is typically done by 543 * adding 'off' and ii->ii_startoff together. However, 'off' 544 * must typically be divided by the number of components in 545 * this interleave array to be properly convert it from a 546 * CCD-relative logical superblock number to a 547 * component-relative superblock number. 548 */ 549 if (ii->ii_ndisk == 1) { 550 /* 551 * When we have just one disk, it can't be a mirror 552 * or a parity config. 553 */ 554 ccdisk = ii->ii_index[0]; 555 cbn = ii->ii_startoff + off; 556 } else { 557 if (cs->sc_flags & CCDF_MIRROR) { 558 /* 559 * We have forced a uniform mapping, resulting 560 * in a single interleave array. We double 561 * up on the first half of the available 562 * components and our mirror is in the second 563 * half. This only works with a single 564 * interleave array because doubling up 565 * doubles the number of sectors, so there 566 * cannot be another interleave array because 567 * the next interleave array's calculations 568 * would be off. 569 */ 570 int ndisk2 = ii->ii_ndisk / 2; 571 ccdisk = ii->ii_index[off % ndisk2]; 572 cbn = ii->ii_startoff + off / ndisk2; 573 ci2 = &cs->sc_cinfo[ccdisk + ndisk2]; 574 } else { 575 ccdisk = ii->ii_index[off % ii->ii_ndisk]; 576 cbn = ii->ii_startoff + off / ii->ii_ndisk; 577 } 578 } 579 580 ci = &cs->sc_cinfo[ccdisk]; 581 582 /* 583 * Convert cbn from a superblock to a normal block so it 584 * can be used to calculate (along with cboff) the normal 585 * block index into this particular disk. 586 */ 587 cbn *= cs->sc_ileave; 588 } 589 590 /* 591 * Fill in the component buf structure. 592 */ 593 cbp = g_clone_bio(bp); 594 if (cbp == NULL) 595 return (ENOMEM); 596 cbp->bio_done = g_std_done; 597 cbp->bio_offset = dbtob(cbn + cboff + CCD_OFFSET); 598 cbp->bio_data = addr; 599 if (cs->sc_ileave == 0) 600 cbc = dbtob((off_t)(ci->ci_size - cbn)); 601 else 602 cbc = dbtob((off_t)(cs->sc_ileave - cboff)); 603 cbp->bio_length = (cbc < bcount) ? cbc : bcount; 604 605 cbp->bio_from = ci->ci_consumer; 606 cb[0] = cbp; 607 608 if (cs->sc_flags & CCDF_MIRROR) { 609 cbp = g_clone_bio(bp); 610 if (cbp == NULL) 611 return (ENOMEM); 612 cbp->bio_done = cb[0]->bio_done = ccdiodone; 613 cbp->bio_offset = cb[0]->bio_offset; 614 cbp->bio_data = cb[0]->bio_data; 615 cbp->bio_length = cb[0]->bio_length; 616 cbp->bio_from = ci2->ci_consumer; 617 cbp->bio_caller1 = cb[0]; 618 cb[0]->bio_caller1 = cbp; 619 cb[1] = cbp; 620 } 621 return (0); 622 } 623 624 /* 625 * Called only for mirrored operations. 626 */ 627 static void 628 ccdiodone(struct bio *cbp) 629 { 630 struct bio *mbp, *pbp; 631 632 mbp = cbp->bio_caller1; 633 pbp = cbp->bio_parent; 634 635 if (pbp->bio_cmd == BIO_READ) { 636 if (cbp->bio_error == 0) { 637 /* We will not be needing the partner bio */ 638 if (mbp != NULL) { 639 pbp->bio_inbed++; 640 g_destroy_bio(mbp); 641 } 642 g_std_done(cbp); 643 return; 644 } 645 if (mbp != NULL) { 646 /* Try partner the bio instead */ 647 mbp->bio_caller1 = NULL; 648 pbp->bio_inbed++; 649 g_destroy_bio(cbp); 650 g_io_request(mbp, mbp->bio_from); 651 /* 652 * XXX: If this comes back OK, we should actually 653 * try to write the good data on the failed mirror 654 */ 655 return; 656 } 657 g_std_done(cbp); 658 } 659 if (mbp != NULL) { 660 mbp->bio_caller1 = NULL; 661 pbp->bio_inbed++; 662 if (cbp->bio_error != 0 && pbp->bio_error == 0) 663 pbp->bio_error = cbp->bio_error; 664 return; 665 } 666 g_std_done(cbp); 667 } 668 669 static void 670 g_ccd_create(struct gctl_req *req, struct g_class *mp) 671 { 672 int *unit, *ileave, *nprovider; 673 struct g_geom *gp; 674 struct g_consumer *cp; 675 struct g_provider *pp; 676 struct ccd_s *sc; 677 struct sbuf *sb; 678 char buf[20]; 679 int i, error; 680 681 g_topology_assert(); 682 unit = gctl_get_paraml(req, "unit", sizeof (*unit)); 683 ileave = gctl_get_paraml(req, "ileave", sizeof (*ileave)); 684 nprovider = gctl_get_paraml(req, "nprovider", sizeof (*nprovider)); 685 686 /* Check for duplicate unit */ 687 LIST_FOREACH(gp, &mp->geom, geom) { 688 sc = gp->softc; 689 if (sc->sc_unit == *unit) { 690 gctl_error(req, "Unit %d already configured", *unit); 691 return; 692 } 693 } 694 695 if (*nprovider <= 0) { 696 gctl_error(req, "Bogus nprovider argument (= %d)", *nprovider); 697 return; 698 } 699 700 /* Check all providers are valid */ 701 for (i = 0; i < *nprovider; i++) { 702 sprintf(buf, "provider%d", i); 703 pp = gctl_get_provider(req, buf); 704 if (pp == NULL) 705 return; 706 } 707 708 gp = g_new_geomf(mp, "ccd%d", *unit); 709 gp->start = g_ccd_start; 710 gp->orphan = g_ccd_orphan; 711 gp->access = g_ccd_access; 712 sc = g_malloc(sizeof *sc, M_WAITOK | M_ZERO); 713 gp->softc = sc; 714 sc->sc_ndisks = *nprovider; 715 716 /* Allocate space for the component info. */ 717 sc->sc_cinfo = g_malloc(sc->sc_ndisks * sizeof(struct ccdcinfo), 718 M_WAITOK | M_ZERO); 719 720 /* Create consumers and attach to all providers */ 721 for (i = 0; i < *nprovider; i++) { 722 sprintf(buf, "provider%d", i); 723 pp = gctl_get_provider(req, buf); 724 cp = g_new_consumer(gp); 725 error = g_attach(cp, pp); 726 KASSERT(error == 0, ("attach to %s failed", pp->name)); 727 sc->sc_cinfo[i].ci_consumer = cp; 728 sc->sc_cinfo[i].ci_provider = pp; 729 } 730 731 sc->sc_unit = *unit; 732 sc->sc_ileave = *ileave; 733 734 if (gctl_get_param(req, "uniform", NULL)) 735 sc->sc_flags |= CCDF_UNIFORM; 736 if (gctl_get_param(req, "mirror", NULL)) 737 sc->sc_flags |= CCDF_MIRROR; 738 739 if (sc->sc_ileave == 0 && (sc->sc_flags & CCDF_MIRROR)) { 740 printf("%s: disabling mirror, interleave is 0\n", gp->name); 741 sc->sc_flags &= ~(CCDF_MIRROR); 742 } 743 744 if ((sc->sc_flags & CCDF_MIRROR) && !(sc->sc_flags & CCDF_UNIFORM)) { 745 printf("%s: mirror/parity forces uniform flag\n", gp->name); 746 sc->sc_flags |= CCDF_UNIFORM; 747 } 748 749 error = ccdinit(req, sc); 750 if (error != 0) { 751 g_ccd_freesc(sc); 752 gp->softc = NULL; 753 g_wither_geom(gp, ENXIO); 754 return; 755 } 756 757 pp = g_new_providerf(gp, "%s", gp->name); 758 pp->mediasize = sc->sc_size * (off_t)sc->sc_secsize; 759 pp->sectorsize = sc->sc_secsize; 760 g_error_provider(pp, 0); 761 762 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 763 sbuf_clear(sb); 764 sbuf_printf(sb, "ccd%d: %d components ", sc->sc_unit, *nprovider); 765 for (i = 0; i < *nprovider; i++) { 766 sbuf_printf(sb, "%s%s", 767 i == 0 ? "(" : ", ", 768 sc->sc_cinfo[i].ci_provider->name); 769 } 770 sbuf_printf(sb, "), %jd blocks ", (off_t)pp->mediasize / DEV_BSIZE); 771 if (sc->sc_ileave != 0) 772 sbuf_printf(sb, "interleaved at %d blocks\n", 773 sc->sc_ileave); 774 else 775 sbuf_printf(sb, "concatenated\n"); 776 sbuf_finish(sb); 777 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 778 sbuf_delete(sb); 779 } 780 781 static void 782 g_ccd_destroy(struct gctl_req *req, struct g_class *mp) 783 { 784 struct g_geom *gp; 785 struct g_provider *pp; 786 struct ccd_s *sc; 787 788 g_topology_assert(); 789 gp = gctl_get_geom(req, mp, "geom"); 790 if (gp == NULL) 791 return; 792 sc = gp->softc; 793 pp = LIST_FIRST(&gp->provider); 794 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0) { 795 gctl_error(req, "%s is open(r%dw%de%d)", gp->name, 796 pp->acr, pp->acw, pp->ace); 797 return; 798 } 799 g_ccd_freesc(sc); 800 gp->softc = NULL; 801 g_wither_geom(gp, ENXIO); 802 } 803 804 static void 805 g_ccd_list(struct gctl_req *req, struct g_class *mp) 806 { 807 struct sbuf *sb; 808 struct ccd_s *cs; 809 struct g_geom *gp; 810 int i, unit, *up; 811 812 up = gctl_get_paraml(req, "unit", sizeof (int)); 813 unit = *up; 814 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 815 sbuf_clear(sb); 816 LIST_FOREACH(gp, &mp->geom, geom) { 817 cs = gp->softc; 818 if (unit >= 0 && unit != cs->sc_unit) 819 continue; 820 sbuf_printf(sb, "ccd%d\t\t%d\t%d\t", 821 cs->sc_unit, cs->sc_ileave, cs->sc_flags & CCDF_USERMASK); 822 823 for (i = 0; i < cs->sc_ndisks; ++i) { 824 sbuf_printf(sb, "%s/dev/%s", i == 0 ? "" : " ", 825 cs->sc_cinfo[i].ci_provider->name); 826 } 827 sbuf_printf(sb, "\n"); 828 } 829 sbuf_finish(sb); 830 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1); 831 sbuf_delete(sb); 832 } 833 834 static void 835 g_ccd_config(struct gctl_req *req, struct g_class *mp, char const *verb) 836 { 837 838 g_topology_assert(); 839 if (!strcmp(verb, "create geom")) { 840 g_ccd_create(req, mp); 841 } else if (!strcmp(verb, "destroy geom")) { 842 g_ccd_destroy(req, mp); 843 } else if (!strcmp(verb, "list")) { 844 g_ccd_list(req, mp); 845 } else { 846 gctl_error(req, "unknown verb"); 847 } 848 } 849 850 static struct g_class g_ccd_class = { 851 .name = "CCD", 852 .ctlreq = g_ccd_config, 853 }; 854 855 DECLARE_GEOM_CLASS(g_ccd_class, g_ccd); 856