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