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