1 /* $NetBSD: rf_states.c,v 1.43 2008/05/20 00:29:54 oster Exp $ */
3 * Copyright (c) 1995 Carnegie-Mellon University.
6 * Author: Mark Holland, William V. Courtright II, Robby Findler
8 * Permission to use, copy, modify and distribute this software and
9 * its documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
16 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: rf_states.c,v 1.43 2008/05/20 00:29:54 oster Exp $");
32 #include <sys/errno.h>
35 #include "rf_threadstuff.h"
39 #include "rf_aselect.h"
40 #include "rf_general.h"
41 #include "rf_states.h"
42 #include "rf_dagutils.h"
43 #include "rf_driver.h"
44 #include "rf_engine.h"
46 #include "rf_etimer.h"
48 #include "rf_paritymap.h"
50 #ifndef RF_DEBUG_STATES
51 #define RF_DEBUG_STATES 0
54 /* prototypes for some of the available states.
60 - either schedule rf_ContinueRaidAccess as a callback and return
61 RF_TRUE, or complete all of their work and return RF_FALSE.
63 - increment desc->state when they have finished their work.
68 StateName(RF_AccessState_t state
)
71 case rf_QuiesceState
:return "QuiesceState";
76 case rf_CreateDAGState
:
77 return "CreateDAGState";
78 case rf_ExecuteDAGState
:
79 return "ExecuteDAGState";
80 case rf_ProcessDAGState
:
81 return "ProcessDAGState";
83 return "CleanupState";
86 case rf_IncrAccessesCountState
:
87 return "IncrAccessesCountState";
88 case rf_DecrAccessesCountState
:
89 return "DecrAccessesCountState";
91 return "!!! UnnamedState !!!";
97 rf_ContinueRaidAccess(RF_RaidAccessDesc_t
*desc
)
99 int suspended
= RF_FALSE
;
100 int current_state_index
= desc
->state
;
101 RF_AccessState_t current_state
= desc
->states
[current_state_index
];
103 int unit
= desc
->raidPtr
->raidid
;
108 current_state_index
= desc
->state
;
109 current_state
= desc
->states
[current_state_index
];
111 switch (current_state
) {
113 case rf_QuiesceState
:
114 suspended
= rf_State_Quiesce(desc
);
116 case rf_IncrAccessesCountState
:
117 suspended
= rf_State_IncrAccessCount(desc
);
120 suspended
= rf_State_Map(desc
);
123 suspended
= rf_State_Lock(desc
);
125 case rf_CreateDAGState
:
126 suspended
= rf_State_CreateDAG(desc
);
128 case rf_ExecuteDAGState
:
129 suspended
= rf_State_ExecuteDAG(desc
);
131 case rf_ProcessDAGState
:
132 suspended
= rf_State_ProcessDAG(desc
);
134 case rf_CleanupState
:
135 suspended
= rf_State_Cleanup(desc
);
137 case rf_DecrAccessesCountState
:
138 suspended
= rf_State_DecrAccessCount(desc
);
141 suspended
= rf_State_LastState(desc
);
145 /* after this point, we cannot dereference desc since
146 * desc may have been freed. desc is only freed in
147 * LastState, so if we renter this function or loop
148 * back up, desc should be valid. */
151 if (rf_printStatesDebug
) {
152 printf("raid%d: State: %-24s StateIndex: %3i desc: 0x%ld %s\n",
153 unit
, StateName(current_state
),
154 current_state_index
, (long) desc
,
155 suspended
? "callback scheduled" : "looping");
158 } while (!suspended
&& current_state
!= rf_LastState
);
165 rf_ContinueDagAccess(RF_DagList_t
*dagList
)
168 RF_AccTraceEntry_t
*tracerec
= &(dagList
->desc
->tracerec
);
171 RF_RaidAccessDesc_t
*desc
;
172 RF_DagHeader_t
*dag_h
;
175 desc
= dagList
->desc
;
178 timer
= tracerec
->timer
;
179 RF_ETIMER_STOP(timer
);
180 RF_ETIMER_EVAL(timer
);
181 tracerec
->specific
.user
.exec_us
= RF_ETIMER_VAL_US(timer
);
182 RF_ETIMER_START(tracerec
->timer
);
185 /* skip to dag which just finished */
186 dag_h
= dagList
->dags
;
187 for (i
= 0; i
< dagList
->numDagsDone
; i
++) {
191 /* check to see if retry is required */
192 if (dag_h
->status
== rf_rollBackward
) {
193 /* when a dag fails, mark desc status as bad and allow
194 * all other dags in the desc to execute to
195 * completion. then, free all dags and start over */
196 desc
->status
= 1; /* bad status */
198 printf("raid%d: DAG failure: %c addr 0x%lx "
199 "(%ld) nblk 0x%x (%d) buf 0x%lx state %d\n",
200 desc
->raidPtr
->raidid
, desc
->type
,
201 (long) desc
->raidAddress
,
202 (long) desc
->raidAddress
, (int) desc
->numBlocks
,
203 (int) desc
->numBlocks
,
204 (unsigned long) (desc
->bufPtr
), desc
->state
);
207 dagList
->numDagsDone
++;
208 rf_ContinueRaidAccess(desc
);
212 rf_State_LastState(RF_RaidAccessDesc_t
*desc
)
214 void (*callbackFunc
) (RF_CBParam_t
) = desc
->callbackFunc
;
215 RF_CBParam_t callbackArg
;
217 callbackArg
.p
= desc
->callbackArg
;
220 * If this is not an async request, wake up the caller
222 if (desc
->async_flag
== 0)
226 * That's all the IO for this one... unbusy the 'disk'.
229 rf_disk_unbusy(desc
);
232 * Wakeup any requests waiting to go.
235 RF_LOCK_MUTEX(((RF_Raid_t
*) desc
->raidPtr
)->mutex
);
236 ((RF_Raid_t
*) desc
->raidPtr
)->openings
++;
237 RF_UNLOCK_MUTEX(((RF_Raid_t
*) desc
->raidPtr
)->mutex
);
239 wakeup(&(desc
->raidPtr
->iodone
));
242 * The parity_map hook has to go here, because the iodone
243 * callback goes straight into the kintf layer.
245 if (desc
->raidPtr
->parity_map
!= NULL
&&
246 desc
->type
== RF_IO_TYPE_WRITE
)
247 rf_paritymap_end(desc
->raidPtr
->parity_map
,
248 desc
->raidAddress
, desc
->numBlocks
);
250 /* printf("Calling biodone on 0x%x\n",desc->bp); */
251 biodone(desc
->bp
); /* access came through ioctl */
254 callbackFunc(callbackArg
);
255 rf_FreeRaidAccDesc(desc
);
261 rf_State_IncrAccessCount(RF_RaidAccessDesc_t
*desc
)
265 raidPtr
= desc
->raidPtr
;
266 /* Bummer. We have to do this to be 100% safe w.r.t. the increment
268 RF_LOCK_MUTEX(raidPtr
->access_suspend_mutex
);
269 raidPtr
->accs_in_flight
++; /* used to detect quiescence */
270 RF_UNLOCK_MUTEX(raidPtr
->access_suspend_mutex
);
277 rf_State_DecrAccessCount(RF_RaidAccessDesc_t
*desc
)
281 raidPtr
= desc
->raidPtr
;
283 RF_LOCK_MUTEX(raidPtr
->access_suspend_mutex
);
284 raidPtr
->accs_in_flight
--;
285 if (raidPtr
->accesses_suspended
&& raidPtr
->accs_in_flight
== 0) {
286 rf_SignalQuiescenceLock(raidPtr
);
288 RF_UNLOCK_MUTEX(raidPtr
->access_suspend_mutex
);
295 rf_State_Quiesce(RF_RaidAccessDesc_t
*desc
)
298 RF_AccTraceEntry_t
*tracerec
= &desc
->tracerec
;
301 RF_CallbackDesc_t
*cb
;
303 int suspended
= RF_FALSE
;
304 int need_cb
, used_cb
;
306 raidPtr
= desc
->raidPtr
;
309 RF_ETIMER_START(timer
);
310 RF_ETIMER_START(desc
->timer
);
317 RF_LOCK_MUTEX(raidPtr
->access_suspend_mutex
);
318 /* Do an initial check to see if we might need a callback structure */
319 if (raidPtr
->accesses_suspended
) {
322 RF_UNLOCK_MUTEX(raidPtr
->access_suspend_mutex
);
325 /* create a callback if we might need it...
327 cb
= rf_AllocCallbackDesc();
330 RF_LOCK_MUTEX(raidPtr
->access_suspend_mutex
);
331 if (raidPtr
->accesses_suspended
) {
332 cb
->callbackFunc
= (void (*) (RF_CBParam_t
)) rf_ContinueRaidAccess
;
333 cb
->callbackArg
.p
= (void *) desc
;
334 cb
->next
= raidPtr
->quiesce_wait_list
;
335 raidPtr
->quiesce_wait_list
= cb
;
339 RF_UNLOCK_MUTEX(raidPtr
->access_suspend_mutex
);
341 if ((need_cb
== 1) && (used_cb
== 0)) {
342 rf_FreeCallbackDesc(cb
);
346 RF_ETIMER_STOP(timer
);
347 RF_ETIMER_EVAL(timer
);
348 tracerec
->specific
.user
.suspend_ovhd_us
+= RF_ETIMER_VAL_US(timer
);
352 if (suspended
&& rf_quiesceDebug
)
353 printf("Stalling access due to quiescence lock\n");
360 rf_State_Map(RF_RaidAccessDesc_t
*desc
)
362 RF_Raid_t
*raidPtr
= desc
->raidPtr
;
364 RF_AccTraceEntry_t
*tracerec
= &desc
->tracerec
;
367 RF_ETIMER_START(timer
);
370 if (!(desc
->asmap
= rf_MapAccess(raidPtr
, desc
->raidAddress
, desc
->numBlocks
,
371 desc
->bufPtr
, RF_DONT_REMAP
)))
375 RF_ETIMER_STOP(timer
);
376 RF_ETIMER_EVAL(timer
);
377 tracerec
->specific
.user
.map_us
= RF_ETIMER_VAL_US(timer
);
385 rf_State_Lock(RF_RaidAccessDesc_t
*desc
)
388 RF_AccTraceEntry_t
*tracerec
= &desc
->tracerec
;
391 RF_Raid_t
*raidPtr
= desc
->raidPtr
;
392 RF_AccessStripeMapHeader_t
*asmh
= desc
->asmap
;
393 RF_AccessStripeMap_t
*asm_p
;
394 RF_StripeNum_t lastStripeID
= -1;
395 int suspended
= RF_FALSE
;
398 RF_ETIMER_START(timer
);
401 /* acquire each lock that we don't already hold */
402 for (asm_p
= asmh
->stripeMap
; asm_p
; asm_p
= asm_p
->next
) {
403 RF_ASSERT(RF_IO_IS_R_OR_W(desc
->type
));
404 if (!rf_suppressLocksAndLargeWrites
&&
406 !(desc
->flags
& RF_DAG_SUPPRESS_LOCKS
) &&
407 !(asm_p
->flags
& RF_ASM_FLAGS_LOCK_TRIED
)) {
408 asm_p
->flags
|= RF_ASM_FLAGS_LOCK_TRIED
;
409 /* locks must be acquired hierarchically */
410 RF_ASSERT(asm_p
->stripeID
> lastStripeID
);
411 lastStripeID
= asm_p
->stripeID
;
413 RF_INIT_LOCK_REQ_DESC(asm_p
->lockReqDesc
, desc
->type
,
414 (void (*) (struct buf
*)) rf_ContinueRaidAccess
, desc
, asm_p
,
415 raidPtr
->Layout
.dataSectorsPerStripe
);
416 if (rf_AcquireStripeLock(raidPtr
->lockTable
, asm_p
->stripeID
,
417 &asm_p
->lockReqDesc
)) {
422 if (desc
->type
== RF_IO_TYPE_WRITE
&&
423 raidPtr
->status
== rf_rs_reconstructing
) {
424 if (!(asm_p
->flags
& RF_ASM_FLAGS_FORCE_TRIED
)) {
427 asm_p
->flags
|= RF_ASM_FLAGS_FORCE_TRIED
;
428 val
= rf_ForceOrBlockRecon(raidPtr
, asm_p
,
429 (void (*) (RF_Raid_t
*, void *)) rf_ContinueRaidAccess
, desc
);
431 asm_p
->flags
|= RF_ASM_FLAGS_RECON_BLOCKED
;
439 printf("raid%d: skipping force/block because already done, psid %ld\n",
440 desc
->raidPtr
->raidid
,
441 (long) asm_p
->stripeID
);
448 printf("raid%d: skipping force/block because not write or not under recon, psid %ld\n",
449 desc
->raidPtr
->raidid
,
450 (long) asm_p
->stripeID
);
456 RF_ETIMER_STOP(timer
);
457 RF_ETIMER_EVAL(timer
);
458 tracerec
->specific
.user
.lock_us
+= RF_ETIMER_VAL_US(timer
);
467 * the following three states create, execute, and post-process dags
468 * the error recovery unit is a single dag.
469 * by default, SelectAlgorithm creates an array of dags, one per parity stripe
470 * in some tricky cases, multiple dags per stripe are created
471 * - dags within a parity stripe are executed sequentially (arbitrary order)
472 * - dags for distinct parity stripes are executed concurrently
474 * repeat until all dags complete successfully -or- dag selection fails
477 * create dag(s) (SelectAlgorithm)
479 * execute dag (DispatchDAG)
483 * !done (RETRY - start over with new dags)
488 rf_State_CreateDAG(RF_RaidAccessDesc_t
*desc
)
491 RF_AccTraceEntry_t
*tracerec
= &desc
->tracerec
;
494 RF_DagHeader_t
*dag_h
;
495 RF_DagList_t
*dagList
;
499 /* generate a dag for the access, and fire it off. When the dag
500 * completes, we'll get re-invoked in the next state. */
502 RF_ETIMER_START(timer
);
504 /* SelectAlgorithm returns one or more dags */
505 selectStatus
= rf_SelectAlgorithm(desc
, desc
->flags
| RF_DAG_SUPPRESS_LOCKS
);
506 #if RF_DEBUG_VALIDATE_DAG
507 if (rf_printDAGsDebug
) {
508 dagList
= desc
->dagList
;
509 for (i
= 0; i
< desc
->numStripes
; i
++) {
510 rf_PrintDAGList(dagList
->dags
);
511 dagList
= dagList
->next
;
514 #endif /* RF_DEBUG_VALIDATE_DAG */
516 RF_ETIMER_STOP(timer
);
517 RF_ETIMER_EVAL(timer
);
518 /* update time to create all dags */
519 tracerec
->specific
.user
.dag_create_us
= RF_ETIMER_VAL_US(timer
);
522 desc
->status
= 0; /* good status */
524 if (selectStatus
|| (desc
->numRetries
> RF_RETRY_THRESHOLD
)) {
525 /* failed to create a dag */
526 /* this happens when there are too many faults or incomplete
529 printf("raid%d: failed to create a dag. "
530 "Too many component failures.\n",
531 desc
->raidPtr
->raidid
);
533 printf("raid%d: IO failed after %d retries.\n",
534 desc
->raidPtr
->raidid
, RF_RETRY_THRESHOLD
);
537 desc
->status
= 1; /* bad status */
538 /* skip straight to rf_State_Cleanup() */
539 desc
->state
= rf_CleanupState
;
540 bp
= (struct buf
*)desc
->bp
;
542 bp
->b_resid
= bp
->b_bcount
;
544 /* bind dags to desc */
545 dagList
= desc
->dagList
;
546 for (i
= 0; i
< desc
->numStripes
; i
++) {
547 dag_h
= dagList
->dags
;
549 dag_h
->bp
= (struct buf
*) desc
->bp
;
551 dag_h
->tracerec
= tracerec
;
555 dagList
= dagList
->next
;
557 desc
->flags
|= RF_DAG_DISPATCH_RETURNED
;
558 desc
->state
++; /* next state should be rf_State_ExecuteDAG */
565 /* the access has an list of dagLists, one dagList per parity stripe.
566 * fire the first dag in each parity stripe (dagList).
567 * dags within a stripe (dagList) must be executed sequentially
568 * - this preserves atomic parity update
569 * dags for independents parity groups (stripes) are fired concurrently */
572 rf_State_ExecuteDAG(RF_RaidAccessDesc_t
*desc
)
575 RF_DagHeader_t
*dag_h
;
576 RF_DagList_t
*dagList
;
578 /* next state is always rf_State_ProcessDAG important to do
579 * this before firing the first dag (it may finish before we
580 * leave this routine) */
583 /* sweep dag array, a stripe at a time, firing the first dag
585 dagList
= desc
->dagList
;
586 for (i
= 0; i
< desc
->numStripes
; i
++) {
587 RF_ASSERT(dagList
->numDags
> 0);
588 RF_ASSERT(dagList
->numDagsDone
== 0);
589 RF_ASSERT(dagList
->numDagsFired
== 0);
591 RF_ETIMER_START(dagList
->tracerec
.timer
);
593 /* fire first dag in this stripe */
594 dag_h
= dagList
->dags
;
596 dagList
->numDagsFired
++;
597 rf_DispatchDAG(dag_h
, (void (*) (void *)) rf_ContinueDagAccess
, dagList
);
598 dagList
= dagList
->next
;
601 /* the DAG will always call the callback, even if there was no
602 * blocking, so we are always suspended in this state */
608 /* rf_State_ProcessDAG is entered when a dag completes.
609 * first, check to all dags in the access have completed
610 * if not, fire as many dags as possible */
613 rf_State_ProcessDAG(RF_RaidAccessDesc_t
*desc
)
615 RF_AccessStripeMapHeader_t
*asmh
= desc
->asmap
;
616 RF_Raid_t
*raidPtr
= desc
->raidPtr
;
617 RF_DagHeader_t
*dag_h
;
618 int i
, j
, done
= RF_TRUE
;
619 RF_DagList_t
*dagList
, *temp
;
621 /* check to see if this is the last dag */
622 dagList
= desc
->dagList
;
623 for (i
= 0; i
< desc
->numStripes
; i
++) {
624 if (dagList
->numDags
!= dagList
->numDagsDone
)
626 dagList
= dagList
->next
;
631 /* a dag failed, retry */
633 dagList
= desc
->dagList
;
634 for (i
= 0; i
< desc
->numStripes
; i
++) {
635 rf_FreeDAG(dagList
->dags
);
637 dagList
= dagList
->next
;
638 rf_FreeDAGList(temp
);
640 desc
->dagList
= NULL
;
642 rf_MarkFailuresInASMList(raidPtr
, asmh
);
644 /* note the retry so that we'll bail in
645 rf_State_CreateDAG() once we've retired
646 the IO RF_RETRY_THRESHOLD times */
650 /* back up to rf_State_CreateDAG */
651 desc
->state
= desc
->state
- 2;
654 /* move on to rf_State_Cleanup */
659 /* more dags to execute */
660 /* see if any are ready to be fired. if so, fire them */
661 /* don't fire the initial dag in a list, it's fired in
662 * rf_State_ExecuteDAG */
663 dagList
= desc
->dagList
;
664 for (i
= 0; i
< desc
->numStripes
; i
++) {
665 if ((dagList
->numDagsDone
< dagList
->numDags
)
666 && (dagList
->numDagsDone
== dagList
->numDagsFired
)
667 && (dagList
->numDagsFired
> 0)) {
669 RF_ETIMER_START(dagList
->tracerec
.timer
);
671 /* fire next dag in this stripe */
672 /* first, skip to next dag awaiting execution */
673 dag_h
= dagList
->dags
;
674 for (j
= 0; j
< dagList
->numDagsDone
; j
++)
676 dagList
->numDagsFired
++;
677 rf_DispatchDAG(dag_h
, (void (*) (void *)) rf_ContinueDagAccess
,
680 dagList
= dagList
->next
;
685 /* only make it this far if all dags complete successfully */
687 rf_State_Cleanup(RF_RaidAccessDesc_t
*desc
)
690 RF_AccTraceEntry_t
*tracerec
= &desc
->tracerec
;
693 RF_AccessStripeMapHeader_t
*asmh
= desc
->asmap
;
694 RF_Raid_t
*raidPtr
= desc
->raidPtr
;
695 RF_AccessStripeMap_t
*asm_p
;
696 RF_DagList_t
*dagList
;
702 timer
= tracerec
->timer
;
703 RF_ETIMER_STOP(timer
);
704 RF_ETIMER_EVAL(timer
);
705 tracerec
->specific
.user
.dag_retry_us
= RF_ETIMER_VAL_US(timer
);
707 /* the RAID I/O is complete. Clean up. */
708 tracerec
->specific
.user
.dag_retry_us
= 0;
710 RF_ETIMER_START(timer
);
713 dagList
= desc
->dagList
;
714 for (i
= 0; i
< desc
->numStripes
; i
++) {
715 rf_FreeDAG(dagList
->dags
);
716 dagList
= dagList
->next
;
719 RF_ETIMER_STOP(timer
);
720 RF_ETIMER_EVAL(timer
);
721 tracerec
->specific
.user
.cleanup_us
= RF_ETIMER_VAL_US(timer
);
723 RF_ETIMER_START(timer
);
725 for (asm_p
= asmh
->stripeMap
; asm_p
; asm_p
= asm_p
->next
) {
726 if (!rf_suppressLocksAndLargeWrites
&&
728 !(desc
->flags
& RF_DAG_SUPPRESS_LOCKS
)) {
729 RF_ASSERT_VALID_LOCKREQ(&asm_p
->lockReqDesc
);
730 rf_ReleaseStripeLock(raidPtr
->lockTable
,
732 &asm_p
->lockReqDesc
);
734 if (asm_p
->flags
& RF_ASM_FLAGS_RECON_BLOCKED
) {
735 rf_UnblockRecon(raidPtr
, asm_p
);
739 RF_ETIMER_STOP(timer
);
740 RF_ETIMER_EVAL(timer
);
741 tracerec
->specific
.user
.lock_us
+= RF_ETIMER_VAL_US(timer
);
743 RF_ETIMER_START(timer
);
745 rf_FreeAccessStripeMap(asmh
);
747 RF_ETIMER_STOP(timer
);
748 RF_ETIMER_EVAL(timer
);
749 tracerec
->specific
.user
.cleanup_us
+= RF_ETIMER_VAL_US(timer
);
751 RF_ETIMER_STOP(desc
->timer
);
752 RF_ETIMER_EVAL(desc
->timer
);
754 timer
= desc
->tracerec
.tot_timer
;
755 RF_ETIMER_STOP(timer
);
756 RF_ETIMER_EVAL(timer
);
757 desc
->tracerec
.total_us
= RF_ETIMER_VAL_US(timer
);
759 rf_LogTraceRec(raidPtr
, tracerec
);
761 desc
->flags
|= RF_DAG_ACCESS_COMPLETE
;