Home | History | Annotate | Line # | Download | only in raidframe
rf_paritylogDiskMgr.c revision 1.9
      1 /*	$NetBSD: rf_paritylogDiskMgr.c,v 1.9 2000/01/14 04:03:52 oster Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author: William V. Courtright II
      7  *
      8  * Permission to use, copy, modify and distribute this software and
      9  * its documentation is hereby granted, provided that both the copyright
     10  * notice and this permission notice appear in all copies of the
     11  * software, derivative works or modified versions, and any portions
     12  * thereof, and that both notices appear in supporting documentation.
     13  *
     14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  *
     18  * Carnegie Mellon requests users of this software to return to
     19  *
     20  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  *  School of Computer Science
     22  *  Carnegie Mellon University
     23  *  Pittsburgh PA 15213-3890
     24  *
     25  * any improvements or extensions that they make and grant Carnegie the
     26  * rights to redistribute these changes.
     27  */
     28 /* Code for flushing and reintegration operations related to parity logging.
     29  *
     30  */
     31 
     32 #include "rf_archs.h"
     33 
     34 #if RF_INCLUDE_PARITYLOGGING > 0
     35 
     36 #include "rf_types.h"
     37 #include "rf_threadstuff.h"
     38 #include "rf_mcpair.h"
     39 #include "rf_raid.h"
     40 #include "rf_dag.h"
     41 #include "rf_dagfuncs.h"
     42 #include "rf_desc.h"
     43 #include "rf_layout.h"
     44 #include "rf_diskqueue.h"
     45 #include "rf_paritylog.h"
     46 #include "rf_general.h"
     47 #include "rf_etimer.h"
     48 #include "rf_paritylogging.h"
     49 #include "rf_engine.h"
     50 #include "rf_dagutils.h"
     51 #include "rf_map.h"
     52 #include "rf_parityscan.h"
     53 
     54 #include "rf_paritylogDiskMgr.h"
     55 
     56 static caddr_t AcquireReintBuffer(RF_RegionBufferQueue_t *);
     57 
     58 static caddr_t
     59 AcquireReintBuffer(pool)
     60 	RF_RegionBufferQueue_t *pool;
     61 {
     62 	caddr_t bufPtr = NULL;
     63 
     64 	/* Return a region buffer from the free list (pool). If the free list
     65 	 * is empty, WAIT. BLOCKING */
     66 
     67 	RF_LOCK_MUTEX(pool->mutex);
     68 	if (pool->availableBuffers > 0) {
     69 		bufPtr = pool->buffers[pool->availBuffersIndex];
     70 		pool->availableBuffers--;
     71 		pool->availBuffersIndex++;
     72 		if (pool->availBuffersIndex == pool->totalBuffers)
     73 			pool->availBuffersIndex = 0;
     74 		RF_UNLOCK_MUTEX(pool->mutex);
     75 	} else {
     76 		RF_PANIC();	/* should never happen in currect config,
     77 				 * single reint */
     78 		RF_WAIT_COND(pool->cond, pool->mutex);
     79 	}
     80 	return (bufPtr);
     81 }
     82 
     83 static void
     84 ReleaseReintBuffer(
     85     RF_RegionBufferQueue_t * pool,
     86     caddr_t bufPtr)
     87 {
     88 	/* Insert a region buffer (bufPtr) into the free list (pool).
     89 	 * NON-BLOCKING */
     90 
     91 	RF_LOCK_MUTEX(pool->mutex);
     92 	pool->availableBuffers++;
     93 	pool->buffers[pool->emptyBuffersIndex] = bufPtr;
     94 	pool->emptyBuffersIndex++;
     95 	if (pool->emptyBuffersIndex == pool->totalBuffers)
     96 		pool->emptyBuffersIndex = 0;
     97 	RF_ASSERT(pool->availableBuffers <= pool->totalBuffers);
     98 	RF_UNLOCK_MUTEX(pool->mutex);
     99 	RF_SIGNAL_COND(pool->cond);
    100 }
    101 
    102 
    103 
    104 static void
    105 ReadRegionLog(
    106     RF_RegionId_t regionID,
    107     RF_MCPair_t * rrd_mcpair,
    108     caddr_t regionBuffer,
    109     RF_Raid_t * raidPtr,
    110     RF_DagHeader_t ** rrd_dag_h,
    111     RF_AllocListElem_t ** rrd_alloclist,
    112     RF_PhysDiskAddr_t ** rrd_pda)
    113 {
    114 	/* Initiate the read a region log from disk.  Once initiated, return
    115 	 * to the calling routine.
    116 	 *
    117 	 * NON-BLOCKING */
    118 
    119 	RF_AccTraceEntry_t *tracerec;
    120 	RF_DagNode_t *rrd_rdNode;
    121 
    122 	/* create DAG to read region log from disk */
    123 	rf_MakeAllocList(*rrd_alloclist);
    124 	*rrd_dag_h = rf_MakeSimpleDAG(raidPtr, 1, 0, regionBuffer,
    125 				      rf_DiskReadFunc, rf_DiskReadUndoFunc,
    126 				      "Rrl", *rrd_alloclist,
    127 				      RF_DAG_FLAGS_NONE,
    128 				      RF_IO_NORMAL_PRIORITY);
    129 
    130 	/* create and initialize PDA for the core log */
    131 	/* RF_Malloc(*rrd_pda, sizeof(RF_PhysDiskAddr_t), (RF_PhysDiskAddr_t
    132 	 * *)); */
    133 	*rrd_pda = rf_AllocPDAList(1);
    134 	rf_MapLogParityLogging(raidPtr, regionID, 0, &((*rrd_pda)->row),
    135 			       &((*rrd_pda)->col), &((*rrd_pda)->startSector));
    136 	(*rrd_pda)->numSector = raidPtr->regionInfo[regionID].capacity;
    137 
    138 	if ((*rrd_pda)->next) {
    139 		(*rrd_pda)->next = NULL;
    140 		printf("set rrd_pda->next to NULL\n");
    141 	}
    142 	/* initialize DAG parameters */
    143 	RF_Malloc(tracerec,sizeof(RF_AccTraceEntry_t), (RF_AccTraceEntry_t *));
    144 	bzero((char *) tracerec, sizeof(RF_AccTraceEntry_t));
    145 	(*rrd_dag_h)->tracerec = tracerec;
    146 	rrd_rdNode = (*rrd_dag_h)->succedents[0]->succedents[0];
    147 	rrd_rdNode->params[0].p = *rrd_pda;
    148 /*  rrd_rdNode->params[1] = regionBuffer; */
    149 	rrd_rdNode->params[2].v = 0;
    150 	rrd_rdNode->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY,
    151 						   0, 0, 0);
    152 
    153 	/* launch region log read dag */
    154 	rf_DispatchDAG(*rrd_dag_h, (void (*) (void *)) rf_MCPairWakeupFunc,
    155 	    (void *) rrd_mcpair);
    156 }
    157 
    158 
    159 
    160 static void
    161 WriteCoreLog(
    162     RF_ParityLog_t * log,
    163     RF_MCPair_t * fwr_mcpair,
    164     RF_Raid_t * raidPtr,
    165     RF_DagHeader_t ** fwr_dag_h,
    166     RF_AllocListElem_t ** fwr_alloclist,
    167     RF_PhysDiskAddr_t ** fwr_pda)
    168 {
    169 	RF_RegionId_t regionID = log->regionID;
    170 	RF_AccTraceEntry_t *tracerec;
    171 	RF_SectorNum_t regionOffset;
    172 	RF_DagNode_t *fwr_wrNode;
    173 
    174 	/* Initiate the write of a core log to a region log disk. Once
    175 	 * initiated, return to the calling routine.
    176 	 *
    177 	 * NON-BLOCKING */
    178 
    179 	/* create DAG to write a core log to a region log disk */
    180 	rf_MakeAllocList(*fwr_alloclist);
    181 	*fwr_dag_h = rf_MakeSimpleDAG(raidPtr, 1, 0, log->bufPtr,
    182 				      rf_DiskWriteFunc, rf_DiskWriteUndoFunc,
    183 	    "Wcl", *fwr_alloclist, RF_DAG_FLAGS_NONE, RF_IO_NORMAL_PRIORITY);
    184 
    185 	/* create and initialize PDA for the region log */
    186 	/* RF_Malloc(*fwr_pda, sizeof(RF_PhysDiskAddr_t), (RF_PhysDiskAddr_t
    187 	 * *)); */
    188 	*fwr_pda = rf_AllocPDAList(1);
    189 	regionOffset = log->diskOffset;
    190 	rf_MapLogParityLogging(raidPtr, regionID, regionOffset,
    191 			       &((*fwr_pda)->row), &((*fwr_pda)->col),
    192 			       &((*fwr_pda)->startSector));
    193 	(*fwr_pda)->numSector = raidPtr->numSectorsPerLog;
    194 
    195 	/* initialize DAG parameters */
    196 	RF_Malloc(tracerec,sizeof(RF_AccTraceEntry_t), (RF_AccTraceEntry_t *));
    197 	bzero((char *) tracerec, sizeof(RF_AccTraceEntry_t));
    198 	(*fwr_dag_h)->tracerec = tracerec;
    199 	fwr_wrNode = (*fwr_dag_h)->succedents[0]->succedents[0];
    200 	fwr_wrNode->params[0].p = *fwr_pda;
    201 /*  fwr_wrNode->params[1] = log->bufPtr; */
    202 	fwr_wrNode->params[2].v = 0;
    203 	fwr_wrNode->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY,
    204 						   0, 0, 0);
    205 
    206 	/* launch the dag to write the core log to disk */
    207 	rf_DispatchDAG(*fwr_dag_h, (void (*) (void *)) rf_MCPairWakeupFunc,
    208 	    (void *) fwr_mcpair);
    209 }
    210 
    211 
    212 static void
    213 ReadRegionParity(
    214     RF_RegionId_t regionID,
    215     RF_MCPair_t * prd_mcpair,
    216     caddr_t parityBuffer,
    217     RF_Raid_t * raidPtr,
    218     RF_DagHeader_t ** prd_dag_h,
    219     RF_AllocListElem_t ** prd_alloclist,
    220     RF_PhysDiskAddr_t ** prd_pda)
    221 {
    222 	/* Initiate the read region parity from disk. Once initiated, return
    223 	 * to the calling routine.
    224 	 *
    225 	 * NON-BLOCKING */
    226 
    227 	RF_AccTraceEntry_t *tracerec;
    228 	RF_DagNode_t *prd_rdNode;
    229 
    230 	/* create DAG to read region parity from disk */
    231 	rf_MakeAllocList(*prd_alloclist);
    232 	*prd_dag_h = rf_MakeSimpleDAG(raidPtr, 1, 0, NULL, rf_DiskReadFunc,
    233 				      rf_DiskReadUndoFunc, "Rrp",
    234 				      *prd_alloclist, RF_DAG_FLAGS_NONE,
    235 				      RF_IO_NORMAL_PRIORITY);
    236 
    237 	/* create and initialize PDA for region parity */
    238 	/* RF_Malloc(*prd_pda, sizeof(RF_PhysDiskAddr_t), (RF_PhysDiskAddr_t
    239 	 * *)); */
    240 	*prd_pda = rf_AllocPDAList(1);
    241 	rf_MapRegionParity(raidPtr, regionID, &((*prd_pda)->row),
    242 			   &((*prd_pda)->col), &((*prd_pda)->startSector),
    243 			   &((*prd_pda)->numSector));
    244 	if (rf_parityLogDebug)
    245 		printf("[reading %d sectors of parity from region %d]\n",
    246 		    (int) (*prd_pda)->numSector, regionID);
    247 	if ((*prd_pda)->next) {
    248 		(*prd_pda)->next = NULL;
    249 		printf("set prd_pda->next to NULL\n");
    250 	}
    251 	/* initialize DAG parameters */
    252 	RF_Malloc(tracerec,sizeof(RF_AccTraceEntry_t), (RF_AccTraceEntry_t *));
    253 	bzero((char *) tracerec, sizeof(RF_AccTraceEntry_t));
    254 	(*prd_dag_h)->tracerec = tracerec;
    255 	prd_rdNode = (*prd_dag_h)->succedents[0]->succedents[0];
    256 	prd_rdNode->params[0].p = *prd_pda;
    257 	prd_rdNode->params[1].p = parityBuffer;
    258 	prd_rdNode->params[2].v = 0;
    259 	prd_rdNode->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY,
    260 						   0, 0, 0);
    261 	if (rf_validateDAGDebug)
    262 		rf_ValidateDAG(*prd_dag_h);
    263 	/* launch region parity read dag */
    264 	rf_DispatchDAG(*prd_dag_h, (void (*) (void *)) rf_MCPairWakeupFunc,
    265 	    (void *) prd_mcpair);
    266 }
    267 
    268 static void
    269 WriteRegionParity(
    270     RF_RegionId_t regionID,
    271     RF_MCPair_t * pwr_mcpair,
    272     caddr_t parityBuffer,
    273     RF_Raid_t * raidPtr,
    274     RF_DagHeader_t ** pwr_dag_h,
    275     RF_AllocListElem_t ** pwr_alloclist,
    276     RF_PhysDiskAddr_t ** pwr_pda)
    277 {
    278 	/* Initiate the write of region parity to disk. Once initiated, return
    279 	 * to the calling routine.
    280 	 *
    281 	 * NON-BLOCKING */
    282 
    283 	RF_AccTraceEntry_t *tracerec;
    284 	RF_DagNode_t *pwr_wrNode;
    285 
    286 	/* create DAG to write region log from disk */
    287 	rf_MakeAllocList(*pwr_alloclist);
    288 	*pwr_dag_h = rf_MakeSimpleDAG(raidPtr, 1, 0, parityBuffer,
    289 				      rf_DiskWriteFunc, rf_DiskWriteUndoFunc,
    290 				      "Wrp", *pwr_alloclist,
    291 				      RF_DAG_FLAGS_NONE,
    292 				      RF_IO_NORMAL_PRIORITY);
    293 
    294 	/* create and initialize PDA for region parity */
    295 	/* RF_Malloc(*pwr_pda, sizeof(RF_PhysDiskAddr_t), (RF_PhysDiskAddr_t
    296 	 * *)); */
    297 	*pwr_pda = rf_AllocPDAList(1);
    298 	rf_MapRegionParity(raidPtr, regionID, &((*pwr_pda)->row),
    299 			   &((*pwr_pda)->col), &((*pwr_pda)->startSector),
    300 			   &((*pwr_pda)->numSector));
    301 
    302 	/* initialize DAG parameters */
    303 	RF_Malloc(tracerec,sizeof(RF_AccTraceEntry_t), (RF_AccTraceEntry_t *));
    304 	bzero((char *) tracerec, sizeof(RF_AccTraceEntry_t));
    305 	(*pwr_dag_h)->tracerec = tracerec;
    306 	pwr_wrNode = (*pwr_dag_h)->succedents[0]->succedents[0];
    307 	pwr_wrNode->params[0].p = *pwr_pda;
    308 /*  pwr_wrNode->params[1] = parityBuffer; */
    309 	pwr_wrNode->params[2].v = 0;
    310 	pwr_wrNode->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY,
    311 						   0, 0, 0);
    312 
    313 	/* launch the dag to write region parity to disk */
    314 	rf_DispatchDAG(*pwr_dag_h, (void (*) (void *)) rf_MCPairWakeupFunc,
    315 	    (void *) pwr_mcpair);
    316 }
    317 
    318 static void
    319 FlushLogsToDisk(
    320     RF_Raid_t * raidPtr,
    321     RF_ParityLog_t * logList)
    322 {
    323 	/* Flush a linked list of core logs to the log disk. Logs contain the
    324 	 * disk location where they should be written.  Logs were written in
    325 	 * FIFO order and that order must be preserved.
    326 	 *
    327 	 * Recommended optimizations: 1) allow multiple flushes to occur
    328 	 * simultaneously 2) coalesce contiguous flush operations
    329 	 *
    330 	 * BLOCKING */
    331 
    332 	RF_ParityLog_t *log;
    333 	RF_RegionId_t regionID;
    334 	RF_MCPair_t *fwr_mcpair;
    335 	RF_DagHeader_t *fwr_dag_h;
    336 	RF_AllocListElem_t *fwr_alloclist;
    337 	RF_PhysDiskAddr_t *fwr_pda;
    338 
    339 	fwr_mcpair = rf_AllocMCPair();
    340 	RF_LOCK_MUTEX(fwr_mcpair->mutex);
    341 
    342 	RF_ASSERT(logList);
    343 	log = logList;
    344 	while (log) {
    345 		regionID = log->regionID;
    346 
    347 		/* create and launch a DAG to write the core log */
    348 		if (rf_parityLogDebug)
    349 			printf("[initiating write of core log for region %d]\n", regionID);
    350 		fwr_mcpair->flag = RF_FALSE;
    351 		WriteCoreLog(log, fwr_mcpair, raidPtr, &fwr_dag_h,
    352 			     &fwr_alloclist, &fwr_pda);
    353 
    354 		/* wait for the DAG to complete */
    355 		while (!fwr_mcpair->flag)
    356 			RF_WAIT_COND(fwr_mcpair->cond, fwr_mcpair->mutex);
    357 		if (fwr_dag_h->status != rf_enable) {
    358 			RF_ERRORMSG1("Unable to write core log to disk (region %d)\n", regionID);
    359 			RF_ASSERT(0);
    360 		}
    361 		/* RF_Free(fwr_pda, sizeof(RF_PhysDiskAddr_t)); */
    362 		rf_FreePhysDiskAddr(fwr_pda);
    363 		rf_FreeDAG(fwr_dag_h);
    364 		rf_FreeAllocList(fwr_alloclist);
    365 
    366 		log = log->next;
    367 	}
    368 	RF_UNLOCK_MUTEX(fwr_mcpair->mutex);
    369 	rf_FreeMCPair(fwr_mcpair);
    370 	rf_ReleaseParityLogs(raidPtr, logList);
    371 }
    372 
    373 static void
    374 ReintegrateRegion(
    375     RF_Raid_t * raidPtr,
    376     RF_RegionId_t regionID,
    377     RF_ParityLog_t * coreLog)
    378 {
    379 	RF_MCPair_t *rrd_mcpair = NULL, *prd_mcpair, *pwr_mcpair;
    380 	RF_DagHeader_t *rrd_dag_h, *prd_dag_h, *pwr_dag_h;
    381 	RF_AllocListElem_t *rrd_alloclist, *prd_alloclist, *pwr_alloclist;
    382 	RF_PhysDiskAddr_t *rrd_pda, *prd_pda, *pwr_pda;
    383 	caddr_t parityBuffer, regionBuffer = NULL;
    384 
    385 	/* Reintegrate a region (regionID). 1. acquire region and parity
    386 	 * buffers 2. read log from disk 3. read parity from disk 4. apply log
    387 	 * to parity 5. apply core log to parity 6. write new parity to disk
    388 	 *
    389 	 * BLOCKING */
    390 
    391 	if (rf_parityLogDebug)
    392 		printf("[reintegrating region %d]\n", regionID);
    393 
    394 	/* initiate read of region parity */
    395 	if (rf_parityLogDebug)
    396 		printf("[initiating read of parity for region %d]\n",regionID);
    397 	parityBuffer = AcquireReintBuffer(&raidPtr->parityBufferPool);
    398 	prd_mcpair = rf_AllocMCPair();
    399 	RF_LOCK_MUTEX(prd_mcpair->mutex);
    400 	prd_mcpair->flag = RF_FALSE;
    401 	ReadRegionParity(regionID, prd_mcpair, parityBuffer, raidPtr,
    402 			 &prd_dag_h, &prd_alloclist, &prd_pda);
    403 
    404 	/* if region log nonempty, initiate read */
    405 	if (raidPtr->regionInfo[regionID].diskCount > 0) {
    406 		if (rf_parityLogDebug)
    407 			printf("[initiating read of disk log for region %d]\n",
    408 			       regionID);
    409 		regionBuffer = AcquireReintBuffer(&raidPtr->regionBufferPool);
    410 		rrd_mcpair = rf_AllocMCPair();
    411 		RF_LOCK_MUTEX(rrd_mcpair->mutex);
    412 		rrd_mcpair->flag = RF_FALSE;
    413 		ReadRegionLog(regionID, rrd_mcpair, regionBuffer, raidPtr,
    414 			      &rrd_dag_h, &rrd_alloclist, &rrd_pda);
    415 	}
    416 	/* wait on read of region parity to complete */
    417 	while (!prd_mcpair->flag) {
    418 		RF_WAIT_COND(prd_mcpair->cond, prd_mcpair->mutex);
    419 	}
    420 	RF_UNLOCK_MUTEX(prd_mcpair->mutex);
    421 	if (prd_dag_h->status != rf_enable) {
    422 		RF_ERRORMSG("Unable to read parity from disk\n");
    423 		/* add code to fail the parity disk */
    424 		RF_ASSERT(0);
    425 	}
    426 	/* apply core log to parity */
    427 	/* if (coreLog) ApplyLogsToParity(coreLog, parityBuffer); */
    428 
    429 	if (raidPtr->regionInfo[regionID].diskCount > 0) {
    430 		/* wait on read of region log to complete */
    431 		while (!rrd_mcpair->flag)
    432 			RF_WAIT_COND(rrd_mcpair->cond, rrd_mcpair->mutex);
    433 		RF_UNLOCK_MUTEX(rrd_mcpair->mutex);
    434 		if (rrd_dag_h->status != rf_enable) {
    435 			RF_ERRORMSG("Unable to read region log from disk\n");
    436 			/* add code to fail the log disk */
    437 			RF_ASSERT(0);
    438 		}
    439 		/* apply region log to parity */
    440 		/* ApplyRegionToParity(regionID, regionBuffer, parityBuffer); */
    441 		/* release resources associated with region log */
    442 		/* RF_Free(rrd_pda, sizeof(RF_PhysDiskAddr_t)); */
    443 		rf_FreePhysDiskAddr(rrd_pda);
    444 		rf_FreeDAG(rrd_dag_h);
    445 		rf_FreeAllocList(rrd_alloclist);
    446 		rf_FreeMCPair(rrd_mcpair);
    447 		ReleaseReintBuffer(&raidPtr->regionBufferPool, regionBuffer);
    448 	}
    449 	/* write reintegrated parity to disk */
    450 	if (rf_parityLogDebug)
    451 		printf("[initiating write of parity for region %d]\n",
    452 		       regionID);
    453 	pwr_mcpair = rf_AllocMCPair();
    454 	RF_LOCK_MUTEX(pwr_mcpair->mutex);
    455 	pwr_mcpair->flag = RF_FALSE;
    456 	WriteRegionParity(regionID, pwr_mcpair, parityBuffer, raidPtr,
    457 			  &pwr_dag_h, &pwr_alloclist, &pwr_pda);
    458 	while (!pwr_mcpair->flag)
    459 		RF_WAIT_COND(pwr_mcpair->cond, pwr_mcpair->mutex);
    460 	RF_UNLOCK_MUTEX(pwr_mcpair->mutex);
    461 	if (pwr_dag_h->status != rf_enable) {
    462 		RF_ERRORMSG("Unable to write parity to disk\n");
    463 		/* add code to fail the parity disk */
    464 		RF_ASSERT(0);
    465 	}
    466 	/* release resources associated with read of old parity */
    467 	/* RF_Free(prd_pda, sizeof(RF_PhysDiskAddr_t)); */
    468 	rf_FreePhysDiskAddr(prd_pda);
    469 	rf_FreeDAG(prd_dag_h);
    470 	rf_FreeAllocList(prd_alloclist);
    471 	rf_FreeMCPair(prd_mcpair);
    472 
    473 	/* release resources associated with write of new parity */
    474 	ReleaseReintBuffer(&raidPtr->parityBufferPool, parityBuffer);
    475 	/* RF_Free(pwr_pda, sizeof(RF_PhysDiskAddr_t)); */
    476 	rf_FreePhysDiskAddr(pwr_pda);
    477 	rf_FreeDAG(pwr_dag_h);
    478 	rf_FreeAllocList(pwr_alloclist);
    479 	rf_FreeMCPair(pwr_mcpair);
    480 
    481 	if (rf_parityLogDebug)
    482 		printf("[finished reintegrating region %d]\n", regionID);
    483 }
    484 
    485 
    486 
    487 static void
    488 ReintegrateLogs(
    489     RF_Raid_t * raidPtr,
    490     RF_ParityLog_t * logList)
    491 {
    492 	RF_ParityLog_t *log, *freeLogList = NULL;
    493 	RF_ParityLogData_t *logData, *logDataList;
    494 	RF_RegionId_t regionID;
    495 
    496 	RF_ASSERT(logList);
    497 	while (logList) {
    498 		log = logList;
    499 		logList = logList->next;
    500 		log->next = NULL;
    501 		regionID = log->regionID;
    502 		ReintegrateRegion(raidPtr, regionID, log);
    503 		log->numRecords = 0;
    504 
    505 		/* remove all items which are blocked on reintegration of this
    506 		 * region */
    507 		RF_LOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    508 		logData = rf_SearchAndDequeueParityLogData(raidPtr, regionID,
    509 			   &raidPtr->parityLogDiskQueue.reintBlockHead,
    510 			   &raidPtr->parityLogDiskQueue.reintBlockTail,
    511 							   RF_TRUE);
    512 		logDataList = logData;
    513 		while (logData) {
    514 			logData->next = rf_SearchAndDequeueParityLogData(
    515 					 raidPtr, regionID,
    516 					 &raidPtr->parityLogDiskQueue.reintBlockHead,
    517 					 &raidPtr->parityLogDiskQueue.reintBlockTail,
    518 					 RF_TRUE);
    519 			logData = logData->next;
    520 		}
    521 		RF_UNLOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    522 
    523 		/* process blocked log data and clear reintInProgress flag for
    524 		 * this region */
    525 		if (logDataList)
    526 			rf_ParityLogAppend(logDataList, RF_TRUE, &log, RF_TRUE);
    527 		else {
    528 			/* Enable flushing for this region.  Holding both
    529 			 * locks provides a synchronization barrier with
    530 			 * DumpParityLogToDisk */
    531 			RF_LOCK_MUTEX(raidPtr->regionInfo[regionID].mutex);
    532 			RF_LOCK_MUTEX(raidPtr->regionInfo[regionID].reintMutex);
    533 			RF_LOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    534 			raidPtr->regionInfo[regionID].diskCount = 0;
    535 			raidPtr->regionInfo[regionID].reintInProgress = RF_FALSE;
    536 			RF_UNLOCK_MUTEX(raidPtr->regionInfo[regionID].mutex);
    537 			RF_UNLOCK_MUTEX(raidPtr->regionInfo[regionID].reintMutex);	/* flushing is now
    538 											 * enabled */
    539 			RF_UNLOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    540 		}
    541 		/* if log wasn't used, attach it to the list of logs to be
    542 		 * returned */
    543 		if (log) {
    544 			log->next = freeLogList;
    545 			freeLogList = log;
    546 		}
    547 	}
    548 	if (freeLogList)
    549 		rf_ReleaseParityLogs(raidPtr, freeLogList);
    550 }
    551 
    552 int
    553 rf_ShutdownLogging(RF_Raid_t * raidPtr)
    554 {
    555 	/* shutdown parity logging 1) disable parity logging in all regions 2)
    556 	 * reintegrate all regions */
    557 
    558 	RF_SectorCount_t diskCount;
    559 	RF_RegionId_t regionID;
    560 	RF_ParityLog_t *log;
    561 
    562 	if (rf_parityLogDebug)
    563 		printf("[shutting down parity logging]\n");
    564 	/* Since parity log maps are volatile, we must reintegrate all
    565 	 * regions. */
    566 	if (rf_forceParityLogReint) {
    567 		for (regionID = 0; regionID < rf_numParityRegions; regionID++) {
    568 			RF_LOCK_MUTEX(raidPtr->regionInfo[regionID].mutex);
    569 			raidPtr->regionInfo[regionID].loggingEnabled =
    570 				RF_FALSE;
    571 			log = raidPtr->regionInfo[regionID].coreLog;
    572 			raidPtr->regionInfo[regionID].coreLog = NULL;
    573 			diskCount = raidPtr->regionInfo[regionID].diskCount;
    574 			RF_UNLOCK_MUTEX(raidPtr->regionInfo[regionID].mutex);
    575 			if (diskCount > 0 || log != NULL)
    576 				ReintegrateRegion(raidPtr, regionID, log);
    577 			if (log != NULL)
    578 				rf_ReleaseParityLogs(raidPtr, log);
    579 		}
    580 	}
    581 	if (rf_parityLogDebug) {
    582 		printf("[parity logging disabled]\n");
    583 		printf("[should be done!]\n");
    584 	}
    585 	return (0);
    586 }
    587 
    588 int
    589 rf_ParityLoggingDiskManager(RF_Raid_t * raidPtr)
    590 {
    591 	RF_ParityLog_t *reintQueue, *flushQueue;
    592 	int     workNeeded, done = RF_FALSE;
    593 	int s;
    594 
    595 	/* Main program for parity logging disk thread.  This routine waits
    596 	 * for work to appear in either the flush or reintegration queues and
    597 	 * is responsible for flushing core logs to the log disk as well as
    598 	 * reintegrating parity regions.
    599 	 *
    600 	 * BLOCKING */
    601 
    602 	s = splbio();
    603 
    604 	RF_LOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    605 
    606 	/*
    607          * Inform our creator that we're running. Don't bother doing the
    608          * mutex lock/unlock dance- we locked above, and we'll unlock
    609          * below with nothing to do, yet.
    610          */
    611 	raidPtr->parityLogDiskQueue.threadState |= RF_PLOG_RUNNING;
    612 	RF_SIGNAL_COND(raidPtr->parityLogDiskQueue.cond);
    613 
    614 	/* empty the work queues */
    615 	flushQueue = raidPtr->parityLogDiskQueue.flushQueue;
    616 	raidPtr->parityLogDiskQueue.flushQueue = NULL;
    617 	reintQueue = raidPtr->parityLogDiskQueue.reintQueue;
    618 	raidPtr->parityLogDiskQueue.reintQueue = NULL;
    619 	workNeeded = (flushQueue || reintQueue);
    620 
    621 	while (!done) {
    622 		while (workNeeded) {
    623 			/* First, flush all logs in the flush queue, freeing
    624 			 * buffers Second, reintegrate all regions which are
    625 			 * reported as full. Third, append queued log data
    626 			 * until blocked.
    627 			 *
    628 			 * Note: Incoming appends (ParityLogAppend) can block on
    629 			 * either 1. empty buffer pool 2. region under
    630 			 * reintegration To preserve a global FIFO ordering of
    631 			 * appends, buffers are not released to the world
    632 			 * until those appends blocked on buffers are removed
    633 			 * from the append queue.  Similarly, regions which
    634 			 * are reintegrated are not opened for general use
    635 			 * until the append queue has been emptied. */
    636 
    637 			RF_UNLOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    638 
    639 			/* empty flushQueue, using free'd log buffers to
    640 			 * process bufTail */
    641 			if (flushQueue)
    642 			       FlushLogsToDisk(raidPtr, flushQueue);
    643 
    644 			/* empty reintQueue, flushing from reintTail as we go */
    645 			if (reintQueue)
    646 				ReintegrateLogs(raidPtr, reintQueue);
    647 
    648 			RF_LOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    649 			flushQueue = raidPtr->parityLogDiskQueue.flushQueue;
    650 			raidPtr->parityLogDiskQueue.flushQueue = NULL;
    651 			reintQueue = raidPtr->parityLogDiskQueue.reintQueue;
    652 			raidPtr->parityLogDiskQueue.reintQueue = NULL;
    653 			workNeeded = (flushQueue || reintQueue);
    654 		}
    655 		/* no work is needed at this point */
    656 		if (raidPtr->parityLogDiskQueue.threadState & RF_PLOG_TERMINATE) {
    657 			/* shutdown parity logging 1. disable parity logging
    658 			 * in all regions 2. reintegrate all regions */
    659 			done = RF_TRUE;	/* thread disabled, no work needed */
    660 			RF_UNLOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    661 			rf_ShutdownLogging(raidPtr);
    662 		}
    663 		if (!done) {
    664 			/* thread enabled, no work needed, so sleep */
    665 			if (rf_parityLogDebug)
    666 				printf("[parity logging disk manager sleeping]\n");
    667 			RF_WAIT_COND(raidPtr->parityLogDiskQueue.cond,
    668 				     raidPtr->parityLogDiskQueue.mutex);
    669 			if (rf_parityLogDebug)
    670 				printf("[parity logging disk manager just woke up]\n");
    671 			flushQueue = raidPtr->parityLogDiskQueue.flushQueue;
    672 			raidPtr->parityLogDiskQueue.flushQueue = NULL;
    673 			reintQueue = raidPtr->parityLogDiskQueue.reintQueue;
    674 			raidPtr->parityLogDiskQueue.reintQueue = NULL;
    675 			workNeeded = (flushQueue || reintQueue);
    676 		}
    677 	}
    678 	/*
    679          * Announce that we're done.
    680          */
    681 	RF_LOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    682 	raidPtr->parityLogDiskQueue.threadState |= RF_PLOG_SHUTDOWN;
    683 	RF_UNLOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
    684 	RF_SIGNAL_COND(raidPtr->parityLogDiskQueue.cond);
    685 
    686 	splx(s);
    687 
    688 	/*
    689          * In the NetBSD kernel, the thread must exit; returning would
    690          * cause the proc trampoline to attempt to return to userspace.
    691          */
    692 	kthread_exit(0);	/* does not return */
    693 }
    694 #endif				/* RF_INCLUDE_PARITYLOGGING > 0 */
    695