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