1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * NFS Version 4 state recovery code.
31  */
32 
33 #include <nfs/nfs4_clnt.h>
34 #include <nfs/nfs4.h>
35 #include <nfs/rnode4.h>
36 #include <sys/cmn_err.h>
37 #include <sys/cred.h>
38 #include <sys/systm.h>
39 #include <sys/flock.h>
40 #include <sys/dnlc.h>
41 #include <sys/ddi.h>
42 #include <sys/disp.h>
43 #include <sys/list.h>
44 #include <sys/sdt.h>
45 
46 extern r4hashq_t *rtable4;
47 
48 /*
49  * Information that describes what needs to be done for recovery.  It is
50  * passed to a client recovery thread as well as passed to various recovery
51  * routines.  rc_mi, rc_vp1, and rc_vp2 refer to the filesystem and
52  * vnode(s) affected by recovery.  rc_vp1 and rc_vp2 are references (use
53  * VN_HOLD) or NULL.  rc_lost_rqst contains information about the lost
54  * lock or open/close request, and it holds reference counts for the
55  * various objects (vnode, etc.).  The recovery thread also uses flags set
56  * in the mntinfo4_t or vnode_t to tell it what to do.  rc_error is used
57  * to save the error that originally triggered the recovery event -- will
58  * later be used to set mi_error if recovery doesn't work.  rc_bseqid_rqst
59  * contains information about the request that got NFS4ERR_BAD_SEQID, and
60  * it holds reference count for the various objects (vnode, open owner,
61  * open stream, lock owner).
62  */
63 
64 typedef struct {
65 	mntinfo4_t *rc_mi;
66 	vnode_t *rc_vp1;
67 	vnode_t *rc_vp2;
68 	nfs4_recov_t rc_action;
69 	stateid4 rc_stateid;
70 	bool_t rc_srv_reboot;		/* server has rebooted */
71 	nfs4_lost_rqst_t *rc_lost_rqst;
72 	nfs4_error_t rc_orig_errors;	/* original errors causing recovery */
73 	int rc_error;
74 	nfs4_bseqid_entry_t *rc_bseqid_rqst;
75 } recov_info_t;
76 
77 /*
78  * How long to wait before trying again if there is an error doing
79  * recovery, in seconds.
80  */
81 
82 static int recov_err_delay = 1;
83 
84 /*
85  * How long to wait when processing NFS4ERR_GRACE or NFS4ERR_DELAY
86  * errors.  Expressed in seconds.  Default is defined as
87  * NFS4ERR_DELAY_TIME and this variable is initialized in nfs4_subr_init()
88  */
89 time_t nfs4err_delay_time = 0;
90 
91 /*
92  * Tuneable to limit how many time "exempt" ops go OTW
93  * after a recovery error.  Exempt op hints are OH_CLOSE,
94  * OH_LOCKU, OH_DELEGRETURN.  These previously always went
95  * OTW even after rnode was "dead" due to recovery errors.
96  *
97  * The tuneable below limits the number of times a start_fop
98  * invocation will retry the exempt hints.  After the limit
99  * is reached, nfs4_start_fop will return an error just like
100  * it would for non-exempt op hints.
101  */
102 int nfs4_max_recov_error_retry = 3;
103 
104 /*
105  * Number of seconds the recovery thread should pause before retry when the
106  * filesystem has been forcibly unmounted.
107  */
108 
109 int nfs4_unmount_delay = 1;
110 
111 #ifdef DEBUG
112 
113 /*
114  * How long to wait (in seconds) between recovery operations on a given
115  * file.  Normally zero, but could be set longer for testing purposes.
116  */
117 static int nfs4_recovdelay = 0;
118 
119 /*
120  * Switch that controls whether to go into the debugger when recovery
121  * fails.
122  */
123 static int nfs4_fail_recov_stop = 0;
124 
125 /*
126  * Tuneables to debug client namespace interaction with server
127  * mount points:
128  *
129  *	nfs4_srvmnt_fail_cnt:
130  *		number of times EACCES returned because client
131  *		attempted to cross server mountpoint
132  *
133  *	nfs4_srvmnt_debug:
134  *		trigger console printf whenever client attempts
135  *		to cross server mountpoint
136  */
137 int nfs4_srvmnt_fail_cnt = 0;
138 int nfs4_srvmnt_debug = 0;
139 #endif
140 
141 /* forward references, in alphabetic order */
142 static void close_after_open_resend(vnode_t *, cred_t *, uint32_t,
143 	nfs4_error_t *);
144 static void errs_to_action(recov_info_t *,
145 	nfs4_server_t *, mntinfo4_t *, stateid4 *, nfs4_lost_rqst_t *, int,
146 	nfs_opnum4, nfs4_bseqid_entry_t *);
147 static void flush_reinstate(nfs4_lost_rqst_t *);
148 static void free_milist(mntinfo4_t **, int);
149 static mntinfo4_t **make_milist(nfs4_server_t *, int *);
150 static int nfs4_check_recov_err(vnode_t *, nfs4_op_hint_t,
151 	nfs4_recov_state_t *, int, char *);
152 static int nfs4_check_srvstub(vnode_t *vp, rnode4_t *rp, nfs4_op_hint_t op);
153 static char *nfs4_getsrvnames(mntinfo4_t *, size_t *);
154 static void nfs4_recov_fh_fail(vnode_t *, int, nfsstat4);
155 static void nfs4_recov_thread(recov_info_t *);
156 static void nfs4_remove_lost_rqsts(mntinfo4_t *, nfs4_server_t *);
157 static void nfs4_resend_lost_rqsts(recov_info_t *, nfs4_server_t *);
158 static cred_t *pid_to_cr(pid_t);
159 static void reclaim_one_lock(vnode_t *, flock64_t *, nfs4_error_t *, int *);
160 static void recov_bad_seqid(recov_info_t *);
161 static void recov_badstate(recov_info_t *, vnode_t *, nfsstat4);
162 static void recov_clientid(recov_info_t *, nfs4_server_t *);
163 static void recov_done(mntinfo4_t *, recov_info_t *);
164 static void recov_filehandle(nfs4_recov_t, mntinfo4_t *, vnode_t *);
165 static void recov_newserver(recov_info_t *, nfs4_server_t **, bool_t *);
166 static void recov_openfiles(recov_info_t *, nfs4_server_t *);
167 static void recov_stale(mntinfo4_t *, vnode_t *);
168 static void nfs4_free_lost_rqst(nfs4_lost_rqst_t *, nfs4_server_t *);
169 static void recov_throttle(recov_info_t *, vnode_t *);
170 static void relock_skip_pid(locklist_t *, pid_t);
171 static void resend_lock(nfs4_lost_rqst_t *, nfs4_error_t *);
172 static void resend_one_op(nfs4_lost_rqst_t *, nfs4_error_t *, mntinfo4_t *,
173 	nfs4_server_t *);
174 static void save_bseqid_rqst(nfs4_bseqid_entry_t *, recov_info_t *);
175 static void start_recovery(recov_info_t *, mntinfo4_t *, vnode_t *, vnode_t *,
176 	nfs4_server_t *);
177 static void start_recovery_action(nfs4_recov_t, bool_t, mntinfo4_t *, vnode_t *,
178 	vnode_t *);
179 static int wait_for_recovery(mntinfo4_t *, nfs4_op_hint_t);
180 
181 /*
182  * Return non-zero if the given errno, status, and rpc status codes
183  * in the nfs4_error_t indicate that client recovery is needed.
184  * "stateful" indicates whether the call that got the error establishes or
185  * removes state on the server (open, close, lock, unlock, delegreturn).
186  */
187 
188 int
189 nfs4_needs_recovery(nfs4_error_t *ep, bool_t stateful, vfs_t *vfsp)
190 {
191 	int recov = 0;
192 	mntinfo4_t *mi;
193 
194 	/*
195 	 * Try failover if the error values justify it and if
196 	 * it's a failover mount.  Don't try if the mount is in
197 	 * progress, failures are handled explicitly by nfs4rootvp.
198 	 */
199 	if (nfs4_try_failover(ep)) {
200 		mi = VFTOMI4(vfsp);
201 		mutex_enter(&mi->mi_lock);
202 		recov = FAILOVER_MOUNT4(mi) && !(mi->mi_flags & MI4_MOUNTING);
203 		mutex_exit(&mi->mi_lock);
204 		if (recov)
205 			return (recov);
206 	}
207 
208 	if (ep->error == EINTR || NFS4_FRC_UNMT_ERR(ep->error, vfsp)) {
209 		/*
210 		 * The server may have gotten the request, so for stateful
211 		 * ops we need to resynchronize and possibly back out the
212 		 * op.
213 		 */
214 		return (stateful);
215 	}
216 	if (ep->error != 0)
217 		return (0);
218 
219 	/* stat values are listed alphabetically */
220 	/*
221 	 * There are two lists here: the errors for which we have code, and
222 	 * the errors for which we plan to have code before FCS.  For the
223 	 * second list, print a warning message but don't attempt recovery.
224 	 */
225 	switch (ep->stat) {
226 	case NFS4ERR_BADHANDLE:
227 	case NFS4ERR_BAD_SEQID:
228 	case NFS4ERR_BAD_STATEID:
229 	case NFS4ERR_DELAY:
230 	case NFS4ERR_EXPIRED:
231 	case NFS4ERR_FHEXPIRED:
232 	case NFS4ERR_GRACE:
233 	case NFS4ERR_OLD_STATEID:
234 	case NFS4ERR_RESOURCE:
235 	case NFS4ERR_STALE_CLIENTID:
236 	case NFS4ERR_STALE_STATEID:
237 	case NFS4ERR_WRONGSEC:
238 	case NFS4ERR_STALE:
239 		recov = 1;
240 		break;
241 #ifdef DEBUG
242 	case NFS4ERR_LEASE_MOVED:
243 	case NFS4ERR_MOVED:
244 		zcmn_err(VFTOMI4(vfsp)->mi_zone->zone_id,
245 		    CE_WARN, "!Can't yet recover from NFS status %d",
246 				ep->stat);
247 		break;
248 #endif
249 	}
250 
251 	return (recov);
252 }
253 
254 /*
255  * Some operations such as DELEGRETURN want to avoid invoking
256  * recovery actions that will only mark the file dead.  If
257  * better handlers are invoked for any of these errors, this
258  * routine should be modified.
259  */
260 int
261 nfs4_recov_marks_dead(nfsstat4 status)
262 {
263 	if (status == NFS4ERR_BAD_SEQID ||
264 	    status == NFS4ERR_EXPIRED ||
265 	    status == NFS4ERR_BAD_STATEID ||
266 	    status == NFS4ERR_OLD_STATEID)
267 		return (1);
268 	return (0);
269 }
270 
271 /*
272  * Transfer the state recovery information in recovp to mi's resend queue,
273  * and mark mi as having a lost state request.
274  */
275 static void
276 nfs4_enqueue_lost_rqst(recov_info_t *recovp, mntinfo4_t *mi)
277 {
278 	nfs4_lost_rqst_t *lrp = recovp->rc_lost_rqst;
279 
280 	ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) ||
281 	    nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
282 
283 	ASSERT(lrp != NULL && lrp->lr_op != 0);
284 
285 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
286 		"nfs4_enqueue_lost_rqst %p, op %d",
287 		(void *)lrp, lrp->lr_op));
288 
289 	mutex_enter(&mi->mi_lock);
290 	mi->mi_recovflags |= MI4R_LOST_STATE;
291 	if (lrp->lr_putfirst)
292 		list_insert_head(&mi->mi_lost_state, lrp);
293 	else
294 		list_insert_tail(&mi->mi_lost_state, lrp);
295 	recovp->rc_lost_rqst = NULL;
296 	mutex_exit(&mi->mi_lock);
297 
298 	nfs4_queue_event(RE_LOST_STATE, mi, NULL, lrp->lr_op, lrp->lr_vp,
299 		lrp->lr_dvp, 0, NULL, 0, TAG_NONE, TAG_NONE, 0, 0);
300 }
301 
302 /*
303  * Transfer the bad seqid recovery information in recovp to mi's
304  * bad seqid queue, and mark mi as having a bad seqid request.
305  */
306 void
307 enqueue_bseqid_rqst(recov_info_t *recovp, mntinfo4_t *mi)
308 {
309 	ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) ||
310 	    nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
311 	ASSERT(recovp->rc_bseqid_rqst != NULL);
312 
313 	mutex_enter(&mi->mi_lock);
314 	mi->mi_recovflags |= MI4R_BAD_SEQID;
315 	list_insert_tail(&mi->mi_bseqid_list, recovp->rc_bseqid_rqst);
316 	recovp->rc_bseqid_rqst = NULL;
317 	mutex_exit(&mi->mi_lock);
318 }
319 
320 /*
321  * Initiate recovery.
322  *
323  * The nfs4_error_t contains the return codes that triggered a recovery
324  * attempt.  mi, vp1, and vp2 refer to the filesystem and files that were
325  * being operated on.  vp1 and vp2 may be NULL.
326  *
327  * Multiple calls are okay.  If recovery is already underway, the call
328  * updates the information about what state needs recovery but does not
329  * start a new thread.  The caller should hold mi->mi_recovlock as a reader
330  * for proper synchronization with any recovery thread.
331  *
332  * This will return TRUE if recovery was aborted, and FALSE otherwise.
333  */
334 bool_t
335 nfs4_start_recovery(nfs4_error_t *ep, mntinfo4_t *mi, vnode_t *vp1,
336     vnode_t *vp2, stateid4 *sid, nfs4_lost_rqst_t *lost_rqstp, nfs_opnum4 op,
337     nfs4_bseqid_entry_t *bsep)
338 {
339 	recov_info_t *recovp;
340 	nfs4_server_t *sp;
341 	bool_t abort = FALSE;
342 	bool_t gone = FALSE;
343 
344 	ASSERT(nfs_zone() == mi->mi_zone);
345 	mutex_enter(&mi->mi_lock);
346 	/*
347 	 * If there is lost state, we need to kick off recovery even if the
348 	 * filesystem has been unmounted or the zone is shutting down.
349 	 */
350 	gone = FS_OR_ZONE_GONE4(mi->mi_vfsp);
351 	if (gone) {
352 		ASSERT(ep->error != EINTR || lost_rqstp != NULL);
353 		if (ep->error == EIO && lost_rqstp == NULL) {
354 			/* failed due to forced unmount, no new lost state */
355 			abort = TRUE;
356 		}
357 		if ((ep->error == 0 || ep->error == ETIMEDOUT) &&
358 		    !(mi->mi_recovflags & MI4R_LOST_STATE)) {
359 			/* some other failure, no existing lost state */
360 			abort = TRUE;
361 		}
362 		if (abort) {
363 			mutex_exit(&mi->mi_lock);
364 			NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
365 				    "nfs4_start_recovery: fs unmounted"));
366 			return (TRUE);
367 		}
368 	}
369 	mi->mi_in_recovery++;
370 	mutex_exit(&mi->mi_lock);
371 
372 	recovp = kmem_alloc(sizeof (recov_info_t), KM_SLEEP);
373 	recovp->rc_orig_errors = *ep;
374 	sp = find_nfs4_server(mi);
375 	errs_to_action(recovp, sp, mi, sid, lost_rqstp,
376 		gone, op, bsep);
377 	if (sp != NULL)
378 		mutex_exit(&sp->s_lock);
379 	start_recovery(recovp, mi, vp1, vp2, sp);
380 	if (sp != NULL)
381 		nfs4_server_rele(sp);
382 	return (FALSE);
383 }
384 
385 /*
386  * Internal version of nfs4_start_recovery.  The difference is that the
387  * caller specifies the recovery action, rather than the errors leading to
388  * recovery.
389  */
390 static void
391 start_recovery_action(nfs4_recov_t what, bool_t reboot, mntinfo4_t *mi,
392 	vnode_t *vp1, vnode_t *vp2)
393 {
394 	recov_info_t *recovp;
395 
396 	ASSERT(nfs_zone() == mi->mi_zone);
397 	mutex_enter(&mi->mi_lock);
398 	mi->mi_in_recovery++;
399 	mutex_exit(&mi->mi_lock);
400 
401 	recovp = kmem_zalloc(sizeof (recov_info_t), KM_SLEEP);
402 	recovp->rc_action = what;
403 	recovp->rc_srv_reboot = reboot;
404 	recovp->rc_error = EIO;
405 	start_recovery(recovp, mi, vp1, vp2, NULL);
406 }
407 
408 static void
409 start_recovery(recov_info_t *recovp, mntinfo4_t *mi,
410 	vnode_t *vp1, vnode_t *vp2, nfs4_server_t *sp)
411 {
412 	NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
413 		"start_recovery: mi %p, what %s", (void*)mi,
414 		nfs4_recov_action_to_str(recovp->rc_action)));
415 
416 	/*
417 	 * Bump the reference on the vfs so that we can pass it to the
418 	 * recovery thread.
419 	 */
420 	VFS_HOLD(mi->mi_vfsp);
421 
422 again:
423 	switch (recovp->rc_action) {
424 	case NR_FAILOVER:
425 		ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) ||
426 		    nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
427 		if (mi->mi_servers->sv_next == NULL)
428 			goto out_no_thread;
429 		mutex_enter(&mi->mi_lock);
430 		mi->mi_recovflags |= MI4R_NEED_NEW_SERVER;
431 		mutex_exit(&mi->mi_lock);
432 
433 		if (recovp->rc_lost_rqst != NULL)
434 			nfs4_enqueue_lost_rqst(recovp, mi);
435 		break;
436 
437 	case NR_CLIENTID:
438 		/*
439 		 * If the filesystem has been unmounted, punt.
440 		 */
441 		if (sp == NULL)
442 			goto out_no_thread;
443 
444 		/*
445 		 * If nobody else is working on the clientid, mark the
446 		 * clientid as being no longer set.  Then mark the specific
447 		 * filesystem being worked on.
448 		 */
449 		if (!nfs4_server_in_recovery(sp)) {
450 			mutex_enter(&sp->s_lock);
451 			sp->s_flags &= ~N4S_CLIENTID_SET;
452 			mutex_exit(&sp->s_lock);
453 		}
454 		ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) ||
455 		    nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
456 		mutex_enter(&mi->mi_lock);
457 		mi->mi_recovflags |= MI4R_NEED_CLIENTID;
458 		if (recovp->rc_srv_reboot)
459 			mi->mi_recovflags |= MI4R_SRV_REBOOT;
460 		mutex_exit(&mi->mi_lock);
461 		break;
462 
463 	case NR_OPENFILES:
464 		ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) ||
465 		    nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
466 		mutex_enter(&mi->mi_lock);
467 		mi->mi_recovflags |= MI4R_REOPEN_FILES;
468 		if (recovp->rc_srv_reboot)
469 			mi->mi_recovflags |= MI4R_SRV_REBOOT;
470 		mutex_exit(&mi->mi_lock);
471 		break;
472 
473 	case NR_WRONGSEC:
474 		ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) ||
475 		    nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
476 		mutex_enter(&mi->mi_lock);
477 		mi->mi_recovflags |= MI4R_NEED_SECINFO;
478 		mutex_exit(&mi->mi_lock);
479 		break;
480 
481 	case NR_EXPIRED:
482 		if (vp1 != NULL)
483 			recov_badstate(recovp, vp1, NFS4ERR_EXPIRED);
484 		if (vp2 != NULL)
485 			recov_badstate(recovp, vp2, NFS4ERR_EXPIRED);
486 		goto out_no_thread;	/* no further recovery possible */
487 
488 	case NR_BAD_STATEID:
489 		if (vp1 != NULL)
490 			recov_badstate(recovp, vp1, NFS4ERR_BAD_STATEID);
491 		if (vp2 != NULL)
492 			recov_badstate(recovp, vp2, NFS4ERR_BAD_STATEID);
493 		goto out_no_thread;	/* no further recovery possible */
494 
495 	case NR_FHEXPIRED:
496 	case NR_BADHANDLE:
497 		if (vp1 != NULL)
498 			recov_throttle(recovp, vp1);
499 		if (vp2 != NULL)
500 			recov_throttle(recovp, vp2);
501 		/*
502 		 * Recover the filehandle now, rather than using a
503 		 * separate thread.  We can do this because filehandle
504 		 * recovery is independent of any other state, and because
505 		 * we know that we are not competing with the recovery
506 		 * thread at this time.  recov_filehandle will deal with
507 		 * threads that are competing to recover this filehandle.
508 		 */
509 		ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) ||
510 		    nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
511 		if (vp1 != NULL)
512 			recov_filehandle(recovp->rc_action, mi, vp1);
513 		if (vp2 != NULL)
514 			recov_filehandle(recovp->rc_action, mi, vp2);
515 		goto out_no_thread;	/* no further recovery needed */
516 
517 	case NR_STALE:
518 		/*
519 		 * NFS4ERR_STALE handling
520 		 * recov_stale() could set MI4R_NEED_NEW_SERVER to
521 		 * indicate that we can and should failover.
522 		 */
523 		ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) ||
524 		    nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
525 
526 		if (vp1 != NULL)
527 			recov_stale(mi, vp1);
528 		if (vp2 != NULL)
529 			recov_stale(mi, vp2);
530 		mutex_enter(&mi->mi_lock);
531 		if ((mi->mi_recovflags & MI4R_NEED_NEW_SERVER) == 0) {
532 			mutex_exit(&mi->mi_lock);
533 			goto out_no_thread;
534 		}
535 		mutex_exit(&mi->mi_lock);
536 		recovp->rc_action = NR_FAILOVER;
537 		goto again;
538 
539 	case NR_BAD_SEQID:
540 		if (recovp->rc_bseqid_rqst) {
541 			enqueue_bseqid_rqst(recovp, mi);
542 			break;
543 		}
544 
545 		if (vp1 != NULL)
546 			recov_badstate(recovp, vp1, NFS4ERR_BAD_SEQID);
547 		if (vp2 != NULL)
548 			recov_badstate(recovp, vp2, NFS4ERR_BAD_SEQID);
549 		goto out_no_thread; /* no further recovery possible */
550 
551 	case NR_OLDSTATEID:
552 		if (vp1 != NULL)
553 			recov_badstate(recovp, vp1, NFS4ERR_OLD_STATEID);
554 		if (vp2 != NULL)
555 			recov_badstate(recovp, vp2, NFS4ERR_OLD_STATEID);
556 		goto out_no_thread;	/* no further recovery possible */
557 
558 	case NR_GRACE:
559 		nfs4_set_grace_wait(mi);
560 		goto out_no_thread; /* no further action required for GRACE */
561 
562 	case NR_DELAY:
563 		if (vp1)
564 			nfs4_set_delay_wait(vp1);
565 		goto out_no_thread; /* no further action required for DELAY */
566 
567 	case NR_LOST_STATE_RQST:
568 	case NR_LOST_LOCK:
569 		nfs4_enqueue_lost_rqst(recovp, mi);
570 		break;
571 
572 	default:
573 		nfs4_queue_event(RE_UNEXPECTED_ACTION, mi, NULL,
574 		    recovp->rc_action, NULL, NULL, 0, NULL, 0, TAG_NONE,
575 		    TAG_NONE, 0, 0);
576 		goto out_no_thread;
577 	}
578 
579 	/*
580 	 * If either file recently went through the same recovery, wait
581 	 * awhile.  This is in case there is some sort of bug; we might not
582 	 * be able to recover properly, but at least we won't bombard the
583 	 * server with calls, and we won't tie up the client.
584 	 */
585 	if (vp1 != NULL)
586 		recov_throttle(recovp, vp1);
587 	if (vp2 != NULL)
588 		recov_throttle(recovp, vp2);
589 
590 	/*
591 	 * If there's already a recovery thread, don't start another one.
592 	 */
593 
594 	mutex_enter(&mi->mi_lock);
595 	if (mi->mi_flags & MI4_RECOV_ACTIV) {
596 		mutex_exit(&mi->mi_lock);
597 		goto out_no_thread;
598 	}
599 	mi->mi_flags |= MI4_RECOV_ACTIV;
600 	mutex_exit(&mi->mi_lock);
601 	NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
602 		"start_recovery: starting new thread for mi %p", (void*)mi));
603 
604 	recovp->rc_mi = mi;
605 	recovp->rc_vp1 = vp1;
606 	if (vp1 != NULL) {
607 		ASSERT(VTOMI4(vp1) == mi);
608 		VN_HOLD(recovp->rc_vp1);
609 	}
610 	recovp->rc_vp2 = vp2;
611 	if (vp2 != NULL) {
612 		ASSERT(VTOMI4(vp2) == mi);
613 		VN_HOLD(recovp->rc_vp2);
614 	}
615 
616 	(void) zthread_create(NULL, 0, nfs4_recov_thread, recovp, 0,
617 			    minclsyspri);
618 	return;
619 
620 	/* not reached by thread creating call */
621 out_no_thread:
622 	mutex_enter(&mi->mi_lock);
623 	mi->mi_in_recovery--;
624 	if (mi->mi_in_recovery == 0)
625 		cv_broadcast(&mi->mi_cv_in_recov);
626 	mutex_exit(&mi->mi_lock);
627 
628 	VFS_RELE(mi->mi_vfsp);
629 	/*
630 	 * Free up resources that were allocated for us.
631 	 */
632 	kmem_free(recovp, sizeof (recov_info_t));
633 }
634 
635 static int
636 nfs4_check_srvstub(vnode_t *vp, rnode4_t *rp, nfs4_op_hint_t op)
637 {
638 	int err = 0;
639 
640 	/*
641 	 * If tuneable does not allow client to cross srv mountpoints and
642 	 * object is a stub, then check check op hint and return EACCES for
643 	 * any hint other than access, rddir, getattr, lookup.
644 	 */
645 	if (rp->r_flags & R4SRVSTUB && op != OH_ACCESS && op != OH_GETACL &&
646 	    op != OH_GETATTR && op != OH_READDIR && op != OH_LOOKUP) {
647 		err = EACCES;
648 #ifdef DEBUG
649 		NFS4_DEBUG(nfs4_srvmnt_debug, (CE_NOTE,
650 			"nfs4_check_srvstub: op=%d err=%d rp=%p vp=%p\n"
651 			"va_nod=%llx r_mntd_fid=%llx\n"
652 			"sv_fsid=(%llx:%llx) r_srv_fsid=(%llx:%llx)",
653 			op, err, (void *)rp, (void *)vp,
654 			(u_longlong_t)rp->r_attr.va_nodeid,
655 			(u_longlong_t)rp->r_mntd_fid,
656 			(u_longlong_t)rp->r_server->sv_fsid.major,
657 			(u_longlong_t)rp->r_server->sv_fsid.minor,
658 			(u_longlong_t)rp->r_srv_fsid.major,
659 			(u_longlong_t)rp->r_srv_fsid.minor));
660 #endif
661 	}
662 
663 	return (err);
664 }
665 
666 static int
667 nfs4_check_recov_err(vnode_t *vp, nfs4_op_hint_t op,
668 			nfs4_recov_state_t *rsp, int retry_err_cnt, char *str)
669 {
670 	rnode4_t *rp;
671 	int error = 0;
672 	int exempt;
673 
674 	if (vp == NULL)
675 		return (0);
676 
677 	exempt = (op == OH_CLOSE || op == OH_LOCKU || op == OH_DELEGRETURN);
678 	rp = VTOR4(vp);
679 	mutex_enter(&rp->r_statelock);
680 
681 	/*
682 	 * If there was a recovery error, then allow op hints "exempt" from
683 	 * recov errors to retry (currently 3 times).  Either r_error or
684 	 * EIO is returned for non-exempt op hints.
685 	 *
686 	 *	Error heirarchy:
687 	 *	a) check for R4ERECOVERR
688 	 *	b) check for R4SRVSTUB (only if R4RECOVERR is not set).
689 	 */
690 	if (rp->r_flags & R4RECOVERR) {
691 		if (exempt && rsp->rs_num_retry_despite_err <=
692 				nfs4_max_recov_error_retry) {
693 
694 			/*
695 			 * Check to make sure that we haven't already inc'd
696 			 * rs_num_retry_despite_err for current nfs4_start_fop
697 			 * instance.  We don't want to double inc (if we were
698 			 * called with vp2, then the vp1 call could have
699 			 * already incremented.
700 			 */
701 			if (retry_err_cnt == rsp->rs_num_retry_despite_err)
702 				rsp->rs_num_retry_despite_err++;
703 
704 			NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
705 				"nfs4_start_fop: %s %p DEAD, cnt=%d", str,
706 				(void *)vp, rsp->rs_num_retry_despite_err));
707 		} else {
708 			error = (rp->r_error ? rp->r_error : EIO);
709 			/*
710 			 * An ESTALE error on a non-regular file is not
711 			 * "sticky".  Return the ESTALE error once, but
712 			 * clear the condition to allow future operations
713 			 * to go OTW.  This will allow the client to
714 			 * recover if the server has merely unshared then
715 			 * re-shared the file system.  For regular files,
716 			 * the unshare has destroyed the open state at the
717 			 * server and we aren't willing to do a reopen (yet).
718 			 */
719 			if (error == ESTALE && vp->v_type != VREG) {
720 				rp->r_flags &=
721 					~(R4RECOVERR|R4RECOVERRP|R4STALE);
722 				rp->r_error = 0;
723 				error = ESTALE;
724 			}
725 			NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
726 				"nfs4_start_fop: %s %p DEAD, cnt=%d error=%d",
727 				str, (void *)vp,
728 				rsp->rs_num_retry_despite_err, error));
729 		}
730 	} else {
731 		error = nfs4_check_srvstub(vp, rp, op);
732 		NFS4_DEBUG(nfs4_client_recov_stub_debug, (CE_NOTE,
733 			"nfs4_start_fop: %s %p SRVSTUB, error=%d", str,
734 			(void *)vp, error));
735 	}
736 	mutex_exit(&rp->r_statelock);
737 	return (error);
738 }
739 
740 /*
741  * Initial setup code that every operation should call if it might invoke
742  * client recovery.  Can block waiting for recovery to finish on a
743  * filesystem.  Either vnode ptr can be NULL.
744  *
745  * Returns 0 if there are no outstanding errors.  Can return an
746  * errno value under various circumstances (e.g., failed recovery, or
747  * interrupted while waiting for recovery to finish).
748  *
749  * There must be a corresponding call to nfs4_end_op() to free up any locks
750  * or resources allocated by this call (assuming this call succeeded),
751  * using the same rsp that's passed in here.
752  *
753  * The open and lock seqid synchronization must be stopped before calling this
754  * function, as it could lead to deadlock when trying to reopen a file or
755  * reclaim a lock.  The synchronization is obtained with calls to:
756  *   nfs4_start_open_seqid_sync()
757  *   nfs4_start_lock_seqid_sync()
758  *
759  * *startrecovp is set TRUE if the caller should not bother with the
760  * over-the-wire call, and just initiate recovery for the given request.
761  * This is typically used for state-releasing ops if the filesystem has
762  * been forcibly unmounted.  startrecovp may be NULL for
763  * non-state-releasing ops.
764  */
765 
766 int
767 nfs4_start_fop(mntinfo4_t *mi, vnode_t *vp1, vnode_t *vp2, nfs4_op_hint_t op,
768 		nfs4_recov_state_t *rsp, bool_t *startrecovp)
769 {
770 	int error = 0, rerr_cnt;
771 	nfs4_server_t *sp = NULL;
772 	nfs4_server_t *tsp;
773 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
774 	time_t droplock_time;
775 #ifdef DEBUG
776 	void *fop_caller;
777 #endif
778 
779 	ASSERT(vp1 == NULL || vp1->v_vfsp == mi->mi_vfsp);
780 	ASSERT(vp2 == NULL || vp2->v_vfsp == mi->mi_vfsp);
781 
782 #ifdef	DEBUG
783 	if ((fop_caller = tsd_get(nfs4_tsd_key)) != NULL) {
784 		cmn_err(CE_PANIC, "Missing nfs4_end_fop: last caller %p",
785 			fop_caller);
786 	}
787 	(void) tsd_set(nfs4_tsd_key, caller());
788 #endif
789 
790 	rsp->rs_sp = NULL;
791 	rsp->rs_flags &= ~NFS4_RS_RENAME_HELD;
792 	rerr_cnt = rsp->rs_num_retry_despite_err;
793 
794 	/*
795 	 * Process the items that may delay() based on server response
796 	 */
797 	error = nfs4_wait_for_grace(mi, rsp);
798 	if (error)
799 		goto out;
800 
801 	if (vp1 != NULL) {
802 		error = nfs4_wait_for_delay(vp1, rsp);
803 		if (error)
804 			goto out;
805 	}
806 
807 	/* Wait for a delegation recall to complete. */
808 
809 	error = wait_for_recall(vp1, vp2, op, rsp);
810 	if (error)
811 		goto out;
812 
813 	/*
814 	 * Wait for any current recovery actions to finish.  Note that a
815 	 * recovery thread can still start up after wait_for_recovery()
816 	 * finishes.  We don't block out recovery operations until we
817 	 * acquire s_recovlock and mi_recovlock.
818 	 */
819 	error = wait_for_recovery(mi, op);
820 	if (error)
821 		goto out;
822 
823 	/*
824 	 * Check to see if the rnode is already marked with a
825 	 * recovery error.  If so, return it immediately.  But
826 	 * always pass CLOSE, LOCKU, and DELEGRETURN so we can
827 	 * clean up state on the server.
828 	 */
829 
830 	if (vp1 != NULL) {
831 		if (error = nfs4_check_recov_err(vp1, op, rsp, rerr_cnt, "vp1"))
832 			goto out;
833 		nfs4_check_remap(mi, vp1, NFS4_REMAP_CKATTRS, &e);
834 	}
835 
836 	if (vp2 != NULL) {
837 		if (error = nfs4_check_recov_err(vp2, op, rsp, rerr_cnt, "vp2"))
838 			goto out;
839 		nfs4_check_remap(mi, vp2, NFS4_REMAP_CKATTRS, &e);
840 	}
841 
842 	/*
843 	 * The lock order calls for us to acquire s_recovlock before
844 	 * mi_recovlock, but we have to hold mi_recovlock to look up sp (to
845 	 * prevent races with the failover/migration code).  So acquire
846 	 * mi_recovlock, look up sp, drop mi_recovlock, acquire
847 	 * s_recovlock and mi_recovlock, then verify that sp is still the
848 	 * right object.  XXX Can we find a simpler way to deal with this?
849 	 */
850 	if (nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER,
851 	    mi->mi_flags & MI4_INT)) {
852 		error = EINTR;
853 		goto out;
854 	}
855 get_sp:
856 	sp = find_nfs4_server(mi);
857 	if (sp != NULL) {
858 		sp->s_otw_call_count++;
859 		mutex_exit(&sp->s_lock);
860 		droplock_time = gethrestime_sec();
861 	}
862 	nfs_rw_exit(&mi->mi_recovlock);
863 
864 	if (sp != NULL) {
865 		if (nfs_rw_enter_sig(&sp->s_recovlock, RW_READER,
866 			    mi->mi_flags & MI4_INT)) {
867 			error = EINTR;
868 			goto out;
869 		}
870 	}
871 	if (nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER,
872 			    mi->mi_flags & MI4_INT)) {
873 		if (sp != NULL)
874 			nfs_rw_exit(&sp->s_recovlock);
875 		error = EINTR;
876 		goto out;
877 	}
878 	/*
879 	 * If the mntinfo4_t hasn't changed nfs4_sever_ts then
880 	 * there's no point in double checking to make sure it
881 	 * has switched.
882 	 */
883 	if (sp == NULL || droplock_time < mi->mi_srvsettime) {
884 		tsp = find_nfs4_server(mi);
885 		if (tsp != sp) {
886 			/* try again */
887 			if (tsp != NULL) {
888 				mutex_exit(&tsp->s_lock);
889 				nfs4_server_rele(tsp);
890 				tsp = NULL;
891 			}
892 			if (sp != NULL) {
893 				nfs_rw_exit(&sp->s_recovlock);
894 				mutex_enter(&sp->s_lock);
895 				sp->s_otw_call_count--;
896 				mutex_exit(&sp->s_lock);
897 				nfs4_server_rele(sp);
898 				sp = NULL;
899 			}
900 			goto get_sp;
901 		} else {
902 			if (tsp != NULL) {
903 				mutex_exit(&tsp->s_lock);
904 				nfs4_server_rele(tsp);
905 				tsp = NULL;
906 			}
907 		}
908 	}
909 
910 	if (sp != NULL) {
911 		rsp->rs_sp = sp;
912 	}
913 
914 	/*
915 	 * If the fileystem uses volatile filehandles, obtain a lock so
916 	 * that we synchronize with renames.  Exception: mount operations
917 	 * can change mi_fh_expire_type, which could be a problem, since
918 	 * the end_op code needs to be consistent with the start_op code
919 	 * about mi_rename_lock.  Since mounts don't compete with renames,
920 	 * it's simpler to just not acquire the rename lock for mounts.
921 	 */
922 	if (NFS4_VOLATILE_FH(mi) && op != OH_MOUNT) {
923 		if (nfs_rw_enter_sig(&mi->mi_rename_lock,
924 				    op == OH_VFH_RENAME ? RW_WRITER : RW_READER,
925 				    mi->mi_flags & MI4_INT)) {
926 			nfs_rw_exit(&mi->mi_recovlock);
927 			if (sp != NULL)
928 				nfs_rw_exit(&sp->s_recovlock);
929 			error = EINTR;
930 			goto out;
931 		}
932 		rsp->rs_flags |= NFS4_RS_RENAME_HELD;
933 	}
934 
935 	if (OH_IS_STATE_RELE(op)) {
936 		/*
937 		 * For forced unmount, letting the request proceed will
938 		 * almost always delay response to the user, so hand it off
939 		 * to the recovery thread.  For exiting lwp's, we don't
940 		 * have a good way to tell if the request will hang.  We
941 		 * generally want processes to handle their own requests so
942 		 * that they can be done in parallel, but if there is
943 		 * already a recovery thread, hand the request off to it.
944 		 * This will improve user response at no cost to overall
945 		 * system throughput.  For zone shutdown, we'd prefer
946 		 * the recovery thread to handle this as well.
947 		 */
948 		ASSERT(startrecovp != NULL);
949 		mutex_enter(&mi->mi_lock);
950 		if (FS_OR_ZONE_GONE4(mi->mi_vfsp))
951 			*startrecovp = TRUE;
952 		else if ((curthread->t_proc_flag & TP_LWPEXIT) &&
953 		    (mi->mi_flags & MI4_RECOV_ACTIV))
954 			*startrecovp = TRUE;
955 		else
956 			*startrecovp = FALSE;
957 		mutex_exit(&mi->mi_lock);
958 	} else
959 		if (startrecovp != NULL)
960 			*startrecovp = FALSE;
961 
962 	ASSERT(error == 0);
963 	return (error);
964 
965 out:
966 	ASSERT(error != 0);
967 	if (sp != NULL) {
968 		mutex_enter(&sp->s_lock);
969 		sp->s_otw_call_count--;
970 		mutex_exit(&sp->s_lock);
971 		nfs4_server_rele(sp);
972 		rsp->rs_sp = NULL;
973 	}
974 	nfs4_end_op_recall(vp1, vp2, rsp);
975 
976 #ifdef	DEBUG
977 	(void) tsd_set(nfs4_tsd_key, NULL);
978 #endif
979 	return (error);
980 }
981 
982 /*
983  * It is up to the caller to determine if rsp->rs_sp being NULL
984  * is detrimental or not.
985  */
986 int
987 nfs4_start_op(mntinfo4_t *mi, vnode_t *vp1, vnode_t *vp2,
988 	nfs4_recov_state_t *rsp)
989 {
990 	ASSERT(rsp->rs_num_retry_despite_err == 0);
991 	rsp->rs_num_retry_despite_err = 0;
992 	return (nfs4_start_fop(mi, vp1, vp2, OH_OTHER, rsp, NULL));
993 }
994 
995 /*
996  * Release any resources acquired by nfs4_start_op().
997  * 'sp' should be the nfs4_server pointer returned by nfs4_start_op().
998  *
999  * The operation hint is used to avoid a deadlock by bypassing delegation
1000  * return logic for writes, which are done while returning a delegation.
1001  */
1002 
1003 void
1004 nfs4_end_fop(mntinfo4_t *mi, vnode_t *vp1, vnode_t *vp2, nfs4_op_hint_t op,
1005 		nfs4_recov_state_t *rsp, bool_t needs_recov)
1006 {
1007 	nfs4_server_t *sp = rsp->rs_sp;
1008 	rnode4_t *rp = NULL;
1009 
1010 #ifdef	lint
1011 	/*
1012 	 * The op hint isn't used any more, but might be in
1013 	 * the future.
1014 	 */
1015 	op = op;
1016 #endif
1017 
1018 #ifdef	DEBUG
1019 	ASSERT(tsd_get(nfs4_tsd_key) != NULL);
1020 	(void) tsd_set(nfs4_tsd_key, NULL);
1021 #endif
1022 
1023 	nfs4_end_op_recall(vp1, vp2, rsp);
1024 
1025 	if (rsp->rs_flags & NFS4_RS_RENAME_HELD)
1026 		nfs_rw_exit(&mi->mi_rename_lock);
1027 
1028 	if (!needs_recov) {
1029 		if (rsp->rs_flags & NFS4_RS_DELAY_MSG) {
1030 			/* may need to clear the delay interval */
1031 			if (vp1 != NULL) {
1032 				rp = VTOR4(vp1);
1033 				mutex_enter(&rp->r_statelock);
1034 				rp->r_delay_interval = 0;
1035 				mutex_exit(&rp->r_statelock);
1036 			}
1037 		}
1038 		rsp->rs_flags &= ~(NFS4_RS_GRACE_MSG|NFS4_RS_DELAY_MSG);
1039 	}
1040 
1041 	/*
1042 	 * If the corresponding nfs4_start_op() found a sp,
1043 	 * then there must still be a sp.
1044 	 */
1045 	if (sp != NULL) {
1046 		nfs_rw_exit(&mi->mi_recovlock);
1047 		nfs_rw_exit(&sp->s_recovlock);
1048 		mutex_enter(&sp->s_lock);
1049 		sp->s_otw_call_count--;
1050 		cv_broadcast(&sp->s_cv_otw_count);
1051 		mutex_exit(&sp->s_lock);
1052 		nfs4_server_rele(sp);
1053 	} else {
1054 		nfs_rw_exit(&mi->mi_recovlock);
1055 	}
1056 }
1057 
1058 void
1059 nfs4_end_op(mntinfo4_t *mi, vnode_t *vp1, vnode_t *vp2,
1060 	    nfs4_recov_state_t *rsp, bool_t needrecov)
1061 {
1062 	nfs4_end_fop(mi, vp1, vp2, OH_OTHER, rsp, needrecov);
1063 }
1064 
1065 /*
1066  * If the filesystem is going through client recovery, block until
1067  * finished.
1068  * Exceptions:
1069  * - state-releasing ops (CLOSE, LOCKU, DELEGRETURN) are allowed to proceed
1070  *   if the filesystem has been forcibly unmounted or the lwp is exiting.
1071  *
1072  * Return value:
1073  * - 0 if no errors
1074  * - EINTR if the call was interrupted
1075  * - EIO if the filesystem has been forcibly unmounted (non-state-releasing
1076  *   op)
1077  * - the errno value from the recovery thread, if recovery failed
1078  */
1079 
1080 static int
1081 wait_for_recovery(mntinfo4_t *mi, nfs4_op_hint_t op_hint)
1082 {
1083 	int error = 0;
1084 
1085 	mutex_enter(&mi->mi_lock);
1086 
1087 	while (mi->mi_recovflags != 0) {
1088 		klwp_t *lwp = ttolwp(curthread);
1089 
1090 		if (mi->mi_flags & MI4_RECOV_FAIL)
1091 			break;
1092 		if (mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED)
1093 			break;
1094 		if (OH_IS_STATE_RELE(op_hint) &&
1095 		    (curthread->t_proc_flag & TP_LWPEXIT))
1096 			break;
1097 
1098 		if (lwp != NULL)
1099 			lwp->lwp_nostop++;
1100 		/* XXX - use different cv? */
1101 		if (cv_wait_sig(&mi->mi_failover_cv, &mi->mi_lock) == 0) {
1102 			error = EINTR;
1103 			if (lwp != NULL)
1104 				lwp->lwp_nostop--;
1105 			break;
1106 		}
1107 		if (lwp != NULL)
1108 			lwp->lwp_nostop--;
1109 	}
1110 
1111 	if (mi->mi_flags & MI4_RECOV_FAIL) {
1112 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1113 			"wait_for_recovery: fail since RECOV FAIL"));
1114 		error = mi->mi_error;
1115 	} else if ((mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED) &&
1116 	    !OH_IS_STATE_RELE(op_hint)) {
1117 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1118 			"wait_for_recovery: forced unmount"));
1119 		error = EIO;
1120 	}
1121 
1122 	mutex_exit(&mi->mi_lock);
1123 
1124 	return (error);
1125 }
1126 
1127 /*
1128  * If the client received NFS4ERR_GRACE for this particular mount,
1129  * the client blocks here until it is time to try again.
1130  *
1131  * Return value:
1132  * - 0 if wait was successful
1133  * - EINTR if the call was interrupted
1134  */
1135 
1136 int
1137 nfs4_wait_for_grace(mntinfo4_t *mi, nfs4_recov_state_t *rsp)
1138 {
1139 	int error = 0;
1140 	time_t curtime, time_to_wait;
1141 
1142 	/* do a unprotected check to reduce mi_lock contention */
1143 	if (mi->mi_grace_wait != 0) {
1144 		mutex_enter(&mi->mi_lock);
1145 
1146 		if (mi->mi_grace_wait != 0) {
1147 			if (!(rsp->rs_flags & NFS4_RS_GRACE_MSG))
1148 				rsp->rs_flags |= NFS4_RS_GRACE_MSG;
1149 
1150 			curtime = gethrestime_sec();
1151 
1152 			if (curtime < mi->mi_grace_wait) {
1153 
1154 				time_to_wait = mi->mi_grace_wait - curtime;
1155 
1156 				mutex_exit(&mi->mi_lock);
1157 
1158 				delay(SEC_TO_TICK(time_to_wait));
1159 
1160 				curtime = gethrestime_sec();
1161 
1162 				mutex_enter(&mi->mi_lock);
1163 
1164 				if (curtime >= mi->mi_grace_wait)
1165 					mi->mi_grace_wait = 0;
1166 			} else {
1167 				mi->mi_grace_wait = 0;
1168 			}
1169 		}
1170 		mutex_exit(&mi->mi_lock);
1171 	}
1172 
1173 	return (error);
1174 }
1175 
1176 /*
1177  * If the client received NFS4ERR_DELAY for an operation on a vnode,
1178  * the client blocks here until it is time to try again.
1179  *
1180  * Return value:
1181  * - 0 if wait was successful
1182  * - EINTR if the call was interrupted
1183  */
1184 
1185 int
1186 nfs4_wait_for_delay(vnode_t *vp, nfs4_recov_state_t *rsp)
1187 {
1188 	int error = 0;
1189 	time_t curtime, time_to_wait;
1190 	rnode4_t *rp;
1191 
1192 	ASSERT(vp != NULL);
1193 
1194 	rp = VTOR4(vp);
1195 
1196 	/* do a unprotected check to reduce r_statelock contention */
1197 	if (rp->r_delay_wait != 0) {
1198 		mutex_enter(&rp->r_statelock);
1199 
1200 		if (rp->r_delay_wait != 0) {
1201 
1202 			if (!(rsp->rs_flags & NFS4_RS_DELAY_MSG)) {
1203 				rsp->rs_flags |= NFS4_RS_DELAY_MSG;
1204 				nfs4_mi_kstat_inc_delay(VTOMI4(vp));
1205 			}
1206 
1207 			curtime = gethrestime_sec();
1208 
1209 			if (curtime < rp->r_delay_wait) {
1210 
1211 				time_to_wait = rp->r_delay_wait - curtime;
1212 
1213 				mutex_exit(&rp->r_statelock);
1214 
1215 				delay(SEC_TO_TICK(time_to_wait));
1216 
1217 				curtime = gethrestime_sec();
1218 
1219 				mutex_enter(&rp->r_statelock);
1220 
1221 				if (curtime >= rp->r_delay_wait)
1222 					rp->r_delay_wait = 0;
1223 			} else {
1224 				rp->r_delay_wait = 0;
1225 			}
1226 		}
1227 		mutex_exit(&rp->r_statelock);
1228 	}
1229 
1230 	return (error);
1231 }
1232 
1233 /*
1234  * The recovery thread.
1235  */
1236 
1237 static void
1238 nfs4_recov_thread(recov_info_t *recovp)
1239 {
1240 	mntinfo4_t *mi = recovp->rc_mi;
1241 	nfs4_server_t *sp;
1242 	int done = 0, error = 0;
1243 	bool_t recov_fail = FALSE;
1244 	callb_cpr_t cpr_info;
1245 	kmutex_t cpr_lock;
1246 
1247 	nfs4_queue_event(RE_START, mi, NULL, mi->mi_recovflags,
1248 	    recovp->rc_vp1, recovp->rc_vp2, 0, NULL, 0, TAG_NONE, TAG_NONE,
1249 	    0, 0);
1250 
1251 	mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
1252 	CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr, "nfsv4Recov");
1253 
1254 	mutex_enter(&mi->mi_lock);
1255 	mi->mi_recovthread = curthread;
1256 	mutex_exit(&mi->mi_lock);
1257 
1258 	/*
1259 	 * We don't really need protection here against failover or
1260 	 * migration, since the current thread is the one that would make
1261 	 * any changes, but hold mi_recovlock anyway for completeness (and
1262 	 * to satisfy any ASSERTs).
1263 	 */
1264 	(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, 0);
1265 	sp = find_nfs4_server(mi);
1266 	if (sp != NULL)
1267 		mutex_exit(&sp->s_lock);
1268 	nfs_rw_exit(&mi->mi_recovlock);
1269 
1270 	/*
1271 	 * Do any necessary recovery, based on the information in recovp
1272 	 * and any recovery flags.
1273 	 */
1274 
1275 	do {
1276 		mutex_enter(&mi->mi_lock);
1277 		if (FS_OR_ZONE_GONE4(mi->mi_vfsp)) {
1278 			bool_t activesrv;
1279 
1280 			NFS4_DEBUG(nfs4_client_recov_debug &&
1281 			    mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED, (CE_NOTE,
1282 				"nfs4_recov_thread: file system has been "
1283 				"unmounted"));
1284 			NFS4_DEBUG(nfs4_client_recov_debug &&
1285 			    zone_status_get(curproc->p_zone) >=
1286 			    ZONE_IS_SHUTTING_DOWN, (CE_NOTE,
1287 				"nfs4_recov_thread: zone shutting down"));
1288 			/*
1289 			 * If the server has lost its state for us and
1290 			 * the filesystem is unmounted, then the filesystem
1291 			 * can be tossed, even if there are lost lock or
1292 			 * lost state calls in the recovery queue.
1293 			 */
1294 			if (mi->mi_recovflags &
1295 			    (MI4R_NEED_CLIENTID | MI4R_REOPEN_FILES)) {
1296 				NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1297 				"nfs4_recov_thread: bailing out"));
1298 				mi->mi_flags |= MI4_RECOV_FAIL;
1299 				mi->mi_error = recovp->rc_error;
1300 				recov_fail = TRUE;
1301 			}
1302 			/*
1303 			 * We don't know if the server has any state for
1304 			 * us, and the filesystem has been unmounted.  If
1305 			 * there are "lost state" recovery items, keep
1306 			 * trying to process them until there are no more
1307 			 * mounted filesystems for the server.  Otherwise,
1308 			 * bail out.  The reason we don't mark the
1309 			 * filesystem as failing recovery is in case we
1310 			 * have to do "lost state" recovery later (e.g., a
1311 			 * user process exits).
1312 			 */
1313 			if (!(mi->mi_recovflags & MI4R_LOST_STATE)) {
1314 				done = 1;
1315 				mutex_exit(&mi->mi_lock);
1316 				break;
1317 			}
1318 			mutex_exit(&mi->mi_lock);
1319 
1320 			if (sp == NULL)
1321 				activesrv = FALSE;
1322 			else {
1323 				mutex_enter(&sp->s_lock);
1324 				activesrv = nfs4_fs_active(sp);
1325 			}
1326 			if (!activesrv) {
1327 				NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1328 					"no active fs for server %p",
1329 					(void *)sp));
1330 				mutex_enter(&mi->mi_lock);
1331 				mi->mi_flags |= MI4_RECOV_FAIL;
1332 				mi->mi_error = recovp->rc_error;
1333 				mutex_exit(&mi->mi_lock);
1334 				recov_fail = TRUE;
1335 				if (sp != NULL) {
1336 					/*
1337 					 * Mark the server instance as
1338 					 * dead, so that nobody will attach
1339 					 * a new filesystem.
1340 					 */
1341 					nfs4_mark_srv_dead(sp);
1342 				}
1343 			}
1344 			if (sp != NULL)
1345 				mutex_exit(&sp->s_lock);
1346 		} else {
1347 			mutex_exit(&mi->mi_lock);
1348 		}
1349 
1350 		/*
1351 		 * Check if we need to select a new server for a
1352 		 * failover.  Choosing a new server will force at
1353 		 * least a check of the clientid.
1354 		 */
1355 		mutex_enter(&mi->mi_lock);
1356 		if (!recov_fail &&
1357 		    (mi->mi_recovflags & MI4R_NEED_NEW_SERVER)) {
1358 			mutex_exit(&mi->mi_lock);
1359 			recov_newserver(recovp, &sp, &recov_fail);
1360 		} else
1361 			mutex_exit(&mi->mi_lock);
1362 
1363 		/*
1364 		 * Check if we need to recover the clientid.  This
1365 		 * must be done before file and lock recovery, and it
1366 		 * potentially affects the recovery threads for other
1367 		 * filesystems, so it gets special treatment.
1368 		 */
1369 		if (sp != NULL && recov_fail == FALSE) {
1370 			mutex_enter(&sp->s_lock);
1371 			if (!(sp->s_flags & N4S_CLIENTID_SET)) {
1372 				mutex_exit(&sp->s_lock);
1373 				recov_clientid(recovp, sp);
1374 			} else {
1375 				/*
1376 				 * Unset this flag in case another recovery
1377 				 * thread successfully recovered the clientid
1378 				 * for us already.
1379 				 */
1380 				mutex_enter(&mi->mi_lock);
1381 				mi->mi_recovflags &= ~MI4R_NEED_CLIENTID;
1382 				mutex_exit(&mi->mi_lock);
1383 				mutex_exit(&sp->s_lock);
1384 			}
1385 		}
1386 
1387 		/*
1388 		 * Check if we need to get the security information.
1389 		 */
1390 		mutex_enter(&mi->mi_lock);
1391 		if ((mi->mi_recovflags & MI4R_NEED_SECINFO) &&
1392 		    !(mi->mi_flags & MI4_RECOV_FAIL)) {
1393 			mutex_exit(&mi->mi_lock);
1394 			(void) nfs_rw_enter_sig(&mi->mi_recovlock,
1395 							RW_WRITER, 0);
1396 			error = nfs4_secinfo_recov(recovp->rc_mi,
1397 					recovp->rc_vp1, recovp->rc_vp2);
1398 			/*
1399 			 * If error, nothing more can be done, stop
1400 			 * the recovery.
1401 			 */
1402 			if (error) {
1403 				mutex_enter(&mi->mi_lock);
1404 				mi->mi_flags |= MI4_RECOV_FAIL;
1405 				mi->mi_error = recovp->rc_error;
1406 				mutex_exit(&mi->mi_lock);
1407 				nfs4_queue_event(RE_WRONGSEC, mi, NULL,
1408 				    error, recovp->rc_vp1, recovp->rc_vp2,
1409 				    0, NULL, 0, TAG_NONE, TAG_NONE, 0, 0);
1410 			}
1411 			nfs_rw_exit(&mi->mi_recovlock);
1412 		} else
1413 			mutex_exit(&mi->mi_lock);
1414 
1415 		/*
1416 		 * Check if there's a bad seqid to recover.
1417 		 */
1418 		mutex_enter(&mi->mi_lock);
1419 		if ((mi->mi_recovflags & MI4R_BAD_SEQID) &&
1420 		    !(mi->mi_flags & MI4_RECOV_FAIL)) {
1421 			mutex_exit(&mi->mi_lock);
1422 			(void) nfs_rw_enter_sig(&mi->mi_recovlock,
1423 					RW_WRITER, 0);
1424 			recov_bad_seqid(recovp);
1425 			nfs_rw_exit(&mi->mi_recovlock);
1426 		} else
1427 			mutex_exit(&mi->mi_lock);
1428 
1429 		/*
1430 		 * Next check for recovery that affects the entire
1431 		 * filesystem.
1432 		 */
1433 		if (sp != NULL) {
1434 			mutex_enter(&mi->mi_lock);
1435 			if ((mi->mi_recovflags & MI4R_REOPEN_FILES) &&
1436 			    !(mi->mi_flags & MI4_RECOV_FAIL)) {
1437 				mutex_exit(&mi->mi_lock);
1438 				recov_openfiles(recovp, sp);
1439 			} else
1440 				mutex_exit(&mi->mi_lock);
1441 		}
1442 
1443 		/*
1444 		 * Send any queued state recovery requests.
1445 		 */
1446 		mutex_enter(&mi->mi_lock);
1447 		if (sp != NULL &&
1448 		    (mi->mi_recovflags & MI4R_LOST_STATE) &&
1449 		    !(mi->mi_flags & MI4_RECOV_FAIL)) {
1450 			mutex_exit(&mi->mi_lock);
1451 			(void) nfs_rw_enter_sig(&mi->mi_recovlock,
1452 				    RW_WRITER, 0);
1453 			nfs4_resend_lost_rqsts(recovp, sp);
1454 			if (list_head(&mi->mi_lost_state) == NULL) {
1455 				/* done */
1456 				mutex_enter(&mi->mi_lock);
1457 				mi->mi_recovflags &= ~MI4R_LOST_STATE;
1458 				mutex_exit(&mi->mi_lock);
1459 			}
1460 			nfs_rw_exit(&mi->mi_recovlock);
1461 		} else {
1462 			mutex_exit(&mi->mi_lock);
1463 		}
1464 
1465 		/*
1466 		 * See if there is anything more to do.  If not, announce
1467 		 * that we are done and exit.
1468 		 *
1469 		 * Need mi_recovlock to keep 'sp' valid.  Must grab
1470 		 * mi_recovlock before mi_lock to preserve lock ordering.
1471 		 */
1472 		(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, 0);
1473 		mutex_enter(&mi->mi_lock);
1474 		if ((mi->mi_recovflags & ~MI4R_SRV_REBOOT) == 0 ||
1475 		    (mi->mi_flags & MI4_RECOV_FAIL)) {
1476 			list_t local_lost_state;
1477 			nfs4_lost_rqst_t *lrp;
1478 
1479 			/*
1480 			 * We need to remove the lost requests before we
1481 			 * unmark the mi as no longer doing recovery to
1482 			 * avoid a race with a new thread putting new lost
1483 			 * requests on the same mi (and the going away
1484 			 * thread would remove the new lost requests).
1485 			 *
1486 			 * Move the lost requests to a local list since
1487 			 * nfs4_remove_lost_rqst() drops mi_lock, and
1488 			 * dropping the mi_lock would make our check to
1489 			 * see if recovery is done no longer valid.
1490 			 */
1491 			list_create(&local_lost_state,
1492 			    sizeof (nfs4_lost_rqst_t),
1493 			    offsetof(nfs4_lost_rqst_t, lr_node));
1494 			list_move_tail(&local_lost_state, &mi->mi_lost_state);
1495 
1496 			done = 1;
1497 			mutex_exit(&mi->mi_lock);
1498 			/*
1499 			 * Now officially free the "moved"
1500 			 * lost requests.
1501 			 */
1502 			while ((lrp = list_head(&local_lost_state)) != NULL) {
1503 				list_remove(&local_lost_state, lrp);
1504 				nfs4_free_lost_rqst(lrp, sp);
1505 			}
1506 			list_destroy(&local_lost_state);
1507 		} else
1508 			mutex_exit(&mi->mi_lock);
1509 		nfs_rw_exit(&mi->mi_recovlock);
1510 
1511 		/*
1512 		 * If the filesystem has been forcibly unmounted, there is
1513 		 * probably no point in retrying immediately.  Furthermore,
1514 		 * there might be user processes waiting for a chance to
1515 		 * queue up "lost state" requests, so that they can exit.
1516 		 * So pause here for a moment.  Same logic for zone shutdown.
1517 		 */
1518 		if (!done && FS_OR_ZONE_GONE4(mi->mi_vfsp)) {
1519 			mutex_enter(&mi->mi_lock);
1520 			cv_broadcast(&mi->mi_failover_cv);
1521 			mutex_exit(&mi->mi_lock);
1522 			delay(SEC_TO_TICK(nfs4_unmount_delay));
1523 		}
1524 
1525 	} while (!done);
1526 
1527 	if (sp != NULL)
1528 		nfs4_server_rele(sp);
1529 
1530 	/*
1531 	 * Return all recalled delegations
1532 	 */
1533 	nfs4_dlistclean();
1534 
1535 	mutex_enter(&mi->mi_lock);
1536 	recov_done(mi, recovp);
1537 	mutex_exit(&mi->mi_lock);
1538 
1539 	/*
1540 	 * Free up resources that were allocated for us.
1541 	 */
1542 	if (recovp->rc_vp1 != NULL)
1543 		VN_RELE(recovp->rc_vp1);
1544 	if (recovp->rc_vp2 != NULL)
1545 		VN_RELE(recovp->rc_vp2);
1546 	/*
1547 	 * We can't be the last reference to this vfsp.  If we were, then
1548 	 * this RELE would cause nfs_free_mi4() to be called and blow away
1549 	 * the mi structure.  start_recovery() puts a HOLD on the vfsp.
1550 	 */
1551 	ASSERT(mi->mi_vfsp->vfs_count > 1);
1552 	VFS_RELE(mi->mi_vfsp);
1553 
1554 	/* now we are done using the mi struct, signal the waiters */
1555 	mutex_enter(&mi->mi_lock);
1556 	mi->mi_in_recovery--;
1557 	if (mi->mi_in_recovery == 0)
1558 		cv_broadcast(&mi->mi_cv_in_recov);
1559 	mutex_exit(&mi->mi_lock);
1560 
1561 	kmem_free(recovp, sizeof (recov_info_t));
1562 	mutex_enter(&cpr_lock);
1563 	CALLB_CPR_EXIT(&cpr_info);
1564 	mutex_destroy(&cpr_lock);
1565 	zthread_exit();
1566 }
1567 
1568 /*
1569  * Log the end of recovery and notify any waiting threads.
1570  */
1571 
1572 static void
1573 recov_done(mntinfo4_t *mi, recov_info_t *recovp)
1574 {
1575 
1576 	ASSERT(MUTEX_HELD(&mi->mi_lock));
1577 
1578 	nfs4_queue_event(RE_END, mi, NULL, 0, recovp->rc_vp1,
1579 		recovp->rc_vp2, 0, NULL, 0, TAG_NONE, TAG_NONE, 0, 0);
1580 	mi->mi_recovthread = NULL;
1581 	mi->mi_flags &= ~MI4_RECOV_ACTIV;
1582 	mi->mi_recovflags &= ~MI4R_SRV_REBOOT;
1583 	cv_broadcast(&mi->mi_failover_cv);
1584 }
1585 
1586 /*
1587  * State-specific recovery routines, by state.
1588  */
1589 
1590 /*
1591  * Failover.
1592  *
1593  * Replaces *spp with a reference to the new server, which must
1594  * eventually be freed.
1595  */
1596 
1597 static void
1598 recov_newserver(recov_info_t *recovp, nfs4_server_t **spp, bool_t *recov_fail)
1599 {
1600 	mntinfo4_t *mi = recovp->rc_mi;
1601 	servinfo4_t *svp = NULL;
1602 	nfs4_server_t *osp = *spp;
1603 	CLIENT *cl;
1604 	enum clnt_stat status;
1605 	struct timeval tv;
1606 	int error;
1607 	int oncethru = 0;
1608 	rnode4_t *rp;
1609 	int index;
1610 	nfs_fh4 fh;
1611 	char *snames;
1612 	size_t len;
1613 
1614 	(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_WRITER, 0);
1615 
1616 	tv.tv_sec = 2;
1617 	tv.tv_usec = 0;
1618 
1619 #ifdef lint
1620 	/*
1621 	 * Lint can't follow the logic, so thinks that snames and len
1622 	 * can be used before being set.  They can't, but lint can't
1623 	 * figure it out.  To address the lint warning, initialize
1624 	 * snames and len for lint.
1625 	 */
1626 	snames = NULL;
1627 	len = 0;
1628 #endif
1629 
1630 	/*
1631 	 * Ping the null NFS procedure of every server in
1632 	 * the list until one responds.  We always start
1633 	 * at the head of the list and always skip the one
1634 	 * that is current, since it's caused us a problem.
1635 	 */
1636 	while (svp == NULL) {
1637 		for (svp = mi->mi_servers; svp; svp = svp->sv_next) {
1638 
1639 			mutex_enter(&mi->mi_lock);
1640 			if (FS_OR_ZONE_GONE4(mi->mi_vfsp)) {
1641 				mi->mi_flags |= MI4_RECOV_FAIL;
1642 				mutex_exit(&mi->mi_lock);
1643 				(void) nfs_rw_exit(&mi->mi_recovlock);
1644 				*recov_fail = TRUE;
1645 				if (oncethru)
1646 					kmem_free(snames, len);
1647 				return;
1648 			}
1649 			mutex_exit(&mi->mi_lock);
1650 
1651 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1652 			if (svp->sv_flags & SV4_NOTINUSE) {
1653 				nfs_rw_exit(&svp->sv_lock);
1654 				continue;
1655 			}
1656 			nfs_rw_exit(&svp->sv_lock);
1657 
1658 			if (!oncethru && svp == mi->mi_curr_serv)
1659 				continue;
1660 
1661 			error = clnt_tli_kcreate(svp->sv_knconf, &svp->sv_addr,
1662 			    NFS_PROGRAM, NFS_V4, 0, 1, CRED(), &cl);
1663 			if (error)
1664 				continue;
1665 
1666 			if (!(mi->mi_flags & MI4_INT))
1667 				cl->cl_nosignal = TRUE;
1668 			status = CLNT_CALL(cl, RFS_NULL, xdr_void, NULL,
1669 			    xdr_void, NULL, tv);
1670 			if (!(mi->mi_flags & MI4_INT))
1671 				cl->cl_nosignal = FALSE;
1672 			AUTH_DESTROY(cl->cl_auth);
1673 			CLNT_DESTROY(cl);
1674 			if (status == RPC_SUCCESS) {
1675 				nfs4_queue_event(RE_FAILOVER, mi,
1676 				    svp == mi->mi_curr_serv ? NULL :
1677 				    svp->sv_hostname, 0, NULL, NULL, 0,
1678 				    NULL, 0, TAG_NONE, TAG_NONE, 0, 0);
1679 				break;
1680 			}
1681 		}
1682 
1683 		if (svp == NULL) {
1684 			if (!oncethru) {
1685 				snames = nfs4_getsrvnames(mi, &len);
1686 				nfs4_queue_fact(RF_SRVS_NOT_RESPOND, mi,
1687 				    0, 0, 0, FALSE, snames, 0, NULL);
1688 				oncethru = 1;
1689 			}
1690 			delay(hz);
1691 		}
1692 	}
1693 
1694 	if (oncethru) {
1695 		nfs4_queue_fact(RF_SRVS_OK, mi, 0, 0, 0, FALSE, snames,
1696 		    0, NULL);
1697 		kmem_free(snames, len);
1698 	}
1699 
1700 #if DEBUG
1701 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1702 	ASSERT((svp->sv_flags & SV4_NOTINUSE) == 0);
1703 	nfs_rw_exit(&svp->sv_lock);
1704 #endif
1705 
1706 	mutex_enter(&mi->mi_lock);
1707 	mi->mi_recovflags &= ~MI4R_NEED_NEW_SERVER;
1708 	if (svp != mi->mi_curr_serv) {
1709 		servinfo4_t *osvp = mi->mi_curr_serv;
1710 
1711 		mutex_exit(&mi->mi_lock);
1712 
1713 		/*
1714 		 * Update server-dependent fields in the root vnode.
1715 		 */
1716 		index = rtable4hash(mi->mi_rootfh);
1717 		rw_enter(&rtable4[index].r_lock, RW_WRITER);
1718 
1719 		rp = r4find(&rtable4[index], mi->mi_rootfh, mi->mi_vfsp);
1720 		if (rp != NULL) {
1721 			NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1722 			    "recov_newserver: remapping %s", rnode4info(rp)));
1723 			mutex_enter(&rp->r_statelock);
1724 			rp->r_server = svp;
1725 			PURGE_ATTRCACHE4_LOCKED(rp);
1726 			mutex_exit(&rp->r_statelock);
1727 			(void) nfs4_free_data_reclaim(rp);
1728 			nfs4_purge_rddir_cache(RTOV4(rp));
1729 			rw_exit(&rtable4[index].r_lock);
1730 			NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
1731 			    "recov_newserver: done with %s",
1732 			    rnode4info(rp)));
1733 			VN_RELE(RTOV4(rp));
1734 		} else
1735 			rw_exit(&rtable4[index].r_lock);
1736 		(void) dnlc_purge_vfsp(mi->mi_vfsp, 0);
1737 
1738 		mutex_enter(&mi->mi_lock);
1739 		mi->mi_recovflags |= MI4R_REOPEN_FILES | MI4R_REMAP_FILES;
1740 		if (recovp->rc_srv_reboot)
1741 			mi->mi_recovflags |= MI4R_SRV_REBOOT;
1742 		mi->mi_curr_serv = svp;
1743 		mi->mi_failover++;
1744 		mi->mi_flags &= ~MI4_BADOWNER_DEBUG;
1745 		mutex_exit(&mi->mi_lock);
1746 
1747 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1748 		fh.nfs_fh4_len = svp->sv_fhandle.fh_len;
1749 		fh.nfs_fh4_val = svp->sv_fhandle.fh_buf;
1750 		sfh4_update(mi->mi_rootfh, &fh);
1751 		fh.nfs_fh4_len = svp->sv_pfhandle.fh_len;
1752 		fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf;
1753 		sfh4_update(mi->mi_srvparentfh, &fh);
1754 		nfs_rw_exit(&svp->sv_lock);
1755 
1756 		*spp = nfs4_move_mi(mi, osvp, svp);
1757 		if (osp != NULL)
1758 			nfs4_server_rele(osp);
1759 	} else
1760 		mutex_exit(&mi->mi_lock);
1761 	(void) nfs_rw_exit(&mi->mi_recovlock);
1762 }
1763 
1764 /*
1765  * Clientid.
1766  */
1767 
1768 static void
1769 recov_clientid(recov_info_t *recovp, nfs4_server_t *sp)
1770 {
1771 	mntinfo4_t *mi = recovp->rc_mi;
1772 	int error = 0;
1773 	int still_stale;
1774 	int need_new_s;
1775 
1776 	ASSERT(sp != NULL);
1777 
1778 	/*
1779 	 * Acquire the recovery lock and then verify that the clientid
1780 	 * still needs to be recovered.  (Note that s_recovlock is supposed
1781 	 * to be acquired before s_lock.)  Since the thread holds the
1782 	 * recovery lock, no other thread will recover the clientid.
1783 	 */
1784 	(void) nfs_rw_enter_sig(&sp->s_recovlock, RW_WRITER, 0);
1785 	(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_WRITER, 0);
1786 	mutex_enter(&sp->s_lock);
1787 	still_stale = ((sp->s_flags & N4S_CLIENTID_SET) == 0);
1788 	mutex_exit(&sp->s_lock);
1789 
1790 	if (still_stale) {
1791 		nfs4_error_t n4e;
1792 
1793 		nfs4_error_zinit(&n4e);
1794 		nfs4setclientid(mi, kcred, TRUE, &n4e);
1795 		error = n4e.error;
1796 		if (error != 0) {
1797 
1798 			/*
1799 			 * nfs4setclientid may have set MI4R_NEED_NEW_SERVER,
1800 			 * if so, just return and let recov_thread drive
1801 			 * failover.
1802 			 */
1803 			mutex_enter(&mi->mi_lock);
1804 			need_new_s = mi->mi_recovflags & MI4R_NEED_NEW_SERVER;
1805 			mutex_exit(&mi->mi_lock);
1806 
1807 			if (need_new_s) {
1808 				nfs_rw_exit(&mi->mi_recovlock);
1809 				nfs_rw_exit(&sp->s_recovlock);
1810 				return;
1811 			}
1812 
1813 			nfs4_queue_event(RE_CLIENTID, mi, NULL, n4e.error, NULL,
1814 			    NULL, n4e.stat, NULL, 0, TAG_NONE, TAG_NONE, 0, 0);
1815 			mutex_enter(&mi->mi_lock);
1816 			mi->mi_flags |= MI4_RECOV_FAIL;
1817 			mi->mi_error = recovp->rc_error;
1818 			mutex_exit(&mi->mi_lock);
1819 			/* don't destroy the nfs4_server, let umount do it */
1820 		}
1821 	}
1822 
1823 	if (error == 0) {
1824 		mutex_enter(&mi->mi_lock);
1825 		mi->mi_recovflags &= ~MI4R_NEED_CLIENTID;
1826 		/*
1827 		 * If still_stale isn't true, then another thread already
1828 		 * recovered the clientid.  And that thread that set the
1829 		 * clientid will have initiated reopening files on all the
1830 		 * filesystems for the server, so we should not initiate
1831 		 * reopening for this filesystem here.
1832 		 */
1833 		if (still_stale) {
1834 			mi->mi_recovflags |= MI4R_REOPEN_FILES;
1835 			if (recovp->rc_srv_reboot)
1836 				mi->mi_recovflags |= MI4R_SRV_REBOOT;
1837 		}
1838 		mutex_exit(&mi->mi_lock);
1839 	}
1840 
1841 	nfs_rw_exit(&mi->mi_recovlock);
1842 
1843 	if (error != 0) {
1844 		nfs_rw_exit(&sp->s_recovlock);
1845 		mutex_enter(&mi->mi_lock);
1846 		if ((mi->mi_flags & MI4_RECOV_FAIL) == 0)
1847 			delay(SEC_TO_TICK(recov_err_delay));
1848 		mutex_exit(&mi->mi_lock);
1849 	} else {
1850 		mntinfo4_t **milist;
1851 		mntinfo4_t *tmi;
1852 		int nummi, i;
1853 
1854 		/*
1855 		 * Initiate recovery of open files for other filesystems.
1856 		 * We create an array of filesystems, rather than just
1857 		 * walking the filesystem list, to avoid deadlock issues
1858 		 * with s_lock and mi_recovlock.
1859 		 */
1860 		milist = make_milist(sp, &nummi);
1861 		for (i = 0; i < nummi; i++) {
1862 			tmi = milist[i];
1863 			if (tmi != mi) {
1864 				(void) nfs_rw_enter_sig(&tmi->mi_recovlock,
1865 							RW_READER, 0);
1866 				start_recovery_action(NR_OPENFILES, TRUE, tmi,
1867 					NULL, NULL);
1868 				nfs_rw_exit(&tmi->mi_recovlock);
1869 			}
1870 		}
1871 		free_milist(milist, nummi);
1872 
1873 		nfs_rw_exit(&sp->s_recovlock);
1874 	}
1875 }
1876 
1877 /*
1878  * Return an array of filesystems associated with the given server.  The
1879  * caller should call free_milist() to free the references and memory.
1880  */
1881 
1882 static mntinfo4_t **
1883 make_milist(nfs4_server_t *sp, int *nummip)
1884 {
1885 	int nummi, i;
1886 	mntinfo4_t **milist;
1887 	mntinfo4_t *tmi;
1888 
1889 	mutex_enter(&sp->s_lock);
1890 	nummi = 0;
1891 	for (tmi = sp->mntinfo4_list; tmi != NULL; tmi = tmi->mi_clientid_next)
1892 		nummi++;
1893 
1894 	milist = kmem_alloc(nummi * sizeof (mntinfo4_t *), KM_NOSLEEP);
1895 
1896 	for (i = 0, tmi = sp->mntinfo4_list; tmi != NULL; i++,
1897 	    tmi = tmi->mi_clientid_next) {
1898 		milist[i] = tmi;
1899 		VFS_HOLD(tmi->mi_vfsp);
1900 	}
1901 	mutex_exit(&sp->s_lock);
1902 
1903 	*nummip = nummi;
1904 	return (milist);
1905 }
1906 
1907 /*
1908  * Free the filesystem list created by make_milist().
1909  */
1910 
1911 static void
1912 free_milist(mntinfo4_t **milist, int nummi)
1913 {
1914 	mntinfo4_t *tmi;
1915 	int i;
1916 
1917 	for (i = 0; i < nummi; i++) {
1918 		tmi = milist[i];
1919 		VFS_RELE(tmi->mi_vfsp);
1920 	}
1921 	kmem_free(milist, nummi * sizeof (mntinfo4_t *));
1922 }
1923 
1924 /*
1925  * Filehandle
1926  */
1927 
1928 /*
1929  * Lookup the filehandle for the given vnode and update the rnode if it has
1930  * changed.
1931  *
1932  * Errors:
1933  * - if the filehandle could not be updated because of an error that
1934  *   requires further recovery, initiate that recovery and return.
1935  * - if the filehandle could not be updated because of a signal, pretend we
1936  *   succeeded and let someone else deal with it.
1937  * - if the filehandle could not be updated and the filesystem has been
1938  *   forcibly unmounted, pretend we succeeded, and let the caller deal with
1939  *   the forced unmount (to retry or not to retry, that is the question).
1940  * - if the filehandle could not be updated because of some other error,
1941  *   mark the rnode bad and return.
1942  */
1943 static void
1944 recov_filehandle(nfs4_recov_t action, mntinfo4_t *mi, vnode_t *vp)
1945 {
1946 	rnode4_t *rp = VTOR4(vp);
1947 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
1948 	bool_t needrecov;
1949 
1950 	mutex_enter(&rp->r_statelock);
1951 
1952 	if (rp->r_flags & R4RECOVERR) {
1953 		mutex_exit(&rp->r_statelock);
1954 		return;
1955 	}
1956 
1957 	/*
1958 	 * If someone else is updating the filehandle, wait for them to
1959 	 * finish and then let our caller retry.
1960 	 */
1961 	if (rp->r_flags & R4RECEXPFH) {
1962 		while (rp->r_flags & R4RECEXPFH) {
1963 			cv_wait(&rp->r_cv, &rp->r_statelock);
1964 		}
1965 		mutex_exit(&rp->r_statelock);
1966 		return;
1967 	}
1968 	rp->r_flags |= R4RECEXPFH;
1969 	mutex_exit(&rp->r_statelock);
1970 
1971 	if (action == NR_BADHANDLE) {
1972 		/* shouldn't happen */
1973 		nfs4_queue_event(RE_BADHANDLE, mi, NULL, 0,
1974 		    vp, NULL, 0, NULL, 0, TAG_NONE, TAG_NONE, 0, 0);
1975 	}
1976 
1977 	nfs4_remap_file(mi, vp, 0, &e);
1978 	needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
1979 
1980 	/*
1981 	 * If we get BADHANDLE or FHEXPIRED in their handler, something is
1982 	 * broken.  Don't try to recover, just mark the file dead.
1983 	 */
1984 	if (needrecov && e.error == 0 &&
1985 	    (e.stat == NFS4ERR_BADHANDLE || e.stat == NFS4ERR_FHEXPIRED))
1986 		needrecov = FALSE;
1987 	if (needrecov) {
1988 		(void) nfs4_start_recovery(&e, mi, vp,
1989 				NULL, NULL, NULL, OP_LOOKUP, NULL);
1990 	} else if (e.error != EINTR &&
1991 	    !NFS4_FRC_UNMT_ERR(e.error, mi->mi_vfsp) &&
1992 	    (e.error != 0 || e.stat != NFS4_OK)) {
1993 		nfs4_recov_fh_fail(vp, e.error, e.stat);
1994 		/*
1995 		 * Don't set r_error to ESTALE.  Higher-level code (e.g.,
1996 		 * cstatat_getvp()) retries on ESTALE, which would cause
1997 		 * an infinite loop.
1998 		 */
1999 	}
2000 
2001 	mutex_enter(&rp->r_statelock);
2002 	rp->r_flags &= ~R4RECEXPFH;
2003 	cv_broadcast(&rp->r_cv);
2004 	mutex_exit(&rp->r_statelock);
2005 }
2006 
2007 /*
2008  * Stale Filehandle
2009  */
2010 
2011 /*
2012  * A stale filehandle can happen when an individual file has
2013  * been removed, or when an entire filesystem has been taken
2014  * offline.  To distinguish these cases, we do this:
2015  * - if a GETATTR with the current filehandle is okay, we do
2016  *   nothing (this can happen with two-filehandle ops)
2017  * - if the GETATTR fails, but a GETATTR of the root filehandle
2018  *   succeeds, mark the rnode with R4STALE, which will stop use
2019  * - if the GETATTR fails, and a GETATTR of the root filehandle
2020  *   also fails, we consider the problem filesystem-wide, so:
2021  *   - if we can failover, we should
2022  *   - if we can't failover, we should mark both the original
2023  *     vnode and the root bad
2024  */
2025 static void
2026 recov_stale(mntinfo4_t *mi, vnode_t *vp)
2027 {
2028 	rnode4_t *rp = VTOR4(vp);
2029 	vnode_t *rootvp = NULL;
2030 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
2031 	nfs4_ga_res_t gar;
2032 	char *fail_msg = "failed to recover from NFS4ERR_STALE";
2033 	bool_t needrecov;
2034 
2035 	mutex_enter(&rp->r_statelock);
2036 
2037 	if (rp->r_flags & R4RECOVERR) {
2038 		mutex_exit(&rp->r_statelock);
2039 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2040 		    "recov_stale: already marked dead, rp %s",
2041 		    rnode4info(rp)));
2042 		return;
2043 	}
2044 
2045 	if (rp->r_flags & R4STALE) {
2046 		mutex_exit(&rp->r_statelock);
2047 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2048 		    "recov_stale: already marked stale, rp %s",
2049 		    rnode4info(rp)));
2050 		return;
2051 	}
2052 
2053 	mutex_exit(&rp->r_statelock);
2054 
2055 	/* Try a GETATTR on this vnode */
2056 	nfs4_getattr_otw_norecovery(vp, &gar, &e, CRED(), 0);
2057 
2058 	/*
2059 	 * Handle non-STALE recoverable errors
2060 	 */
2061 	needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
2062 	if (needrecov && (e.error != 0 || e.stat != NFS4ERR_STALE)) {
2063 		(void) nfs4_start_recovery(&e, mi, vp,
2064 				NULL, NULL, NULL, OP_GETATTR, NULL);
2065 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2066 		    "recov_stale: error=%d, stat=%d seen on rp %s",
2067 		    e.error, e.stat, rnode4info(rp)));
2068 		goto out;
2069 	}
2070 
2071 	/* Are things OK for this vnode? */
2072 	if (!e.error && e.stat == NFS4_OK) {
2073 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2074 		    "recov_stale: file appears fine, rp %s",
2075 		    rnode4info(rp)));
2076 		goto out;
2077 	}
2078 
2079 	/* Did we get an unrelated non-recoverable error? */
2080 	if (e.error || e.stat != NFS4ERR_STALE) {
2081 		nfs4_fail_recov(vp, fail_msg, e.error, e.stat);
2082 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2083 		    "recov_stale: unrelated fatal error, rp %s",
2084 		    rnode4info(rp)));
2085 		goto out;
2086 	}
2087 
2088 	/*
2089 	 * If we don't appear to be dealing with the root node, find it.
2090 	 */
2091 	if ((vp->v_flag & VROOT) == 0) {
2092 		nfs4_error_zinit(&e);
2093 		e.error = VFS_ROOT(vp->v_vfsp, &rootvp);
2094 		if (e.error) {
2095 			nfs4_fail_recov(vp, fail_msg, 0, NFS4ERR_STALE);
2096 			NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2097 			    "recov_stale: can't find root node for rp %s",
2098 			    rnode4info(rp)));
2099 			goto out;
2100 		}
2101 	}
2102 
2103 	/* Try a GETATTR on the root vnode */
2104 	if (rootvp != NULL) {
2105 		nfs4_error_zinit(&e);
2106 		nfs4_getattr_otw_norecovery(rootvp, &gar, &e, CRED(), 0);
2107 
2108 		/* Try recovery? */
2109 		if (e.error != 0 || e.stat != NFS4ERR_STALE) {
2110 			needrecov = nfs4_needs_recovery(&e, FALSE, vp->v_vfsp);
2111 			if (needrecov) {
2112 				(void) nfs4_start_recovery(&e,
2113 					mi, rootvp, NULL, NULL, NULL,
2114 					OP_GETATTR, NULL);
2115 				NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2116 				    "recov_stale: error=%d, stat=%d seen "
2117 				    "on rp %s", e.error, e.stat,
2118 				    rnode4info(rp)));
2119 			}
2120 		}
2121 
2122 		/*
2123 		 * Check to see if a failover attempt is warranted
2124 		 * NB: nfs4_try_failover doesn't check for STALE
2125 		 * because recov_stale gets a shot first.  Now that
2126 		 * recov_stale has failed, go ahead and try failover.
2127 		 *
2128 		 * If the getattr on the root filehandle was successful,
2129 		 * then mark recovery as failed for 'vp' and exit.
2130 		 */
2131 		if (nfs4_try_failover(&e) == 0 && e.stat != NFS4ERR_STALE) {
2132 			/*
2133 			 * pass the original error to fail_recov, not
2134 			 * the one from trying the root vnode.
2135 			 */
2136 			nfs4_fail_recov(vp, fail_msg, 0, NFS4ERR_STALE);
2137 			NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2138 			    "recov_stale: root node OK, marking "
2139 			    "dead rp %s", rnode4info(rp)));
2140 			goto out;
2141 		}
2142 	}
2143 
2144 	/*
2145 	 * Here, we know that both the original file and the
2146 	 * root filehandle (which may be the same) are stale.
2147 	 * We want to fail over if we can, and if we can't, we
2148 	 * want to mark everything in sight bad.
2149 	 */
2150 	if (FAILOVER_MOUNT4(mi)) {
2151 		mutex_enter(&mi->mi_lock);
2152 		mi->mi_recovflags |= MI4R_NEED_NEW_SERVER;
2153 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2154 		    "recov_stale: failing over due to rp %s",
2155 		    rnode4info(rp)));
2156 		mutex_exit(&mi->mi_lock);
2157 	} else {
2158 		rnode4_t *rootrp;
2159 		servinfo4_t *svp;
2160 
2161 		/*
2162 		 * Can't fail over, so mark things dead.
2163 		 *
2164 		 * If rootvp is set, we know we have a distinct
2165 		 * non-root vnode which can be marked dead in
2166 		 * the usual way.
2167 		 *
2168 		 * Then we want to mark the root vnode dead.
2169 		 * Note that if rootvp wasn't set, our vp is
2170 		 * actually the root vnode.
2171 		 */
2172 		if (rootvp != NULL) {
2173 			NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
2174 			    "recov_stale: can't fail over, marking dead rp %s",
2175 			    rnode4info(rp)));
2176 			nfs4_fail_recov(vp, fail_msg, 0, NFS4ERR_STALE);
2177 		} else {
2178 			rootvp = vp;
2179 			VN_HOLD(rootvp);
2180 		}
2181 
2182 		/*
2183 		 * Mark root dead, but quietly - since
2184 		 * the root rnode is frequently recreated,
2185 		 * we can encounter this at every access.
2186 		 * Also mark recovery as failed on this VFS.
2187 		 */
2188 		rootrp = VTOR4(rootvp);
2189 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_CONT,
2190 		    "recov_stale: marking dead root rp %s",
2191 		    rnode4info(rootrp)));
2192 		mutex_enter(&rootrp->r_statelock);
2193 		rootrp->r_flags |= (R4RECOVERR | R4STALE);
2194 		rootrp->r_error = ESTALE;
2195 		mutex_exit(&rootrp->r_statelock);
2196 		mutex_enter(&mi->mi_lock);
2197 		mi->mi_error = ESTALE;
2198 		mutex_exit(&mi->mi_lock);
2199 
2200 		svp = mi->mi_curr_serv;
2201 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
2202 		svp->sv_flags |= SV4_ROOT_STALE;
2203 		nfs_rw_exit(&svp->sv_lock);
2204 	}
2205 
2206 out:
2207 	if (rootvp)
2208 		VN_RELE(rootvp);
2209 }
2210 
2211 /*
2212  * Locks.
2213  */
2214 
2215 /*
2216  * Reclaim all the active (acquired) locks for the given file.
2217  * If a process lost a lock, the process is sent a SIGLOST.  This is not
2218  * considered an error.
2219  *
2220  * Return values:
2221  * Errors and status are returned via the nfs4_error_t parameter
2222  * If an error indicates that recovery is needed, the caller is responsible
2223  * for dealing with it.
2224  */
2225 
2226 static void
2227 relock_file(vnode_t *vp, mntinfo4_t *mi, nfs4_error_t *ep,
2228     fattr4_change pre_change)
2229 {
2230 	locklist_t *locks, *llp;
2231 	rnode4_t *rp;
2232 
2233 	ASSERT(ep != NULL);
2234 	nfs4_error_zinit(ep);
2235 
2236 	if (VTOMI4(vp)->mi_flags & MI4_LLOCK)
2237 		return;
2238 
2239 	nfs4_flush_lock_owners(VTOR4(vp));
2240 
2241 	/*
2242 	 * If we get an error that requires recovery actions, just bail out
2243 	 * and let the top-level recovery code handle it.
2244 	 *
2245 	 * If we get some other error, kill the process that owned the lock
2246 	 * and mark its remaining locks (if any) as belonging to NOPID, so
2247 	 * that we don't make any more reclaim requests for that process.
2248 	 */
2249 
2250 	rp = VTOR4(vp);
2251 	locks = flk_active_locks_for_vp(vp);
2252 	for (llp = locks; llp != NULL; llp = llp->ll_next) {
2253 		int did_reclaim = 1;
2254 
2255 		ASSERT(llp->ll_vp == vp);
2256 		if (llp->ll_flock.l_pid == NOPID)
2257 			continue;
2258 		reclaim_one_lock(vp, &llp->ll_flock, ep, &did_reclaim);
2259 		/*
2260 		 * If we need to restart recovery, stop processing the
2261 		 * list.  Some errors would be recoverable under other
2262 		 * circumstances, but if they happen here we just give up
2263 		 * on the lock.
2264 		 */
2265 		if (nfs4_needs_recovery(ep, TRUE, vp->v_vfsp)) {
2266 			if (ep->error != 0)
2267 				break;
2268 			if (!nfs4_recov_marks_dead(ep->stat))
2269 				break;
2270 		}
2271 		/*
2272 		 *   In case the server isn't offering us a grace period, or
2273 		 * if we missed it, we might have opened & locked from scratch,
2274 		 * rather than reopened/reclaimed.
2275 		 *   We need to ensure that the object hadn't been otherwise
2276 		 * changed during this time, by comparing the changeinfo.
2277 		 *   We get passed the changeinfo from before the reopen by our
2278 		 * caller, in pre_change.
2279 		 *   The changeinfo from after the reopen is in rp->r_change,
2280 		 * courtesy of the GETATTR in the reopen.
2281 		 *   If they're different, then the file has changed, and we
2282 		 * have to SIGLOST the app.
2283 		 */
2284 		if (ep->error == 0 && ep->stat == NFS4_OK && !did_reclaim) {
2285 			mutex_enter(&rp->r_statelock);
2286 			if (pre_change != rp->r_change)
2287 				ep->stat = NFS4ERR_NO_GRACE;
2288 			mutex_exit(&rp->r_statelock);
2289 		}
2290 		if (ep->error != 0 || ep->stat != NFS4_OK) {
2291 			if (ep->error != 0)
2292 				nfs4_queue_event(RE_FAIL_RELOCK, mi,
2293 				    NULL, ep->error, vp, NULL, 0, NULL,
2294 				    llp->ll_flock.l_pid, TAG_NONE, TAG_NONE,
2295 				    0, 0);
2296 			else
2297 				nfs4_queue_event(RE_FAIL_RELOCK, mi,
2298 				    NULL, 0, vp, NULL, ep->stat, NULL,
2299 				    llp->ll_flock.l_pid, TAG_NONE, TAG_NONE,
2300 				    0, 0);
2301 			nfs4_send_siglost(llp->ll_flock.l_pid, mi, vp, TRUE,
2302 			    ep->error, ep->stat);
2303 			relock_skip_pid(llp, llp->ll_flock.l_pid);
2304 
2305 			/* Reinitialize the nfs4_error and continue */
2306 			nfs4_error_zinit(ep);
2307 		}
2308 	}
2309 
2310 	if (locks != NULL)
2311 		flk_free_locklist(locks);
2312 }
2313 
2314 /*
2315  * Reclaim the given lock.
2316  * If the lock can't be reclaimed, the process is sent SIGLOST, but this is
2317  * not considered an error.
2318  *
2319  * Errors are returned via the nfs4_error_t parameter.
2320  */
2321 static void
2322 reclaim_one_lock(vnode_t *vp, flock64_t *flk, nfs4_error_t *ep,
2323 	int *did_reclaimp)
2324 {
2325 	cred_t *cr;
2326 	rnode4_t *rp = VTOR4(vp);
2327 
2328 	cr = pid_to_cr(flk->l_pid);
2329 	if (cr == NULL) {
2330 		nfs4_error_zinit(ep);
2331 		ep->error = ESRCH;
2332 		return;
2333 	}
2334 
2335 	do {
2336 		mutex_enter(&rp->r_statelock);
2337 		if (rp->r_flags & R4RECOVERR) {
2338 			/*
2339 			 * This shouldn't affect other reclaims, so don't
2340 			 * return an error.
2341 			 */
2342 			mutex_exit(&rp->r_statelock);
2343 			break;
2344 		}
2345 		mutex_exit(&rp->r_statelock);
2346 
2347 		nfs4frlock(NFS4_LCK_CTYPE_RECLAIM, vp, F_SETLK, flk,
2348 				FREAD|FWRITE, 0, cr, ep, NULL, did_reclaimp);
2349 		if (ep->error == 0 && ep->stat == NFS4ERR_FHEXPIRED)
2350 			start_recovery_action(NR_FHEXPIRED, TRUE, VTOMI4(vp),
2351 					    vp, NULL);
2352 	} while (ep->error == 0 && ep->stat == NFS4ERR_FHEXPIRED);
2353 
2354 	crfree(cr);
2355 }
2356 
2357 /*
2358  * Open files.
2359  */
2360 
2361 /*
2362  * Verifies if the nfsstat4 is a valid error for marking this vnode dead.
2363  * Returns 1 if the error is valid; 0 otherwise.
2364  */
2365 static int
2366 nfs4_valid_recov_err_for_vp(vnode_t *vp, nfsstat4 stat)
2367 {
2368 	/*
2369 	 * We should not be marking non-regular files as dead,
2370 	 * except in very rare cases (eg: BADHANDLE or NFS4ERR_BADNAME).
2371 	 */
2372 	if (vp->v_type != VREG && stat != NFS4ERR_BADHANDLE &&
2373 	    stat != NFS4ERR_BADNAME)
2374 		return (0);
2375 
2376 	return (1);
2377 }
2378 
2379 /*
2380  * Failed attempting to recover a filehandle.  If 'stat' is valid for 'vp',
2381  * then mark the object dead.  Since we've had to do a lookup for
2382  * filehandle recovery, we will mark the object dead if we got NOENT.
2383  */
2384 static void
2385 nfs4_recov_fh_fail(vnode_t *vp, int error, nfsstat4 stat)
2386 {
2387 	ASSERT(vp != NULL);
2388 
2389 	if ((error == 0) && (stat != NFS4ERR_NOENT) &&
2390 	    (!nfs4_valid_recov_err_for_vp(vp, stat)))
2391 		return;
2392 
2393 	nfs4_fail_recov(vp, "can't recover filehandle", error, stat);
2394 }
2395 
2396 /*
2397  * Recovery from a "shouldn't happen" error.  In the long term, we'd like
2398  * to mark only the data structure(s) that provided the bad value as being
2399  * bad.  But for now we'll just mark the entire file.
2400  */
2401 
2402 static void
2403 recov_badstate(recov_info_t *recovp, vnode_t *vp, nfsstat4 stat)
2404 {
2405 	ASSERT(vp != NULL);
2406 	recov_throttle(recovp, vp);
2407 
2408 	if (!nfs4_valid_recov_err_for_vp(vp, stat))
2409 		return;
2410 
2411 	nfs4_fail_recov(vp, "", 0, stat);
2412 }
2413 
2414 /*
2415  * Free up the information saved for a lost state request.
2416  */
2417 static void
2418 nfs4_free_lost_rqst(nfs4_lost_rqst_t *lrp, nfs4_server_t *sp)
2419 {
2420 	component4 *filep;
2421 	nfs4_open_stream_t *osp;
2422 	int have_sync_lock;
2423 
2424 	NFS4_DEBUG(nfs4_lost_rqst_debug,
2425 		(CE_NOTE, "nfs4_free_lost_rqst:"));
2426 
2427 	switch (lrp->lr_op) {
2428 	case OP_OPEN:
2429 		filep = &lrp->lr_ofile;
2430 		if (filep->utf8string_val) {
2431 			kmem_free(filep->utf8string_val, filep->utf8string_len);
2432 			filep->utf8string_val = NULL;
2433 		}
2434 		break;
2435 	case OP_DELEGRETURN:
2436 		nfs4delegreturn_cleanup(VTOR4(lrp->lr_vp), sp);
2437 		break;
2438 	case OP_CLOSE:
2439 		osp = lrp->lr_osp;
2440 		ASSERT(osp != NULL);
2441 		mutex_enter(&osp->os_sync_lock);
2442 		have_sync_lock = 1;
2443 		if (osp->os_pending_close) {
2444 			/* clean up the open file state. */
2445 			osp->os_pending_close = 0;
2446 			nfs4close_notw(lrp->lr_vp, osp, &have_sync_lock);
2447 		}
2448 		if (have_sync_lock)
2449 			mutex_exit(&osp->os_sync_lock);
2450 		break;
2451 	}
2452 
2453 	lrp->lr_op = 0;
2454 	if (lrp->lr_oop != NULL) {
2455 		open_owner_rele(lrp->lr_oop);
2456 		lrp->lr_oop = NULL;
2457 	}
2458 	if (lrp->lr_osp != NULL) {
2459 		open_stream_rele(lrp->lr_osp, VTOR4(lrp->lr_vp));
2460 		lrp->lr_osp = NULL;
2461 	}
2462 	if (lrp->lr_lop != NULL) {
2463 		lock_owner_rele(lrp->lr_lop);
2464 		lrp->lr_lop = NULL;
2465 	}
2466 	if (lrp->lr_flk != NULL) {
2467 		kmem_free(lrp->lr_flk, sizeof (flock64_t));
2468 		lrp->lr_flk = NULL;
2469 	}
2470 	if (lrp->lr_vp != NULL) {
2471 		VN_RELE(lrp->lr_vp);
2472 		lrp->lr_vp = NULL;
2473 	}
2474 	if (lrp->lr_dvp != NULL) {
2475 		VN_RELE(lrp->lr_dvp);
2476 		lrp->lr_dvp = NULL;
2477 	}
2478 	if (lrp->lr_cr != NULL) {
2479 		crfree(lrp->lr_cr);
2480 		lrp->lr_cr = NULL;
2481 	}
2482 
2483 	kmem_free(lrp, sizeof (nfs4_lost_rqst_t));
2484 }
2485 
2486 /*
2487  * Remove any lost state requests and free them.
2488  */
2489 static void
2490 nfs4_remove_lost_rqsts(mntinfo4_t *mi, nfs4_server_t *sp)
2491 {
2492 	nfs4_lost_rqst_t *lrp;
2493 
2494 	mutex_enter(&mi->mi_lock);
2495 	while ((lrp = list_head(&mi->mi_lost_state)) != NULL) {
2496 		list_remove(&mi->mi_lost_state, lrp);
2497 		mutex_exit(&mi->mi_lock);
2498 		nfs4_free_lost_rqst(lrp, sp);
2499 		mutex_enter(&mi->mi_lock);
2500 	}
2501 	mutex_exit(&mi->mi_lock);
2502 }
2503 
2504 /*
2505  * Reopen all the files for the given filesystem and reclaim any locks.
2506  */
2507 
2508 static void
2509 recov_openfiles(recov_info_t *recovp, nfs4_server_t *sp)
2510 {
2511 	mntinfo4_t *mi = recovp->rc_mi;
2512 	nfs4_opinst_t *reopenlist = NULL, *rep;
2513 	nfs4_error_t e = { 0, NFS4_OK, RPC_SUCCESS };
2514 	open_claim_type4 claim;
2515 	int remap;
2516 	char *fail_msg = "No such file or directory on replica";
2517 	rnode4_t *rp;
2518 	fattr4_change pre_change;
2519 
2520 	ASSERT(sp != NULL);
2521 
2522 	/*
2523 	 * This check is to allow a 10ms pause before we reopen files
2524 	 * it should allow the server time to have received the CB_NULL
2525 	 * reply and update its internal structures such that (if
2526 	 * applicable) we are granted a delegation on reopened files.
2527 	 */
2528 	mutex_enter(&sp->s_lock);
2529 	if ((sp->s_flags & (N4S_CB_PINGED | N4S_CB_WAITER)) == 0) {
2530 		sp->s_flags |= N4S_CB_WAITER;
2531 		(void) cv_timedwait(&sp->wait_cb_null, &sp->s_lock,
2532 			(lbolt+drv_usectohz(N4S_CB_PAUSE_TIME)));
2533 	}
2534 	mutex_exit(&sp->s_lock);
2535 
2536 	(void) nfs_rw_enter_sig(&sp->s_recovlock, RW_READER, 0);
2537 	(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_WRITER, 0);
2538 
2539 	if (NFS4_VOLATILE_FH(mi)) {
2540 		nfs4_remap_root(mi, &e, 0);
2541 		if (nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp)) {
2542 			(void) nfs4_start_recovery(&e, mi, NULL,
2543 					NULL, NULL, NULL, OP_LOOKUP, NULL);
2544 		}
2545 	}
2546 
2547 	mutex_enter(&mi->mi_lock);
2548 	if (recovp->rc_srv_reboot || (mi->mi_recovflags & MI4R_SRV_REBOOT))
2549 		claim = CLAIM_PREVIOUS;
2550 	else
2551 		claim = CLAIM_NULL;
2552 	mutex_exit(&mi->mi_lock);
2553 
2554 	if (e.error == 0 && e.stat == NFS4_OK) {
2555 		/*
2556 		 * Get a snapshot of open files in the filesystem.  Note
2557 		 * that new opens will stall until the server's grace
2558 		 * period is done.
2559 		 */
2560 		reopenlist = r4mkopenlist(mi);
2561 
2562 		mutex_enter(&mi->mi_lock);
2563 		remap = mi->mi_recovflags & MI4R_REMAP_FILES;
2564 		mutex_exit(&mi->mi_lock);
2565 		/*
2566 		 * Since we are re-establishing state on the
2567 		 * server, its ok to blow away the saved lost
2568 		 * requests since we don't need to reissue it.
2569 		 */
2570 		nfs4_remove_lost_rqsts(mi, sp);
2571 
2572 		for (rep = reopenlist; rep; rep = rep->re_next) {
2573 
2574 			if (remap) {
2575 				nfs4_remap_file(mi, rep->re_vp,
2576 					NFS4_REMAP_CKATTRS, &e);
2577 			}
2578 			if (e.error == ENOENT || e.stat == NFS4ERR_NOENT) {
2579 				/*
2580 				 * The current server does not have the file
2581 				 * that is to be remapped.  This is most
2582 				 * likely due to an improperly maintained
2583 				 * replica.   The files that are missing from
2584 				 * the server will be marked dead and logged
2585 				 * in order to make sys admins aware of the
2586 				 * problem.
2587 				 */
2588 				nfs4_fail_recov(rep->re_vp,
2589 					fail_msg, e.error, e.stat);
2590 				/*
2591 				 * We've already handled the error so clear it.
2592 				 */
2593 				nfs4_error_zinit(&e);
2594 				continue;
2595 			} else if (e.error == 0 && e.stat == NFS4_OK) {
2596 				int j;
2597 
2598 				rp = VTOR4(rep->re_vp);
2599 				mutex_enter(&rp->r_statelock);
2600 				pre_change = rp->r_change;
2601 				mutex_exit(&rp->r_statelock);
2602 
2603 				for (j = 0; j < rep->re_numosp; j++) {
2604 					nfs4_reopen(rep->re_vp, rep->re_osp[j],
2605 						&e, claim, FALSE, TRUE);
2606 					if (e.error != 0 || e.stat != NFS4_OK)
2607 						break;
2608 				}
2609 				if (nfs4_needs_recovery(&e, TRUE,
2610 				    mi->mi_vfsp)) {
2611 					(void) nfs4_start_recovery(&e, mi,
2612 						rep->re_vp, NULL, NULL, NULL,
2613 						OP_OPEN, NULL);
2614 					break;
2615 				}
2616 			}
2617 #ifdef DEBUG
2618 			if (nfs4_recovdelay > 0)
2619 				delay(MSEC_TO_TICK(nfs4_recovdelay * 1000));
2620 #endif
2621 			if (e.error == 0 && e.stat == NFS4_OK)
2622 				relock_file(rep->re_vp, mi, &e, pre_change);
2623 
2624 			if (nfs4_needs_recovery(&e, TRUE, mi->mi_vfsp))
2625 				(void) nfs4_start_recovery(&e, mi,
2626 					rep->re_vp, NULL, NULL, NULL, OP_LOCK,
2627 					NULL);
2628 			if (e.error != 0 || e.stat != NFS4_OK)
2629 				break;
2630 		}
2631 
2632 		/*
2633 		 * Check to see if we need to remap files passed in
2634 		 * via the recovery arguments; this will have been
2635 		 * done for open files.  A failure here is not fatal.
2636 		 */
2637 		if (remap) {
2638 			nfs4_error_t ignore;
2639 			nfs4_check_remap(mi, recovp->rc_vp1, NFS4_REMAP_CKATTRS,
2640 				&ignore);
2641 			nfs4_check_remap(mi, recovp->rc_vp2, NFS4_REMAP_CKATTRS,
2642 				&ignore);
2643 		}
2644 	}
2645 
2646 	if (e.error == 0 && e.stat == NFS4_OK) {
2647 		mutex_enter(&mi->mi_lock);
2648 		mi->mi_recovflags &= ~(MI4R_REOPEN_FILES | MI4R_REMAP_FILES);
2649 		mutex_exit(&mi->mi_lock);
2650 	}
2651 
2652 	nfs_rw_exit(&mi->mi_recovlock);
2653 	nfs_rw_exit(&sp->s_recovlock);
2654 
2655 	if (reopenlist != NULL)
2656 		r4releopenlist(reopenlist);
2657 }
2658 
2659 /*
2660  * Resend the queued state recovery requests in "rqsts".
2661  */
2662 
2663 static void
2664 nfs4_resend_lost_rqsts(recov_info_t *recovp, nfs4_server_t *sp)
2665 {
2666 	nfs4_lost_rqst_t	*lrp, *tlrp;
2667 	mntinfo4_t		*mi = recovp->rc_mi;
2668 	nfs4_error_t		n4e;
2669 #ifdef NOTYET
2670 	uint32_t		deny_bits = 0;
2671 #endif
2672 
2673 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "nfs4_resend_lost_rqsts"));
2674 
2675 	ASSERT(mi != NULL);
2676 	ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
2677 
2678 	mutex_enter(&mi->mi_lock);
2679 	lrp = list_head(&mi->mi_lost_state);
2680 	mutex_exit(&mi->mi_lock);
2681 	while (lrp != NULL) {
2682 		nfs4_error_zinit(&n4e);
2683 		resend_one_op(lrp, &n4e, mi, sp);
2684 		NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
2685 		    "nfs4_resend_lost_rqsts: resend request: for vp %p got "
2686 		    "error %d stat %d", (void *)lrp->lr_vp, n4e.error,
2687 		    n4e.stat));
2688 
2689 		/*
2690 		 * If we get a recovery error that we can actually
2691 		 * recover from (such as ETIMEDOUT, FHEXPIRED), we
2692 		 * return and let the recovery thread redrive the call.
2693 		 * Don't requeue unless the zone is still healthy.
2694 		 */
2695 		if (zone_status_get(curproc->p_zone) < ZONE_IS_SHUTTING_DOWN &&
2696 		    nfs4_needs_recovery(&n4e, TRUE, mi->mi_vfsp) &&
2697 		    (nfs4_try_failover(&n4e) ||
2698 		    NFS4_FRC_UNMT_ERR(n4e.error, mi->mi_vfsp) ||
2699 		    (n4e.error == 0 && n4e.stat != NFS4ERR_BADHANDLE &&
2700 		    !nfs4_recov_marks_dead(n4e.stat)))) {
2701 			/*
2702 			 * For these three errors, we want to delay a bit
2703 			 * instead of pounding the server into submission.
2704 			 * We have to do this manually; the normal
2705 			 * processing for these errors only works for
2706 			 * non-recovery requests.
2707 			 */
2708 			if ((n4e.error == 0 && n4e.stat == NFS4ERR_DELAY) ||
2709 			    (n4e.error == 0 && n4e.stat == NFS4ERR_GRACE) ||
2710 			    (n4e.error == 0 && n4e.stat == NFS4ERR_RESOURCE) ||
2711 			    NFS4_FRC_UNMT_ERR(n4e.error, mi->mi_vfsp)) {
2712 				delay(SEC_TO_TICK(nfs4err_delay_time));
2713 			} else {
2714 				(void) nfs4_start_recovery(&n4e,
2715 					mi, lrp->lr_dvp, lrp->lr_vp, NULL, NULL,
2716 					lrp->lr_op, NULL);
2717 			}
2718 			return;
2719 		}
2720 
2721 		mutex_enter(&mi->mi_lock);
2722 		list_remove(&mi->mi_lost_state, lrp);
2723 		tlrp = lrp;
2724 		lrp = list_head(&mi->mi_lost_state);
2725 		mutex_exit(&mi->mi_lock);
2726 		nfs4_free_lost_rqst(tlrp, sp);
2727 	}
2728 }
2729 
2730 /*
2731  * Resend the given op, and issue any necessary undo call.
2732  * errors are returned via the nfs4_error_t parameter.
2733  */
2734 
2735 static void
2736 resend_one_op(nfs4_lost_rqst_t *lrp, nfs4_error_t *ep,
2737 	mntinfo4_t *mi, nfs4_server_t *sp)
2738 {
2739 	vnode_t *vp;
2740 	nfs4_open_stream_t *osp;
2741 	cred_t *cr;
2742 	uint32_t acc_bits;
2743 
2744 	vp = lrp->lr_vp;
2745 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "resend_one_op: "
2746 	    "have a lost open/close request for vp %p", (void *)vp));
2747 
2748 	switch (lrp->lr_op) {
2749 	case OP_OPEN:
2750 		nfs4_resend_open_otw(&vp, lrp, ep);
2751 		break;
2752 	case OP_OPEN_DOWNGRADE:
2753 		ASSERT(lrp->lr_oop != NULL);
2754 		ep->error = nfs4_start_open_seqid_sync(lrp->lr_oop, mi);
2755 		ASSERT(!ep->error);	/* recov thread always succeeds */
2756 		ASSERT(lrp->lr_osp != NULL);
2757 		mutex_enter(&lrp->lr_osp->os_sync_lock);
2758 		nfs4_open_downgrade(lrp->lr_dg_acc, lrp->lr_dg_deny,
2759 			    lrp->lr_oop, lrp->lr_osp, vp, lrp->lr_cr, lrp,
2760 			    ep, NULL, NULL);
2761 		mutex_exit(&lrp->lr_osp->os_sync_lock);
2762 		nfs4_end_open_seqid_sync(lrp->lr_oop);
2763 		break;
2764 	case OP_CLOSE:
2765 		osp = lrp->lr_osp;
2766 		cr = lrp->lr_cr;
2767 		acc_bits = 0;
2768 		mutex_enter(&osp->os_sync_lock);
2769 		if (osp->os_share_acc_read)
2770 			acc_bits |= OPEN4_SHARE_ACCESS_READ;
2771 		if (osp->os_share_acc_write)
2772 			acc_bits |= OPEN4_SHARE_ACCESS_WRITE;
2773 		mutex_exit(&osp->os_sync_lock);
2774 		nfs4close_one(vp, osp, cr, acc_bits, lrp, ep,
2775 				CLOSE_RESEND, 0, 0, 0);
2776 		break;
2777 	case OP_LOCK:
2778 	case OP_LOCKU:
2779 		resend_lock(lrp, ep);
2780 		goto done;
2781 	case OP_DELEGRETURN:
2782 		nfs4_resend_delegreturn(lrp, ep, sp);
2783 		goto done;
2784 	default:
2785 #ifdef DEBUG
2786 		cmn_err(CE_PANIC, "resend_one_op: unexpected op: %d",
2787 			lrp->lr_op);
2788 #endif
2789 		nfs4_queue_event(RE_LOST_STATE_BAD_OP, mi, NULL,
2790 		    lrp->lr_op, lrp->lr_vp, lrp->lr_dvp, NFS4_OK, NULL, 0,
2791 		    TAG_NONE, TAG_NONE, 0, 0);
2792 		nfs4_error_init(ep, EINVAL);
2793 		return;
2794 	}
2795 
2796 	/*
2797 	 * No need to retry nor send an "undo" CLOSE in the
2798 	 * event the server rebooted.
2799 	 */
2800 	if (ep->error == 0 && (ep->stat == NFS4ERR_STALE_CLIENTID ||
2801 	    ep->stat == NFS4ERR_STALE_STATEID || ep->stat == NFS4ERR_EXPIRED))
2802 		goto done;
2803 
2804 	/*
2805 	 * If we resent a CLOSE or OPEN_DOWNGRADE, there's nothing
2806 	 * to undo.  Undoing locking operations was handled by
2807 	 * resend_lock().
2808 	 */
2809 	if (lrp->lr_op == OP_OPEN_DOWNGRADE || lrp->lr_op == OP_CLOSE)
2810 		goto done;
2811 
2812 	/*
2813 	 * If we get any other error for OPEN, then don't attempt
2814 	 * to undo the resend of the open (since it was never
2815 	 * successful!).
2816 	 */
2817 	ASSERT(lrp->lr_op == OP_OPEN);
2818 	if (ep->error || ep->stat != NFS4_OK)
2819 		goto done;
2820 
2821 	/*
2822 	 * Now let's undo our OPEN.
2823 	 */
2824 	nfs4_error_zinit(ep);
2825 	close_after_open_resend(vp, lrp->lr_cr, lrp->lr_oacc, ep);
2826 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "resend_one_op: "
2827 	    "nfs4close_one: for vp %p got error %d stat %d",
2828 	    (void *)vp, ep->error, ep->stat));
2829 
2830 done:
2831 	if (vp != lrp->lr_vp)
2832 		VN_RELE(vp);
2833 }
2834 
2835 /*
2836  * Close a file that was opened via a resent OPEN.
2837  * Most errors are passed back to the caller (via the return value and
2838  * *statp), except for FHEXPIRED, which is retried.
2839  *
2840  * It might be conceptually cleaner to push the CLOSE request onto the
2841  * front of the resend queue, rather than sending it here.  That would
2842  * match the way we undo lost lock requests.  On the other
2843  * hand, we've already got something that works, and there's no reason to
2844  * change it at this time.
2845  */
2846 
2847 static void
2848 close_after_open_resend(vnode_t *vp, cred_t *cr, uint32_t acc_bits,
2849 			nfs4_error_t *ep)
2850 {
2851 
2852 	for (;;) {
2853 		nfs4close_one(vp, NULL, cr, acc_bits, NULL, ep,
2854 				CLOSE_AFTER_RESEND, 0, 0, 0);
2855 		if (ep->error == 0 && ep->stat == NFS4_OK)
2856 			break;		/* success; done */
2857 		if (ep->error != 0 || ep->stat != NFS4ERR_FHEXPIRED)
2858 			break;
2859 		/* else retry FHEXPIRED */
2860 	}
2861 
2862 }
2863 
2864 /*
2865  * Resend the given lost lock request.  Return an errno value.  If zero,
2866  * *statp is set to the NFS status code for the call.
2867  *
2868  * Issue a SIGLOST and mark the rnode dead if we get a non-recovery error or
2869  * a recovery error that we don't actually recover from yet (eg: BAD_SEQID).
2870  * Let the recovery thread redrive the call if we get a recovery error that
2871  * we can actually recover from.
2872  */
2873 static void
2874 resend_lock(nfs4_lost_rqst_t *lrp, nfs4_error_t *ep)
2875 {
2876 	bool_t		send_siglost = FALSE;
2877 	vnode_t		*vp = lrp->lr_vp;
2878 
2879 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "resend_lock:"));
2880 	ASSERT(lrp->lr_ctype == NFS4_LCK_CTYPE_REINSTATE ||
2881 	    lrp->lr_ctype == NFS4_LCK_CTYPE_RESEND);
2882 
2883 	nfs4frlock(lrp->lr_ctype, vp, F_SETLK,
2884 		    lrp->lr_flk, FREAD|FWRITE, 0, lrp->lr_cr, ep, lrp, NULL);
2885 
2886 	NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE, "resend_lock: "
2887 	    "nfs4frlock for vp %p returned error %d, stat %d",
2888 	    (void *)vp, ep->error, ep->stat));
2889 
2890 	if (ep->error == 0 && ep->stat == 0)
2891 		goto done;
2892 	if (ep->error == 0 && ep->stat == NFS4ERR_DENIED &&
2893 	    lrp->lr_ctype == NFS4_LCK_CTYPE_RESEND)
2894 		goto done;
2895 
2896 	/*
2897 	 * If we failed with a non-recovery error, send SIGLOST and
2898 	 * mark the file dead.
2899 	 */
2900 	if (!nfs4_needs_recovery(ep, TRUE, vp->v_vfsp))
2901 		send_siglost = TRUE;
2902 	else {
2903 		/*
2904 		 * Done with recovering LOST LOCK in the event the
2905 		 * server rebooted or we've lost the lease.
2906 		 */
2907 		if (ep->error == 0 && (ep->stat == NFS4ERR_STALE_CLIENTID ||
2908 		    ep->stat == NFS4ERR_STALE_STATEID ||
2909 		    ep->stat == NFS4ERR_EXPIRED)) {
2910 			goto done;
2911 		}
2912 
2913 		/*
2914 		 * BAD_STATEID on an unlock indicates that the server has
2915 		 * forgotten about the lock anyway, so act like the call
2916 		 * was successful.
2917 		 */
2918 		if (ep->error == 0 && ep->stat == NFS4ERR_BAD_STATEID &&
2919 		    lrp->lr_op == OP_LOCKU)
2920 			goto done;
2921 
2922 		/*
2923 		 * If we got a recovery error that we don't actually
2924 		 * recover from, send SIGLOST.  If the filesystem was
2925 		 * forcibly unmounted, we skip the SIGLOST because (a) it's
2926 		 * unnecessary noise, and (b) there could be a new process
2927 		 * with the same pid as the one that had generated the lost
2928 		 * state request.
2929 		 */
2930 		if (ep->error == 0 && (ep->stat == NFS4ERR_BADHANDLE ||
2931 		    nfs4_recov_marks_dead(ep->stat))) {
2932 			if (!(vp->v_vfsp->vfs_flag & VFS_UNMOUNTED))
2933 				send_siglost = TRUE;
2934 			goto done;
2935 		}
2936 
2937 		/*
2938 		 * If the filesystem was forcibly unmounted, we
2939 		 * still need to synchronize with the server and
2940 		 * release state.  Try again later.
2941 		 */
2942 		if (NFS4_FRC_UNMT_ERR(ep->error, vp->v_vfsp))
2943 			goto done;
2944 
2945 		/*
2946 		 * If we get a recovery error that we can actually
2947 		 * recover from (such as ETIMEDOUT, FHEXPIRED),
2948 		 * return and let the recovery thread redrive the call.
2949 		 *
2950 		 * For the three errors below, we want to delay a bit
2951 		 * instead of pounding the server into submission.
2952 		 */
2953 		if ((ep->error == 0 && ep->stat == NFS4ERR_DELAY) ||
2954 		    (ep->error == 0 && ep->stat == NFS4ERR_GRACE) ||
2955 		    (ep->error == 0 && ep->stat == NFS4ERR_RESOURCE))
2956 			delay(SEC_TO_TICK(recov_err_delay));
2957 		goto done;
2958 	}
2959 
2960 done:
2961 	if (send_siglost) {
2962 		cred_t *sv_cred;
2963 
2964 		/*
2965 		 * Must be root or the actual thread being issued the
2966 		 * SIGLOST for this to work, so just become root.
2967 		 */
2968 		sv_cred = curthread->t_cred;
2969 		curthread->t_cred = kcred;
2970 		nfs4_send_siglost(lrp->lr_flk->l_pid, VTOMI4(vp), vp, FALSE,
2971 		    ep->error, ep->stat);
2972 		curthread->t_cred = sv_cred;
2973 
2974 		/*
2975 		 * Flush any additional reinstantiation requests for
2976 		 * this operation.  Sending multiple SIGLOSTs to the user
2977 		 * process is unlikely to help and may cause trouble.
2978 		 */
2979 		if (lrp->lr_ctype == NFS4_LCK_CTYPE_REINSTATE)
2980 			flush_reinstate(lrp);
2981 	}
2982 }
2983 
2984 /*
2985  * Remove any lock reinstantiation requests that correspond to the given
2986  * lost request.  We only remove items that follow lrp in the queue,
2987  * assuming that lrp will be removed by the generic lost state code.
2988  */
2989 
2990 static void
2991 flush_reinstate(nfs4_lost_rqst_t *lrp)
2992 {
2993 	vnode_t *vp;
2994 	pid_t pid;
2995 	mntinfo4_t *mi;
2996 	nfs4_lost_rqst_t *nlrp;
2997 
2998 	vp = lrp->lr_vp;
2999 	mi = VTOMI4(vp);
3000 	pid = lrp->lr_flk->l_pid;
3001 
3002 	/*
3003 	 * If there are any more reinstantation requests to get rid of,
3004 	 * they should all be clustered at the front of the lost state
3005 	 * queue.
3006 	 */
3007 	mutex_enter(&mi->mi_lock);
3008 	for (lrp = list_next(&mi->mi_lost_state, lrp); lrp != NULL;
3009 	    lrp = nlrp) {
3010 		nlrp = list_next(&mi->mi_lost_state, lrp);
3011 		if (lrp->lr_op != OP_LOCK && lrp->lr_op != OP_LOCKU)
3012 			break;
3013 		if (lrp->lr_ctype != NFS4_LCK_CTYPE_REINSTATE)
3014 			break;
3015 		ASSERT(lrp->lr_vp == vp);
3016 		ASSERT(lrp->lr_flk->l_pid == pid);
3017 		NFS4_DEBUG(nfs4_lost_rqst_debug, (CE_NOTE,
3018 				"remove reinstantiation %p", (void *)lrp));
3019 		list_remove(&mi->mi_lost_state, lrp);
3020 		nfs4_free_lost_rqst(lrp, NULL);
3021 	}
3022 	mutex_exit(&mi->mi_lock);
3023 }
3024 
3025 /*
3026  * End of state-specific recovery routines.
3027  */
3028 
3029 /*
3030  * Allocate a lost request struct, initialize it from lost_rqstp (including
3031  * bumping the reference counts for the referenced vnode, etc.), and hang
3032  * it off of recovp.
3033  */
3034 
3035 static void
3036 nfs4_save_lost_rqst(nfs4_lost_rqst_t *lost_rqstp, recov_info_t *recovp,
3037 	nfs4_recov_t *action, mntinfo4_t *mi)
3038 {
3039 	nfs4_lost_rqst_t *destp;
3040 
3041 	ASSERT(recovp->rc_lost_rqst == NULL);
3042 
3043 	destp = kmem_alloc(sizeof (nfs4_lost_rqst_t), KM_SLEEP);
3044 	recovp->rc_lost_rqst = destp;
3045 
3046 	if (lost_rqstp->lr_op == OP_LOCK ||
3047 	    lost_rqstp->lr_op == OP_LOCKU) {
3048 		ASSERT(lost_rqstp->lr_lop);
3049 		*action = NR_LOST_LOCK;
3050 		destp->lr_ctype = lost_rqstp->lr_ctype;
3051 		destp->lr_locktype = lost_rqstp->lr_locktype;
3052 	} else if (lost_rqstp->lr_op == OP_OPEN) {
3053 		component4 *srcfp, *destfp;
3054 
3055 		destp->lr_oacc = lost_rqstp->lr_oacc;
3056 		destp->lr_odeny = lost_rqstp->lr_odeny;
3057 		destp->lr_oclaim = lost_rqstp->lr_oclaim;
3058 		if (lost_rqstp->lr_oclaim == CLAIM_DELEGATE_CUR)
3059 			destp->lr_ostateid = lost_rqstp->lr_ostateid;
3060 
3061 		srcfp = &lost_rqstp->lr_ofile;
3062 		destfp = &destp->lr_ofile;
3063 		/*
3064 		 * Consume caller's utf8string
3065 		 */
3066 		destfp->utf8string_len = srcfp->utf8string_len;
3067 		destfp->utf8string_val = srcfp->utf8string_val;
3068 		srcfp->utf8string_len = 0;
3069 		srcfp->utf8string_val = NULL;	/* make sure not reused */
3070 
3071 		*action = NR_LOST_STATE_RQST;
3072 	} else if (lost_rqstp->lr_op == OP_OPEN_DOWNGRADE) {
3073 		destp->lr_dg_acc = lost_rqstp->lr_dg_acc;
3074 		destp->lr_dg_deny = lost_rqstp->lr_dg_deny;
3075 
3076 		*action = NR_LOST_STATE_RQST;
3077 	} else if (lost_rqstp->lr_op == OP_CLOSE) {
3078 		ASSERT(lost_rqstp->lr_oop);
3079 		*action = NR_LOST_STATE_RQST;
3080 	} else if (lost_rqstp->lr_op == OP_DELEGRETURN) {
3081 		*action = NR_LOST_STATE_RQST;
3082 	} else {
3083 #ifdef DEBUG
3084 		cmn_err(CE_PANIC, "nfs4_save_lost_rqst: bad op %d",
3085 			lost_rqstp->lr_op);
3086 #endif
3087 		nfs4_queue_event(RE_LOST_STATE_BAD_OP, mi, NULL,
3088 		    lost_rqstp->lr_op, lost_rqstp->lr_vp, lost_rqstp->lr_dvp,
3089 		    NFS4_OK, NULL, curproc->p_pid, TAG_NONE, TAG_NONE, 0, 0);
3090 		*action = NR_UNUSED;
3091 		recovp->rc_lost_rqst = NULL;
3092 		kmem_free(destp, sizeof (nfs4_lost_rqst_t));
3093 		return;
3094 	}
3095 
3096 	destp->lr_op = lost_rqstp->lr_op;
3097 	destp->lr_vp = lost_rqstp->lr_vp;
3098 	if (destp->lr_vp)
3099 		VN_HOLD(destp->lr_vp);
3100 	destp->lr_dvp = lost_rqstp->lr_dvp;
3101 	if (destp->lr_dvp)
3102 		VN_HOLD(destp->lr_dvp);
3103 	destp->lr_oop = lost_rqstp->lr_oop;
3104 	if (destp->lr_oop)
3105 		open_owner_hold(destp->lr_oop);
3106 	destp->lr_osp = lost_rqstp->lr_osp;
3107 	if (destp->lr_osp)
3108 		open_stream_hold(destp->lr_osp);
3109 	destp->lr_lop = lost_rqstp->lr_lop;
3110 	if (destp->lr_lop)
3111 		lock_owner_hold(destp->lr_lop);
3112 	destp->lr_cr = lost_rqstp->lr_cr;
3113 	if (destp->lr_cr)
3114 		crhold(destp->lr_cr);
3115 	if (lost_rqstp->lr_flk == NULL)
3116 		destp->lr_flk = NULL;
3117 	else {
3118 		destp->lr_flk = kmem_alloc(sizeof (flock64_t), KM_SLEEP);
3119 		*destp->lr_flk = *lost_rqstp->lr_flk;
3120 	}
3121 	destp->lr_putfirst = lost_rqstp->lr_putfirst;
3122 }
3123 
3124 /*
3125  * Map the given return values (errno and nfs4 status code) to a recovery
3126  * action and fill in the following fields of recovp: rc_action,
3127  * rc_srv_reboot, rc_stateid, rc_lost_rqst.
3128  */
3129 
3130 void
3131 errs_to_action(recov_info_t *recovp,
3132 	nfs4_server_t *sp, mntinfo4_t *mi, stateid4 *sidp,
3133 	nfs4_lost_rqst_t *lost_rqstp, int unmounted, nfs_opnum4 op,
3134 	nfs4_bseqid_entry_t *bsep)
3135 {
3136 	nfs4_recov_t action = NR_UNUSED;
3137 	bool_t reboot = FALSE;
3138 	int try_f;
3139 	int error = recovp->rc_orig_errors.error;
3140 	nfsstat4 stat = recovp->rc_orig_errors.stat;
3141 
3142 	bzero(&recovp->rc_stateid, sizeof (stateid4));
3143 	recovp->rc_lost_rqst = NULL;
3144 	recovp->rc_bseqid_rqst = NULL;
3145 
3146 	try_f = nfs4_try_failover(&recovp->rc_orig_errors) &&
3147 			FAILOVER_MOUNT4(mi);
3148 
3149 	/*
3150 	 * We start recovery for EINTR only in the lost lock
3151 	 * or lost open/close case.
3152 	 */
3153 
3154 	if (try_f || error == EINTR || (error == EIO && unmounted)) {
3155 		recovp->rc_error = (error != 0 ? error : geterrno4(stat));
3156 		if (lost_rqstp) {
3157 			ASSERT(lost_rqstp->lr_op != 0);
3158 			nfs4_save_lost_rqst(lost_rqstp, recovp, &action, mi);
3159 		}
3160 		if (try_f)
3161 			action = NR_FAILOVER;
3162 	} else if (error != 0) {
3163 		recovp->rc_error = error;
3164 		nfs4_queue_event(RE_UNEXPECTED_ERRNO, mi, NULL, error, NULL,
3165 		    NULL, 0, NULL, 0, TAG_NONE, TAG_NONE, 0, 0);
3166 		action = NR_CLIENTID;
3167 	} else {
3168 		recovp->rc_error = geterrno4(stat);
3169 		switch (stat) {
3170 #ifdef notyet
3171 		case NFS4ERR_LEASE_MOVED:
3172 			action = xxx;
3173 			break;
3174 		case NFS4ERR_MOVED:
3175 			action = xxx;
3176 			break;
3177 #endif
3178 		case NFS4ERR_BADHANDLE:
3179 			action = NR_BADHANDLE;
3180 			break;
3181 		case NFS4ERR_BAD_SEQID:
3182 			if (bsep)
3183 				save_bseqid_rqst(bsep, recovp);
3184 			action = NR_BAD_SEQID;
3185 			break;
3186 		case NFS4ERR_OLD_STATEID:
3187 			action = NR_OLDSTATEID;
3188 			break;
3189 		case NFS4ERR_WRONGSEC:
3190 			action = NR_WRONGSEC;
3191 			break;
3192 		case NFS4ERR_FHEXPIRED:
3193 			action = NR_FHEXPIRED;
3194 			break;
3195 		case NFS4ERR_BAD_STATEID:
3196 			if (sp == NULL || (sp != NULL && inlease(sp))) {
3197 
3198 				action = NR_BAD_STATEID;
3199 				if (sidp)
3200 					recovp->rc_stateid = *sidp;
3201 			} else
3202 				action = NR_CLIENTID;
3203 			break;
3204 		case NFS4ERR_EXPIRED:
3205 			/*
3206 			 * The client's lease has expired, either due
3207 			 * to a network partition or perhaps a client
3208 			 * error.  In either case, try an NR_CLIENTID
3209 			 * style recovery.  reboot remains false, since
3210 			 * there is no evidence the server has rebooted.
3211 			 * This will cause CLAIM_NULL opens and lock
3212 			 * requests without the reclaim bit.
3213 			 */
3214 			action = NR_CLIENTID;
3215 
3216 			DTRACE_PROBE4(nfs4__expired,
3217 					nfs4_server_t *, sp,
3218 					mntinfo4_t *, mi,
3219 					stateid4 *, sidp, int, op);
3220 
3221 			break;
3222 		case NFS4ERR_STALE_CLIENTID:
3223 		case NFS4ERR_STALE_STATEID:
3224 			action = NR_CLIENTID;
3225 			reboot = TRUE;
3226 			break;
3227 		case NFS4ERR_RESOURCE:
3228 			/*
3229 			 * If this had been a FAILOVER mount, then
3230 			 * we'd have tried failover.  Since it's not,
3231 			 * just delay a while and retry.
3232 			 */
3233 			action = NR_DELAY;
3234 			break;
3235 		case NFS4ERR_GRACE:
3236 			action = NR_GRACE;
3237 			break;
3238 		case NFS4ERR_DELAY:
3239 			action = NR_DELAY;
3240 			break;
3241 		case NFS4ERR_STALE:
3242 			action = NR_STALE;
3243 			break;
3244 		default:
3245 			nfs4_queue_event(RE_UNEXPECTED_STATUS, mi, NULL, 0,
3246 			    NULL, NULL, stat, NULL, 0, TAG_NONE, TAG_NONE,
3247 			    0, 0);
3248 			action = NR_CLIENTID;
3249 			break;
3250 		}
3251 	}
3252 
3253 	/* make sure action got set */
3254 	ASSERT(action != NR_UNUSED);
3255 	recovp->rc_srv_reboot = reboot;
3256 	recovp->rc_action = action;
3257 	nfs4_queue_fact(RF_ERR, mi, stat, action, op, reboot, NULL, error,
3258 		NULL);
3259 }
3260 
3261 /*
3262  * Return the (held) credential for the process with the given pid.
3263  * May return NULL (e.g., process not found).
3264  */
3265 
3266 static cred_t *
3267 pid_to_cr(pid_t pid)
3268 {
3269 	proc_t *p;
3270 	cred_t *cr;
3271 
3272 	mutex_enter(&pidlock);
3273 	if ((p = prfind(pid)) == NULL) {
3274 		mutex_exit(&pidlock);
3275 		return (NULL);
3276 	}
3277 
3278 	mutex_enter(&p->p_crlock);
3279 	crhold(cr = p->p_cred);
3280 	mutex_exit(&p->p_crlock);
3281 	mutex_exit(&pidlock);
3282 
3283 	return (cr);
3284 }
3285 
3286 /*
3287  * Send SIGLOST to the given process and queue the event.
3288  *
3289  * The 'dump' boolean tells us whether this action should dump the
3290  * in-kernel queue of recovery messages or not.
3291  */
3292 
3293 void
3294 nfs4_send_siglost(pid_t pid, mntinfo4_t *mi, vnode_t *vp, bool_t dump,
3295     int error, nfsstat4 stat)
3296 {
3297 	proc_t *p;
3298 
3299 	mutex_enter(&pidlock);
3300 	p = prfind(pid);
3301 	if (p)
3302 		psignal(p, SIGLOST);
3303 	mutex_exit(&pidlock);
3304 	nfs4_queue_event(dump ? RE_SIGLOST : RE_SIGLOST_NO_DUMP, mi,
3305 	    NULL, error, vp, NULL, stat, NULL, pid, TAG_NONE, TAG_NONE, 0, 0);
3306 }
3307 
3308 /*
3309  * Scan the lock list for entries that match the given pid.  Change the
3310  * pid in those that do to NOPID.
3311  */
3312 
3313 static void
3314 relock_skip_pid(locklist_t *llp, pid_t pid)
3315 {
3316 	for (; llp != NULL; llp = llp->ll_next) {
3317 		if (llp->ll_flock.l_pid == pid)
3318 			llp->ll_flock.l_pid = NOPID;
3319 	}
3320 }
3321 
3322 /*
3323  * Mark a file as having failed recovery, after making a last-ditch effort
3324  * to return any delegation.
3325  *
3326  * Sets r_error to EIO or ESTALE for the given vnode.
3327  */
3328 void
3329 nfs4_fail_recov(vnode_t *vp, char *why, int error, nfsstat4 stat)
3330 {
3331 	rnode4_t *rp = VTOR4(vp);
3332 
3333 #ifdef DEBUG
3334 	if (nfs4_fail_recov_stop)
3335 		debug_enter("nfs4_fail_recov");
3336 #endif
3337 
3338 	mutex_enter(&rp->r_statelock);
3339 	if (rp->r_flags & (R4RECOVERR|R4RECOVERRP)) {
3340 		mutex_exit(&rp->r_statelock);
3341 		return;
3342 	}
3343 
3344 	/*
3345 	 * Set R4RECOVERRP to indicate that a recovery error is in
3346 	 * progress.  This will shut down reads and writes at the top
3347 	 * half.  Don't set R4RECOVERR until after we've returned the
3348 	 * delegation, otherwise it will fail.
3349 	 */
3350 
3351 	rp->r_flags |= R4RECOVERRP;
3352 	mutex_exit(&rp->r_statelock);
3353 
3354 	nfs4delegabandon(rp);
3355 
3356 	mutex_enter(&rp->r_statelock);
3357 	rp->r_flags |= (R4RECOVERR | R4STALE);
3358 	rp->r_error = (error == 0 && stat == NFS4ERR_STALE) ? ESTALE : EIO;
3359 	PURGE_ATTRCACHE4_LOCKED(rp);
3360 	if (!(vp->v_vfsp->vfs_flag & VFS_UNMOUNTED))
3361 		nfs4_queue_event(RE_DEAD_FILE, VTOMI4(vp), NULL, error,
3362 		    vp, NULL, stat, why, 0, TAG_NONE, TAG_NONE, 0, 0);
3363 	mutex_exit(&rp->r_statelock);
3364 
3365 	dnlc_purge_vp(vp);
3366 }
3367 
3368 /*
3369  * recov_throttle: if the file had the same recovery action within the
3370  * throttle interval, wait for the throttle interval to finish before
3371  * proceeding.
3372  *
3373  * Side effects: updates the rnode with the current recovery information.
3374  */
3375 
3376 static void
3377 recov_throttle(recov_info_t *recovp, vnode_t *vp)
3378 {
3379 	time_t curtime, time_to_wait;
3380 	rnode4_t *rp = VTOR4(vp);
3381 
3382 	curtime = gethrestime_sec();
3383 
3384 	mutex_enter(&rp->r_statelock);
3385 	NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3386 		"recov_throttle: now: (%d, %ld), last: (%d, %ld)",
3387 		recovp->rc_action, curtime,
3388 		rp->r_recov_act, rp->r_last_recov));
3389 	if (recovp->rc_action == rp->r_recov_act &&
3390 	    rp->r_last_recov + recov_err_delay > curtime) {
3391 		time_to_wait = rp->r_last_recov + recov_err_delay - curtime;
3392 		mutex_exit(&rp->r_statelock);
3393 		delay(SEC_TO_TICK(time_to_wait));
3394 		curtime = gethrestime_sec();
3395 		mutex_enter(&rp->r_statelock);
3396 	}
3397 
3398 	rp->r_last_recov = curtime;
3399 	rp->r_recov_act = recovp->rc_action;
3400 	mutex_exit(&rp->r_statelock);
3401 }
3402 
3403 /*
3404  * React to NFS4ERR_GRACE by setting the time we'll permit
3405  * the next call to this filesystem.
3406  */
3407 void
3408 nfs4_set_grace_wait(mntinfo4_t *mi)
3409 {
3410 	mutex_enter(&mi->mi_lock);
3411 	/* Mark the time for the future */
3412 	mi->mi_grace_wait = gethrestime_sec() + nfs4err_delay_time;
3413 	mutex_exit(&mi->mi_lock);
3414 }
3415 
3416 /*
3417  * React to MFS4ERR_DELAY by setting the time we'll permit
3418  * the next call to this vnode.
3419  */
3420 void
3421 nfs4_set_delay_wait(vnode_t *vp)
3422 {
3423 	rnode4_t *rp = VTOR4(vp);
3424 
3425 	mutex_enter(&rp->r_statelock);
3426 	/*
3427 	 * Calculate amount we should delay, initial
3428 	 * delay will be short and then we will back off.
3429 	 */
3430 	if (rp->r_delay_interval == 0)
3431 		rp->r_delay_interval = NFS4_INITIAL_DELAY_INTERVAL;
3432 	else
3433 		/* calculate next interval value */
3434 		rp->r_delay_interval =
3435 		    MIN(NFS4_MAX_DELAY_INTERVAL, (rp->r_delay_interval << 1));
3436 	rp->r_delay_wait = gethrestime_sec() + rp->r_delay_interval;
3437 	mutex_exit(&rp->r_statelock);
3438 }
3439 
3440 /*
3441  * The caller is responsible for freeing the returned string.
3442  */
3443 static char *
3444 nfs4_getsrvnames(mntinfo4_t *mi, size_t *len)
3445 {
3446 	servinfo4_t *svp;
3447 	char *srvnames;
3448 	char *namep;
3449 	size_t length;
3450 
3451 	/*
3452 	 * Calculate the length of the string required to hold all
3453 	 * of the server names plus either a comma or a null
3454 	 * character following each individual one.
3455 	 */
3456 	length = 0;
3457 	for (svp = mi->mi_servers; svp != NULL; svp = svp->sv_next) {
3458 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3459 		if (svp->sv_flags & SV4_NOTINUSE) {
3460 			nfs_rw_exit(&svp->sv_lock);
3461 			continue;
3462 		}
3463 		nfs_rw_exit(&svp->sv_lock);
3464 		length += svp->sv_hostnamelen;
3465 	}
3466 
3467 	srvnames = kmem_alloc(length, KM_SLEEP);
3468 
3469 	namep = srvnames;
3470 	for (svp = mi->mi_servers; svp != NULL; svp = svp->sv_next) {
3471 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3472 		if (svp->sv_flags & SV4_NOTINUSE) {
3473 			nfs_rw_exit(&svp->sv_lock);
3474 			continue;
3475 		}
3476 		nfs_rw_exit(&svp->sv_lock);
3477 		(void) strcpy(namep, svp->sv_hostname);
3478 		namep += svp->sv_hostnamelen - 1;
3479 		*namep++ = ',';
3480 	}
3481 	*--namep = '\0';
3482 
3483 	*len = length;
3484 
3485 	return (srvnames);
3486 }
3487 
3488 static void
3489 save_bseqid_rqst(nfs4_bseqid_entry_t *bsep, recov_info_t *recovp)
3490 {
3491 	nfs4_bseqid_entry_t *destp;
3492 
3493 	destp = kmem_alloc(sizeof (nfs4_bseqid_entry_t), KM_SLEEP);
3494 	recovp->rc_bseqid_rqst = destp;
3495 
3496 	if (bsep->bs_oop)
3497 		open_owner_hold(bsep->bs_oop);
3498 	destp->bs_oop = bsep->bs_oop;
3499 	if (bsep->bs_lop)
3500 		lock_owner_hold(bsep->bs_lop);
3501 	destp->bs_lop = bsep->bs_lop;
3502 	if (bsep->bs_vp)
3503 		VN_HOLD(bsep->bs_vp);
3504 	destp->bs_vp = bsep->bs_vp;
3505 	destp->bs_pid = bsep->bs_pid;
3506 	destp->bs_tag = bsep->bs_tag;
3507 	destp->bs_seqid = bsep->bs_seqid;
3508 }
3509 
3510 static void
3511 free_bseqid_rqst(nfs4_bseqid_entry_t *bsep)
3512 {
3513 	if (bsep->bs_oop)
3514 		open_owner_rele(bsep->bs_oop);
3515 	if (bsep->bs_lop)
3516 		lock_owner_rele(bsep->bs_lop);
3517 	if (bsep->bs_vp)
3518 		VN_RELE(bsep->bs_vp);
3519 	kmem_free(bsep, sizeof (nfs4_bseqid_entry_t));
3520 }
3521 
3522 /*
3523  * We don't actually fully recover from NFS4ERR_BAD_SEQID.  We
3524  * simply mark the open owner and open stream (if provided) as "bad".
3525  * Then future uses of these data structures will be limited to basically
3526  * just cleaning up the internal client state (no going OTW).
3527  *
3528  * The result of this is to return errors back to the app/usr when
3529  * we receive NFS4ERR_BAD_SEQID, but also allow future/new calls to
3530  * succeed so progress can be made.
3531  */
3532 void
3533 recov_bad_seqid(recov_info_t *recovp)
3534 {
3535 	mntinfo4_t		*mi = recovp->rc_mi;
3536 	nfs4_open_owner_t	*bad_oop;
3537 	nfs4_lock_owner_t	*bad_lop;
3538 	vnode_t			*vp;
3539 	rnode4_t		*rp = NULL;
3540 	pid_t			pid;
3541 	nfs4_bseqid_entry_t	*bsep, *tbsep;
3542 	int			error;
3543 
3544 	ASSERT(mi != NULL);
3545 	ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
3546 
3547 	mutex_enter(&mi->mi_lock);
3548 	bsep = list_head(&mi->mi_bseqid_list);
3549 	mutex_exit(&mi->mi_lock);
3550 
3551 	/*
3552 	 * Handle all the bad seqid entries on mi's list.
3553 	 */
3554 	while (bsep != NULL) {
3555 		bad_oop = bsep->bs_oop;
3556 		bad_lop = bsep->bs_lop;
3557 		vp = bsep->bs_vp;
3558 		pid = bsep->bs_pid;
3559 
3560 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
3561 		    "recov_bad_seqid: mark oop %p lop %p as bad for "
3562 		    "vp %p tag %s pid %d: last good seqid %d for tag %s",
3563 		    (void *)bad_oop, (void *)bad_lop, (void *)vp,
3564 		    nfs4_ctags[bsep->bs_tag].ct_str, pid,
3565 		    bad_oop ?  bad_oop->oo_last_good_seqid : 0,
3566 		    bad_oop ? nfs4_ctags[bad_oop->oo_last_good_op].ct_str :
3567 		    nfs4_ctags[TAG_NONE].ct_str));
3568 
3569 		nfs4_queue_event(RE_BAD_SEQID, mi, NULL,
3570 		    0, vp, NULL, NFS4ERR_BAD_SEQID, NULL, pid, bsep->bs_tag,
3571 		    bad_oop ? bad_oop->oo_last_good_op : TAG_NONE,
3572 		    bsep->bs_seqid, bad_oop ? bad_oop->oo_last_good_seqid : 0);
3573 
3574 		if (bad_oop) {
3575 			/* essentially reset the open owner */
3576 			error = nfs4_start_open_seqid_sync(bad_oop, mi);
3577 			ASSERT(!error);	/* recov thread always succeeds */
3578 			bad_oop->oo_name = nfs4_get_new_oo_name();
3579 			bad_oop->oo_seqid = 0;
3580 			nfs4_end_open_seqid_sync(bad_oop);
3581 		}
3582 
3583 		if (bad_lop) {
3584 			mutex_enter(&bad_lop->lo_lock);
3585 			bad_lop->lo_flags |= NFS4_BAD_SEQID_LOCK;
3586 			mutex_exit(&bad_lop->lo_lock);
3587 
3588 			ASSERT(vp != NULL);
3589 			rp = VTOR4(vp);
3590 			mutex_enter(&rp->r_statelock);
3591 			rp->r_flags |= R4LODANGLERS;
3592 			mutex_exit(&rp->r_statelock);
3593 
3594 			nfs4_send_siglost(pid, mi, vp, TRUE,
3595 			    0, NFS4ERR_BAD_SEQID);
3596 		}
3597 
3598 		mutex_enter(&mi->mi_lock);
3599 		list_remove(&mi->mi_bseqid_list, bsep);
3600 		tbsep = bsep;
3601 		bsep = list_head(&mi->mi_bseqid_list);
3602 		mutex_exit(&mi->mi_lock);
3603 		free_bseqid_rqst(tbsep);
3604 	}
3605 
3606 	mutex_enter(&mi->mi_lock);
3607 	mi->mi_recovflags &= ~MI4R_BAD_SEQID;
3608 	mutex_exit(&mi->mi_lock);
3609 }
3610