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