xref: /freebsd/sys/geom/geom_ccd.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1fa521b03SWarner Losh /*-
2*b61a5730SWarner Losh  * SPDX-License-Identifier: (BSD-2-Clause AND BSD-3-Clause)
33728855aSPedro F. Giffuni  *
43b1746dfSPoul-Henning Kamp  * Copyright (c) 2003 Poul-Henning Kamp.
52afb21f3SPedro F. Giffuni  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
62afb21f3SPedro F. Giffuni  * All rights reserved.
72afb21f3SPedro F. Giffuni  *
82afb21f3SPedro F. Giffuni  * This code is derived from software contributed to The NetBSD Foundation
92afb21f3SPedro F. Giffuni  * by Jason R. Thorpe.
102afb21f3SPedro F. Giffuni  *
112afb21f3SPedro F. Giffuni  * Redistribution and use in source and binary forms, with or without
122afb21f3SPedro F. Giffuni  * modification, are permitted provided that the following conditions
132afb21f3SPedro F. Giffuni  * are met:
142afb21f3SPedro F. Giffuni  * 1. Redistributions of source code must retain the above copyright
152afb21f3SPedro F. Giffuni  *    notice, this list of conditions and the following disclaimer.
162afb21f3SPedro F. Giffuni  * 2. Redistributions in binary form must reproduce the above copyright
172afb21f3SPedro F. Giffuni  *    notice, this list of conditions and the following disclaimer in the
182afb21f3SPedro F. Giffuni  *    documentation and/or other materials provided with the distribution.
192afb21f3SPedro F. Giffuni  *
202afb21f3SPedro F. Giffuni  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
212afb21f3SPedro F. Giffuni  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
222afb21f3SPedro F. Giffuni  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
232afb21f3SPedro F. Giffuni  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
242afb21f3SPedro F. Giffuni  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
252afb21f3SPedro F. Giffuni  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
262afb21f3SPedro F. Giffuni  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
272afb21f3SPedro F. Giffuni  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
282afb21f3SPedro F. Giffuni  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
292afb21f3SPedro F. Giffuni  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
302afb21f3SPedro F. Giffuni  * POSSIBILITY OF SUCH DAMAGE.
312afb21f3SPedro F. Giffuni  *
322afb21f3SPedro F. Giffuni  * $NetBSD: ccd.c,v 1.22 1995/12/08 19:13:26 thorpej Exp $
332afb21f3SPedro F. Giffuni  */
342afb21f3SPedro F. Giffuni 
352afb21f3SPedro F. Giffuni /*-
362afb21f3SPedro F. Giffuni  * Copyright (c) 1988 University of Utah.
37a56bb8a5SSatoshi Asami  * Copyright (c) 1990, 1993
38a56bb8a5SSatoshi Asami  *	The Regents of the University of California.  All rights reserved.
39a56bb8a5SSatoshi Asami  *
40a56bb8a5SSatoshi Asami  * This code is derived from software contributed to Berkeley by
41a56bb8a5SSatoshi Asami  * the Systems Programming Group of the University of Utah Computer
42a56bb8a5SSatoshi Asami  * Science Department.
43a56bb8a5SSatoshi Asami  *
44a56bb8a5SSatoshi Asami  * Redistribution and use in source and binary forms, with or without
45a56bb8a5SSatoshi Asami  * modification, are permitted provided that the following conditions
46a56bb8a5SSatoshi Asami  * are met:
47a56bb8a5SSatoshi Asami  * 1. Redistributions of source code must retain the above copyright
48a56bb8a5SSatoshi Asami  *    notice, this list of conditions and the following disclaimer.
49a56bb8a5SSatoshi Asami  * 2. Redistributions in binary form must reproduce the above copyright
50a56bb8a5SSatoshi Asami  *    notice, this list of conditions and the following disclaimer in the
51a56bb8a5SSatoshi Asami  *    documentation and/or other materials provided with the distribution.
522afb21f3SPedro F. Giffuni  * 3. Neither the name of the University nor the names of its contributors
532afb21f3SPedro F. Giffuni  *    may be used to endorse or promote products derived from this software
542afb21f3SPedro F. Giffuni  *    without specific prior written permission.
55a56bb8a5SSatoshi Asami  *
562afb21f3SPedro F. Giffuni  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
572afb21f3SPedro F. Giffuni  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
582afb21f3SPedro F. Giffuni  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
592afb21f3SPedro F. Giffuni  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
602afb21f3SPedro F. Giffuni  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
612afb21f3SPedro F. Giffuni  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
622afb21f3SPedro F. Giffuni  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
632afb21f3SPedro F. Giffuni  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
642afb21f3SPedro F. Giffuni  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65a56bb8a5SSatoshi Asami  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66a56bb8a5SSatoshi Asami  * SUCH DAMAGE.
67a56bb8a5SSatoshi Asami  *
682afb21f3SPedro F. Giffuni  * from: Utah $Hdr: cd.c 1.6 90/11/28$
692afb21f3SPedro F. Giffuni  */
702afb21f3SPedro F. Giffuni 
712afb21f3SPedro F. Giffuni /*
72a56bb8a5SSatoshi Asami  * Dynamic configuration and disklabel support by:
73a56bb8a5SSatoshi Asami  *	Jason R. Thorpe <thorpej@nas.nasa.gov>
74a56bb8a5SSatoshi Asami  *	Numerical Aerodynamic Simulation Facility
75a56bb8a5SSatoshi Asami  *	Mail Stop 258-6
76a56bb8a5SSatoshi Asami  *	NASA Ames Research Center
77a56bb8a5SSatoshi Asami  *	Moffett Field, CA 94035
78a56bb8a5SSatoshi Asami  */
79a56bb8a5SSatoshi Asami 
80a56bb8a5SSatoshi Asami #include <sys/param.h>
81a56bb8a5SSatoshi Asami #include <sys/systm.h>
82e2a13e8cSSatoshi Asami #include <sys/kernel.h>
83b7b98418SPeter Wemm #include <sys/module.h>
849626b608SPoul-Henning Kamp #include <sys/bio.h>
85a56bb8a5SSatoshi Asami #include <sys/malloc.h>
865d807a0eSAndrey V. Elsukov #include <sys/sbuf.h>
87189337d8SPoul-Henning Kamp #include <geom/geom.h>
88a56bb8a5SSatoshi Asami 
890f557e0aSPoul-Henning Kamp /*
903a3fcd72SPoul-Henning Kamp  * Number of blocks to untouched in front of a component partition.
913a3fcd72SPoul-Henning Kamp  * This is to avoid violating its disklabel area when it starts at the
923a3fcd72SPoul-Henning Kamp  * beginning of the slice.
930f557e0aSPoul-Henning Kamp  */
943a3fcd72SPoul-Henning Kamp #if !defined(CCD_OFFSET)
953a3fcd72SPoul-Henning Kamp #define CCD_OFFSET 16
963a3fcd72SPoul-Henning Kamp #endif
973a3fcd72SPoul-Henning Kamp 
983a3fcd72SPoul-Henning Kamp /* sc_flags */
993a3fcd72SPoul-Henning Kamp #define CCDF_UNIFORM	0x02	/* use LCCD of sizes for uniform interleave */
1003a3fcd72SPoul-Henning Kamp #define CCDF_MIRROR	0x04	/* use mirroring */
1013f4f4a14SMartin Cracauer #define CCDF_NO_OFFSET	0x08	/* do not leave space in front */
1023f4f4a14SMartin Cracauer #define CCDF_LINUX	0x10	/* use Linux compatibility mode */
1033a3fcd72SPoul-Henning Kamp 
1043a3fcd72SPoul-Henning Kamp /* Mask of user-settable ccd flags. */
1053a3fcd72SPoul-Henning Kamp #define CCDF_USERMASK	(CCDF_UNIFORM|CCDF_MIRROR)
1060f557e0aSPoul-Henning Kamp 
1070f557e0aSPoul-Henning Kamp /*
1080f557e0aSPoul-Henning Kamp  * Interleave description table.
1090f557e0aSPoul-Henning Kamp  * Computed at boot time to speed irregular-interleave lookups.
1100f557e0aSPoul-Henning Kamp  * The idea is that we interleave in "groups".  First we interleave
1110f557e0aSPoul-Henning Kamp  * evenly over all component disks up to the size of the smallest
1120f557e0aSPoul-Henning Kamp  * component (the first group), then we interleave evenly over all
1130f557e0aSPoul-Henning Kamp  * remaining disks up to the size of the next-smallest (second group),
1140f557e0aSPoul-Henning Kamp  * and so on.
1150f557e0aSPoul-Henning Kamp  *
1160f557e0aSPoul-Henning Kamp  * Each table entry describes the interleave characteristics of one
1170f557e0aSPoul-Henning Kamp  * of these groups.  For example if a concatenated disk consisted of
1180f557e0aSPoul-Henning Kamp  * three components of 5, 3, and 7 DEV_BSIZE blocks interleaved at
1190f557e0aSPoul-Henning Kamp  * DEV_BSIZE (1), the table would have three entries:
1200f557e0aSPoul-Henning Kamp  *
1210f557e0aSPoul-Henning Kamp  *	ndisk	startblk	startoff	dev
1220f557e0aSPoul-Henning Kamp  *	3	0		0		0, 1, 2
1230f557e0aSPoul-Henning Kamp  *	2	9		3		0, 2
1240f557e0aSPoul-Henning Kamp  *	1	13		5		2
1250f557e0aSPoul-Henning Kamp  *	0	-		-		-
1260f557e0aSPoul-Henning Kamp  *
1270f557e0aSPoul-Henning Kamp  * which says that the first nine blocks (0-8) are interleaved over
1280f557e0aSPoul-Henning Kamp  * 3 disks (0, 1, 2) starting at block offset 0 on any component disk,
1290f557e0aSPoul-Henning Kamp  * the next 4 blocks (9-12) are interleaved over 2 disks (0, 2) starting
1300f557e0aSPoul-Henning Kamp  * at component block 3, and the remaining blocks (13-14) are on disk
1310f557e0aSPoul-Henning Kamp  * 2 starting at offset 5.
1320f557e0aSPoul-Henning Kamp  */
1330f557e0aSPoul-Henning Kamp struct ccdiinfo {
1340f557e0aSPoul-Henning Kamp 	int	ii_ndisk;	/* # of disks range is interleaved over */
1350f557e0aSPoul-Henning Kamp 	daddr_t	ii_startblk;	/* starting scaled block # for range */
1360f557e0aSPoul-Henning Kamp 	daddr_t	ii_startoff;	/* starting component offset (block #) */
1370f557e0aSPoul-Henning Kamp 	int	*ii_index;	/* ordered list of components in range */
1380f557e0aSPoul-Henning Kamp };
1390f557e0aSPoul-Henning Kamp 
1400f557e0aSPoul-Henning Kamp /*
1413a3fcd72SPoul-Henning Kamp  * Component info table.
1423a3fcd72SPoul-Henning Kamp  * Describes a single component of a concatenated disk.
1430f557e0aSPoul-Henning Kamp  */
1443a3fcd72SPoul-Henning Kamp struct ccdcinfo {
145e65b2137SPaul Saab 	daddr_t		ci_size; 		/* size */
1463a3fcd72SPoul-Henning Kamp 	struct g_provider *ci_provider;		/* provider */
1473a3fcd72SPoul-Henning Kamp 	struct g_consumer *ci_consumer;		/* consumer */
1480f557e0aSPoul-Henning Kamp };
1490f557e0aSPoul-Henning Kamp 
1500f557e0aSPoul-Henning Kamp /*
1510f557e0aSPoul-Henning Kamp  * A concatenated disk is described by this structure.
1520f557e0aSPoul-Henning Kamp  */
1533a3fcd72SPoul-Henning Kamp 
1540f557e0aSPoul-Henning Kamp struct ccd_s {
1550f557e0aSPoul-Henning Kamp 	LIST_ENTRY(ccd_s) list;
1560f557e0aSPoul-Henning Kamp 
1570f557e0aSPoul-Henning Kamp 	int		 sc_unit;		/* logical unit number */
1580f557e0aSPoul-Henning Kamp 	int		 sc_flags;		/* flags */
159e65b2137SPaul Saab 	daddr_t		 sc_size;		/* size of ccd */
1600f557e0aSPoul-Henning Kamp 	int		 sc_ileave;		/* interleave */
1613a3fcd72SPoul-Henning Kamp 	u_int		 sc_ndisks;		/* number of components */
1620f557e0aSPoul-Henning Kamp 	struct ccdcinfo	 *sc_cinfo;		/* component info */
1630f557e0aSPoul-Henning Kamp 	struct ccdiinfo	 *sc_itable;		/* interleave table */
1640ff783dcSJohn Baldwin 	uint32_t	 sc_secsize;		/* # bytes per sector */
1650f557e0aSPoul-Henning Kamp 	int		 sc_pick;		/* side of mirror picked */
1660f557e0aSPoul-Henning Kamp 	daddr_t		 sc_blk[2];		/* mirror localization */
1670ff783dcSJohn Baldwin 	uint32_t	 sc_offset;		/* actual offset used */
1680f557e0aSPoul-Henning Kamp };
1690f557e0aSPoul-Henning Kamp 
1703a3fcd72SPoul-Henning Kamp static g_start_t g_ccd_start;
17101706d20SPoul-Henning Kamp static void ccdiodone(struct bio *bp);
1723a3fcd72SPoul-Henning Kamp static void ccdinterleave(struct ccd_s *);
1733a3fcd72SPoul-Henning Kamp static int ccdinit(struct gctl_req *req, struct ccd_s *);
1743a3fcd72SPoul-Henning Kamp static int ccdbuffer(struct bio **ret, struct ccd_s *,
17501706d20SPoul-Henning Kamp 		      struct bio *, daddr_t, caddr_t, long);
176a56bb8a5SSatoshi Asami 
177e2738b4fSPoul-Henning Kamp static void
g_ccd_orphan(struct g_consumer * cp)1783a3fcd72SPoul-Henning Kamp g_ccd_orphan(struct g_consumer *cp)
179a56bb8a5SSatoshi Asami {
180c44c213fSPoul-Henning Kamp 	/*
181c44c213fSPoul-Henning Kamp 	 * XXX: We don't do anything here.  It is not obvious
182c44c213fSPoul-Henning Kamp 	 * XXX: what DTRT would be, so we do what the previous
183c44c213fSPoul-Henning Kamp 	 * XXX: code did: ignore it and let the user cope.
184c44c213fSPoul-Henning Kamp 	 */
185b7b98418SPeter Wemm }
186d8594dfbSSatoshi Asami 
187b7b98418SPeter Wemm static int
g_ccd_access(struct g_provider * pp,int dr,int dw,int de)1883a3fcd72SPoul-Henning Kamp g_ccd_access(struct g_provider *pp, int dr, int dw, int de)
189b7b98418SPeter Wemm {
1903a3fcd72SPoul-Henning Kamp 	struct g_geom *gp;
1913a3fcd72SPoul-Henning Kamp 	struct g_consumer *cp1, *cp2;
1923a3fcd72SPoul-Henning Kamp 	int error;
193b7b98418SPeter Wemm 
1943a3fcd72SPoul-Henning Kamp 	de += dr;
1953a3fcd72SPoul-Henning Kamp 	de += dw;
1963a3fcd72SPoul-Henning Kamp 
1973a3fcd72SPoul-Henning Kamp 	gp = pp->geom;
1983a3fcd72SPoul-Henning Kamp 	error = ENXIO;
1993a3fcd72SPoul-Henning Kamp 	LIST_FOREACH(cp1, &gp->consumer, consumer) {
200d2bae332SPoul-Henning Kamp 		error = g_access(cp1, dr, dw, de);
2013a3fcd72SPoul-Henning Kamp 		if (error) {
2023a3fcd72SPoul-Henning Kamp 			LIST_FOREACH(cp2, &gp->consumer, consumer) {
2033a3fcd72SPoul-Henning Kamp 				if (cp1 == cp2)
204b7b98418SPeter Wemm 					break;
205d2bae332SPoul-Henning Kamp 				g_access(cp2, -dr, -dw, -de);
2063a3fcd72SPoul-Henning Kamp 			}
207b7b98418SPeter Wemm 			break;
2083a3fcd72SPoul-Henning Kamp 		}
209e2a13e8cSSatoshi Asami 	}
210b7b98418SPeter Wemm 	return (error);
211e2a13e8cSSatoshi Asami }
212b7b98418SPeter Wemm 
2133a3fcd72SPoul-Henning Kamp /*
2143a3fcd72SPoul-Henning Kamp  * Free the softc and its substructures.
2153a3fcd72SPoul-Henning Kamp  */
2163a3fcd72SPoul-Henning Kamp static void
g_ccd_freesc(struct ccd_s * sc)2173a3fcd72SPoul-Henning Kamp g_ccd_freesc(struct ccd_s *sc)
2183a3fcd72SPoul-Henning Kamp {
2193a3fcd72SPoul-Henning Kamp 	struct ccdiinfo *ii;
2203a3fcd72SPoul-Henning Kamp 
2213a3fcd72SPoul-Henning Kamp 	g_free(sc->sc_cinfo);
2223a3fcd72SPoul-Henning Kamp 	if (sc->sc_itable != NULL) {
2233a3fcd72SPoul-Henning Kamp 		for (ii = sc->sc_itable; ii->ii_ndisk > 0; ii++)
2243a3fcd72SPoul-Henning Kamp 			g_free(ii->ii_index);
2253a3fcd72SPoul-Henning Kamp 		g_free(sc->sc_itable);
2263a3fcd72SPoul-Henning Kamp 	}
2273a3fcd72SPoul-Henning Kamp 	g_free(sc);
2283a3fcd72SPoul-Henning Kamp }
2293a3fcd72SPoul-Henning Kamp 
230a56bb8a5SSatoshi Asami static int
ccdinit(struct gctl_req * req,struct ccd_s * cs)2313a3fcd72SPoul-Henning Kamp ccdinit(struct gctl_req *req, struct ccd_s *cs)
232a56bb8a5SSatoshi Asami {
2333a3fcd72SPoul-Henning Kamp 	struct ccdcinfo *ci;
234e65b2137SPaul Saab 	daddr_t size;
2351464240eSMatthew Dillon 	int ix;
236e65b2137SPaul Saab 	daddr_t minsize;
237a56bb8a5SSatoshi Asami 	int maxsecsize;
238ffee6e99SPoul-Henning Kamp 	off_t mediasize;
239ffee6e99SPoul-Henning Kamp 	u_int sectorsize;
240a56bb8a5SSatoshi Asami 
241a56bb8a5SSatoshi Asami 	cs->sc_size = 0;
242a56bb8a5SSatoshi Asami 
243a56bb8a5SSatoshi Asami 	maxsecsize = 0;
244a56bb8a5SSatoshi Asami 	minsize = 0;
2453f4f4a14SMartin Cracauer 
2463f4f4a14SMartin Cracauer 	if (cs->sc_flags & CCDF_LINUX) {
2473f4f4a14SMartin Cracauer 		cs->sc_offset = 0;
2483f4f4a14SMartin Cracauer 		cs->sc_ileave *= 2;
2493f4f4a14SMartin Cracauer 		if (cs->sc_flags & CCDF_MIRROR && cs->sc_ndisks != 2)
2503f4f4a14SMartin Cracauer 			gctl_error(req, "Mirror mode for Linux raids is "
2513f4f4a14SMartin Cracauer 			                "only supported with 2 devices");
2523f4f4a14SMartin Cracauer 	} else {
2533f4f4a14SMartin Cracauer 		if (cs->sc_flags & CCDF_NO_OFFSET)
2543f4f4a14SMartin Cracauer 			cs->sc_offset = 0;
2553f4f4a14SMartin Cracauer 		else
2563f4f4a14SMartin Cracauer 			cs->sc_offset = CCD_OFFSET;
2573f4f4a14SMartin Cracauer 	}
2583a3fcd72SPoul-Henning Kamp 	for (ix = 0; ix < cs->sc_ndisks; ix++) {
259a56bb8a5SSatoshi Asami 		ci = &cs->sc_cinfo[ix];
260a56bb8a5SSatoshi Asami 
2613a3fcd72SPoul-Henning Kamp 		mediasize = ci->ci_provider->mediasize;
2623a3fcd72SPoul-Henning Kamp 		sectorsize = ci->ci_provider->sectorsize;
263ffee6e99SPoul-Henning Kamp 		if (sectorsize > maxsecsize)
264ffee6e99SPoul-Henning Kamp 			maxsecsize = sectorsize;
2653f4f4a14SMartin Cracauer 		size = mediasize / DEV_BSIZE - cs->sc_offset;
266a56bb8a5SSatoshi Asami 
2673a3fcd72SPoul-Henning Kamp 		/* Truncate to interleave boundary */
268a56bb8a5SSatoshi Asami 
269a56bb8a5SSatoshi Asami 		if (cs->sc_ileave > 1)
270a56bb8a5SSatoshi Asami 			size -= size % cs->sc_ileave;
271a56bb8a5SSatoshi Asami 
272a56bb8a5SSatoshi Asami 		if (size == 0) {
2733a3fcd72SPoul-Henning Kamp 			gctl_error(req, "Component %s has effective size zero",
2743a3fcd72SPoul-Henning Kamp 			    ci->ci_provider->name);
2753a3fcd72SPoul-Henning Kamp 			return(ENODEV);
276a56bb8a5SSatoshi Asami 		}
277a56bb8a5SSatoshi Asami 
278a56bb8a5SSatoshi Asami 		if (minsize == 0 || size < minsize)
279a56bb8a5SSatoshi Asami 			minsize = size;
280a56bb8a5SSatoshi Asami 		ci->ci_size = size;
281a56bb8a5SSatoshi Asami 		cs->sc_size += size;
282a56bb8a5SSatoshi Asami 	}
283a56bb8a5SSatoshi Asami 
284a56bb8a5SSatoshi Asami 	/*
285a56bb8a5SSatoshi Asami 	 * Don't allow the interleave to be smaller than
286a56bb8a5SSatoshi Asami 	 * the biggest component sector.
287a56bb8a5SSatoshi Asami 	 */
288a56bb8a5SSatoshi Asami 	if ((cs->sc_ileave > 0) &&
289a56bb8a5SSatoshi Asami 	    (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) {
2903a3fcd72SPoul-Henning Kamp 		gctl_error(req, "Interleave to small for sector size");
2913a3fcd72SPoul-Henning Kamp 		return(EINVAL);
292a56bb8a5SSatoshi Asami 	}
293a56bb8a5SSatoshi Asami 
294a56bb8a5SSatoshi Asami 	/*
295a56bb8a5SSatoshi Asami 	 * If uniform interleave is desired set all sizes to that of
296e8d57122SPedro F. Giffuni 	 * the smallest component.  This will guarantee that a single
2971464240eSMatthew Dillon 	 * interleave table is generated.
2981464240eSMatthew Dillon 	 *
2991464240eSMatthew Dillon 	 * Lost space must be taken into account when calculating the
3001464240eSMatthew Dillon 	 * overall size.  Half the space is lost when CCDF_MIRROR is
301ddbf51afSPoul-Henning Kamp 	 * specified.
302a56bb8a5SSatoshi Asami 	 */
30301706d20SPoul-Henning Kamp 	if (cs->sc_flags & CCDF_UNIFORM) {
3043a3fcd72SPoul-Henning Kamp 		for (ix = 0; ix < cs->sc_ndisks; ix++) {
3053a3fcd72SPoul-Henning Kamp 			ci = &cs->sc_cinfo[ix];
306a56bb8a5SSatoshi Asami 			ci->ci_size = minsize;
3071464240eSMatthew Dillon 		}
3083a3fcd72SPoul-Henning Kamp 		cs->sc_size = cs->sc_ndisks * minsize;
3093a3fcd72SPoul-Henning Kamp 	}
3103a3fcd72SPoul-Henning Kamp 
31101706d20SPoul-Henning Kamp 	if (cs->sc_flags & CCDF_MIRROR) {
31234f35216SSatoshi Asami 		/*
31334f35216SSatoshi Asami 		 * Check to see if an even number of components
3141464240eSMatthew Dillon 		 * have been specified.  The interleave must also
3151464240eSMatthew Dillon 		 * be non-zero in order for us to be able to
316e8d57122SPedro F. Giffuni 		 * guarantee the topology.
31734f35216SSatoshi Asami 		 */
3183a3fcd72SPoul-Henning Kamp 		if (cs->sc_ndisks % 2) {
3193a3fcd72SPoul-Henning Kamp 			gctl_error(req,
3203a3fcd72SPoul-Henning Kamp 			      "Mirroring requires an even number of disks");
3213a3fcd72SPoul-Henning Kamp 			return(EINVAL);
32234f35216SSatoshi Asami 		}
3231464240eSMatthew Dillon 		if (cs->sc_ileave == 0) {
3243a3fcd72SPoul-Henning Kamp 			gctl_error(req,
3253a3fcd72SPoul-Henning Kamp 			     "An interleave must be specified when mirroring");
3263a3fcd72SPoul-Henning Kamp 			return(EINVAL);
32709b59204SSatoshi Asami 		}
3283a3fcd72SPoul-Henning Kamp 		cs->sc_size = (cs->sc_ndisks/2) * minsize;
3291464240eSMatthew Dillon 	}
330a56bb8a5SSatoshi Asami 
331a56bb8a5SSatoshi Asami 	/*
332a56bb8a5SSatoshi Asami 	 * Construct the interleave table.
333a56bb8a5SSatoshi Asami 	 */
3343a3fcd72SPoul-Henning Kamp 	ccdinterleave(cs);
335a56bb8a5SSatoshi Asami 
336a56bb8a5SSatoshi Asami 	/*
337a56bb8a5SSatoshi Asami 	 * Create pseudo-geometry based on 1MB cylinders.  It's
338a56bb8a5SSatoshi Asami 	 * pretty close.
339a56bb8a5SSatoshi Asami 	 */
3403a3fcd72SPoul-Henning Kamp 	cs->sc_secsize = maxsecsize;
341a56bb8a5SSatoshi Asami 
342a56bb8a5SSatoshi Asami 	return (0);
343a56bb8a5SSatoshi Asami }
344a56bb8a5SSatoshi Asami 
345a56bb8a5SSatoshi Asami static void
ccdinterleave(struct ccd_s * cs)3463a3fcd72SPoul-Henning Kamp ccdinterleave(struct ccd_s *cs)
347a56bb8a5SSatoshi Asami {
3481464240eSMatthew Dillon 	struct ccdcinfo *ci, *smallci;
3491464240eSMatthew Dillon 	struct ccdiinfo *ii;
3501464240eSMatthew Dillon 	daddr_t bn, lbn;
3511464240eSMatthew Dillon 	int ix;
352e65b2137SPaul Saab 	daddr_t size;
353a56bb8a5SSatoshi Asami 
354a56bb8a5SSatoshi Asami 	/*
3551464240eSMatthew Dillon 	 * Allocate an interleave table.  The worst case occurs when each
3561464240eSMatthew Dillon 	 * of N disks is of a different size, resulting in N interleave
3571464240eSMatthew Dillon 	 * tables.
3581464240eSMatthew Dillon 	 *
359a56bb8a5SSatoshi Asami 	 * Chances are this is too big, but we don't care.
360a56bb8a5SSatoshi Asami 	 */
3613a3fcd72SPoul-Henning Kamp 	size = (cs->sc_ndisks + 1) * sizeof(struct ccdiinfo);
3623a3fcd72SPoul-Henning Kamp 	cs->sc_itable = g_malloc(size, M_WAITOK | M_ZERO);
363a56bb8a5SSatoshi Asami 
364a56bb8a5SSatoshi Asami 	/*
365a56bb8a5SSatoshi Asami 	 * Trivial case: no interleave (actually interleave of disk size).
366a56bb8a5SSatoshi Asami 	 * Each table entry represents a single component in its entirety.
3671464240eSMatthew Dillon 	 *
368ddbf51afSPoul-Henning Kamp 	 * An interleave of 0 may not be used with a mirror setup.
369a56bb8a5SSatoshi Asami 	 */
370a56bb8a5SSatoshi Asami 	if (cs->sc_ileave == 0) {
371a56bb8a5SSatoshi Asami 		bn = 0;
372a56bb8a5SSatoshi Asami 		ii = cs->sc_itable;
373a56bb8a5SSatoshi Asami 
3743a3fcd72SPoul-Henning Kamp 		for (ix = 0; ix < cs->sc_ndisks; ix++) {
375a56bb8a5SSatoshi Asami 			/* Allocate space for ii_index. */
3763a3fcd72SPoul-Henning Kamp 			ii->ii_index = g_malloc(sizeof(int), M_WAITOK);
377a56bb8a5SSatoshi Asami 			ii->ii_ndisk = 1;
378a56bb8a5SSatoshi Asami 			ii->ii_startblk = bn;
379a56bb8a5SSatoshi Asami 			ii->ii_startoff = 0;
380a56bb8a5SSatoshi Asami 			ii->ii_index[0] = ix;
381a56bb8a5SSatoshi Asami 			bn += cs->sc_cinfo[ix].ci_size;
382a56bb8a5SSatoshi Asami 			ii++;
383a56bb8a5SSatoshi Asami 		}
384a56bb8a5SSatoshi Asami 		ii->ii_ndisk = 0;
385a56bb8a5SSatoshi Asami 		return;
386a56bb8a5SSatoshi Asami 	}
387a56bb8a5SSatoshi Asami 
388a56bb8a5SSatoshi Asami 	/*
389a56bb8a5SSatoshi Asami 	 * The following isn't fast or pretty; it doesn't have to be.
390a56bb8a5SSatoshi Asami 	 */
391a56bb8a5SSatoshi Asami 	size = 0;
392a56bb8a5SSatoshi Asami 	bn = lbn = 0;
393a56bb8a5SSatoshi Asami 	for (ii = cs->sc_itable; ; ii++) {
3941464240eSMatthew Dillon 		/*
3951464240eSMatthew Dillon 		 * Allocate space for ii_index.  We might allocate more then
3961464240eSMatthew Dillon 		 * we use.
3971464240eSMatthew Dillon 		 */
3983a3fcd72SPoul-Henning Kamp 		ii->ii_index = g_malloc((sizeof(int) * cs->sc_ndisks),
3993a3fcd72SPoul-Henning Kamp 		    M_WAITOK);
400a56bb8a5SSatoshi Asami 
401a56bb8a5SSatoshi Asami 		/*
402a56bb8a5SSatoshi Asami 		 * Locate the smallest of the remaining components
403a56bb8a5SSatoshi Asami 		 */
404a56bb8a5SSatoshi Asami 		smallci = NULL;
4053a3fcd72SPoul-Henning Kamp 		for (ci = cs->sc_cinfo; ci < &cs->sc_cinfo[cs->sc_ndisks];
4061464240eSMatthew Dillon 		    ci++) {
407a56bb8a5SSatoshi Asami 			if (ci->ci_size > size &&
408a56bb8a5SSatoshi Asami 			    (smallci == NULL ||
4091464240eSMatthew Dillon 			     ci->ci_size < smallci->ci_size)) {
410a56bb8a5SSatoshi Asami 				smallci = ci;
4111464240eSMatthew Dillon 			}
4121464240eSMatthew Dillon 		}
413a56bb8a5SSatoshi Asami 
414a56bb8a5SSatoshi Asami 		/*
415a56bb8a5SSatoshi Asami 		 * Nobody left, all done
416a56bb8a5SSatoshi Asami 		 */
417a56bb8a5SSatoshi Asami 		if (smallci == NULL) {
418a56bb8a5SSatoshi Asami 			ii->ii_ndisk = 0;
4193a3fcd72SPoul-Henning Kamp 			g_free(ii->ii_index);
4203a3fcd72SPoul-Henning Kamp 			ii->ii_index = NULL;
421a56bb8a5SSatoshi Asami 			break;
422a56bb8a5SSatoshi Asami 		}
423a56bb8a5SSatoshi Asami 
424a56bb8a5SSatoshi Asami 		/*
4251464240eSMatthew Dillon 		 * Record starting logical block using an sc_ileave blocksize.
426a56bb8a5SSatoshi Asami 		 */
427a56bb8a5SSatoshi Asami 		ii->ii_startblk = bn / cs->sc_ileave;
4281464240eSMatthew Dillon 
4291464240eSMatthew Dillon 		/*
4303a3fcd72SPoul-Henning Kamp 		 * Record starting component block using an sc_ileave
4311464240eSMatthew Dillon 		 * blocksize.  This value is relative to the beginning of
4321464240eSMatthew Dillon 		 * a component disk.
4331464240eSMatthew Dillon 		 */
434a56bb8a5SSatoshi Asami 		ii->ii_startoff = lbn;
435a56bb8a5SSatoshi Asami 
436a56bb8a5SSatoshi Asami 		/*
437a56bb8a5SSatoshi Asami 		 * Determine how many disks take part in this interleave
438a56bb8a5SSatoshi Asami 		 * and record their indices.
439a56bb8a5SSatoshi Asami 		 */
440a56bb8a5SSatoshi Asami 		ix = 0;
441a56bb8a5SSatoshi Asami 		for (ci = cs->sc_cinfo;
4423a3fcd72SPoul-Henning Kamp 		    ci < &cs->sc_cinfo[cs->sc_ndisks]; ci++) {
4431464240eSMatthew Dillon 			if (ci->ci_size >= smallci->ci_size) {
444a56bb8a5SSatoshi Asami 				ii->ii_index[ix++] = ci - cs->sc_cinfo;
4451464240eSMatthew Dillon 			}
4461464240eSMatthew Dillon 		}
447a56bb8a5SSatoshi Asami 		ii->ii_ndisk = ix;
448a56bb8a5SSatoshi Asami 		bn += ix * (smallci->ci_size - size);
449a56bb8a5SSatoshi Asami 		lbn = smallci->ci_size / cs->sc_ileave;
450a56bb8a5SSatoshi Asami 		size = smallci->ci_size;
451a56bb8a5SSatoshi Asami 	}
452a56bb8a5SSatoshi Asami }
453a56bb8a5SSatoshi Asami 
454e2738b4fSPoul-Henning Kamp static void
g_ccd_start(struct bio * bp)4553a3fcd72SPoul-Henning Kamp g_ccd_start(struct bio *bp)
456a56bb8a5SSatoshi Asami {
4571464240eSMatthew Dillon 	long bcount, rcount;
4583a3fcd72SPoul-Henning Kamp 	struct bio *cbp[2];
459a56bb8a5SSatoshi Asami 	caddr_t addr;
460a56bb8a5SSatoshi Asami 	daddr_t bn;
4610f76d6d8SPoul-Henning Kamp 	int err;
4623a3fcd72SPoul-Henning Kamp 	struct ccd_s *cs;
4633a3fcd72SPoul-Henning Kamp 
4643a3fcd72SPoul-Henning Kamp 	cs = bp->bio_to->geom->softc;
465a56bb8a5SSatoshi Asami 
466a56bb8a5SSatoshi Asami 	/*
467e30b2edaSPoul-Henning Kamp 	 * Block all GETATTR requests, we wouldn't know which of our
468e30b2edaSPoul-Henning Kamp 	 * subdevices we should ship it off to.
469e30b2edaSPoul-Henning Kamp 	 * XXX: this may not be the right policy.
470e30b2edaSPoul-Henning Kamp 	 */
471e30b2edaSPoul-Henning Kamp 	if(bp->bio_cmd == BIO_GETATTR) {
472e30b2edaSPoul-Henning Kamp 		g_io_deliver(bp, EINVAL);
473e30b2edaSPoul-Henning Kamp 		return;
474e30b2edaSPoul-Henning Kamp 	}
475e30b2edaSPoul-Henning Kamp 
476e30b2edaSPoul-Henning Kamp 	/*
477a56bb8a5SSatoshi Asami 	 * Translate the partition-relative block number to an absolute.
478a56bb8a5SSatoshi Asami 	 */
4793a3fcd72SPoul-Henning Kamp 	bn = bp->bio_offset / cs->sc_secsize;
480a56bb8a5SSatoshi Asami 
481a56bb8a5SSatoshi Asami 	/*
482a56bb8a5SSatoshi Asami 	 * Allocate component buffers and fire off the requests
483a56bb8a5SSatoshi Asami 	 */
4849d7f7369SPoul-Henning Kamp 	addr = bp->bio_data;
4853a3fcd72SPoul-Henning Kamp 	for (bcount = bp->bio_length; bcount > 0; bcount -= rcount) {
4860f76d6d8SPoul-Henning Kamp 		err = ccdbuffer(cbp, cs, bp, bn, addr, bcount);
4870f76d6d8SPoul-Henning Kamp 		if (err) {
488ec421aecSPoul-Henning Kamp 			bp->bio_completed += bcount;
489c12ec49eSPoul-Henning Kamp 			if (bp->bio_error == 0)
49077154759SPoul-Henning Kamp 				bp->bio_error = err;
491ec421aecSPoul-Henning Kamp 			if (bp->bio_completed == bp->bio_length)
492ec421aecSPoul-Henning Kamp 				g_io_deliver(bp, bp->bio_error);
4930f76d6d8SPoul-Henning Kamp 			return;
4940f76d6d8SPoul-Henning Kamp 		}
4953a3fcd72SPoul-Henning Kamp 		rcount = cbp[0]->bio_length;
4961464240eSMatthew Dillon 
4973a3fcd72SPoul-Henning Kamp 		if (cs->sc_flags & CCDF_MIRROR) {
4981464240eSMatthew Dillon 			/*
4991464240eSMatthew Dillon 			 * Mirroring.  Writes go to both disks, reads are
5001464240eSMatthew Dillon 			 * taken from whichever disk seems most appropriate.
5011464240eSMatthew Dillon 			 *
5021464240eSMatthew Dillon 			 * We attempt to localize reads to the disk whos arm
5031464240eSMatthew Dillon 			 * is nearest the read request.  We ignore seeks due
5041464240eSMatthew Dillon 			 * to writes when making this determination and we
5051464240eSMatthew Dillon 			 * also try to avoid hogging.
5061464240eSMatthew Dillon 			 */
5073a3fcd72SPoul-Henning Kamp 			if (cbp[0]->bio_cmd != BIO_READ) {
5083a3fcd72SPoul-Henning Kamp 				g_io_request(cbp[0], cbp[0]->bio_from);
5093a3fcd72SPoul-Henning Kamp 				g_io_request(cbp[1], cbp[1]->bio_from);
5101464240eSMatthew Dillon 			} else {
5111464240eSMatthew Dillon 				int pick = cs->sc_pick;
5121464240eSMatthew Dillon 				daddr_t range = cs->sc_size / 16;
5131464240eSMatthew Dillon 
5141464240eSMatthew Dillon 				if (bn < cs->sc_blk[pick] - range ||
5151464240eSMatthew Dillon 				    bn > cs->sc_blk[pick] + range
5161464240eSMatthew Dillon 				) {
5171464240eSMatthew Dillon 					cs->sc_pick = pick = 1 - pick;
5181464240eSMatthew Dillon 				}
5191464240eSMatthew Dillon 				cs->sc_blk[pick] = bn + btodb(rcount);
5203a3fcd72SPoul-Henning Kamp 				g_io_request(cbp[pick], cbp[pick]->bio_from);
5211464240eSMatthew Dillon 			}
5221464240eSMatthew Dillon 		} else {
5231464240eSMatthew Dillon 			/*
5241464240eSMatthew Dillon 			 * Not mirroring
5251464240eSMatthew Dillon 			 */
5263a3fcd72SPoul-Henning Kamp 			g_io_request(cbp[0], cbp[0]->bio_from);
5273bc746beSSatoshi Asami 		}
528a56bb8a5SSatoshi Asami 		bn += btodb(rcount);
529a56bb8a5SSatoshi Asami 		addr += rcount;
530a56bb8a5SSatoshi Asami 	}
531a56bb8a5SSatoshi Asami }
532a56bb8a5SSatoshi Asami 
533a56bb8a5SSatoshi Asami /*
534a56bb8a5SSatoshi Asami  * Build a component buffer header.
535a56bb8a5SSatoshi Asami  */
5360f76d6d8SPoul-Henning Kamp static int
ccdbuffer(struct bio ** cb,struct ccd_s * cs,struct bio * bp,daddr_t bn,caddr_t addr,long bcount)5373a3fcd72SPoul-Henning Kamp ccdbuffer(struct bio **cb, struct ccd_s *cs, struct bio *bp, daddr_t bn, caddr_t addr, long bcount)
538a56bb8a5SSatoshi Asami {
539ec421aecSPoul-Henning Kamp 	struct ccdcinfo *ci, *ci2 = NULL;
5403a3fcd72SPoul-Henning Kamp 	struct bio *cbp;
5411464240eSMatthew Dillon 	daddr_t cbn, cboff;
5421464240eSMatthew Dillon 	off_t cbc;
543a56bb8a5SSatoshi Asami 
544a56bb8a5SSatoshi Asami 	/*
545a56bb8a5SSatoshi Asami 	 * Determine which component bn falls in.
546a56bb8a5SSatoshi Asami 	 */
547a56bb8a5SSatoshi Asami 	cbn = bn;
548a56bb8a5SSatoshi Asami 	cboff = 0;
549a56bb8a5SSatoshi Asami 
550a56bb8a5SSatoshi Asami 	if (cs->sc_ileave == 0) {
5511464240eSMatthew Dillon 		/*
5521464240eSMatthew Dillon 		 * Serially concatenated and neither a mirror nor a parity
5531464240eSMatthew Dillon 		 * config.  This is a special case.
5541464240eSMatthew Dillon 		 */
5551464240eSMatthew Dillon 		daddr_t sblk;
556a56bb8a5SSatoshi Asami 
557a56bb8a5SSatoshi Asami 		sblk = 0;
558a56bb8a5SSatoshi Asami 		for (ci = cs->sc_cinfo; cbn >= sblk + ci->ci_size; ci++)
559a56bb8a5SSatoshi Asami 			sblk += ci->ci_size;
560a56bb8a5SSatoshi Asami 		cbn -= sblk;
5611464240eSMatthew Dillon 	} else {
5621464240eSMatthew Dillon 		struct ccdiinfo *ii;
563a56bb8a5SSatoshi Asami 		int ccdisk, off;
564a56bb8a5SSatoshi Asami 
5651464240eSMatthew Dillon 		/*
5661464240eSMatthew Dillon 		 * Calculate cbn, the logical superblock (sc_ileave chunks),
5671464240eSMatthew Dillon 		 * and cboff, a normal block offset (DEV_BSIZE chunks) relative
5681464240eSMatthew Dillon 		 * to cbn.
5691464240eSMatthew Dillon 		 */
5701464240eSMatthew Dillon 		cboff = cbn % cs->sc_ileave;	/* DEV_BSIZE gran */
5711464240eSMatthew Dillon 		cbn = cbn / cs->sc_ileave;	/* DEV_BSIZE * ileave gran */
5721464240eSMatthew Dillon 
5731464240eSMatthew Dillon 		/*
5741464240eSMatthew Dillon 		 * Figure out which interleave table to use.
5751464240eSMatthew Dillon 		 */
5761464240eSMatthew Dillon 		for (ii = cs->sc_itable; ii->ii_ndisk; ii++) {
577a56bb8a5SSatoshi Asami 			if (ii->ii_startblk > cbn)
578a56bb8a5SSatoshi Asami 				break;
5791464240eSMatthew Dillon 		}
580a56bb8a5SSatoshi Asami 		ii--;
5811464240eSMatthew Dillon 
5821464240eSMatthew Dillon 		/*
5831464240eSMatthew Dillon 		 * off is the logical superblock relative to the beginning
5841464240eSMatthew Dillon 		 * of this interleave block.
5851464240eSMatthew Dillon 		 */
586a56bb8a5SSatoshi Asami 		off = cbn - ii->ii_startblk;
5871464240eSMatthew Dillon 
5881464240eSMatthew Dillon 		/*
5891464240eSMatthew Dillon 		 * We must calculate which disk component to use (ccdisk),
5901464240eSMatthew Dillon 		 * and recalculate cbn to be the superblock relative to
5911464240eSMatthew Dillon 		 * the beginning of the component.  This is typically done by
5921464240eSMatthew Dillon 		 * adding 'off' and ii->ii_startoff together.  However, 'off'
5931464240eSMatthew Dillon 		 * must typically be divided by the number of components in
5941464240eSMatthew Dillon 		 * this interleave array to be properly convert it from a
5951464240eSMatthew Dillon 		 * CCD-relative logical superblock number to a
5961464240eSMatthew Dillon 		 * component-relative superblock number.
5971464240eSMatthew Dillon 		 */
598a56bb8a5SSatoshi Asami 		if (ii->ii_ndisk == 1) {
5991464240eSMatthew Dillon 			/*
6001464240eSMatthew Dillon 			 * When we have just one disk, it can't be a mirror
6011464240eSMatthew Dillon 			 * or a parity config.
6021464240eSMatthew Dillon 			 */
603a56bb8a5SSatoshi Asami 			ccdisk = ii->ii_index[0];
604a56bb8a5SSatoshi Asami 			cbn = ii->ii_startoff + off;
605a56bb8a5SSatoshi Asami 		} else {
6063a3fcd72SPoul-Henning Kamp 			if (cs->sc_flags & CCDF_MIRROR) {
6071464240eSMatthew Dillon 				/*
6081464240eSMatthew Dillon 				 * We have forced a uniform mapping, resulting
6091464240eSMatthew Dillon 				 * in a single interleave array.  We double
6101464240eSMatthew Dillon 				 * up on the first half of the available
6111464240eSMatthew Dillon 				 * components and our mirror is in the second
6121464240eSMatthew Dillon 				 * half.  This only works with a single
6131464240eSMatthew Dillon 				 * interleave array because doubling up
6141464240eSMatthew Dillon 				 * doubles the number of sectors, so there
6151464240eSMatthew Dillon 				 * cannot be another interleave array because
6161464240eSMatthew Dillon 				 * the next interleave array's calculations
6171464240eSMatthew Dillon 				 * would be off.
6181464240eSMatthew Dillon 				 */
6191464240eSMatthew Dillon 				int ndisk2 = ii->ii_ndisk / 2;
6201464240eSMatthew Dillon 				ccdisk = ii->ii_index[off % ndisk2];
6211464240eSMatthew Dillon 				cbn = ii->ii_startoff + off / ndisk2;
6221464240eSMatthew Dillon 				ci2 = &cs->sc_cinfo[ccdisk + ndisk2];
6231464240eSMatthew Dillon 			} else {
624a56bb8a5SSatoshi Asami 				ccdisk = ii->ii_index[off % ii->ii_ndisk];
625a56bb8a5SSatoshi Asami 				cbn = ii->ii_startoff + off / ii->ii_ndisk;
626a56bb8a5SSatoshi Asami 			}
6277ecb65faSSatoshi Asami 		}
6281464240eSMatthew Dillon 
629a56bb8a5SSatoshi Asami 		ci = &cs->sc_cinfo[ccdisk];
6301464240eSMatthew Dillon 
6311464240eSMatthew Dillon 		/*
6321464240eSMatthew Dillon 		 * Convert cbn from a superblock to a normal block so it
6331464240eSMatthew Dillon 		 * can be used to calculate (along with cboff) the normal
6341464240eSMatthew Dillon 		 * block index into this particular disk.
6351464240eSMatthew Dillon 		 */
6361464240eSMatthew Dillon 		cbn *= cs->sc_ileave;
637a56bb8a5SSatoshi Asami 	}
638a56bb8a5SSatoshi Asami 
639a56bb8a5SSatoshi Asami 	/*
640a56bb8a5SSatoshi Asami 	 * Fill in the component buf structure.
641a56bb8a5SSatoshi Asami 	 */
6423a3fcd72SPoul-Henning Kamp 	cbp = g_clone_bio(bp);
643ec421aecSPoul-Henning Kamp 	if (cbp == NULL)
644ec421aecSPoul-Henning Kamp 		return (ENOMEM);
6453a3fcd72SPoul-Henning Kamp 	cbp->bio_done = g_std_done;
6463f4f4a14SMartin Cracauer 	cbp->bio_offset = dbtob(cbn + cboff + cs->sc_offset);
6473a3fcd72SPoul-Henning Kamp 	cbp->bio_data = addr;
648a56bb8a5SSatoshi Asami 	if (cs->sc_ileave == 0)
64940969e38SDavid Greenman               cbc = dbtob((off_t)(ci->ci_size - cbn));
650a56bb8a5SSatoshi Asami 	else
65140969e38SDavid Greenman               cbc = dbtob((off_t)(cs->sc_ileave - cboff));
6523a3fcd72SPoul-Henning Kamp 	cbp->bio_length = (cbc < bcount) ? cbc : bcount;
653c0b89506SJohn Dyson 
6543a3fcd72SPoul-Henning Kamp 	cbp->bio_from = ci->ci_consumer;
6553bc746beSSatoshi Asami 	cb[0] = cbp;
6561464240eSMatthew Dillon 
6573a3fcd72SPoul-Henning Kamp 	if (cs->sc_flags & CCDF_MIRROR) {
6583a3fcd72SPoul-Henning Kamp 		cbp = g_clone_bio(bp);
659ec421aecSPoul-Henning Kamp 		if (cbp == NULL)
660ec421aecSPoul-Henning Kamp 			return (ENOMEM);
6613a3fcd72SPoul-Henning Kamp 		cbp->bio_done = cb[0]->bio_done = ccdiodone;
6623a3fcd72SPoul-Henning Kamp 		cbp->bio_offset = cb[0]->bio_offset;
6633a3fcd72SPoul-Henning Kamp 		cbp->bio_data = cb[0]->bio_data;
6643a3fcd72SPoul-Henning Kamp 		cbp->bio_length = cb[0]->bio_length;
6653a3fcd72SPoul-Henning Kamp 		cbp->bio_from = ci2->ci_consumer;
6663a3fcd72SPoul-Henning Kamp 		cbp->bio_caller1 = cb[0];
6673a3fcd72SPoul-Henning Kamp 		cb[0]->bio_caller1 = cbp;
6683bc746beSSatoshi Asami 		cb[1] = cbp;
6693bc746beSSatoshi Asami 	}
6700f76d6d8SPoul-Henning Kamp 	return (0);
671a56bb8a5SSatoshi Asami }
672a56bb8a5SSatoshi Asami 
673a56bb8a5SSatoshi Asami /*
674c44c213fSPoul-Henning Kamp  * Called only for mirrored operations.
675a56bb8a5SSatoshi Asami  */
676e2738b4fSPoul-Henning Kamp static void
ccdiodone(struct bio * cbp)6773a3fcd72SPoul-Henning Kamp ccdiodone(struct bio *cbp)
678a56bb8a5SSatoshi Asami {
6793a3fcd72SPoul-Henning Kamp 	struct bio *mbp, *pbp;
680a56bb8a5SSatoshi Asami 
6813a3fcd72SPoul-Henning Kamp 	mbp = cbp->bio_caller1;
6823a3fcd72SPoul-Henning Kamp 	pbp = cbp->bio_parent;
683a56bb8a5SSatoshi Asami 
6843a3fcd72SPoul-Henning Kamp 	if (pbp->bio_cmd == BIO_READ) {
6853a3fcd72SPoul-Henning Kamp 		if (cbp->bio_error == 0) {
686c44c213fSPoul-Henning Kamp 			/* We will not be needing the partner bio */
687c44c213fSPoul-Henning Kamp 			if (mbp != NULL) {
6883a3fcd72SPoul-Henning Kamp 				pbp->bio_inbed++;
6893a3fcd72SPoul-Henning Kamp 				g_destroy_bio(mbp);
690c44c213fSPoul-Henning Kamp 			}
691c44c213fSPoul-Henning Kamp 			g_std_done(cbp);
692e7322872SSatoshi Asami 			return;
693e7322872SSatoshi Asami 		}
6943a3fcd72SPoul-Henning Kamp 		if (mbp != NULL) {
695c44c213fSPoul-Henning Kamp 			/* Try partner the bio instead */
6963a3fcd72SPoul-Henning Kamp 			mbp->bio_caller1 = NULL;
6973a3fcd72SPoul-Henning Kamp 			pbp->bio_inbed++;
6983a3fcd72SPoul-Henning Kamp 			g_destroy_bio(cbp);
6993a3fcd72SPoul-Henning Kamp 			g_io_request(mbp, mbp->bio_from);
700c44c213fSPoul-Henning Kamp 			/*
701c44c213fSPoul-Henning Kamp 			 * XXX: If this comes back OK, we should actually
702c44c213fSPoul-Henning Kamp 			 * try to write the good data on the failed mirror
703c44c213fSPoul-Henning Kamp 			 */
7041464240eSMatthew Dillon 			return;
7051464240eSMatthew Dillon 		}
7063a3fcd72SPoul-Henning Kamp 		g_std_done(cbp);
707114ebb2fSPoul-Henning Kamp 		return;
7081464240eSMatthew Dillon 	}
7093a3fcd72SPoul-Henning Kamp 	if (mbp != NULL) {
7103a3fcd72SPoul-Henning Kamp 		mbp->bio_caller1 = NULL;
7113a3fcd72SPoul-Henning Kamp 		pbp->bio_inbed++;
712c44c213fSPoul-Henning Kamp 		if (cbp->bio_error != 0 && pbp->bio_error == 0)
7133a3fcd72SPoul-Henning Kamp 			pbp->bio_error = cbp->bio_error;
714114ebb2fSPoul-Henning Kamp 		g_destroy_bio(cbp);
7153a3fcd72SPoul-Henning Kamp 		return;
7161464240eSMatthew Dillon 	}
7173a3fcd72SPoul-Henning Kamp 	g_std_done(cbp);
7181464240eSMatthew Dillon }
719e7322872SSatoshi Asami 
720a56bb8a5SSatoshi Asami static void
g_ccd_create(struct gctl_req * req,struct g_class * mp)7213a3fcd72SPoul-Henning Kamp g_ccd_create(struct gctl_req *req, struct g_class *mp)
722a56bb8a5SSatoshi Asami {
7233a3fcd72SPoul-Henning Kamp 	int *unit, *ileave, *nprovider;
7243a3fcd72SPoul-Henning Kamp 	struct g_geom *gp;
7253a3fcd72SPoul-Henning Kamp 	struct g_consumer *cp;
7263a3fcd72SPoul-Henning Kamp 	struct g_provider *pp;
7273a3fcd72SPoul-Henning Kamp 	struct ccd_s *sc;
728189337d8SPoul-Henning Kamp 	struct sbuf *sb;
7293a3fcd72SPoul-Henning Kamp 	char buf[20];
7303a3fcd72SPoul-Henning Kamp 	int i, error;
7313a3fcd72SPoul-Henning Kamp 
7323a3fcd72SPoul-Henning Kamp 	g_topology_assert();
7333a3fcd72SPoul-Henning Kamp 	unit = gctl_get_paraml(req, "unit", sizeof (*unit));
734251048a1SUlf Lilleengen 	if (unit == NULL) {
735251048a1SUlf Lilleengen 		gctl_error(req, "unit parameter not given");
736251048a1SUlf Lilleengen 		return;
737251048a1SUlf Lilleengen 	}
7383a3fcd72SPoul-Henning Kamp 	ileave = gctl_get_paraml(req, "ileave", sizeof (*ileave));
739251048a1SUlf Lilleengen 	if (ileave == NULL) {
740251048a1SUlf Lilleengen 		gctl_error(req, "ileave parameter not given");
741251048a1SUlf Lilleengen 		return;
742251048a1SUlf Lilleengen 	}
7433a3fcd72SPoul-Henning Kamp 	nprovider = gctl_get_paraml(req, "nprovider", sizeof (*nprovider));
744251048a1SUlf Lilleengen 	if (nprovider == NULL) {
745251048a1SUlf Lilleengen 		gctl_error(req, "nprovider parameter not given");
746251048a1SUlf Lilleengen 		return;
747251048a1SUlf Lilleengen 	}
7483a3fcd72SPoul-Henning Kamp 
7493a3fcd72SPoul-Henning Kamp 	/* Check for duplicate unit */
7503a3fcd72SPoul-Henning Kamp 	LIST_FOREACH(gp, &mp->geom, geom) {
7513a3fcd72SPoul-Henning Kamp 		sc = gp->softc;
75292b5e86eSPoul-Henning Kamp 		if (sc != NULL && sc->sc_unit == *unit) {
7533a3fcd72SPoul-Henning Kamp 			gctl_error(req, "Unit %d already configured", *unit);
7543a3fcd72SPoul-Henning Kamp 			return;
7553a3fcd72SPoul-Henning Kamp 		}
7563a3fcd72SPoul-Henning Kamp 	}
7573a3fcd72SPoul-Henning Kamp 
7583a3fcd72SPoul-Henning Kamp 	if (*nprovider <= 0) {
7593a3fcd72SPoul-Henning Kamp 		gctl_error(req, "Bogus nprovider argument (= %d)", *nprovider);
7603a3fcd72SPoul-Henning Kamp 		return;
7613a3fcd72SPoul-Henning Kamp 	}
7623a3fcd72SPoul-Henning Kamp 
7633a3fcd72SPoul-Henning Kamp 	/* Check all providers are valid */
7643a3fcd72SPoul-Henning Kamp 	for (i = 0; i < *nprovider; i++) {
7657201590bSXin LI 		snprintf(buf, sizeof(buf), "provider%d", i);
7663a3fcd72SPoul-Henning Kamp 		pp = gctl_get_provider(req, buf);
7673a3fcd72SPoul-Henning Kamp 		if (pp == NULL)
7683a3fcd72SPoul-Henning Kamp 			return;
7693a3fcd72SPoul-Henning Kamp 	}
7703a3fcd72SPoul-Henning Kamp 
7713a3fcd72SPoul-Henning Kamp 	gp = g_new_geomf(mp, "ccd%d", *unit);
7723a3fcd72SPoul-Henning Kamp 	sc = g_malloc(sizeof *sc, M_WAITOK | M_ZERO);
7733a3fcd72SPoul-Henning Kamp 	gp->softc = sc;
7743a3fcd72SPoul-Henning Kamp 	sc->sc_ndisks = *nprovider;
7753a3fcd72SPoul-Henning Kamp 
7763a3fcd72SPoul-Henning Kamp 	/* Allocate space for the component info. */
7773a3fcd72SPoul-Henning Kamp 	sc->sc_cinfo = g_malloc(sc->sc_ndisks * sizeof(struct ccdcinfo),
7783a3fcd72SPoul-Henning Kamp 	    M_WAITOK | M_ZERO);
7793a3fcd72SPoul-Henning Kamp 
7803a3fcd72SPoul-Henning Kamp 	/* Create consumers and attach to all providers */
7813a3fcd72SPoul-Henning Kamp 	for (i = 0; i < *nprovider; i++) {
7827201590bSXin LI 		snprintf(buf, sizeof(buf), "provider%d", i);
7833a3fcd72SPoul-Henning Kamp 		pp = gctl_get_provider(req, buf);
7843a3fcd72SPoul-Henning Kamp 		cp = g_new_consumer(gp);
7853a3fcd72SPoul-Henning Kamp 		error = g_attach(cp, pp);
7863a3fcd72SPoul-Henning Kamp 		KASSERT(error == 0, ("attach to %s failed", pp->name));
7873a3fcd72SPoul-Henning Kamp 		sc->sc_cinfo[i].ci_consumer = cp;
7883a3fcd72SPoul-Henning Kamp 		sc->sc_cinfo[i].ci_provider = pp;
7893a3fcd72SPoul-Henning Kamp 	}
7903a3fcd72SPoul-Henning Kamp 
7913a3fcd72SPoul-Henning Kamp 	sc->sc_unit = *unit;
7923a3fcd72SPoul-Henning Kamp 	sc->sc_ileave = *ileave;
7933a3fcd72SPoul-Henning Kamp 
7943f4f4a14SMartin Cracauer 	if (gctl_get_param(req, "no_offset", NULL))
7953f4f4a14SMartin Cracauer 		sc->sc_flags |= CCDF_NO_OFFSET;
7963f4f4a14SMartin Cracauer 	if (gctl_get_param(req, "linux", NULL))
7973f4f4a14SMartin Cracauer 		sc->sc_flags |= CCDF_LINUX;
7983f4f4a14SMartin Cracauer 
7993a3fcd72SPoul-Henning Kamp 	if (gctl_get_param(req, "uniform", NULL))
8003a3fcd72SPoul-Henning Kamp 		sc->sc_flags |= CCDF_UNIFORM;
8013a3fcd72SPoul-Henning Kamp 	if (gctl_get_param(req, "mirror", NULL))
8023a3fcd72SPoul-Henning Kamp 		sc->sc_flags |= CCDF_MIRROR;
8033a3fcd72SPoul-Henning Kamp 
8043a3fcd72SPoul-Henning Kamp 	if (sc->sc_ileave == 0 && (sc->sc_flags & CCDF_MIRROR)) {
8053a3fcd72SPoul-Henning Kamp 		printf("%s: disabling mirror, interleave is 0\n", gp->name);
8063a3fcd72SPoul-Henning Kamp 		sc->sc_flags &= ~(CCDF_MIRROR);
8073a3fcd72SPoul-Henning Kamp 	}
8083a3fcd72SPoul-Henning Kamp 
8093a3fcd72SPoul-Henning Kamp 	if ((sc->sc_flags & CCDF_MIRROR) && !(sc->sc_flags & CCDF_UNIFORM)) {
8103a3fcd72SPoul-Henning Kamp 		printf("%s: mirror/parity forces uniform flag\n", gp->name);
8113a3fcd72SPoul-Henning Kamp 		sc->sc_flags |= CCDF_UNIFORM;
8123a3fcd72SPoul-Henning Kamp 	}
8133a3fcd72SPoul-Henning Kamp 
8143a3fcd72SPoul-Henning Kamp 	error = ccdinit(req, sc);
8153a3fcd72SPoul-Henning Kamp 	if (error != 0) {
8163a3fcd72SPoul-Henning Kamp 		g_ccd_freesc(sc);
8173a3fcd72SPoul-Henning Kamp 		gp->softc = NULL;
8183a3fcd72SPoul-Henning Kamp 		g_wither_geom(gp, ENXIO);
8193a3fcd72SPoul-Henning Kamp 		return;
8203a3fcd72SPoul-Henning Kamp 	}
8213a3fcd72SPoul-Henning Kamp 
8223a3fcd72SPoul-Henning Kamp 	pp = g_new_providerf(gp, "%s", gp->name);
8233a3fcd72SPoul-Henning Kamp 	pp->mediasize = sc->sc_size * (off_t)sc->sc_secsize;
8243a3fcd72SPoul-Henning Kamp 	pp->sectorsize = sc->sc_secsize;
8253a3fcd72SPoul-Henning Kamp 	g_error_provider(pp, 0);
826189337d8SPoul-Henning Kamp 
8272616144eSDag-Erling Smørgrav 	sb = sbuf_new_auto();
8283a3fcd72SPoul-Henning Kamp 	sbuf_printf(sb, "ccd%d: %d components ", sc->sc_unit, *nprovider);
8293a3fcd72SPoul-Henning Kamp 	for (i = 0; i < *nprovider; i++) {
8303a3fcd72SPoul-Henning Kamp 		sbuf_printf(sb, "%s%s",
8313a3fcd72SPoul-Henning Kamp 		    i == 0 ? "(" : ", ",
8323a3fcd72SPoul-Henning Kamp 		    sc->sc_cinfo[i].ci_provider->name);
8333a3fcd72SPoul-Henning Kamp 	}
8343a3fcd72SPoul-Henning Kamp 	sbuf_printf(sb, "), %jd blocks ", (off_t)pp->mediasize / DEV_BSIZE);
8353a3fcd72SPoul-Henning Kamp 	if (sc->sc_ileave != 0)
8363a3fcd72SPoul-Henning Kamp 		sbuf_printf(sb, "interleaved at %d blocks\n",
8373a3fcd72SPoul-Henning Kamp 			sc->sc_ileave);
8383a3fcd72SPoul-Henning Kamp 	else
8393a3fcd72SPoul-Henning Kamp 		sbuf_printf(sb, "concatenated\n");
8403a3fcd72SPoul-Henning Kamp 	sbuf_finish(sb);
84141063f93SMarcel Moolenaar 	gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
8423a3fcd72SPoul-Henning Kamp 	sbuf_delete(sb);
8433a3fcd72SPoul-Henning Kamp }
8443a3fcd72SPoul-Henning Kamp 
845f6367971SPoul-Henning Kamp static int
g_ccd_destroy_geom(struct gctl_req * req,struct g_class * mp,struct g_geom * gp)846f6367971SPoul-Henning Kamp g_ccd_destroy_geom(struct gctl_req *req, struct g_class *mp, struct g_geom *gp)
8473a3fcd72SPoul-Henning Kamp {
8483a3fcd72SPoul-Henning Kamp 	struct g_provider *pp;
8493a3fcd72SPoul-Henning Kamp 	struct ccd_s *sc;
8503a3fcd72SPoul-Henning Kamp 
8513a3fcd72SPoul-Henning Kamp 	g_topology_assert();
8523a3fcd72SPoul-Henning Kamp 	sc = gp->softc;
8533a3fcd72SPoul-Henning Kamp 	pp = LIST_FIRST(&gp->provider);
854f6367971SPoul-Henning Kamp 	if (sc == NULL || pp == NULL)
855f6367971SPoul-Henning Kamp 		return (EBUSY);
8563a3fcd72SPoul-Henning Kamp 	if (pp->acr != 0 || pp->acw != 0 || pp->ace != 0) {
8573a3fcd72SPoul-Henning Kamp 		gctl_error(req, "%s is open(r%dw%de%d)", gp->name,
8583a3fcd72SPoul-Henning Kamp 		    pp->acr, pp->acw, pp->ace);
859f6367971SPoul-Henning Kamp 		return (EBUSY);
8603a3fcd72SPoul-Henning Kamp 	}
8613a3fcd72SPoul-Henning Kamp 	g_ccd_freesc(sc);
8623a3fcd72SPoul-Henning Kamp 	gp->softc = NULL;
8633a3fcd72SPoul-Henning Kamp 	g_wither_geom(gp, ENXIO);
864f6367971SPoul-Henning Kamp 	return (0);
8653a3fcd72SPoul-Henning Kamp }
8663a3fcd72SPoul-Henning Kamp 
8673a3fcd72SPoul-Henning Kamp static void
g_ccd_list(struct gctl_req * req,struct g_class * mp)8683a3fcd72SPoul-Henning Kamp g_ccd_list(struct gctl_req *req, struct g_class *mp)
8693a3fcd72SPoul-Henning Kamp {
8703a3fcd72SPoul-Henning Kamp 	struct sbuf *sb;
8713a3fcd72SPoul-Henning Kamp 	struct ccd_s *cs;
8723a3fcd72SPoul-Henning Kamp 	struct g_geom *gp;
8733a3fcd72SPoul-Henning Kamp 	int i, unit, *up;
8743a3fcd72SPoul-Henning Kamp 
875251048a1SUlf Lilleengen 	up = gctl_get_paraml(req, "unit", sizeof (*up));
876251048a1SUlf Lilleengen 	if (up == NULL) {
877251048a1SUlf Lilleengen 		gctl_error(req, "unit parameter not given");
878251048a1SUlf Lilleengen 		return;
879251048a1SUlf Lilleengen 	}
8803a3fcd72SPoul-Henning Kamp 	unit = *up;
8812616144eSDag-Erling Smørgrav 	sb = sbuf_new_auto();
8823a3fcd72SPoul-Henning Kamp 	LIST_FOREACH(gp, &mp->geom, geom) {
8833a3fcd72SPoul-Henning Kamp 		cs = gp->softc;
88492b5e86eSPoul-Henning Kamp 		if (cs == NULL || (unit >= 0 && unit != cs->sc_unit))
8850f557e0aSPoul-Henning Kamp 			continue;
886189337d8SPoul-Henning Kamp 		sbuf_printf(sb, "ccd%d\t\t%d\t%d\t",
8873a3fcd72SPoul-Henning Kamp 		    cs->sc_unit, cs->sc_ileave, cs->sc_flags & CCDF_USERMASK);
888189337d8SPoul-Henning Kamp 
8893a3fcd72SPoul-Henning Kamp 		for (i = 0; i < cs->sc_ndisks; ++i) {
8903a3fcd72SPoul-Henning Kamp 			sbuf_printf(sb, "%s/dev/%s", i == 0 ? "" : " ",
8913a3fcd72SPoul-Henning Kamp 			    cs->sc_cinfo[i].ci_provider->name);
892189337d8SPoul-Henning Kamp 		}
893189337d8SPoul-Henning Kamp 		sbuf_printf(sb, "\n");
894189337d8SPoul-Henning Kamp 	}
895189337d8SPoul-Henning Kamp 	sbuf_finish(sb);
89641063f93SMarcel Moolenaar 	gctl_set_param_err(req, "output", sbuf_data(sb), sbuf_len(sb) + 1);
8973a3fcd72SPoul-Henning Kamp 	sbuf_delete(sb);
898189337d8SPoul-Henning Kamp }
899189337d8SPoul-Henning Kamp 
900189337d8SPoul-Henning Kamp static void
g_ccd_config(struct gctl_req * req,struct g_class * mp,char const * verb)901189337d8SPoul-Henning Kamp g_ccd_config(struct gctl_req *req, struct g_class *mp, char const *verb)
902189337d8SPoul-Henning Kamp {
903f6367971SPoul-Henning Kamp 	struct g_geom *gp;
904189337d8SPoul-Henning Kamp 
905189337d8SPoul-Henning Kamp 	g_topology_assert();
906189337d8SPoul-Henning Kamp 	if (!strcmp(verb, "create geom")) {
9073a3fcd72SPoul-Henning Kamp 		g_ccd_create(req, mp);
908189337d8SPoul-Henning Kamp 	} else if (!strcmp(verb, "destroy geom")) {
909f6367971SPoul-Henning Kamp 		gp = gctl_get_geom(req, mp, "geom");
910f6367971SPoul-Henning Kamp 		if (gp != NULL)
911f6367971SPoul-Henning Kamp 			g_ccd_destroy_geom(req, mp, gp);
912189337d8SPoul-Henning Kamp 	} else if (!strcmp(verb, "list")) {
9133a3fcd72SPoul-Henning Kamp 		g_ccd_list(req, mp);
914189337d8SPoul-Henning Kamp 	} else {
915189337d8SPoul-Henning Kamp 		gctl_error(req, "unknown verb");
916189337d8SPoul-Henning Kamp 	}
917189337d8SPoul-Henning Kamp }
918189337d8SPoul-Henning Kamp 
919189337d8SPoul-Henning Kamp static struct g_class g_ccd_class = {
920189337d8SPoul-Henning Kamp 	.name = "CCD",
9215721c9c7SPoul-Henning Kamp 	.version = G_VERSION,
922189337d8SPoul-Henning Kamp 	.ctlreq = g_ccd_config,
923f6367971SPoul-Henning Kamp 	.destroy_geom = g_ccd_destroy_geom,
924650ee351SPoul-Henning Kamp 	.start = g_ccd_start,
925650ee351SPoul-Henning Kamp 	.orphan = g_ccd_orphan,
926650ee351SPoul-Henning Kamp 	.access = g_ccd_access,
927189337d8SPoul-Henning Kamp };
928189337d8SPoul-Henning Kamp 
929189337d8SPoul-Henning Kamp DECLARE_GEOM_CLASS(g_ccd_class, g_ccd);
93074d6c131SKyle Evans MODULE_VERSION(geom_ccd, 0);
931