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