Home | History | Annotate | Line # | Download | only in raidframe
rf_map.c revision 1.27
      1 /*	$NetBSD: rf_map.c,v 1.27 2003/12/30 22:11:14 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.27 2003/12/30 22:11:14 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, *pt = NULL;
    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 		pt = p;
    522 		asm_count++;
    523 	}
    524 
    525 	/* debug only */
    526 	for (t = 0, pdp = pdaList; pdp; pdp = pdp->next)
    527 		t++;
    528 	RF_ASSERT(t == count);
    529 
    530 	if (pdaList)
    531 		rf_FreePDAList(pdaList);
    532 	rf_FreeASMList(hdr->stripeMap);
    533 	rf_FreeAccessStripeMapHeader(hdr);
    534 }
    535 /* We can't use the large write optimization if there are any failures
    536  * in the stripe.  In the declustered layout, there is no way to
    537  * immediately determine what disks constitute a stripe, so we
    538  * actually have to hunt through the stripe looking for failures.  The
    539  * reason we map the parity instead of just using asm->parityInfo->col
    540  * is because the latter may have been already redirected to a spare
    541  * drive, which would mess up the computation of the stripe offset.
    542  *
    543  * ASSUMES AT MOST ONE FAILURE IN THE STRIPE.  */
    544 int
    545 rf_CheckStripeForFailures(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap)
    546 {
    547 	RF_RowCol_t tcol, pcol, *diskids, i;
    548 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    549 	RF_StripeCount_t stripeOffset;
    550 	int     numFailures;
    551 	RF_RaidAddr_t sosAddr;
    552 	RF_SectorNum_t diskOffset, poffset;
    553 
    554 	/* quick out in the fault-free case.  */
    555 	RF_LOCK_MUTEX(raidPtr->mutex);
    556 	numFailures = raidPtr->numFailures;
    557 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    558 	if (numFailures == 0)
    559 		return (0);
    560 
    561 	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr,
    562 						     asmap->raidAddress);
    563 	(layoutPtr->map->IdentifyStripe) (raidPtr, asmap->raidAddress,
    564 					  &diskids);
    565 	(layoutPtr->map->MapParity) (raidPtr, asmap->raidAddress,
    566 				     &pcol, &poffset, 0);	/* get pcol */
    567 
    568 	/* this need not be true if we've redirected the access to a
    569 	 * spare in another row RF_ASSERT(row == testrow); */
    570 	stripeOffset = 0;
    571 	for (i = 0; i < layoutPtr->numDataCol + layoutPtr->numParityCol; i++) {
    572 		if (diskids[i] != pcol) {
    573 			if (RF_DEAD_DISK(raidPtr->Disks[diskids[i]].status)) {
    574 				if (raidPtr->status != rf_rs_reconstructing)
    575 					return (1);
    576 				RF_ASSERT(raidPtr->reconControl->fcol == diskids[i]);
    577 				layoutPtr->map->MapSector(raidPtr,
    578 				    sosAddr + stripeOffset * layoutPtr->sectorsPerStripeUnit,
    579 				    &tcol, &diskOffset, 0);
    580 				RF_ASSERT(tcol == diskids[i]);
    581 				if (!rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, diskOffset))
    582 					return (1);
    583 				asmap->flags |= RF_ASM_REDIR_LARGE_WRITE;
    584 				return (0);
    585 			}
    586 			stripeOffset++;
    587 		}
    588 	}
    589 	return (0);
    590 }
    591 #if (RF_INCLUDE_DECL_PQ > 0) || (RF_INCLUDE_RAID6 > 0) || (RF_INCLUDE_EVENODD >0)
    592 /*
    593    return the number of failed data units in the stripe.
    594 */
    595 
    596 int
    597 rf_NumFailedDataUnitsInStripe(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap)
    598 {
    599 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    600 	RF_RowCol_t tcol, i;
    601 	RF_SectorNum_t diskOffset;
    602 	RF_RaidAddr_t sosAddr;
    603 	int     numFailures;
    604 
    605 	/* quick out in the fault-free case.  */
    606 	RF_LOCK_MUTEX(raidPtr->mutex);
    607 	numFailures = raidPtr->numFailures;
    608 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    609 	if (numFailures == 0)
    610 		return (0);
    611 	numFailures = 0;
    612 
    613 	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr,
    614 						     asmap->raidAddress);
    615 	for (i = 0; i < layoutPtr->numDataCol; i++) {
    616 		(layoutPtr->map->MapSector) (raidPtr, sosAddr + i * layoutPtr->sectorsPerStripeUnit,
    617 		    &trow, &tcol, &diskOffset, 0);
    618 		if (RF_DEAD_DISK(raidPtr->Disks[tcol].status))
    619 			numFailures++;
    620 	}
    621 
    622 	return numFailures;
    623 }
    624 #endif
    625 
    626 /****************************************************************************
    627  *
    628  * debug routines
    629  *
    630  ***************************************************************************/
    631 #if RF_DEBUG_MAP
    632 void
    633 rf_PrintAccessStripeMap(RF_AccessStripeMapHeader_t *asm_h)
    634 {
    635 	rf_PrintFullAccessStripeMap(asm_h, 0);
    636 }
    637 #endif
    638 
    639 /* prbuf - flag to print buffer pointers */
    640 void
    641 rf_PrintFullAccessStripeMap(RF_AccessStripeMapHeader_t *asm_h, int prbuf)
    642 {
    643 	int     i;
    644 	RF_AccessStripeMap_t *asmap = asm_h->stripeMap;
    645 	RF_PhysDiskAddr_t *p;
    646 	printf("%d stripes total\n", (int) asm_h->numStripes);
    647 	for (; asmap; asmap = asmap->next) {
    648 		/* printf("Num failures: %d\n",asmap->numDataFailed); */
    649 		/* printf("Num sectors:
    650 		 * %d\n",(int)asmap->totalSectorsAccessed); */
    651 		printf("Stripe %d (%d sectors), failures: %d data, %d parity: ",
    652 		    (int) asmap->stripeID,
    653 		    (int) asmap->totalSectorsAccessed,
    654 		    (int) asmap->numDataFailed,
    655 		    (int) asmap->numParityFailed);
    656 		if (asmap->parityInfo) {
    657 			printf("Parity [c%d s%d-%d", asmap->parityInfo->col,
    658 			    (int) asmap->parityInfo->startSector,
    659 			    (int) (asmap->parityInfo->startSector +
    660 				asmap->parityInfo->numSector - 1));
    661 			if (prbuf)
    662 				printf(" b0x%lx", (unsigned long) asmap->parityInfo->bufPtr);
    663 			if (asmap->parityInfo->next) {
    664 				printf(", c%d s%d-%d", asmap->parityInfo->next->col,
    665 				    (int) asmap->parityInfo->next->startSector,
    666 				    (int) (asmap->parityInfo->next->startSector +
    667 					asmap->parityInfo->next->numSector - 1));
    668 				if (prbuf)
    669 					printf(" b0x%lx", (unsigned long) asmap->parityInfo->next->bufPtr);
    670 				RF_ASSERT(asmap->parityInfo->next->next == NULL);
    671 			}
    672 			printf("]\n\t");
    673 		}
    674 		for (i = 0, p = asmap->physInfo; p; p = p->next, i++) {
    675 			printf("SU c%d s%d-%d ", p->col, (int) p->startSector,
    676 			    (int) (p->startSector + p->numSector - 1));
    677 			if (prbuf)
    678 				printf("b0x%lx ", (unsigned long) p->bufPtr);
    679 			if (i && !(i & 1))
    680 				printf("\n\t");
    681 		}
    682 		printf("\n");
    683 		p = asm_h->stripeMap->failedPDAs[0];
    684 		if (asm_h->stripeMap->numDataFailed + asm_h->stripeMap->numParityFailed > 1)
    685 			printf("[multiple failures]\n");
    686 		else
    687 			if (asm_h->stripeMap->numDataFailed + asm_h->stripeMap->numParityFailed > 0)
    688 				printf("\t[Failed PDA: c%d s%d-%d]\n", p->col,
    689 				    (int) p->startSector, (int) (p->startSector + p->numSector - 1));
    690 	}
    691 }
    692 
    693 #if RF_MAP_DEBUG
    694 void
    695 rf_PrintRaidAddressInfo(RF_Raid_t *raidPtr, RF_RaidAddr_t raidAddr,
    696 			RF_SectorCount_t numBlocks)
    697 {
    698 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    699 	RF_RaidAddr_t ra, sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, raidAddr);
    700 
    701 	printf("Raid addrs of SU boundaries from start of stripe to end of access:\n\t");
    702 	for (ra = sosAddr; ra <= raidAddr + numBlocks; ra += layoutPtr->sectorsPerStripeUnit) {
    703 		printf("%d (0x%x), ", (int) ra, (int) ra);
    704 	}
    705 	printf("\n");
    706 	printf("Offset into stripe unit: %d (0x%x)\n",
    707 	    (int) (raidAddr % layoutPtr->sectorsPerStripeUnit),
    708 	    (int) (raidAddr % layoutPtr->sectorsPerStripeUnit));
    709 }
    710 #endif
    711 /* given a parity descriptor and the starting address within a stripe,
    712  * range restrict the parity descriptor to touch only the correct
    713  * stuff.  */
    714 void
    715 rf_ASMParityAdjust(RF_PhysDiskAddr_t *toAdjust,
    716 		   RF_StripeNum_t startAddrWithinStripe,
    717 		   RF_SectorNum_t endAddress,
    718 		   RF_RaidLayout_t *layoutPtr,
    719 		   RF_AccessStripeMap_t *asm_p)
    720 {
    721 	RF_PhysDiskAddr_t *new_pda;
    722 
    723 	/* when we're accessing only a portion of one stripe unit, we
    724 	 * want the parity descriptor to identify only the chunk of
    725 	 * parity associated with the data.  When the access spans
    726 	 * exactly one stripe unit boundary and is less than a stripe
    727 	 * unit in size, it uses two disjoint regions of the parity
    728 	 * unit.  When an access spans more than one stripe unit
    729 	 * boundary, it uses all of the parity unit.
    730 	 *
    731 	 * To better handle the case where stripe units are small, we
    732 	 * may eventually want to change the 2nd case so that if the
    733 	 * SU size is below some threshold, we just read/write the
    734 	 * whole thing instead of breaking it up into two accesses. */
    735 	if (asm_p->numStripeUnitsAccessed == 1) {
    736 		int     x = (startAddrWithinStripe % layoutPtr->sectorsPerStripeUnit);
    737 		toAdjust->startSector += x;
    738 		toAdjust->raidAddress += x;
    739 		toAdjust->numSector = asm_p->physInfo->numSector;
    740 		RF_ASSERT(toAdjust->numSector != 0);
    741 	} else
    742 		if (asm_p->numStripeUnitsAccessed == 2 && asm_p->totalSectorsAccessed < layoutPtr->sectorsPerStripeUnit) {
    743 			int     x = (startAddrWithinStripe % layoutPtr->sectorsPerStripeUnit);
    744 
    745 			/* create a second pda and copy the parity map info
    746 			 * into it */
    747 			RF_ASSERT(toAdjust->next == NULL);
    748 			new_pda = toAdjust->next = rf_AllocPhysDiskAddr();
    749 			*new_pda = *toAdjust;	/* structure assignment */
    750 			new_pda->next = NULL;
    751 
    752 			/* adjust the start sector & number of blocks for the
    753 			 * first parity pda */
    754 			toAdjust->startSector += x;
    755 			toAdjust->raidAddress += x;
    756 			toAdjust->numSector = rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr, startAddrWithinStripe) - startAddrWithinStripe;
    757 			RF_ASSERT(toAdjust->numSector != 0);
    758 
    759 			/* adjust the second pda */
    760 			new_pda->numSector = endAddress - rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, endAddress);
    761 			/* new_pda->raidAddress =
    762 			 * rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr,
    763 			 * toAdjust->raidAddress); */
    764 			RF_ASSERT(new_pda->numSector != 0);
    765 		}
    766 }
    767 
    768 /* Check if a disk has been spared or failed. If spared, redirect the
    769  * I/O.  If it has been failed, record it in the asm pointer.  Fourth
    770  * arg is whether data or parity.  */
    771 void
    772 rf_ASMCheckStatus(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *pda_p,
    773 		  RF_AccessStripeMap_t *asm_p, RF_RaidDisk_t *disks,
    774 		  int parity)
    775 {
    776 	RF_DiskStatus_t dstatus;
    777 	RF_RowCol_t fcol;
    778 
    779 	dstatus = disks[pda_p->col].status;
    780 
    781 	if (dstatus == rf_ds_spared) {
    782 		/* if the disk has been spared, redirect access to the spare */
    783 		fcol = pda_p->col;
    784 		pda_p->col = disks[fcol].spareCol;
    785 	} else
    786 		if (dstatus == rf_ds_dist_spared) {
    787 			/* ditto if disk has been spared to dist spare space */
    788 #if RF_DEBUG_MAP
    789 			RF_RowCol_t oc = pda_p->col;
    790 			RF_SectorNum_t oo = pda_p->startSector;
    791 #endif
    792 			if (pda_p->type == RF_PDA_TYPE_DATA)
    793 				raidPtr->Layout.map->MapSector(raidPtr, pda_p->raidAddress, &pda_p->col, &pda_p->startSector, RF_REMAP);
    794 			else
    795 				raidPtr->Layout.map->MapParity(raidPtr, pda_p->raidAddress, &pda_p->col, &pda_p->startSector, RF_REMAP);
    796 
    797 #if RF_DEBUG_MAP
    798 			if (rf_mapDebug) {
    799 				printf("Redirected c %d o %d -> c %d o %d\n", oc, (int) oo,
    800 				    pda_p->col, (int) pda_p->startSector);
    801 			}
    802 #endif
    803 		} else
    804 			if (RF_DEAD_DISK(dstatus)) {
    805 				/* if the disk is inaccessible, mark the
    806 				 * failure */
    807 				if (parity)
    808 					asm_p->numParityFailed++;
    809 				else {
    810 					asm_p->numDataFailed++;
    811 				}
    812 				asm_p->failedPDAs[asm_p->numFailedPDAs] = pda_p;
    813 				asm_p->numFailedPDAs++;
    814 #if 0
    815 				switch (asm_p->numParityFailed + asm_p->numDataFailed) {
    816 				case 1:
    817 					asm_p->failedPDAs[0] = pda_p;
    818 					break;
    819 				case 2:
    820 					asm_p->failedPDAs[1] = pda_p;
    821 				default:
    822 					break;
    823 				}
    824 #endif
    825 			}
    826 	/* the redirected access should never span a stripe unit boundary */
    827 	RF_ASSERT(rf_RaidAddressToStripeUnitID(&raidPtr->Layout, pda_p->raidAddress) ==
    828 	    rf_RaidAddressToStripeUnitID(&raidPtr->Layout, pda_p->raidAddress + pda_p->numSector - 1));
    829 	RF_ASSERT(pda_p->col != -1);
    830 }
    831