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