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