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