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