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 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989  AT&T.
277c478bd9Sstevel@tonic-gate  *	All rights reserved.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/cred.h>
347c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
35aa59c4cbSrsb #include <sys/vfs_opreg.h>
367c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
377c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
397c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
407c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
417c478bd9Sstevel@tonic-gate #include <sys/mount.h>
427c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
437c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
447c478bd9Sstevel@tonic-gate #include <sys/errno.h>
457c478bd9Sstevel@tonic-gate #include <sys/debug.h>
467c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
477c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
487c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
497c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
507c478bd9Sstevel@tonic-gate #include <sys/acl.h>
517c478bd9Sstevel@tonic-gate #include <sys/flock.h>
527c478bd9Sstevel@tonic-gate #include <sys/policy.h>
537c478bd9Sstevel@tonic-gate #include <sys/zone.h>
547c478bd9Sstevel@tonic-gate #include <sys/class.h>
557c478bd9Sstevel@tonic-gate #include <sys/socket.h>
567c478bd9Sstevel@tonic-gate #include <sys/netconfig.h>
5745916cd2Sjpk #include <sys/tsol/tnet.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <rpc/types.h>
607c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
617c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
647c478bd9Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
657c478bd9Sstevel@tonic-gate #include <nfs/rnode.h>
667c478bd9Sstevel@tonic-gate #include <nfs/mount.h>
677c478bd9Sstevel@tonic-gate #include <nfs/nfs_acl.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * From rpcsec module (common/rpcsec).
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate extern int sec_clnt_loadinfo(struct sec_data *, struct sec_data **, model_t);
757c478bd9Sstevel@tonic-gate extern void sec_clnt_freeinfo(struct sec_data *);
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * The order and contents of this structure must be kept in sync with that of
797c478bd9Sstevel@tonic-gate  * rfsreqcnt_v3_tmpl in nfs_stats.c
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate static char *rfsnames_v3[] = {
827c478bd9Sstevel@tonic-gate 	"null", "getattr", "setattr", "lookup", "access", "readlink", "read",
837c478bd9Sstevel@tonic-gate 	"write", "create", "mkdir", "symlink", "mknod", "remove", "rmdir",
847c478bd9Sstevel@tonic-gate 	"rename", "link", "readdir", "readdirplus", "fsstat", "fsinfo",
857c478bd9Sstevel@tonic-gate 	"pathconf", "commit"
867c478bd9Sstevel@tonic-gate };
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * This table maps from NFS protocol number into call type.
907c478bd9Sstevel@tonic-gate  * Zero means a "Lookup" type call
917c478bd9Sstevel@tonic-gate  * One  means a "Read" type call
927c478bd9Sstevel@tonic-gate  * Two  means a "Write" type call
937c478bd9Sstevel@tonic-gate  * This is used to select a default time-out.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate static uchar_t call_type_v3[] = {
967c478bd9Sstevel@tonic-gate 	0, 0, 1, 0, 0, 0, 1,
977c478bd9Sstevel@tonic-gate 	2, 2, 2, 2, 2, 2, 2,
987c478bd9Sstevel@tonic-gate 	2, 2, 1, 2, 0, 0, 0,
997c478bd9Sstevel@tonic-gate 	2 };
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * Similar table, but to determine which timer to use
1037c478bd9Sstevel@tonic-gate  * (only real reads and writes!)
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate static uchar_t timer_type_v3[] = {
1067c478bd9Sstevel@tonic-gate 	0, 0, 0, 0, 0, 0, 1,
1077c478bd9Sstevel@tonic-gate 	2, 0, 0, 0, 0, 0, 0,
1087c478bd9Sstevel@tonic-gate 	0, 0, 1, 1, 0, 0, 0,
1097c478bd9Sstevel@tonic-gate 	0 };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * This table maps from NFS protocol number into a call type
1137c478bd9Sstevel@tonic-gate  * for the semisoft mount option.
1147c478bd9Sstevel@tonic-gate  * Zero means do not repeat operation.
1157c478bd9Sstevel@tonic-gate  * One  means repeat.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate static uchar_t ss_call_type_v3[] = {
1187c478bd9Sstevel@tonic-gate 	0, 0, 1, 0, 0, 0, 0,
1197c478bd9Sstevel@tonic-gate 	1, 1, 1, 1, 1, 1, 1,
1207c478bd9Sstevel@tonic-gate 	1, 1, 0, 0, 0, 0, 0,
1217c478bd9Sstevel@tonic-gate 	1 };
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate  * nfs3 vfs operations.
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate static int	nfs3_mount(vfs_t *, vnode_t *, struct mounta *, cred_t *);
1277c478bd9Sstevel@tonic-gate static int	nfs3_unmount(vfs_t *, int, cred_t *);
1287c478bd9Sstevel@tonic-gate static int	nfs3_root(vfs_t *, vnode_t **);
1297c478bd9Sstevel@tonic-gate static int	nfs3_statvfs(vfs_t *, struct statvfs64 *);
1307c478bd9Sstevel@tonic-gate static int	nfs3_sync(vfs_t *, short, cred_t *);
1317c478bd9Sstevel@tonic-gate static int	nfs3_vget(vfs_t *, vnode_t **, fid_t *);
1327c478bd9Sstevel@tonic-gate static int	nfs3_mountroot(vfs_t *, whymountroot_t);
1337c478bd9Sstevel@tonic-gate static void	nfs3_freevfs(vfs_t *);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static int	nfs3rootvp(vnode_t **, vfs_t *, struct servinfo *,
1367c478bd9Sstevel@tonic-gate 		    int, cred_t *, zone_t *);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*
1397c478bd9Sstevel@tonic-gate  * Initialize the vfs structure
1407c478bd9Sstevel@tonic-gate  */
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate static int nfs3fstyp;
1437c478bd9Sstevel@tonic-gate vfsops_t *nfs3_vfsops;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate /*
1467c478bd9Sstevel@tonic-gate  * Debug variable to check for rdma based
1477c478bd9Sstevel@tonic-gate  * transport startup and cleanup. Controlled
1487c478bd9Sstevel@tonic-gate  * through /etc/system. Off by default.
1497c478bd9Sstevel@tonic-gate  */
1507c478bd9Sstevel@tonic-gate extern int rdma_debug;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate int
nfs3init(int fstyp,char * name)1537c478bd9Sstevel@tonic-gate nfs3init(int fstyp, char *name)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t nfs3_vfsops_template[] = {
156aa59c4cbSrsb 		VFSNAME_MOUNT,		{ .vfs_mount = nfs3_mount },
157aa59c4cbSrsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = nfs3_unmount },
158aa59c4cbSrsb 		VFSNAME_ROOT,		{ .vfs_root = nfs3_root },
159aa59c4cbSrsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = nfs3_statvfs },
160aa59c4cbSrsb 		VFSNAME_SYNC,		{ .vfs_sync = nfs3_sync },
161aa59c4cbSrsb 		VFSNAME_VGET,		{ .vfs_vget = nfs3_vget },
162aa59c4cbSrsb 		VFSNAME_MOUNTROOT,	{ .vfs_mountroot = nfs3_mountroot },
163aa59c4cbSrsb 		VFSNAME_FREEVFS,	{ .vfs_freevfs = nfs3_freevfs },
164aa59c4cbSrsb 		NULL,			NULL
1657c478bd9Sstevel@tonic-gate 	};
1667c478bd9Sstevel@tonic-gate 	int error;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstyp, nfs3_vfsops_template, &nfs3_vfsops);
1697c478bd9Sstevel@tonic-gate 	if (error != 0) {
1707c478bd9Sstevel@tonic-gate 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
1717c478bd9Sstevel@tonic-gate 		    "nfs3init: bad vfs ops template");
1727c478bd9Sstevel@tonic-gate 		return (error);
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	error = vn_make_ops(name, nfs3_vnodeops_template, &nfs3_vnodeops);
1767c478bd9Sstevel@tonic-gate 	if (error != 0) {
1777c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstyp);
1787c478bd9Sstevel@tonic-gate 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
1797c478bd9Sstevel@tonic-gate 		    "nfs3init: bad vnode ops template");
1807c478bd9Sstevel@tonic-gate 		return (error);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	nfs3fstyp = fstyp;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	return (0);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate void
nfs3fini(void)1897c478bd9Sstevel@tonic-gate nfs3fini(void)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
19339d3e169Sevanl static void
nfs3_free_args(struct nfs_args * nargs,nfs_fhandle * fh)19439d3e169Sevanl nfs3_free_args(struct nfs_args *nargs, nfs_fhandle *fh)
19539d3e169Sevanl {
19639d3e169Sevanl 
19739d3e169Sevanl 	if (fh)
19839d3e169Sevanl 		kmem_free(fh, sizeof (*fh));
19939d3e169Sevanl 
20039d3e169Sevanl 	if (nargs->knconf) {
20139d3e169Sevanl 		if (nargs->knconf->knc_protofmly)
202dcedb507SMarcel Telka 			kmem_free(nargs->knconf->knc_protofmly, KNC_STRSIZE);
203dcedb507SMarcel Telka 		if (nargs->knconf->knc_proto)
204dcedb507SMarcel Telka 			kmem_free(nargs->knconf->knc_proto, KNC_STRSIZE);
20539d3e169Sevanl 		kmem_free(nargs->knconf, sizeof (*nargs->knconf));
20639d3e169Sevanl 		nargs->knconf = NULL;
20739d3e169Sevanl 	}
20839d3e169Sevanl 
20939d3e169Sevanl 	if (nargs->fh) {
21039d3e169Sevanl 		kmem_free(nargs->fh, strlen(nargs->fh) + 1);
21139d3e169Sevanl 		nargs->fh = NULL;
21239d3e169Sevanl 	}
21339d3e169Sevanl 
21439d3e169Sevanl 	if (nargs->hostname) {
21539d3e169Sevanl 		kmem_free(nargs->hostname, strlen(nargs->hostname) + 1);
21639d3e169Sevanl 		nargs->hostname = NULL;
21739d3e169Sevanl 	}
21839d3e169Sevanl 
21939d3e169Sevanl 	if (nargs->addr) {
22039d3e169Sevanl 		if (nargs->addr->buf) {
22139d3e169Sevanl 			ASSERT(nargs->addr->len);
22239d3e169Sevanl 			kmem_free(nargs->addr->buf, nargs->addr->len);
22339d3e169Sevanl 		}
22439d3e169Sevanl 		kmem_free(nargs->addr, sizeof (struct netbuf));
22539d3e169Sevanl 		nargs->addr = NULL;
22639d3e169Sevanl 	}
22739d3e169Sevanl 
22839d3e169Sevanl 	if (nargs->syncaddr) {
22939d3e169Sevanl 		ASSERT(nargs->syncaddr->len);
23039d3e169Sevanl 		if (nargs->syncaddr->buf) {
23139d3e169Sevanl 			ASSERT(nargs->syncaddr->len);
23239d3e169Sevanl 			kmem_free(nargs->syncaddr->buf, nargs->syncaddr->len);
23339d3e169Sevanl 		}
23439d3e169Sevanl 		kmem_free(nargs->syncaddr, sizeof (struct netbuf));
23539d3e169Sevanl 		nargs->syncaddr = NULL;
23639d3e169Sevanl 	}
23739d3e169Sevanl 
23839d3e169Sevanl 	if (nargs->netname) {
23939d3e169Sevanl 		kmem_free(nargs->netname, strlen(nargs->netname) + 1);
24039d3e169Sevanl 		nargs->netname = NULL;
24139d3e169Sevanl 	}
24239d3e169Sevanl 
24339d3e169Sevanl 	if (nargs->nfs_ext_u.nfs_extA.secdata) {
24439d3e169Sevanl 		sec_clnt_freeinfo(nargs->nfs_ext_u.nfs_extA.secdata);
24539d3e169Sevanl 		nargs->nfs_ext_u.nfs_extA.secdata = NULL;
24639d3e169Sevanl 	}
24739d3e169Sevanl }
24839d3e169Sevanl 
24939d3e169Sevanl static int
nfs3_copyin(char * data,int datalen,struct nfs_args * nargs,nfs_fhandle * fh)25039d3e169Sevanl nfs3_copyin(char *data, int datalen, struct nfs_args *nargs, nfs_fhandle *fh)
25139d3e169Sevanl {
25239d3e169Sevanl 
25339d3e169Sevanl 	int error;
25439d3e169Sevanl 	size_t nlen;			/* length of netname */
25539d3e169Sevanl 	size_t hlen;			/* length of hostname */
25639d3e169Sevanl 	char netname[MAXNETNAMELEN+1];  /* server's netname */
25739d3e169Sevanl 	struct netbuf addr;		/* server's address */
25839d3e169Sevanl 	struct netbuf syncaddr;		/* AUTH_DES time sync addr */
25939d3e169Sevanl 	struct knetconfig *knconf;	/* transport knetconfig structure */
26039d3e169Sevanl 	struct sec_data *secdata = NULL;	/* security data */
26139d3e169Sevanl 	STRUCT_DECL(nfs_args, args);    	/* nfs mount arguments */
26239d3e169Sevanl 	STRUCT_DECL(knetconfig, knconf_tmp);
26339d3e169Sevanl 	STRUCT_DECL(netbuf, addr_tmp);
26439d3e169Sevanl 	int flags;
26539d3e169Sevanl 	char *p, *pf;
26639d3e169Sevanl 	char *userbufptr;
26739d3e169Sevanl 
26839d3e169Sevanl 
26939d3e169Sevanl 	bzero(nargs, sizeof (*nargs));
27039d3e169Sevanl 
27139d3e169Sevanl 	STRUCT_INIT(args, get_udatamodel());
27239d3e169Sevanl 	bzero(STRUCT_BUF(args), SIZEOF_STRUCT(nfs_args, DATAMODEL_NATIVE));
27339d3e169Sevanl 	if (copyin(data, STRUCT_BUF(args), MIN(datalen, STRUCT_SIZE(args))))
27439d3e169Sevanl 		return (EFAULT);
27539d3e169Sevanl 
27639d3e169Sevanl 	nargs->wsize = STRUCT_FGET(args, wsize);
27739d3e169Sevanl 	nargs->rsize = STRUCT_FGET(args, rsize);
27839d3e169Sevanl 	nargs->timeo = STRUCT_FGET(args, timeo);
27939d3e169Sevanl 	nargs->retrans = STRUCT_FGET(args, retrans);
28039d3e169Sevanl 	nargs->acregmin = STRUCT_FGET(args, acregmin);
28139d3e169Sevanl 	nargs->acregmax = STRUCT_FGET(args, acregmax);
28239d3e169Sevanl 	nargs->acdirmin = STRUCT_FGET(args, acdirmin);
28339d3e169Sevanl 	nargs->acdirmax = STRUCT_FGET(args, acdirmax);
28439d3e169Sevanl 
28539d3e169Sevanl 	flags = STRUCT_FGET(args, flags);
28639d3e169Sevanl 	nargs->flags = flags;
28739d3e169Sevanl 
28839d3e169Sevanl 	addr.buf = NULL;
28939d3e169Sevanl 	syncaddr.buf = NULL;
29039d3e169Sevanl 
29139d3e169Sevanl 	/*
29239d3e169Sevanl 	 * Allocate space for a knetconfig structure and
29339d3e169Sevanl 	 * its strings and copy in from user-land.
29439d3e169Sevanl 	 */
29539d3e169Sevanl 	knconf = kmem_zalloc(sizeof (*knconf), KM_SLEEP);
29639d3e169Sevanl 	STRUCT_INIT(knconf_tmp, get_udatamodel());
29739d3e169Sevanl 	if (copyin(STRUCT_FGETP(args, knconf), STRUCT_BUF(knconf_tmp),
298dcedb507SMarcel Telka 	    STRUCT_SIZE(knconf_tmp))) {
29939d3e169Sevanl 		kmem_free(knconf, sizeof (*knconf));
30039d3e169Sevanl 		return (EFAULT);
30139d3e169Sevanl 	}
30239d3e169Sevanl 
30339d3e169Sevanl 	knconf->knc_semantics = STRUCT_FGET(knconf_tmp, knc_semantics);
30439d3e169Sevanl 	knconf->knc_protofmly = STRUCT_FGETP(knconf_tmp, knc_protofmly);
30539d3e169Sevanl 	knconf->knc_proto = STRUCT_FGETP(knconf_tmp, knc_proto);
30639d3e169Sevanl 	if (get_udatamodel() != DATAMODEL_LP64) {
30739d3e169Sevanl 		knconf->knc_rdev = expldev(STRUCT_FGET(knconf_tmp, knc_rdev));
30839d3e169Sevanl 	} else {
30939d3e169Sevanl 		knconf->knc_rdev = STRUCT_FGET(knconf_tmp, knc_rdev);
31039d3e169Sevanl 	}
31139d3e169Sevanl 
31239d3e169Sevanl 	pf = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
31339d3e169Sevanl 	p = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
31439d3e169Sevanl 	error = copyinstr(knconf->knc_protofmly, pf, KNC_STRSIZE, NULL);
31539d3e169Sevanl 	if (error) {
31639d3e169Sevanl 		kmem_free(pf, KNC_STRSIZE);
31739d3e169Sevanl 		kmem_free(p, KNC_STRSIZE);
31839d3e169Sevanl 		kmem_free(knconf, sizeof (*knconf));
31939d3e169Sevanl 		return (error);
32039d3e169Sevanl 	}
32139d3e169Sevanl 
32239d3e169Sevanl 	error = copyinstr(knconf->knc_proto, p, KNC_STRSIZE, NULL);
32339d3e169Sevanl 	if (error) {
32439d3e169Sevanl 		kmem_free(pf, KNC_STRSIZE);
32539d3e169Sevanl 		kmem_free(p, KNC_STRSIZE);
32639d3e169Sevanl 		kmem_free(knconf, sizeof (*knconf));
32739d3e169Sevanl 		return (error);
32839d3e169Sevanl 	}
32939d3e169Sevanl 
33039d3e169Sevanl 
33139d3e169Sevanl 	knconf->knc_protofmly = pf;
33239d3e169Sevanl 	knconf->knc_proto = p;
33339d3e169Sevanl 
33439d3e169Sevanl 	nargs->knconf = knconf;
33539d3e169Sevanl 	/*
33639d3e169Sevanl 	 * Get server address
33739d3e169Sevanl 	 */
33839d3e169Sevanl 	STRUCT_INIT(addr_tmp, get_udatamodel());
33939d3e169Sevanl 	if (copyin(STRUCT_FGETP(args, addr), STRUCT_BUF(addr_tmp),
340dcedb507SMarcel Telka 	    STRUCT_SIZE(addr_tmp))) {
341dcedb507SMarcel Telka 		error = EFAULT;
342dcedb507SMarcel Telka 		goto errout;
34339d3e169Sevanl 	}
34439d3e169Sevanl 
34539d3e169Sevanl 	nargs->addr = kmem_alloc(sizeof (struct netbuf), KM_SLEEP);
34639d3e169Sevanl 	userbufptr = STRUCT_FGETP(addr_tmp, buf);
34739d3e169Sevanl 	addr.len = STRUCT_FGET(addr_tmp, len);
34839d3e169Sevanl 	addr.buf = kmem_alloc(addr.len, KM_SLEEP);
34939d3e169Sevanl 	addr.maxlen = addr.len;
35039d3e169Sevanl 	if (copyin(userbufptr, addr.buf, addr.len)) {
35139d3e169Sevanl 		kmem_free(addr.buf, addr.len);
35239d3e169Sevanl 		error = EFAULT;
35339d3e169Sevanl 		goto errout;
35439d3e169Sevanl 	}
35539d3e169Sevanl 	bcopy(&addr, nargs->addr, sizeof (struct netbuf));
35639d3e169Sevanl 
35739d3e169Sevanl 	/*
35839d3e169Sevanl 	 * Get the root fhandle
35939d3e169Sevanl 	 */
36039d3e169Sevanl 
36139d3e169Sevanl 	if (copyin(STRUCT_FGETP(args, fh), fh, sizeof (nfs_fhandle))) {
36239d3e169Sevanl 		error = EFAULT;
36339d3e169Sevanl 		goto errout;
36439d3e169Sevanl 	}
36539d3e169Sevanl 
36639d3e169Sevanl 
36739d3e169Sevanl 	/*
36839d3e169Sevanl 	 * Get server's hostname
36939d3e169Sevanl 	 */
37039d3e169Sevanl 	if (flags & NFSMNT_HOSTNAME) {
371dcedb507SMarcel Telka 		error = copyinstr(STRUCT_FGETP(args, hostname), netname,
372dcedb507SMarcel Telka 		    sizeof (netname), &hlen);
37339d3e169Sevanl 	if (error)
37439d3e169Sevanl 		goto errout;
37539d3e169Sevanl 	nargs->hostname = kmem_zalloc(hlen, KM_SLEEP);
37639d3e169Sevanl 	(void) strcpy(nargs->hostname, netname);
37739d3e169Sevanl 	} else {
37839d3e169Sevanl 	nargs->hostname = NULL;
37939d3e169Sevanl 	}
38039d3e169Sevanl 
38139d3e169Sevanl 
38239d3e169Sevanl 	/*
38339d3e169Sevanl 	 * If there are syncaddr and netname data, load them in. This is
38439d3e169Sevanl 	 * to support data needed for NFSV4 when AUTH_DH is the negotiated
38539d3e169Sevanl 	 * flavor via SECINFO. (instead of using MOUNT protocol in V3).
38639d3e169Sevanl 	 */
38739d3e169Sevanl 	netname[0] = '\0';
38839d3e169Sevanl 	if (flags & NFSMNT_SECURE) {
38939d3e169Sevanl 		if (STRUCT_FGETP(args, syncaddr) == NULL) {
39039d3e169Sevanl 			error = EINVAL;
39139d3e169Sevanl 			goto errout;
39239d3e169Sevanl 		}
39339d3e169Sevanl 		/* get syncaddr */
39439d3e169Sevanl 		STRUCT_INIT(addr_tmp, get_udatamodel());
39539d3e169Sevanl 		if (copyin(STRUCT_FGETP(args, syncaddr), STRUCT_BUF(addr_tmp),
396dcedb507SMarcel Telka 		    STRUCT_SIZE(addr_tmp))) {
39739d3e169Sevanl 			error = EINVAL;
39839d3e169Sevanl 			goto errout;
39939d3e169Sevanl 		}
40039d3e169Sevanl 		userbufptr = STRUCT_FGETP(addr_tmp, buf);
40139d3e169Sevanl 		syncaddr.len = STRUCT_FGET(addr_tmp, len);
40239d3e169Sevanl 		syncaddr.buf = kmem_alloc(syncaddr.len, KM_SLEEP);
40339d3e169Sevanl 		syncaddr.maxlen = syncaddr.len;
40439d3e169Sevanl 		if (copyin(userbufptr, syncaddr.buf, syncaddr.len)) {
40539d3e169Sevanl 			kmem_free(syncaddr.buf, syncaddr.len);
40639d3e169Sevanl 			error = EFAULT;
40739d3e169Sevanl 			goto errout;
40839d3e169Sevanl 		}
40939d3e169Sevanl 
41039d3e169Sevanl 		nargs->syncaddr = kmem_alloc(sizeof (struct netbuf), KM_SLEEP);
41139d3e169Sevanl 		bcopy(&syncaddr, nargs->syncaddr, sizeof (struct netbuf));
41239d3e169Sevanl 
41339d3e169Sevanl 		ASSERT(STRUCT_FGETP(args, netname));
41439d3e169Sevanl 
41539d3e169Sevanl 		if (copyinstr(STRUCT_FGETP(args, netname), netname,
416dcedb507SMarcel Telka 		    sizeof (netname), &nlen)) {
41739d3e169Sevanl 			error = EFAULT;
41839d3e169Sevanl 			goto errout;
41939d3e169Sevanl 		}
42039d3e169Sevanl 
42139d3e169Sevanl 		netname[nlen] = '\0';
42239d3e169Sevanl 		nargs->netname = kmem_zalloc(nlen, KM_SLEEP);
42339d3e169Sevanl 		(void) strcpy(nargs->netname, netname);
42439d3e169Sevanl 	}
42539d3e169Sevanl 
42639d3e169Sevanl 	/*
42739d3e169Sevanl 	 * Get the extention data which has the security data structure.
42839d3e169Sevanl 	 * This includes data for AUTH_SYS as well.
42939d3e169Sevanl 	 */
43039d3e169Sevanl 	if (flags & NFSMNT_NEWARGS) {
43139d3e169Sevanl 		nargs->nfs_args_ext = STRUCT_FGET(args, nfs_args_ext);
43239d3e169Sevanl 		if (nargs->nfs_args_ext == NFS_ARGS_EXTA ||
433dcedb507SMarcel Telka 		    nargs->nfs_args_ext == NFS_ARGS_EXTB) {
43439d3e169Sevanl 			/*
43539d3e169Sevanl 			 * Indicating the application is using the new
43639d3e169Sevanl 			 * sec_data structure to pass in the security
43739d3e169Sevanl 			 * data.
43839d3e169Sevanl 			 */
43939d3e169Sevanl 			if (STRUCT_FGETP(args,
440dcedb507SMarcel Telka 			    nfs_ext_u.nfs_extA.secdata) != NULL) {
44139d3e169Sevanl 				error = sec_clnt_loadinfo(
442dcedb507SMarcel Telka 				    (struct sec_data *)STRUCT_FGETP(args,
443dcedb507SMarcel Telka 				    nfs_ext_u.nfs_extA.secdata), &secdata,
444dcedb507SMarcel Telka 				    get_udatamodel());
44539d3e169Sevanl 			}
44639d3e169Sevanl 			nargs->nfs_ext_u.nfs_extA.secdata = secdata;
44739d3e169Sevanl 		}
44839d3e169Sevanl 	}
44939d3e169Sevanl 
45039d3e169Sevanl 	if (error)
45139d3e169Sevanl 		goto errout;
45239d3e169Sevanl 
45339d3e169Sevanl 	/*
45439d3e169Sevanl 	 * Failover support:
45539d3e169Sevanl 	 *
45639d3e169Sevanl 	 * We may have a linked list of nfs_args structures,
45739d3e169Sevanl 	 * which means the user is looking for failover.  If
45839d3e169Sevanl 	 * the mount is either not "read-only" or "soft",
45939d3e169Sevanl 	 * we want to bail out with EINVAL.
46039d3e169Sevanl 	 */
46139d3e169Sevanl 	if (nargs->nfs_args_ext == NFS_ARGS_EXTB)
46239d3e169Sevanl 		nargs->nfs_ext_u.nfs_extB.next =
463dcedb507SMarcel Telka 		    STRUCT_FGETP(args, nfs_ext_u.nfs_extB.next);
46439d3e169Sevanl 
46539d3e169Sevanl errout:
46639d3e169Sevanl 	if (error)
46739d3e169Sevanl 		nfs3_free_args(nargs, fh);
46839d3e169Sevanl 
46939d3e169Sevanl 	return (error);
47039d3e169Sevanl }
47139d3e169Sevanl 
47239d3e169Sevanl 
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate  * nfs mount vfsop
4757c478bd9Sstevel@tonic-gate  * Set up mount info record and attach it to vfs struct.
4767c478bd9Sstevel@tonic-gate  */
4777c478bd9Sstevel@tonic-gate static int
nfs3_mount(vfs_t * vfsp,vnode_t * mvp,struct mounta * uap,cred_t * cr)4787c478bd9Sstevel@tonic-gate nfs3_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
4797c478bd9Sstevel@tonic-gate {
48039d3e169Sevanl 	struct nfs_args	*args = NULL;
48139d3e169Sevanl 	nfs_fhandle	*fhandle = NULL;
4827c478bd9Sstevel@tonic-gate 	char *data = uap->dataptr;
4837c478bd9Sstevel@tonic-gate 	int error;
4847c478bd9Sstevel@tonic-gate 	vnode_t *rtvp;			/* the server's root */
4857c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;			/* mount info, pointed at by vfs */
4867c478bd9Sstevel@tonic-gate 	size_t nlen;			/* length of netname */
4877c478bd9Sstevel@tonic-gate 	struct knetconfig *knconf;	/* transport knetconfig structure */
4887c478bd9Sstevel@tonic-gate 	struct knetconfig *rdma_knconf;	/* rdma transport structure */
4897c478bd9Sstevel@tonic-gate 	rnode_t *rp;
4907c478bd9Sstevel@tonic-gate 	struct servinfo *svp;		/* nfs server info */
4917c478bd9Sstevel@tonic-gate 	struct servinfo *svp_tail = NULL; /* previous nfs server info */
4927c478bd9Sstevel@tonic-gate 	struct servinfo *svp_head;	/* first nfs server info */
4937c478bd9Sstevel@tonic-gate 	struct servinfo *svp_2ndlast;	/* 2nd last in server info list */
4947c478bd9Sstevel@tonic-gate 	struct sec_data *secdata;	/* security data */
4957c478bd9Sstevel@tonic-gate 	int flags, addr_type;
496108322fbScarlsonj 	zone_t *zone = nfs_zone();
49745916cd2Sjpk 	zone_t *mntzone = NULL;
4987c478bd9Sstevel@tonic-gate 
49939d3e169Sevanl 
5007c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
5017c478bd9Sstevel@tonic-gate 		return (EPERM);
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 	if (mvp->v_type != VDIR)
5047c478bd9Sstevel@tonic-gate 		return (ENOTDIR);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	/*
5077c478bd9Sstevel@tonic-gate 	 * get arguments
5087c478bd9Sstevel@tonic-gate 	 *
5097c478bd9Sstevel@tonic-gate 	 * nfs_args is now versioned and is extensible, so
5107c478bd9Sstevel@tonic-gate 	 * uap->datalen might be different from sizeof (args)
5117c478bd9Sstevel@tonic-gate 	 * in a compatible situation.
5127c478bd9Sstevel@tonic-gate 	 */
51339d3e169Sevanl 
5147c478bd9Sstevel@tonic-gate more:
5157c478bd9Sstevel@tonic-gate 
51639d3e169Sevanl 	if (!(uap->flags & MS_SYSSPACE)) {
51739d3e169Sevanl 		if (args == NULL)
51839d3e169Sevanl 			args = kmem_alloc(sizeof (struct nfs_args), KM_SLEEP);
51939d3e169Sevanl 		else {
52039d3e169Sevanl 			nfs3_free_args(args, fhandle);
52139d3e169Sevanl 			fhandle = NULL;
52239d3e169Sevanl 		}
52339d3e169Sevanl 		if (fhandle == NULL)
52439d3e169Sevanl 			fhandle = kmem_alloc(sizeof (nfs_fhandle), KM_SLEEP);
52539d3e169Sevanl 		error = nfs3_copyin(data, uap->datalen, args, fhandle);
52639d3e169Sevanl 		if (error) {
52739d3e169Sevanl 			if (args)
52839d3e169Sevanl 				kmem_free(args, sizeof (*args));
52939d3e169Sevanl 			return (error);
53039d3e169Sevanl 		}
53139d3e169Sevanl 	} else {
53239d3e169Sevanl 		args = (struct nfs_args *)data;
53339d3e169Sevanl 		fhandle = (nfs_fhandle *)args->fh;
53439d3e169Sevanl 	}
53539d3e169Sevanl 
53639d3e169Sevanl 
53739d3e169Sevanl 	flags = args->flags;
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_REMOUNT) {
54039d3e169Sevanl 		size_t	n;
54139d3e169Sevanl 		char	name[FSTYPSZ];
5427c478bd9Sstevel@tonic-gate 
54339d3e169Sevanl 		if (uap->flags & MS_SYSSPACE) {
5447c478bd9Sstevel@tonic-gate 			error = copystr(uap->fstype, name, FSTYPSZ, &n);
54539d3e169Sevanl 		} else {
54639d3e169Sevanl 			nfs3_free_args(args, fhandle);
54739d3e169Sevanl 			kmem_free(args, sizeof (*args));
5487c478bd9Sstevel@tonic-gate 			error = copyinstr(uap->fstype, name, FSTYPSZ, &n);
54939d3e169Sevanl 		}
5507c478bd9Sstevel@tonic-gate 		if (error) {
5517c478bd9Sstevel@tonic-gate 			if (error == ENAMETOOLONG)
5527c478bd9Sstevel@tonic-gate 				return (EINVAL);
5537c478bd9Sstevel@tonic-gate 			return (error);
5547c478bd9Sstevel@tonic-gate 		}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		/*
5577c478bd9Sstevel@tonic-gate 		 * This check is to ensure that the request is a
5587c478bd9Sstevel@tonic-gate 		 * genuine nfs remount request.
5597c478bd9Sstevel@tonic-gate 		 */
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 		if (strncmp(name, "nfs", 3) != 0)
5627c478bd9Sstevel@tonic-gate 			return (EINVAL);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		/*
5657c478bd9Sstevel@tonic-gate 		 * If the request changes the locking type, disallow the
5667c478bd9Sstevel@tonic-gate 		 * remount,
5677c478bd9Sstevel@tonic-gate 		 * because it's questionable whether we can transfer the
5687c478bd9Sstevel@tonic-gate 		 * locking state correctly.
5697c478bd9Sstevel@tonic-gate 		 */
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 		if ((mi = VFTOMI(vfsp)) != NULL) {
5727c478bd9Sstevel@tonic-gate 			uint_t new_mi_llock;
5737c478bd9Sstevel@tonic-gate 			uint_t old_mi_llock;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 			new_mi_llock = (flags & NFSMNT_LLOCK) ? 1 : 0;
5767c478bd9Sstevel@tonic-gate 			old_mi_llock = (mi->mi_flags & MI_LLOCK) ? 1 : 0;
5777c478bd9Sstevel@tonic-gate 			if (old_mi_llock != new_mi_llock)
5787c478bd9Sstevel@tonic-gate 				return (EBUSY);
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 		return (0);
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
5847c478bd9Sstevel@tonic-gate 	if (!(uap->flags & MS_OVERLAY) &&
5857c478bd9Sstevel@tonic-gate 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
5867c478bd9Sstevel@tonic-gate 		mutex_exit(&mvp->v_lock);
58739d3e169Sevanl 		if (!(uap->flags & MS_SYSSPACE)) {
58839d3e169Sevanl 			nfs3_free_args(args, fhandle);
58939d3e169Sevanl 			kmem_free(args, sizeof (*args));
59039d3e169Sevanl 		}
5917c478bd9Sstevel@tonic-gate 		return (EBUSY);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/* make sure things are zeroed for errout: */
5967c478bd9Sstevel@tonic-gate 	rtvp = NULL;
5977c478bd9Sstevel@tonic-gate 	mi = NULL;
5987c478bd9Sstevel@tonic-gate 	secdata = NULL;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/*
6017c478bd9Sstevel@tonic-gate 	 * A valid knetconfig structure is required.
6027c478bd9Sstevel@tonic-gate 	 */
60339d3e169Sevanl 	if (!(flags & NFSMNT_KNCONF)) {
60439d3e169Sevanl 		if (!(uap->flags & MS_SYSSPACE)) {
60539d3e169Sevanl 			nfs3_free_args(args, fhandle);
60639d3e169Sevanl 			kmem_free(args, sizeof (*args));
60739d3e169Sevanl 		}
6087c478bd9Sstevel@tonic-gate 		return (EINVAL);
60939d3e169Sevanl 	}
61039d3e169Sevanl 
61139d3e169Sevanl 	if ((strlen(args->knconf->knc_protofmly) >= KNC_STRSIZE) ||
612dcedb507SMarcel Telka 	    (strlen(args->knconf->knc_proto) >= KNC_STRSIZE)) {
61339d3e169Sevanl 		if (!(uap->flags & MS_SYSSPACE)) {
61439d3e169Sevanl 			nfs3_free_args(args, fhandle);
61539d3e169Sevanl 			kmem_free(args, sizeof (*args));
61639d3e169Sevanl 		}
61739d3e169Sevanl 		return (EINVAL);
61839d3e169Sevanl 	}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	/*
6217c478bd9Sstevel@tonic-gate 	 * Allocate a servinfo struct.
6227c478bd9Sstevel@tonic-gate 	 */
6237c478bd9Sstevel@tonic-gate 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
6247c478bd9Sstevel@tonic-gate 	mutex_init(&svp->sv_lock, NULL, MUTEX_DEFAULT, NULL);
6257c478bd9Sstevel@tonic-gate 	if (svp_tail) {
6267c478bd9Sstevel@tonic-gate 		svp_2ndlast = svp_tail;
6277c478bd9Sstevel@tonic-gate 		svp_tail->sv_next = svp;
6287c478bd9Sstevel@tonic-gate 	} else {
6297c478bd9Sstevel@tonic-gate 		svp_head = svp;
6307c478bd9Sstevel@tonic-gate 		svp_2ndlast = svp;
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	svp_tail = svp;
6347c478bd9Sstevel@tonic-gate 
63539d3e169Sevanl 	svp->sv_knconf = args->knconf;
63639d3e169Sevanl 	args->knconf = NULL;
6377c478bd9Sstevel@tonic-gate 
63839d3e169Sevanl 	if (args->addr == NULL || args->addr->buf == NULL) {
63939d3e169Sevanl 		error = EINVAL;
6407c478bd9Sstevel@tonic-gate 		goto errout;
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate 
64339d3e169Sevanl 	svp->sv_addr.maxlen = args->addr->maxlen;
64439d3e169Sevanl 	svp->sv_addr.len = args->addr->len;
64539d3e169Sevanl 	svp->sv_addr.buf = args->addr->buf;
64639d3e169Sevanl 	args->addr->buf = NULL;
64739d3e169Sevanl 
6487c478bd9Sstevel@tonic-gate 	/*
6497c478bd9Sstevel@tonic-gate 	 * Check the root fhandle length
6507c478bd9Sstevel@tonic-gate 	 */
65139d3e169Sevanl 	ASSERT(fhandle);
65239d3e169Sevanl 	if (fhandle->fh_len > NFS3_FHSIZE || fhandle->fh_len == 0) {
6537c478bd9Sstevel@tonic-gate 		error = EINVAL;
6547c478bd9Sstevel@tonic-gate #ifdef DEBUG
6557c478bd9Sstevel@tonic-gate 		zcmn_err(getzoneid(), CE_WARN,
6567c478bd9Sstevel@tonic-gate 		    "nfs3_mount: got an invalid fhandle. fh_len = %d",
65739d3e169Sevanl 		    fhandle->fh_len);
65839d3e169Sevanl 		fhandle->fh_len = NFS_FHANDLE_LEN;
65939d3e169Sevanl 		nfs_printfhandle(fhandle);
6607c478bd9Sstevel@tonic-gate #endif
6617c478bd9Sstevel@tonic-gate 		goto errout;
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 
66439d3e169Sevanl 	bcopy(&fhandle->fh_buf, &svp->sv_fhandle.fh_buf, fhandle->fh_len);
66539d3e169Sevanl 	svp->sv_fhandle.fh_len = fhandle->fh_len;
66639d3e169Sevanl 
6677c478bd9Sstevel@tonic-gate 	/*
6687c478bd9Sstevel@tonic-gate 	 * Get server's hostname
6697c478bd9Sstevel@tonic-gate 	 */
6707c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_HOSTNAME) {
67139d3e169Sevanl 		if (args->hostname == NULL) {
67239d3e169Sevanl 			error = EINVAL;
6737c478bd9Sstevel@tonic-gate 			goto errout;
67439d3e169Sevanl 		}
67539d3e169Sevanl 		svp->sv_hostnamelen = strlen(args->hostname) + 1;
67639d3e169Sevanl 		svp->sv_hostname = args->hostname;
67739d3e169Sevanl 		args->hostname = NULL;
6787c478bd9Sstevel@tonic-gate 	} else {
6797c478bd9Sstevel@tonic-gate 		char *p = "unknown-host";
68039d3e169Sevanl 		svp->sv_hostnamelen = strlen(p) + 1;
68139d3e169Sevanl 		svp->sv_hostname = kmem_zalloc(svp->sv_hostnamelen, KM_SLEEP);
68239d3e169Sevanl 		(void) strcpy(svp->sv_hostname, p);
6837c478bd9Sstevel@tonic-gate 	}
68439d3e169Sevanl 
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	/*
6877c478bd9Sstevel@tonic-gate 	 * RDMA MOUNT SUPPORT FOR NFS v3:
6887c478bd9Sstevel@tonic-gate 	 * Establish, is it possible to use RDMA, if so overload the
6897c478bd9Sstevel@tonic-gate 	 * knconf with rdma specific knconf and free the orignal.
6907c478bd9Sstevel@tonic-gate 	 */
6917c478bd9Sstevel@tonic-gate 	if ((flags & NFSMNT_TRYRDMA) || (flags & NFSMNT_DORDMA)) {
6927c478bd9Sstevel@tonic-gate 		/*
6937c478bd9Sstevel@tonic-gate 		 * Determine the addr type for RDMA, IPv4 or v6.
6947c478bd9Sstevel@tonic-gate 		 */
6957c478bd9Sstevel@tonic-gate 		if (strcmp(svp->sv_knconf->knc_protofmly, NC_INET) == 0)
6967c478bd9Sstevel@tonic-gate 			addr_type = AF_INET;
6977c478bd9Sstevel@tonic-gate 		else if (strcmp(svp->sv_knconf->knc_protofmly, NC_INET6) == 0)
6987c478bd9Sstevel@tonic-gate 			addr_type = AF_INET6;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		if (rdma_reachable(addr_type, &svp->sv_addr,
701dcedb507SMarcel Telka 		    &rdma_knconf) == 0) {
7027c478bd9Sstevel@tonic-gate 			/*
7037c478bd9Sstevel@tonic-gate 			 * If successful, hijack the orignal knconf and
7047c478bd9Sstevel@tonic-gate 			 * replace with a new one, depending on the flags.
7057c478bd9Sstevel@tonic-gate 			 */
7067c478bd9Sstevel@tonic-gate 			svp->sv_origknconf = svp->sv_knconf;
7077c478bd9Sstevel@tonic-gate 			svp->sv_knconf = rdma_knconf;
7087c478bd9Sstevel@tonic-gate 			knconf = rdma_knconf;
7097c478bd9Sstevel@tonic-gate 		} else {
7107c478bd9Sstevel@tonic-gate 			if (flags & NFSMNT_TRYRDMA) {
7117c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7127c478bd9Sstevel@tonic-gate 				if (rdma_debug)
7137c478bd9Sstevel@tonic-gate 					zcmn_err(getzoneid(), CE_WARN,
7147c478bd9Sstevel@tonic-gate 					    "no RDMA onboard, revert\n");
7157c478bd9Sstevel@tonic-gate #endif
7167c478bd9Sstevel@tonic-gate 			}
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 			if (flags & NFSMNT_DORDMA) {
7197c478bd9Sstevel@tonic-gate 				/*
7207c478bd9Sstevel@tonic-gate 				 * If proto=rdma is specified and no RDMA
7217c478bd9Sstevel@tonic-gate 				 * path to this server is avialable then
7227c478bd9Sstevel@tonic-gate 				 * ditch this server.
7237c478bd9Sstevel@tonic-gate 				 * This is not included in the mountable
7247c478bd9Sstevel@tonic-gate 				 * server list or the replica list.
7257c478bd9Sstevel@tonic-gate 				 * Check if more servers are specified;
7267c478bd9Sstevel@tonic-gate 				 * Failover case, otherwise bail out of mount.
7277c478bd9Sstevel@tonic-gate 				 */
728dcedb507SMarcel Telka 				if (args->nfs_args_ext == NFS_ARGS_EXTB &&
729dcedb507SMarcel Telka 				    args->nfs_ext_u.nfs_extB.next != NULL) {
73039d3e169Sevanl 					data = (char *)
731dcedb507SMarcel Telka 					    args->nfs_ext_u.nfs_extB.next;
7327c478bd9Sstevel@tonic-gate 					if (uap->flags & MS_RDONLY &&
7337c478bd9Sstevel@tonic-gate 					    !(flags & NFSMNT_SOFT)) {
7347c478bd9Sstevel@tonic-gate 						if (svp_head->sv_next == NULL) {
7357c478bd9Sstevel@tonic-gate 							svp_tail = NULL;
7367c478bd9Sstevel@tonic-gate 							svp_2ndlast = NULL;
7377c478bd9Sstevel@tonic-gate 							sv_free(svp_head);
7387c478bd9Sstevel@tonic-gate 							goto more;
7397c478bd9Sstevel@tonic-gate 						} else {
7407c478bd9Sstevel@tonic-gate 							svp_tail = svp_2ndlast;
7417c478bd9Sstevel@tonic-gate 							svp_2ndlast->sv_next =
7427c478bd9Sstevel@tonic-gate 							    NULL;
7437c478bd9Sstevel@tonic-gate 							sv_free(svp);
7447c478bd9Sstevel@tonic-gate 							goto more;
7457c478bd9Sstevel@tonic-gate 						}
7467c478bd9Sstevel@tonic-gate 					}
7477c478bd9Sstevel@tonic-gate 				} else {
7487c478bd9Sstevel@tonic-gate 					/*
7497c478bd9Sstevel@tonic-gate 					 * This is the last server specified
7507c478bd9Sstevel@tonic-gate 					 * in the nfs_args list passed down
7517c478bd9Sstevel@tonic-gate 					 * and its not rdma capable.
7527c478bd9Sstevel@tonic-gate 					 */
7537c478bd9Sstevel@tonic-gate 					if (svp_head->sv_next == NULL) {
7547c478bd9Sstevel@tonic-gate 						/*
7557c478bd9Sstevel@tonic-gate 						 * Is this the only one
7567c478bd9Sstevel@tonic-gate 						 */
7577c478bd9Sstevel@tonic-gate 						error = EINVAL;
7587c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7597c478bd9Sstevel@tonic-gate 						if (rdma_debug)
7607c478bd9Sstevel@tonic-gate 							zcmn_err(getzoneid(),
7617c478bd9Sstevel@tonic-gate 							    CE_WARN,
7627c478bd9Sstevel@tonic-gate 							    "No RDMA srv");
7637c478bd9Sstevel@tonic-gate #endif
7647c478bd9Sstevel@tonic-gate 						goto errout;
7657c478bd9Sstevel@tonic-gate 					} else {
7667c478bd9Sstevel@tonic-gate 						/*
7677c478bd9Sstevel@tonic-gate 						 * There is list, since some
7687c478bd9Sstevel@tonic-gate 						 * servers specified before
7697c478bd9Sstevel@tonic-gate 						 * this passed all requirements
7707c478bd9Sstevel@tonic-gate 						 */
7717c478bd9Sstevel@tonic-gate 						svp_tail = svp_2ndlast;
7727c478bd9Sstevel@tonic-gate 						svp_2ndlast->sv_next = NULL;
7737c478bd9Sstevel@tonic-gate 						sv_free(svp);
7747c478bd9Sstevel@tonic-gate 						goto proceed;
7757c478bd9Sstevel@tonic-gate 					}
7767c478bd9Sstevel@tonic-gate 				}
7777c478bd9Sstevel@tonic-gate 			}
7787c478bd9Sstevel@tonic-gate 		}
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	/*
7827c478bd9Sstevel@tonic-gate 	 * Get the extention data which has the new security data structure.
7837c478bd9Sstevel@tonic-gate 	 */
7847c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_NEWARGS) {
78539d3e169Sevanl 		switch (args->nfs_args_ext) {
7867c478bd9Sstevel@tonic-gate 		case NFS_ARGS_EXTA:
7877c478bd9Sstevel@tonic-gate 		case NFS_ARGS_EXTB:
7887c478bd9Sstevel@tonic-gate 			/*
7897c478bd9Sstevel@tonic-gate 			 * Indicating the application is using the new
7907c478bd9Sstevel@tonic-gate 			 * sec_data structure to pass in the security
7917c478bd9Sstevel@tonic-gate 			 * data.
7927c478bd9Sstevel@tonic-gate 			 */
79339d3e169Sevanl 			secdata = args->nfs_ext_u.nfs_extA.secdata;
79439d3e169Sevanl 			if (args->nfs_ext_u.nfs_extA.secdata == NULL) {
7957c478bd9Sstevel@tonic-gate 				error = EINVAL;
7967c478bd9Sstevel@tonic-gate 			} else {
79739d3e169Sevanl 				/*
79839d3e169Sevanl 				 * Need to validate the flavor here if
79939d3e169Sevanl 				 * sysspace, userspace was already
80039d3e169Sevanl 				 * validate from the nfs_copyin function.
80139d3e169Sevanl 				 */
80239d3e169Sevanl 				switch (secdata->rpcflavor) {
80339d3e169Sevanl 				case AUTH_NONE:
80439d3e169Sevanl 				case AUTH_UNIX:
80539d3e169Sevanl 				case AUTH_LOOPBACK:
80639d3e169Sevanl 				case AUTH_DES:
80739d3e169Sevanl 				case RPCSEC_GSS:
808dcedb507SMarcel Telka 					args->nfs_ext_u.nfs_extA.secdata = NULL;
80939d3e169Sevanl 					break;
81039d3e169Sevanl 				default:
81139d3e169Sevanl 					error = EINVAL;
81239d3e169Sevanl 					goto errout;
81339d3e169Sevanl 				}
8147c478bd9Sstevel@tonic-gate 			}
8157c478bd9Sstevel@tonic-gate 			break;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 		default:
8187c478bd9Sstevel@tonic-gate 			error = EINVAL;
8197c478bd9Sstevel@tonic-gate 			break;
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 	} else if (flags & NFSMNT_SECURE) {
8227c478bd9Sstevel@tonic-gate 		/*
8237c478bd9Sstevel@tonic-gate 		 * Keep this for backward compatibility to support
8247c478bd9Sstevel@tonic-gate 		 * NFSMNT_SECURE/NFSMNT_RPCTIMESYNC flags.
8257c478bd9Sstevel@tonic-gate 		 */
82639d3e169Sevanl 		if (args->syncaddr == NULL || args->syncaddr->buf == NULL) {
8277c478bd9Sstevel@tonic-gate 			error = EINVAL;
82839d3e169Sevanl 			goto errout;
8297c478bd9Sstevel@tonic-gate 		}
8307c478bd9Sstevel@tonic-gate 		/*
8317c478bd9Sstevel@tonic-gate 		 * Move security related data to the sec_data structure.
8327c478bd9Sstevel@tonic-gate 		 */
83339d3e169Sevanl 		{
8347c478bd9Sstevel@tonic-gate 			dh_k4_clntdata_t *data;
8357c478bd9Sstevel@tonic-gate 			char *pf, *p;
8367c478bd9Sstevel@tonic-gate 			secdata = kmem_alloc(sizeof (*secdata), KM_SLEEP);
8377c478bd9Sstevel@tonic-gate 			if (flags & NFSMNT_RPCTIMESYNC)
8387c478bd9Sstevel@tonic-gate 				secdata->flags |= AUTH_F_RPCTIMESYNC;
8397c478bd9Sstevel@tonic-gate 			data = kmem_alloc(sizeof (*data), KM_SLEEP);
84039d3e169Sevanl 			bcopy(args->syncaddr, &data->syncaddr,
841dcedb507SMarcel Telka 			    sizeof (*args->syncaddr));
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 			/*
8447c478bd9Sstevel@tonic-gate 			 * duplicate the knconf information for the
8457c478bd9Sstevel@tonic-gate 			 * new opaque data.
8467c478bd9Sstevel@tonic-gate 			 */
8477c478bd9Sstevel@tonic-gate 			data->knconf = kmem_alloc(sizeof (*knconf), KM_SLEEP);
8487c478bd9Sstevel@tonic-gate 			*data->knconf = *knconf;
8497c478bd9Sstevel@tonic-gate 			pf = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
8507c478bd9Sstevel@tonic-gate 			p = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
8517c478bd9Sstevel@tonic-gate 			bcopy(knconf->knc_protofmly, pf, KNC_STRSIZE);
8527c478bd9Sstevel@tonic-gate 			bcopy(knconf->knc_proto, pf, KNC_STRSIZE);
8537c478bd9Sstevel@tonic-gate 			data->knconf->knc_protofmly = pf;
8547c478bd9Sstevel@tonic-gate 			data->knconf->knc_proto = p;
8557c478bd9Sstevel@tonic-gate 
85639d3e169Sevanl 			nlen = strlen(args->hostname) + 1;
8577c478bd9Sstevel@tonic-gate 			/* move server netname to the sec_data structure */
8587c478bd9Sstevel@tonic-gate 			if (nlen != 0) {
8597c478bd9Sstevel@tonic-gate 				data->netname = kmem_alloc(nlen, KM_SLEEP);
86039d3e169Sevanl 				bcopy(args->hostname, data->netname, nlen);
86139d3e169Sevanl 				data->netnamelen = nlen;
8627c478bd9Sstevel@tonic-gate 			}
8637c478bd9Sstevel@tonic-gate 			secdata->secmod = secdata->rpcflavor = AUTH_DES;
8647c478bd9Sstevel@tonic-gate 			secdata->data = (caddr_t)data;
8657c478bd9Sstevel@tonic-gate 		}
86639d3e169Sevanl 	} else 	{
8677c478bd9Sstevel@tonic-gate 		secdata = kmem_alloc(sizeof (*secdata), KM_SLEEP);
8687c478bd9Sstevel@tonic-gate 		secdata->secmod = secdata->rpcflavor = AUTH_UNIX;
8697c478bd9Sstevel@tonic-gate 		secdata->data = NULL;
8707c478bd9Sstevel@tonic-gate 	}
87139d3e169Sevanl 
8727c478bd9Sstevel@tonic-gate 	svp->sv_secdata = secdata;
8737c478bd9Sstevel@tonic-gate 	if (error)
8747c478bd9Sstevel@tonic-gate 		goto errout;
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 	/*
8777c478bd9Sstevel@tonic-gate 	 * See bug 1180236.
8787c478bd9Sstevel@tonic-gate 	 * If mount secure failed, we will fall back to AUTH_NONE
8797c478bd9Sstevel@tonic-gate 	 * and try again.  nfs3rootvp() will turn this back off.
8807c478bd9Sstevel@tonic-gate 	 *
8817c478bd9Sstevel@tonic-gate 	 * The NFS Version 3 mount uses the FSINFO and GETATTR
8827c478bd9Sstevel@tonic-gate 	 * procedures.  The server should not care if these procedures
8837c478bd9Sstevel@tonic-gate 	 * have the proper security flavor, so if mount retries using
8847c478bd9Sstevel@tonic-gate 	 * AUTH_NONE that does not require a credential setup for root
8857c478bd9Sstevel@tonic-gate 	 * then the automounter would work without requiring root to be
8867c478bd9Sstevel@tonic-gate 	 * keylogged into AUTH_DES.
8877c478bd9Sstevel@tonic-gate 	 */
8887c478bd9Sstevel@tonic-gate 	if (secdata->rpcflavor != AUTH_UNIX &&
8897c478bd9Sstevel@tonic-gate 	    secdata->rpcflavor != AUTH_LOOPBACK)
8907c478bd9Sstevel@tonic-gate 		secdata->flags |= AUTH_F_TRYNONE;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	/*
8937c478bd9Sstevel@tonic-gate 	 * Failover support:
8947c478bd9Sstevel@tonic-gate 	 *
8957c478bd9Sstevel@tonic-gate 	 * We may have a linked list of nfs_args structures,
8967c478bd9Sstevel@tonic-gate 	 * which means the user is looking for failover.  If
8977c478bd9Sstevel@tonic-gate 	 * the mount is either not "read-only" or "soft",
8987c478bd9Sstevel@tonic-gate 	 * we want to bail out with EINVAL.
8997c478bd9Sstevel@tonic-gate 	 */
90039d3e169Sevanl 	if (args->nfs_args_ext == NFS_ARGS_EXTB &&
90139d3e169Sevanl 	    args->nfs_ext_u.nfs_extB.next != NULL) {
9027c478bd9Sstevel@tonic-gate 		if (uap->flags & MS_RDONLY && !(flags & NFSMNT_SOFT)) {
90339d3e169Sevanl 			data = (char *)args->nfs_ext_u.nfs_extB.next;
9047c478bd9Sstevel@tonic-gate 			goto more;
9057c478bd9Sstevel@tonic-gate 		}
9067c478bd9Sstevel@tonic-gate 		error = EINVAL;
9077c478bd9Sstevel@tonic-gate 		goto errout;
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	/*
9117c478bd9Sstevel@tonic-gate 	 * Determine the zone we're being mounted into.
9127c478bd9Sstevel@tonic-gate 	 */
91345916cd2Sjpk 	zone_hold(mntzone = zone);		/* start with this assumption */
9147c478bd9Sstevel@tonic-gate 	if (getzoneid() == GLOBAL_ZONEID) {
91545916cd2Sjpk 		zone_rele(mntzone);
9167c478bd9Sstevel@tonic-gate 		mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
9177c478bd9Sstevel@tonic-gate 		ASSERT(mntzone != NULL);
9187c478bd9Sstevel@tonic-gate 		if (mntzone != zone) {
9197c478bd9Sstevel@tonic-gate 			error = EBUSY;
9207c478bd9Sstevel@tonic-gate 			goto errout;
9217c478bd9Sstevel@tonic-gate 		}
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 
92445916cd2Sjpk 	if (is_system_labeled()) {
92545916cd2Sjpk 		error = nfs_mount_label_policy(vfsp, &svp->sv_addr,
92645916cd2Sjpk 		    svp->sv_knconf, cr);
92745916cd2Sjpk 
92845916cd2Sjpk 		if (error > 0)
92945916cd2Sjpk 			goto errout;
93045916cd2Sjpk 
93145916cd2Sjpk 		if (error == -1) {
93245916cd2Sjpk 			/* change mount to read-only to prevent write-down */
93345916cd2Sjpk 			vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
93445916cd2Sjpk 		}
93545916cd2Sjpk 	}
93645916cd2Sjpk 
9377c478bd9Sstevel@tonic-gate 	/*
9387c478bd9Sstevel@tonic-gate 	 * Stop the mount from going any further if the zone is going away.
9397c478bd9Sstevel@tonic-gate 	 */
94045916cd2Sjpk 	if (zone_status_get(mntzone) >= ZONE_IS_SHUTTING_DOWN) {
9417c478bd9Sstevel@tonic-gate 		error = EBUSY;
9427c478bd9Sstevel@tonic-gate 		goto errout;
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	/*
9467c478bd9Sstevel@tonic-gate 	 * Get root vnode.
9477c478bd9Sstevel@tonic-gate 	 */
9487c478bd9Sstevel@tonic-gate proceed:
94945916cd2Sjpk 	error = nfs3rootvp(&rtvp, vfsp, svp_head, flags, cr, mntzone);
9507c478bd9Sstevel@tonic-gate 
951