Home | History | Annotate | Line # | Download | only in raidframe
rf_map.c revision 1.30
      1 /*	$NetBSD: rf_map.c,v 1.30 2004/03/05 02:53:56 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.30 2004/03/05 02:53:56 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_MIN_FREE_ASMHDR  32
    315 
    316 static struct pool rf_asm_pool;
    317 #define RF_MAX_FREE_ASM 192
    318 #define RF_MIN_FREE_ASM  64
    319 
    320 static struct pool rf_pda_pool;   /* may need to be visible for
    321 				     rf_dagdegrd.c and rf_dagdegwr.c,
    322 				     if they can be convinced to free
    323 				     the space easily */
    324 #define RF_MAX_FREE_PDA 192
    325 #define RF_MIN_FREE_PDA  64
    326 
    327 /* called at shutdown time.  So far, all that is necessary is to
    328    release all the free lists */
    329 static void rf_ShutdownMapModule(void *);
    330 static void
    331 rf_ShutdownMapModule(void *ignored)
    332 {
    333 	pool_destroy(&rf_asmhdr_pool);
    334 	pool_destroy(&rf_asm_pool);
    335 	pool_destroy(&rf_pda_pool);
    336 }
    337 
    338 int
    339 rf_ConfigureMapModule(RF_ShutdownList_t **listp)
    340 {
    341 
    342 	pool_init(&rf_asmhdr_pool, sizeof(RF_AccessStripeMapHeader_t),
    343 		  0, 0, 0, "rf_asmhdr_pl", NULL);
    344 	pool_sethiwat(&rf_asmhdr_pool, RF_MAX_FREE_ASMHDR);
    345 	pool_prime(&rf_asmhdr_pool, RF_MIN_FREE_ASMHDR);
    346 	pool_setlowat(&rf_asmhdr_pool, RF_MIN_FREE_ASMHDR);
    347 
    348 	pool_init(&rf_asm_pool, sizeof(RF_AccessStripeMap_t),
    349 		  0, 0, 0, "rf_asm_pl", NULL);
    350 	pool_sethiwat(&rf_asm_pool, RF_MAX_FREE_ASM);
    351 	pool_prime(&rf_asm_pool, RF_MIN_FREE_ASM);
    352 	pool_setlowat(&rf_asm_pool, RF_MIN_FREE_ASM);
    353 
    354 	pool_init(&rf_pda_pool, sizeof(RF_PhysDiskAddr_t),
    355 		  0, 0, 0, "rf_pda_pl", NULL);
    356 	pool_sethiwat(&rf_pda_pool, RF_MAX_FREE_PDA);
    357 	pool_prime(&rf_pda_pool, RF_MIN_FREE_PDA);
    358 	pool_setlowat(&rf_pda_pool, RF_MIN_FREE_PDA);
    359 
    360 	rf_ShutdownCreate(listp, rf_ShutdownMapModule, NULL);
    361 
    362 	return (0);
    363 }
    364 
    365 RF_AccessStripeMapHeader_t *
    366 rf_AllocAccessStripeMapHeader()
    367 {
    368 	RF_AccessStripeMapHeader_t *p;
    369 
    370 	p = pool_get(&rf_asmhdr_pool, PR_WAITOK);
    371 	memset((char *) p, 0, sizeof(RF_AccessStripeMapHeader_t));
    372 
    373 	return (p);
    374 }
    375 
    376 void
    377 rf_FreeAccessStripeMapHeader(RF_AccessStripeMapHeader_t *p)
    378 {
    379 	pool_put(&rf_asmhdr_pool, p);
    380 }
    381 
    382 RF_PhysDiskAddr_t *
    383 rf_AllocPhysDiskAddr()
    384 {
    385 	RF_PhysDiskAddr_t *p;
    386 
    387 	p = pool_get(&rf_pda_pool, PR_WAITOK);
    388 	memset((char *) p, 0, sizeof(RF_PhysDiskAddr_t));
    389 
    390 	return (p);
    391 }
    392 /* allocates a list of PDAs, locking the free list only once when we
    393  * have to call calloc, we do it one component at a time to simplify
    394  * the process of freeing the list at program shutdown.  This should
    395  * not be much of a performance hit, because it should be very
    396  * infrequently executed.  */
    397 RF_PhysDiskAddr_t *
    398 rf_AllocPDAList(int count)
    399 {
    400 	RF_PhysDiskAddr_t *p, *prev;
    401 	int i;
    402 
    403 	p = NULL;
    404 	prev = NULL;
    405 	for (i = 0; i < count; i++) {
    406 		p = pool_get(&rf_pda_pool, PR_WAITOK);
    407 		p->next = prev;
    408 		prev = p;
    409 	}
    410 
    411 	return (p);
    412 }
    413 
    414 #if RF_INCLUDE_PARITYLOGGING > 0
    415 void
    416 rf_FreePhysDiskAddr(RF_PhysDiskAddr_t *p)
    417 {
    418 	pool_put(&rf_pda_pool, p);
    419 }
    420 #endif
    421 
    422 static void
    423 rf_FreePDAList(RF_PhysDiskAddr_t *pda_list)
    424 {
    425 	RF_PhysDiskAddr_t *p, *tmp;
    426 
    427 	p=pda_list;
    428 	while (p) {
    429 		tmp = p->next;
    430 		pool_put(&rf_pda_pool, p);
    431 		p = tmp;
    432 	}
    433 }
    434 
    435 /* this is essentially identical to AllocPDAList.  I should combine
    436  * the two.  when we have to call calloc, we do it one component at a
    437  * time to simplify the process of freeing the list at program
    438  * shutdown.  This should not be much of a performance hit, because it
    439  * should be very infrequently executed.  */
    440 RF_AccessStripeMap_t *
    441 rf_AllocASMList(int count)
    442 {
    443 	RF_AccessStripeMap_t *p, *prev;
    444 	int i;
    445 
    446 	p = NULL;
    447 	prev = NULL;
    448 	for (i = 0; i < count; i++) {
    449 		p = pool_get(&rf_asm_pool, PR_WAITOK);
    450 		p->next = prev;
    451 		prev = p;
    452 	}
    453 	return (p);
    454 }
    455 
    456 static void
    457 rf_FreeASMList(RF_AccessStripeMap_t *asm_list)
    458 {
    459 	RF_AccessStripeMap_t *p, *tmp;
    460 
    461 	p=asm_list;
    462 	while (p) {
    463 		tmp = p->next;
    464 		pool_put(&rf_asm_pool, p);
    465 		p = tmp;
    466 	}
    467 }
    468 
    469 void
    470 rf_FreeAccessStripeMap(RF_AccessStripeMapHeader_t *hdr)
    471 {
    472 	RF_AccessStripeMap_t *p;
    473 	RF_PhysDiskAddr_t *pdp, *trailer, *pdaList = NULL, *pdaEnd = NULL;
    474 	int     count = 0, t, asm_count = 0;
    475 
    476 	for (p = hdr->stripeMap; p; p = p->next) {
    477 
    478 		/* link the 3 pda lists into the accumulating pda list */
    479 
    480 		if (!pdaList)
    481 			pdaList = p->qInfo;
    482 		else
    483 			pdaEnd->next = p->qInfo;
    484 		for (trailer = NULL, pdp = p->qInfo; pdp;) {
    485 			trailer = pdp;
    486 			pdp = pdp->next;
    487 			count++;
    488 		}
    489 		if (trailer)
    490 			pdaEnd = trailer;
    491 
    492 		if (!pdaList)
    493 			pdaList = p->parityInfo;
    494 		else
    495 			pdaEnd->next = p->parityInfo;
    496 		for (trailer = NULL, pdp = p->parityInfo; pdp;) {
    497 			trailer = pdp;
    498 			pdp = pdp->next;
    499 			count++;
    500 		}
    501 		if (trailer)
    502 			pdaEnd = trailer;
    503 
    504 		if (!pdaList)
    505 			pdaList = p->physInfo;
    506 		else
    507 			pdaEnd->next = p->physInfo;
    508 		for (trailer = NULL, pdp = p->physInfo; pdp;) {
    509 			trailer = pdp;
    510 			pdp = pdp->next;
    511 			count++;
    512 		}
    513 		if (trailer)
    514 			pdaEnd = trailer;
    515 
    516 		asm_count++;
    517 	}
    518 
    519 	/* debug only */
    520 	for (t = 0, pdp = pdaList; pdp; pdp = pdp->next)
    521 		t++;
    522 	RF_ASSERT(t == count);
    523 
    524 	if (pdaList)
    525 		rf_FreePDAList(pdaList);
    526 	rf_FreeASMList(hdr->stripeMap);
    527 	rf_FreeAccessStripeMapHeader(hdr);
    528 }
    529 /* We can't use the large write optimization if there are any failures
    530  * in the stripe.  In the declustered layout, there is no way to
    531  * immediately determine what disks constitute a stripe, so we
    532  * actually have to hunt through the stripe looking for failures.  The
    533  * reason we map the parity instead of just using asm->parityInfo->col
    534  * is because the latter may have been already redirected to a spare
    535  * drive, which would mess up the computation of the stripe offset.
    536  *
    537  * ASSUMES AT MOST ONE FAILURE IN THE STRIPE.  */
    538 int
    539 rf_CheckStripeForFailures(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap)
    540 {
    541 	RF_RowCol_t tcol, pcol, *diskids, i;
    542 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    543 	RF_StripeCount_t stripeOffset;
    544 	int     numFailures;
    545 	RF_RaidAddr_t sosAddr;
    546 	RF_SectorNum_t diskOffset, poffset;
    547 
    548 	/* quick out in the fault-free case.  */
    549 	RF_LOCK_MUTEX(raidPtr->mutex);
    550 	numFailures = raidPtr->numFailures;
    551 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    552 	if (numFailures == 0)
    553 		return (0);
    554 
    555 	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr,
    556 						     asmap->raidAddress);
    557 	(layoutPtr->map->IdentifyStripe) (raidPtr, asmap->raidAddress,
    558 					  &diskids);
    559 	(layoutPtr->map->MapParity) (raidPtr, asmap->raidAddress,
    560 				     &pcol, &poffset, 0);	/* get pcol */
    561 
    562 	/* this need not be true if we've redirected the access to a
    563 	 * spare in another row RF_ASSERT(row == testrow); */
    564 	stripeOffset = 0;
    565 	for (i = 0; i < layoutPtr->numDataCol + layoutPtr->numParityCol; i++) {
    566 		if (diskids[i] != pcol) {
    567 			if (RF_DEAD_DISK(raidPtr->Disks[diskids[i]].status)) {
    568 				if (raidPtr->status != rf_rs_reconstructing)
    569 					return (1);
    570 				RF_ASSERT(raidPtr->reconControl->fcol == diskids[i]);
    571 				layoutPtr->map->MapSector(raidPtr,
    572 				    sosAddr + stripeOffset * layoutPtr->sectorsPerStripeUnit,
    573 				    &tcol, &diskOffset, 0);
    574 				RF_ASSERT(tcol == diskids[i]);
    575 				if (!rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, diskOffset))
    576 					return (1);
    577 				asmap->flags |= RF_ASM_REDIR_LARGE_WRITE;
    578 				return (0);
    579 			}
    580 			stripeOffset++;
    581 		}
    582 	}
    583 	return (0);
    584 }
    585 #if (RF_INCLUDE_DECL_PQ > 0) || (RF_INCLUDE_RAID6 > 0) || (RF_INCLUDE_EVENODD >0)
    586 /*
    587    return the number of failed data units in the stripe.
    588 */
    589 
    590 int
    591 rf_NumFailedDataUnitsInStripe(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap)
    592 {
    593 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    594 	RF_RowCol_t tcol, i;
    595 	RF_SectorNum_t diskOffset;
    596 	RF_RaidAddr_t sosAddr;
    597 	int     numFailures;
    598 
    599 	/* quick out in the fault-free case.  */
    600 	RF_LOCK_MUTEX(raidPtr->mutex);
    601 	numFailures = raidPtr->numFailures;
    602 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    603 	if (numFailures == 0)
    604 		return (0);
    605 	numFailures = 0;
    606 
    607 	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr,
    608 						     asmap->raidAddress);
    609 	for (i = 0; i < layoutPtr->numDataCol; i++) {
    610 		(layoutPtr->map->MapSector) (raidPtr, sosAddr + i * layoutPtr->sectorsPerStripeUnit,
    611 		    &trow, &tcol, &diskOffset, 0);
    612 		if (RF_DEAD_DISK(raidPtr->Disks[tcol].status))
    613 			numFailures++;
    614 	}
    615 
    616 	return numFailures;
    617 }
    618 #endif
    619 
    620 /****************************************************************************
    621  *
    622  * debug routines
    623  *
    624  ***************************************************************************/
    625 #if RF_DEBUG_MAP
    626 void
    627 rf_PrintAccessStripeMap(RF_AccessStripeMapHeader_t *asm_h)
    628 {
    629 	rf_PrintFullAccessStripeMap(asm_h, 0);
    630 }
    631 #endif
    632 
    633 /* prbuf - flag to print buffer pointers */
    634 void
    635 rf_PrintFullAccessStripeMap(RF_AccessStripeMapHeader_t *asm_h, int prbuf)
    636 {
    637 	int     i;
    638 	RF_AccessStripeMap_t *asmap = asm_h->stripeMap;
    639 	RF_PhysDiskAddr_t *p;
    640 	printf("%d stripes total\n", (int) asm_h->numStripes);
    641 	for (; asmap; asmap = asmap->next) {
    642 		/* printf("Num failures: %d\n",asmap->numDataFailed); */
    643 		/* printf("Num sectors:
    644 		 * %d\n",(int)asmap->totalSectorsAccessed); */
    645 		printf("Stripe %d (%d sectors), failures: %d data, %d parity: ",
    646 		    (int) asmap->stripeID,
    647 		    (int) asmap->totalSectorsAccessed,
    648 		    (int) asmap->numDataFailed,
    649 		    (int) asmap->numParityFailed);
    650 		if (asmap->parityInfo) {
    651 			printf("Parity [c%d s%d-%d", asmap->parityInfo->col,
    652 			    (int) asmap->parityInfo->startSector,
    653 			    (int) (asmap->parityInfo->startSector +
    654 				asmap->parityInfo->numSector - 1));
    655 			if (prbuf)
    656 				printf(" b0x%lx", (unsigned long) asmap->parityInfo->bufPtr);
    657 			if (asmap->parityInfo->next) {
    658 				printf(", c%d s%d-%d", asmap->parityInfo->next->col,
    659 				    (int) asmap->parityInfo->next->startSector,
    660 				    (int) (asmap->parityInfo->next->startSector +
    661 					asmap->parityInfo->next->numSector - 1));
    662 				if (prbuf)
    663 					printf(" b0x%lx", (unsigned long) asmap->parityInfo->next->bufPtr);
    664 				RF_ASSERT(asmap->parityInfo->next->next == NULL);
    665 			}
    666 			printf("]\n\t");
    667 		}
    668 		for (i = 0, p = asmap->physInfo; p; p = p->next, i++) {
    669 			printf("SU c%d s%d-%d ", p->col, (int) p->startSector,
    670 			    (int) (p->startSector + p->numSector - 1));
    671 			if (prbuf)
    672 				printf("b0x%lx ", (unsigned long) p->bufPtr);
    673 			if (i && !(i & 1))
    674 				printf("\n\t");
    675 		}
    676 		printf("\n");
    677 		p = asm_h->stripeMap->failedPDAs[0];
    678 		if (asm_h->stripeMap->numDataFailed + asm_h->stripeMap->numParityFailed > 1)
    679 			printf("[multiple failures]\n");
    680 		else
    681 			if (asm_h->stripeMap->numDataFailed + asm_h->stripeMap->numParityFailed > 0)
    682 				printf("\t[Failed PDA: c%d s%d-%d]\n", p->col,
    683 				    (int) p->startSector, (int) (p->startSector + p->numSector - 1));
    684 	}
    685 }
    686 
    687 #if RF_MAP_DEBUG
    688 void
    689 rf_PrintRaidAddressInfo(RF_Raid_t *raidPtr, RF_RaidAddr_t raidAddr,
    690 			RF_SectorCount_t numBlocks)
    691 {
    692 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    693 	RF_RaidAddr_t ra, sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, raidAddr);
    694 
    695 	printf("Raid addrs of SU boundaries from start of stripe to end of access:\n\t");
    696 	for (ra = sosAddr; ra <= raidAddr + numBlocks; ra += layoutPtr->sectorsPerStripeUnit) {
    697 		printf("%d (0x%x), ", (int) ra, (int) ra);
    698 	}
    699 	printf("\n");
    700 	printf("Offset into stripe unit: %d (0x%x)\n",
    701 	    (int) (raidAddr % layoutPtr->sectorsPerStripeUnit),
    702 	    (int) (raidAddr % layoutPtr->sectorsPerStripeUnit));
    703 }
    704 #endif
    705 /* given a parity descriptor and the starting address within a stripe,
    706  * range restrict the parity descriptor to touch only the correct
    707  * stuff.  */
    708 void
    709 rf_ASMParityAdjust(RF_PhysDiskAddr_t *toAdjust,
    710 		   RF_StripeNum_t startAddrWithinStripe,
    711 		   RF_SectorNum_t endAddress,
    712 		   RF_RaidLayout_t *layoutPtr,
    713 		   RF_AccessStripeMap_t *asm_p)
    714 {
    715 	RF_PhysDiskAddr_t *new_pda;
    716 
    717 	/* when we're accessing only a portion of one stripe unit, we
    718 	 * want the parity descriptor to identify only the chunk of
    719 	 * parity associated with the data.  When the access spans
    720 	 * exactly one stripe unit boundary and is less than a stripe
    721 	 * unit in size, it uses two disjoint regions of the parity
    722 	 * unit.  When an access spans more than one stripe unit
    723 	 * boundary, it uses all of the parity unit.
    724 	 *
    725 	 * To better handle the case where stripe units are small, we
    726 	 * may eventually want to change the 2nd case so that if the
    727 	 * SU size is below some threshold, we just read/write the
    728 	 * whole thing instead of breaking it up into two accesses. */
    729 	if (asm_p->numStripeUnitsAccessed == 1) {
    730 		int     x = (startAddrWithinStripe % layoutPtr->sectorsPerStripeUnit);
    731 		toAdjust->startSector += x;
    732 		toAdjust->raidAddress += x;
    733 		toAdjust->numSector = asm_p->physInfo->numSector;
    734 		RF_ASSERT(toAdjust->numSector != 0);
    735 	} else
    736 		if (asm_p->numStripeUnitsAccessed == 2 && asm_p->totalSectorsAccessed < layoutPtr->sectorsPerStripeUnit) {
    737 			int     x = (startAddrWithinStripe % layoutPtr->sectorsPerStripeUnit);
    738 
    739 			/* create a second pda and copy the parity map info
    740 			 * into it */
    741 			RF_ASSERT(toAdjust->next == NULL);
    742 			new_pda = toAdjust->next = rf_AllocPhysDiskAddr();
    743 			*new_pda = *toAdjust;	/* structure assignment */
    744 			new_pda->next = NULL;
    745 
    746 			/* adjust the start sector & number of blocks for the
    747 			 * first parity pda */
    748 			toAdjust->startSector += x;
    749 			toAdjust->raidAddress += x;
    750 			toAdjust->numSector = rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr, startAddrWithinStripe) - startAddrWithinStripe;
    751 			RF_ASSERT(toAdjust->numSector != 0);
    752 
    753 			/* adjust the second pda */
    754 			new_pda->numSector = endAddress - rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, endAddress);
    755 			/* new_pda->raidAddress =
    756 			 * rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr,
    757 			 * toAdjust->raidAddress); */
    758 			RF_ASSERT(new_pda->numSector != 0);
    759 		}
    760 }
    761 
    762 /* Check if a disk has been spared or failed. If spared, redirect the
    763  * I/O.  If it has been failed, record it in the asm pointer.  Fourth
    764  * arg is whether data or parity.  */
    765 void
    766 rf_ASMCheckStatus(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *pda_p,
    767 		  RF_AccessStripeMap_t *asm_p, RF_RaidDisk_t *disks,
    768 		  int parity)
    769 {
    770 	RF_DiskStatus_t dstatus;
    771 	RF_RowCol_t fcol;
    772 
    773 	dstatus = disks[pda_p->col].status;
    774 
    775 	if (dstatus == rf_ds_spared) {
    776 		/* if the disk has been spared, redirect access to the spare */
    777 		fcol = pda_p->col;
    778 		pda_p->col = disks[fcol].spareCol;
    779 	} else
    780 		if (dstatus == rf_ds_dist_spared) {
    781 			/* ditto if disk has been spared to dist spare space */
    782 #if RF_DEBUG_MAP
    783 			RF_RowCol_t oc = pda_p->col;
    784 			RF_SectorNum_t oo = pda_p->startSector;
    785 #endif
    786 			if (pda_p->type == RF_PDA_TYPE_DATA)
    787 				raidPtr->Layout.map->MapSector(raidPtr, pda_p->raidAddress, &pda_p->col, &pda_p->startSector, RF_REMAP);
    788 			else
    789 				raidPtr->Layout.map->MapParity(raidPtr, pda_p->raidAddress, &pda_p->col, &pda_p->startSector, RF_REMAP);
    790 
    791 #if RF_DEBUG_MAP
    792 			if (rf_mapDebug) {
    793 				printf("Redirected c %d o %d -> c %d o %d\n", oc, (int) oo,
    794 				    pda_p->col, (int) pda_p->startSector);
    795 			}
    796 #endif
    797 		} else
    798 			if (RF_DEAD_DISK(dstatus)) {
    799 				/* if the disk is inaccessible, mark the
    800 				 * failure */
    801 				if (parity)
    802 					asm_p->numParityFailed++;
    803 				else {
    804 					asm_p->numDataFailed++;
    805 				}
    806 				asm_p->failedPDAs[asm_p->numFailedPDAs] = pda_p;
    807 				asm_p->numFailedPDAs++;
    808 #if 0
    809 				switch (asm_p->numParityFailed + asm_p->numDataFailed) {
    810 				case 1:
    811 					asm_p->failedPDAs[0] = pda_p;
    812 					break;
    813 				case 2:
    814 					asm_p->failedPDAs[1] = pda_p;
    815 				default:
    816 					break;
    817 				}
    818 #endif
    819 			}
    820 	/* the redirected access should never span a stripe unit boundary */
    821 	RF_ASSERT(rf_RaidAddressToStripeUnitID(&raidPtr->Layout, pda_p->raidAddress) ==
    822 	    rf_RaidAddressToStripeUnitID(&raidPtr->Layout, pda_p->raidAddress + pda_p->numSector - 1));
    823 	RF_ASSERT(pda_p->col != -1);
    824 }
    825