Home | History | Annotate | Line # | Download | only in raidframe
rf_driver.c revision 1.85
      1 /*	$NetBSD: rf_driver.c,v 1.85 2004/02/27 02:55:17 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/cdefs.h>
     76 __KERNEL_RCSID(0, "$NetBSD: rf_driver.c,v 1.85 2004/02/27 02:55:17 oster Exp $");
     77 
     78 #include "opt_raid_diagnostic.h"
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/ioctl.h>
     83 #include <sys/fcntl.h>
     84 #include <sys/vnode.h>
     85 
     86 
     87 #include "rf_archs.h"
     88 #include "rf_threadstuff.h"
     89 
     90 #include <sys/errno.h>
     91 
     92 #include "rf_raid.h"
     93 #include "rf_dag.h"
     94 #include "rf_aselect.h"
     95 #include "rf_diskqueue.h"
     96 #include "rf_parityscan.h"
     97 #include "rf_alloclist.h"
     98 #include "rf_dagutils.h"
     99 #include "rf_utils.h"
    100 #include "rf_etimer.h"
    101 #include "rf_acctrace.h"
    102 #include "rf_general.h"
    103 #include "rf_desc.h"
    104 #include "rf_states.h"
    105 #include "rf_decluster.h"
    106 #include "rf_map.h"
    107 #include "rf_revent.h"
    108 #include "rf_callback.h"
    109 #include "rf_engine.h"
    110 #include "rf_mcpair.h"
    111 #include "rf_nwayxor.h"
    112 #include "rf_copyback.h"
    113 #include "rf_driver.h"
    114 #include "rf_options.h"
    115 #include "rf_shutdown.h"
    116 #include "rf_kintf.h"
    117 
    118 #include <sys/buf.h>
    119 
    120 #ifndef RF_ACCESS_DEBUG
    121 #define RF_ACCESS_DEBUG 0
    122 #endif
    123 
    124 /* rad == RF_RaidAccessDesc_t */
    125 RF_DECLARE_MUTEX(rf_rad_pool_lock)
    126 static struct pool rf_rad_pool;
    127 #define RF_MAX_FREE_RAD 128
    128 #define RF_RAD_INC       16
    129 #define RF_RAD_INITIAL   32
    130 
    131 /* debug variables */
    132 char    rf_panicbuf[2048];	/* a buffer to hold an error msg when we panic */
    133 
    134 /* main configuration routines */
    135 static int raidframe_booted = 0;
    136 
    137 static void rf_ConfigureDebug(RF_Config_t * cfgPtr);
    138 static void set_debug_option(char *name, long val);
    139 static void rf_UnconfigureArray(void);
    140 static void rf_ShutdownRDFreeList(void *);
    141 static int rf_ConfigureRDFreeList(RF_ShutdownList_t **);
    142 
    143 RF_DECLARE_MUTEX(rf_printf_mutex)	/* debug only:  avoids interleaved
    144 					 * printfs by different stripes */
    145 
    146 #define SIGNAL_QUIESCENT_COND(_raid_)  wakeup(&((_raid_)->accesses_suspended))
    147 #define WAIT_FOR_QUIESCENCE(_raid_) \
    148 	ltsleep(&((_raid_)->accesses_suspended), PRIBIO, \
    149 		"raidframe quiesce", 0, &((_raid_)->access_suspend_mutex))
    150 
    151 static int configureCount = 0;	/* number of active configurations */
    152 static int isconfigged = 0;	/* is basic raidframe (non per-array)
    153 				 * stuff configged */
    154 RF_DECLARE_LKMGR_STATIC_MUTEX(configureMutex)	/* used to lock the configuration
    155 					 * stuff */
    156 static RF_ShutdownList_t *globalShutdown;	/* non array-specific
    157 						 * stuff */
    158 
    159 static int rf_ConfigureRDFreeList(RF_ShutdownList_t ** listp);
    160 
    161 /* called at system boot time */
    162 int
    163 rf_BootRaidframe()
    164 {
    165 
    166 	if (raidframe_booted)
    167 		return (EBUSY);
    168 	raidframe_booted = 1;
    169 	lockinit(&configureMutex, PRIBIO, "RAIDframe lock", 0, 0);
    170  	configureCount = 0;
    171 	isconfigged = 0;
    172 	globalShutdown = NULL;
    173 	return (0);
    174 }
    175 
    176 /*
    177  * Called whenever an array is shutdown
    178  */
    179 static void
    180 rf_UnconfigureArray()
    181 {
    182 	int     rc;
    183 
    184 	RF_LOCK_LKMGR_MUTEX(configureMutex);
    185 	if (--configureCount == 0) {	/* if no active configurations, shut
    186 					 * everything down */
    187 		isconfigged = 0;
    188 
    189 		rc = rf_ShutdownList(&globalShutdown);
    190 		if (rc) {
    191 			RF_ERRORMSG1("RAIDFRAME: unable to do global shutdown, rc=%d\n", rc);
    192 		}
    193 
    194 		/*
    195 	         * We must wait until now, because the AllocList module
    196 	         * uses the DebugMem module.
    197 	         */
    198 #if RF_DEBUG_MEM
    199 		if (rf_memDebug)
    200 			rf_print_unfreed();
    201 #endif
    202 	}
    203 	RF_UNLOCK_LKMGR_MUTEX(configureMutex);
    204 }
    205 
    206 /*
    207  * Called to shut down an array.
    208  */
    209 int
    210 rf_Shutdown(RF_Raid_t *raidPtr)
    211 {
    212 	if (!raidPtr->valid) {
    213 		RF_ERRORMSG("Attempt to shut down unconfigured RAIDframe driver.  Aborting shutdown\n");
    214 		return (EINVAL);
    215 	}
    216 	/*
    217          * wait for outstanding IOs to land
    218          * As described in rf_raid.h, we use the rad_freelist lock
    219          * to protect the per-array info about outstanding descs
    220          * since we need to do freelist locking anyway, and this
    221          * cuts down on the amount of serialization we've got going
    222          * on.
    223          */
    224 	RF_LOCK_MUTEX(rf_rad_pool_lock);
    225 	if (raidPtr->waitShutdown) {
    226 		RF_UNLOCK_MUTEX(rf_rad_pool_lock);
    227 		return (EBUSY);
    228 	}
    229 	raidPtr->waitShutdown = 1;
    230 	while (raidPtr->nAccOutstanding) {
    231 		RF_WAIT_COND(raidPtr->outstandingCond, rf_rad_pool_lock);
    232 	}
    233 	RF_UNLOCK_MUTEX(rf_rad_pool_lock);
    234 
    235 	/* Wait for any parity re-writes to stop... */
    236 	while (raidPtr->parity_rewrite_in_progress) {
    237 		printf("Waiting for parity re-write to exit...\n");
    238 		tsleep(&raidPtr->parity_rewrite_in_progress, PRIBIO,
    239 		       "rfprwshutdown", 0);
    240 	}
    241 
    242 	raidPtr->valid = 0;
    243 
    244 	rf_update_component_labels(raidPtr, RF_FINAL_COMPONENT_UPDATE);
    245 
    246 	rf_UnconfigureVnodes(raidPtr);
    247 
    248 	rf_ShutdownList(&raidPtr->shutdownList);
    249 
    250 	rf_UnconfigureArray();
    251 
    252 	return (0);
    253 }
    254 
    255 
    256 #define DO_INIT_CONFIGURE(f) { \
    257 	rc = f (&globalShutdown); \
    258 	if (rc) { \
    259 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    260 		rf_ShutdownList(&globalShutdown); \
    261 		configureCount--; \
    262 		RF_UNLOCK_LKMGR_MUTEX(configureMutex); \
    263 		return(rc); \
    264 	} \
    265 }
    266 
    267 #define DO_RAID_FAIL() { \
    268 	rf_UnconfigureVnodes(raidPtr); \
    269 	rf_ShutdownList(&raidPtr->shutdownList); \
    270 	rf_UnconfigureArray(); \
    271 }
    272 
    273 #define DO_RAID_INIT_CONFIGURE(f) { \
    274 	rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
    275 	if (rc) { \
    276 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    277 		DO_RAID_FAIL(); \
    278 		return(rc); \
    279 	} \
    280 }
    281 
    282 #define DO_RAID_MUTEX(_m_) { \
    283 	rf_mutex_init((_m_)); \
    284 }
    285 
    286 int
    287 rf_Configure(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr, RF_AutoConfig_t *ac)
    288 {
    289 	RF_RowCol_t col;
    290 	int rc;
    291 
    292 	RF_LOCK_LKMGR_MUTEX(configureMutex);
    293 	configureCount++;
    294 	if (isconfigged == 0) {
    295 		rf_mutex_init(&rf_printf_mutex);
    296 
    297 		/* initialize globals */
    298 
    299 		DO_INIT_CONFIGURE(rf_ConfigureAllocList);
    300 
    301 		/*
    302 	         * Yes, this does make debugging general to the whole
    303 	         * system instead of being array specific. Bummer, drag.
    304 		 */
    305 		rf_ConfigureDebug(cfgPtr);
    306 		DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
    307 		DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
    308 		DO_INIT_CONFIGURE(rf_ConfigureMapModule);
    309 		DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
    310 		DO_INIT_CONFIGURE(rf_ConfigureCallback);
    311 		DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
    312 		DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
    313 		DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
    314 		DO_INIT_CONFIGURE(rf_ConfigureMCPair);
    315 		DO_INIT_CONFIGURE(rf_ConfigureDAGs);
    316 		DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
    317 		DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
    318 		DO_INIT_CONFIGURE(rf_ConfigureCopyback);
    319 		DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
    320 		isconfigged = 1;
    321 	}
    322 	RF_UNLOCK_LKMGR_MUTEX(configureMutex);
    323 
    324 	DO_RAID_MUTEX(&raidPtr->mutex);
    325 	/* set up the cleanup list.  Do this after ConfigureDebug so that
    326 	 * value of memDebug will be set */
    327 
    328 	rf_MakeAllocList(raidPtr->cleanupList);
    329 	if (raidPtr->cleanupList == NULL) {
    330 		DO_RAID_FAIL();
    331 		return (ENOMEM);
    332 	}
    333 	rc = rf_ShutdownCreate(&raidPtr->shutdownList,
    334 	    (void (*) (void *)) rf_FreeAllocList,
    335 	    raidPtr->cleanupList);
    336 	if (rc) {
    337 		rf_print_unable_to_add_shutdown(__FILE__, __LINE__, rc);
    338 		DO_RAID_FAIL();
    339 		return (rc);
    340 	}
    341 	raidPtr->numCol = cfgPtr->numCol;
    342 	raidPtr->numSpare = cfgPtr->numSpare;
    343 
    344 	raidPtr->status = rf_rs_optimal;
    345 	raidPtr->reconControl = NULL;
    346 
    347 	TAILQ_INIT(&(raidPtr->iodone));
    348 	simple_lock_init(&(raidPtr->iodone_lock));
    349 
    350 	DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
    351 	DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
    352 
    353 	raidPtr->outstandingCond = 0;
    354 
    355 	raidPtr->nAccOutstanding = 0;
    356 	raidPtr->waitShutdown = 0;
    357 
    358 	DO_RAID_MUTEX(&raidPtr->access_suspend_mutex);
    359 
    360 	raidPtr->waitForReconCond = 0;
    361 
    362 	if (ac!=NULL) {
    363 		/* We have an AutoConfig structure..  Don't do the
    364 		   normal disk configuration... call the auto config
    365 		   stuff */
    366 		rf_AutoConfigureDisks(raidPtr, cfgPtr, ac);
    367 	} else {
    368 		DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
    369 		DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
    370 	}
    371 	/* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
    372 	 * no. is set */
    373 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
    374 
    375 	DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
    376 
    377 	DO_RAID_INIT_CONFIGURE(rf_ConfigurePSStatus);
    378 
    379 #if RF_INCLUDE_CHAINDECLUSTER > 0
    380 	for (col = 0; col < raidPtr->numCol; col++) {
    381 		/*
    382 		 * XXX better distribution
    383 		 */
    384 		raidPtr->hist_diskreq[col] = 0;
    385 	}
    386 #endif
    387 	raidPtr->numNewFailures = 0;
    388 	raidPtr->copyback_in_progress = 0;
    389 	raidPtr->parity_rewrite_in_progress = 0;
    390 	raidPtr->adding_hot_spare = 0;
    391 	raidPtr->recon_in_progress = 0;
    392 	raidPtr->maxOutstanding = cfgPtr->maxOutstandingDiskReqs;
    393 
    394 	/* autoconfigure and root_partition will actually get filled in
    395 	   after the config is done */
    396 	raidPtr->autoconfigure = 0;
    397 	raidPtr->root_partition = 0;
    398 	raidPtr->last_unit = raidPtr->raidid;
    399 	raidPtr->config_order = 0;
    400 
    401 	if (rf_keepAccTotals) {
    402 		raidPtr->keep_acc_totals = 1;
    403 	}
    404 	rf_StartUserStats(raidPtr);
    405 
    406 	raidPtr->valid = 1;
    407 
    408 	printf("raid%d: %s\n", raidPtr->raidid,
    409 	       raidPtr->Layout.map->configName);
    410 	printf("raid%d: Components:", raidPtr->raidid);
    411 
    412 	for (col = 0; col < raidPtr->numCol; col++) {
    413 		printf(" %s", raidPtr->Disks[col].devname);
    414 		if (RF_DEAD_DISK(raidPtr->Disks[col].status)) {
    415 			printf("[**FAILED**]");
    416 		}
    417 	}
    418 	printf("\n");
    419 	printf("raid%d: Total Sectors: %lu (%lu MB)\n",
    420 	       raidPtr->raidid,
    421 	       (unsigned long) raidPtr->totalSectors,
    422 	       (unsigned long) (raidPtr->totalSectors / 1024 *
    423 				(1 << raidPtr->logBytesPerSector) / 1024));
    424 
    425 	return (0);
    426 }
    427 
    428 static void
    429 rf_ShutdownRDFreeList(void *ignored)
    430 {
    431 	pool_destroy(&rf_rad_pool);
    432 }
    433 
    434 static int
    435 rf_ConfigureRDFreeList(RF_ShutdownList_t **listp)
    436 {
    437 	int     rc;
    438 
    439 	pool_init(&rf_rad_pool, sizeof(RF_RaidAccessDesc_t), 0, 0, 0,
    440 		  "rf_rad_pl", NULL);
    441 	pool_sethiwat(&rf_rad_pool, RF_MAX_FREE_RAD);
    442 	pool_prime(&rf_rad_pool, RF_RAD_INITIAL);
    443 	rc = rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
    444 	if (rc) {
    445 		rf_print_unable_to_add_shutdown(__FILE__, __LINE__, rc);
    446 		rf_ShutdownRDFreeList(NULL);
    447 		return (rc);
    448 	}
    449 	simple_lock_init(&rf_rad_pool_lock);
    450 	return (0);
    451 }
    452 
    453 RF_RaidAccessDesc_t *
    454 rf_AllocRaidAccDesc(RF_Raid_t *raidPtr, RF_IoType_t type,
    455 		    RF_RaidAddr_t raidAddress, RF_SectorCount_t numBlocks,
    456 		    caddr_t bufPtr, void *bp, RF_RaidAccessFlags_t flags,
    457 		    RF_AccessState_t *states)
    458 {
    459 	RF_RaidAccessDesc_t *desc;
    460 
    461 	desc = pool_get(&rf_rad_pool, PR_WAITOK);
    462 	simple_lock_init(&desc->mutex);
    463 
    464 	RF_LOCK_MUTEX(rf_rad_pool_lock);
    465 	if (raidPtr->waitShutdown) {
    466 		/*
    467 	         * Actually, we're shutting the array down. Free the desc
    468 	         * and return NULL.
    469 	         */
    470 
    471 		RF_UNLOCK_MUTEX(rf_rad_pool_lock);
    472 		pool_put(&rf_rad_pool, desc);
    473 		return (NULL);
    474 	}
    475 	raidPtr->nAccOutstanding++;
    476 
    477 	RF_UNLOCK_MUTEX(rf_rad_pool_lock);
    478 
    479 	desc->raidPtr = (void *) raidPtr;
    480 	desc->type = type;
    481 	desc->raidAddress = raidAddress;
    482 	desc->numBlocks = numBlocks;
    483 	desc->bufPtr = bufPtr;
    484 	desc->bp = bp;
    485 	desc->paramDAG = NULL;
    486 	desc->paramASM = NULL;
    487 	desc->flags = flags;
    488 	desc->states = states;
    489 	desc->state = 0;
    490 
    491 	desc->status = 0;
    492 	memset((char *) &desc->tracerec, 0, sizeof(RF_AccTraceEntry_t));
    493 	desc->callbackFunc = NULL;
    494 	desc->callbackArg = NULL;
    495 	desc->next = NULL;
    496 	desc->cleanupList = NULL;
    497 	rf_MakeAllocList(desc->cleanupList);
    498 	return (desc);
    499 }
    500 
    501 void
    502 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t *desc)
    503 {
    504 	RF_Raid_t *raidPtr = desc->raidPtr;
    505 	RF_DagList_t *dagList, *temp;
    506 
    507 	RF_ASSERT(desc);
    508 
    509 	/* Cleanup the dagList(s) */
    510 	dagList = desc->dagList;
    511 	while(dagList != NULL) {
    512 		temp = dagList;
    513 		dagList = dagList->next;
    514 		rf_FreeDAGList(temp);
    515 	}
    516 
    517 	rf_FreeAllocList(desc->cleanupList);
    518 	pool_put(&rf_rad_pool, desc);
    519 	RF_LOCK_MUTEX(rf_rad_pool_lock);
    520 	raidPtr->nAccOutstanding--;
    521 	if (raidPtr->waitShutdown) {
    522 		RF_SIGNAL_COND(raidPtr->outstandingCond);
    523 	}
    524 	RF_UNLOCK_MUTEX(rf_rad_pool_lock);
    525 }
    526 /*********************************************************************
    527  * Main routine for performing an access.
    528  * Accesses are retried until a DAG can not be selected.  This occurs
    529  * when either the DAG library is incomplete or there are too many
    530  * failures in a parity group.
    531  *
    532  * type should be read or write async_flag should be RF_TRUE or
    533  * RF_FALSE bp_in is a buf pointer.  void * to facilitate ignoring it
    534  * outside the kernel
    535  ********************************************************************/
    536 int
    537 rf_DoAccess(RF_Raid_t * raidPtr, RF_IoType_t type, int async_flag,
    538 	    RF_RaidAddr_t raidAddress, RF_SectorCount_t numBlocks,
    539 	    caddr_t bufPtr, void *bp_in, RF_RaidAccessFlags_t flags)
    540 {
    541 	RF_RaidAccessDesc_t *desc;
    542 	caddr_t lbufPtr = bufPtr;
    543 	struct buf *bp = (struct buf *) bp_in;
    544 
    545 	raidAddress += rf_raidSectorOffset;
    546 
    547 #if RF_ACCESS_DEBUG
    548 	if (rf_accessDebug) {
    549 
    550 		printf("logBytes is: %d %d %d\n", raidPtr->raidid,
    551 		    raidPtr->logBytesPerSector,
    552 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks));
    553 		printf("raid%d: %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", raidPtr->raidid,
    554 		    (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
    555 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
    556 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
    557 		    (int) numBlocks,
    558 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks),
    559 		    (long) bufPtr);
    560 	}
    561 #endif
    562 	if (raidAddress + numBlocks > raidPtr->totalSectors) {
    563 
    564 		printf("DoAccess: raid addr %lu too large to access %lu sectors.  Max legal addr is %lu\n",
    565 		    (u_long) raidAddress, (u_long) numBlocks, (u_long) raidPtr->totalSectors);
    566 
    567 
    568 		bp->b_flags |= B_ERROR;
    569 		bp->b_resid = bp->b_bcount;
    570 		bp->b_error = ENOSPC;
    571 		biodone(bp);
    572 		return (ENOSPC);
    573 	}
    574 	desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
    575 	    numBlocks, lbufPtr, bp, flags, raidPtr->Layout.map->states);
    576 
    577 	if (desc == NULL) {
    578 		return (ENOMEM);
    579 	}
    580 	RF_ETIMER_START(desc->tracerec.tot_timer);
    581 
    582 	desc->async_flag = async_flag;
    583 
    584 	rf_ContinueRaidAccess(desc);
    585 
    586 	return (0);
    587 }
    588 #if 0
    589 /* force the array into reconfigured mode without doing reconstruction */
    590 int
    591 rf_SetReconfiguredMode(RF_Raid_t *raidPtr, int col)
    592 {
    593 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
    594 		printf("Can't set reconfigured mode in dedicated-spare array\n");
    595 		RF_PANIC();
    596 	}
    597 	RF_LOCK_MUTEX(raidPtr->mutex);
    598 	raidPtr->numFailures++;
    599 	raidPtr->Disks[col].status = rf_ds_dist_spared;
    600 	raidPtr->status = rf_rs_reconfigured;
    601 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    602 	/* install spare table only if declustering + distributed sparing
    603 	 * architecture. */
    604 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
    605 		rf_InstallSpareTable(raidPtr, col);
    606 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    607 	return (0);
    608 }
    609 #endif
    610 
    611 int
    612 rf_FailDisk(RF_Raid_t *raidPtr, int fcol, int initRecon)
    613 {
    614 	RF_LOCK_MUTEX(raidPtr->mutex);
    615 	if (raidPtr->Disks[fcol].status != rf_ds_failed) {
    616 		/* must be failing something that is valid, or else it's
    617 		   already marked as failed (in which case we don't
    618 		   want to mark it failed again!) */
    619 		raidPtr->numFailures++;
    620 		raidPtr->Disks[fcol].status = rf_ds_failed;
    621 		raidPtr->status = rf_rs_degraded;
    622 	}
    623 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    624 
    625 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    626 
    627 	/* Close the component, so that it's not "locked" if someone
    628 	   else want's to use it! */
    629 
    630 	rf_close_component(raidPtr, raidPtr->raid_cinfo[fcol].ci_vp,
    631 			   raidPtr->Disks[fcol].auto_configured);
    632 
    633 	RF_LOCK_MUTEX(raidPtr->mutex);
    634 	raidPtr->raid_cinfo[fcol].ci_vp = NULL;
    635 
    636 	/* Need to mark the component as not being auto_configured
    637 	   (in case it was previously). */
    638 
    639 	raidPtr->Disks[fcol].auto_configured = 0;
    640 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    641 
    642 	if (initRecon)
    643 		rf_ReconstructFailedDisk(raidPtr, fcol);
    644 	return (0);
    645 }
    646 /* releases a thread that is waiting for the array to become quiesced.
    647  * access_suspend_mutex should be locked upon calling this
    648  */
    649 void
    650 rf_SignalQuiescenceLock(RF_Raid_t *raidPtr)
    651 {
    652 #if RF_DEBUG_QUIESCE
    653 	if (rf_quiesceDebug) {
    654 		printf("raid%d: Signalling quiescence lock\n",
    655 		       raidPtr->raidid);
    656 	}
    657 #endif
    658 	raidPtr->access_suspend_release = 1;
    659 
    660 	if (raidPtr->waiting_for_quiescence) {
    661 		SIGNAL_QUIESCENT_COND(raidPtr);
    662 	}
    663 }
    664 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
    665 int
    666 rf_SuspendNewRequestsAndWait(RF_Raid_t *raidPtr)
    667 {
    668 #if RF_DEBUG_QUIESCE
    669 	if (rf_quiesceDebug)
    670 		printf("raid%d: Suspending new reqs\n", raidPtr->raidid);
    671 #endif
    672 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    673 	raidPtr->accesses_suspended++;
    674 	raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
    675 
    676 	if (raidPtr->waiting_for_quiescence) {
    677 		raidPtr->access_suspend_release = 0;
    678 		while (!raidPtr->access_suspend_release) {
    679 			printf("raid%d: Suspending: Waiting for Quiescence\n",
    680 			       raidPtr->raidid);
    681 			WAIT_FOR_QUIESCENCE(raidPtr);
    682 			raidPtr->waiting_for_quiescence = 0;
    683 		}
    684 	}
    685 	printf("raid%d: Quiescence reached..\n", raidPtr->raidid);
    686 
    687 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    688 	return (raidPtr->waiting_for_quiescence);
    689 }
    690 /* wake up everyone waiting for quiescence to be released */
    691 void
    692 rf_ResumeNewRequests(RF_Raid_t *raidPtr)
    693 {
    694 	RF_CallbackDesc_t *t, *cb;
    695 
    696 #if RF_DEBUG_QUIESCE
    697 	if (rf_quiesceDebug)
    698 		printf("Resuming new reqs\n");
    699 #endif
    700 
    701 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    702 	raidPtr->accesses_suspended--;
    703 	if (raidPtr->accesses_suspended == 0)
    704 		cb = raidPtr->quiesce_wait_list;
    705 	else
    706 		cb = NULL;
    707 	raidPtr->quiesce_wait_list = NULL;
    708 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    709 
    710 	while (cb) {
    711 		t = cb;
    712 		cb = cb->next;
    713 		(t->callbackFunc) (t->callbackArg);
    714 		rf_FreeCallbackDesc(t);
    715 	}
    716 }
    717 /*****************************************************************************************
    718  *
    719  * debug routines
    720  *
    721  ****************************************************************************************/
    722 
    723 static void
    724 set_debug_option(char *name, long val)
    725 {
    726 	RF_DebugName_t *p;
    727 
    728 	for (p = rf_debugNames; p->name; p++) {
    729 		if (!strcmp(p->name, name)) {
    730 			*(p->ptr) = val;
    731 			printf("[Set debug variable %s to %ld]\n", name, val);
    732 			return;
    733 		}
    734 	}
    735 	RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
    736 }
    737 
    738 
    739 /* would like to use sscanf here, but apparently not available in kernel */
    740 /*ARGSUSED*/
    741 static void
    742 rf_ConfigureDebug(RF_Config_t *cfgPtr)
    743 {
    744 	char   *val_p, *name_p, *white_p;
    745 	long    val;
    746 	int     i;
    747 
    748 	rf_ResetDebugOptions();
    749 	for (i = 0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
    750 		name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
    751 		white_p = rf_find_white(name_p);	/* skip to start of 2nd
    752 							 * word */
    753 		val_p = rf_find_non_white(white_p);
    754 		if (*val_p == '0' && *(val_p + 1) == 'x')
    755 			val = rf_htoi(val_p + 2);
    756 		else
    757 			val = rf_atoi(val_p);
    758 		*white_p = '\0';
    759 		set_debug_option(name_p, val);
    760 	}
    761 }
    762 /* performance monitoring stuff */
    763 
    764 #define TIMEVAL_TO_US(t) (((long) t.tv_sec) * 1000000L + (long) t.tv_usec)
    765 
    766 #if !defined(_KERNEL) && !defined(SIMULATE)
    767 
    768 /*
    769  * Throughput stats currently only used in user-level RAIDframe
    770  */
    771 
    772 static int
    773 rf_InitThroughputStats(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
    774 		       RF_Config_t *cfgPtr)
    775 {
    776 	int     rc;
    777 
    778 	/* these used by user-level raidframe only */
    779 	rf_mutex_init(&raidPtr->throughputstats.mutex);
    780 	raidPtr->throughputstats.sum_io_us = 0;
    781 	raidPtr->throughputstats.num_ios = 0;
    782 	raidPtr->throughputstats.num_out_ios = 0;
    783 	return (0);
    784 }
    785 
    786 void
    787 rf_StartThroughputStats(RF_Raid_t *raidPtr)
    788 {
    789 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
    790 	raidPtr->throughputstats.num_ios++;
    791 	raidPtr->throughputstats.num_out_ios++;
    792 	if (raidPtr->throughputstats.num_out_ios == 1)
    793 		RF_GETTIME(raidPtr->throughputstats.start);
    794 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
    795 }
    796 
    797 static void
    798 rf_StopThroughputStats(RF_Raid_t *raidPtr)
    799 {
    800 	struct timeval diff;
    801 
    802 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
    803 	raidPtr->throughputstats.num_out_ios--;
    804 	if (raidPtr->throughputstats.num_out_ios == 0) {
    805 		RF_GETTIME(raidPtr->throughputstats.stop);
    806 		RF_TIMEVAL_DIFF(&raidPtr->throughputstats.start, &raidPtr->throughputstats.stop, &diff);
    807 		raidPtr->throughputstats.sum_io_us += TIMEVAL_TO_US(diff);
    808 	}
    809 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
    810 }
    811 
    812 static void
    813 rf_PrintThroughputStats(RF_Raid_t *raidPtr)
    814 {
    815 	RF_ASSERT(raidPtr->throughputstats.num_out_ios == 0);
    816 	if (raidPtr->throughputstats.sum_io_us != 0) {
    817 		printf("[Througphut: %8.2f IOs/second]\n", raidPtr->throughputstats.num_ios
    818 		    / (raidPtr->throughputstats.sum_io_us / 1000000.0));
    819 	}
    820 }
    821 #endif				/* !KERNEL && !SIMULATE */
    822 
    823 void
    824 rf_StartUserStats(RF_Raid_t *raidPtr)
    825 {
    826 	RF_GETTIME(raidPtr->userstats.start);
    827 	raidPtr->userstats.sum_io_us = 0;
    828 	raidPtr->userstats.num_ios = 0;
    829 	raidPtr->userstats.num_sect_moved = 0;
    830 }
    831 
    832 void
    833 rf_StopUserStats(RF_Raid_t *raidPtr)
    834 {
    835 	RF_GETTIME(raidPtr->userstats.stop);
    836 }
    837 
    838 /* rt: resp time in us
    839    numsect: number of sectors for this access */
    840 void
    841 rf_UpdateUserStats(RF_Raid_t *raidPtr, int rt, int numsect)
    842 {
    843 	raidPtr->userstats.sum_io_us += rt;
    844 	raidPtr->userstats.num_ios++;
    845 	raidPtr->userstats.num_sect_moved += numsect;
    846 }
    847 
    848 void
    849 rf_PrintUserStats(RF_Raid_t *raidPtr)
    850 {
    851 	long    elapsed_us, mbs, mbs_frac;
    852 	struct timeval diff;
    853 
    854 	RF_TIMEVAL_DIFF(&raidPtr->userstats.start,
    855 			&raidPtr->userstats.stop, &diff);
    856 	elapsed_us = TIMEVAL_TO_US(diff);
    857 
    858 	/* 2000 sectors per megabyte, 10000000 microseconds per second */
    859 	if (elapsed_us)
    860 		mbs = (raidPtr->userstats.num_sect_moved / 2000) /
    861 			(elapsed_us / 1000000);
    862 	else
    863 		mbs = 0;
    864 
    865 	/* this computes only the first digit of the fractional mb/s moved */
    866 	if (elapsed_us) {
    867 		mbs_frac = ((raidPtr->userstats.num_sect_moved / 200) /
    868 			    (elapsed_us / 1000000)) - (mbs * 10);
    869 	} else {
    870 		mbs_frac = 0;
    871 	}
    872 
    873 	printf("raid%d: Number of I/Os:             %ld\n",
    874 	       raidPtr->raidid, raidPtr->userstats.num_ios);
    875 	printf("raid%d: Elapsed time (us):          %ld\n",
    876 	       raidPtr->raidid, elapsed_us);
    877 	printf("raid%d: User I/Os per second:       %ld\n",
    878 	       raidPtr->raidid, RF_DB0_CHECK(raidPtr->userstats.num_ios,
    879 					     (elapsed_us / 1000000)));
    880 	printf("raid%d: Average user response time: %ld us\n",
    881 	       raidPtr->raidid, RF_DB0_CHECK(raidPtr->userstats.sum_io_us,
    882 					     raidPtr->userstats.num_ios));
    883 	printf("raid%d: Total sectors moved:        %ld\n",
    884 	       raidPtr->raidid, raidPtr->userstats.num_sect_moved);
    885 	printf("raid%d: Average access size (sect): %ld\n",
    886 	       raidPtr->raidid, RF_DB0_CHECK(raidPtr->userstats.num_sect_moved,
    887 					     raidPtr->userstats.num_ios));
    888 	printf("raid%d: Achieved data rate:         %ld.%ld MB/sec\n",
    889 	       raidPtr->raidid, mbs, mbs_frac);
    890 }
    891 
    892 
    893 void
    894 rf_print_panic_message(int line, char *file)
    895 {
    896 	sprintf(rf_panicbuf,"raidframe error at line %d file %s",
    897 		line, file);
    898 }
    899 
    900 #ifdef RAID_DIAGNOSTIC
    901 void
    902 rf_print_assert_panic_message(int line,	char *file, char *condition)
    903 {
    904 	sprintf(rf_panicbuf,
    905 		"raidframe error at line %d file %s (failed asserting %s)\n",
    906 		line, file, condition);
    907 }
    908 #endif
    909 
    910 void
    911 rf_print_unable_to_init_mutex(char *file, int line, int rc)
    912 {
    913 	RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n",
    914 		     file, line, rc);
    915 }
    916 
    917 void
    918 rf_print_unable_to_add_shutdown(char *file, int line, int rc)
    919 {
    920 	RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
    921 		     file, line, rc);
    922 }
    923