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