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