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