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