Home | History | Annotate | Line # | Download | only in raidframe
rf_reconstruct.c revision 1.66
      1 /*	$NetBSD: rf_reconstruct.c,v 1.66 2004/02/29 04:03:50 oster 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.66 2004/02/29 04:03:50 oster Exp $");
     37 
     38 #include <sys/time.h>
     39 #include <sys/buf.h>
     40 #include <sys/errno.h>
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/proc.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/fcntl.h>
     47 #include <sys/vnode.h>
     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 
     98 static struct pool rf_recond_pool;
     99 #define RF_MAX_FREE_RECOND  4
    100 #define RF_RECOND_INC       1
    101 
    102 static RF_RaidReconDesc_t *
    103 AllocRaidReconDesc(RF_Raid_t * raidPtr,
    104     RF_RowCol_t col, RF_RaidDisk_t * spareDiskPtr,
    105     int numDisksDone, RF_RowCol_t scol);
    106 static void FreeReconDesc(RF_RaidReconDesc_t * reconDesc);
    107 static int
    108 ProcessReconEvent(RF_Raid_t * raidPtr, RF_ReconEvent_t * event);
    109 static int
    110 IssueNextReadRequest(RF_Raid_t * raidPtr, RF_RowCol_t col);
    111 static int TryToRead(RF_Raid_t * raidPtr, RF_RowCol_t col);
    112 static int
    113 ComputePSDiskOffsets(RF_Raid_t * raidPtr, RF_StripeNum_t psid,
    114     RF_RowCol_t col, RF_SectorNum_t * outDiskOffset,
    115     RF_SectorNum_t * outFailedDiskSectorOffset,
    116     RF_RowCol_t * spCol, RF_SectorNum_t * spOffset);
    117 static int IssueNextWriteRequest(RF_Raid_t * raidPtr);
    118 static int ReconReadDoneProc(void *arg, int status);
    119 static int ReconWriteDoneProc(void *arg, int status);
    120 static void
    121 CheckForNewMinHeadSep(RF_Raid_t * raidPtr, RF_HeadSepLimit_t hsCtr);
    122 static int
    123 CheckHeadSeparation(RF_Raid_t * raidPtr, RF_PerDiskReconCtrl_t * ctrl,
    124     RF_RowCol_t col, RF_HeadSepLimit_t hsCtr,
    125     RF_ReconUnitNum_t which_ru);
    126 static int
    127 CheckForcedOrBlockedReconstruction(RF_Raid_t * raidPtr,
    128     RF_ReconParityStripeStatus_t * pssPtr, RF_PerDiskReconCtrl_t * ctrl,
    129     RF_RowCol_t col, RF_StripeNum_t psid,
    130     RF_ReconUnitNum_t which_ru);
    131 static void ForceReconReadDoneProc(void *arg, int status);
    132 
    133 static void rf_ShutdownReconstruction(void *);
    134 
    135 struct RF_ReconDoneProc_s {
    136 	void    (*proc) (RF_Raid_t *, void *);
    137 	void   *arg;
    138 	RF_ReconDoneProc_t *next;
    139 };
    140 
    141 /**************************************************************************
    142  *
    143  * sets up the parameters that will be used by the reconstruction process
    144  * currently there are none, except for those that the layout-specific
    145  * configuration (e.g. rf_ConfigureDeclustered) routine sets up.
    146  *
    147  * in the kernel, we fire off the recon thread.
    148  *
    149  **************************************************************************/
    150 static void
    151 rf_ShutdownReconstruction(void *ignored)
    152 {
    153 	pool_destroy(&rf_recond_pool);
    154 }
    155 
    156 int
    157 rf_ConfigureReconstruction(RF_ShutdownList_t **listp)
    158 {
    159 
    160 	pool_init(&rf_recond_pool, sizeof(RF_RaidReconDesc_t), 0, 0, 0,
    161 		  "rf_recond_pl", NULL);
    162 	rf_ShutdownCreate(listp, rf_ShutdownReconstruction, NULL);
    163 
    164 	return (0);
    165 }
    166 
    167 static RF_RaidReconDesc_t *
    168 AllocRaidReconDesc(RF_Raid_t *raidPtr, RF_RowCol_t col,
    169 		   RF_RaidDisk_t *spareDiskPtr, int numDisksDone,
    170 		   RF_RowCol_t scol)
    171 {
    172 
    173 	RF_RaidReconDesc_t *reconDesc;
    174 
    175 	reconDesc = pool_get(&rf_recond_pool, PR_WAITOK); /* XXX WAITOK?? */
    176 
    177 	reconDesc->raidPtr = raidPtr;
    178 	reconDesc->col = col;
    179 	reconDesc->spareDiskPtr = spareDiskPtr;
    180 	reconDesc->numDisksDone = numDisksDone;
    181 	reconDesc->scol = scol;
    182 	reconDesc->state = 0;
    183 	reconDesc->next = NULL;
    184 
    185 	return (reconDesc);
    186 }
    187 
    188 static void
    189 FreeReconDesc(RF_RaidReconDesc_t *reconDesc)
    190 {
    191 #if RF_RECON_STATS > 0
    192 	printf("raid%d: %lu recon event waits, %lu recon delays\n",
    193 	       reconDesc->raidPtr->raidid,
    194 	       (long) reconDesc->numReconEventWaits,
    195 	       (long) reconDesc->numReconExecDelays);
    196 #endif				/* RF_RECON_STATS > 0 */
    197 	printf("raid%d: %lu max exec ticks\n",
    198 	       reconDesc->raidPtr->raidid,
    199 	       (long) reconDesc->maxReconExecTicks);
    200 #if (RF_RECON_STATS > 0) || defined(KERNEL)
    201 	printf("\n");
    202 #endif				/* (RF_RECON_STATS > 0) || KERNEL */
    203 	pool_put(&rf_recond_pool, reconDesc);
    204 }
    205 
    206 
    207 /*****************************************************************************
    208  *
    209  * primary routine to reconstruct a failed disk.  This should be called from
    210  * within its own thread.  It won't return until reconstruction completes,
    211  * fails, or is aborted.
    212  *****************************************************************************/
    213 int
    214 rf_ReconstructFailedDisk(RF_Raid_t *raidPtr, RF_RowCol_t col)
    215 {
    216 	const RF_LayoutSW_t *lp;
    217 	int     rc;
    218 
    219 	lp = raidPtr->Layout.map;
    220 	if (lp->SubmitReconBuffer) {
    221 		/*
    222 	         * The current infrastructure only supports reconstructing one
    223 	         * disk at a time for each array.
    224 	         */
    225 		RF_LOCK_MUTEX(raidPtr->mutex);
    226 		while (raidPtr->reconInProgress) {
    227 			RF_WAIT_COND(raidPtr->waitForReconCond, raidPtr->mutex);
    228 		}
    229 		raidPtr->reconInProgress++;
    230 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    231 		rc = rf_ReconstructFailedDiskBasic(raidPtr, col);
    232 		RF_LOCK_MUTEX(raidPtr->mutex);
    233 		raidPtr->reconInProgress--;
    234 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    235 	} else {
    236 		RF_ERRORMSG1("RECON: no way to reconstruct failed disk for arch %c\n",
    237 		    lp->parityConfig);
    238 		rc = EIO;
    239 	}
    240 	RF_SIGNAL_COND(raidPtr->waitForReconCond);
    241 	return (rc);
    242 }
    243 
    244 int
    245 rf_ReconstructFailedDiskBasic(RF_Raid_t *raidPtr, RF_RowCol_t col)
    246 {
    247 	RF_ComponentLabel_t c_label;
    248 	RF_RaidDisk_t *spareDiskPtr = NULL;
    249 	RF_RaidReconDesc_t *reconDesc;
    250 	RF_RowCol_t scol;
    251 	int     numDisksDone = 0, rc;
    252 
    253 	/* first look for a spare drive onto which to reconstruct the data */
    254 	/* spare disk descriptors are stored in row 0.  This may have to
    255 	 * change eventually */
    256 
    257 	RF_LOCK_MUTEX(raidPtr->mutex);
    258 	RF_ASSERT(raidPtr->Disks[col].status == rf_ds_failed);
    259 
    260 	if (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE) {
    261 		if (raidPtr->status != rf_rs_degraded) {
    262 			RF_ERRORMSG1("Unable to reconstruct disk at col %d because status not degraded\n", col);
    263 			RF_UNLOCK_MUTEX(raidPtr->mutex);
    264 			return (EINVAL);
    265 		}
    266 		scol = (-1);
    267 	} else {
    268 		for (scol = raidPtr->numCol; scol < raidPtr->numCol + raidPtr->numSpare; scol++) {
    269 			if (raidPtr->Disks[scol].status == rf_ds_spare) {
    270 				spareDiskPtr = &raidPtr->Disks[scol];
    271 				spareDiskPtr->status = rf_ds_used_spare;
    272 				break;
    273 			}
    274 		}
    275 		if (!spareDiskPtr) {
    276 			RF_ERRORMSG1("Unable to reconstruct disk at col %d because no spares are available\n", col);
    277 			RF_UNLOCK_MUTEX(raidPtr->mutex);
    278 			return (ENOSPC);
    279 		}
    280 		printf("RECON: initiating reconstruction on col %d -> spare at col %d\n", col, scol);
    281 	}
    282 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    283 
    284 	reconDesc = AllocRaidReconDesc((void *) raidPtr, col, spareDiskPtr, numDisksDone, scol);
    285 	raidPtr->reconDesc = (void *) reconDesc;
    286 #if RF_RECON_STATS > 0
    287 	reconDesc->hsStallCount = 0;
    288 	reconDesc->numReconExecDelays = 0;
    289 	reconDesc->numReconEventWaits = 0;
    290 #endif				/* RF_RECON_STATS > 0 */
    291 	reconDesc->reconExecTimerRunning = 0;
    292 	reconDesc->reconExecTicks = 0;
    293 	reconDesc->maxReconExecTicks = 0;
    294 	rc = rf_ContinueReconstructFailedDisk(reconDesc);
    295 
    296 	if (!rc) {
    297 		/* fix up the component label */
    298 		/* Don't actually need the read here.. */
    299 		raidread_component_label(
    300                         raidPtr->raid_cinfo[scol].ci_dev,
    301 			raidPtr->raid_cinfo[scol].ci_vp,
    302 			&c_label);
    303 
    304 		raid_init_component_label( raidPtr, &c_label);
    305 		c_label.row = 0;
    306 		c_label.column = col;
    307 		c_label.clean = RF_RAID_DIRTY;
    308 		c_label.status = rf_ds_optimal;
    309 		c_label.partitionSize = raidPtr->Disks[scol].partitionSize;
    310 
    311 		/* We've just done a rebuild based on all the other
    312 		   disks, so at this point the parity is known to be
    313 		   clean, even if it wasn't before. */
    314 
    315 		/* XXX doesn't hold for RAID 6!!*/
    316 
    317 		RF_LOCK_MUTEX(raidPtr->mutex);
    318 		raidPtr->parity_good = RF_RAID_CLEAN;
    319 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    320 
    321 		/* XXXX MORE NEEDED HERE */
    322 
    323 		raidwrite_component_label(
    324                         raidPtr->raid_cinfo[scol].ci_dev,
    325 			raidPtr->raid_cinfo[scol].ci_vp,
    326 			&c_label);
    327 
    328 
    329 		rf_update_component_labels(raidPtr,
    330 					   RF_NORMAL_COMPONENT_UPDATE);
    331 
    332 	}
    333 	return (rc);
    334 }
    335 
    336 /*
    337 
    338    Allow reconstructing a disk in-place -- i.e. component /dev/sd2e goes AWOL,
    339    and you don't get a spare until the next Monday.  With this function
    340    (and hot-swappable drives) you can now put your new disk containing
    341    /dev/sd2e on the bus, scsictl it alive, and then use raidctl(8) to
    342    rebuild the data "on the spot".
    343 
    344 */
    345 
    346 int
    347 rf_ReconstructInPlace(RF_Raid_t *raidPtr, RF_RowCol_t col)
    348 {
    349 	RF_RaidDisk_t *spareDiskPtr = NULL;
    350 	RF_RaidReconDesc_t *reconDesc;
    351 	const RF_LayoutSW_t *lp;
    352 	RF_ComponentLabel_t c_label;
    353 	int     numDisksDone = 0, rc;
    354 	struct partinfo dpart;
    355 	struct vnode *vp;
    356 	struct vattr va;
    357 	struct proc *proc;
    358 	int retcode;
    359 	int ac;
    360 
    361 	lp = raidPtr->Layout.map;
    362 	if (!lp->SubmitReconBuffer) {
    363 		RF_ERRORMSG1("RECON: no way to reconstruct failed disk for arch %c\n",
    364 			     lp->parityConfig);
    365 		/* wakeup anyone who might be waiting to do a reconstruct */
    366 		RF_SIGNAL_COND(raidPtr->waitForReconCond);
    367 		return(EIO);
    368 	}
    369 
    370 	/*
    371 	 * The current infrastructure only supports reconstructing one
    372 	 * disk at a time for each array.
    373 	 */
    374 	RF_LOCK_MUTEX(raidPtr->mutex);
    375 
    376 	if (raidPtr->Disks[col].status != rf_ds_failed) {
    377 		/* "It's gone..." */
    378 		raidPtr->numFailures++;
    379 		raidPtr->Disks[col].status = rf_ds_failed;
    380 		raidPtr->status = rf_rs_degraded;
    381 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    382 		rf_update_component_labels(raidPtr,
    383 					   RF_NORMAL_COMPONENT_UPDATE);
    384 		RF_LOCK_MUTEX(raidPtr->mutex);
    385 	}
    386 
    387 	while (raidPtr->reconInProgress) {
    388 		RF_WAIT_COND(raidPtr->waitForReconCond, raidPtr->mutex);
    389 	}
    390 
    391 	raidPtr->reconInProgress++;
    392 
    393 	/* first look for a spare drive onto which to reconstruct the
    394 	   data.  spare disk descriptors are stored in row 0.  This
    395 	   may have to change eventually */
    396 
    397 	/* Actually, we don't care if it's failed or not...  On a RAID
    398 	   set with correct parity, this function should be callable
    399 	   on any component without ill affects. */
    400 	/* RF_ASSERT(raidPtr->Disks[col].status == rf_ds_failed); */
    401 
    402 	if (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE) {
    403 		RF_ERRORMSG1("Unable to reconstruct to disk at col %d: operation not supported for RF_DISTRIBUTE_SPARE\n", col);
    404 
    405 		raidPtr->reconInProgress--;
    406 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    407 		RF_SIGNAL_COND(raidPtr->waitForReconCond);
    408 		return (EINVAL);
    409 	}
    410 
    411 	proc = raidPtr->engine_thread;
    412 
    413 	/* This device may have been opened successfully the
    414 	   first time. Close it before trying to open it again.. */
    415 
    416 	if (raidPtr->raid_cinfo[col].ci_vp != NULL) {
    417 #if 0
    418 		printf("Closed the open device: %s\n",
    419 		       raidPtr->Disks[col].devname);
    420 #endif
    421 		vp = raidPtr->raid_cinfo[col].ci_vp;
    422 		ac = raidPtr->Disks[col].auto_configured;
    423 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    424 		rf_close_component(raidPtr, vp, ac);
    425 		RF_LOCK_MUTEX(raidPtr->mutex);
    426 		raidPtr->raid_cinfo[col].ci_vp = NULL;
    427 	}
    428 	/* note that this disk was *not* auto_configured (any longer)*/
    429 	raidPtr->Disks[col].auto_configured = 0;
    430 
    431 #if 0
    432 	printf("About to (re-)open the device for rebuilding: %s\n",
    433 	       raidPtr->Disks[col].devname);
    434 #endif
    435 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    436 	retcode = raidlookup(raidPtr->Disks[col].devname, proc, &vp);
    437 
    438 	if (retcode) {
    439 		printf("raid%d: rebuilding: raidlookup on device: %s failed: %d!\n",raidPtr->raidid,
    440 		       raidPtr->Disks[col].devname, retcode);
    441 
    442 		/* the component isn't responding properly...
    443 		   must be still dead :-( */
    444 		RF_LOCK_MUTEX(raidPtr->mutex);
    445 		raidPtr->reconInProgress--;
    446 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    447 		RF_SIGNAL_COND(raidPtr->waitForReconCond);
    448 		return(retcode);
    449 	}
    450 
    451 	/* Ok, so we can at least do a lookup...
    452 	   How about actually getting a vp for it? */
    453 
    454 	if ((retcode = VOP_GETATTR(vp, &va, proc->p_ucred, proc)) != 0) {
    455 		RF_LOCK_MUTEX(raidPtr->mutex);
    456 		raidPtr->reconInProgress--;
    457 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    458 		RF_SIGNAL_COND(raidPtr->waitForReconCond);
    459 		return(retcode);
    460 	}
    461 
    462 	retcode = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, proc->p_ucred, proc);
    463 	if (retcode) {
    464 		RF_LOCK_MUTEX(raidPtr->mutex);
    465 		raidPtr->reconInProgress--;
    466 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    467 		RF_SIGNAL_COND(raidPtr->waitForReconCond);
    468 		return(retcode);
    469 	}
    470 	RF_LOCK_MUTEX(raidPtr->mutex);
    471 	raidPtr->Disks[col].blockSize =	dpart.disklab->d_secsize;
    472 
    473 	raidPtr->Disks[col].numBlocks = dpart.part->p_size -
    474 		rf_protectedSectors;
    475 
    476 	raidPtr->raid_cinfo[col].ci_vp = vp;
    477 	raidPtr->raid_cinfo[col].ci_dev = va.va_rdev;
    478 
    479 	raidPtr->Disks[col].dev = va.va_rdev;
    480 
    481 	/* we allow the user to specify that only a fraction
    482 	   of the disks should be used this is just for debug:
    483 	   it speeds up * the parity scan */
    484 	raidPtr->Disks[col].numBlocks = raidPtr->Disks[col].numBlocks *
    485 		rf_sizePercentage / 100;
    486 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    487 
    488 	spareDiskPtr = &raidPtr->Disks[col];
    489 	spareDiskPtr->status = rf_ds_used_spare;
    490 
    491 	printf("raid%d: initiating in-place reconstruction on column %d\n",
    492 	       raidPtr->raidid, col);
    493 
    494 	reconDesc = AllocRaidReconDesc((void *) raidPtr, col, spareDiskPtr,
    495 				       numDisksDone, col);
    496 	raidPtr->reconDesc = (void *) reconDesc;
    497 #if RF_RECON_STATS > 0
    498 	reconDesc->hsStallCount = 0;
    499 	reconDesc->numReconExecDelays = 0;
    500 	reconDesc->numReconEventWaits = 0;
    501 #endif				/* RF_RECON_STATS > 0 */
    502 	reconDesc->reconExecTimerRunning = 0;
    503 	reconDesc->reconExecTicks = 0;
    504 	reconDesc->maxReconExecTicks = 0;
    505 	rc = rf_ContinueReconstructFailedDisk(reconDesc);
    506 
    507 	RF_LOCK_MUTEX(raidPtr->mutex);
    508 	raidPtr->reconInProgress--;
    509 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    510 
    511 	if (!rc) {
    512 		RF_LOCK_MUTEX(raidPtr->mutex);
    513 		/* Need to set these here, as at this point it'll be claiming
    514 		   that the disk is in rf_ds_spared!  But we know better :-) */
    515 
    516 		raidPtr->Disks[col].status = rf_ds_optimal;
    517 		raidPtr->status = rf_rs_optimal;
    518 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    519 
    520 		/* fix up the component label */
    521 		/* Don't actually need the read here.. */
    522 		raidread_component_label(raidPtr->raid_cinfo[col].ci_dev,
    523 					 raidPtr->raid_cinfo[col].ci_vp,
    524 					 &c_label);
    525 
    526 		RF_LOCK_MUTEX(raidPtr->mutex);
    527 		raid_init_component_label(raidPtr, &c_label);
    528 
    529 		c_label.row = 0;
    530 		c_label.column = col;
    531 
    532 		/* We've just done a rebuild based on all the other
    533 		   disks, so at this point the parity is known to be
    534 		   clean, even if it wasn't before. */
    535 
    536 		/* XXX doesn't hold for RAID 6!!*/
    537 
    538 		raidPtr->parity_good = RF_RAID_CLEAN;
    539 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    540 
    541 		raidwrite_component_label(raidPtr->raid_cinfo[col].ci_dev,
    542 					  raidPtr->raid_cinfo[col].ci_vp,
    543 					  &c_label);
    544 
    545 		rf_update_component_labels(raidPtr,
    546 					   RF_NORMAL_COMPONENT_UPDATE);
    547 
    548 	}
    549 	RF_SIGNAL_COND(raidPtr->waitForReconCond);
    550 	return (rc);
    551 }
    552 
    553 
    554 int
    555 rf_ContinueReconstructFailedDisk(RF_RaidReconDesc_t *reconDesc)
    556 {
    557 	RF_Raid_t *raidPtr = reconDesc->raidPtr;
    558 	RF_RowCol_t col = reconDesc->col;
    559 	RF_RowCol_t scol = reconDesc->scol;
    560 	RF_ReconMap_t *mapPtr;
    561 	RF_ReconCtrl_t *tmp_reconctrl;
    562 	RF_ReconEvent_t *event;
    563 	struct timeval etime, elpsd;
    564 	unsigned long xor_s, xor_resid_us;
    565 	int     i, ds;
    566 
    567 	switch (reconDesc->state) {
    568 
    569 
    570 	case 0:
    571 
    572 		raidPtr->accumXorTimeUs = 0;
    573 
    574 		/* create one trace record per physical disk */
    575 		RF_Malloc(raidPtr->recon_tracerecs, raidPtr->numCol * sizeof(RF_AccTraceEntry_t), (RF_AccTraceEntry_t *));
    576 
    577 		/* quiesce the array prior to starting recon.  this is needed
    578 		 * to assure no nasty interactions with pending user writes.
    579 		 * We need to do this before we change the disk or row status. */
    580 		reconDesc->state = 1;
    581 
    582 		Dprintf("RECON: begin request suspend\n");
    583 		rf_SuspendNewRequestsAndWait(raidPtr);
    584 		Dprintf("RECON: end request suspend\n");
    585 		rf_StartUserStats(raidPtr);	/* zero out the stats kept on
    586 						 * user accs */
    587 
    588 		/* fall through to state 1 */
    589 
    590 	case 1:
    591 
    592 		/* allocate our RF_ReconCTRL_t before we protect raidPtr->reconControl[row] */
    593 		tmp_reconctrl = rf_MakeReconControl(reconDesc, col, scol);
    594 
    595 		RF_LOCK_MUTEX(raidPtr->mutex);
    596 
    597 		/* create the reconstruction control pointer and install it in
    598 		 * the right slot */
    599 		raidPtr->reconControl = tmp_reconctrl;
    600 		mapPtr = raidPtr->reconControl->reconMap;
    601 		raidPtr->status = rf_rs_reconstructing;
    602 		raidPtr->Disks[col].status = rf_ds_reconstructing;
    603 		raidPtr->Disks[col].spareCol = scol;
    604 
    605 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    606 
    607 		RF_GETTIME(raidPtr->reconControl->starttime);
    608 
    609 		/* now start up the actual reconstruction: issue a read for
    610 		 * each surviving disk */
    611 
    612 		reconDesc->numDisksDone = 0;
    613 		for (i = 0; i < raidPtr->numCol; i++) {
    614 			if (i != col) {
    615 				/* find and issue the next I/O on the
    616 				 * indicated disk */
    617 				if (IssueNextReadRequest(raidPtr, i)) {
    618 					Dprintf1("RECON: done issuing for c%d\n", i);
    619 					reconDesc->numDisksDone++;
    620 				}
    621 			}
    622 		}
    623 
    624 	case 2:
    625 		Dprintf("RECON: resume requests\n");
    626 		rf_ResumeNewRequests(raidPtr);
    627 
    628 
    629 		reconDesc->state = 3;
    630 
    631 	case 3:
    632 
    633 		/* process reconstruction events until all disks report that
    634 		 * they've completed all work */
    635 		mapPtr = raidPtr->reconControl->reconMap;
    636 
    637 
    638 
    639 		while (reconDesc->numDisksDone < raidPtr->numCol - 1) {
    640 
    641 			event = rf_GetNextReconEvent(reconDesc, (void (*) (void *)) rf_ContinueReconstructFailedDisk, reconDesc);
    642 			RF_ASSERT(event);
    643 
    644 			if (ProcessReconEvent(raidPtr, event))
    645 				reconDesc->numDisksDone++;
    646 			raidPtr->reconControl->numRUsTotal =
    647 				mapPtr->totalRUs;
    648 			raidPtr->reconControl->numRUsComplete =
    649 				mapPtr->totalRUs -
    650 				rf_UnitsLeftToReconstruct(mapPtr);
    651 #if RF_DEBUG_RECON
    652 			raidPtr->reconControl->percentComplete =
    653 				(raidPtr->reconControl->numRUsComplete * 100 / raidPtr->reconControl->numRUsTotal);
    654 			if (rf_prReconSched) {
    655 				rf_PrintReconSchedule(raidPtr->reconControl->reconMap, &(raidPtr->reconControl->starttime));
    656 			}
    657 #endif
    658 		}
    659 
    660 
    661 
    662 		reconDesc->state = 4;
    663 
    664 
    665 	case 4:
    666 		mapPtr = raidPtr->reconControl->reconMap;
    667 		if (rf_reconDebug) {
    668 			printf("RECON: all reads completed\n");
    669 		}
    670 		/* at this point all the reads have completed.  We now wait
    671 		 * for any pending writes to complete, and then we're done */
    672 
    673 		while (rf_UnitsLeftToReconstruct(raidPtr->reconControl->reconMap) > 0) {
    674 
    675 			event = rf_GetNextReconEvent(reconDesc, (void (*) (void *)) rf_ContinueReconstructFailedDisk, reconDesc);
    676 			RF_ASSERT(event);
    677 
    678 			(void) ProcessReconEvent(raidPtr, event);	/* ignore return code */
    679 #if RF_DEBUG_RECON
    680 			raidPtr->reconControl->percentComplete = 100 - (rf_UnitsLeftToReconstruct(mapPtr) * 100 / mapPtr->totalRUs);
    681 			if (rf_prReconSched) {
    682 				rf_PrintReconSchedule(raidPtr->reconControl->reconMap, &(raidPtr->reconControl->starttime));
    683 			}
    684 #endif
    685 		}
    686 		reconDesc->state = 5;
    687 
    688 	case 5:
    689 		/* Success:  mark the dead disk as reconstructed.  We quiesce
    690 		 * the array here to assure no nasty interactions with pending
    691 		 * user accesses when we free up the psstatus structure as
    692 		 * part of FreeReconControl() */
    693 
    694 		reconDesc->state = 6;
    695 
    696 		rf_SuspendNewRequestsAndWait(raidPtr);
    697 		rf_StopUserStats(raidPtr);
    698 		rf_PrintUserStats(raidPtr);	/* print out the stats on user
    699 						 * accs accumulated during
    700 						 * recon */
    701 
    702 		/* fall through to state 6 */
    703 	case 6:
    704 
    705 
    706 
    707 		RF_LOCK_MUTEX(raidPtr->mutex);
    708 		raidPtr->numFailures--;
    709 		ds = (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE);
    710 		raidPtr->Disks[col].status = (ds) ? rf_ds_dist_spared : rf_ds_spared;
    711 		raidPtr->status = (ds) ? rf_rs_reconfigured : rf_rs_optimal;
    712 		RF_UNLOCK_MUTEX(raidPtr->mutex);
    713 		RF_GETTIME(etime);
    714 		RF_TIMEVAL_DIFF(&(raidPtr->reconControl->starttime), &etime, &elpsd);
    715 
    716 		/* XXX -- why is state 7 different from state 6 if there is no
    717 		 * return() here? -- XXX Note that I set elpsd above & use it
    718 		 * below, so if you put a return here you'll have to fix this.
    719 		 * (also, FreeReconControl is called below) */
    720 
    721 	case 7:
    722 
    723 		rf_ResumeNewRequests(raidPtr);
    724 
    725 		printf("raid%d: Reconstruction of disk at col %d completed\n",
    726 		       raidPtr->raidid, col);
    727 		xor_s = raidPtr->accumXorTimeUs / 1000000;
    728 		xor_resid_us = raidPtr->accumXorTimeUs % 1000000;
    729 		printf("raid%d: Recon time was %d.%06d seconds, accumulated XOR time was %ld us (%ld.%06ld)\n",
    730 		       raidPtr->raidid,
    731 		       (int) elpsd.tv_sec, (int) elpsd.tv_usec,
    732 		       raidPtr->accumXorTimeUs, xor_s, xor_resid_us);
    733 		printf("raid%d:  (start time %d sec %d usec, end time %d sec %d usec)\n",
    734 		       raidPtr->raidid,
    735 		       (int) raidPtr->reconControl->starttime.tv_sec,
    736 		       (int) raidPtr->reconControl->starttime.tv_usec,
    737 		       (int) etime.tv_sec, (int) etime.tv_usec);
    738 
    739 #if RF_RECON_STATS > 0
    740 		printf("raid%d: Total head-sep stall count was %d\n",
    741 		       raidPtr->raidid, (int) reconDesc->hsStallCount);
    742 #endif				/* RF_RECON_STATS > 0 */
    743 		rf_FreeReconControl(raidPtr);
    744 		RF_Free(raidPtr->recon_tracerecs, raidPtr->numCol * sizeof(RF_AccTraceEntry_t));
    745 		FreeReconDesc(reconDesc);
    746 
    747 	}
    748 
    749 	return (0);
    750 }
    751 /*****************************************************************************
    752  * do the right thing upon each reconstruction event.
    753  * returns nonzero if and only if there is nothing left unread on the
    754  * indicated disk
    755  *****************************************************************************/
    756 static int
    757 ProcessReconEvent(RF_Raid_t *raidPtr, RF_ReconEvent_t *event)
    758 {
    759 	int     retcode = 0, submitblocked;
    760 	RF_ReconBuffer_t *rbuf;
    761 	RF_SectorCount_t sectorsPerRU;
    762 
    763 	Dprintf1("RECON: ProcessReconEvent type %d\n", event->type);
    764 	switch (event->type) {
    765 
    766 		/* a read I/O has completed */
    767 	case RF_REVENT_READDONE:
    768 		rbuf = raidPtr->reconControl->perDiskInfo[event->col].rbuf;
    769 		Dprintf2("RECON: READDONE EVENT: col %d psid %ld\n",
    770 		    event->col, rbuf->parityStripeID);
    771 		Dprintf7("RECON: done read  psid %ld buf %lx  %02x %02x %02x %02x %02x\n",
    772 		    rbuf->parityStripeID, rbuf->buffer, rbuf->buffer[0] & 0xff, rbuf->buffer[1] & 0xff,
    773 		    rbuf->buffer[2] & 0xff, rbuf->buffer[3] & 0xff, rbuf->buffer[4] & 0xff);
    774 		rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
    775 		submitblocked = rf_SubmitReconBuffer(rbuf, 0, 0);
    776 		Dprintf1("RECON: submitblocked=%d\n", submitblocked);
    777 		if (!submitblocked)
    778 			retcode = IssueNextReadRequest(raidPtr, event->col);
    779 		break;
    780 
    781 		/* a write I/O has completed */
    782 	case RF_REVENT_WRITEDONE:
    783 #if RF_DEBUG_RECON
    784 		if (rf_floatingRbufDebug) {
    785 			rf_CheckFloatingRbufCount(raidPtr, 1);
    786 		}
    787 #endif
    788 		sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU;
    789 		rbuf = (RF_ReconBuffer_t *) event->arg;
    790 		rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
    791 		Dprintf3("RECON: WRITEDONE EVENT: psid %d ru %d (%d %% complete)\n",
    792 		    rbuf->parityStripeID, rbuf->which_ru, raidPtr->reconControl->percentComplete);
    793 		rf_ReconMapUpdate(raidPtr, raidPtr->reconControl->reconMap,
    794 		    rbuf->failedDiskSectorOffset, rbuf->failedDiskSectorOffset + sectorsPerRU - 1);
    795 		rf_RemoveFromActiveReconTable(raidPtr, rbuf->parityStripeID, rbuf->which_ru);
    796 
    797 		if (rbuf->type == RF_RBUF_TYPE_FLOATING) {
    798 			RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
    799 			raidPtr->numFullReconBuffers--;
    800 			rf_ReleaseFloatingReconBuffer(raidPtr, rbuf);
    801 			RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
    802 		} else
    803 			if (rbuf->type == RF_RBUF_TYPE_FORCED)
    804 				rf_FreeReconBuffer(rbuf);
    805 			else
    806 				RF_ASSERT(0);
    807 		break;
    808 
    809 	case RF_REVENT_BUFCLEAR:	/* A buffer-stall condition has been
    810 					 * cleared */
    811 		Dprintf1("RECON: BUFCLEAR EVENT: col %d\n", event->col);
    812 		submitblocked = rf_SubmitReconBuffer(raidPtr->reconControl->perDiskInfo[event->col].rbuf, 0, (int) (long) event->arg);
    813 		RF_ASSERT(!submitblocked);	/* we wouldn't have gotten the
    814 						 * BUFCLEAR event if we
    815 						 * couldn't submit */
    816 		retcode = IssueNextReadRequest(raidPtr, event->col);
    817 		break;
    818 
    819 	case RF_REVENT_BLOCKCLEAR:	/* A user-write reconstruction
    820 					 * blockage has been cleared */
    821 		DDprintf1("RECON: BLOCKCLEAR EVENT: col %d\n", event->col);
    822 		retcode = TryToRead(raidPtr, event->col);
    823 		break;
    824 
    825 	case RF_REVENT_HEADSEPCLEAR:	/* A max-head-separation
    826 					 * reconstruction blockage has been
    827 					 * cleared */
    828 		Dprintf1("RECON: HEADSEPCLEAR EVENT: col %d\n", event->col);
    829 		retcode = TryToRead(raidPtr, event->col);
    830 		break;
    831 
    832 		/* a buffer has become ready to write */
    833 	case RF_REVENT_BUFREADY:
    834 		Dprintf1("RECON: BUFREADY EVENT: col %d\n", event->col);
    835 		retcode = IssueNextWriteRequest(raidPtr);
    836 #if RF_DEBUG_RECON
    837 		if (rf_floatingRbufDebug) {
    838 			rf_CheckFloatingRbufCount(raidPtr, 1);
    839 		}
    840 #endif
    841 		break;
    842 
    843 		/* we need to skip the current RU entirely because it got
    844 		 * recon'd while we were waiting for something else to happen */
    845 	case RF_REVENT_SKIP:
    846 		DDprintf1("RECON: SKIP EVENT: col %d\n", event->col);
    847 		retcode = IssueNextReadRequest(raidPtr, event->col);
    848 		break;
    849 
    850 		/* a forced-reconstruction read access has completed.  Just
    851 		 * submit the buffer */
    852 	case RF_REVENT_FORCEDREADDONE:
    853 		rbuf = (RF_ReconBuffer_t *) event->arg;
    854 		rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
    855 		DDprintf1("RECON: FORCEDREADDONE EVENT: col %d\n", event->col);
    856 		submitblocked = rf_SubmitReconBuffer(rbuf, 1, 0);
    857 		RF_ASSERT(!submitblocked);
    858 		break;
    859 
    860 	default:
    861 		RF_PANIC();
    862 	}
    863 	rf_FreeReconEventDesc(event);
    864 	return (retcode);
    865 }
    866 /*****************************************************************************
    867  *
    868  * find the next thing that's needed on the indicated disk, and issue
    869  * a read request for it.  We assume that the reconstruction buffer
    870  * associated with this process is free to receive the data.  If
    871  * reconstruction is blocked on the indicated RU, we issue a
    872  * blockage-release request instead of a physical disk read request.
    873  * If the current disk gets too far ahead of the others, we issue a
    874  * head-separation wait request and return.
    875  *
    876  * ctrl->{ru_count, curPSID, diskOffset} and
    877  * rbuf->failedDiskSectorOffset are maintained to point to the unit
    878  * we're currently accessing.  Note that this deviates from the
    879  * standard C idiom of having counters point to the next thing to be
    880  * accessed.  This allows us to easily retry when we're blocked by
    881  * head separation or reconstruction-blockage events.
    882  *
    883  * returns nonzero if and only if there is nothing left unread on the
    884  * indicated disk
    885  *
    886  *****************************************************************************/
    887 static int
    888 IssueNextReadRequest(RF_Raid_t *raidPtr, RF_RowCol_t col)
    889 {
    890 	RF_PerDiskReconCtrl_t *ctrl = &raidPtr->reconControl->perDiskInfo[col];
    891 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    892 	RF_ReconBuffer_t *rbuf = ctrl->rbuf;
    893 	RF_ReconUnitCount_t RUsPerPU = layoutPtr->SUsPerPU / layoutPtr->SUsPerRU;
    894 	RF_SectorCount_t sectorsPerRU = layoutPtr->sectorsPerStripeUnit * layoutPtr->SUsPerRU;
    895 	int     do_new_check = 0, retcode = 0, status;
    896 
    897 	/* if we are currently the slowest disk, mark that we have to do a new
    898 	 * check */
    899 	if (ctrl->headSepCounter <= raidPtr->reconControl->minHeadSepCounter)
    900 		do_new_check = 1;
    901 
    902 	while (1) {
    903 
    904 		ctrl->ru_count++;
    905 		if (ctrl->ru_count < RUsPerPU) {
    906 			ctrl->diskOffset += sectorsPerRU;
    907 			rbuf->failedDiskSectorOffset += sectorsPerRU;
    908 		} else {
    909 			ctrl->curPSID++;
    910 			ctrl->ru_count = 0;
    911 			/* code left over from when head-sep was based on
    912 			 * parity stripe id */
    913 			if (ctrl->curPSID >= raidPtr->reconControl->lastPSID) {
    914 				CheckForNewMinHeadSep(raidPtr, ++(ctrl->headSepCounter));
    915 				return (1);	/* finito! */
    916 			}
    917 			/* find the disk offsets of the start of the parity
    918 			 * stripe on both the current disk and the failed
    919 			 * disk. skip this entire parity stripe if either disk
    920 			 * does not appear in the indicated PS */
    921 			status = ComputePSDiskOffsets(raidPtr, ctrl->curPSID, col, &ctrl->diskOffset, &rbuf->failedDiskSectorOffset,
    922 			    &rbuf->spCol, &rbuf->spOffset);
    923 			if (status) {
    924 				ctrl->ru_count = RUsPerPU - 1;
    925 				continue;
    926 			}
    927 		}
    928 		rbuf->which_ru = ctrl->ru_count;
    929 
    930 		/* skip this RU if it's already been reconstructed */
    931 		if (rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, rbuf->failedDiskSectorOffset)) {
    932 			Dprintf2("Skipping psid %ld ru %d: already reconstructed\n", ctrl->curPSID, ctrl->ru_count);
    933 			continue;
    934 		}
    935 		break;
    936 	}
    937 	ctrl->headSepCounter++;
    938 	if (do_new_check)
    939 		CheckForNewMinHeadSep(raidPtr, ctrl->headSepCounter);	/* update min if needed */
    940 
    941 
    942 	/* at this point, we have definitely decided what to do, and we have
    943 	 * only to see if we can actually do it now */
    944 	rbuf->parityStripeID = ctrl->curPSID;
    945 	rbuf->which_ru = ctrl->ru_count;
    946 	memset((char *) &raidPtr->recon_tracerecs[col], 0,
    947 	    sizeof(raidPtr->recon_tracerecs[col]));
    948 	raidPtr->recon_tracerecs[col].reconacc = 1;
    949 	RF_ETIMER_START(raidPtr->recon_tracerecs[col].recon_timer);
    950 	retcode = TryToRead(raidPtr, col);
    951 	return (retcode);
    952 }
    953 
    954 /*
    955  * tries to issue the next read on the indicated disk.  We may be
    956  * blocked by (a) the heads being too far apart, or (b) recon on the
    957  * indicated RU being blocked due to a write by a user thread.  In
    958  * this case, we issue a head-sep or blockage wait request, which will
    959  * cause this same routine to be invoked again later when the blockage
    960  * has cleared.
    961  */
    962 
    963 static int
    964 TryToRead(RF_Raid_t *raidPtr, RF_RowCol_t col)
    965 {
    966 	RF_PerDiskReconCtrl_t *ctrl = &raidPtr->reconControl->perDiskInfo[col];
    967 	RF_SectorCount_t sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU;
    968 	RF_StripeNum_t psid = ctrl->curPSID;
    969 	RF_ReconUnitNum_t which_ru = ctrl->ru_count;
    970 	RF_DiskQueueData_t *req;
    971 	int     status, created = 0;
    972 	RF_ReconParityStripeStatus_t *pssPtr;
    973 
    974 	/* if the current disk is too far ahead of the others, issue a
    975 	 * head-separation wait and return */
    976 	if (CheckHeadSeparation(raidPtr, ctrl, col, ctrl->headSepCounter, which_ru))
    977 		return (0);
    978 	RF_LOCK_PSS_MUTEX(raidPtr, psid);
    979 	pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl->pssTable, psid, which_ru, RF_PSS_CREATE, &created);
    980 
    981 	/* if recon is blocked on the indicated parity stripe, issue a
    982 	 * block-wait request and return. this also must mark the indicated RU
    983 	 * in the stripe as under reconstruction if not blocked. */
    984 	status = CheckForcedOrBlockedReconstruction(raidPtr, pssPtr, ctrl, col, psid, which_ru);
    985 	if (status == RF_PSS_RECON_BLOCKED) {
    986 		Dprintf2("RECON: Stalling psid %ld ru %d: recon blocked\n", psid, which_ru);
    987 		goto out;
    988 	} else
    989 		if (status == RF_PSS_FORCED_ON_WRITE) {
    990 			rf_CauseReconEvent(raidPtr, col, NULL, RF_REVENT_SKIP);
    991 			goto out;
    992 		}
    993 	/* make one last check to be sure that the indicated RU didn't get
    994 	 * reconstructed while we were waiting for something else to happen.
    995 	 * This is unfortunate in that it causes us to make this check twice
    996 	 * in the normal case.  Might want to make some attempt to re-work
    997 	 * this so that we only do this check if we've definitely blocked on
    998 	 * one of the above checks.  When this condition is detected, we may
    999 	 * have just created a bogus status entry, which we need to delete. */
   1000 	if (rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, ctrl->rbuf->failedDiskSectorOffset)) {
   1001 		Dprintf2("RECON: Skipping psid %ld ru %d: prior recon after stall\n", psid, which_ru);
   1002 		if (created)
   1003 			rf_PSStatusDelete(raidPtr, raidPtr->reconControl->pssTable, pssPtr);
   1004 		rf_CauseReconEvent(raidPtr, col, NULL, RF_REVENT_SKIP);
   1005 		goto out;
   1006 	}
   1007 	/* found something to read.  issue the I/O */
   1008 	Dprintf4("RECON: Read for psid %ld on col %d offset %ld buf %lx\n",
   1009 	    psid, col, ctrl->diskOffset, ctrl->rbuf->buffer);
   1010 	RF_ETIMER_STOP(raidPtr->recon_tracerecs[col].recon_timer);
   1011 	RF_ETIMER_EVAL(raidPtr->recon_tracerecs[col].recon_timer);
   1012 	raidPtr->recon_tracerecs[col].specific.recon.recon_start_to_fetch_us =
   1013 	    RF_ETIMER_VAL_US(raidPtr->recon_tracerecs[col].recon_timer);
   1014 	RF_ETIMER_START(raidPtr->recon_tracerecs[col].recon_timer);
   1015 
   1016 	/* should be ok to use a NULL proc pointer here, all the bufs we use
   1017 	 * should be in kernel space */
   1018 	req = rf_CreateDiskQueueData(RF_IO_TYPE_READ, ctrl->diskOffset, sectorsPerRU, ctrl->rbuf->buffer, psid, which_ru,
   1019 	    ReconReadDoneProc, (void *) ctrl, NULL, &raidPtr->recon_tracerecs[col], (void *) raidPtr, 0, NULL);
   1020 
   1021 	RF_ASSERT(req);		/* XXX -- fix this -- XXX */
   1022 
   1023 	ctrl->rbuf->arg = (void *) req;
   1024 	rf_DiskIOEnqueue(&raidPtr->Queues[col], req, RF_IO_RECON_PRIORITY);
   1025 	pssPtr->issued[col] = 1;
   1026 
   1027 out:
   1028 	RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
   1029 	return (0);
   1030 }
   1031 
   1032 
   1033 /*
   1034  * given a parity stripe ID, we want to find out whether both the
   1035  * current disk and the failed disk exist in that parity stripe.  If
   1036  * not, we want to skip this whole PS.  If so, we want to find the
   1037  * disk offset of the start of the PS on both the current disk and the
   1038  * failed disk.
   1039  *
   1040  * this works by getting a list of disks comprising the indicated
   1041  * parity stripe, and searching the list for the current and failed
   1042  * disks.  Once we've decided they both exist in the parity stripe, we
   1043  * need to decide whether each is data or parity, so that we'll know
   1044  * which mapping function to call to get the corresponding disk
   1045  * offsets.
   1046  *
   1047  * this is kind of unpleasant, but doing it this way allows the
   1048  * reconstruction code to use parity stripe IDs rather than physical
   1049  * disks address to march through the failed disk, which greatly
   1050  * simplifies a lot of code, as well as eliminating the need for a
   1051  * reverse-mapping function.  I also think it will execute faster,
   1052  * since the calls to the mapping module are kept to a minimum.
   1053  *
   1054  * ASSUMES THAT THE STRIPE IDENTIFIER IDENTIFIES THE DISKS COMPRISING
   1055  * THE STRIPE IN THE CORRECT ORDER
   1056  *
   1057  * raidPtr          - raid descriptor
   1058  * psid             - parity stripe identifier
   1059  * col              - column of disk to find the offsets for
   1060  * spCol            - out: col of spare unit for failed unit
   1061  * spOffset         - out: offset into disk containing spare unit
   1062  *
   1063  */
   1064 
   1065 
   1066 static int
   1067 ComputePSDiskOffsets(RF_Raid_t *raidPtr, RF_StripeNum_t psid,
   1068 		     RF_RowCol_t col, RF_SectorNum_t *outDiskOffset,
   1069 		     RF_SectorNum_t *outFailedDiskSectorOffset,
   1070 		     RF_RowCol_t *spCol, RF_SectorNum_t *spOffset)
   1071 {
   1072 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
   1073 	RF_RowCol_t fcol = raidPtr->reconControl->fcol;
   1074 	RF_RaidAddr_t sosRaidAddress;	/* start-of-stripe */
   1075 	RF_RowCol_t *diskids;
   1076 	u_int   i, j, k, i_offset, j_offset;
   1077 	RF_RowCol_t pcol;
   1078 	int     testcol;
   1079 	RF_SectorNum_t poffset;
   1080 	char    i_is_parity = 0, j_is_parity = 0;
   1081 	RF_RowCol_t stripeWidth = layoutPtr->numDataCol + layoutPtr->numParityCol;
   1082 
   1083 	/* get a listing of the disks comprising that stripe */
   1084 	sosRaidAddress = rf_ParityStripeIDToRaidAddress(layoutPtr, psid);
   1085 	(layoutPtr->map->IdentifyStripe) (raidPtr, sosRaidAddress, &diskids);
   1086 	RF_ASSERT(diskids);
   1087 
   1088 	/* reject this entire parity stripe if it does not contain the
   1089 	 * indicated disk or it does not contain the failed disk */
   1090 
   1091 	for (i = 0; i < stripeWidth; i++) {
   1092 		if (col == diskids[i])
   1093 			break;
   1094 	}
   1095 	if (i == stripeWidth)
   1096 		goto skipit;
   1097 	for (j = 0; j < stripeWidth; j++) {
   1098 		if (fcol == diskids[j])
   1099 			break;
   1100 	}
   1101 	if (j == stripeWidth) {
   1102 		goto skipit;
   1103 	}
   1104 	/* find out which disk the parity is on */
   1105 	(layoutPtr->map->MapParity) (raidPtr, sosRaidAddress, &pcol, &poffset, RF_DONT_REMAP);
   1106 
   1107 	/* find out if either the current RU or the failed RU is parity */
   1108 	/* also, if the parity occurs in this stripe prior to the data and/or
   1109 	 * failed col, we need to decrement i and/or j */
   1110 	for (k = 0; k < stripeWidth; k++)
   1111 		if (diskids[k] == pcol)
   1112 			break;
   1113 	RF_ASSERT(k < stripeWidth);
   1114 	i_offset = i;
   1115 	j_offset = j;
   1116 	if (k < i)
   1117 		i_offset--;
   1118 	else
   1119 		if (k == i) {
   1120 			i_is_parity = 1;
   1121 			i_offset = 0;
   1122 		}		/* set offsets to zero to disable multiply
   1123 				 * below */
   1124 	if (k < j)
   1125 		j_offset--;
   1126 	else
   1127 		if (k == j) {
   1128 			j_is_parity = 1;
   1129 			j_offset = 0;
   1130 		}
   1131 	/* at this point, [ij]_is_parity tells us whether the [current,failed]
   1132 	 * disk is parity at the start of this RU, and, if data, "[ij]_offset"
   1133 	 * tells us how far into the stripe the [current,failed] disk is. */
   1134 
   1135 	/* call the mapping routine to get the offset into the current disk,
   1136 	 * repeat for failed disk. */
   1137 	if (i_is_parity)
   1138 		layoutPtr->map->MapParity(raidPtr, sosRaidAddress + i_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outDiskOffset, RF_DONT_REMAP);
   1139 	else
   1140 		layoutPtr->map->MapSector(raidPtr, sosRaidAddress + i_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outDiskOffset, RF_DONT_REMAP);
   1141 
   1142 	RF_ASSERT(col == testcol);
   1143 
   1144 	if (j_is_parity)
   1145 		layoutPtr->map->MapParity(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outFailedDiskSectorOffset, RF_DONT_REMAP);
   1146 	else
   1147 		layoutPtr->map->MapSector(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outFailedDiskSectorOffset, RF_DONT_REMAP);
   1148 	RF_ASSERT(fcol == testcol);
   1149 
   1150 	/* now locate the spare unit for the failed unit */
   1151 	if (layoutPtr->map->flags & RF_DISTRIBUTE_SPARE) {
   1152 		if (j_is_parity)
   1153 			layoutPtr->map->MapParity(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, spCol, spOffset, RF_REMAP);
   1154 		else
   1155 			layoutPtr->map->MapSector(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, spCol, spOffset, RF_REMAP);
   1156 	} else {
   1157 		*spCol = raidPtr->reconControl->spareCol;
   1158 		*spOffset = *outFailedDiskSectorOffset;
   1159 	}
   1160 
   1161 	return (0);
   1162 
   1163 skipit:
   1164 	Dprintf2("RECON: Skipping psid %ld: nothing needed from r%d c%d\n",
   1165 	    psid, col);
   1166 	return (1);
   1167 }
   1168 /* this is called when a buffer has become ready to write to the replacement disk */
   1169 static int
   1170 IssueNextWriteRequest(RF_Raid_t *raidPtr)
   1171 {
   1172 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
   1173 	RF_SectorCount_t sectorsPerRU = layoutPtr->sectorsPerStripeUnit * layoutPtr->SUsPerRU;
   1174 	RF_RowCol_t fcol = raidPtr->reconControl->fcol;
   1175 	RF_ReconBuffer_t *rbuf;
   1176 	RF_DiskQueueData_t *req;
   1177 
   1178 	rbuf = rf_GetFullReconBuffer(raidPtr->reconControl);
   1179 	RF_ASSERT(rbuf);	/* there must be one available, or we wouldn't
   1180 				 * have gotten the event that sent us here */
   1181 	RF_ASSERT(rbuf->pssPtr);
   1182 
   1183 	rbuf->pssPtr->writeRbuf = rbuf;
   1184 	rbuf->pssPtr = NULL;
   1185 
   1186 	Dprintf6("RECON: New write (c %d offs %d) for psid %ld ru %d (failed disk offset %ld) buf %lx\n",
   1187 	    rbuf->spCol, rbuf->spOffset, rbuf->parityStripeID,
   1188 	    rbuf->which_ru, rbuf->failedDiskSectorOffset, rbuf->buffer);
   1189 	Dprintf6("RECON: new write psid %ld   %02x %02x %02x %02x %02x\n",
   1190 	    rbuf->parityStripeID, rbuf->buffer[0] & 0xff, rbuf->buffer[1] & 0xff,
   1191 	    rbuf->buffer[2] & 0xff, rbuf->buffer[3] & 0xff, rbuf->buffer[4] & 0xff);
   1192 
   1193 	/* should be ok to use a NULL b_proc here b/c all addrs should be in
   1194 	 * kernel space */
   1195 	req = rf_CreateDiskQueueData(RF_IO_TYPE_WRITE, rbuf->spOffset,
   1196 	    sectorsPerRU, rbuf->buffer,
   1197 	    rbuf->parityStripeID, rbuf->which_ru,
   1198 	    ReconWriteDoneProc, (void *) rbuf, NULL,
   1199 	    &raidPtr->recon_tracerecs[fcol],
   1200 	    (void *) raidPtr, 0, NULL);
   1201 
   1202 	RF_ASSERT(req);		/* XXX -- fix this -- XXX */
   1203 
   1204 	rbuf->arg = (void *) req;
   1205 	rf_DiskIOEnqueue(&raidPtr->Queues[rbuf->spCol], req, RF_IO_RECON_PRIORITY);
   1206 
   1207 	return (0);
   1208 }
   1209 
   1210 /*
   1211  * this gets called upon the completion of a reconstruction read
   1212  * operation the arg is a pointer to the per-disk reconstruction
   1213  * control structure for the process that just finished a read.
   1214  *
   1215  * called at interrupt context in the kernel, so don't do anything
   1216  * illegal here.
   1217  */
   1218 static int
   1219 ReconReadDoneProc(void *arg, int status)
   1220 {
   1221 	RF_PerDiskReconCtrl_t *ctrl = (RF_PerDiskReconCtrl_t *) arg;
   1222 	RF_Raid_t *raidPtr = ctrl->reconCtrl->reconDesc->raidPtr;
   1223 
   1224 	if (status) {
   1225 		/*
   1226 	         * XXX
   1227 	         */
   1228 		printf("Recon read failed!\n");
   1229 		RF_PANIC();
   1230 	}
   1231 	RF_ETIMER_STOP(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
   1232 	RF_ETIMER_EVAL(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
   1233 	raidPtr->recon_tracerecs[ctrl->col].specific.recon.recon_fetch_to_return_us =
   1234 	    RF_ETIMER_VAL_US(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
   1235 	RF_ETIMER_START(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
   1236 
   1237 	rf_CauseReconEvent(raidPtr, ctrl->col, NULL, RF_REVENT_READDONE);
   1238 	return (0);
   1239 }
   1240 /* this gets called upon the completion of a reconstruction write operation.
   1241  * the arg is a pointer to the rbuf that was just written
   1242  *
   1243  * called at interrupt context in the kernel, so don't do anything illegal here.
   1244  */
   1245 static int
   1246 ReconWriteDoneProc(void *arg, int status)
   1247 {
   1248 	RF_ReconBuffer_t *rbuf = (RF_ReconBuffer_t *) arg;
   1249 
   1250 	Dprintf2("Reconstruction completed on psid %ld ru %d\n", rbuf->parityStripeID, rbuf->which_ru);
   1251 	if (status) {
   1252 		printf("Recon write failed!\n");	/* fprintf(stderr,"Recon
   1253 							 * write failed!\n"); */
   1254 		RF_PANIC();
   1255 	}
   1256 	rf_CauseReconEvent((RF_Raid_t *) rbuf->raidPtr, rbuf->col, arg, RF_REVENT_WRITEDONE);
   1257 	return (0);
   1258 }
   1259 
   1260 
   1261 /*
   1262  * computes a new minimum head sep, and wakes up anyone who needs to
   1263  * be woken as a result
   1264  */
   1265 static void
   1266 CheckForNewMinHeadSep(RF_Raid_t *raidPtr, RF_HeadSepLimit_t hsCtr)
   1267 {
   1268 	RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl;
   1269 	RF_HeadSepLimit_t new_min;
   1270 	RF_RowCol_t i;
   1271 	RF_CallbackDesc_t *p;
   1272 	RF_ASSERT(hsCtr >= reconCtrlPtr->minHeadSepCounter);	/* from the definition
   1273 								 * of a minimum */
   1274 
   1275 
   1276 	RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1277 
   1278 	new_min = ~(1L << (8 * sizeof(long) - 1));	/* 0x7FFF....FFF */
   1279 	for (i = 0; i < raidPtr->numCol; i++)
   1280 		if (i != reconCtrlPtr->fcol) {
   1281 			if (reconCtrlPtr->perDiskInfo[i].headSepCounter < new_min)
   1282 				new_min = reconCtrlPtr->perDiskInfo[i].headSepCounter;
   1283 		}
   1284 	/* set the new minimum and wake up anyone who can now run again */
   1285 	if (new_min != reconCtrlPtr->minHeadSepCounter) {
   1286 		reconCtrlPtr->minHeadSepCounter = new_min;
   1287 		Dprintf1("RECON:  new min head pos counter val is %ld\n", new_min);
   1288 		while (reconCtrlPtr->headSepCBList) {
   1289 			if (reconCtrlPtr->headSepCBList->callbackArg.v > new_min)
   1290 				break;
   1291 			p = reconCtrlPtr->headSepCBList;
   1292 			reconCtrlPtr->headSepCBList = p->next;
   1293 			p->next = NULL;
   1294 			rf_CauseReconEvent(raidPtr, p->col, NULL, RF_REVENT_HEADSEPCLEAR);
   1295 			rf_FreeCallbackDesc(p);
   1296 		}
   1297 
   1298 	}
   1299 	RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1300 }
   1301 
   1302 /*
   1303  * checks to see that the maximum head separation will not be violated
   1304  * if we initiate a reconstruction I/O on the indicated disk.
   1305  * Limiting the maximum head separation between two disks eliminates
   1306  * the nasty buffer-stall conditions that occur when one disk races
   1307  * ahead of the others and consumes all of the floating recon buffers.
   1308  * This code is complex and unpleasant but it's necessary to avoid
   1309  * some very nasty, albeit fairly rare, reconstruction behavior.
   1310  *
   1311  * returns non-zero if and only if we have to stop working on the
   1312  * indicated disk due to a head-separation delay.
   1313  */
   1314 static int
   1315 CheckHeadSeparation(RF_Raid_t *raidPtr, RF_PerDiskReconCtrl_t *ctrl,
   1316 		    RF_RowCol_t col, RF_HeadSepLimit_t hsCtr,
   1317 		    RF_ReconUnitNum_t which_ru)
   1318 {
   1319 	RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl;
   1320 	RF_CallbackDesc_t *cb, *p, *pt;
   1321 	int     retval = 0;
   1322 
   1323 	/* if we're too far ahead of the slowest disk, stop working on this
   1324 	 * disk until the slower ones catch up.  We do this by scheduling a
   1325 	 * wakeup callback for the time when the slowest disk has caught up.
   1326 	 * We define "caught up" with 20% hysteresis, i.e. the head separation
   1327 	 * must have fallen to at most 80% of the max allowable head
   1328 	 * separation before we'll wake up.
   1329 	 *
   1330 	 */
   1331 	RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1332 	if ((raidPtr->headSepLimit >= 0) &&
   1333 	    ((ctrl->headSepCounter - reconCtrlPtr->minHeadSepCounter) > raidPtr->headSepLimit)) {
   1334 		Dprintf5("raid%d: RECON: head sep stall: col %d hsCtr %ld minHSCtr %ld limit %ld\n",
   1335 			 raidPtr->raidid, col, ctrl->headSepCounter,
   1336 			 reconCtrlPtr->minHeadSepCounter,
   1337 			 raidPtr->headSepLimit);
   1338 		cb = rf_AllocCallbackDesc();
   1339 		/* the minHeadSepCounter value we have to get to before we'll
   1340 		 * wake up.  build in 20% hysteresis. */
   1341 		cb->callbackArg.v = (ctrl->headSepCounter - raidPtr->headSepLimit + raidPtr->headSepLimit / 5);
   1342 		cb->col = col;
   1343 		cb->next = NULL;
   1344 
   1345 		/* insert this callback descriptor into the sorted list of
   1346 		 * pending head-sep callbacks */
   1347 		p = reconCtrlPtr->headSepCBList;
   1348 		if (!p)
   1349 			reconCtrlPtr->headSepCBList = cb;
   1350 		else
   1351 			if (cb->callbackArg.v < p->callbackArg.v) {
   1352 				cb->next = reconCtrlPtr->headSepCBList;
   1353 				reconCtrlPtr->headSepCBList = cb;
   1354 			} else {
   1355 				for (pt = p, p = p->next; p && (p->callbackArg.v < cb->callbackArg.v); pt = p, p = p->next);
   1356 				cb->next = p;
   1357 				pt->next = cb;
   1358 			}
   1359 		retval = 1;
   1360 #if RF_RECON_STATS > 0
   1361 		ctrl->reconCtrl->reconDesc->hsStallCount++;
   1362 #endif				/* RF_RECON_STATS > 0 */
   1363 	}
   1364 	RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1365 
   1366 	return (retval);
   1367 }
   1368 /*
   1369  * checks to see if reconstruction has been either forced or blocked
   1370  * by a user operation.  if forced, we skip this RU entirely.  else if
   1371  * blocked, put ourselves on the wait list.  else return 0.
   1372  *
   1373  * ASSUMES THE PSS MUTEX IS LOCKED UPON ENTRY
   1374  */
   1375 static int
   1376 CheckForcedOrBlockedReconstruction(RF_Raid_t *raidPtr,
   1377 				   RF_ReconParityStripeStatus_t *pssPtr,
   1378 				   RF_PerDiskReconCtrl_t *ctrl,
   1379 				   RF_RowCol_t col, RF_StripeNum_t psid,
   1380 				   RF_ReconUnitNum_t which_ru)
   1381 {
   1382 	RF_CallbackDesc_t *cb;
   1383 	int     retcode = 0;
   1384 
   1385 	if ((pssPtr->flags & RF_PSS_FORCED_ON_READ) || (pssPtr->flags & RF_PSS_FORCED_ON_WRITE))
   1386 		retcode = RF_PSS_FORCED_ON_WRITE;
   1387 	else
   1388 		if (pssPtr->flags & RF_PSS_RECON_BLOCKED) {
   1389 			Dprintf3("RECON: col %d blocked at psid %ld ru %d\n", col, psid, which_ru);
   1390 			cb = rf_AllocCallbackDesc();	/* append ourselves to
   1391 							 * the blockage-wait
   1392 							 * list */
   1393 			cb->col = col;
   1394 			cb->next = pssPtr->blockWaitList;
   1395 			pssPtr->blockWaitList = cb;
   1396 			retcode = RF_PSS_RECON_BLOCKED;
   1397 		}
   1398 	if (!retcode)
   1399 		pssPtr->flags |= RF_PSS_UNDER_RECON;	/* mark this RU as under
   1400 							 * reconstruction */
   1401 
   1402 	return (retcode);
   1403 }
   1404 /*
   1405  * if reconstruction is currently ongoing for the indicated stripeID,
   1406  * reconstruction is forced to completion and we return non-zero to
   1407  * indicate that the caller must wait.  If not, then reconstruction is
   1408  * blocked on the indicated stripe and the routine returns zero.  If
   1409  * and only if we return non-zero, we'll cause the cbFunc to get
   1410  * invoked with the cbArg when the reconstruction has completed.
   1411  */
   1412 int
   1413 rf_ForceOrBlockRecon(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap,
   1414 		     void (*cbFunc)(RF_Raid_t *, void *), void *cbArg)
   1415 {
   1416 	RF_StripeNum_t stripeID = asmap->stripeID;	/* the stripe ID we're
   1417 							 * forcing recon on */
   1418 	RF_SectorCount_t sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU;	/* num sects in one RU */
   1419 	RF_ReconParityStripeStatus_t *pssPtr;	/* a pointer to the parity
   1420 						 * stripe status structure */
   1421 	RF_StripeNum_t psid;	/* parity stripe id */
   1422 	RF_SectorNum_t offset, fd_offset;	/* disk offset, failed-disk
   1423 						 * offset */
   1424 	RF_RowCol_t *diskids;
   1425 	RF_ReconUnitNum_t which_ru;	/* RU within parity stripe */
   1426 	RF_RowCol_t fcol, diskno, i;
   1427 	RF_ReconBuffer_t *new_rbuf;	/* ptr to newly allocated rbufs */
   1428 	RF_DiskQueueData_t *req;/* disk I/O req to be enqueued */
   1429 	RF_CallbackDesc_t *cb;
   1430 	int     created = 0, nPromoted;
   1431 
   1432 	psid = rf_MapStripeIDToParityStripeID(&raidPtr->Layout, stripeID, &which_ru);
   1433 
   1434 	RF_LOCK_PSS_MUTEX(raidPtr, psid);
   1435 
   1436 	pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl->pssTable, psid, which_ru, RF_PSS_CREATE | RF_PSS_RECON_BLOCKED, &created);
   1437 
   1438 	/* if recon is not ongoing on this PS, just return */
   1439 	if (!(pssPtr->flags & RF_PSS_UNDER_RECON)) {
   1440 		RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
   1441 		return (0);
   1442 	}
   1443 	/* otherwise, we have to wait for reconstruction to complete on this
   1444 	 * RU. */
   1445 	/* In order to avoid waiting for a potentially large number of
   1446 	 * low-priority accesses to complete, we force a normal-priority (i.e.
   1447 	 * not low-priority) reconstruction on this RU. */
   1448 	if (!(pssPtr->flags & RF_PSS_FORCED_ON_WRITE) && !(pssPtr->flags & RF_PSS_FORCED_ON_READ)) {
   1449 		DDprintf1("Forcing recon on psid %ld\n", psid);
   1450 		pssPtr->flags |= RF_PSS_FORCED_ON_WRITE;	/* mark this RU as under
   1451 								 * forced recon */
   1452 		pssPtr->flags &= ~RF_PSS_RECON_BLOCKED;	/* clear the blockage
   1453 							 * that we just set */
   1454 		fcol = raidPtr->reconControl->fcol;
   1455 
   1456 		/* get a listing of the disks comprising the indicated stripe */
   1457 		(raidPtr->Layout.map->IdentifyStripe) (raidPtr, asmap->raidAddress, &diskids);
   1458 
   1459 		/* For previously issued reads, elevate them to normal
   1460 		 * priority.  If the I/O has already completed, it won't be
   1461 		 * found in the queue, and hence this will be a no-op. For
   1462 		 * unissued reads, allocate buffers and issue new reads.  The
   1463 		 * fact that we've set the FORCED bit means that the regular
   1464 		 * recon procs will not re-issue these reqs */
   1465 		for (i = 0; i < raidPtr->Layout.numDataCol + raidPtr->Layout.numParityCol; i++)
   1466 			if ((diskno = diskids[i]) != fcol) {
   1467 				if (pssPtr->issued[diskno]) {
   1468 					nPromoted = rf_DiskIOPromote(&raidPtr->Queues[diskno], psid, which_ru);
   1469 					if (rf_reconDebug && nPromoted)
   1470 						printf("raid%d: promoted read from col %d\n", raidPtr->raidid, diskno);
   1471 				} else {
   1472 					new_rbuf = rf_MakeReconBuffer(raidPtr, diskno, RF_RBUF_TYPE_FORCED);	/* create new buf */
   1473 					ComputePSDiskOffsets(raidPtr, psid, diskno, &offset, &fd_offset,
   1474 					    &new_rbuf->spCol, &new_rbuf->spOffset);	/* find offsets & spare
   1475 													 * location */
   1476 					new_rbuf->parityStripeID = psid;	/* fill in the buffer */
   1477 					new_rbuf->which_ru = which_ru;
   1478 					new_rbuf->failedDiskSectorOffset = fd_offset;
   1479 					new_rbuf->priority = RF_IO_NORMAL_PRIORITY;
   1480 
   1481 					/* use NULL b_proc b/c all addrs
   1482 					 * should be in kernel space */
   1483 					req = rf_CreateDiskQueueData(RF_IO_TYPE_READ, offset + which_ru * sectorsPerRU, sectorsPerRU, new_rbuf->buffer,
   1484 					    psid, which_ru, (int (*) (void *, int)) ForceReconReadDoneProc, (void *) new_rbuf, NULL,
   1485 					    NULL, (void *) raidPtr, 0, NULL);
   1486 
   1487 					RF_ASSERT(req);	/* XXX -- fix this --
   1488 							 * XXX */
   1489 
   1490 					new_rbuf->arg = req;
   1491 					rf_DiskIOEnqueue(&raidPtr->Queues[diskno], req, RF_IO_NORMAL_PRIORITY);	/* enqueue the I/O */
   1492 					Dprintf2("raid%d: Issued new read req on col %d\n", raidPtr->raidid, diskno);
   1493 				}
   1494 			}
   1495 		/* if the write is sitting in the disk queue, elevate its
   1496 		 * priority */
   1497 		if (rf_DiskIOPromote(&raidPtr->Queues[fcol], psid, which_ru))
   1498 			printf("raid%d: promoted write to col %d\n",
   1499 			       raidPtr->raidid, fcol);
   1500 	}
   1501 	/* install a callback descriptor to be invoked when recon completes on
   1502 	 * this parity stripe. */
   1503 	cb = rf_AllocCallbackDesc();
   1504 	/* XXX the following is bogus.. These functions don't really match!!
   1505 	 * GO */
   1506 	cb->callbackFunc = (void (*) (RF_CBParam_t)) cbFunc;
   1507 	cb->callbackArg.p = (void *) cbArg;
   1508 	cb->next = pssPtr->procWaitList;
   1509 	pssPtr->procWaitList = cb;
   1510 	DDprintf2("raid%d: Waiting for forced recon on psid %ld\n",
   1511 		  raidPtr->raidid, psid);
   1512 
   1513 	RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
   1514 	return (1);
   1515 }
   1516 /* called upon the completion of a forced reconstruction read.
   1517  * all we do is schedule the FORCEDREADONE event.
   1518  * called at interrupt context in the kernel, so don't do anything illegal here.
   1519  */
   1520 static void
   1521 ForceReconReadDoneProc(void *arg, int status)
   1522 {
   1523 	RF_ReconBuffer_t *rbuf = arg;
   1524 
   1525 	if (status) {
   1526 		printf("Forced recon read failed!\n");	/* fprintf(stderr,"Forced
   1527 							 *  recon read
   1528 							 * failed!\n"); */
   1529 		RF_PANIC();
   1530 	}
   1531 	rf_CauseReconEvent((RF_Raid_t *) rbuf->raidPtr, rbuf->col, (void *) rbuf, RF_REVENT_FORCEDREADDONE);
   1532 }
   1533 /* releases a block on the reconstruction of the indicated stripe */
   1534 int
   1535 rf_UnblockRecon(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap)
   1536 {
   1537 	RF_StripeNum_t stripeID = asmap->stripeID;
   1538 	RF_ReconParityStripeStatus_t *pssPtr;
   1539 	RF_ReconUnitNum_t which_ru;
   1540 	RF_StripeNum_t psid;
   1541 	int     created = 0;
   1542 	RF_CallbackDesc_t *cb;
   1543 
   1544 	psid = rf_MapStripeIDToParityStripeID(&raidPtr->Layout, stripeID, &which_ru);
   1545 	RF_LOCK_PSS_MUTEX(raidPtr, psid);
   1546 	pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl->pssTable, psid, which_ru, RF_PSS_NONE, &created);
   1547 
   1548 	/* When recon is forced, the pss desc can get deleted before we get
   1549 	 * back to unblock recon. But, this can _only_ happen when recon is
   1550 	 * forced. It would be good to put some kind of sanity check here, but
   1551 	 * how to decide if recon was just forced or not? */
   1552 	if (!pssPtr) {
   1553 		/* printf("Warning: no pss descriptor upon unblock on psid %ld
   1554 		 * RU %d\n",psid,which_ru); */
   1555 #if (RF_DEBUG_RECON > 0) || (RF_DEBUG_PSS > 0)
   1556 		if (rf_reconDebug || rf_pssDebug)
   1557 			printf("Warning: no pss descriptor upon unblock on psid %ld RU %d\n", (long) psid, which_ru);
   1558 #endif
   1559 		goto out;
   1560 	}
   1561 	pssPtr->blockCount--;
   1562 	Dprintf3("raid%d: unblocking recon on psid %ld: blockcount is %d\n",
   1563 		 raidPtr->raidid, psid, pssPtr->blockCount);
   1564 	if (pssPtr->blockCount == 0) {	/* if recon blockage has been released */
   1565 
   1566 		/* unblock recon before calling CauseReconEvent in case
   1567 		 * CauseReconEvent causes us to try to issue a new read before
   1568 		 * returning here. */
   1569 		pssPtr->flags &= ~RF_PSS_RECON_BLOCKED;
   1570 
   1571 
   1572 		while (pssPtr->blockWaitList) {
   1573 			/* spin through the block-wait list and
   1574 			   release all the waiters */
   1575 			cb = pssPtr->blockWaitList;
   1576 			pssPtr->blockWaitList = cb->next;
   1577 			cb->next = NULL;
   1578 			rf_CauseReconEvent(raidPtr, cb->col, NULL, RF_REVENT_BLOCKCLEAR);
   1579 			rf_FreeCallbackDesc(cb);
   1580 		}
   1581 		if (!(pssPtr->flags & RF_PSS_UNDER_RECON)) {
   1582 			/* if no recon was requested while recon was blocked */
   1583 			rf_PSStatusDelete(raidPtr, raidPtr->reconControl->pssTable, pssPtr);
   1584 		}
   1585 	}
   1586 out:
   1587 	RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
   1588 	return (0);
   1589 }
   1590