xref: /titanic_51/usr/src/uts/common/fs/nfs/nfs_vfsops.c (revision 8a790dc67da1bad097488e7ad4a58af32d7ab24d)
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 /*
22a19609f8Sjv227347  * Copyright (c) 1986, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
257c478bd9Sstevel@tonic-gate  *	All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/param.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/systm.h>
317c478bd9Sstevel@tonic-gate #include <sys/cred.h>
327c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
33aa59c4cbSrsb #include <sys/vfs_opreg.h>
347c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
357c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
367c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
397c478bd9Sstevel@tonic-gate #include <sys/mount.h>
407c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
417c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/debug.h>
447c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
457c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
467c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
477c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
487c478bd9Sstevel@tonic-gate #include <sys/acl.h>
497c478bd9Sstevel@tonic-gate #include <sys/flock.h>
507c478bd9Sstevel@tonic-gate #include <sys/policy.h>
517c478bd9Sstevel@tonic-gate #include <sys/zone.h>
527c478bd9Sstevel@tonic-gate #include <sys/class.h>
537c478bd9Sstevel@tonic-gate #include <sys/socket.h>
547c478bd9Sstevel@tonic-gate #include <sys/netconfig.h>
5545916cd2Sjpk #include <sys/mntent.h>
5645916cd2Sjpk #include <sys/tsol/label.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #include <rpc/types.h>
597c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
607c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
637c478bd9Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
647c478bd9Sstevel@tonic-gate #include <nfs/rnode.h>
657c478bd9Sstevel@tonic-gate #include <nfs/mount.h>
667c478bd9Sstevel@tonic-gate #include <nfs/nfs_acl.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * From rpcsec module (common/rpcsec).
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate extern int sec_clnt_loadinfo(struct sec_data *, struct sec_data **, model_t);
747c478bd9Sstevel@tonic-gate extern void sec_clnt_freeinfo(struct sec_data *);
757c478bd9Sstevel@tonic-gate 
7639d3e169Sevanl static int pathconf_copyin(struct nfs_args *, struct pathcnf *);
7739d3e169Sevanl static int pathconf_get(struct mntinfo *, struct nfs_args *);
787c478bd9Sstevel@tonic-gate static void pathconf_rele(struct mntinfo *);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * The order and contents of this structure must be kept in sync with that of
827c478bd9Sstevel@tonic-gate  * rfsreqcnt_v2_tmpl in nfs_stats.c
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate static char *rfsnames_v2[] = {
857c478bd9Sstevel@tonic-gate 	"null", "getattr", "setattr", "unused", "lookup", "readlink", "read",
867c478bd9Sstevel@tonic-gate 	"unused", "write", "create", "remove", "rename", "link", "symlink",
877c478bd9Sstevel@tonic-gate 	"mkdir", "rmdir", "readdir", "fsstat"
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_v2[] = {
987c478bd9Sstevel@tonic-gate 	0, 0, 1, 0, 0, 0, 1,
997c478bd9Sstevel@tonic-gate 	0, 2, 2, 2, 2, 2, 2,
1007c478bd9Sstevel@tonic-gate 	2, 2, 1, 0
1017c478bd9Sstevel@tonic-gate };
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_v2[] = {
1087c478bd9Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0, 1,
1097c478bd9Sstevel@tonic-gate 	0, 2, 0, 0, 0, 0, 0,
1107c478bd9Sstevel@tonic-gate 	0, 0, 1, 0
1117c478bd9Sstevel@tonic-gate };
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_v2[] = {
1207c478bd9Sstevel@tonic-gate 	0, 0, 1, 0, 0, 0, 0,
1217c478bd9Sstevel@tonic-gate 	0, 1, 1, 1, 1, 1, 1,
1227c478bd9Sstevel@tonic-gate 	1, 1, 0, 0
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * nfs vfs operations.
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate static int	nfs_mount(vfs_t *, vnode_t *, struct mounta *, cred_t *);
1297c478bd9Sstevel@tonic-gate static int	nfs_unmount(vfs_t *, int, cred_t *);
1307c478bd9Sstevel@tonic-gate static int	nfs_root(vfs_t *, vnode_t **);
1317c478bd9Sstevel@tonic-gate static int	nfs_statvfs(vfs_t *, struct statvfs64 *);
1327c478bd9Sstevel@tonic-gate static int	nfs_sync(vfs_t *, short, cred_t *);
1337c478bd9Sstevel@tonic-gate static int	nfs_vget(vfs_t *, vnode_t **, fid_t *);
1347c478bd9Sstevel@tonic-gate static int	nfs_mountroot(vfs_t *, whymountroot_t);
1357c478bd9Sstevel@tonic-gate static void	nfs_freevfs(vfs_t *);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate static int	nfsrootvp(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 int nfsfstyp;
1457c478bd9Sstevel@tonic-gate vfsops_t *nfs_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 int rdma_debug = 0;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate int
1557c478bd9Sstevel@tonic-gate nfsinit(int fstyp, char *name)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t nfs_vfsops_template[] = {
158aa59c4cbSrsb 		VFSNAME_MOUNT,		{ .vfs_mount = nfs_mount },
159aa59c4cbSrsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = nfs_unmount },
160aa59c4cbSrsb 		VFSNAME_ROOT,		{ .vfs_root = nfs_root },
161aa59c4cbSrsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = nfs_statvfs },
162aa59c4cbSrsb 		VFSNAME_SYNC,		{ .vfs_sync = nfs_sync },
163aa59c4cbSrsb 		VFSNAME_VGET,		{ .vfs_vget = nfs_vget },
164aa59c4cbSrsb 		VFSNAME_MOUNTROOT,	{ .vfs_mountroot = nfs_mountroot },
165aa59c4cbSrsb 		VFSNAME_FREEVFS,	{ .vfs_freevfs = nfs_freevfs },
1667c478bd9Sstevel@tonic-gate 		NULL,			NULL
1677c478bd9Sstevel@tonic-gate 	};
1687c478bd9Sstevel@tonic-gate 	int error;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstyp, nfs_vfsops_template, &nfs_vfsops);
1717c478bd9Sstevel@tonic-gate 	if (error != 0) {
1727c478bd9Sstevel@tonic-gate 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
1737c478bd9Sstevel@tonic-gate 		    "nfsinit: bad vfs ops template");
1747c478bd9Sstevel@tonic-gate 		return (error);
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	error = vn_make_ops(name, nfs_vnodeops_template, &nfs_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 		    "nfsinit: bad vnode ops template");
1827c478bd9Sstevel@tonic-gate 		return (error);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	nfsfstyp = fstyp;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	return (0);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate void
1927c478bd9Sstevel@tonic-gate nfsfini(void)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
19639d3e169Sevanl static void
19739d3e169Sevanl nfs_free_args(struct nfs_args *nargs, nfs_fhandle *fh)
19839d3e169Sevanl {
19939d3e169Sevanl 
20039d3e169Sevanl 	if (fh)
20139d3e169Sevanl 		kmem_free(fh, sizeof (*fh));
20239d3e169Sevanl 
20339d3e169Sevanl 	if (nargs->pathconf) {
20439d3e169Sevanl 		kmem_free(nargs->pathconf, sizeof (struct pathcnf));
20539d3e169Sevanl 		nargs->pathconf = NULL;
20639d3e169Sevanl 	}
20739d3e169Sevanl 
20839d3e169Sevanl 	if (nargs->knconf) {
20939d3e169Sevanl 		if (nargs->knconf->knc_protofmly)
210dcedb507SMarcel Telka 			kmem_free(nargs->knconf->knc_protofmly, KNC_STRSIZE);
21139d3e169Sevanl 		if (nargs->knconf->knc_proto)
21239d3e169Sevanl 			kmem_free(nargs->knconf->knc_proto, KNC_STRSIZE);
21339d3e169Sevanl 		kmem_free(nargs->knconf, sizeof (*nargs->knconf));
21439d3e169Sevanl 		nargs->knconf = NULL;
21539d3e169Sevanl 	}
21639d3e169Sevanl 
21739d3e169Sevanl 	if (nargs->fh) {
21839d3e169Sevanl 		kmem_free(nargs->fh, strlen(nargs->fh) + 1);
21939d3e169Sevanl 		nargs->fh = NULL;
22039d3e169Sevanl 	}
22139d3e169Sevanl 
22239d3e169Sevanl 	if (nargs->hostname) {
22339d3e169Sevanl 		kmem_free(nargs->hostname, strlen(nargs->hostname) + 1);
22439d3e169Sevanl 		nargs->hostname = NULL;
22539d3e169Sevanl 	}
22639d3e169Sevanl 
22739d3e169Sevanl 	if (nargs->addr) {
22839d3e169Sevanl 		if (nargs->addr->buf) {
22939d3e169Sevanl 			ASSERT(nargs->addr->len);
23039d3e169Sevanl 			kmem_free(nargs->addr->buf, nargs->addr->len);
23139d3e169Sevanl 		}
23239d3e169Sevanl 		kmem_free(nargs->addr, sizeof (struct netbuf));
23339d3e169Sevanl 		nargs->addr = NULL;
23439d3e169Sevanl 	}
23539d3e169Sevanl 
23639d3e169Sevanl 	if (nargs->syncaddr) {
23739d3e169Sevanl 		ASSERT(nargs->syncaddr->len);
23839d3e169Sevanl 		if (nargs->syncaddr->buf) {
23939d3e169Sevanl 			ASSERT(nargs->syncaddr->len);
24039d3e169Sevanl 			kmem_free(nargs->syncaddr->buf, nargs->syncaddr->len);
24139d3e169Sevanl 		}
24239d3e169Sevanl 		kmem_free(nargs->syncaddr, sizeof (struct netbuf));
24339d3e169Sevanl 		nargs->syncaddr = NULL;
24439d3e169Sevanl 	}
24539d3e169Sevanl 
24639d3e169Sevanl 	if (nargs->netname) {
24739d3e169Sevanl 		kmem_free(nargs->netname, strlen(nargs->netname) + 1);
24839d3e169Sevanl 		nargs->netname = NULL;
24939d3e169Sevanl 	}
25039d3e169Sevanl 
25139d3e169Sevanl 	if (nargs->nfs_ext_u.nfs_extA.secdata) {
252dcedb507SMarcel Telka 		sec_clnt_freeinfo(nargs->nfs_ext_u.nfs_extA.secdata);
25339d3e169Sevanl 		nargs->nfs_ext_u.nfs_extA.secdata = NULL;
25439d3e169Sevanl 	}
25539d3e169Sevanl }
25639d3e169Sevanl 
25739d3e169Sevanl static int
25839d3e169Sevanl nfs_copyin(char *data, int datalen, struct nfs_args *nargs, nfs_fhandle *fh)
25939d3e169Sevanl {
26039d3e169Sevanl 
26139d3e169Sevanl 	int error;
26239d3e169Sevanl 	size_t nlen;			/* length of netname */
26339d3e169Sevanl 	size_t hlen;			/* length of hostname */
26439d3e169Sevanl 	char netname[MAXNETNAMELEN+1];	/* server's netname */
26539d3e169Sevanl 	struct netbuf addr;		/* server's address */
26639d3e169Sevanl 	struct netbuf syncaddr;		/* AUTH_DES time sync addr */
26739d3e169Sevanl 	struct knetconfig *knconf;	/* transport knetconfig structure */
26839d3e169Sevanl 	struct sec_data *secdata = NULL;	/* security data */
26939d3e169Sevanl 	STRUCT_DECL(nfs_args, args);		/* nfs mount arguments */
27039d3e169Sevanl 	STRUCT_DECL(knetconfig, knconf_tmp);
27139d3e169Sevanl 	STRUCT_DECL(netbuf, addr_tmp);
27239d3e169Sevanl 	int flags;
27339d3e169Sevanl 	struct pathcnf	*pc;		/* Pathconf */
27439d3e169Sevanl 	char *p, *pf;
27539d3e169Sevanl 	char *userbufptr;
27639d3e169Sevanl 
27739d3e169Sevanl 
27839d3e169Sevanl 	bzero(nargs, sizeof (*nargs));
27939d3e169Sevanl 
28039d3e169Sevanl 	STRUCT_INIT(args, get_udatamodel());
28139d3e169Sevanl 	bzero(STRUCT_BUF(args), SIZEOF_STRUCT(nfs_args, DATAMODEL_NATIVE));
282dcedb507SMarcel Telka 	if (copyin(data, STRUCT_BUF(args), MIN(datalen, STRUCT_SIZE(args))))
28339d3e169Sevanl 		return (EFAULT);
28439d3e169Sevanl 
28539d3e169Sevanl 	nargs->wsize = STRUCT_FGET(args, wsize);
28639d3e169Sevanl 	nargs->rsize = STRUCT_FGET(args, rsize);
28739d3e169Sevanl 	nargs->timeo = STRUCT_FGET(args, timeo);
28839d3e169Sevanl 	nargs->retrans = STRUCT_FGET(args, retrans);
28939d3e169Sevanl 	nargs->acregmin = STRUCT_FGET(args, acregmin);
29039d3e169Sevanl 	nargs->acregmax = STRUCT_FGET(args, acregmax);
29139d3e169Sevanl 	nargs->acdirmin = STRUCT_FGET(args, acdirmin);
29239d3e169Sevanl 	nargs->acdirmax = STRUCT_FGET(args, acdirmax);
29339d3e169Sevanl 
29439d3e169Sevanl 	flags = STRUCT_FGET(args, flags);
29539d3e169Sevanl 	nargs->flags = flags;
29639d3e169Sevanl 
29739d3e169Sevanl 
29839d3e169Sevanl 	addr.buf = NULL;
29939d3e169Sevanl 	syncaddr.buf = NULL;
30039d3e169Sevanl 
30139d3e169Sevanl 	/*
30239d3e169Sevanl 	 * Allocate space for a knetconfig structure and
30339d3e169Sevanl 	 * its strings and copy in from user-land.
30439d3e169Sevanl 	 */
30539d3e169Sevanl 	knconf = kmem_zalloc(sizeof (*knconf), KM_SLEEP);
30639d3e169Sevanl 	STRUCT_INIT(knconf_tmp, get_udatamodel());
30739d3e169Sevanl 	if (copyin(STRUCT_FGETP(args, knconf), STRUCT_BUF(knconf_tmp),
30839d3e169Sevanl 	    STRUCT_SIZE(knconf_tmp))) {
30939d3e169Sevanl 		kmem_free(knconf, sizeof (*knconf));
31039d3e169Sevanl 		return (EFAULT);
31139d3e169Sevanl 	}
31239d3e169Sevanl 
31339d3e169Sevanl 	knconf->knc_semantics = STRUCT_FGET(knconf_tmp, knc_semantics);
31439d3e169Sevanl 	knconf->knc_protofmly = STRUCT_FGETP(knconf_tmp, knc_protofmly);
31539d3e169Sevanl 	knconf->knc_proto = STRUCT_FGETP(knconf_tmp, knc_proto);
31639d3e169Sevanl 	if (get_udatamodel() != DATAMODEL_LP64) {
31739d3e169Sevanl 		knconf->knc_rdev = expldev(STRUCT_FGET(knconf_tmp, knc_rdev));
31839d3e169Sevanl 	} else {
31939d3e169Sevanl 		knconf->knc_rdev = STRUCT_FGET(knconf_tmp, knc_rdev);
32039d3e169Sevanl 	}
32139d3e169Sevanl 
32239d3e169Sevanl 	pf = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
32339d3e169Sevanl 	p = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
32439d3e169Sevanl 	error = copyinstr(knconf->knc_protofmly, pf, KNC_STRSIZE, NULL);
32539d3e169Sevanl 	if (error) {
32639d3e169Sevanl 		kmem_free(pf, KNC_STRSIZE);
32739d3e169Sevanl 		kmem_free(p, KNC_STRSIZE);
32839d3e169Sevanl 		kmem_free(knconf, sizeof (*knconf));
32939d3e169Sevanl 		return (error);
33039d3e169Sevanl 	}
33139d3e169Sevanl 
33239d3e169Sevanl 	error = copyinstr(knconf->knc_proto, p, KNC_STRSIZE, NULL);
33339d3e169Sevanl 	if (error) {
33439d3e169Sevanl 		kmem_free(pf, KNC_STRSIZE);
33539d3e169Sevanl 		kmem_free(p, KNC_STRSIZE);
33639d3e169Sevanl 		kmem_free(knconf, sizeof (*knconf));
33739d3e169Sevanl 		return (error);
33839d3e169Sevanl 	}
33939d3e169Sevanl 
34039d3e169Sevanl 
34139d3e169Sevanl 	knconf->knc_protofmly = pf;
34239d3e169Sevanl 	knconf->knc_proto = p;
34339d3e169Sevanl 
34439d3e169Sevanl 	nargs->knconf = knconf;
34539d3e169Sevanl 
34639d3e169Sevanl 	/* Copyin pathconf if there is one */
34739d3e169Sevanl 	if (STRUCT_FGETP(args, pathconf) != NULL) {
34839d3e169Sevanl 		pc = kmem_alloc(sizeof (*pc), KM_SLEEP);
34939d3e169Sevanl 		error = pathconf_copyin(STRUCT_BUF(args), pc);
35039d3e169Sevanl 		nargs->pathconf = pc;
35139d3e169Sevanl 		if (error)
35239d3e169Sevanl 			goto errout;
35339d3e169Sevanl 	}
35439d3e169Sevanl 
35539d3e169Sevanl 	/*
35639d3e169Sevanl 	 * Get server address
35739d3e169Sevanl 	 */
35839d3e169Sevanl 	STRUCT_INIT(addr_tmp, get_udatamodel());
35939d3e169Sevanl 	if (copyin(STRUCT_FGETP(args, addr), STRUCT_BUF(addr_tmp),
36039d3e169Sevanl 	    STRUCT_SIZE(addr_tmp))) {
36139d3e169Sevanl 		error = EFAULT;
36239d3e169Sevanl 		goto errout;
36339d3e169Sevanl 	}
36439d3e169Sevanl 	nargs->addr = kmem_alloc(sizeof (struct netbuf), KM_SLEEP);
36539d3e169Sevanl 	userbufptr = STRUCT_FGETP(addr_tmp, buf);
36639d3e169Sevanl 	addr.len = STRUCT_FGET(addr_tmp, len);
36739d3e169Sevanl 	addr.buf = kmem_alloc(addr.len, KM_SLEEP);
36839d3e169Sevanl 	addr.maxlen = addr.len;
36939d3e169Sevanl 	if (copyin(userbufptr, addr.buf, addr.len)) {
37039d3e169Sevanl 		kmem_free(addr.buf, addr.len);
37139d3e169Sevanl 		error = EFAULT;
37239d3e169Sevanl 		goto errout;
37339d3e169Sevanl 	}
37439d3e169Sevanl 	bcopy(&addr, nargs->addr, sizeof (struct netbuf));
37539d3e169Sevanl 
37639d3e169Sevanl 	/*
37739d3e169Sevanl 	 * Get the root fhandle
37839d3e169Sevanl 	 */
37939d3e169Sevanl 
38039d3e169Sevanl 	if (copyin(STRUCT_FGETP(args, fh), &fh->fh_buf, NFS_FHSIZE)) {
38139d3e169Sevanl 		error = EFAULT;
38239d3e169Sevanl 		goto errout;
38339d3e169Sevanl 	}
38439d3e169Sevanl 	fh->fh_len = NFS_FHSIZE;
38539d3e169Sevanl 
38639d3e169Sevanl 	/*
38739d3e169Sevanl 	 * Get server's hostname
38839d3e169Sevanl 	 */
38939d3e169Sevanl 	if (flags & NFSMNT_HOSTNAME) {
390dcedb507SMarcel Telka 		error = copyinstr(STRUCT_FGETP(args, hostname), netname,
391dcedb507SMarcel Telka 		    sizeof (netname), &hlen);
39239d3e169Sevanl 		if (error)
39339d3e169Sevanl 			goto errout;
39439d3e169Sevanl 		nargs->hostname = kmem_zalloc(hlen, KM_SLEEP);
39539d3e169Sevanl 		(void) strcpy(nargs->hostname, netname);
39639d3e169Sevanl 
39739d3e169Sevanl 	} else {
39839d3e169Sevanl 		nargs->hostname = NULL;
39939d3e169Sevanl 	}
40039d3e169Sevanl 
40139d3e169Sevanl 
40239d3e169Sevanl 	/*
40339d3e169Sevanl 	 * If there are syncaddr and netname data, load them in. This is
40439d3e169Sevanl 	 * to support data needed for NFSV4 when AUTH_DH is the negotiated
40539d3e169Sevanl 	 * flavor via SECINFO. (instead of using MOUNT protocol in V3).
40639d3e169Sevanl 	 */
40739d3e169Sevanl 	netname[0] = '\0';
40839d3e169Sevanl 	if (flags & NFSMNT_SECURE) {
40939d3e169Sevanl 		if (STRUCT_FGETP(args, syncaddr) == NULL) {
41039d3e169Sevanl 			error = EINVAL;
41139d3e169Sevanl 			goto errout;
41239d3e169Sevanl 		}
41339d3e169Sevanl 		/* get syncaddr */
41439d3e169Sevanl 		STRUCT_INIT(addr_tmp, get_udatamodel());
41539d3e169Sevanl 		if (copyin(STRUCT_FGETP(args, syncaddr), STRUCT_BUF(addr_tmp),
41639d3e169Sevanl 		    STRUCT_SIZE(addr_tmp))) {
41739d3e169Sevanl 			error = EINVAL;
41839d3e169Sevanl 			goto errout;
41939d3e169Sevanl 		}
42039d3e169Sevanl 		userbufptr = STRUCT_FGETP(addr_tmp, buf);
42139d3e169Sevanl 		syncaddr.len = STRUCT_FGET(addr_tmp, len);
42239d3e169Sevanl 		syncaddr.buf = kmem_alloc(syncaddr.len, KM_SLEEP);
42339d3e169Sevanl 		syncaddr.maxlen = syncaddr.len;
42439d3e169Sevanl 		if (copyin(userbufptr, syncaddr.buf, syncaddr.len)) {
42539d3e169Sevanl 			kmem_free(syncaddr.buf, syncaddr.len);
42639d3e169Sevanl 			error = EFAULT;
42739d3e169Sevanl 			goto errout;
42839d3e169Sevanl 		}
42939d3e169Sevanl 
43039d3e169Sevanl 		nargs->syncaddr = kmem_alloc(sizeof (struct netbuf), KM_SLEEP);
43139d3e169Sevanl 		bcopy(&syncaddr, nargs->syncaddr, sizeof (struct netbuf));
43239d3e169Sevanl 
43339d3e169Sevanl 		ASSERT(STRUCT_FGETP(args, netname));
43439d3e169Sevanl 		if (copyinstr(STRUCT_FGETP(args, netname), netname,
43539d3e169Sevanl 		    sizeof (netname), &nlen)) {
43639d3e169Sevanl 			error = EFAULT;
43739d3e169Sevanl 			goto errout;
43839d3e169Sevanl 		}
43939d3e169Sevanl 
44039d3e169Sevanl 		netname[nlen] = '\0';
44139d3e169Sevanl 		nargs->netname = kmem_zalloc(nlen, KM_SLEEP);
44239d3e169Sevanl 		(void) strcpy(nargs->netname, netname);
44339d3e169Sevanl 	}
44439d3e169Sevanl 
44539d3e169Sevanl 	/*
44639d3e169Sevanl 	 * Get the extention data which has the security data structure.
44739d3e169Sevanl 	 * This includes data for AUTH_SYS as well.
44839d3e169Sevanl 	 */
44939d3e169Sevanl 	if (flags & NFSMNT_NEWARGS) {
45039d3e169Sevanl 		nargs->nfs_args_ext = STRUCT_FGET(args, nfs_args_ext);
45139d3e169Sevanl 		if (nargs->nfs_args_ext == NFS_ARGS_EXTA ||
45239d3e169Sevanl 		    nargs->nfs_args_ext == NFS_ARGS_EXTB) {
45339d3e169Sevanl 			/*
45439d3e169Sevanl 			 * Indicating the application is using the new
45539d3e169Sevanl 			 * sec_data structure to pass in the security
45639d3e169Sevanl 			 * data.
45739d3e169Sevanl 			 */
45839d3e169Sevanl 			if (STRUCT_FGETP(args,
45939d3e169Sevanl 			    nfs_ext_u.nfs_extA.secdata) != NULL) {
46039d3e169Sevanl 				error = sec_clnt_loadinfo(
46139d3e169Sevanl 				    (struct sec_data *)STRUCT_FGETP(args,
462dcedb507SMarcel Telka 				    nfs_ext_u.nfs_extA.secdata), &secdata,
463dcedb507SMarcel Telka 				    get_udatamodel());
46439d3e169Sevanl 			}
46539d3e169Sevanl 			nargs->nfs_ext_u.nfs_extA.secdata = secdata;
46639d3e169Sevanl 		}
46739d3e169Sevanl 	}
46839d3e169Sevanl 
46939d3e169Sevanl 	if (error)
47039d3e169Sevanl 		goto errout;
47139d3e169Sevanl 
47239d3e169Sevanl 	/*
47339d3e169Sevanl 	 * Failover support:
47439d3e169Sevanl 	 *
47539d3e169Sevanl 	 * We may have a linked list of nfs_args structures,
47639d3e169Sevanl 	 * which means the user is looking for failover.  If
47739d3e169Sevanl 	 * the mount is either not "read-only" or "soft",
47839d3e169Sevanl 	 * we want to bail out with EINVAL.
47939d3e169Sevanl 	 */
48039d3e169Sevanl 	if (nargs->nfs_args_ext == NFS_ARGS_EXTB)
48139d3e169Sevanl 		nargs->nfs_ext_u.nfs_extB.next =
48239d3e169Sevanl 		    STRUCT_FGETP(args, nfs_ext_u.nfs_extB.next);
48339d3e169Sevanl 
48439d3e169Sevanl errout:
48539d3e169Sevanl 	if (error)
48639d3e169Sevanl 		nfs_free_args(nargs, fh);
48739d3e169Sevanl 
48839d3e169Sevanl 	return (error);
48939d3e169Sevanl }
49039d3e169Sevanl 
49139d3e169Sevanl 
4927c478bd9Sstevel@tonic-gate /*
4937c478bd9Sstevel@tonic-gate  * nfs mount vfsop
4947c478bd9Sstevel@tonic-gate  * Set up mount info record and attach it to vfs struct.
4957c478bd9Sstevel@tonic-gate  */
4967c478bd9Sstevel@tonic-gate static int
4977c478bd9Sstevel@tonic-gate nfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	char *data = uap->dataptr;
5007c478bd9Sstevel@tonic-gate 	int error;
5017c478bd9Sstevel@tonic-gate 	vnode_t *rtvp;			/* the server's root */
5027c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;			/* mount info, pointed at by vfs */
5037c478bd9Sstevel@tonic-gate 	size_t nlen;			/* length of netname */
5047c478bd9Sstevel@tonic-gate 	struct knetconfig *knconf;	/* transport knetconfig structure */
5057c478bd9Sstevel@tonic-gate 	struct knetconfig *rdma_knconf;	/* rdma transport structure */
5067c478bd9Sstevel@tonic-gate 	rnode_t *rp;
5077c478bd9Sstevel@tonic-gate 	struct servinfo *svp;		/* nfs server info */
5087c478bd9Sstevel@tonic-gate 	struct servinfo *svp_tail = NULL; /* previous nfs server info */
5097c478bd9Sstevel@tonic-gate 	struct servinfo *svp_head;	/* first nfs server info */
5107c478bd9Sstevel@tonic-gate 	struct servinfo *svp_2ndlast;	/* 2nd last in the server info list */
5117c478bd9Sstevel@tonic-gate 	struct sec_data *secdata;	/* security data */
51239d3e169Sevanl 	struct nfs_args	*args = NULL;
5137c478bd9Sstevel@tonic-gate 	int flags, addr_type;
514108322fbScarlsonj 	zone_t *zone = nfs_zone();
51545916cd2Sjpk 	zone_t *mntzone = NULL;
51639d3e169Sevanl 	nfs_fhandle	*fhandle = NULL;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
5197c478bd9Sstevel@tonic-gate 		return (error);
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (mvp->v_type != VDIR)
5227c478bd9Sstevel@tonic-gate 		return (ENOTDIR);
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/*
5257c478bd9Sstevel@tonic-gate 	 * get arguments
5267c478bd9Sstevel@tonic-gate 	 *
5277c478bd9Sstevel@tonic-gate 	 * nfs_args is now versioned and is extensible, so
5287c478bd9Sstevel@tonic-gate 	 * uap->datalen might be different from sizeof (args)
5297c478bd9Sstevel@tonic-gate 	 * in a compatible situation.
5307c478bd9Sstevel@tonic-gate 	 */
5317c478bd9Sstevel@tonic-gate more:
5327c478bd9Sstevel@tonic-gate 
53339d3e169Sevanl 	if (!(uap->flags & MS_SYSSPACE)) {
53439d3e169Sevanl 		if (args == NULL)
53539d3e169Sevanl 			args = kmem_alloc(sizeof (struct nfs_args), KM_SLEEP);
53639d3e169Sevanl 		else {
53739d3e169Sevanl 			nfs_free_args(args, fhandle);
53839d3e169Sevanl 			fhandle = NULL;
53939d3e169Sevanl 		}
54039d3e169Sevanl 		if (fhandle == NULL)
54139d3e169Sevanl 			fhandle = kmem_zalloc(sizeof (nfs_fhandle), KM_SLEEP);
54239d3e169Sevanl 		error = nfs_copyin(data, uap->datalen, args, fhandle);
54339d3e169Sevanl 		if (error)  {
54439d3e169Sevanl 			if (args)
54539d3e169Sevanl 				kmem_free(args, sizeof (*args));
54639d3e169Sevanl 			return (error);
54739d3e169Sevanl 		}
54839d3e169Sevanl 	} else {
54939d3e169Sevanl 		args = (struct nfs_args *)data;
55039d3e169Sevanl 		fhandle = (nfs_fhandle *)args->fh;
55139d3e169Sevanl 	}
55239d3e169Sevanl 
55339d3e169Sevanl 
55439d3e169Sevanl 	flags = args->flags;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_REMOUNT) {
5577c478bd9Sstevel@tonic-gate 		size_t n;
5587c478bd9Sstevel@tonic-gate 		char name[FSTYPSZ];
5597c478bd9Sstevel@tonic-gate 
56018b3347eSevanl 		if (uap->flags & MS_SYSSPACE)
5617c478bd9Sstevel@tonic-gate 			error = copystr(uap->fstype, name, FSTYPSZ, &n);
56218b3347eSevanl 		else
5637c478bd9Sstevel@tonic-gate 			error = copyinstr(uap->fstype, name, FSTYPSZ, &n);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		if (error) {
5667c478bd9Sstevel@tonic-gate 			if (error == ENAMETOOLONG)
5677c478bd9Sstevel@tonic-gate 				return (EINVAL);
5687c478bd9Sstevel@tonic-gate 			return (error);
5697c478bd9Sstevel@tonic-gate 		}
5707c478bd9Sstevel@tonic-gate 
57139d3e169Sevanl 
5727c478bd9Sstevel@tonic-gate 		/*
5737c478bd9Sstevel@tonic-gate 		 * This check is to ensure that the request is a
5747c478bd9Sstevel@tonic-gate 		 * genuine nfs remount request.
5757c478bd9Sstevel@tonic-gate 		 */
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 		if (strncmp(name, "nfs", 3) != 0)
5787c478bd9Sstevel@tonic-gate 			return (EINVAL);
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		/*
5817c478bd9Sstevel@tonic-gate 		 * If the request changes the locking type, disallow the
5827c478bd9Sstevel@tonic-gate 		 * remount,
5837c478bd9Sstevel@tonic-gate 		 * because it's questionable whether we can transfer the
5847c478bd9Sstevel@tonic-gate 		 * locking state correctly.
5857c478bd9Sstevel@tonic-gate 		 *
5867c478bd9Sstevel@tonic-gate 		 * Remounts need to save the pathconf information.
5877c478bd9Sstevel@tonic-gate 		 * Part of the infamous static kludge.
5887c478bd9Sstevel@tonic-gate 		 */
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 		if ((mi = VFTOMI(vfsp)) != NULL) {
5917c478bd9Sstevel@tonic-gate 			uint_t new_mi_llock;
5927c478bd9Sstevel@tonic-gate 			uint_t old_mi_llock;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 			new_mi_llock = (flags & NFSMNT_LLOCK) ? 1 : 0;
5957c478bd9Sstevel@tonic-gate 			old_mi_llock = (mi->mi_flags & MI_LLOCK) ? 1 : 0;
5967c478bd9Sstevel@tonic-gate 			if (old_mi_llock != new_mi_llock)
5977c478bd9Sstevel@tonic-gate 				return (EBUSY);
5987c478bd9Sstevel@tonic-gate 		}
59939d3e169Sevanl 		error = pathconf_get((struct mntinfo *)vfsp->vfs_data, args);
60039d3e169Sevanl 
60118b3347eSevanl 		if (!(uap->flags & MS_SYSSPACE)) {
60218b3347eSevanl 			nfs_free_args(args, fhandle);
60318b3347eSevanl 			kmem_free(args, sizeof (*args));
60418b3347eSevanl 		}
60518b3347eSevanl 
60639d3e169Sevanl 		return (error);
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
6107c478bd9Sstevel@tonic-gate 	if (!(uap->flags & MS_OVERLAY) &&
6117c478bd9Sstevel@tonic-gate 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
6127c478bd9Sstevel@tonic-gate 		mutex_exit(&mvp->v_lock);
61318b3347eSevanl 		if (!(uap->flags & MS_SYSSPACE)) {
61439d3e169Sevanl 			nfs_free_args(args, fhandle);
61539d3e169Sevanl 			kmem_free(args, sizeof (*args));
61639d3e169Sevanl 		}
6177c478bd9Sstevel@tonic-gate 		return (EBUSY);
6187c478bd9Sstevel@tonic-gate 	}
6197c478bd9Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/* make sure things are zeroed for errout: */
6227c478bd9Sstevel@tonic-gate 	rtvp = NULL;
6237c478bd9Sstevel@tonic-gate 	mi = NULL;
6247c478bd9Sstevel@tonic-gate 	secdata = NULL;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	/*
6277c478bd9Sstevel@tonic-gate 	 * A valid knetconfig structure is required.
6287c478bd9Sstevel@tonic-gate 	 */
62939d3e169Sevanl 	if (!(flags & NFSMNT_KNCONF)) {
63039d3e169Sevanl 		if (!(uap->flags & MS_SYSSPACE)) {
63139d3e169Sevanl 			nfs_free_args(args, fhandle);
63239d3e169Sevanl 			kmem_free(args, sizeof (*args));
63339d3e169Sevanl 		}
6347c478bd9Sstevel@tonic-gate 		return (EINVAL);
63539d3e169Sevanl 	}
63639d3e169Sevanl 
63739d3e169Sevanl 	if ((strlen(args->knconf->knc_protofmly) >= KNC_STRSIZE) ||
63839d3e169Sevanl 	    (strlen(args->knconf->knc_proto) >= KNC_STRSIZE)) {
63939d3e169Sevanl 		if (!(uap->flags & MS_SYSSPACE)) {
64039d3e169Sevanl 			nfs_free_args(args, fhandle);
64139d3e169Sevanl 			kmem_free(args, sizeof (*args));
64239d3e169Sevanl 		}
64339d3e169Sevanl 		return (EINVAL);
64439d3e169Sevanl 	}
64539d3e169Sevanl 
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	/*
6487c478bd9Sstevel@tonic-gate 	 * Allocate a servinfo struct.
6497c478bd9Sstevel@tonic-gate 	 */
6507c478bd9Sstevel@tonic-gate 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
6517c478bd9Sstevel@tonic-gate 	mutex_init(&svp->sv_lock, NULL, MUTEX_DEFAULT, NULL);
6527c478bd9Sstevel@tonic-gate 	if (svp_tail) {
6537c478bd9Sstevel@tonic-gate 		svp_2ndlast = svp_tail;
6547c478bd9Sstevel@tonic-gate 		svp_tail->sv_next = svp;
6557c478bd9Sstevel@tonic-gate 	} else {
6567c478bd9Sstevel@tonic-gate 		svp_head = svp;
6577c478bd9Sstevel@tonic-gate 		svp_2ndlast = svp;
6587c478bd9Sstevel@tonic-gate 	}
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	svp_tail = svp;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	/*
66339d3e169Sevanl 	 * Get knetconfig and server address
6647c478bd9Sstevel@tonic-gate 	 */
66539d3e169Sevanl 	svp->sv_knconf = args->knconf;
66639d3e169Sevanl 	args->knconf = NULL;
6677c478bd9Sstevel@tonic-gate 
66839d3e169Sevanl 	if (args->addr == NULL || args->addr->buf == NULL) {
66939d3e169Sevanl 		error = EINVAL;
6707c478bd9Sstevel@tonic-gate 		goto errout;
67139d3e169Sevanl 	}
67239d3e169Sevanl 
67339d3e169Sevanl 	svp->sv_addr.maxlen = args->addr->maxlen;
67439d3e169Sevanl 	svp->sv_addr.len = args->addr->len;
67539d3e169Sevanl 	svp->sv_addr.buf = args->addr->buf;
67639d3e169Sevanl 	args->addr->buf = NULL;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 	/*
6797c478bd9Sstevel@tonic-gate 	 * Get the root fhandle
6807c478bd9Sstevel@tonic-gate 	 */
68139d3e169Sevanl 	ASSERT(fhandle);
68239d3e169Sevanl 
68339d3e169Sevanl 	bcopy(&fhandle->fh_buf, &svp->sv_fhandle.fh_buf, fhandle->fh_len);
68439d3e169Sevanl 	svp->sv_fhandle.fh_len = fhandle->fh_len;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/*
6877c478bd9Sstevel@tonic-gate 	 * Get server's hostname
6887c478bd9Sstevel@tonic-gate 	 */
6897c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_HOSTNAME) {
69039d3e169Sevanl 		if (args->hostname == NULL) {
69139d3e169Sevanl 			error = EINVAL;
6927c478bd9Sstevel@tonic-gate 			goto errout;
69339d3e169Sevanl 		}
69439d3e169Sevanl 		svp->sv_hostnamelen = strlen(args->hostname) + 1;
69539d3e169Sevanl 		svp->sv_hostname = args->hostname;
69639d3e169Sevanl 		args->hostname = NULL;
6977c478bd9Sstevel@tonic-gate 	} else {
6987c478bd9Sstevel@tonic-gate 		char *p = "unknown-host";
69939d3e169Sevanl 		svp->sv_hostnamelen = strlen(p) + 1;
70039d3e169Sevanl 		svp->sv_hostname = kmem_zalloc(svp->sv_hostnamelen, KM_SLEEP);
70139d3e169Sevanl 		(void) strcpy(svp->sv_hostname, p);
7027c478bd9Sstevel@tonic-gate 	}
70339d3e169Sevanl 
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	/*
7067c478bd9Sstevel@tonic-gate 	 * RDMA MOUNT SUPPORT FOR NFS v2:
7077c478bd9Sstevel@tonic-gate 	 * Establish, is it possible to use RDMA, if so overload the
7087c478bd9Sstevel@tonic-gate 	 * knconf with rdma specific knconf and free the orignal.
7097c478bd9Sstevel@tonic-gate 	 */
7107c478bd9Sstevel@tonic-gate 	if ((flags & NFSMNT_TRYRDMA) || (flags & NFSMNT_DORDMA)) {
7117c478bd9Sstevel@tonic-gate 		/*
7127c478bd9Sstevel@tonic-gate 		 * Determine the addr type for RDMA, IPv4 or v6.
7137c478bd9Sstevel@tonic-gate 		 */
7147c478bd9Sstevel@tonic-gate 		if (strcmp(svp->sv_knconf->knc_protofmly, NC_INET) == 0)
7157c478bd9Sstevel@tonic-gate 			addr_type = AF_INET;
7167c478bd9Sstevel@tonic-gate 		else if (strcmp(svp->sv_knconf->knc_protofmly, NC_INET6) == 0)
7177c478bd9Sstevel@tonic-gate 			addr_type = AF_INET6;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 		if (rdma_reachable(addr_type, &svp->sv_addr,
7207c478bd9Sstevel@tonic-gate 		    &rdma_knconf) == 0) {
7217c478bd9Sstevel@tonic-gate 			/*
7227c478bd9Sstevel@tonic-gate 			 * If successful, hijack, the orignal knconf and
7237c478bd9Sstevel@tonic-gate 			 * replace with a new one, depending on the flags.
7247c478bd9Sstevel@tonic-gate 			 */
7257c478bd9Sstevel@tonic-gate 			svp->sv_origknconf = svp->sv_knconf;
7267c478bd9Sstevel@tonic-gate 			svp->sv_knconf = rdma_knconf;
7277c478bd9Sstevel@tonic-gate 			knconf = rdma_knconf;
7287c478bd9Sstevel@tonic-gate 		} else {
7297c478bd9Sstevel@tonic-gate 			if (flags & NFSMNT_TRYRDMA) {
7307c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7317c478bd9Sstevel@tonic-gate 				if (rdma_debug)
7327c478bd9Sstevel@tonic-gate 					zcmn_err(getzoneid(), CE_WARN,
7337c478bd9Sstevel@tonic-gate 					    "no RDMA onboard, revert\n");
7347c478bd9Sstevel@tonic-gate #endif
7357c478bd9Sstevel@tonic-gate 			}
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 			if (flags & NFSMNT_DORDMA) {
7387c478bd9Sstevel@tonic-gate 				/*
7397c478bd9Sstevel@tonic-gate 				 * If proto=rdma is specified and no RDMA
7407c478bd9Sstevel@tonic-gate 				 * path to this server is avialable then
7417c478bd9Sstevel@tonic-gate 				 * ditch this server.
7427c478bd9Sstevel@tonic-gate 				 * This is not included in the mountable
7437c478bd9Sstevel@tonic-gate 				 * server list or the replica list.
7447c478bd9Sstevel@tonic-gate 				 * Check if more servers are specified;
7457c478bd9Sstevel@tonic-gate 				 * Failover case, otherwise bail out of mount.
7467c478bd9Sstevel@tonic-gate 				 */
747dcedb507SMarcel Telka 				if (args->nfs_args_ext == NFS_ARGS_EXTB &&
748dcedb507SMarcel Telka 				    args->nfs_ext_u.nfs_extB.next != NULL) {
74939d3e169Sevanl 					data = (char *)
75039d3e169Sevanl 					    args->nfs_ext_u.nfs_extB.next;
7517c478bd9Sstevel@tonic-gate 					if (uap->flags & MS_RDONLY &&
7527c478bd9Sstevel@tonic-gate 					    !(flags & NFSMNT_SOFT)) {
7537c478bd9Sstevel@tonic-gate 						if (svp_head->sv_next == NULL) {
7547c478bd9Sstevel@tonic-gate 							svp_tail = NULL;
7557c478bd9Sstevel@tonic-gate 							svp_2ndlast = NULL;
7567c478bd9Sstevel@tonic-gate 							sv_free(svp_head);
7577c478bd9Sstevel@tonic-gate 							goto more;
7587c478bd9Sstevel@tonic-gate 						} else {
7597c478bd9Sstevel@tonic-gate 							svp_tail = svp_2ndlast;
7607c478bd9Sstevel@tonic-gate 							svp_2ndlast->sv_next =
7617c478bd9Sstevel@tonic-gate 							    NULL;
7627c478bd9Sstevel@tonic-gate 							sv_free(svp);
7637c478bd9Sstevel@tonic-gate 							goto more;
7647c478bd9Sstevel@tonic-gate 						}
7657c478bd9Sstevel@tonic-gate 					}
7667c478bd9Sstevel@tonic-gate 				} else {
7677c478bd9Sstevel@tonic-gate 					/*
7687c478bd9Sstevel@tonic-gate 					 * This is the last server specified
7697c478bd9Sstevel@tonic-gate 					 * in the nfs_args list passed down
7707c478bd9Sstevel@tonic-gate 					 * and its not rdma capable.
7717c478bd9Sstevel@tonic-gate 					 */
7727c478bd9Sstevel@tonic-gate 					if (svp_head->sv_next == NULL) {
7737c478bd9Sstevel@tonic-gate 						/*
7747c478bd9Sstevel@tonic-gate 						 * Is this the only one
7757c478bd9Sstevel@tonic-gate 						 */
7767c478bd9Sstevel@tonic-gate 						error = EINVAL;
7777c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7787c478bd9Sstevel@tonic-gate 						if (rdma_debug)
7797c478bd9Sstevel@tonic-gate 							zcmn_err(getzoneid(),
7807c478bd9Sstevel@tonic-gate 							    CE_WARN,
7817c478bd9Sstevel@tonic-gate 							    "No RDMA srv");
7827c478bd9Sstevel@tonic-gate #endif
7837c478bd9Sstevel@tonic-gate 						goto errout;
7847c478bd9Sstevel@tonic-gate 					} else {
7857c478bd9Sstevel@tonic-gate 						/*
7867c478bd9Sstevel@tonic-gate 						 * There is list, since some
7877c478bd9Sstevel@tonic-gate 						 * servers specified before
7887c478bd9Sstevel@tonic-gate 						 * this passed all requirements
7897c478bd9Sstevel@tonic-gate 						 */
7907c478bd9Sstevel@tonic-gate 						svp_tail = svp_2ndlast;
7917c478bd9Sstevel@tonic-gate 						svp_2ndlast->sv_next = NULL;
7927c478bd9Sstevel@tonic-gate 						sv_free(svp);
7937c478bd9Sstevel@tonic-gate 						goto proceed;
7947c478bd9Sstevel@tonic-gate 					}
7957c478bd9Sstevel@tonic-gate 				}
7967c478bd9Sstevel@tonic-gate 			}
7977c478bd9Sstevel@tonic-gate 		}
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	/*
8017c478bd9Sstevel@tonic-gate 	 * Get the extention data which has the new security data structure.
8027c478bd9Sstevel@tonic-gate 	 */
8037c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_NEWARGS) {
80439d3e169Sevanl 		switch (args->nfs_args_ext) {
8057c478bd9Sstevel@tonic-gate 		case NFS_ARGS_EXTA:
8067c478bd9Sstevel@tonic-gate 		case NFS_ARGS_EXTB:
8077c478bd9Sstevel@tonic-gate 			/*
8087c478bd9Sstevel@tonic-gate 			 * Indicating the application is using the new
8097c478bd9Sstevel@tonic-gate 			 * sec_data structure to pass in the security
8107c478bd9Sstevel@tonic-gate 			 * data.
8117c478bd9Sstevel@tonic-gate 			 */
81239d3e169Sevanl 			secdata = args->nfs_ext_u.nfs_extA.secdata;
81339d3e169Sevanl 			if (secdata == NULL) {
8147c478bd9Sstevel@tonic-gate 				error = EINVAL;
8157c478bd9Sstevel@tonic-gate 			} else {
81639d3e169Sevanl 				/*
81739d3e169Sevanl 				 * Need to validate the flavor here if
81839d3e169Sevanl 				 * sysspace, userspace was already
81939d3e169Sevanl 				 * validate from the nfs_copyin function.
82039d3e169Sevanl 				 */
82139d3e169Sevanl 				switch (secdata->rpcflavor) {
82239d3e169Sevanl 					case AUTH_NONE:
82339d3e169Sevanl 					case AUTH_UNIX:
82439d3e169Sevanl 					case AUTH_LOOPBACK:
82539d3e169Sevanl 					case AUTH_DES:
82639d3e169Sevanl 					case RPCSEC_GSS:
82739d3e169Sevanl 						break;
82839d3e169Sevanl 					default:
82939d3e169Sevanl 						error = EINVAL;
83039d3e169Sevanl 						goto errout;
8317c478bd9Sstevel@tonic-gate 				}
83239d3e169Sevanl 			}
83339d3e169Sevanl 			args->nfs_ext_u.nfs_extA.secdata = NULL;
8347c478bd9Sstevel@tonic-gate 			break;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 		default:
8377c478bd9Sstevel@tonic-gate 			error = EINVAL;
8387c478bd9Sstevel@tonic-gate 			break;
8397c478bd9Sstevel@tonic-gate 		}
8407c478bd9Sstevel@tonic-gate 	} else if (flags & NFSMNT_SECURE) {
8417c478bd9Sstevel@tonic-gate 		/*
8427c478bd9Sstevel@tonic-gate 		 * Keep this for backward compatibility to support
8437c478bd9Sstevel@tonic-gate 		 * NFSMNT_SECURE/NFSMNT_RPCTIMESYNC flags.
8447c478bd9Sstevel@tonic-gate 		 */
84539d3e169Sevanl 		if (args->syncaddr == NULL || args->syncaddr->buf == NULL) {
8467c478bd9Sstevel@tonic-gate 			error = EINVAL;
84739d3e169Sevanl 			goto errout;
84839d3e169Sevanl 		}
84939d3e169Sevanl 
8507c478bd9Sstevel@tonic-gate 		/*
8517c478bd9Sstevel@tonic-gate 		 * get time sync address.
8527c478bd9Sstevel@tonic-gate 		 */
85339d3e169Sevanl 		if (args->syncaddr == NULL) {
8547c478bd9Sstevel@tonic-gate 			error = EFAULT;
85539d3e169Sevanl 			goto errout;
8567c478bd9Sstevel@tonic-gate 		}
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 		/*
8597c478bd9Sstevel@tonic-gate 		 * Move security related data to the sec_data structure.
8607c478bd9Sstevel@tonic-gate 		 */
86139d3e169Sevanl 		{
8627c478bd9Sstevel@tonic-gate 			dh_k4_clntdata_t *data;
8637c478bd9Sstevel@tonic-gate 			char *pf, *p;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 			secdata = kmem_alloc(sizeof (*secdata), KM_SLEEP);
8667c478bd9Sstevel@tonic-gate 			if (flags & NFSMNT_RPCTIMESYNC)
8677c478bd9Sstevel@tonic-gate 				secdata->flags |= AUTH_F_RPCTIMESYNC;
8687c478bd9Sstevel@tonic-gate 			data = kmem_alloc(sizeof (*data), KM_SLEEP);
86939d3e169Sevanl 			bcopy(args->syncaddr, &data->syncaddr,
87039d3e169Sevanl 			    sizeof (*args->syncaddr));
87139d3e169Sevanl 
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 			/*
8747c478bd9Sstevel@tonic-gate 			 * duplicate the knconf information for the
8757c478bd9Sstevel@tonic-gate 			 * new opaque data.
8767c478bd9Sstevel@tonic-gate 			 */
8777c478bd9Sstevel@tonic-gate 			data->knconf = kmem_alloc(sizeof (*knconf), KM_SLEEP);
8787c478bd9Sstevel@tonic-gate 			*data->knconf = *knconf;
8797c478bd9Sstevel@tonic-gate 			pf = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
8807c478bd9Sstevel@tonic-gate 			p = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
8817c478bd9Sstevel@tonic-gate 			bcopy(knconf->knc_protofmly, pf, KNC_STRSIZE);
8827c478bd9Sstevel@tonic-gate 			bcopy(knconf->knc_proto, pf, KNC_STRSIZE);
8837c478bd9Sstevel@tonic-gate 			data->knconf->knc_protofmly = pf;
8847c478bd9Sstevel@tonic-gate 			data->knconf->knc_proto = p;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 			/* move server netname to the sec_data structure */
88739d3e169Sevanl 			nlen = strlen(args->hostname) + 1;
8887c478bd9Sstevel@tonic-gate 			if (nlen != 0) {
8897c478bd9Sstevel@tonic-gate 				data->netname = kmem_alloc(nlen, KM_SLEEP);
89039d3e169Sevanl 				bcopy(args->hostname, data->netname, nlen);
8917c478bd9Sstevel@tonic-gate 				data->netnamelen = (int)nlen;
8927c478bd9Sstevel@tonic-gate 			}
8937c478bd9Sstevel@tonic-gate 			secdata->secmod = secdata->rpcflavor = AUTH_DES;
8947c478bd9Sstevel@tonic-gate 			secdata->data = (caddr_t)data;
8957c478bd9Sstevel@tonic-gate 		}
8967c478bd9Sstevel@tonic-gate 	} else {
8977c478bd9Sstevel@tonic-gate 		secdata = kmem_alloc(sizeof (*secdata), KM_SLEEP);
8987c478bd9Sstevel@tonic-gate 		secdata->secmod = secdata->rpcflavor = AUTH_UNIX;
8997c478bd9Sstevel@tonic-gate 		secdata->data = NULL;
9007c478bd9Sstevel@tonic-gate 	}
9017c478bd9Sstevel@tonic-gate 	svp->sv_secdata = secdata;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	/*
9047c478bd9Sstevel@tonic-gate 	 * See bug 1180236.
9057c478bd9Sstevel@tonic-gate 	 * If mount secure failed, we will fall back to AUTH_NONE
9067c478bd9Sstevel@tonic-gate 	 * and try again.  nfs3rootvp() will turn this back off.
9077c478bd9Sstevel@tonic-gate 	 *
9087c478bd9Sstevel@tonic-gate 	 * The NFS Version 2 mount uses GETATTR and STATFS procedures.
9097c478bd9Sstevel@tonic-gate 	 * The server does not care if these procedures have the proper
9107c478bd9Sstevel@tonic-gate 	 * authentication flavor, so if mount retries using AUTH_NONE
9117c478bd9Sstevel@tonic-gate 	 * that does not require a credential setup for root then the
9127c478bd9Sstevel@tonic-gate 	 * automounter would work without requiring root to be
9137c478bd9Sstevel@tonic-gate 	 * keylogged into AUTH_DES.
9147c478bd9Sstevel@tonic-gate 	 */
9157c478bd9Sstevel@tonic-gate 	if (secdata->rpcflavor != AUTH_UNIX &&
9167c478bd9Sstevel@tonic-gate 	    secdata->rpcflavor != AUTH_LOOPBACK)
9177c478bd9Sstevel@tonic-gate 		secdata->flags |= AUTH_F_TRYNONE;
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	/*
9207c478bd9Sstevel@tonic-gate 	 * Failover support:
9217c478bd9Sstevel@tonic-gate 	 *
9227c478bd9Sstevel@tonic-gate 	 * We may have a linked list of nfs_args structures,
9237c478bd9Sstevel@tonic-gate 	 * which means the user is looking for failover.  If
9247c478bd9Sstevel@tonic-gate 	 * the mount is either not "read-only" or "soft",
9257c478bd9Sstevel@tonic-gate 	 * we want to bail out with EINVAL.
9267c478bd9Sstevel@tonic-gate 	 */
92739d3e169Sevanl 	if (args->nfs_args_ext == NFS_ARGS_EXTB &&
92839d3e169Sevanl 	    args->nfs_ext_u.nfs_extB.next != NULL) {
9297c478bd9Sstevel@tonic-gate 		if (uap->flags & MS_RDONLY && !(flags & NFSMNT_SOFT)) {
93039d3e169Sevanl 			data = (char *)args->nfs_ext_u.nfs_extB.next;
9317c478bd9Sstevel@tonic-gate 			goto more;
9327c478bd9Sstevel@tonic-gate 		}
9337c478bd9Sstevel@tonic-gate 		error = EINVAL;
9347c478bd9Sstevel@tonic-gate 		goto errout;
9357c478bd9Sstevel@tonic-gate 	}
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	/*
9387c478bd9Sstevel@tonic-gate 	 * Determine the zone we're being mounted into.
9397c478bd9Sstevel@tonic-gate 	 */
94045916cd2Sjpk 	zone_hold(mntzone = zone);		/* start with this assumption */
9417c478bd9Sstevel@tonic-gate 	if (getzoneid() == GLOBAL_ZONEID) {
94245916cd2Sjpk 		zone_rele(mntzone);
9437c478bd9Sstevel@tonic-gate 		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
9447c478bd9Sstevel@tonic-gate 		ASSERT(mntzone != NULL);
9457c478bd9Sstevel@tonic-gate 		if (mntzone != zone) {
9467c478bd9Sstevel@tonic-gate 			error = EBUSY;
9477c478bd9Sstevel@tonic-gate 			goto errout;
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 
95145916cd2Sjpk 	if (is_system_labeled()) {
95245916cd2Sjpk 		error = nfs_mount_label_policy(vfsp, &svp->sv_addr,
95345916cd2Sjpk 		    svp->sv_knconf, cr);
95445916cd2Sjpk 
95545916cd2Sjpk 		if (error > 0)
95645916cd2Sjpk 			goto errout;
95745916cd2Sjpk 
95845916cd2Sjpk 		if (error == -1) {
95945916cd2Sjpk 			/* change mount to read-only to prevent write-down */
96045916cd2Sjpk 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
96145916cd2Sjpk 		}
96245916cd2Sjpk 	}
96345916cd2Sjpk 
9647c478bd9Sstevel@tonic-gate 	/*
9657c478bd9Sstevel@tonic-gate 	 * Stop the mount from going any further if the zone is going away.
9667c478bd9Sstevel@tonic-gate 	 */
96745916cd2Sjpk 	if (zone_status_get(mntzone) >= ZONE_IS_SHUTTING_DOWN) {
9687c478bd9Sstevel@tonic-gate 		error = EBUSY;
9697c478bd9Sstevel@tonic-gate 		goto errout;
9707c478bd9Sstevel@tonic-gate 	}
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	/*
9737c478bd9Sstevel@tonic-gate 	 * Get root vnode.
9747c478bd9Sstevel@tonic-gate 	 */
9757c478bd9Sstevel@tonic-gate proceed:
97645916cd2Sjpk 	error = nfsrootvp(&rtvp, vfsp, svp_head, flags, cr, mntzone);
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	if (error)
9797c478bd9Sstevel@tonic-gate 		goto errout;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	/*
9827c478bd9Sstevel@tonic-gate 	 * Set option fields in the mount info record
9837c478bd9Sstevel@tonic-gate 	 */
9847c478bd9Sstevel@tonic-gate 	mi = VTOMI(rtvp);
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	if (svp_head->sv_next)
9877c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_LLOCK;
9887c478bd9Sstevel@tonic-gate 
98939d3e169Sevanl 	error = nfs_setopts(rtvp, DATAMODEL_NATIVE, args);
9907c478bd9Sstevel@tonic-gate 	if (!error) {
9917c478bd9Sstevel@tonic-gate 		/* static pathconf kludge */
99239d3e169Sevanl 		error = pathconf_get(mi, args);
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate errout:
9967c478bd9Sstevel@tonic-gate 	if (rtvp != NULL) {
997dcedb507SMarcel Telka 		if (error) {
9987c478bd9Sstevel@tonic-gate 			rp = VTOR(rtvp);
9997c478bd9Sstevel@tonic-gate 			if (rp->r_flags & RHASHED)
10007c478bd9Sstevel@tonic-gate 				rp_rmhash(rp);
10017c478bd9Sstevel@tonic-gate 		}
1002dcedb507SMarcel Telka 		VN_RELE(rtvp);
1003dcedb507SMarcel Telka 	}
1004dcedb507SMarcel Telka 
1005dcedb507SMarcel Telka 	if (error) {
10067c478bd9Sstevel@tonic-gate 		sv_free(svp_head);
10077c478bd9Sstevel@tonic-gate 		if (mi != NULL) {
10087c478bd9Sstevel@tonic-gate 			nfs_async_stop(vfsp);
10097c478bd9Sstevel@tonic-gate 			nfs_async_manager_stop(vfsp);
10107c478bd9Sstevel@tonic-gate 			if (mi->mi_io_kstats) {
10117c478bd9Sstevel@tonic-gate 				kstat_delete(mi->mi_io_kstats);
10127c478bd9Sstevel@tonic-gate 				mi->mi_io_kstats = NULL;
10137c478bd9Sstevel@tonic-gate 			}
10147c478bd9Sstevel@tonic-gate 			if (mi->mi_ro_kstats) {
10157c478bd9Sstevel@tonic-gate 				kstat_delete(mi->mi_ro_kstats);
10167c478bd9Sstevel@tonic-gate 				mi->mi_ro_kstats = NULL;
10177c478bd9Sstevel@tonic-gate 			}
10187c478bd9Sstevel@tonic-gate 			nfs_free_mi(mi);
10197c478bd9Sstevel@tonic-gate 		}
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
102218b3347eSevanl 	if (!(uap->flags & MS_SYSSPACE)) {
102339d3e169Sevanl 		nfs_free_args(args, fhandle);
102439d3e169Sevanl 		kmem_free(args, sizeof (*args));
102539d3e169Sevanl 	}
102639d3e169Sevanl 
102745916cd2Sjpk 	if (mntzone != NULL)
102845916cd2Sjpk 		zone_rele(mntzone);
102945916cd2Sjpk 
10307c478bd9Sstevel@tonic-gate 	return (error);
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate /*
10347c478bd9Sstevel@tonic-gate  * The pathconf information is kept on a linked list of kmem_alloc'ed
10357c478bd9Sstevel@tonic-gate  * structs. We search the list & add a new struct iff there is no other
10367c478bd9Sstevel@tonic-gate  * struct with the same information.
10377c478bd9Sstevel@tonic-gate  * See sys/pathconf.h for ``the rest of the story.''
10387c478bd9Sstevel@tonic-gate  */
10397c478bd9Sstevel@tonic-gate static struct pathcnf *allpc = NULL;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate static int
104239d3e169Sevanl pathconf_copyin(struct nfs_args *args, struct pathcnf *pc)
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate 	STRUCT_DECL(pathcnf, pc_tmp);
10457c478bd9Sstevel@tonic-gate 	STRUCT_HANDLE(nfs_args, ap);
10467c478bd9Sstevel@tonic-gate 	int i;
104739d3e169Sevanl 	model_t	model;
10487c478bd9Sstevel@tonic-gate 
104939d3e169Sevanl 	model = get_udatamodel();
10507c478bd9Sstevel@tonic-gate 	STRUCT_INIT(pc_tmp, model);
10517c478bd9Sstevel@tonic-gate 	STRUCT_SET_HANDLE(ap, model, args);
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	if ((STRUCT_FGET(ap, flags) & NFSMNT_POSIX) &&
10547c478bd9Sstevel@tonic-gate 	    STRUCT_FGETP(ap, pathconf) != NULL) {
10557c478bd9Sstevel@tonic-gate 		if (copyin(STRUCT_FGETP(ap, pathconf), STRUCT_BUF(pc_tmp),
10567c478bd9Sstevel@tonic-gate 		    STRUCT_SIZE(pc_tmp)))
10577c478bd9Sstevel@tonic-gate 			return (EFAULT);
10587c478bd9Sstevel@tonic-gate 		if (_PC_ISSET(_PC_ERROR, STRUCT_FGET(pc_tmp, pc_mask)))
10597c478bd9Sstevel@tonic-gate 			return (EINVAL);
10607c478bd9Sstevel@tonic-gate 
106139d3e169Sevanl 		pc->pc_link_max = STRUCT_FGET(pc_tmp, pc_link_max);
106239d3e169Sevanl 		pc->pc_max_canon = STRUCT_FGET(pc_tmp, pc_max_canon);
106339d3e169Sevanl 		pc->pc_max_input = STRUCT_FGET(pc_tmp, pc_max_input);
106439d3e169Sevanl 		pc->pc_name_max = STRUCT_FGET(pc_tmp, pc_name_max);
106539d3e169Sevanl 		pc->pc_path_max = STRUCT_FGET(pc_tmp, pc_path_max);
106639d3e169Sevanl 		pc->pc_pipe_buf = STRUCT_FGET(pc_tmp, pc_pipe_buf);
106739d3e169Sevanl 		pc->pc_vdisable = STRUCT_FGET(pc_tmp, pc_vdisable);
106839d3e169Sevanl 		pc->pc_xxx = STRUCT_FGET(pc_tmp, pc_xxx);
10697c478bd9Sstevel@tonic-gate 		for (i = 0; i < _PC_N; i++)
107039d3e169Sevanl 			pc->pc_mask[i] = STRUCT_FGET(pc_tmp, pc_mask[i]);
107139d3e169Sevanl 	}
107239d3e169Sevanl 	return (0);
107339d3e169Sevanl }
107439d3e169Sevanl 
107539d3e169Sevanl static int
107639d3e169Sevanl pathconf_get(struct mntinfo *mi, struct nfs_args *args)
107739d3e169Sevanl {
107839d3e169Sevanl 	struct pathcnf *p, *pc;
107939d3e169Sevanl 
108039d3e169Sevanl 	pc = args->pathconf;
108139d3e169Sevanl 	if (mi->mi_pathconf != NULL) {
108239d3e169Sevanl 		pathconf_rele(mi);
108339d3e169Sevanl 		mi->mi_pathconf = NULL;
108439d3e169Sevanl 	}
108539d3e169Sevanl 
1086dcedb507SMarcel Telka 	if (args->flags & NFSMNT_POSIX && args->pathconf != NULL) {
108739d3e169Sevanl 		if (_PC_ISSET(_PC_ERROR, pc->pc_mask))
108839d3e169Sevanl 			return (EINVAL);
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 		for (p = allpc; p != NULL; p = p->pc_next) {
109139d3e169Sevanl 			if (PCCMP(p, pc) == 0)
10927c478bd9Sstevel@tonic-gate 				break;
10937c478bd9Sstevel@tonic-gate 		}
10947c478bd9Sstevel@tonic-gate 		if (p != NULL) {
10957c478bd9Sstevel@tonic-gate 			mi->mi_pathconf = p;
10967c478bd9Sstevel@tonic-gate 			p->pc_refcnt++;
10977c478bd9Sstevel@tonic-gate 		} else {
10987c478bd9Sstevel@tonic-gate 			p = kmem_alloc(sizeof (*p), KM_SLEEP);
109939d3e169Sevanl 			bcopy(pc, p, sizeof (struct pathcnf));
11007c478bd9Sstevel@tonic-gate 			p->pc_next = allpc;
11017c478bd9Sstevel@tonic-gate 			p->pc_refcnt = 1;
11027c478bd9Sstevel@tonic-gate 			allpc = mi->mi_pathconf = p;
11037c478bd9Sstevel@tonic-gate 		}
11047c478bd9Sstevel@tonic-gate 	}
11057c478bd9Sstevel@tonic-gate 	return (0);
11067c478bd9Sstevel@tonic-gate }
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate /*
11097c478bd9Sstevel@tonic-gate  * release the static pathconf information
11107c478bd9Sstevel@tonic-gate  */
11117c478bd9Sstevel@tonic-gate static void
11127c478bd9Sstevel@tonic-gate pathconf_rele(struct mntinfo *mi)
11137c478bd9Sstevel@tonic-gate {
11147c478bd9Sstevel@tonic-gate 	if (mi->mi_pathconf != NULL) {
11157c478bd9Sstevel@tonic-gate 		if (--mi->mi_pathconf->pc_refcnt == 0) {
11167c478bd9Sstevel@tonic-gate 			struct pathcnf *p;
11177c478bd9Sstevel@tonic-gate 			struct pathcnf *p2;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 			p2 = p = allpc;
11207c478bd9Sstevel@tonic-gate 			while (p != NULL && p != mi->mi_pathconf) {
11217c478bd9Sstevel@tonic-gate 				p2 = p;
11227c478bd9Sstevel@tonic-gate 				p = p->pc_next;
11237c478bd9Sstevel@tonic-gate 			}
11247c478bd9Sstevel@tonic-gate 			if (p == NULL) {
11257c478bd9Sstevel@tonic-gate 				panic("mi->pathconf");
11267c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
11277c478bd9Sstevel@tonic-gate 			}
11287c478bd9Sstevel@tonic-gate 			if (p == allpc)
11297c478bd9Sstevel@tonic-gate 				allpc = p->pc_next;
11307c478bd9Sstevel@tonic-gate 			else
11317c478bd9Sstevel@tonic-gate 				p2->pc_next = p->pc_next;
11327c478bd9Sstevel@tonic-gate 			kmem_free(p, sizeof (*p));
11337c478bd9Sstevel@tonic-gate 			mi->mi_pathconf = NULL;
11347c478bd9Sstevel@tonic-gate 		}
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate }
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate static int nfs_dynamic = 1;	/* global variable to enable dynamic retrans. */
11397c478bd9Sstevel@tonic-gate static ushort_t nfs_max_threads = 8;	/* max number of active async threads */
11407c478bd9Sstevel@tonic-gate static uint_t nfs_async_clusters = 1;	/* # of reqs from each async queue */
11417c478bd9Sstevel@tonic-gate static uint_t nfs_cots_timeo = NFS_COTS_TIMEO;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate static int
11447c478bd9Sstevel@tonic-gate nfsrootvp(vnode_t **rtvpp, vfs_t *vfsp, struct servinfo *svp,
11457c478bd9Sstevel@tonic-gate     int flags, cred_t *cr, zone_t *zone)
11467c478bd9Sstevel@tonic-gate {
11477c478bd9Sstevel@tonic-gate 	vnode_t *rtvp;
11487c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
11497c478bd9Sstevel@tonic-gate 	dev_t nfs_dev;
11507c478bd9Sstevel@tonic-gate 	struct vattr va;
11517c478bd9Sstevel@tonic-gate 	int error;
11527c478bd9Sstevel@tonic-gate 	rnode_t *rp;
11537c478bd9Sstevel@tonic-gate 	int i;
11547c478bd9Sstevel@tonic-gate 	struct nfs_stats *nfsstatsp;
11557c478bd9Sstevel@tonic-gate 	cred_t *lcr = NULL, *tcr = cr;
11567c478bd9Sstevel@tonic-gate 
1157108322fbScarlsonj 	nfsstatsp = zone_getspecific(nfsstat_zone_key, nfs_zone());
11587c478bd9Sstevel@tonic-gate 	ASSERT(nfsstatsp != NULL);
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	/*
11617c478bd9Sstevel@tonic-gate 	 * Create a mount record and link it to the vfs struct.
11627c478bd9Sstevel@tonic-gate 	 */
11637c478bd9Sstevel@tonic-gate 	mi = kmem_zalloc(sizeof (*mi), KM_SLEEP);
11647c478bd9Sstevel@tonic-gate 	mutex_init(&mi->mi_lock, NULL, MUTEX_DEFAULT, NULL);
1165e8dc3b7dSvv149972 	mutex_init(&mi->mi_remap_lock, NULL, MUTEX_DEFAULT, NULL);
11667c478bd9Sstevel@tonic-gate 	mi->mi_flags = MI_ACL | MI_EXTATTR;
11677c478bd9Sstevel@tonic-gate 	if (!(flags & NFSMNT_SOFT))
11687c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_HARD;
11697c478bd9Sstevel@tonic-gate 	if ((flags & NFSMNT_SEMISOFT))
11707c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_SEMISOFT;
11717c478bd9Sstevel@tonic-gate 	if ((flags & NFSMNT_NOPRINT))
11727c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_NOPRINT;
11737c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_INT)
11747c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_INT;
11757c478bd9Sstevel@tonic-gate 	mi->mi_retrans = NFS_RETRIES;
11767c478bd9Sstevel@tonic-gate 	if (svp->sv_knconf->knc_semantics == NC_TPI_COTS_ORD ||
11777c478bd9Sstevel@tonic-gate 	    svp->sv_knconf->knc_semantics == NC_TPI_COTS)
11787c478bd9Sstevel@tonic-gate 		mi->mi_timeo = nfs_cots_timeo;
11797c478bd9Sstevel@tonic-gate 	else
11807c478bd9Sstevel@tonic-gate 		mi->mi_timeo = NFS_TIMEO;
11817c478bd9Sstevel@tonic-gate 	mi->mi_prog = NFS_PROGRAM;
11827c478bd9Sstevel@tonic-gate 	mi->mi_vers = NFS_VERSION;
11837c478bd9Sstevel@tonic-gate 	mi->mi_rfsnames = rfsnames_v2;
11847c478bd9Sstevel@tonic-gate 	mi->mi_reqs = nfsstatsp->nfs_stats_v2.rfsreqcnt_ptr;
11857c478bd9Sstevel@tonic-gate 	mi->mi_call_type = call_type_v2;
11867c478bd9Sstevel@tonic-gate 	mi->mi_ss_call_type = ss_call_type_v2;
11877c478bd9Sstevel@tonic-gate 	mi->mi_timer_type = timer_type_v2;
11887c478bd9Sstevel@tonic-gate 	mi->mi_aclnames = aclnames_v2;
11897c478bd9Sstevel@tonic-gate 	mi->mi_aclreqs = nfsstatsp->nfs_stats_v2.aclreqcnt_ptr;
11907c478bd9Sstevel@tonic-gate 	mi->mi_acl_call_type = acl_call_type_v2;
11917c478bd9Sstevel@tonic-gate 	mi->mi_acl_ss_call_type = acl_ss_call_type_v2;
11927c478bd9Sstevel@tonic-gate 	mi->mi_acl_timer_type = acl_timer_type_v2;
11937c478bd9Sstevel@tonic-gate 	cv_init(&mi->mi_failover_cv, NULL, CV_DEFAULT, NULL);
11947c478bd9Sstevel@tonic-gate 	mi->mi_servers = svp;
11957c478bd9Sstevel@tonic-gate 	mi->mi_curr_serv = svp;
11967c478bd9Sstevel@tonic-gate 	mi->mi_acregmin = SEC2HR(ACREGMIN);
11977c478bd9Sstevel@tonic-gate 	mi->mi_acregmax = SEC2HR(ACREGMAX);
11987c478bd9Sstevel@tonic-gate 	mi->mi_acdirmin = SEC2HR(ACDIRMIN);
11997c478bd9Sstevel@tonic-gate 	mi->mi_acdirmax = SEC2HR(ACDIRMAX);
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	if (nfs_dynamic)
12027c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_DYNAMIC;
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_DIRECTIO)
12057c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_DIRECTIO;
1206*8a790dc6SMarcel Telka 
1207*8a790dc6SMarcel Telka 	mutex_init(&mi->mi_rnodes_lock, NULL, MUTEX_DEFAULT, NULL);
1208*8a790dc6SMarcel Telka 	list_create(&mi->mi_rnodes, sizeof (rnode_t),
1209*8a790dc6SMarcel Telka 	    offsetof(rnode_t, r_mi_link));
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	/*
12127c478bd9Sstevel@tonic-gate 	 * Make a vfs struct for nfs.  We do this here instead of below
12137c478bd9Sstevel@tonic-gate 	 * because rtvp needs a vfs before we can do a getattr on it.
12147c478bd9Sstevel@tonic-gate 	 *
12157c478bd9Sstevel@tonic-gate 	 * Assign a unique device id to the mount
12167c478bd9Sstevel@tonic-gate 	 */
12177c478bd9Sstevel@tonic-gate 	mutex_enter(&nfs_minor_lock);
12187c478bd9Sstevel@tonic-gate 	do {
12197c478bd9Sstevel@tonic-gate 		nfs_minor = (nfs_minor + 1) & MAXMIN32;
12207c478bd9Sstevel@tonic-gate 		nfs_dev = makedevice(nfs_major, nfs_minor);
12217c478bd9Sstevel@tonic-gate 	} while (vfs_devismounted(nfs_dev));
12227c478bd9Sstevel@tonic-gate 	mutex_exit(&nfs_minor_lock);
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	vfsp->vfs_dev = nfs_dev;
12257c478bd9Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, nfs_dev, nfsfstyp);
12267c478bd9Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)mi;
12277c478bd9Sstevel@tonic-gate 	vfsp->vfs_fstype = nfsfstyp;
12287c478bd9Sstevel@tonic-gate 	vfsp->vfs_bsize = NFS_MAXDATA;
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	/*
12317c478bd9Sstevel@tonic-gate 	 * Initialize fields used to support async putpage operations.
12327c478bd9Sstevel@tonic-gate 	 */
12337c478bd9Sstevel@tonic-gate 	for (i = 0; i < NFS_ASYNC_TYPES; i++)
12347c478bd9Sstevel@tonic-gate 		mi->mi_async_clusters[i] = nfs_async_clusters;
12357c478bd9Sstevel@tonic-gate 	mi->mi_async_init_clusters = nfs_async_clusters;
12360776f5e6SVallish Vaidyeshwara 	mi->mi_async_curr[NFS_ASYNC_QUEUE] =
12370776f5e6SVallish Vaidyeshwara 	    mi->mi_async_curr[NFS_ASYNC_PGOPS_QUEUE] = &mi->mi_async_reqs[0];
12387c478bd9Sstevel@tonic-gate 	mi->mi_max_threads = nfs_max_threads;
12397c478bd9Sstevel@tonic-gate 	mutex_init(&mi->mi_async_lock, NULL, MUTEX_DEFAULT, NULL);
12407c478bd9Sstevel@tonic-gate 	cv_init(&mi->mi_async_reqs_cv, NULL, CV_DEFAULT, NULL);
12410776f5e6SVallish Vaidyeshwara 	cv_init(&mi->mi_async_work_cv[NFS_ASYNC_QUEUE], NULL, CV_DEFAULT, NULL);
12420776f5e6SVallish Vaidyeshwara 	cv_init(&mi->mi_async_work_cv[NFS_ASYNC_PGOPS_QUEUE], NULL,
12430776f5e6SVallish Vaidyeshwara 	    CV_DEFAULT, NULL);
12447c478bd9Sstevel@tonic-gate 	cv_init(&mi->mi_async_cv, NULL, CV_DEFAULT, NULL);
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 	mi->mi_vfsp = vfsp;
1247a19609f8Sjv227347 	mi->mi_zone = zone;
1248a19609f8Sjv227347 	zone_init_ref(&mi->mi_zone_ref);
1249a19609f8Sjv227347 	zone_hold_ref(zone, &mi->mi_zone_ref, ZONE_REF_NFS);
12507c478bd9Sstevel@tonic-gate 	nfs_mi_zonelist_add(mi);
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	/*
12537c478bd9Sstevel@tonic-gate 	 * Make the root vnode, use it to get attributes,
12547c478bd9Sstevel@tonic-gate 	 * then remake it with the attributes.
12557c478bd9Sstevel@tonic-gate 	 */
12567c478bd9Sstevel@tonic-gate 	rtvp = makenfsnode((fhandle_t *)svp->sv_fhandle.fh_buf,
12577c478bd9Sstevel@tonic-gate 	    NULL, vfsp, gethrtime(), cr, NULL, NULL);
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	va.va_mask = AT_ALL;
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	/*
12627c478bd9Sstevel@tonic-gate 	 * If the uid is set then set the creds for secure mounts
12637c478bd9Sstevel@tonic-gate 	 * by proxy processes such as automountd.
12647c478bd9Sstevel@tonic-gate 	 */
12657c478bd9Sstevel@tonic-gate 	if (svp->sv_secdata->uid != 0 &&
12667c478bd9Sstevel@tonic-gate 	    svp->sv_secdata->rpcflavor == RPCSEC_GSS) {
12677c478bd9Sstevel@tonic-gate 		lcr = crdup(cr);
12687c478bd9Sstevel@tonic-gate 		(void) crsetugid(lcr, svp->sv_secdata->uid, crgetgid(cr));
12697c478bd9Sstevel@tonic-gate 		tcr = lcr;
12707c478bd9Sstevel@tonic-gate 	}
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	error = nfsgetattr(rtvp, &va, tcr);
12737c478bd9Sstevel@tonic-gate 	if (error)
12747c478bd9Sstevel@tonic-gate 		goto bad;
12757c478bd9Sstevel@tonic-gate 	rtvp->v_type = va.va_type;
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	/*
12787c478bd9Sstevel@tonic-gate 	 * Poll every server to get the filesystem stats; we're
12797c478bd9Sstevel@tonic-gate 	 * only interested in the server's transfer size, and we
12807c478bd9Sstevel@tonic-gate 	 * want the minimum.
12817c478bd9Sstevel@tonic-gate 	 *
12827c478bd9Sstevel@tonic-gate 	 * While we're looping, we'll turn off AUTH_F_TRYNONE,
12837c478bd9Sstevel@tonic-gate 	 * which is only for the mount operation.
12847c478bd9Sstevel@tonic-gate 	 */
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	mi->mi_tsize = MIN(NFS_MAXDATA, nfstsize());
12877c478bd9Sstevel@tonic-gate 	mi->mi_stsize = MIN(NFS_MAXDATA, nfstsize());
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	for (svp = mi->mi_servers; svp != NULL; svp = svp->sv_next) {
12907c478bd9Sstevel@tonic-gate 		struct nfsstatfs fs;
12917c478bd9Sstevel@tonic-gate 		int douprintf;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 		douprintf = 1;
12947c478bd9Sstevel@tonic-gate 		mi->mi_curr_serv = svp;
12957c478bd9Sstevel@tonic-gate 
1296dcedb507SMarcel Telka 		error = rfs2call(mi, RFS_STATFS, xdr_fhandle,
1297dcedb507SMarcel Telka 		    (caddr_t)svp->sv_fhandle.fh_buf, xdr_statfs, (caddr_t)&fs,
1298dcedb507SMarcel Telka 		    tcr, &douprintf, &fs.fs_status, 0, NULL);
12997c478bd9Sstevel@tonic-gate 		if (error)
13007c478bd9Sstevel@tonic-gate 			goto bad;
13017c478bd9Sstevel@tonic-gate 		mi->mi_stsize = MIN(mi->mi_stsize, fs.fs_tsize);
13027c478bd9Sstevel@tonic-gate 		svp->sv_secdata->flags &= ~AUTH_F_TRYNONE;
13037c478bd9Sstevel@tonic-gate 	}
13047c478bd9Sstevel@tonic-gate 	mi->mi_curr_serv = mi->mi_servers;
13057c478bd9Sstevel@tonic-gate 	mi->mi_curread = mi->mi_tsize;
13067c478bd9Sstevel@tonic-gate 	mi->mi_curwrite = mi->mi_stsize;
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	/*
13097c478bd9Sstevel@tonic-gate 	 * Start the manager thread responsible for handling async worker
13107c478bd9Sstevel@tonic-gate 	 * threads.
13117c478bd9Sstevel@tonic-gate 	 */
13127c478bd9Sstevel@tonic-gate 	VFS_HOLD(vfsp);	/* add reference for thread */
13137c478bd9Sstevel@tonic-gate 	mi->mi_manager_thread = zthread_create(NULL, 0, nfs_async_manager,
13147c478bd9Sstevel@tonic-gate 	    vfsp, 0, minclsyspri);
13157c478bd9Sstevel@tonic-gate 	ASSERT(mi->mi_manager_thread != NULL);
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	/*
13187c478bd9Sstevel@tonic-gate 	 * Initialize kstats
13197c478bd9Sstevel@tonic-gate 	 */
13207c478bd9Sstevel@tonic-gate 	nfs_mnt_kstat_init(vfsp);
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	mi->mi_type = rtvp->v_type;
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	*rtvpp = rtvp;
13257c478bd9Sstevel@tonic-gate 	if (lcr != NULL)
13267c478bd9Sstevel@tonic-gate 		crfree(lcr);
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	return (0);
13297c478bd9Sstevel@tonic-gate bad:
13307c478bd9Sstevel@tonic-gate 	/*
13317c478bd9Sstevel@tonic-gate 	 * An error occurred somewhere, need to clean up...
13327c478bd9Sstevel@tonic-gate 	 * We need to release our reference to the root vnode and
13337c478bd9Sstevel@tonic-gate 	 * destroy the mntinfo struct that we just created.
13347c478bd9Sstevel@tonic-gate 	 */
13357c478bd9Sstevel@tonic-gate 	if (lcr != NULL)
13367c478bd9Sstevel@tonic-gate 		crfree(lcr);
13377c478bd9Sstevel@tonic-gate 	rp = VTOR(rtvp);
13387c478bd9Sstevel@tonic-gate 	if (rp->r_flags & RHASHED)
13397c478bd9Sstevel@tonic-gate 		rp_rmhash(rp);
13407c478bd9Sstevel@tonic-gate 	VN_RELE(rtvp);
13417c478bd9Sstevel@tonic-gate 	nfs_async_stop(vfsp);
13427c478bd9Sstevel@tonic-gate 	nfs_async_manager_stop(vfsp);
13437c478bd9Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
13447c478bd9Sstevel@tonic-gate 		kstat_delete(mi->mi_io_kstats);
13457c478bd9Sstevel@tonic-gate 		mi->mi_io_kstats = NULL;
13467c478bd9Sstevel@tonic-gate 	}
13477c478bd9Sstevel@tonic-gate 	if (mi->mi_ro_kstats) {
13487c478bd9Sstevel@tonic-gate 		kstat_delete(mi->mi_ro_kstats);
13497c478bd9Sstevel@tonic-gate 		mi->mi_ro_kstats = NULL;
13507c478bd9Sstevel@tonic-gate 	}
13517c478bd9Sstevel@tonic-gate 	nfs_free_mi(mi);
13527c478bd9Sstevel@tonic-gate 	*rtvpp = NULL;
13537c478bd9Sstevel@tonic-gate 	return (error);
13547c478bd9Sstevel@tonic-gate }
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate /*
13577c478bd9Sstevel@tonic-gate  * vfs operations
13587c478bd9Sstevel@tonic-gate  */
13597c478bd9Sstevel@tonic-gate static int
13607c478bd9Sstevel@tonic-gate nfs_unmount(vfs_t *vfsp, int flag, cred_t *cr)
13617c478bd9Sstevel@tonic-gate {
13627c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
13637c478bd9Sstevel@tonic-gate 	ushort_t omax;
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
13667c478bd9Sstevel@tonic-gate 		return (EPERM);
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 	mi = VFTOMI(vfsp);
13697c478bd9Sstevel@tonic-gate 	if (flag & MS_FORCE) {
1370423be36cSmaheshvs 
13717c478bd9Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_UNMOUNTED;
1372423be36cSmaheshvs 
1373423be36cSmaheshvs 		/*
1374423be36cSmaheshvs 		 * We are about to stop the async manager.
1375423be36cSmaheshvs 		 * Let every one know not to schedule any
1376423be36cSmaheshvs 		 * more async requests.
1377423be36cSmaheshvs 		 */
1378423be36cSmaheshvs 		mutex_enter(&mi->mi_async_lock);
1379423be36cSmaheshvs 		mi->mi_max_threads = 0;
13800776f5e6SVallish Vaidyeshwara 		NFS_WAKEALL_ASYNC_WORKERS(mi->mi_async_work_cv);
1381423be36cSmaheshvs 		mutex_exit(&mi->mi_async_lock);
1382423be36cSmaheshvs 
13837c478bd9Sstevel@tonic-gate 		/*
13847c478bd9Sstevel@tonic-gate 		 * We need to stop the manager thread explicitly; the worker
13857c478bd9Sstevel@tonic-gate 		 * threads can time out and exit on their own.
13867c478bd9Sstevel@tonic-gate 		 */
13877c478bd9Sstevel@tonic-gate 		nfs_async_manager_stop(vfsp);
13887c478bd9Sstevel@tonic-gate 		destroy_rtable(vfsp, cr);
13897c478bd9Sstevel@tonic-gate 		if (mi->mi_io_kstats) {
13907c478bd9Sstevel@tonic-gate 			kstat_delete(mi->mi_io_kstats);
13917c478bd9Sstevel@tonic-gate 			mi->mi_io_kstats = NULL;
13927c478bd9Sstevel@tonic-gate 		}
13937c478bd9Sstevel@tonic-gate 		if (mi->mi_ro_kstats) {
13947c478bd9Sstevel@tonic-gate 			kstat_delete(mi->mi_ro_kstats);
13957c478bd9Sstevel@tonic-gate 			mi->mi_ro_kstats = NULL;
13967c478bd9Sstevel@tonic-gate 		}
13977c478bd9Sstevel@tonic-gate 		return (0);
13987c478bd9Sstevel@tonic-gate 	}
13997c478bd9Sstevel@tonic-gate 	/*
14007c478bd9Sstevel@tonic-gate 	 * Wait until all asynchronous putpage operations on
14017c478bd9Sstevel@tonic-gate 	 * this file system are complete before flushing rnodes
14027c478bd9Sstevel@tonic-gate 	 * from the cache.
14037c478bd9Sstevel@tonic-gate 	 */
14047c478bd9Sstevel@tonic-gate 	omax = mi->mi_max_threads;
14057c478bd9Sstevel@tonic-gate 	if (nfs_async_stop_sig(vfsp)) {
14067c478bd9Sstevel@tonic-gate 		return (EINTR);
14077c478bd9Sstevel@tonic-gate 	}
14087c478bd9Sstevel@tonic-gate 	rflush(vfsp, cr);
14097c478bd9Sstevel@tonic-gate 	/*
14107c478bd9Sstevel@tonic-gate 	 * If there are any active vnodes on this file system,
14117c478bd9Sstevel@tonic-gate 	 * then the file system is busy and can't be umounted.
14127c478bd9Sstevel@tonic-gate 	 */
14137c478bd9Sstevel@tonic-gate 	if (check_rtable(vfsp)) {
14147c478bd9Sstevel@tonic-gate 		mutex_enter(&mi->mi_async_lock);
14157c478bd9Sstevel@tonic-gate 		mi->mi_max_threads = omax;
14167c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_async_lock);
14177c478bd9Sstevel@tonic-gate 		return (EBUSY);
14187c478bd9Sstevel@tonic-gate 	}
14197c478bd9Sstevel@tonic-gate 	/*
14207c478bd9Sstevel@tonic-gate 	 * The unmount can't fail from now on; stop the manager thread.
14217c478bd9Sstevel@tonic-gate 	 */
14227c478bd9Sstevel@tonic-gate 	nfs_async_manager_stop(vfsp);
14237c478bd9Sstevel@tonic-gate 	/*
14247c478bd9Sstevel@tonic-gate 	 * Destroy all rnodes belonging to this file system from the
14257c478bd9Sstevel@tonic-gate 	 * rnode hash queues and purge any resources allocated to
14267c478bd9Sstevel@tonic-gate 	 * them.
14277c478bd9Sstevel@tonic-gate 	 */
14287c478bd9Sstevel@tonic-gate 	destroy_rtable(vfsp, cr);
14297c478bd9Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
14307c478bd9Sstevel@tonic-gate 		kstat_delete(mi->mi_io_kstats);
14317c478bd9Sstevel@tonic-gate 		mi->mi_io_kstats = NULL;
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate 	if (mi->mi_ro_kstats) {
14347c478bd9Sstevel@tonic-gate 		kstat_delete(mi->mi_ro_kstats);
14357c478bd9Sstevel@tonic-gate 		mi->mi_ro_kstats = NULL;
14367c478bd9Sstevel@tonic-gate 	}
14377c478bd9Sstevel@tonic-gate 	return (0);
14387c478bd9Sstevel@tonic-gate }
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate /*
14417c478bd9Sstevel@tonic-gate  * find root of nfs
14427c478bd9Sstevel@tonic-gate  */
14437c478bd9Sstevel@tonic-gate static int
14447c478bd9Sstevel@tonic-gate nfs_root(vfs_t *vfsp, vnode_t **vpp)
14457c478bd9Sstevel@tonic-gate {
14467c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
14477c478bd9Sstevel@tonic-gate 	vnode_t *vp;
14487c478bd9Sstevel@tonic-gate 	servinfo_t *svp;
1449210225b5Sgt29601 	rnode_t *rp;
1450210225b5Sgt29601 	int error = 0;
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	mi = VFTOMI(vfsp);
14537c478bd9Sstevel@tonic-gate 
1454108322fbScarlsonj 	if (nfs_zone() != mi->mi_zone)
14557c478bd9Sstevel@tonic-gate 		return (EPERM);
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 	svp = mi->mi_curr_serv;
14587c478bd9Sstevel@tonic-gate 	if (svp && (svp->sv_flags & SV_ROOT_STALE)) {
14597c478bd9Sstevel@tonic-gate 		mutex_enter(&svp->sv_lock);
14607c478bd9Sstevel@tonic-gate 		svp->sv_flags &= ~SV_ROOT_STALE;
14617c478bd9Sstevel@tonic-gate 		mutex_exit(&svp->sv_lock);
1462210225b5Sgt29601 		error = ENOENT;
14637c478bd9Sstevel@tonic-gate 	}
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	vp = makenfsnode((fhandle_t *)mi->mi_curr_serv->sv_fhandle.fh_buf,
14667c478bd9Sstevel@tonic-gate 	    NULL, vfsp, gethrtime(), CRED(), NULL, NULL);
14677c478bd9Sstevel@tonic-gate 
1468210225b5Sgt29601 	/*
1469210225b5Sgt29601 	 * if the SV_ROOT_STALE flag was reset above, reset the
1470210225b5Sgt29601 	 * RSTALE flag if needed and return an error
1471210225b5Sgt29601 	 */
1472210225b5Sgt29601 	if (error == ENOENT) {
1473210225b5Sgt29601 		rp = VTOR(vp);
1474210225b5Sgt29601 		if (svp && rp->r_flags & RSTALE) {
1475210225b5Sgt29601 			mutex_enter(&rp->r_statelock);
1476210225b5Sgt29601 			rp->r_flags &= ~RSTALE;
1477210225b5Sgt29601 			mutex_exit(&rp->r_statelock);
1478210225b5Sgt29601 		}
14797c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1480210225b5Sgt29601 		return (error);
14817c478bd9Sstevel@tonic-gate 	}
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VNON || vp->v_type == mi->mi_type);
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 	vp->v_type = mi->mi_type;
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	*vpp = vp;
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	return (0);
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate /*
14937c478bd9Sstevel@tonic-gate  * Get file system statistics.
14947c478bd9Sstevel@tonic-gate  */
14957c478bd9Sstevel@tonic-gate static int
14967c478bd9Sstevel@tonic-gate nfs_statvfs(vfs_t *vfsp, struct statvfs64 *sbp)
14977c478bd9Sstevel@tonic-gate {
14987c478bd9Sstevel@tonic-gate 	int error;
14997c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
15007c478bd9Sstevel@tonic-gate 	struct nfsstatfs fs;
15017c478bd9Sstevel@tonic-gate 	int douprintf;
15027c478bd9Sstevel@tonic-gate 	failinfo_t fi;
15037c478bd9Sstevel@tonic-gate 	vnode_t *vp;
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	error = nfs_root(vfsp, &vp);
15067c478bd9Sstevel@tonic-gate 	if (error)
15077c478bd9Sstevel@tonic-gate 		return (error);
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	mi = VFTOMI(vfsp);
15107c478bd9Sstevel@tonic-gate 	douprintf = 1;
15117c478bd9Sstevel@tonic-gate 	fi.vp = vp;
15127c478bd9Sstevel@tonic-gate 	fi.fhp = NULL;		/* no need to update, filehandle not copied */
15137c478bd9Sstevel@tonic-gate 	fi.copyproc = nfscopyfh;
15147c478bd9Sstevel@tonic-gate 	fi.lookupproc = nfslookup;
15157c478bd9Sstevel@tonic-gate 	fi.xattrdirproc = acl_getxattrdir2;
15167c478bd9Sstevel@tonic-gate 
1517dcedb507SMarcel Telka 	error = rfs2call(mi, RFS_STATFS, xdr_fhandle, (caddr_t)VTOFH(vp),
1518dcedb507SMarcel Telka 	    xdr_statfs, (caddr_t)&fs, CRED(), &douprintf, &fs.fs_status, 0,
1519dcedb507SMarcel Telka 	    &fi);
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	if (!error) {
15227c478bd9Sstevel@tonic-gate 		error = geterrno(fs.fs_status);
15237c478bd9Sstevel@tonic-gate 		if (!error) {
15247c478bd9Sstevel@tonic-gate 			mutex_enter(&mi->mi_lock);
15257c478bd9Sstevel@tonic-gate 			if (mi->mi_stsize) {
15267c478bd9Sstevel@tonic-gate 				mi->mi_stsize = MIN(mi->mi_stsize, fs.fs_tsize);
15277c478bd9Sstevel@tonic-gate 			} else {
15287c478bd9Sstevel@tonic-gate 				mi->mi_stsize = fs.fs_tsize;
15297c478bd9Sstevel@tonic-gate 				mi->mi_curwrite = mi->mi_stsize;
15307c478bd9Sstevel@tonic-gate 			}
15317c478bd9Sstevel@tonic-gate 			mutex_exit(&mi->mi_lock);
15327c478bd9Sstevel@tonic-gate 			sbp->f_bsize = fs.fs_bsize;
15337c478bd9Sstevel@tonic-gate 			sbp->f_frsize = fs.fs_bsize;
15347c478bd9Sstevel@tonic-gate 			sbp->f_blocks = (fsblkcnt64_t)fs.fs_blocks;
15357c478bd9Sstevel@tonic-gate 			sbp->f_bfree = (fsblkcnt64_t)fs.fs_bfree;
15367c478bd9Sstevel@tonic-gate 			/*
15377c478bd9Sstevel@tonic-gate 			 * Some servers may return negative available
15387c478bd9Sstevel@tonic-gate 			 * block counts.  They may do this because they
15397c478bd9Sstevel@tonic-gate 			 * calculate the number of available blocks by
15407c478bd9Sstevel@tonic-gate 			 * subtracting the number of used blocks from
15417c478bd9Sstevel@tonic-gate 			 * the total number of blocks modified by the
15427c478bd9Sstevel@tonic-gate 			 * minimum free value.  For example, if the
15437c478bd9Sstevel@tonic-gate 			 * minumum free percentage is 10 and the file
15447c478bd9Sstevel@tonic-gate 			 * system is greater than 90 percent full, then
15457c478bd9Sstevel@tonic-gate 			 * 90 percent of the total blocks minus the
15467c478bd9Sstevel@tonic-gate 			 * actual number of used blocks may be a
15477c478bd9Sstevel@tonic-gate 			 * negative number.
15487c478bd9Sstevel@tonic-gate 			 *
15497c478bd9Sstevel@tonic-gate 			 * In this case, we need to sign extend the
15507c478bd9Sstevel@tonic-gate 			 * negative number through the assignment from
15517c478bd9Sstevel@tonic-gate 			 * the 32 bit bavail count to the 64 bit bavail
15527c478bd9Sstevel@tonic-gate 			 * count.
15537c478bd9Sstevel@tonic-gate 			 *
15547c478bd9Sstevel@tonic-gate 			 * We need to be able to discern between there
15557c478bd9Sstevel@tonic-gate 			 * just being a lot of available blocks on the
15567c478bd9Sstevel@tonic-gate 			 * file system and the case described above.
15577c478bd9Sstevel@tonic-gate 			 * We are making the assumption that it does
15587c478bd9Sstevel@tonic-gate 			 * not make sense to have more available blocks
15597c478bd9Sstevel@tonic-gate 			 * than there are free blocks.  So, if there
15607c478bd9Sstevel@tonic-gate 			 * are, then we treat the number as if it were
15617c478bd9Sstevel@tonic-gate 			 * a negative number and arrange to have it
15627c478bd9Sstevel@tonic-gate 			 * sign extended when it is converted from 32
15637c478bd9Sstevel@tonic-gate 			 * bits to 64 bits.
15647c478bd9Sstevel@tonic-gate 			 */
15657c478bd9Sstevel@tonic-gate 			if (fs.fs_bavail <= fs.fs_bfree)
15667c478bd9Sstevel@tonic-gate 				sbp->f_bavail = (fsblkcnt64_t)fs.fs_bavail;
15677c478bd9Sstevel@tonic-gate 			else {
15687c478bd9Sstevel@tonic-gate 				sbp->f_bavail =
15697c478bd9Sstevel@tonic-gate 				    (fsblkcnt64_t)((long)fs.fs_bavail);
15707c478bd9Sstevel@tonic-gate 			}
15717c478bd9Sstevel@tonic-gate 			sbp->f_files = (fsfilcnt64_t)-1;
15727c478bd9Sstevel@tonic-gate 			sbp->f_ffree = (fsfilcnt64_t)-1;
15737c478bd9Sstevel@tonic-gate 			sbp->f_favail = (fsfilcnt64_t)-1;
15747c478bd9Sstevel@tonic-gate 			sbp->f_fsid = (unsigned long)vfsp->vfs_fsid.val[0];
15757c478bd9Sstevel@tonic-gate 			(void) strncpy(sbp->f_basetype,
15767c478bd9Sstevel@tonic-gate 			    vfssw[vfsp->vfs_fstype].vsw_name, FSTYPSZ);
15777c478bd9Sstevel@tonic-gate 			sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
15787c478bd9Sstevel@tonic-gate 			sbp->f_namemax = (uint32_t)-1;
15797c478bd9Sstevel@tonic-gate 		} else {
15807c478bd9Sstevel@tonic-gate 			PURGE_STALE_FH(error, vp, CRED());
15817c478bd9Sstevel@tonic-gate 		}
15827c478bd9Sstevel@tonic-gate 	}
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate 	return (error);
15877c478bd9Sstevel@tonic-gate }
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate static kmutex_t nfs_syncbusy;
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate /*
15927c478bd9Sstevel@tonic-gate  * Flush dirty nfs files for file system vfsp.
15937c478bd9Sstevel@tonic-gate  * If vfsp == NULL, all nfs files are flushed.
15947c478bd9Sstevel@tonic-gate  */
15957c478bd9Sstevel@tonic-gate /* ARGSUSED */
15967c478bd9Sstevel@tonic-gate static int
15977c478bd9Sstevel@tonic-gate nfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
15987c478bd9Sstevel@tonic-gate {
15997c478bd9Sstevel@tonic-gate 	/*
16007c478bd9Sstevel@tonic-gate 	 * Cross-zone calls are OK here, since this translates to a
16017c478bd9Sstevel@tonic-gate 	 * VOP_PUTPAGE(B_ASYNC), which gets picked up by the right zone.
16027c478bd9Sstevel@tonic-gate 	 */
16037c478bd9Sstevel@tonic-gate 	if (!(flag & SYNC_ATTR) && mutex_tryenter(&nfs_syncbusy) != 0) {
16047c478bd9Sstevel@tonic-gate 		rflush(vfsp, cr);
16057c478bd9Sstevel@tonic-gate 		mutex_exit(&nfs_syncbusy);
16067c478bd9Sstevel@tonic-gate 	}
16077c478bd9Sstevel@tonic-gate 	return (0);
16087c478bd9Sstevel@tonic-gate }
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate /* ARGSUSED */
16117c478bd9Sstevel@tonic-gate static int
16127c478bd9Sstevel@tonic-gate nfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
16137c478bd9Sstevel@tonic-gate {
16147c478bd9Sstevel@tonic-gate 	int error;
16157c478bd9Sstevel@tonic-gate 	vnode_t *vp;
16167c478bd9Sstevel@tonic-gate 	struct vattr va;
16177c478bd9Sstevel@tonic-gate 	struct nfs_fid *nfsfidp = (struct nfs_fid *)fidp;
16187c478bd9Sstevel@tonic-gate 	zoneid_t zoneid = VFTOMI(vfsp)->mi_zone->zone_id;
16197c478bd9Sstevel@tonic-gate 
1620108322fbScarlsonj 	if (nfs_zone() != VFTOMI(vfsp)->mi_zone)
16217c478bd9Sstevel@tonic-gate 		return (EPERM);
16227c478bd9Sstevel@tonic-gate 	if (fidp->fid_len != (sizeof (*nfsfidp) - sizeof (short))) {
16237c478bd9Sstevel@tonic-gate #ifdef DEBUG
16247c478bd9Sstevel@tonic-gate 		zcmn_err(zoneid, CE_WARN,
16257c478bd9Sstevel@tonic-gate 		    "nfs_vget: bad fid len, %d/%d", fidp->fid_len,
16267c478bd9Sstevel@tonic-gate 		    (int)(sizeof (*nfsfidp) - sizeof (short)));
16277c478bd9Sstevel@tonic-gate #endif
16287c478bd9Sstevel@tonic-gate 		*vpp = NULL;
16297c478bd9Sstevel@tonic-gate 		return (ESTALE);
16307c478bd9Sstevel@tonic-gate 	}
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	vp = makenfsnode((fhandle_t *)(nfsfidp->nf_data), NULL, vfsp,
16337c478bd9Sstevel@tonic-gate 	    gethrtime(), CRED(), NULL, NULL);
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	if (VTOR(vp)->r_flags & RSTALE) {
16367c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
16377c478bd9Sstevel@tonic-gate 		*vpp = NULL;
16387c478bd9Sstevel@tonic-gate 		return (ENOENT);
16397c478bd9Sstevel@tonic-gate 	}
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	if (vp->v_type == VNON) {
16427c478bd9Sstevel@tonic-gate 		va.va_mask = AT_ALL;
16437c478bd9Sstevel@tonic-gate 		error = nfsgetattr(vp, &va, CRED());
16447c478bd9Sstevel@tonic-gate 		if (error) {
16457c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
16467c478bd9Sstevel@tonic-gate 			*vpp = NULL;
16477c478bd9Sstevel@tonic-gate 			return (error);
16487c478bd9Sstevel@tonic-gate 		}
16497c478bd9Sstevel@tonic-gate 		vp->v_type = va.va_type;
16507c478bd9Sstevel@tonic-gate 	}
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	*vpp = vp;
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	return (0);
16557c478bd9Sstevel@tonic-gate }
16567c478bd9Sstevel@tonic-gate 
16577c478bd9Sstevel@tonic-gate /* ARGSUSED */
16587c478bd9Sstevel@tonic-gate static int
16597c478bd9Sstevel@tonic-gate nfs_mountroot(vfs_t *vfsp, whymountroot_t why)
16607c478bd9Sstevel@tonic-gate {
16617c478bd9Sstevel@tonic-gate 	vnode_t *rtvp;
16627c478bd9Sstevel@tonic-gate 	char root_hostname[SYS_NMLN+1];
16637c478bd9Sstevel@tonic-gate 	struct servinfo *svp;
16647c478bd9Sstevel@tonic-gate 	int error;
16657c478bd9Sstevel@tonic-gate 	int vfsflags;
16667c478bd9Sstevel@tonic-gate 	size_t size;
16677c478bd9Sstevel@tonic-gate 	char *root_path;
16687c478bd9Sstevel@tonic-gate 	struct pathname pn;
16697c478bd9Sstevel@tonic-gate 	char *name;
16707c478bd9Sstevel@tonic-gate 	cred_t *cr;
16717c478bd9Sstevel@tonic-gate 	struct nfs_args args;		/* nfs mount arguments */
16727c478bd9Sstevel@tonic-gate 	static char token[10];
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 	bzero(&args, sizeof (args));
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	/* do this BEFORE getfile which causes xid stamps to be initialized */
16777c478bd9Sstevel@tonic-gate 	clkset(-1L);		/* hack for now - until we get time svc? */
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	if (why == ROOT_REMOUNT) {
16807c478bd9Sstevel@tonic-gate 		/*
16817c478bd9Sstevel@tonic-gate 		 * Shouldn't happen.
16827c478bd9Sstevel@tonic-gate 		 */
16837c478bd9Sstevel@tonic-gate 		panic("nfs_mountroot: why == ROOT_REMOUNT");
16847c478bd9Sstevel@tonic-gate 	}
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	if (why == ROOT_UNMOUNT) {
16877c478bd9Sstevel@tonic-gate 		/*
16887c478bd9Sstevel@tonic-gate 		 * Nothing to do for NFS.
16897c478bd9Sstevel@tonic-gate 		 */
16907c478bd9Sstevel@tonic-gate 		return (0);
16917c478bd9Sstevel@tonic-gate 	}
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	/*
16947c478bd9Sstevel@tonic-gate 	 * why == ROOT_INIT
16957c478bd9Sstevel@tonic-gate 	 */
16967c478bd9Sstevel@tonic-gate 
16977c478bd9Sstevel@tonic-gate 	name = token;
16987c478bd9Sstevel@tonic-gate 	*name = 0;
16997c478bd9Sstevel@tonic-gate 	getfsname("root", name, sizeof (token));
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	pn_alloc(&pn);
17027c478bd9Sstevel@tonic-gate 	root_path = pn.pn_path;
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
17057c478bd9Sstevel@tonic-gate 	svp->sv_knconf = kmem_zalloc(sizeof (*svp->sv_knconf), KM_SLEEP);
17067c478bd9Sstevel@tonic-gate 	svp->sv_knconf->knc_protofmly = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
17077c478bd9Sstevel@tonic-gate 	svp->sv_knconf->knc_proto = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 	/*
17107c478bd9Sstevel@tonic-gate 	 * Get server address
17117c478bd9Sstevel@tonic-gate 	 * Get the root fhandle
17127c478bd9Sstevel@tonic-gate 	 * Get server's transport
17137c478bd9Sstevel@tonic-gate 	 * Get server's hostname
17147c478bd9Sstevel@tonic-gate 	 * Get options
17157c478bd9Sstevel@tonic-gate 	 */
17167c478bd9Sstevel@tonic-gate 	args.addr = &svp->sv_addr;
17177c478bd9Sstevel@tonic-gate 	args.fh = (char *)&svp->sv_fhandle.fh_buf;
17187c478bd9Sstevel@tonic-gate 	args.knconf = svp->sv_knconf;
17197c478bd9Sstevel@tonic-gate 	args.hostname = root_hostname;
17207c478bd9Sstevel@tonic-gate 	vfsflags = 0;
17217c478bd9Sstevel@tonic-gate 	if (error = mount_root(*name ? name : "root", root_path, NFS_VERSION,
17227c478bd9Sstevel@tonic-gate 	    &args, &vfsflags)) {
17237c478bd9Sstevel@tonic-gate 		nfs_cmn_err(error, CE_WARN,
17247c478bd9Sstevel@tonic-gate 		    "nfs_mountroot: mount_root failed: %m");
17257c478bd9Sstevel@tonic-gate 		sv_free(svp);
17267c478bd9Sstevel@tonic-gate 		pn_free(&pn);
17277c478bd9Sstevel@tonic-gate 		return (error);
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate 	svp->sv_fhandle.fh_len = NFS_FHSIZE;
17307c478bd9Sstevel@tonic-gate 	svp->sv_hostnamelen = (int)(strlen(root_hostname) + 1);
17317c478bd9Sstevel@tonic-gate 	svp->sv_hostname = kmem_alloc(svp->sv_hostnamelen, KM_SLEEP);
17327c478bd9Sstevel@tonic-gate 	(void) strcpy(svp->sv_hostname, root_hostname);
17337c478bd9Sstevel@tonic-gate 
17347c478bd9Sstevel@tonic-gate 	/*
17357c478bd9Sstevel@tonic-gate 	 * Force root partition to always be mounted with AUTH_UNIX for now
17367c478bd9Sstevel@tonic-gate 	 */
17377c478bd9Sstevel@tonic-gate 	svp->sv_secdata = kmem_alloc(sizeof (*svp->sv_secdata), KM_SLEEP);
17387c478bd9Sstevel@tonic-gate 	svp->sv_secdata->secmod = AUTH_UNIX;
17397c478bd9Sstevel@tonic-gate 	svp->sv_secdata->rpcflavor = AUTH_UNIX;
17407c478bd9Sstevel@tonic-gate 	svp->sv_secdata->data = NULL;
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate 	cr = crgetcred();
17437c478bd9Sstevel@tonic-gate 	rtvp = NULL;
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	error = nfsrootvp(&rtvp, vfsp, svp, args.flags, cr, global_zone);
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	crfree(cr);
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	if (error) {
17507c478bd9Sstevel@tonic-gate 		pn_free(&pn);
1751ab7762b6Smaheshvs 		sv_free(svp);
1752ab7762b6Smaheshvs 		return (error);
17537c478bd9Sstevel@tonic-gate 	}
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	error = nfs_setopts(rtvp, DATAMODEL_NATIVE, &args);
17567c478bd9Sstevel@tonic-gate 	if (error) {
17577c478bd9Sstevel@tonic-gate 		nfs_cmn_err(error, CE_WARN,
17587c478bd9Sstevel@tonic-gate 		    "nfs_mountroot: invalid root mount options");
17597c478bd9Sstevel@tonic-gate 		pn_free(&pn);
17607c478bd9Sstevel@tonic-gate 		goto errout;
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	(void) vfs_lock_wait(vfsp);
17647c478bd9Sstevel@tonic-gate 	vfs_add(NULL, vfsp, vfsflags);
17657c478bd9Sstevel@tonic-gate 	vfs_unlock(vfsp);
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	size = strlen(svp->sv_hostname);
17687c478bd9Sstevel@tonic-gate 	(void) strcpy(rootfs.bo_name, svp->sv_hostname);
17697c478bd9Sstevel@tonic-gate 	rootfs.bo_name[size] = ':';
17707c478bd9Sstevel@tonic-gate 	(void) strcpy(&rootfs.bo_name[size + 1], root_path);
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	pn_free(&pn);
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate errout:
17757c478bd9Sstevel@tonic-gate 	if (error) {
17767c478bd9Sstevel@tonic-gate 		sv_free(svp);
17777c478bd9Sstevel@tonic-gate 		nfs_async_stop(vfsp);
17787c478bd9Sstevel@tonic-gate 		nfs_async_manager_stop(vfsp);
17797c478bd9Sstevel@tonic-gate 	}
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	if (rtvp != NULL)
17827c478bd9Sstevel@tonic-gate 		VN_RELE(rtvp);
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	return (error);
17857c478bd9Sstevel@tonic-gate }
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate /*
17887c478bd9Sstevel@tonic-gate  * Initialization routine for VFS routines.  Should only be called once
17897c478bd9Sstevel@tonic-gate  */
17907c478bd9Sstevel@tonic-gate int
17917c478bd9Sstevel@tonic-gate nfs_vfsinit(void)
17927c478bd9Sstevel@tonic-gate {
17937c478bd9Sstevel@tonic-gate 	mutex_init(&nfs_syncbusy, NULL, MUTEX_DEFAULT, NULL);
17947c478bd9Sstevel@tonic-gate 	return (0);
17957c478bd9Sstevel@tonic-gate }
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate void
17987c478bd9Sstevel@tonic-gate nfs_vfsfini(void)
17997c478bd9Sstevel@tonic-gate {
18007c478bd9Sstevel@tonic-gate 	mutex_destroy(&nfs_syncbusy);
18017c478bd9Sstevel@tonic-gate }
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate void
18047c478bd9Sstevel@tonic-gate nfs_freevfs(vfs_t *vfsp)
18057c478bd9Sstevel@tonic-gate {
18067c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
18077c478bd9Sstevel@tonic-gate 	servinfo_t *svp;
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate 	/* free up the resources */
18107c478bd9Sstevel@tonic-gate 	mi = VFTOMI(vfsp);
18117c478bd9Sstevel@tonic-gate 	pathconf_rele(mi);
18127c478bd9Sstevel@tonic-gate 	svp = mi->mi_servers;
18137c478bd9Sstevel@tonic-gate 	mi->mi_servers = mi->mi_curr_serv = NULL;
18147c478bd9Sstevel@tonic-gate 	sv_free(svp);
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	/*
18177c478bd9Sstevel@tonic-gate 	 * By this time we should have already deleted the
18187c478bd9Sstevel@tonic-gate 	 * mi kstats in the unmount code. If they are still around
18197c478bd9Sstevel@tonic-gate 	 * somethings wrong
18207c478bd9Sstevel@tonic-gate 	 */
18217c478bd9Sstevel@tonic-gate 	ASSERT(mi->mi_io_kstats == NULL);
18227c478bd9Sstevel@tonic-gate 	nfs_free_mi(mi);
18237c478bd9Sstevel@tonic-gate }
1824