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