17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2245916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
287c478bd9Sstevel@tonic-gate  *	All rights reserved.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
387c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
397c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
407c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
417c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
427c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
437c478bd9Sstevel@tonic-gate #include <sys/mount.h>
447c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
457c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
467c478bd9Sstevel@tonic-gate #include <sys/errno.h>
477c478bd9Sstevel@tonic-gate #include <sys/debug.h>
487c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
497c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
507c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
517c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
527c478bd9Sstevel@tonic-gate #include <sys/acl.h>
537c478bd9Sstevel@tonic-gate #include <sys/flock.h>
547c478bd9Sstevel@tonic-gate #include <sys/policy.h>
557c478bd9Sstevel@tonic-gate #include <sys/zone.h>
567c478bd9Sstevel@tonic-gate #include <sys/class.h>
577c478bd9Sstevel@tonic-gate #include <sys/socket.h>
587c478bd9Sstevel@tonic-gate #include <sys/netconfig.h>
5945916cd2Sjpk #include <sys/tsol/tnet.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <rpc/types.h>
627c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
637c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
667c478bd9Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
677c478bd9Sstevel@tonic-gate #include <nfs/rnode.h>
687c478bd9Sstevel@tonic-gate #include <nfs/mount.h>
697c478bd9Sstevel@tonic-gate #include <nfs/nfs_acl.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * From rpcsec module (common/rpcsec).
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate extern int sec_clnt_loadinfo(struct sec_data *, struct sec_data **, model_t);
777c478bd9Sstevel@tonic-gate extern void sec_clnt_freeinfo(struct sec_data *);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * The order and contents of this structure must be kept in sync with that of
817c478bd9Sstevel@tonic-gate  * rfsreqcnt_v3_tmpl in nfs_stats.c
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate static char *rfsnames_v3[] = {
847c478bd9Sstevel@tonic-gate 	"null", "getattr", "setattr", "lookup", "access", "readlink", "read",
857c478bd9Sstevel@tonic-gate 	"write", "create", "mkdir", "symlink", "mknod", "remove", "rmdir",
867c478bd9Sstevel@tonic-gate 	"rename", "link", "readdir", "readdirplus", "fsstat", "fsinfo",
877c478bd9Sstevel@tonic-gate 	"pathconf", "commit"
887c478bd9Sstevel@tonic-gate };
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * This table maps from NFS protocol number into call type.
927c478bd9Sstevel@tonic-gate  * Zero means a "Lookup" type call
937c478bd9Sstevel@tonic-gate  * One  means a "Read" type call
947c478bd9Sstevel@tonic-gate  * Two  means a "Write" type call
957c478bd9Sstevel@tonic-gate  * This is used to select a default time-out.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate static uchar_t call_type_v3[] = {
987c478bd9Sstevel@tonic-gate 	0, 0, 1, 0, 0, 0, 1,
997c478bd9Sstevel@tonic-gate 	2, 2, 2, 2, 2, 2, 2,
1007c478bd9Sstevel@tonic-gate 	2, 2, 1, 2, 0, 0, 0,
1017c478bd9Sstevel@tonic-gate 	2 };
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * Similar table, but to determine which timer to use
1057c478bd9Sstevel@tonic-gate  * (only real reads and writes!)
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate static uchar_t timer_type_v3[] = {
1087c478bd9Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0, 1,
1097c478bd9Sstevel@tonic-gate 	2, 0, 0, 0, 0, 0, 0,
1107c478bd9Sstevel@tonic-gate 	0, 0, 1, 1, 0, 0, 0,
1117c478bd9Sstevel@tonic-gate 	0 };
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * This table maps from NFS protocol number into a call type
1157c478bd9Sstevel@tonic-gate  * for the semisoft mount option.
1167c478bd9Sstevel@tonic-gate  * Zero means do not repeat operation.
1177c478bd9Sstevel@tonic-gate  * One  means repeat.
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate static uchar_t ss_call_type_v3[] = {
1207c478bd9Sstevel@tonic-gate 	0, 0, 1, 0, 0, 0, 0,
1217c478bd9Sstevel@tonic-gate 	1, 1, 1, 1, 1, 1, 1,
1227c478bd9Sstevel@tonic-gate 	1, 1, 0, 0, 0, 0, 0,
1237c478bd9Sstevel@tonic-gate 	1 };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * nfs3 vfs operations.
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate static int	nfs3_mount(vfs_t *, vnode_t *, struct mounta *, cred_t *);
1297c478bd9Sstevel@tonic-gate static int	nfs3_unmount(vfs_t *, int, cred_t *);
1307c478bd9Sstevel@tonic-gate static int	nfs3_root(vfs_t *, vnode_t **);
1317c478bd9Sstevel@tonic-gate static int	nfs3_statvfs(vfs_t *, struct statvfs64 *);
1327c478bd9Sstevel@tonic-gate static int	nfs3_sync(vfs_t *, short, cred_t *);
1337c478bd9Sstevel@tonic-gate static int	nfs3_vget(vfs_t *, vnode_t **, fid_t *);
1347c478bd9Sstevel@tonic-gate static int	nfs3_mountroot(vfs_t *, whymountroot_t);
1357c478bd9Sstevel@tonic-gate static void	nfs3_freevfs(vfs_t *);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static int	nfs3rootvp(vnode_t **, vfs_t *, struct servinfo *,
1387c478bd9Sstevel@tonic-gate 		    int, cred_t *, zone_t *);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate  * Initialize the vfs structure
1427c478bd9Sstevel@tonic-gate  */
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate static int nfs3fstyp;
1457c478bd9Sstevel@tonic-gate vfsops_t *nfs3_vfsops;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * Debug variable to check for rdma based
1497c478bd9Sstevel@tonic-gate  * transport startup and cleanup. Controlled
1507c478bd9Sstevel@tonic-gate  * through /etc/system. Off by default.
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate extern int rdma_debug;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate int
1557c478bd9Sstevel@tonic-gate nfs3init(int fstyp, char *name)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t nfs3_vfsops_template[] = {
1587c478bd9Sstevel@tonic-gate 		VFSNAME_MOUNT, nfs3_mount,
1597c478bd9Sstevel@tonic-gate 		VFSNAME_UNMOUNT, nfs3_unmount,
1607c478bd9Sstevel@tonic-gate 		VFSNAME_ROOT, nfs3_root,
1617c478bd9Sstevel@tonic-gate 		VFSNAME_STATVFS, nfs3_statvfs,
1627c478bd9Sstevel@tonic-gate 		VFSNAME_SYNC, (fs_generic_func_p) nfs3_sync,
1637c478bd9Sstevel@tonic-gate 		VFSNAME_VGET, nfs3_vget,
1647c478bd9Sstevel@tonic-gate 		VFSNAME_MOUNTROOT, nfs3_mountroot,
1657c478bd9Sstevel@tonic-gate 		VFSNAME_FREEVFS, (fs_generic_func_p)nfs3_freevfs,
1667c478bd9Sstevel@tonic-gate 		NULL, NULL
1677c478bd9Sstevel@tonic-gate 	};
1687c478bd9Sstevel@tonic-gate 	int error;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstyp, nfs3_vfsops_template, &nfs3_vfsops);
1717c478bd9Sstevel@tonic-gate 	if (error != 0) {
1727c478bd9Sstevel@tonic-gate 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
1737c478bd9Sstevel@tonic-gate 		    "nfs3init: bad vfs ops template");
1747c478bd9Sstevel@tonic-gate 		return (error);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	error = vn_make_ops(name, nfs3_vnodeops_template, &nfs3_vnodeops);
1787c478bd9Sstevel@tonic-gate 	if (error != 0) {
1797c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstyp);
1807c478bd9Sstevel@tonic-gate 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
1817c478bd9Sstevel@tonic-gate 		    "nfs3init: bad vnode ops template");
1827c478bd9Sstevel@tonic-gate 		return (error);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	nfs3fstyp = fstyp;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	return (0);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate void
1917c478bd9Sstevel@tonic-gate nfs3fini(void)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
195*39d3e169Sevanl static void
196*39d3e169Sevanl nfs3_free_args(struct nfs_args *nargs, nfs_fhandle *fh)
197*39d3e169Sevanl {
198*39d3e169Sevanl 
199*39d3e169Sevanl 	if (fh)
200*39d3e169Sevanl 		kmem_free(fh, sizeof (*fh));
201*39d3e169Sevanl 
202*39d3e169Sevanl 	if (nargs->knconf) {
203*39d3e169Sevanl 		if (nargs->knconf->knc_protofmly)
204*39d3e169Sevanl 			kmem_free(nargs->knconf->knc_protofmly,
205*39d3e169Sevanl 				KNC_STRSIZE);
206*39d3e169Sevanl 	if (nargs->knconf->knc_proto)
207*39d3e169Sevanl 		kmem_free(nargs->knconf->knc_proto, KNC_STRSIZE);
208*39d3e169Sevanl 		kmem_free(nargs->knconf, sizeof (*nargs->knconf));
209*39d3e169Sevanl 		nargs->knconf = NULL;
210*39d3e169Sevanl 	}
211*39d3e169Sevanl 
212*39d3e169Sevanl 	if (nargs->fh) {
213*39d3e169Sevanl 		kmem_free(nargs->fh, strlen(nargs->fh) + 1);
214*39d3e169Sevanl 		nargs->fh = NULL;
215*39d3e169Sevanl 	}
216*39d3e169Sevanl 
217*39d3e169Sevanl 	if (nargs->hostname) {
218*39d3e169Sevanl 		kmem_free(nargs->hostname, strlen(nargs->hostname) + 1);
219*39d3e169Sevanl 		nargs->hostname = NULL;
220*39d3e169Sevanl 	}
221*39d3e169Sevanl 
222*39d3e169Sevanl 	if (nargs->addr) {
223*39d3e169Sevanl 		if (nargs->addr->buf) {
224*39d3e169Sevanl 			ASSERT(nargs->addr->len);
225*39d3e169Sevanl 			kmem_free(nargs->addr->buf, nargs->addr->len);
226*39d3e169Sevanl 		}
227*39d3e169Sevanl 		kmem_free(nargs->addr, sizeof (struct netbuf));
228*39d3e169Sevanl 		nargs->addr = NULL;
229*39d3e169Sevanl 	}
230*39d3e169Sevanl 
231*39d3e169Sevanl 	if (nargs->syncaddr) {
232*39d3e169Sevanl 		ASSERT(nargs->syncaddr->len);
233*39d3e169Sevanl 		if (nargs->syncaddr->buf) {
234*39d3e169Sevanl 			ASSERT(nargs->syncaddr->len);
235*39d3e169Sevanl 			kmem_free(nargs->syncaddr->buf, nargs->syncaddr->len);
236*39d3e169Sevanl 		}
237*39d3e169Sevanl 		kmem_free(nargs->syncaddr, sizeof (struct netbuf));
238*39d3e169Sevanl 		nargs->syncaddr = NULL;
239*39d3e169Sevanl 	}
240*39d3e169Sevanl 
241*39d3e169Sevanl 	if (nargs->netname) {
242*39d3e169Sevanl 		kmem_free(nargs->netname, strlen(nargs->netname) + 1);
243*39d3e169Sevanl 		nargs->netname = NULL;
244*39d3e169Sevanl 	}
245*39d3e169Sevanl 
246*39d3e169Sevanl 	if (nargs->nfs_ext_u.nfs_extA.secdata) {
247*39d3e169Sevanl 		sec_clnt_freeinfo(nargs->nfs_ext_u.nfs_extA.secdata);
248*39d3e169Sevanl 		nargs->nfs_ext_u.nfs_extA.secdata = NULL;
249*39d3e169Sevanl 	}
250*39d3e169Sevanl }
251*39d3e169Sevanl 
252*39d3e169Sevanl static int
253*39d3e169Sevanl nfs3_copyin(char *data, int datalen, struct nfs_args *nargs, nfs_fhandle *fh)
254*39d3e169Sevanl {
255*39d3e169Sevanl 
256*39d3e169Sevanl 	int error;
257*39d3e169Sevanl 	size_t nlen;			/* length of netname */
258*39d3e169Sevanl 	size_t hlen;			/* length of hostname */
259*39d3e169Sevanl 	char netname[MAXNETNAMELEN+1];  /* server's netname */
260*39d3e169Sevanl 	struct netbuf addr;		/* server's address */
261*39d3e169Sevanl 	struct netbuf syncaddr;		/* AUTH_DES time sync addr */
262*39d3e169Sevanl 	struct knetconfig *knconf;	/* transport knetconfig structure */
263*39d3e169Sevanl 	struct sec_data *secdata = NULL;	/* security data */
264*39d3e169Sevanl 	STRUCT_DECL(nfs_args, args);    	/* nfs mount arguments */
265*39d3e169Sevanl 	STRUCT_DECL(knetconfig, knconf_tmp);
266*39d3e169Sevanl 	STRUCT_DECL(netbuf, addr_tmp);
267*39d3e169Sevanl 	int flags;
268*39d3e169Sevanl 	char *p, *pf;
269*39d3e169Sevanl 	char *userbufptr;
270*39d3e169Sevanl 
271*39d3e169Sevanl 
272*39d3e169Sevanl 	bzero(nargs, sizeof (*nargs));
273*39d3e169Sevanl 
274*39d3e169Sevanl 	STRUCT_INIT(args, get_udatamodel());
275*39d3e169Sevanl 	bzero(STRUCT_BUF(args), SIZEOF_STRUCT(nfs_args, DATAMODEL_NATIVE));
276*39d3e169Sevanl 	if (copyin(data, STRUCT_BUF(args), MIN(datalen, STRUCT_SIZE(args))))
277*39d3e169Sevanl 		return (EFAULT);
278*39d3e169Sevanl 
279*39d3e169Sevanl 	nargs->wsize = STRUCT_FGET(args, wsize);
280*39d3e169Sevanl 	nargs->rsize = STRUCT_FGET(args, rsize);
281*39d3e169Sevanl 	nargs->timeo = STRUCT_FGET(args, timeo);
282*39d3e169Sevanl 	nargs->retrans = STRUCT_FGET(args, retrans);
283*39d3e169Sevanl 	nargs->acregmin = STRUCT_FGET(args, acregmin);
284*39d3e169Sevanl 	nargs->acregmax = STRUCT_FGET(args, acregmax);
285*39d3e169Sevanl 	nargs->acdirmin = STRUCT_FGET(args, acdirmin);
286*39d3e169Sevanl 	nargs->acdirmax = STRUCT_FGET(args, acdirmax);
287*39d3e169Sevanl 
288*39d3e169Sevanl 	flags = STRUCT_FGET(args, flags);
289*39d3e169Sevanl 	nargs->flags = flags;
290*39d3e169Sevanl 
291*39d3e169Sevanl 	addr.buf = NULL;
292*39d3e169Sevanl 	syncaddr.buf = NULL;
293*39d3e169Sevanl 
294*39d3e169Sevanl 	/*
295*39d3e169Sevanl 	 * Allocate space for a knetconfig structure and
296*39d3e169Sevanl 	 * its strings and copy in from user-land.
297*39d3e169Sevanl 	 */
298*39d3e169Sevanl 	knconf = kmem_zalloc(sizeof (*knconf), KM_SLEEP);
299*39d3e169Sevanl 	STRUCT_INIT(knconf_tmp, get_udatamodel());
300*39d3e169Sevanl 	if (copyin(STRUCT_FGETP(args, knconf), STRUCT_BUF(knconf_tmp),
301*39d3e169Sevanl 		STRUCT_SIZE(knconf_tmp))) {
302*39d3e169Sevanl 		kmem_free(knconf, sizeof (*knconf));
303*39d3e169Sevanl 		return (EFAULT);
304*39d3e169Sevanl 	}
305*39d3e169Sevanl 
306*39d3e169Sevanl 	knconf->knc_semantics = STRUCT_FGET(knconf_tmp, knc_semantics);
307*39d3e169Sevanl 	knconf->knc_protofmly = STRUCT_FGETP(knconf_tmp, knc_protofmly);
308*39d3e169Sevanl 	knconf->knc_proto = STRUCT_FGETP(knconf_tmp, knc_proto);
309*39d3e169Sevanl 	if (get_udatamodel() != DATAMODEL_LP64) {
310*39d3e169Sevanl 		knconf->knc_rdev = expldev(STRUCT_FGET(knconf_tmp, knc_rdev));
311*39d3e169Sevanl 	} else {
312*39d3e169Sevanl 		knconf->knc_rdev = STRUCT_FGET(knconf_tmp, knc_rdev);
313*39d3e169Sevanl 	}
314*39d3e169Sevanl 
315*39d3e169Sevanl 	pf = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
316*39d3e169Sevanl 	p = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
317*39d3e169Sevanl 	error = copyinstr(knconf->knc_protofmly, pf, KNC_STRSIZE, NULL);
318*39d3e169Sevanl 	if (error) {
319*39d3e169Sevanl 		kmem_free(pf, KNC_STRSIZE);
320*39d3e169Sevanl 		kmem_free(p, KNC_STRSIZE);
321*39d3e169Sevanl 		kmem_free(knconf, sizeof (*knconf));
322*39d3e169Sevanl 		return (error);
323*39d3e169Sevanl 	}
324*39d3e169Sevanl 
325*39d3e169Sevanl 	error = copyinstr(knconf->knc_proto, p, KNC_STRSIZE, NULL);
326*39d3e169Sevanl 	if (error) {
327*39d3e169Sevanl 		kmem_free(pf, KNC_STRSIZE);
328*39d3e169Sevanl 		kmem_free(p, KNC_STRSIZE);
329*39d3e169Sevanl 		kmem_free(knconf, sizeof (*knconf));
330*39d3e169Sevanl 		return (error);
331*39d3e169Sevanl 	}
332*39d3e169Sevanl 
333*39d3e169Sevanl 
334*39d3e169Sevanl 	knconf->knc_protofmly = pf;
335*39d3e169Sevanl 	knconf->knc_proto = p;
336*39d3e169Sevanl 
337*39d3e169Sevanl 	nargs->knconf = knconf;
338*39d3e169Sevanl 	/*
339*39d3e169Sevanl 	 * Get server address
340*39d3e169Sevanl 	 */
341*39d3e169Sevanl 	STRUCT_INIT(addr_tmp, get_udatamodel());
342*39d3e169Sevanl 	if (copyin(STRUCT_FGETP(args, addr), STRUCT_BUF(addr_tmp),
343*39d3e169Sevanl 	STRUCT_SIZE(addr_tmp))) {
344*39d3e169Sevanl 	error = EFAULT;
345*39d3e169Sevanl 	goto errout;
346*39d3e169Sevanl 	}
347*39d3e169Sevanl 
348*39d3e169Sevanl 	nargs->addr = kmem_alloc(sizeof (struct netbuf), KM_SLEEP);
349*39d3e169Sevanl 	userbufptr = STRUCT_FGETP(addr_tmp, buf);
350*39d3e169Sevanl 	addr.len = STRUCT_FGET(addr_tmp, len);
351*39d3e169Sevanl 	addr.buf = kmem_alloc(addr.len, KM_SLEEP);
352*39d3e169Sevanl 	addr.maxlen = addr.len;
353*39d3e169Sevanl 	if (copyin(userbufptr, addr.buf, addr.len)) {
354*39d3e169Sevanl 		kmem_free(addr.buf, addr.len);
355*39d3e169Sevanl 		error = EFAULT;
356*39d3e169Sevanl 		goto errout;
357*39d3e169Sevanl 	}
358*39d3e169Sevanl 	bcopy(&addr, nargs->addr, sizeof (struct netbuf));
359*39d3e169Sevanl 
360*39d3e169Sevanl 	/*
361*39d3e169Sevanl 	 * Get the root fhandle
362*39d3e169Sevanl 	 */
363*39d3e169Sevanl 
364*39d3e169Sevanl 	if (copyin(STRUCT_FGETP(args, fh), fh, sizeof (nfs_fhandle))) {
365*39d3e169Sevanl 		error = EFAULT;
366*39d3e169Sevanl 		goto errout;
367*39d3e169Sevanl 	}
368*39d3e169Sevanl 
369*39d3e169Sevanl 
370*39d3e169Sevanl 	/*
371*39d3e169Sevanl 	 * Get server's hostname
372*39d3e169Sevanl 	 */
373*39d3e169Sevanl 	if (flags & NFSMNT_HOSTNAME) {
374*39d3e169Sevanl 		error = copyinstr(STRUCT_FGETP(args, hostname),
375*39d3e169Sevanl 			netname, sizeof (netname), &hlen);
376*39d3e169Sevanl 	if (error)
377*39d3e169Sevanl 		goto errout;
378*39d3e169Sevanl 	nargs->hostname = kmem_zalloc(hlen, KM_SLEEP);
379*39d3e169Sevanl 	(void) strcpy(nargs->hostname, netname);
380*39d3e169Sevanl 	} else {
381*39d3e169Sevanl 	nargs->hostname = NULL;
382*39d3e169Sevanl 	}
383*39d3e169Sevanl 
384*39d3e169Sevanl 
385*39d3e169Sevanl 	/*
386*39d3e169Sevanl 	 * If there are syncaddr and netname data, load them in. This is
387*39d3e169Sevanl 	 * to support data needed for NFSV4 when AUTH_DH is the negotiated
388*39d3e169Sevanl 	 * flavor via SECINFO. (instead of using MOUNT protocol in V3).
389*39d3e169Sevanl 	 */
390*39d3e169Sevanl 	netname[0] = '\0';
391*39d3e169Sevanl 	if (flags & NFSMNT_SECURE) {
392*39d3e169Sevanl 		if (STRUCT_FGETP(args, syncaddr) == NULL) {
393*39d3e169Sevanl 			error = EINVAL;
394*39d3e169Sevanl 			goto errout;
395*39d3e169Sevanl 		}
396*39d3e169Sevanl 		/* get syncaddr */
397*39d3e169Sevanl 		STRUCT_INIT(addr_tmp, get_udatamodel());
398*39d3e169Sevanl 		if (copyin(STRUCT_FGETP(args, syncaddr), STRUCT_BUF(addr_tmp),
399*39d3e169Sevanl 			STRUCT_SIZE(addr_tmp))) {
400*39d3e169Sevanl 			error = EINVAL;
401*39d3e169Sevanl 			goto errout;
402*39d3e169Sevanl 		}
403*39d3e169Sevanl 		userbufptr = STRUCT_FGETP(addr_tmp, buf);
404*39d3e169Sevanl 		syncaddr.len = STRUCT_FGET(addr_tmp, len);
405*39d3e169Sevanl 		syncaddr.buf = kmem_alloc(syncaddr.len, KM_SLEEP);
406*39d3e169Sevanl 		syncaddr.maxlen = syncaddr.len;
407*39d3e169Sevanl 		if (copyin(userbufptr, syncaddr.buf, syncaddr.len)) {
408*39d3e169Sevanl 			kmem_free(syncaddr.buf, syncaddr.len);
409*39d3e169Sevanl 			error = EFAULT;
410*39d3e169Sevanl 			goto errout;
411*39d3e169Sevanl 		}
412*39d3e169Sevanl 
413*39d3e169Sevanl 		nargs->syncaddr = kmem_alloc(sizeof (struct netbuf), KM_SLEEP);
414*39d3e169Sevanl 		bcopy(&syncaddr, nargs->syncaddr, sizeof (struct netbuf));
415*39d3e169Sevanl 
416*39d3e169Sevanl 		ASSERT(STRUCT_FGETP(args, netname));
417*39d3e169Sevanl 
418*39d3e169Sevanl 		if (copyinstr(STRUCT_FGETP(args, netname), netname,
419*39d3e169Sevanl 			sizeof (netname), &nlen)) {
420*39d3e169Sevanl 			error = EFAULT;
421*39d3e169Sevanl 			goto errout;
422*39d3e169Sevanl 		}
423*39d3e169Sevanl 
424*39d3e169Sevanl 		netname[nlen] = '\0';
425*39d3e169Sevanl 		nargs->netname = kmem_zalloc(nlen, KM_SLEEP);
426*39d3e169Sevanl 		(void) strcpy(nargs->netname, netname);
427*39d3e169Sevanl 	}
428*39d3e169Sevanl 
429*39d3e169Sevanl 	/*
430*39d3e169Sevanl 	 * Get the extention data which has the security data structure.
431*39d3e169Sevanl 	 * This includes data for AUTH_SYS as well.
432*39d3e169Sevanl 	 */
433*39d3e169Sevanl 	if (flags & NFSMNT_NEWARGS) {
434*39d3e169Sevanl 		nargs->nfs_args_ext = STRUCT_FGET(args, nfs_args_ext);
435*39d3e169Sevanl 		if (nargs->nfs_args_ext == NFS_ARGS_EXTA ||
436*39d3e169Sevanl 			nargs->nfs_args_ext == NFS_ARGS_EXTB) {
437*39d3e169Sevanl 			/*
438*39d3e169Sevanl 			 * Indicating the application is using the new
439*39d3e169Sevanl 			 * sec_data structure to pass in the security
440*39d3e169Sevanl 			 * data.
441*39d3e169Sevanl 			 */
442*39d3e169Sevanl 			if (STRUCT_FGETP(args,
443*39d3e169Sevanl 				nfs_ext_u.nfs_extA.secdata) != NULL) {
444*39d3e169Sevanl 				error = sec_clnt_loadinfo(
445*39d3e169Sevanl 					(struct sec_data *)STRUCT_FGETP(args,
446*39d3e169Sevanl 					nfs_ext_u.nfs_extA.secdata),
447*39d3e169Sevanl 					&secdata, get_udatamodel());
448*39d3e169Sevanl 			}
449*39d3e169Sevanl 			nargs->nfs_ext_u.nfs_extA.secdata = secdata;
450*39d3e169Sevanl 		}
451*39d3e169Sevanl 	}
452*39d3e169Sevanl 
453*39d3e169Sevanl 	if (error)
454*39d3e169Sevanl 		goto errout;
455*39d3e169Sevanl 
456*39d3e169Sevanl 	/*
457*39d3e169Sevanl 	 * Failover support:
458*39d3e169Sevanl 	 *
459*39d3e169Sevanl 	 * We may have a linked list of nfs_args structures,
460*39d3e169Sevanl 	 * which means the user is looking for failover.  If
461*39d3e169Sevanl 	 * the mount is either not "read-only" or "soft",
462*39d3e169Sevanl 	 * we want to bail out with EINVAL.
463*39d3e169Sevanl 	 */
464*39d3e169Sevanl 	if (nargs->nfs_args_ext == NFS_ARGS_EXTB)
465*39d3e169Sevanl 		nargs->nfs_ext_u.nfs_extB.next =
466*39d3e169Sevanl 			STRUCT_FGETP(args, nfs_ext_u.nfs_extB.next);
467*39d3e169Sevanl 
468*39d3e169Sevanl errout:
469*39d3e169Sevanl 	if (error)
470*39d3e169Sevanl 		nfs3_free_args(nargs, fh);
471*39d3e169Sevanl 
472*39d3e169Sevanl 	return (error);
473*39d3e169Sevanl }
474*39d3e169Sevanl 
475*39d3e169Sevanl 
4767c478bd9Sstevel@tonic-gate /*
4777c478bd9Sstevel@tonic-gate  * nfs mount vfsop
4787c478bd9Sstevel@tonic-gate  * Set up mount info record and attach it to vfs struct.
4797c478bd9Sstevel@tonic-gate  */
4807c478bd9Sstevel@tonic-gate static int
4817c478bd9Sstevel@tonic-gate nfs3_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
4827c478bd9Sstevel@tonic-gate {
483*39d3e169Sevanl 	struct nfs_args	*args = NULL;
484*39d3e169Sevanl 	nfs_fhandle	*fhandle = NULL;
4857c478bd9Sstevel@tonic-gate 	char *data = uap->dataptr;
4867c478bd9Sstevel@tonic-gate 	int error;
4877c478bd9Sstevel@tonic-gate 	vnode_t *rtvp;			/* the server's root */
4887c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;			/* mount info, pointed at by vfs */
4897c478bd9Sstevel@tonic-gate 	size_t nlen;			/* length of netname */
4907c478bd9Sstevel@tonic-gate 	struct knetconfig *knconf;	/* transport knetconfig structure */
4917c478bd9Sstevel@tonic-gate 	struct knetconfig *rdma_knconf;	/* rdma transport structure */
4927c478bd9Sstevel@tonic-gate 	rnode_t *rp;
4937c478bd9Sstevel@tonic-gate 	struct servinfo *svp;		/* nfs server info */
4947c478bd9Sstevel@tonic-gate 	struct servinfo *svp_tail = NULL; /* previous nfs server info */
4957c478bd9Sstevel@tonic-gate 	struct servinfo *svp_head;	/* first nfs server info */
4967c478bd9Sstevel@tonic-gate 	struct servinfo *svp_2ndlast;	/* 2nd last in server info list */
4977c478bd9Sstevel@tonic-gate 	struct sec_data *secdata;	/* security data */
4987c478bd9Sstevel@tonic-gate 	int flags, addr_type;
499108322fbScarlsonj 	zone_t *zone = nfs_zone();
50045916cd2Sjpk 	zone_t *mntzone = NULL;
5017c478bd9Sstevel@tonic-gate 
502*39d3e169Sevanl 
5037c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
5047c478bd9Sstevel@tonic-gate 		return (EPERM);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if (mvp->v_type != VDIR)
5077c478bd9Sstevel@tonic-gate 		return (ENOTDIR);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/*
5107c478bd9Sstevel@tonic-gate 	 * get arguments
5117c478bd9Sstevel@tonic-gate 	 *
5127c478bd9Sstevel@tonic-gate 	 * nfs_args is now versioned and is extensible, so
5137c478bd9Sstevel@tonic-gate 	 * uap->datalen might be different from sizeof (args)
5147c478bd9Sstevel@tonic-gate 	 * in a compatible situation.
5157c478bd9Sstevel@tonic-gate 	 */
516*39d3e169Sevanl 
5177c478bd9Sstevel@tonic-gate more:
5187c478bd9Sstevel@tonic-gate 
519*39d3e169Sevanl 	if (!(uap->flags & MS_SYSSPACE)) {
520*39d3e169Sevanl 		if (args == NULL)
521*39d3e169Sevanl 			args = kmem_alloc(sizeof (struct nfs_args), KM_SLEEP);
522*39d3e169Sevanl 		else {
523*39d3e169Sevanl 			nfs3_free_args(args, fhandle);
524*39d3e169Sevanl 			fhandle = NULL;
525*39d3e169Sevanl 		}
526*39d3e169Sevanl 		if (fhandle == NULL)
527*39d3e169Sevanl 			fhandle = kmem_alloc(sizeof (nfs_fhandle), KM_SLEEP);
528*39d3e169Sevanl 		error = nfs3_copyin(data, uap->datalen, args, fhandle);
529*39d3e169Sevanl 		if (error) {
530*39d3e169Sevanl 			if (args)
531*39d3e169Sevanl 				kmem_free(args, sizeof (*args));
532*39d3e169Sevanl 			return (error);
533*39d3e169Sevanl 		}
534*39d3e169Sevanl 	} else {
535*39d3e169Sevanl 		args = (struct nfs_args *)data;
536*39d3e169Sevanl 		fhandle = (nfs_fhandle *)args->fh;
537*39d3e169Sevanl 	}
538*39d3e169Sevanl 
539*39d3e169Sevanl 
540*39d3e169Sevanl 	flags = args->flags;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_REMOUNT) {
543*39d3e169Sevanl 		size_t	n;
544*39d3e169Sevanl 		char	name[FSTYPSZ];
5457c478bd9Sstevel@tonic-gate 
546*39d3e169Sevanl 		if (uap->flags & MS_SYSSPACE) {
5477c478bd9Sstevel@tonic-gate 			error = copystr(uap->fstype, name, FSTYPSZ, &n);
548*39d3e169Sevanl 		} else {
549*39d3e169Sevanl 			nfs3_free_args(args, fhandle);
550*39d3e169Sevanl 			kmem_free(args, sizeof (*args));
5517c478bd9Sstevel@tonic-gate 			error = copyinstr(uap->fstype, name, FSTYPSZ, &n);
552*39d3e169Sevanl 		}
5537c478bd9Sstevel@tonic-gate 		if (error) {
5547c478bd9Sstevel@tonic-gate 			if (error == ENAMETOOLONG)
5557c478bd9Sstevel@tonic-gate 				return (EINVAL);
5567c478bd9Sstevel@tonic-gate 			return (error);
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 		/*
5607c478bd9Sstevel@tonic-gate 		 * This check is to ensure that the request is a
5617c478bd9Sstevel@tonic-gate 		 * genuine nfs remount request.
5627c478bd9Sstevel@tonic-gate 		 */
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		if (strncmp(name, "nfs", 3) != 0)
5657c478bd9Sstevel@tonic-gate 			return (EINVAL);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 		/*
5687c478bd9Sstevel@tonic-gate 		 * If the request changes the locking type, disallow the
5697c478bd9Sstevel@tonic-gate 		 * remount,
5707c478bd9Sstevel@tonic-gate 		 * because it's questionable whether we can transfer the
5717c478bd9Sstevel@tonic-gate 		 * locking state correctly.
5727c478bd9Sstevel@tonic-gate 		 */
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 		if ((mi = VFTOMI(vfsp)) != NULL) {
5757c478bd9Sstevel@tonic-gate 			uint_t new_mi_llock;
5767c478bd9Sstevel@tonic-gate 			uint_t old_mi_llock;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 			new_mi_llock = (flags & NFSMNT_LLOCK) ? 1 : 0;
5797c478bd9Sstevel@tonic-gate 			old_mi_llock = (mi->mi_flags & MI_LLOCK) ? 1 : 0;
5807c478bd9Sstevel@tonic-gate 			if (old_mi_llock != new_mi_llock)
5817c478bd9Sstevel@tonic-gate 				return (EBUSY);
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 		return (0);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
5877c478bd9Sstevel@tonic-gate 	if (!(uap->flags & MS_OVERLAY) &&
5887c478bd9Sstevel@tonic-gate 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
5897c478bd9Sstevel@tonic-gate 		mutex_exit(&mvp->v_lock);
590*39d3e169Sevanl 		if (!(uap->flags & MS_SYSSPACE)) {
591*39d3e169Sevanl 			nfs3_free_args(args, fhandle);
592*39d3e169Sevanl 			kmem_free(args, sizeof (*args));
593*39d3e169Sevanl 		}
5947c478bd9Sstevel@tonic-gate 		return (EBUSY);
5957c478bd9Sstevel@tonic-gate 	}
5967c478bd9Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	/* make sure things are zeroed for errout: */
5997c478bd9Sstevel@tonic-gate 	rtvp = NULL;
6007c478bd9Sstevel@tonic-gate 	mi = NULL;
6017c478bd9Sstevel@tonic-gate 	secdata = NULL;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	/*
6047c478bd9Sstevel@tonic-gate 	 * A valid knetconfig structure is required.
6057c478bd9Sstevel@tonic-gate 	 */
606*39d3e169Sevanl 	if (!(flags & NFSMNT_KNCONF)) {
607*39d3e169Sevanl 		if (!(uap->flags & MS_SYSSPACE)) {
608*39d3e169Sevanl 			nfs3_free_args(args, fhandle);
609*39d3e169Sevanl 			kmem_free(args, sizeof (*args));
610*39d3e169Sevanl 		}
6117c478bd9Sstevel@tonic-gate 		return (EINVAL);
612*39d3e169Sevanl 	}
613*39d3e169Sevanl 
614*39d3e169Sevanl 	if ((strlen(args->knconf->knc_protofmly) >= KNC_STRSIZE) ||
615*39d3e169Sevanl 		(strlen(args->knconf->knc_proto) >= KNC_STRSIZE)) {
616*39d3e169Sevanl 		if (!(uap->flags & MS_SYSSPACE)) {
617*39d3e169Sevanl 			nfs3_free_args(args, fhandle);
618*39d3e169Sevanl 			kmem_free(args, sizeof (*args));
619*39d3e169Sevanl 		}
620*39d3e169Sevanl 		return (EINVAL);
621*39d3e169Sevanl 	}
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	/*
6247c478bd9Sstevel@tonic-gate 	 * Allocate a servinfo struct.
6257c478bd9Sstevel@tonic-gate 	 */
6267c478bd9Sstevel@tonic-gate 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
6277c478bd9Sstevel@tonic-gate 	mutex_init(&svp->sv_lock, NULL, MUTEX_DEFAULT, NULL);
6287c478bd9Sstevel@tonic-gate 	if (svp_tail) {
6297c478bd9Sstevel@tonic-gate 		svp_2ndlast = svp_tail;
6307c478bd9Sstevel@tonic-gate 		svp_tail->sv_next = svp;
6317c478bd9Sstevel@tonic-gate 	} else {
6327c478bd9Sstevel@tonic-gate 		svp_head = svp;
6337c478bd9Sstevel@tonic-gate 		svp_2ndlast = svp;
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	svp_tail = svp;
6377c478bd9Sstevel@tonic-gate 
638*39d3e169Sevanl 	svp->sv_knconf = args->knconf;
639*39d3e169Sevanl 	args->knconf = NULL;
6407c478bd9Sstevel@tonic-gate 
641*39d3e169Sevanl 	if (args->addr == NULL || args->addr->buf == NULL) {
642*39d3e169Sevanl 		error = EINVAL;
6437c478bd9Sstevel@tonic-gate 		goto errout;
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
646*39d3e169Sevanl 	svp->sv_addr.maxlen = args->addr->maxlen;
647*39d3e169Sevanl 	svp->sv_addr.len = args->addr->len;
648*39d3e169Sevanl 	svp->sv_addr.buf = args->addr->buf;
649*39d3e169Sevanl 	args->addr->buf = NULL;
650*39d3e169Sevanl 
6517c478bd9Sstevel@tonic-gate 	/*
6527c478bd9Sstevel@tonic-gate 	 * Check the root fhandle length
6537c478bd9Sstevel@tonic-gate 	 */
654*39d3e169Sevanl 	ASSERT(fhandle);
655*39d3e169Sevanl 	if (fhandle->fh_len > NFS3_FHSIZE || fhandle->fh_len == 0) {
6567c478bd9Sstevel@tonic-gate 		error = EINVAL;
6577c478bd9Sstevel@tonic-gate #ifdef DEBUG
6587c478bd9Sstevel@tonic-gate 		zcmn_err(getzoneid(), CE_WARN,
6597c478bd9Sstevel@tonic-gate 		    "nfs3_mount: got an invalid fhandle. fh_len = %d",
660*39d3e169Sevanl 		    fhandle->fh_len);
661*39d3e169Sevanl 		fhandle->fh_len = NFS_FHANDLE_LEN;
662*39d3e169Sevanl 		nfs_printfhandle(fhandle);
6637c478bd9Sstevel@tonic-gate #endif
6647c478bd9Sstevel@tonic-gate 		goto errout;
6657c478bd9Sstevel@tonic-gate 	}
6667c478bd9Sstevel@tonic-gate 
667*39d3e169Sevanl 	bcopy(&fhandle->fh_buf, &svp->sv_fhandle.fh_buf, fhandle->fh_len);
668*39d3e169Sevanl 	svp->sv_fhandle.fh_len = fhandle->fh_len;
669*39d3e169Sevanl 
6707c478bd9Sstevel@tonic-gate 	/*
6717c478bd9Sstevel@tonic-gate 	 * Get server's hostname
6727c478bd9Sstevel@tonic-gate 	 */
6737c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_HOSTNAME) {
674*39d3e169Sevanl 		if (args->hostname == NULL) {
675*39d3e169Sevanl 			error = EINVAL;
6767c478bd9Sstevel@tonic-gate 			goto errout;
677*39d3e169Sevanl 		}
678*39d3e169Sevanl 		svp->sv_hostnamelen = strlen(args->hostname) + 1;
679*39d3e169Sevanl 		svp->sv_hostname = args->hostname;
680*39d3e169Sevanl 		args->hostname = NULL;
6817c478bd9Sstevel@tonic-gate 	} else {
6827c478bd9Sstevel@tonic-gate 		char *p = "unknown-host";
683*39d3e169Sevanl 		svp->sv_hostnamelen = strlen(p) + 1;
684*39d3e169Sevanl 		svp->sv_hostname = kmem_zalloc(svp->sv_hostnamelen, KM_SLEEP);
685*39d3e169Sevanl 		(void) strcpy(svp->sv_hostname, p);
6867c478bd9Sstevel@tonic-gate 	}
687*39d3e169Sevanl 
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	/*
6907c478bd9Sstevel@tonic-gate 	 * RDMA MOUNT SUPPORT FOR NFS v3:
6917c478bd9Sstevel@tonic-gate 	 * Establish, is it possible to use RDMA, if so overload the
6927c478bd9Sstevel@tonic-gate 	 * knconf with rdma specific knconf and free the orignal.
6937c478bd9Sstevel@tonic-gate 	 */
6947c478bd9Sstevel@tonic-gate 	if ((flags & NFSMNT_TRYRDMA) || (flags & NFSMNT_DORDMA)) {
6957c478bd9Sstevel@tonic-gate 		/*
6967c478bd9Sstevel@tonic-gate 		 * Determine the addr type for RDMA, IPv4 or v6.
6977c478bd9Sstevel@tonic-gate 		 */
6987c478bd9Sstevel@tonic-gate 		if (strcmp(svp->sv_knconf->knc_protofmly, NC_INET) == 0)
6997c478bd9Sstevel@tonic-gate 			addr_type = AF_INET;
7007c478bd9Sstevel@tonic-gate 		else if (strcmp(svp->sv_knconf->knc_protofmly, NC_INET6) == 0)
7017c478bd9Sstevel@tonic-gate 			addr_type = AF_INET6;
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		if (rdma_reachable(addr_type, &svp->sv_addr,
7047c478bd9Sstevel@tonic-gate 			&rdma_knconf) == 0) {
7057c478bd9Sstevel@tonic-gate 			/*
7067c478bd9Sstevel@tonic-gate 			 * If successful, hijack the orignal knconf and
7077c478bd9Sstevel@tonic-gate 			 * replace with a new one, depending on the flags.
7087c478bd9Sstevel@tonic-gate 			 */
7097c478bd9Sstevel@tonic-gate 			svp->sv_origknconf = svp->sv_knconf;
7107c478bd9Sstevel@tonic-gate 			svp->sv_knconf = rdma_knconf;
7117c478bd9Sstevel@tonic-gate 			knconf = rdma_knconf;
7127c478bd9Sstevel@tonic-gate 		} else {
7137c478bd9Sstevel@tonic-gate 			if (flags & NFSMNT_TRYRDMA) {
7147c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7157c478bd9Sstevel@tonic-gate 				if (rdma_debug)
7167c478bd9Sstevel@tonic-gate 					zcmn_err(getzoneid(), CE_WARN,
7177c478bd9Sstevel@tonic-gate 					    "no RDMA onboard, revert\n");
7187c478bd9Sstevel@tonic-gate #endif
7197c478bd9Sstevel@tonic-gate 			}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 			if (flags & NFSMNT_DORDMA) {
7227c478bd9Sstevel@tonic-gate 				/*
7237c478bd9Sstevel@tonic-gate 				 * If proto=rdma is specified and no RDMA
7247c478bd9Sstevel@tonic-gate 				 * path to this server is avialable then
7257c478bd9Sstevel@tonic-gate 				 * ditch this server.
7267c478bd9Sstevel@tonic-gate 				 * This is not included in the mountable
7277c478bd9Sstevel@tonic-gate 				 * server list or the replica list.
7287c478bd9Sstevel@tonic-gate 				 * Check if more servers are specified;
7297c478bd9Sstevel@tonic-gate 				 * Failover case, otherwise bail out of mount.
7307c478bd9Sstevel@tonic-gate 				 */
731*39d3e169Sevanl 				if (args->nfs_args_ext ==
732*39d3e169Sevanl 				    NFS_ARGS_EXTB &&
733*39d3e169Sevanl 					args->nfs_ext_u.nfs_extB.next
734*39d3e169Sevanl 					!= NULL) {
735*39d3e169Sevanl 					data = (char *)
736*39d3e169Sevanl 						args->nfs_ext_u.nfs_extB.next;
7377c478bd9Sstevel@tonic-gate 					if (uap->flags & MS_RDONLY &&
7387c478bd9Sstevel@tonic-gate 					    !(flags & NFSMNT_SOFT)) {
7397c478bd9Sstevel@tonic-gate 						if (svp_head->sv_next == NULL) {
7407c478bd9Sstevel@tonic-gate 							svp_tail = NULL;
7417c478bd9Sstevel@tonic-gate 							svp_2ndlast = NULL;
7427c478bd9Sstevel@tonic-gate 							sv_free(svp_head);
7437c478bd9Sstevel@tonic-gate 							goto more;
7447c478bd9Sstevel@tonic-gate 						} else {
7457c478bd9Sstevel@tonic-gate 							svp_tail = svp_2ndlast;
7467c478bd9Sstevel@tonic-gate 							svp_2ndlast->sv_next =
7477c478bd9Sstevel@tonic-gate 							    NULL;
7487c478bd9Sstevel@tonic-gate 							sv_free(svp);
7497c478bd9Sstevel@tonic-gate 							goto more;
7507c478bd9Sstevel@tonic-gate 						}
7517c478bd9Sstevel@tonic-gate 					}
7527c478bd9Sstevel@tonic-gate 				} else {
7537c478bd9Sstevel@tonic-gate 					/*
7547c478bd9Sstevel@tonic-gate 					 * This is the last server specified
7557c478bd9Sstevel@tonic-gate 					 * in the nfs_args list passed down
7567c478bd9Sstevel@tonic-gate 					 * and its not rdma capable.
7577c478bd9Sstevel@tonic-gate 					 */
7587c478bd9Sstevel@tonic-gate 					if (svp_head->sv_next == NULL) {
7597c478bd9Sstevel@tonic-gate 						/*
7607c478bd9Sstevel@tonic-gate 						 * Is this the only one
7617c478bd9Sstevel@tonic-gate 						 */
7627c478bd9Sstevel@tonic-gate 						error = EINVAL;
7637c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7647c478bd9Sstevel@tonic-gate 						if (rdma_debug)
7657c478bd9Sstevel@tonic-gate 							zcmn_err(getzoneid(),
7667c478bd9Sstevel@tonic-gate 							    CE_WARN,
7677c478bd9Sstevel@tonic-gate 							    "No RDMA srv");
7687c478bd9Sstevel@tonic-gate #endif
7697c478bd9Sstevel@tonic-gate 						goto errout;
7707c478bd9Sstevel@tonic-gate 					} else {
7717c478bd9Sstevel@tonic-gate 						/*
7727c478bd9Sstevel@tonic-gate 						 * There is list, since some
7737c478bd9Sstevel@tonic-gate 						 * servers specified before
7747c478bd9Sstevel@tonic-gate 						 * this passed all requirements
7757c478bd9Sstevel@tonic-gate 						 */
7767c478bd9Sstevel@tonic-gate 						svp_tail = svp_2ndlast;
7777c478bd9Sstevel@tonic-gate 						svp_2ndlast->sv_next = NULL;
7787c478bd9Sstevel@tonic-gate 						sv_free(svp);
7797c478bd9Sstevel@tonic-gate 						goto proceed;
7807c478bd9Sstevel@tonic-gate 					}
7817c478bd9Sstevel@tonic-gate 				}
7827c478bd9Sstevel@tonic-gate 			}
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	/*
7877c478bd9Sstevel@tonic-gate 	 * Get the extention data which has the new security data structure.
7887c478bd9Sstevel@tonic-gate 	 */
7897c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_NEWARGS) {
790*39d3e169Sevanl 		switch (args->nfs_args_ext) {
7917c478bd9Sstevel@tonic-gate 		case NFS_ARGS_EXTA:
7927c478bd9Sstevel@tonic-gate 		case NFS_ARGS_EXTB:
7937c478bd9Sstevel@tonic-gate 			/*
7947c478bd9Sstevel@tonic-gate 			 * Indicating the application is using the new
7957c478bd9Sstevel@tonic-gate 			 * sec_data structure to pass in the security
7967c478bd9Sstevel@tonic-gate 			 * data.
7977c478bd9Sstevel@tonic-gate 			 */
798*39d3e169Sevanl 			secdata = args->nfs_ext_u.nfs_extA.secdata;
799*39d3e169Sevanl 			if (args->nfs_ext_u.nfs_extA.secdata == NULL) {
8007c478bd9Sstevel@tonic-gate 				error = EINVAL;
8017c478bd9Sstevel@tonic-gate 			} else {
802*39d3e169Sevanl 				/*
803*39d3e169Sevanl 				 * Need to validate the flavor here if
804*39d3e169Sevanl 				 * sysspace, userspace was already
805*39d3e169Sevanl 				 * validate from the nfs_copyin function.
806*39d3e169Sevanl 				 */
807*39d3e169Sevanl 				switch (secdata->rpcflavor) {
808*39d3e169Sevanl 				case AUTH_NONE:
809*39d3e169Sevanl 				case AUTH_UNIX:
810*39d3e169Sevanl 				case AUTH_LOOPBACK:
811*39d3e169Sevanl 				case AUTH_DES:
812*39d3e169Sevanl 				case RPCSEC_GSS:
813*39d3e169Sevanl 					args->nfs_ext_u.nfs_extA.secdata =
814*39d3e169Sevanl 						NULL;
815*39d3e169Sevanl 					break;
816*39d3e169Sevanl 				default:
817*39d3e169Sevanl 					error = EINVAL;
818*39d3e169Sevanl 					goto errout;
819*39d3e169Sevanl 				}
8207c478bd9Sstevel@tonic-gate 			}
8217c478bd9Sstevel@tonic-gate 			break;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		default:
8247c478bd9Sstevel@tonic-gate 			error = EINVAL;
8257c478bd9Sstevel@tonic-gate 			break;
8267c478bd9Sstevel@tonic-gate 		}
8277c478bd9Sstevel@tonic-gate 	} else if (flags & NFSMNT_SECURE) {
8287c478bd9Sstevel@tonic-gate 		/*
8297c478bd9Sstevel@tonic-gate 		 * Keep this for backward compatibility to support
8307c478bd9Sstevel@tonic-gate 		 * NFSMNT_SECURE/NFSMNT_RPCTIMESYNC flags.
8317c478bd9Sstevel@tonic-gate 		 */
832*39d3e169Sevanl 		if (args->syncaddr == NULL || args->syncaddr->buf == NULL) {
8337c478bd9Sstevel@tonic-gate 			error = EINVAL;
834*39d3e169Sevanl 			goto errout;
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 		/*
8377c478bd9Sstevel@tonic-gate 		 * Move security related data to the sec_data structure.
8387c478bd9Sstevel@tonic-gate 		 */
839*39d3e169Sevanl 		{
8407c478bd9Sstevel@tonic-gate 			dh_k4_clntdata_t *data;
8417c478bd9Sstevel@tonic-gate 			char *pf, *p;
8427c478bd9Sstevel@tonic-gate 			secdata = kmem_alloc(sizeof (*secdata), KM_SLEEP);
8437c478bd9Sstevel@tonic-gate 			if (flags & NFSMNT_RPCTIMESYNC)
8447c478bd9Sstevel@tonic-gate 				secdata->flags |= AUTH_F_RPCTIMESYNC;
8457c478bd9Sstevel@tonic-gate 			data = kmem_alloc(sizeof (*data), KM_SLEEP);
846*39d3e169Sevanl 			bcopy(args->syncaddr, &data->syncaddr,
847*39d3e169Sevanl 				sizeof (*args->syncaddr));
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 			/*
8507c478bd9Sstevel@tonic-gate 			 * duplicate the knconf information for the
8517c478bd9Sstevel@tonic-gate 			 * new opaque data.
8527c478bd9Sstevel@tonic-gate 			 */
8537c478bd9Sstevel@tonic-gate 			data->knconf = kmem_alloc(sizeof (*knconf), KM_SLEEP);
8547c478bd9Sstevel@tonic-gate 			*data->knconf = *knconf;
8557c478bd9Sstevel@tonic-gate 			pf = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
8567c478bd9Sstevel@tonic-gate 			p = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
8577c478bd9Sstevel@tonic-gate 			bcopy(knconf->knc_protofmly, pf, KNC_STRSIZE);
8587c478bd9Sstevel@tonic-gate 			bcopy(knconf->knc_proto, pf, KNC_STRSIZE);
8597c478bd9Sstevel@tonic-gate 			data->knconf->knc_protofmly = pf;
8607c478bd9Sstevel@tonic-gate 			data->knconf->knc_proto = p;
8617c478bd9Sstevel@tonic-gate 
862*39d3e169Sevanl 			nlen = strlen(args->hostname) + 1;
8637c478bd9Sstevel@tonic-gate 			/* move server netname to the sec_data structure */
8647c478bd9Sstevel@tonic-gate 			if (nlen != 0) {
8657c478bd9Sstevel@tonic-gate 				data->netname = kmem_alloc(nlen, KM_SLEEP);
866*39d3e169Sevanl 				bcopy(args->hostname, data->netname, nlen);
867*39d3e169Sevanl 				data->netnamelen = nlen;
8687c478bd9Sstevel@tonic-gate 			}
8697c478bd9Sstevel@tonic-gate 			secdata->secmod = secdata->rpcflavor = AUTH_DES;
8707c478bd9Sstevel@tonic-gate 			secdata->data = (caddr_t)data;
8717c478bd9Sstevel@tonic-gate 		}
872*39d3e169Sevanl 	} else 	{
8737c478bd9Sstevel@tonic-gate 		secdata = kmem_alloc(sizeof (*secdata), KM_SLEEP);
8747c478bd9Sstevel@tonic-gate 		secdata->secmod = secdata->rpcflavor = AUTH_UNIX;
8757c478bd9Sstevel@tonic-gate 		secdata->data = NULL;
8767c478bd9Sstevel@tonic-gate 	}
877*39d3e169Sevanl 
8787c478bd9Sstevel@tonic-gate 	svp->sv_secdata = secdata;
8797c478bd9Sstevel@tonic-gate 	if (error)
8807c478bd9Sstevel@tonic-gate 		goto errout;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/*
8837c478bd9Sstevel@tonic-gate 	 * See bug 1180236.
8847c478bd9Sstevel@tonic-gate 	 * If mount secure failed, we will fall back to AUTH_NONE
8857c478bd9Sstevel@tonic-gate 	 * and try again.  nfs3rootvp() will turn this back off.
8867c478bd9Sstevel@tonic-gate 	 *
8877c478bd9Sstevel@tonic-gate 	 * The NFS Version 3 mount uses the FSINFO and GETATTR
8887c478bd9Sstevel@tonic-gate 	 * procedures.  The server should not care if these procedures
8897c478bd9Sstevel@tonic-gate 	 * have the proper security flavor, so if mount retries using
8907c478bd9Sstevel@tonic-gate 	 * AUTH_NONE that does not require a credential setup for root
8917c478bd9Sstevel@tonic-gate 	 * then the automounter would work without requiring root to be
8927c478bd9Sstevel@tonic-gate 	 * keylogged into AUTH_DES.
8937c478bd9Sstevel@tonic-gate 	 */
8947c478bd9Sstevel@tonic-gate 	if (secdata->rpcflavor != AUTH_UNIX &&
8957c478bd9Sstevel@tonic-gate 	    secdata->rpcflavor != AUTH_LOOPBACK)
8967c478bd9Sstevel@tonic-gate 		secdata->flags |= AUTH_F_TRYNONE;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	/*
8997c478bd9Sstevel@tonic-gate 	 * Failover support:
9007c478bd9Sstevel@tonic-gate 	 *
9017c478bd9Sstevel@tonic-gate 	 * We may have a linked list of nfs_args structures,
9027c478bd9Sstevel@tonic-gate 	 * which means the user is looking for failover.  If
9037c478bd9Sstevel@tonic-gate 	 * the mount is either not "read-only" or "soft",
9047c478bd9Sstevel@tonic-gate 	 * we want to bail out with EINVAL.
9057c478bd9Sstevel@tonic-gate 	 */
906*39d3e169Sevanl 	if (args->nfs_args_ext == NFS_ARGS_EXTB &&
907*39d3e169Sevanl 	    args->nfs_ext_u.nfs_extB.next != NULL) {
9087c478bd9Sstevel@tonic-gate 		if (uap->flags & MS_RDONLY && !(flags & NFSMNT_SOFT)) {
909*39d3e169Sevanl 			data = (char *)args->nfs_ext_u.nfs_extB.next;
9107c478bd9Sstevel@tonic-gate 			goto more;
9117c478bd9Sstevel@tonic-gate 		}
9127c478bd9Sstevel@tonic-gate 		error = EINVAL;
9137c478bd9Sstevel@tonic-gate 		goto errout;
9147c478bd9Sstevel@tonic-gate 	}
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 	/*
9177c478bd9Sstevel@tonic-gate 	 * Determine the zone we're being mounted into.
9187c478bd9Sstevel@tonic-gate 	 */
91945916cd2Sjpk 	zone_hold(mntzone = zone);		/* start with this assumption */
9207c478bd9Sstevel@tonic-gate 	if (getzoneid() == GLOBAL_ZONEID) {
92145916cd2Sjpk 		zone_rele(mntzone);
9227c478bd9Sstevel@tonic-gate 		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
9237c478bd9Sstevel@tonic-gate 		ASSERT(mntzone != NULL);
9247c478bd9Sstevel@tonic-gate 		if (mntzone != zone) {
9257c478bd9Sstevel@tonic-gate 			error = EBUSY;
9267c478bd9Sstevel@tonic-gate 			goto errout;
9277c478bd9Sstevel@tonic-gate 		}
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 
93045916cd2Sjpk 	if (is_system_labeled()) {
93145916cd2Sjpk 		error = nfs_mount_label_policy(vfsp, &svp->sv_addr,
93245916cd2Sjpk 		    svp->sv_knconf, cr);
93345916cd2Sjpk 
93445916cd2Sjpk 		if (error > 0)
93545916cd2Sjpk 			goto errout;
93645916cd2Sjpk 
93745916cd2Sjpk 		if (error == -1) {
93845916cd2Sjpk 			/* change mount to read-only to prevent write-down */
93945916cd2Sjpk 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
94045916cd2Sjpk 		}
94145916cd2Sjpk 	}
94245916cd2Sjpk 
9437c478bd9Sstevel@tonic-gate 	/*
9447c478bd9Sstevel@tonic-gate 	 * Stop the mount from going any further if the zone is going away.
9457c478bd9Sstevel@tonic-gate 	 */
94645916cd2Sjpk 	if (zone_status_get(mntzone) >= ZONE_IS_SHUTTING_DOWN) {
9477c478bd9Sstevel@tonic-gate 		error = EBUSY;
9487c478bd9Sstevel@tonic-gate 		goto errout;
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	/*
9527c478bd9Sstevel@tonic-gate 	 * Get root vnode.
9537c478bd9Sstevel@tonic-gate 	 */
9547c478bd9Sstevel@tonic-gate proceed:
95545916cd2Sjpk 	error = nfs3rootvp(&rtvp, vfsp, svp_head, flags, cr, mntzone);
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	if (error)
9587c478bd9Sstevel@tonic-gate 		goto errout;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	/*
9617c478bd9Sstevel@tonic-gate 	 * Set option fields in the mount info record
9627c478bd9Sstevel@tonic-gate 	 */
9637c478bd9Sstevel@tonic-gate 	mi = VTOMI(rtvp);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	if (svp_head->sv_next)
9667c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_LLOCK;
9677c478bd9Sstevel@tonic-gate 
968*39d3e169Sevanl 	error = nfs_setopts(rtvp, DATAMODEL_NATIVE, args);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate errout:
9717c478bd9Sstevel@tonic-gate 	if (error) {
9727c478bd9Sstevel@tonic-gate 		if (rtvp != NULL) {
9737c478bd9Sstevel@tonic-gate 			rp = VTOR(rtvp);
9747c478bd9Sstevel@tonic-gate 			if (rp->r_flags & RHASHED)
9757c478bd9Sstevel@tonic-gate 				rp_rmhash(rp);
9767c478bd9Sstevel@tonic-gate 		}
9777c478bd9Sstevel@tonic-gate 		sv_free(svp_head);
9787c478bd9Sstevel@tonic-gate 		if (mi != NULL) {
9797c478bd9Sstevel@tonic-gate 			nfs_async_stop(vfsp);
9807c478bd9Sstevel@tonic-gate 			nfs_async_manager_stop(vfsp);
9817c478bd9Sstevel@tonic-gate 			if (mi->mi_io_kstats) {
9827c478bd9Sstevel@tonic-gate 				kstat_delete(mi->mi_io_kstats);
9837c478bd9Sstevel@tonic-gate 				mi->mi_io_kstats = NULL;
9847c478bd9Sstevel@tonic-gate 			}
9857c478bd9Sstevel@tonic-gate 			if (mi->mi_ro_kstats) {
9867c478bd9Sstevel@tonic-gate 				kstat_delete(mi->mi_ro_kstats);
9877c478bd9Sstevel@tonic-gate 				mi->mi_ro_kstats = NULL;
9887c478bd9Sstevel@tonic-gate 			}
9897c478bd9Sstevel@tonic-gate 			nfs_free_mi(mi);
9907c478bd9Sstevel@tonic-gate 		}
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate 
993*39d3e169Sevanl 
994*39d3e169Sevanl 	if (!(uap->flags & MS_SYSSPACE)) {
995*39d3e169Sevanl 		nfs3_free_args(args, fhandle);
996*39d3e169Sevanl 		kmem_free(args, sizeof (*args));
997*39d3e169Sevanl 	}
998*39d3e169Sevanl 
9997c478bd9Sstevel@tonic-gate 	if (rtvp != NULL)
10007c478bd9Sstevel@tonic-gate 		VN_RELE(rtvp);
10017c478bd9Sstevel@tonic-gate 
100245916cd2Sjpk 	if (mntzone != NULL)
100345916cd2Sjpk 		zone_rele(mntzone);
100445916cd2Sjpk 
10057c478bd9Sstevel@tonic-gate 	return (error);
1006*39d3e169Sevanl 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate static int nfs3_dynamic = 0;	/* global variable to enable dynamic retrans. */
10097c478bd9Sstevel@tonic-gate static ushort_t nfs3_max_threads = 8;	/* max number of active async threads */
10107c478bd9Sstevel@tonic-gate static uint_t nfs3_bsize = 32 * 1024;	/* client `block' size */
10117c478bd9Sstevel@tonic-gate static uint_t nfs3_async_clusters = 1;	/* # of reqs from each async queue */
10127c478bd9Sstevel@tonic-gate static uint_t nfs3_cots_timeo = NFS_COTS_TIMEO;
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate static int
10157c478bd9Sstevel@tonic-gate nfs3rootvp(vnode_t **rtvpp, vfs_t *vfsp, struct servinfo *svp,
10167c478bd9Sstevel@tonic-gate 	int flags, cred_t *cr, zone_t *zone)
10177c478bd9Sstevel@tonic-gate {
10187c478bd9Sstevel@tonic-gate 	vnode_t *rtvp;
10197c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
10207c478bd9Sstevel@tonic-gate 	dev_t nfs_dev;
10217c478bd9Sstevel@tonic-gate 	struct vattr va;
10227c478bd9Sstevel@tonic-gate 	struct FSINFO3args args;
10237c478bd9Sstevel@tonic-gate 	struct FSINFO3res res;
10247c478bd9Sstevel@tonic-gate 	int error;
10257c478bd9Sstevel@tonic-gate 	int douprintf;
10267c478bd9Sstevel@tonic-gate 	rnode_t *rp;
10277c478bd9Sstevel@tonic-gate 	int i;
10287c478bd9Sstevel@tonic-gate 	uint_t max_transfer_size;
10297c478bd9Sstevel@tonic-gate 	struct nfs_stats *nfsstatsp;
10307c478bd9Sstevel@tonic-gate 	cred_t *lcr = NULL, *tcr = cr;
10317c478bd9Sstevel@tonic-gate 
1032108322fbScarlsonj 	nfsstatsp = zone_getspecific(nfsstat_zone_key, nfs_zone());
10337c478bd9Sstevel@tonic-gate 	ASSERT(nfsstatsp != NULL);
10347c478bd9Sstevel@tonic-gate 
1035108322fbScarlsonj 	ASSERT(nfs_zone() == zone);
10367c478bd9Sstevel@tonic-gate 	/*
10377c478bd9Sstevel@tonic-gate 	 * Create a mount record and link it to the vfs struct.
10387c478bd9Sstevel@tonic-gate 	 */
10397c478bd9Sstevel@tonic-gate 	mi = kmem_zalloc(sizeof (*mi), KM_SLEEP);
10407c478bd9Sstevel@tonic-gate 	mutex_init(&mi->mi_lock, NULL, MUTEX_DEFAULT, NULL);
1041e8dc3b7dSvv 	mutex_init(&mi->mi_remap_lock, NULL, MUTEX_DEFAULT, NULL);
10427c478bd9Sstevel@tonic-gate 	mi->mi_flags = MI_ACL | MI_EXTATTR;
10437c478bd9Sstevel@tonic-gate 	if (!(flags & NFSMNT_SOFT))
10447c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_HARD;
10457c478bd9Sstevel@tonic-gate 	if ((flags & NFSMNT_SEMISOFT))
10467c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_SEMISOFT;
10477c478bd9Sstevel@tonic-gate 	if ((flags & NFSMNT_NOPRINT))
10487c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_NOPRINT;
10497c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_INT)
10507c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_INT;
10517c478bd9Sstevel@tonic-gate 	mi->mi_retrans = NFS_RETRIES;
10527c478bd9Sstevel@tonic-gate 	if (svp->sv_knconf->knc_semantics == NC_TPI_COTS_ORD ||
10537c478bd9Sstevel@tonic-gate 	    svp->sv_knconf->knc_semantics == NC_TPI_COTS)
10547c478bd9Sstevel@tonic-gate 		mi->mi_timeo = nfs3_cots_timeo;
10557c478bd9Sstevel@tonic-gate 	else
10567c478bd9Sstevel@tonic-gate 		mi->mi_timeo = NFS_TIMEO;
10577c478bd9Sstevel@tonic-gate 	mi->mi_prog = NFS_PROGRAM;
10587c478bd9Sstevel@tonic-gate 	mi->mi_vers = NFS_V3;
10597c478bd9Sstevel@tonic-gate 	mi->mi_rfsnames = rfsnames_v3;
10607c478bd9Sstevel@tonic-gate 	mi->mi_reqs = nfsstatsp->nfs_stats_v3.rfsreqcnt_ptr;
10617c478bd9Sstevel@tonic-gate 	mi->mi_call_type = call_type_v3;
10627c478bd9Sstevel@tonic-gate 	mi->mi_ss_call_type = ss_call_type_v3;
10637c478bd9Sstevel@tonic-gate 	mi->mi_timer_type = timer_type_v3;
10647c478bd9Sstevel@tonic-gate 	mi->mi_aclnames = aclnames_v3;
10657c478bd9Sstevel@tonic-gate 	mi->mi_aclreqs = nfsstatsp->nfs_stats_v3.aclreqcnt_ptr;
10667c478bd9Sstevel@tonic-gate 	mi->mi_acl_call_type = acl_call_type_v3;
10677c478bd9Sstevel@tonic-gate 	mi->mi_acl_ss_call_type = acl_ss_call_type_v3;
10687c478bd9Sstevel@tonic-gate 	mi->mi_acl_timer_type = acl_timer_type_v3;
10697c478bd9Sstevel@tonic-gate 	cv_init(&mi->mi_failover_cv, NULL, CV_DEFAULT, NULL);
10707c478bd9Sstevel@tonic-gate 	mi->mi_servers = svp;
10717c478bd9Sstevel@tonic-gate 	mi->mi_curr_serv = svp;
10727c478bd9Sstevel@tonic-gate 	mi->mi_acregmin = SEC2HR(ACREGMIN);
10737c478bd9Sstevel@tonic-gate 	mi->mi_acregmax = SEC2HR(ACREGMAX);
10747c478bd9Sstevel@tonic-gate 	mi->mi_acdirmin = SEC2HR(ACDIRMIN);
10757c478bd9Sstevel@tonic-gate 	mi->mi_acdirmax = SEC2HR(ACDIRMAX);
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	if (nfs3_dynamic)
10787c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_DYNAMIC;
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_DIRECTIO)
10817c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_DIRECTIO;
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	/*
10847c478bd9Sstevel@tonic-gate 	 * Make a vfs struct for nfs.  We do this here instead of below
10857c478bd9Sstevel@tonic-gate 	 * because rtvp needs a vfs before we can do a getattr on it.
10867c478bd9Sstevel@tonic-gate 	 *
10877c478bd9Sstevel@tonic-gate 	 * Assign a unique device id to the mount
10887c478bd9Sstevel@tonic-gate 	 */
10897c478bd9Sstevel@tonic-gate 	mutex_enter(&nfs_minor_lock);
10907c478bd9Sstevel@tonic-gate 	do {
10917c478bd9Sstevel@tonic-gate 		nfs_minor = (nfs_minor + 1) & MAXMIN32;
10927c478bd9Sstevel@tonic-gate 		nfs_dev = makedevice(nfs_major, nfs_minor);
10937c478bd9Sstevel@tonic-gate 	} while (vfs_devismounted(nfs_dev));
10947c478bd9Sstevel@tonic-gate 	mutex_exit(&nfs_minor_lock);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	vfsp->vfs_dev = nfs_dev;
10977c478bd9Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, nfs_dev, nfs3fstyp);
10987c478bd9Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)mi;
10997c478bd9Sstevel@tonic-gate 	vfsp->vfs_fstype = nfsfstyp;
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate 	/*
11027c478bd9Sstevel@tonic-gate 	 * Verify that nfs3_bsize tuneable is set to an
11037c478bd9Sstevel@tonic-gate 	 * acceptable value.  It be a multiple of PAGESIZE or
11047c478bd9Sstevel@tonic-gate 	 * file corruption can occur.
11057c478bd9Sstevel@tonic-gate 	 */
11067c478bd9Sstevel@tonic-gate 	if (nfs3_bsize & PAGEOFFSET)
11077c478bd9Sstevel@tonic-gate 		nfs3_bsize &= PAGEMASK;
11087c478bd9Sstevel@tonic-gate 	if (nfs3_bsize < PAGESIZE)
11097c478bd9Sstevel@tonic-gate 		nfs3_bsize = PAGESIZE;
11107c478bd9Sstevel@tonic-gate 	vfsp->vfs_bsize = nfs3_bsize;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 	/*
11137c478bd9Sstevel@tonic-gate 	 * Initialize fields used to support async putpage operations.
11147c478bd9Sstevel@tonic-gate 	 */
11157c478bd9Sstevel@tonic-gate 	for (i = 0; i < NFS_ASYNC_TYPES; i++)
11167c478bd9Sstevel@tonic-gate 		mi->mi_async_clusters[i] = nfs3_async_clusters;
11177c478bd9Sstevel@tonic-gate 	mi->mi_async_init_clusters = nfs3_async_clusters;
11187c478bd9Sstevel@tonic-gate 	mi->mi_async_curr = &mi->mi_async_reqs[0];
11197c478bd9Sstevel@tonic-gate 	mi->mi_max_threads = nfs3_max_threads;
11207c478bd9Sstevel@tonic-gate 	mutex_init(&mi->mi_async_lock, NULL, MUTEX_DEFAULT, NULL);
11217c478bd9Sstevel@tonic-gate 	cv_init(&mi->mi_async_reqs_cv, NULL, CV_DEFAULT, NULL);
11227c478bd9Sstevel@tonic-gate 	cv_init(&mi->mi_async_work_cv, NULL, CV_DEFAULT, NULL);
11237c478bd9Sstevel@tonic-gate 	cv_init(&mi->mi_async_cv, NULL, CV_DEFAULT, NULL);
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	mi->mi_vfsp = vfsp;
11267c478bd9Sstevel@tonic-gate 	zone_hold(mi->mi_zone = zone);
11277c478bd9Sstevel@tonic-gate 	nfs_mi_zonelist_add(mi);
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	/*
11307c478bd9Sstevel@tonic-gate 	 * Make the root vnode, use it to get attributes,
11317c478bd9Sstevel@tonic-gate 	 * then remake it with the attributes.
11327c478bd9Sstevel@tonic-gate 	 */
11337c478bd9Sstevel@tonic-gate 	rtvp = makenfs3node((nfs_fh3 *)&svp->sv_fhandle,
11347c478bd9Sstevel@tonic-gate 	    NULL, vfsp, gethrtime(), cr, NULL, NULL);
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	/*
11377c478bd9Sstevel@tonic-gate 	 * Make the FSINFO calls, primarily at this point to
11387c478bd9Sstevel@tonic-gate 	 * determine the transfer size.  For client failover,
11397c478bd9Sstevel@tonic-gate 	 * we'll want this to be the minimum bid from any
11407c478bd9Sstevel@tonic-gate 	 * server, so that we don't overrun stated limits.
11417c478bd9Sstevel@tonic-gate 	 *
11427c478bd9Sstevel@tonic-gate 	 * While we're looping, we'll turn off AUTH_F_TRYNONE,
11437c478bd9Sstevel@tonic-gate 	 * which is only for the mount operation.
11447c478bd9Sstevel@tonic-gate 	 */
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	mi->mi_tsize = nfs3_tsize(svp->sv_knconf);
11477c478bd9Sstevel@tonic-gate 	mi->mi_stsize = mi->mi_tsize;
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate 	mi->mi_curread = nfs3_bsize;
11507c478bd9Sstevel@tonic-gate 	mi->mi_curwrite = mi->mi_curread;
11517c478bd9Sstevel@tonic-gate 
11527c478bd9Sstevel@tonic-gate 	/*
11537c478bd9Sstevel@tonic-gate 	 * If the uid is set then set the creds for secure mounts
11547c478bd9Sstevel@tonic-gate 	 * by proxy processes such as automountd.
11557c478bd9Sstevel@tonic-gate 	 */
11567c478bd9Sstevel@tonic-gate 	if (svp->sv_secdata->uid != 0 &&
11577c478bd9Sstevel@tonic-gate 	    svp->sv_secdata->rpcflavor == RPCSEC_GSS) {
11587c478bd9Sstevel@tonic-gate 		lcr = crdup(cr);
11597c478bd9Sstevel@tonic-gate 		(void) crsetugid(lcr, svp->sv_secdata->uid, crgetgid(cr));
11607c478bd9Sstevel@tonic-gate 		tcr = lcr;
11617c478bd9Sstevel@tonic-gate 	}
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	for (svp = mi->mi_servers; svp != NULL; svp = svp->sv_next) {
11647c478bd9Sstevel@tonic-gate 		douprintf = 1;
11657c478bd9Sstevel@tonic-gate 		mi->mi_curr_serv = svp;
11667c478bd9Sstevel@tonic-gate 		max_transfer_size = nfs3_tsize(svp->sv_knconf);
11677c478bd9Sstevel@tonic-gate 		mi->mi_tsize = MIN(max_transfer_size, mi->mi_tsize);
11687c478bd9Sstevel@tonic-gate 		mi->mi_stsize = MIN(max_transfer_size, mi->mi_stsize);
11697c478bd9Sstevel@tonic-gate 		mi->mi_curread = MIN(max_transfer_size, mi->mi_curread);
11707c478bd9Sstevel@tonic-gate 		mi->mi_curwrite = MIN(max_transfer_size, mi->mi_curwrite);
11717c478bd9Sstevel@tonic-gate 		args.fsroot = *(nfs_fh3 *)&svp->sv_fhandle;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 		error = rfs3call(mi, NFSPROC3_FSINFO,
11747c478bd9Sstevel@tonic-gate 		    xdr_nfs_fh3, (caddr_t)&args,
11757c478bd9Sstevel@tonic-gate 		    xdr_FSINFO3res, (caddr_t)&res, tcr,
11767c478bd9Sstevel@tonic-gate 		    &douprintf, &res.status, 0, NULL);
11777c478bd9Sstevel@tonic-gate 		if (error)
11787c478bd9Sstevel@tonic-gate 			goto bad;
11797c478bd9Sstevel@tonic-gate 		error = geterrno3(res.status);
11807c478bd9Sstevel@tonic-gate 		if (error)
11817c478bd9Sstevel@tonic-gate 			goto bad;
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 		/* get type of root node */
11847c478bd9Sstevel@tonic-gate 		if (res.resok.obj_attributes.attributes) {
11857c478bd9Sstevel@tonic-gate 			if (res.resok.obj_attributes.attr.type < NF3REG ||
11867c478bd9Sstevel@tonic-gate 			    res.resok.obj_attributes.attr.type > NF3FIFO) {
11877c478bd9Sstevel@tonic-gate #ifdef DEBUG
11887c478bd9Sstevel@tonic-gate 				zcmn_err(getzoneid(), CE_WARN,
11897c478bd9Sstevel@tonic-gate 			    "NFS3 server %s returned a bad file type for root",
11907c478bd9Sstevel@tonic-gate 				    svp->sv_hostname);
11917c478bd9Sstevel@tonic-gate #else
11927c478bd9Sstevel@tonic-gate 				zcmn_err(getzoneid(), CE_WARN,
11937c478bd9Sstevel@tonic-gate 			    "NFS server %s returned a bad file type for root",
11947c478bd9Sstevel@tonic-gate 				    svp->sv_hostname);
11957c478bd9Sstevel@tonic-gate #endif
11967c478bd9Sstevel@tonic-gate 				error = EINVAL;
11977c478bd9Sstevel@tonic-gate 				goto bad;
11987c478bd9Sstevel@tonic-gate 			} else {
11997c478bd9Sstevel@tonic-gate 				if (rtvp->v_type != VNON &&
12007c478bd9Sstevel@tonic-gate 		rtvp->v_type != nf3_to_vt[res.resok.obj_attributes.attr.type]) {
12017c478bd9Sstevel@tonic-gate #ifdef DEBUG
12027c478bd9Sstevel@tonic-gate 					zcmn_err(getzoneid(), CE_WARN,
12037c478bd9Sstevel@tonic-gate 		"NFS3 server %s returned a different file type for root",
12047c478bd9Sstevel@tonic-gate 					    svp->sv_hostname);
12057c478bd9Sstevel@tonic-gate #else
12067c478bd9Sstevel@tonic-gate 					zcmn_err(getzoneid(), CE_WARN,
12077c478bd9Sstevel@tonic-gate 		"NFS server %s returned a different file type for root",
12087c478bd9Sstevel@tonic-gate 					    svp->sv_hostname);
12097c478bd9Sstevel@tonic-gate #endif
12107c478bd9Sstevel@tonic-gate 					error = EINVAL;
12117c478bd9Sstevel@tonic-gate 					goto bad;
12127c478bd9Sstevel@tonic-gate 				}
12137c478bd9Sstevel@tonic-gate 				rtvp->v_type =
12147c478bd9Sstevel@tonic-gate 				nf3_to_vt[res.resok.obj_attributes.attr.type];
12157c478bd9Sstevel@tonic-gate 			}
12167c478bd9Sstevel@tonic-gate 		}
12177c478bd9Sstevel@tonic-gate 
12187c478bd9Sstevel@tonic-gate 		if (res.resok.rtmax != 0) {
12197c478bd9Sstevel@tonic-gate 			mi->mi_tsize = MIN(res.resok.rtmax, mi->mi_tsize);
12207c478bd9Sstevel@tonic-gate 			if (res.resok.rtpref != 0) {
12217c478bd9Sstevel@tonic-gate 				mi->mi_curread = MIN(res.resok.rtpref,
12227c478bd9Sstevel@tonic-gate 						    mi->mi_curread);
12237c478bd9Sstevel@tonic-gate 			} else {
12247c478bd9Sstevel@tonic-gate 				mi->mi_curread = MIN(res.resok.rtmax,
12257c478bd9Sstevel@tonic-gate 						    mi->mi_curread);
12267c478bd9Sstevel@tonic-gate 			}
12277c478bd9Sstevel@tonic-gate 		} else if (res.resok.rtpref != 0) {
12287c478bd9Sstevel@tonic-gate 			mi->mi_tsize = MIN(res.resok.rtpref, mi->mi_tsize);
12297c478bd9Sstevel@tonic-gate 			mi->mi_curread = MIN(res.resok.rtpref, mi->mi_curread);
12307c478bd9Sstevel@tonic-gate 		} else {
12317c478bd9Sstevel@tonic-gate #ifdef DEBUG
12327c478bd9Sstevel@tonic-gate 			zcmn_err(getzoneid(), CE_WARN,
12337c478bd9Sstevel@tonic-gate 			    "NFS3 server %s returned 0 for read transfer sizes",
12347c478bd9Sstevel@tonic-gate 			    svp->sv_hostname);
12357c478bd9Sstevel@tonic-gate #else
12367c478bd9Sstevel@tonic-gate 			zcmn_err(getzoneid(), CE_WARN,
12377c478bd9Sstevel@tonic-gate 			    "NFS server %s returned 0 for read transfer sizes",
12387c478bd9Sstevel@tonic-gate 			    svp->sv_hostname);
12397c478bd9Sstevel@tonic-gate #endif
12407c478bd9Sstevel@tonic-gate 			error = EIO;
12417c478bd9Sstevel@tonic-gate 			goto bad;
12427c478bd9Sstevel@tonic-gate 		}
12437c478bd9Sstevel@tonic-gate 		if (res.resok.wtmax != 0) {
12447c478bd9Sstevel@tonic-gate 			mi->mi_stsize = MIN(res.resok.wtmax, mi->mi_stsize);
12457c478bd9Sstevel@tonic-gate 			if (res.resok.wtpref != 0) {
12467c478bd9Sstevel@tonic-gate 				mi->mi_curwrite = MIN(res.resok.wtpref,
12477c478bd9Sstevel@tonic-gate 						    mi->mi_curwrite);
12487c478bd9Sstevel@tonic-gate 			} else {
12497c478bd9Sstevel@tonic-gate 				mi->mi_curwrite = MIN(res.resok.wtmax,
12507c478bd9Sstevel@tonic-gate 						    mi->mi_curwrite);
12517c478bd9Sstevel@tonic-gate 			}
12527c478bd9Sstevel@tonic-gate 		} else if (res.resok.wtpref != 0) {
12537c478bd9Sstevel@tonic-gate 			mi->mi_stsize = MIN(res.resok.wtpref, mi->mi_stsize);
12547c478bd9Sstevel@tonic-gate 			mi->mi_curwrite = MIN(res.resok.wtpref,
12557c478bd9Sstevel@tonic-gate 					    mi->mi_curwrite);
12567c478bd9Sstevel@tonic-gate 		} else {
12577c478bd9Sstevel@tonic-gate #ifdef DEBUG
12587c478bd9Sstevel@tonic-gate 			zcmn_err(getzoneid(), CE_WARN,
12597c478bd9Sstevel@tonic-gate 			"NFS3 server %s returned 0 for write transfer sizes",
12607c478bd9Sstevel@tonic-gate 			    svp->sv_hostname);
12617c478bd9Sstevel@tonic-gate #else
12627c478bd9Sstevel@tonic-gate 			zcmn_err(getzoneid(), CE_WARN,
12637c478bd9Sstevel@tonic-gate 			"NFS server %s returned 0 for write transfer sizes",
12647c478bd9Sstevel@tonic-gate 			    svp->sv_hostname);
12657c478bd9Sstevel@tonic-gate #endif
12667c478bd9Sstevel@tonic-gate 			error = EIO;
12677c478bd9Sstevel@tonic-gate 			goto bad;
12687c478bd9Sstevel@tonic-gate 		}
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 		/*
12717c478bd9Sstevel@tonic-gate 		 * These signal the ability of the server to create
12727c478bd9Sstevel@tonic-gate 		 * hard links and symbolic links, so they really
12737c478bd9Sstevel@tonic-gate 		 * aren't relevant if there is more than one server.
12747c478bd9Sstevel@tonic-gate 		 * We'll set them here, though it probably looks odd.
12757c478bd9Sstevel@tonic-gate 		 */
12767c478bd9Sstevel@tonic-gate 		if (res.resok.properties & FSF3_LINK)
12777c478bd9Sstevel@tonic-gate 			mi->mi_flags |= MI_LINK;
12787c478bd9Sstevel@tonic-gate 		if (res.resok.properties & FSF3_SYMLINK)
12797c478bd9Sstevel@tonic-gate 			mi->mi_flags |= MI_SYMLINK;
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 		/* Pick up smallest non-zero maxfilesize value */
12827c478bd9Sstevel@tonic-gate 		if (res.resok.maxfilesize) {
12837c478bd9Sstevel@tonic-gate 			if (mi->mi_maxfilesize) {
12847c478bd9Sstevel@tonic-gate 				mi->mi_maxfilesize = MIN(mi->mi_maxfilesize,
12857c478bd9Sstevel@tonic-gate 							res.resok.maxfilesize);
12867c478bd9Sstevel@tonic-gate 			} else
12877c478bd9Sstevel@tonic-gate 				mi->mi_maxfilesize = res.resok.maxfilesize;
12887c478bd9Sstevel@tonic-gate 		}
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 		/*
12917c478bd9Sstevel@tonic-gate 		 * AUTH_F_TRYNONE is only for the mount operation,
12927c478bd9Sstevel@tonic-gate 		 * so turn it back off.
12937c478bd9Sstevel@tonic-gate 		 */
12947c478bd9Sstevel@tonic-gate 		svp->sv_secdata->flags &= ~AUTH_F_TRYNONE;
12957c478bd9Sstevel@tonic-gate 	}
12967c478bd9Sstevel@tonic-gate 	mi->mi_curr_serv = mi->mi_servers;
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	/*
12997c478bd9Sstevel@tonic-gate 	 * Start the thread responsible for handling async worker threads.
13007c478bd9Sstevel@tonic-gate 	 */
13017c478bd9Sstevel@tonic-gate 	VFS_HOLD(vfsp);	/* add reference for thread */
13027c478bd9Sstevel@tonic-gate 	mi->mi_manager_thread = zthread_create(NULL, 0, nfs_async_manager,
13037c478bd9Sstevel@tonic-gate 					vfsp, 0, minclsyspri);
13047c478bd9Sstevel@tonic-gate 	ASSERT(mi->mi_manager_thread != NULL);
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	/*
13077c478bd9Sstevel@tonic-gate 	 * Initialize kstats
13087c478bd9Sstevel@tonic-gate 	 */
13097c478bd9Sstevel@tonic-gate 	nfs_mnt_kstat_init(vfsp);
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	/* If we didn't get a type, get one now */
13127c478bd9Sstevel@tonic-gate 	if (rtvp->v_type == VNON) {
13137c478bd9Sstevel@tonic-gate 		va.va_mask = AT_ALL;
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 		error = nfs3getattr(rtvp, &va, tcr);
13167c478bd9Sstevel@tonic-gate 		if (error)
13177c478bd9Sstevel@tonic-gate 			goto bad;
13187c478bd9Sstevel@tonic-gate 		rtvp->v_type = va.va_type;
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	mi->mi_type = rtvp->v_type;
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	*rtvpp = rtvp;
13247c478bd9Sstevel@tonic-gate 	if (lcr != NULL)
13257c478bd9Sstevel@tonic-gate 		crfree(lcr);
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 	return (0);
13287c478bd9Sstevel@tonic-gate bad:
13297c478bd9Sstevel@tonic-gate 	/*
13307c478bd9Sstevel@tonic-gate 	 * An error occurred somewhere, need to clean up...
13317c478bd9Sstevel@tonic-gate 	 * We need to release our reference to the root vnode and
13327c478bd9Sstevel@tonic-gate 	 * destroy the mntinfo struct that we just created.
13337c478bd9Sstevel@tonic-gate 	 */
13347c478bd9Sstevel@tonic-gate 	if (lcr != NULL)
13357c478bd9Sstevel@tonic-gate 		crfree(lcr);
13367c478bd9Sstevel@tonic-gate 	rp = VTOR(rtvp);
13377c478bd9Sstevel@tonic-gate 	if (rp->r_flags & RHASHED)
13387c478bd9Sstevel@tonic-gate 		rp_rmhash(rp);
13397c478bd9Sstevel@tonic-gate 	VN_RELE(rtvp);
13407c478bd9Sstevel@tonic-gate 	nfs_async_stop(vfsp);
13417c478bd9Sstevel@tonic-gate 	nfs_async_manager_stop(vfsp);
13427c478bd9Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
13437c478bd9Sstevel@tonic-gate 		kstat_delete(mi->mi_io_kstats);
13447c478bd9Sstevel@tonic-gate 		mi->mi_io_kstats = NULL;
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 	if (mi->mi_ro_kstats) {
13477c478bd9Sstevel@tonic-gate 		kstat_delete(mi->mi_ro_kstats);
13487c478bd9Sstevel@tonic-gate 		mi->mi_ro_kstats = NULL;
13497c478bd9Sstevel@tonic-gate 	}
13507c478bd9Sstevel@tonic-gate 	nfs_free_mi(mi);
13517c478bd9Sstevel@tonic-gate 	*rtvpp = NULL;
13527c478bd9Sstevel@tonic-gate 	return (error);
13537c478bd9Sstevel@tonic-gate }
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate /*
13567c478bd9Sstevel@tonic-gate  * vfs operations
13577c478bd9Sstevel@tonic-gate  */
13587c478bd9Sstevel@tonic-gate static int
13597c478bd9Sstevel@tonic-gate nfs3_unmount(vfs_t *vfsp, int flag, cred_t *cr)
13607c478bd9Sstevel@tonic-gate {
13617c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
13627c478bd9Sstevel@tonic-gate 	ushort_t omax;
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
13657c478bd9Sstevel@tonic-gate 		return (EPERM);
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	mi = VFTOMI(vfsp);
13687c478bd9Sstevel@tonic-gate 	if (flag & MS_FORCE) {
13697c478bd9Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_UNMOUNTED;
13707c478bd9Sstevel@tonic-gate 		/*
13717c478bd9Sstevel@tonic-gate 		 * We need to stop the manager thread explicitly; the worker
13727c478bd9Sstevel@tonic-gate 		 * threads can time out and exit on their own.
13737c478bd9Sstevel@tonic-gate 		 */
13747c478bd9Sstevel@tonic-gate 		nfs_async_manager_stop(vfsp);
13757c478bd9Sstevel@tonic-gate 		destroy_rtable(vfsp, cr);
13767c478bd9Sstevel@tonic-gate 		if (mi->mi_io_kstats) {
13777c478bd9Sstevel@tonic-gate 			kstat_delete(mi->mi_io_kstats);
13787c478bd9Sstevel@tonic-gate 			mi->mi_io_kstats = NULL;
13797c478bd9Sstevel@tonic-gate 		}
13807c478bd9Sstevel@tonic-gate 		if (mi->mi_ro_kstats) {
13817c478bd9Sstevel@tonic-gate 			kstat_delete(mi->mi_ro_kstats);
13827c478bd9Sstevel@tonic-gate 			mi->mi_ro_kstats = NULL;
13837c478bd9Sstevel@tonic-gate 		}
13847c478bd9Sstevel@tonic-gate 		return (0);
13857c478bd9Sstevel@tonic-gate 	}
13867c478bd9Sstevel@tonic-gate 	/*
13877c478bd9Sstevel@tonic-gate 	 * Wait until all asynchronous putpage operations on
13887c478bd9Sstevel@tonic-gate 	 * this file system are complete before flushing rnodes
13897c478bd9Sstevel@tonic-gate 	 * from the cache.
13907c478bd9Sstevel@tonic-gate 	 */
13917c478bd9Sstevel@tonic-gate 	omax = mi->mi_max_threads;
13927c478bd9Sstevel@tonic-gate 	if (nfs_async_stop_sig(vfsp)) {
13937c478bd9Sstevel@tonic-gate 		return (EINTR);
13947c478bd9Sstevel@tonic-gate 	}
13957c478bd9Sstevel@tonic-gate 	rflush(vfsp, cr);
13967c478bd9Sstevel@tonic-gate 	/*
13977c478bd9Sstevel@tonic-gate 	 * If there are any active vnodes on this file system,
13987c478bd9Sstevel@tonic-gate 	 * then the file system is busy and can't be umounted.
13997c478bd9Sstevel@tonic-gate 	 */
14007c478bd9Sstevel@tonic-gate 	if (check_rtable(vfsp)) {
14017c478bd9Sstevel@tonic-gate 		mutex_enter(&mi->mi_async_lock);
14027c478bd9Sstevel@tonic-gate 		mi->mi_max_threads = omax;
14037c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
14047c478bd9Sstevel@tonic-gate 		return (EBUSY);
14057c478bd9Sstevel@tonic-gate 	}
14067c478bd9Sstevel@tonic-gate 	/*
14077c478bd9Sstevel@tonic-gate 	 * The unmount can't fail from now on; stop the worker thread manager.
14087c478bd9Sstevel@tonic-gate 	 */
14097c478bd9Sstevel@tonic-gate 	nfs_async_manager_stop(vfsp);
14107c478bd9Sstevel@tonic-gate 	/*
14117c478bd9Sstevel@tonic-gate 	 * Destroy all rnodes belonging to this file system from the
14127c478bd9Sstevel@tonic-gate 	 * rnode hash queues and purge any resources allocated to
14137c478bd9Sstevel@tonic-gate 	 * them.
14147c478bd9Sstevel@tonic-gate 	 */
14157c478bd9Sstevel@tonic-gate 	destroy_rtable(vfsp, cr);
14167c478bd9Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
14177c478bd9Sstevel@tonic-gate 		kstat_delete(mi->mi_io_kstats);
14187c478bd9Sstevel@tonic-gate 		mi->mi_io_kstats = NULL;
14197c478bd9Sstevel@tonic-gate 	}
14207c478bd9Sstevel@tonic-gate 	if (mi->mi_ro_kstats) {
14217c478bd9Sstevel@tonic-gate 		kstat_delete(mi->mi_ro_kstats);
14227c478bd9Sstevel@tonic-gate 		mi->mi_ro_kstats = NULL;
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 	return (0);
14257c478bd9Sstevel@tonic-gate }
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate /*
14287c478bd9Sstevel@tonic-gate  * find root of nfs
14297c478bd9Sstevel@tonic-gate  */
14307c478bd9Sstevel@tonic-gate static int
14317c478bd9Sstevel@tonic-gate nfs3_root(vfs_t *vfsp, vnode_t **vpp)
14327c478bd9Sstevel@tonic-gate {
14337c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
14347c478bd9Sstevel@tonic-gate 	vnode_t *vp;
14357c478bd9Sstevel@tonic-gate 	servinfo_t *svp;
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 	mi = VFTOMI(vfsp);
14387c478bd9Sstevel@tonic-gate 
1439108322fbScarlsonj 	if (nfs_zone() != mi->mi_zone)
14407c478bd9Sstevel@tonic-gate 		return (EPERM);
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate 	svp = mi->mi_curr_serv;
14437c478bd9Sstevel@tonic-gate 	if (svp && (svp->sv_flags & SV_ROOT_STALE)) {
14447c478bd9Sstevel@tonic-gate 		mutex_enter(&svp->sv_lock);
14457c478bd9Sstevel@tonic-gate 		svp->sv_flags &= ~SV_ROOT_STALE;
14467c478bd9Sstevel@tonic-gate 		mutex_exit(&svp->sv_lock);
14477c478bd9Sstevel@tonic-gate 		return (ENOENT);
14487c478bd9Sstevel@tonic-gate 	}
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	vp = makenfs3node((nfs_fh3 *)&mi->mi_curr_serv->sv_fhandle,
14517c478bd9Sstevel@tonic-gate 	    NULL, vfsp, gethrtime(), CRED(), NULL, NULL);
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	if (VTOR(vp)->r_flags & RSTALE) {
14547c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
14557c478bd9Sstevel@tonic-gate 		return (ENOENT);
14567c478bd9Sstevel@tonic-gate 	}
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VNON || vp->v_type == mi->mi_type);
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	vp->v_type = mi->mi_type;
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	*vpp = vp;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 	return (0);
14657c478bd9Sstevel@tonic-gate }
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate /*
14687c478bd9Sstevel@tonic-gate  * Get file system statistics.
14697c478bd9Sstevel@tonic-gate  */
14707c478bd9Sstevel@tonic-gate static int
14717c478bd9Sstevel@tonic-gate nfs3_statvfs(vfs_t *vfsp, struct statvfs64 *sbp)
14727c478bd9Sstevel@tonic-gate {
14737c478bd9Sstevel@tonic-gate 	int error;
14747c478bd9Sstevel@tonic-gate 	struct mntinfo *mi;
14757c478bd9Sstevel@tonic-gate 	struct FSSTAT3args args;
14767c478bd9Sstevel@tonic-gate 	struct FSSTAT3res res;
14777c478bd9Sstevel@tonic-gate 	int douprintf;
14787c478bd9Sstevel@tonic-gate 	failinfo_t fi;
14797c478bd9Sstevel@tonic-gate 	vnode_t *vp;
14807c478bd9Sstevel@tonic-gate 	cred_t *cr;
14817c478bd9Sstevel@tonic-gate 	hrtime_t t;
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	mi = VFTOMI(vfsp);
1484108322fbScarlsonj 	if (nfs_zone() != mi->mi_zone)
14857c478bd9Sstevel@tonic-gate 		return (EPERM);
14867c478bd9Sstevel@tonic-gate 	error = nfs3_root(vfsp, &vp);
14877c478bd9Sstevel@tonic-gate 	if (error)
14887c478bd9Sstevel@tonic-gate 		return (error);
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	cr = CRED();
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 	args.fsroot = *VTOFH3(vp);
14937c478bd9Sstevel@tonic-gate 	fi.vp = vp;
14947c478bd9Sstevel@tonic-gate 	fi.fhp = (caddr_t)&args.fsroot;
14957c478bd9Sstevel@tonic-gate 	fi.copyproc = nfs3copyfh;
14967c478bd9Sstevel@tonic-gate 	fi.lookupproc = nfs3lookup;
14977c478bd9Sstevel@tonic-gate 	fi.xattrdirproc = acl_getxattrdir3;
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	douprintf = 1;
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	t = gethrtime();
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	error = rfs3call(mi, NFSPROC3_FSSTAT,
15047c478bd9Sstevel@tonic-gate 	    xdr_nfs_fh3, (caddr_t)&args,
15057c478bd9Sstevel@tonic-gate 	    xdr_FSSTAT3res, (caddr_t)&res, cr,
15067c478bd9Sstevel@tonic-gate 	    &douprintf, &res.status, 0, &fi);
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	if (error) {
15097c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
15107c478bd9Sstevel@tonic-gate 		return (error);
15117c478bd9Sstevel@tonic-gate 	}
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	error = geterrno3(res.status);
15147c478bd9Sstevel@tonic-gate 	if (!error) {
15157c478bd9Sstevel@tonic-gate 		nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr);
15167c478bd9Sstevel@tonic-gate 		sbp->f_bsize = MAXBSIZE;
15177c478bd9Sstevel@tonic-gate 		sbp->f_frsize = DEV_BSIZE;
15187c478bd9Sstevel@tonic-gate 		/*
15197c478bd9Sstevel@tonic-gate 		 * Allow -1 fields to pass through unconverted.  These
15207c478bd9Sstevel@tonic-gate 		 * indicate "don't know" fields.
15217c478bd9Sstevel@tonic-gate 		 */
15227c478bd9Sstevel@tonic-gate 		if (res.resok.tbytes == (size3)-1)
15237c478bd9Sstevel@tonic-gate 			sbp->f_blocks = (fsblkcnt64_t)res.resok.tbytes;
15247c478bd9Sstevel@tonic-gate 		else {
15257c478bd9Sstevel@tonic-gate 			sbp->f_blocks = (fsblkcnt64_t)
15267c478bd9Sstevel@tonic-gate 			    (res.resok.tbytes / DEV_BSIZE);
15277c478bd9Sstevel@tonic-gate 		}
15287c478bd9Sstevel@tonic-gate 		if (res.resok.fbytes == (size3)-1)
15297c478bd9Sstevel@tonic-gate 			sbp->f_bfree = (fsblkcnt64_t)res.resok.fbytes;
15307c478bd9Sstevel@tonic-gate 		else {
15317c478bd9Sstevel@tonic-gate 			sbp->f_bfree = (fsblkcnt64_t)
15327c478bd9Sstevel@tonic-gate 			    (res.resok.fbytes / DEV_BSIZE);
15337c478bd9Sstevel@tonic-gate 		}
15347c478bd9Sstevel@tonic-gate 		if (res.resok.abytes == (size3)-1)
15357c478bd9Sstevel@tonic-gate 			sbp->f_bavail = (fsblkcnt64_t)res.resok.abytes;
15367c478bd9Sstevel@tonic-gate 		else {
15377c478bd9Sstevel@tonic-gate 			sbp->f_bavail = (fsblkcnt64_t)
15387c478bd9Sstevel@tonic-gate 			    (res.resok.abytes / DEV_BSIZE);
15397c478bd9Sstevel@tonic-gate 		}
15407c478bd9Sstevel@tonic-gate 		sbp->f_files = (fsfilcnt64_t)res.resok.tfiles;
15417c478bd9Sstevel@tonic-gate 		sbp->f_ffree = (fsfilcnt64_t)res.resok.ffiles;
15427c478bd9Sstevel@tonic-gate 		sbp->f_favail = (fsfilcnt64_t)res.resok.afiles;
15437c478bd9Sstevel@tonic-gate 		sbp->f_fsid = (unsigned long)vfsp->vfs_fsid.val[0];
15447c478bd9Sstevel@tonic-gate 		(void) strncpy(sbp->f_basetype,
15457c478bd9Sstevel@tonic-gate 		    vfssw[vfsp->vfs_fstype].vsw_name, FSTYPSZ);
15467c478bd9Sstevel@tonic-gate 		sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
15477c478bd9Sstevel@tonic-gate 		sbp->f_namemax = (ulong_t)-1;
15487c478bd9Sstevel@tonic-gate 	} else {
15497c478bd9Sstevel@tonic-gate 		nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr);
15507c478bd9Sstevel@tonic-gate 		PURGE_STALE_FH(error, vp, cr);
15517c478bd9Sstevel@tonic-gate 	}
15527c478bd9Sstevel@tonic-gate 
15537c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	return (error);
15567c478bd9Sstevel@tonic-gate }
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate static kmutex_t nfs3_syncbusy;
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate /*
15617c478bd9Sstevel@tonic-gate  * Flush dirty nfs files for file system vfsp.
15627c478bd9Sstevel@tonic-gate  * If vfsp == NULL, all nfs files are flushed.
15637c478bd9Sstevel@tonic-gate  */
15647c478bd9Sstevel@tonic-gate /* ARGSUSED */
15657c478bd9Sstevel@tonic-gate static int
15667c478bd9Sstevel@tonic-gate nfs3_sync(vfs_t *vfsp, short flag, cred_t *cr)
15677c478bd9Sstevel@tonic-gate {
15687c478bd9Sstevel@tonic-gate 	/*
15697c478bd9Sstevel@tonic-gate 	 * Cross-zone calls are OK here, since this translates to a
15707c478bd9Sstevel@tonic-gate 	 * VOP_PUTPAGE(B_ASYNC), which gets picked up by the right zone.
15717c478bd9Sstevel@tonic-gate 	 */
15727c478bd9Sstevel@tonic-gate 	if (!(flag & SYNC_ATTR) && mutex_tryenter(&nfs3_syncbusy) != 0) {
15737c478bd9Sstevel@tonic-gate 		rflush(vfsp, cr);
15747c478bd9Sstevel@tonic-gate 		mutex_exit(&nfs3_syncbusy);
15757c478bd9Sstevel@tonic-gate 	}
15767c478bd9Sstevel@tonic-gate 	return (0);
15777c478bd9Sstevel@tonic-gate }
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate /* ARGSUSED */
15807c478bd9Sstevel@tonic-gate static int
15817c478bd9Sstevel@tonic-gate nfs3_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
15827c478bd9Sstevel@tonic-gate {
15837c478bd9Sstevel@tonic-gate 	int error;
15847c478bd9Sstevel@tonic-gate 	nfs_fh3 fh;
15857c478bd9Sstevel@tonic-gate 	vnode_t *vp;
15867c478bd9Sstevel@tonic-gate 	struct vattr va;
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	if (fidp->fid_len > NFS3_FHSIZE) {
15897c478bd9Sstevel@tonic-gate 		*vpp = NULL;
15907c478bd9Sstevel@tonic-gate 		return (ESTALE);
15917c478bd9Sstevel@tonic-gate 	}
15927c478bd9Sstevel@tonic-gate 
1593108322fbScarlsonj 	if (nfs_zone() != VFTOMI(vfsp)->mi_zone)
15947c478bd9Sstevel@tonic-gate 		return (EPERM);
15957c478bd9Sstevel@tonic-gate 	fh.fh3_length = fidp->fid_len;
15967c478bd9Sstevel@tonic-gate 	bcopy(fidp->fid_data, fh.fh3_u.data, fh.fh3_length);
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 	vp = makenfs3node(&fh, NULL, vfsp, gethrtime(), CRED(), NULL, NULL);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	if (VTOR(vp)->r_flags & RSTALE) {
16017c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
16027c478bd9Sstevel@tonic-gate 		*vpp = NULL;
16037c478bd9Sstevel@tonic-gate 		return (ENOENT);
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	if (vp->v_type == VNON) {
16077c478bd9Sstevel@tonic-gate 		va.va_mask = AT_ALL;
16087c478bd9Sstevel@tonic-gate 		error = nfs3getattr(vp, &va, CRED());
16097c478bd9Sstevel@tonic-gate 		if (error) {
16107c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
16117c478bd9Sstevel@tonic-gate 			*vpp = NULL;
16127c478bd9Sstevel@tonic-gate 			return (error);
16137c478bd9Sstevel@tonic-gate 		}
16147c478bd9Sstevel@tonic-gate 		vp->v_type = va.va_type;
16157c478bd9Sstevel@tonic-gate 	}
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	*vpp = vp;
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 	return (0);
16207c478bd9Sstevel@tonic-gate }
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate /* ARGSUSED */
16237c478bd9Sstevel@tonic-gate static int
16247c478bd9Sstevel@tonic-gate nfs3_mountroot(vfs_t *vfsp, whymountroot_t why)
16257c478bd9Sstevel@tonic-gate {
16267c478bd9Sstevel@tonic-gate 	vnode_t *rtvp;
16277c478bd9Sstevel@tonic-gate 	char root_hostname[SYS_NMLN+1];
16287c478bd9Sstevel@tonic-gate 	struct servinfo *svp;
16297c478bd9Sstevel@tonic-gate 	int error;
16307c478bd9Sstevel@tonic-gate 	int vfsflags;
16317c478bd9Sstevel@tonic-gate 	size_t size;
16327c478bd9Sstevel@tonic-gate 	char *root_path;
16337c478bd9Sstevel@tonic-gate 	struct pathname pn;
16347c478bd9Sstevel@tonic-gate 	char *name;
16357c478bd9Sstevel@tonic-gate 	cred_t *cr;
16367c478bd9Sstevel@tonic-gate 	struct nfs_args args;		/* nfs mount arguments */
16377c478bd9Sstevel@tonic-gate 	static char token[10];
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	bzero(&args, sizeof (args));
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	/* do this BEFORE getfile which causes xid stamps to be initialized */
16427c478bd9Sstevel@tonic-gate 	clkset(-1L);		/* hack for now - until we get time svc? */
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	if (why == ROOT_REMOUNT) {
16457c478bd9Sstevel@tonic-gate 		/*
16467c478bd9Sstevel@tonic-gate 		 * Shouldn't happen.
16477c478bd9Sstevel@tonic-gate 		 */
16487c478bd9Sstevel@tonic-gate 		panic("nfs3_mountroot: why == ROOT_REMOUNT");
16497c478bd9Sstevel@tonic-gate 	}
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	if (why == ROOT_UNMOUNT) {
16527c478bd9Sstevel@tonic-gate 		/*
16537c478bd9Sstevel@tonic-gate 		 * Nothing to do for NFS.
16547c478bd9Sstevel@tonic-gate 		 */
16557c478bd9Sstevel@tonic-gate 		return (0);
16567c478bd9Sstevel@tonic-gate 	}
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	/*
16597c478bd9Sstevel@tonic-gate 	 * why == ROOT_INIT
16607c478bd9Sstevel@tonic-gate 	 */
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	name = token;
16637c478bd9Sstevel@tonic-gate 	*name = 0;
16647c478bd9Sstevel@tonic-gate 	getfsname("root", name, sizeof (token));
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	pn_alloc(&pn);
16677c478bd9Sstevel@tonic-gate 	root_path = pn.pn_path;
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
16707c478bd9Sstevel@tonic-gate 	svp->sv_knconf = kmem_zalloc(sizeof (*svp->sv_knconf), KM_SLEEP);
16717c478bd9Sstevel@tonic-gate 	svp->sv_knconf->knc_protofmly = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
16727c478bd9Sstevel@tonic-gate 	svp->sv_knconf->knc_proto = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	/*
16757c478bd9Sstevel@tonic-gate 	 * Get server address
16767c478bd9Sstevel@tonic-gate 	 * Get the root fhandle
16777c478bd9Sstevel@tonic-gate 	 * Get server's transport
16787c478bd9Sstevel@tonic-gate 	 * Get server's hostname
16797c478bd9Sstevel@tonic-gate 	 * Get options
16807c478bd9Sstevel@tonic-gate 	 */
16817c478bd9Sstevel@tonic-gate 	args.addr = &svp->sv_addr;
16827c478bd9Sstevel@tonic-gate 	args.fh = (char *)&svp->sv_fhandle;
16837c478bd9Sstevel@tonic-gate 	args.knconf = svp->sv_knconf;
16847c478bd9Sstevel@tonic-gate 	args.hostname = root_hostname;
16857c478bd9Sstevel@tonic-gate 	vfsflags = 0;
16867c478bd9Sstevel@tonic-gate 	if (error = mount_root(*name ? name : "root", root_path, NFS_V3,
16877c478bd9Sstevel@tonic-gate 	    &args, &vfsflags)) {
16887c478bd9Sstevel@tonic-gate 		if (error == EPROTONOSUPPORT)
16897c478bd9Sstevel@tonic-gate 			nfs_cmn_err(error, CE_WARN, "nfs3_mountroot: "
16907c478bd9Sstevel@tonic-gate 			    "mount_root failed: server doesn't support NFS V3");
16917c478bd9Sstevel@tonic-gate 		else
16927c478bd9Sstevel@tonic-gate 			nfs_cmn_err(error, CE_WARN,
16937c478bd9Sstevel@tonic-gate 			    "nfs3_mountroot: mount_root failed: %m");
16947c478bd9Sstevel@tonic-gate 		sv_free(svp);
16957c478bd9Sstevel@tonic-gate 		pn_free(&pn);
16967c478bd9Sstevel@tonic-gate 		return (error);
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 	svp->sv_hostnamelen = (int)(strlen(root_hostname) + 1);
16997c478bd9Sstevel@tonic-gate 	svp->sv_hostname = kmem_alloc(svp->sv_hostnamelen, KM_SLEEP);
17007c478bd9Sstevel@tonic-gate 	(void) strcpy(svp->sv_hostname, root_hostname);
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	/*
17037c478bd9Sstevel@tonic-gate 	 * Force root partition to always be mounted with AUTH_UNIX for now
17047c478bd9Sstevel@tonic-gate 	 */
17057c478bd9Sstevel@tonic-gate 	svp->sv_secdata = kmem_alloc(sizeof (*svp->sv_secdata), KM_SLEEP);
17067c478bd9Sstevel@tonic-gate 	svp->sv_secdata->secmod = AUTH_UNIX;
17077c478bd9Sstevel@tonic-gate 	svp->sv_secdata->rpcflavor = AUTH_UNIX;
17087c478bd9Sstevel@tonic-gate 	svp->sv_secdata->data = NULL;
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	cr = crgetcred();
17117c478bd9Sstevel@tonic-gate 	rtvp = NULL;
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	error = nfs3rootvp(&rtvp, vfsp, svp, args.flags, cr, global_zone);
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 	crfree(cr);
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	if (error) {
17187c478bd9Sstevel@tonic-gate 		pn_free(&pn);
17197c478bd9Sstevel@tonic-gate 		goto errout;
17207c478bd9Sstevel@tonic-gate 	}
17217c478bd9Sstevel@tonic-gate 
17227c478bd9Sstevel@tonic-gate 	error = nfs_setopts(rtvp, DATAMODEL_NATIVE, &args);
17237c478bd9Sstevel@tonic-gate 	if (error) {
17247c478bd9Sstevel@tonic-gate 		nfs_cmn_err(error, CE_WARN,
17257c478bd9Sstevel@tonic-gate 		    "nfs3_mountroot: invalid root mount options");
17267c478bd9Sstevel@tonic-gate 		pn_free(&pn);
17277c478bd9Sstevel@tonic-gate 		goto errout;
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	(void) vfs_lock_wait(vfsp);
17317c478bd9Sstevel@tonic-gate 	vfs_add(NULL, vfsp, vfsflags);
17327c478bd9Sstevel@tonic-gate 	vfs_unlock(vfsp);
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	size = strlen(svp->sv_hostname);
17357c478bd9Sstevel@tonic-gate 	(void) strcpy(rootfs.bo_name, svp->sv_hostname);
17367c478bd9Sstevel@tonic-gate 	rootfs.bo_name[size] = ':';
17377c478bd9Sstevel@tonic-gate 	(void) strcpy(&rootfs.bo_name[size + 1], root_path);
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate 	pn_free(&pn);
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate errout:
17427c478bd9Sstevel@tonic-gate 	if (error) {
17437c478bd9Sstevel@tonic-gate 		sv_free(svp);
17447c478bd9Sstevel@tonic-gate 		nfs_async_stop(vfsp);
17457c478bd9Sstevel@tonic-gate 		nfs_async_manager_stop(vfsp);
17467c478bd9Sstevel@tonic-gate 	}
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 	if (rtvp != NULL)
17497c478bd9Sstevel@tonic-gate 		VN_RELE(rtvp);
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	return (error);
17527c478bd9Sstevel@tonic-gate }
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate /*
17557c478bd9Sstevel@tonic-gate  * Initialization routine for VFS routines.  Should only be called once
17567c478bd9Sstevel@tonic-gate  */
17577c478bd9Sstevel@tonic-gate int
17587c478bd9Sstevel@tonic-gate nfs3_vfsinit(void)
17597c478bd9Sstevel@tonic-gate {
17607c478bd9Sstevel@tonic-gate 	mutex_init(&nfs3_syncbusy, NULL, MUTEX_DEFAULT, NULL);
17617c478bd9Sstevel@tonic-gate 	return (0);
17627c478bd9Sstevel@tonic-gate }
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate void
17657c478bd9Sstevel@tonic-gate nfs3_vfsfini(void)
17667c478bd9Sstevel@tonic-gate {
17677c478bd9Sstevel@tonic-gate 	mutex_destroy(&nfs3_syncbusy);
17687c478bd9Sstevel@tonic-gate }
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate void
17717c478bd9Sstevel@tonic-gate nfs3_freevfs(vfs_t *vfsp)
17727c478bd9Sstevel@tonic-gate {
17737c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
17747c478bd9Sstevel@tonic-gate 	servinfo_t *svp;
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate 	/* free up the resources */
17777c478bd9Sstevel@tonic-gate 	mi = VFTOMI(vfsp);
17787c478bd9Sstevel@tonic-gate 	svp = mi->mi_servers;
17797c478bd9Sstevel@tonic-gate 	mi->mi_servers = mi->mi_curr_serv = NULL;
17807c478bd9Sstevel@tonic-gate 	sv_free(svp);
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	/*
17837c478bd9Sstevel@tonic-gate 	 * By this time we should have already deleted the
17847c478bd9Sstevel@tonic-gate 	 * mi kstats in the unmount code. If they are still around
17857c478bd9Sstevel@tonic-gate 	 * somethings wrong
17867c478bd9Sstevel@tonic-gate 	 */
17877c478bd9Sstevel@tonic-gate 	ASSERT(mi->mi_io_kstats == NULL);
17887c478bd9Sstevel@tonic-gate 	nfs_free_mi(mi);
17897c478bd9Sstevel@tonic-gate }
1790