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