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