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