rf_parityscan.c revision 1.4 1 /* $NetBSD: rf_parityscan.c,v 1.4 1999/03/14 22:10:46 oster Exp $ */
2 /*
3 * Copyright (c) 1995 Carnegie-Mellon University.
4 * All rights reserved.
5 *
6 * Author: Mark Holland
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
29 /*****************************************************************************
30 *
31 * rf_parityscan.c -- misc utilities related to parity verification
32 *
33 *****************************************************************************/
34
35 #include "rf_types.h"
36 #include "rf_raid.h"
37 #include "rf_dag.h"
38 #include "rf_dagfuncs.h"
39 #include "rf_dagutils.h"
40 #include "rf_mcpair.h"
41 #include "rf_general.h"
42 #include "rf_engine.h"
43 #include "rf_parityscan.h"
44 #include "rf_map.h"
45 #include "rf_sys.h"
46
47 /*****************************************************************************************
48 *
49 * walk through the entire arry and write new parity.
50 * This works by creating two DAGs, one to read a stripe of data and one to
51 * write new parity. The first is executed, the data is xored together, and
52 * then the second is executed. To avoid constantly building and tearing down
53 * the DAGs, we create them a priori and fill them in with the mapping
54 * information as we go along.
55 *
56 * there should never be more than one thread running this.
57 *
58 ****************************************************************************************/
59
60 int
61 rf_RewriteParity(raidPtr)
62 RF_Raid_t *raidPtr;
63 {
64 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
65 RF_AccessStripeMapHeader_t *asm_h;
66 int rc;
67 RF_PhysDiskAddr_t pda;
68 RF_SectorNum_t i;
69
70 pda.startSector = 0;
71 pda.numSector = raidPtr->Layout.sectorsPerStripeUnit;
72
73 for (i = 0; i < raidPtr->totalSectors;
74 i += layoutPtr->dataSectorsPerStripe) {
75 asm_h = rf_MapAccess(raidPtr, i,
76 layoutPtr->dataSectorsPerStripe,
77 NULL, RF_DONT_REMAP);
78
79 rc = rf_VerifyParity(raidPtr, asm_h->stripeMap, 1, 0);
80
81 switch (rc) {
82 case RF_PARITY_OKAY:
83 case RF_PARITY_CORRECTED:
84 break;
85 case RF_PARITY_BAD:
86 printf("Parity bad during correction\n");
87 RF_PANIC();
88 break;
89 case RF_PARITY_COULD_NOT_CORRECT:
90 printf("Could not correct bad parity\n");
91 RF_PANIC();
92 break;
93 case RF_PARITY_COULD_NOT_VERIFY:
94 printf("Could not verify parity\n");
95 RF_PANIC();
96 break;
97 default:
98 printf("Bad rc=%d from VerifyParity in RewriteParity\n", rc);
99 RF_PANIC();
100 }
101 rf_FreeAccessStripeMap(asm_h);
102 }
103 return (0);
104 }
105 /*****************************************************************************************
106 *
107 * verify that the parity in a particular stripe is correct.
108 * we validate only the range of parity defined by parityPDA, since
109 * this is all we have locked. The way we do this is to create an asm
110 * that maps the whole stripe and then range-restrict it to the parity
111 * region defined by the parityPDA.
112 *
113 ****************************************************************************************/
114 int
115 rf_VerifyParity(raidPtr, aasm, correct_it, flags)
116 RF_Raid_t *raidPtr;
117 RF_AccessStripeMap_t *aasm;
118 int correct_it;
119 RF_RaidAccessFlags_t flags;
120 {
121 RF_PhysDiskAddr_t *parityPDA;
122 RF_AccessStripeMap_t *doasm;
123 RF_LayoutSW_t *lp;
124 int lrc, rc;
125
126 lp = raidPtr->Layout.map;
127 if (lp->faultsTolerated == 0) {
128 /*
129 * There isn't any parity. Call it "okay."
130 */
131 return (RF_PARITY_OKAY);
132 }
133 rc = RF_PARITY_OKAY;
134 if (lp->VerifyParity) {
135 for (doasm = aasm; doasm; doasm = doasm->next) {
136 for (parityPDA = doasm->parityInfo; parityPDA;
137 parityPDA = parityPDA->next) {
138 lrc = lp->VerifyParity(raidPtr,
139 doasm->raidAddress,
140 parityPDA,
141 correct_it, flags);
142 if (lrc > rc) {
143 /* see rf_parityscan.h for why this
144 * works */
145 rc = lrc;
146 }
147 }
148 }
149 } else {
150 rc = RF_PARITY_COULD_NOT_VERIFY;
151 }
152 return (rc);
153 }
154
155 int
156 rf_VerifyParityBasic(raidPtr, raidAddr, parityPDA, correct_it, flags)
157 RF_Raid_t *raidPtr;
158 RF_RaidAddr_t raidAddr;
159 RF_PhysDiskAddr_t *parityPDA;
160 int correct_it;
161 RF_RaidAccessFlags_t flags;
162 {
163 RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout);
164 RF_RaidAddr_t startAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr,
165 raidAddr);
166 RF_SectorCount_t numsector = parityPDA->numSector;
167 int numbytes = rf_RaidAddressToByte(raidPtr, numsector);
168 int bytesPerStripe = numbytes * layoutPtr->numDataCol;
169 RF_DagHeader_t *rd_dag_h, *wr_dag_h; /* read, write dag */
170 RF_DagNode_t *blockNode, *unblockNode, *wrBlock, *wrUnblock;
171 RF_AccessStripeMapHeader_t *asm_h;
172 RF_AccessStripeMap_t *asmap;
173 RF_AllocListElem_t *alloclist;
174 RF_PhysDiskAddr_t *pda;
175 char *pbuf, *buf, *end_p, *p;
176 int i, retcode;
177 RF_ReconUnitNum_t which_ru;
178 RF_StripeNum_t psID = rf_RaidAddressToParityStripeID(layoutPtr,
179 raidAddr,
180 &which_ru);
181 int stripeWidth = layoutPtr->numDataCol + layoutPtr->numParityCol;
182 RF_AccTraceEntry_t tracerec;
183 RF_MCPair_t *mcpair;
184
185 retcode = RF_PARITY_OKAY;
186
187 mcpair = rf_AllocMCPair();
188 rf_MakeAllocList(alloclist);
189 RF_MallocAndAdd(buf, numbytes * (layoutPtr->numDataCol + layoutPtr->numParityCol), (char *), alloclist);
190 RF_CallocAndAdd(pbuf, 1, numbytes, (char *), alloclist); /* use calloc to make
191 * sure buffer is zeroed */
192 end_p = buf + bytesPerStripe;
193
194 rd_dag_h = rf_MakeSimpleDAG(raidPtr, stripeWidth, numbytes, buf, rf_DiskReadFunc, rf_DiskReadUndoFunc,
195 "Rod", alloclist, flags, RF_IO_NORMAL_PRIORITY);
196 blockNode = rd_dag_h->succedents[0];
197 unblockNode = blockNode->succedents[0]->succedents[0];
198
199 /* map the stripe and fill in the PDAs in the dag */
200 asm_h = rf_MapAccess(raidPtr, startAddr, layoutPtr->dataSectorsPerStripe, buf, RF_DONT_REMAP);
201 asmap = asm_h->stripeMap;
202
203 for (pda = asmap->physInfo, i = 0; i < layoutPtr->numDataCol; i++, pda = pda->next) {
204 RF_ASSERT(pda);
205 rf_RangeRestrictPDA(raidPtr, parityPDA, pda, 0, 1);
206 RF_ASSERT(pda->numSector != 0);
207 if (rf_TryToRedirectPDA(raidPtr, pda, 0))
208 goto out; /* no way to verify parity if disk is
209 * dead. return w/ good status */
210 blockNode->succedents[i]->params[0].p = pda;
211 blockNode->succedents[i]->params[2].v = psID;
212 blockNode->succedents[i]->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, 0, 0, which_ru);
213 }
214
215 RF_ASSERT(!asmap->parityInfo->next);
216 rf_RangeRestrictPDA(raidPtr, parityPDA, asmap->parityInfo, 0, 1);
217 RF_ASSERT(asmap->parityInfo->numSector != 0);
218 if (rf_TryToRedirectPDA(raidPtr, asmap->parityInfo, 1))
219 goto out;
220 blockNode->succedents[layoutPtr->numDataCol]->params[0].p = asmap->parityInfo;
221
222 /* fire off the DAG */
223 bzero((char *) &tracerec, sizeof(tracerec));
224 rd_dag_h->tracerec = &tracerec;
225
226 if (rf_verifyParityDebug) {
227 printf("Parity verify read dag:\n");
228 rf_PrintDAGList(rd_dag_h);
229 }
230 RF_LOCK_MUTEX(mcpair->mutex);
231 mcpair->flag = 0;
232 rf_DispatchDAG(rd_dag_h, (void (*) (void *)) rf_MCPairWakeupFunc,
233 (void *) mcpair);
234 while (!mcpair->flag)
235 RF_WAIT_COND(mcpair->cond, mcpair->mutex);
236 RF_UNLOCK_MUTEX(mcpair->mutex);
237 if (rd_dag_h->status != rf_enable) {
238 RF_ERRORMSG("Unable to verify parity: can't read the stripe\n");
239 retcode = RF_PARITY_COULD_NOT_VERIFY;
240 goto out;
241 }
242 for (p = buf; p < end_p; p += numbytes) {
243 rf_bxor(p, pbuf, numbytes, NULL);
244 }
245 for (i = 0; i < numbytes; i++) {
246 #if 0
247 if (pbuf[i] != 0 || buf[bytesPerStripe + i] != 0) {
248 printf("Bytes: %d %d %d\n", i, pbuf[i], buf[bytesPerStripe + i]);
249 }
250 #endif
251 if (pbuf[i] != buf[bytesPerStripe + i]) {
252 if (!correct_it)
253 RF_ERRORMSG3("Parity verify error: byte %d of parity is 0x%x should be 0x%x\n",
254 i, (u_char) buf[bytesPerStripe + i], (u_char) pbuf[i]);
255 retcode = RF_PARITY_BAD;
256 break;
257 }
258 }
259
260 if (retcode && correct_it) {
261 wr_dag_h = rf_MakeSimpleDAG(raidPtr, 1, numbytes, pbuf, rf_DiskWriteFunc, rf_DiskWriteUndoFunc,
262 "Wnp", alloclist, flags, RF_IO_NORMAL_PRIORITY);
263 wrBlock = wr_dag_h->succedents[0];
264 wrUnblock = wrBlock->succedents[0]->succedents[0];
265 wrBlock->succedents[0]->params[0].p = asmap->parityInfo;
266 wrBlock->succedents[0]->params[2].v = psID;
267 wrBlock->succedents[0]->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, 0, 0, which_ru);
268 bzero((char *) &tracerec, sizeof(tracerec));
269 wr_dag_h->tracerec = &tracerec;
270 if (rf_verifyParityDebug) {
271 printf("Parity verify write dag:\n");
272 rf_PrintDAGList(wr_dag_h);
273 }
274 RF_LOCK_MUTEX(mcpair->mutex);
275 mcpair->flag = 0;
276 rf_DispatchDAG(wr_dag_h, (void (*) (void *)) rf_MCPairWakeupFunc,
277 (void *) mcpair);
278 while (!mcpair->flag)
279 RF_WAIT_COND(mcpair->cond, mcpair->mutex);
280 RF_UNLOCK_MUTEX(mcpair->mutex);
281 if (wr_dag_h->status != rf_enable) {
282 RF_ERRORMSG("Unable to correct parity in VerifyParity: can't write the stripe\n");
283 retcode = RF_PARITY_COULD_NOT_CORRECT;
284 }
285 rf_FreeDAG(wr_dag_h);
286 if (retcode == RF_PARITY_BAD)
287 retcode = RF_PARITY_CORRECTED;
288 }
289 out:
290 rf_FreeAccessStripeMap(asm_h);
291 rf_FreeAllocList(alloclist);
292 rf_FreeDAG(rd_dag_h);
293 rf_FreeMCPair(mcpair);
294 return (retcode);
295 }
296
297 int
298 rf_TryToRedirectPDA(raidPtr, pda, parity)
299 RF_Raid_t *raidPtr;
300 RF_PhysDiskAddr_t *pda;
301 int parity;
302 {
303 if (raidPtr->Disks[pda->row][pda->col].status == rf_ds_reconstructing) {
304 if (rf_CheckRUReconstructed(raidPtr->reconControl[pda->row]->reconMap, pda->startSector)) {
305 if (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE) {
306 RF_RowCol_t or = pda->row, oc = pda->col;
307 RF_SectorNum_t os = pda->startSector;
308 if (parity) {
309 (raidPtr->Layout.map->MapParity) (raidPtr, pda->raidAddress, &pda->row, &pda->col, &pda->startSector, RF_REMAP);
310 if (rf_verifyParityDebug)
311 printf("VerifyParity: Redir P r %d c %d sect %ld -> r %d c %d sect %ld\n",
312 or, oc, (long) os, pda->row, pda->col, (long) pda->startSector);
313 } else {
314 (raidPtr->Layout.map->MapSector) (raidPtr, pda->raidAddress, &pda->row, &pda->col, &pda->startSector, RF_REMAP);
315 if (rf_verifyParityDebug)
316 printf("VerifyParity: Redir D r %d c %d sect %ld -> r %d c %d sect %ld\n",
317 or, oc, (long) os, pda->row, pda->col, (long) pda->startSector);
318 }
319 } else {
320 RF_RowCol_t spRow = raidPtr->Disks[pda->row][pda->col].spareRow;
321 RF_RowCol_t spCol = raidPtr->Disks[pda->row][pda->col].spareCol;
322 pda->row = spRow;
323 pda->col = spCol;
324 }
325 }
326 }
327 if (RF_DEAD_DISK(raidPtr->Disks[pda->row][pda->col].status))
328 return (1);
329 return (0);
330 }
331 /*****************************************************************************************
332 *
333 * currently a stub.
334 *
335 * takes as input an ASM describing a write operation and containing one failure, and
336 * verifies that the parity was correctly updated to reflect the write.
337 *
338 * if it's a data unit that's failed, we read the other data units in the stripe and
339 * the parity unit, XOR them together, and verify that we get the data intended for
340 * the failed disk. Since it's easy, we also validate that the right data got written
341 * to the surviving data disks.
342 *
343 * If it's the parity that failed, there's really no validation we can do except the
344 * above verification that the right data got written to all disks. This is because
345 * the new data intended for the failed disk is supplied in the ASM, but this is of
346 * course not the case for the new parity.
347 *
348 ****************************************************************************************/
349 int
350 rf_VerifyDegrModeWrite(raidPtr, asmh)
351 RF_Raid_t *raidPtr;
352 RF_AccessStripeMapHeader_t *asmh;
353 {
354 return (0);
355 }
356 /* creates a simple DAG with a header, a block-recon node at level 1,
357 * nNodes nodes at level 2, an unblock-recon node at level 3, and
358 * a terminator node at level 4. The stripe address field in
359 * the block and unblock nodes are not touched, nor are the pda
360 * fields in the second-level nodes, so they must be filled in later.
361 *
362 * commit point is established at unblock node - this means that any
363 * failure during dag execution causes the dag to fail
364 */
365 RF_DagHeader_t *
366 rf_MakeSimpleDAG(raidPtr, nNodes, bytesPerSU, databuf, doFunc, undoFunc, name, alloclist, flags, priority)
367 RF_Raid_t *raidPtr;
368 int nNodes;
369 int bytesPerSU;
370 char *databuf;
371 int (*doFunc) (RF_DagNode_t * node);
372 int (*undoFunc) (RF_DagNode_t * node);
373 char *name; /* node names at the second level */
374 RF_AllocListElem_t *alloclist;
375 RF_RaidAccessFlags_t flags;
376 int priority;
377 {
378 RF_DagHeader_t *dag_h;
379 RF_DagNode_t *nodes, *termNode, *blockNode, *unblockNode;
380 int i;
381
382 /* create the nodes, the block & unblock nodes, and the terminator
383 * node */
384 RF_CallocAndAdd(nodes, nNodes + 3, sizeof(RF_DagNode_t), (RF_DagNode_t *), alloclist);
385 blockNode = &nodes[nNodes];
386 unblockNode = blockNode + 1;
387 termNode = unblockNode + 1;
388
389 dag_h = rf_AllocDAGHeader();
390 dag_h->raidPtr = (void *) raidPtr;
391 dag_h->allocList = NULL;/* we won't use this alloc list */
392 dag_h->status = rf_enable;
393 dag_h->numSuccedents = 1;
394 dag_h->creator = "SimpleDAG";
395
396 /* this dag can not commit until the unblock node is reached errors
397 * prior to the commit point imply the dag has failed */
398 dag_h->numCommitNodes = 1;
399 dag_h->numCommits = 0;
400
401 dag_h->succedents[0] = blockNode;
402 rf_InitNode(blockNode, rf_wait, RF_FALSE, rf_NullNodeFunc, rf_NullNodeUndoFunc, NULL, nNodes, 0, 0, 0, dag_h, "Nil", alloclist);
403 rf_InitNode(unblockNode, rf_wait, RF_TRUE, rf_NullNodeFunc, rf_NullNodeUndoFunc, NULL, 1, nNodes, 0, 0, dag_h, "Nil", alloclist);
404 unblockNode->succedents[0] = termNode;
405 for (i = 0; i < nNodes; i++) {
406 blockNode->succedents[i] = unblockNode->antecedents[i] = &nodes[i];
407 unblockNode->antType[i] = rf_control;
408 rf_InitNode(&nodes[i], rf_wait, RF_FALSE, doFunc, undoFunc, rf_GenericWakeupFunc, 1, 1, 4, 0, dag_h, name, alloclist);
409 nodes[i].succedents[0] = unblockNode;
410 nodes[i].antecedents[0] = blockNode;
411 nodes[i].antType[0] = rf_control;
412 nodes[i].params[1].p = (databuf + (i * bytesPerSU));
413 }
414 rf_InitNode(termNode, rf_wait, RF_FALSE, rf_TerminateFunc, rf_TerminateUndoFunc, NULL, 0, 1, 0, 0, dag_h, "Trm", alloclist);
415 termNode->antecedents[0] = unblockNode;
416 termNode->antType[0] = rf_control;
417 return (dag_h);
418 }
419