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