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