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