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