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