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