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