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