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