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