Home | History | Annotate | Line # | Download | only in raidframe
rf_driver.c revision 1.59
      1 /*	$NetBSD: rf_driver.c,v 1.59 2002/09/15 19:25:07 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.59 2002/09/15 19:25:07 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->cleanupList = NULL;
    608 	rf_MakeAllocList(desc->cleanupList);
    609 	return (desc);
    610 }
    611 
    612 void
    613 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t * desc)
    614 {
    615 	RF_Raid_t *raidPtr = desc->raidPtr;
    616 
    617 	RF_ASSERT(desc);
    618 
    619 	rf_FreeAllocList(desc->cleanupList);
    620 	RF_FREELIST_FREE_CLEAN_NOUNLOCK(rf_rad_freelist, desc, next, clean_rad);
    621 	raidPtr->nAccOutstanding--;
    622 	if (raidPtr->waitShutdown) {
    623 		RF_SIGNAL_COND(raidPtr->outstandingCond);
    624 	}
    625 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    626 }
    627 /*********************************************************************
    628  * Main routine for performing an access.
    629  * Accesses are retried until a DAG can not be selected.  This occurs
    630  * when either the DAG library is incomplete or there are too many
    631  * failures in a parity group.
    632  ********************************************************************/
    633 int
    634 rf_DoAccess(
    635     RF_Raid_t * raidPtr,
    636     RF_IoType_t type,
    637     int async_flag,
    638     RF_RaidAddr_t raidAddress,
    639     RF_SectorCount_t numBlocks,
    640     caddr_t bufPtr,
    641     void *bp_in,
    642     RF_RaidAccessFlags_t flags)
    643 /*
    644 type should be read or write
    645 async_flag should be RF_TRUE or RF_FALSE
    646 bp_in is a buf pointer.  void * to facilitate ignoring it outside the kernel
    647 */
    648 {
    649 	RF_RaidAccessDesc_t *desc;
    650 	caddr_t lbufPtr = bufPtr;
    651 	struct buf *bp = (struct buf *) bp_in;
    652 
    653 	raidAddress += rf_raidSectorOffset;
    654 
    655 	if (rf_accessDebug) {
    656 
    657 		printf("logBytes is: %d %d %d\n", raidPtr->raidid,
    658 		    raidPtr->logBytesPerSector,
    659 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks));
    660 		printf("raid%d: %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", raidPtr->raidid,
    661 		    (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
    662 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
    663 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
    664 		    (int) numBlocks,
    665 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks),
    666 		    (long) bufPtr);
    667 	}
    668 	if (raidAddress + numBlocks > raidPtr->totalSectors) {
    669 
    670 		printf("DoAccess: raid addr %lu too large to access %lu sectors.  Max legal addr is %lu\n",
    671 		    (u_long) raidAddress, (u_long) numBlocks, (u_long) raidPtr->totalSectors);
    672 
    673 		IO_BUF_ERR(bp, ENOSPC);
    674 		return (ENOSPC);
    675 	}
    676 	desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
    677 	    numBlocks, lbufPtr, bp, flags, raidPtr->Layout.map->states);
    678 
    679 	if (desc == NULL) {
    680 		return (ENOMEM);
    681 	}
    682 	RF_ETIMER_START(desc->tracerec.tot_timer);
    683 
    684 	desc->async_flag = async_flag;
    685 
    686 	rf_ContinueRaidAccess(desc);
    687 
    688 	return (0);
    689 }
    690 #if 0
    691 /* force the array into reconfigured mode without doing reconstruction */
    692 int
    693 rf_SetReconfiguredMode(raidPtr, row, col)
    694 	RF_Raid_t *raidPtr;
    695 	int     row;
    696 	int     col;
    697 {
    698 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
    699 		printf("Can't set reconfigured mode in dedicated-spare array\n");
    700 		RF_PANIC();
    701 	}
    702 	RF_LOCK_MUTEX(raidPtr->mutex);
    703 	raidPtr->numFailures++;
    704 	raidPtr->Disks[row][col].status = rf_ds_dist_spared;
    705 	raidPtr->status[row] = rf_rs_reconfigured;
    706 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    707 	/* install spare table only if declustering + distributed sparing
    708 	 * architecture. */
    709 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
    710 		rf_InstallSpareTable(raidPtr, row, col);
    711 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    712 	return (0);
    713 }
    714 #endif
    715 
    716 int
    717 rf_FailDisk(
    718     RF_Raid_t * raidPtr,
    719     int frow,
    720     int fcol,
    721     int initRecon)
    722 {
    723 	printf("raid%d: Failing disk r%d c%d\n", raidPtr->raidid, frow, fcol);
    724 	RF_LOCK_MUTEX(raidPtr->mutex);
    725 	raidPtr->numFailures++;
    726 	raidPtr->Disks[frow][fcol].status = rf_ds_failed;
    727 	raidPtr->status[frow] = rf_rs_degraded;
    728 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    729 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    730 
    731 	/* Close the component, so that it's not "locked" if someone
    732 	   else want's to use it! */
    733 
    734 	rf_close_component(raidPtr, raidPtr->raid_cinfo[frow][fcol].ci_vp,
    735 			   raidPtr->Disks[frow][fcol].auto_configured);
    736 	raidPtr->raid_cinfo[frow][fcol].ci_vp = NULL;
    737 
    738 	/* Need to mark the component as not being auto_configured
    739 	   (in case it was previously). */
    740 
    741 	raidPtr->Disks[frow][fcol].auto_configured = 0;
    742 
    743 	if (initRecon)
    744 		rf_ReconstructFailedDisk(raidPtr, frow, fcol);
    745 	return (0);
    746 }
    747 /* releases a thread that is waiting for the array to become quiesced.
    748  * access_suspend_mutex should be locked upon calling this
    749  */
    750 void
    751 rf_SignalQuiescenceLock(raidPtr, reconDesc)
    752 	RF_Raid_t *raidPtr;
    753 	RF_RaidReconDesc_t *reconDesc;
    754 {
    755 	if (rf_quiesceDebug) {
    756 		printf("raid%d: Signalling quiescence lock\n",
    757 		       raidPtr->raidid);
    758 	}
    759 	raidPtr->access_suspend_release = 1;
    760 
    761 	if (raidPtr->waiting_for_quiescence) {
    762 		SIGNAL_QUIESCENT_COND(raidPtr);
    763 	}
    764 }
    765 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
    766 int
    767 rf_SuspendNewRequestsAndWait(raidPtr)
    768 	RF_Raid_t *raidPtr;
    769 {
    770 	if (rf_quiesceDebug)
    771 		printf("raid%d: Suspending new reqs\n", raidPtr->raidid);
    772 
    773 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    774 	raidPtr->accesses_suspended++;
    775 	raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
    776 
    777 	if (raidPtr->waiting_for_quiescence) {
    778 		raidPtr->access_suspend_release = 0;
    779 		while (!raidPtr->access_suspend_release) {
    780 			printf("raid%d: Suspending: Waiting for Quiescence\n",
    781 			       raidPtr->raidid);
    782 			WAIT_FOR_QUIESCENCE(raidPtr);
    783 			raidPtr->waiting_for_quiescence = 0;
    784 		}
    785 	}
    786 	printf("raid%d: Quiescence reached..\n", raidPtr->raidid);
    787 
    788 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    789 	return (raidPtr->waiting_for_quiescence);
    790 }
    791 /* wake up everyone waiting for quiescence to be released */
    792 void
    793 rf_ResumeNewRequests(raidPtr)
    794 	RF_Raid_t *raidPtr;
    795 {
    796 	RF_CallbackDesc_t *t, *cb;
    797 
    798 	if (rf_quiesceDebug)
    799 		printf("Resuming new reqs\n");
    800 
    801 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    802 	raidPtr->accesses_suspended--;
    803 	if (raidPtr->accesses_suspended == 0)
    804 		cb = raidPtr->quiesce_wait_list;
    805 	else
    806 		cb = NULL;
    807 	raidPtr->quiesce_wait_list = NULL;
    808 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    809 
    810 	while (cb) {
    811 		t = cb;
    812 		cb = cb->next;
    813 		(t->callbackFunc) (t->callbackArg);
    814 		rf_FreeCallbackDesc(t);
    815 	}
    816 }
    817 /*****************************************************************************************
    818  *
    819  * debug routines
    820  *
    821  ****************************************************************************************/
    822 
    823 static void
    824 set_debug_option(name, val)
    825 	char   *name;
    826 	long    val;
    827 {
    828 	RF_DebugName_t *p;
    829 
    830 	for (p = rf_debugNames; p->name; p++) {
    831 		if (!strcmp(p->name, name)) {
    832 			*(p->ptr) = val;
    833 			printf("[Set debug variable %s to %ld]\n", name, val);
    834 			return;
    835 		}
    836 	}
    837 	RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
    838 }
    839 
    840 
    841 /* would like to use sscanf here, but apparently not available in kernel */
    842 /*ARGSUSED*/
    843 static void
    844 rf_ConfigureDebug(cfgPtr)
    845 	RF_Config_t *cfgPtr;
    846 {
    847 	char   *val_p, *name_p, *white_p;
    848 	long    val;
    849 	int     i;
    850 
    851 	rf_ResetDebugOptions();
    852 	for (i = 0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
    853 		name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
    854 		white_p = rf_find_white(name_p);	/* skip to start of 2nd
    855 							 * word */
    856 		val_p = rf_find_non_white(white_p);
    857 		if (*val_p == '0' && *(val_p + 1) == 'x')
    858 			val = rf_htoi(val_p + 2);
    859 		else
    860 			val = rf_atoi(val_p);
    861 		*white_p = '\0';
    862 		set_debug_option(name_p, val);
    863 	}
    864 }
    865 /* performance monitoring stuff */
    866 
    867 #define TIMEVAL_TO_US(t) (((long) t.tv_sec) * 1000000L + (long) t.tv_usec)
    868 
    869 #if !defined(_KERNEL) && !defined(SIMULATE)
    870 
    871 /*
    872  * Throughput stats currently only used in user-level RAIDframe
    873  */
    874 
    875 static int
    876 rf_InitThroughputStats(
    877     RF_ShutdownList_t ** listp,
    878     RF_Raid_t * raidPtr,
    879     RF_Config_t * cfgPtr)
    880 {
    881 	int     rc;
    882 
    883 	/* these used by user-level raidframe only */
    884 	rc = rf_create_managed_mutex(listp, &raidPtr->throughputstats.mutex);
    885 	if (rc) {
    886 		rf_print_unable_to_init_mutex(__FILE__, __LINE__, rc);
    887 		return (rc);
    888 	}
    889 	raidPtr->throughputstats.sum_io_us = 0;
    890 	raidPtr->throughputstats.num_ios = 0;
    891 	raidPtr->throughputstats.num_out_ios = 0;
    892 	return (0);
    893 }
    894 
    895 void
    896 rf_StartThroughputStats(RF_Raid_t * raidPtr)
    897 {
    898 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
    899 	raidPtr->throughputstats.num_ios++;
    900 	raidPtr->throughputstats.num_out_ios++;
    901 	if (raidPtr->throughputstats.num_out_ios == 1)
    902 		RF_GETTIME(raidPtr->throughputstats.start);
    903 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
    904 }
    905 
    906 static void
    907 rf_StopThroughputStats(RF_Raid_t * raidPtr)
    908 {
    909 	struct timeval diff;
    910 
    911 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
    912 	raidPtr->throughputstats.num_out_ios--;
    913 	if (raidPtr->throughputstats.num_out_ios == 0) {
    914 		RF_GETTIME(raidPtr->throughputstats.stop);
    915 		RF_TIMEVAL_DIFF(&raidPtr->throughputstats.start, &raidPtr->throughputstats.stop, &diff);
    916 		raidPtr->throughputstats.sum_io_us += TIMEVAL_TO_US(diff);
    917 	}
    918 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
    919 }
    920 
    921 static void
    922 rf_PrintThroughputStats(RF_Raid_t * raidPtr)
    923 {
    924 	RF_ASSERT(raidPtr->throughputstats.num_out_ios == 0);
    925 	if (raidPtr->throughputstats.sum_io_us != 0) {
    926 		printf("[Througphut: %8.2f IOs/second]\n", raidPtr->throughputstats.num_ios
    927 		    / (raidPtr->throughputstats.sum_io_us / 1000000.0));
    928 	}
    929 }
    930 #endif				/* !KERNEL && !SIMULATE */
    931 
    932 void
    933 rf_StartUserStats(RF_Raid_t * raidPtr)
    934 {
    935 	RF_GETTIME(raidPtr->userstats.start);
    936 	raidPtr->userstats.sum_io_us = 0;
    937 	raidPtr->userstats.num_ios = 0;
    938 	raidPtr->userstats.num_sect_moved = 0;
    939 }
    940 
    941 void
    942 rf_StopUserStats(RF_Raid_t * raidPtr)
    943 {
    944 	RF_GETTIME(raidPtr->userstats.stop);
    945 }
    946 
    947 void
    948 rf_UpdateUserStats(raidPtr, rt, numsect)
    949 	RF_Raid_t *raidPtr;
    950 	int     rt;		/* resp time in us */
    951 	int     numsect;	/* number of sectors for this access */
    952 {
    953 	raidPtr->userstats.sum_io_us += rt;
    954 	raidPtr->userstats.num_ios++;
    955 	raidPtr->userstats.num_sect_moved += numsect;
    956 }
    957 
    958 void
    959 rf_PrintUserStats(RF_Raid_t * raidPtr)
    960 {
    961 	long    elapsed_us, mbs, mbs_frac;
    962 	struct timeval diff;
    963 
    964 	RF_TIMEVAL_DIFF(&raidPtr->userstats.start, &raidPtr->userstats.stop, &diff);
    965 	elapsed_us = TIMEVAL_TO_US(diff);
    966 
    967 	/* 2000 sectors per megabyte, 10000000 microseconds per second */
    968 	if (elapsed_us)
    969 		mbs = (raidPtr->userstats.num_sect_moved / 2000) / (elapsed_us / 1000000);
    970 	else
    971 		mbs = 0;
    972 
    973 	/* this computes only the first digit of the fractional mb/s moved */
    974 	if (elapsed_us) {
    975 		mbs_frac = ((raidPtr->userstats.num_sect_moved / 200) / (elapsed_us / 1000000))
    976 		    - (mbs * 10);
    977 	} else {
    978 		mbs_frac = 0;
    979 	}
    980 
    981 	printf("Number of I/Os:             %ld\n", raidPtr->userstats.num_ios);
    982 	printf("Elapsed time (us):          %ld\n", elapsed_us);
    983 	printf("User I/Os per second:       %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_ios, (elapsed_us / 1000000)));
    984 	printf("Average user response time: %ld us\n", RF_DB0_CHECK(raidPtr->userstats.sum_io_us, raidPtr->userstats.num_ios));
    985 	printf("Total sectors moved:        %ld\n", raidPtr->userstats.num_sect_moved);
    986 	printf("Average access size (sect): %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_sect_moved, raidPtr->userstats.num_ios));
    987 	printf("Achieved data rate:         %ld.%ld MB/sec\n", mbs, mbs_frac);
    988 }
    989 
    990 
    991 void
    992 rf_print_panic_message(line,file)
    993 	int line;
    994 	char *file;
    995 {
    996 	sprintf(rf_panicbuf,"raidframe error at line %d file %s",
    997 		line, file);
    998 }
    999 
   1000 void
   1001 rf_print_assert_panic_message(line,file,condition)
   1002 	int line;
   1003 	char *file;
   1004 	char *condition;
   1005 {
   1006 	sprintf(rf_panicbuf,
   1007 		"raidframe error at line %d file %s (failed asserting %s)\n",
   1008 		line, file, condition);
   1009 }
   1010 
   1011 void
   1012 rf_print_unable_to_init_mutex(file,line,rc)
   1013 	char *file;
   1014 	int line;
   1015 	int rc;
   1016 {
   1017 	RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n",
   1018 		     file, line, rc);
   1019 }
   1020 
   1021 void
   1022 rf_print_unable_to_init_cond(file,line,rc)
   1023 	char *file;
   1024 	int line;
   1025 	int rc;
   1026 {
   1027 	RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n",
   1028 		     file, line, rc);
   1029 }
   1030 
   1031 void
   1032 rf_print_unable_to_add_shutdown(file,line,rc)
   1033 	char *file;
   1034 	int line;
   1035 	int rc;
   1036 {
   1037 	RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
   1038 		     file, line, rc);
   1039 }
   1040