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