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