Home | History | Annotate | Line # | Download | only in raidframe
rf_driver.c revision 1.131
      1 /*	$NetBSD: rf_driver.c,v 1.131 2012/12/10 08:36:03 msaitoh 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.131 2012/12/10 08:36:03 msaitoh Exp $");
     70 
     71 #ifdef _KERNEL_OPT
     72 #include "opt_raid_diagnostic.h"
     73 #endif
     74 
     75 #include <sys/param.h>
     76 #include <sys/systm.h>
     77 #include <sys/ioctl.h>
     78 #include <sys/fcntl.h>
     79 #include <sys/vnode.h>
     80 
     81 
     82 #include "rf_archs.h"
     83 #include "rf_threadstuff.h"
     84 
     85 #include <sys/errno.h>
     86 
     87 #include "rf_raid.h"
     88 #include "rf_dag.h"
     89 #include "rf_aselect.h"
     90 #include "rf_diskqueue.h"
     91 #include "rf_parityscan.h"
     92 #include "rf_alloclist.h"
     93 #include "rf_dagutils.h"
     94 #include "rf_utils.h"
     95 #include "rf_etimer.h"
     96 #include "rf_acctrace.h"
     97 #include "rf_general.h"
     98 #include "rf_desc.h"
     99 #include "rf_states.h"
    100 #include "rf_decluster.h"
    101 #include "rf_map.h"
    102 #include "rf_revent.h"
    103 #include "rf_callback.h"
    104 #include "rf_engine.h"
    105 #include "rf_mcpair.h"
    106 #include "rf_nwayxor.h"
    107 #include "rf_copyback.h"
    108 #include "rf_driver.h"
    109 #include "rf_options.h"
    110 #include "rf_shutdown.h"
    111 #include "rf_kintf.h"
    112 #include "rf_paritymap.h"
    113 
    114 #include <sys/buf.h>
    115 
    116 #ifndef RF_ACCESS_DEBUG
    117 #define RF_ACCESS_DEBUG 0
    118 #endif
    119 
    120 /* rad == RF_RaidAccessDesc_t */
    121 #define RF_MAX_FREE_RAD 128
    122 #define RF_MIN_FREE_RAD  32
    123 
    124 /* debug variables */
    125 char    rf_panicbuf[2048];	/* a buffer to hold an error msg when we panic */
    126 
    127 /* main configuration routines */
    128 static int raidframe_booted = 0;
    129 
    130 static void rf_ConfigureDebug(RF_Config_t * cfgPtr);
    131 static void set_debug_option(char *name, long val);
    132 static void rf_UnconfigureArray(void);
    133 static void rf_ShutdownRDFreeList(void *);
    134 static int rf_ConfigureRDFreeList(RF_ShutdownList_t **);
    135 
    136 rf_declare_mutex2(rf_printf_mutex);	/* debug only:  avoids interleaved
    137 					 * printfs by different stripes */
    138 
    139 #define SIGNAL_QUIESCENT_COND(_raid_) \
    140 	rf_broadcast_cond2((_raid_)->access_suspend_cv)
    141 #define WAIT_FOR_QUIESCENCE(_raid_) \
    142 	rf_wait_cond2((_raid_)->access_suspend_cv, \
    143 		      (_raid_)->access_suspend_mutex)
    144 
    145 static int configureCount = 0;	/* number of active configurations */
    146 static int isconfigged = 0;	/* is basic raidframe (non per-array)
    147 				 * stuff configured */
    148 static rf_declare_mutex2(configureMutex); /* used to lock the configuration
    149 					   * stuff */
    150 static RF_ShutdownList_t *globalShutdown;	/* non array-specific
    151 						 * stuff */
    152 
    153 static int rf_ConfigureRDFreeList(RF_ShutdownList_t ** listp);
    154 static int rf_AllocEmergBuffers(RF_Raid_t *);
    155 static void rf_FreeEmergBuffers(RF_Raid_t *);
    156 static void rf_destroy_mutex_cond(RF_Raid_t *);
    157 static void rf_alloc_mutex_cond(RF_Raid_t *);
    158 
    159 /* called at system boot time */
    160 int
    161 rf_BootRaidframe(void)
    162 {
    163 
    164 	if (raidframe_booted)
    165 		return (EBUSY);
    166 	raidframe_booted = 1;
    167 	rf_init_mutex2(configureMutex, IPL_NONE);
    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(void)
    179 {
    180 
    181 	rf_lock_mutex2(configureMutex);
    182 	if (--configureCount == 0) {	/* if no active configurations, shut
    183 					 * everything down */
    184 		rf_destroy_mutex2(rf_printf_mutex);
    185 		isconfigged = 0;
    186 		rf_ShutdownList(&globalShutdown);
    187 
    188 		/*
    189 	         * We must wait until now, because the AllocList module
    190 	         * uses the DebugMem module.
    191 	         */
    192 #if RF_DEBUG_MEM
    193 		if (rf_memDebug)
    194 			rf_print_unfreed();
    195 #endif
    196 	}
    197 	rf_unlock_mutex2(configureMutex);
    198 }
    199 
    200 /*
    201  * Called to shut down an array.
    202  */
    203 int
    204 rf_Shutdown(RF_Raid_t *raidPtr)
    205 {
    206 
    207 	if (!raidPtr->valid) {
    208 		RF_ERRORMSG("Attempt to shut down unconfigured RAIDframe driver.  Aborting shutdown\n");
    209 		return (EINVAL);
    210 	}
    211 	/*
    212          * wait for outstanding IOs to land
    213          * As described in rf_raid.h, we use the rad_freelist lock
    214          * to protect the per-array info about outstanding descs
    215          * since we need to do freelist locking anyway, and this
    216          * cuts down on the amount of serialization we've got going
    217          * on.
    218          */
    219 	rf_lock_mutex2(raidPtr->rad_lock);
    220 	if (raidPtr->waitShutdown) {
    221 		rf_unlock_mutex2(raidPtr->rad_lock);
    222 		return (EBUSY);
    223 	}
    224 	raidPtr->waitShutdown = 1;
    225 	while (raidPtr->nAccOutstanding) {
    226 		rf_wait_cond2(raidPtr->outstandingCond, raidPtr->rad_lock);
    227 	}
    228 	rf_unlock_mutex2(raidPtr->rad_lock);
    229 
    230 	/* Wait for any parity re-writes to stop... */
    231 	while (raidPtr->parity_rewrite_in_progress) {
    232 		printf("raid%d: Waiting for parity re-write to exit...\n",
    233 		       raidPtr->raidid);
    234 		tsleep(&raidPtr->parity_rewrite_in_progress, PRIBIO,
    235 		       "rfprwshutdown", 0);
    236 	}
    237 
    238 	/* Wait for any reconstruction to stop... */
    239 	rf_lock_mutex2(raidPtr->mutex);
    240 	while (raidPtr->reconInProgress) {
    241 		printf("raid%d: Waiting for reconstruction to stop...\n",
    242 		       raidPtr->raidid);
    243 		rf_wait_cond2(raidPtr->waitForReconCond, raidPtr->mutex);
    244 	}
    245 	rf_unlock_mutex2(raidPtr->mutex);
    246 
    247 	raidPtr->valid = 0;
    248 
    249 	if (raidPtr->parity_map != NULL)
    250 		rf_paritymap_detach(raidPtr);
    251 
    252 	rf_update_component_labels(raidPtr, RF_FINAL_COMPONENT_UPDATE);
    253 
    254 	rf_UnconfigureVnodes(raidPtr);
    255 
    256 	rf_FreeEmergBuffers(raidPtr);
    257 
    258 	rf_ShutdownList(&raidPtr->shutdownList);
    259 
    260 	rf_destroy_mutex_cond(raidPtr);
    261 
    262 	rf_UnconfigureArray();
    263 
    264 	return (0);
    265 }
    266 
    267 
    268 #define DO_INIT_CONFIGURE(f) { \
    269 	rc = f (&globalShutdown); \
    270 	if (rc) { \
    271 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    272 		rf_ShutdownList(&globalShutdown); \
    273 		configureCount--; \
    274 		rf_unlock_mutex2(configureMutex); \
    275 		rf_destroy_mutex2(rf_printf_mutex); \
    276 		return(rc); \
    277 	} \
    278 }
    279 
    280 #define DO_RAID_FAIL() { \
    281 	rf_UnconfigureVnodes(raidPtr); \
    282 	rf_FreeEmergBuffers(raidPtr); \
    283 	rf_ShutdownList(&raidPtr->shutdownList); \
    284 	rf_UnconfigureArray(); \
    285 	rf_destroy_mutex_cond(raidPtr); \
    286 }
    287 
    288 #define DO_RAID_INIT_CONFIGURE(f) { \
    289 	rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
    290 	if (rc) { \
    291 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    292 		DO_RAID_FAIL(); \
    293 		return(rc); \
    294 	} \
    295 }
    296 
    297 int
    298 rf_Configure(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr, RF_AutoConfig_t *ac)
    299 {
    300 	RF_RowCol_t col;
    301 	int rc;
    302 
    303 	rf_lock_mutex2(configureMutex);
    304 	configureCount++;
    305 	if (isconfigged == 0) {
    306 		rf_init_mutex2(rf_printf_mutex, IPL_VM);
    307 
    308 		/* initialize globals */
    309 
    310 		DO_INIT_CONFIGURE(rf_ConfigureAllocList);
    311 
    312 		/*
    313 	         * Yes, this does make debugging general to the whole
    314 	         * system instead of being array specific. Bummer, drag.
    315 		 */
    316 		rf_ConfigureDebug(cfgPtr);
    317 		DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
    318 #if RF_ACC_TRACE > 0
    319 		DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
    320 #endif
    321 		DO_INIT_CONFIGURE(rf_ConfigureMapModule);
    322 		DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
    323 		DO_INIT_CONFIGURE(rf_ConfigureCallback);
    324 		DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
    325 		DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
    326 		DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
    327 		DO_INIT_CONFIGURE(rf_ConfigureMCPair);
    328 		DO_INIT_CONFIGURE(rf_ConfigureDAGs);
    329 		DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
    330 		DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
    331 		DO_INIT_CONFIGURE(rf_ConfigureCopyback);
    332 		DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
    333 		DO_INIT_CONFIGURE(rf_ConfigurePSStatus);
    334 		isconfigged = 1;
    335 	}
    336 	rf_unlock_mutex2(configureMutex);
    337 
    338 	rf_alloc_mutex_cond(raidPtr);
    339 
    340 	/* set up the cleanup list.  Do this after ConfigureDebug so that
    341 	 * value of memDebug will be set */
    342 
    343 	rf_MakeAllocList(raidPtr->cleanupList);
    344 	if (raidPtr->cleanupList == NULL) {
    345 		DO_RAID_FAIL();
    346 		return (ENOMEM);
    347 	}
    348 	rf_ShutdownCreate(&raidPtr->shutdownList,
    349 			  (void (*) (void *)) rf_FreeAllocList,
    350 			  raidPtr->cleanupList);
    351 
    352 	raidPtr->numCol = cfgPtr->numCol;
    353 	raidPtr->numSpare = cfgPtr->numSpare;
    354 
    355 	raidPtr->status = rf_rs_optimal;
    356 	raidPtr->reconControl = NULL;
    357 
    358 	DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
    359 	DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
    360 
    361 	raidPtr->nAccOutstanding = 0;
    362 	raidPtr->waitShutdown = 0;
    363 
    364 	if (ac!=NULL) {
    365 		/* We have an AutoConfig structure..  Don't do the
    366 		   normal disk configuration... call the auto config
    367 		   stuff */
    368 		rf_AutoConfigureDisks(raidPtr, cfgPtr, ac);
    369 	} else {
    370 		DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
    371 		DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
    372 	}
    373 	/* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
    374 	 * no. is set */
    375 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
    376 
    377 	DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
    378 
    379 	/* Initialize per-RAID PSS bits */
    380 	rf_InitPSStatus(raidPtr);
    381 
    382 #if RF_INCLUDE_CHAINDECLUSTER > 0
    383 	for (col = 0; col < raidPtr->numCol; col++) {
    384 		/*
    385 		 * XXX better distribution
    386 		 */
    387 		raidPtr->hist_diskreq[col] = 0;
    388 	}
    389 #endif
    390 	raidPtr->numNewFailures = 0;
    391 	raidPtr->copyback_in_progress = 0;
    392 	raidPtr->parity_rewrite_in_progress = 0;
    393 	raidPtr->adding_hot_spare = 0;
    394 	raidPtr->recon_in_progress = 0;
    395 
    396 	raidPtr->maxOutstanding = cfgPtr->maxOutstandingDiskReqs;
    397 
    398 	/* autoconfigure and root_partition will actually get filled in
    399 	   after the config is done */
    400 	raidPtr->autoconfigure = 0;
    401 	raidPtr->root_partition = 0;
    402 	raidPtr->last_unit = raidPtr->raidid;
    403 	raidPtr->config_order = 0;
    404 
    405 	if (rf_keepAccTotals) {
    406 		raidPtr->keep_acc_totals = 1;
    407 	}
    408 
    409 	/* Allocate a bunch of buffers to be used in low-memory conditions */
    410 	raidPtr->iobuf = NULL;
    411 
    412 	rc = rf_AllocEmergBuffers(raidPtr);
    413 	if (rc) {
    414 		printf("raid%d: Unable to allocate emergency buffers.\n",
    415 		       raidPtr->raidid);
    416 		DO_RAID_FAIL();
    417 		return(rc);
    418 	}
    419 
    420 	/* Set up parity map stuff, if applicable. */
    421 #ifndef RF_NO_PARITY_MAP
    422 	rf_paritymap_attach(raidPtr, cfgPtr->force);
    423 #endif
    424 
    425 	raidPtr->valid = 1;
    426 
    427 	printf("raid%d: %s\n", raidPtr->raidid,
    428 	       raidPtr->Layout.map->configName);
    429 	printf("raid%d: Components:", raidPtr->raidid);
    430 
    431 	for (col = 0; col < raidPtr->numCol; col++) {
    432 		printf(" %s", raidPtr->Disks[col].devname);
    433 		if (RF_DEAD_DISK(raidPtr->Disks[col].status)) {
    434 			printf("[**FAILED**]");
    435 		}
    436 	}
    437 	printf("\n");
    438 	printf("raid%d: Total Sectors: %" PRIu64 " (%" PRIu64 " MB)\n",
    439 	       raidPtr->raidid,
    440 	       raidPtr->totalSectors,
    441 	       (raidPtr->totalSectors / 1024 *
    442 				(1 << raidPtr->logBytesPerSector) / 1024));
    443 
    444 	return (0);
    445 }
    446 
    447 
    448 /*
    449 
    450   Routines to allocate and free the "emergency buffers" for a given
    451   RAID set.  These emergency buffers will be used when the kernel runs
    452   out of kernel memory.
    453 
    454  */
    455 
    456 static int
    457 rf_AllocEmergBuffers(RF_Raid_t *raidPtr)
    458 {
    459 	void *tmpbuf;
    460 	RF_VoidPointerListElem_t *vple;
    461 	int i;
    462 
    463 	/* XXX next line needs tuning... */
    464 	raidPtr->numEmergencyBuffers = 10 * raidPtr->numCol;
    465 #if DEBUG
    466 	printf("raid%d: allocating %d buffers of %d bytes.\n",
    467 	       raidPtr->raidid,
    468 	       raidPtr->numEmergencyBuffers,
    469 	       (int)(raidPtr->Layout.sectorsPerStripeUnit <<
    470 	       raidPtr->logBytesPerSector));
    471 #endif
    472 	for (i = 0; i < raidPtr->numEmergencyBuffers; i++) {
    473 		tmpbuf = malloc( raidPtr->Layout.sectorsPerStripeUnit <<
    474 				 raidPtr->logBytesPerSector,
    475 				 M_RAIDFRAME, M_WAITOK);
    476 		if (tmpbuf) {
    477 			vple = rf_AllocVPListElem();
    478 			vple->p= tmpbuf;
    479 			vple->next = raidPtr->iobuf;
    480 			raidPtr->iobuf = vple;
    481 			raidPtr->iobuf_count++;
    482 		} else {
    483 			printf("raid%d: failed to allocate emergency buffer!\n",
    484 			       raidPtr->raidid);
    485 			return 1;
    486 		}
    487 	}
    488 
    489 	/* XXX next line needs tuning too... */
    490 	raidPtr->numEmergencyStripeBuffers = 10;
    491         for (i = 0; i < raidPtr->numEmergencyStripeBuffers; i++) {
    492                 tmpbuf = malloc( raidPtr->numCol * (raidPtr->Layout.sectorsPerStripeUnit <<
    493                                  raidPtr->logBytesPerSector),
    494                                  M_RAIDFRAME, M_WAITOK);
    495                 if (tmpbuf) {
    496                         vple = rf_AllocVPListElem();
    497                         vple->p= tmpbuf;
    498                         vple->next = raidPtr->stripebuf;
    499                         raidPtr->stripebuf = vple;
    500                         raidPtr->stripebuf_count++;
    501                 } else {
    502                         printf("raid%d: failed to allocate emergency stripe buffer!\n",
    503                                raidPtr->raidid);
    504 			return 1;
    505                 }
    506         }
    507 
    508 	return (0);
    509 }
    510 
    511 static void
    512 rf_FreeEmergBuffers(RF_Raid_t *raidPtr)
    513 {
    514 	RF_VoidPointerListElem_t *tmp;
    515 
    516 	/* Free the emergency IO buffers */
    517 	while (raidPtr->iobuf != NULL) {
    518 		tmp = raidPtr->iobuf;
    519 		raidPtr->iobuf = raidPtr->iobuf->next;
    520 		free(tmp->p, M_RAIDFRAME);
    521 		rf_FreeVPListElem(tmp);
    522 	}
    523 
    524 	/* Free the emergency stripe buffers */
    525 	while (raidPtr->stripebuf != NULL) {
    526 		tmp = raidPtr->stripebuf;
    527 		raidPtr->stripebuf = raidPtr->stripebuf->next;
    528 		free(tmp->p, M_RAIDFRAME);
    529 		rf_FreeVPListElem(tmp);
    530 	}
    531 }
    532 
    533 
    534 static void
    535 rf_ShutdownRDFreeList(void *ignored)
    536 {
    537 	pool_destroy(&rf_pools.rad);
    538 }
    539 
    540 static int
    541 rf_ConfigureRDFreeList(RF_ShutdownList_t **listp)
    542 {
    543 
    544 	rf_pool_init(&rf_pools.rad, sizeof(RF_RaidAccessDesc_t),
    545 		     "rf_rad_pl", RF_MIN_FREE_RAD, RF_MAX_FREE_RAD);
    546 	rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
    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 		    void *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_mutex2(raidPtr->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_mutex2(raidPtr->rad_lock);
    568 		pool_put(&rf_pools.rad, desc);
    569 		return (NULL);
    570 	}
    571 	raidPtr->nAccOutstanding++;
    572 
    573 	rf_unlock_mutex2(raidPtr->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_mutex2(raidPtr->rad_lock);
    631 	raidPtr->nAccOutstanding--;
    632 	if (raidPtr->waitShutdown) {
    633 		rf_signal_cond2(raidPtr->outstandingCond);
    634 	}
    635 	rf_unlock_mutex2(raidPtr->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 	    void *bufPtr, struct buf *bp, RF_RaidAccessFlags_t flags)
    651 {
    652 	RF_RaidAccessDesc_t *desc;
    653 	void *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 	if (raidPtr->parity_map != NULL &&
    685 	    type == RF_IO_TYPE_WRITE)
    686 		rf_paritymap_begin(raidPtr->parity_map, raidAddress,
    687 		    numBlocks);
    688 
    689 	rf_ContinueRaidAccess(desc);
    690 
    691 	return (0);
    692 }
    693 #if 0
    694 /* force the array into reconfigured mode without doing reconstruction */
    695 int
    696 rf_SetReconfiguredMode(RF_Raid_t *raidPtr, int col)
    697 {
    698 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
    699 		printf("Can't set reconfigured mode in dedicated-spare array\n");
    700 		RF_PANIC();
    701 	}
    702 	rf_lock_mutex2(raidPtr->mutex);
    703 	raidPtr->numFailures++;
    704 	raidPtr->Disks[col].status = rf_ds_dist_spared;
    705 	raidPtr->status = rf_rs_reconfigured;
    706 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    707 	/* install spare table only if declustering + distributed sparing
    708 	 * architecture. */
    709 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
    710 		rf_InstallSpareTable(raidPtr, col);
    711 	rf_unlock_mutex2(raidPtr->mutex);
    712 	return (0);
    713 }
    714 #endif
    715 
    716 int
    717 rf_FailDisk(RF_Raid_t *raidPtr, int fcol, int initRecon)
    718 {
    719 
    720 	/* need to suspend IO's here -- if there are DAGs in flight
    721 	   and we pull the rug out from under ci_vp, Bad Things
    722 	   can happen.  */
    723 
    724 	rf_SuspendNewRequestsAndWait(raidPtr);
    725 
    726 	rf_lock_mutex2(raidPtr->mutex);
    727 	if (raidPtr->Disks[fcol].status != rf_ds_failed) {
    728 		/* must be failing something that is valid, or else it's
    729 		   already marked as failed (in which case we don't
    730 		   want to mark it failed again!) */
    731 		raidPtr->numFailures++;
    732 		raidPtr->Disks[fcol].status = rf_ds_failed;
    733 		raidPtr->status = rf_rs_degraded;
    734 	}
    735 	rf_unlock_mutex2(raidPtr->mutex);
    736 
    737 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
    738 
    739 	/* Close the component, so that it's not "locked" if someone
    740 	   else want's to use it! */
    741 
    742 	rf_close_component(raidPtr, raidPtr->raid_cinfo[fcol].ci_vp,
    743 			   raidPtr->Disks[fcol].auto_configured);
    744 
    745 	rf_lock_mutex2(raidPtr->mutex);
    746 	raidPtr->raid_cinfo[fcol].ci_vp = NULL;
    747 
    748 	/* Need to mark the component as not being auto_configured
    749 	   (in case it was previously). */
    750 
    751 	raidPtr->Disks[fcol].auto_configured = 0;
    752 	rf_unlock_mutex2(raidPtr->mutex);
    753 	/* now we can allow IO to continue -- we'll be suspending it
    754 	   again in rf_ReconstructFailedDisk() if we have to.. */
    755 
    756 	rf_ResumeNewRequests(raidPtr);
    757 
    758 	if (initRecon)
    759 		rf_ReconstructFailedDisk(raidPtr, fcol);
    760 	return (0);
    761 }
    762 /* releases a thread that is waiting for the array to become quiesced.
    763  * access_suspend_mutex should be locked upon calling this
    764  */
    765 void
    766 rf_SignalQuiescenceLock(RF_Raid_t *raidPtr)
    767 {
    768 #if RF_DEBUG_QUIESCE
    769 	if (rf_quiesceDebug) {
    770 		printf("raid%d: Signalling quiescence lock\n",
    771 		       raidPtr->raidid);
    772 	}
    773 #endif
    774 	raidPtr->access_suspend_release = 1;
    775 
    776 	if (raidPtr->waiting_for_quiescence) {
    777 		SIGNAL_QUIESCENT_COND(raidPtr);
    778 	}
    779 }
    780 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
    781 int
    782 rf_SuspendNewRequestsAndWait(RF_Raid_t *raidPtr)
    783 {
    784 #if RF_DEBUG_QUIESCE
    785 	if (rf_quiesceDebug)
    786 		printf("raid%d: Suspending new reqs\n", raidPtr->raidid);
    787 #endif
    788 	rf_lock_mutex2(raidPtr->access_suspend_mutex);
    789 	raidPtr->accesses_suspended++;
    790 	raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
    791 
    792 	if (raidPtr->waiting_for_quiescence) {
    793 		raidPtr->access_suspend_release = 0;
    794 		while (!raidPtr->access_suspend_release) {
    795 #if RF_DEBUG_QUIESCE
    796 			printf("raid%d: Suspending: Waiting for Quiescence\n",
    797 			       raidPtr->raidid);
    798 #endif
    799 			WAIT_FOR_QUIESCENCE(raidPtr);
    800 			raidPtr->waiting_for_quiescence = 0;
    801 		}
    802 	}
    803 #if RF_DEBUG_QUIESCE
    804 	printf("raid%d: Quiescence reached..\n", raidPtr->raidid);
    805 #endif
    806 
    807 	rf_unlock_mutex2(raidPtr->access_suspend_mutex);
    808 	return (raidPtr->waiting_for_quiescence);
    809 }
    810 /* wake up everyone waiting for quiescence to be released */
    811 void
    812 rf_ResumeNewRequests(RF_Raid_t *raidPtr)
    813 {
    814 	RF_CallbackDesc_t *t, *cb;
    815 
    816 #if RF_DEBUG_QUIESCE
    817 	if (rf_quiesceDebug)
    818 		printf("raid%d: Resuming new requests\n", raidPtr->raidid);
    819 #endif
    820 
    821 	rf_lock_mutex2(raidPtr->access_suspend_mutex);
    822 	raidPtr->accesses_suspended--;
    823 	if (raidPtr->accesses_suspended == 0)
    824 		cb = raidPtr->quiesce_wait_list;
    825 	else
    826 		cb = NULL;
    827 	raidPtr->quiesce_wait_list = NULL;
    828 	rf_unlock_mutex2(raidPtr->access_suspend_mutex);
    829 
    830 	while (cb) {
    831 		t = cb;
    832 		cb = cb->next;
    833 		(t->callbackFunc) (t->callbackArg);
    834 		rf_FreeCallbackDesc(t);
    835 	}
    836 }
    837 /*****************************************************************************************
    838  *
    839  * debug routines
    840  *
    841  ****************************************************************************************/
    842 
    843 static void
    844 set_debug_option(char *name, long val)
    845 {
    846 	RF_DebugName_t *p;
    847 
    848 	for (p = rf_debugNames; p->name; p++) {
    849 		if (!strcmp(p->name, name)) {
    850 			*(p->ptr) = val;
    851 			printf("[Set debug variable %s to %ld]\n", name, val);
    852 			return;
    853 		}
    854 	}
    855 	RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
    856 }
    857 
    858 
    859 /* would like to use sscanf here, but apparently not available in kernel */
    860 /*ARGSUSED*/
    861 static void
    862 rf_ConfigureDebug(RF_Config_t *cfgPtr)
    863 {
    864 	char   *val_p, *name_p, *white_p;
    865 	long    val;
    866 	int     i;
    867 
    868 	rf_ResetDebugOptions();
    869 	for (i = 0; i < RF_MAXDBGV && cfgPtr->debugVars[i][0]; i++) {
    870 		name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
    871 		white_p = rf_find_white(name_p);	/* skip to start of 2nd
    872 							 * word */
    873 		val_p = rf_find_non_white(white_p);
    874 		if (*val_p == '0' && *(val_p + 1) == 'x')
    875 			val = rf_htoi(val_p + 2);
    876 		else
    877 			val = rf_atoi(val_p);
    878 		*white_p = '\0';
    879 		set_debug_option(name_p, val);
    880 	}
    881 }
    882 
    883 void
    884 rf_print_panic_message(int line, const char *file)
    885 {
    886 	snprintf(rf_panicbuf, sizeof(rf_panicbuf),
    887 	    "raidframe error at line %d file %s", line, file);
    888 }
    889 
    890 #ifdef RAID_DIAGNOSTIC
    891 void
    892 rf_print_assert_panic_message(int line,	const char *file, const char *condition)
    893 {
    894 	snprintf(rf_panicbuf, sizeof(rf_panicbuf),
    895 		"raidframe error at line %d file %s (failed asserting %s)\n",
    896 		line, file, condition);
    897 }
    898 #endif
    899 
    900 void
    901 rf_print_unable_to_init_mutex(const char *file, int line, int rc)
    902 {
    903 	RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n",
    904 		     file, line, rc);
    905 }
    906 
    907 void
    908 rf_print_unable_to_add_shutdown(const char *file, int line, int rc)
    909 {
    910 	RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
    911 		     file, line, rc);
    912 }
    913 
    914 static void
    915 rf_alloc_mutex_cond(RF_Raid_t *raidPtr)
    916 {
    917 
    918 	rf_init_mutex2(raidPtr->mutex, IPL_VM);
    919 
    920 	rf_init_cond2(raidPtr->outstandingCond, "rfocond");
    921 	rf_init_mutex2(raidPtr->rad_lock, IPL_VM);
    922 
    923 	rf_init_mutex2(raidPtr->access_suspend_mutex, IPL_VM);
    924 	rf_init_cond2(raidPtr->access_suspend_cv, "rfquiesce");
    925 
    926 	rf_init_cond2(raidPtr->waitForReconCond, "rfrcnw");
    927 
    928 	rf_init_cond2(raidPtr->adding_hot_spare_cv, "raidhs");
    929 }
    930 
    931 static void
    932 rf_destroy_mutex_cond(RF_Raid_t *raidPtr)
    933 {
    934 
    935 	rf_destroy_cond2(raidPtr->waitForReconCond);
    936 	rf_destroy_cond2(raidPtr->adding_hot_spare_cv);
    937 
    938 	rf_destroy_mutex2(raidPtr->access_suspend_mutex);
    939 	rf_destroy_cond2(raidPtr->access_suspend_cv);
    940 
    941 	rf_destroy_cond2(raidPtr->outstandingCond);
    942 	rf_destroy_mutex2(raidPtr->rad_lock);
    943 
    944 	rf_destroy_mutex2(raidPtr->mutex);
    945 }
    946