xref: /illumos-gate/usr/src/uts/common/fs/nfs/nfs_vnops.c (revision 968633ad8faee931821fd6b656eb0d96d4b186c0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
26  *	All rights reserved.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <sys/cred.h>
33 #include <sys/time.h>
34 #include <sys/vnode.h>
35 #include <sys/vfs.h>
36 #include <sys/vfs_opreg.h>
37 #include <sys/file.h>
38 #include <sys/filio.h>
39 #include <sys/uio.h>
40 #include <sys/buf.h>
41 #include <sys/mman.h>
42 #include <sys/pathname.h>
43 #include <sys/dirent.h>
44 #include <sys/debug.h>
45 #include <sys/vmsystm.h>
46 #include <sys/fcntl.h>
47 #include <sys/flock.h>
48 #include <sys/swap.h>
49 #include <sys/errno.h>
50 #include <sys/strsubr.h>
51 #include <sys/sysmacros.h>
52 #include <sys/kmem.h>
53 #include <sys/cmn_err.h>
54 #include <sys/pathconf.h>
55 #include <sys/utsname.h>
56 #include <sys/dnlc.h>
57 #include <sys/acl.h>
58 #include <sys/atomic.h>
59 #include <sys/policy.h>
60 #include <sys/sdt.h>
61 
62 #include <rpc/types.h>
63 #include <rpc/auth.h>
64 #include <rpc/clnt.h>
65 
66 #include <nfs/nfs.h>
67 #include <nfs/nfs_clnt.h>
68 #include <nfs/rnode.h>
69 #include <nfs/nfs_acl.h>
70 #include <nfs/lm.h>
71 
72 #include <vm/hat.h>
73 #include <vm/as.h>
74 #include <vm/page.h>
75 #include <vm/pvn.h>
76 #include <vm/seg.h>
77 #include <vm/seg_map.h>
78 #include <vm/seg_kpm.h>
79 #include <vm/seg_vn.h>
80 
81 #include <fs/fs_subr.h>
82 
83 #include <sys/ddi.h>
84 
85 static int	nfs_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
86 			cred_t *);
87 static int	nfswrite(vnode_t *, caddr_t, uint_t, int, cred_t *);
88 static int	nfsread(vnode_t *, caddr_t, uint_t, int, size_t *, cred_t *);
89 static int	nfssetattr(vnode_t *, struct vattr *, int, cred_t *);
90 static int	nfslookup_dnlc(vnode_t *, char *, vnode_t **, cred_t *);
91 static int	nfslookup_otw(vnode_t *, char *, vnode_t **, cred_t *, int);
92 static int	nfsrename(vnode_t *, char *, vnode_t *, char *, cred_t *,
93 			caller_context_t *);
94 static int	nfsreaddir(vnode_t *, rddir_cache *, cred_t *);
95 static int	nfs_bio(struct buf *, cred_t *);
96 static int	nfs_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
97 			page_t *[], size_t, struct seg *, caddr_t,
98 			enum seg_rw, cred_t *);
99 static void	nfs_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
100 			cred_t *);
101 static int	nfs_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
102 			int, cred_t *);
103 static int	nfs_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
104 			int, cred_t *);
105 static void	nfs_delmap_callback(struct as *, void *, uint_t);
106 
107 /*
108  * Error flags used to pass information about certain special errors
109  * which need to be handled specially.
110  */
111 #define	NFS_EOF			-98
112 
113 /*
114  * These are the vnode ops routines which implement the vnode interface to
115  * the networked file system.  These routines just take their parameters,
116  * make them look networkish by putting the right info into interface structs,
117  * and then calling the appropriate remote routine(s) to do the work.
118  *
119  * Note on directory name lookup cacheing:  If we detect a stale fhandle,
120  * we purge the directory cache relative to that vnode.  This way, the
121  * user won't get burned by the cache repeatedly.  See <nfs/rnode.h> for
122  * more details on rnode locking.
123  */
124 
125 static int	nfs_open(vnode_t **, int, cred_t *, caller_context_t *);
126 static int	nfs_close(vnode_t *, int, int, offset_t, cred_t *,
127 			caller_context_t *);
128 static int	nfs_read(vnode_t *, struct uio *, int, cred_t *,
129 			caller_context_t *);
130 static int	nfs_write(vnode_t *, struct uio *, int, cred_t *,
131 			caller_context_t *);
132 static int	nfs_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
133 			caller_context_t *);
134 static int	nfs_getattr(vnode_t *, struct vattr *, int, cred_t *,
135 			caller_context_t *);
136 static int	nfs_setattr(vnode_t *, struct vattr *, int, cred_t *,
137 			caller_context_t *);
138 static int	nfs_access(vnode_t *, int, int, cred_t *, caller_context_t *);
139 static int	nfs_accessx(void *, int, cred_t *);
140 static int	nfs_readlink(vnode_t *, struct uio *, cred_t *,
141 			caller_context_t *);
142 static int	nfs_fsync(vnode_t *, int, cred_t *, caller_context_t *);
143 static void	nfs_inactive(vnode_t *, cred_t *, caller_context_t *);
144 static int	nfs_lookup(vnode_t *, char *, vnode_t **, struct pathname *,
145 			int, vnode_t *, cred_t *, caller_context_t *,
146 			int *, pathname_t *);
147 static int	nfs_create(vnode_t *, char *, struct vattr *, enum vcexcl,
148 			int, vnode_t **, cred_t *, int, caller_context_t *,
149 			vsecattr_t *);
150 static int	nfs_remove(vnode_t *, char *, cred_t *, caller_context_t *,
151 			int);
152 static int	nfs_link(vnode_t *, vnode_t *, char *, cred_t *,
153 			caller_context_t *, int);
154 static int	nfs_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
155 			caller_context_t *, int);
156 static int	nfs_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
157 			cred_t *, caller_context_t *, int, vsecattr_t *);
158 static int	nfs_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
159 			caller_context_t *, int);
160 static int	nfs_symlink(vnode_t *, char *, struct vattr *, char *,
161 			cred_t *, caller_context_t *, int);
162 static int	nfs_readdir(vnode_t *, struct uio *, cred_t *, int *,
163 			caller_context_t *, int);
164 static int	nfs_fid(vnode_t *, fid_t *, caller_context_t *);
165 static int	nfs_rwlock(vnode_t *, int, caller_context_t *);
166 static void	nfs_rwunlock(vnode_t *, int, caller_context_t *);
167 static int	nfs_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
168 static int	nfs_getpage(vnode_t *, offset_t, size_t, uint_t *,
169 			page_t *[], size_t, struct seg *, caddr_t,
170 			enum seg_rw, cred_t *, caller_context_t *);
171 static int	nfs_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
172 			caller_context_t *);
173 static int	nfs_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
174 			uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
175 static int	nfs_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
176 			uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
177 static int	nfs_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
178 			struct flk_callback *, cred_t *, caller_context_t *);
179 static int	nfs_space(vnode_t *, int, struct flock64 *, int, offset_t,
180 			cred_t *, caller_context_t *);
181 static int	nfs_realvp(vnode_t *, vnode_t **, caller_context_t *);
182 static int	nfs_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
183 			uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
184 static int	nfs_pathconf(vnode_t *, int, ulong_t *, cred_t *,
185 			caller_context_t *);
186 static int	nfs_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
187 			cred_t *, caller_context_t *);
188 static int	nfs_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
189 			caller_context_t *);
190 static int	nfs_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
191 			caller_context_t *);
192 static int	nfs_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
193 			caller_context_t *);
194 
195 struct vnodeops *nfs_vnodeops;
196 
197 const fs_operation_def_t nfs_vnodeops_template[] = {
198 	VOPNAME_OPEN,		{ .vop_open = nfs_open },
199 	VOPNAME_CLOSE,		{ .vop_close = nfs_close },
200 	VOPNAME_READ,		{ .vop_read = nfs_read },
201 	VOPNAME_WRITE,		{ .vop_write = nfs_write },
202 	VOPNAME_IOCTL,		{ .vop_ioctl = nfs_ioctl },
203 	VOPNAME_GETATTR,	{ .vop_getattr = nfs_getattr },
204 	VOPNAME_SETATTR,	{ .vop_setattr = nfs_setattr },
205 	VOPNAME_ACCESS,		{ .vop_access = nfs_access },
206 	VOPNAME_LOOKUP,		{ .vop_lookup = nfs_lookup },
207 	VOPNAME_CREATE,		{ .vop_create = nfs_create },
208 	VOPNAME_REMOVE,		{ .vop_remove = nfs_remove },
209 	VOPNAME_LINK,		{ .vop_link = nfs_link },
210 	VOPNAME_RENAME,		{ .vop_rename = nfs_rename },
211 	VOPNAME_MKDIR,		{ .vop_mkdir = nfs_mkdir },
212 	VOPNAME_RMDIR,		{ .vop_rmdir = nfs_rmdir },
213 	VOPNAME_READDIR,	{ .vop_readdir = nfs_readdir },
214 	VOPNAME_SYMLINK,	{ .vop_symlink = nfs_symlink },
215 	VOPNAME_READLINK,	{ .vop_readlink = nfs_readlink },
216 	VOPNAME_FSYNC,		{ .vop_fsync = nfs_fsync },
217 	VOPNAME_INACTIVE,	{ .vop_inactive = nfs_inactive },
218 	VOPNAME_FID,		{ .vop_fid = nfs_fid },
219 	VOPNAME_RWLOCK,		{ .vop_rwlock = nfs_rwlock },
220 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = nfs_rwunlock },
221 	VOPNAME_SEEK,		{ .vop_seek = nfs_seek },
222 	VOPNAME_FRLOCK,		{ .vop_frlock = nfs_frlock },
223 	VOPNAME_SPACE,		{ .vop_space = nfs_space },
224 	VOPNAME_REALVP,		{ .vop_realvp = nfs_realvp },
225 	VOPNAME_GETPAGE,	{ .vop_getpage = nfs_getpage },
226 	VOPNAME_PUTPAGE,	{ .vop_putpage = nfs_putpage },
227 	VOPNAME_MAP,		{ .vop_map = nfs_map },
228 	VOPNAME_ADDMAP,		{ .vop_addmap = nfs_addmap },
229 	VOPNAME_DELMAP,		{ .vop_delmap = nfs_delmap },
230 	VOPNAME_DUMP,		{ .vop_dump = nfs_dump },
231 	VOPNAME_PATHCONF,	{ .vop_pathconf = nfs_pathconf },
232 	VOPNAME_PAGEIO,		{ .vop_pageio = nfs_pageio },
233 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = nfs_setsecattr },
234 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = nfs_getsecattr },
235 	VOPNAME_SHRLOCK,	{ .vop_shrlock = nfs_shrlock },
236 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
237 	NULL,			NULL
238 };
239 
240 /*
241  * XXX:  This is referenced in modstubs.s
242  */
243 struct vnodeops *
244 nfs_getvnodeops(void)
245 {
246 	return (nfs_vnodeops);
247 }
248 
249 /* ARGSUSED */
250 static int
251 nfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
252 {
253 	int error;
254 	struct vattr va;
255 	rnode_t *rp;
256 	vnode_t *vp;
257 
258 	vp = *vpp;
259 	rp = VTOR(vp);
260 	if (nfs_zone() != VTOMI(vp)->mi_zone)
261 		return (EIO);
262 	mutex_enter(&rp->r_statelock);
263 	if (rp->r_cred == NULL) {
264 		crhold(cr);
265 		rp->r_cred = cr;
266 	}
267 	mutex_exit(&rp->r_statelock);
268 
269 	/*
270 	 * If there is no cached data or if close-to-open
271 	 * consistency checking is turned off, we can avoid
272 	 * the over the wire getattr.  Otherwise, if the
273 	 * file system is mounted readonly, then just verify
274 	 * the caches are up to date using the normal mechanism.
275 	 * Else, if the file is not mmap'd, then just mark
276 	 * the attributes as timed out.  They will be refreshed
277 	 * and the caches validated prior to being used.
278 	 * Else, the file system is mounted writeable so
279 	 * force an over the wire GETATTR in order to ensure
280 	 * that all cached data is valid.
281 	 */
282 	if (vp->v_count > 1 ||
283 	    ((vn_has_cached_data(vp) || HAVE_RDDIR_CACHE(rp)) &&
284 	    !(VTOMI(vp)->mi_flags & MI_NOCTO))) {
285 		if (vn_is_readonly(vp))
286 			error = nfs_validate_caches(vp, cr);
287 		else if (rp->r_mapcnt == 0 && vp->v_count == 1) {
288 			PURGE_ATTRCACHE(vp);
289 			error = 0;
290 		} else {
291 			va.va_mask = AT_ALL;
292 			error = nfs_getattr_otw(vp, &va, cr);
293 		}
294 	} else
295 		error = 0;
296 
297 	return (error);
298 }
299 
300 /* ARGSUSED */
301 static int
302 nfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
303 	caller_context_t *ct)
304 {
305 	rnode_t *rp;
306 	int error;
307 	struct vattr va;
308 
309 	/*
310 	 * zone_enter(2) prevents processes from changing zones with NFS files
311 	 * open; if we happen to get here from the wrong zone we can't do
312 	 * anything over the wire.
313 	 */
314 	if (VTOMI(vp)->mi_zone != nfs_zone()) {
315 		/*
316 		 * We could attempt to clean up locks, except we're sure
317 		 * that the current process didn't acquire any locks on
318 		 * the file: any attempt to lock a file belong to another zone
319 		 * will fail, and one can't lock an NFS file and then change
320 		 * zones, as that fails too.
321 		 *
322 		 * Returning an error here is the sane thing to do.  A
323 		 * subsequent call to VN_RELE() which translates to a
324 		 * nfs_inactive() will clean up state: if the zone of the
325 		 * vnode's origin is still alive and kicking, an async worker
326 		 * thread will handle the request (from the correct zone), and
327 		 * everything (minus the final nfs_getattr_otw() call) should
328 		 * be OK. If the zone is going away nfs_async_inactive() will
329 		 * throw away cached pages inline.
330 		 */
331 		return (EIO);
332 	}
333 
334 	/*
335 	 * If we are using local locking for this filesystem, then
336 	 * release all of the SYSV style record locks.  Otherwise,
337 	 * we are doing network locking and we need to release all
338 	 * of the network locks.  All of the locks held by this
339 	 * process on this file are released no matter what the
340 	 * incoming reference count is.
341 	 */
342 	if (VTOMI(vp)->mi_flags & MI_LLOCK) {
343 		cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
344 		cleanshares(vp, ttoproc(curthread)->p_pid);
345 	} else
346 		nfs_lockrelease(vp, flag, offset, cr);
347 
348 	if (count > 1)
349 		return (0);
350 
351 	/*
352 	 * If the file has been `unlinked', then purge the
353 	 * DNLC so that this vnode will get reycled quicker
354 	 * and the .nfs* file on the server will get removed.
355 	 */
356 	rp = VTOR(vp);
357 	if (rp->r_unldvp != NULL)
358 		dnlc_purge_vp(vp);
359 
360 	/*
361 	 * If the file was open for write and there are pages,
362 	 * then if the file system was mounted using the "no-close-
363 	 *	to-open" semantics, then start an asynchronous flush
364 	 *	of the all of the pages in the file.
365 	 * else the file system was not mounted using the "no-close-
366 	 *	to-open" semantics, then do a synchronous flush and
367 	 *	commit of all of the dirty and uncommitted pages.
368 	 *
369 	 * The asynchronous flush of the pages in the "nocto" path
370 	 * mostly just associates a cred pointer with the rnode so
371 	 * writes which happen later will have a better chance of
372 	 * working.  It also starts the data being written to the
373 	 * server, but without unnecessarily delaying the application.
374 	 */
375 	if ((flag & FWRITE) && vn_has_cached_data(vp)) {
376 		if ((VTOMI(vp)->mi_flags & MI_NOCTO)) {
377 			error = nfs_putpage(vp, (offset_t)0, 0, B_ASYNC,
378 			    cr, ct);
379 			if (error == EAGAIN)
380 				error = 0;
381 		} else
382 			error = nfs_putpage(vp, (offset_t)0, 0, 0, cr, ct);
383 		if (!error) {
384 			mutex_enter(&rp->r_statelock);
385 			error = rp->r_error;
386 			rp->r_error = 0;
387 			mutex_exit(&rp->r_statelock);
388 		}
389 	} else {
390 		mutex_enter(&rp->r_statelock);
391 		error = rp->r_error;
392 		rp->r_error = 0;
393 		mutex_exit(&rp->r_statelock);
394 	}
395 
396 	/*
397 	 * If RWRITEATTR is set, then issue an over the wire GETATTR to
398 	 * refresh the attribute cache with a set of attributes which
399 	 * weren't returned from a WRITE.  This will enable the close-
400 	 * to-open processing to work.
401 	 */
402 	if (rp->r_flags & RWRITEATTR)
403 		(void) nfs_getattr_otw(vp, &va, cr);
404 
405 	return (error);
406 }
407 
408 /* ARGSUSED */
409 static int
410 nfs_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
411 	caller_context_t *ct)
412 {
413 	rnode_t *rp;
414 	u_offset_t off;
415 	offset_t diff;
416 	int on;
417 	size_t n;
418 	caddr_t base;
419 	uint_t flags;
420 	int error;
421 	mntinfo_t *mi;
422 
423 	rp = VTOR(vp);
424 	mi = VTOMI(vp);
425 
426 	if (nfs_zone() != mi->mi_zone)
427 		return (EIO);
428 
429 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
430 
431 	if (vp->v_type != VREG)
432 		return (EISDIR);
433 
434 	if (uiop->uio_resid == 0)
435 		return (0);
436 
437 	if (uiop->uio_loffset > MAXOFF32_T)
438 		return (EFBIG);
439 
440 	if (uiop->uio_loffset < 0 ||
441 	    uiop->uio_loffset + uiop->uio_resid > MAXOFF32_T)
442 		return (EINVAL);
443 
444 	/*
445 	 * Bypass VM if caching has been disabled (e.g., locking) or if
446 	 * using client-side direct I/O and the file is not mmap'd and
447 	 * there are no cached pages.
448 	 */
449 	if ((vp->v_flag & VNOCACHE) ||
450 	    (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
451 	    rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
452 	    !vn_has_cached_data(vp))) {
453 		size_t bufsize;
454 		size_t resid = 0;
455 
456 		/*
457 		 * Let's try to do read in as large a chunk as we can
458 		 * (Filesystem (NFS client) bsize if possible/needed).
459 		 * For V3, this is 32K and for V2, this is 8K.
460 		 */
461 		bufsize = MIN(uiop->uio_resid, VTOMI(vp)->mi_curread);
462 		base = kmem_alloc(bufsize, KM_SLEEP);
463 		do {
464 			n = MIN(uiop->uio_resid, bufsize);
465 			error = nfsread(vp, base, uiop->uio_offset, n,
466 			    &resid, cr);
467 			if (!error) {
468 				n -= resid;
469 				error = uiomove(base, n, UIO_READ, uiop);
470 			}
471 		} while (!error && uiop->uio_resid > 0 && n > 0);
472 		kmem_free(base, bufsize);
473 		return (error);
474 	}
475 
476 	error = 0;
477 
478 	do {
479 		off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
480 		on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
481 		n = MIN(MAXBSIZE - on, uiop->uio_resid);
482 
483 		error = nfs_validate_caches(vp, cr);
484 		if (error)
485 			break;
486 
487 		mutex_enter(&rp->r_statelock);
488 		while (rp->r_flags & RINCACHEPURGE) {
489 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
490 				mutex_exit(&rp->r_statelock);
491 				return (EINTR);
492 			}
493 		}
494 		diff = rp->r_size - uiop->uio_loffset;
495 		mutex_exit(&rp->r_statelock);
496 		if (diff <= 0)
497 			break;
498 		if (diff < n)
499 			n = (size_t)diff;
500 
501 		if (vpm_enable) {
502 			/*
503 			 * Copy data.
504 			 */
505 			error = vpm_data_copy(vp, off + on, n, uiop,
506 			    1, NULL, 0, S_READ);
507 		} else {
508 			base = segmap_getmapflt(segkmap, vp, off + on, n,
509 			    1, S_READ);
510 			error = uiomove(base + on, n, UIO_READ, uiop);
511 		}
512 
513 		if (!error) {
514 			/*
515 			 * If read a whole block or read to eof,
516 			 * won't need this buffer again soon.
517 			 */
518 			mutex_enter(&rp->r_statelock);
519 			if (n + on == MAXBSIZE ||
520 			    uiop->uio_loffset == rp->r_size)
521 				flags = SM_DONTNEED;
522 			else
523 				flags = 0;
524 			mutex_exit(&rp->r_statelock);
525 			if (vpm_enable) {
526 				error = vpm_sync_pages(vp, off, n, flags);
527 			} else {
528 				error = segmap_release(segkmap, base, flags);
529 			}
530 		} else {
531 			if (vpm_enable) {
532 				(void) vpm_sync_pages(vp, off, n, 0);
533 			} else {
534 				(void) segmap_release(segkmap, base, 0);
535 			}
536 		}
537 	} while (!error && uiop->uio_resid > 0);
538 
539 	return (error);
540 }
541 
542 /* ARGSUSED */
543 static int
544 nfs_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
545 	caller_context_t *ct)
546 {
547 	rnode_t *rp;
548 	u_offset_t off;
549 	caddr_t base;
550 	uint_t flags;
551 	int remainder;
552 	size_t n;
553 	int on;
554 	int error;
555 	int resid;
556 	offset_t offset;
557 	rlim_t limit;
558 	mntinfo_t *mi;
559 
560 	rp = VTOR(vp);
561 
562 	mi = VTOMI(vp);
563 	if (nfs_zone() != mi->mi_zone)
564 		return (EIO);
565 	if (vp->v_type != VREG)
566 		return (EISDIR);
567 
568 	if (uiop->uio_resid == 0)
569 		return (0);
570 
571 	if (ioflag & FAPPEND) {
572 		struct vattr va;
573 
574 		/*
575 		 * Must serialize if appending.
576 		 */
577 		if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
578 			nfs_rw_exit(&rp->r_rwlock);
579 			if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
580 			    INTR(vp)))
581 				return (EINTR);
582 		}
583 
584 		va.va_mask = AT_SIZE;
585 		error = nfsgetattr(vp, &va, cr);
586 		if (error)
587 			return (error);
588 		uiop->uio_loffset = va.va_size;
589 	}
590 
591 	if (uiop->uio_loffset > MAXOFF32_T)
592 		return (EFBIG);
593 
594 	offset = uiop->uio_loffset + uiop->uio_resid;
595 
596 	if (uiop->uio_loffset < 0 || offset > MAXOFF32_T)
597 		return (EINVAL);
598 
599 	if (uiop->uio_llimit > (rlim64_t)MAXOFF32_T) {
600 		limit = MAXOFF32_T;
601 	} else {
602 		limit = (rlim_t)uiop->uio_llimit;
603 	}
604 
605 	/*
606 	 * Check to make sure that the process will not exceed
607 	 * its limit on file size.  It is okay to write up to
608 	 * the limit, but not beyond.  Thus, the write which
609 	 * reaches the limit will be short and the next write
610 	 * will return an error.
611 	 */
612 	remainder = 0;
613 	if (offset > limit) {
614 		remainder = offset - limit;
615 		uiop->uio_resid = limit - uiop->uio_offset;
616 		if (uiop->uio_resid <= 0) {
617 			proc_t *p = ttoproc(curthread);
618 
619 			uiop->uio_resid += remainder;
620 			mutex_enter(&p->p_lock);
621 			(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
622 			    p->p_rctls, p, RCA_UNSAFE_SIGINFO);
623 			mutex_exit(&p->p_lock);
624 			return (EFBIG);
625 		}
626 	}
627 
628 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp)))
629 		return (EINTR);
630 
631 	/*
632 	 * Bypass VM if caching has been disabled (e.g., locking) or if
633 	 * using client-side direct I/O and the file is not mmap'd and
634 	 * there are no cached pages.
635 	 */
636 	if ((vp->v_flag & VNOCACHE) ||
637 	    (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
638 	    rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
639 	    !vn_has_cached_data(vp))) {
640 		size_t bufsize;
641 		int count;
642 		uint_t org_offset;
643 
644 nfs_fwrite:
645 		if (rp->r_flags & RSTALE) {
646 			resid = uiop->uio_resid;
647 			offset = uiop->uio_loffset;
648 			error = rp->r_error;
649 			goto bottom;
650 		}
651 		bufsize = MIN(uiop->uio_resid, mi->mi_curwrite);
652 		base = kmem_alloc(bufsize, KM_SLEEP);
653 		do {
654 			resid = uiop->uio_resid;
655 			offset = uiop->uio_loffset;
656 			count = MIN(uiop->uio_resid, bufsize);
657 			org_offset = uiop->uio_offset;
658 			error = uiomove(base, count, UIO_WRITE, uiop);
659 			if (!error) {
660 				error = nfswrite(vp, base, org_offset,
661 				    count, cr);
662 			}
663 		} while (!error && uiop->uio_resid > 0);
664 		kmem_free(base, bufsize);
665 		goto bottom;
666 	}
667 
668 	do {
669 		off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
670 		on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
671 		n = MIN(MAXBSIZE - on, uiop->uio_resid);
672 
673 		resid = uiop->uio_resid;
674 		offset = uiop->uio_loffset;
675 
676 		if (rp->r_flags & RSTALE) {
677 			error = rp->r_error;
678 			break;
679 		}
680 
681 		/*
682 		 * Don't create dirty pages faster than they
683 		 * can be cleaned so that the system doesn't
684 		 * get imbalanced.  If the async queue is
685 		 * maxed out, then wait for it to drain before
686 		 * creating more dirty pages.  Also, wait for
687 		 * any threads doing pagewalks in the vop_getattr
688 		 * entry points so that they don't block for
689 		 * long periods.
690 		 */
691 		mutex_enter(&rp->r_statelock);
692 		while ((mi->mi_max_threads != 0 &&
693 		    rp->r_awcount > 2 * mi->mi_max_threads) ||
694 		    rp->r_gcount > 0)
695 			cv_wait(&rp->r_cv, &rp->r_statelock);
696 		mutex_exit(&rp->r_statelock);
697 
698 		if (vpm_enable) {
699 			/*
700 			 * It will use kpm mappings, so no need to
701 			 * pass an address.
702 			 */
703 			error = writerp(rp, NULL, n, uiop, 0);
704 		} else  {
705 			if (segmap_kpm) {
706 				int pon = uiop->uio_loffset & PAGEOFFSET;
707 				size_t pn = MIN(PAGESIZE - pon,
708 				    uiop->uio_resid);
709 				int pagecreate;
710 
711 				mutex_enter(&rp->r_statelock);
712 				pagecreate = (pon == 0) && (pn == PAGESIZE ||
713 				    uiop->uio_loffset + pn >= rp->r_size);
714 				mutex_exit(&rp->r_statelock);
715 
716 				base = segmap_getmapflt(segkmap, vp, off + on,
717 				    pn, !pagecreate, S_WRITE);
718 
719 				error = writerp(rp, base + pon, n, uiop,
720 				    pagecreate);
721 
722 			} else {
723 				base = segmap_getmapflt(segkmap, vp, off + on,
724 				    n, 0, S_READ);
725 				error = writerp(rp, base + on, n, uiop, 0);
726 			}
727 		}
728 
729 		if (!error) {
730 			if (mi->mi_flags & MI_NOAC)
731 				flags = SM_WRITE;
732 			else if (n + on == MAXBSIZE || IS_SWAPVP(vp)) {
733 				/*
734 				 * Have written a whole block.
735 				 * Start an asynchronous write
736 				 * and mark the buffer to
737 				 * indicate that it won't be
738 				 * needed again soon.
739 				 */
740 				flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
741 			} else
742 				flags = 0;
743 			if ((ioflag & (FSYNC|FDSYNC)) ||
744 			    (rp->r_flags & ROUTOFSPACE)) {
745 				flags &= ~SM_ASYNC;
746 				flags |= SM_WRITE;
747 			}
748 			if (vpm_enable) {
749 				error = vpm_sync_pages(vp, off, n, flags);
750 			} else {
751 				error = segmap_release(segkmap, base, flags);
752 			}
753 		} else {
754 			if (vpm_enable) {
755 				(void) vpm_sync_pages(vp, off, n, 0);
756 			} else {
757 				(void) segmap_release(segkmap, base, 0);
758 			}
759 			/*
760 			 * In the event that we got an access error while
761 			 * faulting in a page for a write-only file just
762 			 * force a write.
763 			 */
764 			if (error == EACCES)
765 				goto nfs_fwrite;
766 		}
767 	} while (!error && uiop->uio_resid > 0);
768 
769 bottom:
770 	if (error) {
771 		uiop->uio_resid = resid + remainder;
772 		uiop->uio_loffset = offset;
773 	} else
774 		uiop->uio_resid += remainder;
775 
776 	nfs_rw_exit(&rp->r_lkserlock);
777 
778 	return (error);
779 }
780 
781 /*
782  * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
783  */
784 static int
785 nfs_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
786 	int flags, cred_t *cr)
787 {
788 	struct buf *bp;
789 	int error;
790 
791 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
792 	bp = pageio_setup(pp, len, vp, flags);
793 	ASSERT(bp != NULL);
794 
795 	/*
796 	 * pageio_setup should have set b_addr to 0.  This
797 	 * is correct since we want to do I/O on a page
798 	 * boundary.  bp_mapin will use this addr to calculate
799 	 * an offset, and then set b_addr to the kernel virtual
800 	 * address it allocated for us.
801 	 */
802 	ASSERT(bp->b_un.b_addr == 0);
803 
804 	bp->b_edev = 0;
805 	bp->b_dev = 0;
806 	bp->b_lblkno = lbtodb(off);
807 	bp->b_file = vp;
808 	bp->b_offset = (offset_t)off;
809 	bp_mapin(bp);
810 
811 	error = nfs_bio(bp, cr);
812 
813 	bp_mapout(bp);
814 	pageio_done(bp);
815 
816 	return (error);
817 }
818 
819 /*
820  * Write to file.  Writes to remote server in largest size
821  * chunks that the server can handle.  Write is synchronous.
822  */
823 static int
824 nfswrite(vnode_t *vp, caddr_t base, uint_t offset, int count, cred_t *cr)
825 {
826 	rnode_t *rp;
827 	mntinfo_t *mi;
828 	struct nfswriteargs wa;
829 	struct nfsattrstat ns;
830 	int error;
831 	int tsize;
832 	int douprintf;
833 
834 	douprintf = 1;
835 
836 	rp = VTOR(vp);
837 	mi = VTOMI(vp);
838 
839 	ASSERT(nfs_zone() == mi->mi_zone);
840 
841 	wa.wa_args = &wa.wa_args_buf;
842 	wa.wa_fhandle = *VTOFH(vp);
843 
844 	do {
845 		tsize = MIN(mi->mi_curwrite, count);
846 		wa.wa_data = base;
847 		wa.wa_begoff = offset;
848 		wa.wa_totcount = tsize;
849 		wa.wa_count = tsize;
850 		wa.wa_offset = offset;
851 
852 		if (mi->mi_io_kstats) {
853 			mutex_enter(&mi->mi_lock);
854 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
855 			mutex_exit(&mi->mi_lock);
856 		}
857 		wa.wa_mblk = NULL;
858 		do {
859 			error = rfs2call(mi, RFS_WRITE,
860 			    xdr_writeargs, (caddr_t)&wa,
861 			    xdr_attrstat, (caddr_t)&ns, cr,
862 			    &douprintf, &ns.ns_status, 0, NULL);
863 		} while (error == ENFS_TRYAGAIN);
864 		if (mi->mi_io_kstats) {
865 			mutex_enter(&mi->mi_lock);
866 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
867 			mutex_exit(&mi->mi_lock);
868 		}
869 
870 		if (!error) {
871 			error = geterrno(ns.ns_status);
872 			/*
873 			 * Can't check for stale fhandle and purge caches
874 			 * here because pages are held by nfs_getpage.
875 			 * Just mark the attribute cache as timed out
876 			 * and set RWRITEATTR to indicate that the file
877 			 * was modified with a WRITE operation.
878 			 */
879 			if (!error) {
880 				count -= tsize;
881 				base += tsize;
882 				offset += tsize;
883 				if (mi->mi_io_kstats) {
884 					mutex_enter(&mi->mi_lock);
885 					KSTAT_IO_PTR(mi->mi_io_kstats)->
886 					    writes++;
887 					KSTAT_IO_PTR(mi->mi_io_kstats)->
888 					    nwritten += tsize;
889 					mutex_exit(&mi->mi_lock);
890 				}
891 				lwp_stat_update(LWP_STAT_OUBLK, 1);
892 				mutex_enter(&rp->r_statelock);
893 				PURGE_ATTRCACHE_LOCKED(rp);
894 				rp->r_flags |= RWRITEATTR;
895 				mutex_exit(&rp->r_statelock);
896 			}
897 		}
898 	} while (!error && count);
899 
900 	return (error);
901 }
902 
903 /*
904  * Read from a file.  Reads data in largest chunks our interface can handle.
905  */
906 static int
907 nfsread(vnode_t *vp, caddr_t base, uint_t offset,
908     int count, size_t *residp, cred_t *cr)
909 {
910 	mntinfo_t *mi;
911 	struct nfsreadargs ra;
912 	struct nfsrdresult rr;
913 	int tsize;
914 	int error;
915 	int douprintf;
916 	failinfo_t fi;
917 	rnode_t *rp;
918 	struct vattr va;
919 	hrtime_t t;
920 
921 	rp = VTOR(vp);
922 	mi = VTOMI(vp);
923 
924 	ASSERT(nfs_zone() == mi->mi_zone);
925 
926 	douprintf = 1;
927 
928 	ra.ra_fhandle = *VTOFH(vp);
929 
930 	fi.vp = vp;
931 	fi.fhp = (caddr_t)&ra.ra_fhandle;
932 	fi.copyproc = nfscopyfh;
933 	fi.lookupproc = nfslookup;
934 	fi.xattrdirproc = acl_getxattrdir2;
935 
936 	do {
937 		if (mi->mi_io_kstats) {
938 			mutex_enter(&mi->mi_lock);
939 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
940 			mutex_exit(&mi->mi_lock);
941 		}
942 
943 		do {
944 			tsize = MIN(mi->mi_curread, count);
945 			rr.rr_data = base;
946 			ra.ra_offset = offset;
947 			ra.ra_totcount = tsize;
948 			ra.ra_count = tsize;
949 			ra.ra_data = base;
950 			t = gethrtime();
951 			error = rfs2call(mi, RFS_READ,
952 			    xdr_readargs, (caddr_t)&ra,
953 			    xdr_rdresult, (caddr_t)&rr, cr,
954 			    &douprintf, &rr.rr_status, 0, &fi);
955 		} while (error == ENFS_TRYAGAIN);
956 
957 		if (mi->mi_io_kstats) {
958 			mutex_enter(&mi->mi_lock);
959 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
960 			mutex_exit(&mi->mi_lock);
961 		}
962 
963 		if (!error) {
964 			error = geterrno(rr.rr_status);
965 			if (!error) {
966 				count -= rr.rr_count;
967 				base += rr.rr_count;
968 				offset += rr.rr_count;
969 				if (mi->mi_io_kstats) {
970 					mutex_enter(&mi->mi_lock);
971 					KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
972 					KSTAT_IO_PTR(mi->mi_io_kstats)->nread +=
973 					    rr.rr_count;
974 					mutex_exit(&mi->mi_lock);
975 				}
976 				lwp_stat_update(LWP_STAT_INBLK, 1);
977 			}
978 		}
979 	} while (!error && count && rr.rr_count == tsize);
980 
981 	*residp = count;
982 
983 	if (!error) {
984 		/*
985 		 * Since no error occurred, we have the current
986 		 * attributes and we need to do a cache check and then
987 		 * potentially update the cached attributes.  We can't
988 		 * use the normal attribute check and cache mechanisms
989 		 * because they might cause a cache flush which would
990 		 * deadlock.  Instead, we just check the cache to see
991 		 * if the attributes have changed.  If it is, then we
992 		 * just mark the attributes as out of date.  The next
993 		 * time that the attributes are checked, they will be
994 		 * out of date, new attributes will be fetched, and
995 		 * the page cache will be flushed.  If the attributes
996 		 * weren't changed, then we just update the cached
997 		 * attributes with these attributes.
998 		 */
999 		/*
1000 		 * If NFS_ACL is supported on the server, then the
1001 		 * attributes returned by server may have minimal
1002 		 * permissions sometimes denying access to users having
1003 		 * proper access.  To get the proper attributes, mark
1004 		 * the attributes as expired so that they will be
1005 		 * regotten via the NFS_ACL GETATTR2 procedure.
1006 		 */
1007 		error = nattr_to_vattr(vp, &rr.rr_attr, &va);
1008 		mutex_enter(&rp->r_statelock);
1009 		if (error || !CACHE_VALID(rp, va.va_mtime, va.va_size) ||
1010 		    (mi->mi_flags & MI_ACL)) {
1011 			mutex_exit(&rp->r_statelock);
1012 			PURGE_ATTRCACHE(vp);
1013 		} else {
1014 			if (rp->r_mtime <= t) {
1015 				nfs_attrcache_va(vp, &va);
1016 			}
1017 			mutex_exit(&rp->r_statelock);
1018 		}
1019 	}
1020 
1021 	return (error);
1022 }
1023 
1024 /* ARGSUSED */
1025 static int
1026 nfs_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
1027 	caller_context_t *ct)
1028 {
1029 
1030 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1031 		return (EIO);
1032 	switch (cmd) {
1033 		case _FIODIRECTIO:
1034 			return (nfs_directio(vp, (int)arg, cr));
1035 		default:
1036 			return (ENOTTY);
1037 	}
1038 }
1039 
1040 /* ARGSUSED */
1041 static int
1042 nfs_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1043 	caller_context_t *ct)
1044 {
1045 	int error;
1046 	rnode_t *rp;
1047 
1048 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1049 		return (EIO);
1050 	/*
1051 	 * If it has been specified that the return value will
1052 	 * just be used as a hint, and we are only being asked
1053 	 * for size, fsid or rdevid, then return the client's
1054 	 * notion of these values without checking to make sure
1055 	 * that the attribute cache is up to date.
1056 	 * The whole point is to avoid an over the wire GETATTR
1057 	 * call.
1058 	 */
1059 	rp = VTOR(vp);
1060 	if (flags & ATTR_HINT) {
1061 		if (vap->va_mask ==
1062 		    (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) {
1063 			mutex_enter(&rp->r_statelock);
1064 			if (vap->va_mask | AT_SIZE)
1065 				vap->va_size = rp->r_size;
1066 			if (vap->va_mask | AT_FSID)
1067 				vap->va_fsid = rp->r_attr.va_fsid;
1068 			if (vap->va_mask | AT_RDEV)
1069 				vap->va_rdev = rp->r_attr.va_rdev;
1070 			mutex_exit(&rp->r_statelock);
1071 			return (0);
1072 		}
1073 	}
1074 
1075 	/*
1076 	 * Only need to flush pages if asking for the mtime
1077 	 * and if there any dirty pages or any outstanding
1078 	 * asynchronous (write) requests for this file.
1079 	 */
1080 	if (vap->va_mask & AT_MTIME) {
1081 		if (vn_has_cached_data(vp) &&
1082 		    ((rp->r_flags & RDIRTY) || rp->r_awcount > 0)) {
1083 			mutex_enter(&rp->r_statelock);
1084 			rp->r_gcount++;
1085 			mutex_exit(&rp->r_statelock);
1086 			error = nfs_putpage(vp, (offset_t)0, 0, 0, cr, ct);
1087 			mutex_enter(&rp->r_statelock);
1088 			if (error && (error == ENOSPC || error == EDQUOT)) {
1089 				if (!rp->r_error)
1090 					rp->r_error = error;
1091 			}
1092 			if (--rp->r_gcount == 0)
1093 				cv_broadcast(&rp->r_cv);
1094 			mutex_exit(&rp->r_statelock);
1095 		}
1096 	}
1097 
1098 	return (nfsgetattr(vp, vap, cr));
1099 }
1100 
1101 /*ARGSUSED4*/
1102 static int
1103 nfs_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1104 		caller_context_t *ct)
1105 {
1106 	int error;
1107 	uint_t mask;
1108 	struct vattr va;
1109 
1110 	mask = vap->va_mask;
1111 
1112 	if (mask & AT_NOSET)
1113 		return (EINVAL);
1114 
1115 	if ((mask & AT_SIZE) &&
1116 	    vap->va_type == VREG &&
1117 	    vap->va_size > MAXOFF32_T)
1118 		return (EFBIG);
1119 
1120 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1121 		return (EIO);
1122 
1123 	va.va_mask = AT_UID | AT_MODE;
1124 
1125 	error = nfsgetattr(vp, &va, cr);
1126 	if (error)
1127 		return (error);
1128 
1129 	error = secpolicy_vnode_setattr(cr, vp, vap, &va, flags, nfs_accessx,
1130 	    vp);
1131 
1132 	if (error)
1133 		return (error);
1134 
1135 	return (nfssetattr(vp, vap, flags, cr));
1136 }
1137 
1138 static int
1139 nfssetattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr)
1140 {
1141 	int error;
1142 	uint_t mask;
1143 	struct nfssaargs args;
1144 	struct nfsattrstat ns;
1145 	int douprintf;
1146 	rnode_t *rp;
1147 	struct vattr va;
1148 	mode_t omode;
1149 	mntinfo_t *mi;
1150 	vsecattr_t *vsp;
1151 	hrtime_t t;
1152 
1153 	mask = vap->va_mask;
1154 
1155 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
1156 
1157 	rp = VTOR(vp);
1158 
1159 	/*
1160 	 * Only need to flush pages if there are any pages and
1161 	 * if the file is marked as dirty in some fashion.  The
1162 	 * file must be flushed so that we can accurately
1163 	 * determine the size of the file and the cached data
1164 	 * after the SETATTR returns.  A file is considered to
1165 	 * be dirty if it is either marked with RDIRTY, has
1166 	 * outstanding i/o's active, or is mmap'd.  In this
1167 	 * last case, we can't tell whether there are dirty
1168 	 * pages, so we flush just to be sure.
1169 	 */
1170 	if (vn_has_cached_data(vp) &&
1171 	    ((rp->r_flags & RDIRTY) ||
1172 	    rp->r_count > 0 ||
1173 	    rp->r_mapcnt > 0)) {
1174 		ASSERT(vp->v_type != VCHR);
1175 		error = nfs_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
1176 		if (error && (error == ENOSPC || error == EDQUOT)) {
1177 			mutex_enter(&rp->r_statelock);
1178 			if (!rp->r_error)
1179 				rp->r_error = error;
1180 			mutex_exit(&rp->r_statelock);
1181 		}
1182 	}
1183 
1184 	/*
1185 	 * If the system call was utime(2) or utimes(2) and the
1186 	 * application did not specify the times, then set the
1187 	 * mtime nanosecond field to 1 billion.  This will get
1188 	 * translated from 1 billion nanoseconds to 1 million
1189 	 * microseconds in the over the wire request.  The
1190 	 * server will use 1 million in the microsecond field
1191 	 * to tell whether both the mtime and atime should be
1192 	 * set to the server's current time.
1193 	 *
1194 	 * This is an overload of the protocol and should be
1195 	 * documented in the NFS Version 2 protocol specification.
1196 	 */
1197 	if ((mask & AT_MTIME) && !(flags & ATTR_UTIME)) {
1198 		vap->va_mtime.tv_nsec = 1000000000;
1199 		if (NFS_TIME_T_OK(vap->va_mtime.tv_sec) &&
1200 		    NFS_TIME_T_OK(vap->va_atime.tv_sec)) {
1201 			error = vattr_to_sattr(vap, &args.saa_sa);
1202 		} else {
1203 			/*
1204 			 * Use server times. vap time values will not be used.
1205 			 * To ensure no time overflow, make sure vap has
1206 			 * valid values, but retain the original values.
1207 			 */
1208 			timestruc_t	mtime = vap->va_mtime;
1209 			timestruc_t	atime = vap->va_atime;
1210 			time_t		now;
1211 
1212 			now = gethrestime_sec();
1213 			if (NFS_TIME_T_OK(now)) {
1214 				/* Just in case server does not know of this */
1215 				vap->va_mtime.tv_sec = now;
1216 				vap->va_atime.tv_sec = now;
1217 			} else {
1218 				vap->va_mtime.tv_sec = 0;
1219 				vap->va_atime.tv_sec = 0;
1220 			}
1221 			error = vattr_to_sattr(vap, &args.saa_sa);
1222 			/* set vap times back on */
1223 			vap->va_mtime = mtime;
1224 			vap->va_atime = atime;
1225 		}
1226 	} else {
1227 		/* Either do not set times or use the client specified times */
1228 		error = vattr_to_sattr(vap, &args.saa_sa);
1229 	}
1230 	if (error) {
1231 		/* req time field(s) overflow - return immediately */
1232 		return (error);
1233 	}
1234 	args.saa_fh = *VTOFH(vp);
1235 
1236 	va.va_mask = AT_MODE;
1237 	error = nfsgetattr(vp, &va, cr);
1238 	if (error)
1239 		return (error);
1240 	omode = va.va_mode;
1241 
1242 	mi = VTOMI(vp);
1243 
1244 	douprintf = 1;
1245 
1246 	t = gethrtime();
1247 
1248 	error = rfs2call(mi, RFS_SETATTR,
1249 	    xdr_saargs, (caddr_t)&args,
1250 	    xdr_attrstat, (caddr_t)&ns, cr,
1251 	    &douprintf, &ns.ns_status, 0, NULL);
1252 
1253 	/*
1254 	 * Purge the access cache and ACL cache if changing either the
1255 	 * owner of the file, the group owner, or the mode.  These may
1256 	 * change the access permissions of the file, so purge old
1257 	 * information and start over again.
1258 	 */
1259 	if ((mask & (AT_UID | AT_GID | AT_MODE)) && (mi->mi_flags & MI_ACL)) {
1260 		(void) nfs_access_purge_rp(rp);
1261 		if (rp->r_secattr != NULL) {
1262 			mutex_enter(&rp->r_statelock);
1263 			vsp = rp->r_secattr;
1264 			rp->r_secattr = NULL;
1265 			mutex_exit(&rp->r_statelock);
1266 			if (vsp != NULL)
1267 				nfs_acl_free(vsp);
1268 		}
1269 	}
1270 
1271 	if (!error) {
1272 		error = geterrno(ns.ns_status);
1273 		if (!error) {
1274 			/*
1275 			 * If changing the size of the file, invalidate
1276 			 * any local cached data which is no longer part
1277 			 * of the file.  We also possibly invalidate the
1278 			 * last page in the file.  We could use
1279 			 * pvn_vpzero(), but this would mark the page as
1280 			 * modified and require it to be written back to
1281 			 * the server for no particularly good reason.
1282 			 * This way, if we access it, then we bring it
1283 			 * back in.  A read should be cheaper than a
1284 			 * write.
1285 			 */
1286 			if (mask & AT_SIZE) {
1287 				nfs_invalidate_pages(vp,
1288 				    (vap->va_size & PAGEMASK), cr);
1289 			}
1290 			(void) nfs_cache_fattr(vp, &ns.ns_attr, &va, t, cr);
1291 			/*
1292 			 * If NFS_ACL is supported on the server, then the
1293 			 * attributes returned by server may have minimal
1294 			 * permissions sometimes denying access to users having
1295 			 * proper access.  To get the proper attributes, mark
1296 			 * the attributes as expired so that they will be
1297 			 * regotten via the NFS_ACL GETATTR2 procedure.
1298 			 */
1299 			if (mi->mi_flags & MI_ACL) {
1300 				PURGE_ATTRCACHE(vp);
1301 			}
1302 			/*
1303 			 * This next check attempts to deal with NFS
1304 			 * servers which can not handle increasing
1305 			 * the size of the file via setattr.  Most
1306 			 * of these servers do not return an error,
1307 			 * but do not change the size of the file.
1308 			 * Hence, this check and then attempt to set
1309 			 * the file size by writing 1 byte at the
1310 			 * offset of the end of the file that we need.
1311 			 */
1312 			if ((mask & AT_SIZE) &&
1313 			    ns.ns_attr.na_size < (uint32_t)vap->va_size) {
1314 				char zb = '\0';
1315 
1316 				error = nfswrite(vp, &zb,
1317 				    vap->va_size - sizeof (zb),
1318 				    sizeof (zb), cr);
1319 			}
1320 			/*
1321 			 * Some servers will change the mode to clear the setuid
1322 			 * and setgid bits when changing the uid or gid.  The
1323 			 * client needs to compensate appropriately.
1324 			 */
1325 			if (mask & (AT_UID | AT_GID)) {
1326 				int terror;
1327 
1328 				va.va_mask = AT_MODE;
1329 				terror = nfsgetattr(vp, &va, cr);
1330 				if (!terror &&
1331 				    (((mask & AT_MODE) &&
1332 				    va.va_mode != vap->va_mode) ||
1333 				    (!(mask & AT_MODE) &&
1334 				    va.va_mode != omode))) {
1335 					va.va_mask = AT_MODE;
1336 					if (mask & AT_MODE)
1337 						va.va_mode = vap->va_mode;
1338 					else
1339 						va.va_mode = omode;
1340 					(void) nfssetattr(vp, &va, 0, cr);
1341 				}
1342 			}
1343 		} else {
1344 			PURGE_ATTRCACHE(vp);
1345 			PURGE_STALE_FH(error, vp, cr);
1346 		}
1347 	} else {
1348 		PURGE_ATTRCACHE(vp);
1349 	}
1350 
1351 	return (error);
1352 }
1353 
1354 static int
1355 nfs_accessx(void *vp, int mode, cred_t *cr)
1356 {
1357 	ASSERT(nfs_zone() == VTOMI((vnode_t *)vp)->mi_zone);
1358 	return (nfs_access(vp, mode, 0, cr, NULL));
1359 }
1360 
1361 /* ARGSUSED */
1362 static int
1363 nfs_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
1364 {
1365 	struct vattr va;
1366 	int error;
1367 	mntinfo_t *mi;
1368 	int shift = 0;
1369 
1370 	mi = VTOMI(vp);
1371 
1372 	if (nfs_zone() != mi->mi_zone)
1373 		return (EIO);
1374 	if (mi->mi_flags & MI_ACL) {
1375 		error = acl_access2(vp, mode, flags, cr);
1376 		if (mi->mi_flags & MI_ACL)
1377 			return (error);
1378 	}
1379 
1380 	va.va_mask = AT_MODE | AT_UID | AT_GID;
1381 	error = nfsgetattr(vp, &va, cr);
1382 	if (error)
1383 		return (error);
1384 
1385 	/*
1386 	 * Disallow write attempts on read-only
1387 	 * file systems, unless the file is a
1388 	 * device node.
1389 	 */
1390 	if ((mode & VWRITE) && vn_is_readonly(vp) && !IS_DEVVP(vp))
1391 		return (EROFS);
1392 
1393 	/*
1394 	 * Disallow attempts to access mandatory lock files.
1395 	 */
1396 	if ((mode & (VWRITE | VREAD | VEXEC)) &&
1397 	    MANDLOCK(vp, va.va_mode))
1398 		return (EACCES);
1399 
1400 	/*
1401 	 * Access check is based on only
1402 	 * one of owner, group, public.
1403 	 * If not owner, then check group.
1404 	 * If not a member of the group,
1405 	 * then check public access.
1406 	 */
1407 	if (crgetuid(cr) != va.va_uid) {
1408 		shift += 3;
1409 		if (!groupmember(va.va_gid, cr))
1410 			shift += 3;
1411 	}
1412 found:
1413 	mode &= ~(va.va_mode << shift);
1414 	if (mode == 0)
1415 		return (0);
1416 
1417 	return (secpolicy_vnode_access(cr, vp, va.va_uid, mode));
1418 }
1419 
1420 static int nfs_do_symlink_cache = 1;
1421 
1422 /* ARGSUSED */
1423 static int
1424 nfs_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
1425 {
1426 	int error;
1427 	struct nfsrdlnres rl;
1428 	rnode_t *rp;
1429 	int douprintf;
1430 	failinfo_t fi;
1431 
1432 	/*
1433 	 * We want to be consistent with UFS semantics so we will return
1434 	 * EINVAL instead of ENXIO. This violates the XNFS spec and
1435 	 * the RFC 1094, which are wrong any way. BUGID 1138002.
1436 	 */
1437 	if (vp->v_type != VLNK)
1438 		return (EINVAL);
1439 
1440 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1441 		return (EIO);
1442 
1443 	rp = VTOR(vp);
1444 	if (nfs_do_symlink_cache && rp->r_symlink.contents != NULL) {
1445 		error = nfs_validate_caches(vp, cr);
1446 		if (error)
1447 			return (error);
1448 		mutex_enter(&rp->r_statelock);
1449 		if (rp->r_symlink.contents != NULL) {
1450 			error = uiomove(rp->r_symlink.contents,
1451 			    rp->r_symlink.len, UIO_READ, uiop);
1452 			mutex_exit(&rp->r_statelock);
1453 			return (error);
1454 		}
1455 		mutex_exit(&rp->r_statelock);
1456 	}
1457 
1458 
1459 	rl.rl_data = kmem_alloc(NFS_MAXPATHLEN, KM_SLEEP);
1460 
1461 	fi.vp = vp;
1462 	fi.fhp = NULL;		/* no need to update, filehandle not copied */
1463 	fi.copyproc = nfscopyfh;
1464 	fi.lookupproc = nfslookup;
1465 	fi.xattrdirproc = acl_getxattrdir2;
1466 
1467 	douprintf = 1;
1468 
1469 	error = rfs2call(VTOMI(vp), RFS_READLINK,
1470 	    xdr_readlink, (caddr_t)VTOFH(vp),
1471 	    xdr_rdlnres, (caddr_t)&rl, cr,
1472 	    &douprintf, &rl.rl_status, 0, &fi);
1473 
1474 	if (error) {
1475 
1476 		kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
1477 		return (error);
1478 	}
1479 
1480 	error = geterrno(rl.rl_status);
1481 	if (!error) {
1482 		error = uiomove(rl.rl_data, (int)rl.rl_count, UIO_READ, uiop);
1483 		if (nfs_do_symlink_cache && rp->r_symlink.contents == NULL) {
1484 			mutex_enter(&rp->r_statelock);
1485 			if (rp->r_symlink.contents == NULL) {
1486 				rp->r_symlink.contents = rl.rl_data;
1487 				rp->r_symlink.len = (int)rl.rl_count;
1488 				rp->r_symlink.size = NFS_MAXPATHLEN;
1489 				mutex_exit(&rp->r_statelock);
1490 			} else {
1491 				mutex_exit(&rp->r_statelock);
1492 
1493 				kmem_free((void *)rl.rl_data,
1494 				    NFS_MAXPATHLEN);
1495 			}
1496 		} else {
1497 
1498 			kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
1499 		}
1500 	} else {
1501 		PURGE_STALE_FH(error, vp, cr);
1502 
1503 		kmem_free((void *)rl.rl_data, NFS_MAXPATHLEN);
1504 	}
1505 
1506 	/*
1507 	 * Conform to UFS semantics (see comment above)
1508 	 */
1509 	return (error == ENXIO ? EINVAL : error);
1510 }
1511 
1512 /*
1513  * Flush local dirty pages to stable storage on the server.
1514  *
1515  * If FNODSYNC is specified, then there is nothing to do because
1516  * metadata changes are not cached on the client before being
1517  * sent to the server.
1518  */
1519 /* ARGSUSED */
1520 static int
1521 nfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
1522 {
1523 	int error;
1524 
1525 	if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
1526 		return (0);
1527 
1528 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1529 		return (EIO);
1530 
1531 	error = nfs_putpage(vp, (offset_t)0, 0, 0, cr, ct);
1532 	if (!error)
1533 		error = VTOR(vp)->r_error;
1534 	return (error);
1535 }
1536 
1537 
1538 /*
1539  * Weirdness: if the file was removed or the target of a rename
1540  * operation while it was open, it got renamed instead.  Here we
1541  * remove the renamed file.
1542  */
1543 /* ARGSUSED */
1544 static void
1545 nfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1546 {
1547 	rnode_t *rp;
1548 
1549 	ASSERT(vp != DNLC_NO_VNODE);
1550 
1551 	/*
1552 	 * If this is coming from the wrong zone, we let someone in the right
1553 	 * zone take care of it asynchronously.  We can get here due to
1554 	 * VN_RELE() being called from pageout() or fsflush().  This call may
1555 	 * potentially turn into an expensive no-op if, for instance, v_count
1556 	 * gets incremented in the meantime, but it's still correct.
1557 	 */
1558 	if (nfs_zone() != VTOMI(vp)->mi_zone) {
1559 		nfs_async_inactive(vp, cr, nfs_inactive);
1560 		return;
1561 	}
1562 
1563 	rp = VTOR(vp);
1564 redo:
1565 	if (rp->r_unldvp != NULL) {
1566 		/*
1567 		 * Save the vnode pointer for the directory where the
1568 		 * unlinked-open file got renamed, then set it to NULL
1569 		 * to prevent another thread from getting here before
1570 		 * we're done with the remove.  While we have the
1571 		 * statelock, make local copies of the pertinent rnode
1572 		 * fields.  If we weren't to do this in an atomic way, the
1573 		 * the unl* fields could become inconsistent with respect
1574 		 * to each other due to a race condition between this
1575 		 * code and nfs_remove().  See bug report 1034328.
1576 		 */
1577 		mutex_enter(&rp->r_statelock);
1578 		if (rp->r_unldvp != NULL) {
1579 			vnode_t *unldvp;
1580 			char *unlname;
1581 			cred_t *unlcred;
1582 			struct nfsdiropargs da;
1583 			enum nfsstat status;
1584 			int douprintf;
1585 			int error;
1586 
1587 			unldvp = rp->r_unldvp;
1588 			rp->r_unldvp = NULL;
1589 			unlname = rp->r_unlname;
1590 			rp->r_unlname = NULL;
1591 			unlcred = rp->r_unlcred;
1592 			rp->r_unlcred = NULL;
1593 			mutex_exit(&rp->r_statelock);
1594 
1595 			/*
1596 			 * If there are any dirty pages left, then flush
1597 			 * them.  This is unfortunate because they just
1598 			 * may get thrown away during the remove operation,
1599 			 * but we have to do this for correctness.
1600 			 */
1601 			if (vn_has_cached_data(vp) &&
1602 			    ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
1603 				ASSERT(vp->v_type != VCHR);
1604 				error = nfs_putpage(vp, (offset_t)0, 0, 0,
1605 				    cr, ct);
1606 				if (error) {
1607 					mutex_enter(&rp->r_statelock);
1608 					if (!rp->r_error)
1609 						rp->r_error = error;
1610 					mutex_exit(&rp->r_statelock);
1611 				}
1612 			}
1613 
1614 			/*
1615 			 * Do the remove operation on the renamed file
1616 			 */
1617 			setdiropargs(&da, unlname, unldvp);
1618 
1619 			douprintf = 1;
1620 
1621 			(void) rfs2call(VTOMI(unldvp), RFS_REMOVE,
1622 			    xdr_diropargs, (caddr_t)&da,
1623 			    xdr_enum, (caddr_t)&status, unlcred,
1624 			    &douprintf, &status, 0, NULL);
1625 
1626 			if (HAVE_RDDIR_CACHE(VTOR(unldvp)))
1627 				nfs_purge_rddir_cache(unldvp);
1628 			PURGE_ATTRCACHE(unldvp);
1629 
1630 			/*
1631 			 * Release stuff held for the remove
1632 			 */
1633 			VN_RELE(unldvp);
1634 			kmem_free(unlname, MAXNAMELEN);
1635 			crfree(unlcred);
1636 			goto redo;
1637 		}
1638 		mutex_exit(&rp->r_statelock);
1639 	}
1640 
1641 	rp_addfree(rp, cr);
1642 }
1643 
1644 /*
1645  * Remote file system operations having to do with directory manipulation.
1646  */
1647 
1648 /* ARGSUSED */
1649 static int
1650 nfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1651 	int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1652 	int *direntflags, pathname_t *realpnp)
1653 {
1654 	int error;
1655 	vnode_t *vp;
1656 	vnode_t *avp = NULL;
1657 	rnode_t *drp;
1658 
1659 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
1660 		return (EPERM);
1661 
1662 	drp = VTOR(dvp);
1663 
1664 	/*
1665 	 * Are we looking up extended attributes?  If so, "dvp" is
1666 	 * the file or directory for which we want attributes, and
1667 	 * we need a lookup of the hidden attribute directory
1668 	 * before we lookup the rest of the path.
1669 	 */
1670 	if (flags & LOOKUP_XATTR) {
1671 		bool_t cflag = ((flags & CREATE_XATTR_DIR) != 0);
1672 		mntinfo_t *mi;
1673 
1674 		mi = VTOMI(dvp);
1675 		if (!(mi->mi_flags & MI_EXTATTR))
1676 			return (EINVAL);
1677 
1678 		if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp)))
1679 			return (EINTR);
1680 
1681 		(void) nfslookup_dnlc(dvp, XATTR_DIR_NAME, &avp, cr);
1682 		if (avp == NULL)
1683 			error = acl_getxattrdir2(dvp, &avp, cflag, cr, 0);
1684 		else
1685 			error = 0;
1686 
1687 		nfs_rw_exit(&drp->r_rwlock);
1688 
1689 		if (error) {
1690 			if (mi->mi_flags & MI_EXTATTR)
1691 				return (error);
1692 			return (EINVAL);
1693 		}
1694 		dvp = avp;
1695 		drp = VTOR(dvp);
1696 	}
1697 
1698 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp))) {
1699 		error = EINTR;
1700 		goto out;
1701 	}
1702 
1703 	error = nfslookup(dvp, nm, vpp, pnp, flags, rdir, cr, 0);
1704 
1705 	nfs_rw_exit(&drp->r_rwlock);
1706 
1707 	/*
1708 	 * If vnode is a device, create special vnode.
1709 	 */
1710 	if (!error && IS_DEVVP(*vpp)) {
1711 		vp = *vpp;
1712 		*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
1713 		VN_RELE(vp);
1714 	}
1715 
1716 out:
1717 	if (avp != NULL)
1718 		VN_RELE(avp);
1719 
1720 	return (error);
1721 }
1722 
1723 static int nfs_lookup_neg_cache = 1;
1724 
1725 #ifdef DEBUG
1726 static int nfs_lookup_dnlc_hits = 0;
1727 static int nfs_lookup_dnlc_misses = 0;
1728 static int nfs_lookup_dnlc_neg_hits = 0;
1729 static int nfs_lookup_dnlc_disappears = 0;
1730 static int nfs_lookup_dnlc_lookups = 0;
1731 #endif
1732 
1733 /* ARGSUSED */
1734 int
1735 nfslookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1736 	int flags, vnode_t *rdir, cred_t *cr, int rfscall_flags)
1737 {
1738 	int error;
1739 
1740 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
1741 
1742 	/*
1743 	 * If lookup is for "", just return dvp.  Don't need
1744 	 * to send it over the wire, look it up in the dnlc,
1745 	 * or perform any access checks.
1746 	 */
1747 	if (*nm == '\0') {
1748 		VN_HOLD(dvp);
1749 		*vpp = dvp;
1750 		return (0);
1751 	}
1752 
1753 	/*
1754 	 * Can't do lookups in non-directories.
1755 	 */
1756 	if (dvp->v_type != VDIR)
1757 		return (ENOTDIR);
1758 
1759 	/*
1760 	 * If we're called with RFSCALL_SOFT, it's important that
1761 	 * the only rfscall is one we make directly; if we permit
1762 	 * an access call because we're looking up "." or validating
1763 	 * a dnlc hit, we'll deadlock because that rfscall will not
1764 	 * have the RFSCALL_SOFT set.
1765 	 */
1766 	if (rfscall_flags & RFSCALL_SOFT)
1767 		goto callit;
1768 
1769 	/*
1770 	 * If lookup is for ".", just return dvp.  Don't need
1771 	 * to send it over the wire or look it up in the dnlc,
1772 	 * just need to check access.
1773 	 */
1774 	if (strcmp(nm, ".") == 0) {
1775 		error = nfs_access(dvp, VEXEC, 0, cr, NULL);
1776 		if (error)
1777 			return (error);
1778 		VN_HOLD(dvp);
1779 		*vpp = dvp;
1780 		return (0);
1781 	}
1782 
1783 	/*
1784 	 * Lookup this name in the DNLC.  If there was a valid entry,
1785 	 * then return the results of the lookup.
1786 	 */
1787 	error = nfslookup_dnlc(dvp, nm, vpp, cr);
1788 	if (error || *vpp != NULL)
1789 		return (error);
1790 
1791 callit:
1792 	error = nfslookup_otw(dvp, nm, vpp, cr, rfscall_flags);
1793 
1794 	return (error);
1795 }
1796 
1797 static int
1798 nfslookup_dnlc(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
1799 {
1800 	int error;
1801 	vnode_t *vp;
1802 
1803 	ASSERT(*nm != '\0');
1804 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
1805 
1806 	/*
1807 	 * Lookup this name in the DNLC.  If successful, then validate
1808 	 * the caches and then recheck the DNLC.  The DNLC is rechecked
1809 	 * just in case this entry got invalidated during the call
1810 	 * to nfs_validate_caches.
1811 	 *
1812 	 * An assumption is being made that it is safe to say that a
1813 	 * file exists which may not on the server.  Any operations to
1814 	 * the server will fail with ESTALE.
1815 	 */
1816 #ifdef DEBUG
1817 	nfs_lookup_dnlc_lookups++;
1818 #endif
1819 	vp = dnlc_lookup(dvp, nm);
1820 	if (vp != NULL) {
1821 		VN_RELE(vp);
1822 		if (vp == DNLC_NO_VNODE && !vn_is_readonly(dvp)) {
1823 			PURGE_ATTRCACHE(dvp);
1824 		}
1825 		error = nfs_validate_caches(dvp, cr);
1826 		if (error)
1827 			return (error);
1828 		vp = dnlc_lookup(dvp, nm);
1829 		if (vp != NULL) {
1830 			error = nfs_access(dvp, VEXEC, 0, cr, NULL);
1831 			if (error) {
1832 				VN_RELE(vp);
1833 				return (error);
1834 			}
1835 			if (vp == DNLC_NO_VNODE) {
1836 				VN_RELE(vp);
1837 #ifdef DEBUG
1838 				nfs_lookup_dnlc_neg_hits++;
1839 #endif
1840 				return (ENOENT);
1841 			}
1842 			*vpp = vp;
1843 #ifdef DEBUG
1844 			nfs_lookup_dnlc_hits++;
1845 #endif
1846 			return (0);
1847 		}
1848 #ifdef DEBUG
1849 		nfs_lookup_dnlc_disappears++;
1850 #endif
1851 	}
1852 #ifdef DEBUG
1853 	else
1854 		nfs_lookup_dnlc_misses++;
1855 #endif
1856 
1857 	*vpp = NULL;
1858 
1859 	return (0);
1860 }
1861 
1862 static int
1863 nfslookup_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr,
1864 	int rfscall_flags)
1865 {
1866 	int error;
1867 	struct nfsdiropargs da;
1868 	struct nfsdiropres dr;
1869 	int douprintf;
1870 	failinfo_t fi;
1871 	hrtime_t t;
1872 
1873 	ASSERT(*nm != '\0');
1874 	ASSERT(dvp->v_type == VDIR);
1875 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
1876 
1877 	setdiropargs(&da, nm, dvp);
1878 
1879 	fi.vp = dvp;
1880 	fi.fhp = NULL;		/* no need to update, filehandle not copied */
1881 	fi.copyproc = nfscopyfh;
1882 	fi.lookupproc = nfslookup;
1883 	fi.xattrdirproc = acl_getxattrdir2;
1884 
1885 	douprintf = 1;
1886 
1887 	t = gethrtime();
1888 
1889 	error = rfs2call(VTOMI(dvp), RFS_LOOKUP,
1890 	    xdr_diropargs, (caddr_t)&da,
1891 	    xdr_diropres, (caddr_t)&dr, cr,
1892 	    &douprintf, &dr.dr_status, rfscall_flags, &fi);
1893 
1894 	if (!error) {
1895 		error = geterrno(dr.dr_status);
1896 		if (!error) {
1897 			*vpp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
1898 			    dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
1899 			/*
1900 			 * If NFS_ACL is supported on the server, then the
1901 			 * attributes returned by server may have minimal
1902 			 * permissions sometimes denying access to users having
1903 			 * proper access.  To get the proper attributes, mark
1904 			 * the attributes as expired so that they will be
1905 			 * regotten via the NFS_ACL GETATTR2 procedure.
1906 			 */
1907 			if (VTOMI(*vpp)->mi_flags & MI_ACL) {
1908 				PURGE_ATTRCACHE(*vpp);
1909 			}
1910 			if (!(rfscall_flags & RFSCALL_SOFT))
1911 				dnlc_update(dvp, nm, *vpp);
1912 		} else {
1913 			PURGE_STALE_FH(error, dvp, cr);
1914 			if (error == ENOENT && nfs_lookup_neg_cache)
1915 				dnlc_enter(dvp, nm, DNLC_NO_VNODE);
1916 		}
1917 	}
1918 
1919 	return (error);
1920 }
1921 
1922 /* ARGSUSED */
1923 static int
1924 nfs_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
1925 	int mode, vnode_t **vpp, cred_t *cr, int lfaware, caller_context_t *ct,
1926 	vsecattr_t *vsecp)
1927 {
1928 	int error;
1929 	struct nfscreatargs args;
1930 	struct nfsdiropres dr;
1931 	int douprintf;
1932 	vnode_t *vp;
1933 	rnode_t *rp;
1934 	struct vattr vattr;
1935 	rnode_t *drp;
1936 	vnode_t *tempvp;
1937 	hrtime_t t;
1938 
1939 	drp = VTOR(dvp);
1940 
1941 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
1942 		return (EPERM);
1943 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
1944 		return (EINTR);
1945 
1946 	/*
1947 	 * We make a copy of the attributes because the caller does not
1948 	 * expect us to change what va points to.
1949 	 */
1950 	vattr = *va;
1951 
1952 	/*
1953 	 * If the pathname is "", just use dvp.  Don't need
1954 	 * to send it over the wire, look it up in the dnlc,
1955 	 * or perform any access checks.
1956 	 */
1957 	if (*nm == '\0') {
1958 		error = 0;
1959 		VN_HOLD(dvp);
1960 		vp = dvp;
1961 	/*
1962 	 * If the pathname is ".", just use dvp.  Don't need
1963 	 * to send it over the wire or look it up in the dnlc,
1964 	 * just need to check access.
1965 	 */
1966 	} else if (strcmp(nm, ".") == 0) {
1967 		error = nfs_access(dvp, VEXEC, 0, cr, ct);
1968 		if (error) {
1969 			nfs_rw_exit(&drp->r_rwlock);
1970 			return (error);
1971 		}
1972 		VN_HOLD(dvp);
1973 		vp = dvp;
1974 	/*
1975 	 * We need to go over the wire, just to be sure whether the
1976 	 * file exists or not.  Using the DNLC can be dangerous in
1977 	 * this case when making a decision regarding existence.
1978 	 */
1979 	} else {
1980 		error = nfslookup_otw(dvp, nm, &vp, cr, 0);
1981 	}
1982 	if (!error) {
1983 		if (exclusive == EXCL)
1984 			error = EEXIST;
1985 		else if (vp->v_type == VDIR && (mode & VWRITE))
1986 			error = EISDIR;
1987 		else {
1988 			/*
1989 			 * If vnode is a device, create special vnode.
1990 			 */
1991 			if (IS_DEVVP(vp)) {
1992 				tempvp = vp;
1993 				vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
1994 				VN_RELE(tempvp);
1995 			}
1996 			if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) {
1997 				if ((vattr.va_mask & AT_SIZE) &&
1998 				    vp->v_type == VREG) {
1999 					vattr.va_mask = AT_SIZE;
2000 					error = nfssetattr(vp, &vattr, 0, cr);
2001 				}
2002 			}
2003 		}
2004 		nfs_rw_exit(&drp->r_rwlock);
2005 		if (error) {
2006 			VN_RELE(vp);
2007 		} else {
2008 			/*
2009 			 * existing file got truncated, notify.
2010 			 */
2011 			vnevent_create(vp, ct);
2012 			*vpp = vp;
2013 		}
2014 		return (error);
2015 	}
2016 
2017 	ASSERT(vattr.va_mask & AT_TYPE);
2018 	if (vattr.va_type == VREG) {
2019 		ASSERT(vattr.va_mask & AT_MODE);
2020 		if (MANDMODE(vattr.va_mode)) {
2021 			nfs_rw_exit(&drp->r_rwlock);
2022 			return (EACCES);
2023 		}
2024 	}
2025 
2026 	dnlc_remove(dvp, nm);
2027 
2028 	setdiropargs(&args.ca_da, nm, dvp);
2029 
2030 	/*
2031 	 * Decide what the group-id of the created file should be.
2032 	 * Set it in attribute list as advisory...then do a setattr
2033 	 * if the server didn't get it right the first time.
2034 	 */
2035 	error = setdirgid(dvp, &vattr.va_gid, cr);
2036 	if (error) {
2037 		nfs_rw_exit(&drp->r_rwlock);
2038 		return (error);
2039 	}
2040 	vattr.va_mask |= AT_GID;
2041 
2042 	/*
2043 	 * This is a completely gross hack to make mknod
2044 	 * work over the wire until we can wack the protocol
2045 	 */
2046 #define	IFCHR		0020000		/* character special */
2047 #define	IFBLK		0060000		/* block special */
2048 #define	IFSOCK		0140000		/* socket */
2049 
2050 	/*
2051 	 * dev_t is uint_t in 5.x and short in 4.x. Both 4.x
2052 	 * supports 8 bit majors. 5.x supports 14 bit majors. 5.x supports 18
2053 	 * bits in the minor number where 4.x supports 8 bits.  If the 5.x
2054 	 * minor/major numbers <= 8 bits long, compress the device
2055 	 * number before sending it. Otherwise, the 4.x server will not
2056 	 * create the device with the correct device number and nothing can be
2057 	 * done about this.
2058 	 */
2059 	if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2060 		dev_t d = vattr.va_rdev;
2061 		dev32_t dev32;
2062 
2063 		if (vattr.va_type == VCHR)
2064 			vattr.va_mode |= IFCHR;
2065 		else
2066 			vattr.va_mode |= IFBLK;
2067 
2068 		(void) cmpldev(&dev32, d);
2069 		if (dev32 & ~((SO4_MAXMAJ << L_BITSMINOR32) | SO4_MAXMIN))
2070 			vattr.va_size = (u_offset_t)dev32;
2071 		else
2072 			vattr.va_size = (u_offset_t)nfsv2_cmpdev(d);
2073 
2074 		vattr.va_mask |= AT_MODE|AT_SIZE;
2075 	} else if (vattr.va_type == VFIFO) {
2076 		vattr.va_mode |= IFCHR;		/* xtra kludge for namedpipe */
2077 		vattr.va_size = (u_offset_t)NFS_FIFO_DEV;	/* blech */
2078 		vattr.va_mask |= AT_MODE|AT_SIZE;
2079 	} else if (vattr.va_type == VSOCK) {
2080 		vattr.va_mode |= IFSOCK;
2081 		/*
2082 		 * To avoid triggering bugs in the servers set AT_SIZE
2083 		 * (all other RFS_CREATE calls set this).
2084 		 */
2085 		vattr.va_size = 0;
2086 		vattr.va_mask |= AT_MODE|AT_SIZE;
2087 	}
2088 
2089 	args.ca_sa = &args.ca_sa_buf;
2090 	error = vattr_to_sattr(&vattr, args.ca_sa);
2091 	if (error) {
2092 		/* req time field(s) overflow - return immediately */
2093 		nfs_rw_exit(&drp->r_rwlock);
2094 		return (error);
2095 	}
2096 
2097 	douprintf = 1;
2098 
2099 	t = gethrtime();
2100 
2101 	error = rfs2call(VTOMI(dvp), RFS_CREATE,
2102 	    xdr_creatargs, (caddr_t)&args,
2103 	    xdr_diropres, (caddr_t)&dr, cr,
2104 	    &douprintf, &dr.dr_status, 0, NULL);
2105 
2106 	PURGE_ATTRCACHE(dvp);	/* mod time changed */
2107 
2108 	if (!error) {
2109 		error = geterrno(dr.dr_status);
2110 		if (!error) {
2111 			if (HAVE_RDDIR_CACHE(drp))
2112 				nfs_purge_rddir_cache(dvp);
2113 			vp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
2114 			    dvp->v_vfsp, t, cr, NULL, NULL);
2115 			/*
2116 			 * If NFS_ACL is supported on the server, then the
2117 			 * attributes returned by server may have minimal
2118 			 * permissions sometimes denying access to users having
2119 			 * proper access.  To get the proper attributes, mark
2120 			 * the attributes as expired so that they will be
2121 			 * regotten via the NFS_ACL GETATTR2 procedure.
2122 			 */
2123 			if (VTOMI(vp)->mi_flags & MI_ACL) {
2124 				PURGE_ATTRCACHE(vp);
2125 			}
2126 			dnlc_update(dvp, nm, vp);
2127 			rp = VTOR(vp);
2128 			if (vattr.va_size == 0) {
2129 				mutex_enter(&rp->r_statelock);
2130 				rp->r_size = 0;
2131 				mutex_exit(&rp->r_statelock);
2132 				if (vn_has_cached_data(vp)) {
2133 					ASSERT(vp->v_type != VCHR);
2134 					nfs_invalidate_pages(vp,
2135 					    (u_offset_t)0, cr);
2136 				}
2137 			}
2138 
2139 			/*
2140 			 * Make sure the gid was set correctly.
2141 			 * If not, try to set it (but don't lose
2142 			 * any sleep over it).
2143 			 */
2144 			if (vattr.va_gid != rp->r_attr.va_gid) {
2145 				vattr.va_mask = AT_GID;
2146 				(void) nfssetattr(vp, &vattr, 0, cr);
2147 			}
2148 
2149 			/*
2150 			 * If vnode is a device create special vnode
2151 			 */
2152 			if (IS_DEVVP(vp)) {
2153 				*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2154 				VN_RELE(vp);
2155 			} else
2156 				*vpp = vp;
2157 		} else {
2158 			PURGE_STALE_FH(error, dvp, cr);
2159 		}
2160 	}
2161 
2162 	nfs_rw_exit(&drp->r_rwlock);
2163 
2164 	return (error);
2165 }
2166 
2167 /*
2168  * Weirdness: if the vnode to be removed is open
2169  * we rename it instead of removing it and nfs_inactive
2170  * will remove the new name.
2171  */
2172 /* ARGSUSED */
2173 static int
2174 nfs_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
2175 {
2176 	int error;
2177 	struct nfsdiropargs da;
2178 	enum nfsstat status;
2179 	vnode_t *vp;
2180 	char *tmpname;
2181 	int douprintf;
2182 	rnode_t *rp;
2183 	rnode_t *drp;
2184 
2185 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
2186 		return (EPERM);
2187 	drp = VTOR(dvp);
2188 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2189 		return (EINTR);
2190 
2191 	error = nfslookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2192 	if (error) {
2193 		nfs_rw_exit(&drp->r_rwlock);
2194 		return (error);
2195 	}
2196 
2197 	if (vp->v_type == VDIR && secpolicy_fs_linkdir(cr, dvp->v_vfsp)) {
2198 		VN_RELE(vp);
2199 		nfs_rw_exit(&drp->r_rwlock);
2200 		return (EPERM);
2201 	}
2202 
2203 	/*
2204 	 * First just remove the entry from the name cache, as it
2205 	 * is most likely the only entry for this vp.
2206 	 */
2207 	dnlc_remove(dvp, nm);
2208 
2209 	/*
2210 	 * If the file has a v_count > 1 then there may be more than one
2211 	 * entry in the name cache due multiple links or an open file,
2212 	 * but we don't have the real reference count so flush all
2213 	 * possible entries.
2214 	 */
2215 	if (vp->v_count > 1)
2216 		dnlc_purge_vp(vp);
2217 
2218 	/*
2219 	 * Now we have the real reference count on the vnode
2220 	 */
2221 	rp = VTOR(vp);
2222 	mutex_enter(&rp->r_statelock);
2223 	if (vp->v_count > 1 &&
2224 	    (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
2225 		mutex_exit(&rp->r_statelock);
2226 		tmpname = newname();
2227 		error = nfsrename(dvp, nm, dvp, tmpname, cr, ct);
2228 		if (error)
2229 			kmem_free(tmpname, MAXNAMELEN);
2230 		else {
2231 			mutex_enter(&rp->r_statelock);
2232 			if (rp->r_unldvp == NULL) {
2233 				VN_HOLD(dvp);
2234 				rp->r_unldvp = dvp;
2235 				if (rp->r_unlcred != NULL)
2236 					crfree(rp->r_unlcred);
2237 				crhold(cr);
2238 				rp->r_unlcred = cr;
2239 				rp->r_unlname = tmpname;
2240 			} else {
2241 				kmem_free(rp->r_unlname, MAXNAMELEN);
2242 				rp->r_unlname = tmpname;
2243 			}
2244 			mutex_exit(&rp->r_statelock);
2245 		}
2246 	} else {
2247 		mutex_exit(&rp->r_statelock);
2248 		/*
2249 		 * We need to flush any dirty pages which happen to
2250 		 * be hanging around before removing the file.  This
2251 		 * shouldn't happen very often and mostly on file
2252 		 * systems mounted "nocto".
2253 		 */
2254 		if (vn_has_cached_data(vp) &&
2255 		    ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
2256 			error = nfs_putpage(vp, (offset_t)0, 0, 0, cr, ct);
2257 			if (error && (error == ENOSPC || error == EDQUOT)) {
2258 				mutex_enter(&rp->r_statelock);
2259 				if (!rp->r_error)
2260 					rp->r_error = error;
2261 				mutex_exit(&rp->r_statelock);
2262 			}
2263 		}
2264 
2265 		setdiropargs(&da, nm, dvp);
2266 
2267 		douprintf = 1;
2268 
2269 		error = rfs2call(VTOMI(dvp), RFS_REMOVE,
2270 		    xdr_diropargs, (caddr_t)&da,
2271 		    xdr_enum, (caddr_t)&status, cr,
2272 		    &douprintf, &status, 0, NULL);
2273 
2274 		/*
2275 		 * The xattr dir may be gone after last attr is removed,
2276 		 * so flush it from dnlc.
2277 		 */
2278 		if (dvp->v_flag & V_XATTRDIR)
2279 			dnlc_purge_vp(dvp);
2280 
2281 		PURGE_ATTRCACHE(dvp);	/* mod time changed */
2282 		PURGE_ATTRCACHE(vp);	/* link count changed */
2283 
2284 		if (!error) {
2285 			error = geterrno(status);
2286 			if (!error) {
2287 				if (HAVE_RDDIR_CACHE(drp))
2288 					nfs_purge_rddir_cache(dvp);
2289 			} else {
2290 				PURGE_STALE_FH(error, dvp, cr);
2291 			}
2292 		}
2293 	}
2294 
2295 	if (error == 0) {
2296 		vnevent_remove(vp, dvp, nm, ct);
2297 	}
2298 	VN_RELE(vp);
2299 
2300 	nfs_rw_exit(&drp->r_rwlock);
2301 
2302 	return (error);
2303 }
2304 
2305 /* ARGSUSED */
2306 static int
2307 nfs_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
2308 	caller_context_t *ct, int flags)
2309 {
2310 	int error;
2311 	struct nfslinkargs args;
2312 	enum nfsstat status;
2313 	vnode_t *realvp;
2314 	int douprintf;
2315 	rnode_t *tdrp;
2316 
2317 	if (nfs_zone() != VTOMI(tdvp)->mi_zone)
2318 		return (EPERM);
2319 	if (VOP_REALVP(svp, &realvp, ct) == 0)
2320 		svp = realvp;
2321 
2322 	args.la_from = VTOFH(svp);
2323 	setdiropargs(&args.la_to, tnm, tdvp);
2324 
2325 	tdrp = VTOR(tdvp);
2326 	if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR(tdvp)))
2327 		return (EINTR);
2328 
2329 	dnlc_remove(tdvp, tnm);
2330 
2331 	douprintf = 1;
2332 
2333 	error = rfs2call(VTOMI(svp), RFS_LINK,
2334 	    xdr_linkargs, (caddr_t)&args,
2335 	    xdr_enum, (caddr_t)&status, cr,
2336 	    &douprintf, &status, 0, NULL);
2337 
2338 	PURGE_ATTRCACHE(tdvp);	/* mod time changed */
2339 	PURGE_ATTRCACHE(svp);	/* link count changed */
2340 
2341 	if (!error) {
2342 		error = geterrno(status);
2343 		if (!error) {
2344 			if (HAVE_RDDIR_CACHE(tdrp))
2345 				nfs_purge_rddir_cache(tdvp);
2346 		}
2347 	}
2348 
2349 	nfs_rw_exit(&tdrp->r_rwlock);
2350 
2351 	if (!error) {
2352 		/*
2353 		 * Notify the source file of this link operation.
2354 		 */
2355 		vnevent_link(svp, ct);
2356 	}
2357 	return (error);
2358 }
2359 
2360 /* ARGSUSED */
2361 static int
2362 nfs_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
2363 	caller_context_t *ct, int flags)
2364 {
2365 	vnode_t *realvp;
2366 
2367 	if (nfs_zone() != VTOMI(odvp)->mi_zone)
2368 		return (EPERM);
2369 	if (VOP_REALVP(ndvp, &realvp, ct) == 0)
2370 		ndvp = realvp;
2371 
2372 	return (nfsrename(odvp, onm, ndvp, nnm, cr, ct));
2373 }
2374 
2375 /*
2376  * nfsrename does the real work of renaming in NFS Version 2.
2377  */
2378 static int
2379 nfsrename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
2380     caller_context_t *ct)
2381 {
2382 	int error;
2383 	enum nfsstat status;
2384 	struct nfsrnmargs args;
2385 	int douprintf;
2386 	vnode_t *nvp = NULL;
2387 	vnode_t *ovp = NULL;
2388 	char *tmpname;
2389 	rnode_t *rp;
2390 	rnode_t *odrp;
2391 	rnode_t *ndrp;
2392 
2393 	ASSERT(nfs_zone() == VTOMI(odvp)->mi_zone);
2394 	if (strcmp(onm, ".") == 0 || strcmp(onm, "..") == 0 ||
2395 	    strcmp(nnm, ".") == 0 || strcmp(nnm, "..") == 0)
2396 		return (EINVAL);
2397 
2398 	odrp = VTOR(odvp);
2399 	ndrp = VTOR(ndvp);
2400 	if ((intptr_t)odrp < (intptr_t)ndrp) {
2401 		if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp)))
2402 			return (EINTR);
2403 		if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp))) {
2404 			nfs_rw_exit(&odrp->r_rwlock);
2405 			return (EINTR);
2406 		}
2407 	} else {
2408 		if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp)))
2409 			return (EINTR);
2410 		if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp))) {
2411 			nfs_rw_exit(&ndrp->r_rwlock);
2412 			return (EINTR);
2413 		}
2414 	}
2415 
2416 	/*
2417 	 * Lookup the target file.  If it exists, it needs to be
2418 	 * checked to see whether it is a mount point and whether
2419 	 * it is active (open).
2420 	 */
2421 	error = nfslookup(ndvp, nnm, &nvp, NULL, 0, NULL, cr, 0);
2422 	if (!error) {
2423 		/*
2424 		 * If this file has been mounted on, then just
2425 		 * return busy because renaming to it would remove
2426 		 * the mounted file system from the name space.
2427 		 */
2428 		if (vn_mountedvfs(nvp) != NULL) {
2429 			VN_RELE(nvp);
2430 			nfs_rw_exit(&odrp->r_rwlock);
2431 			nfs_rw_exit(&ndrp->r_rwlock);
2432 			return (EBUSY);
2433 		}
2434 
2435 		/*
2436 		 * Purge the name cache of all references to this vnode
2437 		 * so that we can check the reference count to infer
2438 		 * whether it is active or not.
2439 		 */
2440 		/*
2441 		 * First just remove the entry from the name cache, as it
2442 		 * is most likely the only entry for this vp.
2443 		 */
2444 		dnlc_remove(ndvp, nnm);
2445 		/*
2446 		 * If the file has a v_count > 1 then there may be more
2447 		 * than one entry in the name cache due multiple links
2448 		 * or an open file, but we don't have the real reference
2449 		 * count so flush all possible entries.
2450 		 */
2451 		if (nvp->v_count > 1)
2452 			dnlc_purge_vp(nvp);
2453 
2454 		/*
2455 		 * If the vnode is active and is not a directory,
2456 		 * arrange to rename it to a
2457 		 * temporary file so that it will continue to be
2458 		 * accessible.  This implements the "unlink-open-file"
2459 		 * semantics for the target of a rename operation.
2460 		 * Before doing this though, make sure that the
2461 		 * source and target files are not already the same.
2462 		 */
2463 		if (nvp->v_count > 1 && nvp->v_type != VDIR) {
2464 			/*
2465 			 * Lookup the source name.
2466 			 */
2467 			error = nfslookup(odvp, onm, &ovp, NULL, 0, NULL,
2468 			    cr, 0);
2469 
2470 			/*
2471 			 * The source name *should* already exist.
2472 			 */
2473 			if (error) {
2474 				VN_RELE(nvp);
2475 				nfs_rw_exit(&odrp->r_rwlock);
2476 				nfs_rw_exit(&ndrp->r_rwlock);
2477 				return (error);
2478 			}
2479 
2480 			/*
2481 			 * Compare the two vnodes.  If they are the same,
2482 			 * just release all held vnodes and return success.
2483 			 */
2484 			if (ovp == nvp) {
2485 				VN_RELE(ovp);
2486 				VN_RELE(nvp);
2487 				nfs_rw_exit(&odrp->r_rwlock);
2488 				nfs_rw_exit(&ndrp->r_rwlock);
2489 				return (0);
2490 			}
2491 
2492 			/*
2493 			 * Can't mix and match directories and non-
2494 			 * directories in rename operations.  We already
2495 			 * know that the target is not a directory.  If
2496 			 * the source is a directory, return an error.
2497 			 */
2498 			if (ovp->v_type == VDIR) {
2499 				VN_RELE(ovp);
2500 				VN_RELE(nvp);
2501 				nfs_rw_exit(&odrp->r_rwlock);
2502 				nfs_rw_exit(&ndrp->r_rwlock);
2503 				return (ENOTDIR);
2504 			}
2505 
2506 			/*
2507 			 * The target file exists, is not the same as
2508 			 * the source file, and is active.  Link it
2509 			 * to a temporary filename to avoid having
2510 			 * the server removing the file completely.
2511 			 */
2512 			tmpname = newname();
2513 			error = nfs_link(ndvp, nvp, tmpname, cr, NULL, 0);
2514 			if (error == EOPNOTSUPP) {
2515 				error = nfs_rename(ndvp, nnm, ndvp, tmpname,
2516 				    cr, NULL, 0);
2517 			}
2518 			if (error) {
2519 				kmem_free(tmpname, MAXNAMELEN);
2520 				VN_RELE(ovp);
2521 				VN_RELE(nvp);
2522 				nfs_rw_exit(&odrp->r_rwlock);
2523 				nfs_rw_exit(&ndrp->r_rwlock);
2524 				return (error);
2525 			}
2526 			rp = VTOR(nvp);
2527 			mutex_enter(&rp->r_statelock);
2528 			if (rp->r_unldvp == NULL) {
2529 				VN_HOLD(ndvp);
2530 				rp->r_unldvp = ndvp;
2531 				if (rp->r_unlcred != NULL)
2532 					crfree(rp->r_unlcred);
2533 				crhold(cr);
2534 				rp->r_unlcred = cr;
2535 				rp->r_unlname = tmpname;
2536 			} else {
2537 				kmem_free(rp->r_unlname, MAXNAMELEN);
2538 				rp->r_unlname = tmpname;
2539 			}
2540 			mutex_exit(&rp->r_statelock);
2541 		}
2542 	}
2543 
2544 	if (ovp == NULL) {
2545 		/*
2546 		 * When renaming directories to be a subdirectory of a
2547 		 * different parent, the dnlc entry for ".." will no
2548 		 * longer be valid, so it must be removed.
2549 		 *
2550 		 * We do a lookup here to determine whether we are renaming
2551 		 * a directory and we need to check if we are renaming
2552 		 * an unlinked file.  This might have already been done
2553 		 * in previous code, so we check ovp == NULL to avoid
2554 		 * doing it twice.
2555 		 */
2556 
2557 		error = nfslookup(odvp, onm, &ovp, NULL, 0, NULL, cr, 0);
2558 
2559 		/*
2560 		 * The source name *should* already exist.
2561 		 */
2562 		if (error) {
2563 			nfs_rw_exit(&odrp->r_rwlock);
2564 			nfs_rw_exit(&ndrp->r_rwlock);
2565 			if (nvp) {
2566 				VN_RELE(nvp);
2567 			}
2568 			return (error);
2569 		}
2570 		ASSERT(ovp != NULL);
2571 	}
2572 
2573 	dnlc_remove(odvp, onm);
2574 	dnlc_remove(ndvp, nnm);
2575 
2576 	setdiropargs(&args.rna_from, onm, odvp);
2577 	setdiropargs(&args.rna_to, nnm, ndvp);
2578 
2579 	douprintf = 1;
2580 
2581 	error = rfs2call(VTOMI(odvp), RFS_RENAME,
2582 	    xdr_rnmargs, (caddr_t)&args,
2583 	    xdr_enum, (caddr_t)&status, cr,
2584 	    &douprintf, &status, 0, NULL);
2585 
2586 	PURGE_ATTRCACHE(odvp);	/* mod time changed */
2587 	PURGE_ATTRCACHE(ndvp);	/* mod time changed */
2588 
2589 	if (!error) {
2590 		error = geterrno(status);
2591 		if (!error) {
2592 			if (HAVE_RDDIR_CACHE(odrp))
2593 				nfs_purge_rddir_cache(odvp);
2594 			if (HAVE_RDDIR_CACHE(ndrp))
2595 				nfs_purge_rddir_cache(ndvp);
2596 			/*
2597 			 * when renaming directories to be a subdirectory of a
2598 			 * different parent, the dnlc entry for ".." will no
2599 			 * longer be valid, so it must be removed
2600 			 */
2601 			rp = VTOR(ovp);
2602 			if (ndvp != odvp) {
2603 				if (ovp->v_type == VDIR) {
2604 					dnlc_remove(ovp, "..");
2605 					if (HAVE_RDDIR_CACHE(rp))
2606 						nfs_purge_rddir_cache(ovp);
2607 				}
2608 			}
2609 
2610 			/*
2611 			 * If we are renaming the unlinked file, update the
2612 			 * r_unldvp and r_unlname as needed.
2613 			 */
2614 			mutex_enter(&rp->r_statelock);
2615 			if (rp->r_unldvp != NULL) {
2616 				if (strcmp(rp->r_unlname, onm) == 0) {
2617 					(void) strncpy(rp->r_unlname,
2618 					    nnm, MAXNAMELEN);
2619 					rp->r_unlname[MAXNAMELEN - 1] = '\0';
2620 
2621 					if (ndvp != rp->r_unldvp) {
2622 						VN_RELE(rp->r_unldvp);
2623 						rp->r_unldvp = ndvp;
2624 						VN_HOLD(ndvp);
2625 					}
2626 				}
2627 			}
2628 			mutex_exit(&rp->r_statelock);
2629 		} else {
2630 			/*
2631 			 * System V defines rename to return EEXIST, not
2632 			 * ENOTEMPTY if the target directory is not empty.
2633 			 * Over the wire, the error is NFSERR_ENOTEMPTY
2634 			 * which geterrno maps to ENOTEMPTY.
2635 			 */
2636 			if (error == ENOTEMPTY)
2637 				error = EEXIST;
2638 		}
2639 	}
2640 
2641 	if (error == 0) {
2642 		if (nvp)
2643 			vnevent_rename_dest(nvp, ndvp, nnm, ct);
2644 
2645 		if (odvp != ndvp)
2646 			vnevent_rename_dest_dir(ndvp, ct);
2647 
2648 		ASSERT(ovp != NULL);
2649 		vnevent_rename_src(ovp, odvp, onm, ct);
2650 	}
2651 
2652 	if (nvp) {
2653 		VN_RELE(nvp);
2654 	}
2655 	VN_RELE(ovp);
2656 
2657 	nfs_rw_exit(&odrp->r_rwlock);
2658 	nfs_rw_exit(&ndrp->r_rwlock);
2659 
2660 	return (error);
2661 }
2662 
2663 /* ARGSUSED */
2664 static int
2665 nfs_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
2666 	caller_context_t *ct, int flags, vsecattr_t *vsecp)
2667 {
2668 	int error;
2669 	struct nfscreatargs args;
2670 	struct nfsdiropres dr;
2671 	int douprintf;
2672 	rnode_t *drp;
2673 	hrtime_t t;
2674 
2675 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
2676 		return (EPERM);
2677 
2678 	setdiropargs(&args.ca_da, nm, dvp);
2679 
2680 	/*
2681 	 * Decide what the group-id and set-gid bit of the created directory
2682 	 * should be.  May have to do a setattr to get the gid right.
2683 	 */
2684 	error = setdirgid(dvp, &va->va_gid, cr);
2685 	if (error)
2686 		return (error);
2687 	error = setdirmode(dvp, &va->va_mode, cr);
2688 	if (error)
2689 		return (error);
2690 	va->va_mask |= AT_MODE|AT_GID;
2691 
2692 	args.ca_sa = &args.ca_sa_buf;
2693 	error = vattr_to_sattr(va, args.ca_sa);
2694 	if (error) {
2695 		/* req time field(s) overflow - return immediately */
2696 		return (error);
2697 	}
2698 
2699 	drp = VTOR(dvp);
2700 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2701 		return (EINTR);
2702 
2703 	dnlc_remove(dvp, nm);
2704 
2705 	douprintf = 1;
2706 
2707 	t = gethrtime();
2708 
2709 	error = rfs2call(VTOMI(dvp), RFS_MKDIR,
2710 	    xdr_creatargs, (caddr_t)&args,
2711 	    xdr_diropres, (caddr_t)&dr, cr,
2712 	    &douprintf, &dr.dr_status, 0, NULL);
2713 
2714 	PURGE_ATTRCACHE(dvp);	/* mod time changed */
2715 
2716 	if (!error) {
2717 		error = geterrno(dr.dr_status);
2718 		if (!error) {
2719 			if (HAVE_RDDIR_CACHE(drp))
2720 				nfs_purge_rddir_cache(dvp);
2721 			/*
2722 			 * The attributes returned by RFS_MKDIR can not
2723 			 * be depended upon, so mark the attribute cache
2724 			 * as purged.  A subsequent GETATTR will get the
2725 			 * correct attributes from the server.
2726 			 */
2727 			*vpp = makenfsnode(&dr.dr_fhandle, &dr.dr_attr,
2728 			    dvp->v_vfsp, t, cr, NULL, NULL);
2729 			PURGE_ATTRCACHE(*vpp);
2730 			dnlc_update(dvp, nm, *vpp);
2731 
2732 			/*
2733 			 * Make sure the gid was set correctly.
2734 			 * If not, try to set it (but don't lose
2735 			 * any sleep over it).
2736 			 */
2737 			if (va->va_gid != VTOR(*vpp)->r_attr.va_gid) {
2738 				va->va_mask = AT_GID;
2739 				(void) nfssetattr(*vpp, va, 0, cr);
2740 			}
2741 		} else {
2742 			PURGE_STALE_FH(error, dvp, cr);
2743 		}
2744 	}
2745 
2746 	nfs_rw_exit(&drp->r_rwlock);
2747 
2748 	return (error);
2749 }
2750 
2751 /* ARGSUSED */
2752 static int
2753 nfs_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
2754 	caller_context_t *ct, int flags)
2755 {
2756 	int error;
2757 	enum nfsstat status;
2758 	struct nfsdiropargs da;
2759 	vnode_t *vp;
2760 	int douprintf;
2761 	rnode_t *drp;
2762 
2763 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
2764 		return (EPERM);
2765 	drp = VTOR(dvp);
2766 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2767 		return (EINTR);
2768 
2769 	/*
2770 	 * Attempt to prevent a rmdir(".") from succeeding.
2771 	 */
2772 	error = nfslookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2773 	if (error) {
2774 		nfs_rw_exit(&drp->r_rwlock);
2775 		return (error);
2776 	}
2777 
2778 	if (vp == cdir) {
2779 		VN_RELE(vp);
2780 		nfs_rw_exit(&drp->r_rwlock);
2781 		return (EINVAL);
2782 	}
2783 
2784 	setdiropargs(&da, nm, dvp);
2785 
2786 	/*
2787 	 * First just remove the entry from the name cache, as it
2788 	 * is most likely an entry for this vp.
2789 	 */
2790 	dnlc_remove(dvp, nm);
2791 
2792 	/*
2793 	 * If there vnode reference count is greater than one, then
2794 	 * there may be additional references in the DNLC which will
2795 	 * need to be purged.  First, trying removing the entry for
2796 	 * the parent directory and see if that removes the additional
2797 	 * reference(s).  If that doesn't do it, then use dnlc_purge_vp
2798 	 * to completely remove any references to the directory which
2799 	 * might still exist in the DNLC.
2800 	 */
2801 	if (vp->v_count > 1) {
2802 		dnlc_remove(vp, "..");
2803 		if (vp->v_count > 1)
2804 			dnlc_purge_vp(vp);
2805 	}
2806 
2807 	douprintf = 1;
2808 
2809 	error = rfs2call(VTOMI(dvp), RFS_RMDIR,
2810 	    xdr_diropargs, (caddr_t)&da,
2811 	    xdr_enum, (caddr_t)&status, cr,
2812 	    &douprintf, &status, 0, NULL);
2813 
2814 	PURGE_ATTRCACHE(dvp);	/* mod time changed */
2815 
2816 	if (error) {
2817 		VN_RELE(vp);
2818 		nfs_rw_exit(&drp->r_rwlock);
2819 		return (error);
2820 	}
2821 
2822 	error = geterrno(status);
2823 	if (!error) {
2824 		if (HAVE_RDDIR_CACHE(drp))
2825 			nfs_purge_rddir_cache(dvp);
2826 		if (HAVE_RDDIR_CACHE(VTOR(vp)))
2827 			nfs_purge_rddir_cache(vp);
2828 	} else {
2829 		PURGE_STALE_FH(error, dvp, cr);
2830 		/*
2831 		 * System V defines rmdir to return EEXIST, not
2832 		 * ENOTEMPTY if the directory is not empty.  Over
2833 		 * the wire, the error is NFSERR_ENOTEMPTY which
2834 		 * geterrno maps to ENOTEMPTY.
2835 		 */
2836 		if (error == ENOTEMPTY)
2837 			error = EEXIST;
2838 	}
2839 
2840 	if (error == 0) {
2841 		vnevent_rmdir(vp, dvp, nm, ct);
2842 	}
2843 	VN_RELE(vp);
2844 
2845 	nfs_rw_exit(&drp->r_rwlock);
2846 
2847 	return (error);
2848 }
2849 
2850 /* ARGSUSED */
2851 static int
2852 nfs_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
2853 	caller_context_t *ct, int flags)
2854 {
2855 	int error;
2856 	struct nfsslargs args;
2857 	enum nfsstat status;
2858 	int douprintf;
2859 	rnode_t *drp;
2860 
2861 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
2862 		return (EPERM);
2863 	setdiropargs(&args.sla_from, lnm, dvp);
2864 	args.sla_sa = &args.sla_sa_buf;
2865 	error = vattr_to_sattr(tva, args.sla_sa);
2866 	if (error) {
2867 		/* req time field(s) overflow - return immediately */
2868 		return (error);
2869 	}
2870 	args.sla_tnm = tnm;
2871 
2872 	drp = VTOR(dvp);
2873 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2874 		return (EINTR);
2875 
2876 	dnlc_remove(dvp, lnm);
2877 
2878 	douprintf = 1;
2879 
2880 	error = rfs2call(VTOMI(dvp), RFS_SYMLINK,
2881 	    xdr_slargs, (caddr_t)&args,
2882 	    xdr_enum, (caddr_t)&status, cr,
2883 	    &douprintf, &status, 0, NULL);
2884 
2885 	PURGE_ATTRCACHE(dvp);	/* mod time changed */
2886 
2887 	if (!error) {
2888 		error = geterrno(status);
2889 		if (!error) {
2890 			if (HAVE_RDDIR_CACHE(drp))
2891 				nfs_purge_rddir_cache(dvp);
2892 		} else {
2893 			PURGE_STALE_FH(error, dvp, cr);
2894 		}
2895 	}
2896 
2897 	nfs_rw_exit(&drp->r_rwlock);
2898 
2899 	return (error);
2900 }
2901 
2902 #ifdef DEBUG
2903 static int nfs_readdir_cache_hits = 0;
2904 static int nfs_readdir_cache_shorts = 0;
2905 static int nfs_readdir_cache_waits = 0;
2906 static int nfs_readdir_cache_misses = 0;
2907 static int nfs_readdir_readahead = 0;
2908 #endif
2909 
2910 static int nfs_shrinkreaddir = 0;
2911 
2912 /*
2913  * Read directory entries.
2914  * There are some weird things to look out for here.  The uio_offset
2915  * field is either 0 or it is the offset returned from a previous
2916  * readdir.  It is an opaque value used by the server to find the
2917  * correct directory block to read. The count field is the number
2918  * of blocks to read on the server.  This is advisory only, the server
2919  * may return only one block's worth of entries.  Entries may be compressed
2920  * on the server.
2921  */
2922 /* ARGSUSED */
2923 static int
2924 nfs_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
2925 	caller_context_t *ct, int flags)
2926 {
2927 	int error;
2928 	size_t count;
2929 	rnode_t *rp;
2930 	rddir_cache *rdc;
2931 	rddir_cache *nrdc;
2932 	rddir_cache *rrdc;
2933 #ifdef DEBUG
2934 	int missed;
2935 #endif
2936 	rddir_cache srdc;
2937 	avl_index_t where;
2938 
2939 	rp = VTOR(vp);
2940 
2941 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
2942 	if (nfs_zone() != VTOMI(vp)->mi_zone)
2943 		return (EIO);
2944 	/*
2945 	 * Make sure that the directory cache is valid.
2946 	 */
2947 	if (HAVE_RDDIR_CACHE(rp)) {
2948 		if (nfs_disable_rddir_cache) {
2949 			/*
2950 			 * Setting nfs_disable_rddir_cache in /etc/system
2951 			 * allows interoperability with servers that do not
2952 			 * properly update the attributes of directories.
2953 			 * Any cached information gets purged before an
2954 			 * access is made to it.
2955 			 */
2956 			nfs_purge_rddir_cache(vp);
2957 		} else {
2958 			error = nfs_validate_caches(vp, cr);
2959 			if (error)
2960 				return (error);
2961 		}
2962 	}
2963 
2964 	/*
2965 	 * UGLINESS: SunOS 3.2 servers apparently cannot always handle an
2966 	 * RFS_READDIR request with rda_count set to more than 0x400. So
2967 	 * we reduce the request size here purely for compatibility.
2968 	 *
2969 	 * In general, this is no longer required.  However, if a server
2970 	 * is discovered which can not handle requests larger than 1024,
2971 	 * nfs_shrinkreaddir can be set to 1 to enable this backwards
2972 	 * compatibility.
2973 	 *
2974 	 * In any case, the request size is limited to NFS_MAXDATA bytes.
2975 	 */
2976 	count = MIN(uiop->uio_iov->iov_len,
2977 	    nfs_shrinkreaddir ? 0x400 : NFS_MAXDATA);
2978 
2979 	nrdc = NULL;
2980 #ifdef DEBUG
2981 	missed = 0;
2982 #endif
2983 top:
2984 	/*
2985 	 * Short circuit last readdir which always returns 0 bytes.
2986 	 * This can be done after the directory has been read through
2987 	 * completely at least once.  This will set r_direof which
2988 	 * can be used to find the value of the last cookie.
2989 	 */
2990 	mutex_enter(&rp->r_statelock);
2991 	if (rp->r_direof != NULL &&
2992 	    uiop->uio_offset == rp->r_direof->nfs_ncookie) {
2993 		mutex_exit(&rp->r_statelock);
2994 #ifdef DEBUG
2995 		nfs_readdir_cache_shorts++;
2996 #endif
2997 		if (eofp)
2998 			*eofp = 1;
2999 		if (nrdc != NULL)
3000 			rddir_cache_rele(nrdc);
3001 		return (0);
3002 	}
3003 	/*
3004 	 * Look for a cache entry.  Cache entries are identified
3005 	 * by the NFS cookie value and the byte count requested.
3006 	 */
3007 	srdc.nfs_cookie = uiop->uio_offset;
3008 	srdc.buflen = count;
3009 	rdc = avl_find(&rp->r_dir, &srdc, &where);
3010 	if (rdc != NULL) {
3011 		rddir_cache_hold(rdc);
3012 		/*
3013 		 * If the cache entry is in the process of being
3014 		 * filled in, wait until this completes.  The
3015 		 * RDDIRWAIT bit is set to indicate that someone
3016 		 * is waiting and then the thread currently
3017 		 * filling the entry is done, it should do a
3018 		 * cv_broadcast to wakeup all of the threads
3019 		 * waiting for it to finish.
3020 		 */
3021 		if (rdc->flags & RDDIR) {
3022 			nfs_rw_exit(&rp->r_rwlock);
3023 			rdc->flags |= RDDIRWAIT;
3024 #ifdef DEBUG
3025 			nfs_readdir_cache_waits++;
3026 #endif
3027 			if (!cv_wait_sig(&rdc->cv, &rp->r_statelock)) {
3028 				/*
3029 				 * We got interrupted, probably
3030 				 * the user typed ^C or an alarm
3031 				 * fired.  We free the new entry
3032 				 * if we allocated one.
3033 				 */
3034 				mutex_exit(&rp->r_statelock);
3035 				(void) nfs_rw_enter_sig(&rp->r_rwlock,
3036 				    RW_READER, FALSE);
3037 				rddir_cache_rele(rdc);
3038 				if (nrdc != NULL)
3039 					rddir_cache_rele(nrdc);
3040 				return (EINTR);
3041 			}
3042 			mutex_exit(&rp->r_statelock);
3043 			(void) nfs_rw_enter_sig(&rp->r_rwlock,
3044 			    RW_READER, FALSE);
3045 			rddir_cache_rele(rdc);
3046 			goto top;
3047 		}
3048 		/*
3049 		 * Check to see if a readdir is required to
3050 		 * fill the entry.  If so, mark this entry
3051 		 * as being filled, remove our reference,
3052 		 * and branch to the code to fill the entry.
3053 		 */
3054 		if (rdc->flags & RDDIRREQ) {
3055 			rdc->flags &= ~RDDIRREQ;
3056 			rdc->flags |= RDDIR;
3057 			if (nrdc != NULL)
3058 				rddir_cache_rele(nrdc);
3059 			nrdc = rdc;
3060 			mutex_exit(&rp->r_statelock);
3061 			goto bottom;
3062 		}
3063 #ifdef DEBUG
3064 		if (!missed)
3065 			nfs_readdir_cache_hits++;
3066 #endif
3067 		/*
3068 		 * If an error occurred while attempting
3069 		 * to fill the cache entry, just return it.
3070 		 */
3071 		if (rdc->error) {
3072 			error = rdc->error;
3073 			mutex_exit(&rp->r_statelock);
3074 			rddir_cache_rele(rdc);
3075 			if (nrdc != NULL)
3076 				rddir_cache_rele(nrdc);
3077 			return (error);
3078 		}
3079 
3080 		/*
3081 		 * The cache entry is complete and good,
3082 		 * copyout the dirent structs to the calling
3083 		 * thread.
3084 		 */
3085 		error = uiomove(rdc->entries, rdc->entlen, UIO_READ, uiop);
3086 
3087 		/*
3088 		 * If no error occurred during the copyout,
3089 		 * update the offset in the uio struct to
3090 		 * contain the value of the next cookie
3091 		 * and set the eof value appropriately.
3092 		 */
3093 		if (!error) {
3094 			uiop->uio_offset = rdc->nfs_ncookie;
3095 			if (eofp)
3096 				*eofp = rdc->eof;
3097 		}
3098 
3099 		/*
3100 		 * Decide whether to do readahead.  Don't if
3101 		 * have already read to the end of directory.
3102 		 */
3103 		if (rdc->eof) {
3104 			rp->r_direof = rdc;
3105 			mutex_exit(&rp->r_statelock);
3106 			rddir_cache_rele(rdc);
3107 			if (nrdc != NULL)
3108 				rddir_cache_rele(nrdc);
3109 			return (error);
3110 		}
3111 
3112 		/*
3113 		 * Check to see whether we found an entry
3114 		 * for the readahead.  If so, we don't need
3115 		 * to do anything further, so free the new
3116 		 * entry if one was allocated.  Otherwise,
3117 		 * allocate a new entry, add it to the cache,
3118 		 * and then initiate an asynchronous readdir
3119 		 * operation to fill it.
3120 		 */
3121 		srdc.nfs_cookie = rdc->nfs_ncookie;
3122 		srdc.buflen = count;
3123 		rrdc = avl_find(&rp->r_dir, &srdc, &where);
3124 		if (rrdc != NULL) {
3125 			if (nrdc != NULL)
3126 				rddir_cache_rele(nrdc);
3127 		} else {
3128 			if (nrdc != NULL)
3129 				rrdc = nrdc;
3130 			else {
3131 				rrdc = rddir_cache_alloc(KM_NOSLEEP);
3132 			}
3133 			if (rrdc != NULL) {
3134 				rrdc->nfs_cookie = rdc->nfs_ncookie;
3135 				rrdc->buflen = count;
3136 				avl_insert(&rp->r_dir, rrdc, where);
3137 				rddir_cache_hold(rrdc);
3138 				mutex_exit(&rp->r_statelock);
3139 				rddir_cache_rele(rdc);
3140 #ifdef DEBUG
3141 				nfs_readdir_readahead++;
3142 #endif
3143 				nfs_async_readdir(vp, rrdc, cr, nfsreaddir);
3144 				return (error);
3145 			}
3146 		}
3147 
3148 		mutex_exit(&rp->r_statelock);
3149 		rddir_cache_rele(rdc);
3150 		return (error);
3151 	}
3152 
3153 	/*
3154 	 * Didn't find an entry in the cache.  Construct a new empty
3155 	 * entry and link it into the cache.  Other processes attempting
3156 	 * to access this entry will need to wait until it is filled in.
3157 	 *
3158 	 * Since kmem_alloc may block, another pass through the cache
3159 	 * will need to be taken to make sure that another process
3160 	 * hasn't already added an entry to the cache for this request.
3161 	 */
3162 	if (nrdc == NULL) {
3163 		mutex_exit(&rp->r_statelock);
3164 		nrdc = rddir_cache_alloc(KM_SLEEP);
3165 		nrdc->nfs_cookie = uiop->uio_offset;
3166 		nrdc->buflen = count;
3167 		goto top;
3168 	}
3169 
3170 	/*
3171 	 * Add this entry to the cache.
3172 	 */
3173 	avl_insert(&rp->r_dir, nrdc, where);
3174 	rddir_cache_hold(nrdc);
3175 	mutex_exit(&rp->r_statelock);
3176 
3177 bottom:
3178 #ifdef DEBUG
3179 	missed = 1;
3180 	nfs_readdir_cache_misses++;
3181 #endif
3182 	/*
3183 	 * Do the readdir.
3184 	 */
3185 	error = nfsreaddir(vp, nrdc, cr);
3186 
3187 	/*
3188 	 * If this operation failed, just return the error which occurred.
3189 	 */
3190 	if (error != 0)
3191 		return (error);
3192 
3193 	/*
3194 	 * Since the RPC operation will have taken sometime and blocked
3195 	 * this process, another pass through the cache will need to be
3196 	 * taken to find the correct cache entry.  It is possible that
3197 	 * the correct cache entry will not be there (although one was
3198 	 * added) because the directory changed during the RPC operation
3199 	 * and the readdir cache was flushed.  In this case, just start
3200 	 * over.  It is hoped that this will not happen too often... :-)
3201 	 */
3202 	nrdc = NULL;
3203 	goto top;
3204 	/* NOTREACHED */
3205 }
3206 
3207 static int
3208 nfsreaddir(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
3209 {
3210 	int error;
3211 	struct nfsrddirargs rda;
3212 	struct nfsrddirres rd;
3213 	rnode_t *rp;
3214 	mntinfo_t *mi;
3215 	uint_t count;
3216 	int douprintf;
3217 	failinfo_t fi, *fip;
3218 
3219 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
3220 	count = rdc->buflen;
3221 
3222 	rp = VTOR(vp);
3223 	mi = VTOMI(vp);
3224 
3225 	rda.rda_fh = *VTOFH(vp);
3226 	rda.rda_offset = rdc->nfs_cookie;
3227 
3228 	/*
3229 	 * NFS client failover support
3230 	 * suppress failover unless we have a zero cookie
3231 	 */
3232 	if (rdc->nfs_cookie == (off_t)0) {
3233 		fi.vp = vp;
3234 		fi.fhp = (caddr_t)&rda.rda_fh;
3235 		fi.copyproc = nfscopyfh;
3236 		fi.lookupproc = nfslookup;
3237 		fi.xattrdirproc = acl_getxattrdir2;
3238 		fip = &fi;
3239 	} else {
3240 		fip = NULL;
3241 	}
3242 
3243 	rd.rd_entries = kmem_alloc(rdc->buflen, KM_SLEEP);
3244 	rd.rd_size = count;
3245 	rd.rd_offset = rda.rda_offset;
3246 
3247 	douprintf = 1;
3248 
3249 	if (mi->mi_io_kstats) {
3250 		mutex_enter(&mi->mi_lock);
3251 		kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
3252 		mutex_exit(&mi->mi_lock);
3253 	}
3254 
3255 	do {
3256 		rda.rda_count = MIN(count, mi->mi_curread);
3257 		error = rfs2call(mi, RFS_READDIR,
3258 		    xdr_rddirargs, (caddr_t)&rda,
3259 		    xdr_getrddirres, (caddr_t)&rd, cr,
3260 		    &douprintf, &rd.rd_status, 0, fip);
3261 	} while (error == ENFS_TRYAGAIN);
3262 
3263 	if (mi->mi_io_kstats) {
3264 		mutex_enter(&mi->mi_lock);
3265 		kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
3266 		mutex_exit(&mi->mi_lock);
3267 	}
3268 
3269 	/*
3270 	 * Since we are actually doing a READDIR RPC, we must have
3271 	 * exclusive access to the cache entry being filled.  Thus,
3272 	 * it is safe to update all fields except for the flags
3273 	 * field.  The r_statelock in the rnode must be held to
3274 	 * prevent two different threads from simultaneously
3275 	 * attempting to update the flags field.  This can happen
3276 	 * if we are turning off RDDIR and the other thread is
3277 	 * trying to set RDDIRWAIT.
3278 	 */
3279 	ASSERT(rdc->flags & RDDIR);
3280 	if (!error) {
3281 		error = geterrno(rd.rd_status);
3282 		if (!error) {
3283 			rdc->nfs_ncookie = rd.rd_offset;
3284 			rdc->eof = rd.rd_eof ? 1 : 0;
3285 			rdc->entlen = rd.rd_size;
3286 			ASSERT(rdc->entlen <= rdc->buflen);
3287 #ifdef DEBUG
3288 			rdc->entries = rddir_cache_buf_alloc(rdc->buflen,
3289 			    KM_SLEEP);
3290 #else
3291 			rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP);
3292 #endif
3293 			bcopy(rd.rd_entries, rdc->entries, rdc->entlen);
3294 			rdc->error = 0;
3295 			if (mi->mi_io_kstats) {
3296 				mutex_enter(&mi->mi_lock);
3297 				KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
3298 				KSTAT_IO_PTR(mi->mi_io_kstats)->nread +=
3299 				    rd.rd_size;
3300 				mutex_exit(&mi->mi_lock);
3301 			}
3302 		} else {
3303 			PURGE_STALE_FH(error, vp, cr);
3304 		}
3305 	}
3306 	if (error) {
3307 		rdc->entries = NULL;
3308 		rdc->error = error;
3309 	}
3310 	kmem_free(rd.rd_entries, rdc->buflen);
3311 
3312 	mutex_enter(&rp->r_statelock);
3313 	rdc->flags &= ~RDDIR;
3314 	if (rdc->flags & RDDIRWAIT) {
3315 		rdc->flags &= ~RDDIRWAIT;
3316 		cv_broadcast(&rdc->cv);
3317 	}
3318 	if (error)
3319 		rdc->flags |= RDDIRREQ;
3320 	mutex_exit(&rp->r_statelock);
3321 
3322 	rddir_cache_rele(rdc);
3323 
3324 	return (error);
3325 }
3326 
3327 #ifdef DEBUG
3328 static int nfs_bio_do_stop = 0;
3329 #endif
3330 
3331 static int
3332 nfs_bio(struct buf *bp, cred_t *cr)
3333 {
3334 	rnode_t *rp = VTOR(bp->b_vp);
3335 	int count;
3336 	int error;
3337 	cred_t *cred;
3338 	uint_t offset;
3339 
3340 	DTRACE_IO1(start, struct buf *, bp);
3341 
3342 	ASSERT(nfs_zone() == VTOMI(bp->b_vp)->mi_zone);
3343 	offset = dbtob(bp->b_blkno);
3344 
3345 	if (bp->b_flags & B_READ) {
3346 		mutex_enter(&rp->r_statelock);
3347 		if (rp->r_cred != NULL) {
3348 			cred = rp->r_cred;
3349 			crhold(cred);
3350 		} else {
3351 			rp->r_cred = cr;
3352 			crhold(cr);
3353 			cred = cr;
3354 			crhold(cred);
3355 		}
3356 		mutex_exit(&rp->r_statelock);
3357 	read_again:
3358 		error = bp->b_error = nfsread(bp->b_vp, bp->b_un.b_addr,
3359 		    offset, bp->b_bcount, &bp->b_resid, cred);
3360 
3361 		crfree(cred);
3362 		if (!error) {
3363 			if (bp->b_resid) {
3364 				/*
3365 				 * Didn't get it all because we hit EOF,
3366 				 * zero all the memory beyond the EOF.
3367 				 */
3368 				/* bzero(rdaddr + */
3369 				bzero(bp->b_un.b_addr +
3370 				    bp->b_bcount - bp->b_resid, bp->b_resid);
3371 			}
3372 			mutex_enter(&rp->r_statelock);
3373 			if (bp->b_resid == bp->b_bcount &&
3374 			    offset >= rp->r_size) {
3375 				/*
3376 				 * We didn't read anything at all as we are
3377 				 * past EOF.  Return an error indicator back
3378 				 * but don't destroy the pages (yet).
3379 				 */
3380 				error = NFS_EOF;
3381 			}
3382 			mutex_exit(&rp->r_statelock);
3383 		} else if (error == EACCES) {
3384 			mutex_enter(&rp->r_statelock);
3385 			if (cred != cr) {
3386 				if (rp->r_cred != NULL)
3387 					crfree(rp->r_cred);
3388 				rp->r_cred = cr;
3389 				crhold(cr);
3390 				cred = cr;
3391 				crhold(cred);
3392 				mutex_exit(&rp->r_statelock);
3393 				goto read_again;
3394 			}
3395 			mutex_exit(&rp->r_statelock);
3396 		}
3397 	} else {
3398 		if (!(rp->r_flags & RSTALE)) {
3399 			mutex_enter(&rp->r_statelock);
3400 			if (rp->r_cred != NULL) {
3401 				cred = rp->r_cred;
3402 				crhold(cred);
3403 			} else {
3404 				rp->r_cred = cr;
3405 				crhold(cr);
3406 				cred = cr;
3407 				crhold(cred);
3408 			}
3409 			mutex_exit(&rp->r_statelock);
3410 		write_again:
3411 			mutex_enter(&rp->r_statelock);
3412 			count = MIN(bp->b_bcount, rp->r_size - offset);
3413 			mutex_exit(&rp->r_statelock);
3414 			if (count < 0)
3415 				cmn_err(CE_PANIC, "nfs_bio: write count < 0");
3416 #ifdef DEBUG
3417 			if (count == 0) {
3418 				zcmn_err(getzoneid(), CE_WARN,
3419 				    "nfs_bio: zero length write at %d",
3420 				    offset);
3421 				nfs_printfhandle(&rp->r_fh);
3422 				if (nfs_bio_do_stop)
3423 					debug_enter("nfs_bio");
3424 			}
3425 #endif
3426 			error = nfswrite(bp->b_vp, bp->b_un.b_addr, offset,
3427 			    count, cred);
3428 			if (error == EACCES) {
3429 				mutex_enter(&rp->r_statelock);
3430 				if (cred != cr) {
3431 					if (rp->r_cred != NULL)
3432 						crfree(rp->r_cred);
3433 					rp->r_cred = cr;
3434 					crhold(cr);
3435 					crfree(cred);
3436 					cred = cr;
3437 					crhold(cred);
3438 					mutex_exit(&rp->r_statelock);
3439 					goto write_again;
3440 				}
3441 				mutex_exit(&rp->r_statelock);
3442 			}
3443 			bp->b_error = error;
3444 			if (error && error != EINTR) {
3445 				/*
3446 				 * Don't print EDQUOT errors on the console.
3447 				 * Don't print asynchronous EACCES errors.
3448 				 * Don't print EFBIG errors.
3449 				 * Print all other write errors.
3450 				 */
3451 				if (error != EDQUOT && error != EFBIG &&
3452 				    (error != EACCES ||
3453 				    !(bp->b_flags & B_ASYNC)))
3454 					nfs_write_error(bp->b_vp, error, cred);
3455 				/*
3456 				 * Update r_error and r_flags as appropriate.
3457 				 * If the error was ESTALE, then mark the
3458 				 * rnode as not being writeable and save
3459 				 * the error status.  Otherwise, save any
3460 				 * errors which occur from asynchronous
3461 				 * page invalidations.  Any errors occurring
3462 				 * from other operations should be saved
3463 				 * by the caller.
3464 				 */
3465 				mutex_enter(&rp->r_statelock);
3466 				if (error == ESTALE) {
3467 					rp->r_flags |= RSTALE;
3468 					if (!rp->r_error)
3469 						rp->r_error = error;
3470 				} else if (!rp->r_error &&
3471 				    (bp->b_flags &
3472 				    (B_INVAL|B_FORCE|B_ASYNC)) ==
3473 				    (B_INVAL|B_FORCE|B_ASYNC)) {
3474 					rp->r_error = error;
3475 				}
3476 				mutex_exit(&rp->r_statelock);
3477 			}
3478 			crfree(cred);
3479 		} else
3480 			error = rp->r_error;
3481 	}
3482 
3483 	if (error != 0 && error != NFS_EOF)
3484 		bp->b_flags |= B_ERROR;
3485 
3486 	DTRACE_IO1(done, struct buf *, bp);
3487 
3488 	return (error);
3489 }
3490 
3491 /* ARGSUSED */
3492 static int
3493 nfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
3494 {
3495 	struct nfs_fid *fp;
3496 	rnode_t *rp;
3497 
3498 	rp = VTOR(vp);
3499 
3500 	if (fidp->fid_len < (sizeof (struct nfs_fid) - sizeof (short))) {
3501 		fidp->fid_len = sizeof (struct nfs_fid) - sizeof (short);
3502 		return (ENOSPC);
3503 	}
3504 	fp = (struct nfs_fid *)fidp;
3505 	fp->nf_pad = 0;
3506 	fp->nf_len = sizeof (struct nfs_fid) - sizeof (short);
3507 	bcopy(rp->r_fh.fh_buf, fp->nf_data, NFS_FHSIZE);
3508 	return (0);
3509 }
3510 
3511 /* ARGSUSED2 */
3512 static int
3513 nfs_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
3514 {
3515 	rnode_t *rp = VTOR(vp);
3516 
3517 	if (!write_lock) {
3518 		(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
3519 		return (V_WRITELOCK_FALSE);
3520 	}
3521 
3522 	if ((rp->r_flags & RDIRECTIO) || (VTOMI(vp)->mi_flags & MI_DIRECTIO)) {
3523 		(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
3524 		if (rp->r_mapcnt == 0 && !vn_has_cached_data(vp))
3525 			return (V_WRITELOCK_FALSE);
3526 		nfs_rw_exit(&rp->r_rwlock);
3527 	}
3528 
3529 	(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
3530 	return (V_WRITELOCK_TRUE);
3531 }
3532 
3533 /* ARGSUSED */
3534 static void
3535 nfs_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
3536 {
3537 	rnode_t *rp = VTOR(vp);
3538 
3539 	nfs_rw_exit(&rp->r_rwlock);
3540 }
3541 
3542 /* ARGSUSED */
3543 static int
3544 nfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
3545 {
3546 
3547 	/*
3548 	 * Because we stuff the readdir cookie into the offset field
3549 	 * someone may attempt to do an lseek with the cookie which
3550 	 * we want to succeed.
3551 	 */
3552 	if (vp->v_type == VDIR)
3553 		return (0);
3554 	if (*noffp < 0 || *noffp > MAXOFF32_T)
3555 		return (EINVAL);
3556 	return (0);
3557 }
3558 
3559 /*
3560  * number of NFS_MAXDATA blocks to read ahead
3561  * optimized for 100 base-T.
3562  */
3563 static int nfs_nra = 4;
3564 
3565 #ifdef DEBUG
3566 static int nfs_lostpage = 0;	/* number of times we lost original page */
3567 #endif
3568 
3569 /*
3570  * Return all the pages from [off..off+len) in file
3571  */
3572 /* ARGSUSED */
3573 static int
3574 nfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
3575 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3576 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
3577 {
3578 	rnode_t *rp;
3579 	int error;
3580 	mntinfo_t *mi;
3581 
3582 	if (vp->v_flag & VNOMAP)
3583 		return (ENOSYS);
3584 
3585 	ASSERT(off <= MAXOFF32_T);
3586 	if (nfs_zone() != VTOMI(vp)->mi_zone)
3587 		return (EIO);
3588 	if (protp != NULL)
3589 		*protp = PROT_ALL;
3590 
3591 	/*
3592 	 * Now valididate that the caches are up to date.
3593 	 */
3594 	error = nfs_validate_caches(vp, cr);
3595 	if (error)
3596 		return (error);
3597 
3598 	rp = VTOR(vp);
3599 	mi = VTOMI(vp);
3600 retry:
3601 	mutex_enter(&rp->r_statelock);
3602 
3603 	/*
3604 	 * Don't create dirty pages faster than they
3605 	 * can be cleaned so that the system doesn't
3606 	 * get imbalanced.  If the async queue is
3607 	 * maxed out, then wait for it to drain before
3608 	 * creating more dirty pages.  Also, wait for
3609 	 * any threads doing pagewalks in the vop_getattr
3610 	 * entry points so that they don't block for
3611 	 * long periods.
3612 	 */
3613 	if (rw == S_CREATE) {
3614 		while ((mi->mi_max_threads != 0 &&
3615 		    rp->r_awcount > 2 * mi->mi_max_threads) ||
3616 		    rp->r_gcount > 0)
3617 			cv_wait(&rp->r_cv, &rp->r_statelock);
3618 	}
3619 
3620 	/*
3621 	 * If we are getting called as a side effect of an nfs_write()
3622 	 * operation the local file size might not be extended yet.
3623 	 * In this case we want to be able to return pages of zeroes.
3624 	 */
3625 	if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
3626 		mutex_exit(&rp->r_statelock);
3627 		return (EFAULT);		/* beyond EOF */
3628 	}
3629 
3630 	mutex_exit(&rp->r_statelock);
3631 
3632 	if (len <= PAGESIZE) {
3633 		error = nfs_getapage(vp, off, len, protp, pl, plsz,
3634 		    seg, addr, rw, cr);
3635 	} else {
3636 		error = pvn_getpages(nfs_getapage, vp, off, len, protp,
3637 		    pl, plsz, seg, addr, rw, cr);
3638 	}
3639 
3640 	switch (error) {
3641 	case NFS_EOF:
3642 		nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
3643 		goto retry;
3644 	case ESTALE:
3645 		PURGE_STALE_FH(error, vp, cr);
3646 	}
3647 
3648 	return (error);
3649 }
3650 
3651 /*
3652  * Called from pvn_getpages or nfs_getpage to get a particular page.
3653  */
3654 /* ARGSUSED */
3655 static int
3656 nfs_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
3657 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
3658 	enum seg_rw rw, cred_t *cr)
3659 {
3660 	rnode_t *rp;
3661 	uint_t bsize;
3662 	struct buf *bp;
3663 	page_t *pp;
3664 	u_offset_t lbn;
3665 	u_offset_t io_off;
3666 	u_offset_t blkoff;
3667 	u_offset_t rablkoff;
3668 	size_t io_len;
3669 	uint_t blksize;
3670 	int error;
3671 	int readahead;
3672 	int readahead_issued = 0;
3673 	int ra_window; /* readahead window */
3674 	page_t *pagefound;
3675 
3676 	if (nfs_zone() != VTOMI(vp)->mi_zone)
3677 		return (EIO);
3678 	rp = VTOR(vp);
3679 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
3680 
3681 reread:
3682 	bp = NULL;
3683 	pp = NULL;
3684 	pagefound = NULL;
3685 
3686 	if (pl != NULL)
3687 		pl[0] = NULL;
3688 
3689 	error = 0;
3690 	lbn = off / bsize;
3691 	blkoff = lbn * bsize;
3692 
3693 	/*
3694 	 * Queueing up the readahead before doing the synchronous read
3695 	 * results in a significant increase in read throughput because
3696 	 * of the increased parallelism between the async threads and
3697 	 * the process context.
3698 	 */
3699 	if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
3700 	    rw != S_CREATE &&
3701 	    !(vp->v_flag & VNOCACHE)) {
3702 		mutex_enter(&rp->r_statelock);
3703 
3704 		/*
3705 		 * Calculate the number of readaheads to do.
3706 		 * a) No readaheads at offset = 0.
3707 		 * b) Do maximum(nfs_nra) readaheads when the readahead
3708 		 *    window is closed.
3709 		 * c) Do readaheads between 1 to (nfs_nra - 1) depending
3710 		 *    upon how far the readahead window is open or close.
3711 		 * d) No readaheads if rp->r_nextr is not within the scope
3712 		 *    of the readahead window (random i/o).
3713 		 */
3714 
3715 		if (off == 0)
3716 			readahead = 0;
3717 		else if (blkoff == rp->r_nextr)
3718 			readahead = nfs_nra;
3719 		else if (rp->r_nextr > blkoff &&
3720 		    ((ra_window = (rp->r_nextr - blkoff) / bsize)
3721 		    <= (nfs_nra - 1)))
3722 			readahead = nfs_nra - ra_window;
3723 		else
3724 			readahead = 0;
3725 
3726 		rablkoff = rp->r_nextr;
3727 		while (readahead > 0 && rablkoff + bsize < rp->r_size) {
3728 			mutex_exit(&rp->r_statelock);
3729 			if (nfs_async_readahead(vp, rablkoff + bsize,
3730 			    addr + (rablkoff + bsize - off), seg, cr,
3731 			    nfs_readahead) < 0) {
3732 				mutex_enter(&rp->r_statelock);
3733 				break;
3734 			}
3735 			readahead--;
3736 			rablkoff += bsize;
3737 			/*
3738 			 * Indicate that we did a readahead so
3739 			 * readahead offset is not updated
3740 			 * by the synchronous read below.
3741 			 */
3742 			readahead_issued = 1;
3743 			mutex_enter(&rp->r_statelock);
3744 			/*
3745 			 * set readahead offset to
3746 			 * offset of last async readahead
3747 			 * request.
3748 			 */
3749 			rp->r_nextr = rablkoff;
3750 		}
3751 		mutex_exit(&rp->r_statelock);
3752 	}
3753 
3754 again:
3755 	if ((pagefound = page_exists(vp, off)) == NULL) {
3756 		if (pl == NULL) {
3757 			(void) nfs_async_readahead(vp, blkoff, addr, seg, cr,
3758 			    nfs_readahead);
3759 		} else if (rw == S_CREATE) {
3760 			/*
3761 			 * Block for this page is not allocated, or the offset
3762 			 * is beyond the current allocation size, or we're
3763 			 * allocating a swap slot and the page was not found,
3764 			 * so allocate it and return a zero page.
3765 			 */
3766 			if ((pp = page_create_va(vp, off,
3767 			    PAGESIZE, PG_WAIT, seg, addr)) == NULL)
3768 				cmn_err(CE_PANIC, "nfs_getapage: page_create");
3769 			io_len = PAGESIZE;
3770 			mutex_enter(&rp->r_statelock);
3771 			rp->r_nextr = off + PAGESIZE;
3772 			mutex_exit(&rp->r_statelock);
3773 		} else {
3774 			/*
3775 			 * Need to go to server to get a BLOCK, exception to
3776 			 * that being while reading at offset = 0 or doing
3777 			 * random i/o, in that case read only a PAGE.
3778 			 */
3779 			mutex_enter(&rp->r_statelock);
3780 			if (blkoff < rp->r_size &&
3781 			    blkoff + bsize >= rp->r_size) {
3782 				/*
3783 				 * If only a block or less is left in
3784 				 * the file, read all that is remaining.
3785 				 */
3786 				if (rp->r_size <= off) {
3787 					/*
3788 					 * Trying to access beyond EOF,
3789 					 * set up to get at least one page.
3790 					 */
3791 					blksize = off + PAGESIZE - blkoff;
3792 				} else
3793 					blksize = rp->r_size - blkoff;
3794 			} else if ((off == 0) ||
3795 			    (off != rp->r_nextr && !readahead_issued)) {
3796 				blksize = PAGESIZE;
3797 				blkoff = off; /* block = page here */
3798 			} else
3799 				blksize = bsize;
3800 			mutex_exit(&rp->r_statelock);
3801 
3802 			pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
3803 			    &io_len, blkoff, blksize, 0);
3804 
3805 			/*
3806 			 * Some other thread has entered the page,
3807 			 * so just use it.
3808 			 */
3809 			if (pp == NULL)
3810 				goto again;
3811 
3812 			/*
3813 			 * Now round the request size up to page boundaries.
3814 			 * This ensures that the entire page will be
3815 			 * initialized to zeroes if EOF is encountered.
3816 			 */
3817 			io_len = ptob(btopr(io_len));
3818 
3819 			bp = pageio_setup(pp, io_len, vp, B_READ);
3820 			ASSERT(bp != NULL);
3821 
3822 			/*
3823 			 * pageio_setup should have set b_addr to 0.  This
3824 			 * is correct since we want to do I/O on a page
3825 			 * boundary.  bp_mapin will use this addr to calculate
3826 			 * an offset, and then set b_addr to the kernel virtual
3827 			 * address it allocated for us.
3828 			 */
3829 			ASSERT(bp->b_un.b_addr == 0);
3830 
3831 			bp->b_edev = 0;
3832 			bp->b_dev = 0;
3833 			bp->b_lblkno = lbtodb(io_off);
3834 			bp->b_file = vp;
3835 			bp->b_offset = (offset_t)off;
3836 			bp_mapin(bp);
3837 
3838 			/*
3839 			 * If doing a write beyond what we believe is EOF,
3840 			 * don't bother trying to read the pages from the
3841 			 * server, we'll just zero the pages here.  We
3842 			 * don't check that the rw flag is S_WRITE here
3843 			 * because some implementations may attempt a
3844 			 * read access to the buffer before copying data.
3845 			 */
3846 			mutex_enter(&rp->r_statelock);
3847 			if (io_off >= rp->r_size && seg == segkmap) {
3848 				mutex_exit(&rp->r_statelock);
3849 				bzero(bp->b_un.b_addr, io_len);
3850 			} else {
3851 				mutex_exit(&rp->r_statelock);
3852 				error = nfs_bio(bp, cr);
3853 			}
3854 
3855 			/*
3856 			 * Unmap the buffer before freeing it.
3857 			 */
3858 			bp_mapout(bp);
3859 			pageio_done(bp);
3860 
3861 			if (error == NFS_EOF) {
3862 				/*
3863 				 * If doing a write system call just return
3864 				 * zeroed pages, else user tried to get pages
3865 				 * beyond EOF, return error.  We don't check
3866 				 * that the rw flag is S_WRITE here because
3867 				 * some implementations may attempt a read
3868 				 * access to the buffer before copying data.
3869 				 */
3870 				if (seg == segkmap)
3871 					error = 0;
3872 				else
3873 					error = EFAULT;
3874 			}
3875 
3876 			if (!readahead_issued && !error) {
3877 				mutex_enter(&rp->r_statelock);
3878 				rp->r_nextr = io_off + io_len;
3879 				mutex_exit(&rp->r_statelock);
3880 			}
3881 		}
3882 	}
3883 
3884 out:
3885 	if (pl == NULL)
3886 		return (error);
3887 
3888 	if (error) {
3889 		if (pp != NULL)
3890 			pvn_read_done(pp, B_ERROR);
3891 		return (error);
3892 	}
3893 
3894 	if (pagefound) {
3895 		se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
3896 
3897 		/*
3898 		 * Page exists in the cache, acquire the appropriate lock.
3899 		 * If this fails, start all over again.
3900 		 */
3901 		if ((pp = page_lookup(vp, off, se)) == NULL) {
3902 #ifdef DEBUG
3903 			nfs_lostpage++;
3904 #endif
3905 			goto reread;
3906 		}
3907 		pl[0] = pp;
3908 		pl[1] = NULL;
3909 		return (0);
3910 	}
3911 
3912 	if (pp != NULL)
3913 		pvn_plist_init(pp, pl, plsz, off, io_len, rw);
3914 
3915 	return (error);
3916 }
3917 
3918 static void
3919 nfs_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
3920 	cred_t *cr)
3921 {
3922 	int error;
3923 	page_t *pp;
3924 	u_offset_t io_off;
3925 	size_t io_len;
3926 	struct buf *bp;
3927 	uint_t bsize, blksize;
3928 	rnode_t *rp = VTOR(vp);
3929 
3930 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
3931 
3932 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
3933 
3934 	mutex_enter(&rp->r_statelock);
3935 	if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
3936 		/*
3937 		 * If less than a block left in file read less
3938 		 * than a block.
3939 		 */
3940 		blksize = rp->r_size - blkoff;
3941 	} else
3942 		blksize = bsize;
3943 	mutex_exit(&rp->r_statelock);
3944 
3945 	pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
3946 	    &io_off, &io_len, blkoff, blksize, 1);
3947 	/*
3948 	 * The isra flag passed to the kluster function is 1, we may have
3949 	 * gotten a return value of NULL for a variety of reasons (# of free
3950 	 * pages < minfree, someone entered the page on the vnode etc). In all
3951 	 * cases, we want to punt on the readahead.
3952 	 */
3953 	if (pp == NULL)
3954 		return;
3955 
3956 	/*
3957 	 * Now round the request size up to page boundaries.
3958 	 * This ensures that the entire page will be
3959 	 * initialized to zeroes if EOF is encountered.
3960 	 */
3961 	io_len = ptob(btopr(io_len));
3962 
3963 	bp = pageio_setup(pp, io_len, vp, B_READ);
3964 	ASSERT(bp != NULL);
3965 
3966 	/*
3967 	 * pageio_setup should have set b_addr to 0.  This is correct since
3968 	 * we want to do I/O on a page boundary. bp_mapin() will use this addr
3969 	 * to calculate an offset, and then set b_addr to the kernel virtual
3970 	 * address it allocated for us.
3971 	 */
3972 	ASSERT(bp->b_un.b_addr == 0);
3973 
3974 	bp->b_edev = 0;
3975 	bp->b_dev = 0;
3976 	bp->b_lblkno = lbtodb(io_off);
3977 	bp->b_file = vp;
3978 	bp->b_offset = (offset_t)blkoff;
3979 	bp_mapin(bp);
3980 
3981 	/*
3982 	 * If doing a write beyond what we believe is EOF, don't bother trying
3983 	 * to read the pages from the server, we'll just zero the pages here.
3984 	 * We don't check that the rw flag is S_WRITE here because some
3985 	 * implementations may attempt a read access to the buffer before
3986 	 * copying data.
3987 	 */
3988 	mutex_enter(&rp->r_statelock);
3989 	if (io_off >= rp->r_size && seg == segkmap) {
3990 		mutex_exit(&rp->r_statelock);
3991 		bzero(bp->b_un.b_addr, io_len);
3992 		error = 0;
3993 	} else {
3994 		mutex_exit(&rp->r_statelock);
3995 		error = nfs_bio(bp, cr);
3996 		if (error == NFS_EOF)
3997 			error = 0;
3998 	}
3999 
4000 	/*
4001 	 * Unmap the buffer before freeing it.
4002 	 */
4003 	bp_mapout(bp);
4004 	pageio_done(bp);
4005 
4006 	pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
4007 
4008 	/*
4009 	 * In case of error set readahead offset
4010 	 * to the lowest offset.
4011 	 * pvn_read_done() calls VN_DISPOSE to destroy the pages
4012 	 */
4013 	if (error && rp->r_nextr > io_off) {
4014 		mutex_enter(&rp->r_statelock);
4015 		if (rp->r_nextr > io_off)
4016 			rp->r_nextr = io_off;
4017 		mutex_exit(&rp->r_statelock);
4018 	}
4019 }
4020 
4021 /*
4022  * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
4023  * If len == 0, do from off to EOF.
4024  *
4025  * The normal cases should be len == 0 && off == 0 (entire vp list),
4026  * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
4027  * (from pageout).
4028  */
4029 /* ARGSUSED */
4030 static int
4031 nfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4032 	caller_context_t *ct)
4033 {
4034 	int error;
4035 	rnode_t *rp;
4036 
4037 	ASSERT(cr != NULL);
4038 
4039 	/*
4040 	 * XXX - Why should this check be made here?
4041 	 */
4042 	if (vp->v_flag & VNOMAP)
4043 		return (ENOSYS);
4044 
4045 	if (len == 0 && !(flags & B_INVAL) && vn_is_readonly(vp))
4046 		return (0);
4047 
4048 	if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
4049 		return (EIO);
4050 	ASSERT(off <= MAXOFF32_T);
4051 
4052 	rp = VTOR(vp);
4053 	mutex_enter(&rp->r_statelock);
4054 	rp->r_count++;
4055 	mutex_exit(&rp->r_statelock);
4056 	error = nfs_putpages(vp, off, len, flags, cr);
4057 	mutex_enter(&rp->r_statelock);
4058 	rp->r_count--;
4059 	cv_broadcast(&rp->r_cv);
4060 	mutex_exit(&rp->r_statelock);
4061 
4062 	return (error);
4063 }
4064 
4065 /*
4066  * Write out a single page, possibly klustering adjacent dirty pages.
4067  */
4068 int
4069 nfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
4070 	int flags, cred_t *cr)
4071 {
4072 	u_offset_t io_off;
4073 	u_offset_t lbn_off;
4074 	u_offset_t lbn;
4075 	size_t io_len;
4076 	uint_t bsize;
4077 	int error;
4078 	rnode_t *rp;
4079 
4080 	ASSERT(!vn_is_readonly(vp));
4081 	ASSERT(pp != NULL);
4082 	ASSERT(cr != NULL);
4083 	ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI(vp)->mi_zone);
4084 
4085 	rp = VTOR(vp);
4086 	ASSERT(rp->r_count > 0);
4087 
4088 	ASSERT(pp->p_offset <= MAXOFF32_T);
4089 
4090 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
4091 	lbn = pp->p_offset / bsize;
4092 	lbn_off = lbn * bsize;
4093 
4094 	/*
4095 	 * Find a kluster that fits in one block, or in
4096 	 * one page if pages are bigger than blocks.  If
4097 	 * there is less file space allocated than a whole
4098 	 * page, we'll shorten the i/o request below.
4099 	 */
4100 	pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
4101 	    roundup(bsize, PAGESIZE), flags);
4102 
4103 	/*
4104 	 * pvn_write_kluster shouldn't have returned a page with offset
4105 	 * behind the original page we were given.  Verify that.
4106 	 */
4107 	ASSERT((pp->p_offset / bsize) >= lbn);
4108 
4109 	/*
4110 	 * Now pp will have the list of kept dirty pages marked for
4111 	 * write back.  It will also handle invalidation and freeing
4112 	 * of pages that are not dirty.  Check for page length rounding
4113 	 * problems.
4114 	 */
4115 	if (io_off + io_len > lbn_off + bsize) {
4116 		ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
4117 		io_len = lbn_off + bsize - io_off;
4118 	}
4119 	/*
4120 	 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
4121 	 * consistent value of r_size. RMODINPROGRESS is set in writerp().
4122 	 * When RMODINPROGRESS is set it indicates that a uiomove() is in
4123 	 * progress and the r_size has not been made consistent with the
4124 	 * new size of the file. When the uiomove() completes the r_size is
4125 	 * updated and the RMODINPROGRESS flag is cleared.
4126 	 *
4127 	 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
4128 	 * consistent value of r_size. Without this handshaking, it is
4129 	 * possible that nfs(3)_bio() picks  up the old value of r_size
4130 	 * before the uiomove() in writerp() completes. This will result
4131 	 * in the write through nfs(3)_bio() being dropped.
4132 	 *
4133 	 * More precisely, there is a window between the time the uiomove()
4134 	 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
4135 	 * operation intervenes in this window, the page will be picked up,
4136 	 * because it is dirty (it will be unlocked, unless it was
4137 	 * pagecreate'd). When the page is picked up as dirty, the dirty
4138 	 * bit is reset (pvn_getdirty()). In nfs(3)write(), r_size is
4139 	 * checked. This will still be the old size. Therefore the page will
4140 	 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
4141 	 * the page will be found to be clean and the write will be dropped.
4142 	 */
4143 	if (rp->r_flags & RMODINPROGRESS) {
4144 		mutex_enter(&rp->r_statelock);
4145 		if ((rp->r_flags & RMODINPROGRESS) &&
4146 		    rp->r_modaddr + MAXBSIZE > io_off &&
4147 		    rp->r_modaddr < io_off + io_len) {
4148 			page_t *plist;
4149 			/*
4150 			 * A write is in progress for this region of the file.
4151 			 * If we did not detect RMODINPROGRESS here then this
4152 			 * path through nfs_putapage() would eventually go to
4153 			 * nfs(3)_bio() and may not write out all of the data
4154 			 * in the pages. We end up losing data. So we decide
4155 			 * to set the modified bit on each page in the page
4156 			 * list and mark the rnode with RDIRTY. This write
4157 			 * will be restarted at some later time.
4158 			 */
4159 			plist = pp;
4160 			while (plist != NULL) {
4161 				pp = plist;
4162 				page_sub(&plist, pp);
4163 				hat_setmod(pp);
4164 				page_io_unlock(pp);
4165 				page_unlock(pp);
4166 			}
4167 			rp->r_flags |= RDIRTY;
4168 			mutex_exit(&rp->r_statelock);
4169 			if (offp)
4170 				*offp = io_off;
4171 			if (lenp)
4172 				*lenp = io_len;
4173 			return (0);
4174 		}
4175 		mutex_exit(&rp->r_statelock);
4176 	}
4177 
4178 	if (flags & B_ASYNC) {
4179 		error = nfs_async_putapage(vp, pp, io_off, io_len, flags, cr,
4180 		    nfs_sync_putapage);
4181 	} else
4182 		error = nfs_sync_putapage(vp, pp, io_off, io_len, flags, cr);
4183 
4184 	if (offp)
4185 		*offp = io_off;
4186 	if (lenp)
4187 		*lenp = io_len;
4188 	return (error);
4189 }
4190 
4191 static int
4192 nfs_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
4193 	int flags, cred_t *cr)
4194 {
4195 	int error;
4196 	rnode_t *rp;
4197 
4198 	flags |= B_WRITE;
4199 
4200 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4201 	error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
4202 
4203 	rp = VTOR(vp);
4204 
4205 	if ((error == ENOSPC || error == EDQUOT || error == EACCES) &&
4206 	    (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
4207 		if (!(rp->r_flags & ROUTOFSPACE)) {
4208 			mutex_enter(&rp->r_statelock);
4209 			rp->r_flags |= ROUTOFSPACE;
4210 			mutex_exit(&rp->r_statelock);
4211 		}
4212 		flags |= B_ERROR;
4213 		pvn_write_done(pp, flags);
4214 		/*
4215 		 * If this was not an async thread, then try again to
4216 		 * write out the pages, but this time, also destroy
4217 		 * them whether or not the write is successful.  This
4218 		 * will prevent memory from filling up with these
4219 		 * pages and destroying them is the only alternative
4220 		 * if they can't be written out.
4221 		 *
4222 		 * Don't do this if this is an async thread because
4223 		 * when the pages are unlocked in pvn_write_done,
4224 		 * some other thread could have come along, locked
4225 		 * them, and queued for an async thread.  It would be
4226 		 * possible for all of the async threads to be tied
4227 		 * up waiting to lock the pages again and they would
4228 		 * all already be locked and waiting for an async
4229 		 * thread to handle them.  Deadlock.
4230 		 */
4231 		if (!(flags & B_ASYNC)) {
4232 			error = nfs_putpage(vp, io_off, io_len,
4233 			    B_INVAL | B_FORCE, cr, NULL);
4234 		}
4235 	} else {
4236 		if (error)
4237 			flags |= B_ERROR;
4238 		else if (rp->r_flags & ROUTOFSPACE) {
4239 			mutex_enter(&rp->r_statelock);
4240 			rp->r_flags &= ~ROUTOFSPACE;
4241 			mutex_exit(&rp->r_statelock);
4242 		}
4243 		pvn_write_done(pp, flags);
4244 	}
4245 
4246 	return (error);
4247 }
4248 
4249 /* ARGSUSED */
4250 static int
4251 nfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4252 	size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4253 	caller_context_t *ct)
4254 {
4255 	struct segvn_crargs vn_a;
4256 	int error;
4257 	rnode_t *rp;
4258 	struct vattr va;
4259 
4260 	if (nfs_zone() != VTOMI(vp)->mi_zone)
4261 		return (EIO);
4262 
4263 	if (vp->v_flag & VNOMAP)
4264 		return (ENOSYS);
4265 
4266 	if (off > MAXOFF32_T)
4267 		return (EFBIG);
4268 
4269 	if (off < 0 || off + len < 0)
4270 		return (ENXIO);
4271 
4272 	if (vp->v_type != VREG)
4273 		return (ENODEV);
4274 
4275 	/*
4276 	 * If there is cached data and if close-to-open consistency
4277 	 * checking is not turned off and if the file system is not
4278 	 * mounted readonly, then force an over the wire getattr.
4279 	 * Otherwise, just invoke nfsgetattr to get a copy of the
4280 	 * attributes.  The attribute cache will be used unless it
4281 	 * is timed out and if it is, then an over the wire getattr
4282 	 * will be issued.
4283 	 */
4284 	va.va_mask = AT_ALL;
4285 	if (vn_has_cached_data(vp) &&
4286 	    !(VTOMI(vp)->mi_flags & MI_NOCTO) && !vn_is_readonly(vp))
4287 		error = nfs_getattr_otw(vp, &va, cr);
4288 	else
4289 		error = nfsgetattr(vp, &va, cr);
4290 	if (error)
4291 		return (error);
4292 
4293 	/*
4294 	 * Check to see if the vnode is currently marked as not cachable.
4295 	 * This means portions of the file are locked (through VOP_FRLOCK).
4296 	 * In this case the map request must be refused.  We use
4297 	 * rp->r_lkserlock to avoid a race with concurrent lock requests.
4298 	 */
4299 	rp = VTOR(vp);
4300 
4301 	/*
4302 	 * Atomically increment r_inmap after acquiring r_rwlock. The
4303 	 * idea here is to acquire r_rwlock to block read/write and
4304 	 * not to protect r_inmap. r_inmap will inform nfs_read/write()
4305 	 * that we are in nfs_map(). Now, r_rwlock is acquired in order
4306 	 * and we can prevent the deadlock that would have occurred
4307 	 * when nfs_addmap() would have acquired it out of order.
4308 	 *
4309 	 * Since we are not protecting r_inmap by any lock, we do not
4310 	 * hold any lock when we decrement it. We atomically decrement
4311 	 * r_inmap after we release r_lkserlock.
4312 	 */
4313 
4314 	if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp)))
4315 		return (EINTR);
4316 	atomic_add_int(&rp->r_inmap, 1);
4317 	nfs_rw_exit(&rp->r_rwlock);
4318 
4319 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp))) {
4320 		atomic_add_int(&rp->r_inmap, -1);
4321 		return (EINTR);
4322 	}
4323 	if (vp->v_flag & VNOCACHE) {
4324 		error = EAGAIN;
4325 		goto done;
4326 	}
4327 
4328 	/*
4329 	 * Don't allow concurrent locks and mapping if mandatory locking is
4330 	 * enabled.
4331 	 */
4332 	if ((flk_has_remote_locks(vp) || lm_has_sleep(vp)) &&
4333 	    MANDLOCK(vp, va.va_mode)) {
4334 		error = EAGAIN;
4335 		goto done;
4336 	}
4337 
4338 	as_rangelock(as);
4339 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4340 	if (error != 0) {
4341 		as_rangeunlock(as);
4342 		goto done;
4343 	}
4344 
4345 	vn_a.vp = vp;
4346 	vn_a.offset = off;
4347 	vn_a.type = (flags & MAP_TYPE);
4348 	vn_a.prot = (uchar_t)prot;
4349 	vn_a.maxprot = (uchar_t)maxprot;
4350 	vn_a.flags = (flags & ~MAP_TYPE);
4351 	vn_a.cred = cr;
4352 	vn_a.amp = NULL;
4353 	vn_a.szc = 0;
4354 	vn_a.lgrp_mem_policy_flags = 0;
4355 
4356 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
4357 	as_rangeunlock(as);
4358 
4359 done:
4360 	nfs_rw_exit(&rp->r_lkserlock);
4361 	atomic_add_int(&rp->r_inmap, -1);
4362 	return (error);
4363 }
4364 
4365 /* ARGSUSED */
4366 static int
4367 nfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4368 	size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4369 	caller_context_t *ct)
4370 {
4371 	rnode_t *rp;
4372 
4373 	if (vp->v_flag & VNOMAP)
4374 		return (ENOSYS);
4375 	if (nfs_zone() != VTOMI(vp)->mi_zone)
4376 		return (EIO);
4377 
4378 	rp = VTOR(vp);
4379 	atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
4380 
4381 	return (0);
4382 }
4383 
4384 /* ARGSUSED */
4385 static int
4386 nfs_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag, offset_t offset,
4387 	struct flk_callback *flk_cbp, cred_t *cr, caller_context_t *ct)
4388 {
4389 	netobj lm_fh;
4390 	int rc;
4391 	u_offset_t start, end;
4392 	rnode_t *rp;
4393 	int error = 0, intr = INTR(vp);
4394 
4395 	/* check for valid cmd parameter */
4396 	if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
4397 		return (EINVAL);
4398 	if (nfs_zone() != VTOMI(vp)->mi_zone)
4399 		return (EIO);
4400 
4401 	/* Verify l_type. */
4402 	switch (bfp->l_type) {
4403 	case F_RDLCK:
4404 		if (cmd != F_GETLK && !(flag & FREAD))
4405 			return (EBADF);
4406 		break;
4407 	case F_WRLCK:
4408 		if (cmd != F_GETLK && !(flag & FWRITE))
4409 			return (EBADF);
4410 		break;
4411 	case F_UNLCK:
4412 		intr = 0;
4413 		break;
4414 
4415 	default:
4416 		return (EINVAL);
4417 	}
4418 
4419 	/* check the validity of the lock range */
4420 	if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
4421 		return (rc);
4422 	if (rc = flk_check_lock_data(start, end, MAXOFF32_T))
4423 		return (rc);
4424 
4425 	/*
4426 	 * If the filesystem is mounted using local locking, pass the
4427 	 * request off to the local locking code.
4428 	 */
4429 	if (VTOMI(vp)->mi_flags & MI_LLOCK) {
4430 		if (offset > MAXOFF32_T)
4431 			return (EFBIG);
4432 		if (cmd == F_SETLK || cmd == F_SETLKW) {
4433 			/*
4434 			 * For complete safety, we should be holding
4435 			 * r_lkserlock.  However, we can't call
4436 			 * lm_safelock and then fs_frlock while
4437 			 * holding r_lkserlock, so just invoke
4438 			 * lm_safelock and expect that this will
4439 			 * catch enough of the cases.
4440 			 */
4441 			if (!lm_safelock(vp, bfp, cr))
4442 				return (EAGAIN);
4443 		}
4444 		return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4445 	}
4446 
4447 	rp = VTOR(vp);
4448 
4449 	/*
4450 	 * Check whether the given lock request can proceed, given the
4451 	 * current file mappings.
4452 	 */
4453 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
4454 		return (EINTR);
4455 	if (cmd == F_SETLK || cmd == F_SETLKW) {
4456 		if (!lm_safelock(vp, bfp, cr)) {
4457 			rc = EAGAIN;
4458 			goto done;
4459 		}
4460 	}
4461 
4462 	/*
4463 	 * Flush the cache after waiting for async I/O to finish.  For new
4464 	 * locks, this is so that the process gets the latest bits from the
4465 	 * server.  For unlocks, this is so that other clients see the
4466 	 * latest bits once the file has been unlocked.  If currently dirty
4467 	 * pages can't be flushed, then don't allow a lock to be set.  But
4468 	 * allow unlocks to succeed, to avoid having orphan locks on the
4469 	 * server.
4470 	 */
4471 	if (cmd != F_GETLK) {
4472 		mutex_enter(&rp->r_statelock);
4473 		while (rp->r_count > 0) {
4474 			if (intr) {
4475 				klwp_t *lwp = ttolwp(curthread);
4476 
4477 				if (lwp != NULL)
4478 					lwp->lwp_nostop++;
4479 				if (cv_wait_sig(&rp->r_cv, &rp->r_statelock)
4480 				    == 0) {
4481 					if (lwp != NULL)
4482 						lwp->lwp_nostop--;
4483 					rc = EINTR;
4484 					break;
4485 				}
4486 				if (lwp != NULL)
4487 					lwp->lwp_nostop--;
4488 			} else
4489 			cv_wait(&rp->r_cv, &rp->r_statelock);
4490 		}
4491 		mutex_exit(&rp->r_statelock);
4492 		if (rc != 0)
4493 			goto done;
4494 		error = nfs_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
4495 		if (error) {
4496 			if (error == ENOSPC || error == EDQUOT) {
4497 				mutex_enter(&rp->r_statelock);
4498 				if (!rp->r_error)
4499 					rp->r_error = error;
4500 				mutex_exit(&rp->r_statelock);
4501 			}
4502 			if (bfp->l_type != F_UNLCK) {
4503 				rc = ENOLCK;
4504 				goto done;
4505 			}
4506 		}
4507 	}
4508 
4509 	lm_fh.n_len = sizeof (fhandle_t);
4510 	lm_fh.n_bytes = (char *)VTOFH(vp);
4511 
4512 	/*
4513 	 * Call the lock manager to do the real work of contacting
4514 	 * the server and obtaining the lock.
4515 	 */
4516 	rc = lm_frlock(vp, cmd, bfp, flag, offset, cr, &lm_fh, flk_cbp);
4517 
4518 	if (rc == 0)
4519 		nfs_lockcompletion(vp, cmd);
4520 
4521 done:
4522 	nfs_rw_exit(&rp->r_lkserlock);
4523 	return (rc);
4524 }
4525 
4526 /*
4527  * Free storage space associated with the specified vnode.  The portion
4528  * to be freed is specified by bfp->l_start and bfp->l_len (already
4529  * normalized to a "whence" of 0).
4530  *
4531  * This is an experimental facility whose continued existence is not
4532  * guaranteed.  Currently, we only support the special case
4533  * of l_len == 0, meaning free to end of file.
4534  */
4535 /* ARGSUSED */
4536 static int
4537 nfs_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
4538 	offset_t offset, cred_t *cr, caller_context_t *ct)
4539 {
4540 	int error;
4541 
4542 	ASSERT(vp->v_type == VREG);
4543 	if (cmd != F_FREESP)
4544 		return (EINVAL);
4545 
4546 	if (offset > MAXOFF32_T)
4547 		return (EFBIG);
4548 
4549 	if ((bfp->l_start > MAXOFF32_T) || (bfp->l_end > MAXOFF32_T) ||
4550 	    (bfp->l_len > MAXOFF32_T))
4551 		return (EFBIG);
4552 
4553 	if (nfs_zone() != VTOMI(vp)->mi_zone)
4554 		return (EIO);
4555 
4556 	error = convoff(vp, bfp, 0, offset);
4557 	if (!error) {
4558 		ASSERT(bfp->l_start >= 0);
4559 		if (bfp->l_len == 0) {
4560 			struct vattr va;
4561 
4562 			/*
4563 			 * ftruncate should not change the ctime and
4564 			 * mtime if we truncate the file to its
4565 			 * previous size.
4566 			 */
4567 			va.va_mask = AT_SIZE;
4568 			error = nfsgetattr(vp, &va, cr);
4569 			if (error || va.va_size == bfp->l_start)
4570 				return (error);
4571 			va.va_mask = AT_SIZE;
4572 			va.va_size = bfp->l_start;
4573 			error = nfssetattr(vp, &va, 0, cr);
4574 		} else
4575 			error = EINVAL;
4576 	}
4577 
4578 	return (error);
4579 }
4580 
4581 /* ARGSUSED */
4582 static int
4583 nfs_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
4584 {
4585 
4586 	return (EINVAL);
4587 }
4588 
4589 /*
4590  * Setup and add an address space callback to do the work of the delmap call.
4591  * The callback will (and must be) deleted in the actual callback function.
4592  *
4593  * This is done in order to take care of the problem that we have with holding
4594  * the address space's a_lock for a long period of time (e.g. if the NFS server
4595  * is down).  Callbacks will be executed in the address space code while the
4596  * a_lock is not held.	Holding the address space's a_lock causes things such
4597  * as ps and fork to hang because they are trying to acquire this lock as well.
4598  */
4599 /* ARGSUSED */
4600 static int
4601 nfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4602 	size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4603 	caller_context_t *ct)
4604 {
4605 	int			caller_found;
4606 	int			error;
4607 	rnode_t			*rp;
4608 	nfs_delmap_args_t	*dmapp;
4609 	nfs_delmapcall_t	*delmap_call;
4610 
4611 	if (vp->v_flag & VNOMAP)
4612 		return (ENOSYS);
4613 	/*
4614 	 * A process may not change zones if it has NFS pages mmap'ed
4615 	 * in, so we can't legitimately get here from the wrong zone.
4616 	 */
4617 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4618 
4619 	rp = VTOR(vp);
4620 
4621 	/*
4622 	 * The way that the address space of this process deletes its mapping
4623 	 * of this file is via the following call chains:
4624 	 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs_delmap()
4625 	 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs_delmap()
4626 	 *
4627 	 * With the use of address space callbacks we are allowed to drop the
4628 	 * address space lock, a_lock, while executing the NFS operations that
4629 	 * need to go over the wire.  Returning EAGAIN to the caller of this
4630 	 * function is what drives the execution of the callback that we add
4631 	 * below.  The callback will be executed by the address space code
4632 	 * after dropping the a_lock.  When the callback is finished, since
4633 	 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
4634 	 * is called again on the same segment to finish the rest of the work
4635 	 * that needs to happen during unmapping.
4636 	 *
4637 	 * This action of calling back into the segment driver causes
4638 	 * nfs_delmap() to get called again, but since the callback was
4639 	 * already executed at this point, it already did the work and there
4640 	 * is nothing left for us to do.
4641 	 *
4642 	 * To Summarize:
4643 	 * - The first time nfs_delmap is called by the current thread is when
4644 	 * we add the caller associated with this delmap to the delmap caller
4645 	 * list, add the callback, and return EAGAIN.
4646 	 * - The second time in this call chain when nfs_delmap is called we
4647 	 * will find this caller in the delmap caller list and realize there
4648 	 * is no more work to do thus removing this caller from the list and
4649 	 * returning the error that was set in the callback execution.
4650 	 */
4651 	caller_found = nfs_find_and_delete_delmapcall(rp, &error);
4652 	if (caller_found) {
4653 		/*
4654 		 * 'error' is from the actual delmap operations.  To avoid
4655 		 * hangs, we need to handle the return of EAGAIN differently
4656 		 * since this is what drives the callback execution.
4657 		 * In this case, we don't want to return EAGAIN and do the
4658 		 * callback execution because there are none to execute.
4659 		 */
4660 		if (error == EAGAIN)
4661 			return (0);
4662 		else
4663 			return (error);
4664 	}
4665 
4666 	/* current caller was not in the list */
4667 	delmap_call = nfs_init_delmapcall();
4668 
4669 	mutex_enter(&rp->r_statelock);
4670 	list_insert_tail(&rp->r_indelmap, delmap_call);
4671 	mutex_exit(&rp->r_statelock);
4672 
4673 	dmapp = kmem_alloc(sizeof (nfs_delmap_args_t), KM_SLEEP);
4674 
4675 	dmapp->vp = vp;
4676 	dmapp->off = off;
4677 	dmapp->addr = addr;
4678 	dmapp->len = len;
4679 	dmapp->prot = prot;
4680 	dmapp->maxprot = maxprot;
4681 	dmapp->flags = flags;
4682 	dmapp->cr = cr;
4683 	dmapp->caller = delmap_call;
4684 
4685 	error = as_add_callback(as, nfs_delmap_callback, dmapp,
4686 	    AS_UNMAP_EVENT, addr, len, KM_SLEEP);
4687 
4688 	return (error ? error : EAGAIN);
4689 }
4690 
4691 /*
4692  * Remove some pages from an mmap'd vnode.  Just update the
4693  * count of pages.  If doing close-to-open, then flush all
4694  * of the pages associated with this file.  Otherwise, start
4695  * an asynchronous page flush to write out any dirty pages.
4696  * This will also associate a credential with the rnode which
4697  * can be used to write the pages.
4698  */
4699 /* ARGSUSED */
4700 static void
4701 nfs_delmap_callback(struct as *as, void *arg, uint_t event)
4702 {
4703 	int			error;
4704 	rnode_t			*rp;
4705 	mntinfo_t		*mi;
4706 	nfs_delmap_args_t	*dmapp = (nfs_delmap_args_t *)arg;
4707 
4708 	rp = VTOR(dmapp->vp);
4709 	mi = VTOMI(dmapp->vp);
4710 
4711 	atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
4712 	ASSERT(rp->r_mapcnt >= 0);
4713 
4714 	/*
4715 	 * Initiate a page flush if there are pages, the file system
4716 	 * was not mounted readonly, the segment was mapped shared, and
4717 	 * the pages themselves were writeable.
4718 	 */
4719 	if (vn_has_cached_data(dmapp->vp) && !vn_is_readonly(dmapp->vp) &&
4720 	    dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
4721 		mutex_enter(&rp->r_statelock);
4722 		rp->r_flags |= RDIRTY;
4723 		mutex_exit(&rp->r_statelock);
4724 		/*
4725 		 * If this is a cross-zone access a sync putpage won't work, so
4726 		 * the best we can do is try an async putpage.  That seems
4727 		 * better than something more draconian such as discarding the
4728 		 * dirty pages.
4729 		 */
4730 		if ((mi->mi_flags & MI_NOCTO) ||
4731 		    nfs_zone() != mi->mi_zone)
4732 			error = nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
4733 			    B_ASYNC, dmapp->cr, NULL);
4734 		else
4735 			error = nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
4736 			    0, dmapp->cr, NULL);
4737 		if (!error) {
4738 			mutex_enter(&rp->r_statelock);
4739 			error = rp->r_error;
4740 			rp->r_error = 0;
4741 			mutex_exit(&rp->r_statelock);
4742 		}
4743 	} else
4744 		error = 0;
4745 
4746 	if ((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO))
4747 		(void) nfs_putpage(dmapp->vp, dmapp->off, dmapp->len,
4748 		    B_INVAL, dmapp->cr, NULL);
4749 
4750 	dmapp->caller->error = error;
4751 	(void) as_delete_callback(as, arg);
4752 	kmem_free(dmapp, sizeof (nfs_delmap_args_t));
4753 }
4754 
4755 /* ARGSUSED */
4756 static int
4757 nfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4758 	caller_context_t *ct)
4759 {
4760 	int error = 0;
4761 
4762 	if (nfs_zone() != VTOMI(vp)->mi_zone)
4763 		return (EIO);
4764 	/*
4765 	 * This looks a little weird because it's written in a general
4766 	 * manner but we make little use of cases.  If cntl() ever gets
4767 	 * widely used, the outer switch will make more sense.
4768 	 */
4769 
4770 	switch (cmd) {
4771 
4772 	/*
4773 	 * Large file spec - need to base answer new query with
4774 	 * hardcoded constant based on the protocol.
4775 	 */
4776 	case _PC_FILESIZEBITS:
4777 		*valp = 32;
4778 		return (0);
4779 
4780 	case _PC_LINK_MAX:
4781 	case _PC_NAME_MAX:
4782 	case _PC_PATH_MAX:
4783 	case _PC_SYMLINK_MAX:
4784 	case _PC_CHOWN_RESTRICTED:
4785 	case _PC_NO_TRUNC: {
4786 		mntinfo_t *mi;
4787 		struct pathcnf *pc;
4788 
4789 		if ((mi = VTOMI(vp)) == NULL || (pc = mi->mi_pathconf) == NULL)
4790 			return (EINVAL);
4791 		error = _PC_ISSET(cmd, pc->pc_mask);    /* error or bool */
4792 		switch (cmd) {
4793 		case _PC_LINK_MAX:
4794 			*valp = pc->pc_link_max;
4795 			break;
4796 		case _PC_NAME_MAX:
4797 			*valp = pc->pc_name_max;
4798 			break;
4799 		case _PC_PATH_MAX:
4800 		case _PC_SYMLINK_MAX:
4801 			*valp = pc->pc_path_max;
4802 			break;
4803 		case _PC_CHOWN_RESTRICTED:
4804 			/*
4805 			 * if we got here, error is really a boolean which
4806 			 * indicates whether cmd is set or not.
4807 			 */
4808 			*valp = error ? 1 : 0;	/* see above */
4809 			error = 0;
4810 			break;
4811 		case _PC_NO_TRUNC:
4812 			/*
4813 			 * if we got here, error is really a boolean which
4814 			 * indicates whether cmd is set or not.
4815 			 */
4816 			*valp = error ? 1 : 0;	/* see above */
4817 			error = 0;
4818 			break;
4819 		}
4820 		return (error ? EINVAL : 0);
4821 		}
4822 
4823 	case _PC_XATTR_EXISTS:
4824 		*valp = 0;
4825 		if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
4826 			vnode_t *avp;
4827 			rnode_t *rp;
4828 			mntinfo_t *mi = VTOMI(vp);
4829 
4830 			if (!(mi->mi_flags & MI_EXTATTR))
4831 				return (0);
4832 
4833 			rp = VTOR(vp);
4834 			if (nfs_rw_enter_sig(&rp->r_rwlock, RW_READER,
4835 			    INTR(vp)))
4836 				return (EINTR);
4837 
4838 			error = nfslookup_dnlc(vp, XATTR_DIR_NAME, &avp, cr);
4839 			if (error || avp == NULL)
4840 				error = acl_getxattrdir2(vp, &avp, 0, cr, 0);
4841 
4842 			nfs_rw_exit(&rp->r_rwlock);
4843 
4844 			if (error == 0 && avp != NULL) {
4845 				error = do_xattr_exists_check(avp, valp, cr);
4846 				VN_RELE(avp);
4847 			}
4848 		}
4849 		return (error ? EINVAL : 0);
4850 
4851 	case _PC_ACL_ENABLED:
4852 		*valp = _ACL_ACLENT_ENABLED;
4853 		return (0);
4854 
4855 	default:
4856 		return (EINVAL);
4857 	}
4858 }
4859 
4860 /*
4861  * Called by async thread to do synchronous pageio. Do the i/o, wait
4862  * for it to complete, and cleanup the page list when done.
4863  */
4864 static int
4865 nfs_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
4866 	int flags, cred_t *cr)
4867 {
4868 	int error;
4869 
4870 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4871 	error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
4872 	if (flags & B_READ)
4873 		pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
4874 	else
4875 		pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
4876 	return (error);
4877 }
4878 
4879 /* ARGSUSED */
4880 static int
4881 nfs_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
4882 	int flags, cred_t *cr, caller_context_t *ct)
4883 {
4884 	int error;
4885 	rnode_t *rp;
4886 
4887 	if (pp == NULL)
4888 		return (EINVAL);
4889 
4890 	if (io_off > MAXOFF32_T)
4891 		return (EFBIG);
4892 	if (nfs_zone() != VTOMI(vp)->mi_zone)
4893 		return (EIO);
4894 	rp = VTOR(vp);
4895 	mutex_enter(&rp->r_statelock);
4896 	rp->r_count++;
4897 	mutex_exit(&rp->r_statelock);
4898 
4899 	if (flags & B_ASYNC) {
4900 		error = nfs_async_pageio(vp, pp, io_off, io_len, flags, cr,
4901 		    nfs_sync_pageio);
4902 	} else
4903 		error = nfs_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
4904 	mutex_enter(&rp->r_statelock);
4905 	rp->r_count--;
4906 	cv_broadcast(&rp->r_cv);
4907 	mutex_exit(&rp->r_statelock);
4908 	return (error);
4909 }
4910 
4911 /* ARGSUSED */
4912 static int
4913 nfs_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
4914 	caller_context_t *ct)
4915 {
4916 	int error;
4917 	mntinfo_t *mi;
4918 
4919 	mi = VTOMI(vp);
4920 
4921 	if (nfs_zone() != mi->mi_zone)
4922 		return (EIO);
4923 	if (mi->mi_flags & MI_ACL) {
4924 		error = acl_setacl2(vp, vsecattr, flag, cr);
4925 		if (mi->mi_flags & MI_ACL)
4926 			return (error);
4927 	}
4928 
4929 	return (ENOSYS);
4930 }
4931 
4932 /* ARGSUSED */
4933 static int
4934 nfs_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
4935 	caller_context_t *ct)
4936 {
4937 	int error;
4938 	mntinfo_t *mi;
4939 
4940 	mi = VTOMI(vp);
4941 
4942 	if (nfs_zone() != mi->mi_zone)
4943 		return (EIO);
4944 	if (mi->mi_flags & MI_ACL) {
4945 		error = acl_getacl2(vp, vsecattr, flag, cr);
4946 		if (mi->mi_flags & MI_ACL)
4947 			return (error);
4948 	}
4949 
4950 	return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
4951 }
4952 
4953 /* ARGSUSED */
4954 static int
4955 nfs_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
4956 	caller_context_t *ct)
4957 {
4958 	int error;
4959 	struct shrlock nshr;
4960 	struct nfs_owner nfs_owner;
4961 	netobj lm_fh;
4962 
4963 	if (nfs_zone() != VTOMI(vp)->mi_zone)
4964 		return (EIO);
4965 
4966 	/*
4967 	 * check for valid cmd parameter
4968 	 */
4969 	if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
4970 		return (EINVAL);
4971 
4972 	/*
4973 	 * Check access permissions
4974 	 */
4975 	if (cmd == F_SHARE &&
4976 	    (((shr->s_access & F_RDACC) && !(flag & FREAD)) ||
4977 	    ((shr->s_access & F_WRACC) && !(flag & FWRITE))))
4978 		return (EBADF);
4979 
4980 	/*
4981 	 * If the filesystem is mounted using local locking, pass the
4982 	 * request off to the local share code.
4983 	 */
4984 	if (VTOMI(vp)->mi_flags & MI_LLOCK)
4985 		return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
4986 
4987 	switch (cmd) {
4988 	case F_SHARE:
4989 	case F_UNSHARE:
4990 		lm_fh.n_len = sizeof (fhandle_t);
4991 		lm_fh.n_bytes = (char *)VTOFH(vp);
4992 
4993 		/*
4994 		 * If passed an owner that is too large to fit in an
4995 		 * nfs_owner it is likely a recursive call from the
4996 		 * lock manager client and pass it straight through.  If
4997 		 * it is not a nfs_owner then simply return an error.
4998 		 */
4999 		if (shr->s_own_len > sizeof (nfs_owner.lowner)) {
5000 			if (((struct nfs_owner *)shr->s_owner)->magic !=
5001 			    NFS_OWNER_MAGIC)
5002 				return (EINVAL);
5003 
5004 			if (error = lm_shrlock(vp, cmd, shr, flag, &lm_fh)) {
5005 				error = set_errno(error);
5006 			}
5007 			return (error);
5008 		}
5009 		/*
5010 		 * Remote share reservations owner is a combination of
5011 		 * a magic number, hostname, and the local owner
5012 		 */
5013 		bzero(&nfs_owner, sizeof (nfs_owner));
5014 		nfs_owner.magic = NFS_OWNER_MAGIC;
5015 		(void) strncpy(nfs_owner.hname, uts_nodename(),
5016 		    sizeof (nfs_owner.hname));
5017 		bcopy(shr->s_owner, nfs_owner.lowner, shr->s_own_len);
5018 		nshr.s_access = shr->s_access;
5019 		nshr.s_deny = shr->s_deny;
5020 		nshr.s_sysid = 0;
5021 		nshr.s_pid = ttoproc(curthread)->p_pid;
5022 		nshr.s_own_len = sizeof (nfs_owner);
5023 		nshr.s_owner = (caddr_t)&nfs_owner;
5024 
5025 		if (error = lm_shrlock(vp, cmd, &nshr, flag, &lm_fh)) {
5026 			error = set_errno(error);
5027 		}
5028 
5029 		break;
5030 
5031 	case F_HASREMOTELOCKS:
5032 		/*
5033 		 * NFS client can't store remote locks itself
5034 		 */
5035 		shr->s_access = 0;
5036 		error = 0;
5037 		break;
5038 
5039 	default:
5040 		error = EINVAL;
5041 		break;
5042 	}
5043 
5044 	return (error);
5045 }
5046