Home | History | Annotate | Line # | Download | only in raidframe
rf_map.c revision 1.6
      1  1.6  thorpej /*	$NetBSD: rf_map.c,v 1.6 2001/07/18 06:45:33 thorpej Exp $	*/
      2  1.1    oster /*
      3  1.1    oster  * Copyright (c) 1995 Carnegie-Mellon University.
      4  1.1    oster  * All rights reserved.
      5  1.1    oster  *
      6  1.1    oster  * Author: Mark Holland
      7  1.1    oster  *
      8  1.1    oster  * Permission to use, copy, modify and distribute this software and
      9  1.1    oster  * its documentation is hereby granted, provided that both the copyright
     10  1.1    oster  * notice and this permission notice appear in all copies of the
     11  1.1    oster  * software, derivative works or modified versions, and any portions
     12  1.1    oster  * thereof, and that both notices appear in supporting documentation.
     13  1.1    oster  *
     14  1.1    oster  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  1.1    oster  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16  1.1    oster  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  1.1    oster  *
     18  1.1    oster  * Carnegie Mellon requests users of this software to return to
     19  1.1    oster  *
     20  1.1    oster  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  1.1    oster  *  School of Computer Science
     22  1.1    oster  *  Carnegie Mellon University
     23  1.1    oster  *  Pittsburgh PA 15213-3890
     24  1.1    oster  *
     25  1.1    oster  * any improvements or extensions that they make and grant Carnegie the
     26  1.1    oster  * rights to redistribute these changes.
     27  1.1    oster  */
     28  1.1    oster 
     29  1.1    oster /**************************************************************************
     30  1.1    oster  *
     31  1.1    oster  * map.c -- main code for mapping RAID addresses to physical disk addresses
     32  1.1    oster  *
     33  1.1    oster  **************************************************************************/
     34  1.1    oster 
     35  1.1    oster #include "rf_types.h"
     36  1.1    oster #include "rf_threadstuff.h"
     37  1.1    oster #include "rf_raid.h"
     38  1.1    oster #include "rf_general.h"
     39  1.1    oster #include "rf_map.h"
     40  1.1    oster #include "rf_freelist.h"
     41  1.1    oster #include "rf_shutdown.h"
     42  1.1    oster 
     43  1.3    oster static void rf_FreePDAList(RF_PhysDiskAddr_t * start, RF_PhysDiskAddr_t * end, int count);
     44  1.3    oster static void
     45  1.3    oster rf_FreeASMList(RF_AccessStripeMap_t * start, RF_AccessStripeMap_t * end,
     46  1.3    oster     int count);
     47  1.1    oster 
     48  1.1    oster /*****************************************************************************************
     49  1.1    oster  *
     50  1.1    oster  * MapAccess -- main 1st order mapping routine.
     51  1.1    oster  *
     52  1.1    oster  * Maps an access in the RAID address space to the corresponding set of physical disk
     53  1.1    oster  * addresses.  The result is returned as a list of AccessStripeMap structures, one per
     54  1.1    oster  * stripe accessed.  Each ASM structure contains a pointer to a list of PhysDiskAddr
     55  1.1    oster  * structures, which describe the physical locations touched by the user access.  Note
     56  1.1    oster  * that this routine returns only static mapping information, i.e. the list of physical
     57  1.1    oster  * addresses returned does not necessarily identify the set of physical locations that
     58  1.1    oster  * will actually be read or written.
     59  1.1    oster  *
     60  1.1    oster  * The routine also maps the parity.  The physical disk location returned always
     61  1.1    oster  * indicates the entire parity unit, even when only a subset of it is being accessed.
     62  1.1    oster  * This is because an access that is not stripe unit aligned but that spans a stripe
     63  1.1    oster  * unit boundary may require access two distinct portions of the parity unit, and we
     64  1.1    oster  * can't yet tell which portion(s) we'll actually need.  We leave it up to the algorithm
     65  1.1    oster  * selection code to decide what subset of the parity unit to access.
     66  1.1    oster  *
     67  1.1    oster  * Note that addresses in the RAID address space must always be maintained as
     68  1.1    oster  * longs, instead of ints.
     69  1.1    oster  *
     70  1.1    oster  * This routine returns NULL if numBlocks is 0
     71  1.1    oster  *
     72  1.1    oster  ****************************************************************************************/
     73  1.1    oster 
     74  1.3    oster RF_AccessStripeMapHeader_t *
     75  1.3    oster rf_MapAccess(raidPtr, raidAddress, numBlocks, buffer, remap)
     76  1.3    oster 	RF_Raid_t *raidPtr;
     77  1.3    oster 	RF_RaidAddr_t raidAddress;	/* starting address in RAID address
     78  1.3    oster 					 * space */
     79  1.3    oster 	RF_SectorCount_t numBlocks;	/* number of blocks in RAID address
     80  1.3    oster 					 * space to access */
     81  1.3    oster 	caddr_t buffer;		/* buffer to supply/receive data */
     82  1.3    oster 	int     remap;		/* 1 => remap addresses to spare space */
     83  1.3    oster {
     84  1.3    oster 	RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout);
     85  1.3    oster 	RF_AccessStripeMapHeader_t *asm_hdr = NULL;
     86  1.3    oster 	RF_AccessStripeMap_t *asm_list = NULL, *asm_p = NULL;
     87  1.3    oster 	int     faultsTolerated = layoutPtr->map->faultsTolerated;
     88  1.3    oster 	RF_RaidAddr_t startAddress = raidAddress;	/* we'll change
     89  1.3    oster 							 * raidAddress along the
     90  1.3    oster 							 * way */
     91  1.3    oster 	RF_RaidAddr_t endAddress = raidAddress + numBlocks;
     92  1.3    oster 	RF_RaidDisk_t **disks = raidPtr->Disks;
     93  1.3    oster 
     94  1.3    oster 	RF_PhysDiskAddr_t *pda_p, *pda_q;
     95  1.3    oster 	RF_StripeCount_t numStripes = 0;
     96  1.3    oster 	RF_RaidAddr_t stripeRealEndAddress, stripeEndAddress, nextStripeUnitAddress;
     97  1.3    oster 	RF_RaidAddr_t startAddrWithinStripe, lastRaidAddr;
     98  1.3    oster 	RF_StripeCount_t totStripes;
     99  1.3    oster 	RF_StripeNum_t stripeID, lastSID, SUID, lastSUID;
    100  1.3    oster 	RF_AccessStripeMap_t *asmList, *t_asm;
    101  1.3    oster 	RF_PhysDiskAddr_t *pdaList, *t_pda;
    102  1.3    oster 
    103  1.3    oster 	/* allocate all the ASMs and PDAs up front */
    104  1.3    oster 	lastRaidAddr = raidAddress + numBlocks - 1;
    105  1.3    oster 	stripeID = rf_RaidAddressToStripeID(layoutPtr, raidAddress);
    106  1.3    oster 	lastSID = rf_RaidAddressToStripeID(layoutPtr, lastRaidAddr);
    107  1.3    oster 	totStripes = lastSID - stripeID + 1;
    108  1.3    oster 	SUID = rf_RaidAddressToStripeUnitID(layoutPtr, raidAddress);
    109  1.3    oster 	lastSUID = rf_RaidAddressToStripeUnitID(layoutPtr, lastRaidAddr);
    110  1.3    oster 
    111  1.3    oster 	asmList = rf_AllocASMList(totStripes);
    112  1.3    oster 	pdaList = rf_AllocPDAList(lastSUID - SUID + 1 + faultsTolerated * totStripes);	/* may also need pda(s)
    113  1.3    oster 											 * per stripe for parity */
    114  1.3    oster 
    115  1.3    oster 	if (raidAddress + numBlocks > raidPtr->totalSectors) {
    116  1.3    oster 		RF_ERRORMSG1("Unable to map access because offset (%d) was invalid\n",
    117  1.3    oster 		    (int) raidAddress);
    118  1.3    oster 		return (NULL);
    119  1.3    oster 	}
    120  1.3    oster 	if (rf_mapDebug)
    121  1.3    oster 		rf_PrintRaidAddressInfo(raidPtr, raidAddress, numBlocks);
    122  1.3    oster 	for (; raidAddress < endAddress;) {
    123  1.3    oster 		/* make the next stripe structure */
    124  1.3    oster 		RF_ASSERT(asmList);
    125  1.3    oster 		t_asm = asmList;
    126  1.3    oster 		asmList = asmList->next;
    127  1.6  thorpej 		memset((char *) t_asm, 0, sizeof(RF_AccessStripeMap_t));
    128  1.3    oster 		if (!asm_p)
    129  1.3    oster 			asm_list = asm_p = t_asm;
    130  1.3    oster 		else {
    131  1.3    oster 			asm_p->next = t_asm;
    132  1.3    oster 			asm_p = asm_p->next;
    133  1.3    oster 		}
    134  1.3    oster 		numStripes++;
    135  1.3    oster 
    136  1.3    oster 		/* map SUs from current location to the end of the stripe */
    137  1.3    oster 		asm_p->stripeID =	/* rf_RaidAddressToStripeID(layoutPtr,
    138  1.3    oster 		        raidAddress) */ stripeID++;
    139  1.3    oster 		stripeRealEndAddress = rf_RaidAddressOfNextStripeBoundary(layoutPtr, raidAddress);
    140  1.3    oster 		stripeEndAddress = RF_MIN(endAddress, stripeRealEndAddress);
    141  1.3    oster 		asm_p->raidAddress = raidAddress;
    142  1.3    oster 		asm_p->endRaidAddress = stripeEndAddress;
    143  1.3    oster 
    144  1.3    oster 		/* map each stripe unit in the stripe */
    145  1.3    oster 		pda_p = NULL;
    146  1.3    oster 		startAddrWithinStripe = raidAddress;	/* Raid addr of start of
    147  1.3    oster 							 * portion of access
    148  1.3    oster 							 * that is within this
    149  1.3    oster 							 * stripe */
    150  1.3    oster 		for (; raidAddress < stripeEndAddress;) {
    151  1.3    oster 			RF_ASSERT(pdaList);
    152  1.3    oster 			t_pda = pdaList;
    153  1.3    oster 			pdaList = pdaList->next;
    154  1.6  thorpej 			memset((char *) t_pda, 0, sizeof(RF_PhysDiskAddr_t));
    155  1.3    oster 			if (!pda_p)
    156  1.3    oster 				asm_p->physInfo = pda_p = t_pda;
    157  1.3    oster 			else {
    158  1.3    oster 				pda_p->next = t_pda;
    159  1.3    oster 				pda_p = pda_p->next;
    160  1.3    oster 			}
    161  1.3    oster 
    162  1.3    oster 			pda_p->type = RF_PDA_TYPE_DATA;
    163  1.3    oster 			(layoutPtr->map->MapSector) (raidPtr, raidAddress, &(pda_p->row), &(pda_p->col), &(pda_p->startSector), remap);
    164  1.3    oster 
    165  1.3    oster 			/* mark any failures we find.  failedPDA is don't-care
    166  1.3    oster 			 * if there is more than one failure */
    167  1.3    oster 			pda_p->raidAddress = raidAddress;	/* the RAID address
    168  1.3    oster 								 * corresponding to this
    169  1.3    oster 								 * physical disk address */
    170  1.3    oster 			nextStripeUnitAddress = rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr, raidAddress);
    171  1.3    oster 			pda_p->numSector = RF_MIN(endAddress, nextStripeUnitAddress) - raidAddress;
    172  1.3    oster 			RF_ASSERT(pda_p->numSector != 0);
    173  1.3    oster 			rf_ASMCheckStatus(raidPtr, pda_p, asm_p, disks, 0);
    174  1.3    oster 			pda_p->bufPtr = buffer + rf_RaidAddressToByte(raidPtr, (raidAddress - startAddress));
    175  1.3    oster 			asm_p->totalSectorsAccessed += pda_p->numSector;
    176  1.3    oster 			asm_p->numStripeUnitsAccessed++;
    177  1.3    oster 			asm_p->origRow = pda_p->row;	/* redundant but
    178  1.3    oster 							 * harmless to do this
    179  1.3    oster 							 * in every loop
    180  1.3    oster 							 * iteration */
    181  1.3    oster 
    182  1.3    oster 			raidAddress = RF_MIN(endAddress, nextStripeUnitAddress);
    183  1.3    oster 		}
    184  1.3    oster 
    185  1.3    oster 		/* Map the parity. At this stage, the startSector and
    186  1.3    oster 		 * numSector fields for the parity unit are always set to
    187  1.3    oster 		 * indicate the entire parity unit. We may modify this after
    188  1.3    oster 		 * mapping the data portion. */
    189  1.3    oster 		switch (faultsTolerated) {
    190  1.3    oster 		case 0:
    191  1.3    oster 			break;
    192  1.3    oster 		case 1:	/* single fault tolerant */
    193  1.3    oster 			RF_ASSERT(pdaList);
    194  1.3    oster 			t_pda = pdaList;
    195  1.3    oster 			pdaList = pdaList->next;
    196  1.6  thorpej 			memset((char *) t_pda, 0, sizeof(RF_PhysDiskAddr_t));
    197  1.3    oster 			pda_p = asm_p->parityInfo = t_pda;
    198  1.3    oster 			pda_p->type = RF_PDA_TYPE_PARITY;
    199  1.3    oster 			(layoutPtr->map->MapParity) (raidPtr, rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe),
    200  1.3    oster 			    &(pda_p->row), &(pda_p->col), &(pda_p->startSector), remap);
    201  1.3    oster 			pda_p->numSector = layoutPtr->sectorsPerStripeUnit;
    202  1.3    oster 			/* raidAddr may be needed to find unit to redirect to */
    203  1.3    oster 			pda_p->raidAddress = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe);
    204  1.3    oster 			rf_ASMCheckStatus(raidPtr, pda_p, asm_p, disks, 1);
    205  1.3    oster 			rf_ASMParityAdjust(asm_p->parityInfo, startAddrWithinStripe, endAddress, layoutPtr, asm_p);
    206  1.3    oster 
    207  1.3    oster 			break;
    208  1.3    oster 		case 2:	/* two fault tolerant */
    209  1.3    oster 			RF_ASSERT(pdaList && pdaList->next);
    210  1.3    oster 			t_pda = pdaList;
    211  1.3    oster 			pdaList = pdaList->next;
    212  1.6  thorpej 			memset((char *) t_pda, 0, sizeof(RF_PhysDiskAddr_t));
    213  1.3    oster 			pda_p = asm_p->parityInfo = t_pda;
    214  1.3    oster 			pda_p->type = RF_PDA_TYPE_PARITY;
    215  1.3    oster 			t_pda = pdaList;
    216  1.3    oster 			pdaList = pdaList->next;
    217  1.6  thorpej 			memset((char *) t_pda, 0, sizeof(RF_PhysDiskAddr_t));
    218  1.3    oster 			pda_q = asm_p->qInfo = t_pda;
    219  1.3    oster 			pda_q->type = RF_PDA_TYPE_Q;
    220  1.3    oster 			(layoutPtr->map->MapParity) (raidPtr, rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe),
    221  1.3    oster 			    &(pda_p->row), &(pda_p->col), &(pda_p->startSector), remap);
    222  1.3    oster 			(layoutPtr->map->MapQ) (raidPtr, rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe),
    223  1.3    oster 			    &(pda_q->row), &(pda_q->col), &(pda_q->startSector), remap);
    224  1.3    oster 			pda_q->numSector = pda_p->numSector = layoutPtr->sectorsPerStripeUnit;
    225  1.3    oster 			/* raidAddr may be needed to find unit to redirect to */
    226  1.3    oster 			pda_p->raidAddress = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe);
    227  1.3    oster 			pda_q->raidAddress = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, startAddrWithinStripe);
    228  1.3    oster 			/* failure mode stuff */
    229  1.3    oster 			rf_ASMCheckStatus(raidPtr, pda_p, asm_p, disks, 1);
    230  1.3    oster 			rf_ASMCheckStatus(raidPtr, pda_q, asm_p, disks, 1);
    231  1.3    oster 			rf_ASMParityAdjust(asm_p->parityInfo, startAddrWithinStripe, endAddress, layoutPtr, asm_p);
    232  1.3    oster 			rf_ASMParityAdjust(asm_p->qInfo, startAddrWithinStripe, endAddress, layoutPtr, asm_p);
    233  1.3    oster 			break;
    234  1.3    oster 		}
    235  1.3    oster 	}
    236  1.3    oster 	RF_ASSERT(asmList == NULL && pdaList == NULL);
    237  1.3    oster 	/* make the header structure */
    238  1.3    oster 	asm_hdr = rf_AllocAccessStripeMapHeader();
    239  1.3    oster 	RF_ASSERT(numStripes == totStripes);
    240  1.3    oster 	asm_hdr->numStripes = numStripes;
    241  1.3    oster 	asm_hdr->stripeMap = asm_list;
    242  1.3    oster 
    243  1.3    oster 	if (rf_mapDebug)
    244  1.3    oster 		rf_PrintAccessStripeMap(asm_hdr);
    245  1.3    oster 	return (asm_hdr);
    246  1.1    oster }
    247  1.1    oster /*****************************************************************************************
    248  1.1    oster  * This routine walks through an ASM list and marks the PDAs that have failed.
    249  1.1    oster  * It's called only when a disk failure causes an in-flight DAG to fail.
    250  1.1    oster  * The parity may consist of two components, but we want to use only one failedPDA
    251  1.1    oster  * pointer.  Thus we set failedPDA to point to the first parity component, and rely
    252  1.1    oster  * on the rest of the code to do the right thing with this.
    253  1.1    oster  ****************************************************************************************/
    254  1.1    oster 
    255  1.3    oster void
    256  1.3    oster rf_MarkFailuresInASMList(raidPtr, asm_h)
    257  1.3    oster 	RF_Raid_t *raidPtr;
    258  1.3    oster 	RF_AccessStripeMapHeader_t *asm_h;
    259  1.3    oster {
    260  1.3    oster 	RF_RaidDisk_t **disks = raidPtr->Disks;
    261  1.3    oster 	RF_AccessStripeMap_t *asmap;
    262  1.3    oster 	RF_PhysDiskAddr_t *pda;
    263  1.3    oster 
    264  1.3    oster 	for (asmap = asm_h->stripeMap; asmap; asmap = asmap->next) {
    265  1.3    oster 		asmap->numDataFailed = asmap->numParityFailed = asmap->numQFailed = 0;
    266  1.3    oster 		asmap->numFailedPDAs = 0;
    267  1.6  thorpej 		memset((char *) asmap->failedPDAs, 0,
    268  1.3    oster 		    RF_MAX_FAILED_PDA * sizeof(RF_PhysDiskAddr_t *));
    269  1.3    oster 		for (pda = asmap->physInfo; pda; pda = pda->next) {
    270  1.3    oster 			if (RF_DEAD_DISK(disks[pda->row][pda->col].status)) {
    271  1.3    oster 				asmap->numDataFailed++;
    272  1.3    oster 				asmap->failedPDAs[asmap->numFailedPDAs] = pda;
    273  1.3    oster 				asmap->numFailedPDAs++;
    274  1.3    oster 			}
    275  1.3    oster 		}
    276  1.3    oster 		pda = asmap->parityInfo;
    277  1.3    oster 		if (pda && RF_DEAD_DISK(disks[pda->row][pda->col].status)) {
    278  1.3    oster 			asmap->numParityFailed++;
    279  1.3    oster 			asmap->failedPDAs[asmap->numFailedPDAs] = pda;
    280  1.3    oster 			asmap->numFailedPDAs++;
    281  1.3    oster 		}
    282  1.3    oster 		pda = asmap->qInfo;
    283  1.3    oster 		if (pda && RF_DEAD_DISK(disks[pda->row][pda->col].status)) {
    284  1.3    oster 			asmap->numQFailed++;
    285  1.3    oster 			asmap->failedPDAs[asmap->numFailedPDAs] = pda;
    286  1.3    oster 			asmap->numFailedPDAs++;
    287  1.3    oster 		}
    288  1.3    oster 	}
    289  1.1    oster }
    290  1.1    oster /*****************************************************************************************
    291  1.1    oster  *
    292  1.1    oster  * DuplicateASM -- duplicates an ASM and returns the new one
    293  1.1    oster  *
    294  1.1    oster  ****************************************************************************************/
    295  1.3    oster RF_AccessStripeMap_t *
    296  1.3    oster rf_DuplicateASM(asmap)
    297  1.3    oster 	RF_AccessStripeMap_t *asmap;
    298  1.3    oster {
    299  1.3    oster 	RF_AccessStripeMap_t *new_asm;
    300  1.3    oster 	RF_PhysDiskAddr_t *pda, *new_pda, *t_pda;
    301  1.3    oster 
    302  1.3    oster 	new_pda = NULL;
    303  1.3    oster 	new_asm = rf_AllocAccessStripeMapComponent();
    304  1.3    oster 	bcopy((char *) asmap, (char *) new_asm, sizeof(RF_AccessStripeMap_t));
    305  1.3    oster 	new_asm->numFailedPDAs = 0;	/* ??? */
    306  1.3    oster 	new_asm->failedPDAs[0] = NULL;
    307  1.3    oster 	new_asm->physInfo = NULL;
    308  1.3    oster 	new_asm->parityInfo = NULL;
    309  1.3    oster 	new_asm->next = NULL;
    310  1.3    oster 
    311  1.3    oster 	for (pda = asmap->physInfo; pda; pda = pda->next) {	/* copy the physInfo
    312  1.3    oster 								 * list */
    313  1.3    oster 		t_pda = rf_AllocPhysDiskAddr();
    314  1.3    oster 		bcopy((char *) pda, (char *) t_pda, sizeof(RF_PhysDiskAddr_t));
    315  1.3    oster 		t_pda->next = NULL;
    316  1.3    oster 		if (!new_asm->physInfo) {
    317  1.3    oster 			new_asm->physInfo = t_pda;
    318  1.3    oster 			new_pda = t_pda;
    319  1.3    oster 		} else {
    320  1.3    oster 			new_pda->next = t_pda;
    321  1.3    oster 			new_pda = new_pda->next;
    322  1.3    oster 		}
    323  1.3    oster 		if (pda == asmap->failedPDAs[0])
    324  1.3    oster 			new_asm->failedPDAs[0] = t_pda;
    325  1.3    oster 	}
    326  1.3    oster 	for (pda = asmap->parityInfo; pda; pda = pda->next) {	/* copy the parityInfo
    327  1.3    oster 								 * list */
    328  1.3    oster 		t_pda = rf_AllocPhysDiskAddr();
    329  1.3    oster 		bcopy((char *) pda, (char *) t_pda, sizeof(RF_PhysDiskAddr_t));
    330  1.3    oster 		t_pda->next = NULL;
    331  1.3    oster 		if (!new_asm->parityInfo) {
    332  1.3    oster 			new_asm->parityInfo = t_pda;
    333  1.3    oster 			new_pda = t_pda;
    334  1.3    oster 		} else {
    335  1.3    oster 			new_pda->next = t_pda;
    336  1.3    oster 			new_pda = new_pda->next;
    337  1.3    oster 		}
    338  1.3    oster 		if (pda == asmap->failedPDAs[0])
    339  1.3    oster 			new_asm->failedPDAs[0] = t_pda;
    340  1.3    oster 	}
    341  1.3    oster 	return (new_asm);
    342  1.1    oster }
    343  1.1    oster /*****************************************************************************************
    344  1.1    oster  *
    345  1.1    oster  * DuplicatePDA -- duplicates a PDA and returns the new one
    346  1.1    oster  *
    347  1.1    oster  ****************************************************************************************/
    348  1.3    oster RF_PhysDiskAddr_t *
    349  1.3    oster rf_DuplicatePDA(pda)
    350  1.3    oster 	RF_PhysDiskAddr_t *pda;
    351  1.3    oster {
    352  1.3    oster 	RF_PhysDiskAddr_t *new;
    353  1.3    oster 
    354  1.3    oster 	new = rf_AllocPhysDiskAddr();
    355  1.3    oster 	bcopy((char *) pda, (char *) new, sizeof(RF_PhysDiskAddr_t));
    356  1.3    oster 	return (new);
    357  1.1    oster }
    358  1.1    oster /*****************************************************************************************
    359  1.1    oster  *
    360  1.1    oster  * routines to allocate and free list elements.  All allocation routines zero the
    361  1.1    oster  * structure before returning it.
    362  1.1    oster  *
    363  1.1    oster  * FreePhysDiskAddr is static.  It should never be called directly, because
    364  1.1    oster  * FreeAccessStripeMap takes care of freeing the PhysDiskAddr list.
    365  1.1    oster  *
    366  1.1    oster  ****************************************************************************************/
    367  1.1    oster 
    368  1.1    oster static RF_FreeList_t *rf_asmhdr_freelist;
    369  1.1    oster #define RF_MAX_FREE_ASMHDR 128
    370  1.1    oster #define RF_ASMHDR_INC       16
    371  1.1    oster #define RF_ASMHDR_INITIAL   32
    372  1.1    oster 
    373  1.1    oster static RF_FreeList_t *rf_asm_freelist;
    374  1.1    oster #define RF_MAX_FREE_ASM 192
    375  1.1    oster #define RF_ASM_INC       24
    376  1.1    oster #define RF_ASM_INITIAL   64
    377  1.1    oster 
    378  1.1    oster static RF_FreeList_t *rf_pda_freelist;
    379  1.1    oster #define RF_MAX_FREE_PDA 192
    380  1.1    oster #define RF_PDA_INC       24
    381  1.1    oster #define RF_PDA_INITIAL   64
    382  1.1    oster 
    383  1.1    oster /* called at shutdown time.  So far, all that is necessary is to release all the free lists */
    384  1.1    oster static void rf_ShutdownMapModule(void *);
    385  1.3    oster static void
    386  1.3    oster rf_ShutdownMapModule(ignored)
    387  1.3    oster 	void   *ignored;
    388  1.1    oster {
    389  1.3    oster 	RF_FREELIST_DESTROY(rf_asmhdr_freelist, next, (RF_AccessStripeMapHeader_t *));
    390  1.3    oster 	RF_FREELIST_DESTROY(rf_pda_freelist, next, (RF_PhysDiskAddr_t *));
    391  1.3    oster 	RF_FREELIST_DESTROY(rf_asm_freelist, next, (RF_AccessStripeMap_t *));
    392  1.1    oster }
    393  1.1    oster 
    394  1.3    oster int
    395  1.3    oster rf_ConfigureMapModule(listp)
    396  1.3    oster 	RF_ShutdownList_t **listp;
    397  1.1    oster {
    398  1.3    oster 	int     rc;
    399  1.1    oster 
    400  1.1    oster 	RF_FREELIST_CREATE(rf_asmhdr_freelist, RF_MAX_FREE_ASMHDR,
    401  1.3    oster 	    RF_ASMHDR_INC, sizeof(RF_AccessStripeMapHeader_t));
    402  1.1    oster 	if (rf_asmhdr_freelist == NULL) {
    403  1.3    oster 		return (ENOMEM);
    404  1.1    oster 	}
    405  1.1    oster 	RF_FREELIST_CREATE(rf_asm_freelist, RF_MAX_FREE_ASM,
    406  1.3    oster 	    RF_ASM_INC, sizeof(RF_AccessStripeMap_t));
    407  1.1    oster 	if (rf_asm_freelist == NULL) {
    408  1.3    oster 		RF_FREELIST_DESTROY(rf_asmhdr_freelist, next, (RF_AccessStripeMapHeader_t *));
    409  1.3    oster 		return (ENOMEM);
    410  1.1    oster 	}
    411  1.1    oster 	RF_FREELIST_CREATE(rf_pda_freelist, RF_MAX_FREE_PDA,
    412  1.3    oster 	    RF_PDA_INC, sizeof(RF_PhysDiskAddr_t));
    413  1.1    oster 	if (rf_pda_freelist == NULL) {
    414  1.3    oster 		RF_FREELIST_DESTROY(rf_asmhdr_freelist, next, (RF_AccessStripeMapHeader_t *));
    415  1.3    oster 		RF_FREELIST_DESTROY(rf_pda_freelist, next, (RF_PhysDiskAddr_t *));
    416  1.3    oster 		return (ENOMEM);
    417  1.1    oster 	}
    418  1.1    oster 	rc = rf_ShutdownCreate(listp, rf_ShutdownMapModule, NULL);
    419  1.1    oster 	if (rc) {
    420  1.1    oster 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n", __FILE__,
    421  1.3    oster 		    __LINE__, rc);
    422  1.1    oster 		rf_ShutdownMapModule(NULL);
    423  1.3    oster 		return (rc);
    424  1.1    oster 	}
    425  1.3    oster 	RF_FREELIST_PRIME(rf_asmhdr_freelist, RF_ASMHDR_INITIAL, next,
    426  1.3    oster 	    (RF_AccessStripeMapHeader_t *));
    427  1.3    oster 	RF_FREELIST_PRIME(rf_asm_freelist, RF_ASM_INITIAL, next,
    428  1.3    oster 	    (RF_AccessStripeMap_t *));
    429  1.3    oster 	RF_FREELIST_PRIME(rf_pda_freelist, RF_PDA_INITIAL, next,
    430  1.3    oster 	    (RF_PhysDiskAddr_t *));
    431  1.1    oster 
    432  1.3    oster 	return (0);
    433  1.1    oster }
    434  1.1    oster 
    435  1.3    oster RF_AccessStripeMapHeader_t *
    436  1.3    oster rf_AllocAccessStripeMapHeader()
    437  1.1    oster {
    438  1.1    oster 	RF_AccessStripeMapHeader_t *p;
    439  1.1    oster 
    440  1.3    oster 	RF_FREELIST_GET(rf_asmhdr_freelist, p, next, (RF_AccessStripeMapHeader_t *));
    441  1.6  thorpej 	memset((char *) p, 0, sizeof(RF_AccessStripeMapHeader_t));
    442  1.1    oster 
    443  1.3    oster 	return (p);
    444  1.1    oster }
    445  1.1    oster 
    446  1.1    oster 
    447  1.3    oster void
    448  1.3    oster rf_FreeAccessStripeMapHeader(p)
    449  1.3    oster 	RF_AccessStripeMapHeader_t *p;
    450  1.1    oster {
    451  1.3    oster 	RF_FREELIST_FREE(rf_asmhdr_freelist, p, next);
    452  1.1    oster }
    453  1.1    oster 
    454  1.3    oster RF_PhysDiskAddr_t *
    455  1.3    oster rf_AllocPhysDiskAddr()
    456  1.1    oster {
    457  1.1    oster 	RF_PhysDiskAddr_t *p;
    458  1.1    oster 
    459  1.3    oster 	RF_FREELIST_GET(rf_pda_freelist, p, next, (RF_PhysDiskAddr_t *));
    460  1.6  thorpej 	memset((char *) p, 0, sizeof(RF_PhysDiskAddr_t));
    461  1.1    oster 
    462  1.3    oster 	return (p);
    463  1.1    oster }
    464  1.1    oster /* allocates a list of PDAs, locking the free list only once
    465  1.1    oster  * when we have to call calloc, we do it one component at a time to simplify
    466  1.1    oster  * the process of freeing the list at program shutdown.  This should not be
    467  1.1    oster  * much of a performance hit, because it should be very infrequently executed.
    468  1.1    oster  */
    469  1.3    oster RF_PhysDiskAddr_t *
    470  1.3    oster rf_AllocPDAList(count)
    471  1.3    oster 	int     count;
    472  1.1    oster {
    473  1.1    oster 	RF_PhysDiskAddr_t *p = NULL;
    474  1.1    oster 
    475  1.3    oster 	RF_FREELIST_GET_N(rf_pda_freelist, p, next, (RF_PhysDiskAddr_t *), count);
    476  1.3    oster 	return (p);
    477  1.1    oster }
    478  1.1    oster 
    479  1.3    oster void
    480  1.3    oster rf_FreePhysDiskAddr(p)
    481  1.3    oster 	RF_PhysDiskAddr_t *p;
    482  1.1    oster {
    483  1.3    oster 	RF_FREELIST_FREE(rf_pda_freelist, p, next);
    484  1.1    oster }
    485  1.1    oster 
    486  1.3    oster static void
    487  1.3    oster rf_FreePDAList(l_start, l_end, count)
    488  1.3    oster 	RF_PhysDiskAddr_t *l_start, *l_end;	/* pointers to start and end
    489  1.3    oster 						 * of list */
    490  1.3    oster 	int     count;		/* number of elements in list */
    491  1.1    oster {
    492  1.3    oster 	RF_FREELIST_FREE_N(rf_pda_freelist, l_start, next, (RF_PhysDiskAddr_t *), count);
    493  1.1    oster }
    494  1.1    oster 
    495  1.3    oster RF_AccessStripeMap_t *
    496  1.3    oster rf_AllocAccessStripeMapComponent()
    497  1.1    oster {
    498  1.1    oster 	RF_AccessStripeMap_t *p;
    499  1.1    oster 
    500  1.3    oster 	RF_FREELIST_GET(rf_asm_freelist, p, next, (RF_AccessStripeMap_t *));
    501  1.6  thorpej 	memset((char *) p, 0, sizeof(RF_AccessStripeMap_t));
    502  1.1    oster 
    503  1.3    oster 	return (p);
    504  1.1    oster }
    505  1.1    oster /* this is essentially identical to AllocPDAList.  I should combine the two.
    506  1.1    oster  * when we have to call calloc, we do it one component at a time to simplify
    507  1.1    oster  * the process of freeing the list at program shutdown.  This should not be
    508  1.1    oster  * much of a performance hit, because it should be very infrequently executed.
    509  1.1    oster  */
    510  1.3    oster RF_AccessStripeMap_t *
    511  1.3    oster rf_AllocASMList(count)
    512  1.3    oster 	int     count;
    513  1.1    oster {
    514  1.1    oster 	RF_AccessStripeMap_t *p = NULL;
    515  1.1    oster 
    516  1.3    oster 	RF_FREELIST_GET_N(rf_asm_freelist, p, next, (RF_AccessStripeMap_t *), count);
    517  1.3    oster 	return (p);
    518  1.1    oster }
    519  1.1    oster 
    520  1.3    oster void
    521  1.3    oster rf_FreeAccessStripeMapComponent(p)
    522  1.3    oster 	RF_AccessStripeMap_t *p;
    523  1.1    oster {
    524  1.3    oster 	RF_FREELIST_FREE(rf_asm_freelist, p, next);
    525  1.1    oster }
    526  1.1    oster 
    527  1.3    oster static void
    528  1.3    oster rf_FreeASMList(l_start, l_end, count)
    529  1.3    oster 	RF_AccessStripeMap_t *l_start, *l_end;
    530  1.3    oster 	int     count;
    531  1.3    oster {
    532  1.3    oster 	RF_FREELIST_FREE_N(rf_asm_freelist, l_start, next, (RF_AccessStripeMap_t *), count);
    533  1.3    oster }
    534  1.3    oster 
    535  1.3    oster void
    536  1.3    oster rf_FreeAccessStripeMap(hdr)
    537  1.3    oster 	RF_AccessStripeMapHeader_t *hdr;
    538  1.3    oster {
    539  1.3    oster 	RF_AccessStripeMap_t *p, *pt = NULL;
    540  1.3    oster 	RF_PhysDiskAddr_t *pdp, *trailer, *pdaList = NULL, *pdaEnd = NULL;
    541  1.3    oster 	int     count = 0, t, asm_count = 0;
    542  1.3    oster 
    543  1.3    oster 	for (p = hdr->stripeMap; p; p = p->next) {
    544  1.3    oster 
    545  1.3    oster 		/* link the 3 pda lists into the accumulating pda list */
    546  1.3    oster 
    547  1.3    oster 		if (!pdaList)
    548  1.3    oster 			pdaList = p->qInfo;
    549  1.3    oster 		else
    550  1.3    oster 			pdaEnd->next = p->qInfo;
    551  1.3    oster 		for (trailer = NULL, pdp = p->qInfo; pdp;) {
    552  1.3    oster 			trailer = pdp;
    553  1.3    oster 			pdp = pdp->next;
    554  1.3    oster 			count++;
    555  1.3    oster 		}
    556  1.3    oster 		if (trailer)
    557  1.3    oster 			pdaEnd = trailer;
    558  1.3    oster 
    559  1.3    oster 		if (!pdaList)
    560  1.3    oster 			pdaList = p->parityInfo;
    561  1.3    oster 		else
    562  1.3    oster 			pdaEnd->next = p->parityInfo;
    563  1.3    oster 		for (trailer = NULL, pdp = p->parityInfo; pdp;) {
    564  1.3    oster 			trailer = pdp;
    565  1.3    oster 			pdp = pdp->next;
    566  1.3    oster 			count++;
    567  1.3    oster 		}
    568  1.3    oster 		if (trailer)
    569  1.3    oster 			pdaEnd = trailer;
    570  1.3    oster 
    571  1.3    oster 		if (!pdaList)
    572  1.3    oster 			pdaList = p->physInfo;
    573  1.3    oster 		else
    574  1.3    oster 			pdaEnd->next = p->physInfo;
    575  1.3    oster 		for (trailer = NULL, pdp = p->physInfo; pdp;) {
    576  1.3    oster 			trailer = pdp;
    577  1.3    oster 			pdp = pdp->next;
    578  1.3    oster 			count++;
    579  1.3    oster 		}
    580  1.3    oster 		if (trailer)
    581  1.3    oster 			pdaEnd = trailer;
    582  1.3    oster 
    583  1.3    oster 		pt = p;
    584  1.3    oster 		asm_count++;
    585  1.3    oster 	}
    586  1.3    oster 
    587  1.3    oster 	/* debug only */
    588  1.3    oster 	for (t = 0, pdp = pdaList; pdp; pdp = pdp->next)
    589  1.3    oster 		t++;
    590  1.3    oster 	RF_ASSERT(t == count);
    591  1.3    oster 
    592  1.3    oster 	if (pdaList)
    593  1.3    oster 		rf_FreePDAList(pdaList, pdaEnd, count);
    594  1.3    oster 	rf_FreeASMList(hdr->stripeMap, pt, asm_count);
    595  1.3    oster 	rf_FreeAccessStripeMapHeader(hdr);
    596  1.1    oster }
    597  1.1    oster /* We can't use the large write optimization if there are any failures in the stripe.
    598  1.1    oster  * In the declustered layout, there is no way to immediately determine what disks
    599  1.1    oster  * constitute a stripe, so we actually have to hunt through the stripe looking for failures.
    600  1.1    oster  * The reason we map the parity instead of just using asm->parityInfo->col is because
    601  1.1    oster  * the latter may have been already redirected to a spare drive, which would
    602  1.1    oster  * mess up the computation of the stripe offset.
    603  1.1    oster  *
    604  1.1    oster  * ASSUMES AT MOST ONE FAILURE IN THE STRIPE.
    605  1.1    oster  */
    606  1.3    oster int
    607  1.3    oster rf_CheckStripeForFailures(raidPtr, asmap)
    608  1.3    oster 	RF_Raid_t *raidPtr;
    609  1.3    oster 	RF_AccessStripeMap_t *asmap;
    610  1.3    oster {
    611  1.3    oster 	RF_RowCol_t trow, tcol, prow, pcol, *diskids, row, i;
    612  1.3    oster 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    613  1.3    oster 	RF_StripeCount_t stripeOffset;
    614  1.3    oster 	int     numFailures;
    615  1.3    oster 	RF_RaidAddr_t sosAddr;
    616  1.3    oster 	RF_SectorNum_t diskOffset, poffset;
    617  1.3    oster 	RF_RowCol_t testrow;
    618  1.3    oster 
    619  1.3    oster 	/* quick out in the fault-free case.  */
    620  1.3    oster 	RF_LOCK_MUTEX(raidPtr->mutex);
    621  1.3    oster 	numFailures = raidPtr->numFailures;
    622  1.3    oster 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    623  1.3    oster 	if (numFailures == 0)
    624  1.3    oster 		return (0);
    625  1.3    oster 
    626  1.3    oster 	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, asmap->raidAddress);
    627  1.3    oster 	row = asmap->physInfo->row;
    628  1.3    oster 	(layoutPtr->map->IdentifyStripe) (raidPtr, asmap->raidAddress, &diskids, &testrow);
    629  1.3    oster 	(layoutPtr->map->MapParity) (raidPtr, asmap->raidAddress, &prow, &pcol, &poffset, 0);	/* get pcol */
    630  1.3    oster 
    631  1.3    oster 	/* this need not be true if we've redirected the access to a spare in
    632  1.3    oster 	 * another row RF_ASSERT(row == testrow); */
    633  1.3    oster 	stripeOffset = 0;
    634  1.3    oster 	for (i = 0; i < layoutPtr->numDataCol + layoutPtr->numParityCol; i++) {
    635  1.3    oster 		if (diskids[i] != pcol) {
    636  1.3    oster 			if (RF_DEAD_DISK(raidPtr->Disks[testrow][diskids[i]].status)) {
    637  1.3    oster 				if (raidPtr->status[testrow] != rf_rs_reconstructing)
    638  1.3    oster 					return (1);
    639  1.3    oster 				RF_ASSERT(raidPtr->reconControl[testrow]->fcol == diskids[i]);
    640  1.3    oster 				layoutPtr->map->MapSector(raidPtr,
    641  1.3    oster 				    sosAddr + stripeOffset * layoutPtr->sectorsPerStripeUnit,
    642  1.3    oster 				    &trow, &tcol, &diskOffset, 0);
    643  1.3    oster 				RF_ASSERT((trow == testrow) && (tcol == diskids[i]));
    644  1.3    oster 				if (!rf_CheckRUReconstructed(raidPtr->reconControl[testrow]->reconMap, diskOffset))
    645  1.3    oster 					return (1);
    646  1.3    oster 				asmap->flags |= RF_ASM_REDIR_LARGE_WRITE;
    647  1.3    oster 				return (0);
    648  1.3    oster 			}
    649  1.3    oster 			stripeOffset++;
    650  1.3    oster 		}
    651  1.3    oster 	}
    652  1.3    oster 	return (0);
    653  1.1    oster }
    654  1.1    oster /*
    655  1.1    oster    return the number of failed data units in the stripe.
    656  1.1    oster */
    657  1.1    oster 
    658  1.3    oster int
    659  1.3    oster rf_NumFailedDataUnitsInStripe(raidPtr, asmap)
    660  1.3    oster 	RF_Raid_t *raidPtr;
    661  1.3    oster 	RF_AccessStripeMap_t *asmap;
    662  1.3    oster {
    663  1.3    oster 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    664  1.3    oster 	RF_RowCol_t trow, tcol, row, i;
    665  1.3    oster 	RF_SectorNum_t diskOffset;
    666  1.3    oster 	RF_RaidAddr_t sosAddr;
    667  1.3    oster 	int     numFailures;
    668  1.3    oster 
    669  1.3    oster 	/* quick out in the fault-free case.  */
    670  1.3    oster 	RF_LOCK_MUTEX(raidPtr->mutex);
    671  1.3    oster 	numFailures = raidPtr->numFailures;
    672  1.3    oster 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    673  1.3    oster 	if (numFailures == 0)
    674  1.3    oster 		return (0);
    675  1.3    oster 	numFailures = 0;
    676  1.3    oster 
    677  1.3    oster 	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, asmap->raidAddress);
    678  1.3    oster 	row = asmap->physInfo->row;
    679  1.3    oster 	for (i = 0; i < layoutPtr->numDataCol; i++) {
    680  1.3    oster 		(layoutPtr->map->MapSector) (raidPtr, sosAddr + i * layoutPtr->sectorsPerStripeUnit,
    681  1.3    oster 		    &trow, &tcol, &diskOffset, 0);
    682  1.3    oster 		if (RF_DEAD_DISK(raidPtr->Disks[trow][tcol].status))
    683  1.3    oster 			numFailures++;
    684  1.3    oster 	}
    685  1.1    oster 
    686  1.3    oster 	return numFailures;
    687  1.1    oster }
    688  1.1    oster 
    689  1.1    oster 
    690  1.1    oster /*****************************************************************************************
    691  1.1    oster  *
    692  1.1    oster  * debug routines
    693  1.1    oster  *
    694  1.1    oster  ****************************************************************************************/
    695  1.1    oster 
    696  1.3    oster void
    697  1.3    oster rf_PrintAccessStripeMap(asm_h)
    698  1.3    oster 	RF_AccessStripeMapHeader_t *asm_h;
    699  1.1    oster {
    700  1.3    oster 	rf_PrintFullAccessStripeMap(asm_h, 0);
    701  1.1    oster }
    702  1.1    oster 
    703  1.3    oster void
    704  1.3    oster rf_PrintFullAccessStripeMap(asm_h, prbuf)
    705  1.3    oster 	RF_AccessStripeMapHeader_t *asm_h;
    706  1.3    oster 	int     prbuf;		/* flag to print buffer pointers */
    707  1.3    oster {
    708  1.3    oster 	int     i;
    709  1.3    oster 	RF_AccessStripeMap_t *asmap = asm_h->stripeMap;
    710  1.3    oster 	RF_PhysDiskAddr_t *p;
    711  1.3    oster 	printf("%d stripes total\n", (int) asm_h->numStripes);
    712  1.3    oster 	for (; asmap; asmap = asmap->next) {
    713  1.3    oster 		/* printf("Num failures: %d\n",asmap->numDataFailed); */
    714  1.3    oster 		/* printf("Num sectors:
    715  1.3    oster 		 * %d\n",(int)asmap->totalSectorsAccessed); */
    716  1.3    oster 		printf("Stripe %d (%d sectors), failures: %d data, %d parity: ",
    717  1.3    oster 		    (int) asmap->stripeID,
    718  1.3    oster 		    (int) asmap->totalSectorsAccessed,
    719  1.3    oster 		    (int) asmap->numDataFailed,
    720  1.3    oster 		    (int) asmap->numParityFailed);
    721  1.3    oster 		if (asmap->parityInfo) {
    722  1.3    oster 			printf("Parity [r%d c%d s%d-%d", asmap->parityInfo->row, asmap->parityInfo->col,
    723  1.3    oster 			    (int) asmap->parityInfo->startSector,
    724  1.3    oster 			    (int) (asmap->parityInfo->startSector +
    725  1.3    oster 				asmap->parityInfo->numSector - 1));
    726  1.3    oster 			if (prbuf)
    727  1.3    oster 				printf(" b0x%lx", (unsigned long) asmap->parityInfo->bufPtr);
    728  1.3    oster 			if (asmap->parityInfo->next) {
    729  1.3    oster 				printf(", r%d c%d s%d-%d", asmap->parityInfo->next->row,
    730  1.3    oster 				    asmap->parityInfo->next->col,
    731  1.3    oster 				    (int) asmap->parityInfo->next->startSector,
    732  1.3    oster 				    (int) (asmap->parityInfo->next->startSector +
    733  1.3    oster 					asmap->parityInfo->next->numSector - 1));
    734  1.3    oster 				if (prbuf)
    735  1.3    oster 					printf(" b0x%lx", (unsigned long) asmap->parityInfo->next->bufPtr);
    736  1.3    oster 				RF_ASSERT(asmap->parityInfo->next->next == NULL);
    737  1.3    oster 			}
    738  1.3    oster 			printf("]\n\t");
    739  1.3    oster 		}
    740  1.3    oster 		for (i = 0, p = asmap->physInfo; p; p = p->next, i++) {
    741  1.3    oster 			printf("SU r%d c%d s%d-%d ", p->row, p->col, (int) p->startSector,
    742  1.3    oster 			    (int) (p->startSector + p->numSector - 1));
    743  1.3    oster 			if (prbuf)
    744  1.3    oster 				printf("b0x%lx ", (unsigned long) p->bufPtr);
    745  1.3    oster 			if (i && !(i & 1))
    746  1.3    oster 				printf("\n\t");
    747  1.3    oster 		}
    748  1.3    oster 		printf("\n");
    749  1.3    oster 		p = asm_h->stripeMap->failedPDAs[0];
    750  1.3    oster 		if (asm_h->stripeMap->numDataFailed + asm_h->stripeMap->numParityFailed > 1)
    751  1.3    oster 			printf("[multiple failures]\n");
    752  1.3    oster 		else
    753  1.3    oster 			if (asm_h->stripeMap->numDataFailed + asm_h->stripeMap->numParityFailed > 0)
    754  1.3    oster 				printf("\t[Failed PDA: r%d c%d s%d-%d]\n", p->row, p->col,
    755  1.3    oster 				    (int) p->startSector, (int) (p->startSector + p->numSector - 1));
    756  1.3    oster 	}
    757  1.1    oster }
    758  1.1    oster 
    759  1.3    oster void
    760  1.3    oster rf_PrintRaidAddressInfo(raidPtr, raidAddr, numBlocks)
    761  1.3    oster 	RF_Raid_t *raidPtr;
    762  1.3    oster 	RF_RaidAddr_t raidAddr;
    763  1.3    oster 	RF_SectorCount_t numBlocks;
    764  1.3    oster {
    765  1.3    oster 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
    766  1.3    oster 	RF_RaidAddr_t ra, sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, raidAddr);
    767  1.3    oster 
    768  1.3    oster 	printf("Raid addrs of SU boundaries from start of stripe to end of access:\n\t");
    769  1.3    oster 	for (ra = sosAddr; ra <= raidAddr + numBlocks; ra += layoutPtr->sectorsPerStripeUnit) {
    770  1.3    oster 		printf("%d (0x%x), ", (int) ra, (int) ra);
    771  1.3    oster 	}
    772  1.3    oster 	printf("\n");
    773  1.3    oster 	printf("Offset into stripe unit: %d (0x%x)\n",
    774  1.3    oster 	    (int) (raidAddr % layoutPtr->sectorsPerStripeUnit),
    775  1.3    oster 	    (int) (raidAddr % layoutPtr->sectorsPerStripeUnit));
    776  1.3    oster }
    777  1.1    oster /*
    778  1.1    oster    given a parity descriptor and the starting address within a stripe,
    779  1.1    oster    range restrict the parity descriptor to touch only the correct stuff.
    780  1.1    oster */
    781  1.3    oster void
    782  1.3    oster rf_ASMParityAdjust(
    783  1.3    oster     RF_PhysDiskAddr_t * toAdjust,
    784  1.3    oster     RF_StripeNum_t startAddrWithinStripe,
    785  1.3    oster     RF_SectorNum_t endAddress,
    786  1.3    oster     RF_RaidLayout_t * layoutPtr,
    787  1.3    oster     RF_AccessStripeMap_t * asm_p)
    788  1.3    oster {
    789  1.3    oster 	RF_PhysDiskAddr_t *new_pda;
    790  1.3    oster 
    791  1.3    oster 	/* when we're accessing only a portion of one stripe unit, we want the
    792  1.3    oster 	 * parity descriptor to identify only the chunk of parity associated
    793  1.3    oster 	 * with the data.  When the access spans exactly one stripe unit
    794  1.3    oster 	 * boundary and is less than a stripe unit in size, it uses two
    795  1.3    oster 	 * disjoint regions of the parity unit.  When an access spans more
    796  1.3    oster 	 * than one stripe unit boundary, it uses all of the parity unit.
    797  1.3    oster 	 *
    798  1.3    oster 	 * To better handle the case where stripe units are small, we may
    799  1.3    oster 	 * eventually want to change the 2nd case so that if the SU size is
    800  1.3    oster 	 * below some threshold, we just read/write the whole thing instead of
    801  1.3    oster 	 * breaking it up into two accesses. */
    802  1.3    oster 	if (asm_p->numStripeUnitsAccessed == 1) {
    803  1.3    oster 		int     x = (startAddrWithinStripe % layoutPtr->sectorsPerStripeUnit);
    804  1.3    oster 		toAdjust->startSector += x;
    805  1.3    oster 		toAdjust->raidAddress += x;
    806  1.3    oster 		toAdjust->numSector = asm_p->physInfo->numSector;
    807  1.3    oster 		RF_ASSERT(toAdjust->numSector != 0);
    808  1.3    oster 	} else
    809  1.3    oster 		if (asm_p->numStripeUnitsAccessed == 2 && asm_p->totalSectorsAccessed < layoutPtr->sectorsPerStripeUnit) {
    810  1.3    oster 			int     x = (startAddrWithinStripe % layoutPtr->sectorsPerStripeUnit);
    811  1.3    oster 
    812  1.3    oster 			/* create a second pda and copy the parity map info
    813  1.3    oster 			 * into it */
    814  1.3    oster 			RF_ASSERT(toAdjust->next == NULL);
    815  1.3    oster 			new_pda = toAdjust->next = rf_AllocPhysDiskAddr();
    816  1.3    oster 			*new_pda = *toAdjust;	/* structure assignment */
    817  1.3    oster 			new_pda->next = NULL;
    818  1.3    oster 
    819  1.3    oster 			/* adjust the start sector & number of blocks for the
    820  1.3    oster 			 * first parity pda */
    821  1.3    oster 			toAdjust->startSector += x;
    822  1.3    oster 			toAdjust->raidAddress += x;
    823  1.3    oster 			toAdjust->numSector = rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr, startAddrWithinStripe) - startAddrWithinStripe;
    824  1.3    oster 			RF_ASSERT(toAdjust->numSector != 0);
    825  1.3    oster 
    826  1.3    oster 			/* adjust the second pda */
    827  1.3    oster 			new_pda->numSector = endAddress - rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, endAddress);
    828  1.3    oster 			/* new_pda->raidAddress =
    829  1.3    oster 			 * rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr,
    830  1.3    oster 			 * toAdjust->raidAddress); */
    831  1.3    oster 			RF_ASSERT(new_pda->numSector != 0);
    832  1.3    oster 		}
    833  1.1    oster }
    834  1.1    oster /*
    835  1.1    oster    Check if a disk has been spared or failed. If spared,
    836  1.3    oster    redirect the I/O.
    837  1.1    oster    If it has been failed, record it in the asm pointer.
    838  1.1    oster    Fourth arg is whether data or parity.
    839  1.1    oster */
    840  1.3    oster void
    841  1.3    oster rf_ASMCheckStatus(
    842  1.3    oster     RF_Raid_t * raidPtr,
    843  1.3    oster     RF_PhysDiskAddr_t * pda_p,
    844  1.3    oster     RF_AccessStripeMap_t * asm_p,
    845  1.3    oster     RF_RaidDisk_t ** disks,
    846  1.3    oster     int parity)
    847  1.3    oster {
    848  1.3    oster 	RF_DiskStatus_t dstatus;
    849  1.3    oster 	RF_RowCol_t frow, fcol;
    850  1.3    oster 
    851  1.3    oster 	dstatus = disks[pda_p->row][pda_p->col].status;
    852  1.3    oster 
    853  1.3    oster 	if (dstatus == rf_ds_spared) {
    854  1.3    oster 		/* if the disk has been spared, redirect access to the spare */
    855  1.3    oster 		frow = pda_p->row;
    856  1.3    oster 		fcol = pda_p->col;
    857  1.3    oster 		pda_p->row = disks[frow][fcol].spareRow;
    858  1.3    oster 		pda_p->col = disks[frow][fcol].spareCol;
    859  1.3    oster 	} else
    860  1.3    oster 		if (dstatus == rf_ds_dist_spared) {
    861  1.3    oster 			/* ditto if disk has been spared to dist spare space */
    862  1.3    oster 			RF_RowCol_t or = pda_p->row, oc = pda_p->col;
    863  1.3    oster 			RF_SectorNum_t oo = pda_p->startSector;
    864  1.3    oster 
    865  1.3    oster 			if (pda_p->type == RF_PDA_TYPE_DATA)
    866  1.3    oster 				raidPtr->Layout.map->MapSector(raidPtr, pda_p->raidAddress, &pda_p->row, &pda_p->col, &pda_p->startSector, RF_REMAP);
    867  1.3    oster 			else
    868  1.3    oster 				raidPtr->Layout.map->MapParity(raidPtr, pda_p->raidAddress, &pda_p->row, &pda_p->col, &pda_p->startSector, RF_REMAP);
    869  1.3    oster 
    870  1.3    oster 			if (rf_mapDebug) {
    871  1.3    oster 				printf("Redirected r %d c %d o %d -> r%d c %d o %d\n", or, oc, (int) oo,
    872  1.3    oster 				    pda_p->row, pda_p->col, (int) pda_p->startSector);
    873  1.3    oster 			}
    874  1.3    oster 		} else
    875  1.3    oster 			if (RF_DEAD_DISK(dstatus)) {
    876  1.3    oster 				/* if the disk is inaccessible, mark the
    877  1.3    oster 				 * failure */
    878  1.3    oster 				if (parity)
    879  1.3    oster 					asm_p->numParityFailed++;
    880  1.3    oster 				else {
    881  1.3    oster 					asm_p->numDataFailed++;
    882  1.1    oster #if 0
    883  1.3    oster 					/* XXX Do we really want this spewing
    884  1.3    oster 					 * out on the console? GO */
    885  1.3    oster 					printf("DATA_FAILED!\n");
    886  1.1    oster #endif
    887  1.3    oster 				}
    888  1.3    oster 				asm_p->failedPDAs[asm_p->numFailedPDAs] = pda_p;
    889  1.3    oster 				asm_p->numFailedPDAs++;
    890  1.1    oster #if 0
    891  1.3    oster 				switch (asm_p->numParityFailed + asm_p->numDataFailed) {
    892  1.3    oster 				case 1:
    893  1.3    oster 					asm_p->failedPDAs[0] = pda_p;
    894  1.3    oster 					break;
    895  1.3    oster 				case 2:
    896  1.3    oster 					asm_p->failedPDAs[1] = pda_p;
    897  1.3    oster 				default:
    898  1.3    oster 					break;
    899  1.3    oster 				}
    900  1.1    oster #endif
    901  1.3    oster 			}
    902  1.3    oster 	/* the redirected access should never span a stripe unit boundary */
    903  1.3    oster 	RF_ASSERT(rf_RaidAddressToStripeUnitID(&raidPtr->Layout, pda_p->raidAddress) ==
    904  1.3    oster 	    rf_RaidAddressToStripeUnitID(&raidPtr->Layout, pda_p->raidAddress + pda_p->numSector - 1));
    905  1.3    oster 	RF_ASSERT(pda_p->col != -1);
    906  1.1    oster }
    907