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