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