Home | History | Annotate | Line # | Download | only in raidframe
rf_map.c revision 1.16
      1 /*	$NetBSD: rf_map.c,v 1.16 2002/09/21 01:12:50 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.16 2002/09/21 01:12:50 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 void
    420 rf_FreePhysDiskAddr(p)
    421 	RF_PhysDiskAddr_t *p;
    422 {
    423 	RF_FREELIST_FREE(rf_pda_freelist, p, next);
    424 }
    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 void
    451 rf_FreeAccessStripeMapComponent(p)
    452 	RF_AccessStripeMap_t *p;
    453 {
    454 	RF_FREELIST_FREE(rf_asm_freelist, p, next);
    455 }
    456 
    457 static void
    458 rf_FreeASMList(l_start, l_end, count)
    459 	RF_AccessStripeMap_t *l_start, *l_end;
    460 	int     count;
    461 {
    462 	RF_FREELIST_FREE_N(rf_asm_freelist, l_start, next, (RF_AccessStripeMap_t *), count);
    463 }
    464 
    465 void
    466 rf_FreeAccessStripeMap(hdr)
    467 	RF_AccessStripeMapHeader_t *hdr;
    468 {
    469 	RF_AccessStripeMap_t *p, *pt = NULL;
    470 	RF_PhysDiskAddr_t *pdp, *trailer, *pdaList = NULL, *pdaEnd = NULL;
    471 	int     count = 0, t, asm_count = 0;
    472 
    473 	for (p = hdr->stripeMap; p; p = p->next) {
    474 
    475 		/* link the 3 pda lists into the accumulating pda list */
    476 
    477 		if (!pdaList)
    478 			pdaList = p->qInfo;
    479 		else
    480 			pdaEnd->next = p->qInfo;
    481 		for (trailer = NULL, pdp = p->qInfo; pdp;) {
    482 			trailer = pdp;
    483 			pdp = pdp->next;
    484 			count++;
    485 		}
    486 		if (trailer)
    487 			pdaEnd = trailer;
    488 
    489 		if (!pdaList)
    490 			pdaList = p->parityInfo;
    491 		else
    492 			pdaEnd->next = p->parityInfo;
    493 		for (trailer = NULL, pdp = p->parityInfo; pdp;) {
    494 			trailer = pdp;
    495 			pdp = pdp->next;
    496 			count++;
    497 		}
    498 		if (trailer)
    499 			pdaEnd = trailer;
    500 
    501 		if (!pdaList)
    502 			pdaList = p->physInfo;
    503 		else
    504 			pdaEnd->next = p->physInfo;
    505 		for (trailer = NULL, pdp = p->physInfo; pdp;) {
    506 			trailer = pdp;
    507 			pdp = pdp->next;
    508 			count++;
    509 		}
    510 		if (trailer)
    511 			pdaEnd = trailer;
    512 
    513 		pt = p;
    514 		asm_count++;
    515 	}
    516 
    517 	/* debug only */
    518 	for (t = 0, pdp = pdaList; pdp; pdp = pdp->next)
    519 		t++;
    520 	RF_ASSERT(t == count);
    521 
    522 	if (pdaList)
    523 		rf_FreePDAList(pdaList, pdaEnd, count);
    524 	rf_FreeASMList(hdr->stripeMap, pt, asm_count);
    525 	rf_FreeAccessStripeMapHeader(hdr);
    526 }
    527 /* We can't use the large write optimization if there are any failures in the stripe.
    528  * In the declustered layout, there is no way to immediately determine what disks
    529  * constitute a stripe, so we actually have to hunt through the stripe looking for failures.
    530  * The reason we map the parity instead of just using asm->parityInfo->col is because
    531  * the latter may have been already redirected to a spare drive, which would
    532  * mess up the computation of the stripe offset.
    533  *
    534  * ASSUMES AT MOST ONE FAILURE IN THE STRIPE.
    535  */
    536 int
    537 rf_CheckStripeForFailures(raidPtr, asmap)
    538 	RF_Raid_t *raidPtr;
    539 	RF_AccessStripeMap_t *asmap;
    540 {
    541 	RF_RowCol_t trow, tcol, prow, pcol, *diskids, row, i;
    542 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    543 	RF_StripeCount_t stripeOffset;
    544 	int     numFailures;
    545 	RF_RaidAddr_t sosAddr;
    546 	RF_SectorNum_t diskOffset, poffset;
    547 	RF_RowCol_t testrow;
    548 
    549 	/* quick out in the fault-free case.  */
    550 	RF_LOCK_MUTEX(raidPtr->mutex);
    551 	numFailures = raidPtr->numFailures;
    552 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    553 	if (numFailures == 0)
    554 		return (0);
    555 
    556 	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, asmap->raidAddress);
    557 	row = asmap->physInfo->row;
    558 	(layoutPtr->map->IdentifyStripe) (raidPtr, asmap->raidAddress, &diskids, &testrow);
    559 	(layoutPtr->map->MapParity) (raidPtr, asmap->raidAddress, &prow, &pcol, &poffset, 0);	/* get pcol */
    560 
    561 	/* this need not be true if we've redirected the access to a spare in
    562 	 * another row RF_ASSERT(row == testrow); */
    563 	stripeOffset = 0;
    564 	for (i = 0; i < layoutPtr->numDataCol + layoutPtr->numParityCol; i++) {
    565 		if (diskids[i] != pcol) {
    566 			if (RF_DEAD_DISK(raidPtr->Disks[testrow][diskids[i]].status)) {
    567 				if (raidPtr->status[testrow] != rf_rs_reconstructing)
    568 					return (1);
    569 				RF_ASSERT(raidPtr->reconControl[testrow]->fcol == diskids[i]);
    570 				layoutPtr->map->MapSector(raidPtr,
    571 				    sosAddr + stripeOffset * layoutPtr->sectorsPerStripeUnit,
    572 				    &trow, &tcol, &diskOffset, 0);
    573 				RF_ASSERT((trow == testrow) && (tcol == diskids[i]));
    574 				if (!rf_CheckRUReconstructed(raidPtr->reconControl[testrow]->reconMap, diskOffset))
    575 					return (1);
    576 				asmap->flags |= RF_ASM_REDIR_LARGE_WRITE;
    577 				return (0);
    578 			}
    579 			stripeOffset++;
    580 		}
    581 	}
    582 	return (0);
    583 }
    584 /*
    585    return the number of failed data units in the stripe.
    586 */
    587 
    588 int
    589 rf_NumFailedDataUnitsInStripe(raidPtr, asmap)
    590 	RF_Raid_t *raidPtr;
    591 	RF_AccessStripeMap_t *asmap;
    592 {
    593 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    594 	RF_RowCol_t trow, tcol, row, i;
    595 	RF_SectorNum_t diskOffset;
    596 	RF_RaidAddr_t sosAddr;
    597 	int     numFailures;
    598 
    599 	/* quick out in the fault-free case.  */
    600 	RF_LOCK_MUTEX(raidPtr->mutex);
    601 	numFailures = raidPtr->numFailures;
    602 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    603 	if (numFailures == 0)
    604 		return (0);
    605 	numFailures = 0;
    606 
    607 	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, asmap->raidAddress);
    608 	row = asmap->physInfo->row;
    609 	for (i = 0; i < layoutPtr->numDataCol; i++) {
    610 		(layoutPtr->map->MapSector) (raidPtr, sosAddr + i * layoutPtr->sectorsPerStripeUnit,
    611 		    &trow, &tcol, &diskOffset, 0);
    612 		if (RF_DEAD_DISK(raidPtr->Disks[trow][tcol].status))
    613 			numFailures++;
    614 	}
    615 
    616 	return numFailures;
    617 }
    618 
    619 
    620 /*****************************************************************************************
    621  *
    622  * debug routines
    623  *
    624  ****************************************************************************************/
    625 
    626 void
    627 rf_PrintAccessStripeMap(asm_h)
    628 	RF_AccessStripeMapHeader_t *asm_h;
    629 {
    630 	rf_PrintFullAccessStripeMap(asm_h, 0);
    631 }
    632 
    633 void
    634 rf_PrintFullAccessStripeMap(asm_h, prbuf)
    635 	RF_AccessStripeMapHeader_t *asm_h;
    636 	int     prbuf;		/* flag to print buffer pointers */
    637 {
    638 	int     i;
    639 	RF_AccessStripeMap_t *asmap = asm_h->stripeMap;
    640 	RF_PhysDiskAddr_t *p;
    641 	printf("%d stripes total\n", (int) asm_h->numStripes);
    642 	for (; asmap; asmap = asmap->next) {
    643 		/* printf("Num failures: %d\n",asmap->numDataFailed); */
    644 		/* printf("Num sectors:
    645 		 * %d\n",(int)asmap->totalSectorsAccessed); */
    646 		printf("Stripe %d (%d sectors), failures: %d data, %d parity: ",
    647 		    (int) asmap->stripeID,
    648 		    (int) asmap->totalSectorsAccessed,
    649 		    (int) asmap->numDataFailed,
    650 		    (int) asmap->numParityFailed);
    651 		if (asmap->parityInfo) {
    652 			printf("Parity [r%d c%d s%d-%d", asmap->parityInfo->row, asmap->parityInfo->col,
    653 			    (int) asmap->parityInfo->startSector,
    654 			    (int) (asmap->parityInfo->startSector +
    655 				asmap->parityInfo->numSector - 1));
    656 			if (prbuf)
    657 				printf(" b0x%lx", (unsigned long) asmap->parityInfo->bufPtr);
    658 			if (asmap->parityInfo->next) {
    659 				printf(", r%d c%d s%d-%d", asmap->parityInfo->next->row,
    660 				    asmap->parityInfo->next->col,
    661 				    (int) asmap->parityInfo->next->startSector,
    662 				    (int) (asmap->parityInfo->next->startSector +
    663 					asmap->parityInfo->next->numSector - 1));
    664 				if (prbuf)
    665 					printf(" b0x%lx", (unsigned long) asmap->parityInfo->next->bufPtr);
    666 				RF_ASSERT(asmap->parityInfo->next->next == NULL);
    667 			}
    668 			printf("]\n\t");
    669 		}
    670 		for (i = 0, p = asmap->physInfo; p; p = p->next, i++) {
    671 			printf("SU r%d c%d s%d-%d ", p->row, p->col, (int) p->startSector,
    672 			    (int) (p->startSector + p->numSector - 1));
    673 			if (prbuf)
    674 				printf("b0x%lx ", (unsigned long) p->bufPtr);
    675 			if (i && !(i & 1))
    676 				printf("\n\t");
    677 		}
    678 		printf("\n");
    679 		p = asm_h->stripeMap->failedPDAs[0];
    680 		if (asm_h->stripeMap->numDataFailed + asm_h->stripeMap->numParityFailed > 1)
    681 			printf("[multiple failures]\n");
    682 		else
    683 			if (asm_h->stripeMap->numDataFailed + asm_h->stripeMap->numParityFailed > 0)
    684 				printf("\t[Failed PDA: r%d c%d s%d-%d]\n", p->row, p->col,
    685 				    (int) p->startSector, (int) (p->startSector + p->numSector - 1));
    686 	}
    687 }
    688 
    689 #if RF_MAP_DEBUG
    690 void
    691 rf_PrintRaidAddressInfo(raidPtr, raidAddr, numBlocks)
    692 	RF_Raid_t *raidPtr;
    693 	RF_RaidAddr_t raidAddr;
    694 	RF_SectorCount_t numBlocks;
    695 {
    696 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    697 	RF_RaidAddr_t ra, sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, raidAddr);
    698 
    699 	printf("Raid addrs of SU boundaries from start of stripe to end of access:\n\t");
    700 	for (ra = sosAddr; ra <= raidAddr + numBlocks; ra += layoutPtr->sectorsPerStripeUnit) {
    701 		printf("%d (0x%x), ", (int) ra, (int) ra);
    702 	}
    703 	printf("\n");
    704 	printf("Offset into stripe unit: %d (0x%x)\n",
    705 	    (int) (raidAddr % layoutPtr->sectorsPerStripeUnit),
    706 	    (int) (raidAddr % layoutPtr->sectorsPerStripeUnit));
    707 }
    708 #endif
    709 /*
    710    given a parity descriptor and the starting address within a stripe,
    711    range restrict the parity descriptor to touch only the correct stuff.
    712 */
    713 void
    714 rf_ASMParityAdjust(
    715     RF_PhysDiskAddr_t * toAdjust,
    716     RF_StripeNum_t startAddrWithinStripe,
    717     RF_SectorNum_t endAddress,
    718     RF_RaidLayout_t * layoutPtr,
    719     RF_AccessStripeMap_t * asm_p)
    720 {
    721 	RF_PhysDiskAddr_t *new_pda;
    722 
    723 	/* when we're accessing only a portion of one stripe unit, we want the
    724 	 * parity descriptor to identify only the chunk of parity associated
    725 	 * with the data.  When the access spans exactly one stripe unit
    726 	 * boundary and is less than a stripe unit in size, it uses two
    727 	 * disjoint regions of the parity unit.  When an access spans more
    728 	 * than one stripe unit boundary, it uses all of the parity unit.
    729 	 *
    730 	 * To better handle the case where stripe units are small, we may
    731 	 * eventually want to change the 2nd case so that if the SU size is
    732 	 * below some threshold, we just read/write the whole thing instead of
    733 	 * breaking it up into two accesses. */
    734 	if (asm_p->numStripeUnitsAccessed == 1) {
    735 		int     x = (startAddrWithinStripe % layoutPtr->sectorsPerStripeUnit);
    736 		toAdjust->startSector += x;
    737 		toAdjust->raidAddress += x;
    738 		toAdjust->numSector = asm_p->physInfo->numSector;
    739 		RF_ASSERT(toAdjust->numSector != 0);
    740 	} else
    741 		if (asm_p->numStripeUnitsAccessed == 2 && asm_p->totalSectorsAccessed < layoutPtr->sectorsPerStripeUnit) {
    742 			int     x = (startAddrWithinStripe % layoutPtr->sectorsPerStripeUnit);
    743 
    744 			/* create a second pda and copy the parity map info
    745 			 * into it */
    746 			RF_ASSERT(toAdjust->next == NULL);
    747 			new_pda = toAdjust->next = rf_AllocPhysDiskAddr();
    748 			*new_pda = *toAdjust;	/* structure assignment */
    749 			new_pda->next = NULL;
    750 
    751 			/* adjust the start sector & number of blocks for the
    752 			 * first parity pda */
    753 			toAdjust->startSector += x;
    754 			toAdjust->raidAddress += x;
    755 			toAdjust->numSector = rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr, startAddrWithinStripe) - startAddrWithinStripe;
    756 			RF_ASSERT(toAdjust->numSector != 0);
    757 
    758 			/* adjust the second pda */
    759 			new_pda->numSector = endAddress - rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, endAddress);
    760 			/* new_pda->raidAddress =
    761 			 * rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr,
    762 			 * toAdjust->raidAddress); */
    763 			RF_ASSERT(new_pda->numSector != 0);
    764 		}
    765 }
    766 
    767 /*
    768    Check if a disk has been spared or failed. If spared,
    769    redirect the I/O.
    770    If it has been failed, record it in the asm pointer.
    771    Fourth arg is whether data or parity.
    772 */
    773 void
    774 rf_ASMCheckStatus(
    775     RF_Raid_t * raidPtr,
    776     RF_PhysDiskAddr_t * pda_p,
    777     RF_AccessStripeMap_t * asm_p,
    778     RF_RaidDisk_t ** disks,
    779     int parity)
    780 {
    781 	RF_DiskStatus_t dstatus;
    782 	RF_RowCol_t frow, fcol;
    783 
    784 	dstatus = disks[pda_p->row][pda_p->col].status;
    785 
    786 	if (dstatus == rf_ds_spared) {
    787 		/* if the disk has been spared, redirect access to the spare */
    788 		frow = pda_p->row;
    789 		fcol = pda_p->col;
    790 		pda_p->row = disks[frow][fcol].spareRow;
    791 		pda_p->col = disks[frow][fcol].spareCol;
    792 	} else
    793 		if (dstatus == rf_ds_dist_spared) {
    794 			/* ditto if disk has been spared to dist spare space */
    795 #if RF_DEBUG_MAP
    796 			RF_RowCol_t or = pda_p->row, oc = pda_p->col;
    797 			RF_SectorNum_t oo = pda_p->startSector;
    798 #endif
    799 			if (pda_p->type == RF_PDA_TYPE_DATA)
    800 				raidPtr->Layout.map->MapSector(raidPtr, pda_p->raidAddress, &pda_p->row, &pda_p->col, &pda_p->startSector, RF_REMAP);
    801 			else
    802 				raidPtr->Layout.map->MapParity(raidPtr, pda_p->raidAddress, &pda_p->row, &pda_p->col, &pda_p->startSector, RF_REMAP);
    803 
    804 #if RF_DEBUG_MAP
    805 			if (rf_mapDebug) {
    806 				printf("Redirected r %d c %d o %d -> r%d c %d o %d\n", or, oc, (int) oo,
    807 				    pda_p->row, pda_p->col, (int) pda_p->startSector);
    808 			}
    809 #endif
    810 		} else
    811 			if (RF_DEAD_DISK(dstatus)) {
    812 				/* if the disk is inaccessible, mark the
    813 				 * failure */
    814 				if (parity)
    815 					asm_p->numParityFailed++;
    816 				else {
    817 					asm_p->numDataFailed++;
    818 				}
    819 				asm_p->failedPDAs[asm_p->numFailedPDAs] = pda_p;
    820 				asm_p->numFailedPDAs++;
    821 #if 0
    822 				switch (asm_p->numParityFailed + asm_p->numDataFailed) {
    823 				case 1:
    824 					asm_p->failedPDAs[0] = pda_p;
    825 					break;
    826 				case 2:
    827 					asm_p->failedPDAs[1] = pda_p;
    828 				default:
    829 					break;
    830 				}
    831 #endif
    832 			}
    833 	/* the redirected access should never span a stripe unit boundary */
    834 	RF_ASSERT(rf_RaidAddressToStripeUnitID(&raidPtr->Layout, pda_p->raidAddress) ==
    835 	    rf_RaidAddressToStripeUnitID(&raidPtr->Layout, pda_p->raidAddress + pda_p->numSector - 1));
    836 	RF_ASSERT(pda_p->col != -1);
    837 }
    838