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