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