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