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