rf_driver.c revision 1.27 1 /* $NetBSD: rf_driver.c,v 1.27 2000/01/09 03:44:33 oster Exp $ */
2 /*-
3 * Copyright (c) 1999 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Greg Oster
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Copyright (c) 1995 Carnegie-Mellon University.
40 * All rights reserved.
41 *
42 * Author: Mark Holland, Khalil Amiri, Claudson Bornstein, William V. Courtright II,
43 * Robby Findler, Daniel Stodolsky, Rachad Youssef, Jim Zelenka
44 *
45 * Permission to use, copy, modify and distribute this software and
46 * its documentation is hereby granted, provided that both the copyright
47 * notice and this permission notice appear in all copies of the
48 * software, derivative works or modified versions, and any portions
49 * thereof, and that both notices appear in supporting documentation.
50 *
51 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
52 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
53 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54 *
55 * Carnegie Mellon requests users of this software to return to
56 *
57 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
58 * School of Computer Science
59 * Carnegie Mellon University
60 * Pittsburgh PA 15213-3890
61 *
62 * any improvements or extensions that they make and grant Carnegie the
63 * rights to redistribute these changes.
64 */
65
66 /******************************************************************************
67 *
68 * rf_driver.c -- main setup, teardown, and access routines for the RAID driver
69 *
70 * all routines are prefixed with rf_ (raidframe), to avoid conficts.
71 *
72 ******************************************************************************/
73
74
75 #include <sys/types.h>
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/ioctl.h>
79 #include <sys/fcntl.h>
80 #include <sys/vnode.h>
81
82
83 #include "rf_archs.h"
84 #include "rf_threadstuff.h"
85
86 #include <sys/errno.h>
87
88 #include "rf_raid.h"
89 #include "rf_dag.h"
90 #include "rf_aselect.h"
91 #include "rf_diskqueue.h"
92 #include "rf_parityscan.h"
93 #include "rf_alloclist.h"
94 #include "rf_dagutils.h"
95 #include "rf_utils.h"
96 #include "rf_etimer.h"
97 #include "rf_acctrace.h"
98 #include "rf_configure.h"
99 #include "rf_general.h"
100 #include "rf_desc.h"
101 #include "rf_states.h"
102 #include "rf_freelist.h"
103 #include "rf_decluster.h"
104 #include "rf_map.h"
105 #include "rf_revent.h"
106 #include "rf_callback.h"
107 #include "rf_engine.h"
108 #include "rf_memchunk.h"
109 #include "rf_mcpair.h"
110 #include "rf_nwayxor.h"
111 #include "rf_debugprint.h"
112 #include "rf_copyback.h"
113 #include "rf_driver.h"
114 #include "rf_options.h"
115 #include "rf_shutdown.h"
116 #include "rf_kintf.h"
117
118 #include <sys/buf.h>
119
120 /* rad == RF_RaidAccessDesc_t */
121 static RF_FreeList_t *rf_rad_freelist;
122 #define RF_MAX_FREE_RAD 128
123 #define RF_RAD_INC 16
124 #define RF_RAD_INITIAL 32
125
126 /* debug variables */
127 char rf_panicbuf[2048]; /* a buffer to hold an error msg when we panic */
128
129 /* main configuration routines */
130 static int raidframe_booted = 0;
131
132 static void rf_ConfigureDebug(RF_Config_t * cfgPtr);
133 static void set_debug_option(char *name, long val);
134 static void rf_UnconfigureArray(void);
135 static int init_rad(RF_RaidAccessDesc_t *);
136 static void clean_rad(RF_RaidAccessDesc_t *);
137 static void rf_ShutdownRDFreeList(void *);
138 static int rf_ConfigureRDFreeList(RF_ShutdownList_t **);
139 void rf_UnconfigureVnodes( RF_Raid_t * );
140
141 RF_DECLARE_MUTEX(rf_printf_mutex) /* debug only: avoids interleaved
142 * printfs by different stripes */
143
144 #define SIGNAL_QUIESCENT_COND(_raid_) wakeup(&((_raid_)->accesses_suspended))
145 #define WAIT_FOR_QUIESCENCE(_raid_) \
146 tsleep(&((_raid_)->accesses_suspended),PRIBIO,"raidframe quiesce", 0);
147
148 #define IO_BUF_ERR(bp, err) { \
149 bp->b_flags |= B_ERROR; \
150 bp->b_resid = bp->b_bcount; \
151 bp->b_error = err; \
152 biodone(bp); \
153 }
154
155 static int configureCount = 0; /* number of active configurations */
156 static int isconfigged = 0; /* is basic raidframe (non per-array)
157 * stuff configged */
158 RF_DECLARE_STATIC_MUTEX(configureMutex) /* used to lock the configuration
159 * stuff */
160 static RF_ShutdownList_t *globalShutdown; /* non array-specific
161 * stuff */
162
163 static int rf_ConfigureRDFreeList(RF_ShutdownList_t ** listp);
164
165 /* called at system boot time */
166 int
167 rf_BootRaidframe()
168 {
169 int rc;
170
171 if (raidframe_booted)
172 return (EBUSY);
173 raidframe_booted = 1;
174
175 rc = rf_mutex_init(&configureMutex);
176 if (rc) {
177 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
178 __LINE__, rc);
179 RF_PANIC();
180 }
181 configureCount = 0;
182 isconfigged = 0;
183 globalShutdown = NULL;
184 return (0);
185 }
186 /*
187 * This function is really just for debugging user-level stuff: it
188 * frees up all memory, other RAIDframe resources which might otherwise
189 * be kept around. This is used with systems like "sentinel" to detect
190 * memory leaks.
191 */
192 int
193 rf_UnbootRaidframe()
194 {
195 int rc;
196
197 RF_LOCK_MUTEX(configureMutex);
198 if (configureCount) {
199 RF_UNLOCK_MUTEX(configureMutex);
200 return (EBUSY);
201 }
202 raidframe_booted = 0;
203 RF_UNLOCK_MUTEX(configureMutex);
204 rc = rf_mutex_destroy(&configureMutex);
205 if (rc) {
206 RF_ERRORMSG3("Unable to destroy mutex file %s line %d rc=%d\n", __FILE__,
207 __LINE__, rc);
208 RF_PANIC();
209 }
210 return (0);
211 }
212 /*
213 * Called whenever an array is shutdown
214 */
215 static void
216 rf_UnconfigureArray()
217 {
218 int rc;
219
220 RF_LOCK_MUTEX(configureMutex);
221 if (--configureCount == 0) { /* if no active configurations, shut
222 * everything down */
223 isconfigged = 0;
224
225 rc = rf_ShutdownList(&globalShutdown);
226 if (rc) {
227 RF_ERRORMSG1("RAIDFRAME: unable to do global shutdown, rc=%d\n", rc);
228 }
229
230 /*
231 * We must wait until now, because the AllocList module
232 * uses the DebugMem module.
233 */
234 if (rf_memDebug)
235 rf_print_unfreed();
236 }
237 RF_UNLOCK_MUTEX(configureMutex);
238 }
239
240 /*
241 * Called to shut down an array.
242 */
243 int
244 rf_Shutdown(raidPtr)
245 RF_Raid_t *raidPtr;
246 {
247
248 if (!raidPtr->valid) {
249 RF_ERRORMSG("Attempt to shut down unconfigured RAIDframe driver. Aborting shutdown\n");
250 return (EINVAL);
251 }
252 /*
253 * wait for outstanding IOs to land
254 * As described in rf_raid.h, we use the rad_freelist lock
255 * to protect the per-array info about outstanding descs
256 * since we need to do freelist locking anyway, and this
257 * cuts down on the amount of serialization we've got going
258 * on.
259 */
260 RF_FREELIST_DO_LOCK(rf_rad_freelist);
261 if (raidPtr->waitShutdown) {
262 RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
263 return (EBUSY);
264 }
265 raidPtr->waitShutdown = 1;
266 while (raidPtr->nAccOutstanding) {
267 RF_WAIT_COND(raidPtr->outstandingCond, RF_FREELIST_MUTEX_OF(rf_rad_freelist));
268 }
269 RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
270
271 raidPtr->valid = 0;
272
273 rf_update_component_labels(raidPtr);
274
275 rf_UnconfigureVnodes(raidPtr);
276
277 rf_ShutdownList(&raidPtr->shutdownList);
278
279 rf_UnconfigureArray();
280
281 return (0);
282 }
283
284 void
285 rf_UnconfigureVnodes( raidPtr )
286 RF_Raid_t *raidPtr;
287 {
288 int r,c;
289 struct proc *p;
290
291
292 /* We take this opportunity to close the vnodes like we should.. */
293
294 p = raidPtr->engine_thread;
295
296 for (r = 0; r < raidPtr->numRow; r++) {
297 for (c = 0; c < raidPtr->numCol; c++) {
298 printf("Closing vnode for row: %d col: %d\n", r, c);
299 if (raidPtr->raid_cinfo[r][c].ci_vp) {
300 VOP_UNLOCK(raidPtr->raid_cinfo[r][c].ci_vp, 0);
301 (void) vn_close(raidPtr->raid_cinfo[r][c].ci_vp,
302 FREAD | FWRITE, p->p_ucred, p);
303 raidPtr->raid_cinfo[r][c].ci_vp = NULL;
304 } else {
305 printf("vnode was NULL\n");
306 }
307
308 }
309 }
310 for (r = 0; r < raidPtr->numSpare; r++) {
311 printf("Closing vnode for spare: %d\n", r);
312 if (raidPtr->raid_cinfo[0][raidPtr->numCol + r].ci_vp) {
313 VOP_UNLOCK(raidPtr->raid_cinfo[0][raidPtr->numCol + r].ci_vp, 0);
314 (void) vn_close(raidPtr->raid_cinfo[0][raidPtr->numCol + r].ci_vp,
315 FREAD | FWRITE, p->p_ucred, p);
316 raidPtr->raid_cinfo[0][raidPtr->numCol + r].ci_vp = NULL;
317 } else {
318 printf("vnode was NULL\n");
319 }
320 }
321
322
323 }
324
325
326 #define DO_INIT_CONFIGURE(f) { \
327 rc = f (&globalShutdown); \
328 if (rc) { \
329 RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
330 rf_ShutdownList(&globalShutdown); \
331 configureCount--; \
332 RF_UNLOCK_MUTEX(configureMutex); \
333 return(rc); \
334 } \
335 }
336
337 #define DO_RAID_FAIL() { \
338 rf_UnconfigureVnodes(raidPtr); \
339 rf_ShutdownList(&raidPtr->shutdownList); \
340 rf_UnconfigureArray(); \
341 }
342
343 #define DO_RAID_INIT_CONFIGURE(f) { \
344 rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
345 if (rc) { \
346 RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
347 DO_RAID_FAIL(); \
348 return(rc); \
349 } \
350 }
351
352 #define DO_RAID_MUTEX(_m_) { \
353 rc = rf_create_managed_mutex(&raidPtr->shutdownList, (_m_)); \
354 if (rc) { \
355 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", \
356 __FILE__, __LINE__, rc); \
357 DO_RAID_FAIL(); \
358 return(rc); \
359 } \
360 }
361
362 #define DO_RAID_COND(_c_) { \
363 rc = rf_create_managed_cond(&raidPtr->shutdownList, (_c_)); \
364 if (rc) { \
365 RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", \
366 __FILE__, __LINE__, rc); \
367 DO_RAID_FAIL(); \
368 return(rc); \
369 } \
370 }
371
372 int
373 rf_Configure(raidPtr, cfgPtr)
374 RF_Raid_t *raidPtr;
375 RF_Config_t *cfgPtr;
376 {
377 RF_RowCol_t row, col;
378 int i, rc;
379 int unit;
380
381 if (raidPtr->valid) {
382 RF_ERRORMSG("RAIDframe configuration not shut down. Aborting configure.\n");
383 return (EINVAL);
384 }
385 RF_LOCK_MUTEX(configureMutex);
386 configureCount++;
387 if (isconfigged == 0) {
388 rc = rf_create_managed_mutex(&globalShutdown, &rf_printf_mutex);
389 if (rc) {
390 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
391 __LINE__, rc);
392 rf_ShutdownList(&globalShutdown);
393 return (rc);
394 }
395 /* initialize globals */
396 printf("RAIDFRAME: protectedSectors is %ld\n", rf_protectedSectors);
397
398 rf_clear_debug_print_buffer();
399
400 DO_INIT_CONFIGURE(rf_ConfigureAllocList);
401 /*
402 * Yes, this does make debugging general to the whole system instead
403 * of being array specific. Bummer, drag.
404 */
405 rf_ConfigureDebug(cfgPtr);
406 DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
407 DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
408 DO_INIT_CONFIGURE(rf_ConfigureMapModule);
409 DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
410 DO_INIT_CONFIGURE(rf_ConfigureCallback);
411 DO_INIT_CONFIGURE(rf_ConfigureMemChunk);
412 DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
413 DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
414 DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
415 DO_INIT_CONFIGURE(rf_ConfigureMCPair);
416 DO_INIT_CONFIGURE(rf_ConfigureDAGs);
417 DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
418 DO_INIT_CONFIGURE(rf_ConfigureDebugPrint);
419 DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
420 DO_INIT_CONFIGURE(rf_ConfigureCopyback);
421 DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
422 isconfigged = 1;
423 }
424 RF_UNLOCK_MUTEX(configureMutex);
425
426 /*
427 * Null out the entire raid descriptor to avoid problems when we reconfig.
428 * This also clears the valid bit.
429 */
430 /* XXX this clearing should be moved UP to outside of here.... that,
431 * or rf_Configure() needs to take more arguments... XXX */
432 unit = raidPtr->raidid;
433 bzero((char *) raidPtr, sizeof(RF_Raid_t));
434 raidPtr->raidid = unit;
435 DO_RAID_MUTEX(&raidPtr->mutex);
436 /* set up the cleanup list. Do this after ConfigureDebug so that
437 * value of memDebug will be set */
438
439 rf_MakeAllocList(raidPtr->cleanupList);
440 if (raidPtr->cleanupList == NULL) {
441 DO_RAID_FAIL();
442 return (ENOMEM);
443 }
444 rc = rf_ShutdownCreate(&raidPtr->shutdownList,
445 (void (*) (void *)) rf_FreeAllocList,
446 raidPtr->cleanupList);
447 if (rc) {
448 RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
449 __FILE__, __LINE__, rc);
450 DO_RAID_FAIL();
451 return (rc);
452 }
453 raidPtr->numRow = cfgPtr->numRow;
454 raidPtr->numCol = cfgPtr->numCol;
455 raidPtr->numSpare = cfgPtr->numSpare;
456
457 /* XXX we don't even pretend to support more than one row in the
458 * kernel... */
459 if (raidPtr->numRow != 1) {
460 RF_ERRORMSG("Only one row supported in kernel.\n");
461 DO_RAID_FAIL();
462 return (EINVAL);
463 }
464 RF_CallocAndAdd(raidPtr->status, raidPtr->numRow, sizeof(RF_RowStatus_t),
465 (RF_RowStatus_t *), raidPtr->cleanupList);
466 if (raidPtr->status == NULL) {
467 DO_RAID_FAIL();
468 return (ENOMEM);
469 }
470 RF_CallocAndAdd(raidPtr->reconControl, raidPtr->numRow,
471 sizeof(RF_ReconCtrl_t *), (RF_ReconCtrl_t **), raidPtr->cleanupList);
472 if (raidPtr->reconControl == NULL) {
473 DO_RAID_FAIL();
474 return (ENOMEM);
475 }
476 for (i = 0; i < raidPtr->numRow; i++) {
477 raidPtr->status[i] = rf_rs_optimal;
478 raidPtr->reconControl[i] = NULL;
479 }
480
481 DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
482 DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
483
484 DO_RAID_COND(&raidPtr->outstandingCond);
485
486 raidPtr->nAccOutstanding = 0;
487 raidPtr->waitShutdown = 0;
488
489 DO_RAID_MUTEX(&raidPtr->access_suspend_mutex);
490 DO_RAID_COND(&raidPtr->quiescent_cond);
491
492 DO_RAID_COND(&raidPtr->waitForReconCond);
493
494 DO_RAID_MUTEX(&raidPtr->recon_done_proc_mutex);
495 DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
496 DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
497 /* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
498 * no. is set */
499 DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
500
501 DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
502
503 DO_RAID_INIT_CONFIGURE(rf_ConfigurePSStatus);
504
505 for (row = 0; row < raidPtr->numRow; row++) {
506 for (col = 0; col < raidPtr->numCol; col++) {
507 /*
508 * XXX better distribution
509 */
510 raidPtr->hist_diskreq[row][col] = 0;
511 }
512 }
513
514 if (rf_keepAccTotals) {
515 raidPtr->keep_acc_totals = 1;
516 }
517 rf_StartUserStats(raidPtr);
518
519 raidPtr->valid = 1;
520 return (0);
521 }
522
523 static int
524 init_rad(desc)
525 RF_RaidAccessDesc_t *desc;
526 {
527 int rc;
528
529 rc = rf_mutex_init(&desc->mutex);
530 if (rc) {
531 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
532 __LINE__, rc);
533 return (rc);
534 }
535 rc = rf_cond_init(&desc->cond);
536 if (rc) {
537 RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
538 __LINE__, rc);
539 rf_mutex_destroy(&desc->mutex);
540 return (rc);
541 }
542 return (0);
543 }
544
545 static void
546 clean_rad(desc)
547 RF_RaidAccessDesc_t *desc;
548 {
549 rf_mutex_destroy(&desc->mutex);
550 rf_cond_destroy(&desc->cond);
551 }
552
553 static void
554 rf_ShutdownRDFreeList(ignored)
555 void *ignored;
556 {
557 RF_FREELIST_DESTROY_CLEAN(rf_rad_freelist, next, (RF_RaidAccessDesc_t *), clean_rad);
558 }
559
560 static int
561 rf_ConfigureRDFreeList(listp)
562 RF_ShutdownList_t **listp;
563 {
564 int rc;
565
566 RF_FREELIST_CREATE(rf_rad_freelist, RF_MAX_FREE_RAD,
567 RF_RAD_INC, sizeof(RF_RaidAccessDesc_t));
568 if (rf_rad_freelist == NULL) {
569 return (ENOMEM);
570 }
571 rc = rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
572 if (rc) {
573 RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n", __FILE__,
574 __LINE__, rc);
575 rf_ShutdownRDFreeList(NULL);
576 return (rc);
577 }
578 RF_FREELIST_PRIME_INIT(rf_rad_freelist, RF_RAD_INITIAL, next,
579 (RF_RaidAccessDesc_t *), init_rad);
580 return (0);
581 }
582
583 RF_RaidAccessDesc_t *
584 rf_AllocRaidAccDesc(
585 RF_Raid_t * raidPtr,
586 RF_IoType_t type,
587 RF_RaidAddr_t raidAddress,
588 RF_SectorCount_t numBlocks,
589 caddr_t bufPtr,
590 void *bp,
591 RF_DagHeader_t ** paramDAG,
592 RF_AccessStripeMapHeader_t ** paramASM,
593 RF_RaidAccessFlags_t flags,
594 void (*cbF) (struct buf *),
595 void *cbA,
596 RF_AccessState_t * states)
597 {
598 RF_RaidAccessDesc_t *desc;
599
600 RF_FREELIST_GET_INIT_NOUNLOCK(rf_rad_freelist, desc, next, (RF_RaidAccessDesc_t *), init_rad);
601 if (raidPtr->waitShutdown) {
602 /*
603 * Actually, we're shutting the array down. Free the desc
604 * and return NULL.
605 */
606 RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
607 RF_FREELIST_FREE_CLEAN(rf_rad_freelist, desc, next, clean_rad);
608 return (NULL);
609 }
610 raidPtr->nAccOutstanding++;
611 RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
612
613 desc->raidPtr = (void *) raidPtr;
614 desc->type = type;
615 desc->raidAddress = raidAddress;
616 desc->numBlocks = numBlocks;
617 desc->bufPtr = bufPtr;
618 desc->bp = bp;
619 desc->paramDAG = paramDAG;
620 desc->paramASM = paramASM;
621 desc->flags = flags;
622 desc->states = states;
623 desc->state = 0;
624
625 desc->status = 0;
626 bzero((char *) &desc->tracerec, sizeof(RF_AccTraceEntry_t));
627 desc->callbackFunc = (void (*) (RF_CBParam_t)) cbF; /* XXX */
628 desc->callbackArg = cbA;
629 desc->next = NULL;
630 desc->head = desc;
631 desc->numPending = 0;
632 desc->cleanupList = NULL;
633 rf_MakeAllocList(desc->cleanupList);
634 return (desc);
635 }
636
637 void
638 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t * desc)
639 {
640 RF_Raid_t *raidPtr = desc->raidPtr;
641
642 RF_ASSERT(desc);
643
644 rf_FreeAllocList(desc->cleanupList);
645 RF_FREELIST_FREE_CLEAN_NOUNLOCK(rf_rad_freelist, desc, next, clean_rad);
646 raidPtr->nAccOutstanding--;
647 if (raidPtr->waitShutdown) {
648 RF_SIGNAL_COND(raidPtr->outstandingCond);
649 }
650 RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
651 }
652 /*********************************************************************
653 * Main routine for performing an access.
654 * Accesses are retried until a DAG can not be selected. This occurs
655 * when either the DAG library is incomplete or there are too many
656 * failures in a parity group.
657 ********************************************************************/
658 int
659 rf_DoAccess(
660 RF_Raid_t * raidPtr,
661 RF_IoType_t type,
662 int async_flag,
663 RF_RaidAddr_t raidAddress,
664 RF_SectorCount_t numBlocks,
665 caddr_t bufPtr,
666 void *bp_in,
667 RF_DagHeader_t ** paramDAG,
668 RF_AccessStripeMapHeader_t ** paramASM,
669 RF_RaidAccessFlags_t flags,
670 RF_RaidAccessDesc_t ** paramDesc,
671 void (*cbF) (struct buf *),
672 void *cbA)
673 /*
674 type should be read or write
675 async_flag should be RF_TRUE or RF_FALSE
676 bp_in is a buf pointer. void * to facilitate ignoring it outside the kernel
677 */
678 {
679 RF_RaidAccessDesc_t *desc;
680 caddr_t lbufPtr = bufPtr;
681 struct buf *bp = (struct buf *) bp_in;
682
683 raidAddress += rf_raidSectorOffset;
684
685 if (!raidPtr->valid) {
686 RF_ERRORMSG("RAIDframe driver not successfully configured. Rejecting access.\n");
687 IO_BUF_ERR(bp, EINVAL);
688 return (EINVAL);
689 }
690
691 if (rf_accessDebug) {
692
693 printf("logBytes is: %d %d %d\n", raidPtr->raidid,
694 raidPtr->logBytesPerSector,
695 (int) rf_RaidAddressToByte(raidPtr, numBlocks));
696 printf("raid%d: %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", raidPtr->raidid,
697 (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
698 (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
699 (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
700 (int) numBlocks,
701 (int) rf_RaidAddressToByte(raidPtr, numBlocks),
702 (long) bufPtr);
703 }
704 if (raidAddress + numBlocks > raidPtr->totalSectors) {
705
706 printf("DoAccess: raid addr %lu too large to access %lu sectors. Max legal addr is %lu\n",
707 (u_long) raidAddress, (u_long) numBlocks, (u_long) raidPtr->totalSectors);
708
709 IO_BUF_ERR(bp, ENOSPC);
710 return (ENOSPC);
711 }
712 desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
713 numBlocks, lbufPtr, bp, paramDAG, paramASM,
714 flags, cbF, cbA, raidPtr->Layout.map->states);
715
716 if (desc == NULL) {
717 return (ENOMEM);
718 }
719 RF_ETIMER_START(desc->tracerec.tot_timer);
720
721 desc->async_flag = async_flag;
722
723 rf_ContinueRaidAccess(desc);
724
725 return (0);
726 }
727 /* force the array into reconfigured mode without doing reconstruction */
728 int
729 rf_SetReconfiguredMode(raidPtr, row, col)
730 RF_Raid_t *raidPtr;
731 int row;
732 int col;
733 {
734 if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
735 printf("Can't set reconfigured mode in dedicated-spare array\n");
736 RF_PANIC();
737 }
738 RF_LOCK_MUTEX(raidPtr->mutex);
739 raidPtr->numFailures++;
740 raidPtr->Disks[row][col].status = rf_ds_dist_spared;
741 raidPtr->status[row] = rf_rs_reconfigured;
742 /* install spare table only if declustering + distributed sparing
743 * architecture. */
744 if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
745 rf_InstallSpareTable(raidPtr, row, col);
746 RF_UNLOCK_MUTEX(raidPtr->mutex);
747 return (0);
748 }
749
750 extern int fail_row, fail_col, fail_time;
751 extern int delayed_recon;
752
753 int
754 rf_FailDisk(
755 RF_Raid_t * raidPtr,
756 int frow,
757 int fcol,
758 int initRecon)
759 {
760 printf("raid%d: Failing disk r%d c%d\n", raidPtr->raidid, frow, fcol);
761 RF_LOCK_MUTEX(raidPtr->mutex);
762 raidPtr->numFailures++;
763 raidPtr->Disks[frow][fcol].status = rf_ds_failed;
764 raidPtr->status[frow] = rf_rs_degraded;
765 RF_UNLOCK_MUTEX(raidPtr->mutex);
766 if (initRecon)
767 rf_ReconstructFailedDisk(raidPtr, frow, fcol);
768 return (0);
769 }
770 /* releases a thread that is waiting for the array to become quiesced.
771 * access_suspend_mutex should be locked upon calling this
772 */
773 void
774 rf_SignalQuiescenceLock(raidPtr, reconDesc)
775 RF_Raid_t *raidPtr;
776 RF_RaidReconDesc_t *reconDesc;
777 {
778 if (rf_quiesceDebug) {
779 printf("raid%d: Signalling quiescence lock\n",
780 raidPtr->raidid);
781 }
782 raidPtr->access_suspend_release = 1;
783
784 if (raidPtr->waiting_for_quiescence) {
785 SIGNAL_QUIESCENT_COND(raidPtr);
786 }
787 }
788 /* suspends all new requests to the array. No effect on accesses that are in flight. */
789 int
790 rf_SuspendNewRequestsAndWait(raidPtr)
791 RF_Raid_t *raidPtr;
792 {
793 if (rf_quiesceDebug)
794 printf("Suspending new reqs\n");
795
796 RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
797 raidPtr->accesses_suspended++;
798 raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
799
800 if (raidPtr->waiting_for_quiescence) {
801 raidPtr->access_suspend_release = 0;
802 while (!raidPtr->access_suspend_release) {
803 printf("Suspending: Waiting for Quiesence\n");
804 WAIT_FOR_QUIESCENCE(raidPtr);
805 raidPtr->waiting_for_quiescence = 0;
806 }
807 }
808 printf("Quiesence reached..\n");
809
810 RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
811 return (raidPtr->waiting_for_quiescence);
812 }
813 /* wake up everyone waiting for quiescence to be released */
814 void
815 rf_ResumeNewRequests(raidPtr)
816 RF_Raid_t *raidPtr;
817 {
818 RF_CallbackDesc_t *t, *cb;
819
820 if (rf_quiesceDebug)
821 printf("Resuming new reqs\n");
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(name, val)
847 char *name;
848 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(cfgPtr)
867 RF_Config_t *cfgPtr;
868 {
869 char *val_p, *name_p, *white_p;
870 long val;
871 int i;
872
873 rf_ResetDebugOptions();
874 for (i = 0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
875 name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
876 white_p = rf_find_white(name_p); /* skip to start of 2nd
877 * word */
878 val_p = rf_find_non_white(white_p);
879 if (*val_p == '0' && *(val_p + 1) == 'x')
880 val = rf_htoi(val_p + 2);
881 else
882 val = rf_atoi(val_p);
883 *white_p = '\0';
884 set_debug_option(name_p, val);
885 }
886 }
887 /* performance monitoring stuff */
888
889 #define TIMEVAL_TO_US(t) (((long) t.tv_sec) * 1000000L + (long) t.tv_usec)
890
891 #if !defined(_KERNEL) && !defined(SIMULATE)
892
893 /*
894 * Throughput stats currently only used in user-level RAIDframe
895 */
896
897 static int
898 rf_InitThroughputStats(
899 RF_ShutdownList_t ** listp,
900 RF_Raid_t * raidPtr,
901 RF_Config_t * cfgPtr)
902 {
903 int rc;
904
905 /* these used by user-level raidframe only */
906 rc = rf_create_managed_mutex(listp, &raidPtr->throughputstats.mutex);
907 if (rc) {
908 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
909 __LINE__, rc);
910 return (rc);
911 }
912 raidPtr->throughputstats.sum_io_us = 0;
913 raidPtr->throughputstats.num_ios = 0;
914 raidPtr->throughputstats.num_out_ios = 0;
915 return (0);
916 }
917
918 void
919 rf_StartThroughputStats(RF_Raid_t * raidPtr)
920 {
921 RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
922 raidPtr->throughputstats.num_ios++;
923 raidPtr->throughputstats.num_out_ios++;
924 if (raidPtr->throughputstats.num_out_ios == 1)
925 RF_GETTIME(raidPtr->throughputstats.start);
926 RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
927 }
928
929 static void
930 rf_StopThroughputStats(RF_Raid_t * raidPtr)
931 {
932 struct timeval diff;
933
934 RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
935 raidPtr->throughputstats.num_out_ios--;
936 if (raidPtr->throughputstats.num_out_ios == 0) {
937 RF_GETTIME(raidPtr->throughputstats.stop);
938 RF_TIMEVAL_DIFF(&raidPtr->throughputstats.start, &raidPtr->throughputstats.stop, &diff);
939 raidPtr->throughputstats.sum_io_us += TIMEVAL_TO_US(diff);
940 }
941 RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
942 }
943
944 static void
945 rf_PrintThroughputStats(RF_Raid_t * raidPtr)
946 {
947 RF_ASSERT(raidPtr->throughputstats.num_out_ios == 0);
948 if (raidPtr->throughputstats.sum_io_us != 0) {
949 printf("[Througphut: %8.2f IOs/second]\n", raidPtr->throughputstats.num_ios
950 / (raidPtr->throughputstats.sum_io_us / 1000000.0));
951 }
952 }
953 #endif /* !KERNEL && !SIMULATE */
954
955 void
956 rf_StartUserStats(RF_Raid_t * raidPtr)
957 {
958 RF_GETTIME(raidPtr->userstats.start);
959 raidPtr->userstats.sum_io_us = 0;
960 raidPtr->userstats.num_ios = 0;
961 raidPtr->userstats.num_sect_moved = 0;
962 }
963
964 void
965 rf_StopUserStats(RF_Raid_t * raidPtr)
966 {
967 RF_GETTIME(raidPtr->userstats.stop);
968 }
969
970 void
971 rf_UpdateUserStats(raidPtr, rt, numsect)
972 RF_Raid_t *raidPtr;
973 int rt; /* resp time in us */
974 int numsect; /* number of sectors for this access */
975 {
976 raidPtr->userstats.sum_io_us += rt;
977 raidPtr->userstats.num_ios++;
978 raidPtr->userstats.num_sect_moved += numsect;
979 }
980
981 void
982 rf_PrintUserStats(RF_Raid_t * raidPtr)
983 {
984 long elapsed_us, mbs, mbs_frac;
985 struct timeval diff;
986
987 RF_TIMEVAL_DIFF(&raidPtr->userstats.start, &raidPtr->userstats.stop, &diff);
988 elapsed_us = TIMEVAL_TO_US(diff);
989
990 /* 2000 sectors per megabyte, 10000000 microseconds per second */
991 if (elapsed_us)
992 mbs = (raidPtr->userstats.num_sect_moved / 2000) / (elapsed_us / 1000000);
993 else
994 mbs = 0;
995
996 /* this computes only the first digit of the fractional mb/s moved */
997 if (elapsed_us) {
998 mbs_frac = ((raidPtr->userstats.num_sect_moved / 200) / (elapsed_us / 1000000))
999 - (mbs * 10);
1000 } else {
1001 mbs_frac = 0;
1002 }
1003
1004 printf("Number of I/Os: %ld\n", raidPtr->userstats.num_ios);
1005 printf("Elapsed time (us): %ld\n", elapsed_us);
1006 printf("User I/Os per second: %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_ios, (elapsed_us / 1000000)));
1007 printf("Average user response time: %ld us\n", RF_DB0_CHECK(raidPtr->userstats.sum_io_us, raidPtr->userstats.num_ios));
1008 printf("Total sectors moved: %ld\n", raidPtr->userstats.num_sect_moved);
1009 printf("Average access size (sect): %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_sect_moved, raidPtr->userstats.num_ios));
1010 printf("Achieved data rate: %ld.%ld MB/sec\n", mbs, mbs_frac);
1011 }
1012