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