rf_dagdegwr.c revision 1.16
1/*	$NetBSD: rf_dagdegwr.c,v 1.16 2004/03/05 03:22:05 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 <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: rf_dagdegwr.c,v 1.16 2004/03/05 03:22:05 oster Exp $");
38
39#include <dev/raidframe/raidframevar.h>
40
41#include "rf_raid.h"
42#include "rf_dag.h"
43#include "rf_dagutils.h"
44#include "rf_dagfuncs.h"
45#include "rf_debugMem.h"
46#include "rf_general.h"
47#include "rf_dagdegwr.h"
48
49
50/******************************************************************************
51 *
52 * General comments on DAG creation:
53 *
54 * All DAGs in this file use roll-away error recovery.  Each DAG has a single
55 * commit node, usually called "Cmt."  If an error occurs before the Cmt node
56 * is reached, the execution engine will halt forward execution and work
57 * backward through the graph, executing the undo functions.  Assuming that
58 * each node in the graph prior to the Cmt node are undoable and atomic - or -
59 * does not make changes to permanent state, the graph will fail atomically.
60 * If an error occurs after the Cmt node executes, the engine will roll-forward
61 * through the graph, blindly executing nodes until it reaches the end.
62 * If a graph reaches the end, it is assumed to have completed successfully.
63 *
64 * A graph has only 1 Cmt node.
65 *
66 */
67
68
69/******************************************************************************
70 *
71 * The following wrappers map the standard DAG creation interface to the
72 * DAG creation routines.  Additionally, these wrappers enable experimentation
73 * with new DAG structures by providing an extra level of indirection, allowing
74 * the DAG creation routines to be replaced at this single point.
75 */
76
77static
78RF_CREATE_DAG_FUNC_DECL(rf_CreateSimpleDegradedWriteDAG)
79{
80	rf_CommonCreateSimpleDegradedWriteDAG(raidPtr, asmap, dag_h, bp,
81	    flags, allocList, 1, rf_RecoveryXorFunc, RF_TRUE);
82}
83
84void
85rf_CreateDegradedWriteDAG(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap,
86			  RF_DagHeader_t *dag_h, 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(RF_Raid_t *raidPtr,
155				      RF_AccessStripeMap_t *asmap,
156				      RF_DagHeader_t *dag_h, 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_DEBUG_DAG
187	if (rf_dagDebug)
188		printf("[Creating degraded-write DAG]\n");
189#endif
190
191	RF_ASSERT(asmap->numDataFailed == 1);
192	dag_h->creator = "SimpleDegradedWriteDAG";
193
194	/*
195         * Generate two ASMs identifying the surviving data
196         * we need in order to recover the lost data.
197         */
198	/* overlappingPDAs array must be zero'd */
199	RF_Malloc(overlappingPDAs, asmap->numStripeUnitsAccessed * sizeof(char), (char *));
200	rf_GenerateFailedAccessASMs(raidPtr, asmap, failedPDA, dag_h, new_asm_h,
201	    &nXorBufs, NULL, overlappingPDAs, allocList);
202
203	/* create all the nodes at once */
204	nWndNodes = asmap->numStripeUnitsAccessed - 1;	/* no access is
205							 * generated for the
206							 * failed pda */
207
208	nRrdNodes = ((new_asm_h[0]) ? new_asm_h[0]->stripeMap->numStripeUnitsAccessed : 0) +
209	    ((new_asm_h[1]) ? new_asm_h[1]->stripeMap->numStripeUnitsAccessed : 0);
210	/*
211         * XXX
212         *
213         * There's a bug with a complete stripe overwrite- that means 0 reads
214         * of old data, and the rest of the DAG generation code doesn't like
215         * that. A release is coming, and I don't wanna risk breaking a critical
216         * DAG generator, so here's what I'm gonna do- if there's no read nodes,
217         * I'm gonna fake there being a read node, and I'm gonna swap in a
218         * no-op node in its place (to make all the link-up code happy).
219         * This should be fixed at some point.  --jimz
220         */
221	if (nRrdNodes == 0) {
222		nRrdNodes = 1;
223		rdnodesFaked = 1;
224	} else {
225		rdnodesFaked = 0;
226	}
227	/* lock, unlock, xor, Wnd, Rrd, W(nfaults) */
228	nNodes = 5 + nfaults + nWndNodes + nRrdNodes;
229	RF_MallocAndAdd(nodes, nNodes * sizeof(RF_DagNode_t),
230	    (RF_DagNode_t *), allocList);
231	i = 0;
232	blockNode = &nodes[i];
233	i += 1;
234	commitNode = &nodes[i];
235	i += 1;
236	unblockNode = &nodes[i];
237	i += 1;
238	termNode = &nodes[i];
239	i += 1;
240	xorNode = &nodes[i];
241	i += 1;
242	wnpNode = &nodes[i];
243	i += 1;
244	wndNodes = &nodes[i];
245	i += nWndNodes;
246	rrdNodes = &nodes[i];
247	i += nRrdNodes;
248	if (nfaults == 2) {
249		wnqNode = &nodes[i];
250		i += 1;
251	} else {
252		wnqNode = NULL;
253	}
254	RF_ASSERT(i == nNodes);
255
256	/* this dag can not commit until all rrd and xor Nodes have completed */
257	dag_h->numCommitNodes = 1;
258	dag_h->numCommits = 0;
259	dag_h->numSuccedents = 1;
260
261	RF_ASSERT(nRrdNodes > 0);
262	rf_InitNode(blockNode, rf_wait, RF_FALSE, rf_NullNodeFunc, rf_NullNodeUndoFunc,
263	    NULL, nRrdNodes, 0, 0, 0, dag_h, "Nil", allocList);
264	rf_InitNode(commitNode, rf_wait, RF_TRUE, rf_NullNodeFunc, rf_NullNodeUndoFunc,
265	    NULL, nWndNodes + nfaults, 1, 0, 0, dag_h, "Cmt", allocList);
266	rf_InitNode(unblockNode, rf_wait, RF_FALSE, rf_NullNodeFunc, rf_NullNodeUndoFunc,
267	    NULL, 1, nWndNodes + nfaults, 0, 0, dag_h, "Nil", allocList);
268	rf_InitNode(termNode, rf_wait, RF_FALSE, rf_TerminateFunc, rf_TerminateUndoFunc,
269	    NULL, 0, 1, 0, 0, dag_h, "Trm", allocList);
270	rf_InitNode(xorNode, rf_wait, RF_FALSE, redFunc, rf_NullNodeUndoFunc, NULL, 1,
271	    nRrdNodes, 2 * nXorBufs + 2, nfaults, dag_h, "Xrc", allocList);
272
273	/*
274         * Fill in the Rrd nodes. If any of the rrd buffers are the same size as
275         * the failed buffer, save a pointer to it so we can use it as the target
276         * of the XOR. The pdas in the rrd nodes have been range-restricted, so if
277         * a buffer is the same size as the failed buffer, it must also be at the
278         * same alignment within the SU.
279         */
280	i = 0;
281	if (new_asm_h[0]) {
282		for (i = 0, pda = new_asm_h[0]->stripeMap->physInfo;
283		    i < new_asm_h[0]->stripeMap->numStripeUnitsAccessed;
284		    i++, pda = pda->next) {
285			rf_InitNode(&rrdNodes[i], rf_wait, RF_FALSE, rf_DiskReadFunc, rf_DiskReadUndoFunc,
286			    rf_GenericWakeupFunc, 1, 1, 4, 0, dag_h, "Rrd", allocList);
287			RF_ASSERT(pda);
288			rrdNodes[i].params[0].p = pda;
289			rrdNodes[i].params[1].p = pda->bufPtr;
290			rrdNodes[i].params[2].v = parityStripeID;
291			rrdNodes[i].params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, which_ru);
292		}
293	}
294	/* i now equals the number of stripe units accessed in new_asm_h[0] */
295	if (new_asm_h[1]) {
296		for (j = 0, pda = new_asm_h[1]->stripeMap->physInfo;
297		    j < new_asm_h[1]->stripeMap->numStripeUnitsAccessed;
298		    j++, pda = pda->next) {
299			rf_InitNode(&rrdNodes[i + j], rf_wait, RF_FALSE, rf_DiskReadFunc, rf_DiskReadUndoFunc,
300			    rf_GenericWakeupFunc, 1, 1, 4, 0, dag_h, "Rrd", allocList);
301			RF_ASSERT(pda);
302			rrdNodes[i + j].params[0].p = pda;
303			rrdNodes[i + j].params[1].p = pda->bufPtr;
304			rrdNodes[i + j].params[2].v = parityStripeID;
305			rrdNodes[i + j].params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, which_ru);
306			if (allowBufferRecycle && (pda->numSector == failedPDA->numSector))
307				xorTargetBuf = pda->bufPtr;
308		}
309	}
310	if (rdnodesFaked) {
311		/*
312	         * This is where we'll init that fake noop read node
313	         * (XXX should the wakeup func be different?)
314	         */
315		rf_InitNode(&rrdNodes[0], rf_wait, RF_FALSE, rf_NullNodeFunc, rf_NullNodeUndoFunc,
316		    NULL, 1, 1, 0, 0, dag_h, "RrN", allocList);
317	}
318	/*
319         * Make a PDA for the parity unit.  The parity PDA should start at
320         * the same offset into the SU as the failed PDA.
321         */
322	/* Danner comment: I don't think this copy is really necessary. We are
323	 * in one of two cases here. (1) The entire failed unit is written.
324	 * Then asmap->parityInfo will describe the entire parity. (2) We are
325	 * only writing a subset of the failed unit and nothing else. Then the
326	 * asmap->parityInfo describes the failed unit and the copy can also
327	 * be avoided. */
328
329	RF_MallocAndAdd(parityPDA, sizeof(RF_PhysDiskAddr_t), (RF_PhysDiskAddr_t *), allocList);
330	parityPDA->col = asmap->parityInfo->col;
331	parityPDA->startSector = ((asmap->parityInfo->startSector / sectorsPerSU)
332	    * sectorsPerSU) + (failedPDA->startSector % sectorsPerSU);
333	parityPDA->numSector = failedPDA->numSector;
334
335	if (!xorTargetBuf) {
336		RF_MallocAndAdd(xorTargetBuf,
337		    rf_RaidAddressToByte(raidPtr, failedPDA->numSector), (char *), allocList);
338	}
339	/* init the Wnp node */
340	rf_InitNode(wnpNode, rf_wait, RF_FALSE, rf_DiskWriteFunc, rf_DiskWriteUndoFunc,
341	    rf_GenericWakeupFunc, 1, 1, 4, 0, dag_h, "Wnp", allocList);
342	wnpNode->params[0].p = parityPDA;
343	wnpNode->params[1].p = xorTargetBuf;
344	wnpNode->params[2].v = parityStripeID;
345	wnpNode->params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, which_ru);
346
347	/* fill in the Wnq Node */
348	if (nfaults == 2) {
349		{
350			RF_MallocAndAdd(parityPDA, sizeof(RF_PhysDiskAddr_t),
351			    (RF_PhysDiskAddr_t *), allocList);
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_MallocAndAdd(xorNode->results[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, 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, 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			memcpy((char *) pda, (char *) wndNodes[i].params[0].p, 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->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#if (RF_INCLUDE_PQ > 0) || (RF_INCLUDE_EVENODD > 0)
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_MallocAndAdd(pda_p, napdas * sizeof(RF_PhysDiskAddr_t),
598			(RF_PhysDiskAddr_t *), allocList);
599	*pdap = pda_p;
600
601	/* linkem together */
602	for (i = 0; i < (napdas - 1); i++)
603		pda_p[i].next = pda_p + (i + 1);
604
605	sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, asmap->raidAddress);
606	for (i = 0; i < numDataCol; i++) {
607		if ((pda_p - (*pdap)) == napdas)
608			continue;
609		pda_p->type = RF_PDA_TYPE_DATA;
610		pda_p->raidAddress = sosAddr + (i * secPerSU);
611		(raidPtr->Layout.map->MapSector) (raidPtr, pda_p->raidAddress, &(pda_p->col), &(pda_p->startSector), 0);
612		/* skip over dead disks */
613		if (RF_DEAD_DISK(raidPtr->Disks[pda_p->col].status))
614			continue;
615		switch (state) {
616		case 1:	/* fone */
617			pda_p->numSector = fone->numSector;
618			pda_p->raidAddress += fone_start;
619			pda_p->startSector += fone_start;
620			RF_MallocAndAdd(pda_p->bufPtr, rf_RaidAddressToByte(raidPtr, pda_p->numSector), (char *), allocList);
621			break;
622		case 2:	/* full stripe */
623			pda_p->numSector = secPerSU;
624			RF_MallocAndAdd(pda_p->bufPtr, rf_RaidAddressToByte(raidPtr, secPerSU), (char *), allocList);
625			break;
626		case 3:	/* two slabs */
627			pda_p->numSector = fone->numSector;
628			pda_p->raidAddress += fone_start;
629			pda_p->startSector += fone_start;
630			RF_MallocAndAdd(pda_p->bufPtr, rf_RaidAddressToByte(raidPtr, pda_p->numSector), (char *), allocList);
631			pda_p++;
632			pda_p->type = RF_PDA_TYPE_DATA;
633			pda_p->raidAddress = sosAddr + (i * secPerSU);
634			(raidPtr->Layout.map->MapSector) (raidPtr, pda_p->raidAddress, &(pda_p->col), &(pda_p->startSector), 0);
635			pda_p->numSector = ftwo->numSector;
636			pda_p->raidAddress += ftwo_start;
637			pda_p->startSector += ftwo_start;
638			RF_MallocAndAdd(pda_p->bufPtr, rf_RaidAddressToByte(raidPtr, pda_p->numSector), (char *), allocList);
639			break;
640		default:
641			RF_PANIC();
642		}
643		pda_p++;
644	}
645
646	RF_ASSERT(pda_p - *pdap == napdas);
647	return;
648}
649#define DISK_NODE_PDA(node)  ((node)->params[0].p)
650
651#define DISK_NODE_PARAMS(_node_,_p_) \
652  (_node_).params[0].p = _p_ ; \
653  (_node_).params[1].p = (_p_)->bufPtr; \
654  (_node_).params[2].v = parityStripeID; \
655  (_node_).params[3].v = RF_CREATE_PARAM3(RF_IO_NORMAL_PRIORITY, which_ru)
656
657void
658rf_DoubleDegSmallWrite(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap,
659		       RF_DagHeader_t *dag_h, void *bp,
660		       RF_RaidAccessFlags_t flags,
661		       RF_AllocListElem_t *allocList,
662		       char *redundantReadNodeName,
663		       char *redundantWriteNodeName,
664		       char *recoveryNodeName,
665		       int (*recovFunc) (RF_DagNode_t *))
666{
667	RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout);
668	RF_DagNode_t *nodes, *wudNodes, *rrdNodes, *recoveryNode, *blockNode,
669	       *unblockNode, *rpNodes, *rqNodes, *wpNodes, *wqNodes, *termNode;
670	RF_PhysDiskAddr_t *pda, *pqPDAs;
671	RF_PhysDiskAddr_t *npdas;
672	int     nWriteNodes, nNodes, nReadNodes, nRrdNodes, nWudNodes, i;
673	RF_ReconUnitNum_t which_ru;
674	int     nPQNodes;
675	RF_StripeNum_t parityStripeID = rf_RaidAddressToParityStripeID(layoutPtr, asmap->raidAddress, &which_ru);
676
677	/* simple small write case - First part looks like a reconstruct-read
678	 * of the failed data units. Then a write of all data units not
679	 * failed. */
680
681
682	/* Hdr | ------Block- /  /         \   Rrd  Rrd ...  Rrd  Rp Rq \  \
683	 * /  -------PQ----- /   \   \ Wud   Wp  WQ	     \    |   /
684	 * --Unblock- | T
685	 *
686	 * Rrd = read recovery data  (potentially none) Wud = write user data
687	 * (not incl. failed disks) Wp = Write P (could be two) Wq = Write Q
688	 * (could be two)
689	 *
690	 */
691
692	rf_WriteGenerateFailedAccessASMs(raidPtr, asmap, &npdas, &nRrdNodes, &pqPDAs, &nPQNodes, allocList);
693
694	RF_ASSERT(asmap->numDataFailed == 1);
695
696	nWudNodes = asmap->numStripeUnitsAccessed - (asmap->numDataFailed);
697	nReadNodes = nRrdNodes + 2 * nPQNodes;
698	nWriteNodes = nWudNodes + 2 * nPQNodes;
699	nNodes = 4 + nReadNodes + nWriteNodes;
700
701	RF_MallocAndAdd(nodes, nNodes * sizeof(RF_DagNode_t), (RF_DagNode_t *), allocList);
702	blockNode = nodes;
703	unblockNode = blockNode + 1;
704	termNode = unblockNode + 1;
705	recoveryNode = termNode + 1;
706	rrdNodes = recoveryNode + 1;
707	rpNodes = rrdNodes + nRrdNodes;
708	rqNodes = rpNodes + nPQNodes;
709	wudNodes = rqNodes + nPQNodes;
710	wpNodes = wudNodes + nWudNodes;
711	wqNodes = wpNodes + nPQNodes;
712
713	dag_h->creator = "PQ_DDSimpleSmallWrite";
714	dag_h->numSuccedents = 1;
715	dag_h->succedents[0] = blockNode;
716	rf_InitNode(termNode, rf_wait, RF_FALSE, rf_TerminateFunc, rf_TerminateUndoFunc, NULL, 0, 1, 0, 0, dag_h, "Trm", allocList);
717	termNode->antecedents[0] = unblockNode;
718	termNode->antType[0] = rf_control;
719
720	/* init the block and unblock nodes */
721	/* The block node has all the read nodes as successors */
722	rf_InitNode(blockNode, rf_wait, RF_FALSE, rf_NullNodeFunc, rf_NullNodeUndoFunc, NULL, nReadNodes, 0, 0, 0, dag_h, "Nil", allocList);
723	for (i = 0; i < nReadNodes; i++)
724		blockNode->succedents[i] = rrdNodes + i;
725
726	/* The unblock node has all the writes as successors */
727	rf_InitNode(unblockNode, rf_wait, RF_FALSE, rf_NullNodeFunc, rf_NullNodeUndoFunc, NULL, 1, nWriteNodes, 0, 0, dag_h, "Nil", allocList);
728	for (i = 0; i < nWriteNodes; i++) {
729		unblockNode->antecedents[i] = wudNodes + i;
730		unblockNode->antType[i] = rf_control;
731	}
732	unblockNode->succedents[0] = termNode;
733
734#define INIT_READ_NODE(node,name) \
735  rf_InitNode(node, rf_wait, RF_FALSE, rf_DiskReadFunc, rf_DiskReadUndoFunc, rf_GenericWakeupFunc, 1, 1, 4, 0, dag_h, name, allocList); \
736  (node)->succedents[0] = recoveryNode; \
737  (node)->antecedents[0] = blockNode; \
738  (node)->antType[0] = rf_control;
739
740	/* build the read nodes */
741	pda = npdas;
742	for (i = 0; i < nRrdNodes; i++, pda = pda->next) {
743		INIT_READ_NODE(rrdNodes + i, "rrd");
744		DISK_NODE_PARAMS(rrdNodes[i], pda);
745	}
746
747	/* read redundancy pdas */
748	pda = pqPDAs;
749	INIT_READ_NODE(rpNodes, "Rp");
750	RF_ASSERT(pda);
751	DISK_NODE_PARAMS(rpNodes[0], pda);
752	pda++;
753	INIT_READ_NODE(rqNodes, redundantReadNodeName);
754	RF_ASSERT(pda);
755	DISK_NODE_PARAMS(rqNodes[0], pda);
756	if (nPQNodes == 2) {
757		pda++;
758		INIT_READ_NODE(rpNodes + 1, "Rp");
759		RF_ASSERT(pda);
760		DISK_NODE_PARAMS(rpNodes[1], pda);
761		pda++;
762		INIT_READ_NODE(rqNodes + 1, redundantReadNodeName);
763		RF_ASSERT(pda);
764		DISK_NODE_PARAMS(rqNodes[1], pda);
765	}
766	/* the recovery node has all reads as precedessors and all writes as
767	 * successors. It generates a result for every write P or write Q
768	 * node. As parameters, it takes a pda per read and a pda per stripe
769	 * of user data written. It also takes as the last params the raidPtr
770	 * and asm. For results, it takes PDA for P & Q. */
771
772
773	rf_InitNode(recoveryNode, rf_wait, RF_FALSE, recovFunc, rf_NullNodeUndoFunc, NULL,
774	    nWriteNodes,	/* succesors */
775	    nReadNodes,		/* preds */
776	    nReadNodes + nWudNodes + 3,	/* params */
777	    2 * nPQNodes,	/* results */
778	    dag_h, recoveryNodeName, allocList);
779
780
781
782	for (i = 0; i < nReadNodes; i++) {
783		recoveryNode->antecedents[i] = rrdNodes + i;
784		recoveryNode->antType[i] = rf_control;
785		recoveryNode->params[i].p = DISK_NODE_PDA(rrdNodes + i);
786	}
787	for (i = 0; i < nWudNodes; i++) {
788		recoveryNode->succedents[i] = wudNodes + i;
789	}
790	recoveryNode->params[nReadNodes + nWudNodes].p = asmap->failedPDAs[0];
791	recoveryNode->params[nReadNodes + nWudNodes + 1].p = raidPtr;
792	recoveryNode->params[nReadNodes + nWudNodes + 2].p = asmap;
793
794	for (; i < nWriteNodes; i++)
795		recoveryNode->succedents[i] = wudNodes + i;
796
797	pda = pqPDAs;
798	recoveryNode->results[0] = pda;
799	pda++;
800	recoveryNode->results[1] = pda;
801	if (nPQNodes == 2) {
802		pda++;
803		recoveryNode->results[2] = pda;
804		pda++;
805		recoveryNode->results[3] = pda;
806	}
807	/* fill writes */
808#define INIT_WRITE_NODE(node,name) \
809  rf_InitNode(node, rf_wait, RF_FALSE, rf_DiskWriteFunc, rf_DiskWriteUndoFunc, rf_GenericWakeupFunc, 1, 1, 4, 0, dag_h, name, allocList); \
810    (node)->succedents[0] = unblockNode; \
811    (node)->antecedents[0] = recoveryNode; \
812    (node)->antType[0] = rf_control;
813
814	pda = asmap->physInfo;
815	for (i = 0; i < nWudNodes; i++) {
816		INIT_WRITE_NODE(wudNodes + i, "Wd");
817		DISK_NODE_PARAMS(wudNodes[i], pda);
818		recoveryNode->params[nReadNodes + i].p = DISK_NODE_PDA(wudNodes + i);
819		pda = pda->next;
820	}
821	/* write redundancy pdas */
822	pda = pqPDAs;
823	INIT_WRITE_NODE(wpNodes, "Wp");
824	RF_ASSERT(pda);
825	DISK_NODE_PARAMS(wpNodes[0], pda);
826	pda++;
827	INIT_WRITE_NODE(wqNodes, "Wq");
828	RF_ASSERT(pda);
829	DISK_NODE_PARAMS(wqNodes[0], pda);
830	if (nPQNodes == 2) {
831		pda++;
832		INIT_WRITE_NODE(wpNodes + 1, "Wp");
833		RF_ASSERT(pda);
834		DISK_NODE_PARAMS(wpNodes[1], pda);
835		pda++;
836		INIT_WRITE_NODE(wqNodes + 1, "Wq");
837		RF_ASSERT(pda);
838		DISK_NODE_PARAMS(wqNodes[1], pda);
839	}
840}
841#endif   /* (RF_INCLUDE_PQ > 0) || (RF_INCLUDE_EVENODD > 0) */
842