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