xref: /netbsd-src/sys/dev/raidframe/rf_map.c (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*	$NetBSD: rf_map.c,v 1.28 2003/12/30 22:12:10 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@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  * map.c -- main code for mapping RAID addresses to physical disk addresses
32  *
33  **************************************************************************/
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: rf_map.c,v 1.28 2003/12/30 22:12:10 oster Exp $");
37 
38 #include <dev/raidframe/raidframevar.h>
39 
40 #include "rf_threadstuff.h"
41 #include "rf_raid.h"
42 #include "rf_general.h"
43 #include "rf_map.h"
44 #include "rf_shutdown.h"
45 
46 static void rf_FreePDAList(RF_PhysDiskAddr_t *pda_list);
47 static void rf_FreeASMList(RF_AccessStripeMap_t *asm_list);
48 
49 /***************************************************************************
50  *
51  * MapAccess -- main 1st order mapping routine.  Maps an access in the
52  * RAID address space to the corresponding set of physical disk
53  * addresses.  The result is returned as a list of AccessStripeMap
54  * structures, one per stripe accessed.  Each ASM structure contains a
55  * pointer to a list of PhysDiskAddr structures, which describe the
56  * physical locations touched by the user access.  Note that this
57  * routine returns only static mapping information, i.e. the list of
58  * physical addresses returned does not necessarily identify the set
59  * of physical locations that will actually be read or written.  The
60  * routine also maps the parity.  The physical disk location returned
61  * always indicates the entire parity unit, even when only a subset of
62  * it is being accessed.  This is because an access that is not stripe
63  * unit aligned but that spans a stripe unit boundary may require
64  * access two distinct portions of the parity unit, and we can't yet
65  * tell which portion(s) we'll actually need.  We leave it up to the
66  * algorithm selection code to decide what subset of the parity unit
67  * to access.  Note that addresses in the RAID address space must
68  * always be maintained as longs, instead of ints.
69  *
70  * This routine returns NULL if numBlocks is 0
71  *
72  * raidAddress - starting address in RAID address space
73  * numBlocks   - number of blocks in RAID address space to access
74  * buffer      - buffer to supply/recieve data
75  * remap       - 1 => remap address to spare space
76  ***************************************************************************/
77 
78 RF_AccessStripeMapHeader_t *
79 rf_MapAccess(RF_Raid_t *raidPtr, RF_RaidAddr_t raidAddress,
80 	     RF_SectorCount_t numBlocks, caddr_t buffer, int remap)
81 {
82 	RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout);
83 	RF_AccessStripeMapHeader_t *asm_hdr = NULL;
84 	RF_AccessStripeMap_t *asm_list = NULL, *asm_p = NULL;
85 	int     faultsTolerated = layoutPtr->map->faultsTolerated;
86 	/* we'll change raidAddress along the way */
87 	RF_RaidAddr_t startAddress = raidAddress;
88 	RF_RaidAddr_t endAddress = raidAddress + numBlocks;
89 	RF_RaidDisk_t *disks = raidPtr->Disks;
90 
91 	RF_PhysDiskAddr_t *pda_p, *pda_q;
92 	RF_StripeCount_t numStripes = 0;
93 	RF_RaidAddr_t stripeRealEndAddress, stripeEndAddress,
94 		nextStripeUnitAddress;
95 	RF_RaidAddr_t startAddrWithinStripe, lastRaidAddr;
96 	RF_StripeCount_t totStripes;
97 	RF_StripeNum_t stripeID, lastSID, SUID, lastSUID;
98 	RF_AccessStripeMap_t *asmList, *t_asm;
99 	RF_PhysDiskAddr_t *pdaList, *t_pda;
100 
101 	/* allocate all the ASMs and PDAs up front */
102 	lastRaidAddr = raidAddress + numBlocks - 1;
103 	stripeID = rf_RaidAddressToStripeID(layoutPtr, raidAddress);
104 	lastSID = rf_RaidAddressToStripeID(layoutPtr, lastRaidAddr);
105 	totStripes = lastSID - stripeID + 1;
106 	SUID = rf_RaidAddressToStripeUnitID(layoutPtr, raidAddress);
107 	lastSUID = rf_RaidAddressToStripeUnitID(layoutPtr, lastRaidAddr);
108 
109 	asmList = rf_AllocASMList(totStripes);
110 
111 	/* may also need pda(s) per stripe for parity */
112 	pdaList = rf_AllocPDAList(lastSUID - SUID + 1 +
113 				  faultsTolerated * totStripes);
114 
115 
116 	if (raidAddress + numBlocks > raidPtr->totalSectors) {
117 		RF_ERRORMSG1("Unable to map access because offset (%d) was invalid\n",
118 		    (int) raidAddress);
119 		return (NULL);
120 	}
121 #if RF_DEBUG_MAP
122 	if (rf_mapDebug)
123 		rf_PrintRaidAddressInfo(raidPtr, raidAddress, numBlocks);
124 #endif
125 	for (; raidAddress < endAddress;) {
126 		/* make the next stripe structure */
127 		RF_ASSERT(asmList);
128 		t_asm = asmList;
129 		asmList = asmList->next;
130 		memset((char *) t_asm, 0, sizeof(RF_AccessStripeMap_t));
131 		if (!asm_p)
132 			asm_list = asm_p = t_asm;
133 		else {
134 			asm_p->next = t_asm;
135 			asm_p = asm_p->next;
136 		}
137 		numStripes++;
138 
139 		/* map SUs from current location to the end of the stripe */
140 		asm_p->stripeID =	/* rf_RaidAddressToStripeID(layoutPtr,
141 		        raidAddress) */ stripeID++;
142 		stripeRealEndAddress = rf_RaidAddressOfNextStripeBoundary(layoutPtr, raidAddress);
143 		stripeEndAddress = RF_MIN(endAddress, stripeRealEndAddress);
144 		asm_p->raidAddress = raidAddress;
145 		asm_p->endRaidAddress = stripeEndAddress;
146 
147 		/* map each stripe unit in the stripe */
148 		pda_p = NULL;
149 
150 		/* Raid addr of start of portion of access that is
151                    within this stripe */
152 		startAddrWithinStripe = raidAddress;
153 
154 		for (; raidAddress < stripeEndAddress;) {
155 			RF_ASSERT(pdaList);
156 			t_pda = pdaList;
157 			pdaList = pdaList->next;
158 			memset((char *) t_pda, 0, sizeof(RF_PhysDiskAddr_t));
159 			if (!pda_p)
160 				asm_p->physInfo = pda_p = t_pda;
161 			else {
162 				pda_p->next = t_pda;
163 				pda_p = pda_p->next;
164 			}
165 
166 			pda_p->type = RF_PDA_TYPE_DATA;
167 			(layoutPtr->map->MapSector) (raidPtr, raidAddress,
168 						     &(pda_p->col),
169 						     &(pda_p->startSector),
170 						     remap);
171 
172 			/* mark any failures we find.  failedPDA is
173 			 * don't-care if there is more than one
174 			 * failure */
175 
176 			/* the RAID address corresponding to this
177                            physical diskaddress */
178 			pda_p->raidAddress = raidAddress;
179 			nextStripeUnitAddress = rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr, raidAddress);
180 			pda_p->numSector = RF_MIN(endAddress, nextStripeUnitAddress) - raidAddress;
181 			RF_ASSERT(pda_p->numSector != 0);
182 			rf_ASMCheckStatus(raidPtr, pda_p, asm_p, disks, 0);
183 			pda_p->bufPtr = buffer + rf_RaidAddressToByte(raidPtr, (raidAddress - startAddress));
184 			asm_p->totalSectorsAccessed += pda_p->numSector;
185 			asm_p->numStripeUnitsAccessed++;
186 
187 			raidAddress = RF_MIN(endAddress, nextStripeUnitAddress);
188 		}
189 
190 		/* Map the parity. At this stage, the startSector and
191 		 * numSector fields for the parity unit are always set
192 		 * to indicate the entire parity unit. We may modify
193 		 * this after mapping the data portion. */
194 		switch (faultsTolerated) {
195 		case 0:
196 			break;
197 		case 1:	/* single fault tolerant */
198 			RF_ASSERT(pdaList);
199 			t_pda = pdaList;
200 			pdaList = pdaList->next;
201 			memset((char *) t_pda, 0, sizeof(RF_PhysDiskAddr_t));
202 			pda_p = asm_p->parityInfo = t_pda;
203 			pda_p->type = RF_PDA_TYPE_PARITY;
204 			(layoutPtr->map->MapParity) (raidPtr, rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe),
205 			    &(pda_p->col), &(pda_p->startSector), remap);
206 			pda_p->numSector = layoutPtr->sectorsPerStripeUnit;
207 			/* raidAddr may be needed to find unit to redirect to */
208 			pda_p->raidAddress = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe);
209 			rf_ASMCheckStatus(raidPtr, pda_p, asm_p, disks, 1);
210 			rf_ASMParityAdjust(asm_p->parityInfo, startAddrWithinStripe, endAddress, layoutPtr, asm_p);
211 
212 			break;
213 		case 2:	/* two fault tolerant */
214 			RF_ASSERT(pdaList && pdaList->next);
215 			t_pda = pdaList;
216 			pdaList = pdaList->next;
217 			memset((char *) t_pda, 0, sizeof(RF_PhysDiskAddr_t));
218 			pda_p = asm_p->parityInfo = t_pda;
219 			pda_p->type = RF_PDA_TYPE_PARITY;
220 			t_pda = pdaList;
221 			pdaList = pdaList->next;
222 			memset((char *) t_pda, 0, sizeof(RF_PhysDiskAddr_t));
223 			pda_q = asm_p->qInfo = t_pda;
224 			pda_q->type = RF_PDA_TYPE_Q;
225 			(layoutPtr->map->MapParity) (raidPtr, rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe),
226 			    &(pda_p->col), &(pda_p->startSector), remap);
227 			(layoutPtr->map->MapQ) (raidPtr, rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe),
228 			    &(pda_q->col), &(pda_q->startSector), remap);
229 			pda_q->numSector = pda_p->numSector = layoutPtr->sectorsPerStripeUnit;
230 			/* raidAddr may be needed to find unit to redirect to */
231 			pda_p->raidAddress = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe);
232 			pda_q->raidAddress = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe);
233 			/* failure mode stuff */
234 			rf_ASMCheckStatus(raidPtr, pda_p, asm_p, disks, 1);
235 			rf_ASMCheckStatus(raidPtr, pda_q, asm_p, disks, 1);
236 			rf_ASMParityAdjust(asm_p->parityInfo, startAddrWithinStripe, endAddress, layoutPtr, asm_p);
237 			rf_ASMParityAdjust(asm_p->qInfo, startAddrWithinStripe, endAddress, layoutPtr, asm_p);
238 			break;
239 		}
240 	}
241 	RF_ASSERT(asmList == NULL && pdaList == NULL);
242 	/* make the header structure */
243 	asm_hdr = rf_AllocAccessStripeMapHeader();
244 	RF_ASSERT(numStripes == totStripes);
245 	asm_hdr->numStripes = numStripes;
246 	asm_hdr->stripeMap = asm_list;
247 
248 #if RF_DEBUG_MAP
249 	if (rf_mapDebug)
250 		rf_PrintAccessStripeMap(asm_hdr);
251 #endif
252 	return (asm_hdr);
253 }
254 
255 /***************************************************************************
256  * This routine walks through an ASM list and marks the PDAs that have
257  * failed.  It's called only when a disk failure causes an in-flight
258  * DAG to fail.  The parity may consist of two components, but we want
259  * to use only one failedPDA pointer.  Thus we set failedPDA to point
260  * to the first parity component, and rely on the rest of the code to
261  * do the right thing with this.
262  ***************************************************************************/
263 
264 void
265 rf_MarkFailuresInASMList(RF_Raid_t *raidPtr,
266 			 RF_AccessStripeMapHeader_t *asm_h)
267 {
268 	RF_RaidDisk_t *disks = raidPtr->Disks;
269 	RF_AccessStripeMap_t *asmap;
270 	RF_PhysDiskAddr_t *pda;
271 
272 	for (asmap = asm_h->stripeMap; asmap; asmap = asmap->next) {
273 		asmap->numDataFailed = 0;
274 		asmap->numParityFailed = 0;
275 		asmap->numQFailed = 0;
276 		asmap->numFailedPDAs = 0;
277 		memset((char *) asmap->failedPDAs, 0,
278 		    RF_MAX_FAILED_PDA * sizeof(RF_PhysDiskAddr_t *));
279 		for (pda = asmap->physInfo; pda; pda = pda->next) {
280 			if (RF_DEAD_DISK(disks[pda->col].status)) {
281 				asmap->numDataFailed++;
282 				asmap->failedPDAs[asmap->numFailedPDAs] = pda;
283 				asmap->numFailedPDAs++;
284 			}
285 		}
286 		pda = asmap->parityInfo;
287 		if (pda && RF_DEAD_DISK(disks[pda->col].status)) {
288 			asmap->numParityFailed++;
289 			asmap->failedPDAs[asmap->numFailedPDAs] = pda;
290 			asmap->numFailedPDAs++;
291 		}
292 		pda = asmap->qInfo;
293 		if (pda && RF_DEAD_DISK(disks[pda->col].status)) {
294 			asmap->numQFailed++;
295 			asmap->failedPDAs[asmap->numFailedPDAs] = pda;
296 			asmap->numFailedPDAs++;
297 		}
298 	}
299 }
300 
301 /***************************************************************************
302  *
303  * routines to allocate and free list elements.  All allocation
304  * routines zero the structure before returning it.
305  *
306  * FreePhysDiskAddr is static.  It should never be called directly,
307  * because FreeAccessStripeMap takes care of freeing the PhysDiskAddr
308  * list.
309  *
310  ***************************************************************************/
311 
312 static struct pool rf_asmhdr_pool;
313 #define RF_MAX_FREE_ASMHDR 128
314 #define RF_ASMHDR_INC       16
315 #define RF_ASMHDR_INITIAL   32
316 
317 static struct pool rf_asm_pool;
318 #define RF_MAX_FREE_ASM 192
319 #define RF_ASM_INC       24
320 #define RF_ASM_INITIAL   64
321 
322 static struct pool rf_pda_pool;   /* may need to be visible for
323 				     rf_dagdegrd.c and rf_dagdegwr.c,
324 				     if they can be convinced to free
325 				     the space easily */
326 #define RF_MAX_FREE_PDA 192
327 #define RF_PDA_INC       24
328 #define RF_PDA_INITIAL   64
329 
330 /* called at shutdown time.  So far, all that is necessary is to
331    release all the free lists */
332 static void rf_ShutdownMapModule(void *);
333 static void
334 rf_ShutdownMapModule(void *ignored)
335 {
336 	pool_destroy(&rf_asmhdr_pool);
337 	pool_destroy(&rf_asm_pool);
338 	pool_destroy(&rf_pda_pool);
339 }
340 
341 int
342 rf_ConfigureMapModule(RF_ShutdownList_t **listp)
343 {
344 	int     rc;
345 
346 	pool_init(&rf_asmhdr_pool, sizeof(RF_AccessStripeMapHeader_t),
347 		  0, 0, 0, "rf_asmhdr_pl", NULL);
348 	pool_sethiwat(&rf_asmhdr_pool, RF_MAX_FREE_ASMHDR);
349 	pool_prime(&rf_asmhdr_pool, RF_ASMHDR_INITIAL);
350 
351 	pool_init(&rf_asm_pool, sizeof(RF_AccessStripeMap_t),
352 		  0, 0, 0, "rf_asm_pl", NULL);
353 	pool_sethiwat(&rf_asm_pool, RF_MAX_FREE_ASM);
354 	pool_prime(&rf_asm_pool, RF_ASM_INITIAL);
355 
356 	pool_init(&rf_pda_pool, sizeof(RF_PhysDiskAddr_t),
357 		  0, 0, 0, "rf_pda_pl", NULL);
358 	pool_sethiwat(&rf_pda_pool, RF_MAX_FREE_PDA);
359 	pool_prime(&rf_pda_pool, RF_PDA_INITIAL);
360 
361 	rc = rf_ShutdownCreate(listp, rf_ShutdownMapModule, NULL);
362 	if (rc) {
363 		rf_print_unable_to_add_shutdown(__FILE__, __LINE__, rc);
364 		rf_ShutdownMapModule(NULL);
365 		return (rc);
366 	}
367 	return (0);
368 }
369 
370 RF_AccessStripeMapHeader_t *
371 rf_AllocAccessStripeMapHeader()
372 {
373 	RF_AccessStripeMapHeader_t *p;
374 
375 	p = pool_get(&rf_asmhdr_pool, PR_WAITOK);
376 	memset((char *) p, 0, sizeof(RF_AccessStripeMapHeader_t));
377 
378 	return (p);
379 }
380 
381 void
382 rf_FreeAccessStripeMapHeader(RF_AccessStripeMapHeader_t *p)
383 {
384 	pool_put(&rf_asmhdr_pool, p);
385 }
386 
387 RF_PhysDiskAddr_t *
388 rf_AllocPhysDiskAddr()
389 {
390 	RF_PhysDiskAddr_t *p;
391 
392 	p = pool_get(&rf_pda_pool, PR_WAITOK);
393 	memset((char *) p, 0, sizeof(RF_PhysDiskAddr_t));
394 
395 	return (p);
396 }
397 /* allocates a list of PDAs, locking the free list only once when we
398  * have to call calloc, we do it one component at a time to simplify
399  * the process of freeing the list at program shutdown.  This should
400  * not be much of a performance hit, because it should be very
401  * infrequently executed.  */
402 RF_PhysDiskAddr_t *
403 rf_AllocPDAList(int count)
404 {
405 	RF_PhysDiskAddr_t *p, *prev;
406 	int i;
407 
408 	p = NULL;
409 	prev = NULL;
410 	for (i = 0; i < count; i++) {
411 		p = pool_get(&rf_pda_pool, PR_WAITOK);
412 		p->next = prev;
413 		prev = p;
414 	}
415 
416 	return (p);
417 }
418 
419 #if RF_INCLUDE_PARITYLOGGING > 0
420 void
421 rf_FreePhysDiskAddr(RF_PhysDiskAddr_t *p)
422 {
423 	pool_put(&rf_pda_pool, p);
424 }
425 #endif
426 
427 static void
428 rf_FreePDAList(RF_PhysDiskAddr_t *pda_list)
429 {
430 	RF_PhysDiskAddr_t *p, *tmp;
431 
432 	p=pda_list;
433 	while (p) {
434 		tmp = p->next;
435 		pool_put(&rf_pda_pool, p);
436 		p = tmp;
437 	}
438 }
439 
440 /* this is essentially identical to AllocPDAList.  I should combine
441  * the two.  when we have to call calloc, we do it one component at a
442  * time to simplify the process of freeing the list at program
443  * shutdown.  This should not be much of a performance hit, because it
444  * should be very infrequently executed.  */
445 RF_AccessStripeMap_t *
446 rf_AllocASMList(int count)
447 {
448 	RF_AccessStripeMap_t *p, *prev;
449 	int i;
450 
451 	p = NULL;
452 	prev = NULL;
453 	for (i = 0; i < count; i++) {
454 		p = pool_get(&rf_asm_pool, PR_WAITOK);
455 		p->next = prev;
456 		prev = p;
457 	}
458 	return (p);
459 }
460 
461 static void
462 rf_FreeASMList(RF_AccessStripeMap_t *asm_list)
463 {
464 	RF_AccessStripeMap_t *p, *tmp;
465 
466 	p=asm_list;
467 	while (p) {
468 		tmp = p->next;
469 		pool_put(&rf_asm_pool, p);
470 		p = tmp;
471 	}
472 }
473 
474 void
475 rf_FreeAccessStripeMap(RF_AccessStripeMapHeader_t *hdr)
476 {
477 	RF_AccessStripeMap_t *p;
478 	RF_PhysDiskAddr_t *pdp, *trailer, *pdaList = NULL, *pdaEnd = NULL;
479 	int     count = 0, t, asm_count = 0;
480 
481 	for (p = hdr->stripeMap; p; p = p->next) {
482 
483 		/* link the 3 pda lists into the accumulating pda list */
484 
485 		if (!pdaList)
486 			pdaList = p->qInfo;
487 		else
488 			pdaEnd->next = p->qInfo;
489 		for (trailer = NULL, pdp = p->qInfo; pdp;) {
490 			trailer = pdp;
491 			pdp = pdp->next;
492 			count++;
493 		}
494 		if (trailer)
495 			pdaEnd = trailer;
496 
497 		if (!pdaList)
498 			pdaList = p->parityInfo;
499 		else
500 			pdaEnd->next = p->parityInfo;
501 		for (trailer = NULL, pdp = p->parityInfo; pdp;) {
502 			trailer = pdp;
503 			pdp = pdp->next;
504 			count++;
505 		}
506 		if (trailer)
507 			pdaEnd = trailer;
508 
509 		if (!pdaList)
510 			pdaList = p->physInfo;
511 		else
512 			pdaEnd->next = p->physInfo;
513 		for (trailer = NULL, pdp = p->physInfo; pdp;) {
514 			trailer = pdp;
515 			pdp = pdp->next;
516 			count++;
517 		}
518 		if (trailer)
519 			pdaEnd = trailer;
520 
521 		asm_count++;
522 	}
523 
524 	/* debug only */
525 	for (t = 0, pdp = pdaList; pdp; pdp = pdp->next)
526 		t++;
527 	RF_ASSERT(t == count);
528 
529 	if (pdaList)
530 		rf_FreePDAList(pdaList);
531 	rf_FreeASMList(hdr->stripeMap);
532 	rf_FreeAccessStripeMapHeader(hdr);
533 }
534 /* We can't use the large write optimization if there are any failures
535  * in the stripe.  In the declustered layout, there is no way to
536  * immediately determine what disks constitute a stripe, so we
537  * actually have to hunt through the stripe looking for failures.  The
538  * reason we map the parity instead of just using asm->parityInfo->col
539  * is because the latter may have been already redirected to a spare
540  * drive, which would mess up the computation of the stripe offset.
541  *
542  * ASSUMES AT MOST ONE FAILURE IN THE STRIPE.  */
543 int
544 rf_CheckStripeForFailures(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap)
545 {
546 	RF_RowCol_t tcol, pcol, *diskids, i;
547 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
548 	RF_StripeCount_t stripeOffset;
549 	int     numFailures;
550 	RF_RaidAddr_t sosAddr;
551 	RF_SectorNum_t diskOffset, poffset;
552 
553 	/* quick out in the fault-free case.  */
554 	RF_LOCK_MUTEX(raidPtr->mutex);
555 	numFailures = raidPtr->numFailures;
556 	RF_UNLOCK_MUTEX(raidPtr->mutex);
557 	if (numFailures == 0)
558 		return (0);
559 
560 	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr,
561 						     asmap->raidAddress);
562 	(layoutPtr->map->IdentifyStripe) (raidPtr, asmap->raidAddress,
563 					  &diskids);
564 	(layoutPtr->map->MapParity) (raidPtr, asmap->raidAddress,
565 				     &pcol, &poffset, 0);	/* get pcol */
566 
567 	/* this need not be true if we've redirected the access to a
568 	 * spare in another row RF_ASSERT(row == testrow); */
569 	stripeOffset = 0;
570 	for (i = 0; i < layoutPtr->numDataCol + layoutPtr->numParityCol; i++) {
571 		if (diskids[i] != pcol) {
572 			if (RF_DEAD_DISK(raidPtr->Disks[diskids[i]].status)) {
573 				if (raidPtr->status != rf_rs_reconstructing)
574 					return (1);
575 				RF_ASSERT(raidPtr->reconControl->fcol == diskids[i]);
576 				layoutPtr->map->MapSector(raidPtr,
577 				    sosAddr + stripeOffset * layoutPtr->sectorsPerStripeUnit,
578 				    &tcol, &diskOffset, 0);
579 				RF_ASSERT(tcol == diskids[i]);
580 				if (!rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, diskOffset))
581 					return (1);
582 				asmap->flags |= RF_ASM_REDIR_LARGE_WRITE;
583 				return (0);
584 			}
585 			stripeOffset++;
586 		}
587 	}
588 	return (0);
589 }
590 #if (RF_INCLUDE_DECL_PQ > 0) || (RF_INCLUDE_RAID6 > 0) || (RF_INCLUDE_EVENODD >0)
591 /*
592    return the number of failed data units in the stripe.
593 */
594 
595 int
596 rf_NumFailedDataUnitsInStripe(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap)
597 {
598 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
599 	RF_RowCol_t tcol, i;
600 	RF_SectorNum_t diskOffset;
601 	RF_RaidAddr_t sosAddr;
602 	int     numFailures;
603 
604 	/* quick out in the fault-free case.  */
605 	RF_LOCK_MUTEX(raidPtr->mutex);
606 	numFailures = raidPtr->numFailures;
607 	RF_UNLOCK_MUTEX(raidPtr->mutex);
608 	if (numFailures == 0)
609 		return (0);
610 	numFailures = 0;
611 
612 	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr,
613 						     asmap->raidAddress);
614 	for (i = 0; i < layoutPtr->numDataCol; i++) {
615 		(layoutPtr->map->MapSector) (raidPtr, sosAddr + i * layoutPtr->sectorsPerStripeUnit,
616 		    &trow, &tcol, &diskOffset, 0);
617 		if (RF_DEAD_DISK(raidPtr->Disks[tcol].status))
618 			numFailures++;
619 	}
620 
621 	return numFailures;
622 }
623 #endif
624 
625 /****************************************************************************
626  *
627  * debug routines
628  *
629  ***************************************************************************/
630 #if RF_DEBUG_MAP
631 void
632 rf_PrintAccessStripeMap(RF_AccessStripeMapHeader_t *asm_h)
633 {
634 	rf_PrintFullAccessStripeMap(asm_h, 0);
635 }
636 #endif
637 
638 /* prbuf - flag to print buffer pointers */
639 void
640 rf_PrintFullAccessStripeMap(RF_AccessStripeMapHeader_t *asm_h, int prbuf)
641 {
642 	int     i;
643 	RF_AccessStripeMap_t *asmap = asm_h->stripeMap;
644 	RF_PhysDiskAddr_t *p;
645 	printf("%d stripes total\n", (int) asm_h->numStripes);
646 	for (; asmap; asmap = asmap->next) {
647 		/* printf("Num failures: %d\n",asmap->numDataFailed); */
648 		/* printf("Num sectors:
649 		 * %d\n",(int)asmap->totalSectorsAccessed); */
650 		printf("Stripe %d (%d sectors), failures: %d data, %d parity: ",
651 		    (int) asmap->stripeID,
652 		    (int) asmap->totalSectorsAccessed,
653 		    (int) asmap->numDataFailed,
654 		    (int) asmap->numParityFailed);
655 		if (asmap->parityInfo) {
656 			printf("Parity [c%d s%d-%d", asmap->parityInfo->col,
657 			    (int) asmap->parityInfo->startSector,
658 			    (int) (asmap->parityInfo->startSector +
659 				asmap->parityInfo->numSector - 1));
660 			if (prbuf)
661 				printf(" b0x%lx", (unsigned long) asmap->parityInfo->bufPtr);
662 			if (asmap->parityInfo->next) {
663 				printf(", c%d s%d-%d", asmap->parityInfo->next->col,
664 				    (int) asmap->parityInfo->next->startSector,
665 				    (int) (asmap->parityInfo->next->startSector +
666 					asmap->parityInfo->next->numSector - 1));
667 				if (prbuf)
668 					printf(" b0x%lx", (unsigned long) asmap->parityInfo->next->bufPtr);
669 				RF_ASSERT(asmap->parityInfo->next->next == NULL);
670 			}
671 			printf("]\n\t");
672 		}
673 		for (i = 0, p = asmap->physInfo; p; p = p->next, i++) {
674 			printf("SU c%d s%d-%d ", p->col, (int) p->startSector,
675 			    (int) (p->startSector + p->numSector - 1));
676 			if (prbuf)
677 				printf("b0x%lx ", (unsigned long) p->bufPtr);
678 			if (i && !(i & 1))
679 				printf("\n\t");
680 		}
681 		printf("\n");
682 		p = asm_h->stripeMap->failedPDAs[0];
683 		if (asm_h->stripeMap->numDataFailed + asm_h->stripeMap->numParityFailed > 1)
684 			printf("[multiple failures]\n");
685 		else
686 			if (asm_h->stripeMap->numDataFailed + asm_h->stripeMap->numParityFailed > 0)
687 				printf("\t[Failed PDA: c%d s%d-%d]\n", p->col,
688 				    (int) p->startSector, (int) (p->startSector + p->numSector - 1));
689 	}
690 }
691 
692 #if RF_MAP_DEBUG
693 void
694 rf_PrintRaidAddressInfo(RF_Raid_t *raidPtr, RF_RaidAddr_t raidAddr,
695 			RF_SectorCount_t numBlocks)
696 {
697 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
698 	RF_RaidAddr_t ra, sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, raidAddr);
699 
700 	printf("Raid addrs of SU boundaries from start of stripe to end of access:\n\t");
701 	for (ra = sosAddr; ra <= raidAddr + numBlocks; ra += layoutPtr->sectorsPerStripeUnit) {
702 		printf("%d (0x%x), ", (int) ra, (int) ra);
703 	}
704 	printf("\n");
705 	printf("Offset into stripe unit: %d (0x%x)\n",
706 	    (int) (raidAddr % layoutPtr->sectorsPerStripeUnit),
707 	    (int) (raidAddr % layoutPtr->sectorsPerStripeUnit));
708 }
709 #endif
710 /* given a parity descriptor and the starting address within a stripe,
711  * range restrict the parity descriptor to touch only the correct
712  * stuff.  */
713 void
714 rf_ASMParityAdjust(RF_PhysDiskAddr_t *toAdjust,
715 		   RF_StripeNum_t startAddrWithinStripe,
716 		   RF_SectorNum_t endAddress,
717 		   RF_RaidLayout_t *layoutPtr,
718 		   RF_AccessStripeMap_t *asm_p)
719 {
720 	RF_PhysDiskAddr_t *new_pda;
721 
722 	/* when we're accessing only a portion of one stripe unit, we
723 	 * want the parity descriptor to identify only the chunk of
724 	 * parity associated with the data.  When the access spans
725 	 * exactly one stripe unit boundary and is less than a stripe
726 	 * unit in size, it uses two disjoint regions of the parity
727 	 * unit.  When an access spans more than one stripe unit
728 	 * boundary, it uses all of the parity unit.
729 	 *
730 	 * To better handle the case where stripe units are small, we
731 	 * may eventually want to change the 2nd case so that if the
732 	 * SU size is below some threshold, we just read/write the
733 	 * whole thing instead of breaking it up into two accesses. */
734 	if (asm_p->numStripeUnitsAccessed == 1) {
735 		int     x = (startAddrWithinStripe % layoutPtr->sectorsPerStripeUnit);
736 		toAdjust->startSector += x;
737 		toAdjust->raidAddress += x;
738 		toAdjust->numSector = asm_p->physInfo->numSector;
739 		RF_ASSERT(toAdjust->numSector != 0);
740 	} else
741 		if (asm_p->numStripeUnitsAccessed == 2 && asm_p->totalSectorsAccessed < layoutPtr->sectorsPerStripeUnit) {
742 			int     x = (startAddrWithinStripe % layoutPtr->sectorsPerStripeUnit);
743 
744 			/* create a second pda and copy the parity map info
745 			 * into it */
746 			RF_ASSERT(toAdjust->next == NULL);
747 			new_pda = toAdjust->next = rf_AllocPhysDiskAddr();
748 			*new_pda = *toAdjust;	/* structure assignment */
749 			new_pda->next = NULL;
750 
751 			/* adjust the start sector & number of blocks for the
752 			 * first parity pda */
753 			toAdjust->startSector += x;
754 			toAdjust->raidAddress += x;
755 			toAdjust->numSector = rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr, startAddrWithinStripe) - startAddrWithinStripe;
756 			RF_ASSERT(toAdjust->numSector != 0);
757 
758 			/* adjust the second pda */
759 			new_pda->numSector = endAddress - rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, endAddress);
760 			/* new_pda->raidAddress =
761 			 * rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr,
762 			 * toAdjust->raidAddress); */
763 			RF_ASSERT(new_pda->numSector != 0);
764 		}
765 }
766 
767 /* Check if a disk has been spared or failed. If spared, redirect the
768  * I/O.  If it has been failed, record it in the asm pointer.  Fourth
769  * arg is whether data or parity.  */
770 void
771 rf_ASMCheckStatus(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *pda_p,
772 		  RF_AccessStripeMap_t *asm_p, RF_RaidDisk_t *disks,
773 		  int parity)
774 {
775 	RF_DiskStatus_t dstatus;
776 	RF_RowCol_t fcol;
777 
778 	dstatus = disks[pda_p->col].status;
779 
780 	if (dstatus == rf_ds_spared) {
781 		/* if the disk has been spared, redirect access to the spare */
782 		fcol = pda_p->col;
783 		pda_p->col = disks[fcol].spareCol;
784 	} else
785 		if (dstatus == rf_ds_dist_spared) {
786 			/* ditto if disk has been spared to dist spare space */
787 #if RF_DEBUG_MAP
788 			RF_RowCol_t oc = pda_p->col;
789 			RF_SectorNum_t oo = pda_p->startSector;
790 #endif
791 			if (pda_p->type == RF_PDA_TYPE_DATA)
792 				raidPtr->Layout.map->MapSector(raidPtr, pda_p->raidAddress, &pda_p->col, &pda_p->startSector, RF_REMAP);
793 			else
794 				raidPtr->Layout.map->MapParity(raidPtr, pda_p->raidAddress, &pda_p->col, &pda_p->startSector, RF_REMAP);
795 
796 #if RF_DEBUG_MAP
797 			if (rf_mapDebug) {
798 				printf("Redirected c %d o %d -> c %d o %d\n", oc, (int) oo,
799 				    pda_p->col, (int) pda_p->startSector);
800 			}
801 #endif
802 		} else
803 			if (RF_DEAD_DISK(dstatus)) {
804 				/* if the disk is inaccessible, mark the
805 				 * failure */
806 				if (parity)
807 					asm_p->numParityFailed++;
808 				else {
809 					asm_p->numDataFailed++;
810 				}
811 				asm_p->failedPDAs[asm_p->numFailedPDAs] = pda_p;
812 				asm_p->numFailedPDAs++;
813 #if 0
814 				switch (asm_p->numParityFailed + asm_p->numDataFailed) {
815 				case 1:
816 					asm_p->failedPDAs[0] = pda_p;
817 					break;
818 				case 2:
819 					asm_p->failedPDAs[1] = pda_p;
820 				default:
821 					break;
822 				}
823 #endif
824 			}
825 	/* the redirected access should never span a stripe unit boundary */
826 	RF_ASSERT(rf_RaidAddressToStripeUnitID(&raidPtr->Layout, pda_p->raidAddress) ==
827 	    rf_RaidAddressToStripeUnitID(&raidPtr->Layout, pda_p->raidAddress + pda_p->numSector - 1));
828 	RF_ASSERT(pda_p->col != -1);
829 }
830