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