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