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 */
75 #define CCDF_NO_OFFSET 0x08 /* do not leave space in front */
76 #define CCDF_LINUX 0x10 /* use Linux compatibility mode */
78 /* Mask of user-settable ccd flags. */
79 #define CCDF_USERMASK (CCDF_UNIFORM|CCDF_MIRROR)
82 * Interleave description table.
83 * Computed at boot time to speed irregular-interleave lookups.
84 * The idea is that we interleave in "groups". First we interleave
85 * evenly over all component disks up to the size of the smallest
86 * component (the first group), then we interleave evenly over all
87 * remaining disks up to the size of the next-smallest (second group),
90 * Each table entry describes the interleave characteristics of one
91 * of these groups. For example if a concatenated disk consisted of
92 * three components of 5, 3, and 7 DEV_BSIZE blocks interleaved at
93 * DEV_BSIZE (1), the table would have three entries:
95 * ndisk startblk startoff dev
101 * which says that the first nine blocks (0-8) are interleaved over
102 * 3 disks (0, 1, 2) starting at block offset 0 on any component disk,
103 * the next 4 blocks (9-12) are interleaved over 2 disks (0, 2) starting
104 * at component block 3, and the remaining blocks (13-14) are on disk
105 * 2 starting at offset 5.
108 int ii_ndisk; /* # of disks range is interleaved over */
109 daddr_t ii_startblk; /* starting scaled block # for range */
110 daddr_t ii_startoff; /* starting component offset (block #) */
111 int *ii_index; /* ordered list of components in range */
115 * Component info table.
116 * Describes a single component of a concatenated disk.
119 daddr_t ci_size; /* size */
120 struct g_provider *ci_provider; /* provider */
121 struct g_consumer *ci_consumer; /* consumer */
125 * A concatenated disk is described by this structure.
129 LIST_ENTRY(ccd_s) list;
131 int sc_unit; /* logical unit number */
132 int sc_flags; /* flags */
133 daddr_t sc_size; /* size of ccd */
134 int sc_ileave; /* interleave */
135 u_int sc_ndisks; /* number of components */
136 struct ccdcinfo *sc_cinfo; /* component info */
137 struct ccdiinfo *sc_itable; /* interleave table */
138 u_int32_t sc_secsize; /* # bytes per sector */
139 int sc_pick; /* side of mirror picked */
140 daddr_t sc_blk[2]; /* mirror localization */
141 u_int32_t sc_offset; /* actual offset used */
144 static g_start_t g_ccd_start;
145 static void ccdiodone(struct bio *bp);
146 static void ccdinterleave(struct ccd_s *);
147 static int ccdinit(struct gctl_req *req, struct ccd_s *);
148 static int ccdbuffer(struct bio **ret, struct ccd_s *,
149 struct bio *, daddr_t, caddr_t, long);
152 g_ccd_orphan(struct g_consumer *cp)
155 * XXX: We don't do anything here. It is not obvious
156 * XXX: what DTRT would be, so we do what the previous
157 * XXX: code did: ignore it and let the user cope.
162 g_ccd_access(struct g_provider *pp, int dr, int dw, int de)
165 struct g_consumer *cp1, *cp2;
173 LIST_FOREACH(cp1, &gp->consumer, consumer) {
174 error = g_access(cp1, dr, dw, de);
176 LIST_FOREACH(cp2, &gp->consumer, consumer) {
179 g_access(cp2, -dr, -dw, -de);
188 * Free the softc and its substructures.
191 g_ccd_freesc(struct ccd_s *sc)
195 g_free(sc->sc_cinfo);
196 if (sc->sc_itable != NULL) {
197 for (ii = sc->sc_itable; ii->ii_ndisk > 0; ii++)
198 if (ii->ii_index != NULL)
199 g_free(ii->ii_index);
200 g_free(sc->sc_itable);
207 ccdinit(struct gctl_req *req, struct ccd_s *cs)
222 if (cs->sc_flags & CCDF_LINUX) {
225 if (cs->sc_flags & CCDF_MIRROR && cs->sc_ndisks != 2)
226 gctl_error(req, "Mirror mode for Linux raids is "
227 "only supported with 2 devices");
229 if (cs->sc_flags & CCDF_NO_OFFSET)
232 cs->sc_offset = CCD_OFFSET;
235 for (ix = 0; ix < cs->sc_ndisks; ix++) {
236 ci = &cs->sc_cinfo[ix];
238 mediasize = ci->ci_provider->mediasize;
239 sectorsize = ci->ci_provider->sectorsize;
240 if (sectorsize > maxsecsize)
241 maxsecsize = sectorsize;
242 size = mediasize / DEV_BSIZE - cs->sc_offset;
244 /* Truncate to interleave boundary */
246 if (cs->sc_ileave > 1)
247 size -= size % cs->sc_ileave;
250 gctl_error(req, "Component %s has effective size zero",
251 ci->ci_provider->name);
255 if (minsize == 0 || size < minsize)
262 * Don't allow the interleave to be smaller than
263 * the biggest component sector.
265 if ((cs->sc_ileave > 0) &&
266 (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) {
267 gctl_error(req, "Interleave to small for sector size");
272 * If uniform interleave is desired set all sizes to that of
273 * the smallest component. This will guarentee that a single
274 * interleave table is generated.
276 * Lost space must be taken into account when calculating the
277 * overall size. Half the space is lost when CCDF_MIRROR is
280 if (cs->sc_flags & CCDF_UNIFORM) {
281 for (ix = 0; ix < cs->sc_ndisks; ix++) {
282 ci = &cs->sc_cinfo[ix];
283 ci->ci_size = minsize;
285 cs->sc_size = cs->sc_ndisks * minsize;
288 if (cs->sc_flags & CCDF_MIRROR) {
290 * Check to see if an even number of components
291 * have been specified. The interleave must also
292 * be non-zero in order for us to be able to
293 * guarentee the topology.
295 if (cs->sc_ndisks % 2) {
297 "Mirroring requires an even number of disks");
300 if (cs->sc_ileave == 0) {
302 "An interleave must be specified when mirroring");
305 cs->sc_size = (cs->sc_ndisks/2) * minsize;
309 * Construct the interleave table.
314 * Create pseudo-geometry based on 1MB cylinders. It's
317 cs->sc_secsize = maxsecsize;
323 ccdinterleave(struct ccd_s *cs)
325 struct ccdcinfo *ci, *smallci;
333 * Allocate an interleave table. The worst case occurs when each
334 * of N disks is of a different size, resulting in N interleave
337 * Chances are this is too big, but we don't care.
339 size = (cs->sc_ndisks + 1) * sizeof(struct ccdiinfo);
340 cs->sc_itable = g_malloc(size, M_WAITOK | M_ZERO);
343 * Trivial case: no interleave (actually interleave of disk size).
344 * Each table entry represents a single component in its entirety.
346 * An interleave of 0 may not be used with a mirror setup.
348 if (cs->sc_ileave == 0) {
352 for (ix = 0; ix < cs->sc_ndisks; ix++) {
353 /* Allocate space for ii_index. */
354 ii->ii_index = g_malloc(sizeof(int), M_WAITOK);
356 ii->ii_startblk = bn;
358 ii->ii_index[0] = ix;
359 bn += cs->sc_cinfo[ix].ci_size;
367 * The following isn't fast or pretty; it doesn't have to be.
371 for (ii = cs->sc_itable; ; ii++) {
373 * Allocate space for ii_index. We might allocate more then
376 ii->ii_index = g_malloc((sizeof(int) * cs->sc_ndisks),
380 * Locate the smallest of the remaining components
383 for (ci = cs->sc_cinfo; ci < &cs->sc_cinfo[cs->sc_ndisks];
385 if (ci->ci_size > size &&
387 ci->ci_size < smallci->ci_size)) {
393 * Nobody left, all done
395 if (smallci == NULL) {
397 g_free(ii->ii_index);
403 * Record starting logical block using an sc_ileave blocksize.
405 ii->ii_startblk = bn / cs->sc_ileave;
408 * Record starting component block using an sc_ileave
409 * blocksize. This value is relative to the beginning of
412 ii->ii_startoff = lbn;
415 * Determine how many disks take part in this interleave
416 * and record their indices.
419 for (ci = cs->sc_cinfo;
420 ci < &cs->sc_cinfo[cs->sc_ndisks]; ci++) {
421 if (ci->ci_size >= smallci->ci_size) {
422 ii->ii_index[ix++] = ci - cs->sc_cinfo;
426 bn += ix * (smallci->ci_size - size);
427 lbn = smallci->ci_size / cs->sc_ileave;
428 size = smallci->ci_size;
433 g_ccd_start(struct bio *bp)
442 cs = bp->bio_to->geom->softc;
445 * Block all GETATTR requests, we wouldn't know which of our
446 * subdevices we should ship it off to.
447 * XXX: this may not be the right policy.
449 if(bp->bio_cmd == BIO_GETATTR) {
450 g_io_deliver(bp, EINVAL);
455 * Translate the partition-relative block number to an absolute.
457 bn = bp->bio_offset / cs->sc_secsize;
460 * Allocate component buffers and fire off the requests
463 for (bcount = bp->bio_length; bcount > 0; bcount -= rcount) {
464 err = ccdbuffer(cbp, cs, bp, bn, addr, bcount);
466 bp->bio_completed += bcount;
467 if (bp->bio_error == 0)
469 if (bp->bio_completed == bp->bio_length)
470 g_io_deliver(bp, bp->bio_error);
473 rcount = cbp[0]->bio_length;
475 if (cs->sc_flags & CCDF_MIRROR) {
477 * Mirroring. Writes go to both disks, reads are
478 * taken from whichever disk seems most appropriate.
480 * We attempt to localize reads to the disk whos arm
481 * is nearest the read request. We ignore seeks due
482 * to writes when making this determination and we
483 * also try to avoid hogging.
485 if (cbp[0]->bio_cmd != BIO_READ) {
486 g_io_request(cbp[0], cbp[0]->bio_from);
487 g_io_request(cbp[1], cbp[1]->bio_from);
489 int pick = cs->sc_pick;
490 daddr_t range = cs->sc_size / 16;
492 if (bn < cs->sc_blk[pick] - range ||
493 bn > cs->sc_blk[pick] + range
495 cs->sc_pick = pick = 1 - pick;
497 cs->sc_blk[pick] = bn + btodb(rcount);
498 g_io_request(cbp[pick], cbp[pick]->bio_from);
504 g_io_request(cbp[0], cbp[0]->bio_from);
512 * Build a component buffer header.
515 ccdbuffer(struct bio **cb, struct ccd_s *cs, struct bio *bp, daddr_t bn, caddr_t addr, long bcount)
517 struct ccdcinfo *ci, *ci2 = NULL;
523 * Determine which component bn falls in.
528 if (cs->sc_ileave == 0) {
530 * Serially concatenated and neither a mirror nor a parity
531 * config. This is a special case.
536 for (ci = cs->sc_cinfo; cbn >= sblk + ci->ci_size; ci++)
544 * Calculate cbn, the logical superblock (sc_ileave chunks),
545 * and cboff, a normal block offset (DEV_BSIZE chunks) relative
548 cboff = cbn % cs->sc_ileave; /* DEV_BSIZE gran */
549 cbn = cbn / cs->sc_ileave; /* DEV_BSIZE * ileave gran */
552 * Figure out which interleave table to use.
554 for (ii = cs->sc_itable; ii->ii_ndisk; ii++) {
555 if (ii->ii_startblk > cbn)
561 * off is the logical superblock relative to the beginning
562 * of this interleave block.
564 off = cbn - ii->ii_startblk;
567 * We must calculate which disk component to use (ccdisk),
568 * and recalculate cbn to be the superblock relative to
569 * the beginning of the component. This is typically done by
570 * adding 'off' and ii->ii_startoff together. However, 'off'
571 * must typically be divided by the number of components in
572 * this interleave array to be properly convert it from a
573 * CCD-relative logical superblock number to a
574 * component-relative superblock number.
576 if (ii->ii_ndisk == 1) {
578 * When we have just one disk, it can't be a mirror
579 * or a parity config.
581 ccdisk = ii->ii_index[0];
582 cbn = ii->ii_startoff + off;
584 if (cs->sc_flags & CCDF_MIRROR) {
586 * We have forced a uniform mapping, resulting
587 * in a single interleave array. We double
588 * up on the first half of the available
589 * components and our mirror is in the second
590 * half. This only works with a single
591 * interleave array because doubling up
592 * doubles the number of sectors, so there
593 * cannot be another interleave array because
594 * the next interleave array's calculations
597 int ndisk2 = ii->ii_ndisk / 2;
598 ccdisk = ii->ii_index[off % ndisk2];
599 cbn = ii->ii_startoff + off / ndisk2;
600 ci2 = &cs->sc_cinfo[ccdisk + ndisk2];
602 ccdisk = ii->ii_index[off % ii->ii_ndisk];
603 cbn = ii->ii_startoff + off / ii->ii_ndisk;
607 ci = &cs->sc_cinfo[ccdisk];
610 * Convert cbn from a superblock to a normal block so it
611 * can be used to calculate (along with cboff) the normal
612 * block index into this particular disk.
614 cbn *= cs->sc_ileave;
618 * Fill in the component buf structure.
620 cbp = g_clone_bio(bp);
623 cbp->bio_done = g_std_done;
624 cbp->bio_offset = dbtob(cbn + cboff + cs->sc_offset);
625 cbp->bio_data = addr;
626 if (cs->sc_ileave == 0)
627 cbc = dbtob((off_t)(ci->ci_size - cbn));
629 cbc = dbtob((off_t)(cs->sc_ileave - cboff));
630 cbp->bio_length = (cbc < bcount) ? cbc : bcount;
632 cbp->bio_from = ci->ci_consumer;
635 if (cs->sc_flags & CCDF_MIRROR) {
636 cbp = g_clone_bio(bp);
639 cbp->bio_done = cb[0]->bio_done = ccdiodone;
640 cbp->bio_offset = cb[0]->bio_offset;
641 cbp->bio_data = cb[0]->bio_data;
642 cbp->bio_length = cb[0]->bio_length;
643 cbp->bio_from = ci2->ci_consumer;
644 cbp->bio_caller1 = cb[0];
645 cb[0]->bio_caller1 = cbp;
652 * Called only for mirrored operations.
655 ccdiodone(struct bio *cbp)
657 struct bio *mbp, *pbp;
659 mbp = cbp->bio_caller1;
660 pbp = cbp->bio_parent;
662 if (pbp->bio_cmd == BIO_READ) {
663 if (cbp->bio_error == 0) {
664 /* We will not be needing the partner bio */
673 /* Try partner the bio instead */
674 mbp->bio_caller1 = NULL;
677 g_io_request(mbp, mbp->bio_from);
679 * XXX: If this comes back OK, we should actually
680 * try to write the good data on the failed mirror
688 mbp->bio_caller1 = NULL;
690 if (cbp->bio_error != 0 && pbp->bio_error == 0)
691 pbp->bio_error = cbp->bio_error;
699 g_ccd_create(struct gctl_req *req, struct g_class *mp)
701 int *unit, *ileave, *nprovider;
703 struct g_consumer *cp;
704 struct g_provider *pp;
711 unit = gctl_get_paraml(req, "unit", sizeof (*unit));
713 gctl_error(req, "unit parameter not given");
716 ileave = gctl_get_paraml(req, "ileave", sizeof (*ileave));
717 if (ileave == NULL) {
718 gctl_error(req, "ileave parameter not given");
721 nprovider = gctl_get_paraml(req, "nprovider", sizeof (*nprovider));
722 if (nprovider == NULL) {
723 gctl_error(req, "nprovider parameter not given");
727 /* Check for duplicate unit */
728 LIST_FOREACH(gp, &mp->geom, geom) {
730 if (sc != NULL && sc->sc_unit == *unit) {
731 gctl_error(req, "Unit %d already configured", *unit);
736 if (*nprovider <= 0) {
737 gctl_error(req, "Bogus nprovider argument (= %d)", *nprovider);
741 /* Check all providers are valid */
742 for (i = 0; i < *nprovider; i++) {
743 sprintf(buf, "provider%d", i);
744 pp = gctl_get_provider(req, buf);
749 gp = g_new_geomf(mp, "ccd%d", *unit);
750 sc = g_malloc(sizeof *sc, M_WAITOK | M_ZERO);
752 sc->sc_ndisks = *nprovider;
754 /* Allocate space for the component info. */
755 sc->sc_cinfo = g_malloc(sc->sc_ndisks * sizeof(struct ccdcinfo),
758 /* Create consumers and attach to all providers */
759 for (i = 0; i < *nprovider; i++) {
760 sprintf(buf, "provider%d", i);
761 pp = gctl_get_provider(req, buf);
762 cp = g_new_consumer(gp);
763 error = g_attach(cp, pp);
764 KASSERT(error == 0, ("attach to %s failed", pp->name));
765 sc->sc_cinfo[i].ci_consumer = cp;
766 sc->sc_cinfo[i].ci_provider = pp;
770 sc->sc_ileave = *ileave;
772 if (gctl_get_param(req, "no_offset", NULL))
773 sc->sc_flags |= CCDF_NO_OFFSET;
774 if (gctl_get_param(req, "linux", NULL))
775 sc->sc_flags |= CCDF_LINUX;
777 if (gctl_get_param(req, "uniform", NULL))
778 sc->sc_flags |= CCDF_UNIFORM;
779 if (gctl_get_param(req, "mirror", NULL))
780 sc->sc_flags |= CCDF_MIRROR;
782 if (sc->sc_ileave == 0 && (sc->sc_flags & CCDF_MIRROR)) {
783 printf("%s: disabling mirror, interleave is 0\n", gp->name);
784 sc->sc_flags &= ~(CCDF_MIRROR);
787 if ((sc->sc_flags & CCDF_MIRROR) && !(sc->sc_flags & CCDF_UNIFORM)) {
788 printf("%s: mirror/parity forces uniform flag\n", gp->name);
789 sc->sc_flags |= CCDF_UNIFORM;
792 error = ccdinit(req, sc);
796 g_wither_geom(gp, ENXIO);
800 pp = g_new_providerf(gp, "%s", gp->name);
801 pp->mediasize = sc->sc_size * (off_t)sc->sc_secsize;
802 pp->sectorsize = sc->sc_secsize;
803 g_error_provider(pp, 0);
805 sb = sbuf_new_auto();
806 sbuf_printf(sb, "ccd%d: %d components ", sc->sc_unit, *nprovider);
807 for (i = 0; i < *nprovider; i++) {
808 sbuf_printf(sb, "%s%s",
810 sc->sc_cinfo[i].ci_provider->name);
812 sbuf_printf(sb, "), %jd blocks ", (off_t)pp->mediasize / DEV_BSIZE);
813 if (sc->sc_ileave != 0)
814 sbuf_printf(sb, "interleaved at %d blocks\n",
817 sbuf_printf(sb, "concatenated\n");
819 gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
824 g_ccd_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
826 struct g_provider *pp;
831 pp = LIST_FIRST(&gp->provider);
832 if (sc == NULL || pp == NULL)
834 if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0) {
835 gctl_error(req, "%s is open(r%dw%de%d)", gp->name,
836 pp->acr, pp->acw, pp->ace);
841 g_wither_geom(gp, ENXIO);
846 g_ccd_list(struct gctl_req *req, struct g_class *mp)
853 up = gctl_get_paraml(req, "unit", sizeof (*up));
855 gctl_error(req, "unit parameter not given");
859 sb = sbuf_new_auto();
860 LIST_FOREACH(gp, &mp->geom, geom) {
862 if (cs == NULL || (unit >= 0 && unit != cs->sc_unit))
864 sbuf_printf(sb, "ccd%d\t\t%d\t%d\t",
865 cs->sc_unit, cs->sc_ileave, cs->sc_flags & CCDF_USERMASK);
867 for (i = 0; i < cs->sc_ndisks; ++i) {
868 sbuf_printf(sb, "%s/dev/%s", i == 0 ? "" : " ",
869 cs->sc_cinfo[i].ci_provider->name);
871 sbuf_printf(sb, "\n");
874 gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
879 g_ccd_config(struct gctl_req *req, struct g_class *mp, char const *verb)
884 if (!strcmp(verb, "create geom")) {
885 g_ccd_create(req, mp);
886 } else if (!strcmp(verb, "destroy geom")) {
887 gp = gctl_get_geom(req, mp, "geom");
889 g_ccd_destroy_geom(req, mp, gp);
890 } else if (!strcmp(verb, "list")) {
893 gctl_error(req, "unknown verb");
897 static struct g_class g_ccd_class = {
899 .version = G_VERSION,
900 .ctlreq = g_ccd_config,
901 .destroy_geom = g_ccd_destroy_geom,
902 .start = g_ccd_start,
903 .orphan = g_ccd_orphan,
904 .access = g_ccd_access,
907 DECLARE_GEOM_CLASS(g_ccd_class, g_ccd);