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