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