rf_reconstruct.c revision 1.95.2.4 1 /* $NetBSD: rf_reconstruct.c,v 1.95.2.4 2008/12/27 19:32:58 bouyer 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 <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: rf_reconstruct.c,v 1.95.2.4 2008/12/27 19:32:58 bouyer Exp $");
37
38 #include <sys/time.h>
39 #include <sys/buf.h>
40 #include <sys/errno.h>
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/ioctl.h>
46 #include <sys/fcntl.h>
47 #include <sys/vnode.h>
48 #include <dev/raidframe/raidframevar.h>
49
50 #include "rf_raid.h"
51 #include "rf_reconutil.h"
52 #include "rf_revent.h"
53 #include "rf_reconbuffer.h"
54 #include "rf_acctrace.h"
55 #include "rf_etimer.h"
56 #include "rf_dag.h"
57 #include "rf_desc.h"
58 #include "rf_debugprint.h"
59 #include "rf_general.h"
60 #include "rf_driver.h"
61 #include "rf_utils.h"
62 #include "rf_shutdown.h"
63
64 #include "rf_kintf.h"
65
66 /* setting these to -1 causes them to be set to their default values if not set by debug options */
67
68 #if RF_DEBUG_RECON
69 #define Dprintf(s) if (rf_reconDebug) rf_debug_printf(s,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)
70 #define Dprintf1(s,a) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),NULL,NULL,NULL,NULL,NULL,NULL,NULL)
71 #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)
72 #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)
73 #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)
74 #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)
75 #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)
76 #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)
77
78 #define DDprintf1(s,a) if (rf_reconDebug) rf_debug_printf(s,(void *)((unsigned long)a),NULL,NULL,NULL,NULL,NULL,NULL,NULL)
79 #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)
80
81 #else /* RF_DEBUG_RECON */
82
83 #define Dprintf(s) {}
84 #define Dprintf1(s,a) {}
85 #define Dprintf2(s,a,b) {}
86 #define Dprintf3(s,a,b,c) {}
87 #define Dprintf4(s,a,b,c,d) {}
88 #define Dprintf5(s,a,b,c,d,e) {}
89 #define Dprintf6(s,a,b,c,d,e,f) {}
90 #define Dprintf7(s,a,b,c,d,e,f,g) {}
91
92 #define DDprintf1(s,a) {}
93 #define DDprintf2(s,a,b) {}
94
95 #endif /* RF_DEBUG_RECON */
96
97 #define RF_RECON_DONE_READS 1
98 #define RF_RECON_READ_ERROR 2
99 #define RF_RECON_WRITE_ERROR 3
100 #define RF_RECON_READ_STOPPED 4
101 #define RF_RECON_WRITE_DONE 5
102
103 #define RF_MAX_FREE_RECONBUFFER 32
104 #define RF_MIN_FREE_RECONBUFFER 16
105
106 static RF_RaidReconDesc_t *AllocRaidReconDesc(RF_Raid_t *, RF_RowCol_t,
107 RF_RaidDisk_t *, int, RF_RowCol_t);
108 static void FreeReconDesc(RF_RaidReconDesc_t *);
109 static int ProcessReconEvent(RF_Raid_t *, RF_ReconEvent_t *);
110 static int IssueNextReadRequest(RF_Raid_t *, RF_RowCol_t);
111 static int TryToRead(RF_Raid_t *, RF_RowCol_t);
112 static int ComputePSDiskOffsets(RF_Raid_t *, RF_StripeNum_t, RF_RowCol_t,
113 RF_SectorNum_t *, RF_SectorNum_t *, RF_RowCol_t *,
114 RF_SectorNum_t *);
115 static int IssueNextWriteRequest(RF_Raid_t *);
116 static int ReconReadDoneProc(void *, int);
117 static int ReconWriteDoneProc(void *, int);
118 static void CheckForNewMinHeadSep(RF_Raid_t *, RF_HeadSepLimit_t);
119 static int CheckHeadSeparation(RF_Raid_t *, RF_PerDiskReconCtrl_t *,
120 RF_RowCol_t, RF_HeadSepLimit_t,
121 RF_ReconUnitNum_t);
122 static int CheckForcedOrBlockedReconstruction(RF_Raid_t *,
123 RF_ReconParityStripeStatus_t *,
124 RF_PerDiskReconCtrl_t *,
125 RF_RowCol_t, RF_StripeNum_t,
126 RF_ReconUnitNum_t);
127 static void ForceReconReadDoneProc(void *, int);
128 static void rf_ShutdownReconstruction(void *);
129
130 struct RF_ReconDoneProc_s {
131 void (*proc) (RF_Raid_t *, void *);
132 void *arg;
133 RF_ReconDoneProc_t *next;
134 };
135
136 /**************************************************************************
137 *
138 * sets up the parameters that will be used by the reconstruction process
139 * currently there are none, except for those that the layout-specific
140 * configuration (e.g. rf_ConfigureDeclustered) routine sets up.
141 *
142 * in the kernel, we fire off the recon thread.
143 *
144 **************************************************************************/
145 static void
146 rf_ShutdownReconstruction(void *ignored)
147 {
148 pool_destroy(&rf_pools.reconbuffer);
149 }
150
151 int
152 rf_ConfigureReconstruction(RF_ShutdownList_t **listp)
153 {
154
155 rf_pool_init(&rf_pools.reconbuffer, sizeof(RF_ReconBuffer_t),
156 "rf_reconbuffer_pl", RF_MIN_FREE_RECONBUFFER, RF_MAX_FREE_RECONBUFFER);
157 rf_ShutdownCreate(listp, rf_ShutdownReconstruction, NULL);
158
159 return (0);
160 }
161
162 static RF_RaidReconDesc_t *
163 AllocRaidReconDesc(RF_Raid_t *raidPtr, RF_RowCol_t col,
164 RF_RaidDisk_t *spareDiskPtr, int numDisksDone,
165 RF_RowCol_t scol)
166 {
167
168 RF_RaidReconDesc_t *reconDesc;
169
170 RF_Malloc(reconDesc, sizeof(RF_RaidReconDesc_t),
171 (RF_RaidReconDesc_t *));
172 reconDesc->raidPtr = raidPtr;
173 reconDesc->col = col;
174 reconDesc->spareDiskPtr = spareDiskPtr;
175 reconDesc->numDisksDone = numDisksDone;
176 reconDesc->scol = scol;
177 reconDesc->next = NULL;
178
179 return (reconDesc);
180 }
181
182 static void
183 FreeReconDesc(RF_RaidReconDesc_t *reconDesc)
184 {
185 #if RF_RECON_STATS > 0
186 printf("raid%d: %lu recon event waits, %lu recon delays\n",
187 reconDesc->raidPtr->raidid,
188 (long) reconDesc->numReconEventWaits,
189 (long) reconDesc->numReconExecDelays);
190 #endif /* RF_RECON_STATS > 0 */
191 printf("raid%d: %lu max exec ticks\n",
192 reconDesc->raidPtr->raidid,
193 (long) reconDesc->maxReconExecTicks);
194 #if (RF_RECON_STATS > 0) || defined(KERNEL)
195 printf("\n");
196 #endif /* (RF_RECON_STATS > 0) || KERNEL */
197 RF_Free(reconDesc, sizeof(RF_RaidReconDesc_t));
198 }
199
200
201 /*****************************************************************************
202 *
203 * primary routine to reconstruct a failed disk. This should be called from
204 * within its own thread. It won't return until reconstruction completes,
205 * fails, or is aborted.
206 *****************************************************************************/
207 int
208 rf_ReconstructFailedDisk(RF_Raid_t *raidPtr, RF_RowCol_t col)
209 {
210 const RF_LayoutSW_t *lp;
211 int rc;
212
213 lp = raidPtr->Layout.map;
214 if (lp->SubmitReconBuffer) {
215 /*
216 * The current infrastructure only supports reconstructing one
217 * disk at a time for each array.
218 */
219 RF_LOCK_MUTEX(raidPtr->mutex);
220 while (raidPtr->reconInProgress) {
221 RF_WAIT_COND(raidPtr->waitForReconCond, raidPtr->mutex);
222 }
223 raidPtr->reconInProgress++;
224 RF_UNLOCK_MUTEX(raidPtr->mutex);
225 rc = rf_ReconstructFailedDiskBasic(raidPtr, col);
226 RF_LOCK_MUTEX(raidPtr->mutex);
227 raidPtr->reconInProgress--;
228 RF_UNLOCK_MUTEX(raidPtr->mutex);
229 } else {
230 RF_ERRORMSG1("RECON: no way to reconstruct failed disk for arch %c\n",
231 lp->parityConfig);
232 rc = EIO;
233 }
234 RF_SIGNAL_COND(raidPtr->waitForReconCond);
235 return (rc);
236 }
237
238 int
239 rf_ReconstructFailedDiskBasic(RF_Raid_t *raidPtr, RF_RowCol_t col)
240 {
241 RF_ComponentLabel_t c_label;
242 RF_RaidDisk_t *spareDiskPtr = NULL;
243 RF_RaidReconDesc_t *reconDesc;
244 RF_RowCol_t scol;
245 int numDisksDone = 0, rc;
246
247 /* first look for a spare drive onto which to reconstruct the data */
248 /* spare disk descriptors are stored in row 0. This may have to
249 * change eventually */
250
251 RF_LOCK_MUTEX(raidPtr->mutex);
252 RF_ASSERT(raidPtr->Disks[col].status == rf_ds_failed);
253 #if RF_INCLUDE_PARITY_DECLUSTERING_DS > 0
254 if (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE) {
255 if (raidPtr->status != rf_rs_degraded) {
256 RF_ERRORMSG1("Unable to reconstruct disk at col %d because status not degraded\n", col);
257 RF_UNLOCK_MUTEX(raidPtr->mutex);
258 return (EINVAL);
259 }
260 scol = (-1);
261 } else {
262 #endif
263 for (scol = raidPtr->numCol; scol < raidPtr->numCol + raidPtr->numSpare; scol++) {
264 if (raidPtr->Disks[scol].status == rf_ds_spare) {
265 spareDiskPtr = &raidPtr->Disks[scol];
266 spareDiskPtr->status = rf_ds_used_spare;
267 break;
268 }
269 }
270 if (!spareDiskPtr) {
271 RF_ERRORMSG1("Unable to reconstruct disk at col %d because no spares are available\n", col);
272 RF_UNLOCK_MUTEX(raidPtr->mutex);
273 return (ENOSPC);
274 }
275 printf("RECON: initiating reconstruction on col %d -> spare at col %d\n", col, scol);
276 #if RF_INCLUDE_PARITY_DECLUSTERING_DS > 0
277 }
278 #endif
279 RF_UNLOCK_MUTEX(raidPtr->mutex);
280
281 reconDesc = AllocRaidReconDesc((void *) raidPtr, col, spareDiskPtr, numDisksDone, scol);
282 raidPtr->reconDesc = (void *) reconDesc;
283 #if RF_RECON_STATS > 0
284 reconDesc->hsStallCount = 0;
285 reconDesc->numReconExecDelays = 0;
286 reconDesc->numReconEventWaits = 0;
287 #endif /* RF_RECON_STATS > 0 */
288 reconDesc->reconExecTimerRunning = 0;
289 reconDesc->reconExecTicks = 0;
290 reconDesc->maxReconExecTicks = 0;
291 rc = rf_ContinueReconstructFailedDisk(reconDesc);
292
293 if (!rc) {
294 /* fix up the component label */
295 /* Don't actually need the read here.. */
296 raidread_component_label(
297 raidPtr->raid_cinfo[scol].ci_dev,
298 raidPtr->raid_cinfo[scol].ci_vp,
299 &c_label);
300
301 raid_init_component_label( raidPtr, &c_label);
302 c_label.row = 0;
303 c_label.column = col;
304 c_label.clean = RF_RAID_DIRTY;
305 c_label.status = rf_ds_optimal;
306 c_label.partitionSize = raidPtr->Disks[scol].partitionSize;
307
308 /* We've just done a rebuild based on all the other
309 disks, so at this point the parity is known to be
310 clean, even if it wasn't before. */
311
312 /* XXX doesn't hold for RAID 6!!*/
313
314 RF_LOCK_MUTEX(raidPtr->mutex);
315 raidPtr->parity_good = RF_RAID_CLEAN;
316 RF_UNLOCK_MUTEX(raidPtr->mutex);
317
318 /* XXXX MORE NEEDED HERE */
319
320 raidwrite_component_label(
321 raidPtr->raid_cinfo[scol].ci_dev,
322 raidPtr->raid_cinfo[scol].ci_vp,
323 &c_label);
324
325 } else {
326 /* Reconstruct failed. */
327
328 RF_LOCK_MUTEX(raidPtr->mutex);
329 /* Failed disk goes back to "failed" status */
330 raidPtr->Disks[col].status = rf_ds_failed;
331
332 /* Spare disk goes back to "spare" status. */
333 spareDiskPtr->status = rf_ds_spare;
334 RF_UNLOCK_MUTEX(raidPtr->mutex);
335
336 }
337 rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
338 return (rc);
339 }
340
341 /*
342
343 Allow reconstructing a disk in-place -- i.e. component /dev/sd2e goes AWOL,
344 and you don't get a spare until the next Monday. With this function
345 (and hot-swappable drives) you can now put your new disk containing
346 /dev/sd2e on the bus, scsictl it alive, and then use raidctl(8) to
347 rebuild the data "on the spot".
348
349 */
350
351 int
352 rf_ReconstructInPlace(RF_Raid_t *raidPtr, RF_RowCol_t col)
353 {
354 RF_RaidDisk_t *spareDiskPtr = NULL;
355 RF_RaidReconDesc_t *reconDesc;
356 const RF_LayoutSW_t *lp;
357 RF_ComponentLabel_t c_label;
358 int numDisksDone = 0, rc;
359 struct partinfo dpart;
360 struct vnode *vp;
361 struct vattr va;
362 struct lwp *lwp;
363 int retcode;
364 int ac;
365
366 lp = raidPtr->Layout.map;
367 if (!lp->SubmitReconBuffer) {
368 RF_ERRORMSG1("RECON: no way to reconstruct failed disk for arch %c\n",
369 lp->parityConfig);
370 /* wakeup anyone who might be waiting to do a reconstruct */
371 RF_SIGNAL_COND(raidPtr->waitForReconCond);
372 return(EIO);
373 }
374
375 /*
376 * The current infrastructure only supports reconstructing one
377 * disk at a time for each array.
378 */
379 RF_LOCK_MUTEX(raidPtr->mutex);
380
381 if (raidPtr->Disks[col].status != rf_ds_failed) {
382 /* "It's gone..." */
383 raidPtr->numFailures++;
384 raidPtr->Disks[col].status = rf_ds_failed;
385 raidPtr->status = rf_rs_degraded;
386 RF_UNLOCK_MUTEX(raidPtr->mutex);
387 rf_update_component_labels(raidPtr,
388 RF_NORMAL_COMPONENT_UPDATE);
389 RF_LOCK_MUTEX(raidPtr->mutex);
390 }
391
392 while (raidPtr->reconInProgress) {
393 RF_WAIT_COND(raidPtr->waitForReconCond, raidPtr->mutex);
394 }
395
396 raidPtr->reconInProgress++;
397
398 /* first look for a spare drive onto which to reconstruct the
399 data. spare disk descriptors are stored in row 0. This
400 may have to change eventually */
401
402 /* Actually, we don't care if it's failed or not... On a RAID
403 set with correct parity, this function should be callable
404 on any component without ill affects. */
405 /* RF_ASSERT(raidPtr->Disks[col].status == rf_ds_failed); */
406
407 #if RF_INCLUDE_PARITY_DECLUSTERING_DS > 0
408 if (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE) {
409 RF_ERRORMSG1("Unable to reconstruct to disk at col %d: operation not supported for RF_DISTRIBUTE_SPARE\n", col);
410
411 raidPtr->reconInProgress--;
412 RF_UNLOCK_MUTEX(raidPtr->mutex);
413 RF_SIGNAL_COND(raidPtr->waitForReconCond);
414 return (EINVAL);
415 }
416 #endif
417 lwp = LIST_FIRST(&raidPtr->engine_thread->p_lwps);
418
419 /* This device may have been opened successfully the
420 first time. Close it before trying to open it again.. */
421
422 if (raidPtr->raid_cinfo[col].ci_vp != NULL) {
423 #if 0
424 printf("Closed the open device: %s\n",
425 raidPtr->Disks[col].devname);
426 #endif
427 vp = raidPtr->raid_cinfo[col].ci_vp;
428 ac = raidPtr->Disks[col].auto_configured;
429 RF_UNLOCK_MUTEX(raidPtr->mutex);
430 rf_close_component(raidPtr, vp, ac);
431 RF_LOCK_MUTEX(raidPtr->mutex);
432 raidPtr->raid_cinfo[col].ci_vp = NULL;
433 }
434 /* note that this disk was *not* auto_configured (any longer)*/
435 raidPtr->Disks[col].auto_configured = 0;
436
437 #if 0
438 printf("About to (re-)open the device for rebuilding: %s\n",
439 raidPtr->Disks[col].devname);
440 #endif
441 RF_UNLOCK_MUTEX(raidPtr->mutex);
442 retcode = dk_lookup(raidPtr->Disks[col].devname, lwp, &vp, UIO_SYSSPACE);
443
444 if (retcode) {
445 printf("raid%d: rebuilding: dk_lookup on device: %s failed: %d!\n",raidPtr->raidid,
446 raidPtr->Disks[col].devname, retcode);
447
448 /* the component isn't responding properly...
449 must be still dead :-( */
450 RF_LOCK_MUTEX(raidPtr->mutex);
451 raidPtr->reconInProgress--;
452 RF_UNLOCK_MUTEX(raidPtr->mutex);
453 RF_SIGNAL_COND(raidPtr->waitForReconCond);
454 return(retcode);
455 }
456
457 /* Ok, so we can at least do a lookup...
458 How about actually getting a vp for it? */
459
460 if ((retcode = VOP_GETATTR(vp, &va, lwp->l_cred, lwp)) != 0) {
461 RF_LOCK_MUTEX(raidPtr->mutex);
462 raidPtr->reconInProgress--;
463 RF_UNLOCK_MUTEX(raidPtr->mutex);
464 RF_SIGNAL_COND(raidPtr->waitForReconCond);
465 return(retcode);
466 }
467
468 retcode = VOP_IOCTL(vp, DIOCGPART, &dpart, FREAD, lwp->l_cred, lwp);
469 if (retcode) {
470 RF_LOCK_MUTEX(raidPtr->mutex);
471 raidPtr->reconInProgress--;
472 RF_UNLOCK_MUTEX(raidPtr->mutex);
473 RF_SIGNAL_COND(raidPtr->waitForReconCond);
474 return(retcode);
475 }
476 RF_LOCK_MUTEX(raidPtr->mutex);
477 raidPtr->Disks[col].blockSize = dpart.disklab->d_secsize;
478
479 raidPtr->Disks[col].numBlocks = dpart.part->p_size -
480 rf_protectedSectors;
481
482 raidPtr->raid_cinfo[col].ci_vp = vp;
483 raidPtr->raid_cinfo[col].ci_dev = va.va_rdev;
484
485 raidPtr->Disks[col].dev = va.va_rdev;
486
487 /* we allow the user to specify that only a fraction
488 of the disks should be used this is just for debug:
489 it speeds up * the parity scan */
490 raidPtr->Disks[col].numBlocks = raidPtr->Disks[col].numBlocks *
491 rf_sizePercentage / 100;
492 RF_UNLOCK_MUTEX(raidPtr->mutex);
493
494 spareDiskPtr = &raidPtr->Disks[col];
495 spareDiskPtr->status = rf_ds_used_spare;
496
497 printf("raid%d: initiating in-place reconstruction on column %d\n",
498 raidPtr->raidid, col);
499
500 reconDesc = AllocRaidReconDesc((void *) raidPtr, col, spareDiskPtr,
501 numDisksDone, col);
502 raidPtr->reconDesc = (void *) reconDesc;
503 #if RF_RECON_STATS > 0
504 reconDesc->hsStallCount = 0;
505 reconDesc->numReconExecDelays = 0;
506 reconDesc->numReconEventWaits = 0;
507 #endif /* RF_RECON_STATS > 0 */
508 reconDesc->reconExecTimerRunning = 0;
509 reconDesc->reconExecTicks = 0;
510 reconDesc->maxReconExecTicks = 0;
511 rc = rf_ContinueReconstructFailedDisk(reconDesc);
512
513 if (!rc) {
514 RF_LOCK_MUTEX(raidPtr->mutex);
515 /* Need to set these here, as at this point it'll be claiming
516 that the disk is in rf_ds_spared! But we know better :-) */
517
518 raidPtr->Disks[col].status = rf_ds_optimal;
519 raidPtr->status = rf_rs_optimal;
520 RF_UNLOCK_MUTEX(raidPtr->mutex);
521
522 /* fix up the component label */
523 /* Don't actually need the read here.. */
524 raidread_component_label(raidPtr->raid_cinfo[col].ci_dev,
525 raidPtr->raid_cinfo[col].ci_vp,
526 &c_label);
527
528 RF_LOCK_MUTEX(raidPtr->mutex);
529 raid_init_component_label(raidPtr, &c_label);
530
531 c_label.row = 0;
532 c_label.column = col;
533
534 /* We've just done a rebuild based on all the other
535 disks, so at this point the parity is known to be
536 clean, even if it wasn't before. */
537
538 /* XXX doesn't hold for RAID 6!!*/
539
540 raidPtr->parity_good = RF_RAID_CLEAN;
541 RF_UNLOCK_MUTEX(raidPtr->mutex);
542
543 raidwrite_component_label(raidPtr->raid_cinfo[col].ci_dev,
544 raidPtr->raid_cinfo[col].ci_vp,
545 &c_label);
546
547 } else {
548 /* Reconstruct-in-place failed. Disk goes back to
549 "failed" status, regardless of what it was before. */
550 RF_LOCK_MUTEX(raidPtr->mutex);
551 raidPtr->Disks[col].status = rf_ds_failed;
552 RF_UNLOCK_MUTEX(raidPtr->mutex);
553 }
554
555 rf_update_component_labels(raidPtr, RF_NORMAL_COMPONENT_UPDATE);
556
557 RF_LOCK_MUTEX(raidPtr->mutex);
558 raidPtr->reconInProgress--;
559 RF_UNLOCK_MUTEX(raidPtr->mutex);
560
561 RF_SIGNAL_COND(raidPtr->waitForReconCond);
562 return (rc);
563 }
564
565
566 int
567 rf_ContinueReconstructFailedDisk(RF_RaidReconDesc_t *reconDesc)
568 {
569 RF_Raid_t *raidPtr = reconDesc->raidPtr;
570 RF_RowCol_t col = reconDesc->col;
571 RF_RowCol_t scol = reconDesc->scol;
572 RF_ReconMap_t *mapPtr;
573 RF_ReconCtrl_t *tmp_reconctrl;
574 RF_ReconEvent_t *event;
575 RF_StripeCount_t incPSID,lastPSID,num_writes,pending_writes,prev;
576 RF_ReconUnitCount_t RUsPerPU;
577 struct timeval etime, elpsd;
578 unsigned long xor_s, xor_resid_us;
579 int i, ds;
580 int status, done;
581 int recon_error, write_error;
582
583 raidPtr->accumXorTimeUs = 0;
584 #if RF_ACC_TRACE > 0
585 /* create one trace record per physical disk */
586 RF_Malloc(raidPtr->recon_tracerecs, raidPtr->numCol * sizeof(RF_AccTraceEntry_t), (RF_AccTraceEntry_t *));
587 #endif
588
589 /* quiesce the array prior to starting recon. this is needed
590 * to assure no nasty interactions with pending user writes.
591 * We need to do this before we change the disk or row status. */
592
593 Dprintf("RECON: begin request suspend\n");
594 rf_SuspendNewRequestsAndWait(raidPtr);
595 Dprintf("RECON: end request suspend\n");
596
597 /* allocate our RF_ReconCTRL_t before we protect raidPtr->reconControl[row] */
598 tmp_reconctrl = rf_MakeReconControl(reconDesc, col, scol);
599
600 RF_LOCK_MUTEX(raidPtr->mutex);
601
602 /* create the reconstruction control pointer and install it in
603 * the right slot */
604 raidPtr->reconControl = tmp_reconctrl;
605 mapPtr = raidPtr->reconControl->reconMap;
606 raidPtr->reconControl->numRUsTotal = mapPtr->totalRUs;
607 raidPtr->reconControl->numRUsComplete = 0;
608 raidPtr->status = rf_rs_reconstructing;
609 raidPtr->Disks[col].status = rf_ds_reconstructing;
610 raidPtr->Disks[col].spareCol = scol;
611
612 RF_UNLOCK_MUTEX(raidPtr->mutex);
613
614 RF_GETTIME(raidPtr->reconControl->starttime);
615
616 Dprintf("RECON: resume requests\n");
617 rf_ResumeNewRequests(raidPtr);
618
619
620 mapPtr = raidPtr->reconControl->reconMap;
621
622 incPSID = RF_RECONMAP_SIZE;
623 lastPSID = raidPtr->Layout.numStripe / raidPtr->Layout.SUsPerPU;
624 RUsPerPU = raidPtr->Layout.SUsPerPU / raidPtr->Layout.SUsPerRU;
625 recon_error = 0;
626 write_error = 0;
627 pending_writes = incPSID;
628 raidPtr->reconControl->lastPSID = incPSID;
629
630 /* start the actual reconstruction */
631
632 done = 0;
633 while (!done) {
634
635 if (raidPtr->waitShutdown) {
636 /* someone is unconfiguring this array... bail on the reconstruct.. */
637 recon_error = 1;
638 break;
639 }
640
641 num_writes = 0;
642
643 /* issue a read for each surviving disk */
644
645 reconDesc->numDisksDone = 0;
646 for (i = 0; i < raidPtr->numCol; i++) {
647 if (i != col) {
648 /* find and issue the next I/O on the
649 * indicated disk */
650 if (IssueNextReadRequest(raidPtr, i)) {
651 Dprintf1("RECON: done issuing for c%d\n", i);
652 reconDesc->numDisksDone++;
653 }
654 }
655 }
656
657 /* process reconstruction events until all disks report that
658 * they've completed all work */
659
660 while (reconDesc->numDisksDone < raidPtr->numCol - 1) {
661
662 event = rf_GetNextReconEvent(reconDesc);
663 status = ProcessReconEvent(raidPtr, event);
664
665 /* the normal case is that a read completes, and all is well. */
666 if (status == RF_RECON_DONE_READS) {
667 reconDesc->numDisksDone++;
668 } else if ((status == RF_RECON_READ_ERROR) ||
669 (status == RF_RECON_WRITE_ERROR)) {
670 /* an error was encountered while reconstructing...
671 Pretend we've finished this disk.
672 */
673 recon_error = 1;
674 raidPtr->reconControl->error = 1;
675
676 /* bump the numDisksDone count for reads,
677 but not for writes */
678 if (status == RF_RECON_READ_ERROR)
679 reconDesc->numDisksDone++;
680
681 /* write errors are special -- when we are
682 done dealing with the reads that are
683 finished, we don't want to wait for any
684 writes */
685 if (status == RF_RECON_WRITE_ERROR)
686 write_error = 1;
687
688 } else if (status == RF_RECON_READ_STOPPED) {
689 /* count this component as being "done" */
690 reconDesc->numDisksDone++;
691 } else if (status == RF_RECON_WRITE_DONE) {
692 num_writes++;
693 }
694
695 if (recon_error) {
696 /* make sure any stragglers are woken up so that
697 their theads will complete, and we can get out
698 of here with all IO processed */
699
700 rf_WakeupHeadSepCBWaiters(raidPtr);
701 }
702
703 raidPtr->reconControl->numRUsTotal =
704 mapPtr->totalRUs;
705 raidPtr->reconControl->numRUsComplete =
706 mapPtr->totalRUs -
707 rf_UnitsLeftToReconstruct(mapPtr);
708
709 #if RF_DEBUG_RECON
710 raidPtr->reconControl->percentComplete =
711 (raidPtr->reconControl->numRUsComplete * 100 / raidPtr->reconControl->numRUsTotal);
712 if (rf_prReconSched) {
713 rf_PrintReconSchedule(raidPtr->reconControl->reconMap, &(raidPtr->reconControl->starttime));
714 }
715 #endif
716 }
717
718 /* reads done, wakup any waiters, and then wait for writes */
719
720 rf_WakeupHeadSepCBWaiters(raidPtr);
721
722 while (!recon_error && (num_writes < pending_writes)) {
723 event = rf_GetNextReconEvent(reconDesc);
724 status = ProcessReconEvent(raidPtr, event);
725
726 if (status == RF_RECON_WRITE_ERROR) {
727 recon_error = 1;
728 raidPtr->reconControl->error = 1;
729 /* an error was encountered at the very end... bail */
730 } else if (status == RF_RECON_WRITE_DONE) {
731 num_writes++;
732 }
733 }
734 if (recon_error ||
735 (raidPtr->reconControl->lastPSID == lastPSID)) {
736 done = 1;
737 break;
738 }
739
740 prev = raidPtr->reconControl->lastPSID;
741 raidPtr->reconControl->lastPSID += incPSID;
742
743 if (raidPtr->reconControl->lastPSID > lastPSID) {
744 pending_writes = lastPSID - prev;
745 raidPtr->reconControl->lastPSID = lastPSID;
746 }
747
748 /* back down curPSID to get ready for the next round... */
749 for (i = 0; i < raidPtr->numCol; i++) {
750 if (i != col) {
751 raidPtr->reconControl->perDiskInfo[i].curPSID--;
752 raidPtr->reconControl->perDiskInfo[i].ru_count = RUsPerPU - 1;
753 }
754 }
755 }
756
757 mapPtr = raidPtr->reconControl->reconMap;
758 if (rf_reconDebug) {
759 printf("RECON: all reads completed\n");
760 }
761 /* at this point all the reads have completed. We now wait
762 * for any pending writes to complete, and then we're done */
763
764 while (!recon_error && rf_UnitsLeftToReconstruct(raidPtr->reconControl->reconMap) > 0) {
765
766 event = rf_GetNextReconEvent(reconDesc);
767 status = ProcessReconEvent(raidPtr, event);
768
769 if (status == RF_RECON_WRITE_ERROR) {
770 recon_error = 1;
771 raidPtr->reconControl->error = 1;
772 /* an error was encountered at the very end... bail */
773 } else {
774 #if RF_DEBUG_RECON
775 raidPtr->reconControl->percentComplete = 100 - (rf_UnitsLeftToReconstruct(mapPtr) * 100 / mapPtr->totalRUs);
776 if (rf_prReconSched) {
777 rf_PrintReconSchedule(raidPtr->reconControl->reconMap, &(raidPtr->reconControl->starttime));
778 }
779 #endif
780 }
781 }
782
783 if (recon_error) {
784 /* we've encountered an error in reconstructing. */
785 printf("raid%d: reconstruction failed.\n", raidPtr->raidid);
786
787 /* we start by blocking IO to the RAID set. */
788 rf_SuspendNewRequestsAndWait(raidPtr);
789
790 RF_LOCK_MUTEX(raidPtr->mutex);
791 /* mark set as being degraded, rather than
792 rf_rs_reconstructing as we were before the problem.
793 After this is done we can update status of the
794 component disks without worrying about someone
795 trying to read from a failed component.
796 */
797 raidPtr->status = rf_rs_degraded;
798 RF_UNLOCK_MUTEX(raidPtr->mutex);
799
800 /* resume IO */
801 rf_ResumeNewRequests(raidPtr);
802
803 /* At this point there are two cases:
804 1) If we've experienced a read error, then we've
805 already waited for all the reads we're going to get,
806 and we just need to wait for the writes.
807
808 2) If we've experienced a write error, we've also
809 already waited for all the reads to complete,
810 but there is little point in waiting for the writes --
811 when they do complete, they will just be ignored.
812
813 So we just wait for writes to complete if we didn't have a
814 write error.
815 */
816
817 if (!write_error) {
818 /* wait for writes to complete */
819 while (raidPtr->reconControl->pending_writes > 0) {
820
821 event = rf_GetNextReconEvent(reconDesc);
822 status = ProcessReconEvent(raidPtr, event);
823
824 if (status == RF_RECON_WRITE_ERROR) {
825 raidPtr->reconControl->error = 1;
826 /* an error was encountered at the very end... bail.
827 This will be very bad news for the user, since
828 at this point there will have been a read error
829 on one component, and a write error on another!
830 */
831 break;
832 }
833 }
834 }
835
836
837 /* cleanup */
838
839 /* drain the event queue - after waiting for the writes above,
840 there shouldn't be much (if anything!) left in the queue. */
841
842 rf_DrainReconEventQueue(reconDesc);
843
844 /* XXX As much as we'd like to free the recon control structure
845 and the reconDesc, we have no way of knowing if/when those will
846 be touched by IO that has yet to occur. It is rather poor to be
847 basically causing a 'memory leak' here, but there doesn't seem to be
848 a cleaner alternative at this time. Perhaps when the reconstruct code
849 gets a makeover this problem will go away.
850 */
851 #if 0
852 rf_FreeReconControl(raidPtr);
853 #endif
854
855 #if RF_ACC_TRACE > 0
856 RF_Free(raidPtr->recon_tracerecs, raidPtr->numCol * sizeof(RF_AccTraceEntry_t));
857 #endif
858 /* XXX see comment above */
859 #if 0
860 FreeReconDesc(reconDesc);
861 #endif
862
863 return (1);
864 }
865
866 /* Success: mark the dead disk as reconstructed. We quiesce
867 * the array here to assure no nasty interactions with pending
868 * user accesses when we free up the psstatus structure as
869 * part of FreeReconControl() */
870
871 rf_SuspendNewRequestsAndWait(raidPtr);
872
873 RF_LOCK_MUTEX(raidPtr->mutex);
874 raidPtr->numFailures--;
875 ds = (raidPtr->Layout.map->flags & RF_DISTRIBUTE_SPARE);
876 raidPtr->Disks[col].status = (ds) ? rf_ds_dist_spared : rf_ds_spared;
877 raidPtr->status = (ds) ? rf_rs_reconfigured : rf_rs_optimal;
878 RF_UNLOCK_MUTEX(raidPtr->mutex);
879 RF_GETTIME(etime);
880 RF_TIMEVAL_DIFF(&(raidPtr->reconControl->starttime), &etime, &elpsd);
881
882 rf_ResumeNewRequests(raidPtr);
883
884 printf("raid%d: Reconstruction of disk at col %d completed\n",
885 raidPtr->raidid, col);
886 xor_s = raidPtr->accumXorTimeUs / 1000000;
887 xor_resid_us = raidPtr->accumXorTimeUs % 1000000;
888 printf("raid%d: Recon time was %d.%06d seconds, accumulated XOR time was %ld us (%ld.%06ld)\n",
889 raidPtr->raidid,
890 (int) elpsd.tv_sec, (int) elpsd.tv_usec,
891 raidPtr->accumXorTimeUs, xor_s, xor_resid_us);
892 printf("raid%d: (start time %d sec %d usec, end time %d sec %d usec)\n",
893 raidPtr->raidid,
894 (int) raidPtr->reconControl->starttime.tv_sec,
895 (int) raidPtr->reconControl->starttime.tv_usec,
896 (int) etime.tv_sec, (int) etime.tv_usec);
897 #if RF_RECON_STATS > 0
898 printf("raid%d: Total head-sep stall count was %d\n",
899 raidPtr->raidid, (int) reconDesc->hsStallCount);
900 #endif /* RF_RECON_STATS > 0 */
901 rf_FreeReconControl(raidPtr);
902 #if RF_ACC_TRACE > 0
903 RF_Free(raidPtr->recon_tracerecs, raidPtr->numCol * sizeof(RF_AccTraceEntry_t));
904 #endif
905 FreeReconDesc(reconDesc);
906
907 return (0);
908
909 }
910 /*****************************************************************************
911 * do the right thing upon each reconstruction event.
912 *****************************************************************************/
913 static int
914 ProcessReconEvent(RF_Raid_t *raidPtr, RF_ReconEvent_t *event)
915 {
916 int retcode = 0, submitblocked;
917 RF_ReconBuffer_t *rbuf;
918 RF_SectorCount_t sectorsPerRU;
919
920 retcode = RF_RECON_READ_STOPPED;
921
922 Dprintf1("RECON: ProcessReconEvent type %d\n", event->type);
923
924 switch (event->type) {
925
926 /* a read I/O has completed */
927 case RF_REVENT_READDONE:
928 rbuf = raidPtr->reconControl->perDiskInfo[event->col].rbuf;
929 Dprintf2("RECON: READDONE EVENT: col %d psid %ld\n",
930 event->col, rbuf->parityStripeID);
931 Dprintf7("RECON: done read psid %ld buf %lx %02x %02x %02x %02x %02x\n",
932 rbuf->parityStripeID, rbuf->buffer, rbuf->buffer[0] & 0xff, rbuf->buffer[1] & 0xff,
933 rbuf->buffer[2] & 0xff, rbuf->buffer[3] & 0xff, rbuf->buffer[4] & 0xff);
934 rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
935 if (!raidPtr->reconControl->error) {
936 submitblocked = rf_SubmitReconBuffer(rbuf, 0, 0);
937 Dprintf1("RECON: submitblocked=%d\n", submitblocked);
938 if (!submitblocked)
939 retcode = IssueNextReadRequest(raidPtr, event->col);
940 else
941 retcode = 0;
942 }
943 break;
944
945 /* a write I/O has completed */
946 case RF_REVENT_WRITEDONE:
947 #if RF_DEBUG_RECON
948 if (rf_floatingRbufDebug) {
949 rf_CheckFloatingRbufCount(raidPtr, 1);
950 }
951 #endif
952 sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU;
953 rbuf = (RF_ReconBuffer_t *) event->arg;
954 rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
955 Dprintf3("RECON: WRITEDONE EVENT: psid %d ru %d (%d %% complete)\n",
956 rbuf->parityStripeID, rbuf->which_ru, raidPtr->reconControl->percentComplete);
957 rf_ReconMapUpdate(raidPtr, raidPtr->reconControl->reconMap,
958 rbuf->failedDiskSectorOffset, rbuf->failedDiskSectorOffset + sectorsPerRU - 1);
959 rf_RemoveFromActiveReconTable(raidPtr, rbuf->parityStripeID, rbuf->which_ru);
960
961 RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
962 raidPtr->reconControl->pending_writes--;
963 RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
964
965 if (rbuf->type == RF_RBUF_TYPE_FLOATING) {
966 RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
967 while(raidPtr->reconControl->rb_lock) {
968 ltsleep(&raidPtr->reconControl->rb_lock, PRIBIO, "reconctrlpre1", 0,
969 &raidPtr->reconControl->rb_mutex);
970 }
971 raidPtr->reconControl->rb_lock = 1;
972 RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
973
974 raidPtr->numFullReconBuffers--;
975 rf_ReleaseFloatingReconBuffer(raidPtr, rbuf);
976
977 RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
978 raidPtr->reconControl->rb_lock = 0;
979 wakeup(&raidPtr->reconControl->rb_lock);
980 RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
981 } else
982 if (rbuf->type == RF_RBUF_TYPE_FORCED)
983 rf_FreeReconBuffer(rbuf);
984 else
985 RF_ASSERT(0);
986 retcode = RF_RECON_WRITE_DONE;
987 break;
988
989 case RF_REVENT_BUFCLEAR: /* A buffer-stall condition has been
990 * cleared */
991 Dprintf1("RECON: BUFCLEAR EVENT: col %d\n", event->col);
992 if (!raidPtr->reconControl->error) {
993 submitblocked = rf_SubmitReconBuffer(raidPtr->reconControl->perDiskInfo[event->col].rbuf,
994 0, (int) (long) event->arg);
995 RF_ASSERT(!submitblocked); /* we wouldn't have gotten the
996 * BUFCLEAR event if we
997 * couldn't submit */
998 retcode = IssueNextReadRequest(raidPtr, event->col);
999 }
1000 break;
1001
1002 case RF_REVENT_BLOCKCLEAR: /* A user-write reconstruction
1003 * blockage has been cleared */
1004 DDprintf1("RECON: BLOCKCLEAR EVENT: col %d\n", event->col);
1005 if (!raidPtr->reconControl->error) {
1006 retcode = TryToRead(raidPtr, event->col);
1007 }
1008 break;
1009
1010 case RF_REVENT_HEADSEPCLEAR: /* A max-head-separation
1011 * reconstruction blockage has been
1012 * cleared */
1013 Dprintf1("RECON: HEADSEPCLEAR EVENT: col %d\n", event->col);
1014 if (!raidPtr->reconControl->error) {
1015 retcode = TryToRead(raidPtr, event->col);
1016 }
1017 break;
1018
1019 /* a buffer has become ready to write */
1020 case RF_REVENT_BUFREADY:
1021 Dprintf1("RECON: BUFREADY EVENT: col %d\n", event->col);
1022 if (!raidPtr->reconControl->error) {
1023 retcode = IssueNextWriteRequest(raidPtr);
1024 #if RF_DEBUG_RECON
1025 if (rf_floatingRbufDebug) {
1026 rf_CheckFloatingRbufCount(raidPtr, 1);
1027 }
1028 #endif
1029 }
1030 break;
1031
1032 /* we need to skip the current RU entirely because it got
1033 * recon'd while we were waiting for something else to happen */
1034 case RF_REVENT_SKIP:
1035 DDprintf1("RECON: SKIP EVENT: col %d\n", event->col);
1036 if (!raidPtr->reconControl->error) {
1037 retcode = IssueNextReadRequest(raidPtr, event->col);
1038 }
1039 break;
1040
1041 /* a forced-reconstruction read access has completed. Just
1042 * submit the buffer */
1043 case RF_REVENT_FORCEDREADDONE:
1044 rbuf = (RF_ReconBuffer_t *) event->arg;
1045 rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
1046 DDprintf1("RECON: FORCEDREADDONE EVENT: col %d\n", event->col);
1047 if (!raidPtr->reconControl->error) {
1048 submitblocked = rf_SubmitReconBuffer(rbuf, 1, 0);
1049 RF_ASSERT(!submitblocked);
1050 retcode = 0;
1051 }
1052 break;
1053
1054 /* A read I/O failed to complete */
1055 case RF_REVENT_READ_FAILED:
1056 retcode = RF_RECON_READ_ERROR;
1057 break;
1058
1059 /* A write I/O failed to complete */
1060 case RF_REVENT_WRITE_FAILED:
1061 retcode = RF_RECON_WRITE_ERROR;
1062
1063 rbuf = (RF_ReconBuffer_t *) event->arg;
1064
1065 /* cleanup the disk queue data */
1066 rf_FreeDiskQueueData((RF_DiskQueueData_t *) rbuf->arg);
1067
1068 /* At this point we're erroring out, badly, and floatingRbufs
1069 may not even be valid. Rather than putting this back onto
1070 the floatingRbufs list, just arrange for its immediate
1071 destruction.
1072 */
1073 rf_FreeReconBuffer(rbuf);
1074 break;
1075
1076 /* a forced read I/O failed to complete */
1077 case RF_REVENT_FORCEDREAD_FAILED:
1078 retcode = RF_RECON_READ_ERROR;
1079 break;
1080
1081 default:
1082 RF_PANIC();
1083 }
1084 rf_FreeReconEventDesc(event);
1085 return (retcode);
1086 }
1087 /*****************************************************************************
1088 *
1089 * find the next thing that's needed on the indicated disk, and issue
1090 * a read request for it. We assume that the reconstruction buffer
1091 * associated with this process is free to receive the data. If
1092 * reconstruction is blocked on the indicated RU, we issue a
1093 * blockage-release request instead of a physical disk read request.
1094 * If the current disk gets too far ahead of the others, we issue a
1095 * head-separation wait request and return.
1096 *
1097 * ctrl->{ru_count, curPSID, diskOffset} and
1098 * rbuf->failedDiskSectorOffset are maintained to point to the unit
1099 * we're currently accessing. Note that this deviates from the
1100 * standard C idiom of having counters point to the next thing to be
1101 * accessed. This allows us to easily retry when we're blocked by
1102 * head separation or reconstruction-blockage events.
1103 *
1104 *****************************************************************************/
1105 static int
1106 IssueNextReadRequest(RF_Raid_t *raidPtr, RF_RowCol_t col)
1107 {
1108 RF_PerDiskReconCtrl_t *ctrl = &raidPtr->reconControl->perDiskInfo[col];
1109 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
1110 RF_ReconBuffer_t *rbuf = ctrl->rbuf;
1111 RF_ReconUnitCount_t RUsPerPU = layoutPtr->SUsPerPU / layoutPtr->SUsPerRU;
1112 RF_SectorCount_t sectorsPerRU = layoutPtr->sectorsPerStripeUnit * layoutPtr->SUsPerRU;
1113 int do_new_check = 0, retcode = 0, status;
1114
1115 /* if we are currently the slowest disk, mark that we have to do a new
1116 * check */
1117 if (ctrl->headSepCounter <= raidPtr->reconControl->minHeadSepCounter)
1118 do_new_check = 1;
1119
1120 while (1) {
1121
1122 ctrl->ru_count++;
1123 if (ctrl->ru_count < RUsPerPU) {
1124 ctrl->diskOffset += sectorsPerRU;
1125 rbuf->failedDiskSectorOffset += sectorsPerRU;
1126 } else {
1127 ctrl->curPSID++;
1128 ctrl->ru_count = 0;
1129 /* code left over from when head-sep was based on
1130 * parity stripe id */
1131 if (ctrl->curPSID >= raidPtr->reconControl->lastPSID) {
1132 CheckForNewMinHeadSep(raidPtr, ++(ctrl->headSepCounter));
1133 return (RF_RECON_DONE_READS); /* finito! */
1134 }
1135 /* find the disk offsets of the start of the parity
1136 * stripe on both the current disk and the failed
1137 * disk. skip this entire parity stripe if either disk
1138 * does not appear in the indicated PS */
1139 status = ComputePSDiskOffsets(raidPtr, ctrl->curPSID, col, &ctrl->diskOffset, &rbuf->failedDiskSectorOffset,
1140 &rbuf->spCol, &rbuf->spOffset);
1141 if (status) {
1142 ctrl->ru_count = RUsPerPU - 1;
1143 continue;
1144 }
1145 }
1146 rbuf->which_ru = ctrl->ru_count;
1147
1148 /* skip this RU if it's already been reconstructed */
1149 if (rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, rbuf->failedDiskSectorOffset)) {
1150 Dprintf2("Skipping psid %ld ru %d: already reconstructed\n", ctrl->curPSID, ctrl->ru_count);
1151 continue;
1152 }
1153 break;
1154 }
1155 ctrl->headSepCounter++;
1156 if (do_new_check)
1157 CheckForNewMinHeadSep(raidPtr, ctrl->headSepCounter); /* update min if needed */
1158
1159
1160 /* at this point, we have definitely decided what to do, and we have
1161 * only to see if we can actually do it now */
1162 rbuf->parityStripeID = ctrl->curPSID;
1163 rbuf->which_ru = ctrl->ru_count;
1164 #if RF_ACC_TRACE > 0
1165 memset((char *) &raidPtr->recon_tracerecs[col], 0,
1166 sizeof(raidPtr->recon_tracerecs[col]));
1167 raidPtr->recon_tracerecs[col].reconacc = 1;
1168 RF_ETIMER_START(raidPtr->recon_tracerecs[col].recon_timer);
1169 #endif
1170 retcode = TryToRead(raidPtr, col);
1171 return (retcode);
1172 }
1173
1174 /*
1175 * tries to issue the next read on the indicated disk. We may be
1176 * blocked by (a) the heads being too far apart, or (b) recon on the
1177 * indicated RU being blocked due to a write by a user thread. In
1178 * this case, we issue a head-sep or blockage wait request, which will
1179 * cause this same routine to be invoked again later when the blockage
1180 * has cleared.
1181 */
1182
1183 static int
1184 TryToRead(RF_Raid_t *raidPtr, RF_RowCol_t col)
1185 {
1186 RF_PerDiskReconCtrl_t *ctrl = &raidPtr->reconControl->perDiskInfo[col];
1187 RF_SectorCount_t sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU;
1188 RF_StripeNum_t psid = ctrl->curPSID;
1189 RF_ReconUnitNum_t which_ru = ctrl->ru_count;
1190 RF_DiskQueueData_t *req;
1191 int status;
1192 RF_ReconParityStripeStatus_t *pssPtr, *newpssPtr;
1193
1194 /* if the current disk is too far ahead of the others, issue a
1195 * head-separation wait and return */
1196 if (CheckHeadSeparation(raidPtr, ctrl, col, ctrl->headSepCounter, which_ru))
1197 return (0);
1198
1199 /* allocate a new PSS in case we need it */
1200 newpssPtr = rf_AllocPSStatus(raidPtr);
1201
1202 RF_LOCK_PSS_MUTEX(raidPtr, psid);
1203 pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl->pssTable, psid, which_ru, RF_PSS_CREATE, newpssPtr);
1204
1205 if (pssPtr != newpssPtr) {
1206 rf_FreePSStatus(raidPtr, newpssPtr);
1207 }
1208
1209 /* if recon is blocked on the indicated parity stripe, issue a
1210 * block-wait request and return. this also must mark the indicated RU
1211 * in the stripe as under reconstruction if not blocked. */
1212 status = CheckForcedOrBlockedReconstruction(raidPtr, pssPtr, ctrl, col, psid, which_ru);
1213 if (status == RF_PSS_RECON_BLOCKED) {
1214 Dprintf2("RECON: Stalling psid %ld ru %d: recon blocked\n", psid, which_ru);
1215 goto out;
1216 } else
1217 if (status == RF_PSS_FORCED_ON_WRITE) {
1218 rf_CauseReconEvent(raidPtr, col, NULL, RF_REVENT_SKIP);
1219 goto out;
1220 }
1221 /* make one last check to be sure that the indicated RU didn't get
1222 * reconstructed while we were waiting for something else to happen.
1223 * This is unfortunate in that it causes us to make this check twice
1224 * in the normal case. Might want to make some attempt to re-work
1225 * this so that we only do this check if we've definitely blocked on
1226 * one of the above checks. When this condition is detected, we may
1227 * have just created a bogus status entry, which we need to delete. */
1228 if (rf_CheckRUReconstructed(raidPtr->reconControl->reconMap, ctrl->rbuf->failedDiskSectorOffset)) {
1229 Dprintf2("RECON: Skipping psid %ld ru %d: prior recon after stall\n", psid, which_ru);
1230 if (pssPtr == newpssPtr)
1231 rf_PSStatusDelete(raidPtr, raidPtr->reconControl->pssTable, pssPtr);
1232 rf_CauseReconEvent(raidPtr, col, NULL, RF_REVENT_SKIP);
1233 goto out;
1234 }
1235 /* found something to read. issue the I/O */
1236 Dprintf4("RECON: Read for psid %ld on col %d offset %ld buf %lx\n",
1237 psid, col, ctrl->diskOffset, ctrl->rbuf->buffer);
1238 #if RF_ACC_TRACE > 0
1239 RF_ETIMER_STOP(raidPtr->recon_tracerecs[col].recon_timer);
1240 RF_ETIMER_EVAL(raidPtr->recon_tracerecs[col].recon_timer);
1241 raidPtr->recon_tracerecs[col].specific.recon.recon_start_to_fetch_us =
1242 RF_ETIMER_VAL_US(raidPtr->recon_tracerecs[col].recon_timer);
1243 RF_ETIMER_START(raidPtr->recon_tracerecs[col].recon_timer);
1244 #endif
1245 /* should be ok to use a NULL proc pointer here, all the bufs we use
1246 * should be in kernel space */
1247 req = rf_CreateDiskQueueData(RF_IO_TYPE_READ, ctrl->diskOffset, sectorsPerRU, ctrl->rbuf->buffer, psid, which_ru,
1248 ReconReadDoneProc, (void *) ctrl,
1249 #if RF_ACC_TRACE > 0
1250 &raidPtr->recon_tracerecs[col],
1251 #else
1252 NULL,
1253 #endif
1254 (void *) raidPtr, 0, NULL, PR_WAITOK);
1255
1256 ctrl->rbuf->arg = (void *) req;
1257 rf_DiskIOEnqueue(&raidPtr->Queues[col], req, RF_IO_RECON_PRIORITY);
1258 pssPtr->issued[col] = 1;
1259
1260 out:
1261 RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
1262 return (0);
1263 }
1264
1265
1266 /*
1267 * given a parity stripe ID, we want to find out whether both the
1268 * current disk and the failed disk exist in that parity stripe. If
1269 * not, we want to skip this whole PS. If so, we want to find the
1270 * disk offset of the start of the PS on both the current disk and the
1271 * failed disk.
1272 *
1273 * this works by getting a list of disks comprising the indicated
1274 * parity stripe, and searching the list for the current and failed
1275 * disks. Once we've decided they both exist in the parity stripe, we
1276 * need to decide whether each is data or parity, so that we'll know
1277 * which mapping function to call to get the corresponding disk
1278 * offsets.
1279 *
1280 * this is kind of unpleasant, but doing it this way allows the
1281 * reconstruction code to use parity stripe IDs rather than physical
1282 * disks address to march through the failed disk, which greatly
1283 * simplifies a lot of code, as well as eliminating the need for a
1284 * reverse-mapping function. I also think it will execute faster,
1285 * since the calls to the mapping module are kept to a minimum.
1286 *
1287 * ASSUMES THAT THE STRIPE IDENTIFIER IDENTIFIES THE DISKS COMPRISING
1288 * THE STRIPE IN THE CORRECT ORDER
1289 *
1290 * raidPtr - raid descriptor
1291 * psid - parity stripe identifier
1292 * col - column of disk to find the offsets for
1293 * spCol - out: col of spare unit for failed unit
1294 * spOffset - out: offset into disk containing spare unit
1295 *
1296 */
1297
1298
1299 static int
1300 ComputePSDiskOffsets(RF_Raid_t *raidPtr, RF_StripeNum_t psid,
1301 RF_RowCol_t col, RF_SectorNum_t *outDiskOffset,
1302 RF_SectorNum_t *outFailedDiskSectorOffset,
1303 RF_RowCol_t *spCol, RF_SectorNum_t *spOffset)
1304 {
1305 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
1306 RF_RowCol_t fcol = raidPtr->reconControl->fcol;
1307 RF_RaidAddr_t sosRaidAddress; /* start-of-stripe */
1308 RF_RowCol_t *diskids;
1309 u_int i, j, k, i_offset, j_offset;
1310 RF_RowCol_t pcol;
1311 int testcol;
1312 RF_SectorNum_t poffset;
1313 char i_is_parity = 0, j_is_parity = 0;
1314 RF_RowCol_t stripeWidth = layoutPtr->numDataCol + layoutPtr->numParityCol;
1315
1316 /* get a listing of the disks comprising that stripe */
1317 sosRaidAddress = rf_ParityStripeIDToRaidAddress(layoutPtr, psid);
1318 (layoutPtr->map->IdentifyStripe) (raidPtr, sosRaidAddress, &diskids);
1319 RF_ASSERT(diskids);
1320
1321 /* reject this entire parity stripe if it does not contain the
1322 * indicated disk or it does not contain the failed disk */
1323
1324 for (i = 0; i < stripeWidth; i++) {
1325 if (col == diskids[i])
1326 break;
1327 }
1328 if (i == stripeWidth)
1329 goto skipit;
1330 for (j = 0; j < stripeWidth; j++) {
1331 if (fcol == diskids[j])
1332 break;
1333 }
1334 if (j == stripeWidth) {
1335 goto skipit;
1336 }
1337 /* find out which disk the parity is on */
1338 (layoutPtr->map->MapParity) (raidPtr, sosRaidAddress, &pcol, &poffset, RF_DONT_REMAP);
1339
1340 /* find out if either the current RU or the failed RU is parity */
1341 /* also, if the parity occurs in this stripe prior to the data and/or
1342 * failed col, we need to decrement i and/or j */
1343 for (k = 0; k < stripeWidth; k++)
1344 if (diskids[k] == pcol)
1345 break;
1346 RF_ASSERT(k < stripeWidth);
1347 i_offset = i;
1348 j_offset = j;
1349 if (k < i)
1350 i_offset--;
1351 else
1352 if (k == i) {
1353 i_is_parity = 1;
1354 i_offset = 0;
1355 } /* set offsets to zero to disable multiply
1356 * below */
1357 if (k < j)
1358 j_offset--;
1359 else
1360 if (k == j) {
1361 j_is_parity = 1;
1362 j_offset = 0;
1363 }
1364 /* at this point, [ij]_is_parity tells us whether the [current,failed]
1365 * disk is parity at the start of this RU, and, if data, "[ij]_offset"
1366 * tells us how far into the stripe the [current,failed] disk is. */
1367
1368 /* call the mapping routine to get the offset into the current disk,
1369 * repeat for failed disk. */
1370 if (i_is_parity)
1371 layoutPtr->map->MapParity(raidPtr, sosRaidAddress + i_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outDiskOffset, RF_DONT_REMAP);
1372 else
1373 layoutPtr->map->MapSector(raidPtr, sosRaidAddress + i_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outDiskOffset, RF_DONT_REMAP);
1374
1375 RF_ASSERT(col == testcol);
1376
1377 if (j_is_parity)
1378 layoutPtr->map->MapParity(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outFailedDiskSectorOffset, RF_DONT_REMAP);
1379 else
1380 layoutPtr->map->MapSector(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, &testcol, outFailedDiskSectorOffset, RF_DONT_REMAP);
1381 RF_ASSERT(fcol == testcol);
1382
1383 /* now locate the spare unit for the failed unit */
1384 #if RF_INCLUDE_PARITY_DECLUSTERING_DS > 0
1385 if (layoutPtr->map->flags & RF_DISTRIBUTE_SPARE) {
1386 if (j_is_parity)
1387 layoutPtr->map->MapParity(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, spCol, spOffset, RF_REMAP);
1388 else
1389 layoutPtr->map->MapSector(raidPtr, sosRaidAddress + j_offset * layoutPtr->sectorsPerStripeUnit, spCol, spOffset, RF_REMAP);
1390 } else {
1391 #endif
1392 *spCol = raidPtr->reconControl->spareCol;
1393 *spOffset = *outFailedDiskSectorOffset;
1394 #if RF_INCLUDE_PARITY_DECLUSTERING_DS > 0
1395 }
1396 #endif
1397 return (0);
1398
1399 skipit:
1400 Dprintf2("RECON: Skipping psid %ld: nothing needed from r%d c%d\n",
1401 psid, col);
1402 return (1);
1403 }
1404 /* this is called when a buffer has become ready to write to the replacement disk */
1405 static int
1406 IssueNextWriteRequest(RF_Raid_t *raidPtr)
1407 {
1408 RF_RaidLayout_t *layoutPtr = &raidPtr->Layout;
1409 RF_SectorCount_t sectorsPerRU = layoutPtr->sectorsPerStripeUnit * layoutPtr->SUsPerRU;
1410 #if RF_ACC_TRACE > 0
1411 RF_RowCol_t fcol = raidPtr->reconControl->fcol;
1412 #endif
1413 RF_ReconBuffer_t *rbuf;
1414 RF_DiskQueueData_t *req;
1415
1416 rbuf = rf_GetFullReconBuffer(raidPtr->reconControl);
1417 RF_ASSERT(rbuf); /* there must be one available, or we wouldn't
1418 * have gotten the event that sent us here */
1419 RF_ASSERT(rbuf->pssPtr);
1420
1421 rbuf->pssPtr->writeRbuf = rbuf;
1422 rbuf->pssPtr = NULL;
1423
1424 Dprintf6("RECON: New write (c %d offs %d) for psid %ld ru %d (failed disk offset %ld) buf %lx\n",
1425 rbuf->spCol, rbuf->spOffset, rbuf->parityStripeID,
1426 rbuf->which_ru, rbuf->failedDiskSectorOffset, rbuf->buffer);
1427 Dprintf6("RECON: new write psid %ld %02x %02x %02x %02x %02x\n",
1428 rbuf->parityStripeID, rbuf->buffer[0] & 0xff, rbuf->buffer[1] & 0xff,
1429 rbuf->buffer[2] & 0xff, rbuf->buffer[3] & 0xff, rbuf->buffer[4] & 0xff);
1430
1431 /* should be ok to use a NULL b_proc here b/c all addrs should be in
1432 * kernel space */
1433 req = rf_CreateDiskQueueData(RF_IO_TYPE_WRITE, rbuf->spOffset,
1434 sectorsPerRU, rbuf->buffer,
1435 rbuf->parityStripeID, rbuf->which_ru,
1436 ReconWriteDoneProc, (void *) rbuf,
1437 #if RF_ACC_TRACE > 0
1438 &raidPtr->recon_tracerecs[fcol],
1439 #else
1440 NULL,
1441 #endif
1442 (void *) raidPtr, 0, NULL, PR_WAITOK);
1443
1444 rbuf->arg = (void *) req;
1445 RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
1446 raidPtr->reconControl->pending_writes++;
1447 RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
1448 rf_DiskIOEnqueue(&raidPtr->Queues[rbuf->spCol], req, RF_IO_RECON_PRIORITY);
1449
1450 return (0);
1451 }
1452
1453 /*
1454 * this gets called upon the completion of a reconstruction read
1455 * operation the arg is a pointer to the per-disk reconstruction
1456 * control structure for the process that just finished a read.
1457 *
1458 * called at interrupt context in the kernel, so don't do anything
1459 * illegal here.
1460 */
1461 static int
1462 ReconReadDoneProc(void *arg, int status)
1463 {
1464 RF_PerDiskReconCtrl_t *ctrl = (RF_PerDiskReconCtrl_t *) arg;
1465 RF_Raid_t *raidPtr;
1466
1467 /* Detect that reconCtrl is no longer valid, and if that
1468 is the case, bail without calling rf_CauseReconEvent().
1469 There won't be anyone listening for this event anyway */
1470
1471 if (ctrl->reconCtrl == NULL)
1472 return(0);
1473
1474 raidPtr = ctrl->reconCtrl->reconDesc->raidPtr;
1475
1476 if (status) {
1477 printf("raid%d: Recon read failed!\n", raidPtr->raidid);
1478 rf_CauseReconEvent(raidPtr, ctrl->col, NULL, RF_REVENT_READ_FAILED);
1479 return(0);
1480 }
1481 #if RF_ACC_TRACE > 0
1482 RF_ETIMER_STOP(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
1483 RF_ETIMER_EVAL(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
1484 raidPtr->recon_tracerecs[ctrl->col].specific.recon.recon_fetch_to_return_us =
1485 RF_ETIMER_VAL_US(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
1486 RF_ETIMER_START(raidPtr->recon_tracerecs[ctrl->col].recon_timer);
1487 #endif
1488 rf_CauseReconEvent(raidPtr, ctrl->col, NULL, RF_REVENT_READDONE);
1489 return (0);
1490 }
1491 /* this gets called upon the completion of a reconstruction write operation.
1492 * the arg is a pointer to the rbuf that was just written
1493 *
1494 * called at interrupt context in the kernel, so don't do anything illegal here.
1495 */
1496 static int
1497 ReconWriteDoneProc(void *arg, int status)
1498 {
1499 RF_ReconBuffer_t *rbuf = (RF_ReconBuffer_t *) arg;
1500
1501 /* Detect that reconControl is no longer valid, and if that
1502 is the case, bail without calling rf_CauseReconEvent().
1503 There won't be anyone listening for this event anyway */
1504
1505 if (rbuf->raidPtr->reconControl == NULL)
1506 return(0);
1507
1508 Dprintf2("Reconstruction completed on psid %ld ru %d\n", rbuf->parityStripeID, rbuf->which_ru);
1509 if (status) {
1510 printf("raid%d: Recon write failed!\n", rbuf->raidPtr->raidid);
1511 rf_CauseReconEvent(rbuf->raidPtr, rbuf->col, arg, RF_REVENT_WRITE_FAILED);
1512 return(0);
1513 }
1514 rf_CauseReconEvent(rbuf->raidPtr, rbuf->col, arg, RF_REVENT_WRITEDONE);
1515 return (0);
1516 }
1517
1518
1519 /*
1520 * computes a new minimum head sep, and wakes up anyone who needs to
1521 * be woken as a result
1522 */
1523 static void
1524 CheckForNewMinHeadSep(RF_Raid_t *raidPtr, RF_HeadSepLimit_t hsCtr)
1525 {
1526 RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl;
1527 RF_HeadSepLimit_t new_min;
1528 RF_RowCol_t i;
1529 RF_CallbackDesc_t *p;
1530 RF_ASSERT(hsCtr >= reconCtrlPtr->minHeadSepCounter); /* from the definition
1531 * of a minimum */
1532
1533
1534 RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
1535 while(reconCtrlPtr->rb_lock) {
1536 ltsleep(&reconCtrlPtr->rb_lock, PRIBIO, "reconctlcnmhs", 0, &reconCtrlPtr->rb_mutex);
1537 }
1538 reconCtrlPtr->rb_lock = 1;
1539 RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
1540
1541 new_min = ~(1L << (8 * sizeof(long) - 1)); /* 0x7FFF....FFF */
1542 for (i = 0; i < raidPtr->numCol; i++)
1543 if (i != reconCtrlPtr->fcol) {
1544 if (reconCtrlPtr->perDiskInfo[i].headSepCounter < new_min)
1545 new_min = reconCtrlPtr->perDiskInfo[i].headSepCounter;
1546 }
1547 /* set the new minimum and wake up anyone who can now run again */
1548 if (new_min != reconCtrlPtr->minHeadSepCounter) {
1549 reconCtrlPtr->minHeadSepCounter = new_min;
1550 Dprintf1("RECON: new min head pos counter val is %ld\n", new_min);
1551 while (reconCtrlPtr->headSepCBList) {
1552 if (reconCtrlPtr->headSepCBList->callbackArg.v > new_min)
1553 break;
1554 p = reconCtrlPtr->headSepCBList;
1555 reconCtrlPtr->headSepCBList = p->next;
1556 p->next = NULL;
1557 rf_CauseReconEvent(raidPtr, p->col, NULL, RF_REVENT_HEADSEPCLEAR);
1558 rf_FreeCallbackDesc(p);
1559 }
1560
1561 }
1562 RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
1563 reconCtrlPtr->rb_lock = 0;
1564 wakeup(&reconCtrlPtr->rb_lock);
1565 RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
1566 }
1567
1568 /*
1569 * checks to see that the maximum head separation will not be violated
1570 * if we initiate a reconstruction I/O on the indicated disk.
1571 * Limiting the maximum head separation between two disks eliminates
1572 * the nasty buffer-stall conditions that occur when one disk races
1573 * ahead of the others and consumes all of the floating recon buffers.
1574 * This code is complex and unpleasant but it's necessary to avoid
1575 * some very nasty, albeit fairly rare, reconstruction behavior.
1576 *
1577 * returns non-zero if and only if we have to stop working on the
1578 * indicated disk due to a head-separation delay.
1579 */
1580 static int
1581 CheckHeadSeparation(RF_Raid_t *raidPtr, RF_PerDiskReconCtrl_t *ctrl,
1582 RF_RowCol_t col, RF_HeadSepLimit_t hsCtr,
1583 RF_ReconUnitNum_t which_ru)
1584 {
1585 RF_ReconCtrl_t *reconCtrlPtr = raidPtr->reconControl;
1586 RF_CallbackDesc_t *cb, *p, *pt;
1587 int retval = 0;
1588
1589 /* if we're too far ahead of the slowest disk, stop working on this
1590 * disk until the slower ones catch up. We do this by scheduling a
1591 * wakeup callback for the time when the slowest disk has caught up.
1592 * We define "caught up" with 20% hysteresis, i.e. the head separation
1593 * must have fallen to at most 80% of the max allowable head
1594 * separation before we'll wake up.
1595 *
1596 */
1597 RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
1598 while(reconCtrlPtr->rb_lock) {
1599 ltsleep(&reconCtrlPtr->rb_lock, PRIBIO, "reconctlchs", 0, &reconCtrlPtr->rb_mutex);
1600 }
1601 reconCtrlPtr->rb_lock = 1;
1602 RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
1603 if ((raidPtr->headSepLimit >= 0) &&
1604 ((ctrl->headSepCounter - reconCtrlPtr->minHeadSepCounter) > raidPtr->headSepLimit)) {
1605 Dprintf5("raid%d: RECON: head sep stall: col %d hsCtr %ld minHSCtr %ld limit %ld\n",
1606 raidPtr->raidid, col, ctrl->headSepCounter,
1607 reconCtrlPtr->minHeadSepCounter,
1608 raidPtr->headSepLimit);
1609 cb = rf_AllocCallbackDesc();
1610 /* the minHeadSepCounter value we have to get to before we'll
1611 * wake up. build in 20% hysteresis. */
1612 cb->callbackArg.v = (ctrl->headSepCounter - raidPtr->headSepLimit + raidPtr->headSepLimit / 5);
1613 cb->col = col;
1614 cb->next = NULL;
1615
1616 /* insert this callback descriptor into the sorted list of
1617 * pending head-sep callbacks */
1618 p = reconCtrlPtr->headSepCBList;
1619 if (!p)
1620 reconCtrlPtr->headSepCBList = cb;
1621 else
1622 if (cb->callbackArg.v < p->callbackArg.v) {
1623 cb->next = reconCtrlPtr->headSepCBList;
1624 reconCtrlPtr->headSepCBList = cb;
1625 } else {
1626 for (pt = p, p = p->next; p && (p->callbackArg.v < cb->callbackArg.v); pt = p, p = p->next);
1627 cb->next = p;
1628 pt->next = cb;
1629 }
1630 retval = 1;
1631 #if RF_RECON_STATS > 0
1632 ctrl->reconCtrl->reconDesc->hsStallCount++;
1633 #endif /* RF_RECON_STATS > 0 */
1634 }
1635 RF_LOCK_MUTEX(reconCtrlPtr->rb_mutex);
1636 reconCtrlPtr->rb_lock = 0;
1637 wakeup(&reconCtrlPtr->rb_lock);
1638 RF_UNLOCK_MUTEX(reconCtrlPtr->rb_mutex);
1639
1640 return (retval);
1641 }
1642 /*
1643 * checks to see if reconstruction has been either forced or blocked
1644 * by a user operation. if forced, we skip this RU entirely. else if
1645 * blocked, put ourselves on the wait list. else return 0.
1646 *
1647 * ASSUMES THE PSS MUTEX IS LOCKED UPON ENTRY
1648 */
1649 static int
1650 CheckForcedOrBlockedReconstruction(RF_Raid_t *raidPtr,
1651 RF_ReconParityStripeStatus_t *pssPtr,
1652 RF_PerDiskReconCtrl_t *ctrl,
1653 RF_RowCol_t col,
1654 RF_StripeNum_t psid,
1655 RF_ReconUnitNum_t which_ru)
1656 {
1657 RF_CallbackDesc_t *cb;
1658 int retcode = 0;
1659
1660 if ((pssPtr->flags & RF_PSS_FORCED_ON_READ) || (pssPtr->flags & RF_PSS_FORCED_ON_WRITE))
1661 retcode = RF_PSS_FORCED_ON_WRITE;
1662 else
1663 if (pssPtr->flags & RF_PSS_RECON_BLOCKED) {
1664 Dprintf3("RECON: col %d blocked at psid %ld ru %d\n", col, psid, which_ru);
1665 cb = rf_AllocCallbackDesc(); /* append ourselves to
1666 * the blockage-wait
1667 * list */
1668 cb->col = col;
1669 cb->next = pssPtr->blockWaitList;
1670 pssPtr->blockWaitList = cb;
1671 retcode = RF_PSS_RECON_BLOCKED;
1672 }
1673 if (!retcode)
1674 pssPtr->flags |= RF_PSS_UNDER_RECON; /* mark this RU as under
1675 * reconstruction */
1676
1677 return (retcode);
1678 }
1679 /*
1680 * if reconstruction is currently ongoing for the indicated stripeID,
1681 * reconstruction is forced to completion and we return non-zero to
1682 * indicate that the caller must wait. If not, then reconstruction is
1683 * blocked on the indicated stripe and the routine returns zero. If
1684 * and only if we return non-zero, we'll cause the cbFunc to get
1685 * invoked with the cbArg when the reconstruction has completed.
1686 */
1687 int
1688 rf_ForceOrBlockRecon(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap,
1689 void (*cbFunc)(RF_Raid_t *, void *), void *cbArg)
1690 {
1691 RF_StripeNum_t stripeID = asmap->stripeID; /* the stripe ID we're
1692 * forcing recon on */
1693 RF_SectorCount_t sectorsPerRU = raidPtr->Layout.sectorsPerStripeUnit * raidPtr->Layout.SUsPerRU; /* num sects in one RU */
1694 RF_ReconParityStripeStatus_t *pssPtr, *newpssPtr; /* a pointer to the parity
1695 * stripe status structure */
1696 RF_StripeNum_t psid; /* parity stripe id */
1697 RF_SectorNum_t offset, fd_offset; /* disk offset, failed-disk
1698 * offset */
1699 RF_RowCol_t *diskids;
1700 RF_ReconUnitNum_t which_ru; /* RU within parity stripe */
1701 RF_RowCol_t fcol, diskno, i;
1702 RF_ReconBuffer_t *new_rbuf; /* ptr to newly allocated rbufs */
1703 RF_DiskQueueData_t *req;/* disk I/O req to be enqueued */
1704 RF_CallbackDesc_t *cb;
1705 int nPromoted;
1706
1707 psid = rf_MapStripeIDToParityStripeID(&raidPtr->Layout, stripeID, &which_ru);
1708
1709 /* allocate a new PSS in case we need it */
1710 newpssPtr = rf_AllocPSStatus(raidPtr);
1711
1712 RF_LOCK_PSS_MUTEX(raidPtr, psid);
1713
1714 pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl->pssTable, psid, which_ru, RF_PSS_CREATE | RF_PSS_RECON_BLOCKED, newpssPtr);
1715
1716 if (pssPtr != newpssPtr) {
1717 rf_FreePSStatus(raidPtr, newpssPtr);
1718 }
1719
1720 /* if recon is not ongoing on this PS, just return */
1721 if (!(pssPtr->flags & RF_PSS_UNDER_RECON)) {
1722 RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
1723 return (0);
1724 }
1725 /* otherwise, we have to wait for reconstruction to complete on this
1726 * RU. */
1727 /* In order to avoid waiting for a potentially large number of
1728 * low-priority accesses to complete, we force a normal-priority (i.e.
1729 * not low-priority) reconstruction on this RU. */
1730 if (!(pssPtr->flags & RF_PSS_FORCED_ON_WRITE) && !(pssPtr->flags & RF_PSS_FORCED_ON_READ)) {
1731 DDprintf1("Forcing recon on psid %ld\n", psid);
1732 pssPtr->flags |= RF_PSS_FORCED_ON_WRITE; /* mark this RU as under
1733 * forced recon */
1734 pssPtr->flags &= ~RF_PSS_RECON_BLOCKED; /* clear the blockage
1735 * that we just set */
1736 fcol = raidPtr->reconControl->fcol;
1737
1738 /* get a listing of the disks comprising the indicated stripe */
1739 (raidPtr->Layout.map->IdentifyStripe) (raidPtr, asmap->raidAddress, &diskids);
1740
1741 /* For previously issued reads, elevate them to normal
1742 * priority. If the I/O has already completed, it won't be
1743 * found in the queue, and hence this will be a no-op. For
1744 * unissued reads, allocate buffers and issue new reads. The
1745 * fact that we've set the FORCED bit means that the regular
1746 * recon procs will not re-issue these reqs */
1747 for (i = 0; i < raidPtr->Layout.numDataCol + raidPtr->Layout.numParityCol; i++)
1748 if ((diskno = diskids[i]) != fcol) {
1749 if (pssPtr->issued[diskno]) {
1750 nPromoted = rf_DiskIOPromote(&raidPtr->Queues[diskno], psid, which_ru);
1751 if (rf_reconDebug && nPromoted)
1752 printf("raid%d: promoted read from col %d\n", raidPtr->raidid, diskno);
1753 } else {
1754 new_rbuf = rf_MakeReconBuffer(raidPtr, diskno, RF_RBUF_TYPE_FORCED); /* create new buf */
1755 ComputePSDiskOffsets(raidPtr, psid, diskno, &offset, &fd_offset,
1756 &new_rbuf->spCol, &new_rbuf->spOffset); /* find offsets & spare
1757 * location */
1758 new_rbuf->parityStripeID = psid; /* fill in the buffer */
1759 new_rbuf->which_ru = which_ru;
1760 new_rbuf->failedDiskSectorOffset = fd_offset;
1761 new_rbuf->priority = RF_IO_NORMAL_PRIORITY;
1762
1763 /* use NULL b_proc b/c all addrs
1764 * should be in kernel space */
1765 req = rf_CreateDiskQueueData(RF_IO_TYPE_READ, offset + which_ru * sectorsPerRU, sectorsPerRU, new_rbuf->buffer,
1766 psid, which_ru, (int (*) (void *, int)) ForceReconReadDoneProc, (void *) new_rbuf,
1767 NULL, (void *) raidPtr, 0, NULL, PR_WAITOK);
1768
1769 new_rbuf->arg = req;
1770 rf_DiskIOEnqueue(&raidPtr->Queues[diskno], req, RF_IO_NORMAL_PRIORITY); /* enqueue the I/O */
1771 Dprintf2("raid%d: Issued new read req on col %d\n", raidPtr->raidid, diskno);
1772 }
1773 }
1774 /* if the write is sitting in the disk queue, elevate its
1775 * priority */
1776 if (rf_DiskIOPromote(&raidPtr->Queues[fcol], psid, which_ru))
1777 printf("raid%d: promoted write to col %d\n",
1778 raidPtr->raidid, fcol);
1779 }
1780 /* install a callback descriptor to be invoked when recon completes on
1781 * this parity stripe. */
1782 cb = rf_AllocCallbackDesc();
1783 /* XXX the following is bogus.. These functions don't really match!!
1784 * GO */
1785 cb->callbackFunc = (void (*) (RF_CBParam_t)) cbFunc;
1786 cb->callbackArg.p = (void *) cbArg;
1787 cb->next = pssPtr->procWaitList;
1788 pssPtr->procWaitList = cb;
1789 DDprintf2("raid%d: Waiting for forced recon on psid %ld\n",
1790 raidPtr->raidid, psid);
1791
1792 RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
1793 return (1);
1794 }
1795 /* called upon the completion of a forced reconstruction read.
1796 * all we do is schedule the FORCEDREADONE event.
1797 * called at interrupt context in the kernel, so don't do anything illegal here.
1798 */
1799 static void
1800 ForceReconReadDoneProc(void *arg, int status)
1801 {
1802 RF_ReconBuffer_t *rbuf = arg;
1803
1804 /* Detect that reconControl is no longer valid, and if that
1805 is the case, bail without calling rf_CauseReconEvent().
1806 There won't be anyone listening for this event anyway */
1807
1808 if (rbuf->raidPtr->reconControl == NULL)
1809 return;
1810
1811 if (status) {
1812 printf("raid%d: Forced recon read failed!\n", rbuf->raidPtr->raidid);
1813 rf_CauseReconEvent(rbuf->raidPtr, rbuf->col, (void *) rbuf, RF_REVENT_FORCEDREAD_FAILED);
1814 return;
1815 }
1816 rf_CauseReconEvent(rbuf->raidPtr, rbuf->col, (void *) rbuf, RF_REVENT_FORCEDREADDONE);
1817 }
1818 /* releases a block on the reconstruction of the indicated stripe */
1819 int
1820 rf_UnblockRecon(RF_Raid_t *raidPtr, RF_AccessStripeMap_t *asmap)
1821 {
1822 RF_StripeNum_t stripeID = asmap->stripeID;
1823 RF_ReconParityStripeStatus_t *pssPtr;
1824 RF_ReconUnitNum_t which_ru;
1825 RF_StripeNum_t psid;
1826 RF_CallbackDesc_t *cb;
1827
1828 psid = rf_MapStripeIDToParityStripeID(&raidPtr->Layout, stripeID, &which_ru);
1829 RF_LOCK_PSS_MUTEX(raidPtr, psid);
1830 pssPtr = rf_LookupRUStatus(raidPtr, raidPtr->reconControl->pssTable, psid, which_ru, RF_PSS_NONE, NULL);
1831
1832 /* When recon is forced, the pss desc can get deleted before we get
1833 * back to unblock recon. But, this can _only_ happen when recon is
1834 * forced. It would be good to put some kind of sanity check here, but
1835 * how to decide if recon was just forced or not? */
1836 if (!pssPtr) {
1837 /* printf("Warning: no pss descriptor upon unblock on psid %ld
1838 * RU %d\n",psid,which_ru); */
1839 #if (RF_DEBUG_RECON > 0) || (RF_DEBUG_PSS > 0)
1840 if (rf_reconDebug || rf_pssDebug)
1841 printf("Warning: no pss descriptor upon unblock on psid %ld RU %d\n", (long) psid, which_ru);
1842 #endif
1843 goto out;
1844 }
1845 pssPtr->blockCount--;
1846 Dprintf3("raid%d: unblocking recon on psid %ld: blockcount is %d\n",
1847 raidPtr->raidid, psid, pssPtr->blockCount);
1848 if (pssPtr->blockCount == 0) { /* if recon blockage has been released */
1849
1850 /* unblock recon before calling CauseReconEvent in case
1851 * CauseReconEvent causes us to try to issue a new read before
1852 * returning here. */
1853 pssPtr->flags &= ~RF_PSS_RECON_BLOCKED;
1854
1855
1856 while (pssPtr->blockWaitList) {
1857 /* spin through the block-wait list and
1858 release all the waiters */
1859 cb = pssPtr->blockWaitList;
1860 pssPtr->blockWaitList = cb->next;
1861 cb->next = NULL;
1862 rf_CauseReconEvent(raidPtr, cb->col, NULL, RF_REVENT_BLOCKCLEAR);
1863 rf_FreeCallbackDesc(cb);
1864 }
1865 if (!(pssPtr->flags & RF_PSS_UNDER_RECON)) {
1866 /* if no recon was requested while recon was blocked */
1867 rf_PSStatusDelete(raidPtr, raidPtr->reconControl->pssTable, pssPtr);
1868 }
1869 }
1870 out:
1871 RF_UNLOCK_PSS_MUTEX(raidPtr, psid);
1872 return (0);
1873 }
1874
1875 void
1876 rf_WakeupHeadSepCBWaiters(RF_Raid_t *raidPtr)
1877 {
1878 RF_CallbackDesc_t *p;
1879
1880 RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
1881 while(raidPtr->reconControl->rb_lock) {
1882 ltsleep(&raidPtr->reconControl->rb_lock, PRIBIO,
1883 "rf_wakeuphscbw", 0, &raidPtr->reconControl->rb_mutex);
1884 }
1885
1886 raidPtr->reconControl->rb_lock = 1;
1887 RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
1888
1889 while (raidPtr->reconControl->headSepCBList) {
1890 p = raidPtr->reconControl->headSepCBList;
1891 raidPtr->reconControl->headSepCBList = p->next;
1892 p->next = NULL;
1893 rf_CauseReconEvent(raidPtr, p->col, NULL, RF_REVENT_HEADSEPCLEAR);
1894 rf_FreeCallbackDesc(p);
1895 }
1896 RF_LOCK_MUTEX(raidPtr->reconControl->rb_mutex);
1897 raidPtr->reconControl->rb_lock = 0;
1898 wakeup(&raidPtr->reconControl->rb_lock);
1899 RF_UNLOCK_MUTEX(raidPtr->reconControl->rb_mutex);
1900
1901 }
1902
1903