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