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