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