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