xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs_vfsops.c (revision e010bda9)
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 /*
22a19609f8Sjv  * 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
nfsinit(int fstyp,char * name)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 },
166aa59c4cbSrsb 		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
nfsfini(void)1927c478bd9Sstevel@tonic-gate nfsfini(void)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
19639d3e169Sevanl static void
nfs_free_args(struct nfs_args * nargs,nfs_fhandle * fh)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
nfs_copyin(char * data,int datalen,struct nfs_args * nargs,nfs_fhandle * fh)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),
308dcedb507SMarcel Telka 	    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),
360dcedb507SMarcel Telka 	    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),
416dcedb507SMarcel Telka 		    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,
435dcedb507SMarcel Telka 		    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 ||
452dcedb507SMarcel Telka 		    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,
459dcedb507SMarcel Telka 			    nfs_ext_u.nfs_extA.secdata) != NULL) {
46039d3e169Sevanl 				error = sec_clnt_loadinfo(
461dcedb507SMarcel Telka 				    (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 =
482dcedb507SMarcel Telka 		    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
nfs_mount(vfs_t * vfsp,vnode_t * mvp,struct mounta * uap,cred_t * cr)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 		}
63439d3e169Sevanl 		return (EINVAL);
63539d3e169Sevanl 	}
63639d3e169Sevanl 
63739d3e169Sevanl 	if ((strlen(args->knconf->knc_protofmly) >= KNC_STRSIZE) ||
638dcedb507SMarcel Telka 	    (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 		}
6437c478bd9Sstevel@tonic-gate 		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;
67039d3e169Sevanl 		goto errout;
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
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,
720dcedb507SMarcel Telka 		    &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 *)
750dcedb507SMarcel Telka 					    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;
83139d3e169Sevanl 				}
8327c478bd9Sstevel@tonic-gate 			}
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 		}
8497c478bd9Sstevel@tonic-gate 
85039d3e169Sevanl 		/*
85139d3e169Sevanl 		 * get time sync address.
85239d3e169Sevanl 		 */
85339d3e169Sevanl 		if (args->syncaddr == NULL) {
85439d3e169Sevanl 			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,
870dcedb507SMarcel Telka 			    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)
956