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