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