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