Home | History | Annotate | Line # | Download | only in raidframe
rf_dagutils.c revision 1.30
      1 /*	$NetBSD: rf_dagutils.c,v 1.30 2004/03/05 02:53:55 oster Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Authors: Mark Holland, William V. Courtright II, Jim Zelenka
      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  * rf_dagutils.c -- utility routines for manipulating dags
     32  *
     33  *****************************************************************************/
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: rf_dagutils.c,v 1.30 2004/03/05 02:53:55 oster Exp $");
     37 
     38 #include <dev/raidframe/raidframevar.h>
     39 
     40 #include "rf_archs.h"
     41 #include "rf_threadstuff.h"
     42 #include "rf_raid.h"
     43 #include "rf_dag.h"
     44 #include "rf_dagutils.h"
     45 #include "rf_dagfuncs.h"
     46 #include "rf_general.h"
     47 #include "rf_map.h"
     48 #include "rf_shutdown.h"
     49 
     50 #define SNUM_DIFF(_a_,_b_) (((_a_)>(_b_))?((_a_)-(_b_)):((_b_)-(_a_)))
     51 
     52 const RF_RedFuncs_t rf_xorFuncs = {
     53 	rf_RegularXorFunc, "Reg Xr",
     54 rf_SimpleXorFunc, "Simple Xr"};
     55 
     56 const RF_RedFuncs_t rf_xorRecoveryFuncs = {
     57 	rf_RecoveryXorFunc, "Recovery Xr",
     58 rf_RecoveryXorFunc, "Recovery Xr"};
     59 
     60 #if RF_DEBUG_VALIDATE_DAG
     61 static void rf_RecurPrintDAG(RF_DagNode_t *, int, int);
     62 static void rf_PrintDAG(RF_DagHeader_t *);
     63 static int rf_ValidateBranch(RF_DagNode_t *, int *, int *,
     64 			     RF_DagNode_t **, int);
     65 static void rf_ValidateBranchVisitedBits(RF_DagNode_t *, int, int);
     66 static void rf_ValidateVisitedBits(RF_DagHeader_t *);
     67 #endif /* RF_DEBUG_VALIDATE_DAG */
     68 
     69 /******************************************************************************
     70  *
     71  * InitNode - initialize a dag node
     72  *
     73  * the size of the propList array is always the same as that of the
     74  * successors array.
     75  *
     76  *****************************************************************************/
     77 void
     78 rf_InitNode(RF_DagNode_t *node, RF_NodeStatus_t initstatus, int commit,
     79     int (*doFunc) (RF_DagNode_t *node),
     80     int (*undoFunc) (RF_DagNode_t *node),
     81     int (*wakeFunc) (RF_DagNode_t *node, int status),
     82     int nSucc, int nAnte, int nParam, int nResult,
     83     RF_DagHeader_t *hdr, char *name, RF_AllocListElem_t *alist)
     84 {
     85 	void  **ptrs;
     86 	int     nptrs;
     87 
     88 	if (nAnte > RF_MAX_ANTECEDENTS)
     89 		RF_PANIC();
     90 	node->status = initstatus;
     91 	node->commitNode = commit;
     92 	node->doFunc = doFunc;
     93 	node->undoFunc = undoFunc;
     94 	node->wakeFunc = wakeFunc;
     95 	node->numParams = nParam;
     96 	node->numResults = nResult;
     97 	node->numAntecedents = nAnte;
     98 	node->numAntDone = 0;
     99 	node->next = NULL;
    100 	node->numSuccedents = nSucc;
    101 	node->name = name;
    102 	node->dagHdr = hdr;
    103 	node->visited = 0;
    104 
    105 	/* allocate all the pointers with one call to malloc */
    106 	nptrs = nSucc + nAnte + nResult + nSucc;
    107 
    108 	if (nptrs <= RF_DAG_PTRCACHESIZE) {
    109 		/*
    110 	         * The dag_ptrs field of the node is basically some scribble
    111 	         * space to be used here. We could get rid of it, and always
    112 	         * allocate the range of pointers, but that's expensive. So,
    113 	         * we pick a "common case" size for the pointer cache. Hopefully,
    114 	         * we'll find that:
    115 	         * (1) Generally, nptrs doesn't exceed RF_DAG_PTRCACHESIZE by
    116 	         *     only a little bit (least efficient case)
    117 	         * (2) Generally, ntprs isn't a lot less than RF_DAG_PTRCACHESIZE
    118 	         *     (wasted memory)
    119 	         */
    120 		ptrs = (void **) node->dag_ptrs;
    121 	} else {
    122 		RF_MallocAndAdd(ptrs, nptrs * sizeof(void *),
    123 				(void **), alist);
    124 	}
    125 	node->succedents = (nSucc) ? (RF_DagNode_t **) ptrs : NULL;
    126 	node->antecedents = (nAnte) ? (RF_DagNode_t **) (ptrs + nSucc) : NULL;
    127 	node->results = (nResult) ? (void **) (ptrs + nSucc + nAnte) : NULL;
    128 	node->propList = (nSucc) ? (RF_PropHeader_t **) (ptrs + nSucc + nAnte + nResult) : NULL;
    129 
    130 	if (nParam) {
    131 		if (nParam <= RF_DAG_PARAMCACHESIZE) {
    132 			node->params = (RF_DagParam_t *) node->dag_params;
    133 		} else {
    134 			RF_MallocAndAdd(node->params,
    135 					nParam * sizeof(RF_DagParam_t),
    136 					(RF_DagParam_t *), alist);
    137 		}
    138 	} else {
    139 		node->params = NULL;
    140 	}
    141 }
    142 
    143 
    144 
    145 /******************************************************************************
    146  *
    147  * allocation and deallocation routines
    148  *
    149  *****************************************************************************/
    150 
    151 void
    152 rf_FreeDAG(RF_DagHeader_t *dag_h)
    153 {
    154 	RF_AccessStripeMapHeader_t *asmap, *t_asmap;
    155 	RF_DagHeader_t *nextDag;
    156 
    157 	while (dag_h) {
    158 		nextDag = dag_h->next;
    159 		rf_FreeAllocList(dag_h->allocList);
    160 		for (asmap = dag_h->asmList; asmap;) {
    161 			t_asmap = asmap;
    162 			asmap = asmap->next;
    163 			rf_FreeAccessStripeMap(t_asmap);
    164 		}
    165 		rf_FreeDAGHeader(dag_h);
    166 		dag_h = nextDag;
    167 	}
    168 }
    169 
    170 static struct pool rf_dagh_pool;
    171 #define RF_MAX_FREE_DAGH 128
    172 #define RF_MIN_FREE_DAGH  32
    173 
    174 static struct pool rf_daglist_pool;
    175 #define RF_MAX_FREE_DAGLIST 128
    176 #define RF_MIN_FREE_DAGLIST  32
    177 
    178 static struct pool rf_funclist_pool;
    179 #define RF_MAX_FREE_FUNCLIST 128
    180 #define RF_MIN_FREE_FUNCLIST  32
    181 
    182 static void rf_ShutdownDAGs(void *);
    183 static void
    184 rf_ShutdownDAGs(void *ignored)
    185 {
    186 	pool_destroy(&rf_dagh_pool);
    187 	pool_destroy(&rf_daglist_pool);
    188 	pool_destroy(&rf_funclist_pool);
    189 }
    190 
    191 int
    192 rf_ConfigureDAGs(RF_ShutdownList_t **listp)
    193 {
    194 
    195 	pool_init(&rf_dagh_pool, sizeof(RF_DagHeader_t), 0, 0, 0,
    196 		  "rf_dagh_pl", NULL);
    197 	pool_sethiwat(&rf_dagh_pool, RF_MAX_FREE_DAGH);
    198 	pool_prime(&rf_dagh_pool, RF_MIN_FREE_DAGH);
    199 	pool_setlowat(&rf_dagh_pool, RF_MIN_FREE_DAGH);
    200 
    201 	pool_init(&rf_daglist_pool, sizeof(RF_DagList_t), 0, 0, 0,
    202 		  "rf_daglist_pl", NULL);
    203 	pool_sethiwat(&rf_daglist_pool, RF_MAX_FREE_DAGLIST);
    204 	pool_prime(&rf_daglist_pool, RF_MIN_FREE_DAGLIST);
    205 	pool_setlowat(&rf_daglist_pool, RF_MIN_FREE_DAGLIST);
    206 
    207 	pool_init(&rf_funclist_pool, sizeof(RF_FuncList_t), 0, 0, 0,
    208 		  "rf_funcist_pl", NULL);
    209 	pool_sethiwat(&rf_funclist_pool, RF_MAX_FREE_FUNCLIST);
    210 	pool_prime(&rf_funclist_pool, RF_MIN_FREE_FUNCLIST);
    211 	pool_setlowat(&rf_funclist_pool, RF_MIN_FREE_FUNCLIST);
    212 
    213 	rf_ShutdownCreate(listp, rf_ShutdownDAGs, NULL);
    214 
    215 	return (0);
    216 }
    217 
    218 RF_DagHeader_t *
    219 rf_AllocDAGHeader()
    220 {
    221 	RF_DagHeader_t *dh;
    222 
    223 	dh = pool_get(&rf_dagh_pool, PR_WAITOK);
    224 	memset((char *) dh, 0, sizeof(RF_DagHeader_t));
    225 	return (dh);
    226 }
    227 
    228 void
    229 rf_FreeDAGHeader(RF_DagHeader_t * dh)
    230 {
    231 	pool_put(&rf_dagh_pool, dh);
    232 }
    233 
    234 RF_DagList_t *
    235 rf_AllocDAGList()
    236 {
    237 	RF_DagList_t *dagList;
    238 
    239 	dagList = pool_get(&rf_daglist_pool, PR_WAITOK);
    240 	memset(dagList, 0, sizeof(RF_DagList_t));
    241 
    242 	return (dagList);
    243 }
    244 
    245 void
    246 rf_FreeDAGList(RF_DagList_t *dagList)
    247 {
    248 	pool_put(&rf_daglist_pool, dagList);
    249 }
    250 
    251 RF_FuncList_t *
    252 rf_AllocFuncList()
    253 {
    254 	RF_FuncList_t *funcList;
    255 
    256 	funcList = pool_get(&rf_funclist_pool, PR_WAITOK);
    257 	memset(funcList, 0, sizeof(RF_FuncList_t));
    258 
    259 	return (funcList);
    260 }
    261 
    262 void
    263 rf_FreeFuncList(RF_FuncList_t *funcList)
    264 {
    265 	pool_put(&rf_funclist_pool, funcList);
    266 }
    267 
    268 
    269 
    270 /* allocates a buffer big enough to hold the data described by pda */
    271 void   *
    272 rf_AllocBuffer(RF_Raid_t *raidPtr, RF_DagHeader_t *dag_h,
    273 	       RF_PhysDiskAddr_t *pda, RF_AllocListElem_t *allocList)
    274 {
    275 	char   *p;
    276 
    277 	RF_MallocAndAdd(p, pda->numSector << raidPtr->logBytesPerSector,
    278 	    (char *), allocList);
    279 	return ((void *) p);
    280 }
    281 #if RF_DEBUG_VALIDATE_DAG
    282 /******************************************************************************
    283  *
    284  * debug routines
    285  *
    286  *****************************************************************************/
    287 
    288 char   *
    289 rf_NodeStatusString(RF_DagNode_t *node)
    290 {
    291 	switch (node->status) {
    292 		case rf_wait:return ("wait");
    293 	case rf_fired:
    294 		return ("fired");
    295 	case rf_good:
    296 		return ("good");
    297 	case rf_bad:
    298 		return ("bad");
    299 	default:
    300 		return ("?");
    301 	}
    302 }
    303 
    304 void
    305 rf_PrintNodeInfoString(RF_DagNode_t *node)
    306 {
    307 	RF_PhysDiskAddr_t *pda;
    308 	int     (*df) (RF_DagNode_t *) = node->doFunc;
    309 	int     i, lk, unlk;
    310 	void   *bufPtr;
    311 
    312 	if ((df == rf_DiskReadFunc) || (df == rf_DiskWriteFunc)
    313 	    || (df == rf_DiskReadMirrorIdleFunc)
    314 	    || (df == rf_DiskReadMirrorPartitionFunc)) {
    315 		pda = (RF_PhysDiskAddr_t *) node->params[0].p;
    316 		bufPtr = (void *) node->params[1].p;
    317 		lk = 0;
    318 		unlk = 0;
    319 		RF_ASSERT(!(lk && unlk));
    320 		printf("c %d offs %ld nsect %d buf 0x%lx %s\n", pda->col,
    321 		    (long) pda->startSector, (int) pda->numSector, (long) bufPtr,
    322 		    (lk) ? "LOCK" : ((unlk) ? "UNLK" : " "));
    323 		return;
    324 	}
    325 	if (df == rf_DiskUnlockFunc) {
    326 		pda = (RF_PhysDiskAddr_t *) node->params[0].p;
    327 		lk = 0;
    328 		unlk = 0;
    329 		RF_ASSERT(!(lk && unlk));
    330 		printf("c %d %s\n", pda->col,
    331 		    (lk) ? "LOCK" : ((unlk) ? "UNLK" : "nop"));
    332 		return;
    333 	}
    334 	if ((df == rf_SimpleXorFunc) || (df == rf_RegularXorFunc)
    335 	    || (df == rf_RecoveryXorFunc)) {
    336 		printf("result buf 0x%lx\n", (long) node->results[0]);
    337 		for (i = 0; i < node->numParams - 1; i += 2) {
    338 			pda = (RF_PhysDiskAddr_t *) node->params[i].p;
    339 			bufPtr = (RF_PhysDiskAddr_t *) node->params[i + 1].p;
    340 			printf("    buf 0x%lx c%d offs %ld nsect %d\n",
    341 			    (long) bufPtr, pda->col,
    342 			    (long) pda->startSector, (int) pda->numSector);
    343 		}
    344 		return;
    345 	}
    346 #if RF_INCLUDE_PARITYLOGGING > 0
    347 	if (df == rf_ParityLogOverwriteFunc || df == rf_ParityLogUpdateFunc) {
    348 		for (i = 0; i < node->numParams - 1; i += 2) {
    349 			pda = (RF_PhysDiskAddr_t *) node->params[i].p;
    350 			bufPtr = (RF_PhysDiskAddr_t *) node->params[i + 1].p;
    351 			printf(" c%d offs %ld nsect %d buf 0x%lx\n",
    352 			    pda->col, (long) pda->startSector,
    353 			    (int) pda->numSector, (long) bufPtr);
    354 		}
    355 		return;
    356 	}
    357 #endif				/* RF_INCLUDE_PARITYLOGGING > 0 */
    358 
    359 	if ((df == rf_TerminateFunc) || (df == rf_NullNodeFunc)) {
    360 		printf("\n");
    361 		return;
    362 	}
    363 	printf("?\n");
    364 }
    365 #ifdef DEBUG
    366 static void
    367 rf_RecurPrintDAG(RF_DagNode_t *node, int depth, int unvisited)
    368 {
    369 	char   *anttype;
    370 	int     i;
    371 
    372 	node->visited = (unvisited) ? 0 : 1;
    373 	printf("(%d) %d C%d %s: %s,s%d %d/%d,a%d/%d,p%d,r%d S{", depth,
    374 	    node->nodeNum, node->commitNode, node->name, rf_NodeStatusString(node),
    375 	    node->numSuccedents, node->numSuccFired, node->numSuccDone,
    376 	    node->numAntecedents, node->numAntDone, node->numParams, node->numResults);
    377 	for (i = 0; i < node->numSuccedents; i++) {
    378 		printf("%d%s", node->succedents[i]->nodeNum,
    379 		    ((i == node->numSuccedents - 1) ? "\0" : " "));
    380 	}
    381 	printf("} A{");
    382 	for (i = 0; i < node->numAntecedents; i++) {
    383 		switch (node->antType[i]) {
    384 		case rf_trueData:
    385 			anttype = "T";
    386 			break;
    387 		case rf_antiData:
    388 			anttype = "A";
    389 			break;
    390 		case rf_outputData:
    391 			anttype = "O";
    392 			break;
    393 		case rf_control:
    394 			anttype = "C";
    395 			break;
    396 		default:
    397 			anttype = "?";
    398 			break;
    399 		}
    400 		printf("%d(%s)%s", node->antecedents[i]->nodeNum, anttype, (i == node->numAntecedents - 1) ? "\0" : " ");
    401 	}
    402 	printf("}; ");
    403 	rf_PrintNodeInfoString(node);
    404 	for (i = 0; i < node->numSuccedents; i++) {
    405 		if (node->succedents[i]->visited == unvisited)
    406 			rf_RecurPrintDAG(node->succedents[i], depth + 1, unvisited);
    407 	}
    408 }
    409 
    410 static void
    411 rf_PrintDAG(RF_DagHeader_t *dag_h)
    412 {
    413 	int     unvisited, i;
    414 	char   *status;
    415 
    416 	/* set dag status */
    417 	switch (dag_h->status) {
    418 	case rf_enable:
    419 		status = "enable";
    420 		break;
    421 	case rf_rollForward:
    422 		status = "rollForward";
    423 		break;
    424 	case rf_rollBackward:
    425 		status = "rollBackward";
    426 		break;
    427 	default:
    428 		status = "illegal!";
    429 		break;
    430 	}
    431 	/* find out if visited bits are currently set or clear */
    432 	unvisited = dag_h->succedents[0]->visited;
    433 
    434 	printf("DAG type:  %s\n", dag_h->creator);
    435 	printf("format is (depth) num commit type: status,nSucc nSuccFired/nSuccDone,nAnte/nAnteDone,nParam,nResult S{x} A{x(type)};  info\n");
    436 	printf("(0) %d Hdr: %s, s%d, (commit %d/%d) S{", dag_h->nodeNum,
    437 	    status, dag_h->numSuccedents, dag_h->numCommitNodes, dag_h->numCommits);
    438 	for (i = 0; i < dag_h->numSuccedents; i++) {
    439 		printf("%d%s", dag_h->succedents[i]->nodeNum,
    440 		    ((i == dag_h->numSuccedents - 1) ? "\0" : " "));
    441 	}
    442 	printf("};\n");
    443 	for (i = 0; i < dag_h->numSuccedents; i++) {
    444 		if (dag_h->succedents[i]->visited == unvisited)
    445 			rf_RecurPrintDAG(dag_h->succedents[i], 1, unvisited);
    446 	}
    447 }
    448 #endif
    449 /* assigns node numbers */
    450 int
    451 rf_AssignNodeNums(RF_DagHeader_t * dag_h)
    452 {
    453 	int     unvisited, i, nnum;
    454 	RF_DagNode_t *node;
    455 
    456 	nnum = 0;
    457 	unvisited = dag_h->succedents[0]->visited;
    458 
    459 	dag_h->nodeNum = nnum++;
    460 	for (i = 0; i < dag_h->numSuccedents; i++) {
    461 		node = dag_h->succedents[i];
    462 		if (node->visited == unvisited) {
    463 			nnum = rf_RecurAssignNodeNums(dag_h->succedents[i], nnum, unvisited);
    464 		}
    465 	}
    466 	return (nnum);
    467 }
    468 
    469 int
    470 rf_RecurAssignNodeNums(RF_DagNode_t *node, int num, int unvisited)
    471 {
    472 	int     i;
    473 
    474 	node->visited = (unvisited) ? 0 : 1;
    475 
    476 	node->nodeNum = num++;
    477 	for (i = 0; i < node->numSuccedents; i++) {
    478 		if (node->succedents[i]->visited == unvisited) {
    479 			num = rf_RecurAssignNodeNums(node->succedents[i], num, unvisited);
    480 		}
    481 	}
    482 	return (num);
    483 }
    484 /* set the header pointers in each node to "newptr" */
    485 void
    486 rf_ResetDAGHeaderPointers(RF_DagHeader_t *dag_h, RF_DagHeader_t *newptr)
    487 {
    488 	int     i;
    489 	for (i = 0; i < dag_h->numSuccedents; i++)
    490 		if (dag_h->succedents[i]->dagHdr != newptr)
    491 			rf_RecurResetDAGHeaderPointers(dag_h->succedents[i], newptr);
    492 }
    493 
    494 void
    495 rf_RecurResetDAGHeaderPointers(RF_DagNode_t *node, RF_DagHeader_t *newptr)
    496 {
    497 	int     i;
    498 	node->dagHdr = newptr;
    499 	for (i = 0; i < node->numSuccedents; i++)
    500 		if (node->succedents[i]->dagHdr != newptr)
    501 			rf_RecurResetDAGHeaderPointers(node->succedents[i], newptr);
    502 }
    503 
    504 
    505 void
    506 rf_PrintDAGList(RF_DagHeader_t * dag_h)
    507 {
    508 	int     i = 0;
    509 
    510 	for (; dag_h; dag_h = dag_h->next) {
    511 		rf_AssignNodeNums(dag_h);
    512 		printf("\n\nDAG %d IN LIST:\n", i++);
    513 		rf_PrintDAG(dag_h);
    514 	}
    515 }
    516 
    517 static int
    518 rf_ValidateBranch(RF_DagNode_t *node, int *scount, int *acount,
    519 		  RF_DagNode_t **nodes, int unvisited)
    520 {
    521 	int     i, retcode = 0;
    522 
    523 	/* construct an array of node pointers indexed by node num */
    524 	node->visited = (unvisited) ? 0 : 1;
    525 	nodes[node->nodeNum] = node;
    526 
    527 	if (node->next != NULL) {
    528 		printf("INVALID DAG: next pointer in node is not NULL\n");
    529 		retcode = 1;
    530 	}
    531 	if (node->status != rf_wait) {
    532 		printf("INVALID DAG: Node status is not wait\n");
    533 		retcode = 1;
    534 	}
    535 	if (node->numAntDone != 0) {
    536 		printf("INVALID DAG: numAntDone is not zero\n");
    537 		retcode = 1;
    538 	}
    539 	if (node->doFunc == rf_TerminateFunc) {
    540 		if (node->numSuccedents != 0) {
    541 			printf("INVALID DAG: Terminator node has succedents\n");
    542 			retcode = 1;
    543 		}
    544 	} else {
    545 		if (node->numSuccedents == 0) {
    546 			printf("INVALID DAG: Non-terminator node has no succedents\n");
    547 			retcode = 1;
    548 		}
    549 	}
    550 	for (i = 0; i < node->numSuccedents; i++) {
    551 		if (!node->succedents[i]) {
    552 			printf("INVALID DAG: succedent %d of node %s is NULL\n", i, node->name);
    553 			retcode = 1;
    554 		}
    555 		scount[node->succedents[i]->nodeNum]++;
    556 	}
    557 	for (i = 0; i < node->numAntecedents; i++) {
    558 		if (!node->antecedents[i]) {
    559 			printf("INVALID DAG: antecedent %d of node %s is NULL\n", i, node->name);
    560 			retcode = 1;
    561 		}
    562 		acount[node->antecedents[i]->nodeNum]++;
    563 	}
    564 	for (i = 0; i < node->numSuccedents; i++) {
    565 		if (node->succedents[i]->visited == unvisited) {
    566 			if (rf_ValidateBranch(node->succedents[i], scount,
    567 				acount, nodes, unvisited)) {
    568 				retcode = 1;
    569 			}
    570 		}
    571 	}
    572 	return (retcode);
    573 }
    574 
    575 static void
    576 rf_ValidateBranchVisitedBits(RF_DagNode_t *node, int unvisited, int rl)
    577 {
    578 	int     i;
    579 
    580 	RF_ASSERT(node->visited == unvisited);
    581 	for (i = 0; i < node->numSuccedents; i++) {
    582 		if (node->succedents[i] == NULL) {
    583 			printf("node=%lx node->succedents[%d] is NULL\n", (long) node, i);
    584 			RF_ASSERT(0);
    585 		}
    586 		rf_ValidateBranchVisitedBits(node->succedents[i], unvisited, rl + 1);
    587 	}
    588 }
    589 /* NOTE:  never call this on a big dag, because it is exponential
    590  * in execution time
    591  */
    592 static void
    593 rf_ValidateVisitedBits(RF_DagHeader_t *dag)
    594 {
    595 	int     i, unvisited;
    596 
    597 	unvisited = dag->succedents[0]->visited;
    598 
    599 	for (i = 0; i < dag->numSuccedents; i++) {
    600 		if (dag->succedents[i] == NULL) {
    601 			printf("dag=%lx dag->succedents[%d] is NULL\n", (long) dag, i);
    602 			RF_ASSERT(0);
    603 		}
    604 		rf_ValidateBranchVisitedBits(dag->succedents[i], unvisited, 0);
    605 	}
    606 }
    607 /* validate a DAG.  _at entry_ verify that:
    608  *   -- numNodesCompleted is zero
    609  *   -- node queue is null
    610  *   -- dag status is rf_enable
    611  *   -- next pointer is null on every node
    612  *   -- all nodes have status wait
    613  *   -- numAntDone is zero in all nodes
    614  *   -- terminator node has zero successors
    615  *   -- no other node besides terminator has zero successors
    616  *   -- no successor or antecedent pointer in a node is NULL
    617  *   -- number of times that each node appears as a successor of another node
    618  *      is equal to the antecedent count on that node
    619  *   -- number of times that each node appears as an antecedent of another node
    620  *      is equal to the succedent count on that node
    621  *   -- what else?
    622  */
    623 int
    624 rf_ValidateDAG(RF_DagHeader_t *dag_h)
    625 {
    626 	int     i, nodecount;
    627 	int    *scount, *acount;/* per-node successor and antecedent counts */
    628 	RF_DagNode_t **nodes;	/* array of ptrs to nodes in dag */
    629 	int     retcode = 0;
    630 	int     unvisited;
    631 	int     commitNodeCount = 0;
    632 
    633 	if (rf_validateVisitedDebug)
    634 		rf_ValidateVisitedBits(dag_h);
    635 
    636 	if (dag_h->numNodesCompleted != 0) {
    637 		printf("INVALID DAG: num nodes completed is %d, should be 0\n", dag_h->numNodesCompleted);
    638 		retcode = 1;
    639 		goto validate_dag_bad;
    640 	}
    641 	if (dag_h->status != rf_enable) {
    642 		printf("INVALID DAG: not enabled\n");
    643 		retcode = 1;
    644 		goto validate_dag_bad;
    645 	}
    646 	if (dag_h->numCommits != 0) {
    647 		printf("INVALID DAG: numCommits != 0 (%d)\n", dag_h->numCommits);
    648 		retcode = 1;
    649 		goto validate_dag_bad;
    650 	}
    651 	if (dag_h->numSuccedents != 1) {
    652 		/* currently, all dags must have only one succedent */
    653 		printf("INVALID DAG: numSuccedents !1 (%d)\n", dag_h->numSuccedents);
    654 		retcode = 1;
    655 		goto validate_dag_bad;
    656 	}
    657 	nodecount = rf_AssignNodeNums(dag_h);
    658 
    659 	unvisited = dag_h->succedents[0]->visited;
    660 
    661 	RF_Malloc(scount, nodecount * sizeof(int), (int *));
    662 	RF_Malloc(acount, nodecount * sizeof(int), (int *));
    663 	RF_Malloc(nodes, nodecount * sizeof(RF_DagNode_t *),
    664 		  (RF_DagNode_t **));
    665 	for (i = 0; i < dag_h->numSuccedents; i++) {
    666 		if ((dag_h->succedents[i]->visited == unvisited)
    667 		    && rf_ValidateBranch(dag_h->succedents[i], scount,
    668 			acount, nodes, unvisited)) {
    669 			retcode = 1;
    670 		}
    671 	}
    672 	/* start at 1 to skip the header node */
    673 	for (i = 1; i < nodecount; i++) {
    674 		if (nodes[i]->commitNode)
    675 			commitNodeCount++;
    676 		if (nodes[i]->doFunc == NULL) {
    677 			printf("INVALID DAG: node %s has an undefined doFunc\n", nodes[i]->name);
    678 			retcode = 1;
    679 			goto validate_dag_out;
    680 		}
    681 		if (nodes[i]->undoFunc == NULL) {
    682 			printf("INVALID DAG: node %s has an undefined doFunc\n", nodes[i]->name);
    683 			retcode = 1;
    684 			goto validate_dag_out;
    685 		}
    686 		if (nodes[i]->numAntecedents != scount[nodes[i]->nodeNum]) {
    687 			printf("INVALID DAG: node %s has %d antecedents but appears as a succedent %d times\n",
    688 			    nodes[i]->name, nodes[i]->numAntecedents, scount[nodes[i]->nodeNum]);
    689 			retcode = 1;
    690 			goto validate_dag_out;
    691 		}
    692 		if (nodes[i]->numSuccedents != acount[nodes[i]->nodeNum]) {
    693 			printf("INVALID DAG: node %s has %d succedents but appears as an antecedent %d times\n",
    694 			    nodes[i]->name, nodes[i]->numSuccedents, acount[nodes[i]->nodeNum]);
    695 			retcode = 1;
    696 			goto validate_dag_out;
    697 		}
    698 	}
    699 
    700 	if (dag_h->numCommitNodes != commitNodeCount) {
    701 		printf("INVALID DAG: incorrect commit node count.  hdr->numCommitNodes (%d) found (%d) commit nodes in graph\n",
    702 		    dag_h->numCommitNodes, commitNodeCount);
    703 		retcode = 1;
    704 		goto validate_dag_out;
    705 	}
    706 validate_dag_out:
    707 	RF_Free(scount, nodecount * sizeof(int));
    708 	RF_Free(acount, nodecount * sizeof(int));
    709 	RF_Free(nodes, nodecount * sizeof(RF_DagNode_t *));
    710 	if (retcode)
    711 		rf_PrintDAGList(dag_h);
    712 
    713 	if (rf_validateVisitedDebug)
    714 		rf_ValidateVisitedBits(dag_h);
    715 
    716 	return (retcode);
    717 
    718 validate_dag_bad:
    719 	rf_PrintDAGList(dag_h);
    720 	return (retcode);
    721 }
    722 
    723 #endif /* RF_DEBUG_VALIDATE_DAG */
    724 
    725 /******************************************************************************
    726  *
    727  * misc construction routines
    728  *
    729  *****************************************************************************/
    730 
    731 void
    732 rf_redirect_asm(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap)
    733 {
    734 	int     ds = (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE) ? 1 : 0;
    735 	int     fcol = raidPtr->reconControl->fcol;
    736 	int     scol = raidPtr->reconControl->spareCol;
    737 	RF_PhysDiskAddr_t *pda;
    738 
    739 	RF_ASSERT(raidPtr->status == rf_rs_reconstructing);
    740 	for (pda = asmap->physInfo; pda; pda = pda->next) {
    741 		if (pda->col == fcol) {
    742 			if (rf_dagDebug) {
    743 				if (!rf_CheckRUReconstructed(raidPtr->reconControl->reconMap,
    744 					pda->startSector)) {
    745 					RF_PANIC();
    746 				}
    747 			}
    748 			/* printf("Remapped data for large write\n"); */
    749 			if (ds) {
    750 				raidPtr->Layout.map->MapSector(raidPtr, pda->raidAddress,
    751 				    &pda->col, &pda->startSector, RF_REMAP);
    752 			} else {
    753 				pda->col = scol;
    754 			}
    755 		}
    756 	}
    757 	for (pda = asmap->parityInfo; pda; pda = pda->next) {
    758 		if (pda->col == fcol) {
    759 			if (rf_dagDebug) {
    760 				if (!rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, pda->startSector)) {
    761 					RF_PANIC();
    762 				}
    763 			}
    764 		}
    765 		if (ds) {
    766 			(raidPtr->Layout.map->MapParity) (raidPtr, pda->raidAddress, &pda->col, &pda->startSector, RF_REMAP);
    767 		} else {
    768 			pda->col = scol;
    769 		}
    770 	}
    771 }
    772 
    773 
    774 /* this routine allocates read buffers and generates stripe maps for the
    775  * regions of the array from the start of the stripe to the start of the
    776  * access, and from the end of the access to the end of the stripe.  It also
    777  * computes and returns the number of DAG nodes needed to read all this data.
    778  * Note that this routine does the wrong thing if the access is fully
    779  * contained within one stripe unit, so we RF_ASSERT against this case at the
    780  * start.
    781  *
    782  * layoutPtr - in: layout information
    783  * asmap     - in: access stripe map
    784  * dag_h     - in: header of the dag to create
    785  * new_asm_h - in: ptr to array of 2 headers.  to be filled in
    786  * nRodNodes - out: num nodes to be generated to read unaccessed data
    787  * sosBuffer, eosBuffer - out: pointers to newly allocated buffer
    788  */
    789 void
    790 rf_MapUnaccessedPortionOfStripe(RF_Raid_t *raidPtr,
    791 				RF_RaidLayout_t *layoutPtr,
    792 				RF_AccessStripeMap_t *asmap,
    793 				RF_DagHeader_t *dag_h,
    794 				RF_AccessStripeMapHeader_t **new_asm_h,
    795 				int *nRodNodes,
    796 				char **sosBuffer, char **eosBuffer,
    797 				RF_AllocListElem_t *allocList)
    798 {
    799 	RF_RaidAddr_t sosRaidAddress, eosRaidAddress;
    800 	RF_SectorNum_t sosNumSector, eosNumSector;
    801 
    802 	RF_ASSERT(asmap->numStripeUnitsAccessed > (layoutPtr->numDataCol / 2));
    803 	/* generate an access map for the region of the array from start of
    804 	 * stripe to start of access */
    805 	new_asm_h[0] = new_asm_h[1] = NULL;
    806 	*nRodNodes = 0;
    807 	if (!rf_RaidAddressStripeAligned(layoutPtr, asmap->raidAddress)) {
    808 		sosRaidAddress = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, asmap->raidAddress);
    809 		sosNumSector = asmap->raidAddress - sosRaidAddress;
    810 		RF_MallocAndAdd(*sosBuffer, rf_RaidAddressToByte(raidPtr, sosNumSector), (char *), allocList);
    811 		new_asm_h[0] = rf_MapAccess(raidPtr, sosRaidAddress, sosNumSector, *sosBuffer, RF_DONT_REMAP);
    812 		new_asm_h[0]->next = dag_h->asmList;
    813 		dag_h->asmList = new_asm_h[0];
    814 		*nRodNodes += new_asm_h[0]->stripeMap->numStripeUnitsAccessed;
    815 
    816 		RF_ASSERT(new_asm_h[0]->stripeMap->next == NULL);
    817 		/* we're totally within one stripe here */
    818 		if (asmap->flags & RF_ASM_REDIR_LARGE_WRITE)
    819 			rf_redirect_asm(raidPtr, new_asm_h[0]->stripeMap);
    820 	}
    821 	/* generate an access map for the region of the array from end of
    822 	 * access to end of stripe */
    823 	if (!rf_RaidAddressStripeAligned(layoutPtr, asmap->endRaidAddress)) {
    824 		eosRaidAddress = asmap->endRaidAddress;
    825 		eosNumSector = rf_RaidAddressOfNextStripeBoundary(layoutPtr, eosRaidAddress) - eosRaidAddress;
    826 		RF_MallocAndAdd(*eosBuffer, rf_RaidAddressToByte(raidPtr, eosNumSector), (char *), allocList);
    827 		new_asm_h[1] = rf_MapAccess(raidPtr, eosRaidAddress, eosNumSector, *eosBuffer, RF_DONT_REMAP);
    828 		new_asm_h[1]->next = dag_h->asmList;
    829 		dag_h->asmList = new_asm_h[1];
    830 		*nRodNodes += new_asm_h[1]->stripeMap->numStripeUnitsAccessed;
    831 
    832 		RF_ASSERT(new_asm_h[1]->stripeMap->next == NULL);
    833 		/* we're totally within one stripe here */
    834 		if (asmap->flags & RF_ASM_REDIR_LARGE_WRITE)
    835 			rf_redirect_asm(raidPtr, new_asm_h[1]->stripeMap);
    836 	}
    837 }
    838 
    839 
    840 
    841 /* returns non-zero if the indicated ranges of stripe unit offsets overlap */
    842 int
    843 rf_PDAOverlap(RF_RaidLayout_t *layoutPtr,
    844 	      RF_PhysDiskAddr_t *src, RF_PhysDiskAddr_t *dest)
    845 {
    846 	RF_SectorNum_t soffs = rf_StripeUnitOffset(layoutPtr, src->startSector);
    847 	RF_SectorNum_t doffs = rf_StripeUnitOffset(layoutPtr, dest->startSector);
    848 	/* use -1 to be sure we stay within SU */
    849 	RF_SectorNum_t send = rf_StripeUnitOffset(layoutPtr, src->startSector + src->numSector - 1);
    850 	RF_SectorNum_t dend = rf_StripeUnitOffset(layoutPtr, dest->startSector + dest->numSector - 1);
    851 	return ((RF_MAX(soffs, doffs) <= RF_MIN(send, dend)) ? 1 : 0);
    852 }
    853 
    854 
    855 /* GenerateFailedAccessASMs
    856  *
    857  * this routine figures out what portion of the stripe needs to be read
    858  * to effect the degraded read or write operation.  It's primary function
    859  * is to identify everything required to recover the data, and then
    860  * eliminate anything that is already being accessed by the user.
    861  *
    862  * The main result is two new ASMs, one for the region from the start of the
    863  * stripe to the start of the access, and one for the region from the end of
    864  * the access to the end of the stripe.  These ASMs describe everything that
    865  * needs to be read to effect the degraded access.  Other results are:
    866  *    nXorBufs -- the total number of buffers that need to be XORed together to
    867  *                recover the lost data,
    868  *    rpBufPtr -- ptr to a newly-allocated buffer to hold the parity.  If NULL
    869  *                at entry, not allocated.
    870  *    overlappingPDAs --
    871  *                describes which of the non-failed PDAs in the user access
    872  *                overlap data that needs to be read to effect recovery.
    873  *                overlappingPDAs[i]==1 if and only if, neglecting the failed
    874  *                PDA, the ith pda in the input asm overlaps data that needs
    875  *                to be read for recovery.
    876  */
    877  /* in: asm - ASM for the actual access, one stripe only */
    878  /* in: failedPDA - which component of the access has failed */
    879  /* in: dag_h - header of the DAG we're going to create */
    880  /* out: new_asm_h - the two new ASMs */
    881  /* out: nXorBufs - the total number of xor bufs required */
    882  /* out: rpBufPtr - a buffer for the parity read */
    883 void
    884 rf_GenerateFailedAccessASMs(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap,
    885 			    RF_PhysDiskAddr_t *failedPDA,
    886 			    RF_DagHeader_t *dag_h,
    887 			    RF_AccessStripeMapHeader_t **new_asm_h,
    888 			    int *nXorBufs, char **rpBufPtr,
    889 			    char *overlappingPDAs,
    890 			    RF_AllocListElem_t *allocList)
    891 {
    892 	RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout);
    893 
    894 	/* s=start, e=end, s=stripe, a=access, f=failed, su=stripe unit */
    895 	RF_RaidAddr_t sosAddr, sosEndAddr, eosStartAddr, eosAddr;
    896 
    897 	RF_SectorCount_t numSect[2], numParitySect;
    898 	RF_PhysDiskAddr_t *pda;
    899 	char   *rdBuf, *bufP;
    900 	int     foundit, i;
    901 
    902 	bufP = NULL;
    903 	foundit = 0;
    904 	/* first compute the following raid addresses: start of stripe,
    905 	 * (sosAddr) MIN(start of access, start of failed SU),   (sosEndAddr)
    906 	 * MAX(end of access, end of failed SU),       (eosStartAddr) end of
    907 	 * stripe (i.e. start of next stripe)   (eosAddr) */
    908 	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, asmap->raidAddress);
    909 	sosEndAddr = RF_MIN(asmap->raidAddress, rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, failedPDA->raidAddress));
    910 	eosStartAddr = RF_MAX(asmap->endRaidAddress, rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr, failedPDA->raidAddress));
    911 	eosAddr = rf_RaidAddressOfNextStripeBoundary(layoutPtr, asmap->raidAddress);
    912 
    913 	/* now generate access stripe maps for each of the above regions of
    914 	 * the stripe.  Use a dummy (NULL) buf ptr for now */
    915 
    916 	new_asm_h[0] = (sosAddr != sosEndAddr) ? rf_MapAccess(raidPtr, sosAddr, sosEndAddr - sosAddr, NULL, RF_DONT_REMAP) : NULL;
    917 	new_asm_h[1] = (eosStartAddr != eosAddr) ? rf_MapAccess(raidPtr, eosStartAddr, eosAddr - eosStartAddr, NULL, RF_DONT_REMAP) : NULL;
    918 
    919 	/* walk through the PDAs and range-restrict each SU to the region of
    920 	 * the SU touched on the failed PDA.  also compute total data buffer
    921 	 * space requirements in this step.  Ignore the parity for now. */
    922 
    923 	numSect[0] = numSect[1] = 0;
    924 	if (new_asm_h[0]) {
    925 		new_asm_h[0]->next = dag_h->asmList;
    926 		dag_h->asmList = new_asm_h[0];
    927 		for (pda = new_asm_h[0]->stripeMap->physInfo; pda; pda = pda->next) {
    928 			rf_RangeRestrictPDA(raidPtr, failedPDA, pda, RF_RESTRICT_NOBUFFER, 0);
    929 			numSect[0] += pda->numSector;
    930 		}
    931 	}
    932 	if (new_asm_h[1]) {
    933 		new_asm_h[1]->next = dag_h->asmList;
    934 		dag_h->asmList = new_asm_h[1];
    935 		for (pda = new_asm_h[1]->stripeMap->physInfo; pda; pda = pda->next) {
    936 			rf_RangeRestrictPDA(raidPtr, failedPDA, pda, RF_RESTRICT_NOBUFFER, 0);
    937 			numSect[1] += pda->numSector;
    938 		}
    939 	}
    940 	numParitySect = failedPDA->numSector;
    941 
    942 	/* allocate buffer space for the data & parity we have to read to
    943 	 * recover from the failure */
    944 
    945 	if (numSect[0] + numSect[1] + ((rpBufPtr) ? numParitySect : 0)) {	/* don't allocate parity
    946 										 * buf if not needed */
    947 		RF_MallocAndAdd(rdBuf, rf_RaidAddressToByte(raidPtr, numSect[0] + numSect[1] + numParitySect), (char *), allocList);
    948 		bufP = rdBuf;
    949 		if (rf_degDagDebug)
    950 			printf("Newly allocated buffer (%d bytes) is 0x%lx\n",
    951 			    (int) rf_RaidAddressToByte(raidPtr, numSect[0] + numSect[1] + numParitySect), (unsigned long) bufP);
    952 	}
    953 	/* now walk through the pdas one last time and assign buffer pointers
    954 	 * (ugh!).  Again, ignore the parity.  also, count nodes to find out
    955 	 * how many bufs need to be xored together */
    956 	(*nXorBufs) = 1;	/* in read case, 1 is for parity.  In write
    957 				 * case, 1 is for failed data */
    958 	if (new_asm_h[0]) {
    959 		for (pda = new_asm_h[0]->stripeMap->physInfo; pda; pda = pda->next) {
    960 			pda->bufPtr = bufP;
    961 			bufP += rf_RaidAddressToByte(raidPtr, pda->numSector);
    962 		}
    963 		*nXorBufs += new_asm_h[0]->stripeMap->numStripeUnitsAccessed;
    964 	}
    965 	if (new_asm_h[1]) {
    966 		for (pda = new_asm_h[1]->stripeMap->physInfo; pda; pda = pda->next) {
    967 			pda->bufPtr = bufP;
    968 			bufP += rf_RaidAddressToByte(raidPtr, pda->numSector);
    969 		}
    970 		(*nXorBufs) += new_asm_h[1]->stripeMap->numStripeUnitsAccessed;
    971 	}
    972 	if (rpBufPtr)
    973 		*rpBufPtr = bufP;	/* the rest of the buffer is for
    974 					 * parity */
    975 
    976 	/* the last step is to figure out how many more distinct buffers need
    977 	 * to get xor'd to produce the missing unit.  there's one for each
    978 	 * user-data read node that overlaps the portion of the failed unit
    979 	 * being accessed */
    980 
    981 	for (foundit = i = 0, pda = asmap->physInfo; pda; i++, pda = pda->next) {
    982 		if (pda == failedPDA) {
    983 			i--;
    984 			foundit = 1;
    985 			continue;
    986 		}
    987 		if (rf_PDAOverlap(layoutPtr, pda, failedPDA)) {
    988 			overlappingPDAs[i] = 1;
    989 			(*nXorBufs)++;
    990 		}
    991 	}
    992 	if (!foundit) {
    993 		RF_ERRORMSG("GenerateFailedAccessASMs: did not find failedPDA in asm list\n");
    994 		RF_ASSERT(0);
    995 	}
    996 	if (rf_degDagDebug) {
    997 		if (new_asm_h[0]) {
    998 			printf("First asm:\n");
    999 			rf_PrintFullAccessStripeMap(new_asm_h[0], 1);
   1000 		}
   1001 		if (new_asm_h[1]) {
   1002 			printf("Second asm:\n");
   1003 			rf_PrintFullAccessStripeMap(new_asm_h[1], 1);
   1004 		}
   1005 	}
   1006 }
   1007 
   1008 
   1009 /* adjusts the offset and number of sectors in the destination pda so that
   1010  * it covers at most the region of the SU covered by the source PDA.  This
   1011  * is exclusively a restriction:  the number of sectors indicated by the
   1012  * target PDA can only shrink.
   1013  *
   1014  * For example:  s = sectors within SU indicated by source PDA
   1015  *               d = sectors within SU indicated by dest PDA
   1016  *               r = results, stored in dest PDA
   1017  *
   1018  * |--------------- one stripe unit ---------------------|
   1019  * |           sssssssssssssssssssssssssssssssss         |
   1020  * |    ddddddddddddddddddddddddddddddddddddddddddddd    |
   1021  * |           rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr         |
   1022  *
   1023  * Another example:
   1024  *
   1025  * |--------------- one stripe unit ---------------------|
   1026  * |           sssssssssssssssssssssssssssssssss         |
   1027  * |    ddddddddddddddddddddddd                          |
   1028  * |           rrrrrrrrrrrrrrrr                          |
   1029  *
   1030  */
   1031 void
   1032 rf_RangeRestrictPDA(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *src,
   1033 		    RF_PhysDiskAddr_t *dest, int dobuffer, int doraidaddr)
   1034 {
   1035 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
   1036 	RF_SectorNum_t soffs = rf_StripeUnitOffset(layoutPtr, src->startSector);
   1037 	RF_SectorNum_t doffs = rf_StripeUnitOffset(layoutPtr, dest->startSector);
   1038 	RF_SectorNum_t send = rf_StripeUnitOffset(layoutPtr, src->startSector + src->numSector - 1);	/* use -1 to be sure we
   1039 													 * stay within SU */
   1040 	RF_SectorNum_t dend = rf_StripeUnitOffset(layoutPtr, dest->startSector + dest->numSector - 1);
   1041 	RF_SectorNum_t subAddr = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, dest->startSector);	/* stripe unit boundary */
   1042 
   1043 	dest->startSector = subAddr + RF_MAX(soffs, doffs);
   1044 	dest->numSector = subAddr + RF_MIN(send, dend) + 1 - dest->startSector;
   1045 
   1046 	if (dobuffer)
   1047 		dest->bufPtr += (soffs > doffs) ? rf_RaidAddressToByte(raidPtr, soffs - doffs) : 0;
   1048 	if (doraidaddr) {
   1049 		dest->raidAddress = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, dest->raidAddress) +
   1050 		    rf_StripeUnitOffset(layoutPtr, dest->startSector);
   1051 	}
   1052 }
   1053 
   1054 #if (RF_INCLUDE_CHAINDECLUSTER > 0)
   1055 
   1056 /*
   1057  * Want the highest of these primes to be the largest one
   1058  * less than the max expected number of columns (won't hurt
   1059  * to be too small or too large, but won't be optimal, either)
   1060  * --jimz
   1061  */
   1062 #define NLOWPRIMES 8
   1063 static int lowprimes[NLOWPRIMES] = {2, 3, 5, 7, 11, 13, 17, 19};
   1064 /*****************************************************************************
   1065  * compute the workload shift factor.  (chained declustering)
   1066  *
   1067  * return nonzero if access should shift to secondary, otherwise,
   1068  * access is to primary
   1069  *****************************************************************************/
   1070 int
   1071 rf_compute_workload_shift(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *pda)
   1072 {
   1073 	/*
   1074          * variables:
   1075          *  d   = column of disk containing primary
   1076          *  f   = column of failed disk
   1077          *  n   = number of disks in array
   1078          *  sd  = "shift distance" (number of columns that d is to the right of f)
   1079          *  v   = numerator of redirection ratio
   1080          *  k   = denominator of redirection ratio
   1081          */
   1082 	RF_RowCol_t d, f, sd, n;
   1083 	int     k, v, ret, i;
   1084 
   1085 	n = raidPtr->numCol;
   1086 
   1087 	/* assign column of primary copy to d */
   1088 	d = pda->col;
   1089 
   1090 	/* assign column of dead disk to f */
   1091 	for (f = 0; ((!RF_DEAD_DISK(raidPtr->Disks[f].status)) && (f < n)); f++);
   1092 
   1093 	RF_ASSERT(f < n);
   1094 	RF_ASSERT(f != d);
   1095 
   1096 	sd = (f > d) ? (n + d - f) : (d - f);
   1097 	RF_ASSERT(sd < n);
   1098 
   1099 	/*
   1100          * v of every k accesses should be redirected
   1101          *
   1102          * v/k := (n-1-sd)/(n-1)
   1103          */
   1104 	v = (n - 1 - sd);
   1105 	k = (n - 1);
   1106 
   1107 #if 1
   1108 	/*
   1109          * XXX
   1110          * Is this worth it?
   1111          *
   1112          * Now reduce the fraction, by repeatedly factoring
   1113          * out primes (just like they teach in elementary school!)
   1114          */
   1115 	for (i = 0; i < NLOWPRIMES; i++) {
   1116 		if (lowprimes[i] > v)
   1117 			break;
   1118 		while (((v % lowprimes[i]) == 0) && ((k % lowprimes[i]) == 0)) {
   1119 			v /= lowprimes[i];
   1120 			k /= lowprimes[i];
   1121 		}
   1122 	}
   1123 #endif
   1124 
   1125 	raidPtr->hist_diskreq[d]++;
   1126 	if (raidPtr->hist_diskreq[d] > v) {
   1127 		ret = 0;	/* do not redirect */
   1128 	} else {
   1129 		ret = 1;	/* redirect */
   1130 	}
   1131 
   1132 #if 0
   1133 	printf("d=%d f=%d sd=%d v=%d k=%d ret=%d h=%d\n", d, f, sd, v, k, ret,
   1134 	    raidPtr->hist_diskreq[d]);
   1135 #endif
   1136 
   1137 	if (raidPtr->hist_diskreq[d] >= k) {
   1138 		/* reset counter */
   1139 		raidPtr->hist_diskreq[d] = 0;
   1140 	}
   1141 	return (ret);
   1142 }
   1143 #endif /* (RF_INCLUDE_CHAINDECLUSTER > 0) */
   1144 
   1145 /*
   1146  * Disk selection routines
   1147  */
   1148 
   1149 /*
   1150  * Selects the disk with the shortest queue from a mirror pair.
   1151  * Both the disk I/Os queued in RAIDframe as well as those at the physical
   1152  * disk are counted as members of the "queue"
   1153  */
   1154 void
   1155 rf_SelectMirrorDiskIdle(RF_DagNode_t * node)
   1156 {
   1157 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->dagHdr->raidPtr;
   1158 	RF_RowCol_t colData, colMirror;
   1159 	int     dataQueueLength, mirrorQueueLength, usemirror;
   1160 	RF_PhysDiskAddr_t *data_pda = (RF_PhysDiskAddr_t *) node->params[0].p;
   1161 	RF_PhysDiskAddr_t *mirror_pda = (RF_PhysDiskAddr_t *) node->params[4].p;
   1162 	RF_PhysDiskAddr_t *tmp_pda;
   1163 	RF_RaidDisk_t *disks = raidPtr->Disks;
   1164 	RF_DiskQueue_t *dqs = raidPtr->Queues, *dataQueue, *mirrorQueue;
   1165 
   1166 	/* return the [row col] of the disk with the shortest queue */
   1167 	colData = data_pda->col;
   1168 	colMirror = mirror_pda->col;
   1169 	dataQueue = &(dqs[colData]);
   1170 	mirrorQueue = &(dqs[colMirror]);
   1171 
   1172 #ifdef RF_LOCK_QUEUES_TO_READ_LEN
   1173 	RF_LOCK_QUEUE_MUTEX(dataQueue, "SelectMirrorDiskIdle");
   1174 #endif				/* RF_LOCK_QUEUES_TO_READ_LEN */
   1175 	dataQueueLength = dataQueue->queueLength + dataQueue->numOutstanding;
   1176 #ifdef RF_LOCK_QUEUES_TO_READ_LEN
   1177 	RF_UNLOCK_QUEUE_MUTEX(dataQueue, "SelectMirrorDiskIdle");
   1178 	RF_LOCK_QUEUE_MUTEX(mirrorQueue, "SelectMirrorDiskIdle");
   1179 #endif				/* RF_LOCK_QUEUES_TO_READ_LEN */
   1180 	mirrorQueueLength = mirrorQueue->queueLength + mirrorQueue->numOutstanding;
   1181 #ifdef RF_LOCK_QUEUES_TO_READ_LEN
   1182 	RF_UNLOCK_QUEUE_MUTEX(mirrorQueue, "SelectMirrorDiskIdle");
   1183 #endif				/* RF_LOCK_QUEUES_TO_READ_LEN */
   1184 
   1185 	usemirror = 0;
   1186 	if (RF_DEAD_DISK(disks[colMirror].status)) {
   1187 		usemirror = 0;
   1188 	} else
   1189 		if (RF_DEAD_DISK(disks[colData].status)) {
   1190 			usemirror = 1;
   1191 		} else
   1192 			if (raidPtr->parity_good == RF_RAID_DIRTY) {
   1193 				/* Trust only the main disk */
   1194 				usemirror = 0;
   1195 			} else
   1196 				if (dataQueueLength < mirrorQueueLength) {
   1197 					usemirror = 0;
   1198 				} else
   1199 					if (mirrorQueueLength < dataQueueLength) {
   1200 						usemirror = 1;
   1201 					} else {
   1202 						/* queues are equal length. attempt
   1203 						 * cleverness. */
   1204 						if (SNUM_DIFF(dataQueue->last_deq_sector, data_pda->startSector)
   1205 						    <= SNUM_DIFF(mirrorQueue->last_deq_sector, mirror_pda->startSector)) {
   1206 							usemirror = 0;
   1207 						} else {
   1208 							usemirror = 1;
   1209 						}
   1210 					}
   1211 
   1212 	if (usemirror) {
   1213 		/* use mirror (parity) disk, swap params 0 & 4 */
   1214 		tmp_pda = data_pda;
   1215 		node->params[0].p = mirror_pda;
   1216 		node->params[4].p = tmp_pda;
   1217 	} else {
   1218 		/* use data disk, leave param 0 unchanged */
   1219 	}
   1220 	/* printf("dataQueueLength %d, mirrorQueueLength
   1221 	 * %d\n",dataQueueLength, mirrorQueueLength); */
   1222 }
   1223 #if (RF_INCLUDE_CHAINDECLUSTER > 0) || (RF_INCLUDE_INTERDECLUSTER > 0) || (RF_DEBUG_VALIDATE_DAG > 0)
   1224 /*
   1225  * Do simple partitioning. This assumes that
   1226  * the data and parity disks are laid out identically.
   1227  */
   1228 void
   1229 rf_SelectMirrorDiskPartition(RF_DagNode_t * node)
   1230 {
   1231 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->dagHdr->raidPtr;
   1232 	RF_RowCol_t colData, colMirror;
   1233 	RF_PhysDiskAddr_t *data_pda = (RF_PhysDiskAddr_t *) node->params[0].p;
   1234 	RF_PhysDiskAddr_t *mirror_pda = (RF_PhysDiskAddr_t *) node->params[4].p;
   1235 	RF_PhysDiskAddr_t *tmp_pda;
   1236 	RF_RaidDisk_t *disks = raidPtr->Disks;
   1237 	int     usemirror;
   1238 
   1239 	/* return the [row col] of the disk with the shortest queue */
   1240 	colData = data_pda->col;
   1241 	colMirror = mirror_pda->col;
   1242 
   1243 	usemirror = 0;
   1244 	if (RF_DEAD_DISK(disks[colMirror].status)) {
   1245 		usemirror = 0;
   1246 	} else
   1247 		if (RF_DEAD_DISK(disks[colData].status)) {
   1248 			usemirror = 1;
   1249 		} else
   1250 			if (raidPtr->parity_good == RF_RAID_DIRTY) {
   1251 				/* Trust only the main disk */
   1252 				usemirror = 0;
   1253 			} else
   1254 				if (data_pda->startSector <
   1255 				    (disks[colData].numBlocks / 2)) {
   1256 					usemirror = 0;
   1257 				} else {
   1258 					usemirror = 1;
   1259 				}
   1260 
   1261 	if (usemirror) {
   1262 		/* use mirror (parity) disk, swap params 0 & 4 */
   1263 		tmp_pda = data_pda;
   1264 		node->params[0].p = mirror_pda;
   1265 		node->params[4].p = tmp_pda;
   1266 	} else {
   1267 		/* use data disk, leave param 0 unchanged */
   1268 	}
   1269 }
   1270 #endif
   1271