xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs_common.c (revision 89b43686)
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
55a59a8b3Srsb  * Common Development and Distribution License (the "License").
65a59a8b3Srsb  * 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 /*
220fbb751dSJohn Levon  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
23*89b43686SBayard Bell  * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  *	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
287c478bd9Sstevel@tonic-gate  *		All rights reserved.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/errno.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/user.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/time.h>
377c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
387c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
39aa59c4cbSrsb #include <sys/vfs_opreg.h>
407c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
417c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
427c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
437c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
447c478bd9Sstevel@tonic-gate #include <rpc/types.h>
457c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
467c478bd9Sstevel@tonic-gate #include <nfs/nfs4.h>
477c478bd9Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
487c478bd9Sstevel@tonic-gate #include <nfs/rnode.h>
497c478bd9Sstevel@tonic-gate #include <nfs/mount.h>
507c478bd9Sstevel@tonic-gate #include <nfs/nfssys.h>
517c478bd9Sstevel@tonic-gate #include <sys/debug.h>
527c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
537c478bd9Sstevel@tonic-gate #include <sys/file.h>
547c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
557c478bd9Sstevel@tonic-gate #include <sys/zone.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * This is the loadable module wrapper.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate #include <sys/systm.h>
617c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
627c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
637c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include <rpc/types.h>
667c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
677c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
687c478bd9Sstevel@tonic-gate #include <rpc/svc.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
71da6c28aaSamw  * The pseudo NFS filesystem to allow diskless booting to dynamically
727c478bd9Sstevel@tonic-gate  * mount either a NFS V2, NFS V3, or NFS V4 filesystem.  This only implements
737c478bd9Sstevel@tonic-gate  * the VFS_MOUNTROOT op and is only intended to be used by the
747c478bd9Sstevel@tonic-gate  * diskless booting code until the real root filesystem is mounted.
757c478bd9Sstevel@tonic-gate  * Nothing else should ever call this!
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * The strategy is that if the initial rootfs type is set to "nfsdyn"
787c478bd9Sstevel@tonic-gate  * by loadrootmodules() this filesystem is called to mount the
797c478bd9Sstevel@tonic-gate  * root filesystem.  It first attempts to mount a V4 filesystem, and if that
807c478bd9Sstevel@tonic-gate  * fails due to an RPC version mismatch it tries V3 and finally V2.
817c478bd9Sstevel@tonic-gate  * Once the real mount succeeds the vfsops and rootfs name are changed
827c478bd9Sstevel@tonic-gate  * to reflect the real filesystem type.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate static int nfsdyninit(int, char *);
857c478bd9Sstevel@tonic-gate static int nfsdyn_mountroot(vfs_t *, whymountroot_t);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate vfsops_t *nfsdyn_vfsops;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * The following data structures are used to configure the NFS
917c478bd9Sstevel@tonic-gate  * system call, the NFS Version 2 client VFS, and the NFS Version
927c478bd9Sstevel@tonic-gate  * 3 client VFS into the system.  The NFS Version 4 structures are defined in
937c478bd9Sstevel@tonic-gate  * nfs4_common.c
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * The NFS system call.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate static struct sysent nfssysent = {
1007c478bd9Sstevel@tonic-gate 	2,
1017c478bd9Sstevel@tonic-gate 	SE_32RVAL1 | SE_ARGC | SE_NOUNLOAD,
1027c478bd9Sstevel@tonic-gate 	nfssys
1037c478bd9Sstevel@tonic-gate };
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static struct modlsys modlsys = {
1067c478bd9Sstevel@tonic-gate 	&mod_syscallops,
1077c478bd9Sstevel@tonic-gate 	"NFS syscall, client, and common",
1087c478bd9Sstevel@tonic-gate 	&nfssysent
1097c478bd9Sstevel@tonic-gate };
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1127c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = {
1137c478bd9Sstevel@tonic-gate 	&mod_syscallops32,
1147c478bd9Sstevel@tonic-gate 	"NFS syscall, client, and common (32-bit)",
1157c478bd9Sstevel@tonic-gate 	&nfssysent
1167c478bd9Sstevel@tonic-gate };
1177c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * The NFS Dynamic client VFS.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate static vfsdef_t vfw = {
1237c478bd9Sstevel@tonic-gate 	VFSDEF_VERSION,
1247c478bd9Sstevel@tonic-gate 	"nfsdyn",
1257c478bd9Sstevel@tonic-gate 	nfsdyninit,
1260fbb751dSJohn Levon 	VSW_ZMOUNT,
1277c478bd9Sstevel@tonic-gate 	NULL
1287c478bd9Sstevel@tonic-gate };
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static struct modlfs modlfs = {
1317c478bd9Sstevel@tonic-gate 	&mod_fsops,
1327c478bd9Sstevel@tonic-gate 	"network filesystem",
1337c478bd9Sstevel@tonic-gate 	&vfw
1347c478bd9Sstevel@tonic-gate };
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * The NFS Version 2 client VFS.
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate static vfsdef_t vfw2 = {
1407c478bd9Sstevel@tonic-gate 	VFSDEF_VERSION,
1417c478bd9Sstevel@tonic-gate 	"nfs",
1427c478bd9Sstevel@tonic-gate 	nfsinit,
1430fbb751dSJohn Levon 	VSW_CANREMOUNT|VSW_NOTZONESAFE|VSW_STATS|VSW_ZMOUNT,
1447c478bd9Sstevel@tonic-gate 	NULL
1457c478bd9Sstevel@tonic-gate };
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate static struct modlfs modlfs2 = {
1487c478bd9Sstevel@tonic-gate 	&mod_fsops,
1497c478bd9Sstevel@tonic-gate 	"network filesystem version 2",
1507c478bd9Sstevel@tonic-gate 	&vfw2
1517c478bd9Sstevel@tonic-gate };
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * The NFS Version 3 client VFS.
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate static vfsdef_t vfw3 = {
1577c478bd9Sstevel@tonic-gate 	VFSDEF_VERSION,
1587c478bd9Sstevel@tonic-gate 	"nfs3",
1597c478bd9Sstevel@tonic-gate 	nfs3init,
1600fbb751dSJohn Levon 	VSW_CANREMOUNT|VSW_NOTZONESAFE|VSW_STATS|VSW_ZMOUNT,
1617c478bd9Sstevel@tonic-gate 	NULL
1627c478bd9Sstevel@tonic-gate };
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate static struct modlfs modlfs3 = {
1657c478bd9Sstevel@tonic-gate 	&mod_fsops,
1667c478bd9Sstevel@tonic-gate 	"network filesystem version 3",
1677c478bd9Sstevel@tonic-gate 	&vfw3
1687c478bd9Sstevel@tonic-gate };
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate extern struct modlfs modlfs4;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * We have too many linkage structures so we define our own XXX
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate struct modlinkage_big {
1767c478bd9Sstevel@tonic-gate 	int		ml_rev;		/* rev of loadable modules system */
1777c478bd9Sstevel@tonic-gate 	void		*ml_linkage[7];	/* NULL terminated list of */
1787c478bd9Sstevel@tonic-gate 					/* linkage structures */
1797c478bd9Sstevel@tonic-gate };
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * All of the module configuration linkages required to configure
1837c478bd9Sstevel@tonic-gate  * the system call and client VFS's into the system.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate static struct modlinkage_big modlinkage = {
1867c478bd9Sstevel@tonic-gate 	MODREV_1,
1877c478bd9Sstevel@tonic-gate 	&modlsys,
1887c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1897c478bd9Sstevel@tonic-gate 	&modlsys32,
1907c478bd9Sstevel@tonic-gate #endif
1917c478bd9Sstevel@tonic-gate 	&modlfs,
1927c478bd9Sstevel@tonic-gate 	&modlfs2,
1937c478bd9Sstevel@tonic-gate 	&modlfs3,
1947c478bd9Sstevel@tonic-gate 	&modlfs4,
1957c478bd9Sstevel@tonic-gate 	NULL
1967c478bd9Sstevel@tonic-gate };
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * This routine is invoked automatically when the kernel module
2007c478bd9Sstevel@tonic-gate  * containing this routine is loaded.  This allows module specific
2017c478bd9Sstevel@tonic-gate  * initialization to be done when the module is loaded.
2027c478bd9Sstevel@tonic-gate  */
2037c478bd9Sstevel@tonic-gate int
2047c478bd9Sstevel@tonic-gate _init(void)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	int status;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if ((status = nfs_clntinit()) != 0) {
2097c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "_init: nfs_clntinit failed");
2107c478bd9Sstevel@tonic-gate 		return (status);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 	/*
2147c478bd9Sstevel@tonic-gate 	 * Create the version specific kstats.
2157c478bd9Sstevel@tonic-gate 	 *
2167c478bd9Sstevel@tonic-gate 	 * PSARC 2001/697 Contract Private Interface
2177c478bd9Sstevel@tonic-gate 	 * All nfs kstats are under SunMC contract
2187c478bd9Sstevel@tonic-gate 	 * Please refer to the PSARC listed above and contact
2197c478bd9Sstevel@tonic-gate 	 * SunMC before making any changes!
2207c478bd9Sstevel@tonic-gate 	 *
2217c478bd9Sstevel@tonic-gate 	 * Changes must be reviewed by Solaris File Sharing
2227c478bd9Sstevel@tonic-gate 	 * Changes must be communicated to contract-2001-697@sun.com
2237c478bd9Sstevel@tonic-gate 	 *
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	zone_key_create(&nfsstat_zone_key, nfsstat_zone_init, NULL,
2277c478bd9Sstevel@tonic-gate 	    nfsstat_zone_fini);
2287c478bd9Sstevel@tonic-gate 	status = mod_install((struct modlinkage *)&modlinkage);
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	if (status)  {
2317c478bd9Sstevel@tonic-gate 		(void) zone_key_delete(nfsstat_zone_key);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		/*
2347c478bd9Sstevel@tonic-gate 		 * Failed to install module, cleanup previous
2357c478bd9Sstevel@tonic-gate 		 * initialization work.
2367c478bd9Sstevel@tonic-gate 		 */
2377c478bd9Sstevel@tonic-gate 		nfs_clntfini();
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 		/*
2407c478bd9Sstevel@tonic-gate 		 * Clean up work performed indirectly by mod_installfs()
2417c478bd9Sstevel@tonic-gate 		 * as a result of our call to mod_install().
2427c478bd9Sstevel@tonic-gate 		 */
2437c478bd9Sstevel@tonic-gate 		nfs4fini();
2447c478bd9Sstevel@tonic-gate 		nfs3fini();
2457c478bd9Sstevel@tonic-gate 		nfsfini();
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 	return (status);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate int
2517c478bd9Sstevel@tonic-gate _fini(void)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	/* Don't allow module to be unloaded */
2547c478bd9Sstevel@tonic-gate 	return (EBUSY);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate int
2587c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	return (mod_info((struct modlinkage *)&modlinkage, modinfop));
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * General utilities
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*
268da6c28aaSamw  * Returns the preferred transfer size in bytes based on
2697c478bd9Sstevel@tonic-gate  * what network interfaces are available.
2707c478bd9Sstevel@tonic-gate  */
2717c478bd9Sstevel@tonic-gate int
2727c478bd9Sstevel@tonic-gate nfstsize(void)
2737c478bd9Sstevel@tonic-gate {
2747c478bd9Sstevel@tonic-gate 	/*
2757c478bd9Sstevel@tonic-gate 	 * For the moment, just return NFS_MAXDATA until we can query the
2767c478bd9Sstevel@tonic-gate 	 * appropriate transport.
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	return (NFS_MAXDATA);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate /*
282da6c28aaSamw  * Returns the preferred transfer size in bytes based on
2837c478bd9Sstevel@tonic-gate  * what network interfaces are available.
2847c478bd9Sstevel@tonic-gate  */
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /* this should reflect the largest transfer size possible */
2877c478bd9Sstevel@tonic-gate static int nfs3_max_transfer_size = 1024 * 1024;
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate int
2907c478bd9Sstevel@tonic-gate nfs3tsize(void)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	/*
2937c478bd9Sstevel@tonic-gate 	 * For the moment, just return nfs3_max_transfer_size until we
2947c478bd9Sstevel@tonic-gate 	 * can query the appropriate transport.
2957c478bd9Sstevel@tonic-gate 	 */
2967c478bd9Sstevel@tonic-gate 	return (nfs3_max_transfer_size);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate static uint_t nfs3_max_transfer_size_clts = 32 * 1024;
3007c478bd9Sstevel@tonic-gate static uint_t nfs3_max_transfer_size_cots = 1024 * 1024;
3017c478bd9Sstevel@tonic-gate static uint_t nfs3_max_transfer_size_rdma = 1024 * 1024;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate uint_t
3047c478bd9Sstevel@tonic-gate nfs3_tsize(struct knetconfig *knp)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	if (knp->knc_semantics == NC_TPI_COTS_ORD ||
3087c478bd9Sstevel@tonic-gate 	    knp->knc_semantics == NC_TPI_COTS)
3097c478bd9Sstevel@tonic-gate 		return (nfs3_max_transfer_size_cots);
3107c478bd9Sstevel@tonic-gate 	if (knp->knc_semantics == NC_TPI_RDMA)
3117c478bd9Sstevel@tonic-gate 		return (nfs3_max_transfer_size_rdma);
3127c478bd9Sstevel@tonic-gate 	return (nfs3_max_transfer_size_clts);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate uint_t
3167c478bd9Sstevel@tonic-gate rfs3_tsize(struct svc_req *req)
3177c478bd9Sstevel@tonic-gate {
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (req->rq_xprt->xp_type == T_COTS_ORD ||
3207c478bd9Sstevel@tonic-gate 	    req->rq_xprt->xp_type == T_COTS)
3217c478bd9Sstevel@tonic-gate 		return (nfs3_max_transfer_size_cots);
3227c478bd9Sstevel@tonic-gate 	if (req->rq_xprt->xp_type == T_RDMA)
3237c478bd9Sstevel@tonic-gate 		return (nfs3_max_transfer_size_rdma);
3247c478bd9Sstevel@tonic-gate 	return (nfs3_max_transfer_size_clts);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /* ARGSUSED */
3287c478bd9Sstevel@tonic-gate static int
3297c478bd9Sstevel@tonic-gate nfsdyninit(int fstyp, char *name)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t nfsdyn_vfsops_template[] = {
332aa59c4cbSrsb 		VFSNAME_MOUNTROOT, { .vfs_mountroot = nfsdyn_mountroot },
3337c478bd9Sstevel@tonic-gate 		NULL, NULL
3347c478bd9Sstevel@tonic-gate 	};
3357c478bd9Sstevel@tonic-gate 	int error;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstyp, nfsdyn_vfsops_template, &nfsdyn_vfsops);
3387c478bd9Sstevel@tonic-gate 	if (error != 0)
3397c478bd9Sstevel@tonic-gate 		return (error);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	return (0);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /* ARGSUSED */
3457c478bd9Sstevel@tonic-gate static int
3467c478bd9Sstevel@tonic-gate nfsdyn_mountroot(vfs_t *vfsp, whymountroot_t why)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate 	char root_hostname[SYS_NMLN+1];
3497c478bd9Sstevel@tonic-gate 	struct servinfo *svp;
3507c478bd9Sstevel@tonic-gate 	int error;
3517c478bd9Sstevel@tonic-gate 	int vfsflags;
3527c478bd9Sstevel@tonic-gate 	char *root_path;
3537c478bd9Sstevel@tonic-gate 	struct pathname pn;
3547c478bd9Sstevel@tonic-gate 	char *name;
3557c478bd9Sstevel@tonic-gate 	static char token[10];
3567c478bd9Sstevel@tonic-gate 	struct nfs_args args;		/* nfs mount arguments */
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	bzero(&args, sizeof (args));
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/* do this BEFORE getfile which causes xid stamps to be initialized */
3617c478bd9Sstevel@tonic-gate 	clkset(-1L);		/* hack for now - until we get time svc? */
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if (why == ROOT_REMOUNT) {
3647c478bd9Sstevel@tonic-gate 		/*
3657c478bd9Sstevel@tonic-gate 		 * Shouldn't happen.
3667c478bd9Sstevel@tonic-gate 		 */
3677c478bd9Sstevel@tonic-gate 		panic("nfs3_mountroot: why == ROOT_REMOUNT\n");
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if (why == ROOT_UNMOUNT) {
3717c478bd9Sstevel@tonic-gate 		/*
3727c478bd9Sstevel@tonic-gate 		 * Nothing to do for NFS.
3737c478bd9Sstevel@tonic-gate 		 */
3747c478bd9Sstevel@tonic-gate 		return (0);
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	/*
3787c478bd9Sstevel@tonic-gate 	 * why == ROOT_INIT
3797c478bd9Sstevel@tonic-gate 	 */
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	name = token;
3827c478bd9Sstevel@tonic-gate 	*name = 0;
3837c478bd9Sstevel@tonic-gate 	getfsname("root", name, sizeof (token));
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	pn_alloc(&pn);
3867c478bd9Sstevel@tonic-gate 	root_path = pn.pn_path;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
3897c478bd9Sstevel@tonic-gate 	mutex_init(&svp->sv_lock, NULL, MUTEX_DEFAULT, NULL);
3907c478bd9Sstevel@tonic-gate 	svp->sv_knconf = kmem_zalloc(sizeof (*svp->sv_knconf), KM_SLEEP);
3917c478bd9Sstevel@tonic-gate 	svp->sv_knconf->knc_protofmly = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
3927c478bd9Sstevel@tonic-gate 	svp->sv_knconf->knc_proto = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * First try version 4
3967c478bd9Sstevel@tonic-gate 	 */
3977c478bd9Sstevel@tonic-gate 	vfs_setops(vfsp, nfs4_vfsops);
3987c478bd9Sstevel@tonic-gate 	args.addr = &svp->sv_addr;
3997c478bd9Sstevel@tonic-gate 	args.fh = (char *)&svp->sv_fhandle;
4007c478bd9Sstevel@tonic-gate 	args.knconf = svp->sv_knconf;
4017c478bd9Sstevel@tonic-gate 	args.hostname = root_hostname;
4027c478bd9Sstevel@tonic-gate 	vfsflags = 0;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	if (error = mount_root(*name ? name : "root", root_path, NFS_V4,
4050fbb751dSJohn Levon 	    &args, &vfsflags)) {
4067c478bd9Sstevel@tonic-gate 		if (error != EPROTONOSUPPORT) {
4077c478bd9Sstevel@tonic-gate 			nfs_cmn_err(error, CE_WARN,
4080fbb751dSJohn Levon 			    "Unable to mount NFS root filesystem: %m");
4097c478bd9Sstevel@tonic-gate 			sv_free(svp);
4107c478bd9Sstevel@tonic-gate 			pn_free(&pn);
4117c478bd9Sstevel@tonic-gate 			vfs_setops(vfsp, nfsdyn_vfsops);
4127c478bd9Sstevel@tonic-gate 			return (error);
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 		/*
4167c478bd9Sstevel@tonic-gate 		 * Then try version 3
4177c478bd9Sstevel@tonic-gate 		 */
4187c478bd9Sstevel@tonic-gate 		bzero(&args, sizeof (args));
4197c478bd9Sstevel@tonic-gate 		vfs_setops(vfsp, nfs3_vfsops);
4207c478bd9Sstevel@tonic-gate 		args.addr = &svp->sv_addr;
4217c478bd9Sstevel@tonic-gate 		args.fh = (char *)&svp->sv_fhandle;
4227c478bd9Sstevel@tonic-gate 		args.knconf = svp->sv_knconf;
4237c478bd9Sstevel@tonic-gate 		args.hostname = root_hostname;
4247c478bd9Sstevel@tonic-gate 		vfsflags = 0;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 		if (error = mount_root(*name ? name : "root", root_path,
4270fbb751dSJohn Levon 		    NFS_V3, &args, &vfsflags)) {
4287c478bd9Sstevel@tonic-gate 			if (error != EPROTONOSUPPORT) {
4297c478bd9Sstevel@tonic-gate 				nfs_cmn_err(error, CE_WARN,
4307c478bd9Sstevel@tonic-gate 				    "Unable to mount NFS root filesystem: %m");
4317c478bd9Sstevel@tonic-gate 				sv_free(svp);
4327c478bd9Sstevel@tonic-gate 				pn_free(&pn);
4337c478bd9Sstevel@tonic-gate 				vfs_setops(vfsp, nfsdyn_vfsops);
4347c478bd9Sstevel@tonic-gate 				return (error);
4357c478bd9Sstevel@tonic-gate 			}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 			/*
4387c478bd9Sstevel@tonic-gate 			 * Finally, try version 2
4397c478bd9Sstevel@tonic-gate 			 */
4407c478bd9Sstevel@tonic-gate 			bzero(&args, sizeof (args));
4417c478bd9Sstevel@tonic-gate 			args.addr = &svp->sv_addr;
4427c478bd9Sstevel@tonic-gate 			args.fh = (char *)&svp->sv_fhandle.fh_buf;
4437c478bd9Sstevel@tonic-gate 			args.knconf = svp->sv_knconf;
4447c478bd9Sstevel@tonic-gate 			args.hostname = root_hostname;
4457c478bd9Sstevel@tonic-gate 			vfsflags = 0;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 			vfs_setops(vfsp, nfs_vfsops);
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 			if (error = mount_root(*name ? name : "root",
4500fbb751dSJohn Levon 			    root_path, NFS_VERSION, &args, &vfsflags)) {
4517c478bd9Sstevel@tonic-gate 				nfs_cmn_err(error, CE_WARN,
4527c478bd9Sstevel@tonic-gate 				    "Unable to mount NFS root filesystem: %m");
4537c478bd9Sstevel@tonic-gate 				sv_free(svp);
4547c478bd9Sstevel@tonic-gate 				pn_free(&pn);
4557c478bd9Sstevel@tonic-gate 				vfs_setops(vfsp, nfsdyn_vfsops);
4567c478bd9Sstevel@tonic-gate 				return (error);
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	sv_free(svp);
4627c478bd9Sstevel@tonic-gate 	pn_free(&pn);
4637c478bd9Sstevel@tonic-gate 	return (VFS_MOUNTROOT(vfsp, why));
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate int
4677c478bd9Sstevel@tonic-gate nfs_setopts(vnode_t *vp, model_t model, struct nfs_args *buf)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;			/* mount info, pointed at by vfs */
4707c478bd9Sstevel@tonic-gate 	STRUCT_HANDLE(nfs_args, args);
4717c478bd9Sstevel@tonic-gate 	int flags;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate #ifdef lint
4747c478bd9Sstevel@tonic-gate 	model = model;
4757c478bd9Sstevel@tonic-gate #endif
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	STRUCT_SET_HANDLE(args, model, buf);
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	flags = STRUCT_FGET(args, flags);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/*
4827c478bd9Sstevel@tonic-gate 	 * Set option fields in mount info record
4837c478bd9Sstevel@tonic-gate 	 */
4847c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_NOAC) {
4877c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_NOAC;
4887c478bd9Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_NOCTO)
4917c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_NOCTO;
4927c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_LLOCK)
4937c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_LLOCK;
4947c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_GRPID)
4957c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_GRPID;
4967c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_RETRANS) {
4977c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(args, retrans) < 0)
4987c478bd9Sstevel@tonic-gate 			return (EINVAL);
4997c478bd9Sstevel@tonic-gate 		mi->mi_retrans = STRUCT_FGET(args, retrans);
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_TIMEO) {
5027c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(args, timeo) <= 0)
5037c478bd9Sstevel@tonic-gate 			return (EINVAL);
5047c478bd9Sstevel@tonic-gate 		mi->mi_timeo = STRUCT_FGET(args, timeo);
5057c478bd9Sstevel@tonic-gate 		/*
5067c478bd9Sstevel@tonic-gate 		 * The following scales the standard deviation and
5077c478bd9Sstevel@tonic-gate 		 * and current retransmission timer to match the
5087c478bd9Sstevel@tonic-gate 		 * initial value for the timeout specified.
5097c478bd9Sstevel@tonic-gate 		 */
5107c478bd9Sstevel@tonic-gate 		mi->mi_timers[NFS_CALLTYPES].rt_deviate =
5117c478bd9Sstevel@tonic-gate 		    (mi->mi_timeo * hz * 2) / 5;
5127c478bd9Sstevel@tonic-gate 		mi->mi_timers[NFS_CALLTYPES].rt_rtxcur =
5137c478bd9Sstevel@tonic-gate 		    mi->mi_timeo * hz / 10;
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_RSIZE) {
5167c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(args, rsize) <= 0)
5177c478bd9Sstevel@tonic-gate 			return (EINVAL);
5187c478bd9Sstevel@tonic-gate 		mi->mi_tsize = MIN(mi->mi_tsize, STRUCT_FGET(args, rsize));
5197c478bd9Sstevel@tonic-gate 		mi->mi_curread = MIN(mi->mi_curread, mi->mi_tsize);
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_WSIZE) {
5227c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(args, wsize) <= 0)
5237c478bd9Sstevel@tonic-gate 			return (EINVAL);
5247c478bd9Sstevel@tonic-gate 		mi->mi_stsize = MIN(mi->mi_stsize, STRUCT_FGET(args, wsize));
5257c478bd9Sstevel@tonic-gate 		mi->mi_curwrite = MIN(mi->mi_curwrite, mi->mi_stsize);
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_ACREGMIN) {
5287c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(args, acregmin) < 0)
5297c478bd9Sstevel@tonic-gate 			mi->mi_acregmin = ACMINMAX;
5307c478bd9Sstevel@tonic-gate 		else
5317c478bd9Sstevel@tonic-gate 			mi->mi_acregmin = MIN(STRUCT_FGET(args, acregmin),
5327c478bd9Sstevel@tonic-gate 			    ACMINMAX);
5337c478bd9Sstevel@tonic-gate 		mi->mi_acregmin = SEC2HR(mi->mi_acregmin);
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_ACREGMAX) {
5367c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(args, acregmax) < 0)
5377c478bd9Sstevel@tonic-gate 			mi->mi_acregmax = ACMAXMAX;
5387c478bd9Sstevel@tonic-gate 		else
5397c478bd9Sstevel@tonic-gate 			mi->mi_acregmax = MIN(STRUCT_FGET(args, acregmax),
5407c478bd9Sstevel@tonic-gate 			    ACMAXMAX);
5417c478bd9Sstevel@tonic-gate 		mi->mi_acregmax = SEC2HR(mi->mi_acregmax);
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_ACDIRMIN) {
5447c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(args, acdirmin) < 0)
5457c478bd9Sstevel@tonic-gate 			mi->mi_acdirmin = ACMINMAX;
5467c478bd9Sstevel@tonic-gate 		else
5477c478bd9Sstevel@tonic-gate 			mi->mi_acdirmin = MIN(STRUCT_FGET(args, acdirmin),
5487c478bd9Sstevel@tonic-gate 			    ACMINMAX);
5497c478bd9Sstevel@tonic-gate 		mi->mi_acdirmin = SEC2HR(mi->mi_acdirmin);
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_ACDIRMAX) {
5527c478bd9Sstevel@tonic-gate 		if (STRUCT_FGET(args, acdirmax) < 0)
5537c478bd9Sstevel@tonic-gate 			mi->mi_acdirmax = ACMAXMAX;
5547c478bd9Sstevel@tonic-gate 		else
5557c478bd9Sstevel@tonic-gate 			mi->mi_acdirmax = MIN(STRUCT_FGET(args, acdirmax),
5567c478bd9Sstevel@tonic-gate 			    ACMAXMAX);
5577c478bd9Sstevel@tonic-gate 		mi->mi_acdirmax = SEC2HR(mi->mi_acdirmax);
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (flags & NFSMNT_LOOPBACK)
5617c478bd9Sstevel@tonic-gate 		mi->mi_flags |= MI_LOOPBACK;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	return (0);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * Set or Clear direct I/O flag
5687c478bd9Sstevel@tonic-gate  * VOP_RWLOCK() is held for write access to prevent a race condition
5697c478bd9Sstevel@tonic-gate  * which would occur if a process is in the middle of a write when
5707c478bd9Sstevel@tonic-gate  * directio flag gets set. It is possible that all pages may not get flushed.
5717c478bd9Sstevel@tonic-gate  */
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate /* ARGSUSED */
5747c478bd9Sstevel@tonic-gate int
5757c478bd9Sstevel@tonic-gate nfs_directio(vnode_t *vp, int cmd, cred_t *cr)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	int	error = 0;
5787c478bd9Sstevel@tonic-gate 	rnode_t	*rp;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if (cmd == DIRECTIO_ON) {
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 		if (rp->r_flags & RDIRECTIO)
5857c478bd9Sstevel@tonic-gate 			return (0);
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 		/*
5887c478bd9Sstevel@tonic-gate 		 * Flush the page cache.
5897c478bd9Sstevel@tonic-gate 		 */
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		if (rp->r_flags & RDIRECTIO) {
5947c478bd9Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
5957c478bd9Sstevel@tonic-gate 			return (0);
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 		if (vn_has_cached_data(vp) &&
5997c478bd9Sstevel@tonic-gate 		    ((rp->r_flags & RDIRTY) || rp->r_awcount > 0)) {
6007c478bd9Sstevel@tonic-gate 			error = VOP_PUTPAGE(vp, (offset_t)0, (uint_t)0,
601da6c28aaSamw 			    B_INVAL, cr, NULL);
6027c478bd9Sstevel@tonic-gate 			if (error) {
6037c478bd9Sstevel@tonic-gate 				if (error == ENOSPC || error == EDQUOT) {
6047c478bd9Sstevel@tonic-gate 					mutex_enter(&rp->r_statelock);
6057c478bd9Sstevel@tonic-gate 					if (!rp->r_error)
6067c478bd9Sstevel@tonic-gate 						rp->r_error = error;
6077c478bd9Sstevel@tonic-gate 					mutex_exit(&rp->r_statelock);
6087c478bd9Sstevel@tonic-gate 				}
6097c478bd9Sstevel@tonic-gate 				VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
6107c478bd9Sstevel@tonic-gate 				return (error);
6117c478bd9Sstevel@tonic-gate 			}
6127c478bd9Sstevel@tonic-gate 		}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
6157c478bd9Sstevel@tonic-gate 		rp->r_flags |= RDIRECTIO;
6167c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
6177c478bd9Sstevel@tonic-gate 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
6187c478bd9Sstevel@tonic-gate 		return (0);
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	if (cmd == DIRECTIO_OFF) {
6227c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
6237c478bd9Sstevel@tonic-gate 		rp->r_flags &= ~RDIRECTIO;	/* disable direct mode */
6247c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
6257c478bd9Sstevel@tonic-gate 		return (0);
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	return (EINVAL);
6297c478bd9Sstevel@tonic-gate }
630