xref: /titanic_51/usr/src/uts/common/fs/nfs/nfs3_vnops.c (revision 7f79af0b29c00a006403444f61b261219f63cfbf)
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 
26 /*
27  *	Copyright (c) 1983,1984,1985,1986,1987,1988,1989 AT&T.
28  *	All rights reserved.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/cred.h>
35 #include <sys/time.h>
36 #include <sys/vnode.h>
37 #include <sys/vfs.h>
38 #include <sys/vfs_opreg.h>
39 #include <sys/file.h>
40 #include <sys/filio.h>
41 #include <sys/uio.h>
42 #include <sys/buf.h>
43 #include <sys/mman.h>
44 #include <sys/pathname.h>
45 #include <sys/dirent.h>
46 #include <sys/debug.h>
47 #include <sys/vmsystm.h>
48 #include <sys/fcntl.h>
49 #include <sys/flock.h>
50 #include <sys/swap.h>
51 #include <sys/errno.h>
52 #include <sys/strsubr.h>
53 #include <sys/sysmacros.h>
54 #include <sys/kmem.h>
55 #include <sys/cmn_err.h>
56 #include <sys/pathconf.h>
57 #include <sys/utsname.h>
58 #include <sys/dnlc.h>
59 #include <sys/acl.h>
60 #include <sys/systeminfo.h>
61 #include <sys/atomic.h>
62 #include <sys/policy.h>
63 #include <sys/sdt.h>
64 
65 #include <rpc/types.h>
66 #include <rpc/auth.h>
67 #include <rpc/clnt.h>
68 #include <rpc/rpc_rdma.h>
69 
70 #include <nfs/nfs.h>
71 #include <nfs/nfs_clnt.h>
72 #include <nfs/rnode.h>
73 #include <nfs/nfs_acl.h>
74 #include <nfs/lm.h>
75 
76 #include <vm/hat.h>
77 #include <vm/as.h>
78 #include <vm/page.h>
79 #include <vm/pvn.h>
80 #include <vm/seg.h>
81 #include <vm/seg_map.h>
82 #include <vm/seg_kpm.h>
83 #include <vm/seg_vn.h>
84 
85 #include <fs/fs_subr.h>
86 
87 #include <sys/ddi.h>
88 
89 static int	nfs3_rdwrlbn(vnode_t *, page_t *, u_offset_t, size_t, int,
90 			cred_t *);
91 static int	nfs3write(vnode_t *, caddr_t, u_offset_t, int, cred_t *,
92 			stable_how *);
93 static int	nfs3read(vnode_t *, caddr_t, offset_t, int, size_t *, cred_t *);
94 static int	nfs3setattr(vnode_t *, struct vattr *, int, cred_t *);
95 static int	nfs3_accessx(void *, int, cred_t *);
96 static int	nfs3lookup_dnlc(vnode_t *, char *, vnode_t **, cred_t *);
97 static int	nfs3lookup_otw(vnode_t *, char *, vnode_t **, cred_t *, int);
98 static int	nfs3create(vnode_t *, char *, struct vattr *, enum vcexcl,
99 			int, vnode_t **, cred_t *, int);
100 static int	nfs3excl_create_settimes(vnode_t *, struct vattr *, cred_t *);
101 static int	nfs3mknod(vnode_t *, char *, struct vattr *, enum vcexcl,
102 			int, vnode_t **, cred_t *);
103 static int	nfs3rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
104 			caller_context_t *);
105 static int	do_nfs3readdir(vnode_t *, rddir_cache *, cred_t *);
106 static void	nfs3readdir(vnode_t *, rddir_cache *, cred_t *);
107 static void	nfs3readdirplus(vnode_t *, rddir_cache *, cred_t *);
108 static int	nfs3_bio(struct buf *, stable_how *, cred_t *);
109 static int	nfs3_getapage(vnode_t *, u_offset_t, size_t, uint_t *,
110 			page_t *[], size_t, struct seg *, caddr_t,
111 			enum seg_rw, cred_t *);
112 static void	nfs3_readahead(vnode_t *, u_offset_t, caddr_t, struct seg *,
113 			cred_t *);
114 static int	nfs3_sync_putapage(vnode_t *, page_t *, u_offset_t, size_t,
115 			int, cred_t *);
116 static int	nfs3_sync_pageio(vnode_t *, page_t *, u_offset_t, size_t,
117 			int, cred_t *);
118 static int	nfs3_commit(vnode_t *, offset3, count3, cred_t *);
119 static void	nfs3_set_mod(vnode_t *);
120 static void	nfs3_get_commit(vnode_t *);
121 static void	nfs3_get_commit_range(vnode_t *, u_offset_t, size_t);
122 #if 0 /* unused */
123 #ifdef DEBUG
124 static int	nfs3_no_uncommitted_pages(vnode_t *);
125 #endif
126 #endif /* unused */
127 static int	nfs3_putpage_commit(vnode_t *, offset_t, size_t, cred_t *);
128 static int	nfs3_commit_vp(vnode_t *, u_offset_t, size_t,  cred_t *);
129 static int	nfs3_sync_commit(vnode_t *, page_t *, offset3, count3,
130 			cred_t *);
131 static void	nfs3_async_commit(vnode_t *, page_t *, offset3, count3,
132 			cred_t *);
133 static void	nfs3_delmap_callback(struct as *, void *, uint_t);
134 
135 /*
136  * Error flags used to pass information about certain special errors
137  * which need to be handled specially.
138  */
139 #define	NFS_EOF			-98
140 #define	NFS_VERF_MISMATCH	-97
141 
142 /* ALIGN64 aligns the given buffer and adjust buffer size to 64 bit */
143 #define	ALIGN64(x, ptr, sz)						\
144 	x = ((uintptr_t)(ptr)) & (sizeof (uint64_t) - 1);		\
145 	if (x) {							\
146 		x = sizeof (uint64_t) - (x);				\
147 		sz -= (x);						\
148 		ptr += (x);						\
149 	}
150 
151 /*
152  * These are the vnode ops routines which implement the vnode interface to
153  * the networked file system.  These routines just take their parameters,
154  * make them look networkish by putting the right info into interface structs,
155  * and then calling the appropriate remote routine(s) to do the work.
156  *
157  * Note on directory name lookup cacheing:  If we detect a stale fhandle,
158  * we purge the directory cache relative to that vnode.  This way, the
159  * user won't get burned by the cache repeatedly.  See <nfs/rnode.h> for
160  * more details on rnode locking.
161  */
162 
163 static int	nfs3_open(vnode_t **, int, cred_t *, caller_context_t *);
164 static int	nfs3_close(vnode_t *, int, int, offset_t, cred_t *,
165 			caller_context_t *);
166 static int	nfs3_read(vnode_t *, struct uio *, int, cred_t *,
167 			caller_context_t *);
168 static int	nfs3_write(vnode_t *, struct uio *, int, cred_t *,
169 			caller_context_t *);
170 static int	nfs3_ioctl(vnode_t *, int, intptr_t, int, cred_t *, int *,
171 			caller_context_t *);
172 static int	nfs3_getattr(vnode_t *, struct vattr *, int, cred_t *,
173 			caller_context_t *);
174 static int	nfs3_setattr(vnode_t *, struct vattr *, int, cred_t *,
175 			caller_context_t *);
176 static int	nfs3_access(vnode_t *, int, int, cred_t *, caller_context_t *);
177 static int	nfs3_readlink(vnode_t *, struct uio *, cred_t *,
178 			caller_context_t *);
179 static int	nfs3_fsync(vnode_t *, int, cred_t *, caller_context_t *);
180 static void	nfs3_inactive(vnode_t *, cred_t *, caller_context_t *);
181 static int	nfs3_lookup(vnode_t *, char *, vnode_t **,
182 			struct pathname *, int, vnode_t *, cred_t *,
183 			caller_context_t *, int *, pathname_t *);
184 static int	nfs3_create(vnode_t *, char *, struct vattr *, enum vcexcl,
185 			int, vnode_t **, cred_t *, int, caller_context_t *,
186 			vsecattr_t *);
187 static int	nfs3_remove(vnode_t *, char *, cred_t *, caller_context_t *,
188 			int);
189 static int	nfs3_link(vnode_t *, vnode_t *, char *, cred_t *,
190 			caller_context_t *, int);
191 static int	nfs3_rename(vnode_t *, char *, vnode_t *, char *, cred_t *,
192 			caller_context_t *, int);
193 static int	nfs3_mkdir(vnode_t *, char *, struct vattr *, vnode_t **,
194 			cred_t *, caller_context_t *, int, vsecattr_t *);
195 static int	nfs3_rmdir(vnode_t *, char *, vnode_t *, cred_t *,
196 			caller_context_t *, int);
197 static int	nfs3_symlink(vnode_t *, char *, struct vattr *, char *,
198 			cred_t *, caller_context_t *, int);
199 static int	nfs3_readdir(vnode_t *, struct uio *, cred_t *, int *,
200 			caller_context_t *, int);
201 static int	nfs3_fid(vnode_t *, fid_t *, caller_context_t *);
202 static int	nfs3_rwlock(vnode_t *, int, caller_context_t *);
203 static void	nfs3_rwunlock(vnode_t *, int, caller_context_t *);
204 static int	nfs3_seek(vnode_t *, offset_t, offset_t *, caller_context_t *);
205 static int	nfs3_getpage(vnode_t *, offset_t, size_t, uint_t *,
206 			page_t *[], size_t, struct seg *, caddr_t,
207 			enum seg_rw, cred_t *, caller_context_t *);
208 static int	nfs3_putpage(vnode_t *, offset_t, size_t, int, cred_t *,
209 			caller_context_t *);
210 static int	nfs3_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
211 			uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
212 static int	nfs3_addmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
213 			uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
214 static int	nfs3_frlock(vnode_t *, int, struct flock64 *, int, offset_t,
215 			struct flk_callback *, cred_t *, caller_context_t *);
216 static int	nfs3_space(vnode_t *, int, struct flock64 *, int, offset_t,
217 			cred_t *, caller_context_t *);
218 static int	nfs3_realvp(vnode_t *, vnode_t **, caller_context_t *);
219 static int	nfs3_delmap(vnode_t *, offset_t, struct as *, caddr_t, size_t,
220 			uint_t, uint_t, uint_t, cred_t *, caller_context_t *);
221 static int	nfs3_pathconf(vnode_t *, int, ulong_t *, cred_t *,
222 			caller_context_t *);
223 static int	nfs3_pageio(vnode_t *, page_t *, u_offset_t, size_t, int,
224 			cred_t *, caller_context_t *);
225 static void	nfs3_dispose(vnode_t *, page_t *, int, int, cred_t *,
226 			caller_context_t *);
227 static int	nfs3_setsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
228 			caller_context_t *);
229 static int	nfs3_getsecattr(vnode_t *, vsecattr_t *, int, cred_t *,
230 			caller_context_t *);
231 static int	nfs3_shrlock(vnode_t *, int, struct shrlock *, int, cred_t *,
232 			caller_context_t *);
233 
234 struct vnodeops *nfs3_vnodeops;
235 
236 const fs_operation_def_t nfs3_vnodeops_template[] = {
237 	VOPNAME_OPEN,		{ .vop_open = nfs3_open },
238 	VOPNAME_CLOSE,		{ .vop_close = nfs3_close },
239 	VOPNAME_READ,		{ .vop_read = nfs3_read },
240 	VOPNAME_WRITE,		{ .vop_write = nfs3_write },
241 	VOPNAME_IOCTL,		{ .vop_ioctl = nfs3_ioctl },
242 	VOPNAME_GETATTR,	{ .vop_getattr = nfs3_getattr },
243 	VOPNAME_SETATTR,	{ .vop_setattr = nfs3_setattr },
244 	VOPNAME_ACCESS,		{ .vop_access = nfs3_access },
245 	VOPNAME_LOOKUP,		{ .vop_lookup = nfs3_lookup },
246 	VOPNAME_CREATE,		{ .vop_create = nfs3_create },
247 	VOPNAME_REMOVE,		{ .vop_remove = nfs3_remove },
248 	VOPNAME_LINK,		{ .vop_link = nfs3_link },
249 	VOPNAME_RENAME,		{ .vop_rename = nfs3_rename },
250 	VOPNAME_MKDIR,		{ .vop_mkdir = nfs3_mkdir },
251 	VOPNAME_RMDIR,		{ .vop_rmdir = nfs3_rmdir },
252 	VOPNAME_READDIR,	{ .vop_readdir = nfs3_readdir },
253 	VOPNAME_SYMLINK,	{ .vop_symlink = nfs3_symlink },
254 	VOPNAME_READLINK,	{ .vop_readlink = nfs3_readlink },
255 	VOPNAME_FSYNC,		{ .vop_fsync = nfs3_fsync },
256 	VOPNAME_INACTIVE,	{ .vop_inactive = nfs3_inactive },
257 	VOPNAME_FID,		{ .vop_fid = nfs3_fid },
258 	VOPNAME_RWLOCK,		{ .vop_rwlock = nfs3_rwlock },
259 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = nfs3_rwunlock },
260 	VOPNAME_SEEK,		{ .vop_seek = nfs3_seek },
261 	VOPNAME_FRLOCK,		{ .vop_frlock = nfs3_frlock },
262 	VOPNAME_SPACE,		{ .vop_space = nfs3_space },
263 	VOPNAME_REALVP,		{ .vop_realvp = nfs3_realvp },
264 	VOPNAME_GETPAGE,	{ .vop_getpage = nfs3_getpage },
265 	VOPNAME_PUTPAGE,	{ .vop_putpage = nfs3_putpage },
266 	VOPNAME_MAP,		{ .vop_map = nfs3_map },
267 	VOPNAME_ADDMAP,		{ .vop_addmap = nfs3_addmap },
268 	VOPNAME_DELMAP,		{ .vop_delmap = nfs3_delmap },
269 	/* no separate nfs3_dump */
270 	VOPNAME_DUMP,		{ .vop_dump = nfs_dump },
271 	VOPNAME_PATHCONF,	{ .vop_pathconf = nfs3_pathconf },
272 	VOPNAME_PAGEIO,		{ .vop_pageio = nfs3_pageio },
273 	VOPNAME_DISPOSE,	{ .vop_dispose = nfs3_dispose },
274 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = nfs3_setsecattr },
275 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = nfs3_getsecattr },
276 	VOPNAME_SHRLOCK,	{ .vop_shrlock = nfs3_shrlock },
277 	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
278 	NULL,			NULL
279 };
280 
281 /*
282  * XXX:  This is referenced in modstubs.s
283  */
284 struct vnodeops *
285 nfs3_getvnodeops(void)
286 {
287 	return (nfs3_vnodeops);
288 }
289 
290 /* ARGSUSED */
291 static int
292 nfs3_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
293 {
294 	int error;
295 	struct vattr va;
296 	rnode_t *rp;
297 	vnode_t *vp;
298 
299 	vp = *vpp;
300 	if (nfs_zone() != VTOMI(vp)->mi_zone)
301 		return (EIO);
302 	rp = VTOR(vp);
303 	mutex_enter(&rp->r_statelock);
304 	if (rp->r_cred == NULL) {
305 		crhold(cr);
306 		rp->r_cred = cr;
307 	}
308 	mutex_exit(&rp->r_statelock);
309 
310 	/*
311 	 * If there is no cached data or if close-to-open
312 	 * consistency checking is turned off, we can avoid
313 	 * the over the wire getattr.  Otherwise, if the
314 	 * file system is mounted readonly, then just verify
315 	 * the caches are up to date using the normal mechanism.
316 	 * Else, if the file is not mmap'd, then just mark
317 	 * the attributes as timed out.  They will be refreshed
318 	 * and the caches validated prior to being used.
319 	 * Else, the file system is mounted writeable so
320 	 * force an over the wire GETATTR in order to ensure
321 	 * that all cached data is valid.
322 	 */
323 	if (vp->v_count > 1 ||
324 	    ((vn_has_cached_data(vp) || HAVE_RDDIR_CACHE(rp)) &&
325 	    !(VTOMI(vp)->mi_flags & MI_NOCTO))) {
326 		if (vn_is_readonly(vp))
327 			error = nfs3_validate_caches(vp, cr);
328 		else if (rp->r_mapcnt == 0 && vp->v_count == 1) {
329 			PURGE_ATTRCACHE(vp);
330 			error = 0;
331 		} else {
332 			va.va_mask = AT_ALL;
333 			error = nfs3_getattr_otw(vp, &va, cr);
334 		}
335 	} else
336 		error = 0;
337 
338 	return (error);
339 }
340 
341 /* ARGSUSED */
342 static int
343 nfs3_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
344 		caller_context_t *ct)
345 {
346 	rnode_t *rp;
347 	int error;
348 	struct vattr va;
349 
350 	/*
351 	 * zone_enter(2) prevents processes from changing zones with NFS files
352 	 * open; if we happen to get here from the wrong zone we can't do
353 	 * anything over the wire.
354 	 */
355 	if (VTOMI(vp)->mi_zone != nfs_zone()) {
356 		/*
357 		 * We could attempt to clean up locks, except we're sure
358 		 * that the current process didn't acquire any locks on
359 		 * the file: any attempt to lock a file belong to another zone
360 		 * will fail, and one can't lock an NFS file and then change
361 		 * zones, as that fails too.
362 		 *
363 		 * Returning an error here is the sane thing to do.  A
364 		 * subsequent call to VN_RELE() which translates to a
365 		 * nfs3_inactive() will clean up state: if the zone of the
366 		 * vnode's origin is still alive and kicking, an async worker
367 		 * thread will handle the request (from the correct zone), and
368 		 * everything (minus the commit and final nfs3_getattr_otw()
369 		 * call) should be OK. If the zone is going away
370 		 * nfs_async_inactive() will throw away cached pages inline.
371 		 */
372 		return (EIO);
373 	}
374 
375 	/*
376 	 * If we are using local locking for this filesystem, then
377 	 * release all of the SYSV style record locks.  Otherwise,
378 	 * we are doing network locking and we need to release all
379 	 * of the network locks.  All of the locks held by this
380 	 * process on this file are released no matter what the
381 	 * incoming reference count is.
382 	 */
383 	if (VTOMI(vp)->mi_flags & MI_LLOCK) {
384 		cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
385 		cleanshares(vp, ttoproc(curthread)->p_pid);
386 	} else
387 		nfs_lockrelease(vp, flag, offset, cr);
388 
389 	if (count > 1)
390 		return (0);
391 
392 	/*
393 	 * If the file has been `unlinked', then purge the
394 	 * DNLC so that this vnode will get reycled quicker
395 	 * and the .nfs* file on the server will get removed.
396 	 */
397 	rp = VTOR(vp);
398 	if (rp->r_unldvp != NULL)
399 		dnlc_purge_vp(vp);
400 
401 	/*
402 	 * If the file was open for write and there are pages,
403 	 * then if the file system was mounted using the "no-close-
404 	 *	to-open" semantics, then start an asynchronous flush
405 	 *	of the all of the pages in the file.
406 	 * else the file system was not mounted using the "no-close-
407 	 *	to-open" semantics, then do a synchronous flush and
408 	 *	commit of all of the dirty and uncommitted pages.
409 	 *
410 	 * The asynchronous flush of the pages in the "nocto" path
411 	 * mostly just associates a cred pointer with the rnode so
412 	 * writes which happen later will have a better chance of
413 	 * working.  It also starts the data being written to the
414 	 * server, but without unnecessarily delaying the application.
415 	 */
416 	if ((flag & FWRITE) && vn_has_cached_data(vp)) {
417 		if (VTOMI(vp)->mi_flags & MI_NOCTO) {
418 			error = nfs3_putpage(vp, (offset_t)0, 0, B_ASYNC,
419 			    cr, ct);
420 			if (error == EAGAIN)
421 				error = 0;
422 		} else
423 			error = nfs3_putpage_commit(vp, (offset_t)0, 0, cr);
424 		if (!error) {
425 			mutex_enter(&rp->r_statelock);
426 			error = rp->r_error;
427 			rp->r_error = 0;
428 			mutex_exit(&rp->r_statelock);
429 		}
430 	} else {
431 		mutex_enter(&rp->r_statelock);
432 		error = rp->r_error;
433 		rp->r_error = 0;
434 		mutex_exit(&rp->r_statelock);
435 	}
436 
437 	/*
438 	 * If RWRITEATTR is set, then issue an over the wire GETATTR to
439 	 * refresh the attribute cache with a set of attributes which
440 	 * weren't returned from a WRITE.  This will enable the close-
441 	 * to-open processing to work.
442 	 */
443 	if (rp->r_flags & RWRITEATTR)
444 		(void) nfs3_getattr_otw(vp, &va, cr);
445 
446 	return (error);
447 }
448 
449 /* ARGSUSED */
450 static int
451 nfs3_directio_read(vnode_t *vp, struct uio *uiop, cred_t *cr)
452 {
453 	mntinfo_t *mi;
454 	READ3args args;
455 	READ3uiores res;
456 	int tsize;
457 	offset_t offset;
458 	ssize_t count;
459 	int error;
460 	int douprintf;
461 	failinfo_t fi;
462 	char *sv_hostname;
463 
464 	mi = VTOMI(vp);
465 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
466 	sv_hostname = VTOR(vp)->r_server->sv_hostname;
467 
468 	douprintf = 1;
469 	args.file = *VTOFH3(vp);
470 	fi.vp = vp;
471 	fi.fhp = (caddr_t)&args.file;
472 	fi.copyproc = nfs3copyfh;
473 	fi.lookupproc = nfs3lookup;
474 	fi.xattrdirproc = acl_getxattrdir3;
475 
476 	res.uiop = uiop;
477 
478 	res.wlist = NULL;
479 
480 	offset = uiop->uio_loffset;
481 	count = uiop->uio_resid;
482 
483 	do {
484 		if (mi->mi_io_kstats) {
485 			mutex_enter(&mi->mi_lock);
486 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
487 			mutex_exit(&mi->mi_lock);
488 		}
489 
490 		do {
491 			tsize = MIN(mi->mi_tsize, count);
492 			args.offset = (offset3)offset;
493 			args.count = (count3)tsize;
494 			res.size = (uint_t)tsize;
495 			args.res_uiop = uiop;
496 			args.res_data_val_alt = NULL;
497 
498 			error = rfs3call(mi, NFSPROC3_READ,
499 			    xdr_READ3args, (caddr_t)&args,
500 			    xdr_READ3uiores, (caddr_t)&res, cr,
501 			    &douprintf, &res.status, 0, &fi);
502 		} while (error == ENFS_TRYAGAIN);
503 
504 		if (mi->mi_io_kstats) {
505 			mutex_enter(&mi->mi_lock);
506 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
507 			mutex_exit(&mi->mi_lock);
508 		}
509 
510 		if (error)
511 			return (error);
512 
513 		error = geterrno3(res.status);
514 		if (error)
515 			return (error);
516 
517 		if (res.count != res.size) {
518 			zcmn_err(getzoneid(), CE_WARN,
519 "nfs3_directio_read: server %s returned incorrect amount",
520 			    sv_hostname);
521 			return (EIO);
522 		}
523 		count -= res.count;
524 		offset += res.count;
525 		if (mi->mi_io_kstats) {
526 			mutex_enter(&mi->mi_lock);
527 			KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
528 			KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.count;
529 			mutex_exit(&mi->mi_lock);
530 		}
531 		lwp_stat_update(LWP_STAT_INBLK, 1);
532 	} while (count && !res.eof);
533 
534 	return (0);
535 }
536 
537 /* ARGSUSED */
538 static int
539 nfs3_read(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
540 	caller_context_t *ct)
541 {
542 	rnode_t *rp;
543 	u_offset_t off;
544 	offset_t diff;
545 	int on;
546 	size_t n;
547 	caddr_t base;
548 	uint_t flags;
549 	int error = 0;
550 	mntinfo_t *mi;
551 
552 	rp = VTOR(vp);
553 	mi = VTOMI(vp);
554 
555 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
556 
557 	if (nfs_zone() != mi->mi_zone)
558 		return (EIO);
559 
560 	if (vp->v_type != VREG)
561 		return (EISDIR);
562 
563 	if (uiop->uio_resid == 0)
564 		return (0);
565 
566 	if (uiop->uio_loffset < 0 || uiop->uio_loffset + uiop->uio_resid < 0)
567 		return (EINVAL);
568 
569 	/*
570 	 * Bypass VM if caching has been disabled (e.g., locking) or if
571 	 * using client-side direct I/O and the file is not mmap'd and
572 	 * there are no cached pages.
573 	 */
574 	if ((vp->v_flag & VNOCACHE) ||
575 	    (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
576 	    rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
577 	    !vn_has_cached_data(vp))) {
578 		return (nfs3_directio_read(vp, uiop, cr));
579 	}
580 
581 	do {
582 		off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
583 		on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
584 		n = MIN(MAXBSIZE - on, uiop->uio_resid);
585 
586 		error = nfs3_validate_caches(vp, cr);
587 		if (error)
588 			break;
589 
590 		mutex_enter(&rp->r_statelock);
591 		while (rp->r_flags & RINCACHEPURGE) {
592 			if (!cv_wait_sig(&rp->r_cv, &rp->r_statelock)) {
593 				mutex_exit(&rp->r_statelock);
594 				return (EINTR);
595 			}
596 		}
597 		diff = rp->r_size - uiop->uio_loffset;
598 		mutex_exit(&rp->r_statelock);
599 		if (diff <= 0)
600 			break;
601 		if (diff < n)
602 			n = (size_t)diff;
603 
604 		if (vpm_enable) {
605 			/*
606 			 * Copy data.
607 			 */
608 			error = vpm_data_copy(vp, off + on, n, uiop,
609 			    1, NULL, 0, S_READ);
610 		} else {
611 			base = segmap_getmapflt(segkmap, vp, off + on, n, 1,
612 			    S_READ);
613 
614 			error = uiomove(base + on, n, UIO_READ, uiop);
615 		}
616 
617 		if (!error) {
618 			/*
619 			 * If read a whole block or read to eof,
620 			 * won't need this buffer again soon.
621 			 */
622 			mutex_enter(&rp->r_statelock);
623 			if (n + on == MAXBSIZE ||
624 			    uiop->uio_loffset == rp->r_size)
625 				flags = SM_DONTNEED;
626 			else
627 				flags = 0;
628 			mutex_exit(&rp->r_statelock);
629 			if (vpm_enable) {
630 				error = vpm_sync_pages(vp, off, n, flags);
631 			} else {
632 				error = segmap_release(segkmap, base, flags);
633 			}
634 		} else {
635 			if (vpm_enable) {
636 				(void) vpm_sync_pages(vp, off, n, 0);
637 			} else {
638 				(void) segmap_release(segkmap, base, 0);
639 			}
640 		}
641 	} while (!error && uiop->uio_resid > 0);
642 
643 	return (error);
644 }
645 
646 /* ARGSUSED */
647 static int
648 nfs3_write(vnode_t *vp, struct uio *uiop, int ioflag, cred_t *cr,
649 	caller_context_t *ct)
650 {
651 	rlim64_t limit = uiop->uio_llimit;
652 	rnode_t *rp;
653 	u_offset_t off;
654 	caddr_t base;
655 	uint_t flags;
656 	int remainder;
657 	size_t n;
658 	int on;
659 	int error;
660 	int resid;
661 	offset_t offset;
662 	mntinfo_t *mi;
663 	uint_t bsize;
664 
665 	rp = VTOR(vp);
666 
667 	if (vp->v_type != VREG)
668 		return (EISDIR);
669 
670 	mi = VTOMI(vp);
671 	if (nfs_zone() != mi->mi_zone)
672 		return (EIO);
673 	if (uiop->uio_resid == 0)
674 		return (0);
675 
676 	if (ioflag & FAPPEND) {
677 		struct vattr va;
678 
679 		/*
680 		 * Must serialize if appending.
681 		 */
682 		if (nfs_rw_lock_held(&rp->r_rwlock, RW_READER)) {
683 			nfs_rw_exit(&rp->r_rwlock);
684 			if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER,
685 			    INTR(vp)))
686 				return (EINTR);
687 		}
688 
689 		va.va_mask = AT_SIZE;
690 		error = nfs3getattr(vp, &va, cr);
691 		if (error)
692 			return (error);
693 		uiop->uio_loffset = va.va_size;
694 	}
695 
696 	offset = uiop->uio_loffset + uiop->uio_resid;
697 
698 	if (uiop->uio_loffset < 0 || offset < 0)
699 		return (EINVAL);
700 
701 	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
702 		limit = MAXOFFSET_T;
703 
704 	/*
705 	 * Check to make sure that the process will not exceed
706 	 * its limit on file size.  It is okay to write up to
707 	 * the limit, but not beyond.  Thus, the write which
708 	 * reaches the limit will be short and the next write
709 	 * will return an error.
710 	 */
711 	remainder = 0;
712 	if (offset > limit) {
713 		remainder = offset - limit;
714 		uiop->uio_resid = limit - uiop->uio_loffset;
715 		if (uiop->uio_resid <= 0) {
716 			proc_t *p = ttoproc(curthread);
717 
718 			uiop->uio_resid += remainder;
719 			mutex_enter(&p->p_lock);
720 			(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE],
721 			    p->p_rctls, p, RCA_UNSAFE_SIGINFO);
722 			mutex_exit(&p->p_lock);
723 			return (EFBIG);
724 		}
725 	}
726 
727 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp)))
728 		return (EINTR);
729 
730 	/*
731 	 * Bypass VM if caching has been disabled (e.g., locking) or if
732 	 * using client-side direct I/O and the file is not mmap'd and
733 	 * there are no cached pages.
734 	 */
735 	if ((vp->v_flag & VNOCACHE) ||
736 	    (((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO)) &&
737 	    rp->r_mapcnt == 0 && rp->r_inmap == 0 &&
738 	    !vn_has_cached_data(vp))) {
739 		size_t bufsize;
740 		int count;
741 		u_offset_t org_offset;
742 		stable_how stab_comm;
743 
744 nfs3_fwrite:
745 		if (rp->r_flags & RSTALE) {
746 			resid = uiop->uio_resid;
747 			offset = uiop->uio_loffset;
748 			error = rp->r_error;
749 			goto bottom;
750 		}
751 		bufsize = MIN(uiop->uio_resid, mi->mi_stsize);
752 		base = kmem_alloc(bufsize, KM_SLEEP);
753 		do {
754 			if (ioflag & FDSYNC)
755 				stab_comm = DATA_SYNC;
756 			else
757 				stab_comm = FILE_SYNC;
758 			resid = uiop->uio_resid;
759 			offset = uiop->uio_loffset;
760 			count = MIN(uiop->uio_resid, bufsize);
761 			org_offset = uiop->uio_loffset;
762 			error = uiomove(base, count, UIO_WRITE, uiop);
763 			if (!error) {
764 				error = nfs3write(vp, base, org_offset,
765 				    count, cr, &stab_comm);
766 			}
767 		} while (!error && uiop->uio_resid > 0);
768 		kmem_free(base, bufsize);
769 		goto bottom;
770 	}
771 
772 
773 	bsize = vp->v_vfsp->vfs_bsize;
774 
775 	do {
776 		off = uiop->uio_loffset & MAXBMASK; /* mapping offset */
777 		on = uiop->uio_loffset & MAXBOFFSET; /* Relative offset */
778 		n = MIN(MAXBSIZE - on, uiop->uio_resid);
779 
780 		resid = uiop->uio_resid;
781 		offset = uiop->uio_loffset;
782 
783 		if (rp->r_flags & RSTALE) {
784 			error = rp->r_error;
785 			break;
786 		}
787 
788 		/*
789 		 * Don't create dirty pages faster than they
790 		 * can be cleaned so that the system doesn't
791 		 * get imbalanced.  If the async queue is
792 		 * maxed out, then wait for it to drain before
793 		 * creating more dirty pages.  Also, wait for
794 		 * any threads doing pagewalks in the vop_getattr
795 		 * entry points so that they don't block for
796 		 * long periods.
797 		 */
798 		mutex_enter(&rp->r_statelock);
799 		while ((mi->mi_max_threads != 0 &&
800 		    rp->r_awcount > 2 * mi->mi_max_threads) ||
801 		    rp->r_gcount > 0)
802 			cv_wait(&rp->r_cv, &rp->r_statelock);
803 		mutex_exit(&rp->r_statelock);
804 
805 		/*
806 		 * Touch the page and fault it in if it is not in core
807 		 * before segmap_getmapflt or vpm_data_copy can lock it.
808 		 * This is to avoid the deadlock if the buffer is mapped
809 		 * to the same file through mmap which we want to write.
810 		 */
811 		uio_prefaultpages((long)n, uiop);
812 
813 		if (vpm_enable) {
814 			/*
815 			 * It will use kpm mappings, so no need to
816 			 * pass an address.
817 			 */
818 			error = writerp(rp, NULL, n, uiop, 0);
819 		} else  {
820 			if (segmap_kpm) {
821 				int pon = uiop->uio_loffset & PAGEOFFSET;
822 				size_t pn = MIN(PAGESIZE - pon,
823 				    uiop->uio_resid);
824 				int pagecreate;
825 
826 				mutex_enter(&rp->r_statelock);
827 				pagecreate = (pon == 0) && (pn == PAGESIZE ||
828 				    uiop->uio_loffset + pn >= rp->r_size);
829 				mutex_exit(&rp->r_statelock);
830 
831 				base = segmap_getmapflt(segkmap, vp, off + on,
832 				    pn, !pagecreate, S_WRITE);
833 
834 				error = writerp(rp, base + pon, n, uiop,
835 				    pagecreate);
836 
837 			} else {
838 				base = segmap_getmapflt(segkmap, vp, off + on,
839 				    n, 0, S_READ);
840 				error = writerp(rp, base + on, n, uiop, 0);
841 			}
842 		}
843 
844 		if (!error) {
845 			if (mi->mi_flags & MI_NOAC)
846 				flags = SM_WRITE;
847 			else if ((uiop->uio_loffset % bsize) == 0 ||
848 			    IS_SWAPVP(vp)) {
849 				/*
850 				 * Have written a whole block.
851 				 * Start an asynchronous write
852 				 * and mark the buffer to
853 				 * indicate that it won't be
854 				 * needed again soon.
855 				 */
856 				flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
857 			} else
858 				flags = 0;
859 			if ((ioflag & (FSYNC|FDSYNC)) ||
860 			    (rp->r_flags & ROUTOFSPACE)) {
861 				flags &= ~SM_ASYNC;
862 				flags |= SM_WRITE;
863 			}
864 			if (vpm_enable) {
865 				error = vpm_sync_pages(vp, off, n, flags);
866 			} else {
867 				error = segmap_release(segkmap, base, flags);
868 			}
869 		} else {
870 			if (vpm_enable) {
871 				(void) vpm_sync_pages(vp, off, n, 0);
872 			} else {
873 				(void) segmap_release(segkmap, base, 0);
874 			}
875 			/*
876 			 * In the event that we got an access error while
877 			 * faulting in a page for a write-only file just
878 			 * force a write.
879 			 */
880 			if (error == EACCES)
881 				goto nfs3_fwrite;
882 		}
883 	} while (!error && uiop->uio_resid > 0);
884 
885 bottom:
886 	if (error) {
887 		uiop->uio_resid = resid + remainder;
888 		uiop->uio_loffset = offset;
889 	} else
890 		uiop->uio_resid += remainder;
891 
892 	nfs_rw_exit(&rp->r_lkserlock);
893 
894 	return (error);
895 }
896 
897 /*
898  * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
899  */
900 static int
901 nfs3_rdwrlbn(vnode_t *vp, page_t *pp, u_offset_t off, size_t len,
902 	int flags, cred_t *cr)
903 {
904 	struct buf *bp;
905 	int error;
906 	page_t *savepp;
907 	uchar_t fsdata;
908 	stable_how stab_comm;
909 
910 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
911 	bp = pageio_setup(pp, len, vp, flags);
912 	ASSERT(bp != NULL);
913 
914 	/*
915 	 * pageio_setup should have set b_addr to 0.  This
916 	 * is correct since we want to do I/O on a page
917 	 * boundary.  bp_mapin will use this addr to calculate
918 	 * an offset, and then set b_addr to the kernel virtual
919 	 * address it allocated for us.
920 	 */
921 	ASSERT(bp->b_un.b_addr == 0);
922 
923 	bp->b_edev = 0;
924 	bp->b_dev = 0;
925 	bp->b_lblkno = lbtodb(off);
926 	bp->b_file = vp;
927 	bp->b_offset = (offset_t)off;
928 	bp_mapin(bp);
929 
930 	/*
931 	 * Calculate the desired level of stability to write data
932 	 * on the server and then mark all of the pages to reflect
933 	 * this.
934 	 */
935 	if ((flags & (B_WRITE|B_ASYNC)) == (B_WRITE|B_ASYNC) &&
936 	    freemem > desfree) {
937 		stab_comm = UNSTABLE;
938 		fsdata = C_DELAYCOMMIT;
939 	} else {
940 		stab_comm = FILE_SYNC;
941 		fsdata = C_NOCOMMIT;
942 	}
943 
944 	savepp = pp;
945 	do {
946 		pp->p_fsdata = fsdata;
947 	} while ((pp = pp->p_next) != savepp);
948 
949 	error = nfs3_bio(bp, &stab_comm, cr);
950 
951 	bp_mapout(bp);
952 	pageio_done(bp);
953 
954 	/*
955 	 * If the server wrote pages in a more stable fashion than
956 	 * was requested, then clear all of the marks in the pages
957 	 * indicating that COMMIT operations were required.
958 	 */
959 	if (stab_comm != UNSTABLE && fsdata == C_DELAYCOMMIT) {
960 		do {
961 			pp->p_fsdata = C_NOCOMMIT;
962 		} while ((pp = pp->p_next) != savepp);
963 	}
964 
965 	return (error);
966 }
967 
968 /*
969  * Write to file.  Writes to remote server in largest size
970  * chunks that the server can handle.  Write is synchronous.
971  */
972 static int
973 nfs3write(vnode_t *vp, caddr_t base, u_offset_t offset, int count, cred_t *cr,
974 	stable_how *stab_comm)
975 {
976 	mntinfo_t *mi;
977 	WRITE3args args;
978 	WRITE3res res;
979 	int error;
980 	int tsize;
981 	rnode_t *rp;
982 	int douprintf;
983 
984 	rp = VTOR(vp);
985 	mi = VTOMI(vp);
986 
987 	ASSERT(nfs_zone() == mi->mi_zone);
988 
989 	args.file = *VTOFH3(vp);
990 	args.stable = *stab_comm;
991 
992 	*stab_comm = FILE_SYNC;
993 
994 	douprintf = 1;
995 
996 	do {
997 		if ((vp->v_flag & VNOCACHE) ||
998 		    (rp->r_flags & RDIRECTIO) ||
999 		    (mi->mi_flags & MI_DIRECTIO))
1000 			tsize = MIN(mi->mi_stsize, count);
1001 		else
1002 			tsize = MIN(mi->mi_curwrite, count);
1003 		args.offset = (offset3)offset;
1004 		args.count = (count3)tsize;
1005 		args.data.data_len = (uint_t)tsize;
1006 		args.data.data_val = base;
1007 
1008 		if (mi->mi_io_kstats) {
1009 			mutex_enter(&mi->mi_lock);
1010 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1011 			mutex_exit(&mi->mi_lock);
1012 		}
1013 		args.mblk = NULL;
1014 		do {
1015 			error = rfs3call(mi, NFSPROC3_WRITE,
1016 			    xdr_WRITE3args, (caddr_t)&args,
1017 			    xdr_WRITE3res, (caddr_t)&res, cr,
1018 			    &douprintf, &res.status, 0, NULL);
1019 		} while (error == ENFS_TRYAGAIN);
1020 		if (mi->mi_io_kstats) {
1021 			mutex_enter(&mi->mi_lock);
1022 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
1023 			mutex_exit(&mi->mi_lock);
1024 		}
1025 
1026 		if (error)
1027 			return (error);
1028 		error = geterrno3(res.status);
1029 		if (!error) {
1030 			if (res.resok.count > args.count) {
1031 				zcmn_err(getzoneid(), CE_WARN,
1032 				    "nfs3write: server %s wrote %u, "
1033 				    "requested was %u",
1034 				    rp->r_server->sv_hostname,
1035 				    res.resok.count, args.count);
1036 				return (EIO);
1037 			}
1038 			if (res.resok.committed == UNSTABLE) {
1039 				*stab_comm = UNSTABLE;
1040 				if (args.stable == DATA_SYNC ||
1041 				    args.stable == FILE_SYNC) {
1042 					zcmn_err(getzoneid(), CE_WARN,
1043 			"nfs3write: server %s did not commit to stable storage",
1044 					    rp->r_server->sv_hostname);
1045 					return (EIO);
1046 				}
1047 			}
1048 			tsize = (int)res.resok.count;
1049 			count -= tsize;
1050 			base += tsize;
1051 			offset += tsize;
1052 			if (mi->mi_io_kstats) {
1053 				mutex_enter(&mi->mi_lock);
1054 				KSTAT_IO_PTR(mi->mi_io_kstats)->writes++;
1055 				KSTAT_IO_PTR(mi->mi_io_kstats)->nwritten +=
1056 				    tsize;
1057 				mutex_exit(&mi->mi_lock);
1058 			}
1059 			lwp_stat_update(LWP_STAT_OUBLK, 1);
1060 			mutex_enter(&rp->r_statelock);
1061 			if (rp->r_flags & RHAVEVERF) {
1062 				if (rp->r_verf != res.resok.verf) {
1063 					nfs3_set_mod(vp);
1064 					rp->r_verf = res.resok.verf;
1065 					/*
1066 					 * If the data was written UNSTABLE,
1067 					 * then might as well stop because
1068 					 * the whole block will have to get
1069 					 * rewritten anyway.
1070 					 */
1071 					if (*stab_comm == UNSTABLE) {
1072 						mutex_exit(&rp->r_statelock);
1073 						break;
1074 					}
1075 				}
1076 			} else {
1077 				rp->r_verf = res.resok.verf;
1078 				rp->r_flags |= RHAVEVERF;
1079 			}
1080 			/*
1081 			 * Mark the attribute cache as timed out and
1082 			 * set RWRITEATTR to indicate that the file
1083 			 * was modified with a WRITE operation and
1084 			 * that the attributes can not be trusted.
1085 			 */
1086 			PURGE_ATTRCACHE_LOCKED(rp);
1087 			rp->r_flags |= RWRITEATTR;
1088 			mutex_exit(&rp->r_statelock);
1089 		}
1090 	} while (!error && count);
1091 
1092 	return (error);
1093 }
1094 
1095 /*
1096  * Read from a file.  Reads data in largest chunks our interface can handle.
1097  */
1098 static int
1099 nfs3read(vnode_t *vp, caddr_t base, offset_t offset, int count,
1100 	size_t *residp, cred_t *cr)
1101 {
1102 	mntinfo_t *mi;
1103 	READ3args args;
1104 	READ3vres res;
1105 	int tsize;
1106 	int error;
1107 	int douprintf;
1108 	failinfo_t fi;
1109 	rnode_t *rp;
1110 	struct vattr va;
1111 	hrtime_t t;
1112 
1113 	rp = VTOR(vp);
1114 	mi = VTOMI(vp);
1115 	ASSERT(nfs_zone() == mi->mi_zone);
1116 	douprintf = 1;
1117 
1118 	args.file = *VTOFH3(vp);
1119 	fi.vp = vp;
1120 	fi.fhp = (caddr_t)&args.file;
1121 	fi.copyproc = nfs3copyfh;
1122 	fi.lookupproc = nfs3lookup;
1123 	fi.xattrdirproc = acl_getxattrdir3;
1124 
1125 	res.pov.fres.vp = vp;
1126 	res.pov.fres.vap = &va;
1127 
1128 	res.wlist = NULL;
1129 	*residp = count;
1130 	do {
1131 		if (mi->mi_io_kstats) {
1132 			mutex_enter(&mi->mi_lock);
1133 			kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
1134 			mutex_exit(&mi->mi_lock);
1135 		}
1136 
1137 		do {
1138 			if ((vp->v_flag & VNOCACHE) ||
1139 			    (rp->r_flags & RDIRECTIO) ||
1140 			    (mi->mi_flags & MI_DIRECTIO))
1141 				tsize = MIN(mi->mi_tsize, count);
1142 			else
1143 				tsize = MIN(mi->mi_curread, count);
1144 			res.data.data_val = base;
1145 			res.data.data_len = tsize;
1146 			args.offset = (offset3)offset;
1147 			args.count = (count3)tsize;
1148 			args.res_uiop = NULL;
1149 			args.res_data_val_alt = base;
1150 
1151 			t = gethrtime();
1152 			error = rfs3call(mi, NFSPROC3_READ,
1153 			    xdr_READ3args, (caddr_t)&args,
1154 			    xdr_READ3vres, (caddr_t)&res, cr,
1155 			    &douprintf, &res.status, 0, &fi);
1156 		} while (error == ENFS_TRYAGAIN);
1157 
1158 		if (mi->mi_io_kstats) {
1159 			mutex_enter(&mi->mi_lock);
1160 			kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
1161 			mutex_exit(&mi->mi_lock);
1162 		}
1163 
1164 		if (error)
1165 			return (error);
1166 
1167 		error = geterrno3(res.status);
1168 		if (error)
1169 			return (error);
1170 
1171 		if (res.count != res.data.data_len) {
1172 			zcmn_err(getzoneid(), CE_WARN,
1173 			    "nfs3read: server %s returned incorrect amount",
1174 			    rp->r_server->sv_hostname);
1175 			return (EIO);
1176 		}
1177 
1178 		count -= res.count;
1179 		*residp = count;
1180 		base += res.count;
1181 		offset += res.count;
1182 		if (mi->mi_io_kstats) {
1183 			mutex_enter(&mi->mi_lock);
1184 			KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
1185 			KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.count;
1186 			mutex_exit(&mi->mi_lock);
1187 		}
1188 		lwp_stat_update(LWP_STAT_INBLK, 1);
1189 	} while (count && !res.eof);
1190 
1191 	if (res.pov.attributes) {
1192 		mutex_enter(&rp->r_statelock);
1193 		if (!CACHE_VALID(rp, va.va_mtime, va.va_size)) {
1194 			mutex_exit(&rp->r_statelock);
1195 			PURGE_ATTRCACHE(vp);
1196 		} else {
1197 			if (rp->r_mtime <= t)
1198 				nfs_attrcache_va(vp, &va);
1199 			mutex_exit(&rp->r_statelock);
1200 		}
1201 	}
1202 
1203 	return (0);
1204 }
1205 
1206 /* ARGSUSED */
1207 static int
1208 nfs3_ioctl(vnode_t *vp, int cmd, intptr_t arg, int flag, cred_t *cr, int *rvalp,
1209 	caller_context_t *ct)
1210 {
1211 
1212 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1213 		return (EIO);
1214 	switch (cmd) {
1215 		case _FIODIRECTIO:
1216 			return (nfs_directio(vp, (int)arg, cr));
1217 		default:
1218 			return (ENOTTY);
1219 	}
1220 }
1221 
1222 /* ARGSUSED */
1223 static int
1224 nfs3_getattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1225 	caller_context_t *ct)
1226 {
1227 	int error;
1228 	rnode_t *rp;
1229 
1230 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1231 		return (EIO);
1232 	/*
1233 	 * If it has been specified that the return value will
1234 	 * just be used as a hint, and we are only being asked
1235 	 * for size, fsid or rdevid, then return the client's
1236 	 * notion of these values without checking to make sure
1237 	 * that the attribute cache is up to date.
1238 	 * The whole point is to avoid an over the wire GETATTR
1239 	 * call.
1240 	 */
1241 	rp = VTOR(vp);
1242 	if (flags & ATTR_HINT) {
1243 		if (vap->va_mask ==
1244 		    (vap->va_mask & (AT_SIZE | AT_FSID | AT_RDEV))) {
1245 			mutex_enter(&rp->r_statelock);
1246 			if (vap->va_mask | AT_SIZE)
1247 				vap->va_size = rp->r_size;
1248 			if (vap->va_mask | AT_FSID)
1249 				vap->va_fsid = rp->r_attr.va_fsid;
1250 			if (vap->va_mask | AT_RDEV)
1251 				vap->va_rdev = rp->r_attr.va_rdev;
1252 			mutex_exit(&rp->r_statelock);
1253 			return (0);
1254 		}
1255 	}
1256 
1257 	/*
1258 	 * Only need to flush pages if asking for the mtime
1259 	 * and if there any dirty pages or any outstanding
1260 	 * asynchronous (write) requests for this file.
1261 	 */
1262 	if (vap->va_mask & AT_MTIME) {
1263 		if (vn_has_cached_data(vp) &&
1264 		    ((rp->r_flags & RDIRTY) || rp->r_awcount > 0)) {
1265 			mutex_enter(&rp->r_statelock);
1266 			rp->r_gcount++;
1267 			mutex_exit(&rp->r_statelock);
1268 			error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, ct);
1269 			mutex_enter(&rp->r_statelock);
1270 			if (error && (error == ENOSPC || error == EDQUOT)) {
1271 				if (!rp->r_error)
1272 					rp->r_error = error;
1273 			}
1274 			if (--rp->r_gcount == 0)
1275 				cv_broadcast(&rp->r_cv);
1276 			mutex_exit(&rp->r_statelock);
1277 		}
1278 	}
1279 
1280 	return (nfs3getattr(vp, vap, cr));
1281 }
1282 
1283 /*ARGSUSED4*/
1284 static int
1285 nfs3_setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr,
1286 		caller_context_t *ct)
1287 {
1288 	int error;
1289 	struct vattr va;
1290 
1291 	if (vap->va_mask & AT_NOSET)
1292 		return (EINVAL);
1293 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1294 		return (EIO);
1295 
1296 	va.va_mask = AT_UID | AT_MODE;
1297 	error = nfs3getattr(vp, &va, cr);
1298 	if (error)
1299 		return (error);
1300 
1301 	error = secpolicy_vnode_setattr(cr, vp, vap, &va, flags, nfs3_accessx,
1302 	    vp);
1303 	if (error)
1304 		return (error);
1305 
1306 	return (nfs3setattr(vp, vap, flags, cr));
1307 }
1308 
1309 static int
1310 nfs3setattr(vnode_t *vp, struct vattr *vap, int flags, cred_t *cr)
1311 {
1312 	int error;
1313 	uint_t mask;
1314 	SETATTR3args args;
1315 	SETATTR3res res;
1316 	int douprintf;
1317 	rnode_t *rp;
1318 	struct vattr va;
1319 	mode_t omode;
1320 	vsecattr_t *vsp;
1321 	hrtime_t t;
1322 
1323 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
1324 	mask = vap->va_mask;
1325 
1326 	rp = VTOR(vp);
1327 
1328 	/*
1329 	 * Only need to flush pages if there are any pages and
1330 	 * if the file is marked as dirty in some fashion.  The
1331 	 * file must be flushed so that we can accurately
1332 	 * determine the size of the file and the cached data
1333 	 * after the SETATTR returns.  A file is considered to
1334 	 * be dirty if it is either marked with RDIRTY, has
1335 	 * outstanding i/o's active, or is mmap'd.  In this
1336 	 * last case, we can't tell whether there are dirty
1337 	 * pages, so we flush just to be sure.
1338 	 */
1339 	if (vn_has_cached_data(vp) &&
1340 	    ((rp->r_flags & RDIRTY) ||
1341 	    rp->r_count > 0 ||
1342 	    rp->r_mapcnt > 0)) {
1343 		ASSERT(vp->v_type != VCHR);
1344 		error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, NULL);
1345 		if (error && (error == ENOSPC || error == EDQUOT)) {
1346 			mutex_enter(&rp->r_statelock);
1347 			if (!rp->r_error)
1348 				rp->r_error = error;
1349 			mutex_exit(&rp->r_statelock);
1350 		}
1351 	}
1352 
1353 	args.object = *RTOFH3(rp);
1354 	/*
1355 	 * If the intent is for the server to set the times,
1356 	 * there is no point in have the mask indicating set mtime or
1357 	 * atime, because the vap values may be junk, and so result
1358 	 * in an overflow error. Remove these flags from the vap mask
1359 	 * before calling in this case, and restore them afterwards.
1360 	 */
1361 	if ((mask & (AT_ATIME | AT_MTIME)) && !(flags & ATTR_UTIME)) {
1362 		/* Use server times, so don't set the args time fields */
1363 		vap->va_mask &= ~(AT_ATIME | AT_MTIME);
1364 		error = vattr_to_sattr3(vap, &args.new_attributes);
1365 		vap->va_mask |= (mask & (AT_ATIME | AT_MTIME));
1366 		if (mask & AT_ATIME) {
1367 			args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
1368 		}
1369 		if (mask & AT_MTIME) {
1370 			args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
1371 		}
1372 	} else {
1373 		/* Either do not set times or use the client specified times */
1374 		error = vattr_to_sattr3(vap, &args.new_attributes);
1375 	}
1376 
1377 	if (error) {
1378 		/* req time field(s) overflow - return immediately */
1379 		return (error);
1380 	}
1381 
1382 	va.va_mask = AT_MODE | AT_CTIME;
1383 	error = nfs3getattr(vp, &va, cr);
1384 	if (error)
1385 		return (error);
1386 	omode = va.va_mode;
1387 
1388 tryagain:
1389 	if (mask & AT_SIZE) {
1390 		args.guard.check = TRUE;
1391 		args.guard.obj_ctime.seconds = va.va_ctime.tv_sec;
1392 		args.guard.obj_ctime.nseconds = va.va_ctime.tv_nsec;
1393 	} else
1394 		args.guard.check = FALSE;
1395 
1396 	douprintf = 1;
1397 
1398 	t = gethrtime();
1399 
1400 	error = rfs3call(VTOMI(vp), NFSPROC3_SETATTR,
1401 	    xdr_SETATTR3args, (caddr_t)&args,
1402 	    xdr_SETATTR3res, (caddr_t)&res, cr,
1403 	    &douprintf, &res.status, 0, NULL);
1404 
1405 	/*
1406 	 * Purge the access cache and ACL cache if changing either the
1407 	 * owner of the file, the group owner, or the mode.  These may
1408 	 * change the access permissions of the file, so purge old
1409 	 * information and start over again.
1410 	 */
1411 	if (mask & (AT_UID | AT_GID | AT_MODE)) {
1412 		(void) nfs_access_purge_rp(rp);
1413 		if (rp->r_secattr != NULL) {
1414 			mutex_enter(&rp->r_statelock);
1415 			vsp = rp->r_secattr;
1416 			rp->r_secattr = NULL;
1417 			mutex_exit(&rp->r_statelock);
1418 			if (vsp != NULL)
1419 				nfs_acl_free(vsp);
1420 		}
1421 	}
1422 
1423 	if (error) {
1424 		PURGE_ATTRCACHE(vp);
1425 		return (error);
1426 	}
1427 
1428 	error = geterrno3(res.status);
1429 	if (!error) {
1430 		/*
1431 		 * If changing the size of the file, invalidate
1432 		 * any local cached data which is no longer part
1433 		 * of the file.  We also possibly invalidate the
1434 		 * last page in the file.  We could use
1435 		 * pvn_vpzero(), but this would mark the page as
1436 		 * modified and require it to be written back to
1437 		 * the server for no particularly good reason.
1438 		 * This way, if we access it, then we bring it
1439 		 * back in.  A read should be cheaper than a
1440 		 * write.
1441 		 */
1442 		if (mask & AT_SIZE) {
1443 			nfs_invalidate_pages(vp,
1444 			    (vap->va_size & PAGEMASK), cr);
1445 		}
1446 		nfs3_cache_wcc_data(vp, &res.resok.obj_wcc, t, cr);
1447 		/*
1448 		 * Some servers will change the mode to clear the setuid
1449 		 * and setgid bits when changing the uid or gid.  The
1450 		 * client needs to compensate appropriately.
1451 		 */
1452 		if (mask & (AT_UID | AT_GID)) {
1453 			int terror;
1454 
1455 			va.va_mask = AT_MODE;
1456 			terror = nfs3getattr(vp, &va, cr);
1457 			if (!terror &&
1458 			    (((mask & AT_MODE) && va.va_mode != vap->va_mode) ||
1459 			    (!(mask & AT_MODE) && va.va_mode != omode))) {
1460 				va.va_mask = AT_MODE;
1461 				if (mask & AT_MODE)
1462 					va.va_mode = vap->va_mode;
1463 				else
1464 					va.va_mode = omode;
1465 				(void) nfs3setattr(vp, &va, 0, cr);
1466 			}
1467 		}
1468 	} else {
1469 		nfs3_cache_wcc_data(vp, &res.resfail.obj_wcc, t, cr);
1470 		/*
1471 		 * If we got back a "not synchronized" error, then
1472 		 * we need to retry with a new guard value.  The
1473 		 * guard value used is the change time.  If the
1474 		 * server returned post_op_attr, then we can just
1475 		 * retry because we have the latest attributes.
1476 		 * Otherwise, we issue a GETATTR to get the latest
1477 		 * attributes and then retry.  If we couldn't get
1478 		 * the attributes this way either, then we give
1479 		 * up because we can't complete the operation as
1480 		 * required.
1481 		 */
1482 		if (res.status == NFS3ERR_NOT_SYNC) {
1483 			va.va_mask = AT_CTIME;
1484 			if (nfs3getattr(vp, &va, cr) == 0)
1485 				goto tryagain;
1486 		}
1487 		PURGE_STALE_FH(error, vp, cr);
1488 	}
1489 
1490 	return (error);
1491 }
1492 
1493 static int
1494 nfs3_accessx(void *vp, int mode, cred_t *cr)
1495 {
1496 	ASSERT(nfs_zone() == VTOMI((vnode_t *)vp)->mi_zone);
1497 	return (nfs3_access(vp, mode, 0, cr, NULL));
1498 }
1499 
1500 /* ARGSUSED */
1501 static int
1502 nfs3_access(vnode_t *vp, int mode, int flags, cred_t *cr, caller_context_t *ct)
1503 {
1504 	int error;
1505 	ACCESS3args args;
1506 	ACCESS3res res;
1507 	int douprintf;
1508 	uint32 acc;
1509 	rnode_t *rp;
1510 	cred_t *cred, *ncr, *ncrfree = NULL;
1511 	failinfo_t fi;
1512 	nfs_access_type_t cacc;
1513 	hrtime_t t;
1514 
1515 	acc = 0;
1516 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1517 		return (EIO);
1518 	if (mode & VREAD)
1519 		acc |= ACCESS3_READ;
1520 	if (mode & VWRITE) {
1521 		if (vn_is_readonly(vp) && !IS_DEVVP(vp))
1522 			return (EROFS);
1523 		if (vp->v_type == VDIR)
1524 			acc |= ACCESS3_DELETE;
1525 		acc |= ACCESS3_MODIFY | ACCESS3_EXTEND;
1526 	}
1527 	if (mode & VEXEC) {
1528 		if (vp->v_type == VDIR)
1529 			acc |= ACCESS3_LOOKUP;
1530 		else
1531 			acc |= ACCESS3_EXECUTE;
1532 	}
1533 
1534 	rp = VTOR(vp);
1535 	args.object = *VTOFH3(vp);
1536 	if (vp->v_type == VDIR) {
1537 		args.access = ACCESS3_READ | ACCESS3_DELETE | ACCESS3_MODIFY |
1538 		    ACCESS3_EXTEND | ACCESS3_LOOKUP;
1539 	} else {
1540 		args.access = ACCESS3_READ | ACCESS3_MODIFY | ACCESS3_EXTEND |
1541 		    ACCESS3_EXECUTE;
1542 	}
1543 	fi.vp = vp;
1544 	fi.fhp = (caddr_t)&args.object;
1545 	fi.copyproc = nfs3copyfh;
1546 	fi.lookupproc = nfs3lookup;
1547 	fi.xattrdirproc = acl_getxattrdir3;
1548 
1549 	cred = cr;
1550 	/*
1551 	 * ncr and ncrfree both initially
1552 	 * point to the memory area returned
1553 	 * by crnetadjust();
1554 	 * ncrfree not NULL when exiting means
1555 	 * that we need to release it
1556 	 */
1557 	ncr = crnetadjust(cred);
1558 	ncrfree = ncr;
1559 tryagain:
1560 	if (rp->r_acache != NULL) {
1561 		cacc = nfs_access_check(rp, acc, cred);
1562 		if (cacc == NFS_ACCESS_ALLOWED) {
1563 			if (ncrfree != NULL)
1564 				crfree(ncrfree);
1565 			return (0);
1566 		}
1567 		if (cacc == NFS_ACCESS_DENIED) {
1568 			/*
1569 			 * If the cred can be adjusted, try again
1570 			 * with the new cred.
1571 			 */
1572 			if (ncr != NULL) {
1573 				cred = ncr;
1574 				ncr = NULL;
1575 				goto tryagain;
1576 			}
1577 			if (ncrfree != NULL)
1578 				crfree(ncrfree);
1579 			return (EACCES);
1580 		}
1581 	}
1582 
1583 	douprintf = 1;
1584 
1585 	t = gethrtime();
1586 
1587 	error = rfs3call(VTOMI(vp), NFSPROC3_ACCESS,
1588 	    xdr_ACCESS3args, (caddr_t)&args,
1589 	    xdr_ACCESS3res, (caddr_t)&res, cred,
1590 	    &douprintf, &res.status, 0, &fi);
1591 
1592 	if (error) {
1593 		if (ncrfree != NULL)
1594 			crfree(ncrfree);
1595 		return (error);
1596 	}
1597 
1598 	error = geterrno3(res.status);
1599 	if (!error) {
1600 		nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr);
1601 		nfs_access_cache(rp, args.access, res.resok.access, cred);
1602 		/*
1603 		 * we just cached results with cred; if cred is the
1604 		 * adjusted credentials from crnetadjust, we do not want
1605 		 * to release them before exiting: hence setting ncrfree
1606 		 * to NULL
1607 		 */
1608 		if (cred != cr)
1609 			ncrfree = NULL;
1610 		if ((acc & res.resok.access) != acc) {
1611 			/*
1612 			 * If the cred can be adjusted, try again
1613 			 * with the new cred.
1614 			 */
1615 			if (ncr != NULL) {
1616 				cred = ncr;
1617 				ncr = NULL;
1618 				goto tryagain;
1619 			}
1620 			error = EACCES;
1621 		}
1622 	} else {
1623 		nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr);
1624 		PURGE_STALE_FH(error, vp, cr);
1625 	}
1626 
1627 	if (ncrfree != NULL)
1628 		crfree(ncrfree);
1629 
1630 	return (error);
1631 }
1632 
1633 static int nfs3_do_symlink_cache = 1;
1634 
1635 /* ARGSUSED */
1636 static int
1637 nfs3_readlink(vnode_t *vp, struct uio *uiop, cred_t *cr, caller_context_t *ct)
1638 {
1639 	int error;
1640 	READLINK3args args;
1641 	READLINK3res res;
1642 	nfspath3 resdata_backup;
1643 	rnode_t *rp;
1644 	int douprintf;
1645 	int len;
1646 	failinfo_t fi;
1647 	hrtime_t t;
1648 
1649 	/*
1650 	 * Can't readlink anything other than a symbolic link.
1651 	 */
1652 	if (vp->v_type != VLNK)
1653 		return (EINVAL);
1654 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1655 		return (EIO);
1656 
1657 	rp = VTOR(vp);
1658 	if (nfs3_do_symlink_cache && rp->r_symlink.contents != NULL) {
1659 		error = nfs3_validate_caches(vp, cr);
1660 		if (error)
1661 			return (error);
1662 		mutex_enter(&rp->r_statelock);
1663 		if (rp->r_symlink.contents != NULL) {
1664 			error = uiomove(rp->r_symlink.contents,
1665 			    rp->r_symlink.len, UIO_READ, uiop);
1666 			mutex_exit(&rp->r_statelock);
1667 			return (error);
1668 		}
1669 		mutex_exit(&rp->r_statelock);
1670 	}
1671 
1672 	args.symlink = *VTOFH3(vp);
1673 	fi.vp = vp;
1674 	fi.fhp = (caddr_t)&args.symlink;
1675 	fi.copyproc = nfs3copyfh;
1676 	fi.lookupproc = nfs3lookup;
1677 	fi.xattrdirproc = acl_getxattrdir3;
1678 
1679 	res.resok.data = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1680 
1681 	resdata_backup = res.resok.data;
1682 
1683 	douprintf = 1;
1684 
1685 	t = gethrtime();
1686 
1687 	error = rfs3call(VTOMI(vp), NFSPROC3_READLINK,
1688 	    xdr_READLINK3args, (caddr_t)&args,
1689 	    xdr_READLINK3res, (caddr_t)&res, cr,
1690 	    &douprintf, &res.status, 0, &fi);
1691 
1692 	if (res.resok.data == nfs3nametoolong)
1693 		error = EINVAL;
1694 
1695 	if (error) {
1696 		kmem_free(resdata_backup, MAXPATHLEN);
1697 		return (error);
1698 	}
1699 
1700 	error = geterrno3(res.status);
1701 	if (!error) {
1702 		nfs3_cache_post_op_attr(vp, &res.resok.symlink_attributes, t,
1703 		    cr);
1704 		len = strlen(res.resok.data);
1705 		error = uiomove(res.resok.data, len, UIO_READ, uiop);
1706 		if (nfs3_do_symlink_cache && rp->r_symlink.contents == NULL) {
1707 			mutex_enter(&rp->r_statelock);
1708 				if (rp->r_symlink.contents == NULL) {
1709 				rp->r_symlink.contents = res.resok.data;
1710 				rp->r_symlink.len = len;
1711 				rp->r_symlink.size = MAXPATHLEN;
1712 				mutex_exit(&rp->r_statelock);
1713 			} else {
1714 				mutex_exit(&rp->r_statelock);
1715 
1716 				kmem_free((void *)res.resok.data, MAXPATHLEN);
1717 			}
1718 		} else {
1719 			kmem_free((void *)res.resok.data, MAXPATHLEN);
1720 		}
1721 	} else {
1722 		nfs3_cache_post_op_attr(vp,
1723 		    &res.resfail.symlink_attributes, t, cr);
1724 		PURGE_STALE_FH(error, vp, cr);
1725 
1726 		kmem_free((void *)res.resok.data, MAXPATHLEN);
1727 
1728 	}
1729 
1730 	/*
1731 	 * The over the wire error for attempting to readlink something
1732 	 * other than a symbolic link is ENXIO.  However, we need to
1733 	 * return EINVAL instead of ENXIO, so we map it here.
1734 	 */
1735 	return (error == ENXIO ? EINVAL : error);
1736 }
1737 
1738 /*
1739  * Flush local dirty pages to stable storage on the server.
1740  *
1741  * If FNODSYNC is specified, then there is nothing to do because
1742  * metadata changes are not cached on the client before being
1743  * sent to the server.
1744  */
1745 /* ARGSUSED */
1746 static int
1747 nfs3_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
1748 {
1749 	int error;
1750 
1751 	if ((syncflag & FNODSYNC) || IS_SWAPVP(vp))
1752 		return (0);
1753 	if (nfs_zone() != VTOMI(vp)->mi_zone)
1754 		return (EIO);
1755 
1756 	error = nfs3_putpage_commit(vp, (offset_t)0, 0, cr);
1757 	if (!error)
1758 		error = VTOR(vp)->r_error;
1759 	return (error);
1760 }
1761 
1762 /*
1763  * Weirdness: if the file was removed or the target of a rename
1764  * operation while it was open, it got renamed instead.  Here we
1765  * remove the renamed file.
1766  */
1767 /* ARGSUSED */
1768 static void
1769 nfs3_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1770 {
1771 	rnode_t *rp;
1772 
1773 	ASSERT(vp != DNLC_NO_VNODE);
1774 
1775 	/*
1776 	 * If this is coming from the wrong zone, we let someone in the right
1777 	 * zone take care of it asynchronously.  We can get here due to
1778 	 * VN_RELE() being called from pageout() or fsflush().  This call may
1779 	 * potentially turn into an expensive no-op if, for instance, v_count
1780 	 * gets incremented in the meantime, but it's still correct.
1781 	 */
1782 	if (nfs_zone() != VTOMI(vp)->mi_zone) {
1783 		nfs_async_inactive(vp, cr, nfs3_inactive);
1784 		return;
1785 	}
1786 
1787 	rp = VTOR(vp);
1788 redo:
1789 	if (rp->r_unldvp != NULL) {
1790 		/*
1791 		 * Save the vnode pointer for the directory where the
1792 		 * unlinked-open file got renamed, then set it to NULL
1793 		 * to prevent another thread from getting here before
1794 		 * we're done with the remove.  While we have the
1795 		 * statelock, make local copies of the pertinent rnode
1796 		 * fields.  If we weren't to do this in an atomic way, the
1797 		 * the unl* fields could become inconsistent with respect
1798 		 * to each other due to a race condition between this
1799 		 * code and nfs_remove().  See bug report 1034328.
1800 		 */
1801 		mutex_enter(&rp->r_statelock);
1802 		if (rp->r_unldvp != NULL) {
1803 			vnode_t *unldvp;
1804 			char *unlname;
1805 			cred_t *unlcred;
1806 			REMOVE3args args;
1807 			REMOVE3res res;
1808 			int douprintf;
1809 			int error;
1810 			hrtime_t t;
1811 
1812 			unldvp = rp->r_unldvp;
1813 			rp->r_unldvp = NULL;
1814 			unlname = rp->r_unlname;
1815 			rp->r_unlname = NULL;
1816 			unlcred = rp->r_unlcred;
1817 			rp->r_unlcred = NULL;
1818 			mutex_exit(&rp->r_statelock);
1819 
1820 			/*
1821 			 * If there are any dirty pages left, then flush
1822 			 * them.  This is unfortunate because they just
1823 			 * may get thrown away during the remove operation,
1824 			 * but we have to do this for correctness.
1825 			 */
1826 			if (vn_has_cached_data(vp) &&
1827 			    ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
1828 				ASSERT(vp->v_type != VCHR);
1829 				error = nfs3_putpage(vp, (offset_t)0, 0, 0,
1830 				    cr, ct);
1831 				if (error) {
1832 					mutex_enter(&rp->r_statelock);
1833 					if (!rp->r_error)
1834 						rp->r_error = error;
1835 					mutex_exit(&rp->r_statelock);
1836 				}
1837 			}
1838 
1839 			/*
1840 			 * Do the remove operation on the renamed file
1841 			 */
1842 			setdiropargs3(&args.object, unlname, unldvp);
1843 
1844 			douprintf = 1;
1845 
1846 			t = gethrtime();
1847 
1848 			error = rfs3call(VTOMI(unldvp), NFSPROC3_REMOVE,
1849 			    xdr_diropargs3, (caddr_t)&args,
1850 			    xdr_REMOVE3res, (caddr_t)&res, unlcred,
1851 			    &douprintf, &res.status, 0, NULL);
1852 
1853 			if (error) {
1854 				PURGE_ATTRCACHE(unldvp);
1855 			} else {
1856 				error = geterrno3(res.status);
1857 				if (!error) {
1858 					nfs3_cache_wcc_data(unldvp,
1859 					    &res.resok.dir_wcc, t, cr);
1860 					if (HAVE_RDDIR_CACHE(VTOR(unldvp)))
1861 						nfs_purge_rddir_cache(unldvp);
1862 				} else {
1863 					nfs3_cache_wcc_data(unldvp,
1864 					    &res.resfail.dir_wcc, t, cr);
1865 					PURGE_STALE_FH(error, unldvp, cr);
1866 				}
1867 			}
1868 
1869 			/*
1870 			 * Release stuff held for the remove
1871 			 */
1872 			VN_RELE(unldvp);
1873 			kmem_free(unlname, MAXNAMELEN);
1874 			crfree(unlcred);
1875 			goto redo;
1876 		}
1877 		mutex_exit(&rp->r_statelock);
1878 	}
1879 
1880 	rp_addfree(rp, cr);
1881 }
1882 
1883 /*
1884  * Remote file system operations having to do with directory manipulation.
1885  */
1886 
1887 /* ARGSUSED */
1888 static int
1889 nfs3_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1890 	int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1891 	int *direntflags, pathname_t *realpnp)
1892 {
1893 	int error;
1894 	vnode_t *vp;
1895 	vnode_t *avp = NULL;
1896 	rnode_t *drp;
1897 
1898 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
1899 		return (EPERM);
1900 
1901 	drp = VTOR(dvp);
1902 
1903 	/*
1904 	 * Are we looking up extended attributes?  If so, "dvp" is
1905 	 * the file or directory for which we want attributes, and
1906 	 * we need a lookup of the hidden attribute directory
1907 	 * before we lookup the rest of the path.
1908 	 */
1909 	if (flags & LOOKUP_XATTR) {
1910 		bool_t cflag = ((flags & CREATE_XATTR_DIR) != 0);
1911 		mntinfo_t *mi;
1912 
1913 		mi = VTOMI(dvp);
1914 		if (!(mi->mi_flags & MI_EXTATTR))
1915 			return (EINVAL);
1916 
1917 		if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp)))
1918 			return (EINTR);
1919 
1920 		(void) nfs3lookup_dnlc(dvp, XATTR_DIR_NAME, &avp, cr);
1921 		if (avp == NULL)
1922 			error = acl_getxattrdir3(dvp, &avp, cflag, cr, 0);
1923 		else
1924 			error = 0;
1925 
1926 		nfs_rw_exit(&drp->r_rwlock);
1927 
1928 		if (error) {
1929 			if (mi->mi_flags & MI_EXTATTR)
1930 				return (error);
1931 			return (EINVAL);
1932 		}
1933 		dvp = avp;
1934 		drp = VTOR(dvp);
1935 	}
1936 
1937 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_READER, INTR(dvp))) {
1938 		error = EINTR;
1939 		goto out;
1940 	}
1941 
1942 	error = nfs3lookup(dvp, nm, vpp, pnp, flags, rdir, cr, 0);
1943 
1944 	nfs_rw_exit(&drp->r_rwlock);
1945 
1946 	/*
1947 	 * If vnode is a device, create special vnode.
1948 	 */
1949 	if (!error && IS_DEVVP(*vpp)) {
1950 		vp = *vpp;
1951 		*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
1952 		VN_RELE(vp);
1953 	}
1954 
1955 out:
1956 	if (avp != NULL)
1957 		VN_RELE(avp);
1958 
1959 	return (error);
1960 }
1961 
1962 static int nfs3_lookup_neg_cache = 1;
1963 
1964 #ifdef DEBUG
1965 static int nfs3_lookup_dnlc_hits = 0;
1966 static int nfs3_lookup_dnlc_misses = 0;
1967 static int nfs3_lookup_dnlc_neg_hits = 0;
1968 static int nfs3_lookup_dnlc_disappears = 0;
1969 static int nfs3_lookup_dnlc_lookups = 0;
1970 #endif
1971 
1972 /* ARGSUSED */
1973 int
1974 nfs3lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1975 	int flags, vnode_t *rdir, cred_t *cr, int rfscall_flags)
1976 {
1977 	int error;
1978 	rnode_t *drp;
1979 
1980 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
1981 	/*
1982 	 * If lookup is for "", just return dvp.  Don't need
1983 	 * to send it over the wire, look it up in the dnlc,
1984 	 * or perform any access checks.
1985 	 */
1986 	if (*nm == '\0') {
1987 		VN_HOLD(dvp);
1988 		*vpp = dvp;
1989 		return (0);
1990 	}
1991 
1992 	/*
1993 	 * Can't do lookups in non-directories.
1994 	 */
1995 	if (dvp->v_type != VDIR)
1996 		return (ENOTDIR);
1997 
1998 	/*
1999 	 * If we're called with RFSCALL_SOFT, it's important that
2000 	 * the only rfscall is one we make directly; if we permit
2001 	 * an access call because we're looking up "." or validating
2002 	 * a dnlc hit, we'll deadlock because that rfscall will not
2003 	 * have the RFSCALL_SOFT set.
2004 	 */
2005 	if (rfscall_flags & RFSCALL_SOFT)
2006 		goto callit;
2007 
2008 	/*
2009 	 * If lookup is for ".", just return dvp.  Don't need
2010 	 * to send it over the wire or look it up in the dnlc,
2011 	 * just need to check access.
2012 	 */
2013 	if (strcmp(nm, ".") == 0) {
2014 		error = nfs3_access(dvp, VEXEC, 0, cr, NULL);
2015 		if (error)
2016 			return (error);
2017 		VN_HOLD(dvp);
2018 		*vpp = dvp;
2019 		return (0);
2020 	}
2021 
2022 	drp = VTOR(dvp);
2023 	if (!(drp->r_flags & RLOOKUP)) {
2024 		mutex_enter(&drp->r_statelock);
2025 		drp->r_flags |= RLOOKUP;
2026 		mutex_exit(&drp->r_statelock);
2027 	}
2028 
2029 	/*
2030 	 * Lookup this name in the DNLC.  If there was a valid entry,
2031 	 * then return the results of the lookup.
2032 	 */
2033 	error = nfs3lookup_dnlc(dvp, nm, vpp, cr);
2034 	if (error || *vpp != NULL)
2035 		return (error);
2036 
2037 callit:
2038 	error = nfs3lookup_otw(dvp, nm, vpp, cr, rfscall_flags);
2039 
2040 	return (error);
2041 }
2042 
2043 static int
2044 nfs3lookup_dnlc(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr)
2045 {
2046 	int error;
2047 	vnode_t *vp;
2048 
2049 	ASSERT(*nm != '\0');
2050 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2051 	/*
2052 	 * Lookup this name in the DNLC.  If successful, then validate
2053 	 * the caches and then recheck the DNLC.  The DNLC is rechecked
2054 	 * just in case this entry got invalidated during the call
2055 	 * to nfs3_validate_caches.
2056 	 *
2057 	 * An assumption is being made that it is safe to say that a
2058 	 * file exists which may not on the server.  Any operations to
2059 	 * the server will fail with ESTALE.
2060 	 */
2061 #ifdef DEBUG
2062 	nfs3_lookup_dnlc_lookups++;
2063 #endif
2064 	vp = dnlc_lookup(dvp, nm);
2065 	if (vp != NULL) {
2066 		VN_RELE(vp);
2067 		if (vp == DNLC_NO_VNODE && !vn_is_readonly(dvp)) {
2068 			PURGE_ATTRCACHE(dvp);
2069 		}
2070 		error = nfs3_validate_caches(dvp, cr);
2071 		if (error)
2072 			return (error);
2073 		vp = dnlc_lookup(dvp, nm);
2074 		if (vp != NULL) {
2075 			error = nfs3_access(dvp, VEXEC, 0, cr, NULL);
2076 			if (error) {
2077 				VN_RELE(vp);
2078 				return (error);
2079 			}
2080 			if (vp == DNLC_NO_VNODE) {
2081 				VN_RELE(vp);
2082 #ifdef DEBUG
2083 				nfs3_lookup_dnlc_neg_hits++;
2084 #endif
2085 				return (ENOENT);
2086 			}
2087 			*vpp = vp;
2088 #ifdef DEBUG
2089 			nfs3_lookup_dnlc_hits++;
2090 #endif
2091 			return (0);
2092 		}
2093 #ifdef DEBUG
2094 		nfs3_lookup_dnlc_disappears++;
2095 #endif
2096 	}
2097 #ifdef DEBUG
2098 	else
2099 		nfs3_lookup_dnlc_misses++;
2100 #endif
2101 
2102 	*vpp = NULL;
2103 
2104 	return (0);
2105 }
2106 
2107 static int
2108 nfs3lookup_otw(vnode_t *dvp, char *nm, vnode_t **vpp, cred_t *cr,
2109 	int rfscall_flags)
2110 {
2111 	int error;
2112 	LOOKUP3args args;
2113 	LOOKUP3vres res;
2114 	int douprintf;
2115 	struct vattr vattr;
2116 	struct vattr dvattr;
2117 	vnode_t *vp;
2118 	failinfo_t fi;
2119 	hrtime_t t;
2120 
2121 	ASSERT(*nm != '\0');
2122 	ASSERT(dvp->v_type == VDIR);
2123 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2124 
2125 	setdiropargs3(&args.what, nm, dvp);
2126 
2127 	fi.vp = dvp;
2128 	fi.fhp = (caddr_t)&args.what.dir;
2129 	fi.copyproc = nfs3copyfh;
2130 	fi.lookupproc = nfs3lookup;
2131 	fi.xattrdirproc = acl_getxattrdir3;
2132 	res.obj_attributes.fres.vp = dvp;
2133 	res.obj_attributes.fres.vap = &vattr;
2134 	res.dir_attributes.fres.vp = dvp;
2135 	res.dir_attributes.fres.vap = &dvattr;
2136 
2137 	douprintf = 1;
2138 
2139 	t = gethrtime();
2140 
2141 	error = rfs3call(VTOMI(dvp), NFSPROC3_LOOKUP,
2142 	    xdr_diropargs3, (caddr_t)&args,
2143 	    xdr_LOOKUP3vres, (caddr_t)&res, cr,
2144 	    &douprintf, &res.status, rfscall_flags, &fi);
2145 
2146 	if (error)
2147 		return (error);
2148 
2149 	nfs3_cache_post_op_vattr(dvp, &res.dir_attributes, t, cr);
2150 
2151 	error = geterrno3(res.status);
2152 	if (error) {
2153 		PURGE_STALE_FH(error, dvp, cr);
2154 		if (error == ENOENT && nfs3_lookup_neg_cache)
2155 			dnlc_enter(dvp, nm, DNLC_NO_VNODE);
2156 		return (error);
2157 	}
2158 
2159 	if (res.obj_attributes.attributes) {
2160 		vp = makenfs3node_va(&res.object, res.obj_attributes.fres.vap,
2161 		    dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
2162 	} else {
2163 		vp = makenfs3node_va(&res.object, NULL,
2164 		    dvp->v_vfsp, t, cr, VTOR(dvp)->r_path, nm);
2165 		if (vp->v_type == VNON) {
2166 			vattr.va_mask = AT_TYPE;
2167 			error = nfs3getattr(vp, &vattr, cr);
2168 			if (error) {
2169 				VN_RELE(vp);
2170 				return (error);
2171 			}
2172 			vp->v_type = vattr.va_type;
2173 		}
2174 	}
2175 
2176 	if (!(rfscall_flags & RFSCALL_SOFT))
2177 		dnlc_update(dvp, nm, vp);
2178 
2179 	*vpp = vp;
2180 
2181 	return (error);
2182 }
2183 
2184 #ifdef DEBUG
2185 static int nfs3_create_misses = 0;
2186 #endif
2187 
2188 /* ARGSUSED */
2189 static int
2190 nfs3_create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2191 	int mode, vnode_t **vpp, cred_t *cr, int lfaware, caller_context_t *ct,
2192 	vsecattr_t *vsecp)
2193 {
2194 	int error;
2195 	vnode_t *vp;
2196 	rnode_t *rp;
2197 	struct vattr vattr;
2198 	rnode_t *drp;
2199 	vnode_t *tempvp;
2200 
2201 	drp = VTOR(dvp);
2202 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
2203 		return (EPERM);
2204 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2205 		return (EINTR);
2206 
2207 top:
2208 	/*
2209 	 * We make a copy of the attributes because the caller does not
2210 	 * expect us to change what va points to.
2211 	 */
2212 	vattr = *va;
2213 
2214 	/*
2215 	 * If the pathname is "", just use dvp.  Don't need
2216 	 * to send it over the wire, look it up in the dnlc,
2217 	 * or perform any access checks.
2218 	 */
2219 	if (*nm == '\0') {
2220 		error = 0;
2221 		VN_HOLD(dvp);
2222 		vp = dvp;
2223 	/*
2224 	 * If the pathname is ".", just use dvp.  Don't need
2225 	 * to send it over the wire or look it up in the dnlc,
2226 	 * just need to check access.
2227 	 */
2228 	} else if (strcmp(nm, ".") == 0) {
2229 		error = nfs3_access(dvp, VEXEC, 0, cr, ct);
2230 		if (error) {
2231 			nfs_rw_exit(&drp->r_rwlock);
2232 			return (error);
2233 		}
2234 		VN_HOLD(dvp);
2235 		vp = dvp;
2236 	/*
2237 	 * We need to go over the wire, just to be sure whether the
2238 	 * file exists or not.  Using the DNLC can be dangerous in
2239 	 * this case when making a decision regarding existence.
2240 	 */
2241 	} else {
2242 		error = nfs3lookup_otw(dvp, nm, &vp, cr, 0);
2243 	}
2244 	if (!error) {
2245 		if (exclusive == EXCL)
2246 			error = EEXIST;
2247 		else if (vp->v_type == VDIR && (mode & VWRITE))
2248 			error = EISDIR;
2249 		else {
2250 			/*
2251 			 * If vnode is a device, create special vnode.
2252 			 */
2253 			if (IS_DEVVP(vp)) {
2254 				tempvp = vp;
2255 				vp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2256 				VN_RELE(tempvp);
2257 			}
2258 			if (!(error = VOP_ACCESS(vp, mode, 0, cr, ct))) {
2259 				if ((vattr.va_mask & AT_SIZE) &&
2260 				    vp->v_type == VREG) {
2261 					rp = VTOR(vp);
2262 					/*
2263 					 * Check here for large file handled
2264 					 * by LF-unaware process (as
2265 					 * ufs_create() does)
2266 					 */
2267 					if (!(lfaware & FOFFMAX)) {
2268 						mutex_enter(&rp->r_statelock);
2269 						if (rp->r_size > MAXOFF32_T)
2270 							error = EOVERFLOW;
2271 						mutex_exit(&rp->r_statelock);
2272 					}
2273 					if (!error) {
2274 						vattr.va_mask = AT_SIZE;
2275 						error = nfs3setattr(vp,
2276 						    &vattr, 0, cr);
2277 					}
2278 				}
2279 			}
2280 		}
2281 		nfs_rw_exit(&drp->r_rwlock);
2282 		if (error) {
2283 			VN_RELE(vp);
2284 		} else {
2285 			/*
2286 			 * existing file got truncated, notify.
2287 			 */
2288 			vnevent_create(vp, ct);
2289 			*vpp = vp;
2290 		}
2291 		return (error);
2292 	}
2293 
2294 	dnlc_remove(dvp, nm);
2295 
2296 	/*
2297 	 * Decide what the group-id of the created file should be.
2298 	 * Set it in attribute list as advisory...
2299 	 */
2300 	error = setdirgid(dvp, &vattr.va_gid, cr);
2301 	if (error) {
2302 		nfs_rw_exit(&drp->r_rwlock);
2303 		return (error);
2304 	}
2305 	vattr.va_mask |= AT_GID;
2306 
2307 	ASSERT(vattr.va_mask & AT_TYPE);
2308 	if (vattr.va_type == VREG) {
2309 		ASSERT(vattr.va_mask & AT_MODE);
2310 		if (MANDMODE(vattr.va_mode)) {
2311 			nfs_rw_exit(&drp->r_rwlock);
2312 			return (EACCES);
2313 		}
2314 		error = nfs3create(dvp, nm, &vattr, exclusive, mode, vpp, cr,
2315 		    lfaware);
2316 		/*
2317 		 * If this is not an exclusive create, then the CREATE
2318 		 * request will be made with the GUARDED mode set.  This
2319 		 * means that the server will return EEXIST if the file
2320 		 * exists.  The file could exist because of a retransmitted
2321 		 * request.  In this case, we recover by starting over and
2322 		 * checking to see whether the file exists.  This second
2323 		 * time through it should and a CREATE request will not be
2324 		 * sent.
2325 		 *
2326 		 * This handles the problem of a dangling CREATE request
2327 		 * which contains attributes which indicate that the file
2328 		 * should be truncated.  This retransmitted request could
2329 		 * possibly truncate valid data in the file if not caught
2330 		 * by the duplicate request mechanism on the server or if
2331 		 * not caught by other means.  The scenario is:
2332 		 *
2333 		 * Client transmits CREATE request with size = 0
2334 		 * Client times out, retransmits request.
2335 		 * Response to the first request arrives from the server
2336 		 *  and the client proceeds on.
2337 		 * Client writes data to the file.
2338 		 * The server now processes retransmitted CREATE request
2339 		 *  and truncates file.
2340 		 *
2341 		 * The use of the GUARDED CREATE request prevents this from
2342 		 * happening because the retransmitted CREATE would fail
2343 		 * with EEXIST and would not truncate the file.
2344 		 */
2345 		if (error == EEXIST && exclusive == NONEXCL) {
2346 #ifdef DEBUG
2347 			nfs3_create_misses++;
2348 #endif
2349 			goto top;
2350 		}
2351 		nfs_rw_exit(&drp->r_rwlock);
2352 		return (error);
2353 	}
2354 	error = nfs3mknod(dvp, nm, &vattr, exclusive, mode, vpp, cr);
2355 	nfs_rw_exit(&drp->r_rwlock);
2356 	return (error);
2357 }
2358 
2359 /* ARGSUSED */
2360 static int
2361 nfs3create(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2362 	int mode, vnode_t **vpp, cred_t *cr, int lfaware)
2363 {
2364 	int error;
2365 	CREATE3args args;
2366 	CREATE3res res;
2367 	int douprintf;
2368 	vnode_t *vp;
2369 	struct vattr vattr;
2370 	nfstime3 *verfp;
2371 	rnode_t *rp;
2372 	timestruc_t now;
2373 	hrtime_t t;
2374 
2375 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2376 	setdiropargs3(&args.where, nm, dvp);
2377 	if (exclusive == EXCL) {
2378 		args.how.mode = EXCLUSIVE;
2379 		/*
2380 		 * Construct the create verifier.  This verifier needs
2381 		 * to be unique between different clients.  It also needs
2382 		 * to vary for each exclusive create request generated
2383 		 * from the client to the server.
2384 		 *
2385 		 * The first attempt is made to use the hostid and a
2386 		 * unique number on the client.  If the hostid has not
2387 		 * been set, the high resolution time that the exclusive
2388 		 * create request is being made is used.  This will work
2389 		 * unless two different clients, both with the hostid
2390 		 * not set, attempt an exclusive create request on the
2391 		 * same file, at exactly the same clock time.  The
2392 		 * chances of this happening seem small enough to be
2393 		 * reasonable.
2394 		 */
2395 		verfp = (nfstime3 *)&args.how.createhow3_u.verf;
2396 		verfp->seconds = nfs_atoi(hw_serial);
2397 		if (verfp->seconds != 0)
2398 			verfp->nseconds = newnum();
2399 		else {
2400 			gethrestime(&now);
2401 			verfp->seconds = now.tv_sec;
2402 			verfp->nseconds = now.tv_nsec;
2403 		}
2404 		/*
2405 		 * Since the server will use this value for the mtime,
2406 		 * make sure that it can't overflow. Zero out the MSB.
2407 		 * The actual value does not matter here, only its uniqeness.
2408 		 */
2409 		verfp->seconds %= INT32_MAX;
2410 	} else {
2411 		/*
2412 		 * Issue the non-exclusive create in guarded mode.  This
2413 		 * may result in some false EEXIST responses for
2414 		 * retransmitted requests, but these will be handled at
2415 		 * a higher level.  By using GUARDED, duplicate requests
2416 		 * to do file truncation and possible access problems
2417 		 * can be avoided.
2418 		 */
2419 		args.how.mode = GUARDED;
2420 		error = vattr_to_sattr3(va,
2421 		    &args.how.createhow3_u.obj_attributes);
2422 		if (error) {
2423 			/* req time field(s) overflow - return immediately */
2424 			return (error);
2425 		}
2426 	}
2427 
2428 	douprintf = 1;
2429 
2430 	t = gethrtime();
2431 
2432 	error = rfs3call(VTOMI(dvp), NFSPROC3_CREATE,
2433 	    xdr_CREATE3args, (caddr_t)&args,
2434 	    xdr_CREATE3res, (caddr_t)&res, cr,
2435 	    &douprintf, &res.status, 0, NULL);
2436 
2437 	if (error) {
2438 		PURGE_ATTRCACHE(dvp);
2439 		return (error);
2440 	}
2441 
2442 	error = geterrno3(res.status);
2443 	if (!error) {
2444 		nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
2445 		if (HAVE_RDDIR_CACHE(VTOR(dvp)))
2446 			nfs_purge_rddir_cache(dvp);
2447 
2448 		/*
2449 		 * On exclusive create the times need to be explicitly
2450 		 * set to clear any potential verifier that may be stored
2451 		 * in one of these fields (see comment below).  This
2452 		 * is done here to cover the case where no post op attrs
2453 		 * were returned or a 'invalid' time was returned in
2454 		 * the attributes.
2455 		 */
2456 		if (exclusive == EXCL)
2457 			va->va_mask |= (AT_MTIME | AT_ATIME);
2458 
2459 		if (!res.resok.obj.handle_follows) {
2460 			error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2461 			if (error)
2462 				return (error);
2463 		} else {
2464 			if (res.resok.obj_attributes.attributes) {
2465 				vp = makenfs3node(&res.resok.obj.handle,
2466 				    &res.resok.obj_attributes.attr,
2467 				    dvp->v_vfsp, t, cr, NULL, NULL);
2468 			} else {
2469 				vp = makenfs3node(&res.resok.obj.handle, NULL,
2470 				    dvp->v_vfsp, t, cr, NULL, NULL);
2471 
2472 				/*
2473 				 * On an exclusive create, it is possible
2474 				 * that attributes were returned but those
2475 				 * postop attributes failed to decode
2476 				 * properly.  If this is the case,
2477 				 * then most likely the atime or mtime
2478 				 * were invalid for our client; this
2479 				 * is caused by the server storing the
2480 				 * create verifier in one of the time
2481 				 * fields(most likely mtime).
2482 				 * So... we are going to setattr just the
2483 				 * atime/mtime to clear things up.
2484 				 */
2485 				if (exclusive == EXCL) {
2486 					if (error =
2487 					    nfs3excl_create_settimes(vp,
2488 					    va, cr)) {
2489 						/*
2490 						 * Setting the times failed.
2491 						 * Remove the file and return
2492 						 * the error.
2493 						 */
2494 						VN_RELE(vp);
2495 						(void) nfs3_remove(dvp,
2496 						    nm, cr, NULL, 0);
2497 						return (error);
2498 					}
2499 				}
2500 
2501 				/*
2502 				 * This handles the non-exclusive case
2503 				 * and the exclusive case where no post op
2504 				 * attrs were returned.
2505 				 */
2506 				if (vp->v_type == VNON) {
2507 					vattr.va_mask = AT_TYPE;
2508 					error = nfs3getattr(vp, &vattr, cr);
2509 					if (error) {
2510 						VN_RELE(vp);
2511 						return (error);
2512 					}
2513 					vp->v_type = vattr.va_type;
2514 				}
2515 			}
2516 			dnlc_update(dvp, nm, vp);
2517 		}
2518 
2519 		rp = VTOR(vp);
2520 
2521 		/*
2522 		 * Check here for large file handled by
2523 		 * LF-unaware process (as ufs_create() does)
2524 		 */
2525 		if ((va->va_mask & AT_SIZE) && vp->v_type == VREG &&
2526 		    !(lfaware & FOFFMAX)) {
2527 			mutex_enter(&rp->r_statelock);
2528 			if (rp->r_size > MAXOFF32_T) {
2529 				mutex_exit(&rp->r_statelock);
2530 				VN_RELE(vp);
2531 				return (EOVERFLOW);
2532 			}
2533 			mutex_exit(&rp->r_statelock);
2534 		}
2535 
2536 		if (exclusive == EXCL &&
2537 		    (va->va_mask & ~(AT_GID | AT_SIZE))) {
2538 			/*
2539 			 * If doing an exclusive create, then generate
2540 			 * a SETATTR to set the initial attributes.
2541 			 * Try to set the mtime and the atime to the
2542 			 * server's current time.  It is somewhat
2543 			 * expected that these fields will be used to
2544 			 * store the exclusive create cookie.  If not,
2545 			 * server implementors will need to know that
2546 			 * a SETATTR will follow an exclusive create
2547 			 * and the cookie should be destroyed if
2548 			 * appropriate. This work may have been done
2549 			 * earlier in this function if post op attrs
2550 			 * were not available.
2551 			 *
2552 			 * The AT_GID and AT_SIZE bits are turned off
2553 			 * so that the SETATTR request will not attempt
2554 			 * to process these.  The gid will be set
2555 			 * separately if appropriate.  The size is turned
2556 			 * off because it is assumed that a new file will
2557 			 * be created empty and if the file wasn't empty,
2558 			 * then the exclusive create will have failed
2559 			 * because the file must have existed already.
2560 			 * Therefore, no truncate operation is needed.
2561 			 */
2562 			va->va_mask &= ~(AT_GID | AT_SIZE);
2563 			error = nfs3setattr(vp, va, 0, cr);
2564 			if (error) {
2565 				/*
2566 				 * Couldn't correct the attributes of
2567 				 * the newly created file and the
2568 				 * attributes are wrong.  Remove the
2569 				 * file and return an error to the
2570 				 * application.
2571 				 */
2572 				VN_RELE(vp);
2573 				(void) nfs3_remove(dvp, nm, cr, NULL, 0);
2574 				return (error);
2575 			}
2576 		}
2577 
2578 		if (va->va_gid != rp->r_attr.va_gid) {
2579 			/*
2580 			 * If the gid on the file isn't right, then
2581 			 * generate a SETATTR to attempt to change
2582 			 * it.  This may or may not work, depending
2583 			 * upon the server's semantics for allowing
2584 			 * file ownership changes.
2585 			 */
2586 			va->va_mask = AT_GID;
2587 			(void) nfs3setattr(vp, va, 0, cr);
2588 		}
2589 
2590 		/*
2591 		 * If vnode is a device create special vnode
2592 		 */
2593 		if (IS_DEVVP(vp)) {
2594 			*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2595 			VN_RELE(vp);
2596 		} else
2597 			*vpp = vp;
2598 	} else {
2599 		nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
2600 		PURGE_STALE_FH(error, dvp, cr);
2601 	}
2602 
2603 	return (error);
2604 }
2605 
2606 /*
2607  * Special setattr function to take care of rest of atime/mtime
2608  * after successful exclusive create.  This function exists to avoid
2609  * handling attributes from the server; exclusive the atime/mtime fields
2610  * may be 'invalid' in client's view and therefore can not be trusted.
2611  */
2612 static int
2613 nfs3excl_create_settimes(vnode_t *vp, struct vattr *vap, cred_t *cr)
2614 {
2615 	int error;
2616 	uint_t mask;
2617 	SETATTR3args args;
2618 	SETATTR3res res;
2619 	int douprintf;
2620 	rnode_t *rp;
2621 	hrtime_t t;
2622 
2623 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
2624 	/* save the caller's mask so that it can be reset later */
2625 	mask = vap->va_mask;
2626 
2627 	rp = VTOR(vp);
2628 
2629 	args.object = *RTOFH3(rp);
2630 	args.guard.check = FALSE;
2631 
2632 	/* Use the mask to initialize the arguments */
2633 	vap->va_mask = 0;
2634 	error = vattr_to_sattr3(vap, &args.new_attributes);
2635 
2636 	/* We want to set just atime/mtime on this request */
2637 	args.new_attributes.atime.set_it = SET_TO_SERVER_TIME;
2638 	args.new_attributes.mtime.set_it = SET_TO_SERVER_TIME;
2639 
2640 	douprintf = 1;
2641 
2642 	t = gethrtime();
2643 
2644 	error = rfs3call(VTOMI(vp), NFSPROC3_SETATTR,
2645 	    xdr_SETATTR3args, (caddr_t)&args,
2646 	    xdr_SETATTR3res, (caddr_t)&res, cr,
2647 	    &douprintf, &res.status, 0, NULL);
2648 
2649 	if (error) {
2650 		vap->va_mask = mask;
2651 		return (error);
2652 	}
2653 
2654 	error = geterrno3(res.status);
2655 	if (!error) {
2656 		/*
2657 		 * It is important to pick up the attributes.
2658 		 * Since this is the exclusive create path, the
2659 		 * attributes on the initial create were ignored
2660 		 * and we need these to have the correct info.
2661 		 */
2662 		nfs3_cache_wcc_data(vp, &res.resok.obj_wcc, t, cr);
2663 		/*
2664 		 * No need to do the atime/mtime work again so clear
2665 		 * the bits.
2666 		 */
2667 		mask &= ~(AT_ATIME | AT_MTIME);
2668 	} else {
2669 		nfs3_cache_wcc_data(vp, &res.resfail.obj_wcc, t, cr);
2670 	}
2671 
2672 	vap->va_mask = mask;
2673 
2674 	return (error);
2675 }
2676 
2677 /* ARGSUSED */
2678 static int
2679 nfs3mknod(vnode_t *dvp, char *nm, struct vattr *va, enum vcexcl exclusive,
2680 	int mode, vnode_t **vpp, cred_t *cr)
2681 {
2682 	int error;
2683 	MKNOD3args args;
2684 	MKNOD3res res;
2685 	int douprintf;
2686 	vnode_t *vp;
2687 	struct vattr vattr;
2688 	hrtime_t t;
2689 
2690 	ASSERT(nfs_zone() == VTOMI(dvp)->mi_zone);
2691 	switch (va->va_type) {
2692 	case VCHR:
2693 	case VBLK:
2694 		setdiropargs3(&args.where, nm, dvp);
2695 		args.what.type = (va->va_type == VCHR) ? NF3CHR : NF3BLK;
2696 		error = vattr_to_sattr3(va,
2697 		    &args.what.mknoddata3_u.device.dev_attributes);
2698 		if (error) {
2699 			/* req time field(s) overflow - return immediately */
2700 			return (error);
2701 		}
2702 		args.what.mknoddata3_u.device.spec.specdata1 =
2703 		    getmajor(va->va_rdev);
2704 		args.what.mknoddata3_u.device.spec.specdata2 =
2705 		    getminor(va->va_rdev);
2706 		break;
2707 
2708 	case VFIFO:
2709 	case VSOCK:
2710 		setdiropargs3(&args.where, nm, dvp);
2711 		args.what.type = (va->va_type == VFIFO) ? NF3FIFO : NF3SOCK;
2712 		error = vattr_to_sattr3(va,
2713 		    &args.what.mknoddata3_u.pipe_attributes);
2714 		if (error) {
2715 			/* req time field(s) overflow - return immediately */
2716 			return (error);
2717 		}
2718 		break;
2719 
2720 	default:
2721 		return (EINVAL);
2722 	}
2723 
2724 	douprintf = 1;
2725 
2726 	t = gethrtime();
2727 
2728 	error = rfs3call(VTOMI(dvp), NFSPROC3_MKNOD,
2729 	    xdr_MKNOD3args, (caddr_t)&args,
2730 	    xdr_MKNOD3res, (caddr_t)&res, cr,
2731 	    &douprintf, &res.status, 0, NULL);
2732 
2733 	if (error) {
2734 		PURGE_ATTRCACHE(dvp);
2735 		return (error);
2736 	}
2737 
2738 	error = geterrno3(res.status);
2739 	if (!error) {
2740 		nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
2741 		if (HAVE_RDDIR_CACHE(VTOR(dvp)))
2742 			nfs_purge_rddir_cache(dvp);
2743 
2744 		if (!res.resok.obj.handle_follows) {
2745 			error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2746 			if (error)
2747 				return (error);
2748 		} else {
2749 			if (res.resok.obj_attributes.attributes) {
2750 				vp = makenfs3node(&res.resok.obj.handle,
2751 				    &res.resok.obj_attributes.attr,
2752 				    dvp->v_vfsp, t, cr, NULL, NULL);
2753 			} else {
2754 				vp = makenfs3node(&res.resok.obj.handle, NULL,
2755 				    dvp->v_vfsp, t, cr, NULL, NULL);
2756 				if (vp->v_type == VNON) {
2757 					vattr.va_mask = AT_TYPE;
2758 					error = nfs3getattr(vp, &vattr, cr);
2759 					if (error) {
2760 						VN_RELE(vp);
2761 						return (error);
2762 					}
2763 					vp->v_type = vattr.va_type;
2764 				}
2765 
2766 			}
2767 			dnlc_update(dvp, nm, vp);
2768 		}
2769 
2770 		if (va->va_gid != VTOR(vp)->r_attr.va_gid) {
2771 			va->va_mask = AT_GID;
2772 			(void) nfs3setattr(vp, va, 0, cr);
2773 		}
2774 
2775 		/*
2776 		 * If vnode is a device create special vnode
2777 		 */
2778 		if (IS_DEVVP(vp)) {
2779 			*vpp = specvp(vp, vp->v_rdev, vp->v_type, cr);
2780 			VN_RELE(vp);
2781 		} else
2782 			*vpp = vp;
2783 	} else {
2784 		nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
2785 		PURGE_STALE_FH(error, dvp, cr);
2786 	}
2787 	return (error);
2788 }
2789 
2790 /*
2791  * Weirdness: if the vnode to be removed is open
2792  * we rename it instead of removing it and nfs_inactive
2793  * will remove the new name.
2794  */
2795 /* ARGSUSED */
2796 static int
2797 nfs3_remove(vnode_t *dvp, char *nm, cred_t *cr, caller_context_t *ct, int flags)
2798 {
2799 	int error;
2800 	REMOVE3args args;
2801 	REMOVE3res res;
2802 	vnode_t *vp;
2803 	char *tmpname;
2804 	int douprintf;
2805 	rnode_t *rp;
2806 	rnode_t *drp;
2807 	hrtime_t t;
2808 
2809 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
2810 		return (EPERM);
2811 	drp = VTOR(dvp);
2812 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
2813 		return (EINTR);
2814 
2815 	error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
2816 	if (error) {
2817 		nfs_rw_exit(&drp->r_rwlock);
2818 		return (error);
2819 	}
2820 
2821 	if (vp->v_type == VDIR && secpolicy_fs_linkdir(cr, dvp->v_vfsp)) {
2822 		VN_RELE(vp);
2823 		nfs_rw_exit(&drp->r_rwlock);
2824 		return (EPERM);
2825 	}
2826 
2827 	/*
2828 	 * First just remove the entry from the name cache, as it
2829 	 * is most likely the only entry for this vp.
2830 	 */
2831 	dnlc_remove(dvp, nm);
2832 
2833 	/*
2834 	 * If the file has a v_count > 1 then there may be more than one
2835 	 * entry in the name cache due multiple links or an open file,
2836 	 * but we don't have the real reference count so flush all
2837 	 * possible entries.
2838 	 */
2839 	if (vp->v_count > 1)
2840 		dnlc_purge_vp(vp);
2841 
2842 	/*
2843 	 * Now we have the real reference count on the vnode
2844 	 */
2845 	rp = VTOR(vp);
2846 	mutex_enter(&rp->r_statelock);
2847 	if (vp->v_count > 1 &&
2848 	    (rp->r_unldvp == NULL || strcmp(nm, rp->r_unlname) == 0)) {
2849 		mutex_exit(&rp->r_statelock);
2850 		tmpname = newname();
2851 		error = nfs3rename(dvp, nm, dvp, tmpname, cr, ct);
2852 		if (error)
2853 			kmem_free(tmpname, MAXNAMELEN);
2854 		else {
2855 			mutex_enter(&rp->r_statelock);
2856 			if (rp->r_unldvp == NULL) {
2857 				VN_HOLD(dvp);
2858 				rp->r_unldvp = dvp;
2859 				if (rp->r_unlcred != NULL)
2860 					crfree(rp->r_unlcred);
2861 				crhold(cr);
2862 				rp->r_unlcred = cr;
2863 				rp->r_unlname = tmpname;
2864 			} else {
2865 				kmem_free(rp->r_unlname, MAXNAMELEN);
2866 				rp->r_unlname = tmpname;
2867 			}
2868 			mutex_exit(&rp->r_statelock);
2869 		}
2870 	} else {
2871 		mutex_exit(&rp->r_statelock);
2872 		/*
2873 		 * We need to flush any dirty pages which happen to
2874 		 * be hanging around before removing the file.  This
2875 		 * shouldn't happen very often and mostly on file
2876 		 * systems mounted "nocto".
2877 		 */
2878 		if (vn_has_cached_data(vp) &&
2879 		    ((rp->r_flags & RDIRTY) || rp->r_count > 0)) {
2880 			error = nfs3_putpage(vp, (offset_t)0, 0, 0, cr, ct);
2881 			if (error && (error == ENOSPC || error == EDQUOT)) {
2882 				mutex_enter(&rp->r_statelock);
2883 				if (!rp->r_error)
2884 					rp->r_error = error;
2885 				mutex_exit(&rp->r_statelock);
2886 			}
2887 		}
2888 
2889 		setdiropargs3(&args.object, nm, dvp);
2890 
2891 		douprintf = 1;
2892 
2893 		t = gethrtime();
2894 
2895 		error = rfs3call(VTOMI(dvp), NFSPROC3_REMOVE,
2896 		    xdr_diropargs3, (caddr_t)&args,
2897 		    xdr_REMOVE3res, (caddr_t)&res, cr,
2898 		    &douprintf, &res.status, 0, NULL);
2899 
2900 		/*
2901 		 * The xattr dir may be gone after last attr is removed,
2902 		 * so flush it from dnlc.
2903 		 */
2904 		if (dvp->v_flag & V_XATTRDIR)
2905 			dnlc_purge_vp(dvp);
2906 
2907 		PURGE_ATTRCACHE(vp);
2908 
2909 		if (error) {
2910 			PURGE_ATTRCACHE(dvp);
2911 		} else {
2912 			error = geterrno3(res.status);
2913 			if (!error) {
2914 				nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t,
2915 				    cr);
2916 				if (HAVE_RDDIR_CACHE(drp))
2917 					nfs_purge_rddir_cache(dvp);
2918 			} else {
2919 				nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc,
2920 				    t, cr);
2921 				PURGE_STALE_FH(error, dvp, cr);
2922 			}
2923 		}
2924 	}
2925 
2926 	if (error == 0) {
2927 		vnevent_remove(vp, dvp, nm, ct);
2928 	}
2929 	VN_RELE(vp);
2930 
2931 	nfs_rw_exit(&drp->r_rwlock);
2932 
2933 	return (error);
2934 }
2935 
2936 /* ARGSUSED */
2937 static int
2938 nfs3_link(vnode_t *tdvp, vnode_t *svp, char *tnm, cred_t *cr,
2939 	caller_context_t *ct, int flags)
2940 {
2941 	int error;
2942 	LINK3args args;
2943 	LINK3res res;
2944 	vnode_t *realvp;
2945 	int douprintf;
2946 	mntinfo_t *mi;
2947 	rnode_t *tdrp;
2948 	hrtime_t t;
2949 
2950 	if (nfs_zone() != VTOMI(tdvp)->mi_zone)
2951 		return (EPERM);
2952 	if (VOP_REALVP(svp, &realvp, ct) == 0)
2953 		svp = realvp;
2954 
2955 	mi = VTOMI(svp);
2956 
2957 	if (!(mi->mi_flags & MI_LINK))
2958 		return (EOPNOTSUPP);
2959 
2960 	args.file = *VTOFH3(svp);
2961 	setdiropargs3(&args.link, tnm, tdvp);
2962 
2963 	tdrp = VTOR(tdvp);
2964 	if (nfs_rw_enter_sig(&tdrp->r_rwlock, RW_WRITER, INTR(tdvp)))
2965 		return (EINTR);
2966 
2967 	dnlc_remove(tdvp, tnm);
2968 
2969 	douprintf = 1;
2970 
2971 	t = gethrtime();
2972 
2973 	error = rfs3call(mi, NFSPROC3_LINK,
2974 	    xdr_LINK3args, (caddr_t)&args,
2975 	    xdr_LINK3res, (caddr_t)&res, cr,
2976 	    &douprintf, &res.status, 0, NULL);
2977 
2978 	if (error) {
2979 		PURGE_ATTRCACHE(tdvp);
2980 		PURGE_ATTRCACHE(svp);
2981 		nfs_rw_exit(&tdrp->r_rwlock);
2982 		return (error);
2983 	}
2984 
2985 	error = geterrno3(res.status);
2986 
2987 	if (!error) {
2988 		nfs3_cache_post_op_attr(svp, &res.resok.file_attributes, t, cr);
2989 		nfs3_cache_wcc_data(tdvp, &res.resok.linkdir_wcc, t, cr);
2990 		if (HAVE_RDDIR_CACHE(tdrp))
2991 			nfs_purge_rddir_cache(tdvp);
2992 		dnlc_update(tdvp, tnm, svp);
2993 	} else {
2994 		nfs3_cache_post_op_attr(svp, &res.resfail.file_attributes, t,
2995 		    cr);
2996 		nfs3_cache_wcc_data(tdvp, &res.resfail.linkdir_wcc, t, cr);
2997 		if (error == EOPNOTSUPP) {
2998 			mutex_enter(&mi->mi_lock);
2999 			mi->mi_flags &= ~MI_LINK;
3000 			mutex_exit(&mi->mi_lock);
3001 		}
3002 	}
3003 
3004 	nfs_rw_exit(&tdrp->r_rwlock);
3005 
3006 	if (!error) {
3007 		/*
3008 		 * Notify the source file of this link operation.
3009 		 */
3010 		vnevent_link(svp, ct);
3011 	}
3012 	return (error);
3013 }
3014 
3015 /* ARGSUSED */
3016 static int
3017 nfs3_rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
3018 	caller_context_t *ct, int flags)
3019 {
3020 	vnode_t *realvp;
3021 
3022 	if (nfs_zone() != VTOMI(odvp)->mi_zone)
3023 		return (EPERM);
3024 	if (VOP_REALVP(ndvp, &realvp, ct) == 0)
3025 		ndvp = realvp;
3026 
3027 	return (nfs3rename(odvp, onm, ndvp, nnm, cr, ct));
3028 }
3029 
3030 /*
3031  * nfs3rename does the real work of renaming in NFS Version 3.
3032  */
3033 static int
3034 nfs3rename(vnode_t *odvp, char *onm, vnode_t *ndvp, char *nnm, cred_t *cr,
3035     caller_context_t *ct)
3036 {
3037 	int error;
3038 	RENAME3args args;
3039 	RENAME3res res;
3040 	int douprintf;
3041 	vnode_t *nvp = NULL;
3042 	vnode_t *ovp = NULL;
3043 	char *tmpname;
3044 	rnode_t *rp;
3045 	rnode_t *odrp;
3046 	rnode_t *ndrp;
3047 	hrtime_t t;
3048 
3049 	ASSERT(nfs_zone() == VTOMI(odvp)->mi_zone);
3050 
3051 	if (strcmp(onm, ".") == 0 || strcmp(onm, "..") == 0 ||
3052 	    strcmp(nnm, ".") == 0 || strcmp(nnm, "..") == 0)
3053 		return (EINVAL);
3054 
3055 	odrp = VTOR(odvp);
3056 	ndrp = VTOR(ndvp);
3057 	if ((intptr_t)odrp < (intptr_t)ndrp) {
3058 		if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp)))
3059 			return (EINTR);
3060 		if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp))) {
3061 			nfs_rw_exit(&odrp->r_rwlock);
3062 			return (EINTR);
3063 		}
3064 	} else {
3065 		if (nfs_rw_enter_sig(&ndrp->r_rwlock, RW_WRITER, INTR(ndvp)))
3066 			return (EINTR);
3067 		if (nfs_rw_enter_sig(&odrp->r_rwlock, RW_WRITER, INTR(odvp))) {
3068 			nfs_rw_exit(&ndrp->r_rwlock);
3069 			return (EINTR);
3070 		}
3071 	}
3072 
3073 	/*
3074 	 * Lookup the target file.  If it exists, it needs to be
3075 	 * checked to see whether it is a mount point and whether
3076 	 * it is active (open).
3077 	 */
3078 	error = nfs3lookup(ndvp, nnm, &nvp, NULL, 0, NULL, cr, 0);
3079 	if (!error) {
3080 		/*
3081 		 * If this file has been mounted on, then just
3082 		 * return busy because renaming to it would remove
3083 		 * the mounted file system from the name space.
3084 		 */
3085 		if (vn_mountedvfs(nvp) != NULL) {
3086 			VN_RELE(nvp);
3087 			nfs_rw_exit(&odrp->r_rwlock);
3088 			nfs_rw_exit(&ndrp->r_rwlock);
3089 			return (EBUSY);
3090 		}
3091 
3092 		/*
3093 		 * Purge the name cache of all references to this vnode
3094 		 * so that we can check the reference count to infer
3095 		 * whether it is active or not.
3096 		 */
3097 		/*
3098 		 * First just remove the entry from the name cache, as it
3099 		 * is most likely the only entry for this vp.
3100 		 */
3101 		dnlc_remove(ndvp, nnm);
3102 		/*
3103 		 * If the file has a v_count > 1 then there may be more
3104 		 * than one entry in the name cache due multiple links
3105 		 * or an open file, but we don't have the real reference
3106 		 * count so flush all possible entries.
3107 		 */
3108 		if (nvp->v_count > 1)
3109 			dnlc_purge_vp(nvp);
3110 
3111 		/*
3112 		 * If the vnode is active and is not a directory,
3113 		 * arrange to rename it to a
3114 		 * temporary file so that it will continue to be
3115 		 * accessible.  This implements the "unlink-open-file"
3116 		 * semantics for the target of a rename operation.
3117 		 * Before doing this though, make sure that the
3118 		 * source and target files are not already the same.
3119 		 */
3120 		if (nvp->v_count > 1 && nvp->v_type != VDIR) {
3121 			/*
3122 			 * Lookup the source name.
3123 			 */
3124 			error = nfs3lookup(odvp, onm, &ovp, NULL, 0, NULL,
3125 			    cr, 0);
3126 
3127 			/*
3128 			 * The source name *should* already exist.
3129 			 */
3130 			if (error) {
3131 				VN_RELE(nvp);
3132 				nfs_rw_exit(&odrp->r_rwlock);
3133 				nfs_rw_exit(&ndrp->r_rwlock);
3134 				return (error);
3135 			}
3136 
3137 			/*
3138 			 * Compare the two vnodes.  If they are the same,
3139 			 * just release all held vnodes and return success.
3140 			 */
3141 			if (ovp == nvp) {
3142 				VN_RELE(ovp);
3143 				VN_RELE(nvp);
3144 				nfs_rw_exit(&odrp->r_rwlock);
3145 				nfs_rw_exit(&ndrp->r_rwlock);
3146 				return (0);
3147 			}
3148 
3149 			/*
3150 			 * Can't mix and match directories and non-
3151 			 * directories in rename operations.  We already
3152 			 * know that the target is not a directory.  If
3153 			 * the source is a directory, return an error.
3154 			 */
3155 			if (ovp->v_type == VDIR) {
3156 				VN_RELE(ovp);
3157 				VN_RELE(nvp);
3158 				nfs_rw_exit(&odrp->r_rwlock);
3159 				nfs_rw_exit(&ndrp->r_rwlock);
3160 				return (ENOTDIR);
3161 			}
3162 
3163 			/*
3164 			 * The target file exists, is not the same as
3165 			 * the source file, and is active.  Link it
3166 			 * to a temporary filename to avoid having
3167 			 * the server removing the file completely.
3168 			 */
3169 			tmpname = newname();
3170 			error = nfs3_link(ndvp, nvp, tmpname, cr, NULL, 0);
3171 			if (error == EOPNOTSUPP) {
3172 				error = nfs3_rename(ndvp, nnm, ndvp, tmpname,
3173 				    cr, NULL, 0);
3174 			}
3175 			if (error) {
3176 				kmem_free(tmpname, MAXNAMELEN);
3177 				VN_RELE(ovp);
3178 				VN_RELE(nvp);
3179 				nfs_rw_exit(&odrp->r_rwlock);
3180 				nfs_rw_exit(&ndrp->r_rwlock);
3181 				return (error);
3182 			}
3183 			rp = VTOR(nvp);
3184 			mutex_enter(&rp->r_statelock);
3185 			if (rp->r_unldvp == NULL) {
3186 				VN_HOLD(ndvp);
3187 				rp->r_unldvp = ndvp;
3188 				if (rp->r_unlcred != NULL)
3189 					crfree(rp->r_unlcred);
3190 				crhold(cr);
3191 				rp->r_unlcred = cr;
3192 				rp->r_unlname = tmpname;
3193 			} else {
3194 				kmem_free(rp->r_unlname, MAXNAMELEN);
3195 				rp->r_unlname = tmpname;
3196 			}
3197 			mutex_exit(&rp->r_statelock);
3198 		}
3199 	}
3200 
3201 	if (ovp == NULL) {
3202 		/*
3203 		 * When renaming directories to be a subdirectory of a
3204 		 * different parent, the dnlc entry for ".." will no
3205 		 * longer be valid, so it must be removed.
3206 		 *
3207 		 * We do a lookup here to determine whether we are renaming
3208 		 * a directory and we need to check if we are renaming
3209 		 * an unlinked file.  This might have already been done
3210 		 * in previous code, so we check ovp == NULL to avoid
3211 		 * doing it twice.
3212 		 */
3213 
3214 		error = nfs3lookup(odvp, onm, &ovp, NULL, 0, NULL, cr, 0);
3215 		/*
3216 		 * The source name *should* already exist.
3217 		 */
3218 		if (error) {
3219 			nfs_rw_exit(&odrp->r_rwlock);
3220 			nfs_rw_exit(&ndrp->r_rwlock);
3221 			if (nvp) {
3222 				VN_RELE(nvp);
3223 			}
3224 			return (error);
3225 		}
3226 		ASSERT(ovp != NULL);
3227 	}
3228 
3229 	dnlc_remove(odvp, onm);
3230 	dnlc_remove(ndvp, nnm);
3231 
3232 	setdiropargs3(&args.from, onm, odvp);
3233 	setdiropargs3(&args.to, nnm, ndvp);
3234 
3235 	douprintf = 1;
3236 
3237 	t = gethrtime();
3238 
3239 	error = rfs3call(VTOMI(odvp), NFSPROC3_RENAME,
3240 	    xdr_RENAME3args, (caddr_t)&args,
3241 	    xdr_RENAME3res, (caddr_t)&res, cr,
3242 	    &douprintf, &res.status, 0, NULL);
3243 
3244 	if (error) {
3245 		PURGE_ATTRCACHE(odvp);
3246 		PURGE_ATTRCACHE(ndvp);
3247 		VN_RELE(ovp);
3248 		nfs_rw_exit(&odrp->r_rwlock);
3249 		nfs_rw_exit(&ndrp->r_rwlock);
3250 		if (nvp) {
3251 			VN_RELE(nvp);
3252 		}
3253 		return (error);
3254 	}
3255 
3256 	error = geterrno3(res.status);
3257 
3258 	if (!error) {
3259 		nfs3_cache_wcc_data(odvp, &res.resok.fromdir_wcc, t, cr);
3260 		if (HAVE_RDDIR_CACHE(odrp))
3261 			nfs_purge_rddir_cache(odvp);
3262 		if (ndvp != odvp) {
3263 			nfs3_cache_wcc_data(ndvp, &res.resok.todir_wcc, t, cr);
3264 			if (HAVE_RDDIR_CACHE(ndrp))
3265 				nfs_purge_rddir_cache(ndvp);
3266 		}
3267 		/*
3268 		 * when renaming directories to be a subdirectory of a
3269 		 * different parent, the dnlc entry for ".." will no
3270 		 * longer be valid, so it must be removed
3271 		 */
3272 		rp = VTOR(ovp);
3273 		if (ndvp != odvp) {
3274 			if (ovp->v_type == VDIR) {
3275 				dnlc_remove(ovp, "..");
3276 				if (HAVE_RDDIR_CACHE(rp))
3277 					nfs_purge_rddir_cache(ovp);
3278 			}
3279 		}
3280 
3281 		/*
3282 		 * If we are renaming the unlinked file, update the
3283 		 * r_unldvp and r_unlname as needed.
3284 		 */
3285 		mutex_enter(&rp->r_statelock);
3286 		if (rp->r_unldvp != NULL) {
3287 			if (strcmp(rp->r_unlname, onm) == 0) {
3288 				(void) strncpy(rp->r_unlname, nnm, MAXNAMELEN);
3289 				rp->r_unlname[MAXNAMELEN - 1] = '\0';
3290 
3291 				if (ndvp != rp->r_unldvp) {
3292 					VN_RELE(rp->r_unldvp);
3293 					rp->r_unldvp = ndvp;
3294 					VN_HOLD(ndvp);
3295 				}
3296 			}
3297 		}
3298 		mutex_exit(&rp->r_statelock);
3299 	} else {
3300 		nfs3_cache_wcc_data(odvp, &res.resfail.fromdir_wcc, t, cr);
3301 		if (ndvp != odvp) {
3302 			nfs3_cache_wcc_data(ndvp, &res.resfail.todir_wcc, t,
3303 			    cr);
3304 		}
3305 		/*
3306 		 * System V defines rename to return EEXIST, not
3307 		 * ENOTEMPTY if the target directory is not empty.
3308 		 * Over the wire, the error is NFSERR_ENOTEMPTY
3309 		 * which geterrno maps to ENOTEMPTY.
3310 		 */
3311 		if (error == ENOTEMPTY)
3312 			error = EEXIST;
3313 	}
3314 
3315 	if (error == 0) {
3316 		if (nvp)
3317 			vnevent_rename_dest(nvp, ndvp, nnm, ct);
3318 
3319 		if (odvp != ndvp)
3320 			vnevent_rename_dest_dir(ndvp, ct);
3321 		ASSERT(ovp != NULL);
3322 		vnevent_rename_src(ovp, odvp, onm, ct);
3323 	}
3324 
3325 	if (nvp) {
3326 		VN_RELE(nvp);
3327 	}
3328 	VN_RELE(ovp);
3329 
3330 	nfs_rw_exit(&odrp->r_rwlock);
3331 	nfs_rw_exit(&ndrp->r_rwlock);
3332 
3333 	return (error);
3334 }
3335 
3336 /* ARGSUSED */
3337 static int
3338 nfs3_mkdir(vnode_t *dvp, char *nm, struct vattr *va, vnode_t **vpp, cred_t *cr,
3339 	caller_context_t *ct, int flags, vsecattr_t *vsecp)
3340 {
3341 	int error;
3342 	MKDIR3args args;
3343 	MKDIR3res res;
3344 	int douprintf;
3345 	struct vattr vattr;
3346 	vnode_t *vp;
3347 	rnode_t *drp;
3348 	hrtime_t t;
3349 
3350 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
3351 		return (EPERM);
3352 	setdiropargs3(&args.where, nm, dvp);
3353 
3354 	/*
3355 	 * Decide what the group-id and set-gid bit of the created directory
3356 	 * should be.  May have to do a setattr to get the gid right.
3357 	 */
3358 	error = setdirgid(dvp, &va->va_gid, cr);
3359 	if (error)
3360 		return (error);
3361 	error = setdirmode(dvp, &va->va_mode, cr);
3362 	if (error)
3363 		return (error);
3364 	va->va_mask |= AT_MODE|AT_GID;
3365 
3366 	error = vattr_to_sattr3(va, &args.attributes);
3367 	if (error) {
3368 		/* req time field(s) overflow - return immediately */
3369 		return (error);
3370 	}
3371 
3372 	drp = VTOR(dvp);
3373 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
3374 		return (EINTR);
3375 
3376 	dnlc_remove(dvp, nm);
3377 
3378 	douprintf = 1;
3379 
3380 	t = gethrtime();
3381 
3382 	error = rfs3call(VTOMI(dvp), NFSPROC3_MKDIR,
3383 	    xdr_MKDIR3args, (caddr_t)&args,
3384 	    xdr_MKDIR3res, (caddr_t)&res, cr,
3385 	    &douprintf, &res.status, 0, NULL);
3386 
3387 	if (error) {
3388 		PURGE_ATTRCACHE(dvp);
3389 		nfs_rw_exit(&drp->r_rwlock);
3390 		return (error);
3391 	}
3392 
3393 	error = geterrno3(res.status);
3394 	if (!error) {
3395 		nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
3396 		if (HAVE_RDDIR_CACHE(drp))
3397 			nfs_purge_rddir_cache(dvp);
3398 
3399 		if (!res.resok.obj.handle_follows) {
3400 			error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
3401 			if (error) {
3402 				nfs_rw_exit(&drp->r_rwlock);
3403 				return (error);
3404 			}
3405 		} else {
3406 			if (res.resok.obj_attributes.attributes) {
3407 				vp = makenfs3node(&res.resok.obj.handle,
3408 				    &res.resok.obj_attributes.attr,
3409 				    dvp->v_vfsp, t, cr, NULL, NULL);
3410 			} else {
3411 				vp = makenfs3node(&res.resok.obj.handle, NULL,
3412 				    dvp->v_vfsp, t, cr, NULL, NULL);
3413 				if (vp->v_type == VNON) {
3414 					vattr.va_mask = AT_TYPE;
3415 					error = nfs3getattr(vp, &vattr, cr);
3416 					if (error) {
3417 						VN_RELE(vp);
3418 						nfs_rw_exit(&drp->r_rwlock);
3419 						return (error);
3420 					}
3421 					vp->v_type = vattr.va_type;
3422 				}
3423 			}
3424 			dnlc_update(dvp, nm, vp);
3425 		}
3426 		if (va->va_gid != VTOR(vp)->r_attr.va_gid) {
3427 			va->va_mask = AT_GID;
3428 			(void) nfs3setattr(vp, va, 0, cr);
3429 		}
3430 		*vpp = vp;
3431 	} else {
3432 		nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
3433 		PURGE_STALE_FH(error, dvp, cr);
3434 	}
3435 
3436 	nfs_rw_exit(&drp->r_rwlock);
3437 
3438 	return (error);
3439 }
3440 
3441 /* ARGSUSED */
3442 static int
3443 nfs3_rmdir(vnode_t *dvp, char *nm, vnode_t *cdir, cred_t *cr,
3444 	caller_context_t *ct, int flags)
3445 {
3446 	int error;
3447 	RMDIR3args args;
3448 	RMDIR3res res;
3449 	vnode_t *vp;
3450 	int douprintf;
3451 	rnode_t *drp;
3452 	hrtime_t t;
3453 
3454 	if (nfs_zone() != VTOMI(dvp)->mi_zone)
3455 		return (EPERM);
3456 	drp = VTOR(dvp);
3457 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
3458 		return (EINTR);
3459 
3460 	/*
3461 	 * Attempt to prevent a rmdir(".") from succeeding.
3462 	 */
3463 	error = nfs3lookup(dvp, nm, &vp, NULL, 0, NULL, cr, 0);
3464 	if (error) {
3465 		nfs_rw_exit(&drp->r_rwlock);
3466 		return (error);
3467 	}
3468 
3469 	if (vp == cdir) {
3470 		VN_RELE(vp);
3471 		nfs_rw_exit(&drp->r_rwlock);
3472 		return (EINVAL);
3473 	}
3474 
3475 	setdiropargs3(&args.object, nm, dvp);
3476 
3477 	/*
3478 	 * First just remove the entry from the name cache, as it
3479 	 * is most likely an entry for this vp.
3480 	 */
3481 	dnlc_remove(dvp, nm);
3482 
3483 	/*
3484 	 * If there vnode reference count is greater than one, then
3485 	 * there may be additional references in the DNLC which will
3486 	 * need to be purged.  First, trying removing the entry for
3487 	 * the parent directory and see if that removes the additional
3488 	 * reference(s).  If that doesn't do it, then use dnlc_purge_vp
3489 	 * to completely remove any references to the directory which
3490 	 * might still exist in the DNLC.
3491 	 */
3492 	if (vp->v_count > 1) {
3493 		dnlc_remove(vp, "..");
3494 		if (vp->v_count > 1)
3495 			dnlc_purge_vp(vp);
3496 	}
3497 
3498 	douprintf = 1;
3499 
3500 	t = gethrtime();
3501 
3502 	error = rfs3call(VTOMI(dvp), NFSPROC3_RMDIR,
3503 	    xdr_diropargs3, (caddr_t)&args,
3504 	    xdr_RMDIR3res, (caddr_t)&res, cr,
3505 	    &douprintf, &res.status, 0, NULL);
3506 
3507 	PURGE_ATTRCACHE(vp);
3508 
3509 	if (error) {
3510 		PURGE_ATTRCACHE(dvp);
3511 		VN_RELE(vp);
3512 		nfs_rw_exit(&drp->r_rwlock);
3513 		return (error);
3514 	}
3515 
3516 	error = geterrno3(res.status);
3517 	if (!error) {
3518 		nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
3519 		if (HAVE_RDDIR_CACHE(drp))
3520 			nfs_purge_rddir_cache(dvp);
3521 		if (HAVE_RDDIR_CACHE(VTOR(vp)))
3522 			nfs_purge_rddir_cache(vp);
3523 	} else {
3524 		nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
3525 		PURGE_STALE_FH(error, dvp, cr);
3526 		/*
3527 		 * System V defines rmdir to return EEXIST, not
3528 		 * ENOTEMPTY if the directory is not empty.  Over
3529 		 * the wire, the error is NFSERR_ENOTEMPTY which
3530 		 * geterrno maps to ENOTEMPTY.
3531 		 */
3532 		if (error == ENOTEMPTY)
3533 			error = EEXIST;
3534 	}
3535 
3536 	if (error == 0) {
3537 		vnevent_rmdir(vp, dvp, nm, ct);
3538 	}
3539 	VN_RELE(vp);
3540 
3541 	nfs_rw_exit(&drp->r_rwlock);
3542 
3543 	return (error);
3544 }
3545 
3546 /* ARGSUSED */
3547 static int
3548 nfs3_symlink(vnode_t *dvp, char *lnm, struct vattr *tva, char *tnm, cred_t *cr,
3549 	caller_context_t *ct, int flags)
3550 {
3551 	int error;
3552 	SYMLINK3args args;
3553 	SYMLINK3res res;
3554 	int douprintf;
3555 	mntinfo_t *mi;
3556 	vnode_t *vp;
3557 	rnode_t *rp;
3558 	char *contents;
3559 	rnode_t *drp;
3560 	hrtime_t t;
3561 
3562 	mi = VTOMI(dvp);
3563 
3564 	if (nfs_zone() != mi->mi_zone)
3565 		return (EPERM);
3566 	if (!(mi->mi_flags & MI_SYMLINK))
3567 		return (EOPNOTSUPP);
3568 
3569 	setdiropargs3(&args.where, lnm, dvp);
3570 	error = vattr_to_sattr3(tva, &args.symlink.symlink_attributes);
3571 	if (error) {
3572 		/* req time field(s) overflow - return immediately */
3573 		return (error);
3574 	}
3575 	args.symlink.symlink_data = tnm;
3576 
3577 	drp = VTOR(dvp);
3578 	if (nfs_rw_enter_sig(&drp->r_rwlock, RW_WRITER, INTR(dvp)))
3579 		return (EINTR);
3580 
3581 	dnlc_remove(dvp, lnm);
3582 
3583 	douprintf = 1;
3584 
3585 	t = gethrtime();
3586 
3587 	error = rfs3call(mi, NFSPROC3_SYMLINK,
3588 	    xdr_SYMLINK3args, (caddr_t)&args,
3589 	    xdr_SYMLINK3res, (caddr_t)&res, cr,
3590 	    &douprintf, &res.status, 0, NULL);
3591 
3592 	if (error) {
3593 		PURGE_ATTRCACHE(dvp);
3594 		nfs_rw_exit(&drp->r_rwlock);
3595 		return (error);
3596 	}
3597 
3598 	error = geterrno3(res.status);
3599 	if (!error) {
3600 		nfs3_cache_wcc_data(dvp, &res.resok.dir_wcc, t, cr);
3601 		if (HAVE_RDDIR_CACHE(drp))
3602 			nfs_purge_rddir_cache(dvp);
3603 
3604 		if (res.resok.obj.handle_follows) {
3605 			if (res.resok.obj_attributes.attributes) {
3606 				vp = makenfs3node(&res.resok.obj.handle,
3607 				    &res.resok.obj_attributes.attr,
3608 				    dvp->v_vfsp, t, cr, NULL, NULL);
3609 			} else {
3610 				vp = makenfs3node(&res.resok.obj.handle, NULL,
3611 				    dvp->v_vfsp, t, cr, NULL, NULL);
3612 				vp->v_type = VLNK;
3613 				vp->v_rdev = 0;
3614 			}
3615 			dnlc_update(dvp, lnm, vp);
3616 			rp = VTOR(vp);
3617 			if (nfs3_do_symlink_cache &&
3618 			    rp->r_symlink.contents == NULL) {
3619 
3620 				contents = kmem_alloc(MAXPATHLEN,
3621 				    KM_NOSLEEP);
3622 
3623 				if (contents != NULL) {
3624 					mutex_enter(&rp->r_statelock);
3625 					if (rp->r_symlink.contents == NULL) {
3626 						rp->r_symlink.len = strlen(tnm);
3627 						bcopy(tnm, contents,
3628 						    rp->r_symlink.len);
3629 						rp->r_symlink.contents =
3630 						    contents;
3631 						rp->r_symlink.size = MAXPATHLEN;
3632 						mutex_exit(&rp->r_statelock);
3633 					} else {
3634 						mutex_exit(&rp->r_statelock);
3635 						kmem_free((void *)contents,
3636 						    MAXPATHLEN);
3637 					}
3638 				}
3639 			}
3640 			VN_RELE(vp);
3641 		}
3642 	} else {
3643 		nfs3_cache_wcc_data(dvp, &res.resfail.dir_wcc, t, cr);
3644 		PURGE_STALE_FH(error, dvp, cr);
3645 		if (error == EOPNOTSUPP) {
3646 			mutex_enter(&mi->mi_lock);
3647 			mi->mi_flags &= ~MI_SYMLINK;
3648 			mutex_exit(&mi->mi_lock);
3649 		}
3650 	}
3651 
3652 	nfs_rw_exit(&drp->r_rwlock);
3653 
3654 	return (error);
3655 }
3656 
3657 #ifdef DEBUG
3658 static int nfs3_readdir_cache_hits = 0;
3659 static int nfs3_readdir_cache_shorts = 0;
3660 static int nfs3_readdir_cache_waits = 0;
3661 static int nfs3_readdir_cache_misses = 0;
3662 static int nfs3_readdir_readahead = 0;
3663 #endif
3664 
3665 static int nfs3_shrinkreaddir = 0;
3666 
3667 /*
3668  * Read directory entries.
3669  * There are some weird things to look out for here.  The uio_loffset
3670  * field is either 0 or it is the offset returned from a previous
3671  * readdir.  It is an opaque value used by the server to find the
3672  * correct directory block to read. The count field is the number
3673  * of blocks to read on the server.  This is advisory only, the server
3674  * may return only one block's worth of entries.  Entries may be compressed
3675  * on the server.
3676  */
3677 /* ARGSUSED */
3678 static int
3679 nfs3_readdir(vnode_t *vp, struct uio *uiop, cred_t *cr, int *eofp,
3680 	caller_context_t *ct, int flags)
3681 {
3682 	int error;
3683 	size_t count;
3684 	rnode_t *rp;
3685 	rddir_cache *rdc;
3686 	rddir_cache *nrdc;
3687 	rddir_cache *rrdc;
3688 #ifdef DEBUG
3689 	int missed;
3690 #endif
3691 	int doreadahead;
3692 	rddir_cache srdc;
3693 	avl_index_t where;
3694 
3695 	if (nfs_zone() != VTOMI(vp)->mi_zone)
3696 		return (EIO);
3697 	rp = VTOR(vp);
3698 
3699 	ASSERT(nfs_rw_lock_held(&rp->r_rwlock, RW_READER));
3700 
3701 	/*
3702 	 * Make sure that the directory cache is valid.
3703 	 */
3704 	if (HAVE_RDDIR_CACHE(rp)) {
3705 		if (nfs_disable_rddir_cache) {
3706 			/*
3707 			 * Setting nfs_disable_rddir_cache in /etc/system
3708 			 * allows interoperability with servers that do not
3709 			 * properly update the attributes of directories.
3710 			 * Any cached information gets purged before an
3711 			 * access is made to it.
3712 			 */
3713 			nfs_purge_rddir_cache(vp);
3714 		} else {
3715 			error = nfs3_validate_caches(vp, cr);
3716 			if (error)
3717 				return (error);
3718 		}
3719 	}
3720 
3721 	/*
3722 	 * It is possible that some servers may not be able to correctly
3723 	 * handle a large READDIR or READDIRPLUS request due to bugs in
3724 	 * their implementation.  In order to continue to interoperate
3725 	 * with them, this workaround is provided to limit the maximum
3726 	 * size of a READDIRPLUS request to 1024.  In any case, the request
3727 	 * size is limited to MAXBSIZE.
3728 	 */
3729 	count = MIN(uiop->uio_iov->iov_len,
3730 	    nfs3_shrinkreaddir ? 1024 : MAXBSIZE);
3731 
3732 	nrdc = NULL;
3733 #ifdef DEBUG
3734 	missed = 0;
3735 #endif
3736 top:
3737 	/*
3738 	 * Short circuit last readdir which always returns 0 bytes.
3739 	 * This can be done after the directory has been read through
3740 	 * completely at least once.  This will set r_direof which
3741 	 * can be used to find the value of the last cookie.
3742 	 */
3743 	mutex_enter(&rp->r_statelock);
3744 	if (rp->r_direof != NULL &&
3745 	    uiop->uio_loffset == rp->r_direof->nfs3_ncookie) {
3746 		mutex_exit(&rp->r_statelock);
3747 #ifdef DEBUG
3748 		nfs3_readdir_cache_shorts++;
3749 #endif
3750 		if (eofp)
3751 			*eofp = 1;
3752 		if (nrdc != NULL)
3753 			rddir_cache_rele(nrdc);
3754 		return (0);
3755 	}
3756 	/*
3757 	 * Look for a cache entry.  Cache entries are identified
3758 	 * by the NFS cookie value and the byte count requested.
3759 	 */
3760 	srdc.nfs3_cookie = uiop->uio_loffset;
3761 	srdc.buflen = count;
3762 	rdc = avl_find(&rp->r_dir, &srdc, &where);
3763 	if (rdc != NULL) {
3764 		rddir_cache_hold(rdc);
3765 		/*
3766 		 * If the cache entry is in the process of being
3767 		 * filled in, wait until this completes.  The
3768 		 * RDDIRWAIT bit is set to indicate that someone
3769 		 * is waiting and then the thread currently
3770 		 * filling the entry is done, it should do a
3771 		 * cv_broadcast to wakeup all of the threads
3772 		 * waiting for it to finish.
3773 		 */
3774 		if (rdc->flags & RDDIR) {
3775 			nfs_rw_exit(&rp->r_rwlock);
3776 			rdc->flags |= RDDIRWAIT;
3777 #ifdef DEBUG
3778 			nfs3_readdir_cache_waits++;
3779 #endif
3780 			if (!cv_wait_sig(&rdc->cv, &rp->r_statelock)) {
3781 				/*
3782 				 * We got interrupted, probably
3783 				 * the user typed ^C or an alarm
3784 				 * fired.  We free the new entry
3785 				 * if we allocated one.
3786 				 */
3787 				mutex_exit(&rp->r_statelock);
3788 				(void) nfs_rw_enter_sig(&rp->r_rwlock,
3789 				    RW_READER, FALSE);
3790 				rddir_cache_rele(rdc);
3791 				if (nrdc != NULL)
3792 					rddir_cache_rele(nrdc);
3793 				return (EINTR);
3794 			}
3795 			mutex_exit(&rp->r_statelock);
3796 			(void) nfs_rw_enter_sig(&rp->r_rwlock,
3797 			    RW_READER, FALSE);
3798 			rddir_cache_rele(rdc);
3799 			goto top;
3800 		}
3801 		/*
3802 		 * Check to see if a readdir is required to
3803 		 * fill the entry.  If so, mark this entry
3804 		 * as being filled, remove our reference,
3805 		 * and branch to the code to fill the entry.
3806 		 */
3807 		if (rdc->flags & RDDIRREQ) {
3808 			rdc->flags &= ~RDDIRREQ;
3809 			rdc->flags |= RDDIR;
3810 			if (nrdc != NULL)
3811 				rddir_cache_rele(nrdc);
3812 			nrdc = rdc;
3813 			mutex_exit(&rp->r_statelock);
3814 			goto bottom;
3815 		}
3816 #ifdef DEBUG
3817 		if (!missed)
3818 			nfs3_readdir_cache_hits++;
3819 #endif
3820 		/*
3821 		 * If an error occurred while attempting
3822 		 * to fill the cache entry, just return it.
3823 		 */
3824 		if (rdc->error) {
3825 			error = rdc->error;
3826 			mutex_exit(&rp->r_statelock);
3827 			rddir_cache_rele(rdc);
3828 			if (nrdc != NULL)
3829 				rddir_cache_rele(nrdc);
3830 			return (error);
3831 		}
3832 
3833 		/*
3834 		 * The cache entry is complete and good,
3835 		 * copyout the dirent structs to the calling
3836 		 * thread.
3837 		 */
3838 		error = uiomove(rdc->entries, rdc->entlen, UIO_READ, uiop);
3839 
3840 		/*
3841 		 * If no error occurred during the copyout,
3842 		 * update the offset in the uio struct to
3843 		 * contain the value of the next cookie
3844 		 * and set the eof value appropriately.
3845 		 */
3846 		if (!error) {
3847 			uiop->uio_loffset = rdc->nfs3_ncookie;
3848 			if (eofp)
3849 				*eofp = rdc->eof;
3850 		}
3851 
3852 		/*
3853 		 * Decide whether to do readahead.
3854 		 *
3855 		 * Don't if have already read to the end of
3856 		 * directory.  There is nothing more to read.
3857 		 *
3858 		 * Don't if the application is not doing
3859 		 * lookups in the directory.  The readahead
3860 		 * is only effective if the application can
3861 		 * be doing work while an async thread is
3862 		 * handling the over the wire request.
3863 		 */
3864 		if (rdc->eof) {
3865 			rp->r_direof = rdc;
3866 			doreadahead = FALSE;
3867 		} else if (!(rp->r_flags & RLOOKUP))
3868 			doreadahead = FALSE;
3869 		else
3870 			doreadahead = TRUE;
3871 
3872 		if (!doreadahead) {
3873 			mutex_exit(&rp->r_statelock);
3874 			rddir_cache_rele(rdc);
3875 			if (nrdc != NULL)
3876 				rddir_cache_rele(nrdc);
3877 			return (error);
3878 		}
3879 
3880 		/*
3881 		 * Check to see whether we found an entry
3882 		 * for the readahead.  If so, we don't need
3883 		 * to do anything further, so free the new
3884 		 * entry if one was allocated.  Otherwise,
3885 		 * allocate a new entry, add it to the cache,
3886 		 * and then initiate an asynchronous readdir
3887 		 * operation to fill it.
3888 		 */
3889 		srdc.nfs3_cookie = rdc->nfs3_ncookie;
3890 		srdc.buflen = count;
3891 		rrdc = avl_find(&rp->r_dir, &srdc, &where);
3892 		if (rrdc != NULL) {
3893 			if (nrdc != NULL)
3894 				rddir_cache_rele(nrdc);
3895 		} else {
3896 			if (nrdc != NULL)
3897 				rrdc = nrdc;
3898 			else {
3899 				rrdc = rddir_cache_alloc(KM_NOSLEEP);
3900 			}
3901 			if (rrdc != NULL) {
3902 				rrdc->nfs3_cookie = rdc->nfs3_ncookie;
3903 				rrdc->buflen = count;
3904 				avl_insert(&rp->r_dir, rrdc, where);
3905 				rddir_cache_hold(rrdc);
3906 				mutex_exit(&rp->r_statelock);
3907 				rddir_cache_rele(rdc);
3908 #ifdef DEBUG
3909 				nfs3_readdir_readahead++;
3910 #endif
3911 				nfs_async_readdir(vp, rrdc, cr, do_nfs3readdir);
3912 				return (error);
3913 			}
3914 		}
3915 
3916 		mutex_exit(&rp->r_statelock);
3917 		rddir_cache_rele(rdc);
3918 		return (error);
3919 	}
3920 
3921 	/*
3922 	 * Didn't find an entry in the cache.  Construct a new empty
3923 	 * entry and link it into the cache.  Other processes attempting
3924 	 * to access this entry will need to wait until it is filled in.
3925 	 *
3926 	 * Since kmem_alloc may block, another pass through the cache
3927 	 * will need to be taken to make sure that another process
3928 	 * hasn't already added an entry to the cache for this request.
3929 	 */
3930 	if (nrdc == NULL) {
3931 		mutex_exit(&rp->r_statelock);
3932 		nrdc = rddir_cache_alloc(KM_SLEEP);
3933 		nrdc->nfs3_cookie = uiop->uio_loffset;
3934 		nrdc->buflen = count;
3935 		goto top;
3936 	}
3937 
3938 	/*
3939 	 * Add this entry to the cache.
3940 	 */
3941 	avl_insert(&rp->r_dir, nrdc, where);
3942 	rddir_cache_hold(nrdc);
3943 	mutex_exit(&rp->r_statelock);
3944 
3945 bottom:
3946 #ifdef DEBUG
3947 	missed = 1;
3948 	nfs3_readdir_cache_misses++;
3949 #endif
3950 	/*
3951 	 * Do the readdir.  This routine decides whether to use
3952 	 * READDIR or READDIRPLUS.
3953 	 */
3954 	error = do_nfs3readdir(vp, nrdc, cr);
3955 
3956 	/*
3957 	 * If this operation failed, just return the error which occurred.
3958 	 */
3959 	if (error != 0)
3960 		return (error);
3961 
3962 	/*
3963 	 * Since the RPC operation will have taken sometime and blocked
3964 	 * this process, another pass through the cache will need to be
3965 	 * taken to find the correct cache entry.  It is possible that
3966 	 * the correct cache entry will not be there (although one was
3967 	 * added) because the directory changed during the RPC operation
3968 	 * and the readdir cache was flushed.  In this case, just start
3969 	 * over.  It is hoped that this will not happen too often... :-)
3970 	 */
3971 	nrdc = NULL;
3972 	goto top;
3973 	/* NOTREACHED */
3974 }
3975 
3976 static int
3977 do_nfs3readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
3978 {
3979 	int error;
3980 	rnode_t *rp;
3981 	mntinfo_t *mi;
3982 
3983 	rp = VTOR(vp);
3984 	mi = VTOMI(vp);
3985 	ASSERT(nfs_zone() == mi->mi_zone);
3986 	/*
3987 	 * Issue the proper request.
3988 	 *
3989 	 * If the server does not support READDIRPLUS, then use READDIR.
3990 	 *
3991 	 * Otherwise --
3992 	 * Issue a READDIRPLUS if reading to fill an empty cache or if
3993 	 * an application has performed a lookup in the directory which
3994 	 * required an over the wire lookup.  The use of READDIRPLUS
3995 	 * will help to (re)populate the DNLC.
3996 	 */
3997 	if (!(mi->mi_flags & MI_READDIRONLY) &&
3998 	    (rp->r_flags & (RLOOKUP | RREADDIRPLUS))) {
3999 		if (rp->r_flags & RREADDIRPLUS) {
4000 			mutex_enter(&rp->r_statelock);
4001 			rp->r_flags &= ~RREADDIRPLUS;
4002 			mutex_exit(&rp->r_statelock);
4003 		}
4004 		nfs3readdirplus(vp, rdc, cr);
4005 		if (rdc->error == EOPNOTSUPP)
4006 			nfs3readdir(vp, rdc, cr);
4007 	} else
4008 		nfs3readdir(vp, rdc, cr);
4009 
4010 	mutex_enter(&rp->r_statelock);
4011 	rdc->flags &= ~RDDIR;
4012 	if (rdc->flags & RDDIRWAIT) {
4013 		rdc->flags &= ~RDDIRWAIT;
4014 		cv_broadcast(&rdc->cv);
4015 	}
4016 	error = rdc->error;
4017 	if (error)
4018 		rdc->flags |= RDDIRREQ;
4019 	mutex_exit(&rp->r_statelock);
4020 
4021 	rddir_cache_rele(rdc);
4022 
4023 	return (error);
4024 }
4025 
4026 static void
4027 nfs3readdir(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
4028 {
4029 	int error;
4030 	READDIR3args args;
4031 	READDIR3vres res;
4032 	vattr_t dva;
4033 	rnode_t *rp;
4034 	int douprintf;
4035 	failinfo_t fi, *fip = NULL;
4036 	mntinfo_t *mi;
4037 	hrtime_t t;
4038 
4039 	rp = VTOR(vp);
4040 	mi = VTOMI(vp);
4041 	ASSERT(nfs_zone() == mi->mi_zone);
4042 
4043 	args.dir = *RTOFH3(rp);
4044 	args.cookie = (cookie3)rdc->nfs3_cookie;
4045 	args.cookieverf = rp->r_cookieverf;
4046 	args.count = rdc->buflen;
4047 
4048 	/*
4049 	 * NFS client failover support
4050 	 * suppress failover unless we have a zero cookie
4051 	 */
4052 	if (args.cookie == (cookie3) 0) {
4053 		fi.vp = vp;
4054 		fi.fhp = (caddr_t)&args.dir;
4055 		fi.copyproc = nfs3copyfh;
4056 		fi.lookupproc = nfs3lookup;
4057 		fi.xattrdirproc = acl_getxattrdir3;
4058 		fip = &fi;
4059 	}
4060 
4061 #ifdef DEBUG
4062 	rdc->entries = rddir_cache_buf_alloc(rdc->buflen, KM_SLEEP);
4063 #else
4064 	rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP);
4065 #endif
4066 
4067 	res.entries = (dirent64_t *)rdc->entries;
4068 	res.entries_size = rdc->buflen;
4069 	res.dir_attributes.fres.vap = &dva;
4070 	res.dir_attributes.fres.vp = vp;
4071 	res.loff = rdc->nfs3_cookie;
4072 
4073 	douprintf = 1;
4074 
4075 	if (mi->mi_io_kstats) {
4076 		mutex_enter(&mi->mi_lock);
4077 		kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
4078 		mutex_exit(&mi->mi_lock);
4079 	}
4080 
4081 	t = gethrtime();
4082 
4083 	error = rfs3call(VTOMI(vp), NFSPROC3_READDIR,
4084 	    xdr_READDIR3args, (caddr_t)&args,
4085 	    xdr_READDIR3vres, (caddr_t)&res, cr,
4086 	    &douprintf, &res.status, 0, fip);
4087 
4088 	if (mi->mi_io_kstats) {
4089 		mutex_enter(&mi->mi_lock);
4090 		kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
4091 		mutex_exit(&mi->mi_lock);
4092 	}
4093 
4094 	if (error)
4095 		goto err;
4096 
4097 	nfs3_cache_post_op_vattr(vp, &res.dir_attributes, t, cr);
4098 
4099 	error = geterrno3(res.status);
4100 	if (error) {
4101 		PURGE_STALE_FH(error, vp, cr);
4102 		goto err;
4103 	}
4104 
4105 	if (mi->mi_io_kstats) {
4106 		mutex_enter(&mi->mi_lock);
4107 		KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
4108 		KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.size;
4109 		mutex_exit(&mi->mi_lock);
4110 	}
4111 
4112 	rdc->nfs3_ncookie = res.loff;
4113 	rp->r_cookieverf = res.cookieverf;
4114 	rdc->eof = res.eof ? 1 : 0;
4115 	rdc->entlen = res.size;
4116 	ASSERT(rdc->entlen <= rdc->buflen);
4117 	rdc->error = 0;
4118 	return;
4119 
4120 err:
4121 	kmem_free(rdc->entries, rdc->buflen);
4122 	rdc->entries = NULL;
4123 	rdc->error = error;
4124 }
4125 
4126 /*
4127  * Read directory entries.
4128  * There are some weird things to look out for here.  The uio_loffset
4129  * field is either 0 or it is the offset returned from a previous
4130  * readdir.  It is an opaque value used by the server to find the
4131  * correct directory block to read. The count field is the number
4132  * of blocks to read on the server.  This is advisory only, the server
4133  * may return only one block's worth of entries.  Entries may be compressed
4134  * on the server.
4135  */
4136 static void
4137 nfs3readdirplus(vnode_t *vp, rddir_cache *rdc, cred_t *cr)
4138 {
4139 	int error;
4140 	READDIRPLUS3args args;
4141 	READDIRPLUS3vres res;
4142 	vattr_t dva;
4143 	rnode_t *rp;
4144 	mntinfo_t *mi;
4145 	int douprintf;
4146 	failinfo_t fi, *fip = NULL;
4147 
4148 	rp = VTOR(vp);
4149 	mi = VTOMI(vp);
4150 	ASSERT(nfs_zone() == mi->mi_zone);
4151 
4152 	args.dir = *RTOFH3(rp);
4153 	args.cookie = (cookie3)rdc->nfs3_cookie;
4154 	args.cookieverf = rp->r_cookieverf;
4155 	args.dircount = rdc->buflen;
4156 	args.maxcount = mi->mi_tsize;
4157 
4158 	/*
4159 	 * NFS client failover support
4160 	 * suppress failover unless we have a zero cookie
4161 	 */
4162 	if (args.cookie == (cookie3)0) {
4163 		fi.vp = vp;
4164 		fi.fhp = (caddr_t)&args.dir;
4165 		fi.copyproc = nfs3copyfh;
4166 		fi.lookupproc = nfs3lookup;
4167 		fi.xattrdirproc = acl_getxattrdir3;
4168 		fip = &fi;
4169 	}
4170 
4171 #ifdef DEBUG
4172 	rdc->entries = rddir_cache_buf_alloc(rdc->buflen, KM_SLEEP);
4173 #else
4174 	rdc->entries = kmem_alloc(rdc->buflen, KM_SLEEP);
4175 #endif
4176 
4177 	res.entries = (dirent64_t *)rdc->entries;
4178 	res.entries_size = rdc->buflen;
4179 	res.dir_attributes.fres.vap = &dva;
4180 	res.dir_attributes.fres.vp = vp;
4181 	res.loff = rdc->nfs3_cookie;
4182 	res.credentials = cr;
4183 
4184 	douprintf = 1;
4185 
4186 	if (mi->mi_io_kstats) {
4187 		mutex_enter(&mi->mi_lock);
4188 		kstat_runq_enter(KSTAT_IO_PTR(mi->mi_io_kstats));
4189 		mutex_exit(&mi->mi_lock);
4190 	}
4191 
4192 	res.time = gethrtime();
4193 
4194 	error = rfs3call(mi, NFSPROC3_READDIRPLUS,
4195 	    xdr_READDIRPLUS3args, (caddr_t)&args,
4196 	    xdr_READDIRPLUS3vres, (caddr_t)&res, cr,
4197 	    &douprintf, &res.status, 0, fip);
4198 
4199 	if (mi->mi_io_kstats) {
4200 		mutex_enter(&mi->mi_lock);
4201 		kstat_runq_exit(KSTAT_IO_PTR(mi->mi_io_kstats));
4202 		mutex_exit(&mi->mi_lock);
4203 	}
4204 
4205 	if (error) {
4206 		goto err;
4207 	}
4208 
4209 	nfs3_cache_post_op_vattr(vp, &res.dir_attributes, res.time, cr);
4210 
4211 	error = geterrno3(res.status);
4212 	if (error) {
4213 		PURGE_STALE_FH(error, vp, cr);
4214 		if (error == EOPNOTSUPP) {
4215 			mutex_enter(&mi->mi_lock);
4216 			mi->mi_flags |= MI_READDIRONLY;
4217 			mutex_exit(&mi->mi_lock);
4218 		}
4219 		goto err;
4220 	}
4221 
4222 	if (mi->mi_io_kstats) {
4223 		mutex_enter(&mi->mi_lock);
4224 		KSTAT_IO_PTR(mi->mi_io_kstats)->reads++;
4225 		KSTAT_IO_PTR(mi->mi_io_kstats)->nread += res.size;
4226 		mutex_exit(&mi->mi_lock);
4227 	}
4228 
4229 	rdc->nfs3_ncookie = res.loff;
4230 	rp->r_cookieverf = res.cookieverf;
4231 	rdc->eof = res.eof ? 1 : 0;
4232 	rdc->entlen = res.size;
4233 	ASSERT(rdc->entlen <= rdc->buflen);
4234 	rdc->error = 0;
4235 
4236 	return;
4237 
4238 err:
4239 	kmem_free(rdc->entries, rdc->buflen);
4240 	rdc->entries = NULL;
4241 	rdc->error = error;
4242 }
4243 
4244 #ifdef DEBUG
4245 static int nfs3_bio_do_stop = 0;
4246 #endif
4247 
4248 static int
4249 nfs3_bio(struct buf *bp, stable_how *stab_comm, cred_t *cr)
4250 {
4251 	rnode_t *rp = VTOR(bp->b_vp);
4252 	int count;
4253 	int error;
4254 	cred_t *cred;
4255 	offset_t offset;
4256 
4257 	ASSERT(nfs_zone() == VTOMI(bp->b_vp)->mi_zone);
4258 	offset = ldbtob(bp->b_lblkno);
4259 
4260 	DTRACE_IO1(start, struct buf *, bp);
4261 
4262 	if (bp->b_flags & B_READ) {
4263 		mutex_enter(&rp->r_statelock);
4264 		if (rp->r_cred != NULL) {
4265 			cred = rp->r_cred;
4266 			crhold(cred);
4267 		} else {
4268 			rp->r_cred = cr;
4269 			crhold(cr);
4270 			cred = cr;
4271 			crhold(cred);
4272 		}
4273 		mutex_exit(&rp->r_statelock);
4274 	read_again:
4275 		error = bp->b_error = nfs3read(bp->b_vp, bp->b_un.b_addr,
4276 		    offset, bp->b_bcount, &bp->b_resid, cred);
4277 		crfree(cred);
4278 		if (!error) {
4279 			if (bp->b_resid) {
4280 				/*
4281 				 * Didn't get it all because we hit EOF,
4282 				 * zero all the memory beyond the EOF.
4283 				 */
4284 				/* bzero(rdaddr + */
4285 				bzero(bp->b_un.b_addr +
4286 				    bp->b_bcount - bp->b_resid, bp->b_resid);
4287 			}
4288 			mutex_enter(&rp->r_statelock);
4289 			if (bp->b_resid == bp->b_bcount &&
4290 			    offset >= rp->r_size) {
4291 				/*
4292 				 * We didn't read anything at all as we are
4293 				 * past EOF.  Return an error indicator back
4294 				 * but don't destroy the pages (yet).
4295 				 */
4296 				error = NFS_EOF;
4297 			}
4298 			mutex_exit(&rp->r_statelock);
4299 		} else if (error == EACCES) {
4300 			mutex_enter(&rp->r_statelock);
4301 			if (cred != cr) {
4302 				if (rp->r_cred != NULL)
4303 					crfree(rp->r_cred);
4304 				rp->r_cred = cr;
4305 				crhold(cr);
4306 				cred = cr;
4307 				crhold(cred);
4308 				mutex_exit(&rp->r_statelock);
4309 				goto read_again;
4310 			}
4311 			mutex_exit(&rp->r_statelock);
4312 		}
4313 	} else {
4314 		if (!(rp->r_flags & RSTALE)) {
4315 			mutex_enter(&rp->r_statelock);
4316 			if (rp->r_cred != NULL) {
4317 				cred = rp->r_cred;
4318 				crhold(cred);
4319 			} else {
4320 				rp->r_cred = cr;
4321 				crhold(cr);
4322 				cred = cr;
4323 				crhold(cred);
4324 			}
4325 			mutex_exit(&rp->r_statelock);
4326 		write_again:
4327 			mutex_enter(&rp->r_statelock);
4328 			count = MIN(bp->b_bcount, rp->r_size - offset);
4329 			mutex_exit(&rp->r_statelock);
4330 			if (count < 0)
4331 				cmn_err(CE_PANIC, "nfs3_bio: write count < 0");
4332 #ifdef DEBUG
4333 			if (count == 0) {
4334 				zcmn_err(getzoneid(), CE_WARN,
4335 				    "nfs3_bio: zero length write at %lld",
4336 				    offset);
4337 				nfs_printfhandle(&rp->r_fh);
4338 				if (nfs3_bio_do_stop)
4339 					debug_enter("nfs3_bio");
4340 			}
4341 #endif
4342 			error = nfs3write(bp->b_vp, bp->b_un.b_addr, offset,
4343 			    count, cred, stab_comm);
4344 			if (error == EACCES) {
4345 				mutex_enter(&rp->r_statelock);
4346 				if (cred != cr) {
4347 					if (rp->r_cred != NULL)
4348 						crfree(rp->r_cred);
4349 					rp->r_cred = cr;
4350 					crhold(cr);
4351 					crfree(cred);
4352 					cred = cr;
4353 					crhold(cred);
4354 					mutex_exit(&rp->r_statelock);
4355 					goto write_again;
4356 				}
4357 				mutex_exit(&rp->r_statelock);
4358 			}
4359 			bp->b_error = error;
4360 			if (error && error != EINTR) {
4361 				/*
4362 				 * Don't print EDQUOT errors on the console.
4363 				 * Don't print asynchronous EACCES errors.
4364 				 * Don't print EFBIG errors.
4365 				 * Print all other write errors.
4366 				 */
4367 				if (error != EDQUOT && error != EFBIG &&
4368 				    (error != EACCES ||
4369 				    !(bp->b_flags & B_ASYNC)))
4370 					nfs_write_error(bp->b_vp, error, cred);
4371 				/*
4372 				 * Update r_error and r_flags as appropriate.
4373 				 * If the error was ESTALE, then mark the
4374 				 * rnode as not being writeable and save
4375 				 * the error status.  Otherwise, save any
4376 				 * errors which occur from asynchronous
4377 				 * page invalidations.  Any errors occurring
4378 				 * from other operations should be saved
4379 				 * by the caller.
4380 				 */
4381 				mutex_enter(&rp->r_statelock);
4382 				if (error == ESTALE) {
4383 					rp->r_flags |= RSTALE;
4384 					if (!rp->r_error)
4385 						rp->r_error = error;
4386 				} else if (!rp->r_error &&
4387 				    (bp->b_flags &
4388 				    (B_INVAL|B_FORCE|B_ASYNC)) ==
4389 				    (B_INVAL|B_FORCE|B_ASYNC)) {
4390 					rp->r_error = error;
4391 				}
4392 				mutex_exit(&rp->r_statelock);
4393 			}
4394 			crfree(cred);
4395 		} else
4396 			error = rp->r_error;
4397 	}
4398 
4399 	if (error != 0 && error != NFS_EOF)
4400 		bp->b_flags |= B_ERROR;
4401 
4402 	DTRACE_IO1(done, struct buf *, bp);
4403 
4404 	return (error);
4405 }
4406 
4407 /* ARGSUSED */
4408 static int
4409 nfs3_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4410 {
4411 	rnode_t *rp;
4412 
4413 	if (nfs_zone() != VTOMI(vp)->mi_zone)
4414 		return (EIO);
4415 	rp = VTOR(vp);
4416 
4417 	if (fidp->fid_len < (ushort_t)rp->r_fh.fh_len) {
4418 		fidp->fid_len = rp->r_fh.fh_len;
4419 		return (ENOSPC);
4420 	}
4421 	fidp->fid_len = rp->r_fh.fh_len;
4422 	bcopy(rp->r_fh.fh_buf, fidp->fid_data, fidp->fid_len);
4423 	return (0);
4424 }
4425 
4426 /* ARGSUSED2 */
4427 static int
4428 nfs3_rwlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
4429 {
4430 	rnode_t *rp = VTOR(vp);
4431 
4432 	if (!write_lock) {
4433 		(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
4434 		return (V_WRITELOCK_FALSE);
4435 	}
4436 
4437 	if ((rp->r_flags & RDIRECTIO) || (VTOMI(vp)->mi_flags & MI_DIRECTIO)) {
4438 		(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_READER, FALSE);
4439 		if (rp->r_mapcnt == 0 && !vn_has_cached_data(vp))
4440 			return (V_WRITELOCK_FALSE);
4441 		nfs_rw_exit(&rp->r_rwlock);
4442 	}
4443 
4444 	(void) nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, FALSE);
4445 	return (V_WRITELOCK_TRUE);
4446 }
4447 
4448 /* ARGSUSED */
4449 static void
4450 nfs3_rwunlock(vnode_t *vp, int write_lock, caller_context_t *ctp)
4451 {
4452 	rnode_t *rp = VTOR(vp);
4453 
4454 	nfs_rw_exit(&rp->r_rwlock);
4455 }
4456 
4457 /* ARGSUSED */
4458 static int
4459 nfs3_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, caller_context_t *ct)
4460 {
4461 
4462 	/*
4463 	 * Because we stuff the readdir cookie into the offset field
4464 	 * someone may attempt to do an lseek with the cookie which
4465 	 * we want to succeed.
4466 	 */
4467 	if (vp->v_type == VDIR)
4468 		return (0);
4469 	if (*noffp < 0)
4470 		return (EINVAL);
4471 	return (0);
4472 }
4473 
4474 /*
4475  * number of nfs3_bsize blocks to read ahead.
4476  */
4477 static int nfs3_nra = 4;
4478 
4479 #ifdef DEBUG
4480 static int nfs3_lostpage = 0;	/* number of times we lost original page */
4481 #endif
4482 
4483 /*
4484  * Return all the pages from [off..off+len) in file
4485  */
4486 /* ARGSUSED */
4487 static int
4488 nfs3_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4489 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4490 	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4491 {
4492 	rnode_t *rp;
4493 	int error;
4494 	mntinfo_t *mi;
4495 
4496 	if (vp->v_flag & VNOMAP)
4497 		return (ENOSYS);
4498 
4499 	if (nfs_zone() != VTOMI(vp)->mi_zone)
4500 		return (EIO);
4501 	if (protp != NULL)
4502 		*protp = PROT_ALL;
4503 
4504 	/*
4505 	 * Now valididate that the caches are up to date.
4506 	 */
4507 	error = nfs3_validate_caches(vp, cr);
4508 	if (error)
4509 		return (error);
4510 
4511 	rp = VTOR(vp);
4512 	mi = VTOMI(vp);
4513 retry:
4514 	mutex_enter(&rp->r_statelock);
4515 
4516 	/*
4517 	 * Don't create dirty pages faster than they
4518 	 * can be cleaned so that the system doesn't
4519 	 * get imbalanced.  If the async queue is
4520 	 * maxed out, then wait for it to drain before
4521 	 * creating more dirty pages.  Also, wait for
4522 	 * any threads doing pagewalks in the vop_getattr
4523 	 * entry points so that they don't block for
4524 	 * long periods.
4525 	 */
4526 	if (rw == S_CREATE) {
4527 		while ((mi->mi_max_threads != 0 &&
4528 		    rp->r_awcount > 2 * mi->mi_max_threads) ||
4529 		    rp->r_gcount > 0)
4530 			cv_wait(&rp->r_cv, &rp->r_statelock);
4531 	}
4532 
4533 	/*
4534 	 * If we are getting called as a side effect of an nfs_write()
4535 	 * operation the local file size might not be extended yet.
4536 	 * In this case we want to be able to return pages of zeroes.
4537 	 */
4538 	if (off + len > rp->r_size + PAGEOFFSET && seg != segkmap) {
4539 		mutex_exit(&rp->r_statelock);
4540 		return (EFAULT);		/* beyond EOF */
4541 	}
4542 
4543 	mutex_exit(&rp->r_statelock);
4544 
4545 	if (len <= PAGESIZE) {
4546 		error = nfs3_getapage(vp, off, len, protp, pl, plsz,
4547 		    seg, addr, rw, cr);
4548 	} else {
4549 		error = pvn_getpages(nfs3_getapage, vp, off, len, protp,
4550 		    pl, plsz, seg, addr, rw, cr);
4551 	}
4552 
4553 	switch (error) {
4554 	case NFS_EOF:
4555 		nfs_purge_caches(vp, NFS_NOPURGE_DNLC, cr);
4556 		goto retry;
4557 	case ESTALE:
4558 		PURGE_STALE_FH(error, vp, cr);
4559 	}
4560 
4561 	return (error);
4562 }
4563 
4564 /*
4565  * Called from pvn_getpages or nfs3_getpage to get a particular page.
4566  */
4567 /* ARGSUSED */
4568 static int
4569 nfs3_getapage(vnode_t *vp, u_offset_t off, size_t len, uint_t *protp,
4570 	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4571 	enum seg_rw rw, cred_t *cr)
4572 {
4573 	rnode_t *rp;
4574 	uint_t bsize;
4575 	struct buf *bp;
4576 	page_t *pp;
4577 	u_offset_t lbn;
4578 	u_offset_t io_off;
4579 	u_offset_t blkoff;
4580 	u_offset_t rablkoff;
4581 	size_t io_len;
4582 	uint_t blksize;
4583 	int error;
4584 	int readahead;
4585 	int readahead_issued = 0;
4586 	int ra_window; /* readahead window */
4587 	page_t *pagefound;
4588 	page_t *savepp;
4589 
4590 	if (nfs_zone() != VTOMI(vp)->mi_zone)
4591 		return (EIO);
4592 	rp = VTOR(vp);
4593 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
4594 
4595 reread:
4596 	bp = NULL;
4597 	pp = NULL;
4598 	pagefound = NULL;
4599 
4600 	if (pl != NULL)
4601 		pl[0] = NULL;
4602 
4603 	error = 0;
4604 	lbn = off / bsize;
4605 	blkoff = lbn * bsize;
4606 
4607 	/*
4608 	 * Queueing up the readahead before doing the synchronous read
4609 	 * results in a significant increase in read throughput because
4610 	 * of the increased parallelism between the async threads and
4611 	 * the process context.
4612 	 */
4613 	if ((off & ((vp->v_vfsp->vfs_bsize) - 1)) == 0 &&
4614 	    rw != S_CREATE &&
4615 	    !(vp->v_flag & VNOCACHE)) {
4616 		mutex_enter(&rp->r_statelock);
4617 
4618 		/*
4619 		 * Calculate the number of readaheads to do.
4620 		 * a) No readaheads at offset = 0.
4621 		 * b) Do maximum(nfs3_nra) readaheads when the readahead
4622 		 *    window is closed.
4623 		 * c) Do readaheads between 1 to (nfs3_nra - 1) depending
4624 		 *    upon how far the readahead window is open or close.
4625 		 * d) No readaheads if rp->r_nextr is not within the scope
4626 		 *    of the readahead window (random i/o).
4627 		 */
4628 
4629 		if (off == 0)
4630 			readahead = 0;
4631 		else if (blkoff == rp->r_nextr)
4632 			readahead = nfs3_nra;
4633 		else if (rp->r_nextr > blkoff &&
4634 		    ((ra_window = (rp->r_nextr - blkoff) / bsize)
4635 		    <= (nfs3_nra - 1)))
4636 			readahead = nfs3_nra - ra_window;
4637 		else
4638 			readahead = 0;
4639 
4640 		rablkoff = rp->r_nextr;
4641 		while (readahead > 0 && rablkoff + bsize < rp->r_size) {
4642 			mutex_exit(&rp->r_statelock);
4643 			if (nfs_async_readahead(vp, rablkoff + bsize,
4644 			    addr + (rablkoff + bsize - off), seg, cr,
4645 			    nfs3_readahead) < 0) {
4646 				mutex_enter(&rp->r_statelock);
4647 				break;
4648 			}
4649 			readahead--;
4650 			rablkoff += bsize;
4651 			/*
4652 			 * Indicate that we did a readahead so
4653 			 * readahead offset is not updated
4654 			 * by the synchronous read below.
4655 			 */
4656 			readahead_issued = 1;
4657 			mutex_enter(&rp->r_statelock);
4658 			/*
4659 			 * set readahead offset to
4660 			 * offset of last async readahead
4661 			 * request.
4662 			 */
4663 			rp->r_nextr = rablkoff;
4664 		}
4665 		mutex_exit(&rp->r_statelock);
4666 	}
4667 
4668 again:
4669 	if ((pagefound = page_exists(vp, off)) == NULL) {
4670 		if (pl == NULL) {
4671 			(void) nfs_async_readahead(vp, blkoff, addr, seg, cr,
4672 			    nfs3_readahead);
4673 		} else if (rw == S_CREATE) {
4674 			/*
4675 			 * Block for this page is not allocated, or the offset
4676 			 * is beyond the current allocation size, or we're
4677 			 * allocating a swap slot and the page was not found,
4678 			 * so allocate it and return a zero page.
4679 			 */
4680 			if ((pp = page_create_va(vp, off,
4681 			    PAGESIZE, PG_WAIT, seg, addr)) == NULL)
4682 				cmn_err(CE_PANIC, "nfs3_getapage: page_create");
4683 			io_len = PAGESIZE;
4684 			mutex_enter(&rp->r_statelock);
4685 			rp->r_nextr = off + PAGESIZE;
4686 			mutex_exit(&rp->r_statelock);
4687 		} else {
4688 			/*
4689 			 * Need to go to server to get a BLOCK, exception to
4690 			 * that being while reading at offset = 0 or doing
4691 			 * random i/o, in that case read only a PAGE.
4692 			 */
4693 			mutex_enter(&rp->r_statelock);
4694 			if (blkoff < rp->r_size &&
4695 			    blkoff + bsize >= rp->r_size) {
4696 				/*
4697 				 * If only a block or less is left in
4698 				 * the file, read all that is remaining.
4699 				 */
4700 				if (rp->r_size <= off) {
4701 					/*
4702 					 * Trying to access beyond EOF,
4703 					 * set up to get at least one page.
4704 					 */
4705 					blksize = off + PAGESIZE - blkoff;
4706 				} else
4707 					blksize = rp->r_size - blkoff;
4708 			} else if ((off == 0) ||
4709 			    (off != rp->r_nextr && !readahead_issued)) {
4710 				blksize = PAGESIZE;
4711 				blkoff = off; /* block = page here */
4712 			} else
4713 				blksize = bsize;
4714 			mutex_exit(&rp->r_statelock);
4715 
4716 			pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4717 			    &io_len, blkoff, blksize, 0);
4718 
4719 			/*
4720 			 * Some other thread has entered the page,
4721 			 * so just use it.
4722 			 */
4723 			if (pp == NULL)
4724 				goto again;
4725 
4726 			/*
4727 			 * Now round the request size up to page boundaries.
4728 			 * This ensures that the entire page will be
4729 			 * initialized to zeroes if EOF is encountered.
4730 			 */
4731 			io_len = ptob(btopr(io_len));
4732 
4733 			bp = pageio_setup(pp, io_len, vp, B_READ);
4734 			ASSERT(bp != NULL);
4735 
4736 			/*
4737 			 * pageio_setup should have set b_addr to 0.  This
4738 			 * is correct since we want to do I/O on a page
4739 			 * boundary.  bp_mapin will use this addr to calculate
4740 			 * an offset, and then set b_addr to the kernel virtual
4741 			 * address it allocated for us.
4742 			 */
4743 			ASSERT(bp->b_un.b_addr == 0);
4744 
4745 			bp->b_edev = 0;
4746 			bp->b_dev = 0;
4747 			bp->b_lblkno = lbtodb(io_off);
4748 			bp->b_file = vp;
4749 			bp->b_offset = (offset_t)off;
4750 			bp_mapin(bp);
4751 
4752 			/*
4753 			 * If doing a write beyond what we believe is EOF,
4754 			 * don't bother trying to read the pages from the
4755 			 * server, we'll just zero the pages here.  We
4756 			 * don't check that the rw flag is S_WRITE here
4757 			 * because some implementations may attempt a
4758 			 * read access to the buffer before copying data.
4759 			 */
4760 			mutex_enter(&rp->r_statelock);
4761 			if (io_off >= rp->r_size && seg == segkmap) {
4762 				mutex_exit(&rp->r_statelock);
4763 				bzero(bp->b_un.b_addr, io_len);
4764 			} else {
4765 				mutex_exit(&rp->r_statelock);
4766 				error = nfs3_bio(bp, NULL, cr);
4767 			}
4768 
4769 			/*
4770 			 * Unmap the buffer before freeing it.
4771 			 */
4772 			bp_mapout(bp);
4773 			pageio_done(bp);
4774 
4775 			savepp = pp;
4776 			do {
4777 				pp->p_fsdata = C_NOCOMMIT;
4778 			} while ((pp = pp->p_next) != savepp);
4779 
4780 			if (error == NFS_EOF) {
4781 				/*
4782 				 * If doing a write system call just return
4783 				 * zeroed pages, else user tried to get pages
4784 				 * beyond EOF, return error.  We don't check
4785 				 * that the rw flag is S_WRITE here because
4786 				 * some implementations may attempt a read
4787 				 * access to the buffer before copying data.
4788 				 */
4789 				if (seg == segkmap)
4790 					error = 0;
4791 				else
4792 					error = EFAULT;
4793 			}
4794 
4795 			if (!readahead_issued && !error) {
4796 				mutex_enter(&rp->r_statelock);
4797 				rp->r_nextr = io_off + io_len;
4798 				mutex_exit(&rp->r_statelock);
4799 			}
4800 		}
4801 	}
4802 
4803 out:
4804 	if (pl == NULL)
4805 		return (error);
4806 
4807 	if (error) {
4808 		if (pp != NULL)
4809 			pvn_read_done(pp, B_ERROR);
4810 		return (error);
4811 	}
4812 
4813 	if (pagefound) {
4814 		se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
4815 
4816 		/*
4817 		 * Page exists in the cache, acquire the appropriate lock.
4818 		 * If this fails, start all over again.
4819 		 */
4820 		if ((pp = page_lookup(vp, off, se)) == NULL) {
4821 #ifdef DEBUG
4822 			nfs3_lostpage++;
4823 #endif
4824 			goto reread;
4825 		}
4826 		pl[0] = pp;
4827 		pl[1] = NULL;
4828 		return (0);
4829 	}
4830 
4831 	if (pp != NULL)
4832 		pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4833 
4834 	return (error);
4835 }
4836 
4837 static void
4838 nfs3_readahead(vnode_t *vp, u_offset_t blkoff, caddr_t addr, struct seg *seg,
4839 	cred_t *cr)
4840 {
4841 	int error;
4842 	page_t *pp;
4843 	u_offset_t io_off;
4844 	size_t io_len;
4845 	struct buf *bp;
4846 	uint_t bsize, blksize;
4847 	rnode_t *rp = VTOR(vp);
4848 	page_t *savepp;
4849 
4850 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
4851 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
4852 
4853 	mutex_enter(&rp->r_statelock);
4854 	if (blkoff < rp->r_size && blkoff + bsize > rp->r_size) {
4855 		/*
4856 		 * If less than a block left in file read less
4857 		 * than a block.
4858 		 */
4859 		blksize = rp->r_size - blkoff;
4860 	} else
4861 		blksize = bsize;
4862 	mutex_exit(&rp->r_statelock);
4863 
4864 	pp = pvn_read_kluster(vp, blkoff, segkmap, addr,
4865 	    &io_off, &io_len, blkoff, blksize, 1);
4866 	/*
4867 	 * The isra flag passed to the kluster function is 1, we may have
4868 	 * gotten a return value of NULL for a variety of reasons (# of free
4869 	 * pages < minfree, someone entered the page on the vnode etc). In all
4870 	 * cases, we want to punt on the readahead.
4871 	 */
4872 	if (pp == NULL)
4873 		return;
4874 
4875 	/*
4876 	 * Now round the request size up to page boundaries.
4877 	 * This ensures that the entire page will be
4878 	 * initialized to zeroes if EOF is encountered.
4879 	 */
4880 	io_len = ptob(btopr(io_len));
4881 
4882 	bp = pageio_setup(pp, io_len, vp, B_READ);
4883 	ASSERT(bp != NULL);
4884 
4885 	/*
4886 	 * pageio_setup should have set b_addr to 0.  This is correct since
4887 	 * we want to do I/O on a page boundary. bp_mapin() will use this addr
4888 	 * to calculate an offset, and then set b_addr to the kernel virtual
4889 	 * address it allocated for us.
4890 	 */
4891 	ASSERT(bp->b_un.b_addr == 0);
4892 
4893 	bp->b_edev = 0;
4894 	bp->b_dev = 0;
4895 	bp->b_lblkno = lbtodb(io_off);
4896 	bp->b_file = vp;
4897 	bp->b_offset = (offset_t)blkoff;
4898 	bp_mapin(bp);
4899 
4900 	/*
4901 	 * If doing a write beyond what we believe is EOF, don't bother trying
4902 	 * to read the pages from the server, we'll just zero the pages here.
4903 	 * We don't check that the rw flag is S_WRITE here because some
4904 	 * implementations may attempt a read access to the buffer before
4905 	 * copying data.
4906 	 */
4907 	mutex_enter(&rp->r_statelock);
4908 	if (io_off >= rp->r_size && seg == segkmap) {
4909 		mutex_exit(&rp->r_statelock);
4910 		bzero(bp->b_un.b_addr, io_len);
4911 		error = 0;
4912 	} else {
4913 		mutex_exit(&rp->r_statelock);
4914 		error = nfs3_bio(bp, NULL, cr);
4915 		if (error == NFS_EOF)
4916 			error = 0;
4917 	}
4918 
4919 	/*
4920 	 * Unmap the buffer before freeing it.
4921 	 */
4922 	bp_mapout(bp);
4923 	pageio_done(bp);
4924 
4925 	savepp = pp;
4926 	do {
4927 		pp->p_fsdata = C_NOCOMMIT;
4928 	} while ((pp = pp->p_next) != savepp);
4929 
4930 	pvn_read_done(pp, error ? B_READ | B_ERROR : B_READ);
4931 
4932 	/*
4933 	 * In case of error set readahead offset
4934 	 * to the lowest offset.
4935 	 * pvn_read_done() calls VN_DISPOSE to destroy the pages
4936 	 */
4937 	if (error && rp->r_nextr > io_off) {
4938 		mutex_enter(&rp->r_statelock);
4939 		if (rp->r_nextr > io_off)
4940 			rp->r_nextr = io_off;
4941 		mutex_exit(&rp->r_statelock);
4942 	}
4943 }
4944 
4945 /*
4946  * Flags are composed of {B_INVAL, B_FREE, B_DONTNEED, B_FORCE}
4947  * If len == 0, do from off to EOF.
4948  *
4949  * The normal cases should be len == 0 && off == 0 (entire vp list),
4950  * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
4951  * (from pageout).
4952  */
4953 /* ARGSUSED */
4954 static int
4955 nfs3_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4956 	caller_context_t *ct)
4957 {
4958 	int error;
4959 	rnode_t *rp;
4960 
4961 	ASSERT(cr != NULL);
4962 
4963 	/*
4964 	 * XXX - Why should this check be made here?
4965 	 */
4966 	if (vp->v_flag & VNOMAP)
4967 		return (ENOSYS);
4968 	if (len == 0 && !(flags & B_INVAL) && vn_is_readonly(vp))
4969 		return (0);
4970 	if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
4971 		return (EIO);
4972 
4973 	rp = VTOR(vp);
4974 	mutex_enter(&rp->r_statelock);
4975 	rp->r_count++;
4976 	mutex_exit(&rp->r_statelock);
4977 	error = nfs_putpages(vp, off, len, flags, cr);
4978 	mutex_enter(&rp->r_statelock);
4979 	rp->r_count--;
4980 	cv_broadcast(&rp->r_cv);
4981 	mutex_exit(&rp->r_statelock);
4982 
4983 	return (error);
4984 }
4985 
4986 /*
4987  * Write out a single page, possibly klustering adjacent dirty pages.
4988  */
4989 int
4990 nfs3_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, size_t *lenp,
4991 	int flags, cred_t *cr)
4992 {
4993 	u_offset_t io_off;
4994 	u_offset_t lbn_off;
4995 	u_offset_t lbn;
4996 	size_t io_len;
4997 	uint_t bsize;
4998 	int error;
4999 	rnode_t *rp;
5000 
5001 	ASSERT(!vn_is_readonly(vp));
5002 	ASSERT(pp != NULL);
5003 	ASSERT(cr != NULL);
5004 	ASSERT((flags & B_ASYNC) || nfs_zone() == VTOMI(vp)->mi_zone);
5005 
5006 	rp = VTOR(vp);
5007 	ASSERT(rp->r_count > 0);
5008 
5009 	bsize = MAX(vp->v_vfsp->vfs_bsize, PAGESIZE);
5010 	lbn = pp->p_offset / bsize;
5011 	lbn_off = lbn * bsize;
5012 
5013 	/*
5014 	 * Find a kluster that fits in one block, or in
5015 	 * one page if pages are bigger than blocks.  If
5016 	 * there is less file space allocated than a whole
5017 	 * page, we'll shorten the i/o request below.
5018 	 */
5019 	pp = pvn_write_kluster(vp, pp, &io_off, &io_len, lbn_off,
5020 	    roundup(bsize, PAGESIZE), flags);
5021 
5022 	/*
5023 	 * pvn_write_kluster shouldn't have returned a page with offset
5024 	 * behind the original page we were given.  Verify that.
5025 	 */
5026 	ASSERT((pp->p_offset / bsize) >= lbn);
5027 
5028 	/*
5029 	 * Now pp will have the list of kept dirty pages marked for
5030 	 * write back.  It will also handle invalidation and freeing
5031 	 * of pages that are not dirty.  Check for page length rounding
5032 	 * problems.
5033 	 */
5034 	if (io_off + io_len > lbn_off + bsize) {
5035 		ASSERT((io_off + io_len) - (lbn_off + bsize) < PAGESIZE);
5036 		io_len = lbn_off + bsize - io_off;
5037 	}
5038 	/*
5039 	 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
5040 	 * consistent value of r_size. RMODINPROGRESS is set in writerp().
5041 	 * When RMODINPROGRESS is set it indicates that a uiomove() is in
5042 	 * progress and the r_size has not been made consistent with the
5043 	 * new size of the file. When the uiomove() completes the r_size is
5044 	 * updated and the RMODINPROGRESS flag is cleared.
5045 	 *
5046 	 * The RMODINPROGRESS flag makes sure that nfs(3)_bio() sees a
5047 	 * consistent value of r_size. Without this handshaking, it is
5048 	 * possible that nfs(3)_bio() picks  up the old value of r_size
5049 	 * before the uiomove() in writerp() completes. This will result
5050 	 * in the write through nfs(3)_bio() being dropped.
5051 	 *
5052 	 * More precisely, there is a window between the time the uiomove()
5053 	 * completes and the time the r_size is updated. If a VOP_PUTPAGE()
5054 	 * operation intervenes in this window, the page will be picked up,
5055 	 * because it is dirty (it will be unlocked, unless it was
5056 	 * pagecreate'd). When the page is picked up as dirty, the dirty
5057 	 * bit is reset (pvn_getdirty()). In nfs(3)write(), r_size is
5058 	 * checked. This will still be the old size. Therefore the page will
5059 	 * not be written out. When segmap_release() calls VOP_PUTPAGE(),
5060 	 * the page will be found to be clean and the write will be dropped.
5061 	 */
5062 	if (rp->r_flags & RMODINPROGRESS) {
5063 		mutex_enter(&rp->r_statelock);
5064 		if ((rp->r_flags & RMODINPROGRESS) &&
5065 		    rp->r_modaddr + MAXBSIZE > io_off &&
5066 		    rp->r_modaddr < io_off + io_len) {
5067 			page_t *plist;
5068 			/*
5069 			 * A write is in progress for this region of the file.
5070 			 * If we did not detect RMODINPROGRESS here then this
5071 			 * path through nfs_putapage() would eventually go to
5072 			 * nfs(3)_bio() and may not write out all of the data
5073 			 * in the pages. We end up losing data. So we decide
5074 			 * to set the modified bit on each page in the page
5075 			 * list and mark the rnode with RDIRTY. This write
5076 			 * will be restarted at some later time.
5077 			 */
5078 			plist = pp;
5079 			while (plist != NULL) {
5080 				pp = plist;
5081 				page_sub(&plist, pp);
5082 				hat_setmod(pp);
5083 				page_io_unlock(pp);
5084 				page_unlock(pp);
5085 			}
5086 			rp->r_flags |= RDIRTY;
5087 			mutex_exit(&rp->r_statelock);
5088 			if (offp)
5089 				*offp = io_off;
5090 			if (lenp)
5091 				*lenp = io_len;
5092 			return (0);
5093 		}
5094 		mutex_exit(&rp->r_statelock);
5095 	}
5096 
5097 	if (flags & B_ASYNC) {
5098 		error = nfs_async_putapage(vp, pp, io_off, io_len, flags, cr,
5099 		    nfs3_sync_putapage);
5100 	} else
5101 		error = nfs3_sync_putapage(vp, pp, io_off, io_len, flags, cr);
5102 
5103 	if (offp)
5104 		*offp = io_off;
5105 	if (lenp)
5106 		*lenp = io_len;
5107 	return (error);
5108 }
5109 
5110 static int
5111 nfs3_sync_putapage(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5112 	int flags, cred_t *cr)
5113 {
5114 	int error;
5115 	rnode_t *rp;
5116 
5117 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5118 
5119 	flags |= B_WRITE;
5120 
5121 	error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5122 
5123 	rp = VTOR(vp);
5124 
5125 	if ((error == ENOSPC || error == EDQUOT || error == EFBIG ||
5126 	    error == EACCES) &&
5127 	    (flags & (B_INVAL|B_FORCE)) != (B_INVAL|B_FORCE)) {
5128 		if (!(rp->r_flags & ROUTOFSPACE)) {
5129 			mutex_enter(&rp->r_statelock);
5130 			rp->r_flags |= ROUTOFSPACE;
5131 			mutex_exit(&rp->r_statelock);
5132 		}
5133 		flags |= B_ERROR;
5134 		pvn_write_done(pp, flags);
5135 		/*
5136 		 * If this was not an async thread, then try again to
5137 		 * write out the pages, but this time, also destroy
5138 		 * them whether or not the write is successful.  This
5139 		 * will prevent memory from filling up with these
5140 		 * pages and destroying them is the only alternative
5141 		 * if they can't be written out.
5142 		 *
5143 		 * Don't do this if this is an async thread because
5144 		 * when the pages are unlocked in pvn_write_done,
5145 		 * some other thread could have come along, locked
5146 		 * them, and queued for an async thread.  It would be
5147 		 * possible for all of the async threads to be tied
5148 		 * up waiting to lock the pages again and they would
5149 		 * all already be locked and waiting for an async
5150 		 * thread to handle them.  Deadlock.
5151 		 */
5152 		if (!(flags & B_ASYNC)) {
5153 			error = nfs3_putpage(vp, io_off, io_len,
5154 			    B_INVAL | B_FORCE, cr, NULL);
5155 		}
5156 	} else {
5157 		if (error)
5158 			flags |= B_ERROR;
5159 		else if (rp->r_flags & ROUTOFSPACE) {
5160 			mutex_enter(&rp->r_statelock);
5161 			rp->r_flags &= ~ROUTOFSPACE;
5162 			mutex_exit(&rp->r_statelock);
5163 		}
5164 		pvn_write_done(pp, flags);
5165 		if (freemem < desfree)
5166 			(void) nfs3_commit_vp(vp, (u_offset_t)0, 0, cr);
5167 	}
5168 
5169 	return (error);
5170 }
5171 
5172 /* ARGSUSED */
5173 static int
5174 nfs3_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
5175 	size_t len, uchar_t prot, uchar_t maxprot, uint_t flags,
5176 	cred_t *cr, caller_context_t *ct)
5177 {
5178 	struct segvn_crargs vn_a;
5179 	int error;
5180 	rnode_t *rp;
5181 	struct vattr va;
5182 
5183 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5184 		return (EIO);
5185 
5186 	if (vp->v_flag & VNOMAP)
5187 		return (ENOSYS);
5188 
5189 	if (off < 0 || off + len < 0)
5190 		return (ENXIO);
5191 
5192 	if (vp->v_type != VREG)
5193 		return (ENODEV);
5194 
5195 	/*
5196 	 * If there is cached data and if close-to-open consistency
5197 	 * checking is not turned off and if the file system is not
5198 	 * mounted readonly, then force an over the wire getattr.
5199 	 * Otherwise, just invoke nfs3getattr to get a copy of the
5200 	 * attributes.  The attribute cache will be used unless it
5201 	 * is timed out and if it is, then an over the wire getattr
5202 	 * will be issued.
5203 	 */
5204 	va.va_mask = AT_ALL;
5205 	if (vn_has_cached_data(vp) &&
5206 	    !(VTOMI(vp)->mi_flags & MI_NOCTO) && !vn_is_readonly(vp))
5207 		error = nfs3_getattr_otw(vp, &va, cr);
5208 	else
5209 		error = nfs3getattr(vp, &va, cr);
5210 	if (error)
5211 		return (error);
5212 
5213 	/*
5214 	 * Check to see if the vnode is currently marked as not cachable.
5215 	 * This means portions of the file are locked (through VOP_FRLOCK).
5216 	 * In this case the map request must be refused.  We use
5217 	 * rp->r_lkserlock to avoid a race with concurrent lock requests.
5218 	 */
5219 	rp = VTOR(vp);
5220 
5221 	/*
5222 	 * Atomically increment r_inmap after acquiring r_rwlock. The
5223 	 * idea here is to acquire r_rwlock to block read/write and
5224 	 * not to protect r_inmap. r_inmap will inform nfs3_read/write()
5225 	 * that we are in nfs3_map(). Now, r_rwlock is acquired in order
5226 	 * and we can prevent the deadlock that would have occurred
5227 	 * when nfs3_addmap() would have acquired it out of order.
5228 	 *
5229 	 * Since we are not protecting r_inmap by any lock, we do not
5230 	 * hold any lock when we decrement it. We atomically decrement
5231 	 * r_inmap after we release r_lkserlock.
5232 	 */
5233 
5234 	if (nfs_rw_enter_sig(&rp->r_rwlock, RW_WRITER, INTR(vp)))
5235 		return (EINTR);
5236 	atomic_add_int(&rp->r_inmap, 1);
5237 	nfs_rw_exit(&rp->r_rwlock);
5238 
5239 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_READER, INTR(vp))) {
5240 		atomic_add_int(&rp->r_inmap, -1);
5241 		return (EINTR);
5242 	}
5243 
5244 	if (vp->v_flag & VNOCACHE) {
5245 		error = EAGAIN;
5246 		goto done;
5247 	}
5248 
5249 	/*
5250 	 * Don't allow concurrent locks and mapping if mandatory locking is
5251 	 * enabled.
5252 	 */
5253 	if ((flk_has_remote_locks(vp) || lm_has_sleep(vp)) &&
5254 	    MANDLOCK(vp, va.va_mode)) {
5255 		error = EAGAIN;
5256 		goto done;
5257 	}
5258 
5259 	as_rangelock(as);
5260 	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
5261 	if (error != 0) {
5262 		as_rangeunlock(as);
5263 		goto done;
5264 	}
5265 
5266 	vn_a.vp = vp;
5267 	vn_a.offset = off;
5268 	vn_a.type = (flags & MAP_TYPE);
5269 	vn_a.prot = (uchar_t)prot;
5270 	vn_a.maxprot = (uchar_t)maxprot;
5271 	vn_a.flags = (flags & ~MAP_TYPE);
5272 	vn_a.cred = cr;
5273 	vn_a.amp = NULL;
5274 	vn_a.szc = 0;
5275 	vn_a.lgrp_mem_policy_flags = 0;
5276 
5277 	error = as_map(as, *addrp, len, segvn_create, &vn_a);
5278 	as_rangeunlock(as);
5279 
5280 done:
5281 	nfs_rw_exit(&rp->r_lkserlock);
5282 	atomic_add_int(&rp->r_inmap, -1);
5283 	return (error);
5284 }
5285 
5286 /* ARGSUSED */
5287 static int
5288 nfs3_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5289 	size_t len, uchar_t prot, uchar_t maxprot, uint_t flags,
5290 	cred_t *cr, caller_context_t *ct)
5291 {
5292 	rnode_t *rp;
5293 
5294 	if (vp->v_flag & VNOMAP)
5295 		return (ENOSYS);
5296 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5297 		return (EIO);
5298 
5299 	rp = VTOR(vp);
5300 	atomic_add_long((ulong_t *)&rp->r_mapcnt, btopr(len));
5301 
5302 	return (0);
5303 }
5304 
5305 /* ARGSUSED */
5306 static int
5307 nfs3_frlock(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
5308 	offset_t offset, struct flk_callback *flk_cbp, cred_t *cr,
5309 	caller_context_t *ct)
5310 {
5311 	netobj lm_fh3;
5312 	int rc;
5313 	u_offset_t start, end;
5314 	rnode_t *rp;
5315 	int error = 0, intr = INTR(vp);
5316 
5317 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5318 		return (EIO);
5319 	/* check for valid cmd parameter */
5320 	if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW)
5321 		return (EINVAL);
5322 
5323 	/* Verify l_type. */
5324 	switch (bfp->l_type) {
5325 	case F_RDLCK:
5326 		if (cmd != F_GETLK && !(flag & FREAD))
5327 			return (EBADF);
5328 		break;
5329 	case F_WRLCK:
5330 		if (cmd != F_GETLK && !(flag & FWRITE))
5331 			return (EBADF);
5332 		break;
5333 	case F_UNLCK:
5334 		intr = 0;
5335 		break;
5336 
5337 	default:
5338 		return (EINVAL);
5339 	}
5340 
5341 	/* check the validity of the lock range */
5342 	if (rc = flk_convert_lock_data(vp, bfp, &start, &end, offset))
5343 		return (rc);
5344 	if (rc = flk_check_lock_data(start, end, MAXEND))
5345 		return (rc);
5346 
5347 	/*
5348 	 * If the filesystem is mounted using local locking, pass the
5349 	 * request off to the local locking code.
5350 	 */
5351 	if (VTOMI(vp)->mi_flags & MI_LLOCK) {
5352 		if (cmd == F_SETLK || cmd == F_SETLKW) {
5353 			/*
5354 			 * For complete safety, we should be holding
5355 			 * r_lkserlock.  However, we can't call
5356 			 * lm_safelock and then fs_frlock while
5357 			 * holding r_lkserlock, so just invoke
5358 			 * lm_safelock and expect that this will
5359 			 * catch enough of the cases.
5360 			 */
5361 			if (!lm_safelock(vp, bfp, cr))
5362 				return (EAGAIN);
5363 		}
5364 		return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
5365 	}
5366 
5367 	rp = VTOR(vp);
5368 
5369 	/*
5370 	 * Check whether the given lock request can proceed, given the
5371 	 * current file mappings.
5372 	 */
5373 	if (nfs_rw_enter_sig(&rp->r_lkserlock, RW_WRITER, intr))
5374 		return (EINTR);
5375 	if (cmd == F_SETLK || cmd == F_SETLKW) {
5376 		if (!lm_safelock(vp, bfp, cr)) {
5377 			rc = EAGAIN;
5378 			goto done;
5379 		}
5380 	}
5381 
5382 	/*
5383 	 * Flush the cache after waiting for async I/O to finish.  For new
5384 	 * locks, this is so that the process gets the latest bits from the
5385 	 * server.  For unlocks, this is so that other clients see the
5386 	 * latest bits once the file has been unlocked.  If currently dirty
5387 	 * pages can't be flushed, then don't allow a lock to be set.  But
5388 	 * allow unlocks to succeed, to avoid having orphan locks on the
5389 	 * server.
5390 	 */
5391 	if (cmd != F_GETLK) {
5392 		mutex_enter(&rp->r_statelock);
5393 		while (rp->r_count > 0) {
5394 			if (intr) {
5395 				klwp_t *lwp = ttolwp(curthread);
5396 
5397 				if (lwp != NULL)
5398 					lwp->lwp_nostop++;
5399 				if (cv_wait_sig(&rp->r_cv,
5400 				    &rp->r_statelock) == 0) {
5401 					if (lwp != NULL)
5402 						lwp->lwp_nostop--;
5403 					rc = EINTR;
5404 					break;
5405 				}
5406 				if (lwp != NULL)
5407 					lwp->lwp_nostop--;
5408 			} else
5409 				cv_wait(&rp->r_cv, &rp->r_statelock);
5410 		}
5411 		mutex_exit(&rp->r_statelock);
5412 		if (rc != 0)
5413 			goto done;
5414 		error = nfs3_putpage(vp, (offset_t)0, 0, B_INVAL, cr, ct);
5415 		if (error) {
5416 			if (error == ENOSPC || error == EDQUOT) {
5417 				mutex_enter(&rp->r_statelock);
5418 				if (!rp->r_error)
5419 					rp->r_error = error;
5420 				mutex_exit(&rp->r_statelock);
5421 			}
5422 			if (bfp->l_type != F_UNLCK) {
5423 				rc = ENOLCK;
5424 				goto done;
5425 			}
5426 		}
5427 	}
5428 
5429 	lm_fh3.n_len = VTOFH3(vp)->fh3_length;
5430 	lm_fh3.n_bytes = (char *)&(VTOFH3(vp)->fh3_u.data);
5431 
5432 	/*
5433 	 * Call the lock manager to do the real work of contacting
5434 	 * the server and obtaining the lock.
5435 	 */
5436 	rc = lm4_frlock(vp, cmd, bfp, flag, offset, cr, &lm_fh3, flk_cbp);
5437 
5438 	if (rc == 0)
5439 		nfs_lockcompletion(vp, cmd);
5440 
5441 done:
5442 	nfs_rw_exit(&rp->r_lkserlock);
5443 	return (rc);
5444 }
5445 
5446 /*
5447  * Free storage space associated with the specified vnode.  The portion
5448  * to be freed is specified by bfp->l_start and bfp->l_len (already
5449  * normalized to a "whence" of 0).
5450  *
5451  * This is an experimental facility whose continued existence is not
5452  * guaranteed.  Currently, we only support the special case
5453  * of l_len == 0, meaning free to end of file.
5454  */
5455 /* ARGSUSED */
5456 static int
5457 nfs3_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
5458 	offset_t offset, cred_t *cr, caller_context_t *ct)
5459 {
5460 	int error;
5461 
5462 	ASSERT(vp->v_type == VREG);
5463 	if (cmd != F_FREESP)
5464 		return (EINVAL);
5465 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5466 		return (EIO);
5467 
5468 	error = convoff(vp, bfp, 0, offset);
5469 	if (!error) {
5470 		ASSERT(bfp->l_start >= 0);
5471 		if (bfp->l_len == 0) {
5472 			struct vattr va;
5473 
5474 			/*
5475 			 * ftruncate should not change the ctime and
5476 			 * mtime if we truncate the file to its
5477 			 * previous size.
5478 			 */
5479 			va.va_mask = AT_SIZE;
5480 			error = nfs3getattr(vp, &va, cr);
5481 			if (error || va.va_size == bfp->l_start)
5482 				return (error);
5483 			va.va_mask = AT_SIZE;
5484 			va.va_size = bfp->l_start;
5485 			error = nfs3setattr(vp, &va, 0, cr);
5486 		} else
5487 			error = EINVAL;
5488 	}
5489 
5490 	return (error);
5491 }
5492 
5493 /* ARGSUSED */
5494 static int
5495 nfs3_realvp(vnode_t *vp, vnode_t **vpp, caller_context_t *ct)
5496 {
5497 
5498 	return (EINVAL);
5499 }
5500 
5501 /*
5502  * Setup and add an address space callback to do the work of the delmap call.
5503  * The callback will (and must be) deleted in the actual callback function.
5504  *
5505  * This is done in order to take care of the problem that we have with holding
5506  * the address space's a_lock for a long period of time (e.g. if the NFS server
5507  * is down).  Callbacks will be executed in the address space code while the
5508  * a_lock is not held.	Holding the address space's a_lock causes things such
5509  * as ps and fork to hang because they are trying to acquire this lock as well.
5510  */
5511 /* ARGSUSED */
5512 static int
5513 nfs3_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5514 	size_t len, uint_t prot, uint_t maxprot, uint_t flags,
5515 	cred_t *cr, caller_context_t *ct)
5516 {
5517 	int			caller_found;
5518 	int			error;
5519 	rnode_t			*rp;
5520 	nfs_delmap_args_t	*dmapp;
5521 	nfs_delmapcall_t	*delmap_call;
5522 
5523 	if (vp->v_flag & VNOMAP)
5524 		return (ENOSYS);
5525 	/*
5526 	 * A process may not change zones if it has NFS pages mmap'ed
5527 	 * in, so we can't legitimately get here from the wrong zone.
5528 	 */
5529 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5530 
5531 	rp = VTOR(vp);
5532 
5533 	/*
5534 	 * The way that the address space of this process deletes its mapping
5535 	 * of this file is via the following call chains:
5536 	 * - as_free()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs3_delmap()
5537 	 * - as_unmap()->SEGOP_UNMAP()/segvn_unmap()->VOP_DELMAP()/nfs3_delmap()
5538 	 *
5539 	 * With the use of address space callbacks we are allowed to drop the
5540 	 * address space lock, a_lock, while executing the NFS operations that
5541 	 * need to go over the wire.  Returning EAGAIN to the caller of this
5542 	 * function is what drives the execution of the callback that we add
5543 	 * below.  The callback will be executed by the address space code
5544 	 * after dropping the a_lock.  When the callback is finished, since
5545 	 * we dropped the a_lock, it must be re-acquired and segvn_unmap()
5546 	 * is called again on the same segment to finish the rest of the work
5547 	 * that needs to happen during unmapping.
5548 	 *
5549 	 * This action of calling back into the segment driver causes
5550 	 * nfs3_delmap() to get called again, but since the callback was
5551 	 * already executed at this point, it already did the work and there
5552 	 * is nothing left for us to do.
5553 	 *
5554 	 * To Summarize:
5555 	 * - The first time nfs3_delmap is called by the current thread is when
5556 	 * we add the caller associated with this delmap to the delmap caller
5557 	 * list, add the callback, and return EAGAIN.
5558 	 * - The second time in this call chain when nfs3_delmap is called we
5559 	 * will find this caller in the delmap caller list and realize there
5560 	 * is no more work to do thus removing this caller from the list and
5561 	 * returning the error that was set in the callback execution.
5562 	 */
5563 	caller_found = nfs_find_and_delete_delmapcall(rp, &error);
5564 	if (caller_found) {
5565 		/*
5566 		 * 'error' is from the actual delmap operations.  To avoid
5567 		 * hangs, we need to handle the return of EAGAIN differently
5568 		 * since this is what drives the callback execution.
5569 		 * In this case, we don't want to return EAGAIN and do the
5570 		 * callback execution because there are none to execute.
5571 		 */
5572 		if (error == EAGAIN)
5573 			return (0);
5574 		else
5575 			return (error);
5576 	}
5577 
5578 	/* current caller was not in the list */
5579 	delmap_call = nfs_init_delmapcall();
5580 
5581 	mutex_enter(&rp->r_statelock);
5582 	list_insert_tail(&rp->r_indelmap, delmap_call);
5583 	mutex_exit(&rp->r_statelock);
5584 
5585 	dmapp = kmem_alloc(sizeof (nfs_delmap_args_t), KM_SLEEP);
5586 
5587 	dmapp->vp = vp;
5588 	dmapp->off = off;
5589 	dmapp->addr = addr;
5590 	dmapp->len = len;
5591 	dmapp->prot = prot;
5592 	dmapp->maxprot = maxprot;
5593 	dmapp->flags = flags;
5594 	dmapp->cr = cr;
5595 	dmapp->caller = delmap_call;
5596 
5597 	error = as_add_callback(as, nfs3_delmap_callback, dmapp,
5598 	    AS_UNMAP_EVENT, addr, len, KM_SLEEP);
5599 
5600 	return (error ? error : EAGAIN);
5601 }
5602 
5603 /*
5604  * Remove some pages from an mmap'd vnode.  Just update the
5605  * count of pages.  If doing close-to-open, then flush and
5606  * commit all of the pages associated with this file.
5607  * Otherwise, start an asynchronous page flush to write out
5608  * any dirty pages.  This will also associate a credential
5609  * with the rnode which can be used to write the pages.
5610  */
5611 /* ARGSUSED */
5612 static void
5613 nfs3_delmap_callback(struct as *as, void *arg, uint_t event)
5614 {
5615 	int			error;
5616 	rnode_t			*rp;
5617 	mntinfo_t		*mi;
5618 	nfs_delmap_args_t	*dmapp = (nfs_delmap_args_t *)arg;
5619 
5620 	rp = VTOR(dmapp->vp);
5621 	mi = VTOMI(dmapp->vp);
5622 
5623 	atomic_add_long((ulong_t *)&rp->r_mapcnt, -btopr(dmapp->len));
5624 	ASSERT(rp->r_mapcnt >= 0);
5625 
5626 	/*
5627 	 * Initiate a page flush and potential commit if there are
5628 	 * pages, the file system was not mounted readonly, the segment
5629 	 * was mapped shared, and the pages themselves were writeable.
5630 	 */
5631 	if (vn_has_cached_data(dmapp->vp) && !vn_is_readonly(dmapp->vp) &&
5632 	    dmapp->flags == MAP_SHARED && (dmapp->maxprot & PROT_WRITE)) {
5633 		mutex_enter(&rp->r_statelock);
5634 		rp->r_flags |= RDIRTY;
5635 		mutex_exit(&rp->r_statelock);
5636 		/*
5637 		 * If this is a cross-zone access a sync putpage won't work, so
5638 		 * the best we can do is try an async putpage.  That seems
5639 		 * better than something more draconian such as discarding the
5640 		 * dirty pages.
5641 		 */
5642 		if ((mi->mi_flags & MI_NOCTO) ||
5643 		    nfs_zone() != mi->mi_zone)
5644 			error = nfs3_putpage(dmapp->vp, dmapp->off, dmapp->len,
5645 			    B_ASYNC, dmapp->cr, NULL);
5646 		else
5647 			error = nfs3_putpage_commit(dmapp->vp, dmapp->off,
5648 			    dmapp->len, dmapp->cr);
5649 		if (!error) {
5650 			mutex_enter(&rp->r_statelock);
5651 			error = rp->r_error;
5652 			rp->r_error = 0;
5653 			mutex_exit(&rp->r_statelock);
5654 		}
5655 	} else
5656 		error = 0;
5657 
5658 	if ((rp->r_flags & RDIRECTIO) || (mi->mi_flags & MI_DIRECTIO))
5659 		(void) nfs3_putpage(dmapp->vp, dmapp->off, dmapp->len,
5660 		    B_INVAL, dmapp->cr, NULL);
5661 
5662 	dmapp->caller->error = error;
5663 	(void) as_delete_callback(as, arg);
5664 	kmem_free(dmapp, sizeof (nfs_delmap_args_t));
5665 }
5666 
5667 static int nfs3_pathconf_disable_cache = 0;
5668 
5669 #ifdef DEBUG
5670 static int nfs3_pathconf_cache_hits = 0;
5671 static int nfs3_pathconf_cache_misses = 0;
5672 #endif
5673 
5674 /* ARGSUSED */
5675 static int
5676 nfs3_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
5677 	caller_context_t *ct)
5678 {
5679 	int error;
5680 	PATHCONF3args args;
5681 	PATHCONF3res res;
5682 	int douprintf;
5683 	failinfo_t fi;
5684 	rnode_t *rp;
5685 	hrtime_t t;
5686 
5687 	if (nfs_zone() != VTOMI(vp)->mi_zone)
5688 		return (EIO);
5689 	/*
5690 	 * Large file spec - need to base answer on info stored
5691 	 * on original FSINFO response.
5692 	 */
5693 	if (cmd == _PC_FILESIZEBITS) {
5694 		unsigned long long ll;
5695 		long l = 1;
5696 
5697 		ll = VTOMI(vp)->mi_maxfilesize;
5698 
5699 		if (ll == 0) {
5700 			*valp = 0;
5701 			return (0);
5702 		}
5703 
5704 		if (ll & 0xffffffff00000000) {
5705 			l += 32; ll >>= 32;
5706 		}
5707 		if (ll & 0xffff0000) {
5708 			l += 16; ll >>= 16;
5709 		}
5710 		if (ll & 0xff00) {
5711 			l += 8; ll >>= 8;
5712 		}
5713 		if (ll & 0xf0) {
5714 			l += 4; ll >>= 4;
5715 		}
5716 		if (ll & 0xc) {
5717 			l += 2; ll >>= 2;
5718 		}
5719 		if (ll & 0x2)
5720 			l += 2;
5721 		else if (ll & 0x1)
5722 			l += 1;
5723 		*valp = l;
5724 		return (0);
5725 	}
5726 
5727 	if (cmd == _PC_ACL_ENABLED) {
5728 		*valp = _ACL_ACLENT_ENABLED;
5729 		return (0);
5730 	}
5731 
5732 	if (cmd == _PC_XATTR_EXISTS) {
5733 		error = 0;
5734 		*valp = 0;
5735 		if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
5736 			vnode_t *avp;
5737 			rnode_t *rp;
5738 			int error = 0;
5739 			mntinfo_t *mi = VTOMI(vp);
5740 
5741 			if (!(mi->mi_flags & MI_EXTATTR))
5742 				return (0);
5743 
5744 			rp = VTOR(vp);
5745 			if (nfs_rw_enter_sig(&rp->r_rwlock, RW_READER,
5746 			    INTR(vp)))
5747 				return (EINTR);
5748 
5749 			error = nfs3lookup_dnlc(vp, XATTR_DIR_NAME, &avp, cr);
5750 			if (error || avp == NULL)
5751 				error = acl_getxattrdir3(vp, &avp, 0, cr, 0);
5752 
5753 			nfs_rw_exit(&rp->r_rwlock);
5754 
5755 			if (error == 0 && avp != NULL) {
5756 				error = do_xattr_exists_check(avp, valp, cr);
5757 				VN_RELE(avp);
5758 			} else if (error == ENOENT) {
5759 				error = 0;
5760 				*valp = 0;
5761 			}
5762 		}
5763 		return (error);
5764 	}
5765 
5766 	rp = VTOR(vp);
5767 	if (rp->r_pathconf != NULL) {
5768 		mutex_enter(&rp->r_statelock);
5769 		if (rp->r_pathconf != NULL && nfs3_pathconf_disable_cache) {
5770 			kmem_free(rp->r_pathconf, sizeof (*rp->r_pathconf));
5771 			rp->r_pathconf = NULL;
5772 		}
5773 		if (rp->r_pathconf != NULL) {
5774 			error = 0;
5775 			switch (cmd) {
5776 			case _PC_LINK_MAX:
5777 				*valp = rp->r_pathconf->link_max;
5778 				break;
5779 			case _PC_NAME_MAX:
5780 				*valp = rp->r_pathconf->name_max;
5781 				break;
5782 			case _PC_PATH_MAX:
5783 			case _PC_SYMLINK_MAX:
5784 				*valp = MAXPATHLEN;
5785 				break;
5786 			case _PC_CHOWN_RESTRICTED:
5787 				*valp = rp->r_pathconf->chown_restricted;
5788 				break;
5789 			case _PC_NO_TRUNC:
5790 				*valp = rp->r_pathconf->no_trunc;
5791 				break;
5792 			default:
5793 				error = EINVAL;
5794 				break;
5795 			}
5796 			mutex_exit(&rp->r_statelock);
5797 #ifdef DEBUG
5798 			nfs3_pathconf_cache_hits++;
5799 #endif
5800 			return (error);
5801 		}
5802 		mutex_exit(&rp->r_statelock);
5803 	}
5804 #ifdef DEBUG
5805 	nfs3_pathconf_cache_misses++;
5806 #endif
5807 
5808 	args.object = *VTOFH3(vp);
5809 	fi.vp = vp;
5810 	fi.fhp = (caddr_t)&args.object;
5811 	fi.copyproc = nfs3copyfh;
5812 	fi.lookupproc = nfs3lookup;
5813 	fi.xattrdirproc = acl_getxattrdir3;
5814 
5815 	douprintf = 1;
5816 
5817 	t = gethrtime();
5818 
5819 	error = rfs3call(VTOMI(vp), NFSPROC3_PATHCONF,
5820 	    xdr_nfs_fh3, (caddr_t)&args,
5821 	    xdr_PATHCONF3res, (caddr_t)&res, cr,
5822 	    &douprintf, &res.status, 0, &fi);
5823 
5824 	if (error)
5825 		return (error);
5826 
5827 	error = geterrno3(res.status);
5828 
5829 	if (!error) {
5830 		nfs3_cache_post_op_attr(vp, &res.resok.obj_attributes, t, cr);
5831 		if (!nfs3_pathconf_disable_cache) {
5832 			mutex_enter(&rp->r_statelock);
5833 			if (rp->r_pathconf == NULL) {
5834 				rp->r_pathconf = kmem_alloc(
5835 				    sizeof (*rp->r_pathconf), KM_NOSLEEP);
5836 				if (rp->r_pathconf != NULL)
5837 					*rp->r_pathconf = res.resok.info;
5838 			}
5839 			mutex_exit(&rp->r_statelock);
5840 		}
5841 		switch (cmd) {
5842 		case _PC_LINK_MAX:
5843 			*valp = res.resok.info.link_max;
5844 			break;
5845 		case _PC_NAME_MAX:
5846 			*valp = res.resok.info.name_max;
5847 			break;
5848 		case _PC_PATH_MAX:
5849 		case _PC_SYMLINK_MAX:
5850 			*valp = MAXPATHLEN;
5851 			break;
5852 		case _PC_CHOWN_RESTRICTED:
5853 			*valp = res.resok.info.chown_restricted;
5854 			break;
5855 		case _PC_NO_TRUNC:
5856 			*valp = res.resok.info.no_trunc;
5857 			break;
5858 		default:
5859 			return (EINVAL);
5860 		}
5861 	} else {
5862 		nfs3_cache_post_op_attr(vp, &res.resfail.obj_attributes, t, cr);
5863 		PURGE_STALE_FH(error, vp, cr);
5864 	}
5865 
5866 	return (error);
5867 }
5868 
5869 /*
5870  * Called by async thread to do synchronous pageio. Do the i/o, wait
5871  * for it to complete, and cleanup the page list when done.
5872  */
5873 static int
5874 nfs3_sync_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5875 	int flags, cred_t *cr)
5876 {
5877 	int error;
5878 
5879 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
5880 	error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5881 	if (flags & B_READ)
5882 		pvn_read_done(pp, (error ? B_ERROR : 0) | flags);
5883 	else
5884 		pvn_write_done(pp, (error ? B_ERROR : 0) | flags);
5885 	return (error);
5886 }
5887 
5888 /* ARGSUSED */
5889 static int
5890 nfs3_pageio(vnode_t *vp, page_t *pp, u_offset_t io_off, size_t io_len,
5891 	int flags, cred_t *cr, caller_context_t *ct)
5892 {
5893 	int error;
5894 	rnode_t *rp;
5895 
5896 	if (pp == NULL)
5897 		return (EINVAL);
5898 	if (!(flags & B_ASYNC) && nfs_zone() != VTOMI(vp)->mi_zone)
5899 		return (EIO);
5900 
5901 	rp = VTOR(vp);
5902 	mutex_enter(&rp->r_statelock);
5903 	rp->r_count++;
5904 	mutex_exit(&rp->r_statelock);
5905 
5906 	if (flags & B_ASYNC) {
5907 		error = nfs_async_pageio(vp, pp, io_off, io_len, flags, cr,
5908 		    nfs3_sync_pageio);
5909 	} else
5910 		error = nfs3_rdwrlbn(vp, pp, io_off, io_len, flags, cr);
5911 	mutex_enter(&rp->r_statelock);
5912 	rp->r_count--;
5913 	cv_broadcast(&rp->r_cv);
5914 	mutex_exit(&rp->r_statelock);
5915 	return (error);
5916 }
5917 
5918 /* ARGSUSED */
5919 static void
5920 nfs3_dispose(vnode_t *vp, page_t *pp, int fl, int dn, cred_t *cr,
5921 	caller_context_t *ct)
5922 {
5923 	int error;
5924 	rnode_t *rp;
5925 	page_t *plist;
5926 	page_t *pptr;
5927 	offset3 offset;
5928 	count3 len;
5929 	k_sigset_t smask;
5930 
5931 	/*
5932 	 * We should get called with fl equal to either B_FREE or
5933 	 * B_INVAL.  Any other value is illegal.
5934 	 *
5935 	 * The page that we are either supposed to free or destroy
5936 	 * should be exclusive locked and its io lock should not
5937 	 * be held.
5938 	 */
5939 	ASSERT(fl == B_FREE || fl == B_INVAL);
5940 	ASSERT((PAGE_EXCL(pp) && !page_iolock_assert(pp)) || panicstr);
5941 	rp = VTOR(vp);
5942 
5943 	/*
5944 	 * If the page doesn't need to be committed or we shouldn't
5945 	 * even bother attempting to commit it, then just make sure
5946 	 * that the p_fsdata byte is clear and then either free or
5947 	 * destroy the page as appropriate.
5948 	 */
5949 	if (pp->p_fsdata == C_NOCOMMIT || (rp->r_flags & RSTALE)) {
5950 		pp->p_fsdata = C_NOCOMMIT;
5951 		if (fl == B_FREE)
5952 			page_free(pp, dn);
5953 		else
5954 			page_destroy(pp, dn);
5955 		return;
5956 	}
5957 
5958 	/*
5959 	 * If there is a page invalidation operation going on, then
5960 	 * if this is one of the pages being destroyed, then just
5961 	 * clear the p_fsdata byte and then either free or destroy
5962 	 * the page as appropriate.
5963 	 */
5964 	mutex_enter(&rp->r_statelock);
5965 	if ((rp->r_flags & RTRUNCATE) && pp->p_offset >= rp->r_truncaddr) {
5966 		mutex_exit(&rp->r_statelock);
5967 		pp->p_fsdata = C_NOCOMMIT;
5968 		if (fl == B_FREE)
5969 			page_free(pp, dn);
5970 		else
5971 			page_destroy(pp, dn);
5972 		return;
5973 	}
5974 
5975 	/*
5976 	 * If we are freeing this page and someone else is already
5977 	 * waiting to do a commit, then just unlock the page and
5978 	 * return.  That other thread will take care of commiting
5979 	 * this page.  The page can be freed sometime after the
5980 	 * commit has finished.  Otherwise, if the page is marked
5981 	 * as delay commit, then we may be getting called from
5982 	 * pvn_write_done, one page at a time.   This could result
5983 	 * in one commit per page, so we end up doing lots of small
5984 	 * commits instead of fewer larger commits.  This is bad,
5985 	 * we want do as few commits as possible.
5986 	 */
5987 	if (fl == B_FREE) {
5988 		if (rp->r_flags & RCOMMITWAIT) {
5989 			page_unlock(pp);
5990 			mutex_exit(&rp->r_statelock);
5991 			return;
5992 		}
5993 		if (pp->p_fsdata == C_DELAYCOMMIT) {
5994 			pp->p_fsdata = C_COMMIT;
5995 			page_unlock(pp);
5996 			mutex_exit(&rp->r_statelock);
5997 			return;
5998 		}
5999 	}
6000 
6001 	/*
6002 	 * Check to see if there is a signal which would prevent an
6003 	 * attempt to commit the pages from being successful.  If so,
6004 	 * then don't bother with all of the work to gather pages and
6005 	 * generate the unsuccessful RPC.  Just return from here and
6006 	 * let the page be committed at some later time.
6007 	 */
6008 	sigintr(&smask, VTOMI(vp)->mi_flags & MI_INT);
6009 	if (ttolwp(curthread) != NULL && ISSIG(curthread, JUSTLOOKING)) {
6010 		sigunintr(&smask);
6011 		page_unlock(pp);
6012 		mutex_exit(&rp->r_statelock);
6013 		return;
6014 	}
6015 	sigunintr(&smask);
6016 
6017 	/*
6018 	 * We are starting to need to commit pages, so let's try
6019 	 * to commit as many as possible at once to reduce the
6020 	 * overhead.
6021 	 *
6022 	 * Set the `commit inprogress' state bit.  We must
6023 	 * first wait until any current one finishes.  Then
6024 	 * we initialize the c_pages list with this page.
6025 	 */
6026 	while (rp->r_flags & RCOMMIT) {
6027 		rp->r_flags |= RCOMMITWAIT;
6028 		cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
6029 		rp->r_flags &= ~RCOMMITWAIT;
6030 	}
6031 	rp->r_flags |= RCOMMIT;
6032 	mutex_exit(&rp->r_statelock);
6033 	ASSERT(rp->r_commit.c_pages == NULL);
6034 	rp->r_commit.c_pages = pp;
6035 	rp->r_commit.c_commbase = (offset3)pp->p_offset;
6036 	rp->r_commit.c_commlen = PAGESIZE;
6037 
6038 	/*
6039 	 * Gather together all other pages which can be committed.
6040 	 * They will all be chained off r_commit.c_pages.
6041 	 */
6042 	nfs3_get_commit(vp);
6043 
6044 	/*
6045 	 * Clear the `commit inprogress' status and disconnect
6046 	 * the list of pages to be committed from the rnode.
6047 	 * At this same time, we also save the starting offset
6048 	 * and length of data to be committed on the server.
6049 	 */
6050 	plist = rp->r_commit.c_pages;
6051 	rp->r_commit.c_pages = NULL;
6052 	offset = rp->r_commit.c_commbase;
6053 	len = rp->r_commit.c_commlen;
6054 	mutex_enter(&rp->r_statelock);
6055 	rp->r_flags &= ~RCOMMIT;
6056 	cv_broadcast(&rp->r_commit.c_cv);
6057 	mutex_exit(&rp->r_statelock);
6058 
6059 	if (curproc == proc_pageout || curproc == proc_fsflush ||
6060 	    nfs_zone() != VTOMI(vp)->mi_zone) {
6061 		nfs_async_commit(vp, plist, offset, len, cr, nfs3_async_commit);
6062 		return;
6063 	}
6064 
6065 	/*
6066 	 * Actually generate the COMMIT3 over the wire operation.
6067 	 */
6068 	error = nfs3_commit(vp, offset, len, cr);
6069 
6070 	/*
6071 	 * If we got an error during the commit, just unlock all
6072 	 * of the pages.  The pages will get retransmitted to the
6073 	 * server during a putpage operation.
6074 	 */
6075 	if (error) {
6076 		while (plist != NULL) {
6077 			pptr = plist;
6078 			page_sub(&plist, pptr);
6079 			page_unlock(pptr);
6080 		}
6081 		return;
6082 	}
6083 
6084 	/*
6085 	 * We've tried as hard as we can to commit the data to stable
6086 	 * storage on the server.  We release the rest of the pages
6087 	 * and clear the commit required state.  They will be put
6088 	 * onto the tail of the cachelist if they are nolonger
6089 	 * mapped.
6090 	 */
6091 	while (plist != pp) {
6092 		pptr = plist;
6093 		page_sub(&plist, pptr);
6094 		pptr->p_fsdata = C_NOCOMMIT;
6095 		(void) page_release(pptr, 1);
6096 	}
6097 
6098 	/*
6099 	 * It is possible that nfs3_commit didn't return error but
6100 	 * some other thread has modified the page we are going
6101 	 * to free/destroy.
6102 	 *    In this case we need to rewrite the page. Do an explicit check
6103 	 * before attempting to free/destroy the page. If modified, needs to
6104 	 * be rewritten so unlock the page and return.
6105 	 */
6106 	if (hat_ismod(pp)) {
6107 		pp->p_fsdata = C_NOCOMMIT;
6108 		page_unlock(pp);
6109 		return;
6110 	}
6111 
6112 	/*
6113 	 * Now, as appropriate, either free or destroy the page
6114 	 * that we were called with.
6115 	 */
6116 	pp->p_fsdata = C_NOCOMMIT;
6117 	if (fl == B_FREE)
6118 		page_free(pp, dn);
6119 	else
6120 		page_destroy(pp, dn);
6121 }
6122 
6123 static int
6124 nfs3_commit(vnode_t *vp, offset3 offset, count3 count, cred_t *cr)
6125 {
6126 	int error;
6127 	rnode_t *rp;
6128 	COMMIT3args args;
6129 	COMMIT3res res;
6130 	int douprintf;
6131 	cred_t *cred;
6132 
6133 	rp = VTOR(vp);
6134 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6135 
6136 	mutex_enter(&rp->r_statelock);
6137 	if (rp->r_cred != NULL) {
6138 		cred = rp->r_cred;
6139 		crhold(cred);
6140 	} else {
6141 		rp->r_cred = cr;
6142 		crhold(cr);
6143 		cred = cr;
6144 		crhold(cred);
6145 	}
6146 	mutex_exit(&rp->r_statelock);
6147 
6148 	args.file = *VTOFH3(vp);
6149 	args.offset = offset;
6150 	args.count = count;
6151 
6152 doitagain:
6153 	douprintf = 1;
6154 	error = rfs3call(VTOMI(vp), NFSPROC3_COMMIT,
6155 	    xdr_COMMIT3args, (caddr_t)&args,
6156 	    xdr_COMMIT3res, (caddr_t)&res, cred,
6157 	    &douprintf, &res.status, 0, NULL);
6158 
6159 	crfree(cred);
6160 
6161 	if (error)
6162 		return (error);
6163 
6164 	error = geterrno3(res.status);
6165 	if (!error) {
6166 		ASSERT(rp->r_flags & RHAVEVERF);
6167 		mutex_enter(&rp->r_statelock);
6168 		if (rp->r_verf == res.resok.verf) {
6169 			mutex_exit(&rp->r_statelock);
6170 			return (0);
6171 		}
6172 		nfs3_set_mod(vp);
6173 		rp->r_verf = res.resok.verf;
6174 		mutex_exit(&rp->r_statelock);
6175 		error = NFS_VERF_MISMATCH;
6176 	} else {
6177 		if (error == EACCES) {
6178 			mutex_enter(&rp->r_statelock);
6179 			if (cred != cr) {
6180 				if (rp->r_cred != NULL)
6181 					crfree(rp->r_cred);
6182 				rp->r_cred = cr;
6183 				crhold(cr);
6184 				cred = cr;
6185 				crhold(cred);
6186 				mutex_exit(&rp->r_statelock);
6187 				goto doitagain;
6188 			}
6189 			mutex_exit(&rp->r_statelock);
6190 		}
6191 		/*
6192 		 * Can't do a PURGE_STALE_FH here because this
6193 		 * can cause a deadlock.  nfs3_commit can
6194 		 * be called from nfs3_dispose which can be called
6195 		 * indirectly via pvn_vplist_dirty.  PURGE_STALE_FH
6196 		 * can call back to pvn_vplist_dirty.
6197 		 */
6198 		if (error == ESTALE) {
6199 			mutex_enter(&rp->r_statelock);
6200 			rp->r_flags |= RSTALE;
6201 			if (!rp->r_error)
6202 				rp->r_error = error;
6203 			mutex_exit(&rp->r_statelock);
6204 			PURGE_ATTRCACHE(vp);
6205 		} else {
6206 			mutex_enter(&rp->r_statelock);
6207 			if (!rp->r_error)
6208 				rp->r_error = error;
6209 			mutex_exit(&rp->r_statelock);
6210 		}
6211 	}
6212 
6213 	return (error);
6214 }
6215 
6216 static void
6217 nfs3_set_mod(vnode_t *vp)
6218 {
6219 	page_t *pp;
6220 	kmutex_t *vphm;
6221 
6222 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6223 	vphm = page_vnode_mutex(vp);
6224 	mutex_enter(vphm);
6225 	if ((pp = vp->v_pages) != NULL) {
6226 		do {
6227 			if (pp->p_fsdata != C_NOCOMMIT) {
6228 				hat_setmod(pp);
6229 				pp->p_fsdata = C_NOCOMMIT;
6230 			}
6231 		} while ((pp = pp->p_vpnext) != vp->v_pages);
6232 	}
6233 	mutex_exit(vphm);
6234 }
6235 
6236 
6237 /*
6238  * This routine is used to gather together a page list of the pages
6239  * which are to be committed on the server.  This routine must not
6240  * be called if the calling thread holds any locked pages.
6241  *
6242  * The calling thread must have set RCOMMIT.  This bit is used to
6243  * serialize access to the commit structure in the rnode.  As long
6244  * as the thread has set RCOMMIT, then it can manipulate the commit
6245  * structure without requiring any other locks.
6246  */
6247 static void
6248 nfs3_get_commit(vnode_t *vp)
6249 {
6250 	rnode_t *rp;
6251 	page_t *pp;
6252 	kmutex_t *vphm;
6253 
6254 	rp = VTOR(vp);
6255 
6256 	ASSERT(rp->r_flags & RCOMMIT);
6257 
6258 	vphm = page_vnode_mutex(vp);
6259 	mutex_enter(vphm);
6260 
6261 	/*
6262 	 * If there are no pages associated with this vnode, then
6263 	 * just return.
6264 	 */
6265 	if ((pp = vp->v_pages) == NULL) {
6266 		mutex_exit(vphm);
6267 		return;
6268 	}
6269 
6270 	/*
6271 	 * Step through all of the pages associated with this vnode
6272 	 * looking for pages which need to be committed.
6273 	 */
6274 	do {
6275 		/*
6276 		 * If this page does not need to be committed or is
6277 		 * modified, then just skip it.
6278 		 */
6279 		if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp))
6280 			continue;
6281 
6282 		/*
6283 		 * Attempt to lock the page.  If we can't, then
6284 		 * someone else is messing with it and we will
6285 		 * just skip it.
6286 		 */
6287 		if (!page_trylock(pp, SE_EXCL))
6288 			continue;
6289 
6290 		/*
6291 		 * If this page does not need to be committed or is
6292 		 * modified, then just skip it.  Recheck now that
6293 		 * the page is locked.
6294 		 */
6295 		if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
6296 			page_unlock(pp);
6297 			continue;
6298 		}
6299 
6300 		if (PP_ISFREE(pp)) {
6301 			cmn_err(CE_PANIC, "nfs3_get_commit: %p is free",
6302 			    (void *)pp);
6303 		}
6304 
6305 		/*
6306 		 * The page needs to be committed and we locked it.
6307 		 * Update the base and length parameters and add it
6308 		 * to r_pages.
6309 		 */
6310 		if (rp->r_commit.c_pages == NULL) {
6311 			rp->r_commit.c_commbase = (offset3)pp->p_offset;
6312 			rp->r_commit.c_commlen = PAGESIZE;
6313 		} else if (pp->p_offset < rp->r_commit.c_commbase) {
6314 			rp->r_commit.c_commlen = rp->r_commit.c_commbase -
6315 			    (offset3)pp->p_offset + rp->r_commit.c_commlen;
6316 			rp->r_commit.c_commbase = (offset3)pp->p_offset;
6317 		} else if ((rp->r_commit.c_commbase + rp->r_commit.c_commlen)
6318 		    <= pp->p_offset) {
6319 			rp->r_commit.c_commlen = (offset3)pp->p_offset -
6320 			    rp->r_commit.c_commbase + PAGESIZE;
6321 		}
6322 		page_add(&rp->r_commit.c_pages, pp);
6323 	} while ((pp = pp->p_vpnext) != vp->v_pages);
6324 
6325 	mutex_exit(vphm);
6326 }
6327 
6328 /*
6329  * This routine is used to gather together a page list of the pages
6330  * which are to be committed on the server.  This routine must not
6331  * be called if the calling thread holds any locked pages.
6332  *
6333  * The calling thread must have set RCOMMIT.  This bit is used to
6334  * serialize access to the commit structure in the rnode.  As long
6335  * as the thread has set RCOMMIT, then it can manipulate the commit
6336  * structure without requiring any other locks.
6337  */
6338 static void
6339 nfs3_get_commit_range(vnode_t *vp, u_offset_t soff, size_t len)
6340 {
6341 
6342 	rnode_t *rp;
6343 	page_t *pp;
6344 	u_offset_t end;
6345 	u_offset_t off;
6346 
6347 	ASSERT(len != 0);
6348 
6349 	rp = VTOR(vp);
6350 
6351 	ASSERT(rp->r_flags & RCOMMIT);
6352 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6353 
6354 	/*
6355 	 * If there are no pages associated with this vnode, then
6356 	 * just return.
6357 	 */
6358 	if ((pp = vp->v_pages) == NULL)
6359 		return;
6360 
6361 	/*
6362 	 * Calculate the ending offset.
6363 	 */
6364 	end = soff + len;
6365 
6366 	for (off = soff; off < end; off += PAGESIZE) {
6367 		/*
6368 		 * Lookup each page by vp, offset.
6369 		 */
6370 		if ((pp = page_lookup_nowait(vp, off, SE_EXCL)) == NULL)
6371 			continue;
6372 
6373 		/*
6374 		 * If this page does not need to be committed or is
6375 		 * modified, then just skip it.
6376 		 */
6377 		if (pp->p_fsdata == C_NOCOMMIT || hat_ismod(pp)) {
6378 			page_unlock(pp);
6379 			continue;
6380 		}
6381 
6382 		ASSERT(PP_ISFREE(pp) == 0);
6383 
6384 		/*
6385 		 * The page needs to be committed and we locked it.
6386 		 * Update the base and length parameters and add it
6387 		 * to r_pages.
6388 		 */
6389 		if (rp->r_commit.c_pages == NULL) {
6390 			rp->r_commit.c_commbase = (offset3)pp->p_offset;
6391 			rp->r_commit.c_commlen = PAGESIZE;
6392 		} else {
6393 			rp->r_commit.c_commlen = (offset3)pp->p_offset -
6394 			    rp->r_commit.c_commbase + PAGESIZE;
6395 		}
6396 		page_add(&rp->r_commit.c_pages, pp);
6397 	}
6398 }
6399 
6400 #if 0	/* unused */
6401 #ifdef DEBUG
6402 static int
6403 nfs3_no_uncommitted_pages(vnode_t *vp)
6404 {
6405 	page_t *pp;
6406 	kmutex_t *vphm;
6407 
6408 	vphm = page_vnode_mutex(vp);
6409 	mutex_enter(vphm);
6410 	if ((pp = vp->v_pages) != NULL) {
6411 		do {
6412 			if (pp->p_fsdata != C_NOCOMMIT) {
6413 				mutex_exit(vphm);
6414 				return (0);
6415 			}
6416 		} while ((pp = pp->p_vpnext) != vp->v_pages);
6417 	}
6418 	mutex_exit(vphm);
6419 
6420 	return (1);
6421 }
6422 #endif
6423 #endif
6424 
6425 static int
6426 nfs3_putpage_commit(vnode_t *vp, offset_t poff, size_t plen, cred_t *cr)
6427 {
6428 	int error;
6429 	writeverf3 write_verf;
6430 	rnode_t *rp = VTOR(vp);
6431 
6432 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6433 	/*
6434 	 * Flush the data portion of the file and then commit any
6435 	 * portions which need to be committed.  This may need to
6436 	 * be done twice if the server has changed state since
6437 	 * data was last written.  The data will need to be
6438 	 * rewritten to the server and then a new commit done.
6439 	 *
6440 	 * In fact, this may need to be done several times if the
6441 	 * server is having problems and crashing while we are
6442 	 * attempting to do this.
6443 	 */
6444 
6445 top:
6446 	/*
6447 	 * Do a flush based on the poff and plen arguments.  This
6448 	 * will asynchronously write out any modified pages in the
6449 	 * range specified by (poff, plen).  This starts all of the
6450 	 * i/o operations which will be waited for in the next
6451 	 * call to nfs3_putpage
6452 	 */
6453 
6454 	mutex_enter(&rp->r_statelock);
6455 	write_verf = rp->r_verf;
6456 	mutex_exit(&rp->r_statelock);
6457 
6458 	error = nfs3_putpage(vp, poff, plen, B_ASYNC, cr, NULL);
6459 	if (error == EAGAIN)
6460 		error = 0;
6461 
6462 	/*
6463 	 * Do a flush based on the poff and plen arguments.  This
6464 	 * will synchronously write out any modified pages in the
6465 	 * range specified by (poff, plen) and wait until all of
6466 	 * the asynchronous i/o's in that range are done as well.
6467 	 */
6468 	if (!error)
6469 		error = nfs3_putpage(vp, poff, plen, 0, cr, NULL);
6470 
6471 	if (error)
6472 		return (error);
6473 
6474 	mutex_enter(&rp->r_statelock);
6475 	if (rp->r_verf != write_verf) {
6476 		mutex_exit(&rp->r_statelock);
6477 		goto top;
6478 	}
6479 	mutex_exit(&rp->r_statelock);
6480 
6481 	/*
6482 	 * Now commit any pages which might need to be committed.
6483 	 * If the error, NFS_VERF_MISMATCH, is returned, then
6484 	 * start over with the flush operation.
6485 	 */
6486 
6487 	error = nfs3_commit_vp(vp, poff, plen, cr);
6488 
6489 	if (error == NFS_VERF_MISMATCH)
6490 		goto top;
6491 
6492 	return (error);
6493 }
6494 
6495 static int
6496 nfs3_commit_vp(vnode_t *vp, u_offset_t poff, size_t plen, cred_t *cr)
6497 {
6498 	rnode_t *rp;
6499 	page_t *plist;
6500 	offset3 offset;
6501 	count3 len;
6502 
6503 
6504 	rp = VTOR(vp);
6505 
6506 	if (nfs_zone() != VTOMI(vp)->mi_zone)
6507 		return (EIO);
6508 	/*
6509 	 * Set the `commit inprogress' state bit.  We must
6510 	 * first wait until any current one finishes.
6511 	 */
6512 	mutex_enter(&rp->r_statelock);
6513 	while (rp->r_flags & RCOMMIT) {
6514 		rp->r_flags |= RCOMMITWAIT;
6515 		cv_wait(&rp->r_commit.c_cv, &rp->r_statelock);
6516 		rp->r_flags &= ~RCOMMITWAIT;
6517 	}
6518 	rp->r_flags |= RCOMMIT;
6519 	mutex_exit(&rp->r_statelock);
6520 
6521 	/*
6522 	 * Gather together all of the pages which need to be
6523 	 * committed.
6524 	 */
6525 	if (plen == 0)
6526 		nfs3_get_commit(vp);
6527 	else
6528 		nfs3_get_commit_range(vp, poff, plen);
6529 
6530 	/*
6531 	 * Clear the `commit inprogress' bit and disconnect the
6532 	 * page list which was gathered together in nfs3_get_commit.
6533 	 */
6534 	plist = rp->r_commit.c_pages;
6535 	rp->r_commit.c_pages = NULL;
6536 	offset = rp->r_commit.c_commbase;
6537 	len = rp->r_commit.c_commlen;
6538 	mutex_enter(&rp->r_statelock);
6539 	rp->r_flags &= ~RCOMMIT;
6540 	cv_broadcast(&rp->r_commit.c_cv);
6541 	mutex_exit(&rp->r_statelock);
6542 
6543 	/*
6544 	 * If any pages need to be committed, commit them and
6545 	 * then unlock them so that they can be freed some
6546 	 * time later.
6547 	 */
6548 	if (plist != NULL) {
6549 		/*
6550 		 * No error occurred during the flush portion
6551 		 * of this operation, so now attempt to commit
6552 		 * the data to stable storage on the server.
6553 		 *
6554 		 * This will unlock all of the pages on the list.
6555 		 */
6556 		return (nfs3_sync_commit(vp, plist, offset, len, cr));
6557 	}
6558 	return (0);
6559 }
6560 
6561 static int
6562 nfs3_sync_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
6563 	cred_t *cr)
6564 {
6565 	int error;
6566 	page_t *pp;
6567 
6568 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6569 	error = nfs3_commit(vp, offset, count, cr);
6570 
6571 	/*
6572 	 * If we got an error, then just unlock all of the pages
6573 	 * on the list.
6574 	 */
6575 	if (error) {
6576 		while (plist != NULL) {
6577 			pp = plist;
6578 			page_sub(&plist, pp);
6579 			page_unlock(pp);
6580 		}
6581 		return (error);
6582 	}
6583 	/*
6584 	 * We've tried as hard as we can to commit the data to stable
6585 	 * storage on the server.  We just unlock the pages and clear
6586 	 * the commit required state.  They will get freed later.
6587 	 */
6588 	while (plist != NULL) {
6589 		pp = plist;
6590 		page_sub(&plist, pp);
6591 		pp->p_fsdata = C_NOCOMMIT;
6592 		page_unlock(pp);
6593 	}
6594 
6595 	return (error);
6596 }
6597 
6598 static void
6599 nfs3_async_commit(vnode_t *vp, page_t *plist, offset3 offset, count3 count,
6600 	cred_t *cr)
6601 {
6602 	ASSERT(nfs_zone() == VTOMI(vp)->mi_zone);
6603 	(void) nfs3_sync_commit(vp, plist, offset, count, cr);
6604 }
6605 
6606 /* ARGSUSED */
6607 static int
6608 nfs3_setsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
6609 	caller_context_t *ct)
6610 {
6611 	int error;
6612 	mntinfo_t *mi;
6613 
6614 	mi = VTOMI(vp);
6615 
6616 	if (nfs_zone() != mi->mi_zone)
6617 		return (EIO);
6618 
6619 	if (mi->mi_flags & MI_ACL) {
6620 		error = acl_setacl3(vp, vsecattr, flag, cr);
6621 		if (mi->mi_flags & MI_ACL)
6622 			return (error);
6623 	}
6624 
6625 	return (ENOSYS);
6626 }
6627 
6628 /* ARGSUSED */
6629 static int
6630 nfs3_getsecattr(vnode_t *vp, vsecattr_t *vsecattr, int flag, cred_t *cr,
6631 	caller_context_t *ct)
6632 {
6633 	int error;
6634 	mntinfo_t *mi;
6635 
6636 	mi = VTOMI(vp);
6637 
6638 	if (nfs_zone() != mi->mi_zone)
6639 		return (EIO);
6640 
6641 	if (mi->mi_flags & MI_ACL) {
6642 		error = acl_getacl3(vp, vsecattr, flag, cr);
6643 		if (mi->mi_flags & MI_ACL)
6644 			return (error);
6645 	}
6646 
6647 	return (fs_fab_acl(vp, vsecattr, flag, cr, ct));
6648 }
6649 
6650 /* ARGSUSED */
6651 static int
6652 nfs3_shrlock(vnode_t *vp, int cmd, struct shrlock *shr, int flag, cred_t *cr,
6653 	caller_context_t *ct)
6654 {
6655 	int error;
6656 	struct shrlock nshr;
6657 	struct nfs_owner nfs_owner;
6658 	netobj lm_fh3;
6659 
6660 	if (nfs_zone() != VTOMI(vp)->mi_zone)
6661 		return (EIO);
6662 
6663 	/*
6664 	 * check for valid cmd parameter
6665 	 */
6666 	if (cmd != F_SHARE && cmd != F_UNSHARE && cmd != F_HASREMOTELOCKS)
6667 		return (EINVAL);
6668 
6669 	/*
6670 	 * Check access permissions
6671 	 */
6672 	if (cmd == F_SHARE &&
6673 	    (((shr->s_access & F_RDACC) && !(flag & FREAD)) ||
6674 	    ((shr->s_access & F_WRACC) && !(flag & FWRITE))))
6675 		return (EBADF);
6676 
6677 	/*
6678 	 * If the filesystem is mounted using local locking, pass the
6679 	 * request off to the local share code.
6680 	 */
6681 	if (VTOMI(vp)->mi_flags & MI_LLOCK)
6682 		return (fs_shrlock(vp, cmd, shr, flag, cr, ct));
6683 
6684 	switch (cmd) {
6685 	case F_SHARE:
6686 	case F_UNSHARE:
6687 		lm_fh3.n_len = VTOFH3(vp)->fh3_length;
6688 		lm_fh3.n_bytes = (char *)&(VTOFH3(vp)->fh3_u.data);
6689 
6690 		/*
6691 		 * If passed an owner that is too large to fit in an
6692 		 * nfs_owner it is likely a recursive call from the
6693 		 * lock manager client and pass it straight through.  If
6694 		 * it is not a nfs_owner then simply return an error.
6695 		 */
6696 		if (shr->s_own_len > sizeof (nfs_owner.lowner)) {
6697 			if (((struct nfs_owner *)shr->s_owner)->magic !=
6698 			    NFS_OWNER_MAGIC)
6699 				return (EINVAL);
6700 
6701 			if (error = lm4_shrlock(vp, cmd, shr, flag, &lm_fh3)) {
6702 				error = set_errno(error);
6703 			}
6704 			return (error);
6705 		}
6706 		/*
6707 		 * Remote share reservations owner is a combination of
6708 		 * a magic number, hostname, and the local owner
6709 		 */
6710 		bzero(&nfs_owner, sizeof (nfs_owner));
6711 		nfs_owner.magic = NFS_OWNER_MAGIC;
6712 		(void) strncpy(nfs_owner.hname, uts_nodename(),
6713 		    sizeof (nfs_owner.hname));
6714 		bcopy(shr->s_owner, nfs_owner.lowner, shr->s_own_len);
6715 		nshr.s_access = shr->s_access;
6716 		nshr.s_deny = shr->s_deny;
6717 		nshr.s_sysid = 0;
6718 		nshr.s_pid = ttoproc(curthread)->p_pid;
6719 		nshr.s_own_len = sizeof (nfs_owner);
6720 		nshr.s_owner = (caddr_t)&nfs_owner;
6721 
6722 		if (error = lm4_shrlock(vp, cmd, &nshr, flag, &lm_fh3)) {
6723 			error = set_errno(error);
6724 		}
6725 
6726 		break;
6727 
6728 	case F_HASREMOTELOCKS:
6729 		/*
6730 		 * NFS client can't store remote locks itself
6731 		 */
6732 		shr->s_access = 0;
6733 		error = 0;
6734 		break;
6735 
6736 	default:
6737 		error = EINVAL;
6738 		break;
6739 	}
6740 
6741 	return (error);
6742 }
6743