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