Home | History | Annotate | Line # | Download | only in raidframe
rf_driver.c revision 1.72
      1 /*	$NetBSD: rf_driver.c,v 1.72 2003/12/29 02:38:17 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.72 2003/12/29 02:38:17 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_freelist.h"
    106 #include "rf_decluster.h"
    107 #include "rf_map.h"
    108 #include "rf_revent.h"
    109 #include "rf_callback.h"
    110 #include "rf_engine.h"
    111 #include "rf_mcpair.h"
    112 #include "rf_nwayxor.h"
    113 #include "rf_copyback.h"
    114 #include "rf_driver.h"
    115 #include "rf_options.h"
    116 #include "rf_shutdown.h"
    117 #include "rf_kintf.h"
    118 
    119 #include <sys/buf.h>
    120 
    121 #ifndef RF_ACCESS_DEBUG
    122 #define RF_ACCESS_DEBUG 0
    123 #endif
    124 
    125 /* rad == RF_RaidAccessDesc_t */
    126 static RF_FreeList_t *rf_rad_freelist;
    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_FREELIST_DO_LOCK(rf_rad_freelist);
    242 	if (raidPtr->waitShutdown) {
    243 		RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    244 		return (EBUSY);
    245 	}
    246 	raidPtr->waitShutdown = 1;
    247 	while (raidPtr->nAccOutstanding) {
    248 		RF_WAIT_COND(raidPtr->outstandingCond, RF_FREELIST_MUTEX_OF(rf_rad_freelist));
    249 	}
    250 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    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 	RF_FREELIST_DESTROY_CLEAN(rf_rad_freelist, next, (RF_RaidAccessDesc_t *), clean_rad);
    501 }
    502 
    503 static int
    504 rf_ConfigureRDFreeList(listp)
    505 	RF_ShutdownList_t **listp;
    506 {
    507 	int     rc;
    508 
    509 	RF_FREELIST_CREATE(rf_rad_freelist, RF_MAX_FREE_RAD,
    510 	    RF_RAD_INC, sizeof(RF_RaidAccessDesc_t));
    511 	if (rf_rad_freelist == NULL) {
    512 		return (ENOMEM);
    513 	}
    514 	rc = rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
    515 	if (rc) {
    516 		rf_print_unable_to_add_shutdown(__FILE__, __LINE__, rc);
    517 		rf_ShutdownRDFreeList(NULL);
    518 		return (rc);
    519 	}
    520 	RF_FREELIST_PRIME_INIT(rf_rad_freelist, RF_RAD_INITIAL, next,
    521 	    (RF_RaidAccessDesc_t *), init_rad);
    522 	return (0);
    523 }
    524 
    525 RF_RaidAccessDesc_t *
    526 rf_AllocRaidAccDesc(
    527     RF_Raid_t * raidPtr,
    528     RF_IoType_t type,
    529     RF_RaidAddr_t raidAddress,
    530     RF_SectorCount_t numBlocks,
    531     caddr_t bufPtr,
    532     void *bp,
    533     RF_RaidAccessFlags_t flags,
    534     RF_AccessState_t * states)
    535 {
    536 	RF_RaidAccessDesc_t *desc;
    537 
    538 	RF_FREELIST_GET_INIT_NOUNLOCK(rf_rad_freelist, desc, next, (RF_RaidAccessDesc_t *), init_rad);
    539 	if (raidPtr->waitShutdown) {
    540 		/*
    541 	         * Actually, we're shutting the array down. Free the desc
    542 	         * and return NULL.
    543 	         */
    544 		RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    545 		RF_FREELIST_FREE_CLEAN(rf_rad_freelist, desc, next, clean_rad);
    546 		return (NULL);
    547 	}
    548 	raidPtr->nAccOutstanding++;
    549 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    550 
    551 	desc->raidPtr = (void *) raidPtr;
    552 	desc->type = type;
    553 	desc->raidAddress = raidAddress;
    554 	desc->numBlocks = numBlocks;
    555 	desc->bufPtr = bufPtr;
    556 	desc->bp = bp;
    557 	desc->paramDAG = NULL;
    558 	desc->paramASM = NULL;
    559 	desc->flags = flags;
    560 	desc->states = states;
    561 	desc->state = 0;
    562 
    563 	desc->status = 0;
    564 	memset((char *) &desc->tracerec, 0, sizeof(RF_AccTraceEntry_t));
    565 	desc->callbackFunc = NULL;
    566 	desc->callbackArg = NULL;
    567 	desc->next = NULL;
    568 	desc->head = desc;
    569 	desc->cleanupList = NULL;
    570 	rf_MakeAllocList(desc->cleanupList);
    571 	return (desc);
    572 }
    573 
    574 void
    575 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t * desc)
    576 {
    577 	RF_Raid_t *raidPtr = desc->raidPtr;
    578 
    579 	RF_ASSERT(desc);
    580 
    581 	rf_FreeAllocList(desc->cleanupList);
    582 	RF_FREELIST_FREE_CLEAN_NOUNLOCK(rf_rad_freelist, desc, next, clean_rad);
    583 	raidPtr->nAccOutstanding--;
    584 	if (raidPtr->waitShutdown) {
    585 		RF_SIGNAL_COND(raidPtr->outstandingCond);
    586 	}
    587 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    588 }
    589 /*********************************************************************
    590  * Main routine for performing an access.
    591  * Accesses are retried until a DAG can not be selected.  This occurs
    592  * when either the DAG library is incomplete or there are too many
    593  * failures in a parity group.
    594  ********************************************************************/
    595 int
    596 rf_DoAccess(
    597     RF_Raid_t * raidPtr,
    598     RF_IoType_t type,
    599     int async_flag,
    600     RF_RaidAddr_t raidAddress,
    601     RF_SectorCount_t numBlocks,
    602     caddr_t bufPtr,
    603     void *bp_in,
    604     RF_RaidAccessFlags_t flags)
    605 /*
    606 type should be read or write
    607 async_flag should be RF_TRUE or RF_FALSE
    608 bp_in is a buf pointer.  void * to facilitate ignoring it outside the kernel
    609 */
    610 {
    611 	RF_RaidAccessDesc_t *desc;
    612 	caddr_t lbufPtr = bufPtr;
    613 	struct buf *bp = (struct buf *) bp_in;
    614 
    615 	raidAddress += rf_raidSectorOffset;
    616 
    617 #if RF_ACCESS_DEBUG
    618 	if (rf_accessDebug) {
    619 
    620 		printf("logBytes is: %d %d %d\n", raidPtr->raidid,
    621 		    raidPtr->logBytesPerSector,
    622 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks));
    623 		printf("raid%d: %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", raidPtr->raidid,
    624 		    (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
    625 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
    626 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
    627 		    (int) numBlocks,
    628 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks),
    629 		    (long) bufPtr);
    630 	}
    631 #endif
    632 	if (raidAddress + numBlocks > raidPtr->totalSectors) {
    633 
    634 		printf("DoAccess: raid addr %lu too large to access %lu sectors.  Max legal addr is %lu\n",
    635 		    (u_long) raidAddress, (u_long) numBlocks, (u_long) raidPtr->totalSectors);
    636 
    637 		IO_BUF_ERR(bp, ENOSPC);
    638 		return (ENOSPC);
    639 	}
    640 	desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
    641 	    numBlocks, lbufPtr, bp, flags, raidPtr->Layout.map->states);
    642 
    643 	if (desc == NULL) {
    644 		return (ENOMEM);
    645 	}
    646 	RF_ETIMER_START(desc->tracerec.tot_timer);
    647 
    648 	desc->async_flag = async_flag;
    649 
    650 	rf_ContinueRaidAccess(desc);
    651 
    652 	return (0);
    653 }
    654 #if 0
    655 /* force the array into reconfigured mode without doing reconstruction */
    656 int
    657 rf_SetReconfiguredMode(raidPtr, col)
    658 	RF_Raid_t *raidPtr;
    659 	int     col;
    660 {
    661 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
    662 		printf("Can't set reconfigured mode in dedicated-spare array\n");
    663 		RF_PANIC();
    664 	}
    665 	RF_LOCK_MUTEX(raidPtr->mutex);
    666 	raidPtr->numFailures++;
    667 	raidPtr->Disks[col].status = rf_ds_dist_spared;
    668 	raidPtr->status = rf_rs_reconfigured;
    669 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    670 	/* install spare table only if declustering + distributed sparing
    671 	 * architecture. */
    672 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
    673 		rf_InstallSpareTable(raidPtr, col);
    674 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    675 	return (0);
    676 }
    677 #endif
    678 
    679 int
    680 rf_FailDisk(
    681     RF_Raid_t * raidPtr,
    682     int fcol,
    683     int initRecon)
    684 {
    685 	RF_LOCK_MUTEX(raidPtr->mutex);
    686 	if (raidPtr->Disks[fcol].status != rf_ds_failed) {
    687 		/* must be failing something that is valid, or else it's
    688 		   already marked as failed (in which case we don't
    689 		   want to mark it failed again!) */
    690 		raidPtr->numFailures++;
    691 		raidPtr->Disks[fcol].status = rf_ds_failed;
    692 		raidPtr->status = rf_rs_degraded;
    693 	}
    694 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    695 
    696 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    697 
    698 	/* Close the component, so that it's not "locked" if someone
    699 	   else want's to use it! */
    700 
    701 	rf_close_component(raidPtr, raidPtr->raid_cinfo[fcol].ci_vp,
    702 			   raidPtr->Disks[fcol].auto_configured);
    703 
    704 	RF_LOCK_MUTEX(raidPtr->mutex);
    705 	raidPtr->raid_cinfo[fcol].ci_vp = NULL;
    706 
    707 	/* Need to mark the component as not being auto_configured
    708 	   (in case it was previously). */
    709 
    710 	raidPtr->Disks[fcol].auto_configured = 0;
    711 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    712 
    713 	if (initRecon)
    714 		rf_ReconstructFailedDisk(raidPtr, fcol);
    715 	return (0);
    716 }
    717 /* releases a thread that is waiting for the array to become quiesced.
    718  * access_suspend_mutex should be locked upon calling this
    719  */
    720 void
    721 rf_SignalQuiescenceLock(raidPtr)
    722 	RF_Raid_t *raidPtr;
    723 {
    724 #if RF_DEBUG_QUIESCE
    725 	if (rf_quiesceDebug) {
    726 		printf("raid%d: Signalling quiescence lock\n",
    727 		       raidPtr->raidid);
    728 	}
    729 #endif
    730 	raidPtr->access_suspend_release = 1;
    731 
    732 	if (raidPtr->waiting_for_quiescence) {
    733 		SIGNAL_QUIESCENT_COND(raidPtr);
    734 	}
    735 }
    736 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
    737 int
    738 rf_SuspendNewRequestsAndWait(raidPtr)
    739 	RF_Raid_t *raidPtr;
    740 {
    741 #if RF_DEBUG_QUIESCE
    742 	if (rf_quiesceDebug)
    743 		printf("raid%d: Suspending new reqs\n", raidPtr->raidid);
    744 #endif
    745 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    746 	raidPtr->accesses_suspended++;
    747 	raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
    748 
    749 	if (raidPtr->waiting_for_quiescence) {
    750 		raidPtr->access_suspend_release = 0;
    751 		while (!raidPtr->access_suspend_release) {
    752 			printf("raid%d: Suspending: Waiting for Quiescence\n",
    753 			       raidPtr->raidid);
    754 			WAIT_FOR_QUIESCENCE(raidPtr);
    755 			raidPtr->waiting_for_quiescence = 0;
    756 		}
    757 	}
    758 	printf("raid%d: Quiescence reached..\n", raidPtr->raidid);
    759 
    760 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    761 	return (raidPtr->waiting_for_quiescence);
    762 }
    763 /* wake up everyone waiting for quiescence to be released */
    764 void
    765 rf_ResumeNewRequests(raidPtr)
    766 	RF_Raid_t *raidPtr;
    767 {
    768 	RF_CallbackDesc_t *t, *cb;
    769 
    770 #if RF_DEBUG_QUIESCE
    771 	if (rf_quiesceDebug)
    772 		printf("Resuming new reqs\n");
    773 #endif
    774 
    775 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    776 	raidPtr->accesses_suspended--;
    777 	if (raidPtr->accesses_suspended == 0)
    778 		cb = raidPtr->quiesce_wait_list;
    779 	else
    780 		cb = NULL;
    781 	raidPtr->quiesce_wait_list = NULL;
    782 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    783 
    784 	while (cb) {
    785 		t = cb;
    786 		cb = cb->next;
    787 		(t->callbackFunc) (t->callbackArg);
    788 		rf_FreeCallbackDesc(t);
    789 	}
    790 }
    791 /*****************************************************************************************
    792  *
    793  * debug routines
    794  *
    795  ****************************************************************************************/
    796 
    797 static void
    798 set_debug_option(name, val)
    799 	char   *name;
    800 	long    val;
    801 {
    802 	RF_DebugName_t *p;
    803 
    804 	for (p = rf_debugNames; p->name; p++) {
    805 		if (!strcmp(p->name, name)) {
    806 			*(p->ptr) = val;
    807 			printf("[Set debug variable %s to %ld]\n", name, val);
    808 			return;
    809 		}
    810 	}
    811 	RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
    812 }
    813 
    814 
    815 /* would like to use sscanf here, but apparently not available in kernel */
    816 /*ARGSUSED*/
    817 static void
    818 rf_ConfigureDebug(cfgPtr)
    819 	RF_Config_t *cfgPtr;
    820 {
    821 	char   *val_p, *name_p, *white_p;
    822 	long    val;
    823 	int     i;
    824 
    825 	rf_ResetDebugOptions();
    826 	for (i = 0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
    827 		name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
    828 		white_p = rf_find_white(name_p);	/* skip to start of 2nd
    829 							 * word */
    830 		val_p = rf_find_non_white(white_p);
    831 		if (*val_p == '0' && *(val_p + 1) == 'x')
    832 			val = rf_htoi(val_p + 2);
    833 		else
    834 			val = rf_atoi(val_p);
    835 		*white_p = '\0';
    836 		set_debug_option(name_p, val);
    837 	}
    838 }
    839 /* performance monitoring stuff */
    840 
    841 #define TIMEVAL_TO_US(t) (((long) t.tv_sec) * 1000000L + (long) t.tv_usec)
    842 
    843 #if !defined(_KERNEL) && !defined(SIMULATE)
    844 
    845 /*
    846  * Throughput stats currently only used in user-level RAIDframe
    847  */
    848 
    849 static int
    850 rf_InitThroughputStats(
    851     RF_ShutdownList_t ** listp,
    852     RF_Raid_t * raidPtr,
    853     RF_Config_t * cfgPtr)
    854 {
    855 	int     rc;
    856 
    857 	/* these used by user-level raidframe only */
    858 	rc = rf_create_managed_mutex(listp, &raidPtr->throughputstats.mutex);
    859 	if (rc) {
    860 		rf_print_unable_to_init_mutex(__FILE__, __LINE__, rc);
    861 		return (rc);
    862 	}
    863 	raidPtr->throughputstats.sum_io_us = 0;
    864 	raidPtr->throughputstats.num_ios = 0;
    865 	raidPtr->throughputstats.num_out_ios = 0;
    866 	return (0);
    867 }
    868 
    869 void
    870 rf_StartThroughputStats(RF_Raid_t * raidPtr)
    871 {
    872 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
    873 	raidPtr->throughputstats.num_ios++;
    874 	raidPtr->throughputstats.num_out_ios++;
    875 	if (raidPtr->throughputstats.num_out_ios == 1)
    876 		RF_GETTIME(raidPtr->throughputstats.start);
    877 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
    878 }
    879 
    880 static void
    881 rf_StopThroughputStats(RF_Raid_t * raidPtr)
    882 {
    883 	struct timeval diff;
    884 
    885 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
    886 	raidPtr->throughputstats.num_out_ios--;
    887 	if (raidPtr->throughputstats.num_out_ios == 0) {
    888 		RF_GETTIME(raidPtr->throughputstats.stop);
    889 		RF_TIMEVAL_DIFF(&raidPtr->throughputstats.start, &raidPtr->throughputstats.stop, &diff);
    890 		raidPtr->throughputstats.sum_io_us += TIMEVAL_TO_US(diff);
    891 	}
    892 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
    893 }
    894 
    895 static void
    896 rf_PrintThroughputStats(RF_Raid_t * raidPtr)
    897 {
    898 	RF_ASSERT(raidPtr->throughputstats.num_out_ios == 0);
    899 	if (raidPtr->throughputstats.sum_io_us != 0) {
    900 		printf("[Througphut: %8.2f IOs/second]\n", raidPtr->throughputstats.num_ios
    901 		    / (raidPtr->throughputstats.sum_io_us / 1000000.0));
    902 	}
    903 }
    904 #endif				/* !KERNEL && !SIMULATE */
    905 
    906 void
    907 rf_StartUserStats(RF_Raid_t * raidPtr)
    908 {
    909 	RF_GETTIME(raidPtr->userstats.start);
    910 	raidPtr->userstats.sum_io_us = 0;
    911 	raidPtr->userstats.num_ios = 0;
    912 	raidPtr->userstats.num_sect_moved = 0;
    913 }
    914 
    915 void
    916 rf_StopUserStats(RF_Raid_t * raidPtr)
    917 {
    918 	RF_GETTIME(raidPtr->userstats.stop);
    919 }
    920 
    921 void
    922 rf_UpdateUserStats(raidPtr, rt, numsect)
    923 	RF_Raid_t *raidPtr;
    924 	int     rt;		/* resp time in us */
    925 	int     numsect;	/* number of sectors for this access */
    926 {
    927 	raidPtr->userstats.sum_io_us += rt;
    928 	raidPtr->userstats.num_ios++;
    929 	raidPtr->userstats.num_sect_moved += numsect;
    930 }
    931 
    932 void
    933 rf_PrintUserStats(RF_Raid_t * raidPtr)
    934 {
    935 	long    elapsed_us, mbs, mbs_frac;
    936 	struct timeval diff;
    937 
    938 	RF_TIMEVAL_DIFF(&raidPtr->userstats.start,
    939 			&raidPtr->userstats.stop, &diff);
    940 	elapsed_us = TIMEVAL_TO_US(diff);
    941 
    942 	/* 2000 sectors per megabyte, 10000000 microseconds per second */
    943 	if (elapsed_us)
    944 		mbs = (raidPtr->userstats.num_sect_moved / 2000) /
    945 			(elapsed_us / 1000000);
    946 	else
    947 		mbs = 0;
    948 
    949 	/* this computes only the first digit of the fractional mb/s moved */
    950 	if (elapsed_us) {
    951 		mbs_frac = ((raidPtr->userstats.num_sect_moved / 200) /
    952 			    (elapsed_us / 1000000)) - (mbs * 10);
    953 	} else {
    954 		mbs_frac = 0;
    955 	}
    956 
    957 	printf("raid%d: Number of I/Os:             %ld\n",
    958 	       raidPtr->raidid, raidPtr->userstats.num_ios);
    959 	printf("raid%d: Elapsed time (us):          %ld\n",
    960 	       raidPtr->raidid, elapsed_us);
    961 	printf("raid%d: User I/Os per second:       %ld\n",
    962 	       raidPtr->raidid, RF_DB0_CHECK(raidPtr->userstats.num_ios,
    963 					     (elapsed_us / 1000000)));
    964 	printf("raid%d: Average user response time: %ld us\n",
    965 	       raidPtr->raidid, RF_DB0_CHECK(raidPtr->userstats.sum_io_us,
    966 					     raidPtr->userstats.num_ios));
    967 	printf("raid%d: Total sectors moved:        %ld\n",
    968 	       raidPtr->raidid, raidPtr->userstats.num_sect_moved);
    969 	printf("raid%d: Average access size (sect): %ld\n",
    970 	       raidPtr->raidid, RF_DB0_CHECK(raidPtr->userstats.num_sect_moved,
    971 					     raidPtr->userstats.num_ios));
    972 	printf("raid%d: Achieved data rate:         %ld.%ld MB/sec\n",
    973 	       raidPtr->raidid, mbs, mbs_frac);
    974 }
    975 
    976 
    977 void
    978 rf_print_panic_message(line,file)
    979 	int line;
    980 	char *file;
    981 {
    982 	sprintf(rf_panicbuf,"raidframe error at line %d file %s",
    983 		line, file);
    984 }
    985 
    986 #ifdef RAID_DIAGNOSTIC
    987 void
    988 rf_print_assert_panic_message(line,file,condition)
    989 	int line;
    990 	char *file;
    991 	char *condition;
    992 {
    993 	sprintf(rf_panicbuf,
    994 		"raidframe error at line %d file %s (failed asserting %s)\n",
    995 		line, file, condition);
    996 }
    997 #endif
    998 
    999 void
   1000 rf_print_unable_to_init_mutex(file,line,rc)
   1001 	char *file;
   1002 	int line;
   1003 	int rc;
   1004 {
   1005 	RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n",
   1006 		     file, line, rc);
   1007 }
   1008 
   1009 void
   1010 rf_print_unable_to_init_cond(file,line,rc)
   1011 	char *file;
   1012 	int line;
   1013 	int rc;
   1014 {
   1015 	RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n",
   1016 		     file, line, rc);
   1017 }
   1018 
   1019 void
   1020 rf_print_unable_to_add_shutdown(file,line,rc)
   1021 	char *file;
   1022 	int line;
   1023 	int rc;
   1024 {
   1025 	RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
   1026 		     file, line, rc);
   1027 }
   1028