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