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