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