Home | History | Annotate | Line # | Download | only in raidframe
rf_driver.c revision 1.118.10.1
      1 /*	$NetBSD: rf_driver.c,v 1.118.10.1 2008/12/23 04:03:00 snj 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  *
     18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * Copyright (c) 1995 Carnegie-Mellon University.
     33  * All rights reserved.
     34  *
     35  * Author: Mark Holland, Khalil Amiri, Claudson Bornstein, William V. Courtright II,
     36  *         Robby Findler, Daniel Stodolsky, Rachad Youssef, Jim Zelenka
     37  *
     38  * Permission to use, copy, modify and distribute this software and
     39  * its documentation is hereby granted, provided that both the copyright
     40  * notice and this permission notice appear in all copies of the
     41  * software, derivative works or modified versions, and any portions
     42  * thereof, and that both notices appear in supporting documentation.
     43  *
     44  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     45  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     46  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     47  *
     48  * Carnegie Mellon requests users of this software to return to
     49  *
     50  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     51  *  School of Computer Science
     52  *  Carnegie Mellon University
     53  *  Pittsburgh PA 15213-3890
     54  *
     55  * any improvements or extensions that they make and grant Carnegie the
     56  * rights to redistribute these changes.
     57  */
     58 
     59 /******************************************************************************
     60  *
     61  * rf_driver.c -- main setup, teardown, and access routines for the RAID driver
     62  *
     63  * all routines are prefixed with rf_ (raidframe), to avoid conficts.
     64  *
     65  ******************************************************************************/
     66 
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: rf_driver.c,v 1.118.10.1 2008/12/23 04:03:00 snj Exp $");
     70 
     71 #include "opt_raid_diagnostic.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/ioctl.h>
     76 #include <sys/fcntl.h>
     77 #include <sys/vnode.h>
     78 
     79 
     80 #include "rf_archs.h"
     81 #include "rf_threadstuff.h"
     82 
     83 #include <sys/errno.h>
     84 
     85 #include "rf_raid.h"
     86 #include "rf_dag.h"
     87 #include "rf_aselect.h"
     88 #include "rf_diskqueue.h"
     89 #include "rf_parityscan.h"
     90 #include "rf_alloclist.h"
     91 #include "rf_dagutils.h"
     92 #include "rf_utils.h"
     93 #include "rf_etimer.h"
     94 #include "rf_acctrace.h"
     95 #include "rf_general.h"
     96 #include "rf_desc.h"
     97 #include "rf_states.h"
     98 #include "rf_decluster.h"
     99 #include "rf_map.h"
    100 #include "rf_revent.h"
    101 #include "rf_callback.h"
    102 #include "rf_engine.h"
    103 #include "rf_mcpair.h"
    104 #include "rf_nwayxor.h"
    105 #include "rf_copyback.h"
    106 #include "rf_driver.h"
    107 #include "rf_options.h"
    108 #include "rf_shutdown.h"
    109 #include "rf_kintf.h"
    110 
    111 #include <sys/buf.h>
    112 
    113 #ifndef RF_ACCESS_DEBUG
    114 #define RF_ACCESS_DEBUG 0
    115 #endif
    116 
    117 /* rad == RF_RaidAccessDesc_t */
    118 RF_DECLARE_MUTEX(rf_rad_lock)
    119 #define RF_MAX_FREE_RAD 128
    120 #define RF_MIN_FREE_RAD  32
    121 
    122 /* debug variables */
    123 char    rf_panicbuf[2048];	/* a buffer to hold an error msg when we panic */
    124 
    125 /* main configuration routines */
    126 static int raidframe_booted = 0;
    127 
    128 static void rf_ConfigureDebug(RF_Config_t * cfgPtr);
    129 static void set_debug_option(char *name, long val);
    130 static void rf_UnconfigureArray(void);
    131 static void rf_ShutdownRDFreeList(void *);
    132 static int rf_ConfigureRDFreeList(RF_ShutdownList_t **);
    133 
    134 RF_DECLARE_MUTEX(rf_printf_mutex)	/* debug only:  avoids interleaved
    135 					 * printfs by different stripes */
    136 
    137 #define SIGNAL_QUIESCENT_COND(_raid_)  wakeup(&((_raid_)->accesses_suspended))
    138 #define WAIT_FOR_QUIESCENCE(_raid_) \
    139 	ltsleep(&((_raid_)->accesses_suspended), PRIBIO, \
    140 		"raidframe quiesce", 0, &((_raid_)->access_suspend_mutex))
    141 
    142 static int configureCount = 0;	/* number of active configurations */
    143 static int isconfigged = 0;	/* is basic raidframe (non per-array)
    144 				 * stuff configured */
    145 RF_DECLARE_LKMGR_STATIC_MUTEX(configureMutex)	/* used to lock the configuration
    146 					 * stuff */
    147 static RF_ShutdownList_t *globalShutdown;	/* non array-specific
    148 						 * stuff */
    149 
    150 static int rf_ConfigureRDFreeList(RF_ShutdownList_t ** listp);
    151 static int rf_AllocEmergBuffers(RF_Raid_t *);
    152 static void rf_FreeEmergBuffers(RF_Raid_t *);
    153 
    154 /* called at system boot time */
    155 int
    156 rf_BootRaidframe()
    157 {
    158 
    159 	if (raidframe_booted)
    160 		return (EBUSY);
    161 	raidframe_booted = 1;
    162 	mutex_init(&configureMutex, MUTEX_DEFAULT, IPL_NONE);
    163  	configureCount = 0;
    164 	isconfigged = 0;
    165 	globalShutdown = NULL;
    166 	return (0);
    167 }
    168 
    169 /*
    170  * Called whenever an array is shutdown
    171  */
    172 static void
    173 rf_UnconfigureArray()
    174 {
    175 
    176 	RF_LOCK_LKMGR_MUTEX(configureMutex);
    177 	if (--configureCount == 0) {	/* if no active configurations, shut
    178 					 * everything down */
    179 		isconfigged = 0;
    180 		rf_ShutdownList(&globalShutdown);
    181 
    182 		/*
    183 	         * We must wait until now, because the AllocList module
    184 	         * uses the DebugMem module.
    185 	         */
    186 #if RF_DEBUG_MEM
    187 		if (rf_memDebug)
    188 			rf_print_unfreed();
    189 #endif
    190 	}
    191 	RF_UNLOCK_LKMGR_MUTEX(configureMutex);
    192 }
    193 
    194 /*
    195  * Called to shut down an array.
    196  */
    197 int
    198 rf_Shutdown(RF_Raid_t *raidPtr)
    199 {
    200 
    201 	if (!raidPtr->valid) {
    202 		RF_ERRORMSG("Attempt to shut down unconfigured RAIDframe driver.  Aborting shutdown\n");
    203 		return (EINVAL);
    204 	}
    205 	/*
    206          * wait for outstanding IOs to land
    207          * As described in rf_raid.h, we use the rad_freelist lock
    208          * to protect the per-array info about outstanding descs
    209          * since we need to do freelist locking anyway, and this
    210          * cuts down on the amount of serialization we've got going
    211          * on.
    212          */
    213 	RF_LOCK_MUTEX(rf_rad_lock);
    214 	if (raidPtr->waitShutdown) {
    215 		RF_UNLOCK_MUTEX(rf_rad_lock);
    216 		return (EBUSY);
    217 	}
    218 	raidPtr->waitShutdown = 1;
    219 	while (raidPtr->nAccOutstanding) {
    220 		RF_WAIT_COND(raidPtr->outstandingCond, rf_rad_lock);
    221 	}
    222 	RF_UNLOCK_MUTEX(rf_rad_lock);
    223 
    224 	/* Wait for any parity re-writes to stop... */
    225 	while (raidPtr->parity_rewrite_in_progress) {
    226 		printf("raid%d: Waiting for parity re-write to exit...\n",
    227 		       raidPtr->raidid);
    228 		tsleep(&raidPtr->parity_rewrite_in_progress, PRIBIO,
    229 		       "rfprwshutdown", 0);
    230 	}
    231 
    232 	/* Wait for any reconstruction to stop... */
    233 	while (raidPtr->reconInProgress) {
    234 		printf("raid%d: Waiting for reconstruction to stop...\n",
    235 		       raidPtr->raidid);
    236 		tsleep(&raidPtr->waitForReconCond, PRIBIO,
    237 		       "rfreshutdown",0);
    238 	}
    239 
    240 	raidPtr->valid = 0;
    241 
    242 	rf_update_component_labels(raidPtr, RF_FINAL_COMPONENT_UPDATE);
    243 
    244 	rf_UnconfigureVnodes(raidPtr);
    245 
    246 	rf_FreeEmergBuffers(raidPtr);
    247 
    248 	rf_ShutdownList(&raidPtr->shutdownList);
    249 
    250 	rf_UnconfigureArray();
    251 
    252 	return (0);
    253 }
    254 
    255 
    256 #define DO_INIT_CONFIGURE(f) { \
    257 	rc = f (&globalShutdown); \
    258 	if (rc) { \
    259 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    260 		rf_ShutdownList(&globalShutdown); \
    261 		configureCount--; \
    262 		RF_UNLOCK_LKMGR_MUTEX(configureMutex); \
    263 		return(rc); \
    264 	} \
    265 }
    266 
    267 #define DO_RAID_FAIL() { \
    268 	rf_UnconfigureVnodes(raidPtr); \
    269 	rf_FreeEmergBuffers(raidPtr); \
    270 	rf_ShutdownList(&raidPtr->shutdownList); \
    271 	rf_UnconfigureArray(); \
    272 }
    273 
    274 #define DO_RAID_INIT_CONFIGURE(f) { \
    275 	rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
    276 	if (rc) { \
    277 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    278 		DO_RAID_FAIL(); \
    279 		return(rc); \
    280 	} \
    281 }
    282 
    283 #define DO_RAID_MUTEX(_m_) { \
    284 	rf_mutex_init((_m_)); \
    285 }
    286 
    287 int
    288 rf_Configure(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr, RF_AutoConfig_t *ac)
    289 {
    290 	RF_RowCol_t col;
    291 	int rc;
    292 
    293 	RF_LOCK_LKMGR_MUTEX(configureMutex);
    294 	configureCount++;
    295 	if (isconfigged == 0) {
    296 		rf_mutex_init(&rf_printf_mutex);
    297 
    298 		/* initialize globals */
    299 
    300 		DO_INIT_CONFIGURE(rf_ConfigureAllocList);
    301 
    302 		/*
    303 	         * Yes, this does make debugging general to the whole
    304 	         * system instead of being array specific. Bummer, drag.
    305 		 */
    306 		rf_ConfigureDebug(cfgPtr);
    307 		DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
    308 #if RF_ACC_TRACE > 0
    309 		DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
    310 #endif
    311 		DO_INIT_CONFIGURE(rf_ConfigureMapModule);
    312 		DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
    313 		DO_INIT_CONFIGURE(rf_ConfigureCallback);
    314 		DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
    315 		DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
    316 		DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
    317 		DO_INIT_CONFIGURE(rf_ConfigureMCPair);
    318 		DO_INIT_CONFIGURE(rf_ConfigureDAGs);
    319 		DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
    320 		DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
    321 		DO_INIT_CONFIGURE(rf_ConfigureCopyback);
    322 		DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
    323 		DO_INIT_CONFIGURE(rf_ConfigurePSStatus);
    324 		isconfigged = 1;
    325 	}
    326 	RF_UNLOCK_LKMGR_MUTEX(configureMutex);
    327 
    328 	DO_RAID_MUTEX(&raidPtr->mutex);
    329 	/* set up the cleanup list.  Do this after ConfigureDebug so that
    330 	 * value of memDebug will be set */
    331 
    332 	rf_MakeAllocList(raidPtr->cleanupList);
    333 	if (raidPtr->cleanupList == NULL) {
    334 		DO_RAID_FAIL();
    335 		return (ENOMEM);
    336 	}
    337 	rf_ShutdownCreate(&raidPtr->shutdownList,
    338 			  (void (*) (void *)) rf_FreeAllocList,
    339 			  raidPtr->cleanupList);
    340 
    341 	raidPtr->numCol = cfgPtr->numCol;
    342 	raidPtr->numSpare = cfgPtr->numSpare;
    343 
    344 	raidPtr->status = rf_rs_optimal;
    345 	raidPtr->reconControl = NULL;
    346 
    347 	TAILQ_INIT(&(raidPtr->iodone));
    348 	simple_lock_init(&(raidPtr->iodone_lock));
    349 
    350 	DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
    351 	DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
    352 
    353 	raidPtr->outstandingCond = 0;
    354 
    355 	raidPtr->nAccOutstanding = 0;
    356 	raidPtr->waitShutdown = 0;
    357 
    358 	DO_RAID_MUTEX(&raidPtr->access_suspend_mutex);
    359 
    360 	raidPtr->waitForReconCond = 0;
    361 
    362 	if (ac!=NULL) {
    363 		/* We have an AutoConfig structure..  Don't do the
    364 		   normal disk configuration... call the auto config
    365 		   stuff */
    366 		rf_AutoConfigureDisks(raidPtr, cfgPtr, ac);
    367 	} else {
    368 		DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
    369 		DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
    370 	}
    371 	/* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
    372 	 * no. is set */
    373 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
    374 
    375 	DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
    376 
    377 	/* Initialize per-RAID PSS bits */
    378 	rf_InitPSStatus(raidPtr);
    379 
    380 #if RF_INCLUDE_CHAINDECLUSTER > 0
    381 	for (col = 0; col < raidPtr->numCol; col++) {
    382 		/*
    383 		 * XXX better distribution
    384 		 */
    385 		raidPtr->hist_diskreq[col] = 0;
    386 	}
    387 #endif
    388 	raidPtr->numNewFailures = 0;
    389 	raidPtr->copyback_in_progress = 0;
    390 	raidPtr->parity_rewrite_in_progress = 0;
    391 	raidPtr->adding_hot_spare = 0;
    392 	raidPtr->recon_in_progress = 0;
    393 	raidPtr->maxOutstanding = cfgPtr->maxOutstandingDiskReqs;
    394 
    395 	/* autoconfigure and root_partition will actually get filled in
    396 	   after the config is done */
    397 	raidPtr->autoconfigure = 0;
    398 	raidPtr->root_partition = 0;
    399 	raidPtr->last_unit = raidPtr->raidid;
    400 	raidPtr->config_order = 0;
    401 
    402 	if (rf_keepAccTotals) {
    403 		raidPtr->keep_acc_totals = 1;
    404 	}
    405 
    406 	/* Allocate a bunch of buffers to be used in low-memory conditions */
    407 	raidPtr->iobuf = NULL;
    408 
    409 	rc = rf_AllocEmergBuffers(raidPtr);
    410 	if (rc) {
    411 		printf("raid%d: Unable to allocate emergency buffers.\n",
    412 		       raidPtr->raidid);
    413 		DO_RAID_FAIL();
    414 		return(rc);
    415 	}
    416 
    417 	raidPtr->valid = 1;
    418 
    419 	printf("raid%d: %s\n", raidPtr->raidid,
    420 	       raidPtr->Layout.map->configName);
    421 	printf("raid%d: Components:", raidPtr->raidid);
    422 
    423 	for (col = 0; col < raidPtr->numCol; col++) {
    424 		printf(" %s", raidPtr->Disks[col].devname);
    425 		if (RF_DEAD_DISK(raidPtr->Disks[col].status)) {
    426 			printf("[**FAILED**]");
    427 		}
    428 	}
    429 	printf("\n");
    430 	printf("raid%d: Total Sectors: %" PRIu64 " (%" PRIu64 " MB)\n",
    431 	       raidPtr->raidid,
    432 	       raidPtr->totalSectors,
    433 	       (raidPtr->totalSectors / 1024 *
    434 				(1 << raidPtr->logBytesPerSector) / 1024));
    435 
    436 	return (0);
    437 }
    438 
    439 
    440 /*
    441 
    442   Routines to allocate and free the "emergency buffers" for a given
    443   RAID set.  These emergency buffers will be used when the kernel runs
    444   out of kernel memory.
    445 
    446  */
    447 
    448 static int
    449 rf_AllocEmergBuffers(RF_Raid_t *raidPtr)
    450 {
    451 	void *tmpbuf;
    452 	RF_VoidPointerListElem_t *vple;
    453 	int i;
    454 
    455 	/* XXX next line needs tuning... */
    456 	raidPtr->numEmergencyBuffers = 10 * raidPtr->numCol;
    457 #if DEBUG
    458 	printf("raid%d: allocating %d buffers of %d bytes.\n",
    459 	       raidPtr->raidid,
    460 	       raidPtr->numEmergencyBuffers,
    461 	       (int)(raidPtr->Layout.sectorsPerStripeUnit <<
    462 	       raidPtr->logBytesPerSector));
    463 #endif
    464 	for (i = 0; i < raidPtr->numEmergencyBuffers; i++) {
    465 		tmpbuf = malloc( raidPtr->Layout.sectorsPerStripeUnit <<
    466 				 raidPtr->logBytesPerSector,
    467 				 M_RAIDFRAME, M_WAITOK);
    468 		if (tmpbuf) {
    469 			vple = rf_AllocVPListElem();
    470 			vple->p= tmpbuf;
    471 			vple->next = raidPtr->iobuf;
    472 			raidPtr->iobuf = vple;
    473 			raidPtr->iobuf_count++;
    474 		} else {
    475 			printf("raid%d: failed to allocate emergency buffer!\n",
    476 			       raidPtr->raidid);
    477 			return 1;
    478 		}
    479 	}
    480 
    481 	/* XXX next line needs tuning too... */
    482 	raidPtr->numEmergencyStripeBuffers = 10;
    483         for (i = 0; i < raidPtr->numEmergencyStripeBuffers; i++) {
    484                 tmpbuf = malloc( raidPtr->numCol * (raidPtr->Layout.sectorsPerStripeUnit <<
    485                                  raidPtr->logBytesPerSector),
    486                                  M_RAIDFRAME, M_WAITOK);
    487                 if (tmpbuf) {
    488                         vple = rf_AllocVPListElem();
    489                         vple->p= tmpbuf;
    490                         vple->next = raidPtr->stripebuf;
    491                         raidPtr->stripebuf = vple;
    492                         raidPtr->stripebuf_count++;
    493                 } else {
    494                         printf("raid%d: failed to allocate emergency stripe buffer!\n",
    495                                raidPtr->raidid);
    496 			return 1;
    497                 }
    498         }
    499 
    500 	return (0);
    501 }
    502 
    503 static void
    504 rf_FreeEmergBuffers(RF_Raid_t *raidPtr)
    505 {
    506 	RF_VoidPointerListElem_t *tmp;
    507 
    508 	/* Free the emergency IO buffers */
    509 	while (raidPtr->iobuf != NULL) {
    510 		tmp = raidPtr->iobuf;
    511 		raidPtr->iobuf = raidPtr->iobuf->next;
    512 		free(tmp->p, M_RAIDFRAME);
    513 		rf_FreeVPListElem(tmp);
    514 	}
    515 
    516 	/* Free the emergency stripe buffers */
    517 	while (raidPtr->stripebuf != NULL) {
    518 		tmp = raidPtr->stripebuf;
    519 		raidPtr->stripebuf = raidPtr->stripebuf->next;
    520 		free(tmp->p, M_RAIDFRAME);
    521 		rf_FreeVPListElem(tmp);
    522 	}
    523 }
    524 
    525 
    526 static void
    527 rf_ShutdownRDFreeList(void *ignored)
    528 {
    529 	pool_destroy(&rf_pools.rad);
    530 }
    531 
    532 static int
    533 rf_ConfigureRDFreeList(RF_ShutdownList_t **listp)
    534 {
    535 
    536 	rf_pool_init(&rf_pools.rad, sizeof(RF_RaidAccessDesc_t),
    537 		     "rf_rad_pl", RF_MIN_FREE_RAD, RF_MAX_FREE_RAD);
    538 	rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
    539 	simple_lock_init(&rf_rad_lock);
    540 	return (0);
    541 }
    542 
    543 RF_RaidAccessDesc_t *
    544 rf_AllocRaidAccDesc(RF_Raid_t *raidPtr, RF_IoType_t type,
    545 		    RF_RaidAddr_t raidAddress, RF_SectorCount_t numBlocks,
    546 		    void *bufPtr, void *bp, RF_RaidAccessFlags_t flags,
    547 		    const RF_AccessState_t *states)
    548 {
    549 	RF_RaidAccessDesc_t *desc;
    550 
    551 	desc = pool_get(&rf_pools.rad, PR_WAITOK);
    552 
    553 	RF_LOCK_MUTEX(rf_rad_lock);
    554 	if (raidPtr->waitShutdown) {
    555 		/*
    556 	         * Actually, we're shutting the array down. Free the desc
    557 	         * and return NULL.
    558 	         */
    559 
    560 		RF_UNLOCK_MUTEX(rf_rad_lock);
    561 		pool_put(&rf_pools.rad, desc);
    562 		return (NULL);
    563 	}
    564 	raidPtr->nAccOutstanding++;
    565 
    566 	RF_UNLOCK_MUTEX(rf_rad_lock);
    567 
    568 	desc->raidPtr = (void *) raidPtr;
    569 	desc->type = type;
    570 	desc->raidAddress = raidAddress;
    571 	desc->numBlocks = numBlocks;
    572 	desc->bufPtr = bufPtr;
    573 	desc->bp = bp;
    574 	desc->flags = flags;
    575 	desc->states = states;
    576 	desc->state = 0;
    577 	desc->dagList = NULL;
    578 
    579 	desc->status = 0;
    580 	desc->numRetries = 0;
    581 #if RF_ACC_TRACE > 0
    582 	memset((char *) &desc->tracerec, 0, sizeof(RF_AccTraceEntry_t));
    583 #endif
    584 	desc->callbackFunc = NULL;
    585 	desc->callbackArg = NULL;
    586 	desc->next = NULL;
    587 	desc->iobufs = NULL;
    588 	desc->stripebufs = NULL;
    589 
    590 	return (desc);
    591 }
    592 
    593 void
    594 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t *desc)
    595 {
    596 	RF_Raid_t *raidPtr = desc->raidPtr;
    597 	RF_DagList_t *dagList, *temp;
    598 	RF_VoidPointerListElem_t *tmp;
    599 
    600 	RF_ASSERT(desc);
    601 
    602 	/* Cleanup the dagList(s) */
    603 	dagList = desc->dagList;
    604 	while(dagList != NULL) {
    605 		temp = dagList;
    606 		dagList = dagList->next;
    607 		rf_FreeDAGList(temp);
    608 	}
    609 
    610 	while (desc->iobufs) {
    611 		tmp = desc->iobufs;
    612 		desc->iobufs = desc->iobufs->next;
    613 		rf_FreeIOBuffer(raidPtr, tmp);
    614 	}
    615 
    616 	while (desc->stripebufs) {
    617 		tmp = desc->stripebufs;
    618 		desc->stripebufs = desc->stripebufs->next;
    619 		rf_FreeStripeBuffer(raidPtr, tmp);
    620 	}
    621 
    622 	pool_put(&rf_pools.rad, desc);
    623 	RF_LOCK_MUTEX(rf_rad_lock);
    624 	raidPtr->nAccOutstanding--;
    625 	if (raidPtr->waitShutdown) {
    626 		RF_SIGNAL_COND(raidPtr->outstandingCond);
    627 	}
    628 	RF_UNLOCK_MUTEX(rf_rad_lock);
    629 }
    630 /*********************************************************************
    631  * Main routine for performing an access.
    632  * Accesses are retried until a DAG can not be selected.  This occurs
    633  * when either the DAG library is incomplete or there are too many
    634  * failures in a parity group.
    635  *
    636  * type should be read or write async_flag should be RF_TRUE or
    637  * RF_FALSE bp_in is a buf pointer.  void *to facilitate ignoring it
    638  * outside the kernel
    639  ********************************************************************/
    640 int
    641 rf_DoAccess(RF_Raid_t * raidPtr, RF_IoType_t type, int async_flag,
    642 	    RF_RaidAddr_t raidAddress, RF_SectorCount_t numBlocks,
    643 	    void *bufPtr, struct buf *bp, RF_RaidAccessFlags_t flags)
    644 {
    645 	RF_RaidAccessDesc_t *desc;
    646 	void *lbufPtr = bufPtr;
    647 
    648 	raidAddress += rf_raidSectorOffset;
    649 
    650 #if RF_ACCESS_DEBUG
    651 	if (rf_accessDebug) {
    652 
    653 		printf("logBytes is: %d %d %d\n", raidPtr->raidid,
    654 		    raidPtr->logBytesPerSector,
    655 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks));
    656 		printf("raid%d: %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", raidPtr->raidid,
    657 		    (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
    658 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
    659 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
    660 		    (int) numBlocks,
    661 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks),
    662 		    (long) bufPtr);
    663 	}
    664 #endif
    665 
    666 	desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
    667 	    numBlocks, lbufPtr, bp, flags, raidPtr->Layout.map->states);
    668 
    669 	if (desc == NULL) {
    670 		return (ENOMEM);
    671 	}
    672 #if RF_ACC_TRACE > 0
    673 	RF_ETIMER_START(desc->tracerec.tot_timer);
    674 #endif
    675 	desc->async_flag = async_flag;
    676 
    677 	rf_ContinueRaidAccess(desc);
    678 
    679 	return (0);
    680 }
    681 #if 0
    682 /* force the array into reconfigured mode without doing reconstruction */
    683 int
    684 rf_SetReconfiguredMode(RF_Raid_t *raidPtr, int col)
    685 {
    686 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
    687 		printf("Can't set reconfigured mode in dedicated-spare array\n");
    688 		RF_PANIC();
    689 	}
    690 	RF_LOCK_MUTEX(raidPtr->mutex);
    691 	raidPtr->numFailures++;
    692 	raidPtr->Disks[col].status = rf_ds_dist_spared;
    693 	raidPtr->status = rf_rs_reconfigured;
    694 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    695 	/* install spare table only if declustering + distributed sparing
    696 	 * architecture. */
    697 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
    698 		rf_InstallSpareTable(raidPtr, col);
    699 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    700 	return (0);
    701 }
    702 #endif
    703 
    704 int
    705 rf_FailDisk(RF_Raid_t *raidPtr, int fcol, int initRecon)
    706 {
    707 
    708 	/* need to suspend IO's here -- if there are DAGs in flight
    709 	   and we pull the rug out from under ci_vp, Bad Things
    710 	   can happen.  */
    711 
    712 	rf_SuspendNewRequestsAndWait(raidPtr);
    713 
    714 	RF_LOCK_MUTEX(raidPtr->mutex);
    715 	if (raidPtr->Disks[fcol].status != rf_ds_failed) {
    716 		/* must be failing something that is valid, or else it's
    717 		   already marked as failed (in which case we don't
    718 		   want to mark it failed again!) */
    719 		raidPtr->numFailures++;
    720 		raidPtr->Disks[fcol].status = rf_ds_failed;
    721 		raidPtr->status = rf_rs_degraded;
    722 	}
    723 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    724 
    725 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    726 
    727 	/* Close the component, so that it's not "locked" if someone
    728 	   else want's to use it! */
    729 
    730 	rf_close_component(raidPtr, raidPtr->raid_cinfo[fcol].ci_vp,
    731 			   raidPtr->Disks[fcol].auto_configured);
    732 
    733 	RF_LOCK_MUTEX(raidPtr->mutex);
    734 	raidPtr->raid_cinfo[fcol].ci_vp = NULL;
    735 
    736 	/* Need to mark the component as not being auto_configured
    737 	   (in case it was previously). */
    738 
    739 	raidPtr->Disks[fcol].auto_configured = 0;
    740 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    741 	/* now we can allow IO to continue -- we'll be suspending it
    742 	   again in rf_ReconstructFailedDisk() if we have to.. */
    743 
    744 	rf_ResumeNewRequests(raidPtr);
    745 
    746 	if (initRecon)
    747 		rf_ReconstructFailedDisk(raidPtr, fcol);
    748 	return (0);
    749 }
    750 /* releases a thread that is waiting for the array to become quiesced.
    751  * access_suspend_mutex should be locked upon calling this
    752  */
    753 void
    754 rf_SignalQuiescenceLock(RF_Raid_t *raidPtr)
    755 {
    756 #if RF_DEBUG_QUIESCE
    757 	if (rf_quiesceDebug) {
    758 		printf("raid%d: Signalling quiescence lock\n",
    759 		       raidPtr->raidid);
    760 	}
    761 #endif
    762 	raidPtr->access_suspend_release = 1;
    763 
    764 	if (raidPtr->waiting_for_quiescence) {
    765 		SIGNAL_QUIESCENT_COND(raidPtr);
    766 	}
    767 }
    768 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
    769 int
    770 rf_SuspendNewRequestsAndWait(RF_Raid_t *raidPtr)
    771 {
    772 #if RF_DEBUG_QUIESCE
    773 	if (rf_quiesceDebug)
    774 		printf("raid%d: Suspending new reqs\n", raidPtr->raidid);
    775 #endif
    776 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    777 	raidPtr->accesses_suspended++;
    778 	raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
    779 
    780 	if (raidPtr->waiting_for_quiescence) {
    781 		raidPtr->access_suspend_release = 0;
    782 		while (!raidPtr->access_suspend_release) {
    783 #if RF_DEBUG_QUIESCE
    784 			printf("raid%d: Suspending: Waiting for Quiescence\n",
    785 			       raidPtr->raidid);
    786 #endif
    787 			WAIT_FOR_QUIESCENCE(raidPtr);
    788 			raidPtr->waiting_for_quiescence = 0;
    789 		}
    790 	}
    791 #if RF_DEBUG_QUIESCE
    792 	printf("raid%d: Quiescence reached..\n", raidPtr->raidid);
    793 #endif
    794 
    795 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    796 	return (raidPtr->waiting_for_quiescence);
    797 }
    798 /* wake up everyone waiting for quiescence to be released */
    799 void
    800 rf_ResumeNewRequests(RF_Raid_t *raidPtr)
    801 {
    802 	RF_CallbackDesc_t *t, *cb;
    803 
    804 #if RF_DEBUG_QUIESCE
    805 	if (rf_quiesceDebug)
    806 		printf("raid%d: Resuming new requests\n", raidPtr->raidid);
    807 #endif
    808 
    809 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    810 	raidPtr->accesses_suspended--;
    811 	if (raidPtr->accesses_suspended == 0)
    812 		cb = raidPtr->quiesce_wait_list;
    813 	else
    814 		cb = NULL;
    815 	raidPtr->quiesce_wait_list = NULL;
    816 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    817 
    818 	while (cb) {
    819 		t = cb;
    820 		cb = cb->next;
    821 		(t->callbackFunc) (t->callbackArg);
    822 		rf_FreeCallbackDesc(t);
    823 	}
    824 }
    825 /*****************************************************************************************
    826  *
    827  * debug routines
    828  *
    829  ****************************************************************************************/
    830 
    831 static void
    832 set_debug_option(char *name, long val)
    833 {
    834 	RF_DebugName_t *p;
    835 
    836 	for (p = rf_debugNames; p->name; p++) {
    837 		if (!strcmp(p->name, name)) {
    838 			*(p->ptr) = val;
    839 			printf("[Set debug variable %s to %ld]\n", name, val);
    840 			return;
    841 		}
    842 	}
    843 	RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
    844 }
    845 
    846 
    847 /* would like to use sscanf here, but apparently not available in kernel */
    848 /*ARGSUSED*/
    849 static void
    850 rf_ConfigureDebug(RF_Config_t *cfgPtr)
    851 {
    852 	char   *val_p, *name_p, *white_p;
    853 	long    val;
    854 	int     i;
    855 
    856 	rf_ResetDebugOptions();
    857 	for (i = 0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
    858 		name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
    859 		white_p = rf_find_white(name_p);	/* skip to start of 2nd
    860 							 * word */
    861 		val_p = rf_find_non_white(white_p);
    862 		if (*val_p == '0' && *(val_p + 1) == 'x')
    863 			val = rf_htoi(val_p + 2);
    864 		else
    865 			val = rf_atoi(val_p);
    866 		*white_p = '\0';
    867 		set_debug_option(name_p, val);
    868 	}
    869 }
    870 
    871 void
    872 rf_print_panic_message(int line, const char *file)
    873 {
    874 	snprintf(rf_panicbuf, sizeof(rf_panicbuf),
    875 	    "raidframe error at line %d file %s", line, file);
    876 }
    877 
    878 #ifdef RAID_DIAGNOSTIC
    879 void
    880 rf_print_assert_panic_message(int line,	const char *file, const char *condition)
    881 {
    882 	snprintf(rf_panicbuf, sizeof(rf_panicbuf),
    883 		"raidframe error at line %d file %s (failed asserting %s)\n",
    884 		line, file, condition);
    885 }
    886 #endif
    887 
    888 void
    889 rf_print_unable_to_init_mutex(const char *file, int line, int rc)
    890 {
    891 	RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n",
    892 		     file, line, rc);
    893 }
    894 
    895 void
    896 rf_print_unable_to_add_shutdown(const char *file, int line, int rc)
    897 {
    898 	RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
    899 		     file, line, rc);
    900 }
    901