Home | History | Annotate | Line # | Download | only in dev
ccd.c revision 1.135
      1 /*	$NetBSD: ccd.c,v 1.135 2010/11/14 03:49:52 uebayasi Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997, 1998, 1999, 2007, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe, and by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1990, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * This code is derived from software contributed to Berkeley by
     37  * the Systems Programming Group of the University of Utah Computer
     38  * Science Department.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  * 3. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  * from: Utah $Hdr: cd.c 1.6 90/11/28$
     65  *
     66  *	@(#)cd.c	8.2 (Berkeley) 11/16/93
     67  */
     68 
     69 /*
     70  * Copyright (c) 1988 University of Utah.
     71  *
     72  * This code is derived from software contributed to Berkeley by
     73  * the Systems Programming Group of the University of Utah Computer
     74  * Science Department.
     75  *
     76  * Redistribution and use in source and binary forms, with or without
     77  * modification, are permitted provided that the following conditions
     78  * are met:
     79  * 1. Redistributions of source code must retain the above copyright
     80  *    notice, this list of conditions and the following disclaimer.
     81  * 2. Redistributions in binary form must reproduce the above copyright
     82  *    notice, this list of conditions and the following disclaimer in the
     83  *    documentation and/or other materials provided with the distribution.
     84  * 3. All advertising materials mentioning features or use of this software
     85  *    must display the following acknowledgement:
     86  *	This product includes software developed by the University of
     87  *	California, Berkeley and its contributors.
     88  * 4. Neither the name of the University nor the names of its contributors
     89  *    may be used to endorse or promote products derived from this software
     90  *    without specific prior written permission.
     91  *
     92  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     93  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     95  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     96  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     97  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     98  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    100  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    101  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    102  * SUCH DAMAGE.
    103  *
    104  * from: Utah $Hdr: cd.c 1.6 90/11/28$
    105  *
    106  *	@(#)cd.c	8.2 (Berkeley) 11/16/93
    107  */
    108 
    109 /*
    110  * "Concatenated" disk driver.
    111  *
    112  * Notes on concurrency:
    113  *
    114  * => sc_dvlock serializes access to the device nodes, excluding block I/O.
    115  *
    116  * => sc_iolock serializes access to (sc_flags & CCDF_INITED), disk stats,
    117  *    sc_stop, sc_bufq and b_resid from master buffers.
    118  *
    119  * => a combination of CCDF_INITED, sc_inflight, and sc_iolock is used to
    120  *    serialize I/O and configuration changes.
    121  *
    122  * => the in-core disk label does not change while the device is open.
    123  *
    124  * On memory consumption: ccd fans out I/O requests and so needs to
    125  * allocate memory.  If the system is desperately low on memory, we
    126  * single thread I/O.
    127  */
    128 
    129 #include <sys/cdefs.h>
    130 __KERNEL_RCSID(0, "$NetBSD: ccd.c,v 1.135 2010/11/14 03:49:52 uebayasi Exp $");
    131 
    132 #include <sys/param.h>
    133 #include <sys/systm.h>
    134 #include <sys/kernel.h>
    135 #include <sys/proc.h>
    136 #include <sys/errno.h>
    137 #include <sys/buf.h>
    138 #include <sys/kmem.h>
    139 #include <sys/pool.h>
    140 #include <sys/namei.h>
    141 #include <sys/stat.h>
    142 #include <sys/ioctl.h>
    143 #include <sys/disklabel.h>
    144 #include <sys/device.h>
    145 #include <sys/disk.h>
    146 #include <sys/syslog.h>
    147 #include <sys/fcntl.h>
    148 #include <sys/vnode.h>
    149 #include <sys/conf.h>
    150 #include <sys/mutex.h>
    151 #include <sys/queue.h>
    152 #include <sys/kauth.h>
    153 #include <sys/kthread.h>
    154 #include <sys/bufq.h>
    155 
    156 #include <uvm/uvm_extern.h>
    157 
    158 #include <dev/ccdvar.h>
    159 #include <dev/dkvar.h>
    160 
    161 #if defined(CCDDEBUG) && !defined(DEBUG)
    162 #define DEBUG
    163 #endif
    164 
    165 #ifdef DEBUG
    166 #define CCDB_FOLLOW	0x01
    167 #define CCDB_INIT	0x02
    168 #define CCDB_IO		0x04
    169 #define CCDB_LABEL	0x08
    170 #define CCDB_VNODE	0x10
    171 int ccddebug = 0x00;
    172 #endif
    173 
    174 #define	ccdunit(x)	DISKUNIT(x)
    175 
    176 struct ccdbuf {
    177 	struct buf	cb_buf;		/* new I/O buf */
    178 	struct buf	*cb_obp;	/* ptr. to original I/O buf */
    179 	struct ccd_softc *cb_sc;	/* pointer to ccd softc */
    180 	int		cb_comp;	/* target component */
    181 	SIMPLEQ_ENTRY(ccdbuf) cb_q;	/* fifo of component buffers */
    182 };
    183 
    184 /* component buffer pool */
    185 static pool_cache_t ccd_cache;
    186 
    187 #define	CCD_GETBUF()		pool_cache_get(ccd_cache, PR_WAITOK)
    188 #define	CCD_PUTBUF(cbp)		pool_cache_put(ccd_cache, cbp)
    189 
    190 #define CCDLABELDEV(dev)	\
    191 	(MAKEDISKDEV(major((dev)), ccdunit((dev)), RAW_PART))
    192 
    193 /* called by main() at boot time */
    194 void	ccdattach(int);
    195 
    196 /* called by biodone() at interrupt time */
    197 static void	ccdiodone(struct buf *);
    198 
    199 static void	ccdinterleave(struct ccd_softc *);
    200 static int	ccdinit(struct ccd_softc *, char **, struct vnode **,
    201 		    struct lwp *);
    202 static struct ccdbuf *ccdbuffer(struct ccd_softc *, struct buf *,
    203 		    daddr_t, void *, long);
    204 static void	ccdgetdefaultlabel(struct ccd_softc *, struct disklabel *);
    205 static void	ccdgetdisklabel(dev_t);
    206 static void	ccdmakedisklabel(struct ccd_softc *);
    207 static void	ccdstart(struct ccd_softc *);
    208 static void	ccdthread(void *);
    209 
    210 static dev_type_open(ccdopen);
    211 static dev_type_close(ccdclose);
    212 static dev_type_read(ccdread);
    213 static dev_type_write(ccdwrite);
    214 static dev_type_ioctl(ccdioctl);
    215 static dev_type_strategy(ccdstrategy);
    216 static dev_type_size(ccdsize);
    217 
    218 const struct bdevsw ccd_bdevsw = {
    219 	.d_open = ccdopen,
    220 	.d_close = ccdclose,
    221 	.d_strategy = ccdstrategy,
    222 	.d_ioctl = ccdioctl,
    223 	.d_dump = nodump,
    224 	.d_psize = ccdsize,
    225 	.d_flag = D_DISK | D_MPSAFE
    226 };
    227 
    228 const struct cdevsw ccd_cdevsw = {
    229 	.d_open = ccdopen,
    230 	.d_close = ccdclose,
    231 	.d_read = ccdread,
    232 	.d_write = ccdwrite,
    233 	.d_ioctl = ccdioctl,
    234 	.d_stop = nostop,
    235 	.d_tty = notty,
    236 	.d_poll = nopoll,
    237 	.d_mmap = nommap,
    238 	.d_kqfilter = nokqfilter,
    239 	.d_flag = D_DISK | D_MPSAFE
    240 };
    241 
    242 #ifdef DEBUG
    243 static	void printiinfo(struct ccdiinfo *);
    244 #endif
    245 
    246 /* Publically visible for the benefit of libkvm and ccdconfig(8). */
    247 struct ccd_softc 	*ccd_softc;
    248 const int		ccd_softc_elemsize = sizeof(struct ccd_softc);
    249 int			numccd = 0;
    250 
    251 /*
    252  * Called by main() during pseudo-device attachment.  All we need
    253  * to do is allocate enough space for devices to be configured later.
    254  */
    255 void
    256 ccdattach(int num)
    257 {
    258 	struct ccd_softc *cs;
    259 	int i;
    260 
    261 	if (num <= 0) {
    262 #ifdef DIAGNOSTIC
    263 		panic("ccdattach: count <= 0");
    264 #endif
    265 		return;
    266 	}
    267 
    268 	ccd_softc = kmem_zalloc(num * ccd_softc_elemsize, KM_SLEEP);
    269 	if (ccd_softc == NULL) {
    270 		printf("WARNING: no memory for concatenated disks\n");
    271 		return;
    272 	}
    273 	numccd = num;
    274 
    275 	/* Initialize the component buffer pool. */
    276 	ccd_cache = pool_cache_init(sizeof(struct ccdbuf), 0,
    277 	    0, 0, "ccdbuf", NULL, IPL_BIO, NULL, NULL, NULL);
    278 
    279 	/* Initialize per-softc structures. */
    280 	for (i = 0; i < num; i++) {
    281 		cs = &ccd_softc[i];
    282 		snprintf(cs->sc_xname, sizeof(cs->sc_xname), "ccd%d", i);
    283 		mutex_init(&cs->sc_dvlock, MUTEX_DEFAULT, IPL_NONE);
    284 		cs->sc_iolock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    285 		cv_init(&cs->sc_stop, "ccdstop");
    286 		cv_init(&cs->sc_push, "ccdthr");
    287 		disk_init(&cs->sc_dkdev, cs->sc_xname, NULL); /* XXX */
    288 	}
    289 }
    290 
    291 static int
    292 ccdinit(struct ccd_softc *cs, char **cpaths, struct vnode **vpp,
    293     struct lwp *l)
    294 {
    295 	struct ccdcinfo *ci = NULL;
    296 	size_t size;
    297 	int ix;
    298 	struct vattr va;
    299 	size_t minsize;
    300 	int maxsecsize;
    301 	struct partinfo dpart;
    302 	struct ccdgeom *ccg = &cs->sc_geom;
    303 	char *tmppath;
    304 	int error, path_alloced;
    305 
    306 #ifdef DEBUG
    307 	if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    308 		printf("%s: ccdinit\n", cs->sc_xname);
    309 #endif
    310 
    311 	/* Allocate space for the component info. */
    312 	cs->sc_cinfo = kmem_alloc(cs->sc_nccdisks * sizeof(*cs->sc_cinfo),
    313 	    KM_SLEEP);
    314 	tmppath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
    315 
    316 	cs->sc_size = 0;
    317 
    318 	/*
    319 	 * Verify that each component piece exists and record
    320 	 * relevant information about it.
    321 	 */
    322 	maxsecsize = 0;
    323 	minsize = 0;
    324 	for (ix = 0, path_alloced = 0; ix < cs->sc_nccdisks; ix++) {
    325 		ci = &cs->sc_cinfo[ix];
    326 		ci->ci_vp = vpp[ix];
    327 
    328 		/*
    329 		 * Copy in the pathname of the component.
    330 		 */
    331 		memset(tmppath, 0, sizeof(tmppath));	/* sanity */
    332 		error = copyinstr(cpaths[ix], tmppath,
    333 		    MAXPATHLEN, &ci->ci_pathlen);
    334 		if (ci->ci_pathlen == 0)
    335 			error = EINVAL;
    336 		if (error) {
    337 #ifdef DEBUG
    338 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    339 				printf("%s: can't copy path, error = %d\n",
    340 				    cs->sc_xname, error);
    341 #endif
    342 			goto out;
    343 		}
    344 		ci->ci_path = kmem_alloc(ci->ci_pathlen, KM_SLEEP);
    345 		memcpy(ci->ci_path, tmppath, ci->ci_pathlen);
    346 		path_alloced++;
    347 
    348 		/*
    349 		 * XXX: Cache the component's dev_t.
    350 		 */
    351 		if ((error = VOP_GETATTR(vpp[ix], &va, l->l_cred)) != 0) {
    352 #ifdef DEBUG
    353 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    354 				printf("%s: %s: getattr failed %s = %d\n",
    355 				    cs->sc_xname, ci->ci_path,
    356 				    "error", error);
    357 #endif
    358 			goto out;
    359 		}
    360 		ci->ci_dev = va.va_rdev;
    361 
    362 		/*
    363 		 * Get partition information for the component.
    364 		 */
    365 		error = VOP_IOCTL(vpp[ix], DIOCGPART, &dpart,
    366 		    FREAD, l->l_cred);
    367 		if (error) {
    368 #ifdef DEBUG
    369 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    370 				 printf("%s: %s: ioctl failed, error = %d\n",
    371 				     cs->sc_xname, ci->ci_path, error);
    372 #endif
    373 			goto out;
    374 		}
    375 
    376 /*
    377  * This diagnostic test is disabled (for now?) since not all port supports
    378  * on-disk BSD disklabel.
    379  */
    380 #if 0 /* def DIAGNOSTIC */
    381 		/* Check fstype field of component. */
    382 		if (dpart.part->p_fstype != FS_CCD)
    383 			printf("%s: WARNING: %s: fstype %d != FS_CCD\n",
    384 			    cs->sc_xname, ci->ci_path, dpart.part->p_fstype);
    385 #endif
    386 
    387 		/*
    388 		 * Calculate the size, truncating to an interleave
    389 		 * boundary if necessary.
    390 		 */
    391 		maxsecsize =
    392 		    ((dpart.disklab->d_secsize > maxsecsize) ?
    393 		    dpart.disklab->d_secsize : maxsecsize);
    394 		size = dpart.part->p_size;
    395 		if (cs->sc_ileave > 1)
    396 			size -= size % cs->sc_ileave;
    397 
    398 		if (size == 0) {
    399 #ifdef DEBUG
    400 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    401 				printf("%s: %s: size == 0\n",
    402 				    cs->sc_xname, ci->ci_path);
    403 #endif
    404 			error = ENODEV;
    405 			goto out;
    406 		}
    407 
    408 		if (minsize == 0 || size < minsize)
    409 			minsize = size;
    410 		ci->ci_size = size;
    411 		cs->sc_size += size;
    412 	}
    413 
    414 	/*
    415 	 * Don't allow the interleave to be smaller than
    416 	 * the biggest component sector.
    417 	 */
    418 	if ((cs->sc_ileave > 0) &&
    419 	    (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) {
    420 #ifdef DEBUG
    421 		if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    422 			printf("%s: interleave must be at least %d\n",
    423 			    cs->sc_xname, (maxsecsize / DEV_BSIZE));
    424 #endif
    425 		error = EINVAL;
    426 		goto out;
    427 	}
    428 
    429 	/*
    430 	 * If uniform interleave is desired set all sizes to that of
    431 	 * the smallest component.
    432 	 */
    433 	if (cs->sc_flags & CCDF_UNIFORM) {
    434 		for (ci = cs->sc_cinfo;
    435 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    436 			ci->ci_size = minsize;
    437 
    438 		cs->sc_size = cs->sc_nccdisks * minsize;
    439 	}
    440 
    441 	/*
    442 	 * Construct the interleave table.
    443 	 */
    444 	ccdinterleave(cs);
    445 
    446 	/*
    447 	 * Create pseudo-geometry based on 1MB cylinders.  It's
    448 	 * pretty close.
    449 	 */
    450 	ccg->ccg_secsize = DEV_BSIZE;
    451 	ccg->ccg_ntracks = 1;
    452 	ccg->ccg_nsectors = 1024 * (1024 / ccg->ccg_secsize);
    453 	ccg->ccg_ncylinders = cs->sc_size / ccg->ccg_nsectors;
    454 
    455 	/*
    456 	 * Create thread to handle deferred I/O.
    457 	 */
    458 	cs->sc_zap = false;
    459 	error = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL, ccdthread,
    460 	    cs, &cs->sc_thread, "%s", cs->sc_xname);
    461 	if (error) {
    462 		printf("ccdinit: can't create thread: %d\n", error);
    463 		goto out;
    464 	}
    465 
    466 	/*
    467 	 * Only now that everything is set up can we enable the device.
    468 	 */
    469 	mutex_enter(cs->sc_iolock);
    470 	cs->sc_flags |= CCDF_INITED;
    471 	mutex_exit(cs->sc_iolock);
    472 	kmem_free(tmppath, MAXPATHLEN);
    473 	return (0);
    474 
    475  out:
    476 	for (ix = 0; ix < path_alloced; ix++) {
    477 		kmem_free(cs->sc_cinfo[ix].ci_path,
    478 		    cs->sc_cinfo[ix].ci_pathlen);
    479 	}
    480 	kmem_free(cs->sc_cinfo, cs->sc_nccdisks * sizeof(struct ccdcinfo));
    481 	kmem_free(tmppath, MAXPATHLEN);
    482 	return (error);
    483 }
    484 
    485 static void
    486 ccdinterleave(struct ccd_softc *cs)
    487 {
    488 	struct ccdcinfo *ci, *smallci;
    489 	struct ccdiinfo *ii;
    490 	daddr_t bn, lbn;
    491 	int ix;
    492 	u_long size;
    493 
    494 #ifdef DEBUG
    495 	if (ccddebug & CCDB_INIT)
    496 		printf("ccdinterleave(%p): ileave %d\n", cs, cs->sc_ileave);
    497 #endif
    498 	/*
    499 	 * Allocate an interleave table.
    500 	 * Chances are this is too big, but we don't care.
    501 	 */
    502 	size = (cs->sc_nccdisks + 1) * sizeof(struct ccdiinfo);
    503 	cs->sc_itable = kmem_zalloc(size, KM_SLEEP);
    504 
    505 	/*
    506 	 * Trivial case: no interleave (actually interleave of disk size).
    507 	 * Each table entry represents a single component in its entirety.
    508 	 */
    509 	if (cs->sc_ileave == 0) {
    510 		bn = 0;
    511 		ii = cs->sc_itable;
    512 
    513 		for (ix = 0; ix < cs->sc_nccdisks; ix++) {
    514 			/* Allocate space for ii_index. */
    515 			ii->ii_indexsz = sizeof(int);
    516 			ii->ii_index = kmem_alloc(ii->ii_indexsz, KM_SLEEP);
    517 			ii->ii_ndisk = 1;
    518 			ii->ii_startblk = bn;
    519 			ii->ii_startoff = 0;
    520 			ii->ii_index[0] = ix;
    521 			bn += cs->sc_cinfo[ix].ci_size;
    522 			ii++;
    523 		}
    524 		ii->ii_ndisk = 0;
    525 #ifdef DEBUG
    526 		if (ccddebug & CCDB_INIT)
    527 			printiinfo(cs->sc_itable);
    528 #endif
    529 		return;
    530 	}
    531 
    532 	/*
    533 	 * The following isn't fast or pretty; it doesn't have to be.
    534 	 */
    535 	size = 0;
    536 	bn = lbn = 0;
    537 	for (ii = cs->sc_itable; ; ii++) {
    538 		/* Allocate space for ii_index. */
    539 		ii->ii_indexsz = sizeof(int) * cs->sc_nccdisks;
    540 		ii->ii_index = kmem_alloc(ii->ii_indexsz, KM_SLEEP);
    541 
    542 		/*
    543 		 * Locate the smallest of the remaining components
    544 		 */
    545 		smallci = NULL;
    546 		for (ci = cs->sc_cinfo;
    547 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    548 			if (ci->ci_size > size &&
    549 			    (smallci == NULL ||
    550 			     ci->ci_size < smallci->ci_size))
    551 				smallci = ci;
    552 
    553 		/*
    554 		 * Nobody left, all done
    555 		 */
    556 		if (smallci == NULL) {
    557 			ii->ii_ndisk = 0;
    558 			break;
    559 		}
    560 
    561 		/*
    562 		 * Record starting logical block and component offset
    563 		 */
    564 		ii->ii_startblk = bn / cs->sc_ileave;
    565 		ii->ii_startoff = lbn;
    566 
    567 		/*
    568 		 * Determine how many disks take part in this interleave
    569 		 * and record their indices.
    570 		 */
    571 		ix = 0;
    572 		for (ci = cs->sc_cinfo;
    573 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    574 			if (ci->ci_size >= smallci->ci_size)
    575 				ii->ii_index[ix++] = ci - cs->sc_cinfo;
    576 		ii->ii_ndisk = ix;
    577 		bn += ix * (smallci->ci_size - size);
    578 		lbn = smallci->ci_size / cs->sc_ileave;
    579 		size = smallci->ci_size;
    580 	}
    581 #ifdef DEBUG
    582 	if (ccddebug & CCDB_INIT)
    583 		printiinfo(cs->sc_itable);
    584 #endif
    585 }
    586 
    587 /* ARGSUSED */
    588 static int
    589 ccdopen(dev_t dev, int flags, int fmt, struct lwp *l)
    590 {
    591 	int unit = ccdunit(dev);
    592 	struct ccd_softc *cs;
    593 	struct disklabel *lp;
    594 	int error = 0, part, pmask;
    595 
    596 #ifdef DEBUG
    597 	if (ccddebug & CCDB_FOLLOW)
    598 		printf("ccdopen(0x%"PRIx64", 0x%x)\n", dev, flags);
    599 #endif
    600 	if (unit >= numccd)
    601 		return (ENXIO);
    602 	cs = &ccd_softc[unit];
    603 
    604 	mutex_enter(&cs->sc_dvlock);
    605 
    606 	lp = cs->sc_dkdev.dk_label;
    607 
    608 	part = DISKPART(dev);
    609 	pmask = (1 << part);
    610 
    611 	/*
    612 	 * If we're initialized, check to see if there are any other
    613 	 * open partitions.  If not, then it's safe to update
    614 	 * the in-core disklabel.  Only read the disklabel if it is
    615 	 * not already valid.
    616 	 */
    617 	if ((cs->sc_flags & (CCDF_INITED|CCDF_VLABEL)) == CCDF_INITED &&
    618 	    cs->sc_dkdev.dk_openmask == 0)
    619 		ccdgetdisklabel(dev);
    620 
    621 	/* Check that the partition exists. */
    622 	if (part != RAW_PART) {
    623 		if (((cs->sc_flags & CCDF_INITED) == 0) ||
    624 		    ((part >= lp->d_npartitions) ||
    625 		     (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
    626 			error = ENXIO;
    627 			goto done;
    628 		}
    629 	}
    630 
    631 	/* Prevent our unit from being unconfigured while open. */
    632 	switch (fmt) {
    633 	case S_IFCHR:
    634 		cs->sc_dkdev.dk_copenmask |= pmask;
    635 		break;
    636 
    637 	case S_IFBLK:
    638 		cs->sc_dkdev.dk_bopenmask |= pmask;
    639 		break;
    640 	}
    641 	cs->sc_dkdev.dk_openmask =
    642 	    cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
    643 
    644  done:
    645 	mutex_exit(&cs->sc_dvlock);
    646 	return (error);
    647 }
    648 
    649 /* ARGSUSED */
    650 static int
    651 ccdclose(dev_t dev, int flags, int fmt, struct lwp *l)
    652 {
    653 	int unit = ccdunit(dev);
    654 	struct ccd_softc *cs;
    655 	int part;
    656 
    657 #ifdef DEBUG
    658 	if (ccddebug & CCDB_FOLLOW)
    659 		printf("ccdclose(0x%"PRIx64", 0x%x)\n", dev, flags);
    660 #endif
    661 
    662 	if (unit >= numccd)
    663 		return (ENXIO);
    664 	cs = &ccd_softc[unit];
    665 
    666 	mutex_enter(&cs->sc_dvlock);
    667 
    668 	part = DISKPART(dev);
    669 
    670 	/* ...that much closer to allowing unconfiguration... */
    671 	switch (fmt) {
    672 	case S_IFCHR:
    673 		cs->sc_dkdev.dk_copenmask &= ~(1 << part);
    674 		break;
    675 
    676 	case S_IFBLK:
    677 		cs->sc_dkdev.dk_bopenmask &= ~(1 << part);
    678 		break;
    679 	}
    680 	cs->sc_dkdev.dk_openmask =
    681 	    cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
    682 
    683 	if (cs->sc_dkdev.dk_openmask == 0) {
    684 		if ((cs->sc_flags & CCDF_KLABEL) == 0)
    685 			cs->sc_flags &= ~CCDF_VLABEL;
    686 	}
    687 
    688 	mutex_exit(&cs->sc_dvlock);
    689 	return (0);
    690 }
    691 
    692 static bool
    693 ccdbackoff(struct ccd_softc *cs)
    694 {
    695 
    696 	/* XXX Arbitrary, should be a uvm call. */
    697 	return uvmexp.free < (uvmexp.freemin >> 1) &&
    698 	    disk_isbusy(&cs->sc_dkdev);
    699 }
    700 
    701 static void
    702 ccdthread(void *cookie)
    703 {
    704 	struct ccd_softc *cs;
    705 
    706 	cs = cookie;
    707 
    708 #ifdef DEBUG
    709  	if (ccddebug & CCDB_FOLLOW)
    710  		printf("ccdthread: hello\n");
    711 #endif
    712 
    713 	mutex_enter(cs->sc_iolock);
    714 	while (__predict_true(!cs->sc_zap)) {
    715 		if (bufq_peek(cs->sc_bufq) == NULL) {
    716 			/* Nothing to do. */
    717 			cv_wait(&cs->sc_push, cs->sc_iolock);
    718 			continue;
    719 		}
    720 		if (ccdbackoff(cs)) {
    721 			/* Wait for memory to become available. */
    722 			(void)cv_timedwait(&cs->sc_push, cs->sc_iolock, 1);
    723 			continue;
    724 		}
    725 #ifdef DEBUG
    726  		if (ccddebug & CCDB_FOLLOW)
    727  			printf("ccdthread: dispatching I/O\n");
    728 #endif
    729 		ccdstart(cs);
    730 		mutex_enter(cs->sc_iolock);
    731 	}
    732 	cs->sc_thread = NULL;
    733 	mutex_exit(cs->sc_iolock);
    734 #ifdef DEBUG
    735  	if (ccddebug & CCDB_FOLLOW)
    736  		printf("ccdthread: goodbye\n");
    737 #endif
    738 	kthread_exit(0);
    739 }
    740 
    741 static void
    742 ccdstrategy(struct buf *bp)
    743 {
    744 	int unit = ccdunit(bp->b_dev);
    745 	struct ccd_softc *cs = &ccd_softc[unit];
    746 
    747 	/* Must be open or reading label. */
    748 	KASSERT(cs->sc_dkdev.dk_openmask != 0 ||
    749 	    (cs->sc_flags & CCDF_RLABEL) != 0);
    750 
    751 	mutex_enter(cs->sc_iolock);
    752 	/* Synchronize with device init/uninit. */
    753 	if (__predict_false((cs->sc_flags & CCDF_INITED) == 0)) {
    754 		mutex_exit(cs->sc_iolock);
    755 #ifdef DEBUG
    756  		if (ccddebug & CCDB_FOLLOW)
    757  			printf("ccdstrategy: unit %d: not inited\n", unit);
    758 #endif
    759  		bp->b_error = ENXIO;
    760  		bp->b_resid = bp->b_bcount;
    761  		biodone(bp);
    762 		return;
    763 	}
    764 
    765 	/* Defer to thread if system is low on memory. */
    766 	bufq_put(cs->sc_bufq, bp);
    767 	if (__predict_false(ccdbackoff(cs))) {
    768 		mutex_exit(cs->sc_iolock);
    769 #ifdef DEBUG
    770  		if (ccddebug & CCDB_FOLLOW)
    771  			printf("ccdstrategy: holding off on I/O\n");
    772 #endif
    773 		return;
    774 	}
    775 	ccdstart(cs);
    776 }
    777 
    778 static void
    779 ccdstart(struct ccd_softc *cs)
    780 {
    781 	daddr_t blkno;
    782 	int wlabel;
    783 	struct disklabel *lp;
    784 	long bcount, rcount;
    785 	struct ccdbuf *cbp;
    786 	char *addr;
    787 	daddr_t bn;
    788 	vnode_t *vp;
    789 	buf_t *bp;
    790 
    791 	KASSERT(mutex_owned(cs->sc_iolock));
    792 
    793 	disk_busy(&cs->sc_dkdev);
    794 	bp = bufq_get(cs->sc_bufq);
    795 	KASSERT(bp != NULL);
    796 
    797 #ifdef DEBUG
    798 	if (ccddebug & CCDB_FOLLOW)
    799 		printf("ccdstart(%s, %p)\n", cs->sc_xname, bp);
    800 #endif
    801 
    802 	/* If it's a nil transfer, wake up the top half now. */
    803 	if (bp->b_bcount == 0)
    804 		goto done;
    805 
    806 	lp = cs->sc_dkdev.dk_label;
    807 
    808 	/*
    809 	 * Do bounds checking and adjust transfer.  If there's an
    810 	 * error, the bounds check will flag that for us.  Convert
    811 	 * the partition relative block number to an absolute.
    812 	 */
    813 	blkno = bp->b_blkno;
    814 	wlabel = cs->sc_flags & (CCDF_WLABEL|CCDF_LABELLING);
    815 	if (DISKPART(bp->b_dev) != RAW_PART) {
    816 		if (bounds_check_with_label(&cs->sc_dkdev, bp, wlabel) <= 0)
    817 			goto done;
    818 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
    819 	}
    820 	mutex_exit(cs->sc_iolock);
    821 	bp->b_rawblkno = blkno;
    822 
    823 	/* Allocate the component buffers and start I/O! */
    824 	bp->b_resid = bp->b_bcount;
    825 	bn = bp->b_rawblkno;
    826 	addr = bp->b_data;
    827 	for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
    828 		cbp = ccdbuffer(cs, bp, bn, addr, bcount);
    829 		rcount = cbp->cb_buf.b_bcount;
    830 		bn += btodb(rcount);
    831 		addr += rcount;
    832 		vp = cbp->cb_buf.b_vp;
    833 		if ((cbp->cb_buf.b_flags & B_READ) == 0) {
    834 			mutex_enter(&vp->v_interlock);
    835 			vp->v_numoutput++;
    836 			mutex_exit(&vp->v_interlock);
    837 		}
    838 		(void)VOP_STRATEGY(vp, &cbp->cb_buf);
    839 	}
    840 	return;
    841 
    842  done:
    843 	disk_unbusy(&cs->sc_dkdev, 0, 0);
    844 	cv_broadcast(&cs->sc_stop);
    845 	cv_broadcast(&cs->sc_push);
    846 	mutex_exit(cs->sc_iolock);
    847 	bp->b_resid = bp->b_bcount;
    848 	biodone(bp);
    849 }
    850 
    851 /*
    852  * Build a component buffer header.
    853  */
    854 static struct ccdbuf *
    855 ccdbuffer(struct ccd_softc *cs, struct buf *bp, daddr_t bn, void *addr,
    856     long bcount)
    857 {
    858 	struct ccdcinfo *ci;
    859 	struct ccdbuf *cbp;
    860 	daddr_t cbn, cboff;
    861 	u_int64_t cbc;
    862 	int ccdisk;
    863 
    864 #ifdef DEBUG
    865 	if (ccddebug & CCDB_IO)
    866 		printf("ccdbuffer(%p, %p, %" PRId64 ", %p, %ld)\n",
    867 		       cs, bp, bn, addr, bcount);
    868 #endif
    869 	/*
    870 	 * Determine which component bn falls in.
    871 	 */
    872 	cbn = bn;
    873 	cboff = 0;
    874 
    875 	/*
    876 	 * Serially concatenated
    877 	 */
    878 	if (cs->sc_ileave == 0) {
    879 		daddr_t sblk;
    880 
    881 		sblk = 0;
    882 		for (ccdisk = 0, ci = &cs->sc_cinfo[ccdisk];
    883 		    cbn >= sblk + ci->ci_size;
    884 		    ccdisk++, ci = &cs->sc_cinfo[ccdisk])
    885 			sblk += ci->ci_size;
    886 		cbn -= sblk;
    887 	}
    888 	/*
    889 	 * Interleaved
    890 	 */
    891 	else {
    892 		struct ccdiinfo *ii;
    893 		int off;
    894 
    895 		cboff = cbn % cs->sc_ileave;
    896 		cbn /= cs->sc_ileave;
    897 		for (ii = cs->sc_itable; ii->ii_ndisk; ii++)
    898 			if (ii->ii_startblk > cbn)
    899 				break;
    900 		ii--;
    901 		off = cbn - ii->ii_startblk;
    902 		if (ii->ii_ndisk == 1) {
    903 			ccdisk = ii->ii_index[0];
    904 			cbn = ii->ii_startoff + off;
    905 		} else {
    906 			ccdisk = ii->ii_index[off % ii->ii_ndisk];
    907 			cbn = ii->ii_startoff + off / ii->ii_ndisk;
    908 		}
    909 		cbn *= cs->sc_ileave;
    910 		ci = &cs->sc_cinfo[ccdisk];
    911 	}
    912 
    913 	/*
    914 	 * Fill in the component buf structure.
    915 	 */
    916 	cbp = CCD_GETBUF();
    917 	KASSERT(cbp != NULL);
    918 	buf_init(&cbp->cb_buf);
    919 	cbp->cb_buf.b_flags = bp->b_flags;
    920 	cbp->cb_buf.b_oflags = bp->b_oflags;
    921 	cbp->cb_buf.b_cflags = bp->b_cflags;
    922 	cbp->cb_buf.b_iodone = ccdiodone;
    923 	cbp->cb_buf.b_proc = bp->b_proc;
    924 	cbp->cb_buf.b_dev = ci->ci_dev;
    925 	cbp->cb_buf.b_blkno = cbn + cboff;
    926 	cbp->cb_buf.b_data = addr;
    927 	cbp->cb_buf.b_vp = ci->ci_vp;
    928 	cbp->cb_buf.b_objlock = &ci->ci_vp->v_interlock;
    929 	if (cs->sc_ileave == 0)
    930 		cbc = dbtob((u_int64_t)(ci->ci_size - cbn));
    931 	else
    932 		cbc = dbtob((u_int64_t)(cs->sc_ileave - cboff));
    933 	cbp->cb_buf.b_bcount = cbc < bcount ? cbc : bcount;
    934 
    935 	/*
    936 	 * context for ccdiodone
    937 	 */
    938 	cbp->cb_obp = bp;
    939 	cbp->cb_sc = cs;
    940 	cbp->cb_comp = ccdisk;
    941 
    942 	BIO_COPYPRIO(&cbp->cb_buf, bp);
    943 
    944 #ifdef DEBUG
    945 	if (ccddebug & CCDB_IO)
    946 		printf(" dev 0x%"PRIx64"(u%lu): cbp %p bn %" PRId64 " addr %p"
    947 		       " bcnt %d\n",
    948 		    ci->ci_dev, (unsigned long) (ci-cs->sc_cinfo), cbp,
    949 		    cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
    950 		    cbp->cb_buf.b_bcount);
    951 #endif
    952 
    953 	return (cbp);
    954 }
    955 
    956 /*
    957  * Called at interrupt time.
    958  * Mark the component as done and if all components are done,
    959  * take a ccd interrupt.
    960  */
    961 static void
    962 ccdiodone(struct buf *vbp)
    963 {
    964 	struct ccdbuf *cbp = (struct ccdbuf *) vbp;
    965 	struct buf *bp = cbp->cb_obp;
    966 	struct ccd_softc *cs = cbp->cb_sc;
    967 	int count;
    968 
    969 #ifdef DEBUG
    970 	if (ccddebug & CCDB_FOLLOW)
    971 		printf("ccdiodone(%p)\n", cbp);
    972 	if (ccddebug & CCDB_IO) {
    973 		printf("ccdiodone: bp %p bcount %d resid %d\n",
    974 		       bp, bp->b_bcount, bp->b_resid);
    975 		printf(" dev 0x%"PRIx64"(u%d), cbp %p bn %" PRId64 " addr %p"
    976 		       " bcnt %d\n",
    977 		       cbp->cb_buf.b_dev, cbp->cb_comp, cbp,
    978 		       cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
    979 		       cbp->cb_buf.b_bcount);
    980 	}
    981 #endif
    982 
    983 	if (cbp->cb_buf.b_error != 0) {
    984 		bp->b_error = cbp->cb_buf.b_error;
    985 		printf("%s: error %d on component %d\n",
    986 		       cs->sc_xname, bp->b_error, cbp->cb_comp);
    987 	}
    988 	count = cbp->cb_buf.b_bcount;
    989 	buf_destroy(&cbp->cb_buf);
    990 	CCD_PUTBUF(cbp);
    991 
    992 	/*
    993 	 * If all done, "interrupt".
    994 	 */
    995 	mutex_enter(cs->sc_iolock);
    996 	bp->b_resid -= count;
    997 	if (bp->b_resid < 0)
    998 		panic("ccdiodone: count");
    999 	if (bp->b_resid == 0) {
   1000 		/*
   1001 		 * Request is done for better or worse, wakeup the top half.
   1002 		 */
   1003 		if (bp->b_error != 0)
   1004 			bp->b_resid = bp->b_bcount;
   1005 		disk_unbusy(&cs->sc_dkdev, (bp->b_bcount - bp->b_resid),
   1006 		    (bp->b_flags & B_READ));
   1007 		if (!disk_isbusy(&cs->sc_dkdev)) {
   1008 			if (bufq_peek(cs->sc_bufq) != NULL) {
   1009 				cv_broadcast(&cs->sc_push);
   1010 			}
   1011 			cv_broadcast(&cs->sc_stop);
   1012 		}
   1013 		mutex_exit(cs->sc_iolock);
   1014 		biodone(bp);
   1015 	} else
   1016 		mutex_exit(cs->sc_iolock);
   1017 }
   1018 
   1019 /* ARGSUSED */
   1020 static int
   1021 ccdread(dev_t dev, struct uio *uio, int flags)
   1022 {
   1023 	int unit = ccdunit(dev);
   1024 	struct ccd_softc *cs;
   1025 
   1026 #ifdef DEBUG
   1027 	if (ccddebug & CCDB_FOLLOW)
   1028 		printf("ccdread(0x%"PRIx64", %p)\n", dev, uio);
   1029 #endif
   1030 	if (unit >= numccd)
   1031 		return (ENXIO);
   1032 	cs = &ccd_softc[unit];
   1033 
   1034 	/* Unlocked advisory check, ccdstrategy check is synchronous. */
   1035 	if ((cs->sc_flags & CCDF_INITED) == 0)
   1036 		return (ENXIO);
   1037 
   1038 	return (physio(ccdstrategy, NULL, dev, B_READ, minphys, uio));
   1039 }
   1040 
   1041 /* ARGSUSED */
   1042 static int
   1043 ccdwrite(dev_t dev, struct uio *uio, int flags)
   1044 {
   1045 	int unit = ccdunit(dev);
   1046 	struct ccd_softc *cs;
   1047 
   1048 #ifdef DEBUG
   1049 	if (ccddebug & CCDB_FOLLOW)
   1050 		printf("ccdwrite(0x%"PRIx64", %p)\n", dev, uio);
   1051 #endif
   1052 	if (unit >= numccd)
   1053 		return (ENXIO);
   1054 	cs = &ccd_softc[unit];
   1055 
   1056 	/* Unlocked advisory check, ccdstrategy check is synchronous. */
   1057 	if ((cs->sc_flags & CCDF_INITED) == 0)
   1058 		return (ENXIO);
   1059 
   1060 	return (physio(ccdstrategy, NULL, dev, B_WRITE, minphys, uio));
   1061 }
   1062 
   1063 static int
   1064 ccdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1065 {
   1066 	int unit = ccdunit(dev);
   1067 	int i, j, lookedup = 0, error = 0;
   1068 	int part, pmask;
   1069 	struct ccd_softc *cs;
   1070 	struct ccd_ioctl *ccio = (struct ccd_ioctl *)data;
   1071 	kauth_cred_t uc;
   1072 	char **cpp;
   1073 	struct vnode **vpp;
   1074 #ifdef __HAVE_OLD_DISKLABEL
   1075 	struct disklabel newlabel;
   1076 #endif
   1077 
   1078 	if (unit >= numccd)
   1079 		return (ENXIO);
   1080 	cs = &ccd_softc[unit];
   1081 	uc = kauth_cred_get();
   1082 
   1083 	/* Must be open for writes for these commands... */
   1084 	switch (cmd) {
   1085 	case CCDIOCSET:
   1086 	case CCDIOCCLR:
   1087 	case DIOCSDINFO:
   1088 	case DIOCWDINFO:
   1089 #ifdef __HAVE_OLD_DISKLABEL
   1090 	case ODIOCSDINFO:
   1091 	case ODIOCWDINFO:
   1092 #endif
   1093 	case DIOCKLABEL:
   1094 	case DIOCWLABEL:
   1095 		if ((flag & FWRITE) == 0)
   1096 			return (EBADF);
   1097 	}
   1098 
   1099 	mutex_enter(&cs->sc_dvlock);
   1100 
   1101 	/* Must be initialized for these... */
   1102 	switch (cmd) {
   1103 	case CCDIOCCLR:
   1104 	case DIOCGDINFO:
   1105 	case DIOCCACHESYNC:
   1106 	case DIOCSDINFO:
   1107 	case DIOCWDINFO:
   1108 	case DIOCGPART:
   1109 	case DIOCWLABEL:
   1110 	case DIOCKLABEL:
   1111 	case DIOCGDEFLABEL:
   1112 #ifdef __HAVE_OLD_DISKLABEL
   1113 	case ODIOCGDINFO:
   1114 	case ODIOCSDINFO:
   1115 	case ODIOCWDINFO:
   1116 	case ODIOCGDEFLABEL:
   1117 #endif
   1118 		if ((cs->sc_flags & CCDF_INITED) == 0) {
   1119 			error = ENXIO;
   1120 			goto out;
   1121 		}
   1122 	}
   1123 
   1124 	switch (cmd) {
   1125 	case CCDIOCSET:
   1126 		if (cs->sc_flags & CCDF_INITED) {
   1127 			error = EBUSY;
   1128 			goto out;
   1129 		}
   1130 
   1131 		/* Validate the flags. */
   1132 		if ((ccio->ccio_flags & CCDF_USERMASK) != ccio->ccio_flags) {
   1133 			error = EINVAL;
   1134 			goto out;
   1135 		}
   1136 
   1137 		if (ccio->ccio_ndisks > CCD_MAXNDISKS ||
   1138 		    ccio->ccio_ndisks == 0) {
   1139 			error = EINVAL;
   1140 			goto out;
   1141 		}
   1142 
   1143 		/* Fill in some important bits. */
   1144 		cs->sc_ileave = ccio->ccio_ileave;
   1145 		cs->sc_nccdisks = ccio->ccio_ndisks;
   1146 		cs->sc_flags = ccio->ccio_flags & CCDF_USERMASK;
   1147 
   1148 		/*
   1149 		 * Allocate space for and copy in the array of
   1150 		 * componet pathnames and device numbers.
   1151 		 */
   1152 		cpp = kmem_alloc(ccio->ccio_ndisks * sizeof(*cpp), KM_SLEEP);
   1153 		vpp = kmem_alloc(ccio->ccio_ndisks * sizeof(*vpp), KM_SLEEP);
   1154 		error = copyin(ccio->ccio_disks, cpp,
   1155 		    ccio->ccio_ndisks * sizeof(*cpp));
   1156 		if (error) {
   1157 			kmem_free(vpp, ccio->ccio_ndisks * sizeof(*vpp));
   1158 			kmem_free(cpp, ccio->ccio_ndisks * sizeof(*cpp));
   1159 			goto out;
   1160 		}
   1161 
   1162 #ifdef DEBUG
   1163 		if (ccddebug & CCDB_INIT)
   1164 			for (i = 0; i < ccio->ccio_ndisks; ++i)
   1165 				printf("ccdioctl: component %d: %p\n",
   1166 				    i, cpp[i]);
   1167 #endif
   1168 
   1169 		for (i = 0; i < ccio->ccio_ndisks; ++i) {
   1170 #ifdef DEBUG
   1171 			if (ccddebug & CCDB_INIT)
   1172 				printf("ccdioctl: lookedup = %d\n", lookedup);
   1173 #endif
   1174 			if ((error = dk_lookup(cpp[i], l, &vpp[i],
   1175 			    UIO_USERSPACE)) != 0) {
   1176 				for (j = 0; j < lookedup; ++j)
   1177 					(void)vn_close(vpp[j], FREAD|FWRITE,
   1178 					    uc);
   1179 				kmem_free(vpp, ccio->ccio_ndisks *
   1180 				    sizeof(*vpp));
   1181 				kmem_free(cpp, ccio->ccio_ndisks *
   1182 				    sizeof(*cpp));
   1183 				goto out;
   1184 			}
   1185 			++lookedup;
   1186 		}
   1187 
   1188 		/* Attach the disk. */
   1189 		disk_attach(&cs->sc_dkdev);
   1190 		bufq_alloc(&cs->sc_bufq, "fcfs", 0);
   1191 
   1192 		/*
   1193 		 * Initialize the ccd.  Fills in the softc for us.
   1194 		 */
   1195 		if ((error = ccdinit(cs, cpp, vpp, l)) != 0) {
   1196 			for (j = 0; j < lookedup; ++j)
   1197 				(void)vn_close(vpp[j], FREAD|FWRITE,
   1198 				    uc);
   1199 			kmem_free(vpp, ccio->ccio_ndisks * sizeof(*vpp));
   1200 			kmem_free(cpp, ccio->ccio_ndisks * sizeof(*cpp));
   1201 			disk_detach(&cs->sc_dkdev);
   1202 			bufq_free(cs->sc_bufq);
   1203 			goto out;
   1204 		}
   1205 
   1206 		/* We can free the temporary variables now. */
   1207 		kmem_free(vpp, ccio->ccio_ndisks * sizeof(*vpp));
   1208 		kmem_free(cpp, ccio->ccio_ndisks * sizeof(*cpp));
   1209 
   1210 		/*
   1211 		 * The ccd has been successfully initialized, so
   1212 		 * we can place it into the array.  Don't try to
   1213 		 * read the disklabel until the disk has been attached,
   1214 		 * because space for the disklabel is allocated
   1215 		 * in disk_attach();
   1216 		 */
   1217 		ccio->ccio_unit = unit;
   1218 		ccio->ccio_size = cs->sc_size;
   1219 
   1220 		/* Try and read the disklabel. */
   1221 		ccdgetdisklabel(dev);
   1222 		break;
   1223 
   1224 	case CCDIOCCLR:
   1225 		/*
   1226 		 * Don't unconfigure if any other partitions are open
   1227 		 * or if both the character and block flavors of this
   1228 		 * partition are open.
   1229 		 */
   1230 		part = DISKPART(dev);
   1231 		pmask = (1 << part);
   1232 		if ((cs->sc_dkdev.dk_openmask & ~pmask) ||
   1233 		    ((cs->sc_dkdev.dk_bopenmask & pmask) &&
   1234 		    (cs->sc_dkdev.dk_copenmask & pmask))) {
   1235 			error = EBUSY;
   1236 			goto out;
   1237 		}
   1238 
   1239 		/* Stop new I/O, wait for in-flight I/O to complete. */
   1240 		mutex_enter(cs->sc_iolock);
   1241 		cs->sc_flags &= ~(CCDF_INITED|CCDF_VLABEL);
   1242 		cs->sc_zap = true;
   1243 		while (disk_isbusy(&cs->sc_dkdev) ||
   1244 		    bufq_peek(cs->sc_bufq) != NULL ||
   1245 		    cs->sc_thread != NULL) {
   1246 			cv_broadcast(&cs->sc_push);
   1247 			(void)cv_timedwait(&cs->sc_stop, cs->sc_iolock, hz);
   1248 		}
   1249 		mutex_exit(cs->sc_iolock);
   1250 
   1251 		/*
   1252 		 * Free ccd_softc information and clear entry.
   1253 		 */
   1254 
   1255 		/* Close the components and free their pathnames. */
   1256 		for (i = 0; i < cs->sc_nccdisks; ++i) {
   1257 			/*
   1258 			 * XXX: this close could potentially fail and
   1259 			 * cause Bad Things.  Maybe we need to force
   1260 			 * the close to happen?
   1261 			 */
   1262 #ifdef DEBUG
   1263 			if (ccddebug & CCDB_VNODE)
   1264 				vprint("CCDIOCCLR: vnode info",
   1265 				    cs->sc_cinfo[i].ci_vp);
   1266 #endif
   1267 			(void)vn_close(cs->sc_cinfo[i].ci_vp, FREAD|FWRITE,
   1268 			    uc);
   1269 			kmem_free(cs->sc_cinfo[i].ci_path,
   1270 			    cs->sc_cinfo[i].ci_pathlen);
   1271 		}
   1272 
   1273 		/* Free interleave index. */
   1274 		for (i = 0; cs->sc_itable[i].ii_ndisk; ++i) {
   1275 			kmem_free(cs->sc_itable[i].ii_index,
   1276 			    cs->sc_itable[i].ii_indexsz);
   1277 		}
   1278 
   1279 		/* Free component info and interleave table. */
   1280 		kmem_free(cs->sc_cinfo, cs->sc_nccdisks *
   1281 		    sizeof(struct ccdcinfo));
   1282 		kmem_free(cs->sc_itable, (cs->sc_nccdisks + 1) *
   1283 		    sizeof(struct ccdiinfo));
   1284 
   1285 		/* Detatch the disk. */
   1286 		disk_detach(&cs->sc_dkdev);
   1287 		bufq_free(cs->sc_bufq);
   1288 		break;
   1289 
   1290 	case DIOCGDINFO:
   1291 		*(struct disklabel *)data = *(cs->sc_dkdev.dk_label);
   1292 		break;
   1293 
   1294 #ifdef __HAVE_OLD_DISKLABEL
   1295 	case ODIOCGDINFO:
   1296 		newlabel = *(cs->sc_dkdev.dk_label);
   1297 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1298 			return ENOTTY;
   1299 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
   1300 		break;
   1301 #endif
   1302 
   1303 	case DIOCGPART:
   1304 		((struct partinfo *)data)->disklab = cs->sc_dkdev.dk_label;
   1305 		((struct partinfo *)data)->part =
   1306 		    &cs->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
   1307 		break;
   1308 
   1309 	case DIOCCACHESYNC:
   1310 		/*
   1311 		 * XXX Do we really need to care about having a writable
   1312 		 * file descriptor here?
   1313 		 */
   1314 		if ((flag & FWRITE) == 0)
   1315 			return (EBADF);
   1316 
   1317 		/*
   1318 		 * We pass this call down to all components and report
   1319 		 * the first error we encounter.
   1320 		 */
   1321 		for (error = 0, i = 0; i < cs->sc_nccdisks; i++) {
   1322 			j = VOP_IOCTL(cs->sc_cinfo[i].ci_vp, cmd, data,
   1323 				      flag, uc);
   1324 			if (j != 0 && error == 0)
   1325 				error = j;
   1326 		}
   1327 		break;
   1328 
   1329 	case DIOCWDINFO:
   1330 	case DIOCSDINFO:
   1331 #ifdef __HAVE_OLD_DISKLABEL
   1332 	case ODIOCWDINFO:
   1333 	case ODIOCSDINFO:
   1334 #endif
   1335 	{
   1336 		struct disklabel *lp;
   1337 #ifdef __HAVE_OLD_DISKLABEL
   1338 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
   1339 			memset(&newlabel, 0, sizeof newlabel);
   1340 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
   1341 			lp = &newlabel;
   1342 		} else
   1343 #endif
   1344 		lp = (struct disklabel *)data;
   1345 
   1346 		cs->sc_flags |= CCDF_LABELLING;
   1347 
   1348 		error = setdisklabel(cs->sc_dkdev.dk_label,
   1349 		    lp, 0, cs->sc_dkdev.dk_cpulabel);
   1350 		if (error == 0) {
   1351 			if (cmd == DIOCWDINFO
   1352 #ifdef __HAVE_OLD_DISKLABEL
   1353 			    || cmd == ODIOCWDINFO
   1354 #endif
   1355 			   )
   1356 				error = writedisklabel(CCDLABELDEV(dev),
   1357 				    ccdstrategy, cs->sc_dkdev.dk_label,
   1358 				    cs->sc_dkdev.dk_cpulabel);
   1359 		}
   1360 
   1361 		cs->sc_flags &= ~CCDF_LABELLING;
   1362 		break;
   1363 	}
   1364 
   1365 	case DIOCKLABEL:
   1366 		if (*(int *)data != 0)
   1367 			cs->sc_flags |= CCDF_KLABEL;
   1368 		else
   1369 			cs->sc_flags &= ~CCDF_KLABEL;
   1370 		break;
   1371 
   1372 	case DIOCWLABEL:
   1373 		if (*(int *)data != 0)
   1374 			cs->sc_flags |= CCDF_WLABEL;
   1375 		else
   1376 			cs->sc_flags &= ~CCDF_WLABEL;
   1377 		break;
   1378 
   1379 	case DIOCGDEFLABEL:
   1380 		ccdgetdefaultlabel(cs, (struct disklabel *)data);
   1381 		break;
   1382 
   1383 #ifdef __HAVE_OLD_DISKLABEL
   1384 	case ODIOCGDEFLABEL:
   1385 		ccdgetdefaultlabel(cs, &newlabel);
   1386 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1387 			return ENOTTY;
   1388 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
   1389 		break;
   1390 #endif
   1391 
   1392 	default:
   1393 		error = ENOTTY;
   1394 	}
   1395 
   1396  out:
   1397 	mutex_exit(&cs->sc_dvlock);
   1398 	return (error);
   1399 }
   1400 
   1401 static int
   1402 ccdsize(dev_t dev)
   1403 {
   1404 	struct ccd_softc *cs;
   1405 	struct disklabel *lp;
   1406 	int part, unit, omask, size;
   1407 
   1408 	unit = ccdunit(dev);
   1409 	if (unit >= numccd)
   1410 		return (-1);
   1411 	cs = &ccd_softc[unit];
   1412 
   1413 	if ((cs->sc_flags & CCDF_INITED) == 0)
   1414 		return (-1);
   1415 
   1416 	part = DISKPART(dev);
   1417 	omask = cs->sc_dkdev.dk_openmask & (1 << part);
   1418 	lp = cs->sc_dkdev.dk_label;
   1419 
   1420 	if (omask == 0 && ccdopen(dev, 0, S_IFBLK, curlwp))
   1421 		return (-1);
   1422 
   1423 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
   1424 		size = -1;
   1425 	else
   1426 		size = lp->d_partitions[part].p_size *
   1427 		    (lp->d_secsize / DEV_BSIZE);
   1428 
   1429 	if (omask == 0 && ccdclose(dev, 0, S_IFBLK, curlwp))
   1430 		return (-1);
   1431 
   1432 	return (size);
   1433 }
   1434 
   1435 static void
   1436 ccdgetdefaultlabel(struct ccd_softc *cs, struct disklabel *lp)
   1437 {
   1438 	struct ccdgeom *ccg = &cs->sc_geom;
   1439 
   1440 	memset(lp, 0, sizeof(*lp));
   1441 
   1442 	lp->d_secperunit = cs->sc_size;
   1443 	lp->d_secsize = ccg->ccg_secsize;
   1444 	lp->d_nsectors = ccg->ccg_nsectors;
   1445 	lp->d_ntracks = ccg->ccg_ntracks;
   1446 	lp->d_ncylinders = ccg->ccg_ncylinders;
   1447 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1448 
   1449 	strncpy(lp->d_typename, "ccd", sizeof(lp->d_typename));
   1450 	lp->d_type = DTYPE_CCD;
   1451 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1452 	lp->d_rpm = 3600;
   1453 	lp->d_interleave = 1;
   1454 	lp->d_flags = 0;
   1455 
   1456 	lp->d_partitions[RAW_PART].p_offset = 0;
   1457 	lp->d_partitions[RAW_PART].p_size = cs->sc_size;
   1458 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1459 	lp->d_npartitions = RAW_PART + 1;
   1460 
   1461 	lp->d_magic = DISKMAGIC;
   1462 	lp->d_magic2 = DISKMAGIC;
   1463 	lp->d_checksum = dkcksum(cs->sc_dkdev.dk_label);
   1464 }
   1465 
   1466 /*
   1467  * Read the disklabel from the ccd.  If one is not present, fake one
   1468  * up.
   1469  */
   1470 static void
   1471 ccdgetdisklabel(dev_t dev)
   1472 {
   1473 	int unit = ccdunit(dev);
   1474 	struct ccd_softc *cs = &ccd_softc[unit];
   1475 	const char *errstring;
   1476 	struct disklabel *lp = cs->sc_dkdev.dk_label;
   1477 	struct cpu_disklabel *clp = cs->sc_dkdev.dk_cpulabel;
   1478 
   1479 	KASSERT(mutex_owned(&cs->sc_dvlock));
   1480 
   1481 	memset(clp, 0, sizeof(*clp));
   1482 
   1483 	ccdgetdefaultlabel(cs, lp);
   1484 
   1485 	/*
   1486 	 * Call the generic disklabel extraction routine.
   1487 	 */
   1488 	cs->sc_flags |= CCDF_RLABEL;
   1489 	if ((cs->sc_flags & CCDF_NOLABEL) != 0)
   1490 		errstring = "CCDF_NOLABEL set; ignoring on-disk label";
   1491 	else
   1492 		errstring = readdisklabel(CCDLABELDEV(dev), ccdstrategy,
   1493 		    cs->sc_dkdev.dk_label, cs->sc_dkdev.dk_cpulabel);
   1494 	if (errstring)
   1495 		ccdmakedisklabel(cs);
   1496 	else {
   1497 		int i;
   1498 		struct partition *pp;
   1499 
   1500 		/*
   1501 		 * Sanity check whether the found disklabel is valid.
   1502 		 *
   1503 		 * This is necessary since total size of ccd may vary
   1504 		 * when an interleave is changed even though exactly
   1505 		 * same componets are used, and old disklabel may used
   1506 		 * if that is found.
   1507 		 */
   1508 		if (lp->d_secperunit != cs->sc_size)
   1509 			printf("WARNING: %s: "
   1510 			    "total sector size in disklabel (%d) != "
   1511 			    "the size of ccd (%lu)\n", cs->sc_xname,
   1512 			    lp->d_secperunit, (u_long)cs->sc_size);
   1513 		for (i = 0; i < lp->d_npartitions; i++) {
   1514 			pp = &lp->d_partitions[i];
   1515 			if (pp->p_offset + pp->p_size > cs->sc_size)
   1516 				printf("WARNING: %s: end of partition `%c' "
   1517 				    "exceeds the size of ccd (%lu)\n",
   1518 				    cs->sc_xname, 'a' + i, (u_long)cs->sc_size);
   1519 		}
   1520 	}
   1521 
   1522 #ifdef DEBUG
   1523 	/* It's actually extremely common to have unlabeled ccds. */
   1524 	if (ccddebug & CCDB_LABEL)
   1525 		if (errstring != NULL)
   1526 			printf("%s: %s\n", cs->sc_xname, errstring);
   1527 #endif
   1528 
   1529 	/* In-core label now valid. */
   1530 	cs->sc_flags = (cs->sc_flags | CCDF_VLABEL) & ~CCDF_RLABEL;
   1531 }
   1532 
   1533 /*
   1534  * Take care of things one might want to take care of in the event
   1535  * that a disklabel isn't present.
   1536  */
   1537 static void
   1538 ccdmakedisklabel(struct ccd_softc *cs)
   1539 {
   1540 	struct disklabel *lp = cs->sc_dkdev.dk_label;
   1541 
   1542 	/*
   1543 	 * For historical reasons, if there's no disklabel present
   1544 	 * the raw partition must be marked FS_BSDFFS.
   1545 	 */
   1546 	lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
   1547 
   1548 	strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
   1549 
   1550 	lp->d_checksum = dkcksum(lp);
   1551 }
   1552 
   1553 #ifdef DEBUG
   1554 static void
   1555 printiinfo(struct ccdiinfo *ii)
   1556 {
   1557 	int ix, i;
   1558 
   1559 	for (ix = 0; ii->ii_ndisk; ix++, ii++) {
   1560 		printf(" itab[%d]: #dk %d sblk %" PRId64 " soff %" PRId64,
   1561 		    ix, ii->ii_ndisk, ii->ii_startblk, ii->ii_startoff);
   1562 		for (i = 0; i < ii->ii_ndisk; i++)
   1563 			printf(" %d", ii->ii_index[i]);
   1564 		printf("\n");
   1565 	}
   1566 }
   1567 #endif
   1568 
   1569 #ifdef _MODULE
   1570 
   1571 #include <sys/module.h>
   1572 
   1573 MODULE(MODULE_CLASS_DRIVER, ccd, NULL);
   1574 
   1575 static int
   1576 ccd_modcmd(modcmd_t cmd, void *arg)
   1577 {
   1578 	int bmajor = -1, cmajor = -1,  error = 0;
   1579 
   1580 	switch (cmd) {
   1581 	case MODULE_CMD_INIT:
   1582 		ccdattach(4);
   1583 
   1584 		return devsw_attach("ccd", &ccd_bdevsw, &bmajor,
   1585 		    &ccd_cdevsw, &cmajor);
   1586 		break;
   1587 
   1588 	case MODULE_CMD_FINI:
   1589 		return devsw_detach(&ccd_bdevsw, &ccd_cdevsw);
   1590 		break;
   1591 
   1592 	case MODULE_CMD_STAT:
   1593 		return ENOTTY;
   1594 
   1595 	default:
   1596 		return ENOTTY;
   1597 	}
   1598 
   1599 	return error;
   1600 }
   1601 
   1602 #endif
   1603