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