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