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