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