rf_reconstruct.c revision 1.4 1 /* $NetBSD: rf_reconstruct.c,v 1.4 1999/02/05 00:06:16 oster Exp $ */
2 /*
3 * Copyright (c) 1995 Carnegie-Mellon University.
4 * All rights reserved.
5 *
6 * Author: Mark Holland
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_reconstruct.c -- code to perform on-line reconstruction
32 *
33 ************************************************************/
34
35 #include "rf_types.h"
36 #include <sys/time.h>
37 #include <sys/buf.h>
38 #include <sys/errno.h>
39 #include "rf_raid.h"
40 #include "rf_reconutil.h"
41 #include "rf_revent.h"
42 #include "rf_reconbuffer.h"
43 #include "rf_threadid.h"
44 #include "rf_acctrace.h"
45 #include "rf_etimer.h"
46 #include "rf_dag.h"
47 #include "rf_desc.h"
48 #include "rf_general.h"
49 #include "rf_freelist.h"
50 #include "rf_debugprint.h"
51 #include "rf_driver.h"
52 #include "rf_utils.h"
53 #include "rf_cpuutil.h"
54 #include "rf_shutdown.h"
55 #include "rf_sys.h"
56
57 #include "rf_kintf.h"
58
59 /* setting these to -1 causes them to be set to their default values if not set by debug options */
60
61 #define Dprintf(s) if (rf_reconDebug) rf_debug_printf(s,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)
62 #define Dprintf1(s,a) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),NULL,NULL,NULL,NULL,NULL,NULL,NULL)
63 #define Dprintf2(s,a,b) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),NULL,NULL,NULL,NULL,NULL,NULL)
64 #define Dprintf3(s,a,b,c) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),NULL,NULL,NULL,NULL,NULL)
65 #define Dprintf4(s,a,b,c,d) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),NULL,NULL,NULL,NULL)
66 #define Dprintf5(s,a,b,c,d,e) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),NULL,NULL,NULL)
67 #define Dprintf6(s,a,b,c,d,e,f) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),NULL,NULL)
68 #define Dprintf7(s,a,b,c,d,e,f,g) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),(void *)((unsigned long)g),NULL)
69 #define Dprintf8(s,a,b,c,d,e,f,g,h) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),(void *)((unsigned long)g),(void *)((unsigned long)h))
70
71 #define DDprintf1(s,a) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),NULL,NULL,NULL,NULL,NULL,NULL,NULL)
72 #define DDprintf2(s,a,b) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),NULL,NULL,NULL,NULL,NULL,NULL)
73 #define DDprintf3(s,a,b,c) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),NULL,NULL,NULL,NULL,NULL)
74 #define DDprintf4(s,a,b,c,d) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),NULL,NULL,NULL,NULL)
75 #define DDprintf5(s,a,b,c,d,e) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),NULL,NULL,NULL)
76 #define DDprintf6(s,a,b,c,d,e,f) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),NULL,NULL)
77 #define DDprintf7(s,a,b,c,d,e,f,g) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),(void *)((unsigned long)g),NULL)
78 #define DDprintf8(s,a,b,c,d,e,f,g,h) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),(void *)((unsigned long)b),(void *)((unsigned long)c),(void *)((unsigned long)d),(void *)((unsigned long)e),(void *)((unsigned long)f),(void *)((unsigned long)g),(void *)((unsigned long)h))
79
80 static RF_Thread_t recon_thr_handle;
81 static int recon_thread_initialized = 0;
82
83 static RF_FreeList_t *rf_recond_freelist;
84 #define RF_MAX_FREE_RECOND 4
85 #define RF_RECOND_INC 1
86
87 static RF_RaidReconDesc_t *
88 AllocRaidReconDesc(RF_Raid_t * raidPtr,
89 RF_RowCol_t row, RF_RowCol_t col, RF_RaidDisk_t * spareDiskPtr,
90 int numDisksDone, RF_RowCol_t srow, RF_RowCol_t scol);
91 static void FreeReconDesc(RF_RaidReconDesc_t * reconDesc);
92 static int
93 ProcessReconEvent(RF_Raid_t * raidPtr, RF_RowCol_t frow,
94 RF_ReconEvent_t * event);
95 static int
96 IssueNextReadRequest(RF_Raid_t * raidPtr, RF_RowCol_t row,
97 RF_RowCol_t col);
98 static int TryToRead(RF_Raid_t * raidPtr, RF_RowCol_t row, RF_RowCol_t col);
99 static int
100 ComputePSDiskOffsets(RF_Raid_t * raidPtr, RF_StripeNum_t psid,
101 RF_RowCol_t row, RF_RowCol_t col, RF_SectorNum_t * outDiskOffset,
102 RF_SectorNum_t * outFailedDiskSectorOffset, RF_RowCol_t * spRow,
103 RF_RowCol_t * spCol, RF_SectorNum_t * spOffset);
104 static int IssueNextWriteRequest(RF_Raid_t * raidPtr, RF_RowCol_t row);
105 static int ReconReadDoneProc(void *arg, int status);
106 static int ReconWriteDoneProc(void *arg, int status);
107 static void
108 CheckForNewMinHeadSep(RF_Raid_t * raidPtr, RF_RowCol_t row,
109 RF_HeadSepLimit_t hsCtr);
110 static int
111 CheckHeadSeparation(RF_Raid_t * raidPtr, RF_PerDiskReconCtrl_t * ctrl,
112 RF_RowCol_t row, RF_RowCol_t col, RF_HeadSepLimit_t hsCtr,
113 RF_ReconUnitNum_t which_ru);
114 static int
115 CheckForcedOrBlockedReconstruction(RF_Raid_t * raidPtr,
116 RF_ReconParityStripeStatus_t * pssPtr, RF_PerDiskReconCtrl_t * ctrl,
117 RF_RowCol_t row, RF_RowCol_t col, RF_StripeNum_t psid,
118 RF_ReconUnitNum_t which_ru);
119 static void ForceReconReadDoneProc(void *arg, int status);
120
121 static void rf_ShutdownReconstruction(void *);
122
123
124 struct RF_ReconDoneProc_s {
125 void (*proc) (RF_Raid_t *, void *);
126 void *arg;
127 RF_ReconDoneProc_t *next;
128 };
129
130 static RF_FreeList_t *rf_rdp_freelist;
131 #define RF_MAX_FREE_RDP 4
132 #define RF_RDP_INC 1
133
134 static void
135 SignalReconDone(RF_Raid_t * raidPtr)
136 {
137 RF_ReconDoneProc_t *p;
138
139 RF_LOCK_MUTEX(raidPtr->recon_done_proc_mutex);
140 for (p = raidPtr->recon_done_procs; p; p = p->next) {
141 p->proc(raidPtr, p->arg);
142 }
143 RF_UNLOCK_MUTEX(raidPtr->recon_done_proc_mutex);
144 }
145
146 int
147 rf_RegisterReconDoneProc(
148 RF_Raid_t * raidPtr,
149 void (*proc) (RF_Raid_t *, void *),
150 void *arg,
151 RF_ReconDoneProc_t ** handlep)
152 {
153 RF_ReconDoneProc_t *p;
154
155 RF_FREELIST_GET(rf_rdp_freelist, p, next, (RF_ReconDoneProc_t *));
156 if (p == NULL)
157 return (ENOMEM);
158 p->proc = proc;
159 p->arg = arg;
160 RF_LOCK_MUTEX(raidPtr->recon_done_proc_mutex);
161 p->next = raidPtr->recon_done_procs;
162 raidPtr->recon_done_procs = p;
163 RF_UNLOCK_MUTEX(raidPtr->recon_done_proc_mutex);
164 if (handlep)
165 *handlep = p;
166 return (0);
167 }
168 /*****************************************************************************************
169 *
170 * sets up the parameters that will be used by the reconstruction process
171 * currently there are none, except for those that the layout-specific
172 * configuration (e.g. rf_ConfigureDeclustered) routine sets up.
173 *
174 * in the kernel, we fire off the recon thread.
175 *
176 ****************************************************************************************/
177 static void
178 rf_ShutdownReconstruction(ignored)
179 void *ignored;
180 {
181 RF_FREELIST_DESTROY(rf_recond_freelist, next, (RF_RaidReconDesc_t *));
182 RF_FREELIST_DESTROY(rf_rdp_freelist, next, (RF_ReconDoneProc_t *));
183 }
184
185 int
186 rf_ConfigureReconstruction(listp)
187 RF_ShutdownList_t **listp;
188 {
189 int rc;
190
191 RF_FREELIST_CREATE(rf_recond_freelist, RF_MAX_FREE_RECOND,
192 RF_RECOND_INC, sizeof(RF_RaidReconDesc_t));
193 if (rf_recond_freelist == NULL)
194 return (ENOMEM);
195 RF_FREELIST_CREATE(rf_rdp_freelist, RF_MAX_FREE_RDP,
196 RF_RDP_INC, sizeof(RF_ReconDoneProc_t));
197 if (rf_rdp_freelist == NULL) {
198 RF_FREELIST_DESTROY(rf_recond_freelist, next, (RF_RaidReconDesc_t *));
199 return (ENOMEM);
200 }
201 rc = rf_ShutdownCreate(listp, rf_ShutdownReconstruction, NULL);
202 if (rc) {
203 RF_ERRORMSG3("Unable to add to shutdown list file %s line %d rc=%d\n",
204 __FILE__, __LINE__, rc);
205 rf_ShutdownReconstruction(NULL);
206 return (rc);
207 }
208 if (!recon_thread_initialized) {
209 RF_CREATE_THREAD(recon_thr_handle, rf_ReconKernelThread, NULL);
210 recon_thread_initialized = 1;
211 }
212 return (0);
213 }
214
215 static RF_RaidReconDesc_t *
216 AllocRaidReconDesc(raidPtr, row, col, spareDiskPtr, numDisksDone, srow, scol)
217 RF_Raid_t *raidPtr;
218 RF_RowCol_t row;
219 RF_RowCol_t col;
220 RF_RaidDisk_t *spareDiskPtr;
221 int numDisksDone;
222 RF_RowCol_t srow;
223 RF_RowCol_t scol;
224 {
225
226 RF_RaidReconDesc_t *reconDesc;
227
228 RF_FREELIST_GET(rf_recond_freelist, reconDesc, next, (RF_RaidReconDesc_t *));
229
230 reconDesc->raidPtr = raidPtr;
231 reconDesc->row = row;
232 reconDesc->col = col;
233 reconDesc->spareDiskPtr = spareDiskPtr;
234 reconDesc->numDisksDone = numDisksDone;
235 reconDesc->srow = srow;
236 reconDesc->scol = scol;
237 reconDesc->state = 0;
238 reconDesc->next = NULL;
239
240 return (reconDesc);
241 }
242
243 static void
244 FreeReconDesc(reconDesc)
245 RF_RaidReconDesc_t *reconDesc;
246 {
247 #if RF_RECON_STATS > 0
248 printf("RAIDframe: %lu recon event waits, %lu recon delays\n",
249 (long) reconDesc->numReconEventWaits, (long) reconDesc->numReconExecDelays);
250 #endif /* RF_RECON_STATS > 0 */
251 printf("RAIDframe: %lu max exec ticks\n",
252 (long) reconDesc->maxReconExecTicks);
253 #if (RF_RECON_STATS > 0) || defined(KERNEL)
254 printf("\n");
255 #endif /* (RF_RECON_STATS > 0) || KERNEL */
256 RF_FREELIST_FREE(rf_recond_freelist, reconDesc, next);
257 }
258
259
260 /*****************************************************************************************
261 *
262 * primary routine to reconstruct a failed disk. This should be called from
263 * within its own thread. It won't return until reconstruction completes,
264 * fails, or is aborted.
265 ****************************************************************************************/
266 int
267 rf_ReconstructFailedDisk(raidPtr, row, col)
268 RF_Raid_t *raidPtr;
269 RF_RowCol_t row;
270 RF_RowCol_t col;
271 {
272 RF_LayoutSW_t *lp;
273 int rc;
274
275 lp = raidPtr->Layout.map;
276 if (lp->SubmitReconBuffer) {
277 /*
278 * The current infrastructure only supports reconstructing one
279 * disk at a time for each array.
280 */
281 RF_LOCK_MUTEX(raidPtr->mutex);
282 while (raidPtr->reconInProgress) {
283 RF_WAIT_COND(raidPtr->waitForReconCond, raidPtr->mutex);
284 }
285 raidPtr->reconInProgress++;
286 RF_UNLOCK_MUTEX(raidPtr->mutex);
287 rc = rf_ReconstructFailedDiskBasic(raidPtr, row, col);
288 } else {
289 RF_ERRORMSG1("RECON: no way to reconstruct failed disk for arch %c\n",
290 lp->parityConfig);
291 rc = EIO;
292 }
293 RF_LOCK_MUTEX(raidPtr->mutex);
294 raidPtr->reconInProgress--;
295 RF_UNLOCK_MUTEX(raidPtr->mutex);
296 RF_SIGNAL_COND(raidPtr->waitForReconCond);
297 wakeup(&raidPtr->waitForReconCond); /* XXX Methinks this will be
298 * needed at some point... GO */
299 return (rc);
300 }
301
302 int
303 rf_ReconstructFailedDiskBasic(raidPtr, row, col)
304 RF_Raid_t *raidPtr;
305 RF_RowCol_t row;
306 RF_RowCol_t col;
307 {
308 RF_RaidDisk_t *spareDiskPtr = NULL;
309 RF_RaidReconDesc_t *reconDesc;
310 RF_RowCol_t srow, scol;
311 int numDisksDone = 0, rc;
312
313 /* first look for a spare drive onto which to reconstruct the data */
314 /* spare disk descriptors are stored in row 0. This may have to
315 * change eventually */
316
317 RF_LOCK_MUTEX(raidPtr->mutex);
318 RF_ASSERT(raidPtr->Disks[row][col].status == rf_ds_failed);
319
320 if (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE) {
321 if (raidPtr->status[row] != rf_rs_degraded) {
322 RF_ERRORMSG2("Unable to reconstruct disk at row %d col %d because status not degraded\n", row, col);
323 RF_UNLOCK_MUTEX(raidPtr->mutex);
324 return (EINVAL);
325 }
326 srow = row;
327 scol = (-1);
328 } else {
329 srow = 0;
330 for (scol = raidPtr->numCol; scol < raidPtr->numCol + raidPtr->numSpare; scol++) {
331 if (raidPtr->Disks[srow][scol].status == rf_ds_spare) {
332 spareDiskPtr = &raidPtr->Disks[srow][scol];
333 spareDiskPtr->status = rf_ds_used_spare;
334 break;
335 }
336 }
337 if (!spareDiskPtr) {
338 RF_ERRORMSG2("Unable to reconstruct disk at row %d col %d because no spares are available\n", row, col);
339 RF_UNLOCK_MUTEX(raidPtr->mutex);
340 return (ENOSPC);
341 }
342 printf("RECON: initiating reconstruction on row %d col %d -> spare at row %d col %d\n", row, col, srow, scol);
343 }
344 RF_UNLOCK_MUTEX(raidPtr->mutex);
345
346 reconDesc = AllocRaidReconDesc((void *) raidPtr, row, col, spareDiskPtr, numDisksDone, srow, scol);
347 raidPtr->reconDesc = (void *) reconDesc;
348 #if RF_RECON_STATS > 0
349 reconDesc->hsStallCount = 0;
350 reconDesc->numReconExecDelays = 0;
351 reconDesc->numReconEventWaits = 0;
352 #endif /* RF_RECON_STATS > 0 */
353 reconDesc->reconExecTimerRunning = 0;
354 reconDesc->reconExecTicks = 0;
355 reconDesc->maxReconExecTicks = 0;
356 rc = rf_ContinueReconstructFailedDisk(reconDesc);
357 return (rc);
358 }
359
360
361 int
362 rf_ContinueReconstructFailedDisk(reconDesc)
363 RF_RaidReconDesc_t *reconDesc;
364 {
365 RF_Raid_t *raidPtr = reconDesc->raidPtr;
366 RF_RowCol_t row = reconDesc->row;
367 RF_RowCol_t col = reconDesc->col;
368 RF_RowCol_t srow = reconDesc->srow;
369 RF_RowCol_t scol = reconDesc->scol;
370 RF_ReconMap_t *mapPtr;
371
372 RF_ReconEvent_t *event;
373 struct timeval etime, elpsd;
374 unsigned long xor_s, xor_resid_us;
375 int retcode, i, ds;
376
377 switch (reconDesc->state) {
378
379
380 case 0:
381
382 raidPtr->accumXorTimeUs = 0;
383
384 /* create one trace record per physical disk */
385 RF_Malloc(raidPtr->recon_tracerecs, raidPtr->numCol * sizeof(RF_AccTraceEntry_t), (RF_AccTraceEntry_t *));
386
387 /* quiesce the array prior to starting recon. this is needed
388 * to assure no nasty interactions with pending user writes.
389 * We need to do this before we change the disk or row status. */
390 reconDesc->state = 1;
391
392 Dprintf("RECON: begin request suspend\n");
393 retcode = rf_SuspendNewRequestsAndWait(raidPtr);
394 Dprintf("RECON: end request suspend\n");
395 rf_StartUserStats(raidPtr); /* zero out the stats kept on
396 * user accs */
397
398 /* fall through to state 1 */
399
400 case 1:
401
402 RF_LOCK_MUTEX(raidPtr->mutex);
403
404 /* create the reconstruction control pointer and install it in
405 * the right slot */
406 raidPtr->reconControl[row] = rf_MakeReconControl(reconDesc, row, col, srow, scol);
407 mapPtr = raidPtr->reconControl[row]->reconMap;
408 raidPtr->status[row] = rf_rs_reconstructing;
409 raidPtr->Disks[row][col].status = rf_ds_reconstructing;
410 raidPtr->Disks[row][col].spareRow = srow;
411 raidPtr->Disks[row][col].spareCol = scol;
412
413 RF_UNLOCK_MUTEX(raidPtr->mutex);
414
415 RF_GETTIME(raidPtr->reconControl[row]->starttime);
416
417 /* now start up the actual reconstruction: issue a read for
418 * each surviving disk */
419 rf_start_cpu_monitor();
420 reconDesc->numDisksDone = 0;
421 for (i = 0; i < raidPtr->numCol; i++) {
422 if (i != col) {
423 /* find and issue the next I/O on the
424 * indicated disk */
425 if (IssueNextReadRequest(raidPtr, row, i)) {
426 Dprintf2("RECON: done issuing for r%d c%d\n", row, i);
427 reconDesc->numDisksDone++;
428 }
429 }
430 }
431
432 case 2:
433 Dprintf("RECON: resume requests\n");
434 rf_ResumeNewRequests(raidPtr);
435
436
437 reconDesc->state = 3;
438
439 case 3:
440
441 /* process reconstruction events until all disks report that
442 * they've completed all work */
443 mapPtr = raidPtr->reconControl[row]->reconMap;
444
445
446
447 while (reconDesc->numDisksDone < raidPtr->numCol - 1) {
448
449 event = rf_GetNextReconEvent(reconDesc, row, (void (*) (void *)) rf_ContinueReconstructFailedDisk, reconDesc);
450 RF_ASSERT(event);
451
452 if (ProcessReconEvent(raidPtr, row, event))
453 reconDesc->numDisksDone++;
454 raidPtr->reconControl[row]->percentComplete = 100 - (rf_UnitsLeftToReconstruct(mapPtr) * 100 / mapPtr->totalRUs);
455 if (rf_prReconSched) {
456 rf_PrintReconSchedule(raidPtr->reconControl[row]->reconMap, &(raidPtr->reconControl[row]->starttime));
457 }
458 }
459
460
461
462 reconDesc->state = 4;
463
464
465 case 4:
466 mapPtr = raidPtr->reconControl[row]->reconMap;
467 if (rf_reconDebug) {
468 printf("RECON: all reads completed\n");
469 }
470 /* at this point all the reads have completed. We now wait
471 * for any pending writes to complete, and then we're done */
472
473 while (rf_UnitsLeftToReconstruct(raidPtr->reconControl[row]->reconMap) > 0) {
474
475 event = rf_GetNextReconEvent(reconDesc, row, (void (*) (void *)) rf_ContinueReconstructFailedDisk, reconDesc);
476 RF_ASSERT(event);
477
478 (void) ProcessReconEvent(raidPtr, row, event); /* ignore return code */
479 raidPtr->reconControl[row]->percentComplete = 100 - (rf_UnitsLeftToReconstruct(mapPtr) * 100 / mapPtr->totalRUs);
480 if (rf_prReconSched) {
481 rf_PrintReconSchedule(raidPtr->reconControl[row]->reconMap, &(raidPtr->reconControl[row]->starttime));
482 }
483 }
484 reconDesc->state = 5;
485
486 case 5:
487 rf_stop_cpu_monitor();
488
489 /* Success: mark the dead disk as reconstructed. We quiesce
490 * the array here to assure no nasty interactions with pending
491 * user accesses when we free up the psstatus structure as
492 * part of FreeReconControl() */
493
494
495
496 reconDesc->state = 6;
497
498 retcode = rf_SuspendNewRequestsAndWait(raidPtr);
499 rf_StopUserStats(raidPtr);
500 rf_PrintUserStats(raidPtr); /* print out the stats on user
501 * accs accumulated during
502 * recon */
503
504 /* fall through to state 6 */
505 case 6:
506
507
508
509 RF_LOCK_MUTEX(raidPtr->mutex);
510 raidPtr->numFailures--;
511 ds = (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE);
512 raidPtr->Disks[row][col].status = (ds) ? rf_ds_dist_spared : rf_ds_spared;
513 raidPtr->status[row] = (ds) ? rf_rs_reconfigured : rf_rs_optimal;
514 RF_UNLOCK_MUTEX(raidPtr->mutex);
515 RF_GETTIME(etime);
516 RF_TIMEVAL_DIFF(&(raidPtr->reconControl[row]->starttime), &etime, &elpsd);
517
518 /* XXX -- why is state 7 different from state 6 if there is no
519 * return() here? -- XXX Note that I set elpsd above & use it
520 * below, so if you put a return here you'll have to fix this.
521 * (also, FreeReconControl is called below) */
522
523 case 7:
524
525 rf_ResumeNewRequests(raidPtr);
526
527 printf("Reconstruction of disk at row %d col %d completed and spare disk reassigned\n", row, col);
528 xor_s = raidPtr->accumXorTimeUs / 1000000;
529 xor_resid_us = raidPtr->accumXorTimeUs % 1000000;
530 printf("Recon time was %d.%06d seconds, accumulated XOR time was %ld us (%ld.%06ld)\n",
531 (int) elpsd.tv_sec, (int) elpsd.tv_usec, raidPtr->accumXorTimeUs, xor_s, xor_resid_us);
532 printf(" (start time %d sec %d usec, end time %d sec %d usec)\n",
533 (int) raidPtr->reconControl[row]->starttime.tv_sec,
534 (int) raidPtr->reconControl[row]->starttime.tv_usec,
535 (int) etime.tv_sec, (int) etime.tv_usec);
536 rf_print_cpu_util("reconstruction");
537 #if RF_RECON_STATS > 0
538 printf("Total head-sep stall count was %d\n",
539 (int) reconDesc->hsStallCount);
540 #endif /* RF_RECON_STATS > 0 */
541 rf_FreeReconControl(raidPtr, row);
542 RF_Free(raidPtr->recon_tracerecs, raidPtr->numCol * sizeof(RF_AccTraceEntry_t));
543 FreeReconDesc(reconDesc);
544
545 }
546
547 SignalReconDone(raidPtr);
548 return (0);
549 }
550 /*****************************************************************************************
551 * do the right thing upon each reconstruction event.
552 * returns nonzero if and only if there is nothing left unread on the indicated disk
553 ****************************************************************************************/
554 static int
555 ProcessReconEvent(raidPtr, frow, event)
556 RF_Raid_t *raidPtr;
557 RF_RowCol_t frow;
558 RF_ReconEvent_t *event;
559 {
560 int retcode = 0, submitblocked;
561 RF_ReconBuffer_t *rbuf;
562 RF_SectorCount_t sectorsPerRU;
563
564 Dprintf1("RECON: ProcessReconEvent type %d\n", event->type);
565 switch (event->type) {
566
567 /* a read I/O has completed */
568 case RF_REVENT_READDONE:
569 rbuf = raidPtr->reconControl[frow]->perDiskInfo[event->col].rbuf;
570 Dprintf3("RECON: READDONE EVENT: row %d col %d psid %ld\n",
571 frow, event->col, rbuf->parityStripeID);
572 Dprintf7("RECON: done read psid %ld buf %lx %02x %02x %02x %02x %02x\n",
573 rbuf->parityStripeID, rbuf->buffer, rbuf->buffer[0] & 0xff, rbuf->buffer[1] & 0xff,
574 rbuf->buffer[2] & 0xff, rbuf->buffer[3] & 0xff, rbuf->buffer[4] & 0xff);
575 rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
576 submitblocked = rf_SubmitReconBuffer(rbuf, 0, 0);
577 Dprintf1("RECON: submitblocked=%d\n", submitblocked);
578 if (!submitblocked)
579 retcode = IssueNextReadRequest(raidPtr, frow, event->col);
580 break;
581
582 /* a write I/O has completed */
583 case RF_REVENT_WRITEDONE:
584 if (rf_floatingRbufDebug) {
585 rf_CheckFloatingRbufCount(raidPtr, 1);
586 }
587 sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU;
588 rbuf = (RF_ReconBuffer_t *) event->arg;
589 rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
590 Dprintf3("RECON: WRITEDONE EVENT: psid %d ru %d (%d %% complete)\n",
591 rbuf->parityStripeID, rbuf->which_ru, raidPtr->reconControl[frow]->percentComplete);
592 rf_ReconMapUpdate(raidPtr, raidPtr->reconControl[frow]->reconMap,
593 rbuf->failedDiskSectorOffset, rbuf->failedDiskSectorOffset + sectorsPerRU - 1);
594 rf_RemoveFromActiveReconTable(raidPtr, frow, rbuf->parityStripeID, rbuf->which_ru);
595
596 if (rbuf->type == RF_RBUF_TYPE_FLOATING) {
597 RF_LOCK_MUTEX(raidPtr->reconControl[frow]->rb_mutex);
598 raidPtr->numFullReconBuffers--;
599 rf_ReleaseFloatingReconBuffer(raidPtr, frow, rbuf);
600 RF_UNLOCK_MUTEX(raidPtr->reconControl[frow]->rb_mutex);
601 } else
602 if (rbuf->type == RF_RBUF_TYPE_FORCED)
603 rf_FreeReconBuffer(rbuf);
604 else
605 RF_ASSERT(0);
606 break;
607
608 case RF_REVENT_BUFCLEAR: /* A buffer-stall condition has been
609 * cleared */
610 Dprintf2("RECON: BUFCLEAR EVENT: row %d col %d\n", frow, event->col);
611 submitblocked = rf_SubmitReconBuffer(raidPtr->reconControl[frow]->perDiskInfo[event->col].rbuf, 0, (int) (long) event->arg);
612 RF_ASSERT(!submitblocked); /* we wouldn't have gotten the
613 * BUFCLEAR event if we
614 * couldn't submit */
615 retcode = IssueNextReadRequest(raidPtr, frow, event->col);
616 break;
617
618 case RF_REVENT_BLOCKCLEAR: /* A user-write reconstruction
619 * blockage has been cleared */
620 DDprintf2("RECON: BLOCKCLEAR EVENT: row %d col %d\n", frow, event->col);
621 retcode = TryToRead(raidPtr, frow, event->col);
622 break;
623
624 case RF_REVENT_HEADSEPCLEAR: /* A max-head-separation
625 * reconstruction blockage has been
626 * cleared */
627 Dprintf2("RECON: HEADSEPCLEAR EVENT: row %d col %d\n", frow, event->col);
628 retcode = TryToRead(raidPtr, frow, event->col);
629 break;
630
631 /* a buffer has become ready to write */
632 case RF_REVENT_BUFREADY:
633 Dprintf2("RECON: BUFREADY EVENT: row %d col %d\n", frow, event->col);
634 retcode = IssueNextWriteRequest(raidPtr, frow);
635 if (rf_floatingRbufDebug) {
636 rf_CheckFloatingRbufCount(raidPtr, 1);
637 }
638 break;
639
640 /* we need to skip the current RU entirely because it got
641 * recon'd while we were waiting for something else to happen */
642 case RF_REVENT_SKIP:
643 DDprintf2("RECON: SKIP EVENT: row %d col %d\n", frow, event->col);
644 retcode = IssueNextReadRequest(raidPtr, frow, event->col);
645 break;
646
647 /* a forced-reconstruction read access has completed. Just
648 * submit the buffer */
649 case RF_REVENT_FORCEDREADDONE:
650 rbuf = (RF_ReconBuffer_t *) event->arg;
651 rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
652 DDprintf2("RECON: FORCEDREADDONE EVENT: row %d col %d\n", frow, event->col);
653 submitblocked = rf_SubmitReconBuffer(rbuf, 1, 0);
654 RF_ASSERT(!submitblocked);
655 break;
656
657 default:
658 RF_PANIC();
659 }
660 rf_FreeReconEventDesc(event);
661 return (retcode);
662 }
663 /*****************************************************************************************
664 *
665 * find the next thing that's needed on the indicated disk, and issue a read
666 * request for it. We assume that the reconstruction buffer associated with this
667 * process is free to receive the data. If reconstruction is blocked on the
668 * indicated RU, we issue a blockage-release request instead of a physical disk
669 * read request. If the current disk gets too far ahead of the others, we issue
670 * a head-separation wait request and return.
671 *
672 * ctrl->{ru_count, curPSID, diskOffset} and rbuf->failedDiskSectorOffset are
673 * maintained to point the the unit we're currently accessing. Note that this deviates
674 * from the standard C idiom of having counters point to the next thing to be
675 * accessed. This allows us to easily retry when we're blocked by head separation
676 * or reconstruction-blockage events.
677 *
678 * returns nonzero if and only if there is nothing left unread on the indicated disk
679 ****************************************************************************************/
680 static int
681 IssueNextReadRequest(raidPtr, row, col)
682 RF_Raid_t *raidPtr;
683 RF_RowCol_t row;
684 RF_RowCol_t col;
685 {
686 RF_PerDiskReconCtrl_t *ctrl = &raidPtr->reconControl[row]->perDiskInfo[col];
687 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
688 RF_ReconBuffer_t *rbuf = ctrl->rbuf;
689 RF_ReconUnitCount_t RUsPerPU = layoutPtr->SUsPerPU / layoutPtr->SUsPerRU;
690 RF_SectorCount_t sectorsPerRU = layoutPtr->sectorsPerStripeUnit * layoutPtr->SUsPerRU;
691 int do_new_check = 0, retcode = 0, status;
692
693 /* if we are currently the slowest disk, mark that we have to do a new
694 * check */
695 if (ctrl->headSepCounter <= raidPtr->reconControl[row]->minHeadSepCounter)
696 do_new_check = 1;
697
698 while (1) {
699
700 ctrl->ru_count++;
701 if (ctrl->ru_count < RUsPerPU) {
702 ctrl->diskOffset += sectorsPerRU;
703 rbuf->failedDiskSectorOffset += sectorsPerRU;
704 } else {
705 ctrl->curPSID++;
706 ctrl->ru_count = 0;
707 /* code left over from when head-sep was based on
708 * parity stripe id */
709 if (ctrl->curPSID >= raidPtr->reconControl[row]->lastPSID) {
710 CheckForNewMinHeadSep(raidPtr, row, ++(ctrl->headSepCounter));
711 return (1); /* finito! */
712 }
713 /* find the disk offsets of the start of the parity
714 * stripe on both the current disk and the failed
715 * disk. skip this entire parity stripe if either disk
716 * does not appear in the indicated PS */
717 status = ComputePSDiskOffsets(raidPtr, ctrl->curPSID, row, col, &ctrl->diskOffset, &rbuf->failedDiskSectorOffset,
718 &rbuf->spRow, &rbuf->spCol, &rbuf->spOffset);
719 if (status) {
720 ctrl->ru_count = RUsPerPU - 1;
721 continue;
722 }
723 }
724 rbuf->which_ru = ctrl->ru_count;
725
726 /* skip this RU if it's already been reconstructed */
727 if (rf_CheckRUReconstructed(raidPtr->reconControl[row]->reconMap, rbuf->failedDiskSectorOffset)) {
728 Dprintf2("Skipping psid %ld ru %d: already reconstructed\n", ctrl->curPSID, ctrl->ru_count);
729 continue;
730 }
731 break;
732 }
733 ctrl->headSepCounter++;
734 if (do_new_check)
735 CheckForNewMinHeadSep(raidPtr, row, ctrl->headSepCounter); /* update min if needed */
736
737
738 /* at this point, we have definitely decided what to do, and we have
739 * only to see if we can actually do it now */
740 rbuf->parityStripeID = ctrl->curPSID;
741 rbuf->which_ru = ctrl->ru_count;
742 bzero((char *) &raidPtr->recon_tracerecs[col], sizeof(raidPtr->recon_tracerecs[col]));
743 raidPtr->recon_tracerecs[col].reconacc = 1;
744 RF_ETIMER_START(raidPtr->recon_tracerecs[col].recon_timer);
745 retcode = TryToRead(raidPtr, row, col);
746 return (retcode);
747 }
748 /* tries to issue the next read on the indicated disk. We may be blocked by (a) the heads being too
749 * far apart, or (b) recon on the indicated RU being blocked due to a write by a user thread.
750 * In this case, we issue a head-sep or blockage wait request, which will cause this same routine
751 * to be invoked again later when the blockage has cleared.
752 */
753 static int
754 TryToRead(raidPtr, row, col)
755 RF_Raid_t *raidPtr;
756 RF_RowCol_t row;
757 RF_RowCol_t col;
758 {
759 RF_PerDiskReconCtrl_t *ctrl = &raidPtr->reconControl[row]->perDiskInfo[col];
760 RF_SectorCount_t sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU;
761 RF_StripeNum_t psid = ctrl->curPSID;
762 RF_ReconUnitNum_t which_ru = ctrl->ru_count;
763 RF_DiskQueueData_t *req;
764 int status, created = 0;
765 RF_ReconParityStripeStatus_t *pssPtr;
766
767 /* if the current disk is too far ahead of the others, issue a
768 * head-separation wait and return */
769 if (CheckHeadSeparation(raidPtr, ctrl, row, col, ctrl->headSepCounter, which_ru))
770 return (0);
771 RF_LOCK_PSS_MUTEX(raidPtr, row, psid);
772 pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl[row]->pssTable, psid, which_ru, RF_PSS_CREATE, &created);
773
774 /* if recon is blocked on the indicated parity stripe, issue a
775 * block-wait request and return. this also must mark the indicated RU
776 * in the stripe as under reconstruction if not blocked. */
777 status = CheckForcedOrBlockedReconstruction(raidPtr, pssPtr, ctrl, row, col, psid, which_ru);
778 if (status == RF_PSS_RECON_BLOCKED) {
779 Dprintf2("RECON: Stalling psid %ld ru %d: recon blocked\n", psid, which_ru);
780 goto out;
781 } else
782 if (status == RF_PSS_FORCED_ON_WRITE) {
783 rf_CauseReconEvent(raidPtr, row, col, NULL, RF_REVENT_SKIP);
784 goto out;
785 }
786 /* make one last check to be sure that the indicated RU didn't get
787 * reconstructed while we were waiting for something else to happen.
788 * This is unfortunate in that it causes us to make this check twice
789 * in the normal case. Might want to make some attempt to re-work
790 * this so that we only do this check if we've definitely blocked on
791 * one of the above checks. When this condition is detected, we may
792 * have just created a bogus status entry, which we need to delete. */
793 if (rf_CheckRUReconstructed(raidPtr->reconControl[row]->reconMap, ctrl->rbuf->failedDiskSectorOffset)) {
794 Dprintf2("RECON: Skipping psid %ld ru %d: prior recon after stall\n", psid, which_ru);
795 if (created)
796 rf_PSStatusDelete(raidPtr, raidPtr->reconControl[row]->pssTable, pssPtr);
797 rf_CauseReconEvent(raidPtr, row, col, NULL, RF_REVENT_SKIP);
798 goto out;
799 }
800 /* found something to read. issue the I/O */
801 Dprintf5("RECON: Read for psid %ld on row %d col %d offset %ld buf %lx\n",
802 psid, row, col, ctrl->diskOffset, ctrl->rbuf->buffer);
803 RF_ETIMER_STOP(raidPtr->recon_tracerecs[col].recon_timer);
804 RF_ETIMER_EVAL(raidPtr->recon_tracerecs[col].recon_timer);
805 raidPtr->recon_tracerecs[col].specific.recon.recon_start_to_fetch_us =
806 RF_ETIMER_VAL_US(raidPtr->recon_tracerecs[col].recon_timer);
807 RF_ETIMER_START(raidPtr->recon_tracerecs[col].recon_timer);
808
809 /* should be ok to use a NULL proc pointer here, all the bufs we use
810 * should be in kernel space */
811 req = rf_CreateDiskQueueData(RF_IO_TYPE_READ, ctrl->diskOffset, sectorsPerRU, ctrl->rbuf->buffer, psid, which_ru,
812 ReconReadDoneProc, (void *) ctrl, NULL, &raidPtr->recon_tracerecs[col], (void *) raidPtr, 0, NULL);
813
814 RF_ASSERT(req); /* XXX -- fix this -- XXX */
815
816 ctrl->rbuf->arg = (void *) req;
817 rf_DiskIOEnqueue(&raidPtr->Queues[row][col], req, RF_IO_RECON_PRIORITY);
818 pssPtr->issued[col] = 1;
819
820 out:
821 RF_UNLOCK_PSS_MUTEX(raidPtr, row, psid);
822 return (0);
823 }
824
825
826 /* given a parity stripe ID, we want to find out whether both the current disk and the
827 * failed disk exist in that parity stripe. If not, we want to skip this whole PS.
828 * If so, we want to find the disk offset of the start of the PS on both the current
829 * disk and the failed disk.
830 *
831 * this works by getting a list of disks comprising the indicated parity stripe, and
832 * searching the list for the current and failed disks. Once we've decided they both
833 * exist in the parity stripe, we need to decide whether each is data or parity,
834 * so that we'll know which mapping function to call to get the corresponding disk
835 * offsets.
836 *
837 * this is kind of unpleasant, but doing it this way allows the reconstruction code
838 * to use parity stripe IDs rather than physical disks address to march through the
839 * failed disk, which greatly simplifies a lot of code, as well as eliminating the
840 * need for a reverse-mapping function. I also think it will execute faster, since
841 * the calls to the mapping module are kept to a minimum.
842 *
843 * ASSUMES THAT THE STRIPE IDENTIFIER IDENTIFIES THE DISKS COMPRISING THE STRIPE
844 * IN THE CORRECT ORDER
845 */
846 static int
847 ComputePSDiskOffsets(
848 RF_Raid_t * raidPtr, /* raid descriptor */
849 RF_StripeNum_t psid, /* parity stripe identifier */
850 RF_RowCol_t row, /* row and column of disk to find the offsets
851 * for */
852 RF_RowCol_t col,
853 RF_SectorNum_t * outDiskOffset,
854 RF_SectorNum_t * outFailedDiskSectorOffset,
855 RF_RowCol_t * spRow, /* OUT: row,col of spare unit for failed unit */
856 RF_RowCol_t * spCol,
857 RF_SectorNum_t * spOffset)
858 { /* OUT: offset into disk containing spare unit */
859 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
860 RF_RowCol_t fcol = raidPtr->reconControl[row]->fcol;
861 RF_RaidAddr_t sosRaidAddress; /* start-of-stripe */
862 RF_RowCol_t *diskids;
863 u_int i, j, k, i_offset, j_offset;
864 RF_RowCol_t prow, pcol;
865 int testcol, testrow;
866 RF_RowCol_t stripe;
867 RF_SectorNum_t poffset;
868 char i_is_parity = 0, j_is_parity = 0;
869 RF_RowCol_t stripeWidth = layoutPtr->numDataCol + layoutPtr->numParityCol;
870
871 /* get a listing of the disks comprising that stripe */
872 sosRaidAddress = rf_ParityStripeIDToRaidAddress(layoutPtr, psid);
873 (layoutPtr->map->IdentifyStripe) (raidPtr, sosRaidAddress, &diskids, &stripe);
874 RF_ASSERT(diskids);
875
876 /* reject this entire parity stripe if it does not contain the
877 * indicated disk or it does not contain the failed disk */
878 if (row != stripe)
879 goto skipit;
880 for (i = 0; i < stripeWidth; i++) {
881 if (col == diskids[i])
882 break;
883 }
884 if (i == stripeWidth)
885 goto skipit;
886 for (j = 0; j < stripeWidth; j++) {
887 if (fcol == diskids[j])
888 break;
889 }
890 if (j == stripeWidth) {
891 goto skipit;
892 }
893 /* find out which disk the parity is on */
894 (layoutPtr->map->MapParity) (raidPtr, sosRaidAddress, &prow, &pcol, &poffset, RF_DONT_REMAP);
895
896 /* find out if either the current RU or the failed RU is parity */
897 /* also, if the parity occurs in this stripe prior to the data and/or
898 * failed col, we need to decrement i and/or j */
899 for (k = 0; k < stripeWidth; k++)
900 if (diskids[k] == pcol)
901 break;
902 RF_ASSERT(k < stripeWidth);
903 i_offset = i;
904 j_offset = j;
905 if (k < i)
906 i_offset--;
907 else
908 if (k == i) {
909 i_is_parity = 1;
910 i_offset = 0;
911 } /* set offsets to zero to disable multiply
912 * below */
913 if (k < j)
914 j_offset--;
915 else
916 if (k == j) {
917 j_is_parity = 1;
918 j_offset = 0;
919 }
920 /* at this point, [ij]_is_parity tells us whether the [current,failed]
921 * disk is parity at the start of this RU, and, if data, "[ij]_offset"
922 * tells us how far into the stripe the [current,failed] disk is. */
923
924 /* call the mapping routine to get the offset into the current disk,
925 * repeat for failed disk. */
926 if (i_is_parity)
927 layoutPtr->map->MapParity(raidPtr, sosRaidAddress + i_offset * layoutPtr->sectorsPerStripeUnit, &testrow, &testcol, outDiskOffset, RF_DONT_REMAP);
928 else
929 layoutPtr->map->MapSector(raidPtr, sosRaidAddress + i_offset * layoutPtr->sectorsPerStripeUnit, &testrow, &testcol, outDiskOffset, RF_DONT_REMAP);
930
931 RF_ASSERT(row == testrow && col == testcol);
932
933 if (j_is_parity)
934 layoutPtr->map->MapParity(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, &testrow, &testcol, outFailedDiskSectorOffset, RF_DONT_REMAP);
935 else
936 layoutPtr->map->MapSector(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, &testrow, &testcol, outFailedDiskSectorOffset, RF_DONT_REMAP);
937 RF_ASSERT(row == testrow && fcol == testcol);
938
939 /* now locate the spare unit for the failed unit */
940 if (layoutPtr->map->flags & RF_DISTRIBUTE_SPARE) {
941 if (j_is_parity)
942 layoutPtr->map->MapParity(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, spRow, spCol, spOffset, RF_REMAP);
943 else
944 layoutPtr->map->MapSector(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, spRow, spCol, spOffset, RF_REMAP);
945 } else {
946 *spRow = raidPtr->reconControl[row]->spareRow;
947 *spCol = raidPtr->reconControl[row]->spareCol;
948 *spOffset = *outFailedDiskSectorOffset;
949 }
950
951 return (0);
952
953 skipit:
954 Dprintf3("RECON: Skipping psid %ld: nothing needed from r%d c%d\n",
955 psid, row, col);
956 return (1);
957 }
958 /* this is called when a buffer has become ready to write to the replacement disk */
959 static int
960 IssueNextWriteRequest(raidPtr, row)
961 RF_Raid_t *raidPtr;
962 RF_RowCol_t row;
963 {
964 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
965 RF_SectorCount_t sectorsPerRU = layoutPtr->sectorsPerStripeUnit * layoutPtr->SUsPerRU;
966 RF_RowCol_t fcol = raidPtr->reconControl[row]->fcol;
967 RF_ReconBuffer_t *rbuf;
968 RF_DiskQueueData_t *req;
969
970 rbuf = rf_GetFullReconBuffer(raidPtr->reconControl[row]);
971 RF_ASSERT(rbuf); /* there must be one available, or we wouldn't
972 * have gotten the event that sent us here */
973 RF_ASSERT(rbuf->pssPtr);
974
975 rbuf->pssPtr->writeRbuf = rbuf;
976 rbuf->pssPtr = NULL;
977
978 Dprintf7("RECON: New write (r %d c %d offs %d) for psid %ld ru %d (failed disk offset %ld) buf %lx\n",
979 rbuf->spRow, rbuf->spCol, rbuf->spOffset, rbuf->parityStripeID,
980 rbuf->which_ru, rbuf->failedDiskSectorOffset, rbuf->buffer);
981 Dprintf6("RECON: new write psid %ld %02x %02x %02x %02x %02x\n",
982 rbuf->parityStripeID, rbuf->buffer[0] & 0xff, rbuf->buffer[1] & 0xff,
983 rbuf->buffer[2] & 0xff, rbuf->buffer[3] & 0xff, rbuf->buffer[4] & 0xff);
984
985 /* should be ok to use a NULL b_proc here b/c all addrs should be in
986 * kernel space */
987 req = rf_CreateDiskQueueData(RF_IO_TYPE_WRITE, rbuf->spOffset,
988 sectorsPerRU, rbuf->buffer,
989 rbuf->parityStripeID, rbuf->which_ru,
990 ReconWriteDoneProc, (void *) rbuf, NULL,
991 &raidPtr->recon_tracerecs[fcol],
992 (void *) raidPtr, 0, NULL);
993
994 RF_ASSERT(req); /* XXX -- fix this -- XXX */
995
996 rbuf->arg = (void *) req;
997 rf_DiskIOEnqueue(&raidPtr->Queues[rbuf->spRow][rbuf->spCol], req, RF_IO_RECON_PRIORITY);
998
999 return (0);
1000 }
1001 /* this gets called upon the completion of a reconstruction read operation
1002 * the arg is a pointer to the per-disk reconstruction control structure
1003 * for the process that just finished a read.
1004 *
1005 * called at interrupt context in the kernel, so don't do anything illegal here.
1006 */
1007 static int
1008 ReconReadDoneProc(arg, status)
1009 void *arg;
1010 int status;
1011 {
1012 RF_PerDiskReconCtrl_t *ctrl = (RF_PerDiskReconCtrl_t *) arg;
1013 RF_Raid_t *raidPtr = ctrl->reconCtrl->reconDesc->raidPtr;
1014
1015 if (status) {
1016 /*
1017 * XXX
1018 */
1019 printf("Recon read failed!\n");
1020 RF_PANIC();
1021 }
1022 RF_ETIMER_STOP(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
1023 RF_ETIMER_EVAL(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
1024 raidPtr->recon_tracerecs[ctrl->col].specific.recon.recon_fetch_to_return_us =
1025 RF_ETIMER_VAL_US(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
1026 RF_ETIMER_START(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
1027
1028 rf_CauseReconEvent(raidPtr, ctrl->row, ctrl->col, NULL, RF_REVENT_READDONE);
1029 return (0);
1030 }
1031 /* this gets called upon the completion of a reconstruction write operation.
1032 * the arg is a pointer to the rbuf that was just written
1033 *
1034 * called at interrupt context in the kernel, so don't do anything illegal here.
1035 */
1036 static int
1037 ReconWriteDoneProc(arg, status)
1038 void *arg;
1039 int status;
1040 {
1041 RF_ReconBuffer_t *rbuf = (RF_ReconBuffer_t *) arg;
1042
1043 Dprintf2("Reconstruction completed on psid %ld ru %d\n", rbuf->parityStripeID, rbuf->which_ru);
1044 if (status) {
1045 printf("Recon write failed!\n"); /* fprintf(stderr,"Recon
1046 * write failed!\n"); */
1047 RF_PANIC();
1048 }
1049 rf_CauseReconEvent((RF_Raid_t *) rbuf->raidPtr, rbuf->row, rbuf->col, arg, RF_REVENT_WRITEDONE);
1050 return (0);
1051 }
1052
1053
1054 /* computes a new minimum head sep, and wakes up anyone who needs to be woken as a result */
1055 static void
1056 CheckForNewMinHeadSep(raidPtr, row, hsCtr)
1057 RF_Raid_t *raidPtr;
1058 RF_RowCol_t row;
1059 RF_HeadSepLimit_t hsCtr;
1060 {
1061 RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl[row];
1062 RF_HeadSepLimit_t new_min;
1063 RF_RowCol_t i;
1064 RF_CallbackDesc_t *p;
1065 RF_ASSERT(hsCtr >= reconCtrlPtr->minHeadSepCounter); /* from the definition
1066 * of a minimum */
1067
1068
1069 RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
1070
1071 new_min = ~(1L << (8 * sizeof(long) - 1)); /* 0x7FFF....FFF */
1072 for (i = 0; i < raidPtr->numCol; i++)
1073 if (i != reconCtrlPtr->fcol) {
1074 if (reconCtrlPtr->perDiskInfo[i].headSepCounter < new_min)
1075 new_min = reconCtrlPtr->perDiskInfo[i].headSepCounter;
1076 }
1077 /* set the new minimum and wake up anyone who can now run again */
1078 if (new_min != reconCtrlPtr->minHeadSepCounter) {
1079 reconCtrlPtr->minHeadSepCounter = new_min;
1080 Dprintf1("RECON: new min head pos counter val is %ld\n", new_min);
1081 while (reconCtrlPtr->headSepCBList) {
1082 if (reconCtrlPtr->headSepCBList->callbackArg.v > new_min)
1083 break;
1084 p = reconCtrlPtr->headSepCBList;
1085 reconCtrlPtr->headSepCBList = p->next;
1086 p->next = NULL;
1087 rf_CauseReconEvent(raidPtr, p->row, p->col, NULL, RF_REVENT_HEADSEPCLEAR);
1088 rf_FreeCallbackDesc(p);
1089 }
1090
1091 }
1092 RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
1093 }
1094 /* checks to see that the maximum head separation will not be violated
1095 * if we initiate a reconstruction I/O on the indicated disk. Limiting the
1096 * maximum head separation between two disks eliminates the nasty buffer-stall
1097 * conditions that occur when one disk races ahead of the others and consumes
1098 * all of the floating recon buffers. This code is complex and unpleasant
1099 * but it's necessary to avoid some very nasty, albeit fairly rare,
1100 * reconstruction behavior.
1101 *
1102 * returns non-zero if and only if we have to stop working on the indicated disk
1103 * due to a head-separation delay.
1104 */
1105 static int
1106 CheckHeadSeparation(
1107 RF_Raid_t * raidPtr,
1108 RF_PerDiskReconCtrl_t * ctrl,
1109 RF_RowCol_t row,
1110 RF_RowCol_t col,
1111 RF_HeadSepLimit_t hsCtr,
1112 RF_ReconUnitNum_t which_ru)
1113 {
1114 RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl[row];
1115 RF_CallbackDesc_t *cb, *p, *pt;
1116 int retval = 0, tid;
1117
1118 /* if we're too far ahead of the slowest disk, stop working on this
1119 * disk until the slower ones catch up. We do this by scheduling a
1120 * wakeup callback for the time when the slowest disk has caught up.
1121 * We define "caught up" with 20% hysteresis, i.e. the head separation
1122 * must have fallen to at most 80% of the max allowable head
1123 * separation before we'll wake up.
1124 *
1125 */
1126 rf_get_threadid(tid);
1127 RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
1128 if ((raidPtr->headSepLimit >= 0) &&
1129 ((ctrl->headSepCounter - reconCtrlPtr->minHeadSepCounter) > raidPtr->headSepLimit)) {
1130 Dprintf6("[%d] RECON: head sep stall: row %d col %d hsCtr %ld minHSCtr %ld limit %ld\n",
1131 tid, row, col, ctrl->headSepCounter, reconCtrlPtr->minHeadSepCounter, raidPtr->headSepLimit);
1132 cb = rf_AllocCallbackDesc();
1133 /* the minHeadSepCounter value we have to get to before we'll
1134 * wake up. build in 20% hysteresis. */
1135 cb->callbackArg.v = (ctrl->headSepCounter - raidPtr->headSepLimit + raidPtr->headSepLimit / 5);
1136 cb->row = row;
1137 cb->col = col;
1138 cb->next = NULL;
1139
1140 /* insert this callback descriptor into the sorted list of
1141 * pending head-sep callbacks */
1142 p = reconCtrlPtr->headSepCBList;
1143 if (!p)
1144 reconCtrlPtr->headSepCBList = cb;
1145 else
1146 if (cb->callbackArg.v < p->callbackArg.v) {
1147 cb->next = reconCtrlPtr->headSepCBList;
1148 reconCtrlPtr->headSepCBList = cb;
1149 } else {
1150 for (pt = p, p = p->next; p && (p->callbackArg.v < cb->callbackArg.v); pt = p, p = p->next);
1151 cb->next = p;
1152 pt->next = cb;
1153 }
1154 retval = 1;
1155 #if RF_RECON_STATS > 0
1156 ctrl->reconCtrl->reconDesc->hsStallCount++;
1157 #endif /* RF_RECON_STATS > 0 */
1158 }
1159 RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
1160
1161 return (retval);
1162 }
1163 /* checks to see if reconstruction has been either forced or blocked by a user operation.
1164 * if forced, we skip this RU entirely.
1165 * else if blocked, put ourselves on the wait list.
1166 * else return 0.
1167 *
1168 * ASSUMES THE PSS MUTEX IS LOCKED UPON ENTRY
1169 */
1170 static int
1171 CheckForcedOrBlockedReconstruction(
1172 RF_Raid_t * raidPtr,
1173 RF_ReconParityStripeStatus_t * pssPtr,
1174 RF_PerDiskReconCtrl_t * ctrl,
1175 RF_RowCol_t row,
1176 RF_RowCol_t col,
1177 RF_StripeNum_t psid,
1178 RF_ReconUnitNum_t which_ru)
1179 {
1180 RF_CallbackDesc_t *cb;
1181 int retcode = 0;
1182
1183 if ((pssPtr->flags & RF_PSS_FORCED_ON_READ) || (pssPtr->flags & RF_PSS_FORCED_ON_WRITE))
1184 retcode = RF_PSS_FORCED_ON_WRITE;
1185 else
1186 if (pssPtr->flags & RF_PSS_RECON_BLOCKED) {
1187 Dprintf4("RECON: row %d col %d blocked at psid %ld ru %d\n", row, col, psid, which_ru);
1188 cb = rf_AllocCallbackDesc(); /* append ourselves to
1189 * the blockage-wait
1190 * list */
1191 cb->row = row;
1192 cb->col = col;
1193 cb->next = pssPtr->blockWaitList;
1194 pssPtr->blockWaitList = cb;
1195 retcode = RF_PSS_RECON_BLOCKED;
1196 }
1197 if (!retcode)
1198 pssPtr->flags |= RF_PSS_UNDER_RECON; /* mark this RU as under
1199 * reconstruction */
1200
1201 return (retcode);
1202 }
1203 /* if reconstruction is currently ongoing for the indicated stripeID, reconstruction
1204 * is forced to completion and we return non-zero to indicate that the caller must
1205 * wait. If not, then reconstruction is blocked on the indicated stripe and the
1206 * routine returns zero. If and only if we return non-zero, we'll cause the cbFunc
1207 * to get invoked with the cbArg when the reconstruction has completed.
1208 */
1209 int
1210 rf_ForceOrBlockRecon(raidPtr, asmap, cbFunc, cbArg)
1211 RF_Raid_t *raidPtr;
1212 RF_AccessStripeMap_t *asmap;
1213 void (*cbFunc) (RF_Raid_t *, void *);
1214 void *cbArg;
1215 {
1216 RF_RowCol_t row = asmap->physInfo->row; /* which row of the array
1217 * we're working on */
1218 RF_StripeNum_t stripeID = asmap->stripeID; /* the stripe ID we're
1219 * forcing recon on */
1220 RF_SectorCount_t sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU; /* num sects in one RU */
1221 RF_ReconParityStripeStatus_t *pssPtr; /* a pointer to the parity
1222 * stripe status structure */
1223 RF_StripeNum_t psid; /* parity stripe id */
1224 RF_SectorNum_t offset, fd_offset; /* disk offset, failed-disk
1225 * offset */
1226 RF_RowCol_t *diskids;
1227 RF_RowCol_t stripe;
1228 int tid;
1229 RF_ReconUnitNum_t which_ru; /* RU within parity stripe */
1230 RF_RowCol_t fcol, diskno, i;
1231 RF_ReconBuffer_t *new_rbuf; /* ptr to newly allocated rbufs */
1232 RF_DiskQueueData_t *req;/* disk I/O req to be enqueued */
1233 RF_CallbackDesc_t *cb;
1234 int created = 0, nPromoted;
1235
1236 rf_get_threadid(tid);
1237 psid = rf_MapStripeIDToParityStripeID(&raidPtr->Layout, stripeID, &which_ru);
1238
1239 RF_LOCK_PSS_MUTEX(raidPtr, row, psid);
1240
1241 pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl[row]->pssTable, psid, which_ru, RF_PSS_CREATE | RF_PSS_RECON_BLOCKED, &created);
1242
1243 /* if recon is not ongoing on this PS, just return */
1244 if (!(pssPtr->flags & RF_PSS_UNDER_RECON)) {
1245 RF_UNLOCK_PSS_MUTEX(raidPtr, row, psid);
1246 return (0);
1247 }
1248 /* otherwise, we have to wait for reconstruction to complete on this
1249 * RU. */
1250 /* In order to avoid waiting for a potentially large number of
1251 * low-priority accesses to complete, we force a normal-priority (i.e.
1252 * not low-priority) reconstruction on this RU. */
1253 if (!(pssPtr->flags & RF_PSS_FORCED_ON_WRITE) && !(pssPtr->flags & RF_PSS_FORCED_ON_READ)) {
1254 DDprintf1("Forcing recon on psid %ld\n", psid);
1255 pssPtr->flags |= RF_PSS_FORCED_ON_WRITE; /* mark this RU as under
1256 * forced recon */
1257 pssPtr->flags &= ~RF_PSS_RECON_BLOCKED; /* clear the blockage
1258 * that we just set */
1259 fcol = raidPtr->reconControl[row]->fcol;
1260
1261 /* get a listing of the disks comprising the indicated stripe */
1262 (raidPtr->Layout.map->IdentifyStripe) (raidPtr, asmap->raidAddress, &diskids, &stripe);
1263 RF_ASSERT(row == stripe);
1264
1265 /* For previously issued reads, elevate them to normal
1266 * priority. If the I/O has already completed, it won't be
1267 * found in the queue, and hence this will be a no-op. For
1268 * unissued reads, allocate buffers and issue new reads. The
1269 * fact that we've set the FORCED bit means that the regular
1270 * recon procs will not re-issue these reqs */
1271 for (i = 0; i < raidPtr->Layout.numDataCol + raidPtr->Layout.numParityCol; i++)
1272 if ((diskno = diskids[i]) != fcol) {
1273 if (pssPtr->issued[diskno]) {
1274 nPromoted = rf_DiskIOPromote(&raidPtr->Queues[row][diskno], psid, which_ru);
1275 if (rf_reconDebug && nPromoted)
1276 printf("[%d] promoted read from row %d col %d\n", tid, row, diskno);
1277 } else {
1278 new_rbuf = rf_MakeReconBuffer(raidPtr, row, diskno, RF_RBUF_TYPE_FORCED); /* create new buf */
1279 ComputePSDiskOffsets(raidPtr, psid, row, diskno, &offset, &fd_offset,
1280 &new_rbuf->spRow, &new_rbuf->spCol, &new_rbuf->spOffset); /* find offsets & spare
1281 * location */
1282 new_rbuf->parityStripeID = psid; /* fill in the buffer */
1283 new_rbuf->which_ru = which_ru;
1284 new_rbuf->failedDiskSectorOffset = fd_offset;
1285 new_rbuf->priority = RF_IO_NORMAL_PRIORITY;
1286
1287 /* use NULL b_proc b/c all addrs
1288 * should be in kernel space */
1289 req = rf_CreateDiskQueueData(RF_IO_TYPE_READ, offset + which_ru * sectorsPerRU, sectorsPerRU, new_rbuf->buffer,
1290 psid, which_ru, (int (*) (void *, int)) ForceReconReadDoneProc, (void *) new_rbuf, NULL,
1291 NULL, (void *) raidPtr, 0, NULL);
1292
1293 RF_ASSERT(req); /* XXX -- fix this --
1294 * XXX */
1295
1296 new_rbuf->arg = req;
1297 rf_DiskIOEnqueue(&raidPtr->Queues[row][diskno], req, RF_IO_NORMAL_PRIORITY); /* enqueue the I/O */
1298 Dprintf3("[%d] Issued new read req on row %d col %d\n", tid, row, diskno);
1299 }
1300 }
1301 /* if the write is sitting in the disk queue, elevate its
1302 * priority */
1303 if (rf_DiskIOPromote(&raidPtr->Queues[row][fcol], psid, which_ru))
1304 printf("[%d] promoted write to row %d col %d\n", tid, row, fcol);
1305 }
1306 /* install a callback descriptor to be invoked when recon completes on
1307 * this parity stripe. */
1308 cb = rf_AllocCallbackDesc();
1309 /* XXX the following is bogus.. These functions don't really match!!
1310 * GO */
1311 cb->callbackFunc = (void (*) (RF_CBParam_t)) cbFunc;
1312 cb->callbackArg.p = (void *) cbArg;
1313 cb->next = pssPtr->procWaitList;
1314 pssPtr->procWaitList = cb;
1315 DDprintf2("[%d] Waiting for forced recon on psid %ld\n", tid, psid);
1316
1317 RF_UNLOCK_PSS_MUTEX(raidPtr, row, psid);
1318 return (1);
1319 }
1320 /* called upon the completion of a forced reconstruction read.
1321 * all we do is schedule the FORCEDREADONE event.
1322 * called at interrupt context in the kernel, so don't do anything illegal here.
1323 */
1324 static void
1325 ForceReconReadDoneProc(arg, status)
1326 void *arg;
1327 int status;
1328 {
1329 RF_ReconBuffer_t *rbuf = arg;
1330
1331 if (status) {
1332 printf("Forced recon read failed!\n"); /* fprintf(stderr,"Forced
1333 * recon read
1334 * failed!\n"); */
1335 RF_PANIC();
1336 }
1337 rf_CauseReconEvent((RF_Raid_t *) rbuf->raidPtr, rbuf->row, rbuf->col, (void *) rbuf, RF_REVENT_FORCEDREADDONE);
1338 }
1339 /* releases a block on the reconstruction of the indicated stripe */
1340 int
1341 rf_UnblockRecon(raidPtr, asmap)
1342 RF_Raid_t *raidPtr;
1343 RF_AccessStripeMap_t *asmap;
1344 {
1345 RF_RowCol_t row = asmap->origRow;
1346 RF_StripeNum_t stripeID = asmap->stripeID;
1347 RF_ReconParityStripeStatus_t *pssPtr;
1348 RF_ReconUnitNum_t which_ru;
1349 RF_StripeNum_t psid;
1350 int tid, created = 0;
1351 RF_CallbackDesc_t *cb;
1352
1353 rf_get_threadid(tid);
1354 psid = rf_MapStripeIDToParityStripeID(&raidPtr->Layout, stripeID, &which_ru);
1355 RF_LOCK_PSS_MUTEX(raidPtr, row, psid);
1356 pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl[row]->pssTable, psid, which_ru, RF_PSS_NONE, &created);
1357
1358 /* When recon is forced, the pss desc can get deleted before we get
1359 * back to unblock recon. But, this can _only_ happen when recon is
1360 * forced. It would be good to put some kind of sanity check here, but
1361 * how to decide if recon was just forced or not? */
1362 if (!pssPtr) {
1363 /* printf("Warning: no pss descriptor upon unblock on psid %ld
1364 * RU %d\n",psid,which_ru); */
1365 if (rf_reconDebug || rf_pssDebug)
1366 printf("Warning: no pss descriptor upon unblock on psid %ld RU %d\n", (long) psid, which_ru);
1367 goto out;
1368 }
1369 pssPtr->blockCount--;
1370 Dprintf3("[%d] unblocking recon on psid %ld: blockcount is %d\n", tid, psid, pssPtr->blockCount);
1371 if (pssPtr->blockCount == 0) { /* if recon blockage has been released */
1372
1373 /* unblock recon before calling CauseReconEvent in case
1374 * CauseReconEvent causes us to try to issue a new read before
1375 * returning here. */
1376 pssPtr->flags &= ~RF_PSS_RECON_BLOCKED;
1377
1378
1379 while (pssPtr->blockWaitList) { /* spin through the block-wait
1380 * list and release all the
1381 * waiters */
1382 cb = pssPtr->blockWaitList;
1383 pssPtr->blockWaitList = cb->next;
1384 cb->next = NULL;
1385 rf_CauseReconEvent(raidPtr, cb->row, cb->col, NULL, RF_REVENT_BLOCKCLEAR);
1386 rf_FreeCallbackDesc(cb);
1387 }
1388 if (!(pssPtr->flags & RF_PSS_UNDER_RECON)) { /* if no recon was
1389 * requested while recon
1390 * was blocked */
1391 rf_PSStatusDelete(raidPtr, raidPtr->reconControl[row]->pssTable, pssPtr);
1392 }
1393 }
1394 out:
1395 RF_UNLOCK_PSS_MUTEX(raidPtr, row, psid);
1396 return (0);
1397 }
1398