Home | History | Annotate | Line # | Download | only in raidframe
rf_driver.c revision 1.53
      1 /*	$NetBSD: rf_driver.c,v 1.53 2002/08/03 01:24:03 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.53 2002/08/03 01:24:03 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_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 
    140 RF_DECLARE_MUTEX(rf_printf_mutex)	/* debug only:  avoids interleaved
    141 					 * printfs by different stripes */
    142 
    143 #define SIGNAL_QUIESCENT_COND(_raid_)  wakeup(&((_raid_)->accesses_suspended))
    144 #define WAIT_FOR_QUIESCENCE(_raid_) \
    145 	ltsleep(&((_raid_)->accesses_suspended), PRIBIO, \
    146 		"raidframe quiesce", 0, &((_raid_)->access_suspend_mutex))
    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 	/* Wait for any parity re-writes to stop... */
    272 	while (raidPtr->parity_rewrite_in_progress) {
    273 		printf("Waiting for parity re-write to exit...\n");
    274 		tsleep(&raidPtr->parity_rewrite_in_progress, PRIBIO,
    275 		       "rfprwshutdown", 0);
    276 	}
    277 
    278 	raidPtr->valid = 0;
    279 
    280 	rf_update_component_labels(raidPtr, RF_FINAL_COMPONENT_UPDATE);
    281 
    282 	rf_UnconfigureVnodes(raidPtr);
    283 
    284 	rf_ShutdownList(&raidPtr->shutdownList);
    285 
    286 	rf_UnconfigureArray();
    287 
    288 	return (0);
    289 }
    290 
    291 
    292 #define DO_INIT_CONFIGURE(f) { \
    293 	rc = f (&globalShutdown); \
    294 	if (rc) { \
    295 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    296 		rf_ShutdownList(&globalShutdown); \
    297 		configureCount--; \
    298 		RF_UNLOCK_MUTEX(configureMutex); \
    299 		return(rc); \
    300 	} \
    301 }
    302 
    303 #define DO_RAID_FAIL() { \
    304 	rf_UnconfigureVnodes(raidPtr); \
    305 	rf_ShutdownList(&raidPtr->shutdownList); \
    306 	rf_UnconfigureArray(); \
    307 }
    308 
    309 #define DO_RAID_INIT_CONFIGURE(f) { \
    310 	rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
    311 	if (rc) { \
    312 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    313 		DO_RAID_FAIL(); \
    314 		return(rc); \
    315 	} \
    316 }
    317 
    318 #define DO_RAID_MUTEX(_m_) { \
    319 	rc = rf_create_managed_mutex(&raidPtr->shutdownList, (_m_)); \
    320 	if (rc) { \
    321 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", \
    322 			__FILE__, __LINE__, rc); \
    323 		DO_RAID_FAIL(); \
    324 		return(rc); \
    325 	} \
    326 }
    327 
    328 #define DO_RAID_COND(_c_) { \
    329 	rc = rf_create_managed_cond(&raidPtr->shutdownList, (_c_)); \
    330 	if (rc) { \
    331 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", \
    332 			__FILE__, __LINE__, rc); \
    333 		DO_RAID_FAIL(); \
    334 		return(rc); \
    335 	} \
    336 }
    337 
    338 int
    339 rf_Configure(raidPtr, cfgPtr, ac)
    340 	RF_Raid_t *raidPtr;
    341 	RF_Config_t *cfgPtr;
    342 	RF_AutoConfig_t *ac;
    343 {
    344 	RF_RowCol_t row, col;
    345 	int     i, rc;
    346 
    347 	RF_LOCK_MUTEX(configureMutex);
    348 	configureCount++;
    349 	if (isconfigged == 0) {
    350 		rc = rf_create_managed_mutex(&globalShutdown, &rf_printf_mutex);
    351 		if (rc) {
    352 			RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    353 			    __LINE__, rc);
    354 			rf_ShutdownList(&globalShutdown);
    355 			return (rc);
    356 		}
    357 		/* initialize globals */
    358 
    359 		rf_clear_debug_print_buffer();
    360 
    361 		DO_INIT_CONFIGURE(rf_ConfigureAllocList);
    362 
    363 		/*
    364 	         * Yes, this does make debugging general to the whole
    365 	         * system instead of being array specific. Bummer, drag.
    366 		 */
    367 		rf_ConfigureDebug(cfgPtr);
    368 		DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
    369 		DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
    370 		DO_INIT_CONFIGURE(rf_ConfigureMapModule);
    371 		DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
    372 		DO_INIT_CONFIGURE(rf_ConfigureCallback);
    373 		DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
    374 		DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
    375 		DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
    376 		DO_INIT_CONFIGURE(rf_ConfigureMCPair);
    377 		DO_INIT_CONFIGURE(rf_ConfigureDAGs);
    378 		DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
    379 		DO_INIT_CONFIGURE(rf_ConfigureDebugPrint);
    380 		DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
    381 		DO_INIT_CONFIGURE(rf_ConfigureCopyback);
    382 		DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
    383 		isconfigged = 1;
    384 	}
    385 	RF_UNLOCK_MUTEX(configureMutex);
    386 
    387 	DO_RAID_MUTEX(&raidPtr->mutex);
    388 	/* set up the cleanup list.  Do this after ConfigureDebug so that
    389 	 * value of memDebug will be set */
    390 
    391 	rf_MakeAllocList(raidPtr->cleanupList);
    392 	if (raidPtr->cleanupList == NULL) {
    393 		DO_RAID_FAIL();
    394 		return (ENOMEM);
    395 	}
    396 	rc = rf_ShutdownCreate(&raidPtr->shutdownList,
    397 	    (void (*) (void *)) rf_FreeAllocList,
    398 	    raidPtr->cleanupList);
    399 	if (rc) {
    400 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
    401 		    __FILE__, __LINE__, rc);
    402 		DO_RAID_FAIL();
    403 		return (rc);
    404 	}
    405 	raidPtr->numRow = cfgPtr->numRow;
    406 	raidPtr->numCol = cfgPtr->numCol;
    407 	raidPtr->numSpare = cfgPtr->numSpare;
    408 
    409 	/* XXX we don't even pretend to support more than one row in the
    410 	 * kernel... */
    411 	if (raidPtr->numRow != 1) {
    412 		RF_ERRORMSG("Only one row supported in kernel.\n");
    413 		DO_RAID_FAIL();
    414 		return (EINVAL);
    415 	}
    416 	RF_CallocAndAdd(raidPtr->status, raidPtr->numRow, sizeof(RF_RowStatus_t),
    417 	    (RF_RowStatus_t *), raidPtr->cleanupList);
    418 	if (raidPtr->status == NULL) {
    419 		DO_RAID_FAIL();
    420 		return (ENOMEM);
    421 	}
    422 	RF_CallocAndAdd(raidPtr->reconControl, raidPtr->numRow,
    423 	    sizeof(RF_ReconCtrl_t *), (RF_ReconCtrl_t **), raidPtr->cleanupList);
    424 	if (raidPtr->reconControl == NULL) {
    425 		DO_RAID_FAIL();
    426 		return (ENOMEM);
    427 	}
    428 	for (i = 0; i < raidPtr->numRow; i++) {
    429 		raidPtr->status[i] = rf_rs_optimal;
    430 		raidPtr->reconControl[i] = NULL;
    431 	}
    432 
    433 	DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
    434 	DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
    435 
    436 	DO_RAID_COND(&raidPtr->outstandingCond);
    437 
    438 	raidPtr->nAccOutstanding = 0;
    439 	raidPtr->waitShutdown = 0;
    440 
    441 	DO_RAID_MUTEX(&raidPtr->access_suspend_mutex);
    442 	DO_RAID_COND(&raidPtr->quiescent_cond);
    443 
    444 	DO_RAID_COND(&raidPtr->waitForReconCond);
    445 
    446 	DO_RAID_MUTEX(&raidPtr->recon_done_proc_mutex);
    447 
    448 	if (ac!=NULL) {
    449 		/* We have an AutoConfig structure..  Don't do the
    450 		   normal disk configuration... call the auto config
    451 		   stuff */
    452 		rf_AutoConfigureDisks(raidPtr, cfgPtr, ac);
    453 	} else {
    454 		DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
    455 		DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
    456 	}
    457 	/* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
    458 	 * no. is set */
    459 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
    460 
    461 	DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
    462 
    463 	DO_RAID_INIT_CONFIGURE(rf_ConfigurePSStatus);
    464 
    465 	for (row = 0; row < raidPtr->numRow; row++) {
    466 		for (col = 0; col < raidPtr->numCol; col++) {
    467 			/*
    468 		         * XXX better distribution
    469 		         */
    470 			raidPtr->hist_diskreq[row][col] = 0;
    471 		}
    472 	}
    473 
    474 	raidPtr->numNewFailures = 0;
    475 	raidPtr->copyback_in_progress = 0;
    476 	raidPtr->parity_rewrite_in_progress = 0;
    477 	raidPtr->recon_in_progress = 0;
    478 	raidPtr->maxOutstanding = cfgPtr->maxOutstandingDiskReqs;
    479 
    480 	/* autoconfigure and root_partition will actually get filled in
    481 	   after the config is done */
    482 	raidPtr->autoconfigure = 0;
    483 	raidPtr->root_partition = 0;
    484 	raidPtr->last_unit = raidPtr->raidid;
    485 	raidPtr->config_order = 0;
    486 
    487 	if (rf_keepAccTotals) {
    488 		raidPtr->keep_acc_totals = 1;
    489 	}
    490 	rf_StartUserStats(raidPtr);
    491 
    492 	raidPtr->valid = 1;
    493 
    494 	printf("raid%d: %s\n", raidPtr->raidid,
    495 	       raidPtr->Layout.map->configName);
    496 	printf("raid%d: Components:", raidPtr->raidid);
    497 	for (row = 0; row < raidPtr->numRow; row++) {
    498 		for (col = 0; col < raidPtr->numCol; col++) {
    499 			printf(" %s", raidPtr->Disks[row][col].devname);
    500 			if (RF_DEAD_DISK(raidPtr->Disks[row][col].status)) {
    501 				printf("[**FAILED**]");
    502 			}
    503 		}
    504 	}
    505 	printf("\n");
    506 	printf("raid%d: Total Sectors: %lu (%lu MB)\n",
    507 	       raidPtr->raidid,
    508 	       (unsigned long) raidPtr->totalSectors,
    509 	       (unsigned long) (raidPtr->totalSectors / 1024 *
    510 				(1 << raidPtr->logBytesPerSector) / 1024));
    511 
    512 	return (0);
    513 }
    514 
    515 static int
    516 init_rad(desc)
    517 	RF_RaidAccessDesc_t *desc;
    518 {
    519 	int     rc;
    520 
    521 	rc = rf_mutex_init(&desc->mutex);
    522 	if (rc) {
    523 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    524 		    __LINE__, rc);
    525 		return (rc);
    526 	}
    527 	rc = rf_cond_init(&desc->cond);
    528 	if (rc) {
    529 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
    530 		    __LINE__, rc);
    531 		rf_mutex_destroy(&desc->mutex);
    532 		return (rc);
    533 	}
    534 	return (0);
    535 }
    536 
    537 static void
    538 clean_rad(desc)
    539 	RF_RaidAccessDesc_t *desc;
    540 {
    541 	rf_mutex_destroy(&desc->mutex);
    542 	rf_cond_destroy(&desc->cond);
    543 }
    544 
    545 static void
    546 rf_ShutdownRDFreeList(ignored)
    547 	void   *ignored;
    548 {
    549 	RF_FREELIST_DESTROY_CLEAN(rf_rad_freelist, next, (RF_RaidAccessDesc_t *), clean_rad);
    550 }
    551 
    552 static int
    553 rf_ConfigureRDFreeList(listp)
    554 	RF_ShutdownList_t **listp;
    555 {
    556 	int     rc;
    557 
    558 	RF_FREELIST_CREATE(rf_rad_freelist, RF_MAX_FREE_RAD,
    559 	    RF_RAD_INC, sizeof(RF_RaidAccessDesc_t));
    560 	if (rf_rad_freelist == NULL) {
    561 		return (ENOMEM);
    562 	}
    563 	rc = rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
    564 	if (rc) {
    565 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n", __FILE__,
    566 		    __LINE__, rc);
    567 		rf_ShutdownRDFreeList(NULL);
    568 		return (rc);
    569 	}
    570 	RF_FREELIST_PRIME_INIT(rf_rad_freelist, RF_RAD_INITIAL, next,
    571 	    (RF_RaidAccessDesc_t *), init_rad);
    572 	return (0);
    573 }
    574 
    575 RF_RaidAccessDesc_t *
    576 rf_AllocRaidAccDesc(
    577     RF_Raid_t * raidPtr,
    578     RF_IoType_t type,
    579     RF_RaidAddr_t raidAddress,
    580     RF_SectorCount_t numBlocks,
    581     caddr_t bufPtr,
    582     void *bp,
    583     RF_RaidAccessFlags_t flags,
    584     RF_AccessState_t * states)
    585 {
    586 	RF_RaidAccessDesc_t *desc;
    587 
    588 	RF_FREELIST_GET_INIT_NOUNLOCK(rf_rad_freelist, desc, next, (RF_RaidAccessDesc_t *), init_rad);
    589 	if (raidPtr->waitShutdown) {
    590 		/*
    591 	         * Actually, we're shutting the array down. Free the desc
    592 	         * and return NULL.
    593 	         */
    594 		RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    595 		RF_FREELIST_FREE_CLEAN(rf_rad_freelist, desc, next, clean_rad);
    596 		return (NULL);
    597 	}
    598 	raidPtr->nAccOutstanding++;
    599 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    600 
    601 	desc->raidPtr = (void *) raidPtr;
    602 	desc->type = type;
    603 	desc->raidAddress = raidAddress;
    604 	desc->numBlocks = numBlocks;
    605 	desc->bufPtr = bufPtr;
    606 	desc->bp = bp;
    607 	desc->paramDAG = NULL;
    608 	desc->paramASM = NULL;
    609 	desc->flags = flags;
    610 	desc->states = states;
    611 	desc->state = 0;
    612 
    613 	desc->status = 0;
    614 	memset((char *) &desc->tracerec, 0, sizeof(RF_AccTraceEntry_t));
    615 	desc->callbackFunc = NULL;
    616 	desc->callbackArg = NULL;
    617 	desc->next = NULL;
    618 	desc->head = desc;
    619 	desc->numPending = 0;
    620 	desc->cleanupList = NULL;
    621 	rf_MakeAllocList(desc->cleanupList);
    622 	return (desc);
    623 }
    624 
    625 void
    626 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t * desc)
    627 {
    628 	RF_Raid_t *raidPtr = desc->raidPtr;
    629 
    630 	RF_ASSERT(desc);
    631 
    632 	rf_FreeAllocList(desc->cleanupList);
    633 	RF_FREELIST_FREE_CLEAN_NOUNLOCK(rf_rad_freelist, desc, next, clean_rad);
    634 	raidPtr->nAccOutstanding--;
    635 	if (raidPtr->waitShutdown) {
    636 		RF_SIGNAL_COND(raidPtr->outstandingCond);
    637 	}
    638 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    639 }
    640 /*********************************************************************
    641  * Main routine for performing an access.
    642  * Accesses are retried until a DAG can not be selected.  This occurs
    643  * when either the DAG library is incomplete or there are too many
    644  * failures in a parity group.
    645  ********************************************************************/
    646 int
    647 rf_DoAccess(
    648     RF_Raid_t * raidPtr,
    649     RF_IoType_t type,
    650     int async_flag,
    651     RF_RaidAddr_t raidAddress,
    652     RF_SectorCount_t numBlocks,
    653     caddr_t bufPtr,
    654     void *bp_in,
    655     RF_RaidAccessFlags_t flags)
    656 /*
    657 type should be read or write
    658 async_flag should be RF_TRUE or RF_FALSE
    659 bp_in is a buf pointer.  void * to facilitate ignoring it outside the kernel
    660 */
    661 {
    662 	RF_RaidAccessDesc_t *desc;
    663 	caddr_t lbufPtr = bufPtr;
    664 	struct buf *bp = (struct buf *) bp_in;
    665 
    666 	raidAddress += rf_raidSectorOffset;
    667 
    668 	if (rf_accessDebug) {
    669 
    670 		printf("logBytes is: %d %d %d\n", raidPtr->raidid,
    671 		    raidPtr->logBytesPerSector,
    672 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks));
    673 		printf("raid%d: %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", raidPtr->raidid,
    674 		    (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
    675 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
    676 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
    677 		    (int) numBlocks,
    678 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks),
    679 		    (long) bufPtr);
    680 	}
    681 	if (raidAddress + numBlocks > raidPtr->totalSectors) {
    682 
    683 		printf("DoAccess: raid addr %lu too large to access %lu sectors.  Max legal addr is %lu\n",
    684 		    (u_long) raidAddress, (u_long) numBlocks, (u_long) raidPtr->totalSectors);
    685 
    686 		IO_BUF_ERR(bp, ENOSPC);
    687 		return (ENOSPC);
    688 	}
    689 	desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
    690 	    numBlocks, lbufPtr, bp, flags, raidPtr->Layout.map->states);
    691 
    692 	if (desc == NULL) {
    693 		return (ENOMEM);
    694 	}
    695 	RF_ETIMER_START(desc->tracerec.tot_timer);
    696 
    697 	desc->async_flag = async_flag;
    698 
    699 	rf_ContinueRaidAccess(desc);
    700 
    701 	return (0);
    702 }
    703 #if 0
    704 /* force the array into reconfigured mode without doing reconstruction */
    705 int
    706 rf_SetReconfiguredMode(raidPtr, row, col)
    707 	RF_Raid_t *raidPtr;
    708 	int     row;
    709 	int     col;
    710 {
    711 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
    712 		printf("Can't set reconfigured mode in dedicated-spare array\n");
    713 		RF_PANIC();
    714 	}
    715 	RF_LOCK_MUTEX(raidPtr->mutex);
    716 	raidPtr->numFailures++;
    717 	raidPtr->Disks[row][col].status = rf_ds_dist_spared;
    718 	raidPtr->status[row] = rf_rs_reconfigured;
    719 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    720 	/* install spare table only if declustering + distributed sparing
    721 	 * architecture. */
    722 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
    723 		rf_InstallSpareTable(raidPtr, row, col);
    724 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    725 	return (0);
    726 }
    727 #endif
    728 
    729 int
    730 rf_FailDisk(
    731     RF_Raid_t * raidPtr,
    732     int frow,
    733     int fcol,
    734     int initRecon)
    735 {
    736 	printf("raid%d: Failing disk r%d c%d\n", raidPtr->raidid, frow, fcol);
    737 	RF_LOCK_MUTEX(raidPtr->mutex);
    738 	raidPtr->numFailures++;
    739 	raidPtr->Disks[frow][fcol].status = rf_ds_failed;
    740 	raidPtr->status[frow] = rf_rs_degraded;
    741 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    742 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    743 	if (initRecon)
    744 		rf_ReconstructFailedDisk(raidPtr, frow, fcol);
    745 	return (0);
    746 }
    747 /* releases a thread that is waiting for the array to become quiesced.
    748  * access_suspend_mutex should be locked upon calling this
    749  */
    750 void
    751 rf_SignalQuiescenceLock(raidPtr, reconDesc)
    752 	RF_Raid_t *raidPtr;
    753 	RF_RaidReconDesc_t *reconDesc;
    754 {
    755 	if (rf_quiesceDebug) {
    756 		printf("raid%d: Signalling quiescence lock\n",
    757 		       raidPtr->raidid);
    758 	}
    759 	raidPtr->access_suspend_release = 1;
    760 
    761 	if (raidPtr->waiting_for_quiescence) {
    762 		SIGNAL_QUIESCENT_COND(raidPtr);
    763 	}
    764 }
    765 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
    766 int
    767 rf_SuspendNewRequestsAndWait(raidPtr)
    768 	RF_Raid_t *raidPtr;
    769 {
    770 	if (rf_quiesceDebug)
    771 		printf("raid%d: Suspending new reqs\n", raidPtr->raidid);
    772 
    773 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    774 	raidPtr->accesses_suspended++;
    775 	raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
    776 
    777 	if (raidPtr->waiting_for_quiescence) {
    778 		raidPtr->access_suspend_release = 0;
    779 		while (!raidPtr->access_suspend_release) {
    780 			printf("raid%d: Suspending: Waiting for Quiescence\n",
    781 			       raidPtr->raidid);
    782 			WAIT_FOR_QUIESCENCE(raidPtr);
    783 			raidPtr->waiting_for_quiescence = 0;
    784 		}
    785 	}
    786 	printf("raid%d: Quiescence reached..\n", raidPtr->raidid);
    787 
    788 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    789 	return (raidPtr->waiting_for_quiescence);
    790 }
    791 /* wake up everyone waiting for quiescence to be released */
    792 void
    793 rf_ResumeNewRequests(raidPtr)
    794 	RF_Raid_t *raidPtr;
    795 {
    796 	RF_CallbackDesc_t *t, *cb;
    797 
    798 	if (rf_quiesceDebug)
    799 		printf("Resuming new reqs\n");
    800 
    801 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    802 	raidPtr->accesses_suspended--;
    803 	if (raidPtr->accesses_suspended == 0)
    804 		cb = raidPtr->quiesce_wait_list;
    805 	else
    806 		cb = NULL;
    807 	raidPtr->quiesce_wait_list = NULL;
    808 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    809 
    810 	while (cb) {
    811 		t = cb;
    812 		cb = cb->next;
    813 		(t->callbackFunc) (t->callbackArg);
    814 		rf_FreeCallbackDesc(t);
    815 	}
    816 }
    817 /*****************************************************************************************
    818  *
    819  * debug routines
    820  *
    821  ****************************************************************************************/
    822 
    823 static void
    824 set_debug_option(name, val)
    825 	char   *name;
    826 	long    val;
    827 {
    828 	RF_DebugName_t *p;
    829 
    830 	for (p = rf_debugNames; p->name; p++) {
    831 		if (!strcmp(p->name, name)) {
    832 			*(p->ptr) = val;
    833 			printf("[Set debug variable %s to %ld]\n", name, val);
    834 			return;
    835 		}
    836 	}
    837 	RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
    838 }
    839 
    840 
    841 /* would like to use sscanf here, but apparently not available in kernel */
    842 /*ARGSUSED*/
    843 static void
    844 rf_ConfigureDebug(cfgPtr)
    845 	RF_Config_t *cfgPtr;
    846 {
    847 	char   *val_p, *name_p, *white_p;
    848 	long    val;
    849 	int     i;
    850 
    851 	rf_ResetDebugOptions();
    852 	for (i = 0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
    853 		name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
    854 		white_p = rf_find_white(name_p);	/* skip to start of 2nd
    855 							 * word */
    856 		val_p = rf_find_non_white(white_p);
    857 		if (*val_p == '0' && *(val_p + 1) == 'x')
    858 			val = rf_htoi(val_p + 2);
    859 		else
    860 			val = rf_atoi(val_p);
    861 		*white_p = '\0';
    862 		set_debug_option(name_p, val);
    863 	}
    864 }
    865 /* performance monitoring stuff */
    866 
    867 #define TIMEVAL_TO_US(t) (((long) t.tv_sec) * 1000000L + (long) t.tv_usec)
    868 
    869 #if !defined(_KERNEL) && !defined(SIMULATE)
    870 
    871 /*
    872  * Throughput stats currently only used in user-level RAIDframe
    873  */
    874 
    875 static int
    876 rf_InitThroughputStats(
    877     RF_ShutdownList_t ** listp,
    878     RF_Raid_t * raidPtr,
    879     RF_Config_t * cfgPtr)
    880 {
    881 	int     rc;
    882 
    883 	/* these used by user-level raidframe only */
    884 	rc = rf_create_managed_mutex(listp, &raidPtr->throughputstats.mutex);
    885 	if (rc) {
    886 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    887 		    __LINE__, rc);
    888 		return (rc);
    889 	}
    890 	raidPtr->throughputstats.sum_io_us = 0;
    891 	raidPtr->throughputstats.num_ios = 0;
    892 	raidPtr->throughputstats.num_out_ios = 0;
    893 	return (0);
    894 }
    895 
    896 void
    897 rf_StartThroughputStats(RF_Raid_t * raidPtr)
    898 {
    899 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
    900 	raidPtr->throughputstats.num_ios++;
    901 	raidPtr->throughputstats.num_out_ios++;
    902 	if (raidPtr->throughputstats.num_out_ios == 1)
    903 		RF_GETTIME(raidPtr->throughputstats.start);
    904 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
    905 }
    906 
    907 static void
    908 rf_StopThroughputStats(RF_Raid_t * raidPtr)
    909 {
    910 	struct timeval diff;
    911 
    912 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
    913 	raidPtr->throughputstats.num_out_ios--;
    914 	if (raidPtr->throughputstats.num_out_ios == 0) {
    915 		RF_GETTIME(raidPtr->throughputstats.stop);
    916 		RF_TIMEVAL_DIFF(&raidPtr->throughputstats.start, &raidPtr->throughputstats.stop, &diff);
    917 		raidPtr->throughputstats.sum_io_us += TIMEVAL_TO_US(diff);
    918 	}
    919 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
    920 }
    921 
    922 static void
    923 rf_PrintThroughputStats(RF_Raid_t * raidPtr)
    924 {
    925 	RF_ASSERT(raidPtr->throughputstats.num_out_ios == 0);
    926 	if (raidPtr->throughputstats.sum_io_us != 0) {
    927 		printf("[Througphut: %8.2f IOs/second]\n", raidPtr->throughputstats.num_ios
    928 		    / (raidPtr->throughputstats.sum_io_us / 1000000.0));
    929 	}
    930 }
    931 #endif				/* !KERNEL && !SIMULATE */
    932 
    933 void
    934 rf_StartUserStats(RF_Raid_t * raidPtr)
    935 {
    936 	RF_GETTIME(raidPtr->userstats.start);
    937 	raidPtr->userstats.sum_io_us = 0;
    938 	raidPtr->userstats.num_ios = 0;
    939 	raidPtr->userstats.num_sect_moved = 0;
    940 }
    941 
    942 void
    943 rf_StopUserStats(RF_Raid_t * raidPtr)
    944 {
    945 	RF_GETTIME(raidPtr->userstats.stop);
    946 }
    947 
    948 void
    949 rf_UpdateUserStats(raidPtr, rt, numsect)
    950 	RF_Raid_t *raidPtr;
    951 	int     rt;		/* resp time in us */
    952 	int     numsect;	/* number of sectors for this access */
    953 {
    954 	raidPtr->userstats.sum_io_us += rt;
    955 	raidPtr->userstats.num_ios++;
    956 	raidPtr->userstats.num_sect_moved += numsect;
    957 }
    958 
    959 void
    960 rf_PrintUserStats(RF_Raid_t * raidPtr)
    961 {
    962 	long    elapsed_us, mbs, mbs_frac;
    963 	struct timeval diff;
    964 
    965 	RF_TIMEVAL_DIFF(&raidPtr->userstats.start, &raidPtr->userstats.stop, &diff);
    966 	elapsed_us = TIMEVAL_TO_US(diff);
    967 
    968 	/* 2000 sectors per megabyte, 10000000 microseconds per second */
    969 	if (elapsed_us)
    970 		mbs = (raidPtr->userstats.num_sect_moved / 2000) / (elapsed_us / 1000000);
    971 	else
    972 		mbs = 0;
    973 
    974 	/* this computes only the first digit of the fractional mb/s moved */
    975 	if (elapsed_us) {
    976 		mbs_frac = ((raidPtr->userstats.num_sect_moved / 200) / (elapsed_us / 1000000))
    977 		    - (mbs * 10);
    978 	} else {
    979 		mbs_frac = 0;
    980 	}
    981 
    982 	printf("Number of I/Os:             %ld\n", raidPtr->userstats.num_ios);
    983 	printf("Elapsed time (us):          %ld\n", elapsed_us);
    984 	printf("User I/Os per second:       %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_ios, (elapsed_us / 1000000)));
    985 	printf("Average user response time: %ld us\n", RF_DB0_CHECK(raidPtr->userstats.sum_io_us, raidPtr->userstats.num_ios));
    986 	printf("Total sectors moved:        %ld\n", raidPtr->userstats.num_sect_moved);
    987 	printf("Average access size (sect): %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_sect_moved, raidPtr->userstats.num_ios));
    988 	printf("Achieved data rate:         %ld.%ld MB/sec\n", mbs, mbs_frac);
    989 }
    990 
    991 
    992 void
    993 rf_print_panic_message(line,file)
    994 	int line;
    995 	char *file;
    996 {
    997 	sprintf(rf_panicbuf,"raidframe error at line %d file %s",
    998 		line, file);
    999 }
   1000 
   1001 void
   1002 rf_print_assert_panic_message(line,file,condition)
   1003 	int line;
   1004 	char *file;
   1005 	char *condition;
   1006 {
   1007 	sprintf(rf_panicbuf,
   1008 		"raidframe error at line %d file %s (failed asserting %s)\n",
   1009 		line, file, condition);
   1010 }
   1011