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