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