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