Home | History | Annotate | Line # | Download | only in dev
ccd.c revision 1.110
      1 /*	$NetBSD: ccd.c,v 1.110 2006/05/14 21:42:26 elad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996, 1997, 1998, 1999 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.110 2006/05/14 21:42:26 elad 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/lock.h>
    149 #include <sys/queue.h>
    150 #include <sys/kauth.h>
    151 
    152 #include <dev/ccdvar.h>
    153 
    154 #if defined(CCDDEBUG) && !defined(DEBUG)
    155 #define DEBUG
    156 #endif
    157 
    158 #ifdef DEBUG
    159 #define CCDB_FOLLOW	0x01
    160 #define CCDB_INIT	0x02
    161 #define CCDB_IO		0x04
    162 #define CCDB_LABEL	0x08
    163 #define CCDB_VNODE	0x10
    164 int ccddebug = 0x00;
    165 #endif
    166 
    167 #define	ccdunit(x)	DISKUNIT(x)
    168 
    169 struct ccdbuf {
    170 	struct buf	cb_buf;		/* new I/O buf */
    171 	struct buf	*cb_obp;	/* ptr. to original I/O buf */
    172 	struct ccd_softc *cb_sc;	/* pointer to ccd softc */
    173 	int		cb_comp;	/* target component */
    174 	SIMPLEQ_ENTRY(ccdbuf) cb_q;	/* fifo of component buffers */
    175 };
    176 
    177 /* component buffer pool */
    178 static struct pool ccd_cbufpool;
    179 
    180 #define	CCD_GETBUF()		pool_get(&ccd_cbufpool, PR_NOWAIT)
    181 #define	CCD_PUTBUF(cbp)		pool_put(&ccd_cbufpool, cbp)
    182 
    183 #define CCDLABELDEV(dev)	\
    184 	(MAKEDISKDEV(major((dev)), ccdunit((dev)), RAW_PART))
    185 
    186 /* called by main() at boot time */
    187 void	ccdattach(int);
    188 
    189 /* called by biodone() at interrupt time */
    190 static void	ccdiodone(struct buf *);
    191 
    192 static void	ccdstart(struct ccd_softc *);
    193 static void	ccdinterleave(struct ccd_softc *);
    194 static void	ccdintr(struct ccd_softc *, struct buf *);
    195 static int	ccdinit(struct ccd_softc *, char **, struct vnode **,
    196 		    struct lwp *);
    197 static int	ccdlookup(char *, struct lwp *, struct vnode **);
    198 static struct ccdbuf *ccdbuffer(struct ccd_softc *, struct buf *,
    199 		    daddr_t, caddr_t, 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);
    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 		cs->sc_dkdev.dk_name = cs->sc_xname;	/* XXX */
    265 		lockinit(&cs->sc_lock, PRIBIO, "ccdlk", 0, 0);
    266 		pseudo_disk_init(&cs->sc_dkdev);
    267 	}
    268 }
    269 
    270 static int
    271 ccdinit(struct ccd_softc *cs, char **cpaths, struct vnode **vpp,
    272     struct lwp *l)
    273 {
    274 	struct ccdcinfo *ci = NULL;
    275 	size_t size;
    276 	int ix;
    277 	struct vattr va;
    278 	size_t minsize;
    279 	int maxsecsize;
    280 	struct partinfo dpart;
    281 	struct ccdgeom *ccg = &cs->sc_geom;
    282 	char tmppath[MAXPATHLEN];
    283 	int error, path_alloced;
    284 
    285 #ifdef DEBUG
    286 	if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    287 		printf("%s: ccdinit\n", cs->sc_xname);
    288 #endif
    289 
    290 	/* Allocate space for the component info. */
    291 	cs->sc_cinfo = malloc(cs->sc_nccdisks * sizeof(struct ccdcinfo),
    292 	    M_DEVBUF, M_WAITOK);
    293 
    294 	cs->sc_size = 0;
    295 
    296 	/*
    297 	 * Verify that each component piece exists and record
    298 	 * relevant information about it.
    299 	 */
    300 	maxsecsize = 0;
    301 	minsize = 0;
    302 	for (ix = 0, path_alloced = 0; ix < cs->sc_nccdisks; ix++) {
    303 		ci = &cs->sc_cinfo[ix];
    304 		ci->ci_vp = vpp[ix];
    305 
    306 		/*
    307 		 * Copy in the pathname of the component.
    308 		 */
    309 		memset(tmppath, 0, sizeof(tmppath));	/* sanity */
    310 		error = copyinstr(cpaths[ix], tmppath,
    311 		    MAXPATHLEN, &ci->ci_pathlen);
    312 		if (error) {
    313 #ifdef DEBUG
    314 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    315 				printf("%s: can't copy path, error = %d\n",
    316 				    cs->sc_xname, error);
    317 #endif
    318 			goto out;
    319 		}
    320 		ci->ci_path = malloc(ci->ci_pathlen, M_DEVBUF, M_WAITOK);
    321 		memcpy(ci->ci_path, tmppath, ci->ci_pathlen);
    322 		path_alloced++;
    323 
    324 		/*
    325 		 * XXX: Cache the component's dev_t.
    326 		 */
    327 		if ((error = VOP_GETATTR(vpp[ix], &va, l->l_proc->p_cred, l)) != 0) {
    328 #ifdef DEBUG
    329 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    330 				printf("%s: %s: getattr failed %s = %d\n",
    331 				    cs->sc_xname, ci->ci_path,
    332 				    "error", error);
    333 #endif
    334 			goto out;
    335 		}
    336 		ci->ci_dev = va.va_rdev;
    337 
    338 		/*
    339 		 * Get partition information for the component.
    340 		 */
    341 		error = VOP_IOCTL(vpp[ix], DIOCGPART, &dpart,
    342 		    FREAD, l->l_proc->p_cred, l);
    343 		if (error) {
    344 #ifdef DEBUG
    345 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    346 				 printf("%s: %s: ioctl failed, error = %d\n",
    347 				     cs->sc_xname, ci->ci_path, error);
    348 #endif
    349 			goto out;
    350 		}
    351 
    352 /*
    353  * This diagnostic test is disabled (for now?) since not all port supports
    354  * on-disk BSD disklabel.
    355  */
    356 #if 0 /* def DIAGNOSTIC */
    357 		/* Check fstype field of component. */
    358 		if (dpart.part->p_fstype != FS_CCD)
    359 			printf("%s: WARNING: %s: fstype %d != FS_CCD\n",
    360 			    cs->sc_xname, ci->ci_path, dpart.part->p_fstype);
    361 #endif
    362 
    363 		/*
    364 		 * Calculate the size, truncating to an interleave
    365 		 * boundary if necessary.
    366 		 */
    367 		maxsecsize =
    368 		    ((dpart.disklab->d_secsize > maxsecsize) ?
    369 		    dpart.disklab->d_secsize : maxsecsize);
    370 		size = dpart.part->p_size;
    371 		if (cs->sc_ileave > 1)
    372 			size -= size % cs->sc_ileave;
    373 
    374 		if (size == 0) {
    375 #ifdef DEBUG
    376 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    377 				printf("%s: %s: size == 0\n",
    378 				    cs->sc_xname, ci->ci_path);
    379 #endif
    380 			error = ENODEV;
    381 			goto out;
    382 		}
    383 
    384 		if (minsize == 0 || size < minsize)
    385 			minsize = size;
    386 		ci->ci_size = size;
    387 		cs->sc_size += size;
    388 	}
    389 
    390 	/*
    391 	 * Don't allow the interleave to be smaller than
    392 	 * the biggest component sector.
    393 	 */
    394 	if ((cs->sc_ileave > 0) &&
    395 	    (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) {
    396 #ifdef DEBUG
    397 		if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    398 			printf("%s: interleave must be at least %d\n",
    399 			    cs->sc_xname, (maxsecsize / DEV_BSIZE));
    400 #endif
    401 		error = EINVAL;
    402 		goto out;
    403 	}
    404 
    405 	/*
    406 	 * If uniform interleave is desired set all sizes to that of
    407 	 * the smallest component.
    408 	 */
    409 	if (cs->sc_flags & CCDF_UNIFORM) {
    410 		for (ci = cs->sc_cinfo;
    411 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    412 			ci->ci_size = minsize;
    413 
    414 		cs->sc_size = cs->sc_nccdisks * minsize;
    415 	}
    416 
    417 	/*
    418 	 * Construct the interleave table.
    419 	 */
    420 	ccdinterleave(cs);
    421 
    422 	/*
    423 	 * Create pseudo-geometry based on 1MB cylinders.  It's
    424 	 * pretty close.
    425 	 */
    426 	ccg->ccg_secsize = DEV_BSIZE;
    427 	ccg->ccg_ntracks = 1;
    428 	ccg->ccg_nsectors = 1024 * (1024 / ccg->ccg_secsize);
    429 	ccg->ccg_ncylinders = cs->sc_size / ccg->ccg_nsectors;
    430 
    431 	cs->sc_flags |= CCDF_INITED;
    432 
    433 	return (0);
    434 
    435  out:
    436 	for (ix = 0; ix < path_alloced; ix++)
    437 		free(cs->sc_cinfo[ix].ci_path, M_DEVBUF);
    438 	free(cs->sc_cinfo, M_DEVBUF);
    439 	return (error);
    440 }
    441 
    442 static void
    443 ccdinterleave(struct ccd_softc *cs)
    444 {
    445 	struct ccdcinfo *ci, *smallci;
    446 	struct ccdiinfo *ii;
    447 	daddr_t bn, lbn;
    448 	int ix;
    449 	u_long size;
    450 
    451 #ifdef DEBUG
    452 	if (ccddebug & CCDB_INIT)
    453 		printf("ccdinterleave(%p): ileave %d\n", cs, cs->sc_ileave);
    454 #endif
    455 	/*
    456 	 * Allocate an interleave table.
    457 	 * Chances are this is too big, but we don't care.
    458 	 */
    459 	size = (cs->sc_nccdisks + 1) * sizeof(struct ccdiinfo);
    460 	cs->sc_itable = (struct ccdiinfo *)malloc(size, M_DEVBUF,
    461 	    M_WAITOK|M_ZERO);
    462 
    463 	/*
    464 	 * Trivial case: no interleave (actually interleave of disk size).
    465 	 * Each table entry represents a single component in its entirety.
    466 	 */
    467 	if (cs->sc_ileave == 0) {
    468 		bn = 0;
    469 		ii = cs->sc_itable;
    470 
    471 		for (ix = 0; ix < cs->sc_nccdisks; ix++) {
    472 			/* Allocate space for ii_index. */
    473 			ii->ii_index = malloc(sizeof(int), M_DEVBUF, M_WAITOK);
    474 			ii->ii_ndisk = 1;
    475 			ii->ii_startblk = bn;
    476 			ii->ii_startoff = 0;
    477 			ii->ii_index[0] = ix;
    478 			bn += cs->sc_cinfo[ix].ci_size;
    479 			ii++;
    480 		}
    481 		ii->ii_ndisk = 0;
    482 #ifdef DEBUG
    483 		if (ccddebug & CCDB_INIT)
    484 			printiinfo(cs->sc_itable);
    485 #endif
    486 		return;
    487 	}
    488 
    489 	/*
    490 	 * The following isn't fast or pretty; it doesn't have to be.
    491 	 */
    492 	size = 0;
    493 	bn = lbn = 0;
    494 	for (ii = cs->sc_itable; ; ii++) {
    495 		/* Allocate space for ii_index. */
    496 		ii->ii_index = malloc((sizeof(int) * cs->sc_nccdisks),
    497 		    M_DEVBUF, M_WAITOK);
    498 
    499 		/*
    500 		 * Locate the smallest of the remaining components
    501 		 */
    502 		smallci = NULL;
    503 		for (ci = cs->sc_cinfo;
    504 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    505 			if (ci->ci_size > size &&
    506 			    (smallci == NULL ||
    507 			     ci->ci_size < smallci->ci_size))
    508 				smallci = ci;
    509 
    510 		/*
    511 		 * Nobody left, all done
    512 		 */
    513 		if (smallci == NULL) {
    514 			ii->ii_ndisk = 0;
    515 			break;
    516 		}
    517 
    518 		/*
    519 		 * Record starting logical block and component offset
    520 		 */
    521 		ii->ii_startblk = bn / cs->sc_ileave;
    522 		ii->ii_startoff = lbn;
    523 
    524 		/*
    525 		 * Determine how many disks take part in this interleave
    526 		 * and record their indices.
    527 		 */
    528 		ix = 0;
    529 		for (ci = cs->sc_cinfo;
    530 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    531 			if (ci->ci_size >= smallci->ci_size)
    532 				ii->ii_index[ix++] = ci - cs->sc_cinfo;
    533 		ii->ii_ndisk = ix;
    534 		bn += ix * (smallci->ci_size - size);
    535 		lbn = smallci->ci_size / cs->sc_ileave;
    536 		size = smallci->ci_size;
    537 	}
    538 #ifdef DEBUG
    539 	if (ccddebug & CCDB_INIT)
    540 		printiinfo(cs->sc_itable);
    541 #endif
    542 }
    543 
    544 /* ARGSUSED */
    545 static int
    546 ccdopen(dev_t dev, int flags, int fmt, struct lwp *l)
    547 {
    548 	int unit = ccdunit(dev);
    549 	struct ccd_softc *cs;
    550 	struct disklabel *lp;
    551 	int error = 0, part, pmask;
    552 
    553 #ifdef DEBUG
    554 	if (ccddebug & CCDB_FOLLOW)
    555 		printf("ccdopen(0x%x, 0x%x)\n", dev, flags);
    556 #endif
    557 	if (unit >= numccd)
    558 		return (ENXIO);
    559 	cs = &ccd_softc[unit];
    560 
    561 	if ((error = lockmgr(&cs->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
    562 		return (error);
    563 
    564 	lp = cs->sc_dkdev.dk_label;
    565 
    566 	part = DISKPART(dev);
    567 	pmask = (1 << part);
    568 
    569 	/*
    570 	 * If we're initialized, check to see if there are any other
    571 	 * open partitions.  If not, then it's safe to update
    572 	 * the in-core disklabel.  Only read the disklabel if it is
    573 	 * not already valid.
    574 	 */
    575 	if ((cs->sc_flags & (CCDF_INITED|CCDF_VLABEL)) == CCDF_INITED &&
    576 	    cs->sc_dkdev.dk_openmask == 0)
    577 		ccdgetdisklabel(dev);
    578 
    579 	/* Check that the partition exists. */
    580 	if (part != RAW_PART) {
    581 		if (((cs->sc_flags & CCDF_INITED) == 0) ||
    582 		    ((part >= lp->d_npartitions) ||
    583 		     (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
    584 			error = ENXIO;
    585 			goto done;
    586 		}
    587 	}
    588 
    589 	/* Prevent our unit from being unconfigured while open. */
    590 	switch (fmt) {
    591 	case S_IFCHR:
    592 		cs->sc_dkdev.dk_copenmask |= pmask;
    593 		break;
    594 
    595 	case S_IFBLK:
    596 		cs->sc_dkdev.dk_bopenmask |= pmask;
    597 		break;
    598 	}
    599 	cs->sc_dkdev.dk_openmask =
    600 	    cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
    601 
    602  done:
    603 	(void) lockmgr(&cs->sc_lock, LK_RELEASE, NULL);
    604 	return (error);
    605 }
    606 
    607 /* ARGSUSED */
    608 static int
    609 ccdclose(dev_t dev, int flags, int fmt, struct lwp *l)
    610 {
    611 	int unit = ccdunit(dev);
    612 	struct ccd_softc *cs;
    613 	int error = 0, part;
    614 
    615 #ifdef DEBUG
    616 	if (ccddebug & CCDB_FOLLOW)
    617 		printf("ccdclose(0x%x, 0x%x)\n", dev, flags);
    618 #endif
    619 
    620 	if (unit >= numccd)
    621 		return (ENXIO);
    622 	cs = &ccd_softc[unit];
    623 
    624 	if ((error = lockmgr(&cs->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
    625 		return (error);
    626 
    627 	part = DISKPART(dev);
    628 
    629 	/* ...that much closer to allowing unconfiguration... */
    630 	switch (fmt) {
    631 	case S_IFCHR:
    632 		cs->sc_dkdev.dk_copenmask &= ~(1 << part);
    633 		break;
    634 
    635 	case S_IFBLK:
    636 		cs->sc_dkdev.dk_bopenmask &= ~(1 << part);
    637 		break;
    638 	}
    639 	cs->sc_dkdev.dk_openmask =
    640 	    cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
    641 
    642 	if (cs->sc_dkdev.dk_openmask == 0) {
    643 		if ((cs->sc_flags & CCDF_KLABEL) == 0)
    644 			cs->sc_flags &= ~CCDF_VLABEL;
    645 	}
    646 
    647 	(void) lockmgr(&cs->sc_lock, LK_RELEASE, NULL);
    648 	return (0);
    649 }
    650 
    651 static void
    652 ccdstrategy(struct buf *bp)
    653 {
    654 	int unit = ccdunit(bp->b_dev);
    655 	struct ccd_softc *cs = &ccd_softc[unit];
    656 	daddr_t blkno;
    657 	int s;
    658 	int wlabel;
    659 	struct disklabel *lp;
    660 
    661 #ifdef DEBUG
    662 	if (ccddebug & CCDB_FOLLOW)
    663 		printf("ccdstrategy(%p): unit %d\n", bp, unit);
    664 #endif
    665 	if ((cs->sc_flags & CCDF_INITED) == 0) {
    666 #ifdef DEBUG
    667 		if (ccddebug & CCDB_FOLLOW)
    668 			printf("ccdstrategy: unit %d: not inited\n", unit);
    669 #endif
    670 		bp->b_error = ENXIO;
    671 		bp->b_flags |= B_ERROR;
    672 		goto done;
    673 	}
    674 
    675 	/* If it's a nil transfer, wake up the top half now. */
    676 	if (bp->b_bcount == 0)
    677 		goto done;
    678 
    679 	lp = cs->sc_dkdev.dk_label;
    680 
    681 	/*
    682 	 * Do bounds checking and adjust transfer.  If there's an
    683 	 * error, the bounds check will flag that for us.  Convert
    684 	 * the partition relative block number to an absolute.
    685 	 */
    686 	blkno = bp->b_blkno;
    687 	wlabel = cs->sc_flags & (CCDF_WLABEL|CCDF_LABELLING);
    688 	if (DISKPART(bp->b_dev) != RAW_PART) {
    689 		if (bounds_check_with_label(&cs->sc_dkdev, bp, wlabel) <= 0)
    690 			goto done;
    691 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
    692 	}
    693 	bp->b_rawblkno = blkno;
    694 
    695 	/* Place it in the queue and start I/O on the unit. */
    696 	s = splbio();
    697 	BUFQ_PUT(cs->sc_bufq, bp);
    698 	ccdstart(cs);
    699 	splx(s);
    700 	return;
    701 
    702  done:
    703 	bp->b_resid = bp->b_bcount;
    704 	biodone(bp);
    705 }
    706 
    707 static void
    708 ccdstart(struct ccd_softc *cs)
    709 {
    710 	long bcount, rcount;
    711 	struct buf *bp;
    712 	struct ccdbuf *cbp;
    713 	caddr_t addr;
    714 	daddr_t bn;
    715 	SIMPLEQ_HEAD(, ccdbuf) cbufq;
    716 
    717 #ifdef DEBUG
    718 	if (ccddebug & CCDB_FOLLOW)
    719 		printf("ccdstart(%p)\n", cs);
    720 #endif
    721 
    722 	/* See if there is work for us to do. */
    723 	while ((bp = BUFQ_PEEK(cs->sc_bufq)) != NULL) {
    724 		/* Instrumentation. */
    725 		disk_busy(&cs->sc_dkdev);
    726 
    727 		bp->b_resid = bp->b_bcount;
    728 		bn = bp->b_rawblkno;
    729 
    730 		/* Allocate the component buffers. */
    731 		SIMPLEQ_INIT(&cbufq);
    732 		addr = bp->b_data;
    733 		for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
    734 			cbp = ccdbuffer(cs, bp, bn, addr, bcount);
    735 			if (cbp == NULL) {
    736 				/*
    737 				 * Can't allocate a component buffer; just
    738 				 * defer the job until later.
    739 				 *
    740 				 * XXX We might consider a watchdog timer
    741 				 * XXX to make sure we are kicked into action,
    742 				 * XXX or consider a low-water mark for our
    743 				 * XXX component buffer pool.
    744 				 */
    745 				while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    746 					SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    747 					CCD_PUTBUF(cbp);
    748 				}
    749 				disk_unbusy(&cs->sc_dkdev, 0, 0);
    750 				return;
    751 			}
    752 			SIMPLEQ_INSERT_TAIL(&cbufq, cbp, cb_q);
    753 			rcount = cbp->cb_buf.b_bcount;
    754 			bn += btodb(rcount);
    755 			addr += rcount;
    756 		}
    757 
    758 		/* Transfer all set up, remove job from the queue. */
    759 		(void) BUFQ_GET(cs->sc_bufq);
    760 
    761 		/* Now fire off the requests. */
    762 		while ((cbp = SIMPLEQ_FIRST(&cbufq)) != NULL) {
    763 			SIMPLEQ_REMOVE_HEAD(&cbufq, cb_q);
    764 			if ((cbp->cb_buf.b_flags & B_READ) == 0)
    765 				cbp->cb_buf.b_vp->v_numoutput++;
    766 			DEV_STRATEGY(&cbp->cb_buf);
    767 		}
    768 	}
    769 }
    770 
    771 /*
    772  * Build a component buffer header.
    773  */
    774 static struct ccdbuf *
    775 ccdbuffer(struct ccd_softc *cs, struct buf *bp, daddr_t bn, caddr_t addr,
    776     long bcount)
    777 {
    778 	struct ccdcinfo *ci;
    779 	struct ccdbuf *cbp;
    780 	daddr_t cbn, cboff;
    781 	u_int64_t cbc;
    782 	int ccdisk;
    783 
    784 #ifdef DEBUG
    785 	if (ccddebug & CCDB_IO)
    786 		printf("ccdbuffer(%p, %p, %" PRId64 ", %p, %ld)\n",
    787 		       cs, bp, bn, addr, bcount);
    788 #endif
    789 	/*
    790 	 * Determine which component bn falls in.
    791 	 */
    792 	cbn = bn;
    793 	cboff = 0;
    794 
    795 	/*
    796 	 * Serially concatenated
    797 	 */
    798 	if (cs->sc_ileave == 0) {
    799 		daddr_t sblk;
    800 
    801 		sblk = 0;
    802 		for (ccdisk = 0, ci = &cs->sc_cinfo[ccdisk];
    803 		    cbn >= sblk + ci->ci_size;
    804 		    ccdisk++, ci = &cs->sc_cinfo[ccdisk])
    805 			sblk += ci->ci_size;
    806 		cbn -= sblk;
    807 	}
    808 	/*
    809 	 * Interleaved
    810 	 */
    811 	else {
    812 		struct ccdiinfo *ii;
    813 		int off;
    814 
    815 		cboff = cbn % cs->sc_ileave;
    816 		cbn /= cs->sc_ileave;
    817 		for (ii = cs->sc_itable; ii->ii_ndisk; ii++)
    818 			if (ii->ii_startblk > cbn)
    819 				break;
    820 		ii--;
    821 		off = cbn - ii->ii_startblk;
    822 		if (ii->ii_ndisk == 1) {
    823 			ccdisk = ii->ii_index[0];
    824 			cbn = ii->ii_startoff + off;
    825 		} else {
    826 			ccdisk = ii->ii_index[off % ii->ii_ndisk];
    827 			cbn = ii->ii_startoff + off / ii->ii_ndisk;
    828 		}
    829 		cbn *= cs->sc_ileave;
    830 		ci = &cs->sc_cinfo[ccdisk];
    831 	}
    832 
    833 	/*
    834 	 * Fill in the component buf structure.
    835 	 */
    836 	cbp = CCD_GETBUF();
    837 	if (cbp == NULL)
    838 		return (NULL);
    839 	BUF_INIT(&cbp->cb_buf);
    840 	cbp->cb_buf.b_flags = bp->b_flags | B_CALL;
    841 	cbp->cb_buf.b_iodone = ccdiodone;
    842 	cbp->cb_buf.b_proc = bp->b_proc;
    843 	cbp->cb_buf.b_dev = ci->ci_dev;
    844 	cbp->cb_buf.b_blkno = cbn + cboff;
    845 	cbp->cb_buf.b_data = addr;
    846 	cbp->cb_buf.b_vp = ci->ci_vp;
    847 	if (cs->sc_ileave == 0)
    848 		cbc = dbtob((u_int64_t)(ci->ci_size - cbn));
    849 	else
    850 		cbc = dbtob((u_int64_t)(cs->sc_ileave - cboff));
    851 	cbp->cb_buf.b_bcount = cbc < bcount ? cbc : bcount;
    852 
    853 	/*
    854 	 * context for ccdiodone
    855 	 */
    856 	cbp->cb_obp = bp;
    857 	cbp->cb_sc = cs;
    858 	cbp->cb_comp = ccdisk;
    859 
    860 	BIO_COPYPRIO(&cbp->cb_buf, bp);
    861 
    862 #ifdef DEBUG
    863 	if (ccddebug & CCDB_IO)
    864 		printf(" dev 0x%x(u%lu): cbp %p bn %" PRId64 " addr %p"
    865 		       " bcnt %d\n",
    866 		    ci->ci_dev, (unsigned long) (ci-cs->sc_cinfo), cbp,
    867 		    cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
    868 		    cbp->cb_buf.b_bcount);
    869 #endif
    870 
    871 	return (cbp);
    872 }
    873 
    874 static void
    875 ccdintr(struct ccd_softc *cs, struct buf *bp)
    876 {
    877 
    878 #ifdef DEBUG
    879 	if (ccddebug & CCDB_FOLLOW)
    880 		printf("ccdintr(%p, %p)\n", cs, bp);
    881 #endif
    882 	/*
    883 	 * Request is done for better or worse, wakeup the top half.
    884 	 */
    885 	if (bp->b_flags & B_ERROR)
    886 		bp->b_resid = bp->b_bcount;
    887 	disk_unbusy(&cs->sc_dkdev, (bp->b_bcount - bp->b_resid),
    888 	    (bp->b_flags & B_READ));
    889 	biodone(bp);
    890 }
    891 
    892 /*
    893  * Called at interrupt time.
    894  * Mark the component as done and if all components are done,
    895  * take a ccd interrupt.
    896  */
    897 static void
    898 ccdiodone(struct buf *vbp)
    899 {
    900 	struct ccdbuf *cbp = (struct ccdbuf *) vbp;
    901 	struct buf *bp = cbp->cb_obp;
    902 	struct ccd_softc *cs = cbp->cb_sc;
    903 	int count, s;
    904 
    905 	s = splbio();
    906 #ifdef DEBUG
    907 	if (ccddebug & CCDB_FOLLOW)
    908 		printf("ccdiodone(%p)\n", cbp);
    909 	if (ccddebug & CCDB_IO) {
    910 		printf("ccdiodone: bp %p bcount %d resid %d\n",
    911 		       bp, bp->b_bcount, bp->b_resid);
    912 		printf(" dev 0x%x(u%d), cbp %p bn %" PRId64 " addr %p"
    913 		       " bcnt %d\n",
    914 		       cbp->cb_buf.b_dev, cbp->cb_comp, cbp,
    915 		       cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
    916 		       cbp->cb_buf.b_bcount);
    917 	}
    918 #endif
    919 
    920 	if (cbp->cb_buf.b_flags & B_ERROR) {
    921 		bp->b_flags |= B_ERROR;
    922 		bp->b_error = cbp->cb_buf.b_error ?
    923 		    cbp->cb_buf.b_error : EIO;
    924 
    925 		printf("%s: error %d on component %d\n",
    926 		       cs->sc_xname, bp->b_error, cbp->cb_comp);
    927 	}
    928 	count = cbp->cb_buf.b_bcount;
    929 	CCD_PUTBUF(cbp);
    930 
    931 	/*
    932 	 * If all done, "interrupt".
    933 	 */
    934 	bp->b_resid -= count;
    935 	if (bp->b_resid < 0)
    936 		panic("ccdiodone: count");
    937 	if (bp->b_resid == 0)
    938 		ccdintr(cs, bp);
    939 	splx(s);
    940 }
    941 
    942 /* ARGSUSED */
    943 static int
    944 ccdread(dev_t dev, struct uio *uio, int flags)
    945 {
    946 	int unit = ccdunit(dev);
    947 	struct ccd_softc *cs;
    948 
    949 #ifdef DEBUG
    950 	if (ccddebug & CCDB_FOLLOW)
    951 		printf("ccdread(0x%x, %p)\n", dev, uio);
    952 #endif
    953 	if (unit >= numccd)
    954 		return (ENXIO);
    955 	cs = &ccd_softc[unit];
    956 
    957 	if ((cs->sc_flags & CCDF_INITED) == 0)
    958 		return (ENXIO);
    959 
    960 	return (physio(ccdstrategy, NULL, dev, B_READ, minphys, uio));
    961 }
    962 
    963 /* ARGSUSED */
    964 static int
    965 ccdwrite(dev_t dev, struct uio *uio, int flags)
    966 {
    967 	int unit = ccdunit(dev);
    968 	struct ccd_softc *cs;
    969 
    970 #ifdef DEBUG
    971 	if (ccddebug & CCDB_FOLLOW)
    972 		printf("ccdwrite(0x%x, %p)\n", dev, uio);
    973 #endif
    974 	if (unit >= numccd)
    975 		return (ENXIO);
    976 	cs = &ccd_softc[unit];
    977 
    978 	if ((cs->sc_flags & CCDF_INITED) == 0)
    979 		return (ENXIO);
    980 
    981 	return (physio(ccdstrategy, NULL, dev, B_WRITE, minphys, uio));
    982 }
    983 
    984 static int
    985 ccdioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    986 {
    987 	int unit = ccdunit(dev);
    988 	int s, i, j, lookedup = 0, error;
    989 	int part, pmask;
    990 	struct ccd_softc *cs;
    991 	struct ccd_ioctl *ccio = (struct ccd_ioctl *)data;
    992 	kauth_cred_t uc;
    993 	char **cpp;
    994 	struct vnode **vpp;
    995 #ifdef __HAVE_OLD_DISKLABEL
    996 	struct disklabel newlabel;
    997 #endif
    998 
    999 	if (unit >= numccd)
   1000 		return (ENXIO);
   1001 	cs = &ccd_softc[unit];
   1002 
   1003 	uc = (l != NULL) ? l->l_proc->p_cred : NOCRED;
   1004 
   1005 	/* Must be open for writes for these commands... */
   1006 	switch (cmd) {
   1007 	case CCDIOCSET:
   1008 	case CCDIOCCLR:
   1009 	case DIOCSDINFO:
   1010 	case DIOCWDINFO:
   1011 #ifdef __HAVE_OLD_DISKLABEL
   1012 	case ODIOCSDINFO:
   1013 	case ODIOCWDINFO:
   1014 #endif
   1015 	case DIOCKLABEL:
   1016 	case DIOCWLABEL:
   1017 		if ((flag & FWRITE) == 0)
   1018 			return (EBADF);
   1019 	}
   1020 
   1021 	if ((error = lockmgr(&cs->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
   1022 		return (error);
   1023 
   1024 	/* Must be initialized for these... */
   1025 	switch (cmd) {
   1026 	case CCDIOCCLR:
   1027 	case DIOCGDINFO:
   1028 	case DIOCCACHESYNC:
   1029 	case DIOCSDINFO:
   1030 	case DIOCWDINFO:
   1031 	case DIOCGPART:
   1032 	case DIOCWLABEL:
   1033 	case DIOCKLABEL:
   1034 	case DIOCGDEFLABEL:
   1035 #ifdef __HAVE_OLD_DISKLABEL
   1036 	case ODIOCGDINFO:
   1037 	case ODIOCSDINFO:
   1038 	case ODIOCWDINFO:
   1039 	case ODIOCGDEFLABEL:
   1040 #endif
   1041 		if ((cs->sc_flags & CCDF_INITED) == 0) {
   1042 			error = ENXIO;
   1043 			goto out;
   1044 		}
   1045 	}
   1046 
   1047 	switch (cmd) {
   1048 	case CCDIOCSET:
   1049 		if (cs->sc_flags & CCDF_INITED) {
   1050 			error = EBUSY;
   1051 			goto out;
   1052 		}
   1053 
   1054 		/* Validate the flags. */
   1055 		if ((ccio->ccio_flags & CCDF_USERMASK) != ccio->ccio_flags) {
   1056 			error = EINVAL;
   1057 			goto out;
   1058 		}
   1059 
   1060 		if (ccio->ccio_ndisks > CCD_MAXNDISKS) {
   1061 			error = EINVAL;
   1062 			goto out;
   1063 		}
   1064 
   1065 		/* Fill in some important bits. */
   1066 		cs->sc_ileave = ccio->ccio_ileave;
   1067 		cs->sc_nccdisks = ccio->ccio_ndisks;
   1068 		cs->sc_flags = ccio->ccio_flags & CCDF_USERMASK;
   1069 
   1070 		/*
   1071 		 * Allocate space for and copy in the array of
   1072 		 * componet pathnames and device numbers.
   1073 		 */
   1074 		cpp = malloc(ccio->ccio_ndisks * sizeof(char *),
   1075 		    M_DEVBUF, M_WAITOK);
   1076 		vpp = malloc(ccio->ccio_ndisks * sizeof(struct vnode *),
   1077 		    M_DEVBUF, M_WAITOK);
   1078 
   1079 		error = copyin(ccio->ccio_disks, cpp,
   1080 		    ccio->ccio_ndisks * sizeof(char **));
   1081 		if (error) {
   1082 			free(vpp, M_DEVBUF);
   1083 			free(cpp, M_DEVBUF);
   1084 			goto out;
   1085 		}
   1086 
   1087 #ifdef DEBUG
   1088 		if (ccddebug & CCDB_INIT)
   1089 			for (i = 0; i < ccio->ccio_ndisks; ++i)
   1090 				printf("ccdioctl: component %d: %p\n",
   1091 				    i, cpp[i]);
   1092 #endif
   1093 
   1094 		for (i = 0; i < ccio->ccio_ndisks; ++i) {
   1095 #ifdef DEBUG
   1096 			if (ccddebug & CCDB_INIT)
   1097 				printf("ccdioctl: lookedup = %d\n", lookedup);
   1098 #endif
   1099 			if ((error = ccdlookup(cpp[i], l, &vpp[i])) != 0) {
   1100 				for (j = 0; j < lookedup; ++j)
   1101 					(void)vn_close(vpp[j], FREAD|FWRITE,
   1102 					    uc, l);
   1103 				free(vpp, M_DEVBUF);
   1104 				free(cpp, M_DEVBUF);
   1105 				goto out;
   1106 			}
   1107 			++lookedup;
   1108 		}
   1109 
   1110 		/*
   1111 		 * Initialize the ccd.  Fills in the softc for us.
   1112 		 */
   1113 		if ((error = ccdinit(cs, cpp, vpp, l)) != 0) {
   1114 			for (j = 0; j < lookedup; ++j)
   1115 				(void)vn_close(vpp[j], FREAD|FWRITE,
   1116 				    uc, l);
   1117 			free(vpp, M_DEVBUF);
   1118 			free(cpp, M_DEVBUF);
   1119 			goto out;
   1120 		}
   1121 
   1122 		/* We can free the temporary variables now. */
   1123 		free(vpp, M_DEVBUF);
   1124 		free(cpp, M_DEVBUF);
   1125 
   1126 		/*
   1127 		 * The ccd has been successfully initialized, so
   1128 		 * we can place it into the array.  Don't try to
   1129 		 * read the disklabel until the disk has been attached,
   1130 		 * because space for the disklabel is allocated
   1131 		 * in disk_attach();
   1132 		 */
   1133 		ccio->ccio_unit = unit;
   1134 		ccio->ccio_size = cs->sc_size;
   1135 
   1136 		bufq_alloc(&cs->sc_bufq, "fcfs", 0);
   1137 
   1138 		/* Attach the disk. */
   1139 		pseudo_disk_attach(&cs->sc_dkdev);
   1140 
   1141 		/* Try and read the disklabel. */
   1142 		ccdgetdisklabel(dev);
   1143 		break;
   1144 
   1145 	case CCDIOCCLR:
   1146 		/*
   1147 		 * Don't unconfigure if any other partitions are open
   1148 		 * or if both the character and block flavors of this
   1149 		 * partition are open.
   1150 		 */
   1151 		part = DISKPART(dev);
   1152 		pmask = (1 << part);
   1153 		if ((cs->sc_dkdev.dk_openmask & ~pmask) ||
   1154 		    ((cs->sc_dkdev.dk_bopenmask & pmask) &&
   1155 		    (cs->sc_dkdev.dk_copenmask & pmask))) {
   1156 			error = EBUSY;
   1157 			goto out;
   1158 		}
   1159 
   1160 		/* Kill off any queued buffers. */
   1161 		s = splbio();
   1162 		bufq_drain(cs->sc_bufq);
   1163 		splx(s);
   1164 
   1165 		bufq_free(cs->sc_bufq);
   1166 
   1167 		/*
   1168 		 * Free ccd_softc information and clear entry.
   1169 		 */
   1170 
   1171 		/* Close the components and free their pathnames. */
   1172 		for (i = 0; i < cs->sc_nccdisks; ++i) {
   1173 			/*
   1174 			 * XXX: this close could potentially fail and
   1175 			 * cause Bad Things.  Maybe we need to force
   1176 			 * the close to happen?
   1177 			 */
   1178 #ifdef DEBUG
   1179 			if (ccddebug & CCDB_VNODE)
   1180 				vprint("CCDIOCCLR: vnode info",
   1181 				    cs->sc_cinfo[i].ci_vp);
   1182 #endif
   1183 			(void)vn_close(cs->sc_cinfo[i].ci_vp, FREAD|FWRITE,
   1184 			    uc, l);
   1185 			free(cs->sc_cinfo[i].ci_path, M_DEVBUF);
   1186 		}
   1187 
   1188 		/* Free interleave index. */
   1189 		for (i = 0; cs->sc_itable[i].ii_ndisk; ++i)
   1190 			free(cs->sc_itable[i].ii_index, M_DEVBUF);
   1191 
   1192 		/* Free component info and interleave table. */
   1193 		free(cs->sc_cinfo, M_DEVBUF);
   1194 		free(cs->sc_itable, M_DEVBUF);
   1195 		cs->sc_flags &= ~(CCDF_INITED|CCDF_VLABEL);
   1196 
   1197 		/* Detatch the disk. */
   1198 		pseudo_disk_detach(&cs->sc_dkdev);
   1199 		break;
   1200 
   1201 	case DIOCGDINFO:
   1202 		*(struct disklabel *)data = *(cs->sc_dkdev.dk_label);
   1203 		break;
   1204 #ifdef __HAVE_OLD_DISKLABEL
   1205 	case ODIOCGDINFO:
   1206 		newlabel = *(cs->sc_dkdev.dk_label);
   1207 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1208 			return ENOTTY;
   1209 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
   1210 		break;
   1211 #endif
   1212 
   1213 	case DIOCGPART:
   1214 		((struct partinfo *)data)->disklab = cs->sc_dkdev.dk_label;
   1215 		((struct partinfo *)data)->part =
   1216 		    &cs->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
   1217 		break;
   1218 
   1219 	case DIOCCACHESYNC:
   1220 		/*
   1221 		 * XXX Do we really need to care about having a writable
   1222 		 * file descriptor here?
   1223 		 */
   1224 		if ((flag & FWRITE) == 0)
   1225 			return (EBADF);
   1226 
   1227 		/*
   1228 		 * We pass this call down to all components and report
   1229 		 * the first error we encounter.
   1230 		 */
   1231 		for (error = 0, i = 0; i < cs->sc_nccdisks; i++) {
   1232 			j = VOP_IOCTL(cs->sc_cinfo[i].ci_vp, cmd, data,
   1233 				      flag, uc, l);
   1234 			if (j != 0 && error == 0)
   1235 				error = j;
   1236 		}
   1237 		break;
   1238 
   1239 	case DIOCWDINFO:
   1240 	case DIOCSDINFO:
   1241 #ifdef __HAVE_OLD_DISKLABEL
   1242 	case ODIOCWDINFO:
   1243 	case ODIOCSDINFO:
   1244 #endif
   1245 	{
   1246 		struct disklabel *lp;
   1247 #ifdef __HAVE_OLD_DISKLABEL
   1248 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
   1249 			memset(&newlabel, 0, sizeof newlabel);
   1250 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
   1251 			lp = &newlabel;
   1252 		} else
   1253 #endif
   1254 		lp = (struct disklabel *)data;
   1255 
   1256 		cs->sc_flags |= CCDF_LABELLING;
   1257 
   1258 		error = setdisklabel(cs->sc_dkdev.dk_label,
   1259 		    lp, 0, cs->sc_dkdev.dk_cpulabel);
   1260 		if (error == 0) {
   1261 			if (cmd == DIOCWDINFO
   1262 #ifdef __HAVE_OLD_DISKLABEL
   1263 			    || cmd == ODIOCWDINFO
   1264 #endif
   1265 			   )
   1266 				error = writedisklabel(CCDLABELDEV(dev),
   1267 				    ccdstrategy, cs->sc_dkdev.dk_label,
   1268 				    cs->sc_dkdev.dk_cpulabel);
   1269 		}
   1270 
   1271 		cs->sc_flags &= ~CCDF_LABELLING;
   1272 		break;
   1273 	}
   1274 
   1275 	case DIOCKLABEL:
   1276 		if (*(int *)data != 0)
   1277 			cs->sc_flags |= CCDF_KLABEL;
   1278 		else
   1279 			cs->sc_flags &= ~CCDF_KLABEL;
   1280 		break;
   1281 
   1282 	case DIOCWLABEL:
   1283 		if (*(int *)data != 0)
   1284 			cs->sc_flags |= CCDF_WLABEL;
   1285 		else
   1286 			cs->sc_flags &= ~CCDF_WLABEL;
   1287 		break;
   1288 
   1289 	case DIOCGDEFLABEL:
   1290 		ccdgetdefaultlabel(cs, (struct disklabel *)data);
   1291 		break;
   1292 
   1293 #ifdef __HAVE_OLD_DISKLABEL
   1294 	case ODIOCGDEFLABEL:
   1295 		ccdgetdefaultlabel(cs, &newlabel);
   1296 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1297 			return ENOTTY;
   1298 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
   1299 		break;
   1300 #endif
   1301 
   1302 	default:
   1303 		error = ENOTTY;
   1304 	}
   1305 
   1306  out:
   1307 	(void) lockmgr(&cs->sc_lock, LK_RELEASE, NULL);
   1308 	return (error);
   1309 }
   1310 
   1311 static int
   1312 ccdsize(dev_t dev)
   1313 {
   1314 	struct ccd_softc *cs;
   1315 	struct disklabel *lp;
   1316 	int part, unit, omask, size;
   1317 
   1318 	unit = ccdunit(dev);
   1319 	if (unit >= numccd)
   1320 		return (-1);
   1321 	cs = &ccd_softc[unit];
   1322 
   1323 	if ((cs->sc_flags & CCDF_INITED) == 0)
   1324 		return (-1);
   1325 
   1326 	part = DISKPART(dev);
   1327 	omask = cs->sc_dkdev.dk_openmask & (1 << part);
   1328 	lp = cs->sc_dkdev.dk_label;
   1329 
   1330 	if (omask == 0 && ccdopen(dev, 0, S_IFBLK, curlwp))
   1331 		return (-1);
   1332 
   1333 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
   1334 		size = -1;
   1335 	else
   1336 		size = lp->d_partitions[part].p_size *
   1337 		    (lp->d_secsize / DEV_BSIZE);
   1338 
   1339 	if (omask == 0 && ccdclose(dev, 0, S_IFBLK, curlwp))
   1340 		return (-1);
   1341 
   1342 	return (size);
   1343 }
   1344 
   1345 static int
   1346 ccddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
   1347 {
   1348 
   1349 	/* Not implemented. */
   1350 	return ENXIO;
   1351 }
   1352 
   1353 /*
   1354  * Lookup the provided name in the filesystem.  If the file exists,
   1355  * is a valid block device, and isn't being used by anyone else,
   1356  * set *vpp to the file's vnode.
   1357  */
   1358 static int
   1359 ccdlookup(char *path, struct lwp *l, struct vnode **vpp /* result */)
   1360 {
   1361 	struct nameidata nd;
   1362 	struct vnode *vp;
   1363 	struct vattr va;
   1364 	struct proc *p;
   1365 	int error;
   1366 
   1367 	p = l->l_proc;
   1368 
   1369 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, l);
   1370 	if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
   1371 #ifdef DEBUG
   1372 		if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
   1373 			printf("ccdlookup: vn_open error = %d\n", error);
   1374 #endif
   1375 		return (error);
   1376 	}
   1377 	vp = nd.ni_vp;
   1378 
   1379 	if (vp->v_usecount > 1) {
   1380 		VOP_UNLOCK(vp, 0);
   1381 		(void)vn_close(vp, FREAD|FWRITE, p->p_cred, l);
   1382 		return (EBUSY);
   1383 	}
   1384 
   1385 	if ((error = VOP_GETATTR(vp, &va, p->p_cred, l)) != 0) {
   1386 #ifdef DEBUG
   1387 		if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
   1388 			printf("ccdlookup: getattr error = %d\n", error);
   1389 #endif
   1390 		VOP_UNLOCK(vp, 0);
   1391 		(void)vn_close(vp, FREAD|FWRITE, p->p_cred, l);
   1392 		return (error);
   1393 	}
   1394 
   1395 	/* XXX: eventually we should handle VREG, too. */
   1396 	if (va.va_type != VBLK) {
   1397 		VOP_UNLOCK(vp, 0);
   1398 		(void)vn_close(vp, FREAD|FWRITE, p->p_cred, l);
   1399 		return (ENOTBLK);
   1400 	}
   1401 
   1402 #ifdef DEBUG
   1403 	if (ccddebug & CCDB_VNODE)
   1404 		vprint("ccdlookup: vnode info", vp);
   1405 #endif
   1406 
   1407 	VOP_UNLOCK(vp, 0);
   1408 	*vpp = vp;
   1409 	return (0);
   1410 }
   1411 
   1412 static void
   1413 ccdgetdefaultlabel(struct ccd_softc *cs, struct disklabel *lp)
   1414 {
   1415 	struct ccdgeom *ccg = &cs->sc_geom;
   1416 
   1417 	memset(lp, 0, sizeof(*lp));
   1418 
   1419 	lp->d_secperunit = cs->sc_size;
   1420 	lp->d_secsize = ccg->ccg_secsize;
   1421 	lp->d_nsectors = ccg->ccg_nsectors;
   1422 	lp->d_ntracks = ccg->ccg_ntracks;
   1423 	lp->d_ncylinders = ccg->ccg_ncylinders;
   1424 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1425 
   1426 	strncpy(lp->d_typename, "ccd", sizeof(lp->d_typename));
   1427 	lp->d_type = DTYPE_CCD;
   1428 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1429 	lp->d_rpm = 3600;
   1430 	lp->d_interleave = 1;
   1431 	lp->d_flags = 0;
   1432 
   1433 	lp->d_partitions[RAW_PART].p_offset = 0;
   1434 	lp->d_partitions[RAW_PART].p_size = cs->sc_size;
   1435 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1436 	lp->d_npartitions = RAW_PART + 1;
   1437 
   1438 	lp->d_magic = DISKMAGIC;
   1439 	lp->d_magic2 = DISKMAGIC;
   1440 	lp->d_checksum = dkcksum(cs->sc_dkdev.dk_label);
   1441 }
   1442 
   1443 /*
   1444  * Read the disklabel from the ccd.  If one is not present, fake one
   1445  * up.
   1446  */
   1447 static void
   1448 ccdgetdisklabel(dev_t dev)
   1449 {
   1450 	int unit = ccdunit(dev);
   1451 	struct ccd_softc *cs = &ccd_softc[unit];
   1452 	const char *errstring;
   1453 	struct disklabel *lp = cs->sc_dkdev.dk_label;
   1454 	struct cpu_disklabel *clp = cs->sc_dkdev.dk_cpulabel;
   1455 
   1456 	memset(clp, 0, sizeof(*clp));
   1457 
   1458 	ccdgetdefaultlabel(cs, lp);
   1459 
   1460 	/*
   1461 	 * Call the generic disklabel extraction routine.
   1462 	 */
   1463 	if ((cs->sc_flags & CCDF_NOLABEL) != 0)
   1464 		errstring = "CCDF_NOLABEL set; ignoring on-disk label";
   1465 	else
   1466 		errstring = readdisklabel(CCDLABELDEV(dev), ccdstrategy,
   1467 		    cs->sc_dkdev.dk_label, cs->sc_dkdev.dk_cpulabel);
   1468 	if (errstring)
   1469 		ccdmakedisklabel(cs);
   1470 	else {
   1471 		int i;
   1472 		struct partition *pp;
   1473 
   1474 		/*
   1475 		 * Sanity check whether the found disklabel is valid.
   1476 		 *
   1477 		 * This is necessary since total size of ccd may vary
   1478 		 * when an interleave is changed even though exactly
   1479 		 * same componets are used, and old disklabel may used
   1480 		 * if that is found.
   1481 		 */
   1482 		if (lp->d_secperunit != cs->sc_size)
   1483 			printf("WARNING: %s: "
   1484 			    "total sector size in disklabel (%d) != "
   1485 			    "the size of ccd (%lu)\n", cs->sc_xname,
   1486 			    lp->d_secperunit, (u_long)cs->sc_size);
   1487 		for (i = 0; i < lp->d_npartitions; i++) {
   1488 			pp = &lp->d_partitions[i];
   1489 			if (pp->p_offset + pp->p_size > cs->sc_size)
   1490 				printf("WARNING: %s: end of partition `%c' "
   1491 				    "exceeds the size of ccd (%lu)\n",
   1492 				    cs->sc_xname, 'a' + i, (u_long)cs->sc_size);
   1493 		}
   1494 	}
   1495 
   1496 #ifdef DEBUG
   1497 	/* It's actually extremely common to have unlabeled ccds. */
   1498 	if (ccddebug & CCDB_LABEL)
   1499 		if (errstring != NULL)
   1500 			printf("%s: %s\n", cs->sc_xname, errstring);
   1501 #endif
   1502 
   1503 	/* In-core label now valid. */
   1504 	cs->sc_flags |= CCDF_VLABEL;
   1505 }
   1506 
   1507 /*
   1508  * Take care of things one might want to take care of in the event
   1509  * that a disklabel isn't present.
   1510  */
   1511 static void
   1512 ccdmakedisklabel(struct ccd_softc *cs)
   1513 {
   1514 	struct disklabel *lp = cs->sc_dkdev.dk_label;
   1515 
   1516 	/*
   1517 	 * For historical reasons, if there's no disklabel present
   1518 	 * the raw partition must be marked FS_BSDFFS.
   1519 	 */
   1520 	lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
   1521 
   1522 	strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
   1523 
   1524 	lp->d_checksum = dkcksum(lp);
   1525 }
   1526 
   1527 #ifdef DEBUG
   1528 static void
   1529 printiinfo(struct ccdiinfo *ii)
   1530 {
   1531 	int ix, i;
   1532 
   1533 	for (ix = 0; ii->ii_ndisk; ix++, ii++) {
   1534 		printf(" itab[%d]: #dk %d sblk %" PRId64 " soff %" PRId64,
   1535 		    ix, ii->ii_ndisk, ii->ii_startblk, ii->ii_startoff);
   1536 		for (i = 0; i < ii->ii_ndisk; i++)
   1537 			printf(" %d", ii->ii_index[i]);
   1538 		printf("\n");
   1539 	}
   1540 }
   1541 #endif
   1542