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