Home | History | Annotate | Line # | Download | only in raidframe
rf_dagutils.c revision 1.32
      1 /*	$NetBSD: rf_dagutils.c,v 1.32 2004/03/06 22:59:42 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.32 2004/03/06 22:59:42 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_DEBUG_DAG
    743 			if (rf_dagDebug) {
    744 				if (!rf_CheckRUReconstructed(raidPtr->reconControl->reconMap,
    745 					pda->startSector)) {
    746 					RF_PANIC();
    747 				}
    748 			}
    749 #endif
    750 			/* printf("Remapped data for large write\n"); */
    751 			if (ds) {
    752 				raidPtr->Layout.map->MapSector(raidPtr, pda->raidAddress,
    753 				    &pda->col, &pda->startSector, RF_REMAP);
    754 			} else {
    755 				pda->col = scol;
    756 			}
    757 		}
    758 	}
    759 	for (pda = asmap->parityInfo; pda; pda = pda->next) {
    760 		if (pda->col == fcol) {
    761 #if RF_DEBUG_DAG
    762 			if (rf_dagDebug) {
    763 				if (!rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, pda->startSector)) {
    764 					RF_PANIC();
    765 				}
    766 			}
    767 #endif
    768 		}
    769 		if (ds) {
    770 			(raidPtr->Layout.map->MapParity) (raidPtr, pda->raidAddress, &pda->col, &pda->startSector, RF_REMAP);
    771 		} else {
    772 			pda->col = scol;
    773 		}
    774 	}
    775 }
    776 
    777 
    778 /* this routine allocates read buffers and generates stripe maps for the
    779  * regions of the array from the start of the stripe to the start of the
    780  * access, and from the end of the access to the end of the stripe.  It also
    781  * computes and returns the number of DAG nodes needed to read all this data.
    782  * Note that this routine does the wrong thing if the access is fully
    783  * contained within one stripe unit, so we RF_ASSERT against this case at the
    784  * start.
    785  *
    786  * layoutPtr - in: layout information
    787  * asmap     - in: access stripe map
    788  * dag_h     - in: header of the dag to create
    789  * new_asm_h - in: ptr to array of 2 headers.  to be filled in
    790  * nRodNodes - out: num nodes to be generated to read unaccessed data
    791  * sosBuffer, eosBuffer - out: pointers to newly allocated buffer
    792  */
    793 void
    794 rf_MapUnaccessedPortionOfStripe(RF_Raid_t *raidPtr,
    795 				RF_RaidLayout_t *layoutPtr,
    796 				RF_AccessStripeMap_t *asmap,
    797 				RF_DagHeader_t *dag_h,
    798 				RF_AccessStripeMapHeader_t **new_asm_h,
    799 				int *nRodNodes,
    800 				char **sosBuffer, char **eosBuffer,
    801 				RF_AllocListElem_t *allocList)
    802 {
    803 	RF_RaidAddr_t sosRaidAddress, eosRaidAddress;
    804 	RF_SectorNum_t sosNumSector, eosNumSector;
    805 
    806 	RF_ASSERT(asmap->numStripeUnitsAccessed > (layoutPtr->numDataCol / 2));
    807 	/* generate an access map for the region of the array from start of
    808 	 * stripe to start of access */
    809 	new_asm_h[0] = new_asm_h[1] = NULL;
    810 	*nRodNodes = 0;
    811 	if (!rf_RaidAddressStripeAligned(layoutPtr, asmap->raidAddress)) {
    812 		sosRaidAddress = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, asmap->raidAddress);
    813 		sosNumSector = asmap->raidAddress - sosRaidAddress;
    814 		RF_MallocAndAdd(*sosBuffer, rf_RaidAddressToByte(raidPtr, sosNumSector), (char *), allocList);
    815 		new_asm_h[0] = rf_MapAccess(raidPtr, sosRaidAddress, sosNumSector, *sosBuffer, RF_DONT_REMAP);
    816 		new_asm_h[0]->next = dag_h->asmList;
    817 		dag_h->asmList = new_asm_h[0];
    818 		*nRodNodes += new_asm_h[0]->stripeMap->numStripeUnitsAccessed;
    819 
    820 		RF_ASSERT(new_asm_h[0]->stripeMap->next == NULL);
    821 		/* we're totally within one stripe here */
    822 		if (asmap->flags & RF_ASM_REDIR_LARGE_WRITE)
    823 			rf_redirect_asm(raidPtr, new_asm_h[0]->stripeMap);
    824 	}
    825 	/* generate an access map for the region of the array from end of
    826 	 * access to end of stripe */
    827 	if (!rf_RaidAddressStripeAligned(layoutPtr, asmap->endRaidAddress)) {
    828 		eosRaidAddress = asmap->endRaidAddress;
    829 		eosNumSector = rf_RaidAddressOfNextStripeBoundary(layoutPtr, eosRaidAddress) - eosRaidAddress;
    830 		RF_MallocAndAdd(*eosBuffer, rf_RaidAddressToByte(raidPtr, eosNumSector), (char *), allocList);
    831 		new_asm_h[1] = rf_MapAccess(raidPtr, eosRaidAddress, eosNumSector, *eosBuffer, RF_DONT_REMAP);
    832 		new_asm_h[1]->next = dag_h->asmList;
    833 		dag_h->asmList = new_asm_h[1];
    834 		*nRodNodes += new_asm_h[1]->stripeMap->numStripeUnitsAccessed;
    835 
    836 		RF_ASSERT(new_asm_h[1]->stripeMap->next == NULL);
    837 		/* we're totally within one stripe here */
    838 		if (asmap->flags & RF_ASM_REDIR_LARGE_WRITE)
    839 			rf_redirect_asm(raidPtr, new_asm_h[1]->stripeMap);
    840 	}
    841 }
    842 
    843 
    844 
    845 /* returns non-zero if the indicated ranges of stripe unit offsets overlap */
    846 int
    847 rf_PDAOverlap(RF_RaidLayout_t *layoutPtr,
    848 	      RF_PhysDiskAddr_t *src, RF_PhysDiskAddr_t *dest)
    849 {
    850 	RF_SectorNum_t soffs = rf_StripeUnitOffset(layoutPtr, src->startSector);
    851 	RF_SectorNum_t doffs = rf_StripeUnitOffset(layoutPtr, dest->startSector);
    852 	/* use -1 to be sure we stay within SU */
    853 	RF_SectorNum_t send = rf_StripeUnitOffset(layoutPtr, src->startSector + src->numSector - 1);
    854 	RF_SectorNum_t dend = rf_StripeUnitOffset(layoutPtr, dest->startSector + dest->numSector - 1);
    855 	return ((RF_MAX(soffs, doffs) <= RF_MIN(send, dend)) ? 1 : 0);
    856 }
    857 
    858 
    859 /* GenerateFailedAccessASMs
    860  *
    861  * this routine figures out what portion of the stripe needs to be read
    862  * to effect the degraded read or write operation.  It's primary function
    863  * is to identify everything required to recover the data, and then
    864  * eliminate anything that is already being accessed by the user.
    865  *
    866  * The main result is two new ASMs, one for the region from the start of the
    867  * stripe to the start of the access, and one for the region from the end of
    868  * the access to the end of the stripe.  These ASMs describe everything that
    869  * needs to be read to effect the degraded access.  Other results are:
    870  *    nXorBufs -- the total number of buffers that need to be XORed together to
    871  *                recover the lost data,
    872  *    rpBufPtr -- ptr to a newly-allocated buffer to hold the parity.  If NULL
    873  *                at entry, not allocated.
    874  *    overlappingPDAs --
    875  *                describes which of the non-failed PDAs in the user access
    876  *                overlap data that needs to be read to effect recovery.
    877  *                overlappingPDAs[i]==1 if and only if, neglecting the failed
    878  *                PDA, the ith pda in the input asm overlaps data that needs
    879  *                to be read for recovery.
    880  */
    881  /* in: asm - ASM for the actual access, one stripe only */
    882  /* in: failedPDA - which component of the access has failed */
    883  /* in: dag_h - header of the DAG we're going to create */
    884  /* out: new_asm_h - the two new ASMs */
    885  /* out: nXorBufs - the total number of xor bufs required */
    886  /* out: rpBufPtr - a buffer for the parity read */
    887 void
    888 rf_GenerateFailedAccessASMs(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap,
    889 			    RF_PhysDiskAddr_t *failedPDA,
    890 			    RF_DagHeader_t *dag_h,
    891 			    RF_AccessStripeMapHeader_t **new_asm_h,
    892 			    int *nXorBufs, char **rpBufPtr,
    893 			    char *overlappingPDAs,
    894 			    RF_AllocListElem_t *allocList)
    895 {
    896 	RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout);
    897 
    898 	/* s=start, e=end, s=stripe, a=access, f=failed, su=stripe unit */
    899 	RF_RaidAddr_t sosAddr, sosEndAddr, eosStartAddr, eosAddr;
    900 
    901 	RF_SectorCount_t numSect[2], numParitySect;
    902 	RF_PhysDiskAddr_t *pda;
    903 	char   *rdBuf, *bufP;
    904 	int     foundit, i;
    905 
    906 	bufP = NULL;
    907 	foundit = 0;
    908 	/* first compute the following raid addresses: start of stripe,
    909 	 * (sosAddr) MIN(start of access, start of failed SU),   (sosEndAddr)
    910 	 * MAX(end of access, end of failed SU),       (eosStartAddr) end of
    911 	 * stripe (i.e. start of next stripe)   (eosAddr) */
    912 	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, asmap->raidAddress);
    913 	sosEndAddr = RF_MIN(asmap->raidAddress, rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, failedPDA->raidAddress));
    914 	eosStartAddr = RF_MAX(asmap->endRaidAddress, rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr, failedPDA->raidAddress));
    915 	eosAddr = rf_RaidAddressOfNextStripeBoundary(layoutPtr, asmap->raidAddress);
    916 
    917 	/* now generate access stripe maps for each of the above regions of
    918 	 * the stripe.  Use a dummy (NULL) buf ptr for now */
    919 
    920 	new_asm_h[0] = (sosAddr != sosEndAddr) ? rf_MapAccess(raidPtr, sosAddr, sosEndAddr - sosAddr, NULL, RF_DONT_REMAP) : NULL;
    921 	new_asm_h[1] = (eosStartAddr != eosAddr) ? rf_MapAccess(raidPtr, eosStartAddr, eosAddr - eosStartAddr, NULL, RF_DONT_REMAP) : NULL;
    922 
    923 	/* walk through the PDAs and range-restrict each SU to the region of
    924 	 * the SU touched on the failed PDA.  also compute total data buffer
    925 	 * space requirements in this step.  Ignore the parity for now. */
    926 
    927 	numSect[0] = numSect[1] = 0;
    928 	if (new_asm_h[0]) {
    929 		new_asm_h[0]->next = dag_h->asmList;
    930 		dag_h->asmList = new_asm_h[0];
    931 		for (pda = new_asm_h[0]->stripeMap->physInfo; pda; pda = pda->next) {
    932 			rf_RangeRestrictPDA(raidPtr, failedPDA, pda, RF_RESTRICT_NOBUFFER, 0);
    933 			numSect[0] += pda->numSector;
    934 		}
    935 	}
    936 	if (new_asm_h[1]) {
    937 		new_asm_h[1]->next = dag_h->asmList;
    938 		dag_h->asmList = new_asm_h[1];
    939 		for (pda = new_asm_h[1]->stripeMap->physInfo; pda; pda = pda->next) {
    940 			rf_RangeRestrictPDA(raidPtr, failedPDA, pda, RF_RESTRICT_NOBUFFER, 0);
    941 			numSect[1] += pda->numSector;
    942 		}
    943 	}
    944 	numParitySect = failedPDA->numSector;
    945 
    946 	/* allocate buffer space for the data & parity we have to read to
    947 	 * recover from the failure */
    948 
    949 	if (numSect[0] + numSect[1] + ((rpBufPtr) ? numParitySect : 0)) {	/* don't allocate parity
    950 										 * buf if not needed */
    951 		RF_MallocAndAdd(rdBuf, rf_RaidAddressToByte(raidPtr, numSect[0] + numSect[1] + numParitySect), (char *), allocList);
    952 		bufP = rdBuf;
    953 #if RF_DEBUG_DAG
    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 #endif
    958 	}
    959 	/* now walk through the pdas one last time and assign buffer pointers
    960 	 * (ugh!).  Again, ignore the parity.  also, count nodes to find out
    961 	 * how many bufs need to be xored together */
    962 	(*nXorBufs) = 1;	/* in read case, 1 is for parity.  In write
    963 				 * case, 1 is for failed data */
    964 	if (new_asm_h[0]) {
    965 		for (pda = new_asm_h[0]->stripeMap->physInfo; pda; pda = pda->next) {
    966 			pda->bufPtr = bufP;
    967 			bufP += rf_RaidAddressToByte(raidPtr, pda->numSector);
    968 		}
    969 		*nXorBufs += new_asm_h[0]->stripeMap->numStripeUnitsAccessed;
    970 	}
    971 	if (new_asm_h[1]) {
    972 		for (pda = new_asm_h[1]->stripeMap->physInfo; pda; pda = pda->next) {
    973 			pda->bufPtr = bufP;
    974 			bufP += rf_RaidAddressToByte(raidPtr, pda->numSector);
    975 		}
    976 		(*nXorBufs) += new_asm_h[1]->stripeMap->numStripeUnitsAccessed;
    977 	}
    978 	if (rpBufPtr)
    979 		*rpBufPtr = bufP;	/* the rest of the buffer is for
    980 					 * parity */
    981 
    982 	/* the last step is to figure out how many more distinct buffers need
    983 	 * to get xor'd to produce the missing unit.  there's one for each
    984 	 * user-data read node that overlaps the portion of the failed unit
    985 	 * being accessed */
    986 
    987 	for (foundit = i = 0, pda = asmap->physInfo; pda; i++, pda = pda->next) {
    988 		if (pda == failedPDA) {
    989 			i--;
    990 			foundit = 1;
    991 			continue;
    992 		}
    993 		if (rf_PDAOverlap(layoutPtr, pda, failedPDA)) {
    994 			overlappingPDAs[i] = 1;
    995 			(*nXorBufs)++;
    996 		}
    997 	}
    998 	if (!foundit) {
    999 		RF_ERRORMSG("GenerateFailedAccessASMs: did not find failedPDA in asm list\n");
   1000 		RF_ASSERT(0);
   1001 	}
   1002 #if RF_DEBUG_DAG
   1003 	if (rf_degDagDebug) {
   1004 		if (new_asm_h[0]) {
   1005 			printf("First asm:\n");
   1006 			rf_PrintFullAccessStripeMap(new_asm_h[0], 1);
   1007 		}
   1008 		if (new_asm_h[1]) {
   1009 			printf("Second asm:\n");
   1010 			rf_PrintFullAccessStripeMap(new_asm_h[1], 1);
   1011 		}
   1012 	}
   1013 #endif
   1014 }
   1015 
   1016 
   1017 /* adjusts the offset and number of sectors in the destination pda so that
   1018  * it covers at most the region of the SU covered by the source PDA.  This
   1019  * is exclusively a restriction:  the number of sectors indicated by the
   1020  * target PDA can only shrink.
   1021  *
   1022  * For example:  s = sectors within SU indicated by source PDA
   1023  *               d = sectors within SU indicated by dest PDA
   1024  *               r = results, stored in dest PDA
   1025  *
   1026  * |--------------- one stripe unit ---------------------|
   1027  * |           sssssssssssssssssssssssssssssssss         |
   1028  * |    ddddddddddddddddddddddddddddddddddddddddddddd    |
   1029  * |           rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr         |
   1030  *
   1031  * Another example:
   1032  *
   1033  * |--------------- one stripe unit ---------------------|
   1034  * |           sssssssssssssssssssssssssssssssss         |
   1035  * |    ddddddddddddddddddddddd                          |
   1036  * |           rrrrrrrrrrrrrrrr                          |
   1037  *
   1038  */
   1039 void
   1040 rf_RangeRestrictPDA(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *src,
   1041 		    RF_PhysDiskAddr_t *dest, int dobuffer, int doraidaddr)
   1042 {
   1043 	RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
   1044 	RF_SectorNum_t soffs = rf_StripeUnitOffset(layoutPtr, src->startSector);
   1045 	RF_SectorNum_t doffs = rf_StripeUnitOffset(layoutPtr, dest->startSector);
   1046 	RF_SectorNum_t send = rf_StripeUnitOffset(layoutPtr, src->startSector + src->numSector - 1);	/* use -1 to be sure we
   1047 													 * stay within SU */
   1048 	RF_SectorNum_t dend = rf_StripeUnitOffset(layoutPtr, dest->startSector + dest->numSector - 1);
   1049 	RF_SectorNum_t subAddr = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, dest->startSector);	/* stripe unit boundary */
   1050 
   1051 	dest->startSector = subAddr + RF_MAX(soffs, doffs);
   1052 	dest->numSector = subAddr + RF_MIN(send, dend) + 1 - dest->startSector;
   1053 
   1054 	if (dobuffer)
   1055 		dest->bufPtr += (soffs > doffs) ? rf_RaidAddressToByte(raidPtr, soffs - doffs) : 0;
   1056 	if (doraidaddr) {
   1057 		dest->raidAddress = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, dest->raidAddress) +
   1058 		    rf_StripeUnitOffset(layoutPtr, dest->startSector);
   1059 	}
   1060 }
   1061 
   1062 #if (RF_INCLUDE_CHAINDECLUSTER > 0)
   1063 
   1064 /*
   1065  * Want the highest of these primes to be the largest one
   1066  * less than the max expected number of columns (won't hurt
   1067  * to be too small or too large, but won't be optimal, either)
   1068  * --jimz
   1069  */
   1070 #define NLOWPRIMES 8
   1071 static int lowprimes[NLOWPRIMES] = {2, 3, 5, 7, 11, 13, 17, 19};
   1072 /*****************************************************************************
   1073  * compute the workload shift factor.  (chained declustering)
   1074  *
   1075  * return nonzero if access should shift to secondary, otherwise,
   1076  * access is to primary
   1077  *****************************************************************************/
   1078 int
   1079 rf_compute_workload_shift(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *pda)
   1080 {
   1081 	/*
   1082          * variables:
   1083          *  d   = column of disk containing primary
   1084          *  f   = column of failed disk
   1085          *  n   = number of disks in array
   1086          *  sd  = "shift distance" (number of columns that d is to the right of f)
   1087          *  v   = numerator of redirection ratio
   1088          *  k   = denominator of redirection ratio
   1089          */
   1090 	RF_RowCol_t d, f, sd, n;
   1091 	int     k, v, ret, i;
   1092 
   1093 	n = raidPtr->numCol;
   1094 
   1095 	/* assign column of primary copy to d */
   1096 	d = pda->col;
   1097 
   1098 	/* assign column of dead disk to f */
   1099 	for (f = 0; ((!RF_DEAD_DISK(raidPtr->Disks[f].status)) && (f < n)); f++);
   1100 
   1101 	RF_ASSERT(f < n);
   1102 	RF_ASSERT(f != d);
   1103 
   1104 	sd = (f > d) ? (n + d - f) : (d - f);
   1105 	RF_ASSERT(sd < n);
   1106 
   1107 	/*
   1108          * v of every k accesses should be redirected
   1109          *
   1110          * v/k := (n-1-sd)/(n-1)
   1111          */
   1112 	v = (n - 1 - sd);
   1113 	k = (n - 1);
   1114 
   1115 #if 1
   1116 	/*
   1117          * XXX
   1118          * Is this worth it?
   1119          *
   1120          * Now reduce the fraction, by repeatedly factoring
   1121          * out primes (just like they teach in elementary school!)
   1122          */
   1123 	for (i = 0; i < NLOWPRIMES; i++) {
   1124 		if (lowprimes[i] > v)
   1125 			break;
   1126 		while (((v % lowprimes[i]) == 0) && ((k % lowprimes[i]) == 0)) {
   1127 			v /= lowprimes[i];
   1128 			k /= lowprimes[i];
   1129 		}
   1130 	}
   1131 #endif
   1132 
   1133 	raidPtr->hist_diskreq[d]++;
   1134 	if (raidPtr->hist_diskreq[d] > v) {
   1135 		ret = 0;	/* do not redirect */
   1136 	} else {
   1137 		ret = 1;	/* redirect */
   1138 	}
   1139 
   1140 #if 0
   1141 	printf("d=%d f=%d sd=%d v=%d k=%d ret=%d h=%d\n", d, f, sd, v, k, ret,
   1142 	    raidPtr->hist_diskreq[d]);
   1143 #endif
   1144 
   1145 	if (raidPtr->hist_diskreq[d] >= k) {
   1146 		/* reset counter */
   1147 		raidPtr->hist_diskreq[d] = 0;
   1148 	}
   1149 	return (ret);
   1150 }
   1151 #endif /* (RF_INCLUDE_CHAINDECLUSTER > 0) */
   1152 
   1153 /*
   1154  * Disk selection routines
   1155  */
   1156 
   1157 /*
   1158  * Selects the disk with the shortest queue from a mirror pair.
   1159  * Both the disk I/Os queued in RAIDframe as well as those at the physical
   1160  * disk are counted as members of the "queue"
   1161  */
   1162 void
   1163 rf_SelectMirrorDiskIdle(RF_DagNode_t * node)
   1164 {
   1165 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->dagHdr->raidPtr;
   1166 	RF_RowCol_t colData, colMirror;
   1167 	int     dataQueueLength, mirrorQueueLength, usemirror;
   1168 	RF_PhysDiskAddr_t *data_pda = (RF_PhysDiskAddr_t *) node->params[0].p;
   1169 	RF_PhysDiskAddr_t *mirror_pda = (RF_PhysDiskAddr_t *) node->params[4].p;
   1170 	RF_PhysDiskAddr_t *tmp_pda;
   1171 	RF_RaidDisk_t *disks = raidPtr->Disks;
   1172 	RF_DiskQueue_t *dqs = raidPtr->Queues, *dataQueue, *mirrorQueue;
   1173 
   1174 	/* return the [row col] of the disk with the shortest queue */
   1175 	colData = data_pda->col;
   1176 	colMirror = mirror_pda->col;
   1177 	dataQueue = &(dqs[colData]);
   1178 	mirrorQueue = &(dqs[colMirror]);
   1179 
   1180 #ifdef RF_LOCK_QUEUES_TO_READ_LEN
   1181 	RF_LOCK_QUEUE_MUTEX(dataQueue, "SelectMirrorDiskIdle");
   1182 #endif				/* RF_LOCK_QUEUES_TO_READ_LEN */
   1183 	dataQueueLength = dataQueue->queueLength + dataQueue->numOutstanding;
   1184 #ifdef RF_LOCK_QUEUES_TO_READ_LEN
   1185 	RF_UNLOCK_QUEUE_MUTEX(dataQueue, "SelectMirrorDiskIdle");
   1186 	RF_LOCK_QUEUE_MUTEX(mirrorQueue, "SelectMirrorDiskIdle");
   1187 #endif				/* RF_LOCK_QUEUES_TO_READ_LEN */
   1188 	mirrorQueueLength = mirrorQueue->queueLength + mirrorQueue->numOutstanding;
   1189 #ifdef RF_LOCK_QUEUES_TO_READ_LEN
   1190 	RF_UNLOCK_QUEUE_MUTEX(mirrorQueue, "SelectMirrorDiskIdle");
   1191 #endif				/* RF_LOCK_QUEUES_TO_READ_LEN */
   1192 
   1193 	usemirror = 0;
   1194 	if (RF_DEAD_DISK(disks[colMirror].status)) {
   1195 		usemirror = 0;
   1196 	} else
   1197 		if (RF_DEAD_DISK(disks[colData].status)) {
   1198 			usemirror = 1;
   1199 		} else
   1200 			if (raidPtr->parity_good == RF_RAID_DIRTY) {
   1201 				/* Trust only the main disk */
   1202 				usemirror = 0;
   1203 			} else
   1204 				if (dataQueueLength < mirrorQueueLength) {
   1205 					usemirror = 0;
   1206 				} else
   1207 					if (mirrorQueueLength < dataQueueLength) {
   1208 						usemirror = 1;
   1209 					} else {
   1210 						/* queues are equal length. attempt
   1211 						 * cleverness. */
   1212 						if (SNUM_DIFF(dataQueue->last_deq_sector, data_pda->startSector)
   1213 						    <= SNUM_DIFF(mirrorQueue->last_deq_sector, mirror_pda->startSector)) {
   1214 							usemirror = 0;
   1215 						} else {
   1216 							usemirror = 1;
   1217 						}
   1218 					}
   1219 
   1220 	if (usemirror) {
   1221 		/* use mirror (parity) disk, swap params 0 & 4 */
   1222 		tmp_pda = data_pda;
   1223 		node->params[0].p = mirror_pda;
   1224 		node->params[4].p = tmp_pda;
   1225 	} else {
   1226 		/* use data disk, leave param 0 unchanged */
   1227 	}
   1228 	/* printf("dataQueueLength %d, mirrorQueueLength
   1229 	 * %d\n",dataQueueLength, mirrorQueueLength); */
   1230 }
   1231 #if (RF_INCLUDE_CHAINDECLUSTER > 0) || (RF_INCLUDE_INTERDECLUSTER > 0) || (RF_DEBUG_VALIDATE_DAG > 0)
   1232 /*
   1233  * Do simple partitioning. This assumes that
   1234  * the data and parity disks are laid out identically.
   1235  */
   1236 void
   1237 rf_SelectMirrorDiskPartition(RF_DagNode_t * node)
   1238 {
   1239 	RF_Raid_t *raidPtr = (RF_Raid_t *) node->dagHdr->raidPtr;
   1240 	RF_RowCol_t colData, colMirror;
   1241 	RF_PhysDiskAddr_t *data_pda = (RF_PhysDiskAddr_t *) node->params[0].p;
   1242 	RF_PhysDiskAddr_t *mirror_pda = (RF_PhysDiskAddr_t *) node->params[4].p;
   1243 	RF_PhysDiskAddr_t *tmp_pda;
   1244 	RF_RaidDisk_t *disks = raidPtr->Disks;
   1245 	int     usemirror;
   1246 
   1247 	/* return the [row col] of the disk with the shortest queue */
   1248 	colData = data_pda->col;
   1249 	colMirror = mirror_pda->col;
   1250 
   1251 	usemirror = 0;
   1252 	if (RF_DEAD_DISK(disks[colMirror].status)) {
   1253 		usemirror = 0;
   1254 	} else
   1255 		if (RF_DEAD_DISK(disks[colData].status)) {
   1256 			usemirror = 1;
   1257 		} else
   1258 			if (raidPtr->parity_good == RF_RAID_DIRTY) {
   1259 				/* Trust only the main disk */
   1260 				usemirror = 0;
   1261 			} else
   1262 				if (data_pda->startSector <
   1263 				    (disks[colData].numBlocks / 2)) {
   1264 					usemirror = 0;
   1265 				} else {
   1266 					usemirror = 1;
   1267 				}
   1268 
   1269 	if (usemirror) {
   1270 		/* use mirror (parity) disk, swap params 0 & 4 */
   1271 		tmp_pda = data_pda;
   1272 		node->params[0].p = mirror_pda;
   1273 		node->params[4].p = tmp_pda;
   1274 	} else {
   1275 		/* use data disk, leave param 0 unchanged */
   1276 	}
   1277 }
   1278 #endif
   1279