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