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