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