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