xref: /netbsd/sys/dev/raidframe/rf_driver.c (revision 99225d72)
1 /*	$NetBSD: rf_driver.c,v 1.140 2022/08/10 01:16:38 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.140 2022/08/10 01:16:38 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 **, RF_Raid_t *, RF_Config_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, RF_Raid_t *raidPtr, RF_Config_t *cfgPtr);
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
rf_BootRaidframe(bool boot)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
rf_UnconfigureArray(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
rf_Shutdown(RF_Raid_t * raidPtr)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
rf_Configure(RF_Raid_t * raidPtr,RF_Config_t * cfgPtr,RF_AutoConfig_t * ac)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 		DO_INIT_CONFIGURE(rf_ConfigureAllocList);
313 
314 		/*
315 	         * Yes, this does make debugging general to the whole
316 	         * system instead of being array specific. Bummer, drag.
317 		 */
318 		rf_ConfigureDebug(cfgPtr);
319 		DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
320 #if RF_ACC_TRACE > 0
321 		DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
322 #endif
323 		DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
324 		DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
325 		DO_INIT_CONFIGURE(rf_ConfigureCopyback);
326 		isconfigged = 1;
327 	}
328 	rf_unlock_mutex2(configureMutex);
329 
330 	rf_alloc_mutex_cond(raidPtr);
331 
332 	/* set up the cleanup list.  Do this after ConfigureDebug so that
333 	 * value of memDebug will be set */
334 
335 	rf_MakeAllocList(raidPtr->cleanupList);
336 	if (raidPtr->cleanupList == NULL) {
337 		DO_RAID_FAIL();
338 		return (ENOMEM);
339 	}
340 	rf_ShutdownCreate(&raidPtr->shutdownList,
341 			  (void (*) (void *)) rf_FreeAllocList,
342 			  raidPtr->cleanupList);
343 
344 	KASSERT(cfgPtr->numCol < RF_MAXCOL);
345 	KASSERT(cfgPtr->numCol >= 0);
346 	KASSERT(cfgPtr->numSpare < RF_MAXSPARE);
347 	KASSERT(cfgPtr->numSpare >= 0);
348 
349 	raidPtr->numCol = cfgPtr->numCol;
350 	raidPtr->numSpare = cfgPtr->numSpare;
351 
352 	raidPtr->status = rf_rs_optimal;
353 	raidPtr->reconControl = NULL;
354 
355 	DO_RAID_INIT_CONFIGURE(rf_ConfigureMapModule);
356 	DO_RAID_INIT_CONFIGURE(rf_ConfigureReconEvent);
357 	DO_RAID_INIT_CONFIGURE(rf_ConfigureCallback);
358 	DO_RAID_INIT_CONFIGURE(rf_ConfigureRDFreeList);
359 	DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
360 	DO_RAID_INIT_CONFIGURE(rf_ConfigureMCPair);
361 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDAGs);
362 	DO_RAID_INIT_CONFIGURE(rf_ConfigureReconstruction);
363 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
364 	DO_RAID_INIT_CONFIGURE(rf_ConfigurePSStatus);
365 
366 	DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
367 	DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
368 
369 	raidPtr->nAccOutstanding = 0;
370 	raidPtr->waitShutdown = 0;
371 
372 	if (ac!=NULL) {
373 		/* We have an AutoConfig structure..  Don't do the
374 		   normal disk configuration... call the auto config
375 		   stuff */
376 		rf_AutoConfigureDisks(raidPtr, cfgPtr, ac);
377 	} else {
378 		DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
379 		DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
380 	}
381 	/* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
382 	 * no. is set */
383 	DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
384 
385 	DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
386 
387 
388 
389 
390 	/* Initialize per-RAID PSS bits */
391 	rf_InitPSStatus(raidPtr);
392 
393 #if RF_INCLUDE_CHAINDECLUSTER > 0
394 	for (col = 0; col < raidPtr->numCol; col++) {
395 		/*
396 		 * XXX better distribution
397 		 */
398 		raidPtr->hist_diskreq[col] = 0;
399 	}
400 #endif
401 	raidPtr->numNewFailures = 0;
402 	raidPtr->copyback_in_progress = 0;
403 	raidPtr->parity_rewrite_in_progress = 0;
404 	raidPtr->adding_hot_spare = 0;
405 	raidPtr->recon_in_progress = 0;
406 
407 	raidPtr->maxOutstanding = cfgPtr->maxOutstandingDiskReqs;
408 
409 	/* autoconfigure and root_partition will actually get filled in
410 	   after the config is done */
411 	raidPtr->autoconfigure = 0;
412 	raidPtr->root_partition = 0;
413 	raidPtr->last_unit = raidPtr->raidid;
414 	raidPtr->config_order = 0;
415 
416 	if (rf_keepAccTotals) {
417 		raidPtr->keep_acc_totals = 1;
418 	}
419 
420 	/* Allocate a bunch of buffers to be used in low-memory conditions */
421 	raidPtr->iobuf = NULL;
422 
423 	rc = rf_AllocEmergBuffers(raidPtr);
424 	if (rc) {
425 		printf("raid%d: Unable to allocate emergency buffers.\n",
426 		       raidPtr->raidid);
427 		DO_RAID_FAIL();
428 		return(rc);
429 	}
430 
431 	/* Set up parity map stuff, if applicable. */
432 #ifndef RF_NO_PARITY_MAP
433 	rf_paritymap_attach(raidPtr, cfgPtr->force);
434 #endif
435 
436 	raidPtr->valid = 1;
437 
438 	printf("raid%d: %s\n", raidPtr->raidid,
439 	       raidPtr->Layout.map->configName);
440 	printf("raid%d: Components:", raidPtr->raidid);
441 
442 	for (col = 0; col < raidPtr->numCol; col++) {
443 		RF_ComponentLabel_t *clabel;
444 		bool compswapped;
445 
446 		printf(" %s", raidPtr->Disks[col].devname);
447 		if (RF_DEAD_DISK(raidPtr->Disks[col].status)) {
448 			printf("[**FAILED**]");
449 		}
450 		clabel = raidget_component_label(raidPtr, col);
451 		compswapped = clabel->version ==
452 			      bswap32(RF_COMPONENT_LABEL_VERSION);
453 		if (first)
454 			swapped = compswapped;
455 		else if (swapped != compswapped)
456 			printf("raid%d: Component %d has different endian "
457 			       "than first component.", raidPtr->raidid, col);
458 	}
459 	printf("\n");
460 	printf("raid%d: Total Sectors: %" PRIu64 " (%" PRIu64 " MB)\n",
461 	       raidPtr->raidid,
462 	       raidPtr->totalSectors,
463 	       (raidPtr->totalSectors / 1024 *
464 				(1 << raidPtr->logBytesPerSector) / 1024));
465 	if (swapped)
466 		printf("raid%d: Using swapped-endian component labels.\n",
467 		    raidPtr->raidid);
468 
469 	return (0);
470 }
471 
472 
473 /*
474 
475   Routines to allocate and free the "emergency buffers" for a given
476   RAID set.  These emergency buffers will be used when the kernel runs
477   out of kernel memory.
478 
479  */
480 
481 static int
rf_AllocEmergBuffers(RF_Raid_t * raidPtr)482 rf_AllocEmergBuffers(RF_Raid_t *raidPtr)
483 {
484 	void *tmpbuf;
485 	RF_VoidPointerListElem_t *vple;
486 	int i;
487 
488 	/* XXX next line needs tuning... */
489 	raidPtr->numEmergencyBuffers = 10 * raidPtr->numCol;
490 #if DEBUG
491 	printf("raid%d: allocating %d buffers of %d bytes.\n",
492 	       raidPtr->raidid,
493 	       raidPtr->numEmergencyBuffers,
494 	       (int)(raidPtr->Layout.sectorsPerStripeUnit <<
495 	       raidPtr->logBytesPerSector));
496 #endif
497 	for (i = 0; i < raidPtr->numEmergencyBuffers; i++) {
498 		tmpbuf = malloc( raidPtr->Layout.sectorsPerStripeUnit <<
499 				 raidPtr->logBytesPerSector,
500 				 M_RAIDFRAME, M_WAITOK);
501 		if (tmpbuf) {
502 			vple = rf_AllocVPListElem(raidPtr);
503 			vple->p= tmpbuf;
504 			vple->next = raidPtr->iobuf;
505 			raidPtr->iobuf = vple;
506 			raidPtr->iobuf_count++;
507 		} else {
508 			printf("raid%d: failed to allocate emergency buffer!\n",
509 			       raidPtr->raidid);
510 			return 1;
511 		}
512 	}
513 
514 	/* XXX next line needs tuning too... */
515 	raidPtr->numEmergencyStripeBuffers = 10;
516         for (i = 0; i < raidPtr->numEmergencyStripeBuffers; i++) {
517                 tmpbuf = malloc( raidPtr->numCol * (raidPtr->Layout.sectorsPerStripeUnit <<
518                                  raidPtr->logBytesPerSector),
519                                  M_RAIDFRAME, M_WAITOK);
520                 if (tmpbuf) {
521                         vple = rf_AllocVPListElem(raidPtr);
522                         vple->p= tmpbuf;
523                         vple->next = raidPtr->stripebuf;
524                         raidPtr->stripebuf = vple;
525                         raidPtr->stripebuf_count++;
526                 } else {
527                         printf("raid%d: failed to allocate emergency stripe buffer!\n",
528                                raidPtr->raidid);
529 			return 1;
530                 }
531         }
532 
533 	return (0);
534 }
535 
536 static void
rf_FreeEmergBuffers(RF_Raid_t * raidPtr)537 rf_FreeEmergBuffers(RF_Raid_t *raidPtr)
538 {
539 	RF_VoidPointerListElem_t *tmp;
540 
541 	/* Free the emergency IO buffers */
542 	while (raidPtr->iobuf != NULL) {
543 		tmp = raidPtr->iobuf;
544 		raidPtr->iobuf = raidPtr->iobuf->next;
545 		free(tmp->p, M_RAIDFRAME);
546 		rf_FreeVPListElem(raidPtr,tmp);
547 	}
548 
549 	/* Free the emergency stripe buffers */
550 	while (raidPtr->stripebuf != NULL) {
551 		tmp = raidPtr->stripebuf;
552 		raidPtr->stripebuf = raidPtr->stripebuf->next;
553 		free(tmp->p, M_RAIDFRAME);
554 		rf_FreeVPListElem(raidPtr, tmp);
555 	}
556 }
557 
558 
559 static void
rf_ShutdownRDFreeList(void * arg)560 rf_ShutdownRDFreeList(void *arg)
561 {
562 	RF_Raid_t *raidPtr;
563 
564 	raidPtr = (RF_Raid_t *) arg;
565 
566 	pool_destroy(&raidPtr->pools.rad);
567 }
568 
569 static int
rf_ConfigureRDFreeList(RF_ShutdownList_t ** listp,RF_Raid_t * raidPtr,RF_Config_t * cfgPtr)570 rf_ConfigureRDFreeList(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
571 		       RF_Config_t *cfgPtr)
572 {
573 
574 	rf_pool_init(raidPtr, raidPtr->poolNames.rad, &raidPtr->pools.rad, sizeof(RF_RaidAccessDesc_t),
575 		     "rad", RF_MIN_FREE_RAD, RF_MAX_FREE_RAD);
576 	rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, raidPtr);
577 	return (0);
578 }
579 
580 RF_RaidAccessDesc_t *
rf_AllocRaidAccDesc(RF_Raid_t * raidPtr,RF_IoType_t type,RF_RaidAddr_t raidAddress,RF_SectorCount_t numBlocks,void * bufPtr,void * bp,RF_RaidAccessFlags_t flags,const RF_AccessState_t * states)581 rf_AllocRaidAccDesc(RF_Raid_t *raidPtr, RF_IoType_t type,
582 		    RF_RaidAddr_t raidAddress, RF_SectorCount_t numBlocks,
583 		    void *bufPtr, void *bp, RF_RaidAccessFlags_t flags,
584 		    const RF_AccessState_t *states)
585 {
586 	RF_RaidAccessDesc_t *desc;
587 
588 	desc = pool_get(&raidPtr->pools.rad, PR_WAITOK);
589 
590 	rf_lock_mutex2(raidPtr->rad_lock);
591 	if (raidPtr->waitShutdown) {
592 		/*
593 	         * Actually, we're shutting the array down. Free the desc
594 	         * and return NULL.
595 	         */
596 
597 		rf_unlock_mutex2(raidPtr->rad_lock);
598 		pool_put(&raidPtr->pools.rad, desc);
599 		return (NULL);
600 	}
601 	raidPtr->nAccOutstanding++;
602 
603 	rf_unlock_mutex2(raidPtr->rad_lock);
604 
605 	desc->raidPtr = (void *) raidPtr;
606 	desc->type = type;
607 	desc->raidAddress = raidAddress;
608 	desc->numBlocks = numBlocks;
609 	desc->bufPtr = bufPtr;
610 	desc->bp = bp;
611 	desc->flags = flags;
612 	desc->states = states;
613 	desc->state = 0;
614 	desc->dagList = NULL;
615 
616 	desc->status = 0;
617 	desc->numRetries = 0;
618 #if RF_ACC_TRACE > 0
619 	memset(&desc->tracerec, 0, sizeof(desc->tracerec));
620 #endif
621 	desc->callbackFunc = NULL;
622 	desc->callbackArg = NULL;
623 	desc->next = NULL;
624 	desc->iobufs = NULL;
625 	desc->stripebufs = NULL;
626 
627 	return (desc);
628 }
629 
630 void
rf_FreeRaidAccDesc(RF_RaidAccessDesc_t * desc)631 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t *desc)
632 {
633 	RF_Raid_t *raidPtr = desc->raidPtr;
634 	RF_DagList_t *dagList, *temp;
635 	RF_VoidPointerListElem_t *tmp;
636 
637 	RF_ASSERT(desc);
638 
639 	/* Cleanup the dagList(s) */
640 	dagList = desc->dagList;
641 	while(dagList != NULL) {
642 		temp = dagList;
643 		dagList = dagList->next;
644 		rf_FreeDAGList(raidPtr, temp);
645 	}
646 
647 	while (desc->iobufs) {
648 		tmp = desc->iobufs;
649 		desc->iobufs = desc->iobufs->next;
650 		rf_FreeIOBuffer(raidPtr, tmp);
651 	}
652 
653 	while (desc->stripebufs) {
654 		tmp = desc->stripebufs;
655 		desc->stripebufs = desc->stripebufs->next;
656 		rf_FreeStripeBuffer(raidPtr, tmp);
657 	}
658 
659 	pool_put(&raidPtr->pools.rad, desc);
660 	rf_lock_mutex2(raidPtr->rad_lock);
661 	raidPtr->nAccOutstanding--;
662 	if (raidPtr->waitShutdown) {
663 		rf_signal_cond2(raidPtr->outstandingCond);
664 	}
665 	rf_unlock_mutex2(raidPtr->rad_lock);
666 }
667 /*********************************************************************
668  * Main routine for performing an access.
669  * Accesses are retried until a DAG can not be selected.  This occurs
670  * when either the DAG library is incomplete or there are too many
671  * failures in a parity group.
672  *
673  * type should be read or write.  bp_in is a buf pointer.  void *to
674  * facilitate ignoring it outside the kernel
675  ********************************************************************/
676 int
rf_DoAccess(RF_Raid_t * raidPtr,RF_IoType_t type,RF_RaidAddr_t raidAddress,RF_SectorCount_t numBlocks,void * bufPtr,struct buf * bp,RF_RaidAccessFlags_t flags)677 rf_DoAccess(RF_Raid_t * raidPtr, RF_IoType_t type, RF_RaidAddr_t raidAddress, RF_SectorCount_t numBlocks,
678 	    void *bufPtr, struct buf *bp, RF_RaidAccessFlags_t flags)
679 {
680 	RF_RaidAccessDesc_t *desc;
681 	void *lbufPtr = bufPtr;
682 
683 	raidAddress += rf_raidSectorOffset;
684 
685 #if RF_ACCESS_DEBUG
686 	if (rf_accessDebug) {
687 
688 		printf("logBytes is: %d %d %d\n", raidPtr->raidid,
689 		    raidPtr->logBytesPerSector,
690 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks));
691 		printf("raid%d: %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", raidPtr->raidid,
692 		    (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
693 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
694 		    (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
695 		    (int) numBlocks,
696 		    (int) rf_RaidAddressToByte(raidPtr, numBlocks),
697 		    (long) bufPtr);
698 	}
699 #endif
700 
701 	desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
702 	    numBlocks, lbufPtr, bp, flags, raidPtr->Layout.map->states);
703 
704 	if (desc == NULL) {
705 		return (ENOMEM);
706 	}
707 #if RF_ACC_TRACE > 0
708 	RF_ETIMER_START(desc->tracerec.tot_timer);
709 #endif
710 
711 	if (raidPtr->parity_map != NULL &&
712 	    type == RF_IO_TYPE_WRITE)
713 		rf_paritymap_begin(raidPtr->parity_map, raidAddress,
714 		    numBlocks);
715 
716 	rf_ContinueRaidAccess(desc);
717 
718 	return (0);
719 }
720 #if 0
721 /* force the array into reconfigured mode without doing reconstruction */
722 int
723 rf_SetReconfiguredMode(RF_Raid_t *raidPtr, int col)
724 {
725 	if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
726 		printf("Can't set reconfigured mode in dedicated-spare array\n");
727 		RF_PANIC();
728 	}
729 	rf_lock_mutex2(raidPtr->mutex);
730 	raidPtr->numFailures++;
731 	raidPtr->Disks[col].status = rf_ds_dist_spared;
732 	raidPtr->status = rf_rs_reconfigured;
733 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
734 	/* install spare table only if declustering + distributed sparing
735 	 * architecture. */
736 	if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
737 		rf_InstallSpareTable(raidPtr, col);
738 	rf_unlock_mutex2(raidPtr->mutex);
739 	return (0);
740 }
741 #endif
742 
743 int
rf_FailDisk(RF_Raid_t * raidPtr,int fcol,int initRecon)744 rf_FailDisk(RF_Raid_t *raidPtr, int fcol, int initRecon)
745 {
746 
747 	/* need to suspend IO's here -- if there are DAGs in flight
748 	   and we pull the rug out from under ci_vp, Bad Things
749 	   can happen.  */
750 
751 	rf_SuspendNewRequestsAndWait(raidPtr);
752 
753 	rf_lock_mutex2(raidPtr->mutex);
754 	if (raidPtr->Disks[fcol].status != rf_ds_failed) {
755 		/* must be failing something that is valid, or else it's
756 		   already marked as failed (in which case we don't
757 		   want to mark it failed again!) */
758 		raidPtr->numFailures++;
759 		raidPtr->Disks[fcol].status = rf_ds_failed;
760 		raidPtr->status = rf_rs_degraded;
761 	}
762 	rf_unlock_mutex2(raidPtr->mutex);
763 
764 	rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
765 
766 	/* Close the component, so that it's not "locked" if someone
767 	   else want's to use it! */
768 
769 	rf_close_component(raidPtr, raidPtr->raid_cinfo[fcol].ci_vp,
770 			   raidPtr->Disks[fcol].auto_configured);
771 
772 	rf_lock_mutex2(raidPtr->mutex);
773 	raidPtr->raid_cinfo[fcol].ci_vp = NULL;
774 
775 	/* Need to mark the component as not being auto_configured
776 	   (in case it was previously). */
777 
778 	raidPtr->Disks[fcol].auto_configured = 0;
779 	rf_unlock_mutex2(raidPtr->mutex);
780 	/* now we can allow IO to continue -- we'll be suspending it
781 	   again in rf_ReconstructFailedDisk() if we have to.. */
782 
783 	rf_ResumeNewRequests(raidPtr);
784 
785 	if (initRecon)
786 		rf_ReconstructFailedDisk(raidPtr, fcol);
787 	return (0);
788 }
789 /* releases a thread that is waiting for the array to become quiesced.
790  * access_suspend_mutex should be locked upon calling this
791  */
792 void
rf_SignalQuiescenceLock(RF_Raid_t * raidPtr)793 rf_SignalQuiescenceLock(RF_Raid_t *raidPtr)
794 {
795 #if RF_DEBUG_QUIESCE
796 	if (rf_quiesceDebug) {
797 		printf("raid%d: Signalling quiescence lock\n",
798 		       raidPtr->raidid);
799 	}
800 #endif
801 	raidPtr->access_suspend_release = 1;
802 
803 	if (raidPtr->waiting_for_quiescence) {
804 		SIGNAL_QUIESCENT_COND(raidPtr);
805 	}
806 }
807 /* suspends all new requests to the array.  No effect on accesses that are in flight.  */
808 int
rf_SuspendNewRequestsAndWait(RF_Raid_t * raidPtr)809 rf_SuspendNewRequestsAndWait(RF_Raid_t *raidPtr)
810 {
811 #if RF_DEBUG_QUIESCE
812 	if (rf_quiesceDebug)
813 		printf("raid%d: Suspending new reqs\n", raidPtr->raidid);
814 #endif
815 	rf_lock_mutex2(raidPtr->access_suspend_mutex);
816 	raidPtr->accesses_suspended++;
817 	raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
818 
819 	if (raidPtr->waiting_for_quiescence) {
820 		raidPtr->access_suspend_release = 0;
821 		while (!raidPtr->access_suspend_release) {
822 #if RF_DEBUG_QUIESCE
823 			printf("raid%d: Suspending: Waiting for Quiescence\n",
824 			       raidPtr->raidid);
825 #endif
826 			WAIT_FOR_QUIESCENCE(raidPtr);
827 			raidPtr->waiting_for_quiescence = 0;
828 		}
829 	}
830 #if RF_DEBUG_QUIESCE
831 	printf("raid%d: Quiescence reached..\n", raidPtr->raidid);
832 #endif
833 
834 	rf_unlock_mutex2(raidPtr->access_suspend_mutex);
835 	return (raidPtr->waiting_for_quiescence);
836 }
837 /* wake up everyone waiting for quiescence to be released */
838 void
rf_ResumeNewRequests(RF_Raid_t * raidPtr)839 rf_ResumeNewRequests(RF_Raid_t *raidPtr)
840 {
841 	RF_CallbackFuncDesc_t *t, *cb;
842 
843 #if RF_DEBUG_QUIESCE
844 	if (rf_quiesceDebug)
845 		printf("raid%d: Resuming new requests\n", raidPtr->raidid);
846 #endif
847 
848 	rf_lock_mutex2(raidPtr->access_suspend_mutex);
849 	raidPtr->accesses_suspended--;
850 	if (raidPtr->accesses_suspended == 0)
851 		cb = raidPtr->quiesce_wait_list;
852 	else
853 		cb = NULL;
854 	raidPtr->quiesce_wait_list = NULL;
855 	rf_unlock_mutex2(raidPtr->access_suspend_mutex);
856 
857 	while (cb) {
858 		t = cb;
859 		cb = cb->next;
860 		(t->callbackFunc) (t->callbackArg);
861 		rf_FreeCallbackFuncDesc(raidPtr, t);
862 	}
863 }
864 /*****************************************************************************************
865  *
866  * debug routines
867  *
868  ****************************************************************************************/
869 
870 static void
set_debug_option(char * name,long val)871 set_debug_option(char *name, long val)
872 {
873 	RF_DebugName_t *p;
874 
875 	for (p = rf_debugNames; p->name; p++) {
876 		if (!strcmp(p->name, name)) {
877 			*(p->ptr) = val;
878 			printf("[Set debug variable %s to %ld]\n", name, val);
879 			return;
880 		}
881 	}
882 	RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
883 }
884 
885 
886 /* would like to use sscanf here, but apparently not available in kernel */
887 /*ARGSUSED*/
888 static void
rf_ConfigureDebug(RF_Config_t * cfgPtr)889 rf_ConfigureDebug(RF_Config_t *cfgPtr)
890 {
891 	char   *val_p, *name_p, *white_p;
892 	long    val;
893 	int     i;
894 
895 	rf_ResetDebugOptions();
896 	for (i = 0; i < RF_MAXDBGV && cfgPtr->debugVars[i][0]; i++) {
897 		name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
898 		white_p = rf_find_white(name_p);	/* skip to start of 2nd
899 							 * word */
900 		val_p = rf_find_non_white(white_p);
901 		if (*val_p == '0' && *(val_p + 1) == 'x')
902 			val = rf_htoi(val_p + 2);
903 		else
904 			val = rf_atoi(val_p);
905 		*white_p = '\0';
906 		set_debug_option(name_p, val);
907 	}
908 }
909 
910 void
rf_print_panic_message(int line,const char * file)911 rf_print_panic_message(int line, const char *file)
912 {
913 	kern_assert("raidframe error at line %d file %s", line, file);
914 }
915 
916 #ifdef RAID_DIAGNOSTIC
917 void
rf_print_assert_panic_message(int line,const char * file,const char * condition)918 rf_print_assert_panic_message(int line,	const char *file, const char *condition)
919 {
920 	kern_assert("raidframe error at line %d file %s (failed asserting %s)\n",
921 	    line, file, condition);
922 }
923 #endif
924 
925 void
rf_print_unable_to_init_mutex(const char * file,int line,int rc)926 rf_print_unable_to_init_mutex(const char *file, int line, int rc)
927 {
928 	RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n",
929 		     file, line, rc);
930 }
931 
932 void
rf_print_unable_to_add_shutdown(const char * file,int line,int rc)933 rf_print_unable_to_add_shutdown(const char *file, int line, int rc)
934 {
935 	RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
936 		     file, line, rc);
937 }
938 
939 static void
rf_alloc_mutex_cond(RF_Raid_t * raidPtr)940 rf_alloc_mutex_cond(RF_Raid_t *raidPtr)
941 {
942 
943 	rf_init_mutex2(raidPtr->mutex, IPL_VM);
944 
945 	rf_init_cond2(raidPtr->outstandingCond, "rfocond");
946 	rf_init_cond2(raidPtr->parity_rewrite_cv, "rfprwshutdown");
947 	rf_init_mutex2(raidPtr->rad_lock, IPL_VM);
948 
949 	rf_init_mutex2(raidPtr->access_suspend_mutex, IPL_VM);
950 	rf_init_cond2(raidPtr->access_suspend_cv, "rfquiesce");
951 
952 	rf_init_cond2(raidPtr->waitForReconCond, "rfrcnw");
953 
954 	rf_init_cond2(raidPtr->adding_hot_spare_cv, "raidhs");
955 }
956 
957 static void
rf_destroy_mutex_cond(RF_Raid_t * raidPtr)958 rf_destroy_mutex_cond(RF_Raid_t *raidPtr)
959 {
960 
961 	rf_destroy_cond2(raidPtr->waitForReconCond);
962 	rf_destroy_cond2(raidPtr->adding_hot_spare_cv);
963 
964 	rf_destroy_mutex2(raidPtr->access_suspend_mutex);
965 	rf_destroy_cond2(raidPtr->access_suspend_cv);
966 
967 	rf_destroy_cond2(raidPtr->parity_rewrite_cv);
968 	rf_destroy_cond2(raidPtr->outstandingCond);
969 	rf_destroy_mutex2(raidPtr->rad_lock);
970 
971 	rf_destroy_mutex2(raidPtr->mutex);
972 }
973