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