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