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