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