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