Home | History | Annotate | Line # | Download | only in raidframe
rf_reconstruct.c revision 1.105.4.6
      1 /*	$NetBSD: rf_reconstruct.c,v 1.105.4.6 2012/06/13 14:00:49 sborrill Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author: Mark Holland
      7  *
      8  * Permission to use, copy, modify and distribute this software and
      9  * its documentation is hereby granted, provided that both the copyright
     10  * notice and this permission notice appear in all copies of the
     11  * software, derivative works or modified versions, and any portions
     12  * thereof, and that both notices appear in supporting documentation.
     13  *
     14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  *
     18  * Carnegie Mellon requests users of this software to return to
     19  *
     20  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  *  School of Computer Science
     22  *  Carnegie Mellon University
     23  *  Pittsburgh PA 15213-3890
     24  *
     25  * any improvements or extensions that they make and grant Carnegie the
     26  * rights to redistribute these changes.
     27  */
     28 
     29 /************************************************************
     30  *
     31  * rf_reconstruct.c -- code to perform on-line reconstruction
     32  *
     33  ************************************************************/
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: rf_reconstruct.c,v 1.105.4.6 2012/06/13 14:00:49 sborrill Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/time.h>
     40 #include <sys/buf.h>
     41 #include <sys/errno.h>
     42 #include <sys/systm.h>
     43 #include <sys/proc.h>
     44 #include <sys/ioctl.h>
     45 #include <sys/fcntl.h>
     46 #include <sys/vnode.h>
     47 #include <dev/raidframe/raidframevar.h>
     48 
     49 #include "rf_raid.h"
     50 #include "rf_reconutil.h"
     51 #include "rf_revent.h"
     52 #include "rf_reconbuffer.h"
     53 #include "rf_acctrace.h"
     54 #include "rf_etimer.h"
     55 #include "rf_dag.h"
     56 #include "rf_desc.h"
     57 #include "rf_debugprint.h"
     58 #include "rf_general.h"
     59 #include "rf_driver.h"
     60 #include "rf_utils.h"
     61 #include "rf_shutdown.h"
     62 
     63 #include "rf_kintf.h"
     64 
     65 /* setting these to -1 causes them to be set to their default values if not set by debug options */
     66 
     67 #if RF_DEBUG_RECON
     68 #define Dprintf(s)         if (rf_reconDebug) rf_debug_printf(s,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)
     69 #define Dprintf1(s,a)         if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),NULL,NULL,NULL,NULL,NULL,NULL,NULL)
     70 #define Dprintf2(s,a,b)       if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),NULL,NULL,NULL,NULL,NULL,NULL)
     71 #define Dprintf3(s,a,b,c)     if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),NULL,NULL,NULL,NULL,NULL)
     72 #define Dprintf4(s,a,b,c,d)   if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),NULL,NULL,NULL,NULL)
     73 #define Dprintf5(s,a,b,c,d,e) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),NULL,NULL,NULL)
     74 #define Dprintf6(s,a,b,c,d,e,f) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),NULL,NULL)
     75 #define Dprintf7(s,a,b,c,d,e,f,g) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),(void *)((unsigned long)g),NULL)
     76 
     77 #define DDprintf1(s,a)         if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),NULL,NULL,NULL,NULL,NULL,NULL,NULL)
     78 #define DDprintf2(s,a,b)       if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),NULL,NULL,NULL,NULL,NULL,NULL)
     79 
     80 #else /* RF_DEBUG_RECON */
     81 
     82 #define Dprintf(s) {}
     83 #define Dprintf1(s,a) {}
     84 #define Dprintf2(s,a,b) {}
     85 #define Dprintf3(s,a,b,c) {}
     86 #define Dprintf4(s,a,b,c,d) {}
     87 #define Dprintf5(s,a,b,c,d,e) {}
     88 #define Dprintf6(s,a,b,c,d,e,f) {}
     89 #define Dprintf7(s,a,b,c,d,e,f,g) {}
     90 
     91 #define DDprintf1(s,a) {}
     92 #define DDprintf2(s,a,b) {}
     93 
     94 #endif /* RF_DEBUG_RECON */
     95 
     96 #define RF_RECON_DONE_READS   1
     97 #define RF_RECON_READ_ERROR   2
     98 #define RF_RECON_WRITE_ERROR  3
     99 #define RF_RECON_READ_STOPPED 4
    100 #define RF_RECON_WRITE_DONE   5
    101 
    102 #define RF_MAX_FREE_RECONBUFFER 32
    103 #define RF_MIN_FREE_RECONBUFFER 16
    104 
    105 static RF_RaidReconDesc_t *AllocRaidReconDesc(RF_Raid_t *, RF_RowCol_t,
    106 					      RF_RaidDisk_t *, int, RF_RowCol_t);
    107 static void FreeReconDesc(RF_RaidReconDesc_t *);
    108 static int ProcessReconEvent(RF_Raid_t *, RF_ReconEvent_t *);
    109 static int IssueNextReadRequest(RF_Raid_t *, RF_RowCol_t);
    110 static int TryToRead(RF_Raid_t *, RF_RowCol_t);
    111 static int ComputePSDiskOffsets(RF_Raid_t *, RF_StripeNum_t, RF_RowCol_t,
    112 				RF_SectorNum_t *, RF_SectorNum_t *, RF_RowCol_t *,
    113 				RF_SectorNum_t *);
    114 static int IssueNextWriteRequest(RF_Raid_t *);
    115 static int ReconReadDoneProc(void *, int);
    116 static int ReconWriteDoneProc(void *, int);
    117 static void CheckForNewMinHeadSep(RF_Raid_t *, RF_HeadSepLimit_t);
    118 static int CheckHeadSeparation(RF_Raid_t *, RF_PerDiskReconCtrl_t *,
    119 			       RF_RowCol_t, RF_HeadSepLimit_t,
    120 			       RF_ReconUnitNum_t);
    121 static int CheckForcedOrBlockedReconstruction(RF_Raid_t *,
    122 					      RF_ReconParityStripeStatus_t *,
    123 					      RF_PerDiskReconCtrl_t *,
    124 					      RF_RowCol_t, RF_StripeNum_t,
    125 					      RF_ReconUnitNum_t);
    126 static void ForceReconReadDoneProc(void *, int);
    127 static void rf_ShutdownReconstruction(void *);
    128 
    129 struct RF_ReconDoneProc_s {
    130 	void    (*proc) (RF_Raid_t *, void *);
    131 	void   *arg;
    132 	RF_ReconDoneProc_t *next;
    133 };
    134 
    135 /**************************************************************************
    136  *
    137  * sets up the parameters that will be used by the reconstruction process
    138  * currently there are none, except for those that the layout-specific
    139  * configuration (e.g. rf_ConfigureDeclustered) routine sets up.
    140  *
    141  * in the kernel, we fire off the recon thread.
    142  *
    143  **************************************************************************/
    144 static void
    145 rf_ShutdownReconstruction(void *ignored)
    146 {
    147 	pool_destroy(&rf_pools.reconbuffer);
    148 }
    149 
    150 int
    151 rf_ConfigureReconstruction(RF_ShutdownList_t **listp)
    152 {
    153 
    154 	rf_pool_init(&rf_pools.reconbuffer, sizeof(RF_ReconBuffer_t),
    155 		     "rf_reconbuffer_pl", RF_MIN_FREE_RECONBUFFER, RF_MAX_FREE_RECONBUFFER);
    156 	rf_ShutdownCreate(listp, rf_ShutdownReconstruction, NULL);
    157 
    158 	return (0);
    159 }
    160 
    161 static RF_RaidReconDesc_t *
    162 AllocRaidReconDesc(RF_Raid_t *raidPtr, RF_RowCol_t col,
    163 		   RF_RaidDisk_t *spareDiskPtr, int numDisksDone,
    164 		   RF_RowCol_t scol)
    165 {
    166 
    167 	RF_RaidReconDesc_t *reconDesc;
    168 
    169 	RF_Malloc(reconDesc, sizeof(RF_RaidReconDesc_t),
    170 		  (RF_RaidReconDesc_t *));
    171 	reconDesc->raidPtr = raidPtr;
    172 	reconDesc->col = col;
    173 	reconDesc->spareDiskPtr = spareDiskPtr;
    174 	reconDesc->numDisksDone = numDisksDone;
    175 	reconDesc->scol = scol;
    176 	reconDesc->next = NULL;
    177 
    178 	return (reconDesc);
    179 }
    180 
    181 static void
    182 FreeReconDesc(RF_RaidReconDesc_t *reconDesc)
    183 {
    184 #if RF_RECON_STATS > 0
    185 	printf("raid%d: %lu recon event waits, %lu recon delays\n",
    186 	       reconDesc->raidPtr->raidid,
    187 	       (long) reconDesc->numReconEventWaits,
    188 	       (long) reconDesc->numReconExecDelays);
    189 #endif				/* RF_RECON_STATS > 0 */
    190 	printf("raid%d: %lu max exec ticks\n",
    191 	       reconDesc->raidPtr->raidid,
    192 	       (long) reconDesc->maxReconExecTicks);
    193 	RF_Free(reconDesc, sizeof(RF_RaidReconDesc_t));
    194 }
    195 
    196 
    197 /*****************************************************************************
    198  *
    199  * primary routine to reconstruct a failed disk.  This should be called from
    200  * within its own thread.  It won't return until reconstruction completes,
    201  * fails, or is aborted.
    202  *****************************************************************************/
    203 int
    204 rf_ReconstructFailedDisk(RF_Raid_t *raidPtr, RF_RowCol_t col)
    205 {
    206 	const RF_LayoutSW_t *lp;
    207 	int     rc;
    208 
    209 	lp = raidPtr->Layout.map;
    210 	if (lp->SubmitReconBuffer) {
    211 		/*
    212 	         * The current infrastructure only supports reconstructing one
    213 	         * disk at a time for each array.
    214 	         */
    215 		RF_LOCK_MUTEX(raidPtr->mutex);
    216 		while (raidPtr->reconInProgress) {
    217 			RF_WAIT_COND(raidPtr->waitForReconCond, raidPtr->mutex);
    218 		}
    219 		raidPtr->reconInProgress++;
    220 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    221 		rc = rf_ReconstructFailedDiskBasic(raidPtr, col);
    222 		RF_LOCK_MUTEX(raidPtr->mutex);
    223 		raidPtr->reconInProgress--;
    224 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    225 	} else {
    226 		RF_ERRORMSG1("RECON: no way to reconstruct failed disk for arch %c\n",
    227 		    lp->parityConfig);
    228 		rc = EIO;
    229 	}
    230 	RF_SIGNAL_COND(raidPtr->waitForReconCond);
    231 	return (rc);
    232 }
    233 
    234 int
    235 rf_ReconstructFailedDiskBasic(RF_Raid_t *raidPtr, RF_RowCol_t col)
    236 {
    237 	RF_ComponentLabel_t *c_label;
    238 	RF_RaidDisk_t *spareDiskPtr = NULL;
    239 	RF_RaidReconDesc_t *reconDesc;
    240 	RF_RowCol_t scol;
    241 	int     numDisksDone = 0, rc;
    242 
    243 	/* first look for a spare drive onto which to reconstruct the data */
    244 	/* spare disk descriptors are stored in row 0.  This may have to
    245 	 * change eventually */
    246 
    247 	RF_LOCK_MUTEX(raidPtr->mutex);
    248 	RF_ASSERT(raidPtr->Disks[col].status == rf_ds_failed);
    249 #if RF_INCLUDE_PARITY_DECLUSTERING_DS > 0
    250 	if (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE) {
    251 		if (raidPtr->status != rf_rs_degraded) {
    252 			RF_ERRORMSG1("Unable to reconstruct disk at col %d because status not degraded\n", col);
    253 			RF_UNLOCK_MUTEX(raidPtr->mutex);
    254 			return (EINVAL);
    255 		}
    256 		scol = (-1);
    257 	} else {
    258 #endif
    259 		for (scol = raidPtr->numCol; scol < raidPtr->numCol + raidPtr->numSpare; scol++) {
    260 			if (raidPtr->Disks[scol].status == rf_ds_spare) {
    261 				spareDiskPtr = &raidPtr->Disks[scol];
    262 				spareDiskPtr->status = rf_ds_used_spare;
    263 				break;
    264 			}
    265 		}
    266 		if (!spareDiskPtr) {
    267 			RF_ERRORMSG1("Unable to reconstruct disk at col %d because no spares are available\n", col);
    268 			RF_UNLOCK_MUTEX(raidPtr->mutex);
    269 			return (ENOSPC);
    270 		}
    271 		printf("RECON: initiating reconstruction on col %d -> spare at col %d\n", col, scol);
    272 #if RF_INCLUDE_PARITY_DECLUSTERING_DS > 0
    273 	}
    274 #endif
    275 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    276 
    277 	reconDesc = AllocRaidReconDesc((void *) raidPtr, col, spareDiskPtr, numDisksDone, scol);
    278 	raidPtr->reconDesc = (void *) reconDesc;
    279 #if RF_RECON_STATS > 0
    280 	reconDesc->hsStallCount = 0;
    281 	reconDesc->numReconExecDelays = 0;
    282 	reconDesc->numReconEventWaits = 0;
    283 #endif				/* RF_RECON_STATS > 0 */
    284 	reconDesc->reconExecTimerRunning = 0;
    285 	reconDesc->reconExecTicks = 0;
    286 	reconDesc->maxReconExecTicks = 0;
    287 	rc = rf_ContinueReconstructFailedDisk(reconDesc);
    288 
    289 	if (!rc) {
    290 		/* fix up the component label */
    291 		/* Don't actually need the read here.. */
    292 		c_label = raidget_component_label(raidPtr, scol);
    293 
    294 		raid_init_component_label(raidPtr, c_label);
    295 		c_label->row = 0;
    296 		c_label->column = col;
    297 		c_label->clean = RF_RAID_DIRTY;
    298 		c_label->status = rf_ds_optimal;
    299 		rf_component_label_set_partitionsize(c_label,
    300 		    raidPtr->Disks[scol].partitionSize);
    301 
    302 		/* We've just done a rebuild based on all the other
    303 		   disks, so at this point the parity is known to be
    304 		   clean, even if it wasn't before. */
    305 
    306 		/* XXX doesn't hold for RAID 6!!*/
    307 
    308 		RF_LOCK_MUTEX(raidPtr->mutex);
    309 		raidPtr->parity_good = RF_RAID_CLEAN;
    310 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    311 
    312 		/* XXXX MORE NEEDED HERE */
    313 
    314 		raidflush_component_label(raidPtr, scol);
    315 	} else {
    316 		/* Reconstruct failed. */
    317 
    318 		RF_LOCK_MUTEX(raidPtr->mutex);
    319 		/* Failed disk goes back to "failed" status */
    320 		raidPtr->Disks[col].status = rf_ds_failed;
    321 
    322 		/* Spare disk goes back to "spare" status. */
    323 		spareDiskPtr->status = rf_ds_spare;
    324 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    325 
    326 	}
    327 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    328 	return (rc);
    329 }
    330 
    331 /*
    332 
    333    Allow reconstructing a disk in-place -- i.e. component /dev/sd2e goes AWOL,
    334    and you don't get a spare until the next Monday.  With this function
    335    (and hot-swappable drives) you can now put your new disk containing
    336    /dev/sd2e on the bus, scsictl it alive, and then use raidctl(8) to
    337    rebuild the data "on the spot".
    338 
    339 */
    340 
    341 int
    342 rf_ReconstructInPlace(RF_Raid_t *raidPtr, RF_RowCol_t col)
    343 {
    344 	RF_RaidDisk_t *spareDiskPtr = NULL;
    345 	RF_RaidReconDesc_t *reconDesc;
    346 	const RF_LayoutSW_t *lp;
    347 	RF_ComponentLabel_t *c_label;
    348 	int     numDisksDone = 0, rc;
    349 	struct partinfo dpart;
    350 	struct vnode *vp;
    351 	struct vattr va;
    352 	int retcode;
    353 	int ac;
    354 
    355 	lp = raidPtr->Layout.map;
    356 	if (!lp->SubmitReconBuffer) {
    357 		RF_ERRORMSG1("RECON: no way to reconstruct failed disk for arch %c\n",
    358 			     lp->parityConfig);
    359 		/* wakeup anyone who might be waiting to do a reconstruct */
    360 		RF_SIGNAL_COND(raidPtr->waitForReconCond);
    361 		return(EIO);
    362 	}
    363 
    364 	/*
    365 	 * The current infrastructure only supports reconstructing one
    366 	 * disk at a time for each array.
    367 	 */
    368 	RF_LOCK_MUTEX(raidPtr->mutex);
    369 
    370 	if (raidPtr->Disks[col].status != rf_ds_failed) {
    371 		/* "It's gone..." */
    372 		raidPtr->numFailures++;
    373 		raidPtr->Disks[col].status = rf_ds_failed;
    374 		raidPtr->status = rf_rs_degraded;
    375 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    376 		rf_update_component_labels(raidPtr,
    377 					   RF_NORMAL_COMPONENT_UPDATE);
    378 		RF_LOCK_MUTEX(raidPtr->mutex);
    379 	}
    380 
    381 	while (raidPtr->reconInProgress) {
    382 		RF_WAIT_COND(raidPtr->waitForReconCond, raidPtr->mutex);
    383 	}
    384 
    385 	raidPtr->reconInProgress++;
    386 
    387 	/* first look for a spare drive onto which to reconstruct the
    388 	   data.  spare disk descriptors are stored in row 0.  This
    389 	   may have to change eventually */
    390 
    391 	/* Actually, we don't care if it's failed or not...  On a RAID
    392 	   set with correct parity, this function should be callable
    393 	   on any component without ill effects. */
    394 	/* RF_ASSERT(raidPtr->Disks[col].status == rf_ds_failed); */
    395 
    396 #if RF_INCLUDE_PARITY_DECLUSTERING_DS > 0
    397 	if (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE) {
    398 		RF_ERRORMSG1("Unable to reconstruct to disk at col %d: operation not supported for RF_DISTRIBUTE_SPARE\n", col);
    399 
    400 		raidPtr->reconInProgress--;
    401 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    402 		RF_SIGNAL_COND(raidPtr->waitForReconCond);
    403 		return (EINVAL);
    404 	}
    405 #endif
    406 
    407 	/* This device may have been opened successfully the
    408 	   first time. Close it before trying to open it again.. */
    409 
    410 	if (raidPtr->raid_cinfo[col].ci_vp != NULL) {
    411 #if 0
    412 		printf("Closed the open device: %s\n",
    413 		       raidPtr->Disks[col].devname);
    414 #endif
    415 		vp = raidPtr->raid_cinfo[col].ci_vp;
    416 		ac = raidPtr->Disks[col].auto_configured;
    417 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    418 		rf_close_component(raidPtr, vp, ac);
    419 		RF_LOCK_MUTEX(raidPtr->mutex);
    420 		raidPtr->raid_cinfo[col].ci_vp = NULL;
    421 	}
    422 	/* note that this disk was *not* auto_configured (any longer)*/
    423 	raidPtr->Disks[col].auto_configured = 0;
    424 
    425 #if 0
    426 	printf("About to (re-)open the device for rebuilding: %s\n",
    427 	       raidPtr->Disks[col].devname);
    428 #endif
    429 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    430 	retcode = dk_lookup(raidPtr->Disks[col].devname, curlwp, &vp, UIO_SYSSPACE);
    431 
    432 	if (retcode) {
    433 		printf("raid%d: rebuilding: dk_lookup on device: %s failed: %d!\n",raidPtr->raidid,
    434 		       raidPtr->Disks[col].devname, retcode);
    435 
    436 		/* the component isn't responding properly...
    437 		   must be still dead :-( */
    438 		RF_LOCK_MUTEX(raidPtr->mutex);
    439 		raidPtr->reconInProgress--;
    440 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    441 		RF_SIGNAL_COND(raidPtr->waitForReconCond);
    442 		return(retcode);
    443 	}
    444 
    445 	/* Ok, so we can at least do a lookup...
    446 	   How about actually getting a vp for it? */
    447 
    448 	if ((retcode = VOP_GETATTR(vp, &va, curlwp->l_cred)) != 0) {
    449 		RF_LOCK_MUTEX(raidPtr->mutex);
    450 		raidPtr->reconInProgress--;
    451 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    452 		RF_SIGNAL_COND(raidPtr->waitForReconCond);
    453 		return(retcode);
    454 	}
    455 
    456 	retcode = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, curlwp->l_cred);
    457 	if (retcode) {
    458 		RF_LOCK_MUTEX(raidPtr->mutex);
    459 		raidPtr->reconInProgress--;
    460 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    461 		RF_SIGNAL_COND(raidPtr->waitForReconCond);
    462 		return(retcode);
    463 	}
    464 	RF_LOCK_MUTEX(raidPtr->mutex);
    465 	raidPtr->Disks[col].blockSize =	dpart.disklab->d_secsize;
    466 
    467 	raidPtr->Disks[col].numBlocks = dpart.part->p_size -
    468 		rf_protectedSectors;
    469 
    470 	raidPtr->raid_cinfo[col].ci_vp = vp;
    471 	raidPtr->raid_cinfo[col].ci_dev = va.va_rdev;
    472 
    473 	raidPtr->Disks[col].dev = va.va_rdev;
    474 
    475 	/* we allow the user to specify that only a fraction
    476 	   of the disks should be used this is just for debug:
    477 	   it speeds up * the parity scan */
    478 	raidPtr->Disks[col].numBlocks = raidPtr->Disks[col].numBlocks *
    479 		rf_sizePercentage / 100;
    480 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    481 
    482 	spareDiskPtr = &raidPtr->Disks[col];
    483 	spareDiskPtr->status = rf_ds_used_spare;
    484 
    485 	printf("raid%d: initiating in-place reconstruction on column %d\n",
    486 	       raidPtr->raidid, col);
    487 
    488 	reconDesc = AllocRaidReconDesc((void *) raidPtr, col, spareDiskPtr,
    489 				       numDisksDone, col);
    490 	raidPtr->reconDesc = (void *) reconDesc;
    491 #if RF_RECON_STATS > 0
    492 	reconDesc->hsStallCount = 0;
    493 	reconDesc->numReconExecDelays = 0;
    494 	reconDesc->numReconEventWaits = 0;
    495 #endif				/* RF_RECON_STATS > 0 */
    496 	reconDesc->reconExecTimerRunning = 0;
    497 	reconDesc->reconExecTicks = 0;
    498 	reconDesc->maxReconExecTicks = 0;
    499 	rc = rf_ContinueReconstructFailedDisk(reconDesc);
    500 
    501 	if (!rc) {
    502 		RF_LOCK_MUTEX(raidPtr->mutex);
    503 		/* Need to set these here, as at this point it'll be claiming
    504 		   that the disk is in rf_ds_spared!  But we know better :-) */
    505 
    506 		raidPtr->Disks[col].status = rf_ds_optimal;
    507 		raidPtr->status = rf_rs_optimal;
    508 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    509 
    510 		/* fix up the component label */
    511 		/* Don't actually need the read here.. */
    512 		c_label = raidget_component_label(raidPtr, col);
    513 
    514 		RF_LOCK_MUTEX(raidPtr->mutex);
    515 		raid_init_component_label(raidPtr, c_label);
    516 
    517 		c_label->row = 0;
    518 		c_label->column = col;
    519 
    520 		/* We've just done a rebuild based on all the other
    521 		   disks, so at this point the parity is known to be
    522 		   clean, even if it wasn't before. */
    523 
    524 		/* XXX doesn't hold for RAID 6!!*/
    525 
    526 		raidPtr->parity_good = RF_RAID_CLEAN;
    527 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    528 
    529 		raidflush_component_label(raidPtr, col);
    530 	} else {
    531 		/* Reconstruct-in-place failed.  Disk goes back to
    532 		   "failed" status, regardless of what it was before.  */
    533 		RF_LOCK_MUTEX(raidPtr->mutex);
    534 		raidPtr->Disks[col].status = rf_ds_failed;
    535 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    536 	}
    537 
    538 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    539 
    540 	RF_LOCK_MUTEX(raidPtr->mutex);
    541 	raidPtr->reconInProgress--;
    542 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    543 
    544 	RF_SIGNAL_COND(raidPtr->waitForReconCond);
    545 	return (rc);
    546 }
    547 
    548 
    549 int
    550 rf_ContinueReconstructFailedDisk(RF_RaidReconDesc_t *reconDesc)
    551 {
    552 	RF_Raid_t *raidPtr = reconDesc->raidPtr;
    553 	RF_RowCol_t col = reconDesc->col;
    554 	RF_RowCol_t scol = reconDesc->scol;
    555 	RF_ReconMap_t *mapPtr;
    556 	RF_ReconCtrl_t *tmp_reconctrl;
    557 	RF_ReconEvent_t *event;
    558 	RF_StripeCount_t incPSID,lastPSID,num_writes,pending_writes,prev;
    559 #if RF_INCLUDE_RAID5_RS > 0
    560 	RF_StripeCount_t startPSID,endPSID,aPSID,bPSID,offPSID;
    561 #endif
    562 	RF_ReconUnitCount_t RUsPerPU;
    563 	struct timeval etime, elpsd;
    564 	unsigned long xor_s, xor_resid_us;
    565 	int     i, ds;
    566 	int status, done;
    567 	int recon_error, write_error;
    568 
    569 	raidPtr->accumXorTimeUs = 0;
    570 #if RF_ACC_TRACE > 0
    571 	/* create one trace record per physical disk */
    572 	RF_Malloc(raidPtr->recon_tracerecs, raidPtr->numCol * sizeof(RF_AccTraceEntry_t), (RF_AccTraceEntry_t *));
    573 #endif
    574 
    575 	/* quiesce the array prior to starting recon.  this is needed
    576 	 * to assure no nasty interactions with pending user writes.
    577 	 * We need to do this before we change the disk or row status. */
    578 
    579 	Dprintf("RECON: begin request suspend\n");
    580 	rf_SuspendNewRequestsAndWait(raidPtr);
    581 	Dprintf("RECON: end request suspend\n");
    582 
    583 	/* allocate our RF_ReconCTRL_t before we protect raidPtr->reconControl[row] */
    584 	tmp_reconctrl = rf_MakeReconControl(reconDesc, col, scol);
    585 
    586 	RF_LOCK_MUTEX(raidPtr->mutex);
    587 
    588 	/* create the reconstruction control pointer and install it in
    589 	 * the right slot */
    590 	raidPtr->reconControl = tmp_reconctrl;
    591 	mapPtr = raidPtr->reconControl->reconMap;
    592 	raidPtr->reconControl->numRUsTotal = mapPtr->totalRUs;
    593 	raidPtr->reconControl->numRUsComplete =	0;
    594 	raidPtr->status = rf_rs_reconstructing;
    595 	raidPtr->Disks[col].status = rf_ds_reconstructing;
    596 	raidPtr->Disks[col].spareCol = scol;
    597 
    598 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    599 
    600 	RF_GETTIME(raidPtr->reconControl->starttime);
    601 
    602 	Dprintf("RECON: resume requests\n");
    603 	rf_ResumeNewRequests(raidPtr);
    604 
    605 
    606 	mapPtr = raidPtr->reconControl->reconMap;
    607 
    608 	incPSID = RF_RECONMAP_SIZE;
    609 	lastPSID = raidPtr->Layout.numStripe / raidPtr->Layout.SUsPerPU;
    610 	RUsPerPU = raidPtr->Layout.SUsPerPU / raidPtr->Layout.SUsPerRU;
    611 	recon_error = 0;
    612 	write_error = 0;
    613 	pending_writes = incPSID;
    614 	raidPtr->reconControl->lastPSID = incPSID - 1;
    615 
    616 	/* bounds check raidPtr->reconControl->lastPSID and
    617 	   pending_writes so that we don't attempt to wait for more IO
    618 	   than can possibly happen */
    619 
    620 	if (raidPtr->reconControl->lastPSID > lastPSID)
    621 		raidPtr->reconControl->lastPSID = lastPSID;
    622 
    623 	if (pending_writes > lastPSID)
    624 		pending_writes = lastPSID;
    625 
    626 	/* start the actual reconstruction */
    627 
    628 	done = 0;
    629 	while (!done) {
    630 
    631 		if (raidPtr->waitShutdown) {
    632 			/* someone is unconfiguring this array... bail on the reconstruct.. */
    633 			recon_error = 1;
    634 			break;
    635 		}
    636 
    637 		num_writes = 0;
    638 
    639 #if RF_INCLUDE_RAID5_RS > 0
    640 		/* For RAID5 with Rotated Spares we will be 'short'
    641 		   some number of writes since no writes will get
    642 		   issued for stripes where the spare is on the
    643 		   component being rebuilt.  Account for the shortage
    644 		   here so that we don't hang indefinitely below
    645 		   waiting for writes to complete that were never
    646 		   scheduled.
    647 
    648 		   XXX: Should be fixed for PARITY_DECLUSTERING and
    649 		   others too!
    650 
    651 		*/
    652 
    653 		if (raidPtr->Layout.numDataCol <
    654 		    raidPtr->numCol - raidPtr->Layout.numParityCol) {
    655 			/* numDataCol is at least 2 less than numCol, so
    656 			   should be RAID 5 with Rotated Spares */
    657 
    658 			/* XXX need to update for RAID 6 */
    659 
    660 			startPSID = raidPtr->reconControl->lastPSID - pending_writes + 1;
    661 			endPSID = raidPtr->reconControl->lastPSID;
    662 
    663 			offPSID = raidPtr->numCol - col - 1;
    664 
    665 			aPSID = startPSID - startPSID % raidPtr->numCol + offPSID;
    666 			if (aPSID < startPSID) {
    667 				aPSID += raidPtr->numCol;
    668 			}
    669 
    670 			bPSID = endPSID - ((endPSID - offPSID) % raidPtr->numCol);
    671 
    672 			if (aPSID < endPSID) {
    673 				num_writes = ((bPSID - aPSID) / raidPtr->numCol) + 1;
    674 			}
    675 
    676 			if ((aPSID == endPSID) && (bPSID == endPSID)) {
    677 				num_writes++;
    678 			}
    679 		}
    680 #endif
    681 
    682 		/* issue a read for each surviving disk */
    683 
    684 		reconDesc->numDisksDone = 0;
    685 		for (i = 0; i < raidPtr->numCol; i++) {
    686 			if (i != col) {
    687 				/* find and issue the next I/O on the
    688 				 * indicated disk */
    689 				if (IssueNextReadRequest(raidPtr, i)) {
    690 					Dprintf1("RECON: done issuing for c%d\n", i);
    691 					reconDesc->numDisksDone++;
    692 				}
    693 			}
    694 		}
    695 
    696 		/* process reconstruction events until all disks report that
    697 		 * they've completed all work */
    698 
    699 		while (reconDesc->numDisksDone < raidPtr->numCol - 1) {
    700 
    701 			event = rf_GetNextReconEvent(reconDesc);
    702 			status = ProcessReconEvent(raidPtr, event);
    703 
    704 			/* the normal case is that a read completes, and all is well. */
    705 			if (status == RF_RECON_DONE_READS) {
    706 				reconDesc->numDisksDone++;
    707 			} else if ((status == RF_RECON_READ_ERROR) ||
    708 				   (status == RF_RECON_WRITE_ERROR)) {
    709 				/* an error was encountered while reconstructing...
    710 				   Pretend we've finished this disk.
    711 				*/
    712 				recon_error = 1;
    713 				raidPtr->reconControl->error = 1;
    714 
    715 				/* bump the numDisksDone count for reads,
    716 				   but not for writes */
    717 				if (status == RF_RECON_READ_ERROR)
    718 					reconDesc->numDisksDone++;
    719 
    720 				/* write errors are special -- when we are
    721 				   done dealing with the reads that are
    722 				   finished, we don't want to wait for any
    723 				   writes */
    724 				if (status == RF_RECON_WRITE_ERROR) {
    725 					write_error = 1;
    726 					num_writes++;
    727 				}
    728 
    729 			} else if (status == RF_RECON_READ_STOPPED) {
    730 				/* count this component as being "done" */
    731 				reconDesc->numDisksDone++;
    732 			} else if (status == RF_RECON_WRITE_DONE) {
    733 				num_writes++;
    734 			}
    735 
    736 			if (recon_error) {
    737 				/* make sure any stragglers are woken up so that
    738 				   their theads will complete, and we can get out
    739 				   of here with all IO processed */
    740 
    741 				rf_WakeupHeadSepCBWaiters(raidPtr);
    742 			}
    743 
    744 			raidPtr->reconControl->numRUsTotal =
    745 				mapPtr->totalRUs;
    746 			raidPtr->reconControl->numRUsComplete =
    747 				mapPtr->totalRUs -
    748 				rf_UnitsLeftToReconstruct(mapPtr);
    749 
    750 #if RF_DEBUG_RECON
    751 			raidPtr->reconControl->percentComplete =
    752 				(raidPtr->reconControl->numRUsComplete * 100 / raidPtr->reconControl->numRUsTotal);
    753 			if (rf_prReconSched) {
    754 				rf_PrintReconSchedule(raidPtr->reconControl->reconMap, &(raidPtr->reconControl->starttime));
    755 			}
    756 #endif
    757 		}
    758 
    759 		/* reads done, wakeup any waiters, and then wait for writes */
    760 
    761 		rf_WakeupHeadSepCBWaiters(raidPtr);
    762 
    763 		while (!recon_error && (num_writes < pending_writes)) {
    764 			event = rf_GetNextReconEvent(reconDesc);
    765 			status = ProcessReconEvent(raidPtr, event);
    766 
    767 			if (status == RF_RECON_WRITE_ERROR) {
    768 				num_writes++;
    769 				recon_error = 1;
    770 				raidPtr->reconControl->error = 1;
    771 				/* an error was encountered at the very end... bail */
    772 			} else if (status == RF_RECON_WRITE_DONE) {
    773 				num_writes++;
    774 			} /* else it's something else, and we don't care */
    775 		}
    776 		if (recon_error ||
    777 		    (raidPtr->reconControl->lastPSID == lastPSID)) {
    778 			done = 1;
    779 			break;
    780 		}
    781 
    782 		prev = raidPtr->reconControl->lastPSID;
    783 		raidPtr->reconControl->lastPSID += incPSID;
    784 
    785 		if (raidPtr->reconControl->lastPSID > lastPSID) {
    786 			pending_writes = lastPSID - prev;
    787 			raidPtr->reconControl->lastPSID = lastPSID;
    788 		}
    789 
    790 		/* back down curPSID to get ready for the next round... */
    791 		for (i = 0; i < raidPtr->numCol; i++) {
    792 			if (i != col) {
    793 				raidPtr->reconControl->perDiskInfo[i].curPSID--;
    794 				raidPtr->reconControl->perDiskInfo[i].ru_count = RUsPerPU - 1;
    795 			}
    796 		}
    797 	}
    798 
    799 	mapPtr = raidPtr->reconControl->reconMap;
    800 	if (rf_reconDebug) {
    801 		printf("RECON: all reads completed\n");
    802 	}
    803 	/* at this point all the reads have completed.  We now wait
    804 	 * for any pending writes to complete, and then we're done */
    805 
    806 	while (!recon_error && rf_UnitsLeftToReconstruct(raidPtr->reconControl->reconMap) > 0) {
    807 
    808 		event = rf_GetNextReconEvent(reconDesc);
    809 		status = ProcessReconEvent(raidPtr, event);
    810 
    811 		if (status == RF_RECON_WRITE_ERROR) {
    812 			recon_error = 1;
    813 			raidPtr->reconControl->error = 1;
    814 			/* an error was encountered at the very end... bail */
    815 		} else {
    816 #if RF_DEBUG_RECON
    817 			raidPtr->reconControl->percentComplete = 100 - (rf_UnitsLeftToReconstruct(mapPtr) * 100 / mapPtr->totalRUs);
    818 			if (rf_prReconSched) {
    819 				rf_PrintReconSchedule(raidPtr->reconControl->reconMap, &(raidPtr->reconControl->starttime));
    820 			}
    821 #endif
    822 		}
    823 	}
    824 
    825 	if (recon_error) {
    826 		/* we've encountered an error in reconstructing. */
    827 		printf("raid%d: reconstruction failed.\n", raidPtr->raidid);
    828 
    829 		/* we start by blocking IO to the RAID set. */
    830 		rf_SuspendNewRequestsAndWait(raidPtr);
    831 
    832 		RF_LOCK_MUTEX(raidPtr->mutex);
    833 		/* mark set as being degraded, rather than
    834 		   rf_rs_reconstructing as we were before the problem.
    835 		   After this is done we can update status of the
    836 		   component disks without worrying about someone
    837 		   trying to read from a failed component.
    838 		*/
    839 		raidPtr->status = rf_rs_degraded;
    840 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    841 
    842 		/* resume IO */
    843 		rf_ResumeNewRequests(raidPtr);
    844 
    845 		/* At this point there are two cases:
    846 		   1) If we've experienced a read error, then we've
    847 		   already waited for all the reads we're going to get,
    848 		   and we just need to wait for the writes.
    849 
    850 		   2) If we've experienced a write error, we've also
    851 		   already waited for all the reads to complete,
    852 		   but there is little point in waiting for the writes --
    853 		   when they do complete, they will just be ignored.
    854 
    855 		   So we just wait for writes to complete if we didn't have a
    856 		   write error.
    857 		*/
    858 
    859 		if (!write_error) {
    860 			/* wait for writes to complete */
    861 			while (raidPtr->reconControl->pending_writes > 0) {
    862 
    863 				event = rf_GetNextReconEvent(reconDesc);
    864 				status = ProcessReconEvent(raidPtr, event);
    865 
    866 				if (status == RF_RECON_WRITE_ERROR) {
    867 					raidPtr->reconControl->error = 1;
    868 					/* an error was encountered at the very end... bail.
    869 					   This will be very bad news for the user, since
    870 					   at this point there will have been a read error
    871 					   on one component, and a write error on another!
    872 					*/
    873 					break;
    874 				}
    875 			}
    876 		}
    877 
    878 
    879 		/* cleanup */
    880 
    881 		/* drain the event queue - after waiting for the writes above,
    882 		   there shouldn't be much (if anything!) left in the queue. */
    883 
    884 		rf_DrainReconEventQueue(reconDesc);
    885 
    886 		/* XXX  As much as we'd like to free the recon control structure
    887 		   and the reconDesc, we have no way of knowing if/when those will
    888 		   be touched by IO that has yet to occur.  It is rather poor to be
    889 		   basically causing a 'memory leak' here, but there doesn't seem to be
    890 		   a cleaner alternative at this time.  Perhaps when the reconstruct code
    891 		   gets a makeover this problem will go away.
    892 		*/
    893 #if 0
    894 		rf_FreeReconControl(raidPtr);
    895 #endif
    896 
    897 #if RF_ACC_TRACE > 0
    898 		RF_Free(raidPtr->recon_tracerecs, raidPtr->numCol * sizeof(RF_AccTraceEntry_t));
    899 #endif
    900 		/* XXX see comment above */
    901 #if 0
    902 		FreeReconDesc(reconDesc);
    903 #endif
    904 
    905 		return (1);
    906 	}
    907 
    908 	/* Success:  mark the dead disk as reconstructed.  We quiesce
    909 	 * the array here to assure no nasty interactions with pending
    910 	 * user accesses when we free up the psstatus structure as
    911 	 * part of FreeReconControl() */
    912 
    913 	rf_SuspendNewRequestsAndWait(raidPtr);
    914 
    915 	RF_LOCK_MUTEX(raidPtr->mutex);
    916 	raidPtr->numFailures--;
    917 	ds = (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE);
    918 	raidPtr->Disks[col].status = (ds) ? rf_ds_dist_spared : rf_ds_spared;
    919 	raidPtr->status = (ds) ? rf_rs_reconfigured : rf_rs_optimal;
    920 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    921 	RF_GETTIME(etime);
    922 	RF_TIMEVAL_DIFF(&(raidPtr->reconControl->starttime), &etime, &elpsd);
    923 
    924 	rf_ResumeNewRequests(raidPtr);
    925 
    926 	printf("raid%d: Reconstruction of disk at col %d completed\n",
    927 	       raidPtr->raidid, col);
    928 	xor_s = raidPtr->accumXorTimeUs / 1000000;
    929 	xor_resid_us = raidPtr->accumXorTimeUs % 1000000;
    930 	printf("raid%d: Recon time was %d.%06d seconds, accumulated XOR time was %ld us (%ld.%06ld)\n",
    931 	       raidPtr->raidid,
    932 	       (int) elpsd.tv_sec, (int) elpsd.tv_usec,
    933 	       raidPtr->accumXorTimeUs, xor_s, xor_resid_us);
    934 	printf("raid%d:  (start time %d sec %d usec, end time %d sec %d usec)\n",
    935 	       raidPtr->raidid,
    936 	       (int) raidPtr->reconControl->starttime.tv_sec,
    937 	       (int) raidPtr->reconControl->starttime.tv_usec,
    938 	       (int) etime.tv_sec, (int) etime.tv_usec);
    939 #if RF_RECON_STATS > 0
    940 	printf("raid%d: Total head-sep stall count was %d\n",
    941 	       raidPtr->raidid, (int) reconDesc->hsStallCount);
    942 #endif				/* RF_RECON_STATS > 0 */
    943 	rf_FreeReconControl(raidPtr);
    944 #if RF_ACC_TRACE > 0
    945 	RF_Free(raidPtr->recon_tracerecs, raidPtr->numCol * sizeof(RF_AccTraceEntry_t));
    946 #endif
    947 	FreeReconDesc(reconDesc);
    948 
    949 	return (0);
    950 
    951 }
    952 /*****************************************************************************
    953  * do the right thing upon each reconstruction event.
    954  *****************************************************************************/
    955 static int
    956 ProcessReconEvent(RF_Raid_t *raidPtr, RF_ReconEvent_t *event)
    957 {
    958 	int     retcode = 0, submitblocked;
    959 	RF_ReconBuffer_t *rbuf;
    960 	RF_SectorCount_t sectorsPerRU;
    961 
    962 	retcode = RF_RECON_READ_STOPPED;
    963 
    964 	Dprintf1("RECON: ProcessReconEvent type %d\n", event->type);
    965 
    966 	switch (event->type) {
    967 
    968 		/* a read I/O has completed */
    969 	case RF_REVENT_READDONE:
    970 		rbuf = raidPtr->reconControl->perDiskInfo[event->col].rbuf;
    971 		Dprintf2("RECON: READDONE EVENT: col %d psid %ld\n",
    972 		    event->col, rbuf->parityStripeID);
    973 		Dprintf7("RECON: done read  psid %ld buf %lx  %02x %02x %02x %02x %02x\n",
    974 		    rbuf->parityStripeID, rbuf->buffer, rbuf->buffer[0] & 0xff, rbuf->buffer[1] & 0xff,
    975 		    rbuf->buffer[2] & 0xff, rbuf->buffer[3] & 0xff, rbuf->buffer[4] & 0xff);
    976 		rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
    977 		if (!raidPtr->reconControl->error) {
    978 			submitblocked = rf_SubmitReconBuffer(rbuf, 0, 0);
    979 			Dprintf1("RECON: submitblocked=%d\n", submitblocked);
    980 			if (!submitblocked)
    981 				retcode = IssueNextReadRequest(raidPtr, event->col);
    982 			else
    983 				retcode = 0;
    984 		}
    985 		break;
    986 
    987 		/* a write I/O has completed */
    988 	case RF_REVENT_WRITEDONE:
    989 #if RF_DEBUG_RECON
    990 		if (rf_floatingRbufDebug) {
    991 			rf_CheckFloatingRbufCount(raidPtr, 1);
    992 		}
    993 #endif
    994 		sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU;
    995 		rbuf = (RF_ReconBuffer_t *) event->arg;
    996 		rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
    997 		Dprintf3("RECON: WRITEDONE EVENT: psid %d ru %d (%d %% complete)\n",
    998 		    rbuf->parityStripeID, rbuf->which_ru, raidPtr->reconControl->percentComplete);
    999 		rf_ReconMapUpdate(raidPtr, raidPtr->reconControl->reconMap,
   1000 		    rbuf->failedDiskSectorOffset, rbuf->failedDiskSectorOffset + sectorsPerRU - 1);
   1001 		rf_RemoveFromActiveReconTable(raidPtr, rbuf->parityStripeID, rbuf->which_ru);
   1002 
   1003 		RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1004 		raidPtr->reconControl->pending_writes--;
   1005 		RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1006 
   1007 		if (rbuf->type == RF_RBUF_TYPE_FLOATING) {
   1008 			RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1009 			while(raidPtr->reconControl->rb_lock) {
   1010 				ltsleep(&raidPtr->reconControl->rb_lock, PRIBIO, "reconctrlpre1", 0,
   1011 					&raidPtr->reconControl->rb_mutex);
   1012 			}
   1013 			raidPtr->reconControl->rb_lock = 1;
   1014 			RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1015 
   1016 			raidPtr->numFullReconBuffers--;
   1017 			rf_ReleaseFloatingReconBuffer(raidPtr, rbuf);
   1018 
   1019 			RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1020 			raidPtr->reconControl->rb_lock = 0;
   1021 			wakeup(&raidPtr->reconControl->rb_lock);
   1022 			RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1023 		} else
   1024 			if (rbuf->type == RF_RBUF_TYPE_FORCED)
   1025 				rf_FreeReconBuffer(rbuf);
   1026 			else
   1027 				RF_ASSERT(0);
   1028 		retcode = RF_RECON_WRITE_DONE;
   1029 		break;
   1030 
   1031 	case RF_REVENT_BUFCLEAR:	/* A buffer-stall condition has been
   1032 					 * cleared */
   1033 		Dprintf1("RECON: BUFCLEAR EVENT: col %d\n", event->col);
   1034 		if (!raidPtr->reconControl->error) {
   1035 			submitblocked = rf_SubmitReconBuffer(raidPtr->reconControl->perDiskInfo[event->col].rbuf,
   1036 							     0, (int) (long) event->arg);
   1037 			RF_ASSERT(!submitblocked);	/* we wouldn't have gotten the
   1038 							 * BUFCLEAR event if we
   1039 							 * couldn't submit */
   1040 			retcode = IssueNextReadRequest(raidPtr, event->col);
   1041 		}
   1042 		break;
   1043 
   1044 	case RF_REVENT_BLOCKCLEAR:	/* A user-write reconstruction
   1045 					 * blockage has been cleared */
   1046 		DDprintf1("RECON: BLOCKCLEAR EVENT: col %d\n", event->col);
   1047 		if (!raidPtr->reconControl->error) {
   1048 			retcode = TryToRead(raidPtr, event->col);
   1049 		}
   1050 		break;
   1051 
   1052 	case RF_REVENT_HEADSEPCLEAR:	/* A max-head-separation
   1053 					 * reconstruction blockage has been
   1054 					 * cleared */
   1055 		Dprintf1("RECON: HEADSEPCLEAR EVENT: col %d\n", event->col);
   1056 		if (!raidPtr->reconControl->error) {
   1057 			retcode = TryToRead(raidPtr, event->col);
   1058 		}
   1059 		break;
   1060 
   1061 		/* a buffer has become ready to write */
   1062 	case RF_REVENT_BUFREADY:
   1063 		Dprintf1("RECON: BUFREADY EVENT: col %d\n", event->col);
   1064 		if (!raidPtr->reconControl->error) {
   1065 			retcode = IssueNextWriteRequest(raidPtr);
   1066 #if RF_DEBUG_RECON
   1067 			if (rf_floatingRbufDebug) {
   1068 				rf_CheckFloatingRbufCount(raidPtr, 1);
   1069 			}
   1070 #endif
   1071 		}
   1072 		break;
   1073 
   1074 		/* we need to skip the current RU entirely because it got
   1075 		 * recon'd while we were waiting for something else to happen */
   1076 	case RF_REVENT_SKIP:
   1077 		DDprintf1("RECON: SKIP EVENT: col %d\n", event->col);
   1078 		if (!raidPtr->reconControl->error) {
   1079 			retcode = IssueNextReadRequest(raidPtr, event->col);
   1080 		}
   1081 		break;
   1082 
   1083 		/* a forced-reconstruction read access has completed.  Just
   1084 		 * submit the buffer */
   1085 	case RF_REVENT_FORCEDREADDONE:
   1086 		rbuf = (RF_ReconBuffer_t *) event->arg;
   1087 		rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
   1088 		DDprintf1("RECON: FORCEDREADDONE EVENT: col %d\n", event->col);
   1089 		if (!raidPtr->reconControl->error) {
   1090 			submitblocked = rf_SubmitReconBuffer(rbuf, 1, 0);
   1091 			RF_ASSERT(!submitblocked);
   1092 			retcode = 0;
   1093 		}
   1094 		break;
   1095 
   1096 		/* A read I/O failed to complete */
   1097 	case RF_REVENT_READ_FAILED:
   1098 		retcode = RF_RECON_READ_ERROR;
   1099 		break;
   1100 
   1101 		/* A write I/O failed to complete */
   1102 	case RF_REVENT_WRITE_FAILED:
   1103 		retcode = RF_RECON_WRITE_ERROR;
   1104 
   1105 		/* This is an error, but it was a pending write.
   1106 		   Account for it. */
   1107 		RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1108 		raidPtr->reconControl->pending_writes--;
   1109 		RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1110 
   1111 		rbuf = (RF_ReconBuffer_t *) event->arg;
   1112 
   1113 		/* cleanup the disk queue data */
   1114 		rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
   1115 
   1116 		/* At this point we're erroring out, badly, and floatingRbufs
   1117 		   may not even be valid.  Rather than putting this back onto
   1118 		   the floatingRbufs list, just arrange for its immediate
   1119 		   destruction.
   1120 		*/
   1121 		rf_FreeReconBuffer(rbuf);
   1122 		break;
   1123 
   1124 		/* a forced read I/O failed to complete */
   1125 	case RF_REVENT_FORCEDREAD_FAILED:
   1126 		retcode = RF_RECON_READ_ERROR;
   1127 		break;
   1128 
   1129 	default:
   1130 		RF_PANIC();
   1131 	}
   1132 	rf_FreeReconEventDesc(event);
   1133 	return (retcode);
   1134 }
   1135 /*****************************************************************************
   1136  *
   1137  * find the next thing that's needed on the indicated disk, and issue
   1138  * a read request for it.  We assume that the reconstruction buffer
   1139  * associated with this process is free to receive the data.  If
   1140  * reconstruction is blocked on the indicated RU, we issue a
   1141  * blockage-release request instead of a physical disk read request.
   1142  * If the current disk gets too far ahead of the others, we issue a
   1143  * head-separation wait request and return.
   1144  *
   1145  * ctrl->{ru_count, curPSID, diskOffset} and
   1146  * rbuf->failedDiskSectorOffset are maintained to point to the unit
   1147  * we're currently accessing.  Note that this deviates from the
   1148  * standard C idiom of having counters point to the next thing to be
   1149  * accessed.  This allows us to easily retry when we're blocked by
   1150  * head separation or reconstruction-blockage events.
   1151  *
   1152  *****************************************************************************/
   1153 static int
   1154 IssueNextReadRequest(RF_Raid_t *raidPtr, RF_RowCol_t col)
   1155 {
   1156 	RF_PerDiskReconCtrl_t *ctrl = &raidPtr->reconControl->perDiskInfo[col];
   1157 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
   1158 	RF_ReconBuffer_t *rbuf = ctrl->rbuf;
   1159 	RF_ReconUnitCount_t RUsPerPU = layoutPtr->SUsPerPU / layoutPtr->SUsPerRU;
   1160 	RF_SectorCount_t sectorsPerRU = layoutPtr->sectorsPerStripeUnit * layoutPtr->SUsPerRU;
   1161 	int     do_new_check = 0, retcode = 0, status;
   1162 
   1163 	/* if we are currently the slowest disk, mark that we have to do a new
   1164 	 * check */
   1165 	if (ctrl->headSepCounter <= raidPtr->reconControl->minHeadSepCounter)
   1166 		do_new_check = 1;
   1167 
   1168 	while (1) {
   1169 
   1170 		ctrl->ru_count++;
   1171 		if (ctrl->ru_count < RUsPerPU) {
   1172 			ctrl->diskOffset += sectorsPerRU;
   1173 			rbuf->failedDiskSectorOffset += sectorsPerRU;
   1174 		} else {
   1175 			ctrl->curPSID++;
   1176 			ctrl->ru_count = 0;
   1177 			/* code left over from when head-sep was based on
   1178 			 * parity stripe id */
   1179 			if (ctrl->curPSID > raidPtr->reconControl->lastPSID) {
   1180 				CheckForNewMinHeadSep(raidPtr, ++(ctrl->headSepCounter));
   1181 				return (RF_RECON_DONE_READS);	/* finito! */
   1182 			}
   1183 			/* find the disk offsets of the start of the parity
   1184 			 * stripe on both the current disk and the failed
   1185 			 * disk. skip this entire parity stripe if either disk
   1186 			 * does not appear in the indicated PS */
   1187 			status = ComputePSDiskOffsets(raidPtr, ctrl->curPSID, col, &ctrl->diskOffset, &rbuf->failedDiskSectorOffset,
   1188 			    &rbuf->spCol, &rbuf->spOffset);
   1189 			if (status) {
   1190 				ctrl->ru_count = RUsPerPU - 1;
   1191 				continue;
   1192 			}
   1193 		}
   1194 		rbuf->which_ru = ctrl->ru_count;
   1195 
   1196 		/* skip this RU if it's already been reconstructed */
   1197 		if (rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, rbuf->failedDiskSectorOffset)) {
   1198 			Dprintf2("Skipping psid %ld ru %d: already reconstructed\n", ctrl->curPSID, ctrl->ru_count);
   1199 			continue;
   1200 		}
   1201 		break;
   1202 	}
   1203 	ctrl->headSepCounter++;
   1204 	if (do_new_check)
   1205 		CheckForNewMinHeadSep(raidPtr, ctrl->headSepCounter);	/* update min if needed */
   1206 
   1207 
   1208 	/* at this point, we have definitely decided what to do, and we have
   1209 	 * only to see if we can actually do it now */
   1210 	rbuf->parityStripeID = ctrl->curPSID;
   1211 	rbuf->which_ru = ctrl->ru_count;
   1212 #if RF_ACC_TRACE > 0
   1213 	memset((char *) &raidPtr->recon_tracerecs[col], 0,
   1214 	    sizeof(raidPtr->recon_tracerecs[col]));
   1215 	raidPtr->recon_tracerecs[col].reconacc = 1;
   1216 	RF_ETIMER_START(raidPtr->recon_tracerecs[col].recon_timer);
   1217 #endif
   1218 	retcode = TryToRead(raidPtr, col);
   1219 	return (retcode);
   1220 }
   1221 
   1222 /*
   1223  * tries to issue the next read on the indicated disk.  We may be
   1224  * blocked by (a) the heads being too far apart, or (b) recon on the
   1225  * indicated RU being blocked due to a write by a user thread.  In
   1226  * this case, we issue a head-sep or blockage wait request, which will
   1227  * cause this same routine to be invoked again later when the blockage
   1228  * has cleared.
   1229  */
   1230 
   1231 static int
   1232 TryToRead(RF_Raid_t *raidPtr, RF_RowCol_t col)
   1233 {
   1234 	RF_PerDiskReconCtrl_t *ctrl = &raidPtr->reconControl->perDiskInfo[col];
   1235 	RF_SectorCount_t sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU;
   1236 	RF_StripeNum_t psid = ctrl->curPSID;
   1237 	RF_ReconUnitNum_t which_ru = ctrl->ru_count;
   1238 	RF_DiskQueueData_t *req;
   1239 	int     status;
   1240 	RF_ReconParityStripeStatus_t *pssPtr, *newpssPtr;
   1241 
   1242 	/* if the current disk is too far ahead of the others, issue a
   1243 	 * head-separation wait and return */
   1244 	if (CheckHeadSeparation(raidPtr, ctrl, col, ctrl->headSepCounter, which_ru))
   1245 		return (0);
   1246 
   1247 	/* allocate a new PSS in case we need it */
   1248 	newpssPtr = rf_AllocPSStatus(raidPtr);
   1249 
   1250 	RF_LOCK_PSS_MUTEX(raidPtr, psid);
   1251 	pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl->pssTable, psid, which_ru, RF_PSS_CREATE, newpssPtr);
   1252 
   1253 	if (pssPtr != newpssPtr) {
   1254 		rf_FreePSStatus(raidPtr, newpssPtr);
   1255 	}
   1256 
   1257 	/* if recon is blocked on the indicated parity stripe, issue a
   1258 	 * block-wait request and return. this also must mark the indicated RU
   1259 	 * in the stripe as under reconstruction if not blocked. */
   1260 	status = CheckForcedOrBlockedReconstruction(raidPtr, pssPtr, ctrl, col, psid, which_ru);
   1261 	if (status == RF_PSS_RECON_BLOCKED) {
   1262 		Dprintf2("RECON: Stalling psid %ld ru %d: recon blocked\n", psid, which_ru);
   1263 		goto out;
   1264 	} else
   1265 		if (status == RF_PSS_FORCED_ON_WRITE) {
   1266 			rf_CauseReconEvent(raidPtr, col, NULL, RF_REVENT_SKIP);
   1267 			goto out;
   1268 		}
   1269 	/* make one last check to be sure that the indicated RU didn't get
   1270 	 * reconstructed while we were waiting for something else to happen.
   1271 	 * This is unfortunate in that it causes us to make this check twice
   1272 	 * in the normal case.  Might want to make some attempt to re-work
   1273 	 * this so that we only do this check if we've definitely blocked on
   1274 	 * one of the above checks.  When this condition is detected, we may
   1275 	 * have just created a bogus status entry, which we need to delete. */
   1276 	if (rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, ctrl->rbuf->failedDiskSectorOffset)) {
   1277 		Dprintf2("RECON: Skipping psid %ld ru %d: prior recon after stall\n", psid, which_ru);
   1278 		if (pssPtr == newpssPtr)
   1279 			rf_PSStatusDelete(raidPtr, raidPtr->reconControl->pssTable, pssPtr);
   1280 		rf_CauseReconEvent(raidPtr, col, NULL, RF_REVENT_SKIP);
   1281 		goto out;
   1282 	}
   1283 	/* found something to read.  issue the I/O */
   1284 	Dprintf4("RECON: Read for psid %ld on col %d offset %ld buf %lx\n",
   1285 	    psid, col, ctrl->diskOffset, ctrl->rbuf->buffer);
   1286 #if RF_ACC_TRACE > 0
   1287 	RF_ETIMER_STOP(raidPtr->recon_tracerecs[col].recon_timer);
   1288 	RF_ETIMER_EVAL(raidPtr->recon_tracerecs[col].recon_timer);
   1289 	raidPtr->recon_tracerecs[col].specific.recon.recon_start_to_fetch_us =
   1290 	    RF_ETIMER_VAL_US(raidPtr->recon_tracerecs[col].recon_timer);
   1291 	RF_ETIMER_START(raidPtr->recon_tracerecs[col].recon_timer);
   1292 #endif
   1293 	/* should be ok to use a NULL proc pointer here, all the bufs we use
   1294 	 * should be in kernel space */
   1295 	req = rf_CreateDiskQueueData(RF_IO_TYPE_READ, ctrl->diskOffset, sectorsPerRU, ctrl->rbuf->buffer, psid, which_ru,
   1296 	    ReconReadDoneProc, (void *) ctrl,
   1297 #if RF_ACC_TRACE > 0
   1298 				     &raidPtr->recon_tracerecs[col],
   1299 #else
   1300 				     NULL,
   1301 #endif
   1302 				     (void *) raidPtr, 0, NULL, PR_WAITOK);
   1303 
   1304 	ctrl->rbuf->arg = (void *) req;
   1305 	rf_DiskIOEnqueue(&raidPtr->Queues[col], req, RF_IO_RECON_PRIORITY);
   1306 	pssPtr->issued[col] = 1;
   1307 
   1308 out:
   1309 	RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
   1310 	return (0);
   1311 }
   1312 
   1313 
   1314 /*
   1315  * given a parity stripe ID, we want to find out whether both the
   1316  * current disk and the failed disk exist in that parity stripe.  If
   1317  * not, we want to skip this whole PS.  If so, we want to find the
   1318  * disk offset of the start of the PS on both the current disk and the
   1319  * failed disk.
   1320  *
   1321  * this works by getting a list of disks comprising the indicated
   1322  * parity stripe, and searching the list for the current and failed
   1323  * disks.  Once we've decided they both exist in the parity stripe, we
   1324  * need to decide whether each is data or parity, so that we'll know
   1325  * which mapping function to call to get the corresponding disk
   1326  * offsets.
   1327  *
   1328  * this is kind of unpleasant, but doing it this way allows the
   1329  * reconstruction code to use parity stripe IDs rather than physical
   1330  * disks address to march through the failed disk, which greatly
   1331  * simplifies a lot of code, as well as eliminating the need for a
   1332  * reverse-mapping function.  I also think it will execute faster,
   1333  * since the calls to the mapping module are kept to a minimum.
   1334  *
   1335  * ASSUMES THAT THE STRIPE IDENTIFIER IDENTIFIES THE DISKS COMPRISING
   1336  * THE STRIPE IN THE CORRECT ORDER
   1337  *
   1338  * raidPtr          - raid descriptor
   1339  * psid             - parity stripe identifier
   1340  * col              - column of disk to find the offsets for
   1341  * spCol            - out: col of spare unit for failed unit
   1342  * spOffset         - out: offset into disk containing spare unit
   1343  *
   1344  */
   1345 
   1346 
   1347 static int
   1348 ComputePSDiskOffsets(RF_Raid_t *raidPtr, RF_StripeNum_t psid,
   1349 		     RF_RowCol_t col, RF_SectorNum_t *outDiskOffset,
   1350 		     RF_SectorNum_t *outFailedDiskSectorOffset,
   1351 		     RF_RowCol_t *spCol, RF_SectorNum_t *spOffset)
   1352 {
   1353 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
   1354 	RF_RowCol_t fcol = raidPtr->reconControl->fcol;
   1355 	RF_RaidAddr_t sosRaidAddress;	/* start-of-stripe */
   1356 	RF_RowCol_t *diskids;
   1357 	u_int   i, j, k, i_offset, j_offset;
   1358 	RF_RowCol_t pcol;
   1359 	int     testcol;
   1360 	RF_SectorNum_t poffset;
   1361 	char    i_is_parity = 0, j_is_parity = 0;
   1362 	RF_RowCol_t stripeWidth = layoutPtr->numDataCol + layoutPtr->numParityCol;
   1363 
   1364 	/* get a listing of the disks comprising that stripe */
   1365 	sosRaidAddress = rf_ParityStripeIDToRaidAddress(layoutPtr, psid);
   1366 	(layoutPtr->map->IdentifyStripe) (raidPtr, sosRaidAddress, &diskids);
   1367 	RF_ASSERT(diskids);
   1368 
   1369 	/* reject this entire parity stripe if it does not contain the
   1370 	 * indicated disk or it does not contain the failed disk */
   1371 
   1372 	for (i = 0; i < stripeWidth; i++) {
   1373 		if (col == diskids[i])
   1374 			break;
   1375 	}
   1376 	if (i == stripeWidth)
   1377 		goto skipit;
   1378 	for (j = 0; j < stripeWidth; j++) {
   1379 		if (fcol == diskids[j])
   1380 			break;
   1381 	}
   1382 	if (j == stripeWidth) {
   1383 		goto skipit;
   1384 	}
   1385 	/* find out which disk the parity is on */
   1386 	(layoutPtr->map->MapParity) (raidPtr, sosRaidAddress, &pcol, &poffset, RF_DONT_REMAP);
   1387 
   1388 	/* find out if either the current RU or the failed RU is parity */
   1389 	/* also, if the parity occurs in this stripe prior to the data and/or
   1390 	 * failed col, we need to decrement i and/or j */
   1391 	for (k = 0; k < stripeWidth; k++)
   1392 		if (diskids[k] == pcol)
   1393 			break;
   1394 	RF_ASSERT(k < stripeWidth);
   1395 	i_offset = i;
   1396 	j_offset = j;
   1397 	if (k < i)
   1398 		i_offset--;
   1399 	else
   1400 		if (k == i) {
   1401 			i_is_parity = 1;
   1402 			i_offset = 0;
   1403 		}		/* set offsets to zero to disable multiply
   1404 				 * below */
   1405 	if (k < j)
   1406 		j_offset--;
   1407 	else
   1408 		if (k == j) {
   1409 			j_is_parity = 1;
   1410 			j_offset = 0;
   1411 		}
   1412 	/* at this point, [ij]_is_parity tells us whether the [current,failed]
   1413 	 * disk is parity at the start of this RU, and, if data, "[ij]_offset"
   1414 	 * tells us how far into the stripe the [current,failed] disk is. */
   1415 
   1416 	/* call the mapping routine to get the offset into the current disk,
   1417 	 * repeat for failed disk. */
   1418 	if (i_is_parity)
   1419 		layoutPtr->map->MapParity(raidPtr, sosRaidAddress + i_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outDiskOffset, RF_DONT_REMAP);
   1420 	else
   1421 		layoutPtr->map->MapSector(raidPtr, sosRaidAddress + i_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outDiskOffset, RF_DONT_REMAP);
   1422 
   1423 	RF_ASSERT(col == testcol);
   1424 
   1425 	if (j_is_parity)
   1426 		layoutPtr->map->MapParity(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outFailedDiskSectorOffset, RF_DONT_REMAP);
   1427 	else
   1428 		layoutPtr->map->MapSector(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outFailedDiskSectorOffset, RF_DONT_REMAP);
   1429 	RF_ASSERT(fcol == testcol);
   1430 
   1431 	/* now locate the spare unit for the failed unit */
   1432 #if RF_INCLUDE_PARITY_DECLUSTERING_DS > 0
   1433 	if (layoutPtr->map->flags & RF_DISTRIBUTE_SPARE) {
   1434 		if (j_is_parity)
   1435 			layoutPtr->map->MapParity(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, spCol, spOffset, RF_REMAP);
   1436 		else
   1437 			layoutPtr->map->MapSector(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, spCol, spOffset, RF_REMAP);
   1438 	} else {
   1439 #endif
   1440 		*spCol = raidPtr->reconControl->spareCol;
   1441 		*spOffset = *outFailedDiskSectorOffset;
   1442 #if RF_INCLUDE_PARITY_DECLUSTERING_DS > 0
   1443 	}
   1444 #endif
   1445 	return (0);
   1446 
   1447 skipit:
   1448 	Dprintf2("RECON: Skipping psid %ld: nothing needed from c%d\n",
   1449 	    psid, col);
   1450 	return (1);
   1451 }
   1452 /* this is called when a buffer has become ready to write to the replacement disk */
   1453 static int
   1454 IssueNextWriteRequest(RF_Raid_t *raidPtr)
   1455 {
   1456 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
   1457 	RF_SectorCount_t sectorsPerRU = layoutPtr->sectorsPerStripeUnit * layoutPtr->SUsPerRU;
   1458 #if RF_ACC_TRACE > 0
   1459 	RF_RowCol_t fcol = raidPtr->reconControl->fcol;
   1460 #endif
   1461 	RF_ReconBuffer_t *rbuf;
   1462 	RF_DiskQueueData_t *req;
   1463 
   1464 	rbuf = rf_GetFullReconBuffer(raidPtr->reconControl);
   1465 	RF_ASSERT(rbuf);	/* there must be one available, or we wouldn't
   1466 				 * have gotten the event that sent us here */
   1467 	RF_ASSERT(rbuf->pssPtr);
   1468 
   1469 	rbuf->pssPtr->writeRbuf = rbuf;
   1470 	rbuf->pssPtr = NULL;
   1471 
   1472 	Dprintf6("RECON: New write (c %d offs %d) for psid %ld ru %d (failed disk offset %ld) buf %lx\n",
   1473 	    rbuf->spCol, rbuf->spOffset, rbuf->parityStripeID,
   1474 	    rbuf->which_ru, rbuf->failedDiskSectorOffset, rbuf->buffer);
   1475 	Dprintf6("RECON: new write psid %ld   %02x %02x %02x %02x %02x\n",
   1476 	    rbuf->parityStripeID, rbuf->buffer[0] & 0xff, rbuf->buffer[1] & 0xff,
   1477 	    rbuf->buffer[2] & 0xff, rbuf->buffer[3] & 0xff, rbuf->buffer[4] & 0xff);
   1478 
   1479 	/* should be ok to use a NULL b_proc here b/c all addrs should be in
   1480 	 * kernel space */
   1481 	req = rf_CreateDiskQueueData(RF_IO_TYPE_WRITE, rbuf->spOffset,
   1482 	    sectorsPerRU, rbuf->buffer,
   1483 	    rbuf->parityStripeID, rbuf->which_ru,
   1484 	    ReconWriteDoneProc, (void *) rbuf,
   1485 #if RF_ACC_TRACE > 0
   1486 	    &raidPtr->recon_tracerecs[fcol],
   1487 #else
   1488 				     NULL,
   1489 #endif
   1490 	    (void *) raidPtr, 0, NULL, PR_WAITOK);
   1491 
   1492 	rbuf->arg = (void *) req;
   1493 	RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1494 	raidPtr->reconControl->pending_writes++;
   1495 	RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1496 	rf_DiskIOEnqueue(&raidPtr->Queues[rbuf->spCol], req, RF_IO_RECON_PRIORITY);
   1497 
   1498 	return (0);
   1499 }
   1500 
   1501 /*
   1502  * this gets called upon the completion of a reconstruction read
   1503  * operation the arg is a pointer to the per-disk reconstruction
   1504  * control structure for the process that just finished a read.
   1505  *
   1506  * called at interrupt context in the kernel, so don't do anything
   1507  * illegal here.
   1508  */
   1509 static int
   1510 ReconReadDoneProc(void *arg, int status)
   1511 {
   1512 	RF_PerDiskReconCtrl_t *ctrl = (RF_PerDiskReconCtrl_t *) arg;
   1513 	RF_Raid_t *raidPtr;
   1514 
   1515 	/* Detect that reconCtrl is no longer valid, and if that
   1516 	   is the case, bail without calling rf_CauseReconEvent().
   1517 	   There won't be anyone listening for this event anyway */
   1518 
   1519 	if (ctrl->reconCtrl == NULL)
   1520 		return(0);
   1521 
   1522 	raidPtr = ctrl->reconCtrl->reconDesc->raidPtr;
   1523 
   1524 	if (status) {
   1525 		printf("raid%d: Recon read failed: %d\n", raidPtr->raidid, status);
   1526 		rf_CauseReconEvent(raidPtr, ctrl->col, NULL, RF_REVENT_READ_FAILED);
   1527 		return(0);
   1528 	}
   1529 #if RF_ACC_TRACE > 0
   1530 	RF_ETIMER_STOP(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
   1531 	RF_ETIMER_EVAL(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
   1532 	raidPtr->recon_tracerecs[ctrl->col].specific.recon.recon_fetch_to_return_us =
   1533 	    RF_ETIMER_VAL_US(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
   1534 	RF_ETIMER_START(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
   1535 #endif
   1536 	rf_CauseReconEvent(raidPtr, ctrl->col, NULL, RF_REVENT_READDONE);
   1537 	return (0);
   1538 }
   1539 /* this gets called upon the completion of a reconstruction write operation.
   1540  * the arg is a pointer to the rbuf that was just written
   1541  *
   1542  * called at interrupt context in the kernel, so don't do anything illegal here.
   1543  */
   1544 static int
   1545 ReconWriteDoneProc(void *arg, int status)
   1546 {
   1547 	RF_ReconBuffer_t *rbuf = (RF_ReconBuffer_t *) arg;
   1548 
   1549 	/* Detect that reconControl is no longer valid, and if that
   1550 	   is the case, bail without calling rf_CauseReconEvent().
   1551 	   There won't be anyone listening for this event anyway */
   1552 
   1553 	if (rbuf->raidPtr->reconControl == NULL)
   1554 		return(0);
   1555 
   1556 	Dprintf2("Reconstruction completed on psid %ld ru %d\n", rbuf->parityStripeID, rbuf->which_ru);
   1557 	if (status) {
   1558 		printf("raid%d: Recon write failed!\n", rbuf->raidPtr->raidid);
   1559 		rf_CauseReconEvent(rbuf->raidPtr, rbuf->col, arg, RF_REVENT_WRITE_FAILED);
   1560 		return(0);
   1561 	}
   1562 	rf_CauseReconEvent(rbuf->raidPtr, rbuf->col, arg, RF_REVENT_WRITEDONE);
   1563 	return (0);
   1564 }
   1565 
   1566 
   1567 /*
   1568  * computes a new minimum head sep, and wakes up anyone who needs to
   1569  * be woken as a result
   1570  */
   1571 static void
   1572 CheckForNewMinHeadSep(RF_Raid_t *raidPtr, RF_HeadSepLimit_t hsCtr)
   1573 {
   1574 	RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl;
   1575 	RF_HeadSepLimit_t new_min;
   1576 	RF_RowCol_t i;
   1577 	RF_CallbackDesc_t *p;
   1578 	RF_ASSERT(hsCtr >= reconCtrlPtr->minHeadSepCounter);	/* from the definition
   1579 								 * of a minimum */
   1580 
   1581 
   1582 	RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1583 	while(reconCtrlPtr->rb_lock) {
   1584 		ltsleep(&reconCtrlPtr->rb_lock, PRIBIO, "reconctlcnmhs", 0, &reconCtrlPtr->rb_mutex);
   1585 	}
   1586 	reconCtrlPtr->rb_lock = 1;
   1587 	RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1588 
   1589 	new_min = ~(1L << (8 * sizeof(long) - 1));	/* 0x7FFF....FFF */
   1590 	for (i = 0; i < raidPtr->numCol; i++)
   1591 		if (i != reconCtrlPtr->fcol) {
   1592 			if (reconCtrlPtr->perDiskInfo[i].headSepCounter < new_min)
   1593 				new_min = reconCtrlPtr->perDiskInfo[i].headSepCounter;
   1594 		}
   1595 	/* set the new minimum and wake up anyone who can now run again */
   1596 	if (new_min != reconCtrlPtr->minHeadSepCounter) {
   1597 		reconCtrlPtr->minHeadSepCounter = new_min;
   1598 		Dprintf1("RECON:  new min head pos counter val is %ld\n", new_min);
   1599 		while (reconCtrlPtr->headSepCBList) {
   1600 			if (reconCtrlPtr->headSepCBList->callbackArg.v > new_min)
   1601 				break;
   1602 			p = reconCtrlPtr->headSepCBList;
   1603 			reconCtrlPtr->headSepCBList = p->next;
   1604 			p->next = NULL;
   1605 			rf_CauseReconEvent(raidPtr, p->col, NULL, RF_REVENT_HEADSEPCLEAR);
   1606 			rf_FreeCallbackDesc(p);
   1607 		}
   1608 
   1609 	}
   1610 	RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1611 	reconCtrlPtr->rb_lock = 0;
   1612 	wakeup(&reconCtrlPtr->rb_lock);
   1613 	RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1614 }
   1615 
   1616 /*
   1617  * checks to see that the maximum head separation will not be violated
   1618  * if we initiate a reconstruction I/O on the indicated disk.
   1619  * Limiting the maximum head separation between two disks eliminates
   1620  * the nasty buffer-stall conditions that occur when one disk races
   1621  * ahead of the others and consumes all of the floating recon buffers.
   1622  * This code is complex and unpleasant but it's necessary to avoid
   1623  * some very nasty, albeit fairly rare, reconstruction behavior.
   1624  *
   1625  * returns non-zero if and only if we have to stop working on the
   1626  * indicated disk due to a head-separation delay.
   1627  */
   1628 static int
   1629 CheckHeadSeparation(RF_Raid_t *raidPtr, RF_PerDiskReconCtrl_t *ctrl,
   1630 		    RF_RowCol_t col, RF_HeadSepLimit_t hsCtr,
   1631 		    RF_ReconUnitNum_t which_ru)
   1632 {
   1633 	RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl;
   1634 	RF_CallbackDesc_t *cb, *p, *pt;
   1635 	int     retval = 0;
   1636 
   1637 	/* if we're too far ahead of the slowest disk, stop working on this
   1638 	 * disk until the slower ones catch up.  We do this by scheduling a
   1639 	 * wakeup callback for the time when the slowest disk has caught up.
   1640 	 * We define "caught up" with 20% hysteresis, i.e. the head separation
   1641 	 * must have fallen to at most 80% of the max allowable head
   1642 	 * separation before we'll wake up.
   1643 	 *
   1644 	 */
   1645 	RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1646 	while(reconCtrlPtr->rb_lock) {
   1647 		ltsleep(&reconCtrlPtr->rb_lock, PRIBIO, "reconctlchs", 0, &reconCtrlPtr->rb_mutex);
   1648 	}
   1649 	reconCtrlPtr->rb_lock = 1;
   1650 	RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1651 	if ((raidPtr->headSepLimit >= 0) &&
   1652 	    ((ctrl->headSepCounter - reconCtrlPtr->minHeadSepCounter) > raidPtr->headSepLimit)) {
   1653 		Dprintf5("raid%d: RECON: head sep stall: col %d hsCtr %ld minHSCtr %ld limit %ld\n",
   1654 			 raidPtr->raidid, col, ctrl->headSepCounter,
   1655 			 reconCtrlPtr->minHeadSepCounter,
   1656 			 raidPtr->headSepLimit);
   1657 		cb = rf_AllocCallbackDesc();
   1658 		/* the minHeadSepCounter value we have to get to before we'll
   1659 		 * wake up.  build in 20% hysteresis. */
   1660 		cb->callbackArg.v = (ctrl->headSepCounter - raidPtr->headSepLimit + raidPtr->headSepLimit / 5);
   1661 		cb->col = col;
   1662 		cb->next = NULL;
   1663 
   1664 		/* insert this callback descriptor into the sorted list of
   1665 		 * pending head-sep callbacks */
   1666 		p = reconCtrlPtr->headSepCBList;
   1667 		if (!p)
   1668 			reconCtrlPtr->headSepCBList = cb;
   1669 		else
   1670 			if (cb->callbackArg.v < p->callbackArg.v) {
   1671 				cb->next = reconCtrlPtr->headSepCBList;
   1672 				reconCtrlPtr->headSepCBList = cb;
   1673 			} else {
   1674 				for (pt = p, p = p->next; p && (p->callbackArg.v < cb->callbackArg.v); pt = p, p = p->next);
   1675 				cb->next = p;
   1676 				pt->next = cb;
   1677 			}
   1678 		retval = 1;
   1679 #if RF_RECON_STATS > 0
   1680 		ctrl->reconCtrl->reconDesc->hsStallCount++;
   1681 #endif				/* RF_RECON_STATS > 0 */
   1682 	}
   1683 	RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1684 	reconCtrlPtr->rb_lock = 0;
   1685 	wakeup(&reconCtrlPtr->rb_lock);
   1686 	RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1687 
   1688 	return (retval);
   1689 }
   1690 /*
   1691  * checks to see if reconstruction has been either forced or blocked
   1692  * by a user operation.  if forced, we skip this RU entirely.  else if
   1693  * blocked, put ourselves on the wait list.  else return 0.
   1694  *
   1695  * ASSUMES THE PSS MUTEX IS LOCKED UPON ENTRY
   1696  */
   1697 static int
   1698 CheckForcedOrBlockedReconstruction(RF_Raid_t *raidPtr,
   1699 				   RF_ReconParityStripeStatus_t *pssPtr,
   1700 				   RF_PerDiskReconCtrl_t *ctrl,
   1701 				   RF_RowCol_t col,
   1702 				   RF_StripeNum_t psid,
   1703 				   RF_ReconUnitNum_t which_ru)
   1704 {
   1705 	RF_CallbackDesc_t *cb;
   1706 	int     retcode = 0;
   1707 
   1708 	if ((pssPtr->flags & RF_PSS_FORCED_ON_READ) || (pssPtr->flags & RF_PSS_FORCED_ON_WRITE))
   1709 		retcode = RF_PSS_FORCED_ON_WRITE;
   1710 	else
   1711 		if (pssPtr->flags & RF_PSS_RECON_BLOCKED) {
   1712 			Dprintf3("RECON: col %d blocked at psid %ld ru %d\n", col, psid, which_ru);
   1713 			cb = rf_AllocCallbackDesc();	/* append ourselves to
   1714 							 * the blockage-wait
   1715 							 * list */
   1716 			cb->col = col;
   1717 			cb->next = pssPtr->blockWaitList;
   1718 			pssPtr->blockWaitList = cb;
   1719 			retcode = RF_PSS_RECON_BLOCKED;
   1720 		}
   1721 	if (!retcode)
   1722 		pssPtr->flags |= RF_PSS_UNDER_RECON;	/* mark this RU as under
   1723 							 * reconstruction */
   1724 
   1725 	return (retcode);
   1726 }
   1727 /*
   1728  * if reconstruction is currently ongoing for the indicated stripeID,
   1729  * reconstruction is forced to completion and we return non-zero to
   1730  * indicate that the caller must wait.  If not, then reconstruction is
   1731  * blocked on the indicated stripe and the routine returns zero.  If
   1732  * and only if we return non-zero, we'll cause the cbFunc to get
   1733  * invoked with the cbArg when the reconstruction has completed.
   1734  */
   1735 int
   1736 rf_ForceOrBlockRecon(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap,
   1737 		     void (*cbFunc)(RF_Raid_t *, void *), void *cbArg)
   1738 {
   1739 	RF_StripeNum_t stripeID = asmap->stripeID;	/* the stripe ID we're
   1740 							 * forcing recon on */
   1741 	RF_SectorCount_t sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU;	/* num sects in one RU */
   1742 	RF_ReconParityStripeStatus_t *pssPtr, *newpssPtr;	/* a pointer to the parity
   1743 						 * stripe status structure */
   1744 	RF_StripeNum_t psid;	/* parity stripe id */
   1745 	RF_SectorNum_t offset, fd_offset;	/* disk offset, failed-disk
   1746 						 * offset */
   1747 	RF_RowCol_t *diskids;
   1748 	RF_ReconUnitNum_t which_ru;	/* RU within parity stripe */
   1749 	RF_RowCol_t fcol, diskno, i;
   1750 	RF_ReconBuffer_t *new_rbuf;	/* ptr to newly allocated rbufs */
   1751 	RF_DiskQueueData_t *req;/* disk I/O req to be enqueued */
   1752 	RF_CallbackDesc_t *cb;
   1753 	int     nPromoted;
   1754 
   1755 	psid = rf_MapStripeIDToParityStripeID(&raidPtr->Layout, stripeID, &which_ru);
   1756 
   1757 	/* allocate a new PSS in case we need it */
   1758         newpssPtr = rf_AllocPSStatus(raidPtr);
   1759 
   1760 	RF_LOCK_PSS_MUTEX(raidPtr, psid);
   1761 
   1762 	pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl->pssTable, psid, which_ru, RF_PSS_CREATE | RF_PSS_RECON_BLOCKED, newpssPtr);
   1763 
   1764         if (pssPtr != newpssPtr) {
   1765                 rf_FreePSStatus(raidPtr, newpssPtr);
   1766         }
   1767 
   1768 	/* if recon is not ongoing on this PS, just return */
   1769 	if (!(pssPtr->flags & RF_PSS_UNDER_RECON)) {
   1770 		RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
   1771 		return (0);
   1772 	}
   1773 	/* otherwise, we have to wait for reconstruction to complete on this
   1774 	 * RU. */
   1775 	/* In order to avoid waiting for a potentially large number of
   1776 	 * low-priority accesses to complete, we force a normal-priority (i.e.
   1777 	 * not low-priority) reconstruction on this RU. */
   1778 	if (!(pssPtr->flags & RF_PSS_FORCED_ON_WRITE) && !(pssPtr->flags & RF_PSS_FORCED_ON_READ)) {
   1779 		DDprintf1("Forcing recon on psid %ld\n", psid);
   1780 		pssPtr->flags |= RF_PSS_FORCED_ON_WRITE;	/* mark this RU as under
   1781 								 * forced recon */
   1782 		pssPtr->flags &= ~RF_PSS_RECON_BLOCKED;	/* clear the blockage
   1783 							 * that we just set */
   1784 		fcol = raidPtr->reconControl->fcol;
   1785 
   1786 		/* get a listing of the disks comprising the indicated stripe */
   1787 		(raidPtr->Layout.map->IdentifyStripe) (raidPtr, asmap->raidAddress, &diskids);
   1788 
   1789 		/* For previously issued reads, elevate them to normal
   1790 		 * priority.  If the I/O has already completed, it won't be
   1791 		 * found in the queue, and hence this will be a no-op. For
   1792 		 * unissued reads, allocate buffers and issue new reads.  The
   1793 		 * fact that we've set the FORCED bit means that the regular
   1794 		 * recon procs will not re-issue these reqs */
   1795 		for (i = 0; i < raidPtr->Layout.numDataCol + raidPtr->Layout.numParityCol; i++)
   1796 			if ((diskno = diskids[i]) != fcol) {
   1797 				if (pssPtr->issued[diskno]) {
   1798 					nPromoted = rf_DiskIOPromote(&raidPtr->Queues[diskno], psid, which_ru);
   1799 					if (rf_reconDebug && nPromoted)
   1800 						printf("raid%d: promoted read from col %d\n", raidPtr->raidid, diskno);
   1801 				} else {
   1802 					new_rbuf = rf_MakeReconBuffer(raidPtr, diskno, RF_RBUF_TYPE_FORCED);	/* create new buf */
   1803 					ComputePSDiskOffsets(raidPtr, psid, diskno, &offset, &fd_offset,
   1804 					    &new_rbuf->spCol, &new_rbuf->spOffset);	/* find offsets & spare
   1805 													 * location */
   1806 					new_rbuf->parityStripeID = psid;	/* fill in the buffer */
   1807 					new_rbuf->which_ru = which_ru;
   1808 					new_rbuf->failedDiskSectorOffset = fd_offset;
   1809 					new_rbuf->priority = RF_IO_NORMAL_PRIORITY;
   1810 
   1811 					/* use NULL b_proc b/c all addrs
   1812 					 * should be in kernel space */
   1813 					req = rf_CreateDiskQueueData(RF_IO_TYPE_READ, offset + which_ru * sectorsPerRU, sectorsPerRU, new_rbuf->buffer,
   1814 					    psid, which_ru, (int (*) (void *, int)) ForceReconReadDoneProc, (void *) new_rbuf,
   1815 					    NULL, (void *) raidPtr, 0, NULL, PR_WAITOK);
   1816 
   1817 					new_rbuf->arg = req;
   1818 					rf_DiskIOEnqueue(&raidPtr->Queues[diskno], req, RF_IO_NORMAL_PRIORITY);	/* enqueue the I/O */
   1819 					Dprintf2("raid%d: Issued new read req on col %d\n", raidPtr->raidid, diskno);
   1820 				}
   1821 			}
   1822 		/* if the write is sitting in the disk queue, elevate its
   1823 		 * priority */
   1824 		if (rf_DiskIOPromote(&raidPtr->Queues[fcol], psid, which_ru))
   1825 			if (rf_reconDebug)
   1826 				printf("raid%d: promoted write to col %d\n",
   1827 				       raidPtr->raidid, fcol);
   1828 	}
   1829 	/* install a callback descriptor to be invoked when recon completes on
   1830 	 * this parity stripe. */
   1831 	cb = rf_AllocCallbackDesc();
   1832 	/* XXX the following is bogus.. These functions don't really match!!
   1833 	 * GO */
   1834 	cb->callbackFunc = (void (*) (RF_CBParam_t)) cbFunc;
   1835 	cb->callbackArg.p = (void *) cbArg;
   1836 	cb->next = pssPtr->procWaitList;
   1837 	pssPtr->procWaitList = cb;
   1838 	DDprintf2("raid%d: Waiting for forced recon on psid %ld\n",
   1839 		  raidPtr->raidid, psid);
   1840 
   1841 	RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
   1842 	return (1);
   1843 }
   1844 /* called upon the completion of a forced reconstruction read.
   1845  * all we do is schedule the FORCEDREADONE event.
   1846  * called at interrupt context in the kernel, so don't do anything illegal here.
   1847  */
   1848 static void
   1849 ForceReconReadDoneProc(void *arg, int status)
   1850 {
   1851 	RF_ReconBuffer_t *rbuf = arg;
   1852 
   1853 	/* Detect that reconControl is no longer valid, and if that
   1854 	   is the case, bail without calling rf_CauseReconEvent().
   1855 	   There won't be anyone listening for this event anyway */
   1856 
   1857 	if (rbuf->raidPtr->reconControl == NULL)
   1858 		return;
   1859 
   1860 	if (status) {
   1861 		printf("raid%d: Forced recon read failed!\n", rbuf->raidPtr->raidid);
   1862 		rf_CauseReconEvent(rbuf->raidPtr, rbuf->col, (void *) rbuf, RF_REVENT_FORCEDREAD_FAILED);
   1863 		return;
   1864 	}
   1865 	rf_CauseReconEvent(rbuf->raidPtr, rbuf->col, (void *) rbuf, RF_REVENT_FORCEDREADDONE);
   1866 }
   1867 /* releases a block on the reconstruction of the indicated stripe */
   1868 int
   1869 rf_UnblockRecon(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap)
   1870 {
   1871 	RF_StripeNum_t stripeID = asmap->stripeID;
   1872 	RF_ReconParityStripeStatus_t *pssPtr;
   1873 	RF_ReconUnitNum_t which_ru;
   1874 	RF_StripeNum_t psid;
   1875 	RF_CallbackDesc_t *cb;
   1876 
   1877 	psid = rf_MapStripeIDToParityStripeID(&raidPtr->Layout, stripeID, &which_ru);
   1878 	RF_LOCK_PSS_MUTEX(raidPtr, psid);
   1879 	pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl->pssTable, psid, which_ru, RF_PSS_NONE, NULL);
   1880 
   1881 	/* When recon is forced, the pss desc can get deleted before we get
   1882 	 * back to unblock recon. But, this can _only_ happen when recon is
   1883 	 * forced. It would be good to put some kind of sanity check here, but
   1884 	 * how to decide if recon was just forced or not? */
   1885 	if (!pssPtr) {
   1886 		/* printf("Warning: no pss descriptor upon unblock on psid %ld
   1887 		 * RU %d\n",psid,which_ru); */
   1888 #if (RF_DEBUG_RECON > 0) || (RF_DEBUG_PSS > 0)
   1889 		if (rf_reconDebug || rf_pssDebug)
   1890 			printf("Warning: no pss descriptor upon unblock on psid %ld RU %d\n", (long) psid, which_ru);
   1891 #endif
   1892 		goto out;
   1893 	}
   1894 	pssPtr->blockCount--;
   1895 	Dprintf3("raid%d: unblocking recon on psid %ld: blockcount is %d\n",
   1896 		 raidPtr->raidid, psid, pssPtr->blockCount);
   1897 	if (pssPtr->blockCount == 0) {	/* if recon blockage has been released */
   1898 
   1899 		/* unblock recon before calling CauseReconEvent in case
   1900 		 * CauseReconEvent causes us to try to issue a new read before
   1901 		 * returning here. */
   1902 		pssPtr->flags &= ~RF_PSS_RECON_BLOCKED;
   1903 
   1904 
   1905 		while (pssPtr->blockWaitList) {
   1906 			/* spin through the block-wait list and
   1907 			   release all the waiters */
   1908 			cb = pssPtr->blockWaitList;
   1909 			pssPtr->blockWaitList = cb->next;
   1910 			cb->next = NULL;
   1911 			rf_CauseReconEvent(raidPtr, cb->col, NULL, RF_REVENT_BLOCKCLEAR);
   1912 			rf_FreeCallbackDesc(cb);
   1913 		}
   1914 		if (!(pssPtr->flags & RF_PSS_UNDER_RECON)) {
   1915 			/* if no recon was requested while recon was blocked */
   1916 			rf_PSStatusDelete(raidPtr, raidPtr->reconControl->pssTable, pssPtr);
   1917 		}
   1918 	}
   1919 out:
   1920 	RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
   1921 	return (0);
   1922 }
   1923 
   1924 void
   1925 rf_WakeupHeadSepCBWaiters(RF_Raid_t *raidPtr)
   1926 {
   1927 	RF_CallbackDesc_t *p;
   1928 
   1929 	RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1930 	while(raidPtr->reconControl->rb_lock) {
   1931 		ltsleep(&raidPtr->reconControl->rb_lock, PRIBIO,
   1932 			"rf_wakeuphscbw", 0, &raidPtr->reconControl->rb_mutex);
   1933 	}
   1934 
   1935 	raidPtr->reconControl->rb_lock = 1;
   1936 	RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1937 
   1938 	while (raidPtr->reconControl->headSepCBList) {
   1939 		p = raidPtr->reconControl->headSepCBList;
   1940 		raidPtr->reconControl->headSepCBList = p->next;
   1941 		p->next = NULL;
   1942 		rf_CauseReconEvent(raidPtr, p->col, NULL, RF_REVENT_HEADSEPCLEAR);
   1943 		rf_FreeCallbackDesc(p);
   1944 	}
   1945 	RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1946 	raidPtr->reconControl->rb_lock = 0;
   1947 	wakeup(&raidPtr->reconControl->rb_lock);
   1948 	RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
   1949 
   1950 }
   1951 
   1952