Home | History | Annotate | Line # | Download | only in raidframe
rf_states.c revision 1.44.6.1
      1 /*	$NetBSD: rf_states.c,v 1.44.6.1 2011/06/06 09:08:34 jruoho Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author: Mark Holland, William V. Courtright II, Robby Findler
      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 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: rf_states.c,v 1.44.6.1 2011/06/06 09:08:34 jruoho Exp $");
     31 
     32 #include <sys/errno.h>
     33 
     34 #include "rf_archs.h"
     35 #include "rf_threadstuff.h"
     36 #include "rf_raid.h"
     37 #include "rf_dag.h"
     38 #include "rf_desc.h"
     39 #include "rf_aselect.h"
     40 #include "rf_general.h"
     41 #include "rf_states.h"
     42 #include "rf_dagutils.h"
     43 #include "rf_driver.h"
     44 #include "rf_engine.h"
     45 #include "rf_map.h"
     46 #include "rf_etimer.h"
     47 #include "rf_kintf.h"
     48 #include "rf_paritymap.h"
     49 
     50 #ifndef RF_DEBUG_STATES
     51 #define RF_DEBUG_STATES 0
     52 #endif
     53 
     54 /* prototypes for some of the available states.
     55 
     56    States must:
     57 
     58      - not block.
     59 
     60      - either schedule rf_ContinueRaidAccess as a callback and return
     61        RF_TRUE, or complete all of their work and return RF_FALSE.
     62 
     63      - increment desc->state when they have finished their work.
     64 */
     65 
     66 #if RF_DEBUG_STATES
     67 static char *
     68 StateName(RF_AccessState_t state)
     69 {
     70 	switch (state) {
     71 		case rf_QuiesceState:return "QuiesceState";
     72 	case rf_MapState:
     73 		return "MapState";
     74 	case rf_LockState:
     75 		return "LockState";
     76 	case rf_CreateDAGState:
     77 		return "CreateDAGState";
     78 	case rf_ExecuteDAGState:
     79 		return "ExecuteDAGState";
     80 	case rf_ProcessDAGState:
     81 		return "ProcessDAGState";
     82 	case rf_CleanupState:
     83 		return "CleanupState";
     84 	case rf_LastState:
     85 		return "LastState";
     86 	case rf_IncrAccessesCountState:
     87 		return "IncrAccessesCountState";
     88 	case rf_DecrAccessesCountState:
     89 		return "DecrAccessesCountState";
     90 	default:
     91 		return "!!! UnnamedState !!!";
     92 	}
     93 }
     94 #endif
     95 
     96 void
     97 rf_ContinueRaidAccess(RF_RaidAccessDesc_t *desc)
     98 {
     99 	int     suspended = RF_FALSE;
    100 	int     current_state_index = desc->state;
    101 	RF_AccessState_t current_state = desc->states[current_state_index];
    102 #if RF_DEBUG_STATES
    103 	int     unit = desc->raidPtr->raidid;
    104 #endif
    105 
    106 	do {
    107 
    108 		current_state_index = desc->state;
    109 		current_state = desc->states[current_state_index];
    110 
    111 		switch (current_state) {
    112 
    113 		case rf_QuiesceState:
    114 			suspended = rf_State_Quiesce(desc);
    115 			break;
    116 		case rf_IncrAccessesCountState:
    117 			suspended = rf_State_IncrAccessCount(desc);
    118 			break;
    119 		case rf_MapState:
    120 			suspended = rf_State_Map(desc);
    121 			break;
    122 		case rf_LockState:
    123 			suspended = rf_State_Lock(desc);
    124 			break;
    125 		case rf_CreateDAGState:
    126 			suspended = rf_State_CreateDAG(desc);
    127 			break;
    128 		case rf_ExecuteDAGState:
    129 			suspended = rf_State_ExecuteDAG(desc);
    130 			break;
    131 		case rf_ProcessDAGState:
    132 			suspended = rf_State_ProcessDAG(desc);
    133 			break;
    134 		case rf_CleanupState:
    135 			suspended = rf_State_Cleanup(desc);
    136 			break;
    137 		case rf_DecrAccessesCountState:
    138 			suspended = rf_State_DecrAccessCount(desc);
    139 			break;
    140 		case rf_LastState:
    141 			suspended = rf_State_LastState(desc);
    142 			break;
    143 		}
    144 
    145 		/* after this point, we cannot dereference desc since
    146 		 * desc may have been freed. desc is only freed in
    147 		 * LastState, so if we renter this function or loop
    148 		 * back up, desc should be valid. */
    149 
    150 #if RF_DEBUG_STATES
    151 		if (rf_printStatesDebug) {
    152 			printf("raid%d: State: %-24s StateIndex: %3i desc: 0x%ld %s\n",
    153 			       unit, StateName(current_state),
    154 			       current_state_index, (long) desc,
    155 			       suspended ? "callback scheduled" : "looping");
    156 		}
    157 #endif
    158 	} while (!suspended && current_state != rf_LastState);
    159 
    160 	return;
    161 }
    162 
    163 
    164 void
    165 rf_ContinueDagAccess(RF_DagList_t *dagList)
    166 {
    167 #if RF_ACC_TRACE > 0
    168 	RF_AccTraceEntry_t *tracerec = &(dagList->desc->tracerec);
    169 	RF_Etimer_t timer;
    170 #endif
    171 	RF_RaidAccessDesc_t *desc;
    172 	RF_DagHeader_t *dag_h;
    173 	int     i;
    174 
    175 	desc = dagList->desc;
    176 
    177 #if RF_ACC_TRACE > 0
    178 	timer = tracerec->timer;
    179 	RF_ETIMER_STOP(timer);
    180 	RF_ETIMER_EVAL(timer);
    181 	tracerec->specific.user.exec_us = RF_ETIMER_VAL_US(timer);
    182 	RF_ETIMER_START(tracerec->timer);
    183 #endif
    184 
    185 	/* skip to dag which just finished */
    186 	dag_h = dagList->dags;
    187 	for (i = 0; i < dagList->numDagsDone; i++) {
    188 		dag_h = dag_h->next;
    189 	}
    190 
    191 	/* check to see if retry is required */
    192 	if (dag_h->status == rf_rollBackward) {
    193 		/* when a dag fails, mark desc status as bad and allow
    194 		 * all other dags in the desc to execute to
    195 		 * completion.  then, free all dags and start over */
    196 		desc->status = 1;	/* bad status */
    197 #if 0
    198 		printf("raid%d: DAG failure: %c addr 0x%lx "
    199 		       "(%ld) nblk 0x%x (%d) buf 0x%lx state %d\n",
    200 		       desc->raidPtr->raidid, desc->type,
    201 		       (long) desc->raidAddress,
    202 		       (long) desc->raidAddress, (int) desc->numBlocks,
    203 		       (int) desc->numBlocks,
    204 		       (unsigned long) (desc->bufPtr), desc->state);
    205 #endif
    206 	}
    207 	dagList->numDagsDone++;
    208 	rf_ContinueRaidAccess(desc);
    209 }
    210 
    211 int
    212 rf_State_LastState(RF_RaidAccessDesc_t *desc)
    213 {
    214 	void    (*callbackFunc) (RF_CBParam_t) = desc->callbackFunc;
    215 	RF_CBParam_t callbackArg;
    216 
    217 	callbackArg.p = desc->callbackArg;
    218 
    219 	/*
    220 	 * We don't support non-async IO.
    221 	 */
    222 	KASSERT(desc->async_flag);
    223 
    224 	/*
    225 	 * That's all the IO for this one... unbusy the 'disk'.
    226 	 */
    227 
    228 	rf_disk_unbusy(desc);
    229 
    230 	/*
    231 	 * Wakeup any requests waiting to go.
    232 	 */
    233 
    234 	rf_lock_mutex2(desc->raidPtr->mutex);
    235 	desc->raidPtr->openings++;
    236 	rf_unlock_mutex2(desc->raidPtr->mutex);
    237 
    238 	rf_lock_mutex2(desc->raidPtr->iodone_lock);
    239 	rf_signal_cond2(desc->raidPtr->iodone_cv);
    240 	rf_unlock_mutex2(desc->raidPtr->iodone_lock);
    241 
    242 	/*
    243 	 * The parity_map hook has to go here, because the iodone
    244 	 * callback goes straight into the kintf layer.
    245 	 */
    246 	if (desc->raidPtr->parity_map != NULL &&
    247 	    desc->type == RF_IO_TYPE_WRITE)
    248 		rf_paritymap_end(desc->raidPtr->parity_map,
    249 		    desc->raidAddress, desc->numBlocks);
    250 
    251 	/* printf("Calling biodone on 0x%x\n",desc->bp); */
    252 	biodone(desc->bp);	/* access came through ioctl */
    253 
    254 	if (callbackFunc)
    255 		callbackFunc(callbackArg);
    256 	rf_FreeRaidAccDesc(desc);
    257 
    258 	return RF_FALSE;
    259 }
    260 
    261 int
    262 rf_State_IncrAccessCount(RF_RaidAccessDesc_t *desc)
    263 {
    264 	RF_Raid_t *raidPtr;
    265 
    266 	raidPtr = desc->raidPtr;
    267 	/* Bummer. We have to do this to be 100% safe w.r.t. the increment
    268 	 * below */
    269 	rf_lock_mutex2(raidPtr->access_suspend_mutex);
    270 	raidPtr->accs_in_flight++;	/* used to detect quiescence */
    271 	rf_unlock_mutex2(raidPtr->access_suspend_mutex);
    272 
    273 	desc->state++;
    274 	return RF_FALSE;
    275 }
    276 
    277 int
    278 rf_State_DecrAccessCount(RF_RaidAccessDesc_t *desc)
    279 {
    280 	RF_Raid_t *raidPtr;
    281 
    282 	raidPtr = desc->raidPtr;
    283 
    284 	rf_lock_mutex2(raidPtr->access_suspend_mutex);
    285 	raidPtr->accs_in_flight--;
    286 	if (raidPtr->accesses_suspended && raidPtr->accs_in_flight == 0) {
    287 		rf_SignalQuiescenceLock(raidPtr);
    288 	}
    289 	rf_unlock_mutex2(raidPtr->access_suspend_mutex);
    290 
    291 	desc->state++;
    292 	return RF_FALSE;
    293 }
    294 
    295 int
    296 rf_State_Quiesce(RF_RaidAccessDesc_t *desc)
    297 {
    298 #if RF_ACC_TRACE > 0
    299 	RF_AccTraceEntry_t *tracerec = &desc->tracerec;
    300 	RF_Etimer_t timer;
    301 #endif
    302 	RF_CallbackDesc_t *cb;
    303 	RF_Raid_t *raidPtr;
    304 	int     suspended = RF_FALSE;
    305 	int need_cb, used_cb;
    306 
    307 	raidPtr = desc->raidPtr;
    308 
    309 #if RF_ACC_TRACE > 0
    310 	RF_ETIMER_START(timer);
    311 	RF_ETIMER_START(desc->timer);
    312 #endif
    313 
    314 	need_cb = 0;
    315 	used_cb = 0;
    316 	cb = NULL;
    317 
    318 	rf_lock_mutex2(raidPtr->access_suspend_mutex);
    319 	/* Do an initial check to see if we might need a callback structure */
    320 	if (raidPtr->accesses_suspended) {
    321 		need_cb = 1;
    322 	}
    323 	rf_unlock_mutex2(raidPtr->access_suspend_mutex);
    324 
    325 	if (need_cb) {
    326 		/* create a callback if we might need it...
    327 		   and we likely do. */
    328 		cb = rf_AllocCallbackDesc();
    329 	}
    330 
    331 	rf_lock_mutex2(raidPtr->access_suspend_mutex);
    332 	if (raidPtr->accesses_suspended) {
    333 		cb->callbackFunc = (void (*) (RF_CBParam_t)) rf_ContinueRaidAccess;
    334 		cb->callbackArg.p = (void *) desc;
    335 		cb->next = raidPtr->quiesce_wait_list;
    336 		raidPtr->quiesce_wait_list = cb;
    337 		suspended = RF_TRUE;
    338 		used_cb = 1;
    339 	}
    340 	rf_unlock_mutex2(raidPtr->access_suspend_mutex);
    341 
    342 	if ((need_cb == 1) && (used_cb == 0)) {
    343 		rf_FreeCallbackDesc(cb);
    344 	}
    345 
    346 #if RF_ACC_TRACE > 0
    347 	RF_ETIMER_STOP(timer);
    348 	RF_ETIMER_EVAL(timer);
    349 	tracerec->specific.user.suspend_ovhd_us += RF_ETIMER_VAL_US(timer);
    350 #endif
    351 
    352 #if RF_DEBUG_QUIESCE
    353 	if (suspended && rf_quiesceDebug)
    354 		printf("Stalling access due to quiescence lock\n");
    355 #endif
    356 	desc->state++;
    357 	return suspended;
    358 }
    359 
    360 int
    361 rf_State_Map(RF_RaidAccessDesc_t *desc)
    362 {
    363 	RF_Raid_t *raidPtr = desc->raidPtr;
    364 #if RF_ACC_TRACE > 0
    365 	RF_AccTraceEntry_t *tracerec = &desc->tracerec;
    366 	RF_Etimer_t timer;
    367 
    368 	RF_ETIMER_START(timer);
    369 #endif
    370 
    371 	if (!(desc->asmap = rf_MapAccess(raidPtr, desc->raidAddress, desc->numBlocks,
    372 		    desc->bufPtr, RF_DONT_REMAP)))
    373 		RF_PANIC();
    374 
    375 #if RF_ACC_TRACE > 0
    376 	RF_ETIMER_STOP(timer);
    377 	RF_ETIMER_EVAL(timer);
    378 	tracerec->specific.user.map_us = RF_ETIMER_VAL_US(timer);
    379 #endif
    380 
    381 	desc->state++;
    382 	return RF_FALSE;
    383 }
    384 
    385 int
    386 rf_State_Lock(RF_RaidAccessDesc_t *desc)
    387 {
    388 #if RF_ACC_TRACE > 0
    389 	RF_AccTraceEntry_t *tracerec = &desc->tracerec;
    390 	RF_Etimer_t timer;
    391 #endif
    392 	RF_Raid_t *raidPtr = desc->raidPtr;
    393 	RF_AccessStripeMapHeader_t *asmh = desc->asmap;
    394 	RF_AccessStripeMap_t *asm_p;
    395 	RF_StripeNum_t lastStripeID = -1;
    396 	int     suspended = RF_FALSE;
    397 
    398 #if RF_ACC_TRACE > 0
    399 	RF_ETIMER_START(timer);
    400 #endif
    401 
    402 	/* acquire each lock that we don't already hold */
    403 	for (asm_p = asmh->stripeMap; asm_p; asm_p = asm_p->next) {
    404 		RF_ASSERT(RF_IO_IS_R_OR_W(desc->type));
    405 		if (!rf_suppressLocksAndLargeWrites &&
    406 		    asm_p->parityInfo &&
    407 		    !(desc->flags & RF_DAG_SUPPRESS_LOCKS) &&
    408 		    !(asm_p->flags & RF_ASM_FLAGS_LOCK_TRIED)) {
    409 			asm_p->flags |= RF_ASM_FLAGS_LOCK_TRIED;
    410 				/* locks must be acquired hierarchically */
    411 			RF_ASSERT(asm_p->stripeID > lastStripeID);
    412 			lastStripeID = asm_p->stripeID;
    413 
    414 			RF_INIT_LOCK_REQ_DESC(asm_p->lockReqDesc, desc->type,
    415 					      (void (*) (struct buf *)) rf_ContinueRaidAccess, desc, asm_p,
    416 					      raidPtr->Layout.dataSectorsPerStripe);
    417 			if (rf_AcquireStripeLock(raidPtr->lockTable, asm_p->stripeID,
    418 						 &asm_p->lockReqDesc)) {
    419 				suspended = RF_TRUE;
    420 				break;
    421 			}
    422 		}
    423 		if (desc->type == RF_IO_TYPE_WRITE &&
    424 		    raidPtr->status == rf_rs_reconstructing) {
    425 			if (!(asm_p->flags & RF_ASM_FLAGS_FORCE_TRIED)) {
    426 				int     val;
    427 
    428 				asm_p->flags |= RF_ASM_FLAGS_FORCE_TRIED;
    429 				val = rf_ForceOrBlockRecon(raidPtr, asm_p,
    430 							   (void (*) (RF_Raid_t *, void *)) rf_ContinueRaidAccess, desc);
    431 				if (val == 0) {
    432 					asm_p->flags |= RF_ASM_FLAGS_RECON_BLOCKED;
    433 				} else {
    434 					suspended = RF_TRUE;
    435 					break;
    436 				}
    437 			} else {
    438 #if RF_DEBUG_PSS > 0
    439 				if (rf_pssDebug) {
    440 					printf("raid%d: skipping force/block because already done, psid %ld\n",
    441 					       desc->raidPtr->raidid,
    442 					       (long) asm_p->stripeID);
    443 				}
    444 #endif
    445 			}
    446 		} else {
    447 #if RF_DEBUG_PSS > 0
    448 			if (rf_pssDebug) {
    449 				printf("raid%d: skipping force/block because not write or not under recon, psid %ld\n",
    450 				       desc->raidPtr->raidid,
    451 				       (long) asm_p->stripeID);
    452 			}
    453 #endif
    454 		}
    455 	}
    456 #if RF_ACC_TRACE > 0
    457 	RF_ETIMER_STOP(timer);
    458 	RF_ETIMER_EVAL(timer);
    459 	tracerec->specific.user.lock_us += RF_ETIMER_VAL_US(timer);
    460 #endif
    461 	if (suspended)
    462 		return (RF_TRUE);
    463 
    464 	desc->state++;
    465 	return (RF_FALSE);
    466 }
    467 /*
    468  * the following three states create, execute, and post-process dags
    469  * the error recovery unit is a single dag.
    470  * by default, SelectAlgorithm creates an array of dags, one per parity stripe
    471  * in some tricky cases, multiple dags per stripe are created
    472  *   - dags within a parity stripe are executed sequentially (arbitrary order)
    473  *   - dags for distinct parity stripes are executed concurrently
    474  *
    475  * repeat until all dags complete successfully -or- dag selection fails
    476  *
    477  * while !done
    478  *   create dag(s) (SelectAlgorithm)
    479  *   if dag
    480  *     execute dag (DispatchDAG)
    481  *     if dag successful
    482  *       done (SUCCESS)
    483  *     else
    484  *       !done (RETRY - start over with new dags)
    485  *   else
    486  *     done (FAIL)
    487  */
    488 int
    489 rf_State_CreateDAG(RF_RaidAccessDesc_t *desc)
    490 {
    491 #if RF_ACC_TRACE > 0
    492 	RF_AccTraceEntry_t *tracerec = &desc->tracerec;
    493 	RF_Etimer_t timer;
    494 #endif
    495 	RF_DagHeader_t *dag_h;
    496 	RF_DagList_t *dagList;
    497 	struct buf *bp;
    498 	int     i, selectStatus;
    499 
    500 	/* generate a dag for the access, and fire it off.  When the dag
    501 	 * completes, we'll get re-invoked in the next state. */
    502 #if RF_ACC_TRACE > 0
    503 	RF_ETIMER_START(timer);
    504 #endif
    505 	/* SelectAlgorithm returns one or more dags */
    506 	selectStatus = rf_SelectAlgorithm(desc, desc->flags | RF_DAG_SUPPRESS_LOCKS);
    507 #if RF_DEBUG_VALIDATE_DAG
    508 	if (rf_printDAGsDebug) {
    509 		dagList = desc->dagList;
    510 		for (i = 0; i < desc->numStripes; i++) {
    511 			rf_PrintDAGList(dagList->dags);
    512 			dagList = dagList->next;
    513 		}
    514 	}
    515 #endif /* RF_DEBUG_VALIDATE_DAG */
    516 #if RF_ACC_TRACE > 0
    517 	RF_ETIMER_STOP(timer);
    518 	RF_ETIMER_EVAL(timer);
    519 	/* update time to create all dags */
    520 	tracerec->specific.user.dag_create_us = RF_ETIMER_VAL_US(timer);
    521 #endif
    522 
    523 	desc->status = 0;	/* good status */
    524 
    525 	if (selectStatus || (desc->numRetries > RF_RETRY_THRESHOLD)) {
    526 		/* failed to create a dag */
    527 		/* this happens when there are too many faults or incomplete
    528 		 * dag libraries */
    529 		if (selectStatus) {
    530 			printf("raid%d: failed to create a dag. "
    531 			       "Too many component failures.\n",
    532 			       desc->raidPtr->raidid);
    533 		} else {
    534 			printf("raid%d: IO failed after %d retries.\n",
    535 			       desc->raidPtr->raidid, RF_RETRY_THRESHOLD);
    536 		}
    537 
    538 		desc->status = 1; /* bad status */
    539 		/* skip straight to rf_State_Cleanup() */
    540 		desc->state = rf_CleanupState;
    541 		bp = (struct buf *)desc->bp;
    542 		bp->b_error = EIO;
    543 		bp->b_resid = bp->b_bcount;
    544 	} else {
    545 		/* bind dags to desc */
    546 		dagList = desc->dagList;
    547 		for (i = 0; i < desc->numStripes; i++) {
    548 			dag_h = dagList->dags;
    549 			while (dag_h) {
    550 				dag_h->bp = (struct buf *) desc->bp;
    551 #if RF_ACC_TRACE > 0
    552 				dag_h->tracerec = tracerec;
    553 #endif
    554 				dag_h = dag_h->next;
    555 			}
    556 			dagList = dagList->next;
    557 		}
    558 		desc->flags |= RF_DAG_DISPATCH_RETURNED;
    559 		desc->state++;	/* next state should be rf_State_ExecuteDAG */
    560 	}
    561 	return RF_FALSE;
    562 }
    563 
    564 
    565 
    566 /* the access has an list of dagLists, one dagList per parity stripe.
    567  * fire the first dag in each parity stripe (dagList).
    568  * dags within a stripe (dagList) must be executed sequentially
    569  *  - this preserves atomic parity update
    570  * dags for independents parity groups (stripes) are fired concurrently */
    571 
    572 int
    573 rf_State_ExecuteDAG(RF_RaidAccessDesc_t *desc)
    574 {
    575 	int     i;
    576 	RF_DagHeader_t *dag_h;
    577 	RF_DagList_t *dagList;
    578 
    579 	/* next state is always rf_State_ProcessDAG important to do
    580 	 * this before firing the first dag (it may finish before we
    581 	 * leave this routine) */
    582 	desc->state++;
    583 
    584 	/* sweep dag array, a stripe at a time, firing the first dag
    585 	 * in each stripe */
    586 	dagList = desc->dagList;
    587 	for (i = 0; i < desc->numStripes; i++) {
    588 		RF_ASSERT(dagList->numDags > 0);
    589 		RF_ASSERT(dagList->numDagsDone == 0);
    590 		RF_ASSERT(dagList->numDagsFired == 0);
    591 #if RF_ACC_TRACE > 0
    592 		RF_ETIMER_START(dagList->tracerec.timer);
    593 #endif
    594 		/* fire first dag in this stripe */
    595 		dag_h = dagList->dags;
    596 		RF_ASSERT(dag_h);
    597 		dagList->numDagsFired++;
    598 		rf_DispatchDAG(dag_h, (void (*) (void *)) rf_ContinueDagAccess, dagList);
    599 		dagList = dagList->next;
    600 	}
    601 
    602 	/* the DAG will always call the callback, even if there was no
    603 	 * blocking, so we are always suspended in this state */
    604 	return RF_TRUE;
    605 }
    606 
    607 
    608 
    609 /* rf_State_ProcessDAG is entered when a dag completes.
    610  * first, check to all dags in the access have completed
    611  * if not, fire as many dags as possible */
    612 
    613 int
    614 rf_State_ProcessDAG(RF_RaidAccessDesc_t *desc)
    615 {
    616 	RF_AccessStripeMapHeader_t *asmh = desc->asmap;
    617 	RF_Raid_t *raidPtr = desc->raidPtr;
    618 	RF_DagHeader_t *dag_h;
    619 	int     i, j, done = RF_TRUE;
    620 	RF_DagList_t *dagList, *temp;
    621 
    622 	/* check to see if this is the last dag */
    623 	dagList = desc->dagList;
    624 	for (i = 0; i < desc->numStripes; i++) {
    625 		if (dagList->numDags != dagList->numDagsDone)
    626 			done = RF_FALSE;
    627 		dagList = dagList->next;
    628 	}
    629 
    630 	if (done) {
    631 		if (desc->status) {
    632 			/* a dag failed, retry */
    633 			/* free all dags */
    634 			dagList = desc->dagList;
    635 			for (i = 0; i < desc->numStripes; i++) {
    636 				rf_FreeDAG(dagList->dags);
    637 				temp = dagList;
    638 				dagList = dagList->next;
    639 				rf_FreeDAGList(temp);
    640 			}
    641 			desc->dagList = NULL;
    642 
    643 			rf_MarkFailuresInASMList(raidPtr, asmh);
    644 
    645 			/* note the retry so that we'll bail in
    646 			   rf_State_CreateDAG() once we've retired
    647 			   the IO RF_RETRY_THRESHOLD times */
    648 
    649 			desc->numRetries++;
    650 
    651 			/* back up to rf_State_CreateDAG */
    652 			desc->state = desc->state - 2;
    653 			return RF_FALSE;
    654 		} else {
    655 			/* move on to rf_State_Cleanup */
    656 			desc->state++;
    657 		}
    658 		return RF_FALSE;
    659 	} else {
    660 		/* more dags to execute */
    661 		/* see if any are ready to be fired.  if so, fire them */
    662 		/* don't fire the initial dag in a list, it's fired in
    663 		 * rf_State_ExecuteDAG */
    664 		dagList = desc->dagList;
    665 		for (i = 0; i < desc->numStripes; i++) {
    666 			if ((dagList->numDagsDone < dagList->numDags)
    667 			    && (dagList->numDagsDone == dagList->numDagsFired)
    668 			    && (dagList->numDagsFired > 0)) {
    669 #if RF_ACC_TRACE > 0
    670 				RF_ETIMER_START(dagList->tracerec.timer);
    671 #endif
    672 				/* fire next dag in this stripe */
    673 				/* first, skip to next dag awaiting execution */
    674 				dag_h = dagList->dags;
    675 				for (j = 0; j < dagList->numDagsDone; j++)
    676 					dag_h = dag_h->next;
    677 				dagList->numDagsFired++;
    678 				rf_DispatchDAG(dag_h, (void (*) (void *)) rf_ContinueDagAccess,
    679 				    dagList);
    680 			}
    681 			dagList = dagList->next;
    682 		}
    683 		return RF_TRUE;
    684 	}
    685 }
    686 /* only make it this far if all dags complete successfully */
    687 int
    688 rf_State_Cleanup(RF_RaidAccessDesc_t *desc)
    689 {
    690 #if RF_ACC_TRACE > 0
    691 	RF_AccTraceEntry_t *tracerec = &desc->tracerec;
    692 	RF_Etimer_t timer;
    693 #endif
    694 	RF_AccessStripeMapHeader_t *asmh = desc->asmap;
    695 	RF_Raid_t *raidPtr = desc->raidPtr;
    696 	RF_AccessStripeMap_t *asm_p;
    697 	RF_DagList_t *dagList;
    698 	int i;
    699 
    700 	desc->state++;
    701 
    702 #if RF_ACC_TRACE > 0
    703 	timer = tracerec->timer;
    704 	RF_ETIMER_STOP(timer);
    705 	RF_ETIMER_EVAL(timer);
    706 	tracerec->specific.user.dag_retry_us = RF_ETIMER_VAL_US(timer);
    707 
    708 	/* the RAID I/O is complete.  Clean up. */
    709 	tracerec->specific.user.dag_retry_us = 0;
    710 
    711 	RF_ETIMER_START(timer);
    712 #endif
    713 	/* free all dags */
    714 	dagList = desc->dagList;
    715 	for (i = 0; i < desc->numStripes; i++) {
    716 		rf_FreeDAG(dagList->dags);
    717 		dagList = dagList->next;
    718 	}
    719 #if RF_ACC_TRACE > 0
    720 	RF_ETIMER_STOP(timer);
    721 	RF_ETIMER_EVAL(timer);
    722 	tracerec->specific.user.cleanup_us = RF_ETIMER_VAL_US(timer);
    723 
    724 	RF_ETIMER_START(timer);
    725 #endif
    726 	for (asm_p = asmh->stripeMap; asm_p; asm_p = asm_p->next) {
    727 		if (!rf_suppressLocksAndLargeWrites &&
    728 		    asm_p->parityInfo &&
    729 		    !(desc->flags & RF_DAG_SUPPRESS_LOCKS)) {
    730 			RF_ASSERT_VALID_LOCKREQ(&asm_p->lockReqDesc);
    731 			rf_ReleaseStripeLock(raidPtr->lockTable,
    732 					     asm_p->stripeID,
    733 					     &asm_p->lockReqDesc);
    734 		}
    735 		if (asm_p->flags & RF_ASM_FLAGS_RECON_BLOCKED) {
    736 			rf_UnblockRecon(raidPtr, asm_p);
    737 		}
    738 	}
    739 #if RF_ACC_TRACE > 0
    740 	RF_ETIMER_STOP(timer);
    741 	RF_ETIMER_EVAL(timer);
    742 	tracerec->specific.user.lock_us += RF_ETIMER_VAL_US(timer);
    743 
    744 	RF_ETIMER_START(timer);
    745 #endif
    746 	rf_FreeAccessStripeMap(asmh);
    747 #if RF_ACC_TRACE > 0
    748 	RF_ETIMER_STOP(timer);
    749 	RF_ETIMER_EVAL(timer);
    750 	tracerec->specific.user.cleanup_us += RF_ETIMER_VAL_US(timer);
    751 
    752 	RF_ETIMER_STOP(desc->timer);
    753 	RF_ETIMER_EVAL(desc->timer);
    754 
    755 	timer = desc->tracerec.tot_timer;
    756 	RF_ETIMER_STOP(timer);
    757 	RF_ETIMER_EVAL(timer);
    758 	desc->tracerec.total_us = RF_ETIMER_VAL_US(timer);
    759 
    760 	rf_LogTraceRec(raidPtr, tracerec);
    761 #endif
    762 	desc->flags |= RF_DAG_ACCESS_COMPLETE;
    763 
    764 	return RF_FALSE;
    765 }
    766