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