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