Home | History | Annotate | Line # | Download | only in raidframe
rf_netbsdkintf.c revision 1.173
      1 /*	$NetBSD: rf_netbsdkintf.c,v 1.173 2004/01/25 18:06:48 hannken Exp $	*/
      2 /*-
      3  * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Greg Oster; Jason R. Thorpe.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1990, 1993
     40  *      The Regents of the University of California.  All rights reserved.
     41  *
     42  * This code is derived from software contributed to Berkeley by
     43  * the Systems Programming Group of the University of Utah Computer
     44  * Science Department.
     45  *
     46  * Redistribution and use in source and binary forms, with or without
     47  * modification, are permitted provided that the following conditions
     48  * are met:
     49  * 1. Redistributions of source code must retain the above copyright
     50  *    notice, this list of conditions and the following disclaimer.
     51  * 2. Redistributions in binary form must reproduce the above copyright
     52  *    notice, this list of conditions and the following disclaimer in the
     53  *    documentation and/or other materials provided with the distribution.
     54  * 3. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  * from: Utah $Hdr: cd.c 1.6 90/11/28$
     71  *
     72  *      @(#)cd.c        8.2 (Berkeley) 11/16/93
     73  */
     74 
     75 /*
     76  * Copyright (c) 1988 University of Utah.
     77  *
     78  * This code is derived from software contributed to Berkeley by
     79  * the Systems Programming Group of the University of Utah Computer
     80  * Science Department.
     81  *
     82  * Redistribution and use in source and binary forms, with or without
     83  * modification, are permitted provided that the following conditions
     84  * are met:
     85  * 1. Redistributions of source code must retain the above copyright
     86  *    notice, this list of conditions and the following disclaimer.
     87  * 2. Redistributions in binary form must reproduce the above copyright
     88  *    notice, this list of conditions and the following disclaimer in the
     89  *    documentation and/or other materials provided with the distribution.
     90  * 3. All advertising materials mentioning features or use of this software
     91  *    must display the following acknowledgement:
     92  *      This product includes software developed by the University of
     93  *      California, Berkeley and its contributors.
     94  * 4. Neither the name of the University nor the names of its contributors
     95  *    may be used to endorse or promote products derived from this software
     96  *    without specific prior written permission.
     97  *
     98  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     99  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    100  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    101  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    102  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    103  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    104  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    105  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    106  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    107  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    108  * SUCH DAMAGE.
    109  *
    110  * from: Utah $Hdr: cd.c 1.6 90/11/28$
    111  *
    112  *      @(#)cd.c        8.2 (Berkeley) 11/16/93
    113  */
    114 
    115 /*
    116  * Copyright (c) 1995 Carnegie-Mellon University.
    117  * All rights reserved.
    118  *
    119  * Authors: Mark Holland, Jim Zelenka
    120  *
    121  * Permission to use, copy, modify and distribute this software and
    122  * its documentation is hereby granted, provided that both the copyright
    123  * notice and this permission notice appear in all copies of the
    124  * software, derivative works or modified versions, and any portions
    125  * thereof, and that both notices appear in supporting documentation.
    126  *
    127  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
    128  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
    129  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
    130  *
    131  * Carnegie Mellon requests users of this software to return to
    132  *
    133  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
    134  *  School of Computer Science
    135  *  Carnegie Mellon University
    136  *  Pittsburgh PA 15213-3890
    137  *
    138  * any improvements or extensions that they make and grant Carnegie the
    139  * rights to redistribute these changes.
    140  */
    141 
    142 /***********************************************************
    143  *
    144  * rf_kintf.c -- the kernel interface routines for RAIDframe
    145  *
    146  ***********************************************************/
    147 
    148 #include <sys/cdefs.h>
    149 __KERNEL_RCSID(0, "$NetBSD: rf_netbsdkintf.c,v 1.173 2004/01/25 18:06:48 hannken Exp $");
    150 
    151 #include <sys/param.h>
    152 #include <sys/errno.h>
    153 #include <sys/pool.h>
    154 #include <sys/proc.h>
    155 #include <sys/queue.h>
    156 #include <sys/disk.h>
    157 #include <sys/device.h>
    158 #include <sys/stat.h>
    159 #include <sys/ioctl.h>
    160 #include <sys/fcntl.h>
    161 #include <sys/systm.h>
    162 #include <sys/namei.h>
    163 #include <sys/vnode.h>
    164 #include <sys/disklabel.h>
    165 #include <sys/conf.h>
    166 #include <sys/lock.h>
    167 #include <sys/buf.h>
    168 #include <sys/user.h>
    169 #include <sys/reboot.h>
    170 
    171 #include <dev/raidframe/raidframevar.h>
    172 #include <dev/raidframe/raidframeio.h>
    173 #include "raid.h"
    174 #include "opt_raid_autoconfig.h"
    175 #include "rf_raid.h"
    176 #include "rf_copyback.h"
    177 #include "rf_dag.h"
    178 #include "rf_dagflags.h"
    179 #include "rf_desc.h"
    180 #include "rf_diskqueue.h"
    181 #include "rf_etimer.h"
    182 #include "rf_general.h"
    183 #include "rf_kintf.h"
    184 #include "rf_options.h"
    185 #include "rf_driver.h"
    186 #include "rf_parityscan.h"
    187 #include "rf_threadstuff.h"
    188 
    189 #ifdef DEBUG
    190 int     rf_kdebug_level = 0;
    191 #define db1_printf(a) if (rf_kdebug_level > 0) printf a
    192 #else				/* DEBUG */
    193 #define db1_printf(a) { }
    194 #endif				/* DEBUG */
    195 
    196 static RF_Raid_t **raidPtrs;	/* global raid device descriptors */
    197 
    198 RF_DECLARE_STATIC_MUTEX(rf_sparet_wait_mutex)
    199 
    200 static RF_SparetWait_t *rf_sparet_wait_queue;	/* requests to install a
    201 						 * spare table */
    202 static RF_SparetWait_t *rf_sparet_resp_queue;	/* responses from
    203 						 * installation process */
    204 
    205 MALLOC_DEFINE(M_RAIDFRAME, "RAIDframe", "RAIDframe structures");
    206 
    207 /* prototypes */
    208 static void KernelWakeupFunc(struct buf * bp);
    209 static void InitBP(struct buf * bp, struct vnode *, unsigned rw_flag,
    210 		   dev_t dev, RF_SectorNum_t startSect,
    211 		   RF_SectorCount_t numSect, caddr_t buf,
    212 		   void (*cbFunc) (struct buf *), void *cbArg,
    213 		   int logBytesPerSector, struct proc * b_proc);
    214 static void raidinit(RF_Raid_t *);
    215 
    216 void raidattach(int);
    217 
    218 dev_type_open(raidopen);
    219 dev_type_close(raidclose);
    220 dev_type_read(raidread);
    221 dev_type_write(raidwrite);
    222 dev_type_ioctl(raidioctl);
    223 dev_type_strategy(raidstrategy);
    224 dev_type_dump(raiddump);
    225 dev_type_size(raidsize);
    226 
    227 const struct bdevsw raid_bdevsw = {
    228 	raidopen, raidclose, raidstrategy, raidioctl,
    229 	raiddump, raidsize, D_DISK
    230 };
    231 
    232 const struct cdevsw raid_cdevsw = {
    233 	raidopen, raidclose, raidread, raidwrite, raidioctl,
    234 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    235 };
    236 
    237 /*
    238  * Pilfered from ccd.c
    239  */
    240 
    241 struct raidbuf {
    242 	struct buf rf_buf;	/* new I/O buf.  MUST BE FIRST!!! */
    243 	struct buf *rf_obp;	/* ptr. to original I/O buf */
    244 	RF_DiskQueueData_t *req;/* the request that this was part of.. */
    245 };
    246 
    247 /* component buffer pool */
    248 struct pool raidframe_cbufpool;
    249 
    250 /* XXX Not sure if the following should be replacing the raidPtrs above,
    251    or if it should be used in conjunction with that...
    252 */
    253 
    254 struct raid_softc {
    255 	int     sc_flags;	/* flags */
    256 	int     sc_cflags;	/* configuration flags */
    257 	size_t  sc_size;        /* size of the raid device */
    258 	char    sc_xname[20];	/* XXX external name */
    259 	struct disk sc_dkdev;	/* generic disk device info */
    260 	struct bufq_state buf_queue;	/* used for the device queue */
    261 };
    262 /* sc_flags */
    263 #define RAIDF_INITED	0x01	/* unit has been initialized */
    264 #define RAIDF_WLABEL	0x02	/* label area is writable */
    265 #define RAIDF_LABELLING	0x04	/* unit is currently being labelled */
    266 #define RAIDF_WANTED	0x40	/* someone is waiting to obtain a lock */
    267 #define RAIDF_LOCKED	0x80	/* unit is locked */
    268 
    269 #define	raidunit(x)	DISKUNIT(x)
    270 int numraid = 0;
    271 
    272 /*
    273  * Allow RAIDOUTSTANDING number of simultaneous IO's to this RAID device.
    274  * Be aware that large numbers can allow the driver to consume a lot of
    275  * kernel memory, especially on writes, and in degraded mode reads.
    276  *
    277  * For example: with a stripe width of 64 blocks (32k) and 5 disks,
    278  * a single 64K write will typically require 64K for the old data,
    279  * 64K for the old parity, and 64K for the new parity, for a total
    280  * of 192K (if the parity buffer is not re-used immediately).
    281  * Even it if is used immediately, that's still 128K, which when multiplied
    282  * by say 10 requests, is 1280K, *on top* of the 640K of incoming data.
    283  *
    284  * Now in degraded mode, for example, a 64K read on the above setup may
    285  * require data reconstruction, which will require *all* of the 4 remaining
    286  * disks to participate -- 4 * 32K/disk == 128K again.
    287  */
    288 
    289 #ifndef RAIDOUTSTANDING
    290 #define RAIDOUTSTANDING   6
    291 #endif
    292 
    293 #define RAIDLABELDEV(dev)	\
    294 	(MAKEDISKDEV(major((dev)), raidunit((dev)), RAW_PART))
    295 
    296 /* declared here, and made public, for the benefit of KVM stuff.. */
    297 struct raid_softc *raid_softc;
    298 
    299 static void raidgetdefaultlabel(RF_Raid_t *, struct raid_softc *,
    300 				     struct disklabel *);
    301 static void raidgetdisklabel(dev_t);
    302 static void raidmakedisklabel(struct raid_softc *);
    303 
    304 static int raidlock(struct raid_softc *);
    305 static void raidunlock(struct raid_softc *);
    306 
    307 static void rf_markalldirty(RF_Raid_t *);
    308 
    309 struct device *raidrootdev;
    310 
    311 void rf_ReconThread(struct rf_recon_req *);
    312 void rf_RewriteParityThread(RF_Raid_t *raidPtr);
    313 void rf_CopybackThread(RF_Raid_t *raidPtr);
    314 void rf_ReconstructInPlaceThread(struct rf_recon_req *);
    315 int rf_autoconfig(struct device *self);
    316 void rf_buildroothack(RF_ConfigSet_t *);
    317 
    318 RF_AutoConfig_t *rf_find_raid_components(void);
    319 RF_ConfigSet_t *rf_create_auto_sets(RF_AutoConfig_t *);
    320 static int rf_does_it_fit(RF_ConfigSet_t *,RF_AutoConfig_t *);
    321 static int rf_reasonable_label(RF_ComponentLabel_t *);
    322 void rf_create_configuration(RF_AutoConfig_t *,RF_Config_t *, RF_Raid_t *);
    323 int rf_set_autoconfig(RF_Raid_t *, int);
    324 int rf_set_rootpartition(RF_Raid_t *, int);
    325 void rf_release_all_vps(RF_ConfigSet_t *);
    326 void rf_cleanup_config_set(RF_ConfigSet_t *);
    327 int rf_have_enough_components(RF_ConfigSet_t *);
    328 int rf_auto_config_set(RF_ConfigSet_t *, int *);
    329 
    330 static int raidautoconfig = 0; /* Debugging, mostly.  Set to 0 to not
    331 				  allow autoconfig to take place.
    332 			          Note that this is overridden by having
    333 			          RAID_AUTOCONFIG as an option in the
    334 			          kernel config file.  */
    335 
    336 void
    337 raidattach(int num)
    338 {
    339 	int raidID;
    340 	int i, rc;
    341 
    342 #ifdef DEBUG
    343 	printf("raidattach: Asked for %d units\n", num);
    344 #endif
    345 
    346 	if (num <= 0) {
    347 #ifdef DIAGNOSTIC
    348 		panic("raidattach: count <= 0");
    349 #endif
    350 		return;
    351 	}
    352 	/* This is where all the initialization stuff gets done. */
    353 
    354 	numraid = num;
    355 
    356 	/* Make some space for requested number of units... */
    357 
    358 	RF_Malloc(raidPtrs, num * sizeof(RF_Raid_t *), (RF_Raid_t **));
    359 	if (raidPtrs == NULL) {
    360 		panic("raidPtrs is NULL!!");
    361 	}
    362 
    363 	/* Initialize the component buffer pool. */
    364 	pool_init(&raidframe_cbufpool, sizeof(struct raidbuf), 0,
    365 	    0, 0, "raidpl", NULL);
    366 
    367 	rf_mutex_init(&rf_sparet_wait_mutex);
    368 
    369 	rf_sparet_wait_queue = rf_sparet_resp_queue = NULL;
    370 
    371 	for (i = 0; i < num; i++)
    372 		raidPtrs[i] = NULL;
    373 	rc = rf_BootRaidframe();
    374 	if (rc == 0)
    375 		printf("Kernelized RAIDframe activated\n");
    376 	else
    377 		panic("Serious error booting RAID!!");
    378 
    379 	/* put together some datastructures like the CCD device does.. This
    380 	 * lets us lock the device and what-not when it gets opened. */
    381 
    382 	raid_softc = (struct raid_softc *)
    383 		malloc(num * sizeof(struct raid_softc),
    384 		       M_RAIDFRAME, M_NOWAIT);
    385 	if (raid_softc == NULL) {
    386 		printf("WARNING: no memory for RAIDframe driver\n");
    387 		return;
    388 	}
    389 
    390 	memset(raid_softc, 0, num * sizeof(struct raid_softc));
    391 
    392 	raidrootdev = (struct device *)malloc(num * sizeof(struct device),
    393 					      M_RAIDFRAME, M_NOWAIT);
    394 	if (raidrootdev == NULL) {
    395 		panic("No memory for RAIDframe driver!!?!?!");
    396 	}
    397 
    398 	for (raidID = 0; raidID < num; raidID++) {
    399 		bufq_alloc(&raid_softc[raidID].buf_queue, BUFQ_FCFS);
    400 
    401 		raidrootdev[raidID].dv_class  = DV_DISK;
    402 		raidrootdev[raidID].dv_cfdata = NULL;
    403 		raidrootdev[raidID].dv_unit   = raidID;
    404 		raidrootdev[raidID].dv_parent = NULL;
    405 		raidrootdev[raidID].dv_flags  = 0;
    406 		sprintf(raidrootdev[raidID].dv_xname,"raid%d",raidID);
    407 
    408 		RF_Malloc(raidPtrs[raidID], sizeof(RF_Raid_t),
    409 			  (RF_Raid_t *));
    410 		if (raidPtrs[raidID] == NULL) {
    411 			printf("WARNING: raidPtrs[%d] is NULL\n", raidID);
    412 			numraid = raidID;
    413 			return;
    414 		}
    415 	}
    416 
    417 #ifdef RAID_AUTOCONFIG
    418 	raidautoconfig = 1;
    419 #endif
    420 
    421 	/*
    422 	 * Register a finalizer which will be used to auto-config RAID
    423 	 * sets once all real hardware devices have been found.
    424 	 */
    425 	if (config_finalize_register(NULL, rf_autoconfig) != 0)
    426 		printf("WARNING: unable to register RAIDframe finalizer\n");
    427 }
    428 
    429 int
    430 rf_autoconfig(struct device *self)
    431 {
    432 	RF_AutoConfig_t *ac_list;
    433 	RF_ConfigSet_t *config_sets;
    434 
    435 	if (raidautoconfig == 0)
    436 		return (0);
    437 
    438 	/* XXX This code can only be run once. */
    439 	raidautoconfig = 0;
    440 
    441 	/* 1. locate all RAID components on the system */
    442 #ifdef DEBUG
    443 	printf("Searching for RAID components...\n");
    444 #endif
    445 	ac_list = rf_find_raid_components();
    446 
    447 	/* 2. Sort them into their respective sets. */
    448 	config_sets = rf_create_auto_sets(ac_list);
    449 
    450 	/*
    451 	 * 3. Evaluate each set andconfigure the valid ones.
    452 	 * This gets done in rf_buildroothack().
    453 	 */
    454 	rf_buildroothack(config_sets);
    455 
    456 	return (1);
    457 }
    458 
    459 void
    460 rf_buildroothack(RF_ConfigSet_t *config_sets)
    461 {
    462 	RF_ConfigSet_t *cset;
    463 	RF_ConfigSet_t *next_cset;
    464 	int retcode;
    465 	int raidID;
    466 	int rootID;
    467 	int num_root;
    468 
    469 	rootID = 0;
    470 	num_root = 0;
    471 	cset = config_sets;
    472 	while(cset != NULL ) {
    473 		next_cset = cset->next;
    474 		if (rf_have_enough_components(cset) &&
    475 		    cset->ac->clabel->autoconfigure==1) {
    476 			retcode = rf_auto_config_set(cset,&raidID);
    477 			if (!retcode) {
    478 				if (cset->rootable) {
    479 					rootID = raidID;
    480 					num_root++;
    481 				}
    482 			} else {
    483 				/* The autoconfig didn't work :( */
    484 #if DEBUG
    485 				printf("Autoconfig failed with code %d for raid%d\n", retcode, raidID);
    486 #endif
    487 				rf_release_all_vps(cset);
    488 			}
    489 		} else {
    490 			/* we're not autoconfiguring this set...
    491 			   release the associated resources */
    492 			rf_release_all_vps(cset);
    493 		}
    494 		/* cleanup */
    495 		rf_cleanup_config_set(cset);
    496 		cset = next_cset;
    497 	}
    498 
    499 	/* we found something bootable... */
    500 
    501 	if (num_root == 1) {
    502 		booted_device = &raidrootdev[rootID];
    503 	} else if (num_root > 1) {
    504 		/* we can't guess.. require the user to answer... */
    505 		boothowto |= RB_ASKNAME;
    506 	}
    507 }
    508 
    509 
    510 int
    511 raidsize(dev_t dev)
    512 {
    513 	struct raid_softc *rs;
    514 	struct disklabel *lp;
    515 	int     part, unit, omask, size;
    516 
    517 	unit = raidunit(dev);
    518 	if (unit >= numraid)
    519 		return (-1);
    520 	rs = &raid_softc[unit];
    521 
    522 	if ((rs->sc_flags & RAIDF_INITED) == 0)
    523 		return (-1);
    524 
    525 	part = DISKPART(dev);
    526 	omask = rs->sc_dkdev.dk_openmask & (1 << part);
    527 	lp = rs->sc_dkdev.dk_label;
    528 
    529 	if (omask == 0 && raidopen(dev, 0, S_IFBLK, curproc))
    530 		return (-1);
    531 
    532 	if (lp->d_partitions[part].p_fstype != FS_SWAP)
    533 		size = -1;
    534 	else
    535 		size = lp->d_partitions[part].p_size *
    536 		    (lp->d_secsize / DEV_BSIZE);
    537 
    538 	if (omask == 0 && raidclose(dev, 0, S_IFBLK, curproc))
    539 		return (-1);
    540 
    541 	return (size);
    542 
    543 }
    544 
    545 int
    546 raiddump(dev_t dev, daddr_t blkno, caddr_t va, size_t  size)
    547 {
    548 	/* Not implemented. */
    549 	return ENXIO;
    550 }
    551 /* ARGSUSED */
    552 int
    553 raidopen(dev_t dev, int flags, int fmt, struct proc *p)
    554 {
    555 	int     unit = raidunit(dev);
    556 	struct raid_softc *rs;
    557 	struct disklabel *lp;
    558 	int     part, pmask;
    559 	int     error = 0;
    560 
    561 	if (unit >= numraid)
    562 		return (ENXIO);
    563 	rs = &raid_softc[unit];
    564 
    565 	if ((error = raidlock(rs)) != 0)
    566 		return (error);
    567 	lp = rs->sc_dkdev.dk_label;
    568 
    569 	part = DISKPART(dev);
    570 	pmask = (1 << part);
    571 
    572 	if ((rs->sc_flags & RAIDF_INITED) &&
    573 	    (rs->sc_dkdev.dk_openmask == 0))
    574 		raidgetdisklabel(dev);
    575 
    576 	/* make sure that this partition exists */
    577 
    578 	if (part != RAW_PART) {
    579 		if (((rs->sc_flags & RAIDF_INITED) == 0) ||
    580 		    ((part >= lp->d_npartitions) ||
    581 			(lp->d_partitions[part].p_fstype == FS_UNUSED))) {
    582 			error = ENXIO;
    583 			raidunlock(rs);
    584 			return (error);
    585 		}
    586 	}
    587 	/* Prevent this unit from being unconfigured while open. */
    588 	switch (fmt) {
    589 	case S_IFCHR:
    590 		rs->sc_dkdev.dk_copenmask |= pmask;
    591 		break;
    592 
    593 	case S_IFBLK:
    594 		rs->sc_dkdev.dk_bopenmask |= pmask;
    595 		break;
    596 	}
    597 
    598 	if ((rs->sc_dkdev.dk_openmask == 0) &&
    599 	    ((rs->sc_flags & RAIDF_INITED) != 0)) {
    600 		/* First one... mark things as dirty... Note that we *MUST*
    601 		 have done a configure before this.  I DO NOT WANT TO BE
    602 		 SCRIBBLING TO RANDOM COMPONENTS UNTIL IT'S BEEN DETERMINED
    603 		 THAT THEY BELONG TOGETHER!!!!! */
    604 		/* XXX should check to see if we're only open for reading
    605 		   here... If so, we needn't do this, but then need some
    606 		   other way of keeping track of what's happened.. */
    607 
    608 		rf_markalldirty( raidPtrs[unit] );
    609 	}
    610 
    611 
    612 	rs->sc_dkdev.dk_openmask =
    613 	    rs->sc_dkdev.dk_copenmask | rs->sc_dkdev.dk_bopenmask;
    614 
    615 	raidunlock(rs);
    616 
    617 	return (error);
    618 
    619 
    620 }
    621 /* ARGSUSED */
    622 int
    623 raidclose(dev_t dev, int flags, int fmt, struct proc *p)
    624 {
    625 	int     unit = raidunit(dev);
    626 	struct raid_softc *rs;
    627 	int     error = 0;
    628 	int     part;
    629 
    630 	if (unit >= numraid)
    631 		return (ENXIO);
    632 	rs = &raid_softc[unit];
    633 
    634 	if ((error = raidlock(rs)) != 0)
    635 		return (error);
    636 
    637 	part = DISKPART(dev);
    638 
    639 	/* ...that much closer to allowing unconfiguration... */
    640 	switch (fmt) {
    641 	case S_IFCHR:
    642 		rs->sc_dkdev.dk_copenmask &= ~(1 << part);
    643 		break;
    644 
    645 	case S_IFBLK:
    646 		rs->sc_dkdev.dk_bopenmask &= ~(1 << part);
    647 		break;
    648 	}
    649 	rs->sc_dkdev.dk_openmask =
    650 	    rs->sc_dkdev.dk_copenmask | rs->sc_dkdev.dk_bopenmask;
    651 
    652 	if ((rs->sc_dkdev.dk_openmask == 0) &&
    653 	    ((rs->sc_flags & RAIDF_INITED) != 0)) {
    654 		/* Last one... device is not unconfigured yet.
    655 		   Device shutdown has taken care of setting the
    656 		   clean bits if RAIDF_INITED is not set
    657 		   mark things as clean... */
    658 
    659 		rf_update_component_labels(raidPtrs[unit],
    660 						 RF_FINAL_COMPONENT_UPDATE);
    661 		if (doing_shutdown) {
    662 			/* last one, and we're going down, so
    663 			   lights out for this RAID set too. */
    664 			error = rf_Shutdown(raidPtrs[unit]);
    665 
    666 			/* It's no longer initialized... */
    667 			rs->sc_flags &= ~RAIDF_INITED;
    668 
    669 			/* Detach the disk. */
    670 			disk_detach(&rs->sc_dkdev);
    671 		}
    672 	}
    673 
    674 	raidunlock(rs);
    675 	return (0);
    676 
    677 }
    678 
    679 void
    680 raidstrategy(struct buf *bp)
    681 {
    682 	int s;
    683 
    684 	unsigned int raidID = raidunit(bp->b_dev);
    685 	RF_Raid_t *raidPtr;
    686 	struct raid_softc *rs = &raid_softc[raidID];
    687 	int     wlabel;
    688 
    689 	if ((rs->sc_flags & RAIDF_INITED) ==0) {
    690 		bp->b_error = ENXIO;
    691 		bp->b_flags |= B_ERROR;
    692 		bp->b_resid = bp->b_bcount;
    693 		biodone(bp);
    694 		return;
    695 	}
    696 	if (raidID >= numraid || !raidPtrs[raidID]) {
    697 		bp->b_error = ENODEV;
    698 		bp->b_flags |= B_ERROR;
    699 		bp->b_resid = bp->b_bcount;
    700 		biodone(bp);
    701 		return;
    702 	}
    703 	raidPtr = raidPtrs[raidID];
    704 	if (!raidPtr->valid) {
    705 		bp->b_error = ENODEV;
    706 		bp->b_flags |= B_ERROR;
    707 		bp->b_resid = bp->b_bcount;
    708 		biodone(bp);
    709 		return;
    710 	}
    711 	if (bp->b_bcount == 0) {
    712 		db1_printf(("b_bcount is zero..\n"));
    713 		biodone(bp);
    714 		return;
    715 	}
    716 
    717 	/*
    718 	 * Do bounds checking and adjust transfer.  If there's an
    719 	 * error, the bounds check will flag that for us.
    720 	 */
    721 
    722 	wlabel = rs->sc_flags & (RAIDF_WLABEL | RAIDF_LABELLING);
    723 	if (DISKPART(bp->b_dev) != RAW_PART)
    724 		if (bounds_check_with_label(&rs->sc_dkdev, bp, wlabel) <= 0) {
    725 			db1_printf(("Bounds check failed!!:%d %d\n",
    726 				(int) bp->b_blkno, (int) wlabel));
    727 			biodone(bp);
    728 			return;
    729 		}
    730 	s = splbio();
    731 
    732 	bp->b_resid = 0;
    733 
    734 	/* stuff it onto our queue */
    735 	BUFQ_PUT(&rs->buf_queue, bp);
    736 
    737 	raidstart(raidPtrs[raidID]);
    738 
    739 	splx(s);
    740 }
    741 /* ARGSUSED */
    742 int
    743 raidread(dev_t dev, struct uio *uio, int flags)
    744 {
    745 	int     unit = raidunit(dev);
    746 	struct raid_softc *rs;
    747 
    748 	if (unit >= numraid)
    749 		return (ENXIO);
    750 	rs = &raid_softc[unit];
    751 
    752 	if ((rs->sc_flags & RAIDF_INITED) == 0)
    753 		return (ENXIO);
    754 
    755 	return (physio(raidstrategy, NULL, dev, B_READ, minphys, uio));
    756 
    757 }
    758 /* ARGSUSED */
    759 int
    760 raidwrite(dev_t dev, struct uio *uio, int flags)
    761 {
    762 	int     unit = raidunit(dev);
    763 	struct raid_softc *rs;
    764 
    765 	if (unit >= numraid)
    766 		return (ENXIO);
    767 	rs = &raid_softc[unit];
    768 
    769 	if ((rs->sc_flags & RAIDF_INITED) == 0)
    770 		return (ENXIO);
    771 
    772 	return (physio(raidstrategy, NULL, dev, B_WRITE, minphys, uio));
    773 
    774 }
    775 
    776 int
    777 raidioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    778 {
    779 	int     unit = raidunit(dev);
    780 	int     error = 0;
    781 	int     part, pmask;
    782 	struct raid_softc *rs;
    783 	RF_Config_t *k_cfg, *u_cfg;
    784 	RF_Raid_t *raidPtr;
    785 	RF_RaidDisk_t *diskPtr;
    786 	RF_AccTotals_t *totals;
    787 	RF_DeviceConfig_t *d_cfg, **ucfgp;
    788 	u_char *specific_buf;
    789 	int retcode = 0;
    790 	int column;
    791 	int raidid;
    792 	struct rf_recon_req *rrcopy, *rr;
    793 	RF_ComponentLabel_t *clabel;
    794 	RF_ComponentLabel_t ci_label;
    795 	RF_ComponentLabel_t **clabel_ptr;
    796 	RF_SingleComponent_t *sparePtr,*componentPtr;
    797 	RF_SingleComponent_t hot_spare;
    798 	RF_SingleComponent_t component;
    799 	RF_ProgressInfo_t progressInfo, **progressInfoPtr;
    800 	int i, j, d;
    801 #ifdef __HAVE_OLD_DISKLABEL
    802 	struct disklabel newlabel;
    803 #endif
    804 
    805 	if (unit >= numraid)
    806 		return (ENXIO);
    807 	rs = &raid_softc[unit];
    808 	raidPtr = raidPtrs[unit];
    809 
    810 	db1_printf(("raidioctl: %d %d %d %d\n", (int) dev,
    811 		(int) DISKPART(dev), (int) unit, (int) cmd));
    812 
    813 	/* Must be open for writes for these commands... */
    814 	switch (cmd) {
    815 	case DIOCSDINFO:
    816 	case DIOCWDINFO:
    817 #ifdef __HAVE_OLD_DISKLABEL
    818 	case ODIOCWDINFO:
    819 	case ODIOCSDINFO:
    820 #endif
    821 	case DIOCWLABEL:
    822 		if ((flag & FWRITE) == 0)
    823 			return (EBADF);
    824 	}
    825 
    826 	/* Must be initialized for these... */
    827 	switch (cmd) {
    828 	case DIOCGDINFO:
    829 	case DIOCSDINFO:
    830 	case DIOCWDINFO:
    831 #ifdef __HAVE_OLD_DISKLABEL
    832 	case ODIOCGDINFO:
    833 	case ODIOCWDINFO:
    834 	case ODIOCSDINFO:
    835 	case ODIOCGDEFLABEL:
    836 #endif
    837 	case DIOCGPART:
    838 	case DIOCWLABEL:
    839 	case DIOCGDEFLABEL:
    840 	case RAIDFRAME_SHUTDOWN:
    841 	case RAIDFRAME_REWRITEPARITY:
    842 	case RAIDFRAME_GET_INFO:
    843 	case RAIDFRAME_RESET_ACCTOTALS:
    844 	case RAIDFRAME_GET_ACCTOTALS:
    845 	case RAIDFRAME_KEEP_ACCTOTALS:
    846 	case RAIDFRAME_GET_SIZE:
    847 	case RAIDFRAME_FAIL_DISK:
    848 	case RAIDFRAME_COPYBACK:
    849 	case RAIDFRAME_CHECK_RECON_STATUS:
    850 	case RAIDFRAME_CHECK_RECON_STATUS_EXT:
    851 	case RAIDFRAME_GET_COMPONENT_LABEL:
    852 	case RAIDFRAME_SET_COMPONENT_LABEL:
    853 	case RAIDFRAME_ADD_HOT_SPARE:
    854 	case RAIDFRAME_REMOVE_HOT_SPARE:
    855 	case RAIDFRAME_INIT_LABELS:
    856 	case RAIDFRAME_REBUILD_IN_PLACE:
    857 	case RAIDFRAME_CHECK_PARITY:
    858 	case RAIDFRAME_CHECK_PARITYREWRITE_STATUS:
    859 	case RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT:
    860 	case RAIDFRAME_CHECK_COPYBACK_STATUS:
    861 	case RAIDFRAME_CHECK_COPYBACK_STATUS_EXT:
    862 	case RAIDFRAME_SET_AUTOCONFIG:
    863 	case RAIDFRAME_SET_ROOT:
    864 	case RAIDFRAME_DELETE_COMPONENT:
    865 	case RAIDFRAME_INCORPORATE_HOT_SPARE:
    866 		if ((rs->sc_flags & RAIDF_INITED) == 0)
    867 			return (ENXIO);
    868 	}
    869 
    870 	switch (cmd) {
    871 
    872 		/* configure the system */
    873 	case RAIDFRAME_CONFIGURE:
    874 
    875 		if (raidPtr->valid) {
    876 			/* There is a valid RAID set running on this unit! */
    877 			printf("raid%d: Device already configured!\n",unit);
    878 			return(EINVAL);
    879 		}
    880 
    881 		/* copy-in the configuration information */
    882 		/* data points to a pointer to the configuration structure */
    883 
    884 		u_cfg = *((RF_Config_t **) data);
    885 		RF_Malloc(k_cfg, sizeof(RF_Config_t), (RF_Config_t *));
    886 		if (k_cfg == NULL) {
    887 			return (ENOMEM);
    888 		}
    889 		retcode = copyin(u_cfg, k_cfg, sizeof(RF_Config_t));
    890 		if (retcode) {
    891 			RF_Free(k_cfg, sizeof(RF_Config_t));
    892 			db1_printf(("rf_ioctl: retcode=%d copyin.1\n",
    893 				retcode));
    894 			return (retcode);
    895 		}
    896 		/* allocate a buffer for the layout-specific data, and copy it
    897 		 * in */
    898 		if (k_cfg->layoutSpecificSize) {
    899 			if (k_cfg->layoutSpecificSize > 10000) {
    900 				/* sanity check */
    901 				RF_Free(k_cfg, sizeof(RF_Config_t));
    902 				return (EINVAL);
    903 			}
    904 			RF_Malloc(specific_buf, k_cfg->layoutSpecificSize,
    905 			    (u_char *));
    906 			if (specific_buf == NULL) {
    907 				RF_Free(k_cfg, sizeof(RF_Config_t));
    908 				return (ENOMEM);
    909 			}
    910 			retcode = copyin(k_cfg->layoutSpecific, specific_buf,
    911 			    k_cfg->layoutSpecificSize);
    912 			if (retcode) {
    913 				RF_Free(k_cfg, sizeof(RF_Config_t));
    914 				RF_Free(specific_buf,
    915 					k_cfg->layoutSpecificSize);
    916 				db1_printf(("rf_ioctl: retcode=%d copyin.2\n",
    917 					retcode));
    918 				return (retcode);
    919 			}
    920 		} else
    921 			specific_buf = NULL;
    922 		k_cfg->layoutSpecific = specific_buf;
    923 
    924 		/* should do some kind of sanity check on the configuration.
    925 		 * Store the sum of all the bytes in the last byte? */
    926 
    927 		/* configure the system */
    928 
    929 		/*
    930 		 * Clear the entire RAID descriptor, just to make sure
    931 		 *  there is no stale data left in the case of a
    932 		 *  reconfiguration
    933 		 */
    934 		memset((char *) raidPtr, 0, sizeof(RF_Raid_t));
    935 		raidPtr->raidid = unit;
    936 
    937 		retcode = rf_Configure(raidPtr, k_cfg, NULL);
    938 
    939 		if (retcode == 0) {
    940 
    941 			/* allow this many simultaneous IO's to
    942 			   this RAID device */
    943 			raidPtr->openings = RAIDOUTSTANDING;
    944 
    945 			raidinit(raidPtr);
    946 			rf_markalldirty(raidPtr);
    947 		}
    948 		/* free the buffers.  No return code here. */
    949 		if (k_cfg->layoutSpecificSize) {
    950 			RF_Free(specific_buf, k_cfg->layoutSpecificSize);
    951 		}
    952 		RF_Free(k_cfg, sizeof(RF_Config_t));
    953 
    954 		return (retcode);
    955 
    956 		/* shutdown the system */
    957 	case RAIDFRAME_SHUTDOWN:
    958 
    959 		if ((error = raidlock(rs)) != 0)
    960 			return (error);
    961 
    962 		/*
    963 		 * If somebody has a partition mounted, we shouldn't
    964 		 * shutdown.
    965 		 */
    966 
    967 		part = DISKPART(dev);
    968 		pmask = (1 << part);
    969 		if ((rs->sc_dkdev.dk_openmask & ~pmask) ||
    970 		    ((rs->sc_dkdev.dk_bopenmask & pmask) &&
    971 			(rs->sc_dkdev.dk_copenmask & pmask))) {
    972 			raidunlock(rs);
    973 			return (EBUSY);
    974 		}
    975 
    976 		retcode = rf_Shutdown(raidPtr);
    977 
    978 		/* It's no longer initialized... */
    979 		rs->sc_flags &= ~RAIDF_INITED;
    980 
    981 		/* Detach the disk. */
    982 		disk_detach(&rs->sc_dkdev);
    983 
    984 		raidunlock(rs);
    985 
    986 		return (retcode);
    987 	case RAIDFRAME_GET_COMPONENT_LABEL:
    988 		clabel_ptr = (RF_ComponentLabel_t **) data;
    989 		/* need to read the component label for the disk indicated
    990 		   by row,column in clabel */
    991 
    992 		/* For practice, let's get it directly fromdisk, rather
    993 		   than from the in-core copy */
    994 		RF_Malloc( clabel, sizeof( RF_ComponentLabel_t ),
    995 			   (RF_ComponentLabel_t *));
    996 		if (clabel == NULL)
    997 			return (ENOMEM);
    998 
    999 		memset((char *) clabel, 0, sizeof(RF_ComponentLabel_t));
   1000 
   1001 		retcode = copyin( *clabel_ptr, clabel,
   1002 				  sizeof(RF_ComponentLabel_t));
   1003 
   1004 		if (retcode) {
   1005 			RF_Free( clabel, sizeof(RF_ComponentLabel_t));
   1006 			return(retcode);
   1007 		}
   1008 
   1009 		clabel->row = 0; /* Don't allow looking at anything else.*/
   1010 
   1011 		column = clabel->column;
   1012 
   1013 		if ((column < 0) || (column >= raidPtr->numCol +
   1014 				     raidPtr->numSpare)) {
   1015 			RF_Free( clabel, sizeof(RF_ComponentLabel_t));
   1016 			return(EINVAL);
   1017 		}
   1018 
   1019 		raidread_component_label(raidPtr->Disks[column].dev,
   1020 				raidPtr->raid_cinfo[column].ci_vp,
   1021 				clabel );
   1022 
   1023 		retcode = copyout(clabel, *clabel_ptr,
   1024 				  sizeof(RF_ComponentLabel_t));
   1025 		RF_Free(clabel, sizeof(RF_ComponentLabel_t));
   1026 		return (retcode);
   1027 
   1028 	case RAIDFRAME_SET_COMPONENT_LABEL:
   1029 		clabel = (RF_ComponentLabel_t *) data;
   1030 
   1031 		/* XXX check the label for valid stuff... */
   1032 		/* Note that some things *should not* get modified --
   1033 		   the user should be re-initing the labels instead of
   1034 		   trying to patch things.
   1035 		   */
   1036 
   1037 		raidid = raidPtr->raidid;
   1038 		printf("raid%d: Got component label:\n", raidid);
   1039 		printf("raid%d: Version: %d\n", raidid, clabel->version);
   1040 		printf("raid%d: Serial Number: %d\n", raidid, clabel->serial_number);
   1041 		printf("raid%d: Mod counter: %d\n", raidid, clabel->mod_counter);
   1042 		printf("raid%d: Column: %d\n", raidid, clabel->column);
   1043 		printf("raid%d: Num Columns: %d\n", raidid, clabel->num_columns);
   1044 		printf("raid%d: Clean: %d\n", raidid, clabel->clean);
   1045 		printf("raid%d: Status: %d\n", raidid, clabel->status);
   1046 
   1047 		clabel->row = 0;
   1048 		column = clabel->column;
   1049 
   1050 		if ((column < 0) || (column >= raidPtr->numCol)) {
   1051 			return(EINVAL);
   1052 		}
   1053 
   1054 		/* XXX this isn't allowed to do anything for now :-) */
   1055 
   1056 		/* XXX and before it is, we need to fill in the rest
   1057 		   of the fields!?!?!?! */
   1058 #if 0
   1059 		raidwrite_component_label(
   1060                             raidPtr->Disks[column].dev,
   1061 			    raidPtr->raid_cinfo[column].ci_vp,
   1062 			    clabel );
   1063 #endif
   1064 		return (0);
   1065 
   1066 	case RAIDFRAME_INIT_LABELS:
   1067 		clabel = (RF_ComponentLabel_t *) data;
   1068 		/*
   1069 		   we only want the serial number from
   1070 		   the above.  We get all the rest of the information
   1071 		   from the config that was used to create this RAID
   1072 		   set.
   1073 		   */
   1074 
   1075 		raidPtr->serial_number = clabel->serial_number;
   1076 
   1077 		raid_init_component_label(raidPtr, &ci_label);
   1078 		ci_label.serial_number = clabel->serial_number;
   1079 		ci_label.row = 0; /* we dont' pretend to support more */
   1080 
   1081 		for(column=0;column<raidPtr->numCol;column++) {
   1082 			diskPtr = &raidPtr->Disks[column];
   1083 			if (!RF_DEAD_DISK(diskPtr->status)) {
   1084 				ci_label.partitionSize = diskPtr->partitionSize;
   1085 				ci_label.column = column;
   1086 				raidwrite_component_label(
   1087 							  raidPtr->Disks[column].dev,
   1088 							  raidPtr->raid_cinfo[column].ci_vp,
   1089 							  &ci_label );
   1090 			}
   1091 		}
   1092 
   1093 		return (retcode);
   1094 	case RAIDFRAME_SET_AUTOCONFIG:
   1095 		d = rf_set_autoconfig(raidPtr, *(int *) data);
   1096 		printf("raid%d: New autoconfig value is: %d\n",
   1097 		       raidPtr->raidid, d);
   1098 		*(int *) data = d;
   1099 		return (retcode);
   1100 
   1101 	case RAIDFRAME_SET_ROOT:
   1102 		d = rf_set_rootpartition(raidPtr, *(int *) data);
   1103 		printf("raid%d: New rootpartition value is: %d\n",
   1104 		       raidPtr->raidid, d);
   1105 		*(int *) data = d;
   1106 		return (retcode);
   1107 
   1108 		/* initialize all parity */
   1109 	case RAIDFRAME_REWRITEPARITY:
   1110 
   1111 		if (raidPtr->Layout.map->faultsTolerated == 0) {
   1112 			/* Parity for RAID 0 is trivially correct */
   1113 			raidPtr->parity_good = RF_RAID_CLEAN;
   1114 			return(0);
   1115 		}
   1116 
   1117 		if (raidPtr->parity_rewrite_in_progress == 1) {
   1118 			/* Re-write is already in progress! */
   1119 			return(EINVAL);
   1120 		}
   1121 
   1122 		retcode = RF_CREATE_THREAD(raidPtr->parity_rewrite_thread,
   1123 					   rf_RewriteParityThread,
   1124 					   raidPtr,"raid_parity");
   1125 		return (retcode);
   1126 
   1127 
   1128 	case RAIDFRAME_ADD_HOT_SPARE:
   1129 		sparePtr = (RF_SingleComponent_t *) data;
   1130 		memcpy( &hot_spare, sparePtr, sizeof(RF_SingleComponent_t));
   1131 		retcode = rf_add_hot_spare(raidPtr, &hot_spare);
   1132 		return(retcode);
   1133 
   1134 	case RAIDFRAME_REMOVE_HOT_SPARE:
   1135 		return(retcode);
   1136 
   1137 	case RAIDFRAME_DELETE_COMPONENT:
   1138 		componentPtr = (RF_SingleComponent_t *)data;
   1139 		memcpy( &component, componentPtr,
   1140 			sizeof(RF_SingleComponent_t));
   1141 		retcode = rf_delete_component(raidPtr, &component);
   1142 		return(retcode);
   1143 
   1144 	case RAIDFRAME_INCORPORATE_HOT_SPARE:
   1145 		componentPtr = (RF_SingleComponent_t *)data;
   1146 		memcpy( &component, componentPtr,
   1147 			sizeof(RF_SingleComponent_t));
   1148 		retcode = rf_incorporate_hot_spare(raidPtr, &component);
   1149 		return(retcode);
   1150 
   1151 	case RAIDFRAME_REBUILD_IN_PLACE:
   1152 
   1153 		if (raidPtr->Layout.map->faultsTolerated == 0) {
   1154 			/* Can't do this on a RAID 0!! */
   1155 			return(EINVAL);
   1156 		}
   1157 
   1158 		if (raidPtr->recon_in_progress == 1) {
   1159 			/* a reconstruct is already in progress! */
   1160 			return(EINVAL);
   1161 		}
   1162 
   1163 		componentPtr = (RF_SingleComponent_t *) data;
   1164 		memcpy( &component, componentPtr,
   1165 			sizeof(RF_SingleComponent_t));
   1166 		component.row = 0; /* we don't support any more */
   1167 		column = component.column;
   1168 
   1169 		if ((column < 0) || (column >= raidPtr->numCol)) {
   1170 			return(EINVAL);
   1171 		}
   1172 
   1173 		RF_LOCK_MUTEX(raidPtr->mutex);
   1174 		if ((raidPtr->Disks[column].status == rf_ds_optimal) &&
   1175 		    (raidPtr->numFailures > 0)) {
   1176 			/* XXX 0 above shouldn't be constant!!! */
   1177 			/* some component other than this has failed.
   1178 			   Let's not make things worse than they already
   1179 			   are... */
   1180 			printf("raid%d: Unable to reconstruct to disk at:\n",
   1181 			       raidPtr->raidid);
   1182 			printf("raid%d:     Col: %d   Too many failures.\n",
   1183 			       raidPtr->raidid, column);
   1184 			RF_UNLOCK_MUTEX(raidPtr->mutex);
   1185 			return (EINVAL);
   1186 		}
   1187 		if (raidPtr->Disks[column].status ==
   1188 		    rf_ds_reconstructing) {
   1189 			printf("raid%d: Unable to reconstruct to disk at:\n",
   1190 			       raidPtr->raidid);
   1191 			printf("raid%d:    Col: %d   Reconstruction already occuring!\n", raidPtr->raidid, column);
   1192 
   1193 			RF_UNLOCK_MUTEX(raidPtr->mutex);
   1194 			return (EINVAL);
   1195 		}
   1196 		if (raidPtr->Disks[column].status == rf_ds_spared) {
   1197 			RF_UNLOCK_MUTEX(raidPtr->mutex);
   1198 			return (EINVAL);
   1199 		}
   1200 		RF_UNLOCK_MUTEX(raidPtr->mutex);
   1201 
   1202 		RF_Malloc(rrcopy, sizeof(*rrcopy), (struct rf_recon_req *));
   1203 		if (rrcopy == NULL)
   1204 			return(ENOMEM);
   1205 
   1206 		rrcopy->raidPtr = (void *) raidPtr;
   1207 		rrcopy->col = column;
   1208 
   1209 		retcode = RF_CREATE_THREAD(raidPtr->recon_thread,
   1210 					   rf_ReconstructInPlaceThread,
   1211 					   rrcopy,"raid_reconip");
   1212 		return(retcode);
   1213 
   1214 	case RAIDFRAME_GET_INFO:
   1215 		if (!raidPtr->valid)
   1216 			return (ENODEV);
   1217 		ucfgp = (RF_DeviceConfig_t **) data;
   1218 		RF_Malloc(d_cfg, sizeof(RF_DeviceConfig_t),
   1219 			  (RF_DeviceConfig_t *));
   1220 		if (d_cfg == NULL)
   1221 			return (ENOMEM);
   1222 		memset((char *) d_cfg, 0, sizeof(RF_DeviceConfig_t));
   1223 		d_cfg->rows = 1; /* there is only 1 row now */
   1224 		d_cfg->cols = raidPtr->numCol;
   1225 		d_cfg->ndevs = raidPtr->numCol;
   1226 		if (d_cfg->ndevs >= RF_MAX_DISKS) {
   1227 			RF_Free(d_cfg, sizeof(RF_DeviceConfig_t));
   1228 			return (ENOMEM);
   1229 		}
   1230 		d_cfg->nspares = raidPtr->numSpare;
   1231 		if (d_cfg->nspares >= RF_MAX_DISKS) {
   1232 			RF_Free(d_cfg, sizeof(RF_DeviceConfig_t));
   1233 			return (ENOMEM);
   1234 		}
   1235 		d_cfg->maxqdepth = raidPtr->maxQueueDepth;
   1236 		d = 0;
   1237 		for (j = 0; j < d_cfg->cols; j++) {
   1238 			d_cfg->devs[d] = raidPtr->Disks[j];
   1239 			d++;
   1240 		}
   1241 		for (j = d_cfg->cols, i = 0; i < d_cfg->nspares; i++, j++) {
   1242 			d_cfg->spares[i] = raidPtr->Disks[j];
   1243 		}
   1244 		retcode = copyout(d_cfg, *ucfgp, sizeof(RF_DeviceConfig_t));
   1245 		RF_Free(d_cfg, sizeof(RF_DeviceConfig_t));
   1246 
   1247 		return (retcode);
   1248 
   1249 	case RAIDFRAME_CHECK_PARITY:
   1250 		*(int *) data = raidPtr->parity_good;
   1251 		return (0);
   1252 
   1253 	case RAIDFRAME_RESET_ACCTOTALS:
   1254 		memset(&raidPtr->acc_totals, 0, sizeof(raidPtr->acc_totals));
   1255 		return (0);
   1256 
   1257 	case RAIDFRAME_GET_ACCTOTALS:
   1258 		totals = (RF_AccTotals_t *) data;
   1259 		*totals = raidPtr->acc_totals;
   1260 		return (0);
   1261 
   1262 	case RAIDFRAME_KEEP_ACCTOTALS:
   1263 		raidPtr->keep_acc_totals = *(int *)data;
   1264 		return (0);
   1265 
   1266 	case RAIDFRAME_GET_SIZE:
   1267 		*(int *) data = raidPtr->totalSectors;
   1268 		return (0);
   1269 
   1270 		/* fail a disk & optionally start reconstruction */
   1271 	case RAIDFRAME_FAIL_DISK:
   1272 
   1273 		if (raidPtr->Layout.map->faultsTolerated == 0) {
   1274 			/* Can't do this on a RAID 0!! */
   1275 			return(EINVAL);
   1276 		}
   1277 
   1278 		rr = (struct rf_recon_req *) data;
   1279 		rr->row = 0;
   1280 		if (rr->col < 0 || rr->col >= raidPtr->numCol)
   1281 			return (EINVAL);
   1282 
   1283 
   1284 		RF_LOCK_MUTEX(raidPtr->mutex);
   1285 		if ((raidPtr->Disks[rr->col].status ==
   1286 		     rf_ds_optimal) && (raidPtr->numFailures > 0)) {
   1287 			/* some other component has failed.  Let's not make
   1288 			   things worse. XXX wrong for RAID6 */
   1289 			RF_UNLOCK_MUTEX(raidPtr->mutex);
   1290 			return (EINVAL);
   1291 		}
   1292 		if (raidPtr->Disks[rr->col].status == rf_ds_spared) {
   1293 			/* Can't fail a spared disk! */
   1294 			RF_UNLOCK_MUTEX(raidPtr->mutex);
   1295 			return (EINVAL);
   1296 		}
   1297 		RF_UNLOCK_MUTEX(raidPtr->mutex);
   1298 
   1299 		/* make a copy of the recon request so that we don't rely on
   1300 		 * the user's buffer */
   1301 		RF_Malloc(rrcopy, sizeof(*rrcopy), (struct rf_recon_req *));
   1302 		if (rrcopy == NULL)
   1303 			return(ENOMEM);
   1304 		memcpy(rrcopy, rr, sizeof(*rr));
   1305 		rrcopy->raidPtr = (void *) raidPtr;
   1306 
   1307 		retcode = RF_CREATE_THREAD(raidPtr->recon_thread,
   1308 					   rf_ReconThread,
   1309 					   rrcopy,"raid_recon");
   1310 		return (0);
   1311 
   1312 		/* invoke a copyback operation after recon on whatever disk
   1313 		 * needs it, if any */
   1314 	case RAIDFRAME_COPYBACK:
   1315 
   1316 		if (raidPtr->Layout.map->faultsTolerated == 0) {
   1317 			/* This makes no sense on a RAID 0!! */
   1318 			return(EINVAL);
   1319 		}
   1320 
   1321 		if (raidPtr->copyback_in_progress == 1) {
   1322 			/* Copyback is already in progress! */
   1323 			return(EINVAL);
   1324 		}
   1325 
   1326 		retcode = RF_CREATE_THREAD(raidPtr->copyback_thread,
   1327 					   rf_CopybackThread,
   1328 					   raidPtr,"raid_copyback");
   1329 		return (retcode);
   1330 
   1331 		/* return the percentage completion of reconstruction */
   1332 	case RAIDFRAME_CHECK_RECON_STATUS:
   1333 		if (raidPtr->Layout.map->faultsTolerated == 0) {
   1334 			/* This makes no sense on a RAID 0, so tell the
   1335 			   user it's done. */
   1336 			*(int *) data = 100;
   1337 			return(0);
   1338 		}
   1339 		if (raidPtr->status != rf_rs_reconstructing)
   1340 			*(int *) data = 100;
   1341 		else {
   1342 			if (raidPtr->reconControl->numRUsTotal > 0) {
   1343 				*(int *) data = (raidPtr->reconControl->numRUsComplete * 100 / raidPtr->reconControl->numRUsTotal);
   1344 			} else {
   1345 				*(int *) data = 0;
   1346 			}
   1347 		}
   1348 		return (0);
   1349 	case RAIDFRAME_CHECK_RECON_STATUS_EXT:
   1350 		progressInfoPtr = (RF_ProgressInfo_t **) data;
   1351 		if (raidPtr->status != rf_rs_reconstructing) {
   1352 			progressInfo.remaining = 0;
   1353 			progressInfo.completed = 100;
   1354 			progressInfo.total = 100;
   1355 		} else {
   1356 			progressInfo.total =
   1357 				raidPtr->reconControl->numRUsTotal;
   1358 			progressInfo.completed =
   1359 				raidPtr->reconControl->numRUsComplete;
   1360 			progressInfo.remaining = progressInfo.total -
   1361 				progressInfo.completed;
   1362 		}
   1363 		retcode = copyout(&progressInfo, *progressInfoPtr,
   1364 				  sizeof(RF_ProgressInfo_t));
   1365 		return (retcode);
   1366 
   1367 	case RAIDFRAME_CHECK_PARITYREWRITE_STATUS:
   1368 		if (raidPtr->Layout.map->faultsTolerated == 0) {
   1369 			/* This makes no sense on a RAID 0, so tell the
   1370 			   user it's done. */
   1371 			*(int *) data = 100;
   1372 			return(0);
   1373 		}
   1374 		if (raidPtr->parity_rewrite_in_progress == 1) {
   1375 			*(int *) data = 100 *
   1376 				raidPtr->parity_rewrite_stripes_done /
   1377 				raidPtr->Layout.numStripe;
   1378 		} else {
   1379 			*(int *) data = 100;
   1380 		}
   1381 		return (0);
   1382 
   1383 	case RAIDFRAME_CHECK_PARITYREWRITE_STATUS_EXT:
   1384 		progressInfoPtr = (RF_ProgressInfo_t **) data;
   1385 		if (raidPtr->parity_rewrite_in_progress == 1) {
   1386 			progressInfo.total = raidPtr->Layout.numStripe;
   1387 			progressInfo.completed =
   1388 				raidPtr->parity_rewrite_stripes_done;
   1389 			progressInfo.remaining = progressInfo.total -
   1390 				progressInfo.completed;
   1391 		} else {
   1392 			progressInfo.remaining = 0;
   1393 			progressInfo.completed = 100;
   1394 			progressInfo.total = 100;
   1395 		}
   1396 		retcode = copyout(&progressInfo, *progressInfoPtr,
   1397 				  sizeof(RF_ProgressInfo_t));
   1398 		return (retcode);
   1399 
   1400 	case RAIDFRAME_CHECK_COPYBACK_STATUS:
   1401 		if (raidPtr->Layout.map->faultsTolerated == 0) {
   1402 			/* This makes no sense on a RAID 0 */
   1403 			*(int *) data = 100;
   1404 			return(0);
   1405 		}
   1406 		if (raidPtr->copyback_in_progress == 1) {
   1407 			*(int *) data = 100 * raidPtr->copyback_stripes_done /
   1408 				raidPtr->Layout.numStripe;
   1409 		} else {
   1410 			*(int *) data = 100;
   1411 		}
   1412 		return (0);
   1413 
   1414 	case RAIDFRAME_CHECK_COPYBACK_STATUS_EXT:
   1415 		progressInfoPtr = (RF_ProgressInfo_t **) data;
   1416 		if (raidPtr->copyback_in_progress == 1) {
   1417 			progressInfo.total = raidPtr->Layout.numStripe;
   1418 			progressInfo.completed =
   1419 				raidPtr->copyback_stripes_done;
   1420 			progressInfo.remaining = progressInfo.total -
   1421 				progressInfo.completed;
   1422 		} else {
   1423 			progressInfo.remaining = 0;
   1424 			progressInfo.completed = 100;
   1425 			progressInfo.total = 100;
   1426 		}
   1427 		retcode = copyout(&progressInfo, *progressInfoPtr,
   1428 				  sizeof(RF_ProgressInfo_t));
   1429 		return (retcode);
   1430 
   1431 		/* the sparetable daemon calls this to wait for the kernel to
   1432 		 * need a spare table. this ioctl does not return until a
   1433 		 * spare table is needed. XXX -- calling mpsleep here in the
   1434 		 * ioctl code is almost certainly wrong and evil. -- XXX XXX
   1435 		 * -- I should either compute the spare table in the kernel,
   1436 		 * or have a different -- XXX XXX -- interface (a different
   1437 		 * character device) for delivering the table     -- XXX */
   1438 #if 0
   1439 	case RAIDFRAME_SPARET_WAIT:
   1440 		RF_LOCK_MUTEX(rf_sparet_wait_mutex);
   1441 		while (!rf_sparet_wait_queue)
   1442 			mpsleep(&rf_sparet_wait_queue, (PZERO + 1) | PCATCH, "sparet wait", 0, (void *) simple_lock_addr(rf_sparet_wait_mutex), MS_LOCK_SIMPLE);
   1443 		waitreq = rf_sparet_wait_queue;
   1444 		rf_sparet_wait_queue = rf_sparet_wait_queue->next;
   1445 		RF_UNLOCK_MUTEX(rf_sparet_wait_mutex);
   1446 
   1447 		/* structure assignment */
   1448 		*((RF_SparetWait_t *) data) = *waitreq;
   1449 
   1450 		RF_Free(waitreq, sizeof(*waitreq));
   1451 		return (0);
   1452 
   1453 		/* wakes up a process waiting on SPARET_WAIT and puts an error
   1454 		 * code in it that will cause the dameon to exit */
   1455 	case RAIDFRAME_ABORT_SPARET_WAIT:
   1456 		RF_Malloc(waitreq, sizeof(*waitreq), (RF_SparetWait_t *));
   1457 		waitreq->fcol = -1;
   1458 		RF_LOCK_MUTEX(rf_sparet_wait_mutex);
   1459 		waitreq->next = rf_sparet_wait_queue;
   1460 		rf_sparet_wait_queue = waitreq;
   1461 		RF_UNLOCK_MUTEX(rf_sparet_wait_mutex);
   1462 		wakeup(&rf_sparet_wait_queue);
   1463 		return (0);
   1464 
   1465 		/* used by the spare table daemon to deliver a spare table
   1466 		 * into the kernel */
   1467 	case RAIDFRAME_SEND_SPARET:
   1468 
   1469 		/* install the spare table */
   1470 		retcode = rf_SetSpareTable(raidPtr, *(void **) data);
   1471 
   1472 		/* respond to the requestor.  the return status of the spare
   1473 		 * table installation is passed in the "fcol" field */
   1474 		RF_Malloc(waitreq, sizeof(*waitreq), (RF_SparetWait_t *));
   1475 		waitreq->fcol = retcode;
   1476 		RF_LOCK_MUTEX(rf_sparet_wait_mutex);
   1477 		waitreq->next = rf_sparet_resp_queue;
   1478 		rf_sparet_resp_queue = waitreq;
   1479 		wakeup(&rf_sparet_resp_queue);
   1480 		RF_UNLOCK_MUTEX(rf_sparet_wait_mutex);
   1481 
   1482 		return (retcode);
   1483 #endif
   1484 
   1485 	default:
   1486 		break; /* fall through to the os-specific code below */
   1487 
   1488 	}
   1489 
   1490 	if (!raidPtr->valid)
   1491 		return (EINVAL);
   1492 
   1493 	/*
   1494 	 * Add support for "regular" device ioctls here.
   1495 	 */
   1496 
   1497 	switch (cmd) {
   1498 	case DIOCGDINFO:
   1499 		*(struct disklabel *) data = *(rs->sc_dkdev.dk_label);
   1500 		break;
   1501 #ifdef __HAVE_OLD_DISKLABEL
   1502 	case ODIOCGDINFO:
   1503 		newlabel = *(rs->sc_dkdev.dk_label);
   1504 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1505 			return ENOTTY;
   1506 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
   1507 		break;
   1508 #endif
   1509 
   1510 	case DIOCGPART:
   1511 		((struct partinfo *) data)->disklab = rs->sc_dkdev.dk_label;
   1512 		((struct partinfo *) data)->part =
   1513 		    &rs->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
   1514 		break;
   1515 
   1516 	case DIOCWDINFO:
   1517 	case DIOCSDINFO:
   1518 #ifdef __HAVE_OLD_DISKLABEL
   1519 	case ODIOCWDINFO:
   1520 	case ODIOCSDINFO:
   1521 #endif
   1522 	{
   1523 		struct disklabel *lp;
   1524 #ifdef __HAVE_OLD_DISKLABEL
   1525 		if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
   1526 			memset(&newlabel, 0, sizeof newlabel);
   1527 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
   1528 			lp = &newlabel;
   1529 		} else
   1530 #endif
   1531 		lp = (struct disklabel *)data;
   1532 
   1533 		if ((error = raidlock(rs)) != 0)
   1534 			return (error);
   1535 
   1536 		rs->sc_flags |= RAIDF_LABELLING;
   1537 
   1538 		error = setdisklabel(rs->sc_dkdev.dk_label,
   1539 		    lp, 0, rs->sc_dkdev.dk_cpulabel);
   1540 		if (error == 0) {
   1541 			if (cmd == DIOCWDINFO
   1542 #ifdef __HAVE_OLD_DISKLABEL
   1543 			    || cmd == ODIOCWDINFO
   1544 #endif
   1545 			   )
   1546 				error = writedisklabel(RAIDLABELDEV(dev),
   1547 				    raidstrategy, rs->sc_dkdev.dk_label,
   1548 				    rs->sc_dkdev.dk_cpulabel);
   1549 		}
   1550 		rs->sc_flags &= ~RAIDF_LABELLING;
   1551 
   1552 		raidunlock(rs);
   1553 
   1554 		if (error)
   1555 			return (error);
   1556 		break;
   1557 	}
   1558 
   1559 	case DIOCWLABEL:
   1560 		if (*(int *) data != 0)
   1561 			rs->sc_flags |= RAIDF_WLABEL;
   1562 		else
   1563 			rs->sc_flags &= ~RAIDF_WLABEL;
   1564 		break;
   1565 
   1566 	case DIOCGDEFLABEL:
   1567 		raidgetdefaultlabel(raidPtr, rs, (struct disklabel *) data);
   1568 		break;
   1569 
   1570 #ifdef __HAVE_OLD_DISKLABEL
   1571 	case ODIOCGDEFLABEL:
   1572 		raidgetdefaultlabel(raidPtr, rs, &newlabel);
   1573 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
   1574 			return ENOTTY;
   1575 		memcpy(data, &newlabel, sizeof (struct olddisklabel));
   1576 		break;
   1577 #endif
   1578 
   1579 	default:
   1580 		retcode = ENOTTY;
   1581 	}
   1582 	return (retcode);
   1583 
   1584 }
   1585 
   1586 
   1587 /* raidinit -- complete the rest of the initialization for the
   1588    RAIDframe device.  */
   1589 
   1590 
   1591 static void
   1592 raidinit(RF_Raid_t *raidPtr)
   1593 {
   1594 	struct raid_softc *rs;
   1595 	int     unit;
   1596 
   1597 	unit = raidPtr->raidid;
   1598 
   1599 	rs = &raid_softc[unit];
   1600 
   1601 	/* XXX should check return code first... */
   1602 	rs->sc_flags |= RAIDF_INITED;
   1603 
   1604 	sprintf(rs->sc_xname, "raid%d", unit);	/* XXX doesn't check bounds. */
   1605 
   1606 	rs->sc_dkdev.dk_name = rs->sc_xname;
   1607 
   1608 	/* disk_attach actually creates space for the CPU disklabel, among
   1609 	 * other things, so it's critical to call this *BEFORE* we try putzing
   1610 	 * with disklabels. */
   1611 
   1612 	disk_attach(&rs->sc_dkdev);
   1613 
   1614 	/* XXX There may be a weird interaction here between this, and
   1615 	 * protectedSectors, as used in RAIDframe.  */
   1616 
   1617 	rs->sc_size = raidPtr->totalSectors;
   1618 
   1619 }
   1620 #if (RF_INCLUDE_PARITY_DECLUSTERING_DS > 0)
   1621 /* wake up the daemon & tell it to get us a spare table
   1622  * XXX
   1623  * the entries in the queues should be tagged with the raidPtr
   1624  * so that in the extremely rare case that two recons happen at once,
   1625  * we know for which device were requesting a spare table
   1626  * XXX
   1627  *
   1628  * XXX This code is not currently used. GO
   1629  */
   1630 int
   1631 rf_GetSpareTableFromDaemon(RF_SparetWait_t *req)
   1632 {
   1633 	int     retcode;
   1634 
   1635 	RF_LOCK_MUTEX(rf_sparet_wait_mutex);
   1636 	req->next = rf_sparet_wait_queue;
   1637 	rf_sparet_wait_queue = req;
   1638 	wakeup(&rf_sparet_wait_queue);
   1639 
   1640 	/* mpsleep unlocks the mutex */
   1641 	while (!rf_sparet_resp_queue) {
   1642 		tsleep(&rf_sparet_resp_queue, PRIBIO,
   1643 		    "raidframe getsparetable", 0);
   1644 	}
   1645 	req = rf_sparet_resp_queue;
   1646 	rf_sparet_resp_queue = req->next;
   1647 	RF_UNLOCK_MUTEX(rf_sparet_wait_mutex);
   1648 
   1649 	retcode = req->fcol;
   1650 	RF_Free(req, sizeof(*req));	/* this is not the same req as we
   1651 					 * alloc'd */
   1652 	return (retcode);
   1653 }
   1654 #endif
   1655 
   1656 /* a wrapper around rf_DoAccess that extracts appropriate info from the
   1657  * bp & passes it down.
   1658  * any calls originating in the kernel must use non-blocking I/O
   1659  * do some extra sanity checking to return "appropriate" error values for
   1660  * certain conditions (to make some standard utilities work)
   1661  *
   1662  * Formerly known as: rf_DoAccessKernel
   1663  */
   1664 void
   1665 raidstart(RF_Raid_t *raidPtr)
   1666 {
   1667 	RF_SectorCount_t num_blocks, pb, sum;
   1668 	RF_RaidAddr_t raid_addr;
   1669 	struct partition *pp;
   1670 	daddr_t blocknum;
   1671 	int     unit;
   1672 	struct raid_softc *rs;
   1673 	int     do_async;
   1674 	struct buf *bp;
   1675 
   1676 	unit = raidPtr->raidid;
   1677 	rs = &raid_softc[unit];
   1678 
   1679 	/* quick check to see if anything has died recently */
   1680 	RF_LOCK_MUTEX(raidPtr->mutex);
   1681 	if (raidPtr->numNewFailures > 0) {
   1682 		RF_UNLOCK_MUTEX(raidPtr->mutex);
   1683 		rf_update_component_labels(raidPtr,
   1684 					   RF_NORMAL_COMPONENT_UPDATE);
   1685 		RF_LOCK_MUTEX(raidPtr->mutex);
   1686 		raidPtr->numNewFailures--;
   1687 	}
   1688 
   1689 	/* Check to see if we're at the limit... */
   1690 	while (raidPtr->openings > 0) {
   1691 		RF_UNLOCK_MUTEX(raidPtr->mutex);
   1692 
   1693 		/* get the next item, if any, from the queue */
   1694 		if ((bp = BUFQ_GET(&rs->buf_queue)) == NULL) {
   1695 			/* nothing more to do */
   1696 			return;
   1697 		}
   1698 
   1699 		/* Ok, for the bp we have here, bp->b_blkno is relative to the
   1700 		 * partition.. Need to make it absolute to the underlying
   1701 		 * device.. */
   1702 
   1703 		blocknum = bp->b_blkno;
   1704 		if (DISKPART(bp->b_dev) != RAW_PART) {
   1705 			pp = &rs->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
   1706 			blocknum += pp->p_offset;
   1707 		}
   1708 
   1709 		db1_printf(("Blocks: %d, %d\n", (int) bp->b_blkno,
   1710 			    (int) blocknum));
   1711 
   1712 		db1_printf(("bp->b_bcount = %d\n", (int) bp->b_bcount));
   1713 		db1_printf(("bp->b_resid = %d\n", (int) bp->b_resid));
   1714 
   1715 		/* *THIS* is where we adjust what block we're going to...
   1716 		 * but DO NOT TOUCH bp->b_blkno!!! */
   1717 		raid_addr = blocknum;
   1718 
   1719 		num_blocks = bp->b_bcount >> raidPtr->logBytesPerSector;
   1720 		pb = (bp->b_bcount & raidPtr->sectorMask) ? 1 : 0;
   1721 		sum = raid_addr + num_blocks + pb;
   1722 		if (1 || rf_debugKernelAccess) {
   1723 			db1_printf(("raid_addr=%d sum=%d num_blocks=%d(+%d) (%d)\n",
   1724 				    (int) raid_addr, (int) sum, (int) num_blocks,
   1725 				    (int) pb, (int) bp->b_resid));
   1726 		}
   1727 		if ((sum > raidPtr->totalSectors) || (sum < raid_addr)
   1728 		    || (sum < num_blocks) || (sum < pb)) {
   1729 			bp->b_error = ENOSPC;
   1730 			bp->b_flags |= B_ERROR;
   1731 			bp->b_resid = bp->b_bcount;
   1732 			biodone(bp);
   1733 			RF_LOCK_MUTEX(raidPtr->mutex);
   1734 			continue;
   1735 		}
   1736 		/*
   1737 		 * XXX rf_DoAccess() should do this, not just DoAccessKernel()
   1738 		 */
   1739 
   1740 		if (bp->b_bcount & raidPtr->sectorMask) {
   1741 			bp->b_error = EINVAL;
   1742 			bp->b_flags |= B_ERROR;
   1743 			bp->b_resid = bp->b_bcount;
   1744 			biodone(bp);
   1745 			RF_LOCK_MUTEX(raidPtr->mutex);
   1746 			continue;
   1747 
   1748 		}
   1749 		db1_printf(("Calling DoAccess..\n"));
   1750 
   1751 
   1752 		RF_LOCK_MUTEX(raidPtr->mutex);
   1753 		raidPtr->openings--;
   1754 		RF_UNLOCK_MUTEX(raidPtr->mutex);
   1755 
   1756 		/*
   1757 		 * Everything is async.
   1758 		 */
   1759 		do_async = 1;
   1760 
   1761 		disk_busy(&rs->sc_dkdev);
   1762 
   1763 		/* XXX we're still at splbio() here... do we *really*
   1764 		   need to be? */
   1765 
   1766 		/* don't ever condition on bp->b_flags & B_WRITE.
   1767 		 * always condition on B_READ instead */
   1768 
   1769 		bp->b_error = rf_DoAccess(raidPtr, (bp->b_flags & B_READ) ?
   1770 				      RF_IO_TYPE_READ : RF_IO_TYPE_WRITE,
   1771 				      do_async, raid_addr, num_blocks,
   1772 				      bp->b_data, bp, RF_DAG_NONBLOCKING_IO);
   1773 
   1774 		if (bp->b_error) {
   1775 			bp->b_flags |= B_ERROR;
   1776 		}
   1777 
   1778 		RF_LOCK_MUTEX(raidPtr->mutex);
   1779 	}
   1780 	RF_UNLOCK_MUTEX(raidPtr->mutex);
   1781 }
   1782 
   1783 
   1784 
   1785 
   1786 /* invoke an I/O from kernel mode.  Disk queue should be locked upon entry */
   1787 
   1788 int
   1789 rf_DispatchKernelIO(RF_DiskQueue_t *queue, RF_DiskQueueData_t *req)
   1790 {
   1791 	int     op = (req->type == RF_IO_TYPE_READ) ? B_READ : B_WRITE;
   1792 	struct buf *bp;
   1793 	struct raidbuf *raidbp = NULL;
   1794 
   1795 	req->queue = queue;
   1796 
   1797 #if DIAGNOSTIC
   1798 	if (queue->raidPtr->raidid >= numraid) {
   1799 		printf("Invalid unit number: %d %d\n", queue->raidPtr->raidid,
   1800 		    numraid);
   1801 		panic("Invalid Unit number in rf_DispatchKernelIO");
   1802 	}
   1803 #endif
   1804 
   1805 	bp = req->bp;
   1806 #if 1
   1807 	/* XXX when there is a physical disk failure, someone is passing us a
   1808 	 * buffer that contains old stuff!!  Attempt to deal with this problem
   1809 	 * without taking a performance hit... (not sure where the real bug
   1810 	 * is.  It's buried in RAIDframe somewhere) :-(  GO ) */
   1811 
   1812 	if (bp->b_flags & B_ERROR) {
   1813 		bp->b_flags &= ~B_ERROR;
   1814 	}
   1815 	if (bp->b_error != 0) {
   1816 		bp->b_error = 0;
   1817 	}
   1818 #endif
   1819 	raidbp = pool_get(&raidframe_cbufpool, PR_NOWAIT);
   1820 	if (raidbp == NULL) {
   1821 		bp->b_flags |= B_ERROR;
   1822 		bp->b_error = ENOMEM;
   1823 		return (ENOMEM);
   1824 	}
   1825 	BUF_INIT(&raidbp->rf_buf);
   1826 
   1827 	/*
   1828 	 * context for raidiodone
   1829 	 */
   1830 	raidbp->rf_obp = bp;
   1831 	raidbp->req = req;
   1832 
   1833 	BIO_COPYPRIO(&raidbp->rf_buf, bp);
   1834 
   1835 	switch (req->type) {
   1836 	case RF_IO_TYPE_NOP:	/* used primarily to unlock a locked queue */
   1837 		/* XXX need to do something extra here.. */
   1838 		/* I'm leaving this in, as I've never actually seen it used,
   1839 		 * and I'd like folks to report it... GO */
   1840 		printf(("WAKEUP CALLED\n"));
   1841 		queue->numOutstanding++;
   1842 
   1843 		/* XXX need to glue the original buffer into this??  */
   1844 
   1845 		KernelWakeupFunc(&raidbp->rf_buf);
   1846 		break;
   1847 
   1848 	case RF_IO_TYPE_READ:
   1849 	case RF_IO_TYPE_WRITE:
   1850 
   1851 		if (req->tracerec) {
   1852 			RF_ETIMER_START(req->tracerec->timer);
   1853 		}
   1854 		InitBP(&raidbp->rf_buf, queue->rf_cinfo->ci_vp,
   1855 		    op | bp->b_flags, queue->rf_cinfo->ci_dev,
   1856 		    req->sectorOffset, req->numSector,
   1857 		    req->buf, KernelWakeupFunc, (void *) req,
   1858 		    queue->raidPtr->logBytesPerSector, req->b_proc);
   1859 
   1860 		if (rf_debugKernelAccess) {
   1861 			db1_printf(("dispatch: bp->b_blkno = %ld\n",
   1862 				(long) bp->b_blkno));
   1863 		}
   1864 		queue->numOutstanding++;
   1865 		queue->last_deq_sector = req->sectorOffset;
   1866 		/* acc wouldn't have been let in if there were any pending
   1867 		 * reqs at any other priority */
   1868 		queue->curPriority = req->priority;
   1869 
   1870 		db1_printf(("Going for %c to unit %d col %d\n",
   1871 			    req->type, queue->raidPtr->raidid,
   1872 			    queue->col));
   1873 		db1_printf(("sector %d count %d (%d bytes) %d\n",
   1874 			(int) req->sectorOffset, (int) req->numSector,
   1875 			(int) (req->numSector <<
   1876 			    queue->raidPtr->logBytesPerSector),
   1877 			(int) queue->raidPtr->logBytesPerSector));
   1878 		if ((raidbp->rf_buf.b_flags & B_READ) == 0) {
   1879 			raidbp->rf_buf.b_vp->v_numoutput++;
   1880 		}
   1881 		VOP_STRATEGY(raidbp->rf_buf.b_vp, &raidbp->rf_buf);
   1882 
   1883 		break;
   1884 
   1885 	default:
   1886 		panic("bad req->type in rf_DispatchKernelIO");
   1887 	}
   1888 	db1_printf(("Exiting from DispatchKernelIO\n"));
   1889 
   1890 	return (0);
   1891 }
   1892 /* this is the callback function associated with a I/O invoked from
   1893    kernel code.
   1894  */
   1895 static void
   1896 KernelWakeupFunc(struct buf *vbp)
   1897 {
   1898 	RF_DiskQueueData_t *req = NULL;
   1899 	RF_DiskQueue_t *queue;
   1900 	struct raidbuf *raidbp = (struct raidbuf *) vbp;
   1901 	struct buf *bp;
   1902 	int s;
   1903 
   1904 	s = splbio();
   1905 	db1_printf(("recovering the request queue:\n"));
   1906 	req = raidbp->req;
   1907 
   1908 	bp = raidbp->rf_obp;
   1909 
   1910 	queue = (RF_DiskQueue_t *) req->queue;
   1911 
   1912 	if (raidbp->rf_buf.b_flags & B_ERROR) {
   1913 		bp->b_flags |= B_ERROR;
   1914 		bp->b_error = raidbp->rf_buf.b_error ?
   1915 		    raidbp->rf_buf.b_error : EIO;
   1916 	}
   1917 
   1918 	/* XXX methinks this could be wrong... */
   1919 #if 1
   1920 	bp->b_resid = raidbp->rf_buf.b_resid;
   1921 #endif
   1922 
   1923 	if (req->tracerec) {
   1924 		RF_ETIMER_STOP(req->tracerec->timer);
   1925 		RF_ETIMER_EVAL(req->tracerec->timer);
   1926 		RF_LOCK_MUTEX(rf_tracing_mutex);
   1927 		req->tracerec->diskwait_us += RF_ETIMER_VAL_US(req->tracerec->timer);
   1928 		req->tracerec->phys_io_us += RF_ETIMER_VAL_US(req->tracerec->timer);
   1929 		req->tracerec->num_phys_ios++;
   1930 		RF_UNLOCK_MUTEX(rf_tracing_mutex);
   1931 	}
   1932 	bp->b_bcount = raidbp->rf_buf.b_bcount;	/* XXXX ?? */
   1933 
   1934 	/* XXX Ok, let's get aggressive... If B_ERROR is set, let's go
   1935 	 * ballistic, and mark the component as hosed... */
   1936 
   1937 	if (bp->b_flags & B_ERROR) {
   1938 		/* Mark the disk as dead */
   1939 		/* but only mark it once... */
   1940 		if (queue->raidPtr->Disks[queue->col].status ==
   1941 		    rf_ds_optimal) {
   1942 			printf("raid%d: IO Error.  Marking %s as failed.\n",
   1943 			       queue->raidPtr->raidid,
   1944 			       queue->raidPtr->Disks[queue->col].devname);
   1945 			queue->raidPtr->Disks[queue->col].status =
   1946 			    rf_ds_failed;
   1947 			queue->raidPtr->status = rf_rs_degraded;
   1948 			queue->raidPtr->numFailures++;
   1949 			queue->raidPtr->numNewFailures++;
   1950 		} else {	/* Disk is already dead... */
   1951 			/* printf("Disk already marked as dead!\n"); */
   1952 		}
   1953 
   1954 	}
   1955 
   1956 	pool_put(&raidframe_cbufpool, raidbp);
   1957 
   1958 	/* Fill in the error value */
   1959 
   1960 	req->error = (bp->b_flags & B_ERROR) ? bp->b_error : 0;
   1961 
   1962 	simple_lock(&queue->raidPtr->iodone_lock);
   1963 
   1964 	/* Drop this one on the "finished" queue... */
   1965 	TAILQ_INSERT_TAIL(&(queue->raidPtr->iodone), req, iodone_entries);
   1966 
   1967 	/* Let the raidio thread know there is work to be done. */
   1968 	wakeup(&(queue->raidPtr->iodone));
   1969 
   1970 	simple_unlock(&queue->raidPtr->iodone_lock);
   1971 
   1972 	splx(s);
   1973 }
   1974 
   1975 
   1976 
   1977 /*
   1978  * initialize a buf structure for doing an I/O in the kernel.
   1979  */
   1980 static void
   1981 InitBP(struct buf *bp, struct vnode *b_vp, unsigned rw_flag, dev_t dev,
   1982        RF_SectorNum_t startSect, RF_SectorCount_t numSect, caddr_t buf,
   1983        void (*cbFunc) (struct buf *), void *cbArg, int logBytesPerSector,
   1984        struct proc *b_proc)
   1985 {
   1986 	/* bp->b_flags       = B_PHYS | rw_flag; */
   1987 	bp->b_flags = B_CALL | rw_flag;	/* XXX need B_PHYS here too??? */
   1988 	bp->b_bcount = numSect << logBytesPerSector;
   1989 	bp->b_bufsize = bp->b_bcount;
   1990 	bp->b_error = 0;
   1991 	bp->b_dev = dev;
   1992 	bp->b_data = buf;
   1993 	bp->b_blkno = startSect;
   1994 	bp->b_resid = bp->b_bcount;	/* XXX is this right!??!?!! */
   1995 	if (bp->b_bcount == 0) {
   1996 		panic("bp->b_bcount is zero in InitBP!!");
   1997 	}
   1998 	bp->b_proc = b_proc;
   1999 	bp->b_iodone = cbFunc;
   2000 	bp->b_vp = b_vp;
   2001 
   2002 }
   2003 
   2004 static void
   2005 raidgetdefaultlabel(RF_Raid_t *raidPtr, struct raid_softc *rs,
   2006 		    struct disklabel *lp)
   2007 {
   2008 	memset(lp, 0, sizeof(*lp));
   2009 
   2010 	/* fabricate a label... */
   2011 	lp->d_secperunit = raidPtr->totalSectors;
   2012 	lp->d_secsize = raidPtr->bytesPerSector;
   2013 	lp->d_nsectors = raidPtr->Layout.dataSectorsPerStripe;
   2014 	lp->d_ntracks = 4 * raidPtr->numCol;
   2015 	lp->d_ncylinders = raidPtr->totalSectors /
   2016 		(lp->d_nsectors * lp->d_ntracks);
   2017 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   2018 
   2019 	strncpy(lp->d_typename, "raid", sizeof(lp->d_typename));
   2020 	lp->d_type = DTYPE_RAID;
   2021 	strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   2022 	lp->d_rpm = 3600;
   2023 	lp->d_interleave = 1;
   2024 	lp->d_flags = 0;
   2025 
   2026 	lp->d_partitions[RAW_PART].p_offset = 0;
   2027 	lp->d_partitions[RAW_PART].p_size = raidPtr->totalSectors;
   2028 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   2029 	lp->d_npartitions = RAW_PART + 1;
   2030 
   2031 	lp->d_magic = DISKMAGIC;
   2032 	lp->d_magic2 = DISKMAGIC;
   2033 	lp->d_checksum = dkcksum(rs->sc_dkdev.dk_label);
   2034 
   2035 }
   2036 /*
   2037  * Read the disklabel from the raid device.  If one is not present, fake one
   2038  * up.
   2039  */
   2040 static void
   2041 raidgetdisklabel(dev_t dev)
   2042 {
   2043 	int     unit = raidunit(dev);
   2044 	struct raid_softc *rs = &raid_softc[unit];
   2045 	const char   *errstring;
   2046 	struct disklabel *lp = rs->sc_dkdev.dk_label;
   2047 	struct cpu_disklabel *clp = rs->sc_dkdev.dk_cpulabel;
   2048 	RF_Raid_t *raidPtr;
   2049 
   2050 	db1_printf(("Getting the disklabel...\n"));
   2051 
   2052 	memset(clp, 0, sizeof(*clp));
   2053 
   2054 	raidPtr = raidPtrs[unit];
   2055 
   2056 	raidgetdefaultlabel(raidPtr, rs, lp);
   2057 
   2058 	/*
   2059 	 * Call the generic disklabel extraction routine.
   2060 	 */
   2061 	errstring = readdisklabel(RAIDLABELDEV(dev), raidstrategy,
   2062 	    rs->sc_dkdev.dk_label, rs->sc_dkdev.dk_cpulabel);
   2063 	if (errstring)
   2064 		raidmakedisklabel(rs);
   2065 	else {
   2066 		int     i;
   2067 		struct partition *pp;
   2068 
   2069 		/*
   2070 		 * Sanity check whether the found disklabel is valid.
   2071 		 *
   2072 		 * This is necessary since total size of the raid device
   2073 		 * may vary when an interleave is changed even though exactly
   2074 		 * same componets are used, and old disklabel may used
   2075 		 * if that is found.
   2076 		 */
   2077 		if (lp->d_secperunit != rs->sc_size)
   2078 			printf("raid%d: WARNING: %s: "
   2079 			    "total sector size in disklabel (%d) != "
   2080 			    "the size of raid (%ld)\n", unit, rs->sc_xname,
   2081 			    lp->d_secperunit, (long) rs->sc_size);
   2082 		for (i = 0; i < lp->d_npartitions; i++) {
   2083 			pp = &lp->d_partitions[i];
   2084 			if (pp->p_offset + pp->p_size > rs->sc_size)
   2085 				printf("raid%d: WARNING: %s: end of partition `%c' "
   2086 				       "exceeds the size of raid (%ld)\n",
   2087 				       unit, rs->sc_xname, 'a' + i, (long) rs->sc_size);
   2088 		}
   2089 	}
   2090 
   2091 }
   2092 /*
   2093  * Take care of things one might want to take care of in the event
   2094  * that a disklabel isn't present.
   2095  */
   2096 static void
   2097 raidmakedisklabel(struct raid_softc *rs)
   2098 {
   2099 	struct disklabel *lp = rs->sc_dkdev.dk_label;
   2100 	db1_printf(("Making a label..\n"));
   2101 
   2102 	/*
   2103 	 * For historical reasons, if there's no disklabel present
   2104 	 * the raw partition must be marked FS_BSDFFS.
   2105 	 */
   2106 
   2107 	lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
   2108 
   2109 	strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
   2110 
   2111 	lp->d_checksum = dkcksum(lp);
   2112 }
   2113 /*
   2114  * Lookup the provided name in the filesystem.  If the file exists,
   2115  * is a valid block device, and isn't being used by anyone else,
   2116  * set *vpp to the file's vnode.
   2117  * You'll find the original of this in ccd.c
   2118  */
   2119 int
   2120 raidlookup(char *path, struct proc *p, struct vnode **vpp)
   2121 {
   2122 	struct nameidata nd;
   2123 	struct vnode *vp;
   2124 	struct vattr va;
   2125 	int     error;
   2126 
   2127 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, p);
   2128 	if ((error = vn_open(&nd, FREAD | FWRITE, 0)) != 0) {
   2129 		return (error);
   2130 	}
   2131 	vp = nd.ni_vp;
   2132 	if (vp->v_usecount > 1) {
   2133 		VOP_UNLOCK(vp, 0);
   2134 		(void) vn_close(vp, FREAD | FWRITE, p->p_ucred, p);
   2135 		return (EBUSY);
   2136 	}
   2137 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0) {
   2138 		VOP_UNLOCK(vp, 0);
   2139 		(void) vn_close(vp, FREAD | FWRITE, p->p_ucred, p);
   2140 		return (error);
   2141 	}
   2142 	/* XXX: eventually we should handle VREG, too. */
   2143 	if (va.va_type != VBLK) {
   2144 		VOP_UNLOCK(vp, 0);
   2145 		(void) vn_close(vp, FREAD | FWRITE, p->p_ucred, p);
   2146 		return (ENOTBLK);
   2147 	}
   2148 	VOP_UNLOCK(vp, 0);
   2149 	*vpp = vp;
   2150 	return (0);
   2151 }
   2152 /*
   2153  * Wait interruptibly for an exclusive lock.
   2154  *
   2155  * XXX
   2156  * Several drivers do this; it should be abstracted and made MP-safe.
   2157  * (Hmm... where have we seen this warning before :->  GO )
   2158  */
   2159 static int
   2160 raidlock(struct raid_softc *rs)
   2161 {
   2162 	int     error;
   2163 
   2164 	while ((rs->sc_flags & RAIDF_LOCKED) != 0) {
   2165 		rs->sc_flags |= RAIDF_WANTED;
   2166 		if ((error =
   2167 			tsleep(rs, PRIBIO | PCATCH, "raidlck", 0)) != 0)
   2168 			return (error);
   2169 	}
   2170 	rs->sc_flags |= RAIDF_LOCKED;
   2171 	return (0);
   2172 }
   2173 /*
   2174  * Unlock and wake up any waiters.
   2175  */
   2176 static void
   2177 raidunlock(struct raid_softc *rs)
   2178 {
   2179 
   2180 	rs->sc_flags &= ~RAIDF_LOCKED;
   2181 	if ((rs->sc_flags & RAIDF_WANTED) != 0) {
   2182 		rs->sc_flags &= ~RAIDF_WANTED;
   2183 		wakeup(rs);
   2184 	}
   2185 }
   2186 
   2187 
   2188 #define RF_COMPONENT_INFO_OFFSET  16384 /* bytes */
   2189 #define RF_COMPONENT_INFO_SIZE     1024 /* bytes */
   2190 
   2191 int
   2192 raidmarkclean(dev_t dev, struct vnode *b_vp, int mod_counter)
   2193 {
   2194 	RF_ComponentLabel_t clabel;
   2195 	raidread_component_label(dev, b_vp, &clabel);
   2196 	clabel.mod_counter = mod_counter;
   2197 	clabel.clean = RF_RAID_CLEAN;
   2198 	raidwrite_component_label(dev, b_vp, &clabel);
   2199 	return(0);
   2200 }
   2201 
   2202 
   2203 int
   2204 raidmarkdirty(dev_t dev, struct vnode *b_vp, int mod_counter)
   2205 {
   2206 	RF_ComponentLabel_t clabel;
   2207 	raidread_component_label(dev, b_vp, &clabel);
   2208 	clabel.mod_counter = mod_counter;
   2209 	clabel.clean = RF_RAID_DIRTY;
   2210 	raidwrite_component_label(dev, b_vp, &clabel);
   2211 	return(0);
   2212 }
   2213 
   2214 /* ARGSUSED */
   2215 int
   2216 raidread_component_label(dev_t dev, struct vnode *b_vp,
   2217 			 RF_ComponentLabel_t *clabel)
   2218 {
   2219 	struct buf *bp;
   2220 	const struct bdevsw *bdev;
   2221 	int error;
   2222 
   2223 	/* XXX should probably ensure that we don't try to do this if
   2224 	   someone has changed rf_protected_sectors. */
   2225 
   2226 	if (b_vp == NULL) {
   2227 		/* For whatever reason, this component is not valid.
   2228 		   Don't try to read a component label from it. */
   2229 		return(EINVAL);
   2230 	}
   2231 
   2232 	/* get a block of the appropriate size... */
   2233 	bp = geteblk((int)RF_COMPONENT_INFO_SIZE);
   2234 	bp->b_dev = dev;
   2235 
   2236 	/* get our ducks in a row for the read */
   2237 	bp->b_blkno = RF_COMPONENT_INFO_OFFSET / DEV_BSIZE;
   2238 	bp->b_bcount = RF_COMPONENT_INFO_SIZE;
   2239 	bp->b_flags |= B_READ;
   2240  	bp->b_resid = RF_COMPONENT_INFO_SIZE / DEV_BSIZE;
   2241 
   2242 	bdev = bdevsw_lookup(bp->b_dev);
   2243 	if (bdev == NULL)
   2244 		return (ENXIO);
   2245 	(*bdev->d_strategy)(bp);
   2246 
   2247 	error = biowait(bp);
   2248 
   2249 	if (!error) {
   2250 		memcpy(clabel, bp->b_data,
   2251 		       sizeof(RF_ComponentLabel_t));
   2252         }
   2253 
   2254 	brelse(bp);
   2255 	return(error);
   2256 }
   2257 /* ARGSUSED */
   2258 int
   2259 raidwrite_component_label(dev_t dev, struct vnode *b_vp,
   2260 			  RF_ComponentLabel_t *clabel)
   2261 {
   2262 	struct buf *bp;
   2263 	const struct bdevsw *bdev;
   2264 	int error;
   2265 
   2266 	/* get a block of the appropriate size... */
   2267 	bp = geteblk((int)RF_COMPONENT_INFO_SIZE);
   2268 	bp->b_dev = dev;
   2269 
   2270 	/* get our ducks in a row for the write */
   2271 	bp->b_blkno = RF_COMPONENT_INFO_OFFSET / DEV_BSIZE;
   2272 	bp->b_bcount = RF_COMPONENT_INFO_SIZE;
   2273 	bp->b_flags |= B_WRITE;
   2274  	bp->b_resid = RF_COMPONENT_INFO_SIZE / DEV_BSIZE;
   2275 
   2276 	memset(bp->b_data, 0, RF_COMPONENT_INFO_SIZE );
   2277 
   2278 	memcpy(bp->b_data, clabel, sizeof(RF_ComponentLabel_t));
   2279 
   2280 	bdev = bdevsw_lookup(bp->b_dev);
   2281 	if (bdev == NULL)
   2282 		return (ENXIO);
   2283 	(*bdev->d_strategy)(bp);
   2284 	error = biowait(bp);
   2285 	brelse(bp);
   2286 	if (error) {
   2287 #if 1
   2288 		printf("Failed to write RAID component info!\n");
   2289 #endif
   2290 	}
   2291 
   2292 	return(error);
   2293 }
   2294 
   2295 void
   2296 rf_markalldirty(RF_Raid_t *raidPtr)
   2297 {
   2298 	RF_ComponentLabel_t clabel;
   2299 	int sparecol;
   2300 	int c;
   2301 	int j;
   2302 	int scol = -1;
   2303 
   2304 	raidPtr->mod_counter++;
   2305 	for (c = 0; c < raidPtr->numCol; c++) {
   2306 		/* we don't want to touch (at all) a disk that has
   2307 		   failed */
   2308 		if (!RF_DEAD_DISK(raidPtr->Disks[c].status)) {
   2309 			raidread_component_label(
   2310 						 raidPtr->Disks[c].dev,
   2311 						 raidPtr->raid_cinfo[c].ci_vp,
   2312 						 &clabel);
   2313 			if (clabel.status == rf_ds_spared) {
   2314 				/* XXX do something special...
   2315 				   but whatever you do, don't
   2316 				   try to access it!! */
   2317 			} else {
   2318 				raidmarkdirty(
   2319 					      raidPtr->Disks[c].dev,
   2320 					      raidPtr->raid_cinfo[c].ci_vp,
   2321 					      raidPtr->mod_counter);
   2322 			}
   2323 		}
   2324 	}
   2325 
   2326 	for( c = 0; c < raidPtr->numSpare ; c++) {
   2327 		sparecol = raidPtr->numCol + c;
   2328 		if (raidPtr->Disks[sparecol].status == rf_ds_used_spare) {
   2329 			/*
   2330 
   2331 			   we claim this disk is "optimal" if it's
   2332 			   rf_ds_used_spare, as that means it should be
   2333 			   directly substitutable for the disk it replaced.
   2334 			   We note that too...
   2335 
   2336 			 */
   2337 
   2338 			for(j=0;j<raidPtr->numCol;j++) {
   2339 				if (raidPtr->Disks[j].spareCol == sparecol) {
   2340 					scol = j;
   2341 					break;
   2342 				}
   2343 			}
   2344 
   2345 			raidread_component_label(
   2346 				 raidPtr->Disks[sparecol].dev,
   2347 				 raidPtr->raid_cinfo[sparecol].ci_vp,
   2348 				 &clabel);
   2349 			/* make sure status is noted */
   2350 
   2351 			raid_init_component_label(raidPtr, &clabel);
   2352 
   2353 			clabel.row = 0;
   2354 			clabel.column = scol;
   2355 			/* Note: we *don't* change status from rf_ds_used_spare
   2356 			   to rf_ds_optimal */
   2357 			/* clabel.status = rf_ds_optimal; */
   2358 
   2359 			raidmarkdirty(raidPtr->Disks[sparecol].dev,
   2360 				      raidPtr->raid_cinfo[sparecol].ci_vp,
   2361 				      raidPtr->mod_counter);
   2362 		}
   2363 	}
   2364 }
   2365 
   2366 
   2367 void
   2368 rf_update_component_labels(RF_Raid_t *raidPtr, int final)
   2369 {
   2370 	RF_ComponentLabel_t clabel;
   2371 	int sparecol;
   2372 	int c;
   2373 	int j;
   2374 	int scol;
   2375 
   2376 	scol = -1;
   2377 
   2378 	/* XXX should do extra checks to make sure things really are clean,
   2379 	   rather than blindly setting the clean bit... */
   2380 
   2381 	raidPtr->mod_counter++;
   2382 
   2383 	for (c = 0; c < raidPtr->numCol; c++) {
   2384 		if (raidPtr->Disks[c].status == rf_ds_optimal) {
   2385 			raidread_component_label(
   2386 						 raidPtr->Disks[c].dev,
   2387 						 raidPtr->raid_cinfo[c].ci_vp,
   2388 						 &clabel);
   2389 				/* make sure status is noted */
   2390 			clabel.status = rf_ds_optimal;
   2391 				/* bump the counter */
   2392 			clabel.mod_counter = raidPtr->mod_counter;
   2393 
   2394 			raidwrite_component_label(
   2395 						  raidPtr->Disks[c].dev,
   2396 						  raidPtr->raid_cinfo[c].ci_vp,
   2397 						  &clabel);
   2398 			if (final == RF_FINAL_COMPONENT_UPDATE) {
   2399 				if (raidPtr->parity_good == RF_RAID_CLEAN) {
   2400 					raidmarkclean(
   2401 						      raidPtr->Disks[c].dev,
   2402 						      raidPtr->raid_cinfo[c].ci_vp,
   2403 						      raidPtr->mod_counter);
   2404 				}
   2405 			}
   2406 		}
   2407 		/* else we don't touch it.. */
   2408 	}
   2409 
   2410 	for( c = 0; c < raidPtr->numSpare ; c++) {
   2411 		sparecol = raidPtr->numCol + c;
   2412 		/* Need to ensure that the reconstruct actually completed! */
   2413 		if (raidPtr->Disks[sparecol].status == rf_ds_used_spare) {
   2414 			/*
   2415 
   2416 			   we claim this disk is "optimal" if it's
   2417 			   rf_ds_used_spare, as that means it should be
   2418 			   directly substitutable for the disk it replaced.
   2419 			   We note that too...
   2420 
   2421 			 */
   2422 
   2423 			for(j=0;j<raidPtr->numCol;j++) {
   2424 				if (raidPtr->Disks[j].spareCol == sparecol) {
   2425 					scol = j;
   2426 					break;
   2427 				}
   2428 			}
   2429 
   2430 			/* XXX shouldn't *really* need this... */
   2431 			raidread_component_label(
   2432 				      raidPtr->Disks[sparecol].dev,
   2433 				      raidPtr->raid_cinfo[sparecol].ci_vp,
   2434 				      &clabel);
   2435 			/* make sure status is noted */
   2436 
   2437 			raid_init_component_label(raidPtr, &clabel);
   2438 
   2439 			clabel.mod_counter = raidPtr->mod_counter;
   2440 			clabel.column = scol;
   2441 			clabel.status = rf_ds_optimal;
   2442 
   2443 			raidwrite_component_label(
   2444 				      raidPtr->Disks[sparecol].dev,
   2445 				      raidPtr->raid_cinfo[sparecol].ci_vp,
   2446 				      &clabel);
   2447 			if (final == RF_FINAL_COMPONENT_UPDATE) {
   2448 				if (raidPtr->parity_good == RF_RAID_CLEAN) {
   2449 					raidmarkclean( raidPtr->Disks[sparecol].dev,
   2450 						       raidPtr->raid_cinfo[sparecol].ci_vp,
   2451 						       raidPtr->mod_counter);
   2452 				}
   2453 			}
   2454 		}
   2455 	}
   2456 }
   2457 
   2458 void
   2459 rf_close_component(RF_Raid_t *raidPtr, struct vnode *vp, int auto_configured)
   2460 {
   2461 	struct proc *p;
   2462 
   2463 	p = raidPtr->engine_thread;
   2464 
   2465 	if (vp != NULL) {
   2466 		if (auto_configured == 1) {
   2467 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2468 			VOP_CLOSE(vp, FREAD | FWRITE, NOCRED, 0);
   2469 			vput(vp);
   2470 
   2471 		} else {
   2472 			(void) vn_close(vp, FREAD | FWRITE, p->p_ucred, p);
   2473 		}
   2474 	}
   2475 }
   2476 
   2477 
   2478 void
   2479 rf_UnconfigureVnodes(RF_Raid_t *raidPtr)
   2480 {
   2481 	int r,c;
   2482 	struct vnode *vp;
   2483 	int acd;
   2484 
   2485 
   2486 	/* We take this opportunity to close the vnodes like we should.. */
   2487 
   2488 	for (c = 0; c < raidPtr->numCol; c++) {
   2489 		vp = raidPtr->raid_cinfo[c].ci_vp;
   2490 		acd = raidPtr->Disks[c].auto_configured;
   2491 		rf_close_component(raidPtr, vp, acd);
   2492 		raidPtr->raid_cinfo[c].ci_vp = NULL;
   2493 		raidPtr->Disks[c].auto_configured = 0;
   2494 	}
   2495 
   2496 	for (r = 0; r < raidPtr->numSpare; r++) {
   2497 		vp = raidPtr->raid_cinfo[raidPtr->numCol + r].ci_vp;
   2498 		acd = raidPtr->Disks[raidPtr->numCol + r].auto_configured;
   2499 		rf_close_component(raidPtr, vp, acd);
   2500 		raidPtr->raid_cinfo[raidPtr->numCol + r].ci_vp = NULL;
   2501 		raidPtr->Disks[raidPtr->numCol + r].auto_configured = 0;
   2502 	}
   2503 }
   2504 
   2505 
   2506 void
   2507 rf_ReconThread(struct rf_recon_req *req)
   2508 {
   2509 	int     s;
   2510 	RF_Raid_t *raidPtr;
   2511 
   2512 	s = splbio();
   2513 	raidPtr = (RF_Raid_t *) req->raidPtr;
   2514 	raidPtr->recon_in_progress = 1;
   2515 
   2516 	rf_FailDisk((RF_Raid_t *) req->raidPtr, req->col,
   2517 		    ((req->flags & RF_FDFLAGS_RECON) ? 1 : 0));
   2518 
   2519 	RF_Free(req, sizeof(*req));
   2520 
   2521 	raidPtr->recon_in_progress = 0;
   2522 	splx(s);
   2523 
   2524 	/* That's all... */
   2525 	kthread_exit(0);        /* does not return */
   2526 }
   2527 
   2528 void
   2529 rf_RewriteParityThread(RF_Raid_t *raidPtr)
   2530 {
   2531 	int retcode;
   2532 	int s;
   2533 
   2534 	raidPtr->parity_rewrite_in_progress = 1;
   2535 	s = splbio();
   2536 	retcode = rf_RewriteParity(raidPtr);
   2537 	splx(s);
   2538 	if (retcode) {
   2539 		printf("raid%d: Error re-writing parity!\n",raidPtr->raidid);
   2540 	} else {
   2541 		/* set the clean bit!  If we shutdown correctly,
   2542 		   the clean bit on each component label will get
   2543 		   set */
   2544 		raidPtr->parity_good = RF_RAID_CLEAN;
   2545 	}
   2546 	raidPtr->parity_rewrite_in_progress = 0;
   2547 
   2548 	/* Anyone waiting for us to stop?  If so, inform them... */
   2549 	if (raidPtr->waitShutdown) {
   2550 		wakeup(&raidPtr->parity_rewrite_in_progress);
   2551 	}
   2552 
   2553 	/* That's all... */
   2554 	kthread_exit(0);        /* does not return */
   2555 }
   2556 
   2557 
   2558 void
   2559 rf_CopybackThread(RF_Raid_t *raidPtr)
   2560 {
   2561 	int s;
   2562 
   2563 	raidPtr->copyback_in_progress = 1;
   2564 	s = splbio();
   2565 	rf_CopybackReconstructedData(raidPtr);
   2566 	splx(s);
   2567 	raidPtr->copyback_in_progress = 0;
   2568 
   2569 	/* That's all... */
   2570 	kthread_exit(0);        /* does not return */
   2571 }
   2572 
   2573 
   2574 void
   2575 rf_ReconstructInPlaceThread(struct rf_recon_req *req)
   2576 {
   2577 	int s;
   2578 	RF_Raid_t *raidPtr;
   2579 
   2580 	s = splbio();
   2581 	raidPtr = req->raidPtr;
   2582 	raidPtr->recon_in_progress = 1;
   2583 	rf_ReconstructInPlace(raidPtr, req->col);
   2584 	RF_Free(req, sizeof(*req));
   2585 	raidPtr->recon_in_progress = 0;
   2586 	splx(s);
   2587 
   2588 	/* That's all... */
   2589 	kthread_exit(0);        /* does not return */
   2590 }
   2591 
   2592 RF_AutoConfig_t *
   2593 rf_find_raid_components()
   2594 {
   2595 	struct vnode *vp;
   2596 	struct disklabel label;
   2597 	struct device *dv;
   2598 	dev_t dev;
   2599 	int bmajor;
   2600 	int error;
   2601 	int i;
   2602 	int good_one;
   2603 	RF_ComponentLabel_t *clabel;
   2604 	RF_AutoConfig_t *ac_list;
   2605 	RF_AutoConfig_t *ac;
   2606 
   2607 
   2608 	/* initialize the AutoConfig list */
   2609 	ac_list = NULL;
   2610 
   2611 	/* we begin by trolling through *all* the devices on the system */
   2612 
   2613 	for (dv = alldevs.tqh_first; dv != NULL;
   2614 	     dv = dv->dv_list.tqe_next) {
   2615 
   2616 		/* we are only interested in disks... */
   2617 		if (dv->dv_class != DV_DISK)
   2618 			continue;
   2619 
   2620 		/* we don't care about floppies... */
   2621 		if (!strcmp(dv->dv_cfdata->cf_name,"fd")) {
   2622 			continue;
   2623 		}
   2624 
   2625 		/* we don't care about CD's... */
   2626 		if (!strcmp(dv->dv_cfdata->cf_name,"cd")) {
   2627 			continue;
   2628 		}
   2629 
   2630 		/* hdfd is the Atari/Hades floppy driver */
   2631 		if (!strcmp(dv->dv_cfdata->cf_name,"hdfd")) {
   2632 			continue;
   2633 		}
   2634 		/* fdisa is the Atari/Milan floppy driver */
   2635 		if (!strcmp(dv->dv_cfdata->cf_name,"fdisa")) {
   2636 			continue;
   2637 		}
   2638 
   2639 		/* need to find the device_name_to_block_device_major stuff */
   2640 		bmajor = devsw_name2blk(dv->dv_xname, NULL, 0);
   2641 
   2642 		/* get a vnode for the raw partition of this disk */
   2643 
   2644 		dev = MAKEDISKDEV(bmajor, dv->dv_unit, RAW_PART);
   2645 		if (bdevvp(dev, &vp))
   2646 			panic("RAID can't alloc vnode");
   2647 
   2648 		error = VOP_OPEN(vp, FREAD, NOCRED, 0);
   2649 
   2650 		if (error) {
   2651 			/* "Who cares."  Continue looking
   2652 			   for something that exists*/
   2653 			vput(vp);
   2654 			continue;
   2655 		}
   2656 
   2657 		/* Ok, the disk exists.  Go get the disklabel. */
   2658 		error = VOP_IOCTL(vp, DIOCGDINFO, &label, FREAD, NOCRED, 0);
   2659 		if (error) {
   2660 			/*
   2661 			 * XXX can't happen - open() would
   2662 			 * have errored out (or faked up one)
   2663 			 */
   2664 			printf("can't get label for dev %s%c (%d)!?!?\n",
   2665 			       dv->dv_xname, 'a' + RAW_PART, error);
   2666 		}
   2667 
   2668 		/* don't need this any more.  We'll allocate it again
   2669 		   a little later if we really do... */
   2670 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2671 		VOP_CLOSE(vp, FREAD | FWRITE, NOCRED, 0);
   2672 		vput(vp);
   2673 
   2674 		for (i=0; i < label.d_npartitions; i++) {
   2675 			/* We only support partitions marked as RAID */
   2676 			if (label.d_partitions[i].p_fstype != FS_RAID)
   2677 				continue;
   2678 
   2679 			dev = MAKEDISKDEV(bmajor, dv->dv_unit, i);
   2680 			if (bdevvp(dev, &vp))
   2681 				panic("RAID can't alloc vnode");
   2682 
   2683 			error = VOP_OPEN(vp, FREAD, NOCRED, 0);
   2684 			if (error) {
   2685 				/* Whatever... */
   2686 				vput(vp);
   2687 				continue;
   2688 			}
   2689 
   2690 			good_one = 0;
   2691 
   2692 			clabel = (RF_ComponentLabel_t *)
   2693 				malloc(sizeof(RF_ComponentLabel_t),
   2694 				       M_RAIDFRAME, M_NOWAIT);
   2695 			if (clabel == NULL) {
   2696 				/* XXX CLEANUP HERE */
   2697 				printf("RAID auto config: out of memory!\n");
   2698 				return(NULL); /* XXX probably should panic? */
   2699 			}
   2700 
   2701 			if (!raidread_component_label(dev, vp, clabel)) {
   2702 				/* Got the label.  Does it look reasonable? */
   2703 				if (rf_reasonable_label(clabel) &&
   2704 				    (clabel->partitionSize <=
   2705 				     label.d_partitions[i].p_size)) {
   2706 #if DEBUG
   2707 					printf("Component on: %s%c: %d\n",
   2708 					       dv->dv_xname, 'a'+i,
   2709 					       label.d_partitions[i].p_size);
   2710 					rf_print_component_label(clabel);
   2711 #endif
   2712 					/* if it's reasonable, add it,
   2713 					   else ignore it. */
   2714 					ac = (RF_AutoConfig_t *)
   2715 						malloc(sizeof(RF_AutoConfig_t),
   2716 						       M_RAIDFRAME,
   2717 						       M_NOWAIT);
   2718 					if (ac == NULL) {
   2719 						/* XXX should panic?? */
   2720 						return(NULL);
   2721 					}
   2722 
   2723 					sprintf(ac->devname, "%s%c",
   2724 						dv->dv_xname, 'a'+i);
   2725 					ac->dev = dev;
   2726 					ac->vp = vp;
   2727 					ac->clabel = clabel;
   2728 					ac->next = ac_list;
   2729 					ac_list = ac;
   2730 					good_one = 1;
   2731 				}
   2732 			}
   2733 			if (!good_one) {
   2734 				/* cleanup */
   2735 				free(clabel, M_RAIDFRAME);
   2736 				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
   2737 				VOP_CLOSE(vp, FREAD | FWRITE, NOCRED, 0);
   2738 				vput(vp);
   2739 			}
   2740 		}
   2741 	}
   2742 	return(ac_list);
   2743 }
   2744 
   2745 static int
   2746 rf_reasonable_label(RF_ComponentLabel_t *clabel)
   2747 {
   2748 
   2749 	if (((clabel->version==RF_COMPONENT_LABEL_VERSION_1) ||
   2750 	     (clabel->version==RF_COMPONENT_LABEL_VERSION)) &&
   2751 	    ((clabel->clean == RF_RAID_CLEAN) ||
   2752 	     (clabel->clean == RF_RAID_DIRTY)) &&
   2753 	    clabel->row >=0 &&
   2754 	    clabel->column >= 0 &&
   2755 	    clabel->num_rows > 0 &&
   2756 	    clabel->num_columns > 0 &&
   2757 	    clabel->row < clabel->num_rows &&
   2758 	    clabel->column < clabel->num_columns &&
   2759 	    clabel->blockSize > 0 &&
   2760 	    clabel->numBlocks > 0) {
   2761 		/* label looks reasonable enough... */
   2762 		return(1);
   2763 	}
   2764 	return(0);
   2765 }
   2766 
   2767 
   2768 #if DEBUG
   2769 void
   2770 rf_print_component_label(RF_ComponentLabel_t *clabel)
   2771 {
   2772 	printf("   Row: %d Column: %d Num Rows: %d Num Columns: %d\n",
   2773 	       clabel->row, clabel->column,
   2774 	       clabel->num_rows, clabel->num_columns);
   2775 	printf("   Version: %d Serial Number: %d Mod Counter: %d\n",
   2776 	       clabel->version, clabel->serial_number,
   2777 	       clabel->mod_counter);
   2778 	printf("   Clean: %s Status: %d\n",
   2779 	       clabel->clean ? "Yes" : "No", clabel->status );
   2780 	printf("   sectPerSU: %d SUsPerPU: %d SUsPerRU: %d\n",
   2781 	       clabel->sectPerSU, clabel->SUsPerPU, clabel->SUsPerRU);
   2782 	printf("   RAID Level: %c  blocksize: %d numBlocks: %d\n",
   2783 	       (char) clabel->parityConfig, clabel->blockSize,
   2784 	       clabel->numBlocks);
   2785 	printf("   Autoconfig: %s\n", clabel->autoconfigure ? "Yes" : "No" );
   2786 	printf("   Contains root partition: %s\n",
   2787 	       clabel->root_partition ? "Yes" : "No" );
   2788 	printf("   Last configured as: raid%d\n", clabel->last_unit );
   2789 #if 0
   2790 	   printf("   Config order: %d\n", clabel->config_order);
   2791 #endif
   2792 
   2793 }
   2794 #endif
   2795 
   2796 RF_ConfigSet_t *
   2797 rf_create_auto_sets(RF_AutoConfig_t *ac_list)
   2798 {
   2799 	RF_AutoConfig_t *ac;
   2800 	RF_ConfigSet_t *config_sets;
   2801 	RF_ConfigSet_t *cset;
   2802 	RF_AutoConfig_t *ac_next;
   2803 
   2804 
   2805 	config_sets = NULL;
   2806 
   2807 	/* Go through the AutoConfig list, and figure out which components
   2808 	   belong to what sets.  */
   2809 	ac = ac_list;
   2810 	while(ac!=NULL) {
   2811 		/* we're going to putz with ac->next, so save it here
   2812 		   for use at the end of the loop */
   2813 		ac_next = ac->next;
   2814 
   2815 		if (config_sets == NULL) {
   2816 			/* will need at least this one... */
   2817 			config_sets = (RF_ConfigSet_t *)
   2818 				malloc(sizeof(RF_ConfigSet_t),
   2819 				       M_RAIDFRAME, M_NOWAIT);
   2820 			if (config_sets == NULL) {
   2821 				panic("rf_create_auto_sets: No memory!");
   2822 			}
   2823 			/* this one is easy :) */
   2824 			config_sets->ac = ac;
   2825 			config_sets->next = NULL;
   2826 			config_sets->rootable = 0;
   2827 			ac->next = NULL;
   2828 		} else {
   2829 			/* which set does this component fit into? */
   2830 			cset = config_sets;
   2831 			while(cset!=NULL) {
   2832 				if (rf_does_it_fit(cset, ac)) {
   2833 					/* looks like it matches... */
   2834 					ac->next = cset->ac;
   2835 					cset->ac = ac;
   2836 					break;
   2837 				}
   2838 				cset = cset->next;
   2839 			}
   2840 			if (cset==NULL) {
   2841 				/* didn't find a match above... new set..*/
   2842 				cset = (RF_ConfigSet_t *)
   2843 					malloc(sizeof(RF_ConfigSet_t),
   2844 					       M_RAIDFRAME, M_NOWAIT);
   2845 				if (cset == NULL) {
   2846 					panic("rf_create_auto_sets: No memory!");
   2847 				}
   2848 				cset->ac = ac;
   2849 				ac->next = NULL;
   2850 				cset->next = config_sets;
   2851 				cset->rootable = 0;
   2852 				config_sets = cset;
   2853 			}
   2854 		}
   2855 		ac = ac_next;
   2856 	}
   2857 
   2858 
   2859 	return(config_sets);
   2860 }
   2861 
   2862 static int
   2863 rf_does_it_fit(RF_ConfigSet_t *cset, RF_AutoConfig_t *ac)
   2864 {
   2865 	RF_ComponentLabel_t *clabel1, *clabel2;
   2866 
   2867 	/* If this one matches the *first* one in the set, that's good
   2868 	   enough, since the other members of the set would have been
   2869 	   through here too... */
   2870 	/* note that we are not checking partitionSize here..
   2871 
   2872 	   Note that we are also not checking the mod_counters here.
   2873 	   If everything else matches execpt the mod_counter, that's
   2874 	   good enough for this test.  We will deal with the mod_counters
   2875 	   a little later in the autoconfiguration process.
   2876 
   2877 	    (clabel1->mod_counter == clabel2->mod_counter) &&
   2878 
   2879 	   The reason we don't check for this is that failed disks
   2880 	   will have lower modification counts.  If those disks are
   2881 	   not added to the set they used to belong to, then they will
   2882 	   form their own set, which may result in 2 different sets,
   2883 	   for example, competing to be configured at raid0, and
   2884 	   perhaps competing to be the root filesystem set.  If the
   2885 	   wrong ones get configured, or both attempt to become /,
   2886 	   weird behaviour and or serious lossage will occur.  Thus we
   2887 	   need to bring them into the fold here, and kick them out at
   2888 	   a later point.
   2889 
   2890 	*/
   2891 
   2892 	clabel1 = cset->ac->clabel;
   2893 	clabel2 = ac->clabel;
   2894 	if ((clabel1->version == clabel2->version) &&
   2895 	    (clabel1->serial_number == clabel2->serial_number) &&
   2896 	    (clabel1->num_rows == clabel2->num_rows) &&
   2897 	    (clabel1->num_columns == clabel2->num_columns) &&
   2898 	    (clabel1->sectPerSU == clabel2->sectPerSU) &&
   2899 	    (clabel1->SUsPerPU == clabel2->SUsPerPU) &&
   2900 	    (clabel1->SUsPerRU == clabel2->SUsPerRU) &&
   2901 	    (clabel1->parityConfig == clabel2->parityConfig) &&
   2902 	    (clabel1->maxOutstanding == clabel2->maxOutstanding) &&
   2903 	    (clabel1->blockSize == clabel2->blockSize) &&
   2904 	    (clabel1->numBlocks == clabel2->numBlocks) &&
   2905 	    (clabel1->autoconfigure == clabel2->autoconfigure) &&
   2906 	    (clabel1->root_partition == clabel2->root_partition) &&
   2907 	    (clabel1->last_unit == clabel2->last_unit) &&
   2908 	    (clabel1->config_order == clabel2->config_order)) {
   2909 		/* if it get's here, it almost *has* to be a match */
   2910 	} else {
   2911 		/* it's not consistent with somebody in the set..
   2912 		   punt */
   2913 		return(0);
   2914 	}
   2915 	/* all was fine.. it must fit... */
   2916 	return(1);
   2917 }
   2918 
   2919 int
   2920 rf_have_enough_components(RF_ConfigSet_t *cset)
   2921 {
   2922 	RF_AutoConfig_t *ac;
   2923 	RF_AutoConfig_t *auto_config;
   2924 	RF_ComponentLabel_t *clabel;
   2925 	int c;
   2926 	int num_cols;
   2927 	int num_missing;
   2928 	int mod_counter;
   2929 	int mod_counter_found;
   2930 	int even_pair_failed;
   2931 	char parity_type;
   2932 
   2933 
   2934 	/* check to see that we have enough 'live' components
   2935 	   of this set.  If so, we can configure it if necessary */
   2936 
   2937 	num_cols = cset->ac->clabel->num_columns;
   2938 	parity_type = cset->ac->clabel->parityConfig;
   2939 
   2940 	/* XXX Check for duplicate components!?!?!? */
   2941 
   2942 	/* Determine what the mod_counter is supposed to be for this set. */
   2943 
   2944 	mod_counter_found = 0;
   2945 	mod_counter = 0;
   2946 	ac = cset->ac;
   2947 	while(ac!=NULL) {
   2948 		if (mod_counter_found==0) {
   2949 			mod_counter = ac->clabel->mod_counter;
   2950 			mod_counter_found = 1;
   2951 		} else {
   2952 			if (ac->clabel->mod_counter > mod_counter) {
   2953 				mod_counter = ac->clabel->mod_counter;
   2954 			}
   2955 		}
   2956 		ac = ac->next;
   2957 	}
   2958 
   2959 	num_missing = 0;
   2960 	auto_config = cset->ac;
   2961 
   2962 	even_pair_failed = 0;
   2963 	for(c=0; c<num_cols; c++) {
   2964 		ac = auto_config;
   2965 		while(ac!=NULL) {
   2966 			if ((ac->clabel->column == c) &&
   2967 			    (ac->clabel->mod_counter == mod_counter)) {
   2968 				/* it's this one... */
   2969 #if DEBUG
   2970 				printf("Found: %s at %d\n",
   2971 				       ac->devname,c);
   2972 #endif
   2973 				break;
   2974 			}
   2975 			ac=ac->next;
   2976 		}
   2977 		if (ac==NULL) {
   2978 				/* Didn't find one here! */
   2979 				/* special case for RAID 1, especially
   2980 				   where there are more than 2
   2981 				   components (where RAIDframe treats
   2982 				   things a little differently :( ) */
   2983 			if (parity_type == '1') {
   2984 				if (c%2 == 0) { /* even component */
   2985 					even_pair_failed = 1;
   2986 				} else { /* odd component.  If
   2987 					    we're failed, and
   2988 					    so is the even
   2989 					    component, it's
   2990 					    "Good Night, Charlie" */
   2991 					if (even_pair_failed == 1) {
   2992 						return(0);
   2993 					}
   2994 				}
   2995 			} else {
   2996 				/* normal accounting */
   2997 				num_missing++;
   2998 			}
   2999 		}
   3000 		if ((parity_type == '1') && (c%2 == 1)) {
   3001 				/* Just did an even component, and we didn't
   3002 				   bail.. reset the even_pair_failed flag,
   3003 				   and go on to the next component.... */
   3004 			even_pair_failed = 0;
   3005 		}
   3006 	}
   3007 
   3008 	clabel = cset->ac->clabel;
   3009 
   3010 	if (((clabel->parityConfig == '0') && (num_missing > 0)) ||
   3011 	    ((clabel->parityConfig == '4') && (num_missing > 1)) ||
   3012 	    ((clabel->parityConfig == '5') && (num_missing > 1))) {
   3013 		/* XXX this needs to be made *much* more general */
   3014 		/* Too many failures */
   3015 		return(0);
   3016 	}
   3017 	/* otherwise, all is well, and we've got enough to take a kick
   3018 	   at autoconfiguring this set */
   3019 	return(1);
   3020 }
   3021 
   3022 void
   3023 rf_create_configuration(RF_AutoConfig_t *ac, RF_Config_t *config,
   3024 			RF_Raid_t *raidPtr)
   3025 {
   3026 	RF_ComponentLabel_t *clabel;
   3027 	int i;
   3028 
   3029 	clabel = ac->clabel;
   3030 
   3031 	/* 1. Fill in the common stuff */
   3032 	config->numRow = clabel->num_rows = 1;
   3033 	config->numCol = clabel->num_columns;
   3034 	config->numSpare = 0; /* XXX should this be set here? */
   3035 	config->sectPerSU = clabel->sectPerSU;
   3036 	config->SUsPerPU = clabel->SUsPerPU;
   3037 	config->SUsPerRU = clabel->SUsPerRU;
   3038 	config->parityConfig = clabel->parityConfig;
   3039 	/* XXX... */
   3040 	strcpy(config->diskQueueType,"fifo");
   3041 	config->maxOutstandingDiskReqs = clabel->maxOutstanding;
   3042 	config->layoutSpecificSize = 0; /* XXX ?? */
   3043 
   3044 	while(ac!=NULL) {
   3045 		/* row/col values will be in range due to the checks
   3046 		   in reasonable_label() */
   3047 		strcpy(config->devnames[0][ac->clabel->column],
   3048 		       ac->devname);
   3049 		ac = ac->next;
   3050 	}
   3051 
   3052 	for(i=0;i<RF_MAXDBGV;i++) {
   3053 		config->debugVars[i][0] = 0;
   3054 	}
   3055 }
   3056 
   3057 int
   3058 rf_set_autoconfig(RF_Raid_t *raidPtr, int new_value)
   3059 {
   3060 	RF_ComponentLabel_t clabel;
   3061 	struct vnode *vp;
   3062 	dev_t dev;
   3063 	int column;
   3064 	int sparecol;
   3065 
   3066 	raidPtr->autoconfigure = new_value;
   3067 
   3068 	for(column=0; column<raidPtr->numCol; column++) {
   3069 		if (raidPtr->Disks[column].status == rf_ds_optimal) {
   3070 			dev = raidPtr->Disks[column].dev;
   3071 			vp = raidPtr->raid_cinfo[column].ci_vp;
   3072 			raidread_component_label(dev, vp, &clabel);
   3073 			clabel.autoconfigure = new_value;
   3074 			raidwrite_component_label(dev, vp, &clabel);
   3075 		}
   3076 	}
   3077 	for(column = 0; column < raidPtr->numSpare ; column++) {
   3078 		sparecol = raidPtr->numCol + column;
   3079 		if (raidPtr->Disks[sparecol].status == rf_ds_used_spare) {
   3080 			dev = raidPtr->Disks[sparecol].dev;
   3081 			vp = raidPtr->raid_cinfo[sparecol].ci_vp;
   3082 			raidread_component_label(dev, vp, &clabel);
   3083 			clabel.autoconfigure = new_value;
   3084 			raidwrite_component_label(dev, vp, &clabel);
   3085 		}
   3086 	}
   3087 	return(new_value);
   3088 }
   3089 
   3090 int
   3091 rf_set_rootpartition(RF_Raid_t *raidPtr, int new_value)
   3092 {
   3093 	RF_ComponentLabel_t clabel;
   3094 	struct vnode *vp;
   3095 	dev_t dev;
   3096 	int column;
   3097 	int sparecol;
   3098 
   3099 	raidPtr->root_partition = new_value;
   3100 	for(column=0; column<raidPtr->numCol; column++) {
   3101 		if (raidPtr->Disks[column].status == rf_ds_optimal) {
   3102 			dev = raidPtr->Disks[column].dev;
   3103 			vp = raidPtr->raid_cinfo[column].ci_vp;
   3104 			raidread_component_label(dev, vp, &clabel);
   3105 			clabel.root_partition = new_value;
   3106 			raidwrite_component_label(dev, vp, &clabel);
   3107 		}
   3108 	}
   3109 	for(column = 0; column < raidPtr->numSpare ; column++) {
   3110 		sparecol = raidPtr->numCol + column;
   3111 		if (raidPtr->Disks[sparecol].status == rf_ds_used_spare) {
   3112 			dev = raidPtr->Disks[sparecol].dev;
   3113 			vp = raidPtr->raid_cinfo[sparecol].ci_vp;
   3114 			raidread_component_label(dev, vp, &clabel);
   3115 			clabel.root_partition = new_value;
   3116 			raidwrite_component_label(dev, vp, &clabel);
   3117 		}
   3118 	}
   3119 	return(new_value);
   3120 }
   3121 
   3122 void
   3123 rf_release_all_vps(RF_ConfigSet_t *cset)
   3124 {
   3125 	RF_AutoConfig_t *ac;
   3126 
   3127 	ac = cset->ac;
   3128 	while(ac!=NULL) {
   3129 		/* Close the vp, and give it back */
   3130 		if (ac->vp) {
   3131 			vn_lock(ac->vp, LK_EXCLUSIVE | LK_RETRY);
   3132 			VOP_CLOSE(ac->vp, FREAD, NOCRED, 0);
   3133 			vput(ac->vp);
   3134 			ac->vp = NULL;
   3135 		}
   3136 		ac = ac->next;
   3137 	}
   3138 }
   3139 
   3140 
   3141 void
   3142 rf_cleanup_config_set(RF_ConfigSet_t *cset)
   3143 {
   3144 	RF_AutoConfig_t *ac;
   3145 	RF_AutoConfig_t *next_ac;
   3146 
   3147 	ac = cset->ac;
   3148 	while(ac!=NULL) {
   3149 		next_ac = ac->next;
   3150 		/* nuke the label */
   3151 		free(ac->clabel, M_RAIDFRAME);
   3152 		/* cleanup the config structure */
   3153 		free(ac, M_RAIDFRAME);
   3154 		/* "next.." */
   3155 		ac = next_ac;
   3156 	}
   3157 	/* and, finally, nuke the config set */
   3158 	free(cset, M_RAIDFRAME);
   3159 }
   3160 
   3161 
   3162 void
   3163 raid_init_component_label(RF_Raid_t *raidPtr, RF_ComponentLabel_t *clabel)
   3164 {
   3165 	/* current version number */
   3166 	clabel->version = RF_COMPONENT_LABEL_VERSION;
   3167 	clabel->serial_number = raidPtr->serial_number;
   3168 	clabel->mod_counter = raidPtr->mod_counter;
   3169 	clabel->num_rows = 1;
   3170 	clabel->num_columns = raidPtr->numCol;
   3171 	clabel->clean = RF_RAID_DIRTY; /* not clean */
   3172 	clabel->status = rf_ds_optimal; /* "It's good!" */
   3173 
   3174 	clabel->sectPerSU = raidPtr->Layout.sectorsPerStripeUnit;
   3175 	clabel->SUsPerPU = raidPtr->Layout.SUsPerPU;
   3176 	clabel->SUsPerRU = raidPtr->Layout.SUsPerRU;
   3177 
   3178 	clabel->blockSize = raidPtr->bytesPerSector;
   3179 	clabel->numBlocks = raidPtr->sectorsPerDisk;
   3180 
   3181 	/* XXX not portable */
   3182 	clabel->parityConfig = raidPtr->Layout.map->parityConfig;
   3183 	clabel->maxOutstanding = raidPtr->maxOutstanding;
   3184 	clabel->autoconfigure = raidPtr->autoconfigure;
   3185 	clabel->root_partition = raidPtr->root_partition;
   3186 	clabel->last_unit = raidPtr->raidid;
   3187 	clabel->config_order = raidPtr->config_order;
   3188 }
   3189 
   3190 int
   3191 rf_auto_config_set(RF_ConfigSet_t *cset, int *unit)
   3192 {
   3193 	RF_Raid_t *raidPtr;
   3194 	RF_Config_t *config;
   3195 	int raidID;
   3196 	int retcode;
   3197 
   3198 #if DEBUG
   3199 	printf("RAID autoconfigure\n");
   3200 #endif
   3201 
   3202 	retcode = 0;
   3203 	*unit = -1;
   3204 
   3205 	/* 1. Create a config structure */
   3206 
   3207 	config = (RF_Config_t *)malloc(sizeof(RF_Config_t),
   3208 				       M_RAIDFRAME,
   3209 				       M_NOWAIT);
   3210 	if (config==NULL) {
   3211 		printf("Out of mem!?!?\n");
   3212 				/* XXX do something more intelligent here. */
   3213 		return(1);
   3214 	}
   3215 
   3216 	memset(config, 0, sizeof(RF_Config_t));
   3217 
   3218 	/*
   3219 	   2. Figure out what RAID ID this one is supposed to live at
   3220 	   See if we can get the same RAID dev that it was configured
   3221 	   on last time..
   3222 	*/
   3223 
   3224 	raidID = cset->ac->clabel->last_unit;
   3225 	if ((raidID < 0) || (raidID >= numraid)) {
   3226 		/* let's not wander off into lala land. */
   3227 		raidID = numraid - 1;
   3228 	}
   3229 	if (raidPtrs[raidID]->valid != 0) {
   3230 
   3231 		/*
   3232 		   Nope... Go looking for an alternative...
   3233 		   Start high so we don't immediately use raid0 if that's
   3234 		   not taken.
   3235 		*/
   3236 
   3237 		for(raidID = numraid - 1; raidID >= 0; raidID--) {
   3238 			if (raidPtrs[raidID]->valid == 0) {
   3239 				/* can use this one! */
   3240 				break;
   3241 			}
   3242 		}
   3243 	}
   3244 
   3245 	if (raidID < 0) {
   3246 		/* punt... */
   3247 		printf("Unable to auto configure this set!\n");
   3248 		printf("(Out of RAID devs!)\n");
   3249 		return(1);
   3250 	}
   3251 
   3252 #if DEBUG
   3253 	printf("Configuring raid%d:\n",raidID);
   3254 #endif
   3255 
   3256 	raidPtr = raidPtrs[raidID];
   3257 
   3258 	/* XXX all this stuff should be done SOMEWHERE ELSE! */
   3259 	raidPtr->raidid = raidID;
   3260 	raidPtr->openings = RAIDOUTSTANDING;
   3261 
   3262 	/* 3. Build the configuration structure */
   3263 	rf_create_configuration(cset->ac, config, raidPtr);
   3264 
   3265 	/* 4. Do the configuration */
   3266 	retcode = rf_Configure(raidPtr, config, cset->ac);
   3267 
   3268 	if (retcode == 0) {
   3269 
   3270 		raidinit(raidPtrs[raidID]);
   3271 
   3272 		rf_markalldirty(raidPtrs[raidID]);
   3273 		raidPtrs[raidID]->autoconfigure = 1; /* XXX do this here? */
   3274 		if (cset->ac->clabel->root_partition==1) {
   3275 			/* everything configured just fine.  Make a note
   3276 			   that this set is eligible to be root. */
   3277 			cset->rootable = 1;
   3278 			/* XXX do this here? */
   3279 			raidPtrs[raidID]->root_partition = 1;
   3280 		}
   3281 	}
   3282 
   3283 	/* 5. Cleanup */
   3284 	free(config, M_RAIDFRAME);
   3285 
   3286 	*unit = raidID;
   3287 	return(retcode);
   3288 }
   3289 
   3290 void
   3291 rf_disk_unbusy(RF_RaidAccessDesc_t *desc)
   3292 {
   3293 	struct buf *bp;
   3294 
   3295 	bp = (struct buf *)desc->bp;
   3296 	disk_unbusy(&raid_softc[desc->raidPtr->raidid].sc_dkdev,
   3297 	    (bp->b_bcount - bp->b_resid), (bp->b_flags & B_READ));
   3298 }
   3299