Home | History | Annotate | Line # | Download | only in raidframe
rf_engine.c revision 1.4
      1 /*	$NetBSD: rf_engine.c,v 1.4 1999/02/05 00:06:11 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 occured before the commit node in   *
     52  *   the graph, backward recovery is used.  Otherwise, forward recovery is  *
     53  *   used.                                                                  *
     54  *                                                                          *
     55  ****************************************************************************/
     56 
     57 #include "rf_threadstuff.h"
     58 
     59 #include <sys/errno.h>
     60 
     61 #include "rf_dag.h"
     62 #include "rf_engine.h"
     63 #include "rf_threadid.h"
     64 #include "rf_etimer.h"
     65 #include "rf_general.h"
     66 #include "rf_dagutils.h"
     67 #include "rf_shutdown.h"
     68 #include "rf_raid.h"
     69 
     70 static void DAGExecutionThread(RF_ThreadArg_t arg);
     71 
     72 #define DO_INIT(_l_,_r_) { \
     73   int _rc; \
     74   _rc = rf_create_managed_mutex(_l_,&(_r_)->node_queue_mutex); \
     75   if (_rc) { \
     76     return(_rc); \
     77   } \
     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 /*
     87  * XXX Is this spl-ing really necessary?
     88  */
     89 #define DO_LOCK(_r_)      { ks = splbio(); RF_LOCK_MUTEX((_r_)->node_queue_mutex); }
     90 #define DO_UNLOCK(_r_)    { RF_UNLOCK_MUTEX((_r_)->node_queue_mutex); splx(ks); }
     91 #define DO_WAIT(_r_)   tsleep(&(_r_)->node_queue, PRIBIO | PCATCH, "raidframe nq",0)
     92 #define DO_SIGNAL(_r_)    wakeup(&(_r_)->node_queue)
     93 
     94 static void rf_ShutdownEngine(void *);
     95 
     96 static void
     97 rf_ShutdownEngine(arg)
     98 	void   *arg;
     99 {
    100 	RF_Raid_t *raidPtr;
    101 
    102 	raidPtr = (RF_Raid_t *) arg;
    103 	raidPtr->shutdown_engine = 1;
    104 	DO_SIGNAL(raidPtr);
    105 	/* XXX something is missing here... */
    106 #ifdef DEBUG
    107 	printf("IGNORING WAIT_STOP\n");
    108 #endif
    109 }
    110 
    111 int
    112 rf_ConfigureEngine(
    113     RF_ShutdownList_t ** listp,
    114     RF_Raid_t * raidPtr,
    115     RF_Config_t * cfgPtr)
    116 {
    117 	int     rc, tid = 0;
    118 
    119 	if (rf_engineDebug) {
    120 		rf_get_threadid(tid);
    121 	}
    122 	DO_INIT(listp, raidPtr);
    123 
    124 	raidPtr->node_queue = NULL;
    125 	raidPtr->dags_in_flight = 0;
    126 
    127 	rc = rf_init_managed_threadgroup(listp, &raidPtr->engine_tg);
    128 	if (rc)
    129 		return (rc);
    130 
    131 	/* we create the execution thread only once per system boot. no need
    132 	 * to check return code b/c the kernel panics if it can't create the
    133 	 * thread. */
    134 	if (rf_engineDebug) {
    135 		printf("[%d] Creating engine thread\n", tid);
    136 	}
    137 	if (RF_CREATE_THREAD(raidPtr->engine_thread, DAGExecutionThread, raidPtr)) {
    138 		RF_ERRORMSG("RAIDFRAME: Unable to create engine thread\n");
    139 		return (ENOMEM);
    140 	}
    141 	if (rf_engineDebug) {
    142 		printf("[%d] Created engine thread\n", tid);
    143 	}
    144 	RF_THREADGROUP_STARTED(&raidPtr->engine_tg);
    145 	/* XXX something is missing here... */
    146 #ifdef debug
    147 	printf("Skipping the WAIT_START!!\n");
    148 #endif
    149 #if 0
    150 	RF_THREADGROUP_WAIT_START(&raidPtr->engine_tg);
    151 #endif
    152 	/* engine thread is now running and waiting for work */
    153 	if (rf_engineDebug) {
    154 		printf("[%d] Engine thread running and waiting for events\n", tid);
    155 	}
    156 	rc = rf_ShutdownCreate(listp, rf_ShutdownEngine, raidPtr);
    157 	if (rc) {
    158 		RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n", __FILE__,
    159 		    __LINE__, rc);
    160 		rf_ShutdownEngine(NULL);
    161 	}
    162 	return (rc);
    163 }
    164 
    165 static int
    166 BranchDone(RF_DagNode_t * node)
    167 {
    168 	int     i;
    169 
    170 	/* return true if forward execution is completed for a node and it's
    171 	 * succedents */
    172 	switch (node->status) {
    173 	case rf_wait:
    174 		/* should never be called in this state */
    175 		RF_PANIC();
    176 		break;
    177 	case rf_fired:
    178 		/* node is currently executing, so we're not done */
    179 		return (RF_FALSE);
    180 	case rf_good:
    181 		for (i = 0; i < node->numSuccedents; i++)	/* for each succedent */
    182 			if (!BranchDone(node->succedents[i]))	/* recursively check
    183 								 * branch */
    184 				return RF_FALSE;
    185 		return RF_TRUE;	/* node and all succedent branches aren't in
    186 				 * fired state */
    187 		break;
    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 		break;
    201 	default:
    202 		/* illegal node status */
    203 		RF_PANIC();
    204 		break;
    205 	}
    206 }
    207 
    208 static int
    209 NodeReady(RF_DagNode_t * node)
    210 {
    211 	int     ready;
    212 
    213 	switch (node->dagHdr->status) {
    214 	case rf_enable:
    215 	case rf_rollForward:
    216 		if ((node->status == rf_wait) && (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) && (node->numSuccDone == node->numSuccedents))
    226 			ready = RF_TRUE;
    227 		else
    228 			ready = RF_FALSE;
    229 		break;
    230 	default:
    231 		printf("Execution engine found illegal DAG status in NodeReady\n");
    232 		RF_PANIC();
    233 		break;
    234 	}
    235 
    236 	return (ready);
    237 }
    238 
    239 
    240 
    241 /* user context and dag-exec-thread context:
    242  * Fire a node.  The node's status field determines which function, do or undo,
    243  * to be fired.
    244  * This routine assumes that the node's status field has alread been set to
    245  * "fired" or "recover" to indicate the direction of execution.
    246  */
    247 static void
    248 FireNode(RF_DagNode_t * node)
    249 {
    250 	int     tid;
    251 
    252 	switch (node->status) {
    253 	case rf_fired:
    254 		/* fire the do function of a node */
    255 		if (rf_engineDebug) {
    256 			rf_get_threadid(tid);
    257 			printf("[%d] Firing node 0x%lx (%s)\n", tid, (unsigned long) node, node->name);
    258 		}
    259 		if (node->flags & RF_DAGNODE_FLAG_YIELD) {
    260 #if defined(__NetBSD__) && defined(_KERNEL)
    261 			/* thread_block(); */
    262 			/* printf("Need to block the thread here...\n");  */
    263 			/* XXX thread_block is actually mentioned in
    264 			 * /usr/include/vm/vm_extern.h */
    265 #else
    266 			thread_block();
    267 #endif
    268 		}
    269 		(*(node->doFunc)) (node);
    270 		break;
    271 	case rf_recover:
    272 		/* fire the undo function of a node */
    273 		if (rf_engineDebug || 1) {
    274 			rf_get_threadid(tid);
    275 			printf("[%d] Firing (undo) node 0x%lx (%s)\n", tid, (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(
    302     int numNodes,
    303     RF_DagNode_t ** nodeList)
    304 {
    305 	RF_DagStatus_t dstat;
    306 	RF_DagNode_t *node;
    307 	int     i, j;
    308 
    309 	/* first, mark all nodes which are ready to be fired */
    310 	for (i = 0; i < numNodes; i++) {
    311 		node = nodeList[i];
    312 		dstat = node->dagHdr->status;
    313 		RF_ASSERT((node->status == rf_wait) || (node->status == rf_good));
    314 		if (NodeReady(node)) {
    315 			if ((dstat == rf_enable) || (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 				RF_ASSERT(node->commitNode == RF_FALSE);	/* only one commit node
    326 										 * per graph */
    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) || (nodeList[i]->status == rf_recover))
    334 			FireNode(nodeList[i]);
    335 	}
    336 }
    337 
    338 
    339 /* user context:
    340  * Attempt to fire each node in a linked list.
    341  * The entire list is fired atomically.
    342  */
    343 static void
    344 FireNodeList(RF_DagNode_t * nodeList)
    345 {
    346 	RF_DagNode_t *node, *next;
    347 	RF_DagStatus_t dstat;
    348 	int     j;
    349 
    350 	if (nodeList) {
    351 		/* first, mark all nodes which are ready to be fired */
    352 		for (node = nodeList; node; node = next) {
    353 			next = node->next;
    354 			dstat = node->dagHdr->status;
    355 			RF_ASSERT((node->status == rf_wait) || (node->status == rf_good));
    356 			if (NodeReady(node)) {
    357 				if ((dstat == rf_enable) || (dstat == rf_rollForward)) {
    358 					RF_ASSERT(node->status == rf_wait);
    359 					if (node->commitNode)
    360 						node->dagHdr->numCommits++;
    361 					node->status = rf_fired;
    362 					for (j = 0; j < node->numAntecedents; j++)
    363 						node->antecedents[j]->numSuccFired++;
    364 				} else {
    365 					RF_ASSERT(dstat == rf_rollBackward);
    366 					RF_ASSERT(node->status == rf_good);
    367 					RF_ASSERT(node->commitNode == RF_FALSE);	/* only one commit node
    368 											 * per graph */
    369 					node->status = rf_recover;
    370 				}
    371 			}
    372 		}
    373 		/* now, fire the nodes */
    374 		for (node = nodeList; node; node = next) {
    375 			next = node->next;
    376 			if ((node->status == rf_fired) || (node->status == rf_recover))
    377 				FireNode(node);
    378 		}
    379 	}
    380 }
    381 /* interrupt context:
    382  * for each succedent
    383  *    propagate required results from node to succedent
    384  *    increment succedent's numAntDone
    385  *    place newly-enable nodes on node queue for firing
    386  *
    387  * To save context switches, we don't place NIL nodes on the node queue,
    388  * but rather just process them as if they had fired.  Note that NIL nodes
    389  * that are the direct successors of the header will actually get fired by
    390  * DispatchDAG, which is fine because no context switches are involved.
    391  *
    392  * Important:  when running at user level, this can be called by any
    393  * disk thread, and so the increment and check of the antecedent count
    394  * must be locked.  I used the node queue mutex and locked down the
    395  * entire function, but this is certainly overkill.
    396  */
    397 static void
    398 PropagateResults(
    399     RF_DagNode_t * node,
    400     int context)
    401 {
    402 	RF_DagNode_t *s, *a;
    403 	RF_Raid_t *raidPtr;
    404 	int     tid, i, ks;
    405 	RF_DagNode_t *finishlist = NULL;	/* a list of NIL nodes to be
    406 						 * finished */
    407 	RF_DagNode_t *skiplist = NULL;	/* list of nodes with failed truedata
    408 					 * antecedents */
    409 	RF_DagNode_t *firelist = NULL;	/* a list of nodes to be fired */
    410 	RF_DagNode_t *q = NULL, *qh = NULL, *next;
    411 	int     j, skipNode;
    412 
    413 	rf_get_threadid(tid);
    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 							s->next = firelist;	/* put node on a list to
    462 										 * be fired after we
    463 										 * unlock */
    464 							firelist = s;
    465 						} else {	/* enqueue the node for
    466 								 * the dag exec thread
    467 								 * to fire */
    468 							RF_ASSERT(NodeReady(s));
    469 							if (q) {
    470 								q->next = s;
    471 								q = s;
    472 							} else {
    473 								qh = q = s;
    474 								qh->next = NULL;
    475 							}
    476 						}
    477 				}
    478 			}
    479 		}
    480 
    481 		if (q) {
    482 			/* xfer our local list of nodes to the node queue */
    483 			q->next = raidPtr->node_queue;
    484 			raidPtr->node_queue = qh;
    485 			DO_SIGNAL(raidPtr);
    486 		}
    487 		DO_UNLOCK(raidPtr);
    488 
    489 		for (; skiplist; skiplist = next) {
    490 			next = skiplist->next;
    491 			skiplist->status = rf_skipped;
    492 			for (i = 0; i < skiplist->numAntecedents; i++) {
    493 				skiplist->antecedents[i]->numSuccFired++;
    494 			}
    495 			if (skiplist->commitNode) {
    496 				skiplist->dagHdr->numCommits++;
    497 			}
    498 			rf_FinishNode(skiplist, context);
    499 		}
    500 		for (; finishlist; finishlist = next) {
    501 			/* NIL nodes: no need to fire them */
    502 			next = finishlist->next;
    503 			finishlist->status = rf_good;
    504 			for (i = 0; i < finishlist->numAntecedents; i++) {
    505 				finishlist->antecedents[i]->numSuccFired++;
    506 			}
    507 			if (finishlist->commitNode)
    508 				finishlist->dagHdr->numCommits++;
    509 			/*
    510 		         * Okay, here we're calling rf_FinishNode() on nodes that
    511 		         * have the null function as their work proc. Such a node
    512 		         * could be the terminal node in a DAG. If so, it will
    513 		         * cause the DAG to complete, which will in turn free
    514 		         * memory used by the DAG, which includes the node in
    515 		         * question. Thus, we must avoid referencing the node
    516 		         * at all after calling rf_FinishNode() on it.
    517 		         */
    518 			rf_FinishNode(finishlist, context);	/* recursive call */
    519 		}
    520 		/* fire all nodes in firelist */
    521 		FireNodeList(firelist);
    522 		break;
    523 
    524 	case rf_rollBackward:
    525 		for (i = 0; i < node->numAntecedents; i++) {
    526 			a = *(node->antecedents + i);
    527 			RF_ASSERT(a->status == rf_good);
    528 			RF_ASSERT(a->numSuccDone <= a->numSuccedents);
    529 			RF_ASSERT(a->numSuccDone <= a->numSuccFired);
    530 
    531 			if (a->numSuccDone == a->numSuccFired) {
    532 				if (a->undoFunc == rf_NullNodeFunc) {
    533 					/* don't fire NIL nodes, just process
    534 					 * them */
    535 					a->next = finishlist;
    536 					finishlist = a;
    537 				} else {
    538 					if (context != RF_INTR_CONTEXT) {
    539 						/* we only have to enqueue if
    540 						 * we're at intr context */
    541 						a->next = firelist;	/* put node on a list to
    542 									 * be fired after we
    543 									 * unlock */
    544 						firelist = a;
    545 					} else {	/* enqueue the node for
    546 							 * the dag exec thread
    547 							 * to fire */
    548 						RF_ASSERT(NodeReady(a));
    549 						if (q) {
    550 							q->next = a;
    551 							q = a;
    552 						} else {
    553 							qh = q = a;
    554 							qh->next = NULL;
    555 						}
    556 					}
    557 				}
    558 			}
    559 		}
    560 		if (q) {
    561 			/* xfer our local list of nodes to the node queue */
    562 			q->next = raidPtr->node_queue;
    563 			raidPtr->node_queue = qh;
    564 			DO_SIGNAL(raidPtr);
    565 		}
    566 		DO_UNLOCK(raidPtr);
    567 		for (; finishlist; finishlist = next) {	/* NIL nodes: no need to
    568 							 * fire them */
    569 			next = finishlist->next;
    570 			finishlist->status = rf_good;
    571 			/*
    572 		         * Okay, here we're calling rf_FinishNode() on nodes that
    573 		         * have the null function as their work proc. Such a node
    574 		         * could be the first node in a DAG. If so, it will
    575 		         * cause the DAG to complete, which will in turn free
    576 		         * memory used by the DAG, which includes the node in
    577 		         * question. Thus, we must avoid referencing the node
    578 		         * at all after calling rf_FinishNode() on it.
    579 		         */
    580 			rf_FinishNode(finishlist, context);	/* recursive call */
    581 		}
    582 		/* fire all nodes in firelist */
    583 		FireNodeList(firelist);
    584 
    585 		break;
    586 	default:
    587 		printf("Engine found illegal DAG status in PropagateResults()\n");
    588 		RF_PANIC();
    589 		break;
    590 	}
    591 }
    592 
    593 
    594 
    595 /*
    596  * Process a fired node which has completed
    597  */
    598 static void
    599 ProcessNode(
    600     RF_DagNode_t * node,
    601     int context)
    602 {
    603 	RF_Raid_t *raidPtr;
    604 	int     tid;
    605 
    606 	raidPtr = node->dagHdr->raidPtr;
    607 
    608 	switch (node->status) {
    609 	case rf_good:
    610 		/* normal case, don't need to do anything */
    611 		break;
    612 	case rf_bad:
    613 		if ((node->dagHdr->numCommits > 0) || (node->dagHdr->numCommitNodes == 0)) {
    614 			node->dagHdr->status = rf_rollForward;	/* crossed commit
    615 								 * barrier */
    616 			if (rf_engineDebug || 1) {
    617 				rf_get_threadid(tid);
    618 				printf("[%d] node (%s) returned fail, rolling forward\n", tid, node->name);
    619 			}
    620 		} else {
    621 			node->dagHdr->status = rf_rollBackward;	/* never reached commit
    622 								 * barrier */
    623 			if (rf_engineDebug || 1) {
    624 				rf_get_threadid(tid);
    625 				printf("[%d] node (%s) returned fail, rolling backward\n", tid, node->name);
    626 			}
    627 		}
    628 		break;
    629 	case rf_undone:
    630 		/* normal rollBackward case, don't need to do anything */
    631 		break;
    632 	case rf_panic:
    633 		/* an undo node failed!!! */
    634 		printf("UNDO of a node failed!!!/n");
    635 		break;
    636 	default:
    637 		printf("node finished execution with an illegal status!!!\n");
    638 		RF_PANIC();
    639 		break;
    640 	}
    641 
    642 	/* enqueue node's succedents (antecedents if rollBackward) for
    643 	 * execution */
    644 	PropagateResults(node, context);
    645 }
    646 
    647 
    648 
    649 /* user context or dag-exec-thread context:
    650  * This is the first step in post-processing a newly-completed node.
    651  * This routine is called by each node execution function to mark the node
    652  * as complete and fire off any successors that have been enabled.
    653  */
    654 int
    655 rf_FinishNode(
    656     RF_DagNode_t * node,
    657     int context)
    658 {
    659 	/* as far as I can tell, retcode is not used -wvcii */
    660 	int     retcode = RF_FALSE;
    661 	node->dagHdr->numNodesCompleted++;
    662 	ProcessNode(node, context);
    663 
    664 	return (retcode);
    665 }
    666 
    667 
    668 /* user context:
    669  * submit dag for execution, return non-zero if we have to wait for completion.
    670  * if and only if we return non-zero, we'll cause cbFunc to get invoked with
    671  * cbArg when the DAG has completed.
    672  *
    673  * for now we always return 1.  If the DAG does not cause any I/O, then the callback
    674  * may get invoked before DispatchDAG returns.  There's code in state 5 of ContinueRaidAccess
    675  * to handle this.
    676  *
    677  * All we do here is fire the direct successors of the header node.  The
    678  * DAG execution thread does the rest of the dag processing.
    679  */
    680 int
    681 rf_DispatchDAG(
    682     RF_DagHeader_t * dag,
    683     void (*cbFunc) (void *),
    684     void *cbArg)
    685 {
    686 	RF_Raid_t *raidPtr;
    687 	int     tid;
    688 
    689 	raidPtr = dag->raidPtr;
    690 	if (dag->tracerec) {
    691 		RF_ETIMER_START(dag->tracerec->timer);
    692 	}
    693 	if (rf_engineDebug || rf_validateDAGDebug) {
    694 		if (rf_ValidateDAG(dag))
    695 			RF_PANIC();
    696 	}
    697 	if (rf_engineDebug) {
    698 		rf_get_threadid(tid);
    699 		printf("[%d] Entering DispatchDAG\n", tid);
    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, tid;
    727 	int     s;
    728 
    729 	raidPtr = (RF_Raid_t *) arg;
    730 
    731 	rf_assign_threadid();
    732 	if (rf_engineDebug) {
    733 		rf_get_threadid(tid);
    734 		printf("[%d] Engine thread is running\n", tid);
    735 	}
    736 #ifndef __NetBSD__
    737 	thread = current_thread();
    738 	thread_swappable(thread, RF_FALSE);
    739 	thread->priority = thread->sched_pri = BASEPRI_SYSTEM;
    740 	s = spl0();
    741 #endif
    742 	/* XXX what to put here XXX */
    743 
    744 	s = splbio();
    745 
    746 	RF_THREADGROUP_RUNNING(&raidPtr->engine_tg);
    747 
    748 	DO_LOCK(raidPtr);
    749 	while (!raidPtr->shutdown_engine) {
    750 
    751 		while (raidPtr->node_queue != NULL) {
    752 			local_nq = raidPtr->node_queue;
    753 			fire_nq = NULL;
    754 			term_nq = NULL;
    755 			raidPtr->node_queue = NULL;
    756 			DO_UNLOCK(raidPtr);
    757 
    758 			/* first, strip out the terminal nodes */
    759 			while (local_nq) {
    760 				nd = local_nq;
    761 				local_nq = local_nq->next;
    762 				switch (nd->dagHdr->status) {
    763 				case rf_enable:
    764 				case rf_rollForward:
    765 					if (nd->numSuccedents == 0) {
    766 						/* end of the dag, add to
    767 						 * callback list */
    768 						nd->next = term_nq;
    769 						term_nq = nd;
    770 					} else {
    771 						/* not the end, add to the
    772 						 * fire queue */
    773 						nd->next = fire_nq;
    774 						fire_nq = nd;
    775 					}
    776 					break;
    777 				case rf_rollBackward:
    778 					if (nd->numAntecedents == 0) {
    779 						/* end of the dag, add to the
    780 						 * callback list */
    781 						nd->next = term_nq;
    782 						term_nq = nd;
    783 					} else {
    784 						/* not the end, add to the
    785 						 * fire queue */
    786 						nd->next = fire_nq;
    787 						fire_nq = nd;
    788 					}
    789 					break;
    790 				default:
    791 					RF_PANIC();
    792 					break;
    793 				}
    794 			}
    795 
    796 			/* execute callback of dags which have reached the
    797 			 * terminal node */
    798 			while (term_nq) {
    799 				nd = term_nq;
    800 				term_nq = term_nq->next;
    801 				nd->next = NULL;
    802 				(nd->dagHdr->cbFunc) (nd->dagHdr->cbArg);
    803 				raidPtr->dags_in_flight--;	/* debug only */
    804 			}
    805 
    806 			/* fire remaining nodes */
    807 			FireNodeList(fire_nq);
    808 
    809 			DO_LOCK(raidPtr);
    810 		}
    811 		while (!raidPtr->shutdown_engine && raidPtr->node_queue == NULL)
    812 			DO_WAIT(raidPtr);
    813 	}
    814 	DO_UNLOCK(raidPtr);
    815 
    816 	RF_THREADGROUP_DONE(&raidPtr->engine_tg);
    817 #ifdef __NetBSD__
    818 	splx(s);
    819 	kthread_exit(0);
    820 #else
    821 	splx(s);
    822 	thread_terminate(thread);
    823 	thread_halt_self();
    824 #endif
    825 }
    826