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