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