Home | History | Annotate | Line # | Download | only in raidframe
rf_driver.c revision 1.15
      1 /*	$NetBSD: rf_driver.c,v 1.15 1999/08/14 03:10:03 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  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Copyright (c) 1995 Carnegie-Mellon University.
     40  * All rights reserved.
     41  *
     42  * Author: Mark Holland, Khalil Amiri, Claudson Bornstein, William V. Courtright II,
     43  *         Robby Findler, Daniel Stodolsky, Rachad Youssef, Jim Zelenka
     44  *
     45  * Permission to use, copy, modify and distribute this software and
     46  * its documentation is hereby granted, provided that both the copyright
     47  * notice and this permission notice appear in all copies of the
     48  * software, derivative works or modified versions, and any portions
     49  * thereof, and that both notices appear in supporting documentation.
     50  *
     51  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     52  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     53  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     54  *
     55  * Carnegie Mellon requests users of this software to return to
     56  *
     57  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     58  *  School of Computer Science
     59  *  Carnegie Mellon University
     60  *  Pittsburgh PA 15213-3890
     61  *
     62  * any improvements or extensions that they make and grant Carnegie the
     63  * rights to redistribute these changes.
     64  */
     65 
     66 /******************************************************************************
     67  *
     68  * rf_driver.c -- main setup, teardown, and access routines for the RAID driver
     69  *
     70  * all routines are prefixed with rf_ (raidframe), to avoid conficts.
     71  *
     72  ******************************************************************************/
     73 
     74 
     75 #include <sys/types.h>
     76 #include <sys/param.h>
     77 #include <sys/systm.h>
     78 #include <sys/ioctl.h>
     79 #include <sys/fcntl.h>
     80 #include <sys/vnode.h>
     81 
     82 
     83 #include "rf_archs.h"
     84 #include "rf_threadstuff.h"
     85 
     86 #include <sys/errno.h>
     87 
     88 #include "rf_raid.h"
     89 #include "rf_dag.h"
     90 #include "rf_aselect.h"
     91 #include "rf_diskqueue.h"
     92 #include "rf_parityscan.h"
     93 #include "rf_alloclist.h"
     94 #include "rf_threadid.h"
     95 #include "rf_dagutils.h"
     96 #include "rf_utils.h"
     97 #include "rf_etimer.h"
     98 #include "rf_acctrace.h"
     99 #include "rf_configure.h"
    100 #include "rf_general.h"
    101 #include "rf_desc.h"
    102 #include "rf_states.h"
    103 #include "rf_freelist.h"
    104 #include "rf_decluster.h"
    105 #include "rf_map.h"
    106 #include "rf_diskthreads.h"
    107 #include "rf_revent.h"
    108 #include "rf_callback.h"
    109 #include "rf_engine.h"
    110 #include "rf_memchunk.h"
    111 #include "rf_mcpair.h"
    112 #include "rf_nwayxor.h"
    113 #include "rf_debugprint.h"
    114 #include "rf_copyback.h"
    115 #include "rf_driver.h"
    116 #include "rf_options.h"
    117 #include "rf_shutdown.h"
    118 #include "rf_cpuutil.h"
    119 
    120 #include <sys/buf.h>
    121 
    122 #if DKUSAGE > 0
    123 #include <sys/dkusage.h>
    124 #include <io/common/iotypes.h>
    125 #include <io/cam/dec_cam.h>
    126 #include <io/cam/cam.h>
    127 #include <io/cam/pdrv.h>
    128 #endif				/* DKUSAGE > 0 */
    129 
    130 /* rad == RF_RaidAccessDesc_t */
    131 static RF_FreeList_t *rf_rad_freelist;
    132 #define RF_MAX_FREE_RAD 128
    133 #define RF_RAD_INC       16
    134 #define RF_RAD_INITIAL   32
    135 
    136 /* debug variables */
    137 char    rf_panicbuf[2048];	/* a buffer to hold an error msg when we panic */
    138 
    139 /* main configuration routines */
    140 static int raidframe_booted = 0;
    141 
    142 static void rf_ConfigureDebug(RF_Config_t * cfgPtr);
    143 static void set_debug_option(char *name, long val);
    144 static void rf_UnconfigureArray(void);
    145 static int init_rad(RF_RaidAccessDesc_t *);
    146 static void clean_rad(RF_RaidAccessDesc_t *);
    147 static void rf_ShutdownRDFreeList(void *);
    148 static int rf_ConfigureRDFreeList(RF_ShutdownList_t **);
    149 void rf_UnconfigureVnodes( RF_Raid_t * );
    150 
    151 /* XXX move these to their own .h file! */
    152 int raidwrite_component_label(dev_t, struct vnode *, RF_ComponentLabel_t *);
    153 int raidread_component_label(dev_t, struct vnode *, RF_ComponentLabel_t *);
    154 int raidmarkclean(dev_t dev, struct vnode *b_vp,int);
    155 void rf_update_component_labels( RF_Raid_t *);
    156 
    157 RF_DECLARE_MUTEX(rf_printf_mutex)	/* debug only:  avoids interleaved
    158 					 * printfs by different stripes */
    159 RF_DECLARE_GLOBAL_THREADID	/* declarations for threadid.h */
    160 
    161 
    162 #define SIGNAL_QUIESCENT_COND(_raid_)  wakeup(&((_raid_)->accesses_suspended))
    163 #define WAIT_FOR_QUIESCENCE(_raid_) \
    164 	tsleep(&((_raid_)->accesses_suspended),PRIBIO,"raidframe quiesce", 0);
    165 
    166 #if DKUSAGE > 0
    167 #define IO_BUF_ERR(bp, err, unit) { \
    168 	bp->b_flags |= B_ERROR; \
    169 	bp->b_resid = bp->b_bcount; \
    170 	bp->b_error = err; \
    171 	RF_DKU_END_IO(unit, bp); \
    172 	biodone(bp); \
    173 }
    174 #else
    175 #define IO_BUF_ERR(bp, err, unit) { \
    176 	bp->b_flags |= B_ERROR; \
    177 	bp->b_resid = bp->b_bcount; \
    178 	bp->b_error = err; \
    179 	RF_DKU_END_IO(unit); \
    180 	biodone(bp); \
    181 }
    182 #endif				/* DKUSAGE > 0 */
    183 
    184 static int configureCount = 0;	/* number of active configurations */
    185 static int isconfigged = 0;	/* is basic raidframe (non per-array)
    186 				 * stuff configged */
    187 RF_DECLARE_STATIC_MUTEX(configureMutex)	/* used to lock the configuration
    188 					 * stuff */
    189 static RF_ShutdownList_t *globalShutdown;	/* non array-specific
    190 						 * stuff */
    191 
    192 static int rf_ConfigureRDFreeList(RF_ShutdownList_t ** listp);
    193 
    194 /* called at system boot time */
    195 int
    196 rf_BootRaidframe()
    197 {
    198 	int     rc;
    199 
    200 	if (raidframe_booted)
    201 		return (EBUSY);
    202 	raidframe_booted = 1;
    203 
    204 #if RF_DEBUG_ATOMIC > 0
    205 	rf_atent_init();
    206 #endif				/* RF_DEBUG_ATOMIC > 0 */
    207 
    208 	rf_setup_threadid();
    209 	rf_assign_threadid();
    210 
    211 	rc = rf_mutex_init(&configureMutex);
    212 	if (rc) {
    213 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    214 		    __LINE__, rc);
    215 		RF_PANIC();
    216 	}
    217 	configureCount = 0;
    218 	isconfigged = 0;
    219 	globalShutdown = NULL;
    220 	return (0);
    221 }
    222 /*
    223  * This function is really just for debugging user-level stuff: it
    224  * frees up all memory, other RAIDframe resources which might otherwise
    225  * be kept around. This is used with systems like "sentinel" to detect
    226  * memory leaks.
    227  */
    228 int
    229 rf_UnbootRaidframe()
    230 {
    231 	int     rc;
    232 
    233 	RF_LOCK_MUTEX(configureMutex);
    234 	if (configureCount) {
    235 		RF_UNLOCK_MUTEX(configureMutex);
    236 		return (EBUSY);
    237 	}
    238 	raidframe_booted = 0;
    239 	RF_UNLOCK_MUTEX(configureMutex);
    240 	rc = rf_mutex_destroy(&configureMutex);
    241 	if (rc) {
    242 		RF_ERRORMSG3("Unable to destroy mutex file %s line %d rc=%d\n", __FILE__,
    243 		    __LINE__, rc);
    244 		RF_PANIC();
    245 	}
    246 #if RF_DEBUG_ATOMIC > 0
    247 	rf_atent_shutdown();
    248 #endif				/* RF_DEBUG_ATOMIC > 0 */
    249 	return (0);
    250 }
    251 /*
    252  * Called whenever an array is shutdown
    253  */
    254 static void
    255 rf_UnconfigureArray()
    256 {
    257 	int     rc;
    258 
    259 	RF_LOCK_MUTEX(configureMutex);
    260 	if (--configureCount == 0) {	/* if no active configurations, shut
    261 					 * everything down */
    262 		isconfigged = 0;
    263 
    264 		rc = rf_ShutdownList(&globalShutdown);
    265 		if (rc) {
    266 			RF_ERRORMSG1("RAIDFRAME: unable to do global shutdown, rc=%d\n", rc);
    267 		}
    268 		rf_shutdown_threadid();
    269 
    270 		/*
    271 	         * We must wait until now, because the AllocList module
    272 	         * uses the DebugMem module.
    273 	         */
    274 		if (rf_memDebug)
    275 			rf_print_unfreed();
    276 	}
    277 	RF_UNLOCK_MUTEX(configureMutex);
    278 }
    279 
    280 /*
    281  * Called to shut down an array.
    282  */
    283 int
    284 rf_Shutdown(raidPtr)
    285 	RF_Raid_t *raidPtr;
    286 {
    287 
    288 	if (!raidPtr->valid) {
    289 		RF_ERRORMSG("Attempt to shut down unconfigured RAIDframe driver.  Aborting shutdown\n");
    290 		return (EINVAL);
    291 	}
    292 	/*
    293          * wait for outstanding IOs to land
    294          * As described in rf_raid.h, we use the rad_freelist lock
    295          * to protect the per-array info about outstanding descs
    296          * since we need to do freelist locking anyway, and this
    297          * cuts down on the amount of serialization we've got going
    298          * on.
    299          */
    300 	RF_FREELIST_DO_LOCK(rf_rad_freelist);
    301 	if (raidPtr->waitShutdown) {
    302 		RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    303 		return (EBUSY);
    304 	}
    305 	raidPtr->waitShutdown = 1;
    306 	while (raidPtr->nAccOutstanding) {
    307 		RF_WAIT_COND(raidPtr->outstandingCond, RF_FREELIST_MUTEX_OF(rf_rad_freelist));
    308 	}
    309 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    310 
    311 	raidPtr->valid = 0;
    312 
    313 	rf_update_component_labels(raidPtr);
    314 
    315 	rf_UnconfigureVnodes(raidPtr);
    316 
    317 	rf_ShutdownList(&raidPtr->shutdownList);
    318 
    319 	rf_UnconfigureArray();
    320 
    321 	return (0);
    322 }
    323 
    324 void
    325 rf_UnconfigureVnodes( raidPtr )
    326 	RF_Raid_t *raidPtr;
    327 {
    328 	int r,c;
    329 	struct proc *p;
    330 
    331 
    332 	/* We take this opportunity to close the vnodes like we should.. */
    333 
    334 	p = raidPtr->engine_thread;
    335 
    336 	for (r = 0; r < raidPtr->numRow; r++) {
    337 		for (c = 0; c < raidPtr->numCol; c++) {
    338 			printf("Closing vnode for row: %d col: %d\n", r, c);
    339 			if (raidPtr->raid_cinfo[r][c].ci_vp) {
    340 				VOP_UNLOCK(raidPtr->raid_cinfo[r][c].ci_vp, 0);
    341 				(void) vn_close(raidPtr->raid_cinfo[r][c].ci_vp,
    342 				    FREAD | FWRITE, p->p_ucred, p);
    343 				raidPtr->raid_cinfo[r][c].ci_vp = NULL;
    344 			} else {
    345 				printf("vnode was NULL\n");
    346 			}
    347 
    348 		}
    349 	}
    350 	for (r = 0; r < raidPtr->numSpare; r++) {
    351 		printf("Closing vnode for spare: %d\n", r);
    352 		if (raidPtr->raid_cinfo[0][raidPtr->numCol + r].ci_vp) {
    353 			VOP_UNLOCK(raidPtr->raid_cinfo[0][raidPtr->numCol + r].ci_vp, 0);
    354 			(void) vn_close(raidPtr->raid_cinfo[0][raidPtr->numCol + r].ci_vp,
    355 			    FREAD | FWRITE, p->p_ucred, p);
    356 			raidPtr->raid_cinfo[0][raidPtr->numCol + r].ci_vp = NULL;
    357 		} else {
    358 			printf("vnode was NULL\n");
    359 		}
    360 	}
    361 
    362 
    363 }
    364 
    365 
    366 #define DO_INIT_CONFIGURE(f) { \
    367 	rc = f (&globalShutdown); \
    368 	if (rc) { \
    369 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    370 		rf_ShutdownList(&globalShutdown); \
    371 		configureCount--; \
    372 		RF_UNLOCK_MUTEX(configureMutex); \
    373 		return(rc); \
    374 	} \
    375 }
    376 
    377 #define DO_RAID_FAIL() { \
    378 	rf_UnconfigureVnodes(raidPtr); \
    379 	rf_ShutdownList(&raidPtr->shutdownList); \
    380 	rf_UnconfigureArray(); \
    381 }
    382 
    383 #define DO_RAID_INIT_CONFIGURE(f) { \
    384 	rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
    385 	if (rc) { \
    386 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
    387 		DO_RAID_FAIL(); \
    388 		return(rc); \
    389 	} \
    390 }
    391 
    392 #define DO_RAID_MUTEX(_m_) { \
    393 	rc = rf_create_managed_mutex(&raidPtr->shutdownList, (_m_)); \
    394 	if (rc) { \
    395 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", \
    396 			__FILE__, __LINE__, rc); \
    397 		DO_RAID_FAIL(); \
    398 		return(rc); \
    399 	} \
    400 }
    401 
    402 #define DO_RAID_COND(_c_) { \
    403 	rc = rf_create_managed_cond(&raidPtr->shutdownList, (_c_)); \
    404 	if (rc) { \
    405 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", \
    406 			__FILE__, __LINE__, rc); \
    407 		DO_RAID_FAIL(); \
    408 		return(rc); \
    409 	} \
    410 }
    411 
    412 int
    413 rf_Configure(raidPtr, cfgPtr)
    414 	RF_Raid_t *raidPtr;
    415 	RF_Config_t *cfgPtr;
    416 {
    417 	RF_RowCol_t row, col;
    418 	int     i, rc;
    419 	int     unit;
    420 
    421 	if (raidPtr->valid) {
    422 		RF_ERRORMSG("RAIDframe configuration not shut down.  Aborting configure.\n");
    423 		return (EINVAL);
    424 	}
    425 	RF_LOCK_MUTEX(configureMutex);
    426 	configureCount++;
    427 	if (isconfigged == 0) {
    428 		rc = rf_create_managed_mutex(&globalShutdown, &rf_printf_mutex);
    429 		if (rc) {
    430 			RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    431 			    __LINE__, rc);
    432 			rf_ShutdownList(&globalShutdown);
    433 			return (rc);
    434 		}
    435 		/* initialize globals */
    436 		printf("RAIDFRAME: protectedSectors is %ld\n", rf_protectedSectors);
    437 
    438 		rf_clear_debug_print_buffer();
    439 
    440 		DO_INIT_CONFIGURE(rf_ConfigureAllocList);
    441 		/*
    442 	         * Yes, this does make debugging general to the whole system instead
    443 	         * of being array specific. Bummer, drag.
    444 	         */
    445 		rf_ConfigureDebug(cfgPtr);
    446 		DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
    447 		DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
    448 		DO_INIT_CONFIGURE(rf_ConfigureMapModule);
    449 		DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
    450 		DO_INIT_CONFIGURE(rf_ConfigureCallback);
    451 		DO_INIT_CONFIGURE(rf_ConfigureMemChunk);
    452 		DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
    453 		DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
    454 		DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
    455 		DO_INIT_CONFIGURE(rf_ConfigureMCPair);
    456 #if !defined(__NetBSD__)
    457 		DO_INIT_CONFIGURE(rf_ConfigureCamLayer);
    458 #endif
    459 		DO_INIT_CONFIGURE(rf_ConfigureDAGs);
    460 		DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
    461 		DO_INIT_CONFIGURE(rf_ConfigureDebugPrint);
    462 		DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
    463 		DO_INIT_CONFIGURE(rf_ConfigureCopyback);
    464 		DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
    465 		DO_INIT_CONFIGURE(rf_ConfigureCpuMonitor);
    466 		isconfigged = 1;
    467 	}
    468 	RF_UNLOCK_MUTEX(configureMutex);
    469 
    470 	/*
    471          * Null out the entire raid descriptor to avoid problems when we reconfig.
    472          * This also clears the valid bit.
    473          */
    474 	/* XXX this clearing should be moved UP to outside of here.... that,
    475 	 * or rf_Configure() needs to take more arguments... XXX */
    476 	unit = raidPtr->raidid;
    477 	bzero((char *) raidPtr, sizeof(RF_Raid_t));
    478 	raidPtr->raidid = unit;
    479 	DO_RAID_MUTEX(&raidPtr->mutex);
    480 	/* set up the cleanup list.  Do this after ConfigureDebug so that
    481 	 * value of memDebug will be set */
    482 
    483 	rf_MakeAllocList(raidPtr->cleanupList);
    484 	if (raidPtr->cleanupList == NULL) {
    485 		DO_RAID_FAIL();
    486 		return (ENOMEM);
    487 	}
    488 	rc = rf_ShutdownCreate(&raidPtr->shutdownList,
    489 	    (void (*) (void *)) rf_FreeAllocList,
    490 	    raidPtr->cleanupList);
    491 	if (rc) {
    492 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
    493 		    __FILE__, __LINE__, rc);
    494 		DO_RAID_FAIL();
    495 		return (rc);
    496 	}
    497 	raidPtr->numRow = cfgPtr->numRow;
    498 	raidPtr->numCol = cfgPtr->numCol;
    499 	raidPtr->numSpare = cfgPtr->numSpare;
    500 
    501 	/* XXX we don't even pretend to support more than one row in the
    502 	 * kernel... */
    503 	if (raidPtr->numRow != 1) {
    504 		RF_ERRORMSG("Only one row supported in kernel.\n");
    505 		DO_RAID_FAIL();
    506 		return (EINVAL);
    507 	}
    508 	RF_CallocAndAdd(raidPtr->status, raidPtr->numRow, sizeof(RF_RowStatus_t),
    509 	    (RF_RowStatus_t *), raidPtr->cleanupList);
    510 	if (raidPtr->status == NULL) {
    511 		DO_RAID_FAIL();
    512 		return (ENOMEM);
    513 	}
    514 	RF_CallocAndAdd(raidPtr->reconControl, raidPtr->numRow,
    515 	    sizeof(RF_ReconCtrl_t *), (RF_ReconCtrl_t **), raidPtr->cleanupList);
    516 	if (raidPtr->reconControl == NULL) {
    517 		DO_RAID_FAIL();
    518 		return (ENOMEM);
    519 	}
    520 	for (i = 0; i < raidPtr->numRow; i++) {
    521 		raidPtr->status[i] = rf_rs_optimal;
    522 		raidPtr->reconControl[i] = NULL;
    523 	}
    524 
    525 	DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
    526 	DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
    527 
    528 	DO_RAID_COND(&raidPtr->outstandingCond);
    529 
    530 	raidPtr->nAccOutstanding = 0;
    531 	raidPtr->waitShutdown = 0;
    532 
    533 	DO_RAID_MUTEX(&raidPtr->access_suspend_mutex);
    534 	DO_RAID_COND(&raidPtr->quiescent_cond);
    535 
    536 	DO_RAID_COND(&raidPtr->waitForReconCond);
    537 
    538 	DO_RAID_MUTEX(&raidPtr->recon_done_proc_mutex);
    539 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
    540 	DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
    541 	/* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
    542 	 * no. is set */
    543 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
    544 
    545 	DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
    546 
    547 	DO_RAID_INIT_CONFIGURE(rf_ConfigurePSStatus);
    548 
    549 	for (row = 0; row < raidPtr->numRow; row++) {
    550 		for (col = 0; col < raidPtr->numCol; col++) {
    551 			/*
    552 		         * XXX better distribution
    553 		         */
    554 			raidPtr->hist_diskreq[row][col] = 0;
    555 		}
    556 	}
    557 
    558 	if (rf_keepAccTotals) {
    559 		raidPtr->keep_acc_totals = 1;
    560 	}
    561 	rf_StartUserStats(raidPtr);
    562 
    563 	raidPtr->valid = 1;
    564 	return (0);
    565 }
    566 
    567 static int
    568 init_rad(desc)
    569 	RF_RaidAccessDesc_t *desc;
    570 {
    571 	int     rc;
    572 
    573 	rc = rf_mutex_init(&desc->mutex);
    574 	if (rc) {
    575 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    576 		    __LINE__, rc);
    577 		return (rc);
    578 	}
    579 	rc = rf_cond_init(&desc->cond);
    580 	if (rc) {
    581 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
    582 		    __LINE__, rc);
    583 		rf_mutex_destroy(&desc->mutex);
    584 		return (rc);
    585 	}
    586 	return (0);
    587 }
    588 
    589 static void
    590 clean_rad(desc)
    591 	RF_RaidAccessDesc_t *desc;
    592 {
    593 	rf_mutex_destroy(&desc->mutex);
    594 	rf_cond_destroy(&desc->cond);
    595 }
    596 
    597 static void
    598 rf_ShutdownRDFreeList(ignored)
    599 	void   *ignored;
    600 {
    601 	RF_FREELIST_DESTROY_CLEAN(rf_rad_freelist, next, (RF_RaidAccessDesc_t *), clean_rad);
    602 }
    603 
    604 static int
    605 rf_ConfigureRDFreeList(listp)
    606 	RF_ShutdownList_t **listp;
    607 {
    608 	int     rc;
    609 
    610 	RF_FREELIST_CREATE(rf_rad_freelist, RF_MAX_FREE_RAD,
    611 	    RF_RAD_INC, sizeof(RF_RaidAccessDesc_t));
    612 	if (rf_rad_freelist == NULL) {
    613 		return (ENOMEM);
    614 	}
    615 	rc = rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
    616 	if (rc) {
    617 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n", __FILE__,
    618 		    __LINE__, rc);
    619 		rf_ShutdownRDFreeList(NULL);
    620 		return (rc);
    621 	}
    622 	RF_FREELIST_PRIME_INIT(rf_rad_freelist, RF_RAD_INITIAL, next,
    623 	    (RF_RaidAccessDesc_t *), init_rad);
    624 	return (0);
    625 }
    626 
    627 RF_RaidAccessDesc_t *
    628 rf_AllocRaidAccDesc(
    629     RF_Raid_t * raidPtr,
    630     RF_IoType_t type,
    631     RF_RaidAddr_t raidAddress,
    632     RF_SectorCount_t numBlocks,
    633     caddr_t bufPtr,
    634     void *bp,
    635     RF_DagHeader_t ** paramDAG,
    636     RF_AccessStripeMapHeader_t ** paramASM,
    637     RF_RaidAccessFlags_t flags,
    638     void (*cbF) (struct buf *),
    639     void *cbA,
    640     RF_AccessState_t * states)
    641 {
    642 	RF_RaidAccessDesc_t *desc;
    643 
    644 	RF_FREELIST_GET_INIT_NOUNLOCK(rf_rad_freelist, desc, next, (RF_RaidAccessDesc_t *), init_rad);
    645 	if (raidPtr->waitShutdown) {
    646 		/*
    647 	         * Actually, we're shutting the array down. Free the desc
    648 	         * and return NULL.
    649 	         */
    650 		RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    651 		RF_FREELIST_FREE_CLEAN(rf_rad_freelist, desc, next, clean_rad);
    652 		return (NULL);
    653 	}
    654 	raidPtr->nAccOutstanding++;
    655 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    656 
    657 	desc->raidPtr = (void *) raidPtr;
    658 	desc->type = type;
    659 	desc->raidAddress = raidAddress;
    660 	desc->numBlocks = numBlocks;
    661 	desc->bufPtr = bufPtr;
    662 	desc->bp = bp;
    663 	desc->paramDAG = paramDAG;
    664 	desc->paramASM = paramASM;
    665 	desc->flags = flags;
    666 	desc->states = states;
    667 	desc->state = 0;
    668 
    669 	desc->status = 0;
    670 	bzero((char *) &desc->tracerec, sizeof(RF_AccTraceEntry_t));
    671 	desc->callbackFunc = (void (*) (RF_CBParam_t)) cbF;	/* XXX */
    672 	desc->callbackArg = cbA;
    673 	desc->next = NULL;
    674 	desc->head = desc;
    675 	desc->numPending = 0;
    676 	desc->cleanupList = NULL;
    677 	rf_MakeAllocList(desc->cleanupList);
    678 	rf_get_threadid(desc->tid);
    679 	return (desc);
    680 }
    681 
    682 void
    683 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t * desc)
    684 {
    685 	RF_Raid_t *raidPtr = desc->raidPtr;
    686 
    687 	RF_ASSERT(desc);
    688 
    689 	rf_FreeAllocList(desc->cleanupList);
    690 	RF_FREELIST_FREE_CLEAN_NOUNLOCK(rf_rad_freelist, desc, next, clean_rad);
    691 	raidPtr->nAccOutstanding--;
    692 	if (raidPtr->waitShutdown) {
    693 		RF_SIGNAL_COND(raidPtr->outstandingCond);
    694 	}
    695 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
    696 }
    697 /*********************************************************************
    698  * Main routine for performing an access.
    699  * Accesses are retried until a DAG can not be selected.  This occurs
    700  * when either the DAG library is incomplete or there are too many
    701  * failures in a parity group.
    702  ********************************************************************/
    703 int
    704 rf_DoAccess(
    705     RF_Raid_t * raidPtr,
    706     RF_IoType_t type,
    707     int async_flag,
    708     RF_RaidAddr_t raidAddress,
    709     RF_SectorCount_t numBlocks,
    710     caddr_t bufPtr,
    711     void *bp_in,
    712     RF_DagHeader_t ** paramDAG,
    713     RF_AccessStripeMapHeader_t ** paramASM,
    714     RF_RaidAccessFlags_t flags,
    715     RF_RaidAccessDesc_t ** paramDesc,
    716     void (*cbF) (struct buf *),
    717     void *cbA)
    718 /*
    719 type should be read or write
    720 async_flag should be RF_TRUE or RF_FALSE
    721 bp_in is a buf pointer.  void * to facilitate ignoring it outside the kernel
    722 */
    723 {
    724 	int     tid;
    725 	RF_RaidAccessDesc_t *desc;
    726 	caddr_t lbufPtr = bufPtr;
    727 	struct buf *bp = (struct buf *) bp_in;
    728 #if DFSTRACE > 0
    729 	struct {
    730 		RF_uint64 raidAddr;
    731 		int     numBlocks;
    732 		char    type;
    733 	}       dfsrecord;
    734 #endif				/* DFSTRACE > 0 */
    735 
    736 	raidAddress += rf_raidSectorOffset;
    737 
    738 	if (!raidPtr->valid) {
    739 		RF_ERRORMSG("RAIDframe driver not successfully configured.  Rejecting access.\n");
    740 		IO_BUF_ERR(bp, EINVAL, raidPtr->raidid);
    741 		return (EINVAL);
    742 	}
    743 #if defined(KERNEL) && DFSTRACE > 0
    744 	if (rf_DFSTraceAccesses) {
    745 		dfsrecord.raidAddr = raidAddress;
    746 		dfsrecord.numBlocks = numBlocks;
    747 		dfsrecord.type = type;
    748 		dfs_log(DFS_NOTE, (char *) &dfsrecord, sizeof(dfsrecord), 0);
    749 	}
    750 #endif				/* KERNEL && DFSTRACE > 0 */
    751 
    752 	rf_get_threadid(tid);
    753 	if (rf_accessDebug) {
    754 
    755 		printf("logBytes is: %d %d %d\n", raidPtr->raidid,
    756 		    raidPtr->logBytesPerSector,
    757 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks));
    758 		printf("[%d] %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", tid,
    759 		    (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
    760 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
    761 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
    762 		    (int) numBlocks,
    763 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks),
    764 		    (long) bufPtr);
    765 	}
    766 	if (raidAddress + numBlocks > raidPtr->totalSectors) {
    767 
    768 		printf("DoAccess: raid addr %lu too large to access %lu sectors.  Max legal addr is %lu\n",
    769 		    (u_long) raidAddress, (u_long) numBlocks, (u_long) raidPtr->totalSectors);
    770 
    771 		if (type == RF_IO_TYPE_READ) {
    772 			IO_BUF_ERR(bp, ENOSPC, raidPtr->raidid);
    773 			return (ENOSPC);
    774 		} else {
    775 			IO_BUF_ERR(bp, ENOSPC, raidPtr->raidid);
    776 			return (ENOSPC);
    777 		}
    778 	}
    779 	desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
    780 	    numBlocks, lbufPtr, bp, paramDAG, paramASM,
    781 	    flags, cbF, cbA, raidPtr->Layout.map->states);
    782 
    783 	if (desc == NULL) {
    784 		return (ENOMEM);
    785 	}
    786 	RF_ETIMER_START(desc->tracerec.tot_timer);
    787 
    788 	desc->async_flag = async_flag;
    789 
    790 	rf_ContinueRaidAccess(desc);
    791 
    792 	return (0);
    793 }
    794 /* force the array into reconfigured mode without doing reconstruction */
    795 int
    796 rf_SetReconfiguredMode(raidPtr, row, col)
    797 	RF_Raid_t *raidPtr;
    798 	int     row;
    799 	int     col;
    800 {
    801 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
    802 		printf("Can't set reconfigured mode in dedicated-spare array\n");
    803 		RF_PANIC();
    804 	}
    805 	RF_LOCK_MUTEX(raidPtr->mutex);
    806 	raidPtr->numFailures++;
    807 	raidPtr->Disks[row][col].status = rf_ds_dist_spared;
    808 	raidPtr->status[row] = rf_rs_reconfigured;
    809 	/* install spare table only if declustering + distributed sparing
    810 	 * architecture. */
    811 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
    812 		rf_InstallSpareTable(raidPtr, row, col);
    813 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    814 	return (0);
    815 }
    816 
    817 extern int fail_row, fail_col, fail_time;
    818 extern int delayed_recon;
    819 
    820 int
    821 rf_FailDisk(
    822     RF_Raid_t * raidPtr,
    823     int frow,
    824     int fcol,
    825     int initRecon)
    826 {
    827 	int     tid;
    828 
    829 	rf_get_threadid(tid);
    830 	printf("[%d] Failing disk r%d c%d\n", tid, frow, fcol);
    831 	RF_LOCK_MUTEX(raidPtr->mutex);
    832 	raidPtr->numFailures++;
    833 	raidPtr->Disks[frow][fcol].status = rf_ds_failed;
    834 	raidPtr->status[frow] = rf_rs_degraded;
    835 	RF_UNLOCK_MUTEX(raidPtr->mutex);
    836 	if (initRecon)
    837 		rf_ReconstructFailedDisk(raidPtr, frow, fcol);
    838 	return (0);
    839 }
    840 /* releases a thread that is waiting for the array to become quiesced.
    841  * access_suspend_mutex should be locked upon calling this
    842  */
    843 void
    844 rf_SignalQuiescenceLock(raidPtr, reconDesc)
    845 	RF_Raid_t *raidPtr;
    846 	RF_RaidReconDesc_t *reconDesc;
    847 {
    848 	int     tid;
    849 
    850 	if (rf_quiesceDebug) {
    851 		rf_get_threadid(tid);
    852 		printf("[%d] Signalling quiescence lock\n", tid);
    853 	}
    854 	raidPtr->access_suspend_release = 1;
    855 
    856 	if (raidPtr->waiting_for_quiescence) {
    857 		SIGNAL_QUIESCENT_COND(raidPtr);
    858 	}
    859 }
    860 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
    861 int
    862 rf_SuspendNewRequestsAndWait(raidPtr)
    863 	RF_Raid_t *raidPtr;
    864 {
    865 	if (rf_quiesceDebug)
    866 		printf("Suspending new reqs\n");
    867 
    868 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    869 	raidPtr->accesses_suspended++;
    870 	raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
    871 
    872 	if (raidPtr->waiting_for_quiescence) {
    873 		raidPtr->access_suspend_release = 0;
    874 		while (!raidPtr->access_suspend_release) {
    875 			printf("Suspending: Waiting for Quiesence\n");
    876 			WAIT_FOR_QUIESCENCE(raidPtr);
    877 			raidPtr->waiting_for_quiescence = 0;
    878 		}
    879 	}
    880 	printf("Quiesence reached..\n");
    881 
    882 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    883 	return (raidPtr->waiting_for_quiescence);
    884 }
    885 /* wake up everyone waiting for quiescence to be released */
    886 void
    887 rf_ResumeNewRequests(raidPtr)
    888 	RF_Raid_t *raidPtr;
    889 {
    890 	RF_CallbackDesc_t *t, *cb;
    891 
    892 	if (rf_quiesceDebug)
    893 		printf("Resuming new reqs\n");
    894 
    895 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
    896 	raidPtr->accesses_suspended--;
    897 	if (raidPtr->accesses_suspended == 0)
    898 		cb = raidPtr->quiesce_wait_list;
    899 	else
    900 		cb = NULL;
    901 	raidPtr->quiesce_wait_list = NULL;
    902 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
    903 
    904 	while (cb) {
    905 		t = cb;
    906 		cb = cb->next;
    907 		(t->callbackFunc) (t->callbackArg);
    908 		rf_FreeCallbackDesc(t);
    909 	}
    910 }
    911 /*****************************************************************************************
    912  *
    913  * debug routines
    914  *
    915  ****************************************************************************************/
    916 
    917 static void
    918 set_debug_option(name, val)
    919 	char   *name;
    920 	long    val;
    921 {
    922 	RF_DebugName_t *p;
    923 
    924 	for (p = rf_debugNames; p->name; p++) {
    925 		if (!strcmp(p->name, name)) {
    926 			*(p->ptr) = val;
    927 			printf("[Set debug variable %s to %ld]\n", name, val);
    928 			return;
    929 		}
    930 	}
    931 	RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
    932 }
    933 
    934 
    935 /* would like to use sscanf here, but apparently not available in kernel */
    936 /*ARGSUSED*/
    937 static void
    938 rf_ConfigureDebug(cfgPtr)
    939 	RF_Config_t *cfgPtr;
    940 {
    941 	char   *val_p, *name_p, *white_p;
    942 	long    val;
    943 	int     i;
    944 
    945 	rf_ResetDebugOptions();
    946 	for (i = 0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
    947 		name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
    948 		white_p = rf_find_white(name_p);	/* skip to start of 2nd
    949 							 * word */
    950 		val_p = rf_find_non_white(white_p);
    951 		if (*val_p == '0' && *(val_p + 1) == 'x')
    952 			val = rf_htoi(val_p + 2);
    953 		else
    954 			val = rf_atoi(val_p);
    955 		*white_p = '\0';
    956 		set_debug_option(name_p, val);
    957 	}
    958 }
    959 /* performance monitoring stuff */
    960 
    961 #define TIMEVAL_TO_US(t) (((long) t.tv_sec) * 1000000L + (long) t.tv_usec)
    962 
    963 #if !defined(_KERNEL) && !defined(SIMULATE)
    964 
    965 /*
    966  * Throughput stats currently only used in user-level RAIDframe
    967  */
    968 
    969 static int
    970 rf_InitThroughputStats(
    971     RF_ShutdownList_t ** listp,
    972     RF_Raid_t * raidPtr,
    973     RF_Config_t * cfgPtr)
    974 {
    975 	int     rc;
    976 
    977 	/* these used by user-level raidframe only */
    978 	rc = rf_create_managed_mutex(listp, &raidPtr->throughputstats.mutex);
    979 	if (rc) {
    980 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
    981 		    __LINE__, rc);
    982 		return (rc);
    983 	}
    984 	raidPtr->throughputstats.sum_io_us = 0;
    985 	raidPtr->throughputstats.num_ios = 0;
    986 	raidPtr->throughputstats.num_out_ios = 0;
    987 	return (0);
    988 }
    989 
    990 void
    991 rf_StartThroughputStats(RF_Raid_t * raidPtr)
    992 {
    993 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
    994 	raidPtr->throughputstats.num_ios++;
    995 	raidPtr->throughputstats.num_out_ios++;
    996 	if (raidPtr->throughputstats.num_out_ios == 1)
    997 		RF_GETTIME(raidPtr->throughputstats.start);
    998 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
    999 }
   1000 
   1001 static void
   1002 rf_StopThroughputStats(RF_Raid_t * raidPtr)
   1003 {
   1004 	struct timeval diff;
   1005 
   1006 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
   1007 	raidPtr->throughputstats.num_out_ios--;
   1008 	if (raidPtr->throughputstats.num_out_ios == 0) {
   1009 		RF_GETTIME(raidPtr->throughputstats.stop);
   1010 		RF_TIMEVAL_DIFF(&raidPtr->throughputstats.start, &raidPtr->throughputstats.stop, &diff);
   1011 		raidPtr->throughputstats.sum_io_us += TIMEVAL_TO_US(diff);
   1012 	}
   1013 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
   1014 }
   1015 
   1016 static void
   1017 rf_PrintThroughputStats(RF_Raid_t * raidPtr)
   1018 {
   1019 	RF_ASSERT(raidPtr->throughputstats.num_out_ios == 0);
   1020 	if (raidPtr->throughputstats.sum_io_us != 0) {
   1021 		printf("[Througphut: %8.2f IOs/second]\n", raidPtr->throughputstats.num_ios
   1022 		    / (raidPtr->throughputstats.sum_io_us / 1000000.0));
   1023 	}
   1024 }
   1025 #endif				/* !KERNEL && !SIMULATE */
   1026 
   1027 void
   1028 rf_StartUserStats(RF_Raid_t * raidPtr)
   1029 {
   1030 	RF_GETTIME(raidPtr->userstats.start);
   1031 	raidPtr->userstats.sum_io_us = 0;
   1032 	raidPtr->userstats.num_ios = 0;
   1033 	raidPtr->userstats.num_sect_moved = 0;
   1034 }
   1035 
   1036 void
   1037 rf_StopUserStats(RF_Raid_t * raidPtr)
   1038 {
   1039 	RF_GETTIME(raidPtr->userstats.stop);
   1040 }
   1041 
   1042 void
   1043 rf_UpdateUserStats(raidPtr, rt, numsect)
   1044 	RF_Raid_t *raidPtr;
   1045 	int     rt;		/* resp time in us */
   1046 	int     numsect;	/* number of sectors for this access */
   1047 {
   1048 	raidPtr->userstats.sum_io_us += rt;
   1049 	raidPtr->userstats.num_ios++;
   1050 	raidPtr->userstats.num_sect_moved += numsect;
   1051 }
   1052 
   1053 void
   1054 rf_PrintUserStats(RF_Raid_t * raidPtr)
   1055 {
   1056 	long    elapsed_us, mbs, mbs_frac;
   1057 	struct timeval diff;
   1058 
   1059 	RF_TIMEVAL_DIFF(&raidPtr->userstats.start, &raidPtr->userstats.stop, &diff);
   1060 	elapsed_us = TIMEVAL_TO_US(diff);
   1061 
   1062 	/* 2000 sectors per megabyte, 10000000 microseconds per second */
   1063 	if (elapsed_us)
   1064 		mbs = (raidPtr->userstats.num_sect_moved / 2000) / (elapsed_us / 1000000);
   1065 	else
   1066 		mbs = 0;
   1067 
   1068 	/* this computes only the first digit of the fractional mb/s moved */
   1069 	if (elapsed_us) {
   1070 		mbs_frac = ((raidPtr->userstats.num_sect_moved / 200) / (elapsed_us / 1000000))
   1071 		    - (mbs * 10);
   1072 	} else {
   1073 		mbs_frac = 0;
   1074 	}
   1075 
   1076 	printf("Number of I/Os:             %ld\n", raidPtr->userstats.num_ios);
   1077 	printf("Elapsed time (us):          %ld\n", elapsed_us);
   1078 	printf("User I/Os per second:       %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_ios, (elapsed_us / 1000000)));
   1079 	printf("Average user response time: %ld us\n", RF_DB0_CHECK(raidPtr->userstats.sum_io_us, raidPtr->userstats.num_ios));
   1080 	printf("Total sectors moved:        %ld\n", raidPtr->userstats.num_sect_moved);
   1081 	printf("Average access size (sect): %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_sect_moved, raidPtr->userstats.num_ios));
   1082 	printf("Achieved data rate:         %ld.%ld MB/sec\n", mbs, mbs_frac);
   1083 }
   1084