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