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