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