Home | History | Annotate | Line # | Download | only in raidframe
rf_reconstruct.c revision 1.71
      1 /*	$NetBSD: rf_reconstruct.c,v 1.71 2004/03/03 17:14:46 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.71 2004/03/03 17:14:46 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 		/* A read I/O failed to complete */
    855 	case RF_REVENT_READ_FAILED:
    856 		/* fallthru to panic... */
    857 
    858 		/* A write I/O failed to complete */
    859 	case RF_REVENT_WRITE_FAILED:
    860 		/* fallthru to panic... */
    861 
    862 		/* a forced read I/O failed to complete */
    863 	case RF_REVENT_FORCEDREAD_FAILED:
    864 		/* fallthru to panic... */
    865 
    866 	default:
    867 		RF_PANIC();
    868 	}
    869 	rf_FreeReconEventDesc(event);
    870 	return (retcode);
    871 }
    872 /*****************************************************************************
    873  *
    874  * find the next thing that's needed on the indicated disk, and issue
    875  * a read request for it.  We assume that the reconstruction buffer
    876  * associated with this process is free to receive the data.  If
    877  * reconstruction is blocked on the indicated RU, we issue a
    878  * blockage-release request instead of a physical disk read request.
    879  * If the current disk gets too far ahead of the others, we issue a
    880  * head-separation wait request and return.
    881  *
    882  * ctrl->{ru_count, curPSID, diskOffset} and
    883  * rbuf->failedDiskSectorOffset are maintained to point to the unit
    884  * we're currently accessing.  Note that this deviates from the
    885  * standard C idiom of having counters point to the next thing to be
    886  * accessed.  This allows us to easily retry when we're blocked by
    887  * head separation or reconstruction-blockage events.
    888  *
    889  * returns nonzero if and only if there is nothing left unread on the
    890  * indicated disk
    891  *
    892  *****************************************************************************/
    893 static int
    894 IssueNextReadRequest(RF_Raid_t *raidPtr, RF_RowCol_t col)
    895 {
    896 	RF_PerDiskReconCtrl_t *ctrl = &raidPtr->reconControl->perDiskInfo[col];
    897 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    898 	RF_ReconBuffer_t *rbuf = ctrl->rbuf;
    899 	RF_ReconUnitCount_t RUsPerPU = layoutPtr->SUsPerPU / layoutPtr->SUsPerRU;
    900 	RF_SectorCount_t sectorsPerRU = layoutPtr->sectorsPerStripeUnit * layoutPtr->SUsPerRU;
    901 	int     do_new_check = 0, retcode = 0, status;
    902 
    903 	/* if we are currently the slowest disk, mark that we have to do a new
    904 	 * check */
    905 	if (ctrl->headSepCounter <= raidPtr->reconControl->minHeadSepCounter)
    906 		do_new_check = 1;
    907 
    908 	while (1) {
    909 
    910 		ctrl->ru_count++;
    911 		if (ctrl->ru_count < RUsPerPU) {
    912 			ctrl->diskOffset += sectorsPerRU;
    913 			rbuf->failedDiskSectorOffset += sectorsPerRU;
    914 		} else {
    915 			ctrl->curPSID++;
    916 			ctrl->ru_count = 0;
    917 			/* code left over from when head-sep was based on
    918 			 * parity stripe id */
    919 			if (ctrl->curPSID >= raidPtr->reconControl->lastPSID) {
    920 				CheckForNewMinHeadSep(raidPtr, ++(ctrl->headSepCounter));
    921 				return (1);	/* finito! */
    922 			}
    923 			/* find the disk offsets of the start of the parity
    924 			 * stripe on both the current disk and the failed
    925 			 * disk. skip this entire parity stripe if either disk
    926 			 * does not appear in the indicated PS */
    927 			status = ComputePSDiskOffsets(raidPtr, ctrl->curPSID, col, &ctrl->diskOffset, &rbuf->failedDiskSectorOffset,
    928 			    &rbuf->spCol, &rbuf->spOffset);
    929 			if (status) {
    930 				ctrl->ru_count = RUsPerPU - 1;
    931 				continue;
    932 			}
    933 		}
    934 		rbuf->which_ru = ctrl->ru_count;
    935 
    936 		/* skip this RU if it's already been reconstructed */
    937 		if (rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, rbuf->failedDiskSectorOffset)) {
    938 			Dprintf2("Skipping psid %ld ru %d: already reconstructed\n", ctrl->curPSID, ctrl->ru_count);
    939 			continue;
    940 		}
    941 		break;
    942 	}
    943 	ctrl->headSepCounter++;
    944 	if (do_new_check)
    945 		CheckForNewMinHeadSep(raidPtr, ctrl->headSepCounter);	/* update min if needed */
    946 
    947 
    948 	/* at this point, we have definitely decided what to do, and we have
    949 	 * only to see if we can actually do it now */
    950 	rbuf->parityStripeID = ctrl->curPSID;
    951 	rbuf->which_ru = ctrl->ru_count;
    952 #if RF_ACC_TRACE > 0
    953 	memset((char *) &raidPtr->recon_tracerecs[col], 0,
    954 	    sizeof(raidPtr->recon_tracerecs[col]));
    955 	raidPtr->recon_tracerecs[col].reconacc = 1;
    956 	RF_ETIMER_START(raidPtr->recon_tracerecs[col].recon_timer);
    957 #endif
    958 	retcode = TryToRead(raidPtr, col);
    959 	return (retcode);
    960 }
    961 
    962 /*
    963  * tries to issue the next read on the indicated disk.  We may be
    964  * blocked by (a) the heads being too far apart, or (b) recon on the
    965  * indicated RU being blocked due to a write by a user thread.  In
    966  * this case, we issue a head-sep or blockage wait request, which will
    967  * cause this same routine to be invoked again later when the blockage
    968  * has cleared.
    969  */
    970 
    971 static int
    972 TryToRead(RF_Raid_t *raidPtr, RF_RowCol_t col)
    973 {
    974 	RF_PerDiskReconCtrl_t *ctrl = &raidPtr->reconControl->perDiskInfo[col];
    975 	RF_SectorCount_t sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU;
    976 	RF_StripeNum_t psid = ctrl->curPSID;
    977 	RF_ReconUnitNum_t which_ru = ctrl->ru_count;
    978 	RF_DiskQueueData_t *req;
    979 	int     status;
    980 	RF_ReconParityStripeStatus_t *pssPtr, *newpssPtr;
    981 
    982 	/* if the current disk is too far ahead of the others, issue a
    983 	 * head-separation wait and return */
    984 	if (CheckHeadSeparation(raidPtr, ctrl, col, ctrl->headSepCounter, which_ru))
    985 		return (0);
    986 
    987 	/* allocate a new PSS in case we need it */
    988 	newpssPtr = rf_AllocPSStatus(raidPtr);
    989 
    990 	RF_LOCK_PSS_MUTEX(raidPtr, psid);
    991 	pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl->pssTable, psid, which_ru, RF_PSS_CREATE, newpssPtr);
    992 
    993 	if (pssPtr != newpssPtr) {
    994 		rf_FreePSStatus(raidPtr, newpssPtr);
    995 	}
    996 
    997 	/* if recon is blocked on the indicated parity stripe, issue a
    998 	 * block-wait request and return. this also must mark the indicated RU
    999 	 * in the stripe as under reconstruction if not blocked. */
   1000 	status = CheckForcedOrBlockedReconstruction(raidPtr, pssPtr, ctrl, col, psid, which_ru);
   1001 	if (status == RF_PSS_RECON_BLOCKED) {
   1002 		Dprintf2("RECON: Stalling psid %ld ru %d: recon blocked\n", psid, which_ru);
   1003 		goto out;
   1004 	} else
   1005 		if (status == RF_PSS_FORCED_ON_WRITE) {
   1006 			rf_CauseReconEvent(raidPtr, col, NULL, RF_REVENT_SKIP);
   1007 			goto out;
   1008 		}
   1009 	/* make one last check to be sure that the indicated RU didn't get
   1010 	 * reconstructed while we were waiting for something else to happen.
   1011 	 * This is unfortunate in that it causes us to make this check twice
   1012 	 * in the normal case.  Might want to make some attempt to re-work
   1013 	 * this so that we only do this check if we've definitely blocked on
   1014 	 * one of the above checks.  When this condition is detected, we may
   1015 	 * have just created a bogus status entry, which we need to delete. */
   1016 	if (rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, ctrl->rbuf->failedDiskSectorOffset)) {
   1017 		Dprintf2("RECON: Skipping psid %ld ru %d: prior recon after stall\n", psid, which_ru);
   1018 		if (pssPtr == newpssPtr)
   1019 			rf_PSStatusDelete(raidPtr, raidPtr->reconControl->pssTable, pssPtr);
   1020 		rf_CauseReconEvent(raidPtr, col, NULL, RF_REVENT_SKIP);
   1021 		goto out;
   1022 	}
   1023 	/* found something to read.  issue the I/O */
   1024 	Dprintf4("RECON: Read for psid %ld on col %d offset %ld buf %lx\n",
   1025 	    psid, col, ctrl->diskOffset, ctrl->rbuf->buffer);
   1026 #if RF_ACC_TRACE > 0
   1027 	RF_ETIMER_STOP(raidPtr->recon_tracerecs[col].recon_timer);
   1028 	RF_ETIMER_EVAL(raidPtr->recon_tracerecs[col].recon_timer);
   1029 	raidPtr->recon_tracerecs[col].specific.recon.recon_start_to_fetch_us =
   1030 	    RF_ETIMER_VAL_US(raidPtr->recon_tracerecs[col].recon_timer);
   1031 	RF_ETIMER_START(raidPtr->recon_tracerecs[col].recon_timer);
   1032 #endif
   1033 	/* should be ok to use a NULL proc pointer here, all the bufs we use
   1034 	 * should be in kernel space */
   1035 	req = rf_CreateDiskQueueData(RF_IO_TYPE_READ, ctrl->diskOffset, sectorsPerRU, ctrl->rbuf->buffer, psid, which_ru,
   1036 	    ReconReadDoneProc, (void *) ctrl, NULL,
   1037 #if RF_ACC_TRACE > 0
   1038 				     &raidPtr->recon_tracerecs[col],
   1039 #else
   1040 				     NULL,
   1041 #endif
   1042 				     (void *) raidPtr, 0, NULL);
   1043 
   1044 	RF_ASSERT(req);		/* XXX -- fix this -- XXX */
   1045 
   1046 	ctrl->rbuf->arg = (void *) req;
   1047 	rf_DiskIOEnqueue(&raidPtr->Queues[col], req, RF_IO_RECON_PRIORITY);
   1048 	pssPtr->issued[col] = 1;
   1049 
   1050 out:
   1051 	RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
   1052 	return (0);
   1053 }
   1054 
   1055 
   1056 /*
   1057  * given a parity stripe ID, we want to find out whether both the
   1058  * current disk and the failed disk exist in that parity stripe.  If
   1059  * not, we want to skip this whole PS.  If so, we want to find the
   1060  * disk offset of the start of the PS on both the current disk and the
   1061  * failed disk.
   1062  *
   1063  * this works by getting a list of disks comprising the indicated
   1064  * parity stripe, and searching the list for the current and failed
   1065  * disks.  Once we've decided they both exist in the parity stripe, we
   1066  * need to decide whether each is data or parity, so that we'll know
   1067  * which mapping function to call to get the corresponding disk
   1068  * offsets.
   1069  *
   1070  * this is kind of unpleasant, but doing it this way allows the
   1071  * reconstruction code to use parity stripe IDs rather than physical
   1072  * disks address to march through the failed disk, which greatly
   1073  * simplifies a lot of code, as well as eliminating the need for a
   1074  * reverse-mapping function.  I also think it will execute faster,
   1075  * since the calls to the mapping module are kept to a minimum.
   1076  *
   1077  * ASSUMES THAT THE STRIPE IDENTIFIER IDENTIFIES THE DISKS COMPRISING
   1078  * THE STRIPE IN THE CORRECT ORDER
   1079  *
   1080  * raidPtr          - raid descriptor
   1081  * psid             - parity stripe identifier
   1082  * col              - column of disk to find the offsets for
   1083  * spCol            - out: col of spare unit for failed unit
   1084  * spOffset         - out: offset into disk containing spare unit
   1085  *
   1086  */
   1087 
   1088 
   1089 static int
   1090 ComputePSDiskOffsets(RF_Raid_t *raidPtr, RF_StripeNum_t psid,
   1091 		     RF_RowCol_t col, RF_SectorNum_t *outDiskOffset,
   1092 		     RF_SectorNum_t *outFailedDiskSectorOffset,
   1093 		     RF_RowCol_t *spCol, RF_SectorNum_t *spOffset)
   1094 {
   1095 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
   1096 	RF_RowCol_t fcol = raidPtr->reconControl->fcol;
   1097 	RF_RaidAddr_t sosRaidAddress;	/* start-of-stripe */
   1098 	RF_RowCol_t *diskids;
   1099 	u_int   i, j, k, i_offset, j_offset;
   1100 	RF_RowCol_t pcol;
   1101 	int     testcol;
   1102 	RF_SectorNum_t poffset;
   1103 	char    i_is_parity = 0, j_is_parity = 0;
   1104 	RF_RowCol_t stripeWidth = layoutPtr->numDataCol + layoutPtr->numParityCol;
   1105 
   1106 	/* get a listing of the disks comprising that stripe */
   1107 	sosRaidAddress = rf_ParityStripeIDToRaidAddress(layoutPtr, psid);
   1108 	(layoutPtr->map->IdentifyStripe) (raidPtr, sosRaidAddress, &diskids);
   1109 	RF_ASSERT(diskids);
   1110 
   1111 	/* reject this entire parity stripe if it does not contain the
   1112 	 * indicated disk or it does not contain the failed disk */
   1113 
   1114 	for (i = 0; i < stripeWidth; i++) {
   1115 		if (col == diskids[i])
   1116 			break;
   1117 	}
   1118 	if (i == stripeWidth)
   1119 		goto skipit;
   1120 	for (j = 0; j < stripeWidth; j++) {
   1121 		if (fcol == diskids[j])
   1122 			break;
   1123 	}
   1124 	if (j == stripeWidth) {
   1125 		goto skipit;
   1126 	}
   1127 	/* find out which disk the parity is on */
   1128 	(layoutPtr->map->MapParity) (raidPtr, sosRaidAddress, &pcol, &poffset, RF_DONT_REMAP);
   1129 
   1130 	/* find out if either the current RU or the failed RU is parity */
   1131 	/* also, if the parity occurs in this stripe prior to the data and/or
   1132 	 * failed col, we need to decrement i and/or j */
   1133 	for (k = 0; k < stripeWidth; k++)
   1134 		if (diskids[k] == pcol)
   1135 			break;
   1136 	RF_ASSERT(k < stripeWidth);
   1137 	i_offset = i;
   1138 	j_offset = j;
   1139 	if (k < i)
   1140 		i_offset--;
   1141 	else
   1142 		if (k == i) {
   1143 			i_is_parity = 1;
   1144 			i_offset = 0;
   1145 		}		/* set offsets to zero to disable multiply
   1146 				 * below */
   1147 	if (k < j)
   1148 		j_offset--;
   1149 	else
   1150 		if (k == j) {
   1151 			j_is_parity = 1;
   1152 			j_offset = 0;
   1153 		}
   1154 	/* at this point, [ij]_is_parity tells us whether the [current,failed]
   1155 	 * disk is parity at the start of this RU, and, if data, "[ij]_offset"
   1156 	 * tells us how far into the stripe the [current,failed] disk is. */
   1157 
   1158 	/* call the mapping routine to get the offset into the current disk,
   1159 	 * repeat for failed disk. */
   1160 	if (i_is_parity)
   1161 		layoutPtr->map->MapParity(raidPtr, sosRaidAddress + i_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outDiskOffset, RF_DONT_REMAP);
   1162 	else
   1163 		layoutPtr->map->MapSector(raidPtr, sosRaidAddress + i_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outDiskOffset, RF_DONT_REMAP);
   1164 
   1165 	RF_ASSERT(col == testcol);
   1166 
   1167 	if (j_is_parity)
   1168 		layoutPtr->map->MapParity(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outFailedDiskSectorOffset, RF_DONT_REMAP);
   1169 	else
   1170 		layoutPtr->map->MapSector(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outFailedDiskSectorOffset, RF_DONT_REMAP);
   1171 	RF_ASSERT(fcol == testcol);
   1172 
   1173 	/* now locate the spare unit for the failed unit */
   1174 	if (layoutPtr->map->flags & RF_DISTRIBUTE_SPARE) {
   1175 		if (j_is_parity)
   1176 			layoutPtr->map->MapParity(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, spCol, spOffset, RF_REMAP);
   1177 		else
   1178 			layoutPtr->map->MapSector(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, spCol, spOffset, RF_REMAP);
   1179 	} else {
   1180 		*spCol = raidPtr->reconControl->spareCol;
   1181 		*spOffset = *outFailedDiskSectorOffset;
   1182 	}
   1183 
   1184 	return (0);
   1185 
   1186 skipit:
   1187 	Dprintf2("RECON: Skipping psid %ld: nothing needed from r%d c%d\n",
   1188 	    psid, col);
   1189 	return (1);
   1190 }
   1191 /* this is called when a buffer has become ready to write to the replacement disk */
   1192 static int
   1193 IssueNextWriteRequest(RF_Raid_t *raidPtr)
   1194 {
   1195 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
   1196 	RF_SectorCount_t sectorsPerRU = layoutPtr->sectorsPerStripeUnit * layoutPtr->SUsPerRU;
   1197 #if RF_ACC_TRACE > 0
   1198 	RF_RowCol_t fcol = raidPtr->reconControl->fcol;
   1199 #endif
   1200 	RF_ReconBuffer_t *rbuf;
   1201 	RF_DiskQueueData_t *req;
   1202 
   1203 	rbuf = rf_GetFullReconBuffer(raidPtr->reconControl);
   1204 	RF_ASSERT(rbuf);	/* there must be one available, or we wouldn't
   1205 				 * have gotten the event that sent us here */
   1206 	RF_ASSERT(rbuf->pssPtr);
   1207 
   1208 	rbuf->pssPtr->writeRbuf = rbuf;
   1209 	rbuf->pssPtr = NULL;
   1210 
   1211 	Dprintf6("RECON: New write (c %d offs %d) for psid %ld ru %d (failed disk offset %ld) buf %lx\n",
   1212 	    rbuf->spCol, rbuf->spOffset, rbuf->parityStripeID,
   1213 	    rbuf->which_ru, rbuf->failedDiskSectorOffset, rbuf->buffer);
   1214 	Dprintf6("RECON: new write psid %ld   %02x %02x %02x %02x %02x\n",
   1215 	    rbuf->parityStripeID, rbuf->buffer[0] & 0xff, rbuf->buffer[1] & 0xff,
   1216 	    rbuf->buffer[2] & 0xff, rbuf->buffer[3] & 0xff, rbuf->buffer[4] & 0xff);
   1217 
   1218 	/* should be ok to use a NULL b_proc here b/c all addrs should be in
   1219 	 * kernel space */
   1220 	req = rf_CreateDiskQueueData(RF_IO_TYPE_WRITE, rbuf->spOffset,
   1221 	    sectorsPerRU, rbuf->buffer,
   1222 	    rbuf->parityStripeID, rbuf->which_ru,
   1223 	    ReconWriteDoneProc, (void *) rbuf, NULL,
   1224 #if RF_ACC_TRACE > 0
   1225 	    &raidPtr->recon_tracerecs[fcol],
   1226 #else
   1227 				     NULL,
   1228 #endif
   1229 	    (void *) raidPtr, 0, NULL);
   1230 
   1231 	RF_ASSERT(req);		/* XXX -- fix this -- XXX */
   1232 
   1233 	rbuf->arg = (void *) req;
   1234 	rf_DiskIOEnqueue(&raidPtr->Queues[rbuf->spCol], req, RF_IO_RECON_PRIORITY);
   1235 
   1236 	return (0);
   1237 }
   1238 
   1239 /*
   1240  * this gets called upon the completion of a reconstruction read
   1241  * operation the arg is a pointer to the per-disk reconstruction
   1242  * control structure for the process that just finished a read.
   1243  *
   1244  * called at interrupt context in the kernel, so don't do anything
   1245  * illegal here.
   1246  */
   1247 static int
   1248 ReconReadDoneProc(void *arg, int status)
   1249 {
   1250 	RF_PerDiskReconCtrl_t *ctrl = (RF_PerDiskReconCtrl_t *) arg;
   1251 	RF_Raid_t *raidPtr = ctrl->reconCtrl->reconDesc->raidPtr;
   1252 
   1253 	if (status) {
   1254 		printf("raid%d: Recon read failed!\n", raidPtr->raidid);
   1255 		rf_CauseReconEvent(raidPtr, ctrl->col, NULL, RF_REVENT_READ_FAILED);
   1256 		return(0);
   1257 	}
   1258 #if RF_ACC_TRACE > 0
   1259 	RF_ETIMER_STOP(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
   1260 	RF_ETIMER_EVAL(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
   1261 	raidPtr->recon_tracerecs[ctrl->col].specific.recon.recon_fetch_to_return_us =
   1262 	    RF_ETIMER_VAL_US(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
   1263 	RF_ETIMER_START(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
   1264 #endif
   1265 	rf_CauseReconEvent(raidPtr, ctrl->col, NULL, RF_REVENT_READDONE);
   1266 	return (0);
   1267 }
   1268 /* this gets called upon the completion of a reconstruction write operation.
   1269  * the arg is a pointer to the rbuf that was just written
   1270  *
   1271  * called at interrupt context in the kernel, so don't do anything illegal here.
   1272  */
   1273 static int
   1274 ReconWriteDoneProc(void *arg, int status)
   1275 {
   1276 	RF_ReconBuffer_t *rbuf = (RF_ReconBuffer_t *) arg;
   1277 
   1278 	Dprintf2("Reconstruction completed on psid %ld ru %d\n", rbuf->parityStripeID, rbuf->which_ru);
   1279 	if (status) {
   1280 		printf("raid%d: Recon write failed!\n", rbuf->raidPtr->raidid);
   1281 		rf_CauseReconEvent(rbuf->raidPtr, rbuf->col, arg, RF_REVENT_WRITE_FAILED);
   1282 		return(0);
   1283 	}
   1284 	rf_CauseReconEvent(rbuf->raidPtr, rbuf->col, arg, RF_REVENT_WRITEDONE);
   1285 	return (0);
   1286 }
   1287 
   1288 
   1289 /*
   1290  * computes a new minimum head sep, and wakes up anyone who needs to
   1291  * be woken as a result
   1292  */
   1293 static void
   1294 CheckForNewMinHeadSep(RF_Raid_t *raidPtr, RF_HeadSepLimit_t hsCtr)
   1295 {
   1296 	RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl;
   1297 	RF_HeadSepLimit_t new_min;
   1298 	RF_RowCol_t i;
   1299 	RF_CallbackDesc_t *p;
   1300 	RF_ASSERT(hsCtr >= reconCtrlPtr->minHeadSepCounter);	/* from the definition
   1301 								 * of a minimum */
   1302 
   1303 
   1304 	RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1305 
   1306 	new_min = ~(1L << (8 * sizeof(long) - 1));	/* 0x7FFF....FFF */
   1307 	for (i = 0; i < raidPtr->numCol; i++)
   1308 		if (i != reconCtrlPtr->fcol) {
   1309 			if (reconCtrlPtr->perDiskInfo[i].headSepCounter < new_min)
   1310 				new_min = reconCtrlPtr->perDiskInfo[i].headSepCounter;
   1311 		}
   1312 	/* set the new minimum and wake up anyone who can now run again */
   1313 	if (new_min != reconCtrlPtr->minHeadSepCounter) {
   1314 		reconCtrlPtr->minHeadSepCounter = new_min;
   1315 		Dprintf1("RECON:  new min head pos counter val is %ld\n", new_min);
   1316 		while (reconCtrlPtr->headSepCBList) {
   1317 			if (reconCtrlPtr->headSepCBList->callbackArg.v > new_min)
   1318 				break;
   1319 			p = reconCtrlPtr->headSepCBList;
   1320 			reconCtrlPtr->headSepCBList = p->next;
   1321 			p->next = NULL;
   1322 			rf_CauseReconEvent(raidPtr, p->col, NULL, RF_REVENT_HEADSEPCLEAR);
   1323 			rf_FreeCallbackDesc(p);
   1324 		}
   1325 
   1326 	}
   1327 	RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1328 }
   1329 
   1330 /*
   1331  * checks to see that the maximum head separation will not be violated
   1332  * if we initiate a reconstruction I/O on the indicated disk.
   1333  * Limiting the maximum head separation between two disks eliminates
   1334  * the nasty buffer-stall conditions that occur when one disk races
   1335  * ahead of the others and consumes all of the floating recon buffers.
   1336  * This code is complex and unpleasant but it's necessary to avoid
   1337  * some very nasty, albeit fairly rare, reconstruction behavior.
   1338  *
   1339  * returns non-zero if and only if we have to stop working on the
   1340  * indicated disk due to a head-separation delay.
   1341  */
   1342 static int
   1343 CheckHeadSeparation(RF_Raid_t *raidPtr, RF_PerDiskReconCtrl_t *ctrl,
   1344 		    RF_RowCol_t col, RF_HeadSepLimit_t hsCtr,
   1345 		    RF_ReconUnitNum_t which_ru)
   1346 {
   1347 	RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl;
   1348 	RF_CallbackDesc_t *cb, *p, *pt;
   1349 	int     retval = 0;
   1350 
   1351 	/* if we're too far ahead of the slowest disk, stop working on this
   1352 	 * disk until the slower ones catch up.  We do this by scheduling a
   1353 	 * wakeup callback for the time when the slowest disk has caught up.
   1354 	 * We define "caught up" with 20% hysteresis, i.e. the head separation
   1355 	 * must have fallen to at most 80% of the max allowable head
   1356 	 * separation before we'll wake up.
   1357 	 *
   1358 	 */
   1359 	RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1360 	if ((raidPtr->headSepLimit >= 0) &&
   1361 	    ((ctrl->headSepCounter - reconCtrlPtr->minHeadSepCounter) > raidPtr->headSepLimit)) {
   1362 		Dprintf5("raid%d: RECON: head sep stall: col %d hsCtr %ld minHSCtr %ld limit %ld\n",
   1363 			 raidPtr->raidid, col, ctrl->headSepCounter,
   1364 			 reconCtrlPtr->minHeadSepCounter,
   1365 			 raidPtr->headSepLimit);
   1366 		cb = rf_AllocCallbackDesc();
   1367 		/* the minHeadSepCounter value we have to get to before we'll
   1368 		 * wake up.  build in 20% hysteresis. */
   1369 		cb->callbackArg.v = (ctrl->headSepCounter - raidPtr->headSepLimit + raidPtr->headSepLimit / 5);
   1370 		cb->col = col;
   1371 		cb->next = NULL;
   1372 
   1373 		/* insert this callback descriptor into the sorted list of
   1374 		 * pending head-sep callbacks */
   1375 		p = reconCtrlPtr->headSepCBList;
   1376 		if (!p)
   1377 			reconCtrlPtr->headSepCBList = cb;
   1378 		else
   1379 			if (cb->callbackArg.v < p->callbackArg.v) {
   1380 				cb->next = reconCtrlPtr->headSepCBList;
   1381 				reconCtrlPtr->headSepCBList = cb;
   1382 			} else {
   1383 				for (pt = p, p = p->next; p && (p->callbackArg.v < cb->callbackArg.v); pt = p, p = p->next);
   1384 				cb->next = p;
   1385 				pt->next = cb;
   1386 			}
   1387 		retval = 1;
   1388 #if RF_RECON_STATS > 0
   1389 		ctrl->reconCtrl->reconDesc->hsStallCount++;
   1390 #endif				/* RF_RECON_STATS > 0 */
   1391 	}
   1392 	RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
   1393 
   1394 	return (retval);
   1395 }
   1396 /*
   1397  * checks to see if reconstruction has been either forced or blocked
   1398  * by a user operation.  if forced, we skip this RU entirely.  else if
   1399  * blocked, put ourselves on the wait list.  else return 0.
   1400  *
   1401  * ASSUMES THE PSS MUTEX IS LOCKED UPON ENTRY
   1402  */
   1403 static int
   1404 CheckForcedOrBlockedReconstruction(RF_Raid_t *raidPtr,
   1405 				   RF_ReconParityStripeStatus_t *pssPtr,
   1406 				   RF_PerDiskReconCtrl_t *ctrl,
   1407 				   RF_RowCol_t col, RF_StripeNum_t psid,
   1408 				   RF_ReconUnitNum_t which_ru)
   1409 {
   1410 	RF_CallbackDesc_t *cb;
   1411 	int     retcode = 0;
   1412 
   1413 	if ((pssPtr->flags & RF_PSS_FORCED_ON_READ) || (pssPtr->flags & RF_PSS_FORCED_ON_WRITE))
   1414 		retcode = RF_PSS_FORCED_ON_WRITE;
   1415 	else
   1416 		if (pssPtr->flags & RF_PSS_RECON_BLOCKED) {
   1417 			Dprintf3("RECON: col %d blocked at psid %ld ru %d\n", col, psid, which_ru);
   1418 			cb = rf_AllocCallbackDesc();	/* append ourselves to
   1419 							 * the blockage-wait
   1420 							 * list */
   1421 			cb->col = col;
   1422 			cb->next = pssPtr->blockWaitList;
   1423 			pssPtr->blockWaitList = cb;
   1424 			retcode = RF_PSS_RECON_BLOCKED;
   1425 		}
   1426 	if (!retcode)
   1427 		pssPtr->flags |= RF_PSS_UNDER_RECON;	/* mark this RU as under
   1428 							 * reconstruction */
   1429 
   1430 	return (retcode);
   1431 }
   1432 /*
   1433  * if reconstruction is currently ongoing for the indicated stripeID,
   1434  * reconstruction is forced to completion and we return non-zero to
   1435  * indicate that the caller must wait.  If not, then reconstruction is
   1436  * blocked on the indicated stripe and the routine returns zero.  If
   1437  * and only if we return non-zero, we'll cause the cbFunc to get
   1438  * invoked with the cbArg when the reconstruction has completed.
   1439  */
   1440 int
   1441 rf_ForceOrBlockRecon(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap,
   1442 		     void (*cbFunc)(RF_Raid_t *, void *), void *cbArg)
   1443 {
   1444 	RF_StripeNum_t stripeID = asmap->stripeID;	/* the stripe ID we're
   1445 							 * forcing recon on */
   1446 	RF_SectorCount_t sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU;	/* num sects in one RU */
   1447 	RF_ReconParityStripeStatus_t *pssPtr, *newpssPtr;	/* a pointer to the parity
   1448 						 * stripe status structure */
   1449 	RF_StripeNum_t psid;	/* parity stripe id */
   1450 	RF_SectorNum_t offset, fd_offset;	/* disk offset, failed-disk
   1451 						 * offset */
   1452 	RF_RowCol_t *diskids;
   1453 	RF_ReconUnitNum_t which_ru;	/* RU within parity stripe */
   1454 	RF_RowCol_t fcol, diskno, i;
   1455 	RF_ReconBuffer_t *new_rbuf;	/* ptr to newly allocated rbufs */
   1456 	RF_DiskQueueData_t *req;/* disk I/O req to be enqueued */
   1457 	RF_CallbackDesc_t *cb;
   1458 	int     nPromoted;
   1459 
   1460 	psid = rf_MapStripeIDToParityStripeID(&raidPtr->Layout, stripeID, &which_ru);
   1461 
   1462 	/* allocate a new PSS in case we need it */
   1463         newpssPtr = rf_AllocPSStatus(raidPtr);
   1464 
   1465 	RF_LOCK_PSS_MUTEX(raidPtr, psid);
   1466 
   1467 	pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl->pssTable, psid, which_ru, RF_PSS_CREATE | RF_PSS_RECON_BLOCKED, newpssPtr);
   1468 
   1469         if (pssPtr != newpssPtr) {
   1470                 rf_FreePSStatus(raidPtr, newpssPtr);
   1471         }
   1472 
   1473 	/* if recon is not ongoing on this PS, just return */
   1474 	if (!(pssPtr->flags & RF_PSS_UNDER_RECON)) {
   1475 		RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
   1476 		return (0);
   1477 	}
   1478 	/* otherwise, we have to wait for reconstruction to complete on this
   1479 	 * RU. */
   1480 	/* In order to avoid waiting for a potentially large number of
   1481 	 * low-priority accesses to complete, we force a normal-priority (i.e.
   1482 	 * not low-priority) reconstruction on this RU. */
   1483 	if (!(pssPtr->flags & RF_PSS_FORCED_ON_WRITE) && !(pssPtr->flags & RF_PSS_FORCED_ON_READ)) {
   1484 		DDprintf1("Forcing recon on psid %ld\n", psid);
   1485 		pssPtr->flags |= RF_PSS_FORCED_ON_WRITE;	/* mark this RU as under
   1486 								 * forced recon */
   1487 		pssPtr->flags &= ~RF_PSS_RECON_BLOCKED;	/* clear the blockage
   1488 							 * that we just set */
   1489 		fcol = raidPtr->reconControl->fcol;
   1490 
   1491 		/* get a listing of the disks comprising the indicated stripe */
   1492 		(raidPtr->Layout.map->IdentifyStripe) (raidPtr, asmap->raidAddress, &diskids);
   1493 
   1494 		/* For previously issued reads, elevate them to normal
   1495 		 * priority.  If the I/O has already completed, it won't be
   1496 		 * found in the queue, and hence this will be a no-op. For
   1497 		 * unissued reads, allocate buffers and issue new reads.  The
   1498 		 * fact that we've set the FORCED bit means that the regular
   1499 		 * recon procs will not re-issue these reqs */
   1500 		for (i = 0; i < raidPtr->Layout.numDataCol + raidPtr->Layout.numParityCol; i++)
   1501 			if ((diskno = diskids[i]) != fcol) {
   1502 				if (pssPtr->issued[diskno]) {
   1503 					nPromoted = rf_DiskIOPromote(&raidPtr->Queues[diskno], psid, which_ru);
   1504 					if (rf_reconDebug && nPromoted)
   1505 						printf("raid%d: promoted read from col %d\n", raidPtr->raidid, diskno);
   1506 				} else {
   1507 					new_rbuf = rf_MakeReconBuffer(raidPtr, diskno, RF_RBUF_TYPE_FORCED);	/* create new buf */
   1508 					ComputePSDiskOffsets(raidPtr, psid, diskno, &offset, &fd_offset,
   1509 					    &new_rbuf->spCol, &new_rbuf->spOffset);	/* find offsets & spare
   1510 													 * location */
   1511 					new_rbuf->parityStripeID = psid;	/* fill in the buffer */
   1512 					new_rbuf->which_ru = which_ru;
   1513 					new_rbuf->failedDiskSectorOffset = fd_offset;
   1514 					new_rbuf->priority = RF_IO_NORMAL_PRIORITY;
   1515 
   1516 					/* use NULL b_proc b/c all addrs
   1517 					 * should be in kernel space */
   1518 					req = rf_CreateDiskQueueData(RF_IO_TYPE_READ, offset + which_ru * sectorsPerRU, sectorsPerRU, new_rbuf->buffer,
   1519 					    psid, which_ru, (int (*) (void *, int)) ForceReconReadDoneProc, (void *) new_rbuf, NULL,
   1520 					    NULL, (void *) raidPtr, 0, NULL);
   1521 
   1522 					RF_ASSERT(req);	/* XXX -- fix this --
   1523 							 * XXX */
   1524 
   1525 					new_rbuf->arg = req;
   1526 					rf_DiskIOEnqueue(&raidPtr->Queues[diskno], req, RF_IO_NORMAL_PRIORITY);	/* enqueue the I/O */
   1527 					Dprintf2("raid%d: Issued new read req on col %d\n", raidPtr->raidid, diskno);
   1528 				}
   1529 			}
   1530 		/* if the write is sitting in the disk queue, elevate its
   1531 		 * priority */
   1532 		if (rf_DiskIOPromote(&raidPtr->Queues[fcol], psid, which_ru))
   1533 			printf("raid%d: promoted write to col %d\n",
   1534 			       raidPtr->raidid, fcol);
   1535 	}
   1536 	/* install a callback descriptor to be invoked when recon completes on
   1537 	 * this parity stripe. */
   1538 	cb = rf_AllocCallbackDesc();
   1539 	/* XXX the following is bogus.. These functions don't really match!!
   1540 	 * GO */
   1541 	cb->callbackFunc = (void (*) (RF_CBParam_t)) cbFunc;
   1542 	cb->callbackArg.p = (void *) cbArg;
   1543 	cb->next = pssPtr->procWaitList;
   1544 	pssPtr->procWaitList = cb;
   1545 	DDprintf2("raid%d: Waiting for forced recon on psid %ld\n",
   1546 		  raidPtr->raidid, psid);
   1547 
   1548 	RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
   1549 	return (1);
   1550 }
   1551 /* called upon the completion of a forced reconstruction read.
   1552  * all we do is schedule the FORCEDREADONE event.
   1553  * called at interrupt context in the kernel, so don't do anything illegal here.
   1554  */
   1555 static void
   1556 ForceReconReadDoneProc(void *arg, int status)
   1557 {
   1558 	RF_ReconBuffer_t *rbuf = arg;
   1559 
   1560 	if (status) {
   1561 		printf("raid%d: Forced recon read failed!\n", rbuf->raidPtr->raidid);
   1562 		rf_CauseReconEvent(rbuf->raidPtr, rbuf->col, (void *) rbuf, RF_REVENT_FORCEDREAD_FAILED);
   1563 	}
   1564 	rf_CauseReconEvent(rbuf->raidPtr, rbuf->col, (void *) rbuf, RF_REVENT_FORCEDREADDONE);
   1565 }
   1566 /* releases a block on the reconstruction of the indicated stripe */
   1567 int
   1568 rf_UnblockRecon(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap)
   1569 {
   1570 	RF_StripeNum_t stripeID = asmap->stripeID;
   1571 	RF_ReconParityStripeStatus_t *pssPtr;
   1572 	RF_ReconUnitNum_t which_ru;
   1573 	RF_StripeNum_t psid;
   1574 	RF_CallbackDesc_t *cb;
   1575 
   1576 	psid = rf_MapStripeIDToParityStripeID(&raidPtr->Layout, stripeID, &which_ru);
   1577 	RF_LOCK_PSS_MUTEX(raidPtr, psid);
   1578 	pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl->pssTable, psid, which_ru, RF_PSS_NONE, NULL);
   1579 
   1580 	/* When recon is forced, the pss desc can get deleted before we get
   1581 	 * back to unblock recon. But, this can _only_ happen when recon is
   1582 	 * forced. It would be good to put some kind of sanity check here, but
   1583 	 * how to decide if recon was just forced or not? */
   1584 	if (!pssPtr) {
   1585 		/* printf("Warning: no pss descriptor upon unblock on psid %ld
   1586 		 * RU %d\n",psid,which_ru); */
   1587 #if (RF_DEBUG_RECON > 0) || (RF_DEBUG_PSS > 0)
   1588 		if (rf_reconDebug || rf_pssDebug)
   1589 			printf("Warning: no pss descriptor upon unblock on psid %ld RU %d\n", (long) psid, which_ru);
   1590 #endif
   1591 		goto out;
   1592 	}
   1593 	pssPtr->blockCount--;
   1594 	Dprintf3("raid%d: unblocking recon on psid %ld: blockcount is %d\n",
   1595 		 raidPtr->raidid, psid, pssPtr->blockCount);
   1596 	if (pssPtr->blockCount == 0) {	/* if recon blockage has been released */
   1597 
   1598 		/* unblock recon before calling CauseReconEvent in case
   1599 		 * CauseReconEvent causes us to try to issue a new read before
   1600 		 * returning here. */
   1601 		pssPtr->flags &= ~RF_PSS_RECON_BLOCKED;
   1602 
   1603 
   1604 		while (pssPtr->blockWaitList) {
   1605 			/* spin through the block-wait list and
   1606 			   release all the waiters */
   1607 			cb = pssPtr->blockWaitList;
   1608 			pssPtr->blockWaitList = cb->next;
   1609 			cb->next = NULL;
   1610 			rf_CauseReconEvent(raidPtr, cb->col, NULL, RF_REVENT_BLOCKCLEAR);
   1611 			rf_FreeCallbackDesc(cb);
   1612 		}
   1613 		if (!(pssPtr->flags & RF_PSS_UNDER_RECON)) {
   1614 			/* if no recon was requested while recon was blocked */
   1615 			rf_PSStatusDelete(raidPtr, raidPtr->reconControl->pssTable, pssPtr);
   1616 		}
   1617 	}
   1618 out:
   1619 	RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
   1620 	return (0);
   1621 }
   1622