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