Home | History | Annotate | Line # | Download | only in dev
ccd.c revision 1.171
      1 /*	$NetBSD: ccd.c,v 1.171 2017/04/05 18:34:56 jdolecek 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.171 2017/04/05 18:34:56 jdolecek Exp $");
     92 
     93 #if defined(_KERNEL_OPT)
     94 #include "opt_compat_netbsd.h"
     95 #endif
     96 
     97 #include <sys/param.h>
     98 #include <sys/systm.h>
     99 #include <sys/kernel.h>
    100 #include <sys/proc.h>
    101 #include <sys/errno.h>
    102 #include <sys/buf.h>
    103 #include <sys/kmem.h>
    104 #include <sys/pool.h>
    105 #include <sys/module.h>
    106 #include <sys/namei.h>
    107 #include <sys/stat.h>
    108 #include <sys/ioctl.h>
    109 #include <sys/disklabel.h>
    110 #include <sys/device.h>
    111 #include <sys/disk.h>
    112 #include <sys/syslog.h>
    113 #include <sys/fcntl.h>
    114 #include <sys/vnode.h>
    115 #include <sys/conf.h>
    116 #include <sys/mutex.h>
    117 #include <sys/queue.h>
    118 #include <sys/kauth.h>
    119 #include <sys/kthread.h>
    120 #include <sys/bufq.h>
    121 #include <sys/sysctl.h>
    122 
    123 #include <uvm/uvm_extern.h>
    124 
    125 #include <dev/ccdvar.h>
    126 #include <dev/dkvar.h>
    127 
    128 #include <miscfs/specfs/specdev.h> /* for v_rdev */
    129 
    130 #include "ioconf.h"
    131 
    132 #if defined(CCDDEBUG) && !defined(DEBUG)
    133 #define DEBUG
    134 #endif
    135 
    136 #ifdef DEBUG
    137 #define CCDB_FOLLOW	0x01
    138 #define CCDB_INIT	0x02
    139 #define CCDB_IO		0x04
    140 #define CCDB_LABEL	0x08
    141 #define CCDB_VNODE	0x10
    142 int ccddebug = 0x00;
    143 #endif
    144 
    145 #define	ccdunit(x)	DISKUNIT(x)
    146 
    147 struct ccdbuf {
    148 	struct buf	cb_buf;		/* new I/O buf */
    149 	struct buf	*cb_obp;	/* ptr. to original I/O buf */
    150 	struct ccd_softc *cb_sc;	/* pointer to ccd softc */
    151 	int		cb_comp;	/* target component */
    152 	SIMPLEQ_ENTRY(ccdbuf) cb_q;	/* fifo of component buffers */
    153 };
    154 
    155 /* component buffer pool */
    156 static pool_cache_t ccd_cache;
    157 
    158 #define	CCD_GETBUF()		pool_cache_get(ccd_cache, PR_WAITOK)
    159 #define	CCD_PUTBUF(cbp)		pool_cache_put(ccd_cache, cbp)
    160 
    161 #define CCDLABELDEV(dev)	\
    162 	(MAKEDISKDEV(major((dev)), ccdunit((dev)), RAW_PART))
    163 
    164 /* called by main() at boot time */
    165 void	ccddetach(void);
    166 
    167 /* called by biodone() at interrupt time */
    168 static void	ccdiodone(struct buf *);
    169 
    170 static void	ccdinterleave(struct ccd_softc *);
    171 static int	ccdinit(struct ccd_softc *, char **, struct vnode **,
    172 		    struct lwp *);
    173 static struct ccdbuf *ccdbuffer(struct ccd_softc *, struct buf *,
    174 		    daddr_t, void *, long);
    175 static void	ccdgetdefaultlabel(struct ccd_softc *, struct disklabel *);
    176 static void	ccdgetdisklabel(dev_t);
    177 static void	ccdmakedisklabel(struct ccd_softc *);
    178 static void	ccdstart(struct ccd_softc *);
    179 static void	ccdthread(void *);
    180 
    181 static dev_type_open(ccdopen);
    182 static dev_type_close(ccdclose);
    183 static dev_type_read(ccdread);
    184 static dev_type_write(ccdwrite);
    185 static dev_type_ioctl(ccdioctl);
    186 static dev_type_strategy(ccdstrategy);
    187 static dev_type_size(ccdsize);
    188 
    189 const struct bdevsw ccd_bdevsw = {
    190 	.d_open = ccdopen,
    191 	.d_close = ccdclose,
    192 	.d_strategy = ccdstrategy,
    193 	.d_ioctl = ccdioctl,
    194 	.d_dump = nodump,
    195 	.d_psize = ccdsize,
    196 	.d_discard = nodiscard,
    197 	.d_flag = D_DISK | D_MPSAFE
    198 };
    199 
    200 const struct cdevsw ccd_cdevsw = {
    201 	.d_open = ccdopen,
    202 	.d_close = ccdclose,
    203 	.d_read = ccdread,
    204 	.d_write = ccdwrite,
    205 	.d_ioctl = ccdioctl,
    206 	.d_stop = nostop,
    207 	.d_tty = notty,
    208 	.d_poll = nopoll,
    209 	.d_mmap = nommap,
    210 	.d_kqfilter = nokqfilter,
    211 	.d_discard = nodiscard,
    212 	.d_flag = D_DISK | D_MPSAFE
    213 };
    214 
    215 #ifdef DEBUG
    216 static	void printiinfo(struct ccdiinfo *);
    217 #endif
    218 
    219 static LIST_HEAD(, ccd_softc) ccds = LIST_HEAD_INITIALIZER(ccds);
    220 static kmutex_t ccd_lock;
    221 static size_t ccd_nactive = 0;
    222 
    223 #ifdef _MODULE
    224 static struct sysctllog *ccd_clog;
    225 #endif
    226 
    227 SYSCTL_SETUP_PROTO(sysctl_kern_ccd_setup);
    228 
    229 static struct ccd_softc *
    230 ccdcreate(int unit) {
    231 	struct ccd_softc *sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
    232 	if (sc == NULL) {
    233 #ifdef DIAGNOSTIC
    234 		printf("%s: out of memory\n", __func__);
    235 #endif
    236 		return NULL;
    237 	}
    238 	/* Initialize per-softc structures. */
    239 	snprintf(sc->sc_xname, sizeof(sc->sc_xname), "ccd%d", unit);
    240 	sc->sc_unit = unit;
    241 	mutex_init(&sc->sc_dvlock, MUTEX_DEFAULT, IPL_NONE);
    242 	sc->sc_iolock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
    243 	cv_init(&sc->sc_stop, "ccdstop");
    244 	cv_init(&sc->sc_push, "ccdthr");
    245 	disk_init(&sc->sc_dkdev, sc->sc_xname, NULL); /* XXX */
    246 	return sc;
    247 }
    248 
    249 static void
    250 ccddestroy(struct ccd_softc *sc) {
    251 	mutex_obj_free(sc->sc_iolock);
    252 	mutex_exit(&sc->sc_dvlock);
    253 	mutex_destroy(&sc->sc_dvlock);
    254 	cv_destroy(&sc->sc_stop);
    255 	cv_destroy(&sc->sc_push);
    256 	disk_destroy(&sc->sc_dkdev);
    257 	kmem_free(sc, sizeof(*sc));
    258 }
    259 
    260 static struct ccd_softc *
    261 ccdget(int unit, int make) {
    262 	struct ccd_softc *sc;
    263 	if (unit < 0) {
    264 #ifdef DIAGNOSTIC
    265 		panic("%s: unit %d!", __func__, unit);
    266 #endif
    267 		return NULL;
    268 	}
    269 	mutex_enter(&ccd_lock);
    270 	LIST_FOREACH(sc, &ccds, sc_link) {
    271 		if (sc->sc_unit == unit) {
    272 			mutex_exit(&ccd_lock);
    273 			return sc;
    274 		}
    275 	}
    276 	mutex_exit(&ccd_lock);
    277 	if (!make)
    278 		return NULL;
    279 	if ((sc = ccdcreate(unit)) == NULL)
    280 		return NULL;
    281 	mutex_enter(&ccd_lock);
    282 	LIST_INSERT_HEAD(&ccds, sc, sc_link);
    283 	ccd_nactive++;
    284 	mutex_exit(&ccd_lock);
    285 	return sc;
    286 }
    287 
    288 static void
    289 ccdput(struct ccd_softc *sc) {
    290 	mutex_enter(&ccd_lock);
    291 	LIST_REMOVE(sc, sc_link);
    292 	ccd_nactive--;
    293 	mutex_exit(&ccd_lock);
    294 	ccddestroy(sc);
    295 }
    296 
    297 /*
    298  * Called by main() during pseudo-device attachment.  All we need
    299  * to do is allocate enough space for devices to be configured later.
    300  */
    301 void
    302 ccdattach(int num)
    303 {
    304 	mutex_init(&ccd_lock, MUTEX_DEFAULT, IPL_NONE);
    305 
    306 	/* Initialize the component buffer pool. */
    307 	ccd_cache = pool_cache_init(sizeof(struct ccdbuf), 0,
    308 	    0, 0, "ccdbuf", NULL, IPL_BIO, NULL, NULL, NULL);
    309 }
    310 
    311 void
    312 ccddetach(void)
    313 {
    314 	pool_cache_destroy(ccd_cache);
    315 	mutex_destroy(&ccd_lock);
    316 }
    317 
    318 static int
    319 ccdinit(struct ccd_softc *cs, char **cpaths, struct vnode **vpp,
    320     struct lwp *l)
    321 {
    322 	struct ccdcinfo *ci = NULL;
    323 	int ix;
    324 	struct ccdgeom *ccg = &cs->sc_geom;
    325 	char *tmppath;
    326 	int error, path_alloced;
    327 	uint64_t psize, minsize;
    328 	unsigned secsize, maxsecsize;
    329 	struct disk_geom *dg;
    330 
    331 #ifdef DEBUG
    332 	if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    333 		printf("%s: ccdinit\n", cs->sc_xname);
    334 #endif
    335 
    336 	/* Allocate space for the component info. */
    337 	cs->sc_cinfo = kmem_alloc(cs->sc_nccdisks * sizeof(*cs->sc_cinfo),
    338 	    KM_SLEEP);
    339 	tmppath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
    340 
    341 	cs->sc_size = 0;
    342 
    343 	/*
    344 	 * Verify that each component piece exists and record
    345 	 * relevant information about it.
    346 	 */
    347 	maxsecsize = 0;
    348 	minsize = 0;
    349 	for (ix = 0, path_alloced = 0; ix < cs->sc_nccdisks; ix++) {
    350 		ci = &cs->sc_cinfo[ix];
    351 		ci->ci_vp = vpp[ix];
    352 
    353 		/*
    354 		 * Copy in the pathname of the component.
    355 		 */
    356 		memset(tmppath, 0, MAXPATHLEN);	/* sanity */
    357 		error = copyinstr(cpaths[ix], tmppath,
    358 		    MAXPATHLEN, &ci->ci_pathlen);
    359 		if (ci->ci_pathlen == 0)
    360 			error = EINVAL;
    361 		if (error) {
    362 #ifdef DEBUG
    363 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    364 				printf("%s: can't copy path, error = %d\n",
    365 				    cs->sc_xname, error);
    366 #endif
    367 			goto out;
    368 		}
    369 		ci->ci_path = kmem_alloc(ci->ci_pathlen, KM_SLEEP);
    370 		memcpy(ci->ci_path, tmppath, ci->ci_pathlen);
    371 		path_alloced++;
    372 
    373 		/*
    374 		 * XXX: Cache the component's dev_t.
    375 		 */
    376 		ci->ci_dev = vpp[ix]->v_rdev;
    377 
    378 		/*
    379 		 * Get partition information for the component.
    380 		 */
    381 		error = getdisksize(vpp[ix], &psize, &secsize);
    382 		if (error) {
    383 #ifdef DEBUG
    384 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    385 				 printf("%s: %s: disksize failed, error = %d\n",
    386 				     cs->sc_xname, ci->ci_path, error);
    387 #endif
    388 			goto out;
    389 		}
    390 
    391 		/*
    392 		 * Calculate the size, truncating to an interleave
    393 		 * boundary if necessary.
    394 		 */
    395 		maxsecsize = secsize > maxsecsize ? secsize : maxsecsize;
    396 		if (cs->sc_ileave > 1)
    397 			psize -= psize % cs->sc_ileave;
    398 
    399 		if (psize == 0) {
    400 #ifdef DEBUG
    401 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    402 				printf("%s: %s: size == 0\n",
    403 				    cs->sc_xname, ci->ci_path);
    404 #endif
    405 			error = ENODEV;
    406 			goto out;
    407 		}
    408 
    409 		if (minsize == 0 || psize < minsize)
    410 			minsize = psize;
    411 		ci->ci_size = psize;
    412 		cs->sc_size += psize;
    413 	}
    414 
    415 	/*
    416 	 * Don't allow the interleave to be smaller than
    417 	 * the biggest component sector.
    418 	 */
    419 	if ((cs->sc_ileave > 0) &&
    420 	    (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) {
    421 #ifdef DEBUG
    422 		if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    423 			printf("%s: interleave must be at least %d\n",
    424 			    cs->sc_xname, (maxsecsize / DEV_BSIZE));
    425 #endif
    426 		error = EINVAL;
    427 		goto out;
    428 	}
    429 
    430 	/*
    431 	 * If uniform interleave is desired set all sizes to that of
    432 	 * the smallest component.
    433 	 */
    434 	if (cs->sc_flags & CCDF_UNIFORM) {
    435 		for (ci = cs->sc_cinfo;
    436 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    437 			ci->ci_size = minsize;
    438 
    439 		cs->sc_size = cs->sc_nccdisks * minsize;
    440 	}
    441 
    442 	/*
    443 	 * Construct the interleave table.
    444 	 */
    445 	ccdinterleave(cs);
    446 
    447 	/*
    448 	 * Create pseudo-geometry based on 1MB cylinders.  It's
    449 	 * pretty close.
    450 	 */
    451 	ccg->ccg_secsize = DEV_BSIZE;
    452 	ccg->ccg_ntracks = 1;
    453 	ccg->ccg_nsectors = 1024 * (1024 / ccg->ccg_secsize);
    454 	ccg->ccg_ncylinders = cs->sc_size / ccg->ccg_nsectors;
    455 
    456         dg = &cs->sc_dkdev.dk_geom;
    457         memset(dg, 0, sizeof(*dg));
    458 	dg->dg_secperunit = cs->sc_size;
    459 	dg->dg_secsize = ccg->ccg_secsize;
    460 	dg->dg_nsectors = ccg->ccg_nsectors;
    461 	dg->dg_ntracks = ccg->ccg_ntracks;
    462 	dg->dg_ncylinders = ccg->ccg_ncylinders;
    463 
    464 	if (cs->sc_ileave > 0)
    465 	        aprint_normal("%s: Interleaving %d component%s "
    466 	            "(%d block interleave)\n", cs->sc_xname,
    467         	    cs->sc_nccdisks, (cs->sc_nccdisks != 0 ? "s" : ""),
    468         	    cs->sc_ileave);
    469 	else
    470 	        aprint_normal("%s: Concatenating %d component%s\n",
    471 	            cs->sc_xname,
    472         	    cs->sc_nccdisks, (cs->sc_nccdisks != 0 ? "s" : ""));
    473 	for (ix = 0; ix < cs->sc_nccdisks; ix++) {
    474 		ci = &cs->sc_cinfo[ix];
    475 		aprint_normal("%s: %s (%ju blocks)\n", cs->sc_xname,
    476 		    ci->ci_path, (uintmax_t)ci->ci_size);
    477 	}
    478 	aprint_normal("%s: total %ju blocks\n", cs->sc_xname, cs->sc_size);
    479 
    480 	/*
    481 	 * Create thread to handle deferred I/O.
    482 	 */
    483 	cs->sc_zap = false;
    484 	error = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL, ccdthread,
    485 	    cs, &cs->sc_thread, "%s", cs->sc_xname);
    486 	if (error) {
    487 		printf("ccdinit: can't create thread: %d\n", error);
    488 		goto out;
    489 	}
    490 
    491 	/*
    492 	 * Only now that everything is set up can we enable the device.
    493 	 */
    494 	mutex_enter(cs->sc_iolock);
    495 	cs->sc_flags |= CCDF_INITED;
    496 	mutex_exit(cs->sc_iolock);
    497 	kmem_free(tmppath, MAXPATHLEN);
    498 	return (0);
    499 
    500  out:
    501 	for (ix = 0; ix < path_alloced; ix++) {
    502 		kmem_free(cs->sc_cinfo[ix].ci_path,
    503 		    cs->sc_cinfo[ix].ci_pathlen);
    504 	}
    505 	kmem_free(cs->sc_cinfo, cs->sc_nccdisks * sizeof(struct ccdcinfo));
    506 	kmem_free(tmppath, MAXPATHLEN);
    507 	return (error);
    508 }
    509 
    510 static void
    511 ccdinterleave(struct ccd_softc *cs)
    512 {
    513 	struct ccdcinfo *ci, *smallci;
    514 	struct ccdiinfo *ii;
    515 	daddr_t bn, lbn;
    516 	int ix;
    517 	u_long size;
    518 
    519 #ifdef DEBUG
    520 	if (ccddebug & CCDB_INIT)
    521 		printf("ccdinterleave(%p): ileave %d\n", cs, cs->sc_ileave);
    522 #endif
    523 	/*
    524 	 * Allocate an interleave table.
    525 	 * Chances are this is too big, but we don't care.
    526 	 */
    527 	size = (cs->sc_nccdisks + 1) * sizeof(struct ccdiinfo);
    528 	cs->sc_itable = kmem_zalloc(size, KM_SLEEP);
    529 
    530 	/*
    531 	 * Trivial case: no interleave (actually interleave of disk size).
    532 	 * Each table entry represents a single component in its entirety.
    533 	 */
    534 	if (cs->sc_ileave == 0) {
    535 		bn = 0;
    536 		ii = cs->sc_itable;
    537 
    538 		for (ix = 0; ix < cs->sc_nccdisks; ix++) {
    539 			/* Allocate space for ii_index. */
    540 			ii->ii_indexsz = sizeof(int);
    541 			ii->ii_index = kmem_alloc(ii->ii_indexsz, KM_SLEEP);
    542 			ii->ii_ndisk = 1;
    543 			ii->ii_startblk = bn;
    544 			ii->ii_startoff = 0;
    545 			ii->ii_index[0] = ix;
    546 			bn += cs->sc_cinfo[ix].ci_size;
    547 			ii++;
    548 		}
    549 		ii->ii_ndisk = 0;
    550 #ifdef DEBUG
    551 		if (ccddebug & CCDB_INIT)
    552 			printiinfo(cs->sc_itable);
    553 #endif
    554 		return;
    555 	}
    556 
    557 	/*
    558 	 * The following isn't fast or pretty; it doesn't have to be.
    559 	 */
    560 	size = 0;
    561 	bn = lbn = 0;
    562 	for (ii = cs->sc_itable; ; ii++) {
    563 		/* Allocate space for ii_index. */
    564 		ii->ii_indexsz = sizeof(int) * cs->sc_nccdisks;
    565 		ii->ii_index = kmem_alloc(ii->ii_indexsz, KM_SLEEP);
    566 
    567 		/*
    568 		 * Locate the smallest of the remaining components
    569 		 */
    570 		smallci = NULL;
    571 		for (ci = cs->sc_cinfo;
    572 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    573 			if (ci->ci_size > size &&
    574 			    (smallci == NULL ||
    575 			     ci->ci_size < smallci->ci_size))
    576 				smallci = ci;
    577 
    578 		/*
    579 		 * Nobody left, all done
    580 		 */
    581 		if (smallci == NULL) {
    582 			ii->ii_ndisk = 0;
    583 			break;
    584 		}
    585 
    586 		/*
    587 		 * Record starting logical block and component offset
    588 		 */
    589 		ii->ii_startblk = bn / cs->sc_ileave;
    590 		ii->ii_startoff = lbn;
    591 
    592 		/*
    593 		 * Determine how many disks take part in this interleave
    594 		 * and record their indices.
    595 		 */
    596 		ix = 0;
    597 		for (ci = cs->sc_cinfo;
    598 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    599 			if (ci->ci_size >= smallci->ci_size)
    600 				ii->ii_index[ix++] = ci - cs->sc_cinfo;
    601 		ii->ii_ndisk = ix;
    602 		bn += ix * (smallci->ci_size - size);
    603 		lbn = smallci->ci_size / cs->sc_ileave;
    604 		size = smallci->ci_size;
    605 	}
    606 #ifdef DEBUG
    607 	if (ccddebug & CCDB_INIT)
    608 		printiinfo(cs->sc_itable);
    609 #endif
    610 }
    611 
    612 /* ARGSUSED */
    613 static int
    614 ccdopen(dev_t dev, int flags, int fmt, struct lwp *l)
    615 {
    616 	int unit = ccdunit(dev);
    617 	struct ccd_softc *cs;
    618 	struct disklabel *lp;
    619 	int error = 0, part, pmask;
    620 
    621 #ifdef DEBUG
    622 	if (ccddebug & CCDB_FOLLOW)
    623 		printf("ccdopen(0x%"PRIx64", 0x%x)\n", dev, flags);
    624 #endif
    625 	if ((cs = ccdget(unit, 1)) == NULL)
    626 		return ENXIO;
    627 
    628 	mutex_enter(&cs->sc_dvlock);
    629 
    630 	lp = cs->sc_dkdev.dk_label;
    631 
    632 	part = DISKPART(dev);
    633 	pmask = (1 << part);
    634 
    635 	/*
    636 	 * If we're initialized, check to see if there are any other
    637 	 * open partitions.  If not, then it's safe to update
    638 	 * the in-core disklabel.  Only read the disklabel if it is
    639 	 * not already valid.
    640 	 */
    641 	if ((cs->sc_flags & (CCDF_INITED|CCDF_VLABEL)) == CCDF_INITED &&
    642 	    cs->sc_dkdev.dk_openmask == 0)
    643 		ccdgetdisklabel(dev);
    644 
    645 	/* Check that the partition exists. */
    646 	if (part != RAW_PART) {
    647 		if (((cs->sc_flags & CCDF_INITED) == 0) ||
    648 		    ((part >= lp->d_npartitions) ||
    649 		     (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
    650 			error = ENXIO;
    651 			goto done;
    652 		}
    653 	}
    654 
    655 	/* Prevent our unit from being unconfigured while open. */
    656 	switch (fmt) {
    657 	case S_IFCHR:
    658 		cs->sc_dkdev.dk_copenmask |= pmask;
    659 		break;
    660 
    661 	case S_IFBLK:
    662 		cs->sc_dkdev.dk_bopenmask |= pmask;
    663 		break;
    664 	}
    665 	cs->sc_dkdev.dk_openmask =
    666 	    cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
    667 
    668  done:
    669 	mutex_exit(&cs->sc_dvlock);
    670 	return (error);
    671 }
    672 
    673 /* ARGSUSED */
    674 static int
    675 ccdclose(dev_t dev, int flags, int fmt, struct lwp *l)
    676 {
    677 	int unit = ccdunit(dev);
    678 	struct ccd_softc *cs;
    679 	int part;
    680 
    681 #ifdef DEBUG
    682 	if (ccddebug & CCDB_FOLLOW)
    683 		printf("ccdclose(0x%"PRIx64", 0x%x)\n", dev, flags);
    684 #endif
    685 
    686 	if ((cs = ccdget(unit, 0)) == NULL)
    687 		return ENXIO;
    688 
    689 	mutex_enter(&cs->sc_dvlock);
    690 
    691 	part = DISKPART(dev);
    692 
    693 	/* ...that much closer to allowing unconfiguration... */
    694 	switch (fmt) {
    695 	case S_IFCHR:
    696 		cs->sc_dkdev.dk_copenmask &= ~(1 << part);
    697 		break;
    698 
    699 	case S_IFBLK:
    700 		cs->sc_dkdev.dk_bopenmask &= ~(1 << part);
    701 		break;
    702 	}
    703 	cs->sc_dkdev.dk_openmask =
    704 	    cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
    705 
    706 	if (cs->sc_dkdev.dk_openmask == 0) {
    707 		if ((cs->sc_flags & CCDF_KLABEL) == 0)
    708 			cs->sc_flags &= ~CCDF_VLABEL;
    709 	}
    710 
    711 	mutex_exit(&cs->sc_dvlock);
    712 	return (0);
    713 }
    714 
    715 static bool
    716 ccdbackoff(struct ccd_softc *cs)
    717 {
    718 
    719 	/* XXX Arbitrary, should be a uvm call. */
    720 	return uvmexp.free < (uvmexp.freemin >> 1) &&
    721 	    disk_isbusy(&cs->sc_dkdev);
    722 }
    723 
    724 static void
    725 ccdthread(void *cookie)
    726 {
    727 	struct ccd_softc *cs;
    728 
    729 	cs = cookie;
    730 
    731 #ifdef DEBUG
    732  	if (ccddebug & CCDB_FOLLOW)
    733  		printf("ccdthread: hello\n");
    734 #endif
    735 
    736 	mutex_enter(cs->sc_iolock);
    737 	while (__predict_true(!cs->sc_zap)) {
    738 		if (bufq_peek(cs->sc_bufq) == NULL) {
    739 			/* Nothing to do. */
    740 			cv_wait(&cs->sc_push, cs->sc_iolock);
    741 			continue;
    742 		}
    743 		if (ccdbackoff(cs)) {
    744 			/* Wait for memory to become available. */
    745 			(void)cv_timedwait(&cs->sc_push, cs->sc_iolock, 1);
    746 			continue;
    747 		}
    748 #ifdef DEBUG
    749  		if (ccddebug & CCDB_FOLLOW)
    750  			printf("ccdthread: dispatching I/O\n");
    751 #endif
    752 		ccdstart(cs);
    753 		mutex_enter(cs->sc_iolock);
    754 	}
    755 	cs->sc_thread = NULL;
    756 	mutex_exit(cs->sc_iolock);
    757 #ifdef DEBUG
    758  	if (ccddebug & CCDB_FOLLOW)
    759  		printf("ccdthread: goodbye\n");
    760 #endif
    761 	kthread_exit(0);
    762 }
    763 
    764 static void
    765 ccdstrategy(struct buf *bp)
    766 {
    767 	int unit = ccdunit(bp->b_dev);
    768 	struct ccd_softc *cs;
    769 	if ((cs = ccdget(unit, 0)) == NULL)
    770 		return;
    771 
    772 	/* Must be open or reading label. */
    773 	KASSERT(cs->sc_dkdev.dk_openmask != 0 ||
    774 	    (cs->sc_flags & CCDF_RLABEL) != 0);
    775 
    776 	mutex_enter(cs->sc_iolock);
    777 	/* Synchronize with device init/uninit. */
    778 	if (__predict_false((cs->sc_flags & CCDF_INITED) == 0)) {
    779 		mutex_exit(cs->sc_iolock);
    780 #ifdef DEBUG
    781  		if (ccddebug & CCDB_FOLLOW)
    782  			printf("ccdstrategy: unit %d: not inited\n", unit);
    783 #endif
    784  		bp->b_error = ENXIO;
    785  		bp->b_resid = bp->b_bcount;
    786  		biodone(bp);
    787 		return;
    788 	}
    789 
    790 	/* Defer to thread if system is low on memory. */
    791 	bufq_put(cs->sc_bufq, bp);
    792 	if (__predict_false(ccdbackoff(cs))) {
    793 		mutex_exit(cs->sc_iolock);
    794 #ifdef DEBUG
    795  		if (ccddebug & CCDB_FOLLOW)
    796  			printf("ccdstrategy: holding off on I/O\n");
    797 #endif
    798 		return;
    799 	}
    800 	ccdstart(cs);
    801 }
    802 
    803 static void
    804 ccdstart(struct ccd_softc *cs)
    805 {
    806 	daddr_t blkno;
    807 	int wlabel;
    808 	struct disklabel *lp;
    809 	long bcount, rcount;
    810 	struct ccdbuf *cbp;
    811 	char *addr;
    812 	daddr_t bn;
    813 	vnode_t *vp;
    814 	buf_t *bp;
    815 
    816 	KASSERT(mutex_owned(cs->sc_iolock));
    817 
    818 	bp = bufq_get(cs->sc_bufq);
    819 	KASSERT(bp != NULL);
    820 
    821 	disk_busy(&cs->sc_dkdev);
    822 
    823 #ifdef DEBUG
    824 	if (ccddebug & CCDB_FOLLOW)
    825 		printf("ccdstart(%s, %p)\n", cs->sc_xname, bp);
    826 #endif
    827 
    828 	/* If it's a nil transfer, wake up the top half now. */
    829 	if (bp->b_bcount == 0)
    830 		goto done;
    831 
    832 	lp = cs->sc_dkdev.dk_label;
    833 
    834 	/*
    835 	 * Do bounds checking and adjust transfer.  If there's an
    836 	 * error, the bounds check will flag that for us.  Convert
    837 	 * the partition relative block number to an absolute.
    838 	 */
    839 	blkno = bp->b_blkno;
    840 	wlabel = cs->sc_flags & (CCDF_WLABEL|CCDF_LABELLING);
    841 	if (DISKPART(bp->b_dev) != RAW_PART) {
    842 		if (bounds_check_with_label(&cs->sc_dkdev, bp, wlabel) <= 0)
    843 			goto done;
    844 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
    845 	}
    846 	mutex_exit(cs->sc_iolock);
    847 	bp->b_rawblkno = blkno;
    848 
    849 	/* Allocate the component buffers and start I/O! */
    850 	bp->b_resid = bp->b_bcount;
    851 	bn = bp->b_rawblkno;
    852 	addr = bp->b_data;
    853 	for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
    854 		cbp = ccdbuffer(cs, bp, bn, addr, bcount);
    855 		rcount = cbp->cb_buf.b_bcount;
    856 		bn += btodb(rcount);
    857 		addr += rcount;
    858 		vp = cbp->cb_buf.b_vp;
    859 		if ((cbp->cb_buf.b_flags & B_READ) == 0) {
    860 			mutex_enter(vp->v_interlock);
    861 			vp->v_numoutput++;
    862 			mutex_exit(vp->v_interlock);
    863 		}
    864 		(void)VOP_STRATEGY(vp, &cbp->cb_buf);
    865 	}
    866 	return;
    867 
    868  done:
    869 	disk_unbusy(&cs->sc_dkdev, 0, 0);
    870 	cv_broadcast(&cs->sc_stop);
    871 	cv_broadcast(&cs->sc_push);
    872 	mutex_exit(cs->sc_iolock);
    873 	bp->b_resid = bp->b_bcount;
    874 	biodone(bp);
    875 }
    876 
    877 /*
    878  * Build a component buffer header.
    879  */
    880 static struct ccdbuf *
    881 ccdbuffer(struct ccd_softc *cs, struct buf *bp, daddr_t bn, void *addr,
    882     long bcount)
    883 {
    884 	struct ccdcinfo *ci;
    885 	struct ccdbuf *cbp;
    886 	daddr_t cbn, cboff;
    887 	u_int64_t cbc;
    888 	int ccdisk;
    889 
    890 #ifdef DEBUG
    891 	if (ccddebug & CCDB_IO)
    892 		printf("ccdbuffer(%p, %p, %" PRId64 ", %p, %ld)\n",
    893 		       cs, bp, bn, addr, bcount);
    894 #endif
    895 	/*
    896 	 * Determine which component bn falls in.
    897 	 */
    898 	cbn = bn;
    899 	cboff = 0;
    900 
    901 	/*
    902 	 * Serially concatenated
    903 	 */
    904 	if (cs->sc_ileave == 0) {
    905 		daddr_t sblk;
    906 
    907 		sblk = 0;
    908 		for (ccdisk = 0, ci = &cs->sc_cinfo[ccdisk];
    909 		    cbn >= sblk + ci->ci_size;
    910 		    ccdisk++, ci = &cs->sc_cinfo[ccdisk])
    911 			sblk += ci->ci_size;
    912 		cbn -= sblk;
    913 	}
    914 	/*
    915 	 * Interleaved
    916 	 */
    917 	else {
    918 		struct ccdiinfo *ii;
    919 		int off;
    920 
    921 		cboff = cbn % cs->sc_ileave;
    922 		cbn /= cs->sc_ileave;
    923 		for (ii = cs->sc_itable; ii->ii_ndisk; ii++)
    924 			if (ii->ii_startblk > cbn)
    925 				break;
    926 		ii--;
    927 		off = cbn - ii->ii_startblk;
    928 		if (ii->ii_ndisk == 1) {
    929 			ccdisk = ii->ii_index[0];
    930 			cbn = ii->ii_startoff + off;
    931 		} else {
    932 			ccdisk = ii->ii_index[off % ii->ii_ndisk];
    933 			cbn = ii->ii_startoff + off / ii->ii_ndisk;
    934 		}
    935 		cbn *= cs->sc_ileave;
    936 		ci = &cs->sc_cinfo[ccdisk];
    937 	}
    938 
    939 	/*
    940 	 * Fill in the component buf structure.
    941 	 */
    942 	cbp = CCD_GETBUF();
    943 	KASSERT(cbp != NULL);
    944 	buf_init(&cbp->cb_buf);
    945 	cbp->cb_buf.b_flags = bp->b_flags;
    946 	cbp->cb_buf.b_oflags = bp->b_oflags;
    947 	cbp->cb_buf.b_cflags = bp->b_cflags;
    948 	cbp->cb_buf.b_iodone = ccdiodone;
    949 	cbp->cb_buf.b_proc = bp->b_proc;
    950 	cbp->cb_buf.b_dev = ci->ci_dev;
    951 	cbp->cb_buf.b_blkno = cbn + cboff;
    952 	cbp->cb_buf.b_data = addr;
    953 	cbp->cb_buf.b_vp = ci->ci_vp;
    954 	cbp->cb_buf.b_objlock = ci->ci_vp->v_interlock;
    955 	if (cs->sc_ileave == 0)
    956 		cbc = dbtob((u_int64_t)(ci->ci_size - cbn));
    957 	else
    958 		cbc = dbtob((u_int64_t)(cs->sc_ileave - cboff));
    959 	cbp->cb_buf.b_bcount = cbc < bcount ? cbc : bcount;
    960 
    961 	/*
    962 	 * context for ccdiodone
    963 	 */
    964 	cbp->cb_obp = bp;
    965 	cbp->cb_sc = cs;
    966 	cbp->cb_comp = ccdisk;
    967 
    968 	BIO_COPYPRIO(&cbp->cb_buf, bp);
    969 
    970 #ifdef DEBUG
    971 	if (ccddebug & CCDB_IO)
    972 		printf(" dev 0x%"PRIx64"(u%lu): cbp %p bn %" PRId64 " addr %p"
    973 		       " bcnt %d\n",
    974 		    ci->ci_dev, (unsigned long) (ci-cs->sc_cinfo), cbp,
    975 		    cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
    976 		    cbp->cb_buf.b_bcount);
    977 #endif
    978 
    979 	return (cbp);
    980 }
    981 
    982 /*
    983  * Called at interrupt time.
    984  * Mark the component as done and if all components are done,
    985  * take a ccd interrupt.
    986  */
    987 static void
    988 ccdiodone(struct buf *vbp)
    989 {
    990 	struct ccdbuf *cbp = (struct ccdbuf *) vbp;
    991 	struct buf *bp = cbp->cb_obp;
    992 	struct ccd_softc *cs = cbp->cb_sc;
    993 	int count;
    994 
    995 #ifdef DEBUG
    996 	if (ccddebug & CCDB_FOLLOW)
    997 		printf("ccdiodone(%p)\n", cbp);
    998 	if (ccddebug & CCDB_IO) {
    999 		printf("ccdiodone: bp %p bcount %d resid %d\n",
   1000 		       bp, bp->b_bcount, bp->b_resid);
   1001 		printf(" dev 0x%"PRIx64"(u%d), cbp %p bn %" PRId64 " addr %p"
   1002 		       " bcnt %d\n",
   1003 		       cbp->cb_buf.b_dev, cbp->cb_comp, cbp,
   1004 		       cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
   1005 		       cbp->cb_buf.b_bcount);
   1006 	}
   1007 #endif
   1008 
   1009 	if (cbp->cb_buf.b_error != 0) {
   1010 		bp->b_error = cbp->cb_buf.b_error;
   1011 		printf("%s: error %d on component %d\n",
   1012 		       cs->sc_xname, bp->b_error, cbp->cb_comp);
   1013 	}
   1014 	count = cbp->cb_buf.b_bcount;
   1015 	buf_destroy(&cbp->cb_buf);
   1016 	CCD_PUTBUF(cbp);
   1017 
   1018 	/*
   1019 	 * If all done, "interrupt".
   1020 	 */
   1021 	mutex_enter(cs->sc_iolock);
   1022 	bp->b_resid -= count;
   1023 	if (bp->b_resid < 0)
   1024 		panic("ccdiodone: count");
   1025 	if (bp->b_resid == 0) {
   1026 		/*
   1027 		 * Request is done for better or worse, wakeup the top half.
   1028 		 */
   1029 		if (bp->b_error != 0)
   1030 			bp->b_resid = bp->b_bcount;
   1031 		disk_unbusy(&cs->sc_dkdev, (bp->b_bcount - bp->b_resid),
   1032 		    (bp->b_flags & B_READ));
   1033 		if (!disk_isbusy(&cs->sc_dkdev)) {
   1034 			if (bufq_peek(cs->sc_bufq) != NULL) {
   1035 				cv_broadcast(&cs->sc_push);
   1036 			}
   1037 			cv_broadcast(&cs->sc_stop);
   1038 		}
   1039 		mutex_exit(cs->sc_iolock);
   1040 		biodone(bp);
   1041 	} else
   1042 		mutex_exit(cs->sc_iolock);
   1043 }
   1044 
   1045 /* ARGSUSED */
   1046 static int
   1047 ccdread(dev_t dev, struct uio *uio, int flags)
   1048 {
   1049 	int unit = ccdunit(dev);
   1050 	struct ccd_softc *cs;
   1051 
   1052 #ifdef DEBUG
   1053 	if (ccddebug & CCDB_FOLLOW)
   1054 		printf("ccdread(0x%"PRIx64", %p)\n", dev, uio);
   1055 #endif
   1056 	if ((cs = ccdget(unit, 0)) == NULL)
   1057 		return 0;
   1058 
   1059 	/* Unlocked advisory check, ccdstrategy check is synchronous. */
   1060 	if ((cs->sc_flags & CCDF_INITED) == 0)
   1061 		return (ENXIO);
   1062 
   1063 	return (physio(ccdstrategy, NULL, dev, B_READ, minphys, uio));
   1064 }
   1065 
   1066 /* ARGSUSED */
   1067 static int
   1068 ccdwrite(dev_t dev, struct uio *uio, int flags)
   1069 {
   1070 	int unit = ccdunit(dev);
   1071 	struct ccd_softc *cs;
   1072 
   1073 #ifdef DEBUG
   1074 	if (ccddebug & CCDB_FOLLOW)
   1075 		printf("ccdwrite(0x%"PRIx64", %p)\n", dev, uio);
   1076 #endif
   1077 	if ((cs = ccdget(unit, 0)) == NULL)
   1078 		return ENOENT;
   1079 
   1080 	/* Unlocked advisory check, ccdstrategy check is synchronous. */
   1081 	if ((cs->sc_flags & CCDF_INITED) == 0)
   1082 		return (ENXIO);
   1083 
   1084 	return (physio(ccdstrategy, NULL, dev, B_WRITE, minphys, uio));
   1085 }
   1086 
   1087 static int
   1088 ccdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1089 {
   1090 	int unit = ccdunit(dev);
   1091 	int i, j, lookedup = 0, error = 0;
   1092 	int part, pmask, make;
   1093 	struct ccd_softc *cs;
   1094 	struct ccd_ioctl *ccio = (struct ccd_ioctl *)data;
   1095 	kauth_cred_t uc;
   1096 	char **cpp;
   1097 	struct pathbuf *pb;
   1098 	struct vnode **vpp;
   1099 #ifdef __HAVE_OLD_DISKLABEL
   1100 	struct disklabel newlabel;
   1101 #endif
   1102 
   1103 	switch (cmd) {
   1104 #if defined(COMPAT_60) && !defined(_LP64)
   1105 	case CCDIOCSET_60:
   1106 #endif
   1107 	case CCDIOCSET:
   1108 		make = 1;
   1109 		break;
   1110 	default:
   1111 		make = 0;
   1112 		break;
   1113 	}
   1114 
   1115 	if ((cs = ccdget(unit, make)) == NULL)
   1116 		return ENOENT;
   1117 	uc = kauth_cred_get();
   1118 
   1119 /*
   1120  * Compat code must not be called if on a platform where
   1121  * sizeof (size_t) == sizeof (uint64_t) as CCDIOCSET will
   1122  * be the same as CCDIOCSET_60
   1123  */
   1124 #if defined(COMPAT_60) && !defined(_LP64)
   1125 	switch (cmd) {
   1126 	case CCDIOCSET_60: {
   1127 		struct ccd_ioctl ccionew;
   1128        		struct ccd_ioctl_60 *ccio60 =
   1129        		    (struct ccd_ioctl_60 *)data;
   1130 		ccionew.ccio_disks = ccio->ccio_disks;
   1131 		ccionew.ccio_ndisks = ccio->ccio_ndisks;
   1132 		ccionew.ccio_ileave = ccio->ccio_ileave;
   1133 		ccionew.ccio_flags = ccio->ccio_flags;
   1134 		ccionew.ccio_unit = ccio->ccio_unit;
   1135 		error = ccdioctl(dev, CCDIOCSET, &ccionew, flag, l);
   1136 		if (!error) {
   1137 			/* Copy data back, adjust types if necessary */
   1138 			ccio60->ccio_disks = ccionew.ccio_disks;
   1139 			ccio60->ccio_ndisks = ccionew.ccio_ndisks;
   1140 			ccio60->ccio_ileave = ccionew.ccio_ileave;
   1141 			ccio60->ccio_flags = ccionew.ccio_flags;
   1142 			ccio60->ccio_unit = ccionew.ccio_unit;
   1143 			ccio60->ccio_size = (size_t)ccionew.ccio_size;
   1144 		}
   1145 		return error;
   1146 		}
   1147 		break;
   1148 
   1149 	case CCDIOCCLR_60:
   1150 		/*
   1151 		 * ccio_size member not used, so existing struct OK
   1152 		 * drop through to existing non-compat version
   1153 		 */
   1154 		cmd = CCDIOCCLR;
   1155 		break;
   1156 	}
   1157 #endif /* COMPAT_60 && !_LP64*/
   1158 
   1159 	/* Must be open for writes for these commands... */
   1160 	switch (cmd) {
   1161 	case CCDIOCSET:
   1162 	case CCDIOCCLR:
   1163 	case DIOCSDINFO:
   1164 	case DIOCWDINFO:
   1165 	case DIOCCACHESYNC:
   1166 	case DIOCAWEDGE:
   1167 	case DIOCDWEDGE:
   1168 	case DIOCMWEDGES:
   1169 #ifdef __HAVE_OLD_DISKLABEL
   1170 	case ODIOCSDINFO:
   1171 	case ODIOCWDINFO:
   1172 #endif
   1173 	case DIOCKLABEL:
   1174 	case DIOCWLABEL:
   1175 		if ((flag & FWRITE) == 0)
   1176 			return (EBADF);
   1177 	}
   1178 
   1179 	mutex_enter(&cs->sc_dvlock);
   1180 
   1181 	/* Must be initialized for these... */
   1182 	switch (cmd) {
   1183 	case CCDIOCCLR:
   1184 	case DIOCGDINFO:
   1185 	case DIOCGSTRATEGY:
   1186 	case DIOCGCACHE:
   1187 	case DIOCCACHESYNC:
   1188 	case DIOCAWEDGE:
   1189 	case DIOCDWEDGE:
   1190 	case DIOCLWEDGES:
   1191 	case DIOCMWEDGES:
   1192 	case DIOCSDINFO:
   1193 	case DIOCWDINFO:
   1194 	case DIOCGPARTINFO:
   1195 	case DIOCWLABEL:
   1196 	case DIOCKLABEL:
   1197 	case DIOCGDEFLABEL:
   1198 #ifdef __HAVE_OLD_DISKLABEL
   1199 	case ODIOCGDINFO:
   1200 	case ODIOCSDINFO:
   1201 	case ODIOCWDINFO:
   1202 	case ODIOCGDEFLABEL:
   1203 #endif
   1204 		if ((cs->sc_flags & CCDF_INITED) == 0) {
   1205 			error = ENXIO;
   1206 			goto out;
   1207 		}
   1208 	}
   1209 
   1210 	error = disk_ioctl(&cs->sc_dkdev, dev, cmd, data, flag, l);
   1211 	if (error != EPASSTHROUGH)
   1212 		goto out;
   1213 
   1214 	error = 0;
   1215 	switch (cmd) {
   1216 	case CCDIOCSET:
   1217 		if (cs->sc_flags & CCDF_INITED) {
   1218 			error = EBUSY;
   1219 			goto out;
   1220 		}
   1221 
   1222 		/* Validate the flags. */
   1223 		if ((ccio->ccio_flags & CCDF_USERMASK) != ccio->ccio_flags) {
   1224 			error = EINVAL;
   1225 			goto out;
   1226 		}
   1227 
   1228 		if (ccio->ccio_ndisks > CCD_MAXNDISKS ||
   1229 		    ccio->ccio_ndisks == 0) {
   1230 			error = EINVAL;
   1231 			goto out;
   1232 		}
   1233 
   1234 		/* Fill in some important bits. */
   1235 		cs->sc_ileave = ccio->ccio_ileave;
   1236 		cs->sc_nccdisks = ccio->ccio_ndisks;
   1237 		cs->sc_flags = ccio->ccio_flags & CCDF_USERMASK;
   1238 
   1239 		/*
   1240 		 * Allocate space for and copy in the array of
   1241 		 * component pathnames and device numbers.
   1242 		 */
   1243 		cpp = kmem_alloc(ccio->ccio_ndisks * sizeof(*cpp), KM_SLEEP);
   1244 		vpp = kmem_alloc(ccio->ccio_ndisks * sizeof(*vpp), KM_SLEEP);
   1245 		error = copyin(ccio->ccio_disks, cpp,
   1246 		    ccio->ccio_ndisks * sizeof(*cpp));
   1247 		if (error) {
   1248 			kmem_free(vpp, ccio->ccio_ndisks * sizeof(*vpp));
   1249 			kmem_free(cpp, ccio->ccio_ndisks * sizeof(*cpp));
   1250 			goto out;
   1251 		}
   1252 
   1253 #ifdef DEBUG
   1254 		if (ccddebug & CCDB_INIT)
   1255 			for (i = 0; i < ccio->ccio_ndisks; ++i)
   1256 				printf("ccdioctl: component %d: %p\n",
   1257 				    i, cpp[i]);
   1258 #endif
   1259 
   1260 		for (i = 0; i < ccio->ccio_ndisks; ++i) {
   1261 #ifdef DEBUG
   1262 			if (ccddebug & CCDB_INIT)
   1263 				printf("ccdioctl: lookedup = %d\n", lookedup);
   1264 #endif
   1265 			error = pathbuf_copyin(cpp[i], &pb);
   1266 			if (error == 0) {
   1267 				error = dk_lookup(pb, l, &vpp[i]);
   1268 			}
   1269 			pathbuf_destroy(pb);
   1270 			if (error != 0) {
   1271 				for (j = 0; j < lookedup; ++j)
   1272 					(void)vn_close(vpp[j], FREAD|FWRITE,
   1273 					    uc);
   1274 				kmem_free(vpp, ccio->ccio_ndisks *
   1275 				    sizeof(*vpp));
   1276 				kmem_free(cpp, ccio->ccio_ndisks *
   1277 				    sizeof(*cpp));
   1278 				goto out;
   1279 			}
   1280 			++lookedup;
   1281 		}
   1282 
   1283 		/* Attach the disk. */
   1284 		disk_attach(&cs->sc_dkdev);
   1285 		bufq_alloc(&cs->sc_bufq, "fcfs", 0);
   1286 
   1287 		/*
   1288 		 * Initialize the ccd.  Fills in the softc for us.
   1289 		 */
   1290 		if ((error = ccdinit(cs, cpp, vpp, l)) != 0) {
   1291 			for (j = 0; j < lookedup; ++j)
   1292 				(void)vn_close(vpp[j], FREAD|FWRITE,
   1293 				    uc);
   1294 			kmem_free(vpp, ccio->ccio_ndisks * sizeof(*vpp));
   1295 			kmem_free(cpp, ccio->ccio_ndisks * sizeof(*cpp));
   1296 			disk_detach(&cs->sc_dkdev);
   1297 			mutex_exit(&cs->sc_dvlock);
   1298 			bufq_free(cs->sc_bufq);
   1299 			return error;
   1300 		}
   1301 
   1302 		/* We can free the temporary variables now. */
   1303 		kmem_free(vpp, ccio->ccio_ndisks * sizeof(*vpp));
   1304 		kmem_free(cpp, ccio->ccio_ndisks * sizeof(*cpp));
   1305 
   1306 		/*
   1307 		 * The ccd has been successfully initialized, so
   1308 		 * we can place it into the array.  Don't try to
   1309 		 * read the disklabel until the disk has been attached,
   1310 		 * because space for the disklabel is allocated
   1311 		 * in disk_attach();
   1312 		 */
   1313 		ccio->ccio_unit = unit;
   1314 		ccio->ccio_size = cs->sc_size;
   1315 
   1316 		/* Try and read the disklabel. */
   1317 		ccdgetdisklabel(dev);
   1318 		disk_set_info(NULL, &cs->sc_dkdev, NULL);
   1319 
   1320 		/* discover wedges */
   1321 		mutex_exit(&cs->sc_dvlock);
   1322 		dkwedge_discover(&cs->sc_dkdev);
   1323 		return 0;
   1324 
   1325 	case CCDIOCCLR:
   1326 		/*
   1327 		 * Don't unconfigure if any other partitions are open
   1328 		 * or if both the character and block flavors of this
   1329 		 * partition are open.
   1330 		 */
   1331 		part = DISKPART(dev);
   1332 		pmask = (1 << part);
   1333 		if ((cs->sc_dkdev.dk_openmask & ~pmask) ||
   1334 		    ((cs->sc_dkdev.dk_bopenmask & pmask) &&
   1335 		    (cs->sc_dkdev.dk_copenmask & pmask))) {
   1336 			error = EBUSY;
   1337 			goto out;
   1338 		}
   1339 
   1340 		/* Delete all of our wedges. */
   1341 		dkwedge_delall(&cs->sc_dkdev);
   1342 
   1343 		/* Stop new I/O, wait for in-flight I/O to complete. */
   1344 		mutex_enter(cs->sc_iolock);
   1345 		cs->sc_flags &= ~(CCDF_INITED|CCDF_VLABEL);
   1346 		cs->sc_zap = true;
   1347 		while (disk_isbusy(&cs->sc_dkdev) ||
   1348 		    bufq_peek(cs->sc_bufq) != NULL ||
   1349 		    cs->sc_thread != NULL) {
   1350 			cv_broadcast(&cs->sc_push);
   1351 			(void)cv_timedwait(&cs->sc_stop, cs->sc_iolock, hz);
   1352 		}
   1353 		mutex_exit(cs->sc_iolock);
   1354 
   1355 		/*
   1356 		 * Free ccd_softc information and clear entry.
   1357 		 */
   1358 
   1359 		/* Close the components and free their pathnames. */
   1360 		for (i = 0; i < cs->sc_nccdisks; ++i) {
   1361 			/*
   1362 			 * XXX: this close could potentially fail and
   1363 			 * cause Bad Things.  Maybe we need to force
   1364 			 * the close to happen?
   1365 			 */
   1366 #ifdef DEBUG
   1367 			if (ccddebug & CCDB_VNODE)
   1368 				vprint("CCDIOCCLR: vnode info",
   1369 				    cs->sc_cinfo[i].ci_vp);
   1370 #endif
   1371 			(void)vn_close(cs->sc_cinfo[i].ci_vp, FREAD|FWRITE,
   1372 			    uc);
   1373 			kmem_free(cs->sc_cinfo[i].ci_path,
   1374 			    cs->sc_cinfo[i].ci_pathlen);
   1375 		}
   1376 
   1377 		/* Free interleave index. */
   1378 		for (i = 0; cs->sc_itable[i].ii_ndisk; ++i) {
   1379 			kmem_free(cs->sc_itable[i].ii_index,
   1380 			    cs->sc_itable[i].ii_indexsz);
   1381 		}
   1382 
   1383 		/* Free component info and interleave table. */
   1384 		kmem_free(cs->sc_cinfo, cs->sc_nccdisks *
   1385 		    sizeof(struct ccdcinfo));
   1386 		kmem_free(cs->sc_itable, (cs->sc_nccdisks + 1) *
   1387 		    sizeof(struct ccdiinfo));
   1388 
   1389 		aprint_normal("%s: detached\n", cs->sc_xname);
   1390 
   1391 		/* Detach the disk. */
   1392 		disk_detach(&cs->sc_dkdev);
   1393 		bufq_free(cs->sc_bufq);
   1394 		ccdput(cs);
   1395 		/* Don't break, otherwise cs is read again. */
   1396 		return 0;
   1397 
   1398 	case DIOCGSTRATEGY:
   1399 	    {
   1400 		struct disk_strategy *dks = (void *)data;
   1401 
   1402 		mutex_enter(cs->sc_iolock);
   1403 		if (cs->sc_bufq != NULL)
   1404 			strlcpy(dks->dks_name,
   1405 			    bufq_getstrategyname(cs->sc_bufq),
   1406 			    sizeof(dks->dks_name));
   1407 		else
   1408 			error = EINVAL;
   1409 		mutex_exit(cs->sc_iolock);
   1410 		dks->dks_paramlen = 0;
   1411 		break;
   1412 	    }
   1413 
   1414 	case DIOCGCACHE:
   1415 	    {
   1416 		int dkcache = 0;
   1417 
   1418 		/*
   1419 		 * We pass this call down to all components and report
   1420 		 * intersection of the flags returned by the components.
   1421 		 * If any errors out, we return error. CCD components
   1422 		 * can not change unless the device is unconfigured, so
   1423 		 * device feature flags will remain static. RCE/WCE can change
   1424 		 * of course, if set directly on underlying device.
   1425 		 */
   1426 		for (error = 0, i = 0; i < cs->sc_nccdisks; i++) {
   1427 			error = VOP_IOCTL(cs->sc_cinfo[i].ci_vp, cmd, &j,
   1428 				      flag, uc);
   1429 			if (error)
   1430 				break;
   1431 
   1432 			if (i == 0)
   1433 				dkcache = j;
   1434 			else
   1435 				dkcache = DKCACHE_COMBINE(dkcache, j);
   1436 		}
   1437 
   1438 		*((int *)data) = dkcache;
   1439 		break;
   1440 	    }
   1441 
   1442 	case DIOCCACHESYNC:
   1443 		/*
   1444 		 * We pass this call down to all components and report
   1445 		 * the first error we encounter.
   1446 		 */
   1447 		for (error = 0, i = 0; i < cs->sc_nccdisks; i++) {
   1448 			j = VOP_IOCTL(cs->sc_cinfo[i].ci_vp, cmd, data,
   1449 				      flag, uc);
   1450 			if (j != 0 && error == 0)
   1451 				error = j;
   1452 		}
   1453 		break;
   1454 
   1455 	case DIOCWDINFO:
   1456 	case DIOCSDINFO:
   1457 #ifdef __HAVE_OLD_DISKLABEL
   1458 	case ODIOCWDINFO:
   1459 	case ODIOCSDINFO:
   1460 #endif
   1461 	{
   1462 		struct disklabel *lp;
   1463 #ifdef __HAVE_OLD_DISKLABEL
   1464 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
   1465 			memset(&newlabel, 0, sizeof newlabel);
   1466 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
   1467 			lp = &newlabel;
   1468 		} else
   1469 #endif
   1470 		lp = (struct disklabel *)data;
   1471 
   1472 		cs->sc_flags |= CCDF_LABELLING;
   1473 
   1474 		error = setdisklabel(cs->sc_dkdev.dk_label,
   1475 		    lp, 0, cs->sc_dkdev.dk_cpulabel);
   1476 		if (error == 0) {
   1477 			if (cmd == DIOCWDINFO
   1478 #ifdef __HAVE_OLD_DISKLABEL
   1479 			    || cmd == ODIOCWDINFO
   1480 #endif
   1481 			   )
   1482 				error = writedisklabel(CCDLABELDEV(dev),
   1483 				    ccdstrategy, cs->sc_dkdev.dk_label,
   1484 				    cs->sc_dkdev.dk_cpulabel);
   1485 		}
   1486 
   1487 		cs->sc_flags &= ~CCDF_LABELLING;
   1488 		break;
   1489 	}
   1490 
   1491 	case DIOCKLABEL:
   1492 		if (*(int *)data != 0)
   1493 			cs->sc_flags |= CCDF_KLABEL;
   1494 		else
   1495 			cs->sc_flags &= ~CCDF_KLABEL;
   1496 		break;
   1497 
   1498 	case DIOCWLABEL:
   1499 		if (*(int *)data != 0)
   1500 			cs->sc_flags |= CCDF_WLABEL;
   1501 		else
   1502 			cs->sc_flags &= ~CCDF_WLABEL;
   1503 		break;
   1504 
   1505 	case DIOCGDEFLABEL:
   1506 		ccdgetdefaultlabel(cs, (struct disklabel *)data);
   1507 		break;
   1508 
   1509 #ifdef __HAVE_OLD_DISKLABEL
   1510 	case ODIOCGDEFLABEL:
   1511 		ccdgetdefaultlabel(cs, &newlabel);
   1512 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1513 			return ENOTTY;
   1514 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
   1515 		break;
   1516 #endif
   1517 
   1518 	default:
   1519 		error = ENOTTY;
   1520 	}
   1521 
   1522  out:
   1523 	mutex_exit(&cs->sc_dvlock);
   1524 	return (error);
   1525 }
   1526 
   1527 static int
   1528 ccdsize(dev_t dev)
   1529 {
   1530 	struct ccd_softc *cs;
   1531 	struct disklabel *lp;
   1532 	int part, unit, omask, size;
   1533 
   1534 	unit = ccdunit(dev);
   1535 	if ((cs = ccdget(unit, 0)) == NULL)
   1536 		return -1;
   1537 
   1538 	if ((cs->sc_flags & CCDF_INITED) == 0)
   1539 		return (-1);
   1540 
   1541 	part = DISKPART(dev);
   1542 	omask = cs->sc_dkdev.dk_openmask & (1 << part);
   1543 	lp = cs->sc_dkdev.dk_label;
   1544 
   1545 	if (omask == 0 && ccdopen(dev, 0, S_IFBLK, curlwp))
   1546 		return (-1);
   1547 
   1548 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
   1549 		size = -1;
   1550 	else
   1551 		size = lp->d_partitions[part].p_size *
   1552 		    (lp->d_secsize / DEV_BSIZE);
   1553 
   1554 	if (omask == 0 && ccdclose(dev, 0, S_IFBLK, curlwp))
   1555 		return (-1);
   1556 
   1557 	return (size);
   1558 }
   1559 
   1560 static void
   1561 ccdgetdefaultlabel(struct ccd_softc *cs, struct disklabel *lp)
   1562 {
   1563 	struct ccdgeom *ccg = &cs->sc_geom;
   1564 
   1565 	memset(lp, 0, sizeof(*lp));
   1566 
   1567 	if (cs->sc_size > UINT32_MAX)
   1568 		lp->d_secperunit = UINT32_MAX;
   1569 	else
   1570 		lp->d_secperunit = cs->sc_size;
   1571 	lp->d_secsize = ccg->ccg_secsize;
   1572 	lp->d_nsectors = ccg->ccg_nsectors;
   1573 	lp->d_ntracks = ccg->ccg_ntracks;
   1574 	lp->d_ncylinders = ccg->ccg_ncylinders;
   1575 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1576 
   1577 	strncpy(lp->d_typename, "ccd", sizeof(lp->d_typename));
   1578 	lp->d_type = DKTYPE_CCD;
   1579 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1580 	lp->d_rpm = 3600;
   1581 	lp->d_interleave = 1;
   1582 	lp->d_flags = 0;
   1583 
   1584 	lp->d_partitions[RAW_PART].p_offset = 0;
   1585 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
   1586 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1587 	lp->d_npartitions = RAW_PART + 1;
   1588 
   1589 	lp->d_magic = DISKMAGIC;
   1590 	lp->d_magic2 = DISKMAGIC;
   1591 	lp->d_checksum = dkcksum(cs->sc_dkdev.dk_label);
   1592 }
   1593 
   1594 /*
   1595  * Read the disklabel from the ccd.  If one is not present, fake one
   1596  * up.
   1597  */
   1598 static void
   1599 ccdgetdisklabel(dev_t dev)
   1600 {
   1601 	int unit = ccdunit(dev);
   1602 	struct ccd_softc *cs;
   1603 	const char *errstring;
   1604 	struct disklabel *lp;
   1605 	struct cpu_disklabel *clp;
   1606 
   1607 	if ((cs = ccdget(unit, 0)) == NULL)
   1608 		return;
   1609 	lp = cs->sc_dkdev.dk_label;
   1610 	clp = cs->sc_dkdev.dk_cpulabel;
   1611 	KASSERT(mutex_owned(&cs->sc_dvlock));
   1612 
   1613 	memset(clp, 0, sizeof(*clp));
   1614 
   1615 	ccdgetdefaultlabel(cs, lp);
   1616 
   1617 	/*
   1618 	 * Call the generic disklabel extraction routine.
   1619 	 */
   1620 	cs->sc_flags |= CCDF_RLABEL;
   1621 	if ((cs->sc_flags & CCDF_NOLABEL) != 0)
   1622 		errstring = "CCDF_NOLABEL set; ignoring on-disk label";
   1623 	else
   1624 		errstring = readdisklabel(CCDLABELDEV(dev), ccdstrategy,
   1625 		    cs->sc_dkdev.dk_label, cs->sc_dkdev.dk_cpulabel);
   1626 	if (errstring)
   1627 		ccdmakedisklabel(cs);
   1628 	else {
   1629 		int i;
   1630 		struct partition *pp;
   1631 
   1632 		/*
   1633 		 * Sanity check whether the found disklabel is valid.
   1634 		 *
   1635 		 * This is necessary since total size of ccd may vary
   1636 		 * when an interleave is changed even though exactly
   1637 		 * same componets are used, and old disklabel may used
   1638 		 * if that is found.
   1639 		 */
   1640 		if (lp->d_secperunit < UINT32_MAX ?
   1641 			lp->d_secperunit != cs->sc_size :
   1642 			lp->d_secperunit > cs->sc_size)
   1643 			printf("WARNING: %s: "
   1644 			    "total sector size in disklabel (%ju) != "
   1645 			    "the size of ccd (%ju)\n", cs->sc_xname,
   1646 			    (uintmax_t)lp->d_secperunit,
   1647 			    (uintmax_t)cs->sc_size);
   1648 		for (i = 0; i < lp->d_npartitions; i++) {
   1649 			pp = &lp->d_partitions[i];
   1650 			if (pp->p_offset + pp->p_size > cs->sc_size)
   1651 				printf("WARNING: %s: end of partition `%c' "
   1652 				    "exceeds the size of ccd (%ju)\n",
   1653 				    cs->sc_xname, 'a' + i, (uintmax_t)cs->sc_size);
   1654 		}
   1655 	}
   1656 
   1657 #ifdef DEBUG
   1658 	/* It's actually extremely common to have unlabeled ccds. */
   1659 	if (ccddebug & CCDB_LABEL)
   1660 		if (errstring != NULL)
   1661 			printf("%s: %s\n", cs->sc_xname, errstring);
   1662 #endif
   1663 
   1664 	/* In-core label now valid. */
   1665 	cs->sc_flags = (cs->sc_flags | CCDF_VLABEL) & ~CCDF_RLABEL;
   1666 }
   1667 
   1668 /*
   1669  * Take care of things one might want to take care of in the event
   1670  * that a disklabel isn't present.
   1671  */
   1672 static void
   1673 ccdmakedisklabel(struct ccd_softc *cs)
   1674 {
   1675 	struct disklabel *lp = cs->sc_dkdev.dk_label;
   1676 
   1677 	/*
   1678 	 * For historical reasons, if there's no disklabel present
   1679 	 * the raw partition must be marked FS_BSDFFS.
   1680 	 */
   1681 	lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
   1682 
   1683 	strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
   1684 
   1685 	lp->d_checksum = dkcksum(lp);
   1686 }
   1687 
   1688 #ifdef DEBUG
   1689 static void
   1690 printiinfo(struct ccdiinfo *ii)
   1691 {
   1692 	int ix, i;
   1693 
   1694 	for (ix = 0; ii->ii_ndisk; ix++, ii++) {
   1695 		printf(" itab[%d]: #dk %d sblk %" PRId64 " soff %" PRId64,
   1696 		    ix, ii->ii_ndisk, ii->ii_startblk, ii->ii_startoff);
   1697 		for (i = 0; i < ii->ii_ndisk; i++)
   1698 			printf(" %d", ii->ii_index[i]);
   1699 		printf("\n");
   1700 	}
   1701 }
   1702 #endif
   1703 
   1704 MODULE(MODULE_CLASS_DRIVER, ccd, "dk_subr");
   1705 
   1706 static int
   1707 ccd_modcmd(modcmd_t cmd, void *arg)
   1708 {
   1709 	int error = 0;
   1710 #ifdef _MODULE
   1711 	int bmajor = -1, cmajor = -1;
   1712 #endif
   1713 
   1714 
   1715 	switch (cmd) {
   1716 	case MODULE_CMD_INIT:
   1717 #ifdef _MODULE
   1718 		ccdattach(0);
   1719 
   1720 		error = devsw_attach("ccd", &ccd_bdevsw, &bmajor,
   1721 		    &ccd_cdevsw, &cmajor);
   1722 		sysctl_kern_ccd_setup(&ccd_clog);
   1723 #endif
   1724 		break;
   1725 
   1726 	case MODULE_CMD_FINI:
   1727 #ifdef _MODULE
   1728 		mutex_enter(&ccd_lock);
   1729 		if (ccd_nactive) {
   1730 			mutex_exit(&ccd_lock);
   1731 			error = EBUSY;
   1732 		} else {
   1733 			mutex_exit(&ccd_lock);
   1734 			error = devsw_detach(&ccd_bdevsw, &ccd_cdevsw);
   1735 			ccddetach();
   1736 		}
   1737 		sysctl_teardown(&ccd_clog);
   1738 #endif
   1739 		break;
   1740 
   1741 	case MODULE_CMD_STAT:
   1742 		return ENOTTY;
   1743 
   1744 	default:
   1745 		return ENOTTY;
   1746 	}
   1747 
   1748 	return error;
   1749 }
   1750 
   1751 static int
   1752 ccd_units_sysctl(SYSCTLFN_ARGS)
   1753 {
   1754 	struct sysctlnode node;
   1755 	struct ccd_softc *sc;
   1756 	int error, i, nccd, *units;
   1757 	size_t size;
   1758 
   1759 	nccd = 0;
   1760 	mutex_enter(&ccd_lock);
   1761 	LIST_FOREACH(sc, &ccds, sc_link)
   1762 		nccd++;
   1763 	mutex_exit(&ccd_lock);
   1764 
   1765 	if (nccd != 0) {
   1766 		size = nccd * sizeof(*units);
   1767 		units = kmem_zalloc(size, KM_SLEEP);
   1768 		if (units == NULL)
   1769 			return ENOMEM;
   1770 
   1771 		i = 0;
   1772 		mutex_enter(&ccd_lock);
   1773 		LIST_FOREACH(sc, &ccds, sc_link) {
   1774 			if (i >= nccd)
   1775 				break;
   1776 			units[i] = sc->sc_unit;
   1777 		}
   1778 		mutex_exit(&ccd_lock);
   1779 	} else {
   1780 		units = NULL;
   1781 		size = 0;
   1782 	}
   1783 
   1784 	node = *rnode;
   1785 	node.sysctl_data = units;
   1786 	node.sysctl_size = size;
   1787 
   1788 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1789 	if (units)
   1790 		kmem_free(units, size);
   1791 	return error;
   1792 }
   1793 
   1794 static int
   1795 ccd_info_sysctl(SYSCTLFN_ARGS)
   1796 {
   1797 	struct sysctlnode node;
   1798 	struct ccddiskinfo ccd;
   1799 	struct ccd_softc *sc;
   1800 	int unit;
   1801 
   1802 	if (newp == NULL || newlen != sizeof(int))
   1803 		return EINVAL;
   1804 
   1805 	unit = *(const int *)newp;
   1806 	newp = NULL;
   1807 	newlen = 0;
   1808 	ccd.ccd_ndisks = ~0;
   1809 	mutex_enter(&ccd_lock);
   1810 	LIST_FOREACH(sc, &ccds, sc_link) {
   1811 		if (sc->sc_unit == unit) {
   1812 			ccd.ccd_ileave = sc->sc_ileave;
   1813 			ccd.ccd_size = sc->sc_size;
   1814 			ccd.ccd_ndisks = sc->sc_nccdisks;
   1815 			ccd.ccd_flags = sc->sc_flags;
   1816 			break;
   1817 		}
   1818 	}
   1819 	mutex_exit(&ccd_lock);
   1820 
   1821 	if (ccd.ccd_ndisks == ~0)
   1822 		return ENOENT;
   1823 
   1824 	node = *rnode;
   1825 	node.sysctl_data = &ccd;
   1826 	node.sysctl_size = sizeof(ccd);
   1827 
   1828 	return sysctl_lookup(SYSCTLFN_CALL(&node));
   1829 }
   1830 
   1831 static int
   1832 ccd_components_sysctl(SYSCTLFN_ARGS)
   1833 {
   1834 	struct sysctlnode node;
   1835 	int error, unit;
   1836 	size_t size;
   1837 	char *names, *p, *ep;
   1838 	struct ccd_softc *sc;
   1839 
   1840 	if (newp == NULL || newlen != sizeof(int))
   1841 		return EINVAL;
   1842 
   1843 	size = 0;
   1844 	unit = *(const int *)newp;
   1845 	newp = NULL;
   1846 	newlen = 0;
   1847 	mutex_enter(&ccd_lock);
   1848 	LIST_FOREACH(sc, &ccds, sc_link)
   1849 		if (sc->sc_unit == unit) {
   1850 			for (size_t i = 0; i < sc->sc_nccdisks; i++)
   1851 				size += strlen(sc->sc_cinfo[i].ci_path) + 1;
   1852 			break;
   1853 		}
   1854 	mutex_exit(&ccd_lock);
   1855 
   1856 	if (size == 0)
   1857 		return ENOENT;
   1858 	names = kmem_zalloc(size, KM_SLEEP);
   1859 	if (names == NULL)
   1860 		return ENOMEM;
   1861 
   1862 	p = names;
   1863 	ep = names + size;
   1864 	mutex_enter(&ccd_lock);
   1865 	LIST_FOREACH(sc, &ccds, sc_link)
   1866 		if (sc->sc_unit == unit) {
   1867 			for (size_t i = 0; i < sc->sc_nccdisks; i++) {
   1868 				char *d = sc->sc_cinfo[i].ci_path;
   1869 				while (p < ep && (*p++ = *d++) != '\0')
   1870 					continue;
   1871 			}
   1872 			break;
   1873 		}
   1874 	mutex_exit(&ccd_lock);
   1875 
   1876 	node = *rnode;
   1877 	node.sysctl_data = names;
   1878 	node.sysctl_size = ep - names;
   1879 
   1880 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1881 	kmem_free(names, size);
   1882 	return error;
   1883 }
   1884 
   1885 SYSCTL_SETUP(sysctl_kern_ccd_setup, "sysctl kern.ccd subtree setup")
   1886 {
   1887 	const struct sysctlnode *node = NULL;
   1888 
   1889 	sysctl_createv(clog, 0, NULL, &node,
   1890 	    CTLFLAG_PERMANENT,
   1891 	    CTLTYPE_NODE, "ccd",
   1892 	    SYSCTL_DESCR("ConCatenated Disk state"),
   1893 	    NULL, 0, NULL, 0,
   1894 	    CTL_KERN, CTL_CREATE, CTL_EOL);
   1895 
   1896 	if (node == NULL)
   1897 		return;
   1898 
   1899 	sysctl_createv(clog, 0, &node, NULL,
   1900 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY,
   1901 	    CTLTYPE_STRUCT, "units",
   1902 	    SYSCTL_DESCR("List of ccd unit numbers"),
   1903 	    ccd_units_sysctl, 0, NULL, 0,
   1904 	    CTL_CREATE, CTL_EOL);
   1905 	sysctl_createv(clog, 0, &node, NULL,
   1906 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1907 	    CTLTYPE_STRUCT, "info",
   1908 	    SYSCTL_DESCR("Information about a CCD unit"),
   1909 	    ccd_info_sysctl, 0, NULL, 0,
   1910 	    CTL_CREATE, CTL_EOL);
   1911 	sysctl_createv(clog, 0, &node, NULL,
   1912 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE,
   1913 	    CTLTYPE_STRUCT, "components",
   1914 	    SYSCTL_DESCR("Information about CCD components"),
   1915 	    ccd_components_sysctl, 0, NULL, 0,
   1916 	    CTL_CREATE, CTL_EOL);
   1917 }
   1918