Home | History | Annotate | Line # | Download | only in raidframe
rf_raid1.c revision 1.17
      1 /*	$NetBSD: rf_raid1.c,v 1.17 2004/01/10 00:56:28 oster Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author: William V. Courtright II
      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_raid1.c -- implements RAID Level 1
     32  *
     33  *****************************************************************************/
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: rf_raid1.c,v 1.17 2004/01/10 00:56:28 oster Exp $");
     37 
     38 #include "rf_raid.h"
     39 #include "rf_raid1.h"
     40 #include "rf_dag.h"
     41 #include "rf_dagffrd.h"
     42 #include "rf_dagffwr.h"
     43 #include "rf_dagdegrd.h"
     44 #include "rf_dagutils.h"
     45 #include "rf_dagfuncs.h"
     46 #include "rf_diskqueue.h"
     47 #include "rf_general.h"
     48 #include "rf_utils.h"
     49 #include "rf_parityscan.h"
     50 #include "rf_mcpair.h"
     51 #include "rf_layout.h"
     52 #include "rf_map.h"
     53 #include "rf_engine.h"
     54 #include "rf_reconbuffer.h"
     55 
     56 typedef struct RF_Raid1ConfigInfo_s {
     57 	RF_RowCol_t **stripeIdentifier;
     58 }       RF_Raid1ConfigInfo_t;
     59 /* start of day code specific to RAID level 1 */
     60 int
     61 rf_ConfigureRAID1(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
     62 		  RF_Config_t *cfgPtr)
     63 {
     64 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
     65 	RF_Raid1ConfigInfo_t *info;
     66 	RF_RowCol_t i;
     67 
     68 	/* create a RAID level 1 configuration structure */
     69 	RF_MallocAndAdd(info, sizeof(RF_Raid1ConfigInfo_t), (RF_Raid1ConfigInfo_t *), raidPtr->cleanupList);
     70 	if (info == NULL)
     71 		return (ENOMEM);
     72 	layoutPtr->layoutSpecificInfo = (void *) info;
     73 
     74 	/* ... and fill it in. */
     75 	info->stripeIdentifier = rf_make_2d_array(raidPtr->numCol / 2, 2, raidPtr->cleanupList);
     76 	if (info->stripeIdentifier == NULL)
     77 		return (ENOMEM);
     78 	for (i = 0; i < (raidPtr->numCol / 2); i++) {
     79 		info->stripeIdentifier[i][0] = (2 * i);
     80 		info->stripeIdentifier[i][1] = (2 * i) + 1;
     81 	}
     82 
     83 	/* this implementation of RAID level 1 uses one row of numCol disks
     84 	 * and allows multiple (numCol / 2) stripes per row.  A stripe
     85 	 * consists of a single data unit and a single parity (mirror) unit.
     86 	 * stripe id = raidAddr / stripeUnitSize */
     87 	raidPtr->totalSectors = layoutPtr->stripeUnitsPerDisk * (raidPtr->numCol / 2) * layoutPtr->sectorsPerStripeUnit;
     88 	layoutPtr->numStripe = layoutPtr->stripeUnitsPerDisk * (raidPtr->numCol / 2);
     89 	layoutPtr->dataSectorsPerStripe = layoutPtr->sectorsPerStripeUnit;
     90 	layoutPtr->numDataCol = 1;
     91 	layoutPtr->numParityCol = 1;
     92 	return (0);
     93 }
     94 
     95 
     96 /* returns the physical disk location of the primary copy in the mirror pair */
     97 void
     98 rf_MapSectorRAID1(RF_Raid_t *raidPtr, RF_RaidAddr_t raidSector,
     99 		  RF_RowCol_t *col, RF_SectorNum_t *diskSector, int remap)
    100 {
    101 	RF_StripeNum_t SUID = raidSector / raidPtr->Layout.sectorsPerStripeUnit;
    102 	RF_RowCol_t mirrorPair = SUID % (raidPtr->numCol / 2);
    103 
    104 	*col = 2 * mirrorPair;
    105 	*diskSector = ((SUID / (raidPtr->numCol / 2)) * raidPtr->Layout.sectorsPerStripeUnit) + (raidSector % raidPtr->Layout.sectorsPerStripeUnit);
    106 }
    107 
    108 
    109 /* Map Parity
    110  *
    111  * returns the physical disk location of the secondary copy in the mirror
    112  * pair
    113  */
    114 void
    115 rf_MapParityRAID1(RF_Raid_t *raidPtr, RF_RaidAddr_t raidSector,
    116 		  RF_RowCol_t *col, RF_SectorNum_t *diskSector, int remap)
    117 {
    118 	RF_StripeNum_t SUID = raidSector / raidPtr->Layout.sectorsPerStripeUnit;
    119 	RF_RowCol_t mirrorPair = SUID % (raidPtr->numCol / 2);
    120 
    121 	*col = (2 * mirrorPair) + 1;
    122 
    123 	*diskSector = ((SUID / (raidPtr->numCol / 2)) * raidPtr->Layout.sectorsPerStripeUnit) + (raidSector % raidPtr->Layout.sectorsPerStripeUnit);
    124 }
    125 
    126 
    127 /* IdentifyStripeRAID1
    128  *
    129  * returns a list of disks for a given redundancy group
    130  */
    131 void
    132 rf_IdentifyStripeRAID1(RF_Raid_t *raidPtr, RF_RaidAddr_t addr,
    133 		       RF_RowCol_t **diskids)
    134 {
    135 	RF_StripeNum_t stripeID = rf_RaidAddressToStripeID(&raidPtr->Layout, addr);
    136 	RF_Raid1ConfigInfo_t *info = raidPtr->Layout.layoutSpecificInfo;
    137 	RF_ASSERT(stripeID >= 0);
    138 	RF_ASSERT(addr >= 0);
    139 	*diskids = info->stripeIdentifier[stripeID % (raidPtr->numCol / 2)];
    140 	RF_ASSERT(*diskids);
    141 }
    142 
    143 
    144 /* MapSIDToPSIDRAID1
    145  *
    146  * maps a logical stripe to a stripe in the redundant array
    147  */
    148 void
    149 rf_MapSIDToPSIDRAID1(RF_RaidLayout_t *layoutPtr, RF_StripeNum_t stripeID,
    150 		     RF_StripeNum_t *psID, RF_ReconUnitNum_t *which_ru)
    151 {
    152 	*which_ru = 0;
    153 	*psID = stripeID;
    154 }
    155 
    156 
    157 
    158 /******************************************************************************
    159  * select a graph to perform a single-stripe access
    160  *
    161  * Parameters:  raidPtr    - description of the physical array
    162  *              type       - type of operation (read or write) requested
    163  *              asmap      - logical & physical addresses for this access
    164  *              createFunc - name of function to use to create the graph
    165  *****************************************************************************/
    166 
    167 void
    168 rf_RAID1DagSelect(RF_Raid_t *raidPtr, RF_IoType_t type,
    169 		  RF_AccessStripeMap_t *asmap, RF_VoidFuncPtr *createFunc)
    170 {
    171 	RF_RowCol_t fcol, oc;
    172 	RF_PhysDiskAddr_t *failedPDA;
    173 	int     prior_recon;
    174 	RF_RowStatus_t rstat;
    175 	RF_SectorNum_t oo;
    176 
    177 
    178 	RF_ASSERT(RF_IO_IS_R_OR_W(type));
    179 
    180 	if (asmap->numDataFailed + asmap->numParityFailed > 1) {
    181 		if (rf_dagDebug)
    182 			RF_ERRORMSG("Multiple disks failed in a single group!  Aborting I/O operation.\n");
    183 		*createFunc = NULL;
    184 		return;
    185 	}
    186 	if (asmap->numDataFailed + asmap->numParityFailed) {
    187 		/*
    188 	         * We've got a fault. Re-map to spare space, iff applicable.
    189 	         * Shouldn't the arch-independent code do this for us?
    190 	         * Anyway, it turns out if we don't do this here, then when
    191 	         * we're reconstructing, writes go only to the surviving
    192 	         * original disk, and aren't reflected on the reconstructed
    193 	         * spare. Oops. --jimz
    194 	         */
    195 		failedPDA = asmap->failedPDAs[0];
    196 		fcol = failedPDA->col;
    197 		rstat = raidPtr->status;
    198 		prior_recon = (rstat == rf_rs_reconfigured) || (
    199 		    (rstat == rf_rs_reconstructing) ?
    200 		    rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, failedPDA->startSector) : 0
    201 		    );
    202 		if (prior_recon) {
    203 			oc = fcol;
    204 			oo = failedPDA->startSector;
    205 			/*
    206 		         * If we did distributed sparing, we'd monkey with that here.
    207 		         * But we don't, so we'll
    208 		         */
    209 			failedPDA->col = raidPtr->Disks[fcol].spareCol;
    210 			/*
    211 		         * Redirect other components, iff necessary. This looks
    212 		         * pretty suspicious to me, but it's what the raid5
    213 		         * DAG select does.
    214 		         */
    215 			if (asmap->parityInfo->next) {
    216 				if (failedPDA == asmap->parityInfo) {
    217 					failedPDA->next->col = failedPDA->col;
    218 				} else {
    219 					if (failedPDA == asmap->parityInfo->next) {
    220 						asmap->parityInfo->col = failedPDA->col;
    221 					}
    222 				}
    223 			}
    224 			if (rf_dagDebug || rf_mapDebug) {
    225 				printf("raid%d: Redirected type '%c' c %d o %ld -> c %d o %ld\n",
    226 				       raidPtr->raidid, type, oc,
    227 				       (long) oo,
    228 				       failedPDA->col,
    229 				       (long) failedPDA->startSector);
    230 			}
    231 			asmap->numDataFailed = asmap->numParityFailed = 0;
    232 		}
    233 	}
    234 	if (type == RF_IO_TYPE_READ) {
    235 		if (asmap->numDataFailed == 0)
    236 			*createFunc = (RF_VoidFuncPtr) rf_CreateMirrorIdleReadDAG;
    237 		else
    238 			*createFunc = (RF_VoidFuncPtr) rf_CreateRaidOneDegradedReadDAG;
    239 	} else {
    240 		*createFunc = (RF_VoidFuncPtr) rf_CreateRaidOneWriteDAG;
    241 	}
    242 }
    243 
    244 int
    245 rf_VerifyParityRAID1(RF_Raid_t *raidPtr, RF_RaidAddr_t raidAddr,
    246 		     RF_PhysDiskAddr_t *parityPDA, int correct_it,
    247 		     RF_RaidAccessFlags_t flags)
    248 {
    249 	int     nbytes, bcount, stripeWidth, ret, i, j, nbad, *bbufs;
    250 	RF_DagNode_t *blockNode, *wrBlock;
    251 	RF_DagHeader_t *rd_dag_h, *wr_dag_h;
    252 	RF_AccessStripeMapHeader_t *asm_h;
    253 	RF_AllocListElem_t *allocList;
    254 	RF_AccTraceEntry_t tracerec;
    255 	RF_ReconUnitNum_t which_ru;
    256 	RF_RaidLayout_t *layoutPtr;
    257 	RF_AccessStripeMap_t *aasm;
    258 	RF_SectorCount_t nsector;
    259 	RF_RaidAddr_t startAddr;
    260 	char   *buf, *buf1, *buf2;
    261 	RF_PhysDiskAddr_t *pda;
    262 	RF_StripeNum_t psID;
    263 	RF_MCPair_t *mcpair;
    264 
    265 	layoutPtr = &raidPtr->Layout;
    266 	startAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, raidAddr);
    267 	nsector = parityPDA->numSector;
    268 	nbytes = rf_RaidAddressToByte(raidPtr, nsector);
    269 	psID = rf_RaidAddressToParityStripeID(layoutPtr, raidAddr, &which_ru);
    270 
    271 	asm_h = NULL;
    272 	rd_dag_h = wr_dag_h = NULL;
    273 	mcpair = NULL;
    274 
    275 	ret = RF_PARITY_COULD_NOT_VERIFY;
    276 
    277 	rf_MakeAllocList(allocList);
    278 	if (allocList == NULL)
    279 		return (RF_PARITY_COULD_NOT_VERIFY);
    280 	mcpair = rf_AllocMCPair();
    281 	if (mcpair == NULL)
    282 		goto done;
    283 	RF_ASSERT(layoutPtr->numDataCol == layoutPtr->numParityCol);
    284 	stripeWidth = layoutPtr->numDataCol + layoutPtr->numParityCol;
    285 	bcount = nbytes * (layoutPtr->numDataCol + layoutPtr->numParityCol);
    286 	RF_MallocAndAdd(buf, bcount, (char *), allocList);
    287 	if (buf == NULL)
    288 		goto done;
    289 #if RF_DEBUG_VERIFYPARITY
    290 	if (rf_verifyParityDebug) {
    291 		printf("raid%d: RAID1 parity verify: buf=%lx bcount=%d (%lx - %lx)\n",
    292 		       raidPtr->raidid, (long) buf, bcount, (long) buf,
    293 		       (long) buf + bcount);
    294 	}
    295 #endif
    296 	/*
    297          * Generate a DAG which will read the entire stripe- then we can
    298          * just compare data chunks versus "parity" chunks.
    299          */
    300 
    301 	rd_dag_h = rf_MakeSimpleDAG(raidPtr, stripeWidth, nbytes, buf,
    302 	    rf_DiskReadFunc, rf_DiskReadUndoFunc, "Rod", allocList, flags,
    303 	    RF_IO_NORMAL_PRIORITY);
    304 	if (rd_dag_h == NULL)
    305 		goto done;
    306 	blockNode = rd_dag_h->succedents[0];
    307 
    308 	/*
    309          * Map the access to physical disk addresses (PDAs)- this will
    310          * get us both a list of data addresses, and "parity" addresses
    311          * (which are really mirror copies).
    312          */
    313 	asm_h = rf_MapAccess(raidPtr, startAddr, layoutPtr->dataSectorsPerStripe,
    314 	    buf, RF_DONT_REMAP);
    315 	aasm = asm_h->stripeMap;
    316 
    317 	buf1 = buf;
    318 	/*
    319          * Loop through the data blocks, setting up read nodes for each.
    320          */
    321 	for (pda = aasm->physInfo, i = 0; i < layoutPtr->numDataCol; i++, pda = pda->next) {
    322 		RF_ASSERT(pda);
    323 
    324 		rf_RangeRestrictPDA(raidPtr, parityPDA, pda, 0, 1);
    325 
    326 		RF_ASSERT(pda->numSector != 0);
    327 		if (rf_TryToRedirectPDA(raidPtr, pda, 0)) {
    328 			/* cannot verify parity with dead disk */
    329 			goto done;
    330 		}
    331 		pda->bufPtr = buf1;
    332 		blockNode->succedents[i]->params[0].p = pda;
    333 		blockNode->succedents[i]->params[1].p = buf1;
    334 		blockNode->succedents[i]->params[2].v = psID;
    335 		blockNode->succedents[i]->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, which_ru);
    336 		buf1 += nbytes;
    337 	}
    338 	RF_ASSERT(pda == NULL);
    339 	/*
    340          * keep i, buf1 running
    341          *
    342          * Loop through parity blocks, setting up read nodes for each.
    343          */
    344 	for (pda = aasm->parityInfo; i < layoutPtr->numDataCol + layoutPtr->numParityCol; i++, pda = pda->next) {
    345 		RF_ASSERT(pda);
    346 		rf_RangeRestrictPDA(raidPtr, parityPDA, pda, 0, 1);
    347 		RF_ASSERT(pda->numSector != 0);
    348 		if (rf_TryToRedirectPDA(raidPtr, pda, 0)) {
    349 			/* cannot verify parity with dead disk */
    350 			goto done;
    351 		}
    352 		pda->bufPtr = buf1;
    353 		blockNode->succedents[i]->params[0].p = pda;
    354 		blockNode->succedents[i]->params[1].p = buf1;
    355 		blockNode->succedents[i]->params[2].v = psID;
    356 		blockNode->succedents[i]->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, which_ru);
    357 		buf1 += nbytes;
    358 	}
    359 	RF_ASSERT(pda == NULL);
    360 
    361 	memset((char *) &tracerec, 0, sizeof(tracerec));
    362 	rd_dag_h->tracerec = &tracerec;
    363 
    364 #if 0
    365 	if (rf_verifyParityDebug > 1) {
    366 		printf("raid%d: RAID1 parity verify read dag:\n",
    367 		       raidPtr->raidid);
    368 		rf_PrintDAGList(rd_dag_h);
    369 	}
    370 #endif
    371 	RF_LOCK_MUTEX(mcpair->mutex);
    372 	mcpair->flag = 0;
    373 	rf_DispatchDAG(rd_dag_h, (void (*) (void *)) rf_MCPairWakeupFunc,
    374 	    (void *) mcpair);
    375 	while (mcpair->flag == 0) {
    376 		RF_WAIT_MCPAIR(mcpair);
    377 	}
    378 	RF_UNLOCK_MUTEX(mcpair->mutex);
    379 
    380 	if (rd_dag_h->status != rf_enable) {
    381 		RF_ERRORMSG("Unable to verify raid1 parity: can't read stripe\n");
    382 		ret = RF_PARITY_COULD_NOT_VERIFY;
    383 		goto done;
    384 	}
    385 	/*
    386          * buf1 is the beginning of the data blocks chunk
    387          * buf2 is the beginning of the parity blocks chunk
    388          */
    389 	buf1 = buf;
    390 	buf2 = buf + (nbytes * layoutPtr->numDataCol);
    391 	ret = RF_PARITY_OKAY;
    392 	/*
    393          * bbufs is "bad bufs"- an array whose entries are the data
    394          * column numbers where we had miscompares. (That is, column 0
    395          * and column 1 of the array are mirror copies, and are considered
    396          * "data column 0" for this purpose).
    397          */
    398 	RF_MallocAndAdd(bbufs, layoutPtr->numParityCol * sizeof(int), (int *),
    399 	    allocList);
    400 	nbad = 0;
    401 	/*
    402          * Check data vs "parity" (mirror copy).
    403          */
    404 	for (i = 0; i < layoutPtr->numDataCol; i++) {
    405 #if RF_DEBUG_VERIFYPARITY
    406 		if (rf_verifyParityDebug) {
    407 			printf("raid%d: RAID1 parity verify %d bytes: i=%d buf1=%lx buf2=%lx buf=%lx\n",
    408 			       raidPtr->raidid, nbytes, i, (long) buf1,
    409 			       (long) buf2, (long) buf);
    410 		}
    411 #endif
    412 		ret = memcmp(buf1, buf2, nbytes);
    413 		if (ret) {
    414 #if RF_DEBUG_VERIFYPARITY
    415 			if (rf_verifyParityDebug > 1) {
    416 				for (j = 0; j < nbytes; j++) {
    417 					if (buf1[j] != buf2[j])
    418 						break;
    419 				}
    420 				printf("psid=%ld j=%d\n", (long) psID, j);
    421 				printf("buf1 %02x %02x %02x %02x %02x\n", buf1[0] & 0xff,
    422 				    buf1[1] & 0xff, buf1[2] & 0xff, buf1[3] & 0xff, buf1[4] & 0xff);
    423 				printf("buf2 %02x %02x %02x %02x %02x\n", buf2[0] & 0xff,
    424 				    buf2[1] & 0xff, buf2[2] & 0xff, buf2[3] & 0xff, buf2[4] & 0xff);
    425 			}
    426 			if (rf_verifyParityDebug) {
    427 				printf("raid%d: RAID1: found bad parity, i=%d\n", raidPtr->raidid, i);
    428 			}
    429 #endif
    430 			/*
    431 		         * Parity is bad. Keep track of which columns were bad.
    432 		         */
    433 			if (bbufs)
    434 				bbufs[nbad] = i;
    435 			nbad++;
    436 			ret = RF_PARITY_BAD;
    437 		}
    438 		buf1 += nbytes;
    439 		buf2 += nbytes;
    440 	}
    441 
    442 	if ((ret != RF_PARITY_OKAY) && correct_it) {
    443 		ret = RF_PARITY_COULD_NOT_CORRECT;
    444 #if RF_DEBUG_VERIFYPARITY
    445 		if (rf_verifyParityDebug) {
    446 			printf("raid%d: RAID1 parity verify: parity not correct\n", raidPtr->raidid);
    447 		}
    448 #endif
    449 		if (bbufs == NULL)
    450 			goto done;
    451 		/*
    452 	         * Make a DAG with one write node for each bad unit. We'll simply
    453 	         * write the contents of the data unit onto the parity unit for
    454 	         * correction. (It's possible that the mirror copy was the correct
    455 	         * copy, and that we're spooging good data by writing bad over it,
    456 	         * but there's no way we can know that.
    457 	         */
    458 		wr_dag_h = rf_MakeSimpleDAG(raidPtr, nbad, nbytes, buf,
    459 		    rf_DiskWriteFunc, rf_DiskWriteUndoFunc, "Wnp", allocList, flags,
    460 		    RF_IO_NORMAL_PRIORITY);
    461 		if (wr_dag_h == NULL)
    462 			goto done;
    463 		wrBlock = wr_dag_h->succedents[0];
    464 		/*
    465 	         * Fill in a write node for each bad compare.
    466 	         */
    467 		for (i = 0; i < nbad; i++) {
    468 			j = i + layoutPtr->numDataCol;
    469 			pda = blockNode->succedents[j]->params[0].p;
    470 			pda->bufPtr = blockNode->succedents[i]->params[1].p;
    471 			wrBlock->succedents[i]->params[0].p = pda;
    472 			wrBlock->succedents[i]->params[1].p = pda->bufPtr;
    473 			wrBlock->succedents[i]->params[2].v = psID;
    474 			wrBlock->succedents[0]->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, which_ru);
    475 		}
    476 		memset((char *) &tracerec, 0, sizeof(tracerec));
    477 		wr_dag_h->tracerec = &tracerec;
    478 #if 0
    479 		if (rf_verifyParityDebug > 1) {
    480 			printf("Parity verify write dag:\n");
    481 			rf_PrintDAGList(wr_dag_h);
    482 		}
    483 #endif
    484 		RF_LOCK_MUTEX(mcpair->mutex);
    485 		mcpair->flag = 0;
    486 		/* fire off the write DAG */
    487 		rf_DispatchDAG(wr_dag_h, (void (*) (void *)) rf_MCPairWakeupFunc,
    488 		    (void *) mcpair);
    489 		while (!mcpair->flag) {
    490 			RF_WAIT_COND(mcpair->cond, mcpair->mutex);
    491 		}
    492 		RF_UNLOCK_MUTEX(mcpair->mutex);
    493 		if (wr_dag_h->status != rf_enable) {
    494 			RF_ERRORMSG("Unable to correct RAID1 parity in VerifyParity\n");
    495 			goto done;
    496 		}
    497 		ret = RF_PARITY_CORRECTED;
    498 	}
    499 done:
    500 	/*
    501          * All done. We might've gotten here without doing part of the function,
    502          * so cleanup what we have to and return our running status.
    503          */
    504 	if (asm_h)
    505 		rf_FreeAccessStripeMap(asm_h);
    506 	if (rd_dag_h)
    507 		rf_FreeDAG(rd_dag_h);
    508 	if (wr_dag_h)
    509 		rf_FreeDAG(wr_dag_h);
    510 	if (mcpair)
    511 		rf_FreeMCPair(mcpair);
    512 	rf_FreeAllocList(allocList);
    513 #if RF_DEBUG_VERIFYPARITY
    514 	if (rf_verifyParityDebug) {
    515 		printf("raid%d: RAID1 parity verify, returning %d\n",
    516 		       raidPtr->raidid, ret);
    517 	}
    518 #endif
    519 	return (ret);
    520 }
    521 
    522 /* rbuf          - the recon buffer to submit
    523  * keep_it       - whether we can keep this buffer or we have to return it
    524  * use_committed - whether to use a committed or an available recon buffer
    525  */
    526 
    527 int
    528 rf_SubmitReconBufferRAID1(RF_ReconBuffer_t *rbuf, int keep_it,
    529 			  int use_committed)
    530 {
    531 	RF_ReconParityStripeStatus_t *pssPtr;
    532 	RF_ReconCtrl_t *reconCtrlPtr;
    533 	int     retcode, created;
    534 	RF_CallbackDesc_t *cb, *p;
    535 	RF_ReconBuffer_t *t;
    536 	RF_Raid_t *raidPtr;
    537 	caddr_t ta;
    538 
    539 	retcode = 0;
    540 	created = 0;
    541 
    542 	raidPtr = rbuf->raidPtr;
    543 	reconCtrlPtr = raidPtr->reconControl;
    544 
    545 	RF_ASSERT(rbuf);
    546 	RF_ASSERT(rbuf->col != reconCtrlPtr->fcol);
    547 
    548 #if RF_DEBUG_RECON
    549 	if (rf_reconbufferDebug) {
    550 		printf("raid%d: RAID1 reconbuffer submission c%d psid %ld ru%d (failed offset %ld)\n",
    551 		       raidPtr->raidid, rbuf->col,
    552 		       (long) rbuf->parityStripeID, rbuf->which_ru,
    553 		       (long) rbuf->failedDiskSectorOffset);
    554 	}
    555 #endif
    556 	if (rf_reconDebug) {
    557 		printf("RAID1 reconbuffer submit psid %ld buf %lx\n",
    558 		    (long) rbuf->parityStripeID, (long) rbuf->buffer);
    559 		printf("RAID1 psid %ld   %02x %02x %02x %02x %02x\n",
    560 		    (long) rbuf->parityStripeID,
    561 		    rbuf->buffer[0], rbuf->buffer[1], rbuf->buffer[2], rbuf->buffer[3],
    562 		    rbuf->buffer[4]);
    563 	}
    564 	RF_LOCK_PSS_MUTEX(raidPtr, rbuf->parityStripeID);
    565 
    566 	RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
    567 
    568 	pssPtr = rf_LookupRUStatus(raidPtr, reconCtrlPtr->pssTable,
    569 	    rbuf->parityStripeID, rbuf->which_ru, RF_PSS_NONE, &created);
    570 	RF_ASSERT(pssPtr);	/* if it didn't exist, we wouldn't have gotten
    571 				 * an rbuf for it */
    572 
    573 	/*
    574          * Since this is simple mirroring, the first submission for a stripe is also
    575          * treated as the last.
    576          */
    577 
    578 	t = NULL;
    579 	if (keep_it) {
    580 #if RF_DEBUG_RECON
    581 		if (rf_reconbufferDebug) {
    582 			printf("raid%d: RAID1 rbuf submission: keeping rbuf\n",
    583 			       raidPtr->raidid);
    584 		}
    585 #endif
    586 		t = rbuf;
    587 	} else {
    588 		if (use_committed) {
    589 #if RF_DEBUG_RECON
    590 			if (rf_reconbufferDebug) {
    591 				printf("raid%d: RAID1 rbuf submission: using committed rbuf\n", raidPtr->raidid);
    592 			}
    593 #endif
    594 			t = reconCtrlPtr->committedRbufs;
    595 			RF_ASSERT(t);
    596 			reconCtrlPtr->committedRbufs = t->next;
    597 			t->next = NULL;
    598 		} else
    599 			if (reconCtrlPtr->floatingRbufs) {
    600 #if RF_DEBUG_RECON
    601 				if (rf_reconbufferDebug) {
    602 					printf("raid%d: RAID1 rbuf submission: using floating rbuf\n", raidPtr->raidid);
    603 				}
    604 #endif
    605 				t = reconCtrlPtr->floatingRbufs;
    606 				reconCtrlPtr->floatingRbufs = t->next;
    607 				t->next = NULL;
    608 			}
    609 	}
    610 	if (t == NULL) {
    611 #if RF_DEBUG_RECON
    612 		if (rf_reconbufferDebug) {
    613 			printf("raid%d: RAID1 rbuf submission: waiting for rbuf\n", raidPtr->raidid);
    614 		}
    615 #endif
    616 		RF_ASSERT((keep_it == 0) && (use_committed == 0));
    617 		raidPtr->procsInBufWait++;
    618 		if ((raidPtr->procsInBufWait == (raidPtr->numCol - 1))
    619 		    && (raidPtr->numFullReconBuffers == 0)) {
    620 			/* ruh-ro */
    621 			RF_ERRORMSG("Buffer wait deadlock\n");
    622 			rf_PrintPSStatusTable(raidPtr);
    623 			RF_PANIC();
    624 		}
    625 		pssPtr->flags |= RF_PSS_BUFFERWAIT;
    626 		cb = rf_AllocCallbackDesc();
    627 		cb->col = rbuf->col;
    628 		cb->callbackArg.v = rbuf->parityStripeID;
    629 		cb->callbackArg2.v = rbuf->which_ru;
    630 		cb->next = NULL;
    631 		if (reconCtrlPtr->bufferWaitList == NULL) {
    632 			/* we are the wait list- lucky us */
    633 			reconCtrlPtr->bufferWaitList = cb;
    634 		} else {
    635 			/* append to wait list */
    636 			for (p = reconCtrlPtr->bufferWaitList; p->next; p = p->next);
    637 			p->next = cb;
    638 		}
    639 		retcode = 1;
    640 		goto out;
    641 	}
    642 	if (t != rbuf) {
    643 		t->col = reconCtrlPtr->fcol;
    644 		t->parityStripeID = rbuf->parityStripeID;
    645 		t->which_ru = rbuf->which_ru;
    646 		t->failedDiskSectorOffset = rbuf->failedDiskSectorOffset;
    647 		t->spCol = rbuf->spCol;
    648 		t->spOffset = rbuf->spOffset;
    649 		/* Swap buffers. DANCE! */
    650 		ta = t->buffer;
    651 		t->buffer = rbuf->buffer;
    652 		rbuf->buffer = ta;
    653 	}
    654 	/*
    655          * Use the rbuf we've been given as the target.
    656          */
    657 	RF_ASSERT(pssPtr->rbuf == NULL);
    658 	pssPtr->rbuf = t;
    659 
    660 	t->count = 1;
    661 	/*
    662          * Below, we use 1 for numDataCol (which is equal to the count in the
    663          * previous line), so we'll always be done.
    664          */
    665 	rf_CheckForFullRbuf(raidPtr, reconCtrlPtr, pssPtr, 1);
    666 
    667 out:
    668 	RF_UNLOCK_PSS_MUTEX(raidPtr, rbuf->parityStripeID);
    669 	RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
    670 #if RF_DEBUG_RECON
    671 	if (rf_reconbufferDebug) {
    672 		printf("raid%d: RAID1 rbuf submission: returning %d\n",
    673 		       raidPtr->raidid, retcode);
    674 	}
    675 #endif
    676 	return (retcode);
    677 }
    678