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