xref: /netbsd-src/sys/dev/raidframe/rf_driver.c (revision 7cc2f76925f078d01ddc9e640a98f4ccfc9f8c3b)
1 /*	$NetBSD: rf_driver.c,v 1.39 2000/12/15 02:12:58 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@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 
140 RF_DECLARE_MUTEX(rf_printf_mutex)	/* debug only:  avoids interleaved
141 					 * printfs by different stripes */
142 
143 #define SIGNAL_QUIESCENT_COND(_raid_)  wakeup(&((_raid_)->accesses_suspended))
144 #define WAIT_FOR_QUIESCENCE(_raid_) \
145 	ltsleep(&((_raid_)->accesses_suspended), PRIBIO, \
146 		"raidframe quiesce", 0, &((_raid_)->access_suspend_mutex))
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 	/* Wait for any parity re-writes to stop... */
272 	while (raidPtr->parity_rewrite_in_progress) {
273 		printf("Waiting for parity re-write to exit...\n");
274 		tsleep(&raidPtr->parity_rewrite_in_progress, PRIBIO,
275 		       "rfprwshutdown", 0);
276 	}
277 
278 	raidPtr->valid = 0;
279 
280 	rf_update_component_labels(raidPtr, RF_FINAL_COMPONENT_UPDATE);
281 
282 	rf_UnconfigureVnodes(raidPtr);
283 
284 	rf_ShutdownList(&raidPtr->shutdownList);
285 
286 	rf_UnconfigureArray();
287 
288 	return (0);
289 }
290 
291 
292 #define DO_INIT_CONFIGURE(f) { \
293 	rc = f (&globalShutdown); \
294 	if (rc) { \
295 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
296 		rf_ShutdownList(&globalShutdown); \
297 		configureCount--; \
298 		RF_UNLOCK_MUTEX(configureMutex); \
299 		return(rc); \
300 	} \
301 }
302 
303 #define DO_RAID_FAIL() { \
304 	rf_UnconfigureVnodes(raidPtr); \
305 	rf_ShutdownList(&raidPtr->shutdownList); \
306 	rf_UnconfigureArray(); \
307 }
308 
309 #define DO_RAID_INIT_CONFIGURE(f) { \
310 	rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
311 	if (rc) { \
312 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
313 		DO_RAID_FAIL(); \
314 		return(rc); \
315 	} \
316 }
317 
318 #define DO_RAID_MUTEX(_m_) { \
319 	rc = rf_create_managed_mutex(&raidPtr->shutdownList, (_m_)); \
320 	if (rc) { \
321 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", \
322 			__FILE__, __LINE__, rc); \
323 		DO_RAID_FAIL(); \
324 		return(rc); \
325 	} \
326 }
327 
328 #define DO_RAID_COND(_c_) { \
329 	rc = rf_create_managed_cond(&raidPtr->shutdownList, (_c_)); \
330 	if (rc) { \
331 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", \
332 			__FILE__, __LINE__, rc); \
333 		DO_RAID_FAIL(); \
334 		return(rc); \
335 	} \
336 }
337 
338 int
339 rf_Configure(raidPtr, cfgPtr, ac)
340 	RF_Raid_t *raidPtr;
341 	RF_Config_t *cfgPtr;
342 	RF_AutoConfig_t *ac;
343 {
344 	RF_RowCol_t row, col;
345 	int     i, rc;
346 
347 	/* XXX This check can probably be removed now, since
348 	   RAIDFRAME_CONFIGURRE now checks to make sure that the
349 	   RAID set is not already valid
350 	*/
351 	if (raidPtr->valid) {
352 		RF_ERRORMSG("RAIDframe configuration not shut down.  Aborting configure.\n");
353 		return (EINVAL);
354 	}
355 	RF_LOCK_MUTEX(configureMutex);
356 	configureCount++;
357 	if (isconfigged == 0) {
358 		rc = rf_create_managed_mutex(&globalShutdown, &rf_printf_mutex);
359 		if (rc) {
360 			RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
361 			    __LINE__, rc);
362 			rf_ShutdownList(&globalShutdown);
363 			return (rc);
364 		}
365 		/* initialize globals */
366 		printf("RAIDFRAME: protectedSectors is %ld\n",
367 		       rf_protectedSectors);
368 
369 		rf_clear_debug_print_buffer();
370 
371 		DO_INIT_CONFIGURE(rf_ConfigureAllocList);
372 
373 		/*
374 	         * Yes, this does make debugging general to the whole
375 	         * system instead of being array specific. Bummer, drag.
376 		 */
377 		rf_ConfigureDebug(cfgPtr);
378 		DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
379 		DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
380 		DO_INIT_CONFIGURE(rf_ConfigureMapModule);
381 		DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
382 		DO_INIT_CONFIGURE(rf_ConfigureCallback);
383 		DO_INIT_CONFIGURE(rf_ConfigureMemChunk);
384 		DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
385 		DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
386 		DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
387 		DO_INIT_CONFIGURE(rf_ConfigureMCPair);
388 		DO_INIT_CONFIGURE(rf_ConfigureDAGs);
389 		DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
390 		DO_INIT_CONFIGURE(rf_ConfigureDebugPrint);
391 		DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
392 		DO_INIT_CONFIGURE(rf_ConfigureCopyback);
393 		DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
394 		isconfigged = 1;
395 	}
396 	RF_UNLOCK_MUTEX(configureMutex);
397 
398 	DO_RAID_MUTEX(&raidPtr->mutex);
399 	/* set up the cleanup list.  Do this after ConfigureDebug so that
400 	 * value of memDebug will be set */
401 
402 	rf_MakeAllocList(raidPtr->cleanupList);
403 	if (raidPtr->cleanupList == NULL) {
404 		DO_RAID_FAIL();
405 		return (ENOMEM);
406 	}
407 	rc = rf_ShutdownCreate(&raidPtr->shutdownList,
408 	    (void (*) (void *)) rf_FreeAllocList,
409 	    raidPtr->cleanupList);
410 	if (rc) {
411 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
412 		    __FILE__, __LINE__, rc);
413 		DO_RAID_FAIL();
414 		return (rc);
415 	}
416 	raidPtr->numRow = cfgPtr->numRow;
417 	raidPtr->numCol = cfgPtr->numCol;
418 	raidPtr->numSpare = cfgPtr->numSpare;
419 
420 	/* XXX we don't even pretend to support more than one row in the
421 	 * kernel... */
422 	if (raidPtr->numRow != 1) {
423 		RF_ERRORMSG("Only one row supported in kernel.\n");
424 		DO_RAID_FAIL();
425 		return (EINVAL);
426 	}
427 	RF_CallocAndAdd(raidPtr->status, raidPtr->numRow, sizeof(RF_RowStatus_t),
428 	    (RF_RowStatus_t *), raidPtr->cleanupList);
429 	if (raidPtr->status == NULL) {
430 		DO_RAID_FAIL();
431 		return (ENOMEM);
432 	}
433 	RF_CallocAndAdd(raidPtr->reconControl, raidPtr->numRow,
434 	    sizeof(RF_ReconCtrl_t *), (RF_ReconCtrl_t **), raidPtr->cleanupList);
435 	if (raidPtr->reconControl == NULL) {
436 		DO_RAID_FAIL();
437 		return (ENOMEM);
438 	}
439 	for (i = 0; i < raidPtr->numRow; i++) {
440 		raidPtr->status[i] = rf_rs_optimal;
441 		raidPtr->reconControl[i] = NULL;
442 	}
443 
444 	DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
445 	DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
446 
447 	DO_RAID_COND(&raidPtr->outstandingCond);
448 
449 	raidPtr->nAccOutstanding = 0;
450 	raidPtr->waitShutdown = 0;
451 
452 	DO_RAID_MUTEX(&raidPtr->access_suspend_mutex);
453 	DO_RAID_COND(&raidPtr->quiescent_cond);
454 
455 	DO_RAID_COND(&raidPtr->waitForReconCond);
456 
457 	DO_RAID_MUTEX(&raidPtr->recon_done_proc_mutex);
458 
459 	if (ac!=NULL) {
460 		/* We have an AutoConfig structure..  Don't do the
461 		   normal disk configuration... call the auto config
462 		   stuff */
463 		rf_AutoConfigureDisks(raidPtr, cfgPtr, ac);
464 	} else {
465 		DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
466 		DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
467 	}
468 	/* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
469 	 * no. is set */
470 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
471 
472 	DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
473 
474 	DO_RAID_INIT_CONFIGURE(rf_ConfigurePSStatus);
475 
476 	for (row = 0; row < raidPtr->numRow; row++) {
477 		for (col = 0; col < raidPtr->numCol; col++) {
478 			/*
479 		         * XXX better distribution
480 		         */
481 			raidPtr->hist_diskreq[row][col] = 0;
482 		}
483 	}
484 
485 	raidPtr->numNewFailures = 0;
486 	raidPtr->copyback_in_progress = 0;
487 	raidPtr->parity_rewrite_in_progress = 0;
488 	raidPtr->recon_in_progress = 0;
489 	raidPtr->maxOutstanding = cfgPtr->maxOutstandingDiskReqs;
490 
491 	/* autoconfigure and root_partition will actually get filled in
492 	   after the config is done */
493 	raidPtr->autoconfigure = 0;
494 	raidPtr->root_partition = 0;
495 	raidPtr->last_unit = raidPtr->raidid;
496 	raidPtr->config_order = 0;
497 
498 	if (rf_keepAccTotals) {
499 		raidPtr->keep_acc_totals = 1;
500 	}
501 	rf_StartUserStats(raidPtr);
502 
503 	raidPtr->valid = 1;
504 	return (0);
505 }
506 
507 static int
508 init_rad(desc)
509 	RF_RaidAccessDesc_t *desc;
510 {
511 	int     rc;
512 
513 	rc = rf_mutex_init(&desc->mutex);
514 	if (rc) {
515 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
516 		    __LINE__, rc);
517 		return (rc);
518 	}
519 	rc = rf_cond_init(&desc->cond);
520 	if (rc) {
521 		RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
522 		    __LINE__, rc);
523 		rf_mutex_destroy(&desc->mutex);
524 		return (rc);
525 	}
526 	return (0);
527 }
528 
529 static void
530 clean_rad(desc)
531 	RF_RaidAccessDesc_t *desc;
532 {
533 	rf_mutex_destroy(&desc->mutex);
534 	rf_cond_destroy(&desc->cond);
535 }
536 
537 static void
538 rf_ShutdownRDFreeList(ignored)
539 	void   *ignored;
540 {
541 	RF_FREELIST_DESTROY_CLEAN(rf_rad_freelist, next, (RF_RaidAccessDesc_t *), clean_rad);
542 }
543 
544 static int
545 rf_ConfigureRDFreeList(listp)
546 	RF_ShutdownList_t **listp;
547 {
548 	int     rc;
549 
550 	RF_FREELIST_CREATE(rf_rad_freelist, RF_MAX_FREE_RAD,
551 	    RF_RAD_INC, sizeof(RF_RaidAccessDesc_t));
552 	if (rf_rad_freelist == NULL) {
553 		return (ENOMEM);
554 	}
555 	rc = rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
556 	if (rc) {
557 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n", __FILE__,
558 		    __LINE__, rc);
559 		rf_ShutdownRDFreeList(NULL);
560 		return (rc);
561 	}
562 	RF_FREELIST_PRIME_INIT(rf_rad_freelist, RF_RAD_INITIAL, next,
563 	    (RF_RaidAccessDesc_t *), init_rad);
564 	return (0);
565 }
566 
567 RF_RaidAccessDesc_t *
568 rf_AllocRaidAccDesc(
569     RF_Raid_t * raidPtr,
570     RF_IoType_t type,
571     RF_RaidAddr_t raidAddress,
572     RF_SectorCount_t numBlocks,
573     caddr_t bufPtr,
574     void *bp,
575     RF_DagHeader_t ** paramDAG,
576     RF_AccessStripeMapHeader_t ** paramASM,
577     RF_RaidAccessFlags_t flags,
578     void (*cbF) (struct buf *),
579     void *cbA,
580     RF_AccessState_t * states)
581 {
582 	RF_RaidAccessDesc_t *desc;
583 
584 	RF_FREELIST_GET_INIT_NOUNLOCK(rf_rad_freelist, desc, next, (RF_RaidAccessDesc_t *), init_rad);
585 	if (raidPtr->waitShutdown) {
586 		/*
587 	         * Actually, we're shutting the array down. Free the desc
588 	         * and return NULL.
589 	         */
590 		RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
591 		RF_FREELIST_FREE_CLEAN(rf_rad_freelist, desc, next, clean_rad);
592 		return (NULL);
593 	}
594 	raidPtr->nAccOutstanding++;
595 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
596 
597 	desc->raidPtr = (void *) raidPtr;
598 	desc->type = type;
599 	desc->raidAddress = raidAddress;
600 	desc->numBlocks = numBlocks;
601 	desc->bufPtr = bufPtr;
602 	desc->bp = bp;
603 	desc->paramDAG = paramDAG;
604 	desc->paramASM = paramASM;
605 	desc->flags = flags;
606 	desc->states = states;
607 	desc->state = 0;
608 
609 	desc->status = 0;
610 	bzero((char *) &desc->tracerec, sizeof(RF_AccTraceEntry_t));
611 	desc->callbackFunc = (void (*) (RF_CBParam_t)) cbF;	/* XXX */
612 	desc->callbackArg = cbA;
613 	desc->next = NULL;
614 	desc->head = desc;
615 	desc->numPending = 0;
616 	desc->cleanupList = NULL;
617 	rf_MakeAllocList(desc->cleanupList);
618 	return (desc);
619 }
620 
621 void
622 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t * desc)
623 {
624 	RF_Raid_t *raidPtr = desc->raidPtr;
625 
626 	RF_ASSERT(desc);
627 
628 	rf_FreeAllocList(desc->cleanupList);
629 	RF_FREELIST_FREE_CLEAN_NOUNLOCK(rf_rad_freelist, desc, next, clean_rad);
630 	raidPtr->nAccOutstanding--;
631 	if (raidPtr->waitShutdown) {
632 		RF_SIGNAL_COND(raidPtr->outstandingCond);
633 	}
634 	RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
635 }
636 /*********************************************************************
637  * Main routine for performing an access.
638  * Accesses are retried until a DAG can not be selected.  This occurs
639  * when either the DAG library is incomplete or there are too many
640  * failures in a parity group.
641  ********************************************************************/
642 int
643 rf_DoAccess(
644     RF_Raid_t * raidPtr,
645     RF_IoType_t type,
646     int async_flag,
647     RF_RaidAddr_t raidAddress,
648     RF_SectorCount_t numBlocks,
649     caddr_t bufPtr,
650     void *bp_in,
651     RF_DagHeader_t ** paramDAG,
652     RF_AccessStripeMapHeader_t ** paramASM,
653     RF_RaidAccessFlags_t flags,
654     RF_RaidAccessDesc_t ** paramDesc,
655     void (*cbF) (struct buf *),
656     void *cbA)
657 /*
658 type should be read or write
659 async_flag should be RF_TRUE or RF_FALSE
660 bp_in is a buf pointer.  void * to facilitate ignoring it outside the kernel
661 */
662 {
663 	RF_RaidAccessDesc_t *desc;
664 	caddr_t lbufPtr = bufPtr;
665 	struct buf *bp = (struct buf *) bp_in;
666 
667 	raidAddress += rf_raidSectorOffset;
668 
669 	if (!raidPtr->valid) {
670 		RF_ERRORMSG("RAIDframe driver not successfully configured.  Rejecting access.\n");
671 		IO_BUF_ERR(bp, EINVAL);
672 		return (EINVAL);
673 	}
674 
675 	if (rf_accessDebug) {
676 
677 		printf("logBytes is: %d %d %d\n", raidPtr->raidid,
678 		    raidPtr->logBytesPerSector,
679 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks));
680 		printf("raid%d: %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", raidPtr->raidid,
681 		    (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
682 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
683 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
684 		    (int) numBlocks,
685 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks),
686 		    (long) bufPtr);
687 	}
688 	if (raidAddress + numBlocks > raidPtr->totalSectors) {
689 
690 		printf("DoAccess: raid addr %lu too large to access %lu sectors.  Max legal addr is %lu\n",
691 		    (u_long) raidAddress, (u_long) numBlocks, (u_long) raidPtr->totalSectors);
692 
693 		IO_BUF_ERR(bp, ENOSPC);
694 		return (ENOSPC);
695 	}
696 	desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
697 	    numBlocks, lbufPtr, bp, paramDAG, paramASM,
698 	    flags, cbF, cbA, raidPtr->Layout.map->states);
699 
700 	if (desc == NULL) {
701 		return (ENOMEM);
702 	}
703 	RF_ETIMER_START(desc->tracerec.tot_timer);
704 
705 	desc->async_flag = async_flag;
706 
707 	rf_ContinueRaidAccess(desc);
708 
709 	return (0);
710 }
711 /* force the array into reconfigured mode without doing reconstruction */
712 int
713 rf_SetReconfiguredMode(raidPtr, row, col)
714 	RF_Raid_t *raidPtr;
715 	int     row;
716 	int     col;
717 {
718 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
719 		printf("Can't set reconfigured mode in dedicated-spare array\n");
720 		RF_PANIC();
721 	}
722 	RF_LOCK_MUTEX(raidPtr->mutex);
723 	raidPtr->numFailures++;
724 	raidPtr->Disks[row][col].status = rf_ds_dist_spared;
725 	raidPtr->status[row] = rf_rs_reconfigured;
726 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
727 	/* install spare table only if declustering + distributed sparing
728 	 * architecture. */
729 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
730 		rf_InstallSpareTable(raidPtr, row, col);
731 	RF_UNLOCK_MUTEX(raidPtr->mutex);
732 	return (0);
733 }
734 
735 extern int fail_row, fail_col, fail_time;
736 extern int delayed_recon;
737 
738 int
739 rf_FailDisk(
740     RF_Raid_t * raidPtr,
741     int frow,
742     int fcol,
743     int initRecon)
744 {
745 	printf("raid%d: Failing disk r%d c%d\n", raidPtr->raidid, frow, fcol);
746 	RF_LOCK_MUTEX(raidPtr->mutex);
747 	raidPtr->numFailures++;
748 	raidPtr->Disks[frow][fcol].status = rf_ds_failed;
749 	raidPtr->status[frow] = rf_rs_degraded;
750 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
751 	RF_UNLOCK_MUTEX(raidPtr->mutex);
752 	if (initRecon)
753 		rf_ReconstructFailedDisk(raidPtr, frow, fcol);
754 	return (0);
755 }
756 /* releases a thread that is waiting for the array to become quiesced.
757  * access_suspend_mutex should be locked upon calling this
758  */
759 void
760 rf_SignalQuiescenceLock(raidPtr, reconDesc)
761 	RF_Raid_t *raidPtr;
762 	RF_RaidReconDesc_t *reconDesc;
763 {
764 	if (rf_quiesceDebug) {
765 		printf("raid%d: Signalling quiescence lock\n",
766 		       raidPtr->raidid);
767 	}
768 	raidPtr->access_suspend_release = 1;
769 
770 	if (raidPtr->waiting_for_quiescence) {
771 		SIGNAL_QUIESCENT_COND(raidPtr);
772 	}
773 }
774 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
775 int
776 rf_SuspendNewRequestsAndWait(raidPtr)
777 	RF_Raid_t *raidPtr;
778 {
779 	if (rf_quiesceDebug)
780 		printf("Suspending new reqs\n");
781 
782 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
783 	raidPtr->accesses_suspended++;
784 	raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
785 
786 	if (raidPtr->waiting_for_quiescence) {
787 		raidPtr->access_suspend_release = 0;
788 		while (!raidPtr->access_suspend_release) {
789 			printf("Suspending: Waiting for Quiescence\n");
790 			WAIT_FOR_QUIESCENCE(raidPtr);
791 			raidPtr->waiting_for_quiescence = 0;
792 		}
793 	}
794 	printf("Quiescence reached..\n");
795 
796 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
797 	return (raidPtr->waiting_for_quiescence);
798 }
799 /* wake up everyone waiting for quiescence to be released */
800 void
801 rf_ResumeNewRequests(raidPtr)
802 	RF_Raid_t *raidPtr;
803 {
804 	RF_CallbackDesc_t *t, *cb;
805 
806 	if (rf_quiesceDebug)
807 		printf("Resuming new reqs\n");
808 
809 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
810 	raidPtr->accesses_suspended--;
811 	if (raidPtr->accesses_suspended == 0)
812 		cb = raidPtr->quiesce_wait_list;
813 	else
814 		cb = NULL;
815 	raidPtr->quiesce_wait_list = NULL;
816 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
817 
818 	while (cb) {
819 		t = cb;
820 		cb = cb->next;
821 		(t->callbackFunc) (t->callbackArg);
822 		rf_FreeCallbackDesc(t);
823 	}
824 }
825 /*****************************************************************************************
826  *
827  * debug routines
828  *
829  ****************************************************************************************/
830 
831 static void
832 set_debug_option(name, val)
833 	char   *name;
834 	long    val;
835 {
836 	RF_DebugName_t *p;
837 
838 	for (p = rf_debugNames; p->name; p++) {
839 		if (!strcmp(p->name, name)) {
840 			*(p->ptr) = val;
841 			printf("[Set debug variable %s to %ld]\n", name, val);
842 			return;
843 		}
844 	}
845 	RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
846 }
847 
848 
849 /* would like to use sscanf here, but apparently not available in kernel */
850 /*ARGSUSED*/
851 static void
852 rf_ConfigureDebug(cfgPtr)
853 	RF_Config_t *cfgPtr;
854 {
855 	char   *val_p, *name_p, *white_p;
856 	long    val;
857 	int     i;
858 
859 	rf_ResetDebugOptions();
860 	for (i = 0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
861 		name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
862 		white_p = rf_find_white(name_p);	/* skip to start of 2nd
863 							 * word */
864 		val_p = rf_find_non_white(white_p);
865 		if (*val_p == '0' && *(val_p + 1) == 'x')
866 			val = rf_htoi(val_p + 2);
867 		else
868 			val = rf_atoi(val_p);
869 		*white_p = '\0';
870 		set_debug_option(name_p, val);
871 	}
872 }
873 /* performance monitoring stuff */
874 
875 #define TIMEVAL_TO_US(t) (((long) t.tv_sec) * 1000000L + (long) t.tv_usec)
876 
877 #if !defined(_KERNEL) && !defined(SIMULATE)
878 
879 /*
880  * Throughput stats currently only used in user-level RAIDframe
881  */
882 
883 static int
884 rf_InitThroughputStats(
885     RF_ShutdownList_t ** listp,
886     RF_Raid_t * raidPtr,
887     RF_Config_t * cfgPtr)
888 {
889 	int     rc;
890 
891 	/* these used by user-level raidframe only */
892 	rc = rf_create_managed_mutex(listp, &raidPtr->throughputstats.mutex);
893 	if (rc) {
894 		RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
895 		    __LINE__, rc);
896 		return (rc);
897 	}
898 	raidPtr->throughputstats.sum_io_us = 0;
899 	raidPtr->throughputstats.num_ios = 0;
900 	raidPtr->throughputstats.num_out_ios = 0;
901 	return (0);
902 }
903 
904 void
905 rf_StartThroughputStats(RF_Raid_t * raidPtr)
906 {
907 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
908 	raidPtr->throughputstats.num_ios++;
909 	raidPtr->throughputstats.num_out_ios++;
910 	if (raidPtr->throughputstats.num_out_ios == 1)
911 		RF_GETTIME(raidPtr->throughputstats.start);
912 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
913 }
914 
915 static void
916 rf_StopThroughputStats(RF_Raid_t * raidPtr)
917 {
918 	struct timeval diff;
919 
920 	RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
921 	raidPtr->throughputstats.num_out_ios--;
922 	if (raidPtr->throughputstats.num_out_ios == 0) {
923 		RF_GETTIME(raidPtr->throughputstats.stop);
924 		RF_TIMEVAL_DIFF(&raidPtr->throughputstats.start, &raidPtr->throughputstats.stop, &diff);
925 		raidPtr->throughputstats.sum_io_us += TIMEVAL_TO_US(diff);
926 	}
927 	RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
928 }
929 
930 static void
931 rf_PrintThroughputStats(RF_Raid_t * raidPtr)
932 {
933 	RF_ASSERT(raidPtr->throughputstats.num_out_ios == 0);
934 	if (raidPtr->throughputstats.sum_io_us != 0) {
935 		printf("[Througphut: %8.2f IOs/second]\n", raidPtr->throughputstats.num_ios
936 		    / (raidPtr->throughputstats.sum_io_us / 1000000.0));
937 	}
938 }
939 #endif				/* !KERNEL && !SIMULATE */
940 
941 void
942 rf_StartUserStats(RF_Raid_t * raidPtr)
943 {
944 	RF_GETTIME(raidPtr->userstats.start);
945 	raidPtr->userstats.sum_io_us = 0;
946 	raidPtr->userstats.num_ios = 0;
947 	raidPtr->userstats.num_sect_moved = 0;
948 }
949 
950 void
951 rf_StopUserStats(RF_Raid_t * raidPtr)
952 {
953 	RF_GETTIME(raidPtr->userstats.stop);
954 }
955 
956 void
957 rf_UpdateUserStats(raidPtr, rt, numsect)
958 	RF_Raid_t *raidPtr;
959 	int     rt;		/* resp time in us */
960 	int     numsect;	/* number of sectors for this access */
961 {
962 	raidPtr->userstats.sum_io_us += rt;
963 	raidPtr->userstats.num_ios++;
964 	raidPtr->userstats.num_sect_moved += numsect;
965 }
966 
967 void
968 rf_PrintUserStats(RF_Raid_t * raidPtr)
969 {
970 	long    elapsed_us, mbs, mbs_frac;
971 	struct timeval diff;
972 
973 	RF_TIMEVAL_DIFF(&raidPtr->userstats.start, &raidPtr->userstats.stop, &diff);
974 	elapsed_us = TIMEVAL_TO_US(diff);
975 
976 	/* 2000 sectors per megabyte, 10000000 microseconds per second */
977 	if (elapsed_us)
978 		mbs = (raidPtr->userstats.num_sect_moved / 2000) / (elapsed_us / 1000000);
979 	else
980 		mbs = 0;
981 
982 	/* this computes only the first digit of the fractional mb/s moved */
983 	if (elapsed_us) {
984 		mbs_frac = ((raidPtr->userstats.num_sect_moved / 200) / (elapsed_us / 1000000))
985 		    - (mbs * 10);
986 	} else {
987 		mbs_frac = 0;
988 	}
989 
990 	printf("Number of I/Os:             %ld\n", raidPtr->userstats.num_ios);
991 	printf("Elapsed time (us):          %ld\n", elapsed_us);
992 	printf("User I/Os per second:       %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_ios, (elapsed_us / 1000000)));
993 	printf("Average user response time: %ld us\n", RF_DB0_CHECK(raidPtr->userstats.sum_io_us, raidPtr->userstats.num_ios));
994 	printf("Total sectors moved:        %ld\n", raidPtr->userstats.num_sect_moved);
995 	printf("Average access size (sect): %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_sect_moved, raidPtr->userstats.num_ios));
996 	printf("Achieved data rate:         %ld.%ld MB/sec\n", mbs, mbs_frac);
997 }
998 
999 
1000 void
1001 rf_print_panic_message(line,file)
1002 	int line;
1003 	char *file;
1004 {
1005 	sprintf(rf_panicbuf,"raidframe error at line %d file %s",
1006 		line, file);
1007 }
1008 
1009 void
1010 rf_print_assert_panic_message(line,file,condition)
1011 	int line;
1012 	char *file;
1013 	char *condition;
1014 {
1015 	sprintf(rf_panicbuf,
1016 		"raidframe error at line %d file %s (failed asserting %s)\n",
1017 		line, file, condition);
1018 }
1019