xref: /netbsd-src/sys/dev/raidframe/rf_driver.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: rf_driver.c,v 1.122 2009/11/17 18:54:26 jld Exp $	*/
2 /*-
3  * Copyright (c) 1999 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Greg Oster
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*
32  * Copyright (c) 1995 Carnegie-Mellon University.
33  * All rights reserved.
34  *
35  * Author: Mark Holland, Khalil Amiri, Claudson Bornstein, William V. Courtright II,
36  *         Robby Findler, Daniel Stodolsky, Rachad Youssef, Jim Zelenka
37  *
38  * Permission to use, copy, modify and distribute this software and
39  * its documentation is hereby granted, provided that both the copyright
40  * notice and this permission notice appear in all copies of the
41  * software, derivative works or modified versions, and any portions
42  * thereof, and that both notices appear in supporting documentation.
43  *
44  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
46  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47  *
48  * Carnegie Mellon requests users of this software to return to
49  *
50  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
51  *  School of Computer Science
52  *  Carnegie Mellon University
53  *  Pittsburgh PA 15213-3890
54  *
55  * any improvements or extensions that they make and grant Carnegie the
56  * rights to redistribute these changes.
57  */
58 
59 /******************************************************************************
60  *
61  * rf_driver.c -- main setup, teardown, and access routines for the RAID driver
62  *
63  * all routines are prefixed with rf_ (raidframe), to avoid conficts.
64  *
65  ******************************************************************************/
66 
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: rf_driver.c,v 1.122 2009/11/17 18:54:26 jld Exp $");
70 
71 #ifdef _KERNEL_OPT
72 #include "opt_raid_diagnostic.h"
73 #endif
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/ioctl.h>
78 #include <sys/fcntl.h>
79 #include <sys/vnode.h>
80 
81 
82 #include "rf_archs.h"
83 #include "rf_threadstuff.h"
84 
85 #include <sys/errno.h>
86 
87 #include "rf_raid.h"
88 #include "rf_dag.h"
89 #include "rf_aselect.h"
90 #include "rf_diskqueue.h"
91 #include "rf_parityscan.h"
92 #include "rf_alloclist.h"
93 #include "rf_dagutils.h"
94 #include "rf_utils.h"
95 #include "rf_etimer.h"
96 #include "rf_acctrace.h"
97 #include "rf_general.h"
98 #include "rf_desc.h"
99 #include "rf_states.h"
100 #include "rf_decluster.h"
101 #include "rf_map.h"
102 #include "rf_revent.h"
103 #include "rf_callback.h"
104 #include "rf_engine.h"
105 #include "rf_mcpair.h"
106 #include "rf_nwayxor.h"
107 #include "rf_copyback.h"
108 #include "rf_driver.h"
109 #include "rf_options.h"
110 #include "rf_shutdown.h"
111 #include "rf_kintf.h"
112 #include "rf_paritymap.h"
113 
114 #include <sys/buf.h>
115 
116 #ifndef RF_ACCESS_DEBUG
117 #define RF_ACCESS_DEBUG 0
118 #endif
119 
120 /* rad == RF_RaidAccessDesc_t */
121 RF_DECLARE_MUTEX(rf_rad_lock)
122 #define RF_MAX_FREE_RAD 128
123 #define RF_MIN_FREE_RAD  32
124 
125 /* debug variables */
126 char    rf_panicbuf[2048];	/* a buffer to hold an error msg when we panic */
127 
128 /* main configuration routines */
129 static int raidframe_booted = 0;
130 
131 static void rf_ConfigureDebug(RF_Config_t * cfgPtr);
132 static void set_debug_option(char *name, long val);
133 static void rf_UnconfigureArray(void);
134 static void rf_ShutdownRDFreeList(void *);
135 static int rf_ConfigureRDFreeList(RF_ShutdownList_t **);
136 
137 RF_DECLARE_MUTEX(rf_printf_mutex)	/* debug only:  avoids interleaved
138 					 * printfs by different stripes */
139 
140 #define SIGNAL_QUIESCENT_COND(_raid_)  wakeup(&((_raid_)->accesses_suspended))
141 #define WAIT_FOR_QUIESCENCE(_raid_) \
142 	ltsleep(&((_raid_)->accesses_suspended), PRIBIO, \
143 		"raidframe quiesce", 0, &((_raid_)->access_suspend_mutex))
144 
145 static int configureCount = 0;	/* number of active configurations */
146 static int isconfigged = 0;	/* is basic raidframe (non per-array)
147 				 * stuff configured */
148 RF_DECLARE_LKMGR_STATIC_MUTEX(configureMutex)	/* used to lock the configuration
149 					 * stuff */
150 static RF_ShutdownList_t *globalShutdown;	/* non array-specific
151 						 * stuff */
152 
153 static int rf_ConfigureRDFreeList(RF_ShutdownList_t ** listp);
154 static int rf_AllocEmergBuffers(RF_Raid_t *);
155 static void rf_FreeEmergBuffers(RF_Raid_t *);
156 
157 /* called at system boot time */
158 int
159 rf_BootRaidframe(void)
160 {
161 
162 	if (raidframe_booted)
163 		return (EBUSY);
164 	raidframe_booted = 1;
165 	mutex_init(&configureMutex, MUTEX_DEFAULT, IPL_NONE);
166  	configureCount = 0;
167 	isconfigged = 0;
168 	globalShutdown = NULL;
169 	return (0);
170 }
171 
172 /*
173  * Called whenever an array is shutdown
174  */
175 static void
176 rf_UnconfigureArray(void)
177 {
178 
179 	RF_LOCK_LKMGR_MUTEX(configureMutex);
180 	if (--configureCount == 0) {	/* if no active configurations, shut
181 					 * everything down */
182 		isconfigged = 0;
183 		rf_ShutdownList(&globalShutdown);
184 
185 		/*
186 	         * We must wait until now, because the AllocList module
187 	         * uses the DebugMem module.
188 	         */
189 #if RF_DEBUG_MEM
190 		if (rf_memDebug)
191 			rf_print_unfreed();
192 #endif
193 	}
194 	RF_UNLOCK_LKMGR_MUTEX(configureMutex);
195 }
196 
197 /*
198  * Called to shut down an array.
199  */
200 int
201 rf_Shutdown(RF_Raid_t *raidPtr)
202 {
203 
204 	if (!raidPtr->valid) {
205 		RF_ERRORMSG("Attempt to shut down unconfigured RAIDframe driver.  Aborting shutdown\n");
206 		return (EINVAL);
207 	}
208 	/*
209          * wait for outstanding IOs to land
210          * As described in rf_raid.h, we use the rad_freelist lock
211          * to protect the per-array info about outstanding descs
212          * since we need to do freelist locking anyway, and this
213          * cuts down on the amount of serialization we've got going
214          * on.
215          */
216 	RF_LOCK_MUTEX(rf_rad_lock);
217 	if (raidPtr->waitShutdown) {
218 		RF_UNLOCK_MUTEX(rf_rad_lock);
219 		return (EBUSY);
220 	}
221 	raidPtr->waitShutdown = 1;
222 	while (raidPtr->nAccOutstanding) {
223 		RF_WAIT_COND(raidPtr->outstandingCond, rf_rad_lock);
224 	}
225 	RF_UNLOCK_MUTEX(rf_rad_lock);
226 
227 	/* Wait for any parity re-writes to stop... */
228 	while (raidPtr->parity_rewrite_in_progress) {
229 		printf("raid%d: Waiting for parity re-write to exit...\n",
230 		       raidPtr->raidid);
231 		tsleep(&raidPtr->parity_rewrite_in_progress, PRIBIO,
232 		       "rfprwshutdown", 0);
233 	}
234 
235 	/* Wait for any reconstruction to stop... */
236 	while (raidPtr->reconInProgress) {
237 		printf("raid%d: Waiting for reconstruction to stop...\n",
238 		       raidPtr->raidid);
239 		tsleep(&raidPtr->waitForReconCond, PRIBIO,
240 		       "rfreshutdown",0);
241 	}
242 
243 	raidPtr->valid = 0;
244 
245 	if (raidPtr->parity_map != NULL)
246 		rf_paritymap_detach(raidPtr);
247 
248 	rf_update_component_labels(raidPtr, RF_FINAL_COMPONENT_UPDATE);
249 
250 	rf_UnconfigureVnodes(raidPtr);
251 
252 	rf_FreeEmergBuffers(raidPtr);
253 
254 	rf_ShutdownList(&raidPtr->shutdownList);
255 
256 	rf_UnconfigureArray();
257 
258 	return (0);
259 }
260 
261 
262 #define DO_INIT_CONFIGURE(f) { \
263 	rc = f (&globalShutdown); \
264 	if (rc) { \
265 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
266 		rf_ShutdownList(&globalShutdown); \
267 		configureCount--; \
268 		RF_UNLOCK_LKMGR_MUTEX(configureMutex); \
269 		return(rc); \
270 	} \
271 }
272 
273 #define DO_RAID_FAIL() { \
274 	rf_UnconfigureVnodes(raidPtr); \
275 	rf_FreeEmergBuffers(raidPtr); \
276 	rf_ShutdownList(&raidPtr->shutdownList); \
277 	rf_UnconfigureArray(); \
278 }
279 
280 #define DO_RAID_INIT_CONFIGURE(f) { \
281 	rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
282 	if (rc) { \
283 		RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
284 		DO_RAID_FAIL(); \
285 		return(rc); \
286 	} \
287 }
288 
289 #define DO_RAID_MUTEX(_m_) { \
290 	rf_mutex_init((_m_)); \
291 }
292 
293 int
294 rf_Configure(RF_Raid_t *raidPtr, RF_Config_t *cfgPtr, RF_AutoConfig_t *ac)
295 {
296 	RF_RowCol_t col;
297 	int rc;
298 
299 	RF_LOCK_LKMGR_MUTEX(configureMutex);
300 	configureCount++;
301 	if (isconfigged == 0) {
302 		rf_mutex_init(&rf_printf_mutex);
303 
304 		/* initialize globals */
305 
306 		DO_INIT_CONFIGURE(rf_ConfigureAllocList);
307 
308 		/*
309 	         * Yes, this does make debugging general to the whole
310 	         * system instead of being array specific. Bummer, drag.
311 		 */
312 		rf_ConfigureDebug(cfgPtr);
313 		DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
314 #if RF_ACC_TRACE > 0
315 		DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
316 #endif
317 		DO_INIT_CONFIGURE(rf_ConfigureMapModule);
318 		DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
319 		DO_INIT_CONFIGURE(rf_ConfigureCallback);
320 		DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
321 		DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
322 		DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
323 		DO_INIT_CONFIGURE(rf_ConfigureMCPair);
324 		DO_INIT_CONFIGURE(rf_ConfigureDAGs);
325 		DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
326 		DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
327 		DO_INIT_CONFIGURE(rf_ConfigureCopyback);
328 		DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
329 		DO_INIT_CONFIGURE(rf_ConfigurePSStatus);
330 		isconfigged = 1;
331 	}
332 	RF_UNLOCK_LKMGR_MUTEX(configureMutex);
333 
334 	DO_RAID_MUTEX(&raidPtr->mutex);
335 	/* set up the cleanup list.  Do this after ConfigureDebug so that
336 	 * value of memDebug will be set */
337 
338 	rf_MakeAllocList(raidPtr->cleanupList);
339 	if (raidPtr->cleanupList == NULL) {
340 		DO_RAID_FAIL();
341 		return (ENOMEM);
342 	}
343 	rf_ShutdownCreate(&raidPtr->shutdownList,
344 			  (void (*) (void *)) rf_FreeAllocList,
345 			  raidPtr->cleanupList);
346 
347 	raidPtr->numCol = cfgPtr->numCol;
348 	raidPtr->numSpare = cfgPtr->numSpare;
349 
350 	raidPtr->status = rf_rs_optimal;
351 	raidPtr->reconControl = NULL;
352 
353 	TAILQ_INIT(&(raidPtr->iodone));
354 	simple_lock_init(&(raidPtr->iodone_lock));
355 
356 	DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
357 	DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
358 
359 	raidPtr->outstandingCond = 0;
360 
361 	raidPtr->nAccOutstanding = 0;
362 	raidPtr->waitShutdown = 0;
363 
364 	DO_RAID_MUTEX(&raidPtr->access_suspend_mutex);
365 
366 	raidPtr->waitForReconCond = 0;
367 
368 	if (ac!=NULL) {
369 		/* We have an AutoConfig structure..  Don't do the
370 		   normal disk configuration... call the auto config
371 		   stuff */
372 		rf_AutoConfigureDisks(raidPtr, cfgPtr, ac);
373 	} else {
374 		DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
375 		DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
376 	}
377 	/* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
378 	 * no. is set */
379 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
380 
381 	DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
382 
383 	/* Initialize per-RAID PSS bits */
384 	rf_InitPSStatus(raidPtr);
385 
386 #if RF_INCLUDE_CHAINDECLUSTER > 0
387 	for (col = 0; col < raidPtr->numCol; col++) {
388 		/*
389 		 * XXX better distribution
390 		 */
391 		raidPtr->hist_diskreq[col] = 0;
392 	}
393 #endif
394 	raidPtr->numNewFailures = 0;
395 	raidPtr->copyback_in_progress = 0;
396 	raidPtr->parity_rewrite_in_progress = 0;
397 	raidPtr->adding_hot_spare = 0;
398 	raidPtr->recon_in_progress = 0;
399 	raidPtr->maxOutstanding = cfgPtr->maxOutstandingDiskReqs;
400 
401 	/* autoconfigure and root_partition will actually get filled in
402 	   after the config is done */
403 	raidPtr->autoconfigure = 0;
404 	raidPtr->root_partition = 0;
405 	raidPtr->last_unit = raidPtr->raidid;
406 	raidPtr->config_order = 0;
407 
408 	if (rf_keepAccTotals) {
409 		raidPtr->keep_acc_totals = 1;
410 	}
411 
412 	/* Allocate a bunch of buffers to be used in low-memory conditions */
413 	raidPtr->iobuf = NULL;
414 
415 	rc = rf_AllocEmergBuffers(raidPtr);
416 	if (rc) {
417 		printf("raid%d: Unable to allocate emergency buffers.\n",
418 		       raidPtr->raidid);
419 		DO_RAID_FAIL();
420 		return(rc);
421 	}
422 
423 	/* Set up parity map stuff, if applicable. */
424 #ifndef RF_NO_PARITY_MAP
425 	rf_paritymap_attach(raidPtr, cfgPtr->force);
426 #endif
427 
428 	raidPtr->valid = 1;
429 
430 	printf("raid%d: %s\n", raidPtr->raidid,
431 	       raidPtr->Layout.map->configName);
432 	printf("raid%d: Components:", raidPtr->raidid);
433 
434 	for (col = 0; col < raidPtr->numCol; col++) {
435 		printf(" %s", raidPtr->Disks[col].devname);
436 		if (RF_DEAD_DISK(raidPtr->Disks[col].status)) {
437 			printf("[**FAILED**]");
438 		}
439 	}
440 	printf("\n");
441 	printf("raid%d: Total Sectors: %" PRIu64 " (%" PRIu64 " MB)\n",
442 	       raidPtr->raidid,
443 	       raidPtr->totalSectors,
444 	       (raidPtr->totalSectors / 1024 *
445 				(1 << raidPtr->logBytesPerSector) / 1024));
446 
447 	return (0);
448 }
449 
450 
451 /*
452 
453   Routines to allocate and free the "emergency buffers" for a given
454   RAID set.  These emergency buffers will be used when the kernel runs
455   out of kernel memory.
456 
457  */
458 
459 static int
460 rf_AllocEmergBuffers(RF_Raid_t *raidPtr)
461 {
462 	void *tmpbuf;
463 	RF_VoidPointerListElem_t *vple;
464 	int i;
465 
466 	/* XXX next line needs tuning... */
467 	raidPtr->numEmergencyBuffers = 10 * raidPtr->numCol;
468 #if DEBUG
469 	printf("raid%d: allocating %d buffers of %d bytes.\n",
470 	       raidPtr->raidid,
471 	       raidPtr->numEmergencyBuffers,
472 	       (int)(raidPtr->Layout.sectorsPerStripeUnit <<
473 	       raidPtr->logBytesPerSector));
474 #endif
475 	for (i = 0; i < raidPtr->numEmergencyBuffers; i++) {
476 		tmpbuf = malloc( raidPtr->Layout.sectorsPerStripeUnit <<
477 				 raidPtr->logBytesPerSector,
478 				 M_RAIDFRAME, M_WAITOK);
479 		if (tmpbuf) {
480 			vple = rf_AllocVPListElem();
481 			vple->p= tmpbuf;
482 			vple->next = raidPtr->iobuf;
483 			raidPtr->iobuf = vple;
484 			raidPtr->iobuf_count++;
485 		} else {
486 			printf("raid%d: failed to allocate emergency buffer!\n",
487 			       raidPtr->raidid);
488 			return 1;
489 		}
490 	}
491 
492 	/* XXX next line needs tuning too... */
493 	raidPtr->numEmergencyStripeBuffers = 10;
494         for (i = 0; i < raidPtr->numEmergencyStripeBuffers; i++) {
495                 tmpbuf = malloc( raidPtr->numCol * (raidPtr->Layout.sectorsPerStripeUnit <<
496                                  raidPtr->logBytesPerSector),
497                                  M_RAIDFRAME, M_WAITOK);
498                 if (tmpbuf) {
499                         vple = rf_AllocVPListElem();
500                         vple->p= tmpbuf;
501                         vple->next = raidPtr->stripebuf;
502                         raidPtr->stripebuf = vple;
503                         raidPtr->stripebuf_count++;
504                 } else {
505                         printf("raid%d: failed to allocate emergency stripe buffer!\n",
506                                raidPtr->raidid);
507 			return 1;
508                 }
509         }
510 
511 	return (0);
512 }
513 
514 static void
515 rf_FreeEmergBuffers(RF_Raid_t *raidPtr)
516 {
517 	RF_VoidPointerListElem_t *tmp;
518 
519 	/* Free the emergency IO buffers */
520 	while (raidPtr->iobuf != NULL) {
521 		tmp = raidPtr->iobuf;
522 		raidPtr->iobuf = raidPtr->iobuf->next;
523 		free(tmp->p, M_RAIDFRAME);
524 		rf_FreeVPListElem(tmp);
525 	}
526 
527 	/* Free the emergency stripe buffers */
528 	while (raidPtr->stripebuf != NULL) {
529 		tmp = raidPtr->stripebuf;
530 		raidPtr->stripebuf = raidPtr->stripebuf->next;
531 		free(tmp->p, M_RAIDFRAME);
532 		rf_FreeVPListElem(tmp);
533 	}
534 }
535 
536 
537 static void
538 rf_ShutdownRDFreeList(void *ignored)
539 {
540 	pool_destroy(&rf_pools.rad);
541 }
542 
543 static int
544 rf_ConfigureRDFreeList(RF_ShutdownList_t **listp)
545 {
546 
547 	rf_pool_init(&rf_pools.rad, sizeof(RF_RaidAccessDesc_t),
548 		     "rf_rad_pl", RF_MIN_FREE_RAD, RF_MAX_FREE_RAD);
549 	rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
550 	simple_lock_init(&rf_rad_lock);
551 	return (0);
552 }
553 
554 RF_RaidAccessDesc_t *
555 rf_AllocRaidAccDesc(RF_Raid_t *raidPtr, RF_IoType_t type,
556 		    RF_RaidAddr_t raidAddress, RF_SectorCount_t numBlocks,
557 		    void *bufPtr, void *bp, RF_RaidAccessFlags_t flags,
558 		    const RF_AccessState_t *states)
559 {
560 	RF_RaidAccessDesc_t *desc;
561 
562 	desc = pool_get(&rf_pools.rad, PR_WAITOK);
563 
564 	RF_LOCK_MUTEX(rf_rad_lock);
565 	if (raidPtr->waitShutdown) {
566 		/*
567 	         * Actually, we're shutting the array down. Free the desc
568 	         * and return NULL.
569 	         */
570 
571 		RF_UNLOCK_MUTEX(rf_rad_lock);
572 		pool_put(&rf_pools.rad, desc);
573 		return (NULL);
574 	}
575 	raidPtr->nAccOutstanding++;
576 
577 	RF_UNLOCK_MUTEX(rf_rad_lock);
578 
579 	desc->raidPtr = (void *) raidPtr;
580 	desc->type = type;
581 	desc->raidAddress = raidAddress;
582 	desc->numBlocks = numBlocks;
583 	desc->bufPtr = bufPtr;
584 	desc->bp = bp;
585 	desc->flags = flags;
586 	desc->states = states;
587 	desc->state = 0;
588 	desc->dagList = NULL;
589 
590 	desc->status = 0;
591 	desc->numRetries = 0;
592 #if RF_ACC_TRACE > 0
593 	memset((char *) &desc->tracerec, 0, sizeof(RF_AccTraceEntry_t));
594 #endif
595 	desc->callbackFunc = NULL;
596 	desc->callbackArg = NULL;
597 	desc->next = NULL;
598 	desc->iobufs = NULL;
599 	desc->stripebufs = NULL;
600 
601 	return (desc);
602 }
603 
604 void
605 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t *desc)
606 {
607 	RF_Raid_t *raidPtr = desc->raidPtr;
608 	RF_DagList_t *dagList, *temp;
609 	RF_VoidPointerListElem_t *tmp;
610 
611 	RF_ASSERT(desc);
612 
613 	/* Cleanup the dagList(s) */
614 	dagList = desc->dagList;
615 	while(dagList != NULL) {
616 		temp = dagList;
617 		dagList = dagList->next;
618 		rf_FreeDAGList(temp);
619 	}
620 
621 	while (desc->iobufs) {
622 		tmp = desc->iobufs;
623 		desc->iobufs = desc->iobufs->next;
624 		rf_FreeIOBuffer(raidPtr, tmp);
625 	}
626 
627 	while (desc->stripebufs) {
628 		tmp = desc->stripebufs;
629 		desc->stripebufs = desc->stripebufs->next;
630 		rf_FreeStripeBuffer(raidPtr, tmp);
631 	}
632 
633 	pool_put(&rf_pools.rad, desc);
634 	RF_LOCK_MUTEX(rf_rad_lock);
635 	raidPtr->nAccOutstanding--;
636 	if (raidPtr->waitShutdown) {
637 		RF_SIGNAL_COND(raidPtr->outstandingCond);
638 	}
639 	RF_UNLOCK_MUTEX(rf_rad_lock);
640 }
641 /*********************************************************************
642  * Main routine for performing an access.
643  * Accesses are retried until a DAG can not be selected.  This occurs
644  * when either the DAG library is incomplete or there are too many
645  * failures in a parity group.
646  *
647  * type should be read or write async_flag should be RF_TRUE or
648  * RF_FALSE bp_in is a buf pointer.  void *to facilitate ignoring it
649  * outside the kernel
650  ********************************************************************/
651 int
652 rf_DoAccess(RF_Raid_t * raidPtr, RF_IoType_t type, int async_flag,
653 	    RF_RaidAddr_t raidAddress, RF_SectorCount_t numBlocks,
654 	    void *bufPtr, struct buf *bp, RF_RaidAccessFlags_t flags)
655 {
656 	RF_RaidAccessDesc_t *desc;
657 	void *lbufPtr = bufPtr;
658 
659 	raidAddress += rf_raidSectorOffset;
660 
661 #if RF_ACCESS_DEBUG
662 	if (rf_accessDebug) {
663 
664 		printf("logBytes is: %d %d %d\n", raidPtr->raidid,
665 		    raidPtr->logBytesPerSector,
666 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks));
667 		printf("raid%d: %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", raidPtr->raidid,
668 		    (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
669 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
670 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
671 		    (int) numBlocks,
672 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks),
673 		    (long) bufPtr);
674 	}
675 #endif
676 
677 	desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
678 	    numBlocks, lbufPtr, bp, flags, raidPtr->Layout.map->states);
679 
680 	if (desc == NULL) {
681 		return (ENOMEM);
682 	}
683 #if RF_ACC_TRACE > 0
684 	RF_ETIMER_START(desc->tracerec.tot_timer);
685 #endif
686 	desc->async_flag = async_flag;
687 
688 	if (raidPtr->parity_map != NULL &&
689 	    type == RF_IO_TYPE_WRITE)
690 		rf_paritymap_begin(raidPtr->parity_map, raidAddress,
691 		    numBlocks);
692 
693 	rf_ContinueRaidAccess(desc);
694 
695 	return (0);
696 }
697 #if 0
698 /* force the array into reconfigured mode without doing reconstruction */
699 int
700 rf_SetReconfiguredMode(RF_Raid_t *raidPtr, int col)
701 {
702 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
703 		printf("Can't set reconfigured mode in dedicated-spare array\n");
704 		RF_PANIC();
705 	}
706 	RF_LOCK_MUTEX(raidPtr->mutex);
707 	raidPtr->numFailures++;
708 	raidPtr->Disks[col].status = rf_ds_dist_spared;
709 	raidPtr->status = rf_rs_reconfigured;
710 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
711 	/* install spare table only if declustering + distributed sparing
712 	 * architecture. */
713 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
714 		rf_InstallSpareTable(raidPtr, col);
715 	RF_UNLOCK_MUTEX(raidPtr->mutex);
716 	return (0);
717 }
718 #endif
719 
720 int
721 rf_FailDisk(RF_Raid_t *raidPtr, int fcol, int initRecon)
722 {
723 
724 	/* need to suspend IO's here -- if there are DAGs in flight
725 	   and we pull the rug out from under ci_vp, Bad Things
726 	   can happen.  */
727 
728 	rf_SuspendNewRequestsAndWait(raidPtr);
729 
730 	RF_LOCK_MUTEX(raidPtr->mutex);
731 	if (raidPtr->Disks[fcol].status != rf_ds_failed) {
732 		/* must be failing something that is valid, or else it's
733 		   already marked as failed (in which case we don't
734 		   want to mark it failed again!) */
735 		raidPtr->numFailures++;
736 		raidPtr->Disks[fcol].status = rf_ds_failed;
737 		raidPtr->status = rf_rs_degraded;
738 	}
739 	RF_UNLOCK_MUTEX(raidPtr->mutex);
740 
741 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
742 
743 	/* Close the component, so that it's not "locked" if someone
744 	   else want's to use it! */
745 
746 	rf_close_component(raidPtr, raidPtr->raid_cinfo[fcol].ci_vp,
747 			   raidPtr->Disks[fcol].auto_configured);
748 
749 	RF_LOCK_MUTEX(raidPtr->mutex);
750 	raidPtr->raid_cinfo[fcol].ci_vp = NULL;
751 
752 	/* Need to mark the component as not being auto_configured
753 	   (in case it was previously). */
754 
755 	raidPtr->Disks[fcol].auto_configured = 0;
756 	RF_UNLOCK_MUTEX(raidPtr->mutex);
757 	/* now we can allow IO to continue -- we'll be suspending it
758 	   again in rf_ReconstructFailedDisk() if we have to.. */
759 
760 	rf_ResumeNewRequests(raidPtr);
761 
762 	if (initRecon)
763 		rf_ReconstructFailedDisk(raidPtr, fcol);
764 	return (0);
765 }
766 /* releases a thread that is waiting for the array to become quiesced.
767  * access_suspend_mutex should be locked upon calling this
768  */
769 void
770 rf_SignalQuiescenceLock(RF_Raid_t *raidPtr)
771 {
772 #if RF_DEBUG_QUIESCE
773 	if (rf_quiesceDebug) {
774 		printf("raid%d: Signalling quiescence lock\n",
775 		       raidPtr->raidid);
776 	}
777 #endif
778 	raidPtr->access_suspend_release = 1;
779 
780 	if (raidPtr->waiting_for_quiescence) {
781 		SIGNAL_QUIESCENT_COND(raidPtr);
782 	}
783 }
784 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
785 int
786 rf_SuspendNewRequestsAndWait(RF_Raid_t *raidPtr)
787 {
788 #if RF_DEBUG_QUIESCE
789 	if (rf_quiesceDebug)
790 		printf("raid%d: Suspending new reqs\n", raidPtr->raidid);
791 #endif
792 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
793 	raidPtr->accesses_suspended++;
794 	raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
795 
796 	if (raidPtr->waiting_for_quiescence) {
797 		raidPtr->access_suspend_release = 0;
798 		while (!raidPtr->access_suspend_release) {
799 #if RF_DEBUG_QUIESCE
800 			printf("raid%d: Suspending: Waiting for Quiescence\n",
801 			       raidPtr->raidid);
802 #endif
803 			WAIT_FOR_QUIESCENCE(raidPtr);
804 			raidPtr->waiting_for_quiescence = 0;
805 		}
806 	}
807 #if RF_DEBUG_QUIESCE
808 	printf("raid%d: Quiescence reached..\n", raidPtr->raidid);
809 #endif
810 
811 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
812 	return (raidPtr->waiting_for_quiescence);
813 }
814 /* wake up everyone waiting for quiescence to be released */
815 void
816 rf_ResumeNewRequests(RF_Raid_t *raidPtr)
817 {
818 	RF_CallbackDesc_t *t, *cb;
819 
820 #if RF_DEBUG_QUIESCE
821 	if (rf_quiesceDebug)
822 		printf("raid%d: Resuming new requests\n", raidPtr->raidid);
823 #endif
824 
825 	RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
826 	raidPtr->accesses_suspended--;
827 	if (raidPtr->accesses_suspended == 0)
828 		cb = raidPtr->quiesce_wait_list;
829 	else
830 		cb = NULL;
831 	raidPtr->quiesce_wait_list = NULL;
832 	RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
833 
834 	while (cb) {
835 		t = cb;
836 		cb = cb->next;
837 		(t->callbackFunc) (t->callbackArg);
838 		rf_FreeCallbackDesc(t);
839 	}
840 }
841 /*****************************************************************************************
842  *
843  * debug routines
844  *
845  ****************************************************************************************/
846 
847 static void
848 set_debug_option(char *name, long val)
849 {
850 	RF_DebugName_t *p;
851 
852 	for (p = rf_debugNames; p->name; p++) {
853 		if (!strcmp(p->name, name)) {
854 			*(p->ptr) = val;
855 			printf("[Set debug variable %s to %ld]\n", name, val);
856 			return;
857 		}
858 	}
859 	RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
860 }
861 
862 
863 /* would like to use sscanf here, but apparently not available in kernel */
864 /*ARGSUSED*/
865 static void
866 rf_ConfigureDebug(RF_Config_t *cfgPtr)
867 {
868 	char   *val_p, *name_p, *white_p;
869 	long    val;
870 	int     i;
871 
872 	rf_ResetDebugOptions();
873 	for (i = 0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
874 		name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
875 		white_p = rf_find_white(name_p);	/* skip to start of 2nd
876 							 * word */
877 		val_p = rf_find_non_white(white_p);
878 		if (*val_p == '0' && *(val_p + 1) == 'x')
879 			val = rf_htoi(val_p + 2);
880 		else
881 			val = rf_atoi(val_p);
882 		*white_p = '\0';
883 		set_debug_option(name_p, val);
884 	}
885 }
886 
887 void
888 rf_print_panic_message(int line, const char *file)
889 {
890 	snprintf(rf_panicbuf, sizeof(rf_panicbuf),
891 	    "raidframe error at line %d file %s", line, file);
892 }
893 
894 #ifdef RAID_DIAGNOSTIC
895 void
896 rf_print_assert_panic_message(int line,	const char *file, const char *condition)
897 {
898 	snprintf(rf_panicbuf, sizeof(rf_panicbuf),
899 		"raidframe error at line %d file %s (failed asserting %s)\n",
900 		line, file, condition);
901 }
902 #endif
903 
904 void
905 rf_print_unable_to_init_mutex(const char *file, int line, int rc)
906 {
907 	RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n",
908 		     file, line, rc);
909 }
910 
911 void
912 rf_print_unable_to_add_shutdown(const char *file, int line, int rc)
913 {
914 	RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
915 		     file, line, rc);
916 }
917