xref: /titanic_50/usr/src/uts/common/fs/nfs/nfs4_vfsops.c (revision 2810e2a6e10a5e0c740e995ef77b89025f847106)
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 2009 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 		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 = &mi->mi_async_reqs[0];
2375 	mi->mi_max_threads = nfs4_max_threads;
2376 	mutex_init(&mi->mi_async_lock, NULL, MUTEX_DEFAULT, NULL);
2377 	cv_init(&mi->mi_async_reqs_cv, NULL, CV_DEFAULT, NULL);
2378 	cv_init(&mi->mi_async_work_cv, NULL, CV_DEFAULT, NULL);
2379 	cv_init(&mi->mi_async_cv, NULL, CV_DEFAULT, NULL);
2380 	cv_init(&mi->mi_inact_req_cv, NULL, CV_DEFAULT, NULL);
2381 
2382 	mi->mi_vfsp = vfsp;
2383 	zone_hold(mi->mi_zone = zone);
2384 	nfs4_mi_zonelist_add(mi);
2385 
2386 	/*
2387 	 * Initialize the <open owner/cred> hash table.
2388 	 */
2389 	for (i = 0; i < NFS4_NUM_OO_BUCKETS; i++) {
2390 		bucketp = &(mi->mi_oo_list[i]);
2391 		mutex_init(&bucketp->b_lock, NULL, MUTEX_DEFAULT, NULL);
2392 		list_create(&bucketp->b_oo_hash_list,
2393 		    sizeof (nfs4_open_owner_t),
2394 		    offsetof(nfs4_open_owner_t, oo_hash_node));
2395 	}
2396 
2397 	/*
2398 	 * Initialize the freed open owner list.
2399 	 */
2400 	mi->mi_foo_num = 0;
2401 	mi->mi_foo_max = NFS4_NUM_FREED_OPEN_OWNERS;
2402 	list_create(&mi->mi_foo_list, sizeof (nfs4_open_owner_t),
2403 	    offsetof(nfs4_open_owner_t, oo_foo_node));
2404 
2405 	list_create(&mi->mi_lost_state, sizeof (nfs4_lost_rqst_t),
2406 	    offsetof(nfs4_lost_rqst_t, lr_node));
2407 
2408 	list_create(&mi->mi_bseqid_list, sizeof (nfs4_bseqid_entry_t),
2409 	    offsetof(nfs4_bseqid_entry_t, bs_node));
2410 
2411 	/*
2412 	 * Initialize the msg buffer.
2413 	 */
2414 	list_create(&mi->mi_msg_list, sizeof (nfs4_debug_msg_t),
2415 	    offsetof(nfs4_debug_msg_t, msg_node));
2416 	mi->mi_msg_count = 0;
2417 	mutex_init(&mi->mi_msg_list_lock, NULL, MUTEX_DEFAULT, NULL);
2418 
2419 	/*
2420 	 * Initialize kstats
2421 	 */
2422 	nfs4_mnt_kstat_init(vfsp);
2423 
2424 	/*
2425 	 * Initialize the shared filehandle pool.
2426 	 */
2427 	sfh4_createtab(&mi->mi_filehandles);
2428 
2429 	/*
2430 	 * Save server path we're attempting to mount.
2431 	 */
2432 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
2433 	origsvp = copy_svp(svp);
2434 	nfs_rw_exit(&svp->sv_lock);
2435 
2436 	/*
2437 	 * Make the GETFH call to get root fh for each replica.
2438 	 */
2439 	if (svp_head->sv_next)
2440 		droptext = ", dropping replica";
2441 
2442 	/*
2443 	 * If the uid is set then set the creds for secure mounts
2444 	 * by proxy processes such as automountd.
2445 	 */
2446 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
2447 	if (svp->sv_secdata->uid != 0 &&
2448 	    svp->sv_secdata->rpcflavor == RPCSEC_GSS) {
2449 		lcr = crdup(cr);
2450 		(void) crsetugid(lcr, svp->sv_secdata->uid, crgetgid(cr));
2451 		tcr = lcr;
2452 	}
2453 	nfs_rw_exit(&svp->sv_lock);
2454 	for (svp = svp_head; svp; svp = svp->sv_next) {
2455 		if (nfs4_chkdup_servinfo4(svp_head, svp)) {
2456 			nfs_cmn_err(error, CE_WARN,
2457 			    VERS_MSG "Host %s is a duplicate%s",
2458 			    svp->sv_hostname, droptext);
2459 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
2460 			svp->sv_flags |= SV4_NOTINUSE;
2461 			nfs_rw_exit(&svp->sv_lock);
2462 			continue;
2463 		}
2464 		mi->mi_curr_serv = svp;
2465 
2466 		/*
2467 		 * Just in case server path being mounted contains
2468 		 * symlinks and fails w/STALE, save the initial sv_path
2469 		 * so we can redrive the initial mount compound with the
2470 		 * initial sv_path -- not a symlink-expanded version.
2471 		 *
2472 		 * This could only happen if a symlink was expanded
2473 		 * and the expanded mount compound failed stale.  Because
2474 		 * it could be the case that the symlink was removed at
2475 		 * the server (and replaced with another symlink/dir,
2476 		 * we need to use the initial sv_path when attempting
2477 		 * to re-lookup everything and recover.
2478 		 *
2479 		 * Other mount errors should evenutally be handled here also
2480 		 * (NFS4ERR_DELAY, NFS4ERR_RESOURCE).  For now, all mount
2481 		 * failures will result in mount being redriven a few times.
2482 		 */
2483 		num_retry = nfs4_max_mount_retry;
2484 		do {
2485 			nfs4getfh_otw(mi, svp, &tmp_vtype,
2486 			    ((flags & NFSMNT_PUBLIC) ? NFS4_GETFH_PUBLIC : 0) |
2487 			    NFS4_GETFH_NEEDSOP, tcr, &e);
2488 
2489 			if (e.error == 0 && e.stat == NFS4_OK)
2490 				break;
2491 
2492 			/*
2493 			 * For some reason, the mount compound failed.  Before
2494 			 * retrying, we need to restore original conditions.
2495 			 */
2496 			svp = restore_svp(mi, svp, origsvp);
2497 			svp_head = svp;
2498 
2499 		} while (num_retry-- > 0);
2500 		error = e.error ? e.error : geterrno4(e.stat);
2501 		if (error) {
2502 			nfs_cmn_err(error, CE_WARN,
2503 			    VERS_MSG "initial call to %s failed%s: %m",
2504 			    svp->sv_hostname, droptext);
2505 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
2506 			svp->sv_flags |= SV4_NOTINUSE;
2507 			nfs_rw_exit(&svp->sv_lock);
2508 			mi->mi_flags &= ~MI4_RECOV_FAIL;
2509 			mi->mi_error = 0;
2510 			continue;
2511 		}
2512 
2513 		if (tmp_vtype == VBAD) {
2514 			zcmn_err(mi->mi_zone->zone_id, CE_WARN,
2515 			    VERS_MSG "%s returned a bad file type for "
2516 			    "root%s", svp->sv_hostname, droptext);
2517 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
2518 			svp->sv_flags |= SV4_NOTINUSE;
2519 			nfs_rw_exit(&svp->sv_lock);
2520 			continue;
2521 		}
2522 
2523 		if (vtype == VNON) {
2524 			vtype = tmp_vtype;
2525 		} else if (vtype != tmp_vtype) {
2526 			zcmn_err(mi->mi_zone->zone_id, CE_WARN,
2527 			    VERS_MSG "%s returned a different file type "
2528 			    "for root%s", svp->sv_hostname, droptext);
2529 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
2530 			svp->sv_flags |= SV4_NOTINUSE;
2531 			nfs_rw_exit(&svp->sv_lock);
2532 			continue;
2533 		}
2534 		if (firstsvp == NULL)
2535 			firstsvp = svp;
2536 	}
2537 
2538 	if (firstsvp == NULL) {
2539 		if (error == 0)
2540 			error = ENOENT;
2541 		goto bad;
2542 	}
2543 
2544 	mi->mi_curr_serv = svp = firstsvp;
2545 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
2546 	ASSERT((mi->mi_curr_serv->sv_flags & SV4_NOTINUSE) == 0);
2547 	fh.nfs_fh4_len = svp->sv_fhandle.fh_len;
2548 	fh.nfs_fh4_val = svp->sv_fhandle.fh_buf;
2549 	mi->mi_rootfh = sfh4_get(&fh, mi);
2550 	fh.nfs_fh4_len = svp->sv_pfhandle.fh_len;
2551 	fh.nfs_fh4_val = svp->sv_pfhandle.fh_buf;
2552 	mi->mi_srvparentfh = sfh4_get(&fh, mi);
2553 	nfs_rw_exit(&svp->sv_lock);
2554 
2555 	/*
2556 	 * Get the fname for filesystem root.
2557 	 */
2558 	mi->mi_fname = fn_get(NULL, ".", mi->mi_rootfh);
2559 	mfname = mi->mi_fname;
2560 	fn_hold(mfname);
2561 
2562 	/*
2563 	 * Make the root vnode without attributes.
2564 	 */
2565 	rtvp = makenfs4node_by_fh(mi->mi_rootfh, NULL,
2566 	    &mfname, NULL, mi, cr, gethrtime());
2567 	rtvp->v_type = vtype;
2568 
2569 	mi->mi_curread = mi->mi_tsize;
2570 	mi->mi_curwrite = mi->mi_stsize;
2571 
2572 	/*
2573 	 * Start the manager thread responsible for handling async worker
2574 	 * threads.
2575 	 */
2576 	MI4_HOLD(mi);
2577 	VFS_HOLD(vfsp);	/* add reference for thread */
2578 	mi->mi_manager_thread = zthread_create(NULL, 0, nfs4_async_manager,
2579 	    vfsp, 0, minclsyspri);
2580 	ASSERT(mi->mi_manager_thread != NULL);
2581 
2582 	/*
2583 	 * Create the thread that handles over-the-wire calls for
2584 	 * VOP_INACTIVE.
2585 	 * This needs to happen after the manager thread is created.
2586 	 */
2587 	MI4_HOLD(mi);
2588 	mi->mi_inactive_thread = zthread_create(NULL, 0, nfs4_inactive_thread,
2589 	    mi, 0, minclsyspri);
2590 	ASSERT(mi->mi_inactive_thread != NULL);
2591 
2592 	/* If we didn't get a type, get one now */
2593 	if (rtvp->v_type == VNON) {
2594 		va.va_mask = AT_TYPE;
2595 		error = nfs4getattr(rtvp, &va, tcr);
2596 		if (error)
2597 			goto bad;
2598 		rtvp->v_type = va.va_type;
2599 	}
2600 
2601 	mi->mi_type = rtvp->v_type;
2602 
2603 	mutex_enter(&mi->mi_lock);
2604 	mi->mi_flags &= ~MI4_MOUNTING;
2605 	mutex_exit(&mi->mi_lock);
2606 
2607 	/* Update VFS with new server and path info */
2608 	if ((strcmp(svp->sv_hostname, origsvp->sv_hostname) != 0) ||
2609 	    (strcmp(svp->sv_path, origsvp->sv_path) != 0)) {
2610 		len = svp->sv_hostnamelen + svp->sv_pathlen;
2611 		resource = kmem_zalloc(len, KM_SLEEP);
2612 		(void) strcat(resource, svp->sv_hostname);
2613 		(void) strcat(resource, ":");
2614 		(void) strcat(resource, svp->sv_path);
2615 		vfs_setresource(vfsp, resource);
2616 		kmem_free(resource, len);
2617 	}
2618 
2619 	sv4_free(origsvp);
2620 	*rtvpp = rtvp;
2621 	if (lcr != NULL)
2622 		crfree(lcr);
2623 
2624 	return (0);
2625 bad:
2626 	/*
2627 	 * An error occurred somewhere, need to clean up...
2628 	 */
2629 	if (lcr != NULL)
2630 		crfree(lcr);
2631 
2632 	if (rtvp != NULL) {
2633 		/*
2634 		 * We need to release our reference to the root vnode and
2635 		 * destroy the mntinfo4 struct that we just created.
2636 		 */
2637 		rp = VTOR4(rtvp);
2638 		if (rp->r_flags & R4HASHED)
2639 			rp4_rmhash(rp);
2640 		VN_RELE(rtvp);
2641 	}
2642 	nfs4_async_stop(vfsp);
2643 	nfs4_async_manager_stop(vfsp);
2644 	removed = nfs4_mi_zonelist_remove(mi);
2645 	if (removed)
2646 		zone_rele(mi->mi_zone);
2647 
2648 	/*
2649 	 * This releases the initial "hold" of the mi since it will never
2650 	 * be referenced by the vfsp.  Also, when mount returns to vfs.c
2651 	 * with an error, the vfsp will be destroyed, not rele'd.
2652 	 */
2653 	MI4_RELE(mi);
2654 
2655 	if (origsvp != NULL)
2656 		sv4_free(origsvp);
2657 
2658 	*rtvpp = NULL;
2659 	return (error);
2660 }
2661 
2662 /*
2663  * vfs operations
2664  */
2665 static int
2666 nfs4_unmount(vfs_t *vfsp, int flag, cred_t *cr)
2667 {
2668 	mntinfo4_t		*mi;
2669 	ushort_t		omax;
2670 	int			removed;
2671 
2672 	bool_t			must_unlock;
2673 
2674 	nfs4_ephemeral_tree_t	*eph_tree;
2675 
2676 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
2677 		return (EPERM);
2678 
2679 	mi = VFTOMI4(vfsp);
2680 
2681 	if (flag & MS_FORCE) {
2682 		vfsp->vfs_flag |= VFS_UNMOUNTED;
2683 		if (nfs_zone() != mi->mi_zone) {
2684 			/*
2685 			 * If the request is coming from the wrong zone,
2686 			 * we don't want to create any new threads, and
2687 			 * performance is not a concern.  Do everything
2688 			 * inline.
2689 			 */
2690 			NFS4_DEBUG(nfs4_client_zone_debug, (CE_NOTE,
2691 			    "nfs4_unmount x-zone forced unmount of vfs %p\n",
2692 			    (void *)vfsp));
2693 			nfs4_free_mount(vfsp, flag, cr);
2694 		} else {
2695 			/*
2696 			 * Free data structures asynchronously, to avoid
2697 			 * blocking the current thread (for performance
2698 			 * reasons only).
2699 			 */
2700 			async_free_mount(vfsp, flag, cr);
2701 		}
2702 
2703 		return (0);
2704 	}
2705 
2706 	/*
2707 	 * Wait until all asynchronous putpage operations on
2708 	 * this file system are complete before flushing rnodes
2709 	 * from the cache.
2710 	 */
2711 	omax = mi->mi_max_threads;
2712 	if (nfs4_async_stop_sig(vfsp))
2713 		return (EINTR);
2714 
2715 	r4flush(vfsp, cr);
2716 
2717 	/*
2718 	 * About the only reason that this would fail would be
2719 	 * that the harvester is already busy tearing down this
2720 	 * node. So we fail back to the caller and let them try
2721 	 * again when needed.
2722 	 */
2723 	if (nfs4_ephemeral_umount(mi, flag, cr,
2724 	    &must_unlock, &eph_tree)) {
2725 		ASSERT(must_unlock == FALSE);
2726 		mutex_enter(&mi->mi_async_lock);
2727 		mi->mi_max_threads = omax;
2728 		mutex_exit(&mi->mi_async_lock);
2729 
2730 		return (EBUSY);
2731 	}
2732 
2733 	/*
2734 	 * If there are any active vnodes on this file system,
2735 	 * then the file system is busy and can't be unmounted.
2736 	 */
2737 	if (check_rtable4(vfsp)) {
2738 		nfs4_ephemeral_umount_unlock(&must_unlock, &eph_tree);
2739 
2740 		mutex_enter(&mi->mi_async_lock);
2741 		mi->mi_max_threads = omax;
2742 		mutex_exit(&mi->mi_async_lock);
2743 
2744 		return (EBUSY);
2745 	}
2746 
2747 	/*
2748 	 * The unmount can't fail from now on, so record any
2749 	 * ephemeral changes.
2750 	 */
2751 	nfs4_ephemeral_umount_activate(mi, &must_unlock, &eph_tree);
2752 
2753 	/*
2754 	 * There are no active files that could require over-the-wire
2755 	 * calls to the server, so stop the async manager and the
2756 	 * inactive thread.
2757 	 */
2758 	nfs4_async_manager_stop(vfsp);
2759 
2760 	/*
2761 	 * Destroy all rnodes belonging to this file system from the
2762 	 * rnode hash queues and purge any resources allocated to
2763 	 * them.
2764 	 */
2765 	destroy_rtable4(vfsp, cr);
2766 	vfsp->vfs_flag |= VFS_UNMOUNTED;
2767 
2768 	nfs4_remove_mi_from_server(mi, NULL);
2769 	removed = nfs4_mi_zonelist_remove(mi);
2770 	if (removed)
2771 		zone_rele(mi->mi_zone);
2772 
2773 	return (0);
2774 }
2775 
2776 /*
2777  * find root of nfs
2778  */
2779 static int
2780 nfs4_root(vfs_t *vfsp, vnode_t **vpp)
2781 {
2782 	mntinfo4_t *mi;
2783 	vnode_t *vp;
2784 	nfs4_fname_t *mfname;
2785 	servinfo4_t *svp;
2786 
2787 	mi = VFTOMI4(vfsp);
2788 
2789 	if (nfs_zone() != mi->mi_zone)
2790 		return (EPERM);
2791 
2792 	svp = mi->mi_curr_serv;
2793 	if (svp) {
2794 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
2795 		if (svp->sv_flags & SV4_ROOT_STALE) {
2796 			nfs_rw_exit(&svp->sv_lock);
2797 
2798 			(void) nfs_rw_enter_sig(&svp->sv_lock, RW_WRITER, 0);
2799 			if (svp->sv_flags & SV4_ROOT_STALE) {
2800 				svp->sv_flags &= ~SV4_ROOT_STALE;
2801 				nfs_rw_exit(&svp->sv_lock);
2802 				return (ENOENT);
2803 			}
2804 			nfs_rw_exit(&svp->sv_lock);
2805 		} else
2806 			nfs_rw_exit(&svp->sv_lock);
2807 	}
2808 
2809 	mfname = mi->mi_fname;
2810 	fn_hold(mfname);
2811 	vp = makenfs4node_by_fh(mi->mi_rootfh, NULL, &mfname, NULL,
2812 	    VFTOMI4(vfsp), CRED(), gethrtime());
2813 
2814 	if (VTOR4(vp)->r_flags & R4STALE) {
2815 		VN_RELE(vp);
2816 		return (ENOENT);
2817 	}
2818 
2819 	ASSERT(vp->v_type == VNON || vp->v_type == mi->mi_type);
2820 
2821 	vp->v_type = mi->mi_type;
2822 
2823 	*vpp = vp;
2824 
2825 	return (0);
2826 }
2827 
2828 static int
2829 nfs4_statfs_otw(vnode_t *vp, struct statvfs64 *sbp, cred_t *cr)
2830 {
2831 	int error;
2832 	nfs4_ga_res_t gar;
2833 	nfs4_ga_ext_res_t ger;
2834 
2835 	gar.n4g_ext_res = &ger;
2836 
2837 	if (error = nfs4_attr_otw(vp, TAG_FSINFO, &gar,
2838 	    NFS4_STATFS_ATTR_MASK, cr))
2839 		return (error);
2840 
2841 	*sbp = gar.n4g_ext_res->n4g_sb;
2842 
2843 	return (0);
2844 }
2845 
2846 /*
2847  * Get file system statistics.
2848  */
2849 static int
2850 nfs4_statvfs(vfs_t *vfsp, struct statvfs64 *sbp)
2851 {
2852 	int error;
2853 	vnode_t *vp;
2854 	cred_t *cr;
2855 
2856 	error = nfs4_root(vfsp, &vp);
2857 	if (error)
2858 		return (error);
2859 
2860 	cr = CRED();
2861 
2862 	error = nfs4_statfs_otw(vp, sbp, cr);
2863 	if (!error) {
2864 		(void) strncpy(sbp->f_basetype,
2865 		    vfssw[vfsp->vfs_fstype].vsw_name, FSTYPSZ);
2866 		sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
2867 	} else {
2868 		nfs4_purge_stale_fh(error, vp, cr);
2869 	}
2870 
2871 	VN_RELE(vp);
2872 
2873 	return (error);
2874 }
2875 
2876 static kmutex_t nfs4_syncbusy;
2877 
2878 /*
2879  * Flush dirty nfs files for file system vfsp.
2880  * If vfsp == NULL, all nfs files are flushed.
2881  *
2882  * SYNC_CLOSE in flag is passed to us to
2883  * indicate that we are shutting down and or
2884  * rebooting.
2885  */
2886 static int
2887 nfs4_sync(vfs_t *vfsp, short flag, cred_t *cr)
2888 {
2889 	/*
2890 	 * Cross-zone calls are OK here, since this translates to a
2891 	 * VOP_PUTPAGE(B_ASYNC), which gets picked up by the right zone.
2892 	 */
2893 	if (!(flag & SYNC_ATTR) && mutex_tryenter(&nfs4_syncbusy) != 0) {
2894 		r4flush(vfsp, cr);
2895 		mutex_exit(&nfs4_syncbusy);
2896 	}
2897 
2898 	/*
2899 	 * if SYNC_CLOSE is set then we know that
2900 	 * the system is rebooting, mark the mntinfo
2901 	 * for later examination.
2902 	 */
2903 	if (vfsp && (flag & SYNC_CLOSE)) {
2904 		mntinfo4_t *mi;
2905 
2906 		mi = VFTOMI4(vfsp);
2907 		if (!(mi->mi_flags & MI4_SHUTDOWN)) {
2908 			mutex_enter(&mi->mi_lock);
2909 			mi->mi_flags |= MI4_SHUTDOWN;
2910 			mutex_exit(&mi->mi_lock);
2911 		}
2912 	}
2913 	return (0);
2914 }
2915 
2916 /*
2917  * vget is difficult, if not impossible, to support in v4 because we don't
2918  * know the parent directory or name, which makes it impossible to create a
2919  * useful shadow vnode.  And we need the shadow vnode for things like
2920  * OPEN.
2921  */
2922 
2923 /* ARGSUSED */
2924 /*
2925  * XXX Check nfs4_vget_pseudo() for dependency.
2926  */
2927 static int
2928 nfs4_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
2929 {
2930 	return (EREMOTE);
2931 }
2932 
2933 /*
2934  * nfs4_mountroot get called in the case where we are diskless booting.  All
2935  * we need from here is the ability to get the server info and from there we
2936  * can simply call nfs4_rootvp.
2937  */
2938 /* ARGSUSED */
2939 static int
2940 nfs4_mountroot(vfs_t *vfsp, whymountroot_t why)
2941 {
2942 	vnode_t *rtvp;
2943 	char root_hostname[SYS_NMLN+1];
2944 	struct servinfo4 *svp;
2945 	int error;
2946 	int vfsflags;
2947 	size_t size;
2948 	char *root_path;
2949 	struct pathname pn;
2950 	char *name;
2951 	cred_t *cr;
2952 	mntinfo4_t *mi;
2953 	struct nfs_args args;		/* nfs mount arguments */
2954 	static char token[10];
2955 	nfs4_error_t n4e;
2956 
2957 	bzero(&args, sizeof (args));
2958 
2959 	/* do this BEFORE getfile which causes xid stamps to be initialized */
2960 	clkset(-1L);		/* hack for now - until we get time svc? */
2961 
2962 	if (why == ROOT_REMOUNT) {
2963 		/*
2964 		 * Shouldn't happen.
2965 		 */
2966 		panic("nfs4_mountroot: why == ROOT_REMOUNT");
2967 	}
2968 
2969 	if (why == ROOT_UNMOUNT) {
2970 		/*
2971 		 * Nothing to do for NFS.
2972 		 */
2973 		return (0);
2974 	}
2975 
2976 	/*
2977 	 * why == ROOT_INIT
2978 	 */
2979 
2980 	name = token;
2981 	*name = 0;
2982 	(void) getfsname("root", name, sizeof (token));
2983 
2984 	pn_alloc(&pn);
2985 	root_path = pn.pn_path;
2986 
2987 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
2988 	nfs_rw_init(&svp->sv_lock, NULL, RW_DEFAULT, NULL);
2989 	svp->sv_knconf = kmem_zalloc(sizeof (*svp->sv_knconf), KM_SLEEP);
2990 	svp->sv_knconf->knc_protofmly = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
2991 	svp->sv_knconf->knc_proto = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
2992 
2993 	/*
2994 	 * Get server address
2995 	 * Get the root path
2996 	 * Get server's transport
2997 	 * Get server's hostname
2998 	 * Get options
2999 	 */
3000 	args.addr = &svp->sv_addr;
3001 	(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3002 	args.fh = (char *)&svp->sv_fhandle;
3003 	args.knconf = svp->sv_knconf;
3004 	args.hostname = root_hostname;
3005 	vfsflags = 0;
3006 	if (error = mount_root(*name ? name : "root", root_path, NFS_V4,
3007 	    &args, &vfsflags)) {
3008 		if (error == EPROTONOSUPPORT)
3009 			nfs_cmn_err(error, CE_WARN, "nfs4_mountroot: "
3010 			    "mount_root failed: server doesn't support NFS V4");
3011 		else
3012 			nfs_cmn_err(error, CE_WARN,
3013 			    "nfs4_mountroot: mount_root failed: %m");
3014 		nfs_rw_exit(&svp->sv_lock);
3015 		sv4_free(svp);
3016 		pn_free(&pn);
3017 		return (error);
3018 	}
3019 	nfs_rw_exit(&svp->sv_lock);
3020 	svp->sv_hostnamelen = (int)(strlen(root_hostname) + 1);
3021 	svp->sv_hostname = kmem_alloc(svp->sv_hostnamelen, KM_SLEEP);
3022 	(void) strcpy(svp->sv_hostname, root_hostname);
3023 
3024 	svp->sv_pathlen = (int)(strlen(root_path) + 1);
3025 	svp->sv_path = kmem_alloc(svp->sv_pathlen, KM_SLEEP);
3026 	(void) strcpy(svp->sv_path, root_path);
3027 
3028 	/*
3029 	 * Force root partition to always be mounted with AUTH_UNIX for now
3030 	 */
3031 	svp->sv_secdata = kmem_alloc(sizeof (*svp->sv_secdata), KM_SLEEP);
3032 	svp->sv_secdata->secmod = AUTH_UNIX;
3033 	svp->sv_secdata->rpcflavor = AUTH_UNIX;
3034 	svp->sv_secdata->data = NULL;
3035 
3036 	cr = crgetcred();
3037 	rtvp = NULL;
3038 
3039 	error = nfs4rootvp(&rtvp, vfsp, svp, args.flags, cr, global_zone);
3040 
3041 	if (error) {
3042 		crfree(cr);
3043 		pn_free(&pn);
3044 		sv4_free(svp);
3045 		return (error);
3046 	}
3047 
3048 	mi = VTOMI4(rtvp);
3049 
3050 	/*
3051 	 * Send client id to the server, if necessary
3052 	 */
3053 	nfs4_error_zinit(&n4e);
3054 	nfs4setclientid(mi, cr, FALSE, &n4e);
3055 	error = n4e.error;
3056 
3057 	crfree(cr);
3058 
3059 	if (error) {
3060 		pn_free(&pn);
3061 		goto errout;
3062 	}
3063 
3064 	error = nfs4_setopts(rtvp, DATAMODEL_NATIVE, &args);
3065 	if (error) {
3066 		nfs_cmn_err(error, CE_WARN,
3067 		    "nfs4_mountroot: invalid root mount options");
3068 		pn_free(&pn);
3069 		goto errout;
3070 	}
3071 
3072 	(void) vfs_lock_wait(vfsp);
3073 	vfs_add(NULL, vfsp, vfsflags);
3074 	vfs_unlock(vfsp);
3075 
3076 	size = strlen(svp->sv_hostname);
3077 	(void) strcpy(rootfs.bo_name, svp->sv_hostname);
3078 	rootfs.bo_name[size] = ':';
3079 	(void) strcpy(&rootfs.bo_name[size + 1], root_path);
3080 
3081 	pn_free(&pn);
3082 
3083 errout:
3084 	if (error) {
3085 		sv4_free(svp);
3086 		nfs4_async_stop(vfsp);
3087 		nfs4_async_manager_stop(vfsp);
3088 	}
3089 
3090 	if (rtvp != NULL)
3091 		VN_RELE(rtvp);
3092 
3093 	return (error);
3094 }
3095 
3096 /*
3097  * Initialization routine for VFS routines.  Should only be called once
3098  */
3099 int
3100 nfs4_vfsinit(void)
3101 {
3102 	mutex_init(&nfs4_syncbusy, NULL, MUTEX_DEFAULT, NULL);
3103 	nfs4setclientid_init();
3104 	nfs4_ephemeral_init();
3105 	return (0);
3106 }
3107 
3108 void
3109 nfs4_vfsfini(void)
3110 {
3111 	nfs4_ephemeral_fini();
3112 	nfs4setclientid_fini();
3113 	mutex_destroy(&nfs4_syncbusy);
3114 }
3115 
3116 void
3117 nfs4_freevfs(vfs_t *vfsp)
3118 {
3119 	mntinfo4_t *mi;
3120 
3121 	/* need to release the initial hold */
3122 	mi = VFTOMI4(vfsp);
3123 	MI4_RELE(mi);
3124 }
3125 
3126 /*
3127  * Client side SETCLIENTID and SETCLIENTID_CONFIRM
3128  */
3129 struct nfs4_server nfs4_server_lst =
3130 	{ &nfs4_server_lst, &nfs4_server_lst };
3131 
3132 kmutex_t nfs4_server_lst_lock;
3133 
3134 static void
3135 nfs4setclientid_init(void)
3136 {
3137 	mutex_init(&nfs4_server_lst_lock, NULL, MUTEX_DEFAULT, NULL);
3138 }
3139 
3140 static void
3141 nfs4setclientid_fini(void)
3142 {
3143 	mutex_destroy(&nfs4_server_lst_lock);
3144 }
3145 
3146 int nfs4_retry_sclid_delay = NFS4_RETRY_SCLID_DELAY;
3147 int nfs4_num_sclid_retries = NFS4_NUM_SCLID_RETRIES;
3148 
3149 /*
3150  * Set the clientid for the server for "mi".  No-op if the clientid is
3151  * already set.
3152  *
3153  * The recovery boolean should be set to TRUE if this function was called
3154  * by the recovery code, and FALSE otherwise.  This is used to determine
3155  * if we need to call nfs4_start/end_op as well as grab the mi_recovlock
3156  * for adding a mntinfo4_t to a nfs4_server_t.
3157  *
3158  * Error is returned via 'n4ep'.  If there was a 'n4ep->stat' error, then
3159  * 'n4ep->error' is set to geterrno4(n4ep->stat).
3160  */
3161 void
3162 nfs4setclientid(mntinfo4_t *mi, cred_t *cr, bool_t recovery, nfs4_error_t *n4ep)
3163 {
3164 	struct nfs4_server *np;
3165 	struct servinfo4 *svp = mi->mi_curr_serv;
3166 	nfs4_recov_state_t recov_state;
3167 	int num_retries = 0;
3168 	bool_t retry;
3169 	cred_t *lcr = NULL;
3170 	int retry_inuse = 1; /* only retry once on NFS4ERR_CLID_INUSE */
3171 	time_t lease_time = 0;
3172 
3173 	recov_state.rs_flags = 0;
3174 	recov_state.rs_num_retry_despite_err = 0;
3175 	ASSERT(n4ep != NULL);
3176 
3177 recov_retry:
3178 	retry = FALSE;
3179 	nfs4_error_zinit(n4ep);
3180 	if (!recovery)
3181 		(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, 0);
3182 
3183 	mutex_enter(&nfs4_server_lst_lock);
3184 	np = servinfo4_to_nfs4_server(svp); /* This locks np if it is found */
3185 	mutex_exit(&nfs4_server_lst_lock);
3186 	if (!np) {
3187 		struct nfs4_server *tnp;
3188 		np = new_nfs4_server(svp, cr);
3189 		mutex_enter(&np->s_lock);
3190 
3191 		mutex_enter(&nfs4_server_lst_lock);
3192 		tnp = servinfo4_to_nfs4_server(svp);
3193 		if (tnp) {
3194 			/*
3195 			 * another thread snuck in and put server on list.
3196 			 * since we aren't adding it to the nfs4_server_list
3197 			 * we need to set the ref count to 0 and destroy it.
3198 			 */
3199 			np->s_refcnt = 0;
3200 			destroy_nfs4_server(np);
3201 			np = tnp;
3202 		} else {
3203 			/*
3204 			 * do not give list a reference until everything
3205 			 * succeeds
3206 			 */
3207 			insque(np, &nfs4_server_lst);
3208 		}
3209 		mutex_exit(&nfs4_server_lst_lock);
3210 	}
3211 	ASSERT(MUTEX_HELD(&np->s_lock));
3212 	/*
3213 	 * If we find the server already has N4S_CLIENTID_SET, then
3214 	 * just return, we've already done SETCLIENTID to that server
3215 	 */
3216 	if (np->s_flags & N4S_CLIENTID_SET) {
3217 		/* add mi to np's mntinfo4_list */
3218 		nfs4_add_mi_to_server(np, mi);
3219 		if (!recovery)
3220 			nfs_rw_exit(&mi->mi_recovlock);
3221 		mutex_exit(&np->s_lock);
3222 		nfs4_server_rele(np);
3223 		return;
3224 	}
3225 	mutex_exit(&np->s_lock);
3226 
3227 
3228 	/*
3229 	 * Drop the mi_recovlock since nfs4_start_op will
3230 	 * acquire it again for us.
3231 	 */
3232 	if (!recovery) {
3233 		nfs_rw_exit(&mi->mi_recovlock);
3234 
3235 		n4ep->error = nfs4_start_op(mi, NULL, NULL, &recov_state);
3236 		if (n4ep->error) {
3237 			nfs4_server_rele(np);
3238 			return;
3239 		}
3240 	}
3241 
3242 	mutex_enter(&np->s_lock);
3243 	while (np->s_flags & N4S_CLIENTID_PEND) {
3244 		if (!cv_wait_sig(&np->s_clientid_pend, &np->s_lock)) {
3245 			mutex_exit(&np->s_lock);
3246 			nfs4_server_rele(np);
3247 			if (!recovery)
3248 				nfs4_end_op(mi, NULL, NULL, &recov_state,
3249 				    recovery);
3250 			n4ep->error = EINTR;
3251 			return;
3252 		}
3253 	}
3254 
3255 	if (np->s_flags & N4S_CLIENTID_SET) {
3256 		/* XXX copied/pasted from above */
3257 		/* add mi to np's mntinfo4_list */
3258 		nfs4_add_mi_to_server(np, mi);
3259 		mutex_exit(&np->s_lock);
3260 		nfs4_server_rele(np);
3261 		if (!recovery)
3262 			nfs4_end_op(mi, NULL, NULL, &recov_state, recovery);
3263 		return;
3264 	}
3265 
3266 	/*
3267 	 * Reset the N4S_CB_PINGED flag. This is used to
3268 	 * indicate if we have received a CB_NULL from the
3269 	 * server. Also we reset the waiter flag.
3270 	 */
3271 	np->s_flags &= ~(N4S_CB_PINGED | N4S_CB_WAITER);
3272 	/* any failure must now clear this flag */
3273 	np->s_flags |= N4S_CLIENTID_PEND;
3274 	mutex_exit(&np->s_lock);
3275 	nfs4setclientid_otw(mi, svp, cr, np, n4ep, &retry_inuse);
3276 
3277 	if (n4ep->error == EACCES) {
3278 		/*
3279 		 * If the uid is set then set the creds for secure mounts
3280 		 * by proxy processes such as automountd.
3281 		 */
3282 		(void) nfs_rw_enter_sig(&svp->sv_lock, RW_READER, 0);
3283 		if (svp->sv_secdata->uid != 0) {
3284 			lcr = crdup(cr);
3285 			(void) crsetugid(lcr, svp->sv_secdata->uid,
3286 			    crgetgid(cr));
3287 		}
3288 		nfs_rw_exit(&svp->sv_lock);
3289 
3290 		if (lcr != NULL) {
3291 			mutex_enter(&np->s_lock);
3292 			crfree(np->s_cred);
3293 			np->s_cred = lcr;
3294 			mutex_exit(&np->s_lock);
3295 			nfs4setclientid_otw(mi, svp, lcr, np, n4ep,
3296 			    &retry_inuse);
3297 		}
3298 	}
3299 	mutex_enter(&np->s_lock);
3300 	lease_time = np->s_lease_time;
3301 	np->s_flags &= ~N4S_CLIENTID_PEND;
3302 	mutex_exit(&np->s_lock);
3303 
3304 	if (n4ep->error != 0 || n4ep->stat != NFS4_OK) {
3305 		/*
3306 		 * Start recovery if failover is a possibility.  If
3307 		 * invoked by the recovery thread itself, then just
3308 		 * return and let it handle the failover first.  NB:
3309 		 * recovery is not allowed if the mount is in progress
3310 		 * since the infrastructure is not sufficiently setup
3311 		 * to allow it.  Just return the error (after suitable
3312 		 * retries).
3313 		 */
3314 		if (FAILOVER_MOUNT4(mi) && nfs4_try_failover(n4ep)) {
3315 			(void) nfs4_start_recovery(n4ep, mi, NULL,
3316 			    NULL, NULL, NULL, OP_SETCLIENTID, NULL, NULL, NULL);
3317 			/*
3318 			 * Don't retry here, just return and let
3319 			 * recovery take over.
3320 			 */
3321 			if (recovery)
3322 				retry = FALSE;
3323 		} else if (nfs4_rpc_retry_error(n4ep->error) ||
3324 		    n4ep->stat == NFS4ERR_RESOURCE ||
3325 		    n4ep->stat == NFS4ERR_STALE_CLIENTID) {
3326 
3327 			retry = TRUE;
3328 			/*
3329 			 * Always retry if in recovery or once had
3330 			 * contact with the server (but now it's
3331 			 * overloaded).
3332 			 */
3333 			if (recovery == TRUE ||
3334 			    n4ep->error == ETIMEDOUT ||
3335 			    n4ep->error == ECONNRESET)
3336 				num_retries = 0;
3337 		} else if (retry_inuse && n4ep->error == 0 &&
3338 		    n4ep->stat == NFS4ERR_CLID_INUSE) {
3339 			retry = TRUE;
3340 			num_retries = 0;
3341 		}
3342 	} else {
3343 		/*
3344 		 * Since everything succeeded give the list a reference count if
3345 		 * it hasn't been given one by add_new_nfs4_server() or if this
3346 		 * is not a recovery situation in which case it is already on
3347 		 * the list.
3348 		 */
3349 		mutex_enter(&np->s_lock);
3350 		if ((np->s_flags & N4S_INSERTED) == 0) {
3351 			np->s_refcnt++;
3352 			np->s_flags |= N4S_INSERTED;
3353 		}
3354 		mutex_exit(&np->s_lock);
3355 	}
3356 
3357 	if (!recovery)
3358 		nfs4_end_op(mi, NULL, NULL, &recov_state, recovery);
3359 
3360 
3361 	if (retry && num_retries++ < nfs4_num_sclid_retries) {
3362 		if (retry_inuse) {
3363 			delay(SEC_TO_TICK(lease_time + nfs4_retry_sclid_delay));
3364 			retry_inuse = 0;
3365 		} else
3366 			delay(SEC_TO_TICK(nfs4_retry_sclid_delay));
3367 
3368 		nfs4_server_rele(np);
3369 		goto recov_retry;
3370 	}
3371 
3372 
3373 	if (n4ep->error == 0)
3374 		n4ep->error = geterrno4(n4ep->stat);
3375 
3376 	/* broadcast before release in case no other threads are waiting */
3377 	cv_broadcast(&np->s_clientid_pend);
3378 	nfs4_server_rele(np);
3379 }
3380 
3381 int nfs4setclientid_otw_debug = 0;
3382 
3383 /*
3384  * This function handles the recovery of STALE_CLIENTID for SETCLIENTID_CONFRIM,
3385  * but nothing else; the calling function must be designed to handle those
3386  * other errors.
3387  */
3388 static void
3389 nfs4setclientid_otw(mntinfo4_t *mi, struct servinfo4 *svp,  cred_t *cr,
3390     struct nfs4_server *np, nfs4_error_t *ep, int *retry_inusep)
3391 {
3392 	COMPOUND4args_clnt args;
3393 	COMPOUND4res_clnt res;
3394 	nfs_argop4 argop[3];
3395 	SETCLIENTID4args *s_args;
3396 	SETCLIENTID4resok *s_resok;
3397 	int doqueue = 1;
3398 	nfs4_ga_res_t *garp = NULL;
3399 	timespec_t prop_time, after_time;
3400 	verifier4 verf;
3401 	clientid4 tmp_clientid;
3402 
3403 	ASSERT(!MUTEX_HELD(&np->s_lock));
3404 
3405 	args.ctag = TAG_SETCLIENTID;
3406 
3407 	args.array = argop;
3408 	args.array_len = 3;
3409 
3410 	/* PUTROOTFH */
3411 	argop[0].argop = OP_PUTROOTFH;
3412 
3413 	/* GETATTR */
3414 	argop[1].argop = OP_GETATTR;
3415 	argop[1].nfs_argop4_u.opgetattr.attr_request = FATTR4_LEASE_TIME_MASK;
3416 	argop[1].nfs_argop4_u.opgetattr.mi = mi;
3417 
3418 	/* SETCLIENTID */
3419 	argop[2].argop = OP_SETCLIENTID;
3420 
3421 	s_args = &argop[2].nfs_argop4_u.opsetclientid;
3422 
3423 	mutex_enter(&np->s_lock);
3424 
3425 	s_args->client.verifier = np->clidtosend.verifier;
3426 	s_args->client.id_len = np->clidtosend.id_len;
3427 	ASSERT(s_args->client.id_len <= NFS4_OPAQUE_LIMIT);
3428 	s_args->client.id_val = np->clidtosend.id_val;
3429 
3430 	/*
3431 	 * Callback needs to happen on non-RDMA transport
3432 	 * Check if we have saved the original knetconfig
3433 	 * if so, use that instead.
3434 	 */
3435 	if (svp->sv_origknconf != NULL)
3436 		nfs4_cb_args(np, svp->sv_origknconf, s_args);
3437 	else
3438 		nfs4_cb_args(np, svp->sv_knconf, s_args);
3439 
3440 	mutex_exit(&np->s_lock);
3441 
3442 	rfs4call(mi, &args, &res, cr, &doqueue, 0, ep);
3443 
3444 	if (ep->error)
3445 		return;
3446 
3447 	/* getattr lease_time res */
3448 	if ((res.array_len >= 2) &&
3449 	    (res.array[1].nfs_resop4_u.opgetattr.status == NFS4_OK)) {
3450 		garp = &res.array[1].nfs_resop4_u.opgetattr.ga_res;
3451 
3452 #ifndef _LP64
3453 		/*
3454 		 * The 32 bit client cannot handle a lease time greater than
3455 		 * (INT32_MAX/1000000).  This is due to the use of the
3456 		 * lease_time in calls to drv_usectohz() in
3457 		 * nfs4_renew_lease_thread().  The problem is that
3458 		 * drv_usectohz() takes a time_t (which is just a long = 4
3459 		 * bytes) as its parameter.  The lease_time is multiplied by
3460 		 * 1000000 to convert seconds to usecs for the parameter.  If
3461 		 * a number bigger than (INT32_MAX/1000000) is used then we
3462 		 * overflow on the 32bit client.
3463 		 */
3464 		if (garp->n4g_ext_res->n4g_leasetime > (INT32_MAX/1000000)) {
3465 			garp->n4g_ext_res->n4g_leasetime = INT32_MAX/1000000;
3466 		}
3467 #endif
3468 
3469 		mutex_enter(&np->s_lock);
3470 		np->s_lease_time = garp->n4g_ext_res->n4g_leasetime;
3471 
3472 		/*
3473 		 * Keep track of the lease period for the mi's
3474 		 * mi_msg_list.  We need an appropiate time
3475 		 * bound to associate past facts with a current
3476 		 * event.  The lease period is perfect for this.
3477 		 */
3478 		mutex_enter(&mi->mi_msg_list_lock);
3479 		mi->mi_lease_period = np->s_lease_time;
3480 		mutex_exit(&mi->mi_msg_list_lock);
3481 		mutex_exit(&np->s_lock);
3482 	}
3483 
3484 
3485 	if (res.status == NFS4ERR_CLID_INUSE) {
3486 		clientaddr4 *clid_inuse;
3487 
3488 		if (!(*retry_inusep)) {
3489 			clid_inuse = &res.array->nfs_resop4_u.
3490 			    opsetclientid.SETCLIENTID4res_u.client_using;
3491 
3492 			zcmn_err(mi->mi_zone->zone_id, CE_NOTE,
3493 			    "NFS4 mount (SETCLIENTID failed)."
3494 			    "  nfs4_client_id.id is in"
3495 			    "use already by: r_netid<%s> r_addr<%s>",
3496 			    clid_inuse->r_netid, clid_inuse->r_addr);
3497 		}
3498 
3499 		/*
3500 		 * XXX - The client should be more robust in its
3501 		 * handling of clientid in use errors (regen another
3502 		 * clientid and try again?)
3503 		 */
3504 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3505 		return;
3506 	}
3507 
3508 	if (res.status) {
3509 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3510 		return;
3511 	}
3512 
3513 	s_resok = &res.array[2].nfs_resop4_u.
3514 	    opsetclientid.SETCLIENTID4res_u.resok4;
3515 
3516 	tmp_clientid = s_resok->clientid;
3517 
3518 	verf = s_resok->setclientid_confirm;
3519 
3520 #ifdef	DEBUG
3521 	if (nfs4setclientid_otw_debug) {
3522 		union {
3523 			clientid4	clientid;
3524 			int		foo[2];
3525 		} cid;
3526 
3527 		cid.clientid = s_resok->clientid;
3528 
3529 		zcmn_err(mi->mi_zone->zone_id, CE_NOTE,
3530 		"nfs4setclientid_otw: OK, clientid = %x,%x, "
3531 		"verifier = %" PRIx64 "\n", cid.foo[0], cid.foo[1], verf);
3532 	}
3533 #endif
3534 
3535 	(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3536 
3537 	/* Confirm the client id and get the lease_time attribute */
3538 
3539 	args.ctag = TAG_SETCLIENTID_CF;
3540 
3541 	args.array = argop;
3542 	args.array_len = 1;
3543 
3544 	argop[0].argop = OP_SETCLIENTID_CONFIRM;
3545 
3546 	argop[0].nfs_argop4_u.opsetclientid_confirm.clientid = tmp_clientid;
3547 	argop[0].nfs_argop4_u.opsetclientid_confirm.setclientid_confirm = verf;
3548 
3549 	/* used to figure out RTT for np */
3550 	gethrestime(&prop_time);
3551 
3552 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "nfs4setlientid_otw: "
3553 	    "start time: %ld sec %ld nsec", prop_time.tv_sec,
3554 	    prop_time.tv_nsec));
3555 
3556 	rfs4call(mi, &args, &res, cr, &doqueue, 0, ep);
3557 
3558 	gethrestime(&after_time);
3559 	mutex_enter(&np->s_lock);
3560 	np->propagation_delay.tv_sec =
3561 	    MAX(1, after_time.tv_sec - prop_time.tv_sec);
3562 	mutex_exit(&np->s_lock);
3563 
3564 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "nfs4setlcientid_otw: "
3565 	    "finish time: %ld sec ", after_time.tv_sec));
3566 
3567 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "nfs4setclientid_otw: "
3568 	    "propagation delay set to %ld sec",
3569 	    np->propagation_delay.tv_sec));
3570 
3571 	if (ep->error)
3572 		return;
3573 
3574 	if (res.status == NFS4ERR_CLID_INUSE) {
3575 		clientaddr4 *clid_inuse;
3576 
3577 		if (!(*retry_inusep)) {
3578 			clid_inuse = &res.array->nfs_resop4_u.
3579 			    opsetclientid.SETCLIENTID4res_u.client_using;
3580 
3581 			zcmn_err(mi->mi_zone->zone_id, CE_NOTE,
3582 			    "SETCLIENTID_CONFIRM failed.  "
3583 			    "nfs4_client_id.id is in use already by: "
3584 			    "r_netid<%s> r_addr<%s>",
3585 			    clid_inuse->r_netid, clid_inuse->r_addr);
3586 		}
3587 
3588 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3589 		return;
3590 	}
3591 
3592 	if (res.status) {
3593 		(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3594 		return;
3595 	}
3596 
3597 	mutex_enter(&np->s_lock);
3598 	np->clientid = tmp_clientid;
3599 	np->s_flags |= N4S_CLIENTID_SET;
3600 
3601 	/* Add mi to np's mntinfo4 list */
3602 	nfs4_add_mi_to_server(np, mi);
3603 
3604 	if (np->lease_valid == NFS4_LEASE_NOT_STARTED) {
3605 		/*
3606 		 * Start lease management thread.
3607 		 * Keep trying until we succeed.
3608 		 */
3609 
3610 		np->s_refcnt++;		/* pass reference to thread */
3611 		(void) zthread_create(NULL, 0, nfs4_renew_lease_thread, np, 0,
3612 		    minclsyspri);
3613 	}
3614 	mutex_exit(&np->s_lock);
3615 
3616 	(void) xdr_free(xdr_COMPOUND4res_clnt, (caddr_t)&res);
3617 }
3618 
3619 /*
3620  * Add mi to sp's mntinfo4_list if it isn't already in the list.  Makes
3621  * mi's clientid the same as sp's.
3622  * Assumes sp is locked down.
3623  */
3624 void
3625 nfs4_add_mi_to_server(nfs4_server_t *sp, mntinfo4_t *mi)
3626 {
3627 	mntinfo4_t *tmi;
3628 	int in_list = 0;
3629 
3630 	ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) ||
3631 	    nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
3632 	ASSERT(sp != &nfs4_server_lst);
3633 	ASSERT(MUTEX_HELD(&sp->s_lock));
3634 
3635 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3636 	    "nfs4_add_mi_to_server: add mi %p to sp %p",
3637 	    (void*)mi, (void*)sp));
3638 
3639 	for (tmi = sp->mntinfo4_list;
3640 	    tmi != NULL;
3641 	    tmi = tmi->mi_clientid_next) {
3642 		if (tmi == mi) {
3643 			NFS4_DEBUG(nfs4_client_lease_debug,
3644 			    (CE_NOTE,
3645 			    "nfs4_add_mi_to_server: mi in list"));
3646 			in_list = 1;
3647 		}
3648 	}
3649 
3650 	/*
3651 	 * First put a hold on the mntinfo4's vfsp so that references via
3652 	 * mntinfo4_list will be valid.
3653 	 */
3654 	if (!in_list)
3655 		VFS_HOLD(mi->mi_vfsp);
3656 
3657 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE, "nfs4_add_mi_to_server: "
3658 	    "hold vfs %p for mi: %p", (void*)mi->mi_vfsp, (void*)mi));
3659 
3660 	if (!in_list) {
3661 		if (sp->mntinfo4_list)
3662 			sp->mntinfo4_list->mi_clientid_prev = mi;
3663 		mi->mi_clientid_next = sp->mntinfo4_list;
3664 		mi->mi_srv = sp;
3665 		sp->mntinfo4_list = mi;
3666 		mi->mi_srvsettime = gethrestime_sec();
3667 		mi->mi_srvset_cnt++;
3668 	}
3669 
3670 	/* set mi's clientid to that of sp's for later matching */
3671 	mi->mi_clientid = sp->clientid;
3672 
3673 	/*
3674 	 * Update the clientid for any other mi's belonging to sp.  This
3675 	 * must be done here while we hold sp->s_lock, so that
3676 	 * find_nfs4_server() continues to work.
3677 	 */
3678 
3679 	for (tmi = sp->mntinfo4_list;
3680 	    tmi != NULL;
3681 	    tmi = tmi->mi_clientid_next) {
3682 		if (tmi != mi) {
3683 			tmi->mi_clientid = sp->clientid;
3684 		}
3685 	}
3686 }
3687 
3688 /*
3689  * Remove the mi from sp's mntinfo4_list and release its reference.
3690  * Exception: if mi still has open files, flag it for later removal (when
3691  * all the files are closed).
3692  *
3693  * If this is the last mntinfo4 in sp's list then tell the lease renewal
3694  * thread to exit.
3695  */
3696 static void
3697 nfs4_remove_mi_from_server_nolock(mntinfo4_t *mi, nfs4_server_t *sp)
3698 {
3699 	NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3700 	    "nfs4_remove_mi_from_server_nolock: remove mi %p from sp %p",
3701 	    (void*)mi, (void*)sp));
3702 
3703 	ASSERT(sp != NULL);
3704 	ASSERT(MUTEX_HELD(&sp->s_lock));
3705 	ASSERT(mi->mi_open_files >= 0);
3706 
3707 	/*
3708 	 * First make sure this mntinfo4 can be taken off of the list,
3709 	 * ie: it doesn't have any open files remaining.
3710 	 */
3711 	if (mi->mi_open_files > 0) {
3712 		NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3713 		    "nfs4_remove_mi_from_server_nolock: don't "
3714 		    "remove mi since it still has files open"));
3715 
3716 		mutex_enter(&mi->mi_lock);
3717 		mi->mi_flags |= MI4_REMOVE_ON_LAST_CLOSE;
3718 		mutex_exit(&mi->mi_lock);
3719 		return;
3720 	}
3721 
3722 	VFS_HOLD(mi->mi_vfsp);
3723 	remove_mi(sp, mi);
3724 	VFS_RELE(mi->mi_vfsp);
3725 
3726 	if (sp->mntinfo4_list == NULL) {
3727 		/* last fs unmounted, kill the thread */
3728 		NFS4_DEBUG(nfs4_client_lease_debug, (CE_NOTE,
3729 		    "remove_mi_from_nfs4_server_nolock: kill the thread"));
3730 		nfs4_mark_srv_dead(sp);
3731 	}
3732 }
3733 
3734 /*
3735  * Remove mi from sp's mntinfo4_list and release the vfs reference.
3736  */
3737 static void
3738 remove_mi(nfs4_server_t *sp, mntinfo4_t *mi)
3739 {
3740 	ASSERT(MUTEX_HELD(&sp->s_lock));
3741 
3742 	/*
3743 	 * We release a reference, and the caller must still have a
3744 	 * reference.
3745 	 */
3746 	ASSERT(mi->mi_vfsp->vfs_count >= 2);
3747 
3748 	if (mi->mi_clientid_prev) {
3749 		mi->mi_clientid_prev->mi_clientid_next = mi->mi_clientid_next;
3750 	} else {
3751 		/* This is the first mi in sp's mntinfo4_list */
3752 		/*
3753 		 * Make sure the first mntinfo4 in the list is the actual
3754 		 * mntinfo4 passed in.
3755 		 */
3756 		ASSERT(sp->mntinfo4_list == mi);
3757 
3758 		sp->mntinfo4_list = mi->mi_clientid_next;
3759 	}
3760 	if (mi->mi_clientid_next)
3761 		mi->mi_clientid_next->mi_clientid_prev = mi->mi_clientid_prev;
3762 
3763 	/* Now mark the mntinfo4's links as being removed */
3764 	mi->mi_clientid_prev = mi->mi_clientid_next = NULL;
3765 	mi->mi_srv = NULL;
3766 	mi->mi_srvset_cnt++;
3767 
3768 	VFS_RELE(mi->mi_vfsp);
3769 }
3770 
3771 /*
3772  * Free all the entries in sp's mntinfo4_list.
3773  */
3774 static void
3775 remove_all_mi(nfs4_server_t *sp)
3776 {
3777 	mntinfo4_t *mi;
3778 
3779 	ASSERT(MUTEX_HELD(&sp->s_lock));
3780 
3781 	while (sp->mntinfo4_list != NULL) {
3782 		mi = sp->mntinfo4_list;
3783 		/*
3784 		 * Grab a reference in case there is only one left (which
3785 		 * remove_mi() frees).
3786 		 */
3787 		VFS_HOLD(mi->mi_vfsp);
3788 		remove_mi(sp, mi);
3789 		VFS_RELE(mi->mi_vfsp);
3790 	}
3791 }
3792 
3793 /*
3794  * Remove the mi from sp's mntinfo4_list as above, and rele the vfs.
3795  *
3796  * This version can be called with a null nfs4_server_t arg,
3797  * and will either find the right one and handle locking, or
3798  * do nothing because the mi wasn't added to an sp's mntinfo4_list.
3799  */
3800 void
3801 nfs4_remove_mi_from_server(mntinfo4_t *mi, nfs4_server_t *esp)
3802 {
3803 	nfs4_server_t	*sp;
3804 
3805 	if (esp) {
3806 		nfs4_remove_mi_from_server_nolock(mi, esp);
3807 		return;
3808 	}
3809 
3810 	(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, 0);
3811 	if (sp = find_nfs4_server_all(mi, 1)) {
3812 		nfs4_remove_mi_from_server_nolock(mi, sp);
3813 		mutex_exit(&sp->s_lock);
3814 		nfs4_server_rele(sp);
3815 	}
3816 	nfs_rw_exit(&mi->mi_recovlock);
3817 }
3818 
3819 /*
3820  * Return TRUE if the given server has any non-unmounted filesystems.
3821  */
3822 
3823 bool_t
3824 nfs4_fs_active(nfs4_server_t *sp)
3825 {
3826 	mntinfo4_t *mi;
3827 
3828 	ASSERT(MUTEX_HELD(&sp->s_lock));
3829 
3830 	for (mi = sp->mntinfo4_list; mi != NULL; mi = mi->mi_clientid_next) {
3831 		if (!(mi->mi_vfsp->vfs_flag & VFS_UNMOUNTED))
3832 			return (TRUE);
3833 	}
3834 
3835 	return (FALSE);
3836 }
3837 
3838 /*
3839  * Mark sp as finished and notify any waiters.
3840  */
3841 
3842 void
3843 nfs4_mark_srv_dead(nfs4_server_t *sp)
3844 {
3845 	ASSERT(MUTEX_HELD(&sp->s_lock));
3846 
3847 	sp->s_thread_exit = NFS4_THREAD_EXIT;
3848 	cv_broadcast(&sp->cv_thread_exit);
3849 }
3850 
3851 /*
3852  * Create a new nfs4_server_t structure.
3853  * Returns new node unlocked and not in list, but with a reference count of
3854  * 1.
3855  */
3856 struct nfs4_server *
3857 new_nfs4_server(struct servinfo4 *svp, cred_t *cr)
3858 {
3859 	struct nfs4_server *np;
3860 	timespec_t tt;
3861 	union {
3862 		struct {
3863 			uint32_t sec;
3864 			uint32_t subsec;
3865 		} un_curtime;
3866 		verifier4	un_verifier;
3867 	} nfs4clientid_verifier;
3868 	/*
3869 	 * We change this ID string carefully and with the Solaris
3870 	 * NFS server behaviour in mind.  "+referrals" indicates
3871 	 * a client that can handle an NFSv4 referral.
3872 	 */
3873 	char id_val[] = "Solaris: %s, NFSv4 kernel client +referrals";
3874 	int len;
3875 
3876 	np = kmem_zalloc(sizeof (struct nfs4_server), KM_SLEEP);
3877 	np->saddr.len = svp->sv_addr.len;
3878 	np->saddr.maxlen = svp->sv_addr.maxlen;
3879 	np->saddr.buf = kmem_alloc(svp->sv_addr.maxlen, KM_SLEEP);
3880 	bcopy(svp->sv_addr.buf, np->saddr.buf, svp->sv_addr.len);
3881 	np->s_refcnt = 1;
3882 
3883 	/*
3884 	 * Build the nfs_client_id4 for this server mount.  Ensure
3885 	 * the verifier is useful and that the identification is
3886 	 * somehow based on the server's address for the case of
3887 	 * multi-homed servers.
3888 	 */
3889 	nfs4clientid_verifier.un_verifier = 0;
3890 	gethrestime(&tt);
3891 	nfs4clientid_verifier.un_curtime.sec = (uint32_t)tt.tv_sec;
3892 	nfs4clientid_verifier.un_curtime.subsec = (uint32_t)tt.tv_nsec;
3893 	np->clidtosend.verifier = nfs4clientid_verifier.un_verifier;
3894 
3895 	/*
3896 	 * calculate the length of the opaque identifier.  Subtract 2
3897 	 * for the "%s" and add the traditional +1 for null
3898 	 * termination.
3899 	 */
3900 	len = strlen(id_val) - 2 + strlen(uts_nodename()) + 1;
3901 	np->clidtosend.id_len = len + np->saddr.maxlen;
3902 
3903 	np->clidtosend.id_val = kmem_alloc(np->clidtosend.id_len, KM_SLEEP);
3904 	(void) sprintf(np->clidtosend.id_val, id_val, uts_nodename());
3905 	bcopy(np->saddr.buf, &np->clidtosend.id_val[len], np->saddr.len);
3906 
3907 	np->s_flags = 0;
3908 	np->mntinfo4_list = NULL;
3909 	/* save cred for issuing rfs4calls inside the renew thread */
3910 	crhold(cr);
3911 	np->s_cred = cr;
3912 	cv_init(&np->cv_thread_exit, NULL, CV_DEFAULT, NULL);
3913 	mutex_init(&np->s_lock, NULL, MUTEX_DEFAULT, NULL);
3914 	nfs_rw_init(&np->s_recovlock, NULL, RW_DEFAULT, NULL);
3915 	list_create(&np->s_deleg_list, sizeof (rnode4_t),
3916 	    offsetof(rnode4_t, r_deleg_link));
3917 	np->s_thread_exit = 0;
3918 	np->state_ref_count = 0;
3919 	np->lease_valid = NFS4_LEASE_NOT_STARTED;
3920 	cv_init(&np->s_cv_otw_count, NULL, CV_DEFAULT, NULL);
3921 	cv_init(&np->s_clientid_pend, NULL, CV_DEFAULT, NULL);
3922 	np->s_otw_call_count = 0;
3923 	cv_init(&np->wait_cb_null, NULL, CV_DEFAULT, NULL);
3924 	np->zoneid = getzoneid();
3925 	np->zone_globals = nfs4_get_callback_globals();
3926 	ASSERT(np->zone_globals != NULL);
3927 	return (np);
3928 }
3929 
3930 /*
3931  * Create a new nfs4_server_t structure and add it to the list.
3932  * Returns new node locked; reference must eventually be freed.
3933  */
3934 static struct nfs4_server *
3935 add_new_nfs4_server(struct servinfo4 *svp, cred_t *cr)
3936 {
3937 	nfs4_server_t *sp;
3938 
3939 	ASSERT(MUTEX_HELD(&nfs4_server_lst_lock));
3940 	sp = new_nfs4_server(svp, cr);
3941 	mutex_enter(&sp->s_lock);
3942 	insque(sp, &nfs4_server_lst);
3943 	sp->s_refcnt++;			/* list gets a reference */
3944 	sp->s_flags |= N4S_INSERTED;
3945 	sp->clientid = 0;
3946 	return (sp);
3947 }
3948 
3949 int nfs4_server_t_debug = 0;
3950 
3951 #ifdef lint
3952 extern void
3953 dumpnfs4slist(char *, mntinfo4_t *, clientid4, servinfo4_t *);
3954 #endif
3955 
3956 #ifndef lint
3957 #ifdef DEBUG
3958 void
3959 dumpnfs4slist(char *txt, mntinfo4_t *mi, clientid4 clientid, servinfo4_t *srv_p)
3960 {
3961 	int hash16(void *p, int len);
3962 	nfs4_server_t *np;
3963 
3964 	NFS4_DEBUG(nfs4_server_t_debug, (CE_NOTE,
3965 	    "dumping nfs4_server_t list in %s", txt));
3966 	NFS4_DEBUG(nfs4_server_t_debug, (CE_CONT,
3967 	    "mi 0x%p, want clientid %llx, addr %d/%04X",
3968 	    mi, (longlong_t)clientid, srv_p->sv_addr.len,
3969 	    hash16((void *)srv_p->sv_addr.buf, srv_p->sv_addr.len)));
3970 	for (np = nfs4_server_lst.forw; np != &nfs4_server_lst;
3971 	    np = np->forw) {
3972 		NFS4_DEBUG(nfs4_server_t_debug, (CE_CONT,
3973 		    "node 0x%p,    clientid %llx, addr %d/%04X, cnt %d",
3974 		    np, (longlong_t)np->clientid, np->saddr.len,
3975 		    hash16((void *)np->saddr.buf, np->saddr.len),
3976 		    np->state_ref_count));
3977 		if (np->saddr.len == srv_p->sv_addr.len &&
3978 		    bcmp(np->saddr.buf, srv_p->sv_addr.buf,
3979 		    np->saddr.len) == 0)
3980 			NFS4_DEBUG(nfs4_server_t_debug, (CE_CONT,
3981 			    " - address matches"));
3982 		if (np->clientid == clientid || np->clientid == 0)
3983 			NFS4_DEBUG(nfs4_server_t_debug, (CE_CONT,
3984 			    " - clientid matches"));
3985 		if (np->s_thread_exit != NFS4_THREAD_EXIT)
3986 			NFS4_DEBUG(nfs4_server_t_debug, (CE_CONT,
3987 			    " - thread not exiting"));
3988 	}
3989 	delay(hz);
3990 }
3991 #endif
3992 #endif
3993 
3994 
3995 /*
3996  * Move a mntinfo4_t from one server list to another.
3997  * Locking of the two nfs4_server_t nodes will be done in list order.
3998  *
3999  * Returns NULL if the current nfs4_server_t for the filesystem could not
4000  * be found (e.g., due to forced unmount).  Otherwise returns a reference
4001  * to the new nfs4_server_t, which must eventually be freed.
4002  */
4003 nfs4_server_t *
4004 nfs4_move_mi(mntinfo4_t *mi, servinfo4_t *old, servinfo4_t *new)
4005 {
4006 	nfs4_server_t *p, *op = NULL, *np = NULL;
4007 	int num_open;
4008 	zoneid_t zoneid = nfs_zoneid();
4009 
4010 	ASSERT(nfs_zone() == mi->mi_zone);
4011 
4012 	mutex_enter(&nfs4_server_lst_lock);
4013 #ifdef DEBUG
4014 	if (nfs4_server_t_debug)
4015 		dumpnfs4slist("nfs4_move_mi", mi, (clientid4)0, new);
4016 #endif
4017 	for (p = nfs4_server_lst.forw; p != &nfs4_server_lst; p = p->forw) {
4018 		if (p->zoneid != zoneid)
4019 			continue;
4020 		if (p->saddr.len == old->sv_addr.len &&
4021 		    bcmp(p->saddr.buf, old->sv_addr.buf, p->saddr.len) == 0 &&
4022 		    p->s_thread_exit != NFS4_THREAD_EXIT) {
4023 			op = p;
4024 			mutex_enter(&op->s_lock);
4025 			op->s_refcnt++;
4026 		}
4027 		if (p->saddr.len == new->sv_addr.len &&
4028 		    bcmp(p->saddr.buf, new->sv_addr.buf, p->saddr.len) == 0 &&
4029 		    p->s_thread_exit != NFS4_THREAD_EXIT) {
4030 			np = p;
4031 			mutex_enter(&np->s_lock);
4032 		}
4033 		if (op != NULL && np != NULL)
4034 			break;
4035 	}
4036 	if (op == NULL) {
4037 		/*
4038 		 * Filesystem has been forcibly unmounted.  Bail out.
4039 		 */
4040 		if (np != NULL)
4041 			mutex_exit(&np->s_lock);
4042 		mutex_exit(&nfs4_server_lst_lock);
4043 		return (NULL);
4044 	}
4045 	if (np != NULL) {
4046 		np->s_refcnt++;
4047 	} else {
4048 #ifdef DEBUG
4049 		NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
4050 		    "nfs4_move_mi: no target nfs4_server, will create."));
4051 #endif
4052 		np = add_new_nfs4_server(new, kcred);
4053 	}
4054 	mutex_exit(&nfs4_server_lst_lock);
4055 
4056 	NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
4057 	    "nfs4_move_mi: for mi 0x%p, "
4058 	    "old servinfo4 0x%p, new servinfo4 0x%p, "
4059 	    "old nfs4_server 0x%p, new nfs4_server 0x%p, ",
4060 	    (void*)mi, (void*)old, (void*)new,
4061 	    (void*)op, (void*)np));
4062 	ASSERT(op != NULL && np != NULL);
4063 
4064 	/* discard any delegations */
4065 	nfs4_deleg_discard(mi, op);
4066 
4067 	num_open = mi->mi_open_files;
4068 	mi->mi_open_files = 0;
4069 	op->state_ref_count -= num_open;
4070 	ASSERT(op->state_ref_count >= 0);
4071 	np->state_ref_count += num_open;
4072 	nfs4_remove_mi_from_server_nolock(mi, op);
4073 	mi->mi_open_files = num_open;
4074 	NFS4_DEBUG(nfs4_client_failover_debug, (CE_NOTE,
4075 	    "nfs4_move_mi: mi_open_files %d, op->cnt %d, np->cnt %d",
4076 	    mi->mi_open_files, op->state_ref_count, np->state_ref_count));
4077 
4078 	nfs4_add_mi_to_server(np, mi);
4079 
4080 	mutex_exit(&op->s_lock);
4081 	mutex_exit(&np->s_lock);
4082 	nfs4_server_rele(op);
4083 
4084 	return (np);
4085 }
4086 
4087 /*
4088  * Need to have the nfs4_server_lst_lock.
4089  * Search the nfs4_server list to find a match on this servinfo4
4090  * based on its address.
4091  *
4092  * Returns NULL if no match is found.  Otherwise returns a reference (which
4093  * must eventually be freed) to a locked nfs4_server.
4094  */
4095 nfs4_server_t *
4096 servinfo4_to_nfs4_server(servinfo4_t *srv_p)
4097 {
4098 	nfs4_server_t *np;
4099 	zoneid_t zoneid = nfs_zoneid();
4100 
4101 	ASSERT(MUTEX_HELD(&nfs4_server_lst_lock));
4102 	for (np = nfs4_server_lst.forw; np != &nfs4_server_lst; np = np->forw) {
4103 		if (np->zoneid == zoneid &&
4104 		    np->saddr.len == srv_p->sv_addr.len &&
4105 		    bcmp(np->saddr.buf, srv_p->sv_addr.buf,
4106 		    np->saddr.len) == 0 &&
4107 		    np->s_thread_exit != NFS4_THREAD_EXIT) {
4108 			mutex_enter(&np->s_lock);
4109 			np->s_refcnt++;
4110 			return (np);
4111 		}
4112 	}
4113 	return (NULL);
4114 }
4115 
4116 /*
4117  * Locks the nfs4_server down if it is found and returns a reference that
4118  * must eventually be freed.
4119  */
4120 static nfs4_server_t *
4121 lookup_nfs4_server(nfs4_server_t *sp, int any_state)
4122 {
4123 	nfs4_server_t *np;
4124 
4125 	mutex_enter(&nfs4_server_lst_lock);
4126 	for (np = nfs4_server_lst.forw; np != &nfs4_server_lst; np = np->forw) {
4127 		mutex_enter(&np->s_lock);
4128 		if (np == sp && np->s_refcnt > 0 &&
4129 		    (np->s_thread_exit != NFS4_THREAD_EXIT || any_state)) {
4130 			mutex_exit(&nfs4_server_lst_lock);
4131 			np->s_refcnt++;
4132 			return (np);
4133 		}
4134 		mutex_exit(&np->s_lock);
4135 	}
4136 	mutex_exit(&nfs4_server_lst_lock);
4137 
4138 	return (NULL);
4139 }
4140 
4141 /*
4142  * The caller should be holding mi->mi_recovlock, and it should continue to
4143  * hold the lock until done with the returned nfs4_server_t.  Once
4144  * mi->mi_recovlock is released, there is no guarantee that the returned
4145  * mi->nfs4_server_t will continue to correspond to mi.
4146  */
4147 nfs4_server_t *
4148 find_nfs4_server(mntinfo4_t *mi)
4149 {
4150 	ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) ||
4151 	    nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
4152 
4153 	return (lookup_nfs4_server(mi->mi_srv, 0));
4154 }
4155 
4156 /*
4157  * Same as above, but takes an "any_state" parameter which can be
4158  * set to 1 if the caller wishes to find nfs4_server_t's which
4159  * have been marked for termination by the exit of the renew
4160  * thread.  This should only be used by operations which are
4161  * cleaning up and will not cause an OTW op.
4162  */
4163 nfs4_server_t *
4164 find_nfs4_server_all(mntinfo4_t *mi, int any_state)
4165 {
4166 	ASSERT(nfs_rw_lock_held(&mi->mi_recovlock, RW_READER) ||
4167 	    nfs_rw_lock_held(&mi->mi_recovlock, RW_WRITER));
4168 
4169 	return (lookup_nfs4_server(mi->mi_srv, any_state));
4170 }
4171 
4172 /*
4173  * Lock sp, but only if it's still active (in the list and hasn't been
4174  * flagged as exiting) or 'any_state' is non-zero.
4175  * Returns TRUE if sp got locked and adds a reference to sp.
4176  */
4177 bool_t
4178 nfs4_server_vlock(nfs4_server_t *sp, int any_state)
4179 {
4180 	return (lookup_nfs4_server(sp, any_state) != NULL);
4181 }
4182 
4183 /*
4184  * Release the reference to sp and destroy it if that's the last one.
4185  */
4186 
4187 void
4188 nfs4_server_rele(nfs4_server_t *sp)
4189 {
4190 	mutex_enter(&sp->s_lock);
4191 	ASSERT(sp->s_refcnt > 0);
4192 	sp->s_refcnt--;
4193 	if (sp->s_refcnt > 0) {
4194 		mutex_exit(&sp->s_lock);
4195 		return;
4196 	}
4197 	mutex_exit(&sp->s_lock);
4198 
4199 	mutex_enter(&nfs4_server_lst_lock);
4200 	mutex_enter(&sp->s_lock);
4201 	if (sp->s_refcnt > 0) {
4202 		mutex_exit(&sp->s_lock);
4203 		mutex_exit(&nfs4_server_lst_lock);
4204 		return;
4205 	}
4206 	remque(sp);
4207 	sp->forw = sp->back = NULL;
4208 	mutex_exit(&nfs4_server_lst_lock);
4209 	destroy_nfs4_server(sp);
4210 }
4211 
4212 static void
4213 destroy_nfs4_server(nfs4_server_t *sp)
4214 {
4215 	ASSERT(MUTEX_HELD(&sp->s_lock));
4216 	ASSERT(sp->s_refcnt == 0);
4217 	ASSERT(sp->s_otw_call_count == 0);
4218 
4219 	remove_all_mi(sp);
4220 
4221 	crfree(sp->s_cred);
4222 	kmem_free(sp->saddr.buf, sp->saddr.maxlen);
4223 	kmem_free(sp->clidtosend.id_val, sp->clidtosend.id_len);
4224 	mutex_exit(&sp->s_lock);
4225 
4226 	/* destroy the nfs4_server */
4227 	nfs4callback_destroy(sp);
4228 	list_destroy(&sp->s_deleg_list);
4229 	mutex_destroy(&sp->s_lock);
4230 	cv_destroy(&sp->cv_thread_exit);
4231 	cv_destroy(&sp->s_cv_otw_count);
4232 	cv_destroy(&sp->s_clientid_pend);
4233 	cv_destroy(&sp->wait_cb_null);
4234 	nfs_rw_destroy(&sp->s_recovlock);
4235 	kmem_free(sp, sizeof (*sp));
4236 }
4237 
4238 /*
4239  * Fork off a thread to free the data structures for a mount.
4240  */
4241 
4242 static void
4243 async_free_mount(vfs_t *vfsp, int flag, cred_t *cr)
4244 {
4245 	freemountargs_t *args;
4246 	args = kmem_alloc(sizeof (freemountargs_t), KM_SLEEP);
4247 	args->fm_vfsp = vfsp;
4248 	VFS_HOLD(vfsp);
4249 	MI4_HOLD(VFTOMI4(vfsp));
4250 	args->fm_flag = flag;
4251 	args->fm_cr = cr;
4252 	crhold(cr);
4253 	(void) zthread_create(NULL, 0, nfs4_free_mount_thread, args, 0,
4254 	    minclsyspri);
4255 }
4256 
4257 static void
4258 nfs4_free_mount_thread(freemountargs_t *args)
4259 {
4260 	mntinfo4_t *mi;
4261 	nfs4_free_mount(args->fm_vfsp, args->fm_flag, args->fm_cr);
4262 	mi = VFTOMI4(args->fm_vfsp);
4263 	crfree(args->fm_cr);
4264 	VFS_RELE(args->fm_vfsp);
4265 	MI4_RELE(mi);
4266 	kmem_free(args, sizeof (freemountargs_t));
4267 	zthread_exit();
4268 	/* NOTREACHED */
4269 }
4270 
4271 /*
4272  * Thread to free the data structures for a given filesystem.
4273  */
4274 static void
4275 nfs4_free_mount(vfs_t *vfsp, int flag, cred_t *cr)
4276 {
4277 	mntinfo4_t		*mi = VFTOMI4(vfsp);
4278 	nfs4_server_t		*sp;
4279 	callb_cpr_t		cpr_info;
4280 	kmutex_t		cpr_lock;
4281 	boolean_t		async_thread;
4282 	int			removed;
4283 
4284 	bool_t			must_unlock;
4285 	nfs4_ephemeral_tree_t	*eph_tree;
4286 
4287 	/*
4288 	 * We need to participate in the CPR framework if this is a kernel
4289 	 * thread.
4290 	 */
4291 	async_thread = (curproc == nfs_zone()->zone_zsched);
4292 	if (async_thread) {
4293 		mutex_init(&cpr_lock, NULL, MUTEX_DEFAULT, NULL);
4294 		CALLB_CPR_INIT(&cpr_info, &cpr_lock, callb_generic_cpr,
4295 		    "nfsv4AsyncUnmount");
4296 	}
4297 
4298 	/*
4299 	 * We need to wait for all outstanding OTW calls
4300 	 * and recovery to finish before we remove the mi
4301 	 * from the nfs4_server_t, as current pending
4302 	 * calls might still need this linkage (in order
4303 	 * to find a nfs4_server_t from a mntinfo4_t).
4304 	 */
4305 	(void) nfs_rw_enter_sig(&mi->mi_recovlock, RW_READER, FALSE);
4306 	sp = find_nfs4_server(mi);
4307 	nfs_rw_exit(&mi->mi_recovlock);
4308 
4309 	if (sp) {
4310 		while (sp->s_otw_call_count != 0) {
4311 			if (async_thread) {
4312 				mutex_enter(&cpr_lock);
4313 				CALLB_CPR_SAFE_BEGIN(&cpr_info);
4314 				mutex_exit(&cpr_lock);
4315 			}
4316 			cv_wait(&sp->s_cv_otw_count, &sp->s_lock);
4317 			if (async_thread) {
4318 				mutex_enter(&cpr_lock);
4319 				CALLB_CPR_SAFE_END(&cpr_info, &cpr_lock);
4320 				mutex_exit(&cpr_lock);
4321 			}
4322 		}
4323 		mutex_exit(&sp->s_lock);
4324 		nfs4_server_rele(sp);
4325 		sp = NULL;
4326 	}
4327 
4328 	mutex_enter(&mi->mi_lock);
4329 	while (mi->mi_in_recovery != 0) {
4330 		if (async_thread) {
4331 			mutex_enter(&cpr_lock);
4332 			CALLB_CPR_SAFE_BEGIN(&cpr_info);
4333 			mutex_exit(&cpr_lock);
4334 		}
4335 		cv_wait(&mi->mi_cv_in_recov, &mi->mi_lock);
4336 		if (async_thread) {
4337 			mutex_enter(&cpr_lock);
4338 			CALLB_CPR_SAFE_END(&cpr_info, &cpr_lock);
4339 			mutex_exit(&cpr_lock);
4340 		}
4341 	}
4342 	mutex_exit(&mi->mi_lock);
4343 
4344 	/*
4345 	 * If we got an error, then do not nuke the
4346 	 * tree. Either the harvester is busy reclaiming
4347 	 * this node or we ran into some busy condition.
4348 	 *
4349 	 * The harvester will eventually come along and cleanup.
4350 	 * The only problem would be the root mount point.
4351 	 *
4352 	 * Since the busy node can occur for a variety
4353 	 * of reasons and can result in an entry staying
4354 	 * in df output but no longer accessible from the
4355 	 * directory tree, we are okay.
4356 	 */
4357 	if (!nfs4_ephemeral_umount(mi, flag, cr,
4358 	    &must_unlock, &eph_tree))
4359 		nfs4_ephemeral_umount_activate(mi, &must_unlock,
4360 		    &eph_tree);
4361 
4362 	/*
4363 	 * The original purge of the dnlc via 'dounmount'
4364 	 * doesn't guarantee that another dnlc entry was not
4365 	 * added while we waitied for all outstanding OTW
4366 	 * and recovery calls to finish.  So re-purge the
4367 	 * dnlc now.
4368 	 */
4369 	(void) dnlc_purge_vfsp(vfsp, 0);
4370 
4371 	/*
4372 	 * We need to explicitly stop the manager thread; the asyc worker
4373 	 * threads can timeout and exit on their own.
4374 	 */
4375 	mutex_enter(&mi->mi_async_lock);
4376 	mi->mi_max_threads = 0;
4377 	cv_broadcast(&mi->mi_async_work_cv);
4378 	mutex_exit(&mi->mi_async_lock);
4379 	if (mi->mi_manager_thread)
4380 		nfs4_async_manager_stop(vfsp);
4381 
4382 	destroy_rtable4(vfsp, cr);
4383 
4384 	nfs4_remove_mi_from_server(mi, NULL);
4385 
4386 	if (async_thread) {
4387 		mutex_enter(&cpr_lock);
4388 		CALLB_CPR_EXIT(&cpr_info);	/* drops cpr_lock */
4389 		mutex_destroy(&cpr_lock);
4390 	}
4391 
4392 	removed = nfs4_mi_zonelist_remove(mi);
4393 	if (removed)
4394 		zone_rele(mi->mi_zone);
4395 }
4396 
4397 /* Referral related sub-routines */
4398 
4399 /* Freeup knetconfig */
4400 static void
4401 free_knconf_contents(struct knetconfig *k)
4402 {
4403 	if (k == NULL)
4404 		return;
4405 	if (k->knc_protofmly)
4406 		kmem_free(k->knc_protofmly, KNC_STRSIZE);
4407 	if (k->knc_proto)
4408 		kmem_free(k->knc_proto, KNC_STRSIZE);
4409 }
4410 
4411 /*
4412  * This updates newpath variable with exact name component from the
4413  * path which gave us a NFS4ERR_MOVED error.
4414  * If the path is /rp/aaa/bbb and nth value is 1, aaa is returned.
4415  */
4416 static char *
4417 extract_referral_point(const char *svp, int nth)
4418 {
4419 	int num_slashes = 0;
4420 	const char *p;
4421 	char *newpath = NULL;
4422 	int i = 0;
4423 
4424 	newpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
4425 	for (p = svp; *p; p++) {
4426 		if (*p == '/')
4427 			num_slashes++;
4428 		if (num_slashes == nth + 1) {
4429 			p++;
4430 			while (*p != '/') {
4431 				if (*p == '\0')
4432 					break;
4433 				newpath[i] = *p;
4434 				i++;
4435 				p++;
4436 			}
4437 			newpath[i++] = '\0';
4438 			break;
4439 		}
4440 	}
4441 	return (newpath);
4442 }
4443 
4444 /*
4445  * This sets up a new path in sv_path to do a lookup of the referral point.
4446  * If the path is /rp/aaa/bbb and the referral point is aaa,
4447  * this updates /rp/aaa. This path will be used to get referral
4448  * location.
4449  */
4450 static void
4451 setup_newsvpath(servinfo4_t *svp, int nth)
4452 {
4453 	int num_slashes = 0, pathlen, i = 0;
4454 	char *newpath, *p;
4455 
4456 	newpath = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
4457 	for (p = svp->sv_path; *p; p++) {
4458 		newpath[i] =  *p;
4459 		if (*p == '/')
4460 			num_slashes++;
4461 		if (num_slashes == nth + 1) {
4462 			newpath[i] = '\0';
4463 			pathlen = strlen(newpath) + 1;
4464 			kmem_free(svp->sv_path, svp->sv_pathlen);
4465 			svp->sv_path = kmem_alloc(pathlen, KM_SLEEP);
4466 			svp->sv_pathlen = pathlen;
4467 			bcopy(newpath, svp->sv_path, pathlen);
4468 			break;
4469 		}
4470 		i++;
4471 	}
4472 	kmem_free(newpath, MAXPATHLEN);
4473 }
4474