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