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