rf_dagutils.c revision 1.39 1 1.39 oster /* $NetBSD: rf_dagutils.c,v 1.39 2004/03/19 15:16:18 oster Exp $ */
2 1.1 oster /*
3 1.1 oster * Copyright (c) 1995 Carnegie-Mellon University.
4 1.1 oster * All rights reserved.
5 1.1 oster *
6 1.1 oster * Authors: Mark Holland, William V. Courtright II, Jim Zelenka
7 1.1 oster *
8 1.1 oster * Permission to use, copy, modify and distribute this software and
9 1.1 oster * its documentation is hereby granted, provided that both the copyright
10 1.1 oster * notice and this permission notice appear in all copies of the
11 1.1 oster * software, derivative works or modified versions, and any portions
12 1.1 oster * thereof, and that both notices appear in supporting documentation.
13 1.1 oster *
14 1.1 oster * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 1.1 oster * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
16 1.1 oster * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 1.1 oster *
18 1.1 oster * Carnegie Mellon requests users of this software to return to
19 1.1 oster *
20 1.1 oster * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 1.1 oster * School of Computer Science
22 1.1 oster * Carnegie Mellon University
23 1.1 oster * Pittsburgh PA 15213-3890
24 1.1 oster *
25 1.1 oster * any improvements or extensions that they make and grant Carnegie the
26 1.1 oster * rights to redistribute these changes.
27 1.1 oster */
28 1.1 oster
29 1.1 oster /******************************************************************************
30 1.1 oster *
31 1.1 oster * rf_dagutils.c -- utility routines for manipulating dags
32 1.1 oster *
33 1.1 oster *****************************************************************************/
34 1.9 lukem
35 1.9 lukem #include <sys/cdefs.h>
36 1.39 oster __KERNEL_RCSID(0, "$NetBSD: rf_dagutils.c,v 1.39 2004/03/19 15:16:18 oster Exp $");
37 1.1 oster
38 1.8 oster #include <dev/raidframe/raidframevar.h>
39 1.8 oster
40 1.1 oster #include "rf_archs.h"
41 1.1 oster #include "rf_threadstuff.h"
42 1.1 oster #include "rf_raid.h"
43 1.1 oster #include "rf_dag.h"
44 1.1 oster #include "rf_dagutils.h"
45 1.1 oster #include "rf_dagfuncs.h"
46 1.1 oster #include "rf_general.h"
47 1.1 oster #include "rf_map.h"
48 1.1 oster #include "rf_shutdown.h"
49 1.1 oster
50 1.1 oster #define SNUM_DIFF(_a_,_b_) (((_a_)>(_b_))?((_a_)-(_b_)):((_b_)-(_a_)))
51 1.1 oster
52 1.20 jdolecek const RF_RedFuncs_t rf_xorFuncs = {
53 1.1 oster rf_RegularXorFunc, "Reg Xr",
54 1.32 oster rf_SimpleXorFunc, "Simple Xr"};
55 1.1 oster
56 1.20 jdolecek const RF_RedFuncs_t rf_xorRecoveryFuncs = {
57 1.1 oster rf_RecoveryXorFunc, "Recovery Xr",
58 1.32 oster rf_RecoveryXorFunc, "Recovery Xr"};
59 1.1 oster
60 1.13 oster #if RF_DEBUG_VALIDATE_DAG
61 1.1 oster static void rf_RecurPrintDAG(RF_DagNode_t *, int, int);
62 1.1 oster static void rf_PrintDAG(RF_DagHeader_t *);
63 1.12 oster static int rf_ValidateBranch(RF_DagNode_t *, int *, int *,
64 1.12 oster RF_DagNode_t **, int);
65 1.1 oster static void rf_ValidateBranchVisitedBits(RF_DagNode_t *, int, int);
66 1.1 oster static void rf_ValidateVisitedBits(RF_DagHeader_t *);
67 1.13 oster #endif /* RF_DEBUG_VALIDATE_DAG */
68 1.1 oster
69 1.1 oster /******************************************************************************
70 1.1 oster *
71 1.1 oster * InitNode - initialize a dag node
72 1.1 oster *
73 1.1 oster * the size of the propList array is always the same as that of the
74 1.1 oster * successors array.
75 1.1 oster *
76 1.1 oster *****************************************************************************/
77 1.3 oster void
78 1.23 oster rf_InitNode(RF_DagNode_t *node, RF_NodeStatus_t initstatus, int commit,
79 1.23 oster int (*doFunc) (RF_DagNode_t *node),
80 1.23 oster int (*undoFunc) (RF_DagNode_t *node),
81 1.23 oster int (*wakeFunc) (RF_DagNode_t *node, int status),
82 1.23 oster int nSucc, int nAnte, int nParam, int nResult,
83 1.23 oster RF_DagHeader_t *hdr, char *name, RF_AllocListElem_t *alist)
84 1.3 oster {
85 1.3 oster void **ptrs;
86 1.3 oster int nptrs;
87 1.3 oster
88 1.3 oster if (nAnte > RF_MAX_ANTECEDENTS)
89 1.3 oster RF_PANIC();
90 1.3 oster node->status = initstatus;
91 1.3 oster node->commitNode = commit;
92 1.3 oster node->doFunc = doFunc;
93 1.3 oster node->undoFunc = undoFunc;
94 1.3 oster node->wakeFunc = wakeFunc;
95 1.3 oster node->numParams = nParam;
96 1.3 oster node->numResults = nResult;
97 1.3 oster node->numAntecedents = nAnte;
98 1.3 oster node->numAntDone = 0;
99 1.3 oster node->next = NULL;
100 1.38 oster /* node->list_next = NULL */ /* Don't touch this here!
101 1.38 oster It may already be
102 1.38 oster in use by the caller! */
103 1.3 oster node->numSuccedents = nSucc;
104 1.3 oster node->name = name;
105 1.3 oster node->dagHdr = hdr;
106 1.3 oster node->visited = 0;
107 1.3 oster
108 1.3 oster /* allocate all the pointers with one call to malloc */
109 1.3 oster nptrs = nSucc + nAnte + nResult + nSucc;
110 1.3 oster
111 1.3 oster if (nptrs <= RF_DAG_PTRCACHESIZE) {
112 1.3 oster /*
113 1.3 oster * The dag_ptrs field of the node is basically some scribble
114 1.3 oster * space to be used here. We could get rid of it, and always
115 1.3 oster * allocate the range of pointers, but that's expensive. So,
116 1.3 oster * we pick a "common case" size for the pointer cache. Hopefully,
117 1.3 oster * we'll find that:
118 1.3 oster * (1) Generally, nptrs doesn't exceed RF_DAG_PTRCACHESIZE by
119 1.3 oster * only a little bit (least efficient case)
120 1.3 oster * (2) Generally, ntprs isn't a lot less than RF_DAG_PTRCACHESIZE
121 1.3 oster * (wasted memory)
122 1.3 oster */
123 1.3 oster ptrs = (void **) node->dag_ptrs;
124 1.3 oster } else {
125 1.22 oster RF_MallocAndAdd(ptrs, nptrs * sizeof(void *),
126 1.22 oster (void **), alist);
127 1.3 oster }
128 1.3 oster node->succedents = (nSucc) ? (RF_DagNode_t **) ptrs : NULL;
129 1.3 oster node->antecedents = (nAnte) ? (RF_DagNode_t **) (ptrs + nSucc) : NULL;
130 1.3 oster node->results = (nResult) ? (void **) (ptrs + nSucc + nAnte) : NULL;
131 1.3 oster node->propList = (nSucc) ? (RF_PropHeader_t **) (ptrs + nSucc + nAnte + nResult) : NULL;
132 1.3 oster
133 1.3 oster if (nParam) {
134 1.3 oster if (nParam <= RF_DAG_PARAMCACHESIZE) {
135 1.3 oster node->params = (RF_DagParam_t *) node->dag_params;
136 1.3 oster } else {
137 1.22 oster RF_MallocAndAdd(node->params,
138 1.22 oster nParam * sizeof(RF_DagParam_t),
139 1.22 oster (RF_DagParam_t *), alist);
140 1.3 oster }
141 1.3 oster } else {
142 1.3 oster node->params = NULL;
143 1.3 oster }
144 1.1 oster }
145 1.1 oster
146 1.1 oster
147 1.1 oster
148 1.1 oster /******************************************************************************
149 1.1 oster *
150 1.1 oster * allocation and deallocation routines
151 1.1 oster *
152 1.1 oster *****************************************************************************/
153 1.1 oster
154 1.3 oster void
155 1.23 oster rf_FreeDAG(RF_DagHeader_t *dag_h)
156 1.3 oster {
157 1.3 oster RF_AccessStripeMapHeader_t *asmap, *t_asmap;
158 1.39 oster RF_PhysDiskAddr_t *pda;
159 1.38 oster RF_DagNode_t *tmpnode;
160 1.3 oster RF_DagHeader_t *nextDag;
161 1.3 oster
162 1.3 oster while (dag_h) {
163 1.3 oster nextDag = dag_h->next;
164 1.3 oster rf_FreeAllocList(dag_h->allocList);
165 1.3 oster for (asmap = dag_h->asmList; asmap;) {
166 1.3 oster t_asmap = asmap;
167 1.3 oster asmap = asmap->next;
168 1.3 oster rf_FreeAccessStripeMap(t_asmap);
169 1.3 oster }
170 1.39 oster while (dag_h->pda_cleanup_list) {
171 1.39 oster pda = dag_h->pda_cleanup_list;
172 1.39 oster dag_h->pda_cleanup_list = dag_h->pda_cleanup_list->next;
173 1.39 oster rf_FreePhysDiskAddr(pda);
174 1.39 oster }
175 1.39 oster while (dag_h->nodes) {
176 1.38 oster tmpnode = dag_h->nodes;
177 1.38 oster dag_h->nodes = dag_h->nodes->list_next;
178 1.38 oster rf_FreeDAGNode(tmpnode);
179 1.38 oster }
180 1.3 oster rf_FreeDAGHeader(dag_h);
181 1.3 oster dag_h = nextDag;
182 1.3 oster }
183 1.3 oster }
184 1.3 oster
185 1.1 oster #define RF_MAX_FREE_DAGH 128
186 1.30 oster #define RF_MIN_FREE_DAGH 32
187 1.1 oster
188 1.38 oster #define RF_MAX_FREE_DAGNODE 512 /* XXX Tune this... */
189 1.38 oster #define RF_MIN_FREE_DAGNODE 128 /* XXX Tune this... */
190 1.38 oster
191 1.25 oster #define RF_MAX_FREE_DAGLIST 128
192 1.30 oster #define RF_MIN_FREE_DAGLIST 32
193 1.25 oster
194 1.27 oster #define RF_MAX_FREE_FUNCLIST 128
195 1.30 oster #define RF_MIN_FREE_FUNCLIST 32
196 1.25 oster
197 1.1 oster static void rf_ShutdownDAGs(void *);
198 1.3 oster static void
199 1.23 oster rf_ShutdownDAGs(void *ignored)
200 1.1 oster {
201 1.36 oster pool_destroy(&rf_pools.dagh);
202 1.38 oster pool_destroy(&rf_pools.dagnode);
203 1.36 oster pool_destroy(&rf_pools.daglist);
204 1.36 oster pool_destroy(&rf_pools.funclist);
205 1.1 oster }
206 1.1 oster
207 1.3 oster int
208 1.23 oster rf_ConfigureDAGs(RF_ShutdownList_t **listp)
209 1.1 oster {
210 1.1 oster
211 1.38 oster rf_pool_init(&rf_pools.dagnode, sizeof(RF_DagNode_t),
212 1.38 oster "rf_dagnode_pl", RF_MIN_FREE_DAGNODE, RF_MAX_FREE_DAGNODE);
213 1.36 oster rf_pool_init(&rf_pools.dagh, sizeof(RF_DagHeader_t),
214 1.36 oster "rf_dagh_pl", RF_MIN_FREE_DAGH, RF_MAX_FREE_DAGH);
215 1.36 oster rf_pool_init(&rf_pools.daglist, sizeof(RF_DagList_t),
216 1.36 oster "rf_daglist_pl", RF_MIN_FREE_DAGLIST, RF_MAX_FREE_DAGLIST);
217 1.36 oster rf_pool_init(&rf_pools.funclist, sizeof(RF_FuncList_t),
218 1.36 oster "rf_funclist_pl", RF_MIN_FREE_FUNCLIST, RF_MAX_FREE_FUNCLIST);
219 1.29 oster rf_ShutdownCreate(listp, rf_ShutdownDAGs, NULL);
220 1.29 oster
221 1.3 oster return (0);
222 1.1 oster }
223 1.1 oster
224 1.3 oster RF_DagHeader_t *
225 1.3 oster rf_AllocDAGHeader()
226 1.1 oster {
227 1.1 oster RF_DagHeader_t *dh;
228 1.1 oster
229 1.36 oster dh = pool_get(&rf_pools.dagh, PR_WAITOK);
230 1.28 oster memset((char *) dh, 0, sizeof(RF_DagHeader_t));
231 1.3 oster return (dh);
232 1.1 oster }
233 1.1 oster
234 1.3 oster void
235 1.3 oster rf_FreeDAGHeader(RF_DagHeader_t * dh)
236 1.1 oster {
237 1.36 oster pool_put(&rf_pools.dagh, dh);
238 1.1 oster }
239 1.25 oster
240 1.38 oster RF_DagNode_t *
241 1.38 oster rf_AllocDAGNode()
242 1.38 oster {
243 1.38 oster RF_DagNode_t *node;
244 1.38 oster
245 1.38 oster node = pool_get(&rf_pools.dagnode, PR_WAITOK);
246 1.38 oster memset(node, 0, sizeof(RF_DagNode_t));
247 1.38 oster return (node);
248 1.38 oster }
249 1.38 oster
250 1.38 oster void
251 1.38 oster rf_FreeDAGNode(RF_DagNode_t *node)
252 1.38 oster {
253 1.38 oster pool_put(&rf_pools.dagnode, node);
254 1.38 oster }
255 1.38 oster
256 1.25 oster RF_DagList_t *
257 1.25 oster rf_AllocDAGList()
258 1.25 oster {
259 1.25 oster RF_DagList_t *dagList;
260 1.25 oster
261 1.36 oster dagList = pool_get(&rf_pools.daglist, PR_WAITOK);
262 1.25 oster memset(dagList, 0, sizeof(RF_DagList_t));
263 1.25 oster
264 1.25 oster return (dagList);
265 1.25 oster }
266 1.25 oster
267 1.25 oster void
268 1.25 oster rf_FreeDAGList(RF_DagList_t *dagList)
269 1.25 oster {
270 1.36 oster pool_put(&rf_pools.daglist, dagList);
271 1.25 oster }
272 1.25 oster
273 1.27 oster RF_FuncList_t *
274 1.27 oster rf_AllocFuncList()
275 1.27 oster {
276 1.27 oster RF_FuncList_t *funcList;
277 1.27 oster
278 1.36 oster funcList = pool_get(&rf_pools.funclist, PR_WAITOK);
279 1.27 oster memset(funcList, 0, sizeof(RF_FuncList_t));
280 1.27 oster
281 1.27 oster return (funcList);
282 1.27 oster }
283 1.27 oster
284 1.27 oster void
285 1.27 oster rf_FreeFuncList(RF_FuncList_t *funcList)
286 1.27 oster {
287 1.36 oster pool_put(&rf_pools.funclist, funcList);
288 1.27 oster }
289 1.25 oster
290 1.25 oster
291 1.25 oster
292 1.1 oster /* allocates a buffer big enough to hold the data described by pda */
293 1.3 oster void *
294 1.33 oster rf_AllocBuffer(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *pda,
295 1.33 oster RF_AllocListElem_t *allocList)
296 1.3 oster {
297 1.3 oster char *p;
298 1.3 oster
299 1.3 oster RF_MallocAndAdd(p, pda->numSector << raidPtr->logBytesPerSector,
300 1.3 oster (char *), allocList);
301 1.3 oster return ((void *) p);
302 1.1 oster }
303 1.13 oster #if RF_DEBUG_VALIDATE_DAG
304 1.1 oster /******************************************************************************
305 1.1 oster *
306 1.1 oster * debug routines
307 1.1 oster *
308 1.1 oster *****************************************************************************/
309 1.1 oster
310 1.3 oster char *
311 1.23 oster rf_NodeStatusString(RF_DagNode_t *node)
312 1.1 oster {
313 1.3 oster switch (node->status) {
314 1.34 oster case rf_wait:
315 1.34 oster return ("wait");
316 1.3 oster case rf_fired:
317 1.3 oster return ("fired");
318 1.3 oster case rf_good:
319 1.3 oster return ("good");
320 1.3 oster case rf_bad:
321 1.3 oster return ("bad");
322 1.3 oster default:
323 1.3 oster return ("?");
324 1.3 oster }
325 1.3 oster }
326 1.1 oster
327 1.3 oster void
328 1.23 oster rf_PrintNodeInfoString(RF_DagNode_t *node)
329 1.3 oster {
330 1.3 oster RF_PhysDiskAddr_t *pda;
331 1.3 oster int (*df) (RF_DagNode_t *) = node->doFunc;
332 1.3 oster int i, lk, unlk;
333 1.3 oster void *bufPtr;
334 1.3 oster
335 1.3 oster if ((df == rf_DiskReadFunc) || (df == rf_DiskWriteFunc)
336 1.3 oster || (df == rf_DiskReadMirrorIdleFunc)
337 1.3 oster || (df == rf_DiskReadMirrorPartitionFunc)) {
338 1.3 oster pda = (RF_PhysDiskAddr_t *) node->params[0].p;
339 1.3 oster bufPtr = (void *) node->params[1].p;
340 1.24 oster lk = 0;
341 1.24 oster unlk = 0;
342 1.3 oster RF_ASSERT(!(lk && unlk));
343 1.21 oster printf("c %d offs %ld nsect %d buf 0x%lx %s\n", pda->col,
344 1.3 oster (long) pda->startSector, (int) pda->numSector, (long) bufPtr,
345 1.3 oster (lk) ? "LOCK" : ((unlk) ? "UNLK" : " "));
346 1.3 oster return;
347 1.3 oster }
348 1.3 oster if (df == rf_DiskUnlockFunc) {
349 1.3 oster pda = (RF_PhysDiskAddr_t *) node->params[0].p;
350 1.24 oster lk = 0;
351 1.24 oster unlk = 0;
352 1.3 oster RF_ASSERT(!(lk && unlk));
353 1.21 oster printf("c %d %s\n", pda->col,
354 1.3 oster (lk) ? "LOCK" : ((unlk) ? "UNLK" : "nop"));
355 1.3 oster return;
356 1.3 oster }
357 1.3 oster if ((df == rf_SimpleXorFunc) || (df == rf_RegularXorFunc)
358 1.3 oster || (df == rf_RecoveryXorFunc)) {
359 1.3 oster printf("result buf 0x%lx\n", (long) node->results[0]);
360 1.3 oster for (i = 0; i < node->numParams - 1; i += 2) {
361 1.3 oster pda = (RF_PhysDiskAddr_t *) node->params[i].p;
362 1.3 oster bufPtr = (RF_PhysDiskAddr_t *) node->params[i + 1].p;
363 1.21 oster printf(" buf 0x%lx c%d offs %ld nsect %d\n",
364 1.21 oster (long) bufPtr, pda->col,
365 1.3 oster (long) pda->startSector, (int) pda->numSector);
366 1.3 oster }
367 1.3 oster return;
368 1.3 oster }
369 1.1 oster #if RF_INCLUDE_PARITYLOGGING > 0
370 1.3 oster if (df == rf_ParityLogOverwriteFunc || df == rf_ParityLogUpdateFunc) {
371 1.3 oster for (i = 0; i < node->numParams - 1; i += 2) {
372 1.3 oster pda = (RF_PhysDiskAddr_t *) node->params[i].p;
373 1.3 oster bufPtr = (RF_PhysDiskAddr_t *) node->params[i + 1].p;
374 1.21 oster printf(" c%d offs %ld nsect %d buf 0x%lx\n",
375 1.21 oster pda->col, (long) pda->startSector,
376 1.3 oster (int) pda->numSector, (long) bufPtr);
377 1.3 oster }
378 1.3 oster return;
379 1.3 oster }
380 1.3 oster #endif /* RF_INCLUDE_PARITYLOGGING > 0 */
381 1.3 oster
382 1.3 oster if ((df == rf_TerminateFunc) || (df == rf_NullNodeFunc)) {
383 1.3 oster printf("\n");
384 1.3 oster return;
385 1.3 oster }
386 1.3 oster printf("?\n");
387 1.3 oster }
388 1.16 oster #ifdef DEBUG
389 1.3 oster static void
390 1.23 oster rf_RecurPrintDAG(RF_DagNode_t *node, int depth, int unvisited)
391 1.3 oster {
392 1.3 oster char *anttype;
393 1.3 oster int i;
394 1.3 oster
395 1.3 oster node->visited = (unvisited) ? 0 : 1;
396 1.3 oster printf("(%d) %d C%d %s: %s,s%d %d/%d,a%d/%d,p%d,r%d S{", depth,
397 1.3 oster node->nodeNum, node->commitNode, node->name, rf_NodeStatusString(node),
398 1.3 oster node->numSuccedents, node->numSuccFired, node->numSuccDone,
399 1.3 oster node->numAntecedents, node->numAntDone, node->numParams, node->numResults);
400 1.3 oster for (i = 0; i < node->numSuccedents; i++) {
401 1.3 oster printf("%d%s", node->succedents[i]->nodeNum,
402 1.3 oster ((i == node->numSuccedents - 1) ? "\0" : " "));
403 1.3 oster }
404 1.3 oster printf("} A{");
405 1.3 oster for (i = 0; i < node->numAntecedents; i++) {
406 1.3 oster switch (node->antType[i]) {
407 1.3 oster case rf_trueData:
408 1.3 oster anttype = "T";
409 1.3 oster break;
410 1.3 oster case rf_antiData:
411 1.3 oster anttype = "A";
412 1.3 oster break;
413 1.3 oster case rf_outputData:
414 1.3 oster anttype = "O";
415 1.3 oster break;
416 1.3 oster case rf_control:
417 1.3 oster anttype = "C";
418 1.3 oster break;
419 1.3 oster default:
420 1.3 oster anttype = "?";
421 1.3 oster break;
422 1.3 oster }
423 1.3 oster printf("%d(%s)%s", node->antecedents[i]->nodeNum, anttype, (i == node->numAntecedents - 1) ? "\0" : " ");
424 1.3 oster }
425 1.3 oster printf("}; ");
426 1.3 oster rf_PrintNodeInfoString(node);
427 1.3 oster for (i = 0; i < node->numSuccedents; i++) {
428 1.3 oster if (node->succedents[i]->visited == unvisited)
429 1.3 oster rf_RecurPrintDAG(node->succedents[i], depth + 1, unvisited);
430 1.3 oster }
431 1.1 oster }
432 1.1 oster
433 1.3 oster static void
434 1.23 oster rf_PrintDAG(RF_DagHeader_t *dag_h)
435 1.3 oster {
436 1.3 oster int unvisited, i;
437 1.3 oster char *status;
438 1.3 oster
439 1.3 oster /* set dag status */
440 1.3 oster switch (dag_h->status) {
441 1.3 oster case rf_enable:
442 1.3 oster status = "enable";
443 1.3 oster break;
444 1.3 oster case rf_rollForward:
445 1.3 oster status = "rollForward";
446 1.3 oster break;
447 1.3 oster case rf_rollBackward:
448 1.3 oster status = "rollBackward";
449 1.3 oster break;
450 1.3 oster default:
451 1.3 oster status = "illegal!";
452 1.3 oster break;
453 1.3 oster }
454 1.3 oster /* find out if visited bits are currently set or clear */
455 1.3 oster unvisited = dag_h->succedents[0]->visited;
456 1.3 oster
457 1.3 oster printf("DAG type: %s\n", dag_h->creator);
458 1.3 oster printf("format is (depth) num commit type: status,nSucc nSuccFired/nSuccDone,nAnte/nAnteDone,nParam,nResult S{x} A{x(type)}; info\n");
459 1.3 oster printf("(0) %d Hdr: %s, s%d, (commit %d/%d) S{", dag_h->nodeNum,
460 1.3 oster status, dag_h->numSuccedents, dag_h->numCommitNodes, dag_h->numCommits);
461 1.3 oster for (i = 0; i < dag_h->numSuccedents; i++) {
462 1.3 oster printf("%d%s", dag_h->succedents[i]->nodeNum,
463 1.3 oster ((i == dag_h->numSuccedents - 1) ? "\0" : " "));
464 1.3 oster }
465 1.3 oster printf("};\n");
466 1.3 oster for (i = 0; i < dag_h->numSuccedents; i++) {
467 1.3 oster if (dag_h->succedents[i]->visited == unvisited)
468 1.3 oster rf_RecurPrintDAG(dag_h->succedents[i], 1, unvisited);
469 1.3 oster }
470 1.3 oster }
471 1.16 oster #endif
472 1.1 oster /* assigns node numbers */
473 1.3 oster int
474 1.3 oster rf_AssignNodeNums(RF_DagHeader_t * dag_h)
475 1.1 oster {
476 1.3 oster int unvisited, i, nnum;
477 1.3 oster RF_DagNode_t *node;
478 1.1 oster
479 1.3 oster nnum = 0;
480 1.3 oster unvisited = dag_h->succedents[0]->visited;
481 1.3 oster
482 1.3 oster dag_h->nodeNum = nnum++;
483 1.3 oster for (i = 0; i < dag_h->numSuccedents; i++) {
484 1.3 oster node = dag_h->succedents[i];
485 1.3 oster if (node->visited == unvisited) {
486 1.3 oster nnum = rf_RecurAssignNodeNums(dag_h->succedents[i], nnum, unvisited);
487 1.3 oster }
488 1.3 oster }
489 1.3 oster return (nnum);
490 1.1 oster }
491 1.1 oster
492 1.3 oster int
493 1.23 oster rf_RecurAssignNodeNums(RF_DagNode_t *node, int num, int unvisited)
494 1.3 oster {
495 1.3 oster int i;
496 1.3 oster
497 1.3 oster node->visited = (unvisited) ? 0 : 1;
498 1.3 oster
499 1.3 oster node->nodeNum = num++;
500 1.3 oster for (i = 0; i < node->numSuccedents; i++) {
501 1.3 oster if (node->succedents[i]->visited == unvisited) {
502 1.3 oster num = rf_RecurAssignNodeNums(node->succedents[i], num, unvisited);
503 1.3 oster }
504 1.3 oster }
505 1.3 oster return (num);
506 1.3 oster }
507 1.1 oster /* set the header pointers in each node to "newptr" */
508 1.3 oster void
509 1.23 oster rf_ResetDAGHeaderPointers(RF_DagHeader_t *dag_h, RF_DagHeader_t *newptr)
510 1.3 oster {
511 1.3 oster int i;
512 1.3 oster for (i = 0; i < dag_h->numSuccedents; i++)
513 1.3 oster if (dag_h->succedents[i]->dagHdr != newptr)
514 1.3 oster rf_RecurResetDAGHeaderPointers(dag_h->succedents[i], newptr);
515 1.1 oster }
516 1.1 oster
517 1.3 oster void
518 1.23 oster rf_RecurResetDAGHeaderPointers(RF_DagNode_t *node, RF_DagHeader_t *newptr)
519 1.1 oster {
520 1.3 oster int i;
521 1.3 oster node->dagHdr = newptr;
522 1.3 oster for (i = 0; i < node->numSuccedents; i++)
523 1.3 oster if (node->succedents[i]->dagHdr != newptr)
524 1.3 oster rf_RecurResetDAGHeaderPointers(node->succedents[i], newptr);
525 1.3 oster }
526 1.1 oster
527 1.1 oster
528 1.3 oster void
529 1.3 oster rf_PrintDAGList(RF_DagHeader_t * dag_h)
530 1.3 oster {
531 1.3 oster int i = 0;
532 1.3 oster
533 1.3 oster for (; dag_h; dag_h = dag_h->next) {
534 1.3 oster rf_AssignNodeNums(dag_h);
535 1.3 oster printf("\n\nDAG %d IN LIST:\n", i++);
536 1.3 oster rf_PrintDAG(dag_h);
537 1.3 oster }
538 1.1 oster }
539 1.1 oster
540 1.3 oster static int
541 1.23 oster rf_ValidateBranch(RF_DagNode_t *node, int *scount, int *acount,
542 1.23 oster RF_DagNode_t **nodes, int unvisited)
543 1.3 oster {
544 1.3 oster int i, retcode = 0;
545 1.3 oster
546 1.3 oster /* construct an array of node pointers indexed by node num */
547 1.3 oster node->visited = (unvisited) ? 0 : 1;
548 1.3 oster nodes[node->nodeNum] = node;
549 1.3 oster
550 1.3 oster if (node->next != NULL) {
551 1.3 oster printf("INVALID DAG: next pointer in node is not NULL\n");
552 1.3 oster retcode = 1;
553 1.3 oster }
554 1.3 oster if (node->status != rf_wait) {
555 1.3 oster printf("INVALID DAG: Node status is not wait\n");
556 1.3 oster retcode = 1;
557 1.3 oster }
558 1.3 oster if (node->numAntDone != 0) {
559 1.3 oster printf("INVALID DAG: numAntDone is not zero\n");
560 1.3 oster retcode = 1;
561 1.3 oster }
562 1.3 oster if (node->doFunc == rf_TerminateFunc) {
563 1.3 oster if (node->numSuccedents != 0) {
564 1.3 oster printf("INVALID DAG: Terminator node has succedents\n");
565 1.3 oster retcode = 1;
566 1.3 oster }
567 1.3 oster } else {
568 1.3 oster if (node->numSuccedents == 0) {
569 1.3 oster printf("INVALID DAG: Non-terminator node has no succedents\n");
570 1.3 oster retcode = 1;
571 1.3 oster }
572 1.3 oster }
573 1.3 oster for (i = 0; i < node->numSuccedents; i++) {
574 1.3 oster if (!node->succedents[i]) {
575 1.3 oster printf("INVALID DAG: succedent %d of node %s is NULL\n", i, node->name);
576 1.3 oster retcode = 1;
577 1.3 oster }
578 1.3 oster scount[node->succedents[i]->nodeNum]++;
579 1.3 oster }
580 1.3 oster for (i = 0; i < node->numAntecedents; i++) {
581 1.3 oster if (!node->antecedents[i]) {
582 1.3 oster printf("INVALID DAG: antecedent %d of node %s is NULL\n", i, node->name);
583 1.3 oster retcode = 1;
584 1.3 oster }
585 1.3 oster acount[node->antecedents[i]->nodeNum]++;
586 1.3 oster }
587 1.3 oster for (i = 0; i < node->numSuccedents; i++) {
588 1.3 oster if (node->succedents[i]->visited == unvisited) {
589 1.3 oster if (rf_ValidateBranch(node->succedents[i], scount,
590 1.3 oster acount, nodes, unvisited)) {
591 1.3 oster retcode = 1;
592 1.3 oster }
593 1.3 oster }
594 1.3 oster }
595 1.3 oster return (retcode);
596 1.3 oster }
597 1.3 oster
598 1.3 oster static void
599 1.23 oster rf_ValidateBranchVisitedBits(RF_DagNode_t *node, int unvisited, int rl)
600 1.3 oster {
601 1.3 oster int i;
602 1.3 oster
603 1.3 oster RF_ASSERT(node->visited == unvisited);
604 1.3 oster for (i = 0; i < node->numSuccedents; i++) {
605 1.3 oster if (node->succedents[i] == NULL) {
606 1.3 oster printf("node=%lx node->succedents[%d] is NULL\n", (long) node, i);
607 1.3 oster RF_ASSERT(0);
608 1.3 oster }
609 1.3 oster rf_ValidateBranchVisitedBits(node->succedents[i], unvisited, rl + 1);
610 1.3 oster }
611 1.3 oster }
612 1.3 oster /* NOTE: never call this on a big dag, because it is exponential
613 1.3 oster * in execution time
614 1.3 oster */
615 1.3 oster static void
616 1.23 oster rf_ValidateVisitedBits(RF_DagHeader_t *dag)
617 1.3 oster {
618 1.3 oster int i, unvisited;
619 1.3 oster
620 1.3 oster unvisited = dag->succedents[0]->visited;
621 1.3 oster
622 1.3 oster for (i = 0; i < dag->numSuccedents; i++) {
623 1.3 oster if (dag->succedents[i] == NULL) {
624 1.3 oster printf("dag=%lx dag->succedents[%d] is NULL\n", (long) dag, i);
625 1.3 oster RF_ASSERT(0);
626 1.3 oster }
627 1.3 oster rf_ValidateBranchVisitedBits(dag->succedents[i], unvisited, 0);
628 1.3 oster }
629 1.3 oster }
630 1.1 oster /* validate a DAG. _at entry_ verify that:
631 1.1 oster * -- numNodesCompleted is zero
632 1.1 oster * -- node queue is null
633 1.1 oster * -- dag status is rf_enable
634 1.1 oster * -- next pointer is null on every node
635 1.1 oster * -- all nodes have status wait
636 1.1 oster * -- numAntDone is zero in all nodes
637 1.1 oster * -- terminator node has zero successors
638 1.1 oster * -- no other node besides terminator has zero successors
639 1.1 oster * -- no successor or antecedent pointer in a node is NULL
640 1.1 oster * -- number of times that each node appears as a successor of another node
641 1.1 oster * is equal to the antecedent count on that node
642 1.1 oster * -- number of times that each node appears as an antecedent of another node
643 1.1 oster * is equal to the succedent count on that node
644 1.1 oster * -- what else?
645 1.1 oster */
646 1.3 oster int
647 1.23 oster rf_ValidateDAG(RF_DagHeader_t *dag_h)
648 1.3 oster {
649 1.3 oster int i, nodecount;
650 1.3 oster int *scount, *acount;/* per-node successor and antecedent counts */
651 1.3 oster RF_DagNode_t **nodes; /* array of ptrs to nodes in dag */
652 1.3 oster int retcode = 0;
653 1.3 oster int unvisited;
654 1.3 oster int commitNodeCount = 0;
655 1.3 oster
656 1.3 oster if (rf_validateVisitedDebug)
657 1.3 oster rf_ValidateVisitedBits(dag_h);
658 1.3 oster
659 1.3 oster if (dag_h->numNodesCompleted != 0) {
660 1.3 oster printf("INVALID DAG: num nodes completed is %d, should be 0\n", dag_h->numNodesCompleted);
661 1.3 oster retcode = 1;
662 1.3 oster goto validate_dag_bad;
663 1.3 oster }
664 1.3 oster if (dag_h->status != rf_enable) {
665 1.3 oster printf("INVALID DAG: not enabled\n");
666 1.3 oster retcode = 1;
667 1.3 oster goto validate_dag_bad;
668 1.3 oster }
669 1.3 oster if (dag_h->numCommits != 0) {
670 1.3 oster printf("INVALID DAG: numCommits != 0 (%d)\n", dag_h->numCommits);
671 1.3 oster retcode = 1;
672 1.3 oster goto validate_dag_bad;
673 1.3 oster }
674 1.3 oster if (dag_h->numSuccedents != 1) {
675 1.3 oster /* currently, all dags must have only one succedent */
676 1.3 oster printf("INVALID DAG: numSuccedents !1 (%d)\n", dag_h->numSuccedents);
677 1.3 oster retcode = 1;
678 1.3 oster goto validate_dag_bad;
679 1.3 oster }
680 1.3 oster nodecount = rf_AssignNodeNums(dag_h);
681 1.3 oster
682 1.3 oster unvisited = dag_h->succedents[0]->visited;
683 1.3 oster
684 1.22 oster RF_Malloc(scount, nodecount * sizeof(int), (int *));
685 1.22 oster RF_Malloc(acount, nodecount * sizeof(int), (int *));
686 1.22 oster RF_Malloc(nodes, nodecount * sizeof(RF_DagNode_t *),
687 1.22 oster (RF_DagNode_t **));
688 1.3 oster for (i = 0; i < dag_h->numSuccedents; i++) {
689 1.3 oster if ((dag_h->succedents[i]->visited == unvisited)
690 1.3 oster && rf_ValidateBranch(dag_h->succedents[i], scount,
691 1.3 oster acount, nodes, unvisited)) {
692 1.3 oster retcode = 1;
693 1.3 oster }
694 1.3 oster }
695 1.3 oster /* start at 1 to skip the header node */
696 1.3 oster for (i = 1; i < nodecount; i++) {
697 1.3 oster if (nodes[i]->commitNode)
698 1.3 oster commitNodeCount++;
699 1.3 oster if (nodes[i]->doFunc == NULL) {
700 1.3 oster printf("INVALID DAG: node %s has an undefined doFunc\n", nodes[i]->name);
701 1.3 oster retcode = 1;
702 1.3 oster goto validate_dag_out;
703 1.3 oster }
704 1.3 oster if (nodes[i]->undoFunc == NULL) {
705 1.3 oster printf("INVALID DAG: node %s has an undefined doFunc\n", nodes[i]->name);
706 1.3 oster retcode = 1;
707 1.3 oster goto validate_dag_out;
708 1.3 oster }
709 1.3 oster if (nodes[i]->numAntecedents != scount[nodes[i]->nodeNum]) {
710 1.3 oster printf("INVALID DAG: node %s has %d antecedents but appears as a succedent %d times\n",
711 1.3 oster nodes[i]->name, nodes[i]->numAntecedents, scount[nodes[i]->nodeNum]);
712 1.3 oster retcode = 1;
713 1.3 oster goto validate_dag_out;
714 1.3 oster }
715 1.3 oster if (nodes[i]->numSuccedents != acount[nodes[i]->nodeNum]) {
716 1.3 oster printf("INVALID DAG: node %s has %d succedents but appears as an antecedent %d times\n",
717 1.3 oster nodes[i]->name, nodes[i]->numSuccedents, acount[nodes[i]->nodeNum]);
718 1.3 oster retcode = 1;
719 1.3 oster goto validate_dag_out;
720 1.3 oster }
721 1.3 oster }
722 1.1 oster
723 1.3 oster if (dag_h->numCommitNodes != commitNodeCount) {
724 1.3 oster printf("INVALID DAG: incorrect commit node count. hdr->numCommitNodes (%d) found (%d) commit nodes in graph\n",
725 1.3 oster dag_h->numCommitNodes, commitNodeCount);
726 1.3 oster retcode = 1;
727 1.3 oster goto validate_dag_out;
728 1.3 oster }
729 1.1 oster validate_dag_out:
730 1.3 oster RF_Free(scount, nodecount * sizeof(int));
731 1.3 oster RF_Free(acount, nodecount * sizeof(int));
732 1.3 oster RF_Free(nodes, nodecount * sizeof(RF_DagNode_t *));
733 1.3 oster if (retcode)
734 1.3 oster rf_PrintDAGList(dag_h);
735 1.3 oster
736 1.3 oster if (rf_validateVisitedDebug)
737 1.3 oster rf_ValidateVisitedBits(dag_h);
738 1.3 oster
739 1.3 oster return (retcode);
740 1.1 oster
741 1.1 oster validate_dag_bad:
742 1.3 oster rf_PrintDAGList(dag_h);
743 1.3 oster return (retcode);
744 1.1 oster }
745 1.1 oster
746 1.13 oster #endif /* RF_DEBUG_VALIDATE_DAG */
747 1.1 oster
748 1.1 oster /******************************************************************************
749 1.1 oster *
750 1.1 oster * misc construction routines
751 1.1 oster *
752 1.1 oster *****************************************************************************/
753 1.1 oster
754 1.3 oster void
755 1.23 oster rf_redirect_asm(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap)
756 1.3 oster {
757 1.3 oster int ds = (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE) ? 1 : 0;
758 1.21 oster int fcol = raidPtr->reconControl->fcol;
759 1.21 oster int scol = raidPtr->reconControl->spareCol;
760 1.3 oster RF_PhysDiskAddr_t *pda;
761 1.3 oster
762 1.21 oster RF_ASSERT(raidPtr->status == rf_rs_reconstructing);
763 1.3 oster for (pda = asmap->physInfo; pda; pda = pda->next) {
764 1.3 oster if (pda->col == fcol) {
765 1.31 oster #if RF_DEBUG_DAG
766 1.3 oster if (rf_dagDebug) {
767 1.21 oster if (!rf_CheckRUReconstructed(raidPtr->reconControl->reconMap,
768 1.3 oster pda->startSector)) {
769 1.3 oster RF_PANIC();
770 1.3 oster }
771 1.3 oster }
772 1.31 oster #endif
773 1.3 oster /* printf("Remapped data for large write\n"); */
774 1.3 oster if (ds) {
775 1.3 oster raidPtr->Layout.map->MapSector(raidPtr, pda->raidAddress,
776 1.21 oster &pda->col, &pda->startSector, RF_REMAP);
777 1.3 oster } else {
778 1.3 oster pda->col = scol;
779 1.3 oster }
780 1.3 oster }
781 1.3 oster }
782 1.3 oster for (pda = asmap->parityInfo; pda; pda = pda->next) {
783 1.3 oster if (pda->col == fcol) {
784 1.31 oster #if RF_DEBUG_DAG
785 1.3 oster if (rf_dagDebug) {
786 1.21 oster if (!rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, pda->startSector)) {
787 1.3 oster RF_PANIC();
788 1.3 oster }
789 1.3 oster }
790 1.31 oster #endif
791 1.3 oster }
792 1.3 oster if (ds) {
793 1.21 oster (raidPtr->Layout.map->MapParity) (raidPtr, pda->raidAddress, &pda->col, &pda->startSector, RF_REMAP);
794 1.3 oster } else {
795 1.3 oster pda->col = scol;
796 1.3 oster }
797 1.3 oster }
798 1.1 oster }
799 1.1 oster
800 1.1 oster
801 1.1 oster /* this routine allocates read buffers and generates stripe maps for the
802 1.1 oster * regions of the array from the start of the stripe to the start of the
803 1.1 oster * access, and from the end of the access to the end of the stripe. It also
804 1.1 oster * computes and returns the number of DAG nodes needed to read all this data.
805 1.1 oster * Note that this routine does the wrong thing if the access is fully
806 1.1 oster * contained within one stripe unit, so we RF_ASSERT against this case at the
807 1.1 oster * start.
808 1.23 oster *
809 1.23 oster * layoutPtr - in: layout information
810 1.23 oster * asmap - in: access stripe map
811 1.23 oster * dag_h - in: header of the dag to create
812 1.23 oster * new_asm_h - in: ptr to array of 2 headers. to be filled in
813 1.23 oster * nRodNodes - out: num nodes to be generated to read unaccessed data
814 1.23 oster * sosBuffer, eosBuffer - out: pointers to newly allocated buffer
815 1.1 oster */
816 1.3 oster void
817 1.23 oster rf_MapUnaccessedPortionOfStripe(RF_Raid_t *raidPtr,
818 1.23 oster RF_RaidLayout_t *layoutPtr,
819 1.23 oster RF_AccessStripeMap_t *asmap,
820 1.23 oster RF_DagHeader_t *dag_h,
821 1.23 oster RF_AccessStripeMapHeader_t **new_asm_h,
822 1.23 oster int *nRodNodes,
823 1.23 oster char **sosBuffer, char **eosBuffer,
824 1.23 oster RF_AllocListElem_t *allocList)
825 1.3 oster {
826 1.3 oster RF_RaidAddr_t sosRaidAddress, eosRaidAddress;
827 1.3 oster RF_SectorNum_t sosNumSector, eosNumSector;
828 1.3 oster
829 1.3 oster RF_ASSERT(asmap->numStripeUnitsAccessed > (layoutPtr->numDataCol / 2));
830 1.3 oster /* generate an access map for the region of the array from start of
831 1.3 oster * stripe to start of access */
832 1.3 oster new_asm_h[0] = new_asm_h[1] = NULL;
833 1.3 oster *nRodNodes = 0;
834 1.3 oster if (!rf_RaidAddressStripeAligned(layoutPtr, asmap->raidAddress)) {
835 1.3 oster sosRaidAddress = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, asmap->raidAddress);
836 1.3 oster sosNumSector = asmap->raidAddress - sosRaidAddress;
837 1.3 oster RF_MallocAndAdd(*sosBuffer, rf_RaidAddressToByte(raidPtr, sosNumSector), (char *), allocList);
838 1.3 oster new_asm_h[0] = rf_MapAccess(raidPtr, sosRaidAddress, sosNumSector, *sosBuffer, RF_DONT_REMAP);
839 1.3 oster new_asm_h[0]->next = dag_h->asmList;
840 1.3 oster dag_h->asmList = new_asm_h[0];
841 1.3 oster *nRodNodes += new_asm_h[0]->stripeMap->numStripeUnitsAccessed;
842 1.3 oster
843 1.3 oster RF_ASSERT(new_asm_h[0]->stripeMap->next == NULL);
844 1.3 oster /* we're totally within one stripe here */
845 1.3 oster if (asmap->flags & RF_ASM_REDIR_LARGE_WRITE)
846 1.3 oster rf_redirect_asm(raidPtr, new_asm_h[0]->stripeMap);
847 1.3 oster }
848 1.3 oster /* generate an access map for the region of the array from end of
849 1.3 oster * access to end of stripe */
850 1.3 oster if (!rf_RaidAddressStripeAligned(layoutPtr, asmap->endRaidAddress)) {
851 1.3 oster eosRaidAddress = asmap->endRaidAddress;
852 1.3 oster eosNumSector = rf_RaidAddressOfNextStripeBoundary(layoutPtr, eosRaidAddress) - eosRaidAddress;
853 1.3 oster RF_MallocAndAdd(*eosBuffer, rf_RaidAddressToByte(raidPtr, eosNumSector), (char *), allocList);
854 1.3 oster new_asm_h[1] = rf_MapAccess(raidPtr, eosRaidAddress, eosNumSector, *eosBuffer, RF_DONT_REMAP);
855 1.3 oster new_asm_h[1]->next = dag_h->asmList;
856 1.3 oster dag_h->asmList = new_asm_h[1];
857 1.3 oster *nRodNodes += new_asm_h[1]->stripeMap->numStripeUnitsAccessed;
858 1.3 oster
859 1.3 oster RF_ASSERT(new_asm_h[1]->stripeMap->next == NULL);
860 1.3 oster /* we're totally within one stripe here */
861 1.3 oster if (asmap->flags & RF_ASM_REDIR_LARGE_WRITE)
862 1.3 oster rf_redirect_asm(raidPtr, new_asm_h[1]->stripeMap);
863 1.3 oster }
864 1.1 oster }
865 1.1 oster
866 1.1 oster
867 1.1 oster
868 1.1 oster /* returns non-zero if the indicated ranges of stripe unit offsets overlap */
869 1.3 oster int
870 1.23 oster rf_PDAOverlap(RF_RaidLayout_t *layoutPtr,
871 1.23 oster RF_PhysDiskAddr_t *src, RF_PhysDiskAddr_t *dest)
872 1.3 oster {
873 1.3 oster RF_SectorNum_t soffs = rf_StripeUnitOffset(layoutPtr, src->startSector);
874 1.3 oster RF_SectorNum_t doffs = rf_StripeUnitOffset(layoutPtr, dest->startSector);
875 1.3 oster /* use -1 to be sure we stay within SU */
876 1.3 oster RF_SectorNum_t send = rf_StripeUnitOffset(layoutPtr, src->startSector + src->numSector - 1);
877 1.3 oster RF_SectorNum_t dend = rf_StripeUnitOffset(layoutPtr, dest->startSector + dest->numSector - 1);
878 1.3 oster return ((RF_MAX(soffs, doffs) <= RF_MIN(send, dend)) ? 1 : 0);
879 1.1 oster }
880 1.1 oster
881 1.1 oster
882 1.1 oster /* GenerateFailedAccessASMs
883 1.1 oster *
884 1.1 oster * this routine figures out what portion of the stripe needs to be read
885 1.1 oster * to effect the degraded read or write operation. It's primary function
886 1.1 oster * is to identify everything required to recover the data, and then
887 1.1 oster * eliminate anything that is already being accessed by the user.
888 1.1 oster *
889 1.1 oster * The main result is two new ASMs, one for the region from the start of the
890 1.1 oster * stripe to the start of the access, and one for the region from the end of
891 1.1 oster * the access to the end of the stripe. These ASMs describe everything that
892 1.1 oster * needs to be read to effect the degraded access. Other results are:
893 1.1 oster * nXorBufs -- the total number of buffers that need to be XORed together to
894 1.1 oster * recover the lost data,
895 1.1 oster * rpBufPtr -- ptr to a newly-allocated buffer to hold the parity. If NULL
896 1.1 oster * at entry, not allocated.
897 1.1 oster * overlappingPDAs --
898 1.1 oster * describes which of the non-failed PDAs in the user access
899 1.1 oster * overlap data that needs to be read to effect recovery.
900 1.1 oster * overlappingPDAs[i]==1 if and only if, neglecting the failed
901 1.1 oster * PDA, the ith pda in the input asm overlaps data that needs
902 1.1 oster * to be read for recovery.
903 1.1 oster */
904 1.1 oster /* in: asm - ASM for the actual access, one stripe only */
905 1.10 wiz /* in: failedPDA - which component of the access has failed */
906 1.1 oster /* in: dag_h - header of the DAG we're going to create */
907 1.1 oster /* out: new_asm_h - the two new ASMs */
908 1.1 oster /* out: nXorBufs - the total number of xor bufs required */
909 1.1 oster /* out: rpBufPtr - a buffer for the parity read */
910 1.3 oster void
911 1.23 oster rf_GenerateFailedAccessASMs(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap,
912 1.23 oster RF_PhysDiskAddr_t *failedPDA,
913 1.23 oster RF_DagHeader_t *dag_h,
914 1.23 oster RF_AccessStripeMapHeader_t **new_asm_h,
915 1.23 oster int *nXorBufs, char **rpBufPtr,
916 1.23 oster char *overlappingPDAs,
917 1.23 oster RF_AllocListElem_t *allocList)
918 1.3 oster {
919 1.3 oster RF_RaidLayout_t *layoutPtr = &(raidPtr->Layout);
920 1.3 oster
921 1.3 oster /* s=start, e=end, s=stripe, a=access, f=failed, su=stripe unit */
922 1.3 oster RF_RaidAddr_t sosAddr, sosEndAddr, eosStartAddr, eosAddr;
923 1.3 oster RF_PhysDiskAddr_t *pda;
924 1.3 oster int foundit, i;
925 1.3 oster
926 1.3 oster foundit = 0;
927 1.3 oster /* first compute the following raid addresses: start of stripe,
928 1.3 oster * (sosAddr) MIN(start of access, start of failed SU), (sosEndAddr)
929 1.3 oster * MAX(end of access, end of failed SU), (eosStartAddr) end of
930 1.3 oster * stripe (i.e. start of next stripe) (eosAddr) */
931 1.3 oster sosAddr = rf_RaidAddressOfPrevStripeBoundary(layoutPtr, asmap->raidAddress);
932 1.3 oster sosEndAddr = RF_MIN(asmap->raidAddress, rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, failedPDA->raidAddress));
933 1.3 oster eosStartAddr = RF_MAX(asmap->endRaidAddress, rf_RaidAddressOfNextStripeUnitBoundary(layoutPtr, failedPDA->raidAddress));
934 1.3 oster eosAddr = rf_RaidAddressOfNextStripeBoundary(layoutPtr, asmap->raidAddress);
935 1.3 oster
936 1.3 oster /* now generate access stripe maps for each of the above regions of
937 1.3 oster * the stripe. Use a dummy (NULL) buf ptr for now */
938 1.3 oster
939 1.3 oster new_asm_h[0] = (sosAddr != sosEndAddr) ? rf_MapAccess(raidPtr, sosAddr, sosEndAddr - sosAddr, NULL, RF_DONT_REMAP) : NULL;
940 1.3 oster new_asm_h[1] = (eosStartAddr != eosAddr) ? rf_MapAccess(raidPtr, eosStartAddr, eosAddr - eosStartAddr, NULL, RF_DONT_REMAP) : NULL;
941 1.3 oster
942 1.3 oster /* walk through the PDAs and range-restrict each SU to the region of
943 1.3 oster * the SU touched on the failed PDA. also compute total data buffer
944 1.3 oster * space requirements in this step. Ignore the parity for now. */
945 1.35 oster /* Also count nodes to find out how many bufs need to be xored together */
946 1.35 oster (*nXorBufs) = 1; /* in read case, 1 is for parity. In write
947 1.35 oster * case, 1 is for failed data */
948 1.3 oster
949 1.3 oster if (new_asm_h[0]) {
950 1.3 oster new_asm_h[0]->next = dag_h->asmList;
951 1.3 oster dag_h->asmList = new_asm_h[0];
952 1.3 oster for (pda = new_asm_h[0]->stripeMap->physInfo; pda; pda = pda->next) {
953 1.3 oster rf_RangeRestrictPDA(raidPtr, failedPDA, pda, RF_RESTRICT_NOBUFFER, 0);
954 1.35 oster pda->bufPtr = rf_AllocBuffer(raidPtr, pda, allocList);
955 1.3 oster }
956 1.35 oster (*nXorBufs) += new_asm_h[0]->stripeMap->numStripeUnitsAccessed;
957 1.3 oster }
958 1.3 oster if (new_asm_h[1]) {
959 1.3 oster new_asm_h[1]->next = dag_h->asmList;
960 1.3 oster dag_h->asmList = new_asm_h[1];
961 1.3 oster for (pda = new_asm_h[1]->stripeMap->physInfo; pda; pda = pda->next) {
962 1.3 oster rf_RangeRestrictPDA(raidPtr, failedPDA, pda, RF_RESTRICT_NOBUFFER, 0);
963 1.35 oster pda->bufPtr = rf_AllocBuffer(raidPtr, pda, allocList);
964 1.3 oster }
965 1.3 oster (*nXorBufs) += new_asm_h[1]->stripeMap->numStripeUnitsAccessed;
966 1.3 oster }
967 1.35 oster
968 1.35 oster /* allocate a buffer for parity */
969 1.35 oster if (rpBufPtr)
970 1.35 oster *rpBufPtr = rf_AllocBuffer(raidPtr, failedPDA, allocList);
971 1.3 oster
972 1.3 oster /* the last step is to figure out how many more distinct buffers need
973 1.3 oster * to get xor'd to produce the missing unit. there's one for each
974 1.3 oster * user-data read node that overlaps the portion of the failed unit
975 1.3 oster * being accessed */
976 1.3 oster
977 1.3 oster for (foundit = i = 0, pda = asmap->physInfo; pda; i++, pda = pda->next) {
978 1.3 oster if (pda == failedPDA) {
979 1.3 oster i--;
980 1.3 oster foundit = 1;
981 1.3 oster continue;
982 1.3 oster }
983 1.3 oster if (rf_PDAOverlap(layoutPtr, pda, failedPDA)) {
984 1.3 oster overlappingPDAs[i] = 1;
985 1.3 oster (*nXorBufs)++;
986 1.3 oster }
987 1.3 oster }
988 1.3 oster if (!foundit) {
989 1.3 oster RF_ERRORMSG("GenerateFailedAccessASMs: did not find failedPDA in asm list\n");
990 1.3 oster RF_ASSERT(0);
991 1.3 oster }
992 1.31 oster #if RF_DEBUG_DAG
993 1.3 oster if (rf_degDagDebug) {
994 1.3 oster if (new_asm_h[0]) {
995 1.3 oster printf("First asm:\n");
996 1.3 oster rf_PrintFullAccessStripeMap(new_asm_h[0], 1);
997 1.3 oster }
998 1.3 oster if (new_asm_h[1]) {
999 1.3 oster printf("Second asm:\n");
1000 1.3 oster rf_PrintFullAccessStripeMap(new_asm_h[1], 1);
1001 1.3 oster }
1002 1.3 oster }
1003 1.31 oster #endif
1004 1.1 oster }
1005 1.1 oster
1006 1.1 oster
1007 1.1 oster /* adjusts the offset and number of sectors in the destination pda so that
1008 1.1 oster * it covers at most the region of the SU covered by the source PDA. This
1009 1.1 oster * is exclusively a restriction: the number of sectors indicated by the
1010 1.1 oster * target PDA can only shrink.
1011 1.1 oster *
1012 1.1 oster * For example: s = sectors within SU indicated by source PDA
1013 1.1 oster * d = sectors within SU indicated by dest PDA
1014 1.1 oster * r = results, stored in dest PDA
1015 1.1 oster *
1016 1.1 oster * |--------------- one stripe unit ---------------------|
1017 1.1 oster * | sssssssssssssssssssssssssssssssss |
1018 1.1 oster * | ddddddddddddddddddddddddddddddddddddddddddddd |
1019 1.1 oster * | rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr |
1020 1.1 oster *
1021 1.1 oster * Another example:
1022 1.1 oster *
1023 1.1 oster * |--------------- one stripe unit ---------------------|
1024 1.1 oster * | sssssssssssssssssssssssssssssssss |
1025 1.1 oster * | ddddddddddddddddddddddd |
1026 1.1 oster * | rrrrrrrrrrrrrrrr |
1027 1.1 oster *
1028 1.1 oster */
1029 1.3 oster void
1030 1.23 oster rf_RangeRestrictPDA(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *src,
1031 1.23 oster RF_PhysDiskAddr_t *dest, int dobuffer, int doraidaddr)
1032 1.3 oster {
1033 1.3 oster RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
1034 1.3 oster RF_SectorNum_t soffs = rf_StripeUnitOffset(layoutPtr, src->startSector);
1035 1.3 oster RF_SectorNum_t doffs = rf_StripeUnitOffset(layoutPtr, dest->startSector);
1036 1.3 oster RF_SectorNum_t send = rf_StripeUnitOffset(layoutPtr, src->startSector + src->numSector - 1); /* use -1 to be sure we
1037 1.3 oster * stay within SU */
1038 1.3 oster RF_SectorNum_t dend = rf_StripeUnitOffset(layoutPtr, dest->startSector + dest->numSector - 1);
1039 1.3 oster RF_SectorNum_t subAddr = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, dest->startSector); /* stripe unit boundary */
1040 1.3 oster
1041 1.3 oster dest->startSector = subAddr + RF_MAX(soffs, doffs);
1042 1.3 oster dest->numSector = subAddr + RF_MIN(send, dend) + 1 - dest->startSector;
1043 1.3 oster
1044 1.3 oster if (dobuffer)
1045 1.3 oster dest->bufPtr += (soffs > doffs) ? rf_RaidAddressToByte(raidPtr, soffs - doffs) : 0;
1046 1.3 oster if (doraidaddr) {
1047 1.3 oster dest->raidAddress = rf_RaidAddressOfPrevStripeUnitBoundary(layoutPtr, dest->raidAddress) +
1048 1.3 oster rf_StripeUnitOffset(layoutPtr, dest->startSector);
1049 1.3 oster }
1050 1.1 oster }
1051 1.11 oster
1052 1.11 oster #if (RF_INCLUDE_CHAINDECLUSTER > 0)
1053 1.11 oster
1054 1.1 oster /*
1055 1.1 oster * Want the highest of these primes to be the largest one
1056 1.1 oster * less than the max expected number of columns (won't hurt
1057 1.1 oster * to be too small or too large, but won't be optimal, either)
1058 1.1 oster * --jimz
1059 1.1 oster */
1060 1.1 oster #define NLOWPRIMES 8
1061 1.3 oster static int lowprimes[NLOWPRIMES] = {2, 3, 5, 7, 11, 13, 17, 19};
1062 1.1 oster /*****************************************************************************
1063 1.1 oster * compute the workload shift factor. (chained declustering)
1064 1.1 oster *
1065 1.1 oster * return nonzero if access should shift to secondary, otherwise,
1066 1.1 oster * access is to primary
1067 1.1 oster *****************************************************************************/
1068 1.3 oster int
1069 1.23 oster rf_compute_workload_shift(RF_Raid_t *raidPtr, RF_PhysDiskAddr_t *pda)
1070 1.3 oster {
1071 1.3 oster /*
1072 1.3 oster * variables:
1073 1.3 oster * d = column of disk containing primary
1074 1.3 oster * f = column of failed disk
1075 1.3 oster * n = number of disks in array
1076 1.3 oster * sd = "shift distance" (number of columns that d is to the right of f)
1077 1.3 oster * v = numerator of redirection ratio
1078 1.3 oster * k = denominator of redirection ratio
1079 1.3 oster */
1080 1.21 oster RF_RowCol_t d, f, sd, n;
1081 1.3 oster int k, v, ret, i;
1082 1.3 oster
1083 1.3 oster n = raidPtr->numCol;
1084 1.3 oster
1085 1.3 oster /* assign column of primary copy to d */
1086 1.3 oster d = pda->col;
1087 1.3 oster
1088 1.3 oster /* assign column of dead disk to f */
1089 1.21 oster for (f = 0; ((!RF_DEAD_DISK(raidPtr->Disks[f].status)) && (f < n)); f++);
1090 1.3 oster
1091 1.3 oster RF_ASSERT(f < n);
1092 1.3 oster RF_ASSERT(f != d);
1093 1.3 oster
1094 1.3 oster sd = (f > d) ? (n + d - f) : (d - f);
1095 1.3 oster RF_ASSERT(sd < n);
1096 1.3 oster
1097 1.3 oster /*
1098 1.3 oster * v of every k accesses should be redirected
1099 1.3 oster *
1100 1.3 oster * v/k := (n-1-sd)/(n-1)
1101 1.3 oster */
1102 1.3 oster v = (n - 1 - sd);
1103 1.3 oster k = (n - 1);
1104 1.1 oster
1105 1.1 oster #if 1
1106 1.3 oster /*
1107 1.3 oster * XXX
1108 1.3 oster * Is this worth it?
1109 1.3 oster *
1110 1.3 oster * Now reduce the fraction, by repeatedly factoring
1111 1.3 oster * out primes (just like they teach in elementary school!)
1112 1.3 oster */
1113 1.3 oster for (i = 0; i < NLOWPRIMES; i++) {
1114 1.3 oster if (lowprimes[i] > v)
1115 1.3 oster break;
1116 1.3 oster while (((v % lowprimes[i]) == 0) && ((k % lowprimes[i]) == 0)) {
1117 1.3 oster v /= lowprimes[i];
1118 1.3 oster k /= lowprimes[i];
1119 1.3 oster }
1120 1.3 oster }
1121 1.1 oster #endif
1122 1.1 oster
1123 1.21 oster raidPtr->hist_diskreq[d]++;
1124 1.21 oster if (raidPtr->hist_diskreq[d] > v) {
1125 1.3 oster ret = 0; /* do not redirect */
1126 1.3 oster } else {
1127 1.3 oster ret = 1; /* redirect */
1128 1.3 oster }
1129 1.1 oster
1130 1.1 oster #if 0
1131 1.3 oster printf("d=%d f=%d sd=%d v=%d k=%d ret=%d h=%d\n", d, f, sd, v, k, ret,
1132 1.21 oster raidPtr->hist_diskreq[d]);
1133 1.1 oster #endif
1134 1.1 oster
1135 1.21 oster if (raidPtr->hist_diskreq[d] >= k) {
1136 1.3 oster /* reset counter */
1137 1.21 oster raidPtr->hist_diskreq[d] = 0;
1138 1.3 oster }
1139 1.3 oster return (ret);
1140 1.1 oster }
1141 1.11 oster #endif /* (RF_INCLUDE_CHAINDECLUSTER > 0) */
1142 1.11 oster
1143 1.1 oster /*
1144 1.1 oster * Disk selection routines
1145 1.1 oster */
1146 1.1 oster
1147 1.1 oster /*
1148 1.1 oster * Selects the disk with the shortest queue from a mirror pair.
1149 1.1 oster * Both the disk I/Os queued in RAIDframe as well as those at the physical
1150 1.1 oster * disk are counted as members of the "queue"
1151 1.1 oster */
1152 1.3 oster void
1153 1.3 oster rf_SelectMirrorDiskIdle(RF_DagNode_t * node)
1154 1.1 oster {
1155 1.3 oster RF_Raid_t *raidPtr = (RF_Raid_t *) node->dagHdr->raidPtr;
1156 1.21 oster RF_RowCol_t colData, colMirror;
1157 1.3 oster int dataQueueLength, mirrorQueueLength, usemirror;
1158 1.3 oster RF_PhysDiskAddr_t *data_pda = (RF_PhysDiskAddr_t *) node->params[0].p;
1159 1.3 oster RF_PhysDiskAddr_t *mirror_pda = (RF_PhysDiskAddr_t *) node->params[4].p;
1160 1.3 oster RF_PhysDiskAddr_t *tmp_pda;
1161 1.21 oster RF_RaidDisk_t *disks = raidPtr->Disks;
1162 1.21 oster RF_DiskQueue_t *dqs = raidPtr->Queues, *dataQueue, *mirrorQueue;
1163 1.3 oster
1164 1.3 oster /* return the [row col] of the disk with the shortest queue */
1165 1.3 oster colData = data_pda->col;
1166 1.3 oster colMirror = mirror_pda->col;
1167 1.21 oster dataQueue = &(dqs[colData]);
1168 1.21 oster mirrorQueue = &(dqs[colMirror]);
1169 1.1 oster
1170 1.1 oster #ifdef RF_LOCK_QUEUES_TO_READ_LEN
1171 1.3 oster RF_LOCK_QUEUE_MUTEX(dataQueue, "SelectMirrorDiskIdle");
1172 1.3 oster #endif /* RF_LOCK_QUEUES_TO_READ_LEN */
1173 1.3 oster dataQueueLength = dataQueue->queueLength + dataQueue->numOutstanding;
1174 1.1 oster #ifdef RF_LOCK_QUEUES_TO_READ_LEN
1175 1.3 oster RF_UNLOCK_QUEUE_MUTEX(dataQueue, "SelectMirrorDiskIdle");
1176 1.3 oster RF_LOCK_QUEUE_MUTEX(mirrorQueue, "SelectMirrorDiskIdle");
1177 1.3 oster #endif /* RF_LOCK_QUEUES_TO_READ_LEN */
1178 1.3 oster mirrorQueueLength = mirrorQueue->queueLength + mirrorQueue->numOutstanding;
1179 1.1 oster #ifdef RF_LOCK_QUEUES_TO_READ_LEN
1180 1.3 oster RF_UNLOCK_QUEUE_MUTEX(mirrorQueue, "SelectMirrorDiskIdle");
1181 1.3 oster #endif /* RF_LOCK_QUEUES_TO_READ_LEN */
1182 1.1 oster
1183 1.3 oster usemirror = 0;
1184 1.21 oster if (RF_DEAD_DISK(disks[colMirror].status)) {
1185 1.3 oster usemirror = 0;
1186 1.3 oster } else
1187 1.21 oster if (RF_DEAD_DISK(disks[colData].status)) {
1188 1.3 oster usemirror = 1;
1189 1.3 oster } else
1190 1.5 oster if (raidPtr->parity_good == RF_RAID_DIRTY) {
1191 1.5 oster /* Trust only the main disk */
1192 1.3 oster usemirror = 0;
1193 1.3 oster } else
1194 1.5 oster if (dataQueueLength < mirrorQueueLength) {
1195 1.5 oster usemirror = 0;
1196 1.5 oster } else
1197 1.5 oster if (mirrorQueueLength < dataQueueLength) {
1198 1.5 oster usemirror = 1;
1199 1.3 oster } else {
1200 1.5 oster /* queues are equal length. attempt
1201 1.5 oster * cleverness. */
1202 1.5 oster if (SNUM_DIFF(dataQueue->last_deq_sector, data_pda->startSector)
1203 1.5 oster <= SNUM_DIFF(mirrorQueue->last_deq_sector, mirror_pda->startSector)) {
1204 1.5 oster usemirror = 0;
1205 1.5 oster } else {
1206 1.5 oster usemirror = 1;
1207 1.5 oster }
1208 1.3 oster }
1209 1.3 oster
1210 1.3 oster if (usemirror) {
1211 1.3 oster /* use mirror (parity) disk, swap params 0 & 4 */
1212 1.3 oster tmp_pda = data_pda;
1213 1.3 oster node->params[0].p = mirror_pda;
1214 1.3 oster node->params[4].p = tmp_pda;
1215 1.3 oster } else {
1216 1.3 oster /* use data disk, leave param 0 unchanged */
1217 1.3 oster }
1218 1.3 oster /* printf("dataQueueLength %d, mirrorQueueLength
1219 1.3 oster * %d\n",dataQueueLength, mirrorQueueLength); */
1220 1.1 oster }
1221 1.19 oster #if (RF_INCLUDE_CHAINDECLUSTER > 0) || (RF_INCLUDE_INTERDECLUSTER > 0) || (RF_DEBUG_VALIDATE_DAG > 0)
1222 1.1 oster /*
1223 1.1 oster * Do simple partitioning. This assumes that
1224 1.1 oster * the data and parity disks are laid out identically.
1225 1.1 oster */
1226 1.3 oster void
1227 1.3 oster rf_SelectMirrorDiskPartition(RF_DagNode_t * node)
1228 1.1 oster {
1229 1.3 oster RF_Raid_t *raidPtr = (RF_Raid_t *) node->dagHdr->raidPtr;
1230 1.21 oster RF_RowCol_t colData, colMirror;
1231 1.3 oster RF_PhysDiskAddr_t *data_pda = (RF_PhysDiskAddr_t *) node->params[0].p;
1232 1.3 oster RF_PhysDiskAddr_t *mirror_pda = (RF_PhysDiskAddr_t *) node->params[4].p;
1233 1.3 oster RF_PhysDiskAddr_t *tmp_pda;
1234 1.21 oster RF_RaidDisk_t *disks = raidPtr->Disks;
1235 1.3 oster int usemirror;
1236 1.3 oster
1237 1.3 oster /* return the [row col] of the disk with the shortest queue */
1238 1.3 oster colData = data_pda->col;
1239 1.3 oster colMirror = mirror_pda->col;
1240 1.3 oster
1241 1.3 oster usemirror = 0;
1242 1.21 oster if (RF_DEAD_DISK(disks[colMirror].status)) {
1243 1.3 oster usemirror = 0;
1244 1.3 oster } else
1245 1.21 oster if (RF_DEAD_DISK(disks[colData].status)) {
1246 1.3 oster usemirror = 1;
1247 1.6 oster } else
1248 1.6 oster if (raidPtr->parity_good == RF_RAID_DIRTY) {
1249 1.6 oster /* Trust only the main disk */
1250 1.3 oster usemirror = 0;
1251 1.6 oster } else
1252 1.6 oster if (data_pda->startSector <
1253 1.21 oster (disks[colData].numBlocks / 2)) {
1254 1.6 oster usemirror = 0;
1255 1.6 oster } else {
1256 1.6 oster usemirror = 1;
1257 1.6 oster }
1258 1.3 oster
1259 1.3 oster if (usemirror) {
1260 1.3 oster /* use mirror (parity) disk, swap params 0 & 4 */
1261 1.3 oster tmp_pda = data_pda;
1262 1.3 oster node->params[0].p = mirror_pda;
1263 1.3 oster node->params[4].p = tmp_pda;
1264 1.3 oster } else {
1265 1.3 oster /* use data disk, leave param 0 unchanged */
1266 1.3 oster }
1267 1.1 oster }
1268 1.19 oster #endif
1269