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