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