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