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