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