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