Home | History | Annotate | Line # | Download | only in raidframe
rf_parityscan.c revision 1.22
      1 /*	$NetBSD: rf_parityscan.c,v 1.22 2003/12/31 16:23:50 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.22 2003/12/31 16:23:50 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.  This works by
     53  * creating two DAGs, one to read a stripe of data and one to write
     54  * new parity.  The first is executed, the data is xored together, and
     55  * then the second is executed.  To avoid constantly building and
     56  * tearing down the DAGs, we create them a priori and fill them in
     57  * with the mapping 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(RF_Raid_t *raidPtr)
     65 {
     66 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
     67 	RF_AccessStripeMapHeader_t *asm_h;
     68 	int ret_val;
     69 	int rc;
     70 	RF_SectorNum_t i;
     71 
     72 	if (raidPtr->Layout.map->faultsTolerated == 0) {
     73 		/* There isn't any parity. Call it "okay." */
     74 		return (RF_PARITY_OKAY);
     75 	}
     76 	if (raidPtr->status != rf_rs_optimal) {
     77 		/*
     78 		 * We're in degraded mode.  Don't try to verify parity now!
     79 		 * XXX: this should be a "we don't want to", not a
     80 		 * "we can't" error.
     81 		 */
     82 		return (RF_PARITY_COULD_NOT_VERIFY);
     83 	}
     84 
     85 	ret_val = 0;
     86 
     87 	rc = RF_PARITY_OKAY;
     88 
     89 	for (i = 0; i < raidPtr->totalSectors &&
     90 		     rc <= RF_PARITY_CORRECTED;
     91 	     i += layoutPtr->dataSectorsPerStripe) {
     92 		if (raidPtr->waitShutdown) {
     93 			/* Someone is pulling the plug on this set...
     94 			   abort the re-write */
     95 			return (1);
     96 		}
     97 		asm_h = rf_MapAccess(raidPtr, i,
     98 				     layoutPtr->dataSectorsPerStripe,
     99 				     NULL, RF_DONT_REMAP);
    100 		raidPtr->parity_rewrite_stripes_done =
    101 			i / layoutPtr->dataSectorsPerStripe ;
    102 		rc = rf_VerifyParity(raidPtr, asm_h->stripeMap, 1, 0);
    103 
    104 		switch (rc) {
    105 		case RF_PARITY_OKAY:
    106 		case RF_PARITY_CORRECTED:
    107 			break;
    108 		case RF_PARITY_BAD:
    109 			printf("Parity bad during correction\n");
    110 			ret_val = 1;
    111 			break;
    112 		case RF_PARITY_COULD_NOT_CORRECT:
    113 			printf("Could not correct bad parity\n");
    114 			ret_val = 1;
    115 			break;
    116 		case RF_PARITY_COULD_NOT_VERIFY:
    117 			printf("Could not verify parity\n");
    118 			ret_val = 1;
    119 			break;
    120 		default:
    121 			printf("Bad rc=%d from VerifyParity in RewriteParity\n", rc);
    122 			ret_val = 1;
    123 		}
    124 		rf_FreeAccessStripeMap(asm_h);
    125 	}
    126 	return (ret_val);
    127 }
    128 /*****************************************************************************
    129  *
    130  * verify that the parity in a particular stripe is correct.  we
    131  * validate only the range of parity defined by parityPDA, since this
    132  * is all we have locked.  The way we do this is to create an asm that
    133  * maps the whole stripe and then range-restrict it to the parity
    134  * region defined by the parityPDA.
    135  *
    136  ****************************************************************************/
    137 int
    138 rf_VerifyParity(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *aasm,
    139 		int correct_it, RF_RaidAccessFlags_t flags)
    140 {
    141 	RF_PhysDiskAddr_t *parityPDA;
    142 	RF_AccessStripeMap_t *doasm;
    143 	const RF_LayoutSW_t *lp;
    144 	int     lrc, rc;
    145 
    146 	lp = raidPtr->Layout.map;
    147 	if (lp->faultsTolerated == 0) {
    148 		/*
    149 	         * There isn't any parity. Call it "okay."
    150 	         */
    151 		return (RF_PARITY_OKAY);
    152 	}
    153 	rc = RF_PARITY_OKAY;
    154 	if (lp->VerifyParity) {
    155 		for (doasm = aasm; doasm; doasm = doasm->next) {
    156 			for (parityPDA = doasm->parityInfo; parityPDA;
    157 			     parityPDA = parityPDA->next) {
    158 				lrc = lp->VerifyParity(raidPtr,
    159 						       doasm->raidAddress,
    160 						       parityPDA,
    161 						       correct_it, flags);
    162 				if (lrc > rc) {
    163 					/* see rf_parityscan.h for why this
    164 					 * works */
    165 					rc = lrc;
    166 				}
    167 			}
    168 		}
    169 	} else {
    170 		rc = RF_PARITY_COULD_NOT_VERIFY;
    171 	}
    172 	return (rc);
    173 }
    174 
    175 int
    176 rf_VerifyParityBasic(RF_Raid_t *raidPtr, RF_RaidAddr_t raidAddr,
    177 		     RF_PhysDiskAddr_t *parityPDA, 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, *wrBlock;
    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_MallocAndAdd(pbuf, numbytes, (char *), alloclist);
    208 	end_p = buf + bytesPerStripe;
    209 
    210 	rd_dag_h = rf_MakeSimpleDAG(raidPtr, stripeWidth, numbytes, buf, rf_DiskReadFunc, rf_DiskReadUndoFunc,
    211 	    "Rod", alloclist, flags, RF_IO_NORMAL_PRIORITY);
    212 	blockNode = rd_dag_h->succedents[0];
    213 
    214 	/* map the stripe and fill in the PDAs in the dag */
    215 	asm_h = rf_MapAccess(raidPtr, startAddr, layoutPtr->dataSectorsPerStripe, buf, RF_DONT_REMAP);
    216 	asmap = asm_h->stripeMap;
    217 
    218 	for (pda = asmap->physInfo, i = 0; i < layoutPtr->numDataCol; i++, pda = pda->next) {
    219 		RF_ASSERT(pda);
    220 		rf_RangeRestrictPDA(raidPtr, parityPDA, pda, 0, 1);
    221 		RF_ASSERT(pda->numSector != 0);
    222 		if (rf_TryToRedirectPDA(raidPtr, pda, 0))
    223 			goto out;	/* no way to verify parity if disk is
    224 					 * dead.  return w/ good status */
    225 		blockNode->succedents[i]->params[0].p = pda;
    226 		blockNode->succedents[i]->params[2].v = psID;
    227 		blockNode->succedents[i]->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, 0, 0, which_ru);
    228 	}
    229 
    230 	RF_ASSERT(!asmap->parityInfo->next);
    231 	rf_RangeRestrictPDA(raidPtr, parityPDA, asmap->parityInfo, 0, 1);
    232 	RF_ASSERT(asmap->parityInfo->numSector != 0);
    233 	if (rf_TryToRedirectPDA(raidPtr, asmap->parityInfo, 1))
    234 		goto out;
    235 	blockNode->succedents[layoutPtr->numDataCol]->params[0].p = asmap->parityInfo;
    236 
    237 	/* fire off the DAG */
    238 	memset((char *) &tracerec, 0, sizeof(tracerec));
    239 	rd_dag_h->tracerec = &tracerec;
    240 #if 0
    241 	if (rf_verifyParityDebug) {
    242 		printf("Parity verify read dag:\n");
    243 		rf_PrintDAGList(rd_dag_h);
    244 	}
    245 #endif
    246 	RF_LOCK_MUTEX(mcpair->mutex);
    247 	mcpair->flag = 0;
    248 	rf_DispatchDAG(rd_dag_h, (void (*) (void *)) rf_MCPairWakeupFunc,
    249 	    (void *) mcpair);
    250 	while (!mcpair->flag)
    251 		RF_WAIT_COND(mcpair->cond, mcpair->mutex);
    252 	RF_UNLOCK_MUTEX(mcpair->mutex);
    253 	if (rd_dag_h->status != rf_enable) {
    254 		RF_ERRORMSG("Unable to verify parity:  can't read the stripe\n");
    255 		retcode = RF_PARITY_COULD_NOT_VERIFY;
    256 		goto out;
    257 	}
    258 	for (p = buf; p < end_p; p += numbytes) {
    259 		rf_bxor(p, pbuf, numbytes);
    260 	}
    261 	for (i = 0; i < numbytes; i++) {
    262 		if (pbuf[i] != buf[bytesPerStripe + i]) {
    263 			if (!correct_it)
    264 				RF_ERRORMSG3("Parity verify error: byte %d of parity is 0x%x should be 0x%x\n",
    265 				    i, (u_char) buf[bytesPerStripe + i], (u_char) pbuf[i]);
    266 			retcode = RF_PARITY_BAD;
    267 			break;
    268 		}
    269 	}
    270 
    271 	if (retcode && correct_it) {
    272 		wr_dag_h = rf_MakeSimpleDAG(raidPtr, 1, numbytes, pbuf, rf_DiskWriteFunc, rf_DiskWriteUndoFunc,
    273 		    "Wnp", alloclist, flags, RF_IO_NORMAL_PRIORITY);
    274 		wrBlock = wr_dag_h->succedents[0];
    275 		wrBlock->succedents[0]->params[0].p = asmap->parityInfo;
    276 		wrBlock->succedents[0]->params[2].v = psID;
    277 		wrBlock->succedents[0]->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, 0, 0, which_ru);
    278 		memset((char *) &tracerec, 0, sizeof(tracerec));
    279 		wr_dag_h->tracerec = &tracerec;
    280 #if 0
    281 		if (rf_verifyParityDebug) {
    282 			printf("Parity verify write dag:\n");
    283 			rf_PrintDAGList(wr_dag_h);
    284 		}
    285 #endif
    286 		RF_LOCK_MUTEX(mcpair->mutex);
    287 		mcpair->flag = 0;
    288 		rf_DispatchDAG(wr_dag_h, (void (*) (void *)) rf_MCPairWakeupFunc,
    289 		    (void *) mcpair);
    290 		while (!mcpair->flag)
    291 			RF_WAIT_COND(mcpair->cond, mcpair->mutex);
    292 		RF_UNLOCK_MUTEX(mcpair->mutex);
    293 		if (wr_dag_h->status != rf_enable) {
    294 			RF_ERRORMSG("Unable to correct parity in VerifyParity:  can't write the stripe\n");
    295 			retcode = RF_PARITY_COULD_NOT_CORRECT;
    296 		}
    297 		rf_FreeDAG(wr_dag_h);
    298 		if (retcode == RF_PARITY_BAD)
    299 			retcode = RF_PARITY_CORRECTED;
    300 	}
    301 out:
    302 	rf_FreeAccessStripeMap(asm_h);
    303 	rf_FreeAllocList(alloclist);
    304 	rf_FreeDAG(rd_dag_h);
    305 	rf_FreeMCPair(mcpair);
    306 	return (retcode);
    307 }
    308 
    309 int
    310 rf_TryToRedirectPDA(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *pda, int parity)
    311 {
    312 	if (raidPtr->Disks[pda->col].status == rf_ds_reconstructing) {
    313 		if (rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, pda->startSector)) {
    314 			if (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE) {
    315 #if RF_DEBUG_VERIFYPARITY
    316 				RF_RowCol_t oc = pda->col;
    317 				RF_SectorNum_t os = pda->startSector;
    318 #endif
    319 				if (parity) {
    320 					(raidPtr->Layout.map->MapParity) (raidPtr, pda->raidAddress, &pda->col, &pda->startSector, RF_REMAP);
    321 #if RF_DEBUG_VERIFYPARITY
    322 					if (rf_verifyParityDebug)
    323 						printf("VerifyParity: Redir P c %d sect %ld -> c %d sect %ld\n",
    324 						    oc, (long) os, pda->col, (long) pda->startSector);
    325 #endif
    326 				} else {
    327 					(raidPtr->Layout.map->MapSector) (raidPtr, pda->raidAddress, &pda->col, &pda->startSector, RF_REMAP);
    328 #if RF_DEBUG_VERIFYPARITY
    329 					if (rf_verifyParityDebug)
    330 						printf("VerifyParity: Redir D c %d sect %ld -> c %d sect %ld\n",
    331 						   oc, (long) os, pda->col, (long) pda->startSector);
    332 #endif
    333 				}
    334 			} else {
    335 				RF_RowCol_t spCol = raidPtr->Disks[pda->col].spareCol;
    336 				pda->col = spCol;
    337 			}
    338 		}
    339 	}
    340 	if (RF_DEAD_DISK(raidPtr->Disks[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
    349  * one failure, and verifies that the parity was correctly updated to
    350  * reflect the write.
    351  *
    352  * if it's a data unit that's failed, we read the other data units in
    353  * the stripe and the parity unit, XOR them together, and verify that
    354  * we get the data intended for the failed disk.  Since it's easy, we
    355  * also validate that the right data got written to the surviving data
    356  * disks.
    357  *
    358  * If it's the parity that failed, there's really no validation we can
    359  * do except the above verification that the right data got written to
    360  * all disks.  This is because the new data intended for the failed
    361  * disk is supplied in the ASM, but this is of course not the case for
    362  * the new parity.
    363  *
    364  ****************************************************************************/
    365 #if 0
    366 int
    367 rf_VerifyDegrModeWrite(RF_Raid_t *raidPtr, RF_AccessStripeMapHeader_t *asmh)
    368 {
    369 	return (0);
    370 }
    371 #endif
    372 /* creates a simple DAG with a header, a block-recon node at level 1,
    373  * nNodes nodes at level 2, an unblock-recon node at level 3, and a
    374  * terminator node at level 4.  The stripe address field in the block
    375  * and unblock nodes are not touched, nor are the pda fields in the
    376  * second-level nodes, so they must be filled in later.
    377  *
    378  * commit point is established at unblock node - this means that any
    379  * failure during dag execution causes the dag to fail
    380  *
    381  * name - node names at the second level
    382  */
    383 RF_DagHeader_t *
    384 rf_MakeSimpleDAG(RF_Raid_t *raidPtr, int nNodes, int bytesPerSU, char *databuf,
    385 		 int (*doFunc) (RF_DagNode_t * node),
    386 		 int (*undoFunc) (RF_DagNode_t * node),
    387 		 char *name, RF_AllocListElem_t *alloclist,
    388 		 RF_RaidAccessFlags_t flags, int priority)
    389 {
    390 	RF_DagHeader_t *dag_h;
    391 	RF_DagNode_t *nodes, *termNode, *blockNode, *unblockNode;
    392 	int     i;
    393 
    394 	/* create the nodes, the block & unblock nodes, and the terminator
    395 	 * node */
    396 	RF_MallocAndAdd(nodes, (nNodes + 3) * sizeof(RF_DagNode_t),
    397 			(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