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