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