Home | History | Annotate | Line # | Download | only in raidframe
rf_driver.c revision 1.34
      1 /*	$NetBSD: rf_driver.c,v 1.34 2000/03/07 02:28:05 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/types.h>
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/ioctl.h>
     79 #include <sys/fcntl.h>
     80 #include <sys/vnode.h>
     81 
     82 
     83 #include "rf_archs.h"
     84 #include "rf_threadstuff.h"
     85 
     86 #include <sys/errno.h>
     87 
     88 #include "rf_raid.h"
     89 #include "rf_dag.h"
     90 #include "rf_aselect.h"
     91 #include "rf_diskqueue.h"
     92 #include "rf_parityscan.h"
     93 #include "rf_alloclist.h"
     94 #include "rf_dagutils.h"
     95 #include "rf_utils.h"
     96 #include "rf_etimer.h"
     97 #include "rf_acctrace.h"
     98 #include "rf_configure.h"
     99 #include "rf_general.h"
    100 #include "rf_desc.h"
    101 #include "rf_states.h"
    102 #include "rf_freelist.h"
    103 #include "rf_decluster.h"
    104 #include "rf_map.h"
    105 #include "rf_revent.h"
    106 #include "rf_callback.h"
    107 #include "rf_engine.h"
    108 #include "rf_memchunk.h"
    109 #include "rf_mcpair.h"
    110 #include "rf_nwayxor.h"
    111 #include "rf_debugprint.h"
    112 #include "rf_copyback.h"
    113 #include "rf_driver.h"
    114 #include "rf_options.h"
    115 #include "rf_shutdown.h"
    116 #include "rf_kintf.h"
    117 
    118 #include <sys/buf.h>
    119 
    120 /* rad == RF_RaidAccessDesc_t */
    121 static RF_FreeList_t *rf_rad_freelist;
    122 #define RF_MAX_FREE_RAD 128
    123 #define RF_RAD_INC       16
    124 #define RF_RAD_INITIAL   32
    125 
    126 /* debug variables */
    127 char    rf_panicbuf[2048];	/* a buffer to hold an error msg when we panic */
    128 
    129 /* main configuration routines */
    130 static int raidframe_booted = 0;
    131 
    132 static void rf_ConfigureDebug(RF_Config_t * cfgPtr);
    133 static void set_debug_option(char *name, long val);
    134 static void rf_UnconfigureArray(void);
    135 static int init_rad(RF_RaidAccessDesc_t *);
    136 static void clean_rad(RF_RaidAccessDesc_t *);
    137 static void rf_ShutdownRDFreeList(void *);
    138 static int rf_ConfigureRDFreeList(RF_ShutdownList_t **);
    139 
    140 RF_DECLARE_MUTEX(rf_printf_mutex)	/* debug only:  avoids interleaved
    141 					 * printfs by different stripes */
    142 
    143 #define SIGNAL_QUIESCENT_COND(_raid_)  wakeup(&((_raid_)->accesses_suspended))
    144 #define WAIT_FOR_QUIESCENCE(_raid_) \
    145 	tsleep(&((_raid_)->accesses_suspended),PRIBIO,"raidframe quiesce", 0);
    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_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_mutex_init(&configureMutex);
    175 	if (rc) {
    176 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    177 		    __LINE__, rc);
    178 		RF_PANIC();
    179 	}
    180 	configureCount = 0;
    181 	isconfigged = 0;
    182 	globalShutdown = NULL;
    183 	return (0);
    184 }
    185 /*
    186  * This function is really just for debugging user-level stuff: it
    187  * frees up all memory, other RAIDframe resources which might otherwise
    188  * be kept around. This is used with systems like "sentinel" to detect
    189  * memory leaks.
    190  */
    191 int
    192 rf_UnbootRaidframe()
    193 {
    194 	int     rc;
    195 
    196 	RF_LOCK_MUTEX(configureMutex);
    197 	if (configureCount) {
    198 		RF_UNLOCK_MUTEX(configureMutex);
    199 		return (EBUSY);
    200 	}
    201 	raidframe_booted = 0;
    202 	RF_UNLOCK_MUTEX(configureMutex);
    203 	rc = rf_mutex_destroy(&configureMutex);
    204 	if (rc) {
    205 		RF_ERRORMSG3("Unable to destroy mutex file %s line %d rc=%d\n", __FILE__,
    206 		    __LINE__, rc);
    207 		RF_PANIC();
    208 	}
    209 	return (0);
    210 }
    211 /*
    212  * Called whenever an array is shutdown
    213  */
    214 static void
    215 rf_UnconfigureArray()
    216 {
    217 	int     rc;
    218 
    219 	RF_LOCK_MUTEX(configureMutex);
    220 	if (--configureCount == 0) {	/* if no active configurations, shut
    221 					 * everything down */
    222 		isconfigged = 0;
    223 
    224 		rc = rf_ShutdownList(&globalShutdown);
    225 		if (rc) {
    226 			RF_ERRORMSG1("RAIDFRAME: unable to do global shutdown, rc=%d\n", rc);
    227 		}
    228 
    229 		/*
    230 	         * We must wait until now, because the AllocList module
    231 	         * uses the DebugMem module.
    232 	         */
    233 		if (rf_memDebug)
    234 			rf_print_unfreed();
    235 	}
    236 	RF_UNLOCK_MUTEX(configureMutex);
    237 }
    238 
    239 /*
    240  * Called to shut down an array.
    241  */
    242 int
    243 rf_Shutdown(raidPtr)
    244 	RF_Raid_t *raidPtr;
    245 {
    246 
    247 	if (!raidPtr->valid) {
    248 		RF_ERRORMSG("Attempt to shut down unconfigured RAIDframe driver.  Aborting shutdown\n");
    249 		return (EINVAL);
    250 	}
    251 	/*
    252          * wait for outstanding IOs to land
    253          * As described in rf_raid.h, we use the rad_freelist lock
    254          * to protect the per-array info about outstanding descs
    255          * since we need to do freelist locking anyway, and this
    256          * cuts down on the amount of serialization we've got going
    257          * on.
    258          */
    259 	RF_FREELIST_DO_LOCK(rf_rad_freelist);
    260 	if (raidPtr->waitShutdown) {
    261 		RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    262 		return (EBUSY);
    263 	}
    264 	raidPtr->waitShutdown = 1;
    265 	while (raidPtr->nAccOutstanding) {
    266 		RF_WAIT_COND(raidPtr->outstandingCond, RF_FREELIST_MUTEX_OF(rf_rad_freelist));
    267 	}
    268 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    269 
    270 	raidPtr->valid = 0;
    271 
    272 	rf_final_update_component_labels(raidPtr);
    273 
    274 	rf_UnconfigureVnodes(raidPtr);
    275 
    276 	rf_ShutdownList(&raidPtr->shutdownList);
    277 
    278 	rf_UnconfigureArray();
    279 
    280 	return (0);
    281 }
    282 
    283 
    284 #define DO_INIT_CONFIGURE(f) { \
    285 	rc = f (&globalShutdown); \
    286 	if (rc) { \
    287 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    288 		rf_ShutdownList(&globalShutdown); \
    289 		configureCount--; \
    290 		RF_UNLOCK_MUTEX(configureMutex); \
    291 		return(rc); \
    292 	} \
    293 }
    294 
    295 #define DO_RAID_FAIL() { \
    296 	rf_UnconfigureVnodes(raidPtr); \
    297 	rf_ShutdownList(&raidPtr->shutdownList); \
    298 	rf_UnconfigureArray(); \
    299 }
    300 
    301 #define DO_RAID_INIT_CONFIGURE(f) { \
    302 	rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
    303 	if (rc) { \
    304 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    305 		DO_RAID_FAIL(); \
    306 		return(rc); \
    307 	} \
    308 }
    309 
    310 #define DO_RAID_MUTEX(_m_) { \
    311 	rc = rf_create_managed_mutex(&raidPtr->shutdownList, (_m_)); \
    312 	if (rc) { \
    313 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", \
    314 			__FILE__, __LINE__, rc); \
    315 		DO_RAID_FAIL(); \
    316 		return(rc); \
    317 	} \
    318 }
    319 
    320 #define DO_RAID_COND(_c_) { \
    321 	rc = rf_create_managed_cond(&raidPtr->shutdownList, (_c_)); \
    322 	if (rc) { \
    323 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", \
    324 			__FILE__, __LINE__, rc); \
    325 		DO_RAID_FAIL(); \
    326 		return(rc); \
    327 	} \
    328 }
    329 
    330 int
    331 rf_Configure(raidPtr, cfgPtr, ac)
    332 	RF_Raid_t *raidPtr;
    333 	RF_Config_t *cfgPtr;
    334 	RF_AutoConfig_t *ac;
    335 {
    336 	RF_RowCol_t row, col;
    337 	int     i, rc;
    338 
    339 	/* XXX This check can probably be removed now, since
    340 	   RAIDFRAME_CONFIGURRE now checks to make sure that the
    341 	   RAID set is not already valid
    342 	*/
    343 	if (raidPtr->valid) {
    344 		RF_ERRORMSG("RAIDframe configuration not shut down.  Aborting configure.\n");
    345 		return (EINVAL);
    346 	}
    347 	RF_LOCK_MUTEX(configureMutex);
    348 	configureCount++;
    349 	if (isconfigged == 0) {
    350 		rc = rf_create_managed_mutex(&globalShutdown, &rf_printf_mutex);
    351 		if (rc) {
    352 			RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    353 			    __LINE__, rc);
    354 			rf_ShutdownList(&globalShutdown);
    355 			return (rc);
    356 		}
    357 		/* initialize globals */
    358 		printf("RAIDFRAME: protectedSectors is %ld\n",
    359 		       rf_protectedSectors);
    360 
    361 		rf_clear_debug_print_buffer();
    362 
    363 		DO_INIT_CONFIGURE(rf_ConfigureAllocList);
    364 
    365 		/*
    366 	         * Yes, this does make debugging general to the whole
    367 	         * system instead of being array specific. Bummer, drag.
    368 		 */
    369 		rf_ConfigureDebug(cfgPtr);
    370 		DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
    371 		DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
    372 		DO_INIT_CONFIGURE(rf_ConfigureMapModule);
    373 		DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
    374 		DO_INIT_CONFIGURE(rf_ConfigureCallback);
    375 		DO_INIT_CONFIGURE(rf_ConfigureMemChunk);
    376 		DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
    377 		DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
    378 		DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
    379 		DO_INIT_CONFIGURE(rf_ConfigureMCPair);
    380 		DO_INIT_CONFIGURE(rf_ConfigureDAGs);
    381 		DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
    382 		DO_INIT_CONFIGURE(rf_ConfigureDebugPrint);
    383 		DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
    384 		DO_INIT_CONFIGURE(rf_ConfigureCopyback);
    385 		DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
    386 		isconfigged = 1;
    387 	}
    388 	RF_UNLOCK_MUTEX(configureMutex);
    389 
    390 	DO_RAID_MUTEX(&raidPtr->mutex);
    391 	/* set up the cleanup list.  Do this after ConfigureDebug so that
    392 	 * value of memDebug will be set */
    393 
    394 	rf_MakeAllocList(raidPtr->cleanupList);
    395 	if (raidPtr->cleanupList == NULL) {
    396 		DO_RAID_FAIL();
    397 		return (ENOMEM);
    398 	}
    399 	rc = rf_ShutdownCreate(&raidPtr->shutdownList,
    400 	    (void (*) (void *)) rf_FreeAllocList,
    401 	    raidPtr->cleanupList);
    402 	if (rc) {
    403 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
    404 		    __FILE__, __LINE__, rc);
    405 		DO_RAID_FAIL();
    406 		return (rc);
    407 	}
    408 	raidPtr->numRow = cfgPtr->numRow;
    409 	raidPtr->numCol = cfgPtr->numCol;
    410 	raidPtr->numSpare = cfgPtr->numSpare;
    411 
    412 	/* XXX we don't even pretend to support more than one row in the
    413 	 * kernel... */
    414 	if (raidPtr->numRow != 1) {
    415 		RF_ERRORMSG("Only one row supported in kernel.\n");
    416 		DO_RAID_FAIL();
    417 		return (EINVAL);
    418 	}
    419 	RF_CallocAndAdd(raidPtr->status, raidPtr->numRow, sizeof(RF_RowStatus_t),
    420 	    (RF_RowStatus_t *), raidPtr->cleanupList);
    421 	if (raidPtr->status == NULL) {
    422 		DO_RAID_FAIL();
    423 		return (ENOMEM);
    424 	}
    425 	RF_CallocAndAdd(raidPtr->reconControl, raidPtr->numRow,
    426 	    sizeof(RF_ReconCtrl_t *), (RF_ReconCtrl_t **), raidPtr->cleanupList);
    427 	if (raidPtr->reconControl == NULL) {
    428 		DO_RAID_FAIL();
    429 		return (ENOMEM);
    430 	}
    431 	for (i = 0; i < raidPtr->numRow; i++) {
    432 		raidPtr->status[i] = rf_rs_optimal;
    433 		raidPtr->reconControl[i] = NULL;
    434 	}
    435 
    436 	DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
    437 	DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
    438 
    439 	DO_RAID_COND(&raidPtr->outstandingCond);
    440 
    441 	raidPtr->nAccOutstanding = 0;
    442 	raidPtr->waitShutdown = 0;
    443 
    444 	DO_RAID_MUTEX(&raidPtr->access_suspend_mutex);
    445 	DO_RAID_COND(&raidPtr->quiescent_cond);
    446 
    447 	DO_RAID_COND(&raidPtr->waitForReconCond);
    448 
    449 	DO_RAID_MUTEX(&raidPtr->recon_done_proc_mutex);
    450 
    451 	if (ac!=NULL) {
    452 		/* We have an AutoConfig structure..  Don't do the
    453 		   normal disk configuration... call the auto config
    454 		   stuff */
    455 		rf_AutoConfigureDisks(raidPtr, cfgPtr, ac);
    456 	} else {
    457 		DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
    458 		DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
    459 	}
    460 	/* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
    461 	 * no. is set */
    462 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
    463 
    464 	DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
    465 
    466 	DO_RAID_INIT_CONFIGURE(rf_ConfigurePSStatus);
    467 
    468 	for (row = 0; row < raidPtr->numRow; row++) {
    469 		for (col = 0; col < raidPtr->numCol; col++) {
    470 			/*
    471 		         * XXX better distribution
    472 		         */
    473 			raidPtr->hist_diskreq[row][col] = 0;
    474 		}
    475 	}
    476 
    477 	raidPtr->numNewFailures = 0;
    478 	raidPtr->copyback_in_progress = 0;
    479 	raidPtr->parity_rewrite_in_progress = 0;
    480 	raidPtr->recon_in_progress = 0;
    481 	raidPtr->maxOutstanding = cfgPtr->maxOutstandingDiskReqs;
    482 
    483 	/* autoconfigure and root_partition will actually get filled in
    484 	   after the config is done */
    485 	raidPtr->autoconfigure = 0;
    486 	raidPtr->root_partition = 0;
    487 	raidPtr->last_unit = raidPtr->raidid;
    488 	raidPtr->config_order = 0;
    489 
    490 	if (rf_keepAccTotals) {
    491 		raidPtr->keep_acc_totals = 1;
    492 	}
    493 	rf_StartUserStats(raidPtr);
    494 
    495 	raidPtr->valid = 1;
    496 	return (0);
    497 }
    498 
    499 static int
    500 init_rad(desc)
    501 	RF_RaidAccessDesc_t *desc;
    502 {
    503 	int     rc;
    504 
    505 	rc = rf_mutex_init(&desc->mutex);
    506 	if (rc) {
    507 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    508 		    __LINE__, rc);
    509 		return (rc);
    510 	}
    511 	rc = rf_cond_init(&desc->cond);
    512 	if (rc) {
    513 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
    514 		    __LINE__, rc);
    515 		rf_mutex_destroy(&desc->mutex);
    516 		return (rc);
    517 	}
    518 	return (0);
    519 }
    520 
    521 static void
    522 clean_rad(desc)
    523 	RF_RaidAccessDesc_t *desc;
    524 {
    525 	rf_mutex_destroy(&desc->mutex);
    526 	rf_cond_destroy(&desc->cond);
    527 }
    528 
    529 static void
    530 rf_ShutdownRDFreeList(ignored)
    531 	void   *ignored;
    532 {
    533 	RF_FREELIST_DESTROY_CLEAN(rf_rad_freelist, next, (RF_RaidAccessDesc_t *), clean_rad);
    534 }
    535 
    536 static int
    537 rf_ConfigureRDFreeList(listp)
    538 	RF_ShutdownList_t **listp;
    539 {
    540 	int     rc;
    541 
    542 	RF_FREELIST_CREATE(rf_rad_freelist, RF_MAX_FREE_RAD,
    543 	    RF_RAD_INC, sizeof(RF_RaidAccessDesc_t));
    544 	if (rf_rad_freelist == NULL) {
    545 		return (ENOMEM);
    546 	}
    547 	rc = rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
    548 	if (rc) {
    549 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n", __FILE__,
    550 		    __LINE__, rc);
    551 		rf_ShutdownRDFreeList(NULL);
    552 		return (rc);
    553 	}
    554 	RF_FREELIST_PRIME_INIT(rf_rad_freelist, RF_RAD_INITIAL, next,
    555 	    (RF_RaidAccessDesc_t *), init_rad);
    556 	return (0);
    557 }
    558 
    559 RF_RaidAccessDesc_t *
    560 rf_AllocRaidAccDesc(
    561     RF_Raid_t * raidPtr,
    562     RF_IoType_t type,
    563     RF_RaidAddr_t raidAddress,
    564     RF_SectorCount_t numBlocks,
    565     caddr_t bufPtr,
    566     void *bp,
    567     RF_DagHeader_t ** paramDAG,
    568     RF_AccessStripeMapHeader_t ** paramASM,
    569     RF_RaidAccessFlags_t flags,
    570     void (*cbF) (struct buf *),
    571     void *cbA,
    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 = paramDAG;
    596 	desc->paramASM = paramASM;
    597 	desc->flags = flags;
    598 	desc->states = states;
    599 	desc->state = 0;
    600 
    601 	desc->status = 0;
    602 	bzero((char *) &desc->tracerec, sizeof(RF_AccTraceEntry_t));
    603 	desc->callbackFunc = (void (*) (RF_CBParam_t)) cbF;	/* XXX */
    604 	desc->callbackArg = cbA;
    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_DagHeader_t ** paramDAG,
    644     RF_AccessStripeMapHeader_t ** paramASM,
    645     RF_RaidAccessFlags_t flags,
    646     RF_RaidAccessDesc_t ** paramDesc,
    647     void (*cbF) (struct buf *),
    648     void *cbA)
    649 /*
    650 type should be read or write
    651 async_flag should be RF_TRUE or RF_FALSE
    652 bp_in is a buf pointer.  void * to facilitate ignoring it outside the kernel
    653 */
    654 {
    655 	RF_RaidAccessDesc_t *desc;
    656 	caddr_t lbufPtr = bufPtr;
    657 	struct buf *bp = (struct buf *) bp_in;
    658 
    659 	raidAddress += rf_raidSectorOffset;
    660 
    661 	if (!raidPtr->valid) {
    662 		RF_ERRORMSG("RAIDframe driver not successfully configured.  Rejecting access.\n");
    663 		IO_BUF_ERR(bp, EINVAL);
    664 		return (EINVAL);
    665 	}
    666 
    667 	if (rf_accessDebug) {
    668 
    669 		printf("logBytes is: %d %d %d\n", raidPtr->raidid,
    670 		    raidPtr->logBytesPerSector,
    671 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks));
    672 		printf("raid%d: %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", raidPtr->raidid,
    673 		    (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
    674 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
    675 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
    676 		    (int) numBlocks,
    677 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks),
    678 		    (long) bufPtr);
    679 	}
    680 	if (raidAddress + numBlocks > raidPtr->totalSectors) {
    681 
    682 		printf("DoAccess: raid addr %lu too large to access %lu sectors.  Max legal addr is %lu\n",
    683 		    (u_long) raidAddress, (u_long) numBlocks, (u_long) raidPtr->totalSectors);
    684 
    685 		IO_BUF_ERR(bp, ENOSPC);
    686 		return (ENOSPC);
    687 	}
    688 	desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
    689 	    numBlocks, lbufPtr, bp, paramDAG, paramASM,
    690 	    flags, cbF, cbA, raidPtr->Layout.map->states);
    691 
    692 	if (desc == NULL) {
    693 		return (ENOMEM);
    694 	}
    695 	RF_ETIMER_START(desc->tracerec.tot_timer);
    696 
    697 	desc->async_flag = async_flag;
    698 
    699 	rf_ContinueRaidAccess(desc);
    700 
    701 	return (0);
    702 }
    703 /* force the array into reconfigured mode without doing reconstruction */
    704 int
    705 rf_SetReconfiguredMode(raidPtr, row, col)
    706 	RF_Raid_t *raidPtr;
    707 	int     row;
    708 	int     col;
    709 {
    710 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
    711 		printf("Can't set reconfigured mode in dedicated-spare array\n");
    712 		RF_PANIC();
    713 	}
    714 	RF_LOCK_MUTEX(raidPtr->mutex);
    715 	raidPtr->numFailures++;
    716 	raidPtr->Disks[row][col].status = rf_ds_dist_spared;
    717 	raidPtr->status[row] = rf_rs_reconfigured;
    718 	rf_update_component_labels(raidPtr);
    719 	/* install spare table only if declustering + distributed sparing
    720 	 * architecture. */
    721 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
    722 		rf_InstallSpareTable(raidPtr, row, col);
    723 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    724 	return (0);
    725 }
    726 
    727 extern int fail_row, fail_col, fail_time;
    728 extern int delayed_recon;
    729 
    730 int
    731 rf_FailDisk(
    732     RF_Raid_t * raidPtr,
    733     int frow,
    734     int fcol,
    735     int initRecon)
    736 {
    737 	printf("raid%d: Failing disk r%d c%d\n", raidPtr->raidid, frow, fcol);
    738 	RF_LOCK_MUTEX(raidPtr->mutex);
    739 	raidPtr->numFailures++;
    740 	raidPtr->Disks[frow][fcol].status = rf_ds_failed;
    741 	raidPtr->status[frow] = rf_rs_degraded;
    742 	rf_update_component_labels(raidPtr);
    743 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    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("Suspending new reqs\n");
    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("Suspending: Waiting for Quiesence\n");
    782 			WAIT_FOR_QUIESCENCE(raidPtr);
    783 			raidPtr->waiting_for_quiescence = 0;
    784 		}
    785 	}
    786 	printf("Quiesence reached..\n");
    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_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    887 		    __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