Home | History | Annotate | Line # | Download | only in raidframe
rf_driver.c revision 1.99
      1 /*	$NetBSD: rf_driver.c,v 1.99 2004/04/09 23:10:16 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.99 2004/04/09 23:10:16 oster Exp $");
     77 
     78 #include "opt_raid_diagnostic.h"
     79 
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/ioctl.h>
     83 #include <sys/fcntl.h>
     84 #include <sys/vnode.h>
     85 
     86 
     87 #include "rf_archs.h"
     88 #include "rf_threadstuff.h"
     89 
     90 #include <sys/errno.h>
     91 
     92 #include "rf_raid.h"
     93 #include "rf_dag.h"
     94 #include "rf_aselect.h"
     95 #include "rf_diskqueue.h"
     96 #include "rf_parityscan.h"
     97 #include "rf_alloclist.h"
     98 #include "rf_dagutils.h"
     99 #include "rf_utils.h"
    100 #include "rf_etimer.h"
    101 #include "rf_acctrace.h"
    102 #include "rf_general.h"
    103 #include "rf_desc.h"
    104 #include "rf_states.h"
    105 #include "rf_decluster.h"
    106 #include "rf_map.h"
    107 #include "rf_revent.h"
    108 #include "rf_callback.h"
    109 #include "rf_engine.h"
    110 #include "rf_mcpair.h"
    111 #include "rf_nwayxor.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 #ifndef RF_ACCESS_DEBUG
    121 #define RF_ACCESS_DEBUG 0
    122 #endif
    123 
    124 /* rad == RF_RaidAccessDesc_t */
    125 RF_DECLARE_MUTEX(rf_rad_lock)
    126 #define RF_MAX_FREE_RAD 128
    127 #define RF_MIN_FREE_RAD  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 void rf_ShutdownRDFreeList(void *);
    139 static int rf_ConfigureRDFreeList(RF_ShutdownList_t **);
    140 
    141 RF_DECLARE_MUTEX(rf_printf_mutex)	/* debug only:  avoids interleaved
    142 					 * printfs by different stripes */
    143 
    144 #define SIGNAL_QUIESCENT_COND(_raid_)  wakeup(&((_raid_)->accesses_suspended))
    145 #define WAIT_FOR_QUIESCENCE(_raid_) \
    146 	ltsleep(&((_raid_)->accesses_suspended), PRIBIO, \
    147 		"raidframe quiesce", 0, &((_raid_)->access_suspend_mutex))
    148 
    149 static int configureCount = 0;	/* number of active configurations */
    150 static int isconfigged = 0;	/* is basic raidframe (non per-array)
    151 				 * stuff configged */
    152 RF_DECLARE_LKMGR_STATIC_MUTEX(configureMutex)	/* used to lock the configuration
    153 					 * stuff */
    154 static RF_ShutdownList_t *globalShutdown;	/* non array-specific
    155 						 * stuff */
    156 
    157 static int rf_ConfigureRDFreeList(RF_ShutdownList_t ** listp);
    158 
    159 /* called at system boot time */
    160 int
    161 rf_BootRaidframe()
    162 {
    163 
    164 	if (raidframe_booted)
    165 		return (EBUSY);
    166 	raidframe_booted = 1;
    167 	lockinit(&configureMutex, PRIBIO, "RAIDframe lock", 0, 0);
    168  	configureCount = 0;
    169 	isconfigged = 0;
    170 	globalShutdown = NULL;
    171 	return (0);
    172 }
    173 
    174 /*
    175  * Called whenever an array is shutdown
    176  */
    177 static void
    178 rf_UnconfigureArray()
    179 {
    180 
    181 	RF_LOCK_LKMGR_MUTEX(configureMutex);
    182 	if (--configureCount == 0) {	/* if no active configurations, shut
    183 					 * everything down */
    184 		isconfigged = 0;
    185 		rf_ShutdownList(&globalShutdown);
    186 
    187 		/*
    188 	         * We must wait until now, because the AllocList module
    189 	         * uses the DebugMem module.
    190 	         */
    191 #if RF_DEBUG_MEM
    192 		if (rf_memDebug)
    193 			rf_print_unfreed();
    194 #endif
    195 	}
    196 	RF_UNLOCK_LKMGR_MUTEX(configureMutex);
    197 }
    198 
    199 /*
    200  * Called to shut down an array.
    201  */
    202 int
    203 rf_Shutdown(RF_Raid_t *raidPtr)
    204 {
    205 	if (!raidPtr->valid) {
    206 		RF_ERRORMSG("Attempt to shut down unconfigured RAIDframe driver.  Aborting shutdown\n");
    207 		return (EINVAL);
    208 	}
    209 	/*
    210          * wait for outstanding IOs to land
    211          * As described in rf_raid.h, we use the rad_freelist lock
    212          * to protect the per-array info about outstanding descs
    213          * since we need to do freelist locking anyway, and this
    214          * cuts down on the amount of serialization we've got going
    215          * on.
    216          */
    217 	RF_LOCK_MUTEX(rf_rad_lock);
    218 	if (raidPtr->waitShutdown) {
    219 		RF_UNLOCK_MUTEX(rf_rad_lock);
    220 		return (EBUSY);
    221 	}
    222 	raidPtr->waitShutdown = 1;
    223 	while (raidPtr->nAccOutstanding) {
    224 		RF_WAIT_COND(raidPtr->outstandingCond, rf_rad_lock);
    225 	}
    226 	RF_UNLOCK_MUTEX(rf_rad_lock);
    227 
    228 	/* Wait for any parity re-writes to stop... */
    229 	while (raidPtr->parity_rewrite_in_progress) {
    230 		printf("Waiting for parity re-write to exit...\n");
    231 		tsleep(&raidPtr->parity_rewrite_in_progress, PRIBIO,
    232 		       "rfprwshutdown", 0);
    233 	}
    234 
    235 	raidPtr->valid = 0;
    236 
    237 	rf_update_component_labels(raidPtr, RF_FINAL_COMPONENT_UPDATE);
    238 
    239 	rf_UnconfigureVnodes(raidPtr);
    240 
    241 	rf_ShutdownList(&raidPtr->shutdownList);
    242 
    243 	rf_UnconfigureArray();
    244 
    245 	return (0);
    246 }
    247 
    248 
    249 #define DO_INIT_CONFIGURE(f) { \
    250 	rc = f (&globalShutdown); \
    251 	if (rc) { \
    252 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    253 		rf_ShutdownList(&globalShutdown); \
    254 		configureCount--; \
    255 		RF_UNLOCK_LKMGR_MUTEX(configureMutex); \
    256 		return(rc); \
    257 	} \
    258 }
    259 
    260 #define DO_RAID_FAIL() { \
    261 	rf_UnconfigureVnodes(raidPtr); \
    262 	rf_ShutdownList(&raidPtr->shutdownList); \
    263 	rf_UnconfigureArray(); \
    264 }
    265 
    266 #define DO_RAID_INIT_CONFIGURE(f) { \
    267 	rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
    268 	if (rc) { \
    269 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    270 		DO_RAID_FAIL(); \
    271 		return(rc); \
    272 	} \
    273 }
    274 
    275 #define DO_RAID_MUTEX(_m_) { \
    276 	rf_mutex_init((_m_)); \
    277 }
    278 
    279 int
    280 rf_Configure(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr, RF_AutoConfig_t *ac)
    281 {
    282 	RF_RowCol_t col;
    283 	void *tmpbuf;
    284 	RF_VoidPointerListElem_t *vple;
    285 	int rc, i;
    286 
    287 	RF_LOCK_LKMGR_MUTEX(configureMutex);
    288 	configureCount++;
    289 	if (isconfigged == 0) {
    290 		rf_mutex_init(&rf_printf_mutex);
    291 
    292 		/* initialize globals */
    293 
    294 		DO_INIT_CONFIGURE(rf_ConfigureAllocList);
    295 
    296 		/*
    297 	         * Yes, this does make debugging general to the whole
    298 	         * system instead of being array specific. Bummer, drag.
    299 		 */
    300 		rf_ConfigureDebug(cfgPtr);
    301 		DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
    302 #if RF_ACC_TRACE > 0
    303 		DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
    304 #endif
    305 		DO_INIT_CONFIGURE(rf_ConfigureMapModule);
    306 		DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
    307 		DO_INIT_CONFIGURE(rf_ConfigureCallback);
    308 		DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
    309 		DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
    310 		DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
    311 		DO_INIT_CONFIGURE(rf_ConfigureMCPair);
    312 		DO_INIT_CONFIGURE(rf_ConfigureDAGs);
    313 		DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
    314 		DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
    315 		DO_INIT_CONFIGURE(rf_ConfigureCopyback);
    316 		DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
    317 		isconfigged = 1;
    318 	}
    319 	RF_UNLOCK_LKMGR_MUTEX(configureMutex);
    320 
    321 	DO_RAID_MUTEX(&raidPtr->mutex);
    322 	/* set up the cleanup list.  Do this after ConfigureDebug so that
    323 	 * value of memDebug will be set */
    324 
    325 	rf_MakeAllocList(raidPtr->cleanupList);
    326 	if (raidPtr->cleanupList == NULL) {
    327 		DO_RAID_FAIL();
    328 		return (ENOMEM);
    329 	}
    330 	rf_ShutdownCreate(&raidPtr->shutdownList,
    331 			  (void (*) (void *)) rf_FreeAllocList,
    332 			  raidPtr->cleanupList);
    333 
    334 	raidPtr->numCol = cfgPtr->numCol;
    335 	raidPtr->numSpare = cfgPtr->numSpare;
    336 
    337 	raidPtr->status = rf_rs_optimal;
    338 	raidPtr->reconControl = NULL;
    339 
    340 	TAILQ_INIT(&(raidPtr->iodone));
    341 	simple_lock_init(&(raidPtr->iodone_lock));
    342 
    343 	DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
    344 	DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
    345 
    346 	raidPtr->outstandingCond = 0;
    347 
    348 	raidPtr->nAccOutstanding = 0;
    349 	raidPtr->waitShutdown = 0;
    350 
    351 	DO_RAID_MUTEX(&raidPtr->access_suspend_mutex);
    352 
    353 	raidPtr->waitForReconCond = 0;
    354 
    355 	if (ac!=NULL) {
    356 		/* We have an AutoConfig structure..  Don't do the
    357 		   normal disk configuration... call the auto config
    358 		   stuff */
    359 		rf_AutoConfigureDisks(raidPtr, cfgPtr, ac);
    360 	} else {
    361 		DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
    362 		DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
    363 	}
    364 	/* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
    365 	 * no. is set */
    366 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
    367 
    368 	DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
    369 
    370 	DO_RAID_INIT_CONFIGURE(rf_ConfigurePSStatus);
    371 
    372 #if RF_INCLUDE_CHAINDECLUSTER > 0
    373 	for (col = 0; col < raidPtr->numCol; col++) {
    374 		/*
    375 		 * XXX better distribution
    376 		 */
    377 		raidPtr->hist_diskreq[col] = 0;
    378 	}
    379 #endif
    380 	raidPtr->numNewFailures = 0;
    381 	raidPtr->copyback_in_progress = 0;
    382 	raidPtr->parity_rewrite_in_progress = 0;
    383 	raidPtr->adding_hot_spare = 0;
    384 	raidPtr->recon_in_progress = 0;
    385 	raidPtr->maxOutstanding = cfgPtr->maxOutstandingDiskReqs;
    386 
    387 	/* autoconfigure and root_partition will actually get filled in
    388 	   after the config is done */
    389 	raidPtr->autoconfigure = 0;
    390 	raidPtr->root_partition = 0;
    391 	raidPtr->last_unit = raidPtr->raidid;
    392 	raidPtr->config_order = 0;
    393 
    394 	if (rf_keepAccTotals) {
    395 		raidPtr->keep_acc_totals = 1;
    396 	}
    397 
    398 	/* Allocate a bunch of buffers to be used in low-memory conditions */
    399 	raidPtr->iobuf = NULL;
    400 	/* XXX next line needs tuning... */
    401 	raidPtr->numEmergencyBuffers = 10 * raidPtr->numCol;
    402 #if DEBUG
    403 	printf("raid%d: allocating %d buffers of %d bytes.\n",
    404 	       raidPtr->raidid,
    405 	       raidPtr->numEmergencyBuffers,
    406 	       (int)(raidPtr->Layout.sectorsPerStripeUnit <<
    407 	       raidPtr->logBytesPerSector));
    408 #endif
    409 	for (i = 0; i < raidPtr->numEmergencyBuffers; i++) {
    410 		tmpbuf = malloc( raidPtr->Layout.sectorsPerStripeUnit <<
    411 				 raidPtr->logBytesPerSector,
    412 				 M_RAIDFRAME, M_NOWAIT);
    413 		if (tmpbuf) {
    414 			vple = rf_AllocVPListElem();
    415 			vple->p= tmpbuf;
    416 			vple->next = raidPtr->iobuf;
    417 			raidPtr->iobuf = vple;
    418 			raidPtr->iobuf_count++;
    419 		} else {
    420 			printf("raid%d: failed to allocate emergency buffer!\n",
    421 			       raidPtr->raidid);
    422 		}
    423 	}
    424 
    425 	/* XXX next line needs tuning too... */
    426 	raidPtr->numEmergencyStripeBuffers = 10;
    427         for (i = 0; i < raidPtr->numEmergencyStripeBuffers; i++) {
    428                 tmpbuf = malloc( raidPtr->numCol * (raidPtr->Layout.sectorsPerStripeUnit <<
    429                                  raidPtr->logBytesPerSector),
    430                                  M_RAIDFRAME, M_NOWAIT);
    431                 if (tmpbuf) {
    432                         vple = rf_AllocVPListElem();
    433                         vple->p= tmpbuf;
    434                         vple->next = raidPtr->stripebuf;
    435                         raidPtr->stripebuf = vple;
    436                         raidPtr->stripebuf_count++;
    437                 } else {
    438                         printf("raid%d: failed to allocate emergency stripe buffer!\n",
    439                                raidPtr->raidid);
    440                 }
    441         }
    442 
    443 
    444 	raidPtr->valid = 1;
    445 
    446 	printf("raid%d: %s\n", raidPtr->raidid,
    447 	       raidPtr->Layout.map->configName);
    448 	printf("raid%d: Components:", raidPtr->raidid);
    449 
    450 	for (col = 0; col < raidPtr->numCol; col++) {
    451 		printf(" %s", raidPtr->Disks[col].devname);
    452 		if (RF_DEAD_DISK(raidPtr->Disks[col].status)) {
    453 			printf("[**FAILED**]");
    454 		}
    455 	}
    456 	printf("\n");
    457 	printf("raid%d: Total Sectors: %lu (%lu MB)\n",
    458 	       raidPtr->raidid,
    459 	       (unsigned long) raidPtr->totalSectors,
    460 	       (unsigned long) (raidPtr->totalSectors / 1024 *
    461 				(1 << raidPtr->logBytesPerSector) / 1024));
    462 
    463 	return (0);
    464 }
    465 
    466 static void
    467 rf_ShutdownRDFreeList(void *ignored)
    468 {
    469 	pool_destroy(&rf_pools.rad);
    470 }
    471 
    472 static int
    473 rf_ConfigureRDFreeList(RF_ShutdownList_t **listp)
    474 {
    475 
    476 	rf_pool_init(&rf_pools.rad, sizeof(RF_RaidAccessDesc_t),
    477 		     "rf_rad_pl", RF_MIN_FREE_RAD, RF_MAX_FREE_RAD);
    478 	rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
    479 	simple_lock_init(&rf_rad_lock);
    480 	return (0);
    481 }
    482 
    483 RF_RaidAccessDesc_t *
    484 rf_AllocRaidAccDesc(RF_Raid_t *raidPtr, RF_IoType_t type,
    485 		    RF_RaidAddr_t raidAddress, RF_SectorCount_t numBlocks,
    486 		    caddr_t bufPtr, void *bp, RF_RaidAccessFlags_t flags,
    487 		    RF_AccessState_t *states)
    488 {
    489 	RF_RaidAccessDesc_t *desc;
    490 
    491 	desc = pool_get(&rf_pools.rad, PR_WAITOK);
    492 
    493 	RF_LOCK_MUTEX(rf_rad_lock);
    494 	if (raidPtr->waitShutdown) {
    495 		/*
    496 	         * Actually, we're shutting the array down. Free the desc
    497 	         * and return NULL.
    498 	         */
    499 
    500 		RF_UNLOCK_MUTEX(rf_rad_lock);
    501 		pool_put(&rf_pools.rad, desc);
    502 		return (NULL);
    503 	}
    504 	raidPtr->nAccOutstanding++;
    505 
    506 	RF_UNLOCK_MUTEX(rf_rad_lock);
    507 
    508 	desc->raidPtr = (void *) raidPtr;
    509 	desc->type = type;
    510 	desc->raidAddress = raidAddress;
    511 	desc->numBlocks = numBlocks;
    512 	desc->bufPtr = bufPtr;
    513 	desc->bp = bp;
    514 	desc->flags = flags;
    515 	desc->states = states;
    516 	desc->state = 0;
    517 	desc->dagList = NULL;
    518 
    519 	desc->status = 0;
    520 #if RF_ACC_TRACE > 0
    521 	memset((char *) &desc->tracerec, 0, sizeof(RF_AccTraceEntry_t));
    522 #endif
    523 	desc->callbackFunc = NULL;
    524 	desc->callbackArg = NULL;
    525 	desc->next = NULL;
    526 	desc->iobufs = NULL;
    527 	desc->stripebufs = NULL;
    528 
    529 	return (desc);
    530 }
    531 
    532 void
    533 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t *desc)
    534 {
    535 	RF_Raid_t *raidPtr = desc->raidPtr;
    536 	RF_DagList_t *dagList, *temp;
    537 	RF_VoidPointerListElem_t *tmp;
    538 
    539 	RF_ASSERT(desc);
    540 
    541 	/* Cleanup the dagList(s) */
    542 	dagList = desc->dagList;
    543 	while(dagList != NULL) {
    544 		temp = dagList;
    545 		dagList = dagList->next;
    546 		rf_FreeDAGList(temp);
    547 	}
    548 
    549 	while (desc->iobufs) {
    550 		tmp = desc->iobufs;
    551 		desc->iobufs = desc->iobufs->next;
    552 		rf_FreeIOBuffer(raidPtr, tmp);
    553 	}
    554 
    555 	while (desc->stripebufs) {
    556 		tmp = desc->stripebufs;
    557 		desc->stripebufs = desc->stripebufs->next;
    558 		rf_FreeStripeBuffer(raidPtr, tmp);
    559 	}
    560 
    561 	pool_put(&rf_pools.rad, desc);
    562 	RF_LOCK_MUTEX(rf_rad_lock);
    563 	raidPtr->nAccOutstanding--;
    564 	if (raidPtr->waitShutdown) {
    565 		RF_SIGNAL_COND(raidPtr->outstandingCond);
    566 	}
    567 	RF_UNLOCK_MUTEX(rf_rad_lock);
    568 }
    569 /*********************************************************************
    570  * Main routine for performing an access.
    571  * Accesses are retried until a DAG can not be selected.  This occurs
    572  * when either the DAG library is incomplete or there are too many
    573  * failures in a parity group.
    574  *
    575  * type should be read or write async_flag should be RF_TRUE or
    576  * RF_FALSE bp_in is a buf pointer.  void * to facilitate ignoring it
    577  * outside the kernel
    578  ********************************************************************/
    579 int
    580 rf_DoAccess(RF_Raid_t * raidPtr, RF_IoType_t type, int async_flag,
    581 	    RF_RaidAddr_t raidAddress, RF_SectorCount_t numBlocks,
    582 	    caddr_t bufPtr, void *bp_in, RF_RaidAccessFlags_t flags)
    583 {
    584 	RF_RaidAccessDesc_t *desc;
    585 	caddr_t lbufPtr = bufPtr;
    586 	struct buf *bp = (struct buf *) bp_in;
    587 
    588 	raidAddress += rf_raidSectorOffset;
    589 
    590 #if RF_ACCESS_DEBUG
    591 	if (rf_accessDebug) {
    592 
    593 		printf("logBytes is: %d %d %d\n", raidPtr->raidid,
    594 		    raidPtr->logBytesPerSector,
    595 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks));
    596 		printf("raid%d: %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", raidPtr->raidid,
    597 		    (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
    598 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
    599 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
    600 		    (int) numBlocks,
    601 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks),
    602 		    (long) bufPtr);
    603 	}
    604 #endif
    605 	if (raidAddress + numBlocks > raidPtr->totalSectors) {
    606 
    607 		printf("DoAccess: raid addr %lu too large to access %lu sectors.  Max legal addr is %lu\n",
    608 		    (u_long) raidAddress, (u_long) numBlocks, (u_long) raidPtr->totalSectors);
    609 
    610 
    611 		bp->b_flags |= B_ERROR;
    612 		bp->b_resid = bp->b_bcount;
    613 		bp->b_error = ENOSPC;
    614 		biodone(bp);
    615 		return (ENOSPC);
    616 	}
    617 	desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
    618 	    numBlocks, lbufPtr, bp, flags, raidPtr->Layout.map->states);
    619 
    620 	if (desc == NULL) {
    621 		return (ENOMEM);
    622 	}
    623 #if RF_ACC_TRACE > 0
    624 	RF_ETIMER_START(desc->tracerec.tot_timer);
    625 #endif
    626 	desc->async_flag = async_flag;
    627 
    628 	rf_ContinueRaidAccess(desc);
    629 
    630 	return (0);
    631 }
    632 #if 0
    633 /* force the array into reconfigured mode without doing reconstruction */
    634 int
    635 rf_SetReconfiguredMode(RF_Raid_t *raidPtr, int col)
    636 {
    637 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
    638 		printf("Can't set reconfigured mode in dedicated-spare array\n");
    639 		RF_PANIC();
    640 	}
    641 	RF_LOCK_MUTEX(raidPtr->mutex);
    642 	raidPtr->numFailures++;
    643 	raidPtr->Disks[col].status = rf_ds_dist_spared;
    644 	raidPtr->status = rf_rs_reconfigured;
    645 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    646 	/* install spare table only if declustering + distributed sparing
    647 	 * architecture. */
    648 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
    649 		rf_InstallSpareTable(raidPtr, col);
    650 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    651 	return (0);
    652 }
    653 #endif
    654 
    655 int
    656 rf_FailDisk(RF_Raid_t *raidPtr, int fcol, int initRecon)
    657 {
    658 
    659 	/* need to suspend IO's here -- if there are DAGs in flight
    660 	   and we pull the rug out from under ci_vp, Bad Things
    661 	   can happen.  */
    662 
    663 	rf_SuspendNewRequestsAndWait(raidPtr);
    664 
    665 	RF_LOCK_MUTEX(raidPtr->mutex);
    666 	if (raidPtr->Disks[fcol].status != rf_ds_failed) {
    667 		/* must be failing something that is valid, or else it's
    668 		   already marked as failed (in which case we don't
    669 		   want to mark it failed again!) */
    670 		raidPtr->numFailures++;
    671 		raidPtr->Disks[fcol].status = rf_ds_failed;
    672 		raidPtr->status = rf_rs_degraded;
    673 	}
    674 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    675 
    676 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    677 
    678 	/* Close the component, so that it's not "locked" if someone
    679 	   else want's to use it! */
    680 
    681 	rf_close_component(raidPtr, raidPtr->raid_cinfo[fcol].ci_vp,
    682 			   raidPtr->Disks[fcol].auto_configured);
    683 
    684 	RF_LOCK_MUTEX(raidPtr->mutex);
    685 	raidPtr->raid_cinfo[fcol].ci_vp = NULL;
    686 
    687 	/* Need to mark the component as not being auto_configured
    688 	   (in case it was previously). */
    689 
    690 	raidPtr->Disks[fcol].auto_configured = 0;
    691 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    692 	/* now we can allow IO to continue -- we'll be suspending it
    693 	   again in rf_ReconstructFailedDisk() if we have to.. */
    694 
    695 	rf_ResumeNewRequests(raidPtr);
    696 
    697 	if (initRecon)
    698 		rf_ReconstructFailedDisk(raidPtr, fcol);
    699 	return (0);
    700 }
    701 /* releases a thread that is waiting for the array to become quiesced.
    702  * access_suspend_mutex should be locked upon calling this
    703  */
    704 void
    705 rf_SignalQuiescenceLock(RF_Raid_t *raidPtr)
    706 {
    707 #if RF_DEBUG_QUIESCE
    708 	if (rf_quiesceDebug) {
    709 		printf("raid%d: Signalling quiescence lock\n",
    710 		       raidPtr->raidid);
    711 	}
    712 #endif
    713 	raidPtr->access_suspend_release = 1;
    714 
    715 	if (raidPtr->waiting_for_quiescence) {
    716 		SIGNAL_QUIESCENT_COND(raidPtr);
    717 	}
    718 }
    719 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
    720 int
    721 rf_SuspendNewRequestsAndWait(RF_Raid_t *raidPtr)
    722 {
    723 #if RF_DEBUG_QUIESCE
    724 	if (rf_quiesceDebug)
    725 		printf("raid%d: Suspending new reqs\n", raidPtr->raidid);
    726 #endif
    727 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    728 	raidPtr->accesses_suspended++;
    729 	raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
    730 
    731 	if (raidPtr->waiting_for_quiescence) {
    732 		raidPtr->access_suspend_release = 0;
    733 		while (!raidPtr->access_suspend_release) {
    734 #if RF_DEBUG_QUIESCE
    735 			printf("raid%d: Suspending: Waiting for Quiescence\n",
    736 			       raidPtr->raidid);
    737 #endif
    738 			WAIT_FOR_QUIESCENCE(raidPtr);
    739 			raidPtr->waiting_for_quiescence = 0;
    740 		}
    741 	}
    742 #if RF_DEBUG_QUIESCE
    743 	printf("raid%d: Quiescence reached..\n", raidPtr->raidid);
    744 #endif
    745 
    746 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    747 	return (raidPtr->waiting_for_quiescence);
    748 }
    749 /* wake up everyone waiting for quiescence to be released */
    750 void
    751 rf_ResumeNewRequests(RF_Raid_t *raidPtr)
    752 {
    753 	RF_CallbackDesc_t *t, *cb;
    754 
    755 #if RF_DEBUG_QUIESCE
    756 	if (rf_quiesceDebug)
    757 		printf("Resuming new reqs\n");
    758 #endif
    759 
    760 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    761 	raidPtr->accesses_suspended--;
    762 	if (raidPtr->accesses_suspended == 0)
    763 		cb = raidPtr->quiesce_wait_list;
    764 	else
    765 		cb = NULL;
    766 	raidPtr->quiesce_wait_list = NULL;
    767 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    768 
    769 	while (cb) {
    770 		t = cb;
    771 		cb = cb->next;
    772 		(t->callbackFunc) (t->callbackArg);
    773 		rf_FreeCallbackDesc(t);
    774 	}
    775 }
    776 /*****************************************************************************************
    777  *
    778  * debug routines
    779  *
    780  ****************************************************************************************/
    781 
    782 static void
    783 set_debug_option(char *name, long val)
    784 {
    785 	RF_DebugName_t *p;
    786 
    787 	for (p = rf_debugNames; p->name; p++) {
    788 		if (!strcmp(p->name, name)) {
    789 			*(p->ptr) = val;
    790 			printf("[Set debug variable %s to %ld]\n", name, val);
    791 			return;
    792 		}
    793 	}
    794 	RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
    795 }
    796 
    797 
    798 /* would like to use sscanf here, but apparently not available in kernel */
    799 /*ARGSUSED*/
    800 static void
    801 rf_ConfigureDebug(RF_Config_t *cfgPtr)
    802 {
    803 	char   *val_p, *name_p, *white_p;
    804 	long    val;
    805 	int     i;
    806 
    807 	rf_ResetDebugOptions();
    808 	for (i = 0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
    809 		name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
    810 		white_p = rf_find_white(name_p);	/* skip to start of 2nd
    811 							 * word */
    812 		val_p = rf_find_non_white(white_p);
    813 		if (*val_p == '0' && *(val_p + 1) == 'x')
    814 			val = rf_htoi(val_p + 2);
    815 		else
    816 			val = rf_atoi(val_p);
    817 		*white_p = '\0';
    818 		set_debug_option(name_p, val);
    819 	}
    820 }
    821 
    822 void
    823 rf_print_panic_message(int line, char *file)
    824 {
    825 	sprintf(rf_panicbuf,"raidframe error at line %d file %s",
    826 		line, file);
    827 }
    828 
    829 #ifdef RAID_DIAGNOSTIC
    830 void
    831 rf_print_assert_panic_message(int line,	char *file, char *condition)
    832 {
    833 	sprintf(rf_panicbuf,
    834 		"raidframe error at line %d file %s (failed asserting %s)\n",
    835 		line, file, condition);
    836 }
    837 #endif
    838 
    839 void
    840 rf_print_unable_to_init_mutex(char *file, int line, int rc)
    841 {
    842 	RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n",
    843 		     file, line, rc);
    844 }
    845 
    846 void
    847 rf_print_unable_to_add_shutdown(char *file, int line, int rc)
    848 {
    849 	RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
    850 		     file, line, rc);
    851 }
    852