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