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