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