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