Home | History | Annotate | Line # | Download | only in raidframe
rf_paritylogging.c revision 1.2
      1  1.2  oster /*	$NetBSD: rf_paritylogging.c,v 1.2 1999/01/26 02:34:00 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: William V. Courtright II
      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   parity logging configuration, dag selection, and mapping is implemented here
     32  1.1  oster  */
     33  1.1  oster 
     34  1.1  oster #include "rf_archs.h"
     35  1.1  oster 
     36  1.1  oster #if RF_INCLUDE_PARITYLOGGING > 0
     37  1.1  oster 
     38  1.1  oster #include "rf_types.h"
     39  1.1  oster #include "rf_raid.h"
     40  1.1  oster #include "rf_dag.h"
     41  1.1  oster #include "rf_dagutils.h"
     42  1.1  oster #include "rf_dagfuncs.h"
     43  1.1  oster #include "rf_dagffrd.h"
     44  1.1  oster #include "rf_dagffwr.h"
     45  1.1  oster #include "rf_dagdegrd.h"
     46  1.1  oster #include "rf_dagdegwr.h"
     47  1.1  oster #include "rf_threadid.h"
     48  1.1  oster #include "rf_paritylog.h"
     49  1.1  oster #include "rf_paritylogDiskMgr.h"
     50  1.1  oster #include "rf_paritylogging.h"
     51  1.1  oster #include "rf_parityloggingdags.h"
     52  1.1  oster #include "rf_general.h"
     53  1.1  oster #include "rf_map.h"
     54  1.1  oster #include "rf_utils.h"
     55  1.1  oster #include "rf_shutdown.h"
     56  1.1  oster 
     57  1.1  oster typedef struct RF_ParityLoggingConfigInfo_s {
     58  1.1  oster   RF_RowCol_t  **stripeIdentifier;  /* filled in at config time & used by IdentifyStripe */
     59  1.1  oster } RF_ParityLoggingConfigInfo_t;
     60  1.1  oster 
     61  1.1  oster static void FreeRegionInfo(RF_Raid_t *raidPtr, RF_RegionId_t regionID);
     62  1.1  oster static void rf_ShutdownParityLogging(RF_ThreadArg_t arg);
     63  1.1  oster static void rf_ShutdownParityLoggingRegionInfo(RF_ThreadArg_t arg);
     64  1.1  oster static void rf_ShutdownParityLoggingPool(RF_ThreadArg_t arg);
     65  1.1  oster static void rf_ShutdownParityLoggingRegionBufferPool(RF_ThreadArg_t arg);
     66  1.1  oster static void rf_ShutdownParityLoggingParityBufferPool(RF_ThreadArg_t arg);
     67  1.1  oster static void rf_ShutdownParityLoggingDiskQueue(RF_ThreadArg_t arg);
     68  1.1  oster 
     69  1.1  oster int rf_ConfigureParityLogging(
     70  1.1  oster   RF_ShutdownList_t  **listp,
     71  1.1  oster   RF_Raid_t           *raidPtr,
     72  1.1  oster   RF_Config_t         *cfgPtr)
     73  1.1  oster {
     74  1.1  oster   int i, j, startdisk, rc;
     75  1.1  oster   RF_SectorCount_t totalLogCapacity, fragmentation, lastRegionCapacity;
     76  1.1  oster   RF_SectorCount_t parityBufferCapacity, maxRegionParityRange;
     77  1.1  oster   RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
     78  1.1  oster   RF_ParityLoggingConfigInfo_t *info;
     79  1.1  oster   RF_ParityLog_t *l=NULL, *next;
     80  1.1  oster   caddr_t lHeapPtr;
     81  1.1  oster 
     82  1.1  oster   /*
     83  1.1  oster    * We create multiple entries on the shutdown list here, since
     84  1.1  oster    * this configuration routine is fairly complicated in and of
     85  1.1  oster    * itself, and this makes backing out of a failed configuration
     86  1.1  oster    * much simpler.
     87  1.1  oster    */
     88  1.1  oster 
     89  1.1  oster   raidPtr->numSectorsPerLog = RF_DEFAULT_NUM_SECTORS_PER_LOG;
     90  1.1  oster 
     91  1.1  oster   /* create a parity logging configuration structure */
     92  1.1  oster   RF_MallocAndAdd(info, sizeof(RF_ParityLoggingConfigInfo_t), (RF_ParityLoggingConfigInfo_t *), raidPtr->cleanupList);
     93  1.1  oster   if (info == NULL)
     94  1.1  oster     return(ENOMEM);
     95  1.1  oster   layoutPtr->layoutSpecificInfo = (void *) info;
     96  1.1  oster 
     97  1.1  oster   RF_ASSERT(raidPtr->numRow == 1);
     98  1.1  oster 
     99  1.1  oster   /* the stripe identifier must identify the disks in each stripe,
    100  1.1  oster    * IN THE ORDER THAT THEY APPEAR IN THE STRIPE.
    101  1.1  oster    */
    102  1.1  oster   info->stripeIdentifier = rf_make_2d_array((raidPtr->numCol), (raidPtr->numCol), raidPtr->cleanupList);
    103  1.1  oster   if (info->stripeIdentifier == NULL)
    104  1.1  oster     return(ENOMEM);
    105  1.1  oster 
    106  1.1  oster   startdisk = 0;
    107  1.1  oster   for (i=0; i<(raidPtr->numCol); i++)
    108  1.1  oster     {
    109  1.1  oster       for (j=0; j<(raidPtr->numCol); j++)
    110  1.1  oster 	{
    111  1.1  oster 	  info->stripeIdentifier[i][j] = (startdisk + j) % (raidPtr->numCol - 1);
    112  1.1  oster 	}
    113  1.1  oster       if ((--startdisk) < 0)
    114  1.1  oster 	startdisk = raidPtr->numCol-1-1;
    115  1.1  oster     }
    116  1.1  oster 
    117  1.1  oster   /* fill in the remaining layout parameters */
    118  1.1  oster   layoutPtr->numStripe = layoutPtr->stripeUnitsPerDisk;
    119  1.1  oster   layoutPtr->bytesPerStripeUnit = layoutPtr->sectorsPerStripeUnit << raidPtr->logBytesPerSector;
    120  1.1  oster   layoutPtr->numParityCol = 1;
    121  1.1  oster   layoutPtr->numParityLogCol = 1;
    122  1.1  oster   layoutPtr->numDataCol = raidPtr->numCol - layoutPtr->numParityCol - layoutPtr->numParityLogCol;
    123  1.1  oster   layoutPtr->dataSectorsPerStripe = layoutPtr->numDataCol * layoutPtr->sectorsPerStripeUnit;
    124  1.1  oster   layoutPtr->dataStripeUnitsPerDisk = layoutPtr->stripeUnitsPerDisk;
    125  1.1  oster   raidPtr->sectorsPerDisk = layoutPtr->stripeUnitsPerDisk * layoutPtr->sectorsPerStripeUnit;
    126  1.1  oster 
    127  1.1  oster   raidPtr->totalSectors = layoutPtr->stripeUnitsPerDisk * layoutPtr->numDataCol * layoutPtr->sectorsPerStripeUnit;
    128  1.1  oster 
    129  1.1  oster   /* configure parity log parameters
    130  1.1  oster 
    131  1.1  oster      parameter               comment/constraints
    132  1.1  oster      ----------------        -------------------
    133  1.1  oster    * numParityRegions        all regions (except possibly last) of equal size
    134  1.1  oster    * totalInCoreLogCapacity  amount of memory in bytes available for in-core logs (default 1 MB)
    135  1.1  oster    # numSectorsPerLog        capacity of an in-core log in sectors (1 disk track)
    136  1.1  oster      numParityLogs           total number of in-core logs, should be at least numParityRegions
    137  1.1  oster      regionLogCapacity       size of a region log (except possibly last one) in sectors
    138  1.1  oster      totalLogCapacity        total amount of log space in sectors
    139  1.1  oster 
    140  1.1  oster    * denotes a user settable parameter.
    141  1.1  oster    # logs are fixed to be the size of a disk track, value #defined in rf_paritylog.h
    142  1.1  oster 
    143  1.1  oster   */
    144  1.1  oster 
    145  1.1  oster   totalLogCapacity = layoutPtr->stripeUnitsPerDisk * layoutPtr->sectorsPerStripeUnit * layoutPtr->numParityLogCol;
    146  1.1  oster   raidPtr->regionLogCapacity = totalLogCapacity / rf_numParityRegions;
    147  1.1  oster   if (rf_parityLogDebug)
    148  1.1  oster     printf("bytes per sector %d\n", raidPtr->bytesPerSector);
    149  1.1  oster 
    150  1.1  oster   /* reduce fragmentation within a disk region by adjusting the number of regions
    151  1.1  oster      in an attempt to allow an integral number of logs to fit into a disk region */
    152  1.1  oster   fragmentation = raidPtr->regionLogCapacity % raidPtr->numSectorsPerLog;
    153  1.1  oster   if (fragmentation > 0)
    154  1.1  oster     for (i = 1; i < (raidPtr->numSectorsPerLog / 2); i++)
    155  1.1  oster       {
    156  1.1  oster 	if (((totalLogCapacity / (rf_numParityRegions + i)) % raidPtr->numSectorsPerLog) < fragmentation)
    157  1.1  oster 	  {
    158  1.1  oster 	    rf_numParityRegions++;
    159  1.1  oster 	    raidPtr->regionLogCapacity = totalLogCapacity / rf_numParityRegions;
    160  1.1  oster 	    fragmentation = raidPtr->regionLogCapacity % raidPtr->numSectorsPerLog;
    161  1.1  oster 	  }
    162  1.1  oster 	if (((totalLogCapacity / (rf_numParityRegions - i)) % raidPtr->numSectorsPerLog) < fragmentation)
    163  1.1  oster 	  {
    164  1.1  oster 	    rf_numParityRegions--;
    165  1.1  oster 	    raidPtr->regionLogCapacity = totalLogCapacity / rf_numParityRegions;
    166  1.1  oster 	    fragmentation = raidPtr->regionLogCapacity % raidPtr->numSectorsPerLog;
    167  1.1  oster 	  }
    168  1.1  oster       }
    169  1.1  oster   /* ensure integral number of regions per log */
    170  1.1  oster   raidPtr->regionLogCapacity = (raidPtr->regionLogCapacity / raidPtr->numSectorsPerLog) * raidPtr->numSectorsPerLog;
    171  1.1  oster 
    172  1.1  oster   raidPtr->numParityLogs = rf_totalInCoreLogCapacity / (raidPtr->bytesPerSector * raidPtr->numSectorsPerLog);
    173  1.1  oster   /* to avoid deadlock, must ensure that enough logs exist for each region to have one simultaneously */
    174  1.1  oster   if (raidPtr->numParityLogs < rf_numParityRegions)
    175  1.1  oster     raidPtr->numParityLogs = rf_numParityRegions;
    176  1.1  oster 
    177  1.1  oster   /* create region information structs */
    178  1.1  oster   RF_Malloc(raidPtr->regionInfo, (rf_numParityRegions * sizeof(RF_RegionInfo_t)), (RF_RegionInfo_t *));
    179  1.1  oster   if (raidPtr->regionInfo == NULL)
    180  1.1  oster     return(ENOMEM);
    181  1.1  oster 
    182  1.1  oster   /* last region may not be full capacity */
    183  1.1  oster   lastRegionCapacity = raidPtr->regionLogCapacity;
    184  1.1  oster   while ((rf_numParityRegions - 1) * raidPtr->regionLogCapacity + lastRegionCapacity > totalLogCapacity)
    185  1.1  oster     lastRegionCapacity = lastRegionCapacity - raidPtr->numSectorsPerLog;
    186  1.1  oster 
    187  1.1  oster   raidPtr->regionParityRange = raidPtr->sectorsPerDisk / rf_numParityRegions;
    188  1.1  oster   maxRegionParityRange = raidPtr->regionParityRange;
    189  1.1  oster 
    190  1.1  oster /* i can't remember why this line is in the code -wvcii 6/30/95 */
    191  1.1  oster /*  if (raidPtr->sectorsPerDisk % rf_numParityRegions > 0)
    192  1.1  oster     regionParityRange++; */
    193  1.1  oster 
    194  1.1  oster   /* build pool of unused parity logs */
    195  1.1  oster   RF_Malloc(raidPtr->parityLogBufferHeap, raidPtr->numParityLogs * raidPtr->numSectorsPerLog * raidPtr->bytesPerSector, (caddr_t));
    196  1.1  oster   if (raidPtr->parityLogBufferHeap == NULL)
    197  1.1  oster     return(ENOMEM);
    198  1.1  oster   lHeapPtr = raidPtr->parityLogBufferHeap;
    199  1.1  oster   rc = rf_mutex_init(&raidPtr->parityLogPool.mutex);
    200  1.1  oster   if (rc) {
    201  1.1  oster     RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    202  1.1  oster       __LINE__, rc);
    203  1.1  oster     RF_Free(raidPtr->parityLogBufferHeap, raidPtr->numParityLogs * raidPtr->numSectorsPerLog * raidPtr->bytesPerSector);
    204  1.1  oster     return(ENOMEM);
    205  1.1  oster   }
    206  1.1  oster   for (i = 0; i < raidPtr->numParityLogs; i++)
    207  1.1  oster     {
    208  1.1  oster       if (i == 0)
    209  1.1  oster 	{
    210  1.1  oster 	  RF_Calloc(raidPtr->parityLogPool.parityLogs, 1, sizeof(RF_ParityLog_t), (RF_ParityLog_t *));
    211  1.1  oster           if (raidPtr->parityLogPool.parityLogs == NULL) {
    212  1.1  oster             RF_Free(raidPtr->parityLogBufferHeap, raidPtr->numParityLogs * raidPtr->numSectorsPerLog * raidPtr->bytesPerSector);
    213  1.1  oster             return(ENOMEM);
    214  1.1  oster           }
    215  1.1  oster 	  l = raidPtr->parityLogPool.parityLogs;
    216  1.1  oster 	}
    217  1.1  oster       else
    218  1.1  oster 	{
    219  1.1  oster 	  RF_Calloc(l->next, 1, sizeof(RF_ParityLog_t), (RF_ParityLog_t *));
    220  1.1  oster           if (l->next == NULL) {
    221  1.1  oster             RF_Free(raidPtr->parityLogBufferHeap, raidPtr->numParityLogs * raidPtr->numSectorsPerLog * raidPtr->bytesPerSector);
    222  1.1  oster             for(l=raidPtr->parityLogPool.parityLogs;l;l=next) {
    223  1.1  oster               next = l->next;
    224  1.1  oster               if (l->records)
    225  1.1  oster                 RF_Free(l->records, (raidPtr->numSectorsPerLog * sizeof(RF_ParityLogRecord_t)));
    226  1.1  oster               RF_Free(l, sizeof(RF_ParityLog_t));
    227  1.1  oster             }
    228  1.1  oster             return(ENOMEM);
    229  1.1  oster           }
    230  1.1  oster 	  l = l->next;
    231  1.1  oster 	}
    232  1.1  oster       l->bufPtr = lHeapPtr;
    233  1.1  oster       lHeapPtr += raidPtr->numSectorsPerLog * raidPtr->bytesPerSector;
    234  1.1  oster       RF_Malloc(l->records, (raidPtr->numSectorsPerLog * sizeof(RF_ParityLogRecord_t)), (RF_ParityLogRecord_t *));
    235  1.1  oster       if (l->records == NULL) {
    236  1.1  oster         RF_Free(raidPtr->parityLogBufferHeap, raidPtr->numParityLogs * raidPtr->numSectorsPerLog * raidPtr->bytesPerSector);
    237  1.1  oster         for(l=raidPtr->parityLogPool.parityLogs;l;l=next) {
    238  1.1  oster           next = l->next;
    239  1.1  oster           if (l->records)
    240  1.1  oster             RF_Free(l->records, (raidPtr->numSectorsPerLog * sizeof(RF_ParityLogRecord_t)));
    241  1.1  oster           RF_Free(l, sizeof(RF_ParityLog_t));
    242  1.1  oster         }
    243  1.1  oster         return(ENOMEM);
    244  1.1  oster       }
    245  1.1  oster     }
    246  1.1  oster   rc = rf_ShutdownCreate(listp, rf_ShutdownParityLoggingPool, raidPtr);
    247  1.1  oster   if (rc) {
    248  1.1  oster     RF_ERRORMSG3("Unable to create shutdown entry file %s line %d rc=%d\n", __FILE__,
    249  1.1  oster       __LINE__, rc);
    250  1.1  oster     rf_ShutdownParityLoggingPool(raidPtr);
    251  1.1  oster     return(rc);
    252  1.1  oster   }
    253  1.1  oster 
    254  1.1  oster   /* build pool of region buffers */
    255  1.1  oster   rc = rf_mutex_init(&raidPtr->regionBufferPool.mutex);
    256  1.1  oster   if (rc) {
    257  1.1  oster     RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    258  1.1  oster       __LINE__, rc);
    259  1.1  oster     return(ENOMEM);
    260  1.1  oster   }
    261  1.1  oster   rc = rf_cond_init(&raidPtr->regionBufferPool.cond);
    262  1.1  oster   if (rc) {
    263  1.1  oster     RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
    264  1.1  oster       __LINE__, rc);
    265  1.1  oster     rf_mutex_destroy(&raidPtr->regionBufferPool.mutex);
    266  1.1  oster     return(ENOMEM);
    267  1.1  oster   }
    268  1.1  oster   raidPtr->regionBufferPool.bufferSize = raidPtr->regionLogCapacity * raidPtr->bytesPerSector;
    269  1.1  oster   printf("regionBufferPool.bufferSize %d\n",raidPtr->regionBufferPool.bufferSize);
    270  1.1  oster   raidPtr->regionBufferPool.totalBuffers = 1;  /* for now, only one region at a time may be reintegrated */
    271  1.1  oster   raidPtr->regionBufferPool.availableBuffers = raidPtr->regionBufferPool.totalBuffers;
    272  1.1  oster   raidPtr->regionBufferPool.availBuffersIndex = 0;
    273  1.1  oster   raidPtr->regionBufferPool.emptyBuffersIndex = 0;
    274  1.1  oster   RF_Malloc(raidPtr->regionBufferPool.buffers, raidPtr->regionBufferPool.totalBuffers * sizeof(caddr_t), (caddr_t *));
    275  1.1  oster   if (raidPtr->regionBufferPool.buffers == NULL) {
    276  1.1  oster     rf_mutex_destroy(&raidPtr->regionBufferPool.mutex);
    277  1.1  oster     rf_cond_destroy(&raidPtr->regionBufferPool.cond);
    278  1.1  oster     return(ENOMEM);
    279  1.1  oster   }
    280  1.1  oster   for (i = 0; i < raidPtr->regionBufferPool.totalBuffers; i++) {
    281  1.1  oster     RF_Malloc(raidPtr->regionBufferPool.buffers[i], raidPtr->regionBufferPool.bufferSize * sizeof(char), (caddr_t));
    282  1.1  oster     if (raidPtr->regionBufferPool.buffers == NULL) {
    283  1.1  oster       rf_mutex_destroy(&raidPtr->regionBufferPool.mutex);
    284  1.1  oster       rf_cond_destroy(&raidPtr->regionBufferPool.cond);
    285  1.1  oster       for(j=0;j<i;j++) {
    286  1.1  oster         RF_Free(raidPtr->regionBufferPool.buffers[i], raidPtr->regionBufferPool.bufferSize * sizeof(char));
    287  1.1  oster       }
    288  1.1  oster       RF_Free(raidPtr->regionBufferPool.buffers, raidPtr->regionBufferPool.totalBuffers * sizeof(caddr_t));
    289  1.1  oster       return(ENOMEM);
    290  1.1  oster     }
    291  1.1  oster     printf("raidPtr->regionBufferPool.buffers[%d] = %lx\n", i,
    292  1.1  oster 	   (long)raidPtr->regionBufferPool.buffers[i]);
    293  1.1  oster   }
    294  1.1  oster   rc = rf_ShutdownCreate(listp, rf_ShutdownParityLoggingRegionBufferPool, raidPtr);
    295  1.1  oster   if (rc) {
    296  1.1  oster     RF_ERRORMSG3("Unable to create shutdown entry file %s line %d rc=%d\n", __FILE__,
    297  1.1  oster       __LINE__, rc);
    298  1.1  oster     rf_ShutdownParityLoggingRegionBufferPool(raidPtr);
    299  1.1  oster     return(rc);
    300  1.1  oster   }
    301  1.1  oster 
    302  1.1  oster   /* build pool of parity buffers */
    303  1.1  oster   parityBufferCapacity = maxRegionParityRange;
    304  1.1  oster   rc = rf_mutex_init(&raidPtr->parityBufferPool.mutex);
    305  1.1  oster   if (rc) {
    306  1.1  oster     RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    307  1.1  oster       __LINE__, rc);
    308  1.1  oster     return(rc);
    309  1.1  oster   }
    310  1.1  oster   rc = rf_cond_init(&raidPtr->parityBufferPool.cond);
    311  1.1  oster   if (rc) {
    312  1.1  oster     RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
    313  1.1  oster       __LINE__, rc);
    314  1.1  oster     rf_mutex_destroy(&raidPtr->parityBufferPool.mutex);
    315  1.1  oster     return(ENOMEM);
    316  1.1  oster   }
    317  1.1  oster   raidPtr->parityBufferPool.bufferSize = parityBufferCapacity * raidPtr->bytesPerSector;
    318  1.1  oster   printf("parityBufferPool.bufferSize %d\n",raidPtr->parityBufferPool.bufferSize);
    319  1.1  oster   raidPtr->parityBufferPool.totalBuffers = 1;  /* for now, only one region at a time may be reintegrated */
    320  1.1  oster   raidPtr->parityBufferPool.availableBuffers = raidPtr->parityBufferPool.totalBuffers;
    321  1.1  oster   raidPtr->parityBufferPool.availBuffersIndex = 0;
    322  1.1  oster   raidPtr->parityBufferPool.emptyBuffersIndex = 0;
    323  1.1  oster   RF_Malloc(raidPtr->parityBufferPool.buffers, raidPtr->parityBufferPool.totalBuffers * sizeof(caddr_t), (caddr_t *));
    324  1.1  oster   if (raidPtr->parityBufferPool.buffers == NULL) {
    325  1.1  oster     rf_mutex_destroy(&raidPtr->parityBufferPool.mutex);
    326  1.1  oster     rf_cond_destroy(&raidPtr->parityBufferPool.cond);
    327  1.1  oster     return(ENOMEM);
    328  1.1  oster   }
    329  1.1  oster   for (i = 0; i < raidPtr->parityBufferPool.totalBuffers; i++) {
    330  1.1  oster     RF_Malloc(raidPtr->parityBufferPool.buffers[i], raidPtr->parityBufferPool.bufferSize * sizeof(char), (caddr_t));
    331  1.1  oster     if (raidPtr->parityBufferPool.buffers == NULL) {
    332  1.1  oster       rf_mutex_destroy(&raidPtr->parityBufferPool.mutex);
    333  1.1  oster       rf_cond_destroy(&raidPtr->parityBufferPool.cond);
    334  1.1  oster       for(j=0;j<i;j++) {
    335  1.1  oster         RF_Free(raidPtr->parityBufferPool.buffers[i], raidPtr->regionBufferPool.bufferSize * sizeof(char));
    336  1.1  oster       }
    337  1.1  oster       RF_Free(raidPtr->parityBufferPool.buffers, raidPtr->regionBufferPool.totalBuffers * sizeof(caddr_t));
    338  1.1  oster       return(ENOMEM);
    339  1.1  oster     }
    340  1.1  oster     printf("parityBufferPool.buffers[%d] = %lx\n", i,
    341  1.1  oster 	   (long)raidPtr->parityBufferPool.buffers[i]);
    342  1.1  oster   }
    343  1.1  oster   rc = rf_ShutdownCreate(listp, rf_ShutdownParityLoggingParityBufferPool, raidPtr);
    344  1.1  oster   if (rc) {
    345  1.1  oster     RF_ERRORMSG3("Unable to create shutdown entry file %s line %d rc=%d\n", __FILE__,
    346  1.1  oster       __LINE__, rc);
    347  1.1  oster     rf_ShutdownParityLoggingParityBufferPool(raidPtr);
    348  1.1  oster     return(rc);
    349  1.1  oster   }
    350  1.1  oster 
    351  1.1  oster   /* initialize parityLogDiskQueue */
    352  1.1  oster   rc = rf_create_managed_mutex(listp, &raidPtr->parityLogDiskQueue.mutex);
    353  1.1  oster   if (rc) {
    354  1.1  oster     RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    355  1.1  oster       __LINE__, rc);
    356  1.1  oster     return(rc);
    357  1.1  oster   }
    358  1.1  oster   rc = rf_create_managed_cond(listp, &raidPtr->parityLogDiskQueue.cond);
    359  1.1  oster   if (rc) {
    360  1.1  oster     RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
    361  1.1  oster       __LINE__, rc);
    362  1.1  oster     return(rc);
    363  1.1  oster   }
    364  1.1  oster   raidPtr->parityLogDiskQueue.flushQueue = NULL;
    365  1.1  oster   raidPtr->parityLogDiskQueue.reintQueue = NULL;
    366  1.1  oster   raidPtr->parityLogDiskQueue.bufHead = NULL;
    367  1.1  oster   raidPtr->parityLogDiskQueue.bufTail = NULL;
    368  1.1  oster   raidPtr->parityLogDiskQueue.reintHead = NULL;
    369  1.1  oster   raidPtr->parityLogDiskQueue.reintTail = NULL;
    370  1.1  oster   raidPtr->parityLogDiskQueue.logBlockHead = NULL;
    371  1.1  oster   raidPtr->parityLogDiskQueue.logBlockTail = NULL;
    372  1.1  oster   raidPtr->parityLogDiskQueue.reintBlockHead = NULL;
    373  1.1  oster   raidPtr->parityLogDiskQueue.reintBlockTail = NULL;
    374  1.1  oster   raidPtr->parityLogDiskQueue.freeDataList = NULL;
    375  1.1  oster   raidPtr->parityLogDiskQueue.freeCommonList = NULL;
    376  1.1  oster 
    377  1.1  oster   rc = rf_ShutdownCreate(listp, rf_ShutdownParityLoggingDiskQueue, raidPtr);
    378  1.1  oster   if (rc) {
    379  1.1  oster     RF_ERRORMSG3("Unable to create shutdown entry file %s line %d rc=%d\n", __FILE__,
    380  1.1  oster       __LINE__, rc);
    381  1.1  oster     return(rc);
    382  1.1  oster   }
    383  1.1  oster 
    384  1.1  oster   for (i = 0; i < rf_numParityRegions; i++)
    385  1.1  oster     {
    386  1.1  oster       rc = rf_mutex_init(&raidPtr->regionInfo[i].mutex);
    387  1.1  oster       if (rc) {
    388  1.1  oster         RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    389  1.1  oster           __LINE__, rc);
    390  1.1  oster         for(j=0;j<i;j++)
    391  1.1  oster           FreeRegionInfo(raidPtr, j);
    392  1.1  oster         RF_Free(raidPtr->regionInfo, (rf_numParityRegions * sizeof(RF_RegionInfo_t)));
    393  1.1  oster         return(ENOMEM);
    394  1.1  oster       }
    395  1.1  oster       rc = rf_mutex_init(&raidPtr->regionInfo[i].reintMutex);
    396  1.1  oster       if (rc) {
    397  1.1  oster         RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    398  1.1  oster           __LINE__, rc);
    399  1.1  oster         rf_mutex_destroy(&raidPtr->regionInfo[i].mutex);
    400  1.1  oster         for(j=0;j<i;j++)
    401  1.1  oster           FreeRegionInfo(raidPtr, j);
    402  1.1  oster         RF_Free(raidPtr->regionInfo, (rf_numParityRegions * sizeof(RF_RegionInfo_t)));
    403  1.1  oster         return(ENOMEM);
    404  1.1  oster       }
    405  1.1  oster       raidPtr->regionInfo[i].reintInProgress = RF_FALSE;
    406  1.1  oster       raidPtr->regionInfo[i].regionStartAddr = raidPtr->regionLogCapacity * i;
    407  1.1  oster       raidPtr->regionInfo[i].parityStartAddr = raidPtr->regionParityRange * i;
    408  1.1  oster       if (i < rf_numParityRegions - 1)
    409  1.1  oster 	{
    410  1.1  oster 	  raidPtr->regionInfo[i].capacity = raidPtr->regionLogCapacity;
    411  1.1  oster 	  raidPtr->regionInfo[i].numSectorsParity = raidPtr->regionParityRange;
    412  1.1  oster 	}
    413  1.1  oster       else
    414  1.1  oster 	{
    415  1.1  oster 	  raidPtr->regionInfo[i].capacity = lastRegionCapacity;
    416  1.1  oster 	  raidPtr->regionInfo[i].numSectorsParity = raidPtr->sectorsPerDisk - raidPtr->regionParityRange * i;
    417  1.1  oster 	  if (raidPtr->regionInfo[i].numSectorsParity > maxRegionParityRange)
    418  1.1  oster 	    maxRegionParityRange = raidPtr->regionInfo[i].numSectorsParity;
    419  1.1  oster 	}
    420  1.1  oster       raidPtr->regionInfo[i].diskCount = 0;
    421  1.1  oster       RF_ASSERT(raidPtr->regionInfo[i].capacity + raidPtr->regionInfo[i].regionStartAddr <= totalLogCapacity);
    422  1.1  oster       RF_ASSERT(raidPtr->regionInfo[i].parityStartAddr + raidPtr->regionInfo[i].numSectorsParity <= raidPtr->sectorsPerDisk);
    423  1.1  oster       RF_Malloc(raidPtr->regionInfo[i].diskMap, (raidPtr->regionInfo[i].capacity * sizeof(RF_DiskMap_t)), (RF_DiskMap_t *));
    424  1.1  oster       if (raidPtr->regionInfo[i].diskMap == NULL) {
    425  1.1  oster         rf_mutex_destroy(&raidPtr->regionInfo[i].mutex);
    426  1.1  oster         rf_mutex_destroy(&raidPtr->regionInfo[i].reintMutex);
    427  1.1  oster         for(j=0;j<i;j++)
    428  1.1  oster           FreeRegionInfo(raidPtr, j);
    429  1.1  oster         RF_Free(raidPtr->regionInfo, (rf_numParityRegions * sizeof(RF_RegionInfo_t)));
    430  1.1  oster         return(ENOMEM);
    431  1.1  oster       }
    432  1.1  oster       raidPtr->regionInfo[i].loggingEnabled = RF_FALSE;
    433  1.1  oster       raidPtr->regionInfo[i].coreLog = NULL;
    434  1.1  oster     }
    435  1.1  oster   rc = rf_ShutdownCreate(listp, rf_ShutdownParityLoggingRegionInfo, raidPtr);
    436  1.1  oster   if (rc) {
    437  1.1  oster     RF_ERRORMSG3("Unable to create shutdown entry file %s line %d rc=%d\n", __FILE__,
    438  1.1  oster       __LINE__, rc);
    439  1.1  oster     rf_ShutdownParityLoggingRegionInfo(raidPtr);
    440  1.1  oster     return(rc);
    441  1.1  oster   }
    442  1.1  oster 
    443  1.1  oster   RF_ASSERT(raidPtr->parityLogDiskQueue.threadState == 0);
    444  1.1  oster   raidPtr->parityLogDiskQueue.threadState = RF_PLOG_CREATED;
    445  1.1  oster   rc = RF_CREATE_THREAD(raidPtr->pLogDiskThreadHandle, rf_ParityLoggingDiskManager, raidPtr);
    446  1.1  oster   if (rc) {
    447  1.1  oster     raidPtr->parityLogDiskQueue.threadState = 0;
    448  1.1  oster     RF_ERRORMSG3("Unable to create parity logging disk thread file %s line %d rc=%d\n",
    449  1.1  oster       __FILE__, __LINE__, rc);
    450  1.1  oster     return(ENOMEM);
    451  1.1  oster   }
    452  1.1  oster   /* wait for thread to start */
    453  1.1  oster   RF_LOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    454  1.1  oster   while(!(raidPtr->parityLogDiskQueue.threadState&RF_PLOG_RUNNING)) {
    455  1.1  oster     RF_WAIT_COND(raidPtr->parityLogDiskQueue.cond, raidPtr->parityLogDiskQueue.mutex);
    456  1.1  oster   }
    457  1.1  oster   RF_UNLOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    458  1.1  oster 
    459  1.1  oster   rc = rf_ShutdownCreate(listp, rf_ShutdownParityLogging, raidPtr);
    460  1.1  oster   if (rc) {
    461  1.1  oster     RF_ERRORMSG1("Got rc=%d adding parity logging shutdown event\n", rc);
    462  1.1  oster     rf_ShutdownParityLogging(raidPtr);
    463  1.1  oster     return(rc);
    464  1.1  oster   }
    465  1.1  oster 
    466  1.1  oster   if (rf_parityLogDebug)
    467  1.1  oster     {
    468  1.1  oster       printf("                            size of disk log in sectors: %d\n",
    469  1.1  oster 	     (int)totalLogCapacity);
    470  1.1  oster       printf("                            total number of parity regions is %d\n", (int)rf_numParityRegions);
    471  1.1  oster       printf("                            nominal sectors of log per parity region is %d\n", (int)raidPtr->regionLogCapacity);
    472  1.1  oster       printf("                            nominal region fragmentation is %d sectors\n",(int)fragmentation);
    473  1.1  oster       printf("                            total number of parity logs is %d\n", raidPtr->numParityLogs);
    474  1.1  oster       printf("                            parity log size is %d sectors\n", raidPtr->numSectorsPerLog);
    475  1.1  oster       printf("                            total in-core log space is %d bytes\n", (int) rf_totalInCoreLogCapacity);
    476  1.1  oster     }
    477  1.1  oster 
    478  1.1  oster   rf_EnableParityLogging(raidPtr);
    479  1.1  oster 
    480  1.1  oster   return(0);
    481  1.1  oster }
    482  1.1  oster 
    483  1.1  oster static void FreeRegionInfo(
    484  1.1  oster   RF_Raid_t      *raidPtr,
    485  1.1  oster   RF_RegionId_t   regionID)
    486  1.1  oster {
    487  1.1  oster   RF_LOCK_MUTEX(raidPtr->regionInfo[regionID].mutex);
    488  1.1  oster   RF_Free(raidPtr->regionInfo[regionID].diskMap, (raidPtr->regionInfo[regionID].capacity * sizeof(RF_DiskMap_t)));
    489  1.1  oster   if (!rf_forceParityLogReint && raidPtr->regionInfo[regionID].coreLog) {
    490  1.1  oster     rf_ReleaseParityLogs(raidPtr, raidPtr->regionInfo[regionID].coreLog);
    491  1.1  oster     raidPtr->regionInfo[regionID].coreLog = NULL;
    492  1.1  oster   }
    493  1.1  oster   else {
    494  1.1  oster     RF_ASSERT(raidPtr->regionInfo[regionID].coreLog == NULL);
    495  1.1  oster     RF_ASSERT(raidPtr->regionInfo[regionID].diskCount == 0);
    496  1.1  oster   }
    497  1.1  oster   RF_UNLOCK_MUTEX(raidPtr->regionInfo[regionID].mutex);
    498  1.1  oster   rf_mutex_destroy(&raidPtr->regionInfo[regionID].mutex);
    499  1.1  oster   rf_mutex_destroy(&raidPtr->regionInfo[regionID].reintMutex);
    500  1.1  oster }
    501  1.1  oster 
    502  1.1  oster 
    503  1.1  oster static void FreeParityLogQueue(
    504  1.1  oster   RF_Raid_t            *raidPtr,
    505  1.1  oster   RF_ParityLogQueue_t  *queue)
    506  1.1  oster {
    507  1.1  oster   RF_ParityLog_t *l1, *l2;
    508  1.1  oster 
    509  1.1  oster   RF_LOCK_MUTEX(queue->mutex);
    510  1.1  oster   l1 = queue->parityLogs;
    511  1.1  oster   while (l1)
    512  1.1  oster     {
    513  1.1  oster       l2 = l1;
    514  1.1  oster       l1 = l2->next;
    515  1.1  oster       RF_Free(l2->records, (raidPtr->numSectorsPerLog * sizeof(RF_ParityLogRecord_t)));
    516  1.1  oster       RF_Free(l2, sizeof(RF_ParityLog_t));
    517  1.1  oster     }
    518  1.1  oster   RF_UNLOCK_MUTEX(queue->mutex);
    519  1.1  oster   rf_mutex_destroy(&queue->mutex);
    520  1.1  oster }
    521  1.1  oster 
    522  1.1  oster 
    523  1.1  oster static void FreeRegionBufferQueue(RF_RegionBufferQueue_t *queue)
    524  1.1  oster {
    525  1.1  oster   int i;
    526  1.1  oster 
    527  1.1  oster   RF_LOCK_MUTEX(queue->mutex);
    528  1.1  oster   if (queue->availableBuffers != queue->totalBuffers)
    529  1.1  oster     {
    530  1.1  oster       printf("Attempt to free region queue which is still in use!\n");
    531  1.1  oster       RF_ASSERT(0);
    532  1.1  oster     }
    533  1.1  oster   for (i = 0; i < queue->totalBuffers; i++)
    534  1.1  oster     RF_Free(queue->buffers[i], queue->bufferSize);
    535  1.1  oster   RF_Free(queue->buffers, queue->totalBuffers * sizeof(caddr_t));
    536  1.1  oster   RF_UNLOCK_MUTEX(queue->mutex);
    537  1.1  oster   rf_mutex_destroy(&queue->mutex);
    538  1.1  oster }
    539  1.1  oster 
    540  1.1  oster static void rf_ShutdownParityLoggingRegionInfo(RF_ThreadArg_t arg)
    541  1.1  oster {
    542  1.1  oster   RF_Raid_t *raidPtr;
    543  1.1  oster   RF_RegionId_t i;
    544  1.1  oster 
    545  1.1  oster   raidPtr = (RF_Raid_t *)arg;
    546  1.1  oster   if (rf_parityLogDebug) {
    547  1.1  oster     int tid;
    548  1.1  oster     rf_get_threadid(tid);
    549  1.1  oster     printf("[%d] ShutdownParityLoggingRegionInfo\n", tid);
    550  1.1  oster   }
    551  1.1  oster   /* free region information structs */
    552  1.1  oster   for (i = 0; i < rf_numParityRegions; i++)
    553  1.1  oster     FreeRegionInfo(raidPtr, i);
    554  1.1  oster   RF_Free(raidPtr->regionInfo, (rf_numParityRegions * sizeof(raidPtr->regionInfo)));
    555  1.1  oster   raidPtr->regionInfo = NULL;
    556  1.1  oster }
    557  1.1  oster 
    558  1.1  oster static void rf_ShutdownParityLoggingPool(RF_ThreadArg_t arg)
    559  1.1  oster {
    560  1.1  oster   RF_Raid_t *raidPtr;
    561  1.1  oster 
    562  1.1  oster   raidPtr = (RF_Raid_t *)arg;
    563  1.1  oster   if (rf_parityLogDebug) {
    564  1.1  oster     int tid;
    565  1.1  oster     rf_get_threadid(tid);
    566  1.1  oster     printf("[%d] ShutdownParityLoggingPool\n", tid);
    567  1.1  oster   }
    568  1.1  oster   /* free contents of parityLogPool */
    569  1.1  oster   FreeParityLogQueue(raidPtr, &raidPtr->parityLogPool);
    570  1.1  oster   RF_Free(raidPtr->parityLogBufferHeap, raidPtr->numParityLogs * raidPtr->numSectorsPerLog * raidPtr->bytesPerSector);
    571  1.1  oster }
    572  1.1  oster 
    573  1.1  oster static void rf_ShutdownParityLoggingRegionBufferPool(RF_ThreadArg_t arg)
    574  1.1  oster {
    575  1.1  oster   RF_Raid_t *raidPtr;
    576  1.1  oster 
    577  1.1  oster   raidPtr = (RF_Raid_t *)arg;
    578  1.1  oster   if (rf_parityLogDebug) {
    579  1.1  oster     int tid;
    580  1.1  oster     rf_get_threadid(tid);
    581  1.1  oster     printf("[%d] ShutdownParityLoggingRegionBufferPool\n", tid);
    582  1.1  oster   }
    583  1.1  oster   FreeRegionBufferQueue(&raidPtr->regionBufferPool);
    584  1.1  oster }
    585  1.1  oster 
    586  1.1  oster static void rf_ShutdownParityLoggingParityBufferPool(RF_ThreadArg_t arg)
    587  1.1  oster {
    588  1.1  oster   RF_Raid_t *raidPtr;
    589  1.1  oster 
    590  1.1  oster   raidPtr = (RF_Raid_t *)arg;
    591  1.1  oster   if (rf_parityLogDebug) {
    592  1.1  oster     int tid;
    593  1.1  oster     rf_get_threadid(tid);
    594  1.1  oster     printf("[%d] ShutdownParityLoggingParityBufferPool\n", tid);
    595  1.1  oster   }
    596  1.1  oster   FreeRegionBufferQueue(&raidPtr->parityBufferPool);
    597  1.1  oster }
    598  1.1  oster 
    599  1.1  oster static void rf_ShutdownParityLoggingDiskQueue(RF_ThreadArg_t arg)
    600  1.1  oster {
    601  1.1  oster   RF_ParityLogData_t *d;
    602  1.1  oster   RF_CommonLogData_t *c;
    603  1.1  oster   RF_Raid_t *raidPtr;
    604  1.1  oster 
    605  1.1  oster   raidPtr = (RF_Raid_t *)arg;
    606  1.1  oster   if (rf_parityLogDebug) {
    607  1.1  oster     int tid;
    608  1.1  oster     rf_get_threadid(tid);
    609  1.1  oster     printf("[%d] ShutdownParityLoggingDiskQueue\n", tid);
    610  1.1  oster   }
    611  1.1  oster   /* free disk manager stuff */
    612  1.1  oster   RF_ASSERT(raidPtr->parityLogDiskQueue.bufHead == NULL);
    613  1.1  oster   RF_ASSERT(raidPtr->parityLogDiskQueue.bufTail == NULL);
    614  1.1  oster   RF_ASSERT(raidPtr->parityLogDiskQueue.reintHead == NULL);
    615  1.1  oster   RF_ASSERT(raidPtr->parityLogDiskQueue.reintTail == NULL);
    616  1.1  oster   while (raidPtr->parityLogDiskQueue.freeDataList)
    617  1.1  oster     {
    618  1.1  oster       d = raidPtr->parityLogDiskQueue.freeDataList;
    619  1.1  oster       raidPtr->parityLogDiskQueue.freeDataList = raidPtr->parityLogDiskQueue.freeDataList->next;
    620  1.1  oster       RF_Free(d, sizeof(RF_ParityLogData_t));
    621  1.1  oster     }
    622  1.1  oster   while (raidPtr->parityLogDiskQueue.freeCommonList)
    623  1.1  oster     {
    624  1.1  oster       c = raidPtr->parityLogDiskQueue.freeCommonList;
    625  1.1  oster       rf_mutex_destroy(&c->mutex);
    626  1.1  oster       raidPtr->parityLogDiskQueue.freeCommonList = raidPtr->parityLogDiskQueue.freeCommonList->next;
    627  1.1  oster       RF_Free(c, sizeof(RF_CommonLogData_t));
    628  1.1  oster     }
    629  1.1  oster }
    630  1.1  oster 
    631  1.1  oster static void rf_ShutdownParityLogging(RF_ThreadArg_t arg)
    632  1.1  oster {
    633  1.1  oster   RF_Raid_t *raidPtr;
    634  1.1  oster 
    635  1.1  oster   raidPtr = (RF_Raid_t *)arg;
    636  1.1  oster   if (rf_parityLogDebug) {
    637  1.1  oster     int tid;
    638  1.1  oster     rf_get_threadid(tid);
    639  1.1  oster     printf("[%d] ShutdownParityLogging\n", tid);
    640  1.1  oster   }
    641  1.2  oster 
    642  1.1  oster   /* shutdown disk thread */
    643  1.1  oster   /* This has the desirable side-effect of forcing all regions to be
    644  1.1  oster      reintegrated.  This is necessary since all parity log maps are
    645  1.1  oster      currently held in volatile memory. */
    646  1.1  oster 
    647  1.1  oster   RF_LOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    648  1.1  oster   raidPtr->parityLogDiskQueue.threadState |= RF_PLOG_TERMINATE;
    649  1.1  oster   RF_UNLOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    650  1.1  oster   RF_SIGNAL_COND(raidPtr->parityLogDiskQueue.cond);
    651  1.1  oster   /*
    652  1.1  oster    * pLogDiskThread will now terminate when queues are cleared
    653  1.1  oster    * now wait for it to be done
    654  1.1  oster    */
    655  1.1  oster   RF_LOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    656  1.1  oster   while(!(raidPtr->parityLogDiskQueue.threadState&RF_PLOG_SHUTDOWN)) {
    657  1.1  oster     RF_WAIT_COND(raidPtr->parityLogDiskQueue.cond, raidPtr->parityLogDiskQueue.mutex);
    658  1.1  oster   }
    659  1.1  oster   RF_UNLOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    660  1.1  oster   if (rf_parityLogDebug) {
    661  1.1  oster     int tid;
    662  1.1  oster     rf_get_threadid(tid);
    663  1.1  oster     printf("[%d] ShutdownParityLogging done (thread completed)\n", tid);
    664  1.1  oster   }
    665  1.1  oster }
    666  1.1  oster 
    667  1.1  oster int rf_GetDefaultNumFloatingReconBuffersParityLogging(RF_Raid_t *raidPtr)
    668  1.1  oster {
    669  1.1  oster   return(20);
    670  1.1  oster }
    671  1.1  oster 
    672  1.1  oster RF_HeadSepLimit_t rf_GetDefaultHeadSepLimitParityLogging(RF_Raid_t *raidPtr)
    673  1.1  oster {
    674  1.1  oster   return(10);
    675  1.1  oster }
    676  1.1  oster 
    677  1.1  oster /* return the region ID for a given RAID address */
    678  1.1  oster RF_RegionId_t rf_MapRegionIDParityLogging(
    679  1.1  oster   RF_Raid_t       *raidPtr,
    680  1.1  oster   RF_SectorNum_t   address)
    681  1.1  oster {
    682  1.1  oster   RF_RegionId_t regionID;
    683  1.1  oster 
    684  1.1  oster /*  regionID = address / (raidPtr->regionParityRange * raidPtr->Layout.numDataCol); */
    685  1.1  oster   regionID = address / raidPtr->regionParityRange;
    686  1.1  oster   if (regionID == rf_numParityRegions)
    687  1.1  oster     {
    688  1.1  oster       /* last region may be larger than other regions */
    689  1.1  oster       regionID--;
    690  1.1  oster     }
    691  1.1  oster   RF_ASSERT(address >= raidPtr->regionInfo[regionID].parityStartAddr);
    692  1.1  oster   RF_ASSERT(address < raidPtr->regionInfo[regionID].parityStartAddr + raidPtr->regionInfo[regionID].numSectorsParity);
    693  1.1  oster   RF_ASSERT(regionID < rf_numParityRegions);
    694  1.1  oster   return(regionID);
    695  1.1  oster }
    696  1.1  oster 
    697  1.1  oster 
    698  1.1  oster /* given a logical RAID sector, determine physical disk address of data */
    699  1.1  oster void rf_MapSectorParityLogging(
    700  1.1  oster   RF_Raid_t         *raidPtr,
    701  1.1  oster   RF_RaidAddr_t      raidSector,
    702  1.1  oster   RF_RowCol_t       *row,
    703  1.1  oster   RF_RowCol_t       *col,
    704  1.1  oster   RF_SectorNum_t    *diskSector,
    705  1.1  oster   int                remap)
    706  1.1  oster {
    707  1.1  oster   RF_StripeNum_t SUID = raidSector / raidPtr->Layout.sectorsPerStripeUnit;
    708  1.1  oster   *row = 0;
    709  1.1  oster   /* *col = (SUID % (raidPtr->numCol - raidPtr->Layout.numParityLogCol)); */
    710  1.1  oster   *col = SUID % raidPtr->Layout.numDataCol;
    711  1.1  oster   *diskSector = (SUID / (raidPtr->Layout.numDataCol)) * raidPtr->Layout.sectorsPerStripeUnit +
    712  1.1  oster     (raidSector % raidPtr->Layout.sectorsPerStripeUnit);
    713  1.1  oster }
    714  1.1  oster 
    715  1.1  oster 
    716  1.1  oster /* given a logical RAID sector, determine physical disk address of parity  */
    717  1.1  oster void rf_MapParityParityLogging(
    718  1.1  oster   RF_Raid_t       *raidPtr,
    719  1.1  oster   RF_RaidAddr_t    raidSector,
    720  1.1  oster   RF_RowCol_t     *row,
    721  1.1  oster   RF_RowCol_t     *col,
    722  1.1  oster   RF_SectorNum_t  *diskSector,
    723  1.1  oster   int              remap)
    724  1.1  oster {
    725  1.1  oster   RF_StripeNum_t SUID = raidSector / raidPtr->Layout.sectorsPerStripeUnit;
    726  1.1  oster 
    727  1.1  oster   *row = 0;
    728  1.1  oster   /* *col = raidPtr->Layout.numDataCol-(SUID/raidPtr->Layout.numDataCol)%(raidPtr->numCol - raidPtr->Layout.numParityLogCol); */
    729  1.1  oster   *col = raidPtr->Layout.numDataCol;
    730  1.1  oster   *diskSector =(SUID / (raidPtr->Layout.numDataCol)) * raidPtr->Layout.sectorsPerStripeUnit +
    731  1.1  oster     (raidSector % raidPtr->Layout.sectorsPerStripeUnit);
    732  1.1  oster }
    733  1.1  oster 
    734  1.1  oster 
    735  1.1  oster /* given a regionID and sector offset, determine the physical disk address of the parity log */
    736  1.1  oster void rf_MapLogParityLogging(
    737  1.1  oster   RF_Raid_t       *raidPtr,
    738  1.1  oster   RF_RegionId_t    regionID,
    739  1.1  oster   RF_SectorNum_t   regionOffset,
    740  1.1  oster   RF_RowCol_t     *row,
    741  1.1  oster   RF_RowCol_t     *col,
    742  1.1  oster   RF_SectorNum_t  *startSector)
    743  1.1  oster {
    744  1.1  oster   *row = 0;
    745  1.1  oster   *col = raidPtr->numCol - 1;
    746  1.1  oster   *startSector = raidPtr->regionInfo[regionID].regionStartAddr + regionOffset;
    747  1.1  oster }
    748  1.1  oster 
    749  1.1  oster 
    750  1.1  oster /* given a regionID, determine the physical disk address of the logged parity for that region */
    751  1.1  oster void rf_MapRegionParity(
    752  1.1  oster   RF_Raid_t         *raidPtr,
    753  1.1  oster   RF_RegionId_t      regionID,
    754  1.1  oster   RF_RowCol_t       *row,
    755  1.1  oster   RF_RowCol_t       *col,
    756  1.1  oster   RF_SectorNum_t    *startSector,
    757  1.1  oster   RF_SectorCount_t  *numSector)
    758  1.1  oster {
    759  1.1  oster   *row = 0;
    760  1.1  oster   *col = raidPtr->numCol - 2;
    761  1.1  oster   *startSector = raidPtr->regionInfo[regionID].parityStartAddr;
    762  1.1  oster   *numSector = raidPtr->regionInfo[regionID].numSectorsParity;
    763  1.1  oster }
    764  1.1  oster 
    765  1.1  oster 
    766  1.1  oster /* given a logical RAID address, determine the participating disks in the stripe */
    767  1.1  oster void rf_IdentifyStripeParityLogging(
    768  1.1  oster   RF_Raid_t        *raidPtr,
    769  1.1  oster   RF_RaidAddr_t     addr,
    770  1.1  oster   RF_RowCol_t     **diskids,
    771  1.1  oster   RF_RowCol_t      *outRow)
    772  1.1  oster {
    773  1.1  oster   RF_StripeNum_t stripeID = rf_RaidAddressToStripeID(&raidPtr->Layout, addr);
    774  1.1  oster   RF_ParityLoggingConfigInfo_t *info = (RF_ParityLoggingConfigInfo_t *) raidPtr->Layout.layoutSpecificInfo;
    775  1.1  oster   *outRow = 0;
    776  1.1  oster   *diskids = info->stripeIdentifier[ stripeID % raidPtr->numCol ];
    777  1.1  oster }
    778  1.1  oster 
    779  1.1  oster 
    780  1.1  oster void rf_MapSIDToPSIDParityLogging(
    781  1.1  oster   RF_RaidLayout_t    *layoutPtr,
    782  1.1  oster   RF_StripeNum_t      stripeID,
    783  1.1  oster   RF_StripeNum_t     *psID,
    784  1.1  oster   RF_ReconUnitNum_t  *which_ru)
    785  1.1  oster {
    786  1.1  oster   *which_ru = 0;
    787  1.1  oster   *psID = stripeID;
    788  1.1  oster }
    789  1.1  oster 
    790  1.1  oster 
    791  1.1  oster /* select an algorithm for performing an access.  Returns two pointers,
    792  1.1  oster  * one to a function that will return information about the DAG, and
    793  1.1  oster  * another to a function that will create the dag.
    794  1.1  oster  */
    795  1.1  oster void rf_ParityLoggingDagSelect(
    796  1.1  oster   RF_Raid_t             *raidPtr,
    797  1.1  oster   RF_IoType_t            type,
    798  1.1  oster   RF_AccessStripeMap_t  *asmp,
    799  1.1  oster   RF_VoidFuncPtr        *createFunc)
    800  1.1  oster {
    801  1.1  oster   RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout);
    802  1.1  oster   RF_PhysDiskAddr_t *failedPDA=NULL;
    803  1.1  oster   RF_RowCol_t frow, fcol;
    804  1.1  oster   RF_RowStatus_t rstat;
    805  1.1  oster   int prior_recon;
    806  1.1  oster   int tid;
    807  1.1  oster 
    808  1.1  oster   RF_ASSERT(RF_IO_IS_R_OR_W(type));
    809  1.1  oster 
    810  1.1  oster   if (asmp->numDataFailed + asmp->numParityFailed > 1) {
    811  1.1  oster     RF_ERRORMSG("Multiple disks failed in a single group!  Aborting I/O operation.\n");
    812  1.1  oster     /* *infoFunc = */ *createFunc = NULL;
    813  1.1  oster     return;
    814  1.1  oster   } else if (asmp->numDataFailed + asmp->numParityFailed == 1) {
    815  1.1  oster 
    816  1.1  oster     /* if under recon & already reconstructed, redirect the access to the spare drive
    817  1.1  oster      * and eliminate the failure indication
    818  1.1  oster      */
    819  1.1  oster     failedPDA = asmp->failedPDAs[0];
    820  1.1  oster     frow = failedPDA->row; fcol = failedPDA->col;
    821  1.1  oster     rstat = raidPtr->status[failedPDA->row];
    822  1.1  oster     prior_recon = (rstat == rf_rs_reconfigured) || (
    823  1.1  oster       (rstat == rf_rs_reconstructing) ?
    824  1.1  oster       rf_CheckRUReconstructed(raidPtr->reconControl[frow]->reconMap, failedPDA->startSector) : 0
    825  1.1  oster       );
    826  1.1  oster     if (prior_recon) {
    827  1.1  oster       RF_RowCol_t or = failedPDA->row,oc=failedPDA->col;
    828  1.1  oster       RF_SectorNum_t oo=failedPDA->startSector;
    829  1.1  oster       if (layoutPtr->map->flags & RF_DISTRIBUTE_SPARE) {         /* redirect to dist spare space */
    830  1.1  oster 
    831  1.1  oster         if (failedPDA == asmp->parityInfo) {
    832  1.1  oster 
    833  1.1  oster           /* parity has failed */
    834  1.1  oster           (layoutPtr->map->MapParity)(raidPtr, failedPDA->raidAddress, &failedPDA->row,
    835  1.1  oster                                       &failedPDA->col, &failedPDA->startSector, RF_REMAP);
    836  1.1  oster 
    837  1.1  oster           if (asmp->parityInfo->next) {                          /* redir 2nd component, if any */
    838  1.1  oster             RF_PhysDiskAddr_t *p = asmp->parityInfo->next;
    839  1.1  oster             RF_SectorNum_t SUoffs = p->startSector % layoutPtr->sectorsPerStripeUnit;
    840  1.1  oster             p->row = failedPDA->row;
    841  1.1  oster             p->col = failedPDA->col;
    842  1.1  oster             p->startSector = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, failedPDA->startSector) +
    843  1.1  oster                              SUoffs;    /* cheating:  startSector is not really a RAID address */
    844  1.1  oster           }
    845  1.1  oster 
    846  1.1  oster         } else if (asmp->parityInfo->next && failedPDA == asmp->parityInfo->next) {
    847  1.1  oster           RF_ASSERT(0);            /* should not ever happen */
    848  1.1  oster         } else {
    849  1.1  oster 
    850  1.1  oster           /* data has failed */
    851  1.1  oster           (layoutPtr->map->MapSector)(raidPtr, failedPDA->raidAddress, &failedPDA->row,
    852  1.1  oster                                       &failedPDA->col, &failedPDA->startSector, RF_REMAP);
    853  1.1  oster 
    854  1.1  oster         }
    855  1.1  oster 
    856  1.1  oster       } else {                                                 /* redirect to dedicated spare space */
    857  1.1  oster 
    858  1.1  oster         failedPDA->row = raidPtr->Disks[frow][fcol].spareRow;
    859  1.1  oster         failedPDA->col = raidPtr->Disks[frow][fcol].spareCol;
    860  1.1  oster 
    861  1.1  oster         /* the parity may have two distinct components, both of which may need to be redirected */
    862  1.1  oster         if (asmp->parityInfo->next) {
    863  1.1  oster           if (failedPDA == asmp->parityInfo) {
    864  1.1  oster             failedPDA->next->row = failedPDA->row;
    865  1.1  oster             failedPDA->next->col = failedPDA->col;
    866  1.1  oster           } else if (failedPDA == asmp->parityInfo->next) {    /* paranoid:  should never occur */
    867  1.1  oster             asmp->parityInfo->row = failedPDA->row;
    868  1.1  oster             asmp->parityInfo->col = failedPDA->col;
    869  1.1  oster           }
    870  1.1  oster         }
    871  1.1  oster       }
    872  1.1  oster 
    873  1.1  oster       RF_ASSERT(failedPDA->col != -1);
    874  1.1  oster 
    875  1.1  oster       if (rf_dagDebug || rf_mapDebug) {
    876  1.1  oster         rf_get_threadid(tid);
    877  1.1  oster         printf("[%d] Redirected type '%c' r %d c %d o %ld -> r %d c %d o %ld\n",
    878  1.1  oster              tid,type,or,oc,(long)oo,failedPDA->row,failedPDA->col,(long)failedPDA->startSector);
    879  1.1  oster       }
    880  1.1  oster 
    881  1.1  oster       asmp->numDataFailed = asmp->numParityFailed = 0;
    882  1.1  oster     }
    883  1.1  oster 
    884  1.1  oster   }
    885  1.1  oster 
    886  1.1  oster 
    887  1.1  oster   if (type == RF_IO_TYPE_READ) {
    888  1.1  oster 
    889  1.1  oster     if (asmp->numDataFailed == 0)
    890  1.1  oster       *createFunc = (RF_VoidFuncPtr)rf_CreateFaultFreeReadDAG;
    891  1.1  oster     else
    892  1.1  oster       *createFunc = (RF_VoidFuncPtr)rf_CreateRaidFiveDegradedReadDAG;
    893  1.1  oster 
    894  1.1  oster   }
    895  1.1  oster   else {
    896  1.1  oster 
    897  1.1  oster 
    898  1.1  oster     /* if mirroring, always use large writes.  If the access requires two distinct parity updates,
    899  1.1  oster      * always do a small write.  If the stripe contains a failure but the access does not, do a
    900  1.1  oster      * small write.
    901  1.1  oster      * The first conditional (numStripeUnitsAccessed <= numDataCol/2) uses a less-than-or-equal
    902  1.1  oster      * rather than just a less-than because when G is 3 or 4, numDataCol/2 is 1, and I want
    903  1.1  oster      * single-stripe-unit updates to use just one disk.
    904  1.1  oster      */
    905  1.1  oster     if ( (asmp->numDataFailed + asmp->numParityFailed) == 0) {
    906  1.1  oster       if (((asmp->numStripeUnitsAccessed <= (layoutPtr->numDataCol / 2)) && (layoutPtr->numDataCol!=1)) ||
    907  1.1  oster           (asmp->parityInfo->next!=NULL) || rf_CheckStripeForFailures(raidPtr, asmp)) {
    908  1.1  oster 	*createFunc = (RF_VoidFuncPtr)rf_CreateParityLoggingSmallWriteDAG;
    909  1.1  oster       }
    910  1.1  oster       else
    911  1.1  oster         *createFunc = (RF_VoidFuncPtr)rf_CreateParityLoggingLargeWriteDAG;
    912  1.1  oster     }
    913  1.1  oster     else
    914  1.1  oster       if (asmp->numParityFailed == 1)
    915  1.1  oster         *createFunc = (RF_VoidFuncPtr)rf_CreateNonRedundantWriteDAG;
    916  1.1  oster       else
    917  1.1  oster         if (asmp->numStripeUnitsAccessed != 1 && failedPDA->numSector != layoutPtr->sectorsPerStripeUnit)
    918  1.1  oster           *createFunc = NULL;
    919  1.1  oster         else
    920  1.1  oster           *createFunc = (RF_VoidFuncPtr)rf_CreateDegradedWriteDAG;
    921  1.1  oster   }
    922  1.1  oster }
    923  1.1  oster 
    924  1.1  oster #endif /* RF_INCLUDE_PARITYLOGGING > 0 */
    925