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