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