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