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