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