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