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