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