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