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