rf_engine.c revision 1.18 1 /* $NetBSD: rf_engine.c,v 1.18 2002/09/19 22:36:23 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.18 2002/09/19 22:36:23 oster Exp $");
59
60 #include "rf_threadstuff.h"
61
62 #include <sys/errno.h>
63
64 #include "rf_dag.h"
65 #include "rf_engine.h"
66 #include "rf_etimer.h"
67 #include "rf_general.h"
68 #include "rf_dagutils.h"
69 #include "rf_shutdown.h"
70 #include "rf_raid.h"
71
72 static void DAGExecutionThread(RF_ThreadArg_t arg);
73
74 #define DO_INIT(_l_,_r_) { \
75 int _rc; \
76 _rc = rf_create_managed_mutex(_l_,&(_r_)->node_queue_mutex); \
77 if (_rc) { \
78 return(_rc); \
79 } \
80 _rc = rf_create_managed_cond(_l_,&(_r_)->node_queue_cond); \
81 if (_rc) { \
82 return(_rc); \
83 } \
84 }
85
86 /* synchronization primitives for this file. DO_WAIT should be enclosed in a while loop. */
87
88 #define DO_LOCK(_r_) \
89 do { \
90 ks = splbio(); \
91 RF_LOCK_MUTEX((_r_)->node_queue_mutex); \
92 } while (0)
93
94 #define DO_UNLOCK(_r_) \
95 do { \
96 RF_UNLOCK_MUTEX((_r_)->node_queue_mutex); \
97 splx(ks); \
98 } while (0)
99
100 #define DO_WAIT(_r_) \
101 RF_WAIT_COND((_r_)->node_queue, (_r_)->node_queue_mutex)
102
103 #define DO_SIGNAL(_r_) \
104 RF_BROADCAST_COND((_r_)->node_queue) /* XXX RF_SIGNAL_COND? */
105
106 static void rf_ShutdownEngine(void *);
107
108 static void
109 rf_ShutdownEngine(arg)
110 void *arg;
111 {
112 RF_Raid_t *raidPtr;
113
114 raidPtr = (RF_Raid_t *) arg;
115 raidPtr->shutdown_engine = 1;
116 DO_SIGNAL(raidPtr);
117 }
118
119 int
120 rf_ConfigureEngine(
121 RF_ShutdownList_t ** listp,
122 RF_Raid_t * raidPtr,
123 RF_Config_t * cfgPtr)
124 {
125 int rc;
126
127 DO_INIT(listp, raidPtr);
128
129 raidPtr->node_queue = NULL;
130 raidPtr->dags_in_flight = 0;
131
132 rc = rf_init_managed_threadgroup(listp, &raidPtr->engine_tg);
133 if (rc)
134 return (rc);
135
136 /* we create the execution thread only once per system boot. no need
137 * to check return code b/c the kernel panics if it can't create the
138 * thread. */
139 if (rf_engineDebug) {
140 printf("raid%d: Creating engine thread\n", raidPtr->raidid);
141 }
142 if (RF_CREATE_THREAD(raidPtr->engine_thread, DAGExecutionThread, raidPtr,"raid")) {
143 RF_ERRORMSG("RAIDFRAME: Unable to create engine thread\n");
144 return (ENOMEM);
145 }
146 if (rf_engineDebug) {
147 printf("raid%d: Created engine thread\n", raidPtr->raidid);
148 }
149 RF_THREADGROUP_STARTED(&raidPtr->engine_tg);
150 /* XXX something is missing here... */
151 #ifdef debug
152 printf("Skipping the WAIT_START!!\n");
153 #endif
154 #if 0
155 RF_THREADGROUP_WAIT_START(&raidPtr->engine_tg);
156 #endif
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 rc = rf_ShutdownCreate(listp, rf_ShutdownEngine, raidPtr);
162 if (rc) {
163 rf_print_unable_to_add_shutdown(__FILE__, __LINE__, rc);
164 rf_ShutdownEngine(NULL);
165 }
166 return (rc);
167 }
168
169 static int
170 BranchDone(RF_DagNode_t * node)
171 {
172 int i;
173
174 /* return true if forward execution is completed for a node and it's
175 * succedents */
176 switch (node->status) {
177 case rf_wait:
178 /* should never be called in this state */
179 RF_PANIC();
180 break;
181 case rf_fired:
182 /* node is currently executing, so we're not done */
183 return (RF_FALSE);
184 case rf_good:
185 for (i = 0; i < node->numSuccedents; i++) /* for each succedent */
186 if (!BranchDone(node->succedents[i])) /* recursively check
187 * branch */
188 return RF_FALSE;
189 return RF_TRUE; /* node and all succedent branches aren't in
190 * fired state */
191 break;
192 case rf_bad:
193 /* succedents can't fire */
194 return (RF_TRUE);
195 case rf_recover:
196 /* should never be called in this state */
197 RF_PANIC();
198 break;
199 case rf_undone:
200 case rf_panic:
201 /* XXX need to fix this case */
202 /* for now, assume that we're done */
203 return (RF_TRUE);
204 break;
205 default:
206 /* illegal node status */
207 RF_PANIC();
208 break;
209 }
210 }
211
212 static int
213 NodeReady(RF_DagNode_t * node)
214 {
215 int ready;
216
217 switch (node->dagHdr->status) {
218 case rf_enable:
219 case rf_rollForward:
220 if ((node->status == rf_wait) && (node->numAntecedents == node->numAntDone))
221 ready = RF_TRUE;
222 else
223 ready = RF_FALSE;
224 break;
225 case rf_rollBackward:
226 RF_ASSERT(node->numSuccDone <= node->numSuccedents);
227 RF_ASSERT(node->numSuccFired <= node->numSuccedents);
228 RF_ASSERT(node->numSuccFired <= node->numSuccDone);
229 if ((node->status == rf_good) && (node->numSuccDone == node->numSuccedents))
230 ready = RF_TRUE;
231 else
232 ready = RF_FALSE;
233 break;
234 default:
235 printf("Execution engine found illegal DAG status in NodeReady\n");
236 RF_PANIC();
237 break;
238 }
239
240 return (ready);
241 }
242
243
244
245 /* user context and dag-exec-thread context:
246 * Fire a node. The node's status field determines which function, do or undo,
247 * to be fired.
248 * This routine assumes that the node's status field has alread been set to
249 * "fired" or "recover" to indicate the direction of execution.
250 */
251 static void
252 FireNode(RF_DagNode_t * node)
253 {
254 switch (node->status) {
255 case rf_fired:
256 /* fire the do function of a node */
257 if (rf_engineDebug) {
258 printf("raid%d: Firing node 0x%lx (%s)\n",
259 node->dagHdr->raidPtr->raidid,
260 (unsigned long) node, node->name);
261 }
262 if (node->flags & RF_DAGNODE_FLAG_YIELD) {
263 #if defined(__NetBSD__) && defined(_KERNEL)
264 /* thread_block(); */
265 /* printf("Need to block the thread here...\n"); */
266 /* XXX thread_block is actually mentioned in
267 * /usr/include/vm/vm_extern.h */
268 #else
269 thread_block();
270 #endif
271 }
272 (*(node->doFunc)) (node);
273 break;
274 case rf_recover:
275 /* fire the undo function of a node */
276 if (rf_engineDebug) {
277 printf("raid%d: Firing (undo) node 0x%lx (%s)\n",
278 node->dagHdr->raidPtr->raidid,
279 (unsigned long) node, node->name);
280 }
281 if (node->flags & RF_DAGNODE_FLAG_YIELD)
282 #if defined(__NetBSD__) && defined(_KERNEL)
283 /* thread_block(); */
284 /* printf("Need to block the thread here...\n"); */
285 /* XXX thread_block is actually mentioned in
286 * /usr/include/vm/vm_extern.h */
287 #else
288 thread_block();
289 #endif
290 (*(node->undoFunc)) (node);
291 break;
292 default:
293 RF_PANIC();
294 break;
295 }
296 }
297
298
299
300 /* user context:
301 * Attempt to fire each node in a linear array.
302 * The entire list is fired atomically.
303 */
304 static void
305 FireNodeArray(
306 int numNodes,
307 RF_DagNode_t ** nodeList)
308 {
309 RF_DagStatus_t dstat;
310 RF_DagNode_t *node;
311 int i, j;
312
313 /* first, mark all nodes which are ready to be fired */
314 for (i = 0; i < numNodes; i++) {
315 node = nodeList[i];
316 dstat = node->dagHdr->status;
317 RF_ASSERT((node->status == rf_wait) || (node->status == rf_good));
318 if (NodeReady(node)) {
319 if ((dstat == rf_enable) || (dstat == rf_rollForward)) {
320 RF_ASSERT(node->status == rf_wait);
321 if (node->commitNode)
322 node->dagHdr->numCommits++;
323 node->status = rf_fired;
324 for (j = 0; j < node->numAntecedents; j++)
325 node->antecedents[j]->numSuccFired++;
326 } else {
327 RF_ASSERT(dstat == rf_rollBackward);
328 RF_ASSERT(node->status == rf_good);
329 RF_ASSERT(node->commitNode == RF_FALSE); /* only one commit node
330 * per graph */
331 node->status = rf_recover;
332 }
333 }
334 }
335 /* now, fire the nodes */
336 for (i = 0; i < numNodes; i++) {
337 if ((nodeList[i]->status == rf_fired) || (nodeList[i]->status == rf_recover))
338 FireNode(nodeList[i]);
339 }
340 }
341
342
343 /* user context:
344 * Attempt to fire each node in a linked list.
345 * The entire list is fired atomically.
346 */
347 static void
348 FireNodeList(RF_DagNode_t * nodeList)
349 {
350 RF_DagNode_t *node, *next;
351 RF_DagStatus_t dstat;
352 int j;
353
354 if (nodeList) {
355 /* first, mark all nodes which are ready to be fired */
356 for (node = nodeList; node; node = next) {
357 next = node->next;
358 dstat = node->dagHdr->status;
359 RF_ASSERT((node->status == rf_wait) || (node->status == rf_good));
360 if (NodeReady(node)) {
361 if ((dstat == rf_enable) || (dstat == rf_rollForward)) {
362 RF_ASSERT(node->status == rf_wait);
363 if (node->commitNode)
364 node->dagHdr->numCommits++;
365 node->status = rf_fired;
366 for (j = 0; j < node->numAntecedents; j++)
367 node->antecedents[j]->numSuccFired++;
368 } else {
369 RF_ASSERT(dstat == rf_rollBackward);
370 RF_ASSERT(node->status == rf_good);
371 RF_ASSERT(node->commitNode == RF_FALSE); /* only one commit node
372 * per graph */
373 node->status = rf_recover;
374 }
375 }
376 }
377 /* now, fire the nodes */
378 for (node = nodeList; node; node = next) {
379 next = node->next;
380 if ((node->status == rf_fired) || (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(
403 RF_DagNode_t * node,
404 int context)
405 {
406 RF_DagNode_t *s, *a;
407 RF_Raid_t *raidPtr;
408 int i, ks;
409 RF_DagNode_t *finishlist = NULL; /* a list of NIL nodes to be
410 * finished */
411 RF_DagNode_t *skiplist = NULL; /* list of nodes with failed truedata
412 * antecedents */
413 RF_DagNode_t *firelist = NULL; /* a list of nodes to be fired */
414 RF_DagNode_t *q = NULL, *qh = NULL, *next;
415 int j, skipNode;
416
417 raidPtr = node->dagHdr->raidPtr;
418
419 DO_LOCK(raidPtr);
420
421 /* debug - validate fire counts */
422 for (i = 0; i < node->numAntecedents; i++) {
423 a = *(node->antecedents + i);
424 RF_ASSERT(a->numSuccFired >= a->numSuccDone);
425 RF_ASSERT(a->numSuccFired <= a->numSuccedents);
426 a->numSuccDone++;
427 }
428
429 switch (node->dagHdr->status) {
430 case rf_enable:
431 case rf_rollForward:
432 for (i = 0; i < node->numSuccedents; i++) {
433 s = *(node->succedents + i);
434 RF_ASSERT(s->status == rf_wait);
435 (s->numAntDone)++;
436 if (s->numAntDone == s->numAntecedents) {
437 /* look for NIL nodes */
438 if (s->doFunc == rf_NullNodeFunc) {
439 /* don't fire NIL nodes, just process
440 * them */
441 s->next = finishlist;
442 finishlist = s;
443 } else {
444 /* look to see if the node is to be
445 * skipped */
446 skipNode = RF_FALSE;
447 for (j = 0; j < s->numAntecedents; j++)
448 if ((s->antType[j] == rf_trueData) && (s->antecedents[j]->status == rf_bad))
449 skipNode = RF_TRUE;
450 if (skipNode) {
451 /* this node has one or more
452 * failed true data
453 * dependencies, so skip it */
454 s->next = skiplist;
455 skiplist = s;
456 } else
457 /* add s to list of nodes (q)
458 * to execute */
459 if (context != RF_INTR_CONTEXT) {
460 /* we only have to
461 * enqueue if we're at
462 * intr context */
463 s->next = firelist; /* put node on a list to
464 * be fired after we
465 * unlock */
466 firelist = s;
467 } else { /* enqueue the node for
468 * the dag exec thread
469 * to fire */
470 RF_ASSERT(NodeReady(s));
471 if (q) {
472 q->next = s;
473 q = s;
474 } else {
475 qh = q = s;
476 qh->next = NULL;
477 }
478 }
479 }
480 }
481 }
482
483 if (q) {
484 /* xfer our local list of nodes to the node queue */
485 q->next = raidPtr->node_queue;
486 raidPtr->node_queue = qh;
487 DO_SIGNAL(raidPtr);
488 }
489 DO_UNLOCK(raidPtr);
490
491 for (; skiplist; skiplist = next) {
492 next = skiplist->next;
493 skiplist->status = rf_skipped;
494 for (i = 0; i < skiplist->numAntecedents; i++) {
495 skiplist->antecedents[i]->numSuccFired++;
496 }
497 if (skiplist->commitNode) {
498 skiplist->dagHdr->numCommits++;
499 }
500 rf_FinishNode(skiplist, context);
501 }
502 for (; finishlist; finishlist = next) {
503 /* NIL nodes: no need to fire them */
504 next = finishlist->next;
505 finishlist->status = rf_good;
506 for (i = 0; i < finishlist->numAntecedents; i++) {
507 finishlist->antecedents[i]->numSuccFired++;
508 }
509 if (finishlist->commitNode)
510 finishlist->dagHdr->numCommits++;
511 /*
512 * Okay, here we're calling rf_FinishNode() on nodes that
513 * have the null function as their work proc. Such a node
514 * could be the terminal node in a DAG. If so, it will
515 * cause the DAG to complete, which will in turn free
516 * memory used by the DAG, which includes the node in
517 * question. Thus, we must avoid referencing the node
518 * at all after calling rf_FinishNode() on it.
519 */
520 rf_FinishNode(finishlist, context); /* recursive call */
521 }
522 /* fire all nodes in firelist */
523 FireNodeList(firelist);
524 break;
525
526 case rf_rollBackward:
527 for (i = 0; i < node->numAntecedents; i++) {
528 a = *(node->antecedents + i);
529 RF_ASSERT(a->status == rf_good);
530 RF_ASSERT(a->numSuccDone <= a->numSuccedents);
531 RF_ASSERT(a->numSuccDone <= a->numSuccFired);
532
533 if (a->numSuccDone == a->numSuccFired) {
534 if (a->undoFunc == rf_NullNodeFunc) {
535 /* don't fire NIL nodes, just process
536 * them */
537 a->next = finishlist;
538 finishlist = a;
539 } else {
540 if (context != RF_INTR_CONTEXT) {
541 /* we only have to enqueue if
542 * we're at intr context */
543 a->next = firelist; /* put node on a list to
544 * be fired after we
545 * unlock */
546 firelist = a;
547 } else { /* enqueue the node for
548 * the dag exec thread
549 * to fire */
550 RF_ASSERT(NodeReady(a));
551 if (q) {
552 q->next = a;
553 q = a;
554 } else {
555 qh = q = a;
556 qh->next = NULL;
557 }
558 }
559 }
560 }
561 }
562 if (q) {
563 /* xfer our local list of nodes to the node queue */
564 q->next = raidPtr->node_queue;
565 raidPtr->node_queue = qh;
566 DO_SIGNAL(raidPtr);
567 }
568 DO_UNLOCK(raidPtr);
569 for (; finishlist; finishlist = next) { /* NIL nodes: no need to
570 * fire them */
571 next = finishlist->next;
572 finishlist->status = rf_good;
573 /*
574 * Okay, here we're calling rf_FinishNode() on nodes that
575 * have the null function as their work proc. Such a node
576 * could be the first node in a DAG. If so, it will
577 * cause the DAG to complete, which will in turn free
578 * memory used by the DAG, which includes the node in
579 * question. Thus, we must avoid referencing the node
580 * at all after calling rf_FinishNode() on it.
581 */
582 rf_FinishNode(finishlist, context); /* recursive call */
583 }
584 /* fire all nodes in firelist */
585 FireNodeList(firelist);
586
587 break;
588 default:
589 printf("Engine found illegal DAG status in PropagateResults()\n");
590 RF_PANIC();
591 break;
592 }
593 }
594
595
596
597 /*
598 * Process a fired node which has completed
599 */
600 static void
601 ProcessNode(
602 RF_DagNode_t * node,
603 int context)
604 {
605 RF_Raid_t *raidPtr;
606
607 raidPtr = node->dagHdr->raidPtr;
608
609 switch (node->status) {
610 case rf_good:
611 /* normal case, don't need to do anything */
612 break;
613 case rf_bad:
614 if ((node->dagHdr->numCommits > 0) || (node->dagHdr->numCommitNodes == 0)) {
615 node->dagHdr->status = rf_rollForward; /* crossed commit
616 * barrier */
617 if (rf_engineDebug || 1) {
618 printf("raid%d: node (%s) returned fail, rolling forward\n", raidPtr->raidid, node->name);
619 }
620 } else {
621 node->dagHdr->status = rf_rollBackward; /* never reached commit
622 * barrier */
623 if (rf_engineDebug || 1) {
624 printf("raid%d: node (%s) returned fail, rolling backward\n", raidPtr->raidid, node->name);
625 }
626 }
627 break;
628 case rf_undone:
629 /* normal rollBackward case, don't need to do anything */
630 break;
631 case rf_panic:
632 /* an undo node failed!!! */
633 printf("UNDO of a node failed!!!/n");
634 break;
635 default:
636 printf("node finished execution with an illegal status!!!\n");
637 RF_PANIC();
638 break;
639 }
640
641 /* enqueue node's succedents (antecedents if rollBackward) for
642 * execution */
643 PropagateResults(node, context);
644 }
645
646
647
648 /* user context or dag-exec-thread context:
649 * This is the first step in post-processing a newly-completed node.
650 * This routine is called by each node execution function to mark the node
651 * as complete and fire off any successors that have been enabled.
652 */
653 int
654 rf_FinishNode(
655 RF_DagNode_t * node,
656 int context)
657 {
658 int retcode = RF_FALSE;
659 node->dagHdr->numNodesCompleted++;
660 ProcessNode(node, context);
661
662 return (retcode);
663 }
664
665
666 /* user context:
667 * submit dag for execution, return non-zero if we have to wait for completion.
668 * if and only if we return non-zero, we'll cause cbFunc to get invoked with
669 * cbArg when the DAG has completed.
670 *
671 * for now we always return 1. If the DAG does not cause any I/O, then the callback
672 * may get invoked before DispatchDAG returns. There's code in state 5 of ContinueRaidAccess
673 * to handle this.
674 *
675 * All we do here is fire the direct successors of the header node. The
676 * DAG execution thread does the rest of the dag processing.
677 */
678 int
679 rf_DispatchDAG(
680 RF_DagHeader_t * dag,
681 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:
711 * the thread that handles all DAG node firing.
712 * To minimize locking and unlocking, we grab a copy of the entire node queue and then set the
713 * node queue to NULL before doing any firing of nodes. This way we only have to release the
714 * lock once. Of course, it's probably rare that there's more than one node in the queue at
715 * any one time, but it sometimes happens.
716 *
717 * In the kernel, this thread runs at spl0 and is not swappable. I copied these
718 * characteristics from the aio_completion_thread.
719 */
720
721 static void
722 DAGExecutionThread(RF_ThreadArg_t arg)
723 {
724 RF_DagNode_t *nd, *local_nq, *term_nq, *fire_nq;
725 RF_Raid_t *raidPtr;
726 int ks;
727 int s;
728
729 raidPtr = (RF_Raid_t *) arg;
730
731 if (rf_engineDebug) {
732 printf("raid%d: Engine thread is running\n", raidPtr->raidid);
733 }
734
735 s = splbio();
736
737 RF_THREADGROUP_RUNNING(&raidPtr->engine_tg);
738
739 DO_LOCK(raidPtr);
740 while (!raidPtr->shutdown_engine) {
741
742 while (raidPtr->node_queue != NULL) {
743 local_nq = raidPtr->node_queue;
744 fire_nq = NULL;
745 term_nq = NULL;
746 raidPtr->node_queue = NULL;
747 DO_UNLOCK(raidPtr);
748
749 /* first, strip out the terminal nodes */
750 while (local_nq) {
751 nd = local_nq;
752 local_nq = local_nq->next;
753 switch (nd->dagHdr->status) {
754 case rf_enable:
755 case rf_rollForward:
756 if (nd->numSuccedents == 0) {
757 /* end of the dag, add to
758 * callback list */
759 nd->next = term_nq;
760 term_nq = nd;
761 } else {
762 /* not the end, add to the
763 * fire queue */
764 nd->next = fire_nq;
765 fire_nq = nd;
766 }
767 break;
768 case rf_rollBackward:
769 if (nd->numAntecedents == 0) {
770 /* end of the dag, add to the
771 * callback list */
772 nd->next = term_nq;
773 term_nq = nd;
774 } else {
775 /* not the end, add to the
776 * fire queue */
777 nd->next = fire_nq;
778 fire_nq = nd;
779 }
780 break;
781 default:
782 RF_PANIC();
783 break;
784 }
785 }
786
787 /* execute callback of dags which have reached the
788 * terminal node */
789 while (term_nq) {
790 nd = term_nq;
791 term_nq = term_nq->next;
792 nd->next = NULL;
793 (nd->dagHdr->cbFunc) (nd->dagHdr->cbArg);
794 raidPtr->dags_in_flight--; /* debug only */
795 }
796
797 /* fire remaining nodes */
798 FireNodeList(fire_nq);
799
800 DO_LOCK(raidPtr);
801 }
802 while (!raidPtr->shutdown_engine &&
803 raidPtr->node_queue == NULL) {
804 DO_UNLOCK(raidPtr);
805 DO_WAIT(raidPtr);
806 DO_LOCK(raidPtr);
807 }
808 }
809 DO_UNLOCK(raidPtr);
810
811 RF_THREADGROUP_DONE(&raidPtr->engine_tg);
812
813 splx(s);
814 kthread_exit(0);
815 }
816