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.
7 * Copyright (c) 1988 University of Utah.
9 * This code is derived from software contributed to Berkeley by
10 * the Systems Programming Group of the University of Utah Computer
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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
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.
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
40 * Dynamic configuration and disklabel support by:
41 * Jason R. Thorpe <thorpej@nas.nasa.gov>
42 * Numerical Aerodynamic Simulation Facility
44 * NASA Ames Research Center
45 * Moffett Field, CA 94035
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 $
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/module.h>
60 #include <sys/malloc.h>
61 #include <geom/geom.h>
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.
68 #if !defined(CCD_OFFSET)
73 #define CCDF_UNIFORM 0x02 /* use LCCD of sizes for uniform interleave */
74 #define CCDF_MIRROR 0x04 /* use mirroring */
76 /* Mask of user-settable ccd flags. */
77 #define CCDF_USERMASK (CCDF_UNIFORM|CCDF_MIRROR)
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),
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:
93 * ndisk startblk startoff dev
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.
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 */
113 * Component info table.
114 * Describes a single component of a concatenated disk.
117 daddr_t ci_size; /* size */
118 struct g_provider *ci_provider; /* provider */
119 struct g_consumer *ci_consumer; /* consumer */
123 * A concatenated disk is described by this structure.
127 LIST_ENTRY(ccd_s) list;
129 int sc_unit; /* logical unit number */
130 int sc_flags; /* flags */
131 daddr_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 */
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);
149 g_ccd_orphan(struct g_consumer *cp)
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.
159 g_ccd_access(struct g_provider *pp, int dr, int dw, int de)
162 struct g_consumer *cp1, *cp2;
170 LIST_FOREACH(cp1, &gp->consumer, consumer) {
171 error = g_access(cp1, dr, dw, de);
173 LIST_FOREACH(cp2, &gp->consumer, consumer) {
176 g_access(cp2, -dr, -dw, -de);
185 * Free the softc and its substructures.
188 g_ccd_freesc(struct ccd_s *sc)
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);
204 ccdinit(struct gctl_req *req, struct ccd_s *cs)
218 for (ix = 0; ix < cs->sc_ndisks; ix++) {
219 ci = &cs->sc_cinfo[ix];
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;
227 /* Truncate to interleave boundary */
229 if (cs->sc_ileave > 1)
230 size -= size % cs->sc_ileave;
233 gctl_error(req, "Component %s has effective size zero",
234 ci->ci_provider->name);
238 if (minsize == 0 || size < minsize)
245 * Don't allow the interleave to be smaller than
246 * the biggest component sector.
248 if ((cs->sc_ileave > 0) &&
249 (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) {
250 gctl_error(req, "Interleave to small for sector size");
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.
259 * Lost space must be taken into account when calculating the
260 * overall size. Half the space is lost when CCDF_MIRROR is
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;
268 cs->sc_size = cs->sc_ndisks * minsize;
271 if (cs->sc_flags & CCDF_MIRROR) {
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.
278 if (cs->sc_ndisks % 2) {
280 "Mirroring requires an even number of disks");
283 if (cs->sc_ileave == 0) {
285 "An interleave must be specified when mirroring");
288 cs->sc_size = (cs->sc_ndisks/2) * minsize;
292 * Construct the interleave table.
297 * Create pseudo-geometry based on 1MB cylinders. It's
300 cs->sc_secsize = maxsecsize;
306 ccdinterleave(struct ccd_s *cs)
308 struct ccdcinfo *ci, *smallci;
316 * Allocate an interleave table. The worst case occurs when each
317 * of N disks is of a different size, resulting in N interleave
320 * Chances are this is too big, but we don't care.
322 size = (cs->sc_ndisks + 1) * sizeof(struct ccdiinfo);
323 cs->sc_itable = g_malloc(size, M_WAITOK | M_ZERO);
326 * Trivial case: no interleave (actually interleave of disk size).
327 * Each table entry represents a single component in its entirety.
329 * An interleave of 0 may not be used with a mirror setup.
331 if (cs->sc_ileave == 0) {
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);
339 ii->ii_startblk = bn;
341 ii->ii_index[0] = ix;
342 bn += cs->sc_cinfo[ix].ci_size;
350 * The following isn't fast or pretty; it doesn't have to be.
354 for (ii = cs->sc_itable; ; ii++) {
356 * Allocate space for ii_index. We might allocate more then
359 ii->ii_index = g_malloc((sizeof(int) * cs->sc_ndisks),
363 * Locate the smallest of the remaining components
366 for (ci = cs->sc_cinfo; ci < &cs->sc_cinfo[cs->sc_ndisks];
368 if (ci->ci_size > size &&
370 ci->ci_size < smallci->ci_size)) {
376 * Nobody left, all done
378 if (smallci == NULL) {
380 g_free(ii->ii_index);
386 * Record starting logical block using an sc_ileave blocksize.
388 ii->ii_startblk = bn / cs->sc_ileave;
391 * Record starting component block using an sc_ileave
392 * blocksize. This value is relative to the beginning of
395 ii->ii_startoff = lbn;
398 * Determine how many disks take part in this interleave
399 * and record their indices.
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;
409 bn += ix * (smallci->ci_size - size);
410 lbn = smallci->ci_size / cs->sc_ileave;
411 size = smallci->ci_size;
416 g_ccd_start(struct bio *bp)
425 cs = bp->bio_to->geom->softc;
428 * Block all GETATTR requests, we wouldn't know which of our
429 * subdevices we should ship it off to.
430 * XXX: this may not be the right policy.
432 if(bp->bio_cmd == BIO_GETATTR) {
433 g_io_deliver(bp, EINVAL);
438 * Translate the partition-relative block number to an absolute.
440 bn = bp->bio_offset / cs->sc_secsize;
443 * Allocate component buffers and fire off the requests
446 for (bcount = bp->bio_length; bcount > 0; bcount -= rcount) {
447 err = ccdbuffer(cbp, cs, bp, bn, addr, bcount);
449 bp->bio_completed += bcount;
450 if (bp->bio_error == 0)
452 if (bp->bio_completed == bp->bio_length)
453 g_io_deliver(bp, bp->bio_error);
456 rcount = cbp[0]->bio_length;
458 if (cs->sc_flags & CCDF_MIRROR) {
460 * Mirroring. Writes go to both disks, reads are
461 * taken from whichever disk seems most appropriate.
463 * We attempt to localize reads to the disk whos arm
464 * is nearest the read request. We ignore seeks due
465 * to writes when making this determination and we
466 * also try to avoid hogging.
468 if (cbp[0]->bio_cmd != BIO_READ) {
469 g_io_request(cbp[0], cbp[0]->bio_from);
470 g_io_request(cbp[1], cbp[1]->bio_from);
472 int pick = cs->sc_pick;
473 daddr_t range = cs->sc_size / 16;
475 if (bn < cs->sc_blk[pick] - range ||
476 bn > cs->sc_blk[pick] + range
478 cs->sc_pick = pick = 1 - pick;
480 cs->sc_blk[pick] = bn + btodb(rcount);
481 g_io_request(cbp[pick], cbp[pick]->bio_from);
487 g_io_request(cbp[0], cbp[0]->bio_from);
495 * Build a component buffer header.
498 ccdbuffer(struct bio **cb, struct ccd_s *cs, struct bio *bp, daddr_t bn, caddr_t addr, long bcount)
500 struct ccdcinfo *ci, *ci2 = NULL;
506 * Determine which component bn falls in.
511 if (cs->sc_ileave == 0) {
513 * Serially concatenated and neither a mirror nor a parity
514 * config. This is a special case.
519 for (ci = cs->sc_cinfo; cbn >= sblk + ci->ci_size; ci++)
527 * Calculate cbn, the logical superblock (sc_ileave chunks),
528 * and cboff, a normal block offset (DEV_BSIZE chunks) relative
531 cboff = cbn % cs->sc_ileave; /* DEV_BSIZE gran */
532 cbn = cbn / cs->sc_ileave; /* DEV_BSIZE * ileave gran */
535 * Figure out which interleave table to use.
537 for (ii = cs->sc_itable; ii->ii_ndisk; ii++) {
538 if (ii->ii_startblk > cbn)
544 * off is the logical superblock relative to the beginning
545 * of this interleave block.
547 off = cbn - ii->ii_startblk;
550 * We must calculate which disk component to use (ccdisk),
551 * and recalculate cbn to be the superblock relative to
552 * the beginning of the component. This is typically done by
553 * adding 'off' and ii->ii_startoff together. However, 'off'
554 * must typically be divided by the number of components in
555 * this interleave array to be properly convert it from a
556 * CCD-relative logical superblock number to a
557 * component-relative superblock number.
559 if (ii->ii_ndisk == 1) {
561 * When we have just one disk, it can't be a mirror
562 * or a parity config.
564 ccdisk = ii->ii_index[0];
565 cbn = ii->ii_startoff + off;
567 if (cs->sc_flags & CCDF_MIRROR) {
569 * We have forced a uniform mapping, resulting
570 * in a single interleave array. We double
571 * up on the first half of the available
572 * components and our mirror is in the second
573 * half. This only works with a single
574 * interleave array because doubling up
575 * doubles the number of sectors, so there
576 * cannot be another interleave array because
577 * the next interleave array's calculations
580 int ndisk2 = ii->ii_ndisk / 2;
581 ccdisk = ii->ii_index[off % ndisk2];
582 cbn = ii->ii_startoff + off / ndisk2;
583 ci2 = &cs->sc_cinfo[ccdisk + ndisk2];
585 ccdisk = ii->ii_index[off % ii->ii_ndisk];
586 cbn = ii->ii_startoff + off / ii->ii_ndisk;
590 ci = &cs->sc_cinfo[ccdisk];
593 * Convert cbn from a superblock to a normal block so it
594 * can be used to calculate (along with cboff) the normal
595 * block index into this particular disk.
597 cbn *= cs->sc_ileave;
601 * Fill in the component buf structure.
603 cbp = g_clone_bio(bp);
606 cbp->bio_done = g_std_done;
607 cbp->bio_offset = dbtob(cbn + cboff + CCD_OFFSET);
608 cbp->bio_data = addr;
609 if (cs->sc_ileave == 0)
610 cbc = dbtob((off_t)(ci->ci_size - cbn));
612 cbc = dbtob((off_t)(cs->sc_ileave - cboff));
613 cbp->bio_length = (cbc < bcount) ? cbc : bcount;
615 cbp->bio_from = ci->ci_consumer;
618 if (cs->sc_flags & CCDF_MIRROR) {
619 cbp = g_clone_bio(bp);
622 cbp->bio_done = cb[0]->bio_done = ccdiodone;
623 cbp->bio_offset = cb[0]->bio_offset;
624 cbp->bio_data = cb[0]->bio_data;
625 cbp->bio_length = cb[0]->bio_length;
626 cbp->bio_from = ci2->ci_consumer;
627 cbp->bio_caller1 = cb[0];
628 cb[0]->bio_caller1 = cbp;
635 * Called only for mirrored operations.
638 ccdiodone(struct bio *cbp)
640 struct bio *mbp, *pbp;
642 mbp = cbp->bio_caller1;
643 pbp = cbp->bio_parent;
645 if (pbp->bio_cmd == BIO_READ) {
646 if (cbp->bio_error == 0) {
647 /* We will not be needing the partner bio */
656 /* Try partner the bio instead */
657 mbp->bio_caller1 = NULL;
660 g_io_request(mbp, mbp->bio_from);
662 * XXX: If this comes back OK, we should actually
663 * try to write the good data on the failed mirror
671 mbp->bio_caller1 = NULL;
673 if (cbp->bio_error != 0 && pbp->bio_error == 0)
674 pbp->bio_error = cbp->bio_error;
682 g_ccd_create(struct gctl_req *req, struct g_class *mp)
684 int *unit, *ileave, *nprovider;
686 struct g_consumer *cp;
687 struct g_provider *pp;
694 unit = gctl_get_paraml(req, "unit", sizeof (*unit));
695 ileave = gctl_get_paraml(req, "ileave", sizeof (*ileave));
696 nprovider = gctl_get_paraml(req, "nprovider", sizeof (*nprovider));
698 /* Check for duplicate unit */
699 LIST_FOREACH(gp, &mp->geom, geom) {
701 if (sc != NULL && sc->sc_unit == *unit) {
702 gctl_error(req, "Unit %d already configured", *unit);
707 if (*nprovider <= 0) {
708 gctl_error(req, "Bogus nprovider argument (= %d)", *nprovider);
712 /* Check all providers are valid */
713 for (i = 0; i < *nprovider; i++) {
714 sprintf(buf, "provider%d", i);
715 pp = gctl_get_provider(req, buf);
720 gp = g_new_geomf(mp, "ccd%d", *unit);
721 sc = g_malloc(sizeof *sc, M_WAITOK | M_ZERO);
723 sc->sc_ndisks = *nprovider;
725 /* Allocate space for the component info. */
726 sc->sc_cinfo = g_malloc(sc->sc_ndisks * sizeof(struct ccdcinfo),
729 /* Create consumers and attach to all providers */
730 for (i = 0; i < *nprovider; i++) {
731 sprintf(buf, "provider%d", i);
732 pp = gctl_get_provider(req, buf);
733 cp = g_new_consumer(gp);
734 error = g_attach(cp, pp);
735 KASSERT(error == 0, ("attach to %s failed", pp->name));
736 sc->sc_cinfo[i].ci_consumer = cp;
737 sc->sc_cinfo[i].ci_provider = pp;
741 sc->sc_ileave = *ileave;
743 if (gctl_get_param(req, "uniform", NULL))
744 sc->sc_flags |= CCDF_UNIFORM;
745 if (gctl_get_param(req, "mirror", NULL))
746 sc->sc_flags |= CCDF_MIRROR;
748 if (sc->sc_ileave == 0 && (sc->sc_flags & CCDF_MIRROR)) {
749 printf("%s: disabling mirror, interleave is 0\n", gp->name);
750 sc->sc_flags &= ~(CCDF_MIRROR);
753 if ((sc->sc_flags & CCDF_MIRROR) && !(sc->sc_flags & CCDF_UNIFORM)) {
754 printf("%s: mirror/parity forces uniform flag\n", gp->name);
755 sc->sc_flags |= CCDF_UNIFORM;
758 error = ccdinit(req, sc);
762 g_wither_geom(gp, ENXIO);
766 pp = g_new_providerf(gp, "%s", gp->name);
767 pp->mediasize = sc->sc_size * (off_t)sc->sc_secsize;
768 pp->sectorsize = sc->sc_secsize;
769 g_error_provider(pp, 0);
771 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
772 sbuf_printf(sb, "ccd%d: %d components ", sc->sc_unit, *nprovider);
773 for (i = 0; i < *nprovider; i++) {
774 sbuf_printf(sb, "%s%s",
776 sc->sc_cinfo[i].ci_provider->name);
778 sbuf_printf(sb, "), %jd blocks ", (off_t)pp->mediasize / DEV_BSIZE);
779 if (sc->sc_ileave != 0)
780 sbuf_printf(sb, "interleaved at %d blocks\n",
783 sbuf_printf(sb, "concatenated\n");
785 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
790 g_ccd_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
792 struct g_provider *pp;
797 pp = LIST_FIRST(&gp->provider);
798 if (sc == NULL || pp == NULL)
800 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0) {
801 gctl_error(req, "%s is open(r%dw%de%d)", gp->name,
802 pp->acr, pp->acw, pp->ace);
807 g_wither_geom(gp, ENXIO);
812 g_ccd_list(struct gctl_req *req, struct g_class *mp)
819 up = gctl_get_paraml(req, "unit", sizeof (int));
821 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
822 LIST_FOREACH(gp, &mp->geom, geom) {
824 if (cs == NULL || (unit >= 0 && unit != cs->sc_unit))
826 sbuf_printf(sb, "ccd%d\t\t%d\t%d\t",
827 cs->sc_unit, cs->sc_ileave, cs->sc_flags & CCDF_USERMASK);
829 for (i = 0; i < cs->sc_ndisks; ++i) {
830 sbuf_printf(sb, "%s/dev/%s", i == 0 ? "" : " ",
831 cs->sc_cinfo[i].ci_provider->name);
833 sbuf_printf(sb, "\n");
836 gctl_set_param(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
841 g_ccd_config(struct gctl_req *req, struct g_class *mp, char const *verb)
846 if (!strcmp(verb, "create geom")) {
847 g_ccd_create(req, mp);
848 } else if (!strcmp(verb, "destroy geom")) {
849 gp = gctl_get_geom(req, mp, "geom");
851 g_ccd_destroy_geom(req, mp, gp);
852 } else if (!strcmp(verb, "list")) {
855 gctl_error(req, "unknown verb");
859 static struct g_class g_ccd_class = {
861 .version = G_VERSION,
862 .ctlreq = g_ccd_config,
863 .destroy_geom = g_ccd_destroy_geom,
864 .start = g_ccd_start,
865 .orphan = g_ccd_orphan,
866 .access = g_ccd_access,
869 DECLARE_GEOM_CLASS(g_ccd_class, g_ccd);