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