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