xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs_vnops.c (revision a5652762e5f7bf683d19f18542e5e39df63bad79)
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
5*a5652762Spraks  * Common Development and Distribution License (the "License").
6*a5652762Spraks  * 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*a5652762Spraks  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
267c478bd9Sstevel@tonic-gate  *	All rights reserved.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/cred.h>
357c478bd9Sstevel@tonic-gate #include <sys/time.h>
367c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
377c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
387c478bd9Sstevel@tonic-gate #include <sys/file.h>
397c478bd9Sstevel@tonic-gate #include <sys/filio.h>
407c478bd9Sstevel@tonic-gate #include <sys/uio.h>
417c478bd9Sstevel@tonic-gate #include <sys/buf.h>
427c478bd9Sstevel@tonic-gate #include <sys/mman.h>
437c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
447c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
457c478bd9Sstevel@tonic-gate #include <sys/debug.h>
467c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
477c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
487c478bd9Sstevel@tonic-gate #include <sys/flock.h>
497c478bd9Sstevel@tonic-gate #include <sys/swap.h>
507c478bd9Sstevel@tonic-gate #include <sys/errno.h>
517c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
527c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
537c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
547c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
557c478bd9Sstevel@tonic-gate #include <sys/pathconf.h>
567c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
577c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
587c478bd9Sstevel@tonic-gate #include <sys/acl.h>
597c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
607c478bd9Sstevel@tonic-gate #include <sys/policy.h>
617c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <rpc/types.h>
647c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
657c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #include <nfs/nfs.h>
687c478bd9Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
697c478bd9Sstevel@tonic-gate #include <nfs/rnode.h>
707c478bd9Sstevel@tonic-gate #include <nfs/nfs_acl.h>
717c478bd9Sstevel@tonic-gate #include <nfs/lm.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #include <vm/hat.h>
747c478bd9Sstevel@tonic-gate #include <vm/as.h>
757c478bd9Sstevel@tonic-gate #include <vm/page.h>
767c478bd9Sstevel@tonic-gate #include <vm/pvn.h>
777c478bd9Sstevel@tonic-gate #include <vm/seg.h>
787c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
797c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
807c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate static int	nfs_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
877c478bd9Sstevel@tonic-gate 			cred_t *);
887c478bd9Sstevel@tonic-gate static int	nfswrite(vnode_t *, caddr_t, uint_t, int, cred_t *);
897c478bd9Sstevel@tonic-gate static int	nfsread(vnode_t *, caddr_t, uint_t, int, size_t *, cred_t *);
907c478bd9Sstevel@tonic-gate static int	nfssetattr(vnode_t *, struct vattr *, int, cred_t *);
917c478bd9Sstevel@tonic-gate static int	nfslookup_dnlc(vnode_t *, char *, vnode_t **, cred_t *);
927c478bd9Sstevel@tonic-gate static int	nfslookup_otw(vnode_t *, char *, vnode_t **, cred_t *, int);
937c478bd9Sstevel@tonic-gate static int	nfsrename(vnode_t *, char *, vnode_t *, char *, cred_t *);
947c478bd9Sstevel@tonic-gate static int	nfsreaddir(vnode_t *, rddir_cache *, cred_t *);
957c478bd9Sstevel@tonic-gate static int	nfs_bio(struct buf *, cred_t *);
967c478bd9Sstevel@tonic-gate static int	nfs_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
977c478bd9Sstevel@tonic-gate 			page_t *[], size_t, struct seg *, caddr_t,
987c478bd9Sstevel@tonic-gate 			enum seg_rw, cred_t *);
997c478bd9Sstevel@tonic-gate static void	nfs_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
1007c478bd9Sstevel@tonic-gate 			cred_t *);
1017c478bd9Sstevel@tonic-gate static int	nfs_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
1027c478bd9Sstevel@tonic-gate 			int, cred_t *);
1037c478bd9Sstevel@tonic-gate static int	nfs_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
1047c478bd9Sstevel@tonic-gate 			int, cred_t *);
1057c478bd9Sstevel@tonic-gate static void	nfs_delmap_callback(struct as *, void *, uint_t);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Error flags used to pass information about certain special errors
1097c478bd9Sstevel@tonic-gate  * which need to be handled specially.
1107c478bd9Sstevel@tonic-gate  */
1117c478bd9Sstevel@tonic-gate #define	NFS_EOF			-98
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * These are the vnode ops routines which implement the vnode interface to
1157c478bd9Sstevel@tonic-gate  * the networked file system.  These routines just take their parameters,
1167c478bd9Sstevel@tonic-gate  * make them look networkish by putting the right info into interface structs,
1177c478bd9Sstevel@tonic-gate  * and then calling the appropriate remote routine(s) to do the work.
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * Note on directory name lookup cacheing:  If we detect a stale fhandle,
1207c478bd9Sstevel@tonic-gate  * we purge the directory cache relative to that vnode.  This way, the
1217c478bd9Sstevel@tonic-gate  * user won't get burned by the cache repeatedly.  See <nfs/rnode.h> for
1227c478bd9Sstevel@tonic-gate  * more details on rnode locking.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static int	nfs_open(vnode_t **, int, cred_t *);
1267c478bd9Sstevel@tonic-gate static int	nfs_close(vnode_t *, int, int, offset_t, cred_t *);
1277c478bd9Sstevel@tonic-gate static int	nfs_read(vnode_t *, struct uio *, int, cred_t *,
1287c478bd9Sstevel@tonic-gate 			caller_context_t *);
1297c478bd9Sstevel@tonic-gate static int	nfs_write(vnode_t *, struct uio *, int, cred_t *,
1307c478bd9Sstevel@tonic-gate 			caller_context_t *);
1317c478bd9Sstevel@tonic-gate static int	nfs_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *);
1327c478bd9Sstevel@tonic-gate static int	nfs_getattr(vnode_t *, struct vattr *, int, cred_t *);
1337c478bd9Sstevel@tonic-gate static int	nfs_setattr(vnode_t *, struct vattr *, int, cred_t *,
1347c478bd9Sstevel@tonic-gate 			caller_context_t *);
1357c478bd9Sstevel@tonic-gate static int	nfs_access(vnode_t *, int, int, cred_t *);
1367c478bd9Sstevel@tonic-gate static int	nfs_accessx(void *, int, cred_t *);
1377c478bd9Sstevel@tonic-gate static int	nfs_readlink(vnode_t *, struct uio *, cred_t *);
1387c478bd9Sstevel@tonic-gate static int	nfs_fsync(vnode_t *, int, cred_t *);
1397c478bd9Sstevel@tonic-gate static void	nfs_inactive(vnode_t *, cred_t *);
1407c478bd9Sstevel@tonic-gate static int	nfs_lookup(vnode_t *, char *, vnode_t **, struct pathname *,
1417c478bd9Sstevel@tonic-gate 			int, vnode_t *, cred_t *);
1427c478bd9Sstevel@tonic-gate static int	nfs_create(vnode_t *, char *, struct vattr *, enum vcexcl,
1437c478bd9Sstevel@tonic-gate 			int, vnode_t **, cred_t *, int);
1447c478bd9Sstevel@tonic-gate static int	nfs_remove(vnode_t *, char *, cred_t *);
1457c478bd9Sstevel@tonic-gate static int	nfs_link(vnode_t *, vnode_t *, char *, cred_t *);
1467c478bd9Sstevel@tonic-gate static int	nfs_rename(vnode_t *, char *, vnode_t *, char *, cred_t *);
1477c478bd9Sstevel@tonic-gate static int	nfs_mkdir(vnode_t *, char *, struct vattr *,
1487c478bd9Sstevel@tonic-gate 			vnode_t **, cred_t *);
1497c478bd9Sstevel@tonic-gate static int	nfs_rmdir(vnode_t *, char *, vnode_t *, cred_t *);
1507c478bd9Sstevel@tonic-gate static int	nfs_symlink(vnode_t *, char *, struct vattr *, char *,
1517c478bd9Sstevel@tonic-gate 			cred_t *);
1527c478bd9Sstevel@tonic-gate static int	nfs_readdir(vnode_t *, struct uio *, cred_t *, int *);
1537c478bd9Sstevel@tonic-gate static int	nfs_fid(vnode_t *, fid_t *);
1547c478bd9Sstevel@tonic-gate static int	nfs_rwlock(vnode_t *, int, caller_context_t *);
1557c478bd9Sstevel@tonic-gate static void	nfs_rwunlock(vnode_t *, int, caller_context_t *);
1567c478bd9Sstevel@tonic-gate static int	nfs_seek(vnode_t *, offset_t, offset_t *);
1577c478bd9Sstevel@tonic-gate static int	nfs_getpage(vnode_t *, offset_t, size_t, uint_t *,
1587c478bd9Sstevel@tonic-gate 			page_t *[], size_t, struct seg *, caddr_t,
1597c478bd9Sstevel@tonic-gate 			enum seg_rw, cred_t *);
1607c478bd9Sstevel@tonic-gate static int	nfs_putpage(vnode_t *, offset_t, size_t, int, cred_t *);
1617c478bd9Sstevel@tonic-gate static int	nfs_map(vnode_t *, offset_t, struct as *, caddr_t *,
1627c478bd9Sstevel@tonic-gate 			size_t, uchar_t, uchar_t, uint_t, cred_t *);
1637c478bd9Sstevel@tonic-gate static int	nfs_addmap(vnode_t *, offset_t, struct as *, caddr_t,
1647c478bd9Sstevel@tonic-gate 			size_t, uchar_t, uchar_t, uint_t, cred_t *);
1657c478bd9Sstevel@tonic-gate static int	nfs_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
1667c478bd9Sstevel@tonic-gate 			struct flk_callback *, cred_t *);
1677c478bd9Sstevel@tonic-gate static int	nfs_space(vnode_t *, int, struct flock64 *, int, offset_t,
1687c478bd9Sstevel@tonic-gate 			cred_t *, caller_context_t *);
1697c478bd9Sstevel@tonic-gate static int	nfs_realvp(vnode_t *, vnode_t **);
1707c478bd9Sstevel@tonic-gate static int	nfs_delmap(vnode_t *, offset_t, struct as *, caddr_t,
1717c478bd9Sstevel@tonic-gate 			size_t, uint_t, uint_t, uint_t, cred_t *);
1727c478bd9Sstevel@tonic-gate static int	nfs_pathconf(vnode_t *, int, ulong_t *, cred_t *);
1737c478bd9Sstevel@tonic-gate static int	nfs_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
1747c478bd9Sstevel@tonic-gate 			cred_t *);
1757c478bd9Sstevel@tonic-gate static int	nfs_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *);
1767c478bd9Sstevel@tonic-gate static int	nfs_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *);
1777c478bd9Sstevel@tonic-gate static int	nfs_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate struct vnodeops *nfs_vnodeops;
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate const fs_operation_def_t nfs_vnodeops_template[] = {
1827c478bd9Sstevel@tonic-gate 	VOPNAME_OPEN, nfs_open,
1837c478bd9Sstevel@tonic-gate 	VOPNAME_CLOSE, nfs_close,
1847c478bd9Sstevel@tonic-gate 	VOPNAME_READ, nfs_read,
1857c478bd9Sstevel@tonic-gate 	VOPNAME_WRITE, nfs_write,
1867c478bd9Sstevel@tonic-gate 	VOPNAME_IOCTL, nfs_ioctl,
1877c478bd9Sstevel@tonic-gate 	VOPNAME_GETATTR, nfs_getattr,
1887c478bd9Sstevel@tonic-gate 	VOPNAME_SETATTR, nfs_setattr,
1897c478bd9Sstevel@tonic-gate 	VOPNAME_ACCESS, nfs_access,
1907c478bd9Sstevel@tonic-gate 	VOPNAME_LOOKUP, nfs_lookup,
1917c478bd9Sstevel@tonic-gate 	VOPNAME_CREATE, nfs_create,
1927c478bd9Sstevel@tonic-gate 	VOPNAME_REMOVE, nfs_remove,
1937c478bd9Sstevel@tonic-gate 	VOPNAME_LINK, nfs_link,
1947c478bd9Sstevel@tonic-gate 	VOPNAME_RENAME, nfs_rename,
1957c478bd9Sstevel@tonic-gate 	VOPNAME_MKDIR, nfs_mkdir,
1967c478bd9Sstevel@tonic-gate 	VOPNAME_RMDIR, nfs_rmdir,
1977c478bd9Sstevel@tonic-gate 	VOPNAME_READDIR, nfs_readdir,
1987c478bd9Sstevel@tonic-gate 	VOPNAME_SYMLINK, nfs_symlink,
1997c478bd9Sstevel@tonic-gate 	VOPNAME_READLINK, nfs_readlink,
2007c478bd9Sstevel@tonic-gate 	VOPNAME_FSYNC, nfs_fsync,
2017c478bd9Sstevel@tonic-gate 	VOPNAME_INACTIVE, (fs_generic_func_p) nfs_inactive,
2027c478bd9Sstevel@tonic-gate 	VOPNAME_FID, nfs_fid,
2037c478bd9Sstevel@tonic-gate 	VOPNAME_RWLOCK, nfs_rwlock,
2047c478bd9Sstevel@tonic-gate 	VOPNAME_RWUNLOCK, (fs_generic_func_p) nfs_rwunlock,
2057c478bd9Sstevel@tonic-gate 	VOPNAME_SEEK, nfs_seek,
2067c478bd9Sstevel@tonic-gate 	VOPNAME_FRLOCK, nfs_frlock,
2077c478bd9Sstevel@tonic-gate 	VOPNAME_SPACE, nfs_space,
2087c478bd9Sstevel@tonic-gate 	VOPNAME_REALVP, nfs_realvp,
2097c478bd9Sstevel@tonic-gate 	VOPNAME_GETPAGE, nfs_getpage,
2107c478bd9Sstevel@tonic-gate 	VOPNAME_PUTPAGE, nfs_putpage,
2117c478bd9Sstevel@tonic-gate 	VOPNAME_MAP, (fs_generic_func_p) nfs_map,
2127c478bd9Sstevel@tonic-gate 	VOPNAME_ADDMAP, (fs_generic_func_p) nfs_addmap,
2137c478bd9Sstevel@tonic-gate 	VOPNAME_DELMAP, nfs_delmap,
2147c478bd9Sstevel@tonic-gate 	VOPNAME_DUMP, nfs_dump,
2157c478bd9Sstevel@tonic-gate 	VOPNAME_PATHCONF, nfs_pathconf,
2167c478bd9Sstevel@tonic-gate 	VOPNAME_PAGEIO, nfs_pageio,
2177c478bd9Sstevel@tonic-gate 	VOPNAME_SETSECATTR, nfs_setsecattr,
2187c478bd9Sstevel@tonic-gate 	VOPNAME_GETSECATTR, nfs_getsecattr,
2197c478bd9Sstevel@tonic-gate 	VOPNAME_SHRLOCK, nfs_shrlock,
2207c478bd9Sstevel@tonic-gate 	NULL, NULL
2217c478bd9Sstevel@tonic-gate };
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * XXX:  This is referenced in modstubs.s
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate struct vnodeops *
2277c478bd9Sstevel@tonic-gate nfs_getvnodeops(void)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	return (nfs_vnodeops);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate /* ARGSUSED */
2337c478bd9Sstevel@tonic-gate static int
2347c478bd9Sstevel@tonic-gate nfs_open(vnode_t **vpp, int flag, cred_t *cr)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	int error;
2377c478bd9Sstevel@tonic-gate 	struct vattr va;
2387c478bd9Sstevel@tonic-gate 	rnode_t *rp;
2397c478bd9Sstevel@tonic-gate 	vnode_t *vp;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	vp = *vpp;
2427c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
243108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
2447c478bd9Sstevel@tonic-gate 		return (EIO);
2457c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
2467c478bd9Sstevel@tonic-gate 	if (rp->r_cred == NULL) {
2477c478bd9Sstevel@tonic-gate 		crhold(cr);
2487c478bd9Sstevel@tonic-gate 		rp->r_cred = cr;
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * If there is no cached data or if close-to-open
2547c478bd9Sstevel@tonic-gate 	 * consistency checking is turned off, we can avoid
2557c478bd9Sstevel@tonic-gate 	 * the over the wire getattr.  Otherwise, if the
2567c478bd9Sstevel@tonic-gate 	 * file system is mounted readonly, then just verify
2577c478bd9Sstevel@tonic-gate 	 * the caches are up to date using the normal mechanism.
2587c478bd9Sstevel@tonic-gate 	 * Else, if the file is not mmap'd, then just mark
2597c478bd9Sstevel@tonic-gate 	 * the attributes as timed out.  They will be refreshed
2607c478bd9Sstevel@tonic-gate 	 * and the caches validated prior to being used.
2617c478bd9Sstevel@tonic-gate 	 * Else, the file system is mounted writeable so
2627c478bd9Sstevel@tonic-gate 	 * force an over the wire GETATTR in order to ensure
2637c478bd9Sstevel@tonic-gate 	 * that all cached data is valid.
2647c478bd9Sstevel@tonic-gate 	 */
2657c478bd9Sstevel@tonic-gate 	if (vp->v_count > 1 ||
2667c478bd9Sstevel@tonic-gate 	    ((vn_has_cached_data(vp) || HAVE_RDDIR_CACHE(rp)) &&
2677c478bd9Sstevel@tonic-gate 	    !(VTOMI(vp)->mi_flags & MI_NOCTO))) {
2687c478bd9Sstevel@tonic-gate 		if (vn_is_readonly(vp))
2697c478bd9Sstevel@tonic-gate 			error = nfs_validate_caches(vp, cr);
2707c478bd9Sstevel@tonic-gate 		else if (rp->r_mapcnt == 0 && vp->v_count == 1) {
2717c478bd9Sstevel@tonic-gate 			PURGE_ATTRCACHE(vp);
2727c478bd9Sstevel@tonic-gate 			error = 0;
2737c478bd9Sstevel@tonic-gate 		} else {
2747c478bd9Sstevel@tonic-gate 			va.va_mask = AT_ALL;
2757c478bd9Sstevel@tonic-gate 			error = nfs_getattr_otw(vp, &va, cr);
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 	} else
2787c478bd9Sstevel@tonic-gate 		error = 0;
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	return (error);
2817c478bd9Sstevel@tonic-gate }
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate static int
2847c478bd9Sstevel@tonic-gate nfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 	rnode_t *rp;
2877c478bd9Sstevel@tonic-gate 	int error;
2887c478bd9Sstevel@tonic-gate 	struct vattr va;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * zone_enter(2) prevents processes from changing zones with NFS files
2927c478bd9Sstevel@tonic-gate 	 * open; if we happen to get here from the wrong zone we can't do
2937c478bd9Sstevel@tonic-gate 	 * anything over the wire.
2947c478bd9Sstevel@tonic-gate 	 */
295108322fbScarlsonj 	if (VTOMI(vp)->mi_zone != nfs_zone()) {
2967c478bd9Sstevel@tonic-gate 		/*
2977c478bd9Sstevel@tonic-gate 		 * We could attempt to clean up locks, except we're sure
2987c478bd9Sstevel@tonic-gate 		 * that the current process didn't acquire any locks on
2997c478bd9Sstevel@tonic-gate 		 * the file: any attempt to lock a file belong to another zone
3007c478bd9Sstevel@tonic-gate 		 * will fail, and one can't lock an NFS file and then change
3017c478bd9Sstevel@tonic-gate 		 * zones, as that fails too.
3027c478bd9Sstevel@tonic-gate 		 *
3037c478bd9Sstevel@tonic-gate 		 * Returning an error here is the sane thing to do.  A
3047c478bd9Sstevel@tonic-gate 		 * subsequent call to VN_RELE() which translates to a
3057c478bd9Sstevel@tonic-gate 		 * nfs_inactive() will clean up state: if the zone of the
3067c478bd9Sstevel@tonic-gate 		 * vnode's origin is still alive and kicking, an async worker
3077c478bd9Sstevel@tonic-gate 		 * thread will handle the request (from the correct zone), and
3087c478bd9Sstevel@tonic-gate 		 * everything (minus the final nfs_getattr_otw() call) should
3097c478bd9Sstevel@tonic-gate 		 * be OK. If the zone is going away nfs_async_inactive() will
3107c478bd9Sstevel@tonic-gate 		 * throw away cached pages inline.
3117c478bd9Sstevel@tonic-gate 		 */
3127c478bd9Sstevel@tonic-gate 		return (EIO);
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/*
3167c478bd9Sstevel@tonic-gate 	 * If we are using local locking for this filesystem, then
3177c478bd9Sstevel@tonic-gate 	 * release all of the SYSV style record locks.  Otherwise,
3187c478bd9Sstevel@tonic-gate 	 * we are doing network locking and we need to release all
3197c478bd9Sstevel@tonic-gate 	 * of the network locks.  All of the locks held by this
3207c478bd9Sstevel@tonic-gate 	 * process on this file are released no matter what the
3217c478bd9Sstevel@tonic-gate 	 * incoming reference count is.
3227c478bd9Sstevel@tonic-gate 	 */
3237c478bd9Sstevel@tonic-gate 	if (VTOMI(vp)->mi_flags & MI_LLOCK) {
3247c478bd9Sstevel@tonic-gate 		cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
3257c478bd9Sstevel@tonic-gate 		cleanshares(vp, ttoproc(curthread)->p_pid);
3267c478bd9Sstevel@tonic-gate 	} else
3277c478bd9Sstevel@tonic-gate 		nfs_lockrelease(vp, flag, offset, cr);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if (count > 1)
3307c478bd9Sstevel@tonic-gate 		return (0);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * If the file has been `unlinked', then purge the
3347c478bd9Sstevel@tonic-gate 	 * DNLC so that this vnode will get reycled quicker
3357c478bd9Sstevel@tonic-gate 	 * and the .nfs* file on the server will get removed.
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
3387c478bd9Sstevel@tonic-gate 	if (rp->r_unldvp != NULL)
3397c478bd9Sstevel@tonic-gate 		dnlc_purge_vp(vp);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/*
3427c478bd9Sstevel@tonic-gate 	 * If the file was open for write and there are pages,
3437c478bd9Sstevel@tonic-gate 	 * then if the file system was mounted using the "no-close-
3447c478bd9Sstevel@tonic-gate 	 *	to-open" semantics, then start an asynchronous flush
3457c478bd9Sstevel@tonic-gate 	 *	of the all of the pages in the file.
3467c478bd9Sstevel@tonic-gate 	 * else the file system was not mounted using the "no-close-
3477c478bd9Sstevel@tonic-gate 	 *	to-open" semantics, then do a synchronous flush and
3487c478bd9Sstevel@tonic-gate 	 *	commit of all of the dirty and uncommitted pages.
3497c478bd9Sstevel@tonic-gate 	 *
3507c478bd9Sstevel@tonic-gate 	 * The asynchronous flush of the pages in the "nocto" path
3517c478bd9Sstevel@tonic-gate 	 * mostly just associates a cred pointer with the rnode so
3527c478bd9Sstevel@tonic-gate 	 * writes which happen later will have a better chance of
3537c478bd9Sstevel@tonic-gate 	 * working.  It also starts the data being written to the
3547c478bd9Sstevel@tonic-gate 	 * server, but without unnecessarily delaying the application.
3557c478bd9Sstevel@tonic-gate 	 */
3567c478bd9Sstevel@tonic-gate 	if ((flag & FWRITE) && vn_has_cached_data(vp)) {
3577c478bd9Sstevel@tonic-gate 		if ((VTOMI(vp)->mi_flags & MI_NOCTO)) {
3587c478bd9Sstevel@tonic-gate 			error = nfs_putpage(vp, (offset_t)0, 0, B_ASYNC, cr);
3597c478bd9Sstevel@tonic-gate 			if (error == EAGAIN)
3607c478bd9Sstevel@tonic-gate 				error = 0;
3617c478bd9Sstevel@tonic-gate 		} else
3627c478bd9Sstevel@tonic-gate 			error = nfs_putpage(vp, (offset_t)0, 0, 0, cr);
3637c478bd9Sstevel@tonic-gate 		if (!error) {
3647c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
3657c478bd9Sstevel@tonic-gate 			error = rp->r_error;
3667c478bd9Sstevel@tonic-gate 			rp->r_error = 0;
3677c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 	} else {
3707c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
3717c478bd9Sstevel@tonic-gate 		error = rp->r_error;
3727c478bd9Sstevel@tonic-gate 		rp->r_error = 0;
3737c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
3747c478bd9Sstevel@tonic-gate 	}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	/*
3777c478bd9Sstevel@tonic-gate 	 * If RWRITEATTR is set, then issue an over the wire GETATTR to
3787c478bd9Sstevel@tonic-gate 	 * refresh the attribute cache with a set of attributes which
3797c478bd9Sstevel@tonic-gate 	 * weren't returned from a WRITE.  This will enable the close-
3807c478bd9Sstevel@tonic-gate 	 * to-open processing to work.
3817c478bd9Sstevel@tonic-gate 	 */
3827c478bd9Sstevel@tonic-gate 	if (rp->r_flags & RWRITEATTR)
3837c478bd9Sstevel@tonic-gate 		(void) nfs_getattr_otw(vp, &va, cr);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	return (error);
3867c478bd9Sstevel@tonic-gate }
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /* ARGSUSED */
3897c478bd9Sstevel@tonic-gate static int
3907c478bd9Sstevel@tonic-gate nfs_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
3917c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 	rnode_t *rp;
3947c478bd9Sstevel@tonic-gate 	u_offset_t off;
3957c478bd9Sstevel@tonic-gate 	offset_t diff;
3967c478bd9Sstevel@tonic-gate 	int on;
3977c478bd9Sstevel@tonic-gate 	size_t n;
3987c478bd9Sstevel@tonic-gate 	caddr_t base;
3997c478bd9Sstevel@tonic-gate 	uint_t flags;
4007c478bd9Sstevel@tonic-gate 	int error;
4017c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
4047c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
4057c478bd9Sstevel@tonic-gate 
406108322fbScarlsonj 	if (nfs_zone() != mi->mi_zone)
4077c478bd9Sstevel@tonic-gate 		return (EIO);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG)
4127c478bd9Sstevel@tonic-gate 		return (EISDIR);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if (uiop->uio_resid == 0)
4157c478bd9Sstevel@tonic-gate 		return (0);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (uiop->uio_loffset > MAXOFF32_T)
4187c478bd9Sstevel@tonic-gate 		return (EFBIG);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if (uiop->uio_loffset < 0 ||
4217c478bd9Sstevel@tonic-gate 	    uiop->uio_loffset + uiop->uio_resid > MAXOFF32_T)
4227c478bd9Sstevel@tonic-gate 		return (EINVAL);
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	/*
4257c478bd9Sstevel@tonic-gate 	 * Bypass VM if caching has been disabled (e.g., locking) or if
4267c478bd9Sstevel@tonic-gate 	 * using client-side direct I/O and the file is not mmap'd and
4277c478bd9Sstevel@tonic-gate 	 * there are no cached pages.
4287c478bd9Sstevel@tonic-gate 	 */
4297c478bd9Sstevel@tonic-gate 	if ((vp->v_flag & VNOCACHE) ||
4307c478bd9Sstevel@tonic-gate 	    (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
4317c478bd9Sstevel@tonic-gate 	    rp->r_mapcnt == 0 && !vn_has_cached_data(vp))) {
4327c478bd9Sstevel@tonic-gate 		size_t bufsize;
4337c478bd9Sstevel@tonic-gate 		size_t resid = 0;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		/*
4367c478bd9Sstevel@tonic-gate 		 * Let's try to do read in as large a chunk as we can
4377c478bd9Sstevel@tonic-gate 		 * (Filesystem (NFS client) bsize if possible/needed).
4387c478bd9Sstevel@tonic-gate 		 * For V3, this is 32K and for V2, this is 8K.
4397c478bd9Sstevel@tonic-gate 		 */
4407c478bd9Sstevel@tonic-gate 		bufsize = MIN(uiop->uio_resid, VTOMI(vp)->mi_curread);
4417c478bd9Sstevel@tonic-gate 		base = kmem_alloc(bufsize, KM_SLEEP);
4427c478bd9Sstevel@tonic-gate 		do {
4437c478bd9Sstevel@tonic-gate 			n = MIN(uiop->uio_resid, bufsize);
4447c478bd9Sstevel@tonic-gate 			error = nfsread(vp, base, uiop->uio_offset, n,
4457c478bd9Sstevel@tonic-gate 			    &resid, cr);
4467c478bd9Sstevel@tonic-gate 			if (!error) {
4477c478bd9Sstevel@tonic-gate 				n -= resid;
4487c478bd9Sstevel@tonic-gate 				error = uiomove(base, n, UIO_READ, uiop);
4497c478bd9Sstevel@tonic-gate 			}
4507c478bd9Sstevel@tonic-gate 		} while (!error && uiop->uio_resid > 0 && n > 0);
4517c478bd9Sstevel@tonic-gate 		kmem_free(base, bufsize);
4527c478bd9Sstevel@tonic-gate 		return (error);
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	error = 0;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	do {
4587c478bd9Sstevel@tonic-gate 		off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
4597c478bd9Sstevel@tonic-gate 		on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
4607c478bd9Sstevel@tonic-gate 		n = MIN(MAXBSIZE - on, uiop->uio_resid);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		error = nfs_validate_caches(vp, cr);
4637c478bd9Sstevel@tonic-gate 		if (error)
4647c478bd9Sstevel@tonic-gate 			break;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
4677c478bd9Sstevel@tonic-gate 		diff = rp->r_size - uiop->uio_loffset;
4687c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
4697c478bd9Sstevel@tonic-gate 		if (diff <= 0)
4707c478bd9Sstevel@tonic-gate 			break;
4717c478bd9Sstevel@tonic-gate 		if (diff < n)
4727c478bd9Sstevel@tonic-gate 			n = (size_t)diff;
4737c478bd9Sstevel@tonic-gate 
474*a5652762Spraks 		if (vpm_enable) {
475*a5652762Spraks 			/*
476*a5652762Spraks 			 * Copy data.
477*a5652762Spraks 			 */
478*a5652762Spraks 			error = vpm_data_copy(vp, off + on, n, uiop,
479*a5652762Spraks 						1, NULL, 0, S_READ);
480*a5652762Spraks 		} else {
481*a5652762Spraks 			base = segmap_getmapflt(segkmap, vp, off + on, n,
482*a5652762Spraks 								1, S_READ);
4837c478bd9Sstevel@tonic-gate 			error = uiomove(base + on, n, UIO_READ, uiop);
484*a5652762Spraks 		}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 		if (!error) {
4877c478bd9Sstevel@tonic-gate 			/*
4887c478bd9Sstevel@tonic-gate 			 * If read a whole block or read to eof,
4897c478bd9Sstevel@tonic-gate 			 * won't need this buffer again soon.
4907c478bd9Sstevel@tonic-gate 			 */
4917c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
4927c478bd9Sstevel@tonic-gate 			if (n + on == MAXBSIZE ||
4937c478bd9Sstevel@tonic-gate 			    uiop->uio_loffset == rp->r_size)
4947c478bd9Sstevel@tonic-gate 				flags = SM_DONTNEED;
4957c478bd9Sstevel@tonic-gate 			else
4967c478bd9Sstevel@tonic-gate 				flags = 0;
4977c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
498*a5652762Spraks 			if (vpm_enable) {
499*a5652762Spraks 				error = vpm_sync_pages(vp, off, n, flags);
500*a5652762Spraks 			} else {
5017c478bd9Sstevel@tonic-gate 				error = segmap_release(segkmap, base, flags);
502*a5652762Spraks 			}
503*a5652762Spraks 		} else {
504*a5652762Spraks 			if (vpm_enable) {
505*a5652762Spraks 				(void) vpm_sync_pages(vp, off, n, 0);
506*a5652762Spraks 			} else {
5077c478bd9Sstevel@tonic-gate 				(void) segmap_release(segkmap, base, 0);
508*a5652762Spraks 			}
509*a5652762Spraks 		}
5107c478bd9Sstevel@tonic-gate 	} while (!error && uiop->uio_resid > 0);
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	return (error);
5137c478bd9Sstevel@tonic-gate }
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate /* ARGSUSED */
5167c478bd9Sstevel@tonic-gate static int
5177c478bd9Sstevel@tonic-gate nfs_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
5187c478bd9Sstevel@tonic-gate 	caller_context_t *ct)
5197c478bd9Sstevel@tonic-gate {
5207c478bd9Sstevel@tonic-gate 	rnode_t *rp;
5217c478bd9Sstevel@tonic-gate 	u_offset_t off;
5227c478bd9Sstevel@tonic-gate 	caddr_t base;
5237c478bd9Sstevel@tonic-gate 	uint_t flags;
5247c478bd9Sstevel@tonic-gate 	int remainder;
5257c478bd9Sstevel@tonic-gate 	size_t n;
5267c478bd9Sstevel@tonic-gate 	int on;
5277c478bd9Sstevel@tonic-gate 	int error;
5287c478bd9Sstevel@tonic-gate 	int resid;
5297c478bd9Sstevel@tonic-gate 	offset_t offset;
5307c478bd9Sstevel@tonic-gate 	rlim_t limit;
5317c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
536108322fbScarlsonj 	if (nfs_zone() != mi->mi_zone)
5377c478bd9Sstevel@tonic-gate 		return (EIO);
5387c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG)
5397c478bd9Sstevel@tonic-gate 		return (EISDIR);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	if (uiop->uio_resid == 0)
5427c478bd9Sstevel@tonic-gate 		return (0);
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (ioflag & FAPPEND) {
5457c478bd9Sstevel@tonic-gate 		struct vattr va;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		/*
5487c478bd9Sstevel@tonic-gate 		 * Must serialize if appending.
5497c478bd9Sstevel@tonic-gate 		 */
5507c478bd9Sstevel@tonic-gate 		if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
5517c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&rp->r_rwlock);
5527c478bd9Sstevel@tonic-gate 			if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
5537c478bd9Sstevel@tonic-gate 			    INTR(vp)))
5547c478bd9Sstevel@tonic-gate 				return (EINTR);
5557c478bd9Sstevel@tonic-gate 		}
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 		va.va_mask = AT_SIZE;
5587c478bd9Sstevel@tonic-gate 		error = nfsgetattr(vp, &va, cr);
5597c478bd9Sstevel@tonic-gate 		if (error)
5607c478bd9Sstevel@tonic-gate 			return (error);
5617c478bd9Sstevel@tonic-gate 		uiop->uio_loffset = va.va_size;
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	if (uiop->uio_loffset > MAXOFF32_T)
5657c478bd9Sstevel@tonic-gate 		return (EFBIG);
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	offset = uiop->uio_loffset + uiop->uio_resid;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	if (uiop->uio_loffset < 0 || offset > MAXOFF32_T)
5707c478bd9Sstevel@tonic-gate 		return (EINVAL);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (uiop->uio_llimit > (rlim64_t)MAXOFF32_T) {
5737c478bd9Sstevel@tonic-gate 		limit = MAXOFF32_T;
5747c478bd9Sstevel@tonic-gate 	} else {
5757c478bd9Sstevel@tonic-gate 		limit = (rlim_t)uiop->uio_llimit;
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	/*
5797c478bd9Sstevel@tonic-gate 	 * Check to make sure that the process will not exceed
5807c478bd9Sstevel@tonic-gate 	 * its limit on file size.  It is okay to write up to
5817c478bd9Sstevel@tonic-gate 	 * the limit, but not beyond.  Thus, the write which
5827c478bd9Sstevel@tonic-gate 	 * reaches the limit will be short and the next write
5837c478bd9Sstevel@tonic-gate 	 * will return an error.
5847c478bd9Sstevel@tonic-gate 	 */
5857c478bd9Sstevel@tonic-gate 	remainder = 0;
5867c478bd9Sstevel@tonic-gate 	if (offset > limit) {
5877c478bd9Sstevel@tonic-gate 		remainder = offset - limit;
5887c478bd9Sstevel@tonic-gate 		uiop->uio_resid = limit - uiop->uio_offset;
5897c478bd9Sstevel@tonic-gate 		if (uiop->uio_resid <= 0) {
5907c478bd9Sstevel@tonic-gate 			proc_t *p = ttoproc(curthread);
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 			uiop->uio_resid += remainder;
5937c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
5947c478bd9Sstevel@tonic-gate 			(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
5957c478bd9Sstevel@tonic-gate 			    p->p_rctls, p, RCA_UNSAFE_SIGINFO);
5967c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
5977c478bd9Sstevel@tonic-gate 			return (EFBIG);
5987c478bd9Sstevel@tonic-gate 		}
5997c478bd9Sstevel@tonic-gate 	}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp)))
6027c478bd9Sstevel@tonic-gate 		return (EINTR);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	/*
6057c478bd9Sstevel@tonic-gate 	 * Bypass VM if caching has been disabled (e.g., locking) or if
6067c478bd9Sstevel@tonic-gate 	 * using client-side direct I/O and the file is not mmap'd and
6077c478bd9Sstevel@tonic-gate 	 * there are no cached pages.
6087c478bd9Sstevel@tonic-gate 	 */
6097c478bd9Sstevel@tonic-gate 	if ((vp->v_flag & VNOCACHE) ||
6107c478bd9Sstevel@tonic-gate 	    (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
6117c478bd9Sstevel@tonic-gate 	    rp->r_mapcnt == 0 && !vn_has_cached_data(vp))) {
6127c478bd9Sstevel@tonic-gate 		size_t bufsize;
6137c478bd9Sstevel@tonic-gate 		int count;
6147c478bd9Sstevel@tonic-gate 		uint_t org_offset;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate nfs_fwrite:
6177c478bd9Sstevel@tonic-gate 		if (rp->r_flags & RSTALE) {
6187c478bd9Sstevel@tonic-gate 			resid = uiop->uio_resid;
6197c478bd9Sstevel@tonic-gate 			offset = uiop->uio_loffset;
6207c478bd9Sstevel@tonic-gate 			error = rp->r_error;
6217c478bd9Sstevel@tonic-gate 			goto bottom;
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 		bufsize = MIN(uiop->uio_resid, mi->mi_curwrite);
6247c478bd9Sstevel@tonic-gate 		base = kmem_alloc(bufsize, KM_SLEEP);
6257c478bd9Sstevel@tonic-gate 		do {
6267c478bd9Sstevel@tonic-gate 			resid = uiop->uio_resid;
6277c478bd9Sstevel@tonic-gate 			offset = uiop->uio_loffset;
6287c478bd9Sstevel@tonic-gate 			count = MIN(uiop->uio_resid, bufsize);
6297c478bd9Sstevel@tonic-gate 			org_offset = uiop->uio_offset;
6307c478bd9Sstevel@tonic-gate 			error = uiomove(base, count, UIO_WRITE, uiop);
6317c478bd9Sstevel@tonic-gate 			if (!error) {
6327c478bd9Sstevel@tonic-gate 				error = nfswrite(vp, base, org_offset,
6337c478bd9Sstevel@tonic-gate 				    count, cr);
6347c478bd9Sstevel@tonic-gate 			}
6357c478bd9Sstevel@tonic-gate 		} while (!error && uiop->uio_resid > 0);
6367c478bd9Sstevel@tonic-gate 		kmem_free(base, bufsize);
6377c478bd9Sstevel@tonic-gate 		goto bottom;
6387c478bd9Sstevel@tonic-gate 	}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	do {
6417c478bd9Sstevel@tonic-gate 		off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
6427c478bd9Sstevel@tonic-gate 		on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
6437c478bd9Sstevel@tonic-gate 		n = MIN(MAXBSIZE - on, uiop->uio_resid);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		resid = uiop->uio_resid;
6467c478bd9Sstevel@tonic-gate 		offset = uiop->uio_loffset;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		if (rp->r_flags & RSTALE) {
6497c478bd9Sstevel@tonic-gate 			error = rp->r_error;
6507c478bd9Sstevel@tonic-gate 			break;
6517c478bd9Sstevel@tonic-gate 		}
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 		/*
6547c478bd9Sstevel@tonic-gate 		 * Don't create dirty pages faster than they
6557c478bd9Sstevel@tonic-gate 		 * can be cleaned so that the system doesn't
6567c478bd9Sstevel@tonic-gate 		 * get imbalanced.  If the async queue is
6577c478bd9Sstevel@tonic-gate 		 * maxed out, then wait for it to drain before
6587c478bd9Sstevel@tonic-gate 		 * creating more dirty pages.  Also, wait for
6597c478bd9Sstevel@tonic-gate 		 * any threads doing pagewalks in the vop_getattr
6607c478bd9Sstevel@tonic-gate 		 * entry points so that they don't block for
6617c478bd9Sstevel@tonic-gate 		 * long periods.
6627c478bd9Sstevel@tonic-gate 		 */
6637c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
6647c478bd9Sstevel@tonic-gate 		while ((mi->mi_max_threads != 0 &&
6657c478bd9Sstevel@tonic-gate 		    rp->r_awcount > 2 * mi->mi_max_threads) ||
6667c478bd9Sstevel@tonic-gate 		    rp->r_gcount > 0)
6677c478bd9Sstevel@tonic-gate 			cv_wait(&rp->r_cv, &rp->r_statelock);
6687c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
6697c478bd9Sstevel@tonic-gate 
670*a5652762Spraks 		if (vpm_enable) {
671*a5652762Spraks 			/*
672*a5652762Spraks 			 * It will use kpm mappings, so no need to
673*a5652762Spraks 			 * pass an address.
674*a5652762Spraks 			 */
675*a5652762Spraks 			error = writerp(rp, NULL, n, uiop, 0);
676*a5652762Spraks 		} else  {
6777c478bd9Sstevel@tonic-gate 			if (segmap_kpm) {
6787c478bd9Sstevel@tonic-gate 				int pon = uiop->uio_loffset & PAGEOFFSET;
679*a5652762Spraks 				size_t pn = MIN(PAGESIZE - pon,
680*a5652762Spraks 							uiop->uio_resid);
6817c478bd9Sstevel@tonic-gate 				int pagecreate;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 				mutex_enter(&rp->r_statelock);
6847c478bd9Sstevel@tonic-gate 				pagecreate = (pon == 0) && (pn == PAGESIZE ||
6857c478bd9Sstevel@tonic-gate 					uiop->uio_loffset + pn >= rp->r_size);
6867c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 				base = segmap_getmapflt(segkmap, vp, off + on,
6897c478bd9Sstevel@tonic-gate 						pn, !pagecreate, S_WRITE);
6907c478bd9Sstevel@tonic-gate 
691*a5652762Spraks 				error = writerp(rp, base + pon, n, uiop,
692*a5652762Spraks 								pagecreate);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 			} else {
6957c478bd9Sstevel@tonic-gate 				base = segmap_getmapflt(segkmap, vp, off + on,
6967c478bd9Sstevel@tonic-gate 							n, 0, S_READ);
6977c478bd9Sstevel@tonic-gate 				error = writerp(rp, base + on, n, uiop, 0);
6987c478bd9Sstevel@tonic-gate 			}
699*a5652762Spraks 		}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 		if (!error) {
7027c478bd9Sstevel@tonic-gate 			if (mi->mi_flags & MI_NOAC)
7037c478bd9Sstevel@tonic-gate 				flags = SM_WRITE;
7047c478bd9Sstevel@tonic-gate 			else if (n + on == MAXBSIZE || IS_SWAPVP(vp)) {
7057c478bd9Sstevel@tonic-gate 				/*
7067c478bd9Sstevel@tonic-gate 				 * Have written a whole block.
7077c478bd9Sstevel@tonic-gate 				 * Start an asynchronous write
7087c478bd9Sstevel@tonic-gate 				 * and mark the buffer to
7097c478bd9Sstevel@tonic-gate 				 * indicate that it won't be
7107c478bd9Sstevel@tonic-gate 				 * needed again soon.
7117c478bd9Sstevel@tonic-gate 				 */
7127c478bd9Sstevel@tonic-gate 				flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
7137c478bd9Sstevel@tonic-gate 			} else
7147c478bd9Sstevel@tonic-gate 				flags = 0;
7157c478bd9Sstevel@tonic-gate 			if ((ioflag & (FSYNC|FDSYNC)) ||
7167c478bd9Sstevel@tonic-gate 			    (rp->r_flags & ROUTOFSPACE)) {
7177c478bd9Sstevel@tonic-gate 				flags &= ~SM_ASYNC;
7187c478bd9Sstevel@tonic-gate 				flags |= SM_WRITE;
7197c478bd9Sstevel@tonic-gate 			}
720*a5652762Spraks 			if (vpm_enable) {
721*a5652762Spraks 				error = vpm_sync_pages(vp, off, n, flags);
722*a5652762Spraks 			} else {
7237c478bd9Sstevel@tonic-gate 				error = segmap_release(segkmap, base, flags);
724*a5652762Spraks 			}
725*a5652762Spraks 		} else {
726*a5652762Spraks 			if (vpm_enable) {
727*a5652762Spraks 				(void) vpm_sync_pages(vp, off, n, 0);
7287c478bd9Sstevel@tonic-gate 			} else {
7297c478bd9Sstevel@tonic-gate 				(void) segmap_release(segkmap, base, 0);
730*a5652762Spraks 			}
7317c478bd9Sstevel@tonic-gate 			/*
7327c478bd9Sstevel@tonic-gate 			 * In the event that we got an access error while
7337c478bd9Sstevel@tonic-gate 			 * faulting in a page for a write-only file just
7347c478bd9Sstevel@tonic-gate 			 * force a write.
7357c478bd9Sstevel@tonic-gate 			 */
7367c478bd9Sstevel@tonic-gate 			if (error == EACCES)
7377c478bd9Sstevel@tonic-gate 				goto nfs_fwrite;
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 	} while (!error && uiop->uio_resid > 0);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate bottom:
7427c478bd9Sstevel@tonic-gate 	if (error) {
7437c478bd9Sstevel@tonic-gate 		uiop->uio_resid = resid + remainder;
7447c478bd9Sstevel@tonic-gate 		uiop->uio_loffset = offset;
7457c478bd9Sstevel@tonic-gate 	} else
7467c478bd9Sstevel@tonic-gate 		uiop->uio_resid += remainder;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&rp->r_lkserlock);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	return (error);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate /*
7547c478bd9Sstevel@tonic-gate  * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
7557c478bd9Sstevel@tonic-gate  */
7567c478bd9Sstevel@tonic-gate static int
7577c478bd9Sstevel@tonic-gate nfs_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
7587c478bd9Sstevel@tonic-gate 	int flags, cred_t *cr)
7597c478bd9Sstevel@tonic-gate {
7607c478bd9Sstevel@tonic-gate 	struct buf *bp;
7617c478bd9Sstevel@tonic-gate 	int error;
7627c478bd9Sstevel@tonic-gate 
763108322fbScarlsonj 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
7647c478bd9Sstevel@tonic-gate 	bp = pageio_setup(pp, len, vp, flags);
7657c478bd9Sstevel@tonic-gate 	ASSERT(bp != NULL);
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	/*
7687c478bd9Sstevel@tonic-gate 	 * pageio_setup should have set b_addr to 0.  This
7697c478bd9Sstevel@tonic-gate 	 * is correct since we want to do I/O on a page
7707c478bd9Sstevel@tonic-gate 	 * boundary.  bp_mapin will use this addr to calculate
7717c478bd9Sstevel@tonic-gate 	 * an offset, and then set b_addr to the kernel virtual
7727c478bd9Sstevel@tonic-gate 	 * address it allocated for us.
7737c478bd9Sstevel@tonic-gate 	 */
7747c478bd9Sstevel@tonic-gate 	ASSERT(bp->b_un.b_addr == 0);
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	bp->b_edev = 0;
7777c478bd9Sstevel@tonic-gate 	bp->b_dev = 0;
7787c478bd9Sstevel@tonic-gate 	bp->b_lblkno = lbtodb(off);
7797c478bd9Sstevel@tonic-gate 	bp->b_file = vp;
7807c478bd9Sstevel@tonic-gate 	bp->b_offset = (offset_t)off;
7817c478bd9Sstevel@tonic-gate 	bp_mapin(bp);
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	error = nfs_bio(bp, cr);
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	bp_mapout(bp);
7867c478bd9Sstevel@tonic-gate 	pageio_done(bp);
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	return (error);
7897c478bd9Sstevel@tonic-gate }
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate /*
7927c478bd9Sstevel@tonic-gate  * Write to file.  Writes to remote server in largest size
7937c478bd9Sstevel@tonic-gate  * chunks that the server can handle.  Write is synchronous.
7947c478bd9Sstevel@tonic-gate  */
7957c478bd9Sstevel@tonic-gate static int
7967c478bd9Sstevel@tonic-gate nfswrite(vnode_t *vp, caddr_t base, uint_t offset, int count, cred_t *cr)
7977c478bd9Sstevel@tonic-gate {
7987c478bd9Sstevel@tonic-gate 	rnode_t *rp;
7997c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
8007c478bd9Sstevel@tonic-gate 	struct nfswriteargs wa;
8017c478bd9Sstevel@tonic-gate 	struct nfsattrstat ns;
8027c478bd9Sstevel@tonic-gate 	int error;
8037c478bd9Sstevel@tonic-gate 	int tsize;
8047c478bd9Sstevel@tonic-gate 	int douprintf;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	douprintf = 1;
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
8097c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
8107c478bd9Sstevel@tonic-gate 
811108322fbScarlsonj 	ASSERT(nfs_zone() == mi->mi_zone);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	wa.wa_args = &wa.wa_args_buf;
8147c478bd9Sstevel@tonic-gate 	wa.wa_fhandle = *VTOFH(vp);
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	do {
8177c478bd9Sstevel@tonic-gate 		tsize = MIN(mi->mi_curwrite, count);
8187c478bd9Sstevel@tonic-gate 		wa.wa_data = base;
8197c478bd9Sstevel@tonic-gate 		wa.wa_begoff = offset;
8207c478bd9Sstevel@tonic-gate 		wa.wa_totcount = tsize;
8217c478bd9Sstevel@tonic-gate 		wa.wa_count = tsize;
8227c478bd9Sstevel@tonic-gate 		wa.wa_offset = offset;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 		if (mi->mi_io_kstats) {
8257c478bd9Sstevel@tonic-gate 			mutex_enter(&mi->mi_lock);
8267c478bd9Sstevel@tonic-gate 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
8277c478bd9Sstevel@tonic-gate 			mutex_exit(&mi->mi_lock);
8287c478bd9Sstevel@tonic-gate 		}
8297c478bd9Sstevel@tonic-gate 		wa.wa_mblk = NULL;
8307c478bd9Sstevel@tonic-gate 		do {
8317c478bd9Sstevel@tonic-gate 			error = rfs2call(mi, RFS_WRITE,
8327c478bd9Sstevel@tonic-gate 			    xdr_writeargs, (caddr_t)&wa,
8337c478bd9Sstevel@tonic-gate 			    xdr_attrstat, (caddr_t)&ns, cr,
8347c478bd9Sstevel@tonic-gate 			    &douprintf, &ns.ns_status, 0, NULL);
8357c478bd9Sstevel@tonic-gate 		} while (error == ENFS_TRYAGAIN);
8367c478bd9Sstevel@tonic-gate 		if (mi->mi_io_kstats) {
8377c478bd9Sstevel@tonic-gate 			mutex_enter(&mi->mi_lock);
8387c478bd9Sstevel@tonic-gate 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
8397c478bd9Sstevel@tonic-gate 			mutex_exit(&mi->mi_lock);
8407c478bd9Sstevel@tonic-gate 		}
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 		if (!error) {
8437c478bd9Sstevel@tonic-gate 			error = geterrno(ns.ns_status);
8447c478bd9Sstevel@tonic-gate 			/*
8457c478bd9Sstevel@tonic-gate 			 * Can't check for stale fhandle and purge caches
8467c478bd9Sstevel@tonic-gate 			 * here because pages are held by nfs_getpage.
8477c478bd9Sstevel@tonic-gate 			 * Just mark the attribute cache as timed out
8487c478bd9Sstevel@tonic-gate 			 * and set RWRITEATTR to indicate that the file
8497c478bd9Sstevel@tonic-gate 			 * was modified with a WRITE operation.
8507c478bd9Sstevel@tonic-gate 			 */
8517c478bd9Sstevel@tonic-gate 			if (!error) {
8527c478bd9Sstevel@tonic-gate 				count -= tsize;
8537c478bd9Sstevel@tonic-gate 				base += tsize;
8547c478bd9Sstevel@tonic-gate 				offset += tsize;
8557c478bd9Sstevel@tonic-gate 				if (mi->mi_io_kstats) {
8567c478bd9Sstevel@tonic-gate 					mutex_enter(&mi->mi_lock);
8577c478bd9Sstevel@tonic-gate 				    KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
8587c478bd9Sstevel@tonic-gate 				    KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
8597c478bd9Sstevel@tonic-gate 					    tsize;
8607c478bd9Sstevel@tonic-gate 					mutex_exit(&mi->mi_lock);
8617c478bd9Sstevel@tonic-gate 				}
8627c478bd9Sstevel@tonic-gate 				lwp_stat_update(LWP_STAT_OUBLK, 1);
8637c478bd9Sstevel@tonic-gate 				mutex_enter(&rp->r_statelock);
8647c478bd9Sstevel@tonic-gate 				PURGE_ATTRCACHE_LOCKED(rp);
8657c478bd9Sstevel@tonic-gate 				rp->r_flags |= RWRITEATTR;
8667c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
8677c478bd9Sstevel@tonic-gate 			}
8687c478bd9Sstevel@tonic-gate 		}
8697c478bd9Sstevel@tonic-gate 	} while (!error && count);
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	return (error);
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate /*
8757c478bd9Sstevel@tonic-gate  * Read from a file.  Reads data in largest chunks our interface can handle.
8767c478bd9Sstevel@tonic-gate  */
8777c478bd9Sstevel@tonic-gate static int
8787c478bd9Sstevel@tonic-gate nfsread(vnode_t *vp, caddr_t base, uint_t offset, int count, size_t *residp,
8797c478bd9Sstevel@tonic-gate 	cred_t *cr)
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
8827c478bd9Sstevel@tonic-gate 	struct nfsreadargs ra;
8837c478bd9Sstevel@tonic-gate 	struct nfsrdresult rr;
8847c478bd9Sstevel@tonic-gate 	int tsize;
8857c478bd9Sstevel@tonic-gate 	int error;
8867c478bd9Sstevel@tonic-gate 	int douprintf;
8877c478bd9Sstevel@tonic-gate 	failinfo_t fi;
8887c478bd9Sstevel@tonic-gate 	rnode_t *rp;
8897c478bd9Sstevel@tonic-gate 	struct vattr va;
8907c478bd9Sstevel@tonic-gate 	hrtime_t t;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
8937c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
8947c478bd9Sstevel@tonic-gate 
895108322fbScarlsonj 	ASSERT(nfs_zone() == mi->mi_zone);
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	douprintf = 1;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	ra.ra_fhandle = *VTOFH(vp);
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	fi.vp = vp;
9027c478bd9Sstevel@tonic-gate 	fi.fhp = (caddr_t)&ra.ra_fhandle;
9037c478bd9Sstevel@tonic-gate 	fi.copyproc = nfscopyfh;
9047c478bd9Sstevel@tonic-gate 	fi.lookupproc = nfslookup;
9057c478bd9Sstevel@tonic-gate 	fi.xattrdirproc = acl_getxattrdir2;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	do {
9087c478bd9Sstevel@tonic-gate 		if (mi->mi_io_kstats) {
9097c478bd9Sstevel@tonic-gate 			mutex_enter(&mi->mi_lock);
9107c478bd9Sstevel@tonic-gate 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
9117c478bd9Sstevel@tonic-gate 			mutex_exit(&mi->mi_lock);
9127c478bd9Sstevel@tonic-gate 		}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 		do {
9157c478bd9Sstevel@tonic-gate 			tsize = MIN(mi->mi_curread, count);
9167c478bd9Sstevel@tonic-gate 			rr.rr_data = base;
9177c478bd9Sstevel@tonic-gate 			ra.ra_offset = offset;
9187c478bd9Sstevel@tonic-gate 			ra.ra_totcount = tsize;
9197c478bd9Sstevel@tonic-gate 			ra.ra_count = tsize;
9207c478bd9Sstevel@tonic-gate 			t = gethrtime();
9217c478bd9Sstevel@tonic-gate 			error = rfs2call(mi, RFS_READ,
9227c478bd9Sstevel@tonic-gate 			    xdr_readargs, (caddr_t)&ra,
9237c478bd9Sstevel@tonic-gate 			    xdr_rdresult, (caddr_t)&rr, cr,
9247c478bd9Sstevel@tonic-gate 			    &douprintf, &rr.rr_status, 0, &fi);
9257c478bd9Sstevel@tonic-gate 		} while (error == ENFS_TRYAGAIN);
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		if (mi->mi_io_kstats) {
9287c478bd9Sstevel@tonic-gate 			mutex_enter(&mi->mi_lock);
9297c478bd9Sstevel@tonic-gate 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
9307c478bd9Sstevel@tonic-gate 			mutex_exit(&mi->mi_lock);
9317c478bd9Sstevel@tonic-gate 		}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 		if (!error) {
9347c478bd9Sstevel@tonic-gate 			error = geterrno(rr.rr_status);
9357c478bd9Sstevel@tonic-gate 			if (!error) {
9367c478bd9Sstevel@tonic-gate 				count -= rr.rr_count;
9377c478bd9Sstevel@tonic-gate 				base += rr.rr_count;
9387c478bd9Sstevel@tonic-gate 				offset += rr.rr_count;
9397c478bd9Sstevel@tonic-gate 				if (mi->mi_io_kstats) {
9407c478bd9Sstevel@tonic-gate 					mutex_enter(&mi->mi_lock);
9417c478bd9Sstevel@tonic-gate 					KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
9427c478bd9Sstevel@tonic-gate 					KSTAT_IO_PTR(mi->mi_io_kstats)->nread +=
9437c478bd9Sstevel@tonic-gate 					    rr.rr_count;
9447c478bd9Sstevel@tonic-gate 					mutex_exit(&mi->mi_lock);
9457c478bd9Sstevel@tonic-gate 				}
9467c478bd9Sstevel@tonic-gate 				lwp_stat_update(LWP_STAT_INBLK, 1);
9477c478bd9Sstevel@tonic-gate 			}
9487c478bd9Sstevel@tonic-gate 		}
9497c478bd9Sstevel@tonic-gate 	} while (!error && count && rr.rr_count == tsize);
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	*residp = count;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	if (!error) {
9547c478bd9Sstevel@tonic-gate 		/*
9557c478bd9Sstevel@tonic-gate 		 * Since no error occurred, we have the current
9567c478bd9Sstevel@tonic-gate 		 * attributes and we need to do a cache check and then
9577c478bd9Sstevel@tonic-gate 		 * potentially update the cached attributes.  We can't
9587c478bd9Sstevel@tonic-gate 		 * use the normal attribute check and cache mechanisms
9597c478bd9Sstevel@tonic-gate 		 * because they might cause a cache flush which would
9607c478bd9Sstevel@tonic-gate 		 * deadlock.  Instead, we just check the cache to see
9617c478bd9Sstevel@tonic-gate 		 * if the attributes have changed.  If it is, then we
9627c478bd9Sstevel@tonic-gate 		 * just mark the attributes as out of date.  The next
9637c478bd9Sstevel@tonic-gate 		 * time that the attributes are checked, they will be
9647c478bd9Sstevel@tonic-gate 		 * out of date, new attributes will be fetched, and
9657c478bd9Sstevel@tonic-gate 		 * the page cache will be flushed.  If the attributes
9667c478bd9Sstevel@tonic-gate 		 * weren't changed, then we just update the cached
9677c478bd9Sstevel@tonic-gate 		 * attributes with these attributes.
9687c478bd9Sstevel@tonic-gate 		 */
9697c478bd9Sstevel@tonic-gate 		/*
9707c478bd9Sstevel@tonic-gate 		 * If NFS_ACL is supported on the server, then the
9717c478bd9Sstevel@tonic-gate 		 * attributes returned by server may have minimal
9727c478bd9Sstevel@tonic-gate 		 * permissions sometimes denying access to users having
9737c478bd9Sstevel@tonic-gate 		 * proper access.  To get the proper attributes, mark
9747c478bd9Sstevel@tonic-gate 		 * the attributes as expired so that they will be
9757c478bd9Sstevel@tonic-gate 		 * regotten via the NFS_ACL GETATTR2 procedure.
9767c478bd9Sstevel@tonic-gate 		 */
9777c478bd9Sstevel@tonic-gate 		error = nattr_to_vattr(vp, &rr.rr_attr, &va);
9787c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
9797c478bd9Sstevel@tonic-gate 		if (error || !CACHE_VALID(rp, va.va_mtime, va.va_size) ||
9807c478bd9Sstevel@tonic-gate 		    (mi->mi_flags & MI_ACL)) {
9817c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
9827c478bd9Sstevel@tonic-gate 			PURGE_ATTRCACHE(vp);
9837c478bd9Sstevel@tonic-gate 		} else {
9847c478bd9Sstevel@tonic-gate 			if (rp->r_mtime <= t) {
9857c478bd9Sstevel@tonic-gate 				nfs_attrcache_va(vp, &va);
9867c478bd9Sstevel@tonic-gate 			}
9877c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
9887c478bd9Sstevel@tonic-gate 		}
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	return (error);
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate /* ARGSUSED */
9957c478bd9Sstevel@tonic-gate static int
9967c478bd9Sstevel@tonic-gate nfs_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp)
9977c478bd9Sstevel@tonic-gate {
9987c478bd9Sstevel@tonic-gate 
999108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
10007c478bd9Sstevel@tonic-gate 		return (EIO);
10017c478bd9Sstevel@tonic-gate 	switch (cmd) {
10027c478bd9Sstevel@tonic-gate 		case _FIODIRECTIO:
10037c478bd9Sstevel@tonic-gate 			return (nfs_directio(vp, (int)arg, cr));
10047c478bd9Sstevel@tonic-gate 		default:
10057c478bd9Sstevel@tonic-gate 			return (ENOTTY);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate static int
10107c478bd9Sstevel@tonic-gate nfs_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr)
10117c478bd9Sstevel@tonic-gate {
10127c478bd9Sstevel@tonic-gate 	int error;
10137c478bd9Sstevel@tonic-gate 	rnode_t *rp;
10147c478bd9Sstevel@tonic-gate 
1015108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
10167c478bd9Sstevel@tonic-gate 		return (EIO);
10177c478bd9Sstevel@tonic-gate 	/*
10187c478bd9Sstevel@tonic-gate 	 * If it has been specified that the return value will
10197c478bd9Sstevel@tonic-gate 	 * just be used as a hint, and we are only being asked
10207c478bd9Sstevel@tonic-gate 	 * for size, fsid or rdevid, then return the client's
10217c478bd9Sstevel@tonic-gate 	 * notion of these values without checking to make sure
10227c478bd9Sstevel@tonic-gate 	 * that the attribute cache is up to date.
10237c478bd9Sstevel@tonic-gate 	 * The whole point is to avoid an over the wire GETATTR
10247c478bd9Sstevel@tonic-gate 	 * call.
10257c478bd9Sstevel@tonic-gate 	 */
10267c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
10277c478bd9Sstevel@tonic-gate 	if (flags & ATTR_HINT) {
10287c478bd9Sstevel@tonic-gate 		if (vap->va_mask ==
10297c478bd9Sstevel@tonic-gate 		    (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) {
10307c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
10317c478bd9Sstevel@tonic-gate 			if (vap->va_mask | AT_SIZE)
10327c478bd9Sstevel@tonic-gate 				vap->va_size = rp->r_size;
10337c478bd9Sstevel@tonic-gate 			if (vap->va_mask | AT_FSID)
10347c478bd9Sstevel@tonic-gate 				vap->va_fsid = rp->r_attr.va_fsid;
10357c478bd9Sstevel@tonic-gate 			if (vap->va_mask | AT_RDEV)
10367c478bd9Sstevel@tonic-gate 				vap->va_rdev = rp->r_attr.va_rdev;
10377c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
10387c478bd9Sstevel@tonic-gate 			return (0);
10397c478bd9Sstevel@tonic-gate 		}
10407c478bd9Sstevel@tonic-gate 	}
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	/*
10437c478bd9Sstevel@tonic-gate 	 * Only need to flush pages if asking for the mtime
10447c478bd9Sstevel@tonic-gate 	 * and if there any dirty pages or any outstanding
10457c478bd9Sstevel@tonic-gate 	 * asynchronous (write) requests for this file.
10467c478bd9Sstevel@tonic-gate 	 */
10477c478bd9Sstevel@tonic-gate 	if (vap->va_mask & AT_MTIME) {
10487c478bd9Sstevel@tonic-gate 		if (vn_has_cached_data(vp) &&
10497c478bd9Sstevel@tonic-gate 		    ((rp->r_flags & RDIRTY) || rp->r_awcount > 0)) {
10507c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
10517c478bd9Sstevel@tonic-gate 			rp->r_gcount++;
10527c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
10537c478bd9Sstevel@tonic-gate 			error = nfs_putpage(vp, (offset_t)0, 0, 0, cr);
10547c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
10557c478bd9Sstevel@tonic-gate 			if (error && (error == ENOSPC || error == EDQUOT)) {
10567c478bd9Sstevel@tonic-gate 				if (!rp->r_error)
10577c478bd9Sstevel@tonic-gate 					rp->r_error = error;
10587c478bd9Sstevel@tonic-gate 			}
10597c478bd9Sstevel@tonic-gate 			if (--rp->r_gcount == 0)
10607c478bd9Sstevel@tonic-gate 				cv_broadcast(&rp->r_cv);
10617c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
10627c478bd9Sstevel@tonic-gate 		}
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	return (nfsgetattr(vp, vap, cr));
10667c478bd9Sstevel@tonic-gate }
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate /*ARGSUSED4*/
10697c478bd9Sstevel@tonic-gate static int
10707c478bd9Sstevel@tonic-gate nfs_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
10717c478bd9Sstevel@tonic-gate 		caller_context_t *ct)
10727c478bd9Sstevel@tonic-gate {
10737c478bd9Sstevel@tonic-gate 	int error;
10747c478bd9Sstevel@tonic-gate 	uint_t mask;
10757c478bd9Sstevel@tonic-gate 	struct vattr va;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	mask = vap->va_mask;
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	if (mask & AT_NOSET)
10807c478bd9Sstevel@tonic-gate 		return (EINVAL);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	if ((mask & AT_SIZE) &&
10837c478bd9Sstevel@tonic-gate 	    vap->va_type == VREG &&
10847c478bd9Sstevel@tonic-gate 	    vap->va_size > MAXOFF32_T)
10857c478bd9Sstevel@tonic-gate 		return (EFBIG);
10867c478bd9Sstevel@tonic-gate 
1087108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
10887c478bd9Sstevel@tonic-gate 		return (EIO);
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	va.va_mask = AT_UID | AT_MODE;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	error = nfsgetattr(vp, &va, cr);
10937c478bd9Sstevel@tonic-gate 	if (error)
10947c478bd9Sstevel@tonic-gate 		return (error);
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	error = secpolicy_vnode_setattr(cr, vp, vap, &va, flags, nfs_accessx,
10977c478bd9Sstevel@tonic-gate 			vp);
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	if (error)
11007c478bd9Sstevel@tonic-gate 		return (error);
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	return (nfssetattr(vp, vap, flags, cr));
11037c478bd9Sstevel@tonic-gate }
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate static int
11067c478bd9Sstevel@tonic-gate nfssetattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr)
11077c478bd9Sstevel@tonic-gate {
11087c478bd9Sstevel@tonic-gate 	int error;
11097c478bd9Sstevel@tonic-gate 	uint_t mask;
11107c478bd9Sstevel@tonic-gate 	struct nfssaargs args;
11117c478bd9Sstevel@tonic-gate 	struct nfsattrstat ns;
11127c478bd9Sstevel@tonic-gate 	int douprintf;
11137c478bd9Sstevel@tonic-gate 	rnode_t *rp;
11147c478bd9Sstevel@tonic-gate 	struct vattr va;
11157c478bd9Sstevel@tonic-gate 	mode_t omode;
11167c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
11177c478bd9Sstevel@tonic-gate 	vsecattr_t *vsp;
11187c478bd9Sstevel@tonic-gate 	hrtime_t t;
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	mask = vap->va_mask;
11217c478bd9Sstevel@tonic-gate 
1122108322fbScarlsonj 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	/*
11277c478bd9Sstevel@tonic-gate 	 * Only need to flush pages if there are any pages and
11287c478bd9Sstevel@tonic-gate 	 * if the file is marked as dirty in some fashion.  The
11297c478bd9Sstevel@tonic-gate 	 * file must be flushed so that we can accurately
11307c478bd9Sstevel@tonic-gate 	 * determine the size of the file and the cached data
11317c478bd9Sstevel@tonic-gate 	 * after the SETATTR returns.  A file is considered to
11327c478bd9Sstevel@tonic-gate 	 * be dirty if it is either marked with RDIRTY, has
11337c478bd9Sstevel@tonic-gate 	 * outstanding i/o's active, or is mmap'd.  In this
11347c478bd9Sstevel@tonic-gate 	 * last case, we can't tell whether there are dirty
11357c478bd9Sstevel@tonic-gate 	 * pages, so we flush just to be sure.
11367c478bd9Sstevel@tonic-gate 	 */
11377c478bd9Sstevel@tonic-gate 	if (vn_has_cached_data(vp) &&
11387c478bd9Sstevel@tonic-gate 	    ((rp->r_flags & RDIRTY) ||
11397c478bd9Sstevel@tonic-gate 	    rp->r_count > 0 ||
11407c478bd9Sstevel@tonic-gate 	    rp->r_mapcnt > 0)) {
11417c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_type != VCHR);
11427c478bd9Sstevel@tonic-gate 		error = nfs_putpage(vp, (offset_t)0, 0, 0, cr);
11437c478bd9Sstevel@tonic-gate 		if (error && (error == ENOSPC || error == EDQUOT)) {
11447c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
11457c478bd9Sstevel@tonic-gate 			if (!rp->r_error)
11467c478bd9Sstevel@tonic-gate 				rp->r_error = error;
11477c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
11487c478bd9Sstevel@tonic-gate 		}
11497c478bd9Sstevel@tonic-gate 	}
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 	/*
11527c478bd9Sstevel@tonic-gate 	 * If the system call was utime(2) or utimes(2) and the
11537c478bd9Sstevel@tonic-gate 	 * application did not specify the times, then set the
11547c478bd9Sstevel@tonic-gate 	 * mtime nanosecond field to 1 billion.  This will get
11557c478bd9Sstevel@tonic-gate 	 * translated from 1 billion nanoseconds to 1 million
11567c478bd9Sstevel@tonic-gate 	 * microseconds in the over the wire request.  The
11577c478bd9Sstevel@tonic-gate 	 * server will use 1 million in the microsecond field
11587c478bd9Sstevel@tonic-gate 	 * to tell whether both the mtime and atime should be
11597c478bd9Sstevel@tonic-gate 	 * set to the server's current time.
11607c478bd9Sstevel@tonic-gate 	 *
11617c478bd9Sstevel@tonic-gate 	 * This is an overload of the protocol and should be
11627c478bd9Sstevel@tonic-gate 	 * documented in the NFS Version 2 protocol specification.
11637c478bd9Sstevel@tonic-gate 	 */
11647c478bd9Sstevel@tonic-gate 	if ((mask & AT_MTIME) && !(flags & ATTR_UTIME)) {
11657c478bd9Sstevel@tonic-gate 		vap->va_mtime.tv_nsec = 1000000000;
11667c478bd9Sstevel@tonic-gate 		if (NFS_TIME_T_OK(vap->va_mtime.tv_sec) &&
11677c478bd9Sstevel@tonic-gate 		    NFS_TIME_T_OK(vap->va_atime.tv_sec)) {
11687c478bd9Sstevel@tonic-gate 			error = vattr_to_sattr(vap, &args.saa_sa);
11697c478bd9Sstevel@tonic-gate 		} else {
11707c478bd9Sstevel@tonic-gate 			/*
11717c478bd9Sstevel@tonic-gate 			 * Use server times. vap time values will not be used.
11727c478bd9Sstevel@tonic-gate 			 * To ensure no time overflow, make sure vap has
11737c478bd9Sstevel@tonic-gate 			 * valid values, but retain the original values.
11747c478bd9Sstevel@tonic-gate 			 */
11757c478bd9Sstevel@tonic-gate 			timestruc_t	mtime = vap->va_mtime;
11767c478bd9Sstevel@tonic-gate 			timestruc_t	atime = vap->va_atime;
11777c478bd9Sstevel@tonic-gate 			time_t		now;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 			now = gethrestime_sec();
11807c478bd9Sstevel@tonic-gate 			if (NFS_TIME_T_OK(now)) {
11817c478bd9Sstevel@tonic-gate 				/* Just in case server does not know of this */
11827c478bd9Sstevel@tonic-gate 				vap->va_mtime.tv_sec = now;
11837c478bd9Sstevel@tonic-gate 				vap->va_atime.tv_sec = now;
11847c478bd9Sstevel@tonic-gate 			} else {
11857c478bd9Sstevel@tonic-gate 				vap->va_mtime.tv_sec = 0;
11867c478bd9Sstevel@tonic-gate 				vap->va_atime.tv_sec = 0;
11877c478bd9Sstevel@tonic-gate 			}
11887c478bd9Sstevel@tonic-gate 			error = vattr_to_sattr(vap, &args.saa_sa);
11897c478bd9Sstevel@tonic-gate 			/* set vap times back on */
11907c478bd9Sstevel@tonic-gate 			vap->va_mtime = mtime;
11917c478bd9Sstevel@tonic-gate 			vap->va_atime = atime;
11927c478bd9Sstevel@tonic-gate 		}
11937c478bd9Sstevel@tonic-gate 	} else {
11947c478bd9Sstevel@tonic-gate 		/* Either do not set times or use the client specified times */
11957c478bd9Sstevel@tonic-gate 		error = vattr_to_sattr(vap, &args.saa_sa);
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 	if (error) {
11987c478bd9Sstevel@tonic-gate 		/* req time field(s) overflow - return immediately */
11997c478bd9Sstevel@tonic-gate 		return (error);
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 	args.saa_fh = *VTOFH(vp);
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	va.va_mask = AT_MODE;
12047c478bd9Sstevel@tonic-gate 	error = nfsgetattr(vp, &va, cr);
12057c478bd9Sstevel@tonic-gate 	if (error)
12067c478bd9Sstevel@tonic-gate 		return (error);
12077c478bd9Sstevel@tonic-gate 	omode = va.va_mode;
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	douprintf = 1;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	t = gethrtime();
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	error = rfs2call(mi, RFS_SETATTR,
12167c478bd9Sstevel@tonic-gate 	    xdr_saargs, (caddr_t)&args,
12177c478bd9Sstevel@tonic-gate 	    xdr_attrstat, (caddr_t)&ns, cr,
12187c478bd9Sstevel@tonic-gate 	    &douprintf, &ns.ns_status, 0, NULL);
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 	/*
12217c478bd9Sstevel@tonic-gate 	 * Purge the access cache and ACL cache if changing either the
12227c478bd9Sstevel@tonic-gate 	 * owner of the file, the group owner, or the mode.  These may
12237c478bd9Sstevel@tonic-gate 	 * change the access permissions of the file, so purge old
12247c478bd9Sstevel@tonic-gate 	 * information and start over again.
12257c478bd9Sstevel@tonic-gate 	 */
12267c478bd9Sstevel@tonic-gate 	if ((mask & (AT_UID | AT_GID | AT_MODE)) && (mi->mi_flags & MI_ACL)) {
12277c478bd9Sstevel@tonic-gate 		(void) nfs_access_purge_rp(rp);
12287c478bd9Sstevel@tonic-gate 		if (rp->r_secattr != NULL) {
12297c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
12307c478bd9Sstevel@tonic-gate 			vsp = rp->r_secattr;
12317c478bd9Sstevel@tonic-gate 			rp->r_secattr = NULL;
12327c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
12337c478bd9Sstevel@tonic-gate 			if (vsp != NULL)
12347c478bd9Sstevel@tonic-gate 				nfs_acl_free(vsp);
12357c478bd9Sstevel@tonic-gate 		}
12367c478bd9Sstevel@tonic-gate 	}
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	if (!error) {
12397c478bd9Sstevel@tonic-gate 		error = geterrno(ns.ns_status);
12407c478bd9Sstevel@tonic-gate 		if (!error) {
12417c478bd9Sstevel@tonic-gate 			/*
12427c478bd9Sstevel@tonic-gate 			 * If changing the size of the file, invalidate
12437c478bd9Sstevel@tonic-gate 			 * any local cached data which is no longer part
12447c478bd9Sstevel@tonic-gate 			 * of the file.  We also possibly invalidate the
12457c478bd9Sstevel@tonic-gate 			 * last page in the file.  We could use
12467c478bd9Sstevel@tonic-gate 			 * pvn_vpzero(), but this would mark the page as
12477c478bd9Sstevel@tonic-gate 			 * modified and require it to be written back to
12487c478bd9Sstevel@tonic-gate 			 * the server for no particularly good reason.
12497c478bd9Sstevel@tonic-gate 			 * This way, if we access it, then we bring it
12507c478bd9Sstevel@tonic-gate 			 * back in.  A read should be cheaper than a
12517c478bd9Sstevel@tonic-gate 			 * write.
12527c478bd9Sstevel@tonic-gate 			 */
12537c478bd9Sstevel@tonic-gate 			if (mask & AT_SIZE) {
12547c478bd9Sstevel@tonic-gate 				nfs_invalidate_pages(vp,
12557c478bd9Sstevel@tonic-gate 				    (vap->va_size & PAGEMASK), cr);
12567c478bd9Sstevel@tonic-gate 			}
12577c478bd9Sstevel@tonic-gate 			(void) nfs_cache_fattr(vp, &ns.ns_attr, &va, t, cr);
12587c478bd9Sstevel@tonic-gate 			/*
12597c478bd9Sstevel@tonic-gate 			 * If NFS_ACL is supported on the server, then the
12607c478bd9Sstevel@tonic-gate 			 * attributes returned by server may have minimal
12617c478bd9Sstevel@tonic-gate 			 * permissions sometimes denying access to users having
12627c478bd9Sstevel@tonic-gate 			 * proper access.  To get the proper attributes, mark
12637c478bd9Sstevel@tonic-gate 			 * the attributes as expired so that they will be
12647c478bd9Sstevel@tonic-gate 			 * regotten via the NFS_ACL GETATTR2 procedure.
12657c478bd9Sstevel@tonic-gate 			 */
12667c478bd9Sstevel@tonic-gate 			if (mi->mi_flags & MI_ACL) {
12677c478bd9Sstevel@tonic-gate 				PURGE_ATTRCACHE(vp);
12687c478bd9Sstevel@tonic-gate 			}
12697c478bd9Sstevel@tonic-gate 			/*
12707c478bd9Sstevel@tonic-gate 			 * This next check attempts to deal with NFS
12717c478bd9Sstevel@tonic-gate 			 * servers which can not handle increasing
12727c478bd9Sstevel@tonic-gate 			 * the size of the file via setattr.  Most
12737c478bd9Sstevel@tonic-gate 			 * of these servers do not return an error,
12747c478bd9Sstevel@tonic-gate 			 * but do not change the size of the file.
12757c478bd9Sstevel@tonic-gate 			 * Hence, this check and then attempt to set
12767c478bd9Sstevel@tonic-gate 			 * the file size by writing 1 byte at the
12777c478bd9Sstevel@tonic-gate 			 * offset of the end of the file that we need.
12787c478bd9Sstevel@tonic-gate 			 */
12797c478bd9Sstevel@tonic-gate 			if ((mask & AT_SIZE) &&
12807c478bd9Sstevel@tonic-gate 			    ns.ns_attr.na_size < (uint32_t)vap->va_size) {
12817c478bd9Sstevel@tonic-gate 				char zb = '\0';
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 				error = nfswrite(vp, &zb,
12847c478bd9Sstevel@tonic-gate 				    vap->va_size - sizeof (zb),
12857c478bd9Sstevel@tonic-gate 				    sizeof (zb), cr);
12867c478bd9Sstevel@tonic-gate 			}
12877c478bd9Sstevel@tonic-gate 			/*
12887c478bd9Sstevel@tonic-gate 			 * Some servers will change the mode to clear the setuid
12897c478bd9Sstevel@tonic-gate 			 * and setgid bits when changing the uid or gid.  The
12907c478bd9Sstevel@tonic-gate 			 * client needs to compensate appropriately.
12917c478bd9Sstevel@tonic-gate 			 */
12927c478bd9Sstevel@tonic-gate 			if (mask & (AT_UID | AT_GID)) {
12937c478bd9Sstevel@tonic-gate 				int terror;
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 				va.va_mask = AT_MODE;
12967c478bd9Sstevel@tonic-gate 				terror = nfsgetattr(vp, &va, cr);
12977c478bd9Sstevel@tonic-gate 				if (!terror &&
12987c478bd9Sstevel@tonic-gate 				    (((mask & AT_MODE) &&
12997c478bd9Sstevel@tonic-gate 				    va.va_mode != vap->va_mode) ||
13007c478bd9Sstevel@tonic-gate 				    (!(mask & AT_MODE) &&
13017c478bd9Sstevel@tonic-gate 				    va.va_mode != omode))) {
13027c478bd9Sstevel@tonic-gate 					va.va_mask = AT_MODE;
13037c478bd9Sstevel@tonic-gate 					if (mask & AT_MODE)
13047c478bd9Sstevel@tonic-gate 						va.va_mode = vap->va_mode;
13057c478bd9Sstevel@tonic-gate 					else
13067c478bd9Sstevel@tonic-gate 						va.va_mode = omode;
13077c478bd9Sstevel@tonic-gate 					(void) nfssetattr(vp, &va, 0, cr);
13087c478bd9Sstevel@tonic-gate 				}
13097c478bd9Sstevel@tonic-gate 			}
13107c478bd9Sstevel@tonic-gate 		} else {
13117c478bd9Sstevel@tonic-gate 			PURGE_ATTRCACHE(vp);
13127c478bd9Sstevel@tonic-gate 			PURGE_STALE_FH(error, vp, cr);
13137c478bd9Sstevel@tonic-gate 		}
13147c478bd9Sstevel@tonic-gate 	} else {
13157c478bd9Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);
13167c478bd9Sstevel@tonic-gate 	}
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	return (error);
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate static int
13227c478bd9Sstevel@tonic-gate nfs_accessx(void *vp, int mode, cred_t *cr)
13237c478bd9Sstevel@tonic-gate {
1324108322fbScarlsonj 	ASSERT(nfs_zone() == VTOMI((vnode_t *)vp)->mi_zone);
13257c478bd9Sstevel@tonic-gate 	return (nfs_access(vp, mode, 0, cr));
13267c478bd9Sstevel@tonic-gate }
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate static int
13297c478bd9Sstevel@tonic-gate nfs_access(vnode_t *vp, int mode, int flags, cred_t *cr)
13307c478bd9Sstevel@tonic-gate {
13317c478bd9Sstevel@tonic-gate 	struct vattr va;
13327c478bd9Sstevel@tonic-gate 	int error;
13337c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
13347c478bd9Sstevel@tonic-gate 	int shift = 0;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
13377c478bd9Sstevel@tonic-gate 
1338108322fbScarlsonj 	if (nfs_zone() != mi->mi_zone)
13397c478bd9Sstevel@tonic-gate 		return (EIO);
13407c478bd9Sstevel@tonic-gate 	if (mi->mi_flags & MI_ACL) {
13417c478bd9Sstevel@tonic-gate 		error = acl_access2(vp, mode, flags, cr);
13427c478bd9Sstevel@tonic-gate 		if (mi->mi_flags & MI_ACL)
13437c478bd9Sstevel@tonic-gate 			return (error);
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	va.va_mask = AT_MODE | AT_UID | AT_GID;
13477c478bd9Sstevel@tonic-gate 	error = nfsgetattr(vp, &va, cr);
13487c478bd9Sstevel@tonic-gate 	if (error)
13497c478bd9Sstevel@tonic-gate 		return (error);
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	/*
13527c478bd9Sstevel@tonic-gate 	 * Disallow write attempts on read-only
13537c478bd9Sstevel@tonic-gate 	 * file systems, unless the file is a
13547c478bd9Sstevel@tonic-gate 	 * device node.
13557c478bd9Sstevel@tonic-gate 	 */
13567c478bd9Sstevel@tonic-gate 	if ((mode & VWRITE) && vn_is_readonly(vp) && !IS_DEVVP(vp))
13577c478bd9Sstevel@tonic-gate 		return (EROFS);
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	/*
13607c478bd9Sstevel@tonic-gate 	 * Disallow attempts to access mandatory lock files.
13617c478bd9Sstevel@tonic-gate 	 */
13627c478bd9Sstevel@tonic-gate 	if ((mode & (VWRITE | VREAD | VEXEC)) &&
13637c478bd9Sstevel@tonic-gate 	    MANDLOCK(vp, va.va_mode))
13647c478bd9Sstevel@tonic-gate 		return (EACCES);
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	/*
13677c478bd9Sstevel@tonic-gate 	 * Access check is based on only
13687c478bd9Sstevel@tonic-gate 	 * one of owner, group, public.
13697c478bd9Sstevel@tonic-gate 	 * If not owner, then check group.
13707c478bd9Sstevel@tonic-gate 	 * If not a member of the group,
13717c478bd9Sstevel@tonic-gate 	 * then check public access.
13727c478bd9Sstevel@tonic-gate 	 */
13737c478bd9Sstevel@tonic-gate 	if (crgetuid(cr) != va.va_uid) {
13747c478bd9Sstevel@tonic-gate 		shift += 3;
13757c478bd9Sstevel@tonic-gate 		if (!groupmember(va.va_gid, cr))
13767c478bd9Sstevel@tonic-gate 			shift += 3;
13777c478bd9Sstevel@tonic-gate 	}
13787c478bd9Sstevel@tonic-gate found:
13797c478bd9Sstevel@tonic-gate 	mode &= ~(va.va_mode << shift);
13807c478bd9Sstevel@tonic-gate 	if (mode == 0)
13817c478bd9Sstevel@tonic-gate 		return (0);
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	return (secpolicy_vnode_access(cr, vp, va.va_uid, mode));
13847c478bd9Sstevel@tonic-gate }
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate static int nfs_do_symlink_cache = 1;
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate static int
13897c478bd9Sstevel@tonic-gate nfs_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr)
13907c478bd9Sstevel@tonic-gate {
13917c478bd9Sstevel@tonic-gate 	int error;
13927c478bd9Sstevel@tonic-gate 	struct nfsrdlnres rl;
13937c478bd9Sstevel@tonic-gate 	rnode_t *rp;
13947c478bd9Sstevel@tonic-gate 	int douprintf;
13957c478bd9Sstevel@tonic-gate 	failinfo_t fi;
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	/*
13987c478bd9Sstevel@tonic-gate 	 * We want to be consistent with UFS semantics so we will return
13997c478bd9Sstevel@tonic-gate 	 * EINVAL instead of ENXIO. This violates the XNFS spec and
14007c478bd9Sstevel@tonic-gate 	 * the RFC 1094, which are wrong any way. BUGID 1138002.
14017c478bd9Sstevel@tonic-gate 	 */
14027c478bd9Sstevel@tonic-gate 	if (vp->v_type != VLNK)
14037c478bd9Sstevel@tonic-gate 		return (EINVAL);
14047c478bd9Sstevel@tonic-gate 
1405108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
14067c478bd9Sstevel@tonic-gate 		return (EIO);
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
14097c478bd9Sstevel@tonic-gate 	if (nfs_do_symlink_cache && rp->r_symlink.contents != NULL) {
14107c478bd9Sstevel@tonic-gate 		error = nfs_validate_caches(vp, cr);
14117c478bd9Sstevel@tonic-gate 		if (error)
14127c478bd9Sstevel@tonic-gate 			return (error);
14137c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
14147c478bd9Sstevel@tonic-gate 		if (rp->r_symlink.contents != NULL) {
14157c478bd9Sstevel@tonic-gate 			error = uiomove(rp->r_symlink.contents,
14167c478bd9Sstevel@tonic-gate 			    rp->r_symlink.len, UIO_READ, uiop);
14177c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
14187c478bd9Sstevel@tonic-gate 			return (error);
14197c478bd9Sstevel@tonic-gate 		}
14207c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate 	rl.rl_data = kmem_alloc(NFS_MAXPATHLEN, KM_SLEEP);
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	fi.vp = vp;
14277c478bd9Sstevel@tonic-gate 	fi.fhp = NULL;		/* no need to update, filehandle not copied */
14287c478bd9Sstevel@tonic-gate 	fi.copyproc = nfscopyfh;
14297c478bd9Sstevel@tonic-gate 	fi.lookupproc = nfslookup;
14307c478bd9Sstevel@tonic-gate 	fi.xattrdirproc = acl_getxattrdir2;
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate 	douprintf = 1;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	error = rfs2call(VTOMI(vp), RFS_READLINK,
14357c478bd9Sstevel@tonic-gate 	    xdr_fhandle, (caddr_t)VTOFH(vp),
14367c478bd9Sstevel@tonic-gate 	    xdr_rdlnres, (caddr_t)&rl, cr,
14377c478bd9Sstevel@tonic-gate 	    &douprintf, &rl.rl_status, 0, &fi);
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	if (error) {
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 		kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
14427c478bd9Sstevel@tonic-gate 		return (error);
14437c478bd9Sstevel@tonic-gate 	}
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	error = geterrno(rl.rl_status);
14467c478bd9Sstevel@tonic-gate 	if (!error) {
14477c478bd9Sstevel@tonic-gate 		error = uiomove(rl.rl_data, (int)rl.rl_count, UIO_READ, uiop);
14487c478bd9Sstevel@tonic-gate 		if (nfs_do_symlink_cache && rp->r_symlink.contents == NULL) {
14497c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
14507c478bd9Sstevel@tonic-gate 			if (rp->r_symlink.contents == NULL) {
14517c478bd9Sstevel@tonic-gate 				rp->r_symlink.contents = rl.rl_data;
14527c478bd9Sstevel@tonic-gate 				rp->r_symlink.len = (int)rl.rl_count;
14537c478bd9Sstevel@tonic-gate 				rp->r_symlink.size = NFS_MAXPATHLEN;
14547c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
14557c478bd9Sstevel@tonic-gate 			} else {
14567c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
14577c478bd9Sstevel@tonic-gate 
14587c478bd9Sstevel@tonic-gate 				kmem_free((void *)rl.rl_data,
14597c478bd9Sstevel@tonic-gate 				    NFS_MAXPATHLEN);
14607c478bd9Sstevel@tonic-gate 			}
14617c478bd9Sstevel@tonic-gate 		} else {
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate 			kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
14647c478bd9Sstevel@tonic-gate 		}
14657c478bd9Sstevel@tonic-gate 	} else {
14667c478bd9Sstevel@tonic-gate 		PURGE_STALE_FH(error, vp, cr);
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 		kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
14697c478bd9Sstevel@tonic-gate 	}
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	/*
14727c478bd9Sstevel@tonic-gate 	 * Conform to UFS semantics (see comment above)
14737c478bd9Sstevel@tonic-gate 	 */
14747c478bd9Sstevel@tonic-gate 	return (error == ENXIO ? EINVAL : error);
14757c478bd9Sstevel@tonic-gate }
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate /*
14787c478bd9Sstevel@tonic-gate  * Flush local dirty pages to stable storage on the server.
14797c478bd9Sstevel@tonic-gate  *
14807c478bd9Sstevel@tonic-gate  * If FNODSYNC is specified, then there is nothing to do because
14817c478bd9Sstevel@tonic-gate  * metadata changes are not cached on the client before being
14827c478bd9Sstevel@tonic-gate  * sent to the server.
14837c478bd9Sstevel@tonic-gate  */
14847c478bd9Sstevel@tonic-gate static int
14857c478bd9Sstevel@tonic-gate nfs_fsync(vnode_t *vp, int syncflag, cred_t *cr)
14867c478bd9Sstevel@tonic-gate {
14877c478bd9Sstevel@tonic-gate 	int error;
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
14907c478bd9Sstevel@tonic-gate 		return (0);
14917c478bd9Sstevel@tonic-gate 
1492108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
14937c478bd9Sstevel@tonic-gate 		return (EIO);
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	error = nfs_putpage(vp, (offset_t)0, 0, 0, cr);
14967c478bd9Sstevel@tonic-gate 	if (!error)
14977c478bd9Sstevel@tonic-gate 		error = VTOR(vp)->r_error;
14987c478bd9Sstevel@tonic-gate 	return (error);
14997c478bd9Sstevel@tonic-gate }
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate /*
15037c478bd9Sstevel@tonic-gate  * Weirdness: if the file was removed or the target of a rename
15047c478bd9Sstevel@tonic-gate  * operation while it was open, it got renamed instead.  Here we
15057c478bd9Sstevel@tonic-gate  * remove the renamed file.
15067c478bd9Sstevel@tonic-gate  */
15077c478bd9Sstevel@tonic-gate static void
15087c478bd9Sstevel@tonic-gate nfs_inactive(vnode_t *vp, cred_t *cr)
15097c478bd9Sstevel@tonic-gate {
15107c478bd9Sstevel@tonic-gate 	rnode_t *rp;
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	ASSERT(vp != DNLC_NO_VNODE);
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	/*
15157c478bd9Sstevel@tonic-gate 	 * If this is coming from the wrong zone, we let someone in the right
15167c478bd9Sstevel@tonic-gate 	 * zone take care of it asynchronously.  We can get here due to
15177c478bd9Sstevel@tonic-gate 	 * VN_RELE() being called from pageout() or fsflush().  This call may
15187c478bd9Sstevel@tonic-gate 	 * potentially turn into an expensive no-op if, for instance, v_count
15197c478bd9Sstevel@tonic-gate 	 * gets incremented in the meantime, but it's still correct.
15207c478bd9Sstevel@tonic-gate 	 */
1521108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone) {
15227c478bd9Sstevel@tonic-gate 		nfs_async_inactive(vp, cr, nfs_inactive);
15237c478bd9Sstevel@tonic-gate 		return;
15247c478bd9Sstevel@tonic-gate 	}
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
15277c478bd9Sstevel@tonic-gate redo:
15287c478bd9Sstevel@tonic-gate 	if (rp->r_unldvp != NULL) {
15297c478bd9Sstevel@tonic-gate 		/*
15307c478bd9Sstevel@tonic-gate 		 * Save the vnode pointer for the directory where the
15317c478bd9Sstevel@tonic-gate 		 * unlinked-open file got renamed, then set it to NULL
15327c478bd9Sstevel@tonic-gate 		 * to prevent another thread from getting here before
15337c478bd9Sstevel@tonic-gate 		 * we're done with the remove.  While we have the
15347c478bd9Sstevel@tonic-gate 		 * statelock, make local copies of the pertinent rnode
15357c478bd9Sstevel@tonic-gate 		 * fields.  If we weren't to do this in an atomic way, the
15367c478bd9Sstevel@tonic-gate 		 * the unl* fields could become inconsistent with respect
15377c478bd9Sstevel@tonic-gate 		 * to each other due to a race condition between this
15387c478bd9Sstevel@tonic-gate 		 * code and nfs_remove().  See bug report 1034328.
15397c478bd9Sstevel@tonic-gate 		 */
15407c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
15417c478bd9Sstevel@tonic-gate 		if (rp->r_unldvp != NULL) {
15427c478bd9Sstevel@tonic-gate 			vnode_t *unldvp;
15437c478bd9Sstevel@tonic-gate 			char *unlname;
15447c478bd9Sstevel@tonic-gate 			cred_t *unlcred;
15457c478bd9Sstevel@tonic-gate 			struct nfsdiropargs da;
15467c478bd9Sstevel@tonic-gate 			enum nfsstat status;
15477c478bd9Sstevel@tonic-gate 			int douprintf;
15487c478bd9Sstevel@tonic-gate 			int error;
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 			unldvp = rp->r_unldvp;
15517c478bd9Sstevel@tonic-gate 			rp->r_unldvp = NULL;
15527c478bd9Sstevel@tonic-gate 			unlname = rp->r_unlname;
15537c478bd9Sstevel@tonic-gate 			rp->r_unlname = NULL;
15547c478bd9Sstevel@tonic-gate 			unlcred = rp->r_unlcred;
15557c478bd9Sstevel@tonic-gate 			rp->r_unlcred = NULL;
15567c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 			/*
15597c478bd9Sstevel@tonic-gate 			 * If there are any dirty pages left, then flush
15607c478bd9Sstevel@tonic-gate 			 * them.  This is unfortunate because they just
15617c478bd9Sstevel@tonic-gate 			 * may get thrown away during the remove operation,
15627c478bd9Sstevel@tonic-gate 			 * but we have to do this for correctness.
15637c478bd9Sstevel@tonic-gate 			 */
15647c478bd9Sstevel@tonic-gate 			if (vn_has_cached_data(vp) &&
15657c478bd9Sstevel@tonic-gate 			    ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
15667c478bd9Sstevel@tonic-gate 				ASSERT(vp->v_type != VCHR);
15677c478bd9Sstevel@tonic-gate 				error = nfs_putpage(vp, (offset_t)0, 0, 0, cr);
15687c478bd9Sstevel@tonic-gate 				if (error) {
15697c478bd9Sstevel@tonic-gate 					mutex_enter(&rp->r_statelock);
15707c478bd9Sstevel@tonic-gate 					if (!rp->r_error)
15717c478bd9Sstevel@tonic-gate 						rp->r_error = error;
15727c478bd9Sstevel@tonic-gate 					mutex_exit(&rp->r_statelock);
15737c478bd9Sstevel@tonic-gate 				}
15747c478bd9Sstevel@tonic-gate 			}
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 			/*
15777c478bd9Sstevel@tonic-gate 			 * Do the remove operation on the renamed file
15787c478bd9Sstevel@tonic-gate 			 */
15797c478bd9Sstevel@tonic-gate 			setdiropargs(&da, unlname, unldvp);
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 			douprintf = 1;
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 			(void) rfs2call(VTOMI(unldvp), RFS_REMOVE,
15847c478bd9Sstevel@tonic-gate 			    xdr_diropargs, (caddr_t)&da,
15857c478bd9Sstevel@tonic-gate 			    xdr_enum, (caddr_t)&status, unlcred,
15867c478bd9Sstevel@tonic-gate 			    &douprintf, &status, 0, NULL);
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 			if (HAVE_RDDIR_CACHE(VTOR(unldvp)))
15897c478bd9Sstevel@tonic-gate 				nfs_purge_rddir_cache(unldvp);
15907c478bd9Sstevel@tonic-gate 			PURGE_ATTRCACHE(unldvp);
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 			/*
15937c478bd9Sstevel@tonic-gate 			 * Release stuff held for the remove
15947c478bd9Sstevel@tonic-gate 			 */
15957c478bd9Sstevel@tonic-gate 			VN_RELE(unldvp);
15967c478bd9Sstevel@tonic-gate 			kmem_free(unlname, MAXNAMELEN);
15977c478bd9Sstevel@tonic-gate 			crfree(unlcred);
15987c478bd9Sstevel@tonic-gate 			goto redo;
15997c478bd9Sstevel@tonic-gate 		}
16007c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
16017c478bd9Sstevel@tonic-gate 	}
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	rp_addfree(rp, cr);
16047c478bd9Sstevel@tonic-gate }
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate /*
16077c478bd9Sstevel@tonic-gate  * Remote file system operations having to do with directory manipulation.
16087c478bd9Sstevel@tonic-gate  */
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate static int
16117c478bd9Sstevel@tonic-gate nfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
16127c478bd9Sstevel@tonic-gate 	int flags, vnode_t *rdir, cred_t *cr)
16137c478bd9Sstevel@tonic-gate {
16147c478bd9Sstevel@tonic-gate 	int error;
16157c478bd9Sstevel@tonic-gate 	vnode_t *vp;
16167c478bd9Sstevel@tonic-gate 	vnode_t *avp = NULL;
16177c478bd9Sstevel@tonic-gate 	rnode_t *drp;
16187c478bd9Sstevel@tonic-gate 
1619108322fbScarlsonj 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
16207c478bd9Sstevel@tonic-gate 		return (EPERM);
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	drp = VTOR(dvp);
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	/*
16257c478bd9Sstevel@tonic-gate 	 * Are we looking up extended attributes?  If so, "dvp" is
16267c478bd9Sstevel@tonic-gate 	 * the file or directory for which we want attributes, and
16277c478bd9Sstevel@tonic-gate 	 * we need a lookup of the hidden attribute directory
16287c478bd9Sstevel@tonic-gate 	 * before we lookup the rest of the path.
16297c478bd9Sstevel@tonic-gate 	 */
16307c478bd9Sstevel@tonic-gate 	if (flags & LOOKUP_XATTR) {
16317c478bd9Sstevel@tonic-gate 		bool_t cflag = ((flags & CREATE_XATTR_DIR) != 0);
16327c478bd9Sstevel@tonic-gate 		mntinfo_t *mi;
16337c478bd9Sstevel@tonic-gate 
16347c478bd9Sstevel@tonic-gate 		mi = VTOMI(dvp);
16357c478bd9Sstevel@tonic-gate 		if (!(mi->mi_flags & MI_EXTATTR))
16367c478bd9Sstevel@tonic-gate 			return (EINVAL);
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 		if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp)))
16397c478bd9Sstevel@tonic-gate 			return (EINTR);
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 		(void) nfslookup_dnlc(dvp, XATTR_DIR_NAME, &avp, cr);
16427c478bd9Sstevel@tonic-gate 		if (avp == NULL)
16437c478bd9Sstevel@tonic-gate 			error = acl_getxattrdir2(dvp, &avp, cflag, cr, 0);
16447c478bd9Sstevel@tonic-gate 		else
16457c478bd9Sstevel@tonic-gate 			error = 0;
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&drp->r_rwlock);
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 		if (error) {
16507c478bd9Sstevel@tonic-gate 			if (mi->mi_flags & MI_EXTATTR)
16517c478bd9Sstevel@tonic-gate 				return (error);
16527c478bd9Sstevel@tonic-gate 			return (EINVAL);
16537c478bd9Sstevel@tonic-gate 		}
16547c478bd9Sstevel@tonic-gate 		dvp = avp;
16557c478bd9Sstevel@tonic-gate 		drp = VTOR(dvp);
16567c478bd9Sstevel@tonic-gate 	}
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp))) {
16597c478bd9Sstevel@tonic-gate 		error = EINTR;
16607c478bd9Sstevel@tonic-gate 		goto out;
16617c478bd9Sstevel@tonic-gate 	}
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	error = nfslookup(dvp, nm, vpp, pnp, flags, rdir, cr, 0);
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&drp->r_rwlock);
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	/*
16687c478bd9Sstevel@tonic-gate 	 * If vnode is a device, create special vnode.
16697c478bd9Sstevel@tonic-gate 	 */
16707c478bd9Sstevel@tonic-gate 	if (!error && IS_DEVVP(*vpp)) {
16717c478bd9Sstevel@tonic-gate 		vp = *vpp;
16727c478bd9Sstevel@tonic-gate 		*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
16737c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
16747c478bd9Sstevel@tonic-gate 	}
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate out:
16777c478bd9Sstevel@tonic-gate 	if (avp != NULL)
16787c478bd9Sstevel@tonic-gate 		VN_RELE(avp);
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	return (error);
16817c478bd9Sstevel@tonic-gate }
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate static int nfs_lookup_neg_cache = 1;
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate #ifdef DEBUG
16867c478bd9Sstevel@tonic-gate static int nfs_lookup_dnlc_hits = 0;
16877c478bd9Sstevel@tonic-gate static int nfs_lookup_dnlc_misses = 0;
16887c478bd9Sstevel@tonic-gate static int nfs_lookup_dnlc_neg_hits = 0;
16897c478bd9Sstevel@tonic-gate static int nfs_lookup_dnlc_disappears = 0;
16907c478bd9Sstevel@tonic-gate static int nfs_lookup_dnlc_lookups = 0;
16917c478bd9Sstevel@tonic-gate #endif
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate /* ARGSUSED */
16947c478bd9Sstevel@tonic-gate int
16957c478bd9Sstevel@tonic-gate nfslookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
16967c478bd9Sstevel@tonic-gate 	int flags, vnode_t *rdir, cred_t *cr, int rfscall_flags)
16977c478bd9Sstevel@tonic-gate {
16987c478bd9Sstevel@tonic-gate 	int error;
16997c478bd9Sstevel@tonic-gate 
1700108322fbScarlsonj 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate 	/*
17037c478bd9Sstevel@tonic-gate 	 * If lookup is for "", just return dvp.  Don't need
17047c478bd9Sstevel@tonic-gate 	 * to send it over the wire, look it up in the dnlc,
17057c478bd9Sstevel@tonic-gate 	 * or perform any access checks.
17067c478bd9Sstevel@tonic-gate 	 */
17077c478bd9Sstevel@tonic-gate 	if (*nm == '\0') {
17087c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
17097c478bd9Sstevel@tonic-gate 		*vpp = dvp;
17107c478bd9Sstevel@tonic-gate 		return (0);
17117c478bd9Sstevel@tonic-gate 	}
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	/*
17147c478bd9Sstevel@tonic-gate 	 * Can't do lookups in non-directories.
17157c478bd9Sstevel@tonic-gate 	 */
17167c478bd9Sstevel@tonic-gate 	if (dvp->v_type != VDIR)
17177c478bd9Sstevel@tonic-gate 		return (ENOTDIR);
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	/*
17207c478bd9Sstevel@tonic-gate 	 * If we're called with RFSCALL_SOFT, it's important that
17217c478bd9Sstevel@tonic-gate 	 * the only rfscall is one we make directly; if we permit
17227c478bd9Sstevel@tonic-gate 	 * an access call because we're looking up "." or validating
17237c478bd9Sstevel@tonic-gate 	 * a dnlc hit, we'll deadlock because that rfscall will not
17247c478bd9Sstevel@tonic-gate 	 * have the RFSCALL_SOFT set.
17257c478bd9Sstevel@tonic-gate 	 */
17267c478bd9Sstevel@tonic-gate 	if (rfscall_flags & RFSCALL_SOFT)
17277c478bd9Sstevel@tonic-gate 		goto callit;
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	/*
17307c478bd9Sstevel@tonic-gate 	 * If lookup is for ".", just return dvp.  Don't need
17317c478bd9Sstevel@tonic-gate 	 * to send it over the wire or look it up in the dnlc,
17327c478bd9Sstevel@tonic-gate 	 * just need to check access.
17337c478bd9Sstevel@tonic-gate 	 */
17347c478bd9Sstevel@tonic-gate 	if (strcmp(nm, ".") == 0) {
17357c478bd9Sstevel@tonic-gate 		error = nfs_access(dvp, VEXEC, 0, cr);
17367c478bd9Sstevel@tonic-gate 		if (error)
17377c478bd9Sstevel@tonic-gate 			return (error);
17387c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
17397c478bd9Sstevel@tonic-gate 		*vpp = dvp;
17407c478bd9Sstevel@tonic-gate 		return (0);
17417c478bd9Sstevel@tonic-gate 	}
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 	/*
17447c478bd9Sstevel@tonic-gate 	 * Lookup this name in the DNLC.  If there was a valid entry,
17457c478bd9Sstevel@tonic-gate 	 * then return the results of the lookup.
17467c478bd9Sstevel@tonic-gate 	 */
17477c478bd9Sstevel@tonic-gate 	error = nfslookup_dnlc(dvp, nm, vpp, cr);
17487c478bd9Sstevel@tonic-gate 	if (error || *vpp != NULL)
17497c478bd9Sstevel@tonic-gate 		return (error);
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate callit:
17527c478bd9Sstevel@tonic-gate 	error = nfslookup_otw(dvp, nm, vpp, cr, rfscall_flags);
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	return (error);
17557c478bd9Sstevel@tonic-gate }
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate static int
17587c478bd9Sstevel@tonic-gate nfslookup_dnlc(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
17597c478bd9Sstevel@tonic-gate {
17607c478bd9Sstevel@tonic-gate 	int error;
17617c478bd9Sstevel@tonic-gate 	vnode_t *vp;
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate 	ASSERT(*nm != '\0');
1764108322fbScarlsonj 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 	/*
17677c478bd9Sstevel@tonic-gate 	 * Lookup this name in the DNLC.  If successful, then validate
17687c478bd9Sstevel@tonic-gate 	 * the caches and then recheck the DNLC.  The DNLC is rechecked
17697c478bd9Sstevel@tonic-gate 	 * just in case this entry got invalidated during the call
17707c478bd9Sstevel@tonic-gate 	 * to nfs_validate_caches.
17717c478bd9Sstevel@tonic-gate 	 *
17727c478bd9Sstevel@tonic-gate 	 * An assumption is being made that it is safe to say that a
17737c478bd9Sstevel@tonic-gate 	 * file exists which may not on the server.  Any operations to
17747c478bd9Sstevel@tonic-gate 	 * the server will fail with ESTALE.
17757c478bd9Sstevel@tonic-gate 	 */
17767c478bd9Sstevel@tonic-gate #ifdef DEBUG
17777c478bd9Sstevel@tonic-gate 	nfs_lookup_dnlc_lookups++;
17787c478bd9Sstevel@tonic-gate #endif
17797c478bd9Sstevel@tonic-gate 	vp = dnlc_lookup(dvp, nm);
17807c478bd9Sstevel@tonic-gate 	if (vp != NULL) {
17817c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
17827c478bd9Sstevel@tonic-gate 		if (vp == DNLC_NO_VNODE && !vn_is_readonly(dvp)) {
17837c478bd9Sstevel@tonic-gate 			PURGE_ATTRCACHE(dvp);
17847c478bd9Sstevel@tonic-gate 		}
17857c478bd9Sstevel@tonic-gate 		error = nfs_validate_caches(dvp, cr);
17867c478bd9Sstevel@tonic-gate 		if (error)
17877c478bd9Sstevel@tonic-gate 			return (error);
17887c478bd9Sstevel@tonic-gate 		vp = dnlc_lookup(dvp, nm);
17897c478bd9Sstevel@tonic-gate 		if (vp != NULL) {
17907c478bd9Sstevel@tonic-gate 			error = nfs_access(dvp, VEXEC, 0, cr);
17917c478bd9Sstevel@tonic-gate 			if (error) {
17927c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
17937c478bd9Sstevel@tonic-gate 				return (error);
17947c478bd9Sstevel@tonic-gate 			}
17957c478bd9Sstevel@tonic-gate 			if (vp == DNLC_NO_VNODE) {
17967c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
17977c478bd9Sstevel@tonic-gate #ifdef DEBUG
17987c478bd9Sstevel@tonic-gate 				nfs_lookup_dnlc_neg_hits++;
17997c478bd9Sstevel@tonic-gate #endif
18007c478bd9Sstevel@tonic-gate 				return (ENOENT);
18017c478bd9Sstevel@tonic-gate 			}
18027c478bd9Sstevel@tonic-gate 			*vpp = vp;
18037c478bd9Sstevel@tonic-gate #ifdef DEBUG
18047c478bd9Sstevel@tonic-gate 			nfs_lookup_dnlc_hits++;
18057c478bd9Sstevel@tonic-gate #endif
18067c478bd9Sstevel@tonic-gate 			return (0);
18077c478bd9Sstevel@tonic-gate 		}
18087c478bd9Sstevel@tonic-gate #ifdef DEBUG
18097c478bd9Sstevel@tonic-gate 		nfs_lookup_dnlc_disappears++;
18107c478bd9Sstevel@tonic-gate #endif
18117c478bd9Sstevel@tonic-gate 	}
18127c478bd9Sstevel@tonic-gate #ifdef DEBUG
18137c478bd9Sstevel@tonic-gate 	else
18147c478bd9Sstevel@tonic-gate 		nfs_lookup_dnlc_misses++;
18157c478bd9Sstevel@tonic-gate #endif
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	*vpp = NULL;
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	return (0);
18207c478bd9Sstevel@tonic-gate }
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate static int
18237c478bd9Sstevel@tonic-gate nfslookup_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr,
18247c478bd9Sstevel@tonic-gate 	int rfscall_flags)
18257c478bd9Sstevel@tonic-gate {
18267c478bd9Sstevel@tonic-gate 	int error;
18277c478bd9Sstevel@tonic-gate 	struct nfsdiropargs da;
18287c478bd9Sstevel@tonic-gate 	struct nfsdiropres dr;
18297c478bd9Sstevel@tonic-gate 	int douprintf;
18307c478bd9Sstevel@tonic-gate 	failinfo_t fi;
18317c478bd9Sstevel@tonic-gate 	hrtime_t t;
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	ASSERT(*nm != '\0');
18347c478bd9Sstevel@tonic-gate 	ASSERT(dvp->v_type == VDIR);
1835108322fbScarlsonj 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 	setdiropargs(&da, nm, dvp);
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	fi.vp = dvp;
18407c478bd9Sstevel@tonic-gate 	fi.fhp = NULL;		/* no need to update, filehandle not copied */
18417c478bd9Sstevel@tonic-gate 	fi.copyproc = nfscopyfh;
18427c478bd9Sstevel@tonic-gate 	fi.lookupproc = nfslookup;
18437c478bd9Sstevel@tonic-gate 	fi.xattrdirproc = acl_getxattrdir2;
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	douprintf = 1;
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	t = gethrtime();
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 	error = rfs2call(VTOMI(dvp), RFS_LOOKUP,
18507c478bd9Sstevel@tonic-gate 	    xdr_diropargs, (caddr_t)&da,
18517c478bd9Sstevel@tonic-gate 	    xdr_diropres, (caddr_t)&dr, cr,
18527c478bd9Sstevel@tonic-gate 	    &douprintf, &dr.dr_status, rfscall_flags, &fi);
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	if (!error) {
18557c478bd9Sstevel@tonic-gate 		error = geterrno(dr.dr_status);
18567c478bd9Sstevel@tonic-gate 		if (!error) {
18577c478bd9Sstevel@tonic-gate 			*vpp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
18587c478bd9Sstevel@tonic-gate 			    dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
18597c478bd9Sstevel@tonic-gate 			/*
18607c478bd9Sstevel@tonic-gate 			 * If NFS_ACL is supported on the server, then the
18617c478bd9Sstevel@tonic-gate 			 * attributes returned by server may have minimal
18627c478bd9Sstevel@tonic-gate 			 * permissions sometimes denying access to users having
18637c478bd9Sstevel@tonic-gate 			 * proper access.  To get the proper attributes, mark
18647c478bd9Sstevel@tonic-gate 			 * the attributes as expired so that they will be
18657c478bd9Sstevel@tonic-gate 			 * regotten via the NFS_ACL GETATTR2 procedure.
18667c478bd9Sstevel@tonic-gate 			 */
18677c478bd9Sstevel@tonic-gate 			if (VTOMI(*vpp)->mi_flags & MI_ACL) {
18687c478bd9Sstevel@tonic-gate 				PURGE_ATTRCACHE(*vpp);
18697c478bd9Sstevel@tonic-gate 			}
18707c478bd9Sstevel@tonic-gate 			if (!(rfscall_flags & RFSCALL_SOFT))
18717c478bd9Sstevel@tonic-gate 				dnlc_update(dvp, nm, *vpp);
18727c478bd9Sstevel@tonic-gate 		} else {
18737c478bd9Sstevel@tonic-gate 			PURGE_STALE_FH(error, dvp, cr);
18747c478bd9Sstevel@tonic-gate 			if (error == ENOENT && nfs_lookup_neg_cache)
18757c478bd9Sstevel@tonic-gate 				dnlc_enter(dvp, nm, DNLC_NO_VNODE);
18767c478bd9Sstevel@tonic-gate 		}
18777c478bd9Sstevel@tonic-gate 	}
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 	return (error);
18807c478bd9Sstevel@tonic-gate }
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate /* ARGSUSED */
18837c478bd9Sstevel@tonic-gate static int
18847c478bd9Sstevel@tonic-gate nfs_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
18857c478bd9Sstevel@tonic-gate 	int mode, vnode_t **vpp, cred_t *cr, int lfaware)
18867c478bd9Sstevel@tonic-gate {
18877c478bd9Sstevel@tonic-gate 	int error;
18887c478bd9Sstevel@tonic-gate 	struct nfscreatargs args;
18897c478bd9Sstevel@tonic-gate 	struct nfsdiropres dr;
18907c478bd9Sstevel@tonic-gate 	int douprintf;
18917c478bd9Sstevel@tonic-gate 	vnode_t *vp;
18927c478bd9Sstevel@tonic-gate 	rnode_t *rp;
18937c478bd9Sstevel@tonic-gate 	struct vattr vattr;
18947c478bd9Sstevel@tonic-gate 	rnode_t *drp;
18957c478bd9Sstevel@tonic-gate 	vnode_t *tempvp;
18967c478bd9Sstevel@tonic-gate 	hrtime_t t;
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 	drp = VTOR(dvp);
18997c478bd9Sstevel@tonic-gate 
1900108322fbScarlsonj 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
19017c478bd9Sstevel@tonic-gate 		return (EPERM);
19027c478bd9Sstevel@tonic-gate 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
19037c478bd9Sstevel@tonic-gate 		return (EINTR);
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	/*
19067c478bd9Sstevel@tonic-gate 	 * We make a copy of the attributes because the caller does not
19077c478bd9Sstevel@tonic-gate 	 * expect us to change what va points to.
19087c478bd9Sstevel@tonic-gate 	 */
19097c478bd9Sstevel@tonic-gate 	vattr = *va;
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 	/*
19127c478bd9Sstevel@tonic-gate 	 * If the pathname is "", just use dvp.  Don't need
19137c478bd9Sstevel@tonic-gate 	 * to send it over the wire, look it up in the dnlc,
19147c478bd9Sstevel@tonic-gate 	 * or perform any access checks.
19157c478bd9Sstevel@tonic-gate 	 */
19167c478bd9Sstevel@tonic-gate 	if (*nm == '\0') {
19177c478bd9Sstevel@tonic-gate 		error = 0;
19187c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
19197c478bd9Sstevel@tonic-gate 		vp = dvp;
19207c478bd9Sstevel@tonic-gate 	/*
19217c478bd9Sstevel@tonic-gate 	 * If the pathname is ".", just use dvp.  Don't need
19227c478bd9Sstevel@tonic-gate 	 * to send it over the wire or look it up in the dnlc,
19237c478bd9Sstevel@tonic-gate 	 * just need to check access.
19247c478bd9Sstevel@tonic-gate 	 */
19257c478bd9Sstevel@tonic-gate 	} else if (strcmp(nm, ".") == 0) {
19267c478bd9Sstevel@tonic-gate 		error = nfs_access(dvp, VEXEC, 0, cr);
19277c478bd9Sstevel@tonic-gate 		if (error) {
19287c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&drp->r_rwlock);
19297c478bd9Sstevel@tonic-gate 			return (error);
19307c478bd9Sstevel@tonic-gate 		}
19317c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
19327c478bd9Sstevel@tonic-gate 		vp = dvp;
19337c478bd9Sstevel@tonic-gate 	/*
19347c478bd9Sstevel@tonic-gate 	 * We need to go over the wire, just to be sure whether the
19357c478bd9Sstevel@tonic-gate 	 * file exists or not.  Using the DNLC can be dangerous in
19367c478bd9Sstevel@tonic-gate 	 * this case when making a decision regarding existence.
19377c478bd9Sstevel@tonic-gate 	 */
19387c478bd9Sstevel@tonic-gate 	} else {
19397c478bd9Sstevel@tonic-gate 		error = nfslookup_otw(dvp, nm, &vp, cr, 0);
19407c478bd9Sstevel@tonic-gate 	}
19417c478bd9Sstevel@tonic-gate 	if (!error) {
19427c478bd9Sstevel@tonic-gate 		if (exclusive == EXCL)
19437c478bd9Sstevel@tonic-gate 			error = EEXIST;
19447c478bd9Sstevel@tonic-gate 		else if (vp->v_type == VDIR && (mode & VWRITE))
19457c478bd9Sstevel@tonic-gate 			error = EISDIR;
19467c478bd9Sstevel@tonic-gate 		else {
19477c478bd9Sstevel@tonic-gate 			/*
19487c478bd9Sstevel@tonic-gate 			 * If vnode is a device, create special vnode.
19497c478bd9Sstevel@tonic-gate 			 */
19507c478bd9Sstevel@tonic-gate 			if (IS_DEVVP(vp)) {
19517c478bd9Sstevel@tonic-gate 				tempvp = vp;
19527c478bd9Sstevel@tonic-gate 				vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
19537c478bd9Sstevel@tonic-gate 				VN_RELE(tempvp);
19547c478bd9Sstevel@tonic-gate 			}
19557c478bd9Sstevel@tonic-gate 			if (!(error = VOP_ACCESS(vp, mode, 0, cr))) {
19567c478bd9Sstevel@tonic-gate 				if ((vattr.va_mask & AT_SIZE) &&
19577c478bd9Sstevel@tonic-gate 				    vp->v_type == VREG) {
19587c478bd9Sstevel@tonic-gate 					vattr.va_mask = AT_SIZE;
19597c478bd9Sstevel@tonic-gate 					error = nfssetattr(vp, &vattr, 0, cr);
19607c478bd9Sstevel@tonic-gate 				}
19617c478bd9Sstevel@tonic-gate 			}
19627c478bd9Sstevel@tonic-gate 		}
19637c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&drp->r_rwlock);
19647c478bd9Sstevel@tonic-gate 		if (error) {
19657c478bd9Sstevel@tonic-gate 			VN_RELE(vp);
19667c478bd9Sstevel@tonic-gate 		} else
19677c478bd9Sstevel@tonic-gate 			*vpp = vp;
19687c478bd9Sstevel@tonic-gate 		return (error);
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	ASSERT(vattr.va_mask & AT_TYPE);
19727c478bd9Sstevel@tonic-gate 	if (vattr.va_type == VREG) {
19737c478bd9Sstevel@tonic-gate 		ASSERT(vattr.va_mask & AT_MODE);
19747c478bd9Sstevel@tonic-gate 		if (MANDMODE(vattr.va_mode)) {
19757c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&drp->r_rwlock);
19767c478bd9Sstevel@tonic-gate 			return (EACCES);
19777c478bd9Sstevel@tonic-gate 		}
19787c478bd9Sstevel@tonic-gate 	}
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	dnlc_remove(dvp, nm);
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 	setdiropargs(&args.ca_da, nm, dvp);
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	/*
19857c478bd9Sstevel@tonic-gate 	 * Decide what the group-id of the created file should be.
19867c478bd9Sstevel@tonic-gate 	 * Set it in attribute list as advisory...then do a setattr
19877c478bd9Sstevel@tonic-gate 	 * if the server didn't get it right the first time.
19887c478bd9Sstevel@tonic-gate 	 */
19897c478bd9Sstevel@tonic-gate 	error = setdirgid(dvp, &vattr.va_gid, cr);
19907c478bd9Sstevel@tonic-gate 	if (error) {
19917c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&drp->r_rwlock);
19927c478bd9Sstevel@tonic-gate 		return (error);
19937c478bd9Sstevel@tonic-gate 	}
19947c478bd9Sstevel@tonic-gate 	vattr.va_mask |= AT_GID;
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	/*
19977c478bd9Sstevel@tonic-gate 	 * This is a completely gross hack to make mknod
19987c478bd9Sstevel@tonic-gate 	 * work over the wire until we can wack the protocol
19997c478bd9Sstevel@tonic-gate 	 */
20007c478bd9Sstevel@tonic-gate #define	IFCHR		0020000		/* character special */
20017c478bd9Sstevel@tonic-gate #define	IFBLK		0060000		/* block special */
20027c478bd9Sstevel@tonic-gate #define	IFSOCK		0140000		/* socket */
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	/*
20057c478bd9Sstevel@tonic-gate 	 * dev_t is uint_t in 5.x and short in 4.x. Both 4.x
20067c478bd9Sstevel@tonic-gate 	 * supports 8 bit majors. 5.x supports 14 bit majors. 5.x supports 18
20077c478bd9Sstevel@tonic-gate 	 * bits in the minor number where 4.x supports 8 bits.  If the 5.x
20087c478bd9Sstevel@tonic-gate 	 * minor/major numbers <= 8 bits long, compress the device
20097c478bd9Sstevel@tonic-gate 	 * number before sending it. Otherwise, the 4.x server will not
20107c478bd9Sstevel@tonic-gate 	 * create the device with the correct device number and nothing can be
20117c478bd9Sstevel@tonic-gate 	 * done about this.
20127c478bd9Sstevel@tonic-gate 	 */
20137c478bd9Sstevel@tonic-gate 	if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
20147c478bd9Sstevel@tonic-gate 		dev_t d = vattr.va_rdev;
20157c478bd9Sstevel@tonic-gate 		dev32_t dev32;
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 		if (vattr.va_type == VCHR)
20187c478bd9Sstevel@tonic-gate 			vattr.va_mode |= IFCHR;
20197c478bd9Sstevel@tonic-gate 		else
20207c478bd9Sstevel@tonic-gate 			vattr.va_mode |= IFBLK;
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 		(void) cmpldev(&dev32, d);
20237c478bd9Sstevel@tonic-gate 		if (dev32 & ~((SO4_MAXMAJ << L_BITSMINOR32) | SO4_MAXMIN))
20247c478bd9Sstevel@tonic-gate 			vattr.va_size = (u_offset_t)dev32;
20257c478bd9Sstevel@tonic-gate 		else
20267c478bd9Sstevel@tonic-gate 			vattr.va_size = (u_offset_t)nfsv2_cmpdev(d);
20277c478bd9Sstevel@tonic-gate 
20287c478bd9Sstevel@tonic-gate 		vattr.va_mask |= AT_MODE|AT_SIZE;
20297c478bd9Sstevel@tonic-gate 	} else if (vattr.va_type == VFIFO) {
20307c478bd9Sstevel@tonic-gate 		vattr.va_mode |= IFCHR;		/* xtra kludge for namedpipe */
20317c478bd9Sstevel@tonic-gate 		vattr.va_size = (u_offset_t)NFS_FIFO_DEV;	/* blech */
20327c478bd9Sstevel@tonic-gate 		vattr.va_mask |= AT_MODE|AT_SIZE;
20337c478bd9Sstevel@tonic-gate 	} else if (vattr.va_type == VSOCK) {
20347c478bd9Sstevel@tonic-gate 		vattr.va_mode |= IFSOCK;
20357c478bd9Sstevel@tonic-gate 		/*
20367c478bd9Sstevel@tonic-gate 		 * To avoid triggering bugs in the servers set AT_SIZE
20377c478bd9Sstevel@tonic-gate 		 * (all other RFS_CREATE calls set this).
20387c478bd9Sstevel@tonic-gate 		 */
20397c478bd9Sstevel@tonic-gate 		vattr.va_size = 0;
20407c478bd9Sstevel@tonic-gate 		vattr.va_mask |= AT_MODE|AT_SIZE;
20417c478bd9Sstevel@tonic-gate 	}
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 	args.ca_sa = &args.ca_sa_buf;
20447c478bd9Sstevel@tonic-gate 	error = vattr_to_sattr(&vattr, args.ca_sa);
20457c478bd9Sstevel@tonic-gate 	if (error) {
20467c478bd9Sstevel@tonic-gate 		/* req time field(s) overflow - return immediately */
20477c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&drp->r_rwlock);
20487c478bd9Sstevel@tonic-gate 		return (error);
20497c478bd9Sstevel@tonic-gate 	}
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	douprintf = 1;
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate 	t = gethrtime();
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	error = rfs2call(VTOMI(dvp), RFS_CREATE,
20567c478bd9Sstevel@tonic-gate 	    xdr_creatargs, (caddr_t)&args,
20577c478bd9Sstevel@tonic-gate 	    xdr_diropres, (caddr_t)&dr, cr,
20587c478bd9Sstevel@tonic-gate 	    &douprintf, &dr.dr_status, 0, NULL);
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	PURGE_ATTRCACHE(dvp);	/* mod time changed */
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	if (!error) {
20637c478bd9Sstevel@tonic-gate 		error = geterrno(dr.dr_status);
20647c478bd9Sstevel@tonic-gate 		if (!error) {
20657c478bd9Sstevel@tonic-gate 			if (HAVE_RDDIR_CACHE(drp))
20667c478bd9Sstevel@tonic-gate 				nfs_purge_rddir_cache(dvp);
20677c478bd9Sstevel@tonic-gate 			vp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
20687c478bd9Sstevel@tonic-gate 			    dvp->v_vfsp, t, cr, NULL, NULL);
20697c478bd9Sstevel@tonic-gate 			/*
20707c478bd9Sstevel@tonic-gate 			 * If NFS_ACL is supported on the server, then the
20717c478bd9Sstevel@tonic-gate 			 * attributes returned by server may have minimal
20727c478bd9Sstevel@tonic-gate 			 * permissions sometimes denying access to users having
20737c478bd9Sstevel@tonic-gate 			 * proper access.  To get the proper attributes, mark
20747c478bd9Sstevel@tonic-gate 			 * the attributes as expired so that they will be
20757c478bd9Sstevel@tonic-gate 			 * regotten via the NFS_ACL GETATTR2 procedure.
20767c478bd9Sstevel@tonic-gate 			 */
20777c478bd9Sstevel@tonic-gate 			if (VTOMI(vp)->mi_flags & MI_ACL) {
20787c478bd9Sstevel@tonic-gate 				PURGE_ATTRCACHE(vp);
20797c478bd9Sstevel@tonic-gate 			}
20807c478bd9Sstevel@tonic-gate 			dnlc_update(dvp, nm, vp);
20817c478bd9Sstevel@tonic-gate 			rp = VTOR(vp);
20827c478bd9Sstevel@tonic-gate 			if (vattr.va_size == 0) {
20837c478bd9Sstevel@tonic-gate 				mutex_enter(&rp->r_statelock);
20847c478bd9Sstevel@tonic-gate 				rp->r_size = 0;
20857c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
20867c478bd9Sstevel@tonic-gate 				if (vn_has_cached_data(vp)) {
20877c478bd9Sstevel@tonic-gate 					ASSERT(vp->v_type != VCHR);
20887c478bd9Sstevel@tonic-gate 					nfs_invalidate_pages(vp,
20897c478bd9Sstevel@tonic-gate 					    (u_offset_t)0, cr);
20907c478bd9Sstevel@tonic-gate 				}
20917c478bd9Sstevel@tonic-gate 			}
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 			/*
20947c478bd9Sstevel@tonic-gate 			 * Make sure the gid was set correctly.
20957c478bd9Sstevel@tonic-gate 			 * If not, try to set it (but don't lose
20967c478bd9Sstevel@tonic-gate 			 * any sleep over it).
20977c478bd9Sstevel@tonic-gate 			 */
20987c478bd9Sstevel@tonic-gate 			if (vattr.va_gid != rp->r_attr.va_gid) {
20997c478bd9Sstevel@tonic-gate 				vattr.va_mask = AT_GID;
21007c478bd9Sstevel@tonic-gate 				(void) nfssetattr(vp, &vattr, 0, cr);
21017c478bd9Sstevel@tonic-gate 			}
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 			/*
21047c478bd9Sstevel@tonic-gate 			 * If vnode is a device create special vnode
21057c478bd9Sstevel@tonic-gate 			 */
21067c478bd9Sstevel@tonic-gate 			if (IS_DEVVP(vp)) {
21077c478bd9Sstevel@tonic-gate 				*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
21087c478bd9Sstevel@tonic-gate 				VN_RELE(vp);
21097c478bd9Sstevel@tonic-gate 			} else
21107c478bd9Sstevel@tonic-gate 				*vpp = vp;
21117c478bd9Sstevel@tonic-gate 		} else {
21127c478bd9Sstevel@tonic-gate 			PURGE_STALE_FH(error, dvp, cr);
21137c478bd9Sstevel@tonic-gate 		}
21147c478bd9Sstevel@tonic-gate 	}
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&drp->r_rwlock);
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 	return (error);
21197c478bd9Sstevel@tonic-gate }
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate /*
21227c478bd9Sstevel@tonic-gate  * Weirdness: if the vnode to be removed is open
21237c478bd9Sstevel@tonic-gate  * we rename it instead of removing it and nfs_inactive
21247c478bd9Sstevel@tonic-gate  * will remove the new name.
21257c478bd9Sstevel@tonic-gate  */
21267c478bd9Sstevel@tonic-gate static int
21277c478bd9Sstevel@tonic-gate nfs_remove(vnode_t *dvp, char *nm, cred_t *cr)
21287c478bd9Sstevel@tonic-gate {
21297c478bd9Sstevel@tonic-gate 	int error;
21307c478bd9Sstevel@tonic-gate 	struct nfsdiropargs da;
21317c478bd9Sstevel@tonic-gate 	enum nfsstat status;
21327c478bd9Sstevel@tonic-gate 	vnode_t *vp;
21337c478bd9Sstevel@tonic-gate 	char *tmpname;
21347c478bd9Sstevel@tonic-gate 	int douprintf;
21357c478bd9Sstevel@tonic-gate 	rnode_t *rp;
21367c478bd9Sstevel@tonic-gate 	rnode_t *drp;
21377c478bd9Sstevel@tonic-gate 
2138108322fbScarlsonj 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
21397c478bd9Sstevel@tonic-gate 		return (EPERM);
21407c478bd9Sstevel@tonic-gate 	drp = VTOR(dvp);
21417c478bd9Sstevel@tonic-gate 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
21427c478bd9Sstevel@tonic-gate 		return (EINTR);
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 	error = nfslookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
21457c478bd9Sstevel@tonic-gate 	if (error) {
21467c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&drp->r_rwlock);
21477c478bd9Sstevel@tonic-gate 		return (error);
21487c478bd9Sstevel@tonic-gate 	}
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR && secpolicy_fs_linkdir(cr, dvp->v_vfsp)) {
21517c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
21527c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&drp->r_rwlock);
21537c478bd9Sstevel@tonic-gate 		return (EPERM);
21547c478bd9Sstevel@tonic-gate 	}
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	/*
21577c478bd9Sstevel@tonic-gate 	 * First just remove the entry from the name cache, as it
21587c478bd9Sstevel@tonic-gate 	 * is most likely the only entry for this vp.
21597c478bd9Sstevel@tonic-gate 	 */
21607c478bd9Sstevel@tonic-gate 	dnlc_remove(dvp, nm);
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 	/*
21637c478bd9Sstevel@tonic-gate 	 * If the file has a v_count > 1 then there may be more than one
21647c478bd9Sstevel@tonic-gate 	 * entry in the name cache due multiple links or an open file,
21657c478bd9Sstevel@tonic-gate 	 * but we don't have the real reference count so flush all
21667c478bd9Sstevel@tonic-gate 	 * possible entries.
21677c478bd9Sstevel@tonic-gate 	 */
21687c478bd9Sstevel@tonic-gate 	if (vp->v_count > 1)
21697c478bd9Sstevel@tonic-gate 		dnlc_purge_vp(vp);
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate 	/*
21727c478bd9Sstevel@tonic-gate 	 * Now we have the real reference count on the vnode
21737c478bd9Sstevel@tonic-gate 	 */
21747c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
21757c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
21767c478bd9Sstevel@tonic-gate 	if (vp->v_count > 1 &&
21777c478bd9Sstevel@tonic-gate 	    (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
21787c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
21797c478bd9Sstevel@tonic-gate 		tmpname = newname();
21807c478bd9Sstevel@tonic-gate 		error = nfsrename(dvp, nm, dvp, tmpname, cr);
21817c478bd9Sstevel@tonic-gate 		if (error)
21827c478bd9Sstevel@tonic-gate 			kmem_free(tmpname, MAXNAMELEN);
21837c478bd9Sstevel@tonic-gate 		else {
21847c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
21857c478bd9Sstevel@tonic-gate 			if (rp->r_unldvp == NULL) {
21867c478bd9Sstevel@tonic-gate 				VN_HOLD(dvp);
21877c478bd9Sstevel@tonic-gate 				rp->r_unldvp = dvp;
21887c478bd9Sstevel@tonic-gate 				if (rp->r_unlcred != NULL)
21897c478bd9Sstevel@tonic-gate 					crfree(rp->r_unlcred);
21907c478bd9Sstevel@tonic-gate 				crhold(cr);
21917c478bd9Sstevel@tonic-gate 				rp->r_unlcred = cr;
21927c478bd9Sstevel@tonic-gate 				rp->r_unlname = tmpname;
21937c478bd9Sstevel@tonic-gate 			} else {
21947c478bd9Sstevel@tonic-gate 				kmem_free(rp->r_unlname, MAXNAMELEN);
21957c478bd9Sstevel@tonic-gate 				rp->r_unlname = tmpname;
21967c478bd9Sstevel@tonic-gate 			}
21977c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
21987c478bd9Sstevel@tonic-gate 		}
21997c478bd9Sstevel@tonic-gate 	} else {
22007c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
22017c478bd9Sstevel@tonic-gate 		/*
22027c478bd9Sstevel@tonic-gate 		 * We need to flush any dirty pages which happen to
22037c478bd9Sstevel@tonic-gate 		 * be hanging around before removing the file.  This
22047c478bd9Sstevel@tonic-gate 		 * shouldn't happen very often and mostly on file
22057c478bd9Sstevel@tonic-gate 		 * systems mounted "nocto".
22067c478bd9Sstevel@tonic-gate 		 */
22077c478bd9Sstevel@tonic-gate 		if (vn_has_cached_data(vp) &&
22087c478bd9Sstevel@tonic-gate 		    ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
22097c478bd9Sstevel@tonic-gate 			error = nfs_putpage(vp, (offset_t)0, 0, 0, cr);
22107c478bd9Sstevel@tonic-gate 			if (error && (error == ENOSPC || error == EDQUOT)) {
22117c478bd9Sstevel@tonic-gate 				mutex_enter(&rp->r_statelock);
22127c478bd9Sstevel@tonic-gate 				if (!rp->r_error)
22137c478bd9Sstevel@tonic-gate 					rp->r_error = error;
22147c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
22157c478bd9Sstevel@tonic-gate 			}
22167c478bd9Sstevel@tonic-gate 		}
22177c478bd9Sstevel@tonic-gate 
22187c478bd9Sstevel@tonic-gate 		setdiropargs(&da, nm, dvp);
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 		douprintf = 1;
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 		error = rfs2call(VTOMI(dvp), RFS_REMOVE,
22237c478bd9Sstevel@tonic-gate 		    xdr_diropargs, (caddr_t)&da,
22247c478bd9Sstevel@tonic-gate 		    xdr_enum, (caddr_t)&status, cr,
22257c478bd9Sstevel@tonic-gate 		    &douprintf, &status, 0, NULL);
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 		/*
22287c478bd9Sstevel@tonic-gate 		 * The xattr dir may be gone after last attr is removed,
22297c478bd9Sstevel@tonic-gate 		 * so flush it from dnlc.
22307c478bd9Sstevel@tonic-gate 		 */
22317c478bd9Sstevel@tonic-gate 		if (dvp->v_flag & V_XATTRDIR)
22327c478bd9Sstevel@tonic-gate 			dnlc_purge_vp(dvp);
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate 		PURGE_ATTRCACHE(dvp);	/* mod time changed */
22357c478bd9Sstevel@tonic-gate 		PURGE_ATTRCACHE(vp);	/* link count changed */
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate 		if (!error) {
22387c478bd9Sstevel@tonic-gate 			error = geterrno(status);
22397c478bd9Sstevel@tonic-gate 			if (!error) {
22407c478bd9Sstevel@tonic-gate 				if (HAVE_RDDIR_CACHE(drp))
22417c478bd9Sstevel@tonic-gate 					nfs_purge_rddir_cache(dvp);
22427c478bd9Sstevel@tonic-gate 			} else {
22437c478bd9Sstevel@tonic-gate 				PURGE_STALE_FH(error, dvp, cr);
22447c478bd9Sstevel@tonic-gate 			}
22457c478bd9Sstevel@tonic-gate 		}
22467c478bd9Sstevel@tonic-gate 	}
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&drp->r_rwlock);
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	return (error);
22537c478bd9Sstevel@tonic-gate }
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate static int
22567c478bd9Sstevel@tonic-gate nfs_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr)
22577c478bd9Sstevel@tonic-gate {
22587c478bd9Sstevel@tonic-gate 	int error;
22597c478bd9Sstevel@tonic-gate 	struct nfslinkargs args;
22607c478bd9Sstevel@tonic-gate 	enum nfsstat status;
22617c478bd9Sstevel@tonic-gate 	vnode_t *realvp;
22627c478bd9Sstevel@tonic-gate 	int douprintf;
22637c478bd9Sstevel@tonic-gate 	rnode_t *tdrp;
22647c478bd9Sstevel@tonic-gate 
2265108322fbScarlsonj 	if (nfs_zone() != VTOMI(tdvp)->mi_zone)
22667c478bd9Sstevel@tonic-gate 		return (EPERM);
22677c478bd9Sstevel@tonic-gate 	if (VOP_REALVP(svp, &realvp) == 0)
22687c478bd9Sstevel@tonic-gate 		svp = realvp;
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 	args.la_from = VTOFH(svp);
22717c478bd9Sstevel@tonic-gate 	setdiropargs(&args.la_to, tnm, tdvp);
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 	tdrp = VTOR(tdvp);
22747c478bd9Sstevel@tonic-gate 	if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR(tdvp)))
22757c478bd9Sstevel@tonic-gate 		return (EINTR);
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 	dnlc_remove(tdvp, tnm);
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate 	douprintf = 1;
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 	error = rfs2call(VTOMI(svp), RFS_LINK,
22827c478bd9Sstevel@tonic-gate 	    xdr_linkargs, (caddr_t)&args,
22837c478bd9Sstevel@tonic-gate 	    xdr_enum, (caddr_t)&status, cr,
22847c478bd9Sstevel@tonic-gate 	    &douprintf, &status, 0, NULL);
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 	PURGE_ATTRCACHE(tdvp);	/* mod time changed */
22877c478bd9Sstevel@tonic-gate 	PURGE_ATTRCACHE(svp);	/* link count changed */
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 	if (!error) {
22907c478bd9Sstevel@tonic-gate 		error = geterrno(status);
22917c478bd9Sstevel@tonic-gate 		if (!error) {
22927c478bd9Sstevel@tonic-gate 			if (HAVE_RDDIR_CACHE(tdrp))
22937c478bd9Sstevel@tonic-gate 				nfs_purge_rddir_cache(tdvp);
22947c478bd9Sstevel@tonic-gate 		}
22957c478bd9Sstevel@tonic-gate 	}
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&tdrp->r_rwlock);
22987c478bd9Sstevel@tonic-gate 
22997c478bd9Sstevel@tonic-gate 	return (error);
23007c478bd9Sstevel@tonic-gate }
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate static int
23037c478bd9Sstevel@tonic-gate nfs_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr)
23047c478bd9Sstevel@tonic-gate {
23057c478bd9Sstevel@tonic-gate 	vnode_t *realvp;
23067c478bd9Sstevel@tonic-gate 
2307108322fbScarlsonj 	if (nfs_zone() != VTOMI(odvp)->mi_zone)
23087c478bd9Sstevel@tonic-gate 		return (EPERM);
23097c478bd9Sstevel@tonic-gate 	if (VOP_REALVP(ndvp, &realvp) == 0)
23107c478bd9Sstevel@tonic-gate 		ndvp = realvp;
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 	return (nfsrename(odvp, onm, ndvp, nnm, cr));
23137c478bd9Sstevel@tonic-gate }
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate /*
23167c478bd9Sstevel@tonic-gate  * nfsrename does the real work of renaming in NFS Version 2.
23177c478bd9Sstevel@tonic-gate  */
23187c478bd9Sstevel@tonic-gate static int
23197c478bd9Sstevel@tonic-gate nfsrename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr)
23207c478bd9Sstevel@tonic-gate {
23217c478bd9Sstevel@tonic-gate 	int error;
23227c478bd9Sstevel@tonic-gate 	enum nfsstat status;
23237c478bd9Sstevel@tonic-gate 	struct nfsrnmargs args;
23247c478bd9Sstevel@tonic-gate 	int douprintf;
23257c478bd9Sstevel@tonic-gate 	vnode_t *nvp;
23267c478bd9Sstevel@tonic-gate 	vnode_t *ovp = NULL;
23277c478bd9Sstevel@tonic-gate 	char *tmpname;
23287c478bd9Sstevel@tonic-gate 	rnode_t *rp;
23297c478bd9Sstevel@tonic-gate 	rnode_t *odrp;
23307c478bd9Sstevel@tonic-gate 	rnode_t *ndrp;
23317c478bd9Sstevel@tonic-gate 
2332108322fbScarlsonj 	ASSERT(nfs_zone() == VTOMI(odvp)->mi_zone);
23337c478bd9Sstevel@tonic-gate 	if (strcmp(onm, ".") == 0 || strcmp(onm, "..") == 0 ||
23347c478bd9Sstevel@tonic-gate 	    strcmp(nnm, ".") == 0 || strcmp(nnm, "..") == 0)
23357c478bd9Sstevel@tonic-gate 		return (EINVAL);
23367c478bd9Sstevel@tonic-gate 
23377c478bd9Sstevel@tonic-gate 	odrp = VTOR(odvp);
23387c478bd9Sstevel@tonic-gate 	ndrp = VTOR(ndvp);
23397c478bd9Sstevel@tonic-gate 	if ((intptr_t)odrp < (intptr_t)ndrp) {
23407c478bd9Sstevel@tonic-gate 		if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp)))
23417c478bd9Sstevel@tonic-gate 			return (EINTR);
23427c478bd9Sstevel@tonic-gate 		if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp))) {
23437c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&odrp->r_rwlock);
23447c478bd9Sstevel@tonic-gate 			return (EINTR);
23457c478bd9Sstevel@tonic-gate 		}
23467c478bd9Sstevel@tonic-gate 	} else {
23477c478bd9Sstevel@tonic-gate 		if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp)))
23487c478bd9Sstevel@tonic-gate 			return (EINTR);
23497c478bd9Sstevel@tonic-gate 		if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp))) {
23507c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&ndrp->r_rwlock);
23517c478bd9Sstevel@tonic-gate 			return (EINTR);
23527c478bd9Sstevel@tonic-gate 		}
23537c478bd9Sstevel@tonic-gate 	}
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	/*
23567c478bd9Sstevel@tonic-gate 	 * Lookup the target file.  If it exists, it needs to be
23577c478bd9Sstevel@tonic-gate 	 * checked to see whether it is a mount point and whether
23587c478bd9Sstevel@tonic-gate 	 * it is active (open).
23597c478bd9Sstevel@tonic-gate 	 */
23607c478bd9Sstevel@tonic-gate 	error = nfslookup(ndvp, nnm, &nvp, NULL, 0, NULL, cr, 0);
23617c478bd9Sstevel@tonic-gate 	if (!error) {
23627c478bd9Sstevel@tonic-gate 		/*
23637c478bd9Sstevel@tonic-gate 		 * If this file has been mounted on, then just
23647c478bd9Sstevel@tonic-gate 		 * return busy because renaming to it would remove
23657c478bd9Sstevel@tonic-gate 		 * the mounted file system from the name space.
23667c478bd9Sstevel@tonic-gate 		 */
23677c478bd9Sstevel@tonic-gate 		if (vn_mountedvfs(nvp) != NULL) {
23687c478bd9Sstevel@tonic-gate 			VN_RELE(nvp);
23697c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&odrp->r_rwlock);
23707c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&ndrp->r_rwlock);
23717c478bd9Sstevel@tonic-gate 			return (EBUSY);
23727c478bd9Sstevel@tonic-gate 		}
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate 		/*
23757c478bd9Sstevel@tonic-gate 		 * Purge the name cache of all references to this vnode
23767c478bd9Sstevel@tonic-gate 		 * so that we can check the reference count to infer
23777c478bd9Sstevel@tonic-gate 		 * whether it is active or not.
23787c478bd9Sstevel@tonic-gate 		 */
23797c478bd9Sstevel@tonic-gate 		/*
23807c478bd9Sstevel@tonic-gate 		 * First just remove the entry from the name cache, as it
23817c478bd9Sstevel@tonic-gate 		 * is most likely the only entry for this vp.
23827c478bd9Sstevel@tonic-gate 		 */
23837c478bd9Sstevel@tonic-gate 		dnlc_remove(ndvp, nnm);
23847c478bd9Sstevel@tonic-gate 		/*
23857c478bd9Sstevel@tonic-gate 		 * If the file has a v_count > 1 then there may be more
23867c478bd9Sstevel@tonic-gate 		 * than one entry in the name cache due multiple links
23877c478bd9Sstevel@tonic-gate 		 * or an open file, but we don't have the real reference
23887c478bd9Sstevel@tonic-gate 		 * count so flush all possible entries.
23897c478bd9Sstevel@tonic-gate 		 */
23907c478bd9Sstevel@tonic-gate 		if (nvp->v_count > 1)
23917c478bd9Sstevel@tonic-gate 			dnlc_purge_vp(nvp);
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 		/*
23947c478bd9Sstevel@tonic-gate 		 * If the vnode is active and is not a directory,
23957c478bd9Sstevel@tonic-gate 		 * arrange to rename it to a
23967c478bd9Sstevel@tonic-gate 		 * temporary file so that it will continue to be
23977c478bd9Sstevel@tonic-gate 		 * accessible.  This implements the "unlink-open-file"
23987c478bd9Sstevel@tonic-gate 		 * semantics for the target of a rename operation.
23997c478bd9Sstevel@tonic-gate 		 * Before doing this though, make sure that the
24007c478bd9Sstevel@tonic-gate 		 * source and target files are not already the same.
24017c478bd9Sstevel@tonic-gate 		 */
24027c478bd9Sstevel@tonic-gate 		if (nvp->v_count > 1 && nvp->v_type != VDIR) {
24037c478bd9Sstevel@tonic-gate 			/*
24047c478bd9Sstevel@tonic-gate 			 * Lookup the source name.
24057c478bd9Sstevel@tonic-gate 			 */
24067c478bd9Sstevel@tonic-gate 			error = nfslookup(odvp, onm, &ovp, NULL, 0, NULL,
24077c478bd9Sstevel@tonic-gate 			    cr, 0);
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 			/*
24107c478bd9Sstevel@tonic-gate 			 * The source name *should* already exist.
24117c478bd9Sstevel@tonic-gate 			 */
24127c478bd9Sstevel@tonic-gate 			if (error) {
24137c478bd9Sstevel@tonic-gate 				VN_RELE(nvp);
24147c478bd9Sstevel@tonic-gate 				nfs_rw_exit(&odrp->r_rwlock);
24157c478bd9Sstevel@tonic-gate 				nfs_rw_exit(&ndrp->r_rwlock);
24167c478bd9Sstevel@tonic-gate 				return (error);
24177c478bd9Sstevel@tonic-gate 			}
24187c478bd9Sstevel@tonic-gate 
24197c478bd9Sstevel@tonic-gate 			/*
24207c478bd9Sstevel@tonic-gate 			 * Compare the two vnodes.  If they are the same,
24217c478bd9Sstevel@tonic-gate 			 * just release all held vnodes and return success.
24227c478bd9Sstevel@tonic-gate 			 */
24237c478bd9Sstevel@tonic-gate 			if (ovp == nvp) {
24247c478bd9Sstevel@tonic-gate 				VN_RELE(ovp);
24257c478bd9Sstevel@tonic-gate 				VN_RELE(nvp);
24267c478bd9Sstevel@tonic-gate 				nfs_rw_exit(&odrp->r_rwlock);
24277c478bd9Sstevel@tonic-gate 				nfs_rw_exit(&ndrp->r_rwlock);
24287c478bd9Sstevel@tonic-gate 				return (0);
24297c478bd9Sstevel@tonic-gate 			}
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 			/*
24327c478bd9Sstevel@tonic-gate 			 * Can't mix and match directories and non-
24337c478bd9Sstevel@tonic-gate 			 * directories in rename operations.  We already
24347c478bd9Sstevel@tonic-gate 			 * know that the target is not a directory.  If
24357c478bd9Sstevel@tonic-gate 			 * the source is a directory, return an error.
24367c478bd9Sstevel@tonic-gate 			 */
24377c478bd9Sstevel@tonic-gate 			if (ovp->v_type == VDIR) {
24387c478bd9Sstevel@tonic-gate 				VN_RELE(ovp);
24397c478bd9Sstevel@tonic-gate 				VN_RELE(nvp);
24407c478bd9Sstevel@tonic-gate 				nfs_rw_exit(&odrp->r_rwlock);
24417c478bd9Sstevel@tonic-gate 				nfs_rw_exit(&ndrp->r_rwlock);
24427c478bd9Sstevel@tonic-gate 				return (ENOTDIR);
24437c478bd9Sstevel@tonic-gate 			}
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate 			/*
24467c478bd9Sstevel@tonic-gate 			 * The target file exists, is not the same as
24477c478bd9Sstevel@tonic-gate 			 * the source file, and is active.  Link it
24487c478bd9Sstevel@tonic-gate 			 * to a temporary filename to avoid having
24497c478bd9Sstevel@tonic-gate 			 * the server removing the file completely.
24507c478bd9Sstevel@tonic-gate 			 */
24517c478bd9Sstevel@tonic-gate 			tmpname = newname();
24527c478bd9Sstevel@tonic-gate 			error = nfs_link(ndvp, nvp, tmpname, cr);
24537c478bd9Sstevel@tonic-gate 			if (error == EOPNOTSUPP) {
24547c478bd9Sstevel@tonic-gate 				error = nfs_rename(ndvp, nnm, ndvp, tmpname,
24557c478bd9Sstevel@tonic-gate 				    cr);
24567c478bd9Sstevel@tonic-gate 			}
24577c478bd9Sstevel@tonic-gate 			if (error) {
24587c478bd9Sstevel@tonic-gate 				kmem_free(tmpname, MAXNAMELEN);
24597c478bd9Sstevel@tonic-gate 				VN_RELE(ovp);
24607c478bd9Sstevel@tonic-gate 				VN_RELE(nvp);
24617c478bd9Sstevel@tonic-gate 				nfs_rw_exit(&odrp->r_rwlock);
24627c478bd9Sstevel@tonic-gate 				nfs_rw_exit(&ndrp->r_rwlock);
24637c478bd9Sstevel@tonic-gate 				return (error);
24647c478bd9Sstevel@tonic-gate 			}
24657c478bd9Sstevel@tonic-gate 			rp = VTOR(nvp);
24667c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
24677c478bd9Sstevel@tonic-gate 			if (rp->r_unldvp == NULL) {
24687c478bd9Sstevel@tonic-gate 				VN_HOLD(ndvp);
24697c478bd9Sstevel@tonic-gate 				rp->r_unldvp = ndvp;
24707c478bd9Sstevel@tonic-gate 				if (rp->r_unlcred != NULL)
24717c478bd9Sstevel@tonic-gate 					crfree(rp->r_unlcred);
24727c478bd9Sstevel@tonic-gate 				crhold(cr);
24737c478bd9Sstevel@tonic-gate 				rp->r_unlcred = cr;
24747c478bd9Sstevel@tonic-gate 				rp->r_unlname = tmpname;
24757c478bd9Sstevel@tonic-gate 			} else {
24767c478bd9Sstevel@tonic-gate 				kmem_free(rp->r_unlname, MAXNAMELEN);
24777c478bd9Sstevel@tonic-gate 				rp->r_unlname = tmpname;
24787c478bd9Sstevel@tonic-gate 			}
24797c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
24807c478bd9Sstevel@tonic-gate 		}
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 		VN_RELE(nvp);
24837c478bd9Sstevel@tonic-gate 	}
24847c478bd9Sstevel@tonic-gate 
24857c478bd9Sstevel@tonic-gate 	if (ovp == NULL) {
24867c478bd9Sstevel@tonic-gate 		/*
24877c478bd9Sstevel@tonic-gate 		 * When renaming directories to be a subdirectory of a
24887c478bd9Sstevel@tonic-gate 		 * different parent, the dnlc entry for ".." will no
24897c478bd9Sstevel@tonic-gate 		 * longer be valid, so it must be removed.
24907c478bd9Sstevel@tonic-gate 		 *
24917c478bd9Sstevel@tonic-gate 		 * We do a lookup here to determine whether we are renaming
24927c478bd9Sstevel@tonic-gate 		 * a directory and we need to check if we are renaming
24937c478bd9Sstevel@tonic-gate 		 * an unlinked file.  This might have already been done
24947c478bd9Sstevel@tonic-gate 		 * in previous code, so we check ovp == NULL to avoid
24957c478bd9Sstevel@tonic-gate 		 * doing it twice.
24967c478bd9Sstevel@tonic-gate 		 */
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate 		error = nfslookup(odvp, onm, &ovp, NULL, 0, NULL, cr, 0);
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate 		/*
25017c478bd9Sstevel@tonic-gate 		 * The source name *should* already exist.
25027c478bd9Sstevel@tonic-gate 		 */
25037c478bd9Sstevel@tonic-gate 		if (error) {
25047c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&odrp->r_rwlock);
25057c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&ndrp->r_rwlock);
25067c478bd9Sstevel@tonic-gate 			return (error);
25077c478bd9Sstevel@tonic-gate 		}
25087c478bd9Sstevel@tonic-gate 		ASSERT(ovp != NULL);
25097c478bd9Sstevel@tonic-gate 	}
25107c478bd9Sstevel@tonic-gate 
25117c478bd9Sstevel@tonic-gate 	dnlc_remove(odvp, onm);
25127c478bd9Sstevel@tonic-gate 	dnlc_remove(ndvp, nnm);
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	setdiropargs(&args.rna_from, onm, odvp);
25157c478bd9Sstevel@tonic-gate 	setdiropargs(&args.rna_to, nnm, ndvp);
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	douprintf = 1;
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	error = rfs2call(VTOMI(odvp), RFS_RENAME,
25207c478bd9Sstevel@tonic-gate 	    xdr_rnmargs, (caddr_t)&args,
25217c478bd9Sstevel@tonic-gate 	    xdr_enum, (caddr_t)&status, cr,
25227c478bd9Sstevel@tonic-gate 	    &douprintf, &status, 0, NULL);
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate 	PURGE_ATTRCACHE(odvp);	/* mod time changed */
25257c478bd9Sstevel@tonic-gate 	PURGE_ATTRCACHE(ndvp);	/* mod time changed */
25267c478bd9Sstevel@tonic-gate 
25277c478bd9Sstevel@tonic-gate 	if (!error) {
25287c478bd9Sstevel@tonic-gate 		error = geterrno(status);
25297c478bd9Sstevel@tonic-gate 		if (!error) {
25307c478bd9Sstevel@tonic-gate 			if (HAVE_RDDIR_CACHE(odrp))
25317c478bd9Sstevel@tonic-gate 				nfs_purge_rddir_cache(odvp);
25327c478bd9Sstevel@tonic-gate 			if (HAVE_RDDIR_CACHE(ndrp))
25337c478bd9Sstevel@tonic-gate 				nfs_purge_rddir_cache(ndvp);
25347c478bd9Sstevel@tonic-gate 			/*
25357c478bd9Sstevel@tonic-gate 			 * when renaming directories to be a subdirectory of a
25367c478bd9Sstevel@tonic-gate 			 * different parent, the dnlc entry for ".." will no
25377c478bd9Sstevel@tonic-gate 			 * longer be valid, so it must be removed
25387c478bd9Sstevel@tonic-gate 			 */
25397c478bd9Sstevel@tonic-gate 			rp = VTOR(ovp);
25407c478bd9Sstevel@tonic-gate 			if (ndvp != odvp) {
25417c478bd9Sstevel@tonic-gate 				if (ovp->v_type == VDIR) {
25427c478bd9Sstevel@tonic-gate 					dnlc_remove(ovp, "..");
25437c478bd9Sstevel@tonic-gate 					if (HAVE_RDDIR_CACHE(rp))
25447c478bd9Sstevel@tonic-gate 						nfs_purge_rddir_cache(ovp);
25457c478bd9Sstevel@tonic-gate 				}
25467c478bd9Sstevel@tonic-gate 			}
25477c478bd9Sstevel@tonic-gate 
25487c478bd9Sstevel@tonic-gate 			/*
25497c478bd9Sstevel@tonic-gate 			 * If we are renaming the unlinked file, update the
25507c478bd9Sstevel@tonic-gate 			 * r_unldvp and r_unlname as needed.
25517c478bd9Sstevel@tonic-gate 			 */
25527c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
25537c478bd9Sstevel@tonic-gate 			if (rp->r_unldvp != NULL) {
25547c478bd9Sstevel@tonic-gate 				if (strcmp(rp->r_unlname, onm) == 0) {
25557c478bd9Sstevel@tonic-gate 					(void) strncpy(rp->r_unlname,
25567c478bd9Sstevel@tonic-gate 						    nnm, MAXNAMELEN);
25577c478bd9Sstevel@tonic-gate 					rp->r_unlname[MAXNAMELEN - 1] = '\0';
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 					if (ndvp != rp->r_unldvp) {
25607c478bd9Sstevel@tonic-gate 						VN_RELE(rp->r_unldvp);
25617c478bd9Sstevel@tonic-gate 						rp->r_unldvp = ndvp;
25627c478bd9Sstevel@tonic-gate 						VN_HOLD(ndvp);
25637c478bd9Sstevel@tonic-gate 					}
25647c478bd9Sstevel@tonic-gate 				}
25657c478bd9Sstevel@tonic-gate 			}
25667c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
25677c478bd9Sstevel@tonic-gate 		} else {
25687c478bd9Sstevel@tonic-gate 			/*
25697c478bd9Sstevel@tonic-gate 			 * System V defines rename to return EEXIST, not
25707c478bd9Sstevel@tonic-gate 			 * ENOTEMPTY if the target directory is not empty.
25717c478bd9Sstevel@tonic-gate 			 * Over the wire, the error is NFSERR_ENOTEMPTY
25727c478bd9Sstevel@tonic-gate 			 * which geterrno maps to ENOTEMPTY.
25737c478bd9Sstevel@tonic-gate 			 */
25747c478bd9Sstevel@tonic-gate 			if (error == ENOTEMPTY)
25757c478bd9Sstevel@tonic-gate 				error = EEXIST;
25767c478bd9Sstevel@tonic-gate 		}
25777c478bd9Sstevel@tonic-gate 	}
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	VN_RELE(ovp);
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&odrp->r_rwlock);
25827c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&ndrp->r_rwlock);
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 	return (error);
25857c478bd9Sstevel@tonic-gate }
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate static int
25887c478bd9Sstevel@tonic-gate nfs_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr)
25897c478bd9Sstevel@tonic-gate {
25907c478bd9Sstevel@tonic-gate 	int error;
25917c478bd9Sstevel@tonic-gate 	struct nfscreatargs args;
25927c478bd9Sstevel@tonic-gate 	struct nfsdiropres dr;
25937c478bd9Sstevel@tonic-gate 	int douprintf;
25947c478bd9Sstevel@tonic-gate 	rnode_t *drp;
25957c478bd9Sstevel@tonic-gate 	hrtime_t t;
25967c478bd9Sstevel@tonic-gate 
2597108322fbScarlsonj 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
25987c478bd9Sstevel@tonic-gate 		return (EPERM);
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 	setdiropargs(&args.ca_da, nm, dvp);
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate 	/*
26037c478bd9Sstevel@tonic-gate 	 * Decide what the group-id and set-gid bit of the created directory
26047c478bd9Sstevel@tonic-gate 	 * should be.  May have to do a setattr to get the gid right.
26057c478bd9Sstevel@tonic-gate 	 */
26067c478bd9Sstevel@tonic-gate 	error = setdirgid(dvp, &va->va_gid, cr);
26077c478bd9Sstevel@tonic-gate 	if (error)
26087c478bd9Sstevel@tonic-gate 		return (error);
26097c478bd9Sstevel@tonic-gate 	error = setdirmode(dvp, &va->va_mode, cr);
26107c478bd9Sstevel@tonic-gate 	if (error)
26117c478bd9Sstevel@tonic-gate 		return (error);
26127c478bd9Sstevel@tonic-gate 	va->va_mask |= AT_MODE|AT_GID;
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 	args.ca_sa = &args.ca_sa_buf;
26157c478bd9Sstevel@tonic-gate 	error = vattr_to_sattr(va, args.ca_sa);
26167c478bd9Sstevel@tonic-gate 	if (error) {
26177c478bd9Sstevel@tonic-gate 		/* req time field(s) overflow - return immediately */
26187c478bd9Sstevel@tonic-gate 		return (error);
26197c478bd9Sstevel@tonic-gate 	}
26207c478bd9Sstevel@tonic-gate 
26217c478bd9Sstevel@tonic-gate 	drp = VTOR(dvp);
26227c478bd9Sstevel@tonic-gate 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
26237c478bd9Sstevel@tonic-gate 		return (EINTR);
26247c478bd9Sstevel@tonic-gate 
26257c478bd9Sstevel@tonic-gate 	dnlc_remove(dvp, nm);
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 	douprintf = 1;
26287c478bd9Sstevel@tonic-gate 
26297c478bd9Sstevel@tonic-gate 	t = gethrtime();
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 	error = rfs2call(VTOMI(dvp), RFS_MKDIR,
26327c478bd9Sstevel@tonic-gate 	    xdr_creatargs, (caddr_t)&args,
26337c478bd9Sstevel@tonic-gate 	    xdr_diropres, (caddr_t)&dr, cr,
26347c478bd9Sstevel@tonic-gate 	    &douprintf, &dr.dr_status, 0, NULL);
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 	PURGE_ATTRCACHE(dvp);	/* mod time changed */
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 	if (!error) {
26397c478bd9Sstevel@tonic-gate 		error = geterrno(dr.dr_status);
26407c478bd9Sstevel@tonic-gate 		if (!error) {
26417c478bd9Sstevel@tonic-gate 			if (HAVE_RDDIR_CACHE(drp))
26427c478bd9Sstevel@tonic-gate 				nfs_purge_rddir_cache(dvp);
26437c478bd9Sstevel@tonic-gate 			/*
26447c478bd9Sstevel@tonic-gate 			 * The attributes returned by RFS_MKDIR can not
26457c478bd9Sstevel@tonic-gate 			 * be depended upon, so mark the attribute cache
26467c478bd9Sstevel@tonic-gate 			 * as purged.  A subsequent GETATTR will get the
26477c478bd9Sstevel@tonic-gate 			 * correct attributes from the server.
26487c478bd9Sstevel@tonic-gate 			 */
26497c478bd9Sstevel@tonic-gate 			*vpp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
26507c478bd9Sstevel@tonic-gate 			    dvp->v_vfsp, t, cr, NULL, NULL);
26517c478bd9Sstevel@tonic-gate 			PURGE_ATTRCACHE(*vpp);
26527c478bd9Sstevel@tonic-gate 			dnlc_update(dvp, nm, *vpp);
26537c478bd9Sstevel@tonic-gate 
26547c478bd9Sstevel@tonic-gate 			/*
26557c478bd9Sstevel@tonic-gate 			 * Make sure the gid was set correctly.
26567c478bd9Sstevel@tonic-gate 			 * If not, try to set it (but don't lose
26577c478bd9Sstevel@tonic-gate 			 * any sleep over it).
26587c478bd9Sstevel@tonic-gate 			 */
26597c478bd9Sstevel@tonic-gate 			if (va->va_gid != VTOR(*vpp)->r_attr.va_gid) {
26607c478bd9Sstevel@tonic-gate 				va->va_mask = AT_GID;
26617c478bd9Sstevel@tonic-gate 				(void) nfssetattr(*vpp, va, 0, cr);
26627c478bd9Sstevel@tonic-gate 			}
26637c478bd9Sstevel@tonic-gate 		} else {
26647c478bd9Sstevel@tonic-gate 			PURGE_STALE_FH(error, dvp, cr);
26657c478bd9Sstevel@tonic-gate 		}
26667c478bd9Sstevel@tonic-gate 	}
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&drp->r_rwlock);
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate 	return (error);
26717c478bd9Sstevel@tonic-gate }
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate static int
26747c478bd9Sstevel@tonic-gate nfs_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr)
26757c478bd9Sstevel@tonic-gate {
26767c478bd9Sstevel@tonic-gate 	int error;
26777c478bd9Sstevel@tonic-gate 	enum nfsstat status;
26787c478bd9Sstevel@tonic-gate 	struct nfsdiropargs da;
26797c478bd9Sstevel@tonic-gate 	vnode_t *vp;
26807c478bd9Sstevel@tonic-gate 	int douprintf;
26817c478bd9Sstevel@tonic-gate 	rnode_t *drp;
26827c478bd9Sstevel@tonic-gate 
2683108322fbScarlsonj 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
26847c478bd9Sstevel@tonic-gate 		return (EPERM);
26857c478bd9Sstevel@tonic-gate 	drp = VTOR(dvp);
26867c478bd9Sstevel@tonic-gate 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
26877c478bd9Sstevel@tonic-gate 		return (EINTR);
26887c478bd9Sstevel@tonic-gate 
26897c478bd9Sstevel@tonic-gate 	/*
26907c478bd9Sstevel@tonic-gate 	 * Attempt to prevent a rmdir(".") from succeeding.
26917c478bd9Sstevel@tonic-gate 	 */
26927c478bd9Sstevel@tonic-gate 	error = nfslookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
26937c478bd9Sstevel@tonic-gate 	if (error) {
26947c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&drp->r_rwlock);
26957c478bd9Sstevel@tonic-gate 		return (error);
26967c478bd9Sstevel@tonic-gate 	}
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 	if (vp == cdir) {
26997c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
27007c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&drp->r_rwlock);
27017c478bd9Sstevel@tonic-gate 		return (EINVAL);
27027c478bd9Sstevel@tonic-gate 	}
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 	setdiropargs(&da, nm, dvp);
27057c478bd9Sstevel@tonic-gate 
27067c478bd9Sstevel@tonic-gate 	/*
27077c478bd9Sstevel@tonic-gate 	 * First just remove the entry from the name cache, as it
27087c478bd9Sstevel@tonic-gate 	 * is most likely an entry for this vp.
27097c478bd9Sstevel@tonic-gate 	 */
27107c478bd9Sstevel@tonic-gate 	dnlc_remove(dvp, nm);
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 	/*
27137c478bd9Sstevel@tonic-gate 	 * If there vnode reference count is greater than one, then
27147c478bd9Sstevel@tonic-gate 	 * there may be additional references in the DNLC which will
27157c478bd9Sstevel@tonic-gate 	 * need to be purged.  First, trying removing the entry for
27167c478bd9Sstevel@tonic-gate 	 * the parent directory and see if that removes the additional
27177c478bd9Sstevel@tonic-gate 	 * reference(s).  If that doesn't do it, then use dnlc_purge_vp
27187c478bd9Sstevel@tonic-gate 	 * to completely remove any references to the directory which
27197c478bd9Sstevel@tonic-gate 	 * might still exist in the DNLC.
27207c478bd9Sstevel@tonic-gate 	 */
27217c478bd9Sstevel@tonic-gate 	if (vp->v_count > 1) {
27227c478bd9Sstevel@tonic-gate 		dnlc_remove(vp, "..");
27237c478bd9Sstevel@tonic-gate 		if (vp->v_count > 1)
27247c478bd9Sstevel@tonic-gate 			dnlc_purge_vp(vp);
27257c478bd9Sstevel@tonic-gate 	}
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate 	douprintf = 1;
27287c478bd9Sstevel@tonic-gate 
27297c478bd9Sstevel@tonic-gate 	error = rfs2call(VTOMI(dvp), RFS_RMDIR,
27307c478bd9Sstevel@tonic-gate 	    xdr_diropargs, (caddr_t)&da,
27317c478bd9Sstevel@tonic-gate 	    xdr_enum, (caddr_t)&status, cr,
27327c478bd9Sstevel@tonic-gate 	    &douprintf, &status, 0, NULL);
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	PURGE_ATTRCACHE(dvp);	/* mod time changed */
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 	if (error) {
27377c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
27387c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&drp->r_rwlock);
27397c478bd9Sstevel@tonic-gate 		return (error);
27407c478bd9Sstevel@tonic-gate 	}
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 	error = geterrno(status);
27437c478bd9Sstevel@tonic-gate 	if (!error) {
27447c478bd9Sstevel@tonic-gate 		if (HAVE_RDDIR_CACHE(drp))
27457c478bd9Sstevel@tonic-gate 			nfs_purge_rddir_cache(dvp);
27467c478bd9Sstevel@tonic-gate 		if (HAVE_RDDIR_CACHE(VTOR(vp)))
27477c478bd9Sstevel@tonic-gate 			nfs_purge_rddir_cache(vp);
27487c478bd9Sstevel@tonic-gate 	} else {
27497c478bd9Sstevel@tonic-gate 		PURGE_STALE_FH(error, dvp, cr);
27507c478bd9Sstevel@tonic-gate 		/*
27517c478bd9Sstevel@tonic-gate 		 * System V defines rmdir to return EEXIST, not
27527c478bd9Sstevel@tonic-gate 		 * ENOTEMPTY if the directory is not empty.  Over
27537c478bd9Sstevel@tonic-gate 		 * the wire, the error is NFSERR_ENOTEMPTY which
27547c478bd9Sstevel@tonic-gate 		 * geterrno maps to ENOTEMPTY.
27557c478bd9Sstevel@tonic-gate 		 */
27567c478bd9Sstevel@tonic-gate 		if (error == ENOTEMPTY)
27577c478bd9Sstevel@tonic-gate 			error = EEXIST;
27587c478bd9Sstevel@tonic-gate 	}
27597c478bd9Sstevel@tonic-gate 
27607c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&drp->r_rwlock);
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 	return (error);
27657c478bd9Sstevel@tonic-gate }
27667c478bd9Sstevel@tonic-gate 
27677c478bd9Sstevel@tonic-gate static int
27687c478bd9Sstevel@tonic-gate nfs_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr)
27697c478bd9Sstevel@tonic-gate {
27707c478bd9Sstevel@tonic-gate 	int error;
27717c478bd9Sstevel@tonic-gate 	struct nfsslargs args;
27727c478bd9Sstevel@tonic-gate 	enum nfsstat status;
27737c478bd9Sstevel@tonic-gate 	int douprintf;
27747c478bd9Sstevel@tonic-gate 	rnode_t *drp;
27757c478bd9Sstevel@tonic-gate 
2776108322fbScarlsonj 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
27777c478bd9Sstevel@tonic-gate 		return (EPERM);
27787c478bd9Sstevel@tonic-gate 	setdiropargs(&args.sla_from, lnm, dvp);
27797c478bd9Sstevel@tonic-gate 	args.sla_sa = &args.sla_sa_buf;
27807c478bd9Sstevel@tonic-gate 	error = vattr_to_sattr(tva, args.sla_sa);
27817c478bd9Sstevel@tonic-gate 	if (error) {
27827c478bd9Sstevel@tonic-gate 		/* req time field(s) overflow - return immediately */
27837c478bd9Sstevel@tonic-gate 		return (error);
27847c478bd9Sstevel@tonic-gate 	}
27857c478bd9Sstevel@tonic-gate 	args.sla_tnm = tnm;
27867c478bd9Sstevel@tonic-gate 
27877c478bd9Sstevel@tonic-gate 	drp = VTOR(dvp);
27887c478bd9Sstevel@tonic-gate 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
27897c478bd9Sstevel@tonic-gate 		return (EINTR);
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	dnlc_remove(dvp, lnm);
27927c478bd9Sstevel@tonic-gate 
27937c478bd9Sstevel@tonic-gate 	douprintf = 1;
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 	error = rfs2call(VTOMI(dvp), RFS_SYMLINK,
27967c478bd9Sstevel@tonic-gate 	    xdr_slargs, (caddr_t)&args,
27977c478bd9Sstevel@tonic-gate 	    xdr_enum, (caddr_t)&status, cr,
27987c478bd9Sstevel@tonic-gate 	    &douprintf, &status, 0, NULL);
27997c478bd9Sstevel@tonic-gate 
28007c478bd9Sstevel@tonic-gate 	PURGE_ATTRCACHE(dvp);	/* mod time changed */
28017c478bd9Sstevel@tonic-gate 
28027c478bd9Sstevel@tonic-gate 	if (!error) {
28037c478bd9Sstevel@tonic-gate 		error = geterrno(status);
28047c478bd9Sstevel@tonic-gate 		if (!error) {
28057c478bd9Sstevel@tonic-gate 			if (HAVE_RDDIR_CACHE(drp))
28067c478bd9Sstevel@tonic-gate 				nfs_purge_rddir_cache(dvp);
28077c478bd9Sstevel@tonic-gate 		} else {
28087c478bd9Sstevel@tonic-gate 			PURGE_STALE_FH(error, dvp, cr);
28097c478bd9Sstevel@tonic-gate 		}
28107c478bd9Sstevel@tonic-gate 	}
28117c478bd9Sstevel@tonic-gate 
28127c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&drp->r_rwlock);
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate 	return (error);
28157c478bd9Sstevel@tonic-gate }
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate #ifdef DEBUG
28187c478bd9Sstevel@tonic-gate static int nfs_readdir_cache_hits = 0;
28197c478bd9Sstevel@tonic-gate static int nfs_readdir_cache_shorts = 0;
28207c478bd9Sstevel@tonic-gate static int nfs_readdir_cache_waits = 0;
28217c478bd9Sstevel@tonic-gate static int nfs_readdir_cache_misses = 0;
28227c478bd9Sstevel@tonic-gate static int nfs_readdir_readahead = 0;
28237c478bd9Sstevel@tonic-gate #endif
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate static int nfs_shrinkreaddir = 0;
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate /*
28287c478bd9Sstevel@tonic-gate  * Read directory entries.
28297c478bd9Sstevel@tonic-gate  * There are some weird things to look out for here.  The uio_offset
28307c478bd9Sstevel@tonic-gate  * field is either 0 or it is the offset returned from a previous
28317c478bd9Sstevel@tonic-gate  * readdir.  It is an opaque value used by the server to find the
28327c478bd9Sstevel@tonic-gate  * correct directory block to read. The count field is the number
28337c478bd9Sstevel@tonic-gate  * of blocks to read on the server.  This is advisory only, the server
28347c478bd9Sstevel@tonic-gate  * may return only one block's worth of entries.  Entries may be compressed
28357c478bd9Sstevel@tonic-gate  * on the server.
28367c478bd9Sstevel@tonic-gate  */
28377c478bd9Sstevel@tonic-gate static int
28387c478bd9Sstevel@tonic-gate nfs_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp)
28397c478bd9Sstevel@tonic-gate {
28407c478bd9Sstevel@tonic-gate 	int error;
28417c478bd9Sstevel@tonic-gate 	size_t count;
28427c478bd9Sstevel@tonic-gate 	rnode_t *rp;
28437c478bd9Sstevel@tonic-gate 	rddir_cache *rdc;
28447c478bd9Sstevel@tonic-gate 	rddir_cache *nrdc;
28457c478bd9Sstevel@tonic-gate 	rddir_cache *rrdc;
28467c478bd9Sstevel@tonic-gate #ifdef DEBUG
28477c478bd9Sstevel@tonic-gate 	int missed;
28487c478bd9Sstevel@tonic-gate #endif
28497c478bd9Sstevel@tonic-gate 	rddir_cache srdc;
28507c478bd9Sstevel@tonic-gate 	avl_index_t where;
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2855108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
28567c478bd9Sstevel@tonic-gate 		return (EIO);
28577c478bd9Sstevel@tonic-gate 	/*
28587c478bd9Sstevel@tonic-gate 	 * Make sure that the directory cache is valid.
28597c478bd9Sstevel@tonic-gate 	 */
28607c478bd9Sstevel@tonic-gate 	if (HAVE_RDDIR_CACHE(rp)) {
28617c478bd9Sstevel@tonic-gate 		if (nfs_disable_rddir_cache) {
28627c478bd9Sstevel@tonic-gate 			/*
28637c478bd9Sstevel@tonic-gate 			 * Setting nfs_disable_rddir_cache in /etc/system
28647c478bd9Sstevel@tonic-gate 			 * allows interoperability with servers that do not
28657c478bd9Sstevel@tonic-gate 			 * properly update the attributes of directories.
28667c478bd9Sstevel@tonic-gate 			 * Any cached information gets purged before an
28677c478bd9Sstevel@tonic-gate 			 * access is made to it.
28687c478bd9Sstevel@tonic-gate 			 */
28697c478bd9Sstevel@tonic-gate 			nfs_purge_rddir_cache(vp);
28707c478bd9Sstevel@tonic-gate 		} else {
28717c478bd9Sstevel@tonic-gate 			error = nfs_validate_caches(vp, cr);
28727c478bd9Sstevel@tonic-gate 			if (error)
28737c478bd9Sstevel@tonic-gate 				return (error);
28747c478bd9Sstevel@tonic-gate 		}
28757c478bd9Sstevel@tonic-gate 	}
28767c478bd9Sstevel@tonic-gate 
28777c478bd9Sstevel@tonic-gate 	/*
28787c478bd9Sstevel@tonic-gate 	 * UGLINESS: SunOS 3.2 servers apparently cannot always handle an
28797c478bd9Sstevel@tonic-gate 	 * RFS_READDIR request with rda_count set to more than 0x400. So
28807c478bd9Sstevel@tonic-gate 	 * we reduce the request size here purely for compatibility.
28817c478bd9Sstevel@tonic-gate 	 *
28827c478bd9Sstevel@tonic-gate 	 * In general, this is no longer required.  However, if a server
28837c478bd9Sstevel@tonic-gate 	 * is discovered which can not handle requests larger than 1024,
28847c478bd9Sstevel@tonic-gate 	 * nfs_shrinkreaddir can be set to 1 to enable this backwards
28857c478bd9Sstevel@tonic-gate 	 * compatibility.
28867c478bd9Sstevel@tonic-gate 	 *
28877c478bd9Sstevel@tonic-gate 	 * In any case, the request size is limited to NFS_MAXDATA bytes.
28887c478bd9Sstevel@tonic-gate 	 */
28897c478bd9Sstevel@tonic-gate 	count = MIN(uiop->uio_iov->iov_len,
28907c478bd9Sstevel@tonic-gate 	    nfs_shrinkreaddir ? 0x400 : NFS_MAXDATA);
28917c478bd9Sstevel@tonic-gate 
28927c478bd9Sstevel@tonic-gate 	nrdc = NULL;
28937c478bd9Sstevel@tonic-gate #ifdef DEBUG
28947c478bd9Sstevel@tonic-gate 	missed = 0;
28957c478bd9Sstevel@tonic-gate #endif
28967c478bd9Sstevel@tonic-gate top:
28977c478bd9Sstevel@tonic-gate 	/*
28987c478bd9Sstevel@tonic-gate 	 * Short circuit last readdir which always returns 0 bytes.
28997c478bd9Sstevel@tonic-gate 	 * This can be done after the directory has been read through
29007c478bd9Sstevel@tonic-gate 	 * completely at least once.  This will set r_direof which
29017c478bd9Sstevel@tonic-gate 	 * can be used to find the value of the last cookie.
29027c478bd9Sstevel@tonic-gate 	 */
29037c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
29047c478bd9Sstevel@tonic-gate 	if (rp->r_direof != NULL &&
29057c478bd9Sstevel@tonic-gate 	    uiop->uio_offset == rp->r_direof->nfs_ncookie) {
29067c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
29077c478bd9Sstevel@tonic-gate #ifdef DEBUG
29087c478bd9Sstevel@tonic-gate 		nfs_readdir_cache_shorts++;
29097c478bd9Sstevel@tonic-gate #endif
29107c478bd9Sstevel@tonic-gate 		if (eofp)
29117c478bd9Sstevel@tonic-gate 			*eofp = 1;
29127c478bd9Sstevel@tonic-gate 		if (nrdc != NULL)
29137c478bd9Sstevel@tonic-gate 			rddir_cache_rele(nrdc);
29147c478bd9Sstevel@tonic-gate 		return (0);
29157c478bd9Sstevel@tonic-gate 	}
29167c478bd9Sstevel@tonic-gate 	/*
29177c478bd9Sstevel@tonic-gate 	 * Look for a cache entry.  Cache entries are identified
29187c478bd9Sstevel@tonic-gate 	 * by the NFS cookie value and the byte count requested.
29197c478bd9Sstevel@tonic-gate 	 */
29207c478bd9Sstevel@tonic-gate 	srdc.nfs_cookie = uiop->uio_offset;
29217c478bd9Sstevel@tonic-gate 	srdc.buflen = count;
29227c478bd9Sstevel@tonic-gate 	rdc = avl_find(&rp->r_dir, &srdc, &where);
29237c478bd9Sstevel@tonic-gate 	if (rdc != NULL) {
29247c478bd9Sstevel@tonic-gate 		rddir_cache_hold(rdc);
29257c478bd9Sstevel@tonic-gate 		/*
29267c478bd9Sstevel@tonic-gate 		 * If the cache entry is in the process of being
29277c478bd9Sstevel@tonic-gate 		 * filled in, wait until this completes.  The
29287c478bd9Sstevel@tonic-gate 		 * RDDIRWAIT bit is set to indicate that someone
29297c478bd9Sstevel@tonic-gate 		 * is waiting and then the thread currently
29307c478bd9Sstevel@tonic-gate 		 * filling the entry is done, it should do a
29317c478bd9Sstevel@tonic-gate 		 * cv_broadcast to wakeup all of the threads
29327c478bd9Sstevel@tonic-gate 		 * waiting for it to finish.
29337c478bd9Sstevel@tonic-gate 		 */
29347c478bd9Sstevel@tonic-gate 		if (rdc->flags & RDDIR) {
29357c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&rp->r_rwlock);
29367c478bd9Sstevel@tonic-gate 			rdc->flags |= RDDIRWAIT;
29377c478bd9Sstevel@tonic-gate #ifdef DEBUG
29387c478bd9Sstevel@tonic-gate 			nfs_readdir_cache_waits++;
29397c478bd9Sstevel@tonic-gate #endif
29407c478bd9Sstevel@tonic-gate 			if (!cv_wait_sig(&rdc->cv, &rp->r_statelock)) {
29417c478bd9Sstevel@tonic-gate 				/*
29427c478bd9Sstevel@tonic-gate 				 * We got interrupted, probably
29437c478bd9Sstevel@tonic-gate 				 * the user typed ^C or an alarm
29447c478bd9Sstevel@tonic-gate 				 * fired.  We free the new entry
29457c478bd9Sstevel@tonic-gate 				 * if we allocated one.
29467c478bd9Sstevel@tonic-gate 				 */
29477c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
29487c478bd9Sstevel@tonic-gate 				(void) nfs_rw_enter_sig(&rp->r_rwlock,
29497c478bd9Sstevel@tonic-gate 					RW_READER, FALSE);
29507c478bd9Sstevel@tonic-gate 				rddir_cache_rele(rdc);
29517c478bd9Sstevel@tonic-gate 				if (nrdc != NULL)
29527c478bd9Sstevel@tonic-gate 					rddir_cache_rele(nrdc);
29537c478bd9Sstevel@tonic-gate 				return (EINTR);
29547c478bd9Sstevel@tonic-gate 			}
29557c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
29567c478bd9Sstevel@tonic-gate 			(void) nfs_rw_enter_sig(&rp->r_rwlock,
29577c478bd9Sstevel@tonic-gate 				RW_READER, FALSE);
29587c478bd9Sstevel@tonic-gate 			rddir_cache_rele(rdc);
29597c478bd9Sstevel@tonic-gate 			goto top;
29607c478bd9Sstevel@tonic-gate 		}
29617c478bd9Sstevel@tonic-gate 		/*
29627c478bd9Sstevel@tonic-gate 		 * Check to see if a readdir is required to
29637c478bd9Sstevel@tonic-gate 		 * fill the entry.  If so, mark this entry
29647c478bd9Sstevel@tonic-gate 		 * as being filled, remove our reference,
29657c478bd9Sstevel@tonic-gate 		 * and branch to the code to fill the entry.
29667c478bd9Sstevel@tonic-gate 		 */
29677c478bd9Sstevel@tonic-gate 		if (rdc->flags & RDDIRREQ) {
29687c478bd9Sstevel@tonic-gate 			rdc->flags &= ~RDDIRREQ;
29697c478bd9Sstevel@tonic-gate 			rdc->flags |= RDDIR;
29707c478bd9Sstevel@tonic-gate 			if (nrdc != NULL)
29717c478bd9Sstevel@tonic-gate 				rddir_cache_rele(nrdc);
29727c478bd9Sstevel@tonic-gate 			nrdc = rdc;
29737c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
29747c478bd9Sstevel@tonic-gate 			goto bottom;
29757c478bd9Sstevel@tonic-gate 		}
29767c478bd9Sstevel@tonic-gate #ifdef DEBUG
29777c478bd9Sstevel@tonic-gate 		if (!missed)
29787c478bd9Sstevel@tonic-gate 			nfs_readdir_cache_hits++;
29797c478bd9Sstevel@tonic-gate #endif
29807c478bd9Sstevel@tonic-gate 		/*
29817c478bd9Sstevel@tonic-gate 		 * If an error occurred while attempting
29827c478bd9Sstevel@tonic-gate 		 * to fill the cache entry, just return it.
29837c478bd9Sstevel@tonic-gate 		 */
29847c478bd9Sstevel@tonic-gate 		if (rdc->error) {
29857c478bd9Sstevel@tonic-gate 			error = rdc->error;
29867c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
29877c478bd9Sstevel@tonic-gate 			rddir_cache_rele(rdc);
29887c478bd9Sstevel@tonic-gate 			if (nrdc != NULL)
29897c478bd9Sstevel@tonic-gate 				rddir_cache_rele(nrdc);
29907c478bd9Sstevel@tonic-gate 			return (error);
29917c478bd9Sstevel@tonic-gate 		}
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 		/*
29947c478bd9Sstevel@tonic-gate 		 * The cache entry is complete and good,
29957c478bd9Sstevel@tonic-gate 		 * copyout the dirent structs to the calling
29967c478bd9Sstevel@tonic-gate 		 * thread.
29977c478bd9Sstevel@tonic-gate 		 */
29987c478bd9Sstevel@tonic-gate 		error = uiomove(rdc->entries, rdc->entlen, UIO_READ, uiop);
29997c478bd9Sstevel@tonic-gate 
30007c478bd9Sstevel@tonic-gate 		/*
30017c478bd9Sstevel@tonic-gate 		 * If no error occurred during the copyout,
30027c478bd9Sstevel@tonic-gate 		 * update the offset in the uio struct to
30037c478bd9Sstevel@tonic-gate 		 * contain the value of the next cookie
30047c478bd9Sstevel@tonic-gate 		 * and set the eof value appropriately.
30057c478bd9Sstevel@tonic-gate 		 */
30067c478bd9Sstevel@tonic-gate 		if (!error) {
30077c478bd9Sstevel@tonic-gate 			uiop->uio_offset = rdc->nfs_ncookie;
30087c478bd9Sstevel@tonic-gate 			if (eofp)
30097c478bd9Sstevel@tonic-gate 				*eofp = rdc->eof;
30107c478bd9Sstevel@tonic-gate 		}
30117c478bd9Sstevel@tonic-gate 
30127c478bd9Sstevel@tonic-gate 		/*
30137c478bd9Sstevel@tonic-gate 		 * Decide whether to do readahead.  Don't if
30147c478bd9Sstevel@tonic-gate 		 * have already read to the end of directory.
30157c478bd9Sstevel@tonic-gate 		 */
30167c478bd9Sstevel@tonic-gate 		if (rdc->eof) {
30177c478bd9Sstevel@tonic-gate 			rp->r_direof = rdc;
30187c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
30197c478bd9Sstevel@tonic-gate 			rddir_cache_rele(rdc);
30207c478bd9Sstevel@tonic-gate 			if (nrdc != NULL)
30217c478bd9Sstevel@tonic-gate 				rddir_cache_rele(nrdc);
30227c478bd9Sstevel@tonic-gate 			return (error);
30237c478bd9Sstevel@tonic-gate 		}
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate 		/*
30267c478bd9Sstevel@tonic-gate 		 * Check to see whether we found an entry
30277c478bd9Sstevel@tonic-gate 		 * for the readahead.  If so, we don't need
30287c478bd9Sstevel@tonic-gate 		 * to do anything further, so free the new
30297c478bd9Sstevel@tonic-gate 		 * entry if one was allocated.  Otherwise,
30307c478bd9Sstevel@tonic-gate 		 * allocate a new entry, add it to the cache,
30317c478bd9Sstevel@tonic-gate 		 * and then initiate an asynchronous readdir
30327c478bd9Sstevel@tonic-gate 		 * operation to fill it.
30337c478bd9Sstevel@tonic-gate 		 */
30347c478bd9Sstevel@tonic-gate 		srdc.nfs_cookie = rdc->nfs_ncookie;
30357c478bd9Sstevel@tonic-gate 		srdc.buflen = count;
30367c478bd9Sstevel@tonic-gate 		rrdc = avl_find(&rp->r_dir, &srdc, &where);
30377c478bd9Sstevel@tonic-gate 		if (rrdc != NULL) {
30387c478bd9Sstevel@tonic-gate 			if (nrdc != NULL)
30397c478bd9Sstevel@tonic-gate 				rddir_cache_rele(nrdc);
30407c478bd9Sstevel@tonic-gate 		} else {
30417c478bd9Sstevel@tonic-gate 			if (nrdc != NULL)
30427c478bd9Sstevel@tonic-gate 				rrdc = nrdc;
30437c478bd9Sstevel@tonic-gate 			else {
30447c478bd9Sstevel@tonic-gate 				rrdc = rddir_cache_alloc(KM_NOSLEEP);
30457c478bd9Sstevel@tonic-gate 			}
30467c478bd9Sstevel@tonic-gate 			if (rrdc != NULL) {
30477c478bd9Sstevel@tonic-gate 				rrdc->nfs_cookie = rdc->nfs_ncookie;
30487c478bd9Sstevel@tonic-gate 				rrdc->buflen = count;
30497c478bd9Sstevel@tonic-gate 				avl_insert(&rp->r_dir, rrdc, where);
30507c478bd9Sstevel@tonic-gate 				rddir_cache_hold(rrdc);
30517c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
30527c478bd9Sstevel@tonic-gate 				rddir_cache_rele(rdc);
30537c478bd9Sstevel@tonic-gate #ifdef DEBUG
30547c478bd9Sstevel@tonic-gate 				nfs_readdir_readahead++;
30557c478bd9Sstevel@tonic-gate #endif
30567c478bd9Sstevel@tonic-gate 				nfs_async_readdir(vp, rrdc, cr, nfsreaddir);
30577c478bd9Sstevel@tonic-gate 				return (error);
30587c478bd9Sstevel@tonic-gate 			}
30597c478bd9Sstevel@tonic-gate 		}
30607c478bd9Sstevel@tonic-gate 
30617c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
30627c478bd9Sstevel@tonic-gate 		rddir_cache_rele(rdc);
30637c478bd9Sstevel@tonic-gate 		return (error);
30647c478bd9Sstevel@tonic-gate 	}
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate 	/*
30677c478bd9Sstevel@tonic-gate 	 * Didn't find an entry in the cache.  Construct a new empty
30687c478bd9Sstevel@tonic-gate 	 * entry and link it into the cache.  Other processes attempting
30697c478bd9Sstevel@tonic-gate 	 * to access this entry will need to wait until it is filled in.
30707c478bd9Sstevel@tonic-gate 	 *
30717c478bd9Sstevel@tonic-gate 	 * Since kmem_alloc may block, another pass through the cache
30727c478bd9Sstevel@tonic-gate 	 * will need to be taken to make sure that another process
30737c478bd9Sstevel@tonic-gate 	 * hasn't already added an entry to the cache for this request.
30747c478bd9Sstevel@tonic-gate 	 */
30757c478bd9Sstevel@tonic-gate 	if (nrdc == NULL) {
30767c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
30777c478bd9Sstevel@tonic-gate 		nrdc = rddir_cache_alloc(KM_SLEEP);
30787c478bd9Sstevel@tonic-gate 		nrdc->nfs_cookie = uiop->uio_offset;
30797c478bd9Sstevel@tonic-gate 		nrdc->buflen = count;
30807c478bd9Sstevel@tonic-gate 		goto top;
30817c478bd9Sstevel@tonic-gate 	}
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate 	/*
30847c478bd9Sstevel@tonic-gate 	 * Add this entry to the cache.
30857c478bd9Sstevel@tonic-gate 	 */
30867c478bd9Sstevel@tonic-gate 	avl_insert(&rp->r_dir, nrdc, where);
30877c478bd9Sstevel@tonic-gate 	rddir_cache_hold(nrdc);
30887c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate bottom:
30917c478bd9Sstevel@tonic-gate #ifdef DEBUG
30927c478bd9Sstevel@tonic-gate 	missed = 1;
30937c478bd9Sstevel@tonic-gate 	nfs_readdir_cache_misses++;
30947c478bd9Sstevel@tonic-gate #endif
30957c478bd9Sstevel@tonic-gate 	/*
30967c478bd9Sstevel@tonic-gate 	 * Do the readdir.
30977c478bd9Sstevel@tonic-gate 	 */
30987c478bd9Sstevel@tonic-gate 	error = nfsreaddir(vp, nrdc, cr);
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	/*
31017c478bd9Sstevel@tonic-gate 	 * If this operation failed, just return the error which occurred.
31027c478bd9Sstevel@tonic-gate 	 */
31037c478bd9Sstevel@tonic-gate 	if (error != 0)
31047c478bd9Sstevel@tonic-gate 		return (error);
31057c478bd9Sstevel@tonic-gate 
31067c478bd9Sstevel@tonic-gate 	/*
31077c478bd9Sstevel@tonic-gate 	 * Since the RPC operation will have taken sometime and blocked
31087c478bd9Sstevel@tonic-gate 	 * this process, another pass through the cache will need to be
31097c478bd9Sstevel@tonic-gate 	 * taken to find the correct cache entry.  It is possible that
31107c478bd9Sstevel@tonic-gate 	 * the correct cache entry will not be there (although one was
31117c478bd9Sstevel@tonic-gate 	 * added) because the directory changed during the RPC operation
31127c478bd9Sstevel@tonic-gate 	 * and the readdir cache was flushed.  In this case, just start
31137c478bd9Sstevel@tonic-gate 	 * over.  It is hoped that this will not happen too often... :-)
31147c478bd9Sstevel@tonic-gate 	 */
31157c478bd9Sstevel@tonic-gate 	nrdc = NULL;
31167c478bd9Sstevel@tonic-gate 	goto top;
31177c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
31187c478bd9Sstevel@tonic-gate }
31197c478bd9Sstevel@tonic-gate 
31207c478bd9Sstevel@tonic-gate static int
31217c478bd9Sstevel@tonic-gate nfsreaddir(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
31227c478bd9Sstevel@tonic-gate {
31237c478bd9Sstevel@tonic-gate 	int error;
31247c478bd9Sstevel@tonic-gate 	struct nfsrddirargs rda;
31257c478bd9Sstevel@tonic-gate 	struct nfsrddirres rd;
31267c478bd9Sstevel@tonic-gate 	rnode_t *rp;
31277c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
31287c478bd9Sstevel@tonic-gate 	uint_t count;
31297c478bd9Sstevel@tonic-gate 	int douprintf;
31307c478bd9Sstevel@tonic-gate 	failinfo_t fi, *fip;
31317c478bd9Sstevel@tonic-gate 
3132108322fbScarlsonj 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
31337c478bd9Sstevel@tonic-gate 	count = rdc->buflen;
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
31367c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
31377c478bd9Sstevel@tonic-gate 
31387c478bd9Sstevel@tonic-gate 	rda.rda_fh = *VTOFH(vp);
31397c478bd9Sstevel@tonic-gate 	rda.rda_offset = rdc->nfs_cookie;
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate 	/*
31427c478bd9Sstevel@tonic-gate 	 * NFS client failover support
31437c478bd9Sstevel@tonic-gate 	 * suppress failover unless we have a zero cookie
31447c478bd9Sstevel@tonic-gate 	 */
31457c478bd9Sstevel@tonic-gate 	if (rdc->nfs_cookie == (off_t)0) {
31467c478bd9Sstevel@tonic-gate 		fi.vp = vp;
31477c478bd9Sstevel@tonic-gate 		fi.fhp = (caddr_t)&rda.rda_fh;
31487c478bd9Sstevel@tonic-gate 		fi.copyproc = nfscopyfh;
31497c478bd9Sstevel@tonic-gate 		fi.lookupproc = nfslookup;
31507c478bd9Sstevel@tonic-gate 		fi.xattrdirproc = acl_getxattrdir2;
31517c478bd9Sstevel@tonic-gate 		fip = &fi;
31527c478bd9Sstevel@tonic-gate 	} else {
31537c478bd9Sstevel@tonic-gate 		fip = NULL;
31547c478bd9Sstevel@tonic-gate 	}
31557c478bd9Sstevel@tonic-gate 
31567c478bd9Sstevel@tonic-gate 	rd.rd_entries = kmem_alloc(rdc->buflen, KM_SLEEP);
31577c478bd9Sstevel@tonic-gate 	rd.rd_size = count;
31587c478bd9Sstevel@tonic-gate 	rd.rd_offset = rda.rda_offset;
31597c478bd9Sstevel@tonic-gate 
31607c478bd9Sstevel@tonic-gate 	douprintf = 1;
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
31637c478bd9Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
31647c478bd9Sstevel@tonic-gate 		kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
31657c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
31667c478bd9Sstevel@tonic-gate 	}
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 	do {
31697c478bd9Sstevel@tonic-gate 		rda.rda_count = MIN(count, mi->mi_curread);
31707c478bd9Sstevel@tonic-gate 		error = rfs2call(mi, RFS_READDIR,
31717c478bd9Sstevel@tonic-gate 		    xdr_rddirargs, (caddr_t)&rda,
31727c478bd9Sstevel@tonic-gate 		    xdr_getrddirres, (caddr_t)&rd, cr,
31737c478bd9Sstevel@tonic-gate 		    &douprintf, &rd.rd_status, 0, fip);
31747c478bd9Sstevel@tonic-gate 	} while (error == ENFS_TRYAGAIN);
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate 	if (mi->mi_io_kstats) {
31777c478bd9Sstevel@tonic-gate 		mutex_enter(&mi->mi_lock);
31787c478bd9Sstevel@tonic-gate 		kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
31797c478bd9Sstevel@tonic-gate 		mutex_exit(&mi->mi_lock);
31807c478bd9Sstevel@tonic-gate 	}
31817c478bd9Sstevel@tonic-gate 
31827c478bd9Sstevel@tonic-gate 	/*
31837c478bd9Sstevel@tonic-gate 	 * Since we are actually doing a READDIR RPC, we must have
31847c478bd9Sstevel@tonic-gate 	 * exclusive access to the cache entry being filled.  Thus,
31857c478bd9Sstevel@tonic-gate 	 * it is safe to update all fields except for the flags
31867c478bd9Sstevel@tonic-gate 	 * field.  The r_statelock in the rnode must be held to
31877c478bd9Sstevel@tonic-gate 	 * prevent two different threads from simultaneously
31887c478bd9Sstevel@tonic-gate 	 * attempting to update the flags field.  This can happen
31897c478bd9Sstevel@tonic-gate 	 * if we are turning off RDDIR and the other thread is
31907c478bd9Sstevel@tonic-gate 	 * trying to set RDDIRWAIT.
31917c478bd9Sstevel@tonic-gate 	 */
31927c478bd9Sstevel@tonic-gate 	ASSERT(rdc->flags & RDDIR);
31937c478bd9Sstevel@tonic-gate 	if (!error) {
31947c478bd9Sstevel@tonic-gate 		error = geterrno(rd.rd_status);
31957c478bd9Sstevel@tonic-gate 		if (!error) {
31967c478bd9Sstevel@tonic-gate 			rdc->nfs_ncookie = rd.rd_offset;
31977c478bd9Sstevel@tonic-gate 			rdc->eof = rd.rd_eof ? 1 : 0;
31987c478bd9Sstevel@tonic-gate 			rdc->entlen = rd.rd_size;
31997c478bd9Sstevel@tonic-gate 			ASSERT(rdc->entlen <= rdc->buflen);
32007c478bd9Sstevel@tonic-gate #ifdef DEBUG
32017c478bd9Sstevel@tonic-gate 			rdc->entries = rddir_cache_buf_alloc(rdc->buflen,
32027c478bd9Sstevel@tonic-gate 			    KM_SLEEP);
32037c478bd9Sstevel@tonic-gate #else
32047c478bd9Sstevel@tonic-gate 			rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP);
32057c478bd9Sstevel@tonic-gate #endif
32067c478bd9Sstevel@tonic-gate 			bcopy(rd.rd_entries, rdc->entries, rdc->entlen);
32077c478bd9Sstevel@tonic-gate 			rdc->error = 0;
32087c478bd9Sstevel@tonic-gate 			if (mi->mi_io_kstats) {
32097c478bd9Sstevel@tonic-gate 				mutex_enter(&mi->mi_lock);
32107c478bd9Sstevel@tonic-gate 				KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
32117c478bd9Sstevel@tonic-gate 				KSTAT_IO_PTR(mi->mi_io_kstats)->nread +=
32127c478bd9Sstevel@tonic-gate 				    rd.rd_size;
32137c478bd9Sstevel@tonic-gate 				mutex_exit(&mi->mi_lock);
32147c478bd9Sstevel@tonic-gate 			}
32157c478bd9Sstevel@tonic-gate 		} else {
32167c478bd9Sstevel@tonic-gate 			PURGE_STALE_FH(error, vp, cr);
32177c478bd9Sstevel@tonic-gate 		}
32187c478bd9Sstevel@tonic-gate 	}
32197c478bd9Sstevel@tonic-gate 	if (error) {
32207c478bd9Sstevel@tonic-gate 		rdc->entries = NULL;
32217c478bd9Sstevel@tonic-gate 		rdc->error = error;
32227c478bd9Sstevel@tonic-gate 	}
32237c478bd9Sstevel@tonic-gate 	kmem_free(rd.rd_entries, rdc->buflen);
32247c478bd9Sstevel@tonic-gate 
32257c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
32267c478bd9Sstevel@tonic-gate 	rdc->flags &= ~RDDIR;
32277c478bd9Sstevel@tonic-gate 	if (rdc->flags & RDDIRWAIT) {
32287c478bd9Sstevel@tonic-gate 		rdc->flags &= ~RDDIRWAIT;
32297c478bd9Sstevel@tonic-gate 		cv_broadcast(&rdc->cv);
32307c478bd9Sstevel@tonic-gate 	}
32317c478bd9Sstevel@tonic-gate 	if (error)
32327c478bd9Sstevel@tonic-gate 		rdc->flags |= RDDIRREQ;
32337c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 	rddir_cache_rele(rdc);
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate 	return (error);
32387c478bd9Sstevel@tonic-gate }
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate #ifdef DEBUG
32417c478bd9Sstevel@tonic-gate static int nfs_bio_do_stop = 0;
32427c478bd9Sstevel@tonic-gate #endif
32437c478bd9Sstevel@tonic-gate 
32447c478bd9Sstevel@tonic-gate static int
32457c478bd9Sstevel@tonic-gate nfs_bio(struct buf *bp, cred_t *cr)
32467c478bd9Sstevel@tonic-gate {
32477c478bd9Sstevel@tonic-gate 	rnode_t *rp = VTOR(bp->b_vp);
32487c478bd9Sstevel@tonic-gate 	int count;
32497c478bd9Sstevel@tonic-gate 	int error;
32507c478bd9Sstevel@tonic-gate 	cred_t *cred;
32517c478bd9Sstevel@tonic-gate 	uint_t offset;
32527c478bd9Sstevel@tonic-gate 
32537c478bd9Sstevel@tonic-gate 	DTRACE_IO1(start, struct buf *, bp);
32547c478bd9Sstevel@tonic-gate 
3255108322fbScarlsonj 	ASSERT(nfs_zone() == VTOMI(bp->b_vp)->mi_zone);
32567c478bd9Sstevel@tonic-gate 	offset = dbtob(bp->b_blkno);
32577c478bd9Sstevel@tonic-gate 
32587c478bd9Sstevel@tonic-gate 	if (bp->b_flags & B_READ) {
32597c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
32607c478bd9Sstevel@tonic-gate 		if (rp->r_cred != NULL) {
32617c478bd9Sstevel@tonic-gate 			cred = rp->r_cred;
32627c478bd9Sstevel@tonic-gate 			crhold(cred);
32637c478bd9Sstevel@tonic-gate 		} else {
32647c478bd9Sstevel@tonic-gate 			rp->r_cred = cr;
32657c478bd9Sstevel@tonic-gate 			crhold(cr);
32667c478bd9Sstevel@tonic-gate 			cred = cr;
32677c478bd9Sstevel@tonic-gate 			crhold(cred);
32687c478bd9Sstevel@tonic-gate 		}
32697c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
32707c478bd9Sstevel@tonic-gate 	read_again:
32717c478bd9Sstevel@tonic-gate 		error = bp->b_error = nfsread(bp->b_vp, bp->b_un.b_addr,
32727c478bd9Sstevel@tonic-gate 		    offset, bp->b_bcount, &bp->b_resid, cred);
32737c478bd9Sstevel@tonic-gate 		crfree(cred);
32747c478bd9Sstevel@tonic-gate 		if (!error) {
32757c478bd9Sstevel@tonic-gate 			if (bp->b_resid) {
32767c478bd9Sstevel@tonic-gate 				/*
32777c478bd9Sstevel@tonic-gate 				 * Didn't get it all because we hit EOF,
32787c478bd9Sstevel@tonic-gate 				 * zero all the memory beyond the EOF.
32797c478bd9Sstevel@tonic-gate 				 */
32807c478bd9Sstevel@tonic-gate 				/* bzero(rdaddr + */
32817c478bd9Sstevel@tonic-gate 				bzero(bp->b_un.b_addr +
32827c478bd9Sstevel@tonic-gate 				    bp->b_bcount - bp->b_resid, bp->b_resid);
32837c478bd9Sstevel@tonic-gate 			}
32847c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
32857c478bd9Sstevel@tonic-gate 			if (bp->b_resid == bp->b_bcount &&
32867c478bd9Sstevel@tonic-gate 			    offset >= rp->r_size) {
32877c478bd9Sstevel@tonic-gate 				/*
32887c478bd9Sstevel@tonic-gate 				 * We didn't read anything at all as we are
32897c478bd9Sstevel@tonic-gate 				 * past EOF.  Return an error indicator back
32907c478bd9Sstevel@tonic-gate 				 * but don't destroy the pages (yet).
32917c478bd9Sstevel@tonic-gate 				 */
32927c478bd9Sstevel@tonic-gate 				error = NFS_EOF;
32937c478bd9Sstevel@tonic-gate 			}
32947c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
32957c478bd9Sstevel@tonic-gate 		} else if (error == EACCES) {
32967c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
32977c478bd9Sstevel@tonic-gate 			if (cred != cr) {
32987c478bd9Sstevel@tonic-gate 				if (rp->r_cred != NULL)
32997c478bd9Sstevel@tonic-gate 					crfree(rp->r_cred);
33007c478bd9Sstevel@tonic-gate 				rp->r_cred = cr;
33017c478bd9Sstevel@tonic-gate 				crhold(cr);
33027c478bd9Sstevel@tonic-gate 				cred = cr;
33037c478bd9Sstevel@tonic-gate 				crhold(cred);
33047c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
33057c478bd9Sstevel@tonic-gate 				goto read_again;
33067c478bd9Sstevel@tonic-gate 			}
33077c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
33087c478bd9Sstevel@tonic-gate 		}
33097c478bd9Sstevel@tonic-gate 	} else {
33107c478bd9Sstevel@tonic-gate 		if (!(rp->r_flags & RSTALE)) {
33117c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
33127c478bd9Sstevel@tonic-gate 			if (rp->r_cred != NULL) {
33137c478bd9Sstevel@tonic-gate 				cred = rp->r_cred;
33147c478bd9Sstevel@tonic-gate 				crhold(cred);
33157c478bd9Sstevel@tonic-gate 			} else {
33167c478bd9Sstevel@tonic-gate 				rp->r_cred = cr;
33177c478bd9Sstevel@tonic-gate 				crhold(cr);
33187c478bd9Sstevel@tonic-gate 				cred = cr;
33197c478bd9Sstevel@tonic-gate 				crhold(cred);
33207c478bd9Sstevel@tonic-gate 			}
33217c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
33227c478bd9Sstevel@tonic-gate 		write_again:
33237c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
33247c478bd9Sstevel@tonic-gate 			count = MIN(bp->b_bcount, rp->r_size - offset);
33257c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
33267c478bd9Sstevel@tonic-gate 			if (count < 0)
33277c478bd9Sstevel@tonic-gate 				cmn_err(CE_PANIC, "nfs_bio: write count < 0");
33287c478bd9Sstevel@tonic-gate #ifdef DEBUG
33297c478bd9Sstevel@tonic-gate 			if (count == 0) {
33307c478bd9Sstevel@tonic-gate 				zcmn_err(getzoneid(), CE_WARN,
33317c478bd9Sstevel@tonic-gate 				    "nfs_bio: zero length write at %d",
33327c478bd9Sstevel@tonic-gate 				    offset);
33337c478bd9Sstevel@tonic-gate 				nfs_printfhandle(&rp->r_fh);
33347c478bd9Sstevel@tonic-gate 				if (nfs_bio_do_stop)
33357c478bd9Sstevel@tonic-gate 					debug_enter("nfs_bio");
33367c478bd9Sstevel@tonic-gate 			}
33377c478bd9Sstevel@tonic-gate #endif
33387c478bd9Sstevel@tonic-gate 			error = nfswrite(bp->b_vp, bp->b_un.b_addr, offset,
33397c478bd9Sstevel@tonic-gate 			    count, cred);
33407c478bd9Sstevel@tonic-gate 			if (error == EACCES) {
33417c478bd9Sstevel@tonic-gate 				mutex_enter(&rp->r_statelock);
33427c478bd9Sstevel@tonic-gate 				if (cred != cr) {
33437c478bd9Sstevel@tonic-gate 					if (rp->r_cred != NULL)
33447c478bd9Sstevel@tonic-gate 						crfree(rp->r_cred);
33457c478bd9Sstevel@tonic-gate 					rp->r_cred = cr;
33467c478bd9Sstevel@tonic-gate 					crhold(cr);
33477c478bd9Sstevel@tonic-gate 					crfree(cred);
33487c478bd9Sstevel@tonic-gate 					cred = cr;
33497c478bd9Sstevel@tonic-gate 					crhold(cred);
33507c478bd9Sstevel@tonic-gate 					mutex_exit(&rp->r_statelock);
33517c478bd9Sstevel@tonic-gate 					goto write_again;
33527c478bd9Sstevel@tonic-gate 				}
33537c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
33547c478bd9Sstevel@tonic-gate 			}
33557c478bd9Sstevel@tonic-gate 			bp->b_error = error;
33567c478bd9Sstevel@tonic-gate 			if (error && error != EINTR) {
33577c478bd9Sstevel@tonic-gate 				/*
33587c478bd9Sstevel@tonic-gate 				 * Don't print EDQUOT errors on the console.
33597c478bd9Sstevel@tonic-gate 				 * Don't print asynchronous EACCES errors.
33607c478bd9Sstevel@tonic-gate 				 * Don't print EFBIG errors.
33617c478bd9Sstevel@tonic-gate 				 * Print all other write errors.
33627c478bd9Sstevel@tonic-gate 				 */
33637c478bd9Sstevel@tonic-gate 				if (error != EDQUOT && error != EFBIG &&
33647c478bd9Sstevel@tonic-gate 				    (error != EACCES ||
33657c478bd9Sstevel@tonic-gate 				    !(bp->b_flags & B_ASYNC)))
33667c478bd9Sstevel@tonic-gate 					nfs_write_error(bp->b_vp, error, cred);
33677c478bd9Sstevel@tonic-gate 				/*
33687c478bd9Sstevel@tonic-gate 				 * Update r_error and r_flags as appropriate.
33697c478bd9Sstevel@tonic-gate 				 * If the error was ESTALE, then mark the
33707c478bd9Sstevel@tonic-gate 				 * rnode as not being writeable and save
33717c478bd9Sstevel@tonic-gate 				 * the error status.  Otherwise, save any
33727c478bd9Sstevel@tonic-gate 				 * errors which occur from asynchronous
33737c478bd9Sstevel@tonic-gate 				 * page invalidations.  Any errors occurring
33747c478bd9Sstevel@tonic-gate 				 * from other operations should be saved
33757c478bd9Sstevel@tonic-gate 				 * by the caller.
33767c478bd9Sstevel@tonic-gate 				 */
33777c478bd9Sstevel@tonic-gate 				mutex_enter(&rp->r_statelock);
33787c478bd9Sstevel@tonic-gate 				if (error == ESTALE) {
33797c478bd9Sstevel@tonic-gate 					rp->r_flags |= RSTALE;
33807c478bd9Sstevel@tonic-gate 					if (!rp->r_error)
33817c478bd9Sstevel@tonic-gate 						rp->r_error = error;
33827c478bd9Sstevel@tonic-gate 				} else if (!rp->r_error &&
33837c478bd9Sstevel@tonic-gate 				    (bp->b_flags &
33847c478bd9Sstevel@tonic-gate 				    (B_INVAL|B_FORCE|B_ASYNC)) ==
33857c478bd9Sstevel@tonic-gate 				    (B_INVAL|B_FORCE|B_ASYNC)) {
33867c478bd9Sstevel@tonic-gate 					rp->r_error = error;
33877c478bd9Sstevel@tonic-gate 				}
33887c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
33897c478bd9Sstevel@tonic-gate 			}
33907c478bd9Sstevel@tonic-gate 			crfree(cred);
33917c478bd9Sstevel@tonic-gate 		} else
33927c478bd9Sstevel@tonic-gate 			error = rp->r_error;
33937c478bd9Sstevel@tonic-gate 	}
33947c478bd9Sstevel@tonic-gate 
33957c478bd9Sstevel@tonic-gate 	if (error != 0 && error != NFS_EOF)
33967c478bd9Sstevel@tonic-gate 		bp->b_flags |= B_ERROR;
33977c478bd9Sstevel@tonic-gate 
33987c478bd9Sstevel@tonic-gate 	DTRACE_IO1(done, struct buf *, bp);
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate 	return (error);
34017c478bd9Sstevel@tonic-gate }
34027c478bd9Sstevel@tonic-gate 
34037c478bd9Sstevel@tonic-gate static int
34047c478bd9Sstevel@tonic-gate nfs_fid(vnode_t *vp, fid_t *fidp)
34057c478bd9Sstevel@tonic-gate {
34067c478bd9Sstevel@tonic-gate 	struct nfs_fid *fp;
34077c478bd9Sstevel@tonic-gate 	rnode_t *rp;
34087c478bd9Sstevel@tonic-gate 
34097c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
34107c478bd9Sstevel@tonic-gate 
34117c478bd9Sstevel@tonic-gate 	if (fidp->fid_len < (sizeof (struct nfs_fid) - sizeof (short))) {
34127c478bd9Sstevel@tonic-gate 		fidp->fid_len = sizeof (struct nfs_fid) - sizeof (short);
34137c478bd9Sstevel@tonic-gate 		return (ENOSPC);
34147c478bd9Sstevel@tonic-gate 	}
34157c478bd9Sstevel@tonic-gate 	fp = (struct nfs_fid *)fidp;
34167c478bd9Sstevel@tonic-gate 	fp->nf_pad = 0;
34177c478bd9Sstevel@tonic-gate 	fp->nf_len = sizeof (struct nfs_fid) - sizeof (short);
34187c478bd9Sstevel@tonic-gate 	bcopy(rp->r_fh.fh_buf, fp->nf_data, NFS_FHSIZE);
34197c478bd9Sstevel@tonic-gate 	return (0);
34207c478bd9Sstevel@tonic-gate }
34217c478bd9Sstevel@tonic-gate 
34227c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
34237c478bd9Sstevel@tonic-gate static int
34247c478bd9Sstevel@tonic-gate nfs_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
34257c478bd9Sstevel@tonic-gate {
34267c478bd9Sstevel@tonic-gate 	rnode_t *rp = VTOR(vp);
34277c478bd9Sstevel@tonic-gate 
34287c478bd9Sstevel@tonic-gate 	if (!write_lock) {
34297c478bd9Sstevel@tonic-gate 		(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
34307c478bd9Sstevel@tonic-gate 		return (V_WRITELOCK_FALSE);
34317c478bd9Sstevel@tonic-gate 	}
34327c478bd9Sstevel@tonic-gate 
34337c478bd9Sstevel@tonic-gate 	if ((rp->r_flags & RDIRECTIO) || (VTOMI(vp)->mi_flags & MI_DIRECTIO)) {
34347c478bd9Sstevel@tonic-gate 		(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
34357c478bd9Sstevel@tonic-gate 		if (rp->r_mapcnt == 0 && !vn_has_cached_data(vp))
34367c478bd9Sstevel@tonic-gate 			return (V_WRITELOCK_FALSE);
34377c478bd9Sstevel@tonic-gate 		nfs_rw_exit(&rp->r_rwlock);
34387c478bd9Sstevel@tonic-gate 	}
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 	(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
34417c478bd9Sstevel@tonic-gate 	return (V_WRITELOCK_TRUE);
34427c478bd9Sstevel@tonic-gate }
34437c478bd9Sstevel@tonic-gate 
34447c478bd9Sstevel@tonic-gate /* ARGSUSED */
34457c478bd9Sstevel@tonic-gate static void
34467c478bd9Sstevel@tonic-gate nfs_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
34477c478bd9Sstevel@tonic-gate {
34487c478bd9Sstevel@tonic-gate 	rnode_t *rp = VTOR(vp);
34497c478bd9Sstevel@tonic-gate 
34507c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&rp->r_rwlock);
34517c478bd9Sstevel@tonic-gate }
34527c478bd9Sstevel@tonic-gate 
34537c478bd9Sstevel@tonic-gate /* ARGSUSED */
34547c478bd9Sstevel@tonic-gate static int
34557c478bd9Sstevel@tonic-gate nfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp)
34567c478bd9Sstevel@tonic-gate {
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 	/*
34597c478bd9Sstevel@tonic-gate 	 * Because we stuff the readdir cookie into the offset field
34607c478bd9Sstevel@tonic-gate 	 * someone may attempt to do an lseek with the cookie which
34617c478bd9Sstevel@tonic-gate 	 * we want to succeed.
34627c478bd9Sstevel@tonic-gate 	 */
34637c478bd9Sstevel@tonic-gate 	if (vp->v_type == VDIR)
34647c478bd9Sstevel@tonic-gate 		return (0);
34657c478bd9Sstevel@tonic-gate 	if (*noffp < 0 || *noffp > MAXOFF32_T)
34667c478bd9Sstevel@tonic-gate 		return (EINVAL);
34677c478bd9Sstevel@tonic-gate 	return (0);
34687c478bd9Sstevel@tonic-gate }
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate /*
34717c478bd9Sstevel@tonic-gate  * number of NFS_MAXDATA blocks to read ahead
34727c478bd9Sstevel@tonic-gate  * optimized for 100 base-T.
34737c478bd9Sstevel@tonic-gate  */
34747c478bd9Sstevel@tonic-gate static int nfs_nra = 4;
34757c478bd9Sstevel@tonic-gate 
34767c478bd9Sstevel@tonic-gate #ifdef DEBUG
34777c478bd9Sstevel@tonic-gate static int nfs_lostpage = 0;	/* number of times we lost original page */
34787c478bd9Sstevel@tonic-gate #endif
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate /*
34817c478bd9Sstevel@tonic-gate  * Return all the pages from [off..off+len) in file
34827c478bd9Sstevel@tonic-gate  */
34837c478bd9Sstevel@tonic-gate static int
34847c478bd9Sstevel@tonic-gate nfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
34857c478bd9Sstevel@tonic-gate 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
34867c478bd9Sstevel@tonic-gate 	enum seg_rw rw, cred_t *cr)
34877c478bd9Sstevel@tonic-gate {
34887c478bd9Sstevel@tonic-gate 	rnode_t *rp;
34897c478bd9Sstevel@tonic-gate 	int error;
34907c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
34937c478bd9Sstevel@tonic-gate 		return (ENOSYS);
34947c478bd9Sstevel@tonic-gate 
34957c478bd9Sstevel@tonic-gate 	ASSERT(off <= MAXOFF32_T);
3496108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
34977c478bd9Sstevel@tonic-gate 		return (EIO);
34987c478bd9Sstevel@tonic-gate 	if (protp != NULL)
34997c478bd9Sstevel@tonic-gate 		*protp = PROT_ALL;
35007c478bd9Sstevel@tonic-gate 
35017c478bd9Sstevel@tonic-gate 	/*
35027c478bd9Sstevel@tonic-gate 	 * Now valididate that the caches are up to date.
35037c478bd9Sstevel@tonic-gate 	 */
35047c478bd9Sstevel@tonic-gate 	error = nfs_validate_caches(vp, cr);
35057c478bd9Sstevel@tonic-gate 	if (error)
35067c478bd9Sstevel@tonic-gate 		return (error);
35077c478bd9Sstevel@tonic-gate 
35087c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
35097c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
35107c478bd9Sstevel@tonic-gate retry:
35117c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
35127c478bd9Sstevel@tonic-gate 
35137c478bd9Sstevel@tonic-gate 	/*
35147c478bd9Sstevel@tonic-gate 	 * Don't create dirty pages faster than they
35157c478bd9Sstevel@tonic-gate 	 * can be cleaned so that the system doesn't
35167c478bd9Sstevel@tonic-gate 	 * get imbalanced.  If the async queue is
35177c478bd9Sstevel@tonic-gate 	 * maxed out, then wait for it to drain before
35187c478bd9Sstevel@tonic-gate 	 * creating more dirty pages.  Also, wait for
35197c478bd9Sstevel@tonic-gate 	 * any threads doing pagewalks in the vop_getattr
35207c478bd9Sstevel@tonic-gate 	 * entry points so that they don't block for
35217c478bd9Sstevel@tonic-gate 	 * long periods.
35227c478bd9Sstevel@tonic-gate 	 */
35237c478bd9Sstevel@tonic-gate 	if (rw == S_CREATE) {
35247c478bd9Sstevel@tonic-gate 		while ((mi->mi_max_threads != 0 &&
35257c478bd9Sstevel@tonic-gate 		    rp->r_awcount > 2 * mi->mi_max_threads) ||
35267c478bd9Sstevel@tonic-gate 		    rp->r_gcount > 0)
35277c478bd9Sstevel@tonic-gate 			cv_wait(&rp->r_cv, &rp->r_statelock);
35287c478bd9Sstevel@tonic-gate 	}
35297c478bd9Sstevel@tonic-gate 
35307c478bd9Sstevel@tonic-gate 	/*
35317c478bd9Sstevel@tonic-gate 	 * If we are getting called as a side effect of an nfs_write()
35327c478bd9Sstevel@tonic-gate 	 * operation the local file size might not be extended yet.
35337c478bd9Sstevel@tonic-gate 	 * In this case we want to be able to return pages of zeroes.
35347c478bd9Sstevel@tonic-gate 	 */
35357c478bd9Sstevel@tonic-gate 	if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
35367c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
35377c478bd9Sstevel@tonic-gate 		return (EFAULT);		/* beyond EOF */
35387c478bd9Sstevel@tonic-gate 	}
35397c478bd9Sstevel@tonic-gate 
35407c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
35417c478bd9Sstevel@tonic-gate 
35427c478bd9Sstevel@tonic-gate 	if (len <= PAGESIZE) {
35437c478bd9Sstevel@tonic-gate 		error = nfs_getapage(vp, off, len, protp, pl, plsz,
35447c478bd9Sstevel@tonic-gate 		    seg, addr, rw, cr);
35457c478bd9Sstevel@tonic-gate 	} else {
35467c478bd9Sstevel@tonic-gate 		error = pvn_getpages(nfs_getapage, vp, off, len, protp,
35477c478bd9Sstevel@tonic-gate 		    pl, plsz, seg, addr, rw, cr);
35487c478bd9Sstevel@tonic-gate 	}
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 	switch (error) {
35517c478bd9Sstevel@tonic-gate 	case NFS_EOF:
35527c478bd9Sstevel@tonic-gate 		nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
35537c478bd9Sstevel@tonic-gate 		goto retry;
35547c478bd9Sstevel@tonic-gate 	case ESTALE:
35557c478bd9Sstevel@tonic-gate 		PURGE_STALE_FH(error, vp, cr);
35567c478bd9Sstevel@tonic-gate 	}
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 	return (error);
35597c478bd9Sstevel@tonic-gate }
35607c478bd9Sstevel@tonic-gate 
35617c478bd9Sstevel@tonic-gate /*
35627c478bd9Sstevel@tonic-gate  * Called from pvn_getpages or nfs_getpage to get a particular page.
35637c478bd9Sstevel@tonic-gate  */
35647c478bd9Sstevel@tonic-gate /* ARGSUSED */
35657c478bd9Sstevel@tonic-gate static int
35667c478bd9Sstevel@tonic-gate nfs_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
35677c478bd9Sstevel@tonic-gate 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
35687c478bd9Sstevel@tonic-gate 	enum seg_rw rw, cred_t *cr)
35697c478bd9Sstevel@tonic-gate {
35707c478bd9Sstevel@tonic-gate 	rnode_t *rp;
35717c478bd9Sstevel@tonic-gate 	uint_t bsize;
35727c478bd9Sstevel@tonic-gate 	struct buf *bp;
35737c478bd9Sstevel@tonic-gate 	page_t *pp;
35747c478bd9Sstevel@tonic-gate 	u_offset_t lbn;
35757c478bd9Sstevel@tonic-gate 	u_offset_t io_off;
35767c478bd9Sstevel@tonic-gate 	u_offset_t blkoff;
35777c478bd9Sstevel@tonic-gate 	u_offset_t rablkoff;
35787c478bd9Sstevel@tonic-gate 	size_t io_len;
35797c478bd9Sstevel@tonic-gate 	uint_t blksize;
35807c478bd9Sstevel@tonic-gate 	int error;
35817c478bd9Sstevel@tonic-gate 	int readahead;
35827c478bd9Sstevel@tonic-gate 	int readahead_issued = 0;
35837c478bd9Sstevel@tonic-gate 	int ra_window; /* readahead window */
35847c478bd9Sstevel@tonic-gate 	page_t *pagefound;
35857c478bd9Sstevel@tonic-gate 
3586108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
35877c478bd9Sstevel@tonic-gate 		return (EIO);
35887c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
35897c478bd9Sstevel@tonic-gate 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
35907c478bd9Sstevel@tonic-gate 
35917c478bd9Sstevel@tonic-gate reread:
35927c478bd9Sstevel@tonic-gate 	bp = NULL;
35937c478bd9Sstevel@tonic-gate 	pp = NULL;
35947c478bd9Sstevel@tonic-gate 	pagefound = NULL;
35957c478bd9Sstevel@tonic-gate 
35967c478bd9Sstevel@tonic-gate 	if (pl != NULL)
35977c478bd9Sstevel@tonic-gate 		pl[0] = NULL;
35987c478bd9Sstevel@tonic-gate 
35997c478bd9Sstevel@tonic-gate 	error = 0;
36007c478bd9Sstevel@tonic-gate 	lbn = off / bsize;
36017c478bd9Sstevel@tonic-gate 	blkoff = lbn * bsize;
36027c478bd9Sstevel@tonic-gate 
36037c478bd9Sstevel@tonic-gate 	/*
36047c478bd9Sstevel@tonic-gate 	 * Queueing up the readahead before doing the synchronous read
36057c478bd9Sstevel@tonic-gate 	 * results in a significant increase in read throughput because
36067c478bd9Sstevel@tonic-gate 	 * of the increased parallelism between the async threads and
36077c478bd9Sstevel@tonic-gate 	 * the process context.
36087c478bd9Sstevel@tonic-gate 	 */
36097c478bd9Sstevel@tonic-gate 	if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
36107c478bd9Sstevel@tonic-gate 	    rw != S_CREATE &&
36117c478bd9Sstevel@tonic-gate 	    !(vp->v_flag & VNOCACHE)) {
36127c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
36137c478bd9Sstevel@tonic-gate 
36147c478bd9Sstevel@tonic-gate 		/*
36157c478bd9Sstevel@tonic-gate 		 * Calculate the number of readaheads to do.
36167c478bd9Sstevel@tonic-gate 		 * a) No readaheads at offset = 0.
36177c478bd9Sstevel@tonic-gate 		 * b) Do maximum(nfs_nra) readaheads when the readahead
36187c478bd9Sstevel@tonic-gate 		 *    window is closed.
36197c478bd9Sstevel@tonic-gate 		 * c) Do readaheads between 1 to (nfs_nra - 1) depending
36207c478bd9Sstevel@tonic-gate 		 *    upon how far the readahead window is open or close.
36217c478bd9Sstevel@tonic-gate 		 * d) No readaheads if rp->r_nextr is not within the scope
36227c478bd9Sstevel@tonic-gate 		 *    of the readahead window (random i/o).
36237c478bd9Sstevel@tonic-gate 		 */
36247c478bd9Sstevel@tonic-gate 
36257c478bd9Sstevel@tonic-gate 		if (off == 0)
36267c478bd9Sstevel@tonic-gate 			readahead = 0;
36277c478bd9Sstevel@tonic-gate 		else if (blkoff == rp->r_nextr)
36287c478bd9Sstevel@tonic-gate 			readahead = nfs_nra;
36297c478bd9Sstevel@tonic-gate 		else if (rp->r_nextr > blkoff &&
36307c478bd9Sstevel@tonic-gate 				((ra_window = (rp->r_nextr - blkoff) / bsize)
36317c478bd9Sstevel@tonic-gate 					<= (nfs_nra - 1)))
36327c478bd9Sstevel@tonic-gate 			readahead = nfs_nra - ra_window;
36337c478bd9Sstevel@tonic-gate 		else
36347c478bd9Sstevel@tonic-gate 			readahead = 0;
36357c478bd9Sstevel@tonic-gate 
36367c478bd9Sstevel@tonic-gate 		rablkoff = rp->r_nextr;
36377c478bd9Sstevel@tonic-gate 		while (readahead > 0 && rablkoff + bsize < rp->r_size) {
36387c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
36397c478bd9Sstevel@tonic-gate 			if (nfs_async_readahead(vp, rablkoff + bsize,
36407c478bd9Sstevel@tonic-gate 			    addr + (rablkoff + bsize - off), seg, cr,
36417c478bd9Sstevel@tonic-gate 			    nfs_readahead) < 0) {
36427c478bd9Sstevel@tonic-gate 				mutex_enter(&rp->r_statelock);
36437c478bd9Sstevel@tonic-gate 				break;
36447c478bd9Sstevel@tonic-gate 			}
36457c478bd9Sstevel@tonic-gate 			readahead--;
36467c478bd9Sstevel@tonic-gate 			rablkoff += bsize;
36477c478bd9Sstevel@tonic-gate 			/*
36487c478bd9Sstevel@tonic-gate 			 * Indicate that we did a readahead so
36497c478bd9Sstevel@tonic-gate 			 * readahead offset is not updated
36507c478bd9Sstevel@tonic-gate 			 * by the synchronous read below.
36517c478bd9Sstevel@tonic-gate 			 */
36527c478bd9Sstevel@tonic-gate 			readahead_issued = 1;
36537c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
36547c478bd9Sstevel@tonic-gate 			/*
36557c478bd9Sstevel@tonic-gate 			 * set readahead offset to
36567c478bd9Sstevel@tonic-gate 			 * offset of last async readahead
36577c478bd9Sstevel@tonic-gate 			 * request.
36587c478bd9Sstevel@tonic-gate 			 */
36597c478bd9Sstevel@tonic-gate 			rp->r_nextr = rablkoff;
36607c478bd9Sstevel@tonic-gate 		}
36617c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
36627c478bd9Sstevel@tonic-gate 	}
36637c478bd9Sstevel@tonic-gate 
36647c478bd9Sstevel@tonic-gate again:
36657c478bd9Sstevel@tonic-gate 	if ((pagefound = page_exists(vp, off)) == NULL) {
36667c478bd9Sstevel@tonic-gate 		if (pl == NULL) {
36677c478bd9Sstevel@tonic-gate 			(void) nfs_async_readahead(vp, blkoff, addr, seg, cr,
36687c478bd9Sstevel@tonic-gate 			    nfs_readahead);
36697c478bd9Sstevel@tonic-gate 		} else if (rw == S_CREATE) {
36707c478bd9Sstevel@tonic-gate 			/*
36717c478bd9Sstevel@tonic-gate 			 * Block for this page is not allocated, or the offset
36727c478bd9Sstevel@tonic-gate 			 * is beyond the current allocation size, or we're
36737c478bd9Sstevel@tonic-gate 			 * allocating a swap slot and the page was not found,
36747c478bd9Sstevel@tonic-gate 			 * so allocate it and return a zero page.
36757c478bd9Sstevel@tonic-gate 			 */
36767c478bd9Sstevel@tonic-gate 			if ((pp = page_create_va(vp, off,
36777c478bd9Sstevel@tonic-gate 			    PAGESIZE, PG_WAIT, seg, addr)) == NULL)
36787c478bd9Sstevel@tonic-gate 				cmn_err(CE_PANIC, "nfs_getapage: page_create");
36797c478bd9Sstevel@tonic-gate 			io_len = PAGESIZE;
36807c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
36817c478bd9Sstevel@tonic-gate 			rp->r_nextr = off + PAGESIZE;
36827c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
36837c478bd9Sstevel@tonic-gate 		} else {
36847c478bd9Sstevel@tonic-gate 			/*
36857c478bd9Sstevel@tonic-gate 			 * Need to go to server to get a BLOCK, exception to
36867c478bd9Sstevel@tonic-gate 			 * that being while reading at offset = 0 or doing
36877c478bd9Sstevel@tonic-gate 			 * random i/o, in that case read only a PAGE.
36887c478bd9Sstevel@tonic-gate 			 */
36897c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
36907c478bd9Sstevel@tonic-gate 			if (blkoff < rp->r_size &&
36917c478bd9Sstevel@tonic-gate 			    blkoff + bsize >= rp->r_size) {
36927c478bd9Sstevel@tonic-gate 				/*
36937c478bd9Sstevel@tonic-gate 				 * If only a block or less is left in
36947c478bd9Sstevel@tonic-gate 				 * the file, read all that is remaining.
36957c478bd9Sstevel@tonic-gate 				 */
36967c478bd9Sstevel@tonic-gate 				if (rp->r_size <= off) {
36977c478bd9Sstevel@tonic-gate 					/*
36987c478bd9Sstevel@tonic-gate 					 * Trying to access beyond EOF,
36997c478bd9Sstevel@tonic-gate 					 * set up to get at least one page.
37007c478bd9Sstevel@tonic-gate 					 */
37017c478bd9Sstevel@tonic-gate 					blksize = off + PAGESIZE - blkoff;
37027c478bd9Sstevel@tonic-gate 				} else
37037c478bd9Sstevel@tonic-gate 					blksize = rp->r_size - blkoff;
37047c478bd9Sstevel@tonic-gate 			} else if ((off == 0) ||
37057c478bd9Sstevel@tonic-gate 				(off != rp->r_nextr && !readahead_issued)) {
37067c478bd9Sstevel@tonic-gate 				blksize = PAGESIZE;
37077c478bd9Sstevel@tonic-gate 				blkoff = off; /* block = page here */
37087c478bd9Sstevel@tonic-gate 			} else
37097c478bd9Sstevel@tonic-gate 				blksize = bsize;
37107c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
37117c478bd9Sstevel@tonic-gate 
37127c478bd9Sstevel@tonic-gate 			pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
37137c478bd9Sstevel@tonic-gate 			    &io_len, blkoff, blksize, 0);
37147c478bd9Sstevel@tonic-gate 
37157c478bd9Sstevel@tonic-gate 			/*
37167c478bd9Sstevel@tonic-gate 			 * Some other thread has entered the page,
37177c478bd9Sstevel@tonic-gate 			 * so just use it.
37187c478bd9Sstevel@tonic-gate 			 */
37197c478bd9Sstevel@tonic-gate 			if (pp == NULL)
37207c478bd9Sstevel@tonic-gate 				goto again;
37217c478bd9Sstevel@tonic-gate 
37227c478bd9Sstevel@tonic-gate 			/*
37237c478bd9Sstevel@tonic-gate 			 * Now round the request size up to page boundaries.
37247c478bd9Sstevel@tonic-gate 			 * This ensures that the entire page will be
37257c478bd9Sstevel@tonic-gate 			 * initialized to zeroes if EOF is encountered.
37267c478bd9Sstevel@tonic-gate 			 */
37277c478bd9Sstevel@tonic-gate 			io_len = ptob(btopr(io_len));
37287c478bd9Sstevel@tonic-gate 
37297c478bd9Sstevel@tonic-gate 			bp = pageio_setup(pp, io_len, vp, B_READ);
37307c478bd9Sstevel@tonic-gate 			ASSERT(bp != NULL);
37317c478bd9Sstevel@tonic-gate 
37327c478bd9Sstevel@tonic-gate 			/*
37337c478bd9Sstevel@tonic-gate 			 * pageio_setup should have set b_addr to 0.  This
37347c478bd9Sstevel@tonic-gate 			 * is correct since we want to do I/O on a page
37357c478bd9Sstevel@tonic-gate 			 * boundary.  bp_mapin will use this addr to calculate
37367c478bd9Sstevel@tonic-gate 			 * an offset, and then set b_addr to the kernel virtual
37377c478bd9Sstevel@tonic-gate 			 * address it allocated for us.
37387c478bd9Sstevel@tonic-gate 			 */
37397c478bd9Sstevel@tonic-gate 			ASSERT(bp->b_un.b_addr == 0);
37407c478bd9Sstevel@tonic-gate 
37417c478bd9Sstevel@tonic-gate 			bp->b_edev = 0;
37427c478bd9Sstevel@tonic-gate 			bp->b_dev = 0;
37437c478bd9Sstevel@tonic-gate 			bp->b_lblkno = lbtodb(io_off);
37447c478bd9Sstevel@tonic-gate 			bp->b_file = vp;
37457c478bd9Sstevel@tonic-gate 			bp->b_offset = (offset_t)off;
37467c478bd9Sstevel@tonic-gate 			bp_mapin(bp);
37477c478bd9Sstevel@tonic-gate 
37487c478bd9Sstevel@tonic-gate 			/*
37497c478bd9Sstevel@tonic-gate 			 * If doing a write beyond what we believe is EOF,
37507c478bd9Sstevel@tonic-gate 			 * don't bother trying to read the pages from the
37517c478bd9Sstevel@tonic-gate 			 * server, we'll just zero the pages here.  We
37527c478bd9Sstevel@tonic-gate 			 * don't check that the rw flag is S_WRITE here
37537c478bd9Sstevel@tonic-gate 			 * because some implementations may attempt a
37547c478bd9Sstevel@tonic-gate 			 * read access to the buffer before copying data.
37557c478bd9Sstevel@tonic-gate 			 */
37567c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
37577c478bd9Sstevel@tonic-gate 			if (io_off >= rp->r_size && seg == segkmap) {
37587c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
37597c478bd9Sstevel@tonic-gate 				bzero(bp->b_un.b_addr, io_len);
37607c478bd9Sstevel@tonic-gate 			} else {
37617c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
37627c478bd9Sstevel@tonic-gate 				error = nfs_bio(bp, cr);
37637c478bd9Sstevel@tonic-gate 			}
37647c478bd9Sstevel@tonic-gate 
37657c478bd9Sstevel@tonic-gate 			/*
37667c478bd9Sstevel@tonic-gate 			 * Unmap the buffer before freeing it.
37677c478bd9Sstevel@tonic-gate 			 */
37687c478bd9Sstevel@tonic-gate 			bp_mapout(bp);
37697c478bd9Sstevel@tonic-gate 			pageio_done(bp);
37707c478bd9Sstevel@tonic-gate 
37717c478bd9Sstevel@tonic-gate 			if (error == NFS_EOF) {
37727c478bd9Sstevel@tonic-gate 				/*
37737c478bd9Sstevel@tonic-gate 				 * If doing a write system call just return
37747c478bd9Sstevel@tonic-gate 				 * zeroed pages, else user tried to get pages
37757c478bd9Sstevel@tonic-gate 				 * beyond EOF, return error.  We don't check
37767c478bd9Sstevel@tonic-gate 				 * that the rw flag is S_WRITE here because
37777c478bd9Sstevel@tonic-gate 				 * some implementations may attempt a read
37787c478bd9Sstevel@tonic-gate 				 * access to the buffer before copying data.
37797c478bd9Sstevel@tonic-gate 				 */
37807c478bd9Sstevel@tonic-gate 				if (seg == segkmap)
37817c478bd9Sstevel@tonic-gate 					error = 0;
37827c478bd9Sstevel@tonic-gate 				else
37837c478bd9Sstevel@tonic-gate 					error = EFAULT;
37847c478bd9Sstevel@tonic-gate 			}
37857c478bd9Sstevel@tonic-gate 
37867c478bd9Sstevel@tonic-gate 			if (!readahead_issued && !error) {
37877c478bd9Sstevel@tonic-gate 			    mutex_enter(&rp->r_statelock);
37887c478bd9Sstevel@tonic-gate 			    rp->r_nextr = io_off + io_len;
37897c478bd9Sstevel@tonic-gate 			    mutex_exit(&rp->r_statelock);
37907c478bd9Sstevel@tonic-gate 			}
37917c478bd9Sstevel@tonic-gate 		}
37927c478bd9Sstevel@tonic-gate 	}
37937c478bd9Sstevel@tonic-gate 
37947c478bd9Sstevel@tonic-gate out:
37957c478bd9Sstevel@tonic-gate 	if (pl == NULL)
37967c478bd9Sstevel@tonic-gate 		return (error);
37977c478bd9Sstevel@tonic-gate 
37987c478bd9Sstevel@tonic-gate 	if (error) {
37997c478bd9Sstevel@tonic-gate 		if (pp != NULL)
38007c478bd9Sstevel@tonic-gate 			pvn_read_done(pp, B_ERROR);
38017c478bd9Sstevel@tonic-gate 		return (error);
38027c478bd9Sstevel@tonic-gate 	}
38037c478bd9Sstevel@tonic-gate 
38047c478bd9Sstevel@tonic-gate 	if (pagefound) {
38057c478bd9Sstevel@tonic-gate 		se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
38067c478bd9Sstevel@tonic-gate 
38077c478bd9Sstevel@tonic-gate 		/*
38087c478bd9Sstevel@tonic-gate 		 * Page exists in the cache, acquire the appropriate lock.
38097c478bd9Sstevel@tonic-gate 		 * If this fails, start all over again.
38107c478bd9Sstevel@tonic-gate 		 */
38117c478bd9Sstevel@tonic-gate 		if ((pp = page_lookup(vp, off, se)) == NULL) {
38127c478bd9Sstevel@tonic-gate #ifdef DEBUG
38137c478bd9Sstevel@tonic-gate 			nfs_lostpage++;
38147c478bd9Sstevel@tonic-gate #endif
38157c478bd9Sstevel@tonic-gate 			goto reread;
38167c478bd9Sstevel@tonic-gate 		}
38177c478bd9Sstevel@tonic-gate 		pl[0] = pp;
38187c478bd9Sstevel@tonic-gate 		pl[1] = NULL;
38197c478bd9Sstevel@tonic-gate 		return (0);
38207c478bd9Sstevel@tonic-gate 	}
38217c478bd9Sstevel@tonic-gate 
38227c478bd9Sstevel@tonic-gate 	if (pp != NULL)
38237c478bd9Sstevel@tonic-gate 		pvn_plist_init(pp, pl, plsz, off, io_len, rw);
38247c478bd9Sstevel@tonic-gate 
38257c478bd9Sstevel@tonic-gate 	return (error);
38267c478bd9Sstevel@tonic-gate }
38277c478bd9Sstevel@tonic-gate 
38287c478bd9Sstevel@tonic-gate static void
38297c478bd9Sstevel@tonic-gate nfs_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
38307c478bd9Sstevel@tonic-gate 	cred_t *cr)
38317c478bd9Sstevel@tonic-gate {
38327c478bd9Sstevel@tonic-gate 	int error;
38337c478bd9Sstevel@tonic-gate 	page_t *pp;
38347c478bd9Sstevel@tonic-gate 	u_offset_t io_off;
38357c478bd9Sstevel@tonic-gate 	size_t io_len;
38367c478bd9Sstevel@tonic-gate 	struct buf *bp;
38377c478bd9Sstevel@tonic-gate 	uint_t bsize, blksize;
38387c478bd9Sstevel@tonic-gate 	rnode_t *rp = VTOR(vp);
38397c478bd9Sstevel@tonic-gate 
3840108322fbScarlsonj 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
38417c478bd9Sstevel@tonic-gate 
38427c478bd9Sstevel@tonic-gate 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
38457c478bd9Sstevel@tonic-gate 	if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
38467c478bd9Sstevel@tonic-gate 		/*
38477c478bd9Sstevel@tonic-gate 		 * If less than a block left in file read less
38487c478bd9Sstevel@tonic-gate 		 * than a block.
38497c478bd9Sstevel@tonic-gate 		 */
38507c478bd9Sstevel@tonic-gate 		blksize = rp->r_size - blkoff;
38517c478bd9Sstevel@tonic-gate 	} else
38527c478bd9Sstevel@tonic-gate 		blksize = bsize;
38537c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
38547c478bd9Sstevel@tonic-gate 
38557c478bd9Sstevel@tonic-gate 	pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
38567c478bd9Sstevel@tonic-gate 	    &io_off, &io_len, blkoff, blksize, 1);
38577c478bd9Sstevel@tonic-gate 	/*
38587c478bd9Sstevel@tonic-gate 	 * The isra flag passed to the kluster function is 1, we may have
38597c478bd9Sstevel@tonic-gate 	 * gotten a return value of NULL for a variety of reasons (# of free
38607c478bd9Sstevel@tonic-gate 	 * pages < minfree, someone entered the page on the vnode etc). In all
38617c478bd9Sstevel@tonic-gate 	 * cases, we want to punt on the readahead.
38627c478bd9Sstevel@tonic-gate 	 */
38637c478bd9Sstevel@tonic-gate 	if (pp == NULL)
38647c478bd9Sstevel@tonic-gate 		return;
38657c478bd9Sstevel@tonic-gate 
38667c478bd9Sstevel@tonic-gate 	/*
38677c478bd9Sstevel@tonic-gate 	 * Now round the request size up to page boundaries.
38687c478bd9Sstevel@tonic-gate 	 * This ensures that the entire page will be
38697c478bd9Sstevel@tonic-gate 	 * initialized to zeroes if EOF is encountered.
38707c478bd9Sstevel@tonic-gate 	 */
38717c478bd9Sstevel@tonic-gate 	io_len = ptob(btopr(io_len));
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate 	bp = pageio_setup(pp, io_len, vp, B_READ);
38747c478bd9Sstevel@tonic-gate 	ASSERT(bp != NULL);
38757c478bd9Sstevel@tonic-gate 
38767c478bd9Sstevel@tonic-gate 	/*
38777c478bd9Sstevel@tonic-gate 	 * pageio_setup should have set b_addr to 0.  This is correct since
38787c478bd9Sstevel@tonic-gate 	 * we want to do I/O on a page boundary. bp_mapin() will use this addr
38797c478bd9Sstevel@tonic-gate 	 * to calculate an offset, and then set b_addr to the kernel virtual
38807c478bd9Sstevel@tonic-gate 	 * address it allocated for us.
38817c478bd9Sstevel@tonic-gate 	 */
38827c478bd9Sstevel@tonic-gate 	ASSERT(bp->b_un.b_addr == 0);
38837c478bd9Sstevel@tonic-gate 
38847c478bd9Sstevel@tonic-gate 	bp->b_edev = 0;
38857c478bd9Sstevel@tonic-gate 	bp->b_dev = 0;
38867c478bd9Sstevel@tonic-gate 	bp->b_lblkno = lbtodb(io_off);
38877c478bd9Sstevel@tonic-gate 	bp->b_file = vp;
38887c478bd9Sstevel@tonic-gate 	bp->b_offset = (offset_t)blkoff;
38897c478bd9Sstevel@tonic-gate 	bp_mapin(bp);
38907c478bd9Sstevel@tonic-gate 
38917c478bd9Sstevel@tonic-gate 	/*
38927c478bd9Sstevel@tonic-gate 	 * If doing a write beyond what we believe is EOF, don't bother trying
38937c478bd9Sstevel@tonic-gate 	 * to read the pages from the server, we'll just zero the pages here.
38947c478bd9Sstevel@tonic-gate 	 * We don't check that the rw flag is S_WRITE here because some
38957c478bd9Sstevel@tonic-gate 	 * implementations may attempt a read access to the buffer before
38967c478bd9Sstevel@tonic-gate 	 * copying data.
38977c478bd9Sstevel@tonic-gate 	 */
38987c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
38997c478bd9Sstevel@tonic-gate 	if (io_off >= rp->r_size && seg == segkmap) {
39007c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
39017c478bd9Sstevel@tonic-gate 		bzero(bp->b_un.b_addr, io_len);
39027c478bd9Sstevel@tonic-gate 		error = 0;
39037c478bd9Sstevel@tonic-gate 	} else {
39047c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
39057c478bd9Sstevel@tonic-gate 		error = nfs_bio(bp, cr);
39067c478bd9Sstevel@tonic-gate 		if (error == NFS_EOF)
39077c478bd9Sstevel@tonic-gate 			error = 0;
39087c478bd9Sstevel@tonic-gate 	}
39097c478bd9Sstevel@tonic-gate 
39107c478bd9Sstevel@tonic-gate 	/*
39117c478bd9Sstevel@tonic-gate 	 * Unmap the buffer before freeing it.
39127c478bd9Sstevel@tonic-gate 	 */
39137c478bd9Sstevel@tonic-gate 	bp_mapout(bp);
39147c478bd9Sstevel@tonic-gate 	pageio_done(bp);
39157c478bd9Sstevel@tonic-gate 
39167c478bd9Sstevel@tonic-gate 	pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
39177c478bd9Sstevel@tonic-gate 
39187c478bd9Sstevel@tonic-gate 	/*
39197c478bd9Sstevel@tonic-gate 	 * In case of error set readahead offset
39207c478bd9Sstevel@tonic-gate 	 * to the lowest offset.
39217c478bd9Sstevel@tonic-gate 	 * pvn_read_done() calls VN_DISPOSE to destroy the pages
39227c478bd9Sstevel@tonic-gate 	 */
39237c478bd9Sstevel@tonic-gate 	if (error && rp->r_nextr > io_off) {
39247c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
39257c478bd9Sstevel@tonic-gate 		if (rp->r_nextr > io_off)
39267c478bd9Sstevel@tonic-gate 			rp->r_nextr = io_off;
39277c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
39287c478bd9Sstevel@tonic-gate 	}
39297c478bd9Sstevel@tonic-gate }
39307c478bd9Sstevel@tonic-gate 
39317c478bd9Sstevel@tonic-gate /*
39327c478bd9Sstevel@tonic-gate  * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
39337c478bd9Sstevel@tonic-gate  * If len == 0, do from off to EOF.
39347c478bd9Sstevel@tonic-gate  *
39357c478bd9Sstevel@tonic-gate  * The normal cases should be len == 0 && off == 0 (entire vp list),
39367c478bd9Sstevel@tonic-gate  * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
39377c478bd9Sstevel@tonic-gate  * (from pageout).
39387c478bd9Sstevel@tonic-gate  */
39397c478bd9Sstevel@tonic-gate static int
39407c478bd9Sstevel@tonic-gate nfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr)
39417c478bd9Sstevel@tonic-gate {
39427c478bd9Sstevel@tonic-gate 	int error;
39437c478bd9Sstevel@tonic-gate 	rnode_t *rp;
39447c478bd9Sstevel@tonic-gate 
39457c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
39467c478bd9Sstevel@tonic-gate 
39477c478bd9Sstevel@tonic-gate 	/*
39487c478bd9Sstevel@tonic-gate 	 * XXX - Why should this check be made here?
39497c478bd9Sstevel@tonic-gate 	 */
39507c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
39517c478bd9Sstevel@tonic-gate 		return (ENOSYS);
39527c478bd9Sstevel@tonic-gate 
39537c478bd9Sstevel@tonic-gate 	if (len == 0 && !(flags & B_INVAL) && vn_is_readonly(vp))
39547c478bd9Sstevel@tonic-gate 		return (0);
39557c478bd9Sstevel@tonic-gate 
3956108322fbScarlsonj 	if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
39577c478bd9Sstevel@tonic-gate 		return (EIO);
39587c478bd9Sstevel@tonic-gate 	ASSERT(off <= MAXOFF32_T);
39597c478bd9Sstevel@tonic-gate 
39607c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
39617c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
39627c478bd9Sstevel@tonic-gate 	rp->r_count++;
39637c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
39647c478bd9Sstevel@tonic-gate 	error = nfs_putpages(vp, off, len, flags, cr);
39657c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
39667c478bd9Sstevel@tonic-gate 	rp->r_count--;
39677c478bd9Sstevel@tonic-gate 	cv_broadcast(&rp->r_cv);
39687c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
39697c478bd9Sstevel@tonic-gate 
39707c478bd9Sstevel@tonic-gate 	return (error);
39717c478bd9Sstevel@tonic-gate }
39727c478bd9Sstevel@tonic-gate 
39737c478bd9Sstevel@tonic-gate /*
39747c478bd9Sstevel@tonic-gate  * Write out a single page, possibly klustering adjacent dirty pages.
39757c478bd9Sstevel@tonic-gate  */
39767c478bd9Sstevel@tonic-gate int
39777c478bd9Sstevel@tonic-gate nfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
39787c478bd9Sstevel@tonic-gate 	int flags, cred_t *cr)
39797c478bd9Sstevel@tonic-gate {
39807c478bd9Sstevel@tonic-gate 	u_offset_t io_off;
39817c478bd9Sstevel@tonic-gate 	u_offset_t lbn_off;
39827c478bd9Sstevel@tonic-gate 	u_offset_t lbn;
39837c478bd9Sstevel@tonic-gate 	size_t io_len;
39847c478bd9Sstevel@tonic-gate 	uint_t bsize;
39857c478bd9Sstevel@tonic-gate 	int error;
39867c478bd9Sstevel@tonic-gate 	rnode_t *rp;
39877c478bd9Sstevel@tonic-gate 
39887c478bd9Sstevel@tonic-gate 	ASSERT(!vn_is_readonly(vp));
39897c478bd9Sstevel@tonic-gate 	ASSERT(pp != NULL);
39907c478bd9Sstevel@tonic-gate 	ASSERT(cr != NULL);
3991108322fbScarlsonj 	ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI(vp)->mi_zone);
39927c478bd9Sstevel@tonic-gate 
39937c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
39947c478bd9Sstevel@tonic-gate 	ASSERT(rp->r_count > 0);
39957c478bd9Sstevel@tonic-gate 
39967c478bd9Sstevel@tonic-gate 	ASSERT(pp->p_offset <= MAXOFF32_T);
39977c478bd9Sstevel@tonic-gate 
39987c478bd9Sstevel@tonic-gate 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
39997c478bd9Sstevel@tonic-gate 	lbn = pp->p_offset / bsize;
40007c478bd9Sstevel@tonic-gate 	lbn_off = lbn * bsize;
40017c478bd9Sstevel@tonic-gate 
40027c478bd9Sstevel@tonic-gate 	/*
40037c478bd9Sstevel@tonic-gate 	 * Find a kluster that fits in one block, or in
40047c478bd9Sstevel@tonic-gate 	 * one page if pages are bigger than blocks.  If
40057c478bd9Sstevel@tonic-gate 	 * there is less file space allocated than a whole
40067c478bd9Sstevel@tonic-gate 	 * page, we'll shorten the i/o request below.
40077c478bd9Sstevel@tonic-gate 	 */
40087c478bd9Sstevel@tonic-gate 	pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
40097c478bd9Sstevel@tonic-gate 	    roundup(bsize, PAGESIZE), flags);
40107c478bd9Sstevel@tonic-gate 
40117c478bd9Sstevel@tonic-gate 	/*
40127c478bd9Sstevel@tonic-gate 	 * pvn_write_kluster shouldn't have returned a page with offset
40137c478bd9Sstevel@tonic-gate 	 * behind the original page we were given.  Verify that.
40147c478bd9Sstevel@tonic-gate 	 */
40157c478bd9Sstevel@tonic-gate 	ASSERT((pp->p_offset / bsize) >= lbn);
40167c478bd9Sstevel@tonic-gate 
40177c478bd9Sstevel@tonic-gate 	/*
40187c478bd9Sstevel@tonic-gate 	 * Now pp will have the list of kept dirty pages marked for
40197c478bd9Sstevel@tonic-gate 	 * write back.  It will also handle invalidation and freeing
40207c478bd9Sstevel@tonic-gate 	 * of pages that are not dirty.  Check for page length rounding
40217c478bd9Sstevel@tonic-gate 	 * problems.
40227c478bd9Sstevel@tonic-gate 	 */
40237c478bd9Sstevel@tonic-gate 	if (io_off + io_len > lbn_off + bsize) {
40247c478bd9Sstevel@tonic-gate 		ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
40257c478bd9Sstevel@tonic-gate 		io_len = lbn_off + bsize - io_off;
40267c478bd9Sstevel@tonic-gate 	}
40277c478bd9Sstevel@tonic-gate 	/*
40287c478bd9Sstevel@tonic-gate 	 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
40297c478bd9Sstevel@tonic-gate 	 * consistent value of r_size. RMODINPROGRESS is set in writerp().
40307c478bd9Sstevel@tonic-gate 	 * When RMODINPROGRESS is set it indicates that a uiomove() is in
40317c478bd9Sstevel@tonic-gate 	 * progress and the r_size has not been made consistent with the
40327c478bd9Sstevel@tonic-gate 	 * new size of the file. When the uiomove() completes the r_size is
40337c478bd9Sstevel@tonic-gate 	 * updated and the RMODINPROGRESS flag is cleared.
40347c478bd9Sstevel@tonic-gate 	 *
40357c478bd9Sstevel@tonic-gate 	 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
40367c478bd9Sstevel@tonic-gate 	 * consistent value of r_size. Without this handshaking, it is
40377c478bd9Sstevel@tonic-gate 	 * possible that nfs(3)_bio() picks  up the old value of r_size
40387c478bd9Sstevel@tonic-gate 	 * before the uiomove() in writerp() completes. This will result
40397c478bd9Sstevel@tonic-gate 	 * in the write through nfs(3)_bio() being dropped.
40407c478bd9Sstevel@tonic-gate 	 *
40417c478bd9Sstevel@tonic-gate 	 * More precisely, there is a window between the time the uiomove()
40427c478bd9Sstevel@tonic-gate 	 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
40437c478bd9Sstevel@tonic-gate 	 * operation intervenes in this window, the page will be picked up,
40447c478bd9Sstevel@tonic-gate 	 * because it is dirty (it will be unlocked, unless it was
40457c478bd9Sstevel@tonic-gate 	 * pagecreate'd). When the page is picked up as dirty, the dirty
40467c478bd9Sstevel@tonic-gate 	 * bit is reset (pvn_getdirty()). In nfs(3)write(), r_size is
40477c478bd9Sstevel@tonic-gate 	 * checked. This will still be the old size. Therefore the page will
40487c478bd9Sstevel@tonic-gate 	 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
40497c478bd9Sstevel@tonic-gate 	 * the page will be found to be clean and the write will be dropped.
40507c478bd9Sstevel@tonic-gate 	 */
40517c478bd9Sstevel@tonic-gate 	if (rp->r_flags & RMODINPROGRESS) {
40527c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
40537c478bd9Sstevel@tonic-gate 		if ((rp->r_flags & RMODINPROGRESS) &&
40547c478bd9Sstevel@tonic-gate 		    rp->r_modaddr + MAXBSIZE > io_off &&
40557c478bd9Sstevel@tonic-gate 		    rp->r_modaddr < io_off + io_len) {
40567c478bd9Sstevel@tonic-gate 			page_t *plist;
40577c478bd9Sstevel@tonic-gate 			/*
40587c478bd9Sstevel@tonic-gate 			 * A write is in progress for this region of the file.
40597c478bd9Sstevel@tonic-gate 			 * If we did not detect RMODINPROGRESS here then this
40607c478bd9Sstevel@tonic-gate 			 * path through nfs_putapage() would eventually go to
40617c478bd9Sstevel@tonic-gate 			 * nfs(3)_bio() and may not write out all of the data
40627c478bd9Sstevel@tonic-gate 			 * in the pages. We end up losing data. So we decide
40637c478bd9Sstevel@tonic-gate 			 * to set the modified bit on each page in the page
40647c478bd9Sstevel@tonic-gate 			 * list and mark the rnode with RDIRTY. This write
40657c478bd9Sstevel@tonic-gate 			 * will be restarted at some later time.
40667c478bd9Sstevel@tonic-gate 			 */
40677c478bd9Sstevel@tonic-gate 			plist = pp;
40687c478bd9Sstevel@tonic-gate 			while (plist != NULL) {
40697c478bd9Sstevel@tonic-gate 				pp = plist;
40707c478bd9Sstevel@tonic-gate 				page_sub(&plist, pp);
40717c478bd9Sstevel@tonic-gate 				hat_setmod(pp);
40727c478bd9Sstevel@tonic-gate 				page_io_unlock(pp);
40737c478bd9Sstevel@tonic-gate 				page_unlock(pp);
40747c478bd9Sstevel@tonic-gate 			}
40757c478bd9Sstevel@tonic-gate 			rp->r_flags |= RDIRTY;
40767c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
40777c478bd9Sstevel@tonic-gate 			if (offp)
40787c478bd9Sstevel@tonic-gate 				*offp = io_off;
40797c478bd9Sstevel@tonic-gate 			if (lenp)
40807c478bd9Sstevel@tonic-gate 				*lenp = io_len;
40817c478bd9Sstevel@tonic-gate 			return (0);
40827c478bd9Sstevel@tonic-gate 		}
40837c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
40847c478bd9Sstevel@tonic-gate 	}
40857c478bd9Sstevel@tonic-gate 
40867c478bd9Sstevel@tonic-gate 	if (flags & B_ASYNC) {
40877c478bd9Sstevel@tonic-gate 		error = nfs_async_putapage(vp, pp, io_off, io_len, flags, cr,
40887c478bd9Sstevel@tonic-gate 		    nfs_sync_putapage);
40897c478bd9Sstevel@tonic-gate 	} else
40907c478bd9Sstevel@tonic-gate 		error = nfs_sync_putapage(vp, pp, io_off, io_len, flags, cr);
40917c478bd9Sstevel@tonic-gate 
40927c478bd9Sstevel@tonic-gate 	if (offp)
40937c478bd9Sstevel@tonic-gate 		*offp = io_off;
40947c478bd9Sstevel@tonic-gate 	if (lenp)
40957c478bd9Sstevel@tonic-gate 		*lenp = io_len;
40967c478bd9Sstevel@tonic-gate 	return (error);
40977c478bd9Sstevel@tonic-gate }
40987c478bd9Sstevel@tonic-gate 
40997c478bd9Sstevel@tonic-gate static int
41007c478bd9Sstevel@tonic-gate nfs_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
41017c478bd9Sstevel@tonic-gate 	int flags, cred_t *cr)
41027c478bd9Sstevel@tonic-gate {
41037c478bd9Sstevel@tonic-gate 	int error;
41047c478bd9Sstevel@tonic-gate 	rnode_t *rp;
41057c478bd9Sstevel@tonic-gate 
41067c478bd9Sstevel@tonic-gate 	flags |= B_WRITE;
41077c478bd9Sstevel@tonic-gate 
4108108322fbScarlsonj 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
41097c478bd9Sstevel@tonic-gate 	error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
41107c478bd9Sstevel@tonic-gate 
41117c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
41127c478bd9Sstevel@tonic-gate 
41137c478bd9Sstevel@tonic-gate 	if ((error == ENOSPC || error == EDQUOT || error == EACCES) &&
41147c478bd9Sstevel@tonic-gate 	    (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
41157c478bd9Sstevel@tonic-gate 		if (!(rp->r_flags & ROUTOFSPACE)) {
41167c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
41177c478bd9Sstevel@tonic-gate 			rp->r_flags |= ROUTOFSPACE;
41187c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
41197c478bd9Sstevel@tonic-gate 		}
41207c478bd9Sstevel@tonic-gate 		flags |= B_ERROR;
41217c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, flags);
41227c478bd9Sstevel@tonic-gate 		/*
41237c478bd9Sstevel@tonic-gate 		 * If this was not an async thread, then try again to
41247c478bd9Sstevel@tonic-gate 		 * write out the pages, but this time, also destroy
41257c478bd9Sstevel@tonic-gate 		 * them whether or not the write is successful.  This
41267c478bd9Sstevel@tonic-gate 		 * will prevent memory from filling up with these
41277c478bd9Sstevel@tonic-gate 		 * pages and destroying them is the only alternative
41287c478bd9Sstevel@tonic-gate 		 * if they can't be written out.
41297c478bd9Sstevel@tonic-gate 		 *
41307c478bd9Sstevel@tonic-gate 		 * Don't do this if this is an async thread because
41317c478bd9Sstevel@tonic-gate 		 * when the pages are unlocked in pvn_write_done,
41327c478bd9Sstevel@tonic-gate 		 * some other thread could have come along, locked
41337c478bd9Sstevel@tonic-gate 		 * them, and queued for an async thread.  It would be
41347c478bd9Sstevel@tonic-gate 		 * possible for all of the async threads to be tied
41357c478bd9Sstevel@tonic-gate 		 * up waiting to lock the pages again and they would
41367c478bd9Sstevel@tonic-gate 		 * all already be locked and waiting for an async
41377c478bd9Sstevel@tonic-gate 		 * thread to handle them.  Deadlock.
41387c478bd9Sstevel@tonic-gate 		 */
41397c478bd9Sstevel@tonic-gate 		if (!(flags & B_ASYNC)) {
41407c478bd9Sstevel@tonic-gate 			error = nfs_putpage(vp, io_off, io_len,
41417c478bd9Sstevel@tonic-gate 			    B_INVAL | B_FORCE, cr);
41427c478bd9Sstevel@tonic-gate 		}
41437c478bd9Sstevel@tonic-gate 	} else {
41447c478bd9Sstevel@tonic-gate 		if (error)
41457c478bd9Sstevel@tonic-gate 			flags |= B_ERROR;
41467c478bd9Sstevel@tonic-gate 		else if (rp->r_flags & ROUTOFSPACE) {
41477c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
41487c478bd9Sstevel@tonic-gate 			rp->r_flags &= ~ROUTOFSPACE;
41497c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
41507c478bd9Sstevel@tonic-gate 		}
41517c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, flags);
41527c478bd9Sstevel@tonic-gate 	}
41537c478bd9Sstevel@tonic-gate 
41547c478bd9Sstevel@tonic-gate 	return (error);
41557c478bd9Sstevel@tonic-gate }
41567c478bd9Sstevel@tonic-gate 
41577c478bd9Sstevel@tonic-gate static int
41587c478bd9Sstevel@tonic-gate nfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
41597c478bd9Sstevel@tonic-gate 	size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
41607c478bd9Sstevel@tonic-gate {
41617c478bd9Sstevel@tonic-gate 	struct segvn_crargs vn_a;
41627c478bd9Sstevel@tonic-gate 	int error;
41637c478bd9Sstevel@tonic-gate 	rnode_t *rp;
41647c478bd9Sstevel@tonic-gate 	struct vattr va;
41657c478bd9Sstevel@tonic-gate 
4166108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
41677c478bd9Sstevel@tonic-gate 		return (EIO);
41687c478bd9Sstevel@tonic-gate 
41697c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
41707c478bd9Sstevel@tonic-gate 		return (ENOSYS);
41717c478bd9Sstevel@tonic-gate 
41727c478bd9Sstevel@tonic-gate 	if (off > MAXOFF32_T)
41737c478bd9Sstevel@tonic-gate 		return (EFBIG);
41747c478bd9Sstevel@tonic-gate 
41757c478bd9Sstevel@tonic-gate 	if (off < 0 || off + len < 0)
41767c478bd9Sstevel@tonic-gate 		return (ENXIO);
41777c478bd9Sstevel@tonic-gate 
41787c478bd9Sstevel@tonic-gate 	if (vp->v_type != VREG)
41797c478bd9Sstevel@tonic-gate 		return (ENODEV);
41807c478bd9Sstevel@tonic-gate 
41817c478bd9Sstevel@tonic-gate 	/*
41827c478bd9Sstevel@tonic-gate 	 * If there is cached data and if close-to-open consistency
41837c478bd9Sstevel@tonic-gate 	 * checking is not turned off and if the file system is not
41847c478bd9Sstevel@tonic-gate 	 * mounted readonly, then force an over the wire getattr.
41857c478bd9Sstevel@tonic-gate 	 * Otherwise, just invoke nfsgetattr to get a copy of the
41867c478bd9Sstevel@tonic-gate 	 * attributes.  The attribute cache will be used unless it
41877c478bd9Sstevel@tonic-gate 	 * is timed out and if it is, then an over the wire getattr
41887c478bd9Sstevel@tonic-gate 	 * will be issued.
41897c478bd9Sstevel@tonic-gate 	 */
41907c478bd9Sstevel@tonic-gate 	va.va_mask = AT_ALL;
41917c478bd9Sstevel@tonic-gate 	if (vn_has_cached_data(vp) &&
41927c478bd9Sstevel@tonic-gate 	    !(VTOMI(vp)->mi_flags & MI_NOCTO) && !vn_is_readonly(vp))
41937c478bd9Sstevel@tonic-gate 		error = nfs_getattr_otw(vp, &va, cr);
41947c478bd9Sstevel@tonic-gate 	else
41957c478bd9Sstevel@tonic-gate 		error = nfsgetattr(vp, &va, cr);
41967c478bd9Sstevel@tonic-gate 	if (error)
41977c478bd9Sstevel@tonic-gate 		return (error);
41987c478bd9Sstevel@tonic-gate 
41997c478bd9Sstevel@tonic-gate 	/*
42007c478bd9Sstevel@tonic-gate 	 * Check to see if the vnode is currently marked as not cachable.
42017c478bd9Sstevel@tonic-gate 	 * This means portions of the file are locked (through VOP_FRLOCK).
42027c478bd9Sstevel@tonic-gate 	 * In this case the map request must be refused.  We use
42037c478bd9Sstevel@tonic-gate 	 * rp->r_lkserlock to avoid a race with concurrent lock requests.
42047c478bd9Sstevel@tonic-gate 	 */
42057c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
42067c478bd9Sstevel@tonic-gate 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp)))
42077c478bd9Sstevel@tonic-gate 		return (EINTR);
42087c478bd9Sstevel@tonic-gate 
42097c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOCACHE) {
42107c478bd9Sstevel@tonic-gate 		error = EAGAIN;
42117c478bd9Sstevel@tonic-gate 		goto done;
42127c478bd9Sstevel@tonic-gate 	}
42137c478bd9Sstevel@tonic-gate 
42147c478bd9Sstevel@tonic-gate 	/*
42157c478bd9Sstevel@tonic-gate 	 * Don't allow concurrent locks and mapping if mandatory locking is
42167c478bd9Sstevel@tonic-gate 	 * enabled.
42177c478bd9Sstevel@tonic-gate 	 */
42187c478bd9Sstevel@tonic-gate 	if ((flk_has_remote_locks(vp) || lm_has_sleep(vp)) &&
42197c478bd9Sstevel@tonic-gate 	    MANDLOCK(vp, va.va_mode)) {
42207c478bd9Sstevel@tonic-gate 		error = EAGAIN;
42217c478bd9Sstevel@tonic-gate 		goto done;
42227c478bd9Sstevel@tonic-gate 	}
42237c478bd9Sstevel@tonic-gate 
42247c478bd9Sstevel@tonic-gate 	as_rangelock(as);
42257c478bd9Sstevel@tonic-gate 	if (!(flags & MAP_FIXED)) {
42267c478bd9Sstevel@tonic-gate 		map_addr(addrp, len, off, 1, flags);
42277c478bd9Sstevel@tonic-gate 		if (*addrp == NULL) {
42287c478bd9Sstevel@tonic-gate 			as_rangeunlock(as);
42297c478bd9Sstevel@tonic-gate 			error = ENOMEM;
42307c478bd9Sstevel@tonic-gate 			goto done;
42317c478bd9Sstevel@tonic-gate 		}
42327c478bd9Sstevel@tonic-gate 	} else {
42337c478bd9Sstevel@tonic-gate 		/*
42347c478bd9Sstevel@tonic-gate 		 * User specified address - blow away any previous mappings
42357c478bd9Sstevel@tonic-gate 		 */
42367c478bd9Sstevel@tonic-gate 		(void) as_unmap(as, *addrp, len);
42377c478bd9Sstevel@tonic-gate 	}
42387c478bd9Sstevel@tonic-gate 
42397c478bd9Sstevel@tonic-gate 	vn_a.vp = vp;
42407c478bd9Sstevel@tonic-gate 	vn_a.offset = off;
42417c478bd9Sstevel@tonic-gate 	vn_a.type = (flags & MAP_TYPE);
42427c478bd9Sstevel@tonic-gate 	vn_a.prot = (uchar_t)prot;
42437c478bd9Sstevel@tonic-gate 	vn_a.maxprot = (uchar_t)maxprot;
42447c478bd9Sstevel@tonic-gate 	vn_a.flags = (flags & ~MAP_TYPE);
42457c478bd9Sstevel@tonic-gate 	vn_a.cred = cr;
42467c478bd9Sstevel@tonic-gate 	vn_a.amp = NULL;
42477c478bd9Sstevel@tonic-gate 	vn_a.szc = 0;
42487c478bd9Sstevel@tonic-gate 	vn_a.lgrp_mem_policy_flags = 0;
42497c478bd9Sstevel@tonic-gate 
42507c478bd9Sstevel@tonic-gate 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
42517c478bd9Sstevel@tonic-gate 	as_rangeunlock(as);
42527c478bd9Sstevel@tonic-gate 
42537c478bd9Sstevel@tonic-gate done:
42547c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&rp->r_lkserlock);
42557c478bd9Sstevel@tonic-gate 	return (error);
42567c478bd9Sstevel@tonic-gate }
42577c478bd9Sstevel@tonic-gate 
42587c478bd9Sstevel@tonic-gate /* ARGSUSED */
42597c478bd9Sstevel@tonic-gate static int
42607c478bd9Sstevel@tonic-gate nfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
42617c478bd9Sstevel@tonic-gate 	size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
42627c478bd9Sstevel@tonic-gate {
42637c478bd9Sstevel@tonic-gate 	rnode_t *rp;
42647c478bd9Sstevel@tonic-gate 
42657c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
42667c478bd9Sstevel@tonic-gate 		return (ENOSYS);
4267108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
42687c478bd9Sstevel@tonic-gate 		return (EIO);
42697c478bd9Sstevel@tonic-gate 
42707c478bd9Sstevel@tonic-gate 	/*
42717c478bd9Sstevel@tonic-gate 	 * Need to hold rwlock while incrementing the mapcnt so that
42727c478bd9Sstevel@tonic-gate 	 * mmap'ing can be serialized with writes so that the caching
42737c478bd9Sstevel@tonic-gate 	 * can be handled correctly.
42747c478bd9Sstevel@tonic-gate 	 */
42757c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
42767c478bd9Sstevel@tonic-gate 	if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp)))
42777c478bd9Sstevel@tonic-gate 		return (EINTR);
42787c478bd9Sstevel@tonic-gate 	atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
42797c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&rp->r_rwlock);
42807c478bd9Sstevel@tonic-gate 
42817c478bd9Sstevel@tonic-gate 	return (0);
42827c478bd9Sstevel@tonic-gate }
42837c478bd9Sstevel@tonic-gate 
42847c478bd9Sstevel@tonic-gate static int
42857c478bd9Sstevel@tonic-gate nfs_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
42867c478bd9Sstevel@tonic-gate 	offset_t offset, struct flk_callback *flk_cbp, cred_t *cr)
42877c478bd9Sstevel@tonic-gate {
42887c478bd9Sstevel@tonic-gate 	netobj lm_fh;
42897c478bd9Sstevel@tonic-gate 	int rc;
42907c478bd9Sstevel@tonic-gate 	u_offset_t start, end;
42917c478bd9Sstevel@tonic-gate 	rnode_t *rp;
42927c478bd9Sstevel@tonic-gate 	int error = 0, intr = INTR(vp);
42937c478bd9Sstevel@tonic-gate 
42947c478bd9Sstevel@tonic-gate 	/* check for valid cmd parameter */
42957c478bd9Sstevel@tonic-gate 	if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
42967c478bd9Sstevel@tonic-gate 		return (EINVAL);
4297108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
42987c478bd9Sstevel@tonic-gate 		return (EIO);
42997c478bd9Sstevel@tonic-gate 
43007c478bd9Sstevel@tonic-gate 	/* Verify l_type. */
43017c478bd9Sstevel@tonic-gate 	switch (bfp->l_type) {
43027c478bd9Sstevel@tonic-gate 	case F_RDLCK:
43037c478bd9Sstevel@tonic-gate 		if (cmd != F_GETLK && !(flag & FREAD))
43047c478bd9Sstevel@tonic-gate 			return (EBADF);
43057c478bd9Sstevel@tonic-gate 		break;
43067c478bd9Sstevel@tonic-gate 	case F_WRLCK:
43077c478bd9Sstevel@tonic-gate 		if (cmd != F_GETLK && !(flag & FWRITE))
43087c478bd9Sstevel@tonic-gate 			return (EBADF);
43097c478bd9Sstevel@tonic-gate 		break;
43107c478bd9Sstevel@tonic-gate 	case F_UNLCK:
43117c478bd9Sstevel@tonic-gate 		intr = 0;
43127c478bd9Sstevel@tonic-gate 		break;
43137c478bd9Sstevel@tonic-gate 
43147c478bd9Sstevel@tonic-gate 	default:
43157c478bd9Sstevel@tonic-gate 		return (EINVAL);
43167c478bd9Sstevel@tonic-gate 	}
43177c478bd9Sstevel@tonic-gate 
43187c478bd9Sstevel@tonic-gate 	/* check the validity of the lock range */
43197c478bd9Sstevel@tonic-gate 	if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
43207c478bd9Sstevel@tonic-gate 		return (rc);
43217c478bd9Sstevel@tonic-gate 	if (rc = flk_check_lock_data(start, end, MAXOFF32_T))
43227c478bd9Sstevel@tonic-gate 		return (rc);
43237c478bd9Sstevel@tonic-gate 
43247c478bd9Sstevel@tonic-gate 	/*
43257c478bd9Sstevel@tonic-gate 	 * If the filesystem is mounted using local locking, pass the
43267c478bd9Sstevel@tonic-gate 	 * request off to the local locking code.
43277c478bd9Sstevel@tonic-gate 	 */
43287c478bd9Sstevel@tonic-gate 	if (VTOMI(vp)->mi_flags & MI_LLOCK) {
43297c478bd9Sstevel@tonic-gate 		if (offset > MAXOFF32_T)
43307c478bd9Sstevel@tonic-gate 			return (EFBIG);
43317c478bd9Sstevel@tonic-gate 		if (cmd == F_SETLK || cmd == F_SETLKW) {
43327c478bd9Sstevel@tonic-gate 			/*
43337c478bd9Sstevel@tonic-gate 			 * For complete safety, we should be holding
43347c478bd9Sstevel@tonic-gate 			 * r_lkserlock.  However, we can't call
43357c478bd9Sstevel@tonic-gate 			 * lm_safelock and then fs_frlock while
43367c478bd9Sstevel@tonic-gate 			 * holding r_lkserlock, so just invoke
43377c478bd9Sstevel@tonic-gate 			 * lm_safelock and expect that this will
43387c478bd9Sstevel@tonic-gate 			 * catch enough of the cases.
43397c478bd9Sstevel@tonic-gate 			 */
43407c478bd9Sstevel@tonic-gate 			if (!lm_safelock(vp, bfp, cr))
43417c478bd9Sstevel@tonic-gate 				return (EAGAIN);
43427c478bd9Sstevel@tonic-gate 		}
43437c478bd9Sstevel@tonic-gate 		return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr));
43447c478bd9Sstevel@tonic-gate 	}
43457c478bd9Sstevel@tonic-gate 
43467c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
43477c478bd9Sstevel@tonic-gate 
43487c478bd9Sstevel@tonic-gate 	/*
43497c478bd9Sstevel@tonic-gate 	 * Check whether the given lock request can proceed, given the
43507c478bd9Sstevel@tonic-gate 	 * current file mappings.
43517c478bd9Sstevel@tonic-gate 	 */
43527c478bd9Sstevel@tonic-gate 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
43537c478bd9Sstevel@tonic-gate 		return (EINTR);
43547c478bd9Sstevel@tonic-gate 	if (cmd == F_SETLK || cmd == F_SETLKW) {
43557c478bd9Sstevel@tonic-gate 		if (!lm_safelock(vp, bfp, cr)) {
43567c478bd9Sstevel@tonic-gate 			rc = EAGAIN;
43577c478bd9Sstevel@tonic-gate 			goto done;
43587c478bd9Sstevel@tonic-gate 		}
43597c478bd9Sstevel@tonic-gate 	}
43607c478bd9Sstevel@tonic-gate 
43617c478bd9Sstevel@tonic-gate 	/*
43627c478bd9Sstevel@tonic-gate 	 * Flush the cache after waiting for async I/O to finish.  For new
43637c478bd9Sstevel@tonic-gate 	 * locks, this is so that the process gets the latest bits from the
43647c478bd9Sstevel@tonic-gate 	 * server.  For unlocks, this is so that other clients see the
43657c478bd9Sstevel@tonic-gate 	 * latest bits once the file has been unlocked.  If currently dirty
43667c478bd9Sstevel@tonic-gate 	 * pages can't be flushed, then don't allow a lock to be set.  But
43677c478bd9Sstevel@tonic-gate 	 * allow unlocks to succeed, to avoid having orphan locks on the
43687c478bd9Sstevel@tonic-gate 	 * server.
43697c478bd9Sstevel@tonic-gate 	 */
43707c478bd9Sstevel@tonic-gate 	if (cmd != F_GETLK) {
43717c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
43727c478bd9Sstevel@tonic-gate 		while (rp->r_count > 0) {
43737c478bd9Sstevel@tonic-gate 		    if (intr) {
43747c478bd9Sstevel@tonic-gate 			klwp_t *lwp = ttolwp(curthread);
43757c478bd9Sstevel@tonic-gate 
43767c478bd9Sstevel@tonic-gate 			if (lwp != NULL)
43777c478bd9Sstevel@tonic-gate 				lwp->lwp_nostop++;
43787c478bd9Sstevel@tonic-gate 			if (cv_wait_sig(&rp->r_cv, &rp->r_statelock) == 0) {
43797c478bd9Sstevel@tonic-gate 				if (lwp != NULL)
43807c478bd9Sstevel@tonic-gate 					lwp->lwp_nostop--;
43817c478bd9Sstevel@tonic-gate 				rc = EINTR;
43827c478bd9Sstevel@tonic-gate 				break;
43837c478bd9Sstevel@tonic-gate 			}
43847c478bd9Sstevel@tonic-gate 			if (lwp != NULL)
43857c478bd9Sstevel@tonic-gate 				lwp->lwp_nostop--;
43867c478bd9Sstevel@tonic-gate 		    } else
43877c478bd9Sstevel@tonic-gate 			cv_wait(&rp->r_cv, &rp->r_statelock);
43887c478bd9Sstevel@tonic-gate 		}
43897c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
43907c478bd9Sstevel@tonic-gate 		if (rc != 0)
43917c478bd9Sstevel@tonic-gate 			goto done;
43927c478bd9Sstevel@tonic-gate 		error = nfs_putpage(vp, (offset_t)0, 0, B_INVAL, cr);
43937c478bd9Sstevel@tonic-gate 		if (error) {
43947c478bd9Sstevel@tonic-gate 			if (error == ENOSPC || error == EDQUOT) {
43957c478bd9Sstevel@tonic-gate 				mutex_enter(&rp->r_statelock);
43967c478bd9Sstevel@tonic-gate 				if (!rp->r_error)
43977c478bd9Sstevel@tonic-gate 					rp->r_error = error;
43987c478bd9Sstevel@tonic-gate 				mutex_exit(&rp->r_statelock);
43997c478bd9Sstevel@tonic-gate 			}
44007c478bd9Sstevel@tonic-gate 			if (bfp->l_type != F_UNLCK) {
44017c478bd9Sstevel@tonic-gate 				rc = ENOLCK;
44027c478bd9Sstevel@tonic-gate 				goto done;
44037c478bd9Sstevel@tonic-gate 			}
44047c478bd9Sstevel@tonic-gate 		}
44057c478bd9Sstevel@tonic-gate 	}
44067c478bd9Sstevel@tonic-gate 
44077c478bd9Sstevel@tonic-gate 	lm_fh.n_len = sizeof (fhandle_t);
44087c478bd9Sstevel@tonic-gate 	lm_fh.n_bytes = (char *)VTOFH(vp);
44097c478bd9Sstevel@tonic-gate 
44107c478bd9Sstevel@tonic-gate 	/*
44117c478bd9Sstevel@tonic-gate 	 * Call the lock manager to do the real work of contacting
44127c478bd9Sstevel@tonic-gate 	 * the server and obtaining the lock.
44137c478bd9Sstevel@tonic-gate 	 */
44147c478bd9Sstevel@tonic-gate 	rc = lm_frlock(vp, cmd, bfp, flag, offset, cr, &lm_fh, flk_cbp);
44157c478bd9Sstevel@tonic-gate 
44167c478bd9Sstevel@tonic-gate 	if (rc == 0)
44177c478bd9Sstevel@tonic-gate 		nfs_lockcompletion(vp, cmd);
44187c478bd9Sstevel@tonic-gate 
44197c478bd9Sstevel@tonic-gate done:
44207c478bd9Sstevel@tonic-gate 	nfs_rw_exit(&rp->r_lkserlock);
44217c478bd9Sstevel@tonic-gate 	return (rc);
44227c478bd9Sstevel@tonic-gate }
44237c478bd9Sstevel@tonic-gate 
44247c478bd9Sstevel@tonic-gate /*
44257c478bd9Sstevel@tonic-gate  * Free storage space associated with the specified vnode.  The portion
44267c478bd9Sstevel@tonic-gate  * to be freed is specified by bfp->l_start and bfp->l_len (already
44277c478bd9Sstevel@tonic-gate  * normalized to a "whence" of 0).
44287c478bd9Sstevel@tonic-gate  *
44297c478bd9Sstevel@tonic-gate  * This is an experimental facility whose continued existence is not
44307c478bd9Sstevel@tonic-gate  * guaranteed.  Currently, we only support the special case
44317c478bd9Sstevel@tonic-gate  * of l_len == 0, meaning free to end of file.
44327c478bd9Sstevel@tonic-gate  */
44337c478bd9Sstevel@tonic-gate /* ARGSUSED */
44347c478bd9Sstevel@tonic-gate static int
44357c478bd9Sstevel@tonic-gate nfs_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
44367c478bd9Sstevel@tonic-gate 	offset_t offset, cred_t *cr, caller_context_t *ct)
44377c478bd9Sstevel@tonic-gate {
44387c478bd9Sstevel@tonic-gate 	int error;
44397c478bd9Sstevel@tonic-gate 
44407c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VREG);
44417c478bd9Sstevel@tonic-gate 	if (cmd != F_FREESP)
44427c478bd9Sstevel@tonic-gate 		return (EINVAL);
44437c478bd9Sstevel@tonic-gate 
44447c478bd9Sstevel@tonic-gate 	if (offset > MAXOFF32_T)
44457c478bd9Sstevel@tonic-gate 		return (EFBIG);
44467c478bd9Sstevel@tonic-gate 
44477c478bd9Sstevel@tonic-gate 	if ((bfp->l_start > MAXOFF32_T) || (bfp->l_end > MAXOFF32_T) ||
44487c478bd9Sstevel@tonic-gate 	    (bfp->l_len > MAXOFF32_T))
44497c478bd9Sstevel@tonic-gate 		return (EFBIG);
44507c478bd9Sstevel@tonic-gate 
4451108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
44527c478bd9Sstevel@tonic-gate 		return (EIO);
44537c478bd9Sstevel@tonic-gate 
44547c478bd9Sstevel@tonic-gate 	error = convoff(vp, bfp, 0, offset);
44557c478bd9Sstevel@tonic-gate 	if (!error) {
44567c478bd9Sstevel@tonic-gate 		ASSERT(bfp->l_start >= 0);
44577c478bd9Sstevel@tonic-gate 		if (bfp->l_len == 0) {
44587c478bd9Sstevel@tonic-gate 			struct vattr va;
44597c478bd9Sstevel@tonic-gate 
44607c478bd9Sstevel@tonic-gate 			/*
44617c478bd9Sstevel@tonic-gate 			 * ftruncate should not change the ctime and
44627c478bd9Sstevel@tonic-gate 			 * mtime if we truncate the file to its
44637c478bd9Sstevel@tonic-gate 			 * previous size.
44647c478bd9Sstevel@tonic-gate 			 */
44657c478bd9Sstevel@tonic-gate 			va.va_mask = AT_SIZE;
44667c478bd9Sstevel@tonic-gate 			error = nfsgetattr(vp, &va, cr);
44677c478bd9Sstevel@tonic-gate 			if (error || va.va_size == bfp->l_start)
44687c478bd9Sstevel@tonic-gate 				return (error);
44697c478bd9Sstevel@tonic-gate 			va.va_mask = AT_SIZE;
44707c478bd9Sstevel@tonic-gate 			va.va_size = bfp->l_start;
44717c478bd9Sstevel@tonic-gate 			error = nfssetattr(vp, &va, 0, cr);
44727c478bd9Sstevel@tonic-gate 		} else
44737c478bd9Sstevel@tonic-gate 			error = EINVAL;
44747c478bd9Sstevel@tonic-gate 	}
44757c478bd9Sstevel@tonic-gate 
44767c478bd9Sstevel@tonic-gate 	return (error);
44777c478bd9Sstevel@tonic-gate }
44787c478bd9Sstevel@tonic-gate 
44797c478bd9Sstevel@tonic-gate /* ARGSUSED */
44807c478bd9Sstevel@tonic-gate static int
44817c478bd9Sstevel@tonic-gate nfs_realvp(vnode_t *vp, vnode_t **vpp)
44827c478bd9Sstevel@tonic-gate {
44837c478bd9Sstevel@tonic-gate 
44847c478bd9Sstevel@tonic-gate 	return (EINVAL);
44857c478bd9Sstevel@tonic-gate }
44867c478bd9Sstevel@tonic-gate 
44877c478bd9Sstevel@tonic-gate /*
44887c478bd9Sstevel@tonic-gate  * Setup and add an address space callback to do the work of the delmap call.
44897c478bd9Sstevel@tonic-gate  * The callback will (and must be) deleted in the actual callback function.
44907c478bd9Sstevel@tonic-gate  *
44917c478bd9Sstevel@tonic-gate  * This is done in order to take care of the problem that we have with holding
44927c478bd9Sstevel@tonic-gate  * the address space's a_lock for a long period of time (e.g. if the NFS server
44937c478bd9Sstevel@tonic-gate  * is down).  Callbacks will be executed in the address space code while the
44947c478bd9Sstevel@tonic-gate  * a_lock is not held.	Holding the address space's a_lock causes things such
44957c478bd9Sstevel@tonic-gate  * as ps and fork to hang because they are trying to acquire this lock as well.
44967c478bd9Sstevel@tonic-gate  */
44977c478bd9Sstevel@tonic-gate /* ARGSUSED */
44987c478bd9Sstevel@tonic-gate static int
44997c478bd9Sstevel@tonic-gate nfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
45007c478bd9Sstevel@tonic-gate 	size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr)
45017c478bd9Sstevel@tonic-gate {
45027c478bd9Sstevel@tonic-gate 	int			caller_found;
45037c478bd9Sstevel@tonic-gate 	int			error;
45047c478bd9Sstevel@tonic-gate 	rnode_t			*rp;
45057c478bd9Sstevel@tonic-gate 	nfs_delmap_args_t	*dmapp;
45067c478bd9Sstevel@tonic-gate 	nfs_delmapcall_t	*delmap_call;
45077c478bd9Sstevel@tonic-gate 
45087c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
45097c478bd9Sstevel@tonic-gate 		return (ENOSYS);
45107c478bd9Sstevel@tonic-gate 	/*
45117c478bd9Sstevel@tonic-gate 	 * A process may not change zones if it has NFS pages mmap'ed
45127c478bd9Sstevel@tonic-gate 	 * in, so we can't legitimately get here from the wrong zone.
45137c478bd9Sstevel@tonic-gate 	 */
4514108322fbScarlsonj 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
45157c478bd9Sstevel@tonic-gate 
45167c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
45177c478bd9Sstevel@tonic-gate 
45187c478bd9Sstevel@tonic-gate 	/*
45197c478bd9Sstevel@tonic-gate 	 * The way that the address space of this process deletes its mapping
45207c478bd9Sstevel@tonic-gate 	 * of this file is via the following call chains:
45217c478bd9Sstevel@tonic-gate 	 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs_delmap()
45227c478bd9Sstevel@tonic-gate 	 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs_delmap()
45237c478bd9Sstevel@tonic-gate 	 *
45247c478bd9Sstevel@tonic-gate 	 * With the use of address space callbacks we are allowed to drop the
45257c478bd9Sstevel@tonic-gate 	 * address space lock, a_lock, while executing the NFS operations that
45267c478bd9Sstevel@tonic-gate 	 * need to go over the wire.  Returning EAGAIN to the caller of this
45277c478bd9Sstevel@tonic-gate 	 * function is what drives the execution of the callback that we add
45287c478bd9Sstevel@tonic-gate 	 * below.  The callback will be executed by the address space code
45297c478bd9Sstevel@tonic-gate 	 * after dropping the a_lock.  When the callback is finished, since
45307c478bd9Sstevel@tonic-gate 	 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
45317c478bd9Sstevel@tonic-gate 	 * is called again on the same segment to finish the rest of the work
45327c478bd9Sstevel@tonic-gate 	 * that needs to happen during unmapping.
45337c478bd9Sstevel@tonic-gate 	 *
45347c478bd9Sstevel@tonic-gate 	 * This action of calling back into the segment driver causes
45357c478bd9Sstevel@tonic-gate 	 * nfs_delmap() to get called again, but since the callback was
45367c478bd9Sstevel@tonic-gate 	 * already executed at this point, it already did the work and there
45377c478bd9Sstevel@tonic-gate 	 * is nothing left for us to do.
45387c478bd9Sstevel@tonic-gate 	 *
45397c478bd9Sstevel@tonic-gate 	 * To Summarize:
45407c478bd9Sstevel@tonic-gate 	 * - The first time nfs_delmap is called by the current thread is when
45417c478bd9Sstevel@tonic-gate 	 * we add the caller associated with this delmap to the delmap caller
45427c478bd9Sstevel@tonic-gate 	 * list, add the callback, and return EAGAIN.
45437c478bd9Sstevel@tonic-gate 	 * - The second time in this call chain when nfs_delmap is called we
45447c478bd9Sstevel@tonic-gate 	 * will find this caller in the delmap caller list and realize there
45457c478bd9Sstevel@tonic-gate 	 * is no more work to do thus removing this caller from the list and
45467c478bd9Sstevel@tonic-gate 	 * returning the error that was set in the callback execution.
45477c478bd9Sstevel@tonic-gate 	 */
45487c478bd9Sstevel@tonic-gate 	caller_found = nfs_find_and_delete_delmapcall(rp, &error);
45497c478bd9Sstevel@tonic-gate 	if (caller_found) {
45507c478bd9Sstevel@tonic-gate 		/*
45517c478bd9Sstevel@tonic-gate 		 * 'error' is from the actual delmap operations.  To avoid
45527c478bd9Sstevel@tonic-gate 		 * hangs, we need to handle the return of EAGAIN differently
45537c478bd9Sstevel@tonic-gate 		 * since this is what drives the callback execution.
45547c478bd9Sstevel@tonic-gate 		 * In this case, we don't want to return EAGAIN and do the
45557c478bd9Sstevel@tonic-gate 		 * callback execution because there are none to execute.
45567c478bd9Sstevel@tonic-gate 		 */
45577c478bd9Sstevel@tonic-gate 		if (error == EAGAIN)
45587c478bd9Sstevel@tonic-gate 			return (0);
45597c478bd9Sstevel@tonic-gate 		else
45607c478bd9Sstevel@tonic-gate 			return (error);
45617c478bd9Sstevel@tonic-gate 	}
45627c478bd9Sstevel@tonic-gate 
45637c478bd9Sstevel@tonic-gate 	/* current caller was not in the list */
45647c478bd9Sstevel@tonic-gate 	delmap_call = nfs_init_delmapcall();
45657c478bd9Sstevel@tonic-gate 
45667c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
45677c478bd9Sstevel@tonic-gate 	list_insert_tail(&rp->r_indelmap, delmap_call);
45687c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
45697c478bd9Sstevel@tonic-gate 
45707c478bd9Sstevel@tonic-gate 	dmapp = kmem_alloc(sizeof (nfs_delmap_args_t), KM_SLEEP);
45717c478bd9Sstevel@tonic-gate 
45727c478bd9Sstevel@tonic-gate 	dmapp->vp = vp;
45737c478bd9Sstevel@tonic-gate 	dmapp->off = off;
45747c478bd9Sstevel@tonic-gate 	dmapp->addr = addr;
45757c478bd9Sstevel@tonic-gate 	dmapp->len = len;
45767c478bd9Sstevel@tonic-gate 	dmapp->prot = prot;
45777c478bd9Sstevel@tonic-gate 	dmapp->maxprot = maxprot;
45787c478bd9Sstevel@tonic-gate 	dmapp->flags = flags;
45797c478bd9Sstevel@tonic-gate 	dmapp->cr = cr;
45807c478bd9Sstevel@tonic-gate 	dmapp->caller = delmap_call;
45817c478bd9Sstevel@tonic-gate 
45827c478bd9Sstevel@tonic-gate 	error = as_add_callback(as, nfs_delmap_callback, dmapp,
45837c478bd9Sstevel@tonic-gate 	    AS_UNMAP_EVENT, addr, len, KM_SLEEP);
45847c478bd9Sstevel@tonic-gate 
45857c478bd9Sstevel@tonic-gate 	return (error ? error : EAGAIN);
45867c478bd9Sstevel@tonic-gate }
45877c478bd9Sstevel@tonic-gate 
45887c478bd9Sstevel@tonic-gate /*
45897c478bd9Sstevel@tonic-gate  * Remove some pages from an mmap'd vnode.  Just update the
45907c478bd9Sstevel@tonic-gate  * count of pages.  If doing close-to-open, then flush all
45917c478bd9Sstevel@tonic-gate  * of the pages associated with this file.  Otherwise, start
45927c478bd9Sstevel@tonic-gate  * an asynchronous page flush to write out any dirty pages.
45937c478bd9Sstevel@tonic-gate  * This will also associate a credential with the rnode which
45947c478bd9Sstevel@tonic-gate  * can be used to write the pages.
45957c478bd9Sstevel@tonic-gate  */
45967c478bd9Sstevel@tonic-gate /* ARGSUSED */
45977c478bd9Sstevel@tonic-gate static void
45987c478bd9Sstevel@tonic-gate nfs_delmap_callback(struct as *as, void *arg, uint_t event)
45997c478bd9Sstevel@tonic-gate {
46007c478bd9Sstevel@tonic-gate 	int			error;
46017c478bd9Sstevel@tonic-gate 	rnode_t			*rp;
46027c478bd9Sstevel@tonic-gate 	mntinfo_t		*mi;
46037c478bd9Sstevel@tonic-gate 	nfs_delmap_args_t	*dmapp = (nfs_delmap_args_t *)arg;
46047c478bd9Sstevel@tonic-gate 
46057c478bd9Sstevel@tonic-gate 	rp = VTOR(dmapp->vp);
46067c478bd9Sstevel@tonic-gate 	mi = VTOMI(dmapp->vp);
46077c478bd9Sstevel@tonic-gate 
46087c478bd9Sstevel@tonic-gate 	atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
46097c478bd9Sstevel@tonic-gate 	ASSERT(rp->r_mapcnt >= 0);
46107c478bd9Sstevel@tonic-gate 
46117c478bd9Sstevel@tonic-gate 	/*
46127c478bd9Sstevel@tonic-gate 	 * Initiate a page flush if there are pages, the file system
46137c478bd9Sstevel@tonic-gate 	 * was not mounted readonly, the segment was mapped shared, and
46147c478bd9Sstevel@tonic-gate 	 * the pages themselves were writeable.
46157c478bd9Sstevel@tonic-gate 	 */
46167c478bd9Sstevel@tonic-gate 	if (vn_has_cached_data(dmapp->vp) && !vn_is_readonly(dmapp->vp) &&
46177c478bd9Sstevel@tonic-gate 	    dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
46187c478bd9Sstevel@tonic-gate 		mutex_enter(&rp->r_statelock);
46197c478bd9Sstevel@tonic-gate 		rp->r_flags |= RDIRTY;
46207c478bd9Sstevel@tonic-gate 		mutex_exit(&rp->r_statelock);
46217c478bd9Sstevel@tonic-gate 		/*
46227c478bd9Sstevel@tonic-gate 		 * If this is a cross-zone access a sync putpage won't work, so
46237c478bd9Sstevel@tonic-gate 		 * the best we can do is try an async putpage.  That seems
46247c478bd9Sstevel@tonic-gate 		 * better than something more draconian such as discarding the
46257c478bd9Sstevel@tonic-gate 		 * dirty pages.
46267c478bd9Sstevel@tonic-gate 		 */
46277c478bd9Sstevel@tonic-gate 		if ((mi->mi_flags & MI_NOCTO) ||
4628108322fbScarlsonj 		    nfs_zone() != mi->mi_zone)
46297c478bd9Sstevel@tonic-gate 			error = nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
46307c478bd9Sstevel@tonic-gate 			    B_ASYNC, dmapp->cr);
46317c478bd9Sstevel@tonic-gate 		else
46327c478bd9Sstevel@tonic-gate 			error = nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
46337c478bd9Sstevel@tonic-gate 			    0, dmapp->cr);
46347c478bd9Sstevel@tonic-gate 		if (!error) {
46357c478bd9Sstevel@tonic-gate 			mutex_enter(&rp->r_statelock);
46367c478bd9Sstevel@tonic-gate 			error = rp->r_error;
46377c478bd9Sstevel@tonic-gate 			rp->r_error = 0;
46387c478bd9Sstevel@tonic-gate 			mutex_exit(&rp->r_statelock);
46397c478bd9Sstevel@tonic-gate 		}
46407c478bd9Sstevel@tonic-gate 	} else
46417c478bd9Sstevel@tonic-gate 		error = 0;
46427c478bd9Sstevel@tonic-gate 
46437c478bd9Sstevel@tonic-gate 	if ((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO))
46447c478bd9Sstevel@tonic-gate 		(void) nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
46457c478bd9Sstevel@tonic-gate 		    B_INVAL, dmapp->cr);
46467c478bd9Sstevel@tonic-gate 
46477c478bd9Sstevel@tonic-gate 	dmapp->caller->error = error;
46487c478bd9Sstevel@tonic-gate 	(void) as_delete_callback(as, arg);
46497c478bd9Sstevel@tonic-gate 	kmem_free(dmapp, sizeof (nfs_delmap_args_t));
46507c478bd9Sstevel@tonic-gate }
46517c478bd9Sstevel@tonic-gate 
46527c478bd9Sstevel@tonic-gate /* ARGSUSED */
46537c478bd9Sstevel@tonic-gate static int
46547c478bd9Sstevel@tonic-gate nfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
46557c478bd9Sstevel@tonic-gate {
46567c478bd9Sstevel@tonic-gate 	int error = 0;
46577c478bd9Sstevel@tonic-gate 
4658108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
46597c478bd9Sstevel@tonic-gate 		return (EIO);
46607c478bd9Sstevel@tonic-gate 	/*
46617c478bd9Sstevel@tonic-gate 	 * This looks a little weird because it's written in a general
46627c478bd9Sstevel@tonic-gate 	 * manner but we make little use of cases.  If cntl() ever gets
46637c478bd9Sstevel@tonic-gate 	 * widely used, the outer switch will make more sense.
46647c478bd9Sstevel@tonic-gate 	 */
46657c478bd9Sstevel@tonic-gate 
46667c478bd9Sstevel@tonic-gate 	switch (cmd) {
46677c478bd9Sstevel@tonic-gate 
46687c478bd9Sstevel@tonic-gate 	/*
46697c478bd9Sstevel@tonic-gate 	 * Large file spec - need to base answer new query with
46707c478bd9Sstevel@tonic-gate 	 * hardcoded constant based on the protocol.
46717c478bd9Sstevel@tonic-gate 	 */
46727c478bd9Sstevel@tonic-gate 	case _PC_FILESIZEBITS:
46737c478bd9Sstevel@tonic-gate 		*valp = 32;
46747c478bd9Sstevel@tonic-gate 		return (0);
46757c478bd9Sstevel@tonic-gate 
46767c478bd9Sstevel@tonic-gate 	case _PC_LINK_MAX:
46777c478bd9Sstevel@tonic-gate 	case _PC_NAME_MAX:
46787c478bd9Sstevel@tonic-gate 	case _PC_PATH_MAX:
46797c478bd9Sstevel@tonic-gate 	case _PC_SYMLINK_MAX:
46807c478bd9Sstevel@tonic-gate 	case _PC_CHOWN_RESTRICTED:
46817c478bd9Sstevel@tonic-gate 	case _PC_NO_TRUNC: {
46827c478bd9Sstevel@tonic-gate 		mntinfo_t *mi;
46837c478bd9Sstevel@tonic-gate 		struct pathcnf *pc;
46847c478bd9Sstevel@tonic-gate 
46857c478bd9Sstevel@tonic-gate 		if ((mi = VTOMI(vp)) == NULL || (pc = mi->mi_pathconf) == NULL)
46867c478bd9Sstevel@tonic-gate 			return (EINVAL);
46877c478bd9Sstevel@tonic-gate 		error = _PC_ISSET(cmd, pc->pc_mask);    /* error or bool */
46887c478bd9Sstevel@tonic-gate 		switch (cmd) {
46897c478bd9Sstevel@tonic-gate 		case _PC_LINK_MAX:
46907c478bd9Sstevel@tonic-gate 			*valp = pc->pc_link_max;
46917c478bd9Sstevel@tonic-gate 			break;
46927c478bd9Sstevel@tonic-gate 		case _PC_NAME_MAX:
46937c478bd9Sstevel@tonic-gate 			*valp = pc->pc_name_max;
46947c478bd9Sstevel@tonic-gate 			break;
46957c478bd9Sstevel@tonic-gate 		case _PC_PATH_MAX:
46967c478bd9Sstevel@tonic-gate 		case _PC_SYMLINK_MAX:
46977c478bd9Sstevel@tonic-gate 			*valp = pc->pc_path_max;
46987c478bd9Sstevel@tonic-gate 			break;
46997c478bd9Sstevel@tonic-gate 		case _PC_CHOWN_RESTRICTED:
47007c478bd9Sstevel@tonic-gate 			/*
47017c478bd9Sstevel@tonic-gate 			 * if we got here, error is really a boolean which
47027c478bd9Sstevel@tonic-gate 			 * indicates whether cmd is set or not.
47037c478bd9Sstevel@tonic-gate 			 */
47047c478bd9Sstevel@tonic-gate 			*valp = error ? 1 : 0;	/* see above */
47057c478bd9Sstevel@tonic-gate 			error = 0;
47067c478bd9Sstevel@tonic-gate 			break;
47077c478bd9Sstevel@tonic-gate 		case _PC_NO_TRUNC:
47087c478bd9Sstevel@tonic-gate 			/*
47097c478bd9Sstevel@tonic-gate 			 * if we got here, error is really a boolean which
47107c478bd9Sstevel@tonic-gate 			 * indicates whether cmd is set or not.
47117c478bd9Sstevel@tonic-gate 			 */
47127c478bd9Sstevel@tonic-gate 			*valp = error ? 1 : 0;	/* see above */
47137c478bd9Sstevel@tonic-gate 			error = 0;
47147c478bd9Sstevel@tonic-gate 			break;
47157c478bd9Sstevel@tonic-gate 		}
47167c478bd9Sstevel@tonic-gate 		return (error ? EINVAL : 0);
47177c478bd9Sstevel@tonic-gate 	    }
47187c478bd9Sstevel@tonic-gate 
47197c478bd9Sstevel@tonic-gate 	case _PC_XATTR_EXISTS:
47207c478bd9Sstevel@tonic-gate 		*valp = 0;
47217c478bd9Sstevel@tonic-gate 		if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
47227c478bd9Sstevel@tonic-gate 			vnode_t *avp;
47237c478bd9Sstevel@tonic-gate 			rnode_t *rp;
47247c478bd9Sstevel@tonic-gate 			mntinfo_t *mi = VTOMI(vp);
47257c478bd9Sstevel@tonic-gate 
47267c478bd9Sstevel@tonic-gate 			if (!(mi->mi_flags & MI_EXTATTR))
47277c478bd9Sstevel@tonic-gate 				return (0);
47287c478bd9Sstevel@tonic-gate 
47297c478bd9Sstevel@tonic-gate 			rp = VTOR(vp);
47307c478bd9Sstevel@tonic-gate 			if (nfs_rw_enter_sig(&rp->r_rwlock, RW_READER,
47317c478bd9Sstevel@tonic-gate 			    INTR(vp)))
47327c478bd9Sstevel@tonic-gate 				return (EINTR);
47337c478bd9Sstevel@tonic-gate 
47347c478bd9Sstevel@tonic-gate 			error = nfslookup_dnlc(vp, XATTR_DIR_NAME, &avp, cr);
47357c478bd9Sstevel@tonic-gate 			if (error || avp == NULL)
47367c478bd9Sstevel@tonic-gate 				error = acl_getxattrdir2(vp, &avp, 0, cr, 0);
47377c478bd9Sstevel@tonic-gate 
47387c478bd9Sstevel@tonic-gate 			nfs_rw_exit(&rp->r_rwlock);
47397c478bd9Sstevel@tonic-gate 
47407c478bd9Sstevel@tonic-gate 			if (error == 0 && avp != NULL) {
47417c478bd9Sstevel@tonic-gate 				VN_RELE(avp);
47427c478bd9Sstevel@tonic-gate 				*valp = 1;
47437c478bd9Sstevel@tonic-gate 			}
47447c478bd9Sstevel@tonic-gate 		}
47457c478bd9Sstevel@tonic-gate 		return (error ? EINVAL : 0);
47467c478bd9Sstevel@tonic-gate 
47477c478bd9Sstevel@tonic-gate 	case _PC_ACL_ENABLED:
47487c478bd9Sstevel@tonic-gate 		*valp = _ACL_ACLENT_ENABLED;
47497c478bd9Sstevel@tonic-gate 		return (0);
47507c478bd9Sstevel@tonic-gate 
47517c478bd9Sstevel@tonic-gate 	default:
47527c478bd9Sstevel@tonic-gate 		return (EINVAL);
47537c478bd9Sstevel@tonic-gate 	}
47547c478bd9Sstevel@tonic-gate }
47557c478bd9Sstevel@tonic-gate 
47567c478bd9Sstevel@tonic-gate /*
47577c478bd9Sstevel@tonic-gate  * Called by async thread to do synchronous pageio. Do the i/o, wait
47587c478bd9Sstevel@tonic-gate  * for it to complete, and cleanup the page list when done.
47597c478bd9Sstevel@tonic-gate  */
47607c478bd9Sstevel@tonic-gate static int
47617c478bd9Sstevel@tonic-gate nfs_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
47627c478bd9Sstevel@tonic-gate 	int flags, cred_t *cr)
47637c478bd9Sstevel@tonic-gate {
47647c478bd9Sstevel@tonic-gate 	int error;
47657c478bd9Sstevel@tonic-gate 
4766108322fbScarlsonj 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
47677c478bd9Sstevel@tonic-gate 	error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
47687c478bd9Sstevel@tonic-gate 	if (flags & B_READ)
47697c478bd9Sstevel@tonic-gate 		pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
47707c478bd9Sstevel@tonic-gate 	else
47717c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
47727c478bd9Sstevel@tonic-gate 	return (error);
47737c478bd9Sstevel@tonic-gate }
47747c478bd9Sstevel@tonic-gate 
47757c478bd9Sstevel@tonic-gate static int
47767c478bd9Sstevel@tonic-gate nfs_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
47777c478bd9Sstevel@tonic-gate 	int flags, cred_t *cr)
47787c478bd9Sstevel@tonic-gate {
47797c478bd9Sstevel@tonic-gate 	int error;
47807c478bd9Sstevel@tonic-gate 	rnode_t *rp;
47817c478bd9Sstevel@tonic-gate 
47827c478bd9Sstevel@tonic-gate 	if (pp == NULL)
47837c478bd9Sstevel@tonic-gate 		return (EINVAL);
47847c478bd9Sstevel@tonic-gate 
47857c478bd9Sstevel@tonic-gate 	if (io_off > MAXOFF32_T)
47867c478bd9Sstevel@tonic-gate 		return (EFBIG);
4787108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
47887c478bd9Sstevel@tonic-gate 		return (EIO);
47897c478bd9Sstevel@tonic-gate 	rp = VTOR(vp);
47907c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
47917c478bd9Sstevel@tonic-gate 	rp->r_count++;
47927c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
47937c478bd9Sstevel@tonic-gate 
47947c478bd9Sstevel@tonic-gate 	if (flags & B_ASYNC) {
47957c478bd9Sstevel@tonic-gate 		error = nfs_async_pageio(vp, pp, io_off, io_len, flags, cr,
47967c478bd9Sstevel@tonic-gate 		    nfs_sync_pageio);
47977c478bd9Sstevel@tonic-gate 	} else
47987c478bd9Sstevel@tonic-gate 		error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
47997c478bd9Sstevel@tonic-gate 	mutex_enter(&rp->r_statelock);
48007c478bd9Sstevel@tonic-gate 	rp->r_count--;
48017c478bd9Sstevel@tonic-gate 	cv_broadcast(&rp->r_cv);
48027c478bd9Sstevel@tonic-gate 	mutex_exit(&rp->r_statelock);
48037c478bd9Sstevel@tonic-gate 	return (error);
48047c478bd9Sstevel@tonic-gate }
48057c478bd9Sstevel@tonic-gate 
48067c478bd9Sstevel@tonic-gate static int
48077c478bd9Sstevel@tonic-gate nfs_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr)
48087c478bd9Sstevel@tonic-gate {
48097c478bd9Sstevel@tonic-gate 	int error;
48107c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
48117c478bd9Sstevel@tonic-gate 
48127c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
48137c478bd9Sstevel@tonic-gate 
4814108322fbScarlsonj 	if (nfs_zone() != mi->mi_zone)
48157c478bd9Sstevel@tonic-gate 		return (EIO);
48167c478bd9Sstevel@tonic-gate 	if (mi->mi_flags & MI_ACL) {
48177c478bd9Sstevel@tonic-gate 		error = acl_setacl2(vp, vsecattr, flag, cr);
48187c478bd9Sstevel@tonic-gate 		if (mi->mi_flags & MI_ACL)
48197c478bd9Sstevel@tonic-gate 			return (error);
48207c478bd9Sstevel@tonic-gate 	}
48217c478bd9Sstevel@tonic-gate 
48227c478bd9Sstevel@tonic-gate 	return (ENOSYS);
48237c478bd9Sstevel@tonic-gate }
48247c478bd9Sstevel@tonic-gate 
48257c478bd9Sstevel@tonic-gate static int
48267c478bd9Sstevel@tonic-gate nfs_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr)
48277c478bd9Sstevel@tonic-gate {
48287c478bd9Sstevel@tonic-gate 	int error;
48297c478bd9Sstevel@tonic-gate 	mntinfo_t *mi;
48307c478bd9Sstevel@tonic-gate 
48317c478bd9Sstevel@tonic-gate 	mi = VTOMI(vp);
48327c478bd9Sstevel@tonic-gate 
4833108322fbScarlsonj 	if (nfs_zone() != mi->mi_zone)
48347c478bd9Sstevel@tonic-gate 		return (EIO);
48357c478bd9Sstevel@tonic-gate 	if (mi->mi_flags & MI_ACL) {
48367c478bd9Sstevel@tonic-gate 		error = acl_getacl2(vp, vsecattr, flag, cr);
48377c478bd9Sstevel@tonic-gate 		if (mi->mi_flags & MI_ACL)
48387c478bd9Sstevel@tonic-gate 			return (error);
48397c478bd9Sstevel@tonic-gate 	}
48407c478bd9Sstevel@tonic-gate 
48417c478bd9Sstevel@tonic-gate 	return (fs_fab_acl(vp, vsecattr, flag, cr));
48427c478bd9Sstevel@tonic-gate }
48437c478bd9Sstevel@tonic-gate 
48447c478bd9Sstevel@tonic-gate static int
48457c478bd9Sstevel@tonic-gate nfs_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr)
48467c478bd9Sstevel@tonic-gate {
48477c478bd9Sstevel@tonic-gate 	int error;
48487c478bd9Sstevel@tonic-gate 	struct shrlock nshr;
48497c478bd9Sstevel@tonic-gate 	struct nfs_owner nfs_owner;
48507c478bd9Sstevel@tonic-gate 	netobj lm_fh;
48517c478bd9Sstevel@tonic-gate 
4852108322fbScarlsonj 	if (nfs_zone() != VTOMI(vp)->mi_zone)
48537c478bd9Sstevel@tonic-gate 		return (EIO);
48547c478bd9Sstevel@tonic-gate 
48557c478bd9Sstevel@tonic-gate 	/*
48567c478bd9Sstevel@tonic-gate 	 * check for valid cmd parameter
48577c478bd9Sstevel@tonic-gate 	 */
48587c478bd9Sstevel@tonic-gate 	if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
48597c478bd9Sstevel@tonic-gate 		return (EINVAL);
48607c478bd9Sstevel@tonic-gate 
48617c478bd9Sstevel@tonic-gate 	/*
48627c478bd9Sstevel@tonic-gate 	 * Check access permissions
48637c478bd9Sstevel@tonic-gate 	 */
48647c478bd9Sstevel@tonic-gate 	if (cmd == F_SHARE &&
48657c478bd9Sstevel@tonic-gate 	    (((shr->s_access & F_RDACC) && !(flag & FREAD)) ||
48667c478bd9Sstevel@tonic-gate 	    ((shr->s_access & F_WRACC) && !(flag & FWRITE))))
48677c478bd9Sstevel@tonic-gate 		return (EBADF);
48687c478bd9Sstevel@tonic-gate 
48697c478bd9Sstevel@tonic-gate 	/*
48707c478bd9Sstevel@tonic-gate 	 * If the filesystem is mounted using local locking, pass the
48717c478bd9Sstevel@tonic-gate 	 * request off to the local share code.
48727c478bd9Sstevel@tonic-gate 	 */
48737c478bd9Sstevel@tonic-gate 	if (VTOMI(vp)->mi_flags & MI_LLOCK)
48747c478bd9Sstevel@tonic-gate 		return (fs_shrlock(vp, cmd, shr, flag, cr));
48757c478bd9Sstevel@tonic-gate 
48767c478bd9Sstevel@tonic-gate 	switch (cmd) {
48777c478bd9Sstevel@tonic-gate 	case F_SHARE:
48787c478bd9Sstevel@tonic-gate 	case F_UNSHARE:
48797c478bd9Sstevel@tonic-gate 		lm_fh.n_len = sizeof (fhandle_t);
48807c478bd9Sstevel@tonic-gate 		lm_fh.n_bytes = (char *)VTOFH(vp);
48817c478bd9Sstevel@tonic-gate 
48827c478bd9Sstevel@tonic-gate 		/*
48837c478bd9Sstevel@tonic-gate 		 * If passed an owner that is too large to fit in an
48847c478bd9Sstevel@tonic-gate 		 * nfs_owner it is likely a recursive call from the
48857c478bd9Sstevel@tonic-gate 		 * lock manager client and pass it straight through.  If
48867c478bd9Sstevel@tonic-gate 		 * it is not a nfs_owner then simply return an error.
48877c478bd9Sstevel@tonic-gate 		 */
48887c478bd9Sstevel@tonic-gate 		if (shr->s_own_len > sizeof (nfs_owner.lowner)) {
48897c478bd9Sstevel@tonic-gate 			if (((struct nfs_owner *)shr->s_owner)->magic !=
48907c478bd9Sstevel@tonic-gate 			    NFS_OWNER_MAGIC)
48917c478bd9Sstevel@tonic-gate 				return (EINVAL);
48927c478bd9Sstevel@tonic-gate 
48937c478bd9Sstevel@tonic-gate 			if (error = lm_shrlock(vp, cmd, shr, flag, &lm_fh)) {
48947c478bd9Sstevel@tonic-gate 				error = set_errno(error);
48957c478bd9Sstevel@tonic-gate 			}
48967c478bd9Sstevel@tonic-gate 			return (error);
48977c478bd9Sstevel@tonic-gate 		}
48987c478bd9Sstevel@tonic-gate 		/*
48997c478bd9Sstevel@tonic-gate 		 * Remote share reservations owner is a combination of
49007c478bd9Sstevel@tonic-gate 		 * a magic number, hostname, and the local owner
49017c478bd9Sstevel@tonic-gate 		 */
49027c478bd9Sstevel@tonic-gate 		bzero(&nfs_owner, sizeof (nfs_owner));
49037c478bd9Sstevel@tonic-gate 		nfs_owner.magic = NFS_OWNER_MAGIC;
49047c478bd9Sstevel@tonic-gate 		(void) strncpy(nfs_owner.hname, uts_nodename(),
49057c478bd9Sstevel@tonic-gate 		    sizeof (nfs_owner.hname));
49067c478bd9Sstevel@tonic-gate 		bcopy(shr->s_owner, nfs_owner.lowner, shr->s_own_len);
49077c478bd9Sstevel@tonic-gate 		nshr.s_access = shr->s_access;
49087c478bd9Sstevel@tonic-gate 		nshr.s_deny = shr->s_deny;
49097c478bd9Sstevel@tonic-gate 		nshr.s_sysid = 0;
49107c478bd9Sstevel@tonic-gate 		nshr.s_pid = ttoproc(curthread)->p_pid;
49117c478bd9Sstevel@tonic-gate 		nshr.s_own_len = sizeof (nfs_owner);
49127c478bd9Sstevel@tonic-gate 		nshr.s_owner = (caddr_t)&nfs_owner;
49137c478bd9Sstevel@tonic-gate 
49147c478bd9Sstevel@tonic-gate 		if (error = lm_shrlock(vp, cmd, &nshr, flag, &lm_fh)) {
49157c478bd9Sstevel@tonic-gate 			error = set_errno(error);
49167c478bd9Sstevel@tonic-gate 		}
49177c478bd9Sstevel@tonic-gate 
49187c478bd9Sstevel@tonic-gate 		break;
49197c478bd9Sstevel@tonic-gate 
49207c478bd9Sstevel@tonic-gate 	case F_HASREMOTELOCKS:
49217c478bd9Sstevel@tonic-gate 		/*
49227c478bd9Sstevel@tonic-gate 		 * NFS client can't store remote locks itself
49237c478bd9Sstevel@tonic-gate 		 */
49247c478bd9Sstevel@tonic-gate 		shr->s_access = 0;
49257c478bd9Sstevel@tonic-gate 		error = 0;
49267c478bd9Sstevel@tonic-gate 		break;
49277c478bd9Sstevel@tonic-gate 
49287c478bd9Sstevel@tonic-gate 	default:
49297c478bd9Sstevel@tonic-gate 		error = EINVAL;
49307c478bd9Sstevel@tonic-gate 		break;
49317c478bd9Sstevel@tonic-gate 	}
49327c478bd9Sstevel@tonic-gate 
49337c478bd9Sstevel@tonic-gate 	return (error);
49347c478bd9Sstevel@tonic-gate }
4935