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