rf_engine.c revision 1.32 1 /* $NetBSD: rf_engine.c,v 1.32 2004/02/29 04:03:50 oster Exp $ */
2 /*
3 * Copyright (c) 1995 Carnegie-Mellon University.
4 * All rights reserved.
5 *
6 * Author: William V. Courtright II, Mark Holland, Rachad Youssef
7 *
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.
13 *
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.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 */
28
29 /****************************************************************************
30 * *
31 * engine.c -- code for DAG execution engine *
32 * *
33 * Modified to work as follows (holland): *
34 * A user-thread calls into DispatchDAG, which fires off the nodes that *
35 * are direct successors to the header node. DispatchDAG then returns, *
36 * and the rest of the I/O continues asynchronously. As each node *
37 * completes, the node execution function calls FinishNode(). FinishNode *
38 * scans the list of successors to the node and increments the antecedent *
39 * counts. Each node that becomes enabled is placed on a central node *
40 * queue. A dedicated dag-execution thread grabs nodes off of this *
41 * queue and fires them. *
42 * *
43 * NULL nodes are never fired. *
44 * *
45 * Terminator nodes are never fired, but rather cause the callback *
46 * associated with the DAG to be invoked. *
47 * *
48 * If a node fails, the dag either rolls forward to the completion or *
49 * rolls back, undoing previously-completed nodes and fails atomically. *
50 * The direction of recovery is determined by the location of the failed *
51 * node in the graph. If the failure occurred before the commit node in *
52 * the graph, backward recovery is used. Otherwise, forward recovery is *
53 * used. *
54 * *
55 ****************************************************************************/
56
57 #include <sys/cdefs.h>
58 __KERNEL_RCSID(0, "$NetBSD: rf_engine.c,v 1.32 2004/02/29 04:03:50 oster Exp $");
59
60 #include <sys/errno.h>
61
62 #include "rf_threadstuff.h"
63 #include "rf_dag.h"
64 #include "rf_engine.h"
65 #include "rf_etimer.h"
66 #include "rf_general.h"
67 #include "rf_dagutils.h"
68 #include "rf_shutdown.h"
69 #include "rf_raid.h"
70
71 static void rf_ShutdownEngine(void *);
72 static void DAGExecutionThread(RF_ThreadArg_t arg);
73 static void rf_RaidIOThread(RF_ThreadArg_t arg);
74
75 /* synchronization primitives for this file. DO_WAIT should be enclosed in a while loop. */
76
77 #define DO_LOCK(_r_) \
78 do { \
79 ks = splbio(); \
80 RF_LOCK_MUTEX((_r_)->node_queue_mutex); \
81 } while (0)
82
83 #define DO_UNLOCK(_r_) \
84 do { \
85 RF_UNLOCK_MUTEX((_r_)->node_queue_mutex); \
86 splx(ks); \
87 } while (0)
88
89 #define DO_WAIT(_r_) \
90 RF_WAIT_COND((_r_)->node_queue, (_r_)->node_queue_mutex)
91
92 #define DO_SIGNAL(_r_) \
93 RF_BROADCAST_COND((_r_)->node_queue) /* XXX RF_SIGNAL_COND? */
94
95 static void
96 rf_ShutdownEngine(void *arg)
97 {
98 RF_Raid_t *raidPtr;
99 int ks;
100
101 raidPtr = (RF_Raid_t *) arg;
102
103 /* Tell the rf_RaidIOThread to shutdown */
104 simple_lock(&(raidPtr->iodone_lock));
105
106 raidPtr->shutdown_raidio = 1;
107 wakeup(&(raidPtr->iodone));
108
109 /* ...and wait for it to tell us it has finished */
110 while (raidPtr->shutdown_raidio)
111 ltsleep(&(raidPtr->shutdown_raidio), PRIBIO, "raidshutdown", 0,
112 &(raidPtr->iodone_lock));
113
114 simple_unlock(&(raidPtr->iodone_lock));
115
116 /* Now shut down the DAG execution engine. */
117 DO_LOCK(raidPtr);
118 raidPtr->shutdown_engine = 1;
119 DO_SIGNAL(raidPtr);
120 DO_UNLOCK(raidPtr);
121
122 }
123
124 int
125 rf_ConfigureEngine(RF_ShutdownList_t **listp, RF_Raid_t *raidPtr,
126 RF_Config_t *cfgPtr)
127 {
128
129 rf_mutex_init(&raidPtr->node_queue_mutex);
130 raidPtr->node_queue = NULL;
131 raidPtr->dags_in_flight = 0;
132
133 /* we create the execution thread only once per system boot. no need
134 * to check return code b/c the kernel panics if it can't create the
135 * thread. */
136 if (rf_engineDebug) {
137 printf("raid%d: Creating engine thread\n", raidPtr->raidid);
138 }
139 if (RF_CREATE_ENGINE_THREAD(raidPtr->engine_thread,
140 DAGExecutionThread, raidPtr,
141 "raid%d", raidPtr->raidid)) {
142 printf("raid%d: Unable to create engine thread\n",
143 raidPtr->raidid);
144 return (ENOMEM);
145 }
146 if (RF_CREATE_ENGINE_THREAD(raidPtr->engine_helper_thread,
147 rf_RaidIOThread, raidPtr,
148 "raidio%d", raidPtr->raidid)) {
149 printf("raid%d: Unable to create raidio thread\n",
150 raidPtr->raidid);
151 return (ENOMEM);
152 }
153 if (rf_engineDebug) {
154 printf("raid%d: Created engine thread\n", raidPtr->raidid);
155 }
156
157 /* engine thread is now running and waiting for work */
158 if (rf_engineDebug) {
159 printf("raid%d: Engine thread running and waiting for events\n", raidPtr->raidid);
160 }
161 rf_ShutdownCreate(listp, rf_ShutdownEngine, raidPtr);
162
163 return (0);
164 }
165
166 static int
167 BranchDone(RF_DagNode_t *node)
168 {
169 int i;
170
171 /* return true if forward execution is completed for a node and it's
172 * succedents */
173 switch (node->status) {
174 case rf_wait:
175 /* should never be called in this state */
176 RF_PANIC();
177 break;
178 case rf_fired:
179 /* node is currently executing, so we're not done */
180 return (RF_FALSE);
181 case rf_good:
182 /* for each succedent recursively check branch */
183 for (i = 0; i < node->numSuccedents; i++)
184 if (!BranchDone(node->succedents[i]))
185 return RF_FALSE;
186 return RF_TRUE; /* node and all succedent branches aren't in
187 * fired state */
188 case rf_bad:
189 /* succedents can't fire */
190 return (RF_TRUE);
191 case rf_recover:
192 /* should never be called in this state */
193 RF_PANIC();
194 break;
195 case rf_undone:
196 case rf_panic:
197 /* XXX need to fix this case */
198 /* for now, assume that we're done */
199 return (RF_TRUE);
200 default:
201 /* illegal node status */
202 RF_PANIC();
203 break;
204 }
205 }
206
207 static int
208 NodeReady(RF_DagNode_t *node)
209 {
210 int ready;
211
212 switch (node->dagHdr->status) {
213 case rf_enable:
214 case rf_rollForward:
215 if ((node->status == rf_wait) &&
216 (node->numAntecedents == node->numAntDone))
217 ready = RF_TRUE;
218 else
219 ready = RF_FALSE;
220 break;
221 case rf_rollBackward:
222 RF_ASSERT(node->numSuccDone <= node->numSuccedents);
223 RF_ASSERT(node->numSuccFired <= node->numSuccedents);
224 RF_ASSERT(node->numSuccFired <= node->numSuccDone);
225 if ((node->status == rf_good) &&
226 (node->numSuccDone == node->numSuccedents))
227 ready = RF_TRUE;
228 else
229 ready = RF_FALSE;
230 break;
231 default:
232 printf("Execution engine found illegal DAG status in NodeReady\n");
233 RF_PANIC();
234 break;
235 }
236
237 return (ready);
238 }
239
240
241
242 /* user context and dag-exec-thread context: Fire a node. The node's
243 * status field determines which function, do or undo, to be fired.
244 * This routine assumes that the node's status field has alread been
245 * set to "fired" or "recover" to indicate the direction of execution.
246 */
247 static void
248 FireNode(RF_DagNode_t *node)
249 {
250 switch (node->status) {
251 case rf_fired:
252 /* fire the do function of a node */
253 if (rf_engineDebug) {
254 printf("raid%d: Firing node 0x%lx (%s)\n",
255 node->dagHdr->raidPtr->raidid,
256 (unsigned long) node, node->name);
257 }
258 if (node->flags & RF_DAGNODE_FLAG_YIELD) {
259 #if defined(__NetBSD__) && defined(_KERNEL)
260 /* thread_block(); */
261 /* printf("Need to block the thread here...\n"); */
262 /* XXX thread_block is actually mentioned in
263 * /usr/include/vm/vm_extern.h */
264 #else
265 thread_block();
266 #endif
267 }
268 (*(node->doFunc)) (node);
269 break;
270 case rf_recover:
271 /* fire the undo function of a node */
272 if (rf_engineDebug) {
273 printf("raid%d: Firing (undo) node 0x%lx (%s)\n",
274 node->dagHdr->raidPtr->raidid,
275 (unsigned long) node, node->name);
276 }
277 if (node->flags & RF_DAGNODE_FLAG_YIELD)
278 #if defined(__NetBSD__) && defined(_KERNEL)
279 /* thread_block(); */
280 /* printf("Need to block the thread here...\n"); */
281 /* XXX thread_block is actually mentioned in
282 * /usr/include/vm/vm_extern.h */
283 #else
284 thread_block();
285 #endif
286 (*(node->undoFunc)) (node);
287 break;
288 default:
289 RF_PANIC();
290 break;
291 }
292 }
293
294
295
296 /* user context:
297 * Attempt to fire each node in a linear array.
298 * The entire list is fired atomically.
299 */
300 static void
301 FireNodeArray(int numNodes, RF_DagNode_t **nodeList)
302 {
303 RF_DagStatus_t dstat;
304 RF_DagNode_t *node;
305 int i, j;
306
307 /* first, mark all nodes which are ready to be fired */
308 for (i = 0; i < numNodes; i++) {
309 node = nodeList[i];
310 dstat = node->dagHdr->status;
311 RF_ASSERT((node->status == rf_wait) ||
312 (node->status == rf_good));
313 if (NodeReady(node)) {
314 if ((dstat == rf_enable) ||
315 (dstat == rf_rollForward)) {
316 RF_ASSERT(node->status == rf_wait);
317 if (node->commitNode)
318 node->dagHdr->numCommits++;
319 node->status = rf_fired;
320 for (j = 0; j < node->numAntecedents; j++)
321 node->antecedents[j]->numSuccFired++;
322 } else {
323 RF_ASSERT(dstat == rf_rollBackward);
324 RF_ASSERT(node->status == rf_good);
325 /* only one commit node per graph */
326 RF_ASSERT(node->commitNode == RF_FALSE);
327 node->status = rf_recover;
328 }
329 }
330 }
331 /* now, fire the nodes */
332 for (i = 0; i < numNodes; i++) {
333 if ((nodeList[i]->status == rf_fired) ||
334 (nodeList[i]->status == rf_recover))
335 FireNode(nodeList[i]);
336 }
337 }
338
339
340 /* user context:
341 * Attempt to fire each node in a linked list.
342 * The entire list is fired atomically.
343 */
344 static void
345 FireNodeList(RF_DagNode_t *nodeList)
346 {
347 RF_DagNode_t *node, *next;
348 RF_DagStatus_t dstat;
349 int j;
350
351 if (nodeList) {
352 /* first, mark all nodes which are ready to be fired */
353 for (node = nodeList; node; node = next) {
354 next = node->next;
355 dstat = node->dagHdr->status;
356 RF_ASSERT((node->status == rf_wait) ||
357 (node->status == rf_good));
358 if (NodeReady(node)) {
359 if ((dstat == rf_enable) ||
360 (dstat == rf_rollForward)) {
361 RF_ASSERT(node->status == rf_wait);
362 if (node->commitNode)
363 node->dagHdr->numCommits++;
364 node->status = rf_fired;
365 for (j = 0; j < node->numAntecedents; j++)
366 node->antecedents[j]->numSuccFired++;
367 } else {
368 RF_ASSERT(dstat == rf_rollBackward);
369 RF_ASSERT(node->status == rf_good);
370 /* only one commit node per graph */
371 RF_ASSERT(node->commitNode == RF_FALSE);
372 node->status = rf_recover;
373 }
374 }
375 }
376 /* now, fire the nodes */
377 for (node = nodeList; node; node = next) {
378 next = node->next;
379 if ((node->status == rf_fired) ||
380 (node->status == rf_recover))
381 FireNode(node);
382 }
383 }
384 }
385 /* interrupt context:
386 * for each succedent
387 * propagate required results from node to succedent
388 * increment succedent's numAntDone
389 * place newly-enable nodes on node queue for firing
390 *
391 * To save context switches, we don't place NIL nodes on the node queue,
392 * but rather just process them as if they had fired. Note that NIL nodes
393 * that are the direct successors of the header will actually get fired by
394 * DispatchDAG, which is fine because no context switches are involved.
395 *
396 * Important: when running at user level, this can be called by any
397 * disk thread, and so the increment and check of the antecedent count
398 * must be locked. I used the node queue mutex and locked down the
399 * entire function, but this is certainly overkill.
400 */
401 static void
402 PropagateResults(RF_DagNode_t *node, int context)
403 {
404 RF_DagNode_t *s, *a;
405 RF_Raid_t *raidPtr;
406 int i, ks;
407 RF_DagNode_t *finishlist = NULL; /* a list of NIL nodes to be
408 * finished */
409 RF_DagNode_t *skiplist = NULL; /* list of nodes with failed truedata
410 * antecedents */
411 RF_DagNode_t *firelist = NULL; /* a list of nodes to be fired */
412 RF_DagNode_t *q = NULL, *qh = NULL, *next;
413 int j, skipNode;
414
415 raidPtr = node->dagHdr->raidPtr;
416
417 DO_LOCK(raidPtr);
418
419 /* debug - validate fire counts */
420 for (i = 0; i < node->numAntecedents; i++) {
421 a = *(node->antecedents + i);
422 RF_ASSERT(a->numSuccFired >= a->numSuccDone);
423 RF_ASSERT(a->numSuccFired <= a->numSuccedents);
424 a->numSuccDone++;
425 }
426
427 switch (node->dagHdr->status) {
428 case rf_enable:
429 case rf_rollForward:
430 for (i = 0; i < node->numSuccedents; i++) {
431 s = *(node->succedents + i);
432 RF_ASSERT(s->status == rf_wait);
433 (s->numAntDone)++;
434 if (s->numAntDone == s->numAntecedents) {
435 /* look for NIL nodes */
436 if (s->doFunc == rf_NullNodeFunc) {
437 /* don't fire NIL nodes, just process
438 * them */
439 s->next = finishlist;
440 finishlist = s;
441 } else {
442 /* look to see if the node is to be
443 * skipped */
444 skipNode = RF_FALSE;
445 for (j = 0; j < s->numAntecedents; j++)
446 if ((s->antType[j] == rf_trueData) && (s->antecedents[j]->status == rf_bad))
447 skipNode = RF_TRUE;
448 if (skipNode) {
449 /* this node has one or more
450 * failed true data
451 * dependencies, so skip it */
452 s->next = skiplist;
453 skiplist = s;
454 } else
455 /* add s to list of nodes (q)
456 * to execute */
457 if (context != RF_INTR_CONTEXT) {
458 /* we only have to
459 * enqueue if we're at
460 * intr context */
461 /* put node on
462 a list to
463 be fired
464 after we
465 unlock */
466 s->next = firelist;
467 firelist = s;
468 } else {
469 /* enqueue the
470 node for
471 the dag
472 exec thread
473 to fire */
474 RF_ASSERT(NodeReady(s));
475 if (q) {
476 q->next = s;
477 q = s;
478 } else {
479 qh = q = s;
480 qh->next = NULL;
481 }
482 }
483 }
484 }
485 }
486
487 if (q) {
488 /* xfer our local list of nodes to the node queue */
489 q->next = raidPtr->node_queue;
490 raidPtr->node_queue = qh;
491 DO_SIGNAL(raidPtr);
492 }
493 DO_UNLOCK(raidPtr);
494
495 for (; skiplist; skiplist = next) {
496 next = skiplist->next;
497 skiplist->status = rf_skipped;
498 for (i = 0; i < skiplist->numAntecedents; i++) {
499 skiplist->antecedents[i]->numSuccFired++;
500 }
501 if (skiplist->commitNode) {
502 skiplist->dagHdr->numCommits++;
503 }
504 rf_FinishNode(skiplist, context);
505 }
506 for (; finishlist; finishlist = next) {
507 /* NIL nodes: no need to fire them */
508 next = finishlist->next;
509 finishlist->status = rf_good;
510 for (i = 0; i < finishlist->numAntecedents; i++) {
511 finishlist->antecedents[i]->numSuccFired++;
512 }
513 if (finishlist->commitNode)
514 finishlist->dagHdr->numCommits++;
515 /*
516 * Okay, here we're calling rf_FinishNode() on
517 * nodes that have the null function as their
518 * work proc. Such a node could be the
519 * terminal node in a DAG. If so, it will
520 * cause the DAG to complete, which will in
521 * turn free memory used by the DAG, which
522 * includes the node in question. Thus, we
523 * must avoid referencing the node at all
524 * after calling rf_FinishNode() on it. */
525 rf_FinishNode(finishlist, context); /* recursive call */
526 }
527 /* fire all nodes in firelist */
528 FireNodeList(firelist);
529 break;
530
531 case rf_rollBackward:
532 for (i = 0; i < node->numAntecedents; i++) {
533 a = *(node->antecedents + i);
534 RF_ASSERT(a->status == rf_good);
535 RF_ASSERT(a->numSuccDone <= a->numSuccedents);
536 RF_ASSERT(a->numSuccDone <= a->numSuccFired);
537
538 if (a->numSuccDone == a->numSuccFired) {
539 if (a->undoFunc == rf_NullNodeFunc) {
540 /* don't fire NIL nodes, just process
541 * them */
542 a->next = finishlist;
543 finishlist = a;
544 } else {
545 if (context != RF_INTR_CONTEXT) {
546 /* we only have to enqueue if
547 * we're at intr context */
548 /* put node on a list to be
549 fired after we unlock */
550 a->next = firelist;
551
552 firelist = a;
553 } else {
554 /* enqueue the node for the
555 dag exec thread to fire */
556 RF_ASSERT(NodeReady(a));
557 if (q) {
558 q->next = a;
559 q = a;
560 } else {
561 qh = q = a;
562 qh->next = NULL;
563 }
564 }
565 }
566 }
567 }
568 if (q) {
569 /* xfer our local list of nodes to the node queue */
570 q->next = raidPtr->node_queue;
571 raidPtr->node_queue = qh;
572 DO_SIGNAL(raidPtr);
573 }
574 DO_UNLOCK(raidPtr);
575 for (; finishlist; finishlist = next) {
576 /* NIL nodes: no need to fire them */
577 next = finishlist->next;
578 finishlist->status = rf_good;
579 /*
580 * Okay, here we're calling rf_FinishNode() on
581 * nodes that have the null function as their
582 * work proc. Such a node could be the first
583 * node in a DAG. If so, it will cause the DAG
584 * to complete, which will in turn free memory
585 * used by the DAG, which includes the node in
586 * question. Thus, we must avoid referencing
587 * the node at all after calling
588 * rf_FinishNode() on it. */
589 rf_FinishNode(finishlist, context); /* recursive call */
590 }
591 /* fire all nodes in firelist */
592 FireNodeList(firelist);
593
594 break;
595 default:
596 printf("Engine found illegal DAG status in PropagateResults()\n");
597 RF_PANIC();
598 break;
599 }
600 }
601
602
603
604 /*
605 * Process a fired node which has completed
606 */
607 static void
608 ProcessNode(RF_DagNode_t *node, int context)
609 {
610 RF_Raid_t *raidPtr;
611
612 raidPtr = node->dagHdr->raidPtr;
613
614 switch (node->status) {
615 case rf_good:
616 /* normal case, don't need to do anything */
617 break;
618 case rf_bad:
619 if ((node->dagHdr->numCommits > 0) ||
620 (node->dagHdr->numCommitNodes == 0)) {
621 /* crossed commit barrier */
622 node->dagHdr->status = rf_rollForward;
623 if (rf_engineDebug) {
624 printf("raid%d: node (%s) returned fail, rolling forward\n", raidPtr->raidid, node->name);
625 }
626 } else {
627 /* never reached commit barrier */
628 node->dagHdr->status = rf_rollBackward;
629 if (rf_engineDebug) {
630 printf("raid%d: node (%s) returned fail, rolling backward\n", raidPtr->raidid, node->name);
631 }
632 }
633 break;
634 case rf_undone:
635 /* normal rollBackward case, don't need to do anything */
636 break;
637 case rf_panic:
638 /* an undo node failed!!! */
639 printf("UNDO of a node failed!!!/n");
640 break;
641 default:
642 printf("node finished execution with an illegal status!!!\n");
643 RF_PANIC();
644 break;
645 }
646
647 /* enqueue node's succedents (antecedents if rollBackward) for
648 * execution */
649 PropagateResults(node, context);
650 }
651
652
653
654 /* user context or dag-exec-thread context:
655 * This is the first step in post-processing a newly-completed node.
656 * This routine is called by each node execution function to mark the node
657 * as complete and fire off any successors that have been enabled.
658 */
659 int
660 rf_FinishNode(RF_DagNode_t *node, int context)
661 {
662 int retcode = RF_FALSE;
663 node->dagHdr->numNodesCompleted++;
664 ProcessNode(node, context);
665
666 return (retcode);
667 }
668
669
670 /* user context: submit dag for execution, return non-zero if we have
671 * to wait for completion. if and only if we return non-zero, we'll
672 * cause cbFunc to get invoked with cbArg when the DAG has completed.
673 *
674 * for now we always return 1. If the DAG does not cause any I/O,
675 * then the callback may get invoked before DispatchDAG returns.
676 * There's code in state 5 of ContinueRaidAccess to handle this.
677 *
678 * All we do here is fire the direct successors of the header node.
679 * The DAG execution thread does the rest of the dag processing. */
680 int
681 rf_DispatchDAG(RF_DagHeader_t *dag, void (*cbFunc) (void *),
682 void *cbArg)
683 {
684 RF_Raid_t *raidPtr;
685
686 raidPtr = dag->raidPtr;
687 if (dag->tracerec) {
688 RF_ETIMER_START(dag->tracerec->timer);
689 }
690 #if DEBUG
691 #if RF_DEBUG_VALIDATE_DAG
692 if (rf_engineDebug || rf_validateDAGDebug) {
693 if (rf_ValidateDAG(dag))
694 RF_PANIC();
695 }
696 #endif
697 #endif
698 if (rf_engineDebug) {
699 printf("raid%d: Entering DispatchDAG\n", raidPtr->raidid);
700 }
701 raidPtr->dags_in_flight++; /* debug only: blow off proper
702 * locking */
703 dag->cbFunc = cbFunc;
704 dag->cbArg = cbArg;
705 dag->numNodesCompleted = 0;
706 dag->status = rf_enable;
707 FireNodeArray(dag->numSuccedents, dag->succedents);
708 return (1);
709 }
710 /* dedicated kernel thread: the thread that handles all DAG node
711 * firing. To minimize locking and unlocking, we grab a copy of the
712 * entire node queue and then set the node queue to NULL before doing
713 * any firing of nodes. This way we only have to release the lock
714 * once. Of course, it's probably rare that there's more than one
715 * node in the queue at any one time, but it sometimes happens.
716 */
717
718 static void
719 DAGExecutionThread(RF_ThreadArg_t arg)
720 {
721 RF_DagNode_t *nd, *local_nq, *term_nq, *fire_nq;
722 RF_Raid_t *raidPtr;
723 int ks;
724 int s;
725
726 raidPtr = (RF_Raid_t *) arg;
727
728 if (rf_engineDebug) {
729 printf("raid%d: Engine thread is running\n", raidPtr->raidid);
730 }
731
732 s = splbio();
733
734 DO_LOCK(raidPtr);
735 while (!raidPtr->shutdown_engine) {
736
737 while (raidPtr->node_queue != NULL) {
738 local_nq = raidPtr->node_queue;
739 fire_nq = NULL;
740 term_nq = NULL;
741 raidPtr->node_queue = NULL;
742 DO_UNLOCK(raidPtr);
743
744 /* first, strip out the terminal nodes */
745 while (local_nq) {
746 nd = local_nq;
747 local_nq = local_nq->next;
748 switch (nd->dagHdr->status) {
749 case rf_enable:
750 case rf_rollForward:
751 if (nd->numSuccedents == 0) {
752 /* end of the dag, add to
753 * callback list */
754 nd->next = term_nq;
755 term_nq = nd;
756 } else {
757 /* not the end, add to the
758 * fire queue */
759 nd->next = fire_nq;
760 fire_nq = nd;
761 }
762 break;
763 case rf_rollBackward:
764 if (nd->numAntecedents == 0) {
765 /* end of the dag, add to the
766 * callback list */
767 nd->next = term_nq;
768 term_nq = nd;
769 } else {
770 /* not the end, add to the
771 * fire queue */
772 nd->next = fire_nq;
773 fire_nq = nd;
774 }
775 break;
776 default:
777 RF_PANIC();
778 break;
779 }
780 }
781
782 /* execute callback of dags which have reached the
783 * terminal node */
784 while (term_nq) {
785 nd = term_nq;
786 term_nq = term_nq->next;
787 nd->next = NULL;
788 (nd->dagHdr->cbFunc) (nd->dagHdr->cbArg);
789 raidPtr->dags_in_flight--; /* debug only */
790 }
791
792 /* fire remaining nodes */
793 FireNodeList(fire_nq);
794
795 DO_LOCK(raidPtr);
796 }
797 while (!raidPtr->shutdown_engine &&
798 raidPtr->node_queue == NULL) {
799 DO_WAIT(raidPtr);
800 }
801 }
802 DO_UNLOCK(raidPtr);
803
804 splx(s);
805 kthread_exit(0);
806 }
807
808 /*
809 * rf_RaidIOThread() -- When I/O to a component completes,
810 * KernelWakeupFunc() puts the completed request onto raidPtr->iodone
811 * TAILQ. This function looks after requests on that queue by calling
812 * rf_DiskIOComplete() for the request, and by calling any required
813 * CompleteFunc for the request.
814 */
815
816 static void
817 rf_RaidIOThread(RF_ThreadArg_t arg)
818 {
819 RF_Raid_t *raidPtr;
820 RF_DiskQueueData_t *req;
821 int s;
822
823 raidPtr = (RF_Raid_t *) arg;
824
825 s = splbio();
826 simple_lock(&(raidPtr->iodone_lock));
827
828 while (!raidPtr->shutdown_raidio) {
829 /* if there is nothing to do, then snooze. */
830 if (TAILQ_EMPTY(&(raidPtr->iodone))) {
831 ltsleep(&(raidPtr->iodone), PRIBIO, "raidiow", 0,
832 &(raidPtr->iodone_lock));
833 }
834
835 /* See what I/Os, if any, have arrived */
836 while ((req = TAILQ_FIRST(&(raidPtr->iodone))) != NULL) {
837 TAILQ_REMOVE(&(raidPtr->iodone), req, iodone_entries);
838 simple_unlock(&(raidPtr->iodone_lock));
839 rf_DiskIOComplete(req->queue, req, req->error);
840 (req->CompleteFunc) (req->argument, req->error);
841 simple_lock(&(raidPtr->iodone_lock));
842 }
843 }
844
845 /* Let rf_ShutdownEngine know that we're done... */
846 raidPtr->shutdown_raidio = 0;
847 wakeup(&(raidPtr->shutdown_raidio));
848
849 simple_unlock(&(raidPtr->iodone_lock));
850 splx(s);
851
852 kthread_exit(0);
853 }
854