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