rf_driver.c revision 1.18 1 /* $NetBSD: rf_driver.c,v 1.18 1999/12/07 02:40:27 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_threadid.h"
95 #include "rf_dagutils.h"
96 #include "rf_utils.h"
97 #include "rf_etimer.h"
98 #include "rf_acctrace.h"
99 #include "rf_configure.h"
100 #include "rf_general.h"
101 #include "rf_desc.h"
102 #include "rf_states.h"
103 #include "rf_freelist.h"
104 #include "rf_decluster.h"
105 #include "rf_map.h"
106 #include "rf_diskthreads.h"
107 #include "rf_revent.h"
108 #include "rf_callback.h"
109 #include "rf_engine.h"
110 #include "rf_memchunk.h"
111 #include "rf_mcpair.h"
112 #include "rf_nwayxor.h"
113 #include "rf_debugprint.h"
114 #include "rf_copyback.h"
115 #include "rf_driver.h"
116 #include "rf_options.h"
117 #include "rf_shutdown.h"
118 #include "rf_cpuutil.h"
119
120 #include <sys/buf.h>
121
122 /* rad == RF_RaidAccessDesc_t */
123 static RF_FreeList_t *rf_rad_freelist;
124 #define RF_MAX_FREE_RAD 128
125 #define RF_RAD_INC 16
126 #define RF_RAD_INITIAL 32
127
128 /* debug variables */
129 char rf_panicbuf[2048]; /* a buffer to hold an error msg when we panic */
130
131 /* main configuration routines */
132 static int raidframe_booted = 0;
133
134 static void rf_ConfigureDebug(RF_Config_t * cfgPtr);
135 static void set_debug_option(char *name, long val);
136 static void rf_UnconfigureArray(void);
137 static int init_rad(RF_RaidAccessDesc_t *);
138 static void clean_rad(RF_RaidAccessDesc_t *);
139 static void rf_ShutdownRDFreeList(void *);
140 static int rf_ConfigureRDFreeList(RF_ShutdownList_t **);
141 void rf_UnconfigureVnodes( RF_Raid_t * );
142
143 /* XXX move these to their own .h file! */
144 int raidwrite_component_label(dev_t, struct vnode *, RF_ComponentLabel_t *);
145 int raidread_component_label(dev_t, struct vnode *, RF_ComponentLabel_t *);
146 int raidmarkclean(dev_t dev, struct vnode *b_vp,int);
147 void rf_update_component_labels( RF_Raid_t *);
148
149 RF_DECLARE_MUTEX(rf_printf_mutex) /* debug only: avoids interleaved
150 * printfs by different stripes */
151 RF_DECLARE_GLOBAL_THREADID /* declarations for threadid.h */
152
153
154 #define SIGNAL_QUIESCENT_COND(_raid_) wakeup(&((_raid_)->accesses_suspended))
155 #define WAIT_FOR_QUIESCENCE(_raid_) \
156 tsleep(&((_raid_)->accesses_suspended),PRIBIO,"raidframe quiesce", 0);
157
158 #define IO_BUF_ERR(bp, err, unit) { \
159 bp->b_flags |= B_ERROR; \
160 bp->b_resid = bp->b_bcount; \
161 bp->b_error = err; \
162 biodone(bp); \
163 }
164
165 static int configureCount = 0; /* number of active configurations */
166 static int isconfigged = 0; /* is basic raidframe (non per-array)
167 * stuff configged */
168 RF_DECLARE_STATIC_MUTEX(configureMutex) /* used to lock the configuration
169 * stuff */
170 static RF_ShutdownList_t *globalShutdown; /* non array-specific
171 * stuff */
172
173 static int rf_ConfigureRDFreeList(RF_ShutdownList_t ** listp);
174
175 /* called at system boot time */
176 int
177 rf_BootRaidframe()
178 {
179 int rc;
180
181 if (raidframe_booted)
182 return (EBUSY);
183 raidframe_booted = 1;
184
185 rf_setup_threadid();
186 rf_assign_threadid();
187
188 rc = rf_mutex_init(&configureMutex);
189 if (rc) {
190 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
191 __LINE__, rc);
192 RF_PANIC();
193 }
194 configureCount = 0;
195 isconfigged = 0;
196 globalShutdown = NULL;
197 return (0);
198 }
199 /*
200 * This function is really just for debugging user-level stuff: it
201 * frees up all memory, other RAIDframe resources which might otherwise
202 * be kept around. This is used with systems like "sentinel" to detect
203 * memory leaks.
204 */
205 int
206 rf_UnbootRaidframe()
207 {
208 int rc;
209
210 RF_LOCK_MUTEX(configureMutex);
211 if (configureCount) {
212 RF_UNLOCK_MUTEX(configureMutex);
213 return (EBUSY);
214 }
215 raidframe_booted = 0;
216 RF_UNLOCK_MUTEX(configureMutex);
217 rc = rf_mutex_destroy(&configureMutex);
218 if (rc) {
219 RF_ERRORMSG3("Unable to destroy mutex file %s line %d rc=%d\n", __FILE__,
220 __LINE__, rc);
221 RF_PANIC();
222 }
223 return (0);
224 }
225 /*
226 * Called whenever an array is shutdown
227 */
228 static void
229 rf_UnconfigureArray()
230 {
231 int rc;
232
233 RF_LOCK_MUTEX(configureMutex);
234 if (--configureCount == 0) { /* if no active configurations, shut
235 * everything down */
236 isconfigged = 0;
237
238 rc = rf_ShutdownList(&globalShutdown);
239 if (rc) {
240 RF_ERRORMSG1("RAIDFRAME: unable to do global shutdown, rc=%d\n", rc);
241 }
242 rf_shutdown_threadid();
243
244 /*
245 * We must wait until now, because the AllocList module
246 * uses the DebugMem module.
247 */
248 if (rf_memDebug)
249 rf_print_unfreed();
250 }
251 RF_UNLOCK_MUTEX(configureMutex);
252 }
253
254 /*
255 * Called to shut down an array.
256 */
257 int
258 rf_Shutdown(raidPtr)
259 RF_Raid_t *raidPtr;
260 {
261
262 if (!raidPtr->valid) {
263 RF_ERRORMSG("Attempt to shut down unconfigured RAIDframe driver. Aborting shutdown\n");
264 return (EINVAL);
265 }
266 /*
267 * wait for outstanding IOs to land
268 * As described in rf_raid.h, we use the rad_freelist lock
269 * to protect the per-array info about outstanding descs
270 * since we need to do freelist locking anyway, and this
271 * cuts down on the amount of serialization we've got going
272 * on.
273 */
274 RF_FREELIST_DO_LOCK(rf_rad_freelist);
275 if (raidPtr->waitShutdown) {
276 RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
277 return (EBUSY);
278 }
279 raidPtr->waitShutdown = 1;
280 while (raidPtr->nAccOutstanding) {
281 RF_WAIT_COND(raidPtr->outstandingCond, RF_FREELIST_MUTEX_OF(rf_rad_freelist));
282 }
283 RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
284
285 raidPtr->valid = 0;
286
287 rf_update_component_labels(raidPtr);
288
289 rf_UnconfigureVnodes(raidPtr);
290
291 rf_ShutdownList(&raidPtr->shutdownList);
292
293 rf_UnconfigureArray();
294
295 return (0);
296 }
297
298 void
299 rf_UnconfigureVnodes( raidPtr )
300 RF_Raid_t *raidPtr;
301 {
302 int r,c;
303 struct proc *p;
304
305
306 /* We take this opportunity to close the vnodes like we should.. */
307
308 p = raidPtr->engine_thread;
309
310 for (r = 0; r < raidPtr->numRow; r++) {
311 for (c = 0; c < raidPtr->numCol; c++) {
312 printf("Closing vnode for row: %d col: %d\n", r, c);
313 if (raidPtr->raid_cinfo[r][c].ci_vp) {
314 VOP_UNLOCK(raidPtr->raid_cinfo[r][c].ci_vp, 0);
315 (void) vn_close(raidPtr->raid_cinfo[r][c].ci_vp,
316 FREAD | FWRITE, p->p_ucred, p);
317 raidPtr->raid_cinfo[r][c].ci_vp = NULL;
318 } else {
319 printf("vnode was NULL\n");
320 }
321
322 }
323 }
324 for (r = 0; r < raidPtr->numSpare; r++) {
325 printf("Closing vnode for spare: %d\n", r);
326 if (raidPtr->raid_cinfo[0][raidPtr->numCol + r].ci_vp) {
327 VOP_UNLOCK(raidPtr->raid_cinfo[0][raidPtr->numCol + r].ci_vp, 0);
328 (void) vn_close(raidPtr->raid_cinfo[0][raidPtr->numCol + r].ci_vp,
329 FREAD | FWRITE, p->p_ucred, p);
330 raidPtr->raid_cinfo[0][raidPtr->numCol + r].ci_vp = NULL;
331 } else {
332 printf("vnode was NULL\n");
333 }
334 }
335
336
337 }
338
339
340 #define DO_INIT_CONFIGURE(f) { \
341 rc = f (&globalShutdown); \
342 if (rc) { \
343 RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
344 rf_ShutdownList(&globalShutdown); \
345 configureCount--; \
346 RF_UNLOCK_MUTEX(configureMutex); \
347 return(rc); \
348 } \
349 }
350
351 #define DO_RAID_FAIL() { \
352 rf_UnconfigureVnodes(raidPtr); \
353 rf_ShutdownList(&raidPtr->shutdownList); \
354 rf_UnconfigureArray(); \
355 }
356
357 #define DO_RAID_INIT_CONFIGURE(f) { \
358 rc = f (&raidPtr->shutdownList, raidPtr, cfgPtr); \
359 if (rc) { \
360 RF_ERRORMSG2("RAIDFRAME: failed %s with %d\n", RF_STRING(f), rc); \
361 DO_RAID_FAIL(); \
362 return(rc); \
363 } \
364 }
365
366 #define DO_RAID_MUTEX(_m_) { \
367 rc = rf_create_managed_mutex(&raidPtr->shutdownList, (_m_)); \
368 if (rc) { \
369 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", \
370 __FILE__, __LINE__, rc); \
371 DO_RAID_FAIL(); \
372 return(rc); \
373 } \
374 }
375
376 #define DO_RAID_COND(_c_) { \
377 rc = rf_create_managed_cond(&raidPtr->shutdownList, (_c_)); \
378 if (rc) { \
379 RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", \
380 __FILE__, __LINE__, rc); \
381 DO_RAID_FAIL(); \
382 return(rc); \
383 } \
384 }
385
386 int
387 rf_Configure(raidPtr, cfgPtr)
388 RF_Raid_t *raidPtr;
389 RF_Config_t *cfgPtr;
390 {
391 RF_RowCol_t row, col;
392 int i, rc;
393 int unit;
394
395 if (raidPtr->valid) {
396 RF_ERRORMSG("RAIDframe configuration not shut down. Aborting configure.\n");
397 return (EINVAL);
398 }
399 RF_LOCK_MUTEX(configureMutex);
400 configureCount++;
401 if (isconfigged == 0) {
402 rc = rf_create_managed_mutex(&globalShutdown, &rf_printf_mutex);
403 if (rc) {
404 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
405 __LINE__, rc);
406 rf_ShutdownList(&globalShutdown);
407 return (rc);
408 }
409 /* initialize globals */
410 printf("RAIDFRAME: protectedSectors is %ld\n", rf_protectedSectors);
411
412 rf_clear_debug_print_buffer();
413
414 DO_INIT_CONFIGURE(rf_ConfigureAllocList);
415 /*
416 * Yes, this does make debugging general to the whole system instead
417 * of being array specific. Bummer, drag.
418 */
419 rf_ConfigureDebug(cfgPtr);
420 DO_INIT_CONFIGURE(rf_ConfigureDebugMem);
421 DO_INIT_CONFIGURE(rf_ConfigureAccessTrace);
422 DO_INIT_CONFIGURE(rf_ConfigureMapModule);
423 DO_INIT_CONFIGURE(rf_ConfigureReconEvent);
424 DO_INIT_CONFIGURE(rf_ConfigureCallback);
425 DO_INIT_CONFIGURE(rf_ConfigureMemChunk);
426 DO_INIT_CONFIGURE(rf_ConfigureRDFreeList);
427 DO_INIT_CONFIGURE(rf_ConfigureNWayXor);
428 DO_INIT_CONFIGURE(rf_ConfigureStripeLockFreeList);
429 DO_INIT_CONFIGURE(rf_ConfigureMCPair);
430 #if !defined(__NetBSD__)
431 DO_INIT_CONFIGURE(rf_ConfigureCamLayer);
432 #endif
433 DO_INIT_CONFIGURE(rf_ConfigureDAGs);
434 DO_INIT_CONFIGURE(rf_ConfigureDAGFuncs);
435 DO_INIT_CONFIGURE(rf_ConfigureDebugPrint);
436 DO_INIT_CONFIGURE(rf_ConfigureReconstruction);
437 DO_INIT_CONFIGURE(rf_ConfigureCopyback);
438 DO_INIT_CONFIGURE(rf_ConfigureDiskQueueSystem);
439 DO_INIT_CONFIGURE(rf_ConfigureCpuMonitor);
440 isconfigged = 1;
441 }
442 RF_UNLOCK_MUTEX(configureMutex);
443
444 /*
445 * Null out the entire raid descriptor to avoid problems when we reconfig.
446 * This also clears the valid bit.
447 */
448 /* XXX this clearing should be moved UP to outside of here.... that,
449 * or rf_Configure() needs to take more arguments... XXX */
450 unit = raidPtr->raidid;
451 bzero((char *) raidPtr, sizeof(RF_Raid_t));
452 raidPtr->raidid = unit;
453 DO_RAID_MUTEX(&raidPtr->mutex);
454 /* set up the cleanup list. Do this after ConfigureDebug so that
455 * value of memDebug will be set */
456
457 rf_MakeAllocList(raidPtr->cleanupList);
458 if (raidPtr->cleanupList == NULL) {
459 DO_RAID_FAIL();
460 return (ENOMEM);
461 }
462 rc = rf_ShutdownCreate(&raidPtr->shutdownList,
463 (void (*) (void *)) rf_FreeAllocList,
464 raidPtr->cleanupList);
465 if (rc) {
466 RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
467 __FILE__, __LINE__, rc);
468 DO_RAID_FAIL();
469 return (rc);
470 }
471 raidPtr->numRow = cfgPtr->numRow;
472 raidPtr->numCol = cfgPtr->numCol;
473 raidPtr->numSpare = cfgPtr->numSpare;
474
475 /* XXX we don't even pretend to support more than one row in the
476 * kernel... */
477 if (raidPtr->numRow != 1) {
478 RF_ERRORMSG("Only one row supported in kernel.\n");
479 DO_RAID_FAIL();
480 return (EINVAL);
481 }
482 RF_CallocAndAdd(raidPtr->status, raidPtr->numRow, sizeof(RF_RowStatus_t),
483 (RF_RowStatus_t *), raidPtr->cleanupList);
484 if (raidPtr->status == NULL) {
485 DO_RAID_FAIL();
486 return (ENOMEM);
487 }
488 RF_CallocAndAdd(raidPtr->reconControl, raidPtr->numRow,
489 sizeof(RF_ReconCtrl_t *), (RF_ReconCtrl_t **), raidPtr->cleanupList);
490 if (raidPtr->reconControl == NULL) {
491 DO_RAID_FAIL();
492 return (ENOMEM);
493 }
494 for (i = 0; i < raidPtr->numRow; i++) {
495 raidPtr->status[i] = rf_rs_optimal;
496 raidPtr->reconControl[i] = NULL;
497 }
498
499 DO_RAID_INIT_CONFIGURE(rf_ConfigureEngine);
500 DO_RAID_INIT_CONFIGURE(rf_ConfigureStripeLocks);
501
502 DO_RAID_COND(&raidPtr->outstandingCond);
503
504 raidPtr->nAccOutstanding = 0;
505 raidPtr->waitShutdown = 0;
506
507 DO_RAID_MUTEX(&raidPtr->access_suspend_mutex);
508 DO_RAID_COND(&raidPtr->quiescent_cond);
509
510 DO_RAID_COND(&raidPtr->waitForReconCond);
511
512 DO_RAID_MUTEX(&raidPtr->recon_done_proc_mutex);
513 DO_RAID_INIT_CONFIGURE(rf_ConfigureDisks);
514 DO_RAID_INIT_CONFIGURE(rf_ConfigureSpareDisks);
515 /* do this after ConfigureDisks & ConfigureSpareDisks to be sure dev
516 * no. is set */
517 DO_RAID_INIT_CONFIGURE(rf_ConfigureDiskQueues);
518
519 DO_RAID_INIT_CONFIGURE(rf_ConfigureLayout);
520
521 DO_RAID_INIT_CONFIGURE(rf_ConfigurePSStatus);
522
523 for (row = 0; row < raidPtr->numRow; row++) {
524 for (col = 0; col < raidPtr->numCol; col++) {
525 /*
526 * XXX better distribution
527 */
528 raidPtr->hist_diskreq[row][col] = 0;
529 }
530 }
531
532 if (rf_keepAccTotals) {
533 raidPtr->keep_acc_totals = 1;
534 }
535 rf_StartUserStats(raidPtr);
536
537 raidPtr->valid = 1;
538 return (0);
539 }
540
541 static int
542 init_rad(desc)
543 RF_RaidAccessDesc_t *desc;
544 {
545 int rc;
546
547 rc = rf_mutex_init(&desc->mutex);
548 if (rc) {
549 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
550 __LINE__, rc);
551 return (rc);
552 }
553 rc = rf_cond_init(&desc->cond);
554 if (rc) {
555 RF_ERRORMSG3("Unable to init cond file %s line %d rc=%d\n", __FILE__,
556 __LINE__, rc);
557 rf_mutex_destroy(&desc->mutex);
558 return (rc);
559 }
560 return (0);
561 }
562
563 static void
564 clean_rad(desc)
565 RF_RaidAccessDesc_t *desc;
566 {
567 rf_mutex_destroy(&desc->mutex);
568 rf_cond_destroy(&desc->cond);
569 }
570
571 static void
572 rf_ShutdownRDFreeList(ignored)
573 void *ignored;
574 {
575 RF_FREELIST_DESTROY_CLEAN(rf_rad_freelist, next, (RF_RaidAccessDesc_t *), clean_rad);
576 }
577
578 static int
579 rf_ConfigureRDFreeList(listp)
580 RF_ShutdownList_t **listp;
581 {
582 int rc;
583
584 RF_FREELIST_CREATE(rf_rad_freelist, RF_MAX_FREE_RAD,
585 RF_RAD_INC, sizeof(RF_RaidAccessDesc_t));
586 if (rf_rad_freelist == NULL) {
587 return (ENOMEM);
588 }
589 rc = rf_ShutdownCreate(listp, rf_ShutdownRDFreeList, NULL);
590 if (rc) {
591 RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n", __FILE__,
592 __LINE__, rc);
593 rf_ShutdownRDFreeList(NULL);
594 return (rc);
595 }
596 RF_FREELIST_PRIME_INIT(rf_rad_freelist, RF_RAD_INITIAL, next,
597 (RF_RaidAccessDesc_t *), init_rad);
598 return (0);
599 }
600
601 RF_RaidAccessDesc_t *
602 rf_AllocRaidAccDesc(
603 RF_Raid_t * raidPtr,
604 RF_IoType_t type,
605 RF_RaidAddr_t raidAddress,
606 RF_SectorCount_t numBlocks,
607 caddr_t bufPtr,
608 void *bp,
609 RF_DagHeader_t ** paramDAG,
610 RF_AccessStripeMapHeader_t ** paramASM,
611 RF_RaidAccessFlags_t flags,
612 void (*cbF) (struct buf *),
613 void *cbA,
614 RF_AccessState_t * states)
615 {
616 RF_RaidAccessDesc_t *desc;
617
618 RF_FREELIST_GET_INIT_NOUNLOCK(rf_rad_freelist, desc, next, (RF_RaidAccessDesc_t *), init_rad);
619 if (raidPtr->waitShutdown) {
620 /*
621 * Actually, we're shutting the array down. Free the desc
622 * and return NULL.
623 */
624 RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
625 RF_FREELIST_FREE_CLEAN(rf_rad_freelist, desc, next, clean_rad);
626 return (NULL);
627 }
628 raidPtr->nAccOutstanding++;
629 RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
630
631 desc->raidPtr = (void *) raidPtr;
632 desc->type = type;
633 desc->raidAddress = raidAddress;
634 desc->numBlocks = numBlocks;
635 desc->bufPtr = bufPtr;
636 desc->bp = bp;
637 desc->paramDAG = paramDAG;
638 desc->paramASM = paramASM;
639 desc->flags = flags;
640 desc->states = states;
641 desc->state = 0;
642
643 desc->status = 0;
644 bzero((char *) &desc->tracerec, sizeof(RF_AccTraceEntry_t));
645 desc->callbackFunc = (void (*) (RF_CBParam_t)) cbF; /* XXX */
646 desc->callbackArg = cbA;
647 desc->next = NULL;
648 desc->head = desc;
649 desc->numPending = 0;
650 desc->cleanupList = NULL;
651 rf_MakeAllocList(desc->cleanupList);
652 rf_get_threadid(desc->tid);
653 return (desc);
654 }
655
656 void
657 rf_FreeRaidAccDesc(RF_RaidAccessDesc_t * desc)
658 {
659 RF_Raid_t *raidPtr = desc->raidPtr;
660
661 RF_ASSERT(desc);
662
663 rf_FreeAllocList(desc->cleanupList);
664 RF_FREELIST_FREE_CLEAN_NOUNLOCK(rf_rad_freelist, desc, next, clean_rad);
665 raidPtr->nAccOutstanding--;
666 if (raidPtr->waitShutdown) {
667 RF_SIGNAL_COND(raidPtr->outstandingCond);
668 }
669 RF_FREELIST_DO_UNLOCK(rf_rad_freelist);
670 }
671 /*********************************************************************
672 * Main routine for performing an access.
673 * Accesses are retried until a DAG can not be selected. This occurs
674 * when either the DAG library is incomplete or there are too many
675 * failures in a parity group.
676 ********************************************************************/
677 int
678 rf_DoAccess(
679 RF_Raid_t * raidPtr,
680 RF_IoType_t type,
681 int async_flag,
682 RF_RaidAddr_t raidAddress,
683 RF_SectorCount_t numBlocks,
684 caddr_t bufPtr,
685 void *bp_in,
686 RF_DagHeader_t ** paramDAG,
687 RF_AccessStripeMapHeader_t ** paramASM,
688 RF_RaidAccessFlags_t flags,
689 RF_RaidAccessDesc_t ** paramDesc,
690 void (*cbF) (struct buf *),
691 void *cbA)
692 /*
693 type should be read or write
694 async_flag should be RF_TRUE or RF_FALSE
695 bp_in is a buf pointer. void * to facilitate ignoring it outside the kernel
696 */
697 {
698 int tid;
699 RF_RaidAccessDesc_t *desc;
700 caddr_t lbufPtr = bufPtr;
701 struct buf *bp = (struct buf *) bp_in;
702 #if DFSTRACE > 0
703 struct {
704 RF_uint64 raidAddr;
705 int numBlocks;
706 char type;
707 } dfsrecord;
708 #endif /* DFSTRACE > 0 */
709
710 raidAddress += rf_raidSectorOffset;
711
712 if (!raidPtr->valid) {
713 RF_ERRORMSG("RAIDframe driver not successfully configured. Rejecting access.\n");
714 IO_BUF_ERR(bp, EINVAL, raidPtr->raidid);
715 return (EINVAL);
716 }
717 #if defined(KERNEL) && DFSTRACE > 0
718 if (rf_DFSTraceAccesses) {
719 dfsrecord.raidAddr = raidAddress;
720 dfsrecord.numBlocks = numBlocks;
721 dfsrecord.type = type;
722 dfs_log(DFS_NOTE, (char *) &dfsrecord, sizeof(dfsrecord), 0);
723 }
724 #endif /* KERNEL && DFSTRACE > 0 */
725
726 rf_get_threadid(tid);
727 if (rf_accessDebug) {
728
729 printf("logBytes is: %d %d %d\n", raidPtr->raidid,
730 raidPtr->logBytesPerSector,
731 (int) rf_RaidAddressToByte(raidPtr, numBlocks));
732 printf("[%d] %s raidAddr %d (stripeid %d-%d) numBlocks %d (%d bytes) buf 0x%lx\n", tid,
733 (type == RF_IO_TYPE_READ) ? "READ" : "WRITE", (int) raidAddress,
734 (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress),
735 (int) rf_RaidAddressToStripeID(&raidPtr->Layout, raidAddress + numBlocks - 1),
736 (int) numBlocks,
737 (int) rf_RaidAddressToByte(raidPtr, numBlocks),
738 (long) bufPtr);
739 }
740 if (raidAddress + numBlocks > raidPtr->totalSectors) {
741
742 printf("DoAccess: raid addr %lu too large to access %lu sectors. Max legal addr is %lu\n",
743 (u_long) raidAddress, (u_long) numBlocks, (u_long) raidPtr->totalSectors);
744
745 IO_BUF_ERR(bp, ENOSPC, raidPtr->raidid);
746 return (ENOSPC);
747 }
748 desc = rf_AllocRaidAccDesc(raidPtr, type, raidAddress,
749 numBlocks, lbufPtr, bp, paramDAG, paramASM,
750 flags, cbF, cbA, raidPtr->Layout.map->states);
751
752 if (desc == NULL) {
753 return (ENOMEM);
754 }
755 RF_ETIMER_START(desc->tracerec.tot_timer);
756
757 desc->async_flag = async_flag;
758
759 rf_ContinueRaidAccess(desc);
760
761 return (0);
762 }
763 /* force the array into reconfigured mode without doing reconstruction */
764 int
765 rf_SetReconfiguredMode(raidPtr, row, col)
766 RF_Raid_t *raidPtr;
767 int row;
768 int col;
769 {
770 if (!(raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE)) {
771 printf("Can't set reconfigured mode in dedicated-spare array\n");
772 RF_PANIC();
773 }
774 RF_LOCK_MUTEX(raidPtr->mutex);
775 raidPtr->numFailures++;
776 raidPtr->Disks[row][col].status = rf_ds_dist_spared;
777 raidPtr->status[row] = rf_rs_reconfigured;
778 /* install spare table only if declustering + distributed sparing
779 * architecture. */
780 if (raidPtr->Layout.map->flags & RF_BD_DECLUSTERED)
781 rf_InstallSpareTable(raidPtr, row, col);
782 RF_UNLOCK_MUTEX(raidPtr->mutex);
783 return (0);
784 }
785
786 extern int fail_row, fail_col, fail_time;
787 extern int delayed_recon;
788
789 int
790 rf_FailDisk(
791 RF_Raid_t * raidPtr,
792 int frow,
793 int fcol,
794 int initRecon)
795 {
796 int tid;
797
798 rf_get_threadid(tid);
799 printf("[%d] Failing disk r%d c%d\n", tid, frow, fcol);
800 RF_LOCK_MUTEX(raidPtr->mutex);
801 raidPtr->numFailures++;
802 raidPtr->Disks[frow][fcol].status = rf_ds_failed;
803 raidPtr->status[frow] = rf_rs_degraded;
804 RF_UNLOCK_MUTEX(raidPtr->mutex);
805 if (initRecon)
806 rf_ReconstructFailedDisk(raidPtr, frow, fcol);
807 return (0);
808 }
809 /* releases a thread that is waiting for the array to become quiesced.
810 * access_suspend_mutex should be locked upon calling this
811 */
812 void
813 rf_SignalQuiescenceLock(raidPtr, reconDesc)
814 RF_Raid_t *raidPtr;
815 RF_RaidReconDesc_t *reconDesc;
816 {
817 int tid;
818
819 if (rf_quiesceDebug) {
820 rf_get_threadid(tid);
821 printf("[%d] Signalling quiescence lock\n", tid);
822 }
823 raidPtr->access_suspend_release = 1;
824
825 if (raidPtr->waiting_for_quiescence) {
826 SIGNAL_QUIESCENT_COND(raidPtr);
827 }
828 }
829 /* suspends all new requests to the array. No effect on accesses that are in flight. */
830 int
831 rf_SuspendNewRequestsAndWait(raidPtr)
832 RF_Raid_t *raidPtr;
833 {
834 if (rf_quiesceDebug)
835 printf("Suspending new reqs\n");
836
837 RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
838 raidPtr->accesses_suspended++;
839 raidPtr->waiting_for_quiescence = (raidPtr->accs_in_flight == 0) ? 0 : 1;
840
841 if (raidPtr->waiting_for_quiescence) {
842 raidPtr->access_suspend_release = 0;
843 while (!raidPtr->access_suspend_release) {
844 printf("Suspending: Waiting for Quiesence\n");
845 WAIT_FOR_QUIESCENCE(raidPtr);
846 raidPtr->waiting_for_quiescence = 0;
847 }
848 }
849 printf("Quiesence reached..\n");
850
851 RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
852 return (raidPtr->waiting_for_quiescence);
853 }
854 /* wake up everyone waiting for quiescence to be released */
855 void
856 rf_ResumeNewRequests(raidPtr)
857 RF_Raid_t *raidPtr;
858 {
859 RF_CallbackDesc_t *t, *cb;
860
861 if (rf_quiesceDebug)
862 printf("Resuming new reqs\n");
863
864 RF_LOCK_MUTEX(raidPtr->access_suspend_mutex);
865 raidPtr->accesses_suspended--;
866 if (raidPtr->accesses_suspended == 0)
867 cb = raidPtr->quiesce_wait_list;
868 else
869 cb = NULL;
870 raidPtr->quiesce_wait_list = NULL;
871 RF_UNLOCK_MUTEX(raidPtr->access_suspend_mutex);
872
873 while (cb) {
874 t = cb;
875 cb = cb->next;
876 (t->callbackFunc) (t->callbackArg);
877 rf_FreeCallbackDesc(t);
878 }
879 }
880 /*****************************************************************************************
881 *
882 * debug routines
883 *
884 ****************************************************************************************/
885
886 static void
887 set_debug_option(name, val)
888 char *name;
889 long val;
890 {
891 RF_DebugName_t *p;
892
893 for (p = rf_debugNames; p->name; p++) {
894 if (!strcmp(p->name, name)) {
895 *(p->ptr) = val;
896 printf("[Set debug variable %s to %ld]\n", name, val);
897 return;
898 }
899 }
900 RF_ERRORMSG1("Unknown debug string \"%s\"\n", name);
901 }
902
903
904 /* would like to use sscanf here, but apparently not available in kernel */
905 /*ARGSUSED*/
906 static void
907 rf_ConfigureDebug(cfgPtr)
908 RF_Config_t *cfgPtr;
909 {
910 char *val_p, *name_p, *white_p;
911 long val;
912 int i;
913
914 rf_ResetDebugOptions();
915 for (i = 0; cfgPtr->debugVars[i][0] && i < RF_MAXDBGV; i++) {
916 name_p = rf_find_non_white(&cfgPtr->debugVars[i][0]);
917 white_p = rf_find_white(name_p); /* skip to start of 2nd
918 * word */
919 val_p = rf_find_non_white(white_p);
920 if (*val_p == '0' && *(val_p + 1) == 'x')
921 val = rf_htoi(val_p + 2);
922 else
923 val = rf_atoi(val_p);
924 *white_p = '\0';
925 set_debug_option(name_p, val);
926 }
927 }
928 /* performance monitoring stuff */
929
930 #define TIMEVAL_TO_US(t) (((long) t.tv_sec) * 1000000L + (long) t.tv_usec)
931
932 #if !defined(_KERNEL) && !defined(SIMULATE)
933
934 /*
935 * Throughput stats currently only used in user-level RAIDframe
936 */
937
938 static int
939 rf_InitThroughputStats(
940 RF_ShutdownList_t ** listp,
941 RF_Raid_t * raidPtr,
942 RF_Config_t * cfgPtr)
943 {
944 int rc;
945
946 /* these used by user-level raidframe only */
947 rc = rf_create_managed_mutex(listp, &raidPtr->throughputstats.mutex);
948 if (rc) {
949 RF_ERRORMSG3("Unable to init mutex file %s line %d rc=%d\n", __FILE__,
950 __LINE__, rc);
951 return (rc);
952 }
953 raidPtr->throughputstats.sum_io_us = 0;
954 raidPtr->throughputstats.num_ios = 0;
955 raidPtr->throughputstats.num_out_ios = 0;
956 return (0);
957 }
958
959 void
960 rf_StartThroughputStats(RF_Raid_t * raidPtr)
961 {
962 RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
963 raidPtr->throughputstats.num_ios++;
964 raidPtr->throughputstats.num_out_ios++;
965 if (raidPtr->throughputstats.num_out_ios == 1)
966 RF_GETTIME(raidPtr->throughputstats.start);
967 RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
968 }
969
970 static void
971 rf_StopThroughputStats(RF_Raid_t * raidPtr)
972 {
973 struct timeval diff;
974
975 RF_LOCK_MUTEX(raidPtr->throughputstats.mutex);
976 raidPtr->throughputstats.num_out_ios--;
977 if (raidPtr->throughputstats.num_out_ios == 0) {
978 RF_GETTIME(raidPtr->throughputstats.stop);
979 RF_TIMEVAL_DIFF(&raidPtr->throughputstats.start, &raidPtr->throughputstats.stop, &diff);
980 raidPtr->throughputstats.sum_io_us += TIMEVAL_TO_US(diff);
981 }
982 RF_UNLOCK_MUTEX(raidPtr->throughputstats.mutex);
983 }
984
985 static void
986 rf_PrintThroughputStats(RF_Raid_t * raidPtr)
987 {
988 RF_ASSERT(raidPtr->throughputstats.num_out_ios == 0);
989 if (raidPtr->throughputstats.sum_io_us != 0) {
990 printf("[Througphut: %8.2f IOs/second]\n", raidPtr->throughputstats.num_ios
991 / (raidPtr->throughputstats.sum_io_us / 1000000.0));
992 }
993 }
994 #endif /* !KERNEL && !SIMULATE */
995
996 void
997 rf_StartUserStats(RF_Raid_t * raidPtr)
998 {
999 RF_GETTIME(raidPtr->userstats.start);
1000 raidPtr->userstats.sum_io_us = 0;
1001 raidPtr->userstats.num_ios = 0;
1002 raidPtr->userstats.num_sect_moved = 0;
1003 }
1004
1005 void
1006 rf_StopUserStats(RF_Raid_t * raidPtr)
1007 {
1008 RF_GETTIME(raidPtr->userstats.stop);
1009 }
1010
1011 void
1012 rf_UpdateUserStats(raidPtr, rt, numsect)
1013 RF_Raid_t *raidPtr;
1014 int rt; /* resp time in us */
1015 int numsect; /* number of sectors for this access */
1016 {
1017 raidPtr->userstats.sum_io_us += rt;
1018 raidPtr->userstats.num_ios++;
1019 raidPtr->userstats.num_sect_moved += numsect;
1020 }
1021
1022 void
1023 rf_PrintUserStats(RF_Raid_t * raidPtr)
1024 {
1025 long elapsed_us, mbs, mbs_frac;
1026 struct timeval diff;
1027
1028 RF_TIMEVAL_DIFF(&raidPtr->userstats.start, &raidPtr->userstats.stop, &diff);
1029 elapsed_us = TIMEVAL_TO_US(diff);
1030
1031 /* 2000 sectors per megabyte, 10000000 microseconds per second */
1032 if (elapsed_us)
1033 mbs = (raidPtr->userstats.num_sect_moved / 2000) / (elapsed_us / 1000000);
1034 else
1035 mbs = 0;
1036
1037 /* this computes only the first digit of the fractional mb/s moved */
1038 if (elapsed_us) {
1039 mbs_frac = ((raidPtr->userstats.num_sect_moved / 200) / (elapsed_us / 1000000))
1040 - (mbs * 10);
1041 } else {
1042 mbs_frac = 0;
1043 }
1044
1045 printf("Number of I/Os: %ld\n", raidPtr->userstats.num_ios);
1046 printf("Elapsed time (us): %ld\n", elapsed_us);
1047 printf("User I/Os per second: %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_ios, (elapsed_us / 1000000)));
1048 printf("Average user response time: %ld us\n", RF_DB0_CHECK(raidPtr->userstats.sum_io_us, raidPtr->userstats.num_ios));
1049 printf("Total sectors moved: %ld\n", raidPtr->userstats.num_sect_moved);
1050 printf("Average access size (sect): %ld\n", RF_DB0_CHECK(raidPtr->userstats.num_sect_moved, raidPtr->userstats.num_ios));
1051 printf("Achieved data rate: %ld.%ld MB/sec\n", mbs, mbs_frac);
1052 }
1053