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