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