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