Home | History | Annotate | Line # | Download | only in dev
ccd.c revision 1.54
      1  1.54   thorpej /*	$NetBSD: ccd.c,v 1.54 1998/11/13 00:31:02 thorpej Exp $	*/
      2  1.11   thorpej 
      3  1.28   thorpej /*-
      4  1.46   thorpej  * Copyright (c) 1996, 1997, 1998 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.28   thorpej  * by Jason R. Thorpe.
      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  * 3. All advertising materials mentioning features or use of this software
     19  1.11   thorpej  *    must display the following acknowledgement:
     20  1.28   thorpej  *        This product includes software developed by the NetBSD
     21  1.28   thorpej  *        Foundation, Inc. and its contributors.
     22  1.28   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.28   thorpej  *    contributors may be used to endorse or promote products derived
     24  1.28   thorpej  *    from this software without specific prior written permission.
     25  1.11   thorpej  *
     26  1.28   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.28   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.28   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.45       jtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.45       jtc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.28   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.28   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.28   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.28   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.28   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.28   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.11   thorpej  */
     38   1.2       cgd 
     39   1.1   hpeyerl /*
     40   1.1   hpeyerl  * Copyright (c) 1988 University of Utah.
     41   1.3   hpeyerl  * Copyright (c) 1990, 1993
     42   1.3   hpeyerl  *	The Regents of the University of California.  All rights reserved.
     43   1.1   hpeyerl  *
     44   1.1   hpeyerl  * This code is derived from software contributed to Berkeley by
     45   1.1   hpeyerl  * the Systems Programming Group of the University of Utah Computer
     46   1.1   hpeyerl  * Science Department.
     47   1.1   hpeyerl  *
     48   1.1   hpeyerl  * Redistribution and use in source and binary forms, with or without
     49   1.1   hpeyerl  * modification, are permitted provided that the following conditions
     50   1.1   hpeyerl  * are met:
     51   1.1   hpeyerl  * 1. Redistributions of source code must retain the above copyright
     52   1.1   hpeyerl  *    notice, this list of conditions and the following disclaimer.
     53   1.1   hpeyerl  * 2. Redistributions in binary form must reproduce the above copyright
     54   1.1   hpeyerl  *    notice, this list of conditions and the following disclaimer in the
     55   1.1   hpeyerl  *    documentation and/or other materials provided with the distribution.
     56   1.1   hpeyerl  * 3. All advertising materials mentioning features or use of this software
     57   1.1   hpeyerl  *    must display the following acknowledgement:
     58   1.1   hpeyerl  *	This product includes software developed by the University of
     59   1.1   hpeyerl  *	California, Berkeley and its contributors.
     60   1.1   hpeyerl  * 4. Neither the name of the University nor the names of its contributors
     61   1.1   hpeyerl  *    may be used to endorse or promote products derived from this software
     62   1.1   hpeyerl  *    without specific prior written permission.
     63   1.1   hpeyerl  *
     64   1.1   hpeyerl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     65   1.1   hpeyerl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     66   1.1   hpeyerl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     67   1.1   hpeyerl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     68   1.1   hpeyerl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     69   1.1   hpeyerl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     70   1.1   hpeyerl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     71   1.1   hpeyerl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     72   1.1   hpeyerl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     73   1.1   hpeyerl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     74   1.1   hpeyerl  * SUCH DAMAGE.
     75   1.1   hpeyerl  *
     76   1.2       cgd  * from: Utah $Hdr: cd.c 1.6 90/11/28$
     77   1.2       cgd  *
     78   1.3   hpeyerl  *	@(#)cd.c	8.2 (Berkeley) 11/16/93
     79   1.1   hpeyerl  */
     80   1.1   hpeyerl 
     81   1.1   hpeyerl /*
     82   1.1   hpeyerl  * "Concatenated" disk driver.
     83  1.11   thorpej  *
     84  1.11   thorpej  * Dynamic configuration and disklabel support by:
     85  1.11   thorpej  *	Jason R. Thorpe <thorpej (at) nas.nasa.gov>
     86  1.11   thorpej  *	Numerical Aerodynamic Simulation Facility
     87  1.11   thorpej  *	Mail Stop 258-6
     88  1.11   thorpej  *	NASA Ames Research Center
     89  1.11   thorpej  *	Moffett Field, CA 94035
     90  1.24   thorpej  *
     91  1.24   thorpej  * Mirroring support based on code written by Satoshi Asami
     92  1.24   thorpej  * and Nisha Talagala.
     93   1.1   hpeyerl  */
     94   1.1   hpeyerl 
     95   1.1   hpeyerl #include <sys/param.h>
     96   1.1   hpeyerl #include <sys/systm.h>
     97   1.3   hpeyerl #include <sys/proc.h>
     98   1.1   hpeyerl #include <sys/errno.h>
     99   1.1   hpeyerl #include <sys/buf.h>
    100   1.1   hpeyerl #include <sys/malloc.h>
    101  1.11   thorpej #include <sys/namei.h>
    102   1.3   hpeyerl #include <sys/stat.h>
    103   1.3   hpeyerl #include <sys/ioctl.h>
    104   1.3   hpeyerl #include <sys/disklabel.h>
    105  1.11   thorpej #include <sys/device.h>
    106  1.11   thorpej #include <sys/disk.h>
    107  1.11   thorpej #include <sys/syslog.h>
    108   1.3   hpeyerl #include <sys/fcntl.h>
    109  1.11   thorpej #include <sys/vnode.h>
    110  1.31  christos #include <sys/conf.h>
    111   1.1   hpeyerl 
    112   1.1   hpeyerl #include <dev/ccdvar.h>
    113   1.1   hpeyerl 
    114  1.11   thorpej #if defined(CCDDEBUG) && !defined(DEBUG)
    115  1.11   thorpej #define DEBUG
    116  1.11   thorpej #endif
    117  1.11   thorpej 
    118   1.1   hpeyerl #ifdef DEBUG
    119   1.3   hpeyerl #define CCDB_FOLLOW	0x01
    120   1.3   hpeyerl #define CCDB_INIT	0x02
    121   1.3   hpeyerl #define CCDB_IO		0x04
    122  1.11   thorpej #define CCDB_LABEL	0x08
    123  1.11   thorpej #define CCDB_VNODE	0x10
    124  1.24   thorpej int ccddebug = 0x00;
    125   1.1   hpeyerl #endif
    126   1.1   hpeyerl 
    127   1.6       cgd #define	ccdunit(x)	DISKUNIT(x)
    128   1.6       cgd 
    129   1.6       cgd struct ccdbuf {
    130   1.6       cgd 	struct buf	cb_buf;		/* new I/O buf */
    131   1.6       cgd 	struct buf	*cb_obp;	/* ptr. to original I/O buf */
    132   1.6       cgd 	int		cb_unit;	/* target unit */
    133   1.6       cgd 	int		cb_comp;	/* target component */
    134  1.38   thorpej };
    135  1.24   thorpej 
    136  1.51   thorpej /* XXX Safe to wait? */
    137  1.52   thorpej #define	CCD_GETBUF(cs)		pool_get(&(cs)->sc_cbufpool, PR_NOWAIT)
    138  1.51   thorpej #define	CCD_PUTBUF(cs, cbp)	pool_put(&(cs)->sc_cbufpool, cbp)
    139   1.1   hpeyerl 
    140  1.11   thorpej #define CCDLABELDEV(dev)	\
    141  1.11   thorpej 	(MAKEDISKDEV(major((dev)), ccdunit((dev)), RAW_PART))
    142   1.1   hpeyerl 
    143  1.11   thorpej /* called by main() at boot time */
    144  1.11   thorpej void	ccdattach __P((int));
    145  1.11   thorpej 
    146  1.11   thorpej /* called by biodone() at interrupt time */
    147  1.29  christos void	ccdiodone __P((struct buf *));
    148  1.29  christos int	ccdsize __P((dev_t));
    149  1.11   thorpej 
    150  1.11   thorpej static	void ccdstart __P((struct ccd_softc *, struct buf *));
    151  1.11   thorpej static	void ccdinterleave __P((struct ccd_softc *, int));
    152  1.11   thorpej static	void ccdintr __P((struct ccd_softc *, struct buf *));
    153  1.11   thorpej static	int ccdinit __P((struct ccddevice *, char **, struct proc *));
    154  1.11   thorpej static	int ccdlookup __P((char *, struct proc *p, struct vnode **));
    155  1.24   thorpej static	void ccdbuffer __P((struct ccd_softc *, struct buf *,
    156  1.24   thorpej 		daddr_t, caddr_t, long, struct ccdbuf **));
    157  1.44   thorpej static	void ccdgetdefaultlabel __P((struct ccd_softc *, struct disklabel *));
    158  1.11   thorpej static	void ccdgetdisklabel __P((dev_t));
    159  1.11   thorpej static	void ccdmakedisklabel __P((struct ccd_softc *));
    160  1.15   thorpej static	int ccdlock __P((struct ccd_softc *));
    161  1.15   thorpej static	void ccdunlock __P((struct ccd_softc *));
    162   1.3   hpeyerl 
    163  1.11   thorpej #ifdef DEBUG
    164  1.11   thorpej static	void printiinfo __P((struct ccdiinfo *));
    165  1.11   thorpej #endif
    166  1.11   thorpej 
    167  1.11   thorpej /* Non-private for the benefit of libkvm. */
    168  1.11   thorpej struct	ccd_softc *ccd_softc;
    169  1.11   thorpej struct	ccddevice *ccddevs;
    170  1.11   thorpej int	numccd = 0;
    171   1.1   hpeyerl 
    172   1.3   hpeyerl /*
    173  1.11   thorpej  * Called by main() during pseudo-device attachment.  All we need
    174  1.11   thorpej  * to do is allocate enough space for devices to be configured later.
    175   1.1   hpeyerl  */
    176   1.1   hpeyerl void
    177   1.3   hpeyerl ccdattach(num)
    178   1.3   hpeyerl 	int num;
    179   1.3   hpeyerl {
    180  1.11   thorpej 	if (num <= 0) {
    181  1.11   thorpej #ifdef DIAGNOSTIC
    182  1.11   thorpej 		panic("ccdattach: count <= 0");
    183  1.11   thorpej #endif
    184   1.3   hpeyerl 		return;
    185  1.11   thorpej 	}
    186  1.11   thorpej 
    187  1.11   thorpej 	ccd_softc = (struct ccd_softc *)malloc(num * sizeof(struct ccd_softc),
    188  1.11   thorpej 	    M_DEVBUF, M_NOWAIT);
    189  1.11   thorpej 	ccddevs = (struct ccddevice *)malloc(num * sizeof(struct ccddevice),
    190  1.11   thorpej 	    M_DEVBUF, M_NOWAIT);
    191  1.11   thorpej 	if ((ccd_softc == NULL) || (ccddevs == NULL)) {
    192  1.35  christos 		printf("WARNING: no memory for concatenated disks\n");
    193  1.11   thorpej 		if (ccd_softc != NULL)
    194  1.11   thorpej 			free(ccd_softc, M_DEVBUF);
    195  1.11   thorpej 		if (ccddevs != NULL)
    196  1.11   thorpej 			free(ccddevs, M_DEVBUF);
    197   1.3   hpeyerl 		return;
    198   1.3   hpeyerl 	}
    199   1.3   hpeyerl 	numccd = num;
    200  1.11   thorpej 	bzero(ccd_softc, num * sizeof(struct ccd_softc));
    201  1.11   thorpej 	bzero(ccddevs, num * sizeof(struct ccddevice));
    202   1.1   hpeyerl }
    203   1.1   hpeyerl 
    204  1.11   thorpej static int
    205  1.11   thorpej ccdinit(ccd, cpaths, p)
    206   1.1   hpeyerl 	struct ccddevice *ccd;
    207  1.11   thorpej 	char **cpaths;
    208  1.11   thorpej 	struct proc *p;
    209   1.1   hpeyerl {
    210   1.1   hpeyerl 	register struct ccd_softc *cs = &ccd_softc[ccd->ccd_unit];
    211  1.29  christos 	register struct ccdcinfo *ci = NULL;
    212   1.1   hpeyerl 	register size_t size;
    213   1.1   hpeyerl 	register int ix;
    214  1.11   thorpej 	struct vnode *vp;
    215  1.11   thorpej 	struct vattr va;
    216   1.1   hpeyerl 	size_t minsize;
    217  1.11   thorpej 	int maxsecsize;
    218   1.7       cgd 	struct partinfo dpart;
    219  1.11   thorpej 	struct ccdgeom *ccg = &cs->sc_geom;
    220  1.11   thorpej 	char tmppath[MAXPATHLEN];
    221  1.11   thorpej 	int error;
    222   1.1   hpeyerl 
    223   1.1   hpeyerl #ifdef DEBUG
    224   1.3   hpeyerl 	if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    225  1.35  christos 		printf("ccdinit: unit %d\n", ccd->ccd_unit);
    226   1.1   hpeyerl #endif
    227  1.11   thorpej 
    228   1.1   hpeyerl 	cs->sc_size = 0;
    229   1.1   hpeyerl 	cs->sc_ileave = ccd->ccd_interleave;
    230  1.11   thorpej 	cs->sc_nccdisks = ccd->ccd_ndev;
    231  1.35  christos 	sprintf(cs->sc_xname, "ccd%d", ccd->ccd_unit);	/* XXX */
    232  1.11   thorpej 
    233  1.11   thorpej 	/* Allocate space for the component info. */
    234  1.11   thorpej 	cs->sc_cinfo = malloc(cs->sc_nccdisks * sizeof(struct ccdcinfo),
    235  1.11   thorpej 	    M_DEVBUF, M_WAITOK);
    236  1.11   thorpej 
    237   1.1   hpeyerl 	/*
    238   1.1   hpeyerl 	 * Verify that each component piece exists and record
    239   1.1   hpeyerl 	 * relevant information about it.
    240   1.1   hpeyerl 	 */
    241  1.11   thorpej 	maxsecsize = 0;
    242   1.1   hpeyerl 	minsize = 0;
    243  1.11   thorpej 	for (ix = 0; ix < cs->sc_nccdisks; ix++) {
    244  1.11   thorpej 		vp = ccd->ccd_vpp[ix];
    245   1.1   hpeyerl 		ci = &cs->sc_cinfo[ix];
    246  1.11   thorpej 		ci->ci_vp = vp;
    247  1.11   thorpej 
    248  1.11   thorpej 		/*
    249  1.11   thorpej 		 * Copy in the pathname of the component.
    250  1.11   thorpej 		 */
    251  1.11   thorpej 		bzero(tmppath, sizeof(tmppath));	/* sanity */
    252  1.29  christos 		error = copyinstr(cpaths[ix], tmppath,
    253  1.29  christos 		    MAXPATHLEN, &ci->ci_pathlen);
    254  1.29  christos 		if (error) {
    255  1.11   thorpej #ifdef DEBUG
    256  1.11   thorpej 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    257  1.35  christos 				printf("%s: can't copy path, error = %d\n",
    258  1.23   thorpej 				    cs->sc_xname, error);
    259  1.11   thorpej #endif
    260  1.11   thorpej 			free(cs->sc_cinfo, M_DEVBUF);
    261  1.11   thorpej 			return (error);
    262  1.11   thorpej 		}
    263  1.11   thorpej 		ci->ci_path = malloc(ci->ci_pathlen, M_DEVBUF, M_WAITOK);
    264  1.11   thorpej 		bcopy(tmppath, ci->ci_path, ci->ci_pathlen);
    265  1.11   thorpej 
    266  1.11   thorpej 		/*
    267  1.11   thorpej 		 * XXX: Cache the component's dev_t.
    268  1.11   thorpej 		 */
    269  1.29  christos 		if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0) {
    270  1.11   thorpej #ifdef DEBUG
    271  1.11   thorpej 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    272  1.35  christos 				printf("%s: %s: getattr failed %s = %d\n",
    273  1.23   thorpej 				    cs->sc_xname, ci->ci_path,
    274  1.11   thorpej 				    "error", error);
    275  1.11   thorpej #endif
    276  1.11   thorpej 			free(ci->ci_path, M_DEVBUF);
    277  1.11   thorpej 			free(cs->sc_cinfo, M_DEVBUF);
    278  1.11   thorpej 			return (error);
    279  1.11   thorpej 		}
    280  1.11   thorpej 		ci->ci_dev = va.va_rdev;
    281  1.11   thorpej 
    282   1.3   hpeyerl 		/*
    283  1.11   thorpej 		 * Get partition information for the component.
    284   1.3   hpeyerl 		 */
    285  1.29  christos 		error = VOP_IOCTL(vp, DIOCGPART, (caddr_t)&dpart,
    286  1.29  christos 		    FREAD, p->p_ucred, p);
    287  1.29  christos 		if (error) {
    288  1.11   thorpej #ifdef DEBUG
    289  1.11   thorpej 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    290  1.35  christos 				 printf("%s: %s: ioctl failed, error = %d\n",
    291  1.23   thorpej 				     cs->sc_xname, ci->ci_path, error);
    292  1.11   thorpej #endif
    293  1.11   thorpej 			free(ci->ci_path, M_DEVBUF);
    294  1.11   thorpej 			free(cs->sc_cinfo, M_DEVBUF);
    295  1.11   thorpej 			return (error);
    296  1.11   thorpej 		}
    297   1.7       cgd 
    298  1.11   thorpej 		/*
    299  1.11   thorpej 		 * Calculate the size, truncating to an interleave
    300  1.11   thorpej 		 * boundary if necessary.
    301  1.11   thorpej 		 */
    302  1.46   thorpej 		maxsecsize =
    303  1.46   thorpej 		    ((dpart.disklab->d_secsize > maxsecsize) ?
    304  1.46   thorpej 		    dpart.disklab->d_secsize : maxsecsize);
    305  1.46   thorpej 		size = dpart.part->p_size;
    306   1.1   hpeyerl 		if (cs->sc_ileave > 1)
    307   1.1   hpeyerl 			size -= size % cs->sc_ileave;
    308  1.11   thorpej 
    309   1.1   hpeyerl 		if (size == 0) {
    310  1.11   thorpej #ifdef DEBUG
    311  1.11   thorpej 			if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    312  1.35  christos 				printf("%s: %s: size == 0\n",
    313  1.23   thorpej 				    cs->sc_xname, ci->ci_path);
    314  1.11   thorpej #endif
    315  1.11   thorpej 			free(ci->ci_path, M_DEVBUF);
    316  1.11   thorpej 			free(cs->sc_cinfo, M_DEVBUF);
    317  1.11   thorpej 			return (ENODEV);
    318   1.3   hpeyerl 		}
    319  1.11   thorpej 
    320   1.1   hpeyerl 		if (minsize == 0 || size < minsize)
    321   1.1   hpeyerl 			minsize = size;
    322   1.1   hpeyerl 		ci->ci_size = size;
    323   1.1   hpeyerl 		cs->sc_size += size;
    324   1.1   hpeyerl 	}
    325  1.11   thorpej 
    326  1.11   thorpej 	/*
    327  1.11   thorpej 	 * Don't allow the interleave to be smaller than
    328  1.11   thorpej 	 * the biggest component sector.
    329  1.11   thorpej 	 */
    330  1.11   thorpej 	if ((cs->sc_ileave > 0) &&
    331  1.11   thorpej 	    (cs->sc_ileave < (maxsecsize / DEV_BSIZE))) {
    332  1.11   thorpej #ifdef DEBUG
    333  1.11   thorpej 		if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
    334  1.35  christos 			printf("%s: interleave must be at least %d\n",
    335  1.23   thorpej 			    cs->sc_xname, (maxsecsize / DEV_BSIZE));
    336  1.11   thorpej #endif
    337  1.11   thorpej 		free(ci->ci_path, M_DEVBUF);
    338  1.11   thorpej 		free(cs->sc_cinfo, M_DEVBUF);
    339  1.11   thorpej 		return (EINVAL);
    340  1.11   thorpej 	}
    341  1.11   thorpej 
    342   1.1   hpeyerl 	/*
    343   1.1   hpeyerl 	 * If uniform interleave is desired set all sizes to that of
    344   1.1   hpeyerl 	 * the smallest component.
    345   1.1   hpeyerl 	 */
    346   1.3   hpeyerl 	if (ccd->ccd_flags & CCDF_UNIFORM) {
    347   1.1   hpeyerl 		for (ci = cs->sc_cinfo;
    348   1.1   hpeyerl 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    349   1.1   hpeyerl 			ci->ci_size = minsize;
    350  1.24   thorpej 
    351  1.53   thorpej 		cs->sc_size = cs->sc_nccdisks * minsize;
    352   1.1   hpeyerl 	}
    353  1.11   thorpej 
    354  1.11   thorpej 	/*
    355  1.11   thorpej 	 * Construct the interleave table.
    356  1.11   thorpej 	 */
    357  1.11   thorpej 	ccdinterleave(cs, ccd->ccd_unit);
    358  1.11   thorpej 
    359   1.1   hpeyerl 	/*
    360  1.11   thorpej 	 * Create pseudo-geometry based on 1MB cylinders.  It's
    361  1.11   thorpej 	 * pretty close.
    362   1.1   hpeyerl 	 */
    363  1.11   thorpej 	ccg->ccg_secsize = DEV_BSIZE;
    364  1.19   thorpej 	ccg->ccg_ntracks = 1;
    365  1.11   thorpej 	ccg->ccg_nsectors = 1024 * (1024 / ccg->ccg_secsize);
    366  1.11   thorpej 	ccg->ccg_ncylinders = cs->sc_size / ccg->ccg_nsectors;
    367  1.11   thorpej 
    368  1.11   thorpej 	cs->sc_flags |= CCDF_INITED;
    369  1.11   thorpej 	cs->sc_cflags = ccd->ccd_flags;	/* So we can find out later... */
    370  1.11   thorpej 	cs->sc_unit = ccd->ccd_unit;
    371  1.23   thorpej 
    372  1.11   thorpej 	return (0);
    373   1.1   hpeyerl }
    374   1.1   hpeyerl 
    375  1.11   thorpej static void
    376  1.11   thorpej ccdinterleave(cs, unit)
    377   1.1   hpeyerl 	register struct ccd_softc *cs;
    378  1.11   thorpej 	int unit;
    379   1.1   hpeyerl {
    380   1.1   hpeyerl 	register struct ccdcinfo *ci, *smallci;
    381   1.1   hpeyerl 	register struct ccdiinfo *ii;
    382   1.1   hpeyerl 	register daddr_t bn, lbn;
    383   1.1   hpeyerl 	register int ix;
    384   1.1   hpeyerl 	u_long size;
    385   1.1   hpeyerl 
    386   1.1   hpeyerl #ifdef DEBUG
    387   1.3   hpeyerl 	if (ccddebug & CCDB_INIT)
    388  1.35  christos 		printf("ccdinterleave(%p): ileave %d\n", cs, cs->sc_ileave);
    389   1.1   hpeyerl #endif
    390   1.1   hpeyerl 	/*
    391   1.1   hpeyerl 	 * Allocate an interleave table.
    392   1.1   hpeyerl 	 * Chances are this is too big, but we don't care.
    393   1.1   hpeyerl 	 */
    394   1.1   hpeyerl 	size = (cs->sc_nccdisks + 1) * sizeof(struct ccdiinfo);
    395   1.1   hpeyerl 	cs->sc_itable = (struct ccdiinfo *)malloc(size, M_DEVBUF, M_WAITOK);
    396   1.1   hpeyerl 	bzero((caddr_t)cs->sc_itable, size);
    397  1.11   thorpej 
    398   1.1   hpeyerl 	/*
    399   1.1   hpeyerl 	 * Trivial case: no interleave (actually interleave of disk size).
    400  1.11   thorpej 	 * Each table entry represents a single component in its entirety.
    401   1.1   hpeyerl 	 */
    402   1.1   hpeyerl 	if (cs->sc_ileave == 0) {
    403   1.1   hpeyerl 		bn = 0;
    404   1.1   hpeyerl 		ii = cs->sc_itable;
    405  1.11   thorpej 
    406   1.1   hpeyerl 		for (ix = 0; ix < cs->sc_nccdisks; ix++) {
    407  1.19   thorpej 			/* Allocate space for ii_index. */
    408  1.19   thorpej 			ii->ii_index = malloc(sizeof(int), M_DEVBUF, M_WAITOK);
    409   1.1   hpeyerl 			ii->ii_ndisk = 1;
    410   1.1   hpeyerl 			ii->ii_startblk = bn;
    411   1.1   hpeyerl 			ii->ii_startoff = 0;
    412   1.1   hpeyerl 			ii->ii_index[0] = ix;
    413   1.1   hpeyerl 			bn += cs->sc_cinfo[ix].ci_size;
    414   1.1   hpeyerl 			ii++;
    415   1.1   hpeyerl 		}
    416   1.1   hpeyerl 		ii->ii_ndisk = 0;
    417   1.1   hpeyerl #ifdef DEBUG
    418   1.3   hpeyerl 		if (ccddebug & CCDB_INIT)
    419   1.1   hpeyerl 			printiinfo(cs->sc_itable);
    420   1.1   hpeyerl #endif
    421  1.11   thorpej 		return;
    422   1.1   hpeyerl 	}
    423  1.11   thorpej 
    424   1.1   hpeyerl 	/*
    425   1.1   hpeyerl 	 * The following isn't fast or pretty; it doesn't have to be.
    426   1.1   hpeyerl 	 */
    427   1.1   hpeyerl 	size = 0;
    428   1.1   hpeyerl 	bn = lbn = 0;
    429   1.1   hpeyerl 	for (ii = cs->sc_itable; ; ii++) {
    430  1.11   thorpej 		/* Allocate space for ii_index. */
    431  1.19   thorpej 		ii->ii_index = malloc((sizeof(int) * cs->sc_nccdisks),
    432  1.19   thorpej 		    M_DEVBUF, M_WAITOK);
    433  1.11   thorpej 
    434   1.1   hpeyerl 		/*
    435   1.1   hpeyerl 		 * Locate the smallest of the remaining components
    436   1.1   hpeyerl 		 */
    437   1.1   hpeyerl 		smallci = NULL;
    438   1.1   hpeyerl 		for (ci = cs->sc_cinfo;
    439   1.1   hpeyerl 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    440   1.1   hpeyerl 			if (ci->ci_size > size &&
    441   1.1   hpeyerl 			    (smallci == NULL ||
    442   1.1   hpeyerl 			     ci->ci_size < smallci->ci_size))
    443   1.1   hpeyerl 				smallci = ci;
    444  1.11   thorpej 
    445   1.1   hpeyerl 		/*
    446   1.1   hpeyerl 		 * Nobody left, all done
    447   1.1   hpeyerl 		 */
    448   1.1   hpeyerl 		if (smallci == NULL) {
    449   1.1   hpeyerl 			ii->ii_ndisk = 0;
    450   1.1   hpeyerl 			break;
    451   1.1   hpeyerl 		}
    452  1.11   thorpej 
    453   1.1   hpeyerl 		/*
    454   1.1   hpeyerl 		 * Record starting logical block and component offset
    455   1.1   hpeyerl 		 */
    456   1.1   hpeyerl 		ii->ii_startblk = bn / cs->sc_ileave;
    457   1.1   hpeyerl 		ii->ii_startoff = lbn;
    458  1.11   thorpej 
    459   1.1   hpeyerl 		/*
    460   1.1   hpeyerl 		 * Determine how many disks take part in this interleave
    461   1.1   hpeyerl 		 * and record their indices.
    462   1.1   hpeyerl 		 */
    463   1.1   hpeyerl 		ix = 0;
    464   1.1   hpeyerl 		for (ci = cs->sc_cinfo;
    465   1.1   hpeyerl 		     ci < &cs->sc_cinfo[cs->sc_nccdisks]; ci++)
    466   1.1   hpeyerl 			if (ci->ci_size >= smallci->ci_size)
    467   1.1   hpeyerl 				ii->ii_index[ix++] = ci - cs->sc_cinfo;
    468   1.1   hpeyerl 		ii->ii_ndisk = ix;
    469   1.1   hpeyerl 		bn += ix * (smallci->ci_size - size);
    470   1.1   hpeyerl 		lbn = smallci->ci_size / cs->sc_ileave;
    471   1.1   hpeyerl 		size = smallci->ci_size;
    472   1.1   hpeyerl 	}
    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.1   hpeyerl }
    478   1.1   hpeyerl 
    479  1.11   thorpej /* ARGSUSED */
    480  1.11   thorpej int
    481  1.11   thorpej ccdopen(dev, flags, fmt, p)
    482   1.1   hpeyerl 	dev_t dev;
    483  1.11   thorpej 	int flags, fmt;
    484  1.11   thorpej 	struct proc *p;
    485   1.1   hpeyerl {
    486   1.1   hpeyerl 	int unit = ccdunit(dev);
    487  1.11   thorpej 	struct ccd_softc *cs;
    488  1.11   thorpej 	struct disklabel *lp;
    489  1.15   thorpej 	int error = 0, part, pmask;
    490   1.1   hpeyerl 
    491   1.1   hpeyerl #ifdef DEBUG
    492   1.3   hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    493  1.43      fair 		printf("ccdopen(0x%x, 0x%x)\n", dev, flags);
    494   1.1   hpeyerl #endif
    495  1.11   thorpej 	if (unit >= numccd)
    496  1.11   thorpej 		return (ENXIO);
    497  1.11   thorpej 	cs = &ccd_softc[unit];
    498  1.15   thorpej 
    499  1.29  christos 	if ((error = ccdlock(cs)) != 0)
    500  1.15   thorpej 		return (error);
    501  1.15   thorpej 
    502  1.23   thorpej 	lp = cs->sc_dkdev.dk_label;
    503  1.11   thorpej 
    504  1.11   thorpej 	part = DISKPART(dev);
    505  1.11   thorpej 	pmask = (1 << part);
    506  1.11   thorpej 
    507  1.15   thorpej 	/*
    508  1.15   thorpej 	 * If we're initialized, check to see if there are any other
    509  1.15   thorpej 	 * open partitions.  If not, then it's safe to update
    510  1.15   thorpej 	 * the in-core disklabel.
    511  1.15   thorpej 	 */
    512  1.15   thorpej 	if ((cs->sc_flags & CCDF_INITED) && (cs->sc_dkdev.dk_openmask == 0))
    513  1.15   thorpej 		ccdgetdisklabel(dev);
    514  1.15   thorpej 
    515  1.11   thorpej 	/* Check that the partition exists. */
    516  1.27   thorpej 	if (part != RAW_PART) {
    517  1.27   thorpej 		if (((cs->sc_flags & CCDF_INITED) == 0) ||
    518  1.37   thorpej 		    ((part >= lp->d_npartitions) ||
    519  1.27   thorpej 		     (lp->d_partitions[part].p_fstype == FS_UNUSED))) {
    520  1.27   thorpej 			error = ENXIO;
    521  1.27   thorpej 			goto done;
    522  1.27   thorpej 		}
    523  1.15   thorpej 	}
    524  1.11   thorpej 
    525  1.11   thorpej 	/* Prevent our unit from being unconfigured while open. */
    526  1.11   thorpej 	switch (fmt) {
    527  1.11   thorpej 	case S_IFCHR:
    528  1.11   thorpej 		cs->sc_dkdev.dk_copenmask |= pmask;
    529  1.11   thorpej 		break;
    530  1.11   thorpej 
    531  1.11   thorpej 	case S_IFBLK:
    532  1.11   thorpej 		cs->sc_dkdev.dk_bopenmask |= pmask;
    533  1.11   thorpej 		break;
    534  1.11   thorpej 	}
    535  1.11   thorpej 	cs->sc_dkdev.dk_openmask =
    536  1.11   thorpej 	    cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
    537  1.11   thorpej 
    538  1.15   thorpej  done:
    539  1.15   thorpej 	ccdunlock(cs);
    540  1.33   thorpej 	return (error);
    541   1.7       cgd }
    542   1.7       cgd 
    543  1.11   thorpej /* ARGSUSED */
    544  1.11   thorpej int
    545  1.11   thorpej ccdclose(dev, flags, fmt, p)
    546   1.7       cgd 	dev_t dev;
    547  1.11   thorpej 	int flags, fmt;
    548  1.11   thorpej 	struct proc *p;
    549   1.7       cgd {
    550  1.11   thorpej 	int unit = ccdunit(dev);
    551  1.11   thorpej 	struct ccd_softc *cs;
    552  1.15   thorpej 	int error = 0, part;
    553  1.11   thorpej 
    554   1.7       cgd #ifdef DEBUG
    555   1.7       cgd 	if (ccddebug & CCDB_FOLLOW)
    556  1.43      fair 		printf("ccdclose(0x%x, 0x%x)\n", dev, flags);
    557   1.7       cgd #endif
    558  1.11   thorpej 
    559  1.11   thorpej 	if (unit >= numccd)
    560  1.11   thorpej 		return (ENXIO);
    561  1.11   thorpej 	cs = &ccd_softc[unit];
    562  1.15   thorpej 
    563  1.29  christos 	if ((error = ccdlock(cs)) != 0)
    564  1.15   thorpej 		return (error);
    565  1.15   thorpej 
    566  1.11   thorpej 	part = DISKPART(dev);
    567  1.11   thorpej 
    568  1.11   thorpej 	/* ...that much closer to allowing unconfiguration... */
    569  1.11   thorpej 	switch (fmt) {
    570  1.11   thorpej 	case S_IFCHR:
    571  1.11   thorpej 		cs->sc_dkdev.dk_copenmask &= ~(1 << part);
    572  1.11   thorpej 		break;
    573  1.11   thorpej 
    574  1.11   thorpej 	case S_IFBLK:
    575  1.11   thorpej 		cs->sc_dkdev.dk_bopenmask &= ~(1 << part);
    576  1.11   thorpej 		break;
    577  1.11   thorpej 	}
    578  1.11   thorpej 	cs->sc_dkdev.dk_openmask =
    579  1.11   thorpej 	    cs->sc_dkdev.dk_copenmask | cs->sc_dkdev.dk_bopenmask;
    580  1.11   thorpej 
    581  1.15   thorpej 	ccdunlock(cs);
    582   1.7       cgd 	return (0);
    583   1.1   hpeyerl }
    584   1.1   hpeyerl 
    585  1.11   thorpej void
    586   1.1   hpeyerl ccdstrategy(bp)
    587   1.1   hpeyerl 	register struct buf *bp;
    588   1.1   hpeyerl {
    589   1.1   hpeyerl 	register int unit = ccdunit(bp->b_dev);
    590   1.1   hpeyerl 	register struct ccd_softc *cs = &ccd_softc[unit];
    591  1.29  christos 	register int s;
    592  1.11   thorpej 	int wlabel;
    593  1.15   thorpej 	struct disklabel *lp;
    594   1.1   hpeyerl 
    595   1.1   hpeyerl #ifdef DEBUG
    596   1.3   hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    597  1.35  christos 		printf("ccdstrategy(%p): unit %d\n", bp, unit);
    598   1.1   hpeyerl #endif
    599   1.3   hpeyerl 	if ((cs->sc_flags & CCDF_INITED) == 0) {
    600   1.1   hpeyerl 		bp->b_error = ENXIO;
    601   1.1   hpeyerl 		bp->b_flags |= B_ERROR;
    602   1.1   hpeyerl 		goto done;
    603   1.1   hpeyerl 	}
    604  1.11   thorpej 
    605  1.11   thorpej 	/* If it's a nil transfer, wake up the top half now. */
    606  1.11   thorpej 	if (bp->b_bcount == 0)
    607  1.11   thorpej 		goto done;
    608  1.11   thorpej 
    609  1.23   thorpej 	lp = cs->sc_dkdev.dk_label;
    610  1.15   thorpej 
    611  1.11   thorpej 	/*
    612  1.17   thorpej 	 * Do bounds checking and adjust transfer.  If there's an
    613  1.11   thorpej 	 * error, the bounds check will flag that for us.
    614  1.11   thorpej 	 */
    615  1.11   thorpej 	wlabel = cs->sc_flags & (CCDF_WLABEL|CCDF_LABELLING);
    616  1.16   thorpej 	if (DISKPART(bp->b_dev) != RAW_PART)
    617  1.15   thorpej 		if (bounds_check_with_label(bp, lp, wlabel) <= 0)
    618   1.1   hpeyerl 			goto done;
    619  1.11   thorpej 
    620   1.1   hpeyerl 	bp->b_resid = bp->b_bcount;
    621  1.11   thorpej 
    622   1.1   hpeyerl 	/*
    623   1.1   hpeyerl 	 * "Start" the unit.
    624   1.1   hpeyerl 	 */
    625   1.1   hpeyerl 	s = splbio();
    626   1.3   hpeyerl 	ccdstart(cs, bp);
    627   1.1   hpeyerl 	splx(s);
    628   1.1   hpeyerl 	return;
    629   1.1   hpeyerl done:
    630   1.1   hpeyerl 	biodone(bp);
    631   1.1   hpeyerl }
    632   1.1   hpeyerl 
    633  1.11   thorpej static void
    634   1.3   hpeyerl ccdstart(cs, bp)
    635   1.3   hpeyerl 	register struct ccd_softc *cs;
    636   1.3   hpeyerl 	register struct buf *bp;
    637   1.1   hpeyerl {
    638   1.1   hpeyerl 	register long bcount, rcount;
    639  1.24   thorpej 	struct ccdbuf *cbp[4];
    640   1.1   hpeyerl 	caddr_t addr;
    641   1.1   hpeyerl 	daddr_t bn;
    642  1.20   thorpej 	struct partition *pp;
    643   1.1   hpeyerl 
    644   1.1   hpeyerl #ifdef DEBUG
    645   1.3   hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    646  1.35  christos 		printf("ccdstart(%p, %p)\n", cs, bp);
    647   1.1   hpeyerl #endif
    648  1.11   thorpej 
    649  1.23   thorpej 	/* Instrumentation. */
    650  1.23   thorpej 	disk_busy(&cs->sc_dkdev);
    651  1.11   thorpej 
    652   1.1   hpeyerl 	/*
    653  1.17   thorpej 	 * Translate the partition-relative block number to an absolute.
    654   1.1   hpeyerl 	 */
    655  1.20   thorpej 	bn = bp->b_blkno;
    656  1.20   thorpej 	if (DISKPART(bp->b_dev) != RAW_PART) {
    657  1.23   thorpej 		pp = &cs->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
    658  1.20   thorpej 		bn += pp->p_offset;
    659  1.20   thorpej 	}
    660  1.17   thorpej 
    661  1.17   thorpej 	/*
    662  1.17   thorpej 	 * Allocate component buffers and fire off the requests
    663  1.17   thorpej 	 */
    664   1.3   hpeyerl 	addr = bp->b_data;
    665   1.1   hpeyerl 	for (bcount = bp->b_bcount; bcount > 0; bcount -= rcount) {
    666  1.24   thorpej 		ccdbuffer(cs, bp, bn, addr, bcount, cbp);
    667  1.24   thorpej 		rcount = cbp[0]->cb_buf.b_bcount;
    668  1.24   thorpej 		if ((cbp[0]->cb_buf.b_flags & B_READ) == 0)
    669  1.24   thorpej 			cbp[0]->cb_buf.b_vp->v_numoutput++;
    670  1.24   thorpej 		VOP_STRATEGY(&cbp[0]->cb_buf);
    671   1.1   hpeyerl 		bn += btodb(rcount);
    672   1.1   hpeyerl 		addr += rcount;
    673   1.1   hpeyerl 	}
    674   1.1   hpeyerl }
    675   1.1   hpeyerl 
    676   1.1   hpeyerl /*
    677   1.1   hpeyerl  * Build a component buffer header.
    678   1.1   hpeyerl  */
    679  1.24   thorpej static void
    680  1.24   thorpej ccdbuffer(cs, bp, bn, addr, bcount, cbpp)
    681   1.1   hpeyerl 	register struct ccd_softc *cs;
    682   1.1   hpeyerl 	struct buf *bp;
    683   1.1   hpeyerl 	daddr_t bn;
    684   1.1   hpeyerl 	caddr_t addr;
    685   1.1   hpeyerl 	long bcount;
    686  1.24   thorpej 	struct ccdbuf **cbpp;
    687   1.1   hpeyerl {
    688  1.53   thorpej 	register struct ccdcinfo *ci;
    689   1.6       cgd 	register struct ccdbuf *cbp;
    690   1.1   hpeyerl 	register daddr_t cbn, cboff;
    691  1.50   thorpej 	register u_int64_t cbc;
    692  1.36   thorpej 	int ccdisk;
    693   1.1   hpeyerl 
    694   1.1   hpeyerl #ifdef DEBUG
    695   1.3   hpeyerl 	if (ccddebug & CCDB_IO)
    696  1.35  christos 		printf("ccdbuffer(%p, %p, %d, %p, %ld)\n",
    697   1.1   hpeyerl 		       cs, bp, bn, addr, bcount);
    698   1.1   hpeyerl #endif
    699   1.1   hpeyerl 	/*
    700   1.1   hpeyerl 	 * Determine which component bn falls in.
    701   1.1   hpeyerl 	 */
    702   1.1   hpeyerl 	cbn = bn;
    703   1.1   hpeyerl 	cboff = 0;
    704  1.11   thorpej 
    705   1.1   hpeyerl 	/*
    706   1.1   hpeyerl 	 * Serially concatenated
    707   1.1   hpeyerl 	 */
    708   1.1   hpeyerl 	if (cs->sc_ileave == 0) {
    709   1.1   hpeyerl 		register daddr_t sblk;
    710   1.1   hpeyerl 
    711   1.1   hpeyerl 		sblk = 0;
    712  1.36   thorpej 		for (ccdisk = 0, ci = &cs->sc_cinfo[ccdisk];
    713  1.36   thorpej 		    cbn >= sblk + ci->ci_size;
    714  1.36   thorpej 		    ccdisk++, ci = &cs->sc_cinfo[ccdisk])
    715   1.1   hpeyerl 			sblk += ci->ci_size;
    716   1.1   hpeyerl 		cbn -= sblk;
    717   1.1   hpeyerl 	}
    718   1.1   hpeyerl 	/*
    719   1.1   hpeyerl 	 * Interleaved
    720   1.1   hpeyerl 	 */
    721   1.1   hpeyerl 	else {
    722   1.1   hpeyerl 		register struct ccdiinfo *ii;
    723  1.36   thorpej 		int off;
    724   1.1   hpeyerl 
    725   1.1   hpeyerl 		cboff = cbn % cs->sc_ileave;
    726   1.1   hpeyerl 		cbn /= cs->sc_ileave;
    727   1.1   hpeyerl 		for (ii = cs->sc_itable; ii->ii_ndisk; ii++)
    728   1.1   hpeyerl 			if (ii->ii_startblk > cbn)
    729   1.1   hpeyerl 				break;
    730   1.1   hpeyerl 		ii--;
    731   1.1   hpeyerl 		off = cbn - ii->ii_startblk;
    732   1.1   hpeyerl 		if (ii->ii_ndisk == 1) {
    733   1.1   hpeyerl 			ccdisk = ii->ii_index[0];
    734   1.1   hpeyerl 			cbn = ii->ii_startoff + off;
    735   1.1   hpeyerl 		} else {
    736  1.53   thorpej 			ccdisk = ii->ii_index[off % ii->ii_ndisk];
    737  1.53   thorpej 			cbn = ii->ii_startoff + off / ii->ii_ndisk;
    738   1.1   hpeyerl 		}
    739   1.1   hpeyerl 		cbn *= cs->sc_ileave;
    740   1.1   hpeyerl 		ci = &cs->sc_cinfo[ccdisk];
    741   1.1   hpeyerl 	}
    742  1.11   thorpej 
    743   1.1   hpeyerl 	/*
    744   1.1   hpeyerl 	 * Fill in the component buf structure.
    745   1.1   hpeyerl 	 */
    746  1.51   thorpej 	cbp = CCD_GETBUF(cs);
    747   1.6       cgd 	cbp->cb_buf.b_flags = bp->b_flags | B_CALL;
    748  1.29  christos 	cbp->cb_buf.b_iodone = ccdiodone;
    749   1.6       cgd 	cbp->cb_buf.b_proc = bp->b_proc;
    750  1.11   thorpej 	cbp->cb_buf.b_dev = ci->ci_dev;		/* XXX */
    751   1.6       cgd 	cbp->cb_buf.b_blkno = cbn + cboff;
    752   1.6       cgd 	cbp->cb_buf.b_data = addr;
    753  1.11   thorpej 	cbp->cb_buf.b_vp = ci->ci_vp;
    754   1.1   hpeyerl 	if (cs->sc_ileave == 0)
    755  1.50   thorpej 		cbc = dbtob((u_int64_t)(ci->ci_size - cbn));
    756   1.1   hpeyerl 	else
    757  1.50   thorpej 		cbc = dbtob((u_int64_t)(cs->sc_ileave - cboff));
    758  1.50   thorpej 	cbp->cb_buf.b_bcount = cbc < bcount ? cbc : bcount;
    759   1.6       cgd 
    760   1.1   hpeyerl 	/*
    761   1.6       cgd 	 * context for ccdiodone
    762   1.1   hpeyerl 	 */
    763   1.6       cgd 	cbp->cb_obp = bp;
    764  1.36   thorpej 	cbp->cb_unit = cs->sc_unit;
    765  1.36   thorpej 	cbp->cb_comp = ccdisk;
    766   1.6       cgd 
    767  1.24   thorpej 	/* First buffer is dealt with. */
    768  1.24   thorpej 	cbpp[0] = cbp;
    769  1.24   thorpej 
    770   1.1   hpeyerl #ifdef DEBUG
    771   1.3   hpeyerl 	if (ccddebug & CCDB_IO)
    772  1.43      fair 		printf(" dev 0x%x(u%d): cbp %p bn %d addr %p bcnt %ld\n",
    773  1.34  christos 		    ci->ci_dev, ci-cs->sc_cinfo, cbp, cbp->cb_buf.b_blkno,
    774  1.34  christos 		    cbp->cb_buf.b_data, cbp->cb_buf.b_bcount);
    775   1.1   hpeyerl #endif
    776   1.1   hpeyerl }
    777   1.1   hpeyerl 
    778  1.11   thorpej static void
    779   1.3   hpeyerl ccdintr(cs, bp)
    780   1.3   hpeyerl 	register struct ccd_softc *cs;
    781   1.3   hpeyerl 	register struct buf *bp;
    782   1.1   hpeyerl {
    783   1.1   hpeyerl 
    784   1.1   hpeyerl #ifdef DEBUG
    785   1.3   hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    786  1.35  christos 		printf("ccdintr(%p, %p)\n", cs, bp);
    787   1.1   hpeyerl #endif
    788   1.1   hpeyerl 	/*
    789   1.1   hpeyerl 	 * Request is done for better or worse, wakeup the top half.
    790   1.1   hpeyerl 	 */
    791   1.1   hpeyerl 	if (bp->b_flags & B_ERROR)
    792   1.1   hpeyerl 		bp->b_resid = bp->b_bcount;
    793  1.23   thorpej 	disk_unbusy(&cs->sc_dkdev, (bp->b_bcount - bp->b_resid));
    794   1.1   hpeyerl 	biodone(bp);
    795   1.1   hpeyerl }
    796   1.1   hpeyerl 
    797   1.1   hpeyerl /*
    798  1.11   thorpej  * Called at interrupt time.
    799   1.1   hpeyerl  * Mark the component as done and if all components are done,
    800   1.1   hpeyerl  * take a ccd interrupt.
    801   1.1   hpeyerl  */
    802   1.3   hpeyerl void
    803  1.29  christos ccdiodone(vbp)
    804  1.29  christos 	struct buf *vbp;
    805   1.1   hpeyerl {
    806  1.29  christos 	struct ccdbuf *cbp = (struct ccdbuf *) vbp;
    807   1.6       cgd 	register struct buf *bp = cbp->cb_obp;
    808   1.6       cgd 	register int unit = cbp->cb_unit;
    809  1.23   thorpej 	struct ccd_softc *cs = &ccd_softc[unit];
    810  1.53   thorpej 	int count, s;
    811   1.1   hpeyerl 
    812   1.1   hpeyerl 	s = splbio();
    813   1.1   hpeyerl #ifdef DEBUG
    814   1.3   hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    815  1.35  christos 		printf("ccdiodone(%p)\n", cbp);
    816   1.3   hpeyerl 	if (ccddebug & CCDB_IO) {
    817  1.53   thorpej 		printf("ccdiodone: bp %p bcount %ld resid %ld\n",
    818  1.53   thorpej 		       bp, bp->b_bcount, bp->b_resid);
    819  1.43      fair 		printf(" dev 0x%x(u%d), cbp %p bn %d addr %p bcnt %ld\n",
    820   1.6       cgd 		       cbp->cb_buf.b_dev, cbp->cb_comp, cbp,
    821   1.6       cgd 		       cbp->cb_buf.b_blkno, cbp->cb_buf.b_data,
    822   1.6       cgd 		       cbp->cb_buf.b_bcount);
    823   1.1   hpeyerl 	}
    824   1.1   hpeyerl #endif
    825   1.1   hpeyerl 
    826   1.6       cgd 	if (cbp->cb_buf.b_flags & B_ERROR) {
    827  1.53   thorpej 		bp->b_flags |= B_ERROR;
    828  1.53   thorpej 		bp->b_error = cbp->cb_buf.b_error ?
    829  1.53   thorpej 		    cbp->cb_buf.b_error : EIO;
    830  1.24   thorpej 
    831  1.53   thorpej 		printf("%s: error %d on component %d\n",
    832  1.53   thorpej 		       cs->sc_xname, bp->b_error, cbp->cb_comp);
    833   1.1   hpeyerl 	}
    834   1.6       cgd 	count = cbp->cb_buf.b_bcount;
    835  1.51   thorpej 	CCD_PUTBUF(cs, cbp);
    836   1.1   hpeyerl 
    837   1.1   hpeyerl 	/*
    838   1.1   hpeyerl 	 * If all done, "interrupt".
    839  1.53   thorpej 	 */
    840  1.53   thorpej 	bp->b_resid -= count;
    841  1.53   thorpej 	if (bp->b_resid < 0)
    842  1.53   thorpej 		panic("ccdiodone: count");
    843  1.53   thorpej 	if (bp->b_resid == 0)
    844  1.53   thorpej 		ccdintr(&ccd_softc[unit], bp);
    845   1.1   hpeyerl 	splx(s);
    846   1.1   hpeyerl }
    847   1.1   hpeyerl 
    848  1.11   thorpej /* ARGSUSED */
    849  1.10   mycroft int
    850  1.11   thorpej ccdread(dev, uio, flags)
    851   1.3   hpeyerl 	dev_t dev;
    852   1.3   hpeyerl 	struct uio *uio;
    853  1.11   thorpej 	int flags;
    854   1.3   hpeyerl {
    855  1.11   thorpej 	int unit = ccdunit(dev);
    856  1.11   thorpej 	struct ccd_softc *cs;
    857   1.3   hpeyerl 
    858   1.3   hpeyerl #ifdef DEBUG
    859   1.3   hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    860  1.43      fair 		printf("ccdread(0x%x, %p)\n", dev, uio);
    861   1.3   hpeyerl #endif
    862  1.11   thorpej 	if (unit >= numccd)
    863  1.11   thorpej 		return (ENXIO);
    864  1.11   thorpej 	cs = &ccd_softc[unit];
    865  1.11   thorpej 
    866  1.11   thorpej 	if ((cs->sc_flags & CCDF_INITED) == 0)
    867  1.11   thorpej 		return (ENXIO);
    868  1.11   thorpej 
    869  1.11   thorpej 	/*
    870  1.11   thorpej 	 * XXX: It's not clear that using minphys() is completely safe,
    871  1.11   thorpej 	 * in particular, for raw I/O.  Underlying devices might have some
    872  1.11   thorpej 	 * non-obvious limits, because of the copy to user-space.
    873  1.11   thorpej 	 */
    874  1.10   mycroft 	return (physio(ccdstrategy, NULL, dev, B_READ, minphys, uio));
    875   1.3   hpeyerl }
    876   1.3   hpeyerl 
    877  1.11   thorpej /* ARGSUSED */
    878  1.10   mycroft int
    879  1.11   thorpej ccdwrite(dev, uio, flags)
    880   1.3   hpeyerl 	dev_t dev;
    881   1.3   hpeyerl 	struct uio *uio;
    882  1.11   thorpej 	int flags;
    883   1.3   hpeyerl {
    884  1.11   thorpej 	int unit = ccdunit(dev);
    885  1.11   thorpej 	struct ccd_softc *cs;
    886   1.3   hpeyerl 
    887   1.3   hpeyerl #ifdef DEBUG
    888   1.3   hpeyerl 	if (ccddebug & CCDB_FOLLOW)
    889  1.43      fair 		printf("ccdwrite(0x%x, %p)\n", dev, uio);
    890   1.3   hpeyerl #endif
    891  1.11   thorpej 	if (unit >= numccd)
    892  1.11   thorpej 		return (ENXIO);
    893  1.11   thorpej 	cs = &ccd_softc[unit];
    894  1.11   thorpej 
    895  1.11   thorpej 	if ((cs->sc_flags & CCDF_INITED) == 0)
    896  1.11   thorpej 		return (ENXIO);
    897  1.11   thorpej 
    898  1.11   thorpej 	/*
    899  1.11   thorpej 	 * XXX: It's not clear that using minphys() is completely safe,
    900  1.11   thorpej 	 * in particular, for raw I/O.  Underlying devices might have some
    901  1.11   thorpej 	 * non-obvious limits, because of the copy to user-space.
    902  1.11   thorpej 	 */
    903  1.10   mycroft 	return (physio(ccdstrategy, NULL, dev, B_WRITE, minphys, uio));
    904   1.3   hpeyerl }
    905   1.3   hpeyerl 
    906  1.11   thorpej int
    907  1.11   thorpej ccdioctl(dev, cmd, data, flag, p)
    908   1.1   hpeyerl 	dev_t dev;
    909   1.5       cgd 	u_long cmd;
    910   1.1   hpeyerl 	caddr_t data;
    911   1.1   hpeyerl 	int flag;
    912  1.11   thorpej 	struct proc *p;
    913   1.1   hpeyerl {
    914  1.11   thorpej 	int unit = ccdunit(dev);
    915  1.11   thorpej 	int i, j, lookedup = 0, error = 0;
    916  1.39   mycroft 	int part, pmask;
    917  1.11   thorpej 	struct ccd_softc *cs;
    918  1.11   thorpej 	struct ccd_ioctl *ccio = (struct ccd_ioctl *)data;
    919  1.11   thorpej 	struct ccddevice ccd;
    920  1.11   thorpej 	char **cpp;
    921  1.11   thorpej 	struct vnode **vpp;
    922  1.11   thorpej 
    923  1.11   thorpej 	if (unit >= numccd)
    924  1.11   thorpej 		return (ENXIO);
    925  1.11   thorpej 	cs = &ccd_softc[unit];
    926  1.11   thorpej 
    927  1.11   thorpej 	bzero(&ccd, sizeof(ccd));
    928  1.11   thorpej 
    929  1.41   thorpej 	/* Must be open for writes for these commands... */
    930  1.41   thorpej 	switch (cmd) {
    931  1.41   thorpej 	case CCDIOCSET:
    932  1.41   thorpej 	case CCDIOCCLR:
    933  1.41   thorpej 	case DIOCSDINFO:
    934  1.41   thorpej 	case DIOCWDINFO:
    935  1.41   thorpej 	case DIOCWLABEL:
    936  1.41   thorpej 		if ((flag & FWRITE) == 0)
    937  1.41   thorpej 			return (EBADF);
    938  1.41   thorpej 	}
    939  1.41   thorpej 
    940  1.41   thorpej 	/* Must be initialized for these... */
    941  1.41   thorpej 	switch (cmd) {
    942  1.41   thorpej 	case CCDIOCCLR:
    943  1.42    kleink 	case DIOCGDINFO:
    944  1.42    kleink 	case DIOCSDINFO:
    945  1.42    kleink 	case DIOCWDINFO:
    946  1.42    kleink 	case DIOCGPART:
    947  1.41   thorpej 	case DIOCWLABEL:
    948  1.44   thorpej 	case DIOCGDEFLABEL:
    949  1.41   thorpej 		if ((cs->sc_flags & CCDF_INITED) == 0)
    950  1.41   thorpej 			return (ENXIO);
    951  1.41   thorpej 	}
    952  1.41   thorpej 
    953  1.11   thorpej 	switch (cmd) {
    954  1.11   thorpej 	case CCDIOCSET:
    955  1.11   thorpej 		if (cs->sc_flags & CCDF_INITED)
    956  1.11   thorpej 			return (EBUSY);
    957  1.11   thorpej 
    958  1.29  christos 		if ((error = ccdlock(cs)) != 0)
    959  1.15   thorpej 			return (error);
    960  1.54   thorpej 
    961  1.54   thorpej 		/* Validate the flags. */
    962  1.54   thorpej 		if ((ccio->ccio_flags & CCDF_USERMASK) != ccio->ccio_flags)
    963  1.54   thorpej 			return (EINVAL);
    964  1.15   thorpej 
    965  1.11   thorpej 		/* Fill in some important bits. */
    966  1.11   thorpej 		ccd.ccd_unit = unit;
    967  1.11   thorpej 		ccd.ccd_interleave = ccio->ccio_ileave;
    968  1.11   thorpej 		ccd.ccd_flags = ccio->ccio_flags & CCDF_USERMASK;
    969  1.11   thorpej 
    970  1.11   thorpej 		/*
    971  1.11   thorpej 		 * Allocate space for and copy in the array of
    972  1.11   thorpej 		 * componet pathnames and device numbers.
    973  1.11   thorpej 		 */
    974  1.11   thorpej 		cpp = malloc(ccio->ccio_ndisks * sizeof(char *),
    975  1.11   thorpej 		    M_DEVBUF, M_WAITOK);
    976  1.11   thorpej 		vpp = malloc(ccio->ccio_ndisks * sizeof(struct vnode *),
    977  1.11   thorpej 		    M_DEVBUF, M_WAITOK);
    978  1.11   thorpej 
    979  1.11   thorpej 		error = copyin((caddr_t)ccio->ccio_disks, (caddr_t)cpp,
    980  1.11   thorpej 		    ccio->ccio_ndisks * sizeof(char **));
    981  1.11   thorpej 		if (error) {
    982  1.11   thorpej 			free(vpp, M_DEVBUF);
    983  1.11   thorpej 			free(cpp, M_DEVBUF);
    984  1.15   thorpej 			ccdunlock(cs);
    985  1.11   thorpej 			return (error);
    986  1.11   thorpej 		}
    987  1.11   thorpej 
    988  1.11   thorpej #ifdef DEBUG
    989  1.11   thorpej 		if (ccddebug & CCDB_INIT)
    990  1.11   thorpej 			for (i = 0; i < ccio->ccio_ndisks; ++i)
    991  1.35  christos 				printf("ccdioctl: component %d: 0x%p\n",
    992  1.11   thorpej 				    i, cpp[i]);
    993  1.11   thorpej #endif
    994  1.11   thorpej 
    995  1.11   thorpej 		for (i = 0; i < ccio->ccio_ndisks; ++i) {
    996  1.11   thorpej #ifdef DEBUG
    997  1.11   thorpej 			if (ccddebug & CCDB_INIT)
    998  1.35  christos 				printf("ccdioctl: lookedup = %d\n", lookedup);
    999  1.11   thorpej #endif
   1000  1.29  christos 			if ((error = ccdlookup(cpp[i], p, &vpp[i])) != 0) {
   1001  1.11   thorpej 				for (j = 0; j < lookedup; ++j)
   1002  1.12   thorpej 					(void)vn_close(vpp[j], FREAD|FWRITE,
   1003  1.11   thorpej 					    p->p_ucred, p);
   1004  1.11   thorpej 				free(vpp, M_DEVBUF);
   1005  1.11   thorpej 				free(cpp, M_DEVBUF);
   1006  1.15   thorpej 				ccdunlock(cs);
   1007  1.11   thorpej 				return (error);
   1008  1.11   thorpej 			}
   1009  1.11   thorpej 			++lookedup;
   1010  1.11   thorpej 		}
   1011  1.11   thorpej 		ccd.ccd_cpp = cpp;
   1012  1.11   thorpej 		ccd.ccd_vpp = vpp;
   1013  1.11   thorpej 		ccd.ccd_ndev = ccio->ccio_ndisks;
   1014  1.11   thorpej 
   1015  1.11   thorpej 		/*
   1016  1.11   thorpej 		 * Initialize the ccd.  Fills in the softc for us.
   1017  1.11   thorpej 		 */
   1018  1.29  christos 		if ((error = ccdinit(&ccd, cpp, p)) != 0) {
   1019  1.11   thorpej 			for (j = 0; j < lookedup; ++j)
   1020  1.18   thorpej 				(void)vn_close(vpp[j], FREAD|FWRITE,
   1021  1.11   thorpej 				    p->p_ucred, p);
   1022  1.11   thorpej 			bzero(&ccd_softc[unit], sizeof(struct ccd_softc));
   1023  1.11   thorpej 			free(vpp, M_DEVBUF);
   1024  1.11   thorpej 			free(cpp, M_DEVBUF);
   1025  1.15   thorpej 			ccdunlock(cs);
   1026  1.11   thorpej 			return (error);
   1027  1.11   thorpej 		}
   1028  1.11   thorpej 
   1029  1.11   thorpej 		/*
   1030  1.11   thorpej 		 * The ccd has been successfully initialized, so
   1031  1.23   thorpej 		 * we can place it into the array.  Don't try to
   1032  1.23   thorpej 		 * read the disklabel until the disk has been attached,
   1033  1.23   thorpej 		 * because space for the disklabel is allocated
   1034  1.23   thorpej 		 * in disk_attach();
   1035  1.11   thorpej 		 */
   1036  1.11   thorpej 		bcopy(&ccd, &ccddevs[unit], sizeof(ccd));
   1037  1.11   thorpej 		ccio->ccio_unit = unit;
   1038  1.11   thorpej 		ccio->ccio_size = cs->sc_size;
   1039  1.23   thorpej 
   1040  1.23   thorpej 		/* Attach the disk. */
   1041  1.23   thorpej 		cs->sc_dkdev.dk_name = cs->sc_xname;
   1042  1.23   thorpej 		disk_attach(&cs->sc_dkdev);
   1043  1.23   thorpej 
   1044  1.51   thorpej 		/* Initialize the component buffer pool. */
   1045  1.51   thorpej 		pool_init(&cs->sc_cbufpool, sizeof(struct ccdbuf), 0,
   1046  1.51   thorpej 		    0, 0, "ccdpl", 0, NULL, NULL, M_DEVBUF);
   1047  1.51   thorpej 
   1048  1.23   thorpej 		/* Try and read the disklabel. */
   1049  1.11   thorpej 		ccdgetdisklabel(dev);
   1050  1.11   thorpej 
   1051  1.15   thorpej 		ccdunlock(cs);
   1052  1.15   thorpej 
   1053  1.11   thorpej 		break;
   1054  1.11   thorpej 
   1055  1.11   thorpej 	case CCDIOCCLR:
   1056  1.29  christos 		if ((error = ccdlock(cs)) != 0)
   1057  1.15   thorpej 			return (error);
   1058  1.15   thorpej 
   1059  1.11   thorpej 		/*
   1060  1.11   thorpej 		 * Don't unconfigure if any other partitions are open
   1061  1.11   thorpej 		 * or if both the character and block flavors of this
   1062  1.11   thorpej 		 * partition are open.
   1063  1.11   thorpej 		 */
   1064  1.11   thorpej 		part = DISKPART(dev);
   1065  1.11   thorpej 		pmask = (1 << part);
   1066  1.11   thorpej 		if ((cs->sc_dkdev.dk_openmask & ~pmask) ||
   1067  1.11   thorpej 		    ((cs->sc_dkdev.dk_bopenmask & pmask) &&
   1068  1.15   thorpej 		    (cs->sc_dkdev.dk_copenmask & pmask))) {
   1069  1.15   thorpej 			ccdunlock(cs);
   1070  1.11   thorpej 			return (EBUSY);
   1071  1.15   thorpej 		}
   1072  1.11   thorpej 
   1073  1.11   thorpej 		/*
   1074  1.11   thorpej 		 * Free ccd_softc information and clear entry.
   1075  1.11   thorpej 		 */
   1076  1.22   thorpej 
   1077  1.22   thorpej 		/* Close the components and free their pathnames. */
   1078  1.11   thorpej 		for (i = 0; i < cs->sc_nccdisks; ++i) {
   1079  1.11   thorpej 			/*
   1080  1.11   thorpej 			 * XXX: this close could potentially fail and
   1081  1.11   thorpej 			 * cause Bad Things.  Maybe we need to force
   1082  1.11   thorpej 			 * the close to happen?
   1083  1.11   thorpej 			 */
   1084  1.11   thorpej #ifdef DEBUG
   1085  1.11   thorpej 			if (ccddebug & CCDB_VNODE)
   1086  1.11   thorpej 				vprint("CCDIOCCLR: vnode info",
   1087  1.11   thorpej 				    cs->sc_cinfo[i].ci_vp);
   1088  1.11   thorpej #endif
   1089  1.11   thorpej 			(void)vn_close(cs->sc_cinfo[i].ci_vp, FREAD|FWRITE,
   1090  1.11   thorpej 			    p->p_ucred, p);
   1091  1.11   thorpej 			free(cs->sc_cinfo[i].ci_path, M_DEVBUF);
   1092  1.38   thorpej 		}
   1093  1.38   thorpej 
   1094  1.22   thorpej 		/* Free interleave index. */
   1095  1.22   thorpej 		for (i = 0; cs->sc_itable[i].ii_ndisk; ++i)
   1096  1.19   thorpej 			free(cs->sc_itable[i].ii_index, M_DEVBUF);
   1097  1.22   thorpej 
   1098  1.22   thorpej 		/* Free component info and interleave table. */
   1099  1.11   thorpej 		free(cs->sc_cinfo, M_DEVBUF);
   1100  1.11   thorpej 		free(cs->sc_itable, M_DEVBUF);
   1101  1.21   thorpej 		cs->sc_flags &= ~CCDF_INITED;
   1102  1.11   thorpej 
   1103  1.11   thorpej 		/*
   1104  1.11   thorpej 		 * Free ccddevice information and clear entry.
   1105  1.11   thorpej 		 */
   1106  1.11   thorpej 		free(ccddevs[unit].ccd_cpp, M_DEVBUF);
   1107  1.11   thorpej 		free(ccddevs[unit].ccd_vpp, M_DEVBUF);
   1108  1.11   thorpej 		bcopy(&ccd, &ccddevs[unit], sizeof(ccd));
   1109  1.51   thorpej 
   1110  1.51   thorpej 		/* Free the component buffer pool. */
   1111  1.51   thorpej 		pool_destroy(&cs->sc_cbufpool);
   1112  1.15   thorpej 
   1113  1.23   thorpej 		/* Detatch the disk. */
   1114  1.26   hpeyerl 		disk_detach(&cs->sc_dkdev);
   1115  1.23   thorpej 
   1116  1.15   thorpej 		ccdunlock(cs);
   1117  1.15   thorpej 
   1118  1.11   thorpej 		break;
   1119  1.11   thorpej 
   1120  1.11   thorpej 	case DIOCGDINFO:
   1121  1.23   thorpej 		*(struct disklabel *)data = *(cs->sc_dkdev.dk_label);
   1122  1.11   thorpej 		break;
   1123  1.11   thorpej 
   1124  1.11   thorpej 	case DIOCGPART:
   1125  1.23   thorpej 		((struct partinfo *)data)->disklab = cs->sc_dkdev.dk_label;
   1126  1.11   thorpej 		((struct partinfo *)data)->part =
   1127  1.23   thorpej 		    &cs->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
   1128  1.11   thorpej 		break;
   1129  1.11   thorpej 
   1130  1.11   thorpej 	case DIOCWDINFO:
   1131  1.11   thorpej 	case DIOCSDINFO:
   1132  1.29  christos 		if ((error = ccdlock(cs)) != 0)
   1133  1.15   thorpej 			return (error);
   1134  1.15   thorpej 
   1135  1.11   thorpej 		cs->sc_flags |= CCDF_LABELLING;
   1136  1.11   thorpej 
   1137  1.23   thorpej 		error = setdisklabel(cs->sc_dkdev.dk_label,
   1138  1.23   thorpej 		    (struct disklabel *)data, 0, cs->sc_dkdev.dk_cpulabel);
   1139  1.11   thorpej 		if (error == 0) {
   1140  1.11   thorpej 			if (cmd == DIOCWDINFO)
   1141  1.11   thorpej 				error = writedisklabel(CCDLABELDEV(dev),
   1142  1.23   thorpej 				    ccdstrategy, cs->sc_dkdev.dk_label,
   1143  1.23   thorpej 				    cs->sc_dkdev.dk_cpulabel);
   1144  1.11   thorpej 		}
   1145  1.11   thorpej 
   1146  1.11   thorpej 		cs->sc_flags &= ~CCDF_LABELLING;
   1147  1.11   thorpej 
   1148  1.15   thorpej 		ccdunlock(cs);
   1149  1.15   thorpej 
   1150  1.11   thorpej 		if (error)
   1151  1.11   thorpej 			return (error);
   1152  1.11   thorpej 		break;
   1153  1.11   thorpej 
   1154  1.11   thorpej 	case DIOCWLABEL:
   1155  1.11   thorpej 		if (*(int *)data != 0)
   1156  1.11   thorpej 			cs->sc_flags |= CCDF_WLABEL;
   1157  1.11   thorpej 		else
   1158  1.11   thorpej 			cs->sc_flags &= ~CCDF_WLABEL;
   1159  1.11   thorpej 		break;
   1160  1.11   thorpej 
   1161  1.44   thorpej 	case DIOCGDEFLABEL:
   1162  1.44   thorpej 		ccdgetdefaultlabel(cs, (struct disklabel *)data);
   1163  1.44   thorpej 		break;
   1164  1.44   thorpej 
   1165  1.11   thorpej 	default:
   1166  1.11   thorpej 		return (ENOTTY);
   1167  1.11   thorpej 	}
   1168  1.11   thorpej 
   1169  1.11   thorpej 	return (0);
   1170   1.1   hpeyerl }
   1171   1.1   hpeyerl 
   1172  1.11   thorpej int
   1173   1.1   hpeyerl ccdsize(dev)
   1174   1.1   hpeyerl 	dev_t dev;
   1175   1.1   hpeyerl {
   1176  1.11   thorpej 	struct ccd_softc *cs;
   1177  1.40   thorpej 	struct disklabel *lp;
   1178  1.40   thorpej 	int part, unit, omask, size;
   1179  1.40   thorpej 
   1180  1.40   thorpej 	unit = ccdunit(dev);
   1181  1.40   thorpej 	if (unit >= numccd)
   1182  1.40   thorpej 		return (-1);
   1183  1.40   thorpej 	cs = &ccd_softc[unit];
   1184  1.11   thorpej 
   1185  1.40   thorpej 	if ((cs->sc_flags & CCDF_INITED) == 0)
   1186  1.11   thorpej 		return (-1);
   1187  1.11   thorpej 
   1188  1.11   thorpej 	part = DISKPART(dev);
   1189  1.40   thorpej 	omask = cs->sc_dkdev.dk_openmask & (1 << part);
   1190  1.40   thorpej 	lp = cs->sc_dkdev.dk_label;
   1191  1.11   thorpej 
   1192  1.40   thorpej 	if (omask == 0 && ccdopen(dev, 0, S_IFBLK, curproc))
   1193  1.11   thorpej 		return (-1);
   1194  1.11   thorpej 
   1195  1.40   thorpej 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
   1196  1.11   thorpej 		size = -1;
   1197  1.11   thorpej 	else
   1198  1.40   thorpej 		size = lp->d_partitions[part].p_size *
   1199  1.40   thorpej 		    (lp->d_secsize / DEV_BSIZE);
   1200  1.11   thorpej 
   1201  1.40   thorpej 	if (omask == 0 && ccdclose(dev, 0, S_IFBLK, curproc))
   1202  1.11   thorpej 		return (-1);
   1203   1.1   hpeyerl 
   1204  1.11   thorpej 	return (size);
   1205   1.1   hpeyerl }
   1206   1.1   hpeyerl 
   1207   1.9       cgd int
   1208   1.9       cgd ccddump(dev, blkno, va, size)
   1209   1.9       cgd 	dev_t dev;
   1210   1.9       cgd 	daddr_t blkno;
   1211   1.9       cgd 	caddr_t va;
   1212   1.9       cgd 	size_t size;
   1213   1.1   hpeyerl {
   1214   1.9       cgd 
   1215   1.9       cgd 	/* Not implemented. */
   1216   1.9       cgd 	return ENXIO;
   1217  1.11   thorpej }
   1218  1.11   thorpej 
   1219  1.11   thorpej /*
   1220  1.11   thorpej  * Lookup the provided name in the filesystem.  If the file exists,
   1221  1.11   thorpej  * is a valid block device, and isn't being used by anyone else,
   1222  1.11   thorpej  * set *vpp to the file's vnode.
   1223  1.11   thorpej  */
   1224  1.11   thorpej static int
   1225  1.11   thorpej ccdlookup(path, p, vpp)
   1226  1.11   thorpej 	char *path;
   1227  1.11   thorpej 	struct proc *p;
   1228  1.11   thorpej 	struct vnode **vpp;	/* result */
   1229  1.11   thorpej {
   1230  1.11   thorpej 	struct nameidata nd;
   1231  1.11   thorpej 	struct vnode *vp;
   1232  1.11   thorpej 	struct vattr va;
   1233  1.11   thorpej 	int error;
   1234  1.11   thorpej 
   1235  1.11   thorpej 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p);
   1236  1.29  christos 	if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
   1237  1.11   thorpej #ifdef DEBUG
   1238  1.29  christos 		if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
   1239  1.35  christos 			printf("ccdlookup: vn_open error = %d\n", error);
   1240  1.11   thorpej #endif
   1241  1.11   thorpej 		return (error);
   1242  1.11   thorpej 	}
   1243  1.11   thorpej 	vp = nd.ni_vp;
   1244  1.11   thorpej 
   1245  1.11   thorpej 	if (vp->v_usecount > 1) {
   1246  1.49      ross 		VOP_UNLOCK(vp, 0);
   1247  1.11   thorpej 		(void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
   1248  1.11   thorpej 		return (EBUSY);
   1249  1.11   thorpej 	}
   1250  1.11   thorpej 
   1251  1.29  christos 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0) {
   1252  1.11   thorpej #ifdef DEBUG
   1253  1.29  christos 		if (ccddebug & (CCDB_FOLLOW|CCDB_INIT))
   1254  1.35  christos 			printf("ccdlookup: getattr error = %d\n", error);
   1255  1.11   thorpej #endif
   1256  1.49      ross 		VOP_UNLOCK(vp, 0);
   1257  1.11   thorpej 		(void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
   1258  1.11   thorpej 		return (error);
   1259  1.11   thorpej 	}
   1260  1.11   thorpej 
   1261  1.11   thorpej 	/* XXX: eventually we should handle VREG, too. */
   1262  1.11   thorpej 	if (va.va_type != VBLK) {
   1263  1.49      ross 		VOP_UNLOCK(vp, 0);
   1264  1.11   thorpej 		(void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
   1265  1.11   thorpej 		return (ENOTBLK);
   1266  1.11   thorpej 	}
   1267  1.11   thorpej 
   1268  1.11   thorpej #ifdef DEBUG
   1269  1.11   thorpej 	if (ccddebug & CCDB_VNODE)
   1270  1.11   thorpej 		vprint("ccdlookup: vnode info", vp);
   1271  1.11   thorpej #endif
   1272  1.11   thorpej 
   1273  1.49      ross 	VOP_UNLOCK(vp, 0);
   1274  1.11   thorpej 	*vpp = vp;
   1275  1.11   thorpej 	return (0);
   1276  1.11   thorpej }
   1277  1.11   thorpej 
   1278  1.11   thorpej static void
   1279  1.44   thorpej ccdgetdefaultlabel(cs, lp)
   1280  1.44   thorpej 	struct ccd_softc *cs;
   1281  1.44   thorpej 	struct disklabel *lp;
   1282  1.11   thorpej {
   1283  1.11   thorpej 	struct ccdgeom *ccg = &cs->sc_geom;
   1284  1.11   thorpej 
   1285  1.11   thorpej 	bzero(lp, sizeof(*lp));
   1286  1.11   thorpej 
   1287  1.11   thorpej 	lp->d_secperunit = cs->sc_size;
   1288  1.11   thorpej 	lp->d_secsize = ccg->ccg_secsize;
   1289  1.11   thorpej 	lp->d_nsectors = ccg->ccg_nsectors;
   1290  1.11   thorpej 	lp->d_ntracks = ccg->ccg_ntracks;
   1291  1.11   thorpej 	lp->d_ncylinders = ccg->ccg_ncylinders;
   1292  1.19   thorpej 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1293  1.11   thorpej 
   1294  1.11   thorpej 	strncpy(lp->d_typename, "ccd", sizeof(lp->d_typename));
   1295  1.11   thorpej 	lp->d_type = DTYPE_CCD;
   1296  1.11   thorpej 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1297  1.11   thorpej 	lp->d_rpm = 3600;
   1298  1.11   thorpej 	lp->d_interleave = 1;
   1299  1.11   thorpej 	lp->d_flags = 0;
   1300  1.11   thorpej 
   1301  1.11   thorpej 	lp->d_partitions[RAW_PART].p_offset = 0;
   1302  1.11   thorpej 	lp->d_partitions[RAW_PART].p_size = cs->sc_size;
   1303  1.11   thorpej 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1304  1.11   thorpej 	lp->d_npartitions = RAW_PART + 1;
   1305  1.11   thorpej 
   1306  1.11   thorpej 	lp->d_magic = DISKMAGIC;
   1307  1.11   thorpej 	lp->d_magic2 = DISKMAGIC;
   1308  1.23   thorpej 	lp->d_checksum = dkcksum(cs->sc_dkdev.dk_label);
   1309  1.44   thorpej }
   1310  1.44   thorpej 
   1311  1.44   thorpej /*
   1312  1.44   thorpej  * Read the disklabel from the ccd.  If one is not present, fake one
   1313  1.44   thorpej  * up.
   1314  1.44   thorpej  */
   1315  1.44   thorpej static void
   1316  1.44   thorpej ccdgetdisklabel(dev)
   1317  1.44   thorpej 	dev_t dev;
   1318  1.44   thorpej {
   1319  1.44   thorpej 	int unit = ccdunit(dev);
   1320  1.44   thorpej 	struct ccd_softc *cs = &ccd_softc[unit];
   1321  1.44   thorpej 	char *errstring;
   1322  1.44   thorpej 	struct disklabel *lp = cs->sc_dkdev.dk_label;
   1323  1.44   thorpej 	struct cpu_disklabel *clp = cs->sc_dkdev.dk_cpulabel;
   1324  1.44   thorpej 
   1325  1.44   thorpej 	bzero(clp, sizeof(*clp));
   1326  1.44   thorpej 
   1327  1.44   thorpej 	ccdgetdefaultlabel(cs, lp);
   1328  1.11   thorpej 
   1329  1.11   thorpej 	/*
   1330  1.11   thorpej 	 * Call the generic disklabel extraction routine.
   1331  1.11   thorpej 	 */
   1332  1.29  christos 	errstring = readdisklabel(CCDLABELDEV(dev), ccdstrategy,
   1333  1.29  christos 	    cs->sc_dkdev.dk_label, cs->sc_dkdev.dk_cpulabel);
   1334  1.29  christos 	if (errstring)
   1335  1.11   thorpej 		ccdmakedisklabel(cs);
   1336  1.47     enami 	else {
   1337  1.47     enami 		int i;
   1338  1.47     enami 		struct partition *pp;
   1339  1.47     enami 
   1340  1.47     enami 		/*
   1341  1.47     enami 		 * Sanity check whether the found disklabel is valid.
   1342  1.47     enami 		 *
   1343  1.47     enami 		 * This is necessary since total size of ccd may vary
   1344  1.47     enami 		 * when an interleave is changed even though exactly
   1345  1.47     enami 		 * same componets are used, and old disklabel may used
   1346  1.47     enami 		 * if that is found.
   1347  1.47     enami 		 */
   1348  1.47     enami 		if (lp->d_secperunit != cs->sc_size)
   1349  1.47     enami 			printf("WARNING: %s: "
   1350  1.47     enami 			    "total sector size in disklabel (%d) != "
   1351  1.47     enami 			    "the size of ccd (%d)\n", cs->sc_xname,
   1352  1.47     enami 			    lp->d_secperunit, cs->sc_size);
   1353  1.47     enami 		for (i = 0; i < lp->d_npartitions; i++) {
   1354  1.47     enami 			pp = &lp->d_partitions[i];
   1355  1.47     enami 			if (pp->p_offset + pp->p_size > cs->sc_size)
   1356  1.48     enami 				printf("WARNING: %s: end of partition `%c' "
   1357  1.48     enami 				    "exceeds the size of ccd (%d)\n",
   1358  1.48     enami 				    cs->sc_xname, 'a' + i, cs->sc_size);
   1359  1.47     enami 		}
   1360  1.47     enami 	}
   1361  1.11   thorpej 
   1362  1.11   thorpej #ifdef DEBUG
   1363  1.11   thorpej 	/* It's actually extremely common to have unlabeled ccds. */
   1364  1.11   thorpej 	if (ccddebug & CCDB_LABEL)
   1365  1.11   thorpej 		if (errstring != NULL)
   1366  1.35  christos 			printf("%s: %s\n", cs->sc_xname, errstring);
   1367  1.11   thorpej #endif
   1368  1.11   thorpej }
   1369  1.11   thorpej 
   1370  1.11   thorpej /*
   1371  1.11   thorpej  * Take care of things one might want to take care of in the event
   1372  1.11   thorpej  * that a disklabel isn't present.
   1373  1.11   thorpej  */
   1374  1.11   thorpej static void
   1375  1.11   thorpej ccdmakedisklabel(cs)
   1376  1.11   thorpej 	struct ccd_softc *cs;
   1377  1.11   thorpej {
   1378  1.23   thorpej 	struct disklabel *lp = cs->sc_dkdev.dk_label;
   1379  1.11   thorpej 
   1380  1.11   thorpej 	/*
   1381  1.11   thorpej 	 * For historical reasons, if there's no disklabel present
   1382  1.11   thorpej 	 * the raw partition must be marked FS_BSDFFS.
   1383  1.11   thorpej 	 */
   1384  1.11   thorpej 	lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
   1385  1.11   thorpej 
   1386  1.11   thorpej 	strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
   1387  1.44   thorpej 
   1388  1.44   thorpej 	lp->d_checksum = dkcksum(lp);
   1389  1.15   thorpej }
   1390  1.15   thorpej 
   1391  1.15   thorpej /*
   1392  1.15   thorpej  * Wait interruptibly for an exclusive lock.
   1393  1.15   thorpej  *
   1394  1.15   thorpej  * XXX
   1395  1.15   thorpej  * Several drivers do this; it should be abstracted and made MP-safe.
   1396  1.15   thorpej  */
   1397  1.15   thorpej static int
   1398  1.15   thorpej ccdlock(cs)
   1399  1.15   thorpej 	struct ccd_softc *cs;
   1400  1.15   thorpej {
   1401  1.15   thorpej 	int error;
   1402  1.15   thorpej 
   1403  1.15   thorpej 	while ((cs->sc_flags & CCDF_LOCKED) != 0) {
   1404  1.15   thorpej 		cs->sc_flags |= CCDF_WANTED;
   1405  1.15   thorpej 		if ((error = tsleep(cs, PRIBIO | PCATCH, "ccdlck", 0)) != 0)
   1406  1.15   thorpej 			return (error);
   1407  1.15   thorpej 	}
   1408  1.15   thorpej 	cs->sc_flags |= CCDF_LOCKED;
   1409  1.15   thorpej 	return (0);
   1410  1.15   thorpej }
   1411  1.15   thorpej 
   1412  1.15   thorpej /*
   1413  1.15   thorpej  * Unlock and wake up any waiters.
   1414  1.15   thorpej  */
   1415  1.15   thorpej static void
   1416  1.15   thorpej ccdunlock(cs)
   1417  1.15   thorpej 	struct ccd_softc *cs;
   1418  1.15   thorpej {
   1419  1.15   thorpej 
   1420  1.15   thorpej 	cs->sc_flags &= ~CCDF_LOCKED;
   1421  1.15   thorpej 	if ((cs->sc_flags & CCDF_WANTED) != 0) {
   1422  1.15   thorpej 		cs->sc_flags &= ~CCDF_WANTED;
   1423  1.15   thorpej 		wakeup(cs);
   1424  1.15   thorpej 	}
   1425  1.11   thorpej }
   1426  1.11   thorpej 
   1427  1.11   thorpej #ifdef DEBUG
   1428  1.11   thorpej static void
   1429  1.11   thorpej printiinfo(ii)
   1430  1.11   thorpej 	struct ccdiinfo *ii;
   1431  1.11   thorpej {
   1432  1.11   thorpej 	register int ix, i;
   1433  1.11   thorpej 
   1434  1.11   thorpej 	for (ix = 0; ii->ii_ndisk; ix++, ii++) {
   1435  1.35  christos 		printf(" itab[%d]: #dk %d sblk %d soff %d",
   1436  1.34  christos 		    ix, ii->ii_ndisk, ii->ii_startblk, ii->ii_startoff);
   1437  1.11   thorpej 		for (i = 0; i < ii->ii_ndisk; i++)
   1438  1.35  christos 			printf(" %d", ii->ii_index[i]);
   1439  1.35  christos 		printf("\n");
   1440  1.11   thorpej 	}
   1441   1.1   hpeyerl }
   1442   1.1   hpeyerl #endif
   1443