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