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