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