xref: /titanic_50/usr/src/uts/common/fs/nfs/nfs4_vfsops.c (revision 4f85d229295a756a4e6f1759b47df7b97412db7d)
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 /*
28  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
29  *	All Rights Reserved
30  */
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/cred.h>
38 #include <sys/vfs.h>
39 #include <sys/vnode.h>
40 #include <sys/pathname.h>
41 #include <sys/sysmacros.h>
42 #include <sys/kmem.h>
43 #include <sys/mkdev.h>
44 #include <sys/mount.h>
45 #include <sys/statvfs.h>
46 #include <sys/errno.h>
47 #include <sys/debug.h>
48 #include <sys/cmn_err.h>
49 #include <sys/utsname.h>
50 #include <sys/bootconf.h>
51 #include <sys/modctl.h>
52 #include <sys/acl.h>
53 #include <sys/flock.h>
54 #include <sys/time.h>
55 #include <sys/disp.h>
56 #include <sys/policy.h>
57 #include <sys/socket.h>
58 #include <sys/netconfig.h>
59 #include <sys/dnlc.h>
60 #include <sys/list.h>
61 
62 #include <rpc/types.h>
63 #include <rpc/auth.h>
64 #include <rpc/rpcsec_gss.h>
65 #include <rpc/clnt.h>
66 
67 #include <nfs/nfs.h>
68 #include <nfs/nfs_clnt.h>
69 #include <nfs/mount.h>
70 #include <nfs/nfs_acl.h>
71 
72 #include <fs/fs_subr.h>
73 
74 #include <nfs/nfs4.h>
75 #include <nfs/rnode4.h>
76 #include <nfs/nfs4_clnt.h>
77 
78 /*
79  * Arguments passed to thread to free data structures from forced unmount.
80  */
81 
82 typedef struct {
83 	vfs_t *fm_vfsp;
84 	cred_t *fm_cr;
85 } freemountargs_t;
86 
87 static void	async_free_mount(vfs_t *, cred_t *);
88 static void	nfs4_free_mount(vfs_t *, cred_t *);
89 static void	nfs4_free_mount_thread(freemountargs_t *);
90 static int nfs4_chkdup_servinfo4(servinfo4_t *, servinfo4_t *);
91 
92 /*
93  * From rpcsec module (common/rpcsec).
94  */
95 extern int sec_clnt_loadinfo(struct sec_data *, struct sec_data **, model_t);
96 extern void sec_clnt_freeinfo(struct sec_data *);
97 
98 /*
99  * The order and contents of this structure must be kept in sync with that of
100  * rfsreqcnt_v4_tmpl in nfs_stats.c
101  */
102 static char *rfsnames_v4[] = {
103 	"null", "compound", "reserved",	"access", "close", "commit", "create",
104 	"delegpurge", "delegreturn", "getattr",	"getfh", "link", "lock",
105 	"lockt", "locku", "lookup", "lookupp", "nverify", "open", "openattr",
106 	"open_confirm",	"open_downgrade", "putfh", "putpubfh", "putrootfh",
107 	"read", "readdir", "readlink", "remove", "rename", "renew",
108 	"restorefh", "savefh", "secinfo", "setattr", "setclientid",
109 	"setclientid_confirm", "verify", "write"
110 };
111 
112 /*
113  * nfs4_max_mount_retry is the number of times the client will redrive
114  * a mount compound before giving up and returning failure.  The intent
115  * is to redrive mount compounds which fail NFS4ERR_STALE so that
116  * if a component of the server path being mounted goes stale, it can
117  * "recover" by redriving the mount compund (LOOKUP ops).  This recovery
118  * code is needed outside of the recovery framework because mount is a
119  * special case.  The client doesn't create vnodes/rnodes for components
120  * of the server path being mounted.  The recovery code recovers real
121  * client objects, not STALE FHs which map to components of the server
122  * path being mounted.
123  *
124  * We could just fail the mount on the first time, but that would
125  * instantly trigger failover (from nfs4_mount), and the client should
126  * try to re-lookup the STALE FH before doing failover.  The easiest
127  * way to "re-lookup" is to simply redrive the mount compound.
128  */
129 static int nfs4_max_mount_retry = 2;
130 
131 /*
132  * nfs4 vfs operations.
133  */
134 static int	nfs4_mount(vfs_t *, vnode_t *, struct mounta *, cred_t *);
135 static int	nfs4_unmount(vfs_t *, int, cred_t *);
136 static int	nfs4_root(vfs_t *, vnode_t **);
137 static int	nfs4_statvfs(vfs_t *, struct statvfs64 *);
138 static int	nfs4_sync(vfs_t *, short, cred_t *);
139 static int	nfs4_vget(vfs_t *, vnode_t **, fid_t *);
140 static int	nfs4_mountroot(vfs_t *, whymountroot_t);
141 static void	nfs4_freevfs(vfs_t *);
142 
143 static int	nfs4rootvp(vnode_t **, vfs_t *, struct servinfo4 *,
144 		    int, cred_t *, zone_t *);
145 
146 vfsops_t	*nfs4_vfsops;
147 
148 int nfs4_vfsinit(void);
149 void nfs4_vfsfini(void);
150 static void nfs4setclientid_init(void);
151 static void nfs4setclientid_fini(void);
152 static void nfs4setclientid_otw(mntinfo4_t *, servinfo4_t *,  cred_t *,
153 		struct nfs4_server *, nfs4_error_t *, int *);
154 static void	destroy_nfs4_server(nfs4_server_t *);
155 static void	remove_mi(nfs4_server_t *, mntinfo4_t *);
156 
157 /*
158  * Initialize the vfs structure
159  */
160 
161 static int nfs4fstyp;
162 
163 
164 /*
165  * Debug variable to check for rdma based
166  * transport startup and cleanup. Controlled
167  * through /etc/system. Off by default.
168  */
169 extern int rdma_debug;
170 
171 int
172 nfs4init(int fstyp, char *name)
173 {
174 	static const fs_operation_def_t nfs4_vfsops_template[] = {
175 		VFSNAME_MOUNT, nfs4_mount,
176 		VFSNAME_UNMOUNT, nfs4_unmount,
177 		VFSNAME_ROOT, nfs4_root,
178 		VFSNAME_STATVFS, nfs4_statvfs,
179 		VFSNAME_SYNC, (fs_generic_func_p) nfs4_sync,
180 		VFSNAME_VGET, nfs4_vget,
181 		VFSNAME_MOUNTROOT, nfs4_mountroot,
182 		VFSNAME_FREEVFS, (fs_generic_func_p)nfs4_freevfs,
183 		NULL, NULL
184 	};
185 	int error;
186 
187 	error = vfs_setfsops(fstyp, nfs4_vfsops_template, &nfs4_vfsops);
188 	if (error != 0) {
189 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
190 		    "nfs4init: bad vfs ops template");
191 		return (error);
192 	}
193 
194 	error = vn_make_ops(name, nfs4_vnodeops_template, &nfs4_vnodeops);
195 	if (error != 0) {
196 		(void) vfs_freevfsops_by_type(fstyp);
197 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
198 		    "nfs4init: bad vnode ops template");
199 		return (error);
200 	}
201 
202 	nfs4fstyp = fstyp;
203 
204 	(void) nfs4_vfsinit();
205 
206 	(void) nfs4_init_dot_entries();
207 
208 	return (0);
209 }
210 
211 void
212 nfs4fini(void)
213 {
214 	(void) nfs4_destroy_dot_entries();
215 	nfs4_vfsfini();
216 }
217 
218 /*
219  * Create a new sec_data structure to store AUTH_DH related data:
220  * netname, syncaddr, knetconfig. There is no AUTH_F_RPCTIMESYNC
221  * flag set for NFS V4 since we are avoiding to contact the rpcbind
222  * daemon and is using the IP time service (IPPORT_TIMESERVER).
223  *
224  * sec_data can be freed by sec_clnt_freeinfo().
225  */
226 struct sec_data *
227 create_authdh_data(char *netname, int nlen, struct netbuf *syncaddr,
228 		struct knetconfig *knconf) {
229 	struct sec_data *secdata;
230 	dh_k4_clntdata_t *data;
231 	char *pf, *p;
232 
233 	if (syncaddr == NULL || syncaddr->buf == NULL || nlen == 0)
234 		return (NULL);
235 
236 	secdata = kmem_alloc(sizeof (*secdata), KM_SLEEP);
237 	secdata->flags = 0;
238 
239 	data = kmem_alloc(sizeof (*data), KM_SLEEP);
240 
241 	data->syncaddr.maxlen = syncaddr->maxlen;
242 	data->syncaddr.len = syncaddr->len;
243 	data->syncaddr.buf = (char *)kmem_alloc(syncaddr->len, KM_SLEEP);
244 	bcopy(syncaddr->buf, data->syncaddr.buf, syncaddr->len);
245 
246 	/*
247 	 * duplicate the knconf information for the
248 	 * new opaque data.
249 	 */
250 	data->knconf = kmem_alloc(sizeof (*knconf), KM_SLEEP);
251 	*data->knconf = *knconf;
252 	pf = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
253 	p = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
254 	bcopy(knconf->knc_protofmly, pf, KNC_STRSIZE);
255 	bcopy(knconf->knc_proto, p, KNC_STRSIZE);
256 	data->knconf->knc_protofmly = pf;
257 	data->knconf->knc_proto = p;
258 
259 	/* move server netname to the sec_data structure */
260 	data->netname = kmem_alloc(nlen, KM_SLEEP);
261 	bcopy(netname, data->netname, nlen);
262 	data->netnamelen = (int)nlen;
263 
264 	secdata->secmod = AUTH_DH;
265 	secdata->rpcflavor = AUTH_DH;
266 	secdata->data = (caddr_t)data;
267 
268 	return (secdata);
269 }
270 
271 static int
272 nfs4_chkdup_servinfo4(servinfo4_t *svp_head, servinfo4_t *svp)
273 {
274 	servinfo4_t *si;
275 
276 	/*
277 	 * Iterate over the servinfo4 list to make sure
278 	 * we do not have a duplicate. Skip any servinfo4
279 	 * that has been marked "NOT IN USE"
280 	 */
281 	for (si = svp_head; si; si = si->sv_next) {
282 		(void) nfs_rw_enter_sig(&si->sv_lock, RW_READER, 0);
283 		if (si->sv_flags & SV4_NOTINUSE) {
284 			nfs_rw_exit(&si->sv_lock);
285 			continue;
286 		}
287 		nfs_rw_exit(&si->sv_lock);
288 		if (si == svp)
289 			continue;
290 		if (si->sv_addr.len == svp->sv_addr.len &&
291 		    strcmp(si->sv_knconf->knc_protofmly,
292 			svp->sv_knconf->knc_protofmly) == 0 &&
293 		    bcmp(si->sv_addr.buf, svp->sv_addr.buf,
294 			si->sv_addr.len) == 0) {
295 			/* it's a duplicate */
296 			return (1);
297 		}
298 	}
299 	/* it's not a duplicate */
300 	return (0);
301 }
302 
303 /*
304  * nfs mount vfsop
305  * Set up mount info record and attach it to vfs struct.
306  */
307 static int
308 nfs4_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
309 {
310 	char *data = uap->dataptr;
311 	int error;
312 	vnode_t *rtvp;			/* the server's root */
313 	mntinfo4_t *mi;			/* mount info, pointed at by vfs */
314 	size_t hlen;			/* length of hostname */
315 	size_t nlen;			/* length of netname */
316 	char netname[MAXNETNAMELEN+1];	/* server's netname */
317 	struct netbuf addr;		/* server's address */
318 	struct netbuf syncaddr;		/* AUTH_DES time sync addr */
319 	struct knetconfig *knconf;	/* transport knetconfig structure */
320 	struct knetconfig *rdma_knconf;	/* rdma transport structure */
321 	rnode4_t *rp;
322 	struct servinfo4 *svp;		/* nfs server info */
323 	struct servinfo4 *svp_tail = NULL; /* previous nfs server info */
324 	struct servinfo4 *svp_head;	/* first nfs server info */
325 	struct servinfo4 *svp_2ndlast;	/* 2nd last in server info list */
326 	struct sec_data *secdata;	/* security data */
327 	STRUCT_DECL(nfs_args, args);	/* nfs mount arguments */
328 	STRUCT_DECL(knetconfig, knconf_tmp);
329 	STRUCT_DECL(netbuf, addr_tmp);
330 	int flags, addr_type;
331 	char *p, *pf;
332 	struct pathname pn;
333 	char *userbufptr;
334 	zone_t *zone = nfs_zone();
335 	nfs4_error_t n4e;
336 
337 	if (secpolicy_fs_mount(cr, mvp, vfsp) != 0)
338 		return (EPERM);
339 	if (mvp->v_type != VDIR)
340 		return (ENOTDIR);
341 	/*
342 	 * get arguments
343 	 *
344 	 * nfs_args is now versioned and is extensible, so
345 	 * uap->datalen might be different from sizeof (args)
346 	 * in a compatible situation.
347 	 */
348 more:
349 	STRUCT_INIT(args, get_udatamodel());
350 	bzero(STRUCT_BUF(args), SIZEOF_STRUCT(nfs_args, DATAMODEL_NATIVE));
351 	if (copyin(data, STRUCT_BUF(args), MIN(uap->datalen,
352 	    STRUCT_SIZE(args))))
353 		return (EFAULT);
354 
355 	flags = STRUCT_FGET(args, flags);
356 
357 	/*
358 	 * If the request changes the locking type, disallow the remount,
359 	 * because it's questionable whether we can transfer the
360 	 * locking state correctly.
361 	 */
362 	if (uap->flags & MS_REMOUNT) {
363 		if ((mi = VFTOMI4(vfsp)) != NULL) {
364 			uint_t new_mi_llock;
365 			uint_t old_mi_llock;
366 
367 			new_mi_llock = (flags & NFSMNT_LLOCK) ? 1 : 0;
368 			old_mi_llock = (mi->mi_flags & MI4_LLOCK) ? 1 : 0;
369 			if (old_mi_llock != new_mi_llock)
370 				return (EBUSY);
371 		}
372 		return (0);
373 	}
374 
375 	mutex_enter(&mvp->v_lock);
376 	if (!(uap->flags & MS_OVERLAY) &&
377 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
378 		mutex_exit(&mvp->v_lock);
379 		return (EBUSY);
380 	}
381 	mutex_exit(&mvp->v_lock);
382 
383 	/* make sure things are zeroed for errout: */
384 	rtvp = NULL;
385 	mi = NULL;
386 	addr.buf = NULL;
387 	syncaddr.buf = NULL;
388 	secdata = NULL;
389 
390 	/*
391 	 * A valid knetconfig structure is required.
392 	 */
393 	if (!(flags & NFSMNT_KNCONF))
394 		return (EINVAL);
395 
396 	/*
397 	 * Allocate a servinfo4 struct.
398 	 */
399 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
400 	nfs_rw_init(&svp->sv_lock, NULL, RW_DEFAULT, NULL);
401 	if (svp_tail) {
402 		svp_2ndlast = svp_tail;
403 		svp_tail->sv_next = svp;
404 	} else {
405 		svp_head = svp;
406 		svp_2ndlast = svp;
407 	}
408 
409 	svp_tail = svp;
410 
411 	/*
412 	 * Allocate space for a knetconfig structure and
413 	 * its strings and copy in from user-land.
414 	 */
415 	knconf = kmem_zalloc(sizeof (*knconf), KM_SLEEP);
416 	svp->sv_knconf = knconf;
417 	STRUCT_INIT(knconf_tmp, get_udatamodel());
418 	if (copyin(STRUCT_FGETP(args, knconf), STRUCT_BUF(knconf_tmp),
419 	    STRUCT_SIZE(knconf_tmp))) {
420 		sv4_free(svp_head);
421 		return (EFAULT);
422 	}
423 
424 	knconf->knc_semantics = STRUCT_FGET(knconf_tmp, knc_semantics);
425 	knconf->knc_protofmly = STRUCT_FGETP(knconf_tmp, knc_protofmly);
426 	knconf->knc_proto = STRUCT_FGETP(knconf_tmp, knc_proto);
427 	if (get_udatamodel() != DATAMODEL_LP64) {
428 		knconf->knc_rdev = expldev(STRUCT_FGET(knconf_tmp, knc_rdev));
429 	} else {
430 		knconf->knc_rdev = STRUCT_FGET(knconf_tmp, knc_rdev);
431 	}
432 
433 	pf = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
434 	p = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
435 	error = copyinstr(knconf->knc_protofmly, pf, KNC_STRSIZE, NULL);
436 	if (error) {
437 		kmem_free(pf, KNC_STRSIZE);
438 		kmem_free(p, KNC_STRSIZE);
439 		sv4_free(svp_head);
440 		return (error);
441 	}
442 	error = copyinstr(knconf->knc_proto, p, KNC_STRSIZE, NULL);
443 	if (error) {
444 		kmem_free(pf, KNC_STRSIZE);
445 		kmem_free(p, KNC_STRSIZE);
446 		sv4_free(svp_head);
447 		return (error);
448 	}
449 	if (strcmp(p, NC_UDP) == 0) {
450 		kmem_free(pf, KNC_STRSIZE);
451 		kmem_free(p, KNC_STRSIZE);
452 		sv4_free(svp_head);
453 		return (ENOTSUP);
454 	}
455 	knconf->knc_protofmly = pf;
456 	knconf->knc_proto = p;
457 
458 	/*
459 	 * Get server address
460 	 */
461 	STRUCT_INIT(addr_tmp, get_udatamodel());
462 	if (copyin(STRUCT_FGETP(args, addr), STRUCT_BUF(addr_tmp),
463 	    STRUCT_SIZE(addr_tmp))) {
464 		error = EFAULT;
465 		goto errout;
466 	}
467 
468 	userbufptr = addr.buf = STRUCT_FGETP(addr_tmp, buf);
469 	addr.len = STRUCT_FGET(addr_tmp, len);
470 	addr.buf = kmem_alloc(addr.len, KM_SLEEP);
471 	addr.maxlen = addr.len;
472 	if (copyin(userbufptr, addr.buf, addr.len)) {
473 		kmem_free(addr.buf, addr.len);
474 		error = EFAULT;
475 		goto errout;
476 	}
477 
478 	svp->sv_addr = addr;
479 
480 	/*
481 	 * Get the root fhandle
482 	 */
483 	error = pn_get(STRUCT_FGETP(args, fh), UIO_USERSPACE, &pn);
484 
485 	if (error)
486 		goto errout;
487 
488 	/* Volatile fh: keep server paths, so use actual-size strings */
489 	svp->sv_path = kmem_alloc(pn.pn_pathlen + 1, KM_SLEEP);
490 	bcopy(pn.pn_path, svp->sv_path, pn.pn_pathlen);
491 	svp->sv_path[pn.pn_pathlen] = '\0';
492 	svp->sv_pathlen = pn.pn_pathlen + 1;
493 	pn_free(&pn);
494 
495 	/*
496 	 * Get server's hostname
497 	 */
498 	if (flags & NFSMNT_HOSTNAME) {
499 		error = copyinstr(STRUCT_FGETP(args, hostname),
500 				netname, sizeof (netname), &hlen);
501 		if (error)
502 			goto errout;
503 	} else {
504 		char *p = "unknown-host";
505 		hlen = strlen(p) + 1;
506 		(void) strcpy(netname, p);
507 	}
508 	svp->sv_hostnamelen = hlen;
509 	svp->sv_hostname = kmem_alloc(svp->sv_hostnamelen, KM_SLEEP);
510 	(void) strcpy(svp->sv_hostname, netname);
511 
512 	/*
513 	 * RDMA MOUNT SUPPORT FOR NFS v4.
514 	 * Establish, is it possible to use RDMA, if so overload the
515 	 * knconf with rdma specific knconf and free the orignal knconf.
516 	 */
517 	if ((flags & NFSMNT_TRYRDMA) || (flags & NFSMNT_DORDMA)) {
518 		/*
519 		 * Determine the addr type for RDMA, IPv4 or v6.
520 		 */
521 		if (strcmp(svp->sv_knconf->knc_protofmly, NC_INET) == 0)
522 			addr_type = AF_INET;
523 		else if (strcmp(svp->sv_knconf->knc_protofmly, NC_INET6) == 0)
524 			addr_type = AF_INET6;
525 
526 		if (rdma_reachable(addr_type, &svp->sv_addr,
527 			&rdma_knconf) == 0) {
528 			/*
529 			 * If successful, hijack the orignal knconf and
530 			 * replace with the new one, depending on the flags.
531 			 */
532 			svp->sv_origknconf = svp->sv_knconf;
533 			svp->sv_knconf = rdma_knconf;
534 			knconf = rdma_knconf;
535 		} else {
536 			if (flags & NFSMNT_TRYRDMA) {
537 #ifdef	DEBUG
538 				if (rdma_debug)
539 					zcmn_err(getzoneid(), CE_WARN,
540 					    "no RDMA onboard, revert\n");
541 #endif
542 			}
543 
544 			if (flags & NFSMNT_DORDMA) {
545 				/*
546 				 * If proto=rdma is specified and no RDMA
547 				 * path to this server is avialable then
548 				 * ditch this server.
549 				 * This is not included in the mountable
550 				 * server list or the replica list.
551 				 * Check if more servers are specified;
552 				 * Failover case, otherwise bail out of mount.
553 				 */
554 				if (STRUCT_FGET(args, nfs_args_ext) ==
555 				    NFS_ARGS_EXTB && STRUCT_FGETP(args,
556 					nfs_ext_u.nfs_extB.next) != NULL) {
557 					if (uap->flags & MS_RDONLY &&
558 					    !(flags & NFSMNT_SOFT)) {
559 						data = (char *)
560 						    STRUCT_FGETP(args,
561 						nfs_ext_u.nfs_extB.next);
562 						if (svp_head->sv_next == NULL) {
563 							svp_tail = NULL;
564 							svp_2ndlast = NULL;
565 							sv4_free(svp_head);
566 							goto more;
567 						} else {
568 							svp_tail = svp_2ndlast;
569 							svp_2ndlast->sv_next =
570 							    NULL;
571 							sv4_free(svp);
572 							goto more;
573 						}
574 					}
575 				} else {
576 					/*
577 					 * This is the last server specified
578 					 * in the nfs_args list passed down
579 					 * and its not rdma capable.
580 					 */
581 					if (svp_head->sv_next == NULL) {
582 						/*
583 						 * Is this the only one
584 						 */
585 						error = EINVAL;
586 #ifdef	DEBUG
587 						if (rdma_debug)
588 							zcmn_err(getzoneid(),
589 							    CE_WARN,
590 							    "No RDMA srv");
591 #endif
592 						goto errout;
593 					} else {
594 						/*
595 						 * There is list, since some
596 						 * servers specified before
597 						 * this passed all requirements
598 						 */
599 						svp_tail = svp_2ndlast;
600 						svp_2ndlast->sv_next = NULL;
601 						sv4_free(svp);
602 						goto proceed;
603 					}
604 				}
605 			}
606 		}
607 	}
608 
609 	/*
610 	 * If there are syncaddr and netname data, load them in. This is
611 	 * to support data needed for NFSV4 when AUTH_DH is the negotiated
612 	 * flavor via SECINFO. (instead of using MOUNT protocol in V3).
613 	 */
614 	netname[0] = '\0';
615 	if (flags & NFSMNT_SECURE) {
616 
617 		/* get syncaddr */
618 		STRUCT_INIT(addr_tmp, get_udatamodel());
619 		if (copyin(STRUCT_FGETP(args, syncaddr), STRUCT_BUF(addr_tmp),
620 			STRUCT_SIZE(addr_tmp))) {
621 			error = EINVAL;
622 			goto errout;
623 		}
624 		userbufptr = STRUCT_FGETP(addr_tmp, buf);
625 		syncaddr.len = STRUCT_FGET(addr_tmp, len);
626 		syncaddr.buf = kmem_alloc(syncaddr.len, KM_SLEEP);
627 		syncaddr.maxlen = syncaddr.len;
628 		if (copyin(userbufptr, syncaddr.buf, syncaddr.len)) {
629 			kmem_free(syncaddr.buf, syncaddr.len);
630 			error = EFAULT;
631 			goto errout;
632 		}
633 
634 		/* get server's netname */
635 		if (copyinstr(STRUCT_FGETP(args, netname), netname,
636 				sizeof (netname), &nlen)) {
637 			kmem_free(syncaddr.buf, syncaddr.len);
638 			error = EFAULT;
639 			goto errout;
640 		}
641 		netname[nlen] = '\0';
642 
643 		svp->sv_dhsec = create_authdh_data(netname, nlen, &syncaddr,
644 						knconf);
645 	}
646 
647 	/*
648 	 * Get the extention data which has the security data structure.
649 	 * This includes data for AUTH_SYS as well.
650 	 */
651 	if (flags & NFSMNT_NEWARGS) {
652 		switch (STRUCT_FGET(args, nfs_args_ext)) {
653 		case NFS_ARGS_EXTA:
654 		case NFS_ARGS_EXTB:
655 			/*
656 			 * Indicating the application is using the new
657 			 * sec_data structure to pass in the security
658 			 * data.
659 			 */
660 			if (STRUCT_FGETP(args,
661 			    nfs_ext_u.nfs_extA.secdata) == NULL) {
662 				error = EINVAL;
663 			} else {
664 				error = sec_clnt_loadinfo(
665 				    (struct sec_data *)STRUCT_FGETP(args,
666 					nfs_ext_u.nfs_extA.secdata),
667 				    &secdata, get_udatamodel());
668 			}
669 			break;
670 
671 		default:
672 			error = EINVAL;
673 			break;
674 		}
675 
676 	} else if (flags & NFSMNT_SECURE) {
677 		/*
678 		 * NFSMNT_SECURE is deprecated but we keep it
679 		 * to support the rouge user generated application
680 		 * that may use this undocumented interface to do
681 		 * AUTH_DH security.
682 		 */
683 		secdata = create_authdh_data(netname, nlen, &syncaddr, knconf);
684 
685 	} else {
686 		secdata = kmem_alloc(sizeof (*secdata), KM_SLEEP);
687 		secdata->secmod = secdata->rpcflavor = AUTH_SYS;
688 		secdata->data = NULL;
689 	}
690 
691 	svp->sv_secdata = secdata;
692 
693 	/* syncaddr is no longer needed. */
694 	if (syncaddr.buf != NULL)
695 		kmem_free(syncaddr.buf, syncaddr.len);
696 
697 	/*
698 	 * User does not explictly specify a flavor, and a user
699 	 * defined default flavor is passed down.
700 	 */
701 	if (flags & NFSMNT_SECDEFAULT) {
702 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
703 		svp->sv_flags |= SV4_TRYSECDEFAULT;
704 		nfs_rw_exit(&svp->sv_lock);
705 	}
706 
707 	/*
708 	 * Failover support:
709 	 *
710 	 * We may have a linked list of nfs_args structures,
711 	 * which means the user is looking for failover.  If
712 	 * the mount is either not "read-only" or "soft",
713 	 * we want to bail out with EINVAL.
714 	 */
715 	if (STRUCT_FGET(args, nfs_args_ext) == NFS_ARGS_EXTB &&
716 	    STRUCT_FGETP(args, nfs_ext_u.nfs_extB.next) != NULL) {
717 		if (uap->flags & MS_RDONLY && !(flags & NFSMNT_SOFT)) {
718 			data = (char *)STRUCT_FGETP(args,
719 			    nfs_ext_u.nfs_extB.next);
720 			goto more;
721 		}
722 		error = EINVAL;
723 		goto errout;
724 	}
725 
726 	/*
727 	 * Determine the zone we're being mounted into.
728 	 */
729 	if (getzoneid() == GLOBAL_ZONEID) {
730 		zone_t *mntzone;
731 
732 		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
733 		ASSERT(mntzone != NULL);
734 		zone_rele(mntzone);
735 		if (mntzone != zone) {
736 			error = EBUSY;
737 			goto errout;
738 		}
739 	}
740 
741 	/*
742 	 * Stop the mount from going any further if the zone is going away.
743 	 */
744 	if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN) {
745 		error = EBUSY;
746 		goto errout;
747 	}
748 
749 	/*
750 	 * Get root vnode.
751 	 */
752 proceed:
753 	error = nfs4rootvp(&rtvp, vfsp, svp_head, flags, cr, zone);
754 
755 	if (error)
756 		goto errout;
757 
758 	mi = VTOMI4(rtvp);
759 
760 	/*
761 	 * Send client id to the server, if necessary
762 	 */
763 	nfs4_error_zinit(&n4e);
764 	nfs4setclientid(mi, cr, FALSE, &n4e);
765 	error = n4e.error;
766 
767 	if (error)
768 		goto errout;
769 
770 	/*
771 	 * Set option fields in the mount info record
772 	 */
773 
774 	if (svp_head->sv_next) {
775 		mutex_enter(&mi->mi_lock);
776 		mi->mi_flags |= MI4_LLOCK;
777 		mutex_exit(&mi->mi_lock);
778 	}
779 
780 	error = nfs4_setopts(rtvp, get_udatamodel(), STRUCT_BUF(args));
781 
782 errout:
783 	if (error) {
784 		if (rtvp != NULL) {
785 			rp = VTOR4(rtvp);
786 			if (rp->r_flags & R4HASHED)
787 				rp4_rmhash(rp);
788 		}
789 		if (mi != NULL) {
790 			nfs4_async_stop(vfsp);
791 			nfs4_async_manager_stop(vfsp);
792 			nfs4_remove_mi_from_server(mi, NULL);
793 			/*
794 			 * In this error path we need to sfh4_rele() before
795 			 * we free the mntinfo4_t as sfh4_rele() has a
796 			 * dependancy on mi_fh_lock.
797 			 */
798 			if (rtvp != NULL)
799 				VN_RELE(rtvp);
800 			if (mi->mi_io_kstats) {
801 				kstat_delete(mi->mi_io_kstats);
802 				mi->mi_io_kstats = NULL;
803 			}
804 			if (mi->mi_ro_kstats) {
805 				kstat_delete(mi->mi_ro_kstats);
806 				mi->mi_ro_kstats = NULL;
807 			}
808 			if (mi->mi_recov_ksp) {
809 				kstat_delete(mi->mi_recov_ksp);
810 				mi->mi_recov_ksp = NULL;
811 			}
812 			nfs_free_mi4(mi);
813 			return (error);
814 		}
815 		sv4_free(svp_head);
816 	}
817 
818 	if (rtvp != NULL)
819 		VN_RELE(rtvp);
820 
821 	return (error);
822 }
823 
824 #ifdef	DEBUG
825 #define	VERS_MSG	"NFS4 server "
826 #else
827 #define	VERS_MSG	"NFS server "
828 #endif
829 
830 #define	READ_MSG	\
831 	VERS_MSG "%s returned 0 for read transfer size"
832 #define	WRITE_MSG	\
833 	VERS_MSG "%s returned 0 for write transfer size"
834 #define	SIZE_MSG	\
835 	VERS_MSG "%s returned 0 for maximum file size"
836 
837 /*
838  * Get the symbolic link text from the server for a given filehandle
839  * of that symlink.
840  *
841  *	(get symlink text) PUTFH READLINK
842  */
843 static int
844 getlinktext_otw(mntinfo4_t *mi, nfs_fh4 *fh, char **linktextp, cred_t *cr,
845 		int flags)
846 {
847 	COMPOUND4args_clnt args;
848 	COMPOUND4res_clnt res;
849 	int doqueue;
850 	nfs_argop4 argop[2];
851 	nfs_resop4 *resop;
852 	READLINK4res *lr_res;
853 	uint_t len;
854 	bool_t needrecov = FALSE;
855 	nfs4_recov_state_t recov_state;
856 	nfs4_sharedfh_t *sfh;
857 	nfs4_error_t e;
858 	int num_retry = nfs4_max_mount_retry;
859 	int recovery = !(flags & NFS4_GETFH_NEEDSOP);
860 
861 	sfh = sfh4_get(fh, mi);
862 	recov_state.rs_flags = 0;
863 	recov_state.rs_num_retry_despite_err = 0;
864 
865 recov_retry:
866 	nfs4_error_zinit(&e);
867 
868 	args.array_len = 2;
869 	args.array = argop;
870 	args.ctag = TAG_GET_SYMLINK;
871 
872 	if (! recovery) {
873 		e.error = nfs4_start_op(mi, NULL, NULL, &recov_state);
874 		if (e.error) {
875 			sfh4_rele(&sfh);
876 			return (e.error);
877 		}
878 	}
879 
880 	/* 0. putfh symlink fh */
881 	argop[0].argop = OP_CPUTFH;
882 	argop[0].nfs_argop4_u.opcputfh.sfh = sfh;
883 
884 	/* 1. readlink */
885 	argop[1].argop = OP_READLINK;
886 
887 	doqueue = 1;
888 
889 	rfs4call(mi, &args, &res, cr, &doqueue, 0, &e);
890 
891 	needrecov = nfs4_needs_recovery(&e, FALSE, mi->mi_vfsp);
892 
893 	if (needrecov && !recovery && num_retry-- > 0) {
894 
895 		NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
896 		    "getlinktext_otw: initiating recovery\n"));
897 
898 		if (nfs4_start_recovery(&e, mi, NULL, NULL, NULL, NULL,
899 		    OP_READLINK, NULL) == FALSE) {
900 			nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov);
901 			if (!e.error)
902 				(void) xdr_free(xdr_COMPOUND4res_clnt,
903 						(caddr_t)&res);
904 			goto recov_retry;
905 		}
906 	}
907 
908 	/*
909 	 * If non-NFS4 pcol error and/or we weren't able to recover.
910 	 */
911 	if (e.error != 0) {
912 		if (! recovery)
913 			nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov);
914 		sfh4_rele(&sfh);
915 		return (e.error);
916 	}
917 
918 	if (res.status) {
919 		e.error = geterrno4(res.status);
920 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
921 		if (! recovery)
922 			nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov);
923 		sfh4_rele(&sfh);
924 		return (e.error);
925 	}
926 
927 	/* res.status == NFS4_OK */
928 	ASSERT(res.status == NFS4_OK);
929 
930 	resop = &res.array[1];	/* readlink res */
931 	lr_res = &resop->nfs_resop4_u.opreadlink;
932 
933 	/* treat symlink name as data */
934 	*linktextp = utf8_to_str(&lr_res->link, &len, NULL);
935 
936 	if (! recovery)
937 		nfs4_end_op(mi, NULL, NULL, &recov_state, needrecov);
938 	sfh4_rele(&sfh);
939 	(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
940 
941 	return (0);
942 }
943 
944 /*
945  * Skip over consecutive slashes and "/./" in a pathname.
946  */
947 void
948 pathname_skipslashdot(struct pathname *pnp)
949 {
950 	char *c1, *c2;
951 
952 	while (pnp->pn_pathlen > 0 && *pnp->pn_path == '/') {
953 
954 		c1 = pnp->pn_path + 1;
955 		c2 = pnp->pn_path + 2;
956 
957 		if (*c1 == '.' && (*c2 == '/' || *c2 == '\0')) {
958 			pnp->pn_path = pnp->pn_path + 2; /* skip "/." */
959 			pnp->pn_pathlen = pnp->pn_pathlen - 2;
960 		} else {
961 			pnp->pn_path++;
962 			pnp->pn_pathlen--;
963 		}
964 	}
965 }
966 
967 /*
968  * Resolve a symbolic link path. The symlink is in the nth component of
969  * svp->sv_path and has an nfs4 file handle "fh".
970  * Upon return, the sv_path will point to the new path that has the nth
971  * component resolved to its symlink text.
972  */
973 int
974 resolve_sympath(mntinfo4_t *mi, servinfo4_t *svp, int nth, nfs_fh4 *fh,
975 		cred_t *cr, int flags)
976 {
977 	char *oldpath;
978 	char *symlink, *newpath;
979 	struct pathname oldpn, newpn;
980 	char component[MAXNAMELEN];
981 	int i, addlen, error = 0;
982 	int oldpathlen;
983 
984 	/* Get the symbolic link text over the wire. */
985 	error = getlinktext_otw(mi, fh, &symlink, cr, flags);
986 
987 	if (error || symlink == NULL || strlen(symlink) == 0)
988 		return (error);
989 
990 	/*
991 	 * Compose the new pathname.
992 	 * Note:
993 	 *    - only the nth component is resolved for the pathname.
994 	 *    - pathname.pn_pathlen does not count the ending null byte.
995 	 */
996 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
997 	oldpath = svp->sv_path;
998 	oldpathlen = svp->sv_pathlen;
999 	if (error = pn_get(oldpath, UIO_SYSSPACE, &oldpn)) {
1000 		nfs_rw_exit(&svp->sv_lock);
1001 		kmem_free(symlink, strlen(symlink) + 1);
1002 		return (error);
1003 	}
1004 	nfs_rw_exit(&svp->sv_lock);
1005 	pn_alloc(&newpn);
1006 
1007 	/*
1008 	 * Skip over previous components from the oldpath so that the
1009 	 * oldpn.pn_path will point to the symlink component. Skip
1010 	 * leading slashes and "/./" (no OP_LOOKUP on ".") so that
1011 	 * pn_getcompnent can get the component.
1012 	 */
1013 	for (i = 1; i < nth; i++) {
1014 		pathname_skipslashdot(&oldpn);
1015 		error = pn_getcomponent(&oldpn, component);
1016 		if (error)
1017 			goto out;
1018 	}
1019 
1020 	/*
1021 	 * Copy the old path upto the component right before the symlink
1022 	 * if the symlink is not an absolute path.
1023 	 */
1024 	if (symlink[0] != '/') {
1025 		addlen = oldpn.pn_path - oldpn.pn_buf;
1026 		bcopy(oldpn.pn_buf, newpn.pn_path, addlen);
1027 		newpn.pn_pathlen += addlen;
1028 		newpn.pn_path += addlen;
1029 		newpn.pn_buf[newpn.pn_pathlen] = '/';
1030 		newpn.pn_pathlen++;
1031 		newpn.pn_path++;
1032 	}
1033 
1034 	/* copy the resolved symbolic link text */
1035 	addlen = strlen(symlink);
1036 	if (newpn.pn_pathlen + addlen >= newpn.pn_bufsize) {
1037 		error = ENAMETOOLONG;
1038 		goto out;
1039 	}
1040 	bcopy(symlink, newpn.pn_path, addlen);
1041 	newpn.pn_pathlen += addlen;
1042 	newpn.pn_path += addlen;
1043 
1044 	/*
1045 	 * Check if there is any remaining path after the symlink component.
1046 	 * First, skip the symlink component.
1047 	 */
1048 	pathname_skipslashdot(&oldpn);
1049 	if (error = pn_getcomponent(&oldpn, component))
1050 		goto out;
1051 
1052 	addlen = pn_pathleft(&oldpn); /* includes counting the slash */
1053 
1054 	/*
1055 	 * Copy the remaining path to the new pathname if there is any.
1056 	 */
1057 	if (addlen > 0) {
1058 		if (newpn.pn_pathlen + addlen >= newpn.pn_bufsize) {
1059 			error = ENAMETOOLONG;
1060 			goto out;
1061 		}
1062 		bcopy(oldpn.pn_path, newpn.pn_path, addlen);
1063 		newpn.pn_pathlen += addlen;
1064 	}
1065 	newpn.pn_buf[newpn.pn_pathlen] = '\0';
1066 
1067 	/* get the newpath and store it in the servinfo4_t */
1068 	newpath = kmem_alloc(newpn.pn_pathlen + 1, KM_SLEEP);
1069 	bcopy(newpn.pn_buf, newpath, newpn.pn_pathlen);
1070 	newpath[newpn.pn_pathlen] = '\0';
1071 
1072 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
1073 	svp->sv_path = newpath;
1074 	svp->sv_pathlen = strlen(newpath) + 1;
1075 	nfs_rw_exit(&svp->sv_lock);
1076 
1077 	kmem_free(oldpath, oldpathlen);
1078 out:
1079 	kmem_free(symlink, strlen(symlink) + 1);
1080 	pn_free(&newpn);
1081 	pn_free(&oldpn);
1082 
1083 	return (error);
1084 }
1085 
1086 /*
1087  * Get the root filehandle for the given filesystem and server, and update
1088  * svp.
1089  *
1090  * If NFS4_GETFH_NEEDSOP is set, then use nfs4_start_fop and nfs4_end_fop
1091  * to coordinate with recovery.  Otherwise, the caller is assumed to be
1092  * the recovery thread or have already done a start_fop.
1093  *
1094  * Errors are returned by the nfs4_error_t parameter.
1095  */
1096 
1097 static void
1098 nfs4getfh_otw(struct mntinfo4 *mi, servinfo4_t *svp, vtype_t *vtp,
1099 		int flags, cred_t *cr, nfs4_error_t *ep)
1100 {
1101 	COMPOUND4args_clnt args;
1102 	COMPOUND4res_clnt res;
1103 	int doqueue = 1;
1104 	nfs_argop4 *argop;
1105 	nfs_resop4 *resop;
1106 	nfs4_ga_res_t *garp;
1107 	int num_argops;
1108 	lookup4_param_t lookuparg;
1109 	nfs_fh4 *tmpfhp;
1110 	nfs_fh4 *resfhp;
1111 	bool_t needrecov = FALSE;
1112 	nfs4_recov_state_t recov_state;
1113 	int llndx;
1114 	int nthcomp;
1115 	int recovery = !(flags & NFS4_GETFH_NEEDSOP);
1116 
1117 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1118 	ASSERT(svp->sv_path != NULL);
1119 	if (svp->sv_path[0] == '\0') {
1120 		nfs_rw_exit(&svp->sv_lock);
1121 		nfs4_error_init(ep, EINVAL);
1122 		return;
1123 	}
1124 	nfs_rw_exit(&svp->sv_lock);
1125 
1126 	recov_state.rs_flags = 0;
1127 	recov_state.rs_num_retry_despite_err = 0;
1128 recov_retry:
1129 	nfs4_error_zinit(ep);
1130 
1131 	if (!recovery) {
1132 		ep->error = nfs4_start_fop(mi, NULL, NULL, OH_MOUNT,
1133 				&recov_state, NULL);
1134 
1135 		/*
1136 		 * If recovery has been started and this request as
1137 		 * initiated by a mount, then we must wait for recovery
1138 		 * to finish before proceeding, otherwise, the error
1139 		 * cleanup would remove data structures needed by the
1140 		 * recovery thread.
1141 		 */
1142 		if (ep->error) {
1143 			mutex_enter(&mi->mi_lock);
1144 			if (mi->mi_flags & MI4_MOUNTING) {
1145 				mi->mi_flags |= MI4_RECOV_FAIL;
1146 				mi->mi_error = EIO;
1147 
1148 				NFS4_DEBUG(nfs4_client_recov_debug, (CE_NOTE,
1149 				    "nfs4getfh_otw: waiting 4 recovery\n"));
1150 
1151 				while (mi->mi_flags & MI4_RECOV_ACTIV)
1152 					cv_wait(&mi->mi_failover_cv,
1153 					    &mi->mi_lock);
1154 			}
1155 			mutex_exit(&mi->mi_lock);
1156 			return;
1157 		}
1158 
1159 		/*
1160 		 * If the client does not specify a specific flavor to use
1161 		 * and has not gotten a secinfo list from the server yet,
1162 		 * retrieve the secinfo list from the server and use a
1163 		 * flavor from the list to mount.
1164 		 *
1165 		 * If fail to get the secinfo list from the server, then
1166 		 * try the default flavor.
1167 		 */
1168 		if ((svp->sv_flags & SV4_TRYSECDEFAULT) &&
1169 		    svp->sv_secinfo == NULL) {
1170 			(void) nfs4_secinfo_path(mi, cr, FALSE);
1171 		}
1172 	}
1173 
1174 	if (recovery)
1175 		args.ctag = TAG_REMAP_MOUNT;
1176 	else
1177 		args.ctag = TAG_MOUNT;
1178 
1179 	lookuparg.l4_getattrs = LKP4_ALL_ATTRIBUTES;
1180 	lookuparg.argsp = &args;
1181 	lookuparg.resp = &res;
1182 	lookuparg.header_len = 2;	/* Putrootfh, getfh */
1183 	lookuparg.trailer_len = 0;
1184 	lookuparg.ga_bits = FATTR4_FSINFO_MASK;
1185 	lookuparg.mi = mi;
1186 
1187 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1188 	ASSERT(svp->sv_path != NULL);
1189 	llndx = nfs4lookup_setup(svp->sv_path, &lookuparg, 0);
1190 	nfs_rw_exit(&svp->sv_lock);
1191 
1192 	argop = args.array;
1193 	num_argops = args.array_len;
1194 
1195 	/* choose public or root filehandle */
1196 	if (flags & NFS4_GETFH_PUBLIC)
1197 		argop[0].argop = OP_PUTPUBFH;
1198 	else
1199 		argop[0].argop = OP_PUTROOTFH;
1200 
1201 	/* get fh */
1202 	argop[1].argop = OP_GETFH;
1203 
1204 	NFS4_DEBUG(nfs4_client_call_debug, (CE_NOTE,
1205 	    "nfs4getfh_otw: %s call, mi 0x%p",
1206 	    needrecov ? "recov" : "first", (void *)mi));
1207 
1208 	rfs4call(mi, &args, &res, cr, &doqueue, RFSCALL_SOFT, ep);
1209 
1210 	needrecov = nfs4_needs_recovery(ep, FALSE, mi->mi_vfsp);
1211 
1212 	if (needrecov) {
1213 		bool_t abort;
1214 
1215 		if (recovery) {
1216 			nfs4args_lookup_free(argop, num_argops);
1217 			kmem_free(argop,
1218 					lookuparg.arglen * sizeof (nfs_argop4));
1219 			if (!ep->error)
1220 				(void) xdr_free(xdr_COMPOUND4res_clnt,
1221 								(caddr_t)&res);
1222 			return;
1223 		}
1224 
1225 		NFS4_DEBUG(nfs4_client_recov_debug,
1226 		    (CE_NOTE, "nfs4getfh_otw: initiating recovery\n"));
1227 
1228 		abort = nfs4_start_recovery(ep, mi, NULL,
1229 			    NULL, NULL, NULL, OP_GETFH, NULL);
1230 		if (!ep->error) {
1231 			ep->error = geterrno4(res.status);
1232 			(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1233 		}
1234 		nfs4args_lookup_free(argop, num_argops);
1235 		kmem_free(argop, lookuparg.arglen * sizeof (nfs_argop4));
1236 		nfs4_end_fop(mi, NULL, NULL, OH_MOUNT, &recov_state, needrecov);
1237 		/* have another go? */
1238 		if (abort == FALSE)
1239 			goto recov_retry;
1240 		return;
1241 	}
1242 
1243 	/*
1244 	 * No recovery, but check if error is set.
1245 	 */
1246 	if (ep->error)  {
1247 		nfs4args_lookup_free(argop, num_argops);
1248 		kmem_free(argop, lookuparg.arglen * sizeof (nfs_argop4));
1249 		if (!recovery)
1250 			nfs4_end_fop(mi, NULL, NULL, OH_MOUNT, &recov_state,
1251 				needrecov);
1252 		return;
1253 	}
1254 
1255 is_link_err:
1256 
1257 	/* for non-recovery errors */
1258 	if (res.status && res.status != NFS4ERR_SYMLINK) {
1259 		if (!recovery) {
1260 			nfs4_end_fop(mi, NULL, NULL, OH_MOUNT, &recov_state,
1261 				needrecov);
1262 		}
1263 		nfs4args_lookup_free(argop, num_argops);
1264 		kmem_free(argop, lookuparg.arglen * sizeof (nfs_argop4));
1265 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1266 		return;
1267 	}
1268 
1269 	/*
1270 	 * If any intermediate component in the path is a symbolic link,
1271 	 * resolve the symlink, then try mount again using the new path.
1272 	 */
1273 	if (res.status == NFS4ERR_SYMLINK) {
1274 		int where;
1275 
1276 		/*
1277 		 * This must be from OP_LOOKUP failure. The (cfh) for this
1278 		 * OP_LOOKUP is a symlink node. Found out where the
1279 		 * OP_GETFH is for the (cfh) that is a symlink node.
1280 		 *
1281 		 * Example:
1282 		 * (mount) PUTROOTFH, GETFH, LOOKUP comp1, GETFH, GETATTR,
1283 		 * LOOKUP comp2, GETFH, GETATTR, LOOKUP comp3, GETFH, GETATTR
1284 		 *
1285 		 * LOOKUP comp3 fails with SYMLINK because comp2 is a symlink.
1286 		 * In this case, where = 7, nthcomp = 2.
1287 		 */
1288 		where = res.array_len - 2;
1289 		ASSERT(where > 0);
1290 
1291 		resop = &res.array[where - 1];
1292 		ASSERT(resop->resop == OP_GETFH);
1293 		tmpfhp = &resop->nfs_resop4_u.opgetfh.object;
1294 		nthcomp = res.array_len/3 - 1;
1295 
1296 		/*
1297 		 * Need to call nfs4_end_op before resolve_sympath to avoid
1298 		 * potential nfs4_start_op deadlock.
1299 		 */
1300 		if (!recovery)
1301 			nfs4_end_fop(mi, NULL, NULL, OH_MOUNT, &recov_state,
1302 				needrecov);
1303 
1304 		ep->error = resolve_sympath(mi, svp, nthcomp, tmpfhp, cr,
1305 					    flags);
1306 
1307 		nfs4args_lookup_free(argop, num_argops);
1308 		kmem_free(argop, lookuparg.arglen * sizeof (nfs_argop4));
1309 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1310 
1311 		if (ep->error)
1312 			return;
1313 
1314 		goto recov_retry;
1315 	}
1316 
1317 	/* getfh */
1318 	resop = &res.array[res.array_len - 2];
1319 	ASSERT(resop->resop == OP_GETFH);
1320 	resfhp = &resop->nfs_resop4_u.opgetfh.object;
1321 
1322 	/* getattr fsinfo res */
1323 	resop++;
1324 	garp = &resop->nfs_resop4_u.opgetattr.ga_res;
1325 
1326 	*vtp = garp->n4g_va.va_type;
1327 
1328 	mi->mi_fh_expire_type = garp->n4g_ext_res->n4g_fet;
1329 
1330 	mutex_enter(&mi->mi_lock);
1331 	if (garp->n4g_ext_res->n4g_pc4.pc4_link_support)
1332 		mi->mi_flags |= MI4_LINK;
1333 	if (garp->n4g_ext_res->n4g_pc4.pc4_symlink_support)
1334 		mi->mi_flags |= MI4_SYMLINK;
1335 	if (garp->n4g_ext_res->n4g_suppattrs & FATTR4_ACL_MASK)
1336 		mi->mi_flags |= MI4_ACL;
1337 	mutex_exit(&mi->mi_lock);
1338 
1339 	if (garp->n4g_ext_res->n4g_maxread == 0)
1340 		mi->mi_tsize =
1341 			MIN(MAXBSIZE, mi->mi_tsize);
1342 	else
1343 		mi->mi_tsize =
1344 			MIN(garp->n4g_ext_res->n4g_maxread,
1345 			    mi->mi_tsize);
1346 
1347 	if (garp->n4g_ext_res->n4g_maxwrite == 0)
1348 		mi->mi_stsize =
1349 			MIN(MAXBSIZE, mi->mi_stsize);
1350 	else
1351 		mi->mi_stsize =
1352 			MIN(garp->n4g_ext_res->n4g_maxwrite,
1353 			    mi->mi_stsize);
1354 
1355 	if (garp->n4g_ext_res->n4g_maxfilesize != 0)
1356 		mi->mi_maxfilesize =
1357 			MIN(garp->n4g_ext_res->n4g_maxfilesize,
1358 			    mi->mi_maxfilesize);
1359 
1360 	/*
1361 	 * If the final component is a a symbolic link, resolve the symlink,
1362 	 * then try mount again using the new path.
1363 	 *
1364 	 * Assume no symbolic link for root filesysm "/".
1365 	 */
1366 	if (*vtp == VLNK) {
1367 		/*
1368 		 * nthcomp is the total result length minus
1369 		 * the 1st 2 OPs (PUTROOTFH, GETFH),
1370 		 * then divided by 3 (LOOKUP,GETFH,GETATTR)
1371 		 *
1372 		 * e.g. PUTROOTFH GETFH LOOKUP 1st-comp GETFH GETATTR
1373 		 *	LOOKUP 2nd-comp GETFH GETATTR
1374 		 *
1375 		 *	(8 - 2)/3 = 2
1376 		 */
1377 		nthcomp = (res.array_len - 2)/3;
1378 
1379 		/*
1380 		 * Need to call nfs4_end_op before resolve_sympath to avoid
1381 		 * potential nfs4_start_op deadlock. See RFE 4777612.
1382 		 */
1383 		if (!recovery)
1384 			nfs4_end_fop(mi, NULL, NULL, OH_MOUNT, &recov_state,
1385 				needrecov);
1386 
1387 		ep->error = resolve_sympath(mi, svp, nthcomp, resfhp, cr,
1388 					flags);
1389 
1390 		nfs4args_lookup_free(argop, num_argops);
1391 		kmem_free(argop, lookuparg.arglen * sizeof (nfs_argop4));
1392 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1393 
1394 		if (ep->error)
1395 			return;
1396 
1397 		goto recov_retry;
1398 	}
1399 
1400 	/*
1401 	 * We need to figure out where in the compound the getfh
1402 	 * for the parent directory is. If the object to be mounted is
1403 	 * the root, then there is no lookup at all:
1404 	 * PUTROOTFH, GETFH.
1405 	 * If the object to be mounted is in the root, then the compound is:
1406 	 * PUTROOTFH, GETFH, LOOKUP, GETFH, GETATTR.
1407 	 * In either of these cases, the index of the GETFH is 1.
1408 	 * If it is not at the root, then it's something like:
1409 	 * PUTROOTFH, GETFH, LOOKUP, GETFH, GETATTR,
1410 	 * LOOKUP, GETFH, GETATTR
1411 	 * In this case, the index is llndx (last lookup index) - 2.
1412 	 */
1413 	if (llndx == -1 || llndx == 2)
1414 		resop = &res.array[1];
1415 	else {
1416 		ASSERT(llndx > 2);
1417 		resop = &res.array[llndx-2];
1418 	}
1419 
1420 	ASSERT(resop->resop == OP_GETFH);
1421 	tmpfhp = &resop->nfs_resop4_u.opgetfh.object;
1422 
1423 	/* save the filehandles for the replica */
1424 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
1425 	ASSERT(tmpfhp->nfs_fh4_len <= NFS4_FHSIZE);
1426 	svp->sv_pfhandle.fh_len = tmpfhp->nfs_fh4_len;
1427 	bcopy(tmpfhp->nfs_fh4_val, svp->sv_pfhandle.fh_buf,
1428 	    tmpfhp->nfs_fh4_len);
1429 	ASSERT(resfhp->nfs_fh4_len <= NFS4_FHSIZE);
1430 	svp->sv_fhandle.fh_len = resfhp->nfs_fh4_len;
1431 	bcopy(resfhp->nfs_fh4_val, svp->sv_fhandle.fh_buf, resfhp->nfs_fh4_len);
1432 
1433 	/* initialize fsid and supp_attrs for server fs */
1434 	svp->sv_fsid = garp->n4g_fsid;
1435 	svp->sv_supp_attrs =
1436 		garp->n4g_ext_res->n4g_suppattrs | FATTR4_MANDATTR_MASK;
1437 
1438 	nfs_rw_exit(&svp->sv_lock);
1439 
1440 	nfs4args_lookup_free(argop, num_argops);
1441 	kmem_free(argop, lookuparg.arglen * sizeof (nfs_argop4));
1442 	(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
1443 	if (!recovery)
1444 		nfs4_end_fop(mi, NULL, NULL, OH_MOUNT, &recov_state, needrecov);
1445 }
1446 
1447 static ushort_t nfs4_max_threads = 8;	/* max number of active async threads */
1448 static uint_t nfs4_bsize = 32 * 1024;	/* client `block' size */
1449 static uint_t nfs4_async_clusters = 1;	/* # of reqs from each async queue */
1450 static uint_t nfs4_cots_timeo = NFS_COTS_TIMEO;
1451 
1452 /*
1453  * Remap the root filehandle for the given filesystem.
1454  *
1455  * results returned via the nfs4_error_t parameter.
1456  */
1457 void
1458 nfs4_remap_root(mntinfo4_t *mi, nfs4_error_t *ep, int flags)
1459 {
1460 	struct servinfo4 *svp;
1461 	vtype_t vtype;
1462 	nfs_fh4 rootfh;
1463 	int getfh_flags;
1464 	char *orig_sv_path;
1465 	int orig_sv_pathlen, num_retry;
1466 
1467 	mutex_enter(&mi->mi_lock);
1468 	svp = mi->mi_curr_serv;
1469 	getfh_flags =
1470 		(flags & NFS4_REMAP_NEEDSOP) ? NFS4_GETFH_NEEDSOP : 0;
1471 	getfh_flags |=
1472 		(mi->mi_flags & MI4_PUBLIC) ? NFS4_GETFH_PUBLIC : 0;
1473 	mutex_exit(&mi->mi_lock);
1474 
1475 	/*
1476 	 * Just in case server path being mounted contains
1477 	 * symlinks and fails w/STALE, save the initial sv_path
1478 	 * so we can redrive the initial mount compound with the
1479 	 * initial sv_path -- not a symlink-expanded version.
1480 	 *
1481 	 * This could only happen if a symlink was expanded
1482 	 * and the expanded mount compound failed stale.  Because
1483 	 * it could be the case that the symlink was removed at
1484 	 * the server (and replaced with another symlink/dir,
1485 	 * we need to use the initial sv_path when attempting
1486 	 * to re-lookup everything and recover.
1487 	 */
1488 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1489 	orig_sv_pathlen = svp->sv_pathlen;
1490 	orig_sv_path = kmem_alloc(orig_sv_pathlen, KM_SLEEP);
1491 	bcopy(svp->sv_path, orig_sv_path, orig_sv_pathlen);
1492 	nfs_rw_exit(&svp->sv_lock);
1493 
1494 	num_retry = nfs4_max_mount_retry;
1495 
1496 	do {
1497 		/*
1498 		 * Get the root fh from the server.  Retry nfs4_max_mount_retry
1499 		 * (2) times if it fails with STALE since the recovery
1500 		 * infrastructure doesn't do STALE recovery for components
1501 		 * of the server path to the object being mounted.
1502 		 */
1503 		nfs4getfh_otw(mi, svp, &vtype, getfh_flags, CRED(), ep);
1504 
1505 		if (ep->error == 0 && ep->stat == NFS4_OK)
1506 			break;
1507 
1508 		/*
1509 		 * For some reason, the mount compound failed.  Before
1510 		 * retrying, we need to restore the original sv_path
1511 		 * because it might have contained symlinks that were
1512 		 * expanded by nfsgetfh_otw before the failure occurred.
1513 		 * replace current sv_path with orig sv_path -- just in case
1514 		 * it changed due to embedded symlinks.
1515 		 */
1516 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1517 		if (orig_sv_pathlen != svp->sv_pathlen) {
1518 			kmem_free(svp->sv_path, svp->sv_pathlen);
1519 			svp->sv_path = kmem_alloc(orig_sv_pathlen, KM_SLEEP);
1520 			svp->sv_pathlen = orig_sv_pathlen;
1521 
1522 		}
1523 		bcopy(orig_sv_path, svp->sv_path, orig_sv_pathlen);
1524 		nfs_rw_exit(&svp->sv_lock);
1525 
1526 	} while (num_retry-- > 0);
1527 
1528 	kmem_free(orig_sv_path, orig_sv_pathlen);
1529 
1530 	if (ep->error != 0 || ep->stat != 0) {
1531 		return;
1532 	}
1533 
1534 	if (vtype != VNON && vtype != mi->mi_type) {
1535 		/* shouldn't happen */
1536 		zcmn_err(mi->mi_zone->zone_id, CE_WARN,
1537 			"nfs4_remap_root: server root vnode type (%d) doesn't "
1538 			"match mount info (%d)", vtype, mi->mi_type);
1539 	}
1540 
1541 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1542 	rootfh.nfs_fh4_val = svp->sv_fhandle.fh_buf;
1543 	rootfh.nfs_fh4_len = svp->sv_fhandle.fh_len;
1544 	nfs_rw_exit(&svp->sv_lock);
1545 	sfh4_update(mi->mi_rootfh, &rootfh);
1546 
1547 #ifdef DEBUG
1548 	/*
1549 	 * There shouldn't have been any other recovery activity on the
1550 	 * filesystem.
1551 	 */
1552 	mutex_enter(&mi->mi_lock);
1553 	ASSERT(mi->mi_curr_serv == svp);
1554 	mutex_exit(&mi->mi_lock);
1555 #endif
1556 }
1557 
1558 static int
1559 nfs4rootvp(vnode_t **rtvpp, vfs_t *vfsp, struct servinfo4 *svp_head,
1560 	int flags, cred_t *cr, zone_t *zone)
1561 {
1562 	vnode_t *rtvp = NULL;
1563 	mntinfo4_t *mi;
1564 	dev_t nfs_dev;
1565 	int error = 0;
1566 	rnode4_t *rp;
1567 	int i;
1568 	struct vattr va;
1569 	vtype_t vtype = VNON;
1570 	vtype_t tmp_vtype = VNON;
1571 	struct servinfo4 *firstsvp = NULL, *svp = svp_head;
1572 	nfs4_oo_hash_bucket_t *bucketp;
1573 	nfs_fh4 fh;
1574 	char *droptext = "";
1575 	struct nfs_stats *nfsstatsp;
1576 	nfs4_fname_t *mfname;
1577 	nfs4_error_t e;
1578 	char *orig_sv_path;
1579 	int orig_sv_pathlen, num_retry;
1580 	cred_t *lcr = NULL, *tcr = cr;
1581 
1582 	nfsstatsp = zone_getspecific(nfsstat_zone_key, nfs_zone());
1583 	ASSERT(nfsstatsp != NULL);
1584 
1585 	ASSERT(nfs_zone() == zone);
1586 	ASSERT(crgetref(cr));
1587 
1588 	/*
1589 	 * Create a mount record and link it to the vfs struct.
1590 	 */
1591 	mi = kmem_zalloc(sizeof (*mi), KM_SLEEP);
1592 	mutex_init(&mi->mi_lock, NULL, MUTEX_DEFAULT, NULL);
1593 	nfs_rw_init(&mi->mi_recovlock, NULL, RW_DEFAULT, NULL);
1594 	nfs_rw_init(&mi->mi_rename_lock, NULL, RW_DEFAULT, NULL);
1595 	nfs_rw_init(&mi->mi_fh_lock, NULL, RW_DEFAULT, NULL);
1596 
1597 	if (!(flags & NFSMNT_SOFT))
1598 		mi->mi_flags |= MI4_HARD;
1599 	if ((flags & NFSMNT_NOPRINT))
1600 		mi->mi_flags |= MI4_NOPRINT;
1601 	if (flags & NFSMNT_INT)
1602 		mi->mi_flags |= MI4_INT;
1603 	if (flags & NFSMNT_PUBLIC)
1604 		mi->mi_flags |= MI4_PUBLIC;
1605 	mi->mi_retrans = NFS_RETRIES;
1606 	if (svp->sv_knconf->knc_semantics == NC_TPI_COTS_ORD ||
1607 	    svp->sv_knconf->knc_semantics == NC_TPI_COTS)
1608 		mi->mi_timeo = nfs4_cots_timeo;
1609 	else
1610 		mi->mi_timeo = NFS_TIMEO;
1611 	mi->mi_prog = NFS_PROGRAM;
1612 	mi->mi_vers = NFS_V4;
1613 	mi->mi_rfsnames = rfsnames_v4;
1614 	mi->mi_reqs = nfsstatsp->nfs_stats_v4.rfsreqcnt_ptr;
1615 	cv_init(&mi->mi_failover_cv, NULL, CV_DEFAULT, NULL);
1616 	mi->mi_servers = svp;
1617 	mi->mi_curr_serv = svp;
1618 	mi->mi_acregmin = SEC2HR(ACREGMIN);
1619 	mi->mi_acregmax = SEC2HR(ACREGMAX);
1620 	mi->mi_acdirmin = SEC2HR(ACDIRMIN);
1621 	mi->mi_acdirmax = SEC2HR(ACDIRMAX);
1622 	mi->mi_fh_expire_type = FH4_PERSISTENT;
1623 	mi->mi_clientid_next = NULL;
1624 	mi->mi_clientid_prev = NULL;
1625 	mi->mi_grace_wait = 0;
1626 	mi->mi_error = 0;
1627 	mi->mi_srvsettime = 0;
1628 
1629 	mi->mi_tsize = nfs4_tsize(svp->sv_knconf);
1630 	mi->mi_stsize = mi->mi_tsize;
1631 
1632 	if (flags & NFSMNT_DIRECTIO)
1633 		mi->mi_flags |= MI4_DIRECTIO;
1634 
1635 	mi->mi_flags |= MI4_MOUNTING;
1636 
1637 	/*
1638 	 * Make a vfs struct for nfs.  We do this here instead of below
1639 	 * because rtvp needs a vfs before we can do a getattr on it.
1640 	 *
1641 	 * Assign a unique device id to the mount
1642 	 */
1643 	mutex_enter(&nfs_minor_lock);
1644 	do {
1645 		nfs_minor = (nfs_minor + 1) & MAXMIN32;
1646 		nfs_dev = makedevice(nfs_major, nfs_minor);
1647 	} while (vfs_devismounted(nfs_dev));
1648 	mutex_exit(&nfs_minor_lock);
1649 
1650 	vfsp->vfs_dev = nfs_dev;
1651 	vfs_make_fsid(&vfsp->vfs_fsid, nfs_dev, nfs4fstyp);
1652 	vfsp->vfs_data = (caddr_t)mi;
1653 	vfsp->vfs_fstype = nfsfstyp;
1654 	vfsp->vfs_bsize = nfs4_bsize;
1655 
1656 	/*
1657 	 * Initialize fields used to support async putpage operations.
1658 	 */
1659 	for (i = 0; i < NFS4_ASYNC_TYPES; i++)
1660 		mi->mi_async_clusters[i] = nfs4_async_clusters;
1661 	mi->mi_async_init_clusters = nfs4_async_clusters;
1662 	mi->mi_async_curr = &mi->mi_async_reqs[0];
1663 	mi->mi_max_threads = nfs4_max_threads;
1664 	mutex_init(&mi->mi_async_lock, NULL, MUTEX_DEFAULT, NULL);
1665 	cv_init(&mi->mi_async_reqs_cv, NULL, CV_DEFAULT, NULL);
1666 	cv_init(&mi->mi_async_work_cv, NULL, CV_DEFAULT, NULL);
1667 	cv_init(&mi->mi_async_cv, NULL, CV_DEFAULT, NULL);
1668 	cv_init(&mi->mi_inact_req_cv, NULL, CV_DEFAULT, NULL);
1669 
1670 	mi->mi_vfsp = vfsp;
1671 	zone_hold(mi->mi_zone = zone);
1672 	nfs4_mi_zonelist_add(mi);
1673 
1674 	/*
1675 	 * Initialize the <open owner/cred> hash table.
1676 	 */
1677 	for (i = 0; i < NFS4_NUM_OO_BUCKETS; i++) {
1678 		bucketp = &(mi->mi_oo_list[i]);
1679 		mutex_init(&bucketp->b_lock, NULL, MUTEX_DEFAULT, NULL);
1680 		list_create(&bucketp->b_oo_hash_list,
1681 		    sizeof (nfs4_open_owner_t),
1682 		    offsetof(nfs4_open_owner_t, oo_hash_node));
1683 	}
1684 
1685 	/*
1686 	 * Initialize the freed open owner list.
1687 	 */
1688 	mi->mi_foo_num = 0;
1689 	mi->mi_foo_max = NFS4_NUM_FREED_OPEN_OWNERS;
1690 	list_create(&mi->mi_foo_list, sizeof (nfs4_open_owner_t),
1691 	    offsetof(nfs4_open_owner_t, oo_foo_node));
1692 
1693 	list_create(&mi->mi_lost_state, sizeof (nfs4_lost_rqst_t),
1694 	    offsetof(nfs4_lost_rqst_t, lr_node));
1695 
1696 	list_create(&mi->mi_bseqid_list, sizeof (nfs4_bseqid_entry_t),
1697 	    offsetof(nfs4_bseqid_entry_t, bs_node));
1698 
1699 	/*
1700 	 * Initialize the msg buffer.
1701 	 */
1702 	list_create(&mi->mi_msg_list, sizeof (nfs4_debug_msg_t),
1703 	    offsetof(nfs4_debug_msg_t, msg_node));
1704 	mi->mi_msg_count = 0;
1705 	mutex_init(&mi->mi_msg_list_lock, NULL, MUTEX_DEFAULT, NULL);
1706 
1707 	/*
1708 	 * Initialize kstats
1709 	 */
1710 	nfs4_mnt_kstat_init(vfsp);
1711 
1712 	/*
1713 	 * Initialize the shared filehandle pool, and get the fname for
1714 	 * the filesystem root.
1715 	 */
1716 	sfh4_createtab(&mi->mi_filehandles);
1717 	mi->mi_fname = fn_get(NULL, ".");
1718 
1719 	/*
1720 	 * Save server path we're attempting to mount.
1721 	 */
1722 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
1723 	orig_sv_pathlen = svp_head->sv_pathlen;
1724 	orig_sv_path = kmem_alloc(svp_head->sv_pathlen, KM_SLEEP);
1725 	bcopy(svp_head->sv_path, orig_sv_path, svp_head->sv_pathlen);
1726 	nfs_rw_exit(&svp->sv_lock);
1727 
1728 	/*
1729 	 * Make the GETFH call to get root fh for each replica.
1730 	 */
1731 	if (svp_head->sv_next)
1732 		droptext = ", dropping replica";
1733 
1734 	/*
1735 	 * If the uid is set then set the creds for secure mounts
1736 	 * by proxy processes such as automountd.
1737 	 */
1738 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1739 	if (svp->sv_secdata->uid != 0) {
1740 		lcr = crdup(cr);
1741 		(void) crsetugid(lcr, svp->sv_secdata->uid, crgetgid(cr));
1742 		tcr = lcr;
1743 	}
1744 	nfs_rw_exit(&svp->sv_lock);
1745 	for (svp = svp_head; svp; svp = svp->sv_next) {
1746 		if (nfs4_chkdup_servinfo4(svp_head, svp)) {
1747 			nfs_cmn_err(error, CE_WARN,
1748 				VERS_MSG "Host %s is a duplicate%s",
1749 				svp->sv_hostname, droptext);
1750 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
1751 			svp->sv_flags |= SV4_NOTINUSE;
1752 			nfs_rw_exit(&svp->sv_lock);
1753 			continue;
1754 		}
1755 		mi->mi_curr_serv = svp;
1756 
1757 		/*
1758 		 * Just in case server path being mounted contains
1759 		 * symlinks and fails w/STALE, save the initial sv_path
1760 		 * so we can redrive the initial mount compound with the
1761 		 * initial sv_path -- not a symlink-expanded version.
1762 		 *
1763 		 * This could only happen if a symlink was expanded
1764 		 * and the expanded mount compound failed stale.  Because
1765 		 * it could be the case that the symlink was removed at
1766 		 * the server (and replaced with another symlink/dir,
1767 		 * we need to use the initial sv_path when attempting
1768 		 * to re-lookup everything and recover.
1769 		 *
1770 		 * Other mount errors should evenutally be handled here also
1771 		 * (NFS4ERR_DELAY, NFS4ERR_RESOURCE).  For now, all mount
1772 		 * failures will result in mount being redriven a few times.
1773 		 */
1774 		num_retry = nfs4_max_mount_retry;
1775 		do {
1776 			nfs4getfh_otw(mi, svp, &tmp_vtype,
1777 			    ((flags & NFSMNT_PUBLIC) ? NFS4_GETFH_PUBLIC : 0) |
1778 			    NFS4_GETFH_NEEDSOP, tcr, &e);
1779 
1780 			if (e.error == 0 && e.stat == NFS4_OK)
1781 				break;
1782 
1783 			/*
1784 			 * replace current sv_path with orig sv_path -- just in
1785 			 * case it changed due to embedded symlinks.
1786 			 */
1787 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1788 			if (orig_sv_pathlen != svp->sv_pathlen) {
1789 				kmem_free(svp->sv_path, svp->sv_pathlen);
1790 				svp->sv_path = kmem_alloc(orig_sv_pathlen,
1791 							KM_SLEEP);
1792 				svp->sv_pathlen = orig_sv_pathlen;
1793 			}
1794 			bcopy(orig_sv_path, svp->sv_path, orig_sv_pathlen);
1795 			nfs_rw_exit(&svp->sv_lock);
1796 
1797 		} while (num_retry-- > 0);
1798 
1799 		error = e.error ? e.error : geterrno4(e.stat);
1800 		if (error) {
1801 			nfs_cmn_err(error, CE_WARN,
1802 				VERS_MSG "initial call to %s failed%s: %m",
1803 				svp->sv_hostname, droptext);
1804 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
1805 			svp->sv_flags |= SV4_NOTINUSE;
1806 			nfs_rw_exit(&svp->sv_lock);
1807 			mi->mi_flags &= ~MI4_RECOV_FAIL;
1808 			mi->mi_error = 0;
1809 			continue;
1810 		}
1811 
1812 		if (tmp_vtype == VBAD) {
1813 			zcmn_err(mi->mi_zone->zone_id, CE_WARN,
1814 				VERS_MSG "%s returned a bad file type for "
1815 				"root%s", svp->sv_hostname, droptext);
1816 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
1817 			svp->sv_flags |= SV4_NOTINUSE;
1818 			nfs_rw_exit(&svp->sv_lock);
1819 			continue;
1820 		}
1821 
1822 		if (vtype == VNON) {
1823 			vtype = tmp_vtype;
1824 		} else if (vtype != tmp_vtype) {
1825 			zcmn_err(mi->mi_zone->zone_id, CE_WARN,
1826 				VERS_MSG "%s returned a different file type "
1827 				"for root%s", svp->sv_hostname, droptext);
1828 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
1829 			svp->sv_flags |= SV4_NOTINUSE;
1830 			nfs_rw_exit(&svp->sv_lock);
1831 			continue;
1832 		}
1833 		if (firstsvp == NULL)
1834 			firstsvp = svp;
1835 	}
1836 
1837 	kmem_free(orig_sv_path, orig_sv_pathlen);
1838 
1839 	if (firstsvp == NULL) {
1840 		if (error == 0)
1841 			error = ENOENT;
1842 		goto bad;
1843 	}
1844 
1845 	mi->mi_curr_serv = svp = firstsvp;
1846 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
1847 	ASSERT((mi->mi_curr_serv->sv_flags & SV4_NOTINUSE) == 0);
1848 	fh.nfs_fh4_len = svp->sv_fhandle.fh_len;
1849 	fh.nfs_fh4_val = svp->sv_fhandle.fh_buf;
1850 	mi->mi_rootfh = sfh4_get(&fh, mi);
1851 	fh.nfs_fh4_len = svp->sv_pfhandle.fh_len;
1852 	fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf;
1853 	mi->mi_srvparentfh = sfh4_get(&fh, mi);
1854 	nfs_rw_exit(&svp->sv_lock);
1855 
1856 	/*
1857 	 * Make the root vnode without attributes.
1858 	 */
1859 	mfname = mi->mi_fname;
1860 	fn_hold(mfname);
1861 	rtvp = makenfs4node_by_fh(mi->mi_rootfh, NULL,
1862 	    &mfname, NULL, mi, cr, gethrtime());
1863 	rtvp->v_type = vtype;
1864 
1865 	mi->mi_curread = mi->mi_tsize;
1866 	mi->mi_curwrite = mi->mi_stsize;
1867 
1868 	/*
1869 	 * Start the manager thread responsible for handling async worker
1870 	 * threads.
1871 	 */
1872 	VFS_HOLD(vfsp);	/* add reference for thread */
1873 	mi->mi_manager_thread = zthread_create(NULL, 0, nfs4_async_manager,
1874 					vfsp, 0, minclsyspri);
1875 	ASSERT(mi->mi_manager_thread != NULL);
1876 	/*
1877 	 * Create the thread that handles over-the-wire calls for
1878 	 * VOP_INACTIVE.
1879 	 * This needs to happen after the manager thread is created.
1880 	 */
1881 	mi->mi_inactive_thread = zthread_create(NULL, 0, nfs4_inactive_thread,
1882 					mi, 0, minclsyspri);
1883 	ASSERT(mi->mi_inactive_thread != NULL);
1884 
1885 	/* If we didn't get a type, get one now */
1886 	if (rtvp->v_type == VNON) {
1887 		va.va_mask = AT_TYPE;
1888 		error = nfs4getattr(rtvp, &va, tcr);
1889 		if (error)
1890 			goto bad;
1891 		rtvp->v_type = va.va_type;
1892 	}
1893 
1894 	mi->mi_type = rtvp->v_type;
1895 
1896 	mutex_enter(&mi->mi_lock);
1897 	mi->mi_flags &= ~MI4_MOUNTING;
1898 	mutex_exit(&mi->mi_lock);
1899 
1900 	*rtvpp = rtvp;
1901 	if (lcr != NULL)
1902 		crfree(lcr);
1903 
1904 	return (0);
1905 bad:
1906 	/*
1907 	 * An error occurred somewhere, need to clean up...
1908 	 *
1909 	 * XXX Should not svp be cleaned too?
1910 	 */
1911 	if (lcr != NULL)
1912 		crfree(lcr);
1913 	if (rtvp != NULL) {
1914 		/*
1915 		 * We need to release our reference to the root vnode and
1916 		 * destroy the mntinfo4 struct that we just created.
1917 		 */
1918 		rp = VTOR4(rtvp);
1919 		if (rp->r_flags & R4HASHED)
1920 			rp4_rmhash(rp);
1921 		VN_RELE(rtvp);
1922 	}
1923 	nfs4_async_stop(vfsp);
1924 	nfs4_async_manager_stop(vfsp);
1925 	if (mi->mi_io_kstats) {
1926 		kstat_delete(mi->mi_io_kstats);
1927 		mi->mi_io_kstats = NULL;
1928 	}
1929 	if (mi->mi_ro_kstats) {
1930 		kstat_delete(mi->mi_ro_kstats);
1931 		mi->mi_ro_kstats = NULL;
1932 	}
1933 	if (mi->mi_recov_ksp) {
1934 		kstat_delete(mi->mi_recov_ksp);
1935 		mi->mi_recov_ksp = NULL;
1936 	}
1937 	nfs_free_mi4(mi);
1938 	*rtvpp = NULL;
1939 	return (error);
1940 }
1941 
1942 /*
1943  * vfs operations
1944  */
1945 static int
1946 nfs4_unmount(vfs_t *vfsp, int flag, cred_t *cr)
1947 {
1948 	mntinfo4_t *mi;
1949 	ushort_t omax;
1950 
1951 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
1952 		return (EPERM);
1953 
1954 	mi = VFTOMI4(vfsp);
1955 
1956 	if (flag & MS_FORCE) {
1957 		vfsp->vfs_flag |= VFS_UNMOUNTED;
1958 		if (nfs_zone() != mi->mi_zone) {
1959 			/*
1960 			 * If the request is coming from the wrong zone,
1961 			 * we don't want to create any new threads, and
1962 			 * performance is not a concern.  Do everything
1963 			 * inline.
1964 			 */
1965 			NFS4_DEBUG(nfs4_client_zone_debug, (CE_NOTE,
1966 			    "nfs4_unmount x-zone forced unmount of vfs %p\n",
1967 			    (void *)vfsp));
1968 			nfs4_free_mount(vfsp, cr);
1969 		} else {
1970 			/*
1971 			 * Free data structures asynchronously, to avoid
1972 			 * blocking the current thread (for performance
1973 			 * reasons only).
1974 			 */
1975 			async_free_mount(vfsp, cr);
1976 		}
1977 		return (0);
1978 	}
1979 	/*
1980 	 * Wait until all asynchronous putpage operations on
1981 	 * this file system are complete before flushing rnodes
1982 	 * from the cache.
1983 	 */
1984 	omax = mi->mi_max_threads;
1985 	if (nfs4_async_stop_sig(vfsp)) {
1986 		return (EINTR);
1987 	}
1988 	r4flush(vfsp, cr);
1989 	/*
1990 	 * If there are any active vnodes on this file system,
1991 	 * then the file system is busy and can't be umounted.
1992 	 */
1993 	if (check_rtable4(vfsp)) {
1994 		mutex_enter(&mi->mi_async_lock);
1995 		mi->mi_max_threads = omax;
1996 		mutex_exit(&mi->mi_async_lock);
1997 		return (EBUSY);
1998 	}
1999 	/*
2000 	 * The unmount can't fail from now on, and there are no active
2001 	 * files that could require over-the-wire calls to the server,
2002 	 * so stop the async manager and the inactive thread.
2003 	 */
2004 	nfs4_async_manager_stop(vfsp);
2005 	/*
2006 	 * Destroy all rnodes belonging to this file system from the
2007 	 * rnode hash queues and purge any resources allocated to
2008 	 * them.
2009 	 */
2010 	destroy_rtable4(vfsp, cr);
2011 	vfsp->vfs_flag |= VFS_UNMOUNTED;
2012 	nfs4_remove_mi_from_server(mi, NULL);
2013 	if (mi->mi_io_kstats) {
2014 		kstat_delete(mi->mi_io_kstats);
2015 		mi->mi_io_kstats = NULL;
2016 	}
2017 	if (mi->mi_ro_kstats) {
2018 		kstat_delete(mi->mi_ro_kstats);
2019 		mi->mi_ro_kstats = NULL;
2020 	}
2021 	if (mi->mi_recov_ksp) {
2022 		kstat_delete(mi->mi_recov_ksp);
2023 		mi->mi_recov_ksp = NULL;
2024 	}
2025 	return (0);
2026 }
2027 
2028 /*
2029  * find root of nfs
2030  */
2031 static int
2032 nfs4_root(vfs_t *vfsp, vnode_t **vpp)
2033 {
2034 	mntinfo4_t *mi;
2035 	vnode_t *vp;
2036 	nfs4_fname_t *mfname;
2037 	servinfo4_t *svp;
2038 
2039 	mi = VFTOMI4(vfsp);
2040 
2041 	if (nfs_zone() != mi->mi_zone)
2042 		return (EPERM);
2043 
2044 	svp = mi->mi_curr_serv;
2045 	if (svp) {
2046 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
2047 		if (svp->sv_flags & SV4_ROOT_STALE) {
2048 			nfs_rw_exit(&svp->sv_lock);
2049 
2050 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
2051 			if (svp->sv_flags & SV4_ROOT_STALE) {
2052 				svp->sv_flags &= ~SV4_ROOT_STALE;
2053 				nfs_rw_exit(&svp->sv_lock);
2054 				return (ENOENT);
2055 			}
2056 			nfs_rw_exit(&svp->sv_lock);
2057 		} else
2058 			nfs_rw_exit(&svp->sv_lock);
2059 	}
2060 
2061 	mfname = mi->mi_fname;
2062 	fn_hold(mfname);
2063 	vp = makenfs4node_by_fh(mi->mi_rootfh, NULL, &mfname, NULL,
2064 	    VFTOMI4(vfsp), CRED(), gethrtime());
2065 
2066 	if (VTOR4(vp)->r_flags & R4STALE) {
2067 		VN_RELE(vp);
2068 		return (ENOENT);
2069 	}
2070 
2071 	ASSERT(vp->v_type == VNON || vp->v_type == mi->mi_type);
2072 
2073 	vp->v_type = mi->mi_type;
2074 
2075 	*vpp = vp;
2076 
2077 	return (0);
2078 }
2079 
2080 static int
2081 nfs4_statfs_otw(vnode_t *vp, struct statvfs64 *sbp, cred_t *cr)
2082 {
2083 	int error;
2084 	nfs4_ga_res_t gar;
2085 	nfs4_ga_ext_res_t ger;
2086 
2087 	gar.n4g_ext_res = &ger;
2088 
2089 	if (error = nfs4_attr_otw(vp, TAG_FSINFO, &gar,
2090 	    NFS4_STATFS_ATTR_MASK, cr))
2091 		return (error);
2092 
2093 	*sbp = gar.n4g_ext_res->n4g_sb;
2094 
2095 	return (0);
2096 }
2097 
2098 /*
2099  * Get file system statistics.
2100  */
2101 static int
2102 nfs4_statvfs(vfs_t *vfsp, struct statvfs64 *sbp)
2103 {
2104 	int error;
2105 	vnode_t *vp;
2106 	cred_t *cr;
2107 
2108 	error = nfs4_root(vfsp, &vp);
2109 	if (error)
2110 		return (error);
2111 
2112 	cr = CRED();
2113 
2114 	error = nfs4_statfs_otw(vp, sbp, cr);
2115 	if (!error) {
2116 		(void) strncpy(sbp->f_basetype,
2117 			vfssw[vfsp->vfs_fstype].vsw_name, FSTYPSZ);
2118 		sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
2119 	} else {
2120 		nfs4_purge_stale_fh(error, vp, cr);
2121 	}
2122 
2123 	VN_RELE(vp);
2124 
2125 	return (error);
2126 }
2127 
2128 static kmutex_t nfs4_syncbusy;
2129 
2130 /*
2131  * Flush dirty nfs files for file system vfsp.
2132  * If vfsp == NULL, all nfs files are flushed.
2133  *
2134  * SYNC_CLOSE in flag is passed to us to
2135  * indicate that we are shutting down and or
2136  * rebooting.
2137  */
2138 static int
2139 nfs4_sync(vfs_t *vfsp, short flag, cred_t *cr)
2140 {
2141 	/*
2142 	 * Cross-zone calls are OK here, since this translates to a
2143 	 * VOP_PUTPAGE(B_ASYNC), which gets picked up by the right zone.
2144 	 */
2145 	if (!(flag & SYNC_ATTR) && mutex_tryenter(&nfs4_syncbusy) != 0) {
2146 		r4flush(vfsp, cr);
2147 		mutex_exit(&nfs4_syncbusy);
2148 	}
2149 
2150 	/*
2151 	 * if SYNC_CLOSE is set then we know that
2152 	 * the system is rebooting, mark the mntinfo
2153 	 * for later examination.
2154 	 */
2155 	if (vfsp && (flag & SYNC_CLOSE)) {
2156 		mntinfo4_t *mi;
2157 
2158 		mi = VFTOMI4(vfsp);
2159 		if (!(mi->mi_flags & MI4_SHUTDOWN)) {
2160 			mutex_enter(&mi->mi_lock);
2161 			mi->mi_flags |= MI4_SHUTDOWN;
2162 			mutex_exit(&mi->mi_lock);
2163 		}
2164 	}
2165 	return (0);
2166 }
2167 
2168 /*
2169  * vget is difficult, if not impossible, to support in v4 because we don't
2170  * know the parent directory or name, which makes it impossible to create a
2171  * useful shadow vnode.  And we need the shadow vnode for things like
2172  * OPEN.
2173  */
2174 
2175 /* ARGSUSED */
2176 /*
2177  * XXX Check nfs4_vget_pseudo() for dependency.
2178  */
2179 static int
2180 nfs4_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
2181 {
2182 	return (EREMOTE);
2183 }
2184 
2185 /*
2186  * nfs4_mountroot get called in the case where we are diskless booting.  All
2187  * we need from here is the ability to get the server info and from there we
2188  * can simply call nfs4_rootvp.
2189  */
2190 /* ARGSUSED */
2191 static int
2192 nfs4_mountroot(vfs_t *vfsp, whymountroot_t why)
2193 {
2194 	vnode_t *rtvp;
2195 	char root_hostname[SYS_NMLN+1];
2196 	struct servinfo4 *svp;
2197 	int error;
2198 	int vfsflags;
2199 	size_t size;
2200 	char *root_path;
2201 	struct pathname pn;
2202 	char *name;
2203 	cred_t *cr;
2204 	mntinfo4_t *mi;
2205 	struct nfs_args args;		/* nfs mount arguments */
2206 	static char token[10];
2207 	nfs4_error_t n4e;
2208 
2209 	bzero(&args, sizeof (args));
2210 
2211 	/* do this BEFORE getfile which causes xid stamps to be initialized */
2212 	clkset(-1L);		/* hack for now - until we get time svc? */
2213 
2214 	if (why == ROOT_REMOUNT) {
2215 		/*
2216 		 * Shouldn't happen.
2217 		 */
2218 		panic("nfs4_mountroot: why == ROOT_REMOUNT");
2219 	}
2220 
2221 	if (why == ROOT_UNMOUNT) {
2222 		/*
2223 		 * Nothing to do for NFS.
2224 		 */
2225 		return (0);
2226 	}
2227 
2228 	/*
2229 	 * why == ROOT_INIT
2230 	 */
2231 
2232 	name = token;
2233 	*name = 0;
2234 	(void) getfsname("root", name, sizeof (token));
2235 
2236 	pn_alloc(&pn);
2237 	root_path = pn.pn_path;
2238 
2239 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
2240 	nfs_rw_init(&svp->sv_lock, NULL, RW_DEFAULT, NULL);
2241 	svp->sv_knconf = kmem_zalloc(sizeof (*svp->sv_knconf), KM_SLEEP);
2242 	svp->sv_knconf->knc_protofmly = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
2243 	svp->sv_knconf->knc_proto = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
2244 
2245 	/*
2246 	 * Get server address
2247 	 * Get the root path
2248 	 * Get server's transport
2249 	 * Get server's hostname
2250 	 * Get options
2251 	 */
2252 	args.addr = &svp->sv_addr;
2253 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
2254 	args.fh = (char *)&svp->sv_fhandle;
2255 	args.knconf = svp->sv_knconf;
2256 	args.hostname = root_hostname;
2257 	vfsflags = 0;
2258 	if (error = mount_root(*name ? name : "root", root_path, NFS_V4,
2259 	    &args, &vfsflags)) {
2260 		if (error == EPROTONOSUPPORT)
2261 			nfs_cmn_err(error, CE_WARN, "nfs4_mountroot: "
2262 			    "mount_root failed: server doesn't support NFS V4");
2263 		else
2264 			nfs_cmn_err(error, CE_WARN,
2265 			    "nfs4_mountroot: mount_root failed: %m");
2266 		nfs_rw_exit(&svp->sv_lock);
2267 		sv4_free(svp);
2268 		pn_free(&pn);
2269 		return (error);
2270 	}
2271 	nfs_rw_exit(&svp->sv_lock);
2272 	svp->sv_hostnamelen = (int)(strlen(root_hostname) + 1);
2273 	svp->sv_hostname = kmem_alloc(svp->sv_hostnamelen, KM_SLEEP);
2274 	(void) strcpy(svp->sv_hostname, root_hostname);
2275 
2276 	svp->sv_pathlen = (int)(strlen(root_path) + 1);
2277 	svp->sv_path = kmem_alloc(svp->sv_pathlen, KM_SLEEP);
2278 	(void) strcpy(svp->sv_path, root_path);
2279 
2280 	/*
2281 	 * Force root partition to always be mounted with AUTH_UNIX for now
2282 	 */
2283 	svp->sv_secdata = kmem_alloc(sizeof (*svp->sv_secdata), KM_SLEEP);
2284 	svp->sv_secdata->secmod = AUTH_UNIX;
2285 	svp->sv_secdata->rpcflavor = AUTH_UNIX;
2286 	svp->sv_secdata->data = NULL;
2287 
2288 	cr = crgetcred();
2289 	rtvp = NULL;
2290 
2291 	error = nfs4rootvp(&rtvp, vfsp, svp, args.flags, cr, global_zone);
2292 
2293 	if (error) {
2294 		crfree(cr);
2295 		pn_free(&pn);
2296 		goto errout;
2297 	}
2298 
2299 	mi = VTOMI4(rtvp);
2300 
2301 	/*
2302 	 * Send client id to the server, if necessary
2303 	 */
2304 	nfs4_error_zinit(&n4e);
2305 	nfs4setclientid(mi, cr, FALSE, &n4e);
2306 	error = n4e.error;
2307 
2308 	crfree(cr);
2309 
2310 	if (error) {
2311 		pn_free(&pn);
2312 		goto errout;
2313 	}
2314 
2315 	error = nfs4_setopts(rtvp, DATAMODEL_NATIVE, &args);
2316 	if (error) {
2317 		nfs_cmn_err(error, CE_WARN,
2318 		    "nfs4_mountroot: invalid root mount options");
2319 		pn_free(&pn);
2320 		goto errout;
2321 	}
2322 
2323 	(void) vfs_lock_wait(vfsp);
2324 	vfs_add(NULL, vfsp, vfsflags);
2325 	vfs_unlock(vfsp);
2326 
2327 	size = strlen(svp->sv_hostname);
2328 	(void) strcpy(rootfs.bo_name, svp->sv_hostname);
2329 	rootfs.bo_name[size] = ':';
2330 	(void) strcpy(&rootfs.bo_name[size + 1], root_path);
2331 
2332 	pn_free(&pn);
2333 
2334 errout:
2335 	if (error) {
2336 		sv4_free(svp);
2337 		nfs4_async_stop(vfsp);
2338 		nfs4_async_manager_stop(vfsp);
2339 	}
2340 
2341 	if (rtvp != NULL)
2342 		VN_RELE(rtvp);
2343 
2344 	return (error);
2345 }
2346 
2347 /*
2348  * Initialization routine for VFS routines.  Should only be called once
2349  */
2350 int
2351 nfs4_vfsinit(void)
2352 {
2353 	mutex_init(&nfs4_syncbusy, NULL, MUTEX_DEFAULT, NULL);
2354 	nfs4setclientid_init();
2355 	return (0);
2356 }
2357 
2358 void
2359 nfs4_vfsfini(void)
2360 {
2361 	nfs4setclientid_fini();
2362 	mutex_destroy(&nfs4_syncbusy);
2363 }
2364 
2365 void
2366 nfs4_freevfs(vfs_t *vfsp)
2367 {
2368 	mntinfo4_t *mi;
2369 	servinfo4_t *svp;
2370 
2371 	/* free up the resources */
2372 	mi = VFTOMI4(vfsp);
2373 	svp = mi->mi_servers;
2374 	mi->mi_servers = mi->mi_curr_serv = NULL;
2375 	sv4_free(svp);
2376 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "nfs4_freevfs: "
2377 		"free mi %p", (void *)mi));
2378 
2379 	/*
2380 	 * By this time we should have already deleted the
2381 	 * mi kstats in the unmount code. If they are still around
2382 	 * somethings wrong
2383 	 */
2384 	ASSERT(mi->mi_io_kstats == NULL);
2385 
2386 	nfs_free_mi4(mi);
2387 }
2388 
2389 /*
2390  * Client side SETCLIENTID and SETCLIENTID_CONFIRM
2391  */
2392 struct nfs4_server nfs4_server_lst =
2393 	{ &nfs4_server_lst, &nfs4_server_lst };
2394 
2395 kmutex_t nfs4_server_lst_lock;
2396 
2397 static void
2398 nfs4setclientid_init(void)
2399 {
2400 	mutex_init(&nfs4_server_lst_lock, NULL, MUTEX_DEFAULT, NULL);
2401 }
2402 
2403 static void
2404 nfs4setclientid_fini(void)
2405 {
2406 	mutex_destroy(&nfs4_server_lst_lock);
2407 }
2408 
2409 int nfs4_retry_sclid_delay = NFS4_RETRY_SCLID_DELAY;
2410 int nfs4_num_sclid_retries = NFS4_NUM_SCLID_RETRIES;
2411 
2412 /*
2413  * Set the clientid for the server for "mi".  No-op if the clientid is
2414  * already set.
2415  *
2416  * The recovery boolean should be set to TRUE if this function was called
2417  * by the recovery code, and FALSE otherwise.  This is used to determine
2418  * if we need to call nfs4_start/end_op as well as grab the mi_recovlock
2419  * for adding a mntinfo4_t to a nfs4_server_t.
2420  *
2421  * Error is returned via 'n4ep'.  If there was a 'n4ep->stat' error, then
2422  * 'n4ep->error' is set to geterrno4(n4ep->stat).
2423  */
2424 void
2425 nfs4setclientid(mntinfo4_t *mi, cred_t *cr, bool_t recovery, nfs4_error_t *n4ep)
2426 {
2427 	struct nfs4_server *np;
2428 	struct servinfo4 *svp = mi->mi_curr_serv;
2429 	nfs4_recov_state_t recov_state;
2430 	int num_retries = 0;
2431 	bool_t retry = FALSE;
2432 	cred_t *lcr = NULL;
2433 	int retry_inuse = 1; /* only retry once on NFS4ERR_CLID_INUSE */
2434 	time_t lease_time = 0;
2435 
2436 	recov_state.rs_flags = 0;
2437 	recov_state.rs_num_retry_despite_err = 0;
2438 	ASSERT(n4ep != NULL);
2439 
2440 recov_retry:
2441 	nfs4_error_zinit(n4ep);
2442 	if (!recovery)
2443 		(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, 0);
2444 
2445 	/* This locks np if it is found */
2446 	np = servinfo4_to_nfs4_server(svp);
2447 	ASSERT(np == NULL || MUTEX_HELD(&np->s_lock));
2448 
2449 	/*
2450 	 * If we find the server already in the list, then just
2451 	 * return, we've already done SETCLIENTID to that server
2452 	 */
2453 
2454 	if (np && (np->s_flags & N4S_CLIENTID_SET)) {
2455 		/*
2456 		 * XXX - more is needed here.  SETCLIENTID may not
2457 		 * be completed.  A VFS lock may prevent multiple
2458 		 * mounts and provide needed serialization.
2459 		 */
2460 		/* add mi to np's mntinfo4_list */
2461 		nfs4_add_mi_to_server(np, mi);
2462 		if (!recovery)
2463 			nfs_rw_exit(&mi->mi_recovlock);
2464 		mutex_exit(&np->s_lock);
2465 		nfs4_server_rele(np);
2466 		return;
2467 	}
2468 
2469 	/*
2470 	 * Drop the mi_recovlock since nfs4_start_op will
2471 	 * acquire it again for us.
2472 	 */
2473 	if (!recovery)
2474 		nfs_rw_exit(&mi->mi_recovlock);
2475 
2476 	if (!np)
2477 		np = new_nfs4_server(svp, cr);
2478 	else
2479 		mutex_exit(&np->s_lock);
2480 
2481 	if (!recovery) {
2482 		n4ep->error = nfs4_start_op(mi, NULL, NULL, &recov_state);
2483 		if (n4ep->error) {
2484 			nfs4_server_rele(np);
2485 			return;
2486 		}
2487 	}
2488 
2489 	/*
2490 	 * Will potentially add np to global list, which transfers
2491 	 * ownership of the reference to the list.
2492 	 */
2493 	mutex_enter(&nfs4_server_lst_lock);
2494 	mutex_enter(&np->s_lock);
2495 
2496 	/*
2497 	 * Reset the N4S_CB_PINGED flag. This is used to
2498 	 * indicate if we have received a CB_NULL from the
2499 	 * server. Also we reset the waiter flag.
2500 	 */
2501 	np->s_flags &= ~(N4S_CB_PINGED | N4S_CB_WAITER);
2502 
2503 	if (np->s_flags & N4S_CLIENTID_SET) {
2504 		/* XXX copied/pasted from above */
2505 		/*
2506 		 * XXX - more is needed here.  SETCLIENTID may not
2507 		 * be completed.  A VFS lock may prevent multiple
2508 		 * mounts and provide needed serialization.
2509 		 */
2510 		/* add mi to np's mntinfo4_list */
2511 		nfs4_add_mi_to_server(np, mi);
2512 		mutex_exit(&np->s_lock);
2513 		mutex_exit(&nfs4_server_lst_lock);
2514 		nfs4_server_rele(np);
2515 		if (!recovery)
2516 			nfs4_end_op(mi, NULL, NULL, &recov_state, recovery);
2517 		return;
2518 	}
2519 
2520 	nfs4setclientid_otw(mi, svp, cr, np, n4ep, &retry_inuse);
2521 
2522 	if (n4ep->error == EACCES) {
2523 		/*
2524 		 * If the uid is set then set the creds for secure mounts
2525 		 * by proxy processes such as automountd.
2526 		 */
2527 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
2528 		if (svp->sv_secdata->uid != 0) {
2529 			lcr = crdup(cr);
2530 			(void) crsetugid(lcr, svp->sv_secdata->uid,
2531 			    crgetgid(cr));
2532 			crfree(np->s_cred);
2533 			np->s_cred = lcr;
2534 		}
2535 		nfs_rw_exit(&svp->sv_lock);
2536 
2537 		if (lcr != NULL)
2538 			nfs4setclientid_otw(mi, svp, lcr, np, n4ep,
2539 				&retry_inuse);
2540 	}
2541 	lease_time = np->s_lease_time;
2542 	mutex_exit(&np->s_lock);
2543 	mutex_exit(&nfs4_server_lst_lock);
2544 
2545 	if (n4ep->error != 0 || n4ep->stat != NFS4_OK) {
2546 		/*
2547 		 * Start recovery if failover is a possibility.  If
2548 		 * invoked by the recovery thread itself, then just
2549 		 * return and let it handle the failover first.  NB:
2550 		 * recovery is not allowed if the mount is in progress
2551 		 * since the infrastructure is not sufficiently setup
2552 		 * to allow it.  Just return the error (after suitable
2553 		 * retries).
2554 		 */
2555 		if (FAILOVER_MOUNT4(mi) && nfs4_try_failover(n4ep)) {
2556 			(void) nfs4_start_recovery(n4ep, mi, NULL,
2557 				    NULL, NULL, NULL, OP_SETCLIENTID, NULL);
2558 			/*
2559 			 * Don't retry here, just return and let
2560 			 * recovery take over.
2561 			 */
2562 			if (recovery)
2563 				retry = FALSE;
2564 		} else if (nfs4_rpc_retry_error(n4ep->error) ||
2565 			    n4ep->stat == NFS4ERR_RESOURCE ||
2566 			    n4ep->stat == NFS4ERR_STALE_CLIENTID) {
2567 
2568 				retry = TRUE;
2569 				/*
2570 				 * Always retry if in recovery or once had
2571 				 * contact with the server (but now it's
2572 				 * overloaded).
2573 				 */
2574 				if (recovery == TRUE ||
2575 				    n4ep->error == ETIMEDOUT ||
2576 				    n4ep->error == ECONNRESET)
2577 					num_retries = 0;
2578 		} else if (retry_inuse && n4ep->error == 0 &&
2579 			    n4ep->stat == NFS4ERR_CLID_INUSE) {
2580 				retry = TRUE;
2581 				num_retries = 0;
2582 		}
2583 	}
2584 
2585 	if (!recovery)
2586 		nfs4_end_op(mi, NULL, NULL, &recov_state, recovery);
2587 	nfs4_server_rele(np);
2588 
2589 	if (retry && num_retries++ < nfs4_num_sclid_retries) {
2590 		if (retry_inuse) {
2591 			delay(SEC_TO_TICK(lease_time + nfs4_retry_sclid_delay));
2592 			retry_inuse = 0;
2593 		} else
2594 			delay(SEC_TO_TICK(nfs4_retry_sclid_delay));
2595 		goto recov_retry;
2596 	}
2597 
2598 	if (n4ep->error == 0)
2599 		n4ep->error = geterrno4(n4ep->stat);
2600 }
2601 
2602 int nfs4setclientid_otw_debug = 0;
2603 
2604 /*
2605  * This assumes np is locked down.
2606  * This function handles the recovery of STALE_CLIENTID for SETCLIENTID_CONFRIM,
2607  * but nothing else; the calling function must be designed to handle those
2608  * other errors.
2609  */
2610 static void
2611 nfs4setclientid_otw(mntinfo4_t *mi, struct servinfo4 *svp,  cred_t *cr,
2612 	struct nfs4_server *np, nfs4_error_t *ep, int *retry_inusep)
2613 {
2614 	COMPOUND4args_clnt args;
2615 	COMPOUND4res_clnt res;
2616 	nfs_argop4 argop[3];
2617 	SETCLIENTID4args *s_args;
2618 	SETCLIENTID4resok *s_resok;
2619 	int doqueue = 1;
2620 	nfs4_ga_res_t *garp = NULL;
2621 	timespec_t prop_time, after_time;
2622 	verifier4 verf;
2623 	clientid4 tmp_clientid;
2624 
2625 	ASSERT(MUTEX_HELD(&np->s_lock));
2626 
2627 	args.ctag = TAG_SETCLIENTID;
2628 
2629 	args.array = argop;
2630 	args.array_len = 3;
2631 
2632 	/* PUTROOTFH */
2633 	argop[0].argop = OP_PUTROOTFH;
2634 
2635 	/* GETATTR */
2636 	argop[1].argop = OP_GETATTR;
2637 	argop[1].nfs_argop4_u.opgetattr.attr_request = FATTR4_LEASE_TIME_MASK;
2638 	argop[1].nfs_argop4_u.opgetattr.mi = mi;
2639 
2640 	/* SETCLIENTID */
2641 	argop[2].argop = OP_SETCLIENTID;
2642 
2643 	s_args = &argop[2].nfs_argop4_u.opsetclientid;
2644 
2645 	s_args->client.verifier = np->clidtosend.verifier;
2646 	s_args->client.id_len = np->clidtosend.id_len;
2647 	ASSERT(s_args->client.id_len <= NFS4_OPAQUE_LIMIT);
2648 	s_args->client.id_val = np->clidtosend.id_val;
2649 
2650 	/*
2651 	 * Callback needs to happen on non-RDMA transport
2652 	 * Check if we have saved the original knetconfig
2653 	 * if so, use that instead.
2654 	 */
2655 	if (svp->sv_origknconf != NULL)
2656 		nfs4_cb_args(np, svp->sv_origknconf, s_args);
2657 	else
2658 		nfs4_cb_args(np, svp->sv_knconf, s_args);
2659 
2660 	rfs4call(mi, &args, &res, cr, &doqueue, RFSCALL_SOFT, ep);
2661 
2662 	if (ep->error)
2663 		return;
2664 
2665 	/* getattr lease_time res */
2666 	if (res.array_len >= 2) {
2667 		garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res;
2668 
2669 #ifndef _LP64
2670 		/*
2671 		 * The 32 bit client cannot handle a lease time greater than
2672 		 * (INT32_MAX/1000000).  This is due to the use of the
2673 		 * lease_time in calls to drv_usectohz() in
2674 		 * nfs4_renew_lease_thread().  The problem is that
2675 		 * drv_usectohz() takes a time_t (which is just a long = 4
2676 		 * bytes) as its parameter.  The lease_time is multiplied by
2677 		 * 1000000 to convert seconds to usecs for the parameter.  If
2678 		 * a number bigger than (INT32_MAX/1000000) is used then we
2679 		 * overflow on the 32bit client.
2680 		 */
2681 		if (garp->n4g_ext_res->n4g_leasetime > (INT32_MAX/1000000)) {
2682 			garp->n4g_ext_res->n4g_leasetime = INT32_MAX/1000000;
2683 		}
2684 #endif
2685 
2686 		np->s_lease_time = garp->n4g_ext_res->n4g_leasetime;
2687 
2688 		/*
2689 		 * Keep track of the lease period for the mi's
2690 		 * mi_msg_list.  We need an appropiate time
2691 		 * bound to associate past facts with a current
2692 		 * event.  The lease period is perfect for this.
2693 		 */
2694 		mutex_enter(&mi->mi_msg_list_lock);
2695 		mi->mi_lease_period = np->s_lease_time;
2696 		mutex_exit(&mi->mi_msg_list_lock);
2697 	}
2698 
2699 
2700 	if (res.status == NFS4ERR_CLID_INUSE) {
2701 		clientaddr4 *clid_inuse;
2702 
2703 		if (!(*retry_inusep)) {
2704 			clid_inuse = &res.array->nfs_resop4_u.
2705 				opsetclientid.SETCLIENTID4res_u.client_using;
2706 
2707 			zcmn_err(mi->mi_zone->zone_id, CE_NOTE,
2708 			    "NFS4 mount (SETCLIENTID failed)."
2709 			    "  nfs4_client_id.id is in"
2710 			    "use already by: r_netid<%s> r_addr<%s>",
2711 			    clid_inuse->r_netid, clid_inuse->r_addr);
2712 		}
2713 
2714 		/*
2715 		 * XXX - The client should be more robust in its
2716 		 * handling of clientid in use errors (regen another
2717 		 * clientid and try again?)
2718 		 */
2719 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2720 		return;
2721 	}
2722 
2723 	if (res.status) {
2724 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2725 		return;
2726 	}
2727 
2728 	s_resok = &res.array[2].nfs_resop4_u.
2729 		opsetclientid.SETCLIENTID4res_u.resok4;
2730 
2731 	tmp_clientid = s_resok->clientid;
2732 
2733 	verf = s_resok->setclientid_confirm;
2734 
2735 #ifdef	DEBUG
2736 	if (nfs4setclientid_otw_debug) {
2737 		union {
2738 			clientid4	clientid;
2739 			int		foo[2];
2740 		} cid;
2741 
2742 		cid.clientid = s_resok->clientid;
2743 
2744 		zcmn_err(mi->mi_zone->zone_id, CE_NOTE,
2745 		"nfs4setclientid_otw: OK, clientid = %x,%x, "
2746 		"verifier = %" PRIx64 "\n", cid.foo[0], cid.foo[1], verf);
2747 	}
2748 #endif
2749 
2750 	(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2751 
2752 	/* Confirm the client id and get the lease_time attribute */
2753 
2754 	args.ctag = TAG_SETCLIENTID_CF;
2755 
2756 	args.array = argop;
2757 	args.array_len = 1;
2758 
2759 	argop[0].argop = OP_SETCLIENTID_CONFIRM;
2760 
2761 	argop[0].nfs_argop4_u.opsetclientid_confirm.clientid = tmp_clientid;
2762 	argop[0].nfs_argop4_u.opsetclientid_confirm.setclientid_confirm = verf;
2763 
2764 	/* used to figure out RTT for np */
2765 	gethrestime(&prop_time);
2766 
2767 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "nfs4setlientid_otw: "
2768 		"start time: %ld sec %ld nsec", prop_time.tv_sec,
2769 		prop_time.tv_nsec));
2770 
2771 	rfs4call(mi, &args, &res, cr, &doqueue, 0, ep);
2772 
2773 	gethrestime(&after_time);
2774 	np->propagation_delay.tv_sec =
2775 		MAX(1, after_time.tv_sec - prop_time.tv_sec);
2776 
2777 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "nfs4setlcientid_otw: "
2778 		"finish time: %ld sec ", after_time.tv_sec));
2779 
2780 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "nfs4setclientid_otw: "
2781 		"propagation delay set to %ld sec",
2782 		np->propagation_delay.tv_sec));
2783 
2784 	if (ep->error)
2785 		return;
2786 
2787 	if (res.status == NFS4ERR_CLID_INUSE) {
2788 		clientaddr4 *clid_inuse;
2789 
2790 		if (!(*retry_inusep)) {
2791 			clid_inuse = &res.array->nfs_resop4_u.
2792 				opsetclientid.SETCLIENTID4res_u.client_using;
2793 
2794 			zcmn_err(mi->mi_zone->zone_id, CE_NOTE,
2795 			    "SETCLIENTID_CONFIRM failed.  "
2796 			    "nfs4_client_id.id is in use already by: "
2797 			    "r_netid<%s> r_addr<%s>",
2798 			    clid_inuse->r_netid, clid_inuse->r_addr);
2799 		}
2800 
2801 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2802 		return;
2803 	}
2804 
2805 	if (res.status) {
2806 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2807 		return;
2808 	}
2809 
2810 	if (!(np->s_flags & N4S_INSERTED)) {
2811 		ASSERT(MUTEX_HELD(&nfs4_server_lst_lock));
2812 		insque(np, &nfs4_server_lst);
2813 		ASSERT(MUTEX_HELD(&np->s_lock));
2814 		np->s_flags |= N4S_INSERTED;
2815 		np->s_refcnt++;		/* list gets a reference */
2816 	}
2817 
2818 	np->clientid = tmp_clientid;
2819 	np->s_flags |= N4S_CLIENTID_SET;
2820 
2821 	/* Add mi to np's mntinfo4 list */
2822 	nfs4_add_mi_to_server(np, mi);
2823 
2824 	if (np->lease_valid == NFS4_LEASE_NOT_STARTED) {
2825 		/*
2826 		 * Start lease management thread.
2827 		 * Keep trying until we succeed.
2828 		 */
2829 
2830 		np->s_refcnt++;		/* pass reference to thread */
2831 		(void) zthread_create(NULL, 0, nfs4_renew_lease_thread, np, 0,
2832 				    minclsyspri);
2833 	}
2834 
2835 	(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
2836 }
2837 
2838 /*
2839  * Add mi to sp's mntinfo4_list if it isn't already in the list.  Makes
2840  * mi's clientid the same as sp's.
2841  * Assumes sp is locked down.
2842  */
2843 void
2844 nfs4_add_mi_to_server(nfs4_server_t *sp, mntinfo4_t *mi)
2845 {
2846 	mntinfo4_t *tmi;
2847 	int in_list = 0;
2848 
2849 	ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) ||
2850 	    nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
2851 	ASSERT(sp != &nfs4_server_lst);
2852 	ASSERT(MUTEX_HELD(&sp->s_lock));
2853 
2854 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
2855 		"nfs4_add_mi_to_server: add mi %p to sp %p",
2856 		    (void*)mi, (void*)sp));
2857 
2858 	for (tmi = sp->mntinfo4_list;
2859 	    tmi != NULL;
2860 	    tmi = tmi->mi_clientid_next) {
2861 		if (tmi == mi) {
2862 			NFS4_DEBUG(nfs4_client_lease_debug,
2863 				(CE_NOTE,
2864 				"nfs4_add_mi_to_server: mi in list"));
2865 			in_list = 1;
2866 		}
2867 	}
2868 
2869 	/*
2870 	 * First put a hold on the mntinfo4's vfsp so that references via
2871 	 * mntinfo4_list will be valid.
2872 	 */
2873 	if (!in_list)
2874 		VFS_HOLD(mi->mi_vfsp);
2875 
2876 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "nfs4_add_mi_to_server: "
2877 		"hold vfs %p for mi: %p", (void*)mi->mi_vfsp, (void*)mi));
2878 
2879 	if (!in_list) {
2880 		if (sp->mntinfo4_list)
2881 			sp->mntinfo4_list->mi_clientid_prev = mi;
2882 		mi->mi_clientid_next = sp->mntinfo4_list;
2883 		sp->mntinfo4_list = mi;
2884 		mi->mi_srvsettime = gethrestime_sec();
2885 	}
2886 
2887 	/* set mi's clientid to that of sp's for later matching */
2888 	mi->mi_clientid = sp->clientid;
2889 
2890 	/*
2891 	 * Update the clientid for any other mi's belonging to sp.  This
2892 	 * must be done here while we hold sp->s_lock, so that
2893 	 * find_nfs4_server() continues to work.
2894 	 */
2895 
2896 	for (tmi = sp->mntinfo4_list;
2897 	    tmi != NULL;
2898 	    tmi = tmi->mi_clientid_next) {
2899 		if (tmi != mi) {
2900 			tmi->mi_clientid = sp->clientid;
2901 		}
2902 	}
2903 }
2904 
2905 /*
2906  * Remove the mi from sp's mntinfo4_list and release its reference.
2907  * Exception: if mi still has open files, flag it for later removal (when
2908  * all the files are closed).
2909  *
2910  * If this is the last mntinfo4 in sp's list then tell the lease renewal
2911  * thread to exit.
2912  */
2913 static void
2914 nfs4_remove_mi_from_server_nolock(mntinfo4_t *mi, nfs4_server_t *sp)
2915 {
2916 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
2917 		"nfs4_remove_mi_from_server_nolock: remove mi %p from sp %p",
2918 		(void*)mi, (void*)sp));
2919 
2920 	ASSERT(sp != NULL);
2921 	ASSERT(MUTEX_HELD(&sp->s_lock));
2922 	ASSERT(mi->mi_open_files >= 0);
2923 
2924 	/*
2925 	 * First make sure this mntinfo4 can be taken off of the list,
2926 	 * ie: it doesn't have any open files remaining.
2927 	 */
2928 	if (mi->mi_open_files > 0) {
2929 		NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
2930 			"nfs4_remove_mi_from_server_nolock: don't "
2931 			"remove mi since it still has files open"));
2932 
2933 		mutex_enter(&mi->mi_lock);
2934 		mi->mi_flags |= MI4_REMOVE_ON_LAST_CLOSE;
2935 		mutex_exit(&mi->mi_lock);
2936 		return;
2937 	}
2938 
2939 	remove_mi(sp, mi);
2940 
2941 	if (sp->mntinfo4_list == NULL) {
2942 		/* last fs unmounted, kill the thread */
2943 		NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
2944 			"remove_mi_from_nfs4_server_nolock: kill the thread"));
2945 		nfs4_mark_srv_dead(sp);
2946 	}
2947 }
2948 
2949 /*
2950  * Remove mi from sp's mntinfo4_list and release the vfs reference.
2951  */
2952 static void
2953 remove_mi(nfs4_server_t *sp, mntinfo4_t *mi)
2954 {
2955 	ASSERT(MUTEX_HELD(&sp->s_lock));
2956 
2957 	/*
2958 	 * We release a reference, and the caller must still have a
2959 	 * reference.
2960 	 */
2961 	ASSERT(mi->mi_vfsp->vfs_count >= 2);
2962 
2963 	if (mi->mi_clientid_prev) {
2964 		mi->mi_clientid_prev->mi_clientid_next = mi->mi_clientid_next;
2965 	} else {
2966 		/* This is the first mi in sp's mntinfo4_list */
2967 		/*
2968 		 * Make sure the first mntinfo4 in the list is the actual
2969 		 * mntinfo4 passed in.
2970 		 */
2971 		ASSERT(sp->mntinfo4_list == mi);
2972 
2973 		sp->mntinfo4_list = mi->mi_clientid_next;
2974 	}
2975 	if (mi->mi_clientid_next)
2976 		mi->mi_clientid_next->mi_clientid_prev = mi->mi_clientid_prev;
2977 
2978 	/* Now mark the mntinfo4's links as being removed */
2979 	mi->mi_clientid_prev = mi->mi_clientid_next = NULL;
2980 
2981 	VFS_RELE(mi->mi_vfsp);
2982 }
2983 
2984 /*
2985  * Free all the entries in sp's mntinfo4_list.
2986  */
2987 static void
2988 remove_all_mi(nfs4_server_t *sp)
2989 {
2990 	mntinfo4_t *mi;
2991 
2992 	ASSERT(MUTEX_HELD(&sp->s_lock));
2993 
2994 	while (sp->mntinfo4_list != NULL) {
2995 		mi = sp->mntinfo4_list;
2996 		/*
2997 		 * Grab a reference in case there is only one left (which
2998 		 * remove_mi() frees).
2999 		 */
3000 		VFS_HOLD(mi->mi_vfsp);
3001 		remove_mi(sp, mi);
3002 		VFS_RELE(mi->mi_vfsp);
3003 	}
3004 }
3005 
3006 /*
3007  * Remove the mi from sp's mntinfo4_list as above, and rele the vfs.
3008  *
3009  * This version can be called with a null nfs4_server_t arg,
3010  * and will either find the right one and handle locking, or
3011  * do nothing because the mi wasn't added to an sp's mntinfo4_list.
3012  */
3013 void
3014 nfs4_remove_mi_from_server(mntinfo4_t *mi, nfs4_server_t *esp)
3015 {
3016 	nfs4_server_t	*sp;
3017 
3018 	if (esp == NULL) {
3019 		(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, 0);
3020 		sp = find_nfs4_server_all(mi, 1);
3021 	} else
3022 		sp = esp;
3023 
3024 	if (sp != NULL)
3025 		nfs4_remove_mi_from_server_nolock(mi, sp);
3026 
3027 	/*
3028 	 * If we had a valid esp as input, the calling function will be
3029 	 * responsible for unlocking the esp nfs4_server.
3030 	 */
3031 	if (esp == NULL) {
3032 		if (sp != NULL)
3033 			mutex_exit(&sp->s_lock);
3034 		nfs_rw_exit(&mi->mi_recovlock);
3035 		if (sp != NULL)
3036 			nfs4_server_rele(sp);
3037 	}
3038 }
3039 
3040 /*
3041  * Return TRUE if the given server has any non-unmounted filesystems.
3042  */
3043 
3044 bool_t
3045 nfs4_fs_active(nfs4_server_t *sp)
3046 {
3047 	mntinfo4_t *mi;
3048 
3049 	ASSERT(MUTEX_HELD(&sp->s_lock));
3050 
3051 	for (mi = sp->mntinfo4_list; mi != NULL; mi = mi->mi_clientid_next) {
3052 		if (!(mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED))
3053 			return (TRUE);
3054 	}
3055 
3056 	return (FALSE);
3057 }
3058 
3059 /*
3060  * Mark sp as finished and notify any waiters.
3061  */
3062 
3063 void
3064 nfs4_mark_srv_dead(nfs4_server_t *sp)
3065 {
3066 	ASSERT(MUTEX_HELD(&sp->s_lock));
3067 
3068 	sp->s_thread_exit = NFS4_THREAD_EXIT;
3069 	cv_broadcast(&sp->cv_thread_exit);
3070 }
3071 
3072 /*
3073  * Create a new nfs4_server_t structure.
3074  * Returns new node unlocked and not in list, but with a reference count of
3075  * 1.
3076  */
3077 struct nfs4_server *
3078 new_nfs4_server(struct servinfo4 *svp, cred_t *cr)
3079 {
3080 	struct nfs4_server *np;
3081 	timespec_t tt;
3082 	union {
3083 		struct {
3084 			uint32_t sec;
3085 			uint32_t subsec;
3086 		} un_curtime;
3087 		verifier4	un_verifier;
3088 	} nfs4clientid_verifier;
3089 	char id_val[] = "Solaris: %s, NFSv4 kernel client";
3090 	int len;
3091 
3092 	np = kmem_zalloc(sizeof (struct nfs4_server), KM_SLEEP);
3093 	np->saddr.len = svp->sv_addr.len;
3094 	np->saddr.maxlen = svp->sv_addr.maxlen;
3095 	np->saddr.buf = kmem_alloc(svp->sv_addr.maxlen, KM_SLEEP);
3096 	bcopy(svp->sv_addr.buf, np->saddr.buf, svp->sv_addr.len);
3097 	np->s_refcnt = 1;
3098 
3099 	/*
3100 	 * Build the nfs_client_id4 for this server mount.  Ensure
3101 	 * the verifier is useful and that the identification is
3102 	 * somehow based on the server's address for the case of
3103 	 * multi-homed servers.
3104 	 */
3105 	nfs4clientid_verifier.un_verifier = 0;
3106 	gethrestime(&tt);
3107 	nfs4clientid_verifier.un_curtime.sec = (uint32_t)tt.tv_sec;
3108 	nfs4clientid_verifier.un_curtime.subsec = (uint32_t)tt.tv_nsec;
3109 	np->clidtosend.verifier = nfs4clientid_verifier.un_verifier;
3110 
3111 	/*
3112 	 * calculate the length of the opaque identifier.  Subtract 2
3113 	 * for the "%s" and add the traditional +1 for null
3114 	 * termination.
3115 	 */
3116 	len = strlen(id_val) - 2 + strlen(uts_nodename()) + 1;
3117 	np->clidtosend.id_len = len + np->saddr.maxlen;
3118 
3119 	np->clidtosend.id_val = kmem_alloc(np->clidtosend.id_len, KM_SLEEP);
3120 	(void) sprintf(np->clidtosend.id_val, id_val, uts_nodename());
3121 	bcopy(np->saddr.buf, &np->clidtosend.id_val[len], np->saddr.len);
3122 
3123 	np->s_flags = 0;
3124 	np->mntinfo4_list = NULL;
3125 	/* save cred for issuing rfs4calls inside the renew thread */
3126 	crhold(cr);
3127 	np->s_cred = cr;
3128 	cv_init(&np->cv_thread_exit, NULL, CV_DEFAULT, NULL);
3129 	mutex_init(&np->s_lock, NULL, MUTEX_DEFAULT, NULL);
3130 	nfs_rw_init(&np->s_recovlock, NULL, RW_DEFAULT, NULL);
3131 	list_create(&np->s_deleg_list, sizeof (rnode4_t),
3132 	    offsetof(rnode4_t, r_deleg_link));
3133 	np->s_thread_exit = 0;
3134 	np->state_ref_count = 0;
3135 	np->lease_valid = NFS4_LEASE_NOT_STARTED;
3136 	cv_init(&np->s_cv_otw_count, NULL, CV_DEFAULT, NULL);
3137 	np->s_otw_call_count = 0;
3138 	cv_init(&np->wait_cb_null, NULL, CV_DEFAULT, NULL);
3139 	np->zoneid = getzoneid();
3140 	np->zone_globals = nfs4_get_callback_globals();
3141 	ASSERT(np->zone_globals != NULL);
3142 	return (np);
3143 }
3144 
3145 /*
3146  * Create a new nfs4_server_t structure and add it to the list.
3147  * Returns new node locked; reference must eventually be freed.
3148  */
3149 static struct nfs4_server *
3150 add_new_nfs4_server(struct servinfo4 *svp, cred_t *cr)
3151 {
3152 	nfs4_server_t *sp;
3153 
3154 	ASSERT(MUTEX_HELD(&nfs4_server_lst_lock));
3155 	sp = new_nfs4_server(svp, cr);
3156 	mutex_enter(&sp->s_lock);
3157 	insque(sp, &nfs4_server_lst);
3158 	sp->s_refcnt++;			/* list gets a reference */
3159 	sp->clientid = 0;
3160 	sp->s_flags |= N4S_INSERTED;
3161 	return (sp);
3162 }
3163 
3164 int nfs4_server_t_debug = 0;
3165 
3166 #ifdef lint
3167 extern void
3168 dumpnfs4slist(char *, mntinfo4_t *, clientid4, servinfo4_t *);
3169 #endif
3170 
3171 #ifndef lint
3172 #ifdef DEBUG
3173 void
3174 dumpnfs4slist(char *txt, mntinfo4_t *mi, clientid4 clientid, servinfo4_t *srv_p)
3175 {
3176 	int hash16(void *p, int len);
3177 	nfs4_server_t *np;
3178 
3179 	NFS4_DEBUG(nfs4_server_t_debug, (CE_NOTE,
3180 	    "dumping nfs4_server_t list in %s", txt));
3181 	NFS4_DEBUG(nfs4_server_t_debug, (CE_CONT,
3182 	    "mi 0x%p, want clientid %llx, addr %d/%04X",
3183 	    mi, (longlong_t)clientid, srv_p->sv_addr.len,
3184 	    hash16((void *)srv_p->sv_addr.buf, srv_p->sv_addr.len)));
3185 	for (np = nfs4_server_lst.forw; np != &nfs4_server_lst;
3186 	    np = np->forw) {
3187 		NFS4_DEBUG(nfs4_server_t_debug, (CE_CONT,
3188 		    "node 0x%p,    clientid %llx, addr %d/%04X, cnt %d",
3189 		    np, (longlong_t)np->clientid, np->saddr.len,
3190 		    hash16((void *)np->saddr.buf, np->saddr.len),
3191 		    np->state_ref_count));
3192 		if (np->saddr.len == srv_p->sv_addr.len &&
3193 		    bcmp(np->saddr.buf, srv_p->sv_addr.buf,
3194 		    np->saddr.len) == 0)
3195 			NFS4_DEBUG(nfs4_server_t_debug, (CE_CONT,
3196 			    " - address matches"));
3197 		if (np->clientid == clientid || np->clientid == 0)
3198 			NFS4_DEBUG(nfs4_server_t_debug, (CE_CONT,
3199 			    " - clientid matches"));
3200 		if (np->s_thread_exit != NFS4_THREAD_EXIT)
3201 			NFS4_DEBUG(nfs4_server_t_debug, (CE_CONT,
3202 			    " - thread not exiting"));
3203 	}
3204 	delay(hz);
3205 }
3206 #endif
3207 #endif
3208 
3209 
3210 /*
3211  * Move a mntinfo4_t from one server list to another.
3212  * Locking of the two nfs4_server_t nodes will be done in list order.
3213  *
3214  * Returns NULL if the current nfs4_server_t for the filesystem could not
3215  * be found (e.g., due to forced unmount).  Otherwise returns a reference
3216  * to the new nfs4_server_t, which must eventually be freed.
3217  */
3218 nfs4_server_t *
3219 nfs4_move_mi(mntinfo4_t *mi, servinfo4_t *old, servinfo4_t *new)
3220 {
3221 	nfs4_server_t *p, *op = NULL, *np = NULL;
3222 	int num_open;
3223 	zoneid_t zoneid = nfs_zoneid();
3224 
3225 	ASSERT(nfs_zone() == mi->mi_zone);
3226 
3227 	mutex_enter(&nfs4_server_lst_lock);
3228 #ifdef DEBUG
3229 	if (nfs4_server_t_debug)
3230 		dumpnfs4slist("nfs4_move_mi", mi, (clientid4)0, new);
3231 #endif
3232 	for (p = nfs4_server_lst.forw; p != &nfs4_server_lst; p = p->forw) {
3233 		if (p->zoneid != zoneid)
3234 			continue;
3235 		if (p->saddr.len == old->sv_addr.len &&
3236 		    bcmp(p->saddr.buf, old->sv_addr.buf, p->saddr.len) == 0 &&
3237 		    p->s_thread_exit != NFS4_THREAD_EXIT) {
3238 			op = p;
3239 			mutex_enter(&op->s_lock);
3240 			op->s_refcnt++;
3241 		}
3242 		if (p->saddr.len == new->sv_addr.len &&
3243 		    bcmp(p->saddr.buf, new->sv_addr.buf, p->saddr.len) == 0 &&
3244 		    p->s_thread_exit != NFS4_THREAD_EXIT) {
3245 			np = p;
3246 			mutex_enter(&np->s_lock);
3247 		}
3248 		if (op != NULL && np != NULL)
3249 			break;
3250 	}
3251 	if (op == NULL) {
3252 		/*
3253 		 * Filesystem has been forcibly unmounted.  Bail out.
3254 		 */
3255 		if (np != NULL)
3256 			mutex_exit(&np->s_lock);
3257 		mutex_exit(&nfs4_server_lst_lock);
3258 		return (NULL);
3259 	}
3260 	if (np != NULL) {
3261 		np->s_refcnt++;
3262 	} else {
3263 #ifdef DEBUG
3264 		NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
3265 		    "nfs4_move_mi: no target nfs4_server, will create."));
3266 #endif
3267 		np = add_new_nfs4_server(new, kcred);
3268 	}
3269 	mutex_exit(&nfs4_server_lst_lock);
3270 
3271 	NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
3272 	    "nfs4_move_mi: for mi 0x%p, "
3273 	    "old servinfo4 0x%p, new servinfo4 0x%p, "
3274 	    "old nfs4_server 0x%p, new nfs4_server 0x%p, ",
3275 	    (void*)mi, (void*)old, (void*)new,
3276 	    (void*)op, (void*)np));
3277 	ASSERT(op != NULL && np != NULL);
3278 
3279 	/* discard any delegations */
3280 	nfs4_deleg_discard(mi, op);
3281 
3282 	num_open = mi->mi_open_files;
3283 	mi->mi_open_files = 0;
3284 	op->state_ref_count -= num_open;
3285 	ASSERT(op->state_ref_count >= 0);
3286 	np->state_ref_count += num_open;
3287 	nfs4_remove_mi_from_server_nolock(mi, op);
3288 	mi->mi_open_files = num_open;
3289 	NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
3290 	    "nfs4_move_mi: mi_open_files %d, op->cnt %d, np->cnt %d",
3291 	    mi->mi_open_files, op->state_ref_count, np->state_ref_count));
3292 
3293 	nfs4_add_mi_to_server(np, mi);
3294 
3295 	mutex_exit(&op->s_lock);
3296 	nfs4_server_rele(op);
3297 	mutex_exit(&np->s_lock);
3298 
3299 	return (np);
3300 }
3301 
3302 /*
3303  * Search the nfs4_server list to find a match on this servinfo4
3304  * based on its address.
3305  *
3306  * Returns NULL if no match is found.  Otherwise returns a reference (which
3307  * must eventually be freed) to a locked nfs4_server.
3308  */
3309 nfs4_server_t *
3310 servinfo4_to_nfs4_server(servinfo4_t *srv_p)
3311 {
3312 	nfs4_server_t *np;
3313 	zoneid_t zoneid = nfs_zoneid();
3314 
3315 	mutex_enter(&nfs4_server_lst_lock);
3316 	for (np = nfs4_server_lst.forw; np != &nfs4_server_lst; np = np->forw) {
3317 		if (np->zoneid == zoneid &&
3318 		    np->saddr.len == srv_p->sv_addr.len &&
3319 		    bcmp(np->saddr.buf, srv_p->sv_addr.buf,
3320 			    np->saddr.len) == 0 &&
3321 		    np->s_thread_exit != NFS4_THREAD_EXIT) {
3322 			mutex_enter(&np->s_lock);
3323 			np->s_refcnt++;
3324 			mutex_exit(&nfs4_server_lst_lock);
3325 			return (np);
3326 		}
3327 	}
3328 	mutex_exit(&nfs4_server_lst_lock);
3329 	return (NULL);
3330 }
3331 
3332 /*
3333  * Search the nfs4_server_lst to find a match based on clientid and
3334  * addr.
3335  * Locks the nfs4_server down if it is found and returns a reference that
3336  * must eventually be freed.
3337  *
3338  * Returns NULL it no match is found.  This means one of two things: either
3339  * mi is in the process of being mounted, or mi has been unmounted.
3340  *
3341  * The caller should be holding mi->mi_recovlock, and it should continue to
3342  * hold the lock until done with the returned nfs4_server_t.  Once
3343  * mi->mi_recovlock is released, there is no guarantee that the returned
3344  * mi->nfs4_server_t will continue to correspond to mi.
3345  */
3346 nfs4_server_t *
3347 find_nfs4_server(mntinfo4_t *mi)
3348 {
3349 	return (find_nfs4_server_all(mi, 0));
3350 }
3351 
3352 /*
3353  * Same as above, but takes an "all" parameter which can be
3354  * set to 1 if the caller wishes to find nfs4_server_t's which
3355  * have been marked for termination by the exit of the renew
3356  * thread.  This should only be used by operations which are
3357  * cleaning up and will not cause an OTW op.
3358  */
3359 nfs4_server_t *
3360 find_nfs4_server_all(mntinfo4_t *mi, int all)
3361 {
3362 	nfs4_server_t *np;
3363 	servinfo4_t *svp;
3364 	zoneid_t zoneid = mi->mi_zone->zone_id;
3365 
3366 	ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) ||
3367 	    nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
3368 	/*
3369 	 * This can be called from nfs4_unmount() which can be called from the
3370 	 * global zone, hence it's legal for the global zone to muck with
3371 	 * another zone's server list, as long as it doesn't try to contact
3372 	 * them.
3373 	 */
3374 	ASSERT(zoneid == getzoneid() || getzoneid() == GLOBAL_ZONEID ||
3375 	    nfs_global_client_only != 0);
3376 
3377 	/*
3378 	 * The nfs4_server_lst_lock global lock is held when we get a new
3379 	 * clientid (via SETCLIENTID OTW).  Holding this global lock and
3380 	 * mi_recovlock (READER is fine) ensures that the nfs4_server
3381 	 * and this mntinfo4 can't get out of sync, so the following search is
3382 	 * always valid.
3383 	 */
3384 	mutex_enter(&nfs4_server_lst_lock);
3385 #ifdef DEBUG
3386 	if (nfs4_server_t_debug) {
3387 		/* mi->mi_clientid is unprotected, ok for debug output */
3388 		dumpnfs4slist("find_nfs4_server", mi, mi->mi_clientid,
3389 			mi->mi_curr_serv);
3390 	}
3391 #endif
3392 	for (np = nfs4_server_lst.forw; np != &nfs4_server_lst; np = np->forw) {
3393 		mutex_enter(&np->s_lock);
3394 		svp = mi->mi_curr_serv;
3395 
3396 		if (np->zoneid == zoneid &&
3397 		    np->clientid == mi->mi_clientid &&
3398 		    np->saddr.len == svp->sv_addr.len &&
3399 		    bcmp(np->saddr.buf, svp->sv_addr.buf, np->saddr.len) == 0 &&
3400 		    (np->s_thread_exit != NFS4_THREAD_EXIT || all != 0)) {
3401 			mutex_exit(&nfs4_server_lst_lock);
3402 			np->s_refcnt++;
3403 			return (np);
3404 		}
3405 		mutex_exit(&np->s_lock);
3406 	}
3407 	mutex_exit(&nfs4_server_lst_lock);
3408 
3409 	return (NULL);
3410 }
3411 
3412 /*
3413  * Release the reference to sp and destroy it if that's the last one.
3414  */
3415 
3416 void
3417 nfs4_server_rele(nfs4_server_t *sp)
3418 {
3419 	mutex_enter(&sp->s_lock);
3420 	ASSERT(sp->s_refcnt > 0);
3421 	sp->s_refcnt--;
3422 	if (sp->s_refcnt > 0) {
3423 		mutex_exit(&sp->s_lock);
3424 		return;
3425 	}
3426 	if (!(sp->s_flags & N4S_INSERTED)) {
3427 		destroy_nfs4_server(sp);
3428 		return;
3429 	}
3430 	mutex_exit(&sp->s_lock);
3431 	mutex_enter(&nfs4_server_lst_lock);
3432 	mutex_enter(&sp->s_lock);
3433 	if (sp->s_refcnt > 0) {
3434 		mutex_exit(&sp->s_lock);
3435 		mutex_exit(&nfs4_server_lst_lock);
3436 		return;
3437 	}
3438 	if (sp->s_flags & N4S_INSERTED) {
3439 		remque(sp);
3440 		sp->forw = sp->back = NULL;
3441 		sp->s_flags &= ~N4S_INSERTED;
3442 	}
3443 	mutex_exit(&nfs4_server_lst_lock);
3444 	destroy_nfs4_server(sp);
3445 }
3446 
3447 static void
3448 destroy_nfs4_server(nfs4_server_t *sp)
3449 {
3450 	ASSERT(MUTEX_HELD(&sp->s_lock));
3451 	ASSERT(!(sp->s_flags & N4S_INSERTED));
3452 	ASSERT(sp->s_refcnt == 0);
3453 	ASSERT(sp->s_otw_call_count == 0);
3454 
3455 	remove_all_mi(sp);
3456 
3457 	crfree(sp->s_cred);
3458 	kmem_free(sp->saddr.buf, sp->saddr.maxlen);
3459 	kmem_free(sp->clidtosend.id_val, sp->clidtosend.id_len);
3460 	mutex_exit(&sp->s_lock);
3461 
3462 	/* destroy the nfs4_server */
3463 	nfs4callback_destroy(sp);
3464 	list_destroy(&sp->s_deleg_list);
3465 	mutex_destroy(&sp->s_lock);
3466 	cv_destroy(&sp->cv_thread_exit);
3467 	cv_destroy(&sp->s_cv_otw_count);
3468 	cv_destroy(&sp->wait_cb_null);
3469 	nfs_rw_destroy(&sp->s_recovlock);
3470 	kmem_free(sp, sizeof (*sp));
3471 }
3472 
3473 /*
3474  * Lock sp, but only if it's still active (in the list and hasn't been
3475  * flagged as exiting) or 'all' is non-zero.
3476  * Returns TRUE if sp got locked and adds a reference to sp.
3477  */
3478 bool_t
3479 nfs4_server_vlock(nfs4_server_t *sp, int all)
3480 {
3481 	nfs4_server_t *np;
3482 
3483 	mutex_enter(&nfs4_server_lst_lock);
3484 	for (np = nfs4_server_lst.forw; np != &nfs4_server_lst; np = np->forw) {
3485 		if (sp == np && (np->s_thread_exit != NFS4_THREAD_EXIT ||
3486 		    all != 0)) {
3487 			mutex_enter(&np->s_lock);
3488 			np->s_refcnt++;
3489 			mutex_exit(&nfs4_server_lst_lock);
3490 			return (TRUE);
3491 		}
3492 	}
3493 	mutex_exit(&nfs4_server_lst_lock);
3494 	return (FALSE);
3495 }
3496 
3497 /*
3498  * Fork off a thread to free the data structures for a mount.
3499  */
3500 
3501 static void
3502 async_free_mount(vfs_t *vfsp, cred_t *cr)
3503 {
3504 	freemountargs_t *args;
3505 
3506 	args = kmem_alloc(sizeof (freemountargs_t), KM_SLEEP);
3507 	args->fm_vfsp = vfsp;
3508 	VFS_HOLD(vfsp);
3509 	args->fm_cr = cr;
3510 	crhold(cr);
3511 
3512 	(void) zthread_create(NULL, 0, nfs4_free_mount_thread, args, 0,
3513 	    minclsyspri);
3514 }
3515 
3516 static void
3517 nfs4_free_mount_thread(freemountargs_t *args)
3518 {
3519 	nfs4_free_mount(args->fm_vfsp, args->fm_cr);
3520 	VFS_RELE(args->fm_vfsp);
3521 	crfree(args->fm_cr);
3522 	kmem_free(args, sizeof (freemountargs_t));
3523 	zthread_exit();
3524 	/* NOTREACHED */
3525 }
3526 
3527 /*
3528  * Thread to free the data structures for a given filesystem.
3529  */
3530 static void
3531 nfs4_free_mount(vfs_t *vfsp, cred_t *cr)
3532 {
3533 	mntinfo4_t	*mi = VFTOMI4(vfsp);
3534 	nfs4_server_t	*sp;
3535 	callb_cpr_t	cpr_info;
3536 	kmutex_t	cpr_lock;
3537 	boolean_t	async_thread;
3538 
3539 	/*
3540 	 * We need to participate in the CPR framework if this is a kernel
3541 	 * thread.
3542 	 */
3543 	async_thread = (curproc == nfs_zone()->zone_zsched);
3544 	if (async_thread) {
3545 		mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
3546 		CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr,
3547 		    "nfsv4AsyncUnmount");
3548 	}
3549 
3550 	/*
3551 	 * We need to wait for all outstanding OTW calls
3552 	 * and recovery to finish before we remove the mi
3553 	 * from the nfs4_server_t, as current pending
3554 	 * calls might still need this linkage (in order
3555 	 * to find a nfs4_server_t from a mntinfo4_t).
3556 	 */
3557 	(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, FALSE);
3558 	sp = find_nfs4_server(mi);
3559 	nfs_rw_exit(&mi->mi_recovlock);
3560 
3561 	if (sp) {
3562 		while (sp->s_otw_call_count != 0) {
3563 			if (async_thread) {
3564 				mutex_enter(&cpr_lock);
3565 				CALLB_CPR_SAFE_BEGIN(&cpr_info);
3566 				mutex_exit(&cpr_lock);
3567 			}
3568 			cv_wait(&sp->s_cv_otw_count, &sp->s_lock);
3569 			if (async_thread) {
3570 				mutex_enter(&cpr_lock);
3571 				CALLB_CPR_SAFE_END(&cpr_info, &cpr_lock);
3572 				mutex_exit(&cpr_lock);
3573 			}
3574 		}
3575 		mutex_exit(&sp->s_lock);
3576 		nfs4_server_rele(sp);
3577 		sp = NULL;
3578 	}
3579 
3580 
3581 	mutex_enter(&mi->mi_lock);
3582 	while (mi->mi_in_recovery != 0) {
3583 		if (async_thread) {
3584 			mutex_enter(&cpr_lock);
3585 			CALLB_CPR_SAFE_BEGIN(&cpr_info);
3586 			mutex_exit(&cpr_lock);
3587 		}
3588 		cv_wait(&mi->mi_cv_in_recov, &mi->mi_lock);
3589 		if (async_thread) {
3590 			mutex_enter(&cpr_lock);
3591 			CALLB_CPR_SAFE_END(&cpr_info, &cpr_lock);
3592 			mutex_exit(&cpr_lock);
3593 		}
3594 	}
3595 	mutex_exit(&mi->mi_lock);
3596 
3597 	/*
3598 	 * The original purge of the dnlc via 'dounmount'
3599 	 * doesn't guarantee that another dnlc entry was not
3600 	 * added while we waitied for all outstanding OTW
3601 	 * and recovery calls to finish.  So re-purge the
3602 	 * dnlc now.
3603 	 */
3604 	(void) dnlc_purge_vfsp(vfsp, 0);
3605 
3606 	/*
3607 	 * We need to explicitly stop the manager thread; the asyc worker
3608 	 * threads can timeout and exit on their own.
3609 	 */
3610 	nfs4_async_manager_stop(vfsp);
3611 
3612 	destroy_rtable4(vfsp, cr);
3613 
3614 	nfs4_remove_mi_from_server(mi, NULL);
3615 
3616 	if (mi->mi_io_kstats) {
3617 		kstat_delete(mi->mi_io_kstats);
3618 		mi->mi_io_kstats = NULL;
3619 	}
3620 	if (mi->mi_ro_kstats) {
3621 		kstat_delete(mi->mi_ro_kstats);
3622 		mi->mi_ro_kstats = NULL;
3623 	}
3624 	if (mi->mi_recov_ksp) {
3625 		kstat_delete(mi->mi_recov_ksp);
3626 		mi->mi_recov_ksp = NULL;
3627 	}
3628 
3629 	if (async_thread) {
3630 		mutex_enter(&cpr_lock);
3631 		CALLB_CPR_EXIT(&cpr_info);	/* drops cpr_lock */
3632 		mutex_destroy(&cpr_lock);
3633 	}
3634 }
3635