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