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