Home | History | Annotate | Line # | Download | only in raidframe
rf_driver.c revision 1.4
      1 /*	$NetBSD: rf_driver.c,v 1.4 1999/01/26 02:33:56 oster Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author: Mark Holland, Khalil Amiri, Claudson Bornstein, William V. Courtright II,
      7  *         Robby Findler, Daniel Stodolsky, Rachad Youssef, Jim Zelenka
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 /******************************************************************************
     31  *
     32  * rf_driver.c -- main setup, teardown, and access routines for the RAID driver
     33  *
     34  * all routines are prefixed with rf_ (raidframe), to avoid conficts.
     35  *
     36  ******************************************************************************/
     37 
     38 
     39 #include <sys/types.h>
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/fcntl.h>
     44 #include <sys/vnode.h>
     45 
     46 
     47 #include "rf_archs.h"
     48 #include "rf_threadstuff.h"
     49 
     50 #include <sys/errno.h>
     51 
     52 #include "rf_raid.h"
     53 #include "rf_dag.h"
     54 #include "rf_aselect.h"
     55 #include "rf_diskqueue.h"
     56 #include "rf_parityscan.h"
     57 #include "rf_alloclist.h"
     58 #include "rf_threadid.h"
     59 #include "rf_dagutils.h"
     60 #include "rf_utils.h"
     61 #include "rf_etimer.h"
     62 #include "rf_acctrace.h"
     63 #include "rf_configure.h"
     64 #include "rf_general.h"
     65 #include "rf_desc.h"
     66 #include "rf_states.h"
     67 #include "rf_freelist.h"
     68 #include "rf_decluster.h"
     69 #include "rf_map.h"
     70 #include "rf_diskthreads.h"
     71 #include "rf_revent.h"
     72 #include "rf_callback.h"
     73 #include "rf_engine.h"
     74 #include "rf_memchunk.h"
     75 #include "rf_mcpair.h"
     76 #include "rf_nwayxor.h"
     77 #include "rf_debugprint.h"
     78 #include "rf_copyback.h"
     79 #if !defined(__NetBSD__)
     80 #include "rf_camlayer.h"
     81 #endif
     82 #include "rf_driver.h"
     83 #include "rf_options.h"
     84 #include "rf_shutdown.h"
     85 #include "rf_sys.h"
     86 #include "rf_cpuutil.h"
     87 
     88 #include <sys/buf.h>
     89 
     90 #if DKUSAGE > 0
     91 #include <sys/dkusage.h>
     92 #include <io/common/iotypes.h>
     93 #include <io/cam/dec_cam.h>
     94 #include <io/cam/cam.h>
     95 #include <io/cam/pdrv.h>
     96 #endif /* DKUSAGE > 0 */
     97 
     98 #if RF_DEMO > 0
     99 #include "rf_demo.h"
    100 #endif /* RF_DEMO > 0 */
    101 
    102 /* rad == RF_RaidAccessDesc_t */
    103 static RF_FreeList_t *rf_rad_freelist;
    104 #define RF_MAX_FREE_RAD 128
    105 #define RF_RAD_INC       16
    106 #define RF_RAD_INITIAL   32
    107 
    108 /* debug variables */
    109 char rf_panicbuf[2048];       /* a buffer to hold an error msg when we panic */
    110 
    111 /* main configuration routines */
    112 static int raidframe_booted = 0;
    113 
    114 static void rf_ConfigureDebug(RF_Config_t *cfgPtr);
    115 static void set_debug_option(char *name, long val);
    116 static void rf_UnconfigureArray(void);
    117 static int init_rad(RF_RaidAccessDesc_t *);
    118 static void clean_rad(RF_RaidAccessDesc_t *);
    119 static void rf_ShutdownRDFreeList(void *);
    120 static int rf_ConfigureRDFreeList(RF_ShutdownList_t **);
    121 
    122 
    123 RF_DECLARE_MUTEX(rf_printf_mutex)          /* debug only:  avoids interleaved printfs by different stripes */
    124 RF_DECLARE_GLOBAL_THREADID                 /* declarations for threadid.h */
    125 
    126 
    127 #define SIGNAL_QUIESCENT_COND(_raid_)  wakeup(&((_raid_)->accesses_suspended))
    128 #define WAIT_FOR_QUIESCENCE(_raid_) \
    129 	tsleep(&((_raid_)->accesses_suspended),PRIBIO|PCATCH,"raidframe quiesce", 0);
    130 
    131 #if DKUSAGE > 0
    132 #define IO_BUF_ERR(bp, err, unit) { \
    133 	bp->b_flags |= B_ERROR; \
    134 	bp->b_resid = bp->b_bcount; \
    135 	bp->b_error = err; \
    136 	RF_DKU_END_IO(unit, bp); \
    137 	biodone(bp); \
    138 }
    139 #else
    140 #define IO_BUF_ERR(bp, err, unit) { \
    141 	bp->b_flags |= B_ERROR; \
    142 	bp->b_resid = bp->b_bcount; \
    143 	bp->b_error = err; \
    144 	RF_DKU_END_IO(unit); \
    145 	biodone(bp); \
    146 }
    147 #endif /* DKUSAGE > 0 */
    148 
    149 static int configureCount=0;         /* number of active configurations */
    150 static int isconfigged=0;            /* is basic raidframe (non per-array) stuff configged */
    151 RF_DECLARE_STATIC_MUTEX(configureMutex) /* used to lock the configuration stuff */
    152 
    153 static RF_ShutdownList_t *globalShutdown; /* non array-specific stuff */
    154 
    155 static int rf_ConfigureRDFreeList(RF_ShutdownList_t **listp);
    156 
    157 /* called at system boot time */
    158 int rf_BootRaidframe()
    159 {
    160   int rc;
    161 
    162   if (raidframe_booted)
    163     return(EBUSY);
    164   raidframe_booted = 1;
    165 
    166 #if RF_DEBUG_ATOMIC > 0
    167   rf_atent_init();
    168 #endif /* RF_DEBUG_ATOMIC > 0 */
    169 
    170   rf_setup_threadid();
    171   rf_assign_threadid();
    172 
    173   rc = rf_mutex_init(&configureMutex);
    174   if (rc) {
    175     RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    176       __LINE__, rc);
    177     RF_PANIC();
    178   }
    179   configureCount = 0;
    180   isconfigged = 0;
    181   globalShutdown = NULL;
    182   return(0);
    183 }
    184 
    185 /*
    186  * This function is really just for debugging user-level stuff: it
    187  * frees up all memory, other RAIDframe resources which might otherwise
    188  * be kept around. This is used with systems like "sentinel" to detect
    189  * memory leaks.
    190  */
    191 int rf_UnbootRaidframe()
    192 {
    193 	int rc;
    194 
    195 	RF_LOCK_MUTEX(configureMutex);
    196 	if (configureCount) {
    197 		RF_UNLOCK_MUTEX(configureMutex);
    198 		return(EBUSY);
    199 	}
    200 	raidframe_booted = 0;
    201 	RF_UNLOCK_MUTEX(configureMutex);
    202 	rc = rf_mutex_destroy(&configureMutex);
    203 	if (rc) {
    204 		RF_ERRORMSG3("Unable to destroy mutex file %s line %d rc=%d\n", __FILE__,
    205 			__LINE__, rc);
    206 		RF_PANIC();
    207 	}
    208 #if RF_DEBUG_ATOMIC > 0
    209 	rf_atent_shutdown();
    210 #endif /* RF_DEBUG_ATOMIC > 0 */
    211 	return(0);
    212 }
    213 
    214 /*
    215  * Called whenever an array is shutdown
    216  */
    217 static void rf_UnconfigureArray()
    218 {
    219   int rc;
    220 
    221   RF_LOCK_MUTEX(configureMutex);
    222   if (--configureCount == 0) {              /* if no active configurations, shut everything down */
    223     isconfigged = 0;
    224 
    225     rc = rf_ShutdownList(&globalShutdown);
    226     if (rc) {
    227       RF_ERRORMSG1("RAIDFRAME: unable to do global shutdown, rc=%d\n", rc);
    228     }
    229 
    230     rf_shutdown_threadid();
    231 
    232     /*
    233      * We must wait until now, because the AllocList module
    234      * uses the DebugMem module.
    235      */
    236     if (rf_memDebug)
    237       rf_print_unfreed();
    238   }
    239   RF_UNLOCK_MUTEX(configureMutex);
    240 }
    241 
    242 /*
    243  * Called to shut down an array.
    244  */
    245 int rf_Shutdown(raidPtr)
    246   RF_Raid_t   *raidPtr;
    247 {
    248   int r,c;
    249 
    250   struct proc *p;
    251 
    252   if (!raidPtr->valid) {
    253     RF_ERRORMSG("Attempt to shut down unconfigured RAIDframe driver.  Aborting shutdown\n");
    254     return(EINVAL);
    255   }
    256 
    257   /*
    258    * wait for outstanding IOs to land
    259    * As described in rf_raid.h, we use the rad_freelist lock
    260    * to protect the per-array info about outstanding descs
    261    * since we need to do freelist locking anyway, and this
    262    * cuts down on the amount of serialization we've got going
    263    * on.
    264    */
    265   RF_FREELIST_DO_LOCK(rf_rad_freelist);
    266   if (raidPtr->waitShutdown) {
    267     RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    268     return(EBUSY);
    269   }
    270   raidPtr->waitShutdown = 1;
    271   while (raidPtr->nAccOutstanding) {
    272     RF_WAIT_COND(raidPtr->outstandingCond, RF_FREELIST_MUTEX_OF(rf_rad_freelist));
    273   }
    274   RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    275 
    276   raidPtr->valid = 0;
    277 
    278 
    279   /* We take this opportunity to close the vnodes like we should.. */
    280 
    281   p = raidPtr->proc; /* XXX */
    282 
    283   for(r=0;r<raidPtr->numRow;r++) {
    284 	  for(c=0;c<raidPtr->numCol;c++) {
    285 		  printf("Closing vnode for row: %d col: %d\n",r,c);
    286 		  if (raidPtr->raid_cinfo[r][c].ci_vp) {
    287 			  (void)vn_close(raidPtr->raid_cinfo[r][c].ci_vp,
    288 					 FREAD|FWRITE,  p->p_ucred, p);
    289 		  } else {
    290 			  printf("vnode was NULL\n");
    291 		  }
    292 
    293 	  }
    294   }
    295   for(r=0;r<raidPtr->numSpare;r++) {
    296 	  printf("Closing vnode for spare: %d\n",r);
    297 	  if (raidPtr->raid_cinfo[0][raidPtr->numCol+r].ci_vp) {
    298 		  (void)vn_close(raidPtr->raid_cinfo[0][raidPtr->numCol+r].ci_vp,
    299 				 FREAD|FWRITE,  p->p_ucred, p);
    300 	  } else {
    301 		  printf("vnode was NULL\n");
    302 	  }
    303   }
    304 
    305 
    306 
    307   rf_ShutdownList(&raidPtr->shutdownList);
    308 
    309   rf_UnconfigureArray();
    310 
    311   return(0);
    312 }
    313 
    314 #define DO_INIT_CONFIGURE(f) { \
    315 	rc = f (&globalShutdown); \
    316 	if (rc) { \
    317 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    318 		rf_ShutdownList(&globalShutdown); \
    319 		configureCount--; \
    320 		RF_UNLOCK_MUTEX(configureMutex); \
    321 		return(rc); \
    322 	} \
    323 }
    324 
    325 #define DO_RAID_FAIL() { \
    326 	rf_ShutdownList(&raidPtr->shutdownList); \
    327 	rf_UnconfigureArray(); \
    328 }
    329 
    330 #define DO_RAID_INIT_CONFIGURE(f) { \
    331 	rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
    332 	if (rc) { \
    333 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    334 		DO_RAID_FAIL(); \
    335 		return(rc); \
    336 	} \
    337 }
    338 
    339 #define DO_RAID_MUTEX(_m_) { \
    340 	rc = rf_create_managed_mutex(&raidPtr->shutdownList, (_m_)); \
    341 	if (rc) { \
    342 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", \
    343 			__FILE__, __LINE__, rc); \
    344 		DO_RAID_FAIL(); \
    345 		return(rc); \
    346 	} \
    347 }
    348 
    349 #define DO_RAID_COND(_c_) { \
    350 	rc = rf_create_managed_cond(&raidPtr->shutdownList, (_c_)); \
    351 	if (rc) { \
    352 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", \
    353 			__FILE__, __LINE__, rc); \
    354 		DO_RAID_FAIL(); \
    355 		return(rc); \
    356 	} \
    357 }
    358 
    359 int rf_Configure(raidPtr, cfgPtr)
    360   RF_Raid_t    *raidPtr;
    361   RF_Config_t  *cfgPtr;
    362 {
    363   RF_RowCol_t row, col;
    364   int i, rc;
    365   int unit;
    366   struct proc *p;
    367 
    368   if (raidPtr->valid) {
    369     RF_ERRORMSG("RAIDframe configuration not shut down.  Aborting configure.\n");
    370     return(EINVAL);
    371   }
    372 
    373   RF_LOCK_MUTEX(configureMutex);
    374   configureCount++;
    375   if (isconfigged == 0) {
    376     rc = rf_create_managed_mutex(&globalShutdown, &rf_printf_mutex);
    377     if (rc) {
    378       RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    379         __LINE__, rc);
    380       rf_ShutdownList(&globalShutdown);
    381       return(rc);
    382     }
    383 
    384     /* initialize globals */
    385     printf("RAIDFRAME: protectedSectors is %ld\n",rf_protectedSectors);
    386 
    387     rf_clear_debug_print_buffer();
    388 
    389     DO_INIT_CONFIGURE(rf_ConfigureAllocList);
    390     DO_INIT_CONFIGURE(rf_ConfigureEtimer);
    391     /*
    392      * Yes, this does make debugging general to the whole system instead
    393      * of being array specific. Bummer, drag.
    394      */
    395     rf_ConfigureDebug(cfgPtr);
    396     DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
    397     DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
    398     DO_INIT_CONFIGURE(rf_ConfigureMapModule);
    399     DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
    400     DO_INIT_CONFIGURE(rf_ConfigureCallback);
    401     DO_INIT_CONFIGURE(rf_ConfigureMemChunk);
    402     DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
    403     DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
    404     DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
    405     DO_INIT_CONFIGURE(rf_ConfigureMCPair);
    406 #if !defined(__NetBSD__)
    407     DO_INIT_CONFIGURE(rf_ConfigureCamLayer);
    408 #endif
    409     DO_INIT_CONFIGURE(rf_ConfigureDAGs);
    410     DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
    411     DO_INIT_CONFIGURE(rf_ConfigureDebugPrint);
    412     DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
    413     DO_INIT_CONFIGURE(rf_ConfigureCopyback);
    414     DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
    415     DO_INIT_CONFIGURE(rf_ConfigureCpuMonitor);
    416     isconfigged = 1;
    417   }
    418   RF_UNLOCK_MUTEX(configureMutex);
    419 
    420   /*
    421    * Null out the entire raid descriptor to avoid problems when we reconfig.
    422    * This also clears the valid bit.
    423    */
    424   /* XXX this clearing should be moved UP to outside of here.... that, or
    425      rf_Configure() needs to take more arguments... XXX */
    426   unit = raidPtr->raidid;
    427   p = raidPtr->proc;   /* XXX save these... */
    428   bzero((char *)raidPtr, sizeof(RF_Raid_t));
    429   raidPtr->raidid = unit;
    430   raidPtr->proc = p;   /* XXX and then recover them..*/
    431   DO_RAID_MUTEX(&raidPtr->mutex);
    432   /* set up the cleanup list.  Do this after ConfigureDebug so that value of memDebug will be set */
    433 
    434   rf_MakeAllocList(raidPtr->cleanupList);
    435   if (raidPtr->cleanupList == NULL) {
    436     DO_RAID_FAIL();
    437     return(ENOMEM);
    438   }
    439 
    440   rc = rf_ShutdownCreate(&raidPtr->shutdownList,
    441 			 (void (*)(void *))rf_FreeAllocList,
    442 			 raidPtr->cleanupList);
    443   if (rc) {
    444     RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
    445       __FILE__, __LINE__, rc);
    446     DO_RAID_FAIL();
    447     return(rc);
    448   }
    449 
    450   raidPtr->numRow = cfgPtr->numRow;
    451   raidPtr->numCol = cfgPtr->numCol;
    452   raidPtr->numSpare = cfgPtr->numSpare;
    453 
    454   /* XXX we don't even pretend to support more than one row
    455      in the kernel... */
    456   if (raidPtr->numRow != 1) {
    457 	  RF_ERRORMSG("Only one row supported in kernel.\n");
    458 	  DO_RAID_FAIL();
    459 	  return(EINVAL);
    460   }
    461 
    462 
    463 
    464   RF_CallocAndAdd(raidPtr->status, raidPtr->numRow, sizeof(RF_RowStatus_t),
    465     (RF_RowStatus_t *), raidPtr->cleanupList);
    466   if (raidPtr->status == NULL) {
    467     DO_RAID_FAIL();
    468     return(ENOMEM);
    469   }
    470 
    471   RF_CallocAndAdd(raidPtr->reconControl, raidPtr->numRow,
    472     sizeof(RF_ReconCtrl_t *), (RF_ReconCtrl_t **), raidPtr->cleanupList);
    473   if (raidPtr->reconControl == NULL) {
    474     DO_RAID_FAIL();
    475     return(ENOMEM);
    476   }
    477   for (i=0; i<raidPtr->numRow; i++) {
    478     raidPtr->status[i] = rf_rs_optimal;
    479     raidPtr->reconControl[i] = NULL;
    480   }
    481 
    482   DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
    483   DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
    484 
    485   DO_RAID_COND(&raidPtr->outstandingCond);
    486 
    487   raidPtr->nAccOutstanding = 0;
    488   raidPtr->waitShutdown = 0;
    489 
    490   DO_RAID_MUTEX(&raidPtr->access_suspend_mutex);
    491   DO_RAID_COND(&raidPtr->quiescent_cond);
    492 
    493   DO_RAID_COND(&raidPtr->waitForReconCond);
    494 
    495   DO_RAID_MUTEX(&raidPtr->recon_done_proc_mutex);
    496   DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
    497   DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
    498   /* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev no. is set */
    499   DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
    500 
    501   DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
    502 
    503   DO_RAID_INIT_CONFIGURE(rf_ConfigurePSStatus);
    504 
    505   for(row=0;row<raidPtr->numRow;row++) {
    506     for(col=0;col<raidPtr->numCol;col++) {
    507       /*
    508        * XXX better distribution
    509        */
    510       raidPtr->hist_diskreq[row][col] = 0;
    511     }
    512   }
    513 
    514   if (rf_keepAccTotals) {
    515     raidPtr->keep_acc_totals = 1;
    516   }
    517 
    518   rf_StartUserStats(raidPtr);
    519 
    520   raidPtr->valid = 1;
    521   return(0);
    522 }
    523 
    524 static int init_rad(desc)
    525   RF_RaidAccessDesc_t  *desc;
    526 {
    527 	int rc;
    528 
    529 	rc = rf_mutex_init(&desc->mutex);
    530 	if (rc) {
    531 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    532 			__LINE__, rc);
    533 		return(rc);
    534 	}
    535 	rc = rf_cond_init(&desc->cond);
    536 	if (rc) {
    537 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
    538 			__LINE__, rc);
    539 		rf_mutex_destroy(&desc->mutex);
    540 		return(rc);
    541 	}
    542 	return(0);
    543 }
    544 
    545 static void clean_rad(desc)
    546   RF_RaidAccessDesc_t  *desc;
    547 {
    548 	rf_mutex_destroy(&desc->mutex);
    549 	rf_cond_destroy(&desc->cond);
    550 }
    551 
    552 static void rf_ShutdownRDFreeList(ignored)
    553   void  *ignored;
    554 {
    555 	RF_FREELIST_DESTROY_CLEAN(rf_rad_freelist,next,(RF_RaidAccessDesc_t *),clean_rad);
    556 }
    557 
    558 static int rf_ConfigureRDFreeList(listp)
    559   RF_ShutdownList_t **listp;
    560 {
    561 	int rc;
    562 
    563 	RF_FREELIST_CREATE(rf_rad_freelist, RF_MAX_FREE_RAD,
    564 		RF_RAD_INC, sizeof(RF_RaidAccessDesc_t));
    565 	if (rf_rad_freelist == NULL) {
    566 		return(ENOMEM);
    567 	}
    568 	rc = rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
    569 	if (rc) {
    570 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n", __FILE__,
    571 			__LINE__, rc);
    572 		rf_ShutdownRDFreeList(NULL);
    573 		return(rc);
    574 	}
    575 	RF_FREELIST_PRIME_INIT(rf_rad_freelist, RF_RAD_INITIAL,next,
    576 		(RF_RaidAccessDesc_t *),init_rad);
    577 	return(0);
    578 }
    579 
    580 RF_RaidAccessDesc_t *rf_AllocRaidAccDesc(
    581   RF_Raid_t                    *raidPtr,
    582   RF_IoType_t                   type,
    583   RF_RaidAddr_t                 raidAddress,
    584   RF_SectorCount_t              numBlocks,
    585   caddr_t                       bufPtr,
    586   void                         *bp,
    587   RF_DagHeader_t              **paramDAG,
    588   RF_AccessStripeMapHeader_t  **paramASM,
    589   RF_RaidAccessFlags_t          flags,
    590   void                        (*cbF)(struct buf *),
    591   void                         *cbA,
    592   RF_AccessState_t             *states)
    593 {
    594   RF_RaidAccessDesc_t *desc;
    595 
    596   RF_FREELIST_GET_INIT_NOUNLOCK(rf_rad_freelist,desc,next,(RF_RaidAccessDesc_t *),init_rad);
    597   if (raidPtr->waitShutdown) {
    598     /*
    599      * Actually, we're shutting the array down. Free the desc
    600      * and return NULL.
    601      */
    602     RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    603     RF_FREELIST_FREE_CLEAN(rf_rad_freelist,desc,next,clean_rad);
    604     return(NULL);
    605   }
    606   raidPtr->nAccOutstanding++;
    607   RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    608 
    609   desc->raidPtr     = (void*)raidPtr;
    610   desc->type        = type;
    611   desc->raidAddress = raidAddress;
    612   desc->numBlocks   = numBlocks;
    613   desc->bufPtr      = bufPtr;
    614   desc->bp          = bp;
    615   desc->paramDAG    = paramDAG;
    616   desc->paramASM    = paramASM;
    617   desc->flags       = flags;
    618   desc -> states    = states;
    619   desc -> state     = 0;
    620 
    621   desc->status      = 0;
    622   bzero((char *)&desc->tracerec, sizeof(RF_AccTraceEntry_t));
    623   desc->callbackFunc= (void (*)(RF_CBParam_t))cbF; /* XXX */
    624   desc->callbackArg = cbA;
    625   desc->next        = NULL;
    626   desc->head	    = desc;
    627   desc->numPending  = 0;
    628   desc->cleanupList = NULL;
    629   rf_MakeAllocList(desc->cleanupList);
    630   rf_get_threadid(desc->tid);
    631   return(desc);
    632 }
    633 
    634 void rf_FreeRaidAccDesc(RF_RaidAccessDesc_t *desc)
    635 {
    636   RF_Raid_t *raidPtr = desc->raidPtr;
    637 
    638   RF_ASSERT(desc);
    639 
    640   rf_FreeAllocList(desc->cleanupList);
    641   RF_FREELIST_FREE_CLEAN_NOUNLOCK(rf_rad_freelist,desc,next,clean_rad);
    642     raidPtr->nAccOutstanding--;
    643     if (raidPtr->waitShutdown) {
    644       RF_SIGNAL_COND(raidPtr->outstandingCond);
    645     }
    646   RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    647 }
    648 
    649 /*********************************************************************
    650  * Main routine for performing an access.
    651  * Accesses are retried until a DAG can not be selected.  This occurs
    652  * when either the DAG library is incomplete or there are too many
    653  * failures in a parity group.
    654  ********************************************************************/
    655 int rf_DoAccess(
    656   RF_Raid_t                    *raidPtr,
    657   RF_IoType_t                   type,
    658   int                           async_flag,
    659   RF_RaidAddr_t                 raidAddress,
    660   RF_SectorCount_t              numBlocks,
    661   caddr_t                       bufPtr,
    662   void                         *bp_in,
    663   RF_DagHeader_t              **paramDAG,
    664   RF_AccessStripeMapHeader_t  **paramASM,
    665   RF_RaidAccessFlags_t          flags,
    666   RF_RaidAccessDesc_t         **paramDesc,
    667   void                        (*cbF)(struct buf *),
    668   void                         *cbA)
    669 /*
    670 type should be read or write
    671 async_flag should be RF_TRUE or RF_FALSE
    672 bp_in is a buf pointer.  void * to facilitate ignoring it outside the kernel
    673 */
    674 {
    675   int tid;
    676   RF_RaidAccessDesc_t *desc;
    677   caddr_t lbufPtr = bufPtr;
    678   struct buf *bp = (struct buf *) bp_in;
    679 #if DFSTRACE > 0
    680   struct { RF_uint64 raidAddr; int numBlocks; char type;} dfsrecord;
    681 #endif /* DFSTRACE > 0 */
    682 
    683   raidAddress += rf_raidSectorOffset;
    684 
    685   if (!raidPtr->valid) {
    686     RF_ERRORMSG("RAIDframe driver not successfully configured.  Rejecting access.\n");
    687     IO_BUF_ERR(bp, EINVAL, raidPtr->raidid);
    688     return(EINVAL);
    689   }
    690 
    691 #if defined(KERNEL) && DFSTRACE > 0
    692   if (rf_DFSTraceAccesses) {
    693     dfsrecord.raidAddr  = raidAddress;
    694     dfsrecord.numBlocks = numBlocks;
    695     dfsrecord.type      = type;
    696     dfs_log(DFS_NOTE, (char *) &dfsrecord, sizeof(dfsrecord), 0);
    697   }
    698 #endif /* KERNEL && DFSTRACE > 0 */
    699 
    700   rf_get_threadid(tid);
    701   if (rf_accessDebug) {
    702 
    703 	  printf("logBytes is: %d %d %d\n",raidPtr->raidid,
    704 		 raidPtr->logBytesPerSector,
    705 		 (int)rf_RaidAddressToByte(raidPtr,numBlocks));
    706     printf("[%d] %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n",tid,
    707 	   (type==RF_IO_TYPE_READ) ? "READ":"WRITE", (int)raidAddress,
    708 	   (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
    709 	   (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress+numBlocks-1),
    710 	   (int) numBlocks,
    711 	   (int) rf_RaidAddressToByte(raidPtr,numBlocks),
    712 	   (long) bufPtr);
    713   }
    714 
    715   if (raidAddress + numBlocks > raidPtr->totalSectors) {
    716 
    717     printf("DoAccess: raid addr %lu too large to access %lu sectors.  Max legal addr is %lu\n",
    718            (u_long)raidAddress,(u_long)numBlocks,(u_long)raidPtr->totalSectors);
    719 
    720     if (type == RF_IO_TYPE_READ) {
    721       IO_BUF_ERR(bp, ENOSPC, raidPtr->raidid);
    722       return(ENOSPC);
    723     } else {
    724       IO_BUF_ERR(bp, ENOSPC, raidPtr->raidid);
    725       return(ENOSPC);
    726     }
    727   }
    728 
    729   desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
    730 			  numBlocks, lbufPtr, bp, paramDAG, paramASM,
    731 			  flags, cbF, cbA, raidPtr->Layout.map->states);
    732 
    733   if (desc == NULL) {
    734     return(ENOMEM);
    735   }
    736 
    737   RF_ETIMER_START(desc->tracerec.tot_timer);
    738 
    739   desc->async_flag = async_flag;
    740 
    741   rf_ContinueRaidAccess(desc);
    742 
    743   return(0);
    744 }
    745 
    746 /* force the array into reconfigured mode without doing reconstruction */
    747 int rf_SetReconfiguredMode(raidPtr, row, col)
    748   RF_Raid_t  *raidPtr;
    749   int         row;
    750   int         col;
    751 {
    752   if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
    753     printf("Can't set reconfigured mode in dedicated-spare array\n");
    754     RF_PANIC();
    755   }
    756   RF_LOCK_MUTEX(raidPtr->mutex);
    757   raidPtr->numFailures++;
    758   raidPtr->Disks[row][col].status = rf_ds_dist_spared;
    759   raidPtr->status[row] = rf_rs_reconfigured;
    760   /* install spare table only if declustering + distributed sparing architecture. */
    761   if ( raidPtr->Layout.map->flags & RF_BD_DECLUSTERED )
    762      rf_InstallSpareTable(raidPtr, row, col);
    763   RF_UNLOCK_MUTEX(raidPtr->mutex);
    764   return(0);
    765 }
    766 
    767 extern int fail_row, fail_col, fail_time;
    768 extern int delayed_recon;
    769 
    770 int rf_FailDisk(
    771   RF_Raid_t  *raidPtr,
    772   int         frow,
    773   int         fcol,
    774   int         initRecon)
    775 {
    776   int tid;
    777 
    778   rf_get_threadid(tid);
    779   printf("[%d] Failing disk r%d c%d\n",tid,frow,fcol);
    780   RF_LOCK_MUTEX(raidPtr->mutex);
    781   raidPtr->numFailures++;
    782   raidPtr->Disks[frow][fcol].status = rf_ds_failed;
    783   raidPtr->status[frow] = rf_rs_degraded;
    784   RF_UNLOCK_MUTEX(raidPtr->mutex);
    785   if (initRecon)
    786     rf_ReconstructFailedDisk(raidPtr, frow, fcol);
    787   return(0);
    788 }
    789 
    790 /* releases a thread that is waiting for the array to become quiesced.
    791  * access_suspend_mutex should be locked upon calling this
    792  */
    793 void rf_SignalQuiescenceLock(raidPtr, reconDesc)
    794   RF_Raid_t           *raidPtr;
    795   RF_RaidReconDesc_t  *reconDesc;
    796 {
    797   int tid;
    798 
    799   if (rf_quiesceDebug) {
    800     rf_get_threadid(tid);
    801     printf("[%d] Signalling quiescence lock\n", tid);
    802   }
    803   raidPtr->access_suspend_release = 1;
    804 
    805   if (raidPtr->waiting_for_quiescence) {
    806     SIGNAL_QUIESCENT_COND(raidPtr);
    807   }
    808 }
    809 
    810 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
    811 int rf_SuspendNewRequestsAndWait(raidPtr)
    812   RF_Raid_t  *raidPtr;
    813 {
    814   if (rf_quiesceDebug)
    815     printf("Suspending new reqs\n");
    816 
    817   RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    818   raidPtr->accesses_suspended++;
    819   raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
    820 
    821   if (raidPtr->waiting_for_quiescence) {
    822     raidPtr->access_suspend_release=0;
    823     while (!raidPtr->access_suspend_release) {
    824 	    printf("Suspending: Waiting for Quiesence\n");
    825       WAIT_FOR_QUIESCENCE(raidPtr);
    826       raidPtr->waiting_for_quiescence = 0;
    827     }
    828   }
    829   printf("Quiesence reached..\n");
    830 
    831   RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    832   return (raidPtr->waiting_for_quiescence);
    833 }
    834 
    835 /* wake up everyone waiting for quiescence to be released */
    836 void rf_ResumeNewRequests(raidPtr)
    837   RF_Raid_t  *raidPtr;
    838 {
    839   RF_CallbackDesc_t *t, *cb;
    840 
    841   if (rf_quiesceDebug)
    842     printf("Resuming new reqs\n");
    843 
    844   RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    845   raidPtr->accesses_suspended--;
    846   if (raidPtr->accesses_suspended == 0)
    847     cb = raidPtr->quiesce_wait_list;
    848   else
    849     cb = NULL;
    850   raidPtr->quiesce_wait_list = NULL;
    851   RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    852 
    853   while (cb) {
    854     t = cb;
    855     cb = cb->next;
    856     (t->callbackFunc)(t->callbackArg);
    857     rf_FreeCallbackDesc(t);
    858   }
    859 }
    860 
    861 /*****************************************************************************************
    862  *
    863  * debug routines
    864  *
    865  ****************************************************************************************/
    866 
    867 static void set_debug_option(name, val)
    868   char  *name;
    869   long   val;
    870 {
    871   RF_DebugName_t *p;
    872 
    873   for (p = rf_debugNames; p->name; p++) {
    874     if (!strcmp(p->name, name)) {
    875       *(p->ptr) = val;
    876       printf("[Set debug variable %s to %ld]\n",name,val);
    877       return;
    878     }
    879   }
    880   RF_ERRORMSG1("Unknown debug string \"%s\"\n",name);
    881 }
    882 
    883 
    884 /* would like to use sscanf here, but apparently not available in kernel */
    885 /*ARGSUSED*/
    886 static void rf_ConfigureDebug(cfgPtr)
    887   RF_Config_t  *cfgPtr;
    888 {
    889   char *val_p, *name_p, *white_p;
    890   long val;
    891   int i;
    892 
    893   rf_ResetDebugOptions();
    894   for (i=0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
    895     name_p  = rf_find_non_white(&cfgPtr->debugVars[i][0]);
    896     white_p = rf_find_white(name_p);                                   /* skip to start of 2nd word */
    897     val_p   = rf_find_non_white(white_p);
    898     if (*val_p == '0' && *(val_p+1) == 'x') val = rf_htoi(val_p+2);
    899     else val = rf_atoi(val_p);
    900     *white_p = '\0';
    901     set_debug_option(name_p, val);
    902   }
    903 }
    904 
    905 /* performance monitoring stuff */
    906 
    907 #define TIMEVAL_TO_US(t) (((long) t.tv_sec) * 1000000L + (long) t.tv_usec)
    908 
    909 #if !defined(_KERNEL) && !defined(SIMULATE)
    910 
    911 /*
    912  * Throughput stats currently only used in user-level RAIDframe
    913  */
    914 
    915 static int rf_InitThroughputStats(
    916   RF_ShutdownList_t  **listp,
    917   RF_Raid_t           *raidPtr,
    918   RF_Config_t         *cfgPtr)
    919 {
    920   int rc;
    921 
    922   /* these used by user-level raidframe only */
    923   rc = rf_create_managed_mutex(listp, &raidPtr->throughputstats.mutex);
    924   if (rc) {
    925     RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    926       __LINE__, rc);
    927     return(rc);
    928   }
    929   raidPtr->throughputstats.sum_io_us = 0;
    930   raidPtr->throughputstats.num_ios = 0;
    931   raidPtr->throughputstats.num_out_ios = 0;
    932   return(0);
    933 }
    934 
    935 void rf_StartThroughputStats(RF_Raid_t *raidPtr)
    936 {
    937   RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
    938   raidPtr->throughputstats.num_ios++;
    939   raidPtr->throughputstats.num_out_ios++;
    940   if (raidPtr->throughputstats.num_out_ios == 1)
    941     RF_GETTIME(raidPtr->throughputstats.start);
    942   RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
    943 }
    944 
    945 static void rf_StopThroughputStats(RF_Raid_t *raidPtr)
    946 {
    947   struct timeval diff;
    948 
    949   RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
    950   raidPtr->throughputstats.num_out_ios--;
    951   if (raidPtr->throughputstats.num_out_ios == 0) {
    952     RF_GETTIME(raidPtr->throughputstats.stop);
    953     RF_TIMEVAL_DIFF(&raidPtr->throughputstats.start, &raidPtr->throughputstats.stop, &diff);
    954     raidPtr->throughputstats.sum_io_us += TIMEVAL_TO_US(diff);
    955   }
    956   RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
    957 }
    958 
    959 static void rf_PrintThroughputStats(RF_Raid_t *raidPtr)
    960 {
    961   RF_ASSERT(raidPtr->throughputstats.num_out_ios == 0);
    962   if ( raidPtr->throughputstats.sum_io_us != 0 ) {
    963      printf("[Througphut: %8.2f IOs/second]\n", raidPtr->throughputstats.num_ios
    964        / (raidPtr->throughputstats.sum_io_us / 1000000.0));
    965   }
    966 }
    967 
    968 #endif /* !KERNEL && !SIMULATE */
    969 
    970 void rf_StartUserStats(RF_Raid_t *raidPtr)
    971 {
    972   RF_GETTIME(raidPtr->userstats.start);
    973   raidPtr->userstats.sum_io_us = 0;
    974   raidPtr->userstats.num_ios = 0;
    975   raidPtr->userstats.num_sect_moved = 0;
    976 }
    977 
    978 void rf_StopUserStats(RF_Raid_t *raidPtr)
    979 {
    980   RF_GETTIME(raidPtr->userstats.stop);
    981 }
    982 
    983 void rf_UpdateUserStats(raidPtr, rt, numsect)
    984   RF_Raid_t  *raidPtr;
    985   int         rt;       /* resp time in us */
    986   int         numsect;  /* number of sectors for this access */
    987 {
    988   raidPtr->userstats.sum_io_us += rt;
    989   raidPtr->userstats.num_ios++;
    990   raidPtr->userstats.num_sect_moved += numsect;
    991 }
    992 
    993 void rf_PrintUserStats(RF_Raid_t *raidPtr)
    994 {
    995   long elapsed_us, mbs, mbs_frac;
    996   struct timeval diff;
    997 
    998   RF_TIMEVAL_DIFF(&raidPtr->userstats.start, &raidPtr->userstats.stop, &diff);
    999   elapsed_us = TIMEVAL_TO_US(diff);
   1000 
   1001   /* 2000 sectors per megabyte, 10000000 microseconds per second */
   1002   if (elapsed_us)
   1003     mbs = (raidPtr->userstats.num_sect_moved / 2000) / (elapsed_us / 1000000);
   1004   else
   1005     mbs = 0;
   1006 
   1007   /* this computes only the first digit of the fractional mb/s moved */
   1008   if (elapsed_us) {
   1009     mbs_frac = ((raidPtr->userstats.num_sect_moved / 200) / (elapsed_us / 1000000))
   1010       - (mbs * 10);
   1011   }
   1012   else {
   1013     mbs_frac = 0;
   1014   }
   1015 
   1016   printf("Number of I/Os:             %ld\n",raidPtr->userstats.num_ios);
   1017   printf("Elapsed time (us):          %ld\n",elapsed_us);
   1018   printf("User I/Os per second:       %ld\n",RF_DB0_CHECK(raidPtr->userstats.num_ios, (elapsed_us/1000000)));
   1019   printf("Average user response time: %ld us\n",RF_DB0_CHECK(raidPtr->userstats.sum_io_us, raidPtr->userstats.num_ios));
   1020   printf("Total sectors moved:        %ld\n",raidPtr->userstats.num_sect_moved);
   1021   printf("Average access size (sect): %ld\n",RF_DB0_CHECK(raidPtr->userstats.num_sect_moved, raidPtr->userstats.num_ios));
   1022   printf("Achieved data rate:         %ld.%ld MB/sec\n",mbs,mbs_frac);
   1023 }
   1024