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