Home | History | Annotate | Line # | Download | only in raidframe
rf_dagdegwr.c revision 1.3
      1 /*	$NetBSD: rf_dagdegwr.c,v 1.3 1999/02/05 00:06:07 oster Exp $	*/
      2 /*
      3  * Copyright (c) 1995 Carnegie-Mellon University.
      4  * All rights reserved.
      5  *
      6  * Author: Mark Holland, Daniel Stodolsky, William V. Courtright II
      7  *
      8  * Permission to use, copy, modify and distribute this software and
      9  * its documentation is hereby granted, provided that both the copyright
     10  * notice and this permission notice appear in all copies of the
     11  * software, derivative works or modified versions, and any portions
     12  * thereof, and that both notices appear in supporting documentation.
     13  *
     14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     16  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  *
     18  * Carnegie Mellon requests users of this software to return to
     19  *
     20  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21  *  School of Computer Science
     22  *  Carnegie Mellon University
     23  *  Pittsburgh PA 15213-3890
     24  *
     25  * any improvements or extensions that they make and grant Carnegie the
     26  * rights to redistribute these changes.
     27  */
     28 
     29 /*
     30  * rf_dagdegwr.c
     31  *
     32  * code for creating degraded write DAGs
     33  *
     34  */
     35 
     36 #include "rf_types.h"
     37 #include "rf_raid.h"
     38 #include "rf_dag.h"
     39 #include "rf_dagutils.h"
     40 #include "rf_dagfuncs.h"
     41 #include "rf_threadid.h"
     42 #include "rf_debugMem.h"
     43 #include "rf_memchunk.h"
     44 #include "rf_general.h"
     45 #include "rf_dagdegwr.h"
     46 #include "rf_sys.h"
     47 
     48 
     49 /******************************************************************************
     50  *
     51  * General comments on DAG creation:
     52  *
     53  * All DAGs in this file use roll-away error recovery.  Each DAG has a single
     54  * commit node, usually called "Cmt."  If an error occurs before the Cmt node
     55  * is reached, the execution engine will halt forward execution and work
     56  * backward through the graph, executing the undo functions.  Assuming that
     57  * each node in the graph prior to the Cmt node are undoable and atomic - or -
     58  * does not make changes to permanent state, the graph will fail atomically.
     59  * If an error occurs after the Cmt node executes, the engine will roll-forward
     60  * through the graph, blindly executing nodes until it reaches the end.
     61  * If a graph reaches the end, it is assumed to have completed successfully.
     62  *
     63  * A graph has only 1 Cmt node.
     64  *
     65  */
     66 
     67 
     68 /******************************************************************************
     69  *
     70  * The following wrappers map the standard DAG creation interface to the
     71  * DAG creation routines.  Additionally, these wrappers enable experimentation
     72  * with new DAG structures by providing an extra level of indirection, allowing
     73  * the DAG creation routines to be replaced at this single point.
     74  */
     75 
     76 static
     77 RF_CREATE_DAG_FUNC_DECL(rf_CreateSimpleDegradedWriteDAG)
     78 {
     79 	rf_CommonCreateSimpleDegradedWriteDAG(raidPtr, asmap, dag_h, bp,
     80 	    flags, allocList, 1, rf_RecoveryXorFunc, RF_TRUE);
     81 }
     82 
     83 void
     84 rf_CreateDegradedWriteDAG(raidPtr, asmap, dag_h, bp, flags, allocList)
     85 	RF_Raid_t *raidPtr;
     86 	RF_AccessStripeMap_t *asmap;
     87 	RF_DagHeader_t *dag_h;
     88 	void   *bp;
     89 	RF_RaidAccessFlags_t flags;
     90 	RF_AllocListElem_t *allocList;
     91 {
     92 	RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout);
     93 	RF_PhysDiskAddr_t *failedPDA = asmap->failedPDAs[0];
     94 
     95 	RF_ASSERT(asmap->numDataFailed == 1);
     96 	dag_h->creator = "DegradedWriteDAG";
     97 
     98 	/* if the access writes only a portion of the failed unit, and also
     99 	 * writes some portion of at least one surviving unit, we create two
    100 	 * DAGs, one for the failed component and one for the non-failed
    101 	 * component, and do them sequentially.  Note that the fact that we're
    102 	 * accessing only a portion of the failed unit indicates that the
    103 	 * access either starts or ends in the failed unit, and hence we need
    104 	 * create only two dags.  This is inefficient in that the same data or
    105 	 * parity can get read and written twice using this structure.  I need
    106 	 * to fix this to do the access all at once. */
    107 	RF_ASSERT(!(asmap->numStripeUnitsAccessed != 1 && failedPDA->numSector != layoutPtr->sectorsPerStripeUnit));
    108 	rf_CreateSimpleDegradedWriteDAG(raidPtr, asmap, dag_h, bp, flags, allocList);
    109 }
    110 
    111 
    112 
    113 /******************************************************************************
    114  *
    115  * DAG creation code begins here
    116  */
    117 
    118 
    119 
    120 /******************************************************************************
    121  *
    122  * CommonCreateSimpleDegradedWriteDAG -- creates a DAG to do a degraded-mode
    123  * write, which is as follows
    124  *
    125  *                                        / {Wnq} --\
    126  * hdr -> blockNode ->  Rod -> Xor -> Cmt -> Wnp ----> unblock -> term
    127  *                  \  {Rod} /            \  Wnd ---/
    128  *                                        \ {Wnd} -/
    129  *
    130  * commit nodes: Xor, Wnd
    131  *
    132  * IMPORTANT:
    133  * This DAG generator does not work for double-degraded archs since it does not
    134  * generate Q
    135  *
    136  * This dag is essentially identical to the large-write dag, except that the
    137  * write to the failed data unit is suppressed.
    138  *
    139  * IMPORTANT:  this dag does not work in the case where the access writes only
    140  * a portion of the failed unit, and also writes some portion of at least one
    141  * surviving SU.  this case is handled in CreateDegradedWriteDAG above.
    142  *
    143  * The block & unblock nodes are leftovers from a previous version.  They
    144  * do nothing, but I haven't deleted them because it would be a tremendous
    145  * effort to put them back in.
    146  *
    147  * This dag is used whenever a one of the data units in a write has failed.
    148  * If it is the parity unit that failed, the nonredundant write dag (below)
    149  * is used.
    150  *****************************************************************************/
    151 
    152 void
    153 rf_CommonCreateSimpleDegradedWriteDAG(raidPtr, asmap, dag_h, bp, flags,
    154     allocList, nfaults, redFunc, allowBufferRecycle)
    155 	RF_Raid_t *raidPtr;
    156 	RF_AccessStripeMap_t *asmap;
    157 	RF_DagHeader_t *dag_h;
    158 	void   *bp;
    159 	RF_RaidAccessFlags_t flags;
    160 	RF_AllocListElem_t *allocList;
    161 	int     nfaults;
    162 	int     (*redFunc) (RF_DagNode_t *);
    163 	int     allowBufferRecycle;
    164 {
    165 	int     nNodes, nRrdNodes, nWndNodes, nXorBufs, i, j, paramNum,
    166 	        rdnodesFaked;
    167 	RF_DagNode_t *blockNode, *unblockNode, *wnpNode, *wnqNode, *termNode;
    168 	RF_DagNode_t *nodes, *wndNodes, *rrdNodes, *xorNode, *commitNode;
    169 	RF_SectorCount_t sectorsPerSU;
    170 	RF_ReconUnitNum_t which_ru;
    171 	char   *xorTargetBuf = NULL;	/* the target buffer for the XOR
    172 					 * operation */
    173 	char   *overlappingPDAs;/* a temporary array of flags */
    174 	RF_AccessStripeMapHeader_t *new_asm_h[2];
    175 	RF_PhysDiskAddr_t *pda, *parityPDA;
    176 	RF_StripeNum_t parityStripeID;
    177 	RF_PhysDiskAddr_t *failedPDA;
    178 	RF_RaidLayout_t *layoutPtr;
    179 
    180 	layoutPtr = &(raidPtr->Layout);
    181 	parityStripeID = rf_RaidAddressToParityStripeID(layoutPtr, asmap->raidAddress,
    182 	    &which_ru);
    183 	sectorsPerSU = layoutPtr->sectorsPerStripeUnit;
    184 	/* failedPDA points to the pda within the asm that targets the failed
    185 	 * disk */
    186 	failedPDA = asmap->failedPDAs[0];
    187 
    188 	if (rf_dagDebug)
    189 		printf("[Creating degraded-write DAG]\n");
    190 
    191 	RF_ASSERT(asmap->numDataFailed == 1);
    192 	dag_h->creator = "SimpleDegradedWriteDAG";
    193 
    194 	/*
    195          * Generate two ASMs identifying the surviving data
    196          * we need in order to recover the lost data.
    197          */
    198 	/* overlappingPDAs array must be zero'd */
    199 	RF_Calloc(overlappingPDAs, asmap->numStripeUnitsAccessed, sizeof(char), (char *));
    200 	rf_GenerateFailedAccessASMs(raidPtr, asmap, failedPDA, dag_h, new_asm_h,
    201 	    &nXorBufs, NULL, overlappingPDAs, allocList);
    202 
    203 	/* create all the nodes at once */
    204 	nWndNodes = asmap->numStripeUnitsAccessed - 1;	/* no access is
    205 							 * generated for the
    206 							 * failed pda */
    207 
    208 	nRrdNodes = ((new_asm_h[0]) ? new_asm_h[0]->stripeMap->numStripeUnitsAccessed : 0) +
    209 	    ((new_asm_h[1]) ? new_asm_h[1]->stripeMap->numStripeUnitsAccessed : 0);
    210 	/*
    211          * XXX
    212          *
    213          * There's a bug with a complete stripe overwrite- that means 0 reads
    214          * of old data, and the rest of the DAG generation code doesn't like
    215          * that. A release is coming, and I don't wanna risk breaking a critical
    216          * DAG generator, so here's what I'm gonna do- if there's no read nodes,
    217          * I'm gonna fake there being a read node, and I'm gonna swap in a
    218          * no-op node in its place (to make all the link-up code happy).
    219          * This should be fixed at some point.  --jimz
    220          */
    221 	if (nRrdNodes == 0) {
    222 		nRrdNodes = 1;
    223 		rdnodesFaked = 1;
    224 	} else {
    225 		rdnodesFaked = 0;
    226 	}
    227 	/* lock, unlock, xor, Wnd, Rrd, W(nfaults) */
    228 	nNodes = 5 + nfaults + nWndNodes + nRrdNodes;
    229 	RF_CallocAndAdd(nodes, nNodes, sizeof(RF_DagNode_t),
    230 	    (RF_DagNode_t *), allocList);
    231 	i = 0;
    232 	blockNode = &nodes[i];
    233 	i += 1;
    234 	commitNode = &nodes[i];
    235 	i += 1;
    236 	unblockNode = &nodes[i];
    237 	i += 1;
    238 	termNode = &nodes[i];
    239 	i += 1;
    240 	xorNode = &nodes[i];
    241 	i += 1;
    242 	wnpNode = &nodes[i];
    243 	i += 1;
    244 	wndNodes = &nodes[i];
    245 	i += nWndNodes;
    246 	rrdNodes = &nodes[i];
    247 	i += nRrdNodes;
    248 	if (nfaults == 2) {
    249 		wnqNode = &nodes[i];
    250 		i += 1;
    251 	} else {
    252 		wnqNode = NULL;
    253 	}
    254 	RF_ASSERT(i == nNodes);
    255 
    256 	/* this dag can not commit until all rrd and xor Nodes have completed */
    257 	dag_h->numCommitNodes = 1;
    258 	dag_h->numCommits = 0;
    259 	dag_h->numSuccedents = 1;
    260 
    261 	RF_ASSERT(nRrdNodes > 0);
    262 	rf_InitNode(blockNode, rf_wait, RF_FALSE, rf_NullNodeFunc, rf_NullNodeUndoFunc,
    263 	    NULL, nRrdNodes, 0, 0, 0, dag_h, "Nil", allocList);
    264 	rf_InitNode(commitNode, rf_wait, RF_TRUE, rf_NullNodeFunc, rf_NullNodeUndoFunc,
    265 	    NULL, nWndNodes + nfaults, 1, 0, 0, dag_h, "Cmt", allocList);
    266 	rf_InitNode(unblockNode, rf_wait, RF_FALSE, rf_NullNodeFunc, rf_NullNodeUndoFunc,
    267 	    NULL, 1, nWndNodes + nfaults, 0, 0, dag_h, "Nil", allocList);
    268 	rf_InitNode(termNode, rf_wait, RF_FALSE, rf_TerminateFunc, rf_TerminateUndoFunc,
    269 	    NULL, 0, 1, 0, 0, dag_h, "Trm", allocList);
    270 	rf_InitNode(xorNode, rf_wait, RF_FALSE, redFunc, rf_NullNodeUndoFunc, NULL, 1,
    271 	    nRrdNodes, 2 * nXorBufs + 2, nfaults, dag_h, "Xrc", allocList);
    272 
    273 	/*
    274          * Fill in the Rrd nodes. If any of the rrd buffers are the same size as
    275          * the failed buffer, save a pointer to it so we can use it as the target
    276          * of the XOR. The pdas in the rrd nodes have been range-restricted, so if
    277          * a buffer is the same size as the failed buffer, it must also be at the
    278          * same alignment within the SU.
    279          */
    280 	i = 0;
    281 	if (new_asm_h[0]) {
    282 		for (i = 0, pda = new_asm_h[0]->stripeMap->physInfo;
    283 		    i < new_asm_h[0]->stripeMap->numStripeUnitsAccessed;
    284 		    i++, pda = pda->next) {
    285 			rf_InitNode(&rrdNodes[i], rf_wait, RF_FALSE, rf_DiskReadFunc, rf_DiskReadUndoFunc,
    286 			    rf_GenericWakeupFunc, 1, 1, 4, 0, dag_h, "Rrd", allocList);
    287 			RF_ASSERT(pda);
    288 			rrdNodes[i].params[0].p = pda;
    289 			rrdNodes[i].params[1].p = pda->bufPtr;
    290 			rrdNodes[i].params[2].v = parityStripeID;
    291 			rrdNodes[i].params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, 0, 0, which_ru);
    292 		}
    293 	}
    294 	/* i now equals the number of stripe units accessed in new_asm_h[0] */
    295 	if (new_asm_h[1]) {
    296 		for (j = 0, pda = new_asm_h[1]->stripeMap->physInfo;
    297 		    j < new_asm_h[1]->stripeMap->numStripeUnitsAccessed;
    298 		    j++, pda = pda->next) {
    299 			rf_InitNode(&rrdNodes[i + j], rf_wait, RF_FALSE, rf_DiskReadFunc, rf_DiskReadUndoFunc,
    300 			    rf_GenericWakeupFunc, 1, 1, 4, 0, dag_h, "Rrd", allocList);
    301 			RF_ASSERT(pda);
    302 			rrdNodes[i + j].params[0].p = pda;
    303 			rrdNodes[i + j].params[1].p = pda->bufPtr;
    304 			rrdNodes[i + j].params[2].v = parityStripeID;
    305 			rrdNodes[i + j].params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, 0, 0, which_ru);
    306 			if (allowBufferRecycle && (pda->numSector == failedPDA->numSector))
    307 				xorTargetBuf = pda->bufPtr;
    308 		}
    309 	}
    310 	if (rdnodesFaked) {
    311 		/*
    312 	         * This is where we'll init that fake noop read node
    313 	         * (XXX should the wakeup func be different?)
    314 	         */
    315 		rf_InitNode(&rrdNodes[0], rf_wait, RF_FALSE, rf_NullNodeFunc, rf_NullNodeUndoFunc,
    316 		    NULL, 1, 1, 0, 0, dag_h, "RrN", allocList);
    317 	}
    318 	/*
    319          * Make a PDA for the parity unit.  The parity PDA should start at
    320          * the same offset into the SU as the failed PDA.
    321          */
    322 	/* Danner comment: I don't think this copy is really necessary. We are
    323 	 * in one of two cases here. (1) The entire failed unit is written.
    324 	 * Then asmap->parityInfo will describe the entire parity. (2) We are
    325 	 * only writing a subset of the failed unit and nothing else. Then the
    326 	 * asmap->parityInfo describes the failed unit and the copy can also
    327 	 * be avoided. */
    328 
    329 	RF_MallocAndAdd(parityPDA, sizeof(RF_PhysDiskAddr_t), (RF_PhysDiskAddr_t *), allocList);
    330 	parityPDA->row = asmap->parityInfo->row;
    331 	parityPDA->col = asmap->parityInfo->col;
    332 	parityPDA->startSector = ((asmap->parityInfo->startSector / sectorsPerSU)
    333 	    * sectorsPerSU) + (failedPDA->startSector % sectorsPerSU);
    334 	parityPDA->numSector = failedPDA->numSector;
    335 
    336 	if (!xorTargetBuf) {
    337 		RF_CallocAndAdd(xorTargetBuf, 1,
    338 		    rf_RaidAddressToByte(raidPtr, failedPDA->numSector), (char *), allocList);
    339 	}
    340 	/* init the Wnp node */
    341 	rf_InitNode(wnpNode, rf_wait, RF_FALSE, rf_DiskWriteFunc, rf_DiskWriteUndoFunc,
    342 	    rf_GenericWakeupFunc, 1, 1, 4, 0, dag_h, "Wnp", allocList);
    343 	wnpNode->params[0].p = parityPDA;
    344 	wnpNode->params[1].p = xorTargetBuf;
    345 	wnpNode->params[2].v = parityStripeID;
    346 	wnpNode->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, 0, 0, which_ru);
    347 
    348 	/* fill in the Wnq Node */
    349 	if (nfaults == 2) {
    350 		{
    351 			RF_MallocAndAdd(parityPDA, sizeof(RF_PhysDiskAddr_t),
    352 			    (RF_PhysDiskAddr_t *), allocList);
    353 			parityPDA->row = asmap->qInfo->row;
    354 			parityPDA->col = asmap->qInfo->col;
    355 			parityPDA->startSector = ((asmap->qInfo->startSector / sectorsPerSU)
    356 			    * sectorsPerSU) + (failedPDA->startSector % sectorsPerSU);
    357 			parityPDA->numSector = failedPDA->numSector;
    358 
    359 			rf_InitNode(wnqNode, rf_wait, RF_FALSE, rf_DiskWriteFunc, rf_DiskWriteUndoFunc,
    360 			    rf_GenericWakeupFunc, 1, 1, 4, 0, dag_h, "Wnq", allocList);
    361 			wnqNode->params[0].p = parityPDA;
    362 			RF_CallocAndAdd(xorNode->results[1], 1,
    363 			    rf_RaidAddressToByte(raidPtr, failedPDA->numSector), (char *), allocList);
    364 			wnqNode->params[1].p = xorNode->results[1];
    365 			wnqNode->params[2].v = parityStripeID;
    366 			wnqNode->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, 0, 0, which_ru);
    367 		}
    368 	}
    369 	/* fill in the Wnd nodes */
    370 	for (pda = asmap->physInfo, i = 0; i < nWndNodes; i++, pda = pda->next) {
    371 		if (pda == failedPDA) {
    372 			i--;
    373 			continue;
    374 		}
    375 		rf_InitNode(&wndNodes[i], rf_wait, RF_FALSE, rf_DiskWriteFunc, rf_DiskWriteUndoFunc,
    376 		    rf_GenericWakeupFunc, 1, 1, 4, 0, dag_h, "Wnd", allocList);
    377 		RF_ASSERT(pda);
    378 		wndNodes[i].params[0].p = pda;
    379 		wndNodes[i].params[1].p = pda->bufPtr;
    380 		wndNodes[i].params[2].v = parityStripeID;
    381 		wndNodes[i].params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, 0, 0, which_ru);
    382 	}
    383 
    384 	/* fill in the results of the xor node */
    385 	xorNode->results[0] = xorTargetBuf;
    386 
    387 	/* fill in the params of the xor node */
    388 
    389 	paramNum = 0;
    390 	if (rdnodesFaked == 0) {
    391 		for (i = 0; i < nRrdNodes; i++) {
    392 			/* all the Rrd nodes need to be xored together */
    393 			xorNode->params[paramNum++] = rrdNodes[i].params[0];
    394 			xorNode->params[paramNum++] = rrdNodes[i].params[1];
    395 		}
    396 	}
    397 	for (i = 0; i < nWndNodes; i++) {
    398 		/* any Wnd nodes that overlap the failed access need to be
    399 		 * xored in */
    400 		if (overlappingPDAs[i]) {
    401 			RF_MallocAndAdd(pda, sizeof(RF_PhysDiskAddr_t), (RF_PhysDiskAddr_t *), allocList);
    402 			bcopy((char *) wndNodes[i].params[0].p, (char *) pda, sizeof(RF_PhysDiskAddr_t));
    403 			rf_RangeRestrictPDA(raidPtr, failedPDA, pda, RF_RESTRICT_DOBUFFER, 0);
    404 			xorNode->params[paramNum++].p = pda;
    405 			xorNode->params[paramNum++].p = pda->bufPtr;
    406 		}
    407 	}
    408 	RF_Free(overlappingPDAs, asmap->numStripeUnitsAccessed * sizeof(char));
    409 
    410 	/*
    411          * Install the failed PDA into the xor param list so that the
    412          * new data gets xor'd in.
    413          */
    414 	xorNode->params[paramNum++].p = failedPDA;
    415 	xorNode->params[paramNum++].p = failedPDA->bufPtr;
    416 
    417 	/*
    418          * The last 2 params to the recovery xor node are always the failed
    419          * PDA and the raidPtr. install the failedPDA even though we have just
    420          * done so above. This allows us to use the same XOR function for both
    421          * degraded reads and degraded writes.
    422          */
    423 	xorNode->params[paramNum++].p = failedPDA;
    424 	xorNode->params[paramNum++].p = raidPtr;
    425 	RF_ASSERT(paramNum == 2 * nXorBufs + 2);
    426 
    427 	/*
    428          * Code to link nodes begins here
    429          */
    430 
    431 	/* link header to block node */
    432 	RF_ASSERT(blockNode->numAntecedents == 0);
    433 	dag_h->succedents[0] = blockNode;
    434 
    435 	/* link block node to rd nodes */
    436 	RF_ASSERT(blockNode->numSuccedents == nRrdNodes);
    437 	for (i = 0; i < nRrdNodes; i++) {
    438 		RF_ASSERT(rrdNodes[i].numAntecedents == 1);
    439 		blockNode->succedents[i] = &rrdNodes[i];
    440 		rrdNodes[i].antecedents[0] = blockNode;
    441 		rrdNodes[i].antType[0] = rf_control;
    442 	}
    443 
    444 	/* link read nodes to xor node */
    445 	RF_ASSERT(xorNode->numAntecedents == nRrdNodes);
    446 	for (i = 0; i < nRrdNodes; i++) {
    447 		RF_ASSERT(rrdNodes[i].numSuccedents == 1);
    448 		rrdNodes[i].succedents[0] = xorNode;
    449 		xorNode->antecedents[i] = &rrdNodes[i];
    450 		xorNode->antType[i] = rf_trueData;
    451 	}
    452 
    453 	/* link xor node to commit node */
    454 	RF_ASSERT(xorNode->numSuccedents == 1);
    455 	RF_ASSERT(commitNode->numAntecedents == 1);
    456 	xorNode->succedents[0] = commitNode;
    457 	commitNode->antecedents[0] = xorNode;
    458 	commitNode->antType[0] = rf_control;
    459 
    460 	/* link commit node to wnd nodes */
    461 	RF_ASSERT(commitNode->numSuccedents == nfaults + nWndNodes);
    462 	for (i = 0; i < nWndNodes; i++) {
    463 		RF_ASSERT(wndNodes[i].numAntecedents == 1);
    464 		commitNode->succedents[i] = &wndNodes[i];
    465 		wndNodes[i].antecedents[0] = commitNode;
    466 		wndNodes[i].antType[0] = rf_control;
    467 	}
    468 
    469 	/* link the commit node to wnp, wnq nodes */
    470 	RF_ASSERT(wnpNode->numAntecedents == 1);
    471 	commitNode->succedents[nWndNodes] = wnpNode;
    472 	wnpNode->antecedents[0] = commitNode;
    473 	wnpNode->antType[0] = rf_control;
    474 	if (nfaults == 2) {
    475 		RF_ASSERT(wnqNode->numAntecedents == 1);
    476 		commitNode->succedents[nWndNodes + 1] = wnqNode;
    477 		wnqNode->antecedents[0] = commitNode;
    478 		wnqNode->antType[0] = rf_control;
    479 	}
    480 	/* link write new data nodes to unblock node */
    481 	RF_ASSERT(unblockNode->numAntecedents == (nWndNodes + nfaults));
    482 	for (i = 0; i < nWndNodes; i++) {
    483 		RF_ASSERT(wndNodes[i].numSuccedents == 1);
    484 		wndNodes[i].succedents[0] = unblockNode;
    485 		unblockNode->antecedents[i] = &wndNodes[i];
    486 		unblockNode->antType[i] = rf_control;
    487 	}
    488 
    489 	/* link write new parity node to unblock node */
    490 	RF_ASSERT(wnpNode->numSuccedents == 1);
    491 	wnpNode->succedents[0] = unblockNode;
    492 	unblockNode->antecedents[nWndNodes] = wnpNode;
    493 	unblockNode->antType[nWndNodes] = rf_control;
    494 
    495 	/* link write new q node to unblock node */
    496 	if (nfaults == 2) {
    497 		RF_ASSERT(wnqNode->numSuccedents == 1);
    498 		wnqNode->succedents[0] = unblockNode;
    499 		unblockNode->antecedents[nWndNodes + 1] = wnqNode;
    500 		unblockNode->antType[nWndNodes + 1] = rf_control;
    501 	}
    502 	/* link unblock node to term node */
    503 	RF_ASSERT(unblockNode->numSuccedents == 1);
    504 	RF_ASSERT(termNode->numAntecedents == 1);
    505 	RF_ASSERT(termNode->numSuccedents == 0);
    506 	unblockNode->succedents[0] = termNode;
    507 	termNode->antecedents[0] = unblockNode;
    508 	termNode->antType[0] = rf_control;
    509 }
    510 #define CONS_PDA(if,start,num) \
    511   pda_p->row = asmap->if->row;    pda_p->col = asmap->if->col; \
    512   pda_p->startSector = ((asmap->if->startSector / secPerSU) * secPerSU) + start; \
    513   pda_p->numSector = num; \
    514   pda_p->next = NULL; \
    515   RF_MallocAndAdd(pda_p->bufPtr,rf_RaidAddressToByte(raidPtr,num),(char *), allocList)
    516 
    517 void
    518 rf_WriteGenerateFailedAccessASMs(
    519     RF_Raid_t * raidPtr,
    520     RF_AccessStripeMap_t * asmap,
    521     RF_PhysDiskAddr_t ** pdap,
    522     int *nNodep,
    523     RF_PhysDiskAddr_t ** pqpdap,
    524     int *nPQNodep,
    525     RF_AllocListElem_t * allocList)
    526 {
    527 	RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout);
    528 	int     PDAPerDisk, i;
    529 	RF_SectorCount_t secPerSU = layoutPtr->sectorsPerStripeUnit;
    530 	int     numDataCol = layoutPtr->numDataCol;
    531 	int     state;
    532 	unsigned napdas;
    533 	RF_SectorNum_t fone_start, fone_end, ftwo_start = 0, ftwo_end;
    534 	RF_PhysDiskAddr_t *fone = asmap->failedPDAs[0], *ftwo = asmap->failedPDAs[1];
    535 	RF_PhysDiskAddr_t *pda_p;
    536 	RF_RaidAddr_t sosAddr;
    537 
    538 	/* determine how many pda's we will have to generate per unaccess
    539 	 * stripe. If there is only one failed data unit, it is one; if two,
    540 	 * possibly two, depending wether they overlap. */
    541 
    542 	fone_start = rf_StripeUnitOffset(layoutPtr, fone->startSector);
    543 	fone_end = fone_start + fone->numSector;
    544 
    545 	if (asmap->numDataFailed == 1) {
    546 		PDAPerDisk = 1;
    547 		state = 1;
    548 		RF_MallocAndAdd(*pqpdap, 2 * sizeof(RF_PhysDiskAddr_t), (RF_PhysDiskAddr_t *), allocList);
    549 		pda_p = *pqpdap;
    550 		/* build p */
    551 		CONS_PDA(parityInfo, fone_start, fone->numSector);
    552 		pda_p->type = RF_PDA_TYPE_PARITY;
    553 		pda_p++;
    554 		/* build q */
    555 		CONS_PDA(qInfo, fone_start, fone->numSector);
    556 		pda_p->type = RF_PDA_TYPE_Q;
    557 	} else {
    558 		ftwo_start = rf_StripeUnitOffset(layoutPtr, ftwo->startSector);
    559 		ftwo_end = ftwo_start + ftwo->numSector;
    560 		if (fone->numSector + ftwo->numSector > secPerSU) {
    561 			PDAPerDisk = 1;
    562 			state = 2;
    563 			RF_MallocAndAdd(*pqpdap, 2 * sizeof(RF_PhysDiskAddr_t), (RF_PhysDiskAddr_t *), allocList);
    564 			pda_p = *pqpdap;
    565 			CONS_PDA(parityInfo, 0, secPerSU);
    566 			pda_p->type = RF_PDA_TYPE_PARITY;
    567 			pda_p++;
    568 			CONS_PDA(qInfo, 0, secPerSU);
    569 			pda_p->type = RF_PDA_TYPE_Q;
    570 		} else {
    571 			PDAPerDisk = 2;
    572 			state = 3;
    573 			/* four of them, fone, then ftwo */
    574 			RF_MallocAndAdd(*pqpdap, 4 * sizeof(RF_PhysDiskAddr_t), (RF_PhysDiskAddr_t *), allocList);
    575 			pda_p = *pqpdap;
    576 			CONS_PDA(parityInfo, fone_start, fone->numSector);
    577 			pda_p->type = RF_PDA_TYPE_PARITY;
    578 			pda_p++;
    579 			CONS_PDA(qInfo, fone_start, fone->numSector);
    580 			pda_p->type = RF_PDA_TYPE_Q;
    581 			pda_p++;
    582 			CONS_PDA(parityInfo, ftwo_start, ftwo->numSector);
    583 			pda_p->type = RF_PDA_TYPE_PARITY;
    584 			pda_p++;
    585 			CONS_PDA(qInfo, ftwo_start, ftwo->numSector);
    586 			pda_p->type = RF_PDA_TYPE_Q;
    587 		}
    588 	}
    589 	/* figure out number of nonaccessed pda */
    590 	napdas = PDAPerDisk * (numDataCol - 2);
    591 	*nPQNodep = PDAPerDisk;
    592 
    593 	*nNodep = napdas;
    594 	if (napdas == 0)
    595 		return;		/* short circuit */
    596 
    597 	/* allocate up our list of pda's */
    598 
    599 	RF_CallocAndAdd(pda_p, napdas, sizeof(RF_PhysDiskAddr_t), (RF_PhysDiskAddr_t *), allocList);
    600 	*pdap = pda_p;
    601 
    602 	/* linkem together */
    603 	for (i = 0; i < (napdas - 1); i++)
    604 		pda_p[i].next = pda_p + (i + 1);
    605 
    606 	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, asmap->raidAddress);
    607 	for (i = 0; i < numDataCol; i++) {
    608 		if ((pda_p - (*pdap)) == napdas)
    609 			continue;
    610 		pda_p->type = RF_PDA_TYPE_DATA;
    611 		pda_p->raidAddress = sosAddr + (i * secPerSU);
    612 		(raidPtr->Layout.map->MapSector) (raidPtr, pda_p->raidAddress, &(pda_p->row), &(pda_p->col), &(pda_p->startSector), 0);
    613 		/* skip over dead disks */
    614 		if (RF_DEAD_DISK(raidPtr->Disks[pda_p->row][pda_p->col].status))
    615 			continue;
    616 		switch (state) {
    617 		case 1:	/* fone */
    618 			pda_p->numSector = fone->numSector;
    619 			pda_p->raidAddress += fone_start;
    620 			pda_p->startSector += fone_start;
    621 			RF_MallocAndAdd(pda_p->bufPtr, rf_RaidAddressToByte(raidPtr, pda_p->numSector), (char *), allocList);
    622 			break;
    623 		case 2:	/* full stripe */
    624 			pda_p->numSector = secPerSU;
    625 			RF_MallocAndAdd(pda_p->bufPtr, rf_RaidAddressToByte(raidPtr, secPerSU), (char *), allocList);
    626 			break;
    627 		case 3:	/* two slabs */
    628 			pda_p->numSector = fone->numSector;
    629 			pda_p->raidAddress += fone_start;
    630 			pda_p->startSector += fone_start;
    631 			RF_MallocAndAdd(pda_p->bufPtr, rf_RaidAddressToByte(raidPtr, pda_p->numSector), (char *), allocList);
    632 			pda_p++;
    633 			pda_p->type = RF_PDA_TYPE_DATA;
    634 			pda_p->raidAddress = sosAddr + (i * secPerSU);
    635 			(raidPtr->Layout.map->MapSector) (raidPtr, pda_p->raidAddress, &(pda_p->row), &(pda_p->col), &(pda_p->startSector), 0);
    636 			pda_p->numSector = ftwo->numSector;
    637 			pda_p->raidAddress += ftwo_start;
    638 			pda_p->startSector += ftwo_start;
    639 			RF_MallocAndAdd(pda_p->bufPtr, rf_RaidAddressToByte(raidPtr, pda_p->numSector), (char *), allocList);
    640 			break;
    641 		default:
    642 			RF_PANIC();
    643 		}
    644 		pda_p++;
    645 	}
    646 
    647 	RF_ASSERT(pda_p - *pdap == napdas);
    648 	return;
    649 }
    650 #define DISK_NODE_PDA(node)  ((node)->params[0].p)
    651 
    652 #define DISK_NODE_PARAMS(_node_,_p_) \
    653   (_node_).params[0].p = _p_ ; \
    654   (_node_).params[1].p = (_p_)->bufPtr; \
    655   (_node_).params[2].v = parityStripeID; \
    656   (_node_).params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, 0, 0, which_ru)
    657 
    658 void
    659 rf_DoubleDegSmallWrite(
    660     RF_Raid_t * raidPtr,
    661     RF_AccessStripeMap_t * asmap,
    662     RF_DagHeader_t * dag_h,
    663     void *bp,
    664     RF_RaidAccessFlags_t flags,
    665     RF_AllocListElem_t * allocList,
    666     char *redundantReadNodeName,
    667     char *redundantWriteNodeName,
    668     char *recoveryNodeName,
    669     int (*recovFunc) (RF_DagNode_t *))
    670 {
    671 	RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout);
    672 	RF_DagNode_t *nodes, *wudNodes, *rrdNodes, *recoveryNode, *blockNode,
    673 	       *unblockNode, *rpNodes, *rqNodes, *wpNodes, *wqNodes, *termNode;
    674 	RF_PhysDiskAddr_t *pda, *pqPDAs;
    675 	RF_PhysDiskAddr_t *npdas;
    676 	int     nWriteNodes, nNodes, nReadNodes, nRrdNodes, nWudNodes, i;
    677 	RF_ReconUnitNum_t which_ru;
    678 	int     nPQNodes;
    679 	RF_StripeNum_t parityStripeID = rf_RaidAddressToParityStripeID(layoutPtr, asmap->raidAddress, &which_ru);
    680 
    681 	/* simple small write case - First part looks like a reconstruct-read
    682 	 * of the failed data units. Then a write of all data units not
    683 	 * failed. */
    684 
    685 
    686 	/* Hdr | ------Block- /  /         \   Rrd  Rrd ...  Rrd  Rp Rq \  \
    687 	 * /  -------PQ----- /   \   \ Wud   Wp  WQ	     \    |   /
    688 	 * --Unblock- | T
    689 	 *
    690 	 * Rrd = read recovery data  (potentially none) Wud = write user data
    691 	 * (not incl. failed disks) Wp = Write P (could be two) Wq = Write Q
    692 	 * (could be two)
    693 	 *
    694 	 */
    695 
    696 	rf_WriteGenerateFailedAccessASMs(raidPtr, asmap, &npdas, &nRrdNodes, &pqPDAs, &nPQNodes, allocList);
    697 
    698 	RF_ASSERT(asmap->numDataFailed == 1);
    699 
    700 	nWudNodes = asmap->numStripeUnitsAccessed - (asmap->numDataFailed);
    701 	nReadNodes = nRrdNodes + 2 * nPQNodes;
    702 	nWriteNodes = nWudNodes + 2 * nPQNodes;
    703 	nNodes = 4 + nReadNodes + nWriteNodes;
    704 
    705 	RF_CallocAndAdd(nodes, nNodes, sizeof(RF_DagNode_t), (RF_DagNode_t *), allocList);
    706 	blockNode = nodes;
    707 	unblockNode = blockNode + 1;
    708 	termNode = unblockNode + 1;
    709 	recoveryNode = termNode + 1;
    710 	rrdNodes = recoveryNode + 1;
    711 	rpNodes = rrdNodes + nRrdNodes;
    712 	rqNodes = rpNodes + nPQNodes;
    713 	wudNodes = rqNodes + nPQNodes;
    714 	wpNodes = wudNodes + nWudNodes;
    715 	wqNodes = wpNodes + nPQNodes;
    716 
    717 	dag_h->creator = "PQ_DDSimpleSmallWrite";
    718 	dag_h->numSuccedents = 1;
    719 	dag_h->succedents[0] = blockNode;
    720 	rf_InitNode(termNode, rf_wait, RF_FALSE, rf_TerminateFunc, rf_TerminateUndoFunc, NULL, 0, 1, 0, 0, dag_h, "Trm", allocList);
    721 	termNode->antecedents[0] = unblockNode;
    722 	termNode->antType[0] = rf_control;
    723 
    724 	/* init the block and unblock nodes */
    725 	/* The block node has all the read nodes as successors */
    726 	rf_InitNode(blockNode, rf_wait, RF_FALSE, rf_NullNodeFunc, rf_NullNodeUndoFunc, NULL, nReadNodes, 0, 0, 0, dag_h, "Nil", allocList);
    727 	for (i = 0; i < nReadNodes; i++)
    728 		blockNode->succedents[i] = rrdNodes + i;
    729 
    730 	/* The unblock node has all the writes as successors */
    731 	rf_InitNode(unblockNode, rf_wait, RF_FALSE, rf_NullNodeFunc, rf_NullNodeUndoFunc, NULL, 1, nWriteNodes, 0, 0, dag_h, "Nil", allocList);
    732 	for (i = 0; i < nWriteNodes; i++) {
    733 		unblockNode->antecedents[i] = wudNodes + i;
    734 		unblockNode->antType[i] = rf_control;
    735 	}
    736 	unblockNode->succedents[0] = termNode;
    737 
    738 #define INIT_READ_NODE(node,name) \
    739   rf_InitNode(node, rf_wait, RF_FALSE, rf_DiskReadFunc, rf_DiskReadUndoFunc, rf_GenericWakeupFunc, 1, 1, 4, 0, dag_h, name, allocList); \
    740   (node)->succedents[0] = recoveryNode; \
    741   (node)->antecedents[0] = blockNode; \
    742   (node)->antType[0] = rf_control;
    743 
    744 	/* build the read nodes */
    745 	pda = npdas;
    746 	for (i = 0; i < nRrdNodes; i++, pda = pda->next) {
    747 		INIT_READ_NODE(rrdNodes + i, "rrd");
    748 		DISK_NODE_PARAMS(rrdNodes[i], pda);
    749 	}
    750 
    751 	/* read redundancy pdas */
    752 	pda = pqPDAs;
    753 	INIT_READ_NODE(rpNodes, "Rp");
    754 	RF_ASSERT(pda);
    755 	DISK_NODE_PARAMS(rpNodes[0], pda);
    756 	pda++;
    757 	INIT_READ_NODE(rqNodes, redundantReadNodeName);
    758 	RF_ASSERT(pda);
    759 	DISK_NODE_PARAMS(rqNodes[0], pda);
    760 	if (nPQNodes == 2) {
    761 		pda++;
    762 		INIT_READ_NODE(rpNodes + 1, "Rp");
    763 		RF_ASSERT(pda);
    764 		DISK_NODE_PARAMS(rpNodes[1], pda);
    765 		pda++;
    766 		INIT_READ_NODE(rqNodes + 1, redundantReadNodeName);
    767 		RF_ASSERT(pda);
    768 		DISK_NODE_PARAMS(rqNodes[1], pda);
    769 	}
    770 	/* the recovery node has all reads as precedessors and all writes as
    771 	 * successors. It generates a result for every write P or write Q
    772 	 * node. As parameters, it takes a pda per read and a pda per stripe
    773 	 * of user data written. It also takes as the last params the raidPtr
    774 	 * and asm. For results, it takes PDA for P & Q. */
    775 
    776 
    777 	rf_InitNode(recoveryNode, rf_wait, RF_FALSE, recovFunc, rf_NullNodeUndoFunc, NULL,
    778 	    nWriteNodes,	/* succesors */
    779 	    nReadNodes,		/* preds */
    780 	    nReadNodes + nWudNodes + 3,	/* params */
    781 	    2 * nPQNodes,	/* results */
    782 	    dag_h, recoveryNodeName, allocList);
    783 
    784 
    785 
    786 	for (i = 0; i < nReadNodes; i++) {
    787 		recoveryNode->antecedents[i] = rrdNodes + i;
    788 		recoveryNode->antType[i] = rf_control;
    789 		recoveryNode->params[i].p = DISK_NODE_PDA(rrdNodes + i);
    790 	}
    791 	for (i = 0; i < nWudNodes; i++) {
    792 		recoveryNode->succedents[i] = wudNodes + i;
    793 	}
    794 	recoveryNode->params[nReadNodes + nWudNodes].p = asmap->failedPDAs[0];
    795 	recoveryNode->params[nReadNodes + nWudNodes + 1].p = raidPtr;
    796 	recoveryNode->params[nReadNodes + nWudNodes + 2].p = asmap;
    797 
    798 	for (; i < nWriteNodes; i++)
    799 		recoveryNode->succedents[i] = wudNodes + i;
    800 
    801 	pda = pqPDAs;
    802 	recoveryNode->results[0] = pda;
    803 	pda++;
    804 	recoveryNode->results[1] = pda;
    805 	if (nPQNodes == 2) {
    806 		pda++;
    807 		recoveryNode->results[2] = pda;
    808 		pda++;
    809 		recoveryNode->results[3] = pda;
    810 	}
    811 	/* fill writes */
    812 #define INIT_WRITE_NODE(node,name) \
    813   rf_InitNode(node, rf_wait, RF_FALSE, rf_DiskWriteFunc, rf_DiskWriteUndoFunc, rf_GenericWakeupFunc, 1, 1, 4, 0, dag_h, name, allocList); \
    814     (node)->succedents[0] = unblockNode; \
    815     (node)->antecedents[0] = recoveryNode; \
    816     (node)->antType[0] = rf_control;
    817 
    818 	pda = asmap->physInfo;
    819 	for (i = 0; i < nWudNodes; i++) {
    820 		INIT_WRITE_NODE(wudNodes + i, "Wd");
    821 		DISK_NODE_PARAMS(wudNodes[i], pda);
    822 		recoveryNode->params[nReadNodes + i].p = DISK_NODE_PDA(wudNodes + i);
    823 		pda = pda->next;
    824 	}
    825 	/* write redundancy pdas */
    826 	pda = pqPDAs;
    827 	INIT_WRITE_NODE(wpNodes, "Wp");
    828 	RF_ASSERT(pda);
    829 	DISK_NODE_PARAMS(wpNodes[0], pda);
    830 	pda++;
    831 	INIT_WRITE_NODE(wqNodes, "Wq");
    832 	RF_ASSERT(pda);
    833 	DISK_NODE_PARAMS(wqNodes[0], pda);
    834 	if (nPQNodes == 2) {
    835 		pda++;
    836 		INIT_WRITE_NODE(wpNodes + 1, "Wp");
    837 		RF_ASSERT(pda);
    838 		DISK_NODE_PARAMS(wpNodes[1], pda);
    839 		pda++;
    840 		INIT_WRITE_NODE(wqNodes + 1, "Wq");
    841 		RF_ASSERT(pda);
    842 		DISK_NODE_PARAMS(wqNodes[1], pda);
    843 	}
    844 }
    845