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