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