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