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