xref: /netbsd-src/sys/dev/raidframe/rf_paritylogDiskMgr.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: rf_paritylogDiskMgr.c,v 1.11 2001/07/18 06:45:34 thorpej 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 "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 correct 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 	memset((char *) tracerec, 0, 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 	memset((char *) tracerec, 0, 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 	memset((char *) tracerec, 0, 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 	memset((char *) tracerec, 0, 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).
386 	 *
387 	 * 1. acquire region and parity buffers
388 	 * 2. read log from disk
389 	 * 3. read parity from disk
390 	 * 4. apply log to parity
391 	 * 5. apply core log to parity
392 	 * 6. write new parity to disk
393 	 *
394 	 * BLOCKING */
395 
396 	if (rf_parityLogDebug)
397 		printf("[reintegrating region %d]\n", regionID);
398 
399 	/* initiate read of region parity */
400 	if (rf_parityLogDebug)
401 		printf("[initiating read of parity for region %d]\n",regionID);
402 	parityBuffer = AcquireReintBuffer(&raidPtr->parityBufferPool);
403 	prd_mcpair = rf_AllocMCPair();
404 	RF_LOCK_MUTEX(prd_mcpair->mutex);
405 	prd_mcpair->flag = RF_FALSE;
406 	ReadRegionParity(regionID, prd_mcpair, parityBuffer, raidPtr,
407 			 &prd_dag_h, &prd_alloclist, &prd_pda);
408 
409 	/* if region log nonempty, initiate read */
410 	if (raidPtr->regionInfo[regionID].diskCount > 0) {
411 		if (rf_parityLogDebug)
412 			printf("[initiating read of disk log for region %d]\n",
413 			       regionID);
414 		regionBuffer = AcquireReintBuffer(&raidPtr->regionBufferPool);
415 		rrd_mcpair = rf_AllocMCPair();
416 		RF_LOCK_MUTEX(rrd_mcpair->mutex);
417 		rrd_mcpair->flag = RF_FALSE;
418 		ReadRegionLog(regionID, rrd_mcpair, regionBuffer, raidPtr,
419 			      &rrd_dag_h, &rrd_alloclist, &rrd_pda);
420 	}
421 	/* wait on read of region parity to complete */
422 	while (!prd_mcpair->flag) {
423 		RF_WAIT_COND(prd_mcpair->cond, prd_mcpair->mutex);
424 	}
425 	RF_UNLOCK_MUTEX(prd_mcpair->mutex);
426 	if (prd_dag_h->status != rf_enable) {
427 		RF_ERRORMSG("Unable to read parity from disk\n");
428 		/* add code to fail the parity disk */
429 		RF_ASSERT(0);
430 	}
431 	/* apply core log to parity */
432 	/* if (coreLog) ApplyLogsToParity(coreLog, parityBuffer); */
433 
434 	if (raidPtr->regionInfo[regionID].diskCount > 0) {
435 		/* wait on read of region log to complete */
436 		while (!rrd_mcpair->flag)
437 			RF_WAIT_COND(rrd_mcpair->cond, rrd_mcpair->mutex);
438 		RF_UNLOCK_MUTEX(rrd_mcpair->mutex);
439 		if (rrd_dag_h->status != rf_enable) {
440 			RF_ERRORMSG("Unable to read region log from disk\n");
441 			/* add code to fail the log disk */
442 			RF_ASSERT(0);
443 		}
444 		/* apply region log to parity */
445 		/* ApplyRegionToParity(regionID, regionBuffer, parityBuffer); */
446 		/* release resources associated with region log */
447 		/* RF_Free(rrd_pda, sizeof(RF_PhysDiskAddr_t)); */
448 		rf_FreePhysDiskAddr(rrd_pda);
449 		rf_FreeDAG(rrd_dag_h);
450 		rf_FreeAllocList(rrd_alloclist);
451 		rf_FreeMCPair(rrd_mcpair);
452 		ReleaseReintBuffer(&raidPtr->regionBufferPool, regionBuffer);
453 	}
454 	/* write reintegrated parity to disk */
455 	if (rf_parityLogDebug)
456 		printf("[initiating write of parity for region %d]\n",
457 		       regionID);
458 	pwr_mcpair = rf_AllocMCPair();
459 	RF_LOCK_MUTEX(pwr_mcpair->mutex);
460 	pwr_mcpair->flag = RF_FALSE;
461 	WriteRegionParity(regionID, pwr_mcpair, parityBuffer, raidPtr,
462 			  &pwr_dag_h, &pwr_alloclist, &pwr_pda);
463 	while (!pwr_mcpair->flag)
464 		RF_WAIT_COND(pwr_mcpair->cond, pwr_mcpair->mutex);
465 	RF_UNLOCK_MUTEX(pwr_mcpair->mutex);
466 	if (pwr_dag_h->status != rf_enable) {
467 		RF_ERRORMSG("Unable to write parity to disk\n");
468 		/* add code to fail the parity disk */
469 		RF_ASSERT(0);
470 	}
471 	/* release resources associated with read of old parity */
472 	/* RF_Free(prd_pda, sizeof(RF_PhysDiskAddr_t)); */
473 	rf_FreePhysDiskAddr(prd_pda);
474 	rf_FreeDAG(prd_dag_h);
475 	rf_FreeAllocList(prd_alloclist);
476 	rf_FreeMCPair(prd_mcpair);
477 
478 	/* release resources associated with write of new parity */
479 	ReleaseReintBuffer(&raidPtr->parityBufferPool, parityBuffer);
480 	/* RF_Free(pwr_pda, sizeof(RF_PhysDiskAddr_t)); */
481 	rf_FreePhysDiskAddr(pwr_pda);
482 	rf_FreeDAG(pwr_dag_h);
483 	rf_FreeAllocList(pwr_alloclist);
484 	rf_FreeMCPair(pwr_mcpair);
485 
486 	if (rf_parityLogDebug)
487 		printf("[finished reintegrating region %d]\n", regionID);
488 }
489 
490 
491 
492 static void
493 ReintegrateLogs(
494     RF_Raid_t * raidPtr,
495     RF_ParityLog_t * logList)
496 {
497 	RF_ParityLog_t *log, *freeLogList = NULL;
498 	RF_ParityLogData_t *logData, *logDataList;
499 	RF_RegionId_t regionID;
500 
501 	RF_ASSERT(logList);
502 	while (logList) {
503 		log = logList;
504 		logList = logList->next;
505 		log->next = NULL;
506 		regionID = log->regionID;
507 		ReintegrateRegion(raidPtr, regionID, log);
508 		log->numRecords = 0;
509 
510 		/* remove all items which are blocked on reintegration of this
511 		 * region */
512 		RF_LOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
513 		logData = rf_SearchAndDequeueParityLogData(raidPtr, regionID,
514 			   &raidPtr->parityLogDiskQueue.reintBlockHead,
515 			   &raidPtr->parityLogDiskQueue.reintBlockTail,
516 							   RF_TRUE);
517 		logDataList = logData;
518 		while (logData) {
519 			logData->next = rf_SearchAndDequeueParityLogData(
520 					 raidPtr, regionID,
521 					 &raidPtr->parityLogDiskQueue.reintBlockHead,
522 					 &raidPtr->parityLogDiskQueue.reintBlockTail,
523 					 RF_TRUE);
524 			logData = logData->next;
525 		}
526 		RF_UNLOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
527 
528 		/* process blocked log data and clear reintInProgress flag for
529 		 * this region */
530 		if (logDataList)
531 			rf_ParityLogAppend(logDataList, RF_TRUE, &log, RF_TRUE);
532 		else {
533 			/* Enable flushing for this region.  Holding both
534 			 * locks provides a synchronization barrier with
535 			 * DumpParityLogToDisk */
536 			RF_LOCK_MUTEX(raidPtr->regionInfo[regionID].mutex);
537 			RF_LOCK_MUTEX(raidPtr->regionInfo[regionID].reintMutex);
538 			RF_LOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
539 			raidPtr->regionInfo[regionID].diskCount = 0;
540 			raidPtr->regionInfo[regionID].reintInProgress = RF_FALSE;
541 			RF_UNLOCK_MUTEX(raidPtr->regionInfo[regionID].mutex);
542 			RF_UNLOCK_MUTEX(raidPtr->regionInfo[regionID].reintMutex);	/* flushing is now
543 											 * enabled */
544 			RF_UNLOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
545 		}
546 		/* if log wasn't used, attach it to the list of logs to be
547 		 * returned */
548 		if (log) {
549 			log->next = freeLogList;
550 			freeLogList = log;
551 		}
552 	}
553 	if (freeLogList)
554 		rf_ReleaseParityLogs(raidPtr, freeLogList);
555 }
556 
557 int
558 rf_ShutdownLogging(RF_Raid_t * raidPtr)
559 {
560 	/* shutdown parity logging 1) disable parity logging in all regions 2)
561 	 * reintegrate all regions */
562 
563 	RF_SectorCount_t diskCount;
564 	RF_RegionId_t regionID;
565 	RF_ParityLog_t *log;
566 
567 	if (rf_parityLogDebug)
568 		printf("[shutting down parity logging]\n");
569 	/* Since parity log maps are volatile, we must reintegrate all
570 	 * regions. */
571 	if (rf_forceParityLogReint) {
572 		for (regionID = 0; regionID < rf_numParityRegions; regionID++) {
573 			RF_LOCK_MUTEX(raidPtr->regionInfo[regionID].mutex);
574 			raidPtr->regionInfo[regionID].loggingEnabled =
575 				RF_FALSE;
576 			log = raidPtr->regionInfo[regionID].coreLog;
577 			raidPtr->regionInfo[regionID].coreLog = NULL;
578 			diskCount = raidPtr->regionInfo[regionID].diskCount;
579 			RF_UNLOCK_MUTEX(raidPtr->regionInfo[regionID].mutex);
580 			if (diskCount > 0 || log != NULL)
581 				ReintegrateRegion(raidPtr, regionID, log);
582 			if (log != NULL)
583 				rf_ReleaseParityLogs(raidPtr, log);
584 		}
585 	}
586 	if (rf_parityLogDebug) {
587 		printf("[parity logging disabled]\n");
588 		printf("[should be done!]\n");
589 	}
590 	return (0);
591 }
592 
593 int
594 rf_ParityLoggingDiskManager(RF_Raid_t * raidPtr)
595 {
596 	RF_ParityLog_t *reintQueue, *flushQueue;
597 	int     workNeeded, done = RF_FALSE;
598 	int s;
599 
600 	/* Main program for parity logging disk thread.  This routine waits
601 	 * for work to appear in either the flush or reintegration queues and
602 	 * is responsible for flushing core logs to the log disk as well as
603 	 * reintegrating parity regions.
604 	 *
605 	 * BLOCKING */
606 
607 	s = splbio();
608 
609 	RF_LOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
610 
611 	/*
612          * Inform our creator that we're running. Don't bother doing the
613          * mutex lock/unlock dance- we locked above, and we'll unlock
614          * below with nothing to do, yet.
615          */
616 	raidPtr->parityLogDiskQueue.threadState |= RF_PLOG_RUNNING;
617 	RF_SIGNAL_COND(raidPtr->parityLogDiskQueue.cond);
618 
619 	/* empty the work queues */
620 	flushQueue = raidPtr->parityLogDiskQueue.flushQueue;
621 	raidPtr->parityLogDiskQueue.flushQueue = NULL;
622 	reintQueue = raidPtr->parityLogDiskQueue.reintQueue;
623 	raidPtr->parityLogDiskQueue.reintQueue = NULL;
624 	workNeeded = (flushQueue || reintQueue);
625 
626 	while (!done) {
627 		while (workNeeded) {
628 			/* First, flush all logs in the flush queue, freeing
629 			 * buffers Second, reintegrate all regions which are
630 			 * reported as full. Third, append queued log data
631 			 * until blocked.
632 			 *
633 			 * Note: Incoming appends (ParityLogAppend) can block on
634 			 * either 1. empty buffer pool 2. region under
635 			 * reintegration To preserve a global FIFO ordering of
636 			 * appends, buffers are not released to the world
637 			 * until those appends blocked on buffers are removed
638 			 * from the append queue.  Similarly, regions which
639 			 * are reintegrated are not opened for general use
640 			 * until the append queue has been emptied. */
641 
642 			RF_UNLOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
643 
644 			/* empty flushQueue, using free'd log buffers to
645 			 * process bufTail */
646 			if (flushQueue)
647 			       FlushLogsToDisk(raidPtr, flushQueue);
648 
649 			/* empty reintQueue, flushing from reintTail as we go */
650 			if (reintQueue)
651 				ReintegrateLogs(raidPtr, reintQueue);
652 
653 			RF_LOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
654 			flushQueue = raidPtr->parityLogDiskQueue.flushQueue;
655 			raidPtr->parityLogDiskQueue.flushQueue = NULL;
656 			reintQueue = raidPtr->parityLogDiskQueue.reintQueue;
657 			raidPtr->parityLogDiskQueue.reintQueue = NULL;
658 			workNeeded = (flushQueue || reintQueue);
659 		}
660 		/* no work is needed at this point */
661 		if (raidPtr->parityLogDiskQueue.threadState & RF_PLOG_TERMINATE) {
662 			/* shutdown parity logging 1. disable parity logging
663 			 * in all regions 2. reintegrate all regions */
664 			done = RF_TRUE;	/* thread disabled, no work needed */
665 			RF_UNLOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
666 			rf_ShutdownLogging(raidPtr);
667 		}
668 		if (!done) {
669 			/* thread enabled, no work needed, so sleep */
670 			if (rf_parityLogDebug)
671 				printf("[parity logging disk manager sleeping]\n");
672 			RF_WAIT_COND(raidPtr->parityLogDiskQueue.cond,
673 				     raidPtr->parityLogDiskQueue.mutex);
674 			if (rf_parityLogDebug)
675 				printf("[parity logging disk manager just woke up]\n");
676 			flushQueue = raidPtr->parityLogDiskQueue.flushQueue;
677 			raidPtr->parityLogDiskQueue.flushQueue = NULL;
678 			reintQueue = raidPtr->parityLogDiskQueue.reintQueue;
679 			raidPtr->parityLogDiskQueue.reintQueue = NULL;
680 			workNeeded = (flushQueue || reintQueue);
681 		}
682 	}
683 	/*
684          * Announce that we're done.
685          */
686 	RF_LOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
687 	raidPtr->parityLogDiskQueue.threadState |= RF_PLOG_SHUTDOWN;
688 	RF_UNLOCK_MUTEX(raidPtr->parityLogDiskQueue.mutex);
689 	RF_SIGNAL_COND(raidPtr->parityLogDiskQueue.cond);
690 
691 	splx(s);
692 
693 	/*
694          * In the NetBSD kernel, the thread must exit; returning would
695          * cause the proc trampoline to attempt to return to userspace.
696          */
697 	kthread_exit(0);	/* does not return */
698 }
699 #endif				/* RF_INCLUDE_PARITYLOGGING > 0 */
700