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