xref: /onnv-gate/usr/src/uts/common/fs/nfs/nfs_common.c (revision 12633:9f2cda0ed938)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51488Srsb  * Common Development and Distribution License (the "License").
61488Srsb  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12633Sjohn.levon@sun.com  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  *	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
270Sstevel@tonic-gate  *		All rights reserved.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/errno.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/user.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <sys/time.h>
360Sstevel@tonic-gate #include <sys/utsname.h>
370Sstevel@tonic-gate #include <sys/vfs.h>
383898Srsb #include <sys/vfs_opreg.h>
390Sstevel@tonic-gate #include <sys/vnode.h>
400Sstevel@tonic-gate #include <sys/pathname.h>
410Sstevel@tonic-gate #include <sys/bootconf.h>
420Sstevel@tonic-gate #include <fs/fs_subr.h>
430Sstevel@tonic-gate #include <rpc/types.h>
440Sstevel@tonic-gate #include <nfs/nfs.h>
450Sstevel@tonic-gate #include <nfs/nfs4.h>
460Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
470Sstevel@tonic-gate #include <nfs/rnode.h>
480Sstevel@tonic-gate #include <nfs/mount.h>
490Sstevel@tonic-gate #include <nfs/nfssys.h>
500Sstevel@tonic-gate #include <sys/debug.h>
510Sstevel@tonic-gate #include <sys/cmn_err.h>
520Sstevel@tonic-gate #include <sys/file.h>
530Sstevel@tonic-gate #include <sys/fcntl.h>
540Sstevel@tonic-gate #include <sys/zone.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate  * This is the loadable module wrapper.
580Sstevel@tonic-gate  */
590Sstevel@tonic-gate #include <sys/systm.h>
600Sstevel@tonic-gate #include <sys/modctl.h>
610Sstevel@tonic-gate #include <sys/syscall.h>
620Sstevel@tonic-gate #include <sys/ddi.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <rpc/types.h>
650Sstevel@tonic-gate #include <rpc/auth.h>
660Sstevel@tonic-gate #include <rpc/clnt.h>
670Sstevel@tonic-gate #include <rpc/svc.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
705331Samw  * The pseudo NFS filesystem to allow diskless booting to dynamically
710Sstevel@tonic-gate  * mount either a NFS V2, NFS V3, or NFS V4 filesystem.  This only implements
720Sstevel@tonic-gate  * the VFS_MOUNTROOT op and is only intended to be used by the
730Sstevel@tonic-gate  * diskless booting code until the real root filesystem is mounted.
740Sstevel@tonic-gate  * Nothing else should ever call this!
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  * The strategy is that if the initial rootfs type is set to "nfsdyn"
770Sstevel@tonic-gate  * by loadrootmodules() this filesystem is called to mount the
780Sstevel@tonic-gate  * root filesystem.  It first attempts to mount a V4 filesystem, and if that
790Sstevel@tonic-gate  * fails due to an RPC version mismatch it tries V3 and finally V2.
800Sstevel@tonic-gate  * Once the real mount succeeds the vfsops and rootfs name are changed
810Sstevel@tonic-gate  * to reflect the real filesystem type.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate static int nfsdyninit(int, char *);
840Sstevel@tonic-gate static int nfsdyn_mountroot(vfs_t *, whymountroot_t);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate vfsops_t *nfsdyn_vfsops;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * The following data structures are used to configure the NFS
900Sstevel@tonic-gate  * system call, the NFS Version 2 client VFS, and the NFS Version
910Sstevel@tonic-gate  * 3 client VFS into the system.  The NFS Version 4 structures are defined in
920Sstevel@tonic-gate  * nfs4_common.c
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * The NFS system call.
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate static struct sysent nfssysent = {
990Sstevel@tonic-gate 	2,
1000Sstevel@tonic-gate 	SE_32RVAL1 | SE_ARGC | SE_NOUNLOAD,
1010Sstevel@tonic-gate 	nfssys
1020Sstevel@tonic-gate };
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static struct modlsys modlsys = {
1050Sstevel@tonic-gate 	&mod_syscallops,
1060Sstevel@tonic-gate 	"NFS syscall, client, and common",
1070Sstevel@tonic-gate 	&nfssysent
1080Sstevel@tonic-gate };
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1110Sstevel@tonic-gate static struct modlsys modlsys32 = {
1120Sstevel@tonic-gate 	&mod_syscallops32,
1130Sstevel@tonic-gate 	"NFS syscall, client, and common (32-bit)",
1140Sstevel@tonic-gate 	&nfssysent
1150Sstevel@tonic-gate };
1160Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * The NFS Dynamic client VFS.
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate static vfsdef_t vfw = {
1220Sstevel@tonic-gate 	VFSDEF_VERSION,
1230Sstevel@tonic-gate 	"nfsdyn",
1240Sstevel@tonic-gate 	nfsdyninit,
125*12633Sjohn.levon@sun.com 	VSW_ZMOUNT,
1260Sstevel@tonic-gate 	NULL
1270Sstevel@tonic-gate };
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static struct modlfs modlfs = {
1300Sstevel@tonic-gate 	&mod_fsops,
1310Sstevel@tonic-gate 	"network filesystem",
1320Sstevel@tonic-gate 	&vfw
1330Sstevel@tonic-gate };
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate /*
1360Sstevel@tonic-gate  * The NFS Version 2 client VFS.
1370Sstevel@tonic-gate  */
1380Sstevel@tonic-gate static vfsdef_t vfw2 = {
1390Sstevel@tonic-gate 	VFSDEF_VERSION,
1400Sstevel@tonic-gate 	"nfs",
1410Sstevel@tonic-gate 	nfsinit,
142*12633Sjohn.levon@sun.com 	VSW_CANREMOUNT|VSW_NOTZONESAFE|VSW_STATS|VSW_ZMOUNT,
1430Sstevel@tonic-gate 	NULL
1440Sstevel@tonic-gate };
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate static struct modlfs modlfs2 = {
1470Sstevel@tonic-gate 	&mod_fsops,
1480Sstevel@tonic-gate 	"network filesystem version 2",
1490Sstevel@tonic-gate 	&vfw2
1500Sstevel@tonic-gate };
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * The NFS Version 3 client VFS.
1540Sstevel@tonic-gate  */
1550Sstevel@tonic-gate static vfsdef_t vfw3 = {
1560Sstevel@tonic-gate 	VFSDEF_VERSION,
1570Sstevel@tonic-gate 	"nfs3",
1580Sstevel@tonic-gate 	nfs3init,
159*12633Sjohn.levon@sun.com 	VSW_CANREMOUNT|VSW_NOTZONESAFE|VSW_STATS|VSW_ZMOUNT,
1600Sstevel@tonic-gate 	NULL
1610Sstevel@tonic-gate };
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate static struct modlfs modlfs3 = {
1640Sstevel@tonic-gate 	&mod_fsops,
1650Sstevel@tonic-gate 	"network filesystem version 3",
1660Sstevel@tonic-gate 	&vfw3
1670Sstevel@tonic-gate };
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate extern struct modlfs modlfs4;
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate  * We have too many linkage structures so we define our own XXX
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate struct modlinkage_big {
1750Sstevel@tonic-gate 	int		ml_rev;		/* rev of loadable modules system */
1760Sstevel@tonic-gate 	void		*ml_linkage[7];	/* NULL terminated list of */
1770Sstevel@tonic-gate 					/* linkage structures */
1780Sstevel@tonic-gate };
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate /*
1810Sstevel@tonic-gate  * All of the module configuration linkages required to configure
1820Sstevel@tonic-gate  * the system call and client VFS's into the system.
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate static struct modlinkage_big modlinkage = {
1850Sstevel@tonic-gate 	MODREV_1,
1860Sstevel@tonic-gate 	&modlsys,
1870Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1880Sstevel@tonic-gate 	&modlsys32,
1890Sstevel@tonic-gate #endif
1900Sstevel@tonic-gate 	&modlfs,
1910Sstevel@tonic-gate 	&modlfs2,
1920Sstevel@tonic-gate 	&modlfs3,
1930Sstevel@tonic-gate 	&modlfs4,
1940Sstevel@tonic-gate 	NULL
1950Sstevel@tonic-gate };
1960Sstevel@tonic-gate 
1970Sstevel@tonic-gate /*
1980Sstevel@tonic-gate  * specfs - for getfsname only??
1990Sstevel@tonic-gate  * rpcmod - too many symbols to build stubs for them all
2000Sstevel@tonic-gate  */
2010Sstevel@tonic-gate char _depends_on[] = "fs/specfs strmod/rpcmod misc/rpcsec";
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate /*
2040Sstevel@tonic-gate  * This routine is invoked automatically when the kernel module
2050Sstevel@tonic-gate  * containing this routine is loaded.  This allows module specific
2060Sstevel@tonic-gate  * initialization to be done when the module is loaded.
2070Sstevel@tonic-gate  */
2080Sstevel@tonic-gate int
_init(void)2090Sstevel@tonic-gate _init(void)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate 	int status;
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate 	if ((status = nfs_clntinit()) != 0) {
2140Sstevel@tonic-gate 		cmn_err(CE_WARN, "_init: nfs_clntinit failed");
2150Sstevel@tonic-gate 		return (status);
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	/*
2190Sstevel@tonic-gate 	 * Create the version specific kstats.
2200Sstevel@tonic-gate 	 *
2210Sstevel@tonic-gate 	 * PSARC 2001/697 Contract Private Interface
2220Sstevel@tonic-gate 	 * All nfs kstats are under SunMC contract
2230Sstevel@tonic-gate 	 * Please refer to the PSARC listed above and contact
2240Sstevel@tonic-gate 	 * SunMC before making any changes!
2250Sstevel@tonic-gate 	 *
2260Sstevel@tonic-gate 	 * Changes must be reviewed by Solaris File Sharing
2270Sstevel@tonic-gate 	 * Changes must be communicated to contract-2001-697@sun.com
2280Sstevel@tonic-gate 	 *
2290Sstevel@tonic-gate 	 */
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	zone_key_create(&nfsstat_zone_key, nfsstat_zone_init, NULL,
2320Sstevel@tonic-gate 	    nfsstat_zone_fini);
2330Sstevel@tonic-gate 	status = mod_install((struct modlinkage *)&modlinkage);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (status)  {
2360Sstevel@tonic-gate 		(void) zone_key_delete(nfsstat_zone_key);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 		/*
2390Sstevel@tonic-gate 		 * Failed to install module, cleanup previous
2400Sstevel@tonic-gate 		 * initialization work.
2410Sstevel@tonic-gate 		 */
2420Sstevel@tonic-gate 		nfs_clntfini();
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		/*
2450Sstevel@tonic-gate 		 * Clean up work performed indirectly by mod_installfs()
2460Sstevel@tonic-gate 		 * as a result of our call to mod_install().
2470Sstevel@tonic-gate 		 */
2480Sstevel@tonic-gate 		nfs4fini();
2490Sstevel@tonic-gate 		nfs3fini();
2500Sstevel@tonic-gate 		nfsfini();
2510Sstevel@tonic-gate 	}
2520Sstevel@tonic-gate 	return (status);
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate int
_fini(void)2560Sstevel@tonic-gate _fini(void)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate 	/* Don't allow module to be unloaded */
2590Sstevel@tonic-gate 	return (EBUSY);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2630Sstevel@tonic-gate _info(struct modinfo *modinfop)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	return (mod_info((struct modlinkage *)&modlinkage, modinfop));
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate /*
2690Sstevel@tonic-gate  * General utilities
2700Sstevel@tonic-gate  */
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate /*
2735331Samw  * Returns the preferred transfer size in bytes based on
2740Sstevel@tonic-gate  * what network interfaces are available.
2750Sstevel@tonic-gate  */
2760Sstevel@tonic-gate int
nfstsize(void)2770Sstevel@tonic-gate nfstsize(void)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate 	/*
2800Sstevel@tonic-gate 	 * For the moment, just return NFS_MAXDATA until we can query the
2810Sstevel@tonic-gate 	 * appropriate transport.
2820Sstevel@tonic-gate 	 */
2830Sstevel@tonic-gate 	return (NFS_MAXDATA);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /*
2875331Samw  * Returns the preferred transfer size in bytes based on
2880Sstevel@tonic-gate  * what network interfaces are available.
2890Sstevel@tonic-gate  */
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /* this should reflect the largest transfer size possible */
2920Sstevel@tonic-gate static int nfs3_max_transfer_size = 1024 * 1024;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate int
nfs3tsize(void)2950Sstevel@tonic-gate nfs3tsize(void)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	/*
2980Sstevel@tonic-gate 	 * For the moment, just return nfs3_max_transfer_size until we
2990Sstevel@tonic-gate 	 * can query the appropriate transport.
3000Sstevel@tonic-gate 	 */
3010Sstevel@tonic-gate 	return (nfs3_max_transfer_size);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate static uint_t nfs3_max_transfer_size_clts = 32 * 1024;
3050Sstevel@tonic-gate static uint_t nfs3_max_transfer_size_cots = 1024 * 1024;
3060Sstevel@tonic-gate static uint_t nfs3_max_transfer_size_rdma = 1024 * 1024;
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate uint_t
nfs3_tsize(struct knetconfig * knp)3090Sstevel@tonic-gate nfs3_tsize(struct knetconfig *knp)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	if (knp->knc_semantics == NC_TPI_COTS_ORD ||
3130Sstevel@tonic-gate 	    knp->knc_semantics == NC_TPI_COTS)
3140Sstevel@tonic-gate 		return (nfs3_max_transfer_size_cots);
3150Sstevel@tonic-gate 	if (knp->knc_semantics == NC_TPI_RDMA)
3160Sstevel@tonic-gate 		return (nfs3_max_transfer_size_rdma);
3170Sstevel@tonic-gate 	return (nfs3_max_transfer_size_clts);
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate uint_t
rfs3_tsize(struct svc_req * req)3210Sstevel@tonic-gate rfs3_tsize(struct svc_req *req)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	if (req->rq_xprt->xp_type == T_COTS_ORD ||
3250Sstevel@tonic-gate 	    req->rq_xprt->xp_type == T_COTS)
3260Sstevel@tonic-gate 		return (nfs3_max_transfer_size_cots);
3270Sstevel@tonic-gate 	if (req->rq_xprt->xp_type == T_RDMA)
3280Sstevel@tonic-gate 		return (nfs3_max_transfer_size_rdma);
3290Sstevel@tonic-gate 	return (nfs3_max_transfer_size_clts);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate /* ARGSUSED */
3330Sstevel@tonic-gate static int
nfsdyninit(int fstyp,char * name)3340Sstevel@tonic-gate nfsdyninit(int fstyp, char *name)
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	static const fs_operation_def_t nfsdyn_vfsops_template[] = {
3373898Srsb 		VFSNAME_MOUNTROOT, { .vfs_mountroot = nfsdyn_mountroot },
3380Sstevel@tonic-gate 		NULL, NULL
3390Sstevel@tonic-gate 	};
3400Sstevel@tonic-gate 	int error;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	error = vfs_setfsops(fstyp, nfsdyn_vfsops_template, &nfsdyn_vfsops);
3430Sstevel@tonic-gate 	if (error != 0)
3440Sstevel@tonic-gate 		return (error);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	return (0);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate /* ARGSUSED */
3500Sstevel@tonic-gate static int
nfsdyn_mountroot(vfs_t * vfsp,whymountroot_t why)3510Sstevel@tonic-gate nfsdyn_mountroot(vfs_t *vfsp, whymountroot_t why)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	char root_hostname[SYS_NMLN+1];
3540Sstevel@tonic-gate 	struct servinfo *svp;
3550Sstevel@tonic-gate 	int error;
3560Sstevel@tonic-gate 	int vfsflags;
3570Sstevel@tonic-gate 	char *root_path;
3580Sstevel@tonic-gate 	struct pathname pn;
3590Sstevel@tonic-gate 	char *name;
3600Sstevel@tonic-gate 	static char token[10];
3610Sstevel@tonic-gate 	struct nfs_args args;		/* nfs mount arguments */
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	bzero(&args, sizeof (args));
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	/* do this BEFORE getfile which causes xid stamps to be initialized */
3660Sstevel@tonic-gate 	clkset(-1L);		/* hack for now - until we get time svc? */
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	if (why == ROOT_REMOUNT) {
3690Sstevel@tonic-gate 		/*
3700Sstevel@tonic-gate 		 * Shouldn't happen.
3710Sstevel@tonic-gate 		 */
3720Sstevel@tonic-gate 		panic("nfs3_mountroot: why == ROOT_REMOUNT\n");
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 	if (why == ROOT_UNMOUNT) {
3760Sstevel@tonic-gate 		/*
3770Sstevel@tonic-gate 		 * Nothing to do for NFS.
3780Sstevel@tonic-gate 		 */
3790Sstevel@tonic-gate 		return (0);
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	/*
3830Sstevel@tonic-gate 	 * why == ROOT_INIT
3840Sstevel@tonic-gate 	 */
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	name = token;
3870Sstevel@tonic-gate 	*name = 0;
3880Sstevel@tonic-gate 	getfsname("root", name, sizeof (token));
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	pn_alloc(&pn);
3910Sstevel@tonic-gate 	root_path = pn.pn_path;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
3940Sstevel@tonic-gate 	mutex_init(&svp->sv_lock, NULL, MUTEX_DEFAULT, NULL);
3950Sstevel@tonic-gate 	svp->sv_knconf = kmem_zalloc(sizeof (*svp->sv_knconf), KM_SLEEP);
3960Sstevel@tonic-gate 	svp->sv_knconf->knc_protofmly = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
3970Sstevel@tonic-gate 	svp->sv_knconf->knc_proto = kmem_alloc(KNC_STRSIZE, KM_SLEEP);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	/*
4000Sstevel@tonic-gate 	 * First try version 4
4010Sstevel@tonic-gate 	 */
4020Sstevel@tonic-gate 	vfs_setops(vfsp, nfs4_vfsops);
4030Sstevel@tonic-gate 	args.addr = &svp->sv_addr;
4040Sstevel@tonic-gate 	args.fh = (char *)&svp->sv_fhandle;
4050Sstevel@tonic-gate 	args.knconf = svp->sv_knconf;
4060Sstevel@tonic-gate 	args.hostname = root_hostname;
4070Sstevel@tonic-gate 	vfsflags = 0;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate 	if (error = mount_root(*name ? name : "root", root_path, NFS_V4,
410*12633Sjohn.levon@sun.com 	    &args, &vfsflags)) {
4110Sstevel@tonic-gate 		if (error != EPROTONOSUPPORT) {
4120Sstevel@tonic-gate 			nfs_cmn_err(error, CE_WARN,
413*12633Sjohn.levon@sun.com 			    "Unable to mount NFS root filesystem: %m");
4140Sstevel@tonic-gate 			sv_free(svp);
4150Sstevel@tonic-gate 			pn_free(&pn);
4160Sstevel@tonic-gate 			vfs_setops(vfsp, nfsdyn_vfsops);
4170Sstevel@tonic-gate 			return (error);
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 		/*
4210Sstevel@tonic-gate 		 * Then try version 3
4220Sstevel@tonic-gate 		 */
4230Sstevel@tonic-gate 		bzero(&args, sizeof (args));
4240Sstevel@tonic-gate 		vfs_setops(vfsp, nfs3_vfsops);
4250Sstevel@tonic-gate 		args.addr = &svp->sv_addr;
4260Sstevel@tonic-gate 		args.fh = (char *)&svp->sv_fhandle;
4270Sstevel@tonic-gate 		args.knconf = svp->sv_knconf;
4280Sstevel@tonic-gate 		args.hostname = root_hostname;
4290Sstevel@tonic-gate 		vfsflags = 0;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		if (error = mount_root(*name ? name : "root", root_path,
432*12633Sjohn.levon@sun.com 		    NFS_V3, &args, &vfsflags)) {
4330Sstevel@tonic-gate 			if (error != EPROTONOSUPPORT) {
4340Sstevel@tonic-gate 				nfs_cmn_err(error, CE_WARN,
4350Sstevel@tonic-gate 				    "Unable to mount NFS root filesystem: %m");
4360Sstevel@tonic-gate 				sv_free(svp);
4370Sstevel@tonic-gate 				pn_free(&pn);
4380Sstevel@tonic-gate 				vfs_setops(vfsp, nfsdyn_vfsops);
4390Sstevel@tonic-gate 				return (error);
4400Sstevel@tonic-gate 			}
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 			/*
4430Sstevel@tonic-gate 			 * Finally, try version 2
4440Sstevel@tonic-gate 			 */
4450Sstevel@tonic-gate 			bzero(&args, sizeof (args));
4460Sstevel@tonic-gate 			args.addr = &svp->sv_addr;
4470Sstevel@tonic-gate 			args.fh = (char *)&svp->sv_fhandle.fh_buf;
4480Sstevel@tonic-gate 			args.knconf = svp->sv_knconf;
4490Sstevel@tonic-gate 			args.hostname = root_hostname;
4500Sstevel@tonic-gate 			vfsflags = 0;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 			vfs_setops(vfsp, nfs_vfsops);
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 			if (error = mount_root(*name ? name : "root",
455*12633Sjohn.levon@sun.com 			    root_path, NFS_VERSION, &args, &vfsflags)) {
4560Sstevel@tonic-gate 				nfs_cmn_err(error, CE_WARN,
4570Sstevel@tonic-gate 				    "Unable to mount NFS root filesystem: %m");
4580Sstevel@tonic-gate 				sv_free(svp);
4590Sstevel@tonic-gate 				pn_free(&pn);
4600Sstevel@tonic-gate 				vfs_setops(vfsp, nfsdyn_vfsops);
4610Sstevel@tonic-gate 				return (error);
4620Sstevel@tonic-gate 			}
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 	}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	sv_free(svp);
4670Sstevel@tonic-gate 	pn_free(&pn);
4680Sstevel@tonic-gate 	return (VFS_MOUNTROOT(vfsp, why));
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate int
nfs_setopts(vnode_t * vp,model_t model,struct nfs_args * buf)4720Sstevel@tonic-gate nfs_setopts(vnode_t *vp, model_t model, struct nfs_args *buf)
4730Sstevel@tonic-gate {
4740Sstevel@tonic-gate 	mntinfo_t *mi;			/* mount info, pointed at by vfs */
4750Sstevel@tonic-gate 	STRUCT_HANDLE(nfs_args, args);
4760Sstevel@tonic-gate 	int flags;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate #ifdef lint
4790Sstevel@tonic-gate 	model = model;
4800Sstevel@tonic-gate #endif
4810Sstevel@tonic-gate 
4820Sstevel@tonic-gate 	STRUCT_SET_HANDLE(args, model, buf);
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate 	flags = STRUCT_FGET(args, flags);
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	/*
4870Sstevel@tonic-gate 	 * Set option fields in mount info record
4880Sstevel@tonic-gate 	 */
4890Sstevel@tonic-gate 	mi = VTOMI(vp);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	if (flags & NFSMNT_NOAC) {
4920Sstevel@tonic-gate 		mi->mi_flags |= MI_NOAC;
4930Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 	if (flags & NFSMNT_NOCTO)
4960Sstevel@tonic-gate 		mi->mi_flags |= MI_NOCTO;
4970Sstevel@tonic-gate 	if (flags & NFSMNT_LLOCK)
4980Sstevel@tonic-gate 		mi->mi_flags |= MI_LLOCK;
4990Sstevel@tonic-gate 	if (flags & NFSMNT_GRPID)
5000Sstevel@tonic-gate 		mi->mi_flags |= MI_GRPID;
5010Sstevel@tonic-gate 	if (flags & NFSMNT_RETRANS) {
5020Sstevel@tonic-gate 		if (STRUCT_FGET(args, retrans) < 0)
5030Sstevel@tonic-gate 			return (EINVAL);
5040Sstevel@tonic-gate 		mi->mi_retrans = STRUCT_FGET(args, retrans);
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 	if (flags & NFSMNT_TIMEO) {
5070Sstevel@tonic-gate 		if (STRUCT_FGET(args, timeo) <= 0)
5080Sstevel@tonic-gate 			return (EINVAL);
5090Sstevel@tonic-gate 		mi->mi_timeo = STRUCT_FGET(args, timeo);
5100Sstevel@tonic-gate 		/*
5110Sstevel@tonic-gate 		 * The following scales the standard deviation and
5120Sstevel@tonic-gate 		 * and current retransmission timer to match the
5130Sstevel@tonic-gate 		 * initial value for the timeout specified.
5140Sstevel@tonic-gate 		 */
5150Sstevel@tonic-gate 		mi->mi_timers[NFS_CALLTYPES].rt_deviate =
5160Sstevel@tonic-gate 		    (mi->mi_timeo * hz * 2) / 5;
5170Sstevel@tonic-gate 		mi->mi_timers[NFS_CALLTYPES].rt_rtxcur =
5180Sstevel@tonic-gate 		    mi->mi_timeo * hz / 10;
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate 	if (flags & NFSMNT_RSIZE) {
5210Sstevel@tonic-gate 		if (STRUCT_FGET(args, rsize) <= 0)
5220Sstevel@tonic-gate 			return (EINVAL);
5230Sstevel@tonic-gate 		mi->mi_tsize = MIN(mi->mi_tsize, STRUCT_FGET(args, rsize));
5240Sstevel@tonic-gate 		mi->mi_curread = MIN(mi->mi_curread, mi->mi_tsize);
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 	if (flags & NFSMNT_WSIZE) {
5270Sstevel@tonic-gate 		if (STRUCT_FGET(args, wsize) <= 0)
5280Sstevel@tonic-gate 			return (EINVAL);
5290Sstevel@tonic-gate 		mi->mi_stsize = MIN(mi->mi_stsize, STRUCT_FGET(args, wsize));
5300Sstevel@tonic-gate 		mi->mi_curwrite = MIN(mi->mi_curwrite, mi->mi_stsize);
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 	if (flags & NFSMNT_ACREGMIN) {
5330Sstevel@tonic-gate 		if (STRUCT_FGET(args, acregmin) < 0)
5340Sstevel@tonic-gate 			mi->mi_acregmin = ACMINMAX;
5350Sstevel@tonic-gate 		else
5360Sstevel@tonic-gate 			mi->mi_acregmin = MIN(STRUCT_FGET(args, acregmin),
5370Sstevel@tonic-gate 			    ACMINMAX);
5380Sstevel@tonic-gate 		mi->mi_acregmin = SEC2HR(mi->mi_acregmin);
5390Sstevel@tonic-gate 	}
5400Sstevel@tonic-gate 	if (flags & NFSMNT_ACREGMAX) {
5410Sstevel@tonic-gate 		if (STRUCT_FGET(args, acregmax) < 0)
5420Sstevel@tonic-gate 			mi->mi_acregmax = ACMAXMAX;
5430Sstevel@tonic-gate 		else
5440Sstevel@tonic-gate 			mi->mi_acregmax = MIN(STRUCT_FGET(args, acregmax),
5450Sstevel@tonic-gate 			    ACMAXMAX);
5460Sstevel@tonic-gate 		mi->mi_acregmax = SEC2HR(mi->mi_acregmax);
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate 	if (flags & NFSMNT_ACDIRMIN) {
5490Sstevel@tonic-gate 		if (STRUCT_FGET(args, acdirmin) < 0)
5500Sstevel@tonic-gate 			mi->mi_acdirmin = ACMINMAX;
5510Sstevel@tonic-gate 		else
5520Sstevel@tonic-gate 			mi->mi_acdirmin = MIN(STRUCT_FGET(args, acdirmin),
5530Sstevel@tonic-gate 			    ACMINMAX);
5540Sstevel@tonic-gate 		mi->mi_acdirmin = SEC2HR(mi->mi_acdirmin);
5550Sstevel@tonic-gate 	}
5560Sstevel@tonic-gate 	if (flags & NFSMNT_ACDIRMAX) {
5570Sstevel@tonic-gate 		if (STRUCT_FGET(args, acdirmax) < 0)
5580Sstevel@tonic-gate 			mi->mi_acdirmax = ACMAXMAX;
5590Sstevel@tonic-gate 		else
5600Sstevel@tonic-gate 			mi->mi_acdirmax = MIN(STRUCT_FGET(args, acdirmax),
5610Sstevel@tonic-gate 			    ACMAXMAX);
5620Sstevel@tonic-gate 		mi->mi_acdirmax = SEC2HR(mi->mi_acdirmax);
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	if (flags & NFSMNT_LOOPBACK)
5660Sstevel@tonic-gate 		mi->mi_flags |= MI_LOOPBACK;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	return (0);
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate /*
5720Sstevel@tonic-gate  * Set or Clear direct I/O flag
5730Sstevel@tonic-gate  * VOP_RWLOCK() is held for write access to prevent a race condition
5740Sstevel@tonic-gate  * which would occur if a process is in the middle of a write when
5750Sstevel@tonic-gate  * directio flag gets set. It is possible that all pages may not get flushed.
5760Sstevel@tonic-gate  */
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate /* ARGSUSED */
5790Sstevel@tonic-gate int
nfs_directio(vnode_t * vp,int cmd,cred_t * cr)5800Sstevel@tonic-gate nfs_directio(vnode_t *vp, int cmd, cred_t *cr)
5810Sstevel@tonic-gate {
5820Sstevel@tonic-gate 	int	error = 0;
5830Sstevel@tonic-gate 	rnode_t	*rp;
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	rp = VTOR(vp);
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	if (cmd == DIRECTIO_ON) {
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 		if (rp->r_flags & RDIRECTIO)
5900Sstevel@tonic-gate 			return (0);
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 		/*
5930Sstevel@tonic-gate 		 * Flush the page cache.
5940Sstevel@tonic-gate 		 */
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 		(void) VOP_RWLOCK(vp, V_WRITELOCK_TRUE, NULL);
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 		if (rp->r_flags & RDIRECTIO) {
5990Sstevel@tonic-gate 			VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
6000Sstevel@tonic-gate 			return (0);
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 
6030Sstevel@tonic-gate 		if (vn_has_cached_data(vp) &&
6040Sstevel@tonic-gate 		    ((rp->r_flags & RDIRTY) || rp->r_awcount > 0)) {
6050Sstevel@tonic-gate 			error = VOP_PUTPAGE(vp, (offset_t)0, (uint_t)0,
6065331Samw 			    B_INVAL, cr, NULL);
6070Sstevel@tonic-gate 			if (error) {
6080Sstevel@tonic-gate 				if (error == ENOSPC || error == EDQUOT) {
6090Sstevel@tonic-gate 					mutex_enter(&rp->r_statelock);
6100Sstevel@tonic-gate 					if (!rp->r_error)
6110Sstevel@tonic-gate 						rp->r_error = error;
6120Sstevel@tonic-gate 					mutex_exit(&rp->r_statelock);
6130Sstevel@tonic-gate 				}
6140Sstevel@tonic-gate 				VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
6150Sstevel@tonic-gate 				return (error);
6160Sstevel@tonic-gate 			}
6170Sstevel@tonic-gate 		}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
6200Sstevel@tonic-gate 		rp->r_flags |= RDIRECTIO;
6210Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
6220Sstevel@tonic-gate 		VOP_RWUNLOCK(vp, V_WRITELOCK_TRUE, NULL);
6230Sstevel@tonic-gate 		return (0);
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate 	if (cmd == DIRECTIO_OFF) {
6270Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
6280Sstevel@tonic-gate 		rp->r_flags &= ~RDIRECTIO;	/* disable direct mode */
6290Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
6300Sstevel@tonic-gate 		return (0);
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	return (EINVAL);
6340Sstevel@tonic-gate }
635